ag-ui-cloudflare
Version:
Native AG-UI protocol implementation for Cloudflare Workers AI - Enable CopilotKit with edge AI at 93% lower cost
198 lines (188 loc) • 7.91 kB
text/typescript
import { z } from 'zod';
declare enum EventType {
TEXT_MESSAGE_START = "TEXT_MESSAGE_START",
TEXT_MESSAGE_CONTENT = "TEXT_MESSAGE_CONTENT",
TEXT_MESSAGE_END = "TEXT_MESSAGE_END",
TOOL_CALL_START = "TOOL_CALL_START",
TOOL_CALL_ARGS = "TOOL_CALL_ARGS",
TOOL_CALL_END = "TOOL_CALL_END",
TOOL_CALL_RESULT = "TOOL_CALL_RESULT",
RUN_STARTED = "RUN_STARTED",
RUN_FINISHED = "RUN_FINISHED",
RUN_ERROR = "RUN_ERROR",
STEP_STARTED = "STEP_STARTED",
STEP_FINISHED = "STEP_FINISHED",
STATE_SYNC = "STATE_SYNC",
METADATA = "METADATA",
PROGRESS = "PROGRESS",
CUSTOM = "CUSTOM"
}
interface BaseEvent {
type: EventType;
timestamp?: number;
rawEvent?: any;
}
interface AGUIEvent extends BaseEvent {
runId?: string;
data?: any;
metadata?: Record<string, any>;
}
declare class CloudflareAGUIEvents {
static runStarted(runId: string, metadata?: Record<string, any>): AGUIEvent;
static runFinished(runId: string, metadata?: Record<string, any>): AGUIEvent;
static textMessageStart(runId: string, role: string): AGUIEvent;
static textMessageContent(runId: string, delta: string): AGUIEvent;
static textMessageEnd(runId: string): AGUIEvent;
static toolCallStart(runId: string, toolCallId: string, toolName: string): AGUIEvent;
static toolCallArgs(runId: string, toolCallId: string, args: string): AGUIEvent;
static toolCallEnd(runId: string, toolCallId: string): AGUIEvent;
static toolCallResult(runId: string, toolCallId: string, result: string): AGUIEvent;
static error(runId: string, error: Error): AGUIEvent;
static stepStarted(runId: string, stepName: string): AGUIEvent;
static stepFinished(runId: string, stepName: string): AGUIEvent;
static stateSync(runId: string, state: Record<string, any>): AGUIEvent;
static metadata(runId: string, metadata: Record<string, any>): AGUIEvent;
static progress(runId: string, progress: number, message?: string): AGUIEvent;
static custom(runId: string, name: string, value: any): AGUIEvent;
}
declare const CloudflareModelSchema: z.ZodEnum<["@cf/meta/llama-3.1-8b-instruct", "@cf/meta/llama-3.1-70b-instruct", "@cf/meta/llama-3.3-70b-instruct", "@cf/meta/llama-2-7b-chat-int8", "@cf/mistral/mistral-7b-instruct-v0.2", "@cf/google/gemma-7b-it", "@cf/qwen/qwen1.5-14b-chat-awq", "@cf/microsoft/phi-2", "@cf/deepseek-ai/deepseek-math-7b-instruct", "@cf/thebloke/deepseek-coder-6.7b-instruct-awq"]>;
type CloudflareModel = z.infer<typeof CloudflareModelSchema>;
interface CloudflareAIConfig {
accountId: string;
apiToken: string;
model?: CloudflareModel;
baseURL?: string;
gatewayId?: string;
}
interface CloudflareMessage {
role: 'system' | 'user' | 'assistant' | 'tool' | 'function';
content: string;
tool_calls?: ToolCall[];
tool_call_id?: string;
name?: string;
}
interface ToolCall {
id: string;
type: 'function';
function: {
name: string;
arguments: string;
};
}
interface CloudflareStreamChunk {
response?: string;
tool_calls?: ToolCall[];
done?: boolean;
usage?: {
prompt_tokens: number;
completion_tokens: number;
total_tokens: number;
};
}
interface CloudflareCompletionOptions {
messages: CloudflareMessage[];
model?: CloudflareModel;
temperature?: number;
max_tokens?: number;
top_p?: number;
frequency_penalty?: number;
presence_penalty?: number;
stream?: boolean;
tools?: Tool[];
tool_choice?: 'auto' | 'none' | {
type: 'function';
function: {
name: string;
};
};
}
interface Tool {
type: 'function';
function: {
name: string;
description?: string;
parameters?: Record<string, any>;
};
}
interface ModelCapabilities {
streaming: boolean;
functionCalling: boolean;
maxTokens: number;
contextWindow: number;
}
interface CloudflareAGUIAdapterOptions extends CloudflareAIConfig {
systemPrompt?: string;
tools?: Tool[];
streamingEnabled?: boolean;
}
interface AGUIProtocol {
execute(messages: any[], context?: Record<string, any>): AsyncGenerator<AGUIEvent>;
}
type StreamableResult<T> = AsyncGenerator<T>;
declare class CloudflareAGUIAdapter implements AGUIProtocol {
private client;
private options;
private runCounter;
constructor(options: CloudflareAGUIAdapterOptions);
execute(messages: CloudflareMessage[], context?: Record<string, any>): AsyncGenerator<AGUIEvent>;
private handleStreaming;
private handleNonStreaming;
executeWithTools(messages: CloudflareMessage[], tools: Tool[], context?: Record<string, any>): AsyncGenerator<AGUIEvent>;
progressiveGeneration(prompt: string, stages: Array<{
name: string;
instruction: string;
}>): AsyncGenerator<AGUIEvent>;
setModel(model: string): void;
getCapabilities(): any;
listAvailableModels(): Promise<string[]>;
private generateRunId;
}
declare class CloudflareAIClient {
private config;
private baseURL;
private headers;
constructor(config: CloudflareAIConfig);
complete(options: CloudflareCompletionOptions): Promise<CloudflareMessage>;
streamComplete(options: CloudflareCompletionOptions): AsyncGenerator<CloudflareStreamChunk>;
private makeRequest;
listModels(): Promise<string[]>;
getModelCapabilities(model: string): any;
}
interface ProviderConfig extends Omit<CloudflareAGUIAdapterOptions, 'model'> {
model?: CloudflareModel;
}
declare class CloudflareProviders {
static llama3_8b(config: ProviderConfig): CloudflareAGUIAdapter;
static llama3_70b(config: ProviderConfig): CloudflareAGUIAdapter;
static llama3_3_70b(config: ProviderConfig): CloudflareAGUIAdapter;
static mistral7b(config: ProviderConfig): CloudflareAGUIAdapter;
static gemma7b(config: ProviderConfig): CloudflareAGUIAdapter;
static qwen14b(config: ProviderConfig): CloudflareAGUIAdapter;
static phi2(config: ProviderConfig): CloudflareAGUIAdapter;
static deepseekMath(config: ProviderConfig): CloudflareAGUIAdapter;
static deepseekCoder(config: ProviderConfig): CloudflareAGUIAdapter;
static auto(config: ProviderConfig): CloudflareAGUIAdapter;
static createWithGateway(accountId: string, apiToken: string, gatewayId: string, model?: CloudflareModel): CloudflareAGUIAdapter;
}
declare class CloudflareStreamParser {
private parser;
private buffer;
constructor();
private onParse;
parseStream(stream: ReadableStream<Uint8Array>): AsyncGenerator<CloudflareStreamChunk>;
parseSSE(text: string): CloudflareStreamChunk[];
}
declare const CLOUDFLARE_MODELS: {
readonly LLAMA_3_1_8B: "@cf/meta/llama-3.1-8b-instruct";
readonly LLAMA_3_1_70B: "@cf/meta/llama-3.1-70b-instruct";
readonly LLAMA_3_3_70B: "@cf/meta/llama-3.3-70b-instruct";
readonly LLAMA_2_7B: "@cf/meta/llama-2-7b-chat-int8";
readonly MISTRAL_7B: "@cf/mistral/mistral-7b-instruct-v0.2";
readonly GEMMA_7B: "@cf/google/gemma-7b-it";
readonly QWEN_14B: "@cf/qwen/qwen1.5-14b-chat-awq";
readonly PHI_2: "@cf/microsoft/phi-2";
readonly DEEPSEEK_MATH_7B: "@cf/deepseek-ai/deepseek-math-7b-instruct";
readonly DEEPSEEK_CODER_6B: "@cf/thebloke/deepseek-coder-6.7b-instruct-awq";
};
declare function createCloudflareAdapter(config: CloudflareAIConfig): CloudflareAGUIAdapter;
export { type AGUIEvent, type AGUIProtocol, type BaseEvent, CLOUDFLARE_MODELS, CloudflareAGUIAdapter, type CloudflareAGUIAdapterOptions, CloudflareAGUIEvents, CloudflareAIClient, type CloudflareAIConfig, type CloudflareCompletionOptions, type CloudflareMessage, type CloudflareModel, CloudflareProviders, type CloudflareStreamChunk, CloudflareStreamParser, EventType, type ModelCapabilities, type ProviderConfig, type StreamableResult, type Tool, type ToolCall, createCloudflareAdapter };