effect 4.0.0-beta.101 → 4.0.0-beta.102

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 (494) hide show
  1. package/dist/Array.d.ts.map +1 -1
  2. package/dist/Array.js +81 -5
  3. package/dist/Array.js.map +1 -1
  4. package/dist/Channel.d.ts.map +1 -1
  5. package/dist/Channel.js +27 -9
  6. package/dist/Channel.js.map +1 -1
  7. package/dist/Config.d.ts +1 -1
  8. package/dist/Config.d.ts.map +1 -1
  9. package/dist/Config.js +6 -5
  10. package/dist/Config.js.map +1 -1
  11. package/dist/ConfigProvider.js +7 -7
  12. package/dist/ConfigProvider.js.map +1 -1
  13. package/dist/Data.d.ts.map +1 -1
  14. package/dist/Data.js +2 -1
  15. package/dist/Data.js.map +1 -1
  16. package/dist/Effect.d.ts +61 -100
  17. package/dist/Effect.d.ts.map +1 -1
  18. package/dist/Effect.js +66 -39
  19. package/dist/Effect.js.map +1 -1
  20. package/dist/FiberHandle.d.ts.map +1 -1
  21. package/dist/FiberHandle.js +3 -2
  22. package/dist/FiberHandle.js.map +1 -1
  23. package/dist/Iterable.d.ts.map +1 -1
  24. package/dist/Iterable.js +2 -1
  25. package/dist/Iterable.js.map +1 -1
  26. package/dist/JsonPatch.d.ts.map +1 -1
  27. package/dist/JsonPatch.js +4 -3
  28. package/dist/JsonPatch.js.map +1 -1
  29. package/dist/JsonSchema.d.ts.map +1 -1
  30. package/dist/JsonSchema.js +9 -11
  31. package/dist/JsonSchema.js.map +1 -1
  32. package/dist/Logger.d.ts.map +1 -1
  33. package/dist/Logger.js +3 -2
  34. package/dist/Logger.js.map +1 -1
  35. package/dist/ManagedRuntime.d.ts +9 -0
  36. package/dist/ManagedRuntime.d.ts.map +1 -1
  37. package/dist/ManagedRuntime.js +3 -0
  38. package/dist/ManagedRuntime.js.map +1 -1
  39. package/dist/Metric.d.ts.map +1 -1
  40. package/dist/Metric.js +4 -3
  41. package/dist/Metric.js.map +1 -1
  42. package/dist/Optic.d.ts.map +1 -1
  43. package/dist/Optic.js +5 -4
  44. package/dist/Optic.js.map +1 -1
  45. package/dist/Option.d.ts.map +1 -1
  46. package/dist/Option.js +2 -1
  47. package/dist/Option.js.map +1 -1
  48. package/dist/PartitionedSemaphore.d.ts.map +1 -1
  49. package/dist/PartitionedSemaphore.js +1 -4
  50. package/dist/PartitionedSemaphore.js.map +1 -1
  51. package/dist/Record.d.ts +93 -1
  52. package/dist/Record.d.ts.map +1 -1
  53. package/dist/Record.js +57 -19
  54. package/dist/Record.js.map +1 -1
  55. package/dist/References.d.ts +0 -45
  56. package/dist/References.d.ts.map +1 -1
  57. package/dist/References.js +0 -45
  58. package/dist/References.js.map +1 -1
  59. package/dist/Request.d.ts.map +1 -1
  60. package/dist/Request.js +9 -6
  61. package/dist/Request.js.map +1 -1
  62. package/dist/Result.d.ts.map +1 -1
  63. package/dist/Result.js +2 -1
  64. package/dist/Result.js.map +1 -1
  65. package/dist/Schema.d.ts +1268 -495
  66. package/dist/Schema.d.ts.map +1 -1
  67. package/dist/Schema.js +2308 -614
  68. package/dist/Schema.js.map +1 -1
  69. package/dist/SchemaAST.d.ts +2 -2
  70. package/dist/SchemaAST.d.ts.map +1 -1
  71. package/dist/SchemaAST.js +189 -130
  72. package/dist/SchemaAST.js.map +1 -1
  73. package/dist/SchemaError.js +1 -1
  74. package/dist/SchemaError.js.map +1 -1
  75. package/dist/SchemaGetter.d.ts +13 -11
  76. package/dist/SchemaGetter.d.ts.map +1 -1
  77. package/dist/SchemaGetter.js +34 -24
  78. package/dist/SchemaGetter.js.map +1 -1
  79. package/dist/SchemaIssue.js +1 -1
  80. package/dist/SchemaIssue.js.map +1 -1
  81. package/dist/SchemaParser.d.ts.map +1 -1
  82. package/dist/SchemaParser.js +1 -12
  83. package/dist/SchemaParser.js.map +1 -1
  84. package/dist/SchemaRepresentation.d.ts +403 -1504
  85. package/dist/SchemaRepresentation.d.ts.map +1 -1
  86. package/dist/SchemaRepresentation.js +294 -2852
  87. package/dist/SchemaRepresentation.js.map +1 -1
  88. package/dist/SchemaTransformation.d.ts +2 -2
  89. package/dist/SchemaTransformation.js +3 -3
  90. package/dist/SchemaTransformation.js.map +1 -1
  91. package/dist/Semaphore.d.ts +5 -3
  92. package/dist/Semaphore.d.ts.map +1 -1
  93. package/dist/Semaphore.js.map +1 -1
  94. package/dist/Stream.d.ts +15 -9
  95. package/dist/Stream.d.ts.map +1 -1
  96. package/dist/Stream.js +46 -16
  97. package/dist/Stream.js.map +1 -1
  98. package/dist/Struct.d.ts.map +1 -1
  99. package/dist/Struct.js +5 -4
  100. package/dist/Struct.js.map +1 -1
  101. package/dist/Types.d.ts +1 -3
  102. package/dist/Types.d.ts.map +1 -1
  103. package/dist/index.d.ts +0 -4
  104. package/dist/index.d.ts.map +1 -1
  105. package/dist/index.js +0 -4
  106. package/dist/index.js.map +1 -1
  107. package/dist/internal/core.js +2 -1
  108. package/dist/internal/core.js.map +1 -1
  109. package/dist/internal/effect.js +64 -21
  110. package/dist/internal/effect.js.map +1 -1
  111. package/dist/internal/matcher.js +1 -1
  112. package/dist/internal/matcher.js.map +1 -1
  113. package/dist/internal/record.d.ts +0 -3
  114. package/dist/internal/record.d.ts.map +1 -1
  115. package/dist/internal/record.js +10 -5
  116. package/dist/internal/record.js.map +1 -1
  117. package/dist/internal/references.js +0 -4
  118. package/dist/internal/references.js.map +1 -1
  119. package/dist/internal/schema/annotations.js +12 -0
  120. package/dist/internal/schema/annotations.js.map +1 -1
  121. package/dist/internal/schema/fromJsonSchemaDocument.d.ts +2 -0
  122. package/dist/internal/schema/fromJsonSchemaDocument.d.ts.map +1 -0
  123. package/dist/internal/schema/fromJsonSchemaDocument.js +743 -0
  124. package/dist/internal/schema/fromJsonSchemaDocument.js.map +1 -0
  125. package/dist/internal/schema/fromRepresentation.d.ts +2 -0
  126. package/dist/internal/schema/fromRepresentation.d.ts.map +1 -0
  127. package/dist/internal/schema/fromRepresentation.js +252 -0
  128. package/dist/internal/schema/fromRepresentation.js.map +1 -0
  129. package/dist/internal/schema/schema.js +28 -40
  130. package/dist/internal/schema/schema.js.map +1 -1
  131. package/dist/internal/schema/{arbitrary.d.ts → toArbitrary.d.ts} +1 -1
  132. package/dist/internal/schema/toArbitrary.d.ts.map +1 -0
  133. package/dist/internal/schema/{arbitrary.js → toArbitrary.js} +8 -9
  134. package/dist/internal/schema/toArbitrary.js.map +1 -0
  135. package/dist/internal/schema/toCodeDocument.d.ts +2 -0
  136. package/dist/internal/schema/toCodeDocument.d.ts.map +1 -0
  137. package/dist/internal/schema/toCodeDocument.js +438 -0
  138. package/dist/internal/schema/toCodeDocument.js.map +1 -0
  139. package/dist/internal/schema/toEquivalence.d.ts +2 -0
  140. package/dist/internal/schema/toEquivalence.d.ts.map +1 -0
  141. package/dist/internal/schema/{equivalence.js → toEquivalence.js} +15 -13
  142. package/dist/internal/schema/toEquivalence.js.map +1 -0
  143. package/dist/internal/schema/toJsonSchemaDocument.d.ts +2 -0
  144. package/dist/internal/schema/toJsonSchemaDocument.d.ts.map +1 -0
  145. package/dist/internal/schema/toJsonSchemaDocument.js +466 -0
  146. package/dist/internal/schema/toJsonSchemaDocument.js.map +1 -0
  147. package/dist/internal/schema/toRepresentation.d.ts +2 -0
  148. package/dist/internal/schema/toRepresentation.d.ts.map +1 -0
  149. package/dist/internal/schema/toRepresentation.js +369 -0
  150. package/dist/internal/schema/toRepresentation.js.map +1 -0
  151. package/dist/internal/stackTraceLimit.js +3 -6
  152. package/dist/internal/stackTraceLimit.js.map +1 -1
  153. package/dist/testing/TestClock.js +4 -1
  154. package/dist/testing/TestClock.js.map +1 -1
  155. package/dist/unstable/ai/AiError.d.ts +26 -26
  156. package/dist/unstable/ai/AiError.d.ts.map +1 -1
  157. package/dist/unstable/ai/AiError.js +5 -4
  158. package/dist/unstable/ai/AiError.js.map +1 -1
  159. package/dist/unstable/ai/AnthropicStructuredOutput.d.ts +21 -6
  160. package/dist/unstable/ai/AnthropicStructuredOutput.d.ts.map +1 -1
  161. package/dist/unstable/ai/AnthropicStructuredOutput.js +86 -316
  162. package/dist/unstable/ai/AnthropicStructuredOutput.js.map +1 -1
  163. package/dist/unstable/ai/EmbeddingModel.d.ts +1 -1
  164. package/dist/unstable/ai/EmbeddingModel.js +1 -1
  165. package/dist/unstable/ai/EmbeddingModel.js.map +1 -1
  166. package/dist/unstable/ai/LanguageModel.d.ts +4 -1
  167. package/dist/unstable/ai/LanguageModel.d.ts.map +1 -1
  168. package/dist/unstable/ai/LanguageModel.js +19 -17
  169. package/dist/unstable/ai/LanguageModel.js.map +1 -1
  170. package/dist/unstable/ai/McpSchema.d.ts +197 -197
  171. package/dist/unstable/ai/McpSchema.d.ts.map +1 -1
  172. package/dist/unstable/ai/McpSchema.js +13 -13
  173. package/dist/unstable/ai/McpSchema.js.map +1 -1
  174. package/dist/unstable/ai/McpServer.d.ts +4 -4
  175. package/dist/unstable/ai/McpServer.d.ts.map +1 -1
  176. package/dist/unstable/ai/McpServer.js +116 -49
  177. package/dist/unstable/ai/McpServer.js.map +1 -1
  178. package/dist/unstable/ai/OpenAiStructuredOutput.d.ts +32 -10
  179. package/dist/unstable/ai/OpenAiStructuredOutput.d.ts.map +1 -1
  180. package/dist/unstable/ai/OpenAiStructuredOutput.js +193 -377
  181. package/dist/unstable/ai/OpenAiStructuredOutput.js.map +1 -1
  182. package/dist/unstable/ai/Response.d.ts +19 -19
  183. package/dist/unstable/ai/Response.d.ts.map +1 -1
  184. package/dist/unstable/ai/Response.js +14 -14
  185. package/dist/unstable/ai/Response.js.map +1 -1
  186. package/dist/unstable/ai/Tool.d.ts +4 -0
  187. package/dist/unstable/ai/Tool.d.ts.map +1 -1
  188. package/dist/unstable/ai/Tool.js +11 -3
  189. package/dist/unstable/ai/Tool.js.map +1 -1
  190. package/dist/unstable/ai/Toolkit.d.ts +9 -1
  191. package/dist/unstable/ai/Toolkit.d.ts.map +1 -1
  192. package/dist/unstable/ai/Toolkit.js +14 -10
  193. package/dist/unstable/ai/Toolkit.js.map +1 -1
  194. package/dist/unstable/ai/internal/codec-transformer.js +15 -12
  195. package/dist/unstable/ai/internal/codec-transformer.js.map +1 -1
  196. package/dist/unstable/ai/internal/structured-output.d.ts +2 -0
  197. package/dist/unstable/ai/internal/structured-output.d.ts.map +1 -0
  198. package/dist/unstable/ai/internal/structured-output.js +316 -0
  199. package/dist/unstable/ai/internal/structured-output.js.map +1 -0
  200. package/dist/unstable/cli/CliError.d.ts +40 -2
  201. package/dist/unstable/cli/CliError.d.ts.map +1 -1
  202. package/dist/unstable/cli/CliError.js +45 -6
  203. package/dist/unstable/cli/CliError.js.map +1 -1
  204. package/dist/unstable/cli/Command.js +14 -5
  205. package/dist/unstable/cli/Command.js.map +1 -1
  206. package/dist/unstable/cli/Param.js +3 -3
  207. package/dist/unstable/cli/Param.js.map +1 -1
  208. package/dist/unstable/cli/Primitive.js +1 -1
  209. package/dist/unstable/cli/Primitive.js.map +1 -1
  210. package/dist/unstable/cli/internal/command.d.ts +3 -1
  211. package/dist/unstable/cli/internal/command.d.ts.map +1 -1
  212. package/dist/unstable/cli/internal/command.js +11 -4
  213. package/dist/unstable/cli/internal/command.js.map +1 -1
  214. package/dist/unstable/cli/internal/config.d.ts +0 -48
  215. package/dist/unstable/cli/internal/config.d.ts.map +1 -1
  216. package/dist/unstable/cli/internal/config.js +8 -10
  217. package/dist/unstable/cli/internal/config.js.map +1 -1
  218. package/dist/unstable/cluster/ClusterWorkflowEngine.js +1 -1
  219. package/dist/unstable/cluster/ClusterWorkflowEngine.js.map +1 -1
  220. package/dist/unstable/cluster/Entity.js +1 -1
  221. package/dist/unstable/cluster/Entity.js.map +1 -1
  222. package/dist/unstable/cluster/Envelope.d.ts.map +1 -1
  223. package/dist/unstable/cluster/Envelope.js +2 -1
  224. package/dist/unstable/cluster/Envelope.js.map +1 -1
  225. package/dist/unstable/cluster/Reply.js +1 -1
  226. package/dist/unstable/cluster/Reply.js.map +1 -1
  227. package/dist/unstable/cluster/Runner.d.ts +1 -1
  228. package/dist/unstable/cluster/Runner.js +1 -1
  229. package/dist/unstable/cluster/Runner.js.map +1 -1
  230. package/dist/unstable/cluster/RunnerAddress.d.ts +1 -1
  231. package/dist/unstable/cluster/RunnerAddress.js +1 -1
  232. package/dist/unstable/cluster/RunnerAddress.js.map +1 -1
  233. package/dist/unstable/cluster/ShardId.js +1 -1
  234. package/dist/unstable/cluster/ShardId.js.map +1 -1
  235. package/dist/unstable/cluster/Sharding.d.ts.map +1 -1
  236. package/dist/unstable/cluster/Sharding.js +125 -24
  237. package/dist/unstable/cluster/Sharding.js.map +1 -1
  238. package/dist/unstable/cluster/ShardingConfig.d.ts +10 -2
  239. package/dist/unstable/cluster/ShardingConfig.d.ts.map +1 -1
  240. package/dist/unstable/cluster/ShardingConfig.js.map +1 -1
  241. package/dist/unstable/cluster/SingleRunner.d.ts +4 -2
  242. package/dist/unstable/cluster/SingleRunner.d.ts.map +1 -1
  243. package/dist/unstable/cluster/SingleRunner.js +2 -1
  244. package/dist/unstable/cluster/SingleRunner.js.map +1 -1
  245. package/dist/unstable/cluster/SqlMessageStorage.d.ts +7 -4
  246. package/dist/unstable/cluster/SqlMessageStorage.d.ts.map +1 -1
  247. package/dist/unstable/cluster/SqlMessageStorage.js +55 -24
  248. package/dist/unstable/cluster/SqlMessageStorage.js.map +1 -1
  249. package/dist/unstable/cluster/SqlRunnerStorage.d.ts.map +1 -1
  250. package/dist/unstable/cluster/SqlRunnerStorage.js +109 -62
  251. package/dist/unstable/cluster/SqlRunnerStorage.js.map +1 -1
  252. package/dist/unstable/cluster/internal/entityManager.js +19 -7
  253. package/dist/unstable/cluster/internal/entityManager.js.map +1 -1
  254. package/dist/unstable/cluster/internal/shardLock.d.ts +2 -0
  255. package/dist/unstable/cluster/internal/shardLock.d.ts.map +1 -0
  256. package/dist/unstable/cluster/internal/shardLock.js +4 -0
  257. package/dist/unstable/cluster/internal/shardLock.js.map +1 -0
  258. package/dist/unstable/devtools/DevToolsSchema.d.ts +20 -20
  259. package/dist/unstable/devtools/DevToolsSchema.d.ts.map +1 -1
  260. package/dist/unstable/devtools/DevToolsSchema.js +8 -5
  261. package/dist/unstable/devtools/DevToolsSchema.js.map +1 -1
  262. package/dist/unstable/eventlog/EventJournal.d.ts +1 -1
  263. package/dist/unstable/eventlog/EventJournal.js +1 -1
  264. package/dist/unstable/eventlog/EventJournal.js.map +1 -1
  265. package/dist/unstable/eventlog/EventLog.d.ts.map +1 -1
  266. package/dist/unstable/eventlog/EventLog.js +15 -11
  267. package/dist/unstable/eventlog/EventLog.js.map +1 -1
  268. package/dist/unstable/eventlog/EventLogEncryption.d.ts +1 -1
  269. package/dist/unstable/eventlog/EventLogEncryption.js +1 -1
  270. package/dist/unstable/eventlog/EventLogEncryption.js.map +1 -1
  271. package/dist/unstable/eventlog/EventLogMessage.d.ts +4 -4
  272. package/dist/unstable/eventlog/EventLogMessage.d.ts.map +1 -1
  273. package/dist/unstable/eventlog/EventLogMessage.js +8 -3
  274. package/dist/unstable/eventlog/EventLogMessage.js.map +1 -1
  275. package/dist/unstable/eventlog/EventLogRemote.d.ts +1 -1
  276. package/dist/unstable/eventlog/SqlEventJournal.js +2 -2
  277. package/dist/unstable/eventlog/SqlEventJournal.js.map +1 -1
  278. package/dist/unstable/eventlog/SqlEventLogServerEncrypted.js +1 -1
  279. package/dist/unstable/eventlog/SqlEventLogServerEncrypted.js.map +1 -1
  280. package/dist/unstable/eventlog/SqlEventLogServerUnencrypted.js +3 -3
  281. package/dist/unstable/eventlog/SqlEventLogServerUnencrypted.js.map +1 -1
  282. package/dist/unstable/http/Cookies.d.ts.map +1 -1
  283. package/dist/unstable/http/Cookies.js +24 -22
  284. package/dist/unstable/http/Cookies.js.map +1 -1
  285. package/dist/unstable/http/Headers.js +8 -8
  286. package/dist/unstable/http/Headers.js.map +1 -1
  287. package/dist/unstable/http/HttpClientRequest.d.ts +44 -0
  288. package/dist/unstable/http/HttpClientRequest.d.ts.map +1 -1
  289. package/dist/unstable/http/HttpClientRequest.js +14 -0
  290. package/dist/unstable/http/HttpClientRequest.js.map +1 -1
  291. package/dist/unstable/http/HttpServerRequest.d.ts.map +1 -1
  292. package/dist/unstable/http/HttpServerRequest.js +5 -4
  293. package/dist/unstable/http/HttpServerRequest.js.map +1 -1
  294. package/dist/unstable/http/Multipart.d.ts.map +1 -1
  295. package/dist/unstable/http/Multipart.js +17 -15
  296. package/dist/unstable/http/Multipart.js.map +1 -1
  297. package/dist/unstable/http/Template.js +1 -1
  298. package/dist/unstable/http/Template.js.map +1 -1
  299. package/dist/unstable/http/UrlParams.d.ts.map +1 -1
  300. package/dist/unstable/http/UrlParams.js +17 -14
  301. package/dist/unstable/http/UrlParams.js.map +1 -1
  302. package/dist/unstable/httpapi/HttpApi.js +3 -3
  303. package/dist/unstable/httpapi/HttpApi.js.map +1 -1
  304. package/dist/unstable/httpapi/HttpApiClient.js +5 -5
  305. package/dist/unstable/httpapi/HttpApiClient.js.map +1 -1
  306. package/dist/unstable/httpapi/HttpApiGroup.js +1 -1
  307. package/dist/unstable/httpapi/HttpApiGroup.js.map +1 -1
  308. package/dist/unstable/httpapi/OpenApi.d.ts.map +1 -1
  309. package/dist/unstable/httpapi/OpenApi.js +37 -24
  310. package/dist/unstable/httpapi/OpenApi.js.map +1 -1
  311. package/dist/unstable/observability/OtlpExporter.d.ts +72 -1
  312. package/dist/unstable/observability/OtlpExporter.d.ts.map +1 -1
  313. package/dist/unstable/observability/OtlpExporter.js +69 -4
  314. package/dist/unstable/observability/OtlpExporter.js.map +1 -1
  315. package/dist/unstable/observability/OtlpLogger.d.ts +4 -3
  316. package/dist/unstable/observability/OtlpLogger.d.ts.map +1 -1
  317. package/dist/unstable/observability/OtlpLogger.js +2 -2
  318. package/dist/unstable/observability/OtlpLogger.js.map +1 -1
  319. package/dist/unstable/observability/OtlpMetrics.d.ts +7 -3
  320. package/dist/unstable/observability/OtlpMetrics.d.ts.map +1 -1
  321. package/dist/unstable/observability/OtlpMetrics.js +5 -2
  322. package/dist/unstable/observability/OtlpMetrics.js.map +1 -1
  323. package/dist/unstable/observability/OtlpTracer.d.ts +4 -3
  324. package/dist/unstable/observability/OtlpTracer.d.ts.map +1 -1
  325. package/dist/unstable/observability/OtlpTracer.js +2 -2
  326. package/dist/unstable/observability/OtlpTracer.js.map +1 -1
  327. package/dist/unstable/persistence/Persistable.d.ts.map +1 -1
  328. package/dist/unstable/persistence/Persistable.js +2 -1
  329. package/dist/unstable/persistence/Persistable.js.map +1 -1
  330. package/dist/unstable/persistence/RateLimiter.d.ts +2 -2
  331. package/dist/unstable/persistence/RateLimiter.js +2 -2
  332. package/dist/unstable/persistence/RateLimiter.js.map +1 -1
  333. package/dist/unstable/persistence/Redis.d.ts.map +1 -1
  334. package/dist/unstable/persistence/Redis.js +2 -2
  335. package/dist/unstable/persistence/Redis.js.map +1 -1
  336. package/dist/unstable/process/ChildProcessSpawner.js +3 -1
  337. package/dist/unstable/process/ChildProcessSpawner.js.map +1 -1
  338. package/dist/unstable/reactivity/AsyncResult.d.ts.map +1 -1
  339. package/dist/unstable/reactivity/AsyncResult.js +4 -3
  340. package/dist/unstable/reactivity/AsyncResult.js.map +1 -1
  341. package/dist/unstable/reactivity/Atom.d.ts +83 -0
  342. package/dist/unstable/reactivity/Atom.d.ts.map +1 -1
  343. package/dist/unstable/reactivity/Atom.js +31 -0
  344. package/dist/unstable/reactivity/Atom.js.map +1 -1
  345. package/dist/unstable/reactivity/AtomRegistry.js +1 -1
  346. package/dist/unstable/reactivity/AtomRegistry.js.map +1 -1
  347. package/dist/unstable/rpc/RpcClient.d.ts.map +1 -1
  348. package/dist/unstable/rpc/RpcClient.js +2 -1
  349. package/dist/unstable/rpc/RpcClient.js.map +1 -1
  350. package/dist/unstable/schema/VariantSchema.d.ts +1 -2
  351. package/dist/unstable/schema/VariantSchema.d.ts.map +1 -1
  352. package/dist/unstable/schema/VariantSchema.js +13 -10
  353. package/dist/unstable/schema/VariantSchema.js.map +1 -1
  354. package/dist/unstable/socket/Socket.d.ts +1 -1
  355. package/dist/unstable/socket/Socket.js +1 -1
  356. package/dist/unstable/socket/Socket.js.map +1 -1
  357. package/dist/unstable/sql/SqlError.d.ts +2 -2
  358. package/dist/unstable/sql/SqlError.js +2 -2
  359. package/dist/unstable/sql/SqlError.js.map +1 -1
  360. package/dist/unstable/sql/Statement.d.ts.map +1 -1
  361. package/dist/unstable/sql/Statement.js +7 -6
  362. package/dist/unstable/sql/Statement.js.map +1 -1
  363. package/package.json +1 -1
  364. package/src/Array.ts +86 -25
  365. package/src/Channel.ts +32 -14
  366. package/src/Config.ts +6 -5
  367. package/src/ConfigProvider.ts +7 -7
  368. package/src/Data.ts +3 -2
  369. package/src/Effect.ts +70 -116
  370. package/src/FiberHandle.ts +3 -2
  371. package/src/Iterable.ts +2 -1
  372. package/src/JsonPatch.ts +4 -3
  373. package/src/JsonSchema.ts +9 -11
  374. package/src/Logger.ts +3 -2
  375. package/src/ManagedRuntime.ts +13 -0
  376. package/src/Metric.ts +4 -3
  377. package/src/Optic.ts +5 -4
  378. package/src/Option.ts +2 -1
  379. package/src/PartitionedSemaphore.ts +1 -4
  380. package/src/Record.ts +119 -22
  381. package/src/References.ts +0 -46
  382. package/src/Request.ts +5 -7
  383. package/src/Result.ts +2 -1
  384. package/src/Schema.ts +2537 -986
  385. package/src/SchemaAST.ts +228 -149
  386. package/src/SchemaError.ts +1 -1
  387. package/src/SchemaGetter.ts +36 -29
  388. package/src/SchemaIssue.ts +1 -1
  389. package/src/SchemaParser.ts +1 -25
  390. package/src/SchemaRepresentation.ts +697 -3398
  391. package/src/SchemaTransformation.ts +3 -3
  392. package/src/Semaphore.ts +5 -3
  393. package/src/Stream.ts +63 -19
  394. package/src/Struct.ts +5 -4
  395. package/src/Types.ts +1 -3
  396. package/src/index.ts +0 -5
  397. package/src/internal/core.ts +3 -2
  398. package/src/internal/effect.ts +67 -31
  399. package/src/internal/matcher.ts +1 -1
  400. package/src/internal/record.ts +11 -7
  401. package/src/internal/references.ts +0 -5
  402. package/src/internal/schema/annotations.ts +44 -0
  403. package/src/internal/schema/fromJsonSchemaDocument.ts +957 -0
  404. package/src/internal/schema/fromRepresentation.ts +345 -0
  405. package/src/internal/schema/schema.ts +45 -46
  406. package/src/internal/schema/{arbitrary.ts → toArbitrary.ts} +8 -10
  407. package/src/internal/schema/toCodeDocument.ts +572 -0
  408. package/src/internal/schema/{equivalence.ts → toEquivalence.ts} +12 -7
  409. package/src/internal/schema/toJsonSchemaDocument.ts +475 -0
  410. package/src/internal/schema/toRepresentation.ts +401 -0
  411. package/src/internal/stackTraceLimit.ts +3 -7
  412. package/src/testing/TestClock.ts +1 -1
  413. package/src/unstable/ai/AiError.ts +5 -4
  414. package/src/unstable/ai/AnthropicStructuredOutput.ts +102 -333
  415. package/src/unstable/ai/EmbeddingModel.ts +1 -1
  416. package/src/unstable/ai/LanguageModel.ts +30 -28
  417. package/src/unstable/ai/McpSchema.ts +15 -15
  418. package/src/unstable/ai/McpServer.ts +172 -56
  419. package/src/unstable/ai/OpenAiStructuredOutput.ts +230 -395
  420. package/src/unstable/ai/Response.ts +24 -24
  421. package/src/unstable/ai/Tool.ts +22 -4
  422. package/src/unstable/ai/Toolkit.ts +19 -7
  423. package/src/unstable/ai/internal/codec-transformer.ts +13 -7
  424. package/src/unstable/ai/internal/structured-output.ts +458 -0
  425. package/src/unstable/cli/CliError.ts +51 -5
  426. package/src/unstable/cli/Command.ts +4 -4
  427. package/src/unstable/cli/Param.ts +3 -3
  428. package/src/unstable/cli/Primitive.ts +1 -1
  429. package/src/unstable/cli/internal/command.ts +13 -5
  430. package/src/unstable/cli/internal/config.ts +8 -10
  431. package/src/unstable/cluster/ClusterWorkflowEngine.ts +3 -3
  432. package/src/unstable/cluster/Entity.ts +1 -1
  433. package/src/unstable/cluster/Envelope.ts +2 -1
  434. package/src/unstable/cluster/Reply.ts +1 -1
  435. package/src/unstable/cluster/Runner.ts +1 -1
  436. package/src/unstable/cluster/RunnerAddress.ts +1 -1
  437. package/src/unstable/cluster/ShardId.ts +1 -1
  438. package/src/unstable/cluster/Sharding.ts +161 -34
  439. package/src/unstable/cluster/ShardingConfig.ts +5 -1
  440. package/src/unstable/cluster/SingleRunner.ts +4 -2
  441. package/src/unstable/cluster/SqlMessageStorage.ts +83 -35
  442. package/src/unstable/cluster/SqlRunnerStorage.ts +186 -116
  443. package/src/unstable/cluster/internal/entityManager.ts +31 -10
  444. package/src/unstable/cluster/internal/shardLock.ts +9 -0
  445. package/src/unstable/devtools/DevToolsSchema.ts +10 -5
  446. package/src/unstable/eventlog/EventJournal.ts +1 -1
  447. package/src/unstable/eventlog/EventLog.ts +21 -11
  448. package/src/unstable/eventlog/EventLogEncryption.ts +1 -1
  449. package/src/unstable/eventlog/EventLogMessage.ts +10 -3
  450. package/src/unstable/eventlog/SqlEventJournal.ts +2 -2
  451. package/src/unstable/eventlog/SqlEventLogServerEncrypted.ts +1 -1
  452. package/src/unstable/eventlog/SqlEventLogServerUnencrypted.ts +6 -3
  453. package/src/unstable/http/Cookies.ts +29 -24
  454. package/src/unstable/http/Headers.ts +8 -8
  455. package/src/unstable/http/HttpClientRequest.ts +57 -0
  456. package/src/unstable/http/HttpServerRequest.ts +5 -4
  457. package/src/unstable/http/Multipart.ts +16 -13
  458. package/src/unstable/http/Template.ts +1 -1
  459. package/src/unstable/http/UrlParams.ts +17 -14
  460. package/src/unstable/httpapi/HttpApi.ts +3 -3
  461. package/src/unstable/httpapi/HttpApiClient.ts +5 -5
  462. package/src/unstable/httpapi/HttpApiGroup.ts +1 -1
  463. package/src/unstable/httpapi/OpenApi.ts +52 -28
  464. package/src/unstable/observability/OtlpExporter.ts +109 -5
  465. package/src/unstable/observability/OtlpLogger.ts +5 -5
  466. package/src/unstable/observability/OtlpMetrics.ts +8 -4
  467. package/src/unstable/observability/OtlpTracer.ts +8 -4
  468. package/src/unstable/persistence/Persistable.ts +3 -2
  469. package/src/unstable/persistence/RateLimiter.ts +2 -2
  470. package/src/unstable/persistence/Redis.ts +2 -2
  471. package/src/unstable/process/ChildProcessSpawner.ts +1 -1
  472. package/src/unstable/reactivity/AsyncResult.ts +4 -3
  473. package/src/unstable/reactivity/Atom.ts +92 -0
  474. package/src/unstable/reactivity/AtomRegistry.ts +1 -1
  475. package/src/unstable/rpc/RpcClient.ts +2 -1
  476. package/src/unstable/schema/VariantSchema.ts +14 -24
  477. package/src/unstable/socket/Socket.ts +1 -1
  478. package/src/unstable/sql/SqlError.ts +2 -2
  479. package/src/unstable/sql/Statement.ts +7 -6
  480. package/dist/SchemaUtils.d.ts +0 -28
  481. package/dist/SchemaUtils.d.ts.map +0 -1
  482. package/dist/SchemaUtils.js +0 -45
  483. package/dist/SchemaUtils.js.map +0 -1
  484. package/dist/internal/schema/arbitrary.d.ts.map +0 -1
  485. package/dist/internal/schema/arbitrary.js.map +0 -1
  486. package/dist/internal/schema/equivalence.d.ts +0 -2
  487. package/dist/internal/schema/equivalence.d.ts.map +0 -1
  488. package/dist/internal/schema/equivalence.js.map +0 -1
  489. package/dist/internal/schema/representation.d.ts +0 -2
  490. package/dist/internal/schema/representation.d.ts.map +0 -1
  491. package/dist/internal/schema/representation.js +0 -865
  492. package/dist/internal/schema/representation.js.map +0 -1
  493. package/src/SchemaUtils.ts +0 -51
  494. package/src/internal/schema/representation.ts +0 -795
