@qilin-agent/tools 0.0.0-stage → 3.1.3
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 +21 -0
- package/README.i18n.yaml +103 -0
- package/README.md +239 -2
- package/README.zh.md +240 -0
- package/lib/index.js +3705 -0
- package/lib/types/index.d.ts +844 -0
- package/lib/types/index.js +1334 -0
- package/lib/types/json-schema.d.ts +106 -0
- package/lib/types/json-schema.js +577 -0
- package/lib/types/presentation.d.ts +367 -0
- package/lib/types/presentation.js +8 -0
- package/lib/types/ptc.d.ts +80 -0
- package/lib/types/ptc.js +660 -0
- package/lib/types/py-types.d.ts +49 -0
- package/lib/types/py-types.js +788 -0
- package/lib/types/schema.d.ts +243 -0
- package/lib/types/schema.js +350 -0
- package/lib/types/testing.d.ts +25 -0
- package/lib/types/testing.js +25 -0
- package/lib/types/ts-types.d.ts +37 -0
- package/lib/types/ts-types.js +291 -0
- package/lib/types/types.d.ts +61 -0
- package/lib/types/types.js +7 -0
- package/package.json +68 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 kkutysllb
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.i18n.yaml
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
# Bilingual-pair consistency record for README.md (docs/i18n/README.md): per heading
|
|
2
|
+
# section, a hash of its English and Chinese blocks outside code blocks and generated regions.
|
|
3
|
+
# After editing either side, bring the other along and re-record with:
|
|
4
|
+
# pnpm run verify-translation-pairing --write packages/core/tools/README.md
|
|
5
|
+
/:
|
|
6
|
+
en: 3b12a77945c0d1e9
|
|
7
|
+
zh: e9af32047e4f658b
|
|
8
|
+
/qilin-tools:
|
|
9
|
+
en: a4b050d1b91ebf39
|
|
10
|
+
zh: 2a5cffff9987575d
|
|
11
|
+
/qilin-tools/summary:
|
|
12
|
+
en: 7fb51274d4f721df
|
|
13
|
+
zh: da9b19633629eefd
|
|
14
|
+
/qilin-tools/table-of-contents:
|
|
15
|
+
en: d152484eb41ac6b4
|
|
16
|
+
zh: 09388d293f9be9cb
|
|
17
|
+
/qilin-tools/use-this-package:
|
|
18
|
+
en: 5157221ba48c5c22
|
|
19
|
+
zh: 0fd32b2fd246495e
|
|
20
|
+
/qilin-tools/use-this-package/register-a-tool:
|
|
21
|
+
en: 02fce53c21a33d74
|
|
22
|
+
zh: 01110c4714dfd602
|
|
23
|
+
/qilin-tools/use-this-package/configure-the-presentation-mode:
|
|
24
|
+
en: 623fcab2e684b719
|
|
25
|
+
zh: cde146b5f4cd610d
|
|
26
|
+
/qilin-tools/use-this-package/restrict-tools-per-agent:
|
|
27
|
+
en: c6f98b2283cc2e47
|
|
28
|
+
zh: c2cdce5d13473f15
|
|
29
|
+
/qilin-tools/use-this-package/enforce-policy-on-calls:
|
|
30
|
+
en: 1dd4ee9d8cdd2038
|
|
31
|
+
zh: aa3e9790641e521f
|
|
32
|
+
/qilin-tools/use-this-package/host-presentation-descriptors:
|
|
33
|
+
en: d9aec6b6a174584a
|
|
34
|
+
zh: b42039a7015e0364
|
|
35
|
+
/qilin-tools/understand-the-implementation:
|
|
36
|
+
en: ce7e776e9d516bdb
|
|
37
|
+
zh: 59d2751743db7795
|
|
38
|
+
/qilin-tools/understand-the-implementation/design-concept:
|
|
39
|
+
en: 92e8af53e6067bad
|
|
40
|
+
zh: 33c7b99b42c5acac
|
|
41
|
+
/qilin-tools/understand-the-implementation/source-map:
|
|
42
|
+
en: c14e20d6a5c4c408
|
|
43
|
+
zh: ab9dc0fd38006749
|
|
44
|
+
/qilin-tools/understand-the-implementation/execution-and-cancellation:
|
|
45
|
+
en: dc44a88997777988
|
|
46
|
+
zh: 56454aeace1c8b77
|
|
47
|
+
/qilin-tools/understand-the-implementation/ptc-mode:
|
|
48
|
+
en: 3c9d085b862b98e0
|
|
49
|
+
zh: d243755c77c3432d
|
|
50
|
+
/qilin-tools/understand-the-implementation/extension-points:
|
|
51
|
+
en: c2a14d6f22bc9428
|
|
52
|
+
zh: 89c1414fca465931
|
|
53
|
+
/qilin-tools/further-exploration:
|
|
54
|
+
en: 890aed31a2a34950
|
|
55
|
+
zh: d1833fc8d1bb8bca
|
|
56
|
+
/qilin-tools/model-experience:
|
|
57
|
+
en: 215e7ba838619b7b
|
|
58
|
+
zh: a311da3843709f90
|
|
59
|
+
/qilin-tools/model-experience/normal-tool-schemas:
|
|
60
|
+
en: 483b01d9bb2cb49a
|
|
61
|
+
zh: 1d95095e76fb0996
|
|
62
|
+
/qilin-tools/model-experience/normal-tool-schemas/what-the-model-sees:
|
|
63
|
+
en: 1978238355b5755a
|
|
64
|
+
zh: c65b1c5309ec1831
|
|
65
|
+
/qilin-tools/model-experience/normal-tool-schemas/token-effect:
|
|
66
|
+
en: d594b71ff5e4cc87
|
|
67
|
+
zh: dcec1ea0dd2a0fa6
|
|
68
|
+
/qilin-tools/model-experience/normal-tool-schemas/kv-cache-effect:
|
|
69
|
+
en: 7c5e75964e020b2c
|
|
70
|
+
zh: d50d0402d15232d9
|
|
71
|
+
/qilin-tools/model-experience/ptc-mode-schema-and-system-prompt:
|
|
72
|
+
en: eaefd7303f22977d
|
|
73
|
+
zh: 9c425f9501128224
|
|
74
|
+
/qilin-tools/model-experience/ptc-mode-schema-and-system-prompt/what-the-model-sees:
|
|
75
|
+
en: ad6b9ea4c514455f
|
|
76
|
+
zh: fe27c64ce8b0f465
|
|
77
|
+
/qilin-tools/model-experience/ptc-mode-schema-and-system-prompt/what-the-model-sees/typescript-ptc-mode-sdk-instructions-with-bash:
|
|
78
|
+
en: 3a8df15e4ae32f9e
|
|
79
|
+
zh: fc4a800ba363e835
|
|
80
|
+
/qilin-tools/model-experience/ptc-mode-schema-and-system-prompt/token-effect:
|
|
81
|
+
en: 12296fcafc14562c
|
|
82
|
+
zh: f8e9351bb8c6437d
|
|
83
|
+
/qilin-tools/model-experience/ptc-mode-schema-and-system-prompt/kv-cache-effect:
|
|
84
|
+
en: 78906628d634441f
|
|
85
|
+
zh: 492f133aea50cfe3
|
|
86
|
+
/qilin-tools/model-experience/tool-call-history-and-results:
|
|
87
|
+
en: a8503137ad03720c
|
|
88
|
+
zh: 611a2dadf7c0c706
|
|
89
|
+
/qilin-tools/model-experience/tool-call-history-and-results/what-the-model-sees:
|
|
90
|
+
en: 7c568da5a45e99ab
|
|
91
|
+
zh: 7e78f04448fc5450
|
|
92
|
+
/qilin-tools/model-experience/tool-call-history-and-results/token-effect:
|
|
93
|
+
en: 8fe3f024f364ed9f
|
|
94
|
+
zh: 89c1d5d24c5f950b
|
|
95
|
+
/qilin-tools/model-experience/tool-call-history-and-results/kv-cache-effect:
|
|
96
|
+
en: 2fbf2175168e8244
|
|
97
|
+
zh: ad59a120b54a0996
|
|
98
|
+
/qilin-tools/known-limitations-and-deferred-work:
|
|
99
|
+
en: 57ba640ee483c163
|
|
100
|
+
zh: 551dab190d346131
|
|
101
|
+
/qilin-tools/known-limitations-and-deferred-work/dev-note:
|
|
102
|
+
en: e67f633dc6fe7773
|
|
103
|
+
zh: 3bb0c20c93f16f98
|
package/README.md
CHANGED
|
@@ -1,3 +1,240 @@
|
|
|
1
|
-
|
|
1
|
+
---
|
|
2
|
+
description: "The tool registry and execution pipeline for tool authors and maintainers registering, restricting, presenting, or debugging model-facing tools."
|
|
3
|
+
kind: "package-reference"
|
|
4
|
+
---
|
|
2
5
|
|
|
3
|
-
|
|
6
|
+
# @qilin-agent/tools
|
|
7
|
+
|
|
8
|
+
English | [中文](README.zh.md)
|
|
9
|
+
|
|
10
|
+
## Summary
|
|
11
|
+
|
|
12
|
+
Use `qilin-tools` to expose typed capabilities to models, validate calls, enforce allow/deny/ask policy, and return finalized results without ending a turn on ordinary tool failures. Choose native Function Calling, [PTC mode](#ptc-mode), or both with `mode`; an agent can override the default through `presentAs`. Tool authors use `defineTool` to declare typed parameters and outputs, cooperative timeouts, parallel-safety, and optional UI presentation. Models see each permitted tool's declared name, description, and parameter schema; per-agent restrictions can narrow that visible set.
|
|
13
|
+
|
|
14
|
+
## Table of Contents
|
|
15
|
+
|
|
16
|
+
- [Use this package](#use-this-package)
|
|
17
|
+
- [Understand the implementation](#understand-the-implementation)
|
|
18
|
+
- [Further Exploration](#further-exploration)
|
|
19
|
+
- [Model Experience](#model-experience)
|
|
20
|
+
- [Known Limitations and Deferred Work](#known-limitations-and-deferred-work)
|
|
21
|
+
- [Dev Note](#dev-note)
|
|
22
|
+
|
|
23
|
+
-----
|
|
24
|
+
|
|
25
|
+
<a id="use-this-package"></a>
|
|
26
|
+
## Use this package
|
|
27
|
+
|
|
28
|
+
Mount `qilin-tools` wherever agents call tools: it provides `ctx.tools`, the registry every tool plugin registers into and the loop dispatches through. Registering a tool is enough to make it visible — the registry feeds its schemas into the system-prompt assembly automatically.
|
|
29
|
+
|
|
30
|
+
### Register a tool
|
|
31
|
+
|
|
32
|
+
`defineTool` builds a typed tool definition: a model-facing name, description, and parameter schema, a canonical output declaration, and an `execute` body that returns only the declared JSON value. Model arguments are validated before execution; invalid input becomes a normal error result.
|
|
33
|
+
|
|
34
|
+
```ts
|
|
35
|
+
import { readFile } from 'node:fs/promises'
|
|
36
|
+
import type { Context } from '@qilin-agent/kylin'
|
|
37
|
+
import { defineTool } from '@qilin-agent/tools'
|
|
38
|
+
|
|
39
|
+
declare const ctx: Context
|
|
40
|
+
|
|
41
|
+
ctx.tools.register(defineTool({
|
|
42
|
+
name: 'read_file',
|
|
43
|
+
description: 'Read a file from disk.',
|
|
44
|
+
parameters: {
|
|
45
|
+
path: { type: 'string', required: true, description: 'Absolute file path' },
|
|
46
|
+
offset: { type: 'number' },
|
|
47
|
+
limit: { type: 'number' },
|
|
48
|
+
},
|
|
49
|
+
output: {
|
|
50
|
+
schema: { type: 'string' },
|
|
51
|
+
render: (_args, value) => [{ type: 'text', text: value }],
|
|
52
|
+
},
|
|
53
|
+
async execute(args, exec) {
|
|
54
|
+
// args is typed: { path: string; offset?: number; limit?: number }
|
|
55
|
+
return readFile(args.path, { encoding: 'utf8', signal: exec.signal })
|
|
56
|
+
},
|
|
57
|
+
}))
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
The unified schema DSL supports `string`, `number`, `integer`, `boolean`, `null`, `array`, `object`, author-only `json`, and exact-one `oneOf`; `InferValue` preserves exact types through 16 container levels before widening to `JsonValue`. A raw JSON Schema (`JsonSchemaNode`) is the wire-level counterpart shared with subagents, workflows, and MCP.
|
|
61
|
+
|
|
62
|
+
### Configure the presentation mode
|
|
63
|
+
|
|
64
|
+
The `mode` config decides what the model sees: `native` (every visible schema), `ptc` (only `run_code` plus a generated SDK), or `both`.
|
|
65
|
+
|
|
66
|
+
```yaml
|
|
67
|
+
- name: '@qilin-agent/tools'
|
|
68
|
+
config:
|
|
69
|
+
mode: native
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
| Field | Default | Meaning |
|
|
73
|
+
|---|---|---|
|
|
74
|
+
| `mode` | `native` | How visible tools are presented to the model: `native`, `ptc`, or `both` |
|
|
75
|
+
| `maxParallelSubCalls` | `10` | Concurrency cap for a `run_code` program's overlapping sub-calls; `1` restores strictly serial dispatch |
|
|
76
|
+
|
|
77
|
+
The generated [configuration catalog](../../../docs/config-catalog.md#qilin-agenttools) is the exhaustive source for every accepted field. Non-native modes require a composed `ctx.ptcRuntime` whose language has a registered SDK renderer; an agent preset selects its own presentation with [`qilin-agent-tool-presentation`](../agent-tool-presentation/README.md), and one agent can shadow the default with `presentAs(mode)`.
|
|
78
|
+
|
|
79
|
+
### Restrict tools per agent
|
|
80
|
+
|
|
81
|
+
`ctx.tools.restrict(filter)` applies an allow or deny mask to the global tools one agent inherits; masks intersect, scoped registrations stay visible, and the restriction lifts when disposed. `ctx.tools.get(name, scope)` resolves a tool as one scope sees it. A Host-local presenter consumer passes the calling agent when it must match the definition that executed. `ctx.tools.schemas(scope)` returns the visible schemas without the `execute` functions.
|
|
82
|
+
|
|
83
|
+
### Enforce policy on calls
|
|
84
|
+
|
|
85
|
+
`ctx.tools.guard(guard)` registers a monotonic synchronous guard after the extensible `tools/pre-execute` waterfall: a returned reason denies the call, and no later listener can turn that denial back into permission. The pipeline's events give plugins more control — `tools/pre-execute` decides allow/deny/ask, `tools/execute` wraps dispatch for timeout or retry, `tools/post-execute` inspects or replaces the result, and `tools/result` observes the frozen final outcome.
|
|
86
|
+
|
|
87
|
+
### Host presentation descriptors
|
|
88
|
+
|
|
89
|
+
A tool can retain pure `presentCall()` and `presentResult()` methods for Host-local consumers. The built-in Web Client does not consume those values. It selects a renderer through `tool.call.toolview` and derives card props from raw call arguments, result content, failure state, and persisted metadata. The [Client-derived presentation decision](../../../.agents/notes/implemented/architecture/2026-08-23-client-derived-tool-presentation.md) owns this transport split.
|
|
90
|
+
|
|
91
|
+
-----
|
|
92
|
+
|
|
93
|
+
<a id="understand-the-implementation"></a>
|
|
94
|
+
## Understand the implementation
|
|
95
|
+
|
|
96
|
+
<details>
|
|
97
|
+
<summary>Implementation internals — click to expand</summary>
|
|
98
|
+
|
|
99
|
+
This section explains how the package realizes the behavior above; the observable contract is covered in [Use this package](#use-this-package).
|
|
100
|
+
|
|
101
|
+
### Design concept
|
|
102
|
+
|
|
103
|
+
The registry holds typed `ToolDefinition`s in scoped layers and projects them onto the model-facing `ToolSchema` set at request time — `output`, `execute`, `finalizeContent`, `timeoutMs`, and presentation callbacks never leak onto the wire. Every call runs a fixed pipeline: `tools/pre-execute` (extensible allow/deny/ask) → registered monotonic guards → `tools/execute` (around-dispatch wrappers) → `tools/post-execute` (inspect/replace, attach context) → definition-owned `finalizeContent` → the observe-only `tools/result` event. Only the `tools/execute` view may replace the required signal, and the registry re-fuses the caller signal before the body.
|
|
104
|
+
|
|
105
|
+
### Source map
|
|
106
|
+
|
|
107
|
+
| File | Role |
|
|
108
|
+
|---|---|
|
|
109
|
+
| [`src/index.ts`](src/index.ts) | Plugin entry: `ToolRuntime` service, config, registry, execution pipeline |
|
|
110
|
+
| [`src/types.ts`](src/types.ts) | `ToolDefinition`, `ToolExecution`, `ToolExecutionResult`, guard and decision types |
|
|
111
|
+
| [`src/schema.ts`](src/schema.ts) | The `defineTool` DSL: `ValueSchemaSpec`, `ParameterSchemaSpec`, `InferValue`, `InferArgs` |
|
|
112
|
+
| [`src/json-schema.ts`](src/json-schema.ts) | The enforced raw JSON Schema subset and validation |
|
|
113
|
+
| [`src/presentation.ts`](src/presentation.ts) | The `card`-tagged UI render intents |
|
|
114
|
+
| [`src/ptc.ts`](src/ptc.ts) | PTC mode: SDK generation, `run_code` dispatch bridge, settlement |
|
|
115
|
+
| [`src/ts-types.ts`](src/ts-types.ts) | TypeScript SDK type rendering |
|
|
116
|
+
| [`src/py-types.ts`](src/py-types.ts) | Python SDK type rendering |
|
|
117
|
+
|
|
118
|
+
### Execution and cancellation
|
|
119
|
+
|
|
120
|
+
Each typed invocation materializes and freezes parsed arguments, assigns an opaque correlation token, and runs policy and dispatch. A pre-execute denial may attach `ToolErrorInfo` beside its model-facing reason; the native and PTC durable projections preserve the structured name, code, and optional user-facing reason without adding that detail to model content. Cancellation is cooperative and quiescent: every tool body receives the caller-owned `exec.signal` and must observe it; cancellation before body invocation is `ABORTED_BEFORE_DISPATCH`, after invocation it replaces only a successful outcome with `ABORTED`. Denials, wrapper failures, tool failures, post-policy failures, and timeout-owned `TOOL_TIMEOUT` remain more specific. Unknown and throwing tools become structured errors (`UNKNOWN_TOOL`), so a call fails without ending the turn.
|
|
121
|
+
|
|
122
|
+
### PTC mode
|
|
123
|
+
|
|
124
|
+
Under `ptc` or `both`, the registry exposes the reserved `run_code` transport plus a deterministic SDK generated in the loaded runtime's language. Each SDK binding captures a frozen ToolSchema and passes it through the scheduler to its execution context. Before policy, a started call records only pairing ids, name, and normalized arguments; its settle event preserves the rendered result and optional structured error. Description and parameters remain transient and never enter Session events or SDK output. Calls are scheduled through a per-run pool that reuses the native concurrency contract. Under `ptc` alone, a model-direct call naming any other visible tool resolves to `UNKNOWN_TOOL` before policy — the announced surface and the callable surface stay the same. Intermediate binding values are execution-local; only the outer `run_code` result has a hard size cap. The [executor-collapse note](../../../.agents/notes/implemented/bug-fix/2026-08-07-ptc-executor-collapse.md) owns the collapse contract.
|
|
125
|
+
|
|
126
|
+
New sub-calls use `<parent>:ptc:<n>` ids. Consumers treat these ids as opaque and correlate events by exact equality; restored historical ids retain their original bytes. The [PTC mode decision](../../../.agents/notes/implemented/feature/2026-06-15-ptc.md) owns durable naming and restoration rules.
|
|
127
|
+
|
|
128
|
+
`run_code` accepts `timeoutMs` when the mounted runtime supports an override; its schema reports the configured default and maximum, the runtime's usage instructions and the Session working directory. The Node default is 120,000 ms with a 600,000 ms cap, including nested tool and approval waits. A wider `sandbox_permissions` mode requires a non-empty `justification` and approval before the program starts. The grant applies to that complete execution; standing Session policy and nested tools retain their own authority. Programs are never replayed automatically: inspect earlier effects before explicitly retrying a denied program.
|
|
129
|
+
|
|
130
|
+
<a id="extension-points"></a>
|
|
131
|
+
### Extension points
|
|
132
|
+
|
|
133
|
+
Tool plugins call `ctx.tools.register()` and their schemas flow into prompt assembly automatically. `tools/pre-execute` is the reorderable allow/deny/ask gate; `ctx.tools.guard()` adds monotonic owner policy after it; `tools/execute` wraps normalized canonical dispatch for timeout, retry, or metrics; `tools/post-execute` may replace content or value, block with feedback, or attach ordered contexts; `tools/result` observes the immutable final outcome. MCP servers discover tools and register them with the server's schemas.
|
|
134
|
+
|
|
135
|
+
</details>
|
|
136
|
+
|
|
137
|
+
-----
|
|
138
|
+
|
|
139
|
+
<a id="further-exploration"></a>
|
|
140
|
+
## Further Exploration
|
|
141
|
+
|
|
142
|
+
The package-level contract is enough for most consumers; read these when you need the surrounding domain.
|
|
143
|
+
|
|
144
|
+
- [Tools subsystem](../../../docs/subsystems/tools.md) — the full pipeline types, schema DSL, and generated service API.
|
|
145
|
+
- [Generated tool catalog](../../../docs/tool-catalog.md#qilin-agenttools) — the shipped tools' schemas the model receives.
|
|
146
|
+
- [Tool execution pipeline](../../../docs/tool-execution-pipeline.md) — the pipeline visualized.
|
|
147
|
+
- [Adding a tool cookbook](../../../docs/cookbook/adding-a-tool.md) — step-by-step tool authoring.
|
|
148
|
+
- [Cooperative cancellation Agent Note](../../../.agents/notes/implemented/architecture/2026-07-19-cooperative-tool-cancellation.md) — the full cancellation contract.
|
|
149
|
+
- [Core group map](../README.md) — how the core packages compose.
|
|
150
|
+
|
|
151
|
+
-----
|
|
152
|
+
|
|
153
|
+
<a id="model-experience"></a>
|
|
154
|
+
## Model Experience
|
|
155
|
+
|
|
156
|
+
### Normal tool schemas
|
|
157
|
+
|
|
158
|
+
#### What the model sees
|
|
159
|
+
|
|
160
|
+
In normal mode the model sees each visible definition's exact name, description, and JSON Schema; the shipped definitions are recorded in the generated [tool catalog](../../../docs/tool-catalog.md#qilin-agenttools). Agent-scoped restrictions, shadows, and extension registrations change that agent's end-tool set.
|
|
161
|
+
|
|
162
|
+
#### Token effect
|
|
163
|
+
|
|
164
|
+
Fixed per-request cost proportional to the visible definitions. Restrictions that hide tools remove their entire schema cost for that agent.
|
|
165
|
+
|
|
166
|
+
#### KV Cache effect
|
|
167
|
+
|
|
168
|
+
Prefix-stable while visible definitions and their order are unchanged. Registration, disposal, or scoped restriction may invalidate reuse from the first changed schema token.
|
|
169
|
+
|
|
170
|
+
### PTC mode schema and system prompt
|
|
171
|
+
|
|
172
|
+
#### What the model sees
|
|
173
|
+
|
|
174
|
+
PTC mode exposes the generated [`run_code` schema](../../../docs/tool-catalog.md#qilin-agenttools), the SDK instructions below, and the generated exact SDK block for the loaded runtime's language. The TypeScript instructions identify generated declarations as program-only bindings. When the current `bash` parameter schema accepts the example arguments, they also show a complete `run_code` call around `tools.bash(...)`. The `tools:sdk` section uses first-party order 5000 and disables prompt-variable interpolation, preserving literal `{{…}}` text in tool descriptions and schemas for both runtime languages. `both` exposes normal schemas and this PTC mode API; under `ptc` the prompt also carries the `tools:ptc-only` rule earlier in the first-party order, so the model reads which tools it may call before it reads what each one is for.
|
|
175
|
+
|
|
176
|
+
##### TypeScript PTC mode SDK instructions with bash
|
|
177
|
+
|
|
178
|
+
```markdown
|
|
179
|
+
## Writing code for run_code
|
|
180
|
+
|
|
181
|
+
`run_code` takes two required arguments: `code` — the body of an async TypeScript function (erasable syntax only — no `enum` or namespaces; type annotations are advisory, the code runs type-stripped) — and `description`, a short summary of what the program does. The declarations below are SDK bindings for this program. A declaration does not make its name a directly callable tool; only names supplied as separate tool schemas may be called directly. When no separate `bash` schema is supplied, invoke a declared `bash` binding inside `run_code`:
|
|
182
|
+
|
|
183
|
+
`run_code({ code: "return await tools.bash({ command: 'pwd', description: 'Show current directory' })", description: "Show current directory" })`
|
|
184
|
+
|
|
185
|
+
Inside the program:
|
|
186
|
+
|
|
187
|
+
- Call tools as `await tools.name(args)` — quoted access for exotic names: `tools["my-tool"](args)`. Every call resolves to the tool's typed canonical JSON value. Tool arguments must be lossless JSON.
|
|
188
|
+
- A FAILED tool call rejects with `ToolCallError`, whose `toolName` identifies the failed tool and whose `message` is human-readable — `try/catch` it to handle and continue.
|
|
189
|
+
- Independent read-only calls MAY overlap under `Promise.all` (safe calls run concurrently; mutating calls run alone, in submission order). Sequence dependent work with `await`.
|
|
190
|
+
- Emit results with `return` and/or `console.log(...)`. Only what you print or return is program output. A successful tool result containing an image is attached after the run so you can inspect it on the next step; every other intermediate result stays out of the conversation, so extract just what you need.
|
|
191
|
+
|
|
192
|
+
Program-only SDK bindings:
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
#### Token effect
|
|
196
|
+
|
|
197
|
+
Fixed per-request cost proportional to the visible definitions. PTC mode trades end-tool schemas for generated SDK text plus one transport schema rather than promising a universal reduction.
|
|
198
|
+
|
|
199
|
+
#### KV Cache effect
|
|
200
|
+
|
|
201
|
+
Prefix-stable while the PTC mode selection, generated SDK, transport schema, and visible tool set are unchanged. Mode or filter changes may invalidate reuse from the first changed prompt or schema token.
|
|
202
|
+
|
|
203
|
+
### Tool-call history and results
|
|
204
|
+
|
|
205
|
+
#### What the model sees
|
|
206
|
+
|
|
207
|
+
The loop retains model-emitted arguments and the registry's final content. Any thrown or denied call becomes exactly `Error: <message>`; structured user-facing failure detail is not added to that message. PTC mode renders the outer program's printed lines and return value, `(run_code completed with no output)` when both are empty, or `Error: code run failed (<kind>): <message>` followed conditionally by `Captured output:` and the captured lines. Inner dispatch events stay log-only, while a successful image-bearing sub-result is appended after the outer result as source-attributed context.
|
|
208
|
+
|
|
209
|
+
#### Token effect
|
|
210
|
+
|
|
211
|
+
Arguments, results, and additional context are data-dependent and resent until compaction. Restrictions that hide tools also remove their schemas before the model can call them.
|
|
212
|
+
|
|
213
|
+
#### KV Cache effect
|
|
214
|
+
|
|
215
|
+
Append-only; newly visible content follows the reusable request prefix and does not invalidate existing KV Cache entries.
|
|
216
|
+
|
|
217
|
+
## Known Limitations and Deferred Work
|
|
218
|
+
|
|
219
|
+
<a id="known-limitations-and-deferred-work"></a>
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
These limits define when the registry needs special care. They are current package constraints, not a task backlog.
|
|
223
|
+
|
|
224
|
+
- **Concurrency policy is not an event gate** — `executionMode()` reads the resolved tool definition directly; plugins can only declare a classifier on definitions they own.
|
|
225
|
+
- **`tools/pre-execute` deliberately cannot rewrite `exec.arguments`** — logged and rendered args would desync from what ran; the rewrite design is [a proposed Agent Note](../../../.agents/notes/proposed/feature/2026-06-30-pre-tool-input-rewrite.md).
|
|
226
|
+
- **Caller-defined subagent and workflow structured outputs remain object-rooted** — this is a consumer-level guard; the shared schema vocabulary and tool outputs support every JSON root.
|
|
227
|
+
- **`timeoutMs` on a definition is declarative only** — the registry never enforces deadlines; enforcement requires the `@qilin-agent/tool-call-timeout-policy` wrapper.
|
|
228
|
+
- **PTC mode's SDK language follows the one loaded runtime, and a presentation is per agent rather than per tool** — `mode: ptc`/`both` rejects prompt assembly unless `ctx.ptcRuntime.language` has a registered SDK renderer; within one agent no tool can be native-only while another is ptc-only.
|
|
229
|
+
- **PTC mode intermediate values are execution-local and unbounded by bytes** — they cannot be reconstructed from session replay and may exhaust process or worker memory; only the outer `run_code` output has the worker's configurable hard cap.
|
|
230
|
+
- **`run_code` state is fresh per run** — a persistent REPL-style kernel is rejected for the MVP, because cross-call state would be invisible to the log.
|
|
231
|
+
|
|
232
|
+
<a id="dev-note"></a>
|
|
233
|
+
### Dev Note
|
|
234
|
+
|
|
235
|
+
<details>
|
|
236
|
+
<summary>Working context for maintainers — click to expand</summary>
|
|
237
|
+
|
|
238
|
+
None.
|
|
239
|
+
|
|
240
|
+
</details>
|
package/README.zh.md
ADDED
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
---
|
|
2
|
+
description: "面向工具作者与维护者的工具注册表与执行流水线说明,用于注册、限制、呈现或调试面向模型的工具。"
|
|
3
|
+
kind: "package-reference"
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# @qilin-agent/tools
|
|
7
|
+
|
|
8
|
+
[English](README.md) | 中文
|
|
9
|
+
|
|
10
|
+
## 概述
|
|
11
|
+
|
|
12
|
+
使用 `qilin-tools` 可向模型公开类型化能力、校验调用、执行允许/拒绝/询问策略,并在普通工具失败时返回最终结果而不中止当前轮次。通过 `mode` 选择原生 Function Calling(函数调用)、[PTC mode](#ptc-mode) 或两者;单个 agent(智能体)可用 `presentAs` 覆盖默认值。工具作者使用 `defineTool` 声明类型化参数与输出、协作式超时、并行安全属性和可选 UI 展示。模型会看到每个获准工具声明的名称、描述与参数 schema;按 agent 设置的限制可缩小该可见集合。
|
|
13
|
+
|
|
14
|
+
## 目录
|
|
15
|
+
|
|
16
|
+
- [使用本包](#use-this-package)
|
|
17
|
+
- [理解实现](#understand-the-implementation)
|
|
18
|
+
- [进一步探索](#further-exploration)
|
|
19
|
+
- [模型体验](#model-experience)
|
|
20
|
+
- [已知限制与延期工作](#known-limitations-and-deferred-work)
|
|
21
|
+
- [开发备注](#dev-note)
|
|
22
|
+
|
|
23
|
+
-----
|
|
24
|
+
|
|
25
|
+
<a id="use-this-package"></a>
|
|
26
|
+
## 使用本包
|
|
27
|
+
|
|
28
|
+
在任何 agent 调用工具的地方挂载 `qilin-tools`:它提供 `ctx.tools`,即每个工具插件注册进去、循环分发所经过的注册表。注册一个工具就足以让它可见——注册表会自动把其 schema 送入系统提示词组装。
|
|
29
|
+
|
|
30
|
+
### 注册工具
|
|
31
|
+
|
|
32
|
+
`defineTool` 构建类型化工具定义:面向模型的名称、描述与参数 schema、规范输出声明,以及只返回所声明 JSON 值的 `execute` 主体。模型参数在执行前被校验;无效输入变成普通错误结果。
|
|
33
|
+
|
|
34
|
+
```ts
|
|
35
|
+
import { readFile } from 'node:fs/promises'
|
|
36
|
+
import type { Context } from '@qilin-agent/kylin'
|
|
37
|
+
import { defineTool } from '@qilin-agent/tools'
|
|
38
|
+
|
|
39
|
+
declare const ctx: Context
|
|
40
|
+
|
|
41
|
+
ctx.tools.register(defineTool({
|
|
42
|
+
name: 'read_file',
|
|
43
|
+
description: 'Read a file from disk.',
|
|
44
|
+
parameters: {
|
|
45
|
+
path: { type: 'string', required: true, description: 'Absolute file path' },
|
|
46
|
+
offset: { type: 'number' },
|
|
47
|
+
limit: { type: 'number' },
|
|
48
|
+
},
|
|
49
|
+
output: {
|
|
50
|
+
schema: { type: 'string' },
|
|
51
|
+
render: (_args, value) => [{ type: 'text', text: value }],
|
|
52
|
+
},
|
|
53
|
+
async execute(args, exec) {
|
|
54
|
+
// args is typed: { path: string; offset?: number; limit?: number }
|
|
55
|
+
return readFile(args.path, { encoding: 'utf8', signal: exec.signal })
|
|
56
|
+
},
|
|
57
|
+
}))
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
统一 schema DSL 支持 `string`、`number`、`integer`、`boolean`、`null`、`array`、`object`、仅供作者使用的 `json` 与恰好匹配一个分支的 `oneOf`;`InferValue` 在 16 层容器内保留精确类型,之后加宽为 `JsonValue`。原始 JSON Schema(`JsonSchemaNode`)是与 subagent、工作流和 MCP 共享的协议级对应类型。
|
|
61
|
+
|
|
62
|
+
### 配置呈现模式
|
|
63
|
+
|
|
64
|
+
`mode` 配置决定模型看到什么:`native`(每个可见 schema)、`ptc`(只有 `run_code` 加一份生成 SDK)或 `both`。
|
|
65
|
+
|
|
66
|
+
```yaml
|
|
67
|
+
- name: '@qilin-agent/tools'
|
|
68
|
+
config:
|
|
69
|
+
mode: native
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
| 字段 | 默认值 | 含义 |
|
|
73
|
+
|---|---|---|
|
|
74
|
+
| `mode` | `native` | 可见工具向模型呈现的方式:`native`、`ptc` 或 `both` |
|
|
75
|
+
| `maxParallelSubCalls` | `10` | `run_code` 程序重叠子调用的并发上限;`1` 恢复严格串行分发 |
|
|
76
|
+
|
|
77
|
+
生成的[配置目录](../../../docs/config-catalog.zh.md#qilin-agenttools)是每个受支持字段的穷尽式真源。非原生模式要求已组合的 `ctx.ptcRuntime` 且其语言有已注册的 SDK 渲染器;agent preset 通过 [`qilin-agent-tool-presentation`](../agent-tool-presentation/README.zh.md) 自行选择呈现方式,单个 agent 可用 `presentAs(mode)` 遮蔽默认值。
|
|
78
|
+
|
|
79
|
+
### 按 agent 限制工具
|
|
80
|
+
|
|
81
|
+
`ctx.tools.restrict(filter)` 对单个 agent 继承的全局工具应用允许或拒绝掩码;掩码取交集,作用域注册保持可见,限制在 dispose(资源释放)时解除。`ctx.tools.get(name, scope)` 按一个作用域的视角解析工具。使用 Host 本地展示转换器的消费方如需匹配实际执行的定义,会传入发起调用的 agent。`ctx.tools.schemas(scope)` 返回可见 schema(不含 `execute` 函数)。
|
|
82
|
+
|
|
83
|
+
### 对调用实施策略
|
|
84
|
+
|
|
85
|
+
`ctx.tools.guard(guard)` 在可扩展的 `tools/pre-execute` waterfall(瀑布式事件)之后注册单调同步守卫:返回的理由会拒绝调用,后续监听器无法把该拒绝重新变为允许。流水线事件给插件更多控制——`tools/pre-execute` 决定允许/拒绝/询问,`tools/execute` 为超时或重试包装分发,`tools/post-execute` 检查或替换结果,`tools/result` 观测冻结的最终结果。
|
|
86
|
+
|
|
87
|
+
### Host 展示描述
|
|
88
|
+
|
|
89
|
+
工具可以为 Host 本地消费方保留纯函数 `presentCall()` 与 `presentResult()` 方法。内置 Web Client 不消费这些值,而是通过 `tool.call.toolview` 选择 renderer,并从原始调用参数、结果内容、失败状态与持久 metadata 派生 card props。[Client 派生展示决策](../../../.agents/notes/implemented/architecture/2026-08-23-client-derived-tool-presentation.zh.md)负责该 transport 拆分。
|
|
90
|
+
|
|
91
|
+
-----
|
|
92
|
+
|
|
93
|
+
<a id="understand-the-implementation"></a>
|
|
94
|
+
## 理解实现
|
|
95
|
+
|
|
96
|
+
<details>
|
|
97
|
+
<summary>实现细节——点击展开</summary>
|
|
98
|
+
|
|
99
|
+
本节解释该包如何实现上述行为;可观察约定已在[使用本包](#use-this-package)中完整说明。
|
|
100
|
+
|
|
101
|
+
### 设计理念
|
|
102
|
+
|
|
103
|
+
注册表在作用域层中持有类型化 `ToolDefinition`,并在请求时把它们投影为面向模型的 `ToolSchema` 集合——`output`、`execute`、`finalizeContent`、`timeoutMs` 与呈现回调绝不会泄漏到协议上。每次调用都运行一条固定流水线:`tools/pre-execute`(可扩展的允许/拒绝/询问)→ 已注册单调守卫 → `tools/execute`(环绕分发包装层)→ `tools/post-execute`(检查/替换、附加上下文)→ 由定义持有的 `finalizeContent` → 仅观测的 `tools/result` 事件。只有 `tools/execute` 视图可以替换必填信号,注册表会在调用主体前重新融合调用方信号。
|
|
104
|
+
|
|
105
|
+
### 源码地图
|
|
106
|
+
|
|
107
|
+
| 文件 | 职责 |
|
|
108
|
+
|---|---|
|
|
109
|
+
| [`src/index.ts`](src/index.ts) | 插件入口:`ToolRuntime` 服务、配置、注册表、执行流水线 |
|
|
110
|
+
| [`src/types.ts`](src/types.ts) | `ToolDefinition`、`ToolExecution`、`ToolExecutionResult`、守卫与决策类型 |
|
|
111
|
+
| [`src/schema.ts`](src/schema.ts) | `defineTool` DSL:`ValueSchemaSpec`、`ParameterSchemaSpec`、`InferValue`、`InferArgs` |
|
|
112
|
+
| [`src/json-schema.ts`](src/json-schema.ts) | 强制执行的原始 JSON Schema 子集与校验 |
|
|
113
|
+
| [`src/presentation.ts`](src/presentation.ts) | 带 `card` 标签的 UI 呈现意图 |
|
|
114
|
+
| [`src/ptc.ts`](src/ptc.ts) | PTC mode:SDK 生成、`run_code` 分发桥接层、结算 |
|
|
115
|
+
| [`src/ts-types.ts`](src/ts-types.ts) | TypeScript SDK 类型渲染 |
|
|
116
|
+
| [`src/py-types.ts`](src/py-types.ts) | Python SDK 类型渲染 |
|
|
117
|
+
|
|
118
|
+
### 执行与取消
|
|
119
|
+
|
|
120
|
+
每次类型化调用都会实体化并冻结解析后的参数、分配不透明关联 token,再运行策略与分发。pre-execute 拒绝可以在模型可见原因旁附带 `ToolErrorInfo`;原生与 PTC 持久投影会保留结构化名称、代码与可选用户可见原因,但不会把该详情加入模型内容。取消采用协作式并等待完全停稳:每个工具主体都收到调用方拥有的 `exec.signal` 且必须观测它;调用主体前的取消为 `ABORTED_BEFORE_DISPATCH`,调用主体后的取消只能把成功结果替换为 `ABORTED`。拒绝、包装层失败、工具失败、后置策略失败与超时产生的 `TOOL_TIMEOUT` 仍保留更具体的结果。未知工具与抛出异常的工具都会变成结构化错误(`UNKNOWN_TOOL`),因此调用会失败而不会结束轮次。
|
|
121
|
+
|
|
122
|
+
### PTC mode
|
|
123
|
+
|
|
124
|
+
在 `ptc` 或 `both` 下,注册表公开保留的 `run_code` 传输以及按所加载运行时语言生成的确定性 SDK。每个 SDK 绑定捕获冻结的 ToolSchema,经由调度器传入该次执行上下文。已开始的调用在策略之前只记录配对 id、名称和规范化参数;其结算事件保留渲染结果与可选结构化错误。描述与参数 schema 仅临时存活,不进入 Session 事件或 SDK 输出。调用通过复用原生并发约定的每次运行独有池调度。在纯 `ptc` 下,模型直呼其他任何可见工具都会在策略之前解析为 `UNKNOWN_TOOL`——通告面与可调用面保持一致。中间绑定值只存在于执行局部;只有外层 `run_code` 结果有硬大小上限。[执行器塌缩 note](../../../.agents/notes/implemented/bug-fix/2026-08-07-ptc-executor-collapse.zh.md) 拥有该收束约定。
|
|
125
|
+
|
|
126
|
+
新子调用使用 `<parent>:ptc:<n>` 标识。消费方将这些标识视为不透明值,并通过精确相等关联事件;恢复的历史标识保留原始字节。[PTC mode 决策](../../../.agents/notes/implemented/feature/2026-06-15-ptc.zh.md) 负责持久化命名与恢复规则。
|
|
127
|
+
|
|
128
|
+
当已挂载运行时支持覆盖时,`run_code` 接受 `timeoutMs`;其 schema 报告配置的默认值和上限、运行时使用说明及 Session 工作目录。Node 默认值为 120,000 ms,上限为 600,000 ms,包含嵌套工具和审批等待。更宽的 `sandbox_permissions` 模式要求非空 `justification`,并在程序启动前获得审批。授权仅用于该次完整执行;常驻 Session 策略与嵌套工具保留各自权限。程序不会自动重放:显式重试被拒程序前,应检查先前已发生的效果。
|
|
129
|
+
|
|
130
|
+
<a id="extension-points"></a>
|
|
131
|
+
### 扩展点
|
|
132
|
+
|
|
133
|
+
工具插件调用 `ctx.tools.register()`,其 schema 会自动流入提示词组装。`tools/pre-execute` 是可重排的允许/拒绝/询问门禁;`ctx.tools.guard()` 在其后添加单调的拥有方策略;`tools/execute` 为超时、重试或指标包装规范化后的规范分发;`tools/post-execute` 可以替换内容或值、通过反馈阻止,或附加有序上下文;`tools/result` 观测不可变的最终结果。MCP 服务器发现工具后,用服务器的 schema 调用 `ctx.tools.register()`。
|
|
134
|
+
|
|
135
|
+
</details>
|
|
136
|
+
|
|
137
|
+
-----
|
|
138
|
+
|
|
139
|
+
<a id="further-exploration"></a>
|
|
140
|
+
## 进一步探索
|
|
141
|
+
|
|
142
|
+
包级约定对大多数消费方已经足够;需要周边领域时再阅读以下页面。
|
|
143
|
+
|
|
144
|
+
- [工具子系统](../../../docs/subsystems/tools.zh.md)——完整流水线类型、schema DSL 与生成的服务 API。
|
|
145
|
+
- [生成工具目录](../../../docs/tool-catalog.zh.md#qilin-agenttools)——模型收到的已交付工具 schema。
|
|
146
|
+
- [工具执行流水线](../../../docs/tool-execution-pipeline.zh.md)——可视化流水线。
|
|
147
|
+
- [添加工具实操手册](../../../docs/cookbook/adding-a-tool.zh.md)——分步骤的工具编写指南。
|
|
148
|
+
- [协作式取消 Agent Note](../../../.agents/notes/implemented/architecture/2026-07-19-cooperative-tool-cancellation.zh.md)——完整取消约定。
|
|
149
|
+
- [core 分组地图](../README.zh.md)——core 各包如何组合。
|
|
150
|
+
|
|
151
|
+
-----
|
|
152
|
+
|
|
153
|
+
<a id="model-experience"></a>
|
|
154
|
+
## 模型体验
|
|
155
|
+
|
|
156
|
+
### 普通工具 schema
|
|
157
|
+
|
|
158
|
+
#### 模型看到什么
|
|
159
|
+
|
|
160
|
+
在普通模式下,模型会看到每个可见定义的确切名称、描述与 JSON Schema;已交付定义记录在生成的[工具目录](../../../docs/tool-catalog.zh.md#qilin-agenttools)中。agent 作用域的限制、遮蔽与扩展注册会改变该 agent 的最终工具集合。
|
|
161
|
+
|
|
162
|
+
#### Token 影响
|
|
163
|
+
|
|
164
|
+
每次请求的固定成本与可见定义成正比。隐藏工具的限制会为该 agent 移除其全部 schema 成本。
|
|
165
|
+
|
|
166
|
+
#### KV Cache 影响
|
|
167
|
+
|
|
168
|
+
只要可见定义及其顺序不变,前缀就保持稳定。注册、dispose 或作用域限制可能从第一个改变的 schema token 起使复用失效。
|
|
169
|
+
|
|
170
|
+
### PTC mode schema 与系统提示词
|
|
171
|
+
|
|
172
|
+
#### 模型看到什么
|
|
173
|
+
|
|
174
|
+
PTC mode 会公开生成的 [`run_code` schema](../../../docs/tool-catalog.zh.md#qilin-agenttools)、下方 SDK 说明,以及按所加载运行时语言生成的精确 SDK 块。TypeScript 说明会把生成声明明确标为只能在程序内使用的绑定。当当前 `bash` 参数 schema 接受示例参数时,说明还会给出以 `run_code` 包住 `tools.bash(...)` 的完整调用。`tools:sdk` 段使用 first-party 顺序 5000,并关闭提示词变量插值,使两种运行时语言都原样保留工具描述和 schema 中的 `{{…}}` 文本。`both` 会同时公开普通 schema 与此 PTC mode API;在 `ptc` 下,提示词还会带上处于更早 first-party 顺序的 `tools:ptc-only` 规则,让模型先读到「可以调用哪些工具」再读「每个工具做什么」。
|
|
175
|
+
|
|
176
|
+
##### 带 bash 的 TypeScript PTC mode SDK 说明
|
|
177
|
+
|
|
178
|
+
```markdown
|
|
179
|
+
## Writing code for run_code
|
|
180
|
+
|
|
181
|
+
`run_code` takes two required arguments: `code` — the body of an async TypeScript function (erasable syntax only — no `enum` or namespaces; type annotations are advisory, the code runs type-stripped) — and `description`, a short summary of what the program does. The declarations below are SDK bindings for this program. A declaration does not make its name a directly callable tool; only names supplied as separate tool schemas may be called directly. When no separate `bash` schema is supplied, invoke a declared `bash` binding inside `run_code`:
|
|
182
|
+
|
|
183
|
+
`run_code({ code: "return await tools.bash({ command: 'pwd', description: 'Show current directory' })", description: "Show current directory" })`
|
|
184
|
+
|
|
185
|
+
Inside the program:
|
|
186
|
+
|
|
187
|
+
- Call tools as `await tools.name(args)` — quoted access for exotic names: `tools["my-tool"](args)`. Every call resolves to the tool's typed canonical JSON value. Tool arguments must be lossless JSON.
|
|
188
|
+
- A FAILED tool call rejects with `ToolCallError`, whose `toolName` identifies the failed tool and whose `message` is human-readable — `try/catch` it to handle and continue.
|
|
189
|
+
- Independent read-only calls MAY overlap under `Promise.all` (safe calls run concurrently; mutating calls run alone, in submission order). Sequence dependent work with `await`.
|
|
190
|
+
- Emit results with `return` and/or `console.log(...)`. Only what you print or return is program output. A successful tool result containing an image is attached after the run so you can inspect it on the next step; every other intermediate result stays out of the conversation, so extract just what you need.
|
|
191
|
+
|
|
192
|
+
Program-only SDK bindings:
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
#### Token 影响
|
|
196
|
+
|
|
197
|
+
每次请求的固定成本与可见定义成正比。PTC mode 使用生成的 SDK 文本加一个传输 schema 取代最终工具 schema,但不承诺普遍减少成本。
|
|
198
|
+
|
|
199
|
+
#### KV Cache 影响
|
|
200
|
+
|
|
201
|
+
只要 PTC mode 选择、生成的 SDK、传输 schema 与可见工具集合不变,前缀就保持稳定。模式或筛选器变更可能从第一个改变的提示词或 schema token 起使复用失效。
|
|
202
|
+
|
|
203
|
+
### 工具调用历史与结果
|
|
204
|
+
|
|
205
|
+
#### 模型看到什么
|
|
206
|
+
|
|
207
|
+
循环会保留模型发出的参数与注册表的最终内容。任何抛出异常或遭到拒绝的调用,都会转换为确切的 `Error: <message>`;结构化的用户可见失败详情不会加入该消息。PTC mode 只返回外层程序打印的行与呈现后的返回值;两者都为空时返回 `(run_code completed with no output)`;失败时返回 `Error: code run failed (<kind>): <message>`,并根据是否存在已捕获内容,在其后附加 `Captured output:` 与捕获的行。内部分发事件只保留在日志中;成功且含图片的子结果会在外层结果之后作为带来源归属的上下文追加。
|
|
208
|
+
|
|
209
|
+
#### Token 影响
|
|
210
|
+
|
|
211
|
+
参数、结果与附加上下文取决于数据,并会重复发送直至压缩(compaction)。隐藏工具的限制还会在模型可以调用这些工具之前移除其 schema。
|
|
212
|
+
|
|
213
|
+
#### KV Cache 影响
|
|
214
|
+
|
|
215
|
+
仅追加;新的可见内容位于可复用请求前缀之后,不会使现有 KV Cache 条目失效。
|
|
216
|
+
|
|
217
|
+
## 已知限制与延期工作
|
|
218
|
+
|
|
219
|
+
<a id="known-limitations-and-deferred-work"></a>
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
这些限制说明注册表何时需要特别留意。它们是当前包约束,不是任务积压。
|
|
223
|
+
|
|
224
|
+
- **并发策略不是事件门禁**:`executionMode()` 直接读取已解析的工具定义;插件只能在自身拥有的定义上声明分类器。
|
|
225
|
+
- **`tools/pre-execute` 有意不允许改写 `exec.arguments`**:否则日志记录与呈现的参数会与实际运行内容失去同步;改写设计记录在[拟议的 Agent Note](../../../.agents/notes/proposed/feature/2026-06-30-pre-tool-input-rewrite.zh.md)中。
|
|
226
|
+
- **调用方定义的 subagent 与工作流结构化输出仍要求对象根**:这是消费方层面的守卫;共享 schema 词汇与工具输出支持任意 JSON 根。
|
|
227
|
+
- **定义中的 `timeoutMs` 仅作声明之用**:注册表绝不会强制执行截止时间;要强制执行,必须使用 `@qilin-agent/tool-call-timeout-policy` 包装层。
|
|
228
|
+
- **PTC mode 的 SDK 语言由当前加载的运行时决定,且呈现方式按 agent 而非按工具**:`mode: ptc`/`both` 会拒绝组装提示词,除非 `ctx.ptcRuntime.language` 有已注册的 SDK 渲染器;同一个 agent 内不能让一个工具仅使用 Native,而另一个仅使用 PTC。
|
|
229
|
+
- **PTC mode 中间值只存在于执行局部,且没有字节上限**:它们无法从会话回放重建,并可能耗尽进程或 worker 内存;只有外层 `run_code` 输出受 worker 可配置的硬上限约束。
|
|
230
|
+
- **每次运行都会获得全新的 `run_code` 状态**:MVP 不采用持久 REPL 风格内核,因为跨调用状态不会出现在日志中。
|
|
231
|
+
|
|
232
|
+
<a id="dev-note"></a>
|
|
233
|
+
### 开发备注
|
|
234
|
+
|
|
235
|
+
<details>
|
|
236
|
+
<summary>维护者的工作上下文——点击展开</summary>
|
|
237
|
+
|
|
238
|
+
无。
|
|
239
|
+
|
|
240
|
+
</details>
|