@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
package/dist/index.d.ts
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 };
|
package/dist/index.js
CHANGED
|
@@ -1,78 +1,25 @@
|
|
|
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
|
-
|
|
30
1
|
// src/index.ts
|
|
31
|
-
|
|
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");
|
|
2
|
+
import { dateUtils as dateUtils3 } from "@chatbi-v/core";
|
|
59
3
|
|
|
60
4
|
// src/adapter.ts
|
|
61
|
-
|
|
62
|
-
|
|
5
|
+
import {
|
|
6
|
+
createLogger,
|
|
7
|
+
dateUtils as dateUtils2
|
|
8
|
+
} from "@chatbi-v/core";
|
|
9
|
+
import Mock3 from "mockjs";
|
|
63
10
|
|
|
64
11
|
// src/strategies.ts
|
|
65
|
-
|
|
12
|
+
import Mock2 from "mockjs";
|
|
66
13
|
|
|
67
14
|
// src/generator.ts
|
|
68
|
-
|
|
15
|
+
import { dateUtils } from "@chatbi-v/core";
|
|
69
16
|
var MockResponseGenerator = class {
|
|
70
17
|
events = [];
|
|
71
18
|
sessionId;
|
|
72
19
|
agentName;
|
|
73
20
|
currentTodos = [];
|
|
74
21
|
constructor(agentName = "Assistant", sessionId) {
|
|
75
|
-
this.sessionId = sessionId || "conv_" +
|
|
22
|
+
this.sessionId = sessionId || "conv_" + dateUtils.now();
|
|
76
23
|
this.agentName = agentName;
|
|
77
24
|
}
|
|
78
25
|
/**
|
|
@@ -212,8 +159,8 @@ data: ${JSON.stringify(data)}
|
|
|
212
159
|
};
|
|
213
160
|
|
|
214
161
|
// src/utils.ts
|
|
215
|
-
|
|
216
|
-
|
|
162
|
+
import dayjs from "dayjs";
|
|
163
|
+
import Mock from "mockjs";
|
|
217
164
|
var sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
218
165
|
function processTemplate(data, context) {
|
|
219
166
|
if (typeof data === "string") {
|
|
@@ -250,12 +197,12 @@ function flatEvents(eventsSchema, requestData = {}) {
|
|
|
250
197
|
$body: requestData,
|
|
251
198
|
$param: requestData?.param || requestData
|
|
252
199
|
};
|
|
253
|
-
|
|
200
|
+
Mock.Random.extend({
|
|
254
201
|
$query: () => context.$query,
|
|
255
202
|
$body: () => context.$body,
|
|
256
203
|
$param: () => context.$param
|
|
257
204
|
});
|
|
258
|
-
const compiled =
|
|
205
|
+
const compiled = Mock.mock(eventsSchema);
|
|
259
206
|
return Object.values(compiled).flat().map((event) => processTemplate(event, context)).sort((a, b) => {
|
|
260
207
|
const delayA = typeof a.delay === "number" ? a.delay : parseInt(String(a.delay || 0));
|
|
261
208
|
const delayB = typeof b.delay === "number" ? b.delay : parseInt(String(b.delay || 0));
|
|
@@ -272,10 +219,10 @@ function eventsToStream(events) {
|
|
|
272
219
|
}
|
|
273
220
|
(async () => {
|
|
274
221
|
try {
|
|
275
|
-
const startTime = (
|
|
222
|
+
const startTime = dayjs().valueOf();
|
|
276
223
|
for (const eventItem of events) {
|
|
277
224
|
const delay = typeof eventItem.delay === "number" ? eventItem.delay : parseInt(String(eventItem.delay || 0));
|
|
278
|
-
const elapsed = (
|
|
225
|
+
const elapsed = dayjs().valueOf() - startTime;
|
|
279
226
|
const remaining = Math.max(0, delay - elapsed);
|
|
280
227
|
if (remaining > 0) {
|
|
281
228
|
await sleep(remaining);
|
|
@@ -301,7 +248,7 @@ var ChatStreamStrategy = class {
|
|
|
301
248
|
const config = schema;
|
|
302
249
|
const defaultName = config.agentName || "BI\u52A9\u624B";
|
|
303
250
|
const generator = new MockResponseGenerator(defaultName);
|
|
304
|
-
const mockData2 = config.data ?
|
|
251
|
+
const mockData2 = config.data ? Mock2.mock(config.data) : {};
|
|
305
252
|
if (config.plan) {
|
|
306
253
|
generator.initPlan(config.plan);
|
|
307
254
|
}
|
|
@@ -338,7 +285,7 @@ var HistoryStreamStrategy = class {
|
|
|
338
285
|
generate(schema) {
|
|
339
286
|
const config = schema;
|
|
340
287
|
const generator = new MockResponseGenerator("BI\u52A9\u624B");
|
|
341
|
-
const historyMock =
|
|
288
|
+
const historyMock = Mock2.mock(config.template);
|
|
342
289
|
const fullHistory = [...config.prepend || [], ...historyMock.list || historyMock];
|
|
343
290
|
generator.emitHistory(fullHistory);
|
|
344
291
|
return generator.toString().split("\n\n").map((chunk) => chunk + "\n\n");
|
|
@@ -348,7 +295,7 @@ var JsonStrategy = class {
|
|
|
348
295
|
process(config, _requestParams) {
|
|
349
296
|
const jsonConfig = config;
|
|
350
297
|
if (!jsonConfig.responseSchema) return {};
|
|
351
|
-
return
|
|
298
|
+
return Mock2.mock(jsonConfig.responseSchema);
|
|
352
299
|
}
|
|
353
300
|
};
|
|
354
301
|
var SseStrategy = class {
|
|
@@ -370,12 +317,12 @@ var SsePageStrategy = class {
|
|
|
370
317
|
$body: requestParams,
|
|
371
318
|
$param: requestParams?.param || requestParams
|
|
372
319
|
};
|
|
373
|
-
|
|
320
|
+
Mock2.Random.extend({
|
|
374
321
|
$query: () => context.$query,
|
|
375
322
|
$body: () => context.$body,
|
|
376
323
|
$param: () => context.$param
|
|
377
324
|
});
|
|
378
|
-
let pageEvent =
|
|
325
|
+
let pageEvent = Mock2.mock(ssePageConfig.pageEvent);
|
|
379
326
|
pageEvent = processTemplate(pageEvent, context);
|
|
380
327
|
let maxDelay = 0;
|
|
381
328
|
if (events.length > 0) {
|
|
@@ -411,7 +358,7 @@ var StrategyFactory = {
|
|
|
411
358
|
};
|
|
412
359
|
|
|
413
360
|
// src/adapter.ts
|
|
414
|
-
var logger =
|
|
361
|
+
var logger = createLogger("MockAdapter");
|
|
415
362
|
var MockAdapter = class _MockAdapter {
|
|
416
363
|
delay;
|
|
417
364
|
// 遗留策略(为了兼容旧版代码)
|
|
@@ -466,7 +413,7 @@ var MockAdapter = class _MockAdapter {
|
|
|
466
413
|
const error = new Error(`Request failed with status code ${effectiveConfig.status}`);
|
|
467
414
|
error.response = {
|
|
468
415
|
status: effectiveConfig.status,
|
|
469
|
-
data:
|
|
416
|
+
data: Mock3.mock(schema),
|
|
470
417
|
headers: {}
|
|
471
418
|
};
|
|
472
419
|
reject(error);
|
|
@@ -525,10 +472,10 @@ var MockAdapter = class _MockAdapter {
|
|
|
525
472
|
resolve();
|
|
526
473
|
return;
|
|
527
474
|
}
|
|
528
|
-
const requestStart =
|
|
475
|
+
const requestStart = dateUtils2.now();
|
|
529
476
|
logger.info(`[SSE Start] Request: ${config.method} ${config.url}`, {
|
|
530
477
|
params: config.data || config.params,
|
|
531
|
-
time:
|
|
478
|
+
time: dateUtils2.dayjs().toISOString()
|
|
532
479
|
});
|
|
533
480
|
let schema = endpointConfig.responseSchema;
|
|
534
481
|
if (typeof schema === "function") {
|
|
@@ -544,7 +491,7 @@ var MockAdapter = class _MockAdapter {
|
|
|
544
491
|
if (effectiveConfig.status !== 200) {
|
|
545
492
|
const response = {
|
|
546
493
|
status: effectiveConfig.status,
|
|
547
|
-
data:
|
|
494
|
+
data: Mock3.mock(schema),
|
|
548
495
|
headers: {}
|
|
549
496
|
};
|
|
550
497
|
if (callbacks.onResponse) {
|
|
@@ -580,7 +527,7 @@ var MockAdapter = class _MockAdapter {
|
|
|
580
527
|
config.params || config.data
|
|
581
528
|
);
|
|
582
529
|
if (Array.isArray(events)) {
|
|
583
|
-
const startTime =
|
|
530
|
+
const startTime = dateUtils2.now();
|
|
584
531
|
let eventCount = 0;
|
|
585
532
|
logger.info(
|
|
586
533
|
`[SSE Processing] Generated ${events.length} events for ${type} strategy`
|
|
@@ -588,12 +535,12 @@ var MockAdapter = class _MockAdapter {
|
|
|
588
535
|
for (const event of events) {
|
|
589
536
|
if (signal && signal.aborted) {
|
|
590
537
|
logger.info(
|
|
591
|
-
`[SSE Abort] Stream aborted by user after ${
|
|
538
|
+
`[SSE Abort] Stream aborted by user after ${dateUtils2.now() - requestStart}ms`
|
|
592
539
|
);
|
|
593
540
|
break;
|
|
594
541
|
}
|
|
595
542
|
const delay2 = typeof event.delay === "number" ? event.delay : parseInt(String(event.delay || 0));
|
|
596
|
-
const elapsed =
|
|
543
|
+
const elapsed = dateUtils2.now() - startTime;
|
|
597
544
|
const remaining = Math.max(0, delay2 - elapsed);
|
|
598
545
|
if (remaining > 0) {
|
|
599
546
|
await sleep(remaining);
|
|
@@ -622,7 +569,7 @@ data: ${JSON.stringify(event.data)}
|
|
|
622
569
|
} else if (this.isLegacySchema(schema)) {
|
|
623
570
|
chunks = this.generateAdvancedChunks(schema);
|
|
624
571
|
} else {
|
|
625
|
-
const mockData2 =
|
|
572
|
+
const mockData2 = Mock3.mock(schema);
|
|
626
573
|
chunks = [`data: ${JSON.stringify(mockData2)}
|
|
627
574
|
|
|
628
575
|
`];
|
|
@@ -640,7 +587,7 @@ data: ${JSON.stringify(event.data)}
|
|
|
640
587
|
}
|
|
641
588
|
if (!signal || !signal.aborted) {
|
|
642
589
|
logger.info(
|
|
643
|
-
`[SSE Complete] Stream finished successfully in ${
|
|
590
|
+
`[SSE Complete] Stream finished successfully in ${dateUtils2.now() - requestStart}ms`
|
|
644
591
|
);
|
|
645
592
|
if (onFinish) onFinish();
|
|
646
593
|
}
|
|
@@ -813,7 +760,7 @@ var generateMockMessages = (sessionId) => [
|
|
|
813
760
|
key: `msg_${sessionId}_1`,
|
|
814
761
|
role: "assistant",
|
|
815
762
|
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`,
|
|
816
|
-
time:
|
|
763
|
+
time: dateUtils3.dayjs(dateUtils3.now() - 1e4).toISOString()
|
|
817
764
|
}
|
|
818
765
|
];
|
|
819
766
|
var mockData = {
|
|
@@ -824,8 +771,7 @@ var mockData = {
|
|
|
824
771
|
favoritesList: MOCK_FAVORITES
|
|
825
772
|
// Alias for compatibility if needed
|
|
826
773
|
};
|
|
827
|
-
|
|
828
|
-
0 && (module.exports = {
|
|
774
|
+
export {
|
|
829
775
|
ChatStreamStrategy,
|
|
830
776
|
HistoryStreamStrategy,
|
|
831
777
|
JsonStrategy,
|
|
@@ -849,5 +795,4 @@ var mockData = {
|
|
|
849
795
|
processTemplate,
|
|
850
796
|
setupMock,
|
|
851
797
|
sleep
|
|
852
|
-
}
|
|
853
|
-
//# sourceMappingURL=index.js.map
|
|
798
|
+
};
|
package/package.json
CHANGED
|
@@ -1,15 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@chatbi-v/mocks",
|
|
3
|
-
"version": "2.1.
|
|
3
|
+
"version": "2.1.8",
|
|
4
|
+
"type": "module",
|
|
4
5
|
"main": "dist/index.cjs",
|
|
6
|
+
"module": "dist/index.mjs",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
5
8
|
"files": [
|
|
6
9
|
"dist"
|
|
7
10
|
],
|
|
8
11
|
"publishConfig": {
|
|
9
12
|
"access": "public"
|
|
10
13
|
},
|
|
11
|
-
"module": "dist/index.mjs",
|
|
12
|
-
"types": "dist/index.d.ts",
|
|
13
14
|
"exports": {
|
|
14
15
|
".": {
|
|
15
16
|
"types": "./dist/index.d.ts",
|
|
@@ -21,13 +22,13 @@
|
|
|
21
22
|
"mockjs": "^1.1.0",
|
|
22
23
|
"dayjs": "^1.11.10",
|
|
23
24
|
"react": ">=18.0.0",
|
|
24
|
-
"@chatbi-v/core": "2.1.
|
|
25
|
+
"@chatbi-v/core": "2.1.8"
|
|
25
26
|
},
|
|
26
27
|
"devDependencies": {
|
|
27
28
|
"@types/mockjs": "^1.0.10",
|
|
28
29
|
"tsup": "^8.5.1",
|
|
29
|
-
"@chatbi-v/
|
|
30
|
-
"@chatbi-v/
|
|
30
|
+
"@chatbi-v/config": "2.1.8",
|
|
31
|
+
"@chatbi-v/cli": "2.1.8"
|
|
31
32
|
},
|
|
32
33
|
"scripts": {
|
|
33
34
|
"build": "chatbi-cli build",
|