code-mode.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /**
  2. * Code Mode `run_code` transport. Programs call the registry's agent-visible
  3. * tools through nested, sequential executions; each sub-dispatch is logged for
  4. * reconstruction, while only the outer curated result enters model history.
  5. * @module @deepseek-ai/dsh-tools/src/code-mode
  6. */
  7. import { parse } from 'node:path'
  8. import { CallId, HarnessError } from '@deepseek-ai/dsh-llm'
  9. import type { ContentBlock } from '@deepseek-ai/dsh-llm'
  10. import type { CodeBindingFunction, CodeRunResult, CodeRuntime } from '@deepseek-ai/dsh-code-runtime'
  11. import { snapshotJsonValue } from '@deepseek-ai/dsh-session'
  12. import type { JsonValue } from '@deepseek-ai/dsh-session'
  13. import { defineTool } from './schema.ts'
  14. import type { ToolDefinition, ToolRegistry } from './index.ts'
  15. declare module '@deepseek-ai/dsh-session' {
  16. interface SessionEventMap {
  17. /**
  18. * One bridged sub-dispatch from a `run_code` program: the parent
  19. * `run_code` call id, the deterministic sub-call id
  20. * (`<parent>:code:<n>`), the tool `name` with its JSON-normalized
  21. * `arguments` — the exact value dispatched, normalized BEFORE dispatch,
  22. * so this append can never fail on payload shape — whether the sub-call
  23. * errored, and a bounded `resultSummary` of its model-facing text. Before
  24. * bounding, occurrences of a non-root session workspace path are
  25. * normalized to `.` so host-specific absolute path lengths cannot change
  26. * the summary.
  27. * Log-only: `deriveMessages()` ignores it, so sub-calls never re-enter
  28. * model context; persistence and UIs get every call. Appended inside the
  29. * parent `run_code`'s execution (the bridge drains its queue before
  30. * returning), so the turn-enclosure invariant holds by construction.
  31. */
  32. 'tool/code-dispatch': { parentCallId: CallId; subCallId: CallId; name: string; arguments: unknown; isError: boolean; resultSummary: string }
  33. }
  34. }
  35. /** The model-facing name of the Code Mode tool. */
  36. export const RUN_CODE_NAME = 'run_code'
  37. /** The `tools:sdk` section order: inside the 100–199 tool-guidance band, after per-tool guidance sections. */
  38. export const SDK_SECTION_ORDER = 150
  39. /**
  40. * Thrown by `run_code` when the program run itself failed — a program
  41. * exception, a budget expiry, an abort, or substrate death. Extends
  42. * {@link HarnessError} (`code: 'CODE_RUN_FAILED'`); the registry's execution
  43. * pipeline converts it into a structured `isError` result whose text carries
  44. * the failure kind plus the captured logs, so the model can self-correct.
  45. */
  46. export class CodeRunFailedError extends HarnessError {
  47. constructor(message: string) {
  48. super(message, 'CODE_RUN_FAILED')
  49. this.name = 'CodeRunFailedError'
  50. }
  51. }
  52. /**
  53. * Cap for a `tool/code-dispatch` event's `resultSummary`. A log-ergonomics
  54. * constant, not config: the full result already flows to the program; the
  55. * summary exists so log readers see what a sub-call returned at a glance.
  56. */
  57. const SUMMARY_MAX_CHARS = 200
  58. /** Join Native content for the bounded durable sub-dispatch summary; non-text blocks become diagnostic placeholders. */
  59. function textOf(content: ContentBlock[]): string {
  60. return content
  61. .map((block) => {
  62. switch (block.type) {
  63. case 'text': return block.text
  64. // ContentBlockMap is merge-extensible — future block kinds land here
  65. // deliberately (no assertNever on merge-extensible unions).
  66. default: return `[${block.type} content]`
  67. }
  68. })
  69. .join('\n')
  70. }
  71. /** Normalize workspace paths, then bound a sub-call's model-facing text for its durable log summary. */
  72. function summarize(text: string, cwd: string | undefined): string {
  73. const stableText = cwd === undefined || cwd === parse(cwd).root
  74. ? text
  75. : text.replaceAll(cwd, '.')
  76. return stableText.length > SUMMARY_MAX_CHARS ? `${stableText.slice(0, SUMMARY_MAX_CHARS)}…` : stableText
  77. }
  78. /**
  79. * Snapshot one binding call's argument as lossless JSON, then snapshot that
  80. * detached value again so dispatch and logging stay independent without
  81. * reintroducing structured-clone's platform-specific nesting limit.
  82. */
  83. function jsonNormalizeArgs(value: unknown): { dispatched: unknown; logged: unknown } {
  84. let snapshot: JsonValue | undefined
  85. try {
  86. snapshot = snapshotJsonValue(value) as JsonValue | undefined
  87. } catch (error: unknown) {
  88. throw new Error(`tool arguments must be lossless JSON: ${error instanceof Error ? error.message : String(error)}`)
  89. }
  90. if (snapshot === undefined) {
  91. throw new Error('tool arguments must be lossless JSON (call the tool with an arguments object, e.g. `{}`)')
  92. }
  93. const logged = snapshotJsonValue(snapshot)
  94. /* v8 ignore next -- snapshot is already a detached lossless JSON value. */
  95. if (logged === undefined) {
  96. throw new Error('tool arguments could not be detached for durable logging')
  97. }
  98. return { dispatched: snapshot, logged }
  99. }
  100. /** Two-space JSON presentation, matching the existing shallow `run_code` text contract. */
  101. const JSON_INDENT = ' '
  102. /**
  103. * ECMAScript caps `JSON.stringify`'s `space` string at ten characters. The
  104. * renderer also caps TOTAL indentation there, compacting deeper subtrees, so
  105. * formatted output remains linear in the canonical JSON size.
  106. */
  107. const MAX_JSON_INDENT_CHARS = 10
  108. /** A pending fragment in the iterative JSON presentation traversal. */
  109. type JsonRenderTask =
  110. | { kind: 'text'; text: string }
  111. | { kind: 'value'; value: JsonValue; depth: number; compact: boolean }
  112. /** Render one non-string JSON root without recursive traversal or unbounded indentation growth. */
  113. function renderJsonValue(value: Exclude<JsonValue, string>): string {
  114. const chunks: string[] = []
  115. const tasks: JsonRenderTask[] = [{ kind: 'value', value, depth: 0, compact: false }]
  116. for (let task = tasks.pop(); task !== undefined; task = tasks.pop()) {
  117. if (task.kind === 'text') {
  118. chunks.push(task.text)
  119. continue
  120. }
  121. const current = task.value
  122. if (current === null || typeof current === 'boolean' || typeof current === 'number') {
  123. chunks.push(String(current))
  124. continue
  125. }
  126. if (typeof current === 'string') {
  127. chunks.push(JSON.stringify(current))
  128. continue
  129. }
  130. const compact = task.compact || (task.depth + 1) * JSON_INDENT.length > MAX_JSON_INDENT_CHARS
  131. const childDepth = task.depth + 1
  132. if (Array.isArray(current)) {
  133. chunks.push('[')
  134. if (current.length === 0) {
  135. chunks.push(']')
  136. continue
  137. }
  138. tasks.push({ kind: 'text', text: compact ? ']' : `\n${JSON_INDENT.repeat(task.depth)}]` })
  139. for (let index = current.length - 1; index >= 0; index--) {
  140. const item = current[index]
  141. /* v8 ignore next -- canonical JsonValue arrays are dense. */
  142. if (item === undefined) throw new Error('cannot render a sparse JSON array')
  143. tasks.push({ kind: 'value', value: item, depth: childDepth, compact })
  144. tasks.push({
  145. kind: 'text',
  146. text: compact
  147. ? index === 0 ? '' : ','
  148. : `${index === 0 ? '\n' : ',\n'}${JSON_INDENT.repeat(childDepth)}`,
  149. })
  150. }
  151. continue
  152. }
  153. const keys = Object.keys(current)
  154. chunks.push('{')
  155. if (keys.length === 0) {
  156. chunks.push('}')
  157. continue
  158. }
  159. tasks.push({ kind: 'text', text: compact ? '}' : `\n${JSON_INDENT.repeat(task.depth)}}` })
  160. for (let index = keys.length - 1; index >= 0; index--) {
  161. const key = keys[index]
  162. /* v8 ignore next -- the loop is bounded by the captured key count. */
  163. if (key === undefined) throw new Error('cannot render a missing JSON object key')
  164. const item = current[key]
  165. /* v8 ignore next -- canonical JsonValue records contain no undefined properties. */
  166. if (item === undefined) throw new Error('cannot render an undefined JSON object property')
  167. tasks.push({ kind: 'value', value: item, depth: childDepth, compact })
  168. tasks.push({
  169. kind: 'text',
  170. text: compact
  171. ? `${index === 0 ? '' : ','}${JSON.stringify(key)}:`
  172. : `${index === 0 ? '\n' : ',\n'}${JSON_INDENT.repeat(childDepth)}${JSON.stringify(key)}: `,
  173. })
  174. }
  175. }
  176. return chunks.join('')
  177. }
  178. /** Render one present program completion value for the model-facing result text. */
  179. function renderValue(value: JsonValue): string {
  180. return typeof value === 'string' ? value : renderJsonValue(value)
  181. }
  182. /** Canonical value returned by the outer Code Mode transport. */
  183. type RunCodeOutput = { logs: string[]; result?: JsonValue }
  184. /**
  185. * Build the `run_code` {@link ToolDefinition}: one required `code` parameter,
  186. * executed through the dispatch bridge described above. The
  187. * registry reserves it as presentation infrastructure under non-native modes,
  188. * outside the filterable global/scoped capability layers.
  189. * @param registry - the owning registry (sub-calls go through its `execute`,
  190. * bindings cover its registered tools).
  191. * @param requireRuntime - resolves `ctx.codeRuntime` or throws the loud
  192. * misconfiguration error (shared with the registry's assembly-time checks).
  193. * @returns the registry-ready definition.
  194. */
  195. export function createRunCodeTool(registry: ToolRegistry, requireRuntime: () => CodeRuntime): ToolDefinition {
  196. return defineTool({
  197. name: RUN_CODE_NAME,
  198. description:
  199. 'Execute a TypeScript program against the available tools. Write the BODY of an '
  200. + 'async function (erasable syntax only; top-level `await` and `return` work) and '
  201. + 'call tools as `await tools.name(args)` per the declarations in the system prompt. '
  202. + 'Only what you print or return comes back — curate it.',
  203. parameters: {
  204. code: { type: 'string', required: true, description: 'The program: the body of an async TypeScript function.' },
  205. },
  206. output: {
  207. schema: {
  208. type: 'object',
  209. additionalProperties: false,
  210. properties: {
  211. logs: { type: 'array', required: true, items: { type: 'string' } },
  212. result: { type: 'json' },
  213. },
  214. },
  215. render: (_args, value) => {
  216. const rendered = value.result === undefined ? '' : renderValue(value.result)
  217. const parts = [value.logs.join('\n'), rendered].filter(part => part.length > 0)
  218. return [{ type: 'text', text: parts.length > 0 ? parts.join('\n') : '(run_code completed with no output)' }]
  219. },
  220. },
  221. async execute(args, exec): Promise<RunCodeOutput> {
  222. const runtime = requireRuntime()
  223. // The run-scoped abort: follows the outer signal in, and fires when the
  224. // run settles for ANY reason, so an in-flight sub-dispatch is aborted
  225. // (its executor kills on this signal) instead of orphaned, and
  226. // queued-unstarted dispatches are abandoned.
  227. const runController = new AbortController()
  228. const onOuterAbort = (): void => { runController.abort(exec.signal.reason) }
  229. exec.signal.addEventListener('abort', onOuterAbort, { once: true })
  230. let dispatches = 0
  231. // The per-run serialization queue: every binding call chains onto the tail, so even
  232. // `Promise.all` executes the underlying tool calls one at a time in submission order (the
  233. // tool contract carries no concurrency-safety metadata yet).
  234. let queue: Promise<void> = Promise.resolve()
  235. const enqueue = <T>(task: () => Promise<T>): Promise<T> => {
  236. const turn = queue.then(() => {
  237. if (runController.signal.aborted) {
  238. throw new Error(`run_code run is over (${String(runController.signal.reason)}); tool call abandoned`)
  239. }
  240. return task()
  241. })
  242. queue = turn.then(() => undefined, () => undefined)
  243. return turn
  244. }
  245. // Read through a call, not a bare property: the abort state genuinely
  246. // changes across awaits, and a direct `.aborted` re-check after one
  247. // would be narrowed away by control flow analysis.
  248. const runOver = (): boolean => runController.signal.aborted
  249. const binding = (name: string): CodeBindingFunction => async (rawArgs: unknown): Promise<JsonValue> => {
  250. if (runOver()) {
  251. throw new Error(`run_code run is over (${String(runController.signal.reason)}); ${name} not dispatched`)
  252. }
  253. const normalized = jsonNormalizeArgs(rawArgs)
  254. const outcome = await enqueue(async () => {
  255. const n = ++dispatches
  256. const subCallId = CallId(`${String(exec.callId)}:code:${n}`)
  257. const result = await registry.execute({
  258. callId: subCallId,
  259. name,
  260. arguments: normalized.dispatched,
  261. ...exec.agent ? { agent: exec.agent } : {},
  262. parent: exec.token,
  263. signal: runController.signal,
  264. })
  265. for (const context of result.additionalContexts ?? []) {
  266. exec.deferContext(context)
  267. }
  268. const text = textOf(result.content)
  269. exec.agent?.session.append('tool/code-dispatch', {
  270. parentCallId: exec.callId,
  271. subCallId,
  272. name,
  273. // The SIBLING parse of the dispatched value: byte-identical JSON,
  274. // but a separate object — a tool mutating its args cannot desync
  275. // this record from what it actually received.
  276. arguments: normalized.logged,
  277. isError: result.isError,
  278. resultSummary: summarize(text, exec.agent.session.header.cwd),
  279. })
  280. return result.isError
  281. ? { isError: true as const, message: result.error.message }
  282. : { isError: false as const, value: result.value }
  283. })
  284. // A budget expiry or outer cancel that lands while this call was in
  285. // flight already aborted the dispatch; stop the program now rather
  286. // than hand it a result from a run that is over.
  287. if (runOver()) {
  288. throw new Error(`run_code run is over (${String(runController.signal.reason)}); ${name} result discarded`)
  289. }
  290. // The worker turns a binding rejection into ToolCallError and adds
  291. // only the binding name. Native content and internal error metadata
  292. // stay outside the program-facing failure contract.
  293. if (outcome.isError) throw new Error(outcome.message)
  294. return outcome.value
  295. }
  296. // Null-prototype + defineProperty, mirroring the worker-side namespace
  297. // build: a registered tool named `__proto__` must become an ordinary
  298. // own key (a plain-object assignment would hit the prototype setter,
  299. // silently dropping the binding), and the runtime host resolves
  300. // binding names as own properties only.
  301. const functions: Record<string, CodeBindingFunction> = Object.create(null) as Record<string, CodeBindingFunction>
  302. // Enumerate the CALLING AGENT's visible set (scoped tools join,
  303. // restricted globals vanish) — the same view the SDK section declared,
  304. // so a program can bind exactly what its prompt promised; sub-dispatch
  305. // re-resolves per call through the same view (exec.agent threads down).
  306. for (const schema of registry.schemas(exec.agent)) {
  307. if (schema.name === RUN_CODE_NAME) continue
  308. Object.defineProperty(functions, schema.name, { enumerable: true, value: binding(schema.name) })
  309. }
  310. try {
  311. let result: CodeRunResult
  312. try {
  313. result = await runtime.run({
  314. program: args.code,
  315. bindings: [{
  316. global: 'tools',
  317. functions,
  318. errorClass: { name: 'ToolCallError', memberNameProperty: 'toolName' },
  319. }],
  320. signal: runController.signal,
  321. })
  322. } finally {
  323. // Abort sub-dispatches and drain the folded queue before closing the turn.
  324. // Binding failures remain observable through their individual promises.
  325. runController.abort('run_code settled')
  326. await queue
  327. }
  328. if (result.error) {
  329. const logsText = result.logs.length > 0 ? `\nCaptured output:\n${result.logs.join('\n')}` : ''
  330. throw new CodeRunFailedError(`code run failed (${result.error.kind}): ${result.error.message}${logsText}`)
  331. }
  332. return {
  333. logs: result.logs,
  334. ...result.value !== undefined ? { result: result.value } : {},
  335. }
  336. } finally {
  337. exec.signal.removeEventListener('abort', onOuterAbort)
  338. }
  339. },
  340. // The program is the call's always-visible UI label.
  341. presentCall: args => ({
  342. card: 'generic',
  343. title: args.code,
  344. kind: 'execute',
  345. rawInput: args.code,
  346. }),
  347. // Deliberately no presentResult: the generic surface fallback keeps this
  348. // title and reads durable result content without duplicating a large raw
  349. // result into the host view payload.
  350. })
  351. }