@agntn/tools 0.2.0 → 0.2.1
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/README.md +1 -1
- package/dist/_chunks/index.d.mts +205 -0
- package/dist/_chunks/index.d.mts.map +1 -0
- package/dist/_chunks/libs/typebox.mjs +3 -234
- package/dist/_chunks/libs/typebox.mjs.map +1 -1
- package/dist/_chunks/mcp-answers.mjs +8 -1
- package/dist/_chunks/mcp-answers.mjs.map +1 -1
- package/dist/_chunks/src.mjs.map +1 -1
- package/dist/_chunks/updates.mjs +2 -1
- package/dist/_chunks/updates.mjs.map +1 -1
- package/dist/ai.d.mts +1 -1
- package/dist/cli.d.mts +4 -3
- package/dist/cli.d.mts.map +1 -1
- package/dist/cli.mjs +183 -32
- package/dist/cli.mjs.map +1 -1
- package/dist/h3.d.mts +1 -1
- package/dist/h3.mjs +1 -0
- package/dist/h3.mjs.map +1 -1
- package/dist/index.d.mts +3 -177
- package/dist/index.mjs +2 -2
- package/dist/mcp.d.mts +6 -3
- package/dist/mcp.d.mts.map +1 -1
- package/dist/mcp.mjs +4 -2
- package/dist/mcp.mjs.map +1 -1
- package/dist/omp.d.mts +3 -1
- package/dist/omp.d.mts.map +1 -1
- package/dist/omp.mjs +3 -2
- package/dist/omp.mjs.map +1 -1
- package/dist/pi.d.mts +1 -1
- package/dist/pi.mjs +1 -1
- package/dist/pi.mjs.map +1 -1
- package/package.json +1 -1
- package/dist/index.d.mts.map +0 -1
package/README.md
CHANGED
|
@@ -23,7 +23,7 @@ MCP wants an `inputSchema`. Pi wants `parameters` and a prompt snippet. OMP swap
|
|
|
23
23
|
- 🪤 **The OMP TypeBox trap, defused.** TypeBox is bundled, so OMP has nothing left to rewrite behind your back.
|
|
24
24
|
- 🧪 **Schema rules at definition time.** An open object or a union of literals fails in `defineTool`. Not three weeks later in a model's hands.
|
|
25
25
|
- ⏳ **Progress that gets somewhere.** `progress?.("still at it")` turns into `onUpdate` in Pi and OMP and `notifications/progress` in MCP. A slow call stops looking like a dead one.
|
|
26
|
-
- ⌨️ **A CLI on the side.** `runCli` turns the same tools into commands, no CLI library needed. Flags come from the schema.
|
|
26
|
+
- ⌨️ **A CLI on the side.** `runCli` turns the same tools into commands, no CLI library needed. Flags come from the schema. Hints add positionals, short flags and stdin.
|
|
27
27
|
- 🌐 **Runs anywhere.** No `node:*` import in the core or the host adapters. The docs site runs them in a browser tab. The CLI is the one Node part, as a CLI should be.
|
|
28
28
|
|
|
29
29
|
## 📦 Install
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
import { Static, TObject } from "./libs/typebox.mjs";
|
|
2
|
+
/**
|
|
3
|
+
* Makes multi-line tool text safe for a terminal. Unlike {@link sanitizeLine}
|
|
4
|
+
* it keeps the layout: line feeds, tabs and the joiners of emoji and scripts
|
|
5
|
+
* (ZWJ, ZWNJ) stay, CR and CRLF become a line feed, and the Unicode line and
|
|
6
|
+
* paragraph separators become a space so words do not run together.
|
|
7
|
+
*
|
|
8
|
+
* @param text - Tool text.
|
|
9
|
+
* @returns {string} The text without escape sequences or other control characters.
|
|
10
|
+
*/
|
|
11
|
+
export declare function sanitizeText(text: string): string;
|
|
12
|
+
/** Content block understood by MCP, Pi and OMP alike. */
|
|
13
|
+
export type ToolContent = {
|
|
14
|
+
type: "text";
|
|
15
|
+
text: string;
|
|
16
|
+
} | {
|
|
17
|
+
type: "image";
|
|
18
|
+
data: string;
|
|
19
|
+
mimeType: string;
|
|
20
|
+
};
|
|
21
|
+
/** Text for the model plus details for the harness. */
|
|
22
|
+
export interface ToolResult<Details = unknown> {
|
|
23
|
+
content: ToolContent[];
|
|
24
|
+
details: Details;
|
|
25
|
+
/** Set when the tool could not answer. Each adapter carries it to its host. */
|
|
26
|
+
isError?: boolean;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* What a call does to the world, mapped to MCP annotations and OMP approval.
|
|
30
|
+
*
|
|
31
|
+
* - `read` changes nothing.
|
|
32
|
+
* - `write` changes state that the next call can undo or overwrite.
|
|
33
|
+
* - `destructive` deletes or overwrites what cannot be recovered.
|
|
34
|
+
*/
|
|
35
|
+
export type ToolEffect = "read" | "write" | "destructive";
|
|
36
|
+
/**
|
|
37
|
+
* How a tool reads on the command line. Without hints the command is the tool
|
|
38
|
+
* name without its package prefix, in kebab case, and every property is a flag.
|
|
39
|
+
*/
|
|
40
|
+
export interface ToolCliHints {
|
|
41
|
+
/** Command name instead of the derived one. */
|
|
42
|
+
readonly command?: string;
|
|
43
|
+
readonly aliases?: readonly string[];
|
|
44
|
+
/** Line in the command list. Defaults to the first sentence of `description`. */
|
|
45
|
+
readonly description?: string;
|
|
46
|
+
/** Properties taken in this order as positional arguments instead of flags. */
|
|
47
|
+
readonly positional?: readonly string[];
|
|
48
|
+
/** A string property that takes every word left after `positional`, joined by single spaces. */
|
|
49
|
+
readonly rest?: string;
|
|
50
|
+
/** Short flags by property, `{ limit: "n" }` for `-n 5`. Only a whole word counts. */
|
|
51
|
+
readonly short?: Readonly<Record<string, string>>;
|
|
52
|
+
/** String properties that read stdin when given as `-`. */
|
|
53
|
+
readonly stdin?: readonly string[];
|
|
54
|
+
/** `false` takes `--json` away, for a command that writes its own bytes to stdout. */
|
|
55
|
+
readonly json?: boolean;
|
|
56
|
+
}
|
|
57
|
+
/** How far a long call has come, for hosts that draw it. */
|
|
58
|
+
export interface ToolProgress {
|
|
59
|
+
/** Work done so far. MCP skips an update where it doesn't grow. */
|
|
60
|
+
readonly progress: number;
|
|
61
|
+
/** All the work there is, when the tool knows it. */
|
|
62
|
+
readonly total?: number;
|
|
63
|
+
}
|
|
64
|
+
/** An MCP icon, the SDK's own shape: a URL or `data:` URI a client can draw on its card. */
|
|
65
|
+
export interface Icon {
|
|
66
|
+
readonly src: string;
|
|
67
|
+
readonly mimeType?: string;
|
|
68
|
+
/** `WxH` sizes such as `48x48`, or `any` for an SVG. */
|
|
69
|
+
readonly sizes?: readonly string[];
|
|
70
|
+
/** Background the icon is drawn for. */
|
|
71
|
+
readonly theme?: "light" | "dark";
|
|
72
|
+
}
|
|
73
|
+
/** Per-call context the adapters hand to `execute`. */
|
|
74
|
+
export interface ToolCallContext {
|
|
75
|
+
signal?: AbortSignal;
|
|
76
|
+
/** Says the call is still at it. Absent where the host can't show it, so call `progress?.()`. */
|
|
77
|
+
progress?: (message: string, amount?: ToolProgress) => void;
|
|
78
|
+
/** The host's own `ctx`: Pi and OMP pass it, other surfaces don't. Narrow it yourself. */
|
|
79
|
+
host?: unknown;
|
|
80
|
+
}
|
|
81
|
+
export interface ToolDefinition<Input extends TObject = TObject, Details = unknown> {
|
|
82
|
+
/** Wire name, `<package>_<operation>`. */
|
|
83
|
+
readonly name: string;
|
|
84
|
+
/** Human label: MCP `title` and `annotations.title`, Pi and OMP `label`. */
|
|
85
|
+
readonly title: string;
|
|
86
|
+
/** Complete instruction for the model. MCP clients see nothing else. */
|
|
87
|
+
readonly description: string;
|
|
88
|
+
/** MCP `icons` in `tools/list`. Pi, OMP, the AI SDK and the CLI don't draw them. */
|
|
89
|
+
readonly icons?: readonly Icon[];
|
|
90
|
+
/** Pi `promptSnippet`. */
|
|
91
|
+
readonly snippet?: string;
|
|
92
|
+
/** Pi `promptGuidelines`. Must not promise anything `description` lacks. */
|
|
93
|
+
readonly guidelines?: readonly string[];
|
|
94
|
+
readonly effect: ToolEffect;
|
|
95
|
+
/** Repeating the call with the same input has no further effect. Defaults to `effect === "read"`. */
|
|
96
|
+
readonly idempotent?: boolean;
|
|
97
|
+
/** The call reaches outside the local machine. */
|
|
98
|
+
readonly openWorld?: boolean;
|
|
99
|
+
readonly input: Input;
|
|
100
|
+
/** Command line shape for `@agntn/tools/cli`. No other surface reads it. */
|
|
101
|
+
readonly cli?: ToolCliHints;
|
|
102
|
+
/**
|
|
103
|
+
* Runs the tool on input that already passed {@link validateInput}.
|
|
104
|
+
*
|
|
105
|
+
* Method syntax on purpose: it keeps the parameter bivariant, so a list of
|
|
106
|
+
* tools with different inputs is still a `readonly ToolDefinition[]`.
|
|
107
|
+
*/
|
|
108
|
+
execute(input: Static<Input>, context: ToolCallContext): ToolResult<Details> | Promise<ToolResult<Details>>;
|
|
109
|
+
}
|
|
110
|
+
/** Thrown when a tool definition breaks a contract every surface relies on. */
|
|
111
|
+
export declare class ToolDefinitionError extends Error {
|
|
112
|
+
name: string;
|
|
113
|
+
}
|
|
114
|
+
/** Thrown by {@link invokeTool} when the arguments fail the tool schema. */
|
|
115
|
+
export declare class ToolInputError extends Error {
|
|
116
|
+
name: string;
|
|
117
|
+
/** One validation failure per line. Only these line breaks are the core's own. */
|
|
118
|
+
readonly lines: readonly string[];
|
|
119
|
+
/**
|
|
120
|
+
* @param lines - One validation failure per line.
|
|
121
|
+
*/
|
|
122
|
+
constructor(lines: readonly string[]);
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Declares a tool and checks the schema rules every surface depends on.
|
|
126
|
+
*
|
|
127
|
+
* @param tool - Tool definition.
|
|
128
|
+
* @returns {ToolDefinition<Input, Details>} The same definition, typed.
|
|
129
|
+
*/
|
|
130
|
+
export declare function defineTool<const Input extends TObject, Details>(tool: ToolDefinition<Input, Details>): ToolDefinition<Input, Details>;
|
|
131
|
+
/**
|
|
132
|
+
* The input schema as the plain JSON Schema object every host receives.
|
|
133
|
+
*
|
|
134
|
+
* TypeBox 1.x schemas are plain objects already; this only drops the static
|
|
135
|
+
* type, which lacks the index signature host APIs declare.
|
|
136
|
+
*
|
|
137
|
+
* @param tool - Tool whose schema to emit.
|
|
138
|
+
* @returns {Record<string, unknown>} JSON Schema document.
|
|
139
|
+
*/
|
|
140
|
+
export declare function wireSchema(tool: ToolDefinition): Record<string, unknown>;
|
|
141
|
+
/**
|
|
142
|
+
* Indexes tools by name.
|
|
143
|
+
*
|
|
144
|
+
* A `Map`, not a plain object: a lookup of `toString` in a `Record` finds
|
|
145
|
+
* `Object.prototype.toString` and skips the unknown-tool branch.
|
|
146
|
+
*
|
|
147
|
+
* @param tools - Tools to index.
|
|
148
|
+
* @returns {ReadonlyMap<string, ToolDefinition>} Tools by wire name.
|
|
149
|
+
*/
|
|
150
|
+
export declare function indexTools(tools: readonly ToolDefinition[]): ReadonlyMap<string, ToolDefinition>;
|
|
151
|
+
export type InputCheck<Input extends TObject> = {
|
|
152
|
+
ok: true;
|
|
153
|
+
value: Static<Input>;
|
|
154
|
+
} | {
|
|
155
|
+
ok: false;
|
|
156
|
+
lines: readonly string[];
|
|
157
|
+
};
|
|
158
|
+
/**
|
|
159
|
+
* Validates arguments against the tool schema, reporting every failure.
|
|
160
|
+
*
|
|
161
|
+
* Runs on every surface, not only MCP: a host may skip its own validation, and
|
|
162
|
+
* OMP's omptype drops `pattern` from the schema it emits. The message has one
|
|
163
|
+
* line per failure: an undeclared key names itself with the keys its object
|
|
164
|
+
* takes, at any depth, and an enum failure names the allowed values, so the
|
|
165
|
+
* caller does not go back to the tool list and guess. Format from agntn/hashes
|
|
166
|
+
* (#58). TypeBox stops at eight errors, so root keys are read off the arguments.
|
|
167
|
+
*
|
|
168
|
+
* @param tool - Tool whose schema applies.
|
|
169
|
+
* @param args - Arguments as received from the host.
|
|
170
|
+
* @returns {InputCheck<Input>} The typed value, or the failures, one per line.
|
|
171
|
+
*/
|
|
172
|
+
export declare function validateInput<Input extends TObject>(tool: ToolDefinition<Input, unknown>, args: unknown): InputCheck<Input>;
|
|
173
|
+
/**
|
|
174
|
+
* Validates and runs a tool. Every adapter calls through here.
|
|
175
|
+
*
|
|
176
|
+
* Progress is best effort: a line after the call settles goes nowhere, and a host that throws on
|
|
177
|
+
* one doesn't fail the call.
|
|
178
|
+
*
|
|
179
|
+
* @param tool - Tool to run.
|
|
180
|
+
* @param args - Arguments as received from the host.
|
|
181
|
+
* @param context - Per-call context.
|
|
182
|
+
* @returns {Promise<ToolResult>} The executor's result, unchanged.
|
|
183
|
+
* @throws {ToolInputError} When the arguments fail the schema.
|
|
184
|
+
*/
|
|
185
|
+
export declare function invokeTool(tool: ToolDefinition, args: unknown, context?: ToolCallContext): Promise<ToolResult>;
|
|
186
|
+
/**
|
|
187
|
+
* Makes a value safe for one line of terminal or protocol text.
|
|
188
|
+
*
|
|
189
|
+
* Removes escape sequences, then replaces every control, format, line and
|
|
190
|
+
* paragraph separator character with a space: a raw newline, U+2028 or a bidi
|
|
191
|
+
* override (U+202E, a `Cf`) in an echoed value forges a line that reads as the
|
|
192
|
+
* tool's own output. `String()` first, since hostile JSON ignores declared types.
|
|
193
|
+
*
|
|
194
|
+
* @param value - Value to render.
|
|
195
|
+
* @returns {string} Single-line text.
|
|
196
|
+
*/
|
|
197
|
+
export declare function sanitizeLine(value: unknown): string;
|
|
198
|
+
/**
|
|
199
|
+
* Text of a result, for hosts that carry failures as thrown errors.
|
|
200
|
+
*
|
|
201
|
+
* @param result - Tool result.
|
|
202
|
+
* @returns {string} Text blocks joined by newlines.
|
|
203
|
+
*/
|
|
204
|
+
export declare function resultText(result: ToolResult): string;
|
|
205
|
+
//# sourceMappingURL=index.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../../src/escapes.ts","../../src/index.ts"],"mappings":";;;;;;;;;;wBAwDgB,aAAa;;YCtBjB;EACN;EAAc;;EACd;EAAe;EAAc;;;iBAGlB,WAAW;EAC1B,SAAS;EACT,SAAS;;EAET;;;;;;;;;YAUU;;;;;iBAMK;;WAEN;WACA;;WAEA;;WAEA;;WAEA;;WAEA,QAAQ,SAAS;;WAEjB;;WAEA;;;iBAIM;;WAEN;;WAEA;;;iBAIM;WACN;WACA;;WAEA;;WAEA;;;iBAIM;EACf,SAAS;;EAET,YAAY,iBAAiB,SAAS;;EAEtC;;iBAGe,eAAe,cAAc,UAAU,SAAS;;WAEtD;;WAEA;;WAEA;;WAEA,iBAAiB;;WAEjB;;WAEA;WACA,QAAQ;;WAER;;WAEA;WACA,OAAO;;WAEP,MAAM;;;;;;;EAOf,QACE,OAAO,OAAO,QACd,SAAS,kBACR,WAAW,WAAW,QAAQ,WAAW;;;qBAIjC,4BAA4B;EAC9B;;;qBAIE,uBAAuB;EACzB;;WAEA;;;;EAKT,YAAY;;;;;;;;wBAcE,iBAAiB,cAAc,SAAS,SACtD,MAAM,eAAe,OAAO,WAC3B,eAAe,OAAO;;;;;;;;;;wBA+GT,WAAW,MAAM,iBAAiB;;;;;;;;;;wBAalC,WAAW,gBAAgB,mBAAmB,oBAAoB;YAStE,WAAW,cAAc;EAC/B;EAAU,OAAO,OAAO;;EACxB;EAAW;;;;;;;;;;;;;;;;wBAgBD,cAAc,cAAc,SAC1C,MAAM,eAAe,iBACrB,gBACC,WAAW;;;;;;;;;;;;;wBA4JQ,WACpB,MAAM,gBACN,eACA,UAAS,kBACR,QAAQ;;;;;;;;;;;;wBAmCK,aAAa;;;;;;;wBAab,WAAW,QAAQ"}
|
|
@@ -134,9 +134,6 @@ function IsUnsafePropertyKey(key) {
|
|
|
134
134
|
function HasPropertyKey(value, key) {
|
|
135
135
|
return IsUnsafePropertyKey(key) ? Object.prototype.hasOwnProperty.call(value, key) : key in value;
|
|
136
136
|
}
|
|
137
|
-
function EntriesRegExp(value) {
|
|
138
|
-
return Keys(value).map((key) => [new RegExp(`^${key}$`), value[key]]);
|
|
139
|
-
}
|
|
140
137
|
function Entries(value) {
|
|
141
138
|
return Object.entries(value);
|
|
142
139
|
}
|
|
@@ -5623,236 +5620,8 @@ function Errors(...args) {
|
|
|
5623
5620
|
const [_, errors] = Errors$1(context, type, value);
|
|
5624
5621
|
return errors;
|
|
5625
5622
|
}
|
|
5626
|
-
|
|
5627
|
-
|
|
5628
|
-
}
|
|
5629
|
-
function IsOk(value) {
|
|
5630
|
-
return IsObject$1(value) && HasPropertyKey(value, "value");
|
|
5631
|
-
}
|
|
5632
|
-
function Ok(value) {
|
|
5633
|
-
return { value };
|
|
5634
|
-
}
|
|
5635
|
-
function TryArray(value) {
|
|
5636
|
-
return IsArray$1(value) ? Ok(value) : Ok([value]);
|
|
5637
|
-
}
|
|
5638
|
-
function FromBoolean$4(value) {
|
|
5639
|
-
return IsEqual(value, true) ? Ok(BigInt(1)) : Ok(BigInt(0));
|
|
5640
|
-
}
|
|
5641
|
-
const bigintPattern = /^-?(0|[1-9]\d*)n$/;
|
|
5642
|
-
const decimalPattern = /^-?(0|[1-9]\d*)\.\d+$/;
|
|
5643
|
-
const integerPattern = /^-?(0|[1-9]\d*)$/;
|
|
5644
|
-
function IsStringBigIntLike(value) {
|
|
5645
|
-
return bigintPattern.test(value);
|
|
5646
|
-
}
|
|
5647
|
-
function IsStringDecimalLike(value) {
|
|
5648
|
-
return decimalPattern.test(value);
|
|
5649
|
-
}
|
|
5650
|
-
function IsStringIntegerLike(value) {
|
|
5651
|
-
return integerPattern.test(value);
|
|
5652
|
-
}
|
|
5653
|
-
function FromString$5(value) {
|
|
5654
|
-
const lowercase = value.toLowerCase();
|
|
5655
|
-
return IsStringBigIntLike(value) ? Ok(BigInt(value.slice(0, value.length - 1))) : IsStringDecimalLike(value) ? Ok(BigInt(value.split(".")[0])) : IsStringIntegerLike(value) ? Ok(BigInt(value)) : IsEqual(lowercase, "false") ? Ok(BigInt(0)) : IsEqual(lowercase, "true") ? Ok(BigInt(1)) : void 0;
|
|
5656
|
-
}
|
|
5657
|
-
function TryBigInt(value) {
|
|
5658
|
-
return IsBigInt$1(value) ? Ok(value) : IsBoolean$2(value) ? FromBoolean$4(value) : IsNumber$2(value) ? Ok(BigInt(Math.trunc(value))) : IsNull$1(value) ? Ok(BigInt(0)) : IsString$2(value) ? FromString$5(value) : IsUndefined$1(value) ? Ok(BigInt(0)) : void 0;
|
|
5659
|
-
}
|
|
5660
|
-
function FromBigInt$4(value) {
|
|
5661
|
-
return IsEqual(value, BigInt(0)) ? Ok(false) : IsEqual(value, BigInt(1)) ? Ok(true) : void 0;
|
|
5662
|
-
}
|
|
5663
|
-
function FromNumber$3(value) {
|
|
5664
|
-
return IsEqual(value, 0) ? Ok(false) : IsEqual(value, 1) ? Ok(true) : void 0;
|
|
5665
|
-
}
|
|
5666
|
-
function FromString$4(value) {
|
|
5667
|
-
return IsEqual(value.toLowerCase(), "false") ? Ok(false) : IsEqual(value.toLowerCase(), "true") ? Ok(true) : IsEqual(value, "0") ? Ok(false) : IsEqual(value, "1") ? Ok(true) : void 0;
|
|
5668
|
-
}
|
|
5669
|
-
function TryBoolean(value) {
|
|
5670
|
-
return IsBigInt$1(value) ? FromBigInt$4(value) : IsBoolean$2(value) ? Ok(value) : IsNumber$2(value) ? FromNumber$3(value) : IsNull$1(value) ? Ok(false) : IsString$2(value) ? FromString$4(value) : IsUndefined$1(value) ? Ok(false) : void 0;
|
|
5671
|
-
}
|
|
5672
|
-
function FromBigInt$3(value) {
|
|
5673
|
-
return IsEqual(value, BigInt(0)) ? Ok(null) : void 0;
|
|
5674
|
-
}
|
|
5675
|
-
function FromBoolean$3(value) {
|
|
5676
|
-
return IsEqual(value, false) ? Ok(null) : void 0;
|
|
5677
|
-
}
|
|
5678
|
-
function FromNumber$2(value) {
|
|
5679
|
-
return IsEqual(value, 0) ? Ok(null) : void 0;
|
|
5680
|
-
}
|
|
5681
|
-
function FromString$3(value) {
|
|
5682
|
-
const lowercase = value.toLowerCase();
|
|
5683
|
-
return IsEqual(lowercase, "undefined") || IsEqual(lowercase, "null") || IsEqual(value, "") || IsEqual(value, "0") ? Ok(null) : void 0;
|
|
5684
|
-
}
|
|
5685
|
-
function TryNull(value) {
|
|
5686
|
-
return IsBigInt$1(value) ? FromBigInt$3(value) : IsBoolean$2(value) ? FromBoolean$3(value) : IsNumber$2(value) ? FromNumber$2(value) : IsNull$1(value) ? Ok(null) : IsString$2(value) ? FromString$3(value) : IsUndefined$1(value) ? Ok(null) : void 0;
|
|
5687
|
-
}
|
|
5688
|
-
const maxBigInt = BigInt(Number.MAX_SAFE_INTEGER);
|
|
5689
|
-
const minBigInt = BigInt(Number.MIN_SAFE_INTEGER);
|
|
5690
|
-
function FromBigInt$2(value) {
|
|
5691
|
-
return value <= maxBigInt && value >= minBigInt ? Ok(Number(value)) : void 0;
|
|
5692
|
-
}
|
|
5693
|
-
function FromBoolean$2(value) {
|
|
5694
|
-
return Ok(value ? 1 : 0);
|
|
5695
|
-
}
|
|
5696
|
-
function FromString$2(value) {
|
|
5697
|
-
const coerced = +value;
|
|
5698
|
-
if (IsNumber$2(coerced)) return Ok(coerced);
|
|
5699
|
-
const lowercase = value.toLowerCase();
|
|
5700
|
-
if (IsEqual(lowercase, "false")) return Ok(0);
|
|
5701
|
-
if (IsEqual(lowercase, "true")) return Ok(1);
|
|
5702
|
-
const result = TryBigInt(value);
|
|
5703
|
-
if (IsOk(result)) return result.value <= maxBigInt && result.value >= minBigInt ? Ok(Number(result.value)) : void 0;
|
|
5704
|
-
}
|
|
5705
|
-
function TryNumber(value) {
|
|
5706
|
-
return IsBigInt$1(value) ? FromBigInt$2(value) : IsBoolean$2(value) ? FromBoolean$2(value) : IsNumber$2(value) ? Ok(value) : IsNull$1(value) ? Ok(0) : IsString$2(value) ? FromString$2(value) : IsUndefined$1(value) ? Ok(0) : void 0;
|
|
5707
|
-
}
|
|
5708
|
-
function TryString(value) {
|
|
5709
|
-
return IsBigInt$1(value) ? Ok(value.toString()) : IsBoolean$2(value) ? Ok(value.toString()) : IsNumber$2(value) ? Ok(value.toString()) : IsNull$1(value) ? Ok("null") : IsString$2(value) ? Ok(value) : IsUndefined$1(value) ? Ok("") : void 0;
|
|
5710
|
-
}
|
|
5711
|
-
function FromBigInt$1(value) {
|
|
5712
|
-
return IsEqual(value, BigInt(0)) ? Ok(void 0) : void 0;
|
|
5713
|
-
}
|
|
5714
|
-
function FromBoolean$1(value) {
|
|
5715
|
-
return IsEqual(value, false) ? Ok(void 0) : void 0;
|
|
5716
|
-
}
|
|
5717
|
-
function FromNumber$1(value) {
|
|
5718
|
-
return IsEqual(value, 0) ? Ok(void 0) : void 0;
|
|
5719
|
-
}
|
|
5720
|
-
function FromString$1(value) {
|
|
5721
|
-
const lowercase = value.toLowerCase();
|
|
5722
|
-
return IsEqual(lowercase, "undefined") || IsEqual(lowercase, "null") || IsEqual(value, "") || IsEqual(value, "0") ? Ok(void 0) : void 0;
|
|
5723
|
-
}
|
|
5724
|
-
function TryUndefined(value) {
|
|
5725
|
-
return IsBigInt$1(value) ? FromBigInt$1(value) : IsBoolean$2(value) ? FromBoolean$1(value) : IsNumber$2(value) ? FromNumber$1(value) : IsNull$1(value) ? Ok(void 0) : IsString$2(value) ? FromString$1(value) : IsUndefined$1(value) ? Ok(value) : void 0;
|
|
5726
|
-
}
|
|
5727
|
-
function FromArray$2(context, type, value) {
|
|
5728
|
-
return TryArray(value).value.map((value) => FromType$2(context, type.items, value));
|
|
5729
|
-
}
|
|
5730
|
-
function FromBigInt(_context, _type, value) {
|
|
5731
|
-
const result = TryBigInt(value);
|
|
5732
|
-
return IsOk(result) ? result.value : value;
|
|
5733
|
-
}
|
|
5734
|
-
function FromBoolean(_context, _type, value) {
|
|
5735
|
-
const result = TryBoolean(value);
|
|
5736
|
-
return IsOk(result) ? result.value : value;
|
|
5737
|
-
}
|
|
5738
|
-
function FromCyclic$2(context, type, value) {
|
|
5739
|
-
return FromType$2({
|
|
5740
|
-
...context,
|
|
5741
|
-
...type.$defs
|
|
5742
|
-
}, Ref$1(type.$ref), value);
|
|
5743
|
-
}
|
|
5744
|
-
function FromEnum(context, type, value) {
|
|
5745
|
-
return FromType$2(context, Evaluate(type), value);
|
|
5746
|
-
}
|
|
5747
|
-
function FromInteger(_context, _type, value) {
|
|
5748
|
-
const result = TryNumber(value);
|
|
5749
|
-
return IsOk(result) ? Math.trunc(result.value) : value;
|
|
5750
|
-
}
|
|
5751
|
-
function FromIntersect$2(context, type, value) {
|
|
5752
|
-
return FromType$2(context, Evaluate(Instantiate(context, type)), value);
|
|
5753
|
-
}
|
|
5754
|
-
function FromLiteralBigInt(_context, type, value) {
|
|
5755
|
-
const result = TryBigInt(value);
|
|
5756
|
-
return IsOk(result) && IsEqual(type.const, result.value) ? result.value : value;
|
|
5757
|
-
}
|
|
5758
|
-
function FromLiteralBoolean(_context, type, value) {
|
|
5759
|
-
const result = TryBoolean(value);
|
|
5760
|
-
return IsOk(result) && IsEqual(type.const, result.value) ? result.value : value;
|
|
5761
|
-
}
|
|
5762
|
-
function FromLiteralNumber(_context, type, value) {
|
|
5763
|
-
const result = TryNumber(value);
|
|
5764
|
-
return IsOk(result) && IsEqual(type.const, result.value) ? result.value : value;
|
|
5765
|
-
}
|
|
5766
|
-
function FromLiteralString(_context, type, value) {
|
|
5767
|
-
const result = TryString(value);
|
|
5768
|
-
return IsOk(result) && IsEqual(type.const, result.value) ? result.value : value;
|
|
5769
|
-
}
|
|
5770
|
-
function FromLiteral(context, type, value) {
|
|
5771
|
-
if (IsEqual(type.const, value)) return value;
|
|
5772
|
-
return IsLiteralBigInt(type) ? FromLiteralBigInt(context, type, value) : IsLiteralBoolean(type) ? FromLiteralBoolean(context, type, value) : IsLiteralNumber(type) ? FromLiteralNumber(context, type, value) : IsLiteralString(type) ? FromLiteralString(context, type, value) : Unreachable$1();
|
|
5773
|
-
}
|
|
5774
|
-
function FromNull(_context, _type, value) {
|
|
5775
|
-
const result = TryNull(value);
|
|
5776
|
-
return IsOk(result) ? result.value : value;
|
|
5777
|
-
}
|
|
5778
|
-
function FromNumber(_context, _type, value) {
|
|
5779
|
-
const result = TryNumber(value);
|
|
5780
|
-
return IsOk(result) ? result.value : value;
|
|
5781
|
-
}
|
|
5782
|
-
function FromAdditionalProperties(context, entries, additionalProperties, value) {
|
|
5783
|
-
const keys = Keys(value);
|
|
5784
|
-
for (const [regexp, _] of entries) for (const key of keys) if (!regexp.test(key)) value[key] = FromType$2(context, additionalProperties, value[key]);
|
|
5785
|
-
return value;
|
|
5786
|
-
}
|
|
5787
|
-
function IsOptionalUndefined(property, key, value) {
|
|
5788
|
-
return IsOptional(property) && IsUndefined$1(value[key]);
|
|
5789
|
-
}
|
|
5790
|
-
function FromProperties(context, type, value) {
|
|
5791
|
-
const entries = EntriesRegExp(type.properties);
|
|
5792
|
-
const keys = Keys(value);
|
|
5793
|
-
for (const [regexp, property] of entries) for (const key of keys) {
|
|
5794
|
-
if (!regexp.test(key) || IsOptionalUndefined(property, key, value)) continue;
|
|
5795
|
-
value[key] = FromType$2(context, property, value[key]);
|
|
5796
|
-
}
|
|
5797
|
-
return HasPropertyKey(type, "additionalProperties") && IsObject$1(type.additionalProperties) ? FromAdditionalProperties(context, entries, type.additionalProperties, value) : value;
|
|
5798
|
-
}
|
|
5799
|
-
function FromObject$2(context, type, value) {
|
|
5800
|
-
return IsObjectNotArray(value) ? FromProperties(context, type, value) : value;
|
|
5801
|
-
}
|
|
5802
|
-
function FromPatternProperties(context, type, value) {
|
|
5803
|
-
const entries = EntriesRegExp(type.patternProperties);
|
|
5804
|
-
const keys = Keys(value);
|
|
5805
|
-
for (const [regexp, schema] of entries) for (const key of keys) if (regexp.test(key)) value[key] = FromType$2(context, schema, value[key]);
|
|
5806
|
-
return HasPropertyKey(type, "additionalProperties") && IsObject$1(type.additionalProperties) ? FromAdditionalProperties(context, entries, type.additionalProperties, value) : value;
|
|
5807
|
-
}
|
|
5808
|
-
function FromRecord$2(context, type, value) {
|
|
5809
|
-
return IsObjectNotArray(value) ? FromPatternProperties(context, type, value) : value;
|
|
5810
|
-
}
|
|
5811
|
-
function FromRef$2(context, type, value) {
|
|
5812
|
-
return HasPropertyKey(context, type.$ref) ? FromType$2(context, context[type.$ref], value) : value;
|
|
5813
|
-
}
|
|
5814
|
-
function FromString(_context, _type, value) {
|
|
5815
|
-
const result = TryString(value);
|
|
5816
|
-
return IsOk(result) ? result.value : value;
|
|
5817
|
-
}
|
|
5818
|
-
function FromTemplateLiteral(context, type, value) {
|
|
5819
|
-
return FromType$2(context, Evaluate(type), value);
|
|
5820
|
-
}
|
|
5821
|
-
function FromTuple$2(context, type, value) {
|
|
5822
|
-
if (!IsArray$1(value)) return value;
|
|
5823
|
-
for (let index = 0; index < Math.min(type.items.length, value.length); index++) value[index] = FromType$2(context, type.items[index], value[index]);
|
|
5824
|
-
return value;
|
|
5825
|
-
}
|
|
5826
|
-
function FromUndefined(_context, _type, value) {
|
|
5827
|
-
const result = TryUndefined(value);
|
|
5828
|
-
return IsOk(result) ? result.value : value;
|
|
5829
|
-
}
|
|
5830
|
-
function FromUnion$2(context, type, value) {
|
|
5831
|
-
if (type.anyOf.some((type) => Check(context, type, value))) return value;
|
|
5832
|
-
const selected = type.anyOf.map((type) => FromType$2(context, type, Clone(value))).find((value) => Check(context, type, value));
|
|
5833
|
-
return IsUndefined$1(selected) ? value : selected;
|
|
5834
|
-
}
|
|
5835
|
-
function FromVoid(_context, _type, value) {
|
|
5836
|
-
return IsOk(TryUndefined(value)) ? void 0 : value;
|
|
5837
|
-
}
|
|
5838
|
-
function FromType$2(context, type, value) {
|
|
5839
|
-
return IsArray(type) ? FromArray$2(context, type, value) : IsBigInt(type) ? FromBigInt(context, type, value) : IsBoolean(type) ? FromBoolean(context, type, value) : IsCyclic(type) ? FromCyclic$2(context, type, value) : IsEnum$1(type) ? FromEnum(context, type, value) : IsInteger(type) ? FromInteger(context, type, value) : IsIntersect(type) ? FromIntersect$2(context, type, value) : IsLiteral(type) ? FromLiteral(context, type, value) : IsNull(type) ? FromNull(context, type, value) : IsNumber(type) ? FromNumber(context, type, value) : IsObject(type) ? FromObject$2(context, type, value) : IsRecord(type) ? FromRecord$2(context, type, value) : IsRef$1(type) ? FromRef$2(context, type, value) : IsString(type) ? FromString(context, type, value) : IsTemplateLiteral(type) ? FromTemplateLiteral(context, type, value) : IsTuple(type) ? FromTuple$2(context, type, value) : IsUndefined(type) ? FromUndefined(context, type, value) : IsUnion(type) ? FromUnion$2(context, type, value) : IsVoid(type) ? FromVoid(context, type, value) : value;
|
|
5840
|
-
}
|
|
5841
|
-
function Convert(...args) {
|
|
5842
|
-
const [context, type, value] = Match$3(args, {
|
|
5843
|
-
3: (context, type, value) => [
|
|
5844
|
-
context,
|
|
5845
|
-
type,
|
|
5846
|
-
value
|
|
5847
|
-
],
|
|
5848
|
-
2: (type, value) => [
|
|
5849
|
-
{},
|
|
5850
|
-
type,
|
|
5851
|
-
value
|
|
5852
|
-
]
|
|
5853
|
-
});
|
|
5854
|
-
return FromType$2(context, type, value);
|
|
5855
|
-
}
|
|
5623
|
+
BigInt(Number.MAX_SAFE_INTEGER);
|
|
5624
|
+
BigInt(Number.MIN_SAFE_INTEGER);
|
|
5856
5625
|
Union([
|
|
5857
5626
|
_Object_$1({
|
|
5858
5627
|
type: Literal("insert"),
|
|
@@ -5869,6 +5638,6 @@ Union([
|
|
|
5869
5638
|
path: String$2()
|
|
5870
5639
|
})
|
|
5871
5640
|
]);
|
|
5872
|
-
export { Check,
|
|
5641
|
+
export { Check, Errors, type_exports };
|
|
5873
5642
|
|
|
5874
5643
|
//# sourceMappingURL=typebox.mjs.map
|