@asyra/ai-agent-runtime 0.5.1 → 0.6.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.
Files changed (262) hide show
  1. package/CHANGELOG.md +73 -0
  2. package/README.md +9 -5
  3. package/dist/action-batch.d.ts.map +1 -1
  4. package/dist/action-batch.js.map +1 -1
  5. package/dist/action-registry.d.ts +2 -2
  6. package/dist/action-registry.d.ts.map +1 -1
  7. package/dist/action-registry.js +100 -65
  8. package/dist/action-registry.js.map +1 -1
  9. package/dist/audit.d.ts.map +1 -1
  10. package/dist/audit.js.map +1 -1
  11. package/dist/batch-exchange-types.d.ts +21 -0
  12. package/dist/batch-exchange-types.d.ts.map +1 -0
  13. package/dist/batch-exchange-types.js +2 -0
  14. package/dist/batch-exchange-types.js.map +1 -0
  15. package/dist/capability-search.d.ts +46 -0
  16. package/dist/capability-search.d.ts.map +1 -0
  17. package/dist/capability-search.js +210 -0
  18. package/dist/capability-search.js.map +1 -0
  19. package/dist/index.d.ts +37 -4
  20. package/dist/index.d.ts.map +1 -1
  21. package/dist/index.js +28 -2
  22. package/dist/index.js.map +1 -1
  23. package/dist/invocation.d.ts +46 -0
  24. package/dist/invocation.d.ts.map +1 -0
  25. package/dist/invocation.js +71 -0
  26. package/dist/invocation.js.map +1 -0
  27. package/dist/json-value.d.ts +3 -0
  28. package/dist/json-value.d.ts.map +1 -0
  29. package/dist/json-value.js +50 -0
  30. package/dist/json-value.js.map +1 -0
  31. package/dist/knowledge-types.d.ts +81 -0
  32. package/dist/knowledge-types.d.ts.map +1 -0
  33. package/dist/knowledge-types.js +2 -0
  34. package/dist/knowledge-types.js.map +1 -0
  35. package/dist/knowledge.d.ts +17 -0
  36. package/dist/knowledge.d.ts.map +1 -0
  37. package/dist/knowledge.js +585 -0
  38. package/dist/knowledge.js.map +1 -0
  39. package/dist/named-result-inputs.d.ts +9 -0
  40. package/dist/named-result-inputs.d.ts.map +1 -0
  41. package/dist/named-result-inputs.js +94 -0
  42. package/dist/named-result-inputs.js.map +1 -0
  43. package/dist/node/action-observation.d.ts +15 -0
  44. package/dist/node/action-observation.d.ts.map +1 -0
  45. package/dist/node/action-observation.js +126 -0
  46. package/dist/node/action-observation.js.map +1 -0
  47. package/dist/node/batch-exchange.d.ts +19 -0
  48. package/dist/node/batch-exchange.d.ts.map +1 -0
  49. package/dist/node/batch-exchange.js +105 -0
  50. package/dist/node/batch-exchange.js.map +1 -0
  51. package/dist/node/file-record-collector.d.ts +11 -0
  52. package/dist/node/file-record-collector.d.ts.map +1 -0
  53. package/dist/node/file-record-collector.js +142 -0
  54. package/dist/node/file-record-collector.js.map +1 -0
  55. package/dist/node/index.d.ts +22 -0
  56. package/dist/node/index.d.ts.map +1 -0
  57. package/dist/node/index.js +22 -0
  58. package/dist/node/index.js.map +1 -0
  59. package/dist/node/information-delivery.d.ts +90 -0
  60. package/dist/node/information-delivery.d.ts.map +1 -0
  61. package/dist/node/information-delivery.js +144 -0
  62. package/dist/node/information-delivery.js.map +1 -0
  63. package/dist/node/knowledge-file.d.ts +22 -0
  64. package/dist/node/knowledge-file.d.ts.map +1 -0
  65. package/dist/node/knowledge-file.js +106 -0
  66. package/dist/node/knowledge-file.js.map +1 -0
  67. package/dist/node/native-provider-config.d.ts +12 -0
  68. package/dist/node/native-provider-config.d.ts.map +1 -0
  69. package/dist/node/native-provider-config.js +58 -0
  70. package/dist/node/native-provider-config.js.map +1 -0
  71. package/dist/node/native-provider.d.ts +36 -0
  72. package/dist/node/native-provider.d.ts.map +1 -0
  73. package/dist/node/native-provider.js +209 -0
  74. package/dist/node/native-provider.js.map +1 -0
  75. package/dist/node/native-rpc-transport.d.ts +39 -0
  76. package/dist/node/native-rpc-transport.d.ts.map +1 -0
  77. package/dist/node/native-rpc-transport.js +171 -0
  78. package/dist/node/native-rpc-transport.js.map +1 -0
  79. package/dist/node/native-session.d.ts +50 -0
  80. package/dist/node/native-session.d.ts.map +1 -0
  81. package/dist/node/native-session.js +200 -0
  82. package/dist/node/native-session.js.map +1 -0
  83. package/dist/node/payload.d.ts +12 -0
  84. package/dist/node/payload.d.ts.map +1 -0
  85. package/dist/node/payload.js +69 -0
  86. package/dist/node/payload.js.map +1 -0
  87. package/dist/node/provider-content.d.ts +9 -0
  88. package/dist/node/provider-content.d.ts.map +1 -0
  89. package/dist/node/provider-content.js +2 -0
  90. package/dist/node/provider-content.js.map +1 -0
  91. package/dist/node/provider-conversations.d.ts +21 -0
  92. package/dist/node/provider-conversations.d.ts.map +1 -0
  93. package/dist/node/provider-conversations.js +98 -0
  94. package/dist/node/provider-conversations.js.map +1 -0
  95. package/dist/node/record-collector.d.ts +27 -0
  96. package/dist/node/record-collector.d.ts.map +1 -0
  97. package/dist/node/record-collector.js +175 -0
  98. package/dist/node/record-collector.js.map +1 -0
  99. package/dist/node/record-sink.d.ts +4 -0
  100. package/dist/node/record-sink.d.ts.map +1 -0
  101. package/dist/node/record-sink.js +218 -0
  102. package/dist/node/record-sink.js.map +1 -0
  103. package/dist/node/report-cli.d.ts +21 -0
  104. package/dist/node/report-cli.d.ts.map +1 -0
  105. package/dist/node/report-cli.js +285 -0
  106. package/dist/node/report-cli.js.map +1 -0
  107. package/dist/node/responses-provider.d.ts +53 -0
  108. package/dist/node/responses-provider.d.ts.map +1 -0
  109. package/dist/node/responses-provider.js +542 -0
  110. package/dist/node/responses-provider.js.map +1 -0
  111. package/dist/node/responses-tool-search.d.ts +74 -0
  112. package/dist/node/responses-tool-search.d.ts.map +1 -0
  113. package/dist/node/responses-tool-search.js +100 -0
  114. package/dist/node/responses-tool-search.js.map +1 -0
  115. package/dist/node/source-identity.d.ts +10 -0
  116. package/dist/node/source-identity.d.ts.map +1 -0
  117. package/dist/node/source-identity.js +85 -0
  118. package/dist/node/source-identity.js.map +1 -0
  119. package/dist/node/sqlite-tool-store.d.ts +18 -0
  120. package/dist/node/sqlite-tool-store.d.ts.map +1 -0
  121. package/dist/node/sqlite-tool-store.js +198 -0
  122. package/dist/node/sqlite-tool-store.js.map +1 -0
  123. package/dist/node/tool-host-library.d.ts +97 -0
  124. package/dist/node/tool-host-library.d.ts.map +1 -0
  125. package/dist/node/tool-host-library.js +368 -0
  126. package/dist/node/tool-host-library.js.map +1 -0
  127. package/dist/node/tool-provider.d.ts +65 -0
  128. package/dist/node/tool-provider.d.ts.map +1 -0
  129. package/dist/node/tool-provider.js +2 -0
  130. package/dist/node/tool-provider.js.map +1 -0
  131. package/dist/profiler/assessment.d.ts +30 -0
  132. package/dist/profiler/assessment.d.ts.map +1 -0
  133. package/dist/profiler/assessment.js +111 -0
  134. package/dist/profiler/assessment.js.map +1 -0
  135. package/dist/profiler/evaluation.d.ts +288 -0
  136. package/dist/profiler/evaluation.d.ts.map +1 -0
  137. package/dist/profiler/evaluation.js +513 -0
  138. package/dist/profiler/evaluation.js.map +1 -0
  139. package/dist/profiler/record-delivery.d.ts +20 -0
  140. package/dist/profiler/record-delivery.d.ts.map +1 -0
  141. package/dist/profiler/record-delivery.js +279 -0
  142. package/dist/profiler/record-delivery.js.map +1 -0
  143. package/dist/profiler/record-outbox.d.ts +48 -0
  144. package/dist/profiler/record-outbox.d.ts.map +1 -0
  145. package/dist/profiler/record-outbox.js +126 -0
  146. package/dist/profiler/record-outbox.js.map +1 -0
  147. package/dist/profiler/recorder.d.ts +35 -0
  148. package/dist/profiler/recorder.d.ts.map +1 -0
  149. package/dist/profiler/recorder.js +864 -0
  150. package/dist/profiler/recorder.js.map +1 -0
  151. package/dist/profiler/records.d.ts +89 -0
  152. package/dist/profiler/records.d.ts.map +1 -0
  153. package/dist/profiler/records.js +216 -0
  154. package/dist/profiler/records.js.map +1 -0
  155. package/dist/profiler/timing.d.ts +21 -0
  156. package/dist/profiler/timing.d.ts.map +1 -0
  157. package/dist/profiler/timing.js +197 -0
  158. package/dist/profiler/timing.js.map +1 -0
  159. package/dist/profiler/trace.d.ts +31 -0
  160. package/dist/profiler/trace.d.ts.map +1 -0
  161. package/dist/profiler/trace.js +66 -0
  162. package/dist/profiler/trace.js.map +1 -0
  163. package/dist/provider-retry.d.ts.map +1 -1
  164. package/dist/provider-retry.js +1 -0
  165. package/dist/provider-retry.js.map +1 -1
  166. package/dist/provider.d.ts +46 -1
  167. package/dist/provider.d.ts.map +1 -1
  168. package/dist/provider.js +34 -0
  169. package/dist/provider.js.map +1 -1
  170. package/dist/providers/exchange-provider.d.ts +32 -0
  171. package/dist/providers/exchange-provider.d.ts.map +1 -0
  172. package/dist/providers/exchange-provider.js +348 -0
  173. package/dist/providers/exchange-provider.js.map +1 -0
  174. package/dist/providers/generic-http.d.ts +2 -1
  175. package/dist/providers/generic-http.d.ts.map +1 -1
  176. package/dist/providers/generic-http.js +12 -5
  177. package/dist/providers/generic-http.js.map +1 -1
  178. package/dist/recipe-admission.d.ts +5 -0
  179. package/dist/recipe-admission.d.ts.map +1 -0
  180. package/dist/recipe-admission.js +533 -0
  181. package/dist/recipe-admission.js.map +1 -0
  182. package/dist/recipe-execution.d.ts +93 -0
  183. package/dist/recipe-execution.d.ts.map +1 -0
  184. package/dist/recipe-execution.js +562 -0
  185. package/dist/recipe-execution.js.map +1 -0
  186. package/dist/recipe-format.d.ts +18 -0
  187. package/dist/recipe-format.d.ts.map +1 -0
  188. package/dist/recipe-format.js +94 -0
  189. package/dist/recipe-format.js.map +1 -0
  190. package/dist/recipe-schema.d.ts +67 -0
  191. package/dist/recipe-schema.d.ts.map +1 -0
  192. package/dist/recipe-schema.js +959 -0
  193. package/dist/recipe-schema.js.map +1 -0
  194. package/dist/recipe-types.d.ts +86 -0
  195. package/dist/recipe-types.d.ts.map +1 -0
  196. package/dist/recipe-types.js +2 -0
  197. package/dist/recipe-types.js.map +1 -0
  198. package/dist/redaction.d.ts.map +1 -1
  199. package/dist/redaction.js.map +1 -1
  200. package/dist/result-context.d.ts +70 -0
  201. package/dist/result-context.d.ts.map +1 -0
  202. package/dist/result-context.js +186 -0
  203. package/dist/result-context.js.map +1 -0
  204. package/dist/result-delivery.d.ts +64 -0
  205. package/dist/result-delivery.d.ts.map +1 -0
  206. package/dist/result-delivery.js +212 -0
  207. package/dist/result-delivery.js.map +1 -0
  208. package/dist/result-media.d.ts +8 -0
  209. package/dist/result-media.d.ts.map +1 -0
  210. package/dist/result-media.js +14 -0
  211. package/dist/result-media.js.map +1 -0
  212. package/dist/runtime.d.ts +70 -10
  213. package/dist/runtime.d.ts.map +1 -1
  214. package/dist/runtime.js +811 -162
  215. package/dist/runtime.js.map +1 -1
  216. package/dist/schema/index.d.ts +6 -0
  217. package/dist/schema/index.d.ts.map +1 -0
  218. package/dist/schema/index.js +6 -0
  219. package/dist/schema/index.js.map +1 -0
  220. package/dist/schema/native-input.d.ts +2 -0
  221. package/dist/schema/native-input.d.ts.map +1 -0
  222. package/dist/schema/native-input.js +170 -0
  223. package/dist/schema/native-input.js.map +1 -0
  224. package/dist/schema/owner-invocation.d.ts +19 -0
  225. package/dist/schema/owner-invocation.d.ts.map +1 -0
  226. package/dist/schema/owner-invocation.js +40 -0
  227. package/dist/schema/owner-invocation.js.map +1 -0
  228. package/dist/schema/result-delivery.d.ts +57 -0
  229. package/dist/schema/result-delivery.d.ts.map +1 -0
  230. package/dist/schema/result-delivery.js +380 -0
  231. package/dist/schema/result-delivery.js.map +1 -0
  232. package/dist/schema/tool-leases.d.ts +63 -0
  233. package/dist/schema/tool-leases.d.ts.map +1 -0
  234. package/dist/schema/tool-leases.js +439 -0
  235. package/dist/schema/tool-leases.js.map +1 -0
  236. package/dist/schema/validation.d.ts +19 -0
  237. package/dist/schema/validation.d.ts.map +1 -0
  238. package/dist/schema/validation.js +502 -0
  239. package/dist/schema/validation.js.map +1 -0
  240. package/dist/tool-contract.d.ts +105 -0
  241. package/dist/tool-contract.d.ts.map +1 -0
  242. package/dist/tool-contract.js +318 -0
  243. package/dist/tool-contract.js.map +1 -0
  244. package/dist/tool-input-error.d.ts +8 -0
  245. package/dist/tool-input-error.d.ts.map +1 -0
  246. package/dist/tool-input-error.js +10 -0
  247. package/dist/tool-input-error.js.map +1 -0
  248. package/dist/tool-library.d.ts +43 -0
  249. package/dist/tool-library.d.ts.map +1 -0
  250. package/dist/tool-library.js +204 -0
  251. package/dist/tool-library.js.map +1 -0
  252. package/dist/tool-scheduler.d.ts +10 -0
  253. package/dist/tool-scheduler.d.ts.map +1 -0
  254. package/dist/tool-scheduler.js +46 -0
  255. package/dist/tool-scheduler.js.map +1 -0
  256. package/dist/tool-store.d.ts +109 -0
  257. package/dist/tool-store.d.ts.map +1 -0
  258. package/dist/tool-store.js +536 -0
  259. package/dist/tool-store.js.map +1 -0
  260. package/dist/types.d.ts +45 -8
  261. package/dist/types.d.ts.map +1 -1
  262. package/package.json +33 -3
