/** * AI Tool 的 schema 类型(与 ai_agent.constant 中的定义保持一致, * 但不直接引用内部模块,以避免循环依赖) */ export interface AiToolSchema { type: string; function: { name: string; description: string; parameters: { type: string; properties: any; required: string[]; }; }; } export interface plug_item { path: string; note?: string; open: boolean; name: string; params?: string; } export interface plug_css_item { label: string; path: string; } /** * 插件元信息 */ export interface PluginMeta { /** 插件唯一标识 */ id: string; /** 插件名称[cite: 4] */ name: string; /** 插件版本[cite: 4] */ version: string; /** 插件类型[cite: 4] */ /** 插件描述[cite: 4] */ description?: string; /** 作者[cite: 4] */ author?: string; } /** * 宿主程序提供的核心 API 上下文[cite: 4] */ export interface PluginContext { env: { port: number; work_dir: string; version: string; }; params: { [key: string]: any; }; /** * 控制 workflow 执行 * @param file_path - workflow 文件(.act)的绝对路径 * @param action - start 开始执行;stop 停止该文件正在运行的实例 * @param user_id - 执行身份的用户 id。不填则使用 root 用户 * @returns 成功返回 true,失败抛出异常(如文件不存在、无权限、已在运行) */ workflow_act: (file_path: string, action: 'start' | 'stop', user_id?: string) => Promise; } /** * 插件自定义路由定义 * * 共支持三种类型(用 type 区分,不填默认 api): * - api :自定义 API 路由,handler 直接处理请求(等价系统的「自定义 api 路由」) * - resource :页面资源路由,把本地目录或 http 地址映射到一个 URL 前缀(等价「页面资源路由」) * * 匹配方式都是 router 前缀匹配(startsWith),同一前缀取最长匹配,因此 * /api/foo/bar 会优先命中 /api/foo 而不是 /api。 * * workflow 触发不提供专用路由类型,插件在 api 路由的 handler 里自行调用 * context.workflow_act() 控制执行,这样触发条件、鉴权、返回值都由插件自己决定。 */ export type PluginRouteType = 'api' | 'resource'; /** 插件自定义路由 定义 */ export interface PluginRoute { /** 路由类型,默认 'api' */ type?: PluginRouteType; /** 路由前缀,任意路径都可以(不再限定 /api 开头),例如 /plugin/my-route */ router: string; /** 是否需要鉴权,默认 true */ needAuth?: boolean; /** * 路由处理函数 * 仅 type === 'api' 时使用。 * @param req - Express Request 对象,插件自己读取 body、headers 等 * @param res - Express Response 对象,插件自己控制响应 * @returns 返回的内容会作为 HTTP 响应发送,返回 null/undefined 则不自动发送(由插件自己通过 res 发送) */ handler?: (req: import('express').Request, res: import('express').Response) => Promise; /** * 资源路径 * 仅 type === 'resource' 时使用。 * 填本地绝对路径(目录,访问时会拼接 router 之后的剩余路径)或 http(s) 地址(做反向代理)。 */ location?: string; /** 浏览器缓存时间(秒)。仅 type === 'resource' 且 location 为本地目录时生效 */ cache_length?: number; } /** * 插件定义[cite: 4] 可以默认导出 */ export interface Plugin { /** 插件元信息[cite: 4] */ readonly meta: PluginMeta; /** 插件激活时调用,并注入宿主上下文[cite: 4] */ activate(context: PluginContext): void | Promise; /** 插件停用时调用[cite: 4] */ deactivate?(): void | Promise; /** * AI 工具定义列表 * 每个工具包含 schema(给 LLM 的描述)和执行函数 */ tools?: AiToolItem[]; css_list?: plug_css_item[]; /** * 插件自定义 HTTP API 路由列表 * 功能等同于系统的「自定义 API 路由」,handler 直接拿到 Request/Response */ routes?: PluginRoute[]; /** * 【登录鉴权回调】替代旧版的「自定义登录 auth」功能。 * 用户登录时(密码校验之前)被调用: * - 返回 true :放行登录,并为该用户签发 token(拥有用户全部权限) * - 返回 false 或抛错:继续走系统默认的密码登录 * 注意:多个插件同时注册时,任一插件返回 true 即视为通过。 */ login_auth?: (headers: any, req: import('express').Request) => Promise | boolean; /** * 【shell 命令校验回调】替代旧版的「自定义 shell 命令校验」功能。 * 用户执行 shell 命令前被调用,返回值语义(与 exec_type 一致): * -1 拒绝执行(reject) * 0 使用 child_process 执行 * 1 使用 node-pty 执行 * 2 不拦截,继续交给系统默认判断 * 注意:多个插件同时注册时,按注册顺序依次调用,第一个不是「继续(2)」的返回值即生效。 */ shell_cmd_check?: (token: string, cmd: string, params: any[]) => Promise | number; /** * 【日志回调】项目所有日志输出(console.log/info/warn/error/debug)都会调用它。 * 插件可借此收集/转发系统日志。 * @param level 日志级别:'log' | 'info' | 'warn' | 'error' | 'debug' * @param args 原始日志参数(调用 console.xxx 时传入的参数列表) */ on_log?: (level: PluginLogLevel, args: any[]) => void; /** * 【HTTP 请求回调】所有 HTTP 请求处理完成后都会调用它。 * 仅当请求携带有效登录 token 时才会带上 user 信息; * 未登录 / 登录信息拿不到时 user 为 null。 * @param info 请求与响应信息 */ on_http?: (info: PluginHttpInfo) => void; } /** 日志级别 */ export type PluginLogLevel = 'log' | 'info' | 'warn' | 'error' | 'debug'; /** * 传给插件 on_http 回调的请求信息。 * user 为「安全用户数据」(不含密码等敏感字段),未登录时为 null。 * * 除了下面这些便捷字段,插件还可以直接访问原始的 Express 请求/响应对象: * - info.req:完整的 Request(query、body、params、cookies、所有 header、 * protocol、hostname、ip、socket、httpVersion、get() 等全都能拿到) * - info.res:完整的 Response(已经发送的 header、cookie、statusCode 等) * 也就是说 HTTP 的「所有东西」都在这里,便捷字段只是为了省去手写解析。 */ export interface PluginHttpInfo { /** 原始 Express 请求对象(HTTP 请求的全部信息都在这里) */ req: any; /** 原始 Express 响应对象(响应头、cookie、状态码等都在这里) */ res: any; /** 请求方法,如 GET / POST(等价 req.method) */ method: string; /** 请求路径(含查询串),等价 req.originalUrl */ url: string; /** 请求路径(不含查询串) */ path: string; /** 客户端 IP */ ip: string; /** 请求头,等价 req.headers */ headers: any; /** 查询参数,等价 req.query */ query: any; /** 路由参数,等价 req.params */ params: any; /** 请求体(POST/PUT 等);部分接口在插件回调时可能还未被 body-parser 解析 */ body: any; /** URL 查询串,等价 req.url 中 ? 之后的部分 */ search: string; /** 协议,如 http / https,等价 req.protocol */ protocol: string; /** 主机名,等价 req.hostname */ hostname: string; /** HTTP 协议版本,如 1.1 */ httpVersion: string; /** HTTP 响应状态码,等价 res.statusCode */ status: number; /** 处理耗时(毫秒) */ duration: number; /** 登录用户信息(安全字段,不含密码),未登录为 null */ user: any | null; } /** * AI 工具项 */ export interface AiToolItem { /** 工具 schema,用于向 LLM 描述工具(OpenAI function calling 格式) */ schema: AiToolSchema; /** 工具执行函数,接收参数并返回结果 */ handler: (args: any) => Promise; } export declare const run_test: () => void;