@agen-ai/agent-protocol 0.4.2 → 0.4.4

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.
Files changed (52) hide show
  1. package/README.md +151 -21
  2. package/dist/capabilities/types.d.ts +3 -1
  3. package/dist/events/types.d.ts +1 -1
  4. package/dist/foundation/types.d.ts +2 -1
  5. package/dist/foundation/types.js +1 -1
  6. package/dist/jsonSchema/generated.d.ts +13637 -9890
  7. package/dist/jsonSchema/generated.js +3025 -644
  8. package/dist/public/index.d.ts +1 -0
  9. package/dist/public/index.js +1 -0
  10. package/dist/public/structuredOutput.d.ts +2 -0
  11. package/dist/public/structuredOutput.js +1 -0
  12. package/dist/sessions/types.d.ts +2 -0
  13. package/dist/structuredOutput/boundedJson.d.ts +13 -0
  14. package/dist/structuredOutput/boundedJson.js +143 -0
  15. package/dist/structuredOutput/constants.d.ts +24 -0
  16. package/dist/structuredOutput/constants.js +57 -0
  17. package/dist/structuredOutput/definition.d.ts +9 -0
  18. package/dist/structuredOutput/definition.js +15 -0
  19. package/dist/structuredOutput/index.d.ts +9 -0
  20. package/dist/structuredOutput/index.js +12 -0
  21. package/dist/structuredOutput/issues.d.ts +8 -0
  22. package/dist/structuredOutput/issues.js +76 -0
  23. package/dist/structuredOutput/parsers.d.ts +10 -0
  24. package/dist/structuredOutput/parsers.js +74 -0
  25. package/dist/structuredOutput/schemaAdmission.d.ts +9 -0
  26. package/dist/structuredOutput/schemaAdmission.js +247 -0
  27. package/dist/structuredOutput/types.d.ts +75 -0
  28. package/dist/structuredOutput/types.js +0 -0
  29. package/dist/structuredOutput/values.d.ts +5 -0
  30. package/dist/structuredOutput/values.js +89 -0
  31. package/dist/turns/types.d.ts +18 -5
  32. package/dist/zod/capabilities.d.ts +3 -2
  33. package/dist/zod/capabilities.js +2 -0
  34. package/dist/zod/events.d.ts +24 -33
  35. package/dist/zod/events.js +94 -94
  36. package/dist/zod/foundation.d.ts +1 -0
  37. package/dist/zod/foundation.js +3 -0
  38. package/dist/zod/index.d.ts +2 -0
  39. package/dist/zod/index.js +25 -1
  40. package/dist/zod/operations.d.ts +2 -2
  41. package/dist/zod/outputDefinition.d.ts +4 -0
  42. package/dist/zod/outputDefinition.js +22 -0
  43. package/dist/zod/outputSchema.d.ts +4 -0
  44. package/dist/zod/outputSchema.js +177 -0
  45. package/dist/zod/sessions.d.ts +14 -0
  46. package/dist/zod/sessions.js +26 -3
  47. package/dist/zod/structuredOutput.d.ts +14 -0
  48. package/dist/zod/structuredOutput.js +209 -0
  49. package/dist/zod/turns.d.ts +19 -5
  50. package/dist/zod/turns.js +63 -24
  51. package/dist/zod/typeEquality.generated.d.ts +16 -2
  52. package/package.json +6 -2
