ai-functions 2.1.3 → 2.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 (284) hide show
  1. package/.turbo/turbo-build.log +1 -1
  2. package/CHANGELOG.md +90 -1
  3. package/README.md +38 -0
  4. package/dist/ai-promise.d.ts +3 -3
  5. package/dist/ai-promise.d.ts.map +1 -1
  6. package/dist/ai-promise.js +135 -64
  7. package/dist/ai-promise.js.map +1 -1
  8. package/dist/ai-schemas.d.ts +56 -0
  9. package/dist/ai-schemas.d.ts.map +1 -0
  10. package/dist/ai-schemas.js +53 -0
  11. package/dist/ai-schemas.js.map +1 -0
  12. package/dist/ai.d.ts +16 -242
  13. package/dist/ai.d.ts.map +1 -1
  14. package/dist/ai.js +51 -858
  15. package/dist/ai.js.map +1 -1
  16. package/dist/batch/anthropic.d.ts +6 -4
  17. package/dist/batch/anthropic.d.ts.map +1 -1
  18. package/dist/batch/anthropic.js +83 -145
  19. package/dist/batch/anthropic.js.map +1 -1
  20. package/dist/batch/bedrock.d.ts +8 -30
  21. package/dist/batch/bedrock.d.ts.map +1 -1
  22. package/dist/batch/bedrock.js +155 -338
  23. package/dist/batch/bedrock.js.map +1 -1
  24. package/dist/batch/cloudflare.d.ts +8 -20
  25. package/dist/batch/cloudflare.d.ts.map +1 -1
  26. package/dist/batch/cloudflare.js +68 -189
  27. package/dist/batch/cloudflare.js.map +1 -1
  28. package/dist/batch/google.d.ts +6 -20
  29. package/dist/batch/google.d.ts.map +1 -1
  30. package/dist/batch/google.js +70 -238
  31. package/dist/batch/google.js.map +1 -1
  32. package/dist/batch/index.d.ts +4 -1
  33. package/dist/batch/index.d.ts.map +1 -1
  34. package/dist/batch/index.js +4 -1
  35. package/dist/batch/index.js.map +1 -1
  36. package/dist/batch/memory.d.ts +1 -1
  37. package/dist/batch/memory.d.ts.map +1 -1
  38. package/dist/batch/memory.js +14 -10
  39. package/dist/batch/memory.js.map +1 -1
  40. package/dist/batch/openai.d.ts +11 -14
  41. package/dist/batch/openai.d.ts.map +1 -1
  42. package/dist/batch/openai.js +52 -156
  43. package/dist/batch/openai.js.map +1 -1
  44. package/dist/batch/provider.d.ts +111 -0
  45. package/dist/batch/provider.d.ts.map +1 -0
  46. package/dist/batch/provider.js +233 -0
  47. package/dist/batch/provider.js.map +1 -0
  48. package/dist/batch-map.d.ts.map +1 -1
  49. package/dist/batch-map.js +23 -17
  50. package/dist/batch-map.js.map +1 -1
  51. package/dist/batch-queue.d.ts +65 -0
  52. package/dist/batch-queue.d.ts.map +1 -1
  53. package/dist/batch-queue.js +169 -14
  54. package/dist/batch-queue.js.map +1 -1
  55. package/dist/budget.d.ts.map +1 -1
  56. package/dist/budget.js +27 -14
  57. package/dist/budget.js.map +1 -1
  58. package/dist/cache.d.ts +23 -0
  59. package/dist/cache.d.ts.map +1 -1
  60. package/dist/cache.js +36 -15
  61. package/dist/cache.js.map +1 -1
  62. package/dist/context.d.ts +26 -8
  63. package/dist/context.d.ts.map +1 -1
  64. package/dist/context.js +64 -62
  65. package/dist/context.js.map +1 -1
  66. package/dist/digital-objects-registry.d.ts +229 -0
  67. package/dist/digital-objects-registry.d.ts.map +1 -0
  68. package/dist/digital-objects-registry.js +617 -0
  69. package/dist/digital-objects-registry.js.map +1 -0
  70. package/dist/embeddings.d.ts +2 -2
  71. package/dist/embeddings.d.ts.map +1 -1
  72. package/dist/errors.d.ts +22 -0
  73. package/dist/errors.d.ts.map +1 -0
  74. package/dist/errors.js +35 -0
  75. package/dist/errors.js.map +1 -0
  76. package/dist/eval/runner.d.ts +8 -0
  77. package/dist/eval/runner.d.ts.map +1 -1
  78. package/dist/eval/runner.js +41 -35
  79. package/dist/eval/runner.js.map +1 -1
  80. package/dist/eval-log/in-memory.d.ts +34 -0
  81. package/dist/eval-log/in-memory.d.ts.map +1 -0
  82. package/dist/eval-log/in-memory.js +84 -0
  83. package/dist/eval-log/in-memory.js.map +1 -0
  84. package/dist/eval-log/index.d.ts +29 -0
  85. package/dist/eval-log/index.d.ts.map +1 -0
  86. package/dist/eval-log/index.js +39 -0
  87. package/dist/eval-log/index.js.map +1 -0
  88. package/dist/eval-log/types.d.ts +101 -0
  89. package/dist/eval-log/types.d.ts.map +1 -0
  90. package/dist/eval-log/types.js +16 -0
  91. package/dist/eval-log/types.js.map +1 -0
  92. package/dist/function-registry.d.ts +176 -0
  93. package/dist/function-registry.d.ts.map +1 -0
  94. package/dist/function-registry.js +685 -0
  95. package/dist/function-registry.js.map +1 -0
  96. package/dist/generate.d.ts +9 -3
  97. package/dist/generate.d.ts.map +1 -1
  98. package/dist/generate.js +18 -18
  99. package/dist/generate.js.map +1 -1
  100. package/dist/index.d.ts +18 -11
  101. package/dist/index.d.ts.map +1 -1
  102. package/dist/index.js +35 -18
  103. package/dist/index.js.map +1 -1
  104. package/dist/logger.d.ts +118 -0
  105. package/dist/logger.d.ts.map +1 -0
  106. package/dist/logger.js +187 -0
  107. package/dist/logger.js.map +1 -0
  108. package/dist/middleware/budget.d.ts +84 -0
  109. package/dist/middleware/budget.d.ts.map +1 -0
  110. package/dist/middleware/budget.js +110 -0
  111. package/dist/middleware/budget.js.map +1 -0
  112. package/dist/middleware/cache.d.ts +103 -0
  113. package/dist/middleware/cache.d.ts.map +1 -0
  114. package/dist/middleware/cache.js +228 -0
  115. package/dist/middleware/cache.js.map +1 -0
  116. package/dist/middleware/embed-cache.d.ts +99 -0
  117. package/dist/middleware/embed-cache.d.ts.map +1 -0
  118. package/dist/middleware/embed-cache.js +128 -0
  119. package/dist/middleware/embed-cache.js.map +1 -0
  120. package/dist/middleware/index.d.ts +11 -0
  121. package/dist/middleware/index.d.ts.map +1 -0
  122. package/dist/middleware/index.js +11 -0
  123. package/dist/middleware/index.js.map +1 -0
  124. package/dist/middleware/trace.d.ts +103 -0
  125. package/dist/middleware/trace.d.ts.map +1 -0
  126. package/dist/middleware/trace.js +176 -0
  127. package/dist/middleware/trace.js.map +1 -0
  128. package/dist/primitives.d.ts +120 -1
  129. package/dist/primitives.d.ts.map +1 -1
  130. package/dist/primitives.js +398 -26
  131. package/dist/primitives.js.map +1 -1
  132. package/dist/retry.d.ts +66 -1
  133. package/dist/retry.d.ts.map +1 -1
  134. package/dist/retry.js +115 -8
  135. package/dist/retry.js.map +1 -1
  136. package/dist/sandbox.d.ts +36 -0
  137. package/dist/sandbox.d.ts.map +1 -0
  138. package/dist/sandbox.js +44 -0
  139. package/dist/sandbox.js.map +1 -0
  140. package/dist/schema.js +2 -2
  141. package/dist/schema.js.map +1 -1
  142. package/dist/telemetry.d.ts +128 -0
  143. package/dist/telemetry.d.ts.map +1 -0
  144. package/dist/telemetry.js +285 -0
  145. package/dist/telemetry.js.map +1 -0
  146. package/dist/template.d.ts.map +1 -1
  147. package/dist/template.js +6 -1
  148. package/dist/template.js.map +1 -1
  149. package/dist/tool-orchestration.d.ts +66 -4
  150. package/dist/tool-orchestration.d.ts.map +1 -1
  151. package/dist/tool-orchestration.js +123 -23
  152. package/dist/tool-orchestration.js.map +1 -1
  153. package/dist/type-guards.d.ts +28 -0
  154. package/dist/type-guards.d.ts.map +1 -0
  155. package/dist/type-guards.js +29 -0
  156. package/dist/type-guards.js.map +1 -0
  157. package/dist/types.d.ts +155 -19
  158. package/dist/types.d.ts.map +1 -1
  159. package/dist/types.js +36 -1
  160. package/dist/types.js.map +1 -1
  161. package/dist/wrap-for-v3.d.ts +80 -0
  162. package/dist/wrap-for-v3.d.ts.map +1 -0
  163. package/dist/wrap-for-v3.js +89 -0
  164. package/dist/wrap-for-v3.js.map +1 -0
  165. package/examples/00-quickstart.ts +232 -0
  166. package/examples/01-rag-chatbot.ts +212 -0
  167. package/examples/02-multi-agent-research.ts +290 -0
  168. package/examples/03-email-classification.ts +379 -0
  169. package/examples/04-content-moderation.ts +400 -0
  170. package/examples/05-document-extraction.ts +455 -0
  171. package/examples/06-streaming-chat-nextjs.ts +437 -0
  172. package/examples/07-cloudflare-worker.ts +483 -0
  173. package/examples/08-batch-processing.ts +491 -0
  174. package/examples/09-budget-constrained.ts +527 -0
  175. package/examples/10-tool-orchestration.ts +565 -0
  176. package/examples/11-retry-resilience.ts +403 -0
  177. package/examples/12-caching-strategies.ts +422 -0
  178. package/examples/README.md +145 -0
  179. package/package.json +29 -25
  180. package/src/ai-promise.ts +226 -140
  181. package/src/ai-schemas.ts +122 -0
  182. package/src/ai.ts +71 -1176
  183. package/src/batch/anthropic.ts +96 -161
  184. package/src/batch/bedrock.ts +203 -454
  185. package/src/batch/cloudflare.ts +99 -282
  186. package/src/batch/google.ts +91 -297
  187. package/src/batch/index.ts +4 -1
  188. package/src/batch/memory.ts +15 -10
  189. package/src/batch/openai.ts +65 -193
  190. package/src/batch/provider.ts +336 -0
  191. package/src/batch-map.ts +29 -24
  192. package/src/batch-queue.ts +200 -11
  193. package/src/budget.ts +31 -18
  194. package/src/cache.ts +45 -17
  195. package/src/context.ts +106 -77
  196. package/src/digital-objects-registry.ts +750 -0
  197. package/src/errors.ts +37 -0
  198. package/src/eval/runner.ts +60 -36
  199. package/src/eval-log/in-memory.ts +90 -0
  200. package/src/eval-log/index.ts +46 -0
  201. package/src/eval-log/types.ts +110 -0
  202. package/src/function-registry.ts +874 -0
  203. package/src/generate.ts +33 -28
  204. package/src/index.ts +122 -21
  205. package/src/logger.ts +232 -0
  206. package/src/middleware/budget.ts +171 -0
  207. package/src/middleware/cache.ts +299 -0
  208. package/src/middleware/embed-cache.ts +195 -0
  209. package/src/middleware/index.ts +23 -0
  210. package/src/middleware/trace.ts +248 -0
  211. package/src/primitives.ts +589 -62
  212. package/src/retry.ts +144 -18
  213. package/src/sandbox.ts +52 -0
  214. package/src/schema.ts +8 -8
  215. package/src/telemetry.ts +403 -0
  216. package/src/template.ts +8 -4
  217. package/src/tool-orchestration.ts +213 -48
  218. package/src/type-guards.ts +31 -0
  219. package/src/types.ts +186 -27
  220. package/src/wrap-for-v3.ts +105 -0
  221. package/test/ai-promise.test.ts +1080 -0
  222. package/test/ai-proxy.test.ts +1 -1
  223. package/test/batch-autosubmit-errors.test.ts +49 -37
  224. package/test/batch-blog-posts.test.ts +87 -129
  225. package/test/core-functions.test.ts +183 -579
  226. package/test/decide.test.ts +154 -322
  227. package/test/define.test.ts +211 -8
  228. package/test/digital-objects-registry.test.ts +760 -0
  229. package/test/embedding-cache-middleware.test.ts +140 -0
  230. package/test/fill-template.test.ts +89 -0
  231. package/test/generate-core.test.ts +140 -229
  232. package/test/implicit-batch.test.ts +22 -65
  233. package/test/retry-policy-integration.test.ts +117 -0
  234. package/test/sandbox-execution.test.ts +155 -0
  235. package/test/schema.test.ts +55 -19
  236. package/test/template.test.ts +1164 -0
  237. package/test/tool-orchestration.test.ts +270 -0
  238. package/test/wrap-for-v3.test.ts +612 -0
  239. package/vitest.config.js +6 -0
  240. package/vitest.config.ts +20 -0
  241. package/LICENSE +0 -21
  242. package/dist/rpc/auth.d.ts +0 -69
  243. package/dist/rpc/auth.d.ts.map +0 -1
  244. package/dist/rpc/auth.js +0 -136
  245. package/dist/rpc/auth.js.map +0 -1
  246. package/dist/rpc/client.d.ts +0 -62
  247. package/dist/rpc/client.d.ts.map +0 -1
  248. package/dist/rpc/client.js +0 -103
  249. package/dist/rpc/client.js.map +0 -1
  250. package/dist/rpc/deferred.d.ts +0 -60
  251. package/dist/rpc/deferred.d.ts.map +0 -1
  252. package/dist/rpc/deferred.js +0 -96
  253. package/dist/rpc/deferred.js.map +0 -1
  254. package/dist/rpc/index.d.ts +0 -22
  255. package/dist/rpc/index.d.ts.map +0 -1
  256. package/dist/rpc/index.js +0 -38
  257. package/dist/rpc/index.js.map +0 -1
  258. package/dist/rpc/local.d.ts +0 -42
  259. package/dist/rpc/local.d.ts.map +0 -1
  260. package/dist/rpc/local.js +0 -50
  261. package/dist/rpc/local.js.map +0 -1
  262. package/dist/rpc/server.d.ts +0 -165
  263. package/dist/rpc/server.d.ts.map +0 -1
  264. package/dist/rpc/server.js +0 -405
  265. package/dist/rpc/server.js.map +0 -1
  266. package/dist/rpc/session.d.ts +0 -32
  267. package/dist/rpc/session.d.ts.map +0 -1
  268. package/dist/rpc/session.js +0 -43
  269. package/dist/rpc/session.js.map +0 -1
  270. package/dist/rpc/transport.d.ts +0 -306
  271. package/dist/rpc/transport.d.ts.map +0 -1
  272. package/dist/rpc/transport.js +0 -731
  273. package/dist/rpc/transport.js.map +0 -1
  274. package/src/batch/anthropic.js +0 -256
  275. package/src/batch/bedrock.js +0 -584
  276. package/src/batch/cloudflare.js +0 -287
  277. package/src/batch/google.js +0 -359
  278. package/src/batch/index.js +0 -30
  279. package/src/batch/memory.js +0 -187
  280. package/src/batch/openai.js +0 -402
  281. package/src/eval/index.js +0 -7
  282. package/src/eval/models.js +0 -119
  283. package/src/eval/runner.js +0 -147
  284. package/test/schema.test.js +0 -96
