-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconversationExport.ts
More file actions
146 lines (138 loc) · 4.5 KB
/
Copy pathconversationExport.ts
File metadata and controls
146 lines (138 loc) · 4.5 KB
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
/**
* Markdown export for AI Assistant conversations.
*
* Renders a :type:`Conversation` to a self-contained Markdown document
* suitable for sharing or pasting into a notebook. Uses fenced JSON
* blocks for tool calls / results so the round-trip stays unambiguous.
*
* Cross-reference: :mod:`DataLab/datalab/aiassistant/utils/markdown.py`
* (desktop equivalent).
*/
import type { Conversation } from "./conversationStore";
import type { ChatMessage, ToolCall } from "./types";
import { t } from "../i18n/translate";
import {
acceptFromExtensions,
saveBytesToFile,
type SaveFileResult,
} from "../utils/saveFile";
/** Format an ISO-8601 timestamp from an epoch millisecond value. */
function isoStamp(ms: number): string {
if (!ms) return "";
try {
return new Date(ms).toISOString();
} catch {
return "";
}
}
/** Try to pretty-print a JSON-ish blob; falls back to the raw string. */
function prettyJson(text: string): string {
try {
return JSON.stringify(JSON.parse(text), null, 2);
} catch {
return text;
}
}
/** Serialise an ``assistant.tool_calls`` array as fenced JSON. */
function renderToolCalls(toolCalls: ToolCall[]): string {
const compact = toolCalls.map((tc) => ({
id: tc.id,
name: tc.function.name,
arguments: (() => {
try {
return JSON.parse(tc.function.arguments) as unknown;
} catch {
return tc.function.arguments;
}
})(),
}));
return "```json\n" + JSON.stringify(compact, null, 2) + "\n```";
}
/** Render a single :type:`ChatMessage` as a Markdown section. */
function renderMessage(msg: ChatMessage): string {
if (msg.role === "system") {
// System prompt is intentionally omitted from exports — it's
// generated at runtime and not interesting to a human reader.
return "";
}
if (msg.role === "user") {
const lines: string[] = ["## You", ""];
if (typeof msg.content === "string") {
lines.push(msg.content);
} else {
for (const part of msg.content) {
if (part.type === "text") {
lines.push(part.text);
} else if (part.type === "image_url") {
lines.push("> _(image attached)_");
}
}
}
return lines.join("\n");
}
if (msg.role === "assistant") {
const lines: string[] = ["## Assistant", ""];
if (msg.content) {
lines.push(msg.content);
}
if (msg.tool_calls && msg.tool_calls.length > 0) {
if (msg.content) lines.push("");
lines.push("**Tool calls:**", "", renderToolCalls(msg.tool_calls));
}
return lines.join("\n");
}
// Tool result.
return [
`## Tool result (${msg.name})`,
"",
"```json",
prettyJson(msg.content),
"```",
].join("\n");
}
/** Render *conv* as a self-contained Markdown document. */
export function conversationToMarkdown(conv: Conversation): string {
const sections: string[] = [];
sections.push(`# ${conv.title || "(untitled conversation)"}`);
const meta: string[] = [];
if (conv.createdAt) meta.push(`Created: ${isoStamp(conv.createdAt)}`);
if (conv.updatedAt) meta.push(`Updated: ${isoStamp(conv.updatedAt)}`);
if (conv.usage?.totalTokens) {
meta.push(`Tokens: ${conv.usage.totalTokens}`);
}
if (meta.length > 0) {
sections.push(`_${meta.join(" \u2022 ")}_`);
}
for (const msg of conv.messages) {
const rendered = renderMessage(msg);
if (rendered) sections.push(rendered);
}
return sections.join("\n\n") + "\n";
}
/** Strip filesystem-unsafe characters and clamp the length so the
* result is a valid filename on Windows / macOS / Linux. */
export function sanitizeFilename(name: string, maxLen = 80): string {
const cleaned = name
// eslint-disable-next-line no-control-regex -- intentional: strip ASCII control chars from filenames
.replace(/[\\/:*?"<>|\u0000-\u001f]/g, " ")
.replace(/\s+/g, " ")
.trim();
if (!cleaned) return "conversation";
return cleaned.length > maxLen ? cleaned.slice(0, maxLen).trim() : cleaned;
}
/** Save conversation *content* as a Markdown file under *filename*.
* Uses the native "Save as…" picker when available, otherwise downloads
* to the browser's Downloads folder. Returns the {@link SaveFileResult}
* so the caller can toast on the download-fallback path. */
export function downloadMarkdown(
filename: string,
content: string,
): Promise<SaveFileResult> {
const bytes = new TextEncoder().encode(content);
return saveBytesToFile(
bytes,
filename,
[acceptFromExtensions(t("Markdown files"), ["md"], "text/markdown")],
"text/markdown;charset=utf-8",
);
}