import { AsyncLocalStorage } from "async_hooks";
import EventEmitter from "node:events";
import { Server } from "node:http";
import { SafetySetting } from "@google/genai";
import { AxiosError, AxiosInstance, AxiosRequestConfig, InternalAxiosRequestConfig } from "axios";
import { Application } from "express";
import { Job } from "@karinjs/node-schedule";

//#region src/types/models.d.ts
type Feature = 'chat' | 'visual' | 'tool' | 'embedding';
/** 模型配置：名称 + 功能特性 */
interface ModelConfig {
  name: string;
  features: Feature[];
}
/**
 * other roles like developer or function should be handled by different implementations
 */
type Role = 'system' | 'user' | 'assistant' | 'tool' | 'developer';
interface MessageContent {
  type: 'text' | 'image' | 'audio' | 'video' | 'tool' | 'reasoning';
  thoughtSignature?: string;
}
interface TextContent extends MessageContent {
  type: 'text';
  /**
   * Content of text
   */
  text: string;
}
interface ReasoningContent extends MessageContent {
  type: 'reasoning';
  /**
   * thinking text
   */
  text: string;
}
interface ImageContent extends MessageContent {
  type: 'image';
  /**
   * Either a URL of the image or the base64 encoded image data
   */
  image: string;
  mimeType?: string;
}
interface AudioContent extends MessageContent {
  type: 'audio';
  /**
   * Base64 encoded audio data
   */
  data: string;
  format: 'mp3' | 'wav';
}
type History = {
  id: string;
  parentId: string | null;
};
interface IMessage {
  role: Role;
  content: MessageContent[];
  toolCalls?: ToolCall[];
}
/**
 * 模型返回的工具调用，role是assistant
 */
interface ToolCall {
  id: string;
  type: 'function';
  function: FunctionCall;
  thoughtSignature?: string;
}
type ArgumentValue = string | number | boolean | ArgumentValue[];
type FunctionCall = {
  name: string;
  /**
   * raw可能是JSON字符串，需要反序列化一次
   */
  arguments: Record<string, ArgumentValue | Record<string, ArgumentValue>>;
};
/**
 * 用户发送的消息，可能有文本和多模态消息
 */
interface UserMessage extends IMessage {
  role: 'user';
  content: Array<TextContent | ImageContent | AudioContent>;
}
interface SystemMessage extends IMessage {
  role: 'system';
  content: TextContent[];
}
interface DeveloperMessage extends IMessage {
  role: 'developer';
  content: TextContent[];
}
/**
 * 模型返回的消息，包括文本以及可能的工具调用
 */
interface AssistantMessage extends IMessage {
  role: 'assistant';
  content: Array<MessageContent>;
  toolCalls?: ToolCall[];
}
interface ReasoningPart {
  reasoning_content?: string;
  reasoning?: string;
  thinking_content?: string;
  thinking?: string;
  think?: string;
}
/**
 * 客户端执行工具调用的函数结果
 */
interface ToolCallResultMessage extends IMessage {
  role: 'tool';
  content: ToolCallResult[];
}
interface ToolCallResult extends MessageContent {
  type: 'tool';
  tool_call_id?: string;
  content: string;
  name?: string;
}
type HistoryMessage = History & IMessage;
interface ModelResponse {
  id?: string;
  model?: string;
  contents: MessageContent[];
  usage?: ModelUsage;
}
interface ModelUsage {
  promptTokens?: number;
  completionTokens?: number;
  totalTokens?: number;
  cachedTokens?: number;
  reasoningTokens?: number;
}
interface ModelResponseChunk {
  id?: string;
  model?: string;
  delta: MessageContent[];
  toolCall?: ToolCall[];
}
interface EmbeddingResult {
  embeddings: Array<number>[];
}
//#endregion
//#region src/types/cloud.d.ts
interface CloudAPIResponse<T> {
  code: number;
  data: T;
  msg: string;
}
interface PaginationResult<T> {
  items: T[];
  pagination: {
    currentPage: number;
    pageSize: number;
    totalItems: number;
    totalPages: number;
    hasNextPage: boolean;
    hasPreviousPage: boolean;
  };
  type: string;
}
interface CloudSharingService<T extends AbstractShareable<unknown>> {
  setUser(user: User): void;
  getUser(): User | null;
  authenticate(apiKey: string): Promise<User | null>;
  upload(model: Serializable): Promise<T | null>;
  download(shareId: string): Promise<T | null>;
  initializeTransfer(model: Serializable): Promise<string | null>;
  list(filter: Filter, query: string, searchOption: SearchOption): Promise<PaginationResult<T>>;
  delete(shareId: string): Promise<boolean>;
}
type FilterValue = string | number | boolean | FilterValue[];
type Filter = Record<string, FilterValue | Record<string, FilterValue>>;
interface SearchOption {
  searchFields?: string[];
  page?: number;
  pageSize?: number;
}
interface User {
  username: string;
  user_id: string | number;
  api_key?: string;
  github?: string;
  email?: string;
  google?: string;
  linux_do?: string;
  apple?: string;
  microsoft?: string;
}
interface Serializable {
  toString(): string;
}
interface DeSerializable<T> {
  fromString(str: string): T;
}
interface CloudModel {
  modelType: 'settings' | 'executable';
  code?: string;
  name: string;
  id: string;
  cloudId?: string;
  embedded: boolean;
  description: string;
  uploader: User;
  updatedAt: string;
  createdAt: string;
  md5: string;
  toFormatedString(verbose?: boolean): string;
}
type Shareable<T> = Serializable & DeSerializable<T> & CloudModel;
declare abstract class AbstractShareable<T> implements Shareable<T> {
  constructor(params?: Partial<AbstractShareable<T>>);
  modelType: 'settings' | 'executable';
  code?: string;
  createdAt: string;
  description: string;
  embedded: boolean;
  id: string;
  cloudId?: string;
  name: string;
  uploader: User;
  updatedAt: string;
  fromString(str: string): T;
  toString(): string;
  toFormatedString(verbose?: boolean): string;
  md5: string;
}
interface Wait {
  ready(): Promise<void>;
}
//#endregion
//#region src/types/processors.d.ts
declare class ProcessorDTO extends AbstractShareable<ProcessorDTO> {
  constructor(params: Partial<ProcessorDTO>);
  type: 'pre' | 'post';
  fromString(str: string): ProcessorDTO;
  toFormatedString(_verbose?: boolean): string;
}
interface Processor {
  type: 'pre' | 'post';
  name: string;
}
/**
 * 继承这个来实现一个前处理器
 */
declare abstract class PreProcessor implements Processor {
  type: 'pre';
  abstract process(message: UserMessage): Promise<UserMessage>;
  name: string;
  id: string;
}
/**
 * 继承这个来实现一个后处理器
 */
declare abstract class PostProcessor implements Processor {
  type: 'post';
  abstract process(message: AssistantMessage): Promise<AssistantMessage>;
  name: string;
  id: string;
}
//#endregion
//#region src/types/adapter.d.ts
interface ToolCallLimitConfig {
  maxConsecutiveCalls?: number;
  maxConsecutiveIdenticalCalls?: number;
}
declare class SendMessageOption implements Serializable, DeSerializable<SendMessageOption> {
  constructor(option: Partial<SendMessageOption>);
  static create(options?: SendMessageOption | Partial<SendMessageOption>): SendMessageOption;
  model?: string;
  temperature?: number;
  maxToken?: number;
  /**
     * 将系统提示词覆盖
     */
  systemOverride?: string;
  /**
     * 本轮对话禁用历史
     */
  disableHistoryRead?: boolean;
  /**
     * 禁用本轮对话存储到历史
     */
  disableHistorySave?: boolean;
  /**
     * 对话ID。如果不传则默认为第一次对话
     */
  conversationId?: string;
  /**
     * 上一条消息的ID，如果不传则默认为第一次对话
     */
  parentMessageId?: string;
  /**
     * 流模式
     */
  stream?: boolean;
  /**
     * 是否是思考模型
     */
  isThinkingModel?: boolean;
  enableReasoning?: boolean;
  reasoningEffort?: 'high' | 'medium' | 'low' | 'minimal';
  reasoningBudgetTokens?: number;
  toolChoice?: ToolChoice;
  postProcessorIds?: string[];
  preProcessorIds?: string[];
  toolGroupId?: string[];
  responseModalities?: string[];
  safetySettings?: SafetySetting[];
  toolCallLimit?: ToolCallLimitConfig;
  _consecutiveToolCallCount?: number;
  _consecutiveIdenticalToolCallCount?: number;
  _lastToolCallSignature?: string;
  /**
   * Per-tool call timeout in ms. Overrides channel-level toolTimeoutMs.
   */
  toolTimeoutMs?: number;
  /**
   * Agent context fields propagated through the call chain.
   * Set automatically by Chaite when running under a background job or plan.
   */
  jobId?: string;
  planId?: string;
  skillName?: string;
  /**
     * 流模式的回调
     * @param chunk
     */
  onChunk?(chunk: ModelResponseChunk): Promise<void>;
  /**
     * 工具调用轮次，如果有其他非tool_call类消息会被丢弃最后不会返回，但是可以通过onMessageWithToolCall来处理
     * @param message
     */
  onMessageWithToolCall?(message: MessageContent): Promise<void>;
  fromString(str: string): SendMessageOption;
  toString(): string;
}
interface ToolChoice {
  type: 'none' | 'any' | 'auto' | 'specified';
  tools?: string[];
}
interface EmbeddingOption {
  model: string;
  dimensions?: number;
}
type ClientType = 'openai' | 'gemini' | 'claude';
interface IClient {
  name: ClientType;
  features: Feature[];
  tools: Tool[];
  baseUrl: string;
  apiKey: string | string[];
  multipleKeyStrategy: MultipleKeyStrategy;
  historyManager: HistoryManager;
  postProcessors?: PostProcessor[];
  preProcessors?: PreProcessor[];
  sendMessage(message: UserMessage | undefined, options?: SendMessageOption | Partial<SendMessageOption>): Promise<ModelResponse>;
  sendMessageWithHistory(history: IMessage[], options?: SendMessageOption | Partial<SendMessageOption>): Promise<IMessage & {
    usage: ModelUsage;
  }>;
  getEmbedding(text: string | string[], options: EmbeddingOption): Promise<EmbeddingResult>;
  logger: ILogger;
}
interface HistoryManager {
  name: string;
  saveHistory(message: HistoryMessage, conversationId: string): Promise<void>;
  getHistory(messageId?: string, conversationId?: string): Promise<HistoryMessage[]>;
  deleteConversation(conversationId: string): Promise<void>;
  getOneHistory(messageId: string, conversationId: string): Promise<HistoryMessage | undefined>;
}
declare abstract class AbstractHistoryManager implements HistoryManager {
  name: string;
  abstract saveHistory(message: HistoryMessage, conversationId: string): Promise<void>;
  abstract getHistory(messageId?: string, conversationId?: string): Promise<HistoryMessage[]>;
  abstract deleteConversation(conversationId: string): Promise<void>;
  abstract getOneHistory(messageId: string, conversationId: string): Promise<HistoryMessage | undefined>;
}
//#endregion
//#region src/channels/channels.d.ts
declare class DefaultChannelLoadBalancer implements ChannelsLoadBalancer {
  constructor();
  getChannel(name: string, channels: Channel[]): Promise<Channel>;
  private groupByPriority;
  private selectByWeight;
  /**
   * 获取渠道列表及其分配的数量
   * generated by qwen-2.5-max
   *
   * @param modelName 模型名称
   * @param channels 渠道列表
   * @param totalQuantity 总数量
   * @returns 返回一个包含渠道及其分配数量的数组
   */
  getChannels(modelName: string, channels: Channel[], totalQuantity: number): Promise<{
    channel: Channel;
    quantity: number;
  }[]>;
}
/**
 * 渠道
 * 每个渠道对应一个adapter，并记录客户端的options
 */
