@chatbi-v/mocks 2.1.6 → 2.1.8
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/dist/{index.mjs → index.cjs} +87 -34
- package/dist/index.d.cts +330 -0
- package/dist/index.d.ts +330 -0
- package/dist/index.js +33 -88
- package/package.json +7 -6
- package/dist/index.js.map +0 -1
- package/dist/index.mjs.map +0 -1
|
@@ -1,25 +1,78 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
1
30
|
// src/index.ts
|
|
2
|
-
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
|
+
ChatStreamStrategy: () => ChatStreamStrategy,
|
|
34
|
+
HistoryStreamStrategy: () => HistoryStreamStrategy,
|
|
35
|
+
JsonStrategy: () => JsonStrategy,
|
|
36
|
+
MOCK_FAVORITES: () => MOCK_FAVORITES,
|
|
37
|
+
MOCK_SCENES: () => MOCK_SCENES,
|
|
38
|
+
MOCK_SESSIONS: () => MOCK_SESSIONS,
|
|
39
|
+
MOCK_USER: () => MOCK_USER,
|
|
40
|
+
MockAdapter: () => MockAdapter,
|
|
41
|
+
MockResponseGenerator: () => MockResponseGenerator,
|
|
42
|
+
SsePageStrategy: () => SsePageStrategy,
|
|
43
|
+
SseStrategy: () => SseStrategy,
|
|
44
|
+
StrategyFactory: () => StrategyFactory,
|
|
45
|
+
createChatStream: () => createChatStream,
|
|
46
|
+
createHistoryStream: () => createHistoryStream,
|
|
47
|
+
eventsToStream: () => eventsToStream,
|
|
48
|
+
flatEvents: () => flatEvents,
|
|
49
|
+
generateMockMessages: () => generateMockMessages,
|
|
50
|
+
installFetchMock: () => installFetchMock,
|
|
51
|
+
installSSEMock: () => installSSEMock,
|
|
52
|
+
mockData: () => mockData,
|
|
53
|
+
processTemplate: () => processTemplate,
|
|
54
|
+
setupMock: () => setupMock,
|
|
55
|
+
sleep: () => sleep
|
|
56
|
+
});
|
|
57
|
+
module.exports = __toCommonJS(index_exports);
|
|
58
|
+
var import_core3 = require("@chatbi-v/core");
|
|
3
59
|
|
|
4
60
|
// src/adapter.ts
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
dateUtils as dateUtils2
|
|
8
|
-
} from "@chatbi-v/core";
|
|
9
|
-
import Mock3 from "mockjs";
|
|
61
|
+
var import_core2 = require("@chatbi-v/core");
|
|
62
|
+
var import_mockjs3 = __toESM(require("mockjs"), 1);
|
|
10
63
|
|
|
11
64
|
// src/strategies.ts
|
|
12
|
-
|
|
65
|
+
var import_mockjs2 = __toESM(require("mockjs"), 1);
|
|
13
66
|
|
|
14
67
|
// src/generator.ts
|
|
15
|
-
|
|
68
|
+
var import_core = require("@chatbi-v/core");
|
|
16
69
|
var MockResponseGenerator = class {
|
|
17
70
|
events = [];
|
|
18
71
|
sessionId;
|
|
19
72
|
agentName;
|
|
20
73
|
currentTodos = [];
|
|
21
74
|
constructor(agentName = "Assistant", sessionId) {
|
|
22
|
-
this.sessionId = sessionId || "conv_" + dateUtils.now();
|
|
75
|
+
this.sessionId = sessionId || "conv_" + import_core.dateUtils.now();
|
|
23
76
|
this.agentName = agentName;
|
|
24
77
|
}
|
|
25
78
|
/**
|
|
@@ -159,8 +212,8 @@ data: ${JSON.stringify(data)}
|
|
|
159
212
|
};
|
|
160
213
|
|
|
161
214
|
// src/utils.ts
|
|
162
|
-
|
|
163
|
-
|
|
215
|
+
var import_dayjs = __toESM(require("dayjs"), 1);
|
|
216
|
+
var import_mockjs = __toESM(require("mockjs"), 1);
|
|
164
217
|
var sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
165
218
|
function processTemplate(data, context) {
|
|
166
219
|
if (typeof data === "string") {
|
|
@@ -197,12 +250,12 @@ function flatEvents(eventsSchema, requestData = {}) {
|
|
|
197
250
|
$body: requestData,
|
|
198
251
|
$param: requestData?.param || requestData
|
|
199
252
|
};
|
|
200
|
-
|
|
253
|
+
import_mockjs.default.Random.extend({
|
|
201
254
|
$query: () => context.$query,
|
|
202
255
|
$body: () => context.$body,
|
|
203
256
|
$param: () => context.$param
|
|
204
257
|
});
|
|
205
|
-
const compiled =
|
|
258
|
+
const compiled = import_mockjs.default.mock(eventsSchema);
|
|
206
259
|
return Object.values(compiled).flat().map((event) => processTemplate(event, context)).sort((a, b) => {
|
|
207
260
|
const delayA = typeof a.delay === "number" ? a.delay : parseInt(String(a.delay || 0));
|
|
208
261
|
const delayB = typeof b.delay === "number" ? b.delay : parseInt(String(b.delay || 0));
|
|
@@ -219,10 +272,10 @@ function eventsToStream(events) {
|
|
|
219
272
|
}
|
|
220
273
|
(async () => {
|
|
221
274
|
try {
|
|
222
|
-
const startTime =
|
|
275
|
+
const startTime = (0, import_dayjs.default)().valueOf();
|
|
223
276
|
for (const eventItem of events) {
|
|
224
277
|
const delay = typeof eventItem.delay === "number" ? eventItem.delay : parseInt(String(eventItem.delay || 0));
|
|
225
|
-
const elapsed =
|
|
278
|
+
const elapsed = (0, import_dayjs.default)().valueOf() - startTime;
|
|
226
279
|
const remaining = Math.max(0, delay - elapsed);
|
|
227
280
|
if (remaining > 0) {
|
|
228
281
|
await sleep(remaining);
|
|
@@ -248,7 +301,7 @@ var ChatStreamStrategy = class {
|
|
|
248
301
|
const config = schema;
|
|
249
302
|
const defaultName = config.agentName || "BI\u52A9\u624B";
|
|
250
303
|
const generator = new MockResponseGenerator(defaultName);
|
|
251
|
-
const mockData2 = config.data ?
|
|
304
|
+
const mockData2 = config.data ? import_mockjs2.default.mock(config.data) : {};
|
|
252
305
|
if (config.plan) {
|
|
253
306
|
generator.initPlan(config.plan);
|
|
254
307
|
}
|
|
@@ -285,7 +338,7 @@ var HistoryStreamStrategy = class {
|
|
|
285
338
|
generate(schema) {
|
|
286
339
|
const config = schema;
|
|
287
340
|
const generator = new MockResponseGenerator("BI\u52A9\u624B");
|
|
288
|
-
const historyMock =
|
|
341
|
+
const historyMock = import_mockjs2.default.mock(config.template);
|
|
289
342
|
const fullHistory = [...config.prepend || [], ...historyMock.list || historyMock];
|
|
290
343
|
generator.emitHistory(fullHistory);
|
|
291
344
|
return generator.toString().split("\n\n").map((chunk) => chunk + "\n\n");
|
|
@@ -295,7 +348,7 @@ var JsonStrategy = class {
|
|
|
295
348
|
process(config, _requestParams) {
|
|
296
349
|
const jsonConfig = config;
|
|
297
350
|
if (!jsonConfig.responseSchema) return {};
|
|
298
|
-
return
|
|
351
|
+
return import_mockjs2.default.mock(jsonConfig.responseSchema);
|
|
299
352
|
}
|
|
300
353
|
};
|
|
301
354
|
var SseStrategy = class {
|
|
@@ -317,12 +370,12 @@ var SsePageStrategy = class {
|
|
|
317
370
|
$body: requestParams,
|
|
318
371
|
$param: requestParams?.param || requestParams
|
|
319
372
|
};
|
|
320
|
-
|
|
373
|
+
import_mockjs2.default.Random.extend({
|
|
321
374
|
$query: () => context.$query,
|
|
322
375
|
$body: () => context.$body,
|
|
323
376
|
$param: () => context.$param
|
|
324
377
|
});
|
|
325
|
-
let pageEvent =
|
|
378
|
+
let pageEvent = import_mockjs2.default.mock(ssePageConfig.pageEvent);
|
|
326
379
|
pageEvent = processTemplate(pageEvent, context);
|
|
327
380
|
let maxDelay = 0;
|
|
328
381
|
if (events.length > 0) {
|
|
@@ -358,7 +411,7 @@ var StrategyFactory = {
|
|
|
358
411
|
};
|
|
359
412
|
|
|
360
413
|
// src/adapter.ts
|
|
361
|
-
var logger = createLogger("MockAdapter");
|
|
414
|
+
var logger = (0, import_core2.createLogger)("MockAdapter");
|
|
362
415
|
var MockAdapter = class _MockAdapter {
|
|
363
416
|
delay;
|
|
364
417
|
// 遗留策略(为了兼容旧版代码)
|
|
@@ -413,7 +466,7 @@ var MockAdapter = class _MockAdapter {
|
|
|
413
466
|
const error = new Error(`Request failed with status code ${effectiveConfig.status}`);
|
|
414
467
|
error.response = {
|
|
415
468
|
status: effectiveConfig.status,
|
|
416
|
-
data:
|
|
469
|
+
data: import_mockjs3.default.mock(schema),
|
|
417
470
|
headers: {}
|
|
418
471
|
};
|
|
419
472
|
reject(error);
|
|
@@ -472,10 +525,10 @@ var MockAdapter = class _MockAdapter {
|
|
|
472
525
|
resolve();
|
|
473
526
|
return;
|
|
474
527
|
}
|
|
475
|
-
const requestStart =
|
|
528
|
+
const requestStart = import_core2.dateUtils.now();
|
|
476
529
|
logger.info(`[SSE Start] Request: ${config.method} ${config.url}`, {
|
|
477
530
|
params: config.data || config.params,
|
|
478
|
-
time:
|
|
531
|
+
time: import_core2.dateUtils.dayjs().toISOString()
|
|
479
532
|
});
|
|
480
533
|
let schema = endpointConfig.responseSchema;
|
|
481
534
|
if (typeof schema === "function") {
|
|
@@ -491,7 +544,7 @@ var MockAdapter = class _MockAdapter {
|
|
|
491
544
|
if (effectiveConfig.status !== 200) {
|
|
492
545
|
const response = {
|
|
493
546
|
status: effectiveConfig.status,
|
|
494
|
-
data:
|
|
547
|
+
data: import_mockjs3.default.mock(schema),
|
|
495
548
|
headers: {}
|
|
496
549
|
};
|
|
497
550
|
if (callbacks.onResponse) {
|
|
@@ -527,7 +580,7 @@ var MockAdapter = class _MockAdapter {
|
|
|
527
580
|
config.params || config.data
|
|
528
581
|
);
|
|
529
582
|
if (Array.isArray(events)) {
|
|
530
|
-
const startTime =
|
|
583
|
+
const startTime = import_core2.dateUtils.now();
|
|
531
584
|
let eventCount = 0;
|
|
532
585
|
logger.info(
|
|
533
586
|
`[SSE Processing] Generated ${events.length} events for ${type} strategy`
|
|
@@ -535,12 +588,12 @@ var MockAdapter = class _MockAdapter {
|
|
|
535
588
|
for (const event of events) {
|
|
536
589
|
if (signal && signal.aborted) {
|
|
537
590
|
logger.info(
|
|
538
|
-
`[SSE Abort] Stream aborted by user after ${
|
|
591
|
+
`[SSE Abort] Stream aborted by user after ${import_core2.dateUtils.now() - requestStart}ms`
|
|
539
592
|
);
|
|
540
593
|
break;
|
|
541
594
|
}
|
|
542
595
|
const delay2 = typeof event.delay === "number" ? event.delay : parseInt(String(event.delay || 0));
|
|
543
|
-
const elapsed =
|
|
596
|
+
const elapsed = import_core2.dateUtils.now() - startTime;
|
|
544
597
|
const remaining = Math.max(0, delay2 - elapsed);
|
|
545
598
|
if (remaining > 0) {
|
|
546
599
|
await sleep(remaining);
|
|
@@ -569,7 +622,7 @@ data: ${JSON.stringify(event.data)}
|
|
|
569
622
|
} else if (this.isLegacySchema(schema)) {
|
|
570
623
|
chunks = this.generateAdvancedChunks(schema);
|
|
571
624
|
} else {
|
|
572
|
-
const mockData2 =
|
|
625
|
+
const mockData2 = import_mockjs3.default.mock(schema);
|
|
573
626
|
chunks = [`data: ${JSON.stringify(mockData2)}
|
|
574
627
|
|
|
575
628
|
`];
|
|
@@ -587,7 +640,7 @@ data: ${JSON.stringify(event.data)}
|
|
|
587
640
|
}
|
|
588
641
|
if (!signal || !signal.aborted) {
|
|
589
642
|
logger.info(
|
|
590
|
-
`[SSE Complete] Stream finished successfully in ${
|
|
643
|
+
`[SSE Complete] Stream finished successfully in ${import_core2.dateUtils.now() - requestStart}ms`
|
|
591
644
|
);
|
|
592
645
|
if (onFinish) onFinish();
|
|
593
646
|
}
|
|
@@ -760,7 +813,7 @@ var generateMockMessages = (sessionId) => [
|
|
|
760
813
|
key: `msg_${sessionId}_1`,
|
|
761
814
|
role: "assistant",
|
|
762
815
|
content: `\u60A8\u597D\uFF01\u6211\u662F\u60A8\u7684\u667A\u80FD\u52A9\u624B\u3002\u5F53\u524D\u4F1A\u8BDD ID: ${sessionId}\u3002\u8BF7\u95EE\u6709\u4EC0\u4E48\u53EF\u4EE5\u5E2E\u60A8\uFF1F`,
|
|
763
|
-
time:
|
|
816
|
+
time: import_core3.dateUtils.dayjs(import_core3.dateUtils.now() - 1e4).toISOString()
|
|
764
817
|
}
|
|
765
818
|
];
|
|
766
819
|
var mockData = {
|
|
@@ -771,7 +824,8 @@ var mockData = {
|
|
|
771
824
|
favoritesList: MOCK_FAVORITES
|
|
772
825
|
// Alias for compatibility if needed
|
|
773
826
|
};
|
|
774
|
-
export
|
|
827
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
828
|
+
0 && (module.exports = {
|
|
775
829
|
ChatStreamStrategy,
|
|
776
830
|
HistoryStreamStrategy,
|
|
777
831
|
JsonStrategy,
|
|
@@ -795,5 +849,4 @@ export {
|
|
|
795
849
|
processTemplate,
|
|
796
850
|
setupMock,
|
|
797
851
|
sleep
|
|
798
|
-
};
|
|
799
|
-
//# sourceMappingURL=index.mjs.map
|
|
852
|
+
});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,330 @@
|
|
|
1
|
+
import { ApiAdapter, ApiRequestConfig, ApiEndpointConfig, StreamCallbacks } from '@chatbi-v/core';
|
|
2
|
+
|
|
3
|
+
type MockMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
|
|
4
|
+
type MockType = 'json' | 'sse' | 'sse-page';
|
|
5
|
+
interface MockEvent {
|
|
6
|
+
event: string;
|
|
7
|
+
data: any;
|
|
8
|
+
/** Delay in ms, or a mockjs template string like '@increment(100)' */
|
|
9
|
+
delay?: number | string;
|
|
10
|
+
}
|
|
11
|
+
interface MockStrategy {
|
|
12
|
+
/**
|
|
13
|
+
* Process the schema and return the result.
|
|
14
|
+
* For JSON: returns the mocked object.
|
|
15
|
+
* For SSE: returns an array of formatted event strings (chunks).
|
|
16
|
+
*/
|
|
17
|
+
process(config: MockConfig, requestParams?: any): any;
|
|
18
|
+
}
|
|
19
|
+
interface BaseMockConfig {
|
|
20
|
+
/** Optional: URL pattern if used in global interceptor */
|
|
21
|
+
url?: string;
|
|
22
|
+
/** Optional: Method if used in global interceptor */
|
|
23
|
+
method?: MockMethod;
|
|
24
|
+
/** Global delay for the request */
|
|
25
|
+
delay?: number;
|
|
26
|
+
/** Optional: HTTP status code (default: 200) */
|
|
27
|
+
status?: number;
|
|
28
|
+
/** Mock type (default: 'json') */
|
|
29
|
+
type?: MockType;
|
|
30
|
+
}
|
|
31
|
+
interface JsonMockConfig extends BaseMockConfig {
|
|
32
|
+
type?: 'json';
|
|
33
|
+
/** The MockJS template for the response body */
|
|
34
|
+
responseSchema: any;
|
|
35
|
+
}
|
|
36
|
+
interface SseMockConfig extends BaseMockConfig {
|
|
37
|
+
type: 'sse';
|
|
38
|
+
/**
|
|
39
|
+
* Dictionary of event templates.
|
|
40
|
+
* Key can be a mockjs template like 'data|3-5'
|
|
41
|
+
*/
|
|
42
|
+
responseSchema: Record<string, MockEvent[]>;
|
|
43
|
+
}
|
|
44
|
+
interface SsePageMockConfig extends BaseMockConfig {
|
|
45
|
+
type: 'sse-page';
|
|
46
|
+
/** Dictionary of event templates */
|
|
47
|
+
responseSchema: Record<string, MockEvent[]>;
|
|
48
|
+
/** Special event for pagination metadata */
|
|
49
|
+
pageEvent: MockEvent;
|
|
50
|
+
}
|
|
51
|
+
type MockConfig = JsonMockConfig | SseMockConfig | SsePageMockConfig;
|
|
52
|
+
type MockSchema = Record<string, MockConfig>;
|
|
53
|
+
interface ChatStreamConfig {
|
|
54
|
+
_type: 'chat_stream';
|
|
55
|
+
agentName?: string;
|
|
56
|
+
plan?: string[];
|
|
57
|
+
data?: Record<string, any>;
|
|
58
|
+
timeline?: Array<{
|
|
59
|
+
log?: string | {
|
|
60
|
+
content: string;
|
|
61
|
+
level?: 'info' | 'warning' | 'error';
|
|
62
|
+
agent?: string;
|
|
63
|
+
};
|
|
64
|
+
planIndex?: number;
|
|
65
|
+
delay?: number;
|
|
66
|
+
}>;
|
|
67
|
+
content: string | Record<string, any> | ((data: any) => string | Record<string, any>);
|
|
68
|
+
}
|
|
69
|
+
interface HistoryStreamConfig {
|
|
70
|
+
_type: 'history_stream';
|
|
71
|
+
template: Record<string, any>;
|
|
72
|
+
prepend?: any[];
|
|
73
|
+
}
|
|
74
|
+
type AdvancedMockSchema = ChatStreamConfig | HistoryStreamConfig;
|
|
75
|
+
interface MockGeneratorStrategy {
|
|
76
|
+
generate(schema: any): string[];
|
|
77
|
+
}
|
|
78
|
+
declare function createChatStream(config: Omit<ChatStreamConfig, '_type'>): ChatStreamConfig;
|
|
79
|
+
declare function createHistoryStream(config: Omit<HistoryStreamConfig, '_type'>): HistoryStreamConfig;
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* 基于 Mock.js 的请求适配器
|
|
83
|
+
* @description 根据 API 配置中的 responseSchema 自动生成 Mock 数据,支持普通 JSON 和流式 SSE 响应
|
|
84
|
+
*/
|
|
85
|
+
declare class MockAdapter implements ApiAdapter {
|
|
86
|
+
private delay;
|
|
87
|
+
private static strategies;
|
|
88
|
+
/**
|
|
89
|
+
* 构造函数
|
|
90
|
+
* @param delay 全局模拟延迟时间(毫秒),默认 300ms
|
|
91
|
+
*/
|
|
92
|
+
constructor(delay?: number);
|
|
93
|
+
/**
|
|
94
|
+
* 注册自定义 Mock 生成策略
|
|
95
|
+
* @param key 策略唯一标识符 (e.g., 'chat_stream', 'history_stream')
|
|
96
|
+
* @param strategy 实现了 MockGeneratorStrategy 接口的策略实例
|
|
97
|
+
*/
|
|
98
|
+
static registerStrategy(key: string, strategy: MockGeneratorStrategy): void;
|
|
99
|
+
/**
|
|
100
|
+
* 处理普通 HTTP 请求(非流式)
|
|
101
|
+
* @param config 请求配置对象
|
|
102
|
+
* @param endpointConfig API 端点配置,包含 responseSchema
|
|
103
|
+
* @returns Promise 返回模拟的响应数据
|
|
104
|
+
*/
|
|
105
|
+
request<T = any>(config: ApiRequestConfig, endpointConfig?: ApiEndpointConfig): Promise<T>;
|
|
106
|
+
/**
|
|
107
|
+
* 处理流式请求 (SSE)
|
|
108
|
+
* @param config 请求配置对象
|
|
109
|
+
* @param callbacks 流式回调函数集合 (onMessage, onFinish, onError)
|
|
110
|
+
* @param endpointConfig API 端点配置,包含 responseSchema
|
|
111
|
+
*/
|
|
112
|
+
stream(config: ApiRequestConfig, callbacks: StreamCallbacks, endpointConfig?: ApiEndpointConfig): Promise<void>;
|
|
113
|
+
/**
|
|
114
|
+
* 判断是否为遗留的 schema 格式
|
|
115
|
+
*/
|
|
116
|
+
private isLegacySchema;
|
|
117
|
+
/**
|
|
118
|
+
* 判断是否为新版配置格式
|
|
119
|
+
* @description 检查配置对象中是否包含有效的 type 字段
|
|
120
|
+
*/
|
|
121
|
+
private isNewConfig;
|
|
122
|
+
/**
|
|
123
|
+
* 生成遗留的高级 schema 数据块
|
|
124
|
+
*/
|
|
125
|
+
private generateAdvancedChunks;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Mock 响应生成器
|
|
130
|
+
* @description 用于动态生成流式响应数据,支持阶段流转、日志插入和内容分块
|
|
131
|
+
*/
|
|
132
|
+
declare class MockResponseGenerator {
|
|
133
|
+
private events;
|
|
134
|
+
private sessionId;
|
|
135
|
+
private agentName;
|
|
136
|
+
private currentTodos;
|
|
137
|
+
constructor(agentName?: string, sessionId?: string);
|
|
138
|
+
/**
|
|
139
|
+
* 生成历史消息流
|
|
140
|
+
*/
|
|
141
|
+
emitHistory(history: {
|
|
142
|
+
role: string;
|
|
143
|
+
content: string;
|
|
144
|
+
createTime: string;
|
|
145
|
+
todos?: any[];
|
|
146
|
+
}[]): this;
|
|
147
|
+
/**
|
|
148
|
+
* 初始化执行计划
|
|
149
|
+
* @param steps 计划步骤列表
|
|
150
|
+
*/
|
|
151
|
+
initPlan(steps: string[]): this;
|
|
152
|
+
/**
|
|
153
|
+
* 更新执行计划状态
|
|
154
|
+
* @param activeIndex 当前正在进行的步骤索引
|
|
155
|
+
*/
|
|
156
|
+
updatePlanStatus(activeIndex: number): this;
|
|
157
|
+
/**
|
|
158
|
+
* 标记所有计划为完成
|
|
159
|
+
*/
|
|
160
|
+
completePlan(): this;
|
|
161
|
+
/**
|
|
162
|
+
* 添加日志
|
|
163
|
+
* @param content 日志内容
|
|
164
|
+
* @param type 日志类型
|
|
165
|
+
* @param agentName 可选的 Agent 名称
|
|
166
|
+
*/
|
|
167
|
+
addLog(content: string, type?: 'info' | 'warning' | 'error', agentName?: string): this;
|
|
168
|
+
/**
|
|
169
|
+
* 添加系统错误事件
|
|
170
|
+
* @param errorCode 错误码
|
|
171
|
+
* @param errorMessage 错误信息
|
|
172
|
+
*/
|
|
173
|
+
addError(errorCode: string, errorMessage: string): this;
|
|
174
|
+
/**
|
|
175
|
+
* 添加流式内容块
|
|
176
|
+
* @param content 完整内容
|
|
177
|
+
* @param chunkSize 分块大小
|
|
178
|
+
*/
|
|
179
|
+
streamContent(content: string, chunkSize?: number): this;
|
|
180
|
+
/**
|
|
181
|
+
* 结束流
|
|
182
|
+
*/
|
|
183
|
+
finish(): this;
|
|
184
|
+
/**
|
|
185
|
+
* 生成 A2UI 响应
|
|
186
|
+
* @param component A2UI 组件配置对象
|
|
187
|
+
*/
|
|
188
|
+
emitA2UI(component: Record<string, any>): this;
|
|
189
|
+
/**
|
|
190
|
+
* 生成最终的响应字符串
|
|
191
|
+
*/
|
|
192
|
+
toString(): string;
|
|
193
|
+
private pushTodos;
|
|
194
|
+
private pushEvent;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
declare global {
|
|
198
|
+
interface Window {
|
|
199
|
+
_originalFetch: typeof fetch;
|
|
200
|
+
_originalEventSource: typeof EventSource;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
declare function installFetchMock(schema: MockSchema): void;
|
|
204
|
+
declare function installSSEMock(): void;
|
|
205
|
+
declare function setupMock(schema: MockSchema): void;
|
|
206
|
+
|
|
207
|
+
declare class ChatStreamStrategy implements MockGeneratorStrategy {
|
|
208
|
+
generate(schema: any): string[];
|
|
209
|
+
}
|
|
210
|
+
declare class HistoryStreamStrategy implements MockGeneratorStrategy {
|
|
211
|
+
generate(schema: any): string[];
|
|
212
|
+
}
|
|
213
|
+
declare class JsonStrategy implements MockStrategy {
|
|
214
|
+
process(config: MockConfig, _requestParams?: any): any;
|
|
215
|
+
}
|
|
216
|
+
declare class SseStrategy implements MockStrategy {
|
|
217
|
+
process(config: MockConfig, requestParams?: any): MockEvent[];
|
|
218
|
+
}
|
|
219
|
+
declare class SsePageStrategy implements MockStrategy {
|
|
220
|
+
process(config: MockConfig, requestParams?: any): MockEvent[];
|
|
221
|
+
}
|
|
222
|
+
declare const StrategyFactory: {
|
|
223
|
+
getStrategy(type?: string): MockStrategy;
|
|
224
|
+
};
|
|
225
|
+
|
|
226
|
+
declare const sleep: (ms: number) => Promise<unknown>;
|
|
227
|
+
/**
|
|
228
|
+
* Process string templates like "{{$query.pageNo * 10}}" in object values.
|
|
229
|
+
* Supports $query (for backward compatibility), $body, and $param.
|
|
230
|
+
*/
|
|
231
|
+
declare function processTemplate(data: any, context: Record<string, any>): any;
|
|
232
|
+
/**
|
|
233
|
+
* Flatten eventsSchema into a sorted array of events.
|
|
234
|
+
*
|
|
235
|
+
* Supports Mock.js array generation rules in keys, e.g.:
|
|
236
|
+
* { 'data|8-12': [{ event: 'data', ... }] }
|
|
237
|
+
* -> generates 8 to 12 data events.
|
|
238
|
+
*/
|
|
239
|
+
declare function flatEvents(eventsSchema: Record<string, MockEvent[]>, requestData?: any): MockEvent[];
|
|
240
|
+
/**
|
|
241
|
+
* Convert an array of events into a ReadableStream (for SSE).
|
|
242
|
+
*/
|
|
243
|
+
declare function eventsToStream(events: MockEvent[]): ReadableStream;
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* @file index.ts
|
|
247
|
+
* @description Mock 模块入口,导出模拟数据适配器、生成器及静态测试数据
|
|
248
|
+
* @author ChatBI Team
|
|
249
|
+
*/
|
|
250
|
+
/**
|
|
251
|
+
* Mock 数据模块
|
|
252
|
+
* @description 提供前端开发所需的模拟数据适配器和工具
|
|
253
|
+
*/
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* 模拟用户数据
|
|
257
|
+
*/
|
|
258
|
+
declare const MOCK_USER: {
|
|
259
|
+
id: string;
|
|
260
|
+
name: string;
|
|
261
|
+
avatar: string;
|
|
262
|
+
role: string;
|
|
263
|
+
};
|
|
264
|
+
declare const MOCK_SESSIONS: {
|
|
265
|
+
id: string;
|
|
266
|
+
title: string;
|
|
267
|
+
date: string;
|
|
268
|
+
lastMessage: string;
|
|
269
|
+
}[];
|
|
270
|
+
declare const MOCK_FAVORITES: {
|
|
271
|
+
id: string;
|
|
272
|
+
title: string;
|
|
273
|
+
date: string;
|
|
274
|
+
}[];
|
|
275
|
+
declare const MOCK_SCENES: {
|
|
276
|
+
id: string;
|
|
277
|
+
name: string;
|
|
278
|
+
description: string;
|
|
279
|
+
code: string;
|
|
280
|
+
creator: string;
|
|
281
|
+
createTime: string;
|
|
282
|
+
tableCount: number;
|
|
283
|
+
kbCount: number;
|
|
284
|
+
qaCount: number;
|
|
285
|
+
cover: string;
|
|
286
|
+
}[];
|
|
287
|
+
declare const generateMockMessages: (sessionId: string) => {
|
|
288
|
+
key: string;
|
|
289
|
+
role: string;
|
|
290
|
+
content: string;
|
|
291
|
+
time: string;
|
|
292
|
+
}[];
|
|
293
|
+
declare const mockData: {
|
|
294
|
+
user: {
|
|
295
|
+
id: string;
|
|
296
|
+
name: string;
|
|
297
|
+
avatar: string;
|
|
298
|
+
role: string;
|
|
299
|
+
};
|
|
300
|
+
sessions: {
|
|
301
|
+
id: string;
|
|
302
|
+
title: string;
|
|
303
|
+
date: string;
|
|
304
|
+
lastMessage: string;
|
|
305
|
+
}[];
|
|
306
|
+
favorites: {
|
|
307
|
+
id: string;
|
|
308
|
+
title: string;
|
|
309
|
+
date: string;
|
|
310
|
+
}[];
|
|
311
|
+
scenes: {
|
|
312
|
+
id: string;
|
|
313
|
+
name: string;
|
|
314
|
+
description: string;
|
|
315
|
+
code: string;
|
|
316
|
+
creator: string;
|
|
317
|
+
createTime: string;
|
|
318
|
+
tableCount: number;
|
|
319
|
+
kbCount: number;
|
|
320
|
+
qaCount: number;
|
|
321
|
+
cover: string;
|
|
322
|
+
}[];
|
|
323
|
+
favoritesList: {
|
|
324
|
+
id: string;
|
|
325
|
+
title: string;
|
|
326
|
+
date: string;
|
|
327
|
+
}[];
|
|
328
|
+
};
|
|
329
|
+
|
|
330
|
+
export { type AdvancedMockSchema, type BaseMockConfig, type ChatStreamConfig, ChatStreamStrategy, type HistoryStreamConfig, HistoryStreamStrategy, type JsonMockConfig, JsonStrategy, MOCK_FAVORITES, MOCK_SCENES, MOCK_SESSIONS, MOCK_USER, MockAdapter, type MockConfig, type MockEvent, type MockGeneratorStrategy, type MockMethod, MockResponseGenerator, type MockSchema, type MockStrategy, type MockType, type SseMockConfig, type SsePageMockConfig, SsePageStrategy, SseStrategy, StrategyFactory, createChatStream, createHistoryStream, eventsToStream, flatEvents, generateMockMessages, installFetchMock, installSSEMock, mockData, processTemplate, setupMock, sleep };
|