Skip to content

Commit 344ef96

Browse files
committed
fix(task): address CodeRabbit review feedback for session title generation
1 parent fbf4c73 commit 344ef96

29 files changed

Lines changed: 1105 additions & 67 deletions

‎packages/types/src/history.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export const historyItemSchema = z.object({
3434
ts: z.number(),
3535
task: z.string(),
3636
title: z.string().optional(), // User-assigned display name; `task` keeps the original prompt
37+
titleCleared: z.boolean().optional(), // Persisted manual clear: blocks an automatically generated title
3738
tokensIn: z.number(),
3839
tokensOut: z.number(),
3940
cacheWrites: z.number().optional(),

‎src/api/providers/__tests__/base-openai-compatible-provider.spec.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,26 @@ describe("BaseOpenAiCompatibleProvider", () => {
355355
expect(mockCreate.mock.calls[0][0].max_tokens).toBeUndefined()
356356
})
357357

358+
it("forwards the abort signal and deadline to the completion request", async () => {
359+
mockCreate.mockResolvedValueOnce({ choices: [{ message: { content: "ok" } }] })
360+
const { signal } = new AbortController()
361+
362+
await new TestOpenAiCompatibleProvider("test-api-key", {}).completePrompt("hello", {
363+
abortSignal: signal,
364+
timeoutMs: 5_000,
365+
})
366+
367+
expect(mockCreate).toHaveBeenLastCalledWith(expect.anything(), { signal, timeout: 5_000 })
368+
})
369+
370+
it("sends a single-argument request when no cancellation or deadline is given", async () => {
371+
mockCreate.mockResolvedValueOnce({ choices: [{ message: { content: "ok" } }] })
372+
373+
await new TestOpenAiCompatibleProvider("test-api-key", {}).completePrompt("hello", { timeoutMs: 0 })
374+
375+
expect(mockCreate).toHaveBeenLastCalledWith(expect.anything())
376+
})
377+
358378
it("completePrompt sends the custom model id verbatim to the API", async () => {
359379
mockCreate.mockResolvedValueOnce({ choices: [{ message: { content: "ok" } }] })
360380

‎src/api/providers/__tests__/kenari.spec.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,25 @@ describe("KenariHandler", () => {
345345
await expect(handler.completePrompt("ping")).rejects.toThrow("Kenari completion error: boom")
346346
})
347347

348+
it("forwards the abort signal and deadline to the completion request", async () => {
349+
mockCreate.mockResolvedValue({ choices: [{ message: { content: "ok" } }] })
350+
const handler = new KenariHandler(mockOptions)
351+
const { signal } = new AbortController()
352+
353+
await handler.completePrompt("ping", { abortSignal: signal, timeoutMs: 5_000 })
354+
355+
expect(mockCreate).toHaveBeenLastCalledWith(expect.anything(), { signal, timeout: 5_000 })
356+
})
357+
358+
it("sends a single-argument request when no cancellation or deadline is given", async () => {
359+
mockCreate.mockResolvedValue({ choices: [{ message: { content: "ok" } }] })
360+
const handler = new KenariHandler(mockOptions)
361+
362+
await handler.completePrompt("ping", { timeoutMs: Number.NaN })
363+
364+
expect(mockCreate).toHaveBeenLastCalledWith(expect.anything())
365+
})
366+
348367
it("re-throws non-Error rejections unchanged", async () => {
349368
mockCreate.mockRejectedValue("string failure")
350369
const handler = new KenariHandler(mockOptions)

‎src/api/providers/__tests__/openai.spec.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -942,6 +942,25 @@ describe("OpenAiHandler", () => {
942942
})
943943

944944
describe("completePrompt", () => {
945+
it("forwards the abort signal and deadline to the completion request", async () => {
946+
const { signal } = new AbortController()
947+
948+
await handler.completePrompt("Test prompt", { abortSignal: signal, timeoutMs: 5_000 })
949+
950+
expect(mockCreate).toHaveBeenLastCalledWith(expect.anything(), { signal, timeout: 5_000 })
951+
})
952+
953+
it("does not start a request when the caller already aborted", async () => {
954+
const controller = new AbortController()
955+
controller.abort()
956+
mockCreate.mockClear()
957+
958+
await expect(handler.completePrompt("Test prompt", { abortSignal: controller.signal })).rejects.toThrow(
959+
"This operation was aborted",
960+
)
961+
expect(mockCreate).not.toHaveBeenCalled()
962+
})
963+
945964
it("should complete prompt successfully", async () => {
946965
const result = await handler.completePrompt("Test prompt")
947966
expect(result).toBe("Test response")

‎src/api/providers/__tests__/openrouter.spec.ts‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -973,7 +973,42 @@ describe("OpenRouterHandler", () => {
973973
})
974974
})
975975

976+
// Installs a typed completion double without widening the client to `any`.
977+
const installMockClient = (create: ReturnType<typeof vitest.fn>) => {
978+
;(OpenAI as unknown as { prototype: { chat: unknown } }).prototype.chat = {
979+
completions: { create },
980+
}
981+
}
982+
976983
describe("completePrompt", () => {
984+
it("forwards the abort signal and deadline to the completion request", async () => {
985+
const handler = new OpenRouterHandler(mockOptions)
986+
const { signal } = new AbortController()
987+
const mockCreate = vitest.fn().mockResolvedValue({ choices: [{ message: { content: "ok" } }] })
988+
installMockClient(mockCreate)
989+
990+
await handler.completePrompt("test prompt", { abortSignal: signal, timeoutMs: 5_000 })
991+
992+
expect(mockCreate).toHaveBeenCalledWith(expect.objectContaining({ model: mockOptions.openRouterModelId }), {
993+
headers: { "x-anthropic-beta": "fine-grained-tool-streaming-2025-05-14" },
994+
signal,
995+
timeout: 5_000,
996+
})
997+
})
998+
999+
it("does not start a request when the caller already aborted", async () => {
1000+
const handler = new OpenRouterHandler(mockOptions)
1001+
const mockCreate = vitest.fn()
1002+
installMockClient(mockCreate)
1003+
const controller = new AbortController()
1004+
controller.abort()
1005+
1006+
await expect(
1007+
handler.completePrompt("test prompt", { abortSignal: controller.signal }),
1008+
).rejects.toMatchObject({ name: "AbortError" })
1009+
expect(mockCreate).not.toHaveBeenCalled()
1010+
})
1011+
9771012
it("returns correct response", async () => {
9781013
const handler = new OpenRouterHandler(mockOptions)
9791014
const mockResponse = { choices: [{ message: { content: "test completion" } }] }

‎src/api/providers/__tests__/poe.spec.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,29 @@ describe("PoeHandler", () => {
402402
})
403403

404404
describe("completePrompt", () => {
405+
it("forwards the abort signal and deadline to the generation", async () => {
406+
const handler = new PoeHandler({ poeApiKey: "key", apiModelId: "openai/gpt-4o" })
407+
mockGenerateText.mockResolvedValue({ text: "generated response" })
408+
const { signal } = new AbortController()
409+
410+
await handler.completePrompt("complete this", { abortSignal: signal, timeoutMs: 5_000 })
411+
412+
const options = mockGenerateText.mock.calls[0][0]
413+
// The deadline is merged into a per-request signal that still follows the caller's stop.
414+
expect(options.abortSignal).not.toBe(signal)
415+
expect(options.abortSignal?.aborted).toBe(false)
416+
})
417+
418+
it("does not start a generation when the caller already aborted", async () => {
419+
const handler = new PoeHandler({ poeApiKey: "key", apiModelId: "openai/gpt-4o" })
420+
mockGenerateText.mockClear()
421+
const controller = new AbortController()
422+
controller.abort()
423+
424+
await expect(handler.completePrompt("complete this", { abortSignal: controller.signal })).rejects.toThrow()
425+
expect(mockGenerateText).not.toHaveBeenCalled()
426+
})
427+
405428
it("returns generated text", async () => {
406429
const handler = new PoeHandler({ poeApiKey: "key", apiModelId: "openai/gpt-4o" })
407430

‎src/api/providers/__tests__/requesty.spec.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,25 @@ describe("RequestyHandler", () => {
634634
})
635635
})
636636

637+
it("forwards the abort signal and deadline to the completion request", async () => {
638+
const handler = new RequestyHandler(mockOptions)
639+
mockCreate.mockResolvedValue({ choices: [{ message: { content: "ok" } }] })
640+
const { signal } = new AbortController()
641+
642+
await handler.completePrompt("test prompt", { abortSignal: signal, timeoutMs: 5_000 })
643+
644+
expect(mockCreate).toHaveBeenLastCalledWith(expect.anything(), { signal, timeout: 5_000 })
645+
})
646+
647+
it("sends a single-argument request when no cancellation or deadline is given", async () => {
648+
const handler = new RequestyHandler(mockOptions)
649+
mockCreate.mockResolvedValue({ choices: [{ message: { content: "ok" } }] })
650+
651+
await handler.completePrompt("test prompt", { timeoutMs: 0 })
652+
653+
expect(mockCreate).toHaveBeenLastCalledWith(expect.anything())
654+
})
655+
637656
it("omits temperature for Claude Fable 5 in completePrompt", async () => {
638657
const handler = new RequestyHandler(
639658
makeApiHandlerOptions({

‎src/api/providers/__tests__/unbound.spec.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,29 @@ describe("UnboundHandler", () => {
204204
)
205205
})
206206

207+
type OpenAiClientStub = { chat: { completions: { create: ReturnType<typeof vi.fn> } } }
208+
209+
it("forwards the abort signal and deadline to the completion request", async () => {
210+
const mockCreate = (OpenAI as unknown as () => OpenAiClientStub)().chat.completions.create
211+
mockCreate.mockResolvedValue({ choices: [{ message: { content: "ok" } }] })
212+
const handler = new UnboundHandler({ unboundApiKey: "test-key", unboundModelId: "openai/gpt-4o" })
213+
const { signal } = new AbortController()
214+
215+
await handler.completePrompt("Write a haiku", { abortSignal: signal, timeoutMs: 5_000 })
216+
217+
expect(mockCreate).toHaveBeenLastCalledWith(expect.anything(), { signal, timeout: 5_000 })
218+
})
219+
220+
it("sends a single-argument request when no cancellation or deadline is given", async () => {
221+
const mockCreate = (OpenAI as unknown as () => OpenAiClientStub)().chat.completions.create
222+
mockCreate.mockResolvedValue({ choices: [{ message: { content: "ok" } }] })
223+
const handler = new UnboundHandler({ unboundApiKey: "test-key", unboundModelId: "openai/gpt-4o" })
224+
225+
await handler.completePrompt("Write a haiku", { timeoutMs: -1 })
226+
227+
expect(mockCreate).toHaveBeenLastCalledWith(expect.anything())
228+
})
229+
207230
describe("model catalog", () => {
208231
it("reports its cache scope", () => {
209232
const handler = new UnboundHandler({ unboundApiKey: "test-key", unboundModelId: "openai/gpt-4o" })

‎src/api/providers/__tests__/vercel-ai-gateway.spec.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,23 @@ describe("VercelAiGatewayHandler", () => {
763763
}))
764764
})
765765

766+
it("forwards the abort signal and deadline to the completion request", async () => {
767+
const handler = new VercelAiGatewayHandler(mockOptions)
768+
const { signal } = new AbortController()
769+
770+
await handler.completePrompt("Complete this: Hello", { abortSignal: signal, timeoutMs: 5_000 })
771+
772+
expect(mockCreate).toHaveBeenLastCalledWith(expect.anything(), { signal, timeout: 5_000 })
773+
})
774+
775+
it("sends a single-argument request when no cancellation or deadline is given", async () => {
776+
const handler = new VercelAiGatewayHandler(mockOptions)
777+
778+
await handler.completePrompt("Complete this: Hello", { timeoutMs: Number.POSITIVE_INFINITY })
779+
780+
expect(mockCreate).toHaveBeenLastCalledWith(expect.anything())
781+
})
782+
766783
it("completes prompt correctly", async () => {
767784
const handler = new VercelAiGatewayHandler(mockOptions)
768785
const prompt = "Complete this: Hello"

‎src/api/providers/__tests__/zoo-gateway.spec.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,23 @@ describe("ZooGatewayHandler", () => {
506506
}))
507507
})
508508

509+
it("forwards the abort signal and deadline to the completion request", async () => {
510+
const handler = new ZooGatewayHandler(mockOptions)
511+
const { signal } = new AbortController()
512+
513+
await handler.completePrompt("Complete this: Hello", { abortSignal: signal, timeoutMs: 5_000 })
514+
515+
expect(mockCreate).toHaveBeenLastCalledWith(expect.anything(), { signal, timeout: 5_000 })
516+
})
517+
518+
it("sends a single-argument request when no cancellation or deadline is given", async () => {
519+
const handler = new ZooGatewayHandler(mockOptions)
520+
521+
await handler.completePrompt("Complete this: Hello", { timeoutMs: -1 })
522+
523+
expect(mockCreate).toHaveBeenLastCalledWith(expect.anything())
524+
})
525+
509526
it("returns completion text from the gateway", async () => {
510527
const handler = new ZooGatewayHandler(mockOptions)
511528

0 commit comments

Comments
 (0)