/** * ChatKit Service * * Core service for handling ChatKit protocol requests. * Based on https://github.com/openai/chatkit-python/blob/main/chatkit/server.py */ import type { ChatKitStore, StoreContext } from './store'; import type { RunContextRetriever, RunExecutionBackend } from '../runs/RunExecutionBackend'; import type { AiConnectionsInvocationKeyIssuer } from '../ai-gateway/auth/AiConnectionsInvocationKeyIssuer'; /** * AI Provider interface for direct text generation in tests/dev harnesses. * * Product Chat requests go through RunStateCenter / Agent Runtime. This * provider is not a deployment compatibility path. */ export interface AiProvider { /** * Stream a response for the given messages * Returns an async iterator of text chunks */ streamResponse(messages: Array<{ role: 'system' | 'user' | 'assistant'; content: string; }>, options?: { model?: string; temperature?: number; maxTokens?: number; context?: unknown; }): AsyncIterable; } /** * ChatKit Service Options */ export interface ChatKitServiceOptions { store: ChatKitStore; aiProvider?: AiProvider; systemPrompt?: string; /** * Enable xpod Agent Runtime invocation (local-only feature). * When enabled, thread.metadata.runtime controls the runner/worktree. */ enableAgentRuntime?: boolean; allowDirectAiFallback?: boolean; runExecutionBackend?: RunExecutionBackend; contextRetriever?: RunContextRetriever; aiConnectionInvocationKeyIssuer?: Pick; requireAiConnectionsInvocationKeyIssuer?: boolean; } /** * Streaming result wrapper */ export interface StreamingResult { type: 'streaming'; stream(): AsyncIterable; } /** * Non-streaming result wrapper */ export interface NonStreamingResult { type: 'non_streaming'; json: string; } export type ChatKitResult = StreamingResult | NonStreamingResult; /** * ChatKit Service */ export declare class ChatKitService { private readonly logger; private readonly store; private readonly aiProvider?; private readonly systemPrompt; private readonly allowDirectAiFallback; private readonly runStateCenter; private readonly aiConnectionInvocationKeyIssuer?; private readonly requireAiConnectionsInvocationKeyIssuer; constructor(options: ChatKitServiceOptions); /** * Process a ChatKit request */ process(requestBody: string | Buffer, context: TContext): Promise; /** * Convert streaming events to SSE bytes */ private processStreamingAsBytes; /** * Process streaming requests */ private processStreaming; /** * Process non-streaming requests */ private processNonStreaming; /** * Handle threads.create - Create a new thread and optionally respond to initial message */ private handleThreadsCreate; /** * Handle threads.add_user_message - Add a user message and respond */ private handleThreadsAddUserMessage; /** * Handle threads.add_client_tool_output - Handle tool output (not fully implemented) */ private handleThreadsAddClientToolOutput; /** * Handle threads.retry_after_item - Retry generation after a specific item */ private handleThreadsRetryAfterItem; /** * Handle threads.custom_action - Handle custom widget actions (not fully implemented) */ private handleThreadsCustomAction; /** * Handle threads.get_by_id */ private handleThreadsGetById; /** * Handle threads.list */ private handleThreadsList; /** * Handle items.list */ private handleItemsList; /** * Handle items.feedback (acknowledge only for now) */ private handleItemsFeedback; /** * Handle attachments.create */ private handleAttachmentsCreate; /** * Handle attachments.delete */ private handleAttachmentsDelete; /** * Handle threads.update */ private handleThreadsUpdate; /** * Handle threads.delete */ private handleThreadsDelete; /** * Generate AI response for a user message * This is the main response generation logic */ private respond; private withInvocationAiConnections; /** * Build conversation history from thread items */ private buildConversationHistory; /** * Create a user message item */ private createUserMessage; /** * Generate a title for the thread based on the first exchange */ private generateThreadTitle; private projectRunStateEvent; private normalizeThreadMetadata; private resolveThreadWorkspace; private threadRefFromParams; private threadRefFromThread; }