@pikku/core 0.11.2 → 0.12.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/CHANGELOG.md +57 -2
- package/dist/crypto-utils.d.ts +2 -0
- package/dist/crypto-utils.js +78 -0
- package/dist/errors/errors.d.ts +14 -0
- package/dist/errors/errors.js +26 -0
- package/dist/errors/index.d.ts +1 -1
- package/dist/errors/index.js +1 -1
- package/dist/function/function-runner.d.ts +10 -4
- package/dist/function/function-runner.js +126 -45
- package/dist/function/functions.types.d.ts +32 -87
- package/dist/function/functions.types.js +16 -46
- package/dist/function/index.d.ts +2 -2
- package/dist/function/index.js +1 -2
- package/dist/handle-error.d.ts +2 -2
- package/dist/handle-error.js +3 -12
- package/dist/index.d.ts +30 -26
- package/dist/index.js +19 -29
- package/dist/internal.d.ts +3 -0
- package/dist/internal.js +2 -0
- package/dist/middleware/auth-apikey.d.ts +2 -2
- package/dist/middleware/auth-apikey.js +3 -3
- package/dist/middleware/auth-bearer.d.ts +3 -3
- package/dist/middleware/auth-bearer.js +4 -7
- package/dist/middleware/auth-cookie.d.ts +4 -4
- package/dist/middleware/auth-cookie.js +7 -9
- package/dist/middleware/cors.d.ts +46 -0
- package/dist/middleware/cors.js +81 -0
- package/dist/middleware/index.d.ts +5 -3
- package/dist/middleware/index.js +5 -3
- package/dist/middleware/remote-auth.d.ts +5 -0
- package/dist/middleware/remote-auth.js +55 -0
- package/dist/middleware/timeout.d.ts +2 -2
- package/dist/middleware-runner.d.ts +1 -1
- package/dist/middleware-runner.js +8 -10
- package/dist/permissions.d.ts +2 -2
- package/dist/permissions.js +11 -15
- package/dist/pikku-state.d.ts +10 -8
- package/dist/pikku-state.js +48 -25
- package/dist/schema.d.ts +4 -4
- package/dist/schema.js +20 -15
- package/dist/services/ai-agent-runner-service.d.ts +43 -0
- package/dist/services/ai-agent-runner-service.js +1 -0
- package/dist/services/ai-run-state-service.d.ts +13 -0
- package/dist/services/ai-run-state-service.js +1 -0
- package/dist/services/ai-storage-service.d.ts +18 -0
- package/dist/services/ai-storage-service.js +1 -0
- package/dist/services/content-service.d.ts +6 -0
- package/dist/services/deployment-service.d.ts +19 -0
- package/dist/services/deployment-service.js +1 -0
- package/dist/services/gopass-secrets.d.ts +47 -0
- package/dist/services/gopass-secrets.js +97 -0
- package/dist/services/in-memory-ai-run-state-service.d.ts +15 -0
- package/dist/services/in-memory-ai-run-state-service.js +44 -0
- package/dist/services/in-memory-trigger-service.d.ts +22 -0
- package/dist/services/in-memory-trigger-service.js +48 -0
- package/dist/services/in-memory-workflow-service.d.ts +59 -0
- package/dist/services/in-memory-workflow-service.js +287 -0
- package/dist/services/index.d.ts +26 -11
- package/dist/services/index.js +11 -13
- package/dist/services/local-content.d.ts +2 -1
- package/dist/services/local-content.js +6 -1
- package/dist/services/local-secrets.d.ts +25 -2
- package/dist/services/local-secrets.js +45 -0
- package/dist/services/local-variables.d.ts +6 -1
- package/dist/services/local-variables.js +18 -0
- package/dist/services/logger-console.d.ts +2 -1
- package/dist/services/scheduler-service.d.ts +9 -1
- package/dist/services/scheduler-service.js +8 -0
- package/dist/services/schema-service.d.ts +6 -0
- package/dist/services/scoped-secret-service.d.ts +16 -0
- package/dist/services/scoped-secret-service.js +35 -0
- package/dist/services/secret-service.d.ts +25 -4
- package/dist/services/trigger-service.d.ts +16 -0
- package/dist/services/trigger-service.js +1 -0
- package/dist/services/typed-secret-service.d.ts +30 -0
- package/dist/services/typed-secret-service.js +40 -0
- package/dist/services/typed-variables-service.d.ts +27 -0
- package/dist/services/typed-variables-service.js +46 -0
- package/dist/services/user-session-service.d.ts +15 -2
- package/dist/services/user-session-service.js +17 -0
- package/dist/services/variables-service.d.ts +5 -0
- package/dist/services/workflow-service.d.ts +9 -5
- package/dist/types/core.types.d.ts +67 -25
- package/dist/types/core.types.js +8 -1
- package/dist/types/state.types.d.ts +53 -59
- package/dist/utils.d.ts +7 -6
- package/dist/utils.js +27 -12
- package/dist/version.d.ts +6 -0
- package/dist/version.js +19 -0
- package/dist/wirings/ai-agent/ai-agent-assistant-ui.d.ts +5 -0
- package/dist/wirings/ai-agent/ai-agent-assistant-ui.js +146 -0
- package/dist/wirings/ai-agent/ai-agent-helpers.d.ts +28 -0
- package/dist/wirings/ai-agent/ai-agent-helpers.js +40 -0
- package/dist/wirings/ai-agent/ai-agent-memory.d.ts +29 -0
- package/dist/wirings/ai-agent/ai-agent-memory.js +199 -0
- package/dist/wirings/ai-agent/ai-agent-model-config.d.ts +9 -0
- package/dist/wirings/ai-agent/ai-agent-model-config.js +36 -0
- package/dist/wirings/ai-agent/ai-agent-prepare.d.ts +59 -0
- package/dist/wirings/ai-agent/ai-agent-prepare.js +348 -0
- package/dist/wirings/ai-agent/ai-agent-registry.d.ts +15 -0
- package/dist/wirings/ai-agent/ai-agent-registry.js +47 -0
- package/dist/wirings/ai-agent/ai-agent-runner.d.ts +3 -0
- package/dist/wirings/ai-agent/ai-agent-runner.js +195 -0
- package/dist/wirings/ai-agent/ai-agent-stream.d.ts +12 -0
- package/dist/wirings/ai-agent/ai-agent-stream.js +662 -0
- package/dist/wirings/ai-agent/ai-agent.types.d.ts +313 -0
- package/dist/wirings/ai-agent/ai-agent.types.js +1 -0
- package/dist/wirings/ai-agent/index.d.ts +7 -0
- package/dist/wirings/ai-agent/index.js +6 -0
- package/dist/wirings/channel/channel-common.d.ts +8 -5
- package/dist/wirings/channel/channel-common.js +15 -8
- package/dist/wirings/channel/channel-handler.d.ts +3 -3
- package/dist/wirings/channel/channel-handler.js +15 -6
- package/dist/wirings/channel/channel-middleware-runner.d.ts +9 -0
- package/dist/wirings/channel/channel-middleware-runner.js +73 -0
- package/dist/wirings/channel/channel-runner.d.ts +5 -5
- package/dist/wirings/channel/channel-runner.js +10 -9
- package/dist/wirings/channel/channel-store.d.ts +1 -1
- package/dist/wirings/channel/channel.types.d.ts +9 -7
- package/dist/wirings/channel/define-channel-routes.d.ts +21 -0
- package/dist/wirings/channel/define-channel-routes.js +23 -0
- package/dist/wirings/channel/index.d.ts +10 -8
- package/dist/wirings/channel/index.js +7 -8
- package/dist/wirings/channel/local/index.d.ts +3 -3
- package/dist/wirings/channel/local/index.js +3 -3
- package/dist/wirings/channel/local/local-channel-runner.d.ts +7 -2
- package/dist/wirings/channel/local/local-channel-runner.js +22 -9
- package/dist/wirings/channel/local/local-eventhub-service.d.ts +2 -2
- package/dist/wirings/channel/log-channels.d.ts +1 -1
- package/dist/wirings/channel/pikku-abstract-channel-handler.d.ts +1 -1
- package/dist/wirings/channel/serverless/index.d.ts +1 -1
- package/dist/wirings/channel/serverless/index.js +1 -1
- package/dist/wirings/channel/serverless/serverless-channel-runner.d.ts +4 -4
- package/dist/wirings/channel/serverless/serverless-channel-runner.js +40 -23
- package/dist/wirings/cli/channel/cli-channel-runner.d.ts +1 -1
- package/dist/wirings/cli/channel/index.d.ts +1 -1
- package/dist/wirings/cli/channel/index.js +1 -1
- package/dist/wirings/cli/cli-runner.d.ts +3 -3
- package/dist/wirings/cli/cli-runner.js +12 -8
- package/dist/wirings/cli/cli.types.d.ts +9 -9
- package/dist/wirings/cli/command-parser.d.ts +1 -1
- package/dist/wirings/cli/define-cli-commands.d.ts +19 -0
- package/dist/wirings/cli/define-cli-commands.js +20 -0
- package/dist/wirings/cli/index.d.ts +2 -3
- package/dist/wirings/cli/index.js +1 -3
- package/dist/wirings/http/http-routes.d.ts +69 -0
- package/dist/wirings/http/http-routes.js +139 -0
- package/dist/wirings/http/http-runner.d.ts +9 -9
- package/dist/wirings/http/http-runner.js +79 -34
- package/dist/wirings/http/http.types.d.ts +77 -41
- package/dist/wirings/http/index.d.ts +5 -4
- package/dist/wirings/http/index.js +4 -3
- package/dist/wirings/http/log-http-routes.d.ts +1 -1
- package/dist/wirings/http/pikku-fetch-http-request.d.ts +1 -1
- package/dist/wirings/http/pikku-fetch-http-response.d.ts +2 -2
- package/dist/wirings/http/pikku-fetch-http-response.js +6 -1
- package/dist/wirings/http/routers/http-router.d.ts +1 -1
- package/dist/wirings/http/routers/path-to-regex.d.ts +2 -2
- package/dist/wirings/mcp/index.d.ts +4 -3
- package/dist/wirings/mcp/index.js +3 -3
- package/dist/wirings/mcp/mcp-endpoint-registry.d.ts +1 -1
- package/dist/wirings/mcp/mcp-runner.d.ts +1 -6
- package/dist/wirings/mcp/mcp-runner.js +28 -40
- package/dist/wirings/mcp/mcp.types.d.ts +5 -5
- package/dist/wirings/node/index.d.ts +2 -0
- package/dist/wirings/node/index.js +1 -0
- package/dist/wirings/node/node.types.d.ts +20 -0
- package/dist/wirings/node/node.types.js +1 -0
- package/dist/wirings/oauth2/index.d.ts +3 -0
- package/dist/wirings/oauth2/index.js +2 -0
- package/dist/wirings/oauth2/oauth2-client.d.ts +47 -0
- package/dist/wirings/oauth2/oauth2-client.js +267 -0
- package/dist/wirings/oauth2/oauth2.types.d.ts +65 -0
- package/dist/wirings/oauth2/oauth2.types.js +4 -0
- package/dist/wirings/oauth2/wire-oauth2-credential.d.ts +20 -0
- package/dist/wirings/oauth2/wire-oauth2-credential.js +21 -0
- package/dist/wirings/queue/index.d.ts +1 -1
- package/dist/wirings/queue/queue-runner.d.ts +1 -5
- package/dist/wirings/queue/queue-runner.js +11 -8
- package/dist/wirings/queue/queue.types.d.ts +5 -5
- package/dist/wirings/queue/register-queue-helper.d.ts +2 -2
- package/dist/wirings/queue/register-queue-helper.js +1 -1
- package/dist/wirings/queue/validate-worker-config.d.ts +1 -1
- package/dist/wirings/rpc/index.d.ts +3 -1
- package/dist/wirings/rpc/index.js +2 -1
- package/dist/wirings/rpc/rpc-runner.d.ts +58 -8
- package/dist/wirings/rpc/rpc-runner.js +177 -31
- package/dist/wirings/rpc/rpc-types.d.ts +20 -4
- package/dist/wirings/rpc/wire-addon.d.ts +10 -0
- package/dist/wirings/rpc/wire-addon.js +9 -0
- package/dist/wirings/scheduler/index.d.ts +2 -2
- package/dist/wirings/scheduler/index.js +1 -2
- package/dist/wirings/scheduler/log-schedulers.d.ts +1 -1
- package/dist/wirings/scheduler/scheduler-runner.d.ts +3 -5
- package/dist/wirings/scheduler/scheduler-runner.js +11 -14
- package/dist/wirings/scheduler/scheduler.types.d.ts +2 -2
- package/dist/wirings/secret/index.d.ts +3 -0
- package/dist/wirings/secret/index.js +2 -0
- package/dist/wirings/secret/secret.types.d.ts +27 -0
- package/dist/wirings/secret/secret.types.js +1 -0
- package/dist/wirings/secret/validate-secret-definitions.d.ts +6 -0
- package/dist/wirings/secret/validate-secret-definitions.js +55 -0
- package/dist/wirings/trigger/index.d.ts +3 -2
- package/dist/wirings/trigger/index.js +2 -2
- package/dist/wirings/trigger/pikku-trigger-service.d.ts +22 -0
- package/dist/wirings/trigger/pikku-trigger-service.js +70 -0
- package/dist/wirings/trigger/trigger-runner.d.ts +18 -9
- package/dist/wirings/trigger/trigger-runner.js +48 -19
- package/dist/wirings/trigger/trigger.types.d.ts +99 -11
- package/dist/wirings/trigger/trigger.types.js +40 -1
- package/dist/wirings/variable/index.d.ts +3 -0
- package/dist/wirings/variable/index.js +2 -0
- package/dist/wirings/variable/validate-variable-definitions.d.ts +6 -0
- package/dist/wirings/variable/validate-variable-definitions.js +45 -0
- package/dist/wirings/variable/variable.types.d.ts +18 -0
- package/dist/wirings/variable/variable.types.js +1 -0
- package/dist/wirings/workflow/dsl/index.d.ts +1 -1
- package/dist/wirings/workflow/dsl/workflow-dsl.types.d.ts +7 -1
- package/dist/wirings/workflow/dsl/workflow-runner.js +3 -2
- package/dist/wirings/workflow/graph/graph-node.d.ts +1 -6
- package/dist/wirings/workflow/graph/graph-runner.d.ts +5 -31
- package/dist/wirings/workflow/graph/graph-runner.js +384 -296
- package/dist/wirings/workflow/graph/index.d.ts +4 -3
- package/dist/wirings/workflow/graph/index.js +3 -3
- package/dist/wirings/workflow/graph/template.d.ts +15 -0
- package/dist/wirings/workflow/graph/template.js +27 -0
- package/dist/wirings/workflow/graph/wire-workflow-graph.d.ts +15 -0
- package/dist/wirings/workflow/graph/wire-workflow-graph.js +8 -0
- package/dist/wirings/workflow/graph/workflow-graph.types.d.ts +0 -13
- package/dist/wirings/workflow/index.d.ts +6 -6
- package/dist/wirings/workflow/index.js +6 -4
- package/dist/wirings/workflow/pikku-workflow-service.d.ts +29 -19
- package/dist/wirings/workflow/pikku-workflow-service.js +217 -82
- package/dist/wirings/workflow/workflow-helpers.d.ts +36 -0
- package/dist/wirings/workflow/workflow-helpers.js +38 -0
- package/dist/wirings/workflow/workflow.types.d.ts +42 -95
- package/package.json +14 -15
- package/run-tests.sh +4 -1
- package/src/crypto-utils.test.ts +116 -0
- package/src/crypto-utils.ts +99 -0
- package/src/errors/error.test.ts +143 -2
- package/src/errors/errors.ts +27 -0
- package/src/errors/index.ts +1 -1
- package/src/factory-functions.test.ts +42 -2
- package/src/function/function-runner.test.ts +6 -13
- package/src/function/function-runner.ts +161 -53
- package/src/function/functions.types.ts +63 -132
- package/src/function/index.ts +5 -2
- package/src/handle-error.test.ts +361 -0
- package/src/handle-error.ts +6 -14
- package/src/index.ts +97 -30
- package/src/internal.ts +6 -0
- package/src/middleware/auth-apikey.test.ts +16 -13
- package/src/middleware/auth-apikey.ts +3 -3
- package/src/middleware/auth-bearer.test.ts +18 -15
- package/src/middleware/auth-bearer.ts +27 -32
- package/src/middleware/auth-cookie.test.ts +17 -14
- package/src/middleware/auth-cookie.ts +9 -13
- package/src/middleware/cors.test.ts +424 -0
- package/src/middleware/cors.ts +104 -0
- package/src/middleware/index.ts +5 -3
- package/src/middleware/remote-auth.test.ts +488 -0
- package/src/middleware/remote-auth.ts +68 -0
- package/src/middleware-runner.test.ts +123 -1
- package/src/middleware-runner.ts +13 -13
- package/src/permissions.test.ts +83 -9
- package/src/permissions.ts +17 -19
- package/src/pikku-state.test.ts +224 -0
- package/src/pikku-state.ts +59 -35
- package/src/run-tests-script.test.ts +49 -0
- package/src/schema.test.ts +198 -6
- package/src/schema.ts +28 -21
- package/src/services/ai-agent-runner-service.ts +41 -0
- package/src/services/ai-run-state-service.ts +20 -0
- package/src/services/ai-storage-service.ts +39 -0
- package/src/services/content-service.ts +7 -0
- package/src/services/deployment-service.ts +22 -0
- package/src/services/gopass-secrets.ts +98 -0
- package/src/services/in-memory-ai-run-state-service.ts +73 -0
- package/src/services/in-memory-trigger-service.ts +64 -0
- package/src/services/in-memory-workflow-service.test.ts +351 -0
- package/src/services/in-memory-workflow-service.ts +395 -0
- package/src/services/index.ts +50 -14
- package/src/services/local-content.ts +9 -3
- package/src/services/local-secrets.test.ts +80 -0
- package/src/services/local-secrets.ts +53 -2
- package/src/services/local-variables.test.ts +61 -0
- package/src/services/local-variables.ts +23 -1
- package/src/services/logger-console.test.ts +118 -0
- package/src/services/logger-console.ts +2 -1
- package/src/services/scheduler-service.ts +11 -1
- package/src/services/schema-service.ts +7 -0
- package/src/services/scoped-secret-service.test.ts +74 -0
- package/src/services/scoped-secret-service.ts +41 -0
- package/src/services/secret-service.ts +25 -4
- package/src/services/trigger-service.ts +17 -0
- package/src/services/typed-secret-service.test.ts +93 -0
- package/src/services/typed-secret-service.ts +70 -0
- package/src/services/typed-variables-service.test.ts +73 -0
- package/src/services/typed-variables-service.ts +81 -0
- package/src/services/user-session-service.test.ts +113 -0
- package/src/services/user-session-service.ts +25 -2
- package/src/services/variables-service.ts +5 -0
- package/src/services/workflow-service.ts +23 -13
- package/src/time-utils.test.ts +1 -1
- package/src/types/core.types.ts +95 -24
- package/src/types/state.types.ts +56 -73
- package/src/utils.test.ts +350 -0
- package/src/utils.ts +33 -18
- package/src/version.test.ts +80 -0
- package/src/version.ts +25 -0
- package/src/wirings/ai-agent/ai-agent-assistant-ui.test.ts +363 -0
- package/src/wirings/ai-agent/ai-agent-assistant-ui.ts +214 -0
- package/src/wirings/ai-agent/ai-agent-helpers.test.ts +152 -0
- package/src/wirings/ai-agent/ai-agent-helpers.ts +76 -0
- package/src/wirings/ai-agent/ai-agent-memory.ts +300 -0
- package/src/wirings/ai-agent/ai-agent-model-config.test.ts +115 -0
- package/src/wirings/ai-agent/ai-agent-model-config.ts +43 -0
- package/src/wirings/ai-agent/ai-agent-prepare.ts +504 -0
- package/src/wirings/ai-agent/ai-agent-registry.test.ts +37 -0
- package/src/wirings/ai-agent/ai-agent-registry.ts +82 -0
- package/src/wirings/ai-agent/ai-agent-runner.test.ts +317 -0
- package/src/wirings/ai-agent/ai-agent-runner.ts +251 -0
- package/src/wirings/ai-agent/ai-agent-stream.test.ts +652 -0
- package/src/wirings/ai-agent/ai-agent-stream.ts +1049 -0
- package/src/wirings/ai-agent/ai-agent.types.ts +328 -0
- package/src/wirings/ai-agent/index.ts +39 -0
- package/src/wirings/channel/channel-common.ts +33 -20
- package/src/wirings/channel/channel-handler.ts +26 -9
- package/src/wirings/channel/channel-middleware-runner.ts +119 -0
- package/src/wirings/channel/channel-runner.ts +15 -16
- package/src/wirings/channel/channel-store.ts +1 -1
- package/src/wirings/channel/channel.types.ts +21 -9
- package/src/wirings/channel/define-channel-routes.ts +25 -0
- package/src/wirings/channel/index.ts +23 -8
- package/src/wirings/channel/local/index.ts +3 -3
- package/src/wirings/channel/local/local-channel-runner.test.ts +57 -7
- package/src/wirings/channel/local/local-channel-runner.ts +40 -19
- package/src/wirings/channel/local/local-eventhub-service.test.ts +2 -2
- package/src/wirings/channel/local/local-eventhub-service.ts +2 -2
- package/src/wirings/channel/log-channels.ts +1 -1
- package/src/wirings/channel/pikku-abstract-channel-handler.ts +1 -1
- package/src/wirings/channel/serverless/index.ts +5 -1
- package/src/wirings/channel/serverless/serverless-channel-runner.ts +57 -36
- package/src/wirings/cli/channel/cli-channel-runner.ts +1 -1
- package/src/wirings/cli/channel/index.ts +1 -1
- package/src/wirings/cli/cli-runner.test.ts +10 -10
- package/src/wirings/cli/cli-runner.ts +28 -14
- package/src/wirings/cli/cli.types.ts +14 -14
- package/src/wirings/cli/command-parser.test.ts +9 -9
- package/src/wirings/cli/command-parser.ts +1 -1
- package/src/wirings/cli/define-cli-commands.ts +24 -0
- package/src/wirings/cli/index.ts +10 -4
- package/src/wirings/http/http-routes.test.ts +322 -0
- package/src/wirings/http/http-routes.ts +198 -0
- package/src/wirings/http/http-runner.test.ts +12 -20
- package/src/wirings/http/http-runner.ts +101 -51
- package/src/wirings/http/http.types.ts +119 -48
- package/src/wirings/http/index.ts +16 -4
- package/src/wirings/http/log-http-routes.ts +1 -1
- package/src/wirings/http/pikku-fetch-http-request.ts +1 -1
- package/src/wirings/http/pikku-fetch-http-response.ts +12 -5
- package/src/wirings/http/routers/http-router.ts +1 -1
- package/src/wirings/http/routers/path-to-regex.test.ts +1 -1
- package/src/wirings/http/routers/path-to-regex.ts +4 -3
- package/src/wirings/mcp/index.ts +27 -3
- package/src/wirings/mcp/mcp-endpoint-registry.test.ts +3 -3
- package/src/wirings/mcp/mcp-endpoint-registry.ts +5 -1
- package/src/wirings/mcp/mcp-runner.ts +48 -65
- package/src/wirings/mcp/mcp.types.ts +5 -5
- package/src/wirings/node/index.ts +2 -0
- package/src/wirings/node/node.types.ts +23 -0
- package/src/wirings/oauth2/index.ts +3 -0
- package/src/wirings/oauth2/oauth2-client.test.ts +929 -0
- package/src/wirings/oauth2/oauth2-client.ts +335 -0
- package/src/wirings/oauth2/oauth2.types.ts +69 -0
- package/src/wirings/oauth2/wire-oauth2-credential.ts +23 -0
- package/src/wirings/queue/index.ts +13 -2
- package/src/wirings/queue/queue-runner.test.ts +142 -73
- package/src/wirings/queue/queue-runner.ts +18 -14
- package/src/wirings/queue/queue.types.ts +5 -5
- package/src/wirings/queue/register-queue-helper.ts +3 -5
- package/src/wirings/queue/validate-worker-config.test.ts +108 -0
- package/src/wirings/queue/validate-worker-config.ts +4 -1
- package/src/wirings/rpc/index.ts +3 -1
- package/src/wirings/rpc/rpc-runner.ts +254 -41
- package/src/wirings/rpc/rpc-types.ts +28 -4
- package/src/wirings/rpc/wire-addon.ts +20 -0
- package/src/wirings/scheduler/index.ts +6 -4
- package/src/wirings/scheduler/log-schedulers.ts +1 -1
- package/src/wirings/scheduler/scheduler-runner.test.ts +78 -45
- package/src/wirings/scheduler/scheduler-runner.ts +20 -27
- package/src/wirings/scheduler/scheduler.types.ts +2 -2
- package/src/wirings/secret/index.ts +9 -0
- package/src/wirings/secret/secret.types.ts +32 -0
- package/src/wirings/secret/validate-secret-definitions.test.ts +140 -0
- package/src/wirings/secret/validate-secret-definitions.ts +82 -0
- package/src/wirings/trigger/index.ts +10 -2
- package/src/wirings/trigger/pikku-trigger-service.ts +112 -0
- package/src/wirings/trigger/trigger-runner.test.ts +79 -0
- package/src/wirings/trigger/trigger-runner.ts +80 -41
- package/src/wirings/trigger/trigger.types.ts +144 -22
- package/src/wirings/variable/index.ts +8 -0
- package/src/wirings/variable/validate-variable-definitions.test.ts +91 -0
- package/src/wirings/variable/validate-variable-definitions.ts +69 -0
- package/src/wirings/variable/variable.types.ts +22 -0
- package/src/wirings/workflow/dsl/index.ts +1 -0
- package/src/wirings/workflow/dsl/workflow-dsl.types.ts +9 -1
- package/src/wirings/workflow/dsl/workflow-runner.ts +4 -3
- package/src/wirings/workflow/graph/graph-node.ts +1 -6
- package/src/wirings/workflow/graph/graph-runner.test.ts +487 -0
- package/src/wirings/workflow/graph/graph-runner.ts +496 -374
- package/src/wirings/workflow/graph/index.ts +14 -3
- package/src/wirings/workflow/graph/template.test.ts +49 -0
- package/src/wirings/workflow/graph/template.ts +42 -0
- package/src/wirings/workflow/graph/wire-workflow-graph.ts +29 -0
- package/src/wirings/workflow/graph/workflow-graph.types.ts +0 -22
- package/src/wirings/workflow/index.ts +21 -10
- package/src/wirings/workflow/pikku-workflow-service.test.ts +200 -0
- package/src/wirings/workflow/pikku-workflow-service.ts +308 -105
- package/src/wirings/workflow/workflow-helpers.test.ts +129 -0
- package/src/wirings/workflow/workflow-helpers.ts +79 -0
- package/src/wirings/workflow/workflow.types.ts +47 -92
- package/tsconfig.tsbuildinfo +1 -1
- package/dist/wirings/forge-node/forge-node.types.d.ts +0 -120
- package/dist/wirings/forge-node/forge-node.types.js +0 -38
- package/dist/wirings/forge-node/index.d.ts +0 -1
- package/dist/wirings/forge-node/index.js +0 -1
- package/dist/wirings/workflow/wire-workflow.d.ts +0 -42
- package/dist/wirings/workflow/wire-workflow.js +0 -53
- package/dist/wirings/workflow/workflow-utils.d.ts +0 -23
- package/dist/wirings/workflow/workflow-utils.js +0 -66
- package/src/wirings/forge-node/forge-node.types.ts +0 -135
- package/src/wirings/forge-node/index.ts +0 -1
- package/src/wirings/workflow/wire-workflow.ts +0 -94
- package/src/wirings/workflow/workflow-utils.ts +0 -120
|
@@ -0,0 +1,662 @@
|
|
|
1
|
+
import { pikkuState, getSingletonServices } from '../../pikku-state.js';
|
|
2
|
+
import { combineChannelMiddleware, wrapChannelWithMiddleware, } from '../channel/channel-middleware-runner.js';
|
|
3
|
+
import { randomUUID } from 'crypto';
|
|
4
|
+
import { parseWorkingMemory, deepMergeWorkingMemory, resolveMemoryServices, loadContextMessages, trimMessages, } from './ai-agent-memory.js';
|
|
5
|
+
import { prepareAgentRun, resolveAgent, buildInstructions, buildToolDefs, createScopedChannel, ToolApprovalRequired, } from './ai-agent-prepare.js';
|
|
6
|
+
import { createAssistantUIChannel, parseAssistantUIInput, } from './ai-agent-assistant-ui.js';
|
|
7
|
+
import { resolveModelConfig } from './ai-agent-model-config.js';
|
|
8
|
+
function createPersistingChannel(parent, storage, threadId) {
|
|
9
|
+
let fullText = '';
|
|
10
|
+
let stepText = '';
|
|
11
|
+
let stepToolCalls = [];
|
|
12
|
+
let stepToolResults = [];
|
|
13
|
+
const totalUsage = {
|
|
14
|
+
inputTokens: 0,
|
|
15
|
+
outputTokens: 0,
|
|
16
|
+
};
|
|
17
|
+
const flushStep = async () => {
|
|
18
|
+
if (!storage)
|
|
19
|
+
return;
|
|
20
|
+
const text = stepText;
|
|
21
|
+
const calls = stepToolCalls;
|
|
22
|
+
const results = stepToolResults;
|
|
23
|
+
stepText = '';
|
|
24
|
+
stepToolCalls = [];
|
|
25
|
+
stepToolResults = [];
|
|
26
|
+
if (text || calls.length > 0) {
|
|
27
|
+
const assistantMsg = {
|
|
28
|
+
id: randomUUID(),
|
|
29
|
+
role: 'assistant',
|
|
30
|
+
content: text || undefined,
|
|
31
|
+
toolCalls: calls.length > 0 ? calls : undefined,
|
|
32
|
+
createdAt: new Date(),
|
|
33
|
+
};
|
|
34
|
+
const messages = [assistantMsg];
|
|
35
|
+
if (results.length > 0) {
|
|
36
|
+
messages.push({
|
|
37
|
+
id: randomUUID(),
|
|
38
|
+
role: 'tool',
|
|
39
|
+
toolResults: results,
|
|
40
|
+
createdAt: new Date(),
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
await storage.saveMessages(threadId, messages);
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
const channel = {
|
|
47
|
+
channelId: parent.channelId,
|
|
48
|
+
openingData: parent.openingData,
|
|
49
|
+
get state() {
|
|
50
|
+
return parent.state;
|
|
51
|
+
},
|
|
52
|
+
get fullText() {
|
|
53
|
+
return fullText;
|
|
54
|
+
},
|
|
55
|
+
get totalUsage() {
|
|
56
|
+
return totalUsage;
|
|
57
|
+
},
|
|
58
|
+
flush: flushStep,
|
|
59
|
+
close: () => parent.close(),
|
|
60
|
+
send: (event) => {
|
|
61
|
+
if (storage) {
|
|
62
|
+
switch (event.type) {
|
|
63
|
+
case 'text-delta':
|
|
64
|
+
stepText += event.text;
|
|
65
|
+
fullText += event.text;
|
|
66
|
+
break;
|
|
67
|
+
case 'tool-call':
|
|
68
|
+
stepToolCalls.push({
|
|
69
|
+
id: event.toolCallId,
|
|
70
|
+
name: event.toolName,
|
|
71
|
+
args: event.args,
|
|
72
|
+
});
|
|
73
|
+
break;
|
|
74
|
+
case 'tool-result':
|
|
75
|
+
stepToolResults.push({
|
|
76
|
+
id: event.toolCallId,
|
|
77
|
+
name: event.toolName,
|
|
78
|
+
result: typeof event.result === 'string'
|
|
79
|
+
? event.result
|
|
80
|
+
: JSON.stringify(event.result),
|
|
81
|
+
});
|
|
82
|
+
break;
|
|
83
|
+
case 'usage':
|
|
84
|
+
totalUsage.inputTokens += event.tokens.input;
|
|
85
|
+
totalUsage.outputTokens += event.tokens.output;
|
|
86
|
+
if (event.model)
|
|
87
|
+
totalUsage.model = event.model;
|
|
88
|
+
flushStep();
|
|
89
|
+
break;
|
|
90
|
+
case 'done':
|
|
91
|
+
flushStep();
|
|
92
|
+
break;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
parent.send(event);
|
|
96
|
+
},
|
|
97
|
+
};
|
|
98
|
+
return channel;
|
|
99
|
+
}
|
|
100
|
+
async function postStreamCleanup(persistingChannel, aiMiddlewares, singletonServices, storage, memoryConfig, threadId, workingMemorySchemaName, messages, aiRunState, runId) {
|
|
101
|
+
const usage = persistingChannel.totalUsage;
|
|
102
|
+
let outputText = persistingChannel.fullText;
|
|
103
|
+
let outputMessages = messages;
|
|
104
|
+
for (let i = aiMiddlewares.length - 1; i >= 0; i--) {
|
|
105
|
+
const mw = aiMiddlewares[i];
|
|
106
|
+
if (mw.modifyOutput) {
|
|
107
|
+
const result = await mw.modifyOutput(singletonServices, {
|
|
108
|
+
text: outputText,
|
|
109
|
+
messages: outputMessages,
|
|
110
|
+
usage: {
|
|
111
|
+
inputTokens: usage.inputTokens,
|
|
112
|
+
outputTokens: usage.outputTokens,
|
|
113
|
+
},
|
|
114
|
+
});
|
|
115
|
+
outputText = result.text;
|
|
116
|
+
outputMessages = result.messages;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
if (storage && memoryConfig?.workingMemory && outputText) {
|
|
120
|
+
const parsed = parseWorkingMemory(outputText);
|
|
121
|
+
if (parsed) {
|
|
122
|
+
const existing = (await storage.getWorkingMemory(threadId, 'thread')) ?? {};
|
|
123
|
+
const merged = deepMergeWorkingMemory(existing, parsed);
|
|
124
|
+
if (singletonServices.schema && workingMemorySchemaName) {
|
|
125
|
+
try {
|
|
126
|
+
await singletonServices.schema.validateSchema(workingMemorySchemaName, merged);
|
|
127
|
+
}
|
|
128
|
+
catch (err) {
|
|
129
|
+
singletonServices.logger.warn(`Working memory validation failed: ${err instanceof Error ? err.message : String(err)}`);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
await storage.saveWorkingMemory(threadId, 'thread', merged);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
await aiRunState.updateRun(runId, {
|
|
136
|
+
status: 'completed',
|
|
137
|
+
...(usage.model
|
|
138
|
+
? {
|
|
139
|
+
usage: {
|
|
140
|
+
inputTokens: usage.inputTokens,
|
|
141
|
+
outputTokens: usage.outputTokens,
|
|
142
|
+
model: usage.model,
|
|
143
|
+
},
|
|
144
|
+
}
|
|
145
|
+
: {}),
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
async function runStreamStepLoop(params) {
|
|
149
|
+
const { agent, runnerParams, maxSteps, agentRunner, persistingChannel, channel, runId, aiMiddlewares, } = params;
|
|
150
|
+
const singletonServices = getSingletonServices();
|
|
151
|
+
for (let step = 0; step < maxSteps; step++) {
|
|
152
|
+
if (agent.prepareStep) {
|
|
153
|
+
let stopped = false;
|
|
154
|
+
await agent.prepareStep({
|
|
155
|
+
stepNumber: step,
|
|
156
|
+
messages: runnerParams.messages,
|
|
157
|
+
tools: runnerParams.tools,
|
|
158
|
+
toolChoice: runnerParams.toolChoice,
|
|
159
|
+
model: runnerParams.model,
|
|
160
|
+
stop: () => {
|
|
161
|
+
stopped = true;
|
|
162
|
+
},
|
|
163
|
+
});
|
|
164
|
+
if (stopped)
|
|
165
|
+
break;
|
|
166
|
+
}
|
|
167
|
+
channel.send({ type: 'step-start', stepNumber: step });
|
|
168
|
+
const stepResult = await agentRunner.stream(runnerParams, persistingChannel);
|
|
169
|
+
for (const mw of aiMiddlewares) {
|
|
170
|
+
if (mw.afterStep) {
|
|
171
|
+
await mw.afterStep(singletonServices, {
|
|
172
|
+
stepNumber: step,
|
|
173
|
+
text: stepResult.text,
|
|
174
|
+
toolCalls: stepResult.toolCalls,
|
|
175
|
+
toolResults: stepResult.toolResults,
|
|
176
|
+
usage: stepResult.usage,
|
|
177
|
+
finishReason: stepResult.finishReason,
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
if (stepResult.toolCalls.length === 0)
|
|
182
|
+
break;
|
|
183
|
+
const approvalNeeded = checkForApproval(stepResult, runnerParams.tools, runId);
|
|
184
|
+
if (approvalNeeded) {
|
|
185
|
+
return { outcome: 'approval', approval: approvalNeeded };
|
|
186
|
+
}
|
|
187
|
+
appendStepMessages(runnerParams, stepResult);
|
|
188
|
+
}
|
|
189
|
+
return { outcome: 'done' };
|
|
190
|
+
}
|
|
191
|
+
function checkForApproval(stepResult, tools, runId) {
|
|
192
|
+
for (const tc of stepResult.toolCalls) {
|
|
193
|
+
const toolDef = tools.find((t) => t.name === tc.toolName);
|
|
194
|
+
if (toolDef?.needsApproval) {
|
|
195
|
+
return new ToolApprovalRequired(tc.toolCallId, tc.toolName, tc.args);
|
|
196
|
+
}
|
|
197
|
+
const tr = stepResult.toolResults.find((r) => r.toolCallId === tc.toolCallId);
|
|
198
|
+
if (tr?.result &&
|
|
199
|
+
typeof tr.result === 'object' &&
|
|
200
|
+
'__approvalRequired' in tr.result) {
|
|
201
|
+
const r = tr.result;
|
|
202
|
+
return new ToolApprovalRequired(tc.toolCallId, r.toolName, r.args, r.reason, r.displayToolName, r.displayArgs, r.agentRunId);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
return null;
|
|
206
|
+
}
|
|
207
|
+
function appendStepMessages(runnerParams, stepResult) {
|
|
208
|
+
const assistantMsg = {
|
|
209
|
+
id: randomUUID(),
|
|
210
|
+
role: 'assistant',
|
|
211
|
+
content: stepResult.text || undefined,
|
|
212
|
+
toolCalls: stepResult.toolCalls.length > 0
|
|
213
|
+
? stepResult.toolCalls.map((tc) => ({
|
|
214
|
+
id: tc.toolCallId,
|
|
215
|
+
name: tc.toolName,
|
|
216
|
+
args: tc.args,
|
|
217
|
+
}))
|
|
218
|
+
: undefined,
|
|
219
|
+
createdAt: new Date(),
|
|
220
|
+
};
|
|
221
|
+
runnerParams.messages.push(assistantMsg);
|
|
222
|
+
if (stepResult.toolResults.length > 0) {
|
|
223
|
+
const toolMsg = {
|
|
224
|
+
id: randomUUID(),
|
|
225
|
+
role: 'tool',
|
|
226
|
+
toolResults: stepResult.toolResults.map((tr) => ({
|
|
227
|
+
id: tr.toolCallId,
|
|
228
|
+
name: tr.toolName,
|
|
229
|
+
result: typeof tr.result === 'string' ? tr.result : JSON.stringify(tr.result),
|
|
230
|
+
})),
|
|
231
|
+
createdAt: new Date(),
|
|
232
|
+
};
|
|
233
|
+
runnerParams.messages.push(toolMsg);
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
function handleApproval(err, runId, channel, aiRunState, persistingChannel) {
|
|
237
|
+
return (async () => {
|
|
238
|
+
await persistingChannel.flush();
|
|
239
|
+
const pendingApproval = err.agentRunId
|
|
240
|
+
? {
|
|
241
|
+
type: 'agent-call',
|
|
242
|
+
toolCallId: err.toolCallId,
|
|
243
|
+
agentName: err.toolName,
|
|
244
|
+
agentRunId: err.agentRunId,
|
|
245
|
+
displayToolName: err.displayToolName ?? err.toolName,
|
|
246
|
+
displayArgs: err.displayArgs ?? err.args,
|
|
247
|
+
}
|
|
248
|
+
: {
|
|
249
|
+
type: 'tool-call',
|
|
250
|
+
toolCallId: err.toolCallId,
|
|
251
|
+
toolName: err.toolName,
|
|
252
|
+
args: err.args,
|
|
253
|
+
};
|
|
254
|
+
await aiRunState.updateRun(runId, {
|
|
255
|
+
status: 'suspended',
|
|
256
|
+
suspendReason: 'approval',
|
|
257
|
+
pendingApprovals: [pendingApproval],
|
|
258
|
+
});
|
|
259
|
+
const approvalEvent = {
|
|
260
|
+
type: 'approval-request',
|
|
261
|
+
toolCallId: err.toolCallId,
|
|
262
|
+
toolName: err.displayToolName ?? err.toolName,
|
|
263
|
+
args: err.displayArgs ?? err.args,
|
|
264
|
+
reason: err.reason,
|
|
265
|
+
runId,
|
|
266
|
+
};
|
|
267
|
+
channel.send(approvalEvent);
|
|
268
|
+
channel.send({ type: 'done' });
|
|
269
|
+
channel.close();
|
|
270
|
+
})();
|
|
271
|
+
}
|
|
272
|
+
export async function streamAIAgent(agentName, input, channel, params, agentSessionMap, options) {
|
|
273
|
+
const sessionMap = agentSessionMap ?? new Map();
|
|
274
|
+
const { agent: resolvedAgentForProtocol } = resolveAgent(agentName);
|
|
275
|
+
const inputRecord = input;
|
|
276
|
+
const useUIMessageStream = resolvedAgentForProtocol.protocol === 'ui-message-stream' ||
|
|
277
|
+
Array.isArray(inputRecord.messages);
|
|
278
|
+
let normalizedInput;
|
|
279
|
+
if (useUIMessageStream) {
|
|
280
|
+
normalizedInput = parseAssistantUIInput(inputRecord);
|
|
281
|
+
channel = createAssistantUIChannel(channel);
|
|
282
|
+
}
|
|
283
|
+
else {
|
|
284
|
+
normalizedInput = input;
|
|
285
|
+
}
|
|
286
|
+
const streamContext = { channel, options };
|
|
287
|
+
const { agent, packageName, resolvedName, agentRunner, storage, memoryConfig, threadId, userMessage, runnerParams, maxSteps, missingRpcs, workingMemorySchemaName, } = await prepareAgentRun(agentName, normalizedInput, params, sessionMap, streamContext);
|
|
288
|
+
const singletonServices = getSingletonServices();
|
|
289
|
+
const { aiRunState } = singletonServices;
|
|
290
|
+
if (!aiRunState) {
|
|
291
|
+
throw new Error('AIRunStateService not available in singletonServices');
|
|
292
|
+
}
|
|
293
|
+
if (missingRpcs.length > 0) {
|
|
294
|
+
await aiRunState.createRun({
|
|
295
|
+
agentName,
|
|
296
|
+
threadId,
|
|
297
|
+
resourceId: normalizedInput.resourceId,
|
|
298
|
+
status: 'suspended',
|
|
299
|
+
suspendReason: 'rpc-missing',
|
|
300
|
+
missingRpcs,
|
|
301
|
+
usage: { inputTokens: 0, outputTokens: 0, model: agent.model },
|
|
302
|
+
createdAt: new Date(),
|
|
303
|
+
updatedAt: new Date(),
|
|
304
|
+
});
|
|
305
|
+
channel.send({ type: 'suspended', reason: 'rpc-missing', missingRpcs });
|
|
306
|
+
channel.send({ type: 'done' });
|
|
307
|
+
return;
|
|
308
|
+
}
|
|
309
|
+
const aiMiddlewares = agent.aiMiddleware ?? [];
|
|
310
|
+
let modifiedMessages = runnerParams.messages;
|
|
311
|
+
let modifiedInstructions = runnerParams.instructions;
|
|
312
|
+
for (const mw of aiMiddlewares) {
|
|
313
|
+
if (mw.modifyInput) {
|
|
314
|
+
const result = await mw.modifyInput(singletonServices, {
|
|
315
|
+
messages: modifiedMessages,
|
|
316
|
+
instructions: modifiedInstructions,
|
|
317
|
+
});
|
|
318
|
+
modifiedMessages = result.messages;
|
|
319
|
+
modifiedInstructions = result.instructions;
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
runnerParams.messages = modifiedMessages;
|
|
323
|
+
runnerParams.instructions = modifiedInstructions;
|
|
324
|
+
const runId = await aiRunState.createRun({
|
|
325
|
+
agentName,
|
|
326
|
+
threadId,
|
|
327
|
+
resourceId: normalizedInput.resourceId,
|
|
328
|
+
status: 'running',
|
|
329
|
+
usage: { inputTokens: 0, outputTokens: 0, model: agent.model },
|
|
330
|
+
createdAt: new Date(),
|
|
331
|
+
updatedAt: new Date(),
|
|
332
|
+
});
|
|
333
|
+
if (storage) {
|
|
334
|
+
await storage.saveMessages(threadId, [userMessage]);
|
|
335
|
+
}
|
|
336
|
+
const streamMiddleware = aiMiddlewares
|
|
337
|
+
.filter((mw) => mw.modifyOutputStream)
|
|
338
|
+
.map((mw) => {
|
|
339
|
+
const state = {};
|
|
340
|
+
const allEvents = [];
|
|
341
|
+
return async (services, event, next) => {
|
|
342
|
+
allEvents.push(event);
|
|
343
|
+
const result = await mw.modifyOutputStream(services, {
|
|
344
|
+
event,
|
|
345
|
+
allEvents,
|
|
346
|
+
state,
|
|
347
|
+
});
|
|
348
|
+
if (result != null)
|
|
349
|
+
await next(result);
|
|
350
|
+
};
|
|
351
|
+
});
|
|
352
|
+
const agentsMeta = pikkuState(packageName, 'agent', 'agentsMeta');
|
|
353
|
+
const meta = agentsMeta[resolvedName];
|
|
354
|
+
const allChannelMiddleware = combineChannelMiddleware('agent', `stream:${agentName}`, {
|
|
355
|
+
wireInheritedChannelMiddleware: meta?.channelMiddleware,
|
|
356
|
+
wireChannelMiddleware: [
|
|
357
|
+
...(agent.channelMiddleware ?? []),
|
|
358
|
+
...streamMiddleware,
|
|
359
|
+
],
|
|
360
|
+
});
|
|
361
|
+
const wrappedChannel = allChannelMiddleware.length > 0
|
|
362
|
+
? wrapChannelWithMiddleware({ channel }, singletonServices, allChannelMiddleware).channel
|
|
363
|
+
: channel;
|
|
364
|
+
const persistingChannel = createPersistingChannel(wrappedChannel, storage, threadId);
|
|
365
|
+
try {
|
|
366
|
+
const loopResult = await runStreamStepLoop({
|
|
367
|
+
agent,
|
|
368
|
+
runnerParams,
|
|
369
|
+
maxSteps,
|
|
370
|
+
agentRunner,
|
|
371
|
+
persistingChannel,
|
|
372
|
+
channel,
|
|
373
|
+
runId,
|
|
374
|
+
aiMiddlewares,
|
|
375
|
+
});
|
|
376
|
+
if (loopResult.outcome === 'approval') {
|
|
377
|
+
await handleApproval(loopResult.approval, runId, channel, aiRunState, persistingChannel);
|
|
378
|
+
return;
|
|
379
|
+
}
|
|
380
|
+
await postStreamCleanup(persistingChannel, aiMiddlewares, singletonServices, storage, memoryConfig, threadId, workingMemorySchemaName, runnerParams.messages, aiRunState, runId);
|
|
381
|
+
channel.send({ type: 'done' });
|
|
382
|
+
channel.close();
|
|
383
|
+
}
|
|
384
|
+
catch (err) {
|
|
385
|
+
for (const mw of aiMiddlewares) {
|
|
386
|
+
if (mw.onError) {
|
|
387
|
+
try {
|
|
388
|
+
await mw.onError(singletonServices, {
|
|
389
|
+
error: err instanceof Error ? err : new Error(String(err)),
|
|
390
|
+
stepNumber: -1,
|
|
391
|
+
messages: runnerParams.messages,
|
|
392
|
+
});
|
|
393
|
+
}
|
|
394
|
+
catch {
|
|
395
|
+
// onError hooks must not affect error flow
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
await aiRunState.updateRun(runId, { status: 'failed' });
|
|
400
|
+
channel.send({
|
|
401
|
+
type: 'error',
|
|
402
|
+
message: err instanceof Error ? err.message : String(err),
|
|
403
|
+
});
|
|
404
|
+
channel.send({ type: 'done' });
|
|
405
|
+
channel.close();
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
export async function resumeAIAgent(input, channel, params, options) {
|
|
409
|
+
const singletonServices = getSingletonServices();
|
|
410
|
+
const { aiRunState } = singletonServices;
|
|
411
|
+
if (!aiRunState) {
|
|
412
|
+
throw new Error('AIRunStateService not available in singletonServices');
|
|
413
|
+
}
|
|
414
|
+
const run = await aiRunState.getRun(input.runId);
|
|
415
|
+
if (!run) {
|
|
416
|
+
throw new Error(`No run found for runId ${input.runId}`);
|
|
417
|
+
}
|
|
418
|
+
const pending = run.pendingApprovals?.find((p) => p.toolCallId === input.toolCallId);
|
|
419
|
+
if (!pending) {
|
|
420
|
+
throw new Error(`No pending approval for toolCallId ${input.toolCallId} on run ${input.runId}`);
|
|
421
|
+
}
|
|
422
|
+
await aiRunState.resolveApproval(input.toolCallId, input.approved ? 'approved' : 'denied');
|
|
423
|
+
const { agent, packageName, resolvedName } = resolveAgent(run.agentName);
|
|
424
|
+
const { storage } = resolveMemoryServices(agent, singletonServices);
|
|
425
|
+
const memoryConfig = agent.memory;
|
|
426
|
+
const agentRunner = singletonServices.aiAgentRunner;
|
|
427
|
+
if (!agentRunner) {
|
|
428
|
+
throw new Error('AIAgentRunnerService not available in singletonServices');
|
|
429
|
+
}
|
|
430
|
+
if (!input.approved) {
|
|
431
|
+
if (pending.type === 'agent-call') {
|
|
432
|
+
await aiRunState.updateRun(pending.agentRunId, { status: 'failed' });
|
|
433
|
+
}
|
|
434
|
+
const denialResult = 'Tool call was not approved by the user';
|
|
435
|
+
if (storage) {
|
|
436
|
+
await storage.saveMessages(run.threadId, [
|
|
437
|
+
{
|
|
438
|
+
id: randomUUID(),
|
|
439
|
+
role: 'tool',
|
|
440
|
+
toolResults: [
|
|
441
|
+
{
|
|
442
|
+
id: input.toolCallId,
|
|
443
|
+
name: pending.type === 'tool-call'
|
|
444
|
+
? pending.toolName
|
|
445
|
+
: pending.agentName,
|
|
446
|
+
result: denialResult,
|
|
447
|
+
},
|
|
448
|
+
],
|
|
449
|
+
createdAt: new Date(),
|
|
450
|
+
},
|
|
451
|
+
]);
|
|
452
|
+
}
|
|
453
|
+
await aiRunState.updateRun(run.runId, { status: 'running' });
|
|
454
|
+
await continueAfterToolResult(run, agent, packageName, resolvedName, storage, memoryConfig, agentRunner, channel, params, aiRunState, options);
|
|
455
|
+
return;
|
|
456
|
+
}
|
|
457
|
+
if (pending.type === 'agent-call') {
|
|
458
|
+
const subRun = await aiRunState.getRun(pending.agentRunId);
|
|
459
|
+
if (!subRun) {
|
|
460
|
+
throw new Error(`Sub-agent run not found: ${pending.agentRunId}`);
|
|
461
|
+
}
|
|
462
|
+
const subPending = subRun.pendingApprovals?.[0];
|
|
463
|
+
if (!subPending) {
|
|
464
|
+
throw new Error(`No pending approval on sub-agent run ${pending.agentRunId}`);
|
|
465
|
+
}
|
|
466
|
+
const subChannel = createScopedChannel(channel, subRun.agentName, 'resume');
|
|
467
|
+
channel.send({
|
|
468
|
+
type: 'agent-call',
|
|
469
|
+
agentName: subRun.agentName,
|
|
470
|
+
session: 'resume',
|
|
471
|
+
input: null,
|
|
472
|
+
});
|
|
473
|
+
await resumeAIAgent({
|
|
474
|
+
runId: pending.agentRunId,
|
|
475
|
+
toolCallId: subPending.toolCallId,
|
|
476
|
+
approved: true,
|
|
477
|
+
}, subChannel, params, options);
|
|
478
|
+
channel.send({
|
|
479
|
+
type: 'agent-result',
|
|
480
|
+
agentName: subRun.agentName,
|
|
481
|
+
session: 'resume',
|
|
482
|
+
result: null,
|
|
483
|
+
});
|
|
484
|
+
if (storage) {
|
|
485
|
+
await storage.saveMessages(run.threadId, [
|
|
486
|
+
{
|
|
487
|
+
id: randomUUID(),
|
|
488
|
+
role: 'tool',
|
|
489
|
+
toolResults: [
|
|
490
|
+
{
|
|
491
|
+
id: input.toolCallId,
|
|
492
|
+
name: pending.agentName,
|
|
493
|
+
result: 'Sub-agent completed successfully',
|
|
494
|
+
},
|
|
495
|
+
],
|
|
496
|
+
createdAt: new Date(),
|
|
497
|
+
},
|
|
498
|
+
]);
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
else {
|
|
502
|
+
const streamContext = {
|
|
503
|
+
channel,
|
|
504
|
+
options: { ...options, requiresToolApproval: false },
|
|
505
|
+
};
|
|
506
|
+
const aiMiddlewaresForResume = agent.aiMiddleware ?? [];
|
|
507
|
+
const { tools } = buildToolDefs(params, new Map(), run.resourceId, resolvedName, packageName, streamContext, aiMiddlewaresForResume);
|
|
508
|
+
const matchingTool = tools.find((t) => t.name === pending.toolName);
|
|
509
|
+
if (!matchingTool) {
|
|
510
|
+
throw new Error(`Tool "${pending.toolName}" not found in agent definition`);
|
|
511
|
+
}
|
|
512
|
+
const toolArgs = typeof pending.args === 'string' ? JSON.parse(pending.args) : pending.args;
|
|
513
|
+
let toolResult;
|
|
514
|
+
try {
|
|
515
|
+
toolResult = await matchingTool.execute(toolArgs);
|
|
516
|
+
}
|
|
517
|
+
catch (execErr) {
|
|
518
|
+
toolResult = `Error: ${execErr instanceof Error ? execErr.message : String(execErr)}`;
|
|
519
|
+
}
|
|
520
|
+
const resultStr = typeof toolResult === 'string' ? toolResult : JSON.stringify(toolResult);
|
|
521
|
+
if (storage) {
|
|
522
|
+
await storage.saveMessages(run.threadId, [
|
|
523
|
+
{
|
|
524
|
+
id: randomUUID(),
|
|
525
|
+
role: 'tool',
|
|
526
|
+
toolResults: [
|
|
527
|
+
{
|
|
528
|
+
id: input.toolCallId,
|
|
529
|
+
name: pending.toolName,
|
|
530
|
+
result: resultStr,
|
|
531
|
+
},
|
|
532
|
+
],
|
|
533
|
+
createdAt: new Date(),
|
|
534
|
+
},
|
|
535
|
+
]);
|
|
536
|
+
}
|
|
537
|
+
channel.send({
|
|
538
|
+
type: 'tool-result',
|
|
539
|
+
toolCallId: input.toolCallId,
|
|
540
|
+
toolName: pending.toolName,
|
|
541
|
+
result: toolResult,
|
|
542
|
+
});
|
|
543
|
+
}
|
|
544
|
+
await aiRunState.updateRun(run.runId, { status: 'running' });
|
|
545
|
+
await continueAfterToolResult(run, agent, packageName, resolvedName, storage, memoryConfig, agentRunner, channel, params, aiRunState, options);
|
|
546
|
+
}
|
|
547
|
+
async function continueAfterToolResult(run, agent, packageName, resolvedName, storage, memoryConfig, agentRunner, channel, params, aiRunState, options) {
|
|
548
|
+
const singletonServices = getSingletonServices();
|
|
549
|
+
const agentsMeta = pikkuState(packageName, 'agent', 'agentsMeta');
|
|
550
|
+
const meta = agentsMeta[resolvedName];
|
|
551
|
+
const workingMemorySchemaName = meta?.workingMemorySchema ?? null;
|
|
552
|
+
const messages = storage
|
|
553
|
+
? await storage.getMessages(run.threadId, {
|
|
554
|
+
lastN: memoryConfig?.lastMessages ?? 20,
|
|
555
|
+
})
|
|
556
|
+
: [];
|
|
557
|
+
const workingMemoryJsonSchema = workingMemorySchemaName
|
|
558
|
+
? pikkuState(packageName, 'misc', 'schemas').get(workingMemorySchemaName)
|
|
559
|
+
: undefined;
|
|
560
|
+
const contextMessages = await loadContextMessages(memoryConfig, storage, { message: '', threadId: run.threadId, resourceId: run.resourceId }, workingMemoryJsonSchema);
|
|
561
|
+
const allMessages = [...contextMessages, ...messages];
|
|
562
|
+
const trimmedMessages = trimMessages(allMessages);
|
|
563
|
+
const instructions = buildInstructions(resolvedName, packageName);
|
|
564
|
+
const aiMiddlewares = agent.aiMiddleware ?? [];
|
|
565
|
+
let modifiedMessages = trimmedMessages;
|
|
566
|
+
let modifiedInstructions = instructions;
|
|
567
|
+
for (const mw of aiMiddlewares) {
|
|
568
|
+
if (mw.modifyInput) {
|
|
569
|
+
const result = await mw.modifyInput(singletonServices, {
|
|
570
|
+
messages: modifiedMessages,
|
|
571
|
+
instructions: modifiedInstructions,
|
|
572
|
+
});
|
|
573
|
+
modifiedMessages = result.messages;
|
|
574
|
+
modifiedInstructions = result.instructions;
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
const streamMiddleware = aiMiddlewares
|
|
578
|
+
.filter((mw) => mw.modifyOutputStream)
|
|
579
|
+
.map((mw) => {
|
|
580
|
+
const state = {};
|
|
581
|
+
const allEvents = [];
|
|
582
|
+
return async (services, event, next) => {
|
|
583
|
+
allEvents.push(event);
|
|
584
|
+
const result = await mw.modifyOutputStream(services, {
|
|
585
|
+
event,
|
|
586
|
+
allEvents,
|
|
587
|
+
state,
|
|
588
|
+
});
|
|
589
|
+
if (result != null)
|
|
590
|
+
await next(result);
|
|
591
|
+
};
|
|
592
|
+
});
|
|
593
|
+
const allChannelMiddleware = combineChannelMiddleware('agent', `stream:${run.agentName}`, {
|
|
594
|
+
wireInheritedChannelMiddleware: meta?.channelMiddleware,
|
|
595
|
+
wireChannelMiddleware: [
|
|
596
|
+
...(agent.channelMiddleware ?? []),
|
|
597
|
+
...streamMiddleware,
|
|
598
|
+
],
|
|
599
|
+
});
|
|
600
|
+
const wrappedChannel = allChannelMiddleware.length > 0
|
|
601
|
+
? wrapChannelWithMiddleware({ channel }, singletonServices, allChannelMiddleware).channel
|
|
602
|
+
: channel;
|
|
603
|
+
const persistingChannel = createPersistingChannel(wrappedChannel, storage, run.threadId);
|
|
604
|
+
const streamContext = { channel, options };
|
|
605
|
+
const resumeTools = buildToolDefs(params, new Map(), run.resourceId, resolvedName, packageName, streamContext, aiMiddlewares).tools;
|
|
606
|
+
const resolved = resolveModelConfig(resolvedName, agent);
|
|
607
|
+
const maxSteps = resolved.maxSteps ?? 10;
|
|
608
|
+
const runnerParams = {
|
|
609
|
+
model: resolved.model,
|
|
610
|
+
temperature: resolved.temperature,
|
|
611
|
+
instructions: modifiedInstructions,
|
|
612
|
+
messages: modifiedMessages,
|
|
613
|
+
tools: resumeTools,
|
|
614
|
+
maxSteps: 1,
|
|
615
|
+
toolChoice: (agent.toolChoice ?? 'auto'),
|
|
616
|
+
outputSchema: meta?.outputSchema
|
|
617
|
+
? pikkuState(packageName, 'misc', 'schemas').get(meta.outputSchema)
|
|
618
|
+
: undefined,
|
|
619
|
+
};
|
|
620
|
+
try {
|
|
621
|
+
const loopResult = await runStreamStepLoop({
|
|
622
|
+
agent,
|
|
623
|
+
runnerParams,
|
|
624
|
+
maxSteps,
|
|
625
|
+
agentRunner,
|
|
626
|
+
persistingChannel,
|
|
627
|
+
channel,
|
|
628
|
+
runId: run.runId,
|
|
629
|
+
aiMiddlewares,
|
|
630
|
+
});
|
|
631
|
+
if (loopResult.outcome === 'approval') {
|
|
632
|
+
await handleApproval(loopResult.approval, run.runId, channel, aiRunState, persistingChannel);
|
|
633
|
+
return;
|
|
634
|
+
}
|
|
635
|
+
await postStreamCleanup(persistingChannel, aiMiddlewares, singletonServices, storage, memoryConfig, run.threadId, workingMemorySchemaName, runnerParams.messages, aiRunState, run.runId);
|
|
636
|
+
channel.send({ type: 'done' });
|
|
637
|
+
channel.close();
|
|
638
|
+
}
|
|
639
|
+
catch (err) {
|
|
640
|
+
for (const mw of aiMiddlewares) {
|
|
641
|
+
if (mw.onError) {
|
|
642
|
+
try {
|
|
643
|
+
await mw.onError(singletonServices, {
|
|
644
|
+
error: err instanceof Error ? err : new Error(String(err)),
|
|
645
|
+
stepNumber: -1,
|
|
646
|
+
messages: runnerParams.messages,
|
|
647
|
+
});
|
|
648
|
+
}
|
|
649
|
+
catch {
|
|
650
|
+
// onError hooks must not affect error flow
|
|
651
|
+
}
|
|
652
|
+
}
|
|
653
|
+
}
|
|
654
|
+
await aiRunState.updateRun(run.runId, { status: 'failed' });
|
|
655
|
+
channel.send({
|
|
656
|
+
type: 'error',
|
|
657
|
+
message: err instanceof Error ? err.message : String(err),
|
|
658
|
+
});
|
|
659
|
+
channel.send({ type: 'done' });
|
|
660
|
+
channel.close();
|
|
661
|
+
}
|
|
662
|
+
}
|