dotdo 0.0.1 → 0.1.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.
- package/LICENSE +1 -1
- package/README.md +446 -315
- package/cli/README.md +238 -0
- package/cli/agent.ts +72 -0
- package/cli/bin.js +44 -0
- package/cli/bin.ts +38 -0
- package/cli/build.ts +157 -0
- package/cli/commands/auth/login.ts +14 -0
- package/cli/commands/auth/logout.ts +6 -0
- package/cli/commands/auth/whoami.ts +16 -0
- package/cli/commands/deploy-multi.ts +245 -0
- package/cli/commands/dev/deploy.ts +100 -0
- package/cli/commands/dev/dev.ts +95 -0
- package/cli/commands/dev/logs.ts +91 -0
- package/cli/commands/dev-local.ts +88 -0
- package/cli/commands/do-ops.ts +314 -0
- package/cli/commands/index.ts +100 -0
- package/cli/commands/init.ts +247 -0
- package/cli/commands/introspect/emitter.ts +315 -0
- package/cli/commands/introspect/index.ts +193 -0
- package/cli/commands/link.ts +598 -0
- package/cli/commands/snippets.ts +415 -0
- package/cli/commands/tunnel.ts +239 -0
- package/cli/device-auth.ts +289 -0
- package/cli/fallback.ts +12 -0
- package/cli/index.ts +121 -0
- package/cli/main.ts +246 -0
- package/cli/mcp-stdio.ts +790 -0
- package/cli/package.json +62 -0
- package/cli/runtime/do-registry.ts +193 -0
- package/cli/runtime/embedded-db.ts +344 -0
- package/cli/runtime/index.ts +9 -0
- package/cli/runtime/miniflare-adapter.ts +162 -0
- package/cli/sandbox.ts +82 -0
- package/cli/src/args.ts +174 -0
- package/cli/src/auth.ts +55 -0
- package/cli/src/commands/call.ts +84 -0
- package/cli/src/commands/charge.ts +96 -0
- package/cli/src/commands/config.ts +115 -0
- package/cli/src/commands/email.ts +112 -0
- package/cli/src/commands/llm.ts +115 -0
- package/cli/src/commands/queue.ts +134 -0
- package/cli/src/commands/text.ts +86 -0
- package/cli/src/config.ts +185 -0
- package/cli/src/output.ts +246 -0
- package/cli/src/rpc.ts +192 -0
- package/cli/utils/config.ts +282 -0
- package/cli/utils/detect.ts +73 -0
- package/cli/utils/index.ts +15 -0
- package/cli/utils/logger.ts +232 -0
- package/dist/ai/index.js +19 -0
- package/dist/ai/index.js.map +1 -0
- package/dist/ai/template-literals.js +852 -0
- package/dist/ai/template-literals.js.map +1 -0
- package/dist/api/middleware/auth-federation.js +573 -0
- package/dist/api/middleware/auth-federation.js.map +1 -0
- package/dist/api/middleware/auth.js +545 -0
- package/dist/api/middleware/auth.js.map +1 -0
- package/dist/db/actions.js +212 -0
- package/dist/db/actions.js.map +1 -0
- package/dist/db/auth.js +506 -0
- package/dist/db/auth.js.map +1 -0
- package/dist/db/branches.js +65 -0
- package/dist/db/branches.js.map +1 -0
- package/dist/db/clickhouse.js +1074 -0
- package/dist/db/clickhouse.js.map +1 -0
- package/dist/db/dlq.js +39 -0
- package/dist/db/dlq.js.map +1 -0
- package/dist/db/events.js +28 -0
- package/dist/db/events.js.map +1 -0
- package/dist/db/exec.js +64 -0
- package/dist/db/exec.js.map +1 -0
- package/dist/db/files.js +85 -0
- package/dist/db/files.js.map +1 -0
- package/dist/db/flags.js +24 -0
- package/dist/db/flags.js.map +1 -0
- package/dist/db/git.js +116 -0
- package/dist/db/git.js.map +1 -0
- package/dist/db/iceberg/inverted-index.js +862 -0
- package/dist/db/iceberg/inverted-index.js.map +1 -0
- package/dist/db/iceberg/puffin.js +878 -0
- package/dist/db/iceberg/puffin.js.map +1 -0
- package/dist/db/iceberg/search-manifest.js +422 -0
- package/dist/db/iceberg/search-manifest.js.map +1 -0
- package/dist/db/iceberg/types.js +8 -0
- package/dist/db/iceberg/types.js.map +1 -0
- package/dist/db/index.js +121 -0
- package/dist/db/index.js.map +1 -0
- package/dist/db/integrations.js +368 -0
- package/dist/db/integrations.js.map +1 -0
- package/dist/db/json-indexes.js +332 -0
- package/dist/db/json-indexes.js.map +1 -0
- package/dist/db/linked-accounts.js +287 -0
- package/dist/db/linked-accounts.js.map +1 -0
- package/dist/db/nouns.js +183 -0
- package/dist/db/nouns.js.map +1 -0
- package/dist/db/objects.js +170 -0
- package/dist/db/objects.js.map +1 -0
- package/dist/db/primitives/dag-scheduler/index.js +869 -0
- package/dist/db/primitives/dag-scheduler/index.js.map +1 -0
- package/dist/db/primitives/exactly-once-context.js +237 -0
- package/dist/db/primitives/exactly-once-context.js.map +1 -0
- package/dist/db/primitives/index.js +62 -0
- package/dist/db/primitives/index.js.map +1 -0
- package/dist/db/primitives/keyed-router.js +145 -0
- package/dist/db/primitives/keyed-router.js.map +1 -0
- package/dist/db/primitives/observability.js +162 -0
- package/dist/db/primitives/observability.js.map +1 -0
- package/dist/db/primitives/schema-evolution.js +643 -0
- package/dist/db/primitives/schema-evolution.js.map +1 -0
- package/dist/db/primitives/stateful-operator/index.js +770 -0
- package/dist/db/primitives/stateful-operator/index.js.map +1 -0
- package/dist/db/primitives/temporal-store.js +306 -0
- package/dist/db/primitives/temporal-store.js.map +1 -0
- package/dist/db/primitives/typed-column-store.js +1229 -0
- package/dist/db/primitives/typed-column-store.js.map +1 -0
- package/dist/db/primitives/utils/duration.js +162 -0
- package/dist/db/primitives/utils/duration.js.map +1 -0
- package/dist/db/primitives/utils/murmur3.js +116 -0
- package/dist/db/primitives/utils/murmur3.js.map +1 -0
- package/dist/db/primitives/watermark-service.js +136 -0
- package/dist/db/primitives/watermark-service.js.map +1 -0
- package/dist/db/primitives/window-manager.js +764 -0
- package/dist/db/primitives/window-manager.js.map +1 -0
- package/dist/db/relationships.js +66 -0
- package/dist/db/relationships.js.map +1 -0
- package/dist/db/schema-minimal.js +61 -0
- package/dist/db/schema-minimal.js.map +1 -0
- package/dist/db/search.js +28 -0
- package/dist/db/search.js.map +1 -0
- package/dist/db/stores.js +1665 -0
- package/dist/db/stores.js.map +1 -0
- package/dist/db/things.js +297 -0
- package/dist/db/things.js.map +1 -0
- package/dist/db/vault.js +171 -0
- package/dist/db/vault.js.map +1 -0
- package/dist/db/verbs.js +102 -0
- package/dist/db/verbs.js.map +1 -0
- package/dist/do/base.js +48 -0
- package/dist/do/base.js.map +1 -0
- package/dist/do/tiny.js +31 -0
- package/dist/do/tiny.js.map +1 -0
- package/dist/lib/DOAuth.js +261 -0
- package/dist/lib/DOAuth.js.map +1 -0
- package/dist/lib/DODispatcher.js +72 -0
- package/dist/lib/DODispatcher.js.map +1 -0
- package/dist/lib/Modifier.js +189 -0
- package/dist/lib/Modifier.js.map +1 -0
- package/dist/lib/StateStorage.js +403 -0
- package/dist/lib/StateStorage.js.map +1 -0
- package/dist/lib/TypeRegistry.js +122 -0
- package/dist/lib/TypeRegistry.js.map +1 -0
- package/dist/lib/ai/gateway.js +247 -0
- package/dist/lib/ai/gateway.js.map +1 -0
- package/dist/lib/ai/tool-loop-agent.js +591 -0
- package/dist/lib/ai/tool-loop-agent.js.map +1 -0
- package/dist/lib/auto-wiring.js +439 -0
- package/dist/lib/auto-wiring.js.map +1 -0
- package/dist/lib/browse/browserbase.js +163 -0
- package/dist/lib/browse/browserbase.js.map +1 -0
- package/dist/lib/browse/cloudflare.js +144 -0
- package/dist/lib/browse/cloudflare.js.map +1 -0
- package/dist/lib/browse/index.js +62 -0
- package/dist/lib/browse/index.js.map +1 -0
- package/dist/lib/browse/types.js +13 -0
- package/dist/lib/browse/types.js.map +1 -0
- package/dist/lib/cache/index.js +37 -0
- package/dist/lib/cache/index.js.map +1 -0
- package/dist/lib/cache/visibility.js +638 -0
- package/dist/lib/cache/visibility.js.map +1 -0
- package/dist/lib/capabilities.js +268 -0
- package/dist/lib/capabilities.js.map +1 -0
- package/dist/lib/channels/base.js +106 -0
- package/dist/lib/channels/base.js.map +1 -0
- package/dist/lib/channels/discord.js +94 -0
- package/dist/lib/channels/discord.js.map +1 -0
- package/dist/lib/channels/email.js +204 -0
- package/dist/lib/channels/email.js.map +1 -0
- package/dist/lib/channels/index.js +90 -0
- package/dist/lib/channels/index.js.map +1 -0
- package/dist/lib/channels/mdxui-chat.js +95 -0
- package/dist/lib/channels/mdxui-chat.js.map +1 -0
- package/dist/lib/channels/slack-blockkit.js +121 -0
- package/dist/lib/channels/slack-blockkit.js.map +1 -0
- package/dist/lib/channels/types.js +7 -0
- package/dist/lib/channels/types.js.map +1 -0
- package/dist/lib/cloudflare/ai.js +654 -0
- package/dist/lib/cloudflare/ai.js.map +1 -0
- package/dist/lib/cloudflare/index.js +88 -0
- package/dist/lib/cloudflare/index.js.map +1 -0
- package/dist/lib/cloudflare/kv.js +342 -0
- package/dist/lib/cloudflare/kv.js.map +1 -0
- package/dist/lib/cloudflare/queues.js +434 -0
- package/dist/lib/cloudflare/queues.js.map +1 -0
- package/dist/lib/cloudflare/r2.js +604 -0
- package/dist/lib/cloudflare/r2.js.map +1 -0
- package/dist/lib/cloudflare/vectorize.js +494 -0
- package/dist/lib/cloudflare/vectorize.js.map +1 -0
- package/dist/lib/cloudflare/workflows.js +569 -0
- package/dist/lib/cloudflare/workflows.js.map +1 -0
- package/dist/lib/colo/caching.js +196 -0
- package/dist/lib/colo/caching.js.map +1 -0
- package/dist/lib/colo/detection.js +194 -0
- package/dist/lib/colo/detection.js.map +1 -0
- package/dist/lib/colo/external-data.js +219 -0
- package/dist/lib/colo/external-data.js.map +1 -0
- package/dist/lib/colo/globe-data.js +179 -0
- package/dist/lib/colo/globe-data.js.map +1 -0
- package/dist/lib/colo/index.js +16 -0
- package/dist/lib/colo/index.js.map +1 -0
- package/dist/lib/decorators.js +37 -0
- package/dist/lib/decorators.js.map +1 -0
- package/dist/lib/discovery.js +81 -0
- package/dist/lib/discovery.js.map +1 -0
- package/dist/lib/executors/AgenticFunctionExecutor.js +619 -0
- package/dist/lib/executors/AgenticFunctionExecutor.js.map +1 -0
- package/dist/lib/executors/BaseFunctionExecutor.js +328 -0
- package/dist/lib/executors/BaseFunctionExecutor.js.map +1 -0
- package/dist/lib/executors/CascadeExecutor.js +418 -0
- package/dist/lib/executors/CascadeExecutor.js.map +1 -0
- package/dist/lib/executors/CodeFunctionExecutor.js +904 -0
- package/dist/lib/executors/CodeFunctionExecutor.js.map +1 -0
- package/dist/lib/executors/GenerativeFunctionExecutor.js +904 -0
- package/dist/lib/executors/GenerativeFunctionExecutor.js.map +1 -0
- package/dist/lib/executors/HumanFunctionExecutor.js +884 -0
- package/dist/lib/executors/HumanFunctionExecutor.js.map +1 -0
- package/dist/lib/executors/ParallelStepExecutor.js +308 -0
- package/dist/lib/executors/ParallelStepExecutor.js.map +1 -0
- package/dist/lib/executors/types.js +12 -0
- package/dist/lib/executors/types.js.map +1 -0
- package/dist/lib/experiments.js +89 -0
- package/dist/lib/experiments.js.map +1 -0
- package/dist/lib/flags/store.js +262 -0
- package/dist/lib/flags/store.js.map +1 -0
- package/dist/lib/functions/FunctionComposition.js +467 -0
- package/dist/lib/functions/FunctionComposition.js.map +1 -0
- package/dist/lib/functions/FunctionMiddleware.js +457 -0
- package/dist/lib/functions/FunctionMiddleware.js.map +1 -0
- package/dist/lib/functions/FunctionRegistry.js +426 -0
- package/dist/lib/functions/FunctionRegistry.js.map +1 -0
- package/dist/lib/functions/createFunction.js +1048 -0
- package/dist/lib/functions/createFunction.js.map +1 -0
- package/dist/lib/humans/index.js +68 -0
- package/dist/lib/humans/index.js.map +1 -0
- package/dist/lib/humans/templates.js +117 -0
- package/dist/lib/humans/templates.js.map +1 -0
- package/dist/lib/identity.js +98 -0
- package/dist/lib/identity.js.map +1 -0
- package/dist/lib/index.js +9 -0
- package/dist/lib/index.js.map +1 -0
- package/dist/lib/logging/error-logger.js +163 -0
- package/dist/lib/logging/error-logger.js.map +1 -0
- package/dist/lib/logging/index.js +160 -0
- package/dist/lib/logging/index.js.map +1 -0
- package/dist/lib/mixins/bash.js +753 -0
- package/dist/lib/mixins/bash.js.map +1 -0
- package/dist/lib/mixins/fs.js +648 -0
- package/dist/lib/mixins/fs.js.map +1 -0
- package/dist/lib/mixins/git.js +1006 -0
- package/dist/lib/mixins/git.js.map +1 -0
- package/dist/lib/mixins/npm.js +662 -0
- package/dist/lib/mixins/npm.js.map +1 -0
- package/dist/lib/noun-id.js +278 -0
- package/dist/lib/noun-id.js.map +1 -0
- package/dist/lib/rate-limit/sliding-window.js +148 -0
- package/dist/lib/rate-limit/sliding-window.js.map +1 -0
- package/dist/lib/rate-limit.js +110 -0
- package/dist/lib/rate-limit.js.map +1 -0
- package/dist/lib/rpc/bindings.js +548 -0
- package/dist/lib/rpc/bindings.js.map +1 -0
- package/dist/lib/rpc/index.js +64 -0
- package/dist/lib/rpc/index.js.map +1 -0
- package/dist/lib/safe-stringify.js +223 -0
- package/dist/lib/safe-stringify.js.map +1 -0
- package/dist/lib/sandbox/miniflare-sandbox.js +1007 -0
- package/dist/lib/sandbox/miniflare-sandbox.js.map +1 -0
- package/dist/lib/sqids.js +110 -0
- package/dist/lib/sqids.js.map +1 -0
- package/dist/lib/sql/adapters/index.js +10 -0
- package/dist/lib/sql/adapters/index.js.map +1 -0
- package/dist/lib/sql/adapters/node-sql-parser.js +552 -0
- package/dist/lib/sql/adapters/node-sql-parser.js.map +1 -0
- package/dist/lib/sql/adapters/pgsql-parser.js +1190 -0
- package/dist/lib/sql/adapters/pgsql-parser.js.map +1 -0
- package/dist/lib/sql/index.js +277 -0
- package/dist/lib/sql/index.js.map +1 -0
- package/dist/lib/sql/types.js +56 -0
- package/dist/lib/sql/types.js.map +1 -0
- package/dist/lib/type-classifier.js +126 -0
- package/dist/lib/type-classifier.js.map +1 -0
- package/dist/lib/utils/html.js +47 -0
- package/dist/lib/utils/html.js.map +1 -0
- package/dist/lib/validation.js +48 -0
- package/dist/lib/validation.js.map +1 -0
- package/dist/lib/vault/store.js +411 -0
- package/dist/lib/vault/store.js.map +1 -0
- package/dist/metrics/hunch.js +739 -0
- package/dist/metrics/hunch.js.map +1 -0
- package/dist/objects/API.js +302 -0
- package/dist/objects/API.js.map +1 -0
- package/dist/objects/Agent.js +179 -0
- package/dist/objects/Agent.js.map +1 -0
- package/dist/objects/AgenticFunctionExecutor.js +8 -0
- package/dist/objects/AgenticFunctionExecutor.js.map +1 -0
- package/dist/objects/App.js +83 -0
- package/dist/objects/App.js.map +1 -0
- package/dist/objects/Browser.js +884 -0
- package/dist/objects/Browser.js.map +1 -0
- package/dist/objects/Business.js +107 -0
- package/dist/objects/Business.js.map +1 -0
- package/dist/objects/CLI.js +221 -0
- package/dist/objects/CLI.js.map +1 -0
- package/dist/objects/CodeFunctionExecutor.js +8 -0
- package/dist/objects/CodeFunctionExecutor.js.map +1 -0
- package/dist/objects/Collection.js +161 -0
- package/dist/objects/Collection.js.map +1 -0
- package/dist/objects/DO.js +41 -0
- package/dist/objects/DO.js.map +1 -0
- package/dist/objects/DOBase.js +2309 -0
- package/dist/objects/DOBase.js.map +1 -0
- package/dist/objects/DOCache.js +153 -0
- package/dist/objects/DOCache.js.map +1 -0
- package/dist/objects/DOFull.js +1676 -0
- package/dist/objects/DOFull.js.map +1 -0
- package/dist/objects/DOTiny.js +207 -0
- package/dist/objects/DOTiny.js.map +1 -0
- package/dist/objects/Directory.js +199 -0
- package/dist/objects/Directory.js.map +1 -0
- package/dist/objects/Entity.js +413 -0
- package/dist/objects/Entity.js.map +1 -0
- package/dist/objects/Function.js +116 -0
- package/dist/objects/Function.js.map +1 -0
- package/dist/objects/Human.js +231 -0
- package/dist/objects/Human.js.map +1 -0
- package/dist/objects/HumanFunctionExecutor.js +8 -0
- package/dist/objects/HumanFunctionExecutor.js.map +1 -0
- package/dist/objects/IcebergMetadataDO.js +938 -0
- package/dist/objects/IcebergMetadataDO.js.map +1 -0
- package/dist/objects/IntegrationsDO.js +1174 -0
- package/dist/objects/IntegrationsDO.js.map +1 -0
- package/dist/objects/ObservabilityBroadcaster.js +149 -0
- package/dist/objects/ObservabilityBroadcaster.js.map +1 -0
- package/dist/objects/Package.js +154 -0
- package/dist/objects/Package.js.map +1 -0
- package/dist/objects/Product.js +193 -0
- package/dist/objects/Product.js.map +1 -0
- package/dist/objects/SDK.js +152 -0
- package/dist/objects/SDK.js.map +1 -0
- package/dist/objects/SaaS.js +235 -0
- package/dist/objects/SaaS.js.map +1 -0
- package/dist/objects/SandboxDO.js +759 -0
- package/dist/objects/SandboxDO.js.map +1 -0
- package/dist/objects/Service.js +337 -0
- package/dist/objects/Service.js.map +1 -0
- package/dist/objects/Site.js +80 -0
- package/dist/objects/Site.js.map +1 -0
- package/dist/objects/Startup.js +479 -0
- package/dist/objects/Startup.js.map +1 -0
- package/dist/objects/ThingsDO.js +170 -0
- package/dist/objects/ThingsDO.js.map +1 -0
- package/dist/objects/VectorShardDO.js +650 -0
- package/dist/objects/VectorShardDO.js.map +1 -0
- package/dist/objects/Worker.js +144 -0
- package/dist/objects/Worker.js.map +1 -0
- package/dist/objects/Workflow.js +196 -0
- package/dist/objects/Workflow.js.map +1 -0
- package/dist/objects/WorkflowFactory.js +313 -0
- package/dist/objects/WorkflowFactory.js.map +1 -0
- package/dist/objects/WorkflowRuntime.js +863 -0
- package/dist/objects/WorkflowRuntime.js.map +1 -0
- package/dist/objects/circuit-breaker-bulkhead.js +178 -0
- package/dist/objects/circuit-breaker-bulkhead.js.map +1 -0
- package/dist/objects/createFunction.js +934 -0
- package/dist/objects/createFunction.js.map +1 -0
- package/dist/objects/index.js +80 -0
- package/dist/objects/index.js.map +1 -0
- package/dist/objects/lifecycle/Branch.js +275 -0
- package/dist/objects/lifecycle/Branch.js.map +1 -0
- package/dist/objects/lifecycle/Clone.js +1499 -0
- package/dist/objects/lifecycle/Clone.js.map +1 -0
- package/dist/objects/lifecycle/Compact.js +237 -0
- package/dist/objects/lifecycle/Compact.js.map +1 -0
- package/dist/objects/lifecycle/Promote.js +476 -0
- package/dist/objects/lifecycle/Promote.js.map +1 -0
- package/dist/objects/lifecycle/Shard.js +560 -0
- package/dist/objects/lifecycle/Shard.js.map +1 -0
- package/dist/objects/lifecycle/index.js +15 -0
- package/dist/objects/lifecycle/index.js.map +1 -0
- package/dist/objects/lifecycle/types.js +33 -0
- package/dist/objects/lifecycle/types.js.map +1 -0
- package/dist/objects/mixins/infrastructure.js +171 -0
- package/dist/objects/mixins/infrastructure.js.map +1 -0
- package/dist/objects/modules/StoresModule.js +153 -0
- package/dist/objects/modules/StoresModule.js.map +1 -0
- package/dist/objects/persistence/checkpoint-manager.js +606 -0
- package/dist/objects/persistence/checkpoint-manager.js.map +1 -0
- package/dist/objects/persistence/index.js +72 -0
- package/dist/objects/persistence/index.js.map +1 -0
- package/dist/objects/persistence/migration-runner.js +562 -0
- package/dist/objects/persistence/migration-runner.js.map +1 -0
- package/dist/objects/persistence/replication-manager.js +501 -0
- package/dist/objects/persistence/replication-manager.js.map +1 -0
- package/dist/objects/persistence/tiered-storage-manager.js +595 -0
- package/dist/objects/persistence/tiered-storage-manager.js.map +1 -0
- package/dist/objects/persistence/types.js +14 -0
- package/dist/objects/persistence/types.js.map +1 -0
- package/dist/objects/persistence/wal-manager.js +653 -0
- package/dist/objects/persistence/wal-manager.js.map +1 -0
- package/dist/objects/presets/index.js +20 -0
- package/dist/objects/presets/index.js.map +1 -0
- package/dist/objects/presets/primitives.js +188 -0
- package/dist/objects/presets/primitives.js.map +1 -0
- package/dist/objects/primitives/alarm-adapter.js +141 -0
- package/dist/objects/primitives/alarm-adapter.js.map +1 -0
- package/dist/objects/primitives/index.js +337 -0
- package/dist/objects/primitives/index.js.map +1 -0
- package/dist/objects/primitives/storage-adapter.js +182 -0
- package/dist/objects/primitives/storage-adapter.js.map +1 -0
- package/dist/objects/primitives/with-primitives.js +102 -0
- package/dist/objects/primitives/with-primitives.js.map +1 -0
- package/dist/objects/services/StoreManager.js +227 -0
- package/dist/objects/services/StoreManager.js.map +1 -0
- package/dist/objects/services/index.js +13 -0
- package/dist/objects/services/index.js.map +1 -0
- package/dist/objects/transport/auth-layer.js +1451 -0
- package/dist/objects/transport/auth-layer.js.map +1 -0
- package/dist/objects/transport/capnweb-target.js +355 -0
- package/dist/objects/transport/capnweb-target.js.map +1 -0
- package/dist/objects/transport/chain.js +441 -0
- package/dist/objects/transport/chain.js.map +1 -0
- package/dist/objects/transport/handler.js +58 -0
- package/dist/objects/transport/handler.js.map +1 -0
- package/dist/objects/transport/index.js +53 -0
- package/dist/objects/transport/index.js.map +1 -0
- package/dist/objects/transport/mcp-server.js +691 -0
- package/dist/objects/transport/mcp-server.js.map +1 -0
- package/dist/objects/transport/rest-autowire.js +1508 -0
- package/dist/objects/transport/rest-autowire.js.map +1 -0
- package/dist/objects/transport/rest-router.js +440 -0
- package/dist/objects/transport/rest-router.js.map +1 -0
- package/dist/objects/transport/rpc-server.js +1539 -0
- package/dist/objects/transport/rpc-server.js.map +1 -0
- package/dist/objects/transport/shared.js +576 -0
- package/dist/objects/transport/shared.js.map +1 -0
- package/dist/objects/transport/sync-engine.js +291 -0
- package/dist/objects/transport/sync-engine.js.map +1 -0
- package/dist/objects/transport/types.js +8 -0
- package/dist/objects/transport/types.js.map +1 -0
- package/dist/sandbox/index.js +258 -0
- package/dist/sandbox/index.js.map +1 -0
- package/dist/snippets/artifacts-config.js +241 -0
- package/dist/snippets/artifacts-config.js.map +1 -0
- package/dist/snippets/artifacts-ingest.js +832 -0
- package/dist/snippets/artifacts-ingest.js.map +1 -0
- package/dist/snippets/artifacts-serve.js +1035 -0
- package/dist/snippets/artifacts-serve.js.map +1 -0
- package/dist/snippets/artifacts-types.js +161 -0
- package/dist/snippets/artifacts-types.js.map +1 -0
- package/dist/snippets/cache-probe.js +376 -0
- package/dist/snippets/cache-probe.js.map +1 -0
- package/dist/snippets/cache.js +10 -0
- package/dist/snippets/cache.js.map +1 -0
- package/dist/snippets/events.js +469 -0
- package/dist/snippets/events.js.map +1 -0
- package/dist/snippets/index.js +7 -0
- package/dist/snippets/index.js.map +1 -0
- package/dist/snippets/proxy.js +495 -0
- package/dist/snippets/proxy.js.map +1 -0
- package/dist/snippets/search.js +1759 -0
- package/dist/snippets/search.js.map +1 -0
- package/dist/streams/index.js +30 -0
- package/dist/streams/index.js.map +1 -0
- package/dist/streams/observability.js +68 -0
- package/dist/streams/observability.js.map +1 -0
- package/dist/types/AI.js +92 -0
- package/dist/types/AI.js.map +1 -0
- package/dist/types/AIFunction.js +171 -0
- package/dist/types/AIFunction.js.map +1 -0
- package/dist/types/BrowseVerb.js +89 -0
- package/dist/types/BrowseVerb.js.map +1 -0
- package/dist/types/Browser.js +31 -0
- package/dist/types/Browser.js.map +1 -0
- package/dist/types/Chaos.js +15 -0
- package/dist/types/Chaos.js.map +1 -0
- package/dist/types/CloudflareBindings.js +109 -0
- package/dist/types/CloudflareBindings.js.map +1 -0
- package/dist/types/Collection.js +50 -0
- package/dist/types/Collection.js.map +1 -0
- package/dist/types/DO.js +2 -0
- package/dist/types/DO.js.map +1 -0
- package/dist/types/DOLocation.js +63 -0
- package/dist/types/DOLocation.js.map +1 -0
- package/dist/types/EventHandler.js +57 -0
- package/dist/types/EventHandler.js.map +1 -0
- package/dist/types/Experiment.js +33 -0
- package/dist/types/Experiment.js.map +1 -0
- package/dist/types/Flag.js +57 -0
- package/dist/types/Flag.js.map +1 -0
- package/dist/types/Lifecycle.js +13 -0
- package/dist/types/Lifecycle.js.map +1 -0
- package/dist/types/Location.js +169 -0
- package/dist/types/Location.js.map +1 -0
- package/dist/types/Noun.js +66 -0
- package/dist/types/Noun.js.map +1 -0
- package/dist/types/SessionEvent.js +194 -0
- package/dist/types/SessionEvent.js.map +1 -0
- package/dist/types/Thing.js +55 -0
- package/dist/types/Thing.js.map +1 -0
- package/dist/types/ThingDO.js +153 -0
- package/dist/types/ThingDO.js.map +1 -0
- package/dist/types/Things.js +2 -0
- package/dist/types/Things.js.map +1 -0
- package/dist/types/Verb.js +119 -0
- package/dist/types/Verb.js.map +1 -0
- package/dist/types/WorkflowContext.js +70 -0
- package/dist/types/WorkflowContext.js.map +1 -0
- package/dist/types/analytics-api.js +13 -0
- package/dist/types/analytics-api.js.map +1 -0
- package/dist/types/capabilities.js +135 -0
- package/dist/types/capabilities.js.map +1 -0
- package/dist/types/drizzle.js +12 -0
- package/dist/types/drizzle.js.map +1 -0
- package/dist/types/event.js +201 -0
- package/dist/types/event.js.map +1 -0
- package/dist/types/fn.js +12 -0
- package/dist/types/fn.js.map +1 -0
- package/dist/types/iceberg.js +48 -0
- package/dist/types/iceberg.js.map +1 -0
- package/dist/types/ids.js +170 -0
- package/dist/types/ids.js.map +1 -0
- package/dist/types/index.js +41 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/introspect.js +54 -0
- package/dist/types/introspect.js.map +1 -0
- package/dist/types/observability.js +124 -0
- package/dist/types/observability.js.map +1 -0
- package/dist/types/sync-protocol.js +175 -0
- package/dist/types/sync-protocol.js.map +1 -0
- package/dist/types/vector.js +13 -0
- package/dist/types/vector.js.map +1 -0
- package/dist/workflows/ScheduleManager.js +473 -0
- package/dist/workflows/ScheduleManager.js.map +1 -0
- package/dist/workflows/StepDOBridge.js +149 -0
- package/dist/workflows/StepDOBridge.js.map +1 -0
- package/dist/workflows/StepResultStorage.js +232 -0
- package/dist/workflows/StepResultStorage.js.map +1 -0
- package/dist/workflows/WaitForEventManager.js +461 -0
- package/dist/workflows/WaitForEventManager.js.map +1 -0
- package/dist/workflows/analyzer.js +332 -0
- package/dist/workflows/analyzer.js.map +1 -0
- package/dist/workflows/compat/activity-router.js +484 -0
- package/dist/workflows/compat/activity-router.js.map +1 -0
- package/dist/workflows/compat/backends/cloudflare-workflows.js +431 -0
- package/dist/workflows/compat/backends/cloudflare-workflows.js.map +1 -0
- package/dist/workflows/compat/backends/index.js +14 -0
- package/dist/workflows/compat/backends/index.js.map +1 -0
- package/dist/workflows/compat/errors/index.js +375 -0
- package/dist/workflows/compat/errors/index.js.map +1 -0
- package/dist/workflows/compat/index.js +79 -0
- package/dist/workflows/compat/index.js.map +1 -0
- package/dist/workflows/compat/inngest/index.js +989 -0
- package/dist/workflows/compat/inngest/index.js.map +1 -0
- package/dist/workflows/compat/qstash/index.js +1263 -0
- package/dist/workflows/compat/qstash/index.js.map +1 -0
- package/dist/workflows/compat/temporal/activities.js +739 -0
- package/dist/workflows/compat/temporal/activities.js.map +1 -0
- package/dist/workflows/compat/temporal/child-workflows.js +154 -0
- package/dist/workflows/compat/temporal/child-workflows.js.map +1 -0
- package/dist/workflows/compat/temporal/client.js +381 -0
- package/dist/workflows/compat/temporal/client.js.map +1 -0
- package/dist/workflows/compat/temporal/context.js +309 -0
- package/dist/workflows/compat/temporal/context.js.map +1 -0
- package/dist/workflows/compat/temporal/determinism.js +216 -0
- package/dist/workflows/compat/temporal/determinism.js.map +1 -0
- package/dist/workflows/compat/temporal/errors.js +128 -0
- package/dist/workflows/compat/temporal/errors.js.map +1 -0
- package/dist/workflows/compat/temporal/index.js +2464 -0
- package/dist/workflows/compat/temporal/index.js.map +1 -0
- package/dist/workflows/compat/temporal/saga.js +504 -0
- package/dist/workflows/compat/temporal/saga.js.map +1 -0
- package/dist/workflows/compat/temporal/signals.js +364 -0
- package/dist/workflows/compat/temporal/signals.js.map +1 -0
- package/dist/workflows/compat/temporal/storage.js +271 -0
- package/dist/workflows/compat/temporal/storage.js.map +1 -0
- package/dist/workflows/compat/temporal/timers.js +347 -0
- package/dist/workflows/compat/temporal/timers.js.map +1 -0
- package/dist/workflows/compat/temporal/types.js +7 -0
- package/dist/workflows/compat/temporal/types.js.map +1 -0
- package/dist/workflows/compat/temporal/unified-primitives.js +339 -0
- package/dist/workflows/compat/temporal/unified-primitives.js.map +1 -0
- package/dist/workflows/compat/trigger/index.js +468 -0
- package/dist/workflows/compat/trigger/index.js.map +1 -0
- package/dist/workflows/compat/utils/index.js +69 -0
- package/dist/workflows/compat/utils/index.js.map +1 -0
- package/dist/workflows/context/correlation-capability.js +266 -0
- package/dist/workflows/context/correlation-capability.js.map +1 -0
- package/dist/workflows/context/correlation.js +484 -0
- package/dist/workflows/context/correlation.js.map +1 -0
- package/dist/workflows/context/experiment.js +289 -0
- package/dist/workflows/context/experiment.js.map +1 -0
- package/dist/workflows/context/flag.js +244 -0
- package/dist/workflows/context/flag.js.map +1 -0
- package/dist/workflows/context/foundation.js +648 -0
- package/dist/workflows/context/foundation.js.map +1 -0
- package/dist/workflows/context/human-base.js +106 -0
- package/dist/workflows/context/human-base.js.map +1 -0
- package/dist/workflows/context/human.js +368 -0
- package/dist/workflows/context/human.js.map +1 -0
- package/dist/workflows/context/measure.js +354 -0
- package/dist/workflows/context/measure.js.map +1 -0
- package/dist/workflows/context/rate-limit.js +358 -0
- package/dist/workflows/context/rate-limit.js.map +1 -0
- package/dist/workflows/context/user.js +117 -0
- package/dist/workflows/context/user.js.map +1 -0
- package/dist/workflows/context/vault.js +360 -0
- package/dist/workflows/context/vault.js.map +1 -0
- package/dist/workflows/data/entity-events/entity-events.js +489 -0
- package/dist/workflows/data/entity-events/entity-events.js.map +1 -0
- package/dist/workflows/data/experiment/index.js +599 -0
- package/dist/workflows/data/experiment/index.js.map +1 -0
- package/dist/workflows/data/goal/context.js +558 -0
- package/dist/workflows/data/goal/context.js.map +1 -0
- package/dist/workflows/data/goal/index.js +32 -0
- package/dist/workflows/data/goal/index.js.map +1 -0
- package/dist/workflows/data/measure/index.js +840 -0
- package/dist/workflows/data/measure/index.js.map +1 -0
- package/dist/workflows/data/stream/index.js +1149 -0
- package/dist/workflows/data/stream/index.js.map +1 -0
- package/dist/workflows/data/track/context.js +883 -0
- package/dist/workflows/data/track/context.js.map +1 -0
- package/dist/workflows/data/track/index.js +15 -0
- package/dist/workflows/data/track/index.js.map +1 -0
- package/dist/workflows/data/view/context.js +864 -0
- package/dist/workflows/data/view/context.js.map +1 -0
- package/dist/workflows/domain.js +93 -0
- package/dist/workflows/domain.js.map +1 -0
- package/dist/workflows/flag.js +176 -0
- package/dist/workflows/flag.js.map +1 -0
- package/dist/workflows/flags.js +217 -0
- package/dist/workflows/flags.js.map +1 -0
- package/dist/workflows/hash.js +209 -0
- package/dist/workflows/hash.js.map +1 -0
- package/dist/workflows/index.js +50 -0
- package/dist/workflows/index.js.map +1 -0
- package/dist/workflows/on.js +378 -0
- package/dist/workflows/on.js.map +1 -0
- package/dist/workflows/pipeline-promise.js +481 -0
- package/dist/workflows/pipeline-promise.js.map +1 -0
- package/dist/workflows/pipeline-types.js +20 -0
- package/dist/workflows/pipeline-types.js.map +1 -0
- package/dist/workflows/proxy.js +76 -0
- package/dist/workflows/proxy.js.map +1 -0
- package/dist/workflows/runtime.js +310 -0
- package/dist/workflows/runtime.js.map +1 -0
- package/dist/workflows/schedule-builder.js +327 -0
- package/dist/workflows/schedule-builder.js.map +1 -0
- package/dist/workflows/visibility/index.js +146 -0
- package/dist/workflows/visibility/index.js.map +1 -0
- package/dist/workflows/visibility/query-parser.js +150 -0
- package/dist/workflows/visibility/query-parser.js.map +1 -0
- package/dist/workflows/visibility/store.js +223 -0
- package/dist/workflows/visibility/store.js.map +1 -0
- package/dist/workflows/visibility/types.js +30 -0
- package/dist/workflows/visibility/types.js.map +1 -0
- package/dist/workflows/workflow.js +53 -0
- package/dist/workflows/workflow.js.map +1 -0
- package/package.json +294 -46
|
@@ -0,0 +1,1048 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* createFunction() Factory
|
|
3
|
+
*
|
|
4
|
+
* Creates function instances based on type:
|
|
5
|
+
* - CodeFunction: executes TypeScript/JavaScript code
|
|
6
|
+
* - GenerativeFunction: calls LLM for generation
|
|
7
|
+
* - AgenticFunction: orchestrates multi-step AI tasks
|
|
8
|
+
* - HumanFunction: queues tasks for human input
|
|
9
|
+
*
|
|
10
|
+
* The factory validates definitions, registers with DO, and supports composition.
|
|
11
|
+
*/
|
|
12
|
+
// ============================================================================
|
|
13
|
+
// ERROR CLASSES
|
|
14
|
+
// ============================================================================
|
|
15
|
+
export class ValidationError extends Error {
|
|
16
|
+
constructor(message) {
|
|
17
|
+
super(message);
|
|
18
|
+
this.name = 'ValidationError';
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
export class RegistrationError extends Error {
|
|
22
|
+
constructor(message) {
|
|
23
|
+
super(message);
|
|
24
|
+
this.name = 'RegistrationError';
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
export class ExecutionError extends Error {
|
|
28
|
+
constructor(message) {
|
|
29
|
+
super(message);
|
|
30
|
+
this.name = 'ExecutionError';
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
// ============================================================================
|
|
34
|
+
// VALIDATION
|
|
35
|
+
// ============================================================================
|
|
36
|
+
const VALID_NAME_PATTERN = /^[a-zA-Z][a-zA-Z0-9_]*$/;
|
|
37
|
+
const VALID_JSON_SCHEMA_TYPES = ['object', 'array', 'string', 'number', 'integer', 'boolean', 'null'];
|
|
38
|
+
const VALID_FORM_FIELD_TYPES = ['text', 'number', 'boolean', 'select', 'multiselect'];
|
|
39
|
+
function validateBaseFunctionDefinition(def) {
|
|
40
|
+
if (!def.name) {
|
|
41
|
+
throw new ValidationError('Function name is required');
|
|
42
|
+
}
|
|
43
|
+
if (typeof def.name !== 'string' || def.name.trim() === '') {
|
|
44
|
+
throw new ValidationError('Function name must be a non-empty string');
|
|
45
|
+
}
|
|
46
|
+
if (!VALID_NAME_PATTERN.test(def.name)) {
|
|
47
|
+
throw new ValidationError(`Function name "${def.name}" contains invalid characters. Names must start with a letter and contain only letters, numbers, and underscores.`);
|
|
48
|
+
}
|
|
49
|
+
if (!def.type) {
|
|
50
|
+
throw new ValidationError('Function type is required');
|
|
51
|
+
}
|
|
52
|
+
if (!['code', 'generative', 'agentic', 'human'].includes(def.type)) {
|
|
53
|
+
throw new ValidationError(`Invalid function type: ${def.type}. Must be one of: code, generative, agentic, human`);
|
|
54
|
+
}
|
|
55
|
+
if (def.timeout !== undefined && def.timeout < 0) {
|
|
56
|
+
throw new ValidationError('Timeout must be a non-negative number');
|
|
57
|
+
}
|
|
58
|
+
if (def.retries !== undefined && def.retries < 0) {
|
|
59
|
+
throw new ValidationError('Retries must be a non-negative number');
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
function validateCodeFunctionDefinition(def) {
|
|
63
|
+
validateBaseFunctionDefinition(def);
|
|
64
|
+
if (!def.handler) {
|
|
65
|
+
throw new ValidationError('CodeFunction requires a handler');
|
|
66
|
+
}
|
|
67
|
+
if (typeof def.handler !== 'function') {
|
|
68
|
+
throw new ValidationError('Handler must be a function');
|
|
69
|
+
}
|
|
70
|
+
if (def.runtime !== undefined && !['javascript', 'typescript'].includes(def.runtime)) {
|
|
71
|
+
throw new ValidationError(`Invalid runtime: ${def.runtime}. Must be 'javascript' or 'typescript'`);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
function validateGenerativeFunctionDefinition(def) {
|
|
75
|
+
validateBaseFunctionDefinition(def);
|
|
76
|
+
if (!def.model) {
|
|
77
|
+
throw new ValidationError('GenerativeFunction requires a model');
|
|
78
|
+
}
|
|
79
|
+
if (typeof def.model !== 'string' || def.model.trim() === '') {
|
|
80
|
+
throw new ValidationError('Model must be a non-empty string');
|
|
81
|
+
}
|
|
82
|
+
if (def.prompt === undefined || def.prompt === null) {
|
|
83
|
+
throw new ValidationError('GenerativeFunction requires a prompt');
|
|
84
|
+
}
|
|
85
|
+
if (def.temperature !== undefined && (def.temperature < 0 || def.temperature > 2)) {
|
|
86
|
+
throw new ValidationError('Temperature must be between 0 and 2');
|
|
87
|
+
}
|
|
88
|
+
if (def.maxTokens !== undefined && def.maxTokens < 0) {
|
|
89
|
+
throw new ValidationError('maxTokens must be a non-negative number');
|
|
90
|
+
}
|
|
91
|
+
if (def.schema !== undefined) {
|
|
92
|
+
const schemaType = def.schema.type;
|
|
93
|
+
if (schemaType && !VALID_JSON_SCHEMA_TYPES.includes(schemaType)) {
|
|
94
|
+
throw new ValidationError(`Invalid JSON Schema type: ${schemaType}`);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
function validateAgenticFunctionDefinition(def) {
|
|
99
|
+
validateBaseFunctionDefinition(def);
|
|
100
|
+
if (!def.agent) {
|
|
101
|
+
throw new ValidationError('AgenticFunction requires an agent');
|
|
102
|
+
}
|
|
103
|
+
if (typeof def.agent !== 'string' || def.agent.trim() === '') {
|
|
104
|
+
throw new ValidationError('Agent must be a non-empty string');
|
|
105
|
+
}
|
|
106
|
+
if (def.maxIterations !== undefined) {
|
|
107
|
+
if (def.maxIterations < 1) {
|
|
108
|
+
throw new ValidationError('maxIterations must be at least 1');
|
|
109
|
+
}
|
|
110
|
+
if (def.maxIterations > 1000) {
|
|
111
|
+
throw new ValidationError('maxIterations cannot exceed 1000');
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
if (def.tools !== undefined) {
|
|
115
|
+
const uniqueTools = new Set(def.tools);
|
|
116
|
+
if (uniqueTools.size !== def.tools.length) {
|
|
117
|
+
throw new ValidationError('Tools array contains duplicates');
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
function validateHumanFunctionDefinition(def) {
|
|
122
|
+
validateBaseFunctionDefinition(def);
|
|
123
|
+
if (!def.channel) {
|
|
124
|
+
throw new ValidationError('HumanFunction requires a channel');
|
|
125
|
+
}
|
|
126
|
+
if (typeof def.channel !== 'string' || def.channel.trim() === '') {
|
|
127
|
+
throw new ValidationError('Channel must be a non-empty string');
|
|
128
|
+
}
|
|
129
|
+
if (def.timeout === undefined || def.timeout === null) {
|
|
130
|
+
throw new ValidationError('HumanFunction requires a timeout');
|
|
131
|
+
}
|
|
132
|
+
if (def.timeout <= 0) {
|
|
133
|
+
throw new ValidationError('Timeout must be a positive number');
|
|
134
|
+
}
|
|
135
|
+
if (def.escalation) {
|
|
136
|
+
if (def.escalation.timeout > def.timeout) {
|
|
137
|
+
throw new ValidationError('Escalation timeout cannot exceed main timeout');
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
if (def.form) {
|
|
141
|
+
for (const field of def.form.fields) {
|
|
142
|
+
if (!field.name) {
|
|
143
|
+
throw new ValidationError('Form field must have a name');
|
|
144
|
+
}
|
|
145
|
+
if (!VALID_FORM_FIELD_TYPES.includes(field.type)) {
|
|
146
|
+
throw new ValidationError(`Invalid form field type: ${field.type}`);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
function validateFunctionDefinition(def) {
|
|
152
|
+
switch (def.type) {
|
|
153
|
+
case 'code':
|
|
154
|
+
validateCodeFunctionDefinition(def);
|
|
155
|
+
break;
|
|
156
|
+
case 'generative':
|
|
157
|
+
validateGenerativeFunctionDefinition(def);
|
|
158
|
+
break;
|
|
159
|
+
case 'agentic':
|
|
160
|
+
validateAgenticFunctionDefinition(def);
|
|
161
|
+
break;
|
|
162
|
+
case 'human':
|
|
163
|
+
validateHumanFunctionDefinition(def);
|
|
164
|
+
break;
|
|
165
|
+
default:
|
|
166
|
+
throw new ValidationError(`Unknown function type: ${def.type}`);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
// ============================================================================
|
|
170
|
+
// TEMPLATE SUBSTITUTION
|
|
171
|
+
// ============================================================================
|
|
172
|
+
function substituteTemplate(template, input) {
|
|
173
|
+
return template.replace(/\{\{(\w+)\}\}/g, (_, key) => {
|
|
174
|
+
return String(input[key] ?? '');
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
// ============================================================================
|
|
178
|
+
// JSON SCHEMA VALIDATION
|
|
179
|
+
// ============================================================================
|
|
180
|
+
function validateAgainstSchema(data, schema) {
|
|
181
|
+
if (schema.type === 'object') {
|
|
182
|
+
if (typeof data !== 'object' || data === null || Array.isArray(data)) {
|
|
183
|
+
return false;
|
|
184
|
+
}
|
|
185
|
+
const requiredFields = schema.required || [];
|
|
186
|
+
for (const field of requiredFields) {
|
|
187
|
+
if (!(field in data)) {
|
|
188
|
+
return false;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
return true;
|
|
193
|
+
}
|
|
194
|
+
// ============================================================================
|
|
195
|
+
// FORM VALIDATION
|
|
196
|
+
// ============================================================================
|
|
197
|
+
function validateFormResponse(response, form) {
|
|
198
|
+
for (const field of form.fields) {
|
|
199
|
+
const value = response[field.name];
|
|
200
|
+
if (field.required && (value === undefined || value === null)) {
|
|
201
|
+
return false;
|
|
202
|
+
}
|
|
203
|
+
if (value !== undefined && value !== null) {
|
|
204
|
+
switch (field.type) {
|
|
205
|
+
case 'boolean':
|
|
206
|
+
if (typeof value !== 'boolean')
|
|
207
|
+
return false;
|
|
208
|
+
break;
|
|
209
|
+
case 'number':
|
|
210
|
+
if (typeof value !== 'number')
|
|
211
|
+
return false;
|
|
212
|
+
break;
|
|
213
|
+
case 'text':
|
|
214
|
+
if (typeof value !== 'string')
|
|
215
|
+
return false;
|
|
216
|
+
break;
|
|
217
|
+
case 'select':
|
|
218
|
+
if (field.options && !field.options.includes(String(value)))
|
|
219
|
+
return false;
|
|
220
|
+
break;
|
|
221
|
+
case 'multiselect':
|
|
222
|
+
if (!Array.isArray(value))
|
|
223
|
+
return false;
|
|
224
|
+
if (field.options) {
|
|
225
|
+
for (const v of value) {
|
|
226
|
+
if (!field.options.includes(String(v)))
|
|
227
|
+
return false;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
break;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
if (field.validation) {
|
|
234
|
+
const result = field.validation(value);
|
|
235
|
+
if (result !== true && typeof result === 'string')
|
|
236
|
+
return false;
|
|
237
|
+
if (result === false)
|
|
238
|
+
return false;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
return true;
|
|
242
|
+
}
|
|
243
|
+
// ============================================================================
|
|
244
|
+
// FUNCTION INSTANCE CREATION
|
|
245
|
+
// ============================================================================
|
|
246
|
+
function createFunctionInstance(def, options) {
|
|
247
|
+
const id = options.id || crypto.randomUUID();
|
|
248
|
+
const createdAt = new Date();
|
|
249
|
+
let invocationCount = 0;
|
|
250
|
+
const invocationHistory = [];
|
|
251
|
+
let registeredRegistry = options.registry || null;
|
|
252
|
+
// Build base instance properties
|
|
253
|
+
const instance = {
|
|
254
|
+
id,
|
|
255
|
+
name: def.name,
|
|
256
|
+
type: def.type,
|
|
257
|
+
description: def.description,
|
|
258
|
+
createdAt,
|
|
259
|
+
timeout: def.timeout,
|
|
260
|
+
retries: def.retries,
|
|
261
|
+
requiredPermission: def.requiredPermission,
|
|
262
|
+
// Methods will be defined below
|
|
263
|
+
execute: null,
|
|
264
|
+
register: null,
|
|
265
|
+
unregister: null,
|
|
266
|
+
toJSON: null,
|
|
267
|
+
getMetadata: null,
|
|
268
|
+
getInvocationHistory: null,
|
|
269
|
+
chain: null,
|
|
270
|
+
if: null,
|
|
271
|
+
map: null,
|
|
272
|
+
contramap: null,
|
|
273
|
+
catch: null,
|
|
274
|
+
finally: null,
|
|
275
|
+
};
|
|
276
|
+
// Add type-specific properties
|
|
277
|
+
if (def.type === 'code') {
|
|
278
|
+
const codeDef = def;
|
|
279
|
+
instance.handler = codeDef.handler;
|
|
280
|
+
instance.runtime = codeDef.runtime || 'javascript';
|
|
281
|
+
instance.sandboxed = codeDef.sandboxed;
|
|
282
|
+
}
|
|
283
|
+
else if (def.type === 'generative') {
|
|
284
|
+
const genDef = def;
|
|
285
|
+
instance.model = genDef.model;
|
|
286
|
+
instance.prompt = genDef.prompt;
|
|
287
|
+
instance.temperature = genDef.temperature ?? 1;
|
|
288
|
+
instance.maxTokens = genDef.maxTokens;
|
|
289
|
+
instance.stream = genDef.stream;
|
|
290
|
+
instance.schema = genDef.schema;
|
|
291
|
+
}
|
|
292
|
+
else if (def.type === 'agentic') {
|
|
293
|
+
const agentDef = def;
|
|
294
|
+
instance.agent = agentDef.agent;
|
|
295
|
+
instance.tools = agentDef.tools;
|
|
296
|
+
instance.maxIterations = agentDef.maxIterations ?? 10;
|
|
297
|
+
instance.systemPrompt = agentDef.systemPrompt;
|
|
298
|
+
instance.onStep = agentDef.onStep;
|
|
299
|
+
}
|
|
300
|
+
else if (def.type === 'human') {
|
|
301
|
+
const humanDef = def;
|
|
302
|
+
instance.channel = humanDef.channel;
|
|
303
|
+
instance.prompt = humanDef.prompt;
|
|
304
|
+
instance.timeout = humanDef.timeout;
|
|
305
|
+
instance.escalation = humanDef.escalation;
|
|
306
|
+
instance.form = humanDef.form;
|
|
307
|
+
}
|
|
308
|
+
// Execute method
|
|
309
|
+
instance.execute = async (input) => {
|
|
310
|
+
const invocationId = crypto.randomUUID();
|
|
311
|
+
const startTime = Date.now();
|
|
312
|
+
// Emit invoked event
|
|
313
|
+
options.onEvent?.('function.invoked', {
|
|
314
|
+
functionId: id,
|
|
315
|
+
invocationId,
|
|
316
|
+
input,
|
|
317
|
+
});
|
|
318
|
+
const context = {
|
|
319
|
+
functionId: id,
|
|
320
|
+
invocationId,
|
|
321
|
+
input,
|
|
322
|
+
env: options.env,
|
|
323
|
+
emit: async (event, data) => {
|
|
324
|
+
options.onEvent?.(event, data);
|
|
325
|
+
},
|
|
326
|
+
log: (message, data) => {
|
|
327
|
+
console.log(`[${instance.name}] ${message}`, data);
|
|
328
|
+
},
|
|
329
|
+
};
|
|
330
|
+
try {
|
|
331
|
+
let result;
|
|
332
|
+
if (def.type === 'code') {
|
|
333
|
+
result = await executeCodeFunction(def, input, context, instance);
|
|
334
|
+
}
|
|
335
|
+
else if (def.type === 'generative') {
|
|
336
|
+
result = await executeGenerativeFunction(def, input, options.env);
|
|
337
|
+
}
|
|
338
|
+
else if (def.type === 'agentic') {
|
|
339
|
+
result = await executeAgenticFunction(def, input, options.env);
|
|
340
|
+
}
|
|
341
|
+
else if (def.type === 'human') {
|
|
342
|
+
result = await executeHumanFunction(def, input, options.env);
|
|
343
|
+
}
|
|
344
|
+
const duration = Date.now() - startTime;
|
|
345
|
+
invocationCount++;
|
|
346
|
+
// Record invocation
|
|
347
|
+
const record = {
|
|
348
|
+
id: invocationId,
|
|
349
|
+
input,
|
|
350
|
+
output: result,
|
|
351
|
+
status: 'completed',
|
|
352
|
+
startedAt: new Date(startTime),
|
|
353
|
+
completedAt: new Date(),
|
|
354
|
+
duration,
|
|
355
|
+
};
|
|
356
|
+
invocationHistory.push(record);
|
|
357
|
+
// Store in state if available
|
|
358
|
+
if (options.state) {
|
|
359
|
+
await options.state.storage.put(`invocation:${invocationId}`, record);
|
|
360
|
+
}
|
|
361
|
+
// Emit completed event
|
|
362
|
+
options.onEvent?.('function.completed', {
|
|
363
|
+
functionId: id,
|
|
364
|
+
invocationId,
|
|
365
|
+
result,
|
|
366
|
+
duration,
|
|
367
|
+
});
|
|
368
|
+
return result;
|
|
369
|
+
}
|
|
370
|
+
catch (error) {
|
|
371
|
+
const duration = Date.now() - startTime;
|
|
372
|
+
// Record failed invocation
|
|
373
|
+
const record = {
|
|
374
|
+
id: invocationId,
|
|
375
|
+
input,
|
|
376
|
+
output: undefined,
|
|
377
|
+
status: 'failed',
|
|
378
|
+
startedAt: new Date(startTime),
|
|
379
|
+
completedAt: new Date(),
|
|
380
|
+
duration,
|
|
381
|
+
};
|
|
382
|
+
invocationHistory.push(record);
|
|
383
|
+
// Emit failed event
|
|
384
|
+
options.onEvent?.('function.failed', {
|
|
385
|
+
functionId: id,
|
|
386
|
+
invocationId,
|
|
387
|
+
error: error instanceof Error ? error.message : String(error),
|
|
388
|
+
duration,
|
|
389
|
+
});
|
|
390
|
+
throw error;
|
|
391
|
+
}
|
|
392
|
+
};
|
|
393
|
+
// Register method
|
|
394
|
+
instance.register = async (registry) => {
|
|
395
|
+
await registry.register(instance);
|
|
396
|
+
registeredRegistry = registry;
|
|
397
|
+
};
|
|
398
|
+
// Unregister method
|
|
399
|
+
instance.unregister = async () => {
|
|
400
|
+
if (registeredRegistry) {
|
|
401
|
+
await registeredRegistry.unregister(instance.name);
|
|
402
|
+
}
|
|
403
|
+
if (options.state) {
|
|
404
|
+
await options.state.storage.delete(`function:${instance.name}`);
|
|
405
|
+
}
|
|
406
|
+
};
|
|
407
|
+
// toJSON method
|
|
408
|
+
instance.toJSON = () => {
|
|
409
|
+
const json = {
|
|
410
|
+
id,
|
|
411
|
+
name: instance.name,
|
|
412
|
+
type: instance.type,
|
|
413
|
+
description: instance.description,
|
|
414
|
+
createdAt: instance.createdAt.toISOString(),
|
|
415
|
+
timeout: instance.timeout,
|
|
416
|
+
retries: instance.retries,
|
|
417
|
+
requiredPermission: instance.requiredPermission,
|
|
418
|
+
};
|
|
419
|
+
// Add type-specific properties (except handler)
|
|
420
|
+
if (instance.type === 'generative') {
|
|
421
|
+
json.model = instance.model;
|
|
422
|
+
json.temperature = instance.temperature;
|
|
423
|
+
json.maxTokens = instance.maxTokens;
|
|
424
|
+
json.stream = instance.stream;
|
|
425
|
+
json.schema = instance.schema;
|
|
426
|
+
}
|
|
427
|
+
else if (instance.type === 'agentic') {
|
|
428
|
+
json.agent = instance.agent;
|
|
429
|
+
json.tools = instance.tools;
|
|
430
|
+
json.maxIterations = instance.maxIterations;
|
|
431
|
+
json.systemPrompt = instance.systemPrompt;
|
|
432
|
+
}
|
|
433
|
+
else if (instance.type === 'human') {
|
|
434
|
+
json.channel = instance.channel;
|
|
435
|
+
json.timeout = instance.timeout;
|
|
436
|
+
json.escalation = instance.escalation;
|
|
437
|
+
json.form = instance.form;
|
|
438
|
+
}
|
|
439
|
+
else if (instance.type === 'code') {
|
|
440
|
+
json.runtime = instance.runtime;
|
|
441
|
+
json.sandboxed = instance.sandboxed;
|
|
442
|
+
}
|
|
443
|
+
return json;
|
|
444
|
+
};
|
|
445
|
+
// getMetadata method
|
|
446
|
+
instance.getMetadata = () => ({
|
|
447
|
+
id,
|
|
448
|
+
name: instance.name,
|
|
449
|
+
type: instance.type,
|
|
450
|
+
createdAt,
|
|
451
|
+
invocationCount,
|
|
452
|
+
});
|
|
453
|
+
// getInvocationHistory method
|
|
454
|
+
instance.getInvocationHistory = async () => {
|
|
455
|
+
return [...invocationHistory];
|
|
456
|
+
};
|
|
457
|
+
// Composition: then
|
|
458
|
+
instance.chain = createChainMethod(instance, (next) => createComposedFunction([instance, next], options));
|
|
459
|
+
// Composition: if
|
|
460
|
+
instance.if = (predicate) => {
|
|
461
|
+
return createConditionalFunction(instance, predicate, options);
|
|
462
|
+
};
|
|
463
|
+
// Composition: map
|
|
464
|
+
instance.map = (transform) => {
|
|
465
|
+
return createMappedFunction(instance, transform, options);
|
|
466
|
+
};
|
|
467
|
+
// Composition: contramap
|
|
468
|
+
instance.contramap = (transform) => {
|
|
469
|
+
return createContramappedFunction(instance, transform, options);
|
|
470
|
+
};
|
|
471
|
+
// Composition: catch
|
|
472
|
+
instance.catch = (handler) => {
|
|
473
|
+
return createCatchFunction(instance, handler, options);
|
|
474
|
+
};
|
|
475
|
+
// Composition: finally
|
|
476
|
+
instance.finally = (handler) => {
|
|
477
|
+
return createFinallyFunction(instance, handler, options);
|
|
478
|
+
};
|
|
479
|
+
return instance;
|
|
480
|
+
}
|
|
481
|
+
// ============================================================================
|
|
482
|
+
// EXECUTION FUNCTIONS
|
|
483
|
+
// ============================================================================
|
|
484
|
+
async function executeCodeFunction(def, input, context, instance) {
|
|
485
|
+
const timeout = instance.timeout;
|
|
486
|
+
const retries = instance.retries ?? 0;
|
|
487
|
+
let lastError = null;
|
|
488
|
+
for (let attempt = 0; attempt <= retries; attempt++) {
|
|
489
|
+
try {
|
|
490
|
+
if (timeout) {
|
|
491
|
+
const result = await Promise.race([
|
|
492
|
+
Promise.resolve(def.handler(input, context)),
|
|
493
|
+
new Promise((_, reject) => setTimeout(() => reject(new ExecutionError('Function execution timeout')), timeout)),
|
|
494
|
+
]);
|
|
495
|
+
return result;
|
|
496
|
+
}
|
|
497
|
+
else {
|
|
498
|
+
return await Promise.resolve(def.handler(input, context));
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
catch (error) {
|
|
502
|
+
lastError = error instanceof Error ? error : new Error(String(error));
|
|
503
|
+
if (attempt === retries) {
|
|
504
|
+
if (lastError.message.includes('timeout')) {
|
|
505
|
+
throw lastError;
|
|
506
|
+
}
|
|
507
|
+
throw new ExecutionError(lastError.message);
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
throw new ExecutionError(lastError?.message || 'Unknown error');
|
|
512
|
+
}
|
|
513
|
+
async function executeGenerativeFunction(def, input, env) {
|
|
514
|
+
const ai = env.AI;
|
|
515
|
+
// Build prompt
|
|
516
|
+
let promptText;
|
|
517
|
+
if (typeof def.prompt === 'function') {
|
|
518
|
+
promptText = def.prompt(input);
|
|
519
|
+
}
|
|
520
|
+
else {
|
|
521
|
+
promptText = substituteTemplate(def.prompt, input);
|
|
522
|
+
}
|
|
523
|
+
// If streaming, return the stream directly
|
|
524
|
+
if (def.stream) {
|
|
525
|
+
return ai.stream({
|
|
526
|
+
model: def.model,
|
|
527
|
+
prompt: promptText,
|
|
528
|
+
temperature: def.temperature,
|
|
529
|
+
maxTokens: def.maxTokens,
|
|
530
|
+
});
|
|
531
|
+
}
|
|
532
|
+
// Generate response
|
|
533
|
+
const response = await ai.generate({
|
|
534
|
+
model: def.model,
|
|
535
|
+
prompt: promptText,
|
|
536
|
+
temperature: def.temperature,
|
|
537
|
+
maxTokens: def.maxTokens,
|
|
538
|
+
});
|
|
539
|
+
// If schema is defined, parse and validate
|
|
540
|
+
if (def.schema) {
|
|
541
|
+
try {
|
|
542
|
+
const parsed = JSON.parse(response.text);
|
|
543
|
+
if (!validateAgainstSchema(parsed, def.schema)) {
|
|
544
|
+
throw new ExecutionError('AI output does not match schema');
|
|
545
|
+
}
|
|
546
|
+
return parsed;
|
|
547
|
+
}
|
|
548
|
+
catch (error) {
|
|
549
|
+
if (error instanceof ExecutionError)
|
|
550
|
+
throw error;
|
|
551
|
+
throw new ExecutionError('Failed to parse AI output as JSON');
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
return response.text;
|
|
555
|
+
}
|
|
556
|
+
async function executeAgenticFunction(def, input, env) {
|
|
557
|
+
const agentRunner = env.AGENT_RUNNER;
|
|
558
|
+
try {
|
|
559
|
+
const result = await agentRunner.run({
|
|
560
|
+
agent: def.agent,
|
|
561
|
+
input,
|
|
562
|
+
tools: def.tools,
|
|
563
|
+
maxIterations: def.maxIterations ?? 10,
|
|
564
|
+
systemPrompt: def.systemPrompt,
|
|
565
|
+
onStep: def.onStep,
|
|
566
|
+
});
|
|
567
|
+
return result;
|
|
568
|
+
}
|
|
569
|
+
catch (error) {
|
|
570
|
+
throw new ExecutionError(error instanceof Error ? error.message : String(error));
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
async function executeHumanFunction(def, input, env) {
|
|
574
|
+
const notifications = env.NOTIFICATIONS;
|
|
575
|
+
// Build message
|
|
576
|
+
let message;
|
|
577
|
+
if (typeof def.prompt === 'function') {
|
|
578
|
+
message = def.prompt(input);
|
|
579
|
+
}
|
|
580
|
+
else if (def.prompt) {
|
|
581
|
+
message = substituteTemplate(def.prompt, input);
|
|
582
|
+
}
|
|
583
|
+
else {
|
|
584
|
+
message = JSON.stringify(input);
|
|
585
|
+
}
|
|
586
|
+
// Send notification
|
|
587
|
+
await notifications.send({
|
|
588
|
+
channel: def.channel,
|
|
589
|
+
message,
|
|
590
|
+
form: def.form,
|
|
591
|
+
});
|
|
592
|
+
try {
|
|
593
|
+
// Wait for response
|
|
594
|
+
const response = await notifications.waitForResponse({
|
|
595
|
+
timeout: def.timeout,
|
|
596
|
+
});
|
|
597
|
+
// Validate response against form if defined
|
|
598
|
+
if (def.form && !validateFormResponse(response, def.form)) {
|
|
599
|
+
throw new ExecutionError('Response does not match form schema');
|
|
600
|
+
}
|
|
601
|
+
return response;
|
|
602
|
+
}
|
|
603
|
+
catch (error) {
|
|
604
|
+
// Handle timeout with escalation
|
|
605
|
+
if (def.escalation && error instanceof Error && error.message.includes('Timeout')) {
|
|
606
|
+
// Escalate to another channel
|
|
607
|
+
await notifications.send({
|
|
608
|
+
channel: def.escalation.to,
|
|
609
|
+
message,
|
|
610
|
+
form: def.form,
|
|
611
|
+
});
|
|
612
|
+
// Wait for escalated response
|
|
613
|
+
const escalatedResponse = await notifications.waitForResponse({
|
|
614
|
+
timeout: def.escalation.timeout,
|
|
615
|
+
});
|
|
616
|
+
return escalatedResponse;
|
|
617
|
+
}
|
|
618
|
+
throw new ExecutionError(error instanceof Error ? error.message : String(error));
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
// ============================================================================
|
|
622
|
+
// THENABLE HELPER
|
|
623
|
+
// ============================================================================
|
|
624
|
+
/**
|
|
625
|
+
* Creates a 'chain' method for composition chaining.
|
|
626
|
+
* This was renamed from 'then' to avoid conflicts with Promise's thenable interface.
|
|
627
|
+
* Use chain() to compose functions together.
|
|
628
|
+
*/
|
|
629
|
+
function createChainMethod(instance, composeWith) {
|
|
630
|
+
return (next) => {
|
|
631
|
+
return composeWith(next);
|
|
632
|
+
};
|
|
633
|
+
}
|
|
634
|
+
// ============================================================================
|
|
635
|
+
// COMPOSITION FUNCTIONS
|
|
636
|
+
// ============================================================================
|
|
637
|
+
function createComposedFunction(functions, options) {
|
|
638
|
+
const name = functions.map((f) => f.name).join(' -> ');
|
|
639
|
+
const id = crypto.randomUUID();
|
|
640
|
+
const composed = {
|
|
641
|
+
id,
|
|
642
|
+
name,
|
|
643
|
+
type: 'code',
|
|
644
|
+
createdAt: new Date(),
|
|
645
|
+
execute: async (input) => {
|
|
646
|
+
let result = input;
|
|
647
|
+
for (const fn of functions) {
|
|
648
|
+
result = await fn.execute(result);
|
|
649
|
+
}
|
|
650
|
+
return result;
|
|
651
|
+
},
|
|
652
|
+
register: async (registry) => {
|
|
653
|
+
await registry.register(composed);
|
|
654
|
+
},
|
|
655
|
+
unregister: async () => {
|
|
656
|
+
// No-op for composed functions
|
|
657
|
+
},
|
|
658
|
+
toJSON: () => ({
|
|
659
|
+
id,
|
|
660
|
+
name,
|
|
661
|
+
type: 'composed',
|
|
662
|
+
steps: functions.map((f) => ({ name: f.name, type: f.type })),
|
|
663
|
+
}),
|
|
664
|
+
getMetadata: () => ({
|
|
665
|
+
id,
|
|
666
|
+
name,
|
|
667
|
+
type: 'code',
|
|
668
|
+
createdAt: new Date(),
|
|
669
|
+
invocationCount: 0,
|
|
670
|
+
}),
|
|
671
|
+
getInvocationHistory: async () => [],
|
|
672
|
+
chain: null,
|
|
673
|
+
if: (predicate) => {
|
|
674
|
+
return createConditionalFunction(composed, predicate, options);
|
|
675
|
+
},
|
|
676
|
+
map: (transform) => {
|
|
677
|
+
return createMappedFunction(composed, transform, options);
|
|
678
|
+
},
|
|
679
|
+
contramap: (transform) => {
|
|
680
|
+
return createContramappedFunction(composed, transform, options);
|
|
681
|
+
},
|
|
682
|
+
catch: (handler) => {
|
|
683
|
+
return createCatchFunction(composed, handler, options);
|
|
684
|
+
},
|
|
685
|
+
finally: (handler) => {
|
|
686
|
+
return createFinallyFunction(composed, handler, options);
|
|
687
|
+
},
|
|
688
|
+
describe: () => ({
|
|
689
|
+
type: 'pipeline',
|
|
690
|
+
steps: functions.map((f) => ({ name: f.name, type: f.type })),
|
|
691
|
+
}),
|
|
692
|
+
};
|
|
693
|
+
// Set up the thenable-safe then method
|
|
694
|
+
composed.chain = createChainMethod(composed, (next) => createComposedFunction([...functions, next], options));
|
|
695
|
+
return composed;
|
|
696
|
+
}
|
|
697
|
+
function createConditionalFunction(fn, predicate, options) {
|
|
698
|
+
let elseFn = null;
|
|
699
|
+
const conditional = {
|
|
700
|
+
id: crypto.randomUUID(),
|
|
701
|
+
name: `${fn.name} (conditional)`,
|
|
702
|
+
type: fn.type,
|
|
703
|
+
createdAt: new Date(),
|
|
704
|
+
execute: async (input) => {
|
|
705
|
+
if (predicate(input)) {
|
|
706
|
+
return fn.execute(input);
|
|
707
|
+
}
|
|
708
|
+
if (elseFn) {
|
|
709
|
+
return elseFn.execute(input);
|
|
710
|
+
}
|
|
711
|
+
return null;
|
|
712
|
+
},
|
|
713
|
+
register: async (registry) => {
|
|
714
|
+
await registry.register(conditional);
|
|
715
|
+
},
|
|
716
|
+
unregister: async () => { },
|
|
717
|
+
toJSON: () => fn.toJSON(),
|
|
718
|
+
getMetadata: () => fn.getMetadata(),
|
|
719
|
+
getInvocationHistory: async () => fn.getInvocationHistory(),
|
|
720
|
+
chain: null,
|
|
721
|
+
if: (pred) => {
|
|
722
|
+
return createConditionalFunction(conditional, pred, options);
|
|
723
|
+
},
|
|
724
|
+
map: (transform) => {
|
|
725
|
+
return createMappedFunction(conditional, transform, options);
|
|
726
|
+
},
|
|
727
|
+
contramap: (transform) => {
|
|
728
|
+
return createContramappedFunction(conditional, transform, options);
|
|
729
|
+
},
|
|
730
|
+
catch: (handler) => {
|
|
731
|
+
return createCatchFunction(conditional, handler, options);
|
|
732
|
+
},
|
|
733
|
+
finally: (handler) => {
|
|
734
|
+
return createFinallyFunction(conditional, handler, options);
|
|
735
|
+
},
|
|
736
|
+
else: (elseFunction) => {
|
|
737
|
+
elseFn = elseFunction;
|
|
738
|
+
return conditional;
|
|
739
|
+
},
|
|
740
|
+
};
|
|
741
|
+
// Set up thenable-safe then method
|
|
742
|
+
conditional.chain = createChainMethod(conditional, (next) => createComposedFunction([conditional, next], options));
|
|
743
|
+
return conditional;
|
|
744
|
+
}
|
|
745
|
+
function createMappedFunction(fn, transform, options) {
|
|
746
|
+
const mapped = {
|
|
747
|
+
id: crypto.randomUUID(),
|
|
748
|
+
name: `${fn.name} (mapped)`,
|
|
749
|
+
type: fn.type,
|
|
750
|
+
createdAt: new Date(),
|
|
751
|
+
execute: async (input) => {
|
|
752
|
+
const result = await fn.execute(input);
|
|
753
|
+
return transform(result);
|
|
754
|
+
},
|
|
755
|
+
register: async (registry) => {
|
|
756
|
+
await registry.register(mapped);
|
|
757
|
+
},
|
|
758
|
+
unregister: async () => { },
|
|
759
|
+
toJSON: () => fn.toJSON(),
|
|
760
|
+
getMetadata: () => fn.getMetadata(),
|
|
761
|
+
getInvocationHistory: async () => fn.getInvocationHistory(),
|
|
762
|
+
chain: null,
|
|
763
|
+
if: (pred) => {
|
|
764
|
+
return createConditionalFunction(mapped, pred, options);
|
|
765
|
+
},
|
|
766
|
+
map: (transform2) => {
|
|
767
|
+
return createMappedFunction(mapped, transform2, options);
|
|
768
|
+
},
|
|
769
|
+
contramap: (transform2) => {
|
|
770
|
+
return createContramappedFunction(mapped, transform2, options);
|
|
771
|
+
},
|
|
772
|
+
catch: (handler) => {
|
|
773
|
+
return createCatchFunction(mapped, handler, options);
|
|
774
|
+
},
|
|
775
|
+
finally: (handler) => {
|
|
776
|
+
return createFinallyFunction(mapped, handler, options);
|
|
777
|
+
},
|
|
778
|
+
};
|
|
779
|
+
mapped.chain = createChainMethod(mapped, (next) => createComposedFunction([mapped, next], options));
|
|
780
|
+
return mapped;
|
|
781
|
+
}
|
|
782
|
+
function createContramappedFunction(fn, transform, options) {
|
|
783
|
+
const contramapped = {
|
|
784
|
+
id: crypto.randomUUID(),
|
|
785
|
+
name: `${fn.name} (contramapped)`,
|
|
786
|
+
type: fn.type,
|
|
787
|
+
createdAt: new Date(),
|
|
788
|
+
execute: async (input) => {
|
|
789
|
+
const transformedInput = transform(input);
|
|
790
|
+
return fn.execute(transformedInput);
|
|
791
|
+
},
|
|
792
|
+
register: async (registry) => {
|
|
793
|
+
await registry.register(contramapped);
|
|
794
|
+
},
|
|
795
|
+
unregister: async () => { },
|
|
796
|
+
toJSON: () => fn.toJSON(),
|
|
797
|
+
getMetadata: () => fn.getMetadata(),
|
|
798
|
+
getInvocationHistory: async () => fn.getInvocationHistory(),
|
|
799
|
+
chain: null,
|
|
800
|
+
if: (pred) => {
|
|
801
|
+
return createConditionalFunction(contramapped, pred, options);
|
|
802
|
+
},
|
|
803
|
+
map: (transform2) => {
|
|
804
|
+
return createMappedFunction(contramapped, transform2, options);
|
|
805
|
+
},
|
|
806
|
+
contramap: (transform2) => {
|
|
807
|
+
return createContramappedFunction(contramapped, transform2, options);
|
|
808
|
+
},
|
|
809
|
+
catch: (handler) => {
|
|
810
|
+
return createCatchFunction(contramapped, handler, options);
|
|
811
|
+
},
|
|
812
|
+
finally: (handler) => {
|
|
813
|
+
return createFinallyFunction(contramapped, handler, options);
|
|
814
|
+
},
|
|
815
|
+
};
|
|
816
|
+
contramapped.chain = createChainMethod(contramapped, (next) => createComposedFunction([contramapped, next], options));
|
|
817
|
+
return contramapped;
|
|
818
|
+
}
|
|
819
|
+
function createCatchFunction(fn, handler, options) {
|
|
820
|
+
const catchFn = {
|
|
821
|
+
id: crypto.randomUUID(),
|
|
822
|
+
name: `${fn.name} (catch)`,
|
|
823
|
+
type: fn.type,
|
|
824
|
+
createdAt: new Date(),
|
|
825
|
+
execute: async (input) => {
|
|
826
|
+
try {
|
|
827
|
+
return await fn.execute(input);
|
|
828
|
+
}
|
|
829
|
+
catch (error) {
|
|
830
|
+
return handler(error instanceof Error ? error : new Error(String(error)));
|
|
831
|
+
}
|
|
832
|
+
},
|
|
833
|
+
register: async (registry) => {
|
|
834
|
+
await registry.register(catchFn);
|
|
835
|
+
},
|
|
836
|
+
unregister: async () => { },
|
|
837
|
+
toJSON: () => fn.toJSON(),
|
|
838
|
+
getMetadata: () => fn.getMetadata(),
|
|
839
|
+
getInvocationHistory: async () => fn.getInvocationHistory(),
|
|
840
|
+
chain: null,
|
|
841
|
+
if: (pred) => {
|
|
842
|
+
return createConditionalFunction(catchFn, pred, options);
|
|
843
|
+
},
|
|
844
|
+
map: (transform) => {
|
|
845
|
+
return createMappedFunction(catchFn, transform, options);
|
|
846
|
+
},
|
|
847
|
+
contramap: (transform) => {
|
|
848
|
+
return createContramappedFunction(catchFn, transform, options);
|
|
849
|
+
},
|
|
850
|
+
catch: (h) => {
|
|
851
|
+
return createCatchFunction(catchFn, h, options);
|
|
852
|
+
},
|
|
853
|
+
finally: (h) => {
|
|
854
|
+
return createFinallyFunction(catchFn, h, options);
|
|
855
|
+
},
|
|
856
|
+
};
|
|
857
|
+
catchFn.chain = createChainMethod(catchFn, (next) => createComposedFunction([catchFn, next], options));
|
|
858
|
+
return catchFn;
|
|
859
|
+
}
|
|
860
|
+
function createFinallyFunction(fn, handler, options) {
|
|
861
|
+
const finallyFn = {
|
|
862
|
+
id: crypto.randomUUID(),
|
|
863
|
+
name: `${fn.name} (finally)`,
|
|
864
|
+
type: fn.type,
|
|
865
|
+
createdAt: new Date(),
|
|
866
|
+
execute: async (input) => {
|
|
867
|
+
try {
|
|
868
|
+
return await fn.execute(input);
|
|
869
|
+
}
|
|
870
|
+
finally {
|
|
871
|
+
handler();
|
|
872
|
+
}
|
|
873
|
+
},
|
|
874
|
+
register: async (registry) => {
|
|
875
|
+
await registry.register(finallyFn);
|
|
876
|
+
},
|
|
877
|
+
unregister: async () => { },
|
|
878
|
+
toJSON: () => fn.toJSON(),
|
|
879
|
+
getMetadata: () => fn.getMetadata(),
|
|
880
|
+
getInvocationHistory: async () => fn.getInvocationHistory(),
|
|
881
|
+
chain: null,
|
|
882
|
+
if: (pred) => {
|
|
883
|
+
return createConditionalFunction(finallyFn, pred, options);
|
|
884
|
+
},
|
|
885
|
+
map: (transform) => {
|
|
886
|
+
return createMappedFunction(finallyFn, transform, options);
|
|
887
|
+
},
|
|
888
|
+
contramap: (transform) => {
|
|
889
|
+
return createContramappedFunction(finallyFn, transform, options);
|
|
890
|
+
},
|
|
891
|
+
catch: (h) => {
|
|
892
|
+
return createCatchFunction(finallyFn, h, options);
|
|
893
|
+
},
|
|
894
|
+
finally: (h) => {
|
|
895
|
+
return createFinallyFunction(finallyFn, h, options);
|
|
896
|
+
},
|
|
897
|
+
};
|
|
898
|
+
finallyFn.chain = createChainMethod(finallyFn, (next) => createComposedFunction([finallyFn, next], options));
|
|
899
|
+
return finallyFn;
|
|
900
|
+
}
|
|
901
|
+
// ============================================================================
|
|
902
|
+
// PARALLEL EXECUTION
|
|
903
|
+
// ============================================================================
|
|
904
|
+
function createParallelFunction(functions, mode) {
|
|
905
|
+
const parallel = {
|
|
906
|
+
id: crypto.randomUUID(),
|
|
907
|
+
name: `parallel(${functions.map((f) => f.name).join(', ')})`,
|
|
908
|
+
type: 'code',
|
|
909
|
+
createdAt: new Date(),
|
|
910
|
+
execute: async (input) => {
|
|
911
|
+
if (mode === 'all') {
|
|
912
|
+
return Promise.all(functions.map((fn) => fn.execute(input)));
|
|
913
|
+
}
|
|
914
|
+
else {
|
|
915
|
+
const results = await Promise.allSettled(functions.map((fn) => fn.execute(input)));
|
|
916
|
+
return results.map((r) => r.status === 'fulfilled'
|
|
917
|
+
? { status: 'fulfilled', value: r.value }
|
|
918
|
+
: { status: 'rejected', reason: r.reason });
|
|
919
|
+
}
|
|
920
|
+
},
|
|
921
|
+
register: async () => { },
|
|
922
|
+
unregister: async () => { },
|
|
923
|
+
toJSON: () => ({
|
|
924
|
+
id: parallel.id,
|
|
925
|
+
name: parallel.name,
|
|
926
|
+
type: 'parallel',
|
|
927
|
+
mode,
|
|
928
|
+
functions: functions.map((f) => f.name),
|
|
929
|
+
}),
|
|
930
|
+
getMetadata: () => ({
|
|
931
|
+
id: parallel.id,
|
|
932
|
+
name: parallel.name,
|
|
933
|
+
type: 'code',
|
|
934
|
+
createdAt: parallel.createdAt,
|
|
935
|
+
invocationCount: 0,
|
|
936
|
+
}),
|
|
937
|
+
getInvocationHistory: async () => [],
|
|
938
|
+
chain: null,
|
|
939
|
+
if: (pred) => {
|
|
940
|
+
return createConditionalFunction(parallel, pred, { env: {} });
|
|
941
|
+
},
|
|
942
|
+
map: (transform) => {
|
|
943
|
+
return createMappedFunction(parallel, transform, { env: {} });
|
|
944
|
+
},
|
|
945
|
+
contramap: (transform) => {
|
|
946
|
+
return createContramappedFunction(parallel, transform, { env: {} });
|
|
947
|
+
},
|
|
948
|
+
catch: (handler) => {
|
|
949
|
+
return createCatchFunction(parallel, handler, { env: {} });
|
|
950
|
+
},
|
|
951
|
+
finally: (handler) => {
|
|
952
|
+
return createFinallyFunction(parallel, handler, { env: {} });
|
|
953
|
+
},
|
|
954
|
+
};
|
|
955
|
+
parallel.chain = createChainMethod(parallel, (next) => createComposedFunction([parallel, next], { env: {} }));
|
|
956
|
+
return parallel;
|
|
957
|
+
}
|
|
958
|
+
// ============================================================================
|
|
959
|
+
// MAIN FACTORY FUNCTION
|
|
960
|
+
// ============================================================================
|
|
961
|
+
export async function createFunction(definition, options) {
|
|
962
|
+
// Direct implementation instead of IIFE to avoid thenable issues with FunctionInstance
|
|
963
|
+
// Validate the definition
|
|
964
|
+
validateFunctionDefinition(definition);
|
|
965
|
+
// Check if function already exists in registry
|
|
966
|
+
if (options.registry && options.autoRegister !== false) {
|
|
967
|
+
const existing = await options.registry.get(definition.name);
|
|
968
|
+
if (existing && !options.replace) {
|
|
969
|
+
throw new RegistrationError(`Function "${definition.name}" is already registered`);
|
|
970
|
+
}
|
|
971
|
+
}
|
|
972
|
+
// Create the function instance
|
|
973
|
+
const instance = createFunctionInstance(definition, options);
|
|
974
|
+
// Register with DO if provided
|
|
975
|
+
if (options.durableObject?.registerFunction) {
|
|
976
|
+
options.durableObject.registerFunction(instance);
|
|
977
|
+
}
|
|
978
|
+
// Store in state if provided
|
|
979
|
+
if (options.state) {
|
|
980
|
+
await options.state.storage.put(`function:${instance.name}`, {
|
|
981
|
+
name: instance.name,
|
|
982
|
+
type: instance.type,
|
|
983
|
+
description: instance.description,
|
|
984
|
+
createdAt: instance.createdAt.toISOString(),
|
|
985
|
+
});
|
|
986
|
+
}
|
|
987
|
+
// Auto-register with registry
|
|
988
|
+
if (options.registry && options.autoRegister !== false) {
|
|
989
|
+
if (options.replace) {
|
|
990
|
+
await options.registry.unregister(definition.name);
|
|
991
|
+
options.onEvent?.('function.replaced', {
|
|
992
|
+
name: definition.name,
|
|
993
|
+
});
|
|
994
|
+
}
|
|
995
|
+
await options.registry.register(instance);
|
|
996
|
+
options.onEvent?.('function.registered', {
|
|
997
|
+
name: definition.name,
|
|
998
|
+
type: definition.type,
|
|
999
|
+
});
|
|
1000
|
+
}
|
|
1001
|
+
return instance;
|
|
1002
|
+
}
|
|
1003
|
+
// Static methods on createFunction
|
|
1004
|
+
createFunction.all = (functions) => {
|
|
1005
|
+
return createParallelFunction(functions, 'all');
|
|
1006
|
+
};
|
|
1007
|
+
createFunction.allSettled = (functions) => {
|
|
1008
|
+
return createParallelFunction(functions, 'allSettled');
|
|
1009
|
+
};
|
|
1010
|
+
createFunction.fromStorage = async (name, options) => {
|
|
1011
|
+
if (!options.state) {
|
|
1012
|
+
throw new Error('State is required to load from storage');
|
|
1013
|
+
}
|
|
1014
|
+
const stored = (await options.state.storage.get(`function:${name}`));
|
|
1015
|
+
if (!stored) {
|
|
1016
|
+
throw new Error(`Function "${name}" not found in storage`);
|
|
1017
|
+
}
|
|
1018
|
+
// Create a minimal function instance from stored data
|
|
1019
|
+
// Note: handlers cannot be restored from storage
|
|
1020
|
+
const definition = {
|
|
1021
|
+
name: stored.name,
|
|
1022
|
+
type: stored.type,
|
|
1023
|
+
description: stored.description,
|
|
1024
|
+
};
|
|
1025
|
+
// Add minimal required fields based on type
|
|
1026
|
+
if (stored.type === 'code') {
|
|
1027
|
+
definition.handler = () => {
|
|
1028
|
+
throw new Error('Handler not available - function loaded from storage');
|
|
1029
|
+
};
|
|
1030
|
+
}
|
|
1031
|
+
else if (stored.type === 'generative') {
|
|
1032
|
+
definition.model = 'unknown';
|
|
1033
|
+
definition.prompt = '';
|
|
1034
|
+
}
|
|
1035
|
+
else if (stored.type === 'agentic') {
|
|
1036
|
+
definition.agent = 'unknown';
|
|
1037
|
+
}
|
|
1038
|
+
else if (stored.type === 'human') {
|
|
1039
|
+
definition.channel = 'unknown';
|
|
1040
|
+
definition.timeout = 60000;
|
|
1041
|
+
}
|
|
1042
|
+
return createFunction(definition, {
|
|
1043
|
+
...options,
|
|
1044
|
+
autoRegister: options.autoRegister ?? true,
|
|
1045
|
+
});
|
|
1046
|
+
};
|
|
1047
|
+
export default createFunction;
|
|
1048
|
+
//# sourceMappingURL=createFunction.js.map
|