claude-flow 3.10.4 → 3.10.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/v3/@claude-flow/cli/dist/src/benchmarks/gaia-agent-planning.smoke.d.ts +18 -0
- package/v3/@claude-flow/cli/dist/src/benchmarks/gaia-agent-planning.smoke.js +253 -0
- package/v3/@claude-flow/cli/dist/src/benchmarks/gaia-agent.d.ts +198 -0
- package/v3/@claude-flow/cli/dist/src/benchmarks/gaia-agent.js +651 -0
- package/v3/@claude-flow/cli/dist/src/benchmarks/gaia-causal-memory.d.ts +133 -0
- package/v3/@claude-flow/cli/dist/src/benchmarks/gaia-causal-memory.js +281 -0
- package/v3/@claude-flow/cli/dist/src/benchmarks/gaia-causal-memory.smoke.d.ts +22 -0
- package/v3/@claude-flow/cli/dist/src/benchmarks/gaia-causal-memory.smoke.js +300 -0
- package/v3/@claude-flow/cli/dist/src/benchmarks/gaia-convergence.d.ts +138 -0
- package/v3/@claude-flow/cli/dist/src/benchmarks/gaia-convergence.js +260 -0
- package/v3/@claude-flow/cli/dist/src/benchmarks/gaia-convergence.smoke.d.ts +19 -0
- package/v3/@claude-flow/cli/dist/src/benchmarks/gaia-convergence.smoke.js +246 -0
- package/v3/@claude-flow/cli/dist/src/benchmarks/gaia-critic.d.ts +123 -0
- package/v3/@claude-flow/cli/dist/src/benchmarks/gaia-critic.js +312 -0
- package/v3/@claude-flow/cli/dist/src/benchmarks/gaia-critic.smoke.d.ts +21 -0
- package/v3/@claude-flow/cli/dist/src/benchmarks/gaia-critic.smoke.js +327 -0
- package/v3/@claude-flow/cli/dist/src/benchmarks/gaia-decomposer.d.ts +125 -0
- package/v3/@claude-flow/cli/dist/src/benchmarks/gaia-decomposer.js +350 -0
- package/v3/@claude-flow/cli/dist/src/benchmarks/gaia-decomposer.smoke.d.ts +21 -0
- package/v3/@claude-flow/cli/dist/src/benchmarks/gaia-decomposer.smoke.js +228 -0
- package/v3/@claude-flow/cli/dist/src/benchmarks/gaia-e2e-smoke.d.ts +27 -0
- package/v3/@claude-flow/cli/dist/src/benchmarks/gaia-e2e-smoke.js +136 -0
- package/v3/@claude-flow/cli/dist/src/benchmarks/gaia-extract.smoke.d.ts +45 -0
- package/v3/@claude-flow/cli/dist/src/benchmarks/gaia-extract.smoke.js +242 -0
- package/v3/@claude-flow/cli/dist/src/benchmarks/gaia-hardness/features.d.ts +46 -0
- package/v3/@claude-flow/cli/dist/src/benchmarks/gaia-hardness/features.js +170 -0
- package/v3/@claude-flow/cli/dist/src/benchmarks/gaia-hardness/predictor.d.ts +105 -0
- package/v3/@claude-flow/cli/dist/src/benchmarks/gaia-hardness/predictor.js +260 -0
- package/v3/@claude-flow/cli/dist/src/benchmarks/gaia-hardness/predictor.smoke.d.ts +20 -0
- package/v3/@claude-flow/cli/dist/src/benchmarks/gaia-hardness/predictor.smoke.js +235 -0
- package/v3/@claude-flow/cli/dist/src/benchmarks/gaia-hardness/train-data-loader.d.ts +51 -0
- package/v3/@claude-flow/cli/dist/src/benchmarks/gaia-hardness/train-data-loader.js +179 -0
- package/v3/@claude-flow/cli/dist/src/benchmarks/gaia-judge.d.ts +88 -0
- package/v3/@claude-flow/cli/dist/src/benchmarks/gaia-judge.js +437 -0
- package/v3/@claude-flow/cli/dist/src/benchmarks/gaia-loader.d.ts +87 -0
- package/v3/@claude-flow/cli/dist/src/benchmarks/gaia-loader.js +326 -0
- package/v3/@claude-flow/cli/dist/src/benchmarks/gaia-tools/file_read.d.ts +35 -0
- package/v3/@claude-flow/cli/dist/src/benchmarks/gaia-tools/file_read.js +403 -0
- package/v3/@claude-flow/cli/dist/src/benchmarks/gaia-tools/grounded_query.d.ts +126 -0
- package/v3/@claude-flow/cli/dist/src/benchmarks/gaia-tools/grounded_query.js +225 -0
- package/v3/@claude-flow/cli/dist/src/benchmarks/gaia-tools/index.d.ts +32 -0
- package/v3/@claude-flow/cli/dist/src/benchmarks/gaia-tools/index.js +36 -0
- package/v3/@claude-flow/cli/dist/src/benchmarks/gaia-tools/types.d.ts +62 -0
- package/v3/@claude-flow/cli/dist/src/benchmarks/gaia-tools/types.js +12 -0
- package/v3/@claude-flow/cli/dist/src/benchmarks/gaia-tools/web_search.d.ts +30 -0
- package/v3/@claude-flow/cli/dist/src/benchmarks/gaia-tools/web_search.js +210 -0
- package/v3/@claude-flow/cli/dist/src/benchmarks/gaia-voting.d.ts +88 -0
- package/v3/@claude-flow/cli/dist/src/benchmarks/gaia-voting.js +297 -0
- package/v3/@claude-flow/cli/dist/src/benchmarks/gaia-voting.smoke.d.ts +20 -0
- package/v3/@claude-flow/cli/dist/src/benchmarks/gaia-voting.smoke.js +332 -0
- package/v3/@claude-flow/cli/dist/src/commands/gaia-bench.d.ts +40 -0
- package/v3/@claude-flow/cli/dist/src/commands/gaia-bench.js +597 -0
- package/v3/@claude-flow/cli/dist/src/commands/index.js +2 -0
- package/v3/@claude-flow/cli/dist/src/commands/init.js +41 -4
- package/v3/@claude-flow/cli/dist/src/init/executor.js +25 -6
- package/v3/@claude-flow/cli/dist/src/init/mcp-generator.js +9 -4
- package/v3/@claude-flow/cli/package.json +1 -1
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* GAIA Tool: grounded_query — ADR-135 / iter-33
|
|
3
|
+
*
|
|
4
|
+
* Calls Gemini 2.5 Flash with the google_search grounding tool and returns
|
|
5
|
+
* a synthesised factual answer plus source citations in a single API call.
|
|
6
|
+
*
|
|
7
|
+
* Architectural advantage over web_search (Google Custom Search):
|
|
8
|
+
* - web_search: raw snippets → agent reads + parses → 2-3 extra turns
|
|
9
|
+
* - grounded_query: Gemini synthesises an answer + cites sources in 1 call
|
|
10
|
+
*
|
|
11
|
+
* This is Ruflo's cross-provider stack in action:
|
|
12
|
+
* Gemini grounds (google_search tool) → Sonnet/Haiku reasons → answer
|
|
13
|
+
*
|
|
14
|
+
* Credential resolution (mirrors performance-capability.ts / web_search.ts):
|
|
15
|
+
* 1. process.env.GOOGLE_AI_API_KEY
|
|
16
|
+
* 2. gcloud secrets versions access latest --secret=GOOGLE_AI_API_KEY --project=ruv-dev
|
|
17
|
+
* 3. Throws with clear instructions if neither is available
|
|
18
|
+
*
|
|
19
|
+
* Free tier: 1500 grounded queries/day on Gemini 2.5 Flash.
|
|
20
|
+
* Pricing: ~$0.075/M input tokens, ~$0.30/M output tokens, grounding free <1500/day.
|
|
21
|
+
*
|
|
22
|
+
* Regression note (iter-47): this file was absent from feat/adr-135-integrate-tracks
|
|
23
|
+
* because the grounded-query-gemini branch was not cherry-picked during Track integration.
|
|
24
|
+
* The omission caused a 36-question empty-answer failure in iter-42 (13.2% vs 49.1% baseline).
|
|
25
|
+
*
|
|
26
|
+
* Refs: ADR-133, ADR-135, iter-30 research, iter-33, iter-47, #2156
|
|
27
|
+
* Live-tested 2026-05-27: Mercedes Sosa GAIA L1 question → HTTP 200, 4 sources.
|
|
28
|
+
*/
|
|
29
|
+
// ---------------------------------------------------------------------------
|
|
30
|
+
// Constants
|
|
31
|
+
// ---------------------------------------------------------------------------
|
|
32
|
+
const GEMINI_GENERATE_CONTENT_URL = 'https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent';
|
|
33
|
+
const DEFAULT_MAX_TOKENS = 1024;
|
|
34
|
+
const REQUEST_TIMEOUT_MS = 30_000;
|
|
35
|
+
// Gemini 2.5 Flash pricing (USD per 1M tokens, grounding free under 1500/day)
|
|
36
|
+
const PRICE_INPUT_PER_M = 0.075;
|
|
37
|
+
const PRICE_OUTPUT_PER_M = 0.30;
|
|
38
|
+
export const groundedQueryTestHooks = {};
|
|
39
|
+
// ---------------------------------------------------------------------------
|
|
40
|
+
// Credential resolution
|
|
41
|
+
// ---------------------------------------------------------------------------
|
|
42
|
+
/**
|
|
43
|
+
* Resolve a Google AI API key.
|
|
44
|
+
*
|
|
45
|
+
* Order: env var → gcloud secret → throw.
|
|
46
|
+
* Never returns null — throws with actionable guidance if no key is found.
|
|
47
|
+
*/
|
|
48
|
+
export async function resolveGoogleAIApiKey() {
|
|
49
|
+
// 1. Environment variable (fastest path, used in test mocks and CI)
|
|
50
|
+
const envKey = process.env['GOOGLE_AI_API_KEY'];
|
|
51
|
+
if (envKey)
|
|
52
|
+
return envKey;
|
|
53
|
+
// 2. GCP Secrets Manager (matches resolveApiKey pattern in gaia-bench.ts)
|
|
54
|
+
try {
|
|
55
|
+
const { execSync } = await import('node:child_process');
|
|
56
|
+
const key = execSync('gcloud secrets versions access latest --secret=GOOGLE_AI_API_KEY --project=ruv-dev 2>/dev/null', { encoding: 'utf-8', timeout: 5_000 }).trim();
|
|
57
|
+
if (key)
|
|
58
|
+
return key;
|
|
59
|
+
}
|
|
60
|
+
catch {
|
|
61
|
+
// gcloud not installed, project unreachable, or secret not yet created
|
|
62
|
+
}
|
|
63
|
+
throw new Error('grounded_query: No Google AI API key found.\n' +
|
|
64
|
+
'Set GOOGLE_AI_API_KEY env var, or ensure `gcloud` is authenticated and\n' +
|
|
65
|
+
'the secret GOOGLE_AI_API_KEY exists in GCP project ruv-dev.\n' +
|
|
66
|
+
'Get a free key at: https://aistudio.google.com/apikey');
|
|
67
|
+
}
|
|
68
|
+
// ---------------------------------------------------------------------------
|
|
69
|
+
// Gemini Grounding API call
|
|
70
|
+
// ---------------------------------------------------------------------------
|
|
71
|
+
/**
|
|
72
|
+
* Call Gemini 2.5 Flash with google_search grounding and parse the response.
|
|
73
|
+
*
|
|
74
|
+
* Exported so smoke tests can exercise the parser without live HTTP (via
|
|
75
|
+
* groundedQueryTestHooks.fetchResponse).
|
|
76
|
+
*/
|
|
77
|
+
export async function callGeminiGrounded(query, maxTokens, apiKey) {
|
|
78
|
+
const url = `${GEMINI_GENERATE_CONTENT_URL}?key=${encodeURIComponent(apiKey)}`;
|
|
79
|
+
const requestBody = {
|
|
80
|
+
contents: [{ parts: [{ text: query }] }],
|
|
81
|
+
tools: [{ google_search: {} }],
|
|
82
|
+
generationConfig: { maxOutputTokens: maxTokens },
|
|
83
|
+
};
|
|
84
|
+
let rawBody;
|
|
85
|
+
if (groundedQueryTestHooks.fetchResponse) {
|
|
86
|
+
rawBody = await groundedQueryTestHooks.fetchResponse(query, maxTokens, apiKey);
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
const resp = await fetch(url, {
|
|
90
|
+
method: 'POST',
|
|
91
|
+
headers: { 'Content-Type': 'application/json' },
|
|
92
|
+
signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS),
|
|
93
|
+
body: JSON.stringify(requestBody),
|
|
94
|
+
});
|
|
95
|
+
if (!resp.ok) {
|
|
96
|
+
const errText = (await resp.text()).slice(0, 400);
|
|
97
|
+
throw new Error(`grounded_query: Gemini HTTP ${resp.status}: ${errText}`);
|
|
98
|
+
}
|
|
99
|
+
rawBody = (await resp.json());
|
|
100
|
+
}
|
|
101
|
+
return parseGeminiResponse(rawBody);
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Parse a raw Gemini generateContent response into GroundedQueryResult.
|
|
105
|
+
* Exported for unit testing the parser independently.
|
|
106
|
+
*/
|
|
107
|
+
export function parseGeminiResponse(body) {
|
|
108
|
+
const candidate = body.candidates?.[0];
|
|
109
|
+
// Extract text answer
|
|
110
|
+
const answer = (candidate?.content?.parts ?? [])
|
|
111
|
+
.map((p) => p.text ?? '')
|
|
112
|
+
.join('');
|
|
113
|
+
// Extract grounding metadata
|
|
114
|
+
const meta = candidate?.groundingMetadata;
|
|
115
|
+
const chunks = meta?.groundingChunks ?? [];
|
|
116
|
+
const searchQueries = meta?.webSearchQueries ?? [];
|
|
117
|
+
// Build source list from grounding chunks (deduplicate by URI)
|
|
118
|
+
const seenUris = new Set();
|
|
119
|
+
const sources = [];
|
|
120
|
+
for (const chunk of chunks) {
|
|
121
|
+
const uri = chunk.web?.uri ?? '';
|
|
122
|
+
const title = chunk.web?.title ?? '';
|
|
123
|
+
if (uri && !seenUris.has(uri)) {
|
|
124
|
+
seenUris.add(uri);
|
|
125
|
+
sources.push({ title, uri });
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
// Estimate cost
|
|
129
|
+
const inputTokens = body.usageMetadata?.promptTokenCount ?? 0;
|
|
130
|
+
const outputTokens = body.usageMetadata?.candidatesTokenCount ?? 0;
|
|
131
|
+
const costUsd = (inputTokens / 1_000_000) * PRICE_INPUT_PER_M +
|
|
132
|
+
(outputTokens / 1_000_000) * PRICE_OUTPUT_PER_M;
|
|
133
|
+
return {
|
|
134
|
+
answer,
|
|
135
|
+
sources,
|
|
136
|
+
search_queries_used: searchQueries,
|
|
137
|
+
grounded: sources.length > 0 || searchQueries.length > 0,
|
|
138
|
+
model: 'gemini-2.5-flash',
|
|
139
|
+
cost_usd: costUsd,
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
// ---------------------------------------------------------------------------
|
|
143
|
+
// Format output for Claude (mirrors web_search formatResults pattern)
|
|
144
|
+
// ---------------------------------------------------------------------------
|
|
145
|
+
function formatResult(result) {
|
|
146
|
+
const lines = [];
|
|
147
|
+
lines.push(`[grounded_query model: ${result.model}]`);
|
|
148
|
+
if (result.answer) {
|
|
149
|
+
lines.push('');
|
|
150
|
+
lines.push(result.answer.trim());
|
|
151
|
+
}
|
|
152
|
+
if (result.sources.length > 0) {
|
|
153
|
+
lines.push('');
|
|
154
|
+
lines.push('Sources:');
|
|
155
|
+
for (let i = 0; i < result.sources.length; i++) {
|
|
156
|
+
const s = result.sources[i];
|
|
157
|
+
lines.push(`[${i + 1}] ${s.title}`);
|
|
158
|
+
lines.push(` ${s.uri}`);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
if (result.search_queries_used.length > 0) {
|
|
162
|
+
lines.push('');
|
|
163
|
+
lines.push(`Search queries: ${result.search_queries_used.join('; ')}`);
|
|
164
|
+
}
|
|
165
|
+
if (!result.grounded) {
|
|
166
|
+
lines.push('');
|
|
167
|
+
lines.push('[Note: No grounding metadata returned — answer is unverified.]');
|
|
168
|
+
}
|
|
169
|
+
return lines.join('\n');
|
|
170
|
+
}
|
|
171
|
+
// ---------------------------------------------------------------------------
|
|
172
|
+
// GaiaTool implementation
|
|
173
|
+
// ---------------------------------------------------------------------------
|
|
174
|
+
export class GroundedQueryTool {
|
|
175
|
+
name = 'grounded_query';
|
|
176
|
+
definition = {
|
|
177
|
+
name: 'grounded_query',
|
|
178
|
+
description: 'Query Google Search via Gemini 2.5 Flash grounding. ' +
|
|
179
|
+
'Returns a synthesised factual answer with source citations in a single call — ' +
|
|
180
|
+
'more efficient than web_search for factoid questions because Gemini pre-synthesises ' +
|
|
181
|
+
'the answer rather than returning raw snippets. ' +
|
|
182
|
+
'Use this for: factual lookups, "what year did X happen", "how many Y", ' +
|
|
183
|
+
'"who is Z", or any question requiring current web information with a clean answer. ' +
|
|
184
|
+
'Use web_search instead when you need to read a source page in full.',
|
|
185
|
+
input_schema: {
|
|
186
|
+
type: 'object',
|
|
187
|
+
properties: {
|
|
188
|
+
query: {
|
|
189
|
+
type: 'string',
|
|
190
|
+
description: 'Question or factual lookup to ground via Google Search.',
|
|
191
|
+
},
|
|
192
|
+
max_tokens: {
|
|
193
|
+
type: 'number',
|
|
194
|
+
description: `Maximum response length in tokens (default: ${DEFAULT_MAX_TOKENS}).`,
|
|
195
|
+
},
|
|
196
|
+
},
|
|
197
|
+
required: ['query'],
|
|
198
|
+
},
|
|
199
|
+
};
|
|
200
|
+
apiKey;
|
|
201
|
+
constructor(opts) {
|
|
202
|
+
this.apiKey = opts?.apiKey;
|
|
203
|
+
}
|
|
204
|
+
async execute(input) {
|
|
205
|
+
const query = String(input['query'] ?? '').trim();
|
|
206
|
+
if (!query) {
|
|
207
|
+
throw new Error('grounded_query: `query` input is required and must be non-empty.');
|
|
208
|
+
}
|
|
209
|
+
const maxTokens = Math.min(Math.max(64, Number(input['max_tokens'] ?? DEFAULT_MAX_TOKENS)), 8192);
|
|
210
|
+
const resolveKeyFn = groundedQueryTestHooks.resolveKey ?? resolveGoogleAIApiKey;
|
|
211
|
+
const apiKey = this.apiKey ?? (await resolveKeyFn());
|
|
212
|
+
process.stderr.write(`[grounded_query] model=gemini-2.5-flash query=${JSON.stringify(query)}\n`);
|
|
213
|
+
const result = await callGeminiGrounded(query, maxTokens, apiKey);
|
|
214
|
+
process.stderr.write(`[grounded_query] grounded=${result.grounded} sources=${result.sources.length} ` +
|
|
215
|
+
`cost_usd=${result.cost_usd.toFixed(6)}\n`);
|
|
216
|
+
return formatResult(result);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
// ---------------------------------------------------------------------------
|
|
220
|
+
// Convenience factory
|
|
221
|
+
// ---------------------------------------------------------------------------
|
|
222
|
+
export function createGroundedQueryTool(opts) {
|
|
223
|
+
return new GroundedQueryTool(opts);
|
|
224
|
+
}
|
|
225
|
+
//# sourceMappingURL=grounded_query.js.map
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* gaia-tools barrel — ADR-133-PR2
|
|
3
|
+
*
|
|
4
|
+
* Exports all tool implementations + shared types so that gaia-agent.ts
|
|
5
|
+
* (PR-3) and future tools (PR-4: python_exec, PR-5: web_browse) can import
|
|
6
|
+
* from a single entry point.
|
|
7
|
+
*
|
|
8
|
+
* iter-47 fix: re-added grounded_query which was absent from the integration
|
|
9
|
+
* branch because feat/adr-135-grounded-query-gemini was never cherry-picked
|
|
10
|
+
* during Track A/B/D/E/Q integration (iter-42 measured −36pp / 13.2%).
|
|
11
|
+
*
|
|
12
|
+
* Refs: ADR-133, ADR-135, #2156
|
|
13
|
+
*/
|
|
14
|
+
export * from './types.js';
|
|
15
|
+
export * from './web_search.js';
|
|
16
|
+
export * from './file_read.js';
|
|
17
|
+
export * from './grounded_query.js';
|
|
18
|
+
import type { GaiaToolCatalogue } from './types.js';
|
|
19
|
+
/**
|
|
20
|
+
* Returns the default tool catalogue for a GAIA Level-1 run.
|
|
21
|
+
*
|
|
22
|
+
* PR-2 catalogue: web_search + file_read
|
|
23
|
+
* iter-33 adds: grounded_query (Gemini 2.5 Flash grounding — pre-synthesised answer + citations)
|
|
24
|
+
* PR-4 will add: python_exec (E2B sandbox)
|
|
25
|
+
* PR-5 will add: web_browse, image_describe
|
|
26
|
+
*
|
|
27
|
+
* Both web_search and grounded_query are registered so the agent can choose:
|
|
28
|
+
* - grounded_query: for factoid questions needing a clean answer with citations (1 call)
|
|
29
|
+
* - web_search: for questions needing raw snippets or source page reading (multi-backend)
|
|
30
|
+
*/
|
|
31
|
+
export declare function createDefaultToolCatalogue(): GaiaToolCatalogue;
|
|
32
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* gaia-tools barrel — ADR-133-PR2
|
|
3
|
+
*
|
|
4
|
+
* Exports all tool implementations + shared types so that gaia-agent.ts
|
|
5
|
+
* (PR-3) and future tools (PR-4: python_exec, PR-5: web_browse) can import
|
|
6
|
+
* from a single entry point.
|
|
7
|
+
*
|
|
8
|
+
* iter-47 fix: re-added grounded_query which was absent from the integration
|
|
9
|
+
* branch because feat/adr-135-grounded-query-gemini was never cherry-picked
|
|
10
|
+
* during Track A/B/D/E/Q integration (iter-42 measured −36pp / 13.2%).
|
|
11
|
+
*
|
|
12
|
+
* Refs: ADR-133, ADR-135, #2156
|
|
13
|
+
*/
|
|
14
|
+
export * from './types.js';
|
|
15
|
+
export * from './web_search.js';
|
|
16
|
+
export * from './file_read.js';
|
|
17
|
+
export * from './grounded_query.js';
|
|
18
|
+
import { createWebSearchTool } from './web_search.js';
|
|
19
|
+
import { createFileReadTool } from './file_read.js';
|
|
20
|
+
import { createGroundedQueryTool } from './grounded_query.js';
|
|
21
|
+
/**
|
|
22
|
+
* Returns the default tool catalogue for a GAIA Level-1 run.
|
|
23
|
+
*
|
|
24
|
+
* PR-2 catalogue: web_search + file_read
|
|
25
|
+
* iter-33 adds: grounded_query (Gemini 2.5 Flash grounding — pre-synthesised answer + citations)
|
|
26
|
+
* PR-4 will add: python_exec (E2B sandbox)
|
|
27
|
+
* PR-5 will add: web_browse, image_describe
|
|
28
|
+
*
|
|
29
|
+
* Both web_search and grounded_query are registered so the agent can choose:
|
|
30
|
+
* - grounded_query: for factoid questions needing a clean answer with citations (1 call)
|
|
31
|
+
* - web_search: for questions needing raw snippets or source page reading (multi-backend)
|
|
32
|
+
*/
|
|
33
|
+
export function createDefaultToolCatalogue() {
|
|
34
|
+
return [createWebSearchTool(), createFileReadTool(), createGroundedQueryTool()];
|
|
35
|
+
}
|
|
36
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared types for the GAIA agent tool-use subsystem — ADR-133-PR2
|
|
3
|
+
*
|
|
4
|
+
* These types mirror the Anthropic Messages API `tool_use` / `tool_result`
|
|
5
|
+
* content block spec so that `gaia-agent.ts` (PR-3) can call the Anthropic
|
|
6
|
+
* SDK without an extra type-mapping layer.
|
|
7
|
+
*
|
|
8
|
+
* Refs: ADR-133, #2156
|
|
9
|
+
* https://docs.anthropic.com/en/api/messages
|
|
10
|
+
*/
|
|
11
|
+
export interface ToolInputSchema {
|
|
12
|
+
type: 'object';
|
|
13
|
+
properties: Record<string, {
|
|
14
|
+
type: string;
|
|
15
|
+
description?: string;
|
|
16
|
+
}>;
|
|
17
|
+
required?: string[];
|
|
18
|
+
}
|
|
19
|
+
export interface ToolDefinition {
|
|
20
|
+
name: string;
|
|
21
|
+
description: string;
|
|
22
|
+
input_schema: ToolInputSchema;
|
|
23
|
+
}
|
|
24
|
+
export interface ToolUseBlock {
|
|
25
|
+
type: 'tool_use';
|
|
26
|
+
id: string;
|
|
27
|
+
name: string;
|
|
28
|
+
input: Record<string, unknown>;
|
|
29
|
+
}
|
|
30
|
+
export interface ToolResultBlock {
|
|
31
|
+
type: 'tool_result';
|
|
32
|
+
tool_use_id: string;
|
|
33
|
+
content: string;
|
|
34
|
+
/** Optional — set to true when the tool fails so Claude can recover. */
|
|
35
|
+
is_error?: boolean;
|
|
36
|
+
}
|
|
37
|
+
export interface TextBlock {
|
|
38
|
+
type: 'text';
|
|
39
|
+
text: string;
|
|
40
|
+
}
|
|
41
|
+
export type ContentBlock = TextBlock | ToolUseBlock | ToolResultBlock;
|
|
42
|
+
export interface ToolCallResult {
|
|
43
|
+
/** The block that triggered this call. */
|
|
44
|
+
toolUse: ToolUseBlock;
|
|
45
|
+
/** String output to return to Claude (or error description). */
|
|
46
|
+
output: string;
|
|
47
|
+
/** True if the tool returned an error output. */
|
|
48
|
+
isError: boolean;
|
|
49
|
+
}
|
|
50
|
+
export interface GaiaTool {
|
|
51
|
+
/** Must match the `name` field in the ToolDefinition. */
|
|
52
|
+
readonly name: string;
|
|
53
|
+
/** The definition object passed to Anthropic in the `tools` array. */
|
|
54
|
+
readonly definition: ToolDefinition;
|
|
55
|
+
/**
|
|
56
|
+
* Execute the tool. Returns a plain string (success) or throws (will be
|
|
57
|
+
* caught by the agent loop and wrapped in an `is_error: true` result).
|
|
58
|
+
*/
|
|
59
|
+
execute(input: Record<string, unknown>): Promise<string>;
|
|
60
|
+
}
|
|
61
|
+
export type GaiaToolCatalogue = GaiaTool[];
|
|
62
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared types for the GAIA agent tool-use subsystem — ADR-133-PR2
|
|
3
|
+
*
|
|
4
|
+
* These types mirror the Anthropic Messages API `tool_use` / `tool_result`
|
|
5
|
+
* content block spec so that `gaia-agent.ts` (PR-3) can call the Anthropic
|
|
6
|
+
* SDK without an extra type-mapping layer.
|
|
7
|
+
*
|
|
8
|
+
* Refs: ADR-133, #2156
|
|
9
|
+
* https://docs.anthropic.com/en/api/messages
|
|
10
|
+
*/
|
|
11
|
+
export {};
|
|
12
|
+
//# sourceMappingURL=types.js.map
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* GAIA Tool: web_search — ADR-133-PR2
|
|
3
|
+
*
|
|
4
|
+
* Scrapes DuckDuckGo HTML search results for a query string and returns
|
|
5
|
+
* the top-N snippet titles + URLs as a plain-text block. No API key
|
|
6
|
+
* required; uses DDG's HTML endpoint which is publicly accessible.
|
|
7
|
+
*
|
|
8
|
+
* Design notes:
|
|
9
|
+
* - Uses native Node.js https/http (no external fetch polyfill).
|
|
10
|
+
* - Follows the DDG Lite HTML endpoint: https://html.duckduckgo.com/html/?q=…
|
|
11
|
+
* - Parses result titles + URLs via a simple regex (no DOM parser dependency).
|
|
12
|
+
* - Rate-limit aware: 1-second back-off between calls is the caller's
|
|
13
|
+
* responsibility (the agent loop enforces this in PR-3).
|
|
14
|
+
* - PDF / binary detection is handled by file_read.ts, not here.
|
|
15
|
+
*
|
|
16
|
+
* Refs: ADR-133, #2156
|
|
17
|
+
*/
|
|
18
|
+
import { GaiaTool, ToolDefinition } from './types.js';
|
|
19
|
+
export interface SearchResult {
|
|
20
|
+
title: string;
|
|
21
|
+
url: string;
|
|
22
|
+
snippet: string;
|
|
23
|
+
}
|
|
24
|
+
export declare class WebSearchTool implements GaiaTool {
|
|
25
|
+
readonly name = "web_search";
|
|
26
|
+
readonly definition: ToolDefinition;
|
|
27
|
+
execute(input: Record<string, unknown>): Promise<string>;
|
|
28
|
+
}
|
|
29
|
+
export declare function createWebSearchTool(): WebSearchTool;
|
|
30
|
+
//# sourceMappingURL=web_search.d.ts.map
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* GAIA Tool: web_search — ADR-133-PR2
|
|
3
|
+
*
|
|
4
|
+
* Scrapes DuckDuckGo HTML search results for a query string and returns
|
|
5
|
+
* the top-N snippet titles + URLs as a plain-text block. No API key
|
|
6
|
+
* required; uses DDG's HTML endpoint which is publicly accessible.
|
|
7
|
+
*
|
|
8
|
+
* Design notes:
|
|
9
|
+
* - Uses native Node.js https/http (no external fetch polyfill).
|
|
10
|
+
* - Follows the DDG Lite HTML endpoint: https://html.duckduckgo.com/html/?q=…
|
|
11
|
+
* - Parses result titles + URLs via a simple regex (no DOM parser dependency).
|
|
12
|
+
* - Rate-limit aware: 1-second back-off between calls is the caller's
|
|
13
|
+
* responsibility (the agent loop enforces this in PR-3).
|
|
14
|
+
* - PDF / binary detection is handled by file_read.ts, not here.
|
|
15
|
+
*
|
|
16
|
+
* Refs: ADR-133, #2156
|
|
17
|
+
*/
|
|
18
|
+
import * as https from 'node:https';
|
|
19
|
+
// ---------------------------------------------------------------------------
|
|
20
|
+
// Constants
|
|
21
|
+
// ---------------------------------------------------------------------------
|
|
22
|
+
const DDG_HTML_URL = 'https://html.duckduckgo.com/html/';
|
|
23
|
+
const DEFAULT_MAX_RESULTS = 5;
|
|
24
|
+
const REQUEST_TIMEOUT_MS = 20_000;
|
|
25
|
+
// User-Agent that DDG accepts (plain browser UA).
|
|
26
|
+
const UA = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36';
|
|
27
|
+
// ---------------------------------------------------------------------------
|
|
28
|
+
// HTML fetch helper
|
|
29
|
+
// ---------------------------------------------------------------------------
|
|
30
|
+
/**
|
|
31
|
+
* POST to DuckDuckGo's HTML search endpoint and return the raw HTML string.
|
|
32
|
+
* DDG blocks GET for automated scrapers but accepts POST form submissions.
|
|
33
|
+
*/
|
|
34
|
+
async function fetchDdgHtml(query) {
|
|
35
|
+
const body = `q=${encodeURIComponent(query)}&b=&kl=&df=`;
|
|
36
|
+
const bodyBytes = Buffer.from(body, 'utf-8');
|
|
37
|
+
return new Promise((resolve, reject) => {
|
|
38
|
+
const options = {
|
|
39
|
+
hostname: 'html.duckduckgo.com',
|
|
40
|
+
path: '/html/',
|
|
41
|
+
method: 'POST',
|
|
42
|
+
headers: {
|
|
43
|
+
'Content-Type': 'application/x-www-form-urlencoded',
|
|
44
|
+
'Content-Length': bodyBytes.length,
|
|
45
|
+
'User-Agent': UA,
|
|
46
|
+
Accept: 'text/html,application/xhtml+xml',
|
|
47
|
+
'Accept-Language': 'en-US,en;q=0.9',
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
const req = https.request(options, (res) => {
|
|
51
|
+
// Follow a single redirect if needed (DDG occasionally redirects to /html/)
|
|
52
|
+
if (res.statusCode !== undefined &&
|
|
53
|
+
res.statusCode >= 300 &&
|
|
54
|
+
res.statusCode < 400 &&
|
|
55
|
+
res.headers.location) {
|
|
56
|
+
const loc = res.headers.location;
|
|
57
|
+
res.resume();
|
|
58
|
+
// Simple follow — only handle absolute https redirects
|
|
59
|
+
if (loc.startsWith('https://')) {
|
|
60
|
+
https
|
|
61
|
+
.get(loc, { headers: { 'User-Agent': UA } }, (r2) => {
|
|
62
|
+
const chunks = [];
|
|
63
|
+
r2.on('data', (c) => chunks.push(c));
|
|
64
|
+
r2.on('end', () => resolve(Buffer.concat(chunks).toString('utf-8')));
|
|
65
|
+
r2.on('error', reject);
|
|
66
|
+
})
|
|
67
|
+
.on('error', reject);
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
reject(new Error(`Unexpected redirect target: ${loc}`));
|
|
71
|
+
}
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
if (res.statusCode !== 200) {
|
|
75
|
+
res.resume();
|
|
76
|
+
reject(new Error(`DDG returned HTTP ${res.statusCode ?? 'unknown'}`));
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
const chunks = [];
|
|
80
|
+
res.on('data', (c) => chunks.push(c));
|
|
81
|
+
res.on('end', () => resolve(Buffer.concat(chunks).toString('utf-8')));
|
|
82
|
+
res.on('error', reject);
|
|
83
|
+
});
|
|
84
|
+
req.on('error', reject);
|
|
85
|
+
req.setTimeout(REQUEST_TIMEOUT_MS, () => {
|
|
86
|
+
req.destroy(new Error(`web_search timeout after ${REQUEST_TIMEOUT_MS}ms`));
|
|
87
|
+
});
|
|
88
|
+
req.write(bodyBytes);
|
|
89
|
+
req.end();
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
// ---------------------------------------------------------------------------
|
|
93
|
+
// HTML parser (regex-based, no DOM)
|
|
94
|
+
// ---------------------------------------------------------------------------
|
|
95
|
+
/**
|
|
96
|
+
* Extract up to `maxResults` search results from DDG HTML.
|
|
97
|
+
*
|
|
98
|
+
* DDG's HTML result structure (stable as of 2026):
|
|
99
|
+
* <a class="result__a" href="URL">TITLE</a>
|
|
100
|
+
* <a class="result__snippet">SNIPPET</a>
|
|
101
|
+
*
|
|
102
|
+
* We parse with regex to avoid adding an htmlparser2 dependency.
|
|
103
|
+
*/
|
|
104
|
+
function parseDdgHtml(html, maxResults) {
|
|
105
|
+
const results = [];
|
|
106
|
+
// Match result blocks — DDG wraps each result in <div class="result …">
|
|
107
|
+
// We extract title+url from the result__a anchor, and snippet from result__snippet.
|
|
108
|
+
const resultBlockRe = /<a[^>]+class="result__a"[^>]*href="([^"]+)"[^>]*>([\s\S]*?)<\/a>[\s\S]*?(?:<a[^>]+class="result__snippet"[^>]*>([\s\S]*?)<\/a>)?/g;
|
|
109
|
+
let match;
|
|
110
|
+
while ((match = resultBlockRe.exec(html)) !== null && results.length < maxResults) {
|
|
111
|
+
const rawUrl = match[1] ?? '';
|
|
112
|
+
const rawTitle = match[2] ?? '';
|
|
113
|
+
const rawSnippet = match[3] ?? '';
|
|
114
|
+
// DDG wraps URLs in //duckduckgo.com/l/?uddg=ENCODED_URL
|
|
115
|
+
const url = decodeRawUrl(rawUrl);
|
|
116
|
+
const title = stripHtml(rawTitle).trim();
|
|
117
|
+
const snippet = stripHtml(rawSnippet).trim();
|
|
118
|
+
if (url && title) {
|
|
119
|
+
results.push({ title, url, snippet });
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
return results;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Decode the DDG redirect URL back to the real URL.
|
|
126
|
+
* Input example: //duckduckgo.com/l/?uddg=https%3A%2F%2Fexample.com%2F&rut=…
|
|
127
|
+
*/
|
|
128
|
+
function decodeRawUrl(raw) {
|
|
129
|
+
if (raw.startsWith('//duckduckgo.com/l/')) {
|
|
130
|
+
const qIdx = raw.indexOf('uddg=');
|
|
131
|
+
if (qIdx !== -1) {
|
|
132
|
+
const encoded = raw.slice(qIdx + 5).split('&')[0];
|
|
133
|
+
try {
|
|
134
|
+
return decodeURIComponent(encoded);
|
|
135
|
+
}
|
|
136
|
+
catch {
|
|
137
|
+
return raw;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
// Direct URL (some results skip the redirect)
|
|
142
|
+
if (raw.startsWith('http://') || raw.startsWith('https://'))
|
|
143
|
+
return raw;
|
|
144
|
+
return raw;
|
|
145
|
+
}
|
|
146
|
+
/** Strip HTML tags and decode common entities. */
|
|
147
|
+
function stripHtml(html) {
|
|
148
|
+
return html
|
|
149
|
+
.replace(/<[^>]+>/g, '')
|
|
150
|
+
.replace(/&/g, '&')
|
|
151
|
+
.replace(/</g, '<')
|
|
152
|
+
.replace(/>/g, '>')
|
|
153
|
+
.replace(/"/g, '"')
|
|
154
|
+
.replace(/'/g, "'")
|
|
155
|
+
.replace(/ /g, ' ')
|
|
156
|
+
.replace(/\s+/g, ' ')
|
|
157
|
+
.trim();
|
|
158
|
+
}
|
|
159
|
+
// ---------------------------------------------------------------------------
|
|
160
|
+
// Format output for Claude
|
|
161
|
+
// ---------------------------------------------------------------------------
|
|
162
|
+
function formatResults(results) {
|
|
163
|
+
if (results.length === 0) {
|
|
164
|
+
return 'No results found.';
|
|
165
|
+
}
|
|
166
|
+
return results
|
|
167
|
+
.map((r, i) => `[${i + 1}] ${r.title}\n URL: ${r.url}${r.snippet ? '\n ' + r.snippet : ''}`)
|
|
168
|
+
.join('\n\n');
|
|
169
|
+
}
|
|
170
|
+
// ---------------------------------------------------------------------------
|
|
171
|
+
// GaiaTool implementation
|
|
172
|
+
// ---------------------------------------------------------------------------
|
|
173
|
+
export class WebSearchTool {
|
|
174
|
+
name = 'web_search';
|
|
175
|
+
definition = {
|
|
176
|
+
name: 'web_search',
|
|
177
|
+
description: 'Search the web using DuckDuckGo and return the top results (title, URL, snippet). ' +
|
|
178
|
+
'Use this when you need current information, external facts, or to verify claims.',
|
|
179
|
+
input_schema: {
|
|
180
|
+
type: 'object',
|
|
181
|
+
properties: {
|
|
182
|
+
query: {
|
|
183
|
+
type: 'string',
|
|
184
|
+
description: 'The search query string.',
|
|
185
|
+
},
|
|
186
|
+
max_results: {
|
|
187
|
+
type: 'number',
|
|
188
|
+
description: `Maximum number of results to return (default: ${DEFAULT_MAX_RESULTS}, max: 10).`,
|
|
189
|
+
},
|
|
190
|
+
},
|
|
191
|
+
required: ['query'],
|
|
192
|
+
},
|
|
193
|
+
};
|
|
194
|
+
async execute(input) {
|
|
195
|
+
const query = String(input['query'] ?? '').trim();
|
|
196
|
+
if (!query)
|
|
197
|
+
throw new Error('web_search: `query` input is required and must be non-empty.');
|
|
198
|
+
const maxResults = Math.min(Math.max(1, Number(input['max_results'] ?? DEFAULT_MAX_RESULTS)), 10);
|
|
199
|
+
const html = await fetchDdgHtml(query);
|
|
200
|
+
const results = parseDdgHtml(html, maxResults);
|
|
201
|
+
return formatResults(results);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
// ---------------------------------------------------------------------------
|
|
205
|
+
// Convenience factory
|
|
206
|
+
// ---------------------------------------------------------------------------
|
|
207
|
+
export function createWebSearchTool() {
|
|
208
|
+
return new WebSearchTool();
|
|
209
|
+
}
|
|
210
|
+
//# sourceMappingURL=web_search.js.map
|