const API_KEY = 'YOUR_API_KEY';
const BASE_URL = 'https://api.smooth.sh/api/v1';
// Define your tool implementations
const toolImplementations = {
async get_weather({ city }) {
// In reality, you'd call a weather API
const weatherData = {
'New York': { temperature: 72, conditions: 'Partly cloudy', humidity: 45 },
'London': { temperature: 58, conditions: 'Rainy', humidity: 80 },
'Tokyo': { temperature: 68, conditions: 'Clear', humidity: 55 }
};
const weather = weatherData[city];
if (!weather) {
throw new Error(`Weather data not available for: ${city}`);
}
return weather;
},
async send_slack_message({ channel, message }) {
// In reality, you'd use the Slack API
console.log(`[Slack #${channel}] ${message}`);
return true;
}
};
async function request(method, path, body = null) {
const response = await fetch(`${BASE_URL}${path}`, {
method,
headers: {
'apikey': API_KEY,
'Content-Type': 'application/json'
},
body: body ? JSON.stringify(body) : null
});
return response.json();
}
async function handleToolCall(taskId, event) {
const { name, input } = event.payload;
console.log(`Tool called: ${name}`, input);
let response;
try {
const implementation = toolImplementations[name];
if (!implementation) {
throw new Error(`Unknown tool: ${name}`);
}
const output = await implementation(input);
response = { code: 200, output };
} catch (error) {
response = { code: 400, output: error.message };
}
// Send the response
await request('POST', `/task/${taskId}/event`, {
name: 'tool_call',
payload: response,
id: event.id
});
console.log(`Tool response sent for ${name}:`, response);
}
async function runTaskWithTools() {
// Create task with custom tools
const { r: task } = await request('POST', '/task', {
task: 'Look up the weather in New York and send a summary to the #general Slack channel',
custom_tools: [
{
name: 'get_weather',
description: 'Get current weather for a city',
inputs: {
type: 'object',
properties: {
city: { type: 'string', description: 'The city name' }
},
required: ['city']
},
output: 'Weather data with temperature, conditions, humidity'
},
{
name: 'send_slack_message',
description: 'Send a message to a Slack channel',
inputs: {
type: 'object',
properties: {
channel: { type: 'string', description: 'Channel name' },
message: { type: 'string', description: 'Message to send' }
},
required: ['channel', 'message']
},
output: 'Boolean indicating success'
}
]
});
console.log(`Task created: ${task.id}`);
console.log(`Live URL: ${task.live_url}`);
// Poll and handle tool calls
let lastEventT = 0;
const processedEvents = new Set();
while (true) {
const { r: taskStatus } = await request('GET', `/task/${task.id}?event_t=${lastEventT}`);
// Check if task is done
if (!['running', 'waiting'].includes(taskStatus.status)) {
console.log(`Task finished with status: ${taskStatus.status}`);
console.log('Output:', taskStatus.output);
break;
}
// Process tool calls
if (taskStatus.events) {
for (const event of taskStatus.events) {
if (event.name === 'tool_call' && !processedEvents.has(event.id)) {
processedEvents.add(event.id);
await handleToolCall(task.id, event);
}
}
lastEventT = taskStatus.events[taskStatus.events.length - 1].timestamp;
}
await new Promise(r => setTimeout(r, 1000));
}
}
runTaskWithTools().catch(console.error);