@rivet-dev/agentos-core 0.2.20-rc.1 → 0.2.21

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.
@@ -1,270 +0,0 @@
1
- import { zodToJsonSchema as zodV3ToJsonSchema } from "zod-to-json-schema";
2
- const OPTIONAL_WRAPPER_TYPES = new Set(["default", "optional"]);
3
- const TRANSPARENT_WRAPPER_TYPES = new Set([
4
- ...OPTIONAL_WRAPPER_TYPES,
5
- "branded",
6
- "catch",
7
- "readonly",
8
- ]);
9
- const UNSUPPORTED_TYPES = new Set([
10
- "bigint",
11
- "date",
12
- "effects",
13
- "intersection",
14
- "pipeline",
15
- "pipe",
16
- "tuple",
17
- ]);
18
- export class BindingSchemaConversionError extends Error {
19
- path;
20
- zodType;
21
- constructor(path, zodType, details) {
22
- super([
23
- `Unsupported Zod schema at ${path}: ${zodType}`,
24
- details ? `(${details})` : undefined,
25
- ]
26
- .filter(Boolean)
27
- .join(" "));
28
- this.name = "BindingSchemaConversionError";
29
- this.path = path;
30
- this.zodType = zodType;
31
- }
32
- }
33
- function getSchemaDef(schema) {
34
- if (!schema || typeof schema !== "object") {
35
- return {};
36
- }
37
- return (schema._def ??
38
- schema.def ??
39
- {});
40
- }
41
- function getInnerSchema(schema) {
42
- const def = getSchemaDef(schema);
43
- return (def.innerType ?? def.schema ?? def.type ?? def.in);
44
- }
45
- function normalizeTypeName(schema) {
46
- const def = getSchemaDef(schema);
47
- const constructorName = String(schema.constructor?.name ?? "")
48
- .replace(/^Zod/, "")
49
- .toLowerCase();
50
- if (constructorName === "discriminatedunion" || "discriminator" in def) {
51
- return "discriminatedunion";
52
- }
53
- const rawTypeName = String(def.typeName ??
54
- def.type ??
55
- schema.type ??
56
- constructorName);
57
- return rawTypeName.replace(/^Zod/, "").toLowerCase();
58
- }
59
- function displayTypeName(typeName) {
60
- switch (typeName) {
61
- case "discriminatedunion":
62
- return "discriminatedUnion";
63
- case "nativeenum":
64
- return "nativeEnum";
65
- default:
66
- return typeName;
67
- }
68
- }
69
- function getDescription(schema) {
70
- const def = getSchemaDef(schema);
71
- if (typeof def.description === "string") {
72
- return def.description;
73
- }
74
- const instanceDescription = schema.description;
75
- return typeof instanceDescription === "string"
76
- ? instanceDescription
77
- : undefined;
78
- }
79
- function joinPath(parent, segment) {
80
- return parent === "$" ? `$.${segment}` : `${parent}.${segment}`;
81
- }
82
- function metadataProducesRefs(schema) {
83
- const meta = schema.meta?.();
84
- return (typeof meta === "object" &&
85
- meta !== null &&
86
- ("id" in meta || "$ref" in meta || "$defs" in meta));
87
- }
88
- function getChecks(schema) {
89
- const checks = getSchemaDef(schema).checks;
90
- return Array.isArray(checks) ? checks : [];
91
- }
92
- function isCustomRefinement(check) {
93
- const checkDef = getSchemaDef(check);
94
- const nestedDef = getSchemaDef(check._zod);
95
- return (String(checkDef.check ?? nestedDef.check ?? "").toLowerCase() === "custom");
96
- }
97
- function validateChecks(schema, path, typeName) {
98
- if (getChecks(schema).some(isCustomRefinement)) {
99
- throw new BindingSchemaConversionError(path, displayTypeName(typeName), "custom refinements cannot be represented faithfully in JSON Schema");
100
- }
101
- }
102
- function validateSchema(schema, path) {
103
- const typeName = normalizeTypeName(schema);
104
- if (metadataProducesRefs(schema)) {
105
- throw new BindingSchemaConversionError(path, displayTypeName(typeName), "metadata that emits $ref/$defs is not supported");
106
- }
107
- if (UNSUPPORTED_TYPES.has(typeName)) {
108
- throw new BindingSchemaConversionError(path, displayTypeName(typeName));
109
- }
110
- if (typeName === "discriminatedunion") {
111
- throw new BindingSchemaConversionError(path, displayTypeName(typeName));
112
- }
113
- if (TRANSPARENT_WRAPPER_TYPES.has(typeName)) {
114
- const inner = getInnerSchema(schema);
115
- if (!inner) {
116
- throw new BindingSchemaConversionError(path, displayTypeName(typeName), "wrapper schema is missing its inner schema");
117
- }
118
- validateSchema(inner, path);
119
- return;
120
- }
121
- if (typeName === "nullable") {
122
- const inner = getInnerSchema(schema);
123
- if (!inner) {
124
- throw new BindingSchemaConversionError(path, displayTypeName(typeName), "nullable schema is missing its inner schema");
125
- }
126
- validateSchema(inner, path);
127
- return;
128
- }
129
- validateChecks(schema, path, typeName);
130
- if (typeName === "object") {
131
- const rawShape = getSchemaDef(schema).shape;
132
- const shape = typeof rawShape === "function"
133
- ? rawShape()
134
- : (rawShape ?? {});
135
- for (const [fieldName, fieldSchema] of Object.entries(shape)) {
136
- validateSchema(fieldSchema, joinPath(path, fieldName));
137
- }
138
- return;
139
- }
140
- if (typeName === "array") {
141
- const def = getSchemaDef(schema);
142
- const itemSchema = (def.element ?? def.type);
143
- if (!itemSchema) {
144
- throw new BindingSchemaConversionError(path, displayTypeName(typeName), "array schema is missing its item schema");
145
- }
146
- validateSchema(itemSchema, `${path}[]`);
147
- return;
148
- }
149
- if (typeName === "record") {
150
- const def = getSchemaDef(schema);
151
- const keySchema = def.keyType;
152
- const valueSchema = def.valueType;
153
- if (!keySchema || !valueSchema) {
154
- throw new BindingSchemaConversionError(path, displayTypeName(typeName), "record schema is missing its key or value schema");
155
- }
156
- const keyTypeName = normalizeTypeName(keySchema);
157
- if (keyTypeName !== "string" || getChecks(keySchema).length > 0) {
158
- throw new BindingSchemaConversionError(path, displayTypeName(typeName), "record keys must be unconstrained strings");
159
- }
160
- validateSchema(valueSchema, `${path}<record-value>`);
161
- return;
162
- }
163
- if (typeName === "union") {
164
- const options = getSchemaDef(schema).options;
165
- if (!Array.isArray(options) || options.length === 0) {
166
- throw new BindingSchemaConversionError(path, displayTypeName(typeName), "union schema is missing its options");
167
- }
168
- for (let index = 0; index < options.length; index += 1) {
169
- validateSchema(options[index], `${path}<union:${index}>`);
170
- }
171
- return;
172
- }
173
- if (typeName === "literal") {
174
- const def = getSchemaDef(schema);
175
- const literalValues = Array.isArray(def.values)
176
- ? def.values
177
- : "value" in def
178
- ? [def.value]
179
- : [];
180
- const [literalValue] = literalValues;
181
- if (literalValues.length !== 1 ||
182
- !("string number boolean".split(" ").includes(typeof literalValue) ||
183
- literalValue === null)) {
184
- throw new BindingSchemaConversionError(path, displayTypeName(typeName), "literal values must be JSON primitives");
185
- }
186
- return;
187
- }
188
- if (typeName === "string" ||
189
- typeName === "number" ||
190
- typeName === "boolean" ||
191
- typeName === "enum" ||
192
- typeName === "nativeenum") {
193
- return;
194
- }
195
- throw new BindingSchemaConversionError(path, displayTypeName(typeName));
196
- }
197
- function sanitizeJsonSchema(value) {
198
- if (Array.isArray(value)) {
199
- return value.map((item) => sanitizeJsonSchema(item));
200
- }
201
- if (!value || typeof value !== "object") {
202
- return value;
203
- }
204
- const record = value;
205
- const sanitized = {};
206
- for (const [key, entry] of Object.entries(record)) {
207
- if (key === "$schema") {
208
- continue;
209
- }
210
- if (key === "additionalProperties" &&
211
- entry === false &&
212
- record.type === "object") {
213
- continue;
214
- }
215
- sanitized[key] = sanitizeJsonSchema(entry);
216
- }
217
- return sanitized;
218
- }
219
- function findUnsupportedGeneratedKeyword(value, path) {
220
- if (Array.isArray(value)) {
221
- for (let index = 0; index < value.length; index += 1) {
222
- const match = findUnsupportedGeneratedKeyword(value[index], `${path}[${index}]`);
223
- if (match) {
224
- return match;
225
- }
226
- }
227
- return null;
228
- }
229
- if (!value || typeof value !== "object") {
230
- return null;
231
- }
232
- for (const [key, entry] of Object.entries(value)) {
233
- if (key === "$ref" || key === "$defs") {
234
- return { keyword: key, path };
235
- }
236
- const match = findUnsupportedGeneratedKeyword(entry, joinPath(path, key));
237
- if (match) {
238
- return match;
239
- }
240
- }
241
- return null;
242
- }
243
- function generateJsonSchema(schema) {
244
- const nativeJsonSchema = schema.toJSONSchema?.();
245
- if (nativeJsonSchema) {
246
- return nativeJsonSchema;
247
- }
248
- const generated = zodV3ToJsonSchema(schema, {
249
- $refStrategy: "none",
250
- target: "jsonSchema7",
251
- });
252
- if (!generated ||
253
- (typeof generated === "object" && Object.keys(generated).length === 0)) {
254
- throw new BindingSchemaConversionError("$", displayTypeName(normalizeTypeName(schema)), "schema cannot be converted to JSON Schema");
255
- }
256
- return generated;
257
- }
258
- export function zodToJsonSchema(schema) {
259
- validateSchema(schema, "$");
260
- const jsonSchema = generateJsonSchema(schema);
261
- const unsupportedKeyword = findUnsupportedGeneratedKeyword(jsonSchema, "$");
262
- if (unsupportedKeyword) {
263
- throw new BindingSchemaConversionError("$", displayTypeName(normalizeTypeName(schema)), `${unsupportedKeyword.keyword} emitted at ${unsupportedKeyword.path} is not supported`);
264
- }
265
- const sanitized = sanitizeJsonSchema(jsonSchema);
266
- const description = getDescription(schema);
267
- return description && typeof sanitized === "object" && sanitized !== null
268
- ? { ...sanitized, description }
269
- : sanitized;
270
- }
@@ -1,43 +0,0 @@
1
- import type { ZodType } from "zod";
2
- /** Maximum length for binding descriptions (characters). */
3
- export declare const MAX_BINDING_DESCRIPTION_LENGTH = 200;
4
- /**
5
- * A single binding that executes on the host.
6
- */
7
- export interface Binding<INPUT = any, OUTPUT = any> {
8
- /** Description shown to the agent in --help and prompt docs. Max 200 characters. */
9
- description: string;
10
- /** Zod schema for the input. Drives CLI flag generation and validation. */
11
- inputSchema: ZodType<INPUT>;
12
- /** Runs on the host when the agent invokes the binding. */
13
- execute: (input: INPUT) => Promise<OUTPUT> | OUTPUT;
14
- /** Examples included in auto-generated prompt docs. */
15
- examples?: BindingExample<INPUT>[];
16
- /** Timeout in ms. Default: 30000. */
17
- timeout?: number;
18
- }
19
- export interface BindingExample<INPUT = any> {
20
- /** Human description of what this example does. */
21
- description: string;
22
- /** The input args for the example. */
23
- input: INPUT;
24
- }
25
- /**
26
- * A named collection of bindings. Becomes a CLI binary: agentos-{name}.
27
- */
28
- export interface Bindings {
29
- /** Collection name. Must be lowercase alphanumeric + hyphens. Becomes the CLI suffix: agentos-{name}. */
30
- name: string;
31
- /** Description shown in `agentos list-bindings` and prompt docs. */
32
- description: string;
33
- /** The bindings in this collection. Keys become subcommands. */
34
- bindings: Record<string, Binding>;
35
- }
36
- /** Helper to create a binding with type inference. */
37
- export declare function binding<INPUT, OUTPUT>(def: Binding<INPUT, OUTPUT>): Binding<INPUT, OUTPUT>;
38
- /** Helper to create a named binding collection. */
39
- export declare function bindings(def: Bindings): Bindings;
40
- /**
41
- * Validate every binding collection and binding.
42
- */
43
- export declare function validateBindings(collections: Bindings[]): void;
package/dist/bindings.js DELETED
@@ -1,34 +0,0 @@
1
- /** Maximum length for binding descriptions (characters). */
2
- export const MAX_BINDING_DESCRIPTION_LENGTH = 200;
3
- /** Helper to create a binding with type inference. */
4
- export function binding(def) {
5
- return def;
6
- }
7
- /** Helper to create a named binding collection. */
8
- export function bindings(def) {
9
- return def;
10
- }
11
- const BINDING_COMMAND_NAME_RE = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
12
- function validateBindingCommandName(kind, name) {
13
- if (BINDING_COMMAND_NAME_RE.test(name)) {
14
- return;
15
- }
16
- throw new Error(`${kind} name "${name}" must be lowercase alphanumeric with optional single hyphen separators`);
17
- }
18
- /**
19
- * Validate every binding collection and binding.
20
- */
21
- export function validateBindings(collections) {
22
- for (const collection of collections) {
23
- validateBindingCommandName("Binding collection", collection.name);
24
- if (collection.description.length > MAX_BINDING_DESCRIPTION_LENGTH) {
25
- throw new Error(`Binding collection "${collection.name}" description is ${collection.description.length} characters, max is ${MAX_BINDING_DESCRIPTION_LENGTH}`);
26
- }
27
- for (const [bindingName, definition] of Object.entries(collection.bindings)) {
28
- validateBindingCommandName("Binding", bindingName);
29
- if (definition.description.length > MAX_BINDING_DESCRIPTION_LENGTH) {
30
- throw new Error(`Binding "${collection.name}/${bindingName}" description is ${definition.description.length} characters, max is ${MAX_BINDING_DESCRIPTION_LENGTH}`);
31
- }
32
- }
33
- }
34
- }
@@ -1,10 +0,0 @@
1
- export type BindingLimitsConfig = {
2
- defaultBindingTimeoutMs?: number;
3
- maxBindingTimeoutMs?: number;
4
- maxRegisteredCollections?: number;
5
- maxRegisteredBindingsPerVm?: number;
6
- maxBindingsPerCollection?: number;
7
- maxBindingSchemaBytes?: number;
8
- maxExamplesPerBinding?: number;
9
- maxBindingExampleInputBytes?: number;
10
- };