1- /**
2- * Welcome to Cloudflare Workers! This is your first worker.
3- *
4- * - Run `npm run dev` in your terminal to start a development server
5- * - Open a browser tab at http://localhost:8787/ to see your worker in action
6- * - Run `npm run deploy` to publish your worker
7- *
8- * Learn more at https://developers.cloudflare.com/workers/
9- */
10-
111import { Hono } from 'hono' ;
12- import { AgentEnv , applyPermissionlessAgentSessionRouter } from '@nullshot/agent' ;
13- // Import the ToolsService directly
2+ import { applyPermissionlessAgentSessionRouter } from '@nullshot/agent' ;
143import { ToolboxService } from '@nullshot/agent/services' ;
15- import { LanguageModel } from 'ai' ;
4+ import { LanguageModel , stepCountIs , type Provider } from 'ai' ;
165import { createAnthropic } from '@ai-sdk/anthropic' ;
176import { createOpenAI } from '@ai-sdk/openai' ;
187import { AiSdkAgent , AIUISDKMessage } from '@nullshot/agent/aisdk' ;
198import mcpConfig from '../mcp.json' ;
20- // Define AI provider type
21- type AIProvider = 'anthropic' | 'openai' | 'deepseek' ;
22-
23- // Define a type that extends both the autogenerated Env and AgentEnv
24- type EnvWithAgent = Env & AgentEnv ;
25-
26- // Function to validate if a value is a valid AIProvider
27- function isValidAIProvider ( value : unknown ) : value is AIProvider {
28- return value === 'anthropic' || value === 'openai' || value === 'deepseek' ;
29- }
309
3110// Use type assertion to make Hono app compatible with AgentRouterBuilder
32- const app = new Hono < { Bindings : EnvWithAgent } > ( ) ;
11+ const app = new Hono < { Bindings : Env } > ( ) ;
3312applyPermissionlessAgentSessionRouter ( app ) ;
3413
35- export class SimplePromptAgent extends AiSdkAgent < EnvWithAgent > {
36- constructor ( state : DurableObjectState , env : EnvWithAgent ) {
37- // Validate AI_PROVIDER before using it
38- if ( ! isValidAIProvider ( env . AI_PROVIDER ) ) {
39- throw new Error ( `Invalid AI provider: ${ env . AI_PROVIDER } . Expected 'anthropic', 'openai', or 'deepseek'.` ) ;
40- }
41-
14+ export class SimplePromptAgent extends AiSdkAgent < Env > {
15+ constructor ( state : DurableObjectState , env : Env ) {
16+ let provider : Provider ;
4217 let model : LanguageModel ;
4318 // This is just an example, ideally you only want ot inlcude models that you plan to use for your agent itself versus multiple models
4419 switch ( env . AI_PROVIDER ) {
4520 case 'anthropic' :
46- const anthropic = createAnthropic ( {
21+ provider = createAnthropic ( {
4722 apiKey : env . ANTHROPIC_API_KEY ,
4823 } ) ;
49- model = anthropic ( 'claude-3-haiku-20240307' ) ;
24+ model = provider . languageModel ( 'claude-3-haiku-20240307' ) ;
5025 break ;
5126 case 'openai' :
52- const openai = createOpenAI ( {
27+ provider = createOpenAI ( {
5328 apiKey : env . OPEN_AI_API_KEY ,
5429 } ) ;
55- model = openai ( 'gpt-3.5-turbo' ) ;
30+ model = provider . languageModel ( 'gpt-3.5-turbo' ) ;
5631 break ;
5732 case 'deepseek' :
58- const deepseek = createOpenAI ( {
33+ provider = createOpenAI ( {
5934 apiKey : env . DEEPSEEK_API_KEY ,
6035 baseURL : 'https://api.deepseek.com' ,
6136 } ) ;
62- model = deepseek ( 'deepseek-chat' ) ;
37+ model = provider . languageModel ( 'deepseek-chat' ) ;
6338 break ;
6439 default :
6540 // This should never happen due to validation above, but TypeScript requires this
@@ -70,24 +45,25 @@ export class SimplePromptAgent extends AiSdkAgent<EnvWithAgent> {
7045 }
7146
7247 async processMessage ( sessionId : string , messages : AIUISDKMessage ) : Promise < Response > {
73- const result = await this . streamText ( sessionId , {
74- model : this . model ,
48+ // Use the protected streamTextWithMessages method - model is handled automatically by the agent
49+ const result = await this . streamTextWithMessages ( sessionId , messages . messages , {
7550 system : 'You will use tools to help manage and mark off tasks on a todo list.' ,
76- messages : messages . messages ,
7751 maxSteps : 10 ,
52+ stopWhen : stepCountIs ( 10 ) ,
53+ // Enable MCP tools from imported mcp.json
7854 experimental_toolCallStreaming : true ,
79- onError : ( error ) => {
55+ onError : ( error : unknown ) => {
8056 console . error ( 'Error processing message' , error ) ;
8157 } ,
8258 } ) ;
8359
84- return result . toDataStreamResponse ( ) ;
60+ return result . toTextStreamResponse ( ) ;
8561 }
8662}
8763
8864// Export the worker handler
8965export default {
90- async fetch ( request : Request , env : EnvWithAgent , ctx : ExecutionContext ) : Promise < Response > {
66+ async fetch ( request : Request , env : Env , ctx : ExecutionContext ) : Promise < Response > {
9167 // Bootstrap the agent worker with the namespace
9268 return app . fetch ( request , env , ctx ) ;
9369 } ,
0 commit comments