feat(tools): let one agent choose its tool presentation, and ship code
Code Mode was a deployment-wide field on the host `tools` row: a deployment ran every session that way or none. The obvious product shape — 代码模式 beside 标准/极简/创造 in the preset picker — had nothing to hang on. The registry itself cannot move into a preset; the agent loop's scheduler, the api-proxy's presenters, and every tool plugin are its consumers. So split the registry from its projection: `presentAs(mode)` writes one cell on the calling agent's scope layer, exactly as `restrict()` does, and the three reads that decided presentation take that scope's mode instead of the service's. The config `mode` becomes the default agents shadow rather than a process-wide fact. Two consequences are load-bearing. `run_code` now enters a view only for scopes whose own mode presents it — a native agent must not find it dispatchable because another agent in the process does — and the reserved name holds whatever the configured mode, since any agent may select a code mode later. `dsh-agent-tool-mode` is the row a preset carries to declare this. A code mode waits for the host's `codeRuntime` rather than assuming it, so a runtime-less deployment fails the preset at mount, naming the row, instead of at the session's first request. The shipped `code` preset is `standard` plus that row, ordered second.
This commit is contained in:
70
packages/core/agent-tool-mode/src/index.ts
Normal file
70
packages/core/agent-tool-mode/src/index.ts
Normal file
@@ -0,0 +1,70 @@
|
||||
/**
|
||||
* Agent-plane presentation selector: the row an agent preset carries to say
|
||||
* which form of its tools the model sees.
|
||||
*
|
||||
* The tool registry itself stays on the host plane — the agent loop's
|
||||
* scheduler, the API proxy's presenters, and every tool plugin are all its
|
||||
* consumers, so it cannot move into a preset. What a preset CAN own is the
|
||||
* presentation: `ctx.tools.presentAs()` declares it for the mounting agent
|
||||
* alone, so a Code Mode agent runs beside native ones in one process.
|
||||
*
|
||||
* A code mode needs a TypeScript code runtime, which is a host-plane service
|
||||
* ([`dsh-code-runtime-worker`](../../code-runtime/code-runtime-worker/README.md)).
|
||||
* This row therefore waits for it rather than assuming it: a preset selecting
|
||||
* Code Mode against a deployment that composes no runtime fails at mount, named
|
||||
* in the preset's own activation audit, instead of at the first prompt.
|
||||
* @module @deepseek-ai/dsh-agent-tool-mode
|
||||
*/
|
||||
|
||||
import type { Context } from 'cordis'
|
||||
import z from 'schemastery'
|
||||
import type { ToolPresentationMode } from '@deepseek-ai/dsh-tools'
|
||||
// Type-only: brings the `ctx.tools` Context merge into this program.
|
||||
import type {} from '@deepseek-ai/dsh-tools'
|
||||
|
||||
/** Cordis plugin name. */
|
||||
export const name = 'tool-mode'
|
||||
|
||||
/**
|
||||
* Required services. `codeRuntime` is NOT listed: a `native` row must mount in
|
||||
* a deployment that composes no runtime, and the mode-dependent wait is
|
||||
* declared inside {@link apply} instead.
|
||||
*/
|
||||
export const inject = ['tools']
|
||||
|
||||
/** Plugin config. */
|
||||
export interface Config {
|
||||
/**
|
||||
* The form this agent's model sees. `native` sends every visible schema,
|
||||
* `code` sends only `run_code` plus a generated SDK, `both` sends both.
|
||||
* Required rather than defaulted: the deployment default is what a preset
|
||||
* without this row already gets, so an omitted value would mean the row was
|
||||
* composed for nothing.
|
||||
*/
|
||||
mode: ToolPresentationMode
|
||||
}
|
||||
|
||||
/** Runtime schema. */
|
||||
export const Config: z<Config> = z.object({
|
||||
mode: z.union(['native', 'code', 'both'] as const).required(),
|
||||
})
|
||||
|
||||
/**
|
||||
* Declare this agent's tool presentation.
|
||||
* @param ctx - the mounting agent's scope context.
|
||||
* @param config - the selected presentation.
|
||||
*/
|
||||
export function apply(ctx: Context, config: Config): void {
|
||||
// `presentAs` is itself the effect — it registers through the calling
|
||||
// context and hands back that exact disposer — so the declaration unwinds
|
||||
// with this row without a second wrapper owning it.
|
||||
if (config.mode === 'native') {
|
||||
ctx.tools.presentAs('native')
|
||||
return
|
||||
}
|
||||
// The wait is the loud failure: an entry still pending on `codeRuntime` is
|
||||
// what `dsh-agent-presets` reports as an unusable row, naming this id.
|
||||
ctx.inject(['codeRuntime'], (runtimeCtx: Context) => {
|
||||
runtimeCtx.tools.presentAs(config.mode)
|
||||
})
|
||||
}
|
||||
32
packages/core/agent-tool-mode/src/invariant.ts
Normal file
32
packages/core/agent-tool-mode/src/invariant.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
/**
|
||||
* Package-owned invariant companion for `@deepseek-ai/dsh-agent-tool-mode`.
|
||||
* @module @deepseek-ai/dsh-agent-tool-mode/invariant
|
||||
*/
|
||||
|
||||
/* jscpd:ignore-start */
|
||||
import type { Context } from 'cordis'
|
||||
import type { InvariantInstaller } from '@deepseek-ai/dsh-invariants'
|
||||
|
||||
const PACKAGE_NAME = '@deepseek-ai/dsh-agent-tool-mode'
|
||||
|
||||
/** Cordis companion plugin name. */
|
||||
export const name = 'tool-mode-invariant'
|
||||
/** Service required before the companion can reserve package ownership. */
|
||||
export const inject = ['invariants']
|
||||
|
||||
/**
|
||||
* No runtime invariant: this package makes exactly one scoped call into
|
||||
* `ctx.tools` and owns no event or snapshot of its own; the relation it
|
||||
* establishes — which presentation one agent's assembly uses — is the tool
|
||||
* registry's to hold, and `dsh-tools` observes it there.
|
||||
*/
|
||||
const install: InvariantInstaller = () => {}
|
||||
|
||||
/**
|
||||
* Register this package's invariant companion.
|
||||
* @param ctx - Cordis context carrying the invariant service.
|
||||
* @returns the installed registration's disposer after setup succeeds.
|
||||
*/
|
||||
export const apply = (ctx: Context): Promise<() => void> =>
|
||||
Promise.resolve(ctx.invariants.register(PACKAGE_NAME, install))
|
||||
/* jscpd:ignore-end */
|
||||
Reference in New Issue
Block a user