declare class Channel extends AbstractShareable<Channel> implements Wait {
  constructor(params: Partial<Channel>);
  init(): Promise<void>;
  ready(): Promise<void>;
  getOptionsForModel(modelName: string): BaseClientOptions;
  adapterType: ClientType;
  options: BaseClientOptions;
  models: ModelConfig[];
  type: ClientType;
  weight: number;
  priority: number;
  status: 'enabled' | 'disabled';
  disabledReason?: string;
  statistics: ChannelStatistics;
  fromString(str: string): Channel;
  toString(): string;
  toFormatedString(verbose?: boolean): string;
}
//#endregion
//#region src/channels/preset.d.ts
/**
 * 对话模式预设，最终使用的
 */
declare class ChatPreset extends AbstractShareable<ChatPreset> {
  constructor(params?: Partial<ChatPreset>);
  prefix: string;
  local: boolean;
  namespace?: string;
  sendMessageOption: SendMessageOption;
  /**
   * 携带群聊上下文
   */
  groupContext?: 'disable' | 'enabled' | 'use_system';
  /**
   * 禁止系统prompt
   */
  disableSystemInstructions?: boolean;
  toString(): string;
  fromString(str: string): ChatPreset;
  toFormatedString(verbose?: boolean): string;
}
//#endregion
//#region src/types/external.d.ts
interface EventMessage {
  sender: {
    user_id: string | number;
    nickname?: string;
    card?: string;
  };
  group?: {
    group_id: number | string;
  };
}
interface UserModeSelector {
  getChatPreset(e: EventMessage): Promise<ChatPreset>;
}
declare abstract class AbstractUserModeSelector implements UserModeSelector {
  abstract getChatPreset(e: EventMessage): Promise<ChatPreset>;
}
//#endregion
//#region src/adapters/clients.d.ts
declare class AbstractClient implements IClient {
  options: BaseClientOptions;
  constructor(options: BaseClientOptions | Partial<BaseClientOptions>, context?: ChaiteContext);
  fullfillProcessors(preIds?: string[], postIds?: string[]): Promise<{
    pre: PreProcessor[];
    post: PostProcessor[];
  }>;
  fullfillTools(toolGroupIds?: string[]): Promise<Tool[]>;
  sendMessage(message: UserMessage | undefined, options: SendMessageOption | Partial<SendMessageOption>): Promise<ModelResponse>;
  protected shouldPersistHistory(message?: HistoryMessage): boolean;
  private getUsageLogContext;
  protected isEffectivelyEmptyMessage(message?: IMessage): boolean;
  private hasMeaningfulContent;
  private isMessagePartMeaningful;
  protected setToolCallLimitConfig(config?: ToolCallLimitConfig): void;
  private getEffectiveToolCallLimit;
  private updateToolCallTracking;
  private resetToolCallTracking;
  private buildToolCallSignature;
  _sendMessage(_histories: IMessage[], _apiKey: string, _options: SendMessageOption): Promise<HistoryMessage & {
    usage: ModelUsage;
  }>;
  sendMessageWithHistory(history: IMessage[], options?: SendMessageOption | Partial<SendMessageOption>): Promise<IMessage & {
    usage: ModelUsage;
  }>;
  apiKey: string | string[];
  baseUrl: string;
  features: Feature[];
  multipleKeyStrategy: MultipleKeyStrategy;
  name: ClientType;
  tools: Tool[];
  logger: ILogger;
  historyManager: HistoryManager;
  context: ChaiteContext;
  postProcessors?: PostProcessor[];
  preProcessors?: PreProcessor[];
  protected toolCallLimitConfig?: ToolCallLimitConfig;
  getEmbedding(_text: string | string[], _options: EmbeddingOption): Promise<EmbeddingResult>;
}
//#endregion
//#region src/adapters/create.d.ts
declare function createClient(name: ClientType, options: BaseClientOptions | Partial<BaseClientOptions>, context?: ChaiteContext): IClient;
//#endregion
//#region src/adapters/impl/openai/OpenAIClient.d.ts
type OpenAIClientOptions = BaseClientOptions;
declare class OpenAIClient extends AbstractClient {
  constructor(options: OpenAIClientOptions | Partial<OpenAIClientOptions>, context?: ChaiteContext);
  _sendMessage(histories: IMessage[], apiKey: string, options: SendMessageOption): Promise<HistoryMessage & {
    usage: ModelUsage;
  }>;
  getEmbedding(text: string | string[], options: EmbeddingOption): Promise<EmbeddingResult>;
}
declare module 'openai' {
  interface ChatCompletionCreateParamsBase {
    thinking_budget_tokens?: number | null;
  }
}
//#endregion
//#region src/adapters/impl/gemini/GeminiClient.d.ts
type GeminiClientOptions = BaseClientOptions & {
  toolCallLimit?: ToolCallLimitConfig;
};
declare class GeminiClient extends AbstractClient {
  constructor(options: GeminiClientOptions | Partial<GeminiClientOptions>, context?: ChaiteContext);
  _sendMessage(histories: IMessage[], apiKey: string, options: SendMessageOption): Promise<HistoryMessage & {
    usage: ModelUsage;
  }>;
  getEmbedding(text: string | string[], options: EmbeddingOption): Promise<EmbeddingResult>;
}
//#endregion
//#region src/adapters/impl/claude/ClaudeClient.d.ts
declare class ClaudeClient extends AbstractClient {
  constructor(options: GeminiClientOptions | Partial<GeminiClientOptions>, context?: ChaiteContext);
  _sendMessage(histories: IMessage[], apiKey: string, options: SendMessageOption): Promise<HistoryMessage & {
    usage: ModelUsage;
  }>;
}
//#endregion
//#region src/share/shareable.d.ts
type ExecutableSShareableType = 'tool' | 'processor';
/**
 * T是DTO
 * C是T对应的可执行的实例类型
 * @interface ExecutableShareableManager
 */
declare abstract class ExecutableShareableManager<T extends Shareable<T>, C> {
  private type;
  private storage;
  private watcher;
  /**
   * 存储示例名称和文件名的映射
   * @private
   */
  protected instanceMap: Map<string, string>;
  /**
   * codeDirectory是存放实际代码的目录
   * @private
   */
  protected codeDirectory: string;
  cloudService?: CloudSharingService<T>;
  protected constructor(type: ExecutableSShareableType, codeDirectory: string, storage: BasicStorage<T>);
  setCloudService(cloudService: CloudSharingService<T>): void;
  /**
   * 只有可执行类型的实例才需要，如工具、处理器等。设置类不需要
   * @private
   */
  initialize(): Promise<void>;
  private setupFileWatcher;
  private scanInstances;
  /**
   * 返回实例名字们
   * @returns 实例名字列表
   */
  listInstanceNames(): Promise<string[]>;
  listInstances(): Promise<T[]>;
  getInstanceT(id: string): Promise<T | null>;
  /**
   * 获取实例对象而非Shareable DTO
   * @param name
   */
  getInstance(name: string): Promise<C | undefined>;
  /**
   * Plugins historically used both `export default new Tool()` and
   * `export class MyProcessor ...`. Resolve both shapes from the same loader.
   */
  private resolveModuleInstance;
  /**
   * 新增或更新
   * 有id就是更新，靠storage实现去控制，这里不管
   * @param instance
   * @return id
   */
  addInstance(instance: T): Promise<string>;
  addInstanceCode(name: string, code: string): Promise<void>;
  upsertInstanceT(t: T): Promise<string>;
  getInstanceTByCloudId(cloudId: string): Promise<T[]>;
  renameFile(id: string, oldName: string, newName: string): Promise<void>;
  deleteInstance(id: string): Promise<void>;
  /**
   * 序列化实例，返回DTO
   * @param name
   */
  abstract serializeInstance(name: string): Promise<T | null>;
  private checkCloudService;
  shareToCloud(id: string): Promise<string | undefined>;
  listFromCloud(filter: Filter, query: string, searchOption: SearchOption): Promise<PaginationResult<T & {
    downloaded: string;
  }>>;
  getFromCloud(shareId: string): Promise<T | null>;
  shareP2P(name: string): Promise<string | null>;
  dispose(): Promise<void>;
}
type NonExecutableSShareableType = 'chat-preset' | 'tool-settings' | 'channel' | 'tools-group';
declare abstract class NonExecutableShareableManager<T extends Shareable<T>> {
  protected type: NonExecutableSShareableType;
  protected storage: BasicStorage<T>;
  cloudService?: CloudSharingService<T>;
  protected constructor(type: NonExecutableSShareableType, storage: BasicStorage<T>);
  setCloudService(cloudService: CloudSharingService<T>): void;
  addInstance(instance: T): Promise<string>;
  deleteInstance(key: string): Promise<void>;
  listInstances(): Promise<T[]>;
  getInstance(key: string): Promise<T | null>;
  upsertInstance(t: T): Promise<string>;
  getInstanceByCloudId(cloudId: string): Promise<T[]>;
  private checkCloudService;
  shareToCloud(key: string): Promise<string | undefined>;
  shareP2P(key: string): Promise<string | null>;
  getFromCloud(shareId: string): Promise<T | null>;
  listFromCloud(filter: Filter, query: string, searchOption: SearchOption): Promise<PaginationResult<T & {
    id: string;
  }>>;
  deleteFromCloud(key: string): Promise<void>;
}
//#endregion
//#region src/share/tool.d.ts
declare class ToolManager extends ExecutableShareableManager<ToolDTO, Tool> {
  private static instance;
  private constructor();
  setCloudService(cloudService: CloudSharingService<ToolDTO>): void;
  static init(toolsDirectory: string, storage: BasicStorage<ToolDTO>): Promise<ToolManager>;
  static getInstance(): Promise<ToolManager | null>;
  serializeInstance(name: string): Promise<ToolDTO | null>;
}
//#endregion
//#region src/utils/helpers.d.ts
declare function getKey(apiKeys: string[] | string, strategy: MultipleKeyStrategy): Promise<string>;
declare const asyncLocalStorage: AsyncLocalStorage<ChaiteContext>;
declare function useEvent(): Promise<EventMessage | undefined>;
/**
 * 表示构造函数的类型
 */
type AbstractConstructor<T> = abstract new (...args: unknown[]) => T;
/**
 * 将JS代码保存到指定目录并导入其默认导出
 *
 * @param {string} jsContent - JS代码内容
 * @param {string} modulesDir - 保存模块的目录
 * @param {string} fileName - 文件名（不含路径）
 * @param {Function} AbstractClass - 抽象类，用于检查实例是否继承自它
 * @returns {Promise<T | null>} 如果默认导出继承自AbstractClass则返回该实例，否则返回null
 */
declare function saveAndLoadModule<T>(jsContent: string, modulesDir: string, fileName: string, AbstractClass: AbstractConstructor<T>): Promise<T | null>;
/**
 * 解析 JavaScript 代码中的 class 变量 name 的值
 * @param {string} code - 输入的 JavaScript 代码
 * @returns {string|null} - 解析出的 class 的 name 值，或者 null 如果没有找到
 */
declare function extractClassName(code: string): string | null;
//#endregion
//#region src/utils/kv_history.d.ts
interface KVStore {
  get(key: string): Promise<string>;
  set(key: string, value: string): Promise<void>;
  delete(key: string): Promise<void>;
}
declare class KVHistoryManager implements HistoryManager {
  private kvStore;
  name: string;
  constructor(kvStore: KVStore, name: string);
  private getConversationKey;
  private getMessageKey;
  saveHistory(message: HistoryMessage, conversationId: string): Promise<void>;
  getHistory(messageId?: string, conversationId?: string): Promise<HistoryMessage[]>;
  deleteConversation(conversationId: string): Promise<void>;
  getOneHistory(messageId: string, conversationId: string): Promise<HistoryMessage | undefined>;
}
//#endregion
//#region src/utils/history.d.ts
type HistoryCache = Map<string, Map<string, HistoryMessage>>;
declare class InMemoryHistoryManager extends AbstractHistoryManager {
  cache: HistoryCache;
  constructor();
  saveHistory(message: HistoryMessage, conversationId: string): Promise<void>;
  getHistory(messageId?: string, conversationId?: string): Promise<HistoryMessage[]>;
  deleteConversation(conversationId: string): Promise<void>;
  getOneHistory(messageId: string, conversationId: string): Promise<HistoryMessage | undefined>;
  name: string;
}
//#endregion
//#region src/utils/axios.d.ts
interface ApiError {
  status?: number;
  data?: unknown;
  message: string;
  request?: unknown;
  originalError?: Error | AxiosError;
}
interface HttpClientOptions {
  baseURL?: string;
  timeout?: number;
  headers?: Record<string, string>;
  authorizationPrefix?: string;
  getToken?: () => string | Promise<string> | null | undefined;
  onRequest?: (config: InternalAxiosRequestConfig) => InternalAxiosRequestConfig;
  onResponse?: <T>(response: T) => T;
  onError?: (error: ApiError) => ApiError;
}
/**
 * 创建HTTP客户端
 */
