dsh-session-webhook 0.0.0-stage → 0.2.0
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/LICENSE +21 -0
- package/README.md +412 -2
- package/cordis.patch.yml +8 -0
- package/docs/ACCEPTANCE.md +158 -0
- package/docs/DESIGN.md +136 -0
- package/docs/TOOL-CONTRACT.md +315 -0
- package/icon.svg +13 -0
- package/lib/bases.js +527 -0
- package/lib/client.js +1755 -0
- package/lib/control.js +321 -0
- package/lib/deliver.js +272 -0
- package/lib/errors.js +62 -0
- package/lib/http.js +258 -0
- package/lib/index.js +220 -0
- package/lib/store.js +381 -0
- package/lib/tools.js +433 -0
- package/lib/types.js +1 -0
- package/lib/urlfile.js +290 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +72 -4
- package/scripts/cold-resume-health.mjs +268 -0
- package/scripts/verify-client.mjs +1199 -0
- package/scripts/verify-health-contract.mjs +173 -0
- package/scripts/verify-host.mjs +1322 -0
package/lib/tools.js
ADDED
|
@@ -0,0 +1,433 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 三个 agent 工具:`session_webhook_create` / `session_webhook_list` / `session_webhook_revoke`。
|
|
3
|
+
*
|
|
4
|
+
* 与 UI 管理面共用同一份 store、同一套入口解析与 urlFile 重写;区别只在两处访问约束:
|
|
5
|
+
*
|
|
6
|
+
* 1. **没有 `sessionId` 参数**:会话一律取 `exec.agent.id`。跨会话给端点等于向别的
|
|
7
|
+
* agent 上下文注入消息,这条路径在 v2 不存在(不是默认关,是没有)。
|
|
8
|
+
* 2. **没有 `reveal` 参数**:完整 URL 永不进 tool result(会进 transcript 并发给模型
|
|
9
|
+
* 供应商),只回 `maskedUrl`(前 4 后 4)与 urlFile 路径。这不是"禁止 agent 看到"
|
|
10
|
+
* (它有 fs 工具,读一下就能看到),而是默认路径不经过模型。
|
|
11
|
+
*
|
|
12
|
+
* 撤销权限:工具只能撤 `createdBy === 'agent'` 的端点;UI 建的要报错并提示去界面撤销
|
|
13
|
+
* ——避免 agent 静默拆掉人手工配的链路(如 GitHub 通知脚本的 `github-notify`)。
|
|
14
|
+
*
|
|
15
|
+
* **错误出口(所有工具共用 `withToolErrors`)**:`defineTool` 把 execute 的抛出物转成
|
|
16
|
+
* isError 时只保留 message 文本(`@deepseek-ai/dsh-tools` 的 failure 归一化),`code` 与
|
|
17
|
+
* `details` 都不会进模型上下文。所以这里统一把领域错误重抛成
|
|
18
|
+
* `[<code>] <中文说明>(details 进文本)`,真实 bug 重抛成 `[internal] <原始说明>`:
|
|
19
|
+
* 模型因此能用固定前缀解析 code 决定「换名字 / 去界面撤销 / 重试」,同时不把内部异常
|
|
20
|
+
* 伪装成业务错误。
|
|
21
|
+
*
|
|
22
|
+
* `@deepseek-ai/dsh-tools` 是**可选**依赖:没有 tools 服务的 profile 也要能起来。
|
|
23
|
+
* 因此本模块不写进 `inject`,由 index.ts 用 `ctx.get('tools')` 拿到后按需注册,
|
|
24
|
+
* 并用结构化类型守卫窄化(cordis 的 `get` 返回 `any`,这里立刻收成 `unknown`)。
|
|
25
|
+
*/
|
|
26
|
+
import { defineTool } from '@deepseek-ai/dsh-tools';
|
|
27
|
+
import { InjectError, fail } from './errors.js';
|
|
28
|
+
import { hookView, normalizeMode, normalizeName, normalizeTags } from './store.js';
|
|
29
|
+
/**
|
|
30
|
+
* 工具错误出口的载体:`message` 形如 `[<code>] <中文说明>`,同时保留结构化的 `code`
|
|
31
|
+
* 供离线测试断言(框架只把 message 交给模型,`code` 字段本身到不了模型)。
|
|
32
|
+
*/
|
|
33
|
+
export class ToolError extends Error {
|
|
34
|
+
/** 稳定错误码(`internal` 表示真实 bug,不是业务错误)。 */
|
|
35
|
+
code;
|
|
36
|
+
/** 原始异常,便于宿主日志排查(不透给模型)。 */
|
|
37
|
+
cause;
|
|
38
|
+
/**
|
|
39
|
+
* @param code 稳定错误码。
|
|
40
|
+
* @param message 已经带 `[<code>]` 前缀的完整 message。
|
|
41
|
+
* @param cause 原始抛出物。
|
|
42
|
+
*/
|
|
43
|
+
constructor(code, message, cause) {
|
|
44
|
+
super(message);
|
|
45
|
+
this.name = 'ToolError';
|
|
46
|
+
this.code = code;
|
|
47
|
+
this.cause = cause;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* 把一个 `InjectError` 折成模型可见的错误文本(`details` 一并进文本)。
|
|
52
|
+
* @param error 领域错误。
|
|
53
|
+
* @returns 带 `[<code>]` 前缀的新错误。
|
|
54
|
+
*/
|
|
55
|
+
function toolErrorFromInject(error) {
|
|
56
|
+
return new ToolError(error.code, `[${error.code}] ${error.message}${formatDetails(error.message, error.details)}`, error);
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* 把细节字段拼成人类可读后缀;**已经出现在 message 里的值不重复拼**(例如 `name-taken`
|
|
60
|
+
* 的 message 本来就带 id 与打码地址)。
|
|
61
|
+
* @param message 原始中文说明。
|
|
62
|
+
* @param details 领域错误的附加细节。
|
|
63
|
+
* @returns `"(k=v,k2=v2)"` 或空串。
|
|
64
|
+
*/
|
|
65
|
+
function formatDetails(message, details) {
|
|
66
|
+
const parts = [];
|
|
67
|
+
for (const [key, value] of Object.entries(details)) {
|
|
68
|
+
if (typeof value !== 'string' && typeof value !== 'number' && typeof value !== 'boolean')
|
|
69
|
+
continue;
|
|
70
|
+
const text = String(value);
|
|
71
|
+
if (text === '' || message.includes(text))
|
|
72
|
+
continue;
|
|
73
|
+
parts.push(`${key}=${text}`);
|
|
74
|
+
}
|
|
75
|
+
return parts.length === 0 ? '' : `(${parts.join(',')})`;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* 三个工具共用的错误出口。
|
|
79
|
+
*
|
|
80
|
+
* - `InjectError` → `[<code>] <中文说明>`(details 追加进文本);
|
|
81
|
+
* - 其它异常 → `[internal] <原始说明>`,code 固定 `internal`:真实 bug 不冒充业务错误,
|
|
82
|
+
* 但模型仍能看出「这不是你调用方式的问题」。
|
|
83
|
+
* @param action 工具主体。
|
|
84
|
+
* @returns 工具主体的返回值。
|
|
85
|
+
*/
|
|
86
|
+
export async function withToolErrors(action) {
|
|
87
|
+
try {
|
|
88
|
+
return await action();
|
|
89
|
+
}
|
|
90
|
+
catch (error) {
|
|
91
|
+
if (error instanceof InjectError)
|
|
92
|
+
throw toolErrorFromInject(error);
|
|
93
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
94
|
+
throw new ToolError('internal', `[internal] ${message}`, error);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* 结构化窄化 `ctx.get('tools')`:只认「有 register 函数」的服务。
|
|
99
|
+
* 原因:cordis 的 `get` 签名返回 `any`,直接把 `any` 传下去会让本工程的
|
|
100
|
+
* `strict` 约束失效;这里用 unknown + 类型守卫,把判断写在一个地方。
|
|
101
|
+
* @param value 任意宿主返回值。
|
|
102
|
+
* @returns 是否是工具注册面。
|
|
103
|
+
*/
|
|
104
|
+
export function isToolRegistry(value) {
|
|
105
|
+
if (value === null || typeof value !== 'object')
|
|
106
|
+
return false;
|
|
107
|
+
const candidate = value;
|
|
108
|
+
return typeof candidate.register === 'function';
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* 取调用会话 id;工具没有 sessionId 参数,这是唯一来源。
|
|
112
|
+
* @param exec 工具执行上下文。
|
|
113
|
+
* @returns 会话 id。
|
|
114
|
+
*/
|
|
115
|
+
function sessionIdOf(exec) {
|
|
116
|
+
const agent = exec.agent;
|
|
117
|
+
if (agent === undefined || typeof agent.id !== 'string' || agent.id === '') {
|
|
118
|
+
throw fail(400, 'no-session', '该工具只能由会话内的 agent 调用(缺少 exec.agent.id)');
|
|
119
|
+
}
|
|
120
|
+
return agent.id;
|
|
121
|
+
}
|
|
122
|
+
/** 端点投影:store 视图 + 工具口径的 `mode` 字段名。 */
|
|
123
|
+
function endpointView(hook) {
|
|
124
|
+
const view = hookView(hook);
|
|
125
|
+
return {
|
|
126
|
+
id: view.id,
|
|
127
|
+
sessionId: view.sessionId,
|
|
128
|
+
name: view.name,
|
|
129
|
+
mode: view.defaultMode,
|
|
130
|
+
tags: [...view.tags],
|
|
131
|
+
enabled: view.enabled,
|
|
132
|
+
maskedUrl: view.maskedUrl,
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
/** 端点公共字段的 schema 片段(create 与 list 共用)。 */
|
|
136
|
+
function endpointFields() {
|
|
137
|
+
return {
|
|
138
|
+
id: { type: 'string', required: true },
|
|
139
|
+
sessionId: { type: 'string', required: true },
|
|
140
|
+
name: { type: 'string', required: true },
|
|
141
|
+
mode: { type: 'string', required: true },
|
|
142
|
+
tags: { type: 'array', items: { type: 'string' }, required: true },
|
|
143
|
+
enabled: { type: 'boolean', required: true },
|
|
144
|
+
maskedUrl: { type: 'string', required: true },
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
/** 可空字符串字段的 schema 片段(最近使用时间等未使用过时为 null)。 */
|
|
148
|
+
function nullableString() {
|
|
149
|
+
return {
|
|
150
|
+
oneOf: [{ type: 'string' }, { type: 'null' }],
|
|
151
|
+
required: true,
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
/** list 返回的单个端点(公共字段 + 归属与使用情况 + urlFile 路径)。 */
|
|
155
|
+
function listedEndpointFields() {
|
|
156
|
+
return {
|
|
157
|
+
...endpointFields(),
|
|
158
|
+
createdBy: { type: 'string', required: true },
|
|
159
|
+
createdAt: { type: 'string', required: true },
|
|
160
|
+
lastUsedAt: nullableString(),
|
|
161
|
+
lastStatus: nullableString(),
|
|
162
|
+
deliveryCount: { type: 'integer', required: true },
|
|
163
|
+
urlFiles: { type: 'object', additionalProperties: true, required: true },
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* 注册三个工具。
|
|
168
|
+
* @param options 宿主面(tools / store / 现行配置 / urlFile 管理器)。
|
|
169
|
+
* @returns 一次性的 disposer,撤销全部三个注册。
|
|
170
|
+
*/
|
|
171
|
+
export function registerWebhookTools(options) {
|
|
172
|
+
const { tools, store, getConfig, getUrlFiles, logger } = options;
|
|
173
|
+
const create = defineTool({
|
|
174
|
+
name: 'session_webhook_create',
|
|
175
|
+
description: '为**当前会话**创建一条入站 webhook 端点,并把完整 URL 写到本机 urlFile(返回值里只给打码地址与文件路径,'
|
|
176
|
+
+ '不返回完整 URL)。端点的用途是让外部脚本(CI、监控、通知)向本会话投递一条消息:持有该 URL 即可投递,'
|
|
177
|
+
+ '但不能读会话、不能列/改其它端点。一个会话可以有多条端点,按 name 区分。'
|
|
178
|
+
+ '同名端点已存在时默认报 name-taken(错误里带 id 与打码地址);传 reuse: true 可复用已有端点(created: false),'
|
|
179
|
+
+ '复用不会修改它的标签/模式/创建者。本工具创建的端点(createdBy: agent)只能由本工具撤销,UI 建的需在界面撤销。'
|
|
180
|
+
+ '失败时返回的错误消息固定以 `[<稳定错误码>]` 开头(例如 `[ui-owned-endpoint]`、`[name-taken]`),可据此决定下一步(换名字 / 去界面撤销 / 重试)。',
|
|
181
|
+
parameters: {
|
|
182
|
+
name: {
|
|
183
|
+
type: 'string',
|
|
184
|
+
required: true,
|
|
185
|
+
description: '端点名(当前会话内唯一,如 github-notify、gitlab-ci)。用作消息标签与 urlFile 文件名的一部分。',
|
|
186
|
+
},
|
|
187
|
+
tags: {
|
|
188
|
+
type: 'array',
|
|
189
|
+
items: { type: 'string' },
|
|
190
|
+
description: '可选标签,最多 6 个(如 ["github","notify"]),只用于列表过滤与可读性。',
|
|
191
|
+
},
|
|
192
|
+
mode: {
|
|
193
|
+
type: 'string',
|
|
194
|
+
enum: ['steer', 'queue', 'passive'],
|
|
195
|
+
description: '端点默认投递模式:steer 插话(最近步骤边界,空闲则立即开一轮)、queue 排队(下一整轮)、'
|
|
196
|
+
+ 'passive 静默(只进上下文,不唤醒)。默认 steer。',
|
|
197
|
+
},
|
|
198
|
+
reuse: {
|
|
199
|
+
type: 'boolean',
|
|
200
|
+
description: '同名端点已存在时是否直接复用(true)还是报错(默认 false)。复用不会修改已有端点的配置。',
|
|
201
|
+
},
|
|
202
|
+
},
|
|
203
|
+
output: {
|
|
204
|
+
schema: {
|
|
205
|
+
type: 'object',
|
|
206
|
+
properties: {
|
|
207
|
+
created: { type: 'boolean', required: true },
|
|
208
|
+
endpoint: {
|
|
209
|
+
type: 'object',
|
|
210
|
+
properties: endpointFields(),
|
|
211
|
+
additionalProperties: false,
|
|
212
|
+
required: true,
|
|
213
|
+
},
|
|
214
|
+
defaultBase: { type: 'string', required: true },
|
|
215
|
+
urlFiles: { type: 'object', additionalProperties: true, required: true },
|
|
216
|
+
},
|
|
217
|
+
additionalProperties: false,
|
|
218
|
+
},
|
|
219
|
+
render: (_args, value) => [{
|
|
220
|
+
type: 'text',
|
|
221
|
+
text: value.created
|
|
222
|
+
? `已创建 webhook 端点「${value.endpoint.name}」(id: ${value.endpoint.id},模式 ${value.endpoint.mode},`
|
|
223
|
+
+ `打码地址 ${value.endpoint.maskedUrl})。完整 URL 已写到本机 urlFile(默认入口 ${value.defaultBase}):`
|
|
224
|
+
+ `${formatUrlFiles(value.urlFiles)}。把该文件内容交给外部脚本即可投递;撤销用 session_webhook_revoke。`
|
|
225
|
+
: `已复用当前会话已有的 webhook 端点「${value.endpoint.name}」(id: ${value.endpoint.id},`
|
|
226
|
+
+ `创建者见 list,模式 ${value.endpoint.mode},打码地址 ${value.endpoint.maskedUrl})。urlFile:`
|
|
227
|
+
+ `${formatUrlFiles(value.urlFiles)}。`,
|
|
228
|
+
}],
|
|
229
|
+
},
|
|
230
|
+
async execute(args, exec) {
|
|
231
|
+
return withToolErrors(async () => {
|
|
232
|
+
const sessionId = sessionIdOf(exec);
|
|
233
|
+
const name = normalizeName(args.name);
|
|
234
|
+
const tags = normalizeTags(args.tags);
|
|
235
|
+
const defaultMode = normalizeMode(args.mode);
|
|
236
|
+
const config = getConfig();
|
|
237
|
+
const urlFiles = getUrlFiles();
|
|
238
|
+
// 先确认入口集合可解析(direct origin 现取):取不到就别建端点,
|
|
239
|
+
// 避免「建了却写不出 urlFile」。显式配置了 direct 时不依赖 origin。
|
|
240
|
+
config.bases.bases();
|
|
241
|
+
const existing = store.list(sessionId).find((hook) => hook.name === name);
|
|
242
|
+
let hook;
|
|
243
|
+
let created;
|
|
244
|
+
if (existing !== undefined) {
|
|
245
|
+
if (args.reuse !== true) {
|
|
246
|
+
const maskedUrl = hookView(existing).maskedUrl;
|
|
247
|
+
throw fail(409, 'name-taken', `当前会话已有名为「${name}」的端点(id: ${existing.id},打码地址:${maskedUrl})。`
|
|
248
|
+
+ '如需直接复用它,请传 reuse: true;要新建请换一个名字。', { hookId: existing.id, maskedUrl });
|
|
249
|
+
}
|
|
250
|
+
hook = existing;
|
|
251
|
+
created = false;
|
|
252
|
+
}
|
|
253
|
+
else {
|
|
254
|
+
hook = await store.create({
|
|
255
|
+
sessionId,
|
|
256
|
+
name,
|
|
257
|
+
tags,
|
|
258
|
+
defaultMode,
|
|
259
|
+
// UI 建的是 'ui',工具建的是 'agent';撤销权限按这个字段判定。
|
|
260
|
+
createdBy: 'agent',
|
|
261
|
+
});
|
|
262
|
+
created = true;
|
|
263
|
+
}
|
|
264
|
+
const urls = config.bases.urlsFor(config.basePath, hook.token);
|
|
265
|
+
// 写是幂等的,并且会顺手清掉该端点已失效的入口文件:复用分支也重写一次。
|
|
266
|
+
const written = await urlFiles.write(hook.id, urls);
|
|
267
|
+
const expected = Object.keys(urls).length;
|
|
268
|
+
if (Object.keys(written).length < expected) {
|
|
269
|
+
logger?.warn?.(`[session-inject] 端点 ${hook.id} 的 urlFile 未全部写入(${Object.keys(written).length}/${expected})`);
|
|
270
|
+
}
|
|
271
|
+
return {
|
|
272
|
+
created,
|
|
273
|
+
endpoint: endpointView(hook),
|
|
274
|
+
defaultBase: config.bases.defaultBase,
|
|
275
|
+
urlFiles: written,
|
|
276
|
+
};
|
|
277
|
+
});
|
|
278
|
+
},
|
|
279
|
+
});
|
|
280
|
+
const list = defineTool({
|
|
281
|
+
name: 'session_webhook_list',
|
|
282
|
+
description: '列出**当前会话**的全部 webhook 端点(包括在 Web 界面里创建的)。返回每条端点的 id、名称、模式、标签、'
|
|
283
|
+
+ '是否启用、创建者(ui/agent)、打码地址、最近使用情况与已存在的 urlFile 路径;不返回完整 URL。'
|
|
284
|
+
+ '只有 createdBy 为 agent 的端点可以用 session_webhook_revoke 撤销。'
|
|
285
|
+
+ '失败时返回的错误消息固定以 `[<稳定错误码>]` 开头(例如 `[ui-owned-endpoint]`、`[name-taken]`),可据此决定下一步(换名字 / 去界面撤销 / 重试)。',
|
|
286
|
+
parameters: {},
|
|
287
|
+
output: {
|
|
288
|
+
schema: {
|
|
289
|
+
type: 'object',
|
|
290
|
+
properties: {
|
|
291
|
+
endpoints: {
|
|
292
|
+
type: 'array',
|
|
293
|
+
items: {
|
|
294
|
+
type: 'object',
|
|
295
|
+
properties: listedEndpointFields(),
|
|
296
|
+
additionalProperties: false,
|
|
297
|
+
},
|
|
298
|
+
required: true,
|
|
299
|
+
},
|
|
300
|
+
},
|
|
301
|
+
additionalProperties: false,
|
|
302
|
+
},
|
|
303
|
+
render: (_args, value) => {
|
|
304
|
+
if (value.endpoints.length === 0) {
|
|
305
|
+
return [{ type: 'text', text: '当前会话还没有 webhook 端点;可用 session_webhook_create 创建。' }];
|
|
306
|
+
}
|
|
307
|
+
const lines = value.endpoints.map((endpoint) => {
|
|
308
|
+
const files = formatUrlFiles(endpoint.urlFiles);
|
|
309
|
+
return `- ${endpoint.name}(id: ${endpoint.id},${endpoint.mode},${endpoint.createdBy} 建,`
|
|
310
|
+
+ `${endpoint.enabled ? '启用' : '停用'},投递 ${endpoint.deliveryCount} 次`
|
|
311
|
+
+ `${endpoint.lastStatus === null ? '' : `,最近 ${endpoint.lastStatus}`})urlFile:${files}`;
|
|
312
|
+
});
|
|
313
|
+
return [{ type: 'text', text: `当前会话共 ${value.endpoints.length} 个 webhook 端点:\n${lines.join('\n')}` }];
|
|
314
|
+
},
|
|
315
|
+
},
|
|
316
|
+
async execute(_args, exec) {
|
|
317
|
+
return withToolErrors(async () => {
|
|
318
|
+
const sessionId = sessionIdOf(exec);
|
|
319
|
+
const config = getConfig();
|
|
320
|
+
const urlFiles = getUrlFiles();
|
|
321
|
+
const baseNames = config.bases.names();
|
|
322
|
+
const endpoints = [];
|
|
323
|
+
for (const hook of store.list(sessionId)) {
|
|
324
|
+
const endpoint = endpointView(hook);
|
|
325
|
+
endpoints.push({
|
|
326
|
+
...endpoint,
|
|
327
|
+
createdBy: hook.createdBy ?? 'ui',
|
|
328
|
+
createdAt: hook.createdAt,
|
|
329
|
+
lastUsedAt: hook.lastUsedAt ?? null,
|
|
330
|
+
lastStatus: hook.lastStatus ?? null,
|
|
331
|
+
deliveryCount: hook.deliveryCount ?? 0,
|
|
332
|
+
urlFiles: await urlFiles.existing(hook.id, baseNames),
|
|
333
|
+
});
|
|
334
|
+
}
|
|
335
|
+
return { endpoints };
|
|
336
|
+
});
|
|
337
|
+
},
|
|
338
|
+
});
|
|
339
|
+
const revoke = defineTool({
|
|
340
|
+
name: 'session_webhook_revoke',
|
|
341
|
+
description: '撤销**当前会话**的一条 webhook 端点(URL 立即失效,同会话其它端点不受影响),并删除它的 urlFile。'
|
|
342
|
+
+ '参数 id 与 name 只能给一个。只能撤销 createdBy 为 agent(即本工具或同会话 agent 创建)的端点;'
|
|
343
|
+
+ 'UI 里创建的端点会报 ui-owned-endpoint,请在会话菜单「Webhook 端点…」里删除。'
|
|
344
|
+
+ '失败时返回的错误消息固定以 `[<稳定错误码>]` 开头(例如 `[ui-owned-endpoint]`、`[name-taken]`),可据此决定下一步(换名字 / 去界面撤销 / 重试)。',
|
|
345
|
+
parameters: {
|
|
346
|
+
id: { type: 'string', description: '端点 id(与 name 二选一)。' },
|
|
347
|
+
name: { type: 'string', description: '端点名(与 id 二选一)。' },
|
|
348
|
+
},
|
|
349
|
+
output: {
|
|
350
|
+
schema: {
|
|
351
|
+
type: 'object',
|
|
352
|
+
properties: {
|
|
353
|
+
revoked: { type: 'boolean', required: true },
|
|
354
|
+
id: { type: 'string', required: true },
|
|
355
|
+
name: { type: 'string', required: true },
|
|
356
|
+
},
|
|
357
|
+
additionalProperties: false,
|
|
358
|
+
},
|
|
359
|
+
render: (_args, value) => [{
|
|
360
|
+
type: 'text',
|
|
361
|
+
text: `已撤销 webhook 端点「${value.name}」(id: ${value.id}):该 URL 立即失效,对应的 urlFile 已删除。`,
|
|
362
|
+
}],
|
|
363
|
+
},
|
|
364
|
+
async execute(args, exec) {
|
|
365
|
+
return withToolErrors(async () => {
|
|
366
|
+
const sessionId = sessionIdOf(exec);
|
|
367
|
+
const id = args.id === undefined ? undefined : args.id.trim();
|
|
368
|
+
const name = args.name === undefined ? undefined : args.name.trim();
|
|
369
|
+
// schema 不能表达互斥,这里做运行时校验并给出明确报错。
|
|
370
|
+
if (id !== undefined && name !== undefined) {
|
|
371
|
+
throw fail(400, 'bad-request', 'id 与 name 只能提供一个');
|
|
372
|
+
}
|
|
373
|
+
if (id === undefined && name === undefined) {
|
|
374
|
+
throw fail(400, 'bad-request', '必须提供 id 或 name 之一');
|
|
375
|
+
}
|
|
376
|
+
const hook = resolveOwnedEndpoint(store, sessionId, { id, name });
|
|
377
|
+
if ((hook.createdBy ?? 'ui') !== 'agent') {
|
|
378
|
+
throw fail(409, 'ui-owned-endpoint', `端点「${hook.name}」由界面创建,工具不能撤销。请在会话菜单「Webhook 端点…」里删除它。`);
|
|
379
|
+
}
|
|
380
|
+
await store.revoke(hook.id);
|
|
381
|
+
// 端点已经删了(URL 立即失效):urlFile 清理失败只告警,不让工具报「撤销失败」
|
|
382
|
+
// 诱导模型重试(重试只会拿到 unknown-endpoint)。
|
|
383
|
+
try {
|
|
384
|
+
await getUrlFiles().remove(hook.id);
|
|
385
|
+
}
|
|
386
|
+
catch (error) {
|
|
387
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
388
|
+
logger?.warn?.(`[session-inject] 端点 ${hook.id} 已撤销,但 urlFile 清理失败:${reason}`);
|
|
389
|
+
}
|
|
390
|
+
return { revoked: true, id: hook.id, name: hook.name };
|
|
391
|
+
});
|
|
392
|
+
},
|
|
393
|
+
});
|
|
394
|
+
const disposers = [tools.register(create), tools.register(list), tools.register(revoke)];
|
|
395
|
+
return () => {
|
|
396
|
+
for (const dispose of disposers.reverse())
|
|
397
|
+
dispose();
|
|
398
|
+
};
|
|
399
|
+
}
|
|
400
|
+
/**
|
|
401
|
+
* 按 id 或 name 找到当前会话内的端点。
|
|
402
|
+
*
|
|
403
|
+
* **查找是有意会话作用域的**:别的会话的端点一律报 `unknown-endpoint`,不区分
|
|
404
|
+
* 「存在但不属于你」。这是刻意的安全选择——一旦区分两者,就提供了跨会话枚举通道
|
|
405
|
+
* (拿名字或 id 逐个试探,就能问出别的会话有没有这个端点)。不要为了「报错更准」
|
|
406
|
+
* 把这个语义优化掉。
|
|
407
|
+
* @param store 端点注册表。
|
|
408
|
+
* @param sessionId 调用方会话 id。
|
|
409
|
+
* @param selector 已校验互斥的选择器。
|
|
410
|
+
* @returns 命中的端点记录。
|
|
411
|
+
*/
|
|
412
|
+
function resolveOwnedEndpoint(store, sessionId, selector) {
|
|
413
|
+
if (selector.id !== undefined) {
|
|
414
|
+
const hook = store.get(selector.id);
|
|
415
|
+
if (hook === undefined || hook.sessionId !== sessionId) {
|
|
416
|
+
throw fail(404, 'unknown-endpoint', `当前会话没有 id 为 ${selector.id} 的端点`);
|
|
417
|
+
}
|
|
418
|
+
return hook;
|
|
419
|
+
}
|
|
420
|
+
const name = normalizeName(selector.name);
|
|
421
|
+
const hook = store.list(sessionId).find((entry) => entry.name === name);
|
|
422
|
+
if (hook === undefined) {
|
|
423
|
+
throw fail(404, 'unknown-endpoint', `当前会话没有名为「${name}」的端点`);
|
|
424
|
+
}
|
|
425
|
+
return hook;
|
|
426
|
+
}
|
|
427
|
+
/** 把 urlFile 路径表压成一行可读文本(没有文件时明确说没有)。 */
|
|
428
|
+
function formatUrlFiles(files) {
|
|
429
|
+
const entries = Object.entries(files);
|
|
430
|
+
if (entries.length === 0)
|
|
431
|
+
return '(暂无 urlFile,可能是入口未配置或写入失败,见插件日志)';
|
|
432
|
+
return entries.map(([base, path]) => `${base}=${String(path)}`).join(',');
|
|
433
|
+
}
|
package/lib/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|