@pikku/core 0.11.2 → 0.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +33 -2
- package/dist/crypto-utils.d.ts +2 -0
- package/dist/crypto-utils.js +78 -0
- package/dist/errors/errors.d.ts +8 -0
- package/dist/errors/errors.js +16 -0
- package/dist/errors/index.d.ts +0 -1
- package/dist/errors/index.js +0 -1
- package/dist/function/function-runner.d.ts +7 -1
- package/dist/function/function-runner.js +82 -23
- package/dist/function/functions.types.d.ts +31 -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.js +1 -10
- package/dist/index.d.ts +28 -26
- package/dist/index.js +18 -29
- package/dist/middleware/auth-apikey.d.ts +2 -2
- package/dist/middleware/auth-apikey.js +3 -3
- package/dist/middleware/auth-bearer.d.ts +2 -2
- package/dist/middleware/auth-bearer.js +3 -6
- package/dist/middleware/auth-cookie.d.ts +2 -2
- package/dist/middleware/auth-cookie.js +6 -8
- package/dist/middleware/cors.d.ts +46 -0
- package/dist/middleware/cors.js +73 -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.js +8 -10
- package/dist/permissions.js +11 -15
- package/dist/pikku-state.d.ts +1 -0
- package/dist/pikku-state.js +18 -8
- package/dist/schema.js +12 -11
- package/dist/services/ai-agent-runner-service.d.ts +24 -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/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 +94 -0
- package/dist/services/in-memory-trigger-service.d.ts +23 -0
- package/dist/services/in-memory-trigger-service.js +50 -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 +25 -11
- package/dist/services/index.js +10 -13
- package/dist/services/local-secrets.d.ts +23 -0
- package/dist/services/local-secrets.js +45 -0
- package/dist/services/local-variables.d.ts +5 -0
- package/dist/services/local-variables.js +18 -0
- package/dist/services/scheduler-service.d.ts +21 -1
- package/dist/services/scheduler-service.js +14 -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 +23 -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 +13 -0
- 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 -4
- package/dist/types/core.types.d.ts +48 -12
- package/dist/types/core.types.js +8 -1
- package/dist/types/state.types.d.ts +43 -51
- package/dist/utils.d.ts +3 -2
- package/dist/utils.js +15 -3
- package/dist/version.d.ts +6 -0
- package/dist/version.js +19 -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 +60 -0
- package/dist/wirings/ai-agent/ai-agent-prepare.js +312 -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 +96 -0
- package/dist/wirings/ai-agent/ai-agent-stream.d.ts +11 -0
- package/dist/wirings/ai-agent/ai-agent-stream.js +503 -0
- package/dist/wirings/ai-agent/ai-agent.types.d.ts +254 -0
- package/dist/wirings/ai-agent/ai-agent.types.js +1 -0
- package/dist/wirings/ai-agent/index.d.ts +5 -0
- package/dist/wirings/ai-agent/index.js +4 -0
- package/dist/wirings/channel/channel-common.d.ts +7 -4
- package/dist/wirings/channel/channel-common.js +15 -8
- package/dist/wirings/channel/channel-handler.js +12 -3
- 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.js +7 -7
- package/dist/wirings/channel/channel.types.d.ts +5 -1
- 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.js +18 -6
- 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.js +15 -4
- 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 +1 -1
- package/dist/wirings/cli/cli-runner.js +12 -8
- package/dist/wirings/cli/cli.types.d.ts +6 -6
- 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.js +49 -23
- package/dist/wirings/http/http.types.d.ts +78 -38
- package/dist/wirings/http/index.d.ts +5 -4
- package/dist/wirings/http/index.js +4 -3
- package/dist/wirings/mcp/index.d.ts +4 -3
- package/dist/wirings/mcp/index.js +3 -3
- package/dist/wirings/mcp/mcp-runner.d.ts +1 -3
- package/dist/wirings/mcp/mcp-runner.js +25 -39
- package/dist/wirings/mcp/mcp.types.d.ts +3 -3
- 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 +2 -2
- package/dist/wirings/queue/index.js +1 -1
- package/dist/wirings/queue/queue-runner.d.ts +7 -0
- package/dist/wirings/queue/queue-runner.js +17 -6
- package/dist/wirings/queue/queue.types.d.ts +8 -2
- package/dist/wirings/rpc/index.d.ts +1 -1
- package/dist/wirings/rpc/index.js +1 -1
- package/dist/wirings/rpc/rpc-runner.d.ts +33 -5
- package/dist/wirings/rpc/rpc-runner.js +134 -16
- package/dist/wirings/rpc/rpc-types.d.ts +8 -4
- package/dist/wirings/scheduler/index.d.ts +3 -3
- package/dist/wirings/scheduler/index.js +2 -3
- package/dist/wirings/scheduler/scheduler-runner.d.ts +5 -0
- package/dist/wirings/scheduler/scheduler-runner.js +23 -5
- 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 +26 -0
- package/dist/wirings/trigger/pikku-trigger-service.js +75 -0
- package/dist/wirings/trigger/trigger-runner.d.ts +21 -8
- package/dist/wirings/trigger/trigger-runner.js +46 -18
- 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 +28 -14
- package/dist/wirings/workflow/pikku-workflow-service.js +200 -56
- package/dist/wirings/workflow/workflow-helpers.d.ts +36 -0
- package/dist/wirings/workflow/workflow-helpers.js +38 -0
- package/dist/wirings/workflow/workflow-utils.d.ts +1 -23
- package/dist/wirings/workflow/workflow-utils.js +1 -66
- package/dist/wirings/workflow/workflow.types.d.ts +40 -93
- package/package.json +13 -15
- package/run-tests.sh +4 -1
- package/src/crypto-utils.ts +99 -0
- package/src/errors/errors.ts +17 -0
- package/src/errors/index.ts +0 -1
- package/src/factory-functions.test.ts +42 -2
- package/src/function/function-runner.test.ts +4 -11
- package/src/function/function-runner.ts +114 -29
- package/src/function/functions.types.ts +62 -132
- package/src/function/index.ts +5 -2
- package/src/handle-error.ts +2 -10
- package/src/index.ts +99 -25
- package/src/middleware/auth-apikey.test.ts +15 -12
- package/src/middleware/auth-apikey.ts +3 -3
- package/src/middleware/auth-bearer.test.ts +17 -14
- package/src/middleware/auth-bearer.ts +25 -27
- package/src/middleware/auth-cookie.test.ts +16 -13
- package/src/middleware/auth-cookie.ts +6 -8
- package/src/middleware/cors.ts +96 -0
- package/src/middleware/index.ts +5 -3
- package/src/middleware/remote-auth.ts +68 -0
- package/src/middleware-runner.test.ts +122 -0
- package/src/middleware-runner.ts +12 -12
- package/src/permissions.test.ts +81 -7
- package/src/permissions.ts +15 -17
- package/src/pikku-state.ts +20 -8
- package/src/run-tests-script.test.ts +49 -0
- package/src/schema.ts +18 -15
- package/src/services/ai-agent-runner-service.ts +29 -0
- package/src/services/ai-run-state-service.ts +20 -0
- package/src/services/ai-storage-service.ts +39 -0
- package/src/services/deployment-service.ts +22 -0
- package/src/services/gopass-secrets.ts +95 -0
- package/src/services/in-memory-trigger-service.ts +68 -0
- package/src/services/in-memory-workflow-service.ts +395 -0
- package/src/services/index.ts +49 -14
- package/src/services/local-secrets.ts +51 -0
- package/src/services/local-variables.ts +22 -0
- package/src/services/scheduler-service.ts +29 -1
- package/src/services/schema-service.ts +7 -0
- package/src/services/scoped-secret-service.ts +41 -0
- package/src/services/secret-service.ts +25 -4
- package/src/services/trigger-service.ts +38 -0
- package/src/services/typed-secret-service.ts +70 -0
- package/src/services/typed-variables-service.ts +81 -0
- package/src/services/user-session-service.ts +23 -0
- package/src/services/variables-service.ts +5 -0
- package/src/services/workflow-service.ts +22 -2
- package/src/types/core.types.ts +76 -11
- package/src/types/state.types.ts +37 -63
- package/src/utils.ts +20 -6
- package/src/version.test.ts +80 -0
- package/src/version.ts +25 -0
- package/src/wirings/ai-agent/ai-agent-memory.ts +300 -0
- package/src/wirings/ai-agent/ai-agent-model-config.ts +43 -0
- package/src/wirings/ai-agent/ai-agent-prepare.ts +470 -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 +75 -0
- package/src/wirings/ai-agent/ai-agent-runner.ts +137 -0
- package/src/wirings/ai-agent/ai-agent-stream.test.ts +246 -0
- package/src/wirings/ai-agent/ai-agent-stream.ts +774 -0
- package/src/wirings/ai-agent/ai-agent.types.ts +273 -0
- package/src/wirings/ai-agent/index.ts +28 -0
- package/src/wirings/channel/channel-common.ts +32 -19
- package/src/wirings/channel/channel-handler.ts +17 -4
- package/src/wirings/channel/channel-middleware-runner.ts +119 -0
- package/src/wirings/channel/channel-runner.ts +9 -10
- package/src/wirings/channel/channel.types.ts +17 -1
- 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 +44 -1
- package/src/wirings/channel/local/local-channel-runner.ts +22 -6
- package/src/wirings/channel/serverless/index.ts +5 -1
- package/src/wirings/channel/serverless/serverless-channel-runner.ts +20 -4
- package/src/wirings/cli/channel/index.ts +1 -1
- package/src/wirings/cli/cli-runner.test.ts +9 -9
- package/src/wirings/cli/cli-runner.ts +17 -10
- package/src/wirings/cli/cli.types.ts +11 -11
- package/src/wirings/cli/command-parser.test.ts +8 -8
- 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 +3 -3
- package/src/wirings/http/http-runner.ts +63 -29
- package/src/wirings/http/http.types.ts +121 -37
- package/src/wirings/http/index.ts +16 -4
- package/src/wirings/mcp/index.ts +27 -3
- package/src/wirings/mcp/mcp-endpoint-registry.test.ts +3 -3
- package/src/wirings/mcp/mcp-runner.ts +39 -50
- package/src/wirings/mcp/mcp.types.ts +3 -3
- 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 +14 -2
- package/src/wirings/queue/queue-runner.test.ts +97 -55
- package/src/wirings/queue/queue-runner.ts +33 -7
- package/src/wirings/queue/queue.types.ts +12 -2
- package/src/wirings/rpc/index.ts +1 -1
- package/src/wirings/rpc/rpc-runner.ts +187 -17
- package/src/wirings/rpc/rpc-types.ts +13 -4
- package/src/wirings/scheduler/index.ts +7 -4
- package/src/wirings/scheduler/scheduler-runner.test.ts +70 -26
- package/src/wirings/scheduler/scheduler-runner.ts +45 -5
- package/src/wirings/secret/index.ts +9 -0
- package/src/wirings/secret/secret.types.ts +32 -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 +134 -0
- package/src/wirings/trigger/trigger-runner.ts +80 -38
- package/src/wirings/trigger/trigger.types.ts +144 -22
- package/src/wirings/variable/index.ts +8 -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 +495 -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.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 +212 -0
- package/src/wirings/workflow/pikku-workflow-service.ts +285 -66
- package/src/wirings/workflow/workflow-helpers.ts +79 -0
- package/src/wirings/workflow/workflow-utils.ts +0 -120
- package/src/wirings/workflow/workflow.types.ts +45 -90
- 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/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
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
import { CorePermissionGroup, CorePikkuPermission } from '../../function/functions.types.js';
|
|
2
|
+
import { CorePikkuMiddleware, MiddlewareMetadata, PermissionMetadata } from '../../types/core.types.js';
|
|
3
|
+
import type { PikkuChannel } from '../channel/channel.types.js';
|
|
4
|
+
import type { CorePikkuChannelMiddleware } from '../channel/channel.types.js';
|
|
5
|
+
export interface AIThread {
|
|
6
|
+
id: string;
|
|
7
|
+
resourceId: string;
|
|
8
|
+
title?: string;
|
|
9
|
+
metadata?: Record<string, unknown>;
|
|
10
|
+
createdAt: Date;
|
|
11
|
+
updatedAt: Date;
|
|
12
|
+
}
|
|
13
|
+
export interface AIToolCall {
|
|
14
|
+
id: string;
|
|
15
|
+
name: string;
|
|
16
|
+
args: Record<string, unknown>;
|
|
17
|
+
}
|
|
18
|
+
export interface AIToolResult {
|
|
19
|
+
id: string;
|
|
20
|
+
name: string;
|
|
21
|
+
result: string;
|
|
22
|
+
}
|
|
23
|
+
export interface AIMessage {
|
|
24
|
+
id: string;
|
|
25
|
+
role: 'system' | 'user' | 'assistant' | 'tool';
|
|
26
|
+
content?: string;
|
|
27
|
+
toolCalls?: AIToolCall[];
|
|
28
|
+
toolResults?: AIToolResult[];
|
|
29
|
+
createdAt: Date;
|
|
30
|
+
}
|
|
31
|
+
export interface AIAgentStep {
|
|
32
|
+
usage: {
|
|
33
|
+
inputTokens: number;
|
|
34
|
+
outputTokens: number;
|
|
35
|
+
};
|
|
36
|
+
toolCalls?: {
|
|
37
|
+
name: string;
|
|
38
|
+
args: Record<string, unknown>;
|
|
39
|
+
result: string;
|
|
40
|
+
}[];
|
|
41
|
+
}
|
|
42
|
+
export interface AIAgentInput {
|
|
43
|
+
message: string;
|
|
44
|
+
threadId: string;
|
|
45
|
+
resourceId: string;
|
|
46
|
+
}
|
|
47
|
+
export interface AIAgentOutput {
|
|
48
|
+
runId: string;
|
|
49
|
+
text: string;
|
|
50
|
+
object?: unknown;
|
|
51
|
+
threadId: string;
|
|
52
|
+
steps: AIAgentStep[];
|
|
53
|
+
usage: {
|
|
54
|
+
inputTokens: number;
|
|
55
|
+
outputTokens: number;
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
export interface AIAgentToolDef {
|
|
59
|
+
name: string;
|
|
60
|
+
description: string;
|
|
61
|
+
inputSchema: Record<string, unknown>;
|
|
62
|
+
execute: (input: unknown) => Promise<unknown>;
|
|
63
|
+
}
|
|
64
|
+
export interface PikkuAIMiddlewareHooks<State extends Record<string, unknown> = Record<string, unknown>, Services = any> {
|
|
65
|
+
modifyInput?: (services: Services, ctx: {
|
|
66
|
+
messages: AIMessage[];
|
|
67
|
+
instructions: string;
|
|
68
|
+
}) => Promise<{
|
|
69
|
+
messages: AIMessage[];
|
|
70
|
+
instructions: string;
|
|
71
|
+
}> | {
|
|
72
|
+
messages: AIMessage[];
|
|
73
|
+
instructions: string;
|
|
74
|
+
};
|
|
75
|
+
modifyOutputStream?: (services: Services, ctx: {
|
|
76
|
+
event: AIStreamEvent;
|
|
77
|
+
allEvents: readonly AIStreamEvent[];
|
|
78
|
+
state: State;
|
|
79
|
+
}) => Promise<AIStreamEvent | null> | AIStreamEvent | null;
|
|
80
|
+
modifyOutput?: (services: Services, ctx: {
|
|
81
|
+
text: string;
|
|
82
|
+
messages: AIMessage[];
|
|
83
|
+
usage: {
|
|
84
|
+
inputTokens: number;
|
|
85
|
+
outputTokens: number;
|
|
86
|
+
};
|
|
87
|
+
}) => Promise<{
|
|
88
|
+
text: string;
|
|
89
|
+
messages: AIMessage[];
|
|
90
|
+
}> | {
|
|
91
|
+
text: string;
|
|
92
|
+
messages: AIMessage[];
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
export type AIAgentMemoryConfig = {
|
|
96
|
+
storage?: string;
|
|
97
|
+
vector?: string;
|
|
98
|
+
embedder?: string;
|
|
99
|
+
lastMessages?: number;
|
|
100
|
+
workingMemory?: unknown;
|
|
101
|
+
};
|
|
102
|
+
export type CoreAIAgent<PikkuPermission = CorePikkuPermission<any, any>, PikkuMiddleware = CorePikkuMiddleware<any>> = {
|
|
103
|
+
name: string;
|
|
104
|
+
description: string;
|
|
105
|
+
summary?: string;
|
|
106
|
+
errors?: string[];
|
|
107
|
+
instructions: string | string[];
|
|
108
|
+
model: string;
|
|
109
|
+
temperature?: number;
|
|
110
|
+
tools?: unknown[];
|
|
111
|
+
agents?: unknown[];
|
|
112
|
+
memory?: AIAgentMemoryConfig;
|
|
113
|
+
maxSteps?: number;
|
|
114
|
+
toolChoice?: 'auto' | 'required' | 'none';
|
|
115
|
+
input?: unknown;
|
|
116
|
+
output?: unknown;
|
|
117
|
+
tags?: string[];
|
|
118
|
+
middleware?: PikkuMiddleware[];
|
|
119
|
+
channelMiddleware?: CorePikkuChannelMiddleware<any, any>[];
|
|
120
|
+
aiMiddleware?: PikkuAIMiddlewareHooks<any, any>[];
|
|
121
|
+
permissions?: CorePermissionGroup<PikkuPermission>;
|
|
122
|
+
};
|
|
123
|
+
export type AIStreamEvent = {
|
|
124
|
+
type: 'text-delta';
|
|
125
|
+
text: string;
|
|
126
|
+
agent?: string;
|
|
127
|
+
session?: string;
|
|
128
|
+
} | {
|
|
129
|
+
type: 'reasoning-delta';
|
|
130
|
+
text: string;
|
|
131
|
+
agent?: string;
|
|
132
|
+
session?: string;
|
|
133
|
+
} | {
|
|
134
|
+
type: 'tool-call';
|
|
135
|
+
toolCallId: string;
|
|
136
|
+
toolName: string;
|
|
137
|
+
args: unknown;
|
|
138
|
+
agent?: string;
|
|
139
|
+
session?: string;
|
|
140
|
+
} | {
|
|
141
|
+
type: 'tool-result';
|
|
142
|
+
toolCallId: string;
|
|
143
|
+
toolName: string;
|
|
144
|
+
result: unknown;
|
|
145
|
+
agent?: string;
|
|
146
|
+
session?: string;
|
|
147
|
+
} | {
|
|
148
|
+
type: 'agent-call';
|
|
149
|
+
agentName: string;
|
|
150
|
+
session: string;
|
|
151
|
+
input: unknown;
|
|
152
|
+
} | {
|
|
153
|
+
type: 'agent-result';
|
|
154
|
+
agentName: string;
|
|
155
|
+
session: string;
|
|
156
|
+
result: unknown;
|
|
157
|
+
} | {
|
|
158
|
+
type: 'approval-request';
|
|
159
|
+
toolCallId: string;
|
|
160
|
+
toolName: string;
|
|
161
|
+
args: unknown;
|
|
162
|
+
reason?: string;
|
|
163
|
+
agent?: string;
|
|
164
|
+
session?: string;
|
|
165
|
+
} | {
|
|
166
|
+
type: 'usage';
|
|
167
|
+
tokens: {
|
|
168
|
+
input: number;
|
|
169
|
+
output: number;
|
|
170
|
+
};
|
|
171
|
+
model: string;
|
|
172
|
+
agent?: string;
|
|
173
|
+
session?: string;
|
|
174
|
+
} | {
|
|
175
|
+
type: 'error';
|
|
176
|
+
message: string;
|
|
177
|
+
agent?: string;
|
|
178
|
+
session?: string;
|
|
179
|
+
} | {
|
|
180
|
+
type: 'suspended';
|
|
181
|
+
reason: 'rpc-missing';
|
|
182
|
+
missingRpcs: string[];
|
|
183
|
+
} | {
|
|
184
|
+
type: 'done';
|
|
185
|
+
};
|
|
186
|
+
export interface AIStreamChannel extends PikkuChannel<unknown, AIStreamEvent> {
|
|
187
|
+
}
|
|
188
|
+
export type PendingApproval = {
|
|
189
|
+
type: 'tool-call';
|
|
190
|
+
toolCallId: string;
|
|
191
|
+
toolName: string;
|
|
192
|
+
args: unknown;
|
|
193
|
+
} | {
|
|
194
|
+
type: 'agent-call';
|
|
195
|
+
toolCallId: string;
|
|
196
|
+
agentName: string;
|
|
197
|
+
agentRunId: string;
|
|
198
|
+
displayToolName: string;
|
|
199
|
+
displayArgs: unknown;
|
|
200
|
+
};
|
|
201
|
+
export interface AgentRunState {
|
|
202
|
+
runId: string;
|
|
203
|
+
agentName: string;
|
|
204
|
+
threadId: string;
|
|
205
|
+
resourceId: string;
|
|
206
|
+
status: 'running' | 'suspended' | 'completed' | 'failed';
|
|
207
|
+
suspendReason?: 'approval' | 'rpc-missing';
|
|
208
|
+
missingRpcs?: string[];
|
|
209
|
+
pendingApprovals?: PendingApproval[];
|
|
210
|
+
usage: {
|
|
211
|
+
inputTokens: number;
|
|
212
|
+
outputTokens: number;
|
|
213
|
+
model: string;
|
|
214
|
+
};
|
|
215
|
+
createdAt: Date;
|
|
216
|
+
updatedAt: Date;
|
|
217
|
+
}
|
|
218
|
+
export interface AgentRunRow {
|
|
219
|
+
runId: string;
|
|
220
|
+
agentName: string;
|
|
221
|
+
threadId: string;
|
|
222
|
+
resourceId: string;
|
|
223
|
+
status: string;
|
|
224
|
+
suspendReason?: string;
|
|
225
|
+
missingRpcs?: string[];
|
|
226
|
+
usageInputTokens: number;
|
|
227
|
+
usageOutputTokens: number;
|
|
228
|
+
usageModel: string;
|
|
229
|
+
createdAt: Date;
|
|
230
|
+
updatedAt: Date;
|
|
231
|
+
}
|
|
232
|
+
export interface AgentRunService {
|
|
233
|
+
listThreads(options?: {
|
|
234
|
+
agentName?: string;
|
|
235
|
+
limit?: number;
|
|
236
|
+
offset?: number;
|
|
237
|
+
}): Promise<AIThread[]>;
|
|
238
|
+
getThread(threadId: string): Promise<AIThread | null>;
|
|
239
|
+
getThreadMessages(threadId: string): Promise<AIMessage[]>;
|
|
240
|
+
getThreadRuns(threadId: string): Promise<AgentRunRow[]>;
|
|
241
|
+
deleteThread(threadId: string): Promise<boolean>;
|
|
242
|
+
getDistinctAgentNames(): Promise<string[]>;
|
|
243
|
+
}
|
|
244
|
+
export type AIAgentMeta = Record<string, Omit<CoreAIAgent, 'input' | 'output' | 'tools' | 'agents' | 'middleware' | 'channelMiddleware' | 'aiMiddleware' | 'permissions'> & {
|
|
245
|
+
tools?: string[];
|
|
246
|
+
agents?: string[];
|
|
247
|
+
inputSchema: string | null;
|
|
248
|
+
outputSchema: string | null;
|
|
249
|
+
workingMemorySchema: string | null;
|
|
250
|
+
middleware?: MiddlewareMetadata[];
|
|
251
|
+
channelMiddleware?: MiddlewareMetadata[];
|
|
252
|
+
aiMiddleware?: MiddlewareMetadata[];
|
|
253
|
+
permissions?: PermissionMetadata[];
|
|
254
|
+
}>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { runAIAgent } from './ai-agent-runner.js';
|
|
2
|
+
export { streamAIAgent, resumeAIAgent } from './ai-agent-stream.js';
|
|
3
|
+
export { type RunAIAgentParams, type StreamAIAgentOptions, ToolApprovalRequired, } from './ai-agent-prepare.js';
|
|
4
|
+
export { addAIAgent, approveAIAgent, getAIAgents, getAIAgentsMeta, } from './ai-agent-registry.js';
|
|
5
|
+
export type { AIAgentMeta, AIAgentMemoryConfig, AIAgentStep, AgentRunRow, AgentRunService, AgentRunState, AIMessage, AIStreamChannel, AIStreamEvent, AIThread, CoreAIAgent, PendingApproval, PikkuAIMiddlewareHooks, } from './ai-agent.types.js';
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { runAIAgent } from './ai-agent-runner.js';
|
|
2
|
+
export { streamAIAgent, resumeAIAgent } from './ai-agent-stream.js';
|
|
3
|
+
export { ToolApprovalRequired, } from './ai-agent-prepare.js';
|
|
4
|
+
export { addAIAgent, approveAIAgent, getAIAgents, getAIAgentsMeta, } from './ai-agent-registry.js';
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { CoreSingletonServices, MiddlewareMetadata } from '../../types/core.types.js';
|
|
2
2
|
import { CoreChannel, ChannelMessageMeta } from './channel.types.js';
|
|
3
3
|
/**
|
|
4
4
|
* Runs a channel lifecycle function (onConnect or onDisconnect) with proper middleware handling.
|
|
@@ -6,7 +6,8 @@ import { CoreChannel, ChannelMessageMeta } from './channel.types.js';
|
|
|
6
6
|
* This function:
|
|
7
7
|
* 1. Extracts inline middleware from the lifecycle config if present
|
|
8
8
|
* 2. Combines metadata middleware with inline middleware using combineMiddleware()
|
|
9
|
-
* 3.
|
|
9
|
+
* 3. Wraps the channel with channel middleware if present
|
|
10
|
+
* 4. Runs the lifecycle function with middleware support
|
|
10
11
|
*
|
|
11
12
|
* @param channelConfig - The channel configuration
|
|
12
13
|
* @param meta - Metadata for the lifecycle function (connect or disconnect)
|
|
@@ -15,14 +16,16 @@ import { CoreChannel, ChannelMessageMeta } from './channel.types.js';
|
|
|
15
16
|
* @param services - All services (singleton + session)
|
|
16
17
|
* @param channel - The channel instance
|
|
17
18
|
* @param data - Optional data to pass to the lifecycle function (for onConnect)
|
|
19
|
+
* @param channelMiddlewareMeta - Optional channel middleware metadata for wrapping channel.send()
|
|
18
20
|
* @returns Promise<unknown> - Result from the lifecycle function (if any)
|
|
19
21
|
*/
|
|
20
|
-
export declare const runChannelLifecycleWithMiddleware: ({ channelConfig, meta, lifecycleConfig, lifecycleType, services, channel, data, }: {
|
|
22
|
+
export declare const runChannelLifecycleWithMiddleware: ({ channelConfig, meta, lifecycleConfig, lifecycleType, services, channel, data, channelMiddlewareMeta, }: {
|
|
21
23
|
channelConfig: CoreChannel<unknown, any>;
|
|
22
24
|
meta: ChannelMessageMeta;
|
|
23
25
|
lifecycleConfig: any;
|
|
24
26
|
lifecycleType: "connect" | "disconnect";
|
|
25
|
-
services:
|
|
27
|
+
services: CoreSingletonServices;
|
|
26
28
|
channel: any;
|
|
27
29
|
data?: unknown;
|
|
30
|
+
channelMiddlewareMeta?: MiddlewareMetadata[];
|
|
28
31
|
}) => Promise<unknown>;
|
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
import { combineMiddleware, runMiddleware } from '../../middleware-runner.js';
|
|
2
2
|
import { runPikkuFuncDirectly } from '../../function/function-runner.js';
|
|
3
|
+
import { combineChannelMiddleware, wrapChannelWithMiddleware, } from './channel-middleware-runner.js';
|
|
3
4
|
/**
|
|
4
5
|
* Runs a channel lifecycle function (onConnect or onDisconnect) with proper middleware handling.
|
|
5
6
|
*
|
|
6
7
|
* This function:
|
|
7
8
|
* 1. Extracts inline middleware from the lifecycle config if present
|
|
8
9
|
* 2. Combines metadata middleware with inline middleware using combineMiddleware()
|
|
9
|
-
* 3.
|
|
10
|
+
* 3. Wraps the channel with channel middleware if present
|
|
11
|
+
* 4. Runs the lifecycle function with middleware support
|
|
10
12
|
*
|
|
11
13
|
* @param channelConfig - The channel configuration
|
|
12
14
|
* @param meta - Metadata for the lifecycle function (connect or disconnect)
|
|
@@ -15,25 +17,30 @@ import { runPikkuFuncDirectly } from '../../function/function-runner.js';
|
|
|
15
17
|
* @param services - All services (singleton + session)
|
|
16
18
|
* @param channel - The channel instance
|
|
17
19
|
* @param data - Optional data to pass to the lifecycle function (for onConnect)
|
|
20
|
+
* @param channelMiddlewareMeta - Optional channel middleware metadata for wrapping channel.send()
|
|
18
21
|
* @returns Promise<unknown> - Result from the lifecycle function (if any)
|
|
19
22
|
*/
|
|
20
|
-
export const runChannelLifecycleWithMiddleware = async ({ channelConfig, meta, lifecycleConfig, lifecycleType, services, channel, data, }) => {
|
|
21
|
-
// Extract middleware if lifecycle config is an object
|
|
23
|
+
export const runChannelLifecycleWithMiddleware = async ({ channelConfig, meta, lifecycleConfig, lifecycleType, services, channel, data, channelMiddlewareMeta, }) => {
|
|
22
24
|
const lifecycleMiddleware = typeof lifecycleConfig === 'object' && 'middleware' in lifecycleConfig
|
|
23
25
|
? lifecycleConfig.middleware || []
|
|
24
26
|
: [];
|
|
25
|
-
// Use combineMiddleware to properly resolve metadata + inline middleware
|
|
26
27
|
const allMiddleware = combineMiddleware('channel', `${channelConfig.name}:${lifecycleType}`, {
|
|
27
28
|
wireInheritedMiddleware: meta.middleware,
|
|
28
29
|
wireMiddleware: lifecycleMiddleware,
|
|
29
30
|
});
|
|
30
|
-
|
|
31
|
+
const allChannelMiddleware = combineChannelMiddleware('channel', `${channelConfig.name}:${lifecycleType}:cm`, {
|
|
32
|
+
wireInheritedChannelMiddleware: channelMiddlewareMeta,
|
|
33
|
+
wireChannelMiddleware: channelConfig.channelMiddleware,
|
|
34
|
+
});
|
|
35
|
+
let wire = { channel };
|
|
36
|
+
if (allChannelMiddleware.length > 0) {
|
|
37
|
+
wire = wrapChannelWithMiddleware(wire, services, allChannelMiddleware);
|
|
38
|
+
}
|
|
31
39
|
const runLifecycle = async () => {
|
|
32
|
-
return await runPikkuFuncDirectly(meta.
|
|
40
|
+
return await runPikkuFuncDirectly(meta.pikkuFuncId, services, wire, data);
|
|
33
41
|
};
|
|
34
|
-
// Run with middleware if any
|
|
35
42
|
if (allMiddleware.length > 0) {
|
|
36
|
-
return await runMiddleware(services,
|
|
43
|
+
return await runMiddleware(services, wire, allMiddleware, runLifecycle);
|
|
37
44
|
}
|
|
38
45
|
else {
|
|
39
46
|
return await runLifecycle();
|
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
import { pikkuState } from '../../pikku-state.js';
|
|
2
2
|
import { runPikkuFunc } from '../../function/function-runner.js';
|
|
3
|
+
import { createMiddlewareSessionWireProps, } from '../../services/user-session-service.js';
|
|
4
|
+
const getChannelMeta = (channelName) => {
|
|
5
|
+
return pikkuState(null, 'channel', 'meta')[channelName];
|
|
6
|
+
};
|
|
3
7
|
const getRouteMeta = (channelName, routingProperty, routerValue) => {
|
|
4
8
|
const channelMeta = pikkuState(null, 'channel', 'meta')[channelName];
|
|
5
9
|
if (!channelMeta) {
|
|
@@ -44,7 +48,7 @@ export const processMessageHandlers = (services, channelConfig, channelHandler,
|
|
|
44
48
|
channelHandler.getChannel().send(`Unauthorized for ${routeMessage}`);
|
|
45
49
|
return;
|
|
46
50
|
}
|
|
47
|
-
const {
|
|
51
|
+
const { pikkuFuncId, middleware: routeInheritedMiddleware, permissions: inheritedPermissions, } = getRouteMeta(channelConfig.name, routingProperty, routerValue);
|
|
48
52
|
// Get wire middleware: channel-level middleware + message-specific middleware
|
|
49
53
|
const channelWireMiddleware = channelConfig.middleware || [];
|
|
50
54
|
// Check if onMessage is a wrapper object vs direct function config:
|
|
@@ -68,17 +72,22 @@ export const processMessageHandlers = (services, channelConfig, channelHandler,
|
|
|
68
72
|
const cacheKey = routingProperty
|
|
69
73
|
? `${channelConfig.name}:${routingProperty}:${routerValue}`
|
|
70
74
|
: `${channelConfig.name}:default`;
|
|
71
|
-
|
|
75
|
+
const channelMeta = getChannelMeta(channelConfig.name);
|
|
76
|
+
const wireChannelMiddleware = channelConfig.channelMiddleware || [];
|
|
77
|
+
return await runPikkuFunc('channel', cacheKey, pikkuFuncId, {
|
|
72
78
|
singletonServices: services,
|
|
73
79
|
data: () => data,
|
|
74
80
|
inheritedMiddleware,
|
|
75
81
|
wireMiddleware,
|
|
82
|
+
inheritedChannelMiddleware: channelMeta?.channelMiddleware,
|
|
83
|
+
wireChannelMiddleware: wireChannelMiddleware,
|
|
76
84
|
inheritedPermissions,
|
|
77
85
|
wirePermissions,
|
|
78
86
|
tags: channelConfig.tags,
|
|
87
|
+
sessionService: userSession,
|
|
79
88
|
wire: {
|
|
80
89
|
channel: channelHandler.getChannel(),
|
|
81
|
-
|
|
90
|
+
...(userSession && createMiddlewareSessionWireProps(userSession)),
|
|
82
91
|
},
|
|
83
92
|
});
|
|
84
93
|
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { CoreSingletonServices, MiddlewareMetadata, PikkuWire } from '../../types/core.types.js';
|
|
2
|
+
import type { CorePikkuChannelMiddleware } from './channel.types.js';
|
|
3
|
+
export declare const addChannelMiddleware: (tag: string, middleware: CorePikkuChannelMiddleware[], packageName?: string | null) => CorePikkuChannelMiddleware[];
|
|
4
|
+
export declare const combineChannelMiddleware: (wireType: string, uid: string, { wireInheritedChannelMiddleware, wireChannelMiddleware, packageName, }?: {
|
|
5
|
+
wireInheritedChannelMiddleware?: MiddlewareMetadata[];
|
|
6
|
+
wireChannelMiddleware?: CorePikkuChannelMiddleware[];
|
|
7
|
+
packageName?: string | null;
|
|
8
|
+
}) => readonly CorePikkuChannelMiddleware[];
|
|
9
|
+
export declare function wrapChannelWithMiddleware<Out>(wire: PikkuWire, services: CoreSingletonServices, middlewares: readonly CorePikkuChannelMiddleware[]): PikkuWire;
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { pikkuState } from '../../pikku-state.js';
|
|
2
|
+
import { freezeDedupe, getTagGroups } from '../../utils.js';
|
|
3
|
+
export const addChannelMiddleware = (tag, middleware, packageName = null) => {
|
|
4
|
+
const tagGroups = pikkuState(packageName, 'channelMiddleware', 'tagGroup');
|
|
5
|
+
tagGroups[tag] = middleware;
|
|
6
|
+
return middleware;
|
|
7
|
+
};
|
|
8
|
+
const getChannelMiddlewareByName = (name) => {
|
|
9
|
+
const store = pikkuState(null, 'misc', 'channelMiddleware');
|
|
10
|
+
const middleware = store[name];
|
|
11
|
+
return middleware?.[0];
|
|
12
|
+
};
|
|
13
|
+
const channelMiddlewareCache = {};
|
|
14
|
+
export const combineChannelMiddleware = (wireType, uid, { wireInheritedChannelMiddleware, wireChannelMiddleware, packageName = null, } = {}) => {
|
|
15
|
+
const cacheKey = `${wireType}:${uid}`;
|
|
16
|
+
if (channelMiddlewareCache[cacheKey]) {
|
|
17
|
+
return channelMiddlewareCache[cacheKey];
|
|
18
|
+
}
|
|
19
|
+
const resolved = [];
|
|
20
|
+
if (wireInheritedChannelMiddleware) {
|
|
21
|
+
for (const meta of wireInheritedChannelMiddleware) {
|
|
22
|
+
if (meta.type === 'tag') {
|
|
23
|
+
const groups = getTagGroups(pikkuState(packageName, 'channelMiddleware', 'tagGroup'), meta.tag);
|
|
24
|
+
for (const group of groups) {
|
|
25
|
+
resolved.push(...group);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
else if (meta.type === 'wire') {
|
|
29
|
+
const middleware = getChannelMiddlewareByName(meta.name);
|
|
30
|
+
if (middleware) {
|
|
31
|
+
resolved.push(middleware);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
if (wireChannelMiddleware) {
|
|
37
|
+
resolved.push(...wireChannelMiddleware);
|
|
38
|
+
}
|
|
39
|
+
channelMiddlewareCache[cacheKey] = freezeDedupe(resolved);
|
|
40
|
+
return channelMiddlewareCache[cacheKey];
|
|
41
|
+
};
|
|
42
|
+
export function wrapChannelWithMiddleware(wire, services, middlewares) {
|
|
43
|
+
if (middlewares.length === 0 || !wire.channel)
|
|
44
|
+
return wire;
|
|
45
|
+
const channel = wire.channel;
|
|
46
|
+
const originalSend = channel.send.bind(channel);
|
|
47
|
+
const dispatch = async (index, event) => {
|
|
48
|
+
if (index >= middlewares.length) {
|
|
49
|
+
await originalSend(event);
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
const middleware = middlewares[index];
|
|
53
|
+
await middleware(services, event, async (result) => {
|
|
54
|
+
if (result === null || result === undefined)
|
|
55
|
+
return;
|
|
56
|
+
if (Array.isArray(result)) {
|
|
57
|
+
for (const item of result) {
|
|
58
|
+
await dispatch(index + 1, item);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
await dispatch(index + 1, result);
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
};
|
|
66
|
+
return {
|
|
67
|
+
...wire,
|
|
68
|
+
channel: {
|
|
69
|
+
...channel,
|
|
70
|
+
send: ((data) => dispatch(0, data)),
|
|
71
|
+
},
|
|
72
|
+
};
|
|
73
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { NotFoundError } from '../../errors/errors.js';
|
|
1
|
+
import { NotFoundError, PikkuMissingMetaError } from '../../errors/errors.js';
|
|
2
2
|
import { addFunction } from '../../function/function-runner.js';
|
|
3
3
|
import { pikkuState } from '../../pikku-state.js';
|
|
4
4
|
import { coerceTopLevelDataFromSchema, validateSchema } from '../../schema.js';
|
|
@@ -12,20 +12,20 @@ export const wireChannel = (channel) => {
|
|
|
12
12
|
const channelsMeta = pikkuState(null, 'channel', 'meta');
|
|
13
13
|
const channelMeta = channelsMeta[channel.name];
|
|
14
14
|
if (!channelMeta) {
|
|
15
|
-
throw new
|
|
15
|
+
throw new PikkuMissingMetaError(`Missing generated metadata for channel '${channel.name}'`);
|
|
16
16
|
}
|
|
17
17
|
pikkuState(null, 'channel', 'channels').set(channel.name, channel);
|
|
18
18
|
// Register onConnect function if provided
|
|
19
19
|
if (channel.onConnect && channelMeta.connect) {
|
|
20
|
-
addFunction(channelMeta.connect.
|
|
20
|
+
addFunction(channelMeta.connect.pikkuFuncId, channel.onConnect);
|
|
21
21
|
}
|
|
22
22
|
// Register onDisconnect function if provided
|
|
23
23
|
if (channel.onDisconnect && channelMeta.disconnect) {
|
|
24
|
-
addFunction(channelMeta.disconnect.
|
|
24
|
+
addFunction(channelMeta.disconnect.pikkuFuncId, channel.onDisconnect);
|
|
25
25
|
}
|
|
26
26
|
// Register onMessage function if provided
|
|
27
|
-
if (channel.onMessage && channelMeta.message?.
|
|
28
|
-
addFunction(channelMeta.message.
|
|
27
|
+
if (channel.onMessage && channelMeta.message?.pikkuFuncId) {
|
|
28
|
+
addFunction(channelMeta.message.pikkuFuncId, channel.onMessage.func instanceof Function
|
|
29
29
|
? channel.onMessage
|
|
30
30
|
: channel.onMessage.func);
|
|
31
31
|
}
|
|
@@ -43,7 +43,7 @@ export const wireChannel = (channel) => {
|
|
|
43
43
|
return;
|
|
44
44
|
// Register the function using the pikku name from metadata
|
|
45
45
|
// It could be a FuncConfig or a wiring override with a funcConfig
|
|
46
|
-
addFunction(wiringMeta.
|
|
46
|
+
addFunction(wiringMeta.pikkuFuncId, handler.func instanceof Function
|
|
47
47
|
? handler
|
|
48
48
|
: handler.func);
|
|
49
49
|
});
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { PikkuError } from '../../errors/error-handler.js';
|
|
2
2
|
import { HTTPFunctionMetaInputTypes, PikkuHTTPRequest, PikkuHTTPResponse } from '../http/http.types.js';
|
|
3
3
|
import { CoreSingletonServices, CreateWireServices, CorePikkuMiddleware, MiddlewareMetadata, PermissionMetadata } from '../../types/core.types.js';
|
|
4
|
+
export type CorePikkuChannelMiddleware<Services = any, Event = unknown> = (services: Services, event: Event, next: (event: Event | Event[] | null) => Promise<void> | void) => Promise<void> | void;
|
|
5
|
+
export type CorePikkuChannelMiddlewareFactory<In = any, Services = any, Event = unknown> = (input: In) => CorePikkuChannelMiddleware<Services, Event>;
|
|
4
6
|
import { CorePermissionGroup, CorePikkuFunction, CorePikkuFunctionConfig, CorePikkuFunctionSessionless, CorePikkuPermission } from '../../function/functions.types.js';
|
|
5
7
|
export type RunChannelOptions = Partial<{
|
|
6
8
|
skipUserSession: boolean;
|
|
@@ -17,7 +19,7 @@ export type RunChannelParams<ChannelData> = {
|
|
|
17
19
|
createWireServices?: CreateWireServices;
|
|
18
20
|
};
|
|
19
21
|
export interface ChannelMessageMeta {
|
|
20
|
-
|
|
22
|
+
pikkuFuncId: string;
|
|
21
23
|
summary?: string;
|
|
22
24
|
description?: string;
|
|
23
25
|
errors?: string[];
|
|
@@ -42,6 +44,7 @@ export interface ChannelMeta {
|
|
|
42
44
|
tags?: string[];
|
|
43
45
|
middleware?: MiddlewareMetadata[];
|
|
44
46
|
permissions?: PermissionMetadata[];
|
|
47
|
+
channelMiddleware?: MiddlewareMetadata[];
|
|
45
48
|
}
|
|
46
49
|
export type ChannelsMeta = Record<string, ChannelMeta>;
|
|
47
50
|
export type CoreChannel<ChannelData, Channel extends string, ChannelConnect = CorePikkuFunctionConfig<CorePikkuFunction<void, unknown> | CorePikkuFunctionSessionless<void, unknown>, CorePikkuPermission<void>, CorePikkuMiddleware>, ChannelDisconnect = CorePikkuFunctionConfig<CorePikkuFunction<void, void> | CorePikkuFunctionSessionless<void, void>, CorePikkuPermission<void>, CorePikkuMiddleware>, ChannelFunctionMessage = CorePikkuFunctionConfig<CorePikkuFunction<unknown, unknown> | CorePikkuFunctionSessionless<unknown, unknown>, CorePikkuPermission<unknown>, CorePikkuMiddleware>, PikkuPermission = CorePikkuPermission<ChannelData>, PikkuMiddleware = CorePikkuMiddleware> = {
|
|
@@ -63,6 +66,7 @@ export type CoreChannel<ChannelData, Channel extends string, ChannelConnect = Co
|
|
|
63
66
|
middleware?: PikkuMiddleware[];
|
|
64
67
|
}>>;
|
|
65
68
|
middleware?: PikkuMiddleware[];
|
|
69
|
+
channelMiddleware?: Array<CorePikkuChannelMiddleware | CorePikkuChannelMiddlewareFactory>;
|
|
66
70
|
permissions?: CorePermissionGroup<PikkuPermission>;
|
|
67
71
|
auth?: boolean;
|
|
68
72
|
docs?: Partial<{
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type-safe helper for defining channel message routes that can be composed.
|
|
3
|
+
* Returns the routes record as-is (identity function) for use with `wireChannel`.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* ```typescript
|
|
7
|
+
* export const chatRoutes = defineChannelRoutes({
|
|
8
|
+
* greet: pikkuChannelFunc({ func: greetFunc }),
|
|
9
|
+
* farewell: pikkuChannelFunc({ func: farewellFunc }),
|
|
10
|
+
* })
|
|
11
|
+
*
|
|
12
|
+
* wireChannel({
|
|
13
|
+
* name: 'chat',
|
|
14
|
+
* route: '/ws/chat',
|
|
15
|
+
* onMessageWiring: {
|
|
16
|
+
* command: chatRoutes,
|
|
17
|
+
* }
|
|
18
|
+
* })
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
export declare function defineChannelRoutes<T extends Record<string, any>>(routes: T): T;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type-safe helper for defining channel message routes that can be composed.
|
|
3
|
+
* Returns the routes record as-is (identity function) for use with `wireChannel`.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* ```typescript
|
|
7
|
+
* export const chatRoutes = defineChannelRoutes({
|
|
8
|
+
* greet: pikkuChannelFunc({ func: greetFunc }),
|
|
9
|
+
* farewell: pikkuChannelFunc({ func: farewellFunc }),
|
|
10
|
+
* })
|
|
11
|
+
*
|
|
12
|
+
* wireChannel({
|
|
13
|
+
* name: 'chat',
|
|
14
|
+
* route: '/ws/chat',
|
|
15
|
+
* onMessageWiring: {
|
|
16
|
+
* command: chatRoutes,
|
|
17
|
+
* }
|
|
18
|
+
* })
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
export function defineChannelRoutes(routes) {
|
|
22
|
+
return routes;
|
|
23
|
+
}
|
|
@@ -1,8 +1,10 @@
|
|
|
1
|
-
export
|
|
2
|
-
export
|
|
3
|
-
export
|
|
4
|
-
export
|
|
5
|
-
export
|
|
6
|
-
export
|
|
7
|
-
export
|
|
8
|
-
export
|
|
1
|
+
export { wireChannel, openChannel } from './channel-runner.js';
|
|
2
|
+
export { addChannelMiddleware, combineChannelMiddleware, wrapChannelWithMiddleware, } from './channel-middleware-runner.js';
|
|
3
|
+
export { logChannels } from './log-channels.js';
|
|
4
|
+
export { PikkuAbstractChannelHandler } from './pikku-abstract-channel-handler.js';
|
|
5
|
+
export type { EventHubService } from './eventhub-service.js';
|
|
6
|
+
export { ChannelStore } from './channel-store.js';
|
|
7
|
+
export type { Channel } from './channel-store.js';
|
|
8
|
+
export { EventHubStore } from './eventhub-store.js';
|
|
9
|
+
export type { ChannelsMeta, CoreChannel, CorePikkuChannelMiddleware, CorePikkuChannelMiddlewareFactory, ChannelMessageMeta, ChannelMeta, PikkuChannel, PikkuChannelHandlerFactory, } from './channel.types.js';
|
|
10
|
+
export { defineChannelRoutes } from './define-channel-routes.js';
|
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
export
|
|
2
|
-
export
|
|
3
|
-
export
|
|
4
|
-
export
|
|
5
|
-
export
|
|
6
|
-
export
|
|
7
|
-
export
|
|
8
|
-
export * from './eventhub-store.js';
|
|
1
|
+
export { wireChannel, openChannel } from './channel-runner.js';
|
|
2
|
+
export { addChannelMiddleware, combineChannelMiddleware, wrapChannelWithMiddleware, } from './channel-middleware-runner.js';
|
|
3
|
+
export { logChannels } from './log-channels.js';
|
|
4
|
+
export { PikkuAbstractChannelHandler } from './pikku-abstract-channel-handler.js';
|
|
5
|
+
export { ChannelStore } from './channel-store.js';
|
|
6
|
+
export { EventHubStore } from './eventhub-store.js';
|
|
7
|
+
export { defineChannelRoutes } from './define-channel-routes.js';
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export
|
|
2
|
-
export
|
|
3
|
-
export
|
|
1
|
+
export { PikkuLocalChannelHandler } from './local-channel-handler.js';
|
|
2
|
+
export { runLocalChannel } from './local-channel-runner.js';
|
|
3
|
+
export { LocalEventHubService } from './local-eventhub-service.js';
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export
|
|
2
|
-
export
|
|
3
|
-
export
|
|
1
|
+
export { PikkuLocalChannelHandler } from './local-channel-handler.js';
|
|
2
|
+
export { runLocalChannel } from './local-channel-runner.js';
|
|
3
|
+
export { LocalEventHubService } from './local-eventhub-service.js';
|