47 lines
1.7 KiB
TypeScript
47 lines
1.7 KiB
TypeScript
type AssistantToolResult = {
|
|
content: string;
|
|
data?: unknown;
|
|
};
|
|
|
|
export function readToolError(result: AssistantToolResult): string | null {
|
|
if (result.data && typeof result.data === "object" && result.data !== null && "error" in (result.data as Record<string, unknown>)) {
|
|
const error = (result.data as Record<string, unknown>).error;
|
|
return typeof error === "string" ? error : null;
|
|
}
|
|
|
|
try {
|
|
const parsed = JSON.parse(result.content) as unknown;
|
|
if (parsed && typeof parsed === "object" && "error" in (parsed as Record<string, unknown>)) {
|
|
const error = (parsed as Record<string, unknown>).error;
|
|
return typeof error === "string" ? error : null;
|
|
}
|
|
} catch {
|
|
// tool content may be plain text
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
export function readToolSuccessMessage(result: AssistantToolResult): string | null {
|
|
if (result.data && typeof result.data === "object" && result.data !== null) {
|
|
const data = result.data as Record<string, unknown>;
|
|
if (typeof data.message === "string" && data.message.trim().length > 0) return data.message;
|
|
if (typeof data.description === "string" && data.description.trim().length > 0) return data.description;
|
|
}
|
|
|
|
try {
|
|
const parsed = JSON.parse(result.content) as unknown;
|
|
if (parsed && typeof parsed === "object") {
|
|
const content = parsed as Record<string, unknown>;
|
|
if (typeof content.message === "string" && content.message.trim().length > 0) return content.message;
|
|
if (typeof content.description === "string" && content.description.trim().length > 0) return content.description;
|
|
}
|
|
} catch {
|
|
// tool content may be plain text
|
|
}
|
|
|
|
return typeof result.content === "string" && result.content.trim().length > 0
|
|
? result.content
|
|
: null;
|
|
}
|