import { ChatBrokerPlugin } from '../core/plugin.js'; import { Event, Hook, Log } from 'power-helper'; import { Translator, TranslatorParams } from '../core/translator.js'; import { ValidateCallback, ValidateCallbackOutputs } from '../utils/validate.js'; import { z } from 'zod'; import { CtoD } from '../ctod.js'; export type PolymorphicMessage = { type: 'text' | 'image'; content: string; }; export type Message = { role: 'system' | 'user' | 'assistant' | (string & Record); name?: string; content?: string; contents?: PolymorphicMessage[]; }; export type ChatBrokerHooks, O extends ValidateCallback, P extends ChatBrokerPlugin, PS extends Record>> = { /** * @zh 第一次聊天的時候觸發 * @en Triggered when chatting for the first time */ start: { id: string; data: ValidateCallbackOutputs; metadata: Map; plugins: { [K in keyof PS]: { send: (data: PS[K]['__receiveData']) => void; }; }; schema: { input?: S; output: O; }; messages: Message[]; setPreMessages: (messages: (Omit & { content?: string | string[]; } & { contents?: PolymorphicMessage[]; })[]) => void; changeMessages: (messages: Message[]) => void; changeOutputSchema: (output: O) => void; }; /** * @zh 發送聊天訊息給機器人前觸發 * @en Triggered before sending chat message to bot */ talkBefore: { id: string; data: ValidateCallbackOutputs; messages: Message[]; metadata: Map; lastUserMessage: string; }; /** * @zh 當聊天機器人回傳資料的時候觸發 * @en Triggered when the chatbot returns data */ talkAfter: { id: string; data: ValidateCallbackOutputs; response: any; messages: Message[]; parseText: string; metadata: Map; lastUserMessage: string; /** * @zh 宣告解析失敗 * @en Declare parsing failure */ parseFail: (error: any) => void; changeParseText: (text: string) => void; }; /** * @zh 當回傳資料符合規格時觸發 * @en Triggered when the returned data meets the specifications */ succeeded: { id: string; metadata: Map; output: ValidateCallbackOutputs; }; /** * @zh 當回傳資料不符合規格,或是解析錯誤時觸發 * @en Triggered when the returned data does not meet the specifications or parsing errors */ parseFailed: { id: string; error: any; retry: () => void; count: number; response: any; metadata: Map; parserFails: { name: string; error: any; }[]; messages: Message[]; lastUserMessage: string; changeMessages: (messages: Message[]) => void; }; /** * @zh 不論成功失敗,執行結束的時候會執行。 * @en It will be executed when the execution is completed, regardless of success or failure. */ done: { id: string; metadata: Map; }; }; export type RequestContext = { id: string; count: number; isRetry: boolean; metadata: Map; abortController: AbortController; onCancel: (cb: () => void) => void; schema: { input: any; output: any; }; }; export type Params, O extends ValidateCallback, C extends Record, P extends ChatBrokerPlugin, PS extends Record>> = Omit, 'parsers'> & { name?: string; plugins?: PS | (() => PS); request: (messages: Message[], context: RequestContext) => Promise; install?: (context: { log: Log; attach: Hook['attach']; attachAfter: Hook['attachAfter']; translator: Translator; }) => void; }; export declare class ChatBroker, O extends ValidateCallback, P extends ChatBrokerPlugin, PS extends Record>, C extends ChatBrokerHooks = ChatBrokerHooks> { protected __hookType: C; protected log: Log; protected hook: Hook; protected params: Params; protected plugins: PS; protected installed: boolean; protected translator: Translator; protected event: Event<{ cancel: { requestId: string; }; cancelAll: any; }>; constructor(params: Params); protected _install(): any; cancel(requestId?: string): Promise; cloneFrom(ctod: CtoD): ChatBroker; requestWithId>(data: T['__schemeType']): { id: string; request: Promise; }; /** * @zh 將請求發出至聊天機器人。 * @en Send request to chatbot. */ request>(data: T['__schemeType']): Promise; /** * @zh 取得預先請求的資訊,包含輸出規格與預設訊息,這生命週期只會執行到 start 階段為止,也不會觸發 plugin。 * @en Get pre-request information, including output specifications and default messages. This life cycle will only execute up to the start stage and will not trigger plugins. */ getPreRequestInfo>(data: T['__schemeType']): Promise<{ outputSchema: { [x: string]: any; }; outputJsonSchema: z.core.ZodStandardJSONSchemaPayload>; requestMessages: Message[]; }>; }