assignment-user-matcher 1.2.0 → 1.2.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/README.md +18 -2
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/managers/WorkflowManager.d.ts +8 -1
- package/dist/managers/WorkflowManager.d.ts.map +1 -1
- package/dist/managers/WorkflowManager.js +100 -19
- package/dist/matcher.class.d.ts +2 -1
- package/dist/matcher.class.d.ts.map +1 -1
- package/dist/matcher.class.js +26 -12
- package/dist/scoring/match-score.d.ts +11 -1
- package/dist/scoring/match-score.d.ts.map +1 -1
- package/dist/scoring/match-score.js +49 -2
- package/dist/types/matcher.d.ts +21 -3
- package/dist/types/matcher.d.ts.map +1 -1
- package/dist/workflow-builder.d.ts +14 -1
- package/dist/workflow-builder.d.ts.map +1 -1
- package/dist/workflow-builder.js +25 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -51,6 +51,12 @@ Traditional matching systems can become bottlenecks, leading to delays, suboptim
|
|
|
51
51
|
2. **Matching via Tag Intersection:**
|
|
52
52
|
The core matching logic finds users whose tags have a common intersection with an assignment's tags.
|
|
53
53
|
|
|
54
|
+
When using `routingWeights` with the built-in matcher:
|
|
55
|
+
- Only **positive** weights (`> 0`) make assignments eligible.
|
|
56
|
+
- Weight `0` is a **hard veto** (exact tag or suffix wildcard like `lang:*`).
|
|
57
|
+
- Wildcards are **suffix-only** (`prefix*`). Patterns like `skill:*:node` are not treated as wildcards.
|
|
58
|
+
- If a user has `routingWeights` but no positive entries, assignments stay queued.
|
|
59
|
+
|
|
54
60
|
3. **Prioritization Engine:**
|
|
55
61
|
Assignments are typically processed in the order they are received or by a custom prioritization function you can provide. This ensures that older or more critical assignments get attention first. The library aims to match the highest priority assignments to available, suitable users.
|
|
56
62
|
|
|
@@ -213,8 +219,12 @@ type Options = {
|
|
|
213
219
|
// until some are completed or removed.
|
|
214
220
|
maxUserBacklogSize?: number; // Default: 9
|
|
215
221
|
|
|
216
|
-
// Whether to enable
|
|
217
|
-
//
|
|
222
|
+
// Whether to enable default tag injection/matching behavior.
|
|
223
|
+
// Notes for built-in weighted matching (`routingWeights`):
|
|
224
|
+
// - only weights > 0 are eligible
|
|
225
|
+
// - weight 0 is a hard veto (exact and suffix wildcard)
|
|
226
|
+
// - if no positive weights exist, no assignment is pulled from queue
|
|
227
|
+
// If set to false, you should provide a custom `matchingFunction`.
|
|
218
228
|
enableDefaultMatching?: boolean; // Default: true
|
|
219
229
|
|
|
220
230
|
// A custom function to determine the priority of an assignment.
|
|
@@ -228,6 +238,8 @@ type Options = {
|
|
|
228
238
|
// If `enableDefaultMatching` is true, this function can augment or override the default logic.
|
|
229
239
|
// If `enableDefaultMatching` is false, this function is solely responsible for matching.
|
|
230
240
|
// It should return a tuple: `[matchRank, userCost]`.
|
|
241
|
+
// Important: built-in hard-veto/positive-only weighted semantics apply to the
|
|
242
|
+
// built-in matcher. With a custom matchingFunction, you own final scoring logic.
|
|
231
243
|
// - `matchRank`: A score indicating the quality of the match (higher is better).
|
|
232
244
|
// A rank of 0 or less means no match.
|
|
233
245
|
// - `userCost`: A score indicating the "cost" of assigning this task to this user
|
|
@@ -408,6 +420,10 @@ The npm package only ships the compiled artifacts in `dist/`. Before publishing:
|
|
|
408
420
|
2. Build the library: `pnpm run build`
|
|
409
421
|
3. (Optional) Inspect the publish payload: `pnpm pack --dry-run`
|
|
410
422
|
|
|
423
|
+
For version bump + tag release flow (including patch releases), see `RELEASE.md`.
|
|
424
|
+
If you prefer a local helper, copy `scripts/release-patch-local.sh.example` to
|
|
425
|
+
`scripts/release-patch-local.sh` and run it.
|
|
426
|
+
|
|
411
427
|
Consumers can `import AssignmentMatcher from 'assignment-user-matcher'` or `const { AssignmentMatcher } = require('assignment-user-matcher')` without running TypeScript themselves.
|
|
412
428
|
|
|
413
429
|
## License
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import AssignmentMatcher from './matcher.class';
|
|
2
2
|
export type { Assignment, options, Stats, User } from './matcher.class';
|
|
3
|
-
export type { WorkflowDefinition, WorkflowInstance, WorkflowStep, WorkflowEvent, WorkflowEventType, AssignmentResult, ParallelBranchState, DeadLetterEntry, AuditEntry, CircuitBreakerState, WorkflowInstanceWithSnapshot, } from './matcher.class';
|
|
3
|
+
export type { WorkflowDefinition, WorkflowInstance, WorkflowStep, WorkflowTaskType, WorkflowEvent, WorkflowEventType, AssignmentResult, ParallelBranchState, DeadLetterEntry, AuditEntry, CircuitBreakerState, WorkflowInstanceWithSnapshot, } from './matcher.class';
|
|
4
4
|
export { WorkflowBuilder, WorkflowStepBuilder, workflow, linearWorkflow, approvalWorkflow, } from './workflow-builder';
|
|
5
5
|
export * from './utils/cidr';
|
|
6
6
|
export { createKeyBuilders, type KeyBuilders, type RedisKeyConfig } from './utils/keys';
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,iBAAiB,MAAM,iBAAiB,CAAC;AAEhD,YAAY,EAAE,UAAU,EAAE,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,MAAM,iBAAiB,CAAC;AAGxE,YAAY,EACR,kBAAkB,EAClB,gBAAgB,EAChB,YAAY,EACZ,aAAa,EACb,iBAAiB,EACjB,gBAAgB,EAChB,mBAAmB,EACnB,eAAe,EACf,UAAU,EACV,mBAAmB,EACnB,4BAA4B,GAC/B,MAAM,iBAAiB,CAAC;AAGzB,OAAO,EACH,eAAe,EACf,mBAAmB,EACnB,QAAQ,EACR,cAAc,EACd,gBAAgB,GACnB,MAAM,oBAAoB,CAAC;AAG5B,cAAc,cAAc,CAAC;AAC7B,OAAO,EAAE,iBAAiB,EAAE,KAAK,WAAW,EAAE,KAAK,cAAc,EAAE,MAAM,cAAc,CAAC;AAExF,OAAO,EAAE,iBAAiB,EAAE,CAAC;AAC7B,eAAe,iBAAiB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,iBAAiB,MAAM,iBAAiB,CAAC;AAEhD,YAAY,EAAE,UAAU,EAAE,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,MAAM,iBAAiB,CAAC;AAGxE,YAAY,EACR,kBAAkB,EAClB,gBAAgB,EAChB,YAAY,EACZ,gBAAgB,EAChB,aAAa,EACb,iBAAiB,EACjB,gBAAgB,EAChB,mBAAmB,EACnB,eAAe,EACf,UAAU,EACV,mBAAmB,EACnB,4BAA4B,GAC/B,MAAM,iBAAiB,CAAC;AAGzB,OAAO,EACH,eAAe,EACf,mBAAmB,EACnB,QAAQ,EACR,cAAc,EACd,gBAAgB,GACnB,MAAM,oBAAoB,CAAC;AAG5B,cAAc,cAAc,CAAC;AAC7B,OAAO,EAAE,iBAAiB,EAAE,KAAK,WAAW,EAAE,KAAK,cAAc,EAAE,MAAM,cAAc,CAAC;AAExF,OAAO,EAAE,iBAAiB,EAAE,CAAC;AAC7B,eAAe,iBAAiB,CAAC"}
|
|
@@ -1,10 +1,15 @@
|
|
|
1
|
-
import { RedisClientType, WorkflowDefinition, WorkflowInstance, WorkflowEvent, Assignment, WorkflowInstanceWithSnapshot } from '../types/matcher';
|
|
1
|
+
import { RedisClientType, WorkflowDefinition, WorkflowInstance, WorkflowStep, WorkflowEvent, Assignment, WorkflowInstanceWithSnapshot } from '../types/matcher';
|
|
2
2
|
import { KeyBuilders } from '../utils/keys';
|
|
3
3
|
import { ReliabilityManager } from './ReliabilityManager';
|
|
4
4
|
import { TelemetryManager } from './TelemetryManager';
|
|
5
5
|
export interface WorkflowHost {
|
|
6
6
|
addAssignment(assignment: Assignment): Promise<Assignment>;
|
|
7
7
|
matchUsersAssignments(userId: string): Promise<any>;
|
|
8
|
+
executeMachineTask?(args: {
|
|
9
|
+
instance: WorkflowInstance;
|
|
10
|
+
step: WorkflowStep;
|
|
11
|
+
definition: WorkflowDefinition;
|
|
12
|
+
}): Promise<Record<string, any> | void>;
|
|
8
13
|
}
|
|
9
14
|
export declare class WorkflowManager {
|
|
10
15
|
private redisClient;
|
|
@@ -22,6 +27,7 @@ export declare class WorkflowManager {
|
|
|
22
27
|
streamConsumerGroup: string;
|
|
23
28
|
streamConsumerName: string;
|
|
24
29
|
reclaimIntervalMs: number;
|
|
30
|
+
reclaimMinIdleMs?: number;
|
|
25
31
|
});
|
|
26
32
|
init(): Promise<void>;
|
|
27
33
|
private initStreamConsumerGroup;
|
|
@@ -38,6 +44,7 @@ export declare class WorkflowManager {
|
|
|
38
44
|
getActiveWorkflowsForUser(userId: string): Promise<WorkflowInstance[]>;
|
|
39
45
|
cancelWorkflow(instanceId: string): Promise<boolean>;
|
|
40
46
|
private executeWorkflowStep;
|
|
47
|
+
private executeMachineStep;
|
|
41
48
|
private setStepTimeout;
|
|
42
49
|
processExpiredWorkflowSteps(): Promise<number>;
|
|
43
50
|
private createWorkflowAssignment;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"WorkflowManager.d.ts","sourceRoot":"","sources":["../../src/managers/WorkflowManager.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,kBAAkB,EAAE,gBAAgB,
|
|
1
|
+
{"version":3,"file":"WorkflowManager.d.ts","sourceRoot":"","sources":["../../src/managers/WorkflowManager.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,kBAAkB,EAAE,gBAAgB,EAAE,YAAY,EAAE,aAAa,EAAqB,UAAU,EAAE,4BAA4B,EAAuB,MAAM,kBAAkB,CAAC;AACxM,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAC5C,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAC1D,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AAEtD,MAAM,WAAW,YAAY;IACzB,aAAa,CAA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B,cAAc,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EACnC,YAAY,CAAC,EAAE,gBAAgB,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,GAC9C,OAAO,CAAC;QAAE,EAAE,EAAE,OAAO,CAAC;QAAC,QAAQ,CAAC,EAAE,gBAAgB,CAAC;QAAC,KAAK,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC;CAgB3E"}
|
|
@@ -155,33 +155,109 @@ class WorkflowManager {
|
|
|
155
155
|
const expectedVersion = instance.version;
|
|
156
156
|
if (step.parallelStepIds && step.parallelStepIds.length > 0) {
|
|
157
157
|
const branches = [];
|
|
158
|
+
const machineParallelSteps = [];
|
|
158
159
|
for (const parallelStepId of step.parallelStepIds) {
|
|
159
160
|
const parallelStep = definition.steps.find((s) => s.id === parallelStepId);
|
|
160
161
|
if (!parallelStep)
|
|
161
162
|
continue;
|
|
162
|
-
const
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
163
|
+
const taskType = parallelStep.taskType ?? 'assignment';
|
|
164
|
+
if (taskType === 'machine') {
|
|
165
|
+
if (!parallelStep.machineTask?.handler) {
|
|
166
|
+
throw new Error(`Machine step "${parallelStep.id}" is missing machineTask.handler`);
|
|
167
|
+
}
|
|
168
|
+
const syntheticAssignmentId = (0, crypto_1.randomUUID)();
|
|
169
|
+
machineParallelSteps.push({ stepId: parallelStepId, assignmentId: syntheticAssignmentId });
|
|
170
|
+
branches.push({
|
|
171
|
+
stepId: parallelStepId,
|
|
172
|
+
assignmentId: syntheticAssignmentId,
|
|
173
|
+
status: 'pending',
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
else {
|
|
177
|
+
const assignment = await this.createWorkflowAssignment(instance, parallelStep, definition);
|
|
178
|
+
branches.push({
|
|
179
|
+
stepId: parallelStepId,
|
|
180
|
+
assignmentId: assignment.id,
|
|
181
|
+
status: 'pending',
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
const timeoutMs = parallelStep.timeoutMs ?? definition.defaultTimeoutMs;
|
|
185
|
+
if (timeoutMs) {
|
|
186
|
+
await this.setStepTimeout(instance.id, parallelStepId, timeoutMs);
|
|
170
187
|
}
|
|
171
188
|
}
|
|
172
189
|
instance.parallelBranches = branches;
|
|
173
190
|
instance.currentStepId = null;
|
|
191
|
+
instance.currentAssignmentId = undefined;
|
|
192
|
+
await this.saveInstance(instance, expectedVersion);
|
|
193
|
+
for (const machineStep of machineParallelSteps) {
|
|
194
|
+
const parallelStep = definition.steps.find((s) => s.id === machineStep.stepId);
|
|
195
|
+
if (!parallelStep)
|
|
196
|
+
continue;
|
|
197
|
+
await this.executeMachineStep(instance, parallelStep, definition, machineStep.assignmentId);
|
|
198
|
+
}
|
|
199
|
+
return;
|
|
174
200
|
}
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
201
|
+
const taskType = step.taskType ?? 'assignment';
|
|
202
|
+
if (taskType === 'machine') {
|
|
203
|
+
if (!step.machineTask?.handler) {
|
|
204
|
+
throw new Error(`Machine step "${step.id}" is missing machineTask.handler`);
|
|
205
|
+
}
|
|
206
|
+
instance.currentAssignmentId = undefined;
|
|
178
207
|
instance.currentStepId = stepId;
|
|
179
|
-
|
|
180
|
-
|
|
208
|
+
const timeoutMs = step.timeoutMs ?? definition.defaultTimeoutMs;
|
|
209
|
+
if (timeoutMs) {
|
|
210
|
+
await this.setStepTimeout(instance.id, stepId, timeoutMs);
|
|
181
211
|
}
|
|
212
|
+
await this.saveInstance(instance, expectedVersion);
|
|
213
|
+
await this.executeMachineStep(instance, step, definition);
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
const assignment = await this.createWorkflowAssignment(instance, step, definition);
|
|
217
|
+
instance.currentAssignmentId = assignment.id;
|
|
218
|
+
instance.currentStepId = stepId;
|
|
219
|
+
const timeoutMs = step.timeoutMs ?? definition.defaultTimeoutMs;
|
|
220
|
+
if (timeoutMs) {
|
|
221
|
+
await this.setStepTimeout(instance.id, stepId, timeoutMs);
|
|
182
222
|
}
|
|
183
223
|
await this.saveInstance(instance, expectedVersion);
|
|
184
224
|
}
|
|
225
|
+
async executeMachineStep(instance, step, definition, assignmentId) {
|
|
226
|
+
const syntheticAssignmentId = assignmentId ?? (0, crypto_1.randomUUID)();
|
|
227
|
+
const eventBase = {
|
|
228
|
+
eventId: (0, crypto_1.randomUUID)(),
|
|
229
|
+
type: 'COMPLETED',
|
|
230
|
+
userId: instance.initiatorUserId,
|
|
231
|
+
assignmentId: syntheticAssignmentId,
|
|
232
|
+
workflowInstanceId: instance.id,
|
|
233
|
+
stepId: step.id,
|
|
234
|
+
timestamp: Date.now(),
|
|
235
|
+
};
|
|
236
|
+
try {
|
|
237
|
+
if (!this.host.executeMachineTask) {
|
|
238
|
+
throw new Error(`Machine task executor not configured for step "${step.id}" (handler: ${step.machineTask?.handler ?? 'unknown'})`);
|
|
239
|
+
}
|
|
240
|
+
const result = await this.host.executeMachineTask({
|
|
241
|
+
instance,
|
|
242
|
+
step,
|
|
243
|
+
definition,
|
|
244
|
+
});
|
|
245
|
+
await this.processWorkflowEvent({
|
|
246
|
+
...eventBase,
|
|
247
|
+
type: 'COMPLETED',
|
|
248
|
+
payload: result ? { ...result } : undefined,
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
catch (err) {
|
|
252
|
+
await this.processWorkflowEvent({
|
|
253
|
+
...eventBase,
|
|
254
|
+
type: 'FAILED',
|
|
255
|
+
payload: {
|
|
256
|
+
error: err?.message ?? String(err),
|
|
257
|
+
},
|
|
258
|
+
});
|
|
259
|
+
}
|
|
260
|
+
}
|
|
185
261
|
async setStepTimeout(instanceId, stepId, timeoutMs) {
|
|
186
262
|
const expiryKey = this.keys.workflowStepExpiry(instanceId, stepId);
|
|
187
263
|
const expiryTime = Date.now() + timeoutMs;
|
|
@@ -206,7 +282,8 @@ class WorkflowManager {
|
|
|
206
282
|
if (!definition)
|
|
207
283
|
continue;
|
|
208
284
|
const step = definition.steps.find((s) => s.id === currentStepId);
|
|
209
|
-
|
|
285
|
+
const timeoutMs = step?.timeoutMs ?? definition.defaultTimeoutMs;
|
|
286
|
+
if (!timeoutMs)
|
|
210
287
|
continue;
|
|
211
288
|
await this.publishWorkflowEvent({
|
|
212
289
|
eventId: (0, crypto_1.randomUUID)(),
|
|
@@ -223,19 +300,23 @@ class WorkflowManager {
|
|
|
223
300
|
return expiredCount;
|
|
224
301
|
}
|
|
225
302
|
async createWorkflowAssignment(instance, step, definition) {
|
|
303
|
+
if (!step.assignmentTemplate) {
|
|
304
|
+
throw new Error(`Assignment step "${step.id}" is missing assignmentTemplate`);
|
|
305
|
+
}
|
|
306
|
+
const targetUserSelector = step.targetUser ?? 'initiator';
|
|
226
307
|
let targetUserId;
|
|
227
|
-
if (
|
|
308
|
+
if (targetUserSelector === 'initiator') {
|
|
228
309
|
targetUserId = instance.initiatorUserId;
|
|
229
310
|
}
|
|
230
|
-
else if (
|
|
311
|
+
else if (targetUserSelector === 'previous') {
|
|
231
312
|
const lastEntry = instance.history[instance.history.length - 1];
|
|
232
313
|
targetUserId = lastEntry?.userId ?? instance.initiatorUserId;
|
|
233
314
|
}
|
|
234
|
-
else if (typeof
|
|
315
|
+
else if (typeof targetUserSelector === 'object' && 'tag' in targetUserSelector) {
|
|
235
316
|
targetUserId = '';
|
|
236
317
|
}
|
|
237
318
|
else {
|
|
238
|
-
targetUserId =
|
|
319
|
+
targetUserId = targetUserSelector;
|
|
239
320
|
}
|
|
240
321
|
const assignmentId = (0, crypto_1.randomUUID)();
|
|
241
322
|
const assignment = {
|
|
@@ -572,7 +653,7 @@ class WorkflowManager {
|
|
|
572
653
|
const streamKey = this.keys.eventStream();
|
|
573
654
|
let totalReclaimed = 0;
|
|
574
655
|
try {
|
|
575
|
-
const result = await this.subscriberClient.xAutoClaim(streamKey, this.options.streamConsumerGroup, this.options.streamConsumerName, this.options.reclaimIntervalMs, '0-0', { COUNT: 100 });
|
|
656
|
+
const result = await this.subscriberClient.xAutoClaim(streamKey, this.options.streamConsumerGroup, this.options.streamConsumerName, this.options.reclaimMinIdleMs ?? this.options.reclaimIntervalMs, '0-0', { COUNT: 100 });
|
|
576
657
|
if (result && result.messages && result.messages.length > 0) {
|
|
577
658
|
for (const message of result.messages) {
|
|
578
659
|
if (!message)
|
package/dist/matcher.class.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { type PaginationOptions, type PaginationResult, type AssignmentCounts }
|
|
|
2
2
|
import { type WorkflowHost } from './managers/WorkflowManager';
|
|
3
3
|
import type { RedisClientType, User, Assignment, Stats, MatcherOptions, WorkflowDefinition, WorkflowInstance, WorkflowEvent, AssignmentResult, DeadLetterEntry, AuditEntry, WorkflowInstanceWithSnapshot } from './types/matcher';
|
|
4
4
|
export type { RedisClientType, User, Assignment, Stats, MatcherOptions, options } from './types/matcher';
|
|
5
|
-
export type { WorkflowDefinition, WorkflowInstance, WorkflowStep, WorkflowEvent, WorkflowEventType, AssignmentResult, ParallelBranchState, DeadLetterEntry, AuditEntry, CircuitBreakerState, WorkflowInstanceWithSnapshot, } from './types/matcher';
|
|
5
|
+
export type { WorkflowDefinition, WorkflowInstance, WorkflowStep, WorkflowTaskType, WorkflowEvent, WorkflowEventType, AssignmentResult, ParallelBranchState, DeadLetterEntry, AuditEntry, CircuitBreakerState, WorkflowInstanceWithSnapshot, } from './types/matcher';
|
|
6
6
|
export default class AssignmentMatcher implements WorkflowHost {
|
|
7
7
|
redisClient: RedisClientType;
|
|
8
8
|
relevantBatchSize: number;
|
|
@@ -27,6 +27,7 @@ export default class AssignmentMatcher implements WorkflowHost {
|
|
|
27
27
|
private reliability;
|
|
28
28
|
private telemetry;
|
|
29
29
|
private luaScriptSha;
|
|
30
|
+
private usingDefaultMatchScore;
|
|
30
31
|
constructor(redisClient: RedisClientType, options?: MatcherOptions);
|
|
31
32
|
/** Keys for the three assignment stores (used by pagination queries) */
|
|
32
33
|
private get assignmentStoreKeys();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"matcher.class.d.ts","sourceRoot":"","sources":["../src/matcher.class.ts"],"names":[],"mappings":"AAKA,OAAO,EAMH,KAAK,iBAAiB,EACtB,KAAK,gBAAgB,EACrB,KAAK,gBAAgB,EACxB,MAAM,sBAAsB,CAAC;AAI9B,OAAO,EAAmB,KAAK,YAAY,EAAE,MAAM,4BAA4B,CAAC;AAChF,OAAO,KAAK,EACR,eAAe,EACf,IAAI,EACJ,UAAU,EACV,KAAK,EACL,cAAc,EAEd,kBAAkB,EAClB,gBAAgB,EAChB,aAAa,EACb,gBAAgB,EAChB,eAAe,EACf,UAAU,EACV,4BAA4B,EAC/B,MAAM,iBAAiB,CAAC;AAGzB,YAAY,EAAE,eAAe,EAAE,IAAI,EAAE,UAAU,EAAE,KAAK,EAAE,cAAc,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAEzG,YAAY,EACR,kBAAkB,EAClB,gBAAgB,EAChB,YAAY,EACZ,aAAa,EACb,iBAAiB,EACjB,gBAAgB,EAChB,mBAAmB,EACnB,eAAe,EACf,UAAU,EACV,mBAAmB,EACnB,4BAA4B,GAC/B,MAAM,iBAAiB,CAAC;AAqBzB,MAAM,CAAC,OAAO,OAAO,iBAAkB,YAAW,YAAY;
|
|
1
|
+
{"version":3,"file":"matcher.class.d.ts","sourceRoot":"","sources":["../src/matcher.class.ts"],"names":[],"mappings":"AAKA,OAAO,EAMH,KAAK,iBAAiB,EACtB,KAAK,gBAAgB,EACrB,KAAK,gBAAgB,EACxB,MAAM,sBAAsB,CAAC;AAI9B,OAAO,EAAmB,KAAK,YAAY,EAAE,MAAM,4BAA4B,CAAC;AAChF,OAAO,KAAK,EACR,eAAe,EACf,IAAI,EACJ,UAAU,EACV,KAAK,EACL,cAAc,EAEd,kBAAkB,EAClB,gBAAgB,EAChB,aAAa,EACb,gBAAgB,EAChB,eAAe,EACf,UAAU,EACV,4BAA4B,EAC/B,MAAM,iBAAiB,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AAC;IAIxC;;OAEG;IACG,gBAAgB,IAAI,OAAO,CAAC,IAAI,CAAC;IAOjC,qBAAqB,IAAI,OAAO,CAAC,MAAM,CAAC;IAmC9C,OAAO,CAAC,mBAAmB,CAA+B;IAE1D,wBAAwB,CAAC,UAAU,GAAE,MAAc;IAOnD,uBAAuB;IAOvB;;;OAGG;YACW,8BAA8B;YA6B9B,kBAAkB;CAwBnC"}
|
package/dist/matcher.class.js
CHANGED
|
@@ -49,6 +49,7 @@ class AssignmentMatcher {
|
|
|
49
49
|
this.acceptedAssignmentsKey = this.keys.acceptedAssignments();
|
|
50
50
|
this.completedAssignmentsKey = this.keys.completedAssignments();
|
|
51
51
|
this.calculatePriority = options?.prioritizationFunction ?? this.calculatePriority;
|
|
52
|
+
this.usingDefaultMatchScore = !options?.matchingFunction;
|
|
52
53
|
this.matchScore = options?.matchingFunction ?? this.defaultMatchScore.bind(this);
|
|
53
54
|
// Workflow options
|
|
54
55
|
this.enableWorkflows = options?.enableWorkflows ?? false;
|
|
@@ -69,7 +70,8 @@ class AssignmentMatcher {
|
|
|
69
70
|
enableWorkflows: this.enableWorkflows,
|
|
70
71
|
streamConsumerGroup: this.streamConsumerGroup,
|
|
71
72
|
streamConsumerName: this.streamConsumerName,
|
|
72
|
-
reclaimIntervalMs: options?.
|
|
73
|
+
reclaimIntervalMs: options?.workflowReclaimPollIntervalMs ?? 5000,
|
|
74
|
+
reclaimMinIdleMs: options?.workflowOrphanReclaimMs ?? 60000,
|
|
73
75
|
});
|
|
74
76
|
this.initRedis();
|
|
75
77
|
}
|
|
@@ -307,8 +309,8 @@ class AssignmentMatcher {
|
|
|
307
309
|
return (0, pagination_1.getAssignmentsByIdsBatch)(this.redisClient, this.assignmentStoreKeys, ids);
|
|
308
310
|
}
|
|
309
311
|
/** Default match score implementation using extracted scoring module */
|
|
310
|
-
defaultMatchScore(user, assignmentTags, assignmentPriority, _assignmentId) {
|
|
311
|
-
return Promise.resolve((0, match_score_1.calculateMatchScore)(user, assignmentTags, assignmentPriority, this.enableDefaultMatching));
|
|
312
|
+
defaultMatchScore(user, assignmentTags, assignmentPriority, _assignmentId, skillThresholds) {
|
|
313
|
+
return Promise.resolve((0, match_score_1.calculateMatchScore)(user, assignmentTags, assignmentPriority, this.enableDefaultMatching, skillThresholds));
|
|
312
314
|
}
|
|
313
315
|
async calculatePriority(...args) {
|
|
314
316
|
const [assignment] = args;
|
|
@@ -389,6 +391,8 @@ class AssignmentMatcher {
|
|
|
389
391
|
}
|
|
390
392
|
async getUserRelatedAssignments(user) {
|
|
391
393
|
const userAssignments = [];
|
|
394
|
+
const routingWeights = user.routingWeights;
|
|
395
|
+
const hasRoutingWeights = Boolean(routingWeights && Object.keys(routingWeights).length > 0);
|
|
392
396
|
// Priority 1: Check for workflow-targeted assignments (deterministic matching)
|
|
393
397
|
if (this.enableWorkflows) {
|
|
394
398
|
const workflowAssignments = await this.getWorkflowTargetedAssignments(user.id);
|
|
@@ -403,8 +407,8 @@ class AssignmentMatcher {
|
|
|
403
407
|
// Priority 2: Build candidate assignments using per-tag zset union with user weights for scalability.
|
|
404
408
|
const positiveWeights = [];
|
|
405
409
|
const zeroWeightTags = [];
|
|
406
|
-
if (
|
|
407
|
-
for (const [tag, w] of Object.entries(
|
|
410
|
+
if (hasRoutingWeights) {
|
|
411
|
+
for (const [tag, w] of Object.entries(routingWeights)) {
|
|
408
412
|
if (typeof w === 'number' && w > 0)
|
|
409
413
|
positiveWeights.push({ tag, weight: w });
|
|
410
414
|
else if (w === 0)
|
|
@@ -417,18 +421,26 @@ class AssignmentMatcher {
|
|
|
417
421
|
positiveWeights.push({ tag, weight: 1 });
|
|
418
422
|
}
|
|
419
423
|
}
|
|
420
|
-
// Ensure default tag is included
|
|
421
|
-
|
|
424
|
+
// Ensure default tag is included for unweighted users.
|
|
425
|
+
// In weighted + default scoring path, only explicit positive weights may match.
|
|
426
|
+
if (this.enableDefaultMatching && !hasRoutingWeights) {
|
|
422
427
|
const defaultInPos = positiveWeights.find((t) => t.tag === 'default');
|
|
423
|
-
const defaultZero = zeroWeightTags.
|
|
428
|
+
const defaultZero = zeroWeightTags.some((tag) => tag === 'default' || (tag.endsWith('*') && 'default'.startsWith(tag.slice(0, -1))));
|
|
424
429
|
if (!defaultInPos && !defaultZero)
|
|
425
430
|
positiveWeights.push({ tag: 'default', weight: 1 });
|
|
426
431
|
}
|
|
432
|
+
// In weighted default matching, users must define at least one positive weight.
|
|
433
|
+
// If they don't, nothing should be assigned and queue remains intact.
|
|
434
|
+
if (hasRoutingWeights && this.usingDefaultMatchScore && positiveWeights.length === 0) {
|
|
435
|
+
return userAssignments;
|
|
436
|
+
}
|
|
427
437
|
if (positiveWeights.length === 0)
|
|
428
438
|
return userAssignments;
|
|
429
439
|
// Expand wildcards
|
|
430
440
|
const expandedPositive = await this.expandTagWildcards(positiveWeights);
|
|
431
|
-
const expandedZero =
|
|
441
|
+
const expandedZero = this.usingDefaultMatchScore
|
|
442
|
+
? await this.expandTagWildcards(zeroWeightTags.map((t) => ({ tag: t, weight: 0 })))
|
|
443
|
+
: [];
|
|
432
444
|
const finalZeroTags = expandedZero.map((t) => t.tag);
|
|
433
445
|
if (expandedPositive.length === 0)
|
|
434
446
|
return userAssignments;
|
|
@@ -482,16 +494,18 @@ class AssignmentMatcher {
|
|
|
482
494
|
const tags = String(flat[i * 3 + 1] || '');
|
|
483
495
|
const assignmentJson = flat[i * 3 + 2];
|
|
484
496
|
let allowedCidrs;
|
|
497
|
+
let skillThresholds;
|
|
485
498
|
if (assignmentJson) {
|
|
486
499
|
try {
|
|
487
500
|
const parsed = JSON.parse(assignmentJson);
|
|
488
501
|
allowedCidrs = parsed.allowedCidrs;
|
|
502
|
+
skillThresholds = parsed.skillThresholds;
|
|
489
503
|
}
|
|
490
504
|
catch {
|
|
491
505
|
// Ignore JSON parse errors
|
|
492
506
|
}
|
|
493
507
|
}
|
|
494
|
-
details.push({ id: assignmentId, basePriority: priority, tags, allowedCidrs });
|
|
508
|
+
details.push({ id: assignmentId, basePriority: priority, tags, allowedCidrs, skillThresholds });
|
|
495
509
|
}
|
|
496
510
|
// Prefer higher base priority first to satisfy tests and practical expectations
|
|
497
511
|
details.sort((a, b) => b.basePriority - a.basePriority);
|
|
@@ -503,8 +517,8 @@ class AssignmentMatcher {
|
|
|
503
517
|
if (!(0, cidr_1.checkCidrMatch)(user.ip, d.allowedCidrs)) {
|
|
504
518
|
continue; // Skip this assignment - user IP doesn't match allowed CIDRs
|
|
505
519
|
}
|
|
506
|
-
// Use custom matchScore (supports weights) for a final check and tie-break
|
|
507
|
-
const [score, combinedPriority] = await this.matchScore(user, d.tags, d.basePriority, d.id);
|
|
520
|
+
// Use custom matchScore (supports weights and thresholds) for a final check and tie-break
|
|
521
|
+
const [score, combinedPriority] = await this.matchScore(user, d.tags, d.basePriority, d.id, d.skillThresholds);
|
|
508
522
|
if (score && combinedPriority) {
|
|
509
523
|
userAssignments.push({ id: d.id, priority: combinedPriority });
|
|
510
524
|
selected.push({ id: d.id, tags: d.tags });
|
|
@@ -6,11 +6,21 @@ import type { User } from '../matcher.class';
|
|
|
6
6
|
* Check if a pattern matches a tag (supports wildcard patterns ending with *)
|
|
7
7
|
*/
|
|
8
8
|
export declare function matchesPattern(pattern: string, tag: string): boolean;
|
|
9
|
+
/**
|
|
10
|
+
* Get the effective weight for a skill tag from user's routingWeights
|
|
11
|
+
* Supports wildcard patterns (e.g., 'eng*' matches 'english')
|
|
12
|
+
*/
|
|
13
|
+
export declare function getEffectiveWeight(routingWeights: Record<string, number>, tag: string): number;
|
|
14
|
+
/**
|
|
15
|
+
* Check if user meets all skill thresholds
|
|
16
|
+
* Returns true if all thresholds are met, false otherwise
|
|
17
|
+
*/
|
|
18
|
+
export declare function meetsSkillThresholds(routingWeights: Record<string, number> | undefined, skillThresholds: Record<string, number> | undefined): boolean;
|
|
9
19
|
/**
|
|
10
20
|
* Calculate match score between a user and assignment tags
|
|
11
21
|
* Returns [score, combinedPriority]
|
|
12
22
|
*/
|
|
13
|
-
export declare function calculateMatchScore(user: User, assignmentTags: string, assignmentPriority: string | number, enableDefaultMatching: boolean): [number, number];
|
|
23
|
+
export declare function calculateMatchScore(user: User, assignmentTags: string, assignmentPriority: string | number, enableDefaultMatching: boolean, skillThresholds?: Record<string, number>): [number, number];
|
|
14
24
|
/**
|
|
15
25
|
* Calculate default priority based on creation time
|
|
16
26
|
* Note: Returns 0 if createdAt is undefined to match original behavior
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"match-score.d.ts","sourceRoot":"","sources":["../../src/scoring/match-score.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AAE7C;;GAEG;AACH,wBAAgB,cAAc,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO,CAOpE;AAED;;;GAGG;AACH,wBAAgB,mBAAmB,CAC/B,IAAI,EAAE,IAAI,EACV,cAAc,EAAE,MAAM,EACtB,kBAAkB,EAAE,MAAM,GAAG,MAAM,EACnC,qBAAqB,EAAE,OAAO,
|
|
1
|
+
{"version":3,"file":"match-score.d.ts","sourceRoot":"","sources":["../../src/scoring/match-score.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AAE7C;;GAEG;AACH,wBAAgB,cAAc,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO,CAOpE;AAED;;;GAGG;AACH,wBAAgB,kBAAkB,CAC9B,cAAc,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,EACtC,GAAG,EAAE,MAAM,GACZ,MAAM,CAYR;AAED;;;GAGG;AACH,wBAAgB,oBAAoB,CAChC,cAAc,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,SAAS,EAClD,eAAe,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,SAAS,GACpD,OAAO,CAeT;AAED;;;GAGG;AACH,wBAAgB,mBAAmB,CAC/B,IAAI,EAAE,IAAI,EACV,cAAc,EAAE,MAAM,EACtB,kBAAkB,EAAE,MAAM,GAAG,MAAM,EACnC,qBAAqB,EAAE,OAAO,EAC9B,eAAe,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GACzC,CAAC,MAAM,EAAE,MAAM,CAAC,CAoElB;AAED;;;GAGG;AACH,wBAAgB,wBAAwB,CAAC,SAAS,CAAC,EAAE,MAAM,GAAG,MAAM,CAGnE"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.calculateDefaultPriority = exports.calculateMatchScore = exports.matchesPattern = void 0;
|
|
3
|
+
exports.calculateDefaultPriority = exports.calculateMatchScore = exports.meetsSkillThresholds = exports.getEffectiveWeight = exports.matchesPattern = void 0;
|
|
4
4
|
/**
|
|
5
5
|
* Check if a pattern matches a tag (supports wildcard patterns ending with *)
|
|
6
6
|
*/
|
|
@@ -14,14 +14,56 @@ function matchesPattern(pattern, tag) {
|
|
|
14
14
|
return false;
|
|
15
15
|
}
|
|
16
16
|
exports.matchesPattern = matchesPattern;
|
|
17
|
+
/**
|
|
18
|
+
* Get the effective weight for a skill tag from user's routingWeights
|
|
19
|
+
* Supports wildcard patterns (e.g., 'eng*' matches 'english')
|
|
20
|
+
*/
|
|
21
|
+
function getEffectiveWeight(routingWeights, tag) {
|
|
22
|
+
// First check for exact match
|
|
23
|
+
if (tag in routingWeights) {
|
|
24
|
+
return routingWeights[tag];
|
|
25
|
+
}
|
|
26
|
+
// Check wildcard patterns
|
|
27
|
+
for (const [pattern, weight] of Object.entries(routingWeights)) {
|
|
28
|
+
if (matchesPattern(pattern, tag)) {
|
|
29
|
+
return weight;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return 0;
|
|
33
|
+
}
|
|
34
|
+
exports.getEffectiveWeight = getEffectiveWeight;
|
|
35
|
+
/**
|
|
36
|
+
* Check if user meets all skill thresholds
|
|
37
|
+
* Returns true if all thresholds are met, false otherwise
|
|
38
|
+
*/
|
|
39
|
+
function meetsSkillThresholds(routingWeights, skillThresholds) {
|
|
40
|
+
if (!skillThresholds || Object.keys(skillThresholds).length === 0) {
|
|
41
|
+
return true; // No thresholds to check
|
|
42
|
+
}
|
|
43
|
+
if (!routingWeights || Object.keys(routingWeights).length === 0) {
|
|
44
|
+
return false; // User has no weights but thresholds exist
|
|
45
|
+
}
|
|
46
|
+
for (const [skill, minWeight] of Object.entries(skillThresholds)) {
|
|
47
|
+
const userWeight = getEffectiveWeight(routingWeights, skill);
|
|
48
|
+
if (userWeight < minWeight) {
|
|
49
|
+
return false;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return true;
|
|
53
|
+
}
|
|
54
|
+
exports.meetsSkillThresholds = meetsSkillThresholds;
|
|
17
55
|
/**
|
|
18
56
|
* Calculate match score between a user and assignment tags
|
|
19
57
|
* Returns [score, combinedPriority]
|
|
20
58
|
*/
|
|
21
|
-
function calculateMatchScore(user, assignmentTags, assignmentPriority, enableDefaultMatching) {
|
|
59
|
+
function calculateMatchScore(user, assignmentTags, assignmentPriority, enableDefaultMatching, skillThresholds) {
|
|
22
60
|
const aTags = assignmentTags ? assignmentTags.split(',') : [];
|
|
23
61
|
// If weighted skills are present, use them for scoring
|
|
24
62
|
if (user.routingWeights && Object.keys(user.routingWeights).length > 0) {
|
|
63
|
+
// Check skill thresholds first - reject if not met
|
|
64
|
+
if (!meetsSkillThresholds(user.routingWeights, skillThresholds)) {
|
|
65
|
+
return [0, Number(assignmentPriority) || 0];
|
|
66
|
+
}
|
|
25
67
|
// Check for exclusions (weight 0)
|
|
26
68
|
for (const [pattern, weight] of Object.entries(user.routingWeights)) {
|
|
27
69
|
if (weight === 0) {
|
|
@@ -52,6 +94,11 @@ function calculateMatchScore(user, assignmentTags, assignmentPriority, enableDef
|
|
|
52
94
|
const base = Number(assignmentPriority) || 0;
|
|
53
95
|
return [weightSum, base + weightSum];
|
|
54
96
|
}
|
|
97
|
+
// For tag-based matching (no routingWeights), check thresholds
|
|
98
|
+
// If thresholds exist but user has no routingWeights, reject
|
|
99
|
+
if (skillThresholds && Object.keys(skillThresholds).length > 0) {
|
|
100
|
+
return [0, Number(assignmentPriority) || 0];
|
|
101
|
+
}
|
|
55
102
|
// Fallback to default tag-intersection scoring
|
|
56
103
|
const userTagSet = new Set(user.tags);
|
|
57
104
|
const assignmentTagSet = new Set(aTags);
|
package/dist/types/matcher.d.ts
CHANGED
|
@@ -12,6 +12,7 @@ export type Assignment = {
|
|
|
12
12
|
tags: string[];
|
|
13
13
|
priority?: number;
|
|
14
14
|
allowedCidrs?: string[];
|
|
15
|
+
skillThresholds?: Record<string, number>;
|
|
15
16
|
[key: string]: any;
|
|
16
17
|
};
|
|
17
18
|
export type Stats = {
|
|
@@ -26,7 +27,7 @@ export type MatcherOptions = {
|
|
|
26
27
|
enableDefaultMatching?: boolean;
|
|
27
28
|
matchExpirationMs?: number;
|
|
28
29
|
prioritizationFunction?: (...args: (Assignment | undefined)[]) => Promise<number>;
|
|
29
|
-
matchingFunction?: (user: User, assignmentTags: string, assignmentPriority: number | string, assignmentId?: string) => Promise<[number, number]>;
|
|
30
|
+
matchingFunction?: (user: User, assignmentTags: string, assignmentPriority: number | string, assignmentId?: string, skillThresholds?: Record<string, number>) => Promise<[number, number]>;
|
|
30
31
|
/** Enable workflow orchestration features */
|
|
31
32
|
enableWorkflows?: boolean;
|
|
32
33
|
/** Consumer group name for Redis Streams (defaults to 'orchestrator') */
|
|
@@ -39,6 +40,8 @@ export type MatcherOptions = {
|
|
|
39
40
|
workflowIdempotencyTtlMs?: number;
|
|
40
41
|
/** Minimum idle time before reclaiming orphaned messages in ms (default: 60000 = 1min) */
|
|
41
42
|
workflowOrphanReclaimMs?: number;
|
|
43
|
+
/** Polling interval for reclaim loop in ms (default: 5000) */
|
|
44
|
+
workflowReclaimPollIntervalMs?: number;
|
|
42
45
|
/** Number of failures before circuit breaker opens (default: 5) */
|
|
43
46
|
workflowCircuitBreakerThreshold?: number;
|
|
44
47
|
/** Time to wait before attempting to close circuit breaker in ms (default: 30000) */
|
|
@@ -54,6 +57,8 @@ export type MatcherOptions = {
|
|
|
54
57
|
export type options = MatcherOptions;
|
|
55
58
|
/** Event types for workflow lifecycle */
|
|
56
59
|
export type WorkflowEventType = 'STARTED' | 'COMPLETED' | 'REJECTED' | 'EXPIRED' | 'FAILED';
|
|
60
|
+
/** Step execution mode */
|
|
61
|
+
export type WorkflowTaskType = 'assignment' | 'machine';
|
|
57
62
|
/** Event published to Redis Streams for workflow orchestration */
|
|
58
63
|
export interface WorkflowEvent {
|
|
59
64
|
eventId: string;
|
|
@@ -78,12 +83,21 @@ export interface WorkflowStep {
|
|
|
78
83
|
id: string;
|
|
79
84
|
/** Human-readable name */
|
|
80
85
|
name: string;
|
|
86
|
+
/** Step execution mode (default: 'assignment') */
|
|
87
|
+
taskType?: WorkflowTaskType;
|
|
81
88
|
/** Assignment template - merged with workflow context when creating the assignment */
|
|
82
|
-
assignmentTemplate
|
|
89
|
+
assignmentTemplate?: Partial<Assignment>;
|
|
83
90
|
/** Target user selector: 'initiator' | 'previous' | specific userId | tag-based selector */
|
|
84
|
-
targetUser
|
|
91
|
+
targetUser?: 'initiator' | 'previous' | string | {
|
|
85
92
|
tag: string;
|
|
86
93
|
};
|
|
94
|
+
/** Machine task metadata used for code/task worker execution */
|
|
95
|
+
machineTask?: {
|
|
96
|
+
/** Machine handler identifier (resolver-specific) */
|
|
97
|
+
handler: string;
|
|
98
|
+
/** Optional static input merged with workflow context */
|
|
99
|
+
input?: Record<string, any>;
|
|
100
|
+
};
|
|
87
101
|
/** Routing rules for branching (evaluated in order, first match wins) */
|
|
88
102
|
routing?: WorkflowRouting[];
|
|
89
103
|
/** Default next step if no routing condition matches (null = end workflow) */
|
|
@@ -218,8 +232,12 @@ export interface WorkflowInstanceWithSnapshot extends WorkflowInstance {
|
|
|
218
232
|
export interface WorkflowStepBuilder {
|
|
219
233
|
/** Set the step name */
|
|
220
234
|
name(name: string): WorkflowStepBuilder;
|
|
235
|
+
/** Set the execution task type */
|
|
236
|
+
taskType(type: WorkflowTaskType): WorkflowStepBuilder;
|
|
221
237
|
/** Set the assignment template */
|
|
222
238
|
assignment(template: Partial<Assignment>): WorkflowStepBuilder;
|
|
239
|
+
/** Configure machine task metadata */
|
|
240
|
+
machineTask(handler: string, input?: Record<string, any>): WorkflowStepBuilder;
|
|
223
241
|
/** Set the target user */
|
|
224
242
|
targetUser(target: 'initiator' | 'previous' | string | {
|
|
225
243
|
tag: string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"matcher.d.ts","sourceRoot":"","sources":["../../src/types/matcher.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,OAAO,CAAC;AAErC,MAAM,MAAM,eAAe,GAAG,UAAU,CAAC,OAAO,YAAY,CAAC,CAAC;AAE9D,MAAM,WAAW,IAAI;IACjB,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,EAAE,CAAC;IAGf,cAAc,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAGxC,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAC;CACtB;AAED,MAAM,MAAM,UAAU,GAAG;IACrB,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,EAAE,CAAC;IACf,QAAQ,CAAC,EAAE,MAAM,CAAC;IAIlB,YAAY,CAAC,EAAE,MAAM,EAAE,CAAC;
|
|
1
|
+
{"version":3,"file":"matcher.d.ts","sourceRoot":"","sources":["../../src/types/matcher.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,OAAO,CAAC;AAErC,MAAM,MAAM,eAA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GAAG,mBAAmB,CAAC;IACjD,sCAAsC;IACtC,UAAU,CAAC,IAAI,EAAE,OAAO,GAAG,mBAAmB,CAAC;IAC/C,yBAAyB;IACzB,SAAS,CAAC,MAAM,EAAE,OAAO,GAAG,UAAU,GAAG,OAAO,GAAG,mBAAmB,CAAC;IACvE,sBAAsB;IACtB,OAAO,CAAC,KAAK,EAAE,MAAM,GAAG,mBAAmB,CAAC;IAC5C,uBAAuB;IACvB,OAAO,CAAC,EAAE,EAAE,MAAM,GAAG,mBAAmB,CAAC;IACzC,qBAAqB;IACrB,KAAK,IAAI,YAAY,CAAC;CACzB;AAED,uDAAuD;AACvD,MAAM,WAAW,yBAAyB;IACtC,4BAA4B;IAC5B,IAAI,CAAC,IAAI,EAAE,MAAM,GAAG,yBAAyB,CAAC;IAC9C,+BAA+B;IAC/B,OAAO,CAAC,OAAO,EAAE,MAAM,GAAG,yBAAyB,CAAC;IACpD,8BAA8B;IAC9B,SAAS,CAAC,MAAM,EAAE,MAAM,GAAG,yBAAyB,CAAC;IACrD,iCAAiC;IACjC,OAAO,CAAC,IAAI,EAAE,YAAY,GAAG,yBAAyB,CAAC;IACvD,wCAAwC;IACxC,cAAc,CAAC,EAAE,EAAE,MAAM,GAAG,yBAAyB,CAAC;IACtD,mBAAmB;IACnB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,yBAAyB,CAAC;IAC/D,oCAAoC;IACpC,KAAK,IAAI,kBAAkB,CAAC;CAC/B"}
|
|
@@ -37,7 +37,7 @@
|
|
|
37
37
|
* .build();
|
|
38
38
|
* ```
|
|
39
39
|
*/
|
|
40
|
-
import type { WorkflowDefinition, WorkflowStep, Assignment } from './types/matcher';
|
|
40
|
+
import type { WorkflowDefinition, WorkflowStep, Assignment, WorkflowTaskType } from './types/matcher';
|
|
41
41
|
/**
|
|
42
42
|
* Builder for individual workflow steps.
|
|
43
43
|
*/
|
|
@@ -45,6 +45,10 @@ export declare class WorkflowStepBuilder {
|
|
|
45
45
|
private step;
|
|
46
46
|
private parentBuilder;
|
|
47
47
|
constructor(id: string, parentBuilder: WorkflowBuilder);
|
|
48
|
+
/**
|
|
49
|
+
* Set execution mode for this step.
|
|
50
|
+
*/
|
|
51
|
+
taskType(type: WorkflowTaskType): this;
|
|
48
52
|
/**
|
|
49
53
|
* Set the step name.
|
|
50
54
|
*/
|
|
@@ -53,6 +57,10 @@ export declare class WorkflowStepBuilder {
|
|
|
53
57
|
* Set the assignment template for this step.
|
|
54
58
|
*/
|
|
55
59
|
assignment(template: Partial<Assignment>): this;
|
|
60
|
+
/**
|
|
61
|
+
* Configure this step as a machine/code task.
|
|
62
|
+
*/
|
|
63
|
+
machineTask(handler: string, input?: Record<string, any>): this;
|
|
56
64
|
/**
|
|
57
65
|
* Set the target user for this step.
|
|
58
66
|
* @param target - 'initiator' | 'previous' | userId | { tag: string }
|
|
@@ -166,6 +174,11 @@ export declare function linearWorkflow(id: string, name: string, steps: Array<{
|
|
|
166
174
|
targetUser?: 'initiator' | 'previous' | string | {
|
|
167
175
|
tag: string;
|
|
168
176
|
};
|
|
177
|
+
taskType?: WorkflowTaskType;
|
|
178
|
+
machineTask?: {
|
|
179
|
+
handler: string;
|
|
180
|
+
input?: Record<string, any>;
|
|
181
|
+
};
|
|
169
182
|
timeoutMs?: number;
|
|
170
183
|
}>): WorkflowDefinition;
|
|
171
184
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"workflow-builder.d.ts","sourceRoot":"","sources":["../src/workflow-builder.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsCG;AAEH,OAAO,KAAK,EACR,kBAAkB,EAClB,YAAY,
|
|
1
|
+
{"version":3,"file":"workflow-builder.d.ts","sourceRoot":"","sources":["../src/workflow-builder.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsCG;AAEH,OAA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W,CAAC,EAAE;QACV,OAAO,EAAE,MAAM,CAAC;QAChB,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;KAC/B,CAAC;IACF,SAAS,CAAC,EAAE,MAAM,CAAC;CACtB,CAAC,GACH,kBAAkB,CAuBpB;AAED;;GAEG;AACH,wBAAgB,gBAAgB,CAC5B,EAAE,EAAE,MAAM,EACV,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE;IACL,gBAAgB,EAAE,OAAO,CAAC,UAAU,CAAC,CAAC;IACtC,gBAAgB,EAAE,OAAO,CAAC,UAAU,CAAC,CAAC;IACtC,kBAAkB,CAAC,EAAE,OAAO,CAAC,UAAU,CAAC,CAAC;IACzC,kBAAkB,CAAC,EAAE,OAAO,CAAC,UAAU,CAAC,CAAC;IACzC,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,eAAe,CAAC,EAAE,MAAM,CAAC;CAC5B,GACF,kBAAkB,CA6BpB"}
|
package/dist/workflow-builder.js
CHANGED
|
@@ -49,10 +49,18 @@ class WorkflowStepBuilder {
|
|
|
49
49
|
this.step = {
|
|
50
50
|
id,
|
|
51
51
|
name: id,
|
|
52
|
+
taskType: 'assignment',
|
|
52
53
|
assignmentTemplate: {},
|
|
53
54
|
targetUser: 'initiator',
|
|
54
55
|
};
|
|
55
56
|
}
|
|
57
|
+
/**
|
|
58
|
+
* Set execution mode for this step.
|
|
59
|
+
*/
|
|
60
|
+
taskType(type) {
|
|
61
|
+
this.step.taskType = type;
|
|
62
|
+
return this;
|
|
63
|
+
}
|
|
56
64
|
/**
|
|
57
65
|
* Set the step name.
|
|
58
66
|
*/
|
|
@@ -67,6 +75,14 @@ class WorkflowStepBuilder {
|
|
|
67
75
|
this.step.assignmentTemplate = template;
|
|
68
76
|
return this;
|
|
69
77
|
}
|
|
78
|
+
/**
|
|
79
|
+
* Configure this step as a machine/code task.
|
|
80
|
+
*/
|
|
81
|
+
machineTask(handler, input) {
|
|
82
|
+
this.step.taskType = 'machine';
|
|
83
|
+
this.step.machineTask = { handler, input };
|
|
84
|
+
return this;
|
|
85
|
+
}
|
|
70
86
|
/**
|
|
71
87
|
* Set the target user for this step.
|
|
72
88
|
* @param target - 'initiator' | 'previous' | userId | { tag: string }
|
|
@@ -135,6 +151,9 @@ class WorkflowStepBuilder {
|
|
|
135
151
|
* Finish configuring this step and return to the parent builder.
|
|
136
152
|
*/
|
|
137
153
|
done() {
|
|
154
|
+
if ((this.step.taskType ?? 'assignment') === 'machine' && !this.step.machineTask?.handler) {
|
|
155
|
+
throw new Error(`Machine step "${this.step.id}" requires machineTask(handler)`);
|
|
156
|
+
}
|
|
138
157
|
this.parentBuilder._addStep(this.step);
|
|
139
158
|
return this.parentBuilder;
|
|
140
159
|
}
|
|
@@ -241,6 +260,10 @@ class WorkflowBuilder {
|
|
|
241
260
|
}
|
|
242
261
|
// Validate all referenced step IDs exist
|
|
243
262
|
for (const step of this.steps) {
|
|
263
|
+
const taskType = step.taskType ?? 'assignment';
|
|
264
|
+
if (taskType === 'machine' && !step.machineTask?.handler) {
|
|
265
|
+
throw new Error(`Step "${step.id}" has taskType "machine" but no machineTask.handler`);
|
|
266
|
+
}
|
|
244
267
|
if (step.routing) {
|
|
245
268
|
for (const route of step.routing) {
|
|
246
269
|
if (!this.steps.find(s => s.id === route.targetStepId)) {
|
|
@@ -291,8 +314,10 @@ function linearWorkflow(id, name, steps) {
|
|
|
291
314
|
const workflowSteps = steps.map((step, index) => ({
|
|
292
315
|
id: step.id,
|
|
293
316
|
name: step.name,
|
|
317
|
+
taskType: step.taskType ?? 'assignment',
|
|
294
318
|
assignmentTemplate: step.assignment,
|
|
295
319
|
targetUser: step.targetUser ?? 'initiator',
|
|
320
|
+
machineTask: step.machineTask,
|
|
296
321
|
defaultNextStepId: index < steps.length - 1 ? steps[index + 1].id : null,
|
|
297
322
|
timeoutMs: step.timeoutMs,
|
|
298
323
|
}));
|