| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 |
- import { describe, expect, it } from "vitest";
- import type { MessageWithParts } from "@zcode/contracts";
- import { projectSessionTranscript } from "../src/session-transcript.js";
- describe("session transcript projection", () => {
- it("filters model-only goal continuation user inputs", () => {
- const transcript = projectSessionTranscript([
- {
- info: { role: "user" },
- parts: [
- {
- id: "part-user",
- text: "加一个地址系统,可以加,可以选",
- type: "text",
- },
- ],
- },
- {
- info: {
- role: "user",
- source: "goal-continuation",
- synthetic: true,
- visibility: "model-only",
- },
- parts: [
- {
- id: "part-goal",
- metadata: {
- source: "goal-continuation",
- visibility: "model-only",
- },
- synthetic: true,
- text: [
- '<system-reminder source="goal-continuation">',
- "Continue working toward the active session goal.",
- "</system-reminder>",
- ].join("\n"),
- type: "text",
- },
- ],
- },
- {
- info: { role: "assistant" },
- parts: [
- {
- id: "part-assistant",
- text: "继续完成并验证了地址系统。",
- type: "text",
- },
- ],
- },
- ] as unknown as MessageWithParts[]);
- expect(transcript).toEqual([
- {
- content: "加一个地址系统,可以加,可以选",
- role: "user",
- },
- {
- content: "继续完成并验证了地址系统。",
- role: "agent",
- },
- ]);
- });
- it("preserves assistant thought and tool parts for resume rendering", () => {
- const transcript = projectSessionTranscript([
- {
- info: { role: "user" },
- parts: [
- {
- id: "part-user",
- text: "read the file",
- type: "text",
- },
- ],
- },
- {
- info: { role: "assistant" },
- parts: [
- {
- id: "part-thought",
- text: "I should inspect package.json first.",
- type: "reasoning",
- },
- {
- id: "part-text",
- text: "I'll inspect it.",
- type: "text",
- },
- {
- callID: "call-read",
- id: "part-tool",
- state: {
- input: { file_path: "/workspace/project/package.json" },
- output: '{"name":"demo"}',
- status: "completed",
- title: "Read",
- },
- tool: "Read",
- type: "tool",
- },
- ],
- },
- ] as unknown as MessageWithParts[]);
- expect(transcript).toEqual([
- {
- content: "read the file",
- role: "user",
- },
- {
- content: "I'll inspect it.",
- parts: [
- {
- text: "I should inspect package.json first.",
- type: "thought",
- },
- {
- text: "I'll inspect it.",
- type: "text",
- },
- {
- input: { file_path: "/workspace/project/package.json" },
- output: '{"name":"demo"}',
- status: "completed",
- title: "Read",
- toolCallId: "call-read",
- toolName: "Read",
- type: "tool",
- },
- ],
- role: "agent",
- },
- ]);
- });
- it("restores persisted file diff display metadata for completed tools", () => {
- const display = {
- additions: 1,
- deletions: 1,
- filePath: "/workspace/project/src/app.ts",
- kind: "file_diff" as const,
- structuredPatch: [
- {
- oldStart: 1,
- oldLines: 1,
- newStart: 1,
- newLines: 1,
- lines: ["-old", "+new"],
- },
- ],
- truncated: false,
- };
- const transcript = projectSessionTranscript([
- {
- info: { role: "assistant" },
- parts: [
- {
- callID: "call-edit",
- id: "part-tool",
- state: {
- input: {
- file_path: "/workspace/project/src/app.ts",
- old_string: "old",
- new_string: "new",
- },
- metadata: {
- schemaVersion: 1,
- display,
- serialization: {
- budgetStrategy: "inline",
- originalBytes: 42,
- returnedBytes: 42,
- truncated: false,
- },
- },
- output: "The file has been updated successfully.",
- status: "completed",
- title: "Edit",
- time: { start: 1, end: 2 },
- },
- tool: "Edit",
- type: "tool",
- },
- ],
- },
- ] as unknown as MessageWithParts[]);
- expect(transcript[0]?.parts?.[0]).toMatchObject({
- input: {
- file_path: "/workspace/project/src/app.ts",
- old_string: "old",
- new_string: "new",
- },
- output: "The file has been updated successfully.",
- resultDisplay: display,
- status: "completed",
- toolCallId: "call-edit",
- toolName: "Edit",
- type: "tool",
- });
- });
- it.each([
- {
- toolName: "TaskOutput",
- input: { task_id: "task-1", block: false, timeout: 0 },
- display: {
- kind: "task_output",
- retrievalStatus: "not_ready",
- taskStatus: "running",
- output: "partial output",
- } as const,
- },
- {
- toolName: "RespondToCoordinator",
- input: { summary: "Reply", message: "Continue working." },
- display: {
- kind: "respond_to_coordinator",
- status: "success",
- } as const,
- },
- ])("restores persisted $toolName display metadata", ({ toolName, input, display }) => {
- const transcript = projectSessionTranscript([
- {
- info: { role: "assistant" },
- parts: [
- {
- callID: `call-${toolName}`,
- id: `part-${toolName}`,
- state: {
- input,
- metadata: {
- schemaVersion: 1,
- display,
- },
- output: "provider-visible content",
- status: "completed",
- title: toolName,
- time: { start: 1, end: 2 },
- },
- tool: toolName,
- type: "tool",
- },
- ],
- },
- ] as unknown as MessageWithParts[]);
- expect(transcript[0]?.parts?.[0]).toMatchObject({
- input,
- output: "provider-visible content",
- resultDisplay: display,
- status: "completed",
- toolName,
- type: "tool",
- });
- });
- });
|