Skip to content

A desktop coding agent with free access to frontier models.

TypeScript

feat: keep chat title requests in uploads

ogsamrat8f05352d6716
diff --git a/apps/server/src/omnirush-gateway-broker.ts b/apps/server/src/omnirush-gateway-broker.ts
index 13e411b3..b31b804e 100644
--- a/apps/server/src/omnirush-gateway-broker.ts
+++ b/apps/server/src/omnirush-gateway-broker.ts
@@ -83,6 +83,11 @@ type BrokerOptions = {
sessionUploadBudget?: SessionUploadBudget;
/** Hears every sub-agent request moved to the main model (see subagentFallback). */
onSubagentFallback?: (event: SubagentModelFallbackEvent) => void;
+ /**
+ * A chat's title request once answered: the trace record ({ schema, kind: "title", route, model, status,
+ * request, text, completed }), the request as the engine sent it and the title as the model wrote it.
+ */
+ onModelRequest?: (sessionId: string, data: Record<string, unknown>) => void;
/**
* Whether a catalog model may run in a box (sandbox) chat or a native one
* (model-modes/). False: the request gets a 400 model_not_in_mode here and
@@ -178,6 +183,40 @@ export function answerCompleted(tail: string, contentType: string): boolean {
return /"type"\s*:\s*"(?:response\.completed|response\.done|message_stop)"|^data:\s*\[DONE\]/m.test(tail);
}
+/**
+ * The text a model reply carries, as the model wrote it: the Responses API's
+ * output_text deltas (else the completed response's output text), chat
+ * completions' content deltas, or a plain JSON reply's. Null when none.
+ */
+export function replyText(raw: string, contentType: string): string | null {
+ const fromResponse = (r: any): string | null => {
+ if (typeof r?.output_text === "string") return r.output_text;
+ const parts = (Array.isArray(r?.output) ? r.output : []).flatMap((item: any) => (Array.isArray(item?.content) ? item.content : []));
+ const joined = parts.filter((p: any) => typeof p?.text === "string").map((p: any) => p.text).join("");
+ if (joined) return joined;
+ const content = r?.choices?.[0]?.message?.content;
+ return typeof content === "string" ? content : null;
+ };
+ if (!contentType.includes("event-stream")) {
+ try { return fromResponse(JSON.parse(raw)); } catch { return null; }
+ }
+ let deltas: string | null = null;
+ let completed: string | null = null;
+ for (const line of raw.split(/\r?\n/)) {
+ if (!line.startsWith("data:")) continue;
+ let event: any;
+ try { event = JSON.parse(line.slice(5).trim()); } catch { continue; }
+ const chat = event?.choices?.[0]?.delta?.content;
+ if (event?.type === "response.output_text.delta" && typeof event.delta === "string") deltas = (deltas ?? "") + event.delta;
+ else if (typeof chat === "string") deltas = (deltas ?? "") + chat;
+ else if ((event?.type === "response.completed" || event?.type === "response.done") && event.response) completed = fromResponse(event.response);
+ }
+ return deltas ?? completed;
+}
+
+/** A title reply is a few words; one past this is not kept whole. */
+const MAX_TITLE_REPLY_BYTES = 1024 * 1024;
+
const TERMINAL_RESPONSE_EVENTS = new Set(["response.completed", "response.incomplete", "response.failed"]);
const TERMINAL_OR_ERROR_MARKER = /"(?:response\.(?:completed|incomplete|failed)|error)"/;
/** An event larger than this passes through unread; the marker scan below then decides terminality. */
@@ -1299,6 +1338,7 @@ export class OmniRushGatewayBroker {
private readonly log?: BrokerOptions["log"];
private readonly sessionUploadBudget: SessionUploadBudget;
private readonly onSubagentFallback?: BrokerOptions["onSubagentFallback"];
+ private readonly onModelRequest?: BrokerOptions["onModelRequest"];
private readonly subagentRetryDelayMs: number;
private readonly subagentModelRefused?: (model: string) => boolean;
private readonly modelInMode?: BrokerOptions["modelInMode"];
@@ -1388,6 +1428,7 @@ export class OmniRushGatewayBroker {
this.log = options.log;
this.sessionUploadBudget = options.sessionUploadBudget ?? SESSION_UPLOAD_BUDGET;
this.onSubagentFallback = options.onSubagentFallback;
+ this.onModelRequest = options.onModelRequest;
this.subagentRetryDelayMs = options.subagentRetryDelayMs ?? SUBAGENT_RETRY_DELAY_MS;
this.subagentModelRefused = options.subagentModelRefused;
this.modelInMode = options.modelInMode;
@@ -1536,7 +1577,7 @@ export class OmniRushGatewayBroker {
}),
})
: upstreamBody;
- return new Response(responseBody, {
+ return new Response(this.titleRecord(request, normalizedPath, body, response, responseBody), {
status: response.status,
statusText: response.statusText,
headers: responseHeaders(response.headers, requestId),
@@ -2224,6 +2265,37 @@ export class OmniRushGatewayBroker {
return withModel(body, asked.startsWith("omnirush/") ? `omnirush/${chat}` : chat, null) ?? body;
}
+ /**
+ * A chat's title request: its reply passes through unchanged and, once it ends, goes to onModelRequest
+ * with the request as the engine sent it (on the chat's model) and the title text.
+ */
+ private titleRecord(request: Request, path: string, body: ArrayBuffer | string | undefined, response: Response, stream: ReadableStream<Uint8Array> | null): ReadableStream<Uint8Array> | null {
+ const session = request.headers.get("x-omnirush-session-id");
+ if (!this.onModelRequest || !stream || !session || body === undefined || path !== "responses" || request.headers.get(TITLE_HEADER) !== "1") return stream;
+ const sent = this.titleOnChatModel(request, path, body) ?? body;
+ const contentType = response.headers.get("content-type") ?? "";
+ const decoder = new TextDecoder();
+ let reply = "";
+ let bytes = 0;
+ const record = (completed: boolean) => {
+ reply += decoder.decode();
+ let parsed: unknown = null;
+ try { parsed = JSON.parse(typeof sent === "string" ? sent : new TextDecoder().decode(sent)); } catch { /* not JSON: kept as null */ }
+ const model = isRecord(parsed) && typeof parsed.model === "string" ? parsed.model : null;
+ try {
+ this.onModelRequest?.(session, { schema: 1, kind: "title", route: path, model, status: response.status, request: parsed, text: replyText(reply, contentType), completed: completed && answerCompleted(reply.slice(-STRICT_TAIL_CHARS), contentType), ...(bytes >= MAX_TITLE_REPLY_BYTES ? { reply_truncated: true } : {}) });
+ } catch { /* a listener never breaks the request */ }
+ };
+ return stream.pipeThrough(new TransformStream<Uint8Array, Uint8Array>({
+ transform: (chunk, controller) => {
+ if (bytes < MAX_TITLE_REPLY_BYTES) { reply += decoder.decode(chunk, { stream: true }); bytes += chunk.byteLength; }
+ controller.enqueue(chunk);
+ },
+ // shortcut: a title the engine stops reading mid-stream is not recorded (no cancel hook in these types); add one if aborts show up.
+ flush: () => record(true),
+ }));
+ }
+
/**
* A chat's first agent request binds the chat (its pinned model, its mode,
* the box revision and the uploader's redaction policy); a sub-agent names
diff --git a/apps/server/src/server.ts b/apps/server/src/server.ts
index df32ddae..829f8652 100644
--- a/apps/server/src/server.ts
+++ b/apps/server/src/server.ts
@@ -1308,6 +1308,8 @@ export async function startServer(config: ServerConfig): Promise<ServeResult> {
...(config.omnirushGatewayCredentials?.onUpdateSignal ? { onUpdateSignal: config.omnirushGatewayCredentials.onUpdateSignal } : {}),
// Under the desktop: X-OmniRush-Client: gui/<app version> on every call.
...(config.appVersion?.trim() ? { clientVersion: config.appVersion.trim() } : {}),
+ // A chat's title request, as sent, and the title the model wrote: a `model.request` record of kind "title" (never a turn).
+ onModelRequest: (sessionId, data) => recordOpenSessionTrace(config, sessionId, "model.request", data),
onSubagentFallback: (event) => {
if (event.reason !== "refused_recently") subagentModelRefusals(config).mark(event.requested, event.reason);
recordSubagentModelFallback(config, event.rootSessionId, {
diff --git a/apps/server/src/title-capture.test.ts b/apps/server/src/title-capture.test.ts
new file mode 100644
index 00000000..7e876428
--- /dev/null
+++ b/apps/server/src/title-capture.test.ts
@@ -0,0 +1,70 @@
+// A chat's title request (x-omnirush-title, the title plugin marks it): the
+// broker hands the request as sent and the title text the model wrote to
+// onModelRequest; server.ts records it as a `model.request` trace event of
+// kind "title" (never a turn).
+import { describe, expect, test } from "bun:test";
+
+import { OmniRushGatewayBroker, replyText } from "./omnirush-gateway-broker.js";
+
+const CANARY = "canary-7f3a91c2 tidy the parser";
+const sse = (events: Array<Record<string, unknown>>) => events.map((e) => `event: ${e.type}\ndata: ${JSON.stringify(e)}\n\n`).join("");
+const TITLE_STREAM = sse([
+ { type: "response.created", response: { id: "r1" } },
+ { type: "response.output_text.delta", delta: "Tidy " },
+ { type: "response.output_text.delta", delta: "parser é \"q\"" },
+ { type: "response.completed", response: { id: "r1", output: [{ type: "message", content: [{ type: "output_text", text: "Tidy parser é \"q\"" }] }] } },
+]);
+
+function broker(records: Array<[string, Record<string, unknown>]>, reply: string, sent: string[] = []) {
+ return new OmniRushGatewayBroker({
+ credentials: { gatewayUrl: "https://gateway.example/omnirush/v1", accessToken: "access-token", refreshToken: "refresh-token" },
+ engineToken: "local-engine-token",
+ fetch: async (_input, init) => {
+ const raw = init?.body;
+ sent.push(typeof raw === "string" ? raw : new TextDecoder().decode(raw as ArrayBuffer));
+ return new Response(reply, { status: 200, headers: { "content-type": "text/event-stream" } });
+ },
+ onModelRequest: (sessionId, data) => records.push([sessionId, data]),
+ });
+}
+
+const request = { model: "omnirush/gpt-6-mini", instructions: "You are a title generator.", input: [{ role: "user", content: [{ type: "input_text", text: CANARY }] }], stream: true };
+const post = (headers: Record<string, string>) => new Request("http://127.0.0.1/omnirush-gateway/v1/responses", {
+ method: "POST",
+ headers: { Authorization: "Bearer local-engine-token", "Content-Type": "application/json", "x-omnirush-session-id": "ses_t", ...headers },
+ body: JSON.stringify(request),
+});
+
+describe("title capture", () => {
+ test("replyText: Responses deltas, the completed response, chat completions and plain JSON", () => {
+ expect(replyText(TITLE_STREAM, "text/event-stream")).toBe("Tidy parser é \"q\"");
+ expect(replyText(sse([{ type: "response.completed", response: { output: [{ content: [{ type: "output_text", text: "Only done" }] }] } }]), "text/event-stream")).toBe("Only done");
+ expect(replyText(`data: ${JSON.stringify({ choices: [{ delta: { content: "A" } }] })}\n\ndata: [DONE]\n\n`, "text/event-stream")).toBe("A");
+ expect(replyText(JSON.stringify({ output_text: "Plain" }), "application/json")).toBe("Plain");
+ expect(replyText("not json", "application/json")).toBeNull();
+ });
+
+ test("a title request goes to onModelRequest verbatim, on the chat's model; other requests do not", async () => {
+ const records: Array<[string, Record<string, unknown>]> = [];
+ const sent: string[] = [];
+ const gateway = broker(records, TITLE_STREAM, sent);
+ const response = await gateway.handle(post({ "x-omnirush-title": "1", "x-omnirush-chat-model": "gpt-6-astra" }), "responses");
+ // The engine gets the stream unchanged.
+ expect(await response.text()).toBe(TITLE_STREAM);
+ await (await gateway.handle(post({}), "responses")).text();
+ expect(records).toHaveLength(1);
+ const onChatModel = { ...request, model: "omnirush/gpt-6-astra" };
+ expect(records[0]).toEqual(["ses_t", { schema: 1, kind: "title", route: "responses", model: "omnirush/gpt-6-astra", status: 200, request: onChatModel, text: "Tidy parser é \"q\"", completed: true }]);
+ // What was recorded is what went up (the chat's model), the canary verbatim.
+ expect(JSON.parse(sent[0]!).input).toEqual(onChatModel.input);
+ });
+
+ test("a title stream cut short is recorded as not completed, with what came", async () => {
+ const records: Array<[string, Record<string, unknown>]> = [];
+ const gateway = broker(records, sse([{ type: "response.output_text.delta", delta: "Half" }]));
+ await (await gateway.handle(post({ "x-omnirush-title": "1" }), "responses")).text();
+ expect(records).toHaveLength(1);
+ expect(records[0]![1].text).toBe("Half");
+ expect(records[0]![1].completed).toBe(false);
+ });
+});