mirror of
https://github.com/Termix-SSH/Termix.git
synced 2026-11-01 08:05:23 +00:00
fix(ai): stop streams getting cut off and keep tool steps across messages
This commit is contained in:
1 parent
c5b2b880b6
commit
b4457b6121
17 files changed
+726
-254
No files matched your search
@@ -0,0 +1,116 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { toChatHistory, toStoredMessage } from "../../src/backend/history.js";
|
||||
|
||||
const calls = [
|
||||
{ id: "c1", name: "list_hosts", arguments: {} },
|
||||
{ id: "c2", name: "get_host", arguments: { hostId: 1 } },
|
||||
];
|
||||
|
||||
describe("toStoredMessage", () => {
|
||||
it("keeps the answered call on a tool row", () => {
|
||||
expect(
|
||||
toStoredMessage({
|
||||
role: "tool",
|
||||
content: "{}",
|
||||
toolCallId: "c1",
|
||||
toolName: "list_hosts",
|
||||
}),
|
||||
).toEqual({
|
||||
role: "tool",
|
||||
content: "{}",
|
||||
toolCalls: JSON.stringify([{ id: "c1", name: "list_hosts" }]),
|
||||
});
|
||||
});
|
||||
|
||||
it("stores no tool calls for a plain reply", () => {
|
||||
expect(toStoredMessage({ role: "assistant", content: "hi" })).toEqual({
|
||||
role: "assistant",
|
||||
content: "hi",
|
||||
toolCalls: null,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("toChatHistory", () => {
|
||||
it("round-trips a full run", () => {
|
||||
const rows = [
|
||||
{ role: "user", content: "check", toolCalls: null },
|
||||
toStoredMessage({ role: "assistant", content: "", toolCalls: calls }),
|
||||
toStoredMessage({
|
||||
role: "tool",
|
||||
content: '{"hosts":[]}',
|
||||
toolCallId: "c1",
|
||||
toolName: "list_hosts",
|
||||
}),
|
||||
toStoredMessage({
|
||||
role: "tool",
|
||||
content: '{"host":{}}',
|
||||
toolCallId: "c2",
|
||||
toolName: "get_host",
|
||||
}),
|
||||
{ role: "assistant", content: "done", toolCalls: null },
|
||||
];
|
||||
|
||||
expect(toChatHistory(rows)).toEqual([
|
||||
{ role: "user", content: "check" },
|
||||
{ role: "assistant", content: "", toolCalls: calls },
|
||||
{
|
||||
role: "tool",
|
||||
content: '{"hosts":[]}',
|
||||
toolCallId: "c1",
|
||||
toolName: "list_hosts",
|
||||
},
|
||||
{
|
||||
role: "tool",
|
||||
content: '{"host":{}}',
|
||||
toolCallId: "c2",
|
||||
toolName: "get_host",
|
||||
},
|
||||
{ role: "assistant", content: "done" },
|
||||
]);
|
||||
});
|
||||
|
||||
it("fills in results for calls a cut off run never answered", () => {
|
||||
const rows = [
|
||||
{ role: "user", content: "check", toolCalls: null },
|
||||
toStoredMessage({ role: "assistant", content: "", toolCalls: calls }),
|
||||
toStoredMessage({
|
||||
role: "tool",
|
||||
content: "{}",
|
||||
toolCallId: "c1",
|
||||
toolName: "list_hosts",
|
||||
}),
|
||||
{ role: "user", content: "continue", toolCalls: null },
|
||||
];
|
||||
|
||||
const history = toChatHistory(rows);
|
||||
expect(history.map((m) => m.role)).toEqual([
|
||||
"user",
|
||||
"assistant",
|
||||
"tool",
|
||||
"tool",
|
||||
"user",
|
||||
]);
|
||||
expect(history[3]).toMatchObject({
|
||||
toolCallId: "c2",
|
||||
toolName: "get_host",
|
||||
});
|
||||
expect(JSON.parse(history[3].content).error).toMatch(/interrupted/);
|
||||
});
|
||||
|
||||
it("repairs a legacy row saved with tool calls and no results", () => {
|
||||
const history = toChatHistory([
|
||||
{ role: "assistant", content: "", toolCalls: JSON.stringify(calls) },
|
||||
]);
|
||||
expect(history.filter((m) => m.role === "tool")).toHaveLength(2);
|
||||
});
|
||||
|
||||
it("drops tool rows that answer nothing", () => {
|
||||
expect(
|
||||
toChatHistory([
|
||||
{ role: "tool", content: "{}", toolCalls: null },
|
||||
{ role: "user", content: "hi", toolCalls: null },
|
||||
]),
|
||||
).toEqual([{ role: "user", content: "hi" }]);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user