@kindgi/tools 0.0.0-bootstrap.0 → 0.1.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/LICENSE +201 -0
- package/README.md +86 -1
- package/dist/define.d.ts +138 -0
- package/dist/define.d.ts.map +1 -0
- package/dist/define.js +318 -0
- package/dist/define.js.map +1 -0
- package/dist/errors.d.ts +113 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +4 -0
- package/dist/errors.js.map +1 -0
- package/dist/http.d.ts +2 -0
- package/dist/http.d.ts.map +1 -0
- package/dist/http.js +213 -0
- package/dist/http.js.map +1 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +14 -0
- package/dist/index.js.map +1 -0
- package/dist/invoke.d.ts +27 -0
- package/dist/invoke.d.ts.map +1 -0
- package/dist/invoke.js +127 -0
- package/dist/invoke.js.map +1 -0
- package/dist/manifest.d.ts +23 -0
- package/dist/manifest.d.ts.map +1 -0
- package/dist/manifest.js +37 -0
- package/dist/manifest.js.map +1 -0
- package/dist/precondition.d.ts +25 -0
- package/dist/precondition.d.ts.map +1 -0
- package/dist/precondition.js +34 -0
- package/dist/precondition.js.map +1 -0
- package/dist/registry.d.ts +87 -0
- package/dist/registry.d.ts.map +1 -0
- package/dist/registry.js +171 -0
- package/dist/registry.js.map +1 -0
- package/dist/spec-registry.d.ts +48 -0
- package/dist/spec-registry.d.ts.map +1 -0
- package/dist/spec-registry.js +37 -0
- package/dist/spec-registry.js.map +1 -0
- package/dist/tool.schema.json +297 -0
- package/dist/types.d.ts +508 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +14 -0
- package/dist/types.js.map +1 -0
- package/package.json +64 -4
- package/src/define.ts +537 -0
- package/src/errors.ts +139 -0
- package/src/http.ts +268 -0
- package/src/index.ts +68 -0
- package/src/invoke.ts +165 -0
- package/src/manifest.ts +40 -0
- package/src/precondition.ts +40 -0
- package/src/registry.ts +262 -0
- package/src/spec-registry.ts +78 -0
- package/src/tool.schema.json +297 -0
- package/src/types.ts +538 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for describing the origin of the Work and
|
|
141
|
+
reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2026 Kindgi Inc.
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,88 @@
|
|
|
1
1
|
# @kindgi/tools
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Author, register, and invoke tools for Kindgi. A tool is a typed, effect-declared unit of work — the same definition runs in-process for one flow, over MCP for another. See `@kindgi/specs/tool.schema.json` for the wire contract.
|
|
4
|
+
|
|
5
|
+
## Schema authoring surfaces
|
|
6
|
+
|
|
7
|
+
Tools declare their `input` and `output` shapes as **either** JSON Schema Draft 2020-12 objects **or** Zod v4 schemas. The wire form is always JSON Schema — Zod authoring is TS-user sugar the framework converts at author time via `z.toJSONSchema()`.
|
|
8
|
+
|
|
9
|
+
### Authoring with JSON Schema
|
|
10
|
+
|
|
11
|
+
The wire form, portable to Python / Go / Rust pack authors publishing over MCP.
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import { defineTool } from '@kindgi/tools';
|
|
15
|
+
|
|
16
|
+
const echo = defineTool({
|
|
17
|
+
id: 'demo.echo' as never,
|
|
18
|
+
description: 'Echo the input as output.',
|
|
19
|
+
input: {
|
|
20
|
+
type: 'object',
|
|
21
|
+
properties: { msg: { type: 'string' } },
|
|
22
|
+
required: ['msg'],
|
|
23
|
+
additionalProperties: false,
|
|
24
|
+
},
|
|
25
|
+
output: {
|
|
26
|
+
type: 'object',
|
|
27
|
+
properties: { msg: { type: 'string' } },
|
|
28
|
+
required: ['msg'],
|
|
29
|
+
},
|
|
30
|
+
handler: async (input) => {
|
|
31
|
+
// JSON Schema authoring: input defaults to `unknown` at the type level.
|
|
32
|
+
// Cast at the boundary — Ajv already validated the shape at invoke time.
|
|
33
|
+
const { msg } = input as { msg: string };
|
|
34
|
+
return { msg };
|
|
35
|
+
},
|
|
36
|
+
});
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
### Authoring with Zod v4
|
|
40
|
+
|
|
41
|
+
TS-first ergonomics. The framework converts to JSON Schema at author time, caches the wire form on `tool.input` / `tool.output`, and preserves the original Zod schemas on `tool.inputZod` / `tool.outputZod` so callers can `z.infer<typeof tool.inputZod>` for static types.
|
|
42
|
+
|
|
43
|
+
```ts
|
|
44
|
+
import { defineTool } from '@kindgi/tools';
|
|
45
|
+
import { z } from 'zod';
|
|
46
|
+
|
|
47
|
+
const InputSchema = z.object({ msg: z.string() });
|
|
48
|
+
const OutputSchema = z.object({ msg: z.string(), length: z.number().int() });
|
|
49
|
+
|
|
50
|
+
const echo = defineTool({
|
|
51
|
+
id: 'demo.echo' as never,
|
|
52
|
+
description: 'Echo the input with a length count (Zod-authored).',
|
|
53
|
+
input: InputSchema,
|
|
54
|
+
output: OutputSchema,
|
|
55
|
+
handler: async (input) => {
|
|
56
|
+
// Zod authoring: handler input type is `{ msg: string }` — no cast needed.
|
|
57
|
+
return { msg: input.msg, length: input.msg.length };
|
|
58
|
+
},
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
// Downstream inference (e.g., a wrapper that types calls by tool id):
|
|
62
|
+
type EchoInput = z.infer<typeof echo.value.inputZod>; // { msg: string }
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
### When to pick which
|
|
66
|
+
|
|
67
|
+
Prefer **JSON Schema** when:
|
|
68
|
+
|
|
69
|
+
- You publish the tool over MCP and expect non-TS consumers.
|
|
70
|
+
- The manifest is machine-generated (adapter code, MCP client discovery).
|
|
71
|
+
- You want to hand-tune JSON Schema keywords (`format`, `pattern`, `contentMediaType`) with maximum fidelity.
|
|
72
|
+
|
|
73
|
+
Prefer **Zod** when:
|
|
74
|
+
|
|
75
|
+
- You author the tool in a TS package and want typed handlers without a duplicate `type` declaration.
|
|
76
|
+
- You already validate other parts of your code with Zod and want a single source of truth.
|
|
77
|
+
- Runtime + compile-time contract come from the same object.
|
|
78
|
+
|
|
79
|
+
Both are supported. Mixing is fine (Zod input, JSON Schema output, or vice versa).
|
|
80
|
+
|
|
81
|
+
## Peer dependency
|
|
82
|
+
|
|
83
|
+
`zod` (>= 4.0.0) is a peer dependency. Workspaces that never author with Zod never install it and never pay any resolution cost. If you pass a Zod schema without `zod` installed, `defineTool` returns a typed `invalid-schema` error via the `Result` — no module-resolution crash.
|
|
84
|
+
|
|
85
|
+
## Related
|
|
86
|
+
|
|
87
|
+
- `packages/schema/SPEC.md` — §"Zod as optional authoring surface" for the primitive layer.
|
|
88
|
+
- `packages/guardrails/README.md` — mirrors this pattern for check registration.
|
package/dist/define.d.ts
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import type { AnySchema, ZodLikeSchema } from '@kindgi/schema';
|
|
2
|
+
import type { Result, ToolId } from '@kindgi/types';
|
|
3
|
+
import type { InvalidSchemaError, InvalidToolDefinitionError, ToolError, UnknownEffectError } from './errors.js';
|
|
4
|
+
import { type ToolSpecSynthesizerOptions } from './spec-registry.js';
|
|
5
|
+
import { type Tool, type ToolContext, type ToolManifest } from './types.js';
|
|
6
|
+
/**
|
|
7
|
+
* Author-time inference from a Zod schema's `_zod.input` — what a caller
|
|
8
|
+
* may send. Non-Zod schemas fall through to `unknown`.
|
|
9
|
+
*
|
|
10
|
+
* A handler's input is typed `InferOutput` of the input schema: it gets
|
|
11
|
+
* the parsed value (defaults filled, transforms applied), not what the
|
|
12
|
+
* caller sent.
|
|
13
|
+
*/
|
|
14
|
+
export type InferInput<T> = T extends {
|
|
15
|
+
readonly _zod: {
|
|
16
|
+
readonly input: infer I;
|
|
17
|
+
};
|
|
18
|
+
} ? I : unknown;
|
|
19
|
+
/** Symmetric — inferred from `_zod.output`. */
|
|
20
|
+
export type InferOutput<T> = T extends {
|
|
21
|
+
readonly _zod: {
|
|
22
|
+
readonly output: infer O;
|
|
23
|
+
};
|
|
24
|
+
} ? O : unknown;
|
|
25
|
+
/**
|
|
26
|
+
* The author-facing spec accepted by `defineTool`. Two generic parameters,
|
|
27
|
+
* one per schema slot, each of which is either a JSON Schema object or a
|
|
28
|
+
* Zod v4 schema. Two more generics carry the handler's actual argument
|
|
29
|
+
* and return types — defaulting to the schema-inferred types, but
|
|
30
|
+
* available for contextual inference from the handler literal: authors
|
|
31
|
+
* who write `handler: async () => ({ x: 1 })` get
|
|
32
|
+
* `TOutput = { x: number }` without declaring it separately.
|
|
33
|
+
*/
|
|
34
|
+
export interface DefineToolSpec<TInSchema extends AnySchema, TOutSchema extends AnySchema, THandlerIn = InferOutput<TInSchema>, THandlerOut = InferOutput<TOutSchema>> extends Omit<ToolManifest, 'input' | 'output'> {
|
|
35
|
+
/**
|
|
36
|
+
* Tool input schema. Accepts either a Zod v4 schema (auto-converted
|
|
37
|
+
* to JSON Schema at author time; preserved on `tool.inputZod` for
|
|
38
|
+
* TS-side type inference) or a JSON Schema Draft 2020-12 object.
|
|
39
|
+
* Handed to the model as the tool's parameter schema so it can
|
|
40
|
+
* generate valid calls.
|
|
41
|
+
*/
|
|
42
|
+
readonly input: TInSchema;
|
|
43
|
+
/**
|
|
44
|
+
* Tool output schema. Same Zod-or-JSON-Schema shape as `input`.
|
|
45
|
+
* `invokeTool` validates the handler's return value against this
|
|
46
|
+
* before surfacing it — an off-schema return fails with `bad-output`.
|
|
47
|
+
*/
|
|
48
|
+
readonly output: TOutSchema;
|
|
49
|
+
/**
|
|
50
|
+
* Imperative handler. Mutually exclusive with `spec` — a tool is
|
|
51
|
+
* either author-written code or a declarative spec, never both.
|
|
52
|
+
* Omit when providing `spec`; the framework synthesizes a handler
|
|
53
|
+
* from the spec's kind via the registered synthesizer.
|
|
54
|
+
*/
|
|
55
|
+
readonly handler?: (input: THandlerIn, ctx: ToolContext) => Promise<THandlerOut>;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* The concrete Tool returned by `defineTool`. When the author passes a
|
|
59
|
+
* Zod schema at the `input` slot, `inputZod` is present and carries the
|
|
60
|
+
* original schema so callers can `z.infer<typeof tool.inputZod>` at the
|
|
61
|
+
* type level. Symmetric for `output`.
|
|
62
|
+
*
|
|
63
|
+
* Wire form (`tool.input` / `tool.output`) is always the converted JSON
|
|
64
|
+
* Schema — non-TS consumers see no change.
|
|
65
|
+
*/
|
|
66
|
+
export type DefinedTool<TInSchema extends AnySchema, TOutSchema extends AnySchema, THandlerIn = InferOutput<TInSchema>, THandlerOut = InferOutput<TOutSchema>> = Tool<THandlerIn, THandlerOut> & (TInSchema extends ZodLikeSchema ? {
|
|
67
|
+
readonly inputZod: TInSchema;
|
|
68
|
+
} : {
|
|
69
|
+
readonly inputZod?: undefined;
|
|
70
|
+
}) & (TOutSchema extends ZodLikeSchema ? {
|
|
71
|
+
readonly outputZod: TOutSchema;
|
|
72
|
+
} : {
|
|
73
|
+
readonly outputZod?: undefined;
|
|
74
|
+
});
|
|
75
|
+
/**
|
|
76
|
+
* Build a validated `Tool` from a user-supplied definition.
|
|
77
|
+
*
|
|
78
|
+
* Two authoring surfaces coexist:
|
|
79
|
+
* 1. JSON Schema Draft 2020-12 objects — the only shape that crosses
|
|
80
|
+
* the wire. Non-TS consumers use this.
|
|
81
|
+
* 2. Zod v4 schemas — TS-user sugar. Framework detects at author time,
|
|
82
|
+
* converts via `z.toJSONSchema()`, caches the JSON Schema wire form
|
|
83
|
+
* on `tool.input` / `tool.output`, preserves the original Zod schema
|
|
84
|
+
* on `tool.inputZod` / `tool.outputZod` for static type inference.
|
|
85
|
+
*
|
|
86
|
+
* Verifies (fail-fast):
|
|
87
|
+
* 1. The manifest projection conforms to `@kindgi/specs/tool.schema.json`.
|
|
88
|
+
* 2. Every declared `effects[].kind` is in the closed `EFFECT_KINDS` set.
|
|
89
|
+
* 3. `input` and `output` are legal JSON Schema Draft 2020-12 documents
|
|
90
|
+
* (after Zod conversion, when applicable).
|
|
91
|
+
*
|
|
92
|
+
* The `handler` is not validated at author time — its input/output are
|
|
93
|
+
* enforced by `invokeTool` at each call. Returning a tool from this function
|
|
94
|
+
* is the runtime's promise that it's structurally safe to invoke.
|
|
95
|
+
*
|
|
96
|
+
* `zod` is a **peer dependency**. Workspaces that never author with Zod
|
|
97
|
+
* never install it and never pay any resolution cost. Callers that DO
|
|
98
|
+
* pass a Zod schema without `zod` installed get a clean
|
|
99
|
+
* `invalid-schema` error surfaced through the returned `Result`.
|
|
100
|
+
*/
|
|
101
|
+
export declare function defineTool<const TInSchema extends AnySchema, const TOutSchema extends AnySchema, THandlerIn = InferOutput<TInSchema>, THandlerOut = InferOutput<TOutSchema>>(spec: DefineToolSpec<TInSchema, TOutSchema, THandlerIn, THandlerOut>, options?: DefineToolOptions): Result<DefinedTool<TInSchema, TOutSchema, THandlerIn, THandlerOut>, ToolError>;
|
|
102
|
+
/**
|
|
103
|
+
* Overload with explicit data-type generics, for JSON-Schema-only
|
|
104
|
+
* authors who want to state `<TInput, TOutput>` themselves. Without the
|
|
105
|
+
* generics, inference derives the handler types from a Zod schema, or
|
|
106
|
+
* (for JSON Schema) from the handler literal's return type.
|
|
107
|
+
*/
|
|
108
|
+
export declare function defineTool<TInput = unknown, TOutput = unknown>(spec: Tool<TInput, TOutput>, options?: DefineToolOptions): Result<Tool<TInput, TOutput>, ToolError>;
|
|
109
|
+
/** How `defineTool` builds a tool; for the runtime, not for pack authors. */
|
|
110
|
+
export interface DefineToolOptions {
|
|
111
|
+
/** What a declarative `spec`'s synthesized handler gets (the runtime's guarded `fetch`). */
|
|
112
|
+
readonly synthesizer?: ToolSpecSynthesizerOptions;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Async companion to `defineTool` — resolves the Zod peer dependency
|
|
116
|
+
* via dynamic `import()` instead of Node's `createRequire`. Prefer this
|
|
117
|
+
* in environments that prohibit `createRequire` (edge runtimes, some
|
|
118
|
+
* bundler configurations) OR for pack-init code that already lives on
|
|
119
|
+
* the async path.
|
|
120
|
+
*/
|
|
121
|
+
export declare function defineToolAsync<const TInSchema extends AnySchema, const TOutSchema extends AnySchema, THandlerIn = InferOutput<TInSchema>, THandlerOut = InferOutput<TOutSchema>>(spec: DefineToolSpec<TInSchema, TOutSchema, THandlerIn, THandlerOut>, options?: DefineToolOptions): Promise<Result<DefinedTool<TInSchema, TOutSchema, THandlerIn, THandlerOut>, ToolError>>;
|
|
122
|
+
/**
|
|
123
|
+
* Validate an arbitrary wire manifest against `@kindgi/specs/tool.schema.json`
|
|
124
|
+
* + the same effect / input-schema / output-schema checks `defineTool`
|
|
125
|
+
* runs, but without requiring a runtime `handler`. Used by transport
|
|
126
|
+
* layers (HTTP API, MCP publish) that accept metadata-only tool
|
|
127
|
+
* registrations — the actual handler is bound server-side via the
|
|
128
|
+
* runtime registry.
|
|
129
|
+
*
|
|
130
|
+
* The returned value has the manifest fields only; there is no
|
|
131
|
+
* `handler` (metadata-only shape).
|
|
132
|
+
*/
|
|
133
|
+
export declare function validateToolManifest(manifest: unknown): Result<ToolManifest, InvalidToolDefinitionError | InvalidSchemaError | UnknownEffectError>;
|
|
134
|
+
/** Exported for tests and downstream registry building. */
|
|
135
|
+
export declare function toolIdOf(spec: Pick<ToolManifest, 'id'>): ToolId;
|
|
136
|
+
/** The `$id` of the JSON Schema this loader validates against. */
|
|
137
|
+
export declare const TOOL_SCHEMA_URI_EXPORTED = "https://kindgi.com/schemas/v1/tool.schema.json";
|
|
138
|
+
//# sourceMappingURL=define.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"define.d.ts","sourceRoot":"","sources":["../src/define.ts"],"names":[],"mappings":"AAaA,OAAO,KAAK,EACV,SAAS,EAIT,aAAa,EACd,MAAM,gBAAgB,CAAC;AACxB,OAAO,KA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