query-contract.test.ts 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. import type { ToolsClient } from '../src/tools/client.js';
  9. const context={callId:'contract_call',sessionId:'contract_session',workspaceId:'contract_workspace'};
  10. function fixture() {
  11. const payloads: any[]=[];
  12. const client={
  13. 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:'全量金额数量'}}]}]};},
  14. async dataRead(payload:any) {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};},
  15. } as unknown as ToolsClient;
  16. return {tools:createCustomsTools({client,maxRowsPerRead:50,maxToolResultChars:24000}),payloads};
  17. }
  18. test('单表目录保留业务粒度、定义与不可加指标',async()=>{
  19. const {tools}=fixture();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 as any;
  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();const read=tools.find(t=>t.name==='data_read')!;
  27. const output=await read.execute({object_id:'obj1',limit:500,cursor:'cursor_in',count_mode:'none'},context);
  28. assert.equal(payloads[0].limit,50);assert.equal(payloads[0].cursor,'cursor_in');assert.equal(payloads[0].count_mode,'none');
  29. assert.equal((output.content as any).total_matching_rows,null);
  30. assert.equal((output.content as any).total_exact,false);
  31. assert.equal((output.content as any).truncated,true);
  32. assert.equal((output.content as any).next_cursor,'bound-cursor');
  33. assert.equal(output.rows![0]._trace.row_hash,'sha256:row');
  34. });
  35. test('截断状态和警告须进入流式工具结果,供界面直接显示',async()=>{
  36. const registry=new ToolRegistry();registry.register(defineTool({name:'bounded',description:'bounded',parameters:{type:'object'},async execute(){return {content:{truncated:true,warning:'只显示一页,不能视为全部'},rows:[]};}}));
  37. const llm=new ReplayLlmClient([{toolCalls:[{name:'bounded',arguments:{}}]},{text:'这里只展示一页。'}]);
  38. const resolve=()=>({provider:{id:'deepseek',displayName:'test',baseUrl:'http://unused',apiKey:'test',api:'openai-completions' as const,models:['deepseek-flash'],supportsTools:true},model:'deepseek-flash',temperature:0.1,maxOutputTokens:1000});
  39. const runtime=new EmbeddedHarness({registry,llm,resolve});
  40. const preset=presetFromObject({name:'bounded-test',model:{provider:'deepseek',name:'deepseek-flash'},tools:['bounded'],systemPrompt:'test',limits:{maxSteps:3}});
  41. const events=[];
  42. for await(const event of runtime.run({sessionId:'s',workspaceId:'w',messages:[{role:'user',content:'query'}],preset}))events.push(event);
  43. const event=events.find(e=>e.type==='tool_result') as any;
  44. assert.equal(event.truncated,true);assert.equal(event.warning,'只显示一页,不能视为全部');
  45. });