来源仓库:jxc/ai-fresh-delivery 来源提交:f0b1a180e2deb7e4673b9d1c8542619f7bc0f73d 快照树: 09f679334ff38f0ff25e907961c94a8223bcaedd(与来源 HEAD 工作树逐字节一致,3801 个文件) 按约定本仓库不带任何历史提交:来源仓库的历史中含真实凭据 (生产 SSH 私钥 deploy/id_rsa_tencent、server/.env、.env.tencent/.env.production/.env.gitea、 deploy/小程序AppSecret.txt,其中 MP_APPSECRET / S3 AccessKey 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
233 lines
10 KiB
TypeScript
233 lines
10 KiB
TypeScript
import { readFileSync } from 'node:fs';
|
|
import path from 'node:path';
|
|
import { describe, expect, it } from 'vitest';
|
|
import {
|
|
EMPTY_WORKING_SET,
|
|
WORKING_SET_MAX_ENTITIES,
|
|
extractEntities,
|
|
mergeWorkingSet,
|
|
renderWorkingSet,
|
|
workingSetChanged,
|
|
} from '../src/services/assistantWorkingSet';
|
|
import { normalizeWorkingSet } from '../src/services/assistantWorkingSetStore';
|
|
import { executeCapability } from '../src/capabilities/registry';
|
|
import { registerBuiltinCapabilities } from '../src/capabilities/registerBuiltins';
|
|
import { READ_CONVERSATION_HISTORY } from '../src/capabilities/registerAssistantMemory';
|
|
|
|
/**
|
|
* P2b:会话记忆三级(工作集 + 近场原文 + 记忆地图/检索工具)。
|
|
* 这里覆盖纯逻辑与接线;真实 DB 行为见本次提交的本地验证记录。
|
|
*/
|
|
describe('工作集:入参也要参与抽取(2026-09-23 核查修复)', () => {
|
|
it('改单/预览等"等确认"场景(result 为空)仍能记住订单号', () => {
|
|
const merged = mergeWorkingSet(
|
|
{ entities: [], lastTool: null, pendingCapability: null },
|
|
{
|
|
toolName: 'prepare_order_edit',
|
|
args: { order_code: 'ORD_WEB_20260920_BR002_0002', sku_name: '土豆', quantity: 12 },
|
|
result: null,
|
|
pendingCapability: 'prepare_order_edit',
|
|
}
|
|
);
|
|
// 同一对象里的名称键(sku_name)会给该对象新抽到的第一个实体打标签
|
|
expect(merged.entities).toEqual([{ kind: '订单', id: 'ORD_WEB_20260920_BR002_0002', label: '土豆' }]);
|
|
expect(merged.pendingCapability).toBe('prepare_order_edit');
|
|
expect(renderWorkingSet(merged)).toContain('ORD_WEB_20260920_BR002_0002');
|
|
});
|
|
|
|
it('入参里的商品行带名称标签(camelCase 的 skuName/skuCode 也能配对)', () => {
|
|
const merged = mergeWorkingSet(
|
|
{ entities: [], lastTool: null, pendingCapability: null },
|
|
{
|
|
toolName: 'preview_order',
|
|
args: { items: [{ skuName: '白萝卜', skuCode: 'SKU78664646578', quantity: 5 }] },
|
|
result: null,
|
|
}
|
|
);
|
|
expect(merged.entities).toContainEqual({ kind: '商品', id: 'SKU78664646578', label: '白萝卜' });
|
|
});
|
|
|
|
it('入参与结果重复时按 kind+id 去重(不重复堆积)', () => {
|
|
const merged = mergeWorkingSet(
|
|
{ entities: [], lastTool: null, pendingCapability: null },
|
|
{
|
|
toolName: 'list_customers',
|
|
args: { customer_code: 'CUS34935362710' },
|
|
result: { rows: [{ customer_code: 'CUS34935362710', company_name: '自井分公司' }] },
|
|
}
|
|
);
|
|
expect(merged.entities.filter((row) => row.id === 'CUS34935362710')).toHaveLength(1);
|
|
});
|
|
});
|
|
|
|
describe('extractEntities', () => {
|
|
it('从嵌套结果里抽取订单/客户/商品/批次', () => {
|
|
const found = extractEntities({
|
|
data: {
|
|
rows: [
|
|
{ order_code: 'ORD_WEB_20260922_BR002_0001', customer_code: 'CUS34935362710', item_name: '前夹肉' },
|
|
{ order_code: 'ORD_WEB_20260922_BR002_0002', sku_code: 'SKUMTWEGDJY00005U', batch_no: 'IN20260922ZYH6HE' },
|
|
],
|
|
},
|
|
});
|
|
const ids = found.map((x) => x.id);
|
|
expect(ids).toContain('ORD_WEB_20260922_BR002_0001');
|
|
expect(ids).toContain('CUS34935362710');
|
|
expect(ids).toContain('SKUMTWEGDJY00005U');
|
|
expect(ids).toContain('IN20260922ZYH6HE');
|
|
expect(found.find((x) => x.id === 'ORD_WEB_20260922_BR002_0001')?.label).toBe('前夹肉');
|
|
});
|
|
|
|
it('识别结算单/自采单/售后单等编号形态', () => {
|
|
const ids = extractEntities('SS20260816001 CS20260816001 PO20260816001 OIR_ABC123').map((x) => x.kind);
|
|
expect(ids).toContain('结算单');
|
|
expect(ids).toContain('客户结算');
|
|
expect(ids).toContain('自采单');
|
|
expect(ids).toContain('售后单');
|
|
});
|
|
|
|
it('不误吞普通长数字,且有数量上限', () => {
|
|
expect(extractEntities('手机号 13800000000 金额 1234567890')).toEqual([]);
|
|
const many = Array.from({ length: 80 }, (_, i) => ({ order_code: `ORD_WEB_TEST_000${String(i).padStart(4, '0')}` }));
|
|
expect(extractEntities(many).length).toBeLessThanOrEqual(WORKING_SET_MAX_ENTITIES * 2);
|
|
});
|
|
});
|
|
|
|
describe('mergeWorkingSet', () => {
|
|
it('新实体在前、按 kind+id 去重、保留上一轮', () => {
|
|
const prev = { entities: [{ kind: '订单', id: 'ORD_WEB_TEST_0001' }], lastTool: null, pendingCapability: null };
|
|
const next = mergeWorkingSet(prev, {
|
|
toolName: 'list_orders',
|
|
args: { date_from: '2026-09-21', date_to: '2026-09-21' },
|
|
result: { rows: [{ order_code: 'ORD_WEB_TEST_0002' }, { order_code: 'ORD_WEB_TEST_0001' }] },
|
|
at: '2026-09-22T10:00:00.000Z',
|
|
});
|
|
expect(next.entities.map((x) => x.id)).toEqual(['ORD_WEB_TEST_0002', 'ORD_WEB_TEST_0001']);
|
|
expect(next.lastTool?.name).toBe('list_orders');
|
|
expect(next.lastTool?.args).toContain('date_from=2026-09-21');
|
|
});
|
|
|
|
it('实体总量有上限(长会话不会无限膨胀)', () => {
|
|
const rows = Array.from({ length: 60 }, (_, i) => ({ order_code: `ORD_WEB_X_${String(i).padStart(4, '0')}` }));
|
|
const next = mergeWorkingSet(EMPTY_WORKING_SET, { toolName: 'list_orders', result: { rows } });
|
|
expect(next.entities.length).toBeLessThanOrEqual(WORKING_SET_MAX_ENTITIES);
|
|
});
|
|
|
|
it('待确认能力会被记下', () => {
|
|
const next = mergeWorkingSet(EMPTY_WORKING_SET, {
|
|
toolName: 'prepare_order_edit',
|
|
result: { order_code: 'ORD_A' },
|
|
pendingCapability: 'prepare_order_edit',
|
|
});
|
|
expect(next.pendingCapability).toBe('prepare_order_edit');
|
|
});
|
|
});
|
|
|
|
describe('workingSetChanged / renderWorkingSet', () => {
|
|
it('无变化不标记;有变化标记', () => {
|
|
expect(workingSetChanged(EMPTY_WORKING_SET, { ...EMPTY_WORKING_SET })).toBe(false);
|
|
expect(
|
|
workingSetChanged(EMPTY_WORKING_SET, { entities: [{ kind: '订单', id: 'ORD_A' }], lastTool: null, pendingCapability: null })
|
|
).toBe(true);
|
|
});
|
|
|
|
it('渲染成分组片段并带上"数字必须重新调工具"的约束', () => {
|
|
const text = renderWorkingSet({
|
|
entities: [
|
|
{ kind: '订单', id: 'ORD_A', label: '自井食堂' },
|
|
{ kind: '订单', id: 'ORD_B' },
|
|
{ kind: '客户', id: 'CUS1' },
|
|
],
|
|
lastTool: { name: 'list_orders', args: 'date_from=2026-09-21', at: '' },
|
|
pendingCapability: 'prepare_order_edit',
|
|
});
|
|
expect(text).toContain('订单:ORD_A(自井食堂)、ORD_B');
|
|
expect(text).toContain('客户:CUS1');
|
|
expect(text).toContain('上次调用:list_orders date_from=2026-09-21');
|
|
expect(text).toContain('待确认操作:prepare_order_edit');
|
|
expect(text).toContain('必须重新调工具');
|
|
});
|
|
|
|
it('空工作集渲染为空串;超长截断', () => {
|
|
expect(renderWorkingSet(EMPTY_WORKING_SET)).toBe('');
|
|
expect(renderWorkingSet(null)).toBe('');
|
|
const big = {
|
|
entities: Array.from({ length: 24 }, (_, i) => ({ kind: '订单', id: `ORD_VERY_LONG_ID_${i}`, label: '很长的客户名称示例' })),
|
|
lastTool: null,
|
|
pendingCapability: null,
|
|
};
|
|
expect(renderWorkingSet(big, 200).length).toBeLessThanOrEqual(201);
|
|
});
|
|
|
|
it('落库归一化:脏值不会破坏结构', () => {
|
|
expect(normalizeWorkingSet(null)).toEqual(EMPTY_WORKING_SET);
|
|
expect(normalizeWorkingSet({ entities: [{ kind: '', id: '' }, { kind: '订单', id: 'X' }] }).entities).toEqual([
|
|
{ kind: '订单', id: 'X' },
|
|
]);
|
|
});
|
|
});
|
|
|
|
describe('P2b 接线棘轮', () => {
|
|
const read = (rel: string) => readFileSync(path.join(__dirname, '..', rel), 'utf8');
|
|
|
|
it('记忆检索能力已注册(只读、本会话、三端可裁剪)', () => {
|
|
const src = read('src/capabilities/registerAssistantMemory.ts');
|
|
expect(src).toContain("name: READ_CONVERSATION_HISTORY");
|
|
expect(src).toContain("effect: 'read'");
|
|
expect(src).toContain("module: 'memory'");
|
|
expect(read('src/capabilities/registerBuiltins.ts')).toContain('registerAssistantMemoryCapability();');
|
|
expect(read('src/capabilities/modules.ts')).toContain("id: 'memory'");
|
|
});
|
|
|
|
it('检索只在自己的会话里(user_id + branch_code + conversation_id 三重限定)', () => {
|
|
const src = read('src/services/assistantChatLogService.ts');
|
|
expect(src).toContain("'c.id = $1::uuid', 'c.user_id = $2', 'c.branch_code = $3'");
|
|
expect(src).toContain('export async function searchAssistantMessages');
|
|
expect(src).toContain('export async function describeAssistantHistory');
|
|
});
|
|
|
|
it('宿主:记忆工具常驻 + 工作集合并 + 记忆地图注入 + 透传 conversationId', () => {
|
|
const host = read('src/mcp/orderEntryAgentHost.ts');
|
|
expect(host).toContain("listCapabilityToolSchemas({ ctx: input.ctx, surface, modules: ['memory'] })");
|
|
expect(host).toContain('mergeWorkingSet(workingSet, {');
|
|
expect(host).toContain('记忆地图:本会话共');
|
|
expect(host).toContain('conversationId: input.sessionId ?? null');
|
|
expect(host).toContain('working_set: workingSetChanged(workingSetBefore, workingSet) ? workingSet : null');
|
|
});
|
|
|
|
it('路由:读写工作集 + 记忆地图(宿主不依赖落库服务)', () => {
|
|
const route = read('src/routes/orderEntryAgent.ts');
|
|
expect(route).toContain('loadAssistantWorkingSet({ conversationId: sessionId, userId: req.userId })');
|
|
expect(route).toContain('describeAssistantHistory({');
|
|
expect(route).toContain('saveAssistantWorkingSet({');
|
|
expect(read('src/mcp/orderEntryAgentHost.ts')).not.toContain('assistantWorkingSetStore');
|
|
});
|
|
|
|
it('迁移:加 working_set 列(幂等)', () => {
|
|
const sql = readFileSync(
|
|
path.join(__dirname, '..', '..', 'migrations', '20260922_assistant_working_set.sql'),
|
|
'utf8'
|
|
);
|
|
expect(sql).toContain('ADD COLUMN IF NOT EXISTS working_set JSONB');
|
|
});
|
|
});
|
|
|
|
describe('记忆能力的返回契约(真实调用路径)', () => {
|
|
it('executeCapability 只包一层:data 里直接是 {count, matched_total, turns}', async () => {
|
|
registerBuiltinCapabilities();
|
|
const result = await executeCapability(READ_CONVERSATION_HISTORY, {
|
|
userId: '11111111-1111-4111-8111-111111111111',
|
|
ctx: { branchCode: 'BR002', isSuperAdmin: false, permissions: [], roles: ['admin'] } as never,
|
|
args: {},
|
|
surface: 'web',
|
|
conversationId: null, // 无会话上下文 → 不查库,直接给出提示
|
|
} as never);
|
|
expect(result.ok).toBe(true);
|
|
const data = (result as { data?: Record<string, unknown> }).data || {};
|
|
expect(data.count).toBe(0);
|
|
expect(Array.isArray(data.turns)).toBe(true);
|
|
// 曾经踩过的坑:handler 又包了一层 {ok,data} → 真实字段被套两层
|
|
expect(data.data).toBeUndefined();
|
|
});
|
|
});
|