/** * SPI Store 接口定义 * * 从 @cicctencent/agent-midway 迁移至 @cicctencent/agent-server。 * 这些接口定义了数据存储抽象,上层业务应用通过实现这些接口 * 来对接自己的 DB(TypeORM/Prisma 等)。 * * 设计原则: * - 接口只定义业务语义,不包含 TypeORM / SQL 细节 * - agent-server 不定义或导出 DB Entity / Model * - 上层应用通过 MidwayJS IoC 绑定接口到具体实现 * * 使用方式(上层应用): * ```ts * @Provide(SPI_BINDINGS.AgentProfileStore) * export class AgentProfileStoreImpl implements AgentProfileStore { * // 使用 TypeORM Repository 实现 * } * ``` */ export interface BaseRecord { id?: number; createTime?: Date; modifyTime?: Date; creator?: string; updater?: string; [key: string]: any; } export interface AgentProfileRecord extends BaseRecord { applicationId?: number; name?: string; basePrompt?: string; agentType?: string; domain?: string; mcpServers?: (number | string)[] | null; selectedSkills?: string[] | null; alwaysInjectSkills?: string[] | null; disabledTools?: string[] | null; isDefault?: number; /** Agent 开关配置 JSON(统一存储所有布尔开关,如 chatEnabled/canDelegate/allowInteraction) */ agentOptions?: Record | null; /** 是否可用于对话(API 层已归一化为 boolean) */ chatEnabled?: boolean; /** 是否支持委派子 Agent(API 层已归一化为 boolean) */ canDelegate?: boolean; /** 是否支持被其它 Agent 委派为子 Agent(作为 delegate_task 候选) */ canBeDelegated?: boolean; /** 是否允许 Agent 中断并向用户提问(API 层已归一化为 boolean) */ allowInteraction?: boolean; } export interface ApplicationRecord extends BaseRecord { key?: string; name?: string; status?: string; isDefault?: number; } export interface MCPServerRecord extends BaseRecord { applicationId?: number; name?: string; transport?: string; endpoint?: string; command?: string; enabled?: number; status?: string; toolDefinitions?: any[] | null; /** MCP 调用超时(毫秒),0/null 表示使用默认 30000 */ timeout?: number | null; } export interface ModelConfigRecord extends BaseRecord { applicationId?: number; provider?: string; model?: string; apiKey?: string; baseUrl?: string; temperature?: number; maxTokens?: number; timeout?: number; extraConfig?: Record | null; enabled?: number; tier?: string; role?: string; isDefault?: number; } export interface ChatWorkspaceRecord extends BaseRecord { /** 工作空间所属用户 */ userId?: string; applicationId?: number; name?: string; description?: string; /** 是否为默认工作空间 */ isDefault?: number; sort?: number; } export interface ChatThreadRecord extends BaseRecord { /** 工作空间 ID(可选:不使用工作空间的业务可留空) */ workspaceId?: number; applicationId?: number; userId?: string; title?: string; status?: string; isDeleted?: number; /** 父线程ID(会话分叉来源,0/未定义表示根线程) */ parentThreadId?: number; /** 分叉点消息ID(在该消息之后分叉,0 表示从头分叉) */ forkFromMessageId?: number; } export interface ChatMessageRecord extends BaseRecord { applicationId?: number; userId?: string; threadId?: number; role: string; content: string; type?: string; meta?: any; isDeleted?: number; } export interface KnowledgeBaseRecord extends BaseRecord { applicationId?: number; name?: string; status?: string; enabled?: number; } export interface KBDocumentRecord extends BaseRecord { knowledgeBaseId?: number; title?: string; content?: string | null; status?: string; meta?: Record | null; } export interface AgentProfileStore { list(applicationId: number, options?: { includeDisabled?: boolean; }): Promise; get(id: number, applicationId?: number): Promise; getDefault(applicationId: number): Promise; create(applicationId: number, data: Partial): Promise; update(id: number, applicationId: number, data: Partial): Promise; delete(id: number, applicationId: number): Promise; setDefault(id: number, applicationId: number): Promise; /** 获取 Profile 关联的 MCP Server 列表 */ getMCPServers?(id: number, applicationId: number): Promise; } export interface ApplicationStore { list(options?: { status?: string; }): Promise; get(id: number): Promise; getByKey(key: string): Promise; getDefault(): Promise; create(data: Partial): Promise; update(id: number, data: Partial): Promise; delete(id: number): Promise; setDefault(id: number): Promise; archive(id: number): Promise; activate(id: number): Promise; resolveApp(idOrKey: number | string): Promise; /** 获取应用关联的 Agent Profile 列表 */ getAgentProfiles?(idOrKey: number | string): Promise<{ application: ApplicationRecord; agentProfiles: any[]; defaultProfile: any; } | null>; } export interface MCPServerStore { list(applicationId: number, options?: { enabled?: boolean; status?: string; }): Promise; get(id: number, applicationId?: number): Promise; getEnabled(applicationId: number): Promise; /** 获取所有已启用的 MCP Server(跨应用,用于运行时工具缓存) */ getAllEnabled?(): Promise; create(applicationId: number, data: Partial): Promise; update(id: number, applicationId: number, data: Partial): Promise; delete(id: number, applicationId: number): Promise; enable(id: number, applicationId: number): Promise; disable(id: number, applicationId: number): Promise; updateStatus(id: number, applicationId: number, status: 'connected' | 'disconnected' | 'error', error?: string): Promise; updateToolDefinitions(id: number, applicationId: number, tools: any[]): Promise; getTools(id: number, applicationId: number): Promise; getToolsByServerIds(applicationId: number, ids: (number | string)[]): Promise; } export interface ModelConfigStore { list(applicationId: number, options?: { enabled?: boolean; tier?: string; role?: string; }): Promise; get(id: number, applicationId?: number): Promise; getDefault(applicationId: number): Promise; getPrimary(applicationId: number): Promise; getFallbackChain(applicationId: number, role?: string): Promise<{ primary: ModelConfigRecord | null; fallbacks: ModelConfigRecord[]; roleSpecific: ModelConfigRecord[]; }>; create(applicationId: number, data: Partial): Promise; update(id: number, applicationId: number, data: Partial): Promise; delete(id: number, applicationId: number): Promise; setDefault(id: number, applicationId: number): Promise; enable(id: number, applicationId: number): Promise; disable(id: number, applicationId: number): Promise; getByRole(applicationId: number, role: string): Promise; getAvailableConfig(applicationId: number, role?: string): Promise; } export interface ChatStore { getWorkspaces?(userId: string, applicationId: number): Promise; getWorkspace?(workspaceId: number, userId: string, applicationId: number): Promise; createWorkspace?(userId: string, applicationId: number, data: { name: string; description?: string; }): Promise; updateWorkspace?(workspaceId: number, userId: string, applicationId: number, data: Partial): Promise; deleteWorkspace?(workspaceId: number, userId: string, applicationId: number): Promise; /** 确保用户存在默认工作空间,返回所有工作空间(仅在工作空间模式下需要) */ ensureDefaultWorkspaces?(userId: string, applicationId: number): Promise; getThreads(userId: string, applicationId: number, workspaceId?: number, limit?: number, offset?: number, parentId?: number): Promise; getThreadCount?(userId: string, applicationId: number, workspaceId?: number, parentId?: number): Promise; getThread(id: number, userId: string, applicationId: number, workspaceId?: number): Promise; createThread(userId: string, applicationId: number, data: { title?: string; tenantId?: number; }, workspaceId?: number): Promise; updateThread(id: number, userId: string, applicationId: number, data: Partial, workspaceId?: number): Promise; deleteThread(id: number, userId: string, applicationId: number, workspaceId?: number): Promise; /** 会话分叉:从指定消息处分叉出新线程,复制前缀消息,父线程置为只读(forked) */ forkThread?(threadId: number, userId: string, applicationId: number, options?: { forkFromMessageId?: number; title?: string; }, workspaceId?: number): Promise; /** 暂停会话(status='paused' + pausedAt + pauseReason)。可选能力。 */ setPaused?(threadId: number, userId: string, applicationId: number, reason?: string): Promise; /** 恢复暂停的会话(status='active' + 清除 pausedAt/pauseReason)。可选能力。 */ clearPaused?(threadId: number, userId: string, applicationId: number): Promise; getMessages(threadId: number, userId: string, applicationId: number, options?: { limit?: number; offset?: number; }, workspaceId?: number): Promise<{ items: ChatMessageRecord[]; total: number; hasMore: boolean; }>; addMessage(userId: string, applicationId: number, data: { threadId: number; role: string; content: string; type?: string; meta?: any; }, workspaceId?: number): Promise; updateMessage(id: number, userId: string, applicationId: number, data: { content?: string; meta?: any; }, workspaceId?: number): Promise; deleteMessage(id: number, userId: string, applicationId: number, workspaceId?: number): Promise; clearThreadMessages(threadId: number, userId: string, applicationId: number, workspaceId?: number): Promise; searchMessages(query: string, options?: { userId?: string; applicationId?: number; limit?: number; }): Promise>; } export interface KnowledgeBaseStore { list(applicationId: number, options?: { enabled?: boolean; status?: string; }): Promise; get(id: number, applicationId?: number): Promise; create(applicationId: number, data: Partial): Promise; update(id: number, applicationId: number, data: Partial): Promise; delete(id: number, applicationId: number): Promise; enable(id: number, applicationId: number): Promise; disable(id: number, applicationId: number): Promise; getDocuments(knowledgeBaseId: number): Promise; deleteDocument(documentId: number): Promise; createDocument(data: Partial): Promise; updateDocument(id: number, data: Partial): Promise; countDocuments(knowledgeBaseId: number): Promise; } export interface AutomationEntity { id: number; name: string; description?: string; type: string; config: any; status: string; schedule?: string; prompt?: string; runtime?: 'prompt' | 'nodejs' | string; codePackage?: import('@cicctencent/agent-core').NodeAutomationTaskPackage; version?: string; agent_profile_id?: number; } export interface AutomationRunRecord { automationId: number; status: string; triggerType: string; prompt: string; result: string; errors: string[]; startedAt: Date; finishedAt: Date; duration: number; runtime?: string; stdout?: string; stderr?: string; exitCode?: number; logs?: import('@cicctencent/agent-core').NodeAutomationLogEntry[]; artifacts?: string[]; } export interface AutomationStore { /** 查询所有活跃任务(用于调度) */ listActive(): Promise; /** 查询所有未删除任务(用于列表展示) */ list(options?: { type?: string; status?: string; }): Promise; /** 获取单个任务 */ getById(id: number): Promise; /** 创建任务 */ create(data: Partial): Promise; /** 更新任务 */ update(id: number, data: Partial): Promise; /** 删除任务(软删除) */ delete(id: number): Promise; /** 更新任务状态和最近运行结果 */ updateStatus(id: number, status: string, result?: string): Promise; /** 保存运行记录 */ saveRunRecord(record: AutomationRunRecord): Promise; /** 查询运行历史 */ getRunHistory(automationId: number, options?: { limit?: number; offset?: number; }): Promise; } export interface MemoryDBStore { save(entry: { userId: string; threadId: string | number; content: string; category: string; relevance?: number; expiresAt?: number; meta?: any; }): Promise; getByThread(threadId: string | number, limit?: number): Promise; getByUser(userId: string, category?: string, limit?: number): Promise; search(userId: string, query: string, topK?: number): Promise; delete(id: string | number): Promise; clearThread(threadId: string | number): Promise; load(userId: string, options?: { threadId?: number | string; category?: string; limit?: number; }): Promise; clear(userId: string): Promise; } /** 模型供应商配置 */ export interface ModelProviderConfig { id: string; name?: string; provider?: string; model?: string; apiKey?: string; baseUrl?: string; temperature?: number; maxTokens?: number; extraConfig?: Record; } /** 后台任务记录(LLM `background:true` 工具调用或 AgentTaskQueue 长时任务) */ export interface BackgroundTaskRecord extends BaseRecord { /** 任务唯一标识(uuid) */ taskId: string; applicationId: number; tenantId?: number; userId?: string; /** 关联线程ID(0 表示非会话来源) */ threadId?: number; /** 来源:agent_call(LLM后台调用)/ queue(队列长时任务) */ source?: string; /** 任务标题(面板展示用) */ title?: string; /** 工具名(agent_call 来源时为 LLM 调用的工具名) */ toolName?: string; /** 工具入参 JSON(序列化) */ toolInput?: string | null; /** 状态:pending/running/completed/failed/cancelled/interrupted */ status: string; /** 执行结果(completed 时为工具输出) */ result?: string | null; /** 失败原因 */ error?: string | null; /** 创建时间 */ createdAt?: Date; /** 结束时间 */ finishedAt?: Date | null; } export interface BackgroundTaskStore { /** 创建任务记录 */ create(data: Partial): Promise; /** 按 taskId 查询 */ getByTaskId(taskId: string): Promise; /** 按 threadId 查询所有任务(按 createdAt 倒序) */ listByThread(threadId: number, options?: { limit?: number; offset?: number; }): Promise<{ items: BackgroundTaskRecord[]; total: number; }>; /** 按 userId 查询任务 */ listByUser(userId: string, options?: { applicationId?: number; limit?: number; offset?: number; }): Promise<{ items: BackgroundTaskRecord[]; total: number; }>; /** 更新任务(状态/结果/错误/finishedAt) */ updateByTaskId(taskId: string, data: Partial): Promise; /** 查询未完成任务(pending/running),用于进程重启恢复 */ listUnfinished(): Promise; /** 删除任务(物理删除) */ deleteByTaskId(taskId: string): Promise; /** 确保表结构存在(幂等建表/补列) */ ensureSchema?(): Promise; } /** 全局设置存储接口 */ export interface SettingsStore { /** 获取设置数据 */ getSettingsData(applicationId?: number): Promise<{ theme: string; language: string; apiKeys: Record; notifications: Record; modelProviders: ModelProviderConfig[]; defaultModelId: string | null; proxySettings: Record; agentConfig: Record; securityPolicy: Record; }>; /** 获取默认模型配置(用于 Agent 运行时) */ getDefaultModelConfig(applicationId?: number): Promise; } export declare const SPI_BINDINGS: { readonly AgentProfileStore: "AgentProfileStore"; readonly ApplicationStore: "ApplicationStore"; readonly MCPServerStore: "MCPServerStore"; readonly ModelConfigStore: "ModelConfigStore"; readonly ChatStore: "ChatStore"; readonly KnowledgeBaseStore: "KnowledgeBaseStore"; readonly AutomationStore: "AutomationStore"; readonly MemoryDBStore: "MemoryDBStore"; readonly BackgroundTaskStore: "BackgroundTaskStore"; readonly SettingsStore: "SettingsStore"; }; //# sourceMappingURL=store-interfaces.d.ts.map