ai-functions 0.3.0 → 0.4.0

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 (400) hide show
  1. package/.turbo/turbo-build.log +5 -0
  2. package/.turbo/turbo-test.log +105 -0
  3. package/README.md +190 -86
  4. package/TODO.md +138 -0
  5. package/dist/ai-promise.d.ts +219 -0
  6. package/dist/ai-promise.d.ts.map +1 -0
  7. package/dist/ai-promise.js +610 -0
  8. package/dist/ai-promise.js.map +1 -0
  9. package/dist/ai.d.ts +285 -0
  10. package/dist/ai.d.ts.map +1 -0
  11. package/dist/ai.js +842 -0
  12. package/dist/ai.js.map +1 -0
  13. package/dist/batch/anthropic.d.ts +23 -0
  14. package/dist/batch/anthropic.d.ts.map +1 -0
  15. package/dist/batch/anthropic.js +257 -0
  16. package/dist/batch/anthropic.js.map +1 -0
  17. package/dist/batch/bedrock.d.ts +64 -0
  18. package/dist/batch/bedrock.d.ts.map +1 -0
  19. package/dist/batch/bedrock.js +586 -0
  20. package/dist/batch/bedrock.js.map +1 -0
  21. package/dist/batch/cloudflare.d.ts +37 -0
  22. package/dist/batch/cloudflare.d.ts.map +1 -0
  23. package/dist/batch/cloudflare.js +289 -0
  24. package/dist/batch/cloudflare.js.map +1 -0
  25. package/dist/batch/google.d.ts +41 -0
  26. package/dist/batch/google.d.ts.map +1 -0
  27. package/dist/batch/google.js +360 -0
  28. package/dist/batch/google.js.map +1 -0
  29. package/dist/batch/index.d.ts +31 -0
  30. package/dist/batch/index.d.ts.map +1 -0
  31. package/dist/batch/index.js +31 -0
  32. package/dist/batch/index.js.map +1 -0
  33. package/dist/batch/memory.d.ts +44 -0
  34. package/dist/batch/memory.d.ts.map +1 -0
  35. package/dist/batch/memory.js +188 -0
  36. package/dist/batch/memory.js.map +1 -0
  37. package/dist/batch/openai.d.ts +37 -0
  38. package/dist/batch/openai.d.ts.map +1 -0
  39. package/dist/batch/openai.js +403 -0
  40. package/dist/batch/openai.js.map +1 -0
  41. package/dist/batch-map.d.ts +125 -0
  42. package/dist/batch-map.d.ts.map +1 -0
  43. package/dist/batch-map.js +406 -0
  44. package/dist/batch-map.js.map +1 -0
  45. package/dist/batch-queue.d.ts +273 -0
  46. package/dist/batch-queue.d.ts.map +1 -0
  47. package/dist/batch-queue.js +271 -0
  48. package/dist/batch-queue.js.map +1 -0
  49. package/dist/context.d.ts +133 -0
  50. package/dist/context.d.ts.map +1 -0
  51. package/dist/context.js +267 -0
  52. package/dist/context.js.map +1 -0
  53. package/dist/embeddings.d.ts +123 -0
  54. package/dist/embeddings.d.ts.map +1 -0
  55. package/dist/embeddings.js +170 -0
  56. package/dist/embeddings.js.map +1 -0
  57. package/dist/eval/index.d.ts +8 -0
  58. package/dist/eval/index.d.ts.map +1 -0
  59. package/dist/eval/index.js +8 -0
  60. package/dist/eval/index.js.map +1 -0
  61. package/dist/eval/models.d.ts +66 -0
  62. package/dist/eval/models.d.ts.map +1 -0
  63. package/dist/eval/models.js +120 -0
  64. package/dist/eval/models.js.map +1 -0
  65. package/dist/eval/runner.d.ts +64 -0
  66. package/dist/eval/runner.d.ts.map +1 -0
  67. package/dist/eval/runner.js +148 -0
  68. package/dist/eval/runner.js.map +1 -0
  69. package/dist/generate.d.ts +168 -0
  70. package/dist/generate.d.ts.map +1 -0
  71. package/dist/generate.js +174 -0
  72. package/dist/generate.js.map +1 -0
  73. package/dist/index.d.ts +29 -4
  74. package/dist/index.d.ts.map +1 -1
  75. package/dist/index.js +53 -52
  76. package/dist/index.js.map +1 -1
  77. package/dist/primitives.d.ts +292 -0
  78. package/dist/primitives.d.ts.map +1 -0
  79. package/dist/primitives.js +471 -0
  80. package/dist/primitives.js.map +1 -0
  81. package/dist/providers/cloudflare.d.ts +9 -0
  82. package/dist/providers/cloudflare.d.ts.map +1 -0
  83. package/dist/providers/cloudflare.js +9 -0
  84. package/dist/providers/cloudflare.js.map +1 -0
  85. package/dist/providers/index.d.ts +9 -0
  86. package/dist/providers/index.d.ts.map +1 -0
  87. package/dist/providers/index.js +9 -0
  88. package/dist/providers/index.js.map +1 -0
  89. package/dist/schema.d.ts +54 -0
  90. package/dist/schema.d.ts.map +1 -0
  91. package/dist/schema.js +109 -0
  92. package/dist/schema.js.map +1 -0
  93. package/dist/template.d.ts +73 -0
  94. package/dist/template.d.ts.map +1 -0
  95. package/dist/template.js +129 -0
  96. package/dist/template.js.map +1 -0
  97. package/dist/types.d.ts +474 -106
  98. package/dist/types.d.ts.map +1 -1
  99. package/dist/types.js +4 -8
  100. package/dist/types.js.map +1 -1
  101. package/evalite.config.ts +19 -0
  102. package/evals/README.md +212 -0
  103. package/evals/classification.eval.ts +108 -0
  104. package/evals/marketing.eval.ts +370 -0
  105. package/evals/math.eval.ts +94 -0
  106. package/evals/run-evals.ts +166 -0
  107. package/evals/structured-output.eval.ts +143 -0
  108. package/evals/writing.eval.ts +117 -0
  109. package/examples/batch-blog-posts.ts +160 -0
  110. package/package.json +57 -57
  111. package/src/ai-promise.ts +784 -0
  112. package/src/ai.ts +1183 -0
  113. package/src/batch/anthropic.ts +375 -0
  114. package/src/batch/bedrock.ts +801 -0
  115. package/src/batch/cloudflare.ts +421 -0
  116. package/src/batch/google.ts +491 -0
  117. package/src/batch/index.ts +31 -0
  118. package/src/batch/memory.ts +253 -0
  119. package/src/batch/openai.ts +557 -0
  120. package/src/batch-map.ts +534 -0
  121. package/src/batch-queue.ts +493 -0
  122. package/src/context.ts +332 -0
  123. package/src/embeddings.ts +244 -0
  124. package/src/eval/index.ts +8 -0
  125. package/src/eval/models.ts +158 -0
  126. package/src/eval/runner.ts +217 -0
  127. package/src/generate.ts +245 -0
  128. package/src/index.ts +154 -0
  129. package/src/primitives.ts +612 -0
  130. package/src/providers/cloudflare.ts +15 -0
  131. package/src/providers/index.ts +14 -0
  132. package/src/schema.ts +147 -0
  133. package/src/template.ts +209 -0
  134. package/src/types.ts +540 -0
  135. package/test/README.md +105 -0
  136. package/test/ai-proxy.test.ts +192 -0
  137. package/test/async-iterators.test.ts +327 -0
  138. package/test/batch-background.test.ts +482 -0
  139. package/test/batch-blog-posts.test.ts +387 -0
  140. package/test/blog-generation.test.ts +510 -0
  141. package/test/browse-read.test.ts +611 -0
  142. package/test/core-functions.test.ts +694 -0
  143. package/test/decide.test.ts +393 -0
  144. package/test/define.test.ts +274 -0
  145. package/test/e2e-bedrock-manual.ts +163 -0
  146. package/test/e2e-bedrock.test.ts +191 -0
  147. package/test/e2e-flex-gateway.ts +157 -0
  148. package/test/e2e-flex-manual.ts +183 -0
  149. package/test/e2e-flex.test.ts +209 -0
  150. package/test/e2e-google-manual.ts +178 -0
  151. package/test/e2e-google.test.ts +216 -0
  152. package/test/embeddings.test.ts +284 -0
  153. package/test/evals/define-function.eval.test.ts +379 -0
  154. package/test/evals/primitives.eval.test.ts +384 -0
  155. package/test/function-types.test.ts +492 -0
  156. package/test/generate-core.test.ts +319 -0
  157. package/test/generate.test.ts +163 -0
  158. package/test/implicit-batch.test.ts +422 -0
  159. package/test/schema.test.ts +109 -0
  160. package/test/tagged-templates.test.ts +302 -0
  161. package/tsconfig.json +10 -0
  162. package/vitest.config.ts +42 -0
  163. package/LICENSE +0 -21
  164. package/bin/cli.js +0 -5
  165. package/dist/cli/index.d.ts +0 -10
  166. package/dist/cli/index.d.ts.map +0 -1
  167. package/dist/cli/index.js +0 -38
  168. package/dist/cli/index.js.map +0 -1
  169. package/dist/cli/index.test.d.ts +0 -2
  170. package/dist/cli/index.test.d.ts.map +0 -1
  171. package/dist/cli/index.test.js +0 -35
  172. package/dist/cli/index.test.js.map +0 -1
  173. package/dist/constants/models.d.ts +0 -10
  174. package/dist/constants/models.d.ts.map +0 -1
  175. package/dist/constants/models.js +0 -12
  176. package/dist/constants/models.js.map +0 -1
  177. package/dist/converters/index.d.ts +0 -3
  178. package/dist/converters/index.d.ts.map +0 -1
  179. package/dist/converters/index.js +0 -3
  180. package/dist/converters/index.js.map +0 -1
  181. package/dist/converters/model.d.ts +0 -4
  182. package/dist/converters/model.d.ts.map +0 -1
  183. package/dist/converters/model.js +0 -19
  184. package/dist/converters/model.js.map +0 -1
  185. package/dist/converters/schema.d.ts +0 -4
  186. package/dist/converters/schema.d.ts.map +0 -1
  187. package/dist/converters/schema.js +0 -25
  188. package/dist/converters/schema.js.map +0 -1
  189. package/dist/core/responses.d.ts +0 -5
  190. package/dist/core/responses.d.ts.map +0 -1
  191. package/dist/core/responses.js +0 -16
  192. package/dist/core/responses.js.map +0 -1
  193. package/dist/core/responses.test.d.ts +0 -2
  194. package/dist/core/responses.test.d.ts.map +0 -1
  195. package/dist/core/responses.test.js +0 -31
  196. package/dist/core/responses.test.js.map +0 -1
  197. package/dist/errors.d.ts +0 -6
  198. package/dist/errors.d.ts.map +0 -1
  199. package/dist/errors.js +0 -9
  200. package/dist/errors.js.map +0 -1
  201. package/dist/examples/streaming.test.d.ts +0 -2
  202. package/dist/examples/streaming.test.d.ts.map +0 -1
  203. package/dist/examples/streaming.test.js +0 -176
  204. package/dist/examples/streaming.test.js.map +0 -1
  205. package/dist/factory/__tests__/index.test.d.ts +0 -2
  206. package/dist/factory/__tests__/index.test.d.ts.map +0 -1
  207. package/dist/factory/__tests__/index.test.js +0 -430
  208. package/dist/factory/__tests__/index.test.js.map +0 -1
  209. package/dist/factory/__tests__/list.test.d.ts +0 -2
  210. package/dist/factory/__tests__/list.test.d.ts.map +0 -1
  211. package/dist/factory/__tests__/list.test.js +0 -92
  212. package/dist/factory/__tests__/list.test.js.map +0 -1
  213. package/dist/factory/index.d.ts +0 -20
  214. package/dist/factory/index.d.ts.map +0 -1
  215. package/dist/factory/index.js +0 -287
  216. package/dist/factory/index.js.map +0 -1
  217. package/dist/factory/index.test.d.ts +0 -2
  218. package/dist/factory/index.test.d.ts.map +0 -1
  219. package/dist/factory/index.test.js +0 -287
  220. package/dist/factory/index.test.js.map +0 -1
  221. package/dist/factory/list.d.ts +0 -3
  222. package/dist/factory/list.d.ts.map +0 -1
  223. package/dist/factory/list.js +0 -221
  224. package/dist/factory/list.js.map +0 -1
  225. package/dist/factory/list.test.d.ts +0 -2
  226. package/dist/factory/list.test.d.ts.map +0 -1
  227. package/dist/factory/list.test.js +0 -84
  228. package/dist/factory/list.test.js.map +0 -1
  229. package/dist/generate/index.d.ts +0 -5
  230. package/dist/generate/index.d.ts.map +0 -1
  231. package/dist/generate/index.js +0 -17
  232. package/dist/generate/index.js.map +0 -1
  233. package/dist/index.test.d.ts +0 -2
  234. package/dist/index.test.d.ts.map +0 -1
  235. package/dist/index.test.js +0 -59
  236. package/dist/index.test.js.map +0 -1
  237. package/dist/list/await.d.ts +0 -3
  238. package/dist/list/await.d.ts.map +0 -1
  239. package/dist/list/await.js +0 -28
  240. package/dist/list/await.js.map +0 -1
  241. package/dist/list/constants.d.ts +0 -4
  242. package/dist/list/constants.d.ts.map +0 -1
  243. package/dist/list/constants.js +0 -5
  244. package/dist/list/constants.js.map +0 -1
  245. package/dist/list/create-function.d.ts +0 -3
  246. package/dist/list/create-function.d.ts.map +0 -1
  247. package/dist/list/create-function.js +0 -11
  248. package/dist/list/create-function.js.map +0 -1
  249. package/dist/list/index.d.ts +0 -4
  250. package/dist/list/index.d.ts.map +0 -1
  251. package/dist/list/index.js +0 -5
  252. package/dist/list/index.js.map +0 -1
  253. package/dist/list/prompt.d.ts +0 -3
  254. package/dist/list/prompt.d.ts.map +0 -1
  255. package/dist/list/prompt.js +0 -6
  256. package/dist/list/prompt.js.map +0 -1
  257. package/dist/list/schemas.d.ts +0 -4
  258. package/dist/list/schemas.d.ts.map +0 -1
  259. package/dist/list/schemas.js +0 -8
  260. package/dist/list/schemas.js.map +0 -1
  261. package/dist/list/stream.d.ts +0 -3
  262. package/dist/list/stream.d.ts.map +0 -1
  263. package/dist/list/stream.js +0 -33
  264. package/dist/list/stream.js.map +0 -1
  265. package/dist/list/types.d.ts +0 -11
  266. package/dist/list/types.d.ts.map +0 -1
  267. package/dist/list/types.js +0 -2
  268. package/dist/list/types.js.map +0 -1
  269. package/dist/list/validation.d.ts +0 -3
  270. package/dist/list/validation.d.ts.map +0 -1
  271. package/dist/list/validation.js +0 -12
  272. package/dist/list/validation.js.map +0 -1
  273. package/dist/providers/config.d.ts +0 -4
  274. package/dist/providers/config.d.ts.map +0 -1
  275. package/dist/providers/config.js +0 -21
  276. package/dist/providers/config.js.map +0 -1
  277. package/dist/providers/config.test.d.ts +0 -2
  278. package/dist/providers/config.test.d.ts.map +0 -1
  279. package/dist/providers/config.test.js +0 -37
  280. package/dist/providers/config.test.js.map +0 -1
  281. package/dist/proxy/constants.d.ts +0 -4
  282. package/dist/proxy/constants.d.ts.map +0 -1
  283. package/dist/proxy/constants.js +0 -5
  284. package/dist/proxy/constants.js.map +0 -1
  285. package/dist/proxy/create-function.d.ts +0 -4
  286. package/dist/proxy/create-function.d.ts.map +0 -1
  287. package/dist/proxy/create-function.js +0 -24
  288. package/dist/proxy/create-function.js.map +0 -1
  289. package/dist/proxy/create-proxy.d.ts +0 -2
  290. package/dist/proxy/create-proxy.d.ts.map +0 -1
  291. package/dist/proxy/create-proxy.js +0 -11
  292. package/dist/proxy/create-proxy.js.map +0 -1
  293. package/dist/proxy/function-generator.d.ts +0 -9
  294. package/dist/proxy/function-generator.d.ts.map +0 -1
  295. package/dist/proxy/function-generator.js +0 -29
  296. package/dist/proxy/function-generator.js.map +0 -1
  297. package/dist/proxy/index.d.ts +0 -4
  298. package/dist/proxy/index.d.ts.map +0 -1
  299. package/dist/proxy/index.js +0 -4
  300. package/dist/proxy/index.js.map +0 -1
  301. package/dist/proxy/prompt.d.ts +0 -2
  302. package/dist/proxy/prompt.d.ts.map +0 -1
  303. package/dist/proxy/prompt.js +0 -6
  304. package/dist/proxy/prompt.js.map +0 -1
  305. package/dist/proxy/types.d.ts +0 -7
  306. package/dist/proxy/types.d.ts.map +0 -1
  307. package/dist/proxy/types.js +0 -2
  308. package/dist/proxy/types.js.map +0 -1
  309. package/dist/queue/manager.d.ts +0 -5
  310. package/dist/queue/manager.d.ts.map +0 -1
  311. package/dist/queue/manager.js +0 -37
  312. package/dist/queue/manager.js.map +0 -1
  313. package/dist/queue/manager.test.d.ts +0 -2
  314. package/dist/queue/manager.test.d.ts.map +0 -1
  315. package/dist/queue/manager.test.js +0 -52
  316. package/dist/queue/manager.test.js.map +0 -1
  317. package/dist/schema-converter.d.ts +0 -4
  318. package/dist/schema-converter.d.ts.map +0 -1
  319. package/dist/schema-converter.js +0 -30
  320. package/dist/schema-converter.js.map +0 -1
  321. package/dist/stream/index.d.ts +0 -7
  322. package/dist/stream/index.d.ts.map +0 -1
  323. package/dist/stream/index.js +0 -23
  324. package/dist/stream/index.js.map +0 -1
  325. package/dist/streaming/utils.d.ts +0 -4
  326. package/dist/streaming/utils.d.ts.map +0 -1
  327. package/dist/streaming/utils.js +0 -131
  328. package/dist/streaming/utils.js.map +0 -1
  329. package/dist/streaming/utils.test.d.ts +0 -2
  330. package/dist/streaming/utils.test.d.ts.map +0 -1
  331. package/dist/streaming/utils.test.js +0 -84
  332. package/dist/streaming/utils.test.js.map +0 -1
  333. package/dist/templates/result.d.ts +0 -7
  334. package/dist/templates/result.d.ts.map +0 -1
  335. package/dist/templates/result.js +0 -40
  336. package/dist/templates/result.js.map +0 -1
  337. package/dist/templates/result.test.d.ts +0 -2
  338. package/dist/templates/result.test.d.ts.map +0 -1
  339. package/dist/templates/result.test.js +0 -75
  340. package/dist/templates/result.test.js.map +0 -1
  341. package/dist/test/setup.d.ts +0 -2
  342. package/dist/test/setup.d.ts.map +0 -1
  343. package/dist/test/setup.js +0 -21
  344. package/dist/test/setup.js.map +0 -1
  345. package/dist/test-types.d.ts +0 -13
  346. package/dist/test-types.d.ts.map +0 -1
  347. package/dist/test-types.js +0 -55
  348. package/dist/test-types.js.map +0 -1
  349. package/dist/types/index.d.ts +0 -4
  350. package/dist/types/index.d.ts.map +0 -1
  351. package/dist/types/index.js +0 -4
  352. package/dist/types/index.js.map +0 -1
  353. package/dist/types/list.d.ts +0 -10
  354. package/dist/types/list.d.ts.map +0 -1
  355. package/dist/types/list.js +0 -2
  356. package/dist/types/list.js.map +0 -1
  357. package/dist/types/model.d.ts +0 -7
  358. package/dist/types/model.d.ts.map +0 -1
  359. package/dist/types/model.js +0 -2
  360. package/dist/types/model.js.map +0 -1
  361. package/dist/types/options.d.ts +0 -25
  362. package/dist/types/options.d.ts.map +0 -1
  363. package/dist/types/options.js +0 -2
  364. package/dist/types/options.js.map +0 -1
  365. package/dist/types/schema.d.ts +0 -5
  366. package/dist/types/schema.d.ts.map +0 -1
  367. package/dist/types/schema.js +0 -2
  368. package/dist/types/schema.js.map +0 -1
  369. package/dist/utils/__tests__/request-handler.test.d.ts +0 -2
  370. package/dist/utils/__tests__/request-handler.test.d.ts.map +0 -1
  371. package/dist/utils/__tests__/request-handler.test.js +0 -134
  372. package/dist/utils/__tests__/request-handler.test.js.map +0 -1
  373. package/dist/utils/__tests__/schema.test.d.ts +0 -2
  374. package/dist/utils/__tests__/schema.test.d.ts.map +0 -1
  375. package/dist/utils/__tests__/schema.test.js +0 -49
  376. package/dist/utils/__tests__/schema.test.js.map +0 -1
  377. package/dist/utils/__tests__/stream-progress.test.d.ts +0 -2
  378. package/dist/utils/__tests__/stream-progress.test.d.ts.map +0 -1
  379. package/dist/utils/__tests__/stream-progress.test.js +0 -85
  380. package/dist/utils/__tests__/stream-progress.test.js.map +0 -1
  381. package/dist/utils/index.d.ts +0 -2
  382. package/dist/utils/index.d.ts.map +0 -1
  383. package/dist/utils/index.js +0 -2
  384. package/dist/utils/index.js.map +0 -1
  385. package/dist/utils/request-handler.d.ts +0 -17
  386. package/dist/utils/request-handler.d.ts.map +0 -1
  387. package/dist/utils/request-handler.js +0 -105
  388. package/dist/utils/request-handler.js.map +0 -1
  389. package/dist/utils/schema.d.ts +0 -11
  390. package/dist/utils/schema.d.ts.map +0 -1
  391. package/dist/utils/schema.js +0 -51
  392. package/dist/utils/schema.js.map +0 -1
  393. package/dist/utils/stream-progress.d.ts +0 -17
  394. package/dist/utils/stream-progress.d.ts.map +0 -1
  395. package/dist/utils/stream-progress.js +0 -86
  396. package/dist/utils/stream-progress.js.map +0 -1
  397. package/dist/utils/validation.d.ts +0 -3
  398. package/dist/utils/validation.d.ts.map +0 -1
  399. package/dist/utils/validation.js +0 -30
  400. package/dist/utils/validation.js.map +0 -1
