scaling-swarms / frontend /src /editor /embeds /stream-progress.ts
tfrere's picture
tfrere HF Staff
Cursor
feat(embed-studio): drafts with Apply to article, compare, data drawer
f7733a1
Raw History Blame Contribute Delete
1.4 kB
import type { UIMessage } from "ai";
import { normalizeToolPart } from "../../utils/ai-tool-parts";
/**
* Progress of a chart being written, read from the tool call's partial
* input while the model streams it (AI SDK `input-streaming` parts).
*/
export interface StreamProgress {
toolName: "createEmbed" | "patchEmbed";
lines: number;
chars: number;
}
export function streamingProgress(message: UIMessage | undefined): StreamProgress | null {
if (!message || message.role !== "assistant") return null;
const parts = message.parts ?? [];
for (let i = parts.length - 1; i >= 0; i--) {
const tool = normalizeToolPart(parts[i]);
if (!tool) continue;
if (tool.state !== "input-streaming") return null;
if (tool.toolName !== "createEmbed" && tool.toolName !== "patchEmbed") return null;
const input = (tool.input ?? {}) as { html?: unknown; replace?: unknown };
const text = typeof input.html === "string" ? input.html : typeof input.replace === "string" ? input.replace : "";
return {
toolName: tool.toolName,
lines: text ? text.split("\n").length : 0,
chars: text.length,
};
}
return null;
}
export function progressLabel(p: StreamProgress): string {
const verb = p.toolName === "createEmbed" ? "Writing chart" : "Writing the change";
return p.lines > 0 ? `${verb}... ${p.lines} line${p.lines === 1 ? "" : "s"}` : `${verb}...`;
}