Agents, Workflows - Agents SDK v0.3.7: Workflows integration, synchronous state, and scheduleEvery()
5mo ago
Source
CloudflareAgents, Workflows - Agents SDK v0.3.7: Workflows integration, synchronous state, and scheduleEvery()cloudflare.comThe latest release of the Agents SDK brings first-class support for Cloudflare Workflows , synchronous state management, and new scheduling capabilities. Cloudflare Workflows integration Agents excel at real-time communication and state management. Workflows excel at durable execution. Together, they enable powerful patterns where Agents handle WebSocket connections while Workflows handle long-running tasks, retries, and human-in-the-loop flows. Use the new AgentWorkflow class to define workflows with typed access to your Agent: JavaScript import { AgentWorkflow } from "agents/workflows" ; export class ProcessingWorkflow extends AgentWorkflow { async run ( event , step ) { // Call Agent methods via RPC await this . agent . updateStatus ( event . payload . taskId , "processing" ) ; // Non-durable: progress reporting to clients await this . reportProgress ( { step : "process" , percent : 0.5 } ) ; this . broadcastToClients ( { type : "update" , taskId : event . payload . taskId } ) ; // Durable via step: idempotent, won't repeat on retry await step . mergeAgentState ( { taskProgress : 0.5 } ) ; const result = await step . do ( "process" , async () => { return processData ( event . payload . data ) ; } ) ; await step . reportComplete ( result ) ; return result ; } } TypeScript import { AgentWorkflow } from "agents/workflows" ; import type { AgentWorkflowEvent , AgentWorkflowStep } from "agents/workflows" ; export class ProcessingWorkflow extends AgentWorkflow < MyAgent , TaskParams > { async run ( event : AgentWorkflowEvent < TaskParams >, step : AgentWorkflowStep ) { // Call Agent methods via RPC await this . agent . updateStatus ( event . payload . taskId , "processing" ) ; // Non-durable: progress reporting to clients await this . reportProgress ( { step : "process" , percent : 0.5 } ) ; this . broadcastToClients ( { type : "update" , taskId : event . payload . taskId } ) ; // Durable via step: idempotent, won't repeat on retry await step . mergeAgentState ( { taskProgress : 0.5 } ) ; const result = await step . do ( "process" , async () => { return processData ( event . payload . data ) ; } ) ; await step . reportComplete ( result ) ; return result ; } } Start workflows from your Agent with runWorkflow() and handle lifecycle events: JavaScript export class MyAgent extends Agent { async startTask ( taskId , data ) { const instanceId = await this . runWorkflow ( "PROCESSING_WORKFLOW" , { taskId , data , } ) ; return { instanceId }; } async onWorkflowProgress ( workflowName , instanceId , progress ) { this . broadcast ( JSON . stringify ( { type : "progress" , progress } )) ; } async onWorkflowComplete ( workflowName , instanceId , result ) { console . log ( `Workflow ${ instanceId } completed` ) ; } async onWorkflowError ( workflowName , instanceId , error ) { console . error ( `Workflow ${ instanceId } failed:` , error ) ; } } TypeScript export class MyAgent extends Agent { async startTask ( taskId : string , data : string ) { const instanceId = await this . runWorkflow ( "PROCESSING_WORKFLOW" , { taskId , data , } ) ; return { instanceId }; } async onWorkflowProgress ( workflowName : string , instanceId : string , progress : unknown , ) { this . broadcast ( JSON . stringify ( { type : "progress" , progress } )) ; } async onWorkflowComplete ( workflowName : string , instanceId : string , result ?: unknown , ) { console . log ( `Workflow ${ instanceId } completed` ) ; } async onWorkflowError ( workflowName : string , instanceId : string , error : unknown , ) { console . error ( `Workflow ${ instanceId } failed:` , error ) ; } } Key workflow methods on your Agent: runWorkflow(workflowName, params, options?) — Start a workflow with optional metadata getWorkflow(workflowId) / getWorkflows(criteria?) — Query workflows with cursor-based pagination approveWorkflow(workflowId) / rejectWorkflow(workflowId) — Human-in-the-loop approval flows pauseWorkflow() , resumeWorkflow() , terminateWorkflow() — Workflow control Synchronous setState() State updates are now synchronous with a new validateStateChange() validation hook: JavaScript export class MyAgent extends Agent { validateStateChange ( oldState , newState ) { // Return false to reject the change if ( newState . count < 0 ) return false ; // Return modified state to transform return { ... newState , lastUpdated : Date . now () }; } } TypeScript export class MyAgent extends Agent < Env , State > { validateStateChange ( oldState : State , newState : State ) : State
You might also wanna read
DataFlow-Agent: AI Framework for Automated Data Workflow Design and Orchestration
DataFlow-Agent is an AI-powered framework for automating data workflow design within the DataFlow ecosystem. It provides a modular, state-dr
Workflow SDK: Adding Durability and Reliability to TypeScript Functions
The article introduces Workflow SDK, a tool that adds durability, reliability, and observability to asynchronous JavaScript/TypeScript funct
Lightweight Workflow for Multi-Agent Coding Without API Costs
The article describes a lightweight workflow for enabling coding agents to interact with each other without using APIs, SDKs, or additional
juanpabloaj.com·2mo ago
AgentState v1.0.0: Cloud-Native State Management Platform for AI Agents
AgentState v1.0.0 is a cloud-native state management system designed specifically for AI agents, providing durable state persistence with fe
Realtime agent demo
vimeo.com·11mo ago
How to Integrate Existing AI Agents into Microsoft Teams Using TypeScript SDK
This article provides a technical guide for developers on how to integrate existing AI agents or bots into Microsoft Teams without rewriting
microsoft.github.io·2mo ago
Comments
Sign in to join the conversation.
No comments yet. Be the first.