char-fork/llama.cpp

git clone https://git.t4t.associates/char-fork/llama.cpp

Xuan-Son Nguyenui: add browser get_info tool (#27251)7077abbe1

char/aliceai-t5-moe
1.3 KiB39 linesraw
1/**
2 * Browser fallback for the server's `get_info` tool, offered only when the
3 * server does not serve one (llama-server without --agent). It tells the model
4 * which OS the browser runs on and that there is no local file or shell access,
5 * so it does not plan around tools that are not there.
6 *
7 * @see server_tool_get_info in tools/server/server-tools.cpp - the served variant
8 * @see buildBrowserInfoToolDefinition in constants/browser-info.ts - tool schema sent to the LLM
9 */
10
11import { browser } from '$app/environment';
12import {
13	BROWSER_INFO_NOTE,
14	BROWSER_INFO_OS_UA_PATTERNS,
15	BROWSER_INFO_OS_UNKNOWN
16} from '$lib/constants';
17import type { ToolExecutionResult } from '$lib/types';
18
19function detectOs(userAgent: string): string {
20	for (const [pattern, os] of BROWSER_INFO_OS_UA_PATTERNS) {
21		if (pattern.test(userAgent)) return os;
22	}
23
24	return BROWSER_INFO_OS_UNKNOWN;
25}
26
27/**
28 * Result shape mirrors the server tool's JSON so the `get_info` renderer reads
29 * `os` the same way, minus `cwd` - there is no working directory to report.
30 */
31export function executeBrowserInfoTool(): ToolExecutionResult {
32	return {
33		content: JSON.stringify({
34			note: BROWSER_INFO_NOTE,
35			os: browser ? detectOs(navigator.userAgent) : BROWSER_INFO_OS_UNKNOWN
36		}),
37		isError: false
38	};
39}