mirror of
https://github.com/anomalyco/opencode.git
synced 2026-07-13 15:28:29 +00:00
Co-authored-by: Brendan Allan <14191578+Brendonovich@users.noreply.github.com> Co-authored-by: Kit Langton <kit.langton@gmail.com> Co-authored-by: opencode-agent[bot] <opencode-agent[bot]@users.noreply.github.com> Co-authored-by: Affan Ali <93028901+affanali2k3@users.noreply.github.com> Co-authored-by: affanali2k3 <affanalikhanxx@gmail.com> Co-authored-by: Frank <frank@anoma.ly> Co-authored-by: opencode-agent[bot] <219766164+opencode-agent[bot]@users.noreply.github.com> Co-authored-by: 𝓛𝓲𝓽𝓽𝓵𝓮 𝓕𝓻𝓪𝓷𝓴 <little-frank@opencord.local> Co-authored-by: Aiden Cline <63023139+rekram1-node@users.noreply.github.com> Co-authored-by: Jay V <air@live.ca> Co-authored-by: Dax Raad <d@ironbay.co> Co-authored-by: Aarav Sareen <96787824+arvsrn@users.noreply.github.com> Co-authored-by: OpeOginni <107570612+OpeOginni@users.noreply.github.com> Co-authored-by: Luke Parker <10430890+Hona@users.noreply.github.com> Co-authored-by: Ben Guthrie <benjee.012@gmail.com> Co-authored-by: Dax <mail@thdxr.com> Co-authored-by: Filip <34747899+neriousy@users.noreply.github.com> Co-authored-by: Max Anderson <max.a.anderson95@gmail.com> Co-authored-by: Brendan Allan <git@brendonovich.dev> Co-authored-by: Jack <jack@anoma.ly> Co-authored-by: Shoubhit Dash <shoubhit2005@gmail.com> Co-authored-by: Dustin Deus <deusdustin@gmail.com> Co-authored-by: starptech <starptech@starptechs-MBP.fritz.box> Co-authored-by: Aiden Cline <aidenpcline@gmail.com> Co-authored-by: usrnk1 <7547651+usrnk1@users.noreply.github.com> Co-authored-by: Jay <53023+jayair@users.noreply.github.com> Co-authored-by: runvip <164729189+runvip@users.noreply.github.com> Co-authored-by: opencode <opencode@sst.dev> Co-authored-by: Julian Coy <julian@ex-machina.co> Co-authored-by: Vladimir Glafirov <vglafirov@gitlab.com>
98 lines
3.9 KiB
TypeScript
98 lines
3.9 KiB
TypeScript
import { describe, expect, test } from "bun:test"
|
|
import { LLMEvent, LLMResponse } from "../src"
|
|
|
|
const reduce = (events: ReadonlyArray<LLMEvent>) => events.reduce(LLMResponse.reduce, LLMResponse.empty())
|
|
const finishEvents = (events: ReadonlyArray<LLMEvent>) => events.filter(LLMEvent.is.finish)
|
|
|
|
describe("LLMResponse reducer", () => {
|
|
test("assembles interleaved reasoning and text with end metadata", () => {
|
|
const events = [
|
|
LLMEvent.reasoningStart({ id: "r1" }),
|
|
LLMEvent.reasoningDelta({ id: "r1", text: "I should " }),
|
|
LLMEvent.textStart({ id: "t1" }),
|
|
LLMEvent.reasoningDelta({ id: "r1", text: "compare..." }),
|
|
LLMEvent.reasoningEnd({ id: "r1", providerMetadata: { anthropic: { signature: "sig" } } }),
|
|
LLMEvent.textDelta({ id: "t1", text: "Answer" }),
|
|
LLMEvent.textEnd({ id: "t1" }),
|
|
LLMEvent.finish({ reason: "stop", usage: { outputTokens: 5 } }),
|
|
]
|
|
const response = LLMResponse.fromEvents(events)
|
|
|
|
expect(response?.finishReason).toBe("stop")
|
|
expect(response?.usage).toMatchObject({ outputTokens: 5 })
|
|
expect(response?.events).toEqual(events)
|
|
expect(response?.events.map((event) => event.type)).toEqual([
|
|
"reasoning-start",
|
|
"reasoning-delta",
|
|
"text-start",
|
|
"reasoning-delta",
|
|
"reasoning-end",
|
|
"text-delta",
|
|
"text-end",
|
|
"finish",
|
|
])
|
|
expect(finishEvents(response?.events ?? [])).toHaveLength(1)
|
|
expect(response?.message.content).toEqual([
|
|
{
|
|
type: "reasoning",
|
|
text: "I should compare...",
|
|
providerMetadata: { anthropic: { signature: "sig" } },
|
|
},
|
|
{ type: "text", text: "Answer" },
|
|
])
|
|
})
|
|
|
|
test("preserves partial content without completing a failed stream", () => {
|
|
const state = reduce([LLMEvent.textStart({ id: "t1" }), LLMEvent.textDelta({ id: "t1", text: "partial" })])
|
|
|
|
expect(LLMResponse.complete(state)).toBeUndefined()
|
|
expect(state.message.content).toEqual([{ type: "text", text: "partial" }])
|
|
})
|
|
|
|
test("does not complete ended content without a terminal finish", () => {
|
|
const state = reduce([
|
|
LLMEvent.textStart({ id: "t1" }),
|
|
LLMEvent.textDelta({ id: "t1", text: "partial" }),
|
|
LLMEvent.textEnd({ id: "t1" }),
|
|
])
|
|
|
|
expect(LLMResponse.complete(state)).toBeUndefined()
|
|
expect(state.message.content).toEqual([{ type: "text", text: "partial" }])
|
|
})
|
|
|
|
test("uses terminal usage when present and keeps prior usage when finish omits it", () => {
|
|
const withFinishUsage = LLMResponse.fromEvents([
|
|
LLMEvent.stepFinish({ index: 0, reason: "stop", usage: { inputTokens: 3 } }),
|
|
LLMEvent.finish({ reason: "stop", usage: { outputTokens: 2 } }),
|
|
])
|
|
const withoutFinishUsage = LLMResponse.fromEvents([
|
|
LLMEvent.stepFinish({ index: 0, reason: "stop", usage: { inputTokens: 3 } }),
|
|
LLMEvent.finish({ reason: "stop" }),
|
|
])
|
|
|
|
expect(withFinishUsage?.usage).toMatchObject({ outputTokens: 2 })
|
|
expect(withoutFinishUsage?.usage).toMatchObject({ inputTokens: 3 })
|
|
})
|
|
|
|
test("assembles tool-call content only after the completed tool call event", () => {
|
|
const pending = reduce([
|
|
LLMEvent.toolInputStart({ id: "call_1", name: "lookup" }),
|
|
LLMEvent.toolInputDelta({ id: "call_1", name: "lookup", text: '{"query"' }),
|
|
])
|
|
|
|
expect(pending.message.content).toEqual([])
|
|
expect(pending.toolInputs.call_1?.text).toBe('{"query"')
|
|
|
|
const response = LLMResponse.fromEvents([
|
|
...pending.events,
|
|
LLMEvent.toolInputDelta({ id: "call_1", name: "lookup", text: ':"weather"}' }),
|
|
LLMEvent.toolInputEnd({ id: "call_1", name: "lookup" }),
|
|
LLMEvent.toolCall({ id: "call_1", name: "lookup", input: { query: "weather" } }),
|
|
LLMEvent.finish({ reason: "tool-calls" }),
|
|
])
|
|
|
|
expect(response?.message.content).toEqual([
|
|
{ type: "tool-call", id: "call_1", name: "lookup", input: { query: "weather" } },
|
|
])
|
|
})
|
|
})
|