package/src/generate.ts CHANGED
@@ -10,18 +10,15 @@
10
10
  */
11
11
 
12
12
  import {
13
- generateObject as sdkGenerateObject,
14
13
  generateText as sdkGenerateText,
15
- streamObject as sdkStreamObject,
16
14
  streamText as sdkStreamText,
17
- type GenerateObjectResult,
15
+ Output,
18
16
  type GenerateTextResult,
19
- type StreamObjectResult,
20
17
  type StreamTextResult,
21
- type LanguageModel
18
+ type LanguageModel,
22
19
  } from 'ai'
23
20
  import { schema as convertSchema, type SimpleSchema } from './schema.js'
24
- import { isZodSchema } from '@org.ai/core'
21
+ import { isZodSchema } from './type-guards.js'
25
22
  import type { ZodTypeAny } from 'zod'
26
23
 
27
24
  type ModelArg = string | LanguageModel
@@ -44,7 +41,11 @@ interface GenerateObjectOptions<T> {
44
41
  maxRetries?: number
45
42
  abortSignal?: AbortSignal
46
43
  headers?: Record<string, string>
47
- experimental_telemetry?: { isEnabled?: boolean; functionId?: string; metadata?: Record<string, string> }
44
+ experimental_telemetry?: {
45
+ isEnabled?: boolean
46
+ functionId?: string
47
+ metadata?: Record<string, string>
48
+ }
48
49
  }
