Skip to content

Commit 39a8a96

Browse files
author
wuzixuan06
committed
fix(ai): retain turn usage with agent Stop after rebase
1 parent 70cf921 commit 39a8a96

8 files changed

Lines changed: 188 additions & 100 deletions

File tree

‎electron/ai-edition/chat-service.toolloop.test.ts‎

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,20 @@ describe("stopped chat history", () => {
485485
controller.abort();
486486
args.sink.error("AbortError");
487487
args.sink.text("Late");
488-
return { text: "Partial reply", document: args.document, mutated: true, cancelled: true };
488+
return {
489+
text: "Partial reply",
490+
document: args.document,
491+
mutated: true,
492+
cancelled: true,
493+
usage: {
494+
inputTokens: 20,
495+
outputTokens: 5,
496+
totalTokens: 25,
497+
modelCallCount: 2,
498+
countedCallCount: 1,
499+
status: "partial",
500+
},
501+
};
489502
});
490503
const session = createSession("stop_history");
491504
const result = await runChat(
@@ -502,6 +515,8 @@ describe("stopped chat history", () => {
502515
cancelled: true,
503516
assistantMessage: {
504517
content: "Partial reply",
518+
usage: { totalTokens: 25, status: "partial" },
519+
durationMs: expect.any(Number),
505520
stopped: true,
506521
toolCalls: [{ name: "addTrim", summary: "Kept trim" }],
507522
},
@@ -512,7 +527,12 @@ describe("stopped chat history", () => {
512527
await flushChatPersistence();
513528
configureChatPersistence(root);
514529
const stored = selectSession("stop_history", session.id)!;
515-
expect(stored.messages[1]).toMatchObject({ stopped: true, content: "Partial reply" });
530+
expect(stored.messages[1]).toMatchObject({
531+
stopped: true,
532+
content: "Partial reply",
533+
usage: { totalTokens: 25, status: "partial" },
534+
durationMs: expect.any(Number),
535+
});
516536
expect(stored.messages[0].checkpointId).toBeNull();
517537
await runChat("stop_history", session.id, "continue", stubConfig(), fixtureDocument());
518538
expect(selectSession("stop_history", session.id)?.messages).toHaveLength(4);
@@ -527,6 +547,7 @@ describe("stopped chat history", () => {
527547
text: "",
528548
document: args.document,
529549
mutated: false,
550+
usage: createTurnUsageAccumulator().snapshot(),
530551
cancelled: true,
531552
}));
532553
const session = createSession("stop_before_text");

‎electron/ai-edition/chat-service.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,9 @@ export async function runChat(
451451
thinking: (delta: string) => {
452452
if (canEmit()) emit.thinking(delta);
453453
},
454-
usage: (usage: AgentTurnUsage) => { if (canEmit()) emit.usage(usage); },
454+
usage: (usage: AgentTurnUsage) => {
455+
if (canEmit()) emit.usage(usage);
456+
},
455457
toolStart: (name: string, args: unknown) => {
456458
if (canEmit()) emit.toolStart(name, args);
457459
},

‎electron/ai-edition/deep-agent/cancellation.test.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,9 @@ describe("agent cancellation", () => {
188188
],
189189
});
190190
chunk(res, {}, "tool_calls");
191+
res.write(
192+
`data: ${JSON.stringify({ id: "chat-test", object: "chat.completion.chunk", choices: [], usage: { prompt_tokens: 20, completion_tokens: 5, total_tokens: 25 } })}\n\n`,
193+
);
191194
end(res);
192195
} else {
193196
chunk(res, { role: "assistant", content: "Applied. " });
@@ -208,6 +211,12 @@ describe("agent cancellation", () => {
208211
controller.abort();
209212
const result = await run;
210213
expect(result.cancelled).toBe(true);
214+
expect(result.usage).toMatchObject({
215+
totalTokens: 25,
216+
modelCallCount: 2,
217+
countedCallCount: 1,
218+
status: "partial",
219+
});
211220
expect(result.document.project.vocabulary).toBe("Kept");
212221
expect(result.mutated).toBe(true);
213222
expect(events.document).toHaveBeenCalledOnce();

0 commit comments

Comments
 (0)