Open source file-system and knowledge based agent template. Build AI agents that stay up to date with your knowledge base
feat: add `web_search` tools (#36)
Hugoada83aaeedf8
diff --git a/apps/chat/app/components/ToolCallItem.vue b/apps/chat/app/components/ToolCallItem.vueindex 4ca5746..41b77cd 100644--- a/apps/chat/app/components/ToolCallItem.vue+++ b/apps/chat/app/components/ToolCallItem.vue@@ -22,6 +22,11 @@ const fullLabel = computed(() => {|| (args?.command ? [args.command as string] : undefined)|| (firstResultCmd?.title ? [firstResultCmd.title] : firstResultCmd?.command ? [firstResultCmd.command] : [])+ if (toolName === 'search_web') {+ const query = args?.query || '...'+ return state === 'loading' ? `Web search: "${query}"` : `Web search: "${query}"`+ }+if (toolName === 'search_and_read') {const query = args?.query || '...'return state === 'loading' ? `Searching "${query}"` : `Searched "${query}"`diff --git a/bun.lock b/bun.lockindex 24743fc..2147757 100644--- a/bun.lock+++ b/bun.lock@@ -69,7 +69,9 @@"name": "@savoir/agent","version": "0.1.0","dependencies": {+ "@ai-sdk/anthropic": "^3.0.45","@ai-sdk/gateway": "^3.0.45",+ "@ai-sdk/google": "^3.0.29","ai": "^6.0.85","evlog": "^1.9.0","zod": "^4.3.6",@@ -110,8 +112,12 @@"minimatch": "^10.0.1",},"packages": {+ "@ai-sdk/anthropic": ["@ai-sdk/[email protected]", "", { "dependencies": { "@ai-sdk/provider": "3.0.8", "@ai-sdk/provider-utils": "4.0.15" }, "peerDependencies": { "zod": "^3.25.76 || ^4.1.8" } }, "sha512-bpIS3RakSsaUhCRTIvL9bcVNeeUMDXWbndpYdXNeMJIIPcElTcvwktvla+JxIfbeK1AdQjB8ggYVChepeXPGwQ=="],+"@ai-sdk/gateway": ["@ai-sdk/[email protected]", "", { "dependencies": { "@ai-sdk/provider": "3.0.8", "@ai-sdk/provider-utils": "4.0.15", "@vercel/oidc": "3.1.0" }, "peerDependencies": { "zod": "^3.25.76 || ^4.1.8" } }, "sha512-zH1UbNRjG5woOXXFOrVCZraqZuFTtmPvLardMGcgLkzpxKV0U3tAGoyWKSZ862H+eBJfI/Hf2yj/zzGJcCkycg=="],+ "@ai-sdk/google": ["@ai-sdk/[email protected]", "", { "dependencies": { "@ai-sdk/provider": "3.0.8", "@ai-sdk/provider-utils": "4.0.15" }, "peerDependencies": { "zod": "^3.25.76 || ^4.1.8" } }, "sha512-x0hcU10AA+i1ZUQHloGD5qXWsB+Y8qnxlmFUef6Ly4rB53MGVbQExkI9nOKiCO3mu2TGiiNoQMeKWSeQVLfRUA=="],+"@ai-sdk/provider": ["@ai-sdk/[email protected]", "", { "dependencies": { "json-schema": "^0.4.0" } }, "sha512-oGMAgGoQdBXbZqNG0Ze56CHjDZ1IDYOwGYxYjO5KLSlz5HiNQ9udIXsPZ61VWaHGZ5XW/jyjmr6t2xz2jGVwbQ=="],"@ai-sdk/provider-utils": ["@ai-sdk/[email protected]", "", { "dependencies": { "@ai-sdk/provider": "3.0.8", "@standard-schema/spec": "^1.1.0", "eventsource-parser": "^3.0.6" }, "peerDependencies": { "zod": "^3.25.76 || ^4.1.8" } }, "sha512-8XiKWbemmCbvNN0CLR9u3PQiet4gtEVIrX4zzLxnCj06AwsEDJwJVBbKrEI4t6qE8XRSIvU2irka0dcpziKW6w=="],diff --git a/packages/agent/package.json b/packages/agent/package.jsonindex 4b25d01..a3271cb 100644--- a/packages/agent/package.json+++ b/packages/agent/package.json@@ -24,7 +24,9 @@"lint:fix": "eslint . --fix"},"dependencies": {+ "@ai-sdk/anthropic": "^3.0.45","@ai-sdk/gateway": "^3.0.45",+ "@ai-sdk/google": "^3.0.29","ai": "^6.0.85","evlog": "^1.9.0","zod": "^4.3.6"diff --git a/packages/agent/src/agents/base.ts b/packages/agent/src/agents/base.tsindex 405a020..4d6f848 100644--- a/packages/agent/src/agents/base.ts+++ b/packages/agent/src/agents/base.ts@@ -5,6 +5,7 @@ import { compactContext } from '../core/context'import { callOptionsSchema } from '../core/schemas'import { sanitizeToolCallInputs } from '../core/sanitize'import { countConsecutiveToolSteps, shouldForceTextOnlyStep } from '../core/policy'+import { getSearchTools } from '../tools/model-config'import type { AgentCallOptions, AgentExecutionContext, CreateAgentOptions } from '../types'export function createAgent({@@ -53,7 +54,7 @@ export function createAgent({...settings,model: effectiveModel,instructions: buildPrompt(routerConfig, agentConfig),- tools,+ tools: { ...tools, ...getSearchTools(effectiveModel) },stopWhen: stepCountIs(effectiveMaxSteps),experimental_context: executionContext,}diff --git a/packages/agent/src/agents/source.ts b/packages/agent/src/agents/source.tsindex 8c1a31f..f58862b 100644--- a/packages/agent/src/agents/source.ts+++ b/packages/agent/src/agents/source.ts@@ -8,6 +8,7 @@ import { compactContext } from '../core/context'import { callOptionsSchema } from '../core/schemas'import { sanitizeToolCallInputs } from '../core/sanitize'import { countConsecutiveToolSteps, shouldForceTextOnlyStep } from '../core/policy'+import { getSearchTools } from '../tools/model-config'import type { AgentConfigData, AgentCallOptions, AgentExecutionContext, RoutingResult } from '../types'export interface SourceAgentOptions {@@ -70,7 +71,7 @@ export function createSourceAgent({...settings,model: effectiveModel,instructions: applyComplexity(buildChatSystemPrompt(agentConfig), routerConfig),- tools,+ tools: { ...tools, ...getSearchTools(effectiveModel) },stopWhen: stepCountIs(effectiveMaxSteps),experimental_context: executionContext,}diff --git a/packages/agent/src/index.ts b/packages/agent/src/index.tsindex 7ed51d7..f38f52b 100644--- a/packages/agent/src/index.ts+++ b/packages/agent/src/index.ts@@ -1,3 +1,6 @@+// Tools+export { getSearchTools } from './tools/model-config'+// Agentsexport { createSourceAgent } from './agents/source'export type { SourceAgentOptions } from './agents/source'diff --git a/packages/agent/src/prompts/bot.ts b/packages/agent/src/prompts/bot.tsindex f816723..2ddb1e5 100644--- a/packages/agent/src/prompts/bot.ts+++ b/packages/agent/src/prompts/bot.ts@@ -50,6 +50,20 @@ Use \`| head -N\` on all search output. Use \`grep -rlE "term1|term2"\` for mult4. \`cat docs/source1/file1.md\`5. \`cat docs/source2/file2.md\`+## Web Search++You have access to a \`search_web\` tool for finding information NOT in the sandbox.++**Use search_web when:**+- The sandbox search yields no relevant results+- Questions about current events, release dates, or recent changes+- Third-party libraries or services not covered in the sandbox++**Do NOT use search_web when:**+- The question is answerable from sandbox documentation (always search sandbox FIRST)++**Priority:** sandbox docs (bash_batch) → web search → general knowledge+## Response Style- Be concise and helpfuldiff --git a/packages/agent/src/prompts/chat.ts b/packages/agent/src/prompts/chat.tsindex 56c8090..f1bf589 100644--- a/packages/agent/src/prompts/chat.ts+++ b/packages/agent/src/prompts/chat.ts@@ -101,6 +101,20 @@ bash_batch: [- Prefer \`grep -rl\` over \`grep -r\` — file paths are more useful than content dumps.- 1–2 batched calls beats 5 sequential ones.+## Web Search++You have access to a \`search_web\` tool for finding information NOT in the sandbox.++**Use search_web when:**+- The sandbox search yields no relevant results+- Questions about current events, release dates, or recent changes+- Third-party libraries or services not covered in the sandbox++**Do NOT use search_web when:**+- The question is answerable from sandbox documentation (always search sandbox FIRST)++**Priority:** sandbox docs (bash_batch) → web search → general knowledge+## Response Style- Be concise and helpfuldiff --git a/packages/agent/src/prompts/router.ts b/packages/agent/src/prompts/router.tsindex 6eb24e7..0b775cc 100644--- a/packages/agent/src/prompts/router.ts+++ b/packages/agent/src/prompts/router.ts@@ -22,4 +22,6 @@ Analyze the user's question and determine the appropriate configuration for the- Debugging scenarios describing errors or unexpected behavior- Architecture questions spanning multiple systems- Deep analysis requiring cross-referencing many files-- Examples: "My SSR hydration is failing with this error...", "How to build a multi-tenant app with NuxtHub?", "Design a full auth flow with sessions and roles"`+- Examples: "My SSR hydration is failing with this error...", "How to build a multi-tenant app with NuxtHub?", "Design a full auth flow with sessions and roles"++**Note:** Questions referencing current events, recent releases, or topics unlikely to be covered in the sandbox documentation should be classified as at least **moderate** to allow the agent enough steps for web search.`diff --git a/packages/agent/src/prompts/shared.ts b/packages/agent/src/prompts/shared.tsindex a49da44..81617bb 100644--- a/packages/agent/src/prompts/shared.ts+++ b/packages/agent/src/prompts/shared.ts@@ -42,9 +42,9 @@ export const COMPLEXITY_HINTS: Record<AgentConfig['complexity'], string> = {simple:'One bash_batch call: search + read in parallel, then answer.\nbash_batch: ["grep -rl \'keyword\' docs/ --include=\'*.md\' | head -5", "head -100 docs/likely-source/index.md"]\nThen answer.',moderate:- 'One bash_batch: parallel grep across likely directories, then one batch read.\nbash_batch: ["grep -rl \'keyword\' docs/source1/ --include=\'*.md\' | head -5", "grep -rl \'keyword\' docs/source2/ --include=\'*.md\' | head -5"]\nThen bash_batch: read the top results.\nAnswer with what you found.',+ 'One bash_batch: parallel grep across likely directories, then one batch read.\nbash_batch: ["grep -rl \'keyword\' docs/source1/ --include=\'*.md\' | head -5", "grep -rl \'keyword\' docs/source2/ --include=\'*.md\' | head -5"]\nThen bash_batch: read the top results.\nIf sandbox results are insufficient, use `search_web` for supplementary information.\nAnswer with what you found.',complex:- 'Wide search then deep read:\n1. bash_batch: parallel grep -rl across all relevant directories\n2. bash_batch: read top files with head -100, use grep -n -C3 for specific sections\n3. Cross-reference sources, then answer.',+ 'Wide search then deep read:\n1. bash_batch: parallel grep -rl across all relevant directories\n2. bash_batch: read top files with head -100, use grep -n -C3 for specific sections\n3. If needed, `search_web` for external context or recent information\n4. Cross-reference sources, then answer.',}export function applyComplexity(prompt: string, agentConfig: AgentConfig): string {diff --git a/packages/agent/src/tools/model-config.ts b/packages/agent/src/tools/model-config.tsnew file mode 100644index 0000000..bcc3caa--- /dev/null+++ b/packages/agent/src/tools/model-config.ts@@ -0,0 +1,19 @@+import { anthropic } from '@ai-sdk/anthropic'+import { google } from '@ai-sdk/google'++/**+ * Returns provider-native search tools for the given model.+ * These are provider-defined tools executed server-side by the provider.+ */+export function getSearchTools(modelId: string): Record<string, unknown> {+ const [provider] = modelId.split('/')++ switch (provider) {+ case 'anthropic':+ return { web_search: anthropic.tools.webSearch_20250305() }+ case 'google':+ return { google_search: google.tools.googleSearch({}) }+ default:+ return {}+ }+}