@@ -0,0 +1,612 @@
1
+ /**
2
+ * AI Function Primitives with Promise Pipelining
3
+ *
4
+ * All functions return AIPromise for:
5
+ * - Dynamic schema inference from destructuring
6
+ * - Promise pipelining without await
7
+ * - Magical .map() for batch processing
8
+ * - Dependency graph resolution
9
+ *
10
+ * @example
11
+ * ```ts
12
+ * // No await needed until the end!
13
+ * const { summary, keyPoints, conclusion } = ai`write about ${topic}`
14
+ * const isValid = is`${conclusion} is solid given ${keyPoints}`
15
+ * const improved = ai`improve ${conclusion} using ${keyPoints}`
16
+ *
17
+ * // Batch processing with map
18
+ * const ideas = list`startup ideas`
19
+ * const evaluated = await ideas.map(idea => ({
20
+ * idea,
21
+ * viable: is`${idea} is viable`,
22
+ * market: ai`market size for ${idea}`,
23
+ * }))
24
+ *
25
+ * // Only await at the end
26
+ * if (await isValid) {
27
+ * console.log(await improved)
28
+ * }
29
+ * ```
30
+ *
31
+ * @packageDocumentation
32
+ */
33
+
34
+ import {
35
+ AIPromise,
36
+ createAITemplateFunction,
37
+ parseTemplateWithDependencies,
38
+ isAIPromise,
39
+ } from './ai-promise.js'
40
+ import { generateObject, generateText } from './generate.js'
41
+ import type { SimpleSchema } from './schema.js'
42
+ import type { HumanChannel } from './types.js'
43
+
44
+ // ============================================================================
45
+ // Types
46
+ // ============================================================================
47
+
48
+ export type GenerateType =
49
+ | 'text'
50
+ | 'json'
51
+ | 'code'
52
+ | 'list'
53
+ | 'lists'
54
+ | 'markdown'
55
+ | 'yaml'
56
+ | 'diagram'
57
+ | 'slides'
58
+ | 'boolean'
59
+ | 'summary'
60
+ | 'extract'
61
+
62
+ export interface GenerateOptions {
63
+ /** Model to use */
64
+ model?: string
65
+ /** System prompt */
66
+ system?: string
67
+ /** Temperature (0-2) */
68
+ temperature?: number
69
+ /** Max tokens */
70
+ maxTokens?: number
71
+ /** Schema for JSON output */
72
+ schema?: SimpleSchema
73
+ /** Language for code generation */
74
+ language?: string
75
+ /** Format for diagrams */
76
+ format?: 'mermaid' | 'svg' | 'ascii'
77
+ /** Number of slides for presentations */
78
+ slides?: number
79
+ }
80
+
81
+ // ============================================================================
82
+ // Core generate() primitive
83
+ // ============================================================================
84
+
85
+ /**
86
+ * Core generate primitive - all other functions use this under the hood
87
+ */
88
+ export async function generate(
89
+ type: GenerateType,
90
+ prompt: string,
91
+ options?: GenerateOptions
92
+ ): Promise<unknown> {
93
+ const { model = 'sonnet', schema, language, format, slides: slideCount, ...rest } = options || {}
94
+
95
+ switch (type) {
96
+ case 'text':
97
+ case 'markdown':
98
+ return generateTextContent(prompt, model, rest)
99
+ case 'json':
100
+ return generateJsonContent(prompt, model, schema, rest)
101
+ case 'code':
102
+ return generateCodeContent(prompt, model, language || 'typescript', rest)
103
+ case 'list':
104
+ return generateListContent(prompt, model, rest)
105
+ case 'lists':
106
+ return generateListsContent(prompt, model, rest)
107
+ case 'boolean':
108
+ return generateBooleanContent(prompt, model, rest)
109
+ case 'summary':
110
+ return generateSummaryContent(prompt, model, rest)
111
+ case 'extract':
112
+ return generateExtractContent(prompt, model, schema, rest)
113
+ case 'yaml':
114
+ return generateYamlContent(prompt, model, rest)
115
+ case 'diagram':
116
+ return generateDiagramContent(prompt, model, format || 'mermaid', rest)
117
+ case 'slides':
118
+ return generateSlidesContent(prompt, model, slideCount || 10, rest)
119
+ default:
120
+ throw new Error(`Unknown generate type: ${type}`)
121
+ }
122
+ }
123
+
124
+ // Helper functions
125
+ async function generateTextContent(prompt: string, model: string, options: GenerateOptions): Promise<string> {
126
+ const result = await generateText({ model, prompt, system: options.system, temperature: options.temperature, maxTokens: options.maxTokens })
127
+ return result.text
128
+ }
129
+
130
+ async function generateJsonContent(prompt: string, model: string, schema: SimpleSchema | undefined, options: GenerateOptions): Promise<unknown> {
131
+ const effectiveSchema = schema || { result: 'The generated result' }
132
+ const result = await generateObject({ model, schema: effectiveSchema, prompt, system: options.system, temperature: options.temperature, maxTokens: options.maxTokens })
133
+ return result.object
134
+ }
135
+
136
+ async function generateCodeContent(prompt: string, model: string, language: string, options: GenerateOptions): Promise<string> {
137
+ const result = await generateObject({
138
+ model,
139
+ schema: { code: `The ${language} implementation code` },
140
+ prompt: `Generate ${language} code for: ${prompt}`,
141
+ system: `You are an expert ${language} developer. Generate clean, well-documented code.`,
142
+ temperature: options.temperature,
143
+ maxTokens: options.maxTokens,
144
+ })
145
+ return (result.object as { code: string }).code
146
+ }
147
+
148
+ async function generateListContent(prompt: string, model: string, options: GenerateOptions): Promise<string[]> {
149
+ const result = await generateObject({
150
+ model,
151
+ schema: { items: ['List items'] },
152
+ prompt,
153
+ system: options.system || 'Generate a list of items based on the prompt.',
154
+ temperature: options.temperature,
155
+ maxTokens: options.maxTokens,
156
+ })
157
+ return (result.object as { items: string[] }).items
158
+ }
159
+
160
+ async function generateListsContent(prompt: string, model: string, options: GenerateOptions): Promise<Record<string, string[]>> {
161
+ const result = await generateObject({
162
+ model,
163
+ schema: {
164
+ categories: ['Category names as strings'],
165
+ data: 'JSON string containing the categorized lists',
166
+ },
167
+ prompt: `Generate categorized lists for: ${prompt}\n\nFirst identify appropriate category names, then provide the lists as a JSON object.`,
168
+ system: options.system || 'Generate multiple categorized lists. Determine appropriate categories based on the prompt.',
169
+ temperature: options.temperature,
170
+ maxTokens: options.maxTokens,
171
+ })
172
+ const obj = result.object as { categories: string[]; data: string }
173
+ try {
174
+ return JSON.parse(obj.data) as Record<string, string[]>
175
+ } catch {
176
+ const lists: Record<string, string[]> = {}
177
+ for (const cat of obj.categories || []) {
178
+ lists[cat] = []
179
+ }
180
+ return lists
181
+ }
182
+ }
183
+
184
+ async function generateBooleanContent(prompt: string, model: string, options: GenerateOptions): Promise<boolean> {
185
+ const result = await generateObject({
186
+ model,
187
+ schema: { answer: 'true | false' },
188
+ prompt,
189
+ system: options.system || 'Answer the question with true or false.',
190
+ temperature: options.temperature ?? 0,
191
+ maxTokens: options.maxTokens,
192
+ })
193
+ return (result.object as { answer: string }).answer === 'true'
194
+ }
195
+
196
+ async function generateSummaryContent(prompt: string, model: string, options: GenerateOptions): Promise<string> {
197
+ const result = await generateObject({
198
+ model,
199
+ schema: { summary: 'A concise summary of the content' },
200
+ prompt: `Summarize the following:\n\n${prompt}`,
201
+ system: options.system || 'Create a clear, concise summary.',
202
+ temperature: options.temperature,
203
+ maxTokens: options.maxTokens,
204
+ })
205
+ return (result.object as { summary: string }).summary
206
+ }
207
+
208
+ async function generateExtractContent(prompt: string, model: string, schema: SimpleSchema | undefined, options: GenerateOptions): Promise<unknown[]> {
209
+ const effectiveSchema = schema || { items: ['Extracted items'] }
210
+ const result = await generateObject({
211
+ model,
212
+ schema: effectiveSchema,
213
+ prompt: `Extract from the following:\n\n${prompt}`,
214
+ system: options.system || 'Extract the requested information.',
215
+ temperature: options.temperature,
216
+ maxTokens: options.maxTokens,
217
+ })
218
+ const obj = result.object as Record<string, unknown>
219
+ if ('items' in obj && Array.isArray(obj.items)) {
220
+ return obj.items
221
+ }
222
+ return Object.values(obj).flat() as unknown[]
223
+ }
224
+
225
+ async function generateYamlContent(prompt: string, model: string, options: GenerateOptions): Promise<string> {
226
+ const result = await generateObject({
227
+ model,
228
+ schema: { yaml: 'The YAML content' },
229
+ prompt: `Generate YAML for: ${prompt}`,
230
+ system: options.system || 'Generate valid YAML content.',
231
+ temperature: options.temperature,
232
+ maxTokens: options.maxTokens,
233
+ })
234
+ return (result.object as { yaml: string }).yaml
235
+ }
236
+
237
+ async function generateDiagramContent(prompt: string, model: string, format: string, options: GenerateOptions): Promise<string> {
238
+ const result = await generateObject({
239
+ model,
240
+ schema: { diagram: `The ${format} diagram code` },
241
+ prompt: `Generate a ${format} diagram for: ${prompt}`,
242
+ system: options.system || `Generate ${format} diagram syntax.`,
243
+ temperature: options.temperature,
244
+ maxTokens: options.maxTokens,
245
+ })
246
+ return (result.object as { diagram: string }).diagram
247
+ }
248
+
249
+ async function generateSlidesContent(prompt: string, model: string, slideCount: number, options: GenerateOptions): Promise<string> {
250
+ const result = await generateObject({
251
+ model,
252
+ schema: { slides: `Slidev/Marp markdown with ${slideCount} slides` },
253
+ prompt: `Generate a ${slideCount}-slide presentation about: ${prompt}`,
254
+ system: options.system || 'Generate markdown slides in Slidev/Marp format.',
255
+ temperature: options.temperature,
256
+ maxTokens: options.maxTokens,
257
+ })
258
+ return (result.object as { slides: string }).slides
259
+ }
260
+
261
+ // ============================================================================
262
+ // AIPromise-based Functions
263
+ // ============================================================================
264
+
265
+ /**
266
+ * General-purpose AI function with dynamic schema inference
267
+ *
268
+ * @example
269
+ * ```ts
270
+ * // Simple text generation
271
+ * const text = await ai`write a poem about ${topic}`
272
+ *
273
+ * // Dynamic schema from destructuring - no await needed!
274
+ * const { summary, keyPoints, conclusion } = ai`write about ${topic}`
275
+ * console.log(await summary)
276
+ *
277
+ * // Chain with other functions
278
+ * const isValid = is`${conclusion} is solid`
279
+ * const improved = ai`improve ${conclusion}`
280
+ * ```
281
+ */
282
+ export const ai = createAITemplateFunction<unknown>('object')
283
+
284
+ /**
285
+ * Generate text content
286
+ *
287
+ * @example
288
+ * ```ts
289
+ * const post = await write`blog post about ${topic}`
290
+ * ```
291
+ */
292
+ export const write = createAITemplateFunction<string>('text')
293
+
294
+ /**
295
+ * Generate code
296
+ *
297
+ * @example
298
+ * ```ts
299
+ * const code = await code`email validation function`
300
+ * ```
301
+ */
302
+ export const code = createAITemplateFunction<string>('text', {
303
+ system: 'You are an expert programmer. Generate clean, well-documented code.',
304
+ })
305
+
306
+ /**
307
+ * Generate a list of items with .map() support
308
+ *
309
+ * @example
310
+ * ```ts
311
+ * // Simple list
312
+ * const ideas = await list`startup ideas`
313
+ *
314
+ * // With map - batch processes in ONE call!
315
+ * const evaluated = await list`startup ideas`.map(idea => ({
316
+ * idea,
317
+ * viable: is`${idea} is viable`,
318
+ * market: ai`market size for ${idea}`,
319
+ * }))
320
+ *
321
+ * // Async iteration
322
+ * for await (const idea of list`startup ideas`) {
323
+ * console.log(idea)
324
+ * }
325
+ * ```
326
+ */
327
+ export const list = createAITemplateFunction<string[]>('list')
328
+
329
+ /**
330
+ * Generate multiple named lists with dynamic schema
331
+ *
332
+ * @example
333
+ * ```ts
334
+ * // Destructuring infers the schema!
335
+ * const { pros, cons } = await lists`pros and cons of ${topic}`
336
+ *
337
+ * // No await - pipeline with other functions
338
+ * const { benefits, risks, costs } = lists`analysis of ${project}`
339
+ * const summary = ai`summarize: benefits=${benefits}, risks=${risks}`
340
+ * console.log(await summary)
341
+ * ```
342
+ */
343
+ export const lists = createAITemplateFunction<Record<string, string[]>>('lists')
344
+
345
+ /**
346
+ * Extract structured data with dynamic schema
347
+ *
348
+ * @example
349
+ * ```ts
350
+ * // Dynamic schema from destructuring
351
+ * const { name, email, phone } = await extract`contact info from ${document}`
352
+ *
353
+ * // As array
354
+ * const emails = await extract`email addresses from ${text}`
355
+ * ```
356
+ */
357
+ export const extract = createAITemplateFunction<unknown[]>('extract')
358
+
359
+ /**
360
+ * Summarize text
361
+ *
362
+ * @example
363
+ * ```ts
364
+ * const summary = await summarize`${longArticle}`
365
+ * ```
366
+ */
367
+ export const summarize = createAITemplateFunction<string>('text', {
368
+ system: 'Create a clear, concise summary.',
369
+ })
370
+
371
+ /**
372
+ * Check if something is true/false
373
+ *
374
+ * @example
375
+ * ```ts
376
+ * // Simple check
377
+ * const isColor = await is`${topic} a color`
378
+ *
379
+ * // Pipeline - no await needed!
380
+ * const { conclusion } = ai`write about ${topic}`
381
+ * const isValid = is`${conclusion} is well-argued`
382
+ * if (await isValid) { ... }
383
+ * ```
384
+ */
385
+ export const is = createAITemplateFunction<boolean>('boolean')
386
+
387
+ /**
388
+ * Generate a diagram
389
+ *
390
+ * @example
391
+ * ```ts
392
+ * const diagram = await diagram`user authentication flow`
393
+ * ```
394
+ */
395
+ export const diagram = createAITemplateFunction<string>('text', {
396
+ system: 'Generate a Mermaid diagram.',
397
+ })
398
+
399
+ /**
400
+ * Generate presentation slides
401
+ *
402
+ * @example
403
+ * ```ts
404
+ * const slides = await slides`quarterly review`
405
+ * ```
406
+ */
407
+ export const slides = createAITemplateFunction<string>('text', {
408
+ system: 'Generate markdown slides in Slidev/Marp format.',
409
+ })
410
+
411
+ /**
412
+ * Generate an image
413
+ */
414
+ export const image = createAITemplateFunction<Buffer>('text')
415
+
416
+ /**
417
+ * Generate a video
418
+ */
419
+ export const video = createAITemplateFunction<Buffer>('text')
420
+
421
+ // ============================================================================
422
+ // Agentic Functions
423
+ // ============================================================================
424
+
425
+ /**
426
+ * Execute a task
427
+ *
428
+ * @example
429
+ * ```ts
430
+ * const { summary, actions } = await do`send welcome email to ${user}`
431
+ * ```
432
+ */
433
+ function doImpl(
434
+ promptOrStrings: string | TemplateStringsArray,
435
+ ...args: unknown[]
436
+ ): AIPromise<{ summary: string; actions: string[] }> {
437
+ let prompt: string
438
+ let dependencies: { promise: AIPromise<unknown>; path: string[] }[] = []
439
+
440
+ if (Array.isArray(promptOrStrings) && 'raw' in promptOrStrings) {
441
+ const parsed = parseTemplateWithDependencies(promptOrStrings, ...args)
442
+ prompt = parsed.prompt
443
+ dependencies = parsed.dependencies
444
+ } else {
445
+ prompt = promptOrStrings as string
446
+ }
447
+
448
+ const promise = new AIPromise<{ summary: string; actions: string[] }>(
449
+ prompt,
450
+ {
451
+ type: 'object',
452
+ baseSchema: {
453
+ summary: 'Summary of what was done',
454
+ actions: ['List of actions taken'],
455
+ },
456
+ system: 'You are a task executor. Describe what actions you would take.',
457
+ }
458
+ )
459
+
460
+ for (const dep of dependencies) {
461
+ promise.addDependency(dep.promise, dep.path)
462
+ }
463
+
464
+ return promise
465
+ }
466
+
467
+ export { doImpl as do }
468
+
469
+ /**
470
+ * Conduct research on a topic
471
+ *
472
+ * @example
473
+ * ```ts
474
+ * const { summary, findings, sources } = await research`${competitor} vs our product`
475
+ * ```
476
+ */
477
+ export const research = createAITemplateFunction<{ summary: string; findings: string[]; sources: string[] }>('object', {
478
+ system: 'You are a research analyst. Provide thorough research.',
479
+ })
480
+
481
+ // ============================================================================
482
+ // Web Functions
483
+ // ============================================================================
484
+
485
+ /**
486
+ * Read a URL and convert to markdown
487
+ */
488
+ export const read = createAITemplateFunction<string>('text')
489
+
490
+ /**
491
+ * Browse a URL with browser automation
492
+ */
493
+ export async function browse(
494
+ urlOrStrings: string | TemplateStringsArray,
495
+ ...args: unknown[]
496
+ ): Promise<{
497
+ do: (action: string) => Promise<void>
498
+ extract: (query: string) => Promise<unknown>
499
+ screenshot: () => Promise<Buffer>
500
+ close: () => Promise<void>
501
+ }> {
502
+ // Placeholder - actual implementation would use Stagehand or Playwright
503
+ return {
504
+ do: async () => {},
505
+ extract: async () => ({}),
506
+ screenshot: async () => Buffer.from('screenshot'),
507
+ close: async () => {},
508
+ }
509
+ }
510
+
511
+ // ============================================================================
512
+ // Decision Functions
513
+ // ============================================================================
514
+
515
+ /**
516
+ * LLM as judge - compare options and pick the best
517
+ *
518
+ * @example
519
+ * ```ts
520
+ * const winner = await decide`higher click-through rate`(headlineA, headlineB)
521
+ * ```
522
+ */
523
+ export function decide(
524
+ criteriaOrStrings: string | TemplateStringsArray,
525
+ ...templateArgs: unknown[]
526
+ ): <T>(...options: T[]) => AIPromise<T> {
527
+ let criteria: string
528
+
529
+ if (Array.isArray(criteriaOrStrings) && 'raw' in criteriaOrStrings) {
530
+ criteria = criteriaOrStrings.reduce(
531
+ (acc, str, i) => acc + str + (templateArgs[i] ?? ''),
532
+ ''
533
+ )
534
+ } else {
535
+ criteria = criteriaOrStrings as string
536
+ }
537
+
538
+ return <T>(...options: T[]): AIPromise<T> => {
539
+ const optionDescriptions = options
540
+ .map((opt, i) => `Option ${i + 1}: ${JSON.stringify(opt)}`)
541
+ .join('\n')
542
+
543
+ const promise = new AIPromise<T>(
544
+ `Given these options:\n${optionDescriptions}\n\nChoose the best option based on: ${criteria}`,
545
+ {
546
+ type: 'object',
547
+ baseSchema: {
548
+ chosenIndex: 'The index (1-based) of the best option as a number',
549
+ reasoning: 'Brief explanation of why this option is best',
550
+ },
551
+ }
552
+ )
553
+
554
+ // Override resolve to return the actual option
555
+ const originalResolve = promise.resolve.bind(promise)
556
+ ;(promise as any).resolve = async () => {
557
+ const result = await originalResolve() as { chosenIndex: string | number }
558
+ const index = typeof result.chosenIndex === 'string'
559
+ ? parseInt(result.chosenIndex, 10)
560
+ : result.chosenIndex
561
+ return options[index - 1] as T
562
+ }
563
+
564
+ return promise
565
+ }
566
+ }
567
+
568
+ // ============================================================================
569
+ // Human-in-the-Loop Functions
570
+ // ============================================================================
571
+
572
+ export interface HumanOptions extends GenerateOptions {
573
+ channel?: HumanChannel
574
+ assignee?: string
575
+ timeout?: number
576
+ webhook?: string
577
+ }
578
+
579
+ export interface HumanResult<T = unknown> {
580
+ pending: boolean
581
+ requestId: string
582
+ response?: T
583
+ respondedBy?: string
584
+ respondedAt?: Date
585
+ artifacts?: {
586
+ slackBlocks?: unknown[]
587
+ emailHtml?: string
588
+ webComponent?: string
589
+ smsText?: string
590
+ }
591
+ }
592
+
593
+ /**
594
+ * Ask a human for input
595
+ */
596
+ export const ask = createAITemplateFunction<HumanResult<string>>('object', {
597
+ system: 'Generate content for human interaction.',
598
+ })
599
+
600
+ /**
601
+ * Request human approval
602
+ */
603
+ export const approve = createAITemplateFunction<HumanResult<{ approved: boolean; notes?: string }>>('object', {
604
+ system: 'Generate an approval request.',
605
+ })
606
+
607
+ /**
608
+ * Request human review
609
+ */
610
+ export const review = createAITemplateFunction<HumanResult<{ rating?: number; feedback: string; approved?: boolean }>>('object', {
611
+ system: 'Generate a review request.',
612
+ })
@@ -0,0 +1,15 @@
1
+ /**
2
+ * Cloudflare Workers AI Provider
3
+ *
4
+ * Re-exports from ai-providers/cloudflare for backwards compatibility.
5
+ *
6
+ * @packageDocumentation
7
+ */
8
+
9
+ export {
10
+ cloudflare,
11
+ cloudflareEmbedding,
12
+ DEFAULT_CF_EMBEDDING_MODEL,
13
+ type CloudflareConfig,
14
+ type CloudflareEmbeddingModel
15
+ } from 'ai-providers/cloudflare'
@@ -0,0 +1,14 @@
1
+ /**
2
+ * AI Providers
3
+ *
4
+ * Re-exports model providers from ai-providers package.
5
+ *
6
+ * @packageDocumentation
7
+ */
8
+
9
+ export {
10
+ cloudflare,
11
+ cloudflareEmbedding,
12
+ DEFAULT_CF_EMBEDDING_MODEL,
13
+ type CloudflareConfig
14
+ } from 'ai-providers/cloudflare'