@allbluecn/dsh-compat 0.0.1 → 0.0.2
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/README.md +11 -19
- package/package.json +4 -4
- package/src/assemble/assemble.ts +1 -1
- package/src/compat/__tests__/tool-adapter.test.ts +44 -14
- package/src/compat/__tests__/view-bridge.test.ts +86 -144
- package/src/compat/context.ts +2 -0
- package/src/compat/tool-adapter.ts +14 -6
- package/src/compat/view-bridge.ts +21 -132
- package/src/surface.ts +2 -2
package/README.md
CHANGED
|
@@ -1,25 +1,17 @@
|
|
|
1
1
|
# @allbluecn/dsh-compat
|
|
2
2
|
|
|
3
|
-
DSH 插件兼容层(TanStack ai-harness 对标)。一句话定位:把 DSH 插件生态(工具/命令/配置/渲染卡)适配到 ai-harness 0.
|
|
3
|
+
DSH 插件兼容层(TanStack ai-harness 对标)。一句话定位:把 DSH 插件生态(工具/命令/配置/渲染卡)适配到 ai-harness 0.4.0,产出可装配的 PluginContributions + 兼容面守护。
|
|
4
4
|
|
|
5
5
|
**纯度边界**:本包只依赖 `@tanstack/ai-harness` / `@tanstack/ai` / `@tanstack/ai-persistence` / `yaml`,零 apps/web import、零 `@/` 别名、零 prisma/serverFns。禁止引入服务端重依赖(会被客户端 split 拉进 bundle)。
|
|
6
6
|
|
|
7
|
-
##
|
|
7
|
+
## 工具视图桥(官方 resultMetadata)
|
|
8
8
|
|
|
9
|
-
-
|
|
10
|
-
-
|
|
11
|
-
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
- presenter 抛错 / 视图缺省:`__dshView` 字段缺失或结构不合法 → 回退 `GenericToolCard`(DSH 软校验纪律,对齐规格 §8 差异表 #4)。
|
|
19
|
-
- **剥除链 / 模型不可见性**:通道 A 的剥除落点 = harness 级(或插件级)chat middleware 的 `onConfig`,只返回 `{ providerMessages: stripped }`——ai 核心仅当 middleware 显式返回 `providerMessages` 时才改模型可见面(`activities/chat/index.js:2851` `this.providerMessages = config.providerMessages ?? config.messages`;`compose.js:14` 仅在返回含 `messages` 且不含 `providerMessages` 时才回填),canonical `messages`(持久化与 transcript 来源)不被触碰。模型实际看到的 tool 消息已剥除 `__dshView`。
|
|
20
|
-
- **活更新语义**:视图一次性产出(工具 execute 期),不活更。工具后台跑完需刷新卡片时,须另补 transcript 重投影路径(F6 消费),不经剥除链。
|
|
21
|
-
- **已知限制 / 差异**:
|
|
22
|
-
- pending 态无 presenter 视图,回退通用卡 shimmer(规格 §4.1.4 明示接受)。
|
|
23
|
-
- presenter 抛错回退 `GenericToolCard`(软校验,不 fail turn)。
|
|
24
|
-
- `__dshView` 会随 content 进日志/transcript,体积入持久化(DSH 工具卡通常 KB 级,可接受;超大视图须在插件侧截断)。
|
|
25
|
-
- 通道 B 落选原因:客户端投影成本高(`SessionSnapshot` 不暴露 records,须自建 records→视图投影层 + 额外 API 面),且 `appendRecords` 走 input 通道(占用 inputId/actor 语义),对纯渲染桥属重炮打蚊子。
|
|
9
|
+
- **协议**:`presentResult` 产出的视图 JSON 经官方 `setResultMetadata({ view })` 挂到 `ToolCallPart.resultMetadata.view`;工具 execute 返回原始 raw result,模型可见 tool result 不含视图。
|
|
10
|
+
- **ai-harness patch**:`patches/@tanstack__ai-harness@0.4.0.patch` 为官方 0.4.0 的 view reducer 补上应用元数据投影:实时 `TOOL_CALL_RESULT` 与持久化 transcript 重建两条路径都过滤 `metadata.tanstack` 和 `undefined`,其余顶层键写入 `resultMetadata`。
|
|
11
|
+
- **端到端证据**:`conformance/conformance.test.ts` C1 走真实 `assembleProfile → harness → tool execute → setResultMetadata → createSessionView` reducer,断言 view 可读、raw result 不变、模型 providerMessages 的 tool content 不含 `view`。
|
|
12
|
+
- **客户端契约**:`view.store.get().messages[*].parts[*]` 中 `type === 'tool-call'` 的 part 读 `part.resultMetadata.view`。字段形状 = DSH 插件 `presentResult` 产出的视图 JSON(`{ kind, title, rows, ... }`,由 DSH 视图 schema 约束)。
|
|
13
|
+
- **pending 态**:工具调用尚无 result metadata 时回退通用卡 shimmer(`presentCall` pending 视图无下发时机,差异已接受)。
|
|
14
|
+
- **软校验**:presenter 抛错、返回标量/数组或序列化失败时不挂视图、不炸回合、result 原样返回;客户端回退 `GenericToolCard`。
|
|
15
|
+
- **截断**:单视图 UTF-8 JSON 超过 256KB 时挂 `{ kind: 'view-truncated', truncated: true, originalBytes }`,客户端据此回退通用工具卡。
|
|
16
|
+
- **活更新语义**:视图一次性产出(execute 期),不活更。工具后台跑完需刷新卡片时,须另补 transcript 重投影路径。
|
|
17
|
+
- **模型不可见性**:官方 resultMetadata 与 tool result content 天然分离,不需要 strip middleware,也不改写 canonical messages。
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@allbluecn/dsh-compat",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.2",
|
|
4
4
|
"license": "AGPL-3.0-or-later",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"files": [
|
|
@@ -19,9 +19,9 @@
|
|
|
19
19
|
},
|
|
20
20
|
"dependencies": {
|
|
21
21
|
"yaml": "2.9.0",
|
|
22
|
-
"@tanstack/ai-harness": "0.
|
|
23
|
-
"@tanstack/ai": "1.
|
|
24
|
-
"@tanstack/ai-persistence": "1.0
|
|
22
|
+
"@tanstack/ai-harness": "0.4.0",
|
|
23
|
+
"@tanstack/ai": "1.8.0",
|
|
24
|
+
"@tanstack/ai-persistence": "1.1.0"
|
|
25
25
|
},
|
|
26
26
|
"devDependencies": {
|
|
27
27
|
"typescript": "^7",
|
package/src/assemble/assemble.ts
CHANGED
|
@@ -254,7 +254,7 @@ export async function assembleProfile(
|
|
|
254
254
|
// L1 服务 shim(规格 §4.5):预跑用静态 opts.l1(assemble 期无真实 run,工厂不可用)。
|
|
255
255
|
l1: opts.l1,
|
|
256
256
|
// 工具适配器(规格 §4.1.3):parameters(JSON Schema) 直传 inputSchema +
|
|
257
|
-
// execute 上下文适配 + presenter
|
|
257
|
+
// execute 上下文适配 + presenter 捕获(官方 resultMetadata.view)。缺省时 ctx.tools.register
|
|
258
258
|
// 只存 {name, description} 桩,组装产物无法到达模型工具面——conformance 的
|
|
259
259
|
// 「工具 schema → 模型 round-trip」要求全工具面,故 assemble 统一挂适配器。
|
|
260
260
|
adaptTool: dshToolToTanStack,
|
|
@@ -81,6 +81,7 @@ describe('dshToolToTanStack', () => {
|
|
|
81
81
|
it('execute 适配上下文:abortSignal/runId → CordisToolRunContext', async () => {
|
|
82
82
|
let seen: unknown
|
|
83
83
|
const controller = new AbortController()
|
|
84
|
+
const setResultMetadata = (_value: Record<string, unknown>) => {}
|
|
84
85
|
const def = baseDef({
|
|
85
86
|
execute: (args, ctx) => {
|
|
86
87
|
seen = ctx
|
|
@@ -90,35 +91,64 @@ describe('dshToolToTanStack', () => {
|
|
|
90
91
|
const tool = dshToolToTanStack(def)
|
|
91
92
|
const result = (await tool.execute?.(
|
|
92
93
|
{ region: 'cn-north-1' },
|
|
93
|
-
{
|
|
94
|
+
{
|
|
95
|
+
abortSignal: controller.signal,
|
|
96
|
+
runId: 'run-9',
|
|
97
|
+
emitCustomEvent: () => {},
|
|
98
|
+
setResultMetadata,
|
|
99
|
+
},
|
|
94
100
|
)) as Record<string, unknown>
|
|
95
101
|
expect(result.ok).toBe(true)
|
|
96
|
-
expect(seen).toEqual({
|
|
102
|
+
expect(seen).toEqual({
|
|
103
|
+
signal: controller.signal,
|
|
104
|
+
caller: 'run-9',
|
|
105
|
+
setResultMetadata,
|
|
106
|
+
})
|
|
97
107
|
})
|
|
98
108
|
|
|
99
|
-
it('presenter
|
|
100
|
-
const
|
|
109
|
+
it('presenter 视图经 setResultMetadata 挂到 resultMetadata.view,result 原样返回', async () => {
|
|
110
|
+
const view = { kind: 'card', title: 't', rows: [] }
|
|
111
|
+
const raw = { ok: true }
|
|
101
112
|
const def = baseDef({
|
|
102
|
-
execute: () =>
|
|
103
|
-
presentResult: () =>
|
|
113
|
+
execute: () => raw,
|
|
114
|
+
presentResult: () => view,
|
|
104
115
|
})
|
|
105
116
|
const tool = dshToolToTanStack(def)
|
|
106
|
-
const
|
|
107
|
-
|
|
108
|
-
|
|
117
|
+
const captured: Record<string, unknown> = {}
|
|
118
|
+
|
|
119
|
+
const result = await tool.execute?.(
|
|
120
|
+
{ region: 'x' },
|
|
121
|
+
{
|
|
122
|
+
emitCustomEvent: () => {},
|
|
123
|
+
setResultMetadata: (value: Record<string, unknown>) => Object.assign(captured, value),
|
|
124
|
+
},
|
|
125
|
+
)
|
|
126
|
+
|
|
127
|
+
expect(result).toBe(raw)
|
|
128
|
+
expect(captured).toEqual({ view })
|
|
109
129
|
})
|
|
110
130
|
|
|
111
|
-
it('presenter 抛错 →
|
|
131
|
+
it('presenter 抛错 → 不挂 metadata,result 原样返回', async () => {
|
|
132
|
+
const raw = { ok: true }
|
|
112
133
|
const def = baseDef({
|
|
113
|
-
execute: () =>
|
|
134
|
+
execute: () => raw,
|
|
114
135
|
presentResult: () => {
|
|
115
136
|
throw new Error('presenter boom')
|
|
116
137
|
},
|
|
117
138
|
})
|
|
118
139
|
const tool = dshToolToTanStack(def)
|
|
119
|
-
const
|
|
120
|
-
|
|
121
|
-
|
|
140
|
+
const captured: Record<string, unknown> = {}
|
|
141
|
+
|
|
142
|
+
const result = await tool.execute?.(
|
|
143
|
+
{ region: 'x' },
|
|
144
|
+
{
|
|
145
|
+
emitCustomEvent: () => {},
|
|
146
|
+
setResultMetadata: (value: Record<string, unknown>) => Object.assign(captured, value),
|
|
147
|
+
},
|
|
148
|
+
)
|
|
149
|
+
|
|
150
|
+
expect(result).toBe(raw)
|
|
151
|
+
expect(captured).toEqual({})
|
|
122
152
|
})
|
|
123
153
|
|
|
124
154
|
it('timeoutMs 超时 → execute reject', async () => {
|
|
@@ -1,166 +1,108 @@
|
|
|
1
1
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
2
2
|
import { describe, expect, it } from 'vitest'
|
|
3
|
-
import type {
|
|
4
|
-
import {
|
|
5
|
-
MAX_DSH_VIEW_JSON_BYTES,
|
|
6
|
-
attachView,
|
|
7
|
-
dshViewField,
|
|
8
|
-
stripDshViewMiddleware,
|
|
9
|
-
} from '../view-bridge'
|
|
3
|
+
import type { ToolExecutionContext } from '@tanstack/ai'
|
|
4
|
+
import { dshToolToTanStack } from '../tool-adapter'
|
|
10
5
|
|
|
11
6
|
const VIEW = { kind: 'card', title: '部署完成', rows: [{ label: 'r', value: 'v' }] }
|
|
12
7
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
expect(out.__dshView).toEqual(VIEW)
|
|
20
|
-
expect(out.ok).toBe(true)
|
|
21
|
-
})
|
|
8
|
+
function metadataContext(captured: Record<string, unknown>): ToolExecutionContext {
|
|
9
|
+
return {
|
|
10
|
+
emitCustomEvent: () => {},
|
|
11
|
+
setResultMetadata: (value) => Object.assign(captured, value),
|
|
12
|
+
}
|
|
13
|
+
}
|
|
22
14
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
const
|
|
27
|
-
|
|
28
|
-
|
|
15
|
+
describe('resultMetadata.view 视图桥', () => {
|
|
16
|
+
it('presentResult 视图经 setResultMetadata 挂到 view,result 本身不被改写', async () => {
|
|
17
|
+
const raw = { ok: true }
|
|
18
|
+
const tool = dshToolToTanStack({
|
|
19
|
+
name: 'deploy',
|
|
20
|
+
parameters: { type: 'object' },
|
|
21
|
+
execute: async () => raw,
|
|
22
|
+
presentResult: () => VIEW,
|
|
23
|
+
})
|
|
24
|
+
const captured: Record<string, unknown> = {}
|
|
25
|
+
|
|
26
|
+
const result = await tool.execute!({}, metadataContext(captured))
|
|
27
|
+
|
|
28
|
+
expect(tool.resultMetadataSchema).toMatchObject({
|
|
29
|
+
type: 'object',
|
|
30
|
+
properties: { view: { type: 'object' } },
|
|
31
|
+
})
|
|
32
|
+
expect(result).toBe(raw)
|
|
33
|
+
expect(result).toEqual({ ok: true })
|
|
34
|
+
expect(captured).toEqual({ view: VIEW })
|
|
29
35
|
})
|
|
30
36
|
|
|
31
|
-
it('
|
|
32
|
-
const
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
37
|
+
it('标量 result 不被包装,视图仍经 metadata 下发', async () => {
|
|
38
|
+
const raw = 'plain-text'
|
|
39
|
+
const tool = dshToolToTanStack({
|
|
40
|
+
name: 'deploy',
|
|
41
|
+
parameters: { type: 'object' },
|
|
42
|
+
execute: () => raw,
|
|
43
|
+
presentResult: () => VIEW,
|
|
44
|
+
})
|
|
45
|
+
const captured: Record<string, unknown> = {}
|
|
36
46
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
expect(
|
|
47
|
+
const result = await tool.execute!({}, metadataContext(captured))
|
|
48
|
+
|
|
49
|
+
expect(result).toBe(raw)
|
|
50
|
+
expect(captured).toEqual({ view: VIEW })
|
|
40
51
|
})
|
|
41
52
|
|
|
42
|
-
it('presenter
|
|
43
|
-
const
|
|
44
|
-
|
|
53
|
+
it('presenter 缺省、抛错或产出非法视图时不挂 metadata,result 原样返回', async () => {
|
|
54
|
+
const circular: Record<string, unknown> = { kind: 'card' }
|
|
55
|
+
circular.self = circular
|
|
56
|
+
const presenters = [
|
|
57
|
+
undefined,
|
|
45
58
|
() => {
|
|
46
59
|
throw new Error('presenter boom')
|
|
47
60
|
},
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
expect((out as Record<string, unknown>).__dshView).toBeUndefined()
|
|
53
|
-
})
|
|
54
|
-
|
|
55
|
-
it('presenter 产出非法视图(标量/数组)→ 不挂', () => {
|
|
56
|
-
expect((attachView(() => 'nope', {}, { ok: 1 }) as Record<string, unknown>).__dshView).toBeUndefined()
|
|
57
|
-
expect((attachView(() => [1, 2], {}, { ok: 1 }) as Record<string, unknown>).__dshView).toBeUndefined()
|
|
58
|
-
})
|
|
59
|
-
|
|
60
|
-
it('超大视图截断为占位(不挂原始大对象)', () => {
|
|
61
|
-
const big = { kind: 'card', title: 'x'.repeat(MAX_DSH_VIEW_JSON_BYTES + 10) }
|
|
62
|
-
const out = attachView(() => big, {}, { ok: 1 }) as Record<string, unknown>
|
|
63
|
-
const attached = out.__dshView as Record<string, unknown>
|
|
64
|
-
expect(attached.kind).toBe('dsh-view-truncated')
|
|
65
|
-
expect(attached.__dshViewTruncated).toBe(true)
|
|
66
|
-
})
|
|
67
|
-
})
|
|
61
|
+
() => 'not-an-object',
|
|
62
|
+
() => [1, 2],
|
|
63
|
+
() => circular,
|
|
64
|
+
]
|
|
68
65
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
{
|
|
79
|
-
role: 'tool',
|
|
80
|
-
content: JSON.stringify({ ok: true, region: 'cn-north-1', __dshView: VIEW }),
|
|
81
|
-
toolCallId: 'call-1',
|
|
82
|
-
toolName: 'dsh_deploy',
|
|
83
|
-
} as ModelMessage,
|
|
84
|
-
]
|
|
85
|
-
}
|
|
66
|
+
for (const presentResult of presenters) {
|
|
67
|
+
const raw = { ok: true }
|
|
68
|
+
const tool = dshToolToTanStack({
|
|
69
|
+
name: 'deploy',
|
|
70
|
+
parameters: { type: 'object' },
|
|
71
|
+
execute: () => raw,
|
|
72
|
+
presentResult,
|
|
73
|
+
})
|
|
74
|
+
const captured: Record<string, unknown> = {}
|
|
86
75
|
|
|
87
|
-
|
|
88
|
-
const mw = stripDshViewMiddleware()
|
|
76
|
+
const result = await tool.execute!({}, metadataContext(captured))
|
|
89
77
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
const capture: AnyChatMiddleware = {
|
|
93
|
-
name: 'capture',
|
|
94
|
-
onConfig: (_ctx, config) => {
|
|
95
|
-
captured.push(config.providerMessages ?? config.messages)
|
|
96
|
-
return undefined
|
|
97
|
-
},
|
|
78
|
+
expect(result).toBe(raw)
|
|
79
|
+
expect(captured).toEqual({})
|
|
98
80
|
}
|
|
99
|
-
// 直接同步调用 onConfig:先剥除 middleware,再 capture 观察剥除后的 providerMessages。
|
|
100
|
-
const config = { messages, providerMessages: messages } as never
|
|
101
|
-
const partial = mw.onConfig?.({} as never, config)
|
|
102
|
-
const stripped = (partial as { providerMessages?: Array<ModelMessage> } | undefined)
|
|
103
|
-
?.providerMessages
|
|
104
|
-
// 把剥除结果喂给 capture,模拟链式 onConfig。
|
|
105
|
-
capture.onConfig?.({} as never, {
|
|
106
|
-
messages,
|
|
107
|
-
providerMessages: stripped ?? messages,
|
|
108
|
-
} as never)
|
|
109
|
-
return { partial, stripped: stripped ?? messages, seen: captured[0] ?? [] }
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
it('只返回 providerMessages(canonical messages 不动)', () => {
|
|
113
|
-
const messages = messagesWithView()
|
|
114
|
-
const { partial } = runOnConfig(messages)
|
|
115
|
-
const keys = Object.keys((partial as Record<string, unknown>) ?? {})
|
|
116
|
-
expect(keys).toEqual(['providerMessages'])
|
|
117
81
|
})
|
|
118
82
|
|
|
119
|
-
it('
|
|
120
|
-
const
|
|
121
|
-
const
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
})
|
|
144
|
-
|
|
145
|
-
it('carry-forward 前缀一致性:剥除只动后缀,prefix(剥除前 tool 消息之前的消息)逐字节不变', () => {
|
|
146
|
-
const messages = messagesWithView()
|
|
147
|
-
const prefix = messages.slice(0, 2) // user + assistant,位于带 __dshView 的 tool 消息之前
|
|
148
|
-
const { stripped } = runOnConfig(messages)
|
|
149
|
-
const strippedPrefix = stripped.slice(0, 2)
|
|
150
|
-
// 前缀逐条 deep-equal(同一引用,未被改写)→ 跨回合 prompt cache 前缀稳定。
|
|
151
|
-
expect(strippedPrefix).toEqual(prefix)
|
|
152
|
-
expect(strippedPrefix[0]).toBe(prefix[0])
|
|
153
|
-
expect(strippedPrefix[1]).toBe(prefix[1])
|
|
154
|
-
// 变化只落在 tool 消息(后缀),且只是去掉 __dshView 字段
|
|
155
|
-
expect(stripped[2].role).toBe('tool')
|
|
156
|
-
expect(String(stripped[2].content)).not.toContain('__dshView')
|
|
157
|
-
})
|
|
158
|
-
|
|
159
|
-
it('无 __dshView 时 onConfig 返回 undefined(不触碰 providerMessages)', () => {
|
|
160
|
-
const messages: Array<ModelMessage> = [
|
|
161
|
-
{ role: 'tool', content: JSON.stringify({ ok: true }), toolCallId: 'c', toolName: 't' } as ModelMessage,
|
|
162
|
-
]
|
|
163
|
-
const partial = mw.onConfig?.({} as never, { messages } as never)
|
|
164
|
-
expect(partial).toBeUndefined()
|
|
83
|
+
it('视图序列化超过 256KB 时挂截断占位,result 原样返回', async () => {
|
|
84
|
+
const raw = { ok: true }
|
|
85
|
+
const tool = dshToolToTanStack({
|
|
86
|
+
name: 'deploy',
|
|
87
|
+
parameters: { type: 'object' },
|
|
88
|
+
execute: () => raw,
|
|
89
|
+
presentResult: () => ({
|
|
90
|
+
kind: 'card',
|
|
91
|
+
title: 'x'.repeat(256 * 1024 + 10),
|
|
92
|
+
}),
|
|
93
|
+
})
|
|
94
|
+
const captured: Record<string, unknown> = {}
|
|
95
|
+
|
|
96
|
+
const result = await tool.execute!({}, metadataContext(captured))
|
|
97
|
+
|
|
98
|
+
expect(result).toBe(raw)
|
|
99
|
+
expect(captured.view).toEqual({
|
|
100
|
+
kind: 'view-truncated',
|
|
101
|
+
truncated: true,
|
|
102
|
+
originalBytes: expect.any(Number),
|
|
103
|
+
})
|
|
104
|
+
expect((captured.view as { originalBytes: number }).originalBytes).toBeGreaterThan(
|
|
105
|
+
256 * 1024,
|
|
106
|
+
)
|
|
165
107
|
})
|
|
166
108
|
})
|
package/src/compat/context.ts
CHANGED
|
@@ -29,6 +29,8 @@ export interface CordisToolRunContext {
|
|
|
29
29
|
signal?: AbortSignal
|
|
30
30
|
/** 调用方标识(← 官方 `context.runId`)。 */
|
|
31
31
|
caller?: string
|
|
32
|
+
/** 挂载应用专属 resultMetadata(← 官方 `context.setResultMetadata`)。 */
|
|
33
|
+
setResultMetadata?: (value: Record<string, unknown>) => void
|
|
32
34
|
}
|
|
33
35
|
|
|
34
36
|
// ---------------------------------------------------------------------------
|
|
@@ -2,15 +2,15 @@
|
|
|
2
2
|
// DSH 工具定义 → TanStack AnyTool 适配器(规格 §4.1.3)。
|
|
3
3
|
//
|
|
4
4
|
// - `parameters`(JSON Schema)直传官方 `inputSchema`;`output.schema` → `outputSchema`。
|
|
5
|
-
// - execute 适配官方 ToolExecutionContext(signal/runId)→
|
|
6
|
-
//
|
|
5
|
+
// - execute 适配官方 ToolExecutionContext(signal/runId/setResultMetadata)→
|
|
6
|
+
// CordisToolRunContext,调 DSH `def.execute`,再经 viewResultMetadata 挂官方元数据。
|
|
7
7
|
// - `timeoutMs` → execute 内超时;`deferLoading` → 官方 lazy 工具。
|
|
8
8
|
// - `isConcurrencySafe` / `projectContent` / `finalizeContent` / `presentCall` 官方无对应,
|
|
9
9
|
// 忽略并记入 adapterNotes 供 F1-5 收集。
|
|
10
10
|
// - JSON Schema 净化:去除 provider 不认的冷门/已废弃关键字(经验性防御,见 SANITIZE_STRIP_KEYS)。
|
|
11
11
|
import type { AnyTool, ToolExecutionContext } from '@tanstack/ai'
|
|
12
12
|
import type { CordisToolDef, CordisToolRunContext } from './context'
|
|
13
|
-
import {
|
|
13
|
+
import { viewResultMetadata } from './view-bridge'
|
|
14
14
|
|
|
15
15
|
/** provider 不认 / 已废弃的 JSON Schema 关键字净化表(递归剥离)。 */
|
|
16
16
|
const SANITIZE_STRIP_KEYS: ReadonlySet<string> = new Set([
|
|
@@ -62,6 +62,9 @@ export function adaptToolRunContext(
|
|
|
62
62
|
return {
|
|
63
63
|
...(tctx.abortSignal ? { signal: tctx.abortSignal } : {}),
|
|
64
64
|
...(tctx.runId ? { caller: tctx.runId } : {}),
|
|
65
|
+
...(tctx.setResultMetadata
|
|
66
|
+
? { setResultMetadata: tctx.setResultMetadata }
|
|
67
|
+
: {}),
|
|
65
68
|
}
|
|
66
69
|
}
|
|
67
70
|
|
|
@@ -84,7 +87,7 @@ function withTimeout<T>(promise: Promise<T>, ms: number): Promise<T> {
|
|
|
84
87
|
* - parameters → inputSchema(经 {@link sanitizeJsonSchema} 净化后直传)
|
|
85
88
|
* - output.schema → outputSchema
|
|
86
89
|
* - deferLoading → lazy
|
|
87
|
-
* - execute:适配上下文 → DSH execute →
|
|
90
|
+
* - execute:适配上下文 → DSH execute → viewResultMetadata → setResultMetadata
|
|
88
91
|
*/
|
|
89
92
|
export function dshToolToTanStack(def: CordisToolDef): AnyTool {
|
|
90
93
|
const inputSchema = sanitizeJsonSchema(def.parameters)
|
|
@@ -97,6 +100,10 @@ export function dshToolToTanStack(def: CordisToolDef): AnyTool {
|
|
|
97
100
|
description: def.description ?? '',
|
|
98
101
|
...(hasInput ? { inputSchema: inputSchema as AnyTool['inputSchema'] } : {}),
|
|
99
102
|
...(hasOutput ? { outputSchema: outputSchema as AnyTool['outputSchema'] } : {}),
|
|
103
|
+
resultMetadataSchema: {
|
|
104
|
+
type: 'object',
|
|
105
|
+
properties: { view: { type: 'object' } },
|
|
106
|
+
},
|
|
100
107
|
...(def.deferLoading ? { lazy: true } : {}),
|
|
101
108
|
execute: async (args: unknown, tctx?: ToolExecutionContext) => {
|
|
102
109
|
const adapted = adaptToolRunContext(tctx)
|
|
@@ -107,8 +114,9 @@ export function dshToolToTanStack(def: CordisToolDef): AnyTool {
|
|
|
107
114
|
typeof ms === 'number' && ms > 0
|
|
108
115
|
? await withTimeout(run(), ms)
|
|
109
116
|
: await run()
|
|
110
|
-
|
|
111
|
-
|
|
117
|
+
const metadata = viewResultMetadata(def.presentResult, args, raw)
|
|
118
|
+
if (metadata !== undefined) tctx?.setResultMetadata?.(metadata)
|
|
119
|
+
return raw
|
|
112
120
|
},
|
|
113
121
|
}
|
|
114
122
|
return tool
|
|
@@ -1,34 +1,17 @@
|
|
|
1
1
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
2
|
-
//
|
|
3
|
-
//
|
|
4
|
-
//
|
|
5
|
-
// `__dshView`(多视图并存用 `__dshView_<name>`),随 content 落 messages store
|
|
6
|
-
// (持久化/transcript 保留);一层 chat middleware 在 `onConfig` 只返回
|
|
7
|
-
// `{ providerMessages: stripped }` 剥除该字段——canonical `messages` 不动,
|
|
8
|
-
// 模型看不到,prompt cache 前缀稳定。客户端(F6 DshCardView)读
|
|
9
|
-
// `part.result?.__dshView`。
|
|
10
|
-
//
|
|
11
|
-
// 活更新语义:视图**一次性产出**(execute 期),不活更。若将来需要活更
|
|
12
|
-
// (工具后台跑完后刷新卡片),须另补 transcript 重投影路径,不经本文件。
|
|
13
|
-
import type { AnyChatMiddleware, ModelMessage } from '@tanstack/ai'
|
|
2
|
+
// DSH presentResult 视图桥:视图只进入官方 resultMetadata.view,模型可见的
|
|
3
|
+
// tool result 保持原样;ai-harness 的 view reducer 负责把应用元数据投影到
|
|
4
|
+
// ToolCallPart.resultMetadata。视图一次性产出,不活更。
|
|
14
5
|
|
|
15
|
-
/**
|
|
16
|
-
export const
|
|
6
|
+
/** 单个视图 JSON 序列化后的最大字节(超出挂截断占位)。 */
|
|
7
|
+
export const MAX_VIEW_JSON_BYTES = 256 * 1024
|
|
17
8
|
|
|
18
|
-
/**
|
|
19
|
-
export const MAX_DSH_VIEW_JSON_BYTES = 256 * 1024
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* presenter 产出的视图 JSON 的最小形状校验(软校验)。
|
|
23
|
-
* 合法视图 = 一个普通对象(非 null、非数组)。字段形状由 DSH 视图 schema 约束,
|
|
24
|
-
* 这里只挡住「presenter 误返回标量/数组」这类明显不合法,非法则不挂 → 客户端回退
|
|
25
|
-
* GenericToolCard(对齐包 README「presenter 抛错 / 视图缺省」与规格 §8 差异表 #4)。
|
|
26
|
-
*/
|
|
9
|
+
/** presenter 产出的视图 JSON 的最小形状校验(软校验)。 */
|
|
27
10
|
function isPlainObjectView(view: unknown): view is Record<string, unknown> {
|
|
28
11
|
return typeof view === 'object' && view !== null && !Array.isArray(view)
|
|
29
12
|
}
|
|
30
13
|
|
|
31
|
-
/** 视图 JSON
|
|
14
|
+
/** 视图 JSON 的 UTF-8 字节长度;序列化失败返回 null。 */
|
|
32
15
|
function viewJsonBytes(view: Record<string, unknown>): number | null {
|
|
33
16
|
try {
|
|
34
17
|
return Buffer.byteLength(JSON.stringify(view), 'utf8')
|
|
@@ -38,123 +21,29 @@ function viewJsonBytes(view: Record<string, unknown>): number | null {
|
|
|
38
21
|
}
|
|
39
22
|
|
|
40
23
|
/**
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
* name 须是安全标识(字母数字下划线连字符),否则回退单视图字段避免键污染。
|
|
44
|
-
*/
|
|
45
|
-
export function dshViewField(view: Record<string, unknown>): string {
|
|
46
|
-
const name = (view as { name?: unknown }).name
|
|
47
|
-
if (
|
|
48
|
-
typeof name === 'string' &&
|
|
49
|
-
name.length > 0 &&
|
|
50
|
-
/^[A-Za-z0-9_-]+$/.test(name)
|
|
51
|
-
) {
|
|
52
|
-
return `${DSH_VIEW_FIELD}_${name}`
|
|
53
|
-
}
|
|
54
|
-
return DSH_VIEW_FIELD
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/** result 上所有可能的视图字段名(剥除时用,覆盖单视图与任意 name 后缀)。 */
|
|
58
|
-
function viewFieldNames(record: Record<string, unknown>): Array<string> {
|
|
59
|
-
return Object.keys(record).filter(
|
|
60
|
-
(key) => key === DSH_VIEW_FIELD || key.startsWith(`${DSH_VIEW_FIELD}_`),
|
|
61
|
-
)
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* 把 DSH `presentResult` 产出的视图 JSON 挂到 tool result 上(通道 A)。
|
|
66
|
-
*
|
|
67
|
-
* - result 是普通对象 → 直接挂 `result[__dshView]`(或 `__dshView_<name>`)。
|
|
68
|
-
* - result 非对象(string/number/…)→ 包装成 `{ value, [__dshView] }`,让视图
|
|
69
|
-
* 仍能到达客户端;模型看到的是包装对象(presentResult 存在即视为该工具的
|
|
70
|
-
* 结果本就要渲染,属可接受差异)。
|
|
71
|
-
* - presenter 未定义 / 抛错 / 产出非法视图(非普通对象或序列化失败)→ 不挂,
|
|
72
|
-
* 返回原 result → 客户端回退 GenericToolCard(软校验,不 fail turn)。
|
|
73
|
-
* - 单视图超出 {@link MAX_DSH_VIEW_JSON_BYTES} → 截断为占位视图(不挂原始大对象)。
|
|
74
|
-
*
|
|
75
|
-
* @returns 挂好视图的 result(可能因包装/截断与入参不同),或原 result。
|
|
24
|
+
* 生成官方 resultMetadata 载荷;不改写工具 result。
|
|
25
|
+
* presenter 缺省、抛错、返回标量/数组或序列化失败时返回 undefined。
|
|
76
26
|
*/
|
|
77
|
-
export function
|
|
27
|
+
export function viewResultMetadata(
|
|
78
28
|
presentResult: ((args: unknown, result: unknown) => unknown) | undefined,
|
|
79
29
|
args: unknown,
|
|
80
30
|
result: unknown,
|
|
81
|
-
): unknown {
|
|
82
|
-
if (typeof presentResult !== 'function') return
|
|
31
|
+
): Record<string, unknown> | undefined {
|
|
32
|
+
if (typeof presentResult !== 'function') return undefined
|
|
33
|
+
|
|
83
34
|
let view: unknown
|
|
84
35
|
try {
|
|
85
36
|
view = presentResult(args, result)
|
|
86
37
|
} catch {
|
|
87
|
-
|
|
88
|
-
return result
|
|
38
|
+
return undefined
|
|
89
39
|
}
|
|
90
|
-
if (!isPlainObjectView(view)) return
|
|
40
|
+
if (!isPlainObjectView(view)) return undefined
|
|
91
41
|
|
|
92
42
|
const bytes = viewJsonBytes(view)
|
|
93
|
-
if (bytes === null) return
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
originalBytes: bytes,
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
const field = dshViewField(view)
|
|
104
|
-
if (isPlainObjectView(result)) {
|
|
105
|
-
return { ...result, [field]: attached }
|
|
106
|
-
}
|
|
107
|
-
// result 非对象:包装,保住视图通道;模型看到包装对象。
|
|
108
|
-
return { value: result, [field]: attached }
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
/**
|
|
112
|
-
* 剥除 providerMessages 里所有 tool 消息 result 上的 `__dshView*` 字段(通道 A 模型不可见面)。
|
|
113
|
-
* 只改 `providerMessages`(provider-only 视图),canonical `messages` 不动 →
|
|
114
|
-
* 持久化/transcript 仍带视图,模型看不到。剥除确定性 + 只动后缀 ⇒ prompt cache
|
|
115
|
-
* 前缀稳定(依据见包 README,已从「cachedTokens 增长」改挂到本机制推理)。
|
|
116
|
-
*/
|
|
117
|
-
function stripDshViewFromMessages(
|
|
118
|
-
messages: ReadonlyArray<ModelMessage>,
|
|
119
|
-
): { messages: Array<ModelMessage>; touched: boolean } {
|
|
120
|
-
let touched = false
|
|
121
|
-
const out: Array<ModelMessage> = messages.map((message) => {
|
|
122
|
-
if (message.role !== 'tool') return message
|
|
123
|
-
const content = message.content
|
|
124
|
-
if (typeof content !== 'string' || !content.includes(DSH_VIEW_FIELD)) {
|
|
125
|
-
return message
|
|
126
|
-
}
|
|
127
|
-
let parsed: unknown
|
|
128
|
-
try {
|
|
129
|
-
parsed = JSON.parse(content)
|
|
130
|
-
} catch {
|
|
131
|
-
return message
|
|
132
|
-
}
|
|
133
|
-
if (typeof parsed !== 'object' || parsed === null) return message
|
|
134
|
-
const record = parsed as Record<string, unknown>
|
|
135
|
-
const fields = viewFieldNames(record)
|
|
136
|
-
if (fields.length === 0) return message
|
|
137
|
-
touched = true
|
|
138
|
-
const rest: Record<string, unknown> = { ...record }
|
|
139
|
-
for (const field of fields) delete rest[field]
|
|
140
|
-
return { ...message, content: JSON.stringify(rest) }
|
|
141
|
-
})
|
|
142
|
-
return { messages: out, touched }
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
/**
|
|
146
|
-
* 通道 A 剥除 middleware。挂在 harness/插件 chat middleware 链上,`onConfig`
|
|
147
|
-
* 只返回 `{ providerMessages: stripped }`——ai 核心仅当 middleware 显式返回
|
|
148
|
-
* `providerMessages` 时才改模型可见面,canonical `messages`(持久化与 transcript
|
|
149
|
-
* 来源)不被触碰。剥除确定性 + 只动工具结果消息尾部 ⇒ 跨回合 prompt cache 前缀稳定。
|
|
150
|
-
*/
|
|
151
|
-
export function stripDshViewMiddleware(): AnyChatMiddleware {
|
|
152
|
-
return {
|
|
153
|
-
name: 'dsh-compat:view-strip',
|
|
154
|
-
onConfig: (_ctx, config) => {
|
|
155
|
-
const source = config.providerMessages ?? config.messages
|
|
156
|
-
const { messages, touched } = stripDshViewFromMessages(source)
|
|
157
|
-
return touched ? { providerMessages: messages } : undefined
|
|
158
|
-
},
|
|
159
|
-
}
|
|
43
|
+
if (bytes === null) return undefined
|
|
44
|
+
const attached: Record<string, unknown> =
|
|
45
|
+
bytes > MAX_VIEW_JSON_BYTES
|
|
46
|
+
? { kind: 'view-truncated', truncated: true, originalBytes: bytes }
|
|
47
|
+
: view
|
|
48
|
+
return { view: attached }
|
|
160
49
|
}
|
package/src/surface.ts
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
// 的键集 + 关键行为。conformance(F1-6)可断言组装产物未越此边界。
|
|
9
9
|
|
|
10
10
|
/** L0 兼容面版本。 */
|
|
11
|
-
export const DSH_COMPAT_SURFACE_VERSION = '0.
|
|
11
|
+
export const DSH_COMPAT_SURFACE_VERSION = '0.2.0' as const
|
|
12
12
|
|
|
13
13
|
/**
|
|
14
14
|
* L0 provided-services 键集:翻译层 `CordisContext` 暴露给 DSH 插件的面
|
|
@@ -39,7 +39,7 @@ export const DSH_COMPAT_SURFACE = {
|
|
|
39
39
|
'effect → 官方 resources.acquire(逆序清理)',
|
|
40
40
|
'已知事件 → 官方 middleware contribution;未知事件 → 包内互通总线',
|
|
41
41
|
'session lifetime(默认):资源随 session open→close 存活',
|
|
42
|
-
'
|
|
42
|
+
'工具视图经 resultMetadata.view 下发(模型不可见)',
|
|
43
43
|
],
|
|
44
44
|
} as const
|
|
45
45
|
|