import { Mistral } from "@mistralai/mistralai"
import { Doconda } from "@doconda/sdk"
const mistral = new Mistral({ apiKey: process.env.MISTRAL_API_KEY })
const docondaTools = new Doconda().tools()
const tools = docondaTools.map((t) => ({ type: "function" as const, function: { name: t.name, description: t.description, parameters: t.inputSchema } }))
const messages: any[] = [{ role: "user", content: "Redacta un memo para el equipo: la oficina cierra el viernes" }]
while (true) {
const res = await mistral.chat.complete({ model: "mistral-large-latest", messages, tools })
const message = res.choices[0]?.message
if (!message) break
messages.push(message)
if (!message.toolCalls?.length) {
console.log(message.content)
break
}
for (const call of message.toolCalls) {
const args = typeof call.function.arguments === "string" ? JSON.parse(call.function.arguments) : call.function.arguments
const result = await docondaTools.find((t) => t.name === call.function.name)!.run(args)
messages.push({ role: "tool", name: call.function.name, toolCallId: call.id, content: JSON.stringify(result) })
}
}