declare class HttpClient {
  private instance;
  private options;
  /**
   * 构造HTTP客户端
   * @param options 配置选项
   */
  constructor(options?: HttpClientOptions);
  /**
   * 创建Axios实例
   */
  private createAxiosInstance;
  /**
   * 设置拦截器
   */
  private setupInterceptors;
  /**
   * 标准化错误对象
   */
  private normalizeError;
  /**
   * 更新配置
   * @param options 新配置
   */
  updateOptions(options: Partial<HttpClientOptions>): void;
  /**
   * 获取底层 Axios 实例
   */
  getAxiosInstance(): AxiosInstance;
  /**
   * HTTP GET 请求
   */
  get<T>(url: string, config?: AxiosRequestConfig): Promise<T>;
  /**
   * HTTP POST 请求
   */
  post<T, D = unknown>(url: string, data?: D, config?: AxiosRequestConfig): Promise<T>;
  /**
   * HTTP PUT 请求
   */
  put<T, D = unknown>(url: string, data?: D, config?: AxiosRequestConfig): Promise<T>;
  /**
   * HTTP DELETE 请求
   */
  delete<T>(url: string, config?: AxiosRequestConfig): Promise<T>;
  /**
   * HTTP PATCH 请求
   */
  patch<T, D = unknown>(url: string, data?: D, config?: AxiosRequestConfig): Promise<T>;
}
/**
 * 创建默认的HTTP客户端
 */
declare const createHttpClient: (options?: HttpClientOptions) => HttpClient;
//#endregion
//#region src/utils/config.d.ts
declare const DEFAULT_HOST = "127.0.0.1";
declare const DEFAULT_PORT = 48370;
declare class GlobalConfig extends EventEmitter {
  /**
   * jwt密钥
   */
  private authKey;
  /**
   * 监听地址
   */
  private host;
  /**
   * 监听端口
   */
  private port;
  /**
   * 是否开启调试模式
   */
  private debug;
  /**
   * 登录有效期，单位秒
   */
  private loginValidTime;
  setAuthKey(key: string): void;
  setHost(host: string): void;
  setPort(port: number): void;
  setDebug(debug: boolean): void;
  setLoginValidTime(time: number): void;
  getAuthKey(): string;
  getHost(): string;
  getPort(): number;
  getDebug(): boolean;
  getLoginValidTime(): number;
}
//#endregion
//#region src/utils/auth.d.ts
declare class FrontEndAuthHandler {
  private token;
  constructor();
  generateToken(timeout?: number, onetime?: boolean): string;
  validateToken(token: string): boolean;
  static generateJWT(key: string): string;
  static validateJWT(key: string, token: string): boolean;
}
//#endregion
//#region src/utils/logger.d.ts
declare function getLogger(): ILogger;
//#endregion
//#region src/share/cloud.d.ts
declare class DefaultCloudService<T extends AbstractShareable<T>> implements CloudSharingService<T> {
  private cloudApiBaseUrl;
  client: HttpClient;
  type: CloudAPIType;
  user: User;
  identifier: string;
  apiKey: string;
  constructor(cloudApiBaseUrl: string, type: CloudAPIType);
  setUser(user: User): void;
  authenticate(apiKey: string): Promise<User | null>;
  list(filter: Filter, query: string, searchOption: SearchOption): Promise<PaginationResult<T>>;
  private createEmptyInstance;
  private getClassForType;
  upload(model: T): Promise<T | null>;
  download(shareId: string): Promise<T | null>;
  initializeTransfer(model: T): Promise<string | null>;
  delete(shareId: string): Promise<boolean>;
  getUser(): User | null;
}
//#endregion
//#region src/share/channels.d.ts
declare class ChannelsManager extends NonExecutableShareableManager<Channel> {
  protected storage: BasicStorage<Channel>;
  private loadBalancer;
  private static instance;
  private constructor();
  static init(storage: BasicStorage<Channel>, loadBalancer: ChannelsLoadBalancer): Promise<ChannelsManager>;
  static getInstance(): Promise<ChannelsManager | null>;
  getAllChannels(name?: string): Promise<Channel[]>;
  getChannelByType(type: Channel['type']): Promise<Channel[]>;
  getChannelByStatus(status: 'enabled' | 'disabled'): Promise<Channel[]>;
  getChannelByModel(model: string): Promise<Channel[]>;
  getChannelsByModel(model: string, totalQuantity: number): Promise<{
    channel: Channel;
    quantity: number;
  }[]>;
}
//#endregion
//#region src/share/processors.d.ts
declare class ProcessorsManager extends ExecutableShareableManager<ProcessorDTO, Processor> {
  private static instance;
  private constructor();
  setCloudService(cloudService: CloudSharingService<ProcessorDTO>): void;
  static init(processorsDirectory: string, storage: BasicStorage<ProcessorDTO>): Promise<ProcessorsManager>;
  static getInstance(): Promise<ProcessorsManager | null>;
  serializeInstance(name: string): Promise<ProcessorDTO | null>;
}
//#endregion
//#region src/share/preset.d.ts
declare class ChatPresetManager extends NonExecutableShareableManager<ChatPreset> {
  protected storage: BasicStorage<ChatPreset>;
  private static instance;
  private constructor();
  static init(storage: BasicStorage<ChatPreset>): Promise<ChatPresetManager>;
  static getInstance(): Promise<ChatPresetManager | null>;
  getAllPresets(options?: {
    includeLocal?: boolean;
    namespace?: string;
  }): Promise<ChatPreset[]>;
  getPresetByPrefix(prefix: string): Promise<ChatPreset | null>;
}
//#endregion
//#region src/share/tools_group.d.ts
declare class ToolsGroupManager extends NonExecutableShareableManager<ToolsGroupDTO> {
  private static instance;
  private constructor();
  static init(storage: BasicStorage<ToolsGroupDTO>): Promise<ToolsGroupManager>;
  static getInstance(): Promise<ToolsGroupManager | null>;
}
//#endregion
//#region src/share/trigger.d.ts
/**
 * 专门用于管理触发器的管理器
 * 由于触发器需要注册并保持状态运行，因此管理方式不同于普通的可执行代码
 */
declare class TriggerManager<T extends TriggerDTO> {
  private storage;
  /**
   * 存储触发器名称到 DTO ID 的映射
   * 这样通过实例的 name 就可以找到对应的 DTO id
   */
  protected nameToIdMap: Map<string, string>;
  private watcher;
  /**
   * 存储示例名称和文件名的映射
   */
  protected instanceMap: Map<string, string>;
  /**
   * 已注册的触发器实例
   */
  protected activeInstances: Map<string, Trigger>;
  /**
   * 全局上下文，用于传递给触发器
   */
  protected context: ChaiteContext;
  /**
   * codeDirectory是存放实际代码的目录
   */
  protected codeDirectory: string;
  cloudService?: CloudSharingService<T>;
  constructor(codeDirectory: string, storage: BasicStorage<T>);
  setCloudService(cloudService: CloudSharingService<T>): void;
  /**
   * 初始化管理器，加载所有触发器并注册
   */
  initialize(): Promise<void>;
  /**
   * 设置文件监听器，监控触发器文件的变化
   */
  private setupFileWatcher;
  /**
   * 扫描触发器实例
   */
  private scanInstances;
  /**
   * 注册所有启用状态的触发器
   */
  private registerAllEnabledTriggers;
  /**
   * 注册单个触发器
   */
  registerTrigger(dtoName: string): Promise<void>;
  /**
  * 通过触发器实例的 name 删除触发器
  * 注意：这里的 name 是触发器实例的 name，而不是 DTO 中的 name
  */
  deleteInstanceByName(triggerName: string): Promise<void>;
  /**
   * 取消注册触发器
   */
  unregisterTrigger(name: string): Promise<void>;
  /**
   * 返回实例名字们
   */
  listInstanceNames(): Promise<string[]>;
  /**
   * 获取所有触发器DTO
   */
  listInstances(): Promise<T[]>;
  /**
   * 获取触发器DTO
   */
  getInstanceT(id: string): Promise<T | null>;
  /**
   * 通过名称获取触发器DTO
   */
  getInstanceTByName(name: string): Promise<T | null>;
  /**
   * 获取触发器实例对象
   */
  getInstance(name: string): Promise<Trigger | undefined>;
  /**
   * 新增或更新触发器
   */
  addInstance(instance: T): Promise<string>;
  /**
   * 添加触发器代码
   */
  addInstanceCode(name: string, code: string): Promise<void>;
  /**
   * 更新或新增触发器
   */
  upsertInstanceT(t: T): Promise<string>;
  /**
   * 通过云ID获取触发器
   */
  getInstanceTByCloudId(cloudId: string): Promise<T[]>;
  /**
   * 重命名触发器文件
   */
  renameFile(id: string, oldName: string, newName: string): Promise<void>;
  /**
   * 删除触发器
   */
  deleteInstance(id: string): Promise<void>;
  /**
   * 更新上下文
   */
  updateContext(context: ChaiteContext): void;
  /**
   * 序列化触发器，返回DTO
   */
  serializeInstance(name: string): Promise<T | null>;
  private checkCloudService;
  shareToCloud(id: string): Promise<string | undefined>;
  listFromCloud(filter: Filter, query: string, searchOption: SearchOption): Promise<PaginationResult<T & {
    downloaded: string;
  }>>;
  getFromCloud(shareId: string): Promise<T | null>;
  shareP2P(name: string): Promise<string | null>;
  /**
   * 清理资源，关闭文件监听器
   */
  dispose(): Promise<void>;
  /**
   * 启用触发器
   */
  enableTrigger(id: string): Promise<void>;
  /**
   * 禁用触发器
   */
  disableTrigger(id: string): Promise<void>;
  /**
   * 检查触发器是否正在运行
   */
  isRunning(name: string): boolean;
  /**
   * 获取当前运行中的所有触发器
   */
  getRunningTriggers(): string[];
}
//#endregion
//#region src/types/operation-log.d.ts
/**
 * Operation log types for tracking all AI system activities.
 */
/** Fine-grained categories of AI system operations */
type OperationLogType = 'llm.call' | 'processor.call' | 'processor.error' | 'trigger.call' | 'trigger.error' | 'tool.call' | 'tool.error' | 'job.queued' | 'job.start' | 'job.complete' | 'job.failed' | 'job.progress' | 'task.fire' | 'task.complete' | 'task.failed' | 'plan.start' | 'plan.step.start' | 'plan.step.end' | 'plan.complete' | 'plan.failed';
type OperationLogLevel = 'info' | 'warn' | 'error';
interface OperationLog {
  id: string;
  timestamp: number;
  type: OperationLogType;
  level: OperationLogLevel;
  /** Short human-readable summary */
  summary: string;
  /** Extended detail, error message or result snippet */
  detail?: string;
  userId?: string;
  /** QQ group id; omitted for private conversations and system tasks */
  groupId?: string;
  conversationId?: string;
  channelId?: string;
  channelName?: string;
  /** Model name for LLM calls */
  model?: string;
  presetId?: string;
  presetName?: string;
  /** Tool name for tool.call / tool.error */
  toolName?: string;
  processorName?: string;
  triggerName?: string;
  skillName?: string;
  jobId?: string;
  planId?: string;
  stepId?: string;
  /** Elapsed time in milliseconds */
  durationMs?: number;
  /** Input tokens consumed (LLM calls) */
  inputTokens?: number;
  /** Output tokens produced (LLM calls) */
  outputTokens?: number;
  totalTokens?: number;
  cachedTokens?: number;
  reasoningTokens?: number;
  /** Arbitrary extra metadata */
  metadata?: Record<string, unknown>;
}
interface ListLogsFilter {
  type?: OperationLogType;
  level?: OperationLogLevel;
  userId?: string;
  groupId?: string;
  channelId?: string;
  model?: string;
  presetId?: string;
  toolName?: string;
  processorName?: string;
  triggerName?: string;
  /** Unix ms — only logs at or after this time */
  from?: number;
  /** Unix ms — only logs at or before this time */
  to?: number;
  /** Keyword search in summary / detail */
  keyword?: string;
  page?: number;
  pageSize?: number;
}
interface LogsPage {
  items: OperationLog[];
  total: number;
  page: number;
  pageSize: number;
}
interface LogsStats {
  total: number;
  byType: Partial<Record<OperationLogType, number>>;
  byLevel: Record<OperationLogLevel, number>;
  /** Count of logs in last 24 h */
  last24h: number;
  /** Avg LLM call duration ms */
  avgLlmDurationMs: number;
  /** Total tokens (input + output) */
  totalTokens: number;
  inputTokens: number;
  outputTokens: number;
  cachedTokens: number;
  reasoningTokens: number;
  errorRate: number;
  byChannel: Record<string, number>;
  byModel: Record<string, number>;
  byPreset: Record<string, number>;
}
//#endregion
//#region src/types/storage.d.ts
/**
 * 基本存储接口
 */
