@marswave/cola-plugin-sdk
Advanced tools
@@ -1,2 +0,2 @@ | ||
| export { type Plugin, type PluginMeta, type PluginLogger, type PluginErrorOrigin, type PluginErrorClassification, type PluginRuntime, type PluginEvents, type PluginSessionEvent, type PluginEventHandler, type SessionId, type ChannelSender, type InboundDeliveryContext, type DeliveryContext, type PluginCommandContext, type PluginCommandResult, type PluginCommandDefinition, type ChannelDefinition, type ChannelMeta, type ChannelCapabilities, type ChannelGatewayAdapter, type ChannelStatusResult, type GatewayContext, type ChannelOutboundAdapter, type OutboundContext, type ReactionAction, type ReactionContext, type ChannelAuthAdapter, type AuthContext, type ChannelConfigAdapter, type ChannelConfigField, type ChannelConfigFieldOption, type ChannelConfigSchema, type ChannelAgentPromptAdapter, type ChannelAgentToolAdapter, type ChannelAgentTool, type PromptContext, type ToolContext, type DeliverPayload, type DeliverFn, type SttLanguage, type SttStream, type PluginStt, type TtsLanguage, type PluginTts } from './types.js'; | ||
| export { type Plugin, type PluginMeta, type PluginLogger, type PluginErrorOrigin, type PluginErrorClassification, type PluginRuntime, type PluginEvents, type PluginSessionEvent, type PluginEventHandler, type SessionId, type ChannelSender, type InboundDeliveryContext, type DeliveryContext, type PluginCommandContext, type PluginCommandResult, type PluginCommandDefinition, type ChannelDefinition, type ChannelMeta, type ChannelCapabilities, type ChannelGatewayAdapter, type ChannelStatusResult, type GatewayContext, type ChannelOutboundAdapter, type OutboundContext, type ReactionAction, type ReactionContext, type ChannelAuthAdapter, type AuthContext, type ChannelConfigAdapter, type ChannelConfigField, type ChannelConfigFieldOption, type ChannelConfigSchema, type ChannelAgentPromptAdapter, type ChannelAgentTool, type ChannelAgentToolContext, type ChannelAgentToolResult, type PluginTool, type PluginToolScope, type PluginToolContext, type PluginToolResult, type PluginStartContext, type PluginEventScope, type PluginEventSubscribeOptions, type PluginSessionEventOrigin, type PluginToolContent, type PromptContext, type DeliverPayload, type DeliverFn, type SttLanguage, type SttStream, type PluginStt, type TtsLanguage, type PluginTts } from './types.js'; | ||
| export { createPollLoop, withBackoff } from './helpers.js'; | ||
@@ -3,0 +3,0 @@ import type { Plugin, ChannelDefinition, PluginCommandDefinition } from './types.js'; |
@@ -33,5 +33,47 @@ export type PluginErrorOrigin = 'app' | 'infra' | 'agent' | 'unknown'; | ||
| }; | ||
| export type PluginToolContent = { | ||
| type: 'text'; | ||
| text: string; | ||
| } | { | ||
| type: 'image'; | ||
| source: { | ||
| type: 'base64'; | ||
| mediaType: string; | ||
| data: string; | ||
| }; | ||
| }; | ||
| export type PluginSessionLifecycleReason = 'startup' | 'reload' | 'new' | 'resume' | 'fork'; | ||
| export type PluginAssistantStopReason = 'stop' | 'length' | 'toolUse' | 'error' | 'aborted'; | ||
| export type PluginEventScope = 'self' | 'all'; | ||
| export type PluginEventSubscribeOptions = { | ||
| /** 'self' (default) delivers events only for sessions under this plugin's own channel scope. 'all' delivers every session's events (including desktop, CLI, and other plugins). */ | ||
| scope?: PluginEventScope; | ||
| }; | ||
| export type PluginSessionEventOrigin = { | ||
| kind: 'plugin'; | ||
| pluginId: string; | ||
| } | { | ||
| kind: 'desktop'; | ||
| } | { | ||
| kind: 'cli'; | ||
| user: string; | ||
| } | { | ||
| kind: 'other'; | ||
| scopeKey: string; | ||
| }; | ||
| export type PluginSessionEvent = { | ||
| type: 'session:start'; | ||
| sessionId: SessionId; | ||
| origin: PluginSessionEventOrigin; | ||
| reason: PluginSessionLifecycleReason; | ||
| previousSessionFile?: string; | ||
| } | { | ||
| type: 'session:shutdown'; | ||
| sessionId: SessionId; | ||
| origin: PluginSessionEventOrigin; | ||
| reason: 'quit' | 'reload' | 'new' | 'resume' | 'fork'; | ||
| } | { | ||
| type: 'compact:start'; | ||
| sessionId: SessionId; | ||
| origin: PluginSessionEventOrigin; | ||
| promptId?: string; | ||
@@ -41,2 +83,3 @@ } | { | ||
| sessionId: SessionId; | ||
| origin: PluginSessionEventOrigin; | ||
| promptId?: string; | ||
@@ -46,2 +89,74 @@ aborted?: boolean; | ||
| willRetry?: boolean; | ||
| } | { | ||
| type: 'agent:start'; | ||
| sessionId: SessionId; | ||
| origin: PluginSessionEventOrigin; | ||
| } | { | ||
| type: 'agent:end'; | ||
| sessionId: SessionId; | ||
| origin: PluginSessionEventOrigin; | ||
| } | { | ||
| type: 'turn:start'; | ||
| sessionId: SessionId; | ||
| origin: PluginSessionEventOrigin; | ||
| turnIndex: number; | ||
| timestamp: number; | ||
| } | { | ||
| type: 'turn:end'; | ||
| sessionId: SessionId; | ||
| origin: PluginSessionEventOrigin; | ||
| turnIndex: number; | ||
| } | { | ||
| type: 'message:start'; | ||
| sessionId: SessionId; | ||
| origin: PluginSessionEventOrigin; | ||
| } | { | ||
| type: 'message:update'; | ||
| sessionId: SessionId; | ||
| origin: PluginSessionEventOrigin; | ||
| deltaText: string; | ||
| } | { | ||
| type: 'message:end'; | ||
| sessionId: SessionId; | ||
| origin: PluginSessionEventOrigin; | ||
| text: string; | ||
| stopReason?: PluginAssistantStopReason; | ||
| } | { | ||
| type: 'tool:call'; | ||
| sessionId: SessionId; | ||
| origin: PluginSessionEventOrigin; | ||
| toolName: string; | ||
| toolCallId: string; | ||
| input: Record<string, unknown>; | ||
| } | { | ||
| type: 'tool:result'; | ||
| sessionId: SessionId; | ||
| origin: PluginSessionEventOrigin; | ||
| toolName: string; | ||
| toolCallId: string; | ||
| input: Record<string, unknown>; | ||
| content: PluginToolContent[]; | ||
| details: unknown; | ||
| isError: boolean; | ||
| } | { | ||
| type: 'tool:execution_start'; | ||
| sessionId: SessionId; | ||
| origin: PluginSessionEventOrigin; | ||
| toolName: string; | ||
| toolCallId: string; | ||
| input: Record<string, unknown>; | ||
| } | { | ||
| type: 'tool:execution_update'; | ||
| sessionId: SessionId; | ||
| origin: PluginSessionEventOrigin; | ||
| toolName: string; | ||
| toolCallId: string; | ||
| partial: unknown; | ||
| } | { | ||
| type: 'tool:execution_end'; | ||
| sessionId: SessionId; | ||
| origin: PluginSessionEventOrigin; | ||
| toolName: string; | ||
| toolCallId: string; | ||
| isError: boolean; | ||
| }; | ||
@@ -52,3 +167,3 @@ export type PluginEventHandler<T extends PluginSessionEvent['type']> = (event: Extract<PluginSessionEvent, { | ||
| export type PluginEvents = { | ||
| on<T extends PluginSessionEvent['type']>(type: T, handler: PluginEventHandler<T>): () => void; | ||
| on<T extends PluginSessionEvent['type']>(type: T, handler: PluginEventHandler<T>, options?: PluginEventSubscribeOptions): () => void; | ||
| }; | ||
@@ -221,7 +336,31 @@ export type PluginMeta = { | ||
| }; | ||
| /** @deprecated Use `PluginToolContext`. */ | ||
| export type ChannelAgentToolContext = { | ||
| sessionId: SessionId; | ||
| toolCallId: string; | ||
| signal: AbortSignal; | ||
| logger: PluginLogger; | ||
| config: Readonly<Record<string, unknown>>; | ||
| onUpdate?: (partial: { | ||
| content: PluginToolContent[]; | ||
| }) => void; | ||
| }; | ||
| /** @deprecated Use `PluginToolResult`. */ | ||
| export type ChannelAgentToolResult = { | ||
| content: PluginToolContent[]; | ||
| details?: unknown; | ||
| isError?: boolean; | ||
| }; | ||
| /** @deprecated Use `PluginTool` and declare it on `Plugin.tools[]`. */ | ||
| export type ChannelAgentTool = { | ||
| name: string; | ||
| label?: string; | ||
| description: string; | ||
| /** JSON Schema for tool parameters (typebox schemas are JSON Schema at runtime). */ | ||
| parameters: Record<string, unknown>; | ||
| execute(input: unknown): Promise<unknown>; | ||
| /** Optional one-line entry in the system prompt's Available tools section. */ | ||
| promptSnippet?: string; | ||
| /** Optional guideline bullets appended to the system prompt's Guidelines section. */ | ||
| promptGuidelines?: string[]; | ||
| execute(input: unknown, ctx: ChannelAgentToolContext): Promise<ChannelAgentToolResult>; | ||
| }; | ||
@@ -256,7 +395,46 @@ export type ChannelDefinition<State = unknown> = { | ||
| }; | ||
| export type PluginToolScope = 'global' | 'own-channel'; | ||
| export type PluginToolContext = { | ||
| /** Plugin sessionId tuple when the call originates from a plugin scope, otherwise undefined. */ | ||
| sessionId: SessionId | undefined; | ||
| /** Full internal scopeKey (e.g. "plugin:slack:dm:T123:U1", "desktop:local", "cli:mack"). */ | ||
| scopeKey: string; | ||
| toolCallId: string; | ||
| signal: AbortSignal; | ||
| logger: PluginLogger; | ||
| config: Readonly<Record<string, unknown>>; | ||
| onUpdate?: (partial: { | ||
| content: PluginToolContent[]; | ||
| }) => void; | ||
| }; | ||
| export type PluginToolResult = { | ||
| content: PluginToolContent[]; | ||
| details?: unknown; | ||
| isError?: boolean; | ||
| }; | ||
| export type PluginTool = { | ||
| name: string; | ||
| label?: string; | ||
| description: string; | ||
| parameters: Record<string, unknown>; | ||
| /** Defaults to 'global'. Setting 'own-channel' requires `Plugin.channel` to be present. */ | ||
| scope?: PluginToolScope; | ||
| promptSnippet?: string; | ||
| promptGuidelines?: string[]; | ||
| execute(input: unknown, ctx: PluginToolContext): Promise<PluginToolResult>; | ||
| }; | ||
| export type PluginStartContext = { | ||
| config: Readonly<Record<string, unknown>>; | ||
| runtime: PluginRuntime; | ||
| logger: PluginLogger; | ||
| abortSignal: AbortSignal; | ||
| }; | ||
| export type Plugin<State = unknown> = { | ||
| id: string; | ||
| meta: PluginMeta; | ||
| start?(ctx: PluginStartContext): Promise<void> | void; | ||
| stop?(ctx: PluginStartContext): Promise<void> | void; | ||
| channel?: ChannelDefinition<State>; | ||
| tools?: PluginTool[]; | ||
| commands?: PluginCommandDefinition[]; | ||
| }; |
+17
-5
| { | ||
| "name": "@marswave/cola-plugin-sdk", | ||
| "private": false, | ||
| "version": "0.0.1", | ||
| "description": "Local link target for cola plugins under development (Cola feat/connections SDK source).", | ||
| "version": "0.0.2", | ||
| "description": "SDK for building Cola channel plugins that connect external messaging platforms to Cola.", | ||
| "license": "MIT", | ||
| "type": "module", | ||
| "exports": "./distribution/source/index.js", | ||
| "types": "./distribution/source", | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "git+https://github.com/marswaveai/cola.git", | ||
| "directory": "packages/plugin-sdk" | ||
| }, | ||
| "publishConfig": { | ||
| "access": "public" | ||
| }, | ||
| "exports": { | ||
| ".": { | ||
| "types": "./distribution/source/index.d.ts", | ||
| "import": "./distribution/source/index.js" | ||
| } | ||
| }, | ||
| "types": "./distribution/source/index.d.ts", | ||
| "files": [ | ||
@@ -11,0 +23,0 @@ "distribution/source" |
+102
-31
| # Cola Plugin SDK | ||
| TypeScript SDK for building Cola channel plugins. | ||
| [Documentation](https://docs.colaos.ai/en/docs/plugin-sdk/) | ||
| Channel plugins connect external message platforms to Cola. A plugin receives | ||
| messages from a platform, delivers normalized prompts into Cola, and sends Cola's | ||
| reply back through the same platform without importing Cola server internals. | ||
| TypeScript SDK for building Cola plugins. | ||
| English | [中文](./README.zh.md) | ||
| A Cola plugin is a local Node.js package that extends Cola from outside its | ||
| codebase. One plugin can play any combination of three roles: | ||
| - **Channel.** Connect an external message platform — Slack, WeChat, a device, | ||
| a queue — and route messages in and out of Cola. | ||
| - **Tools.** Register LLM-callable tools (Notion search, SQL queries, anything | ||
| the model should be able to invoke). Tools default to global scope, so a | ||
| tool-only plugin needs no channel. | ||
| - **Observer.** Subscribe to Cola's session event stream across every session | ||
| — desktop, CLI, every channel — for analytics, audit mirroring, or | ||
| cross-channel automation. | ||
| Plugins never import Cola desktop, server, or shared internals; the SDK is the | ||
| stable boundary. | ||
| ## Package | ||
@@ -17,21 +28,13 @@ | ||
| When testing against an unreleased Cola checkout, link to the local SDK package | ||
| from the same checkout as the server: | ||
| ```json | ||
| { | ||
| "dependencies": { | ||
| "@marswave/cola-plugin-sdk": "link:/path/to/cola/packages/plugin-sdk" | ||
| } | ||
| } | ||
| ``` | ||
| ## Public API | ||
| - `defineChannel()` for the standard channel-plugin shape. | ||
| - `definePlugin()` for plugins that combine a channel with commands or future | ||
| capability slots. | ||
| - Public types for plugin metadata, channel capabilities, gateway adapters, | ||
| outbound delivery, auth, config schemas, commands, host runtime APIs, STT/TTS, | ||
| and session lifecycle events. | ||
| - `definePlugin()` is the general entry point. It accepts a top-level `tools[]` | ||
| slot (each tool picks `scope: 'global' | 'own-channel'`), optional `start` / | ||
| `stop` lifecycle hooks, and an optional `channel`. | ||
| - `defineChannel()` is a thin wrapper for channel-only plugins that don't | ||
| declare top-level tools or lifecycle hooks. | ||
| - Public types for plugin metadata, channel capabilities, gateway/outbound/auth | ||
| adapters, config schemas, commands, host runtime APIs, STT/TTS, top-level | ||
| tools, and session lifecycle events (including the `origin` tag and | ||
| `scope: 'self' | 'all'` subscription option). | ||
| - `createPollLoop()` and `withBackoff()` helper utilities for polling channels. | ||
@@ -155,3 +158,8 @@ | ||
| - `config.get()` for readonly plugin config. | ||
| - `events.on()` for plugin-scoped session events. | ||
| - `events.on()` for session events — session lifecycle, compaction, agent | ||
| loop, turn boundaries, assistant message stream, `tool:call` / | ||
| `tool:result`, and runtime `tool:execution_*`. Defaults to this plugin's | ||
| own scope; pass `{ scope: 'all' }` to observe every session in Cola. See | ||
| the [event reference](https://github.com/marswave/cola/blob/main/docs/channels/plugin-session-events.md) | ||
| for the full catalogue, the `origin` tag, and the non-forwarding contract. | ||
| - `stt` and `tts` for host-provided local speech services when available. | ||
@@ -161,15 +169,78 @@ - `logger` for local logging and explicit error reporting. | ||
| ## Local Testing | ||
| ### Plugin tools | ||
| Use the Cola CLI that belongs to the same checkout as the server you are testing: | ||
| A plugin can register LLM-callable tools via the top-level `tools` array. Each | ||
| tool declares a `scope`: | ||
| ```bash | ||
| pnpm cli:dev plugin install /path/to/plugin | ||
| pnpm cli:dev channel login example | ||
| pnpm cli:dev channel status | ||
| - `'global'` (default) — visible in every session: desktop, CLI, and every | ||
| plugin channel. Use this for capability-style tools (Notion search, SQL | ||
| query, etc.). | ||
| - `'own-channel'` — visible only when a session under this plugin's own | ||
| channel scope is active. Requires the plugin to also declare `channel`. | ||
| Tool names are exposed to the LLM as `plugin__<pluginId>__<name>` so the same | ||
| short name from different plugins does not collide. `tool:call` and | ||
| `tool:result` events report the un-prefixed name. | ||
| ```ts | ||
| export default definePlugin({ | ||
| id: 'notion', | ||
| meta: { label: 'Notion', description: 'Notion integration' }, | ||
| tools: [ | ||
| { | ||
| name: 'search', | ||
| description: 'Search the Notion workspace', | ||
| parameters: { type: 'object', properties: { q: { type: 'string' } } }, | ||
| async execute({ q }, ctx) { | ||
| // ... | ||
| return { content: [{ type: 'text', text: 'result' }] } | ||
| } | ||
| } | ||
| ] | ||
| }) | ||
| ``` | ||
| The development server should also come from the same checkout, otherwise it may | ||
| not understand the current SDK shape or plugin manifest metadata. | ||
| `parameters` is a JSON Schema object (Typebox schemas are JSON Schema at | ||
| runtime). `execute()` receives the parsed input and a `PluginToolContext` with | ||
| `sessionId` (undefined for desktop/CLI scopes), `scopeKey`, `toolCallId`, an | ||
| `AbortSignal`, the plugin `logger`, and the readonly `config`. Return | ||
| `{ content, details?, isError? }`. | ||
| ### Lifecycle and Cross-Channel Observation | ||
| `Plugin.start(ctx)` runs once when the plugin loads, and `Plugin.stop()` once | ||
| when it unloads. Use them to register listeners, open background workers, etc. | ||
| Each runs under a 5 s timeout so a slow plugin can't stall the host. | ||
| `runtime.events.on(type, handler, options?)` accepts `options.scope`: | ||
| - `'self'` (default) — only sessions under this plugin's own scope. | ||
| - `'all'` — every session in Cola, including desktop, CLI, and other plugins. | ||
| Use this for cross-channel automation and global observability. | ||
| Every event payload carries an `origin` discriminator | ||
| (`{ kind: 'plugin', pluginId } | { kind: 'desktop' } | { kind: 'cli', user } | { kind: 'other', scopeKey }`) | ||
| so cross-scope handlers can tell where the event came from. | ||
| ```ts | ||
| export default definePlugin({ | ||
| id: 'observer', | ||
| meta: { label: 'Observer', description: 'Mirrors activity to a webhook' }, | ||
| async start({ runtime }) { | ||
| runtime.events.on( | ||
| 'turn:end', | ||
| (e) => { | ||
| runtime.logger.info(`turn ${e.turnIndex} ended on ${e.origin.kind}`) | ||
| }, | ||
| { scope: 'all' } | ||
| ) | ||
| } | ||
| }) | ||
| ``` | ||
| Listeners are read-only; the return value is ignored, and each handler runs | ||
| under a 2 s timeout so a slow plugin can't block the agent. The `toolName` on | ||
| `tool:*` events is always the original, unprefixed name (e.g. `lookup_user`, | ||
| not `plugin__example__lookup_user`). | ||
| ## Compatibility | ||
@@ -176,0 +247,0 @@ |
+95
-29
| # Cola Plugin SDK 中文说明 | ||
| Cola Plugin SDK 是用于开发 Cola channel plugin 的 TypeScript SDK。 | ||
| Cola Plugin SDK 是用于开发 Cola 插件的 TypeScript SDK。 | ||
| Channel plugin 负责把外部消息平台接入 Cola:它从平台接收消息,把规范化后的 | ||
| prompt 交给 Cola,再把 Cola 的回复发回同一个平台。插件不应该 import Cola | ||
| desktop、server 或 shared 的内部模块;SDK 就是面向插件作者的稳定边界。 | ||
| Cola 插件是一个本地 Node.js 包,可以在 Cola 代码库之外扩展 Cola。同一个插件 | ||
| 可以承担下面三种角色的任意组合: | ||
| - **Channel。** 把外部消息平台(Slack、微信、设备、消息队列等)接入 Cola, | ||
| 在外部平台和 Cola 之间路由消息。 | ||
| - **Tools。** 注册 LLM 可调用的工具(Notion 搜索、SQL 查询,任何你希望模型 | ||
| 能直接调用的能力)。工具默认是全局可见的,所以纯工具型插件不需要 channel。 | ||
| - **Observer。** 跨所有会话订阅 Cola 的 session 事件流——桌面、CLI、每个 | ||
| 渠道都涵盖——用于埋点、审计镜像或跨渠道自动化。 | ||
| 插件不应该 import Cola desktop、server 或 shared 的内部模块;SDK 就是面向 | ||
| 插件作者的稳定边界。 | ||
| [English](./README.md) | 中文 | ||
| [文档](https://docs.colaos.ai/en/docs/plugin-sdk/) | ||
| ## 安装 | ||
@@ -17,19 +28,12 @@ | ||
| 如果你在测试尚未发布的 SDK 或 loader 改动,插件应链接到同一个 Cola checkout | ||
| 里的 SDK 包: | ||
| ```json | ||
| { | ||
| "dependencies": { | ||
| "@marswave/cola-plugin-sdk": "link:/path/to/cola/packages/plugin-sdk" | ||
| } | ||
| } | ||
| ``` | ||
| ## 主要能力 | ||
| - `defineChannel()`:定义标准 channel plugin。 | ||
| - `definePlugin()`:定义包含 channel、commands 或未来能力槽的插件。 | ||
| - `definePlugin()`:通用入口。可声明顶层 `tools[]`(每个 tool 自带 | ||
| `scope: 'global' | 'own-channel'`)、可选的 `start` / `stop` 生命周期钩子, | ||
| 以及可选的 `channel`。 | ||
| - `defineChannel()`:纯 channel 插件的轻量包装,不需要顶层 tools 或生命周期 | ||
| 钩子时使用。 | ||
| - 公开类型:插件元数据、channel capabilities、gateway/outbound/auth/config | ||
| adapters、commands、host runtime、STT/TTS、session lifecycle events。 | ||
| adapters、commands、host runtime、STT/TTS、顶层 tools,以及带 `origin` | ||
| 标签和 `scope: 'self' | 'all'` 订阅参数的 session lifecycle events。 | ||
| - `createPollLoop()` 和 `withBackoff()`:适合轮询型 channel 的工具函数。 | ||
@@ -71,19 +75,81 @@ | ||
| `ctx.runtime` 暴露插件可以安全使用的宿主能力,包括身份绑定、只读配置、插件级 | ||
| session events、本地 STT/TTS、日志上报,以及在配置或登录状态变化后重启 gateway | ||
| 的 `reloadGateway()`。 | ||
| `ctx.runtime` 暴露插件可以安全使用的宿主能力,包括身份绑定、只读配置、 | ||
| session events(session 生命周期、compaction、agent 循环、turn、assistant | ||
| message stream、`tool:call`/`tool:result`、运行时 `tool:execution_*`, | ||
| 默认本插件 scope,传 `{ scope: 'all' }` 可订阅所有会话)、本地 STT/TTS、 | ||
| 日志上报,以及在配置或登录状态变化后重启 gateway 的 `reloadGateway()`。 | ||
| 完整事件目录、`origin` 标签以及"不转发"契约见 | ||
| [event 参考文档](https://github.com/marswave/cola/blob/main/docs/channels/plugin-session-events.md)。 | ||
| ## 本地测试 | ||
| ### 插件工具 | ||
| 本地测试时使用同一个 checkout 里的 Cola CLI 和开发服务器: | ||
| 插件可以通过顶层 `tools` 数组注册 LLM 可调用的工具。每个工具声明一个 `scope`: | ||
| ```bash | ||
| pnpm cli:dev plugin install /path/to/plugin | ||
| pnpm cli:dev channel login example | ||
| pnpm cli:dev channel status | ||
| - `'global'`(默认)—— 在所有会话(桌面、CLI、所有插件渠道)中可见。适合通用 | ||
| 能力型工具(Notion 搜索、SQL 查询等)。 | ||
| - `'own-channel'` —— 仅当本插件自己的 channel session 处于激活状态时可见。 | ||
| 需要同时声明 `channel`。 | ||
| 工具名会被加上前缀 `plugin__<pluginId>__<name>` 暴露给 LLM,避免不同插件之间 | ||
| 的短名冲突。`tool:call` / `tool:result` 事件回传不带前缀的原名。 | ||
| ```ts | ||
| export default definePlugin({ | ||
| id: 'notion', | ||
| meta: { label: 'Notion', description: 'Notion 集成' }, | ||
| tools: [ | ||
| { | ||
| name: 'search', | ||
| description: '搜索 Notion 工作区', | ||
| parameters: { type: 'object', properties: { q: { type: 'string' } } }, | ||
| async execute({ q }, ctx) { | ||
| // ... | ||
| return { content: [{ type: 'text', text: 'result' }] } | ||
| } | ||
| } | ||
| ] | ||
| }) | ||
| ``` | ||
| 如果 CLI、server 和 SDK 来自不同 checkout,运行中的 server 可能不认识当前的 | ||
| manifest 字段或 SDK 类型。 | ||
| `parameters` 是 JSON Schema(Typebox schema 在运行时就是 JSON Schema)。 | ||
| `execute()` 收到解析好的入参和 `PluginToolContext`,里面包含 `sessionId` | ||
| (桌面 / CLI scope 下为 `undefined`)、`scopeKey`、`toolCallId`、`AbortSignal`、 | ||
| 插件 `logger` 以及只读 `config`。返回 `{ content, details?, isError? }`。 | ||
| ### 生命周期与跨渠道观察 | ||
| `Plugin.start(ctx)` 在插件加载时执行一次,`Plugin.stop()` 在卸载时执行一次。 | ||
| 适合在里面注册监听器、启动后台任务等。两者都有 5 秒超时保护,避免慢启动拖住 | ||
| 宿主。 | ||
| `runtime.events.on(type, handler, options?)` 支持 `options.scope`: | ||
| - `'self'`(默认)—— 只接收本插件 scope 的 session 事件。 | ||
| - `'all'` —— 接收 Cola 里所有 session 事件,包括桌面、CLI 和其他插件。适合 | ||
| 跨渠道自动化和全局可观测性。 | ||
| 每条事件 payload 都带有 `origin` 判别字段 | ||
| (`{ kind: 'plugin', pluginId } | { kind: 'desktop' } | { kind: 'cli', user } | { kind: 'other', scopeKey }`), | ||
| 跨 scope 的 handler 可以据此判断事件来源。 | ||
| ```ts | ||
| export default definePlugin({ | ||
| id: 'observer', | ||
| meta: { label: 'Observer', description: '把活动镜像到 webhook' }, | ||
| async start({ runtime }) { | ||
| runtime.events.on( | ||
| 'turn:end', | ||
| (e) => { | ||
| runtime.logger.info(`turn ${e.turnIndex} 结束于 ${e.origin.kind}`) | ||
| }, | ||
| { scope: 'all' } | ||
| ) | ||
| } | ||
| }) | ||
| ``` | ||
| 监听器是只读的:返回值会被忽略,每个 handler 还有 2 秒超时保护。`tool:*` | ||
| 事件里的 `toolName` 永远是原始名(例如 `lookup_user`,不是 | ||
| `plugin__example__lookup_user`)。 | ||
| ## 兼容性 | ||
@@ -90,0 +156,0 @@ |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
36066
53.96%551
48.12%256
38.38%0
-100%3
50%