vercel / ai

Build AI-powered applications with React, Svelte, Vue, and Solid
https://sdk.vercel.ai/docs
Other
8.58k stars 1.21k forks source link

SvelteKit: support onToolCall / maxToolRoundtrips / tool invocations #1979

Open chanmathew opened 2 weeks ago

chanmathew commented 2 weeks ago

Description

Hi there,

The onToolCall callback doesn't seem to fire when in a Sveltekit project. See this for repro: https://stackblitz.com/edit/vercel-ai-sdk-ontoolcall-not-firing?file=src%2Froutes%2F%2Bpage.svelte

I've tested this on local environment as well and same issue.

Code example

// client
  const { input, handleSubmit, messages } = useChat({
    async onToolCall({ toolCall }) {
        await console.log(toolCall)
    }
  });
// server
const openai = createOpenAI({
    apiKey: env.OPENAI_API_KEY ?? ''
});

export const POST = (async ({ request }) => {
    const { messages } = await request.json();

    const result = await streamText({
        model: openai('gpt-4o'),
        messages,
        tools: {
            call_tool: tool({
                description: 'Fire this tool call when user asks',
                parameters: z.object({
                    tool: z.boolean()
                })
            })
        }
    });

    return result.toAIStreamResponse();
}) satisfies RequestHandler;

Additional context

No response

lgrammel commented 2 weeks ago

useChat tool calling is not supported in Svelte yet: https://sdk.vercel.ai/docs/reference/ai-sdk-ui#ui-framework-support

chanmathew commented 2 weeks ago

useChat tool calling is not supported in Svelte yet: https://sdk.vercel.ai/docs/reference/ai-sdk-ui#ui-framework-support

Ah apologies, missed that!

MagsMagnoli commented 2 weeks ago

@lgrammel is there a ticket or some way to track that progress? (if it is in progress even?)

lgrammel commented 1 week ago

we could track it here. help would be much appreciated!