@@ -0,0 +1,177 @@
1
+ import * as core from "zod/v4/core";
2
+ import { AgentProtocolValidationError } from "../foundation/types.js";
3
+ import { isOutputPropertyName, serializedStringBytes } from "../structuredOutput/boundedJson.js";
4
+ import {
5
+ AGENT_OUTPUT_SCHEMA_BYTES_LIMIT,
6
+ AGENT_OUTPUT_SCHEMA_DEPTH_LIMIT,
7
+ AGENT_OUTPUT_SCHEMA_ENUM_VALUES_LIMIT,
8
+ AGENT_OUTPUT_SCHEMA_NODES_LIMIT,
9
+ AGENT_OUTPUT_SCHEMA_PROPERTIES_LIMIT
10
+ } from "../structuredOutput/constants.js";
11
+ import { StructuredOutputIssues } from "../structuredOutput/issues.js";
12
+ const sourceChecksLimit = 256;
13
+ function unsupported(message, path) {
14
+ const issues = new StructuredOutputIssues();
15
+ issues.add("unsupported_output_schema", message, path);
16
+ throw new AgentProtocolValidationError(issues.issues);
17
+ }
18
+ function finiteNumber(value, path) {
19
+ if (typeof value !== "number" || !Number.isFinite(value)) {
20
+ return unsupported("Output schema bounds must be finite numbers.", path);
21
+ }
22
+ return value;
23
+ }
24
+ function emitNumber(schema, entries, path) {
25
+ let integer = false;
26
+ let minimum;
27
+ let maximum;
28
+ function numberFormat(format) {
29
+ if (format !== "safeint") unsupported("Only the safe-integer Zod numeric format is supported.", path);
30
+ integer = true;
31
+ minimum = Math.max(minimum ?? Number.MIN_SAFE_INTEGER, Number.MIN_SAFE_INTEGER);
32
+ maximum = Math.min(maximum ?? Number.MAX_SAFE_INTEGER, Number.MAX_SAFE_INTEGER);
33
+ }
34
+ if (schema._zod.def.coerce) unsupported("Zod coercion is unsupported; supply a value-preserving neutral output definition.", path);
35
+ if (schema instanceof core.$ZodNumberFormat) {
36
+ if (schema._zod.def.when !== void 0) unsupported("Conditional numeric checks cannot be represented by the portable output schema.", path);
37
+ numberFormat(schema._zod.def.format);
38
+ }
39
+ for (const check of entries) {
40
+ if (check._zod.def.when !== void 0) unsupported("Conditional numeric checks cannot be represented by the portable output schema.", path);
41
+ if (check instanceof core.$ZodCheckGreaterThan && check._zod.def.inclusive === true) {
42
+ minimum = Math.max(minimum ?? -Infinity, finiteNumber(check._zod.def.value, path));
43
+ } else if (check instanceof core.$ZodCheckLessThan && check._zod.def.inclusive === true) {
44
+ maximum = Math.min(maximum ?? Infinity, finiteNumber(check._zod.def.value, path));
45
+ } else if (check instanceof core.$ZodCheckNumberFormat) numberFormat(check._zod.def.format);
46
+ else unsupported("Only inclusive numeric bounds and safe-integer checks are supported.", path);
47
+ }
48
+ if (minimum !== void 0 && maximum !== void 0 && minimum > maximum) {
49
+ unsupported("Output schema numeric bounds are contradictory.", path);
50
+ }
51
+ return {
52
+ type: integer ? "integer" : "number",
53
+ ...minimum === void 0 ? {} : { minimum },
54
+ ...maximum === void 0 ? {} : { maximum }
55
+ };
56
+ }
57
+ function emitZodOutputSchema(root) {
58
+ const active = /* @__PURE__ */ new Set();
59
+ let nodes = 0;
60
+ let checks = 0;
61
+ let stringBytes = 0;
62
+ function chargeNode(depth, path) {
63
+ nodes += 1;
64
+ if (nodes > AGENT_OUTPUT_SCHEMA_NODES_LIMIT) unsupported("Expanded output schema exceeds the schema node limit.", path);
65
+ if (depth > AGENT_OUTPUT_SCHEMA_DEPTH_LIMIT) unsupported("Output schema exceeds the schema depth limit.", path);
66
+ }
67
+ function chargeString(value, path) {
68
+ if (value.length > AGENT_OUTPUT_SCHEMA_BYTES_LIMIT) unsupported("Output schema exceeds the serialized UTF-8 byte limit.", path);
69
+ stringBytes += serializedStringBytes(value, AGENT_OUTPUT_SCHEMA_BYTES_LIMIT - stringBytes);
70
+ if (stringBytes > AGENT_OUTPUT_SCHEMA_BYTES_LIMIT) unsupported("Output schema exceeds the serialized UTF-8 byte limit.", path);
71
+ }
72
+ function emitEnum(schema, path) {
73
+ const values = [];
74
+ function append(value) {
75
+ if (values.length === AGENT_OUTPUT_SCHEMA_ENUM_VALUES_LIMIT) unsupported("Output enum exceeds the entry limit.", path);
76
+ if (typeof value !== "string") unsupported("Only string enum and string literal values are supported.", path);
77
+ chargeString(value, path);
78
+ values.push(value);
79
+ }
80
+ if (schema instanceof core.$ZodEnum) {
81
+ const entries = schema._zod.def.entries;
82
+ for (const key in entries) {
83
+ if (!Object.hasOwn(entries, key)) continue;
84
+ const descriptor = Object.getOwnPropertyDescriptor(entries, key);
85
+ if (!descriptor || !("value" in descriptor)) unsupported("Zod enum values must be data properties.", path);
86
+ append(descriptor.value);
87
+ }
88
+ } else {
89
+ const entries = schema._zod.def.values;
90
+ if (entries.length > AGENT_OUTPUT_SCHEMA_ENUM_VALUES_LIMIT) unsupported("Output enum exceeds the entry limit.", path);
91
+ for (const entry of entries) append(entry);
92
+ }
93
+ const [first, ...rest] = values;
94
+ if (first === void 0) unsupported("Output string enums must contain at least one value.", path);
95
+ return { type: "string", enum: [first, ...rest] };
96
+ }
97
+ function visit(schema, depth, path) {
98
+ chargeNode(depth, path);
99
+ if (!(schema instanceof core.$ZodType)) unsupported("Output schema children must be Zod core schemas.", path);
100
+ if (active.has(schema)) unsupported("Recursive Zod schemas are unsupported.", path);
101
+ active.add(schema);
102
+ try {
103
+ const entries = schema._zod.def.checks ?? [];
104
+ checks += entries.length;
105
+ if (checks > sourceChecksLimit) unsupported("Expanded output schema exceeds the source check limit.", path);
106
+ for (const entry of entries) {
107
+ if (!(entry instanceof core.$ZodCheck)) unsupported("Unrecognized Zod checks are unsupported.", path);
108
+ }
109
+ if (schema instanceof core.$ZodString) {
110
+ if (schema._zod.def.coerce) unsupported("Zod coercion is unsupported; supply a value-preserving neutral output definition.", path);
111
+ if (schema instanceof core.$ZodStringFormat) unsupported("Zod string formats are unsupported by the portable output schema.", path);
112
+ if (entries.some((check) => check instanceof core.$ZodCheckMinLength || check instanceof core.$ZodCheckMaxLength || check instanceof core.$ZodCheckLengthEquals)) {
113
+ unsupported("Zod string lengths count UTF-16 units. Use createAgentOutputDefinition with JSON Schema minLength/maxLength for code-point bounds, or an explicit local validation-only decoder.", path);
114
+ }
115
+ if (entries.length > 0) unsupported("Zod string checks, refinements and overwrites are unsupported.", path);
116
+ return { type: "string" };
117
+ }
118
+ if (schema instanceof core.$ZodNumber) return emitNumber(schema, entries, path);
119
+ if (schema instanceof core.$ZodBoolean) {
120
+ if (schema._zod.def.coerce) unsupported("Zod coercion is unsupported; supply a value-preserving neutral output definition.", path);
121
+ if (entries.length > 0) unsupported("Zod boolean checks are unsupported.", path);
122
+ return { type: "boolean" };
123
+ }
124
+ if (schema instanceof core.$ZodNull) {
125
+ if (entries.length > 0) unsupported("Zod null checks are unsupported.", path);
126
+ return { type: "null" };
127
+ }
128
+ if (schema instanceof core.$ZodEnum || schema instanceof core.$ZodLiteral) {
129
+ if (entries.length > 0) unsupported("Zod enum and literal checks are unsupported.", path);
130
+ return emitEnum(schema, path);
131
+ }
132
+ if (schema instanceof core.$ZodArray) {
133
+ if (entries.length > 0) {
134
+ unsupported("Zod array checks have opaque conditions. Use createAgentOutputDefinition with JSON Schema minItems/maxItems, or an explicit local validation-only decoder.", path);
135
+ }
136
+ return { type: "array", items: visit(schema._zod.def.element, depth + 1, [...path, "items"]) };
137
+ }
138
+ if (schema instanceof core.$ZodNullable) {
139
+ if (entries.length > 0) unsupported("Zod nullable checks are unsupported.", path);
140
+ const inner = visit(schema._zod.def.innerType, depth + 1, [...path, "anyOf", 0]);
141
+ if ("anyOf" in inner || inner.type === "null") unsupported("Nullable output requires one non-null, non-nullable inner schema.", path);
142
+ chargeNode(depth + 1, [...path, "anyOf", 1]);
143
+ return { anyOf: [inner, { type: "null" }] };
144
+ }
145
+ if (schema instanceof core.$ZodObject) {
146
+ if (entries.length > 0) unsupported("Zod object checks and refinements are unsupported.", path);
147
+ if (!(schema._zod.def.catchall instanceof core.$ZodNever)) unsupported("Output objects must be strict. Use z.strictObject or .strict().", path);
148
+ const shape = schema._zod.def.shape;
149
+ const properties = {};
150
+ const required = [];
151
+ for (const key in shape) {
152
+ if (!Object.hasOwn(shape, key)) continue;
153
+ const childPath = [...path, "properties", key];
154
+ if (required.length === AGENT_OUTPUT_SCHEMA_PROPERTIES_LIMIT) unsupported("Output object exceeds the property limit.", path);
155
+ if (!isOutputPropertyName(key)) unsupported("Output object property names must be bounded, nonempty and safe.", path);
156
+ chargeString(key, childPath);
157
+ const descriptor = Object.getOwnPropertyDescriptor(shape, key);
158
+ if (!descriptor || !("value" in descriptor)) unsupported("Resolved Zod object shapes must contain data properties.", childPath);
159
+ const child = descriptor.value;
160
+ if (!(child instanceof core.$ZodType)) unsupported("Output schema children must be Zod core schemas.", childPath);
161
+ properties[key] = visit(child, depth + 1, childPath);
162
+ required.push(key);
163
+ }
164
+ return { type: "object", properties, required, additionalProperties: false };
165
+ }
166
+ return unsupported("This Zod schema type is unsupported. Use a finite strict output object without optional fields, wrappers, transforms or lazy schemas.", path);
167
+ } finally {
168
+ active.delete(schema);
169
+ }
170
+ }
171
+ const result = visit(root, 1, []);
172
+ if (!("type" in result) || result.type !== "object") unsupported("Output schema root must be a strict Zod object.", []);
173
+ return result;
174
+ }
175
+ export {
176
+ emitZodOutputSchema
177
+ };
@@ -29,6 +29,13 @@ export declare const AgentSessionConfigurationPortableSchema: z.ZodDiscriminated
29
29
  export declare const AgentSessionConfigurationSchema: z.ZodType<AgentSessionConfiguration>;
