bailinghub 0.3.4 → 0.5.0
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/.gitignore +5 -0
- package/README.en.md +6 -1
- package/README.md +6 -0
- package/SECURITY.md +11 -0
- package/docker-compose.images.yml +2 -2
- package/docs/AGENT_AUTH_API.en.md +175 -0
- package/docs/AGENT_AUTH_API.md +190 -0
- package/docs/AGENT_CLIENT_QUICKSTART.en.md +212 -0
- package/docs/AGENT_CLIENT_QUICKSTART.md +206 -0
- package/docs/AGENT_CLIENT_RUNTIME_API.en.md +157 -0
- package/docs/AGENT_CLIENT_RUNTIME_API.md +107 -0
- package/docs/CHANGELOG.en.md +59 -1
- package/docs/CHANGELOG.md +27 -1
- package/docs/COMPATIBILITY.en.md +25 -0
- package/docs/CONTRACT.en.md +79 -8
- package/docs/CONTRACT.md +36 -6
- package/docs/INDEPENDENT_VALIDATION.en.md +4 -4
- package/docs/INDEPENDENT_VALIDATION.md +4 -4
- package/docs/QUICKSTART.en.md +6 -0
- package/docs/QUICKSTART.md +1 -1
- package/docs/README.en.md +8 -0
- package/docs/README.md +8 -0
- package/docs/RELEASE_NOTES_v0.4.0.en.md +39 -0
- package/docs/RELEASE_NOTES_v0.4.0.md +39 -0
- package/docs/RELEASE_NOTES_v0.5.0.en.md +41 -0
- package/docs/RELEASE_NOTES_v0.5.0.md +41 -0
- package/docs/STREAMING.en.md +32 -0
- package/docs/STREAMING.md +8 -2
- package/docs/WIDGET_RENDERERS.en.md +123 -6
- package/docs/WIDGET_RENDERERS.md +75 -6
- package/docs/user-guide/console-map.en.md +1 -2
- package/docs/user-guide/console-map.md +2 -2
- package/docs//345/205/274/345/256/271/346/200/247/344/270/216/345/215/207/347/272/247.md +9 -0
- package/package.json +3 -2
- package/schemas/config/chat-entry.schema.json +2 -1
- package/schemas/config/client.schema.json +7 -0
- package/schemas/config/common.schema.json +30 -0
- package/schemas/config/route.schema.json +11 -0
- package/scripts/check-doc-links.mjs +1 -0
- package/scripts/check-image-tags.sh +1 -1
- package/scripts/check-release-consistency.test.mjs +5 -5
- package/scripts/e2e-console.mjs +144 -0
- package/scripts/export-oss.mjs +5 -12
- package/scripts/install.sh +1 -1
- package/scripts/public-content-denylist.mjs +82 -0
- package/scripts/public-content-denylist.test.mjs +53 -0
- package/scripts/publish-images.sh +1 -1
- package/scripts/release-audit.mjs +8 -12
- package/scripts/sdk-runtime-test.mjs +171 -7
- package/scripts/security-scan.mjs +8 -5
- package/scripts/validate-examples.mjs +3 -1
- package/scripts/verify-github-repo.mjs +7 -11
- package/scripts/verify-oss-export.mjs +6 -12
- package/scripts/widget-renderer-test.mjs +244 -16
- package/sdk/php/README.md +27 -1
- package/sdk/php/src/AgentAuth.php +78 -0
- package/sdk/php/src/HubClient.php +3 -0
- package/sdk/php7/README.md +32 -2
- package/sdk/php7/src/AgentAuth.php +77 -0
- package/sdk/php7/src/HubClient.php +3 -0
- package/sql/055_agent_auth.sql +72 -0
- package/sql/056_agent_client_runtime.sql +60 -0
- package/src/adapters/channels/wecom-api.ts +1 -1
- package/src/app/agent-client-runtime.test.ts +221 -0
- package/src/app/agent-client-runtime.ts +408 -0
- package/src/app/agent-tool-invocations.test.ts +462 -0
- package/src/app/agent-tool-invocations.ts +795 -0
- package/src/app/agent-tool-job.test.ts +62 -0
- package/src/app/agent-tool-job.ts +41 -0
- package/src/app/auth.ts +2 -1
- package/src/app/kernel.ts +2 -0
- package/src/app/runtime-context.ts +8 -0
- package/src/app/tool-approvals.ts +5 -1
- package/src/app/tool-assembly.ts +19 -1
- package/src/app/tool-context.ts +2 -1
- package/src/core/config/config-codec.ts +2 -1
- package/src/core/config/config-models.test.ts +28 -1
- package/src/core/config/config-models.ts +46 -3
- package/src/core/config/config-schema.test.ts +8 -0
- package/src/core/config/route-config.test.ts +15 -0
- package/src/core/config/route-config.ts +36 -0
- package/src/core/config/tools-config.test.ts +58 -1
- package/src/core/config/tools-config.ts +84 -0
- package/src/core/contracts/tools.ts +162 -29
- package/src/core/contracts/types.ts +38 -1
- package/src/core/platform/chat-form.test.ts +147 -0
- package/src/core/platform/chat-form.ts +402 -0
- package/src/core/platform/version.ts +1 -1
- package/src/core/runtime/agent-client-trace.test.ts +154 -0
- package/src/core/runtime/agent-client-trace.ts +288 -0
- package/src/core/runtime/execution-runtime.test.ts +61 -0
- package/src/core/runtime/execution-runtime.ts +17 -0
- package/src/core/runtime/launch-runtime.ts +13 -0
- package/src/core/runtime/trace-runtime.test.ts +18 -1
- package/src/core/runtime/trace-runtime.ts +28 -3
- package/src/core/state/state-codec.ts +2 -0
- package/src/infrastructure/config/config-agent-auth-repository.test.ts +147 -0
- package/src/infrastructure/config/config-agent-auth-repository.ts +356 -0
- package/src/infrastructure/config/config-agent-client-runtime-repository.ts +253 -0
- package/src/infrastructure/config/config-approval-ledger.test.ts +32 -0
- package/src/infrastructure/config/config-approval-ledger.ts +16 -0
- package/src/infrastructure/config/config-client-repository.ts +3 -3
- package/src/infrastructure/config/config-conversation-ledger.test.ts +69 -0
- package/src/infrastructure/config/config-conversation-ledger.ts +8 -7
- package/src/infrastructure/config/config-observability-ledger.test.ts +50 -0
- package/src/infrastructure/config/config-observability-ledger.ts +65 -10
- package/src/infrastructure/config/config-route-repository.ts +4 -3
- package/src/infrastructure/config/configstore.ts +14 -3
- package/src/infrastructure/state/state-mysql.ts +2 -2
- package/src/kernel-api/v1/contracts.ts +8 -0
- package/src/routes/admin-runtime.test.ts +214 -0
- package/src/routes/admin-runtime.ts +82 -2
- package/src/routes/agent-api.test.ts +379 -0
- package/src/routes/agent-api.ts +251 -0
- package/src/routes/agent-auth.test.ts +276 -0
- package/src/routes/agent-auth.ts +395 -0
- package/src/routes/approvals.ts +4 -1
- package/src/routes/chat.test.ts +319 -7
- package/src/routes/chat.ts +166 -21
- package/src/routes/critical-routes.test.ts +235 -1
- package/src/routes/private-default.ts +2 -1
- package/src/routes/private.ts +16 -1
- package/src/routes/run.ts +79 -13
- package/web/connect/bailing-connect-php.tgz +0 -0
- package/web/connect/bailing-connect-php7.tgz +0 -0
- package/web/console/assets/{Accounts-CAwrtBiS.js → Accounts-D4RRm5D1.js} +1 -1
- package/web/console/assets/{Approvals-C2BZ86T3.js → Approvals-CsTTKb8o.js} +1 -1
- package/web/console/assets/{Audit-B-E4Y_Up.js → Audit-BzvZ7JZw.js} +1 -1
- package/web/console/assets/{BrandLockup-C18u8Lx5.js → BrandLockup-CYcA93wh.js} +1 -1
- package/web/console/assets/{Channels-DTy8ylH-.js → Channels-DRQZggqM.js} +1 -1
- package/web/console/assets/ChatEntries-BUB7BWKU.js +7 -0
- package/web/console/assets/ChatEntries-DsYtcXN6.css +1 -0
- package/web/console/assets/Clients-CQ_MonGL.css +1 -0
- package/web/console/assets/Clients-HhCyrxfz.js +3 -0
- package/web/console/assets/{Cost-2SSYs1AT.js → Cost-BZgFf1yM.js} +1 -1
- package/web/console/assets/{Credentials-B79LtxCE.js → Credentials-BTe2Q07j.js} +1 -1
- package/web/console/assets/{Diagnostics-BakcRYvb.js → Diagnostics-WL-buYli.js} +1 -1
- package/web/console/assets/{Executors-BXoyo28u.js → Executors-CGZ-7Xmp.js} +1 -1
- package/web/console/assets/{Kb-ISbzquab.js → Kb-VVw8oGsT.js} +1 -1
- package/web/console/assets/{Login-Dlu6Wgcf.js → Login-rwvmBBPl.js} +1 -1
- package/web/console/assets/{Projects-DFzmtnqg.js → Projects-CexaCEY1.js} +1 -1
- package/web/console/assets/Routes-B9ZHbQAp.js +124 -0
- package/web/console/assets/Routes-BbkFpi6Y.css +1 -0
- package/web/console/assets/Runs-4zDD9VOC.css +1 -0
- package/web/console/assets/Runs-BzFNPTwD.js +3 -0
- package/web/console/assets/{Setup-CrBAV-b9.js → Setup-Djh5yadQ.js} +1 -1
- package/web/console/assets/{Shell-B1a8DvXQ.js → Shell-kn763smL.js} +1 -1
- package/web/console/assets/{StorageBuckets-BRfL_eHb.js → StorageBuckets-CQBR41W1.js} +1 -1
- package/web/console/assets/{SystemSettings-CyirVLbq.js → SystemSettings-DgnopekD.js} +1 -1
- package/web/console/assets/{SystemStatus-DZ_OR7sk.js → SystemStatus-Bj8-kPt2.js} +1 -1
- package/web/console/assets/{Targets-CtIMa66c.js → Targets-BAfCt_nH.js} +1 -1
- package/web/console/assets/{Tools-BfDZHt01.js → Tools-xjd0NZdE.js} +1 -1
- package/web/console/assets/{index-DBiByFiO.js → index-DfJA7iJS.js} +2 -2
- package/web/console/assets/{request-DY2cxQqZ.js → request-DkNPOweH.js} +1 -1
- package/web/console/assets/{schema-6mCQ7rR5.js → schema-CgPWAu1_.js} +1 -1
- package/web/console/index.html +1 -1
- package/web/widget/widget.js +493 -14
- package/web-admin/src/pages/ChatEntries.vue +18 -6
- package/web-admin/src/pages/Clients.vue +11 -3
- package/web-admin/src/pages/Routes.vue +282 -16
- package/web-admin/src/pages/Runs.vue +159 -38
- package/web/console/assets/ChatEntries-9xbRiQov.css +0 -1
- package/web/console/assets/ChatEntries-DvD-4IeE.js +0 -7
- package/web/console/assets/Clients--D7NQJKk.css +0 -1
- package/web/console/assets/Clients-DWs9JFr0.js +0 -3
- package/web/console/assets/Routes-B-hm9HGY.js +0 -124
- package/web/console/assets/Routes-DWGQN5Pv.css +0 -1
- package/web/console/assets/Runs-CUtSsPW-.css +0 -1
- package/web/console/assets/Runs-hMRu1im3.js +0 -3
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import assert from 'node:assert/strict';
|
|
2
|
+
import test from 'node:test';
|
|
3
|
+
import type { Job, ToolApproval } from '../contracts/types';
|
|
4
|
+
import { buildAgentClientTrace, safeAgentClientAuditDetail } from './agent-client-trace';
|
|
5
|
+
import { completeTraceEntry } from './trace-runtime';
|
|
6
|
+
|
|
7
|
+
const RUN_ID = '123e4567-e89b-42d3-a456-426614174000';
|
|
8
|
+
const JOB_ID = '223e4567-e89b-42d3-a456-426614174000';
|
|
9
|
+
const INVOCATION_ID = 'a'.repeat(64);
|
|
10
|
+
|
|
11
|
+
function toolJob(): Job {
|
|
12
|
+
return {
|
|
13
|
+
job_id: JOB_ID,
|
|
14
|
+
request_id: `agent-tool:${'b'.repeat(64)}`,
|
|
15
|
+
status: 'done',
|
|
16
|
+
target: 'agent-tool-v1',
|
|
17
|
+
profile: 'general',
|
|
18
|
+
project: '',
|
|
19
|
+
source: 'agent-tool:example-business',
|
|
20
|
+
client_app_id: 'example-business',
|
|
21
|
+
agent_session_id: '323e4567-e89b-42d3-a456-426614174000',
|
|
22
|
+
on_behalf_of: 'tenant-2:user-7',
|
|
23
|
+
thread_id: 42,
|
|
24
|
+
session_id: RUN_ID,
|
|
25
|
+
input_preview: 'Agent tool staff_edit',
|
|
26
|
+
result: { state: 'executed', ok: true, business_status: 200, text: 'PRIVATE_RESULT_TEXT' },
|
|
27
|
+
metadata: {
|
|
28
|
+
agent_invocation_id: INVOCATION_ID,
|
|
29
|
+
agent_tool: 'staff_edit',
|
|
30
|
+
agent_run_id: RUN_ID,
|
|
31
|
+
agent_session_id: 'PRIVATE_AGENT_SESSION',
|
|
32
|
+
},
|
|
33
|
+
dispatch: { route_key: 'tenant-agent' },
|
|
34
|
+
created_at: '2026-08-26T05:46:10.000Z',
|
|
35
|
+
updated_at: '2026-08-26T05:46:12.000Z',
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function approval(): ToolApproval {
|
|
40
|
+
return {
|
|
41
|
+
id: 9,
|
|
42
|
+
job_id: JOB_ID,
|
|
43
|
+
request_id: 'approval-request',
|
|
44
|
+
provider: 'business',
|
|
45
|
+
tool: 'staff_edit',
|
|
46
|
+
scope: 'staff.write',
|
|
47
|
+
risk: 'medium',
|
|
48
|
+
args_hash: 'c'.repeat(64),
|
|
49
|
+
args_json: '{"name":"PRIVATE_APPROVAL_ARGS"}',
|
|
50
|
+
intent_json: '{"reason":"PRIVATE_INTENT"}',
|
|
51
|
+
on_behalf_of: 'PRIVATE_SUBJECT',
|
|
52
|
+
status: 'approved',
|
|
53
|
+
created_at: '2026-08-26T05:46:10.100Z',
|
|
54
|
+
decided_at: '2026-08-26T05:46:10.500Z',
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
test('Agent Client 会话轨迹只投影安全运行边界和工具治理摘要', () => {
|
|
59
|
+
const result = buildAgentClientTrace({
|
|
60
|
+
run: {
|
|
61
|
+
run_id: RUN_ID,
|
|
62
|
+
thread_id: 42,
|
|
63
|
+
client_app_id: 'example-business',
|
|
64
|
+
route_key: 'tenant-agent',
|
|
65
|
+
status: 'completed',
|
|
66
|
+
model: 'deepseek-chat',
|
|
67
|
+
runtime: 'deepseek-harness',
|
|
68
|
+
usage: { input_tokens: 120, output_tokens: 30, total_tokens: 150, hidden_reasoning: 'PRIVATE_REASONING' },
|
|
69
|
+
created_at: '2026-08-26T05:46:00.000Z',
|
|
70
|
+
updated_at: '2026-08-26T05:46:20.000Z',
|
|
71
|
+
completed_at: '2026-08-26T05:46:20.000Z',
|
|
72
|
+
},
|
|
73
|
+
runAudit: [completeTraceEntry({
|
|
74
|
+
ts: '2026-08-26T05:46:01.000Z', job_id: RUN_ID, request_id: 'turn-1',
|
|
75
|
+
event: 'agent_client_turn_context_ready',
|
|
76
|
+
detail: { route: 'tenant-agent', active_tools: 4, agent_session_id: 'PRIVATE_AGENT_SESSION', context: 'PRIVATE_CONTEXT' },
|
|
77
|
+
})],
|
|
78
|
+
tools: [{
|
|
79
|
+
job: toolJob(),
|
|
80
|
+
approvals: [approval()],
|
|
81
|
+
audit: [
|
|
82
|
+
completeTraceEntry({
|
|
83
|
+
ts: '2026-08-26T05:46:10.000Z', job_id: JOB_ID, request_id: 'invoke',
|
|
84
|
+
event: 'agent_tool_invocation_created',
|
|
85
|
+
detail: { invocation_id: INVOCATION_ID, tool: 'staff_edit', route: 'tenant-agent', args_hash: 'PRIVATE_HASH', execution_fingerprint: 'PRIVATE_FINGERPRINT' },
|
|
86
|
+
}),
|
|
87
|
+
{
|
|
88
|
+
...completeTraceEntry({
|
|
89
|
+
ts: '2026-08-26T05:46:10.500Z', job_id: JOB_ID, request_id: 'invoke',
|
|
90
|
+
event: 'agent_tool_invocation_state',
|
|
91
|
+
detail: { invocation_id: INVOCATION_ID, tool: 'staff_edit', state: 'awaiting_approval', ok: false },
|
|
92
|
+
}),
|
|
93
|
+
// 模拟修正严重级前已落库的历史记录。
|
|
94
|
+
severity: 'error' as const,
|
|
95
|
+
},
|
|
96
|
+
completeTraceEntry({
|
|
97
|
+
ts: '2026-08-26T05:46:11.000Z', job_id: JOB_ID, request_id: 'invoke',
|
|
98
|
+
event: 'tool_call',
|
|
99
|
+
detail: {
|
|
100
|
+
provider: 'business', tool: 'staff_edit', scope: 'staff.write', method: 'POST', path: '/staff/edit',
|
|
101
|
+
args: JSON.stringify({ id: 7, name: 'PRIVATE_ARGUMENT_VALUE' }), args_bytes: 48,
|
|
102
|
+
on_behalf_of: 'PRIVATE_SUBJECT', idempotency_key: 'PRIVATE_IDEMPOTENCY_KEY',
|
|
103
|
+
},
|
|
104
|
+
}),
|
|
105
|
+
completeTraceEntry({
|
|
106
|
+
ts: '2026-08-26T05:46:12.000Z', job_id: JOB_ID, request_id: 'invoke',
|
|
107
|
+
event: 'tool_result',
|
|
108
|
+
detail: { tool: 'staff_edit', status: 200, ok: true, duration_ms: 1000, resp: 'PRIVATE_RESPONSE_BODY', resp_bytes: 128, idempotency_key: 'PRIVATE_IDEMPOTENCY_KEY' },
|
|
109
|
+
}),
|
|
110
|
+
completeTraceEntry({
|
|
111
|
+
ts: '2026-08-26T05:46:12.500Z', job_id: JOB_ID, request_id: 'invoke',
|
|
112
|
+
event: 'tool_approved',
|
|
113
|
+
detail: {
|
|
114
|
+
approval_id: 9, tool: 'staff_edit', policy: 'confirm', by: 'reviewer',
|
|
115
|
+
summary: '把员工姓名改成 PRIVATE_APPROVAL_SUMMARY',
|
|
116
|
+
reason: 'PRIVATE_APPROVAL_REASON', error: 'PRIVATE_APPROVAL_ERROR',
|
|
117
|
+
},
|
|
118
|
+
}),
|
|
119
|
+
],
|
|
120
|
+
}],
|
|
121
|
+
}) as any;
|
|
122
|
+
|
|
123
|
+
assert.equal(result.kind, 'agent_client_run');
|
|
124
|
+
assert.deepEqual(result.run.usage, { input_tokens: 120, output_tokens: 30, total_tokens: 150 });
|
|
125
|
+
assert.equal(result.run.governance.hidden_reasoning_sync, false);
|
|
126
|
+
assert.equal(result.trace.summary.tool_invocations, 1);
|
|
127
|
+
assert.equal(result.trace.summary.approvals, 1);
|
|
128
|
+
assert.equal(result.trace.summary.error_count, 0);
|
|
129
|
+
assert.equal(result.trace.events.find((event: any) => event.event === 'agent_tool_invocation_state').severity, 'warning');
|
|
130
|
+
assert.equal(result.invocations[0].tool, 'staff_edit');
|
|
131
|
+
assert.equal(result.invocations[0].approval_status, 'approved');
|
|
132
|
+
|
|
133
|
+
const call = result.trace.events.find((event: any) => event.event === 'tool_call');
|
|
134
|
+
assert.deepEqual(call.detail.argument_keys, ['id', 'name']);
|
|
135
|
+
assert.equal(call.source, 'hub_governance');
|
|
136
|
+
const json = JSON.stringify(result);
|
|
137
|
+
for (const forbidden of [
|
|
138
|
+
'PRIVATE_REASONING', 'PRIVATE_AGENT_SESSION', 'PRIVATE_CONTEXT', 'PRIVATE_ARGUMENT_VALUE',
|
|
139
|
+
'PRIVATE_SUBJECT', 'PRIVATE_IDEMPOTENCY_KEY', 'PRIVATE_RESPONSE_BODY', 'PRIVATE_APPROVAL_ARGS',
|
|
140
|
+
'PRIVATE_INTENT', 'PRIVATE_HASH', 'PRIVATE_FINGERPRINT', 'PRIVATE_RESULT_TEXT',
|
|
141
|
+
'PRIVATE_APPROVAL_SUMMARY', 'PRIVATE_APPROVAL_REASON', 'PRIVATE_APPROVAL_ERROR',
|
|
142
|
+
]) assert.equal(json.includes(forbidden), false, `${forbidden} must not be exposed`);
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
test('Agent Client 工具调用参数只展示键名,不展示值', () => {
|
|
146
|
+
assert.deepEqual(safeAgentClientAuditDetail('tool_call', {
|
|
147
|
+
tool: 'staff_edit',
|
|
148
|
+
args: '["id","name"]',
|
|
149
|
+
idempotency_key: 'hidden',
|
|
150
|
+
summary: 'hidden summary',
|
|
151
|
+
reason: 'hidden reason',
|
|
152
|
+
error: 'hidden error',
|
|
153
|
+
}), { tool: 'staff_edit', argument_keys: ['id', 'name'] });
|
|
154
|
+
});
|
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
import type { Job, ToolApproval, TraceSeverity, TraceStage } from '../contracts/types';
|
|
2
|
+
import { redactText } from './redaction-runtime';
|
|
3
|
+
import {
|
|
4
|
+
traceSeverityOf,
|
|
5
|
+
traceStageOf,
|
|
6
|
+
traceSummaryOf,
|
|
7
|
+
traceTitleOf,
|
|
8
|
+
type RawTraceAudit,
|
|
9
|
+
} from './trace-runtime';
|
|
10
|
+
|
|
11
|
+
export type AgentClientTraceSource = 'local_agent' | 'hub_governance';
|
|
12
|
+
|
|
13
|
+
export interface AgentClientTraceRun {
|
|
14
|
+
run_id: string;
|
|
15
|
+
thread_id: number;
|
|
16
|
+
client_app_id: string;
|
|
17
|
+
route_key: string;
|
|
18
|
+
status: string;
|
|
19
|
+
model: string | null;
|
|
20
|
+
runtime: string | null;
|
|
21
|
+
usage: Record<string, unknown> | null;
|
|
22
|
+
created_at: string;
|
|
23
|
+
updated_at: string;
|
|
24
|
+
completed_at: string | null;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface AgentClientTraceToolInput {
|
|
28
|
+
job: Job;
|
|
29
|
+
audit: RawTraceAudit[];
|
|
30
|
+
approvals: ToolApproval[];
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface AgentClientTraceEvent {
|
|
34
|
+
ts: string;
|
|
35
|
+
event: string;
|
|
36
|
+
stage: TraceStage;
|
|
37
|
+
severity: TraceSeverity;
|
|
38
|
+
title: string;
|
|
39
|
+
summary: string;
|
|
40
|
+
detail: Record<string, unknown>;
|
|
41
|
+
source: AgentClientTraceSource;
|
|
42
|
+
tool_job_id?: string;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const SAFE_USAGE_KEYS = [
|
|
46
|
+
'input_tokens',
|
|
47
|
+
'cached_input_tokens',
|
|
48
|
+
'output_tokens',
|
|
49
|
+
'total_tokens',
|
|
50
|
+
'tool_calls',
|
|
51
|
+
'cost_usd',
|
|
52
|
+
] as const;
|
|
53
|
+
|
|
54
|
+
const SAFE_AUDIT_KEYS = [
|
|
55
|
+
'provider',
|
|
56
|
+
'tool',
|
|
57
|
+
'scope',
|
|
58
|
+
'method',
|
|
59
|
+
'path',
|
|
60
|
+
'status',
|
|
61
|
+
'ok',
|
|
62
|
+
'duration_ms',
|
|
63
|
+
'approval_id',
|
|
64
|
+
'state',
|
|
65
|
+
'business_status',
|
|
66
|
+
'route',
|
|
67
|
+
'invocation_id',
|
|
68
|
+
'client_app_id',
|
|
69
|
+
'agent_thread_id',
|
|
70
|
+
'thread_id',
|
|
71
|
+
'active_tools',
|
|
72
|
+
'args_bytes',
|
|
73
|
+
'args_truncated',
|
|
74
|
+
'resp_bytes',
|
|
75
|
+
'resp_truncated',
|
|
76
|
+
'policy',
|
|
77
|
+
'by',
|
|
78
|
+
'used',
|
|
79
|
+
'limit',
|
|
80
|
+
'remaining',
|
|
81
|
+
'attempt',
|
|
82
|
+
'max',
|
|
83
|
+
'via',
|
|
84
|
+
] as const;
|
|
85
|
+
|
|
86
|
+
function objectValue(value: unknown): Record<string, unknown> {
|
|
87
|
+
return value && typeof value === 'object' && !Array.isArray(value) ? value as Record<string, unknown> : {};
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function safeText(value: unknown, limit: number): string {
|
|
91
|
+
return redactText(String(value ?? '')).slice(0, limit);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function safeUsage(value: Record<string, unknown> | null): Record<string, number> | null {
|
|
95
|
+
if (!value) return null;
|
|
96
|
+
const out: Record<string, number> = {};
|
|
97
|
+
for (const key of SAFE_USAGE_KEYS) {
|
|
98
|
+
const n = Number(value[key]);
|
|
99
|
+
if (Number.isFinite(n) && n >= 0) out[key] = n;
|
|
100
|
+
}
|
|
101
|
+
return Object.keys(out).length ? out : null;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function safeScalar(value: unknown): unknown {
|
|
105
|
+
if (typeof value === 'string') return safeText(value, 512);
|
|
106
|
+
if (typeof value === 'number') return Number.isFinite(value) ? value : undefined;
|
|
107
|
+
if (typeof value === 'boolean' || value === null) return value;
|
|
108
|
+
return undefined;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function argumentKeys(value: unknown): string[] {
|
|
112
|
+
let parsed = value;
|
|
113
|
+
if (typeof value === 'string') {
|
|
114
|
+
try { parsed = JSON.parse(value) as unknown; }
|
|
115
|
+
catch { return []; }
|
|
116
|
+
}
|
|
117
|
+
const keys = Array.isArray(parsed)
|
|
118
|
+
? parsed.filter((item): item is string => typeof item === 'string')
|
|
119
|
+
: parsed && typeof parsed === 'object'
|
|
120
|
+
? Object.keys(parsed as Record<string, unknown>)
|
|
121
|
+
: [];
|
|
122
|
+
return [...new Set(keys.map((key) => safeText(key, 128)).filter(Boolean))].slice(0, 50);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* 会话页是日常审计视图,不复用完整 Job Trace 的 raw detail。
|
|
127
|
+
* 即使 Tool Provider 开了 log_payload,这里也只显示键名、字节数和治理结果;完整载荷仍留在授权的单 Job 审计页。
|
|
128
|
+
*/
|
|
129
|
+
export function safeAgentClientAuditDetail(event: string, value: unknown): Record<string, unknown> {
|
|
130
|
+
const detail = objectValue(value);
|
|
131
|
+
const out: Record<string, unknown> = {};
|
|
132
|
+
for (const key of SAFE_AUDIT_KEYS) {
|
|
133
|
+
const kept = safeScalar(detail[key]);
|
|
134
|
+
if (kept !== undefined) out[key] = kept;
|
|
135
|
+
}
|
|
136
|
+
if (event === 'tool_call') {
|
|
137
|
+
const keys = argumentKeys(detail['args']);
|
|
138
|
+
if (keys.length) out['argument_keys'] = keys;
|
|
139
|
+
}
|
|
140
|
+
return out;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function safeAuditEvent(
|
|
144
|
+
audit: RawTraceAudit,
|
|
145
|
+
source: AgentClientTraceSource,
|
|
146
|
+
toolJobId?: string,
|
|
147
|
+
): AgentClientTraceEvent {
|
|
148
|
+
const event = String(audit.event || 'unknown');
|
|
149
|
+
const detail = safeAgentClientAuditDetail(event, audit.detail);
|
|
150
|
+
const rebuiltSummary = traceSummaryOf(event, detail);
|
|
151
|
+
// Agent Client 早期记录在新增专属 stage/title 前已可能落库,查询时只对这组已知事件补齐语义。
|
|
152
|
+
// 其他审计事件仍以写入时固化的 stage/title 为事实,避免查询层重新解释历史。
|
|
153
|
+
const remapAgentEvent = event.startsWith('agent_client_') || event.startsWith('agent_tool_');
|
|
154
|
+
return {
|
|
155
|
+
ts: new Date(audit.ts).toISOString(),
|
|
156
|
+
event,
|
|
157
|
+
stage: remapAgentEvent ? traceStageOf(event, detail) : audit.stage,
|
|
158
|
+
severity: remapAgentEvent ? traceSeverityOf(event, detail) : audit.severity,
|
|
159
|
+
// title 同样由事件名重建,不投影可由写入方自定义的文本。
|
|
160
|
+
title: safeText(traceTitleOf(event), 128),
|
|
161
|
+
// 不回落到 raw audit.summary:审批 confirmPrompt 等历史摘要可能已替换过真实参数值。
|
|
162
|
+
summary: safeText(rebuiltSummary, 512),
|
|
163
|
+
detail,
|
|
164
|
+
source,
|
|
165
|
+
...(toolJobId ? { tool_job_id: toolJobId } : {}),
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
function elapsedMs(start: string, end: string | null): number | undefined {
|
|
170
|
+
if (!end) return undefined;
|
|
171
|
+
const a = new Date(start).getTime();
|
|
172
|
+
const b = new Date(end).getTime();
|
|
173
|
+
return Number.isFinite(a) && Number.isFinite(b) && b >= a ? b - a : undefined;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
function latestApproval(approvals: ToolApproval[]): ToolApproval | undefined {
|
|
177
|
+
return approvals.length ? approvals[approvals.length - 1] : undefined;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
export function buildAgentClientTrace(input: {
|
|
181
|
+
run: AgentClientTraceRun;
|
|
182
|
+
runAudit: RawTraceAudit[];
|
|
183
|
+
tools: AgentClientTraceToolInput[];
|
|
184
|
+
candidatesTruncated?: boolean;
|
|
185
|
+
}): Record<string, unknown> {
|
|
186
|
+
const runUsage = safeUsage(input.run.usage);
|
|
187
|
+
const approvalCount = input.tools.reduce((count, tool) => count + tool.approvals.length, 0);
|
|
188
|
+
const events: Array<AgentClientTraceEvent & { order: number }> = [];
|
|
189
|
+
let order = 0;
|
|
190
|
+
events.push({
|
|
191
|
+
ts: new Date(input.run.created_at).toISOString(),
|
|
192
|
+
event: 'agent_client_run_started',
|
|
193
|
+
stage: 'launch',
|
|
194
|
+
severity: 'info',
|
|
195
|
+
title: '本地智能体开始处理',
|
|
196
|
+
summary: '本轮由本地智能体负责理解、规划和工具选择',
|
|
197
|
+
detail: { planner: 'local_agent', hidden_reasoning_sync: false },
|
|
198
|
+
source: 'local_agent',
|
|
199
|
+
order: order++,
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
for (const audit of input.runAudit) {
|
|
203
|
+
if (audit.event === 'agent_client_run_completed') continue;
|
|
204
|
+
events.push({ ...safeAuditEvent(audit, 'hub_governance'), order: order++ });
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
const invocations = input.tools.map(({ job, audit, approvals }) => {
|
|
208
|
+
for (const entry of audit) {
|
|
209
|
+
events.push({ ...safeAuditEvent(entry, 'hub_governance', job.job_id), order: order++ });
|
|
210
|
+
}
|
|
211
|
+
const metadata = objectValue(job.metadata);
|
|
212
|
+
const result = objectValue(job.result);
|
|
213
|
+
const approval = latestApproval(approvals);
|
|
214
|
+
const duration = elapsedMs(job.created_at, job.updated_at);
|
|
215
|
+
return {
|
|
216
|
+
job_id: job.job_id,
|
|
217
|
+
invocation_id: safeText(metadata['agent_invocation_id'], 64),
|
|
218
|
+
tool: safeText(metadata['agent_tool'], 64),
|
|
219
|
+
status: job.status,
|
|
220
|
+
state: safeText(result['state'], 64) || null,
|
|
221
|
+
ok: typeof result['ok'] === 'boolean' ? result['ok'] : null,
|
|
222
|
+
business_status: safeScalar(result['business_status']) ?? null,
|
|
223
|
+
approval_status: approval?.status ?? null,
|
|
224
|
+
approval_id: approval?.id ?? null,
|
|
225
|
+
event_count: audit.length,
|
|
226
|
+
...(duration !== undefined ? { duration_ms: duration } : {}),
|
|
227
|
+
created_at: new Date(job.created_at).toISOString(),
|
|
228
|
+
updated_at: new Date(job.updated_at).toISOString(),
|
|
229
|
+
};
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
if (input.run.completed_at) {
|
|
233
|
+
events.push({
|
|
234
|
+
ts: new Date(input.run.completed_at).toISOString(),
|
|
235
|
+
event: 'agent_client_run_completed',
|
|
236
|
+
stage: 'delivery',
|
|
237
|
+
severity: input.run.status === 'completed' ? 'info' : input.run.status === 'failed' ? 'error' : 'warning',
|
|
238
|
+
title: '本地智能体已提交最终回复',
|
|
239
|
+
summary: [input.run.status, input.run.model, input.run.runtime].filter(Boolean).map((v) => safeText(v, 191)).join(' · '),
|
|
240
|
+
detail: { status: input.run.status },
|
|
241
|
+
source: 'local_agent',
|
|
242
|
+
order: order++,
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
events.sort((a, b) => {
|
|
247
|
+
const byTime = new Date(a.ts).getTime() - new Date(b.ts).getTime();
|
|
248
|
+
return byTime || a.order - b.order;
|
|
249
|
+
});
|
|
250
|
+
const publicEvents = events.map(({ order: _order, ...event }) => event);
|
|
251
|
+
const duration = elapsedMs(input.run.created_at, input.run.completed_at ?? input.run.updated_at);
|
|
252
|
+
|
|
253
|
+
return {
|
|
254
|
+
kind: 'agent_client_run',
|
|
255
|
+
run: {
|
|
256
|
+
run_id: input.run.run_id,
|
|
257
|
+
thread_id: input.run.thread_id,
|
|
258
|
+
client_app_id: input.run.client_app_id,
|
|
259
|
+
route_key: input.run.route_key,
|
|
260
|
+
status: input.run.status,
|
|
261
|
+
model: input.run.model,
|
|
262
|
+
runtime: input.run.runtime,
|
|
263
|
+
usage: runUsage,
|
|
264
|
+
created_at: new Date(input.run.created_at).toISOString(),
|
|
265
|
+
updated_at: new Date(input.run.updated_at).toISOString(),
|
|
266
|
+
completed_at: input.run.completed_at ? new Date(input.run.completed_at).toISOString() : null,
|
|
267
|
+
governance: {
|
|
268
|
+
planner: 'local_agent',
|
|
269
|
+
execution: 'bailinghub_governed',
|
|
270
|
+
hidden_reasoning_sync: false,
|
|
271
|
+
tool_payloads: 'summary_only',
|
|
272
|
+
},
|
|
273
|
+
},
|
|
274
|
+
trace: {
|
|
275
|
+
summary: {
|
|
276
|
+
event_count: publicEvents.length,
|
|
277
|
+
tool_invocations: invocations.length,
|
|
278
|
+
approvals: approvalCount,
|
|
279
|
+
warning_count: publicEvents.filter((event) => event.severity === 'warning').length,
|
|
280
|
+
error_count: publicEvents.filter((event) => event.severity === 'error').length,
|
|
281
|
+
...(duration !== undefined ? { duration_ms: duration } : {}),
|
|
282
|
+
partial: !!input.candidatesTruncated,
|
|
283
|
+
},
|
|
284
|
+
events: publicEvents,
|
|
285
|
+
},
|
|
286
|
+
invocations,
|
|
287
|
+
};
|
|
288
|
+
}
|
|
@@ -154,6 +154,67 @@ test('prepareAdapterContext: 仅为声明 bailing-chart 的 llm 客户端注入
|
|
|
154
154
|
assert.equal(String(nonLlmContext.targetConfig['system_prompt'] ?? ''), '基础提示');
|
|
155
155
|
});
|
|
156
156
|
|
|
157
|
+
test('prepareAdapterContext: 仅为声明 bailing-form 的 llm 客户端注入非阻塞表单提示', async () => {
|
|
158
|
+
const common = {
|
|
159
|
+
fullInput: '最终输入',
|
|
160
|
+
session,
|
|
161
|
+
projectPath: null,
|
|
162
|
+
cfg,
|
|
163
|
+
credentialStore: {
|
|
164
|
+
async get() { return dbMain; },
|
|
165
|
+
async touch() { /* no-op */ },
|
|
166
|
+
},
|
|
167
|
+
targetTimeoutMs: () => 5000,
|
|
168
|
+
async assembleToolRuntime() { return undefined; },
|
|
169
|
+
async resolveSendChannels() { return []; },
|
|
170
|
+
makeSendToolDef,
|
|
171
|
+
async runSendMessage() { return { ok: true, text: 'sent' }; },
|
|
172
|
+
route: route(),
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
const formContext = await prepareAdapterContext({
|
|
176
|
+
...common,
|
|
177
|
+
job: job({ metadata: { presentation_capabilities: { renderers: ['bailing-form'] } } }),
|
|
178
|
+
});
|
|
179
|
+
const formPrompt = String(formContext.targetConfig['system_prompt']);
|
|
180
|
+
assert.match(formPrompt, /bailing-form v1/);
|
|
181
|
+
assert.match(formPrompt, /2–8 个结构化字段/);
|
|
182
|
+
assert.match(formPrompt, /应主动输出表单/);
|
|
183
|
+
assert.match(formPrompt, /无需等待用户先提出“输出表单”/);
|
|
184
|
+
assert.match(formPrompt, /不要为了展示该能力而输出表单/);
|
|
185
|
+
assert.match(formPrompt, /text、textarea、number、date、boolean、single_select、multi_select/);
|
|
186
|
+
assert.match(formPrompt, /密码、API Key、Token、私钥/);
|
|
187
|
+
assert.match(formPrompt, /不得把表单当作审批/);
|
|
188
|
+
assert.match(formPrompt, /一次回答最多输出一个/);
|
|
189
|
+
assert.match(formPrompt, /同一会话中新的用户消息/);
|
|
190
|
+
assert.match(formPrompt, /```bailing-form\n\{"version":1/);
|
|
191
|
+
assert.doesNotMatch(formPrompt, /当前聊天客户端支持 bailing-chart/);
|
|
192
|
+
|
|
193
|
+
const noFormContext = await prepareAdapterContext({
|
|
194
|
+
...common,
|
|
195
|
+
job: job({ metadata: { presentation_capabilities: { renderers: ['bailing-chart'] } } }),
|
|
196
|
+
});
|
|
197
|
+
const noFormPrompt = String(noFormContext.targetConfig['system_prompt']);
|
|
198
|
+
assert.match(noFormPrompt, /当前聊天客户端支持 bailing-chart/);
|
|
199
|
+
assert.doesNotMatch(noFormPrompt, /bailing-form/);
|
|
200
|
+
|
|
201
|
+
const bothContext = await prepareAdapterContext({
|
|
202
|
+
...common,
|
|
203
|
+
job: job({ metadata: { presentation_capabilities: { renderers: ['bailing-chart', 'bailing-form'] } } }),
|
|
204
|
+
});
|
|
205
|
+
const bothPrompt = String(bothContext.targetConfig['system_prompt']);
|
|
206
|
+
assert.match(bothPrompt, /当前聊天客户端支持 bailing-chart/);
|
|
207
|
+
assert.match(bothPrompt, /当前聊天客户端支持 bailing-form v1/);
|
|
208
|
+
|
|
209
|
+
const nonLlmContext = await prepareAdapterContext({
|
|
210
|
+
...common,
|
|
211
|
+
credentialStore: null,
|
|
212
|
+
job: job({ target: 'custom-agent', dispatch: { target_config: {} }, metadata: { presentation_capabilities: { renderers: ['bailing-form'] } } }),
|
|
213
|
+
route: route({ target: 'custom-agent', target_config: { system_prompt: '基础提示' } }),
|
|
214
|
+
});
|
|
215
|
+
assert.equal(String(nonLlmContext.targetConfig['system_prompt'] ?? ''), '基础提示');
|
|
216
|
+
});
|
|
217
|
+
|
|
157
218
|
test('prepareAdapterContext: subject_locked 时保持工具锁定并注入可信身份边界提示', async () => {
|
|
158
219
|
const audits: Array<{ event: string; detail: Record<string, unknown> }> = [];
|
|
159
220
|
const ctx = await prepareAdapterContext({
|
|
@@ -67,6 +67,20 @@ const BAILING_CHART_PROMPT = [
|
|
|
67
67
|
'示例:```bailing-chart\n{"kind":"line","title":"近七日营业额","seriesName":"营业额","unit":"元","data":[{"label":"周一","value":12800}]}\n```',
|
|
68
68
|
].join('\n');
|
|
69
69
|
|
|
70
|
+
const BAILING_FORM_PROMPT = [
|
|
71
|
+
'【可选交互能力】当前聊天客户端支持 bailing-form v1 声明式表单。',
|
|
72
|
+
'仅在需要一次收集 2–8 个结构化字段,或让用户从明确选项中选择时使用;只需问一个简单问题时使用普通文字。',
|
|
73
|
+
'当对话上下文已经表明必须补齐上述多项信息才能继续时,应主动输出表单,无需等待用户先提出“输出表单”;如果无需用户输入即可继续完成当前任务,则不要为了展示该能力而输出表单。',
|
|
74
|
+
'一次回答最多输出一个完整的 bailing-form fenced code block,代码块前用一句话说明收集目的;代码块内必须是严格 JSON,不得包含 Markdown、HTML、JavaScript、CSS、远程脚本或数据源。',
|
|
75
|
+
'顶层只使用 version、form_id、title、description、schema、submit_label、cancel_label;version 固定为 1,form_id 和字段名使用小写字母开头的字母数字下划线/连字符,schema 是按字段名组织的对象。',
|
|
76
|
+
'字段 type 只允许 text、textarea、number、date、boolean、single_select、multi_select;公共字段只有 label、description、placeholder、required。',
|
|
77
|
+
'text/textarea 可使用 minLength/maxLength;number 可使用有限数值 min/max;date 可使用 YYYY-MM-DD 形式的 min/max;boolean 无额外字段。',
|
|
78
|
+
'single_select/multi_select 必须使用 options,选项每项只包含字符串 label 和唯一 value;不得输出额外字段、嵌套对象、文件或正则表达式。',
|
|
79
|
+
'不得通过表单索要密码、API Key、Token、私钥、银行卡号、CVV 或其他支付凭据;不得把表单当作审批、授权、确认副作用或绕过业务权限的手段。',
|
|
80
|
+
'表单提交会作为同一会话中新的用户消息继续处理;它不会暂停当前任务,也不代表已获得审批或业务授权。如果客户端未声明 bailing-form,应改用普通文字逐项提问。',
|
|
81
|
+
'示例:```bailing-form\n{"version":1,"form_id":"refund_info","title":"请补充退款信息","description":"这些信息将用于继续本次对话","schema":{"reason":{"type":"textarea","label":"退款原因","required":true,"maxLength":200},"method":{"type":"single_select","label":"退款方式","required":true,"options":[{"label":"原路退回","value":"original"},{"label":"余额退回","value":"balance"}]}},"submit_label":"提交信息","cancel_label":"取消"}\n```',
|
|
82
|
+
].join('\n');
|
|
83
|
+
|
|
70
84
|
export async function prepareAdapterContext(input: PrepareAdapterContextInput): Promise<AdapterContext> {
|
|
71
85
|
const target = input.job.target ?? '';
|
|
72
86
|
let targetConfig = normalizeTargetConfig(target, input.route?.target_config ?? input.job.dispatch?.target_config ?? {});
|
|
@@ -75,6 +89,9 @@ export async function prepareAdapterContext(input: PrepareAdapterContextInput):
|
|
|
75
89
|
if (supportsPresentationRenderer(input.job.metadata, 'bailing-chart')) {
|
|
76
90
|
targetConfig = appendSystemPrompt(targetConfig, BAILING_CHART_PROMPT);
|
|
77
91
|
}
|
|
92
|
+
if (supportsPresentationRenderer(input.job.metadata, 'bailing-form')) {
|
|
93
|
+
targetConfig = appendSystemPrompt(targetConfig, BAILING_FORM_PROMPT);
|
|
94
|
+
}
|
|
78
95
|
}
|
|
79
96
|
targetConfig = { ...targetConfig, _timeout_ms: input.targetTimeoutMs(target, targetConfig) };
|
|
80
97
|
|
|
@@ -7,6 +7,15 @@ import { resolveMemoryConfig } from './memory';
|
|
|
7
7
|
import { type MemoryStoreLike } from './memory-runtime';
|
|
8
8
|
import type { AuditEntry, Job, Route, SessionTarget } from '../contracts/types';
|
|
9
9
|
|
|
10
|
+
/** 由受信 Agent API 构造的中性身份归属;不承载付费或权益语义。 */
|
|
11
|
+
export interface AgentAttribution {
|
|
12
|
+
agentSessionId: string;
|
|
13
|
+
clientAppId: string;
|
|
14
|
+
subjectId: string;
|
|
15
|
+
businessTenantRef?: string;
|
|
16
|
+
onBehalfOf: string;
|
|
17
|
+
}
|
|
18
|
+
|
|
10
19
|
/** launchJob 的入参:触发面(/run、/chat)各自做完闸门与会话解析后交到这里。 */
|
|
11
20
|
export interface LaunchSpec {
|
|
12
21
|
requestId: string;
|
|
@@ -26,6 +35,8 @@ export interface LaunchSpec {
|
|
|
26
35
|
threadScope: string; // ''=不记总账
|
|
27
36
|
principalId: string | null;
|
|
28
37
|
channel: string; // 总账入站消息的渠道标识(接入方 app_id / 'admin' / 'chat:<entry>')
|
|
38
|
+
/** 只有独立 Agent API 可填;普通 /run 永不从请求体构造。 */
|
|
39
|
+
agentAttribution?: AgentAttribution;
|
|
29
40
|
}
|
|
30
41
|
|
|
31
42
|
export interface LaunchStateStoreLike {
|
|
@@ -79,6 +90,7 @@ export async function rejectLaunchJob(
|
|
|
79
90
|
job_id: randomUUID(), request_id: s.requestId, status: 'rejected',
|
|
80
91
|
target: s.target, profile: s.profileName, project: s.project ?? '', source: s.source,
|
|
81
92
|
client_app_id: s.clientAppId, thread_id: input.threadId, session_id: s.session.sessionId,
|
|
93
|
+
agent_session_id: s.agentAttribution?.agentSessionId, on_behalf_of: s.agentAttribution?.onBehalfOf,
|
|
82
94
|
input_preview: s.fullInput.slice(0, 200), input: sanitizeUserInput(s.fullInput),
|
|
83
95
|
dispatch: { target_config: s.route?.target_config ?? {}, is_continue: s.session.isContinue, route_key: s.routeKey ?? undefined, route_name: s.route?.name },
|
|
84
96
|
error: input.error, metadata: s.metadata, callback_url: s.callbackUrl, created_at: deps.now(), updated_at: deps.now(),
|
|
@@ -150,6 +162,7 @@ export async function launchJobRecord(s: LaunchSpec, deps: LaunchRuntimeDeps): P
|
|
|
150
162
|
job_id: randomUUID(), request_id: s.requestId, status: 'queued',
|
|
151
163
|
target: s.target, profile: s.profileName, project: s.project ?? '', source: s.source,
|
|
152
164
|
client_app_id: s.clientAppId, thread_id: threadId, session_id: s.session.sessionId,
|
|
165
|
+
agent_session_id: s.agentAttribution?.agentSessionId, on_behalf_of: s.agentAttribution?.onBehalfOf,
|
|
153
166
|
input_preview: s.fullInput.slice(0, 200),
|
|
154
167
|
input: initial ? initial.dispatchInput : safeUserInput,
|
|
155
168
|
dispatch: baseDispatch as Job['dispatch'],
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// 覆盖:trace 解释层。audit 是事实账本,本模块负责把散装事件稳定映射为阶段、级别、摘要和统计。
|
|
2
2
|
import { test } from 'node:test';
|
|
3
3
|
import assert from 'node:assert/strict';
|
|
4
|
-
import { buildJobTrace, completeTraceEntry, normalizeTraceEvent, traceSeverityOf, traceStageOf } from './trace-runtime';
|
|
4
|
+
import { buildJobTrace, completeTraceEntry, normalizeTraceEvent, traceSeverityOf, traceStageOf, traceSummaryOf } from './trace-runtime';
|
|
5
5
|
import type { Job } from '../contracts/types';
|
|
6
6
|
|
|
7
7
|
function job(extra: Partial<Job> = {}): Job {
|
|
@@ -135,6 +135,23 @@ test('traceSeverityOf: 错误、降级、跳过和正常事件分级', () => {
|
|
|
135
135
|
assert.equal(traceSeverityOf('delivery_skipped'), 'warning');
|
|
136
136
|
assert.equal(traceSeverityOf('retry_scheduled'), 'warning');
|
|
137
137
|
assert.equal(traceSeverityOf('tool_result', { ok: true, status: 200 }), 'info');
|
|
138
|
+
assert.equal(traceSeverityOf('agent_tool_invocation_state', { state: 'in_progress', ok: false }), 'info');
|
|
139
|
+
assert.equal(traceSeverityOf('agent_tool_invocation_state', { state: 'awaiting_approval', ok: false }), 'warning');
|
|
140
|
+
assert.equal(traceSeverityOf('agent_tool_invocation_state', { state: 'reconciliation_required', ok: false }), 'error');
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
test('Agent Client 生命周期与直调工具事件使用明确的展示阶段和标题', () => {
|
|
144
|
+
assert.equal(traceStageOf('agent_client_run_started'), 'launch');
|
|
145
|
+
assert.equal(traceStageOf('agent_client_turn_context_ready'), 'context');
|
|
146
|
+
assert.equal(traceStageOf('agent_tool_invocation_created'), 'tool');
|
|
147
|
+
assert.equal(traceStageOf('agent_client_run_completed'), 'delivery');
|
|
148
|
+
assert.equal(completeTraceEntry({
|
|
149
|
+
ts: '2026-08-26T00:00:00.000Z', job_id: 'run', request_id: 'turn',
|
|
150
|
+
event: 'agent_client_turn_context_ready', detail: { route: 'tenant-agent', active_tools: 3 },
|
|
151
|
+
}).title, '运行上下文已就绪');
|
|
152
|
+
assert.equal(traceSummaryOf('agent_tool_invocation_state', {
|
|
153
|
+
tool: 'staff_edit', state: 'executed', business_status: 200,
|
|
154
|
+
}), 'staff_edit · executed · business=200');
|
|
138
155
|
});
|
|
139
156
|
|
|
140
157
|
test('completeTraceEntry + normalizeTraceEvent: 写入时固化结构化 trace 字段并保留对象 detail', () => {
|