@pikku/core 0.12.4 → 0.12.5
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 +11 -1
- package/dist/dev/hot-reload.d.ts +10 -0
- package/dist/dev/hot-reload.js +158 -0
- package/dist/middleware-runner.d.ts +1 -0
- package/dist/middleware-runner.js +5 -0
- package/dist/permissions.d.ts +1 -0
- package/dist/permissions.js +5 -0
- package/dist/services/in-memory-workflow-service.js +7 -11
- package/dist/wirings/ai-agent/ai-agent-prepare.js +16 -1
- package/dist/wirings/channel/channel-middleware-runner.d.ts +1 -0
- package/dist/wirings/channel/channel-middleware-runner.js +5 -0
- package/dist/wirings/workflow/pikku-workflow-service.js +7 -8
- package/package.json +3 -2
- package/src/crypto-utils.test.ts +214 -0
- package/src/crypto-utils.ts +213 -0
- package/src/dev/hot-reload.test.ts +484 -0
- package/src/dev/hot-reload.ts +212 -0
- package/src/errors/error-handler.ts +62 -0
- package/src/errors/error.test.ts +195 -0
- package/src/errors/errors.ts +374 -0
- package/src/errors/index.ts +2 -0
- package/src/factory-functions.test.ts +109 -0
- package/src/function/function-runner.test.ts +536 -0
- package/src/function/function-runner.ts +365 -0
- package/src/function/functions.types.ts +304 -0
- package/src/function/index.ts +5 -0
- package/src/handle-error.test.ts +424 -0
- package/src/handle-error.ts +69 -0
- package/src/index.ts +118 -0
- package/src/internal.ts +6 -0
- package/src/middleware/auth-apikey.test.ts +363 -0
- package/src/middleware/auth-apikey.ts +47 -0
- package/src/middleware/auth-bearer.test.ts +450 -0
- package/src/middleware/auth-bearer.ts +72 -0
- package/src/middleware/auth-cookie.test.ts +528 -0
- package/src/middleware/auth-cookie.ts +80 -0
- package/src/middleware/cors.test.ts +424 -0
- package/src/middleware/cors.ts +104 -0
- package/src/middleware/index.ts +5 -0
- package/src/middleware/remote-auth.test.ts +488 -0
- package/src/middleware/remote-auth.ts +68 -0
- package/src/middleware/timeout.ts +15 -0
- package/src/middleware-runner.test.ts +418 -0
- package/src/middleware-runner.ts +240 -0
- package/src/permissions.test.ts +434 -0
- package/src/permissions.ts +327 -0
- package/src/pikku-request.ts +23 -0
- package/src/pikku-response.ts +5 -0
- package/src/pikku-state.test.ts +224 -0
- package/src/pikku-state.ts +216 -0
- package/src/run-tests-script.test.ts +49 -0
- package/src/schema.test.ts +249 -0
- package/src/schema.ts +151 -0
- 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 +76 -0
- package/src/services/deployment-service.ts +22 -0
- package/src/services/gateway-service.ts +20 -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 +502 -0
- package/src/services/index.ts +56 -0
- package/src/services/jwt-service.ts +30 -0
- package/src/services/local-content.ts +120 -0
- package/src/services/local-gateway-service.ts +62 -0
- package/src/services/local-secrets.test.ts +80 -0
- package/src/services/local-secrets.ts +94 -0
- package/src/services/local-variables.test.ts +61 -0
- package/src/services/local-variables.ts +39 -0
- package/src/services/logger-console.test.ts +118 -0
- package/src/services/logger-console.ts +98 -0
- package/src/services/logger.ts +57 -0
- package/src/services/scheduler-service.ts +86 -0
- package/src/services/schema-service.ts +33 -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 +38 -0
- 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 +86 -0
- package/src/services/variables-service.ts +11 -0
- package/src/services/workflow-service.ts +95 -0
- package/src/testing/index.ts +2 -0
- package/src/testing/service-tests.ts +873 -0
- package/src/time-utils.test.ts +56 -0
- package/src/time-utils.ts +107 -0
- package/src/types/core.types.ts +478 -0
- package/src/types/state.types.ts +188 -0
- package/src/utils/hash.test.ts +68 -0
- package/src/utils/hash.ts +26 -0
- package/src/utils.test.ts +350 -0
- package/src/utils.ts +129 -0
- package/src/version.test.ts +80 -0
- package/src/version.ts +25 -0
- package/src/wirings/ai-agent/agent-dynamic-workflow.ts +469 -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 +310 -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 +659 -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 +319 -0
- package/src/wirings/ai-agent/ai-agent-runner.ts +773 -0
- package/src/wirings/ai-agent/ai-agent-stream.test.ts +859 -0
- package/src/wirings/ai-agent/ai-agent-stream.ts +1153 -0
- package/src/wirings/ai-agent/ai-agent.types.ts +382 -0
- package/src/wirings/ai-agent/index.ts +37 -0
- package/src/wirings/channel/channel-common.ts +90 -0
- package/src/wirings/channel/channel-handler.ts +250 -0
- package/src/wirings/channel/channel-middleware-runner.ts +126 -0
- package/src/wirings/channel/channel-runner.ts +166 -0
- package/src/wirings/channel/channel-store.ts +29 -0
- package/src/wirings/channel/channel.types.ts +172 -0
- package/src/wirings/channel/define-channel-routes.ts +25 -0
- package/src/wirings/channel/eventhub-service.ts +34 -0
- package/src/wirings/channel/eventhub-store.ts +13 -0
- package/src/wirings/channel/index.ts +24 -0
- package/src/wirings/channel/local/index.ts +3 -0
- package/src/wirings/channel/local/local-channel-handler.ts +81 -0
- package/src/wirings/channel/local/local-channel-runner.test.ts +215 -0
- package/src/wirings/channel/local/local-channel-runner.ts +210 -0
- package/src/wirings/channel/local/local-eventhub-service.test.ts +95 -0
- package/src/wirings/channel/local/local-eventhub-service.ts +101 -0
- package/src/wirings/channel/log-channels.ts +20 -0
- package/src/wirings/channel/pikku-abstract-channel-handler.test.ts +54 -0
- package/src/wirings/channel/pikku-abstract-channel-handler.ts +45 -0
- package/src/wirings/channel/serverless/index.ts +5 -0
- package/src/wirings/channel/serverless/serverless-channel-runner.ts +289 -0
- package/src/wirings/cli/channel/cli-channel-runner.ts +136 -0
- package/src/wirings/cli/channel/index.ts +1 -0
- package/src/wirings/cli/cli-runner.test.ts +403 -0
- package/src/wirings/cli/cli-runner.ts +529 -0
- package/src/wirings/cli/cli.types.ts +358 -0
- package/src/wirings/cli/command-parser.test.ts +445 -0
- package/src/wirings/cli/command-parser.ts +504 -0
- package/src/wirings/cli/define-cli-commands.ts +24 -0
- package/src/wirings/cli/index.ts +19 -0
- package/src/wirings/gateway/gateway-runner.test.ts +474 -0
- package/src/wirings/gateway/gateway-runner.ts +411 -0
- package/src/wirings/gateway/gateway.types.ts +149 -0
- package/src/wirings/gateway/index.ts +13 -0
- package/src/wirings/http/http-routes.test.ts +322 -0
- package/src/wirings/http/http-routes.ts +197 -0
- package/src/wirings/http/http-runner.test.ts +144 -0
- package/src/wirings/http/http-runner.ts +588 -0
- package/src/wirings/http/http.types.ts +369 -0
- package/src/wirings/http/index.ts +28 -0
- package/src/wirings/http/log-http-routes.ts +22 -0
- package/src/wirings/http/pikku-fetch-http-request.test.ts +237 -0
- package/src/wirings/http/pikku-fetch-http-request.ts +170 -0
- package/src/wirings/http/pikku-fetch-http-response.test.ts +82 -0
- package/src/wirings/http/pikku-fetch-http-response.ts +147 -0
- package/src/wirings/http/routers/http-router.ts +13 -0
- package/src/wirings/http/routers/path-to-regex.test.ts +319 -0
- package/src/wirings/http/routers/path-to-regex.ts +126 -0
- package/src/wirings/http/web-request.test.ts +236 -0
- package/src/wirings/http/web-request.ts +104 -0
- package/src/wirings/mcp/index.ts +27 -0
- package/src/wirings/mcp/mcp-endpoint-registry.test.ts +389 -0
- package/src/wirings/mcp/mcp-endpoint-registry.ts +159 -0
- package/src/wirings/mcp/mcp-runner.ts +323 -0
- package/src/wirings/mcp/mcp.types.ts +216 -0
- 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 +31 -0
- package/src/wirings/queue/queue-runner.test.ts +710 -0
- package/src/wirings/queue/queue-runner.ts +192 -0
- package/src/wirings/queue/queue.types.ts +189 -0
- package/src/wirings/queue/register-queue-helper.ts +60 -0
- package/src/wirings/queue/validate-worker-config.test.ts +108 -0
- package/src/wirings/queue/validate-worker-config.ts +116 -0
- package/src/wirings/rpc/index.ts +4 -0
- package/src/wirings/rpc/rpc-runner.ts +460 -0
- package/src/wirings/rpc/rpc-types.ts +56 -0
- package/src/wirings/rpc/wire-addon.ts +21 -0
- package/src/wirings/scheduler/index.ts +11 -0
- package/src/wirings/scheduler/log-schedulers.ts +20 -0
- package/src/wirings/scheduler/scheduler-runner.test.ts +660 -0
- package/src/wirings/scheduler/scheduler-runner.ts +133 -0
- package/src/wirings/scheduler/scheduler.types.ts +53 -0
- 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 -0
- 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 +135 -0
- package/src/wirings/trigger/trigger.types.ts +178 -0
- 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 +31 -0
- package/src/wirings/workflow/dsl/workflow-dsl.types.ts +320 -0
- package/src/wirings/workflow/dsl/workflow-runner.ts +28 -0
- package/src/wirings/workflow/graph/graph-node.ts +222 -0
- package/src/wirings/workflow/graph/graph-runner.test.ts +487 -0
- package/src/wirings/workflow/graph/graph-runner.ts +816 -0
- package/src/wirings/workflow/graph/graph-validation.test.ts +170 -0
- package/src/wirings/workflow/graph/graph-validation.ts +237 -0
- package/src/wirings/workflow/graph/index.ts +19 -0
- 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 +104 -0
- package/src/wirings/workflow/index.ts +82 -0
- package/src/wirings/workflow/pikku-workflow-service.test.ts +200 -0
- package/src/wirings/workflow/pikku-workflow-service.ts +1179 -0
- 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 +274 -0
- package/tsconfig.json +14 -0
- package/tsconfig.tsbuildinfo +1 -0
- package/lcov.info +0 -18891
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import type { PikkuWire } from '../../types/core.types.js'
|
|
2
|
+
import type { CoreQueueWorker, QueueJob, PikkuQueue } from './queue.types.js'
|
|
3
|
+
import type {
|
|
4
|
+
CorePikkuFunctionConfig,
|
|
5
|
+
CorePikkuFunctionSessionless,
|
|
6
|
+
} from '../../function/functions.types.js'
|
|
7
|
+
import { getErrorResponse, PikkuError } from '../../errors/error-handler.js'
|
|
8
|
+
import { PikkuMissingMetaError } from '../../errors/errors.js'
|
|
9
|
+
import {
|
|
10
|
+
getSingletonServices,
|
|
11
|
+
getCreateWireServices,
|
|
12
|
+
pikkuState,
|
|
13
|
+
} from '../../pikku-state.js'
|
|
14
|
+
import { addFunction, runPikkuFunc } from '../../function/function-runner.js'
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Error class for queue processor not found
|
|
18
|
+
*/
|
|
19
|
+
class QueueWorkerNotFoundError extends PikkuError {
|
|
20
|
+
constructor(name: string) {
|
|
21
|
+
super(`Queue processor not found: ${name}`)
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Error class for when a queue job is explicitly failed
|
|
27
|
+
*/
|
|
28
|
+
export class QueueJobFailedError extends PikkuError {
|
|
29
|
+
constructor(jobId: string, reason?: string) {
|
|
30
|
+
super(`Queue job ${jobId} failed${reason ? `: ${reason}` : ''}`)
|
|
31
|
+
this.name = 'QueueJobFailedError'
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Error class for when a queue job is explicitly discarded
|
|
37
|
+
*/
|
|
38
|
+
export class QueueJobDiscardedError extends PikkuError {
|
|
39
|
+
constructor(jobId: string, reason?: string) {
|
|
40
|
+
super(`Queue job ${jobId} discarded${reason ? `: ${reason}` : ''}`)
|
|
41
|
+
this.name = 'QueueJobDiscardedError'
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Add a queue processor to the system
|
|
46
|
+
*/
|
|
47
|
+
export const wireQueueWorker = <
|
|
48
|
+
InputData = any,
|
|
49
|
+
OutputData = any,
|
|
50
|
+
PikkuFunctionConfig extends CorePikkuFunctionConfig<
|
|
51
|
+
CorePikkuFunctionSessionless<InputData, OutputData>
|
|
52
|
+
> = CorePikkuFunctionConfig<
|
|
53
|
+
CorePikkuFunctionSessionless<InputData, OutputData>
|
|
54
|
+
>,
|
|
55
|
+
>(
|
|
56
|
+
queueWorker: CoreQueueWorker<PikkuFunctionConfig>
|
|
57
|
+
) => {
|
|
58
|
+
// Get processor metadata
|
|
59
|
+
const meta = pikkuState(null, 'queue', 'meta')
|
|
60
|
+
const processorMeta = meta[queueWorker.name]
|
|
61
|
+
if (!processorMeta) {
|
|
62
|
+
throw new PikkuMissingMetaError(
|
|
63
|
+
`Missing generated metadata for queue worker '${queueWorker.name}'`
|
|
64
|
+
)
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// Register the function with pikku
|
|
68
|
+
addFunction(processorMeta.pikkuFuncId, {
|
|
69
|
+
func: queueWorker.func.func,
|
|
70
|
+
auth: queueWorker.func.auth,
|
|
71
|
+
permissions: queueWorker.func.permissions,
|
|
72
|
+
middleware: queueWorker.func.middleware as any,
|
|
73
|
+
tags: queueWorker.func.tags,
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
// Store processor definition in state - runtime adapters will pick this up
|
|
77
|
+
const registrations = pikkuState(null, 'queue', 'registrations')
|
|
78
|
+
registrations.set(queueWorker.name, queueWorker)
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Get all registered queue processors
|
|
83
|
+
*/
|
|
84
|
+
export function getQueueWorkers(): Map<string, CoreQueueWorker> {
|
|
85
|
+
return pikkuState(null, 'queue', 'registrations')
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Stop and remove a queue processor
|
|
90
|
+
*/
|
|
91
|
+
export async function removeQueueWorker(name: string): Promise<void> {
|
|
92
|
+
const registrations = pikkuState(null, 'queue', 'registrations')
|
|
93
|
+
const registration = registrations.get(name)
|
|
94
|
+
|
|
95
|
+
if (!registration) {
|
|
96
|
+
throw new QueueWorkerNotFoundError(name)
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
registrations.delete(name)
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Process a single queue job - this function is called by queue consumers
|
|
104
|
+
*/
|
|
105
|
+
export async function runQueueJob({
|
|
106
|
+
job,
|
|
107
|
+
updateProgress,
|
|
108
|
+
}: {
|
|
109
|
+
job: QueueJob
|
|
110
|
+
updateProgress?: (progress: number | string | object) => Promise<void>
|
|
111
|
+
}): Promise<void> {
|
|
112
|
+
const singletonServices = getSingletonServices()
|
|
113
|
+
const createWireServices = getCreateWireServices()
|
|
114
|
+
const logger = singletonServices.logger
|
|
115
|
+
|
|
116
|
+
const meta = pikkuState(null, 'queue', 'meta')
|
|
117
|
+
const processorMeta = meta[job.queueName]
|
|
118
|
+
if (!processorMeta) {
|
|
119
|
+
throw new PikkuMissingMetaError(
|
|
120
|
+
`Missing generated metadata for queue worker '${job.queueName}'`
|
|
121
|
+
)
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// Get the queue worker registration to access middleware
|
|
125
|
+
const registrations = pikkuState(null, 'queue', 'registrations')
|
|
126
|
+
const queueWorker = registrations.get(job.queueName)
|
|
127
|
+
if (!queueWorker) {
|
|
128
|
+
throw new Error(`Queue worker registration not found for: ${job.queueName}`)
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// Create the queue wire object
|
|
132
|
+
const queue: PikkuQueue = {
|
|
133
|
+
queueName: job.queueName,
|
|
134
|
+
jobId: job.id,
|
|
135
|
+
updateProgress:
|
|
136
|
+
updateProgress ||
|
|
137
|
+
(async (progress: number | string | object) => {
|
|
138
|
+
logger.info(`Job ${job.id} progress: ${progress}`)
|
|
139
|
+
// Default implementation - just log the progress
|
|
140
|
+
}),
|
|
141
|
+
fail: async (reason?: string) => {
|
|
142
|
+
throw new QueueJobFailedError(job.id, reason)
|
|
143
|
+
},
|
|
144
|
+
discard: async (reason?: string) => {
|
|
145
|
+
throw new QueueJobDiscardedError(job.id, reason)
|
|
146
|
+
},
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
try {
|
|
150
|
+
logger.info(`Processing job ${job.id} in queue ${job.queueName}`)
|
|
151
|
+
|
|
152
|
+
const wire: PikkuWire = {
|
|
153
|
+
queue,
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// Execute the pikku function with the job data
|
|
157
|
+
const result = await runPikkuFunc(
|
|
158
|
+
'queue',
|
|
159
|
+
job.queueName,
|
|
160
|
+
processorMeta.pikkuFuncId,
|
|
161
|
+
{
|
|
162
|
+
singletonServices,
|
|
163
|
+
createWireServices,
|
|
164
|
+
auth: false,
|
|
165
|
+
data: () => job.data,
|
|
166
|
+
inheritedMiddleware: processorMeta.middleware,
|
|
167
|
+
wireMiddleware: queueWorker.middleware,
|
|
168
|
+
tags: queueWorker.tags,
|
|
169
|
+
wire,
|
|
170
|
+
}
|
|
171
|
+
)
|
|
172
|
+
|
|
173
|
+
logger.debug(
|
|
174
|
+
`Successfully processed job ${job.id} in queue ${job.queueName}`
|
|
175
|
+
)
|
|
176
|
+
|
|
177
|
+
return result
|
|
178
|
+
} catch (error: any) {
|
|
179
|
+
logger.error(
|
|
180
|
+
`Error processing job ${job.id} in queue ${job.queueName}:`,
|
|
181
|
+
error
|
|
182
|
+
)
|
|
183
|
+
|
|
184
|
+
const errorResponse = getErrorResponse(error)
|
|
185
|
+
if (errorResponse != null) {
|
|
186
|
+
logger.error('Processed error response:', errorResponse)
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// Re-throw the error so the queue service can handle retries/DLQ
|
|
190
|
+
throw error
|
|
191
|
+
}
|
|
192
|
+
}
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import type { CommonWireMeta } from '../../types/core.types.js'
|
|
2
|
+
import type { CorePikkuFunctionConfig } from '../../function/functions.types.js'
|
|
3
|
+
import type { QueueConfigMapping } from './validate-worker-config.js'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Configuration for queue workers - how jobs are processed
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* Configuration for queue workers - how jobs are processed
|
|
10
|
+
*/
|
|
11
|
+
export interface PikkuWorkerConfig {
|
|
12
|
+
/** Optional worker name for identification and monitoring */
|
|
13
|
+
name?: string
|
|
14
|
+
/** Number of messages to process in batch / in parallel */
|
|
15
|
+
batchSize?: number
|
|
16
|
+
/** Number of messages to prefetch for efficiency */
|
|
17
|
+
prefetch?: number
|
|
18
|
+
/** Polling interval for pull-based queues in ms */
|
|
19
|
+
pollInterval?: number
|
|
20
|
+
/** Message visibility timeout in seconds */
|
|
21
|
+
visibilityTimeout?: number
|
|
22
|
+
/** Duration of job lock in milliseconds */
|
|
23
|
+
lockDuration?: number
|
|
24
|
+
/** Number of seconds to wait when queue is empty before polling again */
|
|
25
|
+
drainDelay?: number
|
|
26
|
+
/** Keep N completed jobs for inspection */
|
|
27
|
+
removeOnComplete?: number
|
|
28
|
+
/** Keep N failed jobs for inspection */
|
|
29
|
+
removeOnFail?: number
|
|
30
|
+
/** Maximum number of times a job can be recovered from stalled state */
|
|
31
|
+
maxStalledCount?: number
|
|
32
|
+
/** Condition to start processor at instance creation */
|
|
33
|
+
autorun?: boolean
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Configuration for individual jobs - how jobs behave
|
|
38
|
+
*/
|
|
39
|
+
export interface PikkuJobConfig {
|
|
40
|
+
/** Maximum retry attempts for failed jobs */
|
|
41
|
+
retryAttempts?: number
|
|
42
|
+
/** Initial retry delay in milliseconds */
|
|
43
|
+
retryDelay?: number
|
|
44
|
+
/** Retry backoff strategy */
|
|
45
|
+
retryBackoff?: 'linear' | 'exponential' | 'fixed'
|
|
46
|
+
/** Queue for failed messages after max retries */
|
|
47
|
+
deadLetterQueue?: string
|
|
48
|
+
/** How long to retain completed jobs in seconds */
|
|
49
|
+
messageRetention?: number
|
|
50
|
+
/** Job priority (higher numbers = higher priority) */
|
|
51
|
+
priority?: number
|
|
52
|
+
/** Enable FIFO ordering where supported */
|
|
53
|
+
fifo?: boolean
|
|
54
|
+
/** Job timeout in milliseconds */
|
|
55
|
+
timeout?: number
|
|
56
|
+
/** Delay before job execution in milliseconds */
|
|
57
|
+
delay?: number
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Configuration validation result with warnings and fallbacks
|
|
62
|
+
*/
|
|
63
|
+
export interface ConfigValidationResult {
|
|
64
|
+
applied: Partial<PikkuWorkerConfig>
|
|
65
|
+
ignored: Partial<PikkuWorkerConfig>
|
|
66
|
+
warnings: string[]
|
|
67
|
+
fallbacks: { [key: string]: any }
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Queue job representation
|
|
72
|
+
*/
|
|
73
|
+
export type QueueJobStatus =
|
|
74
|
+
| 'waiting'
|
|
75
|
+
| 'active'
|
|
76
|
+
| 'completed'
|
|
77
|
+
| 'failed'
|
|
78
|
+
| 'delayed'
|
|
79
|
+
|
|
80
|
+
export type QueueJobMetadata = {
|
|
81
|
+
progress?: number | string | object | undefined | boolean
|
|
82
|
+
attemptsMade?: number
|
|
83
|
+
maxAttempts?: number
|
|
84
|
+
processedAt?: Date
|
|
85
|
+
completedAt?: Date
|
|
86
|
+
failedAt?: Date
|
|
87
|
+
error?: string
|
|
88
|
+
createdAt: Date
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export interface QueueJob<T = any, R = any> {
|
|
92
|
+
id: string
|
|
93
|
+
queueName: string
|
|
94
|
+
status: () => Promise<QueueJobStatus> | QueueJobStatus
|
|
95
|
+
data: T
|
|
96
|
+
result?: R
|
|
97
|
+
waitForCompletion?: (ttl?: number) => Promise<R>
|
|
98
|
+
metadata?: () => Promise<QueueJobMetadata> | QueueJobMetadata
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Job options for queue operations
|
|
103
|
+
*/
|
|
104
|
+
export interface JobOptions {
|
|
105
|
+
priority?: number
|
|
106
|
+
delay?: number
|
|
107
|
+
attempts?: number
|
|
108
|
+
backoff?: string | { type: string; delay?: number }
|
|
109
|
+
removeOnComplete?: number
|
|
110
|
+
removeOnFail?: number
|
|
111
|
+
jobId?: string
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Queue provider interface for job publishing operations
|
|
116
|
+
*/
|
|
117
|
+
export interface QueueService {
|
|
118
|
+
/** Whether this queue provider supports job results */
|
|
119
|
+
readonly supportsResults: boolean
|
|
120
|
+
|
|
121
|
+
/** Add a job to the queue with type safety */
|
|
122
|
+
add<T>(queueName: string, data: T, options?: JobOptions): Promise<string>
|
|
123
|
+
|
|
124
|
+
/** Get job status and result */
|
|
125
|
+
getJob<T, R>(queueName: string, jobId: string): Promise<QueueJob<T, R> | null>
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Queue service interface that queue adapters implement
|
|
130
|
+
*/
|
|
131
|
+
export interface QueueWorkers {
|
|
132
|
+
/** Service name identifier */
|
|
133
|
+
name: string
|
|
134
|
+
|
|
135
|
+
/** Whether this queue service supports job results */
|
|
136
|
+
supportsResults: boolean
|
|
137
|
+
|
|
138
|
+
/** Configuration mapping for validation */
|
|
139
|
+
configMappings: QueueConfigMapping
|
|
140
|
+
|
|
141
|
+
/** Scan state and register all compatible processors */
|
|
142
|
+
registerQueues(): Promise<Record<string, ConfigValidationResult[]>>
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Queue processor metadata
|
|
147
|
+
*/
|
|
148
|
+
export type QueueWorkersMeta = Record<
|
|
149
|
+
string,
|
|
150
|
+
CommonWireMeta & {
|
|
151
|
+
name: string
|
|
152
|
+
config?: PikkuWorkerConfig
|
|
153
|
+
}
|
|
154
|
+
>
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Core queue processor definition
|
|
158
|
+
*/
|
|
159
|
+
export type CoreQueueWorker<
|
|
160
|
+
PikkuFunctionConfig extends CorePikkuFunctionConfig<
|
|
161
|
+
any,
|
|
162
|
+
any,
|
|
163
|
+
any
|
|
164
|
+
> = CorePikkuFunctionConfig<any, any, any>,
|
|
165
|
+
> = {
|
|
166
|
+
name: string
|
|
167
|
+
func: PikkuFunctionConfig
|
|
168
|
+
config?: PikkuWorkerConfig
|
|
169
|
+
errors?: string[]
|
|
170
|
+
tags?: string[]
|
|
171
|
+
middleware?: PikkuFunctionConfig['middleware']
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Represents a queue wire object for middleware
|
|
176
|
+
* Provides information and actions for the current queue job execution
|
|
177
|
+
*/
|
|
178
|
+
export interface PikkuQueue {
|
|
179
|
+
/** The name of the queue being processed */
|
|
180
|
+
queueName: string
|
|
181
|
+
/** The current job ID */
|
|
182
|
+
jobId: string
|
|
183
|
+
/** Update job progress (0-100 or custom value) */
|
|
184
|
+
updateProgress: (progress: number | string | object) => Promise<void>
|
|
185
|
+
/** Fail the current job with optional reason */
|
|
186
|
+
fail: (reason?: string) => Promise<void>
|
|
187
|
+
/** Discard/delete the job without retrying */
|
|
188
|
+
discard: (reason?: string) => Promise<void>
|
|
189
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import type { ConfigValidationResult, CoreQueueWorker } from './queue.types.js'
|
|
2
|
+
import { getQueueWorkers } from './queue-runner.js'
|
|
3
|
+
import type { QueueConfigMapping } from './validate-worker-config.js'
|
|
4
|
+
import { validateWorkerConfig } from './validate-worker-config.js'
|
|
5
|
+
import type { Logger } from '../../services/logger.js'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Queue processor registration callback
|
|
9
|
+
*/
|
|
10
|
+
export type QueueRegistrationCallback<T = any> = (
|
|
11
|
+
queueName: string,
|
|
12
|
+
processor: CoreQueueWorker
|
|
13
|
+
) => Promise<T>
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Helper function to register queue processors with validation
|
|
17
|
+
* This centralizes the common logic for looping over processors and validating configs
|
|
18
|
+
*
|
|
19
|
+
* @param configMappings - Configuration mapping for the queue implementation
|
|
20
|
+
* @param registerCallback - Callback to register each individual queue processor
|
|
21
|
+
* @param logger - Optional logger for info/error messages
|
|
22
|
+
* @returns Record of validation results by queue name
|
|
23
|
+
*/
|
|
24
|
+
export async function registerQueueWorkers<T = any>(
|
|
25
|
+
configMappings: QueueConfigMapping,
|
|
26
|
+
logger: Logger,
|
|
27
|
+
registerCallback: QueueRegistrationCallback<T>
|
|
28
|
+
): Promise<Record<string, ConfigValidationResult[]>> {
|
|
29
|
+
const configValidation: Record<string, ConfigValidationResult[]> = {}
|
|
30
|
+
const queueWorkers = getQueueWorkers()
|
|
31
|
+
|
|
32
|
+
for (const [queueName, processor] of queueWorkers) {
|
|
33
|
+
logger?.info(`Registering queue processor: ${queueName}`)
|
|
34
|
+
// Validate the processor configuration
|
|
35
|
+
const validationResult = validateWorkerConfig(
|
|
36
|
+
configMappings,
|
|
37
|
+
processor.config
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
// Store validation results
|
|
41
|
+
configValidation[queueName] = configValidation[queueName] || []
|
|
42
|
+
configValidation[queueName].push(validationResult)
|
|
43
|
+
|
|
44
|
+
if (validationResult.warnings.length > 0) {
|
|
45
|
+
logger?.warn(
|
|
46
|
+
`Configuration warnings for queue ${queueName}:`,
|
|
47
|
+
validationResult.warnings
|
|
48
|
+
)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
try {
|
|
52
|
+
await registerCallback(queueName, processor)
|
|
53
|
+
logger?.info(`Successfully registered queue processor: ${queueName}`)
|
|
54
|
+
} catch (error) {
|
|
55
|
+
logger?.error(`Failed to register queue processor ${queueName}:`, error)
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return configValidation
|
|
60
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { describe, test } from 'node:test'
|
|
2
|
+
import assert from 'node:assert'
|
|
3
|
+
import { validateWorkerConfig } from './validate-worker-config.js'
|
|
4
|
+
import type { QueueConfigMapping } from './validate-worker-config.js'
|
|
5
|
+
|
|
6
|
+
const createTestMapping = (): QueueConfigMapping => ({
|
|
7
|
+
supported: {
|
|
8
|
+
concurrency: {
|
|
9
|
+
queueProperty: 'concurrency',
|
|
10
|
+
description: 'Number of concurrent workers',
|
|
11
|
+
},
|
|
12
|
+
maxRetries: {
|
|
13
|
+
queueProperty: 'maxRetries',
|
|
14
|
+
description: 'Maximum retry attempts',
|
|
15
|
+
},
|
|
16
|
+
},
|
|
17
|
+
unsupported: {
|
|
18
|
+
rateLimitMax: {
|
|
19
|
+
reason: 'Not supported',
|
|
20
|
+
explanation: 'This queue does not support rate limiting',
|
|
21
|
+
},
|
|
22
|
+
},
|
|
23
|
+
fallbacks: {
|
|
24
|
+
retryDelay: {
|
|
25
|
+
reason: 'Partial support',
|
|
26
|
+
explanation: 'Uses fixed delay instead of exponential',
|
|
27
|
+
fallbackValue: '1000ms fixed',
|
|
28
|
+
},
|
|
29
|
+
},
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
describe('validateWorkerConfig', () => {
|
|
33
|
+
test('should return empty results for empty config', () => {
|
|
34
|
+
const mapping = createTestMapping()
|
|
35
|
+
const result = validateWorkerConfig(mapping, {})
|
|
36
|
+
assert.deepStrictEqual(result.applied, {})
|
|
37
|
+
assert.deepStrictEqual(result.ignored, {})
|
|
38
|
+
assert.deepStrictEqual(result.warnings, [])
|
|
39
|
+
assert.deepStrictEqual(result.fallbacks, {})
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
test('should return empty results for undefined config', () => {
|
|
43
|
+
const mapping = createTestMapping()
|
|
44
|
+
const result = validateWorkerConfig(mapping)
|
|
45
|
+
assert.deepStrictEqual(result.applied, {})
|
|
46
|
+
assert.deepStrictEqual(result.ignored, {})
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
test('should apply supported configurations', () => {
|
|
50
|
+
const mapping = createTestMapping()
|
|
51
|
+
const result = validateWorkerConfig(mapping, { concurrency: 5 } as any)
|
|
52
|
+
assert.deepStrictEqual(result.applied, { concurrency: 5 })
|
|
53
|
+
assert.deepStrictEqual(result.ignored, {})
|
|
54
|
+
assert.strictEqual(result.warnings.length, 0)
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
test('should ignore unsupported configurations with warning', () => {
|
|
58
|
+
const mapping = createTestMapping()
|
|
59
|
+
const result = validateWorkerConfig(mapping, { rateLimitMax: 100 } as any)
|
|
60
|
+
assert.deepStrictEqual(result.ignored, { rateLimitMax: 100 })
|
|
61
|
+
assert.strictEqual(result.warnings.length, 1)
|
|
62
|
+
assert.ok(result.warnings[0].includes('Not supported'))
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
test('should handle fallback configurations', () => {
|
|
66
|
+
const mapping = createTestMapping()
|
|
67
|
+
const result = validateWorkerConfig(mapping, { retryDelay: 2000 } as any)
|
|
68
|
+
assert.deepStrictEqual(result.applied, { retryDelay: 2000 })
|
|
69
|
+
assert.deepStrictEqual(result.fallbacks, { retryDelay: '1000ms fixed' })
|
|
70
|
+
assert.strictEqual(result.warnings.length, 1)
|
|
71
|
+
assert.ok(result.warnings[0].includes('Partial support'))
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
test('should warn about unknown configurations', () => {
|
|
75
|
+
const mapping = createTestMapping()
|
|
76
|
+
const result = validateWorkerConfig(mapping, {
|
|
77
|
+
unknownOption: 'val',
|
|
78
|
+
} as any)
|
|
79
|
+
assert.deepStrictEqual(result.ignored, { unknownOption: 'val' })
|
|
80
|
+
assert.strictEqual(result.warnings.length, 1)
|
|
81
|
+
assert.ok(result.warnings[0].includes('Unknown configuration'))
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
test('should skip undefined values', () => {
|
|
85
|
+
const mapping = createTestMapping()
|
|
86
|
+
const result = validateWorkerConfig(mapping, {
|
|
87
|
+
concurrency: undefined,
|
|
88
|
+
} as any)
|
|
89
|
+
assert.deepStrictEqual(result.applied, {})
|
|
90
|
+
})
|
|
91
|
+
|
|
92
|
+
test('should handle mixed supported and unsupported configs', () => {
|
|
93
|
+
const mapping = createTestMapping()
|
|
94
|
+
const result = validateWorkerConfig(mapping, {
|
|
95
|
+
concurrency: 3,
|
|
96
|
+
maxRetries: 5,
|
|
97
|
+
rateLimitMax: 100,
|
|
98
|
+
retryDelay: 1000,
|
|
99
|
+
} as any)
|
|
100
|
+
assert.deepStrictEqual(result.applied, {
|
|
101
|
+
concurrency: 3,
|
|
102
|
+
maxRetries: 5,
|
|
103
|
+
retryDelay: 1000,
|
|
104
|
+
})
|
|
105
|
+
assert.deepStrictEqual(result.ignored, { rateLimitMax: 100 })
|
|
106
|
+
assert.strictEqual(result.warnings.length, 2)
|
|
107
|
+
})
|
|
108
|
+
})
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
PikkuWorkerConfig,
|
|
3
|
+
ConfigValidationResult,
|
|
4
|
+
} from './queue.types.js'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Configuration mapping structure for queue implementations
|
|
8
|
+
*/
|
|
9
|
+
export interface QueueConfigMapping {
|
|
10
|
+
/** Configurations that are directly supported */
|
|
11
|
+
supported: Partial<
|
|
12
|
+
Record<
|
|
13
|
+
keyof PikkuWorkerConfig,
|
|
14
|
+
{
|
|
15
|
+
/** The property name in the underlying queue system */
|
|
16
|
+
queueProperty?: string
|
|
17
|
+
/** Optional transform function for the value */
|
|
18
|
+
transform?: (value: any) => any
|
|
19
|
+
/** Description of what this configuration does */
|
|
20
|
+
description: string
|
|
21
|
+
}
|
|
22
|
+
>
|
|
23
|
+
>
|
|
24
|
+
|
|
25
|
+
/** Configurations that are not supported */
|
|
26
|
+
unsupported: Partial<
|
|
27
|
+
Record<
|
|
28
|
+
keyof PikkuWorkerConfig,
|
|
29
|
+
{
|
|
30
|
+
/** Why this configuration is not supported */
|
|
31
|
+
reason: string
|
|
32
|
+
/** Detailed explanation of what the queue system does instead */
|
|
33
|
+
explanation: string
|
|
34
|
+
}
|
|
35
|
+
>
|
|
36
|
+
>
|
|
37
|
+
|
|
38
|
+
/** Configurations that have partial support or workarounds */
|
|
39
|
+
fallbacks: Partial<
|
|
40
|
+
Record<
|
|
41
|
+
keyof PikkuWorkerConfig,
|
|
42
|
+
{
|
|
43
|
+
/** Why this configuration uses a fallback */
|
|
44
|
+
reason: string
|
|
45
|
+
/** Detailed explanation of the fallback behavior */
|
|
46
|
+
explanation: string
|
|
47
|
+
/** The fallback value or description */
|
|
48
|
+
fallbackValue: string
|
|
49
|
+
}
|
|
50
|
+
>
|
|
51
|
+
>
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Validates worker configuration using a mapping table
|
|
56
|
+
* This provides a flexible, maintainable way to validate configurations
|
|
57
|
+
* across different queue implementations
|
|
58
|
+
*
|
|
59
|
+
* @param config - The worker configuration to validate
|
|
60
|
+
* @param configMapping - The mapping table defining supported/unsupported configurations
|
|
61
|
+
* @returns Validation result with applied, ignored, warnings, and fallbacks
|
|
62
|
+
*/
|
|
63
|
+
export function validateWorkerConfig(
|
|
64
|
+
configMapping: QueueConfigMapping,
|
|
65
|
+
config: PikkuWorkerConfig = {}
|
|
66
|
+
): ConfigValidationResult {
|
|
67
|
+
const applied: Partial<PikkuWorkerConfig> = {}
|
|
68
|
+
const ignored: Partial<PikkuWorkerConfig> = {}
|
|
69
|
+
const warnings: string[] = []
|
|
70
|
+
const fallbacks: { [key: string]: any } = {}
|
|
71
|
+
|
|
72
|
+
// Process each configuration property
|
|
73
|
+
for (const [key, value] of Object.entries(config)) {
|
|
74
|
+
if (value === undefined) continue
|
|
75
|
+
|
|
76
|
+
const configKey = key as keyof PikkuWorkerConfig
|
|
77
|
+
|
|
78
|
+
// Check if it's a supported configuration
|
|
79
|
+
if (configKey in configMapping.supported) {
|
|
80
|
+
applied[configKey] = value
|
|
81
|
+
continue
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// Check if it's a fallback configuration
|
|
85
|
+
if (configKey in configMapping.fallbacks) {
|
|
86
|
+
const fallbackMapping = configMapping.fallbacks[configKey]!
|
|
87
|
+
applied[configKey] = value
|
|
88
|
+
fallbacks[key] = fallbackMapping.fallbackValue
|
|
89
|
+
warnings.push(
|
|
90
|
+
`${key}: ${fallbackMapping.reason}. ${fallbackMapping.explanation}`
|
|
91
|
+
)
|
|
92
|
+
continue
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// Check if it's an unsupported configuration
|
|
96
|
+
if (configKey in configMapping.unsupported) {
|
|
97
|
+
ignored[configKey] = value
|
|
98
|
+
const mapping = configMapping.unsupported[configKey]!
|
|
99
|
+
warnings.push(`${key}: ${mapping.reason}. ${mapping.explanation}`)
|
|
100
|
+
continue
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// Unknown configuration
|
|
104
|
+
ignored[configKey] = value
|
|
105
|
+
warnings.push(
|
|
106
|
+
`${key}: Unknown configuration option for this queue implementation`
|
|
107
|
+
)
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
return {
|
|
111
|
+
applied,
|
|
112
|
+
ignored,
|
|
113
|
+
warnings,
|
|
114
|
+
fallbacks,
|
|
115
|
+
}
|
|
116
|
+
}
|