bailinghub 0.3.3 → 0.4.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/README.en.md +3 -1
- package/README.md +4 -0
- package/docker-compose.images.yml +2 -2
- package/docs/CHANGELOG.en.md +58 -1
- package/docs/CHANGELOG.md +26 -1
- package/docs/COMPATIBILITY.en.md +25 -0
- package/docs/CONTRACT.en.md +91 -8
- package/docs/CONTRACT.md +39 -7
- package/docs/DEMO.en.md +6 -0
- package/docs/DEMO.md +2 -0
- package/docs/INDEPENDENT_VALIDATION.en.md +9 -6
- package/docs/INDEPENDENT_VALIDATION.md +9 -6
- package/docs/QUICKSTART.en.md +20 -0
- package/docs/QUICKSTART.md +2 -2
- package/docs/README.en.md +4 -0
- package/docs/README.md +4 -0
- package/docs/RELEASE_NOTES_v0.3.4.en.md +38 -0
- package/docs/RELEASE_NOTES_v0.3.4.md +38 -0
- package/docs/RELEASE_NOTES_v0.4.0.en.md +39 -0
- package/docs/RELEASE_NOTES_v0.4.0.md +39 -0
- package/docs/STREAMING.en.md +32 -0
- package/docs/STREAMING.md +8 -2
- package/docs/WIDGET_RENDERERS.en.md +123 -6
- package/docs/WIDGET_RENDERERS.md +75 -6
- package/docs/user-guide/concepts.en.md +8 -1
- package/docs/user-guide/concepts.md +3 -1
- package/docs/user-guide/console-map.en.md +1 -2
- package/docs/user-guide/console-map.md +3 -3
- package/docs//345/205/274/345/256/271/346/200/247/344/270/216/345/215/207/347/272/247.md +9 -0
- package/package.json +1 -1
- package/schemas/config/chat-entry.schema.json +2 -1
- package/scripts/check-doc-links.mjs +2 -0
- package/scripts/check-image-tags.sh +1 -1
- package/scripts/check-release-consistency.test.mjs +5 -5
- package/scripts/e2e-console.mjs +38 -10
- package/scripts/install.sh +1 -1
- package/scripts/publish-images.sh +1 -1
- package/scripts/release-audit.mjs +2 -0
- package/scripts/verify-github-repo.mjs +2 -0
- package/scripts/widget-renderer-test.mjs +244 -16
- package/src/core/config/config-models.test.ts +18 -1
- package/src/core/config/config-models.ts +25 -3
- package/src/core/config/config-schema.test.ts +2 -0
- package/src/core/contracts/types.ts +2 -1
- package/src/core/platform/chat-form.test.ts +147 -0
- package/src/core/platform/chat-form.ts +402 -0
- package/src/core/platform/version.ts +1 -1
- package/src/core/runtime/execution-runtime.test.ts +73 -4
- package/src/core/runtime/execution-runtime.ts +18 -1
- package/src/routes/chat-mount.test.ts +9 -1
- package/src/routes/chat.test.ts +319 -7
- package/src/routes/chat.ts +179 -28
- package/web/console/assets/{Accounts-08VltF2i.js → Accounts-DuUY2UJU.js} +1 -1
- package/web/console/assets/{Approvals-CqgmsMau.js → Approvals-B11mo1dy.js} +1 -1
- package/web/console/assets/{Audit-bPlpSQt3.js → Audit-CQwJRIJT.js} +1 -1
- package/web/console/assets/{BrandLockup-DhX07g3G.js → BrandLockup-BIF-h1pi.js} +1 -1
- package/web/console/assets/{Channels-BZe0pLgx.js → Channels-BIploUT-.js} +1 -1
- package/web/console/assets/ChatEntries-Cpg_WV-o.js +7 -0
- package/web/console/assets/ChatEntries-DsYtcXN6.css +1 -0
- package/web/console/assets/Clients--D7NQJKk.css +1 -0
- package/web/console/assets/{Clients-BVVZfX3r.js → Clients-DS29eUi2.js} +2 -2
- package/web/console/assets/{Cost-DANcjr_p.js → Cost-BF52tfU2.js} +1 -1
- package/web/console/assets/{Credentials-D_8tP2Xa.js → Credentials-SwfmQAv6.js} +1 -1
- package/web/console/assets/{Diagnostics-Bw1WImFe.js → Diagnostics-CJfbI_8p.js} +1 -1
- package/web/console/assets/{Executors-fuieN2Sl.js → Executors-CzlZlznA.js} +1 -1
- package/web/console/assets/{Kb-BaMi_7xf.js → Kb-CVX22U4k.js} +1 -1
- package/web/console/assets/{Login-Cmz2ppY8.js → Login-PoUVsBI-.js} +1 -1
- package/web/console/assets/{Projects-D6xIgFB3.js → Projects-DYlbjLQy.js} +1 -1
- package/web/console/assets/{Routes-B90i3ZNa.js → Routes-DSEvGujN.js} +1 -1
- package/web/console/assets/{Runs-9qpwotOj.js → Runs-B-TsYZEt.js} +1 -1
- package/web/console/assets/Setup-D7nxgn-9.css +1 -0
- package/web/console/assets/Setup-xiPXit_b.js +1 -0
- package/web/console/assets/{Shell-Dm6eFcl1.js → Shell-DNiWZOYB.js} +1 -1
- package/web/console/assets/{StorageBuckets-DY8WuJlM.js → StorageBuckets-Bjsc3pLd.js} +1 -1
- package/web/console/assets/{SystemSettings-O_NMPzq_.js → SystemSettings-CcKF2lJ9.js} +1 -1
- package/web/console/assets/{SystemStatus-Cok3S7S3.js → SystemStatus-Oj1xjxJn.js} +1 -1
- package/web/console/assets/{Targets-YWBoxqvy.js → Targets-y1KllVXT.js} +1 -1
- package/web/console/assets/{Tools-BgXDw7Mg.js → Tools-DoEP_y9x.js} +1 -1
- package/web/console/assets/{index-S_J0I0Zh.js → index-CxPIS6G7.js} +2 -2
- package/web/console/assets/{request-FM608Kfz.js → request-CBLNpLth.js} +1 -1
- package/web/console/assets/{schema-DWnFqTkB.js → schema-DzwLpuOy.js} +1 -1
- package/web/console/index.html +1 -1
- package/web/widget/widget.js +493 -14
- package/web-admin/src/pages/ChatEntries.vue +46 -23
- package/web-admin/src/pages/Clients.vue +2 -2
- package/web-admin/src/pages/Setup.vue +12 -8
- package/web/console/assets/ChatEntries-BweMx_CU.css +0 -1
- package/web/console/assets/ChatEntries-Ca0lcGqn.js +0 -7
- package/web/console/assets/Clients-DcTYN-Js.css +0 -1
- package/web/console/assets/Setup-BIjCiA2i.js +0 -1
- package/web/console/assets/Setup-BuyKFofL.css +0 -1
package/src/routes/chat.test.ts
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
import test from 'node:test';
|
|
2
2
|
import assert from 'node:assert/strict';
|
|
3
3
|
import { EventEmitter } from 'node:events';
|
|
4
|
+
import { Readable } from 'node:stream';
|
|
4
5
|
import type { IncomingMessage, ServerResponse } from 'node:http';
|
|
5
6
|
import type { AppConfig } from '../core/config/config';
|
|
6
|
-
import type { ChatEntry, Job } from '../core/contracts/types';
|
|
7
|
+
import type { ChatEntry, Job, Route } from '../core/contracts/types';
|
|
7
8
|
import type { RuntimeContext } from '../core/edition';
|
|
8
9
|
import type { RuntimeStateStore } from '../core/state/state-contracts';
|
|
9
10
|
import type { ConfigStoreContract } from '../infrastructure/config/configstore';
|
|
10
11
|
import { CHAT_STREAM_PROTOCOL, InMemoryJobStreamBroker } from '../core/runtime/job-stream';
|
|
11
|
-
import { handleChatConfigFor, handleChatEventsFor, normalizePresentationCapabilities, type ChatApiDeps } from './chat';
|
|
12
|
+
import { handleChatConfigFor, handleChatEventsFor, handleChatFor, handleChatThreadFor, normalizePresentationCapabilities, type ChatApiDeps } from './chat';
|
|
12
13
|
|
|
13
14
|
class FakeResponse {
|
|
14
15
|
statusCode = 0;
|
|
@@ -39,8 +40,19 @@ class FakeResponse {
|
|
|
39
40
|
}
|
|
40
41
|
}
|
|
41
42
|
|
|
42
|
-
function request(origin = 'https://shop.example.com', headers: Record<string, string> = {}): IncomingMessage {
|
|
43
|
-
return Object.assign(new EventEmitter(), { headers: { origin, ...headers } }) as IncomingMessage;
|
|
43
|
+
function request(origin: string | null = 'https://shop.example.com', headers: Record<string, string> = {}): IncomingMessage {
|
|
44
|
+
return Object.assign(new EventEmitter(), { headers: { ...(origin === null ? {} : { origin }), ...headers } }) as IncomingMessage;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function jsonRequest(body: Record<string, unknown>, origin = 'https://shop.example.com'): IncomingMessage {
|
|
48
|
+
const stream = Readable.from([Buffer.from(JSON.stringify(body))]) as unknown as IncomingMessage;
|
|
49
|
+
stream.method = 'POST';
|
|
50
|
+
stream.headers = { origin, 'x-forwarded-for': '127.0.0.1' };
|
|
51
|
+
return stream;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function responseJson(res: FakeResponse): Record<string, unknown> {
|
|
55
|
+
return JSON.parse(Buffer.from(res.body).toString('utf8')) as Record<string, unknown>;
|
|
44
56
|
}
|
|
45
57
|
|
|
46
58
|
function entry(overrides: Partial<ChatEntry> = {}): ChatEntry {
|
|
@@ -114,9 +126,9 @@ function streamDeps(job: Job, broker: InMemoryJobStreamBroker): ChatApiDeps {
|
|
|
114
126
|
};
|
|
115
127
|
}
|
|
116
128
|
|
|
117
|
-
async function configBody(value: ChatEntry | null): Promise<{ status: number; body: Record<string, unknown> }> {
|
|
129
|
+
async function configBody(value: ChatEntry | null, origin: string | null = 'https://shop.example.com'): Promise<{ status: number; body: Record<string, unknown> }> {
|
|
118
130
|
const res = new FakeResponse();
|
|
119
|
-
await handleChatConfigFor(deps(value), request(), res as unknown as ServerResponse, 'pub_demo1234');
|
|
131
|
+
await handleChatConfigFor(deps(value), request(origin), res as unknown as ServerResponse, 'pub_demo1234');
|
|
120
132
|
return {
|
|
121
133
|
status: res.statusCode,
|
|
122
134
|
body: JSON.parse(Buffer.from(res.body).toString('utf8')) as Record<string, unknown>,
|
|
@@ -125,12 +137,13 @@ async function configBody(value: ChatEntry | null): Promise<{ status: number; bo
|
|
|
125
137
|
|
|
126
138
|
test('chat config: 启用入口下发品牌控制且保留部署品牌兼容字段', async () => {
|
|
127
139
|
const result = await configBody(entry({
|
|
128
|
-
appearance: { powered_by_visible: true, powered_by_text: '由示例业务驱动' },
|
|
140
|
+
appearance: { default_open: true, powered_by_visible: true, powered_by_text: '由示例业务驱动' },
|
|
129
141
|
}));
|
|
130
142
|
|
|
131
143
|
assert.equal(result.status, 200);
|
|
132
144
|
assert.equal(result.body.enabled, true);
|
|
133
145
|
assert.equal(result.body.brand, '百灵中枢');
|
|
146
|
+
assert.equal(result.body.default_open, true);
|
|
134
147
|
assert.equal(result.body.powered_by_visible, true);
|
|
135
148
|
assert.equal(result.body.powered_by_text, '由示例业务驱动');
|
|
136
149
|
});
|
|
@@ -139,6 +152,7 @@ test('chat config: 老入口生成默认品牌文案', async () => {
|
|
|
139
152
|
const result = await configBody(entry());
|
|
140
153
|
|
|
141
154
|
assert.equal(result.status, 200);
|
|
155
|
+
assert.equal(result.body.default_open, false);
|
|
142
156
|
assert.equal(result.body.powered_by_visible, true);
|
|
143
157
|
assert.equal(result.body.powered_by_text, '由 百灵中枢 驱动');
|
|
144
158
|
});
|
|
@@ -153,6 +167,32 @@ test('chat config: 停用入口返回静默隐藏状态,不存在入口仍返
|
|
|
153
167
|
assert.deepEqual(missing.body, { error: '聊天入口不存在' });
|
|
154
168
|
});
|
|
155
169
|
|
|
170
|
+
test('chat Origin 白名单: 浏览器来源规范化后精确匹配,错误来源在业务逻辑前拒绝', async () => {
|
|
171
|
+
const normalized = await configBody(entry(), 'https://SHOP.example.com:443/');
|
|
172
|
+
assert.equal(normalized.status, 200);
|
|
173
|
+
|
|
174
|
+
for (const deniedOrigin of ['https://other.example.com', 'https://shop.example.com/path', 'null']) {
|
|
175
|
+
const denied = await configBody(entry(), deniedOrigin);
|
|
176
|
+
assert.equal(denied.status, 403, deniedOrigin);
|
|
177
|
+
assert.deepEqual(denied.body, { error: '该站点未被允许嵌入此聊天入口' });
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
const unrestricted = await configBody(entry({ allowed_origins: [] }), 'https://other.example.com');
|
|
181
|
+
assert.equal(unrestricted.status, 200);
|
|
182
|
+
|
|
183
|
+
const noBrowserOrigin = await configBody(entry(), null);
|
|
184
|
+
assert.equal(noBrowserOrigin.status, 200);
|
|
185
|
+
|
|
186
|
+
const deniedMessage = new FakeResponse();
|
|
187
|
+
await handleChatFor(
|
|
188
|
+
deps(entry()),
|
|
189
|
+
jsonRequest({ message: '不应进入任务链路', visitor_id: 'visitor-12345678' }, 'https://other.example.com'),
|
|
190
|
+
deniedMessage as unknown as ServerResponse,
|
|
191
|
+
'pub_demo1234',
|
|
192
|
+
);
|
|
193
|
+
assert.equal(deniedMessage.statusCode, 403);
|
|
194
|
+
});
|
|
195
|
+
|
|
156
196
|
test('chat events: 增量事件可回放,done 仍以任务库最终结果为权威值', async () => {
|
|
157
197
|
const broker = new InMemoryJobStreamBroker();
|
|
158
198
|
const job = finishedJob();
|
|
@@ -220,3 +260,275 @@ test('chat events: Last-Event-ID 跳过已收到事件,回放窗口丢失时
|
|
|
220
260
|
assert.doesNotMatch(gapBody, /DROPPED_CHUNK/);
|
|
221
261
|
assert.match(gapBody, /LATEST_CHUNK/);
|
|
222
262
|
});
|
|
263
|
+
|
|
264
|
+
const chatRoute: Route = {
|
|
265
|
+
route_key: 'chat.main',
|
|
266
|
+
name: '聊天路由',
|
|
267
|
+
enabled: true,
|
|
268
|
+
target: 'llm',
|
|
269
|
+
target_config: {},
|
|
270
|
+
profile: 'general',
|
|
271
|
+
session_policy: 'per_key',
|
|
272
|
+
};
|
|
273
|
+
|
|
274
|
+
function formReply(): string {
|
|
275
|
+
return '请补充信息:\n```bailing-form\n' + JSON.stringify({
|
|
276
|
+
version: 1,
|
|
277
|
+
form_id: 'refund_info',
|
|
278
|
+
title: '退款信息',
|
|
279
|
+
schema: {
|
|
280
|
+
reason: { type: 'textarea', label: '退款原因', required: true, maxLength: 200 },
|
|
281
|
+
method: {
|
|
282
|
+
type: 'single_select',
|
|
283
|
+
label: '退款方式',
|
|
284
|
+
required: true,
|
|
285
|
+
options: [{ label: '原路退回', value: 'original' }, { label: '余额', value: 'balance' }],
|
|
286
|
+
},
|
|
287
|
+
},
|
|
288
|
+
}) + '\n```';
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
function interactionBody(overrides: Record<string, unknown> = {}): Record<string, unknown> {
|
|
292
|
+
return {
|
|
293
|
+
visitor_id: 'visitor-12345678',
|
|
294
|
+
thread_id: 'case-1',
|
|
295
|
+
interaction_response: {
|
|
296
|
+
type: 'bailing-form',
|
|
297
|
+
version: 1,
|
|
298
|
+
source_job_id: 'source-job-123',
|
|
299
|
+
form_id: 'refund_info',
|
|
300
|
+
submission_id: 'submission-123',
|
|
301
|
+
action: 'submit',
|
|
302
|
+
values: { reason: '商品损坏', method: 'original' },
|
|
303
|
+
...overrides,
|
|
304
|
+
},
|
|
305
|
+
};
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
function sourceFormJob(overrides: Partial<Job> = {}): Job {
|
|
309
|
+
return finishedJob({
|
|
310
|
+
job_id: 'source-job-123',
|
|
311
|
+
request_id: 'source-request-123',
|
|
312
|
+
source: 'chat:pub_demo1234',
|
|
313
|
+
result: { text: formReply() },
|
|
314
|
+
metadata: { chat_entry: 'pub_demo1234', visitor_id: 'visitor-12345678', thread_id: 'case-1' },
|
|
315
|
+
...overrides,
|
|
316
|
+
});
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
function chatFixture(source: Job = sourceFormJob(), throwAfterCreate = false): {
|
|
320
|
+
deps: ChatApiDeps;
|
|
321
|
+
jobs: Map<string, Job>;
|
|
322
|
+
launches: Array<Record<string, unknown>>;
|
|
323
|
+
} {
|
|
324
|
+
const jobs = new Map<string, Job>([[source.job_id, source]]);
|
|
325
|
+
const launches: Array<Record<string, unknown>> = [];
|
|
326
|
+
const config = {
|
|
327
|
+
chatEntries: {
|
|
328
|
+
get: async () => entry(),
|
|
329
|
+
listPageContexts: async () => [],
|
|
330
|
+
},
|
|
331
|
+
rateLimits: { consume: async () => false },
|
|
332
|
+
routes: { get: async () => chatRoute },
|
|
333
|
+
targets: { list: async () => [] },
|
|
334
|
+
conversations: {
|
|
335
|
+
sessionForScope: async () => ({ sessionId: 'session-1', isContinue: true, scopeKey: 'scope-1' }),
|
|
336
|
+
},
|
|
337
|
+
} as unknown as ConfigStoreContract;
|
|
338
|
+
const state = {
|
|
339
|
+
getJob: async (jobId: string) => jobs.get(jobId) ?? null,
|
|
340
|
+
findByRequestId: async (requestId: string) => [...jobs.values()].find((item) => item.request_id === requestId) ?? null,
|
|
341
|
+
} as unknown as RuntimeStateStore;
|
|
342
|
+
const fixtureDeps: ChatApiDeps = {
|
|
343
|
+
cfg: { brand: { name: '百灵中枢' } } as AppConfig,
|
|
344
|
+
isPaused: () => false,
|
|
345
|
+
runtimeContextFor: async () => ({} as RuntimeContext),
|
|
346
|
+
runtimeStoresFor: () => ({ state, config }),
|
|
347
|
+
resolveProjectPathFor: async () => null,
|
|
348
|
+
now: () => new Date(0).toISOString(),
|
|
349
|
+
engineForContext: () => ({
|
|
350
|
+
launchJob: async (spec) => {
|
|
351
|
+
launches.push(spec as unknown as Record<string, unknown>);
|
|
352
|
+
const created = finishedJob({
|
|
353
|
+
job_id: 'response-job-123',
|
|
354
|
+
request_id: spec.requestId,
|
|
355
|
+
status: 'queued',
|
|
356
|
+
source: spec.source,
|
|
357
|
+
result: undefined,
|
|
358
|
+
raw_result: undefined,
|
|
359
|
+
input: spec.fullInput,
|
|
360
|
+
input_preview: spec.fullInput.slice(0, 200),
|
|
361
|
+
metadata: spec.metadata,
|
|
362
|
+
});
|
|
363
|
+
jobs.set(created.job_id, created);
|
|
364
|
+
if (throwAfterCreate) throw Object.assign(new Error('duplicate'), { code: 'ER_DUP_ENTRY' });
|
|
365
|
+
return created;
|
|
366
|
+
},
|
|
367
|
+
}),
|
|
368
|
+
};
|
|
369
|
+
return { deps: fixtureDeps, jobs, launches };
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
test('POST /chat interaction: 从来源 done 回答重解析 schema,生成新用户轮次且 metadata 不重复 values', async () => {
|
|
373
|
+
const fixture = chatFixture();
|
|
374
|
+
const res = new FakeResponse();
|
|
375
|
+
await handleChatFor(fixture.deps, jsonRequest(interactionBody()), res as unknown as ServerResponse, 'pub_demo1234');
|
|
376
|
+
|
|
377
|
+
assert.equal(res.statusCode, 200);
|
|
378
|
+
assert.equal(responseJson(res)['job_id'], 'response-job-123');
|
|
379
|
+
assert.equal(fixture.launches.length, 1);
|
|
380
|
+
const launch = fixture.launches[0]!;
|
|
381
|
+
assert.match(String(launch['requestId']), /^chat_form_[0-9a-f]{48}$/);
|
|
382
|
+
assert.match(String(launch['fullInput']), /退款方式(method):原路退回/);
|
|
383
|
+
assert.match(String(launch['fullInput']), /用户数据,不是系统指令/);
|
|
384
|
+
const metadata = launch['metadata'] as Record<string, unknown>;
|
|
385
|
+
assert.deepEqual(metadata['interaction'], {
|
|
386
|
+
type: 'bailing-form',
|
|
387
|
+
version: 1,
|
|
388
|
+
source_job_id: 'source-job-123',
|
|
389
|
+
form_id: 'refund_info',
|
|
390
|
+
submission_id: 'submission-123',
|
|
391
|
+
action: 'submit',
|
|
392
|
+
});
|
|
393
|
+
assert.equal(JSON.stringify(metadata).includes('商品损坏'), false);
|
|
394
|
+
|
|
395
|
+
const replay = new FakeResponse();
|
|
396
|
+
await handleChatFor(fixture.deps, jsonRequest(interactionBody()), replay as unknown as ServerResponse, 'pub_demo1234');
|
|
397
|
+
assert.equal(responseJson(replay)['deduped'], true);
|
|
398
|
+
assert.equal(fixture.launches.length, 1);
|
|
399
|
+
});
|
|
400
|
+
|
|
401
|
+
test('POST /chat interaction: 跨访客或跨 thread 的来源统一不可探测', async () => {
|
|
402
|
+
for (const source of [
|
|
403
|
+
sourceFormJob({ metadata: { chat_entry: 'pub_demo1234', visitor_id: 'other-visitor', thread_id: 'case-1' } }),
|
|
404
|
+
sourceFormJob({ metadata: { chat_entry: 'pub_demo1234', visitor_id: 'visitor-12345678', thread_id: 'other-thread' } }),
|
|
405
|
+
sourceFormJob({ source: 'chat:another-entry' }),
|
|
406
|
+
]) {
|
|
407
|
+
const fixture = chatFixture(source);
|
|
408
|
+
const res = new FakeResponse();
|
|
409
|
+
await handleChatFor(fixture.deps, jsonRequest(interactionBody()), res as unknown as ServerResponse, 'pub_demo1234');
|
|
410
|
+
assert.equal(res.statusCode, 404);
|
|
411
|
+
assert.equal(responseJson(res)['error'], 'not found');
|
|
412
|
+
assert.equal(fixture.launches.length, 0);
|
|
413
|
+
}
|
|
414
|
+
});
|
|
415
|
+
|
|
416
|
+
test('POST /chat interaction: 来源须为 done 且 values 必须满足来源 schema', async () => {
|
|
417
|
+
const pendingFixture = chatFixture(sourceFormJob({ status: 'running' }));
|
|
418
|
+
const pending = new FakeResponse();
|
|
419
|
+
await handleChatFor(pendingFixture.deps, jsonRequest(interactionBody()), pending as unknown as ServerResponse, 'pub_demo1234');
|
|
420
|
+
assert.equal(pending.statusCode, 409);
|
|
421
|
+
|
|
422
|
+
const invalidFixture = chatFixture();
|
|
423
|
+
const invalid = new FakeResponse();
|
|
424
|
+
await handleChatFor(
|
|
425
|
+
invalidFixture.deps,
|
|
426
|
+
jsonRequest(interactionBody({ values: { reason: '损坏', method: 'not-allowed' } })),
|
|
427
|
+
invalid as unknown as ServerResponse,
|
|
428
|
+
'pub_demo1234',
|
|
429
|
+
);
|
|
430
|
+
assert.equal(invalid.statusCode, 400);
|
|
431
|
+
assert.match(String(responseJson(invalid)['error']), /允许的选项/);
|
|
432
|
+
assert.equal(invalidFixture.launches.length, 0);
|
|
433
|
+
|
|
434
|
+
const absentFixture = chatFixture(sourceFormJob({ result: { text: '这里只有普通文字,没有表单。' } }));
|
|
435
|
+
const absent = new FakeResponse();
|
|
436
|
+
await handleChatFor(absentFixture.deps, jsonRequest(interactionBody()), absent as unknown as ServerResponse, 'pub_demo1234');
|
|
437
|
+
assert.equal(absent.statusCode, 400);
|
|
438
|
+
assert.match(String(responseJson(absent)['error']), /没有可提交/);
|
|
439
|
+
});
|
|
440
|
+
|
|
441
|
+
test('POST /chat interaction: canonical 用户消息超过 4000 字时明确拒绝而非截断', async () => {
|
|
442
|
+
const longReply = '```bailing-form\n' + JSON.stringify({
|
|
443
|
+
version: 1,
|
|
444
|
+
form_id: 'refund_info',
|
|
445
|
+
title: '长表单',
|
|
446
|
+
schema: {
|
|
447
|
+
first: { type: 'textarea', label: '第一段', required: true, maxLength: 2000 },
|
|
448
|
+
second: { type: 'textarea', label: '第二段', required: true, maxLength: 2000 },
|
|
449
|
+
},
|
|
450
|
+
}) + '\n```';
|
|
451
|
+
const fixture = chatFixture(sourceFormJob({ result: { text: longReply } }));
|
|
452
|
+
const res = new FakeResponse();
|
|
453
|
+
await handleChatFor(
|
|
454
|
+
fixture.deps,
|
|
455
|
+
jsonRequest(interactionBody({ values: { first: '甲'.repeat(2000), second: '乙'.repeat(2000) } })),
|
|
456
|
+
res as unknown as ServerResponse,
|
|
457
|
+
'pub_demo1234',
|
|
458
|
+
);
|
|
459
|
+
assert.equal(res.statusCode, 400);
|
|
460
|
+
assert.match(String(responseJson(res)['error']), /超过 4000 字上限/);
|
|
461
|
+
assert.equal(fixture.launches.length, 0);
|
|
462
|
+
});
|
|
463
|
+
|
|
464
|
+
test('POST /chat interaction: createJob 并发唯一键失败后回查赢家并返回幂等结果', async () => {
|
|
465
|
+
const fixture = chatFixture(sourceFormJob(), true);
|
|
466
|
+
const res = new FakeResponse();
|
|
467
|
+
await handleChatFor(fixture.deps, jsonRequest(interactionBody()), res as unknown as ServerResponse, 'pub_demo1234');
|
|
468
|
+
|
|
469
|
+
assert.equal(res.statusCode, 200);
|
|
470
|
+
assert.equal(responseJson(res)['job_id'], 'response-job-123');
|
|
471
|
+
assert.equal(responseJson(res)['deduped'], true);
|
|
472
|
+
assert.equal(fixture.launches.length, 1);
|
|
473
|
+
});
|
|
474
|
+
|
|
475
|
+
test('GET /chat thread: 入站表单提交从 job metadata 回灌简化 interaction,不回传 values', async () => {
|
|
476
|
+
const submitted = finishedJob({
|
|
477
|
+
job_id: 'response-job-123',
|
|
478
|
+
thread_id: 7,
|
|
479
|
+
source: 'chat:pub_demo1234',
|
|
480
|
+
metadata: {
|
|
481
|
+
chat_entry: 'pub_demo1234',
|
|
482
|
+
visitor_id: 'visitor-12345678',
|
|
483
|
+
thread_id: 'case-1',
|
|
484
|
+
interaction: {
|
|
485
|
+
type: 'bailing-form',
|
|
486
|
+
version: 1,
|
|
487
|
+
source_job_id: 'source-job-123',
|
|
488
|
+
form_id: 'refund_info',
|
|
489
|
+
submission_id: 'submission-123',
|
|
490
|
+
action: 'submit',
|
|
491
|
+
},
|
|
492
|
+
},
|
|
493
|
+
});
|
|
494
|
+
const state = { getJob: async () => submitted } as unknown as RuntimeStateStore;
|
|
495
|
+
const config = {
|
|
496
|
+
chatEntries: { get: async () => entry() },
|
|
497
|
+
conversations: {
|
|
498
|
+
findThread: async () => 7,
|
|
499
|
+
threadMessages: async () => [{
|
|
500
|
+
direction: 'in',
|
|
501
|
+
content: '用户提交了上一条回答中的结构化表单。',
|
|
502
|
+
job_id: 'response-job-123',
|
|
503
|
+
created_at: new Date(0).toISOString(),
|
|
504
|
+
}],
|
|
505
|
+
},
|
|
506
|
+
} as unknown as ConfigStoreContract;
|
|
507
|
+
const historyDeps: ChatApiDeps = {
|
|
508
|
+
cfg: { brand: { name: '百灵中枢' } } as AppConfig,
|
|
509
|
+
isPaused: () => false,
|
|
510
|
+
runtimeContextFor: async () => ({} as RuntimeContext),
|
|
511
|
+
runtimeStoresFor: () => ({ state, config }),
|
|
512
|
+
resolveProjectPathFor: async () => null,
|
|
513
|
+
now: () => new Date(0).toISOString(),
|
|
514
|
+
engineForContext: () => ({ launchJob: async () => { throw new Error('not used'); } }),
|
|
515
|
+
};
|
|
516
|
+
const res = new FakeResponse();
|
|
517
|
+
await handleChatThreadFor(
|
|
518
|
+
historyDeps,
|
|
519
|
+
request(),
|
|
520
|
+
res as unknown as ServerResponse,
|
|
521
|
+
'pub_demo1234',
|
|
522
|
+
new URL('https://hub.example.com/chat/pub_demo1234/thread?visitor_id=visitor-12345678&thread_id=case-1'),
|
|
523
|
+
);
|
|
524
|
+
const messages = responseJson(res)['messages'] as Array<Record<string, unknown>>;
|
|
525
|
+
assert.deepEqual(messages[0]?.['interaction'], {
|
|
526
|
+
type: 'bailing-form',
|
|
527
|
+
version: 1,
|
|
528
|
+
source_job_id: 'source-job-123',
|
|
529
|
+
form_id: 'refund_info',
|
|
530
|
+
submission_id: 'submission-123',
|
|
531
|
+
action: 'submit',
|
|
532
|
+
});
|
|
533
|
+
assert.equal(JSON.stringify(messages).includes('values'), false);
|
|
534
|
+
});
|