]> git.djapps.eu Git - pkg/ggml/sources/llama.cpp/blob
6114f17b5bde1a19ba9a912496409300259b0df5
[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 { parsePartialJsonArgs } from '$lib/utils/parse-partial-json-args';
9 import type { AgenticSection } from '$lib/utils/agentic';
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 if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) {
21 return parsed as Record<string, unknown>;
22 }
23 return null;
24 } catch {
25 return null;
26 }
27 }
28
29 /**
30 * Parse a section's toolArgs against an expected tool name. Returns
31 * `null` when:
32 * - the section's toolName doesn't match (component isn't for this
33 * tool);
34 * - the section has no args yet (call hasn't started streaming);
35 * - or the args blob can't be parsed.
36 *
37 * Pass `{ partial: true }` for tools that need to render incrementally
38 * as each token lands (read_file, edit_file, write_file).
39 */
40 export function parseToolArgs(
41 expected: BuiltInTool,
42 section: AgenticSection,
43 options: { partial?: boolean } = {}
44 ): Record<string, unknown> | null {
45 if (section.toolName !== expected || !section.toolArgs) return null;
46 return options.partial
47 ? parsePartialJsonArgs(section.toolArgs)
48 : parseFinalToolArgs(section.toolArgs);
49 }