@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
package/dist/utils.d.ts
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
|
-
import { Logger } from './services/logger.js';
|
|
2
|
-
import {
|
|
3
|
-
export declare const closeWireServices: (logger: Logger, wireServices:
|
|
1
|
+
import type { Logger } from './services/logger.js';
|
|
2
|
+
import type { WireServices } from './types/core.types.js';
|
|
3
|
+
export declare const closeWireServices: (logger: Logger, wireServices: WireServices) => Promise<void>;
|
|
4
4
|
export declare const createWeakUID: () => string;
|
|
5
5
|
export declare const isSerializable: (data: any) => boolean;
|
|
6
|
+
export declare const getTagGroups: <T>(tagGroups: Record<string, T>, tag: string) => T[];
|
|
6
7
|
export declare const freezeDedupe: <T>(arr?: readonly T[] | T[] | undefined) => readonly T[];
|
|
7
8
|
/**
|
|
8
|
-
* Stop all singleton services, including
|
|
9
|
-
*
|
|
9
|
+
* Stop all singleton services, including addon package services.
|
|
10
|
+
* Addon package services are stopped first, then the parent services.
|
|
10
11
|
*
|
|
11
12
|
* @param singletonServices - The parent singleton services to stop
|
|
12
13
|
*/
|
|
13
|
-
export declare const stopSingletonServices: (
|
|
14
|
+
export declare const stopSingletonServices: () => Promise<void>;
|
package/dist/utils.js
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
// but is an issue for the future and will be tackled
|
|
3
|
-
// with dependency injection
|
|
1
|
+
import { getSingletonServices, getAllPackageStates } from './pikku-state.js';
|
|
4
2
|
export const closeWireServices = async (logger, wireServices) => {
|
|
5
3
|
await Promise.all(Object.values(wireServices).map(async (service) => {
|
|
6
4
|
if (service?.close) {
|
|
@@ -13,9 +11,9 @@ export const closeWireServices = async (logger, wireServices) => {
|
|
|
13
11
|
}
|
|
14
12
|
}));
|
|
15
13
|
};
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
}
|
|
14
|
+
const _uidPrefix = Date.now().toString(36);
|
|
15
|
+
let _uidCounter = 0;
|
|
16
|
+
export const createWeakUID = () => `${_uidPrefix}-${++_uidCounter}`;
|
|
19
17
|
export const isSerializable = (data) => {
|
|
20
18
|
return !(typeof data === 'string' ||
|
|
21
19
|
data instanceof ArrayBuffer ||
|
|
@@ -28,6 +26,21 @@ export const isSerializable = (data) => {
|
|
|
28
26
|
data instanceof Float32Array ||
|
|
29
27
|
data instanceof Float64Array);
|
|
30
28
|
};
|
|
29
|
+
export const getTagGroups = (tagGroups, tag) => {
|
|
30
|
+
const results = [];
|
|
31
|
+
const exact = tagGroups[tag];
|
|
32
|
+
if (exact)
|
|
33
|
+
results.push(exact);
|
|
34
|
+
let colonIdx = tag.lastIndexOf(':');
|
|
35
|
+
while (colonIdx !== -1) {
|
|
36
|
+
const parent = tag.slice(0, colonIdx);
|
|
37
|
+
const group = tagGroups[parent];
|
|
38
|
+
if (group)
|
|
39
|
+
results.push(group);
|
|
40
|
+
colonIdx = parent.lastIndexOf(':');
|
|
41
|
+
}
|
|
42
|
+
return results;
|
|
43
|
+
};
|
|
31
44
|
const EMPTY_ARRAY = Object.freeze([]);
|
|
32
45
|
export const freezeDedupe = (arr) => {
|
|
33
46
|
if (!arr || arr.length === 0)
|
|
@@ -61,16 +74,18 @@ const stopService = async (logger, name, service) => {
|
|
|
61
74
|
}
|
|
62
75
|
};
|
|
63
76
|
/**
|
|
64
|
-
* Stop all singleton services, including
|
|
65
|
-
*
|
|
77
|
+
* Stop all singleton services, including addon package services.
|
|
78
|
+
* Addon package services are stopped first, then the parent services.
|
|
66
79
|
*
|
|
67
80
|
* @param singletonServices - The parent singleton services to stop
|
|
68
81
|
*/
|
|
69
|
-
export const stopSingletonServices = async (
|
|
82
|
+
export const stopSingletonServices = async () => {
|
|
83
|
+
const singletonServices = getSingletonServices();
|
|
70
84
|
const logger = singletonServices.logger;
|
|
71
|
-
// First, stop all
|
|
72
|
-
|
|
73
|
-
|
|
85
|
+
// First, stop all addon package singleton services
|
|
86
|
+
const stateMap = getAllPackageStates();
|
|
87
|
+
if (stateMap.size > 0) {
|
|
88
|
+
for (const [packageName, packageState] of stateMap) {
|
|
74
89
|
// Skip main package - we handle it separately
|
|
75
90
|
if (packageName === '__main__')
|
|
76
91
|
continue;
|
package/dist/version.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
const VERSION_SEPARATOR = '@v';
|
|
2
|
+
export function formatVersionedId(baseName, version) {
|
|
3
|
+
return `${baseName}${VERSION_SEPARATOR}${version}`;
|
|
4
|
+
}
|
|
5
|
+
export function parseVersionedId(id) {
|
|
6
|
+
const idx = id.lastIndexOf(VERSION_SEPARATOR);
|
|
7
|
+
if (idx === -1) {
|
|
8
|
+
return { baseName: id, version: null };
|
|
9
|
+
}
|
|
10
|
+
const versionStr = id.slice(idx + VERSION_SEPARATOR.length);
|
|
11
|
+
const version = Number(versionStr);
|
|
12
|
+
if (!Number.isInteger(version) || version < 1) {
|
|
13
|
+
return { baseName: id, version: null };
|
|
14
|
+
}
|
|
15
|
+
return { baseName: id.slice(0, idx), version };
|
|
16
|
+
}
|
|
17
|
+
export function isVersionedId(id) {
|
|
18
|
+
return parseVersionedId(id).version !== null;
|
|
19
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { AIStreamChannel, AIAgentInput } from './ai-agent.types.js';
|
|
2
|
+
export declare function createAssistantUIChannel(parent: AIStreamChannel): AIStreamChannel;
|
|
3
|
+
export declare function parseAssistantUIInput(input: Record<string, unknown>, defaults?: {
|
|
4
|
+
resourceId?: string;
|
|
5
|
+
}): AIAgentInput;
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { randomUUID } from 'crypto';
|
|
2
|
+
export function createAssistantUIChannel(parent) {
|
|
3
|
+
const messageId = randomUUID();
|
|
4
|
+
let started = false;
|
|
5
|
+
let totalInputTokens = 0;
|
|
6
|
+
let totalOutputTokens = 0;
|
|
7
|
+
let pendingToolResults = [];
|
|
8
|
+
let hasToolCalls = false;
|
|
9
|
+
let hasPendingApproval = false;
|
|
10
|
+
const sendChunk = (chunk) => {
|
|
11
|
+
parent.send(chunk);
|
|
12
|
+
};
|
|
13
|
+
const ensureStarted = () => {
|
|
14
|
+
if (!started) {
|
|
15
|
+
started = true;
|
|
16
|
+
sendChunk({ type: 'start', messageId });
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
const flushToolResults = () => {
|
|
20
|
+
for (const chunk of pendingToolResults) {
|
|
21
|
+
sendChunk(chunk);
|
|
22
|
+
}
|
|
23
|
+
pendingToolResults = [];
|
|
24
|
+
};
|
|
25
|
+
return {
|
|
26
|
+
channelId: parent.channelId,
|
|
27
|
+
openingData: parent.openingData,
|
|
28
|
+
get state() {
|
|
29
|
+
return parent.state;
|
|
30
|
+
},
|
|
31
|
+
close: () => parent.close(),
|
|
32
|
+
send: (event) => {
|
|
33
|
+
switch (event.type) {
|
|
34
|
+
case 'text-delta':
|
|
35
|
+
ensureStarted();
|
|
36
|
+
sendChunk({ type: 'text-delta', textDelta: event.text });
|
|
37
|
+
break;
|
|
38
|
+
case 'reasoning-delta':
|
|
39
|
+
ensureStarted();
|
|
40
|
+
sendChunk({ type: 'reasoning-delta', delta: event.text });
|
|
41
|
+
break;
|
|
42
|
+
case 'tool-call':
|
|
43
|
+
ensureStarted();
|
|
44
|
+
hasToolCalls = true;
|
|
45
|
+
sendChunk({
|
|
46
|
+
type: 'tool-call-start',
|
|
47
|
+
id: event.toolCallId,
|
|
48
|
+
toolCallId: event.toolCallId,
|
|
49
|
+
toolName: event.toolName,
|
|
50
|
+
});
|
|
51
|
+
sendChunk({
|
|
52
|
+
type: 'tool-call-delta',
|
|
53
|
+
argsText: typeof event.args === 'string'
|
|
54
|
+
? event.args
|
|
55
|
+
: JSON.stringify(event.args),
|
|
56
|
+
});
|
|
57
|
+
sendChunk({ type: 'tool-call-end' });
|
|
58
|
+
break;
|
|
59
|
+
case 'tool-result':
|
|
60
|
+
ensureStarted();
|
|
61
|
+
pendingToolResults.push({
|
|
62
|
+
type: 'tool-result',
|
|
63
|
+
toolCallId: event.toolCallId,
|
|
64
|
+
result: event.result,
|
|
65
|
+
});
|
|
66
|
+
break;
|
|
67
|
+
case 'agent-call':
|
|
68
|
+
case 'agent-result':
|
|
69
|
+
break;
|
|
70
|
+
case 'usage': {
|
|
71
|
+
ensureStarted();
|
|
72
|
+
totalInputTokens += event.tokens.input;
|
|
73
|
+
totalOutputTokens += event.tokens.output;
|
|
74
|
+
const hasPending = pendingToolResults.length > 0;
|
|
75
|
+
sendChunk({
|
|
76
|
+
type: 'finish-step',
|
|
77
|
+
finishReason: hasToolCalls ? 'tool-calls' : 'stop',
|
|
78
|
+
usage: {
|
|
79
|
+
promptTokens: event.tokens.input,
|
|
80
|
+
completionTokens: event.tokens.output,
|
|
81
|
+
},
|
|
82
|
+
isContinued: hasPending,
|
|
83
|
+
});
|
|
84
|
+
flushToolResults();
|
|
85
|
+
hasToolCalls = false;
|
|
86
|
+
break;
|
|
87
|
+
}
|
|
88
|
+
case 'error':
|
|
89
|
+
ensureStarted();
|
|
90
|
+
sendChunk({ type: 'error', errorText: event.message });
|
|
91
|
+
break;
|
|
92
|
+
case 'approval-request':
|
|
93
|
+
ensureStarted();
|
|
94
|
+
hasPendingApproval = true;
|
|
95
|
+
sendChunk({ type: 'data-approval-request', data: event });
|
|
96
|
+
break;
|
|
97
|
+
case 'suspended':
|
|
98
|
+
ensureStarted();
|
|
99
|
+
sendChunk({ type: 'data-suspended', data: event });
|
|
100
|
+
break;
|
|
101
|
+
case 'done':
|
|
102
|
+
ensureStarted();
|
|
103
|
+
flushToolResults();
|
|
104
|
+
sendChunk({
|
|
105
|
+
type: 'finish',
|
|
106
|
+
finishReason: hasPendingApproval ? 'tool-calls' : 'stop',
|
|
107
|
+
usage: {
|
|
108
|
+
promptTokens: totalInputTokens,
|
|
109
|
+
completionTokens: totalOutputTokens,
|
|
110
|
+
},
|
|
111
|
+
});
|
|
112
|
+
parent.send('[DONE]');
|
|
113
|
+
parent.close();
|
|
114
|
+
break;
|
|
115
|
+
}
|
|
116
|
+
},
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
export function parseAssistantUIInput(input, defaults) {
|
|
120
|
+
const messages = input.messages;
|
|
121
|
+
if (!messages || !Array.isArray(messages) || messages.length === 0) {
|
|
122
|
+
throw new Error('assistant-ui input must contain a non-empty messages array');
|
|
123
|
+
}
|
|
124
|
+
const lastUserMessage = [...messages].reverse().find((m) => m.role === 'user');
|
|
125
|
+
if (!lastUserMessage) {
|
|
126
|
+
throw new Error('No user message found in assistant-ui input');
|
|
127
|
+
}
|
|
128
|
+
let messageText;
|
|
129
|
+
if (typeof lastUserMessage.content === 'string') {
|
|
130
|
+
messageText = lastUserMessage.content;
|
|
131
|
+
}
|
|
132
|
+
else if (Array.isArray(lastUserMessage.parts)) {
|
|
133
|
+
const textPart = lastUserMessage.parts.find((p) => p.type === 'text');
|
|
134
|
+
messageText = textPart ? String(textPart.text) : '';
|
|
135
|
+
}
|
|
136
|
+
else if (Array.isArray(lastUserMessage.content)) {
|
|
137
|
+
const textPart = lastUserMessage.content.find((p) => p.type === 'text');
|
|
138
|
+
messageText = textPart ? String(textPart.text) : '';
|
|
139
|
+
}
|
|
140
|
+
else {
|
|
141
|
+
messageText = '';
|
|
142
|
+
}
|
|
143
|
+
const threadId = input.threadId ?? input.id ?? randomUUID();
|
|
144
|
+
const resourceId = defaults?.resourceId ?? 'default';
|
|
145
|
+
return { message: messageText, threadId, resourceId };
|
|
146
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
export declare function agent<TAgentMap extends Record<string, {
|
|
2
|
+
output: any;
|
|
3
|
+
}>>(agentName: string & keyof TAgentMap): {
|
|
4
|
+
func: (services: any, data: any, wire: any) => Promise<any>;
|
|
5
|
+
};
|
|
6
|
+
export declare function agentStream<TAgentMap extends Record<string, {
|
|
7
|
+
output: any;
|
|
8
|
+
}>>(agentName?: string & keyof TAgentMap): {
|
|
9
|
+
func: (services: any, data: any, wire: any) => Promise<void>;
|
|
10
|
+
};
|
|
11
|
+
export declare function agentResume(): {
|
|
12
|
+
func: (services: any, data: {
|
|
13
|
+
runId: string;
|
|
14
|
+
toolCallId: string;
|
|
15
|
+
approved: boolean;
|
|
16
|
+
}, wire: any) => Promise<void>;
|
|
17
|
+
};
|
|
18
|
+
export declare function agentApprove<TAgentMap extends Record<string, {
|
|
19
|
+
output: any;
|
|
20
|
+
}>>(_agentName: string & keyof TAgentMap): {
|
|
21
|
+
func: (services: any, data: {
|
|
22
|
+
runId: string;
|
|
23
|
+
approvals: {
|
|
24
|
+
toolCallId: string;
|
|
25
|
+
approved: boolean;
|
|
26
|
+
}[];
|
|
27
|
+
}, wire: any) => Promise<any>;
|
|
28
|
+
};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
export function agent(agentName) {
|
|
2
|
+
return {
|
|
3
|
+
func: async (_services, data, { rpc }) => {
|
|
4
|
+
return rpc.agent.run(agentName, data);
|
|
5
|
+
},
|
|
6
|
+
};
|
|
7
|
+
}
|
|
8
|
+
export function agentStream(agentName) {
|
|
9
|
+
return {
|
|
10
|
+
func: async (_services, data, { rpc }) => {
|
|
11
|
+
if (agentName) {
|
|
12
|
+
await rpc.agent.stream(agentName, data);
|
|
13
|
+
}
|
|
14
|
+
else {
|
|
15
|
+
const { agentName: name, ...rest } = data;
|
|
16
|
+
if (!name) {
|
|
17
|
+
throw new Error('agentStream requires an agentName either as a parameter or in the input data');
|
|
18
|
+
}
|
|
19
|
+
await rpc.agent.stream(name, rest);
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
export function agentResume() {
|
|
25
|
+
return {
|
|
26
|
+
func: async (_services, data, { rpc }) => {
|
|
27
|
+
await rpc.agent.resume(data.runId, {
|
|
28
|
+
toolCallId: data.toolCallId,
|
|
29
|
+
approved: data.approved,
|
|
30
|
+
});
|
|
31
|
+
},
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
export function agentApprove(_agentName) {
|
|
35
|
+
return {
|
|
36
|
+
func: async (_services, data, { rpc }) => {
|
|
37
|
+
return rpc.agent.approve(data.runId, data.approvals);
|
|
38
|
+
},
|
|
39
|
+
};
|
|
40
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { CoreSingletonServices } from '../../types/core.types.js';
|
|
2
|
+
import type { CoreAIAgent, AIAgentMemoryConfig, AIAgentInput, AIMessage } from './ai-agent.types.js';
|
|
3
|
+
import type { AIStorageService } from '../../services/ai-storage-service.js';
|
|
4
|
+
import type { Logger } from '../../services/logger.js';
|
|
5
|
+
import type { SchemaService } from '../../services/schema-service.js';
|
|
6
|
+
export declare function resolveMemoryServices(agent: CoreAIAgent, singletonServices: CoreSingletonServices): {
|
|
7
|
+
storage: AIStorageService | undefined;
|
|
8
|
+
};
|
|
9
|
+
export declare function deepMergeWorkingMemory(existing: Record<string, unknown>, updates: Record<string, unknown>): Record<string, unknown>;
|
|
10
|
+
export declare function buildWorkingMemoryPrompt(currentState: Record<string, unknown> | null, jsonSchema?: Record<string, unknown>): string;
|
|
11
|
+
export declare function loadContextMessages(memoryConfig: AIAgentMemoryConfig | undefined, storage: AIStorageService | undefined, input: AIAgentInput, workingMemoryJsonSchema?: Record<string, unknown>): Promise<AIMessage[]>;
|
|
12
|
+
export declare function saveMessages(storage: AIStorageService | undefined, threadId: string, resourceId: string, memoryConfig: AIAgentMemoryConfig | undefined, userMessage: AIMessage, result: {
|
|
13
|
+
text: string;
|
|
14
|
+
steps: {
|
|
15
|
+
toolCalls?: {
|
|
16
|
+
name: string;
|
|
17
|
+
args: Record<string, unknown>;
|
|
18
|
+
result: string;
|
|
19
|
+
}[];
|
|
20
|
+
}[];
|
|
21
|
+
}, options?: {
|
|
22
|
+
workingMemoryJsonSchema?: Record<string, unknown>;
|
|
23
|
+
workingMemorySchemaName?: string | null;
|
|
24
|
+
logger?: Logger;
|
|
25
|
+
schemaService?: SchemaService;
|
|
26
|
+
}): Promise<string>;
|
|
27
|
+
export declare function trimMessages(messages: AIMessage[], maxTokenBudget?: number): AIMessage[];
|
|
28
|
+
export declare function parseWorkingMemory(text: string): Record<string, unknown> | null;
|
|
29
|
+
export declare function stripWorkingMemoryTags(text: string): string;
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
import { randomUUID } from 'crypto';
|
|
2
|
+
export function resolveMemoryServices(agent, singletonServices) {
|
|
3
|
+
const memoryConfig = agent.memory;
|
|
4
|
+
const storage = memoryConfig?.storage
|
|
5
|
+
? singletonServices[memoryConfig.storage]
|
|
6
|
+
: singletonServices.aiStorage;
|
|
7
|
+
return { storage };
|
|
8
|
+
}
|
|
9
|
+
export function deepMergeWorkingMemory(existing, updates) {
|
|
10
|
+
const result = { ...existing };
|
|
11
|
+
for (const key of Object.keys(updates)) {
|
|
12
|
+
const value = updates[key];
|
|
13
|
+
if (value === null) {
|
|
14
|
+
delete result[key];
|
|
15
|
+
}
|
|
16
|
+
else if (typeof value === 'object' &&
|
|
17
|
+
!Array.isArray(value) &&
|
|
18
|
+
typeof result[key] === 'object' &&
|
|
19
|
+
result[key] !== null &&
|
|
20
|
+
!Array.isArray(result[key])) {
|
|
21
|
+
result[key] = deepMergeWorkingMemory(result[key], value);
|
|
22
|
+
}
|
|
23
|
+
else {
|
|
24
|
+
result[key] = value;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return result;
|
|
28
|
+
}
|
|
29
|
+
export function buildWorkingMemoryPrompt(currentState, jsonSchema) {
|
|
30
|
+
const parts = [];
|
|
31
|
+
if (jsonSchema?.properties) {
|
|
32
|
+
const props = jsonSchema.properties;
|
|
33
|
+
const fieldLines = Object.entries(props).map(([name, def]) => {
|
|
34
|
+
const type = def.type ?? 'unknown';
|
|
35
|
+
const desc = def.description ? ` - ${def.description}` : '';
|
|
36
|
+
return ` - ${name} (${type})${desc}`;
|
|
37
|
+
});
|
|
38
|
+
if (fieldLines.length > 0) {
|
|
39
|
+
parts.push(`Working memory fields:\n${fieldLines.join('\n')}`);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
if (currentState && Object.keys(currentState).length > 0) {
|
|
43
|
+
parts.push(`Current working memory:\n${JSON.stringify(currentState)}`);
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
parts.push('Current working memory: (empty)');
|
|
47
|
+
}
|
|
48
|
+
parts.push('When you learn new information, output a partial JSON update in <working_memory> tags. ' +
|
|
49
|
+
'Only include changed fields. Set a field to null to delete it.');
|
|
50
|
+
return parts.join('\n\n');
|
|
51
|
+
}
|
|
52
|
+
export async function loadContextMessages(memoryConfig, storage, input, workingMemoryJsonSchema) {
|
|
53
|
+
const contextMessages = [];
|
|
54
|
+
if (memoryConfig?.workingMemory && storage) {
|
|
55
|
+
const workingMem = await storage.getWorkingMemory(input.threadId, 'thread');
|
|
56
|
+
const prompt = buildWorkingMemoryPrompt(workingMem, workingMemoryJsonSchema);
|
|
57
|
+
contextMessages.push({
|
|
58
|
+
id: `wm-${randomUUID()}`,
|
|
59
|
+
role: 'system',
|
|
60
|
+
content: prompt,
|
|
61
|
+
createdAt: new Date(),
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
return contextMessages;
|
|
65
|
+
}
|
|
66
|
+
export async function saveMessages(storage, threadId, resourceId, memoryConfig, userMessage, result, options) {
|
|
67
|
+
let responseText = result.text;
|
|
68
|
+
if (storage) {
|
|
69
|
+
const newMessages = [userMessage];
|
|
70
|
+
for (const step of result.steps) {
|
|
71
|
+
if (step.toolCalls?.length) {
|
|
72
|
+
newMessages.push({
|
|
73
|
+
id: randomUUID(),
|
|
74
|
+
role: 'assistant',
|
|
75
|
+
toolCalls: step.toolCalls.map((tc) => ({
|
|
76
|
+
id: randomUUID(),
|
|
77
|
+
name: tc.name,
|
|
78
|
+
args: tc.args,
|
|
79
|
+
})),
|
|
80
|
+
createdAt: new Date(),
|
|
81
|
+
});
|
|
82
|
+
newMessages.push({
|
|
83
|
+
id: randomUUID(),
|
|
84
|
+
role: 'tool',
|
|
85
|
+
toolResults: step.toolCalls.map((tc) => ({
|
|
86
|
+
id: randomUUID(),
|
|
87
|
+
name: tc.name,
|
|
88
|
+
result: tc.result,
|
|
89
|
+
})),
|
|
90
|
+
createdAt: new Date(),
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
newMessages.push({
|
|
95
|
+
id: randomUUID(),
|
|
96
|
+
role: 'assistant',
|
|
97
|
+
content: responseText,
|
|
98
|
+
createdAt: new Date(),
|
|
99
|
+
});
|
|
100
|
+
await storage.saveMessages(threadId, newMessages);
|
|
101
|
+
if (memoryConfig?.workingMemory) {
|
|
102
|
+
const parsed = parseWorkingMemory(responseText);
|
|
103
|
+
if (parsed) {
|
|
104
|
+
const existing = (await storage.getWorkingMemory(threadId, 'thread')) ?? {};
|
|
105
|
+
const merged = deepMergeWorkingMemory(existing, parsed);
|
|
106
|
+
if (options?.schemaService && options?.workingMemorySchemaName) {
|
|
107
|
+
try {
|
|
108
|
+
await options.schemaService.validateSchema(options.workingMemorySchemaName, merged);
|
|
109
|
+
}
|
|
110
|
+
catch (err) {
|
|
111
|
+
options.logger?.warn(`Working memory validation failed: ${err instanceof Error ? err.message : String(err)}`);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
await storage.saveWorkingMemory(threadId, 'thread', merged);
|
|
115
|
+
responseText = stripWorkingMemoryTags(responseText);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return responseText;
|
|
120
|
+
}
|
|
121
|
+
export function trimMessages(messages, maxTokenBudget = 100000) {
|
|
122
|
+
const sanitized = sanitizeToolMessages(messages);
|
|
123
|
+
let estimatedTokens = 0;
|
|
124
|
+
const result = [];
|
|
125
|
+
for (let i = sanitized.length - 1; i >= 0; i--) {
|
|
126
|
+
const msg = sanitized[i];
|
|
127
|
+
const msgTokens = estimateTokens(msg);
|
|
128
|
+
if (estimatedTokens + msgTokens > maxTokenBudget && result.length > 0) {
|
|
129
|
+
break;
|
|
130
|
+
}
|
|
131
|
+
estimatedTokens += msgTokens;
|
|
132
|
+
result.unshift(msg);
|
|
133
|
+
}
|
|
134
|
+
if (result.length > 0 &&
|
|
135
|
+
result[0].role !== 'user' &&
|
|
136
|
+
result[0].role !== 'system') {
|
|
137
|
+
const firstUserIdx = result.findIndex((m) => m.role === 'user' || m.role === 'system');
|
|
138
|
+
if (firstUserIdx > 0) {
|
|
139
|
+
return result.slice(firstUserIdx);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
return result;
|
|
143
|
+
}
|
|
144
|
+
function sanitizeToolMessages(messages) {
|
|
145
|
+
const result = [];
|
|
146
|
+
for (let i = 0; i < messages.length; i++) {
|
|
147
|
+
const msg = messages[i];
|
|
148
|
+
if (msg.role === 'assistant' && msg.toolCalls?.length) {
|
|
149
|
+
const next = messages[i + 1];
|
|
150
|
+
if (!next || next.role !== 'tool' || !next.toolResults?.length) {
|
|
151
|
+
if (msg.content) {
|
|
152
|
+
result.push({ ...msg, toolCalls: undefined });
|
|
153
|
+
}
|
|
154
|
+
continue;
|
|
155
|
+
}
|
|
156
|
+
const toolCallIds = new Set(msg.toolCalls.map((tc) => tc.id));
|
|
157
|
+
const resultIds = new Set(next.toolResults.map((tr) => tr.id));
|
|
158
|
+
const allMatched = [...toolCallIds].every((id) => resultIds.has(id));
|
|
159
|
+
if (!allMatched) {
|
|
160
|
+
if (msg.content) {
|
|
161
|
+
result.push({ ...msg, toolCalls: undefined });
|
|
162
|
+
}
|
|
163
|
+
result.push({
|
|
164
|
+
...next,
|
|
165
|
+
toolResults: next.toolResults.filter((tr) => !toolCallIds.has(tr.id)) ||
|
|
166
|
+
undefined,
|
|
167
|
+
});
|
|
168
|
+
i++;
|
|
169
|
+
continue;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
result.push(msg);
|
|
173
|
+
}
|
|
174
|
+
return result.filter((m) => m.role !== 'tool' || (m.toolResults && m.toolResults.length > 0));
|
|
175
|
+
}
|
|
176
|
+
function estimateTokens(msg) {
|
|
177
|
+
let chars = 0;
|
|
178
|
+
if (msg.content)
|
|
179
|
+
chars += msg.content.length;
|
|
180
|
+
if (msg.toolCalls)
|
|
181
|
+
chars += JSON.stringify(msg.toolCalls).length;
|
|
182
|
+
if (msg.toolResults)
|
|
183
|
+
chars += JSON.stringify(msg.toolResults).length;
|
|
184
|
+
return Math.ceil(chars / 4);
|
|
185
|
+
}
|
|
186
|
+
export function parseWorkingMemory(text) {
|
|
187
|
+
const match = text.match(/<working_memory>([\s\S]*?)<\/working_memory>/);
|
|
188
|
+
if (!match)
|
|
189
|
+
return null;
|
|
190
|
+
try {
|
|
191
|
+
return JSON.parse(match[1].trim());
|
|
192
|
+
}
|
|
193
|
+
catch {
|
|
194
|
+
return null;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
export function stripWorkingMemoryTags(text) {
|
|
198
|
+
return text.replace(/<working_memory>[\s\S]*?<\/working_memory>/g, '').trim();
|
|
199
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { pikkuState } from '../../pikku-state.js';
|
|
2
|
+
export function resolveModelConfig(agentName, agent) {
|
|
3
|
+
let config = {};
|
|
4
|
+
try {
|
|
5
|
+
config = pikkuState(null, 'models', 'config') ?? {};
|
|
6
|
+
}
|
|
7
|
+
catch { }
|
|
8
|
+
const models = config.models ?? {};
|
|
9
|
+
const defaults = config.agentDefaults;
|
|
10
|
+
const agentOverride = config.agentOverrides?.[agentName];
|
|
11
|
+
let rawModel = agentOverride?.model ?? agent.model;
|
|
12
|
+
let aliasTemperature;
|
|
13
|
+
let aliasMaxSteps;
|
|
14
|
+
if (!rawModel.includes('/')) {
|
|
15
|
+
const entry = models[rawModel];
|
|
16
|
+
if (!entry)
|
|
17
|
+
throw new Error(`Unknown model alias '${rawModel}'.`);
|
|
18
|
+
if (typeof entry === 'string') {
|
|
19
|
+
rawModel = entry;
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
rawModel = entry.model;
|
|
23
|
+
aliasTemperature = entry.temperature;
|
|
24
|
+
aliasMaxSteps = entry.maxSteps;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
const temperature = agentOverride?.temperature ??
|
|
28
|
+
aliasTemperature ??
|
|
29
|
+
defaults?.temperature ??
|
|
30
|
+
agent.temperature;
|
|
31
|
+
const maxSteps = agentOverride?.maxSteps ??
|
|
32
|
+
aliasMaxSteps ??
|
|
33
|
+
defaults?.maxSteps ??
|
|
34
|
+
agent.maxSteps;
|
|
35
|
+
return { model: rawModel, temperature, maxSteps };
|
|
36
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type { CoreUserSession } from '../../types/core.types.js';
|
|
2
|
+
import type { CoreAIAgent, AIAgentInput, AIAgentToolDef, AIMessage, AIStreamChannel, PikkuAIMiddlewareHooks } from './ai-agent.types.js';
|
|
3
|
+
import type { AIAgentRunnerParams } from '../../services/ai-agent-runner-service.js';
|
|
4
|
+
import { PikkuError } from '../../errors/error-handler.js';
|
|
5
|
+
import type { SessionService } from '../../services/user-session-service.js';
|
|
6
|
+
export type RunAIAgentParams = {
|
|
7
|
+
sessionService?: SessionService<CoreUserSession>;
|
|
8
|
+
};
|
|
9
|
+
export type StreamAIAgentOptions = {
|
|
10
|
+
requiresToolApproval?: 'all' | 'explicit' | false;
|
|
11
|
+
};
|
|
12
|
+
export declare class ToolApprovalRequired extends PikkuError {
|
|
13
|
+
readonly toolCallId: string;
|
|
14
|
+
readonly toolName: string;
|
|
15
|
+
readonly args: unknown;
|
|
16
|
+
readonly reason?: string;
|
|
17
|
+
readonly displayToolName?: string;
|
|
18
|
+
readonly displayArgs?: unknown;
|
|
19
|
+
readonly agentRunId?: string;
|
|
20
|
+
constructor(toolCallId: string, toolName: string, args: unknown, reason?: string, displayToolName?: string, displayArgs?: unknown, agentRunId?: string);
|
|
21
|
+
}
|
|
22
|
+
export type StreamContext = {
|
|
23
|
+
channel: AIStreamChannel;
|
|
24
|
+
options?: StreamAIAgentOptions;
|
|
25
|
+
};
|
|
26
|
+
export declare const resolveAgent: (agentName: string) => {
|
|
27
|
+
agent: CoreAIAgent;
|
|
28
|
+
packageName: string | null;
|
|
29
|
+
resolvedName: string;
|
|
30
|
+
};
|
|
31
|
+
export declare function buildInstructions(agentName: string, packageName: string | null): string;
|
|
32
|
+
export type ScopedChannel = AIStreamChannel & {
|
|
33
|
+
approval: {
|
|
34
|
+
toolCallId: string;
|
|
35
|
+
toolName: string;
|
|
36
|
+
args: unknown;
|
|
37
|
+
runId: string;
|
|
38
|
+
} | null;
|
|
39
|
+
};
|
|
40
|
+
export declare function createScopedChannel(parent: AIStreamChannel, agentName: string, session: string): ScopedChannel;
|
|
41
|
+
export declare function buildToolDefs(params: RunAIAgentParams, agentSessionMap: Map<string, string>, resourceId: string, agentName: string, packageName: string | null, streamContext?: StreamContext, aiMiddlewares?: PikkuAIMiddlewareHooks[]): {
|
|
42
|
+
tools: AIAgentToolDef[];
|
|
43
|
+
missingRpcs: string[];
|
|
44
|
+
};
|
|
45
|
+
export declare function prepareAgentRun(agentName: string, input: AIAgentInput, params: RunAIAgentParams, agentSessionMap: Map<string, string>, streamContext?: StreamContext): Promise<{
|
|
46
|
+
agent: CoreAIAgent;
|
|
47
|
+
packageName: string | null;
|
|
48
|
+
resolvedName: string;
|
|
49
|
+
agentRunner: import("../../services/ai-agent-runner-service.js").AIAgentRunnerService;
|
|
50
|
+
storage: import("../../services/ai-storage-service.js").AIStorageService | undefined;
|
|
51
|
+
memoryConfig: import("./ai-agent.types.js").AIAgentMemoryConfig | undefined;
|
|
52
|
+
threadId: string;
|
|
53
|
+
userMessage: AIMessage;
|
|
54
|
+
runnerParams: AIAgentRunnerParams;
|
|
55
|
+
maxSteps: number;
|
|
56
|
+
missingRpcs: string[];
|
|
57
|
+
workingMemoryJsonSchema: any;
|
|
58
|
+
workingMemorySchemaName: string | null;
|
|
59
|
+
}>;
|