@@ -0,0 +1,572 @@
1
+ import * as Arr from "../../Array.ts"
2
+ import { format, formatPropertyKey } from "../../Formatter.ts"
3
+ import type * as Schema from "../../Schema.ts"
4
+ import type * as SchemaRepresentation from "../../SchemaRepresentation.ts"
5
+ import { errorWithPath } from "../errors.ts"
6
+ import * as InternalRecord from "../record.ts"
7
+ import * as InternalAnnotations from "./annotations.ts"
8
+
9
+ type Path = ReadonlyArray<string | number>
10
+ type CheckRepresentationAnnotation = SchemaRepresentation.CheckRepresentationAnnotation<
11
+ SchemaRepresentation.Representation
12
+ >
13
+
14
+ /** @internal */
15
+ export function makeCode(runtime: string, Type: string): SchemaRepresentation.Code {
16
+ return { runtime, Type }
17
+ }
18
+
19
+ function renderEmittableAnnotation(input: unknown): string | undefined {
20
+ if (
21
+ input === null ||
22
+ typeof input === "string" ||
23
+ typeof input === "boolean" ||
24
+ typeof input === "number" ||
25
+ typeof input === "bigint"
26
+ ) return format(input)
27
+ if (typeof input === "symbol") {
28
+ const key = globalThis.Symbol.keyFor(input)
29
+ return key === undefined ? undefined : `Symbol.for(${format(key)})`
30
+ }
31
+ if (typeof input !== "object") return undefined
32
+ if (Array.isArray(input)) {
33
+ const values: Array<string> = []
34
+ for (const value of input) {
35
+ const rendered = renderEmittableAnnotation(value)
36
+ if (rendered === undefined) return undefined
37
+ values.push(rendered)
38
+ }
39
+ return `[${values.join(", ")}]`
40
+ }
41
+
42
+ const entries: Array<string> = []
43
+ for (const [key, value] of Object.entries(input)) {
44
+ const rendered = renderEmittableAnnotation(value)
45
+ if (rendered === undefined) return undefined
46
+ entries.push(`${formatPropertyKey(key)}: ${rendered}`)
47
+ }
48
+ return `{ ${entries.join(", ")} }`
49
+ }
50
+
51
+ function renderAnnotations(
52
+ annotations: Schema.Annotations.Annotations | undefined
53
+ ): string | undefined {
54
+ if (annotations === undefined) return undefined
55
+ const entries: Array<string> = []
56
+ for (const [key, value] of Object.entries(annotations)) {
57
+ if (InternalAnnotations.annotationExcludedKeys.has(key)) continue
58
+ const rendered = renderEmittableAnnotation(value)
59
+ if (rendered !== undefined) entries.push(`${formatPropertyKey(key)}: ${rendered}`)
60
+ }
61
+ return entries.length === 0 ? undefined : `{ ${entries.join(", ")} }`
62
+ }
63
+
64
+ /** @internal */
65
+ export function sanitizeJavaScriptIdentifier(input: string): string {
66
+ if (input.length === 0) return "_"
67
+ const out = input.replace(/[^A-Za-z0-9_$]/gu, "_")
68
+ const first = out[0]
69
+ return first >= "a" && first <= "z"
70
+ ? first.toUpperCase() + out.slice(1)
71
+ : first >= "0" && first <= "9"
72
+ ? `_${out}`
73
+ : out
74
+ }
75
+
76
+ function isSimpleLiveLiteral(
77
+ representation: SchemaRepresentation.Representation
78
+ ): representation is SchemaRepresentation.Literal {
79
+ return representation._tag === "Literal" && representation.checks.length === 0 &&
80
+ representation.annotations === undefined
81
+ }
82
+
83
+ /** @internal */
84
+ export interface TopologicalSort {
85
+ readonly nonRecursives: ReadonlyArray<{
86
+ readonly $ref: string
87
+ readonly representation: SchemaRepresentation.Representation
88
+ }>
89
+ readonly recursives: Readonly<Record<string, SchemaRepresentation.Representation>>
90
+ }
91
+
92
+ /** @internal */
93
+ export function topologicalSort(
94
+ references: SchemaRepresentation.References
95
+ ): TopologicalSort {
96
+ const identifiers = Object.keys(references)
97
+ const identifierSet = new Set(identifiers)
98
+
99
+ function collectRefs(root: SchemaRepresentation.Representation): ReadonlySet<string> {
100
+ const refs = new Set<string>()
101
+ const visited = new WeakSet<object>()
102
+ const stack: Array<SchemaRepresentation.Representation> = [root]
103
+
104
+ function pushRepresentationSchemas(representation: CheckRepresentationAnnotation | undefined): void {
105
+ if (representation?.schemas !== undefined) stack.push(...representation.schemas)
106
+ }
107
+
108
+ function pushChecks(
109
+ checks: ReadonlyArray<SchemaRepresentation.Check>
110
+ ): void {
111
+ for (const check of checks) {
112
+ pushRepresentationSchemas(check.representation)
113
+ if (check._tag === "FilterGroup") pushChecks(check.checks)
114
+ }
115
+ }
116
+
117
+ while (stack.length > 0) {
118
+ const representation = stack.pop()!
119
+ if (visited.has(representation)) continue
120
+ visited.add(representation)
121
+ if (representation._tag === "Reference") {
122
+ if (identifierSet.has(representation.$ref)) refs.add(representation.$ref)
123
+ continue
124
+ }
125
+
126
+ pushChecks(representation.checks)
127
+ switch (representation._tag) {
128
+ case "Declaration":
129
+ pushRepresentationSchemas(representation.representation)
130
+ stack.push(...representation.typeParameters)
131
+ break
132
+ case "Suspend":
133
+ stack.push(representation.thunk)
134
+ break
135
+ case "TemplateLiteral":
136
+ stack.push(...representation.parts)
137
+ break
138
+ case "Arrays":
139
+ for (const element of representation.elements) stack.push(element.type)
140
+ stack.push(...representation.rest)
141
+ break
142
+ case "Objects":
143
+ for (const property of representation.propertySignatures) stack.push(property.type)
144
+ for (const signature of representation.indexSignatures) {
145
+ stack.push(signature.parameter, signature.type)
146
+ }
147
+ break
148
+ case "Union":
149
+ stack.push(...representation.types)
150
+ break
151
+ }
152
+ }
153
+ return refs
154
+ }
155
+
156
+ const dependencies = new Map<string, ReadonlySet<string>>(
157
+ identifiers.map((identifier) => [identifier, collectRefs(references[identifier])])
158
+ )
159
+ const recursive = new Set<string>()
160
+ const state = new Map<string, 0 | 1 | 2>()
161
+ const stack: Array<string> = []
162
+
163
+ function visit(identifier: string): void {
164
+ const current = state.get(identifier) ?? 0
165
+ if (current === 1) {
166
+ const start = stack.indexOf(identifier)
167
+ for (let index = start; index < stack.length; index++) recursive.add(stack[index])
168
+ return
169
+ }
170
+ if (current === 2) return
171
+ state.set(identifier, 1)
172
+ stack.push(identifier)
173
+ for (const dependency of dependencies.get(identifier)!) visit(dependency)
174
+ stack.pop()
175
+ state.set(identifier, 2)
176
+ }
177
+
178
+ for (const identifier of identifiers) visit(identifier)
179
+
180
+ const inDegree = new Map<string, number>()
181
+ const dependents = new Map<string, Set<string>>()
182
+ for (const identifier of identifiers) {
183
+ if (!recursive.has(identifier)) {
184
+ inDegree.set(identifier, 0)
185
+ dependents.set(identifier, new Set())
186
+ }
187
+ }
188
+ for (const [identifier, internalDependencies] of dependencies) {
189
+ if (recursive.has(identifier)) continue
190
+ for (const dependency of internalDependencies) {
191
+ if (recursive.has(dependency)) continue
192
+ inDegree.set(identifier, inDegree.get(identifier)! + 1)
193
+ dependents.get(dependency)!.add(identifier)
194
+ }
195
+ }
196
+
197
+ const queue: Array<string> = []
198
+ for (const [identifier, degree] of inDegree) {
199
+ if (degree === 0) queue.push(identifier)
200
+ }
201
+ const nonRecursives: Array<{
202
+ readonly $ref: string
203
+ readonly representation: SchemaRepresentation.Representation
204
+ }> = []
205
+ for (let index = 0; index < queue.length; index++) {
206
+ const $ref = queue[index]
207
+ nonRecursives.push({ $ref, representation: references[$ref] })
208
+ for (const dependent of dependents.get($ref)!) {
209
+ const degree = inDegree.get(dependent)! - 1
210
+ inDegree.set(dependent, degree)
211
+ if (degree === 0) queue.push(dependent)
212
+ }
213
+ }
214
+ const recursives: Record<string, SchemaRepresentation.Representation> = {}
215
+ for (const identifier of recursive) InternalRecord.assignProperty(recursives, identifier, references[identifier])
216
+ return { nonRecursives, recursives }
217
+ }
218
+
219
+ /** @internal */
220
+ export function toCodeDocument(
221
+ document: SchemaRepresentation.MultiDocument
222
+ ): SchemaRepresentation.CodeDocument {
223
+ const artifacts: Array<SchemaRepresentation.Artifact> = []
224
+ const sorted = topologicalSort(document.references)
225
+ const sanitizedReferences = new Map<string, string>()
226
+ const uniqueIdentifiers = new Set<string>()
227
+ let compilingRecursiveDefinition = false
228
+ let explicitSuspendDepth = 0
229
+
230
+ for (const { $ref } of sorted.nonRecursives) ensureUniqueIdentifier($ref)
231
+ for (const $ref of Object.keys(sorted.recursives)) ensureUniqueIdentifier($ref)
232
+
233
+ const nonRecursives = sorted.nonRecursives.map(({ $ref, representation }) => ({
234
+ $ref: ensureUniqueIdentifier($ref),
235
+ code: recur(representation, ["references", $ref])
236
+ }))
237
+ const recursives: Record<string, SchemaRepresentation.Code> = {}
238
+ for (const [$ref, representation] of Object.entries(sorted.recursives)) {
239
+ compilingRecursiveDefinition = true
240
+ InternalRecord.assignProperty(recursives, ensureUniqueIdentifier($ref), recur(representation, ["references", $ref]))
241
+ compilingRecursiveDefinition = false
242
+ }
243
+ const codes = document.representations.map((representation, index) =>
244
+ recur(representation, ["representations", index])
245
+ )
246
+
247
+ return {
248
+ codes,
249
+ references: { nonRecursives, recursives },
250
+ artifacts
251
+ }
252
+
253
+ function ensureUniqueIdentifier(original: string): string {
254
+ const existing = sanitizedReferences.get(original)
255
+ if (existing !== undefined) return existing
256
+ const candidate = freshIdentifier(original)
257
+ sanitizedReferences.set(original, candidate)
258
+ return candidate
259
+ }
260
+
261
+ function freshIdentifier(seed: string): string {
262
+ const sanitized = sanitizeJavaScriptIdentifier(seed)
263
+ let candidate = sanitized
264
+ let suffix = 0
265
+ while (uniqueIdentifiers.has(candidate)) candidate = `${sanitized}${++suffix}`
266
+ uniqueIdentifiers.add(candidate)
267
+ return candidate
268
+ }
269
+
270
+ function addImport(importDeclaration: string): void {
271
+ if (!artifacts.some((artifact) => artifact._tag === "Import" && artifact.importDeclaration === importDeclaration)) {
272
+ artifacts.push({ _tag: "Import", importDeclaration })
273
+ }
274
+ }
275
+
276
+ function addSymbol(symbol: symbol): string {
277
+ const identifier = freshIdentifier("_symbol")
278
+ const key = globalThis.Symbol.keyFor(symbol)
279
+ const description = symbol.description
280
+ artifacts.push({
281
+ _tag: "Symbol",
282
+ identifier,
283
+ code: makeCode(
284
+ key === undefined
285
+ ? `Symbol(${description === undefined ? "" : format(description)})`
286
+ : `Symbol.for(${format(key)})`,
287
+ `typeof ${identifier}`
288
+ )
289
+ })
290
+ return identifier
291
+ }
292
+
293
+ function annotationSchemas(
294
+ representation: CheckRepresentationAnnotation | undefined,
295
+ path: Path
296
+ ): ReadonlyArray<SchemaRepresentation.Code> {
297
+ return representation?.schemas?.map((schema, index) => recur(schema, [...path, "schemas", index])) ?? []
298
+ }
299
+
300
+ function checkBrands(
301
+ check: SchemaRepresentation.Check
302
+ ): ReadonlyArray<string> {
303
+ const own = InternalAnnotations.collectBrands(check.annotations)
304
+ if (
305
+ check._tag === "FilterGroup" &&
306
+ check.annotations?.toCode === undefined
307
+ ) {
308
+ return [...own, ...check.checks.flatMap(checkBrands)]
309
+ }
310
+ return own
311
+ }
312
+
313
+ function runtimeBrands(brands: ReadonlyArray<string>): string {
314
+ return brands.length === 0
315
+ ? ""
316
+ : `.pipe(${brands.map((brand) => `Schema.brand(${format(brand)})`).join(", ")})`
317
+ }
318
+
319
+ function typeBrands(brands: ReadonlyArray<string>): string {
320
+ if (brands.length === 0) return ""
321
+ addImport(`import type * as Brand from "effect/Brand"`)
322
+ return brands.map((brand) => ` & Brand.Brand<${format(brand)}>`).join("")
323
+ }
324
+
325
+ function runtimeAnnotate(
326
+ annotations: Schema.Annotations.Annotations | undefined,
327
+ method: "annotate" | "annotateKey" = "annotate"
328
+ ): string {
329
+ const rendered = renderAnnotations(annotations)
330
+ return rendered === undefined ? "" : `.${method}(${rendered})`
331
+ }
332
+
333
+ function compileCheck(
334
+ check: SchemaRepresentation.Check,
335
+ path: Path
336
+ ): string {
337
+ const callback = check.annotations?.toCode
338
+ let runtime: string
339
+ if (callback !== undefined) {
340
+ const schemas = annotationSchemas(check.representation, [...path, "representation"])
341
+ const output = (callback as SchemaRepresentation.Generation.Check)({ schemas })
342
+ for (const importDeclaration of output.importDeclarations ?? []) addImport(importDeclaration)
343
+ runtime = output.runtime
344
+ } else if (check._tag === "Filter") {
345
+ throw errorWithPath("Missing toCode callback", [...path, "annotations", "toCode"])
346
+ } else {
347
+ runtime = `Schema.makeFilterGroup([${
348
+ check.checks.map((child, index) => compileCheck(child, [...path, "checks", index])).join(", ")
349
+ }])`
350
+ }
351
+ runtime += runtimeAnnotate(check.annotations)
352
+ if (check._tag === "Filter" && check.aborted) runtime += ".abort()"
353
+ return runtime
354
+ }
355
+
356
+ function applyNode(
357
+ base: SchemaRepresentation.Code,
358
+ representation: Exclude<SchemaRepresentation.Representation, SchemaRepresentation.Reference>,
359
+ path: Path,
360
+ includeTypeBrands: boolean = true
361
+ ): SchemaRepresentation.Code {
362
+ const nodeBrands = InternalAnnotations.collectBrands(representation.annotations)
363
+ let runtime = base.runtime + runtimeAnnotate(representation.annotations) + runtimeBrands(nodeBrands)
364
+ let Type = base.Type + (includeTypeBrands ? typeBrands(nodeBrands) : "")
365
+ for (let index = 0; index < representation.checks.length; index++) {
366
+ const check = representation.checks[index]
367
+ const brands = checkBrands(check)
368
+ runtime += `.check(${compileCheck(check, [...path, "checks", index])})${runtimeBrands(brands)}`
369
+ if (includeTypeBrands) Type += typeBrands(brands)
370
+ }
371
+ return makeCode(runtime, Type)
372
+ }
373
+
374
+ function recur(
375
+ representation: SchemaRepresentation.Representation,
376
+ path: Path,
377
+ includeTypeBrands: boolean = true
378
+ ): SchemaRepresentation.Code {
379
+ if (representation._tag === "Reference") {
380
+ if (!Object.hasOwn(document.references, representation.$ref)) {
381
+ throw errorWithPath(`Invalid reference ${representation.$ref}`, [...path, "$ref"])
382
+ }
383
+ const identifier = ensureUniqueIdentifier(representation.$ref)
384
+ if (
385
+ compilingRecursiveDefinition && explicitSuspendDepth === 0 &&
386
+ Object.hasOwn(sorted.recursives, representation.$ref)
387
+ ) {
388
+ return makeCode(`Schema.suspend((): Schema.Codec<${identifier}> => ${identifier})`, identifier)
389
+ }
390
+ const Type = includeTypeBrands
391
+ ? identifier
392
+ : recur(document.references[representation.$ref], ["references", representation.$ref], false).Type
393
+ return makeCode(identifier, Type)
394
+ }
395
+ return applyNode(on(representation, path, includeTypeBrands), representation, path, includeTypeBrands)
396
+ }
397
+
398
+ function on(
399
+ representation: Exclude<SchemaRepresentation.Representation, SchemaRepresentation.Reference>,
400
+ path: Path,
401
+ includeTypeBrands: boolean
402
+ ): SchemaRepresentation.Code {
403
+ switch (representation._tag) {
404
+ case "Declaration": {
405
+ const callback = representation.annotations?.toCode
406
+ if (callback === undefined) {
407
+ throw errorWithPath("Missing toCode callback", [...path, "annotations", "toCode"])
408
+ }
409
+ const typeParameters = representation.typeParameters.map((typeParameter, index) =>
410
+ recur(typeParameter, [...path, "typeParameters", index])
411
+ )
412
+ const output = (callback as SchemaRepresentation.Generation.Declaration)({ typeParameters })
413
+ for (const importDeclaration of output.importDeclarations ?? []) addImport(importDeclaration)
414
+ return makeCode(output.runtime, output.Type)
415
+ }
416
+ case "Suspend": {
417
+ explicitSuspendDepth++
418
+ const thunk = recur(representation.thunk, [...path, "thunk"])
419
+ explicitSuspendDepth--
420
+ return makeCode(`Schema.suspend((): Schema.Codec<${thunk.Type}> => ${thunk.runtime})`, thunk.Type)
421
+ }
422
+ case "Null":
423
+ return makeCode("Schema.Null", "null")
424
+ case "Undefined":
425
+ return makeCode("Schema.Undefined", "undefined")
426
+ case "Void":
427
+ return makeCode("Schema.Void", "void")
428
+ case "Never":
429
+ return makeCode("Schema.Never", "never")
430
+ case "Unknown":
431
+ return makeCode("Schema.Unknown", "unknown")
432
+ case "Any":
433
+ return makeCode("Schema.Any", "any")
434
+ case "String":
435
+ return makeCode("Schema.String", "string")
436
+ case "Number":
437
+ return makeCode("Schema.Number", "number")
438
+ case "Boolean":
439
+ return makeCode("Schema.Boolean", "boolean")
440
+ case "BigInt":
441
+ return makeCode("Schema.BigInt", "bigint")
442
+ case "Symbol":
443
+ return makeCode("Schema.Symbol", "symbol")
444
+ case "Literal": {
445
+ const literal = format(representation.literal)
446
+ return makeCode(`Schema.Literal(${literal})`, literal)
447
+ }
448
+ case "UniqueSymbol": {
449
+ const identifier = addSymbol(representation.symbol)
450
+ return makeCode(`Schema.UniqueSymbol(${identifier})`, `typeof ${identifier}`)
451
+ }
452
+ case "ObjectKeyword":
453
+ return makeCode("Schema.ObjectKeyword", "object")
454
+ case "Enum": {
455
+ const identifier = freshIdentifier("_Enum")
456
+ artifacts.push({
457
+ _tag: "Enum",
458
+ identifier,
459
+ code: makeCode(
460
+ `enum ${identifier} { ${
461
+ representation.enums.map(([name, value]) => `${format(name)} = ${format(value)}`).join(
462
+ ", "
463
+ )
464
+ } }`,
465
+ `typeof ${identifier}`
466
+ )
467
+ })
468
+ return makeCode(`Schema.Enum(${identifier})`, identifier)
469
+ }
470
+ case "TemplateLiteral": {
471
+ const parts = representation.parts.map((part, index) => recur(part, [...path, "parts", index], false))
472
+ const Type = `\`${parts.map((part) => `\${${part.Type}}`).join("")}\``
473
+ return makeCode(`Schema.TemplateLiteral([${parts.map((part) => part.runtime).join(", ")}])`, Type)
474
+ }
475
+ case "Arrays": {
476
+ const elements = representation.elements.map((element, index) => {
477
+ const type = recur(element.type, [...path, "elements", index, "type"])
478
+ return makeCode(
479
+ `${element.isOptional ? "Schema.optionalKey(" : ""}${type.runtime}${element.isOptional ? ")" : ""}${
480
+ runtimeAnnotate(element.annotations, "annotateKey")
481
+ }`,
482
+ `${type.Type}${element.isOptional ? "?" : ""}`
483
+ )
484
+ })
485
+ const rest = representation.rest.map((item, index) => recur(item, [...path, "rest", index]))
486
+ if (Arr.isArrayNonEmpty(rest)) {
487
+ const item = rest[0]
488
+ if (elements.length === 0 && rest.length === 1) {
489
+ return makeCode(`Schema.Array(${item.runtime})`, `ReadonlyArray<${item.Type}>`)
490
+ }
491
+ const post = rest.slice(1)
492
+ return makeCode(
493
+ `Schema.TupleWithRest(Schema.Tuple([${elements.map((element) => element.runtime).join(", ")}]), [${
494
+ rest.map((item) => item.runtime).join(", ")
495
+ }])`,
496
+ `readonly [${elements.map((element) => element.Type).join(", ")}, ...Array<${item.Type}>${
497
+ post.length > 0 ? `, ${post.map((item) => item.Type).join(", ")}` : ""
498
+ }]`
499
+ )
500
+ }
501
+ return makeCode(
502
+ `Schema.Tuple([${elements.map((element) => element.runtime).join(", ")}])`,
503
+ `readonly [${elements.map((element) => element.Type).join(", ")}]`
504
+ )
505
+ }
506
+ case "Objects": {
507
+ const properties = representation.propertySignatures.map((property, index) => {
508
+ const isSymbol = typeof property.name === "symbol"
509
+ const name = isSymbol
510
+ ? addSymbol(property.name)
511
+ : formatPropertyKey(property.name)
512
+ const type = recur(property.type, [...path, "propertySignatures", index, "type"])
513
+ let runtime = type.runtime
514
+ if (property.isMutable) runtime = `Schema.mutableKey(${runtime})`
515
+ if (property.isOptional) runtime = `Schema.optionalKey(${runtime})`
516
+ const runtimeName = isSymbol ? `[${name}]` : name
517
+ const typeName = `${property.isMutable ? "" : "readonly "}${runtimeName}${property.isOptional ? "?" : ""}`
518
+ return makeCode(
519
+ `${runtimeName}: ${runtime}${runtimeAnnotate(property.annotations, "annotateKey")}`,
520
+ `${typeName}: ${type.Type}`
521
+ )
522
+ })
523
+ const indexSignatures = representation.indexSignatures.map((signature, index) => ({
524
+ parameter: recur(signature.parameter, [...path, "indexSignatures", index, "parameter"]),
525
+ type: recur(signature.type, [...path, "indexSignatures", index, "type"])
526
+ }))
527
+ const propertyRuntimes = properties.map((property) => property.runtime).join(", ")
528
+ const propertyTypes = properties.map((property) => property.Type).join(", ")
529
+ if (indexSignatures.length === 0) {
530
+ return makeCode(
531
+ `Schema.Struct({ ${propertyRuntimes} })`,
532
+ `{ ${propertyTypes} }`
533
+ )
534
+ }
535
+ if (properties.length === 0 && indexSignatures.length === 1) {
536
+ const signature = indexSignatures[0]
537
+ return makeCode(
538
+ `Schema.Record(${signature.parameter.runtime}, ${signature.type.runtime})`,
539
+ `{ readonly [x: ${signature.parameter.Type}]: ${signature.type.Type} }`
540
+ )
541
+ }
542
+ const indexRuntimes = indexSignatures.map((signature) =>
543
+ `Schema.Record(${signature.parameter.runtime}, ${signature.type.runtime})`
544
+ ).join(", ")
545
+ const indexTypes = indexSignatures.map((signature) =>
546
+ `readonly [x: ${signature.parameter.Type}]: ${signature.type.Type}`
547
+ ).join(", ")
548
+ return makeCode(
549
+ `Schema.StructWithRest(Schema.Struct({ ${propertyRuntimes} }), [${indexRuntimes}])`,
550
+ `{ ${propertyTypes}${properties.length > 0 ? ", " : ""}${indexTypes} }`
551
+ )
552
+ }
553
+ case "Union": {
554
+ if (representation.types.length === 0) return makeCode("Schema.Never", "never")
555
+ if (representation.types.every(isSimpleLiveLiteral)) {
556
+ const literals = representation.types.map((literal) => format(literal.literal))
557
+ return literals.length === 1
558
+ ? makeCode(`Schema.Literal(${literals[0]})`, literals[0])
559
+ : makeCode(`Schema.Literals([${literals.join(", ")}])`, literals.join(" | "))
560
+ }
561
+ const types = representation.types.map((type, index) =>
562
+ recur(type, [...path, "types", index], includeTypeBrands)
563
+ )
564
+ const mode = representation.mode === "anyOf" ? "" : `, { mode: "oneOf" }`
565
+ return makeCode(
566
+ `Schema.Union([${types.map((type) => type.runtime).join(", ")}]${mode})`,
567
+ types.map((type) => type.Type).join(" | ")
568
+ )
569
+ }
570
+ }
571
+ }
572
+ }
@@ -5,7 +5,6 @@ import * as Predicate from "../../Predicate.ts"
5
5
  import type * as Schema from "../../Schema.ts"
