@allbluecn/dsh-compat 0.0.0-stage → 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/LICENSE +661 -0
- package/README.md +16 -2
- package/package.json +34 -3
- package/src/__tests__/package.test.ts +58 -0
- package/src/assemble/__tests__/assemble.test.ts +656 -0
- package/src/assemble/assemble.ts +383 -0
- package/src/assemble/resolve.ts +59 -0
- package/src/compat/__tests__/context.test.ts +211 -0
- package/src/compat/__tests__/events.test.ts +135 -0
- package/src/compat/__tests__/tool-adapter.test.ts +187 -0
- package/src/compat/__tests__/view-bridge.test.ts +108 -0
- package/src/compat/context.ts +498 -0
- package/src/compat/events.ts +189 -0
- package/src/compat/tool-adapter.ts +123 -0
- package/src/compat/view-bridge.ts +49 -0
- package/src/guard/assert-fetchable-url.ts +239 -0
- package/src/index.ts +52 -0
- package/src/patch/__tests__/patch.test.ts +250 -0
- package/src/patch/eval.ts +343 -0
- package/src/patch/merge.ts +45 -0
- package/src/patch/types.ts +37 -0
- package/src/patch/yaml.ts +75 -0
- package/src/plugin-safety/__tests__/build-approval.test.ts +149 -0
- package/src/plugin-safety/__tests__/install-spec.test.ts +148 -0
- package/src/plugin-safety/build-approval.ts +134 -0
- package/src/plugin-safety/index.ts +31 -0
- package/src/plugin-safety/install-spec.ts +189 -0
- package/src/report.ts +47 -0
- package/src/surface.ts +92 -0
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
2
|
+
import { describe, expect, it } from 'vitest'
|
|
3
|
+
import type { AnyChatMiddleware } from '@tanstack/ai'
|
|
4
|
+
import { CordisEventBus, DSH_EVENT_TO_HOOK, KNOWN_DSH_EVENTS } from '../events'
|
|
5
|
+
|
|
6
|
+
/** 造一个 CordisEventBus + 已 drain 的 middleware 映射。 */
|
|
7
|
+
function busWithHandlers(
|
|
8
|
+
register: (bus: CordisEventBus) => void,
|
|
9
|
+
): { bus: CordisEventBus; middleware: Array<AnyChatMiddleware> } {
|
|
10
|
+
const bus = new CordisEventBus()
|
|
11
|
+
register(bus)
|
|
12
|
+
return { bus, middleware: bus.drainMiddleware() }
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
describe('事件映射表', () => {
|
|
16
|
+
it('已知事件映射到正确 middleware 钩子', () => {
|
|
17
|
+
expect(DSH_EVENT_TO_HOOK['pre-execute']).toBe('onBeforeToolCall')
|
|
18
|
+
expect(DSH_EVENT_TO_HOOK['post-execute']).toBe('onAfterToolCall')
|
|
19
|
+
expect(DSH_EVENT_TO_HOOK.llm).toBe('onChunk')
|
|
20
|
+
expect(DSH_EVENT_TO_HOOK.stream).toBe('onChunk')
|
|
21
|
+
expect(DSH_EVENT_TO_HOOK['turn-stopping']).toBe('onShouldContinue')
|
|
22
|
+
expect(KNOWN_DSH_EVENTS.has('pre-execute')).toBe(true)
|
|
23
|
+
expect(KNOWN_DSH_EVENTS.has('unknown-thing')).toBe(false)
|
|
24
|
+
})
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
describe('已知事件 → middleware 钩子', () => {
|
|
28
|
+
it('pre-execute → onBeforeToolCall;skip 决策翻译成 { type: skip, result }', () => {
|
|
29
|
+
const { middleware } = busWithHandlers((bus) => {
|
|
30
|
+
bus.on('pre-execute', (hookCtx) => {
|
|
31
|
+
const name = (hookCtx as { toolName: string }).toolName
|
|
32
|
+
return name === 'danger' ? { type: 'skip', result: { blocked: true } } : undefined
|
|
33
|
+
})
|
|
34
|
+
})
|
|
35
|
+
const mw = middleware[0]
|
|
36
|
+
expect(mw.onBeforeToolCall).toBeTypeOf('function')
|
|
37
|
+
const decision = mw.onBeforeToolCall?.({} as never, {
|
|
38
|
+
toolName: 'danger',
|
|
39
|
+
args: {},
|
|
40
|
+
} as never)
|
|
41
|
+
expect(decision).toEqual({ type: 'skip', result: { blocked: true } })
|
|
42
|
+
// 非 danger 工具:undefined(放行)
|
|
43
|
+
const pass = mw.onBeforeToolCall?.({} as never, { toolName: 'safe' } as never)
|
|
44
|
+
expect(pass).toBeUndefined()
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
it('post-execute → onAfterToolCall;replaceResult 语义', () => {
|
|
48
|
+
const { middleware } = busWithHandlers((bus) => {
|
|
49
|
+
bus.on('post-execute', () => ({ type: 'replaceResult', result: { patched: 1 } }))
|
|
50
|
+
})
|
|
51
|
+
const mw = middleware[0]
|
|
52
|
+
const decision = mw.onAfterToolCall?.({} as never, {
|
|
53
|
+
toolName: 't',
|
|
54
|
+
result: { original: 0 },
|
|
55
|
+
} as never)
|
|
56
|
+
expect(decision).toEqual({ type: 'replaceResult', result: { patched: 1 } })
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
it('llm / stream → onChunk(每个 chunk 派发)', () => {
|
|
60
|
+
const seenLlm: Array<unknown> = []
|
|
61
|
+
const seenStream: Array<unknown> = []
|
|
62
|
+
const { middleware } = busWithHandlers((bus) => {
|
|
63
|
+
bus.on('llm', (chunk) => {
|
|
64
|
+
seenLlm.push(chunk)
|
|
65
|
+
})
|
|
66
|
+
bus.on('stream', (chunk) => {
|
|
67
|
+
seenStream.push(chunk)
|
|
68
|
+
})
|
|
69
|
+
})
|
|
70
|
+
const chunk = { type: 'TEXT_MESSAGE_CONTENT', delta: 'hi' }
|
|
71
|
+
for (const mw of middleware) mw.onChunk?.({} as never, chunk as never)
|
|
72
|
+
expect(seenLlm).toContain(chunk)
|
|
73
|
+
expect(seenStream).toContain(chunk)
|
|
74
|
+
// onChunk 返回 undefined → chunk 原样透传(不改写流)
|
|
75
|
+
expect(middleware.every((mw) => mw.onChunk?.({} as never, chunk as never) === undefined)).toBe(true)
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
it('turn-stopping → onShouldContinue;stop 语义返回 false 终止 loop', () => {
|
|
79
|
+
const { middleware } = busWithHandlers((bus) => {
|
|
80
|
+
bus.on('turn-stopping', () => 'stop')
|
|
81
|
+
})
|
|
82
|
+
const mw = middleware[0]
|
|
83
|
+
expect(mw.onShouldContinue?.({} as never, {} as never)).toBe(false)
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
it('turn-stopping:非 stop 返回值不终止(undefined)', () => {
|
|
87
|
+
const { middleware } = busWithHandlers((bus) => {
|
|
88
|
+
bus.on('turn-stopping', () => 'continue')
|
|
89
|
+
})
|
|
90
|
+
expect(middleware[0].onShouldContinue?.({} as never, {} as never)).toBeUndefined()
|
|
91
|
+
})
|
|
92
|
+
|
|
93
|
+
it('无已知事件处理器 → drainMiddleware 不产出空 middleware', () => {
|
|
94
|
+
const bus = new CordisEventBus()
|
|
95
|
+
bus.on('unknown-thing', () => {})
|
|
96
|
+
expect(bus.drainMiddleware()).toEqual([])
|
|
97
|
+
})
|
|
98
|
+
})
|
|
99
|
+
|
|
100
|
+
describe('未知事件 → 包内互通总线', () => {
|
|
101
|
+
it('emit 派发给所有 on 订阅者', () => {
|
|
102
|
+
const bus = new CordisEventBus()
|
|
103
|
+
const got: Array<unknown> = []
|
|
104
|
+
bus.on('dsh:custom', (v) => got.push(v))
|
|
105
|
+
bus.on('dsh:custom', (v) => got.push(`second:${String(v)}`))
|
|
106
|
+
bus.emit('dsh:custom', 42)
|
|
107
|
+
expect(got).toEqual([42, 'second:42'])
|
|
108
|
+
})
|
|
109
|
+
|
|
110
|
+
it('off 注销后不再接收', () => {
|
|
111
|
+
const bus = new CordisEventBus()
|
|
112
|
+
const got: Array<unknown> = []
|
|
113
|
+
const off = bus.on('dsh:custom', (v) => got.push(v))
|
|
114
|
+
off()
|
|
115
|
+
bus.emit('dsh:custom', 1)
|
|
116
|
+
expect(got).toEqual([])
|
|
117
|
+
})
|
|
118
|
+
|
|
119
|
+
it('waterfall 依次过订阅者,每层返回值作为下一层入参', () => {
|
|
120
|
+
const bus = new CordisEventBus()
|
|
121
|
+
bus.on('pipe', (v) => (typeof v === 'number' ? v + 1 : v))
|
|
122
|
+
bus.on('pipe', (v) => (typeof v === 'number' ? v * 2 : v))
|
|
123
|
+
expect(bus.waterfall('pipe', 3)).toBe(8) // (3+1)*2
|
|
124
|
+
// 无订阅者:原值返回
|
|
125
|
+
expect(bus.waterfall('nobody', 'x')).toBe('x')
|
|
126
|
+
})
|
|
127
|
+
|
|
128
|
+
it('已知事件也可经包内总线在插件间互通(ctx.on 订阅)', () => {
|
|
129
|
+
const bus = new CordisEventBus()
|
|
130
|
+
const got: Array<unknown> = []
|
|
131
|
+
bus.on('pre-execute', (v) => got.push(v))
|
|
132
|
+
bus.emit('pre-execute', 'direct')
|
|
133
|
+
expect(got).toEqual(['direct'])
|
|
134
|
+
})
|
|
135
|
+
})
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
2
|
+
import { describe, expect, it } from 'vitest'
|
|
3
|
+
import type { CordisToolDef } from '../context'
|
|
4
|
+
import {
|
|
5
|
+
adapterNotes,
|
|
6
|
+
dshToolToTanStack,
|
|
7
|
+
sanitizeJsonSchema,
|
|
8
|
+
} from '../tool-adapter'
|
|
9
|
+
|
|
10
|
+
describe('sanitizeJsonSchema(provider 不认关键字净化)', () => {
|
|
11
|
+
it('剥离 draft-04 exclusiveMinimum/Maximum 与冷门关键字,保留通用关键字', () => {
|
|
12
|
+
const schema = {
|
|
13
|
+
type: 'object',
|
|
14
|
+
properties: {
|
|
15
|
+
age: { type: 'number', minimum: 0, exclusiveMinimum: 0 },
|
|
16
|
+
score: { type: 'number', maximum: 100, exclusiveMaximum: 100 },
|
|
17
|
+
tag: { type: 'string', contains: 'x' },
|
|
18
|
+
},
|
|
19
|
+
required: ['age'],
|
|
20
|
+
$schema: 'http://json-schema.org/draft-04/schema#',
|
|
21
|
+
}
|
|
22
|
+
const out = sanitizeJsonSchema(schema) as Record<string, any>
|
|
23
|
+
expect(out.$schema).toBeUndefined()
|
|
24
|
+
expect(out.type).toBe('object')
|
|
25
|
+
expect(out.required).toEqual(['age'])
|
|
26
|
+
expect(out.properties.age).toEqual({ type: 'number', minimum: 0 })
|
|
27
|
+
expect(out.properties.score).toEqual({ type: 'number', maximum: 100 })
|
|
28
|
+
expect(out.properties.tag).toEqual({ type: 'string' })
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
it('递归净化嵌套 items / 不改动入参', () => {
|
|
32
|
+
const schema = {
|
|
33
|
+
type: 'array',
|
|
34
|
+
items: { type: 'number', exclusiveMinimum: 5, description: 'd' },
|
|
35
|
+
}
|
|
36
|
+
const out = sanitizeJsonSchema(schema) as Record<string, any>
|
|
37
|
+
expect(out.items).toEqual({ type: 'number', description: 'd' })
|
|
38
|
+
// 入参未被改动
|
|
39
|
+
expect((schema.items as any).exclusiveMinimum).toBe(5)
|
|
40
|
+
})
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
function baseDef(over: Partial<CordisToolDef> = {}): CordisToolDef {
|
|
44
|
+
return {
|
|
45
|
+
name: 'dsh_deploy',
|
|
46
|
+
description: 'Deploy and render a card.',
|
|
47
|
+
parameters: {
|
|
48
|
+
type: 'object',
|
|
49
|
+
properties: { region: { type: 'string', exclusiveMinimum: 0 } },
|
|
50
|
+
required: ['region'],
|
|
51
|
+
},
|
|
52
|
+
execute: () => ({ ok: true }),
|
|
53
|
+
...over,
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
describe('dshToolToTanStack', () => {
|
|
58
|
+
it('parameters → inputSchema 直传(净化后),description 透传', () => {
|
|
59
|
+
const tool = dshToolToTanStack(baseDef())
|
|
60
|
+
expect(tool.name).toBe('dsh_deploy')
|
|
61
|
+
expect(tool.description).toBe('Deploy and render a card.')
|
|
62
|
+
const schema = tool.inputSchema as Record<string, any>
|
|
63
|
+
expect(schema.type).toBe('object')
|
|
64
|
+
expect(schema.required).toEqual(['region'])
|
|
65
|
+
// 净化生效:exclusiveMinimum 被剥掉
|
|
66
|
+
expect(schema.properties.region).toEqual({ type: 'string' })
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
it('output.schema → outputSchema', () => {
|
|
70
|
+
const tool = dshToolToTanStack(
|
|
71
|
+
baseDef({ output: { schema: { type: 'object' } } }),
|
|
72
|
+
)
|
|
73
|
+
expect(tool.outputSchema).toEqual({ type: 'object' })
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
it('deferLoading → lazy: true;否则无 lazy', () => {
|
|
77
|
+
expect(dshToolToTanStack(baseDef({ deferLoading: true })).lazy).toBe(true)
|
|
78
|
+
expect(dshToolToTanStack(baseDef()).lazy).toBeUndefined()
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
it('execute 适配上下文:abortSignal/runId → CordisToolRunContext', async () => {
|
|
82
|
+
let seen: unknown
|
|
83
|
+
const controller = new AbortController()
|
|
84
|
+
const setResultMetadata = (_value: Record<string, unknown>) => {}
|
|
85
|
+
const def = baseDef({
|
|
86
|
+
execute: (args, ctx) => {
|
|
87
|
+
seen = ctx
|
|
88
|
+
return { ok: true, region: (args as { region: string }).region }
|
|
89
|
+
},
|
|
90
|
+
})
|
|
91
|
+
const tool = dshToolToTanStack(def)
|
|
92
|
+
const result = (await tool.execute?.(
|
|
93
|
+
{ region: 'cn-north-1' },
|
|
94
|
+
{
|
|
95
|
+
abortSignal: controller.signal,
|
|
96
|
+
runId: 'run-9',
|
|
97
|
+
emitCustomEvent: () => {},
|
|
98
|
+
setResultMetadata,
|
|
99
|
+
},
|
|
100
|
+
)) as Record<string, unknown>
|
|
101
|
+
expect(result.ok).toBe(true)
|
|
102
|
+
expect(seen).toEqual({
|
|
103
|
+
signal: controller.signal,
|
|
104
|
+
caller: 'run-9',
|
|
105
|
+
setResultMetadata,
|
|
106
|
+
})
|
|
107
|
+
})
|
|
108
|
+
|
|
109
|
+
it('presenter 视图经 setResultMetadata 挂到 resultMetadata.view,result 原样返回', async () => {
|
|
110
|
+
const view = { kind: 'card', title: 't', rows: [] }
|
|
111
|
+
const raw = { ok: true }
|
|
112
|
+
const def = baseDef({
|
|
113
|
+
execute: () => raw,
|
|
114
|
+
presentResult: () => view,
|
|
115
|
+
})
|
|
116
|
+
const tool = dshToolToTanStack(def)
|
|
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 })
|
|
129
|
+
})
|
|
130
|
+
|
|
131
|
+
it('presenter 抛错 → 不挂 metadata,result 原样返回', async () => {
|
|
132
|
+
const raw = { ok: true }
|
|
133
|
+
const def = baseDef({
|
|
134
|
+
execute: () => raw,
|
|
135
|
+
presentResult: () => {
|
|
136
|
+
throw new Error('presenter boom')
|
|
137
|
+
},
|
|
138
|
+
})
|
|
139
|
+
const tool = dshToolToTanStack(def)
|
|
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({})
|
|
152
|
+
})
|
|
153
|
+
|
|
154
|
+
it('timeoutMs 超时 → execute reject', async () => {
|
|
155
|
+
const def = baseDef({
|
|
156
|
+
timeoutMs: 20,
|
|
157
|
+
execute: () =>
|
|
158
|
+
new Promise((_resolve) => setTimeout(() => _resolve({ ok: 1 }), 5000)),
|
|
159
|
+
})
|
|
160
|
+
const tool = dshToolToTanStack(def)
|
|
161
|
+
await expect(tool.execute?.({ region: 'x' })).rejects.toThrow(/timed out/)
|
|
162
|
+
})
|
|
163
|
+
|
|
164
|
+
it('DSH execute 抛错 → 官方 execute reject(工具失败,模型收到 error)', async () => {
|
|
165
|
+
const def = baseDef({
|
|
166
|
+
execute: () => {
|
|
167
|
+
throw new Error('dsh boom')
|
|
168
|
+
},
|
|
169
|
+
})
|
|
170
|
+
const tool = dshToolToTanStack(def)
|
|
171
|
+
await expect(tool.execute?.({ region: 'x' })).rejects.toThrow('dsh boom')
|
|
172
|
+
})
|
|
173
|
+
|
|
174
|
+
it('adapterNotes 记录官方无对应字段', () => {
|
|
175
|
+
const notes = adapterNotes(
|
|
176
|
+
baseDef({
|
|
177
|
+
isConcurrencySafe: true,
|
|
178
|
+
projectContent: {},
|
|
179
|
+
presentCall: () => ({}),
|
|
180
|
+
}),
|
|
181
|
+
)
|
|
182
|
+
expect(notes).toContain('isConcurrencySafe')
|
|
183
|
+
expect(notes).toContain('projectContent')
|
|
184
|
+
expect(notes.some((n) => n.startsWith('presentCall'))).toBe(true)
|
|
185
|
+
expect(adapterNotes(baseDef())).toEqual([])
|
|
186
|
+
})
|
|
187
|
+
})
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
2
|
+
import { describe, expect, it } from 'vitest'
|
|
3
|
+
import type { ToolExecutionContext } from '@tanstack/ai'
|
|
4
|
+
import { dshToolToTanStack } from '../tool-adapter'
|
|
5
|
+
|
|
6
|
+
const VIEW = { kind: 'card', title: '部署完成', rows: [{ label: 'r', value: 'v' }] }
|
|
7
|
+
|
|
8
|
+
function metadataContext(captured: Record<string, unknown>): ToolExecutionContext {
|
|
9
|
+
return {
|
|
10
|
+
emitCustomEvent: () => {},
|
|
11
|
+
setResultMetadata: (value) => Object.assign(captured, value),
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
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 })
|
|
35
|
+
})
|
|
36
|
+
|
|
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> = {}
|
|
46
|
+
|
|
47
|
+
const result = await tool.execute!({}, metadataContext(captured))
|
|
48
|
+
|
|
49
|
+
expect(result).toBe(raw)
|
|
50
|
+
expect(captured).toEqual({ view: VIEW })
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
it('presenter 缺省、抛错或产出非法视图时不挂 metadata,result 原样返回', async () => {
|
|
54
|
+
const circular: Record<string, unknown> = { kind: 'card' }
|
|
55
|
+
circular.self = circular
|
|
56
|
+
const presenters = [
|
|
57
|
+
undefined,
|
|
58
|
+
() => {
|
|
59
|
+
throw new Error('presenter boom')
|
|
60
|
+
},
|
|
61
|
+
() => 'not-an-object',
|
|
62
|
+
() => [1, 2],
|
|
63
|
+
() => circular,
|
|
64
|
+
]
|
|
65
|
+
|
|
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> = {}
|
|
75
|
+
|
|
76
|
+
const result = await tool.execute!({}, metadataContext(captured))
|
|
77
|
+
|
|
78
|
+
expect(result).toBe(raw)
|
|
79
|
+
expect(captured).toEqual({})
|
|
80
|
+
}
|
|
81
|
+
})
|
|
82
|
+
|
|
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
|
+
)
|
|
107
|
+
})
|
|
108
|
+
})
|