KuralleAgent
Abstract base class for running Kuralle agents on Cloudflare.
Example
Section titled “Example”import { KuralleAgent } from '@kuralle-agents/cf-agent';import { openai } from '@ai-sdk/openai';
class MyAgent extends KuralleAgent<Env> { protected getAgents(): HarnessConfig['agents'] { return [{ id: 'assistant', name: 'Assistant', model: openai('gpt-4o', { apiKey: this.env.OPENAI_API_KEY }), instructions: 'You are a helpful assistant.', }]; }
protected getDefaultAgentId() { return 'assistant'; }}Extends
Section titled “Extends”AIChatAgent<Env,State>
Extended by
Section titled “Extended by”Type Parameters
Section titled “Type Parameters”| Type Parameter | Default type |
|---|---|
|
|
|
|
|
|
Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new KuralleAgent<Env, State>(ctx, env): KuralleAgent<Env, State>;Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
|
|
|
|
|
|
Returns
Section titled “Returns”KuralleAgent<Env, State>
Inherited from
Section titled “Inherited from”AIChatAgent<Env, State>.constructorMethods
Section titled “Methods”onChatMessage()
Section titled “onChatMessage()”onChatMessage(onFinish, options?): Promise<Response>;Called by CF when a chat message arrives.
CF has already:
- Received the WebSocket message from the client
- Parsed and validated it
- Persisted the user message to cf_ai_chat_agent_messages
- Populated this.messages with the full conversation history
We:
- Create a BridgeSessionStore (CF messages + orchestration state)
- Build and run Kuralle Runtime
- Return an SSE Response
CF then:
- Reads the SSE stream via _reply()
- Builds assistant message parts via applyChunkToParts()
- Persists the assistant message
- Broadcasts to all connected clients
- Handles stream resumability
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
|
|
|
|
|
|
Returns
Section titled “Returns”Promise<Response>
Overrides
Section titled “Overrides”AIChatAgent.onChatMessageonRequest()
Section titled “onRequest()”onRequest(request): Promise<Response>;HTTP endpoint handler. Adds Kuralle-specific endpoints on top of CF’s defaults.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
|
|
|
Returns
Section titled “Returns”Promise<Response>
Overrides
Section titled “Overrides”AIChatAgent.onRequestrunScheduledKuralleJob()
Section titled “runScheduledKuralleJob()”runScheduledKuralleJob(job): Promise<void>;DO-alarm callback for scheduled jobs. Wake jobs run an agent-initiated
turn and persist + broadcast the assistant reply through CF’s machinery
(same path as resumeWithSignal); other job kinds go to
onScheduledJob for subclasses to handle.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
|
|
|
Returns
Section titled “Returns”Promise<void>