fix: avoid unsafe string coercion in tui
This commit is contained in:
@@ -95,6 +95,14 @@ function formatTokens(total?: number | null, context?: number | null) {
|
|||||||
return `tokens ${total ?? 0}/${context}${pct !== null ? ` (${pct}%)` : ""}`;
|
return `tokens ${total ?? 0}/${context}${pct !== null ? ` (${pct}%)` : ""}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function asString(value: unknown, fallback = ""): string {
|
||||||
|
if (typeof value === "string") return value;
|
||||||
|
if (typeof value === "number" || typeof value === "boolean") {
|
||||||
|
return String(value);
|
||||||
|
}
|
||||||
|
return fallback;
|
||||||
|
}
|
||||||
|
|
||||||
export async function runTui(opts: TuiOptions) {
|
export async function runTui(opts: TuiOptions) {
|
||||||
const config = loadConfig();
|
const config = loadConfig();
|
||||||
const defaultSession =
|
const defaultSession =
|
||||||
@@ -235,8 +243,8 @@ export async function runTui(opts: TuiOptions) {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (message.role === "toolResult") {
|
if (message.role === "toolResult") {
|
||||||
const toolCallId = String(message.toolCallId ?? "");
|
const toolCallId = asString(message.toolCallId, "");
|
||||||
const toolName = String(message.toolName ?? "tool");
|
const toolName = asString(message.toolName, "tool");
|
||||||
const component = chatLog.startTool(toolCallId, toolName, {});
|
const component = chatLog.startTool(toolCallId, toolName, {});
|
||||||
component.setResult(
|
component.setResult(
|
||||||
{
|
{
|
||||||
@@ -327,9 +335,9 @@ export async function runTui(opts: TuiOptions) {
|
|||||||
if (!currentSessionId || evt.runId !== currentSessionId) return;
|
if (!currentSessionId || evt.runId !== currentSessionId) return;
|
||||||
if (evt.stream === "tool") {
|
if (evt.stream === "tool") {
|
||||||
const data = evt.data ?? {};
|
const data = evt.data ?? {};
|
||||||
const phase = String(data.phase ?? "");
|
const phase = asString(data.phase, "");
|
||||||
const toolCallId = String(data.toolCallId ?? "");
|
const toolCallId = asString(data.toolCallId, "");
|
||||||
const toolName = String(data.name ?? "tool");
|
const toolName = asString(data.name, "tool");
|
||||||
if (!toolCallId) return;
|
if (!toolCallId) return;
|
||||||
if (phase === "start") {
|
if (phase === "start") {
|
||||||
chatLog.startTool(toolCallId, toolName, data.args);
|
chatLog.startTool(toolCallId, toolName, data.args);
|
||||||
|
|||||||
Reference in New Issue
Block a user