]> git.djapps.eu Git - pkg/ggml/sources/llama.cpp/blob
d8ca812b645482679893145e6b412d8e20b0b06f
[pkg/ggml/sources/llama.cpp] /
1 // Helpers shared by the per-tool meta parsers under
2 // `src/lib/components/app/chat/ChatMessages/ChatMessage/ChatMessageToolCall/parsers/`.
3 // Each tool needs the same first three steps (tool-name check,
4 // args-present check, JSON parse) - keeping them here lets each parser
5 // stay focused on its own format quirks.
6
7 import { BuiltInTool } from '$lib/enums';
8 import type { AgenticSection } from '$lib/utils/agentic';
9 import { parsePartialJsonArgs } from '$lib/utils/parse-partial-json-args';
10
11 /**
12 * Strict (final-state) JSON parser for a tool-args blob. Mirrors the
13 * behaviour the per-tool components used before extraction: an
14 * invalid JSON blob, a JSON array, or a JSON primitive all map to
15 * `null` so callers don't have to guard against surprise shapes.
16 */
17 function parseFinalToolArgs(blob: string): Record<string, unknown> | null {
18 try {
19 const parsed: unknown = JSON.parse(blob);
20
21 if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) {
22 return parsed as Record<string, unknown>;
23 }
24
25 return null;
26 } catch {
27 return null;
28 }
29 }
30
31 /**
32 * Parse a section's toolArgs against an expected tool name. Returns
33 * `null` when:
34 * - the section's toolName doesn't match (component isn't for this
35 * tool);
36 * - the section has no args yet (call hasn't started streaming);
37 * - or the args blob can't be parsed.
38 *
39 * Pass `{ partial: true }` for tools that need to render incrementally
40 * as each token lands (read_file, edit_file, write_file).
41 */
42 export function parseToolArgs(
43 expected: BuiltInTool,
44 section: AgenticSection,
45 options: { partial?: boolean } = {}
46 ): Record<string, unknown> | null {
47 if (section.toolName !== expected || !section.toolArgs) return null;
48
49 return options.partial
50 ? parsePartialJsonArgs(section.toolArgs)
51 : parseFinalToolArgs(section.toolArgs);
52 }