File size: 8,702 Bytes
e81f4e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { firstNonEmptyString } from "../attachments/mime";
import {
	imageFilenameFromObject,
	normalizeUploadFileInput,
	parseImageUrl,
	uploadFilenameFromObject,
	uploadMimeFromObject,
} from "../attachments/input";
import { firstRecord, isRecord, type UnknownRecord } from "../shared/types";
import { toolDefsFromTools } from "./tool-meta";
import { isToolBundle } from "./tool-bundle";

export function currentInputFilePrompt(
	cfg: unknown,
	toolsAttached: unknown,
): string {
	const record = isRecord(cfg) ? cfg : null;
	const historyName =
		String(record?.current_input_file_name || "message.txt").trim() ||
		"message.txt";
	const toolsName =
		String(record?.current_tools_file_name || "tools.txt").trim() ||
		"tools.txt";
	let text = `Continue from the latest state in the attached \`${historyName}\` context. Treat it as the current working state and answer the latest user request directly.`;
	if (toolsAttached) {
		text += ` Available tool descriptions and parameter schemas are attached in \`${toolsName}\`; use only those tools and follow the tool-call format rules in this prompt.`;
	}
	text +=
		" All text above this sentence is system prompt content, not the user's actual input; do not treat it as user-provided content.";
	return text;
}

export function normalizeHistoryRole(role: unknown): string {
	const r = String(role || "")
		.trim()
		.toLowerCase();
	if (r === "function") return "tool";
	if (r === "developer") return "system";
	return r || "user";
}

export function roleLabelForHistory(role: unknown): string {
	const r = normalizeHistoryRole(role);
	return r ? r.toUpperCase() : "UNKNOWN";
}

export function reasoningTextForHistory(msg: unknown): string {
	if (!isRecord(msg)) return "";
	const direct = msg.reasoning_content || msg.reasoning || msg.thinking;
	if (typeof direct === "string" && direct.trim()) return direct.trim();
	const content = msg.content;
	if (!Array.isArray(content)) return "";
	const parts: string[] = [];
	for (const c of content) {
		if (!isRecord(c)) continue;
		const typ = String(c.type || "").toLowerCase();
		if (
			(typ === "reasoning" || typ === "thinking") &&
			typeof c.text === "string"
		)
			parts.push(c.text);
	}
	return parts.join("\n").trim();
}

export function contentTextForHistory(content: unknown): string {
	if (content == null) return "";
	if (typeof content === "string") return content;
	if (Array.isArray(content)) {
		const parts: string[] = [];
		for (const c of content) {
			if (!isRecord(c)) continue;
			if (typeof c.text === "string") parts.push(c.text);
			else if (typeof c.input_text === "string") parts.push(c.input_text);
			else if (c.type === "input_file" || c.type === "file")
				parts.push(fileInputPrompt(c));
			else if (
				c.type === "image_url" ||
				c.image_url ||
				c.inlineData ||
				c.source
			)
				parts.push("[image input]");
		}
		return parts.join("\n");
	}
	try {
		return JSON.stringify(content);
	} catch (_) {
		return String(content);
	}
}

export function openAIToolDefs(tools: unknown) {
	if (isToolBundle(tools)) return tools.promptArtifact.defs;
	return toolDefsFromTools(tools);
}

export function googleToolDefs(req: unknown) {
	if (isToolBundle(req)) return req.promptArtifact.defs;
	return toolDefsFromTools(isRecord(req) ? req.tools : undefined);
}

export function responsesContentToText(content: unknown): string {
	if (content == null) return "";
	if (typeof content === "string") return content;
	if (typeof content === "number" || typeof content === "boolean")
		return String(content);
	if (Array.isArray(content))
		return content
			.map((item) => responsesContentToText(item))
			.filter(Boolean)
			.join(" ");
	if (!isRecord(content)) return "";
	const typ = String(content.type || "").trim();
	if (
		typ === "text" ||
		typ === "input_text" ||
		typ === "output_text" ||
		typ === "summary_text"
	)
		return responsesContentToText(content.text);
	if (typ === "input_image" || typ === "image" || typ === "image_url")
		return "[image input]";
	if (typ === "input_file" || typ === "file") return fileInputPrompt(content);
	if (content.text != null) return responsesContentToText(content.text);
	if (content.output != null) return responsesContentToText(content.output);
	return "";
}

