-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtool.js
More file actions
86 lines (74 loc) · 3.02 KB
/
Copy pathtool.js
File metadata and controls
86 lines (74 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
'use strict';
// Manual example: register a local `get_weather` tool and let the agent call it.
//
// The agent runs in the cloud, but `get_weather` runs HERE on your machine.
// When the agent decides it needs weather, the cloud sends a `tool_request`; the
// client runs your function locally and POSTs the result back, then the run
// continues.
//
// export DRIVER_API_KEY=dr_xxxxxxxx
// node examples/tool.js "what should I wear in Barcelona today?"
// node examples/tool.js --zdr "what should I wear in Barcelona today?"
//
// --zdr runs with zero data retention (needs the account entitlement; the
// server answers 403 without it). Client tools work exactly the same either
// way — the tool_request round trip is live streaming, not retention.
//
// Optional:
// export DRIVER_BASE_URL=https://driver.tors.app
// export DRIVER_DEBUG=1 # dump raw events
const { Driver, defineTool } = require('..');
// A fake weather source. Swap the body for a real API call (fetch, axios, …);
// it runs locally, so it can use your network, keys, and secrets.
const FORECAST = {
barcelona: [24, 'sunny'],
london: [14, 'rainy'],
oslo: [3, 'snowy'],
};
// Runs locally on tool_request. Positional args are spread in.
function getWeather(city = '', units = 'celsius') {
const [tempC, sky] = FORECAST[city.trim().toLowerCase()] || [20, 'clear'];
const temp = units === 'celsius' ? tempC : Math.round((tempC * 9) / 5 + 32);
return { city, temp, units, conditions: sky };
}
async function main() {
if (!process.env.DRIVER_API_KEY) {
console.error('set DRIVER_API_KEY (dr_...) — get one from the dashboard');
process.exit(2);
}
const args = process.argv.slice(2);
const zdr = args.includes('--zdr');
const prompt = args.filter((a) => a !== '--zdr').join(' ') || 'what should I wear in Barcelona today?';
const weather = defineTool({
name: 'get_weather',
description: 'Get the current weather for a city.',
params: [
{ name: 'city', description: "city name, e.g. 'Barcelona'" },
{ name: 'units', type: 'string', description: "'celsius' or 'fahrenheit'" },
],
call: getWeather,
});
const driver = new Driver({ tools: [weather] }); // reads DRIVER_API_KEY / DRIVER_BASE_URL
if (process.env.DRIVER_DEBUG) {
driver.on('event', (ev) => console.error('RAW', JSON.stringify(ev)));
}
driver.on('plan', (ev) => {
console.log('\n[plan]');
(ev.items || []).forEach((it, i) => console.log(` ${i + 1}. ${it}`));
});
driver.on('plan_item_start', (ev) => console.log(`\n[->] ${ev.num + 1}. ${ev.def}`));
driver.on('action', (ev) => console.log(` . ${ev.tool}${ev.is_network ? ' [net]' : ''}`));
console.log(`prompt: ${prompt}${zdr ? ' [zdr]' : ''}`);
try {
const done = await driver.run(prompt, { zdr });
console.log(`\n[done] steps=${done.steps} errors=${done.errors}`);
console.log('RESULT:', done.result);
} catch (e) {
console.error(`\nfatal: ${e.message}`);
process.exit(1);
}
}
main().catch((e) => {
console.error('\nfailed:', e.message);
process.exit(1);
});