This commit is contained in:
dahyeon committed 2026-04-17 00:47:02 -07:00
1 parent 7fa293c9e8
commit ae44aa03db
67 files changed
+6164 -243

No files matched your search

+232
View File
@@ -4,6 +4,37 @@ import { getLogger } from "@obs/logger";
type GarnetRedisClient = ReturnType<typeof createClient>;
export interface StreamEntry {
deliveriesCounter?: number | undefined;
id: string;
message: Record<string, string>;
millisElapsedFromDelivery?: number | undefined;
}
export interface StreamReadResult {
messages: StreamEntry[];
name: string;
}
export interface StreamPendingEntry {
consumer: string;
deliveriesCounter: number;
id: string;
millisecondsSinceLastDelivery: number;
}
interface RawStreamMessage {
deliveriesCounter?: number | undefined;
id: string;
message: unknown;
millisElapsedFromDelivery?: number | undefined;
}
interface RawStreamReadResult {
messages: RawStreamMessage[];
name: string;
}
function normalizeGarnetUrl(endpoint: string): string {
const trimmed = endpoint.trim();
if (!trimmed) {
@@ -17,6 +48,29 @@ function normalizeGarnetUrl(endpoint: string): string {
return `redis://${trimmed}`;
}
function normalizeStreamFields(
fields: unknown,
): Record<string, string> {
if (fields instanceof Map) {
return Object.fromEntries(
[...fields.entries()].map(([key, value]) => [
String(key),
String(value),
]),
);
}
if (typeof fields === "object" && fields !== null) {
return Object.fromEntries(
Object.entries(fields as Record<string, unknown>).map(
([key, value]) => [key, String(value)],
),
);
}
return {};
}
export class GarnetClient {
private client: GarnetRedisClient | null = null;
private readonly logger = getLogger();
@@ -98,6 +152,27 @@ export class GarnetClient {
await client.set(key, value);
}
async setStringIfAbsent(
key: string,
value: string,
ttlSeconds?: number,
): Promise<boolean> {
const client = this.getClient();
const setOptions: {
EX?: number;
NX: true;
} = {
NX: true,
};
if (ttlSeconds && ttlSeconds > 0) {
setOptions.EX = ttlSeconds;
}
const result = await client.set(key, value, {
...setOptions,
});
return result === "OK";
}
async delete(key: string): Promise<number> {
return this.getClient().del(key);
}
@@ -132,6 +207,163 @@ export class GarnetClient {
return this.getClient().xAdd(key, "*", fields, trimOptions);
}
async createConsumerGroup(
key: string,
group: string,
startId = "$",
): Promise<void> {
await this.getClient().xGroupCreate(key, group, startId, {
MKSTREAM: true,
});
}
async readGroup(
key: string,
group: string,
consumer: string,
startId: string,
options?: {
blockMs?: number | undefined;
count?: number | undefined;
},
): Promise<StreamReadResult[] | null> {
const readOptions: {
BLOCK?: number;
COUNT?: number;
} = {};
if (options?.blockMs !== undefined) {
readOptions.BLOCK = options.blockMs;
}
if (options?.count !== undefined) {
readOptions.COUNT = options.count;
}
const reply = await this.getClient().xReadGroup(
group,
consumer,
{
key,
id: startId,
},
readOptions,
);
if (!reply) {
return null;
}
return (reply as RawStreamReadResult[]).map((stream) => ({
name: String(stream.name),
messages: stream.messages.map((message) => ({
deliveriesCounter:
typeof message.deliveriesCounter === "number"
? message.deliveriesCounter
: undefined,
id: String(message.id),
message: normalizeStreamFields(message.message),
millisElapsedFromDelivery:
typeof message.millisElapsedFromDelivery === "number"
? message.millisElapsedFromDelivery
: undefined,
})),
}));
}
async acknowledgeStream(
key: string,
group: string,
ids: string | string[],
): Promise<number> {
return this.getClient().xAck(key, group, ids);
}
async getPendingStreamRange(
key: string,
group: string,
count: number,
options?: {
consumer?: string | undefined;
idleMs?: number | undefined;
start?: string | undefined;
end?: string | undefined;
},
): Promise<StreamPendingEntry[]> {
const pendingOptions: {
IDLE?: number;
consumer?: string;
} = {};
if (options?.idleMs !== undefined) {
pendingOptions.IDLE = options.idleMs;
}
if (options?.consumer !== undefined) {
pendingOptions.consumer = options.consumer;
}
const reply = await this.getClient().xPendingRange(
key,
group,
options?.start ?? "-",
options?.end ?? "+",
count,
pendingOptions,
);
return reply.map((entry) => ({
consumer: String(entry.consumer),
deliveriesCounter: entry.deliveriesCounter,
id: String(entry.id),
millisecondsSinceLastDelivery: entry.millisecondsSinceLastDelivery,
}));
}
async claimPendingStream(
key: string,
group: string,
consumer: string,
minIdleTimeMs: number,
ids: string | string[],
options?: {
retryCount?: number | undefined;
},
): Promise<StreamEntry[]> {
const claimOptions: {
RETRYCOUNT?: number;
} = {};
if (options?.retryCount !== undefined) {
claimOptions.RETRYCOUNT = options.retryCount;
}
const claimed = await this.getClient().xClaim(
key,
group,
consumer,
minIdleTimeMs,
ids,
claimOptions,
);
return claimed.flatMap((message) => {
if (!message) {
return [];
}
return [
{
deliveriesCounter:
typeof message.deliveriesCounter === "number"
? message.deliveriesCounter
: undefined,
id: String(message.id),
message: normalizeStreamFields(message.message),
millisElapsedFromDelivery:
typeof message.millisElapsedFromDelivery === "number"
? message.millisElapsedFromDelivery
: undefined,
},
];
});
}
async scanKeys(pattern: string): Promise<string[]> {
const keys: string[] = [];
+19
View File
@@ -0,0 +1,19 @@
import type { EvalReport } from "@eval/types";
export const AGENT_REQUEST_SUBJECT = "lolympus-agent.request";
export const AGENT_ALERT_SUBJECT = "lolympus-agent.alert";
export const AGENT_EVAL_REPORT_SUBJECT = "lolympus-agent.eval.report";
export interface AlertPayload {
kind: "latency" | "eval-score-drop" | "llm-error-rate" | "endpoint-down";
metric: string;
severity: "info" | "warn" | "critical";
suggestedAction?: string | undefined;
threshold: number;
timestamp: string;
traceId?: string | undefined;
value: number;
window: string;
}
export type EvalReportPayload = EvalReport;