fakes.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. import { mkdirSync, mkdtempSync } from 'node:fs';
  2. import os from 'node:os';
  3. import path from 'node:path';
  4. import { HttpError } from '../src/util/common.js';
  5. export function tempDir(prefix = 'cda-test-') {
  6. const directory = mkdtempSync(path.join(os.tmpdir(), prefix));
  7. mkdirSync(directory, { recursive: true });
  8. return directory;
  9. }
  10. export function testSettings(dataDir) {
  11. return {
  12. repoRoot: path.resolve(import.meta.dirname, '..', '..', '..'),
  13. host: '127.0.0.1',
  14. port: 0,
  15. toolsUrl: 'http://127.0.0.1:1',
  16. dataDir,
  17. webDist: path.resolve(import.meta.dirname, '..', '..', '..', 'web', 'dist'),
  18. presetFile: path.resolve(import.meta.dirname, '..', '..', '..', 'agent.cordis.yml'),
  19. apiKey: 'test-key',
  20. baseUrl: 'http://127.0.0.1:1',
  21. modelOverride: null,
  22. providerOverride: null,
  23. maxUploadBytes: 10 * 1024 * 1024,
  24. verificationEnabled: true,
  25. llmMode: 'replay',
  26. replayFile: '',
  27. };
  28. }
  29. /** 假工具服务:内存对象库 + 固定数据,用于 BFF 单测与联调。 */
  30. export class FakeToolsClient {
  31. workspaces = [
  32. { id: 'ws_test', name: '测试工作区', description: '', created_at: '2026-01-01T00:00:00+08:00', object_count: 1 },
  33. ];
  34. objects = [];
  35. runs = [];
  36. dataRows = {};
  37. calculatorResult = '400316.13';
  38. processSheets = [
  39. { name: '1.0-3.0_MLCC', sqlite_name: 's_1_0_3_0_MLCC', rows: 20, columns: ['基础类目', '年度', '月度', '实际金额(美元)'] },
  40. ];
  41. constructor() {
  42. this.objects.push(makeObject({ id: 'obj_raw', name: 'small.csv', kind: 'raw', rows: 100 }));
  43. this.objects.push(makeObject({ id: 'obj_stat', name: 'small_统计结果.xlsx', kind: 'stat', rows: 23, tables: this.processSheets }));
  44. this.dataRows['obj_stat:1.0-3.0_MLCC'] = [
  45. { values: { 基础类目: '多层瓷介电容器(MLCC)', 年度: 2025, 月度: 1, '实际金额(美元)': '400316.13' }, row_index: 0, row_hash: 'sha256:aaa' },
  46. { values: { 基础类目: '多层瓷介电容器(MLCC)', 年度: 2025, 月度: 2, '实际金额(美元)': '410000.00' }, row_index: 1, row_hash: 'sha256:bbb' },
  47. ];
  48. }
  49. async health() {
  50. return { ok: true, service: 'tools', version: 'test', data_dir: '/tmp' };
  51. }
  52. async meta() {
  53. return { tools: ['customs_process', 'data_read', 'calculator'] };
  54. }
  55. async listWorkspaces() {
  56. return { items: this.workspaces };
  57. }
  58. async createWorkspace(name, description = '') {
  59. const workspace = { id: `ws_${this.workspaces.length + 1}`, name, description, created_at: '2026-01-01T00:00:00+08:00', object_count: 0 };
  60. this.workspaces.push(workspace);
  61. return workspace;
  62. }
  63. async deleteWorkspace(id) {
  64. this.workspaces = this.workspaces.filter((item) => item.id !== id);
  65. return { ok: true };
  66. }
  67. async listObjects(workspaceId, kind) {
  68. const items = this.objects.filter((item) => (!kind ? true : item.kind === kind));
  69. return { items };
  70. }
  71. async getObject(objectId) {
  72. const item = this.objects.find((entry) => entry.id === objectId);
  73. if (!item)
  74. throw new HttpError(404, `对象不存在: ${objectId}`);
  75. return item;
  76. }
  77. async deleteObject(objectId) {
  78. this.objects = this.objects.filter((item) => item.id !== objectId);
  79. return { ok: true };
  80. }
  81. async uploadObject(_workspaceId, filename, bytes) {
  82. const object = makeObject({ id: `obj_raw_${this.objects.length + 1}`, name: filename, kind: 'raw', rows: bytes.length });
  83. this.objects.push(object);
  84. return object;
  85. }
  86. async preview(objectId, table = 'raw', limit = 20, offset = 0) {
  87. const all = this.dataRows[`${objectId}:${table}`] ?? [];
  88. const page = all.slice(offset, offset + limit);
  89. return {
  90. columns: Object.keys(all[0]?.values ?? {}),
  91. rows: page.map((row) => row.values),
  92. row_indexes: page.map((row) => row.row_index ?? 0),
  93. row_hashes: page.map((row) => row.row_hash ?? 'sha256:x'),
  94. offset,
  95. limit,
  96. total: all.length,
  97. };
  98. }
  99. async row(objectId, table, rowIndex) {
  100. const all = this.dataRows[`${objectId}:${table}`] ?? [];
  101. const row = all.find((item) => (item.row_index ?? 0) === rowIndex);
  102. if (!row)
  103. throw new HttpError(404, `表 ${table} 中没有第 ${rowIndex} 行`);
  104. return {
  105. object_id: objectId,
  106. table,
  107. row_index: rowIndex,
  108. row_hash: row.row_hash ?? 'sha256:x',
  109. values: row.values,
  110. lineage: { source_object_id: 'obj_raw', source_rows: { year: '2025' } },
  111. };
  112. }
  113. async lineage(objectId) {
  114. return { object_id: objectId, parents: ['obj_raw'], lineage: { output_hash: 'sha256:out' } };
  115. }
  116. async download() {
  117. return new Response('col1,col2\n1,2\n', { headers: { 'content-type': 'text/csv' } });
  118. }
  119. async createRun(payload) {
  120. const run = {
  121. id: `run_${this.runs.length + 1}`,
  122. object_id: 'obj_stat',
  123. input_object: payload.input_object_id,
  124. status: 'succeeded',
  125. operation: payload.operation ?? 'full_pipeline',
  126. progress: 1,
  127. message: '处理完成',
  128. error: '',
  129. started_at: '2026-01-01T00:00:00+08:00',
  130. finished_at: '2026-01-01T00:00:10+08:00',
  131. params: payload.options ?? {},
  132. };
  133. this.runs.push(run);
  134. return run;
  135. }
  136. async getRun(runId) {
  137. const run = this.runs.find((item) => item.id === runId);
  138. if (!run)
  139. throw new HttpError(404, '任务不存在');
  140. return run;
  141. }
  142. async listRuns() {
  143. return { items: this.runs };
  144. }
  145. async processCustoms(payload) {
  146. return {
  147. run_id: 'run_tool_1',
  148. object_id: 'obj_stat_new',
  149. name: payload.output_filename ?? 'small_统计结果.xlsx',
  150. sheets: this.processSheets,
  151. lineage: { input_sha256: 'sha256:in', params_hash: 'sha256:p', output_hash: 'sha256:out', engine: { name: 'fake', version: '1' } },
  152. stats: { included: 100 },
  153. warnings: [],
  154. files: ['统计结果.xlsx'],
  155. };
  156. }
  157. async dataRead(payload) {
  158. const rows = (this.dataRows[`${payload.object_id}:${payload.table ?? 'raw'}`] ?? []).slice(payload.offset ?? 0, (payload.offset ?? 0) + (payload.limit ?? 20));
  159. return {
  160. object_id: payload.object_id,
  161. table: payload.table ?? 'raw',
  162. columns: Object.keys(rows[0]?.values ?? {}),
  163. rows: rows.map((row) => ({
  164. values: row.values,
  165. _trace: {
  166. object_id: payload.object_id,
  167. table: payload.table ?? 'raw',
  168. row_index: row.row_index ?? 0,
  169. row_hash: row.row_hash ?? 'sha256:x',
  170. },
  171. })),
  172. total: (this.dataRows[`${payload.object_id}:${payload.table ?? 'raw'}`] ?? []).length,
  173. offset: payload.offset ?? 0,
  174. limit: payload.limit ?? 20,
  175. exhausted: true,
  176. };
  177. }
  178. async calculate(payload) {
  179. const values = (payload.inputs ?? []).map((input) => Number(input.value));
  180. const result = values.length ? String(values.reduce((sum, value) => sum + value, 0)) : this.calculatorResult;
  181. return {
  182. call_id: 'calc_test_1',
  183. tool: 'calculator',
  184. op: payload.op,
  185. result: payload.op === 'sum' ? result : this.calculatorResult,
  186. expression: payload.op,
  187. inputs: (payload.inputs ?? []).map((input, index) => ({
  188. index,
  189. value: String(input.value),
  190. from_call_id: input.from_call_id ?? null,
  191. row_hash: input.row_hash ?? null,
  192. verified: Boolean(input.from_call_id),
  193. })),
  194. unverified_inputs: 0,
  195. all_inputs_verified: true,
  196. _trace: { calculation_id: 'calc_test_1', result_hash: 'sha256:calc', op: payload.op },
  197. };
  198. }
  199. async ledgerStats() {
  200. return { calls: 1, indexed_values: 2, chain_ok: true };
  201. }
  202. }
  203. export function makeObject(input) {
  204. return {
  205. id: input.id,
  206. workspace_id: 'ws_test',
  207. name: input.name,
  208. kind: input.kind,
  209. media_type: 'text/csv',
  210. path: `/data/${input.id}`,
  211. sha256: 'sha256:obj',
  212. bytes: 1024,
  213. rows: input.rows,
  214. columns: ['I_E_FLAG', 'HS_CODE', 'MONORSUM'],
  215. producer: 'upload',
  216. parents: [],
  217. meta: {},
  218. created_at: '2026-01-01T00:00:00+08:00',
  219. tables: input.tables ?? [{ name: 'raw', sqlite_name: 'raw', rows: input.rows, columns: ['I_E_FLAG', 'HS_CODE'] }],
  220. };
  221. }
  222. export function asToolsClient(fake) {
  223. return fake;
  224. }
  225. export function listen(server) {
  226. return new Promise((resolve, reject) => {
  227. server.listen(0, '127.0.0.1', () => {
  228. const address = server.address();
  229. resolve({
  230. url: `http://127.0.0.1:${address.port}`,
  231. server,
  232. close: () => new Promise((done) => {
  233. server.close(() => done());
  234. }),
  235. });
  236. });
  237. server.on('error', reject);
  238. });
  239. }
  240. /** 解析 SSE 响应为事件数组。 */
  241. export async function collectSse(response) {
  242. const text = await response.text();
  243. const events = [];
  244. for (const block of text.split('\n\n')) {
  245. const lines = block.split('\n').filter(Boolean);
  246. let event = 'message';
  247. let data = '';
  248. for (const line of lines) {
  249. if (line.startsWith('event:'))
  250. event = line.slice(6).trim();
  251. else if (line.startsWith('data:'))
  252. data += line.slice(5).trim();
  253. }
  254. if (!data)
  255. continue;
  256. try {
  257. events.push({ event, data: JSON.parse(data) });
  258. }
  259. catch {
  260. events.push({ event, data });
  261. }
  262. }
  263. return events;
  264. }
  265. //# sourceMappingURL=fakes.js.map