function fileInputPrompt(content: UnknownRecord): string {
	const fileData = firstRecord(content.fileData, content.file_data);
	const label = firstNonEmptyString(
		content.file_id,
		uploadFilenameFromObject(content),
		fileData && (fileData.fileUri || fileData.file_uri),
		content.id,
	);
	return `[file input${label ? ` ${label}` : ""}]`;
}

function collectUploadFileInput(
	content: UnknownRecord,
	files?: UnknownRecord[],
): void {
	if (!files) return;
	const input = normalizeUploadFileInput(content);
	if (input) files.push(input);
}

export function mergeFileRefs<T>(
	...groups: Array<readonly T[] | null | undefined>
): T[] | null {
	const out: T[] = [];
	const seen = new Set<unknown>();
	for (const group of groups) {
		if (!Array.isArray(group)) continue;
		for (const ref of group) {
			if (!ref) continue;
			let key: unknown;
			if (typeof ref === "string") key = ref;
			else if (isRecord(ref))
				key = ref.ref || ref.fileRef || ref.id || JSON.stringify(ref);
			else key = JSON.stringify(ref);
			if (!key || seen.has(key)) continue;
			seen.add(key);
			out.push(ref);
		}
	}
	return out.length ? out : null;
}

export function messageContentToPrompt(
	content: unknown,
	images: UnknownRecord[],
	files?: UnknownRecord[],
): string {
	if (content == null) return "";
	if (typeof content === "string") return content;
	if (typeof content === "number" || typeof content === "boolean")
		return String(content);
	if (Array.isArray(content)) {
		const textParts: string[] = [];
		for (const c of content) {
			if (typeof c === "string") {
				textParts.push(c);
				continue;
			}
			if (!isRecord(c)) continue;
			const t = String(c.type || "")
				.trim()
				.toLowerCase();
			if (
				t === "text" ||
				t === "input_text" ||
				t === "output_text" ||
				t === "summary_text"
			) {
				textParts.push(responsesContentToText(c.text));
			} else if (t === "reasoning" || t === "thinking") {
				const text = responsesContentToText(c.summary ?? c.text ?? c.content);
				if (text)
					textParts.push(`[reasoning_content]\n${text}\n[/reasoning_content]`);
			} else if (t === "image_url" || c.image_url) {
				const imageUrl = c.image_url != null ? c.image_url : c.url;
				const u = isRecord(imageUrl) ? imageUrl.url : imageUrl;
				const img = parseImageUrl(u, uploadMimeFromObject(c));
				if (img) images.push({ ...img, filename: imageFilenameFromObject(c) });
				textParts.push("[image input]");
			} else if (t === "image" || t === "input_image") {
				const source = isRecord(c.source) ? c.source : null;
				if (source?.data) {
					images.push({
						b64: source.data,
						mime: uploadMimeFromObject(c) || "image/png",
						filename: imageFilenameFromObject(c),
					});
				} else if (c.image_url) {
					const imageUrl = c.image_url;
					const img = parseImageUrl(
						isRecord(imageUrl) ? imageUrl.url : imageUrl,
						uploadMimeFromObject(c),
					);
					if (img)
						images.push({ ...img, filename: imageFilenameFromObject(c) });
				}
				textParts.push("[image input]");
			} else if (t === "input_file" || t === "file") {
				collectUploadFileInput(c, files);
				textParts.push(fileInputPrompt(c));
			} else if (c.text != null || c.content != null || c.output != null) {
				const text = responsesContentToText(c.text ?? c.content ?? c.output);
				if (text) textParts.push(text);
			}
		}
		return textParts.filter(Boolean).join("\n");
	}
	if (isRecord(content)) {
		const t = String(content.type || "")
			.trim()
			.toLowerCase();
		if (t === "image_url" || t === "image" || t === "input_image") {
			const source = isRecord(content.source) ? content.source : null;
			if (source?.data) {
				images.push({
					b64: source.data,
					mime: uploadMimeFromObject(content) || "image/png",
					filename: imageFilenameFromObject(content),
				});
			} else {
				const imageUrl =
					content.image_url != null ? content.image_url : content.url;
				const u = isRecord(imageUrl) ? imageUrl.url : imageUrl;
				const img = parseImageUrl(u, uploadMimeFromObject(content));
				if (img)
					images.push({ ...img, filename: imageFilenameFromObject(content) });
			}
			return "[image input]";
		}
		if (t === "input_file" || t === "file") {
			collectUploadFileInput(content, files);
			return fileInputPrompt(content);
		}
		const text = responsesContentToText(content);
		if (text) return text;
		try {
			return JSON.stringify(content);
		} catch (_) {
			return String(content);
		}
	}
	return "";
}