interface BasicStorage<T> {
  getName(): string;
  getItem(key: string): Promise<T | null>;
  setItem(key: string, value: T): Promise<string>;
  removeItem(key: string): Promise<void>;
  listItems(): Promise<T[]>;
  listItemsByEqFilter(filter: Record<string, unknown>): Promise<T[]>;
  listItemsByInQuery(query: Array<{
    field: string;
    values: unknown[];
  }>): Promise<T[]>;
  clear(): Promise<void>;
}
declare abstract class ChaiteStorage<T> implements BasicStorage<T> {
  abstract getItem(key: string): Promise<T | null>;
  abstract setItem(key: string, value: T): Promise<string>;
  abstract removeItem(key: string): Promise<void>;
  abstract listItems(): Promise<T[]>;
  abstract listItemsByEqFilter(filter: Record<string, unknown>): Promise<T[]>;
  abstract listItemsByInQuery(query: Array<{
    field: string;
    values: unknown[];
  }>): Promise<T[]>;
  abstract clear(): Promise<void>;
  getName(): string;
}
interface UserState {
  userId: number | string;
  nickname?: string;
  card?: string;
  conversations: Conversation[];
  settings: UserSettings;
  current: {
    conversationId?: string;
    messageId?: string;
  };
}
interface Conversation {
  id: string;
  name: string;
  lastMessageId: string;
}
interface UserSettings {
  preset: string;
  model: string;
  temperature: number;
  maxToken: number;
  systemOverride: string;
  enableReasoning: boolean;
  reasoningEffort: 'high' | 'medium' | 'low';
  reasoningBudgetTokens: number;
  toolChoice: ToolChoice;
}
//#endregion
//#region src/share/operation-log.d.ts
/**
 * Manages operation logs for the AI system.
 *
 * By default stores up to `maxInMemory` recent entries in a ring buffer.
 * Optionally accepts a `BasicStorage<OperationLog>` for durable persistence —
 * when provided, all writes go to storage and reads query from there.
 *
 * Attach to Chaite via `chaite.setOperationLogManager(mgr)` to activate
 * event-based logging of jobs, plans, and tool calls.
 */
declare class OperationLogManager {
  private readonly storage?;
  private readonly memBuf;
  private readonly maxInMemory;
  constructor(storage?: BasicStorage<OperationLog> | undefined, maxInMemory?: number);
  add(entry: Omit<OperationLog, 'id' | 'timestamp'> & Partial<Pick<OperationLog, 'id' | 'timestamp'>>): Promise<OperationLog>;
  /** Convenience: add a log entry synchronously (fire-and-forget, swallows errors) */
  addSync(entry: Omit<OperationLog, 'id' | 'timestamp'> & Partial<Pick<OperationLog, 'id' | 'timestamp'>>): void;
  list(filter?: ListLogsFilter): Promise<LogsPage>;
  getStats(): Promise<LogsStats>;
  clear(filter?: Pick<ListLogsFilter, 'type' | 'level' | 'userId' | 'from' | 'to'>): Promise<number>;
  private pushMemory;
  private matchesFilter;
}
//#endregion
//#region src/rag/vector.d.ts
/**
 * generated by claude-3-5-sonnet
 */
interface VectorDatabase {
  /**
   * 添加单个向量到数据库
   * @param vector - 要添加的向量，表示为数字数组
   * @param text - 与向量关联的文本
   * @returns 返回添加的向量的唯一标识符
   */
  addVector(vector: number[], text: string): Promise<string>;
  /**
   * 批量添加多个向量到数据库
   * @param vectors - 要添加的向量数组，每个向量表示为数字数组
   * @param texts - 与向量关联的文本数组，顺序应与向量数组对应
   * @returns 返回添加的向量的唯一标识符数组
   */
  addVectors(vectors: number[][], texts: string[]): Promise<string[]>;
  /**
   * 搜索与给定查询向量最相似的向量
   * @param queryVector - 查询向量，表示为数字数组
   * @param k - 要返回的最相似向量的数量
   * @returns 返回最相似向量的数组，每个元素包含id、相似度分数和关联文本
   */
  search(queryVector: number[], k: number): Promise<Array<{
    id: string;
    score: number;
    text: string;
  }>>;
  /**
   * 根据ID获取特定的向量
   * @param id - 向量的唯一标识符
   * @returns 返回包含向量和关联文本的对象，如果未找到则返回null
   */
  getVector(id: string): Promise<{
    vector: number[];
    text: string;
  } | null>;
  /**
   * 从数据库中删除指定ID的向量
   * @param id - 要删除的向量的唯一标识符
   * @returns 返回一个布尔值，表示删除操作是否成功
   */
  deleteVector(id: string): Promise<boolean>;
  /**
   * 更新数据库中指定ID的向量
   * @param id - 要更新的向量的唯一标识符
   * @param newVector - 新的向量，表示为数字数组
   * @param newText - 新的关联文本
   * @returns 返回一个布尔值，表示更新操作是否成功
   */
  updateVector(id: string, newVector: number[], newText: string): Promise<boolean>;
  /**
   * 获取数据库中向量的总数
   * @returns 返回数据库中存储的向量总数
   */
  count(): Promise<number>;
  /**
   * 清空数据库中的所有向量
   * @returns 无返回值
   */
  clear(): Promise<void>;
}
declare abstract class AbstractVectorDatabase implements VectorDatabase {
  abstract addVector(vector: number[], text: string): Promise<string>;
  abstract addVectors(vectors: number[][], texts: string[]): Promise<string[]>;
  abstract search(queryVector: number[], k: number): Promise<Array<{
    id: string;
    score: number;
    text: string;
  }>>;
  abstract getVector(id: string): Promise<{
    vector: number[];
    text: string;
  } | null>;
  abstract deleteVector(id: string): Promise<boolean>;
  abstract clear(): Promise<void>;
  abstract count(): Promise<number>;
  abstract updateVector(id: string, newVector: number[], newText: string): Promise<boolean>;
}
//#endregion
//#region src/rag/manager.d.ts
declare class RAGManager {
  private vectorDb;
  private vectorizer;
  constructor(vectorDb: VectorDatabase, vectorizer: Vectorizer);
  /**
   * 添加文档到 RAG 系统
   * @param document 文档文本
   * @param maxNum 最大分割数
   */
  addDocument(document: string, maxNum?: number): Promise<void>;
  /**
   * 执行 RAG 查询
   * @param query 查询文本
   * @param k 返回结果数量
   * @returns 查询结果
   */
  query(query: string, k: number): Promise<RAGResult[]>;
  /**
   * 聚合检索出来的结果
   * @param results 文本形式
   * @param filterScore 过滤的最低分数
   */
  getAggregatedResults(results: RAGResult[], filterScore?: number): string;
}
//#endregion
//#region src/rag/impl/VectorizerImpl.d.ts
declare class VectorizerImpl implements Vectorizer {
  private client;
  private model;
  private dimensions;
  constructor(client: IClient, model: string, dimensions: number);
  textToVector(text: string): Promise<number[]>;
  batchTextToVector(texts: string[]): Promise<number[][]>;
}
//#endregion
//#region src/types/document.d.ts
type ParserType = 'docx' | 'xlsx' | 'pptx' | 'pdf' | 'txt' | 'other';
type TypePredicate<T> = (value: unknown) => value is T;
interface Parser<T> {
  type: ParserType;
  supportedExtensions: ValidExtension[];
  isCompatibleType: TypePredicate<T>;
  parse(document: T): Promise<string>;
}
type ValidExtension = `.${string}` | string;
type FilePath = `${string}${ValidExtension}`;
declare class DocumentPathParser implements Parser<FilePath> {
  isCompatibleType(value: unknown): value is string;
  supportedExtensions: ValidExtension[];
  type: ParserType;
  parse(document: FilePath): Promise<string>;
}
declare class DocumentFileParser implements Parser<Buffer> {
  isCompatibleType(value: unknown): value is Buffer;
  supportedExtensions: ValidExtension[];
  type: ParserType;
  parse(document: Buffer): Promise<string>;
}
//#endregion
//#region src/rag/impl/PdfParser.d.ts
declare class PdfFileParser extends DocumentPathParser {
  constructor();
  private initPromise;
  valid: boolean;
  type: ParserType;
  supportedExtensions: string[];
  private initializePdfParse;
  ready(): Promise<void>;
  parse(documentPath: string): Promise<string>;
}
declare class PdfBufferParser extends DocumentFileParser {
  constructor();
  private initPromise;
  private initializePdfParse;
  valid: boolean;
  type: ParserType;
  supportedExtensions: string[];
  ready(): Promise<void>;
  parse(buffer: Buffer): Promise<string>;
}
//#endregion
//#region src/rag/impl/DocxParser.d.ts
declare class DocxFileParser extends DocumentPathParser {
  constructor();
  type: ParserType;
  supportedExtensions: string[];
  parse(documentPath: string): Promise<string>;
}
declare class DocxBufferParser extends DocumentFileParser {
  constructor();
  type: ParserType;
  supportedExtensions: string[];
  parse(buffer: Buffer): Promise<string>;
}
//#endregion
//#region src/rag/impl/PureTextParser.d.ts
declare class PureTextFileParser extends DocumentPathParser {
  constructor();
  type: ParserType;
  supportedExtensions: string[];
  parse(documentPath: string): Promise<string>;
}
declare class PureTextBufferParser extends DocumentFileParser {
  constructor();
  type: ParserType;
  supportedExtensions: string[];
  parse(buffer: Buffer): Promise<string>;
}
//#endregion
//#region src/rag/text.d.ts
declare class TextProcessor {
  private static readonly punctuationRegex;
  private static readonly stopWords;
  /**
   * 将文本分割成小段落
   * @param text 输入的长文本
   * @param maxLength 每个段落的最大长度
   * @returns 分割后的段落数组
   */
  static splitIntoChunks(text: string, maxLength: number): string[];
  /**
   * 清理和标准化文本
   * @param text 输入文本
   * @returns 清理后的文本
   */
  static cleanText(text: string): string;
  /**
   * 简单的分词（按空格分割英文，按字符分割中文）
   * @param text 输入文本
   * @returns 分词后的数组
   */
  static tokenize(text: string): string[];
  /**
   * 从文本中移除停用词
   * @param text 输入文本
   * @returns 移除停用词后的文本
   */
  static removeStopWords(text: string): string;
  /**
   * 提取文本中的关键词（基于简单频率）
   * @param text 输入文本
   * @param topN 返回的关键词数量
   * @returns 关键词数组
   */
  static extractKeywords(text: string, topN?: number): string[];
  /**
   * 计算两个文本之间的简单相似度
   * @param text1 第一个文本
   * @param text2 第二个文本
   * @returns 相似度分数（0-1之间）
   */
  static simpleSimilarity(text1: string, text2: string): number;
}
//#endregion
//#region src/agent/skills/skill.types.d.ts
type ExecutionMode = 'plan' | 'workflow' | 'direct';
interface SkillFrontmatter {
  /** Unique skill name, a-z0-9- recommended */
  name: string;
  description: string;
  version?: string;
  license?: string;
  compatibility?: string;
  /** Tool names this skill is allowed to invoke */
  allowedTools?: string[];
  executionMode?: ExecutionMode;
  /** Model used for the planning step when executionMode=plan */
  planningModel?: string;
  /** Path to workflow definition file when executionMode=workflow */
  workflowRef?: string;
  /** ChatPreset name to activate */
  preset?: string;
  /** Processor ids */
  processors?: string[];
  /** Trigger ids */
  triggers?: string[];
  /**
   * When true, this skill is protected from modification or deletion by the LLM's
   * self-management tools (update_skill / delete_skill / create_skill overwrite).
   * Set this in SKILL.md for developer-configured skills you don't want the LLM to touch.
   * Only a human with filesystem access can modify readonly skills.
   */
  readonly?: boolean;
  metadata?: Record<string, unknown>;
}
interface Skill {
  id: string;
  rootDir: string;
  frontmatter: SkillFrontmatter;
  /**
   * Progressive disclosure: load full instruction body on demand.
   * Tries system-prompt.md first, falls back to SKILL.md body.
   */
  loadInstructions(): Promise<string>;
  /** Read a file relative to the skill root directory */
  readFile(relPath: string): Promise<Buffer>;
}
type SkillMeta = Pick<Skill, 'id' | 'rootDir' | 'frontmatter'>;
//#endregion
//#region src/agent/skills/SkillRegistry.d.ts
declare class SkillRegistry {
  private skills;
  private readonly skillsDir;
  constructor(skillsDir: string);
  load(): Promise<void>;
  listSkillMetas(): SkillMeta[];
  getSkillByName(name: string): Skill | null;
  getAllSkills(): Skill[];
  getSkillsDir(): string;
  /**
   * Simple rule-based skill matcher.
   * Searches skill descriptions and names for keyword overlap with the user message.
   * Returns the best match, or null if no good match found.
   */
  matchSkill(userMessage: string): Skill | null;
}
//#endregion
//#region src/agent/background/job.types.d.ts
type JobStatus = 'queued' | 'running' | 'done' | 'failed';
interface BackgroundJob {
  id: string;
  description: string;
  userId: string;
  groupId?: string;
  skillName?: string;
  conversationId?: string;
  /** Associated plan id if the job runs a Plan */
  planId?: string;
  status: JobStatus;
  /** Final output text, available when status=done */
  result?: string;
  error?: string;
  createdAt: number;
  updatedAt: number;
}
/** Payload returned by job:complete / job:failed events */
interface JobCompletePayload {
  jobId: string;
  userId: string;
  groupId?: string;
  status: 'done' | 'failed';
  result?: string;
  error?: string;
  planId?: string;
}
/** Payload returned by job:progress events (per plan step) */
interface JobProgressPayload {
  jobId: string;
  userId: string;
  groupId?: string;
  planId?: string;
  stepIndex?: number;
  stepTotal?: number;
  stepTitle?: string;
}
//#endregion
//#region src/agent/contracts.d.ts
interface AgentToolCall {
  id: string;
  name: string;
  arguments: Record<string, unknown>;
}
interface ToolResult {
  id: string;
  name: string;
  ok: boolean;
  outputText: string;
  error?: {
    kind: 'RETRYABLE' | 'POLICY_DENIED' | 'FATAL';
    message: string;
    cause?: unknown;
  };
  meta?: Record<string, unknown>;
}
interface ToolSchema {
  name: string;
  description?: string;
  schema?: unknown;
}
interface ExecutionContext {
  runId: string;
  userId: string;
  groupId?: string;
  conversationId?: string;
  skillName?: string;
  jobId?: string;
  planId?: string;
  timeoutMs?: number;
  tags?: Record<string, string>;
}
interface ToolExecutor {
  execute(call: AgentToolCall, ctx: ExecutionContext): Promise<ToolResult>;
  listAvailableTools?(ctx: ExecutionContext): Promise<ToolSchema[]>;
}
interface SandboxOptions {
  timeoutMs?: number;
  /** Allowed env vars; empty = no env passed */
  env?: Record<string, string>;
  maxOutputBytes?: number;
}
interface SandboxResult {
  output: string;
  exitCode: number;
  timedOut: boolean;
}
/**
 * Abstract sandbox execution contract.
 * Local impl: child_process with restricted env + timeout.
 * Remote impl: POST to a REST sandbox API.
 */
