import { Suspense, useCallback, useState, useEffect, useRef } from "react"; import { useAgent } from "agents/react"; import { useAgentChat } from "@cloudflare/ai-chat/react"; import { isToolUIPart, isReasoningUIPart, getToolName } from "ai"; import type { UIMessage } from "ai"; import { Button, Badge, InputArea, Empty, Surface, Text } from "@cloudflare/kumo"; import { ConnectionIndicator, ModeToggle, PoweredByAgents, type ConnectionStatus } from "@cloudflare/agents-ui"; import { PaperPlaneRightIcon, StopIcon, TrashIcon, GearIcon, PlayIcon, CodeIcon, InfoIcon, RocketLaunchIcon, ArrowRightIcon, BrainIcon, WarningCircleIcon, CaretDownIcon, BrowserIcon, ArrowClockwiseIcon } from "@phosphor-icons/react"; import type { AppState } from "./server"; const STORAGE_KEY = "worker-bundler-playground-user-id"; function getUserId(): string { if (typeof window === "undefined") return "default"; const stored = localStorage.getItem(STORAGE_KEY); if (stored) return stored; const id = crypto.randomUUID(); localStorage.setItem(STORAGE_KEY, id); return id; } function getMessageText(message: UIMessage): string { return message.parts .filter((part) => part.type === "text") .map((part) => (part as { type: "text"; text: string }).text) .join(""); } // ─── Request Tester Panel ────────────────────────────────────────────────── function RequestTester({ onTest, disabled }: { onTest: ( method: string, path: string, body?: string ) => Promise<{ status: number; body: string }>; disabled: boolean; }) { const [method, setMethod] = useState("GET"); const [path, setPath] = useState("/"); const [body, setBody] = useState(""); const [response, setResponse] = useState<{ status: number; body: string; } | null>(null); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const handleTest = async () => { setLoading(true); setError(null); setResponse(null); try { const result = await onTest( method, path, method !== "GET" && method !== "HEAD" ? body : undefined ); setResponse(result); } catch (e) { setError(e instanceof Error ? e.message : String(e)); } finally { setLoading(false); } }; return (
Test App
setPath(e.target.value)} placeholder="/" className="flex-1 px-3 py-1.5 text-sm rounded-lg border border-kumo-line bg-kumo-base text-kumo-default placeholder:text-kumo-inactive font-mono focus:outline-none focus:ring-1 focus:ring-kumo-accent" />
{method !== "GET" && method !== "HEAD" && (