interface SendMessageParams { content: string; sessionId?: string; metadata?: Record; /** Untrusted client context delivered separately from visible message text. */ context?: Record; /** Stable retry key. Browser runtimes generate one when omitted. */ idempotencyKey?: string; } interface SendMessageResult { message: Message; executionId?: string; } interface ExecutionStatusResult { status: ExecutionStatus; output?: Record; error?: string; steps?: AgentStep[]; } interface ChatProvider { /** Send a message and get a response (non-streaming). */ sendMessage(params: SendMessageParams): Promise; /** Send a message with streaming response. */ sendMessageStream(params: SendMessageParams, signal?: AbortSignal): AsyncGenerator; /** Create a new chat session. */ createSession?(agentId: string, title?: string): Promise; /** List messages in a session. */ listMessages?(sessionId: string, page?: number): Promise<{ messages: Message[]; total: number; }>; /** Get execution status for agent tracking. */ getExecutionStatus?(executionId: string): Promise; /** List pending approval requests. */ listApprovals?(filters?: { status?: string; }): Promise; /** Decide on an approval request. */ decideApproval?(id: string, decision: ApprovalDecision, reason?: string): Promise; /** Cleanup resources. */ disconnect?(): void; } type MessageRole = "user" | "assistant" | "system"; type MessageStatus = "pending" | "streaming" | "complete" | "error"; interface Message { id: string; role: MessageRole; content: string; status: MessageStatus; createdAt: Date; metadata?: Record; executionId?: string; toolCalls?: ToolCall[]; } interface ToolCall { id: string; name: string; arguments: Record; result?: string; status: "pending" | "running" | "complete" | "error"; } type StreamEventType = "content" | "tool_call" | "tool_start" | "tool_end" | "thinking" | "ui" | "execution" | "status" | "error" | "done"; interface StreamEvent { type: StreamEventType; content?: string; error?: string; executionId?: string; status?: ExecutionStatus; toolCall?: ToolCall; /** tool_start / tool_end payload */ toolCallId?: string; toolName?: string; /** tool_end result summary (short string) */ toolSummary?: string; /** thinking payload — intermediate reasoning text between tool rounds */ thinking?: string; /** done payload — final output produced by the execution */ output?: unknown; /** ui payload — safe, declarative render instructions emitted by PromptRails. */ ui?: Record; } type ExecutionStatus = "pending" | "running" | "completed" | "failed" | "cancelled" | "waiting_approval" | "cancel_requested"; interface AgentStep { id: string; name: string; status: ExecutionStatus; input?: Record; output?: Record; durationMs?: number; error?: string; startedAt?: Date; completedAt?: Date; } type ApprovalDecision = "approved" | "rejected"; /** * API v2 approvals are execution-scoped: a run parked at an approval-gated * tool call surfaces with `status: "pending"` (the underlying execution is * `waiting_approval`) and, when the backend enforces a deadline, * `approvalExpiresAt`. `id` and `executionId` are the same execution id — * approving/denying resumes that execution. Who may decide is governed by the * agent's approval policy on the server, not carried on the request. */ interface ApprovalRequest { id: string; executionId: string; agentId?: string; payload: Record; status: "pending" | "approved" | "rejected"; approvalExpiresAt?: Date; createdAt: Date; } interface ChatSession { id: string; agentId: string; title?: string; createdAt: Date; } interface UseChatOptions { provider: ChatProvider; initialMessages?: Message[]; sessionId?: string; onError?: (error: Error) => void; onFinish?: (message: Message) => void; maxRetries?: number; /** * When true (default) the hook uses provider.sendMessageStream and * incrementally updates the assistant message — content deltas append to * `message.content`, tool calls are aggregated into `message.toolCalls`. * Set to false to fall back to the non-streaming provider.sendMessage. */ stream?: boolean; } interface UseStreamingOptions { onChunk?: (chunk: string) => void; onComplete?: (fullContent: string) => void; onError?: (error: Error) => void; /** Called when a tool starts executing (status: "running"). */ onToolStart?: (toolCall: ToolCall) => void; /** Called when a tool finishes (status: "complete"). The toolCall carries the summary in `result`. */ onToolEnd?: (toolCall: ToolCall) => void; /** Called with intermediate reasoning text between tool rounds. */ onThinking?: (content: string) => void; } interface UseAgentOptions { provider: ChatProvider; onStepUpdate?: (step: AgentStep) => void; onComplete?: (steps: AgentStep[]) => void; onError?: (error: Error) => void; pollIntervalMs?: number; } interface UseApprovalOptions { provider: ChatProvider; onApprovalRequired?: (request: ApprovalRequest) => void; onApprovalDecided?: (request: ApprovalRequest) => void; } interface ChatWindowProps { provider: ChatProvider; sessionId?: string; initialMessages?: Message[]; title?: string; placeholder?: string; className?: string; showAgentSteps?: boolean; showApprovals?: boolean; /** * Render tool call cards in assistant messages. * Stream events still flow through the data layer regardless — this only * controls whether the UI shows them. Defaults to true. */ showToolCalls?: boolean; onError?: (error: Error) => void; } interface MessageBubbleProps { message: Message; className?: string; renderMarkdown?: boolean; /** Render tool call cards on this message. Defaults to true. */ showToolCalls?: boolean; } interface TypingIndicatorProps { className?: string; text?: string; } interface AgentStepsProps { steps: AgentStep[]; className?: string; collapsible?: boolean; } interface ApprovalCardProps { request: ApprovalRequest; onApprove: (id: string, reason?: string) => void; onReject: (id: string, reason?: string) => void; className?: string; disabled?: boolean; } interface MessageInputProps { value: string; onChange: (value: string) => void; onSubmit: () => void; placeholder?: string; disabled?: boolean; className?: string; } interface WidgetConfig { provider: { type: "promptrails" | "openai" | "custom"; apiKey?: string; baseUrl?: string; agentId?: string; model?: string; [key: string]: unknown; }; position?: "bottom-right" | "bottom-left"; title?: string; placeholder?: string; primaryColor?: string; workspaceId?: string; /** Persist only the resumable chat reference. Defaults to true. */ persistSession?: boolean; /** Local inactivity lifetime in seconds. Defaults to 24 hours. */ sessionMaxAge?: number; /** Persist an anonymous visitor ID for trace grouping. Defaults to false. */ visitorTracking?: boolean; /** Maximum lifetime of the anonymous visitor ID in seconds. Capped at 90 days. */ visitorMaxAge?: number; /** Optional stylesheet loaded inside the widget's ShadowRoot. */ stylesheetUrl?: string; newSessionLabel?: string; feedbackLabel?: string; errorMessage?: string; bubbleIcon?: string; greeting?: string; width?: number; height?: number; zIndex?: number; /** CSP nonce copied to the Shadow DOM style element. */ styleNonce?: string; /** BCP-47 locale used by built-in labels. Defaults to the document language. */ locale?: string; /** Structured context added to each browser message without changing visible text. */ contextProvider?: () => Record | Promise>; /** Host-owned widget lifecycle and analytics hook. */ onEvent?: (event: WidgetEvent) => void; labels?: Partial; } interface WidgetLabels { open: string; close: string; send: string; newSession: string; helpful: string; notHelpful: string; empty: string; offline: string; } interface WidgetEvent { type: "open" | "close" | "message.sent" | "message.completed" | "message.failed" | "session.new" | "feedback"; detail?: Record; } interface WidgetController { open(): void; close(): void; toggle(): void; send(content: string): Promise; newSession(): Promise; updateContext(context: Record): void; destroy(): void; } export type { AgentStep as A, ChatProvider as C, ExecutionStatus as E, Message as M, SendMessageParams as S, ToolCall as T, UseAgentOptions as U, WidgetConfig as W, AgentStepsProps as a, ApprovalCardProps as b, ApprovalDecision as c, ApprovalRequest as d, ChatSession as e, ChatWindowProps as f, ExecutionStatusResult as g, MessageBubbleProps as h, MessageInputProps as i, MessageRole as j, MessageStatus as k, SendMessageResult as l, StreamEvent as m, StreamEventType as n, TypingIndicatorProps as o, UseApprovalOptions as p, UseChatOptions as q, UseStreamingOptions as r, WidgetController as s, WidgetEvent as t, WidgetLabels as u };