File
Blob: src/worker/lib/ai/types.ts
| 1 | import type { AiErrorCode, AiUsage } from "@/shared/ai"; |
| 2 | |
| 3 | export type AiChatRole = "system" | "user" | "assistant"; |
| 4 | |
| 5 | export interface AiChatMessage { |
| 6 | role: AiChatRole; |
| 7 | content: string; |
| 8 | } |
| 9 | |
| 10 | export interface AiChatOptions { |
| 11 | maxTokens?: number; |
| 12 | temperature?: number; |
| 13 | sessionKey?: string; |
| 14 | signal?: AbortSignal; |
| 15 | } |
| 16 | |
| 17 | export interface AiSummarizeOptions { |
| 18 | maxTokens?: number; |
| 19 | } |
| 20 | |
| 21 | export interface AiSummarizeResult { |
| 22 | summary: string; |
| 23 | usage?: AiUsage; |
| 24 | } |
| 25 | |
| 26 | export type AiFrame = |
| 27 | | { type: "chunk"; text: string } |
| 28 | | { type: "usage"; usage: AiUsage } |
| 29 | | { type: "error"; message: string; code: AiErrorCode }; |
| 30 | |
| 31 | export interface AiClient { |
| 32 | chat(messages: AiChatMessage[], opts?: AiChatOptions): Promise<AsyncIterable<AiFrame>>; |
| 33 | summarize(text: string, opts?: AiSummarizeOptions): Promise<AiSummarizeResult>; |
| 34 | } |
| 35 | |
| 36 | export type AiMode = "workers-ai" | "openai-compat" | "mock"; |
| 37 | |
| 38 | export class AiMisconfiguredError extends Error { |
| 39 | readonly code: AiErrorCode = "ai_misconfigured"; |
| 40 | constructor(message: string) { |
| 41 | super(message); |
| 42 | this.name = "AiMisconfiguredError"; |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | export class AiBackendError extends Error { |
| 47 | readonly code: AiErrorCode; |
| 48 | constructor(message: string, code: AiErrorCode = "ai_backend_failed") { |
| 49 | super(message); |
| 50 | this.name = "AiBackendError"; |
| 51 | this.code = code; |
| 52 | } |
| 53 | } |