@zhin.js/host-http 1.0.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 +49 -0
- package/lib/console-rest-pages.d.ts +70 -0
- package/lib/console-rest-pages.js +680 -0
- package/lib/console-rpc-extended.d.ts +36 -0
- package/lib/console-rpc-extended.js +579 -0
- package/lib/console-rpc.d.ts +98 -0
- package/lib/console-rpc.js +687 -0
- package/lib/http-host.d.ts +50 -0
- package/lib/http-host.js +410 -0
- package/lib/index.d.ts +8 -0
- package/lib/index.js +8 -0
- package/lib/json-body.d.ts +12 -0
- package/lib/json-body.js +37 -0
- package/lib/openapi.d.ts +31 -0
- package/lib/openapi.js +136 -0
- package/lib/project-files.d.ts +20 -0
- package/lib/project-files.js +145 -0
- package/lib/timing-safe-equal.d.ts +2 -0
- package/lib/timing-safe-equal.js +10 -0
- package/lib/token-registry.d.ts +20 -0
- package/lib/token-registry.js +41 -0
- package/package.json +48 -0
- package/src/console-rest-pages.ts +880 -0
- package/src/console-rpc-extended.ts +696 -0
- package/src/console-rpc.ts +759 -0
- package/src/http-host.ts +533 -0
- package/src/index.ts +63 -0
- package/src/json-body.ts +41 -0
- package/src/openapi.ts +172 -0
- package/src/project-files.ts +161 -0
- package/src/timing-safe-equal.ts +9 -0
- package/src/token-registry.ts +58 -0
|
@@ -0,0 +1,687 @@
|
|
|
1
|
+
import { dispatchExtendedConsoleRpc } from './console-rpc-extended.js';
|
|
2
|
+
/**
|
|
3
|
+
* Frontend SDK (@zhin.js/client) calls colon-named aliases; normalize to the
|
|
4
|
+
* canonical dot names before scope checks and dispatch. Alias parameter shapes
|
|
5
|
+
* (`data.{adapter, endpointId, id, type, content, parent}`) are mapped onto the
|
|
6
|
+
* `$`-prefixed fields inside each endpoint case.
|
|
7
|
+
*/
|
|
8
|
+
const RPC_TYPE_ALIASES = Object.freeze({
|
|
9
|
+
'endpoint:list': 'endpoint.list',
|
|
10
|
+
'endpoint:info': 'endpoint.info',
|
|
11
|
+
'endpoint:sendMessage': 'endpoint.send_message',
|
|
12
|
+
});
|
|
13
|
+
/** ADR 0016 demo allowlist subset available on Plugin Runtime Host. */
|
|
14
|
+
const DEMO_RPC_ALLOWLIST = new Set([
|
|
15
|
+
'ping',
|
|
16
|
+
'entries:get',
|
|
17
|
+
'pages:list',
|
|
18
|
+
'config:get',
|
|
19
|
+
'config:get-all',
|
|
20
|
+
'config:get-yaml',
|
|
21
|
+
'schema:get',
|
|
22
|
+
'schema:get-all',
|
|
23
|
+
'endpoint.list',
|
|
24
|
+
'endpoint.info',
|
|
25
|
+
'endpoint.send_message',
|
|
26
|
+
]);
|
|
27
|
+
const DEMO_RPC_WRITE_BLOCKLIST = new Set([
|
|
28
|
+
'config:set',
|
|
29
|
+
'config:save-yaml',
|
|
30
|
+
'files:save',
|
|
31
|
+
'env:save',
|
|
32
|
+
'db:insert',
|
|
33
|
+
'db:update',
|
|
34
|
+
'db:delete',
|
|
35
|
+
'db:drop-table',
|
|
36
|
+
'db:kv:set',
|
|
37
|
+
'db:kv:delete',
|
|
38
|
+
'system:restart',
|
|
39
|
+
]);
|
|
40
|
+
export function pickRpcReply(message, payloads) {
|
|
41
|
+
const requestId = message.requestId;
|
|
42
|
+
return ((requestId != null ? payloads.find((payload) => payload.requestId === requestId) : undefined)
|
|
43
|
+
?? payloads[payloads.length - 1]
|
|
44
|
+
?? null);
|
|
45
|
+
}
|
|
46
|
+
export async function dispatchRuntimeConsoleRpc(message, ctx) {
|
|
47
|
+
const payloads = [];
|
|
48
|
+
const emit = (payload) => {
|
|
49
|
+
payloads.push(payload);
|
|
50
|
+
};
|
|
51
|
+
const type = RPC_TYPE_ALIASES[String(message.type ?? '')] ?? String(message.type ?? '');
|
|
52
|
+
const requestId = message.requestId;
|
|
53
|
+
// Extended RPC surface (cron/schedule、endpoint 社交/inbox) — 自带 scope 矩阵
|
|
54
|
+
if (ctx.extended) {
|
|
55
|
+
const extended = await dispatchExtendedConsoleRpc(type, message.data, { ...ctx.extended, fullScope: ctx.authScope === 'full' });
|
|
56
|
+
if (extended !== undefined) {
|
|
57
|
+
emit('error' in extended
|
|
58
|
+
? { requestId, error: extended.error }
|
|
59
|
+
: { requestId, data: extended.data });
|
|
60
|
+
return payloads;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
if (ctx.authScope === 'demo' && (DEMO_RPC_WRITE_BLOCKLIST.has(type) || !DEMO_RPC_ALLOWLIST.has(type))) {
|
|
64
|
+
emit({ requestId, error: `Demo scope: RPC "${type}" is forbidden` });
|
|
65
|
+
return payloads;
|
|
66
|
+
}
|
|
67
|
+
switch (type) {
|
|
68
|
+
case 'ping':
|
|
69
|
+
emit({ type: 'pong', requestId });
|
|
70
|
+
return payloads;
|
|
71
|
+
case 'entries:get': {
|
|
72
|
+
const pages = await ctx.listPages();
|
|
73
|
+
emit({
|
|
74
|
+
requestId,
|
|
75
|
+
data: pages.map((page) => Object.freeze({
|
|
76
|
+
id: page.localName,
|
|
77
|
+
module: page.module,
|
|
78
|
+
order: page.order,
|
|
79
|
+
enabled: true,
|
|
80
|
+
meta: Object.freeze({ name: page.title }),
|
|
81
|
+
route: page.route,
|
|
82
|
+
hash: page.hash,
|
|
83
|
+
})),
|
|
84
|
+
});
|
|
85
|
+
return payloads;
|
|
86
|
+
}
|
|
87
|
+
case 'pages:list': {
|
|
88
|
+
const pages = await ctx.listPages();
|
|
89
|
+
emit({ requestId, data: pages });
|
|
90
|
+
return payloads;
|
|
91
|
+
}
|
|
92
|
+
case 'config:get-yaml': {
|
|
93
|
+
try {
|
|
94
|
+
const yaml = ctx.readConfigYaml ? await ctx.readConfigYaml() : '';
|
|
95
|
+
const pluginKeys = ctx.listPluginKeys ? await ctx.listPluginKeys() : [];
|
|
96
|
+
emit({ requestId, data: { yaml, pluginKeys } });
|
|
97
|
+
}
|
|
98
|
+
catch (error) {
|
|
99
|
+
emit({
|
|
100
|
+
requestId,
|
|
101
|
+
error: `Failed to read config: ${error instanceof Error ? error.message : String(error)}`,
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
return payloads;
|
|
105
|
+
}
|
|
106
|
+
case 'config:get-all': {
|
|
107
|
+
try {
|
|
108
|
+
const document = ctx.readConfigDocument ? await ctx.readConfigDocument() : {};
|
|
109
|
+
emit({ requestId, data: document });
|
|
110
|
+
}
|
|
111
|
+
catch (error) {
|
|
112
|
+
emit({
|
|
113
|
+
requestId,
|
|
114
|
+
error: `Failed to read config: ${error instanceof Error ? error.message : String(error)}`,
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
return payloads;
|
|
118
|
+
}
|
|
119
|
+
case 'config:get': {
|
|
120
|
+
try {
|
|
121
|
+
const key = String(message.key ?? message.pluginName ?? '');
|
|
122
|
+
const document = ctx.readConfigDocument ? await ctx.readConfigDocument() : {};
|
|
123
|
+
emit({ requestId, data: key ? document[key] : document });
|
|
124
|
+
}
|
|
125
|
+
catch (error) {
|
|
126
|
+
emit({
|
|
127
|
+
requestId,
|
|
128
|
+
error: `Failed to read config: ${error instanceof Error ? error.message : String(error)}`,
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
return payloads;
|
|
132
|
+
}
|
|
133
|
+
case 'config:save-yaml': {
|
|
134
|
+
try {
|
|
135
|
+
const yaml = message.yaml;
|
|
136
|
+
if (typeof yaml !== 'string') {
|
|
137
|
+
emit({ requestId, error: 'yaml field is required' });
|
|
138
|
+
return payloads;
|
|
139
|
+
}
|
|
140
|
+
if (!ctx.writeConfigYaml) {
|
|
141
|
+
emit({ requestId, error: 'Config write is not configured' });
|
|
142
|
+
return payloads;
|
|
143
|
+
}
|
|
144
|
+
await ctx.writeConfigYaml(yaml);
|
|
145
|
+
emit({
|
|
146
|
+
requestId,
|
|
147
|
+
data: { success: true, message: '配置已保存,需重启生效' },
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
catch (error) {
|
|
151
|
+
emit({
|
|
152
|
+
requestId,
|
|
153
|
+
error: `Failed to save config: ${error instanceof Error ? error.message : String(error)}`,
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
return payloads;
|
|
157
|
+
}
|
|
158
|
+
case 'config:set': {
|
|
159
|
+
try {
|
|
160
|
+
const pluginName = message.pluginName;
|
|
161
|
+
if (typeof pluginName !== 'string' || !pluginName) {
|
|
162
|
+
emit({ requestId, error: 'Plugin name is required' });
|
|
163
|
+
return payloads;
|
|
164
|
+
}
|
|
165
|
+
if (!ctx.setConfigKey) {
|
|
166
|
+
emit({ requestId, error: 'Config write is not configured' });
|
|
167
|
+
return payloads;
|
|
168
|
+
}
|
|
169
|
+
const result = await ctx.setConfigKey(pluginName, message.data);
|
|
170
|
+
emit({
|
|
171
|
+
requestId,
|
|
172
|
+
data: {
|
|
173
|
+
success: true,
|
|
174
|
+
reloaded: false,
|
|
175
|
+
message: result.restartRequired
|
|
176
|
+
? '配置已保存,需重启进程才能生效'
|
|
177
|
+
: '配置已保存',
|
|
178
|
+
},
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
catch (error) {
|
|
182
|
+
emit({
|
|
183
|
+
requestId,
|
|
184
|
+
error: `Failed to set config: ${error instanceof Error ? error.message : String(error)}`,
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
return payloads;
|
|
188
|
+
}
|
|
189
|
+
case 'files:tree': {
|
|
190
|
+
try {
|
|
191
|
+
const tree = ctx.listProjectFiles ? await ctx.listProjectFiles() : [];
|
|
192
|
+
emit({ requestId, data: { tree } });
|
|
193
|
+
}
|
|
194
|
+
catch (error) {
|
|
195
|
+
emit({
|
|
196
|
+
requestId,
|
|
197
|
+
error: `Failed to build file tree: ${error instanceof Error ? error.message : String(error)}`,
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
return payloads;
|
|
201
|
+
}
|
|
202
|
+
case 'files:read': {
|
|
203
|
+
try {
|
|
204
|
+
const filePath = String(message.filePath ?? '');
|
|
205
|
+
if (!filePath) {
|
|
206
|
+
emit({ requestId, error: 'filePath is required' });
|
|
207
|
+
return payloads;
|
|
208
|
+
}
|
|
209
|
+
if (!ctx.readProjectFile) {
|
|
210
|
+
emit({ requestId, error: 'File read is not configured' });
|
|
211
|
+
return payloads;
|
|
212
|
+
}
|
|
213
|
+
const file = await ctx.readProjectFile(filePath);
|
|
214
|
+
emit({ requestId, data: file });
|
|
215
|
+
}
|
|
216
|
+
catch (error) {
|
|
217
|
+
emit({
|
|
218
|
+
requestId,
|
|
219
|
+
error: `Failed to read file: ${error instanceof Error ? error.message : String(error)}`,
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
return payloads;
|
|
223
|
+
}
|
|
224
|
+
case 'files:save': {
|
|
225
|
+
try {
|
|
226
|
+
const filePath = String(message.filePath ?? '');
|
|
227
|
+
const content = message.content;
|
|
228
|
+
if (!filePath) {
|
|
229
|
+
emit({ requestId, error: 'filePath is required' });
|
|
230
|
+
return payloads;
|
|
231
|
+
}
|
|
232
|
+
if (typeof content !== 'string') {
|
|
233
|
+
emit({ requestId, error: 'content field is required' });
|
|
234
|
+
return payloads;
|
|
235
|
+
}
|
|
236
|
+
if (!ctx.saveProjectFile) {
|
|
237
|
+
emit({ requestId, error: 'File write is not configured' });
|
|
238
|
+
return payloads;
|
|
239
|
+
}
|
|
240
|
+
await ctx.saveProjectFile(filePath, content);
|
|
241
|
+
emit({ requestId, data: { success: true, message: `文件已保存: ${filePath}` } });
|
|
242
|
+
}
|
|
243
|
+
catch (error) {
|
|
244
|
+
emit({
|
|
245
|
+
requestId,
|
|
246
|
+
error: `Failed to save file: ${error instanceof Error ? error.message : String(error)}`,
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
return payloads;
|
|
250
|
+
}
|
|
251
|
+
case 'env:list': {
|
|
252
|
+
try {
|
|
253
|
+
const files = ctx.listEnvFiles ? await ctx.listEnvFiles() : [];
|
|
254
|
+
emit({ requestId, data: { files } });
|
|
255
|
+
}
|
|
256
|
+
catch (error) {
|
|
257
|
+
emit({
|
|
258
|
+
requestId,
|
|
259
|
+
error: `Failed to list env files: ${error instanceof Error ? error.message : String(error)}`,
|
|
260
|
+
});
|
|
261
|
+
}
|
|
262
|
+
return payloads;
|
|
263
|
+
}
|
|
264
|
+
case 'env:get': {
|
|
265
|
+
try {
|
|
266
|
+
const filename = String(message.filename ?? '');
|
|
267
|
+
if (!filename) {
|
|
268
|
+
emit({ requestId, error: 'filename is required' });
|
|
269
|
+
return payloads;
|
|
270
|
+
}
|
|
271
|
+
if (!ctx.readEnvFile) {
|
|
272
|
+
emit({ requestId, error: 'Env read is not configured' });
|
|
273
|
+
return payloads;
|
|
274
|
+
}
|
|
275
|
+
const content = await ctx.readEnvFile(filename);
|
|
276
|
+
emit({ requestId, data: { content } });
|
|
277
|
+
}
|
|
278
|
+
catch (error) {
|
|
279
|
+
emit({
|
|
280
|
+
requestId,
|
|
281
|
+
error: `Failed to read env file: ${error instanceof Error ? error.message : String(error)}`,
|
|
282
|
+
});
|
|
283
|
+
}
|
|
284
|
+
return payloads;
|
|
285
|
+
}
|
|
286
|
+
case 'env:save': {
|
|
287
|
+
try {
|
|
288
|
+
const filename = String(message.filename ?? '');
|
|
289
|
+
const content = message.content;
|
|
290
|
+
if (!filename) {
|
|
291
|
+
emit({ requestId, error: 'filename is required' });
|
|
292
|
+
return payloads;
|
|
293
|
+
}
|
|
294
|
+
if (typeof content !== 'string') {
|
|
295
|
+
emit({ requestId, error: 'content field is required' });
|
|
296
|
+
return payloads;
|
|
297
|
+
}
|
|
298
|
+
if (!ctx.writeEnvFile) {
|
|
299
|
+
emit({ requestId, error: 'Env write is not configured' });
|
|
300
|
+
return payloads;
|
|
301
|
+
}
|
|
302
|
+
await ctx.writeEnvFile(filename, content);
|
|
303
|
+
emit({ requestId, data: { success: true, message: `环境文件已保存: ${filename}` } });
|
|
304
|
+
}
|
|
305
|
+
catch (error) {
|
|
306
|
+
emit({
|
|
307
|
+
requestId,
|
|
308
|
+
error: `Failed to save env file: ${error instanceof Error ? error.message : String(error)}`,
|
|
309
|
+
});
|
|
310
|
+
}
|
|
311
|
+
return payloads;
|
|
312
|
+
}
|
|
313
|
+
case 'schema:get': {
|
|
314
|
+
try {
|
|
315
|
+
const pluginName = typeof message.pluginName === 'string' ? message.pluginName : undefined;
|
|
316
|
+
const schema = ctx.getSchema ? await ctx.getSchema(pluginName) : null;
|
|
317
|
+
emit({ requestId, data: schema });
|
|
318
|
+
}
|
|
319
|
+
catch (error) {
|
|
320
|
+
emit({
|
|
321
|
+
requestId,
|
|
322
|
+
error: `Failed to get schema: ${error instanceof Error ? error.message : String(error)}`,
|
|
323
|
+
});
|
|
324
|
+
}
|
|
325
|
+
return payloads;
|
|
326
|
+
}
|
|
327
|
+
case 'schema:get-all': {
|
|
328
|
+
try {
|
|
329
|
+
const schemas = ctx.getAllSchemas ? await ctx.getAllSchemas() : {};
|
|
330
|
+
emit({ requestId, data: schemas });
|
|
331
|
+
}
|
|
332
|
+
catch (error) {
|
|
333
|
+
emit({
|
|
334
|
+
requestId,
|
|
335
|
+
error: `Failed to get all schemas: ${error instanceof Error ? error.message : String(error)}`,
|
|
336
|
+
});
|
|
337
|
+
}
|
|
338
|
+
return payloads;
|
|
339
|
+
}
|
|
340
|
+
case 'db:info': {
|
|
341
|
+
if (ctx.database) {
|
|
342
|
+
emit({ requestId, data: ctx.database.info() });
|
|
343
|
+
return payloads;
|
|
344
|
+
}
|
|
345
|
+
if (ctx.dbInfo) {
|
|
346
|
+
try {
|
|
347
|
+
emit({ requestId, data: await ctx.dbInfo() });
|
|
348
|
+
}
|
|
349
|
+
catch (error) {
|
|
350
|
+
emit({
|
|
351
|
+
requestId,
|
|
352
|
+
error: error instanceof Error ? error.message : String(error),
|
|
353
|
+
});
|
|
354
|
+
}
|
|
355
|
+
return payloads;
|
|
356
|
+
}
|
|
357
|
+
emit({
|
|
358
|
+
requestId,
|
|
359
|
+
data: { dialect: null, connected: false, message: 'Database not configured on Plugin Runtime Host' },
|
|
360
|
+
});
|
|
361
|
+
return payloads;
|
|
362
|
+
}
|
|
363
|
+
case 'db:tables': {
|
|
364
|
+
try {
|
|
365
|
+
if (ctx.database) {
|
|
366
|
+
emit({ requestId, data: { tables: ctx.database.tables() } });
|
|
367
|
+
return payloads;
|
|
368
|
+
}
|
|
369
|
+
const tables = ctx.dbTables ? await ctx.dbTables() : [];
|
|
370
|
+
emit({ requestId, data: tables });
|
|
371
|
+
}
|
|
372
|
+
catch (error) {
|
|
373
|
+
emit({
|
|
374
|
+
requestId,
|
|
375
|
+
error: error instanceof Error ? error.message : String(error),
|
|
376
|
+
});
|
|
377
|
+
}
|
|
378
|
+
return payloads;
|
|
379
|
+
}
|
|
380
|
+
case 'db:select': {
|
|
381
|
+
const table = stringField(message, 'table');
|
|
382
|
+
if (!table)
|
|
383
|
+
return errorReply(payloads, requestId, 'table is required');
|
|
384
|
+
const page = positiveInteger(message.page, 1);
|
|
385
|
+
const pageSize = Math.min(positiveInteger(message.pageSize, 50), 500);
|
|
386
|
+
const where = recordField(message.where);
|
|
387
|
+
if (!ctx.database) {
|
|
388
|
+
emit({ requestId, data: { rows: [], total: 0, page, pageSize } });
|
|
389
|
+
return payloads;
|
|
390
|
+
}
|
|
391
|
+
try {
|
|
392
|
+
emit({ requestId, data: await ctx.database.select(table, page, pageSize, where) });
|
|
393
|
+
}
|
|
394
|
+
catch (error) {
|
|
395
|
+
emit({ requestId, error: `Failed to select: ${errorMessage(error)}` });
|
|
396
|
+
}
|
|
397
|
+
return payloads;
|
|
398
|
+
}
|
|
399
|
+
case 'db:insert': {
|
|
400
|
+
if (!ctx.database)
|
|
401
|
+
return errorReply(payloads, requestId, 'Database RPC is not available on Plugin Runtime Host');
|
|
402
|
+
const table = stringField(message, 'table');
|
|
403
|
+
const row = recordField(message.row);
|
|
404
|
+
if (!table || !row)
|
|
405
|
+
return errorReply(payloads, requestId, 'table and row are required');
|
|
406
|
+
try {
|
|
407
|
+
await ctx.database.insert(table, row);
|
|
408
|
+
emit({ requestId, data: { success: true } });
|
|
409
|
+
}
|
|
410
|
+
catch (error) {
|
|
411
|
+
emit({ requestId, error: `Failed to insert: ${errorMessage(error)}` });
|
|
412
|
+
}
|
|
413
|
+
return payloads;
|
|
414
|
+
}
|
|
415
|
+
case 'db:update': {
|
|
416
|
+
if (!ctx.database)
|
|
417
|
+
return errorReply(payloads, requestId, 'Database RPC is not available on Plugin Runtime Host');
|
|
418
|
+
const table = stringField(message, 'table');
|
|
419
|
+
const row = recordField(message.row);
|
|
420
|
+
const where = recordField(message.where);
|
|
421
|
+
if (!table || !row || !where) {
|
|
422
|
+
return errorReply(payloads, requestId, 'table, row, and where are required');
|
|
423
|
+
}
|
|
424
|
+
try {
|
|
425
|
+
const affected = await ctx.database.update(table, row, where);
|
|
426
|
+
emit({ requestId, data: { success: true, affected } });
|
|
427
|
+
}
|
|
428
|
+
catch (error) {
|
|
429
|
+
emit({ requestId, error: `Failed to update: ${errorMessage(error)}` });
|
|
430
|
+
}
|
|
431
|
+
return payloads;
|
|
432
|
+
}
|
|
433
|
+
case 'db:delete': {
|
|
434
|
+
if (!ctx.database)
|
|
435
|
+
return errorReply(payloads, requestId, 'Database RPC is not available on Plugin Runtime Host');
|
|
436
|
+
const table = stringField(message, 'table');
|
|
437
|
+
const where = recordField(message.where);
|
|
438
|
+
if (!table || !where)
|
|
439
|
+
return errorReply(payloads, requestId, 'table and where are required');
|
|
440
|
+
try {
|
|
441
|
+
const deleted = await ctx.database.delete(table, where);
|
|
442
|
+
emit({ requestId, data: { success: true, deleted } });
|
|
443
|
+
}
|
|
444
|
+
catch (error) {
|
|
445
|
+
emit({ requestId, error: `Failed to delete: ${errorMessage(error)}` });
|
|
446
|
+
}
|
|
447
|
+
return payloads;
|
|
448
|
+
}
|
|
449
|
+
case 'db:drop-table': {
|
|
450
|
+
if (!ctx.database)
|
|
451
|
+
return errorReply(payloads, requestId, 'Database RPC is not available on Plugin Runtime Host');
|
|
452
|
+
const table = stringField(message, 'table');
|
|
453
|
+
if (!table)
|
|
454
|
+
return errorReply(payloads, requestId, 'table is required');
|
|
455
|
+
try {
|
|
456
|
+
await ctx.database.dropTable(table);
|
|
457
|
+
emit({ requestId, data: { success: true } });
|
|
458
|
+
}
|
|
459
|
+
catch (error) {
|
|
460
|
+
emit({ requestId, error: `Failed to drop table: ${errorMessage(error)}` });
|
|
461
|
+
}
|
|
462
|
+
return payloads;
|
|
463
|
+
}
|
|
464
|
+
case 'db:kv:get': {
|
|
465
|
+
const table = stringField(message, 'table');
|
|
466
|
+
const key = stringField(message, 'key');
|
|
467
|
+
if (!table || !key)
|
|
468
|
+
return errorReply(payloads, requestId, 'table and key are required');
|
|
469
|
+
if (!ctx.database) {
|
|
470
|
+
emit({ requestId, data: { key, value: null } });
|
|
471
|
+
return payloads;
|
|
472
|
+
}
|
|
473
|
+
try {
|
|
474
|
+
emit({ requestId, data: { key, value: await ctx.database.kvGet(table, key) } });
|
|
475
|
+
}
|
|
476
|
+
catch (error) {
|
|
477
|
+
emit({ requestId, error: `Failed to get kv: ${errorMessage(error)}` });
|
|
478
|
+
}
|
|
479
|
+
return payloads;
|
|
480
|
+
}
|
|
481
|
+
case 'db:kv:set': {
|
|
482
|
+
if (!ctx.database)
|
|
483
|
+
return errorReply(payloads, requestId, 'Database RPC is not available on Plugin Runtime Host');
|
|
484
|
+
const table = stringField(message, 'table');
|
|
485
|
+
const key = stringField(message, 'key');
|
|
486
|
+
if (!table || !key)
|
|
487
|
+
return errorReply(payloads, requestId, 'table and key are required');
|
|
488
|
+
try {
|
|
489
|
+
const ttl = typeof message.ttl === 'number' && Number.isFinite(message.ttl)
|
|
490
|
+
? message.ttl
|
|
491
|
+
: undefined;
|
|
492
|
+
await ctx.database.kvSet(table, key, message.value, ttl);
|
|
493
|
+
emit({ requestId, data: { success: true } });
|
|
494
|
+
}
|
|
495
|
+
catch (error) {
|
|
496
|
+
emit({ requestId, error: `Failed to set kv: ${errorMessage(error)}` });
|
|
497
|
+
}
|
|
498
|
+
return payloads;
|
|
499
|
+
}
|
|
500
|
+
case 'db:kv:delete': {
|
|
501
|
+
if (!ctx.database)
|
|
502
|
+
return errorReply(payloads, requestId, 'Database RPC is not available on Plugin Runtime Host');
|
|
503
|
+
const table = stringField(message, 'table');
|
|
504
|
+
const key = stringField(message, 'key');
|
|
505
|
+
if (!table || !key)
|
|
506
|
+
return errorReply(payloads, requestId, 'table and key are required');
|
|
507
|
+
try {
|
|
508
|
+
await ctx.database.kvDelete(table, key);
|
|
509
|
+
emit({ requestId, data: { success: true } });
|
|
510
|
+
}
|
|
511
|
+
catch (error) {
|
|
512
|
+
emit({ requestId, error: `Failed to delete kv: ${errorMessage(error)}` });
|
|
513
|
+
}
|
|
514
|
+
return payloads;
|
|
515
|
+
}
|
|
516
|
+
case 'db:kv:entries': {
|
|
517
|
+
const table = stringField(message, 'table');
|
|
518
|
+
if (!table)
|
|
519
|
+
return errorReply(payloads, requestId, 'table is required');
|
|
520
|
+
if (!ctx.database) {
|
|
521
|
+
emit({ requestId, data: { entries: [] } });
|
|
522
|
+
return payloads;
|
|
523
|
+
}
|
|
524
|
+
try {
|
|
525
|
+
emit({ requestId, data: { entries: await ctx.database.kvEntries(table) } });
|
|
526
|
+
}
|
|
527
|
+
catch (error) {
|
|
528
|
+
emit({ requestId, error: `Failed to get entries: ${errorMessage(error)}` });
|
|
529
|
+
}
|
|
530
|
+
return payloads;
|
|
531
|
+
}
|
|
532
|
+
case 'endpoint.list': {
|
|
533
|
+
try {
|
|
534
|
+
const endpoints = ctx.listEndpoints ? await ctx.listEndpoints() : [];
|
|
535
|
+
emit({ requestId, data: { endpoints } });
|
|
536
|
+
}
|
|
537
|
+
catch (error) {
|
|
538
|
+
emit({
|
|
539
|
+
requestId,
|
|
540
|
+
error: error instanceof Error ? error.message : String(error),
|
|
541
|
+
});
|
|
542
|
+
}
|
|
543
|
+
return payloads;
|
|
544
|
+
}
|
|
545
|
+
case 'endpoint.info': {
|
|
546
|
+
try {
|
|
547
|
+
const data = (message.data ?? {});
|
|
548
|
+
// Accept both canonical `$`-fields and the SDK colon-alias shape.
|
|
549
|
+
const adapter = String(data.$adapter ?? data.adapter ?? '');
|
|
550
|
+
const endpointId = String(data.$endpoint ?? data.endpointId ?? '');
|
|
551
|
+
if (!adapter || !endpointId) {
|
|
552
|
+
emit({ requestId, error: '$adapter and $endpoint required' });
|
|
553
|
+
return payloads;
|
|
554
|
+
}
|
|
555
|
+
if (!ctx.getEndpoint) {
|
|
556
|
+
emit({ requestId, error: 'Endpoint registry is not configured' });
|
|
557
|
+
return payloads;
|
|
558
|
+
}
|
|
559
|
+
const endpoint = await ctx.getEndpoint(adapter, endpointId);
|
|
560
|
+
if (!endpoint) {
|
|
561
|
+
emit({ requestId, error: 'endpoint not found' });
|
|
562
|
+
return payloads;
|
|
563
|
+
}
|
|
564
|
+
emit({ requestId, data: endpoint });
|
|
565
|
+
}
|
|
566
|
+
catch (error) {
|
|
567
|
+
emit({
|
|
568
|
+
requestId,
|
|
569
|
+
error: error instanceof Error ? error.message : String(error),
|
|
570
|
+
});
|
|
571
|
+
}
|
|
572
|
+
return payloads;
|
|
573
|
+
}
|
|
574
|
+
case 'endpoint.send_message': {
|
|
575
|
+
try {
|
|
576
|
+
const data = (message.data ?? {});
|
|
577
|
+
// Accept both canonical `$`-fields and the SDK colon-alias shape
|
|
578
|
+
// (`endpoint:sendMessage` → data.{adapter, endpointId, id, type, content, parent}).
|
|
579
|
+
const adapter = String(data.$adapter ?? data.adapter ?? '');
|
|
580
|
+
const endpointId = String(data.$endpoint ?? data.endpointId ?? '');
|
|
581
|
+
const channelId = String(data.$channel_id ?? data.id ?? '');
|
|
582
|
+
const channelType = String(data.$channel_type ?? data.type ?? '');
|
|
583
|
+
const content = data.$content !== undefined ? data.$content : data.content;
|
|
584
|
+
// channelType is optional: ImRuntime tolerates an empty value and
|
|
585
|
+
// targets the bare channel id (parity with the legacy RPC handler).
|
|
586
|
+
if (!adapter || !endpointId || !channelId || content === undefined) {
|
|
587
|
+
emit({
|
|
588
|
+
requestId,
|
|
589
|
+
error: '$adapter, $endpoint, $channel_id, $content required',
|
|
590
|
+
});
|
|
591
|
+
return payloads;
|
|
592
|
+
}
|
|
593
|
+
if (!ctx.sendEndpointMessage) {
|
|
594
|
+
emit({ requestId, error: 'Endpoint send is not configured' });
|
|
595
|
+
return payloads;
|
|
596
|
+
}
|
|
597
|
+
const result = await ctx.sendEndpointMessage({
|
|
598
|
+
adapter,
|
|
599
|
+
endpointId,
|
|
600
|
+
channelId,
|
|
601
|
+
channelType,
|
|
602
|
+
content,
|
|
603
|
+
parent: (data.$parent ?? data.parent),
|
|
604
|
+
});
|
|
605
|
+
// Legacy contract: { message_id }. Keep messageId for new callers.
|
|
606
|
+
emit({ requestId, data: { message_id: result.messageId, messageId: result.messageId } });
|
|
607
|
+
}
|
|
608
|
+
catch (error) {
|
|
609
|
+
emit({
|
|
610
|
+
requestId,
|
|
611
|
+
error: error instanceof Error ? error.message : String(error),
|
|
612
|
+
});
|
|
613
|
+
}
|
|
614
|
+
return payloads;
|
|
615
|
+
}
|
|
616
|
+
case 'system:restart': {
|
|
617
|
+
try {
|
|
618
|
+
if (!ctx.requestRestart) {
|
|
619
|
+
emit({ requestId, error: 'Process restart is not configured' });
|
|
620
|
+
return payloads;
|
|
621
|
+
}
|
|
622
|
+
emit({ requestId, data: { success: true, message: '正在重启...' } });
|
|
623
|
+
// Delay so the HTTP response can flush (parity with legacy host-api).
|
|
624
|
+
setTimeout(() => {
|
|
625
|
+
void Promise.resolve(ctx.requestRestart?.()).catch(() => {
|
|
626
|
+
/* restart is best-effort */
|
|
627
|
+
});
|
|
628
|
+
}, 500);
|
|
629
|
+
}
|
|
630
|
+
catch (error) {
|
|
631
|
+
emit({
|
|
632
|
+
requestId,
|
|
633
|
+
error: error instanceof Error ? error.message : String(error),
|
|
634
|
+
});
|
|
635
|
+
}
|
|
636
|
+
return payloads;
|
|
637
|
+
}
|
|
638
|
+
default:
|
|
639
|
+
if (type.startsWith('db:')) {
|
|
640
|
+
emit({
|
|
641
|
+
requestId,
|
|
642
|
+
error: 'Database RPC is not available on Plugin Runtime Host yet',
|
|
643
|
+
});
|
|
644
|
+
return payloads;
|
|
645
|
+
}
|
|
646
|
+
emit({ requestId, error: `Unknown RPC type: ${type || '<empty>'}` });
|
|
647
|
+
return payloads;
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
function stringField(message, key) {
|
|
651
|
+
const value = message[key];
|
|
652
|
+
return typeof value === 'string' ? value.trim() : '';
|
|
653
|
+
}
|
|
654
|
+
function recordField(value) {
|
|
655
|
+
return value && typeof value === 'object' && !Array.isArray(value)
|
|
656
|
+
? value
|
|
657
|
+
: undefined;
|
|
658
|
+
}
|
|
659
|
+
function positiveInteger(value, fallback) {
|
|
660
|
+
const parsed = Number(value ?? fallback);
|
|
661
|
+
return Number.isInteger(parsed) && parsed > 0 ? parsed : fallback;
|
|
662
|
+
}
|
|
663
|
+
function errorMessage(error) {
|
|
664
|
+
return error instanceof Error ? error.message : String(error);
|
|
665
|
+
}
|
|
666
|
+
function errorReply(payloads, requestId, error) {
|
|
667
|
+
payloads.push({ requestId, error });
|
|
668
|
+
return payloads;
|
|
669
|
+
}
|
|
670
|
+
/** HTTP paths under apiBase that demo scope may call (ADR 0016). */
|
|
671
|
+
export function isDemoHttpAllowed(method, pathname, apiBase) {
|
|
672
|
+
const normalizedBase = apiBase.replace(/\/$/u, '') || '/api';
|
|
673
|
+
const m = method.toUpperCase();
|
|
674
|
+
if (m === 'GET' && pathname === `${normalizedBase}/events`)
|
|
675
|
+
return true;
|
|
676
|
+
if (m === 'POST' && pathname === `${normalizedBase}/console/request`)
|
|
677
|
+
return true;
|
|
678
|
+
// Read-only dashboard / plugin inspection routes (legacy host-api parity).
|
|
679
|
+
if (m === 'GET' && pathname === `${normalizedBase}/system/status`)
|
|
680
|
+
return true;
|
|
681
|
+
if (m === 'GET' && pathname === `${normalizedBase}/stats`)
|
|
682
|
+
return true;
|
|
683
|
+
if (m === 'GET' && (pathname === `${normalizedBase}/plugins`
|
|
684
|
+
|| pathname.startsWith(`${normalizedBase}/plugins/`)))
|
|
685
|
+
return true;
|
|
686
|
+
return false;
|
|
687
|
+
}
|