@shuind/dsh-codex-harness 0.2.21 → 0.3.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/CHANGELOG.md +185 -0
- package/README.md +38 -50
- package/RELEASE.md +61 -0
- package/assets/codex.svg +5 -0
- package/cordis.patch.yml +181 -6
- package/docs/preset-library.md +33 -23
- package/docs/tool-policy-benchmark.json +310 -0
- package/docs/tool-policy.md +148 -0
- package/lib/client.js +4774 -2416
- package/lib/compact-command.js +416 -0
- package/lib/compaction.js +141 -12
- package/lib/hybrid-executor.js +1196 -0
- package/lib/index.js +3658 -2334
- package/lib/preferences.js +117 -0
- package/lib/tool-policy.js +2422 -0
- package/lib/typert.js +86 -79
- package/lib/types/client/activity.d.ts +33 -9
- package/lib/types/client/activity.js +4 -3
- package/lib/types/client/desktop.d.ts +67 -0
- package/lib/types/client/desktop.js +229 -0
- package/lib/types/client/index.d.ts +207 -110
- package/lib/types/client/index.js +99 -492
- package/lib/types/client/policy-drafts.d.ts +25 -0
- package/lib/types/client/policy-drafts.js +47 -0
- package/lib/types/client/policy-locale.d.ts +260 -0
- package/lib/types/client/policy-locale.js +357 -0
- package/lib/types/client/preset-manager.d.ts +38 -10
- package/lib/types/client/preset-manager.js +235 -185
- package/lib/types/client/preset-requests.d.ts +12 -0
- package/lib/types/client/preset-requests.js +101 -0
- package/lib/types/client/tool-policy.d.ts +33 -0
- package/lib/types/client/tool-policy.js +336 -0
- package/lib/types/contracts/preset-editor-schema.d.ts +78 -0
- package/lib/types/contracts/preset-editor-schema.js +95 -0
- package/lib/types/{prompt.js → contracts/prompt.js} +13 -7
- package/lib/types/{settings.d.ts → contracts/settings.d.ts} +11 -4
- package/lib/types/{settings.js → contracts/settings.js} +22 -6
- package/lib/types/contracts/tool-policy-contract.d.ts +63 -0
- package/lib/types/contracts/tool-policy-contract.js +33 -0
- package/lib/types/contracts/ui-contracts.d.ts +92 -0
- package/lib/types/contracts/ui-contracts.js +83 -0
- package/lib/types/entries/client.d.ts +10 -0
- package/lib/types/entries/client.js +10 -0
- package/lib/types/{compact-command.d.ts → entries/compact-command.d.ts} +2 -0
- package/lib/types/entries/compact-command.js +176 -0
- package/lib/types/{compaction.d.ts → entries/compaction.d.ts} +15 -3
- package/lib/types/{compaction.js → entries/compaction.js} +47 -11
- package/lib/types/entries/hybrid-executor.d.ts +19 -0
- package/lib/types/entries/hybrid-executor.js +57 -0
- package/lib/types/entries/index.d.ts +66 -0
- package/lib/types/entries/index.js +192 -0
- package/lib/types/entries/preferences.d.ts +11 -0
- package/lib/types/entries/preferences.js +16 -0
- package/lib/types/entries/tool-policy.d.ts +97 -0
- package/lib/types/entries/tool-policy.js +158 -0
- package/lib/types/entries/typert.host.d.ts +23 -0
- package/lib/types/entries/typert.host.js +24 -0
- package/lib/types/hybrid/codex-hybrid-executor.d.ts +44 -0
- package/lib/types/hybrid/codex-hybrid-executor.js +257 -0
- package/lib/types/hybrid/hybrid-sdk.d.ts +13 -0
- package/lib/types/hybrid/hybrid-sdk.js +196 -0
- package/lib/types/hybrid/program-calls.d.ts +28 -0
- package/lib/types/hybrid/program-calls.js +193 -0
- package/lib/types/hybrid/subcall-scheduler.d.ts +90 -0
- package/lib/types/hybrid/subcall-scheduler.js +343 -0
- package/lib/types/policy/deferred-tools.d.ts +42 -0
- package/lib/types/policy/deferred-tools.js +264 -0
- package/lib/types/policy/overlay-installer.d.ts +12 -0
- package/lib/types/policy/overlay-installer.js +179 -0
- package/lib/types/policy/rule-resolver.d.ts +94 -0
- package/lib/types/policy/rule-resolver.js +114 -0
- package/lib/types/policy/tool-capabilities.d.ts +33 -0
- package/lib/types/policy/tool-capabilities.js +46 -0
- package/lib/types/{activity.d.ts → presets/activity.d.ts} +3 -5
- package/lib/types/{activity.js → presets/activity.js} +51 -27
- package/lib/types/presets/desktop-preset.d.ts +22 -0
- package/lib/types/presets/desktop-preset.js +43 -0
- package/lib/types/presets/preset-editor.d.ts +47 -0
- package/lib/types/presets/preset-editor.js +81 -0
- package/lib/types/presets/preset-prompt-catalog.d.ts +23 -0
- package/lib/types/presets/preset-prompt-catalog.js +215 -0
- package/lib/types/presets/preset-tool-catalog.d.ts +5 -0
- package/lib/types/presets/preset-tool-catalog.js +58 -0
- package/lib/types/presets/runtime-enhancements.d.ts +45 -0
- package/lib/types/presets/runtime-enhancements.js +349 -0
- package/lib/types/presets/small-window-compaction.d.ts +15 -0
- package/lib/types/presets/small-window-compaction.js +121 -0
- package/lib/types/responses/compact-checkpoint.d.ts +27 -0
- package/lib/types/responses/compact-checkpoint.js +257 -0
- package/lib/types/responses/fetch-transport.d.ts +16 -0
- package/lib/types/responses/fetch-transport.js +257 -0
- package/lib/types/responses/remote-compact.d.ts +13 -0
- package/lib/types/responses/remote-compact.js +319 -0
- package/lib/types/responses/sse-diagnostics.d.ts +9 -0
- package/lib/types/responses/sse-diagnostics.js +157 -0
- package/lib/types/responses/wire-rewrite.d.ts +35 -0
- package/lib/types/responses/wire-rewrite.js +237 -0
- package/lib/types/tools/exec-output.d.ts +58 -0
- package/lib/types/tools/exec-output.js +173 -0
- package/lib/types/tools/exec-plan-tools.d.ts +37 -0
- package/lib/types/tools/exec-plan-tools.js +265 -0
- package/lib/types/tools/exec-runner.d.ts +21 -0
- package/lib/types/tools/exec-runner.js +385 -0
- package/lib/types/tools/patch-tool.d.ts +16 -0
- package/lib/types/tools/patch-tool.js +258 -0
- package/lib/types/{patch.d.ts → tools/patch.d.ts} +1 -1
- package/lib/types/{patch.js → tools/patch.js} +96 -21
- package/locale/en.json +6 -0
- package/locale/hybrid-executor/en.json +6 -0
- package/locale/hybrid-executor/zh.json +6 -0
- package/locale/tool-policy/en.json +6 -0
- package/locale/tool-policy/zh.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +297 -299
- package/presets/{codex-collaboration → codex-hybrid}/agent.cordis.yml +45 -48
- package/presets/codex-hybrid/preset.yml +3 -0
- package/scripts/benchmark-tool-policy.mjs +186 -0
- package/scripts/build-preset.mjs +19 -0
- package/scripts/clean-build.mjs +11 -0
- package/src/client/activity.ts +83 -0
- package/src/client/desktop.tsx +276 -0
- package/src/client/index.tsx +391 -0
- package/src/client/policy-drafts.ts +56 -0
- package/src/client/policy-locale.ts +362 -0
- package/src/client/preset-manager.tsx +376 -0
- package/src/client/preset-requests.ts +113 -0
- package/src/client/tool-policy.tsx +402 -0
- package/src/contracts/preset-editor-schema.ts +110 -0
- package/src/contracts/prompt.ts +66 -0
- package/src/contracts/settings.ts +103 -0
- package/src/contracts/tool-policy-contract.ts +93 -0
- package/src/contracts/ui-contracts.ts +173 -0
- package/src/entries/client.tsx +14 -0
- package/src/entries/compact-command.ts +256 -0
- package/src/entries/compaction.ts +218 -0
- package/src/entries/hybrid-executor.ts +76 -0
- package/src/entries/index.ts +298 -0
- package/src/entries/preferences.ts +17 -0
- package/src/entries/tool-policy.ts +162 -0
- package/src/entries/typert.host.ts +29 -0
- package/src/hybrid/codex-hybrid-executor.ts +310 -0
- package/src/hybrid/hybrid-sdk.ts +217 -0
- package/src/hybrid/program-calls.ts +202 -0
- package/src/hybrid/subcall-scheduler.ts +446 -0
- package/src/policy/deferred-tools.ts +331 -0
- package/src/policy/overlay-installer.ts +191 -0
- package/src/policy/rule-resolver.ts +128 -0
- package/src/policy/tool-capabilities.ts +88 -0
- package/src/presets/activity.ts +146 -0
- package/src/presets/desktop-preset.ts +60 -0
- package/src/presets/preset-editor.ts +101 -0
- package/src/presets/preset-prompt-catalog.ts +221 -0
- package/src/presets/preset-tool-catalog.ts +58 -0
- package/src/presets/runtime-enhancements.ts +415 -0
- package/src/presets/small-window-compaction.ts +166 -0
- package/src/responses/compact-checkpoint.ts +264 -0
- package/src/responses/fetch-transport.ts +290 -0
- package/src/responses/remote-compact.ts +393 -0
- package/src/responses/sse-diagnostics.ts +156 -0
- package/src/responses/wire-rewrite.ts +261 -0
- package/src/tools/exec-output.ts +211 -0
- package/src/tools/exec-plan-tools.ts +307 -0
- package/src/tools/exec-runner.ts +445 -0
- package/src/tools/patch-tool.ts +331 -0
- package/src/tools/patch.ts +328 -0
- package/tsconfig.json +29 -0
- package/tsdown.config.ts +110 -0
- package/lib/installer.js +0 -237
- package/lib/invariant.js +0 -23
- package/lib/types/activity-types.d.ts +0 -6
- package/lib/types/activity-types.js +0 -3
- package/lib/types/client/preset-copy.d.ts +0 -42
- package/lib/types/client/preset-copy.js +0 -81
- package/lib/types/compact-command.js +0 -79
- package/lib/types/context.d.ts +0 -8
- package/lib/types/context.js +0 -14
- package/lib/types/exec.d.ts +0 -46
- package/lib/types/exec.js +0 -365
- package/lib/types/index.d.ts +0 -95
- package/lib/types/index.js +0 -738
- package/lib/types/installer.d.ts +0 -27
- package/lib/types/installer.js +0 -146
- package/lib/types/invariant.d.ts +0 -16
- package/lib/types/invariant.js +0 -22
- package/lib/types/preset-contract.d.ts +0 -69
- package/lib/types/preset-contract.js +0 -22
- package/lib/types/preset-editor-remote.d.ts +0 -30
- package/lib/types/preset-editor-remote.js +0 -37
- package/lib/types/preset-editor.d.ts +0 -69
- package/lib/types/preset-editor.js +0 -557
- package/lib/types/preset-templates.d.ts +0 -36
- package/lib/types/preset-templates.js +0 -12
- package/lib/types/prompt-sections.d.ts +0 -15
- package/lib/types/prompt-sections.js +0 -23
- package/lib/types/remote.d.ts +0 -40
- package/lib/types/remote.js +0 -683
- package/lib/types/typert.host.d.ts +0 -47
- package/lib/types/typert.host.js +0 -99
- package/presets/codex-collaboration/preset.yml +0 -3
- /package/lib/types/{prompt.d.ts → contracts/prompt.d.ts} +0 -0
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
import { assertSupportedJsonSchema } from '@deepseek-ai/dsh-tools';
|
|
2
|
+
const IDENTIFIER = /^[A-Za-z_$][A-Za-z0-9_$]*$/;
|
|
3
|
+
function renderKey(name) {
|
|
4
|
+
return IDENTIFIER.test(name) ? name : JSON.stringify(name);
|
|
5
|
+
}
|
|
6
|
+
function pad(indent) {
|
|
7
|
+
return ' '.repeat(indent);
|
|
8
|
+
}
|
|
9
|
+
function docLines(description, indent) {
|
|
10
|
+
if (typeof description !== 'string' || description.length === 0)
|
|
11
|
+
return [];
|
|
12
|
+
const collapsed = description.replace(/\s+/gu, ' ').trim();
|
|
13
|
+
if (collapsed.length === 0)
|
|
14
|
+
return [];
|
|
15
|
+
return [`${pad(indent)}/** ${collapsed.replaceAll('*/', String.raw `*\/`)} */`];
|
|
16
|
+
}
|
|
17
|
+
function renderScalar(node, type) {
|
|
18
|
+
const broad = type === 'integer' ? 'number' : type;
|
|
19
|
+
if (Object.hasOwn(node, 'const'))
|
|
20
|
+
return JSON.stringify(node.const);
|
|
21
|
+
if (Object.hasOwn(node, 'enum')) {
|
|
22
|
+
return node.enum.map(value => JSON.stringify(value)).join(' | ');
|
|
23
|
+
}
|
|
24
|
+
return broad;
|
|
25
|
+
}
|
|
26
|
+
function containsUnion(text) {
|
|
27
|
+
return text.includes(' | ');
|
|
28
|
+
}
|
|
29
|
+
function renderObjectNode(node, indent) {
|
|
30
|
+
const properties = node.properties ?? {};
|
|
31
|
+
const required = new Set(node.required ?? []);
|
|
32
|
+
const keys = Object.keys(properties).sort((a, b) => (a < b ? -1 : a > b ? 1 : 0));
|
|
33
|
+
const lines = [];
|
|
34
|
+
for (const key of keys) {
|
|
35
|
+
const child = properties[key];
|
|
36
|
+
if (child === undefined)
|
|
37
|
+
continue;
|
|
38
|
+
lines.push(...docLines(child.description, indent + 1));
|
|
39
|
+
const optional = required.has(key) ? '' : '?';
|
|
40
|
+
lines.push(`${pad(indent + 1)}${renderKey(key)}${optional}: ${renderNode(child, indent + 1)};`);
|
|
41
|
+
}
|
|
42
|
+
if (node.additionalProperties !== false) {
|
|
43
|
+
lines.push(`${pad(indent + 1)}[key: string]: JsonValue;`);
|
|
44
|
+
}
|
|
45
|
+
if (lines.length === 0)
|
|
46
|
+
return '{}';
|
|
47
|
+
return `{\n${lines.join('\n')}\n${pad(indent)}}`;
|
|
48
|
+
}
|
|
49
|
+
function renderNode(node, indent) {
|
|
50
|
+
if (Array.isArray(node.oneOf) && node.oneOf.length > 0) {
|
|
51
|
+
return node.oneOf.map(branch => renderNode(branch, indent)).join(' | ');
|
|
52
|
+
}
|
|
53
|
+
switch (node.type) {
|
|
54
|
+
case undefined:
|
|
55
|
+
return 'JsonValue';
|
|
56
|
+
case 'object':
|
|
57
|
+
return renderObjectNode(node, indent);
|
|
58
|
+
case 'array': {
|
|
59
|
+
const itemText = node.items === undefined ? 'JsonValue' : renderNode(node.items, indent);
|
|
60
|
+
return containsUnion(itemText) ? `(${itemText})[]` : `${itemText}[]`;
|
|
61
|
+
}
|
|
62
|
+
case 'string':
|
|
63
|
+
case 'number':
|
|
64
|
+
case 'integer':
|
|
65
|
+
case 'boolean':
|
|
66
|
+
case 'null':
|
|
67
|
+
return renderScalar(node, node.type);
|
|
68
|
+
default:
|
|
69
|
+
return 'unknown';
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
/** Render one supported JSON Schema node to TypeScript, degrading invalid schemas safely. */
|
|
73
|
+
export function jsonSchemaToTs(schema, indent = 0) {
|
|
74
|
+
try {
|
|
75
|
+
assertSupportedJsonSchema(schema);
|
|
76
|
+
return renderNode(schema, indent);
|
|
77
|
+
}
|
|
78
|
+
catch {
|
|
79
|
+
return 'unknown';
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
function renderPythonAnnotation(node) {
|
|
83
|
+
if (Array.isArray(node.oneOf) && node.oneOf.length > 0) {
|
|
84
|
+
return node.oneOf.map(renderPythonAnnotation).join(' | ');
|
|
85
|
+
}
|
|
86
|
+
switch (node.type) {
|
|
87
|
+
case 'object':
|
|
88
|
+
return 'dict[str, Any]';
|
|
89
|
+
case 'array':
|
|
90
|
+
return node.items === undefined ? 'list[Any]' : `list[${renderPythonAnnotation(node.items)}]`;
|
|
91
|
+
case 'string':
|
|
92
|
+
return 'str';
|
|
93
|
+
case 'integer':
|
|
94
|
+
return 'int';
|
|
95
|
+
case 'number':
|
|
96
|
+
return 'float';
|
|
97
|
+
case 'boolean':
|
|
98
|
+
return 'bool';
|
|
99
|
+
case 'null':
|
|
100
|
+
return 'None';
|
|
101
|
+
default:
|
|
102
|
+
return 'Any';
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
function safePyIdentifier(name) {
|
|
106
|
+
return /^[A-Za-z_][A-Za-z0-9_]*$/u.test(name) ? name : `tool_${name.replace(/[^A-Za-z0-9_]+/gu, '_')}`;
|
|
107
|
+
}
|
|
108
|
+
function renderPythonSdk(sorted, programFormat) {
|
|
109
|
+
const methods = sorted.map((schema) => {
|
|
110
|
+
let returnType = 'Any';
|
|
111
|
+
try {
|
|
112
|
+
assertSupportedJsonSchema(schema.output);
|
|
113
|
+
returnType = renderPythonAnnotation(schema.output);
|
|
114
|
+
}
|
|
115
|
+
catch {
|
|
116
|
+
returnType = 'Any';
|
|
117
|
+
}
|
|
118
|
+
const doc = schema.description.replace(/\s+/gu, ' ').trim().replaceAll('"""', '\\"\\"\\"');
|
|
119
|
+
return [
|
|
120
|
+
` async def ${safePyIdentifier(schema.name)}(self, args: dict[str, Any]) -> ${returnType}:`,
|
|
121
|
+
...(doc ? [` """${doc}"""`] : []),
|
|
122
|
+
' ...',
|
|
123
|
+
].join('\n');
|
|
124
|
+
});
|
|
125
|
+
const outputRule = programFormat === 'function-body'
|
|
126
|
+
? 'Print results cleanly with `print(...)` or `return`.'
|
|
127
|
+
: 'Emit only concise summaries for the conversation.';
|
|
128
|
+
return [
|
|
129
|
+
'## Hybrid SDK bindings (`run_code`)',
|
|
130
|
+
'',
|
|
131
|
+
'Invoke the SDK tools below only inside `run_code` via `await tools.<name>(args)`. Direct wire tools must still be called directly, not through `tools`.',
|
|
132
|
+
`Batch independent read/search calls with \`asyncio.gather\`. ${outputRule}`,
|
|
133
|
+
'',
|
|
134
|
+
'```python',
|
|
135
|
+
'from typing import Any',
|
|
136
|
+
'',
|
|
137
|
+
'class ToolCallError(Exception):',
|
|
138
|
+
' toolName: str',
|
|
139
|
+
'',
|
|
140
|
+
'class _ToolsSdk:',
|
|
141
|
+
...methods,
|
|
142
|
+
'',
|
|
143
|
+
'tools: _ToolsSdk',
|
|
144
|
+
'```',
|
|
145
|
+
].join('\n');
|
|
146
|
+
}
|
|
147
|
+
/** Render deterministic SDK declarations for hybrid `run_code` bindings. */
|
|
148
|
+
export function renderHybridToolsSdk(schemas, language = 'typescript', programFormat = 'function-body') {
|
|
149
|
+
if (schemas.length === 0) {
|
|
150
|
+
return [
|
|
151
|
+
'## Hybrid SDK bindings (`run_code`)',
|
|
152
|
+
'',
|
|
153
|
+
'No SDK tools are currently active in this step. Direct tools remain callable directly without `run_code`. If you need a deferred tool, call `activate_tools` first and invoke it on the next step.',
|
|
154
|
+
].join('\n');
|
|
155
|
+
}
|
|
156
|
+
const sorted = [...schemas].sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0));
|
|
157
|
+
if (language === 'python')
|
|
158
|
+
return renderPythonSdk(sorted, programFormat);
|
|
159
|
+
const programDescription = programFormat === 'function-body'
|
|
160
|
+
? 'the body of an async TypeScript function (erasable syntax only; top-level `await` and `return` work)'
|
|
161
|
+
: 'the TypeScript program to execute';
|
|
162
|
+
const outputInstruction = programFormat === 'function-body'
|
|
163
|
+
? 'Emit curated output with `return` and/or `console.log(...)`.'
|
|
164
|
+
: 'Only printed/emitted program output enters the conversation.';
|
|
165
|
+
const argsMembers = [];
|
|
166
|
+
const outputMembers = [];
|
|
167
|
+
for (const schema of sorted) {
|
|
168
|
+
argsMembers.push(...docLines(schema.description, 1));
|
|
169
|
+
argsMembers.push(`${pad(1)}${renderKey(schema.name)}: ${jsonSchemaToTs(schema.parameters, 1)};`);
|
|
170
|
+
outputMembers.push(`${pad(1)}${renderKey(schema.name)}: ${jsonSchemaToTs(schema.output, 1)};`);
|
|
171
|
+
}
|
|
172
|
+
const declaration = [
|
|
173
|
+
`interface ToolArgsMap {\n${argsMembers.join('\n')}\n}`,
|
|
174
|
+
`interface ToolOutputMap {\n${outputMembers.join('\n')}\n}`,
|
|
175
|
+
'type ToolName = keyof ToolOutputMap',
|
|
176
|
+
['declare class ToolCallError extends Error {', ' readonly name: "ToolCallError";', ' readonly toolName: ToolName;', '}'].join('\n'),
|
|
177
|
+
['declare const tools: {', ' [K in ToolName]: (args: ToolArgsMap[K]) => Promise<ToolOutputMap[K]>;', '}'].join('\n'),
|
|
178
|
+
].join('\n\n');
|
|
179
|
+
return [
|
|
180
|
+
'## Hybrid SDK bindings (`run_code`)',
|
|
181
|
+
'',
|
|
182
|
+
`\`run_code\` executes ${programDescription}. Direct wire tools are NOT available on \`tools\` unless explicitly configured — call those directly. Call every SDK tool below only inside \`run_code\`:`,
|
|
183
|
+
'',
|
|
184
|
+
'- Call tools as `await tools.name(args)` (quoted for exotic names: `tools["my-tool"](args)`). Each resolves to its typed canonical JSON value; arguments must be lossless JSON.',
|
|
185
|
+
'- A failed tool call rejects with `ToolCallError` (carrying `.toolName` and `.message`); handle errors with `try/catch`.',
|
|
186
|
+
'- Batch independent read/search calls concurrently with `Promise.all`; sequence dependent steps with `await`.',
|
|
187
|
+
`- ${outputInstruction} Image-bearing subtool results are attached automatically after the run; extract only what you need from text/JSON results.`,
|
|
188
|
+
'',
|
|
189
|
+
'```ts',
|
|
190
|
+
'type JsonValue = null | boolean | number | string | JsonValue[] | { [key: string]: JsonValue }',
|
|
191
|
+
'',
|
|
192
|
+
declaration,
|
|
193
|
+
'```',
|
|
194
|
+
].join('\n');
|
|
195
|
+
}
|
|
196
|
+
//# sourceMappingURL=hybrid-sdk.js.map
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/** Lossless JSON helpers, hybrid PTC errors, and per-session run/reset coordination. */
|
|
2
|
+
import type { Context } from '@deepseek-ai/cordis';
|
|
3
|
+
import { HarnessError } from '@deepseek-ai/dsh-llm';
|
|
4
|
+
import type { PtcJsonValue } from '@deepseek-ai/dsh-ptc-runtime';
|
|
5
|
+
import type { SandboxMode } from '@deepseek-ai/dsh-sandbox';
|
|
6
|
+
import type { SessionId } from '@deepseek-ai/dsh-session';
|
|
7
|
+
export declare class CodeRunFailedError extends HarnessError {
|
|
8
|
+
constructor(message: string);
|
|
9
|
+
}
|
|
10
|
+
export declare class CodeResetFailedError extends HarnessError {
|
|
11
|
+
constructor(message: string);
|
|
12
|
+
}
|
|
13
|
+
export declare function snapshotJsonValue(value: unknown, seen?: WeakSet<object>): PtcJsonValue | undefined;
|
|
14
|
+
export declare function deepFreeze<T>(value: T): T;
|
|
15
|
+
export declare function jsonNormalizeArgs(value: unknown): {
|
|
16
|
+
dispatched: unknown;
|
|
17
|
+
logged: unknown;
|
|
18
|
+
};
|
|
19
|
+
export declare function renderRunCodeValue(value: PtcJsonValue): string;
|
|
20
|
+
export declare class HybridProgramCalls {
|
|
21
|
+
private readonly calls;
|
|
22
|
+
private readonly resets;
|
|
23
|
+
private disposed;
|
|
24
|
+
constructor(ctx: Context);
|
|
25
|
+
run<T>(sessionId: SessionId, signal: AbortSignal, mode: () => SandboxMode | undefined, fn: (callSignal: AbortSignal) => Promise<T>): Promise<T>;
|
|
26
|
+
reset(sessionId: SessionId, release: () => Promise<void>, notify?: () => void): Promise<void>;
|
|
27
|
+
}
|
|
28
|
+
//# sourceMappingURL=program-calls.d.ts.map
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
import { HarnessError } from '@deepseek-ai/dsh-llm';
|
|
2
|
+
import { toolErrorMessage } from '@deepseek-ai/dsh-tools/pipeline';
|
|
3
|
+
export class CodeRunFailedError extends HarnessError {
|
|
4
|
+
constructor(message) {
|
|
5
|
+
super(message, 'PTC_RUN_FAILED');
|
|
6
|
+
this.name = 'CodeRunFailedError';
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
export class CodeResetFailedError extends HarnessError {
|
|
10
|
+
constructor(message) {
|
|
11
|
+
super(message, 'PTC_RESET_FAILED');
|
|
12
|
+
this.name = 'CodeResetFailedError';
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
export function snapshotJsonValue(value, seen = new WeakSet()) {
|
|
16
|
+
if (value === undefined || typeof value === 'function' || typeof value === 'symbol' || typeof value === 'bigint') {
|
|
17
|
+
return undefined;
|
|
18
|
+
}
|
|
19
|
+
if (value === null || typeof value === 'boolean' || typeof value === 'string')
|
|
20
|
+
return value;
|
|
21
|
+
if (typeof value === 'number') {
|
|
22
|
+
if (!Number.isFinite(value))
|
|
23
|
+
throw new Error('non-finite number is not lossless JSON');
|
|
24
|
+
return Object.is(value, -0) ? 0 : value;
|
|
25
|
+
}
|
|
26
|
+
if (typeof value !== 'object')
|
|
27
|
+
return undefined;
|
|
28
|
+
if (seen.has(value))
|
|
29
|
+
throw new Error('cyclic structure is not lossless JSON');
|
|
30
|
+
seen.add(value);
|
|
31
|
+
try {
|
|
32
|
+
if (Array.isArray(value)) {
|
|
33
|
+
const copy = [];
|
|
34
|
+
for (let i = 0; i < value.length; i++) {
|
|
35
|
+
if (!Object.hasOwn(value, i))
|
|
36
|
+
throw new Error('sparse array is not lossless JSON');
|
|
37
|
+
const item = snapshotJsonValue(value[i], seen);
|
|
38
|
+
if (item === undefined)
|
|
39
|
+
throw new Error('array element is not lossless JSON');
|
|
40
|
+
copy.push(item);
|
|
41
|
+
}
|
|
42
|
+
return copy;
|
|
43
|
+
}
|
|
44
|
+
const proto = Object.getPrototypeOf(value);
|
|
45
|
+
if (proto !== Object.prototype && proto !== null) {
|
|
46
|
+
throw new Error('non-plain object is not lossless JSON');
|
|
47
|
+
}
|
|
48
|
+
const copy = {};
|
|
49
|
+
for (const key of Object.keys(value)) {
|
|
50
|
+
const prop = value[key];
|
|
51
|
+
if (prop === undefined)
|
|
52
|
+
continue;
|
|
53
|
+
const snapped = snapshotJsonValue(prop, seen);
|
|
54
|
+
if (snapped === undefined)
|
|
55
|
+
throw new Error(`property ${JSON.stringify(key)} is not lossless JSON`);
|
|
56
|
+
copy[key] = snapped;
|
|
57
|
+
}
|
|
58
|
+
return copy;
|
|
59
|
+
}
|
|
60
|
+
finally {
|
|
61
|
+
seen.delete(value);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
export function deepFreeze(value) {
|
|
65
|
+
if (value !== null && typeof value === 'object' && !Object.isFrozen(value)) {
|
|
66
|
+
Object.freeze(value);
|
|
67
|
+
for (const child of Object.values(value)) {
|
|
68
|
+
deepFreeze(child);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return value;
|
|
72
|
+
}
|
|
73
|
+
export function jsonNormalizeArgs(value) {
|
|
74
|
+
let snapshot;
|
|
75
|
+
try {
|
|
76
|
+
snapshot = snapshotJsonValue(value);
|
|
77
|
+
}
|
|
78
|
+
catch (error) {
|
|
79
|
+
throw new Error(`tool arguments must be lossless JSON: ${error instanceof Error ? error.message : String(error)}`);
|
|
80
|
+
}
|
|
81
|
+
if (snapshot === undefined) {
|
|
82
|
+
throw new Error('tool arguments must be lossless JSON (call the tool with an arguments object, e.g. `{}`)');
|
|
83
|
+
}
|
|
84
|
+
const logged = snapshotJsonValue(snapshot);
|
|
85
|
+
if (logged === undefined) {
|
|
86
|
+
throw new Error('tool arguments could not be detached for durable logging');
|
|
87
|
+
}
|
|
88
|
+
return { dispatched: snapshot, logged };
|
|
89
|
+
}
|
|
90
|
+
export function renderRunCodeValue(value) {
|
|
91
|
+
if (typeof value === 'string')
|
|
92
|
+
return value;
|
|
93
|
+
const encoded = JSON.stringify(value, null, 2);
|
|
94
|
+
return encoded ?? String(value);
|
|
95
|
+
}
|
|
96
|
+
export class HybridProgramCalls {
|
|
97
|
+
calls = new Map();
|
|
98
|
+
resets = new Map();
|
|
99
|
+
disposed = false;
|
|
100
|
+
constructor(ctx) {
|
|
101
|
+
if (typeof ctx.on === 'function') {
|
|
102
|
+
ctx.on('session/event', (session, event) => {
|
|
103
|
+
if (event.type !== 'sandbox/mode')
|
|
104
|
+
return;
|
|
105
|
+
for (const call of this.calls.get(session.id) ?? []) {
|
|
106
|
+
if (call.mode !== undefined && call.mode !== event.data.mode) {
|
|
107
|
+
call.controller.abort(new Error('sandbox mode changed'));
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}, { global: true });
|
|
111
|
+
}
|
|
112
|
+
if (typeof ctx.effect === 'function') {
|
|
113
|
+
ctx.effect(() => async () => {
|
|
114
|
+
this.disposed = true;
|
|
115
|
+
const active = [...this.calls.values()].flatMap(set => [...set]);
|
|
116
|
+
for (const call of active)
|
|
117
|
+
call.controller.abort(new Error('Hybrid tool executor disposed'));
|
|
118
|
+
await Promise.allSettled([
|
|
119
|
+
...active.map(call => call.done),
|
|
120
|
+
...[...this.resets.values()].map(reset => reset.done),
|
|
121
|
+
]);
|
|
122
|
+
}, 'Codex hybrid PTC calls');
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
async run(sessionId, signal, mode, fn) {
|
|
126
|
+
for (;;) {
|
|
127
|
+
const reset = this.resets.get(sessionId);
|
|
128
|
+
if (reset !== undefined) {
|
|
129
|
+
try {
|
|
130
|
+
await reset.done;
|
|
131
|
+
}
|
|
132
|
+
catch (error) {
|
|
133
|
+
throw new CodeRunFailedError(`Program environment unavailable: ${toolErrorMessage(error)}. Ask the host to reset it before running more code.`);
|
|
134
|
+
}
|
|
135
|
+
continue;
|
|
136
|
+
}
|
|
137
|
+
const pending = [...this.calls.get(sessionId) ?? []].filter(call => call.signal.aborted);
|
|
138
|
+
if (pending.length === 0)
|
|
139
|
+
break;
|
|
140
|
+
await Promise.all(pending.map(call => call.done));
|
|
141
|
+
}
|
|
142
|
+
signal.throwIfAborted();
|
|
143
|
+
if (this.disposed)
|
|
144
|
+
throw new Error('Hybrid tool executor disposed');
|
|
145
|
+
const controller = new AbortController();
|
|
146
|
+
const done = Promise.withResolvers();
|
|
147
|
+
const callSignal = AbortSignal.any([signal, controller.signal]);
|
|
148
|
+
const call = { mode: mode(), controller, signal: callSignal, done: done.promise };
|
|
149
|
+
let sessionCalls = this.calls.get(sessionId);
|
|
150
|
+
if (sessionCalls === undefined) {
|
|
151
|
+
sessionCalls = new Set();
|
|
152
|
+
this.calls.set(sessionId, sessionCalls);
|
|
153
|
+
}
|
|
154
|
+
sessionCalls.add(call);
|
|
155
|
+
try {
|
|
156
|
+
return await fn(callSignal);
|
|
157
|
+
}
|
|
158
|
+
finally {
|
|
159
|
+
sessionCalls.delete(call);
|
|
160
|
+
if (sessionCalls.size === 0)
|
|
161
|
+
this.calls.delete(sessionId);
|
|
162
|
+
done.resolve(undefined);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
reset(sessionId, release, notify) {
|
|
166
|
+
if (this.disposed)
|
|
167
|
+
return Promise.reject(new Error('Hybrid tool executor disposed'));
|
|
168
|
+
const previous = this.resets.get(sessionId);
|
|
169
|
+
if (previous !== undefined && !previous.settled)
|
|
170
|
+
return previous.done;
|
|
171
|
+
const done = Promise.withResolvers();
|
|
172
|
+
const reset = { done: done.promise, settled: false };
|
|
173
|
+
this.resets.set(sessionId, reset);
|
|
174
|
+
const calls = [...this.calls.get(sessionId) ?? []];
|
|
175
|
+
for (const call of calls)
|
|
176
|
+
call.controller.abort(new Error('PTC environment reset'));
|
|
177
|
+
void Promise.allSettled([Promise.resolve().then(release), ...calls.map(call => call.done)]).then((results) => {
|
|
178
|
+
const failure = results.find(result => result.status === 'rejected');
|
|
179
|
+
if (failure !== undefined)
|
|
180
|
+
throw failure.reason;
|
|
181
|
+
notify?.();
|
|
182
|
+
}).then(() => {
|
|
183
|
+
reset.settled = true;
|
|
184
|
+
this.resets.delete(sessionId);
|
|
185
|
+
done.resolve(undefined);
|
|
186
|
+
}, (error) => {
|
|
187
|
+
reset.settled = true;
|
|
188
|
+
done.reject(error);
|
|
189
|
+
});
|
|
190
|
+
return reset.done;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
//# sourceMappingURL=program-calls.js.map
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/** Concurrent/exclusive nested subcall queue and `run_code` execution runtime for hybrid mode. */
|
|
2
|
+
import type { Context } from '@deepseek-ai/cordis';
|
|
3
|
+
import type { Agent } from '@deepseek-ai/dsh-agent';
|
|
4
|
+
import { ToolCallId } from '@deepseek-ai/dsh-llm';
|
|
5
|
+
import type { ContentBlock } from '@deepseek-ai/dsh-llm';
|
|
6
|
+
import type { PtcJsonValue, PtcProgramFormat, PtcRuntime } from '@deepseek-ai/dsh-ptc-runtime';
|
|
7
|
+
import type { SandboxExecutionPolicy, SandboxMode } from '@deepseek-ai/dsh-sandbox';
|
|
8
|
+
import type { Scoped } from '@deepseek-ai/dsh-scope';
|
|
9
|
+
import type { ToolExecution, ToolRunContext, ToolsService } from '@deepseek-ai/dsh-tools';
|
|
10
|
+
import { ToolExecutionPipeline } from '@deepseek-ai/dsh-tools/pipeline';
|
|
11
|
+
export interface PtcDispatchStartEventData {
|
|
12
|
+
rootCallId: ToolCallId;
|
|
13
|
+
parentCallId: ToolCallId;
|
|
14
|
+
subCallId: ToolCallId;
|
|
15
|
+
name: string;
|
|
16
|
+
arguments: unknown;
|
|
17
|
+
}
|
|
18
|
+
export interface PtcDispatchEventData extends PtcDispatchStartEventData {
|
|
19
|
+
isError: boolean;
|
|
20
|
+
content: ContentBlock[];
|
|
21
|
+
meta?: PtcJsonValue;
|
|
22
|
+
error?: {
|
|
23
|
+
name: string;
|
|
24
|
+
code: string;
|
|
25
|
+
reason?: string;
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
export interface PtcDispatchLog {
|
|
29
|
+
readonly exec: ToolExecution;
|
|
30
|
+
readonly agent?: Agent;
|
|
31
|
+
readonly subCallId: ToolCallId;
|
|
32
|
+
readonly name: string;
|
|
33
|
+
readonly isError: boolean;
|
|
34
|
+
readonly content: ContentBlock[];
|
|
35
|
+
}
|
|
36
|
+
declare module '@deepseek-ai/cordis' {
|
|
37
|
+
interface Events {
|
|
38
|
+
'tools/ptc-dispatch-log'(this: Scoped<ToolsService>, dispatch: PtcDispatchLog, next: () => Promise<ContentBlock[]>): Promise<ContentBlock[]>;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
declare module '@deepseek-ai/dsh-session/types' {
|
|
42
|
+
interface SessionEventMap {
|
|
43
|
+
'tool/ptc-dispatch-start': PtcDispatchStartEventData;
|
|
44
|
+
'tool/ptc-dispatch': PtcDispatchEventData;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
declare module '@deepseek-ai/dsh-llm' {
|
|
48
|
+
interface MessageSourceMap {
|
|
49
|
+
'ptc-mode': {
|
|
50
|
+
kind: 'ptc-mode';
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
export type PtcSdkLanguage = 'typescript' | 'python';
|
|
55
|
+
export type SupportedPtcRuntime = PtcRuntime & {
|
|
56
|
+
readonly language: PtcSdkLanguage;
|
|
57
|
+
};
|
|
58
|
+
export interface RunCodeOutput {
|
|
59
|
+
logs: string[];
|
|
60
|
+
result?: PtcJsonValue;
|
|
61
|
+
sandbox?: {
|
|
62
|
+
mode: SandboxMode;
|
|
63
|
+
denied: boolean;
|
|
64
|
+
enforcement?: 'full' | 'partial';
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
export interface RunCodeArgs {
|
|
68
|
+
code: string;
|
|
69
|
+
description: string;
|
|
70
|
+
timeoutMs?: number;
|
|
71
|
+
sandbox_permissions?: string;
|
|
72
|
+
justification?: string;
|
|
73
|
+
}
|
|
74
|
+
export declare function requirePtcRuntime(ctx: Context): SupportedPtcRuntime;
|
|
75
|
+
export declare function resolveProgramFormat(runtime: PtcRuntime): PtcProgramFormat;
|
|
76
|
+
export declare function escalationGuidance(mode: PtcRuntime['sandboxMode']): string;
|
|
77
|
+
export declare function resolveRunCodeDescription(runtime: SupportedPtcRuntime): string;
|
|
78
|
+
export declare function resolveSandboxPolicy(ctx: Context, exec: ToolRunContext): SandboxExecutionPolicy;
|
|
79
|
+
export interface HybridRunHost {
|
|
80
|
+
readonly ctx: Context;
|
|
81
|
+
readonly pipeline: ToolExecutionPipeline;
|
|
82
|
+
readonly allowNestedDirectTools: boolean;
|
|
83
|
+
readonly maxParallelSubCalls: number;
|
|
84
|
+
readonly maxSubCallsPerMinute: number;
|
|
85
|
+
readonly stateOwners: WeakMap<object, WeakSet<PtcRuntime>>;
|
|
86
|
+
readonly isDirect: (name: string) => boolean;
|
|
87
|
+
readonly unwrapRuntime: (runtime: PtcRuntime) => PtcRuntime;
|
|
88
|
+
}
|
|
89
|
+
export declare function executeHybridRunCode(host: HybridRunHost, rawArgs: RunCodeArgs, exec: ToolRunContext, callSignal: AbortSignal): Promise<RunCodeOutput>;
|
|
90
|
+
//# sourceMappingURL=subcall-scheduler.d.ts.map
|