fix(ai): keep the output slot's item id when Copilot rotates it

This commit is contained in:
exe.dev user
2026-11-05 17:53:04 +00:00
Unverified
parent 85b1818138
commit 0a56c0c45e
2 changed files with 48 additions and 3 deletions
+4 -3
View File
@@ -985,8 +985,9 @@ const ITEM_ID_PREFIX: Readonly<Record<string, string>> = {
compaction: "cmp",
}
// An item without an id adopts the id already open in its output slot,
// otherwise it gets a locally minted one.
// An item adopts the id already open in its output slot, since GitHub Copilot
// sends a different id on every event, including the item's own done event.
// Otherwise it keeps its id or gets a locally minted one.
const resolveItem = (
state: Pick<ParserState, "outputItems">,
item: StreamItem,
@@ -994,8 +995,8 @@ const resolveItem = (
): OutputItem => ({
...item,
id:
item.id ??
(index === undefined ? undefined : state.outputItems[index]) ??
item.id ??
`${ITEM_ID_PREFIX[item.type] ?? "item"}_${crypto.randomUUID().replaceAll("-", "")}`,
})
@@ -400,6 +400,50 @@ describe("Open Responses-compatible route", () => {
}),
)
it.effect("keeps one item when GitHub Copilot sends a new item id on every event", () =>
Effect.gen(function* () {
const model = configure({ apiKey: "test-key", baseURL: "https://api.githubcopilot.test" }).model("gpt-5")
const response = yield* LLMClient.generate(ai.request({ model, prompt: "Say hello." })).pipe(
Effect.provide(
fixedResponse(
sseEvents(
{ type: "response.output_item.added", output_index: 0, item: { type: "reasoning", id: "rs-added" } },
{ type: "response.reasoning_summary_part.added", output_index: 0, item_id: "rs-part", summary_index: 0 },
{
type: "response.reasoning_summary_text.delta",
output_index: 0,
item_id: "rs-delta",
summary_index: 0,
delta: "Thinking",
},
{
type: "response.output_item.done",
output_index: 0,
item: { type: "reasoning", id: "rs-done", encrypted_content: "opaque", summary: [] },
},
{ type: "response.output_item.added", output_index: 1, item: { type: "message", id: "msg-added" } },
{ type: "response.output_text.delta", output_index: 1, item_id: "msg-delta", delta: "Hello" },
{ type: "response.output_item.done", output_index: 1, item: { type: "message", id: "msg-done" } },
{ type: "response.completed", response: { id: "resp_1" } },
),
),
),
)
expect(response.events.filter(LLMEvent.is.reasoningStart)).toHaveLength(1)
expect(response.message.content).toMatchObject([
{
type: "reasoning",
text: "Thinking",
providerMetadata: {
"openai-compatible": { itemId: "rs-added", reasoningEncryptedContent: "opaque" },
},
},
{ type: "text", text: "Hello" },
])
}),
)
it.effect("converts the GitHub Copilot billing on the completed event to USD", () =>
Effect.gen(function* () {
const model = configure({ apiKey: "test-key", baseURL: "https://api.githubcopilot.test" }).model("gpt-5")