30
30
  export declare const AgentSessionCreateInputPortableSchema: z.ZodReadonly<z.ZodObject<{
31
31
  operation: z.ZodLiteral<"create">;
32
+ output: z.ZodOptional<z.ZodType<Readonly<{
33
+ kind: "json_schema";
34
+ schema: import("../index.js").AgentOutputSchema;
35
+ }>, unknown, z.core.$ZodTypeInternals<Readonly<{
36
+ kind: "json_schema";
37
+ schema: import("../index.js").AgentOutputSchema;
38
+ }>, unknown>>>;
32
39
  sessionId: z.ZodType<import("../index.js").AgentSessionId, unknown, z.core.$ZodTypeInternals<import("../index.js").AgentSessionId, unknown>>;
33
40
  configuration: z.ZodDiscriminatedUnion<[z.ZodReadonly<z.ZodObject<{
34
41
  kind: z.ZodLiteral<"managed">;
@@ -119,6 +126,13 @@ export declare const AgentSessionBranchInputPortableSchema: z.ZodReadonly<z.ZodO
119
126
  }, z.core.$strict>>;
120
127
  declare const AgentSessionOpenInputPortableSchema: z.ZodDiscriminatedUnion<[z.ZodReadonly<z.ZodObject<{
121
128
  operation: z.ZodLiteral<"create">;
129
+ output: z.ZodOptional<z.ZodType<Readonly<{
130
+ kind: "json_schema";
131
+ schema: import("../index.js").AgentOutputSchema;
132
+ }>, unknown, z.core.$ZodTypeInternals<Readonly<{
133
+ kind: "json_schema";
134
+ schema: import("../index.js").AgentOutputSchema;
135
+ }>, unknown>>>;
122
136
  sessionId: z.ZodType<import("../index.js").AgentSessionId, unknown, z.core.$ZodTypeInternals<import("../index.js").AgentSessionId, unknown>>;
123
137
  configuration: z.ZodDiscriminatedUnion<[z.ZodReadonly<z.ZodObject<{
124
138
  kind: z.ZodLiteral<"managed">;
@@ -1,6 +1,11 @@
1
1
  import { z } from "zod/v4";
2
+ import {
3
+ AgentStructuredOutputRequestPortableSchema,
4
+ AgentStructuredOutputRequestSchema
5
+ } from "./structuredOutput.js";
2
6
  import { compareStringsByUnicodeCodePoint } from "../foundation/ordering.js";
3
7
  import {
8
+ AGENT_PROTOCOL_COLLECTION_MAX_LENGTH,
4
9
  AGENT_PROTOCOL_JSON_BYTES_LIMIT,
5
10
  agentProtocolSerializedJsonBytes
6
11
  } from "../foundation/types.js";
@@ -11,7 +16,8 @@ import {
11
16
  AgentProviderConversationIdSchema,
12
17
  AgentProviderHistoryAnchorSchema,
13
18
  AgentSessionIdSchema,
14
- AgentTurnIdSchema
19
+ AgentTurnIdSchema,
20
+ withAcyclicProtocolInput
15
21
  } from "./foundation.js";
16
22
  import { AgentConfigurationValueSchema } from "./configuration.js";
17
23
  const PositiveSafeIntegerSchema = z.number().int().positive().max(Number.MAX_SAFE_INTEGER);
@@ -73,6 +79,7 @@ const AgentSessionConfigurationSchema = AgentSessionConfigurationPortableSchema.
73
79
  });
74
80
  const AgentSessionCreateInputPortableSchema = z.object({
75
81
  operation: z.literal("create"),
82
+ output: AgentStructuredOutputRequestPortableSchema.optional(),
76
83
  sessionId: AgentSessionIdSchema,
77
84
  configuration: AgentSessionConfigurationPortableSchema
78
85
  }).strict().readonly();
@@ -116,10 +123,26 @@ function validateSessionOpenInput(input, context) {
116
123
  });
117
124
  }
118
125
  }
119
- const AgentSessionCreateInputSchema = AgentSessionCreateInputPortableSchema.superRefine(validateSessionOpenInput);
126
+ const AgentSessionCreateInputFullSchema = AgentSessionCreateInputPortableSchema.unwrap().extend({ output: AgentStructuredOutputRequestSchema.optional() }).readonly();
127
+ const sessionOpenPreflight = {
128
+ maxDepth: 64,
129
+ maxNodes: AGENT_PROTOCOL_JSON_BYTES_LIMIT * 2,
130
+ maxCollectionLength: AGENT_PROTOCOL_COLLECTION_MAX_LENGTH
131
+ };
132
+ const AgentSessionCreateInputSchema = withAcyclicProtocolInput(
133
+ AgentSessionCreateInputFullSchema.superRefine(validateSessionOpenInput),
134
+ sessionOpenPreflight
135
+ );
120
136
  const AgentSessionResumeInputSchema = AgentSessionResumeInputPortableSchema.superRefine(validateSessionOpenInput);
121
137
  const AgentSessionBranchInputSchema = AgentSessionBranchInputPortableSchema.superRefine(validateSessionOpenInput);
122
- const AgentSessionOpenInputSchema = AgentSessionOpenInputPortableSchema.superRefine(validateSessionOpenInput);
138
+ const AgentSessionOpenInputSchema = withAcyclicProtocolInput(
139
+ z.discriminatedUnion("operation", [
140
+ AgentSessionCreateInputFullSchema,
141
+ AgentSessionResumeInputPortableSchema,
142
+ AgentSessionBranchInputPortableSchema
143
+ ]).superRefine(validateSessionOpenInput),
144
+ sessionOpenPreflight
145
+ );
123
146
  export {
124
147
  AgentSessionBindingSchema,
125
148
  AgentSessionBranchInputPortableSchema,
@@ -0,0 +1,14 @@
1
+ import { z } from 'zod/v4';
2
+ import type { AgentOutputSchema, AgentOutputSchemaNode, AgentStructuredOutputCapability, AgentStructuredOutputNonJsonResult, AgentStructuredOutputRequest, AgentStructuredOutputResult } from '../structuredOutput/types.js';
3
+ export declare const AgentOutputSchemaNodePortableSchema: z.ZodType<AgentOutputSchemaNode>;
4
+ export declare const AgentOutputSchemaPortableSchema: z.ZodType<AgentOutputSchema>;
5
+ export declare const AgentOutputSchemaSchema: z.ZodType<AgentOutputSchema>;
6
+ export declare const AgentStructuredOutputCapabilityPortableSchema: z.ZodType<AgentStructuredOutputCapability>;
7
+ export declare const AgentStructuredOutputCapabilitySchema: z.ZodType<AgentStructuredOutputCapability, unknown, z.core.$ZodTypeInternals<AgentStructuredOutputCapability, unknown>>;
8
+ export declare const AgentStructuredOutputRequestPortableSchema: z.ZodType<AgentStructuredOutputRequest>;
9
+ export declare const AgentStructuredOutputRequestSchema: z.ZodType<AgentStructuredOutputRequest>;
10
+ export declare const AgentStructuredOutputNonJsonResultPortableSchema: z.ZodType<AgentStructuredOutputNonJsonResult>;
11
+ export declare const AgentStructuredOutputResultPortableSchema: z.ZodType<AgentStructuredOutputResult>;
12
+ export declare const AgentStructuredOutputNonJsonResultSchema: z.ZodType<AgentStructuredOutputNonJsonResult>;
13
+ export declare const AgentStructuredOutputResultSchema: z.ZodType<AgentStructuredOutputResult>;
14
+ //# sourceMappingURL=structuredOutput.d.ts.map
@@ -0,0 +1,209 @@
1
+ import { z } from "zod/v4";
2
+ import {
3
+ AGENT_OUTPUT_SCHEMA_BYTES_LIMIT,
4
+ AGENT_OUTPUT_SCHEMA_DEPTH_LIMIT,
5
+ AGENT_OUTPUT_SCHEMA_DESCRIPTION_MAX_LENGTH,
6
+ AGENT_OUTPUT_SCHEMA_DIALECT,
7
+ AGENT_OUTPUT_SCHEMA_ENUM_VALUES_LIMIT,
8
+ AGENT_OUTPUT_SCHEMA_LENGTH_BOUND_LIMIT,
9
+ AGENT_OUTPUT_SCHEMA_PROFILE,
10
+ AGENT_OUTPUT_SCHEMA_PROPERTIES_LIMIT,
11
+ AGENT_OUTPUT_SCHEMA_PROPERTY_NAME_MAX_LENGTH,
12
+ AGENT_OUTPUT_VALUE_BYTES_LIMIT,
13
+ AGENT_OUTPUT_VALUE_COLLECTION_LIMIT,
14
+ AGENT_OUTPUT_VALUE_DEPTH_LIMIT,
15
+ AGENT_OUTPUT_VALUE_NODES_LIMIT,
16
+ AGENT_STRUCTURED_OUTPUT_ISSUE_CODE_MAX_LENGTH,
17
+ AGENT_STRUCTURED_OUTPUT_ISSUE_MESSAGE_MAX_LENGTH,
18
+ AGENT_STRUCTURED_OUTPUT_ISSUE_PATH_BYTES_LIMIT,
19
+ AGENT_STRUCTURED_OUTPUT_ISSUE_PATH_SEGMENTS_LIMIT,
20
+ AGENT_STRUCTURED_OUTPUT_ISSUES_BYTES_LIMIT,
21
+ AGENT_STRUCTURED_OUTPUT_ISSUES_LIMIT
22
+ } from "../structuredOutput/constants.js";
23
+ import { snapshotStructuredOutputJson } from "../structuredOutput/boundedJson.js";
24
+ import { safeParseAgentOutputSchema } from "../structuredOutput/parsers.js";
25
+ const encoder = new TextEncoder();
26
+ const PropertyNameSchema = z.string().regex(new RegExp(`^(?!(?:__proto__|prototype|constructor)(?![\\s\\S]))[\\s\\S]{1,${AGENT_OUTPUT_SCHEMA_PROPERTY_NAME_MAX_LENGTH}}(?![\\s\\S])`, "u"));
27
+ const DescriptionSchema = z.string().regex(new RegExp(`^[\\s\\S]{0,${AGENT_OUTPUT_SCHEMA_DESCRIPTION_MAX_LENGTH}}(?![\\s\\S])`, "u"));
28
+ const LengthBoundSchema = z.number().int().min(0).max(AGENT_OUTPUT_SCHEMA_LENGTH_BOUND_LIMIT);
29
+ const objectShape = {
30
+ type: z.literal("object"),
31
+ properties: z.record(PropertyNameSchema, z.lazy(() => AgentOutputSchemaNodePortableSchema)).readonly(),
32
+ required: z.array(PropertyNameSchema).max(AGENT_OUTPUT_SCHEMA_PROPERTIES_LIMIT).readonly(),
33
+ additionalProperties: z.literal(false),
34
+ description: DescriptionSchema.optional()
35
+ };
36
+ const NullNodeSchema = z.object({
37
+ type: z.literal("null"),
38
+ description: DescriptionSchema.optional()
39
+ }).strict().readonly();
40
+ const NonNullNodeSchema = z.lazy(() => z.union([
41
+ z.object(objectShape).strict().readonly(),
42
+ z.object({
43
+ type: z.literal("array"),
44
+ items: AgentOutputSchemaNodePortableSchema,
45
+ minItems: LengthBoundSchema.optional(),
46
+ maxItems: LengthBoundSchema.optional(),
47
+ description: DescriptionSchema.optional()
48
+ }).strict().readonly(),
49
+ z.object({
50
+ type: z.literal("string"),
51
+ enum: z.tuple([z.string()], z.string()).check(z.maxLength(AGENT_OUTPUT_SCHEMA_ENUM_VALUES_LIMIT)).readonly().optional(),
52
+ minLength: LengthBoundSchema.optional(),
53
+ maxLength: LengthBoundSchema.optional(),
54
+ description: DescriptionSchema.optional()
55
+ }).strict().readonly(),
56
+ z.object({
57
+ type: z.enum(["integer", "number"]),
58
+ minimum: z.number().finite().optional(),
59
+ maximum: z.number().finite().optional(),
60
+ description: DescriptionSchema.optional()
61
+ }).strict().readonly(),
62
+ z.object({ type: z.literal("boolean"), description: DescriptionSchema.optional() }).strict().readonly()
63
+ ]));
64
+ const NullableNodeSchema = z.union([
65
+ z.object({
66
+ anyOf: z.tuple([NonNullNodeSchema, NullNodeSchema]).readonly(),
67
+ description: DescriptionSchema.optional()
68
+ }).strict().readonly(),
69
+ z.object({
70
+ anyOf: z.tuple([NullNodeSchema, NonNullNodeSchema]).readonly(),
71
+ description: DescriptionSchema.optional()
72
+ }).strict().transform((value) => Object.freeze({
73
+ anyOf: Object.freeze([value.anyOf[1], value.anyOf[0]]),
74
+ ...value.description === void 0 ? {} : { description: value.description }
75
+ }))
76
+ ]);
77
+ const AgentOutputSchemaNodePortableSchema = z.lazy(() => z.union([
78
+ NonNullNodeSchema,
79
+ NullNodeSchema,
80
+ NullableNodeSchema
81
+ ]));
82
+ const AgentOutputSchemaPortableSchema = z.object({
83
+ ...objectShape,
84
+ $schema: z.literal(AGENT_OUTPUT_SCHEMA_DIALECT).optional()
85
+ }).strict().readonly();
86
+ const AgentOutputSchemaSchema = z.preprocess((input, context) => {
87
+ const parsed = safeParseAgentOutputSchema(input);
88
+ if (parsed.success) return parsed.data;
89
+ for (const issue of parsed.issues) context.addIssue({ code: "custom", path: [...issue.path], message: issue.message });
90
+ return z.NEVER;
91
+ }, AgentOutputSchemaPortableSchema);
92
+ const AgentStructuredOutputCapabilityPortableSchema = z.discriminatedUnion("kind", [
93
+ z.object({ kind: z.literal("unsupported") }).strict().readonly(),
94
+ z.object({
95
+ kind: z.literal("supported"),
96
+ mechanism: z.literal("native"),
97
+ schemaProfile: z.literal(AGENT_OUTPUT_SCHEMA_PROFILE),
98
+ scope: z.enum(["turn", "session"]),
99
+ maxSchemaBytes: z.number().int().positive().max(AGENT_OUTPUT_SCHEMA_BYTES_LIMIT),
100
+ maxOutputBytes: z.number().int().positive().max(AGENT_OUTPUT_VALUE_BYTES_LIMIT),
101
+ resume: z.literal(false),
102
+ branch: z.literal(false)
103
+ }).strict().readonly()
104
+ ]);
105
+ function boundedJsonSchema(schema, bounds) {
106
+ return z.preprocess((input, context) => {
107
+ const parsed = snapshotStructuredOutputJson(input, bounds);
108
+ if (parsed.success) return parsed.data;
109
+ for (const issue of parsed.issues) context.addIssue({ code: "custom", path: [...issue.path], message: issue.message });
110
+ return z.NEVER;
111
+ }, schema);
112
+ }
113
+ const AgentStructuredOutputCapabilitySchema = boundedJsonSchema(AgentStructuredOutputCapabilityPortableSchema, {
114
+ maxBytes: 1024,
115
+ maxDepth: 2,
116
+ maxNodes: 16,
117
+ maxCollectionLength: 16
118
+ });
119
+ const AgentStructuredOutputRequestPortableSchema = z.object({
120
+ kind: z.literal("json_schema"),
121
+ schema: AgentOutputSchemaPortableSchema
122
+ }).strict().readonly();
123
+ const AgentStructuredOutputRequestSchema = boundedJsonSchema(z.object({
124
+ kind: z.literal("json_schema"),
125
+ schema: AgentOutputSchemaSchema
126
+ }).strict().readonly(), {
127
+ maxBytes: AGENT_OUTPUT_SCHEMA_BYTES_LIMIT + 128,
128
+ maxDepth: 3 * AGENT_OUTPUT_SCHEMA_DEPTH_LIMIT + 3,
129
+ maxNodes: AGENT_OUTPUT_SCHEMA_BYTES_LIMIT + 4,
130
+ maxCollectionLength: AGENT_OUTPUT_SCHEMA_PROPERTIES_LIMIT
131
+ });
132
+ const JsonValuePortableSchema = z.lazy(() => z.union([
133
+ z.string(),
134
+ z.number().finite(),
135
+ z.boolean(),
136
+ z.null(),
137
+ z.array(JsonValuePortableSchema).max(AGENT_OUTPUT_VALUE_COLLECTION_LIMIT).readonly(),
138
+ z.record(PropertyNameSchema, JsonValuePortableSchema).readonly()
139
+ ]));
140
+ const JsonObjectPortableSchema = z.record(PropertyNameSchema, JsonValuePortableSchema).readonly();
141
+ const IssuePortableSchema = z.object({
142
+ code: z.string().regex(new RegExp(`^[\\s\\S]{0,${AGENT_STRUCTURED_OUTPUT_ISSUE_CODE_MAX_LENGTH}}(?![\\s\\S])`, "u")),
143
+ message: z.string().regex(new RegExp(`^[\\s\\S]{0,${AGENT_STRUCTURED_OUTPUT_ISSUE_MESSAGE_MAX_LENGTH}}(?![\\s\\S])`, "u")),
144
+ path: z.array(z.union([z.string(), z.number().finite()])).max(AGENT_STRUCTURED_OUTPUT_ISSUE_PATH_SEGMENTS_LIMIT).readonly()
145
+ }).strict().readonly();
146
+ const NotRequestedSchema = z.object({ kind: z.literal("not_requested") }).strict().readonly();
147
+ const SchemaMismatchSchema = z.object({
148
+ kind: z.literal("unavailable"),
149
+ reason: z.literal("schema_mismatch"),
150
+ issues: z.array(IssuePortableSchema).max(AGENT_STRUCTURED_OUTPUT_ISSUES_LIMIT).readonly().optional()
151
+ }).strict().readonly();
152
+ const UnavailableSchema = z.object({
153
+ kind: z.literal("unavailable"),
154
+ reason: z.enum(["refused", "incomplete", "missing_output", "invalid_json", "output_limit_exceeded", "native_retries_exhausted", "provider_error"])
155
+ }).strict().readonly();
156
+ const AgentStructuredOutputNonJsonResultPortableSchema = z.union([
157
+ NotRequestedSchema,
158
+ SchemaMismatchSchema,
159
+ UnavailableSchema
160
+ ]);
161
+ const AgentStructuredOutputResultPortableSchema = z.union([
162
+ AgentStructuredOutputNonJsonResultPortableSchema,
163
+ z.object({ kind: z.literal("json"), value: JsonObjectPortableSchema }).strict().readonly()
164
+ ]);
165
+ const resultBounds = {
166
+ maxBytes: AGENT_OUTPUT_VALUE_BYTES_LIMIT + AGENT_STRUCTURED_OUTPUT_ISSUES_BYTES_LIMIT + 1024,
167
+ maxDepth: AGENT_OUTPUT_VALUE_DEPTH_LIMIT + 1,
168
+ maxNodes: AGENT_OUTPUT_VALUE_NODES_LIMIT + 128,
169
+ maxCollectionLength: AGENT_OUTPUT_VALUE_COLLECTION_LIMIT
170
+ };
171
+ function refineResult(result, context) {
172
+ if (result.kind === "json") {
173
+ const parsed = snapshotStructuredOutputJson(result.value, {
174
+ maxBytes: AGENT_OUTPUT_VALUE_BYTES_LIMIT,
175
+ maxDepth: AGENT_OUTPUT_VALUE_DEPTH_LIMIT,
176
+ maxNodes: AGENT_OUTPUT_VALUE_NODES_LIMIT,
177
+ maxCollectionLength: AGENT_OUTPUT_VALUE_COLLECTION_LIMIT
178
+ });
179
+ if (!parsed.success) {
180
+ for (const issue of parsed.issues) context.addIssue({ code: "custom", path: ["value", ...issue.path], message: issue.message });
181
+ }
182
+ } else if (result.kind === "unavailable" && result.reason === "schema_mismatch" && result.issues) {
183
+ if (encoder.encode(JSON.stringify(result.issues)).byteLength > AGENT_STRUCTURED_OUTPUT_ISSUES_BYTES_LIMIT) context.addIssue({ code: "custom", path: ["issues"], message: "Structured validation issues exceed their aggregate byte limit." });
184
+ for (const [index, issue] of result.issues.entries()) {
185
+ if (encoder.encode(JSON.stringify(issue.path)).byteLength > AGENT_STRUCTURED_OUTPUT_ISSUE_PATH_BYTES_LIMIT) context.addIssue({ code: "custom", path: ["issues", index, "path"], message: "Structured validation issue path exceeds its serialized byte limit." });
186
+ }
187
+ }
188
+ }
189
+ const AgentStructuredOutputNonJsonResultSchema = boundedJsonSchema(
190
+ AgentStructuredOutputNonJsonResultPortableSchema,
191
+ resultBounds
192
+ ).superRefine(refineResult);
193
+ const AgentStructuredOutputResultSchema = boundedJsonSchema(
194
+ AgentStructuredOutputResultPortableSchema,
195
+ resultBounds
196
+ ).superRefine(refineResult);
197
+ export {
198
+ AgentOutputSchemaNodePortableSchema,
199
+ AgentOutputSchemaPortableSchema,
200
+ AgentOutputSchemaSchema,
201
+ AgentStructuredOutputCapabilityPortableSchema,
202
+ AgentStructuredOutputCapabilitySchema,
203
+ AgentStructuredOutputNonJsonResultPortableSchema,
204
+ AgentStructuredOutputNonJsonResultSchema,
205
+ AgentStructuredOutputRequestPortableSchema,
206
+ AgentStructuredOutputRequestSchema,
207
+ AgentStructuredOutputResultPortableSchema,
208
+ AgentStructuredOutputResultSchema
209
+ };
@@ -157,6 +157,13 @@ export declare const AgentTurnInputContentCompositionSchema: z.ZodObject<{
157
157
  export declare const AgentTurnInputContentSchema: z.ZodType<AgentTurnInputContent>;
158
158
  export declare const AgentTurnRunInputPortableSchema: z.ZodReadonly<z.ZodObject<{
159
159
  deadlineAt: z.ZodOptional<z.ZodType<import("../index.js").AgentIsoDateTime, unknown, z.core.$ZodTypeInternals<import("../index.js").AgentIsoDateTime, unknown>>>;
160
+ output: z.ZodOptional<z.ZodType<Readonly<{
161
+ kind: "json_schema";
162
+ schema: import("../index.js").AgentOutputSchema;
163
+ }>, unknown, z.core.$ZodTypeInternals<Readonly<{
164
+ kind: "json_schema";
165
+ schema: import("../index.js").AgentOutputSchema;
166
+ }>, unknown>>>;
160
167
  parts: z.ZodReadonly<z.ZodArray<z.ZodDiscriminatedUnion<[z.ZodReadonly<z.ZodObject<{
161
168
  type: z.ZodLiteral<"text">;
162
169
  text: z.ZodString;
@@ -239,15 +246,22 @@ export declare const AgentContentStreamKindSchema: z.ZodEnum<{
239
246
  }>;
240
247
  export declare const AgentPlanStepSchema: z.ZodType<AgentPlanStep>;
241
248
  export declare const AgentDiffSummarySchema: z.ZodType<AgentDiffSummary>;
242
- export declare const AgentTurnCompletedPayloadPortableSchema: z.ZodReadonly<z.ZodObject<{
249
+ export declare const AgentTurnCompletedPayloadPortableSchema: z.ZodReadonly<z.ZodDiscriminatedUnion<[z.ZodObject<{
250
+ outcome: z.ZodLiteral<"completed">;
251
+ structuredOutput: z.ZodType<import("../index.js").AgentStructuredOutputResult, unknown, z.core.$ZodTypeInternals<import("../index.js").AgentStructuredOutputResult, unknown>>;
252
+ reason: z.ZodOptional<z.ZodString>;
253
+ }, z.core.$strict>, z.ZodObject<{
254
+ outcome: z.ZodLiteral<"failed">;
255
+ structuredOutput: z.ZodType<import("../index.js").AgentStructuredOutputNonJsonResult, unknown, z.core.$ZodTypeInternals<import("../index.js").AgentStructuredOutputNonJsonResult, unknown>>;
256
+ reason: z.ZodOptional<z.ZodString>;
257
+ error: z.ZodType<import("../index.js").AgentError, unknown, z.core.$ZodTypeInternals<import("../index.js").AgentError, unknown>>;
258
+ }, z.core.$strict>, z.ZodObject<{
243
259
  outcome: z.ZodEnum<{
244
260
  canceled: "canceled";
245
- completed: "completed";
246
- failed: "failed";
247
261
  expired: "expired";
248
262
  }>;
263
+ structuredOutput: z.ZodType<import("../index.js").AgentStructuredOutputNonJsonResult, unknown, z.core.$ZodTypeInternals<import("../index.js").AgentStructuredOutputNonJsonResult, unknown>>;
249
264
  reason: z.ZodOptional<z.ZodString>;
250
- error: z.ZodOptional<z.ZodType<import("../index.js").AgentError, unknown, z.core.$ZodTypeInternals<import("../index.js").AgentError, unknown>>>;
251
- }, z.core.$strict>>;
265
+ }, z.core.$strict>], "outcome">>;
252
266
  export declare const AgentTurnCompletedPayloadSchema: z.ZodType<AgentTurnCompletedPayload>;
253
267
  //# sourceMappingURL=turns.d.ts.map
package/dist/zod/turns.js CHANGED
@@ -1,7 +1,17 @@
1
1
  import { z } from "zod/v4";
2
2
  import { AgentErrorSchema } from "./failures.js";
3
+ import { AGENT_STRUCTURED_OUTPUT_TURN_REQUEST_BYTES_LIMIT } from "../structuredOutput/constants.js";
4
+ import {
5
+ AgentStructuredOutputNonJsonResultPortableSchema,
6
+ AgentStructuredOutputNonJsonResultSchema,
7
+ AgentStructuredOutputRequestPortableSchema,
8
+ AgentStructuredOutputRequestSchema,
9
+ AgentStructuredOutputResultPortableSchema,
10
+ AgentStructuredOutputResultSchema
11
+ } from "./structuredOutput.js";
3
12
  import {
4
13
  AGENT_PROTOCOL_COLLECTION_MAX_LENGTH,
14
+ AGENT_PROTOCOL_EVENT_BYTES_LIMIT,
5
15
  AGENT_PROTOCOL_SUMMARY_MAX_LENGTH,
6
16
  AGENT_PROTOCOL_TEXT_MAX_LENGTH,
7
17
  AGENT_PROTOCOL_TURN_INPUT_CONTENT_BYTES_LIMIT,
@@ -27,6 +37,7 @@ import {
27
37
  AgentIsoDateTimeSchema,
28
38
  AgentItemIdSchema,
29
39
  AgentTurnIdSchema,
40
+ withAcyclicProtocolInput,
30
41
  createAgentCanonicalNonBlankStringSchema
31
42
  } from "./foundation.js";
32
43
  const AGENT_PROTOCOL_INLINE_IMAGE_BASE64_MAX_LENGTH = 5e5;
@@ -139,13 +150,26 @@ const AgentTurnRunInputPortableSchema = z.object({
139
150
  turnId: AgentTurnIdSchema,
140
151
  interactionMode: AgentTurnInteractionModeSchema,
141
152
  ...AgentTurnInputContentShape,
142
- deadlineAt: AgentIsoDateTimeSchema.optional()
153
+ deadlineAt: AgentIsoDateTimeSchema.optional(),
154
+ output: AgentStructuredOutputRequestPortableSchema.optional()
143
155
  }).strict().readonly();
144
- const AgentTurnRunInputSchema = AgentTurnInputContentRefinedSchema.safeExtend({
156
+ const AgentTurnRunInputSchema = withAcyclicProtocolInput(AgentTurnInputContentRefinedSchema.safeExtend({
145
157
  turnId: AgentTurnIdSchema,
146
158
  interactionMode: AgentTurnInteractionModeSchema,
147
- deadlineAt: AgentIsoDateTimeSchema.optional()
148
- }).strict().readonly();
159
+ deadlineAt: AgentIsoDateTimeSchema.optional(),
160
+ output: AgentStructuredOutputRequestSchema.optional()
161
+ }).strict().superRefine((input, context) => {
162
+ if (agentProtocolSerializedJsonBytes(input) > AGENT_STRUCTURED_OUTPUT_TURN_REQUEST_BYTES_LIMIT) {
163
+ context.addIssue({
164
+ code: "custom",
165
+ message: `Turn run input cannot exceed ${AGENT_STRUCTURED_OUTPUT_TURN_REQUEST_BYTES_LIMIT} serialized bytes.`
166
+ });
167
+ }
168
+ }).readonly(), {
169
+ maxDepth: 64,
170
+ maxNodes: AGENT_STRUCTURED_OUTPUT_TURN_REQUEST_BYTES_LIMIT,
171
+ maxCollectionLength: AGENT_PROTOCOL_COLLECTION_MAX_LENGTH
172
+ });
149
173
  const AgentTurnInterruptionInputSchema = z.object({
150
174
  turnId: AgentTurnIdSchema,
151
175
  reason: z.enum([
@@ -413,27 +437,42 @@ const AgentDiffSummarySchema = z.object({
413
437
  malformed: z.boolean().optional(),
414
438
  truncated: z.boolean().optional()
415
439
  }).strict().readonly();
416
- const AgentTurnCompletedPayloadPortableSchema = z.object({
417
- outcome: z.enum(["completed", "failed", "canceled", "expired"]),
418
- reason: z.string().max(AGENT_PROTOCOL_SUMMARY_MAX_LENGTH).optional(),
419
- error: AgentErrorSchema.optional()
420
- }).strict().readonly();
421
- const AgentTurnCompletedPayloadSchema = AgentTurnCompletedPayloadPortableSchema.superRefine((payload, context) => {
422
- if (payload.outcome === "failed" && payload.error === void 0) {
423
- context.addIssue({
424
- code: "custom",
425
- path: ["error"],
426
- message: "Failed turns require an error."
427
- });
428
- }
429
- if (payload.outcome !== "failed" && payload.error !== void 0) {
430
- context.addIssue({
431
- code: "custom",
432
- path: ["error"],
433
- message: "Only failed turns may contain an error."
434
- });
440
+ function createTurnCompletedPayloadSchema(result, nonJsonResult) {
441
+ const reason = z.string().max(AGENT_PROTOCOL_SUMMARY_MAX_LENGTH).optional();
442
+ return z.discriminatedUnion("outcome", [
443
+ z.object({
444
+ outcome: z.literal("completed"),
445
+ structuredOutput: result,
446
+ reason
447
+ }).strict(),
448
+ z.object({
449
+ outcome: z.literal("failed"),
450
+ structuredOutput: nonJsonResult,
451
+ reason,
452
+ error: AgentErrorSchema
453
+ }).strict(),
454
+ z.object({
455
+ outcome: z.enum(["canceled", "expired"]),
456
+ structuredOutput: nonJsonResult,
457
+ reason
458
+ }).strict()
459
+ ]).readonly();
460
+ }
461
+ const AgentTurnCompletedPayloadPortableSchema = createTurnCompletedPayloadSchema(
462
+ AgentStructuredOutputResultPortableSchema,
463
+ AgentStructuredOutputNonJsonResultPortableSchema
464
+ );
465
+ const AgentTurnCompletedPayloadSchema = withAcyclicProtocolInput(
466
+ createTurnCompletedPayloadSchema(
467
+ AgentStructuredOutputResultSchema,
468
+ AgentStructuredOutputNonJsonResultSchema
469
+ ),
470
+ {
471
+ maxDepth: 32,
472
+ maxNodes: AGENT_PROTOCOL_EVENT_BYTES_LIMIT,
473
+ maxCollectionLength: AGENT_PROTOCOL_COLLECTION_MAX_LENGTH
435
474
  }
436
- });
475
+ );
437
476
  export {
438
477
  AGENT_PROTOCOL_INLINE_IMAGE_BASE64_MAX_LENGTH,
439
478
  AgentBrowserActionDetailsSchema,