### Install Dependencies Source: https://github.com/cloudflare/agents-starter/blob/main/README.md Installs the necessary Node.js dependencies for the project. ```bash npm install ``` -------------------------------- ### Run Local Development Server Source: https://github.com/cloudflare/agents-starter/blob/main/README.md Starts the local development server for the chat agent application. ```bash npm start ``` -------------------------------- ### Define Scheduling Tool Source: https://github.com/cloudflare/agents-starter/blob/main/README.md Example of defining a tool for scheduling tasks with options for date, delay, or cron patterns. ```typescript const scheduleTask = tool({ description: "schedule a task to be executed at a later time. 'when' can be a date, a delay in seconds, or a cron pattern.", parameters: z.object({ type: z.enum(["scheduled", "delayed", "cron"]), when: z.union([z.number(), z.string()]), payload: z.string() }), execute: async ({ type, when, payload }) => { // ... see the implementation in tools.ts } }); ``` -------------------------------- ### Install Workers AI Provider Source: https://github.com/cloudflare/agents-starter/blob/main/README.md Installs the workers-ai-provider package for use with the ai-sdk. ```sh npm install workers-ai-provider ``` -------------------------------- ### Define Tools with Confirmation Source: https://github.com/cloudflare/agents-starter/blob/main/README.md Example of defining a tool that requires user confirmation before execution, using Zod for parameter validation. ```typescript const searchDatabase = tool({ description: "Search the database for user records", parameters: z.object({ query: z.string(), limit: z.number().optional() }) // No execute function = requires confirmation }); ``` -------------------------------- ### Define Auto-Executing Tool Source: https://github.com/cloudflare/agents-starter/blob/main/README.md Example of defining a tool that executes automatically without requiring confirmation. ```typescript const getCurrentTime = tool({ description: "Get current server time", parameters: z.object({}), execute: async () => new Date().toISOString() }); ``` -------------------------------- ### Create Cloudflare Agent Project Source: https://github.com/cloudflare/agents-starter/blob/main/README.md Command to create a new project using the Cloudflare agents-starter template. ```bash npx create-cloudflare@latest --template cloudflare/agents-starter ``` -------------------------------- ### Tool Configuration Options Source: https://github.com/cloudflare/agents-starter/blob/main/README.md Explains the two methods for configuring tools: direct execution via `execute` function or confirmation via the `executions` object. ```typescript // Tools can be configured in two ways: // 1. With an `execute` function for automatic execution // 2. Without an `execute` function, requiring confirmation and using the `executions` object to handle the confirmed action. NOTE: The keys in `executions` should match `toolsRequiringConfirmation` in `app.tsx`. ``` -------------------------------- ### Deploy to Cloudflare Source: https://github.com/cloudflare/agents-starter/blob/main/README.md Deploys the chat agent application to Cloudflare Workers. ```bash npm run deploy ``` -------------------------------- ### Switch to Workers AI Provider in Server Code Source: https://github.com/cloudflare/agents-starter/blob/main/README.md Demonstrates how to replace the OpenAI SDK import and usage with the workers-ai-provider in server.ts for using Cloudflare's Workers AI. ```diff // server.ts // Change the imports - import { openai } from "@ai-sdk/openai"; + import { createWorkersAI } from 'workers-ai-provider'; // Create a Workers AI instance + const workersai = createWorkersAI({ binding: env.AI }); // Use it when calling the streamText method (or other methods) // from the ai-sdk - const model = openai("gpt-4o-2024-11-20"); + const model = workersai("@cf/deepseek-ai/deepseek-r1-distill-qwen-32b") ``` -------------------------------- ### Handle Tool Confirmations Source: https://github.com/cloudflare/agents-starter/blob/main/README.md Implementation for handling confirmed actions of tools that require user confirmation. ```typescript export const executions = { searchDatabase: async ({ query, limit }: { query: string; limit?: number; }) => { // Implementation for when the tool is confirmed const results = await db.search(query, limit); return results; } // Add more execution handlers for other tools that require confirmation }; ``` -------------------------------- ### Configure Wrangler for AI Binding Source: https://github.com/cloudflare/agents-starter/blob/main/README.md Adds an 'ai' binding to the wrangler.jsonc configuration file to enable AI services. ```jsonc // rest of file "ai": { "binding": "AI" } // rest of file ``` -------------------------------- ### Set OpenAI API Key Source: https://github.com/cloudflare/agents-starter/blob/main/README.md Environment variable configuration for the OpenAI API key. ```env OPENAI_API_KEY=your_openai_api_key ``` -------------------------------- ### AI Chat Agent Theme Toggling (JavaScript/CSS) Source: https://github.com/cloudflare/agents-starter/blob/main/index.html This snippet demonstrates how to toggle a 'dark' class on the document element based on local storage theme settings or system preference for dark mode. It also includes basic CSS for background styling. ```JavaScript document.documentElement.classList.toggle( "dark", localStorage.theme === "dark" || (!("theme" in localStorage) && window.matchMedia("(prefers-color-scheme: dark)").matches) ); ``` ```CSS html { background: var(--background); } @media (prefers-color-scheme: dark) { html { background: var(--background); } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.