| import { base } from '$app/paths'; |
| import { API_TOOLS, X_TOOL_CWD_HEADER } from '$lib/constants'; |
| import { ToolResponseField } from '$lib/enums'; |
| import type { ServerBuiltinToolInfo, ToolExecutionResult } from '$lib/types'; |
| import { apiFetch } from '$lib/utils'; |
| import { getJsonHeaders } from '$lib/utils/api-headers'; |
| import { parseSseJsonStream, type SseJsonEvent } from '$lib/utils/sse'; |
|
|
| export class ToolsService { |
| |
| |
| |
| |
| |
| static async list(): Promise<ServerBuiltinToolInfo[]> { |
| return apiFetch<ServerBuiltinToolInfo[]>(API_TOOLS.LIST); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| static async executeTool( |
| toolName: string, |
| params: Record<string, unknown>, |
| signal?: AbortSignal, |
| cwd?: string |
| ): Promise<ToolExecutionResult> { |
| const result = await apiFetch<Record<string, unknown>>(API_TOOLS.EXECUTE, { |
| body: JSON.stringify({ params, tool: toolName }), |
| headers: cwd ? { [X_TOOL_CWD_HEADER]: cwd } : undefined, |
| method: 'POST', |
| signal |
| }); |
|
|
| if (ToolResponseField.ERROR in result) { |
| return { content: String(result[ToolResponseField.ERROR]), isError: true }; |
| } |
|
|
| if (ToolResponseField.PLAIN_TEXT in result) { |
| return { content: String(result[ToolResponseField.PLAIN_TEXT]), isError: false }; |
| } |
|
|
| return { content: JSON.stringify(result), isError: false }; |
| } |
|
|
| |
| |
| |
| |
| |
| static async executeToolRaw( |
| toolName: string, |
| params: Record<string, unknown>, |
| signal?: AbortSignal, |
| cwd?: string |
| ): Promise<Record<string, unknown>> { |
| return apiFetch<Record<string, unknown>>(API_TOOLS.EXECUTE, { |
| body: JSON.stringify({ params, tool: toolName }), |
| headers: cwd ? { [X_TOOL_CWD_HEADER]: cwd } : undefined, |
| method: 'POST', |
| signal |
| }); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| static async *streamTool( |
| toolName: string, |
| params: Record<string, unknown>, |
| signal?: AbortSignal, |
| cwd?: string |
| ): AsyncGenerator<ToolStreamEvent> { |
| const headers = getJsonHeaders(); |
|
|
| if (cwd) headers[X_TOOL_CWD_HEADER] = cwd; |
|
|
| const response = await fetch(`${base}${API_TOOLS.EXECUTE}`, { |
| body: JSON.stringify({ params, stream: true, tool: toolName }), |
| headers, |
| method: 'POST', |
| signal |
| }); |
|
|
| if (!response.ok || !response.body) { |
| const detail = await formatNonOkResponse(response); |
|
|
| throw new Error(detail); |
| } |
|
|
| const iterator = parseSseJsonStream<ToolServerEvent>(response, signal); |
|
|
| while (true) { |
| const next: IteratorResult<SseJsonEvent<ToolServerEvent>> = await iterator.next(); |
|
|
| if (next.done) return; |
|
|
| const event = next.value.data; |
|
|
| if (event.chunk !== undefined) { |
| yield { chunk: event.chunk, done: false }; |
| } |
|
|
| if (event.done) { |
| yield { chunk: null, done: true, error: event.error }; |
|
|
| return; |
| } |
| } |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| export interface ToolStreamEvent { |
| chunk: string | null; |
| done: boolean; |
| error?: string; |
| } |
|
|
| |
| interface ToolServerEvent { |
| chunk?: string; |
| done?: boolean; |
| error?: string; |
| } |
|
|
| async function formatNonOkResponse(response: Response): Promise<string> { |
| const status = `${response.status} ${response.statusText}`.trim(); |
|
|
| try { |
| const errBody = (await response.clone().json()) as { error?: string; message?: string }; |
|
|
| if (errBody?.error) return `${status}: ${errBody.error}`; |
|
|
| if (errBody?.message) return `${status}: ${errBody.message}`; |
| } catch (error) { |
| console.error('[tools] Non-JSON error response, falling back to raw text:', error); |
| try { |
| const text = await response.text(); |
|
|
| if (text.trim()) return `${status}: ${text.trim()}`; |
| } catch (error) { |
| console.error('[tools] Failed to read error response as text:', error); |
| } |
| } |
|
|
| return status || `HTTP ${response.status}`; |
| } |
|
|