import Anthropic from "@anthropic-ai/sdk";
const client = new Anthropic({ baseURL: "https://api.naga.ac", apiKey: process.env.NAGAAI_API_KEY });
const tools: Anthropic.Tool[] = [{
name: "get_weather",
description: "Current weather for a city.",
input_schema: {
type: "object",
properties: { city: { type: "string" } },
required: ["city"],
},
}];
const getWeather = ({ city }: { city: string }) => ({ city, temperature_c: 18, sky: "cloudy" });
const messages: Anthropic.MessageParam[] = [{ role: "user", content: "What is the weather in Prague?" }];
let message: Anthropic.Message;
while (true) {
message = await client.messages.create({ model: "claude-sonnet-5", max_tokens: 1024, tools, messages });
messages.push({ role: "assistant", content: message.content });
if (message.stop_reason !== "tool_use") break;
const results: Anthropic.ToolResultBlockParam[] = message.content
.filter((block): block is Anthropic.ToolUseBlock => block.type === "tool_use")
.map((block) => ({
type: "tool_result",
tool_use_id: block.id,
content: JSON.stringify(getWeather(block.input as { city: string })),
}));
messages.push({ role: "user", content: results });
}
for (const block of message.content) {
if (block.type === "text") console.log(block.text);
}