interface SandboxExecutor {
  run(entry: string, argsJson: string, opts?: SandboxOptions): Promise<SandboxResult>;
}
/** Minimal interface used by workflow/plan steps to avoid circular dep */
interface IBackgroundJobManager {
  createAndRun(spec: BackgroundJobSpec): Promise<string>;
  getJob(jobId: string): Promise<BackgroundJob | null>;
}
interface BackgroundJobSpec {
  description: string;
  userId: string;
  groupId?: string;
  skillName?: string;
  conversationId?: string;
  /** Arbitrary data for the executor to consume */
  payload?: Record<string, unknown>;
}
/**
 * Runtime context passed to workflow runners and plan executors.
 * Injected by Chaite at execution time — avoids constructor-level circular deps.
 */
interface AgentRunContext {
  /** Send a message through the main LLM pipeline */
  sendMessage(goal: string, options?: Record<string, unknown>): Promise<ModelResponse>;
  toolExecutor?: ToolExecutor;
  storage: BasicStorage<unknown>;
  logger: ILogger;
  jobManager?: IBackgroundJobManager;
  /** Emit a structured audit/progress event */
  emitAudit?(event: AuditEvent): void;
}
type AuditEventType = 'tool:start' | 'tool:end' | 'tool:error' | 'job:queued' | 'job:start' | 'job:complete' | 'job:failed' | 'job:progress' | 'plan:start' | 'plan:step:start' | 'plan:step:end' | 'plan:step:revised' | 'plan:complete' | 'plan:failed';
interface AuditEvent {
  type: AuditEventType;
  timestamp: number;
  runId?: string;
  jobId?: string;
  planId?: string;
  stepId?: string;
  userId?: string;
  groupId?: string;
  data?: Record<string, unknown>;
}
interface AuditEmitter {
  emit(event: AuditEvent): void;
}
//#endregion
//#region src/agent/planning/plan.types.d.ts
type PlanStepStatus = 'pending' | 'running' | 'done' | 'failed' | 'skipped' | 'revised';
type PlanStatus = 'planning' | 'executing' | 'reviewing' | 'done' | 'failed';
interface PlanStep {
  id: string;
  /** 1-based display index */
  index: number;
  /** Short title shown in progress notifications */
  title: string;
  description: string;
  /** Hint about which tool to use; LLM may suggest this */
  toolHint?: string;
  /** Step ids this step depends on */
  dependsOn?: string[];
  status: PlanStepStatus;
  /** Step output after execution */
  result?: string;
  startedAt?: number;
  completedAt?: number;
  retryCount: number;
}
interface Plan {
  id: string;
  /** Associated background job id, if running asynchronously */
  jobId?: string;
  goal: string;
  steps: PlanStep[];
  status: PlanStatus;
  /** How many times the plan has been revised during execution */
  revision: number;
  createdAt: number;
  updatedAt: number;
}
/** Compact progress snapshot for QQ push notifications */
interface PlanProgress {
  planId: string;
  goal: string;
  status: PlanStatus;
  totalSteps: number;
  doneSteps: number;
  currentStep?: {
    index: number;
    title: string;
    status: PlanStepStatus;
  };
  revision: number;
}
declare function getPlanProgress(plan: Plan): PlanProgress;
//#endregion
//#region src/agent/planning/Planner.d.ts
/**
 * Abstract Planner interface.
 * Takes a goal and returns a structured Plan (not yet executed).
 */
interface Planner {
  createPlan(goal: string, ctx: AgentRunContext): Promise<Plan>;
}
/**
 * Default LLM-based planner.
 * Asks the LLM to produce a JSON plan with structured steps.
 * If the LLM output cannot be parsed as structured JSON, falls back to
 * a single-step plan with a direct LLM call.
 */
declare class LlmPlanner implements Planner {
  private readonly planningModel?;
  constructor(planningModel?: string | undefined);
  createPlan(goal: string, ctx: AgentRunContext): Promise<Plan>;
  private parsePlanJson;
}
//#endregion
//#region src/agent/planning/PlanReviewer.d.ts
interface ReviewResult {
  /** Whether the remaining plan should be revised */
  shouldRevise: boolean;
  /** Replacement for remaining pending steps; undefined = keep as-is */
  revisedSteps?: PlanStep[];
  reason?: string;
}
/**
 * Abstract PlanReviewer interface.
 * Called after each step completes to decide whether to revise the remaining plan.
 */
interface PlanReviewer {
  review(plan: Plan, completedStepId: string, ctx: AgentRunContext): Promise<ReviewResult>;
}
/**
 * LLM-based plan reviewer.
 * Asks the LLM whether the remaining steps should be revised given what was learned.
 * Only triggers if the step result is significantly unexpected.
 */
declare class LlmPlanReviewer implements PlanReviewer {
  private readonly model?;
  constructor(model?: string | undefined);
  review(plan: Plan, completedStepId: string, ctx: AgentRunContext): Promise<ReviewResult>;
}
/**
 * No-op reviewer that never revises.
 * Use when you want deterministic execution without LLM-based self-correction.
 */
declare class NoopPlanReviewer implements PlanReviewer {
  review(_plan: Plan, _completedStepId: string, _ctx: AgentRunContext): Promise<ReviewResult>;
}
//#endregion
//#region src/agent/planning/PlanExecutor.d.ts
interface PlanExecutorOptions {
  planner: Planner;
  /** If provided, self-correction is enabled after each step */
  reviewer?: PlanReviewer;
  /** Max retries per step */
  maxStepRetries?: number;
  /** Storage key prefix; defaults to "plan:" */
  storagePrefix?: string;
}
/**
 * Executes a Plan created by a Planner.
 *
 * Lifecycle:
 *  1. planner.createPlan → structured Plan object
 *  2. Persist plan to storage (planId queryable by check_plan_progress)
 *  3. For each pending step (topological order):
 *     a. Set status=running, persist
 *     b. Execute step (LLM call or tool call based on toolHint)
 *     c. Set status=done/failed, persist
 *     d. Emit plan:step:end audit event (Chaite.emit job:progress)
 *     e. Optional: reviewer.review → if shouldRevise, replace pending steps
 *  4. Set plan.status=done/failed, persist
 *  5. Return final output
 */
declare class PlanExecutor {
  private readonly opts;
  private readonly storagePrefix;
  constructor(opts: PlanExecutorOptions);
  execute(goal: string, ctx: AgentRunContext, jobId?: string): Promise<{
    plan: Plan;
    output: string;
  }>;
  private runPendingSteps;
  private getNextReady;
  private executeStep;
  getPlan(planId: string, ctx: AgentRunContext): Promise<Plan | null>;
  private savePlan;
}
//#endregion
//#region src/agent/background/BackgroundJobManager.d.ts
interface BackgroundJobManagerOptions {
  storage: BasicStorage<BackgroundJob>;
  eventEmitter: EventEmitter;
  logger: ILogger;
  /** Max concurrent running jobs. Default: 3 */
  maxConcurrent?: number;
}
/**
 * JobExecutorFn is the actual async work of a job.
 * It receives the jobId and an onProgress callback to emit intermediate updates.
 * It should return the final output string.
 */
type JobExecutorFn = (jobId: string, onProgress: (progress: Omit<JobProgressPayload, 'jobId'>) => void) => Promise<string>;
/**
 * BackgroundJobManager manages the full lifecycle of background jobs:
 *   create → queue → run → complete/fail → emit events
 *
 * Consumers (e.g. karin-plugin-chaite) listen to Chaite EventEmitter:
 *   chaite.on('job:complete', (payload: JobCompletePayload) => { ... send QQ message })
 *   chaite.on('job:progress', (payload: JobProgressPayload) => { ... update status })
 *
 * System tools (spawn_background_task, check_job_status) interact with this manager
 * via ChaiteContext.getChaite().getBackgroundJobManager().
 */
