ai-chatbot/lib/ai/runtime/agent-media-markdown.ts
Your Name dfc5f704b4
Some checks failed
Lint / build (20) (push) Has been cancelled
Playwright Tests / test (push) Has been cancelled
chore: sync latest app updates
2026-04-12 23:45:00 +08:00

54 lines
1.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

export interface AgentToolImageMarkdownItem {
originalUrl: string;
thumbnailUrl?: string | null;
imageSize?: string;
}
export function prependImageModelBadge(
text: string,
modelDisplayName?: string | null,
): string {
const normalizedText = String(text || "").trim();
if (!normalizedText) return normalizedText;
const normalizedModelName = String(modelDisplayName || "").trim();
if (!normalizedModelName) return normalizedText;
if (
/^\s*[◈◇◆🔹⬡✦•·]\s*[^\n]+/im.test(normalizedText) ||
/^\s*(?:Routed to|Using model|使用模型|模型)\s*[::]/im.test(normalizedText)
) {
return normalizedText;
}
return `◈ ${normalizedModelName}\n\n${normalizedText}`;
}
export function buildAgentToolImageMarkdown(params: AgentToolImageMarkdownItem): string {
const originalUrl = String(params.originalUrl || "").trim();
const thumbnailUrl = String(params.thumbnailUrl || originalUrl).trim() || originalUrl;
const imageSize = String(params.imageSize || "2K").trim() || "2K";
return `![thumb|||${originalUrl}|||${imageSize}](${thumbnailUrl})`;
}
export function buildAgentToolImagesMarkdown(items: AgentToolImageMarkdownItem[]) {
return items
.map((item) =>
buildAgentToolImageMarkdown({
originalUrl: item.originalUrl,
thumbnailUrl: item.thumbnailUrl,
imageSize: item.imageSize,
}),
)
.join("\n");
}
export function buildAgentToolVideoMarkdown(videoUrl: string) {
const normalizedUrl = String(videoUrl || "").trim();
if (!normalizedUrl) return "";
return [
`<video src="${normalizedUrl}" controls style="max-width: 100%; border-radius: 8px;"></video>`,
`[下载视频](${normalizedUrl})`,
].join("\n\n");
}