6
6
  import * as SchemaAST from "../../SchemaAST.ts"
7
7
  import * as SchemaParser from "../../SchemaParser.ts"
8
- import { errorWithPath } from "../errors.ts"
9
8
  import * as InternalAnnotations from "./annotations.ts"
10
9
 
11
10
  /** @internal */
@@ -25,7 +24,7 @@ function recur(ast: SchemaAST.AST, path: ReadonlyArray<PropertyKey>): Equivalenc
25
24
  }
26
25
  switch (ast._tag) {
27
26
  case "Never":
28
- throw errorWithPath(`Unsupported AST ${ast._tag}`, path)
27
+ return Equivalence.strictEqual()
29
28
  case "Declaration":
30
29
  case "Null":
31
30
  case "Undefined":
@@ -133,18 +132,24 @@ function recur(ast: SchemaAST.AST, path: ReadonlyArray<PropertyKey>): Equivalenc
133
132
  return true
134
133
  })
135
134
  }
136
- case "Union":
135
+ case "Union": {
136
+ const types = SchemaAST.toType(ast).types
137
+ const compiled = new Map(
138
+ types.map((candidate, i) =>
139
+ [candidate, [SchemaParser._is(candidate), recur(ast.types[i], path)] as const] as const
140
+ )
141
+ )
137
142
  return Equivalence.make((a, b) => {
138
- const candidates = SchemaAST.getCandidates(a, ast.types)
139
- const types = candidates.map(SchemaParser._is)
143
+ const candidates = SchemaAST.getCandidates(a, types)
140
144
  for (let i = 0; i < candidates.length; i++) {
141
- const is = types[i]
145
+ const [is, equivalence] = compiled.get(candidates[i])!
142
146
  if (is(a) && is(b)) {
143
- return recur(candidates[i], path)(a, b)
147
+ return equivalence(a, b)
144
148
  }
145
149
  }
146
150
  return false
147
151
  })
152
+ }
148
153
  case "Suspend": {
149
154
  const get = SchemaAST.memoizeThunk(() => recur(ast.thunk(), path))
150
155
  return Equivalence.make((a, b) => get()(a, b))