declare class BackgroundJobManager implements IBackgroundJobManager {
  private readonly opts;
  private runningCount;
  private readonly maxConcurrent;
  private readonly queue;
  constructor(opts: BackgroundJobManagerOptions);
  /**
   * Create a job and schedule it for execution.
   * @returns jobId
   */
  createAndRun(spec: BackgroundJobSpec, executor?: JobExecutorFn): Promise<string>;
  getJob(jobId: string): Promise<BackgroundJob | null>;
  listJobs(userId?: string): Promise<BackgroundJob[]>;
  private schedule;
  private start;
  private drainQueue;
  /**
   * Default executor used when createAndRun is called without an explicit executor
   * (e.g. from the spawn_background_task tool).
   * Subclasses or factory patterns can override this to inject plan/workflow execution.
   */
  private makeDefaultExecutor;
  /**
   * Build an executor that runs a full agent pipeline for a given goal.
   * Called by the Chaite composition root after wiring ctx.
   */
  buildAgentExecutor(makeRunContext: (jobId: string) => AgentRunContext, planExecutor?: PlanExecutor): JobExecutorFn;
}
//#endregion
//#region src/agent/scheduler/DynamicScheduler.d.ts
interface ScheduledTask {
  id: string;
  description: string;
  /** ISO datetime string OR cron expression (e.g. "0 17 * * *") */
  schedule: string;
  /** If true, task cancels itself after first execution */
  oneShot: boolean;
  userId: string;
  groupId?: string;
  /** The agent goal to run when the task fires */
  goal: string;
  /**
   * Optional: name of a Skill whose workflow/plan to execute at fire time.
   * When set, the task routes directly to that skill's executionMode
   * instead of relying on keyword matching from the goal string.
   */
  skillName?: string;
  status: 'active' | 'cancelled' | 'done';
  createdAt: number;
  /** Timestamp of last execution */
  lastFiredAt?: number;
  /** Next scheduled execution (ISO string, for display) */
  nextFireAt?: string;
}
interface DynamicSchedulerOptions {
  storage: BasicStorage<ScheduledTask>;
  eventEmitter: EventEmitter;
  logger: ILogger;
  /** Called when a task fires — runs the agent and returns output */
  runAgent: (task: ScheduledTask, ctx: AgentRunContext) => Promise<string>;
  /**
   * Build a run context for a scheduled task execution.
   * Implementations MUST forward task.skillName to buildAgentRunContext({ skillName: task.skillName })
   * so that skills with executionMode=workflow|plan are correctly routed.
   *
   * Example:
   *   buildRunContext: (task) => chaite.buildAgentRunContext({
   *     skillName: task.skillName,
   *     userId: task.userId,
   *     groupId: task.groupId,
   *   })
   */
  buildRunContext: (task: ScheduledTask) => AgentRunContext;
}
/**
 * DynamicScheduler allows programmatic (LLM-driven) creation of scheduled tasks.
 *
 * Unlike the static CronTrigger (which requires a developer to write a .js file),
 * DynamicScheduler tasks are created at runtime via the schedule_task tool and
 * persisted in storage so they can be restored after restarts.
 *
 * At fire time:
 *   1. Build AgentRunContext
 *   2. Call runAgent(task, ctx) → the agent fetches data and returns output
 *   3. Emit 'job:complete' with the output → consumed by QQ plugin → sends message
 */
declare class DynamicScheduler {
  private readonly opts;
  private jobs;
  constructor(opts: DynamicSchedulerOptions);
  /**
   * Schedule a new task. Returns the task id.
   * @param schedule ISO datetime OR cron expression
   * @param goal Agent goal to run when the task fires
   * @param oneShot If true, runs once and cancels
   */
  scheduleTask(params: {
    schedule: string;
    goal: string;
    description: string;
    oneShot: boolean;
    userId: string;
    groupId?: string;
    skillName?: string;
  }): Promise<ScheduledTask>;
  cancelTask(taskId: string): Promise<boolean>;
  getTask(taskId: string): Promise<ScheduledTask | null>;
  listTasks(userId?: string): Promise<ScheduledTask[]>;
  /**
   * Restore all active tasks from storage on startup.
   * Should be called once during initialization.
   */
  restoreFromStorage(): Promise<void>;
  private createJob;
  private fire;
  private isIsoDatetime;
}
//#endregion
//#region src/agent/mcp/McpServerConfig.d.ts
/** Persistent configuration for a single MCP server connection */
interface McpServerConfig {
  id: string;
  name: string;
  description?: string;
  baseUrl: string;
  /** Optional Authorization header value (e.g. "Bearer xxx") */
  authHeader?: string;
  /** Default timeout for tool calls in ms */
  timeoutMs?: number;
  /** Whether this server is active */
  enabled: boolean;
  createdAt: number;
  updatedAt: number;
}
/**
 * Manager for MCP server configurations.
 * CRUD over BasicStorage — no cloud sharing (configs contain private credentials).
 *
 * These configs are used by Chaite.buildToolExecutor() to create McpToolExecutors
 * and attach them to channels at startup.
 */
declare class McpServerManager {
  private readonly storage;
  constructor(storage: BasicStorage<McpServerConfig>);
  add(config: Omit<McpServerConfig, 'id' | 'createdAt' | 'updatedAt'>): Promise<McpServerConfig>;
  update(id: string, patch: Partial<Omit<McpServerConfig, 'id' | 'createdAt'>>): Promise<McpServerConfig | null>;
  delete(id: string): Promise<void>;
  get(id: string): Promise<McpServerConfig | null>;
  list(): Promise<McpServerConfig[]>;
  listEnabled(): Promise<McpServerConfig[]>;
}
//#endregion
//#region src/agent/workflow/workflow.types.d.ts
type WorkflowStepType = 'llm' | 'tool' | 'condition' | 'parallel' | 'background' | 'plan' | 'subworkflow';
type OnError = 'abort' | 'skip' | 'retry';
interface WorkflowStepBase {
  id: string;
  type: WorkflowStepType;
  /** Human-readable label shown in progress output */
  label?: string;
  /** Step ids that must complete before this step runs */
  dependsOn?: string[];
  onError?: OnError;
  retryMax?: number;
}
interface LlmStep extends WorkflowStepBase {
  type: 'llm';
  /** Goal/prompt to send to the LLM */
  goal: string;
  /** Injected outputs from prior steps: {stepId} is replaced with that step's output */
  model?: string;
}
interface ToolStep extends WorkflowStepBase {
  type: 'tool';
  toolName: string;
  /** Static args; use "{{stepId}}" template to reference prior step outputs */
  args?: Record<string, unknown>;
}
interface ConditionStep extends WorkflowStepBase {
  type: 'condition';
  /**
   * A JavaScript expression string evaluated with `new Function`.
   * Available variable: `outputs` (Record<stepId, string>).
   * Should return a string: the id of the branch step to run next.
   */
  expression: string;
  branches: Record<string, WorkflowStep[]>;
}
interface ParallelStep extends WorkflowStepBase {
  type: 'parallel';
  steps: WorkflowStep[];
}
interface BackgroundStep extends WorkflowStepBase {
  type: 'background';
  description: string;
  /** Inner workflow or goal to run as a background job */
  goal?: string;
  workflowDef?: WorkflowDefinition;
}
interface PlanStep$1 extends WorkflowStepBase {
  type: 'plan';
  goal: string;
  planningModel?: string;
}
interface SubworkflowStep extends WorkflowStepBase {
  type: 'subworkflow';
  workflowDef: WorkflowDefinition;
}
type WorkflowStep = LlmStep | ToolStep | ConditionStep | ParallelStep | BackgroundStep | PlanStep$1 | SubworkflowStep;
type WorkflowOnComplete = 'push_qq' | 'reply' | 'silent';
interface WorkflowDefinition {
  id: string;
  name?: string;
  description?: string;
  steps: WorkflowStep[];
  onComplete?: WorkflowOnComplete;
}
interface WorkflowStepResult {
  stepId: string;
  status: 'done' | 'failed' | 'skipped';
  output?: string;
  error?: string;
  startedAt: number;
  completedAt: number;
}
interface WorkflowRunResult {
  workflowId: string;
  status: 'done' | 'failed';
  stepResults: WorkflowStepResult[];
  finalOutput?: string;
  error?: string;
  startedAt: number;
  completedAt: number;
}
//#endregion
//#region src/agent/workflow/WorkflowEngine.d.ts
/**
 * Abstract interface for workflow engines.
 * Implementations can vary in how they interpret the workflow definition
 * (e.g. simple sequential, async DAG, remote orchestrator).
 */
interface WorkflowEngine {
  run(workflow: WorkflowDefinition, ctx: AgentRunContext): Promise<WorkflowRunResult>;
}
//#endregion
//#region src/agent/tool-executors/mcp.d.ts
interface McpEndpoint {
  /** Base URL of the MCP server, e.g. https://tools.example.com/mcp */
  baseUrl: string;
  /** Optional Authorization header value */
  authHeader?: string;
  /** Default request timeout in ms */
  defaultTimeoutMs?: number;
}
/**
 * MCP client-side ToolExecutor.
 * Communicates with an MCP server over JSON-RPC 2.0 (HTTP transport).
 * Optional fallback executor is called when the remote call fails.
 *
 * Install @modelcontextprotocol/sdk for full lifecycle support;
 * this implementation uses raw fetch for minimal overhead.
 */
declare class McpToolExecutor implements ToolExecutor {
  private readonly endpoint;
  private readonly fallback?;
  private toolCache;
  constructor(endpoint: McpEndpoint, fallback?: ToolExecutor | undefined);
  execute(call: AgentToolCall, ctx: ExecutionContext): Promise<ToolResult>;
  listAvailableTools(_ctx: ExecutionContext): Promise<ToolSchema[]>;
  /** Invalidate tool list cache (call after server reconnect) */
  invalidateCache(): void;
  private rpcCall;
}
//#endregion
//#region src/core/chaite.d.ts
/**
 * 入口
 */
