query-contract.test.js 4.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import { test } from 'node:test';
  2. import assert from 'node:assert/strict';
  3. import { createCustomsTools } from '../src/harness/tools/index.js';
  4. import { ToolRegistry, defineTool } from '../src/harness/registry.js';
  5. import { EmbeddedHarness } from '../src/harness/runtime.js';
  6. import { ReplayLlmClient } from '../src/harness/llm.js';
  7. import { presetFromObject } from '../src/harness/preset.js';
  8. const context = { callId: 'contract_call', sessionId: 'contract_session', workspaceId: 'contract_workspace' };
  9. function fixture() {
  10. const payloads = [];
  11. const client = {
  12. async listObjects() { return { items: [{ id: 'obj1', name: 'test', kind: 'stat', rows: 100, created_at: '2026-11-18', tables: [{ name: '查询_品牌型号月度', rows: 100, columns: ['品牌', '型号', '金额'], catalog: { subject: '品牌型号', row_grain: '方向×年月×品牌×型号', coverage: '全部纳入记录', aggregation: { non_additive: ['价格指数'] }, metric_definitions: { 金额: '严格Decimal总计' }, preferred_for: '全量金额数量' } }] }] }; },
  13. async dataRead(payload) { payloads.push(payload); return { object_id: 'obj1', table: '查询_品牌型号月度', columns: ['金额'], rows: [{ values: { 金额: '0.3' }, _trace: { object_id: 'obj1', table: '查询_品牌型号月度', row_index: 0, row_hash: 'sha256:row' } }], total: null, total_exact: false, truncated: true, truncation_reason: 'row_limit', next_cursor: 'bound-cursor', warning: '仅返回受限子集,不能作为全量', cache_hit: true, offset: 0, limit: 50, exhausted: false }; },
  14. };
  15. return { tools: createCustomsTools({ client, maxRowsPerRead: 50, maxToolResultChars: 24000 }), payloads };
  16. }
  17. test('单表目录保留业务粒度、定义与不可加指标', async () => {
  18. const { tools } = fixture();
  19. const list = tools.find(t => t.name === 'list_objects');
  20. const output = await list.execute({ object_id: 'obj1', table: '查询_品牌型号月度' }, context);
  21. const content = output.content;
  22. assert.equal(content.objects[0].tables[0].catalog.coverage, '全部纳入记录');
  23. assert.deepEqual(content.objects[0].tables[0].catalog.aggregation.non_additive, ['价格指数']);
  24. });
  25. test('查询参数透传、limit受限,未计数不能伪装成零', async () => {
  26. const { tools, payloads } = fixture();
  27. const read = tools.find(t => t.name === 'data_read');
  28. const output = await read.execute({ object_id: 'obj1', limit: 500, cursor: 'cursor_in', count_mode: 'none' }, context);
  29. assert.equal(payloads[0].limit, 50);
  30. assert.equal(payloads[0].cursor, 'cursor_in');
  31. assert.equal(payloads[0].count_mode, 'none');
  32. assert.equal(output.content.total_matching_rows, null);
  33. assert.equal(output.content.total_exact, false);
  34. assert.equal(output.content.truncated, true);
  35. assert.equal(output.content.next_cursor, 'bound-cursor');
  36. assert.equal(output.rows[0]._trace.row_hash, 'sha256:row');
  37. });
  38. test('截断状态和警告须进入流式工具结果,供界面直接显示', async () => {
  39. const registry = new ToolRegistry();
  40. registry.register(defineTool({ name: 'bounded', description: 'bounded', parameters: { type: 'object' }, async execute() { return { content: { truncated: true, warning: '只显示一页,不能视为全部' }, rows: [] }; } }));
  41. const llm = new ReplayLlmClient([{ toolCalls: [{ name: 'bounded', arguments: {} }] }, { text: '这里只展示一页。' }]);
  42. const resolve = () => ({ provider: { id: 'deepseek', displayName: 'test', baseUrl: 'http://unused', apiKey: 'test', api: 'openai-completions', models: ['deepseek-flash'], supportsTools: true }, model: 'deepseek-flash', temperature: 0.1, maxOutputTokens: 1000 });
  43. const runtime = new EmbeddedHarness({ registry, llm, resolve });
  44. const preset = presetFromObject({ name: 'bounded-test', model: { provider: 'deepseek', name: 'deepseek-flash' }, tools: ['bounded'], systemPrompt: 'test', limits: { maxSteps: 3 } });
  45. const events = [];
  46. for await (const event of runtime.run({ sessionId: 's', workspaceId: 'w', messages: [{ role: 'user', content: 'query' }], preset }))
  47. events.push(event);
  48. const event = events.find(e => e.type === 'tool_result');
  49. assert.equal(event.truncated, true);
  50. assert.equal(event.warning, '只显示一页,不能视为全部');
  51. });
  52. //# sourceMappingURL=query-contract.test.js.map