Integrating Local Tool Functionality

implementationChallenge

Prompt Content

Create a client-side tool (e.g., `getLocalTime` which returns the current local time) and integrate it with your AI SDK agent. The agent should be able to call this tool when appropriate (e.g., if the user asks 'What time is it?'). Show how to define the tool and pass it to `useChat` or `streamText`.

```typescript
// lib/tools.ts (example tool definition)
export async function getLocalTime() {
  return new Date().toLocaleTimeString();
}

export const tools = {
  getLocalTime: {
    description: 'Gets the current local time.',
    parameters: { type: 'object', properties: {} },
    execute: getLocalTime,
  },
};

// app/page.tsx (client component, using useChat with tools)
// ...
import { useChat } from 'ai/react';
import { tools } from '../lib/tools';

export default function Chat() {
  const { messages, input, handleInputChange, handleSubmit } = useChat({
    api: '/api/chat', // Your API route
    tools: tools, // Pass your tools here
  });

  // ... rest of your component
}

// api/chat/route.ts (server-side, to enable tool calling on the model)
// ...
import { experimental_streamText } from 'ai'; // Use experimental for tool calling
// ...

const result = await experimental_streamText({
  model: modelToUse,
  messages,
  tools: {
    getLocalTime: {
      description: 'Gets the current local time.',
      parameters: { type: 'object', properties: {} },
    },
  },
});
```

Try this prompt

Open the workspace to execute this prompt with free credits, or use your own API keys for unlimited usage.

Usage Tips

Copy the prompt and paste it into your preferred AI tool (Claude, ChatGPT, Gemini)

Customize placeholder values with your specific requirements and context

For best results, provide clear examples and test different variations