49
50
 
50
51
  interface GenerateTextOptions {
@@ -65,7 +66,11 @@ interface GenerateTextOptions {
65
66
  tools?: Record<string, unknown>
66
67
  toolChoice?: 'auto' | 'none' | 'required' | { type: 'tool'; toolName: string }
67
68
  maxSteps?: number
68
- experimental_telemetry?: { isEnabled?: boolean; functionId?: string; metadata?: Record<string, string> }
69
+ experimental_telemetry?: {
70
+ isEnabled?: boolean
71
+ functionId?: string
72
+ metadata?: Record<string, string>
73
+ }
69
74
  }
70
75
 
71
76
  /**
@@ -131,16 +136,17 @@ function resolveSchema(schemaArg: SchemaArg): ZodTypeAny {
131
136
  */
132
137
  export async function generateObject<T>(
133
138
  options: GenerateObjectOptions<T>
134
- ): Promise<GenerateObjectResult<T>> {
139
+ ): Promise<{ object: T; usage?: unknown; warnings?: unknown[] | undefined }> {
135
140
  const model = await resolveModel(options.model)
136
141
  const schema = resolveSchema(options.schema as SchemaArg)
137
- // Use 'as any' to handle AI SDK v4 API variance
138
- return sdkGenerateObject({
139
- ...options,
142
+ const { schema: _schema, mode: _mode, ...rest } = options
143
+ // Using unknown cast for SDK compatibility - the AI SDK has complex type unions
144
+ const result = await sdkGenerateText({
145
+ ...rest,
140
146
  model,
141
- schema,
142
- output: 'object'
143
- } as any) as Promise<GenerateObjectResult<T>>
147
+ output: Output.object({ schema }),
148
+ } as unknown as Parameters<typeof sdkGenerateText>[0])
149
+ return { object: result.output as T, usage: result.usage, warnings: result.warnings }
144
150
  }
145
151
 
146
152
  /**
@@ -172,7 +178,7 @@ export async function generateText(
172
178
  const model = await resolveModel(options.model)
173
179
  return sdkGenerateText({
174
180
  ...options,
175
- model
181
+ model,
176
182
  } as Parameters<typeof sdkGenerateText>[0])
177
183
  }
178
184
 
@@ -196,20 +202,19 @@ export async function generateText(
196
202
  */
197
203
  export async function streamObject<T>(
198
204
  options: GenerateObjectOptions<T>
199
- ): Promise<StreamObjectResult<T, T, never>> {
205
+ ): Promise<{ partialObjectStream: AsyncIterable<T> }> {
200
206
  const model = await resolveModel(options.model)
201
207
  const schema = resolveSchema(options.schema as SchemaArg)
202
- // NOTE: Type assertion required due to AI SDK's complex conditional return types.
203
- // The SDK uses different return types based on output mode ('object' | 'array' | 'enum' | 'no-schema')
204
- // and schema definition presence. Our wrapper simplifies this to always use 'object' output mode.
205
- // This cast is safe because we control the input parameters.
206
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
207
- return sdkStreamObject({
208
- ...options,
208
+ const { schema: _schema, mode: _mode, ...rest } = options
209
+ // Using unknown cast for SDK compatibility - the AI SDK has complex type unions
210
+ const result = await sdkStreamText({
211
+ ...rest,
209
212
  model,
210
- schema,
211
- output: 'object'
212
- } as any) as StreamObjectResult<T, T, never>
213
+ output: Output.object({ schema }),
214
+ } as unknown as Parameters<typeof sdkStreamText>[0])
215
+ return {
216
+ partialObjectStream: result.partialOutputStream as AsyncIterable<T>,
217
+ }
213
218
  }
214
219
 
215
220
  /**
@@ -235,6 +240,6 @@ export async function streamText(
235
240
  const model = await resolveModel(options.model)
236
241
  return sdkStreamText({
237
242
  ...options,
238
- model
243
+ model,
239
244
  } as Parameters<typeof sdkStreamText>[0])
240
245
  }
package/src/index.ts CHANGED
@@ -17,10 +17,10 @@
17
17
  */
18
18
 
19
19
  // ============================================================================
20
- // Re-export core primitives from ai-core for backward compatibility
20
+ // Re-export core primitives for backward compatibility
21
21
  // ============================================================================
22
22
 
23
- // Types from ai-core
23
+ // Types
24
24
  export type {
25
25
  AIFunctionDefinition,
26
26
  JSONSchema,
@@ -46,6 +46,7 @@ export type {
46
46
  BaseFunctionDefinition,
47
47
  CodeFunctionDefinition,
48
48
  CodeFunctionResult,
49
+ CodeGenerationDefinition,
49
50
  GenerativeFunctionDefinition,
50
51
  GenerativeFunctionResult,
51
52
  AgenticFunctionDefinition,
@@ -56,12 +57,13 @@ export type {
56
57
  DefinedFunction,
57
58
  FunctionRegistry,
58
59
  AutoDefineResult,
59
- } from '@org.ai/core'
60
+ SandboxEnv,
61
+ } from './types.js'
60
62
 
61
- // Schema exports from ai-core
62
- export { schema, type SimpleSchema } from '@org.ai/core'
63
+ // Schema exports
64
+ export { schema, type SimpleSchema } from './schema.js'
63
65
 
64
- // Template exports from ai-core
66
+ // Template exports
65
67
  export {
66
68
  parseTemplate,
67
69
  createTemplateFunction,
@@ -73,9 +75,9 @@ export {
73
75
  type BatchableFunction,
74
76
  type StreamableList,
75
77
  type ChainablePromise,
76
- } from '@org.ai/core'
78
+ } from './template.js'
77
79
 
78
- // AIPromise exports from ai-core
80
+ // AIPromise exports
79
81
  export {
80
82
  AIPromise,
81
83
  isAIPromise,
@@ -93,12 +95,12 @@ export {
93
95
  type AIPromiseOptions,
94
96
  type StreamingAIPromise,
95
97
  type StreamOptions,
96
- } from '@org.ai/core'
98
+ } from './ai-promise.js'
97
99
 
98
- // Generation exports from ai-core
99
- export { generateObject, generateText, streamObject, streamText } from '@org.ai/core'
100
+ // Generation exports
101
+ export { generateObject, generateText, streamObject, streamText } from './generate.js'
100
102
 
101
- // Primitives from ai-core
103
+ // Primitives
102
104
  export {
103
105
  generate,
104
106
  type GenerateType,
@@ -125,9 +127,9 @@ export {
125
127
  review,
126
128
  type HumanOptions,
127
129
  type HumanResult,
128
- } from '@org.ai/core'
130
+ } from './primitives.js'
129
131
 
130
- // Context exports from ai-core (basic version)
132
+ // Context exports
131
133
  export {
132
134
  configure,
133
135
  getContext,
@@ -137,20 +139,41 @@ export {
137
139
  getModel,
138
140
  getProvider,
139
141
  type ExecutionContext,
140
- } from '@org.ai/core'
142
+ } from './context.js'
141
143
 
142
- // Type guards from ai-core
143
- export { isZodSchema } from '@org.ai/core'
144
+ // Type guards
145
+ export { isZodSchema } from './type-guards.js'
144
146
 
145
147
  // ============================================================================
146
148
  // Export AI Proxy (the AI class/proxy from ai.ts)
147
149
  // This is separate from the core 'ai' template function
148
150
  // ============================================================================
149
151
 
150
- export { AI, define, defineFunction, type AIProxy } from './ai.js'
151
-
152
- // Also export 'ai' primitive as 'aiPrompt' to avoid conflict with AIProxy
153
- export { ai as aiPrompt } from '@org.ai/core'
152
+ export {
153
+ AI,
154
+ ai as aiProxy,
155
+ define,
156
+ defineFunction,
157
+ functions,
158
+ createFunctionRegistry,
159
+ resetGlobalRegistry,
160
+ withTemplate,
161
+ type AIProxy,
162
+ // Also export from new split modules
163
+ createSchemaFunctions,
164
+ type AISchemaOptions,
165
+ type SchemaFunctions,
166
+ type InferSimpleSchemaResult,
167
+ createDefinedFunction,
168
+ convertArgsToJSONSchema,
169
+ fillTemplate,
170
+ generateCode,
171
+ generateAndRunCode,
172
+ type GeneratedCodeRunResult,
173
+ } from './ai.js'
174
+
175
+ // Also export 'ai' primitive as 'aiPrompt' for convenience
176
+ export { ai as aiPrompt } from './primitives.js'
154
177
 
155
178
  // Export embedding utilities (not in ai-core)
156
179
  export * from './embeddings.js'
@@ -169,6 +192,11 @@ export {
169
192
  withBatch as withBatchQueue,
170
193
  registerBatchAdapter,
171
194
  getBatchAdapter,
195
+ registerFlexAdapter,
196
+ getFlexAdapter,
197
+ hasFlexAdapter,
198
+ tiersForModel,
199
+ modelSupportsTier,
172
200
  isBatchMode,
173
201
  deferToBatch,
174
202
  BATCH_MODE_SYMBOL,
@@ -180,10 +208,21 @@ export {
180
208
  type BatchResult,
181
209
  type BatchSubmitResult,
182
210
  type BatchAdapter,
211
+ type FlexAdapter,
183
212
  type BatchQueueOptions,
184
213
  type DeferredOptions,
185
214
  } from './batch-queue.js'
186
215
 
216
+ // Export per-model policy data (sourced from language-models)
217
+ export {
218
+ policyFor as modelPolicyFor,
219
+ type ModelPolicy,
220
+ type BatchTier,
221
+ type RetryPolicyData,
222
+ type CircuitBreakerPolicyData,
223
+ type ErrorCategoryName,
224
+ } from 'language-models'
225
+
187
226
  // Export batch map for automatic batching
188
227
  export {
189
228
  BatchMapPromise,
@@ -330,3 +369,65 @@ export {
330
369
  type FallbackOptions,
331
370
  type FallbackMetrics,
332
371
  } from './retry.js'
372
+
373
+ // Export digital-objects function registry
374
+ export {
375
+ DigitalObjectsFunctionRegistry,
376
+ createDigitalObjectsRegistry,
377
+ FUNCTION_NOUNS,
378
+ FUNCTION_VERBS,
379
+ type StoredFunctionDefinition,
380
+ type FunctionCallData,
381
+ type DigitalObjectsRegistryOptions,
382
+ } from './digital-objects-registry.js'
383
+
384
+ // Export configurable logger
385
+ export {
386
+ configureLogger,
387
+ getLogger,
388
+ resetLogger,
389
+ getLogLevel,
390
+ setLogLevel,
391
+ type Logger,
392
+ type LoggerConfig,
393
+ } from './logger.js'
394
+
395
+ // ============================================================================
396
+ // V3 middleware stack — cache / budget / trace via wrapLanguageModel
397
+ // ============================================================================
398
+
399
+ // Composable middleware primitives
400
+ export {
401
+ cacheMiddleware,
402
+ embeddingCacheMiddleware,
403
+ budgetMiddleware,
404
+ traceMiddleware,
405
+ type CacheMiddlewareOptions,
406
+ type CacheMiddlewareStore,
407
+ type EmbedCacheMiddlewareOptions,
408
+ type EmbedCacheMiddlewareStore,
409
+ type BudgetMiddlewareOptions,
410
+ type PricingOverlay,
411
+ type TraceEvent,
412
+ type TraceEventKind,
413
+ type TraceMiddlewareOptions,
414
+ } from './middleware/index.js'
415
+
416
+ // Convenience composer
417
+ export { wrapForV3, type WrapForV3Options } from './wrap-for-v3.js'
418
+
419
+ // Type re-export — downstream packages (services-as-software EvaluatorPanel
420
+ // model-injection point, etc.) need `LanguageModelV3` for typing wrapped-model
421
+ // arguments. Re-exporting from ai-functions avoids forcing every consumer to
422
+ // depend on `@ai-sdk/provider` directly.
423
+ export type { LanguageModelV3 } from '@ai-sdk/provider'
424
+
425
+ // Pluggable persistence primitive for trace/eval entries
426
+ export {
427
+ InMemoryEvalLogStore,
428
+ getEvalLogStore,
429
+ configureEvalLogStore,
430
+ type EvalLogEntry,
431
+ type EvalLogListOptions,
432
+ type EvalLogStore,
433
+ } from './eval-log/index.js'
package/src/logger.ts ADDED
@@ -0,0 +1,232 @@
1
+ /**
2
+ * Configurable Logger for AI Functions
3
+ *
4
+ * Provides a pluggable logging interface that defaults to console but can be
5
+ * configured to use any logging implementation. This allows library consumers
6
+ * to integrate with their own logging infrastructure.
7
+ *
8
+ * @example
9
+ * ```ts
10
+ * import { configureLogger, getLogger } from 'ai-functions'
11
+ *
12
+ * // Use with default console logger
13
+ * const logger = getLogger()
14
+ * logger.warn('Something happened')
15
+ *
16
+ * // Configure a custom logger
17
+ * configureLogger({
18
+ * debug: (msg, ...args) => myLogger.debug(msg, ...args),
19
+ * info: (msg, ...args) => myLogger.info(msg, ...args),
20
+ * warn: (msg, ...args) => myLogger.warn(msg, ...args),
21
+ * error: (msg, ...args) => myLogger.error(msg, ...args),
22
+ * })
23
+ *
24
+ * // Disable logging entirely
25
+ * configureLogger(null)
26
+ * ```
27
+ *
28
+ * @packageDocumentation
29
+ */
30
+
31
+ // ============================================================================
32
+ // Logger Interface
33
+ // ============================================================================
34
+
35
+ /**
36
+ * Logger interface that matches the standard console methods.
37
+ *
38
+ * All methods are optional to allow partial implementations.
39
+ * Missing methods will be no-ops when called.
40
+ */
41
+ export interface Logger {
42
+ /** Debug level logging (verbose) */
43
+ debug?: (message: string, ...args: unknown[]) => void
44
+ /** Info level logging (general information) */
45
+ info?: (message: string, ...args: unknown[]) => void
46
+ /** Warning level logging (potential issues) */
47
+ warn?: (message: string, ...args: unknown[]) => void
48
+ /** Error level logging (errors and exceptions) */
49
+ error?: (message: string, ...args: unknown[]) => void
50
+ }
51
+
52
+ /**
53
+ * Logger configuration options
54
+ */
55
+ export interface LoggerConfig {
56
+ /** The logger implementation to use */
57
+ logger?: Logger | null
58
+ /** Minimum log level to output */
59
+ level?: 'debug' | 'info' | 'warn' | 'error' | 'silent'
60
+ }
61
+
62
+ // ============================================================================
63
+ // Logger State
64
+ // ============================================================================
65
+
66
+ /** Log level priority (lower = more verbose) */
67
+ const LOG_LEVELS: Record<string, number> = {
68
+ debug: 0,
69
+ info: 1,
70
+ warn: 2,
71
+ error: 3,
72
+ silent: 4,
73
+ }
74
+
75
+ /** Default console-based logger */
76
+ const defaultLogger: Logger = {
77
+ debug: (message: string, ...args: unknown[]) => console.debug(message, ...args),
78
+ info: (message: string, ...args: unknown[]) => console.info(message, ...args),
79
+ warn: (message: string, ...args: unknown[]) => console.warn(message, ...args),
80
+ error: (message: string, ...args: unknown[]) => console.error(message, ...args),
81
+ }
82
+
83
+ /** No-op logger for when logging is disabled */
84
+ const noopLogger: Logger = {
85
+ debug: () => {},
86
+ info: () => {},
87
+ warn: () => {},
88
+ error: () => {},
89
+ }
90
+
91
+ /** Current logger instance */
92
+ let currentLogger: Logger = defaultLogger
93
+
94
+ /** Current minimum log level */
95
+ let currentLevel: string = 'warn'
96
+
97
+ // ============================================================================
98
+ // Configuration Functions
99
+ // ============================================================================
100
+
101
+ /**
102
+ * Configure the global logger for ai-functions.
103
+ *
104
+ * @param config - Logger configuration or just a logger instance
105
+ *
106
+ * @example
107
+ * ```ts
108
+ * // Use a custom logger
109
+ * configureLogger({
110
+ * logger: {
111
+ * debug: (msg) => myLogger.debug(msg),
112
+ * info: (msg) => myLogger.info(msg),
113
+ * warn: (msg) => myLogger.warn(msg),
114
+ * error: (msg) => myLogger.error(msg),
115
+ * },
116
+ * level: 'info',
117
+ * })
118
+ *
119
+ * // Just set the log level
120
+ * configureLogger({ level: 'debug' })
121
+ *
122
+ * // Disable all logging
123
+ * configureLogger({ logger: null })
124
+ * // or
125
+ * configureLogger({ level: 'silent' })
126
+ *
127
+ * // Reset to defaults
128
+ * configureLogger({})
129
+ * ```
130
+ */
131
+ export function configureLogger(config: LoggerConfig | Logger | null): void {
132
+ // Handle null (disable logging)
133
+ if (config === null) {
134
+ currentLogger = noopLogger
135
+ return
136
+ }
137
+
138
+ // Handle Logger directly
139
+ if (config && ('debug' in config || 'info' in config || 'warn' in config || 'error' in config)) {
140
+ // Check if it's a Logger (has at least one log method but no 'logger' or 'level' key)
141
+ if (!('logger' in config) && !('level' in config)) {
142
+ currentLogger = config as Logger
143
+ return
144
+ }
145
+ }
146
+
147
+ // Handle LoggerConfig
148
+ const loggerConfig = config as LoggerConfig
149
+
150
+ if (loggerConfig.logger === null) {
151
+ currentLogger = noopLogger
152
+ } else if (loggerConfig.logger) {
153
+ currentLogger = loggerConfig.logger
154
+ } else if (config === undefined || Object.keys(config as object).length === 0) {
155
+ // Reset to default
156
+ currentLogger = defaultLogger
157
+ currentLevel = 'warn'
158
+ return
159
+ }
160
+
161
+ if (loggerConfig.level) {
162
+ currentLevel = loggerConfig.level
163
+ }
164
+ }
165
+
166
+ /**
167
+ * Get the current logger instance.
168
+ *
169
+ * Returns a logger that respects the configured log level.
170
+ * Methods for levels below the configured minimum will be no-ops.
171
+ *
172
+ * @returns Logger instance with level filtering applied
173
+ *
174
+ * @example
175
+ * ```ts
176
+ * const logger = getLogger()
177
+ * logger.warn('This is a warning')
178
+ * logger.error('This is an error', { details: 'foo' })
179
+ * ```
180
+ */
181
+ export function getLogger(): Required<Logger> {
182
+ const minLevel = LOG_LEVELS[currentLevel] ?? LOG_LEVELS['warn']!
183
+
184
+ return {
185
+ debug: (message: string, ...args: unknown[]) => {
186
+ if (minLevel <= LOG_LEVELS['debug']!) {
187
+ currentLogger.debug?.(message, ...args)
188
+ }
189
+ },
190
+ info: (message: string, ...args: unknown[]) => {
191
+ if (minLevel <= LOG_LEVELS['info']!) {
192
+ currentLogger.info?.(message, ...args)
193
+ }
194
+ },
195
+ warn: (message: string, ...args: unknown[]) => {
196
+ if (minLevel <= LOG_LEVELS['warn']!) {
197
+ currentLogger.warn?.(message, ...args)
198
+ }
199
+ },
200
+ error: (message: string, ...args: unknown[]) => {
201
+ if (minLevel <= LOG_LEVELS['error']!) {
202
+ currentLogger.error?.(message, ...args)
203
+ }
204
+ },
205
+ }
206
+ }
207
+
208
+ /**
209
+ * Reset the logger to default configuration.
210
+ *
211
+ * Restores the console-based logger with 'warn' minimum level.
212
+ */
213
+ export function resetLogger(): void {
214
+ currentLogger = defaultLogger
215
+ currentLevel = 'warn'
216
+ }
217
+
218
+ /**
219
+ * Get the current log level
220
+ */
221
+ export function getLogLevel(): string {
222
+ return currentLevel
223
+ }
224
+
225
+ /**
226
+ * Set the minimum log level
227
+ *
228
+ * @param level - Minimum level to log ('debug' | 'info' | 'warn' | 'error' | 'silent')
229
+ */
230
+ export function setLogLevel(level: 'debug' | 'info' | 'warn' | 'error' | 'silent'): void {
231
+ currentLevel = level
232
+ }