@zhin.js/core 1.1.35 → 1.1.37
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +79 -193
- package/lib/built/ai-outbound/index.d.ts +0 -1
- package/lib/built/ai-outbound/index.js +0 -1
- package/lib/built/ai-outbound/types.d.ts +0 -9
- package/lib/built/ai-trigger.d.ts +0 -5
- package/lib/built/ai-trigger.js +1 -27
- package/lib/built/common-adapter-tools.d.ts +0 -2
- package/lib/built/common-adapter-tools.js +0 -4
- package/lib/built/html-segment-fallback.d.ts +2 -5
- package/lib/built/html-segment-fallback.js +0 -15
- package/lib/built/interactive-segments/index.d.ts +0 -2
- package/lib/built/interactive-segments/index.js +0 -2
- package/lib/built/interactive-segments/keyboard-segment.d.ts +0 -1
- package/lib/built/interactive-segments/keyboard-segment.js +0 -1
- package/lib/built/interactive-segments/onebot-keyboard.d.ts +0 -1
- package/lib/built/interactive-segments/onebot-keyboard.js +0 -2
- package/lib/built/interactive-segments/resolve.d.ts +0 -1
- package/lib/built/interactive-segments/resolve.js +0 -1
- package/lib/built/interactive-segments/types.d.ts +0 -5
- package/lib/built/login-assist.d.ts +1 -8
- package/lib/built/message-enrich.d.ts +2 -8
- package/lib/built/message-enrich.js +3 -11
- package/lib/built/segment-contract/validate.d.ts +2 -2
- package/lib/built/segment-contract/validate.js +1 -3
- package/lib/built/tool-access.d.ts +5 -0
- package/lib/built/tool-access.js +17 -0
- package/lib/built/user-interaction.js +0 -4
- package/lib/feature/handler.d.ts +1 -5
- package/lib/index.d.ts +4 -45
- package/lib/index.js +3 -39
- package/lib/message.d.ts +1 -2
- package/lib/models/system-log.d.ts +0 -2
- package/lib/models/system-log.js +1 -8
- package/lib/models/user.d.ts +0 -2
- package/lib/models/user.js +1 -7
- package/lib/notice.d.ts +1 -2
- package/lib/plugin-runtime/im/conversation-runtime.d.ts +19 -0
- package/lib/plugin-runtime/im/conversation-runtime.js +312 -0
- package/lib/plugin-runtime/im/endpoint-runtime.d.ts +79 -0
- package/lib/plugin-runtime/im/endpoint-runtime.js +162 -0
- package/lib/plugin-runtime/im/im-runtime.d.ts +13 -106
- package/lib/plugin-runtime/im/im-runtime.js +58 -1061
- package/lib/plugin-runtime/im/inbound-runtime.d.ts +31 -0
- package/lib/plugin-runtime/im/inbound-runtime.js +252 -0
- package/lib/plugin-runtime/im/index.d.ts +4 -2
- package/lib/plugin-runtime/im/index.js +2 -2
- package/lib/plugin-runtime/im/ingress-route.d.ts +10 -0
- package/lib/plugin-runtime/im/ingress-route.js +10 -0
- package/lib/plugin-runtime/im/interaction-runtime.d.ts +23 -0
- package/lib/plugin-runtime/im/interaction-runtime.js +181 -0
- package/lib/plugin-runtime/im/interactive-router.d.ts +17 -0
- package/lib/plugin-runtime/im/interactive-router.js +98 -0
- package/lib/plugin-runtime/im/message-bus.d.ts +3 -3
- package/lib/plugin-runtime/im/message-bus.js +3 -3
- package/lib/plugin-runtime/im/message-events.d.ts +26 -0
- package/lib/plugin-runtime/im/message-events.js +61 -0
- package/lib/plugin-runtime/im/outbound-delivery-runtime.d.ts +19 -0
- package/lib/plugin-runtime/im/outbound-delivery-runtime.js +165 -0
- package/lib/plugin-runtime/im/outbound-segments.d.ts +2 -3
- package/lib/plugin-runtime/im/outbound-segments.js +3 -4
- package/lib/plugin-runtime/im/runtime-middleware.d.ts +3 -0
- package/lib/plugin-runtime/im/runtime-middleware.js +13 -0
- package/lib/request.d.ts +1 -2
- package/lib/side-event/base.d.ts +1 -2
- package/lib/side-event/normalize.d.ts +1 -5
- package/lib/side-event/normalize.js +1 -11
- package/lib/system-event.d.ts +1 -2
- package/lib/types.d.ts +9 -116
- package/lib/utils.d.ts +51 -24
- package/lib/utils.js +11 -23
- package/package.json +18 -28
- package/lib/adapter.d.ts +0 -145
- package/lib/adapter.js +0 -377
- package/lib/built/adapter-process.d.ts +0 -58
- package/lib/built/adapter-process.js +0 -128
- package/lib/built/agent-preset.d.ts +0 -64
- package/lib/built/agent-preset.js +0 -79
- package/lib/built/ai-outbound/adapter-access.d.ts +0 -3
- package/lib/built/ai-outbound/adapter-access.js +0 -9
- package/lib/built/authorization.d.ts +0 -10
- package/lib/built/authorization.js +0 -76
- package/lib/built/command.d.ts +0 -72
- package/lib/built/command.js +0 -105
- package/lib/built/component.d.ts +0 -67
- package/lib/built/component.js +0 -97
- package/lib/built/config.d.ts +0 -98
- package/lib/built/config.js +0 -372
- package/lib/built/connect-endpoint-instance.d.ts +0 -17
- package/lib/built/connect-endpoint-instance.js +0 -42
- package/lib/built/database.d.ts +0 -68
- package/lib/built/database.js +0 -106
- package/lib/built/dispatcher.d.ts +0 -141
- package/lib/built/dispatcher.js +0 -383
- package/lib/built/endpoint-commands.d.ts +0 -3
- package/lib/built/endpoint-commands.js +0 -99
- package/lib/built/endpoint-lifecycle-service.d.ts +0 -28
- package/lib/built/endpoint-lifecycle-service.js +0 -242
- package/lib/built/endpoint-lifecycle.d.ts +0 -27
- package/lib/built/endpoint-lifecycle.js +0 -36
- package/lib/built/endpoint-manager.d.ts +0 -34
- package/lib/built/endpoint-manager.js +0 -1
- package/lib/built/html-renderer-loader.d.ts +0 -13
- package/lib/built/html-renderer-loader.js +0 -33
- package/lib/built/inbound-pipeline.d.ts +0 -37
- package/lib/built/inbound-pipeline.js +0 -63
- package/lib/built/inbound-runner.d.ts +0 -22
- package/lib/built/inbound-runner.js +0 -34
- package/lib/built/interactive-segment-contract.d.ts +0 -6
- package/lib/built/interactive-segment-contract.js +0 -9
- package/lib/built/interactive-segments/fallback-store.d.ts +0 -29
- package/lib/built/interactive-segments/fallback-store.js +0 -63
- package/lib/built/interactive-segments/handlers.d.ts +0 -16
- package/lib/built/interactive-segments/handlers.js +0 -74
- package/lib/built/introspection-format.d.ts +0 -10
- package/lib/built/introspection-format.js +0 -28
- package/lib/built/management-command-guard.d.ts +0 -15
- package/lib/built/management-command-guard.js +0 -25
- package/lib/built/message-filter.d.ts +0 -176
- package/lib/built/message-filter.js +0 -239
- package/lib/built/optional-peer-import.d.ts +0 -3
- package/lib/built/optional-peer-import.js +0 -33
- package/lib/built/outbound-media-contract.d.ts +0 -22
- package/lib/built/outbound-media-contract.js +0 -10
- package/lib/built/permission.d.ts +0 -51
- package/lib/built/permission.js +0 -99
- package/lib/built/permit-check.d.ts +0 -7
- package/lib/built/permit-check.js +0 -35
- package/lib/built/permit-parse.d.ts +0 -16
- package/lib/built/permit-parse.js +0 -32
- package/lib/built/platform-permit.d.ts +0 -20
- package/lib/built/platform-permit.js +0 -91
- package/lib/built/rich-segments/adapter-policies.d.ts +0 -9
- package/lib/built/rich-segments/adapter-policies.js +0 -27
- package/lib/built/rich-segments/base.d.ts +0 -11
- package/lib/built/rich-segments/base.js +0 -24
- package/lib/built/rich-segments/builtins.d.ts +0 -4
- package/lib/built/rich-segments/builtins.js +0 -40
- package/lib/built/rich-segments/capabilities.d.ts +0 -6
- package/lib/built/rich-segments/capabilities.js +0 -38
- package/lib/built/rich-segments/html-segment.d.ts +0 -15
- package/lib/built/rich-segments/html-segment.js +0 -62
- package/lib/built/rich-segments/index.d.ts +0 -13
- package/lib/built/rich-segments/index.js +0 -13
- package/lib/built/rich-segments/markdown-segment.d.ts +0 -14
- package/lib/built/rich-segments/markdown-segment.js +0 -58
- package/lib/built/rich-segments/qrcode-segment.d.ts +0 -18
- package/lib/built/rich-segments/qrcode-segment.js +0 -73
- package/lib/built/rich-segments/registry.d.ts +0 -16
- package/lib/built/rich-segments/registry.js +0 -52
- package/lib/built/rich-segments/resolve.d.ts +0 -5
- package/lib/built/rich-segments/resolve.js +0 -87
- package/lib/built/rich-segments/tts-segment.d.ts +0 -13
- package/lib/built/rich-segments/tts-segment.js +0 -51
- package/lib/built/rich-segments/types.d.ts +0 -79
- package/lib/built/rich-segments/types.js +0 -17
- package/lib/built/runtime-io.d.ts +0 -9
- package/lib/built/runtime-io.js +0 -67
- package/lib/built/schedule.d.ts +0 -54
- package/lib/built/schedule.js +0 -112
- package/lib/built/schema-endpoint-manager.d.ts +0 -17
- package/lib/built/schema-endpoint-manager.js +0 -73
- package/lib/built/schema-feature.d.ts +0 -77
- package/lib/built/schema-feature.js +0 -130
- package/lib/built/skill.d.ts +0 -120
- package/lib/built/skill.js +0 -151
- package/lib/built/speech-loader.d.ts +0 -14
- package/lib/built/speech-loader.js +0 -32
- package/lib/built/tool.d.ts +0 -151
- package/lib/built/tool.js +0 -388
- package/lib/command.d.ts +0 -50
- package/lib/command.js +0 -106
- package/lib/endpoint-capabilities.d.ts +0 -46
- package/lib/endpoint-capabilities.js +0 -55
- package/lib/endpoint.d.ts +0 -19
- package/lib/endpoint.js +0 -3
- package/lib/host-plugin-registry.d.ts +0 -8
- package/lib/host-plugin-registry.js +0 -12
- package/lib/plugin-context.d.ts +0 -27
- package/lib/plugin-context.js +0 -64
- package/lib/plugin-runtime/im/interactive.d.ts +0 -26
- package/lib/plugin-runtime/im/interactive.js +0 -46
- package/lib/plugin-runtime/im/service-tokens.d.ts +0 -18
- package/lib/plugin-runtime/im/service-tokens.js +0 -13
- package/lib/plugin.d.ts +0 -268
- package/lib/plugin.js +0 -583
- package/lib/schema-interaction.d.ts +0 -117
- package/lib/schema-interaction.js +0 -247
- package/lib/tool-zod.d.ts +0 -43
- package/lib/tool-zod.js +0 -184
package/lib/adapter.js
DELETED
|
@@ -1,377 +0,0 @@
|
|
|
1
|
-
import { assertOutbound, DEFAULT_ENDPOINT_CAPABILITIES, hasInbound, } from "./endpoint-capabilities.js";
|
|
2
|
-
import { endpointControlOf } from "@zhin.js/adapter";
|
|
3
|
-
import { connectEndpointInstance, disconnectEndpointInstance } from "./built/connect-endpoint-instance.js";
|
|
4
|
-
import { EventEmitter } from "node:events";
|
|
5
|
-
import { getOutboundReplyStore } from "./built/dispatcher.js";
|
|
6
|
-
import { DEFAULT_OUTBOUND_RICH_SEGMENT_POLICY, resolveRichSegments, } from "./built/rich-segments/index.js";
|
|
7
|
-
import { DEFAULT_INTERACTIVE_POLICY, collectKeyboardFallbackMaps, keyboardFallbackStore, resolveKeyboardSegments, } from "./built/interactive-segments/index.js";
|
|
8
|
-
import { DEFAULT_AI_OUTBOUND_CAPABILITIES, } from "./built/ai-outbound/index.js";
|
|
9
|
-
import { createRichSegmentRenderContext } from "./built/rich-segments/capabilities.js";
|
|
10
|
-
import { collectOutboundMediaKinds } from "./built/outbound-media-utils.js";
|
|
11
|
-
import { segment } from "./utils.js";
|
|
12
|
-
import { InboundMessagePipeline } from "./built/inbound-pipeline.js";
|
|
13
|
-
import { formatCompact, truncatePreview, formatContentChainLog, CONTENT_CHAIN_STAGE } from '@zhin.js/logger';
|
|
14
|
-
/**
|
|
15
|
-
* Adapter类:适配器抽象,管理多平台Bot实例。
|
|
16
|
-
* 负责根据配置启动/关闭各平台机器人,统一异常处理。
|
|
17
|
-
*
|
|
18
|
-
* 适配器可以提供 AI 工具,供 AI 服务调用。
|
|
19
|
-
*/
|
|
20
|
-
export class Adapter extends EventEmitter {
|
|
21
|
-
plugin;
|
|
22
|
-
name;
|
|
23
|
-
config;
|
|
24
|
-
endpoints = new Map();
|
|
25
|
-
recallMessageHandler;
|
|
26
|
-
/** Adapter 支持的能力上限(子类覆盖) */
|
|
27
|
-
static capabilities = DEFAULT_ENDPOINT_CAPABILITIES;
|
|
28
|
-
/** 出站富媒体段渲染策略(子类 override static) */
|
|
29
|
-
static outboundRichSegmentPolicy = DEFAULT_OUTBOUND_RICH_SEGMENT_POLICY;
|
|
30
|
-
/** 交互段出站策略:native 保留按钮段,text 降级为编号文本 */
|
|
31
|
-
static interactivePolicy = DEFAULT_INTERACTIVE_POLICY;
|
|
32
|
-
/** AI 结构化出站能力声明(mentions / richSegments / interactive) */
|
|
33
|
-
static aiOutboundCapabilities = DEFAULT_AI_OUTBOUND_CAPABILITIES;
|
|
34
|
-
/** 平台特有 extensions schema + 解析(对齐各 SDK 文档子集) */
|
|
35
|
-
static aiOutboundExtensions;
|
|
36
|
-
getInteractivePolicy() {
|
|
37
|
-
return this.constructor.interactivePolicy;
|
|
38
|
-
}
|
|
39
|
-
/** 入站消息并发计数 */
|
|
40
|
-
#pendingMessages = 0;
|
|
41
|
-
/** 并发上限,0 表示不限制(默认) */
|
|
42
|
-
static DEFAULT_MAX_CONCURRENT_MESSAGES = 0;
|
|
43
|
-
get maxConcurrentMessages() {
|
|
44
|
-
try {
|
|
45
|
-
const configService = this.plugin?.root?.inject('config');
|
|
46
|
-
const appConfig = configService?.getPrimary?.();
|
|
47
|
-
return appConfig?.max_concurrent_messages ?? Adapter.DEFAULT_MAX_CONCURRENT_MESSAGES;
|
|
48
|
-
}
|
|
49
|
-
catch {
|
|
50
|
-
return Adapter.DEFAULT_MAX_CONCURRENT_MESSAGES;
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
/** 当前正在处理的消息数 */
|
|
54
|
-
get pendingMessages() {
|
|
55
|
-
return this.#pendingMessages;
|
|
56
|
-
}
|
|
57
|
-
/**
|
|
58
|
-
* 创建一个适配器运行实例。
|
|
59
|
-
* @param plugin 拥有该适配器的插件实例
|
|
60
|
-
* @param name 适配器名称(如 'process'、'qq' 等)
|
|
61
|
-
* @param config Endpoint 配置列表
|
|
62
|
-
*/
|
|
63
|
-
constructor(plugin, name, config) {
|
|
64
|
-
super();
|
|
65
|
-
this.plugin = plugin;
|
|
66
|
-
this.name = name;
|
|
67
|
-
this.config = config;
|
|
68
|
-
this.recallMessageHandler = (endpoint_id, id) => this.recallEndpointMessage(endpoint_id, id);
|
|
69
|
-
this.on('call.recallMessage', this.recallMessageHandler);
|
|
70
|
-
this.inboundPipeline = new InboundMessagePipeline({
|
|
71
|
-
getPlugin: () => this.plugin,
|
|
72
|
-
logger: this.logger,
|
|
73
|
-
getMaxConcurrentMessages: () => this.maxConcurrentMessages,
|
|
74
|
-
getPendingMessages: () => this.#pendingMessages,
|
|
75
|
-
decrementPending: () => {
|
|
76
|
-
// stop() may zero the counter while in-flight receives still finish;
|
|
77
|
-
// never let the budget go negative.
|
|
78
|
-
this.#pendingMessages = Math.max(0, this.#pendingMessages - 1);
|
|
79
|
-
},
|
|
80
|
-
});
|
|
81
|
-
}
|
|
82
|
-
/** 入站消息管线(替代 emit override 的隐式管线) */
|
|
83
|
-
inboundPipeline;
|
|
84
|
-
/**
|
|
85
|
-
* 重写 emit:拦截 message.receive/notice.receive/request.receive 事件,
|
|
86
|
-
* 将入站处理委托给 InboundMessagePipeline。
|
|
87
|
-
*/
|
|
88
|
-
emit = ((event, ...args) => {
|
|
89
|
-
if (event === 'notice.receive' || event === 'request.receive') {
|
|
90
|
-
return this.inboundPipeline.bridgeNoticeOrRequest(event, args, () => EventEmitter.prototype.emit.call(this, event, ...args));
|
|
91
|
-
}
|
|
92
|
-
if (event !== 'message.receive') {
|
|
93
|
-
return EventEmitter.prototype.emit.call(this, event, ...args);
|
|
94
|
-
}
|
|
95
|
-
const message = args[0];
|
|
96
|
-
// 背压控制:同步返回 false 表示丢弃
|
|
97
|
-
if (this.inboundPipeline.shouldDropDueToBackpressure()) {
|
|
98
|
-
this.logger.warn(formatCompact({ op: 'backpressure_drop', limit: this.maxConcurrentMessages }));
|
|
99
|
-
return false;
|
|
100
|
-
}
|
|
101
|
-
// 同步计数,保证背压判断的准确性
|
|
102
|
-
this.#pendingMessages++;
|
|
103
|
-
// 异步入站管线(middleware → dispatcher → 生命周期 → 观察者)
|
|
104
|
-
this.inboundPipeline.receive(message, () => {
|
|
105
|
-
EventEmitter.prototype.emit.call(this, event, ...args);
|
|
106
|
-
});
|
|
107
|
-
return true;
|
|
108
|
-
});
|
|
109
|
-
get logger() {
|
|
110
|
-
if (!this.plugin)
|
|
111
|
-
throw new Error("Adapter is not associated with any plugin");
|
|
112
|
-
return this.plugin.logger;
|
|
113
|
-
}
|
|
114
|
-
binding(plugin) {
|
|
115
|
-
this.plugin = plugin;
|
|
116
|
-
}
|
|
117
|
-
/**
|
|
118
|
-
* 同步等待入站管线结束(middleware → dispatcher → 生命周期 → 观察者)。
|
|
119
|
-
* 用于 QQ 等需在被动回复完成后再 ack 交互的平台事件。
|
|
120
|
-
*/
|
|
121
|
-
async receiveMessageAwait(message) {
|
|
122
|
-
if (this.inboundPipeline.shouldDropDueToBackpressure()) {
|
|
123
|
-
this.logger.warn(formatCompact({ drop: 'concurrency', limit: this.maxConcurrentMessages }));
|
|
124
|
-
return false;
|
|
125
|
-
}
|
|
126
|
-
this.#pendingMessages++;
|
|
127
|
-
try {
|
|
128
|
-
await this.inboundPipeline.receive(message, () => {
|
|
129
|
-
EventEmitter.prototype.emit.call(this, 'message.receive', message);
|
|
130
|
-
});
|
|
131
|
-
return true;
|
|
132
|
-
}
|
|
133
|
-
finally {
|
|
134
|
-
// Inbound pipeline errors must not leak the concurrency budget; the
|
|
135
|
-
// non-await path uses InboundMessagePipeline.decrementPending in finally.
|
|
136
|
-
this.#pendingMessages = Math.max(0, this.#pendingMessages - 1);
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
/**
|
|
140
|
-
* 出站富媒体能力(Publisher 按此过滤/降级;各 adapter 可覆盖)。
|
|
141
|
-
*/
|
|
142
|
-
getOutboundMediaCapabilities() {
|
|
143
|
-
return {
|
|
144
|
-
image: true,
|
|
145
|
-
audio: true,
|
|
146
|
-
video: true,
|
|
147
|
-
file: true,
|
|
148
|
-
maxAttachmentBytes: 26_214_400,
|
|
149
|
-
};
|
|
150
|
-
}
|
|
151
|
-
getOutboundRichSegmentPolicy() {
|
|
152
|
-
return this.constructor.outboundRichSegmentPolicy;
|
|
153
|
-
}
|
|
154
|
-
/**
|
|
155
|
-
* 出站两阶段:
|
|
156
|
-
* 1. resolveRichSegments — 语义段 → 标准 IM 段(image/audio/text)
|
|
157
|
-
* 2. Endpoint materializeOutboundMedia — base64/本地路径 → 平台 URL(各 adapter 可选)
|
|
158
|
-
* @see docs/essentials/rich-segment-adapters.md
|
|
159
|
-
*/
|
|
160
|
-
async renderSendMessage(options) {
|
|
161
|
-
if (options.content != null) {
|
|
162
|
-
options = {
|
|
163
|
-
...options,
|
|
164
|
-
content: await resolveRichSegments(options.content, this.getOutboundRichSegmentPolicy(), createRichSegmentRenderContext({
|
|
165
|
-
getConfig: () => {
|
|
166
|
-
const cfg = this.plugin.root.inject('config')?.getPrimary();
|
|
167
|
-
return cfg;
|
|
168
|
-
},
|
|
169
|
-
warn: (msg) => this.logger.warn(msg),
|
|
170
|
-
logContentChain: (fields) => {
|
|
171
|
-
this.logger.debug(formatContentChainLog({ ...fields, adapter: this.name }));
|
|
172
|
-
},
|
|
173
|
-
})),
|
|
174
|
-
};
|
|
175
|
-
// 'text' 策略端点:keyboard 即将降级为编号文本,先把有效 fallback
|
|
176
|
-
// 映射写入中央存储(频道键与入站回跳解析一致),供数字回跳路由。
|
|
177
|
-
if (this.getInteractivePolicy() === 'text') {
|
|
178
|
-
const channelKey = `${this.name}-${options.endpoint}-${options.type}:${options.id}`;
|
|
179
|
-
for (const map of collectKeyboardFallbackMaps(options.content)) {
|
|
180
|
-
keyboardFallbackStore.remember(channelKey, map);
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
options = {
|
|
184
|
-
...options,
|
|
185
|
-
content: resolveKeyboardSegments(options.content, this.getInteractivePolicy()),
|
|
186
|
-
};
|
|
187
|
-
const mediaKinds = collectOutboundMediaKinds(options.content);
|
|
188
|
-
if (mediaKinds.length > 0) {
|
|
189
|
-
this.logger.debug(formatContentChainLog({
|
|
190
|
-
stage: CONTENT_CHAIN_STAGE.OUTBOUND,
|
|
191
|
-
adapter: this.name,
|
|
192
|
-
endpoint: options.endpoint,
|
|
193
|
-
media: mediaKinds.join(','),
|
|
194
|
-
}));
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
const fns = this.plugin.root.listeners('before.sendMessage');
|
|
198
|
-
for (const fn of fns) {
|
|
199
|
-
const result = await fn(options);
|
|
200
|
-
if (result)
|
|
201
|
-
options = result;
|
|
202
|
-
}
|
|
203
|
-
return options;
|
|
204
|
-
}
|
|
205
|
-
async sendMessage(options) {
|
|
206
|
-
options = await this.renderSendMessage(options);
|
|
207
|
-
const endpoint = this.endpoints.get(options.endpoint);
|
|
208
|
-
if (!endpoint)
|
|
209
|
-
throw new Error(`Endpoint ${options.endpoint} not found`);
|
|
210
|
-
assertOutbound(endpoint);
|
|
211
|
-
this.logger.debug(formatCompact({
|
|
212
|
-
send: `${options.type}(${options.id})`,
|
|
213
|
-
endpoint: options.endpoint,
|
|
214
|
-
preview: truncatePreview(segment.raw(options.content)),
|
|
215
|
-
}));
|
|
216
|
-
const messageId = await endpoint.$sendMessage(options);
|
|
217
|
-
const replyStore = getOutboundReplyStore();
|
|
218
|
-
this.plugin.root.dispatch('message.send', {
|
|
219
|
-
adapter: this.name,
|
|
220
|
-
options,
|
|
221
|
-
messageId,
|
|
222
|
-
replySource: replyStore?.source,
|
|
223
|
-
replyMessage: replyStore?.message,
|
|
224
|
-
});
|
|
225
|
-
return messageId;
|
|
226
|
-
}
|
|
227
|
-
/**
|
|
228
|
-
* `call.recallMessage` 事件链(prompt.ts 超时撤回等)的统一出口:
|
|
229
|
-
* 经 canonical `EndpointControl` 端口撤回。
|
|
230
|
-
*/
|
|
231
|
-
async recallEndpointMessage(endpointKey, messageId) {
|
|
232
|
-
const endpoint = this.endpoints.get(endpointKey);
|
|
233
|
-
if (!endpoint)
|
|
234
|
-
throw new Error(`Endpoint ${endpointKey} not found`);
|
|
235
|
-
assertOutbound(endpoint);
|
|
236
|
-
const control = endpointControlOf(endpoint);
|
|
237
|
-
if (!control?.recall) {
|
|
238
|
-
throw new Error(`Endpoint ${endpointKey} does not support recall`);
|
|
239
|
-
}
|
|
240
|
-
this.logger.debug(formatCompact({ op: 'recall_message', msgId: messageId, endpoint: endpointKey }));
|
|
241
|
-
await control.recall({
|
|
242
|
-
conversation: {
|
|
243
|
-
endpoint: { id: endpointKey, adapter: String(this.name) },
|
|
244
|
-
kind: 'private',
|
|
245
|
-
id: endpointKey,
|
|
246
|
-
},
|
|
247
|
-
id: messageId,
|
|
248
|
-
});
|
|
249
|
-
}
|
|
250
|
-
/**
|
|
251
|
-
* 编辑已发送的消息。
|
|
252
|
-
* - 如果 Endpoint 的 control 端口实现了 edit,调用平台编辑 API
|
|
253
|
-
* - 否则 fallback 到发送新消息
|
|
254
|
-
* @returns 消息 ID(编辑时返回原 ID,fallback 时返回新消息 ID)
|
|
255
|
-
*/
|
|
256
|
-
async editMessage(options) {
|
|
257
|
-
const endpoint = this.endpoints.get(options.endpoint);
|
|
258
|
-
if (!endpoint)
|
|
259
|
-
throw new Error(`Endpoint ${options.endpoint} not found`);
|
|
260
|
-
assertOutbound(endpoint);
|
|
261
|
-
const control = endpointControlOf(endpoint);
|
|
262
|
-
if (control?.edit) {
|
|
263
|
-
const rendered = await this.renderSendMessage({
|
|
264
|
-
context: options.context,
|
|
265
|
-
endpoint: options.endpoint,
|
|
266
|
-
id: options.id,
|
|
267
|
-
type: options.type,
|
|
268
|
-
content: options.content,
|
|
269
|
-
});
|
|
270
|
-
await control.edit({
|
|
271
|
-
conversation: {
|
|
272
|
-
endpoint: { id: options.endpoint, adapter: String(this.name) },
|
|
273
|
-
kind: options.type === 'group' ? 'group' : 'private',
|
|
274
|
-
id: options.id,
|
|
275
|
-
},
|
|
276
|
-
id: options.messageId,
|
|
277
|
-
}, rendered.content);
|
|
278
|
-
this.logger.debug(formatCompact({
|
|
279
|
-
edit: `${options.type}(${options.id})`,
|
|
280
|
-
endpoint: options.endpoint,
|
|
281
|
-
messageId: options.messageId,
|
|
282
|
-
}));
|
|
283
|
-
return options.messageId;
|
|
284
|
-
}
|
|
285
|
-
this.logger.debug(formatCompact({
|
|
286
|
-
editFallback: 'sendMessage',
|
|
287
|
-
endpoint: options.endpoint,
|
|
288
|
-
reason: 'control.edit not implemented',
|
|
289
|
-
}));
|
|
290
|
-
return this.sendMessage({
|
|
291
|
-
context: options.context,
|
|
292
|
-
endpoint: options.endpoint,
|
|
293
|
-
id: options.id,
|
|
294
|
-
type: options.type,
|
|
295
|
-
content: options.content,
|
|
296
|
-
});
|
|
297
|
-
}
|
|
298
|
-
async start() {
|
|
299
|
-
if (this.endpoints.size > 0) {
|
|
300
|
-
await this.stop();
|
|
301
|
-
}
|
|
302
|
-
const rootAdapters = this.plugin.root.adapters;
|
|
303
|
-
if (!rootAdapters.some((n) => String(n) === String(this.name))) {
|
|
304
|
-
rootAdapters.push(this.name);
|
|
305
|
-
}
|
|
306
|
-
if (!this.config?.length)
|
|
307
|
-
return;
|
|
308
|
-
for (const config of this.config) {
|
|
309
|
-
const endpoint = await connectEndpointInstance({
|
|
310
|
-
plugin: this.plugin,
|
|
311
|
-
adapter: this,
|
|
312
|
-
config: config,
|
|
313
|
-
});
|
|
314
|
-
this.logger.debug(formatCompact({ connect: endpoint.$id, adapter: this.name }));
|
|
315
|
-
this.endpoints.set(endpoint.$id, endpoint);
|
|
316
|
-
}
|
|
317
|
-
this.logger.debug(formatCompact({ adapter: this.name }));
|
|
318
|
-
}
|
|
319
|
-
/**
|
|
320
|
-
* 停止适配器,断开并移除所有 Endpoint 实例
|
|
321
|
-
*/
|
|
322
|
-
async stop() {
|
|
323
|
-
const errors = [];
|
|
324
|
-
for (const [id, endpoint] of this.endpoints) {
|
|
325
|
-
try {
|
|
326
|
-
if (hasInbound(endpoint)) {
|
|
327
|
-
await disconnectEndpointInstance(this.plugin, this, endpoint);
|
|
328
|
-
}
|
|
329
|
-
this.logger.debug(formatCompact({ disconnect: id, adapter: this.name }));
|
|
330
|
-
}
|
|
331
|
-
catch (error) {
|
|
332
|
-
errors.push(error instanceof Error ? error : new Error(String(error)));
|
|
333
|
-
this.logger.error(`endpoint ${id} of adapter ${this.name} disconnect failed:`, error);
|
|
334
|
-
}
|
|
335
|
-
}
|
|
336
|
-
// 无论是否有错误,始终完成清理
|
|
337
|
-
this.endpoints.clear();
|
|
338
|
-
// Drop in-flight concurrency counter so a subsequent start() does not
|
|
339
|
-
// inherit a stale backpressure budget from the previous generation.
|
|
340
|
-
this.#pendingMessages = 0;
|
|
341
|
-
// 从 adapters 数组中移除(可能因重复 start 出现多条同名,需全部删掉)
|
|
342
|
-
const rootAdapters = this.plugin.root.adapters;
|
|
343
|
-
for (let i = rootAdapters.length - 1; i >= 0; i--) {
|
|
344
|
-
if (rootAdapters[i] === this.name)
|
|
345
|
-
rootAdapters.splice(i, 1);
|
|
346
|
-
}
|
|
347
|
-
// 移除所有事件监听器
|
|
348
|
-
this.removeAllListeners();
|
|
349
|
-
this.recallMessageHandler = (endpoint_id, id) => this.recallEndpointMessage(endpoint_id, id);
|
|
350
|
-
this.on('call.recallMessage', this.recallMessageHandler);
|
|
351
|
-
this.logger.debug(formatCompact({ stop: this.name }));
|
|
352
|
-
if (errors.length) {
|
|
353
|
-
throw new AggregateError(errors, `adapter ${this.name}: ${errors.length} endpoint(s) failed to disconnect`);
|
|
354
|
-
}
|
|
355
|
-
}
|
|
356
|
-
}
|
|
357
|
-
(function (Adapter) {
|
|
358
|
-
/**
|
|
359
|
-
* 适配器工厂注册表
|
|
360
|
-
* 灵感来源于 zhinjs/next 的 Adapter.Registry
|
|
361
|
-
*/
|
|
362
|
-
Adapter.Registry = new Map();
|
|
363
|
-
/**
|
|
364
|
-
* 注册适配器工厂
|
|
365
|
-
*
|
|
366
|
-
* @param name 适配器名称
|
|
367
|
-
* @param factory 适配器工厂函数
|
|
368
|
-
* @example
|
|
369
|
-
* ```typescript
|
|
370
|
-
* Adapter.register('icqq', IcqqAdapter);
|
|
371
|
-
* ```
|
|
372
|
-
*/
|
|
373
|
-
function register(name, factory) {
|
|
374
|
-
Adapter.Registry.set(name, factory);
|
|
375
|
-
}
|
|
376
|
-
Adapter.register = register;
|
|
377
|
-
})(Adapter || (Adapter = {}));
|
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
import { Adapter } from "../adapter.js";
|
|
2
|
-
import { Plugin } from "../plugin.js";
|
|
3
|
-
import { Endpoint } from "../endpoint.js";
|
|
4
|
-
import { Message } from "../message.js";
|
|
5
|
-
import type { SendOptions } from "../types.js";
|
|
6
|
-
import type { OutboundRichSegmentPolicy } from "./rich-segments/types.js";
|
|
7
|
-
export declare class ProcessEndpoint implements Endpoint<{
|
|
8
|
-
owner?: string;
|
|
9
|
-
}, {
|
|
10
|
-
content: string;
|
|
11
|
-
ts: number;
|
|
12
|
-
}> {
|
|
13
|
-
#private;
|
|
14
|
-
adapter: ProcessAdapter;
|
|
15
|
-
$config: {
|
|
16
|
-
owner?: string;
|
|
17
|
-
};
|
|
18
|
-
$id: string;
|
|
19
|
-
get logger(): import("@zhin.js/logger").Logger;
|
|
20
|
-
$connected: boolean;
|
|
21
|
-
constructor(adapter: ProcessAdapter, $config?: {
|
|
22
|
-
owner?: string;
|
|
23
|
-
});
|
|
24
|
-
$listenInput(chunk: string): void;
|
|
25
|
-
connect(): Promise<void>;
|
|
26
|
-
disconnect(): Promise<void>;
|
|
27
|
-
$formatMessage(event: {
|
|
28
|
-
content: string;
|
|
29
|
-
ts: number;
|
|
30
|
-
}): Message<{
|
|
31
|
-
content: string;
|
|
32
|
-
ts: number;
|
|
33
|
-
}>;
|
|
34
|
-
get control(): {
|
|
35
|
-
recall: (message: {
|
|
36
|
-
id: string;
|
|
37
|
-
}) => Promise<void>;
|
|
38
|
-
};
|
|
39
|
-
$recallMessage(id: string): Promise<void>;
|
|
40
|
-
$sendMessage(options: SendOptions): Promise<string>;
|
|
41
|
-
$connect(): Promise<void>;
|
|
42
|
-
$disconnect(): Promise<void>;
|
|
43
|
-
}
|
|
44
|
-
export declare class ProcessAdapter extends Adapter<ProcessEndpoint> {
|
|
45
|
-
static readonly capabilities: readonly ["inbound", "outbound"];
|
|
46
|
-
static outboundRichSegmentPolicy: OutboundRichSegmentPolicy;
|
|
47
|
-
constructor(plugin: Plugin);
|
|
48
|
-
/**
|
|
49
|
-
* process 出站由 $sendMessage 写入 stdout;父类 INFO preview 会与终端输出重复,故仅 debug 元数据。
|
|
50
|
-
*/
|
|
51
|
-
sendMessage(options: SendOptions): Promise<string>;
|
|
52
|
-
createEndpoint(config: Adapter.EndpointConfig<ProcessEndpoint>): ProcessEndpoint;
|
|
53
|
-
}
|
|
54
|
-
declare module '../adapter.js' {
|
|
55
|
-
interface Adapters {
|
|
56
|
-
process: ProcessAdapter;
|
|
57
|
-
}
|
|
58
|
-
}
|
|
@@ -1,128 +0,0 @@
|
|
|
1
|
-
import { formatCompact } from "@zhin.js/logger";
|
|
2
|
-
import { Adapter } from "../adapter.js";
|
|
3
|
-
import { Message } from "../message.js";
|
|
4
|
-
import { segment } from "../utils.js";
|
|
5
|
-
import { assertOutbound } from "../endpoint-capabilities.js";
|
|
6
|
-
import { getOutboundReplyStore } from "./dispatcher.js";
|
|
7
|
-
import { bindStdin, runtimePid, runtimeUser } from "./runtime-io.js";
|
|
8
|
-
import { interpretOriginQrcodeForProcess } from "./rich-segments/qrcode-segment.js";
|
|
9
|
-
export class ProcessEndpoint {
|
|
10
|
-
adapter;
|
|
11
|
-
$config;
|
|
12
|
-
$id = runtimePid();
|
|
13
|
-
get logger() {
|
|
14
|
-
return this.adapter.logger;
|
|
15
|
-
}
|
|
16
|
-
$connected = false;
|
|
17
|
-
#unbindStdin = null;
|
|
18
|
-
constructor(adapter, $config = { owner: runtimePid() }) {
|
|
19
|
-
this.adapter = adapter;
|
|
20
|
-
this.$config = $config;
|
|
21
|
-
this.$listenInput = this.$listenInput.bind(this);
|
|
22
|
-
}
|
|
23
|
-
$listenInput(chunk) {
|
|
24
|
-
const content = chunk.trim();
|
|
25
|
-
if (content) {
|
|
26
|
-
this.adapter.emit('message.receive', this.$formatMessage({ content, ts: Date.now() }));
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
async connect() {
|
|
30
|
-
}
|
|
31
|
-
async disconnect() {
|
|
32
|
-
}
|
|
33
|
-
$formatMessage(event) {
|
|
34
|
-
const base = {
|
|
35
|
-
$id: `${event.ts}`,
|
|
36
|
-
$adapter: 'process',
|
|
37
|
-
$endpoint: runtimePid(),
|
|
38
|
-
$sender: {
|
|
39
|
-
id: runtimeUser(),
|
|
40
|
-
name: runtimeUser(),
|
|
41
|
-
role: 'master',
|
|
42
|
-
},
|
|
43
|
-
$channel: {
|
|
44
|
-
id: runtimeUser(),
|
|
45
|
-
type: 'private',
|
|
46
|
-
},
|
|
47
|
-
$content: [{ type: 'text', data: { text: event.content } }],
|
|
48
|
-
$raw: event.content,
|
|
49
|
-
$timestamp: event.ts,
|
|
50
|
-
$recall: async () => {
|
|
51
|
-
await this.$recallMessage(base.$id);
|
|
52
|
-
},
|
|
53
|
-
$reply: async (content) => {
|
|
54
|
-
return await this.adapter.sendMessage({
|
|
55
|
-
context: 'process',
|
|
56
|
-
endpoint: base.$endpoint,
|
|
57
|
-
content,
|
|
58
|
-
id: base.$sender.id,
|
|
59
|
-
type: base.$channel.type,
|
|
60
|
-
});
|
|
61
|
-
},
|
|
62
|
-
};
|
|
63
|
-
return Message.from(event, base);
|
|
64
|
-
}
|
|
65
|
-
get control() {
|
|
66
|
-
return {
|
|
67
|
-
recall: async (message) => this.$recallMessage(message.id),
|
|
68
|
-
};
|
|
69
|
-
}
|
|
70
|
-
async $recallMessage(id) {
|
|
71
|
-
}
|
|
72
|
-
async $sendMessage(options) {
|
|
73
|
-
const content = await interpretOriginQrcodeForProcess(options.content ?? '');
|
|
74
|
-
if (content != null) {
|
|
75
|
-
const preview = segment.raw(content);
|
|
76
|
-
if (preview) {
|
|
77
|
-
process.stdout.write(`${preview}\n`);
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
return `${Date.now()}`;
|
|
81
|
-
}
|
|
82
|
-
async $connect() {
|
|
83
|
-
this.#unbindStdin = bindStdin((chunk) => this.$listenInput(chunk));
|
|
84
|
-
this.$connected = true;
|
|
85
|
-
}
|
|
86
|
-
async $disconnect() {
|
|
87
|
-
this.#unbindStdin?.();
|
|
88
|
-
this.#unbindStdin = null;
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
export class ProcessAdapter extends Adapter {
|
|
92
|
-
static capabilities = ['inbound', 'outbound'];
|
|
93
|
-
static outboundRichSegmentPolicy = {
|
|
94
|
-
qrcode: 'origin',
|
|
95
|
-
html: 'text',
|
|
96
|
-
markdown: 'text',
|
|
97
|
-
};
|
|
98
|
-
constructor(plugin) {
|
|
99
|
-
super(plugin, 'process', [{ owner: runtimePid() }]);
|
|
100
|
-
}
|
|
101
|
-
/**
|
|
102
|
-
* process 出站由 $sendMessage 写入 stdout;父类 INFO preview 会与终端输出重复,故仅 debug 元数据。
|
|
103
|
-
*/
|
|
104
|
-
async sendMessage(options) {
|
|
105
|
-
options = await this.renderSendMessage(options);
|
|
106
|
-
const endpoint = this.endpoints.get(options.endpoint);
|
|
107
|
-
if (!endpoint)
|
|
108
|
-
throw new Error(`Endpoint ${options.endpoint} not found`);
|
|
109
|
-
assertOutbound(endpoint);
|
|
110
|
-
this.logger.debug(formatCompact({
|
|
111
|
-
send: `${options.type}(${options.id})`,
|
|
112
|
-
endpoint: options.endpoint,
|
|
113
|
-
}));
|
|
114
|
-
const messageId = await endpoint.$sendMessage(options);
|
|
115
|
-
const replyStore = getOutboundReplyStore();
|
|
116
|
-
this.plugin.root.dispatch('message.send', {
|
|
117
|
-
adapter: this.name,
|
|
118
|
-
options,
|
|
119
|
-
messageId,
|
|
120
|
-
replySource: replyStore?.source,
|
|
121
|
-
replyMessage: replyStore?.message,
|
|
122
|
-
});
|
|
123
|
-
return messageId;
|
|
124
|
-
}
|
|
125
|
-
createEndpoint(config) {
|
|
126
|
-
return new ProcessEndpoint(this, config);
|
|
127
|
-
}
|
|
128
|
-
}
|
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* AgentPreset — 插件声明的专长 Agent 预设
|
|
3
|
-
*
|
|
4
|
-
* 核心概念:
|
|
5
|
-
* Tool = 单个 AI 可调用的能力(函数)
|
|
6
|
-
* Skill = 一组相关 Tool + 发现元数据(SKILL.md)
|
|
7
|
-
* Agent = 针对特定领域的 Agent 预设(*.agent.md)
|
|
8
|
-
*
|
|
9
|
-
* Agent 预设以标准 *.agent.md 文件提供,框架自动扫描发现。
|
|
10
|
-
* 主 Agent(ZhinAgent)可根据用户请求自动委派给匹配的专长 Agent。
|
|
11
|
-
*/
|
|
12
|
-
import { Feature, type FeatureJSON } from '@zhin.js/kernel';
|
|
13
|
-
import type { Tool } from '../types.js';
|
|
14
|
-
/**
|
|
15
|
-
* Agent 预设(从 *.agent.md 文件解析或编程式注册)
|
|
16
|
-
*/
|
|
17
|
-
export interface AgentPreset {
|
|
18
|
-
/** 预设名称(唯一标识) */
|
|
19
|
-
name: string;
|
|
20
|
-
/** 描述(AI 用来判断何时委派给此 Agent) */
|
|
21
|
-
description: string;
|
|
22
|
-
/** 自定义系统提示词(从 agent.md body 提取或显式指定) */
|
|
23
|
-
systemPrompt?: string;
|
|
24
|
-
/** 关联的工具列表 */
|
|
25
|
-
tools?: Tool[];
|
|
26
|
-
/** 触发关键词(用户消息匹配时优先考虑此 Agent) */
|
|
27
|
-
keywords?: string[];
|
|
28
|
-
/** 分类标签 */
|
|
29
|
-
tags?: string[];
|
|
30
|
-
/** 首选模型名 */
|
|
31
|
-
model?: string;
|
|
32
|
-
/** 首选 Provider 名 */
|
|
33
|
-
provider?: string;
|
|
34
|
-
/** 最大工具调用迭代次数 */
|
|
35
|
-
maxIterations?: number;
|
|
36
|
-
/** 来源插件名(由框架自动填充) */
|
|
37
|
-
pluginName?: string;
|
|
38
|
-
/** *.agent.md 文件的绝对路径 */
|
|
39
|
-
filePath?: string;
|
|
40
|
-
}
|
|
41
|
-
declare module '../plugin.js' {
|
|
42
|
-
namespace Plugin {
|
|
43
|
-
interface Contexts {
|
|
44
|
-
agentPreset: AgentPresetFeature;
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
export declare class AgentPresetFeature extends Feature<AgentPreset> {
|
|
49
|
-
#private;
|
|
50
|
-
readonly name: "agentPreset";
|
|
51
|
-
readonly icon = "Endpoint";
|
|
52
|
-
readonly desc = "Agent \u9884\u8BBE";
|
|
53
|
-
readonly byName: Map<string, AgentPreset>;
|
|
54
|
-
add(preset: AgentPreset, pluginName: string): () => void;
|
|
55
|
-
remove(preset: AgentPreset, pluginName?: string): boolean;
|
|
56
|
-
get(name: string): AgentPreset | undefined;
|
|
57
|
-
/** 清理所有预设注册(热重载时由 Plugin.stop() 调用) */
|
|
58
|
-
dispose(): void;
|
|
59
|
-
getAll(): AgentPreset[];
|
|
60
|
-
search(query: string, options?: {
|
|
61
|
-
maxResults?: number;
|
|
62
|
-
}): AgentPreset[];
|
|
63
|
-
toJSON(pluginName?: string): FeatureJSON;
|
|
64
|
-
}
|