@@ -0,0 +1,959 @@
1
+ import { embedAiToolSchema, projectAiToolSchema } from './tool-contract.js';
2
+ export const recipeRecord = (value) => value !== null && typeof value === 'object' && !Array.isArray(value);
3
+ export const recipeOwn = (value, key) => Object.prototype.hasOwnProperty.call(value, key);
4
+ const commonKeys = ['type', 'description', 'title', 'enum', 'x-runtime-value'];
5
+ const keysByType = {
6
+ object: ['properties', 'required', 'additionalProperties'],
7
+ array: ['items', 'minItems', 'maxItems', 'uniqueItems'],
8
+ string: ['minLength', 'maxLength'],
9
+ number: ['minimum', 'maximum'],
10
+ integer: ['minimum', 'maximum'],
11
+ boolean: [],
12
+ null: []
13
+ };
14
+ /** Conservative implication subset; primitive calls still use the host validator. */
15
+ export const readRecipeSchema = (value, depth = 0) => {
16
+ if (!recipeRecord(value) ||
17
+ depth > 32 ||
18
+ typeof value.type !== 'string' ||
19
+ !recipeOwn(keysByType, value.type)) {
20
+ throw new Error('Expected a concrete supported schema type');
21
+ }
22
+ const type = value.type;
23
+ const keys = [...commonKeys, ...keysByType[type]];
24
+ if (Object.keys(value).some((key) => !keys.includes(key))) {
25
+ throw new Error('Schema keyword is outside the supported composition subset');
26
+ }
27
+ if (value['x-runtime-value'] !== undefined &&
28
+ !['request', 'request-artifact', 'document', 'secret'].includes(value['x-runtime-value'])) {
29
+ throw new Error('Unknown runtime value annotation');
30
+ }
31
+ if (value.enum !== undefined &&
32
+ (!Array.isArray(value.enum) || value.enum.length === 0)) {
33
+ throw new Error('Expected a nonempty enum');
34
+ }
35
+ if (value.enum !== undefined && (type === 'object' || type === 'array')) {
36
+ throw new Error('Structural enums are outside the supported composition subset');
37
+ }
38
+ for (const [min, max] of [
39
+ ['minimum', 'maximum'],
40
+ ['minLength', 'maxLength'],
41
+ ['minItems', 'maxItems']
42
+ ]) {
43
+ for (const key of [min, max]) {
44
+ const bound = value[key];
45
+ if (bound !== undefined &&
46
+ (typeof bound !== 'number' ||
47
+ !Number.isFinite(bound) ||
48
+ (key !== 'minimum' &&
49
+ key !== 'maximum' &&
50
+ (!Number.isSafeInteger(bound) || bound < 0)))) {
51
+ throw new Error('Invalid schema bound');
52
+ }
53
+ }
54
+ if (value[min] !== undefined &&
55
+ value[max] !== undefined &&
56
+ value[min] > value[max]) {
57
+ throw new Error('Contradictory schema bounds');
58
+ }
59
+ }
60
+ if (type === 'object') {
61
+ if (!recipeRecord(value.properties) ||
62
+ !Array.isArray(value.required) ||
63
+ value.additionalProperties !== false) {
64
+ throw new Error('Composition objects require properties, required and additionalProperties false');
65
+ }
66
+ const properties = value.properties;
67
+ if (value.required.some((key) => typeof key !== 'string' || !recipeOwn(properties, key)) ||
68
+ new Set(value.required).size !== value.required.length) {
69
+ throw new Error('Invalid required property names');
70
+ }
71
+ Object.values(value.properties).forEach((child) => readRecipeSchema(child, depth + 1));
72
+ }
73
+ if (type === 'array') {
74
+ if (value.uniqueItems !== undefined &&
75
+ typeof value.uniqueItems !== 'boolean') {
76
+ throw new Error('Expected boolean uniqueItems');
77
+ }
78
+ readRecipeSchema(value.items, depth + 1);
79
+ }
80
+ return { ...value, raw: value };
81
+ };
82
+ /** Intersect supported common constraints; an unknown overlap stays opaque. */
83
+ const intersectRegistered = (left, right) => {
84
+ if (left === false || right === false) {
85
+ return false;
86
+ }
87
+ if (!recipeRecord(left)) {
88
+ return right;
89
+ }
90
+ if (!recipeRecord(right)) {
91
+ return left;
92
+ }
93
+ const result = { ...left };
94
+ for (const [key, value] of Object.entries(right)) {
95
+ if (!recipeOwn(result, key) ||
96
+ JSON.stringify(result[key]) === JSON.stringify(value)) {
97
+ result[key] = value;
98
+ }
99
+ else if (key === 'required' &&
100
+ Array.isArray(result[key]) &&
101
+ Array.isArray(value)) {
102
+ result[key] = [...new Set([...result[key], ...value])];
103
+ }
104
+ else if (key === 'properties' &&
105
+ recipeRecord(result[key]) &&
106
+ recipeRecord(value)) {
107
+ const properties = { ...result[key] };
108
+ for (const [name, child] of Object.entries(value)) {
109
+ if (recipeOwn(properties, name)) {
110
+ properties[name] = intersectRegistered(properties[name], child);
111
+ }
112
+ else {
113
+ properties[name] = left.additionalProperties === false ? false : child;
114
+ }
115
+ }
116
+ if (right.additionalProperties === false) {
117
+ for (const name of Object.keys(properties)) {
118
+ if (!recipeOwn(value, name)) {
119
+ properties[name] = false;
120
+ }
121
+ }
122
+ }
123
+ result[key] = properties;
124
+ }
125
+ else if (key === 'additionalProperties' &&
126
+ typeof result[key] === 'boolean' &&
127
+ typeof value === 'boolean') {
128
+ result[key] = result[key] && value;
129
+ }
130
+ else if (key === 'enum' &&
131
+ Array.isArray(result[key]) &&
132
+ Array.isArray(value)) {
133
+ result[key] = result[key].filter((item) => value.includes(item));
134
+ }
135
+ else if (['description', 'title', 'default', 'examples'].includes(key)) {
136
+ result[key] = value;
137
+ }
138
+ else {
139
+ return { allOf: [left, right] };
140
+ }
141
+ }
142
+ if (recipeRecord(result.properties)) {
143
+ for (const closed of [left, right]) {
144
+ if (closed.additionalProperties !== false) {
145
+ continue;
146
+ }
147
+ const allowed = recipeRecord(closed.properties) ? closed.properties : {};
148
+ for (const name of Object.keys(result.properties)) {
149
+ if (!recipeOwn(allowed, name)) {
150
+ result.properties[name] = false;
151
+ }
152
+ }
153
+ }
154
+ }
155
+ return result;
156
+ };
157
+ /** Registered references are lazy: recursive declarations never expand as trees. */
158
+ export const readRegisteredRecipeSchema = (root, purpose = 'binding', referenceRoot = root) => {
159
+ const references = new Map();
160
+ const parsed = new WeakMap();
161
+ const read = (value, depth = 0) => {
162
+ if (depth > 32) {
163
+ throw new Error('Schema nesting exceeds the proof budget');
164
+ }
165
+ const raw = value;
166
+ const context = { raw, referenceRoot: referenceRoot };
167
+ const opaque = () => ({ type: 'opaque', ...context });
168
+ if (value === false) {
169
+ return { type: 'never', raw: false };
170
+ }
171
+ if (!recipeRecord(value)) {
172
+ return opaque();
173
+ }
174
+ const known = parsed.get(value);
175
+ if (known) {
176
+ return known;
177
+ }
178
+ const annotation = value['x-runtime-value'];
179
+ if (annotation !== undefined &&
180
+ !['request', 'request-artifact', 'document', 'secret'].includes(String(annotation))) {
181
+ throw new Error('Unknown runtime value annotation');
182
+ }
183
+ const metadata = {
184
+ referenceRoot: referenceRoot,
185
+ ...(annotation
186
+ ? {
187
+ 'x-runtime-value': annotation
188
+ }
189
+ : {})
190
+ };
191
+ const normalized = { ...value };
192
+ for (const key of [
193
+ '$defs',
194
+ '$schema',
195
+ 'description',
196
+ 'title',
197
+ 'default',
198
+ 'examples',
199
+ // Presentation metadata cannot hide a complete value from its successor.
200
+ 'x-model-visibility'
201
+ ]) {
202
+ Reflect.deleteProperty(normalized, key);
203
+ }
204
+ if (typeof normalized.$ref === 'string') {
205
+ const pointer = normalized.$ref;
206
+ if (pointer !== '#' && !pointer.startsWith('#/')) {
207
+ return opaque();
208
+ }
209
+ const siblings = { ...normalized };
210
+ delete siblings.$ref;
211
+ delete siblings['x-runtime-value'];
212
+ if (Object.keys(siblings).length) {
213
+ let target = referenceRoot;
214
+ for (const key of recipePointer(pointer.slice(1))) {
215
+ target =
216
+ (recipeRecord(target) || Array.isArray(target)) &&
217
+ recipeOwn(target, key)
218
+ ? target[key]
219
+ : undefined;
220
+ }
221
+ if (target === undefined) {
222
+ return opaque();
223
+ }
224
+ // Siblings constrain the referenced value. They do not replace it or
225
+ // reopen properties prohibited by a closed referenced object.
226
+ let result;
227
+ const constrained = {
228
+ type: 'reference',
229
+ raw,
230
+ ...metadata,
231
+ resolve: () => (result ?? (result = read(intersectRegistered(target, siblings), depth + 1)))
232
+ };
233
+ parsed.set(value, constrained);
234
+ return constrained;
235
+ }
236
+ let resolved = references.get(pointer);
237
+ if (!resolved) {
238
+ let target = referenceRoot;
239
+ for (const key of recipePointer(pointer.slice(1))) {
240
+ target =
241
+ (recipeRecord(target) || Array.isArray(target)) &&
242
+ recipeOwn(target, key)
243
+ ? target[key]
244
+ : undefined;
245
+ }
246
+ if (target === undefined) {
247
+ return opaque();
248
+ }
249
+ const valueAtReference = target;
250
+ resolved = {
251
+ type: 'reference',
252
+ raw,
253
+ resolve: () => read(valueAtReference)
254
+ };
255
+ references.set(pointer, resolved);
256
+ }
257
+ return { ...resolved, ...metadata, raw };
258
+ }
259
+ const mode = recipeOwn(normalized, 'oneOf') ? 'oneOf' : 'anyOf';
260
+ if (recipeOwn(normalized, mode)) {
261
+ const variants = normalized[mode];
262
+ if (!Array.isArray(variants) || variants.length === 0) {
263
+ return opaque();
264
+ }
265
+ Reflect.deleteProperty(normalized, mode);
266
+ const result = {
267
+ type: 'union',
268
+ raw,
269
+ ...metadata,
270
+ exclusive: mode === 'oneOf',
271
+ variants: variants.map((child) => read(intersectRegistered(normalized, child), depth + 1))
272
+ };
273
+ parsed.set(value, result);
274
+ return result;
275
+ }
276
+ if (recipeOwn(normalized, 'const')) {
277
+ if (normalized.const !== null && typeof normalized.const === 'object') {
278
+ return opaque();
279
+ }
280
+ if (Array.isArray(normalized.enum) &&
281
+ !normalized.enum.includes(normalized.const)) {
282
+ return { type: 'never', raw };
283
+ }
284
+ normalized.enum = [normalized.const];
285
+ delete normalized.const;
286
+ }
287
+ if (normalized.type === undefined && Array.isArray(normalized.enum)) {
288
+ const types = new Set(normalized.enum.map((item) => (item === null ? 'null' : typeof item)));
289
+ if (types.size === 1) {
290
+ normalized.type = [...types][0];
291
+ }
292
+ }
293
+ const type = normalized.type;
294
+ if (typeof type !== 'string' || !recipeOwn(keysByType, type)) {
295
+ return opaque();
296
+ }
297
+ const keys = [
298
+ ...commonKeys,
299
+ ...keysByType[type],
300
+ ...(type === 'array' ? ['uniqueItems', 'prefixItems'] : []),
301
+ ...(type === 'object' ? ['propertyNames'] : []),
302
+ ...(['number', 'integer'].includes(type)
303
+ ? ['exclusiveMinimum', 'exclusiveMaximum']
304
+ : []),
305
+ ...(type === 'string' ? ['pattern'] : [])
306
+ ];
307
+ if (Object.keys(normalized).some((key) => !keys.includes(key))) {
308
+ return opaque();
309
+ }
310
+ if (type === 'object') {
311
+ normalized.properties ?? (normalized.properties = {});
312
+ normalized.required ?? (normalized.required = []);
313
+ normalized.additionalProperties ?? (normalized.additionalProperties = true);
314
+ if (!recipeRecord(normalized.properties) ||
315
+ !Array.isArray(normalized.required) ||
316
+ (typeof normalized.additionalProperties !== 'boolean' &&
317
+ !recipeRecord(normalized.additionalProperties)) ||
318
+ normalized.required.some((key) => typeof key !== 'string' ||
319
+ !recipeOwn(normalized.properties, key)) ||
320
+ new Set(normalized.required).size !== normalized.required.length ||
321
+ normalized.enum !== undefined) {
322
+ return opaque();
323
+ }
324
+ const result = {
325
+ ...normalized,
326
+ ...metadata,
327
+ raw,
328
+ additionalProperties: typeof normalized.additionalProperties === 'boolean'
329
+ ? normalized.additionalProperties
330
+ : read(normalized.additionalProperties, depth + 1),
331
+ ...(normalized.propertyNames !== undefined
332
+ ? { propertyNames: read(normalized.propertyNames, depth + 1) }
333
+ : {}),
334
+ properties: Object.fromEntries(Object.entries(normalized.properties).map(([key, child]) => [
335
+ key,
336
+ read(child, depth + 1)
337
+ ]))
338
+ };
339
+ parsed.set(value, result);
340
+ return result;
341
+ }
342
+ if (type === 'array') {
343
+ if ((normalized.uniqueItems !== undefined &&
344
+ typeof normalized.uniqueItems !== 'boolean') ||
345
+ (normalized.prefixItems !== undefined &&
346
+ !Array.isArray(normalized.prefixItems))) {
347
+ return opaque();
348
+ }
349
+ const bounds = {
350
+ ...normalized,
351
+ items: { type: 'null' }
352
+ };
353
+ delete bounds.uniqueItems;
354
+ delete bounds.prefixItems;
355
+ try {
356
+ readRecipeSchema(bounds);
357
+ }
358
+ catch {
359
+ return opaque();
360
+ }
361
+ const result = {
362
+ ...normalized,
363
+ ...metadata,
364
+ raw,
365
+ ...(Array.isArray(normalized.prefixItems)
366
+ ? {
367
+ prefixItems: normalized.prefixItems.map((child) => read(child, depth + 1))
368
+ }
369
+ : {}),
370
+ items: read(normalized.items ?? true, depth + 1)
371
+ };
372
+ parsed.set(value, result);
373
+ return result;
374
+ }
375
+ const primitive = { ...normalized };
376
+ for (const key of ['exclusiveMinimum', 'exclusiveMaximum']) {
377
+ if (primitive[key] !== undefined &&
378
+ (typeof primitive[key] !== 'number' || !Number.isFinite(primitive[key]))) {
379
+ return opaque();
380
+ }
381
+ Reflect.deleteProperty(primitive, key);
382
+ }
383
+ if (primitive.pattern !== undefined) {
384
+ if (typeof primitive.pattern !== 'string') {
385
+ return opaque();
386
+ }
387
+ try {
388
+ new RegExp(primitive.pattern, 'u');
389
+ }
390
+ catch {
391
+ return opaque();
392
+ }
393
+ delete primitive.pattern;
394
+ }
395
+ try {
396
+ readRecipeSchema(primitive);
397
+ }
398
+ catch {
399
+ return opaque();
400
+ }
401
+ return { ...normalized, ...metadata, raw };
402
+ };
403
+ const result = read(root);
404
+ if (result.type === 'opaque' && purpose === 'binding') {
405
+ throw new RecipeReferenceError('Expected a supported dependency schema');
406
+ }
407
+ return result;
408
+ };
409
+ export const recipeResolve = (schema) => {
410
+ const visited = new Set();
411
+ let current = schema;
412
+ let lifetime = schema['x-runtime-value'];
413
+ while (current.type === 'reference') {
414
+ if (visited.has(current) || visited.size >= 32) {
415
+ return {
416
+ type: 'opaque',
417
+ raw: schema.raw,
418
+ referenceRoot: schema.referenceRoot
419
+ };
420
+ }
421
+ visited.add(current);
422
+ current = current.resolve();
423
+ if (lifetime &&
424
+ current['x-runtime-value'] &&
425
+ lifetime !== current['x-runtime-value']) {
426
+ return {
427
+ type: 'opaque',
428
+ raw: schema.raw,
429
+ referenceRoot: schema.referenceRoot
430
+ };
431
+ }
432
+ lifetime ?? (lifetime = current['x-runtime-value']);
433
+ }
434
+ return lifetime ? { ...current, 'x-runtime-value': lifetime } : current;
435
+ };
436
+ export const recipeValidationSchema = (schema) => {
437
+ const raw = schema.raw ?? schema;
438
+ const value = schema.referenceRoot === undefined
439
+ ? raw
440
+ : projectAiToolSchema(raw, schema.referenceRoot);
441
+ const lifetime = schema['x-runtime-value'];
442
+ return lifetime && recipeRecord(value)
443
+ ? { ...value, 'x-runtime-value': lifetime }
444
+ : value;
445
+ };
446
+ export class RecipeReferenceError extends Error {
447
+ }
448
+ export const recipePointer = (path) => {
449
+ if (path === '') {
450
+ return [];
451
+ }
452
+ if (!path.startsWith('/') || /~(?![01])/u.test(path)) {
453
+ throw new RecipeReferenceError(`Expected an RFC 6901 JSON Pointer: ${path}`);
454
+ }
455
+ return path
456
+ .slice(1)
457
+ .split('/')
458
+ .map((part) => part.replace(/~1/g, '/').replace(/~0/g, '~'));
459
+ };
460
+ // A field required by the outer object is guaranteed across intersections
461
+ // and alternatives when repeated declarations are exactly the same schema.
462
+ const independentOutputField = (raw, key, referenceRoot = raw) => {
463
+ if (!recipeRecord(raw) ||
464
+ raw.type !== 'object' ||
465
+ !Array.isArray(raw.required) ||
466
+ !raw.required.includes(key) ||
467
+ !recipeRecord(raw.properties) ||
468
+ !recipeRecord(raw.properties[key])) {
469
+ return;
470
+ }
471
+ const safeBoundary = (s) => Object.keys(s).every((k) => [
472
+ 'type',
473
+ 'properties',
474
+ 'required',
475
+ 'additionalProperties',
476
+ 'allOf',
477
+ 'oneOf',
478
+ 'anyOf',
479
+ '$defs',
480
+ 'description',
481
+ 'title'
482
+ ].includes(k)) &&
483
+ (s.additionalProperties === undefined ||
484
+ typeof s.additionalProperties === 'boolean');
485
+ const fieldSchema = JSON.stringify(raw.properties[key]);
486
+ const independent = (value) => typeof value === 'boolean' ||
487
+ (recipeRecord(value) &&
488
+ safeBoundary(value) &&
489
+ (!recipeRecord(value.properties) ||
490
+ !recipeOwn(value.properties, key) ||
491
+ value.properties[key] === true ||
492
+ JSON.stringify(value.properties[key]) === fieldSchema) &&
493
+ branchesIndependent(value));
494
+ const branchesIndependent = (s) => ['allOf', 'oneOf', 'anyOf'].every((k) => s[k] === undefined || (Array.isArray(s[k]) && s[k].every(independent)));
495
+ if (!safeBoundary(raw) || !branchesIndependent(raw)) {
496
+ return;
497
+ }
498
+ return readRegisteredRecipeSchema(raw.properties[key], 'binding', referenceRoot);
499
+ };
500
+ // Conditional selections validate the complete actual owner result and the
501
+ // destination before dispatch. Do not broaden durable admission normalization.
502
+ const conditionalIntersection = (source, depth = 0) => {
503
+ if (!recipeRecord(source) || source.allOf === undefined) {
504
+ return source;
505
+ }
506
+ if (depth > 32 || !Array.isArray(source.allOf) || !source.allOf.length) {
507
+ return undefined;
508
+ }
509
+ const { allOf, ...siblings } = source;
510
+ let result = siblings;
511
+ for (const child of allOf) {
512
+ const selected = conditionalIntersection(child, depth + 1);
513
+ if (selected === undefined) {
514
+ return undefined;
515
+ }
516
+ result = intersectRegistered(result, selected);
517
+ if (recipeRecord(result) && result.allOf !== undefined) {
518
+ return undefined;
519
+ }
520
+ }
521
+ return result;
522
+ };
523
+ export const recipeSchemaAt = (schema, path, requireAtExecution = false) => {
524
+ const visit = (current, keys, inherited) => {
525
+ current = recipeResolve(current);
526
+ const lifetime = inherited ?? current['x-runtime-value'];
527
+ if (requireAtExecution &&
528
+ keys.length &&
529
+ current.type === 'opaque' &&
530
+ recipeRecord(current.raw) &&
531
+ current.raw.allOf !== undefined) {
532
+ const selected = conditionalIntersection(current.raw);
533
+ if (selected !== undefined) {
534
+ return visit(readRegisteredRecipeSchema(selected, 'result', current.referenceRoot), keys, lifetime);
535
+ }
536
+ }
537
+ if (keys.length &&
538
+ (current.type === 'opaque' || current.type === 'union')) {
539
+ const child = independentOutputField(current.raw, keys[0], current.referenceRoot);
540
+ if (child) {
541
+ return visit(child, keys.slice(1), lifetime);
542
+ }
543
+ }
544
+ if (current.type === 'union') {
545
+ // Request sequences validate the actual source before running the next
546
+ // step. A nullable result may expose fields when present; persisted
547
+ // recipes still require those fields in every possible branch.
548
+ const sources = requireAtExecution && keys.length
549
+ ? current.variants.filter((variant) => recipeResolve(variant).type !== 'null')
550
+ : current.variants;
551
+ if (!sources.length) {
552
+ throw new RecipeReferenceError(`Source path ${path} is absent or not guaranteed to exist`);
553
+ }
554
+ const variants = sources.flatMap((variant) => {
555
+ try {
556
+ return [visit(variant, keys, lifetime)];
557
+ }
558
+ catch (error) {
559
+ if (requireAtExecution &&
560
+ keys.length &&
561
+ error instanceof RecipeReferenceError) {
562
+ return [];
563
+ }
564
+ throw error;
565
+ }
566
+ });
567
+ if (!variants.length) {
568
+ throw new RecipeReferenceError(`Source path ${path} is absent or not guaranteed to exist`);
569
+ }
570
+ if (variants.length !== current.variants.length &&
571
+ variants.length === 1) {
572
+ return variants[0];
573
+ }
574
+ return {
575
+ ...current,
576
+ variants,
577
+ // Projection removes branch discriminators. Preserve the selected value
578
+ // contract, not the source envelope or its exclusive branch semantics.
579
+ ...(keys.length
580
+ ? {
581
+ exclusive: false,
582
+ referenceRoot: undefined,
583
+ raw: {
584
+ anyOf: variants.map((variant, index) => embedAiToolSchema(recipeValidationSchema(variant), '#/anyOf/' + index))
585
+ }
586
+ }
587
+ : {})
588
+ };
589
+ }
590
+ if (!keys.length) {
591
+ return lifetime ? { ...current, 'x-runtime-value': lifetime } : current;
592
+ }
593
+ const [key, ...rest] = keys;
594
+ if (current.type === 'object' &&
595
+ (requireAtExecution || current.required.includes(key)) &&
596
+ recipeOwn(current.properties, key)) {
597
+ return visit(current.properties[key], rest, lifetime);
598
+ }
599
+ // A typed dictionary declares the value contract for each key. Existence
600
+ // is checked against the completed result before dispatching the consumer.
601
+ if (requireAtExecution &&
602
+ current.type === 'object' &&
603
+ typeof current.additionalProperties === 'object' &&
604
+ !['never', 'opaque'].includes(recipeResolve(current.additionalProperties).type) &&
605
+ current.propertyNames === undefined &&
606
+ !recipeOwn(current.properties, key)) {
607
+ return visit(current.additionalProperties, rest, lifetime);
608
+ }
609
+ if (current.type === 'array' &&
610
+ /^(0|[1-9][0-9]*)$/u.test(key) &&
611
+ (Number(key) < (current.minItems ?? 0) ||
612
+ (requireAtExecution && Number(key) < (current.maxItems ?? Infinity)))) {
613
+ return visit(current.prefixItems?.[Number(key)] ?? current.items, rest, lifetime);
614
+ }
615
+ const message = `Source path ${path} is absent or not guaranteed to exist`;
616
+ throw new RecipeReferenceError(message);
617
+ };
618
+ return visit(schema, recipePointer(path));
619
+ };
620
+ /** Only explicit disjoint types/enums/required discriminators prove exclusion. */
621
+ export const recipeDisjoint = (source, target, depth = 0) => {
622
+ if (depth > 64) {
623
+ return false;
624
+ }
625
+ source = recipeResolve(source);
626
+ target = recipeResolve(target);
627
+ if (source.type === 'never' || target.type === 'never') {
628
+ return true;
629
+ }
630
+ if (source.type === 'union') {
631
+ return source.variants.every((item) => recipeDisjoint(item, target, depth + 1));
632
+ }
633
+ if (target.type === 'union') {
634
+ return target.variants.every((item) => recipeDisjoint(source, item, depth + 1));
635
+ }
636
+ if (source.type === 'opaque' || target.type === 'opaque') {
637
+ const known = source.type === 'opaque' ? target : source;
638
+ const opaque = source.type === 'opaque' ? source : target;
639
+ const raw = opaque.raw;
640
+ if (known.type !== 'object' ||
641
+ !recipeRecord(raw) ||
642
+ !recipeRecord(raw.properties)) {
643
+ return false;
644
+ }
645
+ const properties = raw.properties;
646
+ // Root properties constrain every object containing that field, even when
647
+ // another constraint is outside the implication subset. This proves only
648
+ // non-overlap; it never converts an opaque branch into an admissible one.
649
+ return known.required.some((key) => {
650
+ if (!recipeOwn(known.properties, key) || !recipeOwn(properties, key)) {
651
+ return false;
652
+ }
653
+ const constraint = properties[key];
654
+ try {
655
+ const necessary = readRegisteredRecipeSchema(recipeRecord(constraint) && recipeRecord(raw.$defs)
656
+ ? { ...constraint, $defs: raw.$defs }
657
+ : constraint);
658
+ return recipeDisjoint(known.properties[key], necessary, depth + 1);
659
+ }
660
+ catch {
661
+ return false;
662
+ }
663
+ });
664
+ }
665
+ if (source.type !== target.type &&
666
+ !(['number', 'integer'].includes(source.type) &&
667
+ ['number', 'integer'].includes(target.type))) {
668
+ return true;
669
+ }
670
+ if (['number', 'integer'].includes(source.type) &&
671
+ ['number', 'integer'].includes(target.type)) {
672
+ const before = (left, right) => {
673
+ const upper = Math.min(left.maximum ?? Infinity, left.exclusiveMaximum ?? Infinity);
674
+ const lower = Math.max(right.minimum ?? -Infinity, right.exclusiveMinimum ?? -Infinity);
675
+ return (upper < lower ||
676
+ (upper === lower &&
677
+ (left.exclusiveMaximum === upper || right.exclusiveMinimum === lower)));
678
+ };
679
+ if (before(source, target) || before(target, source)) {
680
+ return true;
681
+ }
682
+ }
683
+ const targetEnum = target.enum;
684
+ if (source.enum &&
685
+ targetEnum &&
686
+ source.enum.every((value) => !targetEnum.includes(value))) {
687
+ return true;
688
+ }
689
+ if (source.type === 'object' && target.type === 'object') {
690
+ if (source.required.some((key) => !recipeOwn(target.properties, key) && !target.additionalProperties)) {
691
+ return true;
692
+ }
693
+ if (target.required.some((key) => !recipeOwn(source.properties, key) && !source.additionalProperties)) {
694
+ return true;
695
+ }
696
+ return source.required.some((key) => target.required.includes(key) &&
697
+ recipeOwn(source.properties, key) &&
698
+ recipeOwn(target.properties, key) &&
699
+ recipeDisjoint(source.properties[key], target.properties[key], depth + 1));
700
+ }
701
+ return false;
702
+ };
703
+ export const recipeSelectVariant = (source, target) => {
704
+ target = recipeResolve(target);
705
+ if (target.type !== 'union') {
706
+ return target;
707
+ }
708
+ const candidates = target.variants.filter((item) => !recipeDisjoint(source, item));
709
+ return candidates.length === 1 ? candidates[0] : undefined;
710
+ };
711
+ const lower = (source, target, fallback) => target === undefined || (source ?? fallback) >= target;
712
+ const upper = (source, target) => target === undefined || (source ?? Infinity) <= target;
713
+ const inheritRecipeLifetime = (schema, parent) => parent['x-runtime-value']
714
+ ? { ...schema, 'x-runtime-value': parent['x-runtime-value'] }
715
+ : schema;
716
+ /** Request steps may refine numeric values at invocation. Types and resource
717
+ * lifetimes still match here; the unchanged destination schema validates the
718
+ * resolved value before dispatch. Persisted recipes require static guarantees. */
719
+ export const recipeRequestAssignable = (source, target) => recipeAssignable(source, target, 0, true);
720
+ export const recipeAssignable = (source, target, depth = 0, requestRefinement = false) => {
721
+ if (depth > 64) {
722
+ return false;
723
+ }
724
+ source = recipeResolve(source);
725
+ target = recipeResolve(target);
726
+ if (source.type === 'never' || target.type === 'never') {
727
+ return false;
728
+ }
729
+ if (source.type === 'opaque' || target.type === 'opaque') {
730
+ return false;
731
+ }
732
+ if (source.type === 'union') {
733
+ return source.variants.every((item) => recipeAssignable(inheritRecipeLifetime(item, source), target, depth + 1, requestRefinement));
734
+ }
735
+ if (target.type === 'union') {
736
+ return target.variants.some((item, index) => recipeAssignable(source, inheritRecipeLifetime(item, target), depth + 1, requestRefinement) &&
737
+ (!target.exclusive ||
738
+ target.variants.every((other, otherIndex) => otherIndex === index || recipeDisjoint(source, other))));
739
+ }
740
+ if (source.type !== target.type &&
741
+ !(source.type === 'integer' && target.type === 'number')) {
742
+ return false;
743
+ }
744
+ if (source['x-runtime-value'] !== target['x-runtime-value']) {
745
+ return false;
746
+ }
747
+ // Only numeric restrictions may defer to actual-input validation. Structural,
748
+ // type and lifetime guarantees above and in recursive children stay mandatory.
749
+ if (requestRefinement &&
750
+ (source.type === 'number' || source.type === 'integer')) {
751
+ return !recipeDisjoint(source, target);
752
+ }
753
+ if (target.exclusiveMinimum !== undefined &&
754
+ !((source.minimum !== undefined &&
755
+ source.minimum > target.exclusiveMinimum) ||
756
+ (source.exclusiveMinimum !== undefined &&
757
+ source.exclusiveMinimum >= target.exclusiveMinimum))) {
758
+ return false;
759
+ }
760
+ if (target.exclusiveMaximum !== undefined &&
761
+ !((source.maximum !== undefined &&
762
+ source.maximum < target.exclusiveMaximum) ||
763
+ (source.exclusiveMaximum !== undefined &&
764
+ source.exclusiveMaximum <= target.exclusiveMaximum))) {
765
+ return false;
766
+ }
767
+ if (target.pattern &&
768
+ source.pattern !== target.pattern &&
769
+ !(source.enum &&
770
+ source.enum.every((item) => typeof item === 'string' &&
771
+ new RegExp(target.pattern, 'u').test(item)))) {
772
+ return false;
773
+ }
774
+ const targetEnum = target.enum;
775
+ if (targetEnum &&
776
+ (!source.enum || !source.enum.every((value) => targetEnum.includes(value)))) {
777
+ return false;
778
+ }
779
+ if (!lower(source.minimum, target.minimum, -Infinity) ||
780
+ !upper(source.maximum, target.maximum) ||
781
+ !lower(source.minLength, target.minLength, 0) ||
782
+ !upper(source.maxLength, target.maxLength) ||
783
+ !lower(source.minItems, target.minItems, 0) ||
784
+ !upper(source.maxItems, target.maxItems)) {
785
+ return false;
786
+ }
787
+ if (source.type === 'object' && target.type === 'object') {
788
+ if (!target.required.every((key) => source.required.includes(key))) {
789
+ return false;
790
+ }
791
+ const sourceExtras = source.additionalProperties;
792
+ const targetExtras = target.additionalProperties;
793
+ if (sourceExtras !== false) {
794
+ if (targetExtras === false) {
795
+ return false;
796
+ }
797
+ if (target.propertyNames &&
798
+ !recipeAssignable(source.propertyNames ?? { type: 'string' }, target.propertyNames, depth + 1, requestRefinement)) {
799
+ return false;
800
+ }
801
+ if (typeof targetExtras !== 'boolean') {
802
+ if (sourceExtras === true ||
803
+ !recipeAssignable(sourceExtras, targetExtras, depth + 1, requestRefinement)) {
804
+ return false;
805
+ }
806
+ }
807
+ else if (typeof sourceExtras !== 'boolean' &&
808
+ recipeHasRuntimeValue(sourceExtras)) {
809
+ return false;
810
+ }
811
+ for (const [key, child] of Object.entries(target.properties)) {
812
+ if (!recipeOwn(source.properties, key) &&
813
+ (sourceExtras === true ||
814
+ !recipeAssignable(sourceExtras, child, depth + 1, requestRefinement))) {
815
+ return false;
816
+ }
817
+ }
818
+ }
819
+ return Object.entries(source.properties).every(([key, child]) => {
820
+ // A forbidden optional property has no value to forward. Required
821
+ // forbidden properties remain invalid rather than vacuously assignable.
822
+ if (recipeResolve(child).type === 'never') {
823
+ return !source.required.includes(key);
824
+ }
825
+ if (target.propertyNames &&
826
+ !recipeAssignable({
827
+ type: 'string',
828
+ enum: [key],
829
+ minLength: key.length,
830
+ maxLength: key.length
831
+ }, target.propertyNames, depth + 1, requestRefinement)) {
832
+ return false;
833
+ }
834
+ if (recipeOwn(target.properties, key)) {
835
+ return recipeAssignable(child, target.properties[key], depth + 1, requestRefinement);
836
+ }
837
+ if (typeof targetExtras !== 'boolean') {
838
+ return recipeAssignable(child, targetExtras, depth + 1, requestRefinement);
839
+ }
840
+ return targetExtras && !recipeHasRuntimeValue(child);
841
+ });
842
+ }
843
+ if (source.type === 'array' && target.type === 'array') {
844
+ if (target.uniqueItems && !source.uniqueItems) {
845
+ return false;
846
+ }
847
+ const prefixLength = Math.max(source.prefixItems?.length ?? 0, target.prefixItems?.length ?? 0);
848
+ for (let index = 0; index < Math.min(prefixLength, source.maxItems ?? Infinity); index += 1) {
849
+ if (!recipeAssignable(source.prefixItems?.[index] ?? source.items, target.prefixItems?.[index] ?? target.items, depth + 1, requestRefinement)) {
850
+ return false;
851
+ }
852
+ }
853
+ return ((source.maxItems ?? Infinity) <= prefixLength ||
854
+ recipeAssignable(source.items, target.items, depth + 1, requestRefinement));
855
+ }
856
+ return true;
857
+ };
858
+ export const recipeCapturesValue = (value, schema, depth = 0) => {
859
+ if (depth > 64) {
860
+ return true;
861
+ }
862
+ schema = recipeResolve(schema);
863
+ // A homogeneous map can be checked as a complete literal. Its keys and
864
+ // values still undergo full registered-schema validation at admission.
865
+ if (schema['x-runtime-value']) {
866
+ return true;
867
+ }
868
+ if (schema.type === 'opaque') {
869
+ return true;
870
+ }
871
+ if (schema.type === 'never') {
872
+ return false;
873
+ }
874
+ if (schema.type === 'union') {
875
+ return schema.variants
876
+ .filter((item) => !recipeDisjoint(recipeLiteralShape(value), item))
877
+ .some((item) => recipeCapturesValue(value, item, depth + 1));
878
+ }
879
+ if (schema.type === 'object' && recipeRecord(value)) {
880
+ return Object.entries(value).some(([key, child]) => {
881
+ const extra = typeof schema.additionalProperties === 'object'
882
+ ? schema.additionalProperties
883
+ : undefined;
884
+ const field = recipeOwn(schema.properties, key)
885
+ ? schema.properties[key]
886
+ : extra;
887
+ return !field || recipeCapturesValue(child, field, depth + 1);
888
+ });
889
+ }
890
+ if (schema.type === 'array' && Array.isArray(value)) {
891
+ return value.some((child, index) => recipeCapturesValue(child, schema.prefixItems?.[index] ?? schema.items, depth + 1));
892
+ }
893
+ return false;
894
+ };
895
+ const recipeHasRuntimeValue = (schema, seen = new Set()) => {
896
+ schema = recipeResolve(schema);
897
+ if (schema['x-runtime-value'] || schema.type === 'opaque') {
898
+ return true;
899
+ }
900
+ if (seen.has(schema)) {
901
+ return false;
902
+ }
903
+ seen.add(schema);
904
+ const visit = (child) => recipeHasRuntimeValue(child, seen);
905
+ if (schema.type === 'union') {
906
+ return schema.variants.some(visit);
907
+ }
908
+ if (schema.type === 'object') {
909
+ return (Object.values(schema.properties).some(visit) ||
910
+ (typeof schema.additionalProperties === 'object' &&
911
+ visit(schema.additionalProperties)));
912
+ }
913
+ if (schema.type === 'array') {
914
+ return visit(schema.items) || !!schema.prefixItems?.some(visit);
915
+ }
916
+ return false;
917
+ };
918
+ /** Evidence for variant selection only; never a substitute for full binding checks. */
919
+ export const recipeLiteralShape = (value) => {
920
+ if (recipeRecord(value)) {
921
+ return {
922
+ type: 'object',
923
+ additionalProperties: false,
924
+ required: Object.keys(value),
925
+ properties: Object.fromEntries(Object.entries(value).map(([key, child]) => [
926
+ key,
927
+ recipeLiteralShape(child)
928
+ ]))
929
+ };
930
+ }
931
+ if (Array.isArray(value)) {
932
+ return { type: 'array', items: { type: 'opaque' } };
933
+ }
934
+ if (value === null) {
935
+ return { type: 'null', enum: [null] };
936
+ }
937
+ if (typeof value === 'string' ||
938
+ typeof value === 'number' ||
939
+ typeof value === 'boolean') {
940
+ return {
941
+ type: typeof value,
942
+ enum: [value]
943
+ };
944
+ }
945
+ return { type: 'opaque' };
946
+ };
947
+ /** Select a registered result field. Request sequences may admit declared optional
948
+ * paths when their presence is checked against the actual result at execution. */
949
+ export const registeredRecipeOutputAt = (schema, path, requireAtExecution = false) => {
950
+ if (!path) {
951
+ return schema;
952
+ }
953
+ const selected = recipeSchemaAt(readRegisteredRecipeSchema(schema, 'result'), path, requireAtExecution);
954
+ const materialize = (field) => field.type === 'union'
955
+ ? { anyOf: field.variants.map(materialize) }
956
+ : recipeValidationSchema(field);
957
+ return materialize(selected);
958
+ };
959
+ //# sourceMappingURL=recipe-schema.js.map