ai-functions 0.3.0 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +5 -0
- package/CHANGELOG.md +9 -0
- package/README.md +190 -86
- package/TODO.md +138 -0
- package/dist/ai-promise.d.ts +219 -0
- package/dist/ai-promise.d.ts.map +1 -0
- package/dist/ai-promise.js +610 -0
- package/dist/ai-promise.js.map +1 -0
- package/dist/ai.d.ts +285 -0
- package/dist/ai.d.ts.map +1 -0
- package/dist/ai.js +842 -0
- package/dist/ai.js.map +1 -0
- package/dist/batch/anthropic.d.ts +23 -0
- package/dist/batch/anthropic.d.ts.map +1 -0
- package/dist/batch/anthropic.js +257 -0
- package/dist/batch/anthropic.js.map +1 -0
- package/dist/batch/bedrock.d.ts +64 -0
- package/dist/batch/bedrock.d.ts.map +1 -0
- package/dist/batch/bedrock.js +586 -0
- package/dist/batch/bedrock.js.map +1 -0
- package/dist/batch/cloudflare.d.ts +37 -0
- package/dist/batch/cloudflare.d.ts.map +1 -0
- package/dist/batch/cloudflare.js +289 -0
- package/dist/batch/cloudflare.js.map +1 -0
- package/dist/batch/google.d.ts +41 -0
- package/dist/batch/google.d.ts.map +1 -0
- package/dist/batch/google.js +360 -0
- package/dist/batch/google.js.map +1 -0
- package/dist/batch/index.d.ts +31 -0
- package/dist/batch/index.d.ts.map +1 -0
- package/dist/batch/index.js +31 -0
- package/dist/batch/index.js.map +1 -0
- package/dist/batch/memory.d.ts +44 -0
- package/dist/batch/memory.d.ts.map +1 -0
- package/dist/batch/memory.js +188 -0
- package/dist/batch/memory.js.map +1 -0
- package/dist/batch/openai.d.ts +37 -0
- package/dist/batch/openai.d.ts.map +1 -0
- package/dist/batch/openai.js +403 -0
- package/dist/batch/openai.js.map +1 -0
- package/dist/batch-map.d.ts +125 -0
- package/dist/batch-map.d.ts.map +1 -0
- package/dist/batch-map.js +406 -0
- package/dist/batch-map.js.map +1 -0
- package/dist/batch-queue.d.ts +273 -0
- package/dist/batch-queue.d.ts.map +1 -0
- package/dist/batch-queue.js +271 -0
- package/dist/batch-queue.js.map +1 -0
- package/dist/context.d.ts +133 -0
- package/dist/context.d.ts.map +1 -0
- package/dist/context.js +267 -0
- package/dist/context.js.map +1 -0
- package/dist/embeddings.d.ts +123 -0
- package/dist/embeddings.d.ts.map +1 -0
- package/dist/embeddings.js +170 -0
- package/dist/embeddings.js.map +1 -0
- package/dist/eval/index.d.ts +8 -0
- package/dist/eval/index.d.ts.map +1 -0
- package/dist/eval/index.js +8 -0
- package/dist/eval/index.js.map +1 -0
- package/dist/eval/models.d.ts +66 -0
- package/dist/eval/models.d.ts.map +1 -0
- package/dist/eval/models.js +120 -0
- package/dist/eval/models.js.map +1 -0
- package/dist/eval/runner.d.ts +64 -0
- package/dist/eval/runner.d.ts.map +1 -0
- package/dist/eval/runner.js +148 -0
- package/dist/eval/runner.js.map +1 -0
- package/dist/generate.d.ts +168 -0
- package/dist/generate.d.ts.map +1 -0
- package/dist/generate.js +174 -0
- package/dist/generate.js.map +1 -0
- package/dist/index.d.ts +29 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +53 -52
- package/dist/index.js.map +1 -1
- package/dist/primitives.d.ts +292 -0
- package/dist/primitives.d.ts.map +1 -0
- package/dist/primitives.js +471 -0
- package/dist/primitives.js.map +1 -0
- package/dist/providers/cloudflare.d.ts +9 -0
- package/dist/providers/cloudflare.d.ts.map +1 -0
- package/dist/providers/cloudflare.js +9 -0
- package/dist/providers/cloudflare.js.map +1 -0
- package/dist/providers/index.d.ts +9 -0
- package/dist/providers/index.d.ts.map +1 -0
- package/dist/providers/index.js +9 -0
- package/dist/providers/index.js.map +1 -0
- package/dist/rpc/auth.d.ts +69 -0
- package/dist/rpc/auth.d.ts.map +1 -0
- package/dist/rpc/auth.js +136 -0
- package/dist/rpc/auth.js.map +1 -0
- package/dist/rpc/client.d.ts +62 -0
- package/dist/rpc/client.d.ts.map +1 -0
- package/dist/rpc/client.js +103 -0
- package/dist/rpc/client.js.map +1 -0
- package/dist/rpc/deferred.d.ts +60 -0
- package/dist/rpc/deferred.d.ts.map +1 -0
- package/dist/rpc/deferred.js +96 -0
- package/dist/rpc/deferred.js.map +1 -0
- package/dist/rpc/index.d.ts +22 -0
- package/dist/rpc/index.d.ts.map +1 -0
- package/dist/rpc/index.js +38 -0
- package/dist/rpc/index.js.map +1 -0
- package/dist/rpc/local.d.ts +42 -0
- package/dist/rpc/local.d.ts.map +1 -0
- package/dist/rpc/local.js +50 -0
- package/dist/rpc/local.js.map +1 -0
- package/dist/rpc/server.d.ts +165 -0
- package/dist/rpc/server.d.ts.map +1 -0
- package/dist/rpc/server.js +405 -0
- package/dist/rpc/server.js.map +1 -0
- package/dist/rpc/session.d.ts +32 -0
- package/dist/rpc/session.d.ts.map +1 -0
- package/dist/rpc/session.js +43 -0
- package/dist/rpc/session.js.map +1 -0
- package/dist/rpc/transport.d.ts +306 -0
- package/dist/rpc/transport.d.ts.map +1 -0
- package/dist/rpc/transport.js +731 -0
- package/dist/rpc/transport.js.map +1 -0
- package/dist/schema.d.ts +54 -0
- package/dist/schema.d.ts.map +1 -0
- package/dist/schema.js +109 -0
- package/dist/schema.js.map +1 -0
- package/dist/template.d.ts +73 -0
- package/dist/template.d.ts.map +1 -0
- package/dist/template.js +129 -0
- package/dist/template.js.map +1 -0
- package/dist/types.d.ts +474 -106
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +4 -8
- package/dist/types.js.map +1 -1
- package/evalite.config.ts +19 -0
- package/evals/README.md +212 -0
- package/evals/classification.eval.ts +108 -0
- package/evals/marketing.eval.ts +370 -0
- package/evals/math.eval.ts +94 -0
- package/evals/run-evals.ts +166 -0
- package/evals/structured-output.eval.ts +143 -0
- package/evals/writing.eval.ts +117 -0
- package/examples/batch-blog-posts.ts +160 -0
- package/package.json +57 -57
- package/src/ai-promise.ts +784 -0
- package/src/ai.ts +1183 -0
- package/src/batch/anthropic.ts +375 -0
- package/src/batch/bedrock.ts +801 -0
- package/src/batch/cloudflare.ts +421 -0
- package/src/batch/google.ts +491 -0
- package/src/batch/index.ts +31 -0
- package/src/batch/memory.ts +253 -0
- package/src/batch/openai.ts +557 -0
- package/src/batch-map.ts +534 -0
- package/src/batch-queue.ts +493 -0
- package/src/context.ts +332 -0
- package/src/embeddings.ts +244 -0
- package/src/eval/index.ts +8 -0
- package/src/eval/models.ts +158 -0
- package/src/eval/runner.ts +217 -0
- package/src/generate.ts +245 -0
- package/src/index.ts +154 -0
- package/src/primitives.ts +612 -0
- package/src/providers/cloudflare.ts +15 -0
- package/src/providers/index.ts +14 -0
- package/src/schema.ts +147 -0
- package/src/template.ts +209 -0
- package/src/types.ts +540 -0
- package/test/README.md +105 -0
- package/test/ai-proxy.test.ts +192 -0
- package/test/async-iterators.test.ts +327 -0
- package/test/batch-background.test.ts +482 -0
- package/test/batch-blog-posts.test.ts +387 -0
- package/test/blog-generation.test.ts +510 -0
- package/test/browse-read.test.ts +611 -0
- package/test/core-functions.test.ts +694 -0
- package/test/decide.test.ts +393 -0
- package/test/define.test.ts +274 -0
- package/test/e2e-bedrock-manual.ts +163 -0
- package/test/e2e-bedrock.test.ts +191 -0
- package/test/e2e-flex-gateway.ts +157 -0
- package/test/e2e-flex-manual.ts +183 -0
- package/test/e2e-flex.test.ts +209 -0
- package/test/e2e-google-manual.ts +178 -0
- package/test/e2e-google.test.ts +216 -0
- package/test/embeddings.test.ts +284 -0
- package/test/evals/define-function.eval.test.ts +379 -0
- package/test/evals/primitives.eval.test.ts +384 -0
- package/test/function-types.test.ts +492 -0
- package/test/generate-core.test.ts +319 -0
- package/test/generate.test.ts +163 -0
- package/test/implicit-batch.test.ts +422 -0
- package/test/schema.test.ts +109 -0
- package/test/tagged-templates.test.ts +302 -0
- package/tsconfig.json +10 -0
- package/vitest.config.ts +42 -0
- package/LICENSE +0 -21
- package/bin/cli.js +0 -5
- package/dist/cli/index.d.ts +0 -10
- package/dist/cli/index.d.ts.map +0 -1
- package/dist/cli/index.js +0 -38
- package/dist/cli/index.js.map +0 -1
- package/dist/cli/index.test.d.ts +0 -2
- package/dist/cli/index.test.d.ts.map +0 -1
- package/dist/cli/index.test.js +0 -35
- package/dist/cli/index.test.js.map +0 -1
- package/dist/constants/models.d.ts +0 -10
- package/dist/constants/models.d.ts.map +0 -1
- package/dist/constants/models.js +0 -12
- package/dist/constants/models.js.map +0 -1
- package/dist/converters/index.d.ts +0 -3
- package/dist/converters/index.d.ts.map +0 -1
- package/dist/converters/index.js +0 -3
- package/dist/converters/index.js.map +0 -1
- package/dist/converters/model.d.ts +0 -4
- package/dist/converters/model.d.ts.map +0 -1
- package/dist/converters/model.js +0 -19
- package/dist/converters/model.js.map +0 -1
- package/dist/converters/schema.d.ts +0 -4
- package/dist/converters/schema.d.ts.map +0 -1
- package/dist/converters/schema.js +0 -25
- package/dist/converters/schema.js.map +0 -1
- package/dist/core/responses.d.ts +0 -5
- package/dist/core/responses.d.ts.map +0 -1
- package/dist/core/responses.js +0 -16
- package/dist/core/responses.js.map +0 -1
- package/dist/core/responses.test.d.ts +0 -2
- package/dist/core/responses.test.d.ts.map +0 -1
- package/dist/core/responses.test.js +0 -31
- package/dist/core/responses.test.js.map +0 -1
- package/dist/errors.d.ts +0 -6
- package/dist/errors.d.ts.map +0 -1
- package/dist/errors.js +0 -9
- package/dist/errors.js.map +0 -1
- package/dist/examples/streaming.test.d.ts +0 -2
- package/dist/examples/streaming.test.d.ts.map +0 -1
- package/dist/examples/streaming.test.js +0 -176
- package/dist/examples/streaming.test.js.map +0 -1
- package/dist/factory/__tests__/index.test.d.ts +0 -2
- package/dist/factory/__tests__/index.test.d.ts.map +0 -1
- package/dist/factory/__tests__/index.test.js +0 -430
- package/dist/factory/__tests__/index.test.js.map +0 -1
- package/dist/factory/__tests__/list.test.d.ts +0 -2
- package/dist/factory/__tests__/list.test.d.ts.map +0 -1
- package/dist/factory/__tests__/list.test.js +0 -92
- package/dist/factory/__tests__/list.test.js.map +0 -1
- package/dist/factory/index.d.ts +0 -20
- package/dist/factory/index.d.ts.map +0 -1
- package/dist/factory/index.js +0 -287
- package/dist/factory/index.js.map +0 -1
- package/dist/factory/index.test.d.ts +0 -2
- package/dist/factory/index.test.d.ts.map +0 -1
- package/dist/factory/index.test.js +0 -287
- package/dist/factory/index.test.js.map +0 -1
- package/dist/factory/list.d.ts +0 -3
- package/dist/factory/list.d.ts.map +0 -1
- package/dist/factory/list.js +0 -221
- package/dist/factory/list.js.map +0 -1
- package/dist/factory/list.test.d.ts +0 -2
- package/dist/factory/list.test.d.ts.map +0 -1
- package/dist/factory/list.test.js +0 -84
- package/dist/factory/list.test.js.map +0 -1
- package/dist/generate/index.d.ts +0 -5
- package/dist/generate/index.d.ts.map +0 -1
- package/dist/generate/index.js +0 -17
- package/dist/generate/index.js.map +0 -1
- package/dist/index.test.d.ts +0 -2
- package/dist/index.test.d.ts.map +0 -1
- package/dist/index.test.js +0 -59
- package/dist/index.test.js.map +0 -1
- package/dist/list/await.d.ts +0 -3
- package/dist/list/await.d.ts.map +0 -1
- package/dist/list/await.js +0 -28
- package/dist/list/await.js.map +0 -1
- package/dist/list/constants.d.ts +0 -4
- package/dist/list/constants.d.ts.map +0 -1
- package/dist/list/constants.js +0 -5
- package/dist/list/constants.js.map +0 -1
- package/dist/list/create-function.d.ts +0 -3
- package/dist/list/create-function.d.ts.map +0 -1
- package/dist/list/create-function.js +0 -11
- package/dist/list/create-function.js.map +0 -1
- package/dist/list/index.d.ts +0 -4
- package/dist/list/index.d.ts.map +0 -1
- package/dist/list/index.js +0 -5
- package/dist/list/index.js.map +0 -1
- package/dist/list/prompt.d.ts +0 -3
- package/dist/list/prompt.d.ts.map +0 -1
- package/dist/list/prompt.js +0 -6
- package/dist/list/prompt.js.map +0 -1
- package/dist/list/schemas.d.ts +0 -4
- package/dist/list/schemas.d.ts.map +0 -1
- package/dist/list/schemas.js +0 -8
- package/dist/list/schemas.js.map +0 -1
- package/dist/list/stream.d.ts +0 -3
- package/dist/list/stream.d.ts.map +0 -1
- package/dist/list/stream.js +0 -33
- package/dist/list/stream.js.map +0 -1
- package/dist/list/types.d.ts +0 -11
- package/dist/list/types.d.ts.map +0 -1
- package/dist/list/types.js +0 -2
- package/dist/list/types.js.map +0 -1
- package/dist/list/validation.d.ts +0 -3
- package/dist/list/validation.d.ts.map +0 -1
- package/dist/list/validation.js +0 -12
- package/dist/list/validation.js.map +0 -1
- package/dist/providers/config.d.ts +0 -4
- package/dist/providers/config.d.ts.map +0 -1
- package/dist/providers/config.js +0 -21
- package/dist/providers/config.js.map +0 -1
- package/dist/providers/config.test.d.ts +0 -2
- package/dist/providers/config.test.d.ts.map +0 -1
- package/dist/providers/config.test.js +0 -37
- package/dist/providers/config.test.js.map +0 -1
- package/dist/proxy/constants.d.ts +0 -4
- package/dist/proxy/constants.d.ts.map +0 -1
- package/dist/proxy/constants.js +0 -5
- package/dist/proxy/constants.js.map +0 -1
- package/dist/proxy/create-function.d.ts +0 -4
- package/dist/proxy/create-function.d.ts.map +0 -1
- package/dist/proxy/create-function.js +0 -24
- package/dist/proxy/create-function.js.map +0 -1
- package/dist/proxy/create-proxy.d.ts +0 -2
- package/dist/proxy/create-proxy.d.ts.map +0 -1
- package/dist/proxy/create-proxy.js +0 -11
- package/dist/proxy/create-proxy.js.map +0 -1
- package/dist/proxy/function-generator.d.ts +0 -9
- package/dist/proxy/function-generator.d.ts.map +0 -1
- package/dist/proxy/function-generator.js +0 -29
- package/dist/proxy/function-generator.js.map +0 -1
- package/dist/proxy/index.d.ts +0 -4
- package/dist/proxy/index.d.ts.map +0 -1
- package/dist/proxy/index.js +0 -4
- package/dist/proxy/index.js.map +0 -1
- package/dist/proxy/prompt.d.ts +0 -2
- package/dist/proxy/prompt.d.ts.map +0 -1
- package/dist/proxy/prompt.js +0 -6
- package/dist/proxy/prompt.js.map +0 -1
- package/dist/proxy/types.d.ts +0 -7
- package/dist/proxy/types.d.ts.map +0 -1
- package/dist/proxy/types.js +0 -2
- package/dist/proxy/types.js.map +0 -1
- package/dist/queue/manager.d.ts +0 -5
- package/dist/queue/manager.d.ts.map +0 -1
- package/dist/queue/manager.js +0 -37
- package/dist/queue/manager.js.map +0 -1
- package/dist/queue/manager.test.d.ts +0 -2
- package/dist/queue/manager.test.d.ts.map +0 -1
- package/dist/queue/manager.test.js +0 -52
- package/dist/queue/manager.test.js.map +0 -1
- package/dist/schema-converter.d.ts +0 -4
- package/dist/schema-converter.d.ts.map +0 -1
- package/dist/schema-converter.js +0 -30
- package/dist/schema-converter.js.map +0 -1
- package/dist/stream/index.d.ts +0 -7
- package/dist/stream/index.d.ts.map +0 -1
- package/dist/stream/index.js +0 -23
- package/dist/stream/index.js.map +0 -1
- package/dist/streaming/utils.d.ts +0 -4
- package/dist/streaming/utils.d.ts.map +0 -1
- package/dist/streaming/utils.js +0 -131
- package/dist/streaming/utils.js.map +0 -1
- package/dist/streaming/utils.test.d.ts +0 -2
- package/dist/streaming/utils.test.d.ts.map +0 -1
- package/dist/streaming/utils.test.js +0 -84
- package/dist/streaming/utils.test.js.map +0 -1
- package/dist/templates/result.d.ts +0 -7
- package/dist/templates/result.d.ts.map +0 -1
- package/dist/templates/result.js +0 -40
- package/dist/templates/result.js.map +0 -1
- package/dist/templates/result.test.d.ts +0 -2
- package/dist/templates/result.test.d.ts.map +0 -1
- package/dist/templates/result.test.js +0 -75
- package/dist/templates/result.test.js.map +0 -1
- package/dist/test/setup.d.ts +0 -2
- package/dist/test/setup.d.ts.map +0 -1
- package/dist/test/setup.js +0 -21
- package/dist/test/setup.js.map +0 -1
- package/dist/test-types.d.ts +0 -13
- package/dist/test-types.d.ts.map +0 -1
- package/dist/test-types.js +0 -55
- package/dist/test-types.js.map +0 -1
- package/dist/types/index.d.ts +0 -4
- package/dist/types/index.d.ts.map +0 -1
- package/dist/types/index.js +0 -4
- package/dist/types/index.js.map +0 -1
- package/dist/types/list.d.ts +0 -10
- package/dist/types/list.d.ts.map +0 -1
- package/dist/types/list.js +0 -2
- package/dist/types/list.js.map +0 -1
- package/dist/types/model.d.ts +0 -7
- package/dist/types/model.d.ts.map +0 -1
- package/dist/types/model.js +0 -2
- package/dist/types/model.js.map +0 -1
- package/dist/types/options.d.ts +0 -25
- package/dist/types/options.d.ts.map +0 -1
- package/dist/types/options.js +0 -2
- package/dist/types/options.js.map +0 -1
- package/dist/types/schema.d.ts +0 -5
- package/dist/types/schema.d.ts.map +0 -1
- package/dist/types/schema.js +0 -2
- package/dist/types/schema.js.map +0 -1
- package/dist/utils/__tests__/request-handler.test.d.ts +0 -2
- package/dist/utils/__tests__/request-handler.test.d.ts.map +0 -1
- package/dist/utils/__tests__/request-handler.test.js +0 -134
- package/dist/utils/__tests__/request-handler.test.js.map +0 -1
- package/dist/utils/__tests__/schema.test.d.ts +0 -2
- package/dist/utils/__tests__/schema.test.d.ts.map +0 -1
- package/dist/utils/__tests__/schema.test.js +0 -49
- package/dist/utils/__tests__/schema.test.js.map +0 -1
- package/dist/utils/__tests__/stream-progress.test.d.ts +0 -2
- package/dist/utils/__tests__/stream-progress.test.d.ts.map +0 -1
- package/dist/utils/__tests__/stream-progress.test.js +0 -85
- package/dist/utils/__tests__/stream-progress.test.js.map +0 -1
- package/dist/utils/index.d.ts +0 -2
- package/dist/utils/index.d.ts.map +0 -1
- package/dist/utils/index.js +0 -2
- package/dist/utils/index.js.map +0 -1
- package/dist/utils/request-handler.d.ts +0 -17
- package/dist/utils/request-handler.d.ts.map +0 -1
- package/dist/utils/request-handler.js +0 -105
- package/dist/utils/request-handler.js.map +0 -1
- package/dist/utils/schema.d.ts +0 -11
- package/dist/utils/schema.d.ts.map +0 -1
- package/dist/utils/schema.js +0 -51
- package/dist/utils/schema.js.map +0 -1
- package/dist/utils/stream-progress.d.ts +0 -17
- package/dist/utils/stream-progress.d.ts.map +0 -1
- package/dist/utils/stream-progress.js +0 -86
- package/dist/utils/stream-progress.js.map +0 -1
- package/dist/utils/validation.d.ts +0 -3
- package/dist/utils/validation.d.ts.map +0 -1
- package/dist/utils/validation.js +0 -30
- package/dist/utils/validation.js.map +0 -1
package/CHANGELOG.md
ADDED
package/README.md
CHANGED
|
@@ -1,18 +1,15 @@
|
|
|
1
1
|
# ai-functions
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
[](https://opensource.org/licenses/MIT)
|
|
3
|
+
Call AI like you'd talk to a colleague. No prompts. No configuration. Just say what you need.
|
|
5
4
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
## Features
|
|
5
|
+
```typescript
|
|
6
|
+
import { ai, list, is } from 'ai-functions'
|
|
9
7
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
- 🔍 Support for various output formats (objects, arrays, enums)
|
|
8
|
+
// Ask for anything - it reads like English
|
|
9
|
+
const qualified = is`${lead} a good fit for our enterprise plan?`
|
|
10
|
+
const ideas = list`blog posts that would resonate with ${persona}`
|
|
11
|
+
const { summary, nextSteps } = ai`analyze this sales call: ${transcript}`
|
|
12
|
+
```
|
|
16
13
|
|
|
17
14
|
## Installation
|
|
18
15
|
|
|
@@ -20,131 +17,238 @@ A powerful TypeScript library for building AI-powered applications with template
|
|
|
20
17
|
pnpm add ai-functions
|
|
21
18
|
```
|
|
22
19
|
|
|
23
|
-
##
|
|
20
|
+
## The Magic: Promise Pipelining
|
|
24
21
|
|
|
25
|
-
|
|
22
|
+
Chain operations naturally—no `await` needed until you actually need the result:
|
|
26
23
|
|
|
27
24
|
```typescript
|
|
28
|
-
|
|
25
|
+
// Destructure to get exactly what you need
|
|
26
|
+
const { qualified, score, reason } = ai`qualify this lead: ${lead}`
|
|
27
|
+
|
|
28
|
+
// Chain functions together—dependencies resolve automatically
|
|
29
|
+
const followUp = ai`write follow-up email based on: ${reason}`
|
|
30
|
+
const subject = ai`subject line for: ${followUp}`
|
|
31
|
+
|
|
32
|
+
// Only await when you need the actual value
|
|
33
|
+
if (await qualified) {
|
|
34
|
+
await sendEmail({ to: lead.email, subject: await subject, body: await followUp })
|
|
35
|
+
}
|
|
36
|
+
```
|
|
29
37
|
|
|
30
|
-
|
|
31
|
-
const text = ai`write a blog post in markdown starting with '# ${title}'`
|
|
38
|
+
## Real-World Examples
|
|
32
39
|
|
|
33
|
-
|
|
34
|
-
const summary = ai`Summarize the itinerary: ${itinerary}`
|
|
40
|
+
### Lead Qualification
|
|
35
41
|
|
|
36
|
-
|
|
37
|
-
const
|
|
38
|
-
|
|
42
|
+
```typescript
|
|
43
|
+
const { score, qualified, reasoning } = ai`
|
|
44
|
+
qualify ${lead} for our product
|
|
45
|
+
considering: ${idealCustomerProfile}
|
|
46
|
+
`
|
|
39
47
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
console.log(thing)
|
|
48
|
+
if (await qualified) {
|
|
49
|
+
await assignToSales(lead)
|
|
43
50
|
}
|
|
51
|
+
```
|
|
44
52
|
|
|
45
|
-
|
|
46
|
-
const categorizeProduct = ai.categorizeProduct({
|
|
47
|
-
productType: 'App | API | Marketplace | Platform | Packaged Service | Professional Service | Website',
|
|
48
|
-
customer: 'ideal customer profile in 3-5 words',
|
|
49
|
-
solution: 'describe the offer in 4-10 words',
|
|
50
|
-
description: 'website meta description',
|
|
51
|
-
})
|
|
53
|
+
### Content Marketing
|
|
52
54
|
|
|
53
|
-
|
|
55
|
+
```typescript
|
|
56
|
+
// Generate topic ideas for your audience
|
|
57
|
+
const topics = list`content ideas for ${persona} in ${industry}`
|
|
58
|
+
|
|
59
|
+
// Evaluate each in parallel—single LLM call!
|
|
60
|
+
const evaluated = await topics.map(topic => ({
|
|
61
|
+
topic,
|
|
62
|
+
potential: is`${topic} would drive signups?`,
|
|
63
|
+
difficulty: ai`content difficulty for: ${topic}`,
|
|
64
|
+
}))
|
|
65
|
+
|
|
66
|
+
// Pick the best
|
|
67
|
+
const winner = evaluated.find(t => t.potential && t.difficulty === 'easy')
|
|
54
68
|
```
|
|
55
69
|
|
|
56
|
-
###
|
|
70
|
+
### Sales Intelligence
|
|
57
71
|
|
|
58
|
-
|
|
72
|
+
```typescript
|
|
73
|
+
const { pros, cons, objections } = lists`
|
|
74
|
+
competitive analysis: ${ourProduct} vs ${competitor}
|
|
75
|
+
`
|
|
76
|
+
|
|
77
|
+
const battleCard = ai`
|
|
78
|
+
sales battlecard addressing: ${objections}
|
|
79
|
+
highlighting: ${pros}
|
|
80
|
+
`
|
|
81
|
+
```
|
|
59
82
|
|
|
60
|
-
|
|
83
|
+
### Customer Success
|
|
61
84
|
|
|
62
85
|
```typescript
|
|
63
|
-
|
|
86
|
+
// Analyze customer health
|
|
87
|
+
const { healthy, churnRisk, opportunities } = ai`
|
|
88
|
+
analyze customer health for ${customer}
|
|
89
|
+
based on: ${usageData}
|
|
90
|
+
`
|
|
91
|
+
|
|
92
|
+
if (await churnRisk) {
|
|
93
|
+
const outreach = ai`retention outreach for ${customer} addressing ${churnRisk}`
|
|
94
|
+
await scheduleCall(customer, await outreach)
|
|
95
|
+
}
|
|
64
96
|
```
|
|
65
97
|
|
|
66
|
-
|
|
98
|
+
### Recruiting
|
|
67
99
|
|
|
68
100
|
```typescript
|
|
69
|
-
|
|
101
|
+
const candidates = list`source ${role} candidates from ${jobBoards}`
|
|
102
|
+
|
|
103
|
+
const evaluated = await candidates.map(candidate => ({
|
|
104
|
+
candidate,
|
|
105
|
+
fit: is`${candidate} matches ${requirements}?`,
|
|
106
|
+
summary: ai`one-line summary of ${candidate}`,
|
|
107
|
+
}))
|
|
70
108
|
|
|
71
|
-
const
|
|
109
|
+
const shortlist = evaluated.filter(c => c.fit)
|
|
72
110
|
```
|
|
73
111
|
|
|
74
|
-
|
|
112
|
+
## API Reference
|
|
75
113
|
|
|
76
|
-
|
|
114
|
+
### Generation
|
|
77
115
|
|
|
78
116
|
```typescript
|
|
79
|
-
|
|
117
|
+
ai`anything you need` // flexible object/text
|
|
118
|
+
write`blog post about ${topic}` // long-form content
|
|
119
|
+
summarize`${document}` // condense to key points
|
|
120
|
+
list`ideas for ${topic}` // array of items
|
|
121
|
+
lists`pros and cons of ${topic}` // named lists
|
|
122
|
+
extract`emails from ${text}` // structured extraction
|
|
80
123
|
```
|
|
81
124
|
|
|
82
|
-
###
|
|
125
|
+
### Classification
|
|
83
126
|
|
|
84
127
|
```typescript
|
|
85
|
-
|
|
86
|
-
|
|
128
|
+
is`${email} spam?` // boolean
|
|
129
|
+
decide`which converts better?`(optionA, optionB) // pick best
|
|
130
|
+
```
|
|
87
131
|
|
|
88
|
-
|
|
89
|
-
for await (const title of listBlogPosts(count, topic)) {
|
|
90
|
-
const content = await writeBlogPost(title)
|
|
91
|
-
yield { title, content }
|
|
92
|
-
}
|
|
93
|
-
}
|
|
132
|
+
### Code & Visuals
|
|
94
133
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
}
|
|
134
|
+
```typescript
|
|
135
|
+
code`email validation function` // generate code
|
|
136
|
+
diagram`user flow for ${feature}` // mermaid diagrams
|
|
137
|
+
slides`pitch deck for ${startup}` // presentations
|
|
138
|
+
image`hero image for ${brand}` // image generation
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
### Research & Web
|
|
142
|
+
|
|
143
|
+
```typescript
|
|
144
|
+
research`${competitor} market position` // web research
|
|
145
|
+
read`${url}` // url to markdown
|
|
146
|
+
browse`${url}` // browser automation
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
### Human-in-the-Loop
|
|
150
|
+
|
|
151
|
+
```typescript
|
|
152
|
+
ask`what's the priority for ${feature}?` // free-form input
|
|
153
|
+
approve`deploy ${version} to production?` // yes/no approval
|
|
154
|
+
review`${document}` // detailed feedback
|
|
98
155
|
```
|
|
99
156
|
|
|
100
|
-
|
|
157
|
+
## The `lists` Function
|
|
101
158
|
|
|
102
|
-
|
|
159
|
+
Get exactly what you ask for through destructuring:
|
|
103
160
|
|
|
104
161
|
```typescript
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
}
|
|
162
|
+
// Just name what you want—the schema is inferred!
|
|
163
|
+
const { pros, cons } = lists`pros and cons of ${decision}`
|
|
164
|
+
const { strengths, weaknesses, opportunities, threats } = lists`SWOT for ${company}`
|
|
165
|
+
const { mustHave, niceToHave, outOfScope } = lists`requirements for ${feature}`
|
|
108
166
|
```
|
|
109
167
|
|
|
110
|
-
##
|
|
168
|
+
## Batch Processing with `.map()`
|
|
111
169
|
|
|
112
|
-
|
|
113
|
-
# Install dependencies
|
|
114
|
-
pnpm install
|
|
170
|
+
Process arrays in a single LLM call:
|
|
115
171
|
|
|
116
|
-
|
|
117
|
-
|
|
172
|
+
```typescript
|
|
173
|
+
const leads = await list`leads from ${campaign}`
|
|
174
|
+
|
|
175
|
+
// Each field evaluated for each lead—all in ONE call
|
|
176
|
+
const qualified = await leads.map(lead => ({
|
|
177
|
+
lead,
|
|
178
|
+
score: ai`score 1-100: ${lead}`,
|
|
179
|
+
qualified: is`${lead} matches ${icp}?`,
|
|
180
|
+
nextStep: ai`recommended action for ${lead}`,
|
|
181
|
+
}))
|
|
182
|
+
|
|
183
|
+
// Filter and act
|
|
184
|
+
qualified
|
|
185
|
+
.filter(l => l.qualified)
|
|
186
|
+
.forEach(l => createTask(l.nextStep))
|
|
187
|
+
```
|
|
118
188
|
|
|
119
|
-
|
|
120
|
-
pnpm test:watch
|
|
189
|
+
## Typed Schemas with `AI()`
|
|
121
190
|
|
|
122
|
-
|
|
123
|
-
pnpm build
|
|
191
|
+
For reusable, typed functions:
|
|
124
192
|
|
|
125
|
-
|
|
126
|
-
|
|
193
|
+
```typescript
|
|
194
|
+
const ai = AI({
|
|
195
|
+
qualifyLead: {
|
|
196
|
+
score: 'Lead score 1-100 (number)',
|
|
197
|
+
qualified: 'Whether to pursue (boolean)',
|
|
198
|
+
reasoning: 'Explanation of score',
|
|
199
|
+
nextSteps: ['Recommended actions'],
|
|
200
|
+
},
|
|
201
|
+
|
|
202
|
+
analyzeCompetitor: {
|
|
203
|
+
positioning: 'How they position themselves',
|
|
204
|
+
strengths: ['Their advantages'],
|
|
205
|
+
weaknesses: ['Their disadvantages'],
|
|
206
|
+
battleCard: 'Key talking points for sales',
|
|
207
|
+
},
|
|
208
|
+
})
|
|
127
209
|
|
|
128
|
-
|
|
129
|
-
|
|
210
|
+
// Fully typed!
|
|
211
|
+
const result = await ai.qualifyLead('Enterprise CTO interested in AI automation')
|
|
212
|
+
result.score // number
|
|
213
|
+
result.qualified // boolean
|
|
214
|
+
result.nextSteps // string[]
|
|
130
215
|
```
|
|
131
216
|
|
|
132
|
-
##
|
|
217
|
+
## Schema Syntax
|
|
218
|
+
|
|
219
|
+
| Syntax | Type | Example |
|
|
220
|
+
|--------|------|---------|
|
|
221
|
+
| `'description'` | string | `name: 'Company name'` |
|
|
222
|
+
| `'desc (number)'` | number | `score: 'Score 1-100 (number)'` |
|
|
223
|
+
| `'desc (boolean)'` | boolean | `qualified: 'Pursue? (boolean)'` |
|
|
224
|
+
| `'opt1 \| opt2'` | enum | `priority: 'high \| medium \| low'` |
|
|
225
|
+
| `['description']` | array | `steps: ['Action items']` |
|
|
226
|
+
| `{ nested }` | object | `contact: { name, email }` |
|
|
227
|
+
|
|
228
|
+
## Philosophy
|
|
133
229
|
|
|
134
|
-
|
|
230
|
+
**Code should read like conversation.**
|
|
135
231
|
|
|
136
|
-
|
|
232
|
+
Compare:
|
|
233
|
+
```typescript
|
|
234
|
+
// Traditional AI code
|
|
235
|
+
const response = await openai.chat.completions.create({
|
|
236
|
+
model: "gpt-4",
|
|
237
|
+
messages: [{ role: "user", content: `Analyze this lead: ${JSON.stringify(lead)}` }],
|
|
238
|
+
response_format: { type: "json_object" },
|
|
239
|
+
})
|
|
240
|
+
const result = JSON.parse(response.choices[0].message.content)
|
|
241
|
+
```
|
|
137
242
|
|
|
138
|
-
|
|
243
|
+
```typescript
|
|
244
|
+
// ai-functions
|
|
245
|
+
const { qualified, score, nextStep } = ai`analyze lead: ${lead}`
|
|
246
|
+
```
|
|
139
247
|
|
|
140
|
-
|
|
248
|
+
The second version is what you'd say to a colleague. That's the goal.
|
|
141
249
|
|
|
142
|
-
|
|
250
|
+
## Related Packages
|
|
143
251
|
|
|
144
|
-
- `ai`
|
|
145
|
-
-
|
|
146
|
-
-
|
|
147
|
-
- Zod for schema validation
|
|
148
|
-
- Vitest for testing
|
|
149
|
-
- ESLint for linting
|
|
150
|
-
- Prettier for code formatting
|
|
252
|
+
- [`ai-database`](../ai-database) — AI-powered database operations
|
|
253
|
+
- [`ai-providers`](../ai-providers) — Model provider abstraction
|
|
254
|
+
- [`language-models`](../language-models) — Model definitions
|
package/TODO.md
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
# ai-functions TODO
|
|
2
|
+
|
|
3
|
+
Changes from README that need implementation in tests/code.
|
|
4
|
+
|
|
5
|
+
## Function Types System
|
|
6
|
+
|
|
7
|
+
- [ ] Implement 4 function types: `Generative`, `Code`, `Agentic`, `Human`
|
|
8
|
+
- [ ] Type inference from function name + argument names + argument values
|
|
9
|
+
- [ ] Subjective judgment (e.g., `processRefund({ amount: 12.99 })` vs `amount: 50000`)
|
|
10
|
+
|
|
11
|
+
## Core Architecture
|
|
12
|
+
|
|
13
|
+
- [ ] `generate(type, prompt, opts?)` as the core primitive
|
|
14
|
+
- [ ] Types: `json`, `text`, `code`, `list`, `extract`, `summary`, `diagram`, `slides`, `markdown`, `yaml`, `xml`
|
|
15
|
+
- [ ] Schema support for `json` type
|
|
16
|
+
- [ ] All other functions call `generate` under the hood
|
|
17
|
+
- [ ] `define(name, schema)` - foundation function
|
|
18
|
+
- [ ] Infers function type from name + args
|
|
19
|
+
- [ ] Creates appropriate function (Generative, Code, Agentic, Human)
|
|
20
|
+
- [ ] Caches definition for subsequent calls
|
|
21
|
+
- [ ] `AI({ schemas })` factory for typed instances
|
|
22
|
+
|
|
23
|
+
## Tagged Template Support
|
|
24
|
+
|
|
25
|
+
- [ ] Every function supports tagged template syntax
|
|
26
|
+
- [ ] Objects/arrays in templates auto-stringify to YAML
|
|
27
|
+
- [ ] Template + options chaining: ``fn`prompt`({ model: '...' })``
|
|
28
|
+
|
|
29
|
+
## Standalone Exports
|
|
30
|
+
|
|
31
|
+
All functions exported directly (not just on `ai` object):
|
|
32
|
+
|
|
33
|
+
- [ ] `ai` - text generation
|
|
34
|
+
- [ ] `summarize` - condense text
|
|
35
|
+
- [ ] `do` - single-pass task executor with tools (not agentic loop)
|
|
36
|
+
- [ ] `is` - boolean classification
|
|
37
|
+
- [ ] `list` - generate list
|
|
38
|
+
- [ ] `lists` - multiple named lists
|
|
39
|
+
- [ ] `extract` - extract from text
|
|
40
|
+
- [ ] `write` - generate content
|
|
41
|
+
- [ ] `generate` - core primitive
|
|
42
|
+
- [ ] `decide` - LLM as judge, picks from options
|
|
43
|
+
- [ ] `code` - generate code
|
|
44
|
+
- [ ] `diagram` - generate diagrams (mermaid, etc.)
|
|
45
|
+
- [ ] `slides` - generate presentations (slidev, marp, reveal.js)
|
|
46
|
+
- [ ] `image` - generate images
|
|
47
|
+
- [ ] `video` - generate videos
|
|
48
|
+
- [ ] `research` - agentic research
|
|
49
|
+
- [ ] `read` - URL to markdown (Firecrawl)
|
|
50
|
+
- [ ] `browse` - browser automation (Stagehand/Browserbase)
|
|
51
|
+
- [ ] `page.extract` - extract data from page
|
|
52
|
+
- [ ] `page.do` - perform action on page
|
|
53
|
+
|
|
54
|
+
## Magic Proxy (`ai.*`)
|
|
55
|
+
|
|
56
|
+
- [ ] `ai.anyFunctionName(args)` auto-defines on first call
|
|
57
|
+
- [ ] Infers function type from name + args
|
|
58
|
+
- [ ] Examples:
|
|
59
|
+
- [ ] `ai.fizzBuzz()` → CodeFunction
|
|
60
|
+
- [ ] `ai.storyBrand()` → GenerativeFunction with schema
|
|
61
|
+
- [ ] `ai.launchProductHunt()` → AgenticFunction
|
|
62
|
+
- [ ] `ai.approveExpense()` → HumanFunction
|
|
63
|
+
|
|
64
|
+
## Human Functions
|
|
65
|
+
|
|
66
|
+
- [ ] `ask` - ask human a question
|
|
67
|
+
- [ ] `approve` - request human approval
|
|
68
|
+
- [ ] `review` - request human review
|
|
69
|
+
- [ ] Integration with `human-in-the-loop`, `digital-workers`, `autonomous-agents` packages
|
|
70
|
+
|
|
71
|
+
## Async Iterators
|
|
72
|
+
|
|
73
|
+
- [ ] `list` returns `AsyncIterable` for streaming
|
|
74
|
+
- [ ] `extract` returns `AsyncIterable` for streaming
|
|
75
|
+
- [ ] `for await (const item of list`...`)` pattern
|
|
76
|
+
- [ ] Early termination with `break`
|
|
77
|
+
|
|
78
|
+
## `decide` - LLM as Judge
|
|
79
|
+
|
|
80
|
+
- [ ] Syntax: ``decide`criteria`(optionA, optionB, ...)``
|
|
81
|
+
- [ ] Returns the winning option (same type as inputs)
|
|
82
|
+
- [ ] Type-safe: passing `Product` objects returns `Product`
|
|
83
|
+
|
|
84
|
+
## Options Parameter
|
|
85
|
+
|
|
86
|
+
Every function accepts options as last parameter:
|
|
87
|
+
|
|
88
|
+
- [ ] `model` - model selection
|
|
89
|
+
- [ ] `thinking` - `'low' | 'medium' | 'high' | number` (token budget)
|
|
90
|
+
- [ ] `temperature`
|
|
91
|
+
- [ ] `maxTokens`
|
|
92
|
+
|
|
93
|
+
## Batch & Background Processing
|
|
94
|
+
|
|
95
|
+
- [ ] Background mode: `{ mode: 'background' }` returns job immediately
|
|
96
|
+
- [ ] `job.result()` to get result later
|
|
97
|
+
- [ ] `job.status` for status
|
|
98
|
+
- [ ] Batch mode: `fn.batch([contexts])`
|
|
99
|
+
- [ ] Better pricing (50% off on OpenAI)
|
|
100
|
+
- [ ] Processes all at once
|
|
101
|
+
- [ ] Batch streaming: `fn.batch.stream([contexts])`
|
|
102
|
+
- [ ] `for await` as results complete
|
|
103
|
+
|
|
104
|
+
## Integration with Other Packages
|
|
105
|
+
|
|
106
|
+
- [ ] `ai-sandbox` - `evaluate({ code, tests, module, script })` for CodeFunctions
|
|
107
|
+
- [ ] `ai-database` - `embed` moved there, used by `db.search()`
|
|
108
|
+
- [ ] `ai-providers` / `language-models` - model resolution
|
|
109
|
+
- [ ] `human-in-the-loop` - Human function implementation
|
|
110
|
+
- [ ] `autonomous-agents` - Agent loop (vs single-pass `do`)
|
|
111
|
+
|
|
112
|
+
## Type System
|
|
113
|
+
|
|
114
|
+
- [ ] `AIFunction<TArgs, TReturn>` - callable + tagged template
|
|
115
|
+
- [ ] Schema syntax parsing:
|
|
116
|
+
- [ ] `'description'` → string
|
|
117
|
+
- [ ] `'desc (number)'` → number
|
|
118
|
+
- [ ] `'desc (boolean)'` → boolean
|
|
119
|
+
- [ ] `'opt1 | opt2'` → enum
|
|
120
|
+
- [ ] `['description']` → array
|
|
121
|
+
- [ ] `{ nested }` → object
|
|
122
|
+
- [ ] Type inference for `AI()` factory schemas
|
|
123
|
+
|
|
124
|
+
## Tests to Write
|
|
125
|
+
|
|
126
|
+
- [ ] Tagged template parsing and YAML conversion
|
|
127
|
+
- [ ] Each function type (Generative, Code, Agentic, Human)
|
|
128
|
+
- [ ] Function type inference from name + args
|
|
129
|
+
- [ ] `generate` with all type options
|
|
130
|
+
- [ ] `define` creating correct function types
|
|
131
|
+
- [ ] Magic proxy auto-definition
|
|
132
|
+
- [ ] Async iterators on `list` and `extract`
|
|
133
|
+
- [ ] `decide` returning correct option
|
|
134
|
+
- [ ] Batch and background modes
|
|
135
|
+
- [ ] Options parameter (model, thinking, etc.)
|
|
136
|
+
- [ ] `browse` with `page.extract` and `page.do`
|
|
137
|
+
- [ ] `read` URL fetching
|
|
138
|
+
- [ ] `research` agentic flow
|