Files
OmniRoute/open-sse/executors/deepseek-web/stream-format.ts
Diego Rodrigues de Sa e Souza 17d8cb9f67 refactor(executors): extract pure stream formatters from deepseek-web (#6000)
Extract the pure content/citation formatters (isThinkingModel, isSearchModel,
cleanDeepSeekToken, formatStreamContent, DeepSeekSearchResult, appendSearchCitations)
verbatim into the leaf deepseek-web/stream-format.ts. Host imports the 5 it uses back
into transformSSE/collectSSEContent (cleanDeepSeekToken stays internal to the leaf).

Host 1147 -> 1108 LOC. Byte-identical bodies (verbatim 34/34), leaf has zero imports
(no cycle), all module-private (no re-export). PoW/auth/token-cache/HTTP dispatch
untouched. Adds a split-guard; consumer tests stay green (deepseek-web 35,
deepseek-web-rolling-window-2942 5, deepseek-web-tools-execute 3).
2026-07-02 20:12:23 -03:00

45 lines
1.4 KiB
TypeScript

// Pure DeepSeek stream content/citation formatting. Verbatim from deepseek-web.ts.
export function isThinkingModel(model: string): boolean {
const m = model.toLowerCase();
return m.includes("think") || m.includes("r1") || m.includes("reason");
}
export function isSearchModel(model: string): boolean {
const m = model.toLowerCase();
return m.includes("search") || m.includes("fold");
}
export function cleanDeepSeekToken(text: string): string {
return text.replace(/FINISHED/g, "").replace(/^(SEARCH|WEB_SEARCH|SEARCHING)\s*/i, "");
}
export function formatStreamContent(raw: string, model: string): string {
let text = cleanDeepSeekToken(raw);
if (!isSearchModel(model)) return text;
if (model.toLowerCase().includes("search-silent")) {
return text.replace(/\[citation:(\d+)\]/g, "");
}
return text.replace(/\[citation:(\d+)\]/g, "[$1]");
}
export interface DeepSeekSearchResult {
cite_index?: number;
title?: string;
url?: string;
}
export function appendSearchCitations(
searchResults: DeepSeekSearchResult[],
model: string
): string {
if (searchResults.length === 0 || model.toLowerCase().includes("search-silent")) {
return "";
}
return searchResults
.filter((r) => r.cite_index)
.sort((a, b) => (a.cite_index || 0) - (b.cite_index || 0))
.map((r) => `[${r.cite_index}]: [${r.title}](${r.url})`)
.join("\n");
}