mirror of
https://github.com/getcompanion-ai/co-mono.git
synced 2026-04-15 09:01:14 +00:00
fix
This commit is contained in:
parent
ca0861400d
commit
5a2172fb9d
4 changed files with 98 additions and 42 deletions
|
|
@ -1,13 +1,11 @@
|
|||
import { describe, it, expect } from "vitest";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import type { AgentSessionEvent } from "../src/core/agent-session.js";
|
||||
import { extractUserText, createVercelStreamListener, finishVercelStream } from "../src/core/vercel-ai-stream.js";
|
||||
import { createVercelStreamListener, extractUserText } from "../src/core/vercel-ai-stream.js";
|
||||
|
||||
describe("extractUserText", () => {
|
||||
it("extracts text from useChat v5+ format with parts", () => {
|
||||
const body = {
|
||||
messages: [
|
||||
{ role: "user", parts: [{ type: "text", text: "hello world" }] },
|
||||
],
|
||||
messages: [{ role: "user", parts: [{ type: "text", text: "hello world" }] }],
|
||||
};
|
||||
expect(extractUserText(body)).toBe("hello world");
|
||||
});
|
||||
|
|
@ -70,7 +68,9 @@ describe("createVercelStreamListener", () => {
|
|||
this.writableEnded = true;
|
||||
},
|
||||
chunks,
|
||||
get ended() { return ended; },
|
||||
get ended() {
|
||||
return ended;
|
||||
},
|
||||
} as any;
|
||||
}
|
||||
|
||||
|
|
@ -79,8 +79,11 @@ describe("createVercelStreamListener", () => {
|
|||
.filter((c) => c.startsWith("data: "))
|
||||
.map((c) => {
|
||||
const payload = c.replace(/^data: /, "").replace(/\n\n$/, "");
|
||||
try { return JSON.parse(payload); }
|
||||
catch { return payload; }
|
||||
try {
|
||||
return JSON.parse(payload);
|
||||
} catch {
|
||||
return payload;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
|
@ -129,4 +132,18 @@ describe("createVercelStreamListener", () => {
|
|||
const parsed = parseChunks(response.chunks);
|
||||
expect(parsed).toEqual([{ type: "start", messageId: "test-msg-id" }]);
|
||||
});
|
||||
|
||||
it("ignores events outside the active prompt lifecycle", () => {
|
||||
const response = createMockResponse();
|
||||
const listener = createVercelStreamListener(response, "test-msg-id");
|
||||
|
||||
listener({ type: "turn_start", turnIndex: 0, timestamp: Date.now() } as AgentSessionEvent);
|
||||
listener({ type: "agent_start" } as AgentSessionEvent);
|
||||
listener({ type: "turn_start", turnIndex: 0, timestamp: Date.now() } as AgentSessionEvent);
|
||||
listener({ type: "agent_end", messages: [] } as AgentSessionEvent);
|
||||
listener({ type: "turn_start", turnIndex: 1, timestamp: Date.now() } as AgentSessionEvent);
|
||||
|
||||
const parsed = parseChunks(response.chunks);
|
||||
expect(parsed).toEqual([{ type: "start", messageId: "test-msg-id" }, { type: "start-step" }]);
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue