File size: 11,034 Bytes
3201ca6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
import { describe, expect, it, vi } from "vitest";
import type {
  ActionEvent,
  AgentErrorEvent,
  MessageEvent,
  ObservationEvent,
} from "#/types/agent-server/core";
import type {
  ExecuteBashAction,
  MCPToolAction,
  TaskAction,
} from "#/types/agent-server/core/base/action";
import type {
  ExecuteBashObservation,
  TaskObservation,
} from "#/types/agent-server/core/base/observation";
import type { StreamingDeltaEvent } from "#/types/agent-server/core/events/streaming-delta-event";
import { SecurityRisk } from "#/types/agent-server/core/base/common";
import { eventsToHtml, eventsToMarkdown } from ".";

vi.mock("#/i18n", () => ({
  default: {
    exists: () => false,
    language: "en",
    resolvedLanguage: "en",
    t: (key: string, values?: Record<string, string>) => {
      const translations: Record<string, string> = {
        ACTION_MESSAGE$ACP_TOOL: "Use tool",
        CHAT_INTERFACE$ASSISTANT: "Assistant",
        COMMON$ERROR: "Error",
        TASK$QUERY: "Query",
        TASK$RESULT: "Result",
        TASK$SUBAGENT: "Subagent",
        TASK$TASK_ID: "Task ID",
        TRANSCRIPT_EXPORT$DEFAULT_TITLE: "Conversation transcript",
        TRANSCRIPT_EXPORT$MODEL: "Model",
        TRANSCRIPT_EXPORT$TOOL: "Tool",
        TRANSCRIPT_EXPORT$USER: "User",
      };
      return translations[key] ?? values?.name ?? key;
    },
  },
}));

const timestamp = "2026-07-10T12:34:56.000Z";

const userMessage: MessageEvent = {
  id: "user-1",
  timestamp,
  source: "user",
  llm_message: {
    role: "user",
    content: [{ type: "text", text: "Please diagnose **the failure**." }],
  },
  activated_skills: [],
  extended_content: [],
};

const assistantMessage: MessageEvent = {
  id: "assistant-1",
  timestamp: "2026-07-10T12:35:00.000Z",
  source: "agent",
  llm_message: {
    role: "assistant",
    content: [{ type: "text", text: "The service is healthy now." }],
  },
  activated_skills: [],
  extended_content: [],
};

const terminalAction: ActionEvent<ExecuteBashAction> = {
  id: "action-1",
  timestamp: "2026-07-10T12:34:57.000Z",
  source: "agent",
  thought: [],
  thinking_blocks: [],
  action: {
    kind: "ExecuteBashAction",
    command: "npm test",
    is_input: false,
    timeout: null,
    reset: false,
  },
  tool_name: "terminal",
  tool_call_id: "tool-1",
  tool_call: {
    id: "tool-1",
    type: "function",
    function: { name: "terminal", arguments: "{}" },
  },
  llm_response_id: "response-1",
  security_risk: SecurityRisk.LOW,
  summary: "Run the unit tests",
};

const terminalObservation = (
  output: string,
): ObservationEvent<ExecuteBashObservation> => ({
  id: "observation-1",
  timestamp: "2026-07-10T12:34:58.000Z",
  source: "environment",
  tool_name: "terminal",
  tool_call_id: "tool-1",
  action_id: "action-1",
  observation: {
    kind: "ExecuteBashObservation",
    command: "npm test",
    content: [{ type: "text", text: output }],
    exit_code: 0,
    error: false,
    timeout: false,
    metadata: {
      exit_code: 0,
      pid: 123,
      username: "openhands",
      hostname: "sandbox",
      working_dir: "/workspace/project",
      py_interpreter_path: null,
      prefix: "",
      suffix: "",
    },
  },
});

const defaultOptions = {
  includeToolDetails: true,
  includeTimestamps: true,
  title: "Debug session",
  model: "anthropic/claude-sonnet",
};

describe("conversation transcript export", () => {
  it("exports messages and collapsed tool details as GitHub-ready Markdown", () => {
    const markdown = eventsToMarkdown(
      [
        userMessage,
        terminalAction,
        terminalObservation("3 tests passed"),
        assistantMessage,
      ],
      defaultOptions,
    );

    expect(markdown).toContain("# Debug session");
    expect(markdown).toContain("**Model:** anthropic/claude-sonnet");
    expect(markdown).toContain("## User");
    expect(markdown).toContain("Please diagnose **the failure**.");
    expect(markdown).toContain("<details>");
    expect(markdown).toContain("Run the unit tests");
    expect(markdown).toContain("3 tests passed");
    expect(markdown).toContain("## Assistant");
  });

  it("honors tool-detail and timestamp options in both formats", () => {
    const events = [userMessage, terminalAction, terminalObservation("passed")];
    const options = {
      ...defaultOptions,
      includeToolDetails: false,
      includeTimestamps: false,
    };
    const markdown = eventsToMarkdown(events, options);
    const html = eventsToHtml(events, options);

    expect(markdown).toContain("<strong>Tool:</strong> Run the unit tests");
    expect(markdown).not.toContain("<details>");
    expect(markdown).not.toContain("passed");
    expect(markdown).not.toContain(timestamp);
    expect(html).not.toContain("<details>");
    expect(html).not.toContain("passed");
    expect(html).not.toContain("<time");
  });

  it("exports agent errors and uses the UI's tool-output truncation", () => {
    const errorEvent: AgentErrorEvent = {
      id: "error-1",
      timestamp,
      kind: "AgentErrorEvent",
      source: "agent",
      tool_name: "terminal",
      tool_call_id: "tool-error",
      error: "Command failed with exit code 1",
    };
    const markdown = eventsToMarkdown(
      [terminalAction, terminalObservation("x".repeat(1200)), errorEvent],
      defaultOptions,
    );

    expect(markdown).toContain("## Error");
    expect(markdown).toContain("> Command failed with exit code 1");
    expect(markdown).toContain(`${"x".repeat(1000)}...`);
    expect(markdown).not.toContain("x".repeat(1001));
  });

  it("exports assistant narration once when an observation replaces its action", () => {
    const narratedAction: ActionEvent<ExecuteBashAction> = {
      ...terminalAction,
      thought: [{ type: "text", text: "I checked the failing service." }],
      reasoning_content: "The logs point to a stale process.",
    };
    const markdown = eventsToMarkdown(
      [narratedAction, terminalObservation("passed")],
      defaultOptions,
    );

    expect(markdown.match(/I checked the failing service\./g)).toHaveLength(1);
    expect(markdown.match(/The logs point to a stale process\./g)).toHaveLength(
      1,
    );
  });

  it("exports streaming reasoning with the same inline-think split as chat", () => {
    const streamingEvent: StreamingDeltaEvent = {
      id: "streaming-1",
      timestamp,
      source: "agent",
      kind: "StreamingDeltaEvent",
      reasoning_content: "Separate reasoning",
      content: "<think>Inline reasoning</think>Visible answer",
    };

    const markdown = eventsToMarkdown([streamingEvent], defaultOptions);

    expect(markdown).toContain("Separate reasoning");
    expect(markdown).toContain("Inline reasoning");
    expect(markdown).toContain("Visible answer");
    expect(markdown).not.toContain("&lt;think&gt;");
  });

  it("splits inline reasoning from finalized assistant messages", () => {
    const messageEvent: MessageEvent = {
      ...assistantMessage,
      llm_message: {
        role: "assistant",
        content: [
          {
            type: "text",
            text: "<think>Reloaded reasoning</think>Reloaded answer",
          },
        ],
      },
    };

    const markdown = eventsToMarkdown([messageEvent], defaultOptions);

    expect(markdown).toContain("Reloaded reasoning");
    expect(markdown).toContain("Reloaded answer");
    expect(markdown).not.toContain("&lt;think&gt;");
  });

  it("exports task inputs and results through their display projection", () => {
    const taskAction: ActionEvent<TaskAction> = {
      ...terminalAction,
      id: "task-action",
      tool_name: "task",
      tool_call_id: "task-call",
      tool_call: {
        id: "task-call",
        type: "function",
        function: { name: "task", arguments: "{}" },
      },
      action: {
        kind: "TaskAction",
        prompt: "Inspect the authentication flow",
        subagent_type: "security",
        description: "Inspect authentication",
      },
      summary: "Delegate authentication review",
    };
    const taskObservation: ObservationEvent<TaskObservation> = {
      id: "task-observation",
      timestamp: "2026-07-10T12:34:59.000Z",
      source: "environment",
      tool_name: "task",
      tool_call_id: "task-call",
      action_id: "task-action",
      observation: {
        kind: "TaskObservation",
        content: [{ type: "text", text: "No credential leak found." }],
        task_id: "task-123",
        subagent: "security",
        status: "completed",
        is_error: false,
      },
    };

    const markdown = eventsToMarkdown(
      [taskAction, taskObservation],
      defaultOptions,
    );

    expect(markdown).toContain("Inspect the authentication flow");
    expect(markdown).toContain("No credential leak found.");
    expect(markdown).toContain("task-123");
  });

  it("only exports the redacted display projection, never hidden raw tool arguments", () => {
    const redactedAction: ActionEvent<MCPToolAction> = {
      ...terminalAction,
      id: "secret-action",
      tool_call_id: "secret-tool",
      action: {
        kind: "MCPToolAction",
        data: { api_key: "**********" },
      },
      tool_call: {
        id: "secret-tool",
        type: "function",
        function: {
          name: "mcp_call",
          arguments: '{"api_key":"raw-secret-must-not-export"}',
        },
      },
      tool_name: "mcp_call",
      summary: "Call the configured service",
    };

    const markdown = eventsToMarkdown([redactedAction], defaultOptions);
    const html = eventsToHtml([redactedAction], defaultOptions);

    expect(markdown).toContain("**********");
    expect(html).toContain("**********");
    expect(markdown).not.toContain("raw-secret-must-not-export");
    expect(html).not.toContain("raw-secret-must-not-export");
  });

  it("neutralizes active content in Markdown and HTML", () => {
    const unsafeMessage: MessageEvent = {
      ...userMessage,
      llm_message: {
        role: "user",
        content: [
          {
            type: "text",
            text: "<script>alert(1)</script> [click](javascript:alert(1))\n[ref]: javascript:alert(2)",
          },
        ],
      },
    };
    const markdown = eventsToMarkdown([unsafeMessage], defaultOptions);
    const html = eventsToHtml([unsafeMessage], defaultOptions);

    expect(markdown).not.toContain("<script>");
    expect(markdown).toContain("&lt;script&gt;");
    expect(markdown).toContain("\\[click\\](javascript:alert(1))");
    expect(markdown).toContain("\\[ref\\]: javascript:alert(2)");
    expect(html).not.toContain("<script>");
    expect(html).toContain("&lt;script&gt;");
  });

  it("renders a self-contained HTML document", () => {
    const html = eventsToHtml(
      [
        userMessage,
        terminalAction,
        terminalObservation("3 tests passed"),
        assistantMessage,
      ],
      defaultOptions,
    );

    expect(html).not.toMatch(/<(?:link|script)[^>]+(?:href|src)=/i);
    expect(html).toMatchSnapshot();
  });
});