declare class Chaite extends EventEmitter {
  private channelsManager;
  private toolsManager;
  private processorsManager;
  private chatPresetManager;
  private toolsGroupManager;
  private triggerManager;
  private userModeSelector;
  private userStateStorage;
  private historyManager;
  private logger;
  private static instance;
  private ragManager?;
  private frontendAuthHandler;
  private globalConfig?;
  private expressApp?;
  /** SkillRegistry loaded from SKILL.md directories */
  private skillRegistry?;
  /** BackgroundJobManager for async task execution */
  private backgroundJobManager?;
  /** Shared storage for agent state (plans, jobs, etc.) */
  private agentStorage?;
  /** Dynamic scheduler for LLM-created scheduled tasks */
  private dynamicScheduler?;
  /** MCP server configuration manager */
  private mcpServerManager?;
  /** Workflow engine for executing WorkflowDefinitions */
  private workflowEngine?;
  /** Plan executor for LLM-driven dynamic planning */
  private planExecutor?;
  /** Operation log manager — records fine-grained AI activity */
  private operationLogManager;
  private constructor();
  static init(channelsManager: ChannelsManager, toolsManager: ToolManager, processorsManager: ProcessorsManager, chatPresetManager: ChatPresetManager, toolsGroupManager: ToolsGroupManager, triggerManager: TriggerManager<TriggerDTO>, userModeSelector: UserModeSelector, userStateStorage: BasicStorage<UserState>, historyManager: HistoryManager | undefined, logger: ILogger): Chaite;
  static getInstance(): Chaite;
  onUpdateCustomConfig: (config: CustomConfig) => Promise<CustomConfig>;
  setUpdateConfigCallback(callback: (config: CustomConfig) => Promise<CustomConfig>): void;
  getCustomConfig: () => Promise<CustomConfig>;
  setGetConfig(callback: () => Promise<CustomConfig>): void;
  setRAGManager(ragManager: RAGManager): void;
  getRAGManager(): RAGManager | undefined;
  setGlobalConfig(globalConfig: GlobalConfig): void;
  getGlobalConfig(): GlobalConfig | undefined;
  setFrontendAuthHandler(frontendAuthHandler: FrontEndAuthHandler): void;
  getFrontendAuthHandler(): FrontEndAuthHandler;
  /**
     * 对话
     * @param message
     * @param e
     * @param options 包含对话id和消息id
     */
  sendMessage(message: UserMessage, e: EventMessage, options: SendMessageOption & {
    chatPreset?: ChatPreset;
  }): Promise<ModelResponse>;
  setSkillRegistry(registry: SkillRegistry): void;
  getSkillRegistry(): SkillRegistry | undefined;
  setBackgroundJobManager(manager: BackgroundJobManager): void;
  getBackgroundJobManager(): BackgroundJobManager | undefined;
  setAgentStorage(storage: BasicStorage<unknown>): void;
  getAgentStorage(): BasicStorage<unknown> | undefined;
  setDynamicScheduler(scheduler: DynamicScheduler): void;
  getDynamicScheduler(): DynamicScheduler | undefined;
  setMcpServerManager(manager: McpServerManager): void;
  getMcpServerManager(): McpServerManager | undefined;
  setWorkflowEngine(engine: WorkflowEngine): void;
  getWorkflowEngine(): WorkflowEngine | undefined;
  setPlanExecutor(executor: PlanExecutor): void;
  getPlanExecutor(): PlanExecutor | undefined;
  setOperationLogManager(mgr: OperationLogManager): void;
  getOperationLogManager(): OperationLogManager | undefined;
  /**
   * Subscribe to Chaite's EventEmitter to automatically capture
   * job lifecycle and plan/tool audit events into the operation log.
   */
  private _subscribeOperationLogEvents;
  /**
   * Build a composite McpToolExecutor from all enabled MCP server configs.
   * Returns undefined if no MCP servers are configured.
   * Call this after init and assign to channel.options.toolExecutor if desired.
   *
   * Multiple MCP servers are chained as fallback: first server that has the tool wins.
   */
  buildMcpExecutor(): Promise<McpToolExecutor | undefined>;
  /**
   * Build an AgentRunContext bound to the current Chaite instance.
   * Used by BackgroundJobManager and PlanExecutor when running background tasks.
   */
  buildAgentRunContext(overrides?: {
    jobId?: string;
    skillName?: string;
    userId?: string;
    groupId?: string;
  }): AgentRunContext;
  getChannelsManager(): ChannelsManager;
  setChannelsManager(channelsManager: ChannelsManager): void;
  getToolsManager(): ToolManager;
  setToolsManager(toolsManager: ToolManager): void;
  getProcessorsManager(): ProcessorsManager;
  setProcessorsManager(processorsManager: ProcessorsManager): void;
  getChatPresetManager(): ChatPresetManager;
  setChatPresetManager(chatPresetManager: ChatPresetManager): void;
  getToolsGroupManager(): ToolsGroupManager;
  setToolsGroupManager(toolsGroupManager: ToolsGroupManager): void;
  getTriggerManager(): TriggerManager<TriggerDTO>;
  setTriggerManager(triggerManager: TriggerManager<TriggerDTO>): void;
  getUserModeSelector(): UserModeSelector;
  setUserModeSelector(userModeSelector: UserModeSelector): void;
  getUserStateStorage(): BasicStorage<UserState>;
  setUserStateStorage(userStateStorage: BasicStorage<UserState>): void;
  getHistoryManager(): HistoryManager;
  setHistoryManager(historyManager: HistoryManager): void;
  getLogger(): ILogger;
  setLogger(logger: ILogger): void;
  setCloudService(cloudServiceBaseUrl: string): void;
  auth(apiKey: string): Promise<void>;
  getExpressApp(): Application | undefined;
  runApiServer(configureApp?: (app: Application) => void): Application;
}
//#endregion
//#region src/types/common.d.ts
declare const MultipleKeyStrategyChoice: {
  RANDOM: MultipleKeyStrategy;
  ROUND_ROBIN: MultipleKeyStrategy;
  CONVERSATION_HASH: MultipleKeyStrategy;
};
type MultipleKeyStrategy = 'random' | 'round-robin' | 'conversation-hash';
declare class BaseClientOptions implements Serializable, DeSerializable<BaseClientOptions>, Wait {
  features: Feature[];
  tools: Tool[];
  baseUrl: string;
  apiKey: string | string[];
  multipleKeyStrategy?: MultipleKeyStrategy;
  proxy?: string;
  historyManager: HistoryManager;
  logger?: ILogger;
  postProcessorIds?: string[];
  private postProcessors?;
  preProcessorIds?: string[];
  private preProcessors?;
  /**
   * Optional ToolExecutor for remote-first / sandbox tool execution.
   * If provided, AbstractClient will use it instead of calling tool.run() directly.
   */
  toolExecutor?: ToolExecutor;
  /** Per-tool call timeout in ms (used by ToolExecutor if supported) */
  toolTimeoutMs?: number;
  constructor(options?: Partial<BaseClientOptions>);
  static create(options: BaseClientOptions | Partial<BaseClientOptions>): BaseClientOptions;
  private initPromise;
  ready(): Promise<void>;
  setHistoryManager(historyManager: HistoryManager): void;
  setLogger(logger: ILogger): void;
  getPostProcessors(): PostProcessor[];
  getPreProcessors(): PreProcessor[];
  init(): Promise<void>;
  toString(): string;
  fromString(str: string): BaseClientOptions;
}
interface ILogger {
  debug(msg: object | string, ...args: never[]): void;
  info(msg: object | string, ...args: never[]): void;
  warn(msg: object | string, ...args: never[]): void;
  error(msg: object | string, ...args: never[]): void;
}
declare const DefaultLogger: {
  readonly name: string;
  readonly enableColors: boolean;
  formatDate(): string;
  formatMessage(level: string, msg: object | string, args: never[]): string;
  debug(msg: object | string, ...args: never[]): void;
  error(msg: object | string, ...args: never[]): void;
  info(msg: object | string, ...args: never[]): void;
  warn(msg: object | string, ...args: never[]): void;
};
declare class ChaiteContext {
  constructor(logger?: ILogger);
  logger?: ILogger;
  chaite: Chaite;
  private options?;
  private historyMessages?;
  private event?;
  private data?;
  private client?;
  /** Set when this context is running inside a background job */
  jobId?: string;
  /** Set when this context is executing a plan */
  planId?: string;
  /** Active skill name for this request */
  skillName?: string;
  /** Active workflow id for this request */
  workflowId?: string;
  /** Resource identity used by usage logging. */
  channelId?: string;
  channelName?: string;
  presetId?: string;
  presetName?: string;
  setHistoryMessages(histories: HistoryMessage[]): void;
  getHistoryMessages(): HistoryMessage[] | undefined;
  setOptions(options: SendMessageOption): void;
  getOptions(): SendMessageOption | undefined;
  setEvent(event: EventMessage): void;
  setChaite(chaite: Chaite): void;
  getEvent(): EventMessage | undefined;
  setData(data: Record<string, any>): void;
  getData(): Record<string, any> | undefined;
  setClient(client: AbstractClient): void;
  getClient(): AbstractClient | undefined;
  getChaite(): Chaite;
}
interface Into<U> {
  into(): U;
}
interface From<T> {
  from(value: T): this;
}
interface RAGResult {
  content: string;
  score: number;
}
interface Vectorizer {
  /**
   * 将文本转换为向量
   * @param text 输入文本
   * @returns 文本的向量表示
   */
  textToVector(text: string): Promise<number[]>;
  /**
   * 批量将文本转换为向量
   * @param texts 输入文本数组
   * @returns 文本向量数组
   */
  batchTextToVector(texts: string[]): Promise<number[][]>;
}
type CloudAPIType = 'tool' | 'processor' | 'chat-preset' | 'channel' | 'tool-group' | 'trigger';
type CustomConfigValueType = string | number | boolean | object | undefined | null | CustomConfigValueType[];
type CustomConfig = Record<string, CustomConfigValueType | Record<string, CustomConfigValueType>>;
//#endregion
//#region src/types/tools.d.ts
interface Function {
  name: string;
  description: string;
  parameters: Parameter;
}
interface Parameter {
  type: 'object';
  properties: Record<string, Property>;
  required: string[];
}
interface Property {
  type: 'string' | 'number' | 'boolean' | 'array' | 'object';
  description: string | null;
}
interface Tool {
  name: Function['name'];
  type: 'function';
  function: Function;
  run(args: Record<string, ArgumentValue | Record<string, ArgumentValue>>, chaiteContext?: ChaiteContext): Promise<string>;
}
declare class ToolDTO extends AbstractShareable<ToolDTO> {
  constructor(params: Partial<ToolDTO>);
  status: 'enabled' | 'disabled';
  permission: 'public' | 'private' | 'onetime';
  toFormatedString(_verbose?: boolean): string;
  fromString(str: string): ToolDTO;
}
/**
 * 工具组，与若干个工具关联
 */
interface ToolsGroup {
  id: string;
  name: string;
  description: string;
  toolIds: string[];
  isDefault?: boolean;
}
declare class ToolsGroupDTO extends AbstractShareable<ToolsGroupDTO> implements ToolsGroup {
  constructor(params: Partial<ToolsGroupDTO>);
  toolIds: string[];
  isDefault?: boolean | undefined;
  toFormatedString(_verbose?: boolean): string;
}
/**
 * js写的工具可以继承这个抽象类
 */
declare abstract class CustomTool implements Tool {
  constructor();
  type: "function";
  function: Function;
  run(_args: Record<string, ArgumentValue | Record<string, ArgumentValue>>, _context?: ChaiteContext): Promise<string>;
  name: Function['name'];
}
//#endregion
//#region src/types/channel.d.ts
declare class ChannelStatistics implements Serializable, DeSerializable<ChannelStatistics> {
  callTimes?: number;
  useToken?: number;
  perModel: Record<string, Omit<ChannelStatistics, 'perModel'>>;
  fromString(str: string): ChannelStatistics;
  toString(): string;
}
interface ChannelsLoadBalancer {
  getChannel(model: string, channels: Channel[]): Promise<Channel | null>;
  getChannels(model: string, channels: Channel[], totalQuantity: number): Promise<{
    channel: Channel;
    quantity: number;
  }[]>;
}
//#endregion
//#region src/types/server.d.ts
declare class ChaiteResponse<T> {
  private code;
  private data;
  private message;
  constructor(code: number, data: T, message: string);
  static ok<T>(data: T): ChaiteResponse<T>;
  static okm<T>(data: T, msg: string): ChaiteResponse<T>;
  static fail<T>(data: T, msg: string): ChaiteResponse<T>;
  static failc<T>(data: T, msg: string, code: number): ChaiteResponse<T>;
}
//#endregion
//#region src/types/trigger.d.ts
declare class TriggerDTO extends AbstractShareable<TriggerDTO> {
  constructor(params: Partial<TriggerDTO>);
  status: 'enabled' | 'disabled';
  isOneTime?: boolean;
  fromString(str: string): TriggerDTO;
  toFormatedString(verbose?: boolean): string;
}
interface Trigger {
  name: string;
  isOneTime?: boolean;
  register(context: ChaiteContext): Promise<void>;
  unregister(): Promise<void>;
  queryLLM(message: UserMessage, options: SendMessageOption & {
    chatPreset?: ChatPreset;
  }): Promise<ModelResponse>;
}
declare abstract class BaseTrigger implements Trigger {
  name: string;
  isOneTime?: boolean;
  private triggerExecuted;
  constructor(params: {
    name: string;
    isOneTime?: boolean;
  });
  protected abstract registerImpl(context: ChaiteContext): Promise<void>;
  protected abstract unregisterImpl(): Promise<void>;
  register(context: ChaiteContext): Promise<void>;
  unregister(): Promise<void>;
  protected checkOneTimeAndCleanup(): Promise<boolean>;
  protected triggerAction(action: () => Promise<void>): Promise<void>;
  queryLLM(message: UserMessage, options: SendMessageOption & {
    chatPreset?: ChatPreset;
  }): Promise<ModelResponse>;
}
declare abstract class CronTrigger extends BaseTrigger {
  protected cronExpression: string;
  protected cronJob?: Job;
  constructor(params: {
    name: string;
    description: string;
    cronExpression: string;
    isOneTime?: boolean;
  });
  protected registerImpl(context: ChaiteContext): Promise<void>;
  protected unregisterImpl(): Promise<void>;
  unregister(): Promise<void>;
  protected abstract execute(context: ChaiteContext): Promise<void>;
}
declare abstract class EventTrigger extends BaseTrigger {
  protected bot: EventEmitter;
  protected eventName: string;
  protected eventHandler: (...args: any[]) => void;
  private isRegistered;
  constructor(params: {
    id: string;
    name: string;
    description: string;
    eventName: string;
    bot: EventEmitter;
    isOneTime?: boolean;
  });
  register(context: ChaiteContext): Promise<void>;
  unregister(): Promise<void>;
  protected abstract handleEvent(...args: any[]): Promise<void>;
}
//#endregion
//#region src/const/cloud_api.d.ts
declare const CloudAPI: Record<'USER', string> & Record<'LIST' | 'ADD' | 'GET' | 'TEMP_SHARE' | 'DELETE', Record<CloudAPIType, string>>;
//#endregion
//#region src/const/map.d.ts
declare const CHANNEL_STATUS_MAP: {
  enabled: string;
  disabled: string;
};
declare const PROCESSOR_TYPE_MAP: {
  pre: string;
  post: string;
};
//#endregion
//#region src/version.d.ts
declare const VERSION = "1.10.7";
//#endregion
//#region src/controllers/index.d.ts
declare function createApp(configure?: (app: Application) => void): Application;
declare function runServer(host: string, port: number, configure?: (app: Application) => void): {
  app: Application;
  server: Server;
};
//#endregion
//#region src/agent/tool-executors/local.d.ts
/**
 * Default fallback executor: runs tools in-process via tool.run().
 * Used when no remote/sandbox executor is configured.
 */
