Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/runtime/local-opentui-bridge.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,21 @@ describe("LocalOpenTuiTranscriptBridge", () => {
});

describe("streaming order", () => {
it("marks Markdown entries as streaming until the final assistant message", () => {
const bridge = createBridge();
const ui = bridge.createInteractiveUiFactory()(createUiFactoryInput());

bridge.submitUserTurn({ content: "hello" });
ui.onModelStreamReset();
ui.onModelTextDelta({ text: "```ts" });

expect(bridge.getEntries().at(-1)?.streaming).toBe(true);

ui.onAssistantMessage({ text: "```ts\nconst answer = 42;\n```" });

expect(bridge.getEntries().at(-1)?.streaming).toBe(false);
});

it("keeps tool-call entries after the assistant text that triggered them", () => {
const bridge = createBridge();
const ui = bridge.createInteractiveUiFactory()(createUiFactoryInput());
Expand Down
20 changes: 20 additions & 0 deletions src/runtime/local-opentui-bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -445,17 +445,20 @@ export class LocalOpenTuiTranscriptBridge implements StepCliTuiTranscriptControl
role: "assistant",
caption: null,
content: text,
streaming: true,
});
return;
}

this.updateLocalEntry(this.currentAssistantEntryId, (entry) => ({
...entry,
content: `${entry.content}${text}`,
streaming: true,
}));
},
onModelToolCall: ({ toolName, rawArgs }) => {
this.discardEmptyAssistantEntry();
this.markCurrentAssistantEntryComplete();
// Reset the streaming assistant entry ID so that any text deltas
// arriving *after* this tool call (within the same streaming
// response) create a new assistant entry positioned after the
Expand Down Expand Up @@ -485,6 +488,7 @@ export class LocalOpenTuiTranscriptBridge implements StepCliTuiTranscriptControl
},
onToolStart: (info) => {
this.discardEmptyAssistantEntry();
this.markCurrentAssistantEntryComplete();
this.currentAssistantEntryId = null;
const toolName = readString((info as { toolName?: unknown }).toolName);
const entryId = this.findCurrentToolEntryId(toolName);
Expand Down Expand Up @@ -578,6 +582,7 @@ export class LocalOpenTuiTranscriptBridge implements StepCliTuiTranscriptControl
this.updateLocalEntry(this.currentAssistantEntryId, (entry) => ({
...entry,
content: message,
streaming: false,
}));
this.currentAssistantEntryId = null;
return;
Expand All @@ -592,6 +597,7 @@ export class LocalOpenTuiTranscriptBridge implements StepCliTuiTranscriptControl
this.updateLocalEntry(lastStreamingId, (entry) => ({
...entry,
content: message,
streaming: false,
}));
return;
}
Expand All @@ -601,6 +607,7 @@ export class LocalOpenTuiTranscriptBridge implements StepCliTuiTranscriptControl
role: "assistant",
caption: null,
content: message,
streaming: false,
});
}

Expand All @@ -614,6 +621,17 @@ export class LocalOpenTuiTranscriptBridge implements StepCliTuiTranscriptControl
});
}

private markCurrentAssistantEntryComplete(): void {
if (!this.currentAssistantEntryId) {
return;
}

this.updateLocalEntry(this.currentAssistantEntryId, (entry) => ({
...entry,
streaming: false,
}));
}

private appendLocalEntry(entry: LocalTranscriptEntry): void {
if (
entry.role !== "user" &&
Expand Down Expand Up @@ -801,6 +819,8 @@ function stripLocalTranscriptEntry(
role: entry.role,
caption: entry.caption,
content: entry.content,
hidden: entry.hidden,
streaming: entry.streaming,
};
}

Expand Down
Loading
Loading