response.test.ts 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import { describe, expect, test } from "bun:test"
  2. import { LLMEvent, LLMResponse } from "../src"
  3. const reduce = (events: ReadonlyArray<LLMEvent>) => events.reduce(LLMResponse.reduce, LLMResponse.empty())
  4. const finishEvents = (events: ReadonlyArray<LLMEvent>) => events.filter(LLMEvent.is.finish)
  5. describe("LLMResponse reducer", () => {
  6. test("assembles interleaved reasoning and text with end metadata", () => {
  7. const events = [
  8. LLMEvent.reasoningStart({ id: "r1" }),
  9. LLMEvent.reasoningDelta({ id: "r1", text: "I should " }),
  10. LLMEvent.textStart({ id: "t1" }),
  11. LLMEvent.reasoningDelta({ id: "r1", text: "compare..." }),
  12. LLMEvent.reasoningEnd({ id: "r1", providerMetadata: { anthropic: { signature: "sig" } } }),
  13. LLMEvent.textDelta({ id: "t1", text: "Answer" }),
  14. LLMEvent.textEnd({ id: "t1" }),
  15. LLMEvent.finish({ reason: "stop", usage: { outputTokens: 5 } }),
  16. ]
  17. const response = LLMResponse.fromEvents(events)
  18. expect(response?.finishReason).toBe("stop")
  19. expect(response?.usage).toMatchObject({ outputTokens: 5 })
  20. expect(response?.events).toEqual(events)
  21. expect(response?.events.map((event) => event.type)).toEqual([
  22. "reasoning-start",
  23. "reasoning-delta",
  24. "text-start",
  25. "reasoning-delta",
  26. "reasoning-end",
  27. "text-delta",
  28. "text-end",
  29. "finish",
  30. ])
  31. expect(finishEvents(response?.events ?? [])).toHaveLength(1)
  32. expect(response?.message.content).toEqual([
  33. {
  34. type: "reasoning",
  35. text: "I should compare...",
  36. providerMetadata: { anthropic: { signature: "sig" } },
  37. },
  38. { type: "text", text: "Answer" },
  39. ])
  40. })
  41. test("preserves partial content without completing a failed stream", () => {
  42. const state = reduce([LLMEvent.textStart({ id: "t1" }), LLMEvent.textDelta({ id: "t1", text: "partial" })])
  43. expect(LLMResponse.complete(state)).toBeUndefined()
  44. expect(state.message.content).toEqual([{ type: "text", text: "partial" }])
  45. })
  46. test("does not complete ended content without a terminal finish", () => {
  47. const state = reduce([
  48. LLMEvent.textStart({ id: "t1" }),
  49. LLMEvent.textDelta({ id: "t1", text: "partial" }),
  50. LLMEvent.textEnd({ id: "t1" }),
  51. ])
  52. expect(LLMResponse.complete(state)).toBeUndefined()
  53. expect(state.message.content).toEqual([{ type: "text", text: "partial" }])
  54. })
  55. test("uses terminal usage when present and keeps prior usage when finish omits it", () => {
  56. const withFinishUsage = LLMResponse.fromEvents([
  57. LLMEvent.stepFinish({ index: 0, reason: "stop", usage: { inputTokens: 3 } }),
  58. LLMEvent.finish({ reason: "stop", usage: { outputTokens: 2 } }),
  59. ])
  60. const withoutFinishUsage = LLMResponse.fromEvents([
  61. LLMEvent.stepFinish({ index: 0, reason: "stop", usage: { inputTokens: 3 } }),
  62. LLMEvent.finish({ reason: "stop" }),
  63. ])
  64. expect(withFinishUsage?.usage).toMatchObject({ outputTokens: 2 })
  65. expect(withoutFinishUsage?.usage).toMatchObject({ inputTokens: 3 })
  66. })
  67. test("assembles tool-call content only after the completed tool call event", () => {
  68. const pending = reduce([
  69. LLMEvent.toolInputStart({ id: "call_1", name: "lookup" }),
  70. LLMEvent.toolInputDelta({ id: "call_1", name: "lookup", text: '{"query"' }),
  71. ])
  72. expect(pending.message.content).toEqual([])
  73. expect(pending.toolInputs.call_1?.text).toBe('{"query"')
  74. const response = LLMResponse.fromEvents([
  75. ...pending.events,
  76. LLMEvent.toolInputDelta({ id: "call_1", name: "lookup", text: ':"weather"}' }),
  77. LLMEvent.toolInputEnd({ id: "call_1", name: "lookup" }),
  78. LLMEvent.toolCall({ id: "call_1", name: "lookup", input: { query: "weather" } }),
  79. LLMEvent.finish({ reason: "tool-calls" }),
  80. ])
  81. expect(response?.message.content).toEqual([
  82. { type: "tool-call", id: "call_1", name: "lookup", input: { query: "weather" } },
  83. ])
  84. })
  85. })