declare class LocalToolExecutor implements ToolExecutor {
  private readonly getTools;
  constructor(getTools: () => Tool[]);
  execute(call: AgentToolCall, _ctx: ExecutionContext): Promise<ToolResult>;
  listAvailableTools(_ctx: ExecutionContext): Promise<ToolSchema[]>;
}
//#endregion
//#region src/agent/tool-executors/sandbox.d.ts
/**
 * Runs a trusted tool module in a separate child process with a restricted
 * environment and hard timeout.
 *
 * The tool entry file must accept a JSON string as its first CLI argument
 * and print its result to stdout.
 *
 * NOTE: This is NOT a security sandbox for untrusted code — it provides
 * resource isolation and timeout enforcement for trusted tool modules.
 * For untrusted code, delegate to a container-based remote sandbox.
 */
declare class ChildProcessSandbox implements SandboxExecutor {
  run(entry: string, argsJson: string, opts?: SandboxOptions): Promise<SandboxResult>;
}
/**
 * Delegates sandbox execution to a remote REST API.
 * POST { entry, argsJson, opts } → { output, exitCode, timedOut }
 *
 * Use this when the process host is too resource-constrained to run
 * certain tools locally, but a remote sandbox service is available.
 */
declare class RemoteSandboxExecutor implements SandboxExecutor {
  private readonly baseUrl;
  private readonly authHeader?;
  constructor(baseUrl: string, authHeader?: string | undefined);
  run(entry: string, argsJson: string, opts?: SandboxOptions): Promise<SandboxResult>;
}
/**
 * ToolExecutor that runs tools via a SandboxExecutor.
 * The tool must be a Node.js script that reads args from process.argv[2]
 * and writes the result to stdout.
 *
 * Requires a resolver function that maps tool name → entry file path.
 */
declare class SandboxedToolExecutor implements ToolExecutor {
  private readonly sandbox;
  private readonly resolveEntry;
  private readonly fallback?;
  constructor(sandbox: SandboxExecutor, resolveEntry: (toolName: string) => string | null, fallback?: ToolExecutor | undefined);
  execute(call: AgentToolCall, ctx: ExecutionContext): Promise<ToolResult>;
}
//#endregion
//#region src/agent/tool-executors/proxy.d.ts
/**
 * Wraps a remote tool schema (from MCP or any ToolExecutor) as an in-process Tool.
 *
 * This allows local and remote tools to coexist in the same tool list.
 * AbstractClient.fullfillTools() creates these proxies for each tool
 * discovered via toolExecutor.listAvailableTools(), then adds them to this.tools.
 * The execution loop then uniformly calls tool.run() for every tool —
 * no if/else branching needed.
 */
declare class McpToolProxy implements Tool {
  private readonly executor;
  private readonly execCtxBuilder;
  readonly name: string;
  readonly type: "function";
  readonly function: {
    name: string;
    description: string;
    parameters: Parameter;
  };
  constructor(schema: ToolSchema, executor: ToolExecutor, execCtxBuilder: () => ExecutionContext);
  run(args: Record<string, unknown>, _context?: ChaiteContext): Promise<string>;
}
//#endregion
//#region src/agent/workflow/SimpleWorkflowRunner.d.ts
/**
 * Lightweight workflow runner.
 * Supports: sequential, conditional branching, parallel (Promise.all),
 * background jobs, plan steps, and nested sub-workflows.
 */
declare class SimpleWorkflowRunner implements WorkflowEngine {
  run(workflow: WorkflowDefinition, ctx: AgentRunContext): Promise<WorkflowRunResult>;
  private runSteps;
  private runStep;
  private interpolate;
  private executeStep;
}
//#endregion
//#region src/agent/triggers/WorkflowTrigger.d.ts
/**
 * A CronTrigger that runs a WorkflowDefinition when fired.
 *
 * Usage:
 *   const trigger = new WorkflowTrigger({
 *     name: 'daily-report',
 *     cronExpression: '0 9 * * *',
 *     workflowDef: myWorkflow,
 *   })
 *   await chaite.getTriggerManager().registerTrigger(trigger)
 */
declare class WorkflowTrigger extends CronTrigger {
  private readonly workflowDef;
  private readonly runAsUserId?;
  private readonly runAsGroupId?;
  constructor(params: {
    name: string;
    cronExpression: string;
    workflowDef: WorkflowDefinition;
    isOneTime?: boolean;
    userId?: string;
    groupId?: string;
  });
  protected execute(_context: ChaiteContext): Promise<void>;
}
/**
 * A CronTrigger that resolves and runs a named Skill's workflow when fired.
 * The skill must have `executionMode: workflow` and a valid `workflowRef`.
 *
 * Usage:
 *   const trigger = new SkillWorkflowTrigger({
 *     name: 'daily-weather',
 *     cronExpression: '0 17 * * *',
 *     skillName: 'weather-report',
 *   })
 */
declare class SkillWorkflowTrigger extends CronTrigger {
  private readonly skillName;
  private readonly runAsUserId?;
  private readonly runAsGroupId?;
  constructor(params: {
    name: string;
    cronExpression: string;
    skillName: string;
    isOneTime?: boolean;
    userId?: string;
    groupId?: string;
  });
  protected execute(_context: ChaiteContext): Promise<void>;
}
//#endregion
//#region src/agent/tools/system-tools.d.ts
/**
 * Returns the set of built-in system tools to register with Chaite.
 * These tools give the LLM the ability to spawn background tasks,
 * check job status, and query plan progress — all backed by
 * structured storage rather than LLM-narrated status.
 */
declare function createSystemTools(jobManager: BackgroundJobManager, planExecutor?: PlanExecutor): Tool[];
//#endregion
//#region src/agent/tools/schedule-tools.d.ts
/**
 * System tools that let the LLM schedule, cancel, and list tasks.
 *
 * The LLM should:
 * - Convert natural language times to ISO datetime or cron expressions
 * - Call schedule_task with the structured time + goal description
 *
 * Example LLM call:
 *   schedule_task({
 *     schedule: "2024-01-15T17:00:00",
 *     goal: "查询今日天气预报",
 *     description: "下午五点天气提醒",
 *     one_shot: true
 *   })
 */
declare function createScheduleTools(scheduler: DynamicScheduler): Tool[];
//#endregion
//#region src/agent/tools/self-tools.d.ts
declare function createSelfTools(): Tool[];
//#endregion
export { AbstractClient, AbstractHistoryManager, AbstractShareable, AbstractUserModeSelector, AbstractVectorDatabase, AgentRunContext, AgentToolCall, ApiError, ArgumentValue, AssistantMessage, AudioContent, AuditEmitter, AuditEvent, AuditEventType, BackgroundJob, BackgroundJobManager, BackgroundJobManagerOptions, BackgroundJobSpec, BackgroundStep, BaseClientOptions, BaseTrigger, BasicStorage, CHANNEL_STATUS_MAP, Chaite, ChaiteContext, ChaiteResponse, ChaiteStorage, Channel, ChannelStatistics, ChannelsLoadBalancer, ChannelsManager, ChatPreset, ChatPresetManager, ChildProcessSandbox, ClaudeClient, ClientType, CloudAPI, CloudAPIResponse, CloudAPIType, CloudModel, CloudSharingService, ConditionStep, Conversation, CronTrigger, CustomConfig, CustomConfigValueType, CustomTool, DEFAULT_HOST, DEFAULT_PORT, DeSerializable, DefaultChannelLoadBalancer, DefaultCloudService, DefaultLogger, DeveloperMessage, DocumentFileParser, DocumentPathParser, DocxBufferParser, DocxFileParser, DynamicScheduler, DynamicSchedulerOptions, EmbeddingOption, EmbeddingResult, EventMessage, EventTrigger, ExecutableSShareableType, ExecutableShareableManager, ExecutionContext, ExecutionMode, Feature, FilePath, Filter, From, FrontEndAuthHandler, Function, FunctionCall, GeminiClient, GeminiClientOptions, GlobalConfig, History, HistoryManager, HistoryMessage, HttpClient, HttpClientOptions, IBackgroundJobManager, IClient, ILogger, IMessage, ImageContent, InMemoryHistoryManager, Into, JobCompletePayload, JobExecutorFn, JobProgressPayload, JobStatus, KVHistoryManager, KVStore, ListLogsFilter, LlmPlanReviewer, LlmPlanner, LlmStep, LocalToolExecutor, LogsPage, LogsStats, McpEndpoint, McpServerConfig, McpServerManager, McpToolExecutor, McpToolProxy, MessageContent, ModelConfig, ModelResponse, ModelResponseChunk, ModelUsage, MultipleKeyStrategy, MultipleKeyStrategyChoice, NonExecutableSShareableType, NonExecutableShareableManager, NoopPlanReviewer, OnError, OpenAIClient, OpenAIClientOptions, OperationLog, OperationLogLevel, OperationLogManager, OperationLogType, PROCESSOR_TYPE_MAP, PaginationResult, ParallelStep, Parameter, Parser, ParserType, PdfBufferParser, PdfFileParser, type Plan, PlanExecutor, type PlanExecutorOptions, type PlanProgress, type PlanReviewer, type PlanStatus, type PlanStep, type PlanStepStatus, type Planner, PostProcessor, PreProcessor, Processor, ProcessorDTO, ProcessorsManager, Property, PureTextBufferParser, PureTextFileParser, RAGManager, RAGResult, ReasoningContent, ReasoningPart, RemoteSandboxExecutor, type ReviewResult, Role, SandboxExecutor, SandboxOptions, SandboxResult, SandboxedToolExecutor, ScheduledTask, SearchOption, SendMessageOption, Serializable, Shareable, SimpleWorkflowRunner, Skill, SkillFrontmatter, SkillMeta, SkillRegistry, SkillWorkflowTrigger, SubworkflowStep, SystemMessage, TextContent, TextProcessor, Tool, ToolCall, ToolCallLimitConfig, ToolCallResult, ToolCallResultMessage, ToolChoice, ToolDTO, ToolExecutor, ToolManager, ToolResult, ToolSchema, ToolStep, ToolsGroup, ToolsGroupDTO, ToolsGroupManager, Trigger, TriggerDTO, TriggerManager, User, UserMessage, UserModeSelector, UserSettings, UserState, VERSION, ValidExtension, VectorDatabase, Vectorizer, VectorizerImpl, Wait, WorkflowDefinition, WorkflowEngine, WorkflowOnComplete, WorkflowRunResult, WorkflowStep, WorkflowStepBase, WorkflowStepResult, WorkflowStepType, WorkflowTrigger, asyncLocalStorage, createApp, createClient, createHttpClient, createScheduleTools, createSelfTools, createSystemTools, extractClassName, getKey, getLogger, getPlanProgress, runServer, saveAndLoadModule, useEvent };