@kindgi/tools 0.1.6 → 0.1.7
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/dist/idempotency-key.d.ts +6 -3
- package/dist/idempotency-key.d.ts.map +1 -1
- package/dist/idempotency-key.js +6 -3
- package/dist/idempotency-key.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/invoke-env.d.ts +9 -0
- package/dist/invoke-env.d.ts.map +1 -0
- package/dist/invoke-env.js +88 -0
- package/dist/invoke-env.js.map +1 -0
- package/dist/test-context.d.ts +30 -0
- package/dist/test-context.d.ts.map +1 -0
- package/dist/test-context.js +51 -0
- package/dist/test-context.js.map +1 -0
- package/dist/types.d.ts +4 -2
- package/dist/types.d.ts.map +1 -1
- package/package.json +5 -5
- package/src/idempotency-key.ts +6 -3
- package/src/index.ts +1 -0
- package/src/invoke-env.ts +108 -0
- package/src/test-context.ts +60 -0
- package/src/types.ts +4 -2
|
@@ -9,9 +9,12 @@ export declare const TOOL_IDEMPOTENCY_NAMESPACE = "a70d5bff-e713-4322-b2ea-9d5c4
|
|
|
9
9
|
* failure, a re-run after a crash), different for every other call.
|
|
10
10
|
*
|
|
11
11
|
* It's a name-based UUID (RFC 9562 version 5, SHA-1) under
|
|
12
|
-
* `TOOL_IDEMPOTENCY_NAMESPACE`, over the UTF-8 bytes of the
|
|
13
|
-
*
|
|
14
|
-
*
|
|
12
|
+
* `TOOL_IDEMPOTENCY_NAMESPACE`, over the UTF-8 bytes of the JSON array
|
|
13
|
+
* `[runId, stepScope, toolId]` (with `callId` appended for a call a model
|
|
14
|
+
* asked for) as `JSON.stringify` writes it: no whitespace, and characters
|
|
15
|
+
* beyond ASCII as themselves, never as `\u` escapes. Python's `json.dumps`
|
|
16
|
+
* escapes them unless `ensure_ascii=False`, and an escaped part is another
|
|
17
|
+
* key. `stepScope` is the step's own (`NodeContext.stepScope`):
|
|
15
18
|
* it tells two loop iterations or two fanout branches apart, which a
|
|
16
19
|
* model's call id alone doesn't (it's only unique within one answer).
|
|
17
20
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"idempotency-key.d.ts","sourceRoot":"","sources":["../src/idempotency-key.ts"],"names":[],"mappings":"AAKA;;;GAGG;AACH,eAAO,MAAM,0BAA0B,yCAAyC,CAAC;AAIjF
|
|
1
|
+
{"version":3,"file":"idempotency-key.d.ts","sourceRoot":"","sources":["../src/idempotency-key.ts"],"names":[],"mappings":"AAKA;;;GAGG;AACH,eAAO,MAAM,0BAA0B,yCAAyC,CAAC;AAIjF;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,EAAE;IACxC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,8EAA8E;IAC9E,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;CAC1B,GAAG,MAAM,CAST"}
|
package/dist/idempotency-key.js
CHANGED
|
@@ -13,9 +13,12 @@ const NAMESPACE_BYTES = Buffer.from(TOOL_IDEMPOTENCY_NAMESPACE.replaceAll('-', '
|
|
|
13
13
|
* failure, a re-run after a crash), different for every other call.
|
|
14
14
|
*
|
|
15
15
|
* It's a name-based UUID (RFC 9562 version 5, SHA-1) under
|
|
16
|
-
* `TOOL_IDEMPOTENCY_NAMESPACE`, over the UTF-8 bytes of the
|
|
17
|
-
*
|
|
18
|
-
*
|
|
16
|
+
* `TOOL_IDEMPOTENCY_NAMESPACE`, over the UTF-8 bytes of the JSON array
|
|
17
|
+
* `[runId, stepScope, toolId]` (with `callId` appended for a call a model
|
|
18
|
+
* asked for) as `JSON.stringify` writes it: no whitespace, and characters
|
|
19
|
+
* beyond ASCII as themselves, never as `\u` escapes. Python's `json.dumps`
|
|
20
|
+
* escapes them unless `ensure_ascii=False`, and an escaped part is another
|
|
21
|
+
* key. `stepScope` is the step's own (`NodeContext.stepScope`):
|
|
19
22
|
* it tells two loop iterations or two fanout branches apart, which a
|
|
20
23
|
* model's call id alone doesn't (it's only unique within one answer).
|
|
21
24
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"idempotency-key.js","sourceRoot":"","sources":["../src/idempotency-key.ts"],"names":[],"mappings":"AAAA,sCAAsC;AACtC,iCAAiC;AAEjC,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAEzC;;;GAGG;AACH,MAAM,CAAC,MAAM,0BAA0B,GAAG,sCAAsC,CAAC;AAEjF,MAAM,eAAe,GAAG,MAAM,CAAC,IAAI,CAAC,0BAA0B,CAAC,UAAU,CAAC,GAAG,EAAE,EAAE,CAAC,EAAE,KAAK,CAAC,CAAC;AAE3F
|
|
1
|
+
{"version":3,"file":"idempotency-key.js","sourceRoot":"","sources":["../src/idempotency-key.ts"],"names":[],"mappings":"AAAA,sCAAsC;AACtC,iCAAiC;AAEjC,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAEzC;;;GAGG;AACH,MAAM,CAAC,MAAM,0BAA0B,GAAG,sCAAsC,CAAC;AAEjF,MAAM,eAAe,GAAG,MAAM,CAAC,IAAI,CAAC,0BAA0B,CAAC,UAAU,CAAC,GAAG,EAAE,EAAE,CAAC,EAAE,KAAK,CAAC,CAAC;AAE3F;;;;;;;;;;;;;;GAcG;AACH,MAAM,UAAU,kBAAkB,CAAC,KAMlC;IACC,MAAM,KAAK,GAAG,CAAC,KAAK,CAAC,KAAK,EAAE,KAAK,CAAC,SAAS,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC;IAC3D,IAAI,KAAK,CAAC,MAAM,KAAK,SAAS;QAAE,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;IACzD,MAAM,IAAI,GAAG,UAAU,CAAC,MAAM,CAAC,CAAC,MAAM,CAAC,eAAe,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,EAAE,MAAM,CAAC,CAAC;IAC9F,MAAM,KAAK,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC,QAAQ,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;IAC5C,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,GAAG,IAAI,CAAC,GAAG,IAAI,CAAC;IAC3C,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,GAAG,IAAI,CAAC,GAAG,IAAI,CAAC;IAC3C,MAAM,GAAG,GAAG,KAAK,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;IAClC,OAAO,GAAG,GAAG,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,IAAI,GAAG,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,IAAI,GAAG,CAAC,KAAK,CAAC,EAAE,EAAE,EAAE,CAAC,IAAI,GAAG,CAAC,KAAK,CAAC,EAAE,EAAE,EAAE,CAAC,IAAI,GAAG,CAAC,KAAK,CAAC,EAAE,CAAC,EAAE,CAAC;AAC7G,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -4,6 +4,7 @@ import './http.js';
|
|
|
4
4
|
export { registerToolSpecSynthesizer, getToolSpecSynthesizer } from './spec-registry.js';
|
|
5
5
|
export type { ToolSpecSynthesizer, ToolSpecSynthesizerOptions } from './spec-registry.js';
|
|
6
6
|
export { invokeTool } from './invoke.js';
|
|
7
|
+
export { invokeToolForTest, toolContextForTest } from './test-context.js';
|
|
7
8
|
export { toolSecretNames } from './secret-names.js';
|
|
8
9
|
export { ToolPreconditionError, isToolPreconditionError } from './precondition.js';
|
|
9
10
|
export { TOOL_ENV_RECORD_KEY, toolCallRecordKey } from './record-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O,EAAE,0BAA0B,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AACtF,YAAY,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AACrD,OAAO,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAC;AACnD,YAAY,EAAE,mBAAmB,EAAE,YAAY,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AACvF,OAAO,EAAE,aAAa,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AACxE,YAAY,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AACjD,OAAO,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAC1D,OAAO,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAC1C,YAAY,EACV,OAAO,EACP,eAAe,EACf,MAAM,EACN,UAAU,EACV,YAAY,EACZ,cAAc,EACd,UAAU,EACV,mBAAmB,EACnB,YAAY,EACZ,UAAU,EACV,eAAe,EACf,IAAI,EACJ,aAAa,EACb,aAAa,EACb,WAAW,EACX,IAAI,EACJ,WAAW,EACX,YAAY,EACZ,aAAa,EACb,QAAQ,EACR,UAAU,GACX,MAAM,YAAY,CAAC;AACpB,YAAY,EACV,kBAAkB,EAClB,yBAAyB,EACzB,YAAY,EACZ,oBAAoB,EACpB,kBAAkB,EAClB,0BAA0B,EAC1B,wBAAwB,EACxB,qBAAqB,EACrB,uBAAuB,EACvB,SAAS,EACT,iBAAiB,EACjB,wBAAwB,EACxB,4BAA4B,EAC5B,kBAAkB,GACnB,MAAM,aAAa,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -7,6 +7,7 @@ export { BUILT_IN_TOOL_PREFIX, defineTool, defineToolAsync, validateToolManifest
|
|
|
7
7
|
import './http.js';
|
|
8
8
|
export { registerToolSpecSynthesizer, getToolSpecSynthesizer } from './spec-registry.js';
|
|
9
9
|
export { invokeTool } from './invoke.js';
|
|
10
|
+
export { invokeToolForTest, toolContextForTest } from './test-context.js';
|
|
10
11
|
export { toolSecretNames } from './secret-names.js';
|
|
11
12
|
export { ToolPreconditionError, isToolPreconditionError } from './precondition.js';
|
|
12
13
|
export { TOOL_ENV_RECORD_KEY, toolCallRecordKey } from './record-keys.js';
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,sCAAsC;AACtC,iCAAiC;AAEjC,OAAO,EACL,oBAAoB,EACpB,UAAU,EACV,eAAe,EACf,oBAAoB,EACpB,wBAAwB,IAAI,eAAe,GAC5C,MAAM,aAAa,CAAC;AAQrB,gEAAgE;AAChE,kEAAkE;AAClE,8DAA8D;AAC9D,OAAO,WAAW,CAAC;AACnB,OAAO,EAAE,2BAA2B,EAAE,sBAAsB,EAAE,MAAM,oBAAoB,CAAC;AAEzF,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AACpD,OAAO,EAAE,qBAAqB,EAAE,uBAAuB,EAAE,MAAM,mBAAmB,CAAC;AACnF,OAAO,EAAE,mBAAmB,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AAC1E,OAAO,EAAE,0BAA0B,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAEtF,OAAO,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAC;AAEnD,OAAO,EAAE,aAAa,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAExE,OAAO,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAC1D,OAAO,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,sCAAsC;AACtC,iCAAiC;AAEjC,OAAO,EACL,oBAAoB,EACpB,UAAU,EACV,eAAe,EACf,oBAAoB,EACpB,wBAAwB,IAAI,eAAe,GAC5C,MAAM,aAAa,CAAC;AAQrB,gEAAgE;AAChE,kEAAkE;AAClE,8DAA8D;AAC9D,OAAO,WAAW,CAAC;AACnB,OAAO,EAAE,2BAA2B,EAAE,sBAAsB,EAAE,MAAM,oBAAoB,CAAC;AAEzF,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,iBAAiB,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AAC1E,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AACpD,OAAO,EAAE,qBAAqB,EAAE,uBAAuB,EAAE,MAAM,mBAAmB,CAAC;AACnF,OAAO,EAAE,mBAAmB,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AAC1E,OAAO,EAAE,0BAA0B,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAEtF,OAAO,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAC;AAEnD,OAAO,EAAE,aAAa,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAExE,OAAO,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAC1D,OAAO,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Result } from '@kindgi/types';
|
|
2
|
+
import { ToolPreconditionError } from './precondition.js';
|
|
3
|
+
import type { Tool } from './types.js';
|
|
4
|
+
/**
|
|
5
|
+
* The env the handler gets: the declared names, each with its value. Undefined when the tool
|
|
6
|
+
* declares none, as from the runtime.
|
|
7
|
+
*/
|
|
8
|
+
export declare function envForCall(tool: Pick<Tool, 'id' | 'needsSpec'>, given: Readonly<Record<string, string>> | undefined): Result<Readonly<Record<string, string>> | undefined, ToolPreconditionError>;
|
|
9
|
+
//# sourceMappingURL=invoke-env.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"invoke-env.d.ts","sourceRoot":"","sources":["../src/invoke-env.ts"],"names":[],"mappings":"AAeA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,eAAe,CAAC;AAE5C,OAAO,EAAE,qBAAqB,EAAE,MAAM,mBAAmB,CAAC;AAC1D,OAAO,KAAK,EAAc,IAAI,EAAE,MAAM,YAAY,CAAC;AAKnD;;;GAGG;AACH,wBAAgB,UAAU,CACxB,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE,IAAI,GAAG,WAAW,CAAC,EACpC,KAAK,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,GAAG,SAAS,GAClD,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,GAAG,SAAS,EAAE,qBAAqB,CAAC,CAmB7E"}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// Copyright (C) 2026 Kindgi Inc.
|
|
3
|
+
/**
|
|
4
|
+
* The env values `invokeToolForTest` hands a tool, decided as the runtime decides a pack tool's
|
|
5
|
+
* (its tool bridge, "Env values"): each name the tool declares in `needsSpec.env` takes the
|
|
6
|
+
* context's `env` value, else its schema's `default`, and each is checked against its schema. A
|
|
7
|
+
* name with neither, or a value its schema refuses, refuses the call before the handler runs
|
|
8
|
+
* (`env-value-missing` first, then `env-value-invalid`). The handler sees the declared names
|
|
9
|
+
* only: the runtime sends no others. `env-declaration-invalid` is this helper's own: the runtime
|
|
10
|
+
* never refuses a call for it, it leaves the tool out when it loads (and `defineTool` refuses
|
|
11
|
+
* the schema up front). Test only: `invokeTool` itself never decides env.
|
|
12
|
+
*/
|
|
13
|
+
import { compileInlineSchema } from '@kindgi/schema';
|
|
14
|
+
import { ToolPreconditionError } from './precondition.js';
|
|
15
|
+
/** Compiled env schemas, by schema object (a tool's are defined once and reused). */
|
|
16
|
+
const compiled = new WeakMap();
|
|
17
|
+
/**
|
|
18
|
+
* The env the handler gets: the declared names, each with its value. Undefined when the tool
|
|
19
|
+
* declares none, as from the runtime.
|
|
20
|
+
*/
|
|
21
|
+
export function envForCall(tool, given) {
|
|
22
|
+
const declared = Object.entries(tool.needsSpec?.env ?? {});
|
|
23
|
+
if (declared.length === 0)
|
|
24
|
+
return { kind: 'ok', value: undefined };
|
|
25
|
+
const id = tool.id;
|
|
26
|
+
const chosen = declared.map(([name, schema]) => ({
|
|
27
|
+
name,
|
|
28
|
+
schema,
|
|
29
|
+
value: given !== undefined && Object.hasOwn(given, name) ? given[name] : defaultOf(schema),
|
|
30
|
+
}));
|
|
31
|
+
// As the runtime: a missing value refuses the call first, then each value is checked.
|
|
32
|
+
const missing = chosen.filter((c) => c.value === undefined).map((c) => c.name);
|
|
33
|
+
if (missing.length > 0)
|
|
34
|
+
return refused('env-value-missing', missingMessage(id, missing));
|
|
35
|
+
const values = {};
|
|
36
|
+
for (const { name, schema, value } of chosen) {
|
|
37
|
+
const problem = checkValue(id, name, schema, value);
|
|
38
|
+
if (problem !== undefined)
|
|
39
|
+
return { kind: 'err', error: problem };
|
|
40
|
+
values[name] = value;
|
|
41
|
+
}
|
|
42
|
+
return { kind: 'ok', value: values };
|
|
43
|
+
}
|
|
44
|
+
function missingMessage(id, missing) {
|
|
45
|
+
const one = missing.length === 1;
|
|
46
|
+
const which = one
|
|
47
|
+
? `env value "${missing[0]}"`
|
|
48
|
+
: `env values ${missing.map((n) => `"${n}"`).join(', ')}`;
|
|
49
|
+
return `tool "${id}" needs ${which}: the context's \`env\` doesn't set ${one ? 'it' : 'them'}, and ${one ? 'its schema has' : 'their schemas have'} no default`;
|
|
50
|
+
}
|
|
51
|
+
/** Why a value doesn't fit its schema (or the schema doesn't compile); undefined when it fits. */
|
|
52
|
+
function checkValue(id, name, schema, value) {
|
|
53
|
+
const validator = validatorFor(schema);
|
|
54
|
+
if (validator.kind === 'err') {
|
|
55
|
+
return new ToolPreconditionError('env-declaration-invalid', `tool "${id}" declares env value "${name}" with a schema that doesn't compile: ${validator.error}`);
|
|
56
|
+
}
|
|
57
|
+
const checked = validator.value.validate(value);
|
|
58
|
+
if (checked.kind === 'ok')
|
|
59
|
+
return undefined;
|
|
60
|
+
return new ToolPreconditionError('env-value-invalid', `env value "${name}" is "${value}", which doesn't match the schema tool "${id}" declares for it: ${constraintsBroken(checked.error.errors)}`);
|
|
61
|
+
}
|
|
62
|
+
/** A schema's `default`, when it's a string (env values are strings; `defineTool` checks it). */
|
|
63
|
+
function defaultOf(schema) {
|
|
64
|
+
const fallback = schema.default;
|
|
65
|
+
return typeof fallback === 'string' ? fallback : undefined;
|
|
66
|
+
}
|
|
67
|
+
function validatorFor(schema) {
|
|
68
|
+
const key = schema;
|
|
69
|
+
const cached = compiled.get(key);
|
|
70
|
+
if (cached !== undefined)
|
|
71
|
+
return { kind: 'ok', value: cached };
|
|
72
|
+
const result = compileInlineSchema(schema);
|
|
73
|
+
if (result.kind === 'err')
|
|
74
|
+
return { kind: 'err', error: result.error.message };
|
|
75
|
+
compiled.set(key, result.value);
|
|
76
|
+
return { kind: 'ok', value: result.value };
|
|
77
|
+
}
|
|
78
|
+
/** What a value broke, in the schema validator's words (the runtime's wording). */
|
|
79
|
+
function constraintsBroken(errors) {
|
|
80
|
+
const messages = errors
|
|
81
|
+
.map((e) => e.message)
|
|
82
|
+
.filter((m) => typeof m === 'string');
|
|
83
|
+
return messages.length > 0 ? messages.join('; ') : 'it does not validate';
|
|
84
|
+
}
|
|
85
|
+
function refused(reason, message) {
|
|
86
|
+
return { kind: 'err', error: new ToolPreconditionError(reason, message) };
|
|
87
|
+
}
|
|
88
|
+
//# sourceMappingURL=invoke-env.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"invoke-env.js","sourceRoot":"","sources":["../src/invoke-env.ts"],"names":[],"mappings":"AAAA,sCAAsC;AACtC,iCAAiC;AAEjC;;;;;;;;;GASG;AAEH,OAAO,EAA6B,mBAAmB,EAAE,M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AE,CAAE,CAAoC,CAAC,OAAO,CAAC;SACzD,MAAM,CAAC,CAAC,CAAC,EAAe,EAAE,CAAC,OAAO,CAAC,KAAK,QAAQ,CAAC,CAAC;IACrD,OAAO,QAAQ,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,sBAAsB,CAAC;AAC5E,CAAC;AAED,SAAS,OAAO,CAAC,MAAc,EAAE,OAAe;IAC9C,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,KAAK,EAAE,IAAI,qBAAqB,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,CAAC;AAC5E,CAAC"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { Result } from '@kindgi/types';
|
|
2
|
+
import type { ToolError } from './errors.js';
|
|
3
|
+
import { type InvokeToolOptions } from './invoke.js';
|
|
4
|
+
import type { Tool, ToolContext } from './types.js';
|
|
5
|
+
/**
|
|
6
|
+
* A context for calling a tool in a unit test: tenant `tenant-test`, run `run-test`, a signal
|
|
7
|
+
* that never fires, and whatever you pass over them (`env`, `secrets`, `projectId`, …). The
|
|
8
|
+
* TypeScript counterpart of Python's `ToolContext.for_test`.
|
|
9
|
+
*/
|
|
10
|
+
export declare function toolContextForTest(overrides?: Partial<ToolContext>): ToolContext;
|
|
11
|
+
/**
|
|
12
|
+
* Call a tool in a unit test as a run would: `invokeTool`, with the tool's env values decided
|
|
13
|
+
* the way the runtime decides them for a pack's tool. Each name it declares in `needsSpec.env`
|
|
14
|
+
* takes the context's `env` value, else its schema's `default`; each is checked against its
|
|
15
|
+
* schema; the handler's `ctx.env` holds the declared names only. A name with neither, or a value
|
|
16
|
+
* its schema refuses, is `precondition-failed` (`env-value-missing`, then `env-value-invalid`)
|
|
17
|
+
* and the handler doesn't run. As in a run, the input is checked first: the env is decided as
|
|
18
|
+
* the handler is called. The context is `toolContextForTest(context)`.
|
|
19
|
+
*
|
|
20
|
+
* Test only: in a run, the runtime decides a pack tool's env itself, from its env store, and
|
|
21
|
+
* calls `invokeTool`.
|
|
22
|
+
*
|
|
23
|
+
* ```ts
|
|
24
|
+
* const result = await invokeToolForTest(findOrder, { orderId: 'ord_1001' }, {
|
|
25
|
+
* env: { ORDERS_BASE_URL: 'https://orders.example.com' },
|
|
26
|
+
* });
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export declare function invokeToolForTest<TInput = unknown, TOutput = unknown>(tool: Tool<TInput, TOutput>, input: unknown, context?: Partial<ToolContext>, options?: InvokeToolOptions): Promise<Result<TOutput, ToolError>>;
|
|
30
|
+
//# sourceMappingURL=test-context.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"test-context.d.ts","sourceRoot":"","sources":["../src/test-context.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,MAAM,EAAY,MAAM,eAAe,CAAC;AAEtD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAE7C,OAAO,EAAE,KAAK,iBAAiB,EAAc,MAAM,aAAa,CAAC;AACjE,OAAO,KAAK,EAAE,IAAI,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAEpD;;;;GAIG;AACH,wBAAgB,kBAAkB,CAAC,SAAS,GAAE,OAAO,CAAC,WAAW,CAAM,GAAG,WAAW,CAOpF;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,iBAAiB,CAAC,MAAM,GAAG,OAAO,EAAE,OAAO,GAAG,OAAO,EACnE,IAAI,EAAE,IAAI,CAAC,MAAM,EAAE,OAAO,CAAC,EAC3B,KAAK,EAAE,OAAO,EACd,OAAO,GAAE,OAAO,CAAC,WAAW,CAAM,EAClC,OAAO,CAAC,EAAE,iBAAiB,GAC1B,OAAO,CAAC,MAAM,CAAC,OAAO,EAAE,SAAS,CAAC,CAAC,CAYrC"}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// Copyright (C) 2026 Kindgi Inc.
|
|
3
|
+
import { envForCall } from './invoke-env.js';
|
|
4
|
+
import { invokeTool } from './invoke.js';
|
|
5
|
+
/**
|
|
6
|
+
* A context for calling a tool in a unit test: tenant `tenant-test`, run `run-test`, a signal
|
|
7
|
+
* that never fires, and whatever you pass over them (`env`, `secrets`, `projectId`, …). The
|
|
8
|
+
* TypeScript counterpart of Python's `ToolContext.for_test`.
|
|
9
|
+
*/
|
|
10
|
+
export function toolContextForTest(overrides = {}) {
|
|
11
|
+
return {
|
|
12
|
+
tenantId: 'tenant-test',
|
|
13
|
+
runId: 'run-test',
|
|
14
|
+
abortSignal: new AbortController().signal,
|
|
15
|
+
...overrides,
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Call a tool in a unit test as a run would: `invokeTool`, with the tool's env values decided
|
|
20
|
+
* the way the runtime decides them for a pack's tool. Each name it declares in `needsSpec.env`
|
|
21
|
+
* takes the context's `env` value, else its schema's `default`; each is checked against its
|
|
22
|
+
* schema; the handler's `ctx.env` holds the declared names only. A name with neither, or a value
|
|
23
|
+
* its schema refuses, is `precondition-failed` (`env-value-missing`, then `env-value-invalid`)
|
|
24
|
+
* and the handler doesn't run. As in a run, the input is checked first: the env is decided as
|
|
25
|
+
* the handler is called. The context is `toolContextForTest(context)`.
|
|
26
|
+
*
|
|
27
|
+
* Test only: in a run, the runtime decides a pack tool's env itself, from its env store, and
|
|
28
|
+
* calls `invokeTool`.
|
|
29
|
+
*
|
|
30
|
+
* ```ts
|
|
31
|
+
* const result = await invokeToolForTest(findOrder, { orderId: 'ord_1001' }, {
|
|
32
|
+
* env: { ORDERS_BASE_URL: 'https://orders.example.com' },
|
|
33
|
+
* });
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
export function invokeToolForTest(tool, input, context = {}, options) {
|
|
37
|
+
const withEnv = {
|
|
38
|
+
...tool,
|
|
39
|
+
handler: async (prepared, ctx) => {
|
|
40
|
+
const env = envForCall(tool, ctx.env);
|
|
41
|
+
if (env.kind === 'err')
|
|
42
|
+
throw env.error;
|
|
43
|
+
if (env.value === undefined && ctx.env === undefined)
|
|
44
|
+
return tool.handler(prepared, ctx);
|
|
45
|
+
const { env: _given, ...rest } = ctx;
|
|
46
|
+
return tool.handler(prepared, env.value === undefined ? rest : { ...rest, env: env.value });
|
|
47
|
+
},
|
|
48
|
+
};
|
|
49
|
+
return invokeTool(withEnv, input, toolContextForTest(context), options);
|
|
50
|
+
}
|
|
51
|
+
//# sourceMappingURL=test-context.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"test-context.js","sourceRoot":"","sources":["../src/test-context.ts"],"names":[],"mappings":"AAAA,sCAAsC;AACtC,iCAAiC;AAKjC,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAC7C,OAAO,EAA0B,UAAU,EAAE,MAAM,aAAa,CAAC;AAGjE;;;;GAIG;AACH,MAAM,UAAU,kBAAkB,CAAC,YAAkC,EAAE;IACrE,OAAO;QACL,QAAQ,EAAE,aAAyB;QACnC,KAAK,EAAE,UAAU;QACjB,WAAW,EAAE,IAAI,eAAe,EAAE,CAAC,MAAM;QACzC,GAAG,SAAS;KACb,CAAC;AACJ,CAAC;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,UAAU,iBAAiB,CAC/B,IAA2B,EAC3B,KAAc,EACd,UAAgC,EAAE,EAClC,OAA2B;IAE3B,MAAM,OAAO,GAA0B;QACrC,GAAG,IAAI;QACP,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAE,GAAG,EAAE,EAAE;YAC/B,MAAM,GAAG,GAAG,UAAU,CAAC,IAAI,EAAE,GAAG,CAAC,GAAG,CAAC,CAAC;YACtC,IAAI,GAAG,CAAC,IAAI,KAAK,KAAK;gBAAE,MAAM,GAAG,CAAC,KAAK,CAAC;YACxC,IAAI,GAAG,CAAC,KAAK,KAAK,SAAS,IAAI,GAAG,CAAC,GAAG,KAAK,SAAS;gBAAE,OAAO,IAAI,CAAC,OAAO,CAAC,QAAQ,EAAE,GAAG,CAAC,CAAC;YACzF,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,GAAG,GAAG,CAAC;YACrC,OAAO,IAAI,CAAC,OAAO,CAAC,QAAQ,EAAE,GAAG,CAAC,KAAK,KAAK,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,GAAG,CAAC,KAAK,EAAE,CAAC,CAAC;QAC9F,CAAC;KACF,CAAC;IACF,OAAO,UAAU,CAAC,OAAO,EAAE,KAAK,EAAE,kBAAkB,CAAC,OAAO,CAAC,EAAE,OAAO,CAAC,CAAC;AAC1E,CAAC"}
|
package/dist/types.d.ts
CHANGED
|
@@ -156,8 +156,10 @@ export interface ToolContext {
|
|
|
156
156
|
* call (`record`), so the call re-run after a wait or a retry sees the
|
|
157
157
|
* same ones. Not for credentials: env values are shown with the run
|
|
158
158
|
* (use `secrets`).
|
|
159
|
-
* Absent when the tool declares none, and from an older runtime
|
|
160
|
-
* unit test
|
|
159
|
+
* Absent when the tool declares none, and from an older runtime.
|
|
160
|
+
* In a unit test, `invokeToolForTest` decides them the same way from
|
|
161
|
+
* the context it's given: its `env`, else each schema's `default`, each
|
|
162
|
+
* checked, the declared names only. `invokeTool` passes `env` as given.
|
|
161
163
|
*/
|
|
162
164
|
readonly env?: Readonly<Record<string, string>>;
|
|
163
165
|
/**
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAC1C,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC;AACpD,OAAO,KAAK,EAAE,KAAK,EAAE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AM,OAAO,GAAG,IAAI,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;AAErC,wEAAwE;AACxE,MAAM,WAAW,eAAe;IAC9B,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;IAC7B,QAAQ,CAAC,WAAW,EAAE,UAAU,CAAC;IACjC,QAAQ,CAAC,YAAY,CAAC,EAAE,UAAU,CAAC;CACpC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kindgi/tools",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.7",
|
|
4
4
|
"description": "Tool authoring shape + registry + invocation 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. Pure — no kernel dependency; the kernel bridges tools to node handlers separately.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"repository": {
|
|
@@ -30,9 +30,9 @@
|
|
|
30
30
|
"type-manifest.json"
|
|
31
31
|
],
|
|
32
32
|
"dependencies": {
|
|
33
|
-
"@kindgi/log": "0.1.
|
|
34
|
-
"@kindgi/schema": "0.1.
|
|
35
|
-
"@kindgi/types": "0.1.
|
|
33
|
+
"@kindgi/log": "0.1.7",
|
|
34
|
+
"@kindgi/schema": "0.1.7",
|
|
35
|
+
"@kindgi/types": "0.1.7",
|
|
36
36
|
"ajv": "^8.17.1",
|
|
37
37
|
"ajv-formats": "^3.0.1",
|
|
38
38
|
"semver": "^7.6.3"
|
|
@@ -46,7 +46,7 @@
|
|
|
46
46
|
}
|
|
47
47
|
},
|
|
48
48
|
"devDependencies": {
|
|
49
|
-
"@kindgi/specs": "0.1.
|
|
49
|
+
"@kindgi/specs": "0.1.7",
|
|
50
50
|
"@types/node": "^22.10.5",
|
|
51
51
|
"@types/semver": "^7.5.8",
|
|
52
52
|
"typescript": "^5.7.3",
|
package/src/idempotency-key.ts
CHANGED
|
@@ -17,9 +17,12 @@ const NAMESPACE_BYTES = Buffer.from(TOOL_IDEMPOTENCY_NAMESPACE.replaceAll('-', '
|
|
|
17
17
|
* failure, a re-run after a crash), different for every other call.
|
|
18
18
|
*
|
|
19
19
|
* It's a name-based UUID (RFC 9562 version 5, SHA-1) under
|
|
20
|
-
* `TOOL_IDEMPOTENCY_NAMESPACE`, over the UTF-8 bytes of the
|
|
21
|
-
*
|
|
22
|
-
*
|
|
20
|
+
* `TOOL_IDEMPOTENCY_NAMESPACE`, over the UTF-8 bytes of the JSON array
|
|
21
|
+
* `[runId, stepScope, toolId]` (with `callId` appended for a call a model
|
|
22
|
+
* asked for) as `JSON.stringify` writes it: no whitespace, and characters
|
|
23
|
+
* beyond ASCII as themselves, never as `\u` escapes. Python's `json.dumps`
|
|
24
|
+
* escapes them unless `ensure_ascii=False`, and an escaped part is another
|
|
25
|
+
* key. `stepScope` is the step's own (`NodeContext.stepScope`):
|
|
23
26
|
* it tells two loop iterations or two fanout branches apart, which a
|
|
24
27
|
* model's call id alone doesn't (it's only unique within one answer).
|
|
25
28
|
*/
|
package/src/index.ts
CHANGED
|
@@ -22,6 +22,7 @@ import './http.js';
|
|
|
22
22
|
export { registerToolSpecSynthesizer, getToolSpecSynthesizer } from './spec-registry.js';
|
|
23
23
|
export type { ToolSpecSynthesizer, ToolSpecSynthesizerOptions } from './spec-registry.js';
|
|
24
24
|
export { invokeTool } from './invoke.js';
|
|
25
|
+
export { invokeToolForTest, toolContextForTest } from './test-context.js';
|
|
25
26
|
export { toolSecretNames } from './secret-names.js';
|
|
26
27
|
export { ToolPreconditionError, isToolPreconditionError } from './precondition.js';
|
|
27
28
|
export { TOOL_ENV_RECORD_KEY, toolCallRecordKey } from './record-keys.js';
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// Copyright (C) 2026 Kindgi Inc.
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The env values `invokeToolForTest` hands a tool, decided as the runtime decides a pack tool's
|
|
6
|
+
* (its tool bridge, "Env values"): each name the tool declares in `needsSpec.env` takes the
|
|
7
|
+
* context's `env` value, else its schema's `default`, and each is checked against its schema. A
|
|
8
|
+
* name with neither, or a value its schema refuses, refuses the call before the handler runs
|
|
9
|
+
* (`env-value-missing` first, then `env-value-invalid`). The handler sees the declared names
|
|
10
|
+
* only: the runtime sends no others. `env-declaration-invalid` is this helper's own: the runtime
|
|
11
|
+
* never refuses a call for it, it leaves the tool out when it loads (and `defineTool` refuses
|
|
12
|
+
* the schema up front). Test only: `invokeTool` itself never decides env.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { type CompiledInlineSchema, compileInlineSchema } from '@kindgi/schema';
|
|
16
|
+
import type { Result } from '@kindgi/types';
|
|
17
|
+
|
|
18
|
+
import { ToolPreconditionError } from './precondition.js';
|
|
19
|
+
import type { JsonSchema, Tool } from './types.js';
|
|
20
|
+
|
|
21
|
+
/** Compiled env schemas, by schema object (a tool's are defined once and reused). */
|
|
22
|
+
const compiled = new WeakMap<object, CompiledInlineSchema>();
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* The env the handler gets: the declared names, each with its value. Undefined when the tool
|
|
26
|
+
* declares none, as from the runtime.
|
|
27
|
+
*/
|
|
28
|
+
export function envForCall(
|
|
29
|
+
tool: Pick<Tool, 'id' | 'needsSpec'>,
|
|
30
|
+
given: Readonly<Record<string, string>> | undefined,
|
|
31
|
+
): Result<Readonly<Record<string, string>> | undefined, ToolPreconditionError> {
|
|
32
|
+
const declared = Object.entries(tool.needsSpec?.env ?? {});
|
|
33
|
+
if (declared.length === 0) return { kind: 'ok', value: undefined };
|
|
34
|
+
const id = tool.id as unknown as string;
|
|
35
|
+
const chosen = declared.map(([name, schema]) => ({
|
|
36
|
+
name,
|
|
37
|
+
schema,
|
|
38
|
+
value: given !== undefined && Object.hasOwn(given, name) ? given[name] : defaultOf(schema),
|
|
39
|
+
}));
|
|
40
|
+
// As the runtime: a missing value refuses the call first, then each value is checked.
|
|
41
|
+
const missing = chosen.filter((c) => c.value === undefined).map((c) => c.name);
|
|
42
|
+
if (missing.length > 0) return refused('env-value-missing', missingMessage(id, missing));
|
|
43
|
+
const values: Record<string, string> = {};
|
|
44
|
+
for (const { name, schema, value } of chosen) {
|
|
45
|
+
const problem = checkValue(id, name, schema, value as string);
|
|
46
|
+
if (problem !== undefined) return { kind: 'err', error: problem };
|
|
47
|
+
values[name] = value as string;
|
|
48
|
+
}
|
|
49
|
+
return { kind: 'ok', value: values };
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function missingMessage(id: string, missing: readonly string[]): string {
|
|
53
|
+
const one = missing.length === 1;
|
|
54
|
+
const which = one
|
|
55
|
+
? `env value "${missing[0]}"`
|
|
56
|
+
: `env values ${missing.map((n) => `"${n}"`).join(', ')}`;
|
|
57
|
+
return `tool "${id}" needs ${which}: the context's \`env\` doesn't set ${one ? 'it' : 'them'}, and ${one ? 'its schema has' : 'their schemas have'} no default`;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** Why a value doesn't fit its schema (or the schema doesn't compile); undefined when it fits. */
|
|
61
|
+
function checkValue(
|
|
62
|
+
id: string,
|
|
63
|
+
name: string,
|
|
64
|
+
schema: JsonSchema,
|
|
65
|
+
value: string,
|
|
66
|
+
): ToolPreconditionError | undefined {
|
|
67
|
+
const validator = validatorFor(schema);
|
|
68
|
+
if (validator.kind === 'err') {
|
|
69
|
+
return new ToolPreconditionError(
|
|
70
|
+
'env-declaration-invalid',
|
|
71
|
+
`tool "${id}" declares env value "${name}" with a schema that doesn't compile: ${validator.error}`,
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
const checked = validator.value.validate(value);
|
|
75
|
+
if (checked.kind === 'ok') return undefined;
|
|
76
|
+
return new ToolPreconditionError(
|
|
77
|
+
'env-value-invalid',
|
|
78
|
+
`env value "${name}" is "${value}", which doesn't match the schema tool "${id}" declares for it: ${constraintsBroken(checked.error.errors)}`,
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** A schema's `default`, when it's a string (env values are strings; `defineTool` checks it). */
|
|
83
|
+
function defaultOf(schema: JsonSchema): string | undefined {
|
|
84
|
+
const fallback = (schema as { readonly default?: unknown }).default;
|
|
85
|
+
return typeof fallback === 'string' ? fallback : undefined;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function validatorFor(schema: JsonSchema): Result<CompiledInlineSchema, string> {
|
|
89
|
+
const key = schema as object;
|
|
90
|
+
const cached = compiled.get(key);
|
|
91
|
+
if (cached !== undefined) return { kind: 'ok', value: cached };
|
|
92
|
+
const result = compileInlineSchema(schema);
|
|
93
|
+
if (result.kind === 'err') return { kind: 'err', error: result.error.message };
|
|
94
|
+
compiled.set(key, result.value);
|
|
95
|
+
return { kind: 'ok', value: result.value };
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** What a value broke, in the schema validator's words (the runtime's wording). */
|
|
99
|
+
function constraintsBroken(errors: readonly unknown[]): string {
|
|
100
|
+
const messages = errors
|
|
101
|
+
.map((e) => (e as { readonly message?: unknown }).message)
|
|
102
|
+
.filter((m): m is string => typeof m === 'string');
|
|
103
|
+
return messages.length > 0 ? messages.join('; ') : 'it does not validate';
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function refused(reason: string, message: string): { kind: 'err'; error: ToolPreconditionError } {
|
|
107
|
+
return { kind: 'err', error: new ToolPreconditionError(reason, message) };
|
|
108
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// Copyright (C) 2026 Kindgi Inc.
|
|
3
|
+
|
|
4
|
+
import type { Result, TenantId } from '@kindgi/types';
|
|
5
|
+
|
|
6
|
+
import type { ToolError } from './errors.js';
|
|
7
|
+
import { envForCall } from './invoke-env.js';
|
|
8
|
+
import { type InvokeToolOptions, invokeTool } from './invoke.js';
|
|
9
|
+
import type { Tool, ToolContext } from './types.js';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* A context for calling a tool in a unit test: tenant `tenant-test`, run `run-test`, a signal
|
|
13
|
+
* that never fires, and whatever you pass over them (`env`, `secrets`, `projectId`, …). The
|
|
14
|
+
* TypeScript counterpart of Python's `ToolContext.for_test`.
|
|
15
|
+
*/
|
|
16
|
+
export function toolContextForTest(overrides: Partial<ToolContext> = {}): ToolContext {
|
|
17
|
+
return {
|
|
18
|
+
tenantId: 'tenant-test' as TenantId,
|
|
19
|
+
runId: 'run-test',
|
|
20
|
+
abortSignal: new AbortController().signal,
|
|
21
|
+
...overrides,
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Call a tool in a unit test as a run would: `invokeTool`, with the tool's env values decided
|
|
27
|
+
* the way the runtime decides them for a pack's tool. Each name it declares in `needsSpec.env`
|
|
28
|
+
* takes the context's `env` value, else its schema's `default`; each is checked against its
|
|
29
|
+
* schema; the handler's `ctx.env` holds the declared names only. A name with neither, or a value
|
|
30
|
+
* its schema refuses, is `precondition-failed` (`env-value-missing`, then `env-value-invalid`)
|
|
31
|
+
* and the handler doesn't run. As in a run, the input is checked first: the env is decided as
|
|
32
|
+
* the handler is called. The context is `toolContextForTest(context)`.
|
|
33
|
+
*
|
|
34
|
+
* Test only: in a run, the runtime decides a pack tool's env itself, from its env store, and
|
|
35
|
+
* calls `invokeTool`.
|
|
36
|
+
*
|
|
37
|
+
* ```ts
|
|
38
|
+
* const result = await invokeToolForTest(findOrder, { orderId: 'ord_1001' }, {
|
|
39
|
+
* env: { ORDERS_BASE_URL: 'https://orders.example.com' },
|
|
40
|
+
* });
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
export function invokeToolForTest<TInput = unknown, TOutput = unknown>(
|
|
44
|
+
tool: Tool<TInput, TOutput>,
|
|
45
|
+
input: unknown,
|
|
46
|
+
context: Partial<ToolContext> = {},
|
|
47
|
+
options?: InvokeToolOptions,
|
|
48
|
+
): Promise<Result<TOutput, ToolError>> {
|
|
49
|
+
const withEnv: Tool<TInput, TOutput> = {
|
|
50
|
+
...tool,
|
|
51
|
+
handler: async (prepared, ctx) => {
|
|
52
|
+
const env = envForCall(tool, ctx.env);
|
|
53
|
+
if (env.kind === 'err') throw env.error;
|
|
54
|
+
if (env.value === undefined && ctx.env === undefined) return tool.handler(prepared, ctx);
|
|
55
|
+
const { env: _given, ...rest } = ctx;
|
|
56
|
+
return tool.handler(prepared, env.value === undefined ? rest : { ...rest, env: env.value });
|
|
57
|
+
},
|
|
58
|
+
};
|
|
59
|
+
return invokeTool(withEnv, input, toolContextForTest(context), options);
|
|
60
|
+
}
|
package/src/types.ts
CHANGED
|
@@ -174,8 +174,10 @@ export interface ToolContext {
|
|
|
174
174
|
* call (`record`), so the call re-run after a wait or a retry sees the
|
|
175
175
|
* same ones. Not for credentials: env values are shown with the run
|
|
176
176
|
* (use `secrets`).
|
|
177
|
-
* Absent when the tool declares none, and from an older runtime
|
|
178
|
-
* unit test
|
|
177
|
+
* Absent when the tool declares none, and from an older runtime.
|
|
178
|
+
* In a unit test, `invokeToolForTest` decides them the same way from
|
|
179
|
+
* the context it's given: its `env`, else each schema's `default`, each
|
|
180
|
+
* checked, the declared names only. `invokeTool` passes `env` as given.
|
|
179
181
|
*/
|
|
180
182
|
readonly env?: Readonly<Record<string, string>>;
|
|
181
183
|
/**
|