Skip to content

secondsky/claude-skills / plugins/cloudflare-agents/skills/cloudflare-agents

cloudflare-agents

Verified

Build AI agents on Cloudflare Workers with MCP integration, tool use, and LLM providers.

templates/chat-agent-streaming.ts

View on GitHub
// AIChatAgent with streaming responses

import { AIChatAgent } from "agents/ai-chat-agent";
import { streamText } from "ai";
import { openai } from "@ai-sdk/openai";
import { anthropic } from "@ai-sdk/anthropic";

interface Env {
  OPENAI_API_KEY?: string;
  ANTHROPIC_API_KEY?: string;
}

export class StreamingChatAgent extends AIChatAgent<Env> {
  // Handle incoming chat messages and stream response
  async onChatMessage(onFinish) {
    // Choose model based on available API keys
    const model = this.env.ANTHROPIC_API_KEY
      ? anthropic('claude-sonnet-4-5')
      : openai('gpt-4o-mini');

    // Stream text generation
    const result = streamText({
      model,
      messages: this.messages,  // Built-in message history
      onFinish,                  // Called when response complete
      temperature: 0.7,
      maxTokens: 1000
    });

    // Return streaming response
    return result.toTextStreamResponse();
  }

  // Optional: Customize state updates
  async onStateUpdate(state, source) {
    console.log('Chat updated:');
    console.log('  Messages:', this.messages.length);
    console.log('  Last message:', this.messages[this.messages.length - 1]?.content);
  }

  // Optional: Custom message persistence
  async onStart() {
    // Load previous messages from SQL if needed
    const saved = await this.sql`SELECT * FROM messages ORDER BY timestamp`;

    if (saved.length > 0) {
      // Restore message history
      console.log('Restored', saved.length, 'messages');
    }
  }
}

export default StreamingChatAgent;
reverse-engineer-anythingmorluto/reaReverse engineer native, managed, Electron/JavaScript, packaged, firmware, and browser targets with REA, and build agent environments (AgentEnv bundles) of web applications from a live instance or a capture, and of Linux desktop apps with a task from a request. Use shipped-artifact or requested runtime evidence to explain features, compare versions, decompile code, or guide a reconstruction. Skip REA for ordinary source-repository architecture analysis.Stars: 472kUpdated Claude CodeCodexVerifiedInstall
diagnosing-superpowersobra/superpowersUse when a superpowers session went wrong and your human partner wants to know why — repeated work, ignored plans, stumbles, poor results, a skill that didn't fire, "it took too long", "why is it so expensive", "what is it doing" — or wants to build a bug report for the superpowers maintainers, for the current session or a past one identified by id or path, on any harness.Stars: 322kUpdated Claude CodeCodexVerifiedInstall
dispatching-parallel-agentsobra/superpowersUse when facing 2+ independent tasks that can be worked on without shared state or sequential dependenciesStars: 322kUpdated Claude CodeCodexVerifiedInstall
executing-plansobra/superpowersUse when executing an implementation plan in the current session as the implementer yourself — your human partner chose inline execution, or no subagent tool is availableStars: 322kUpdated Claude CodeCodexVerifiedInstall