@veridid/workflow 0.0.0-stage → 1.0.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/LICENSE +202 -0
- package/README.md +43 -2
- package/build/WorkflowEvents.d.mts +40 -0
- package/build/WorkflowEvents.d.mts.map +1 -0
- package/build/WorkflowEvents.mjs +11 -0
- package/build/WorkflowEvents.mjs.map +1 -0
- package/build/WorkflowModule.d.mts +24 -0
- package/build/WorkflowModule.d.mts.map +1 -0
- package/build/WorkflowModule.mjs +479 -0
- package/build/WorkflowModule.mjs.map +1 -0
- package/build/WorkflowModuleConfig.d.mts +53 -0
- package/build/WorkflowModuleConfig.d.mts.map +1 -0
- package/build/WorkflowModuleConfig.mjs +21 -0
- package/build/WorkflowModuleConfig.mjs.map +1 -0
- package/build/_virtual/_@oxc-project_runtime@0.112.0/helpers/decorate.mjs +14 -0
- package/build/_virtual/_@oxc-project_runtime@0.112.0/helpers/decorateMetadata.mjs +11 -0
- package/build/_virtual/_@oxc-project_runtime@0.112.0/helpers/decorateParam.mjs +9 -0
- package/build/_virtual/_rolldown/runtime.mjs +41 -0
- package/build/actions/ActionRegistry.d.mts +65 -0
- package/build/actions/ActionRegistry.d.mts.map +1 -0
- package/build/actions/ActionRegistry.mjs +360 -0
- package/build/actions/ActionRegistry.mjs.map +1 -0
- package/build/api/WorkflowApi.d.mts +99 -0
- package/build/api/WorkflowApi.d.mts.map +1 -0
- package/build/api/WorkflowApi.mjs +228 -0
- package/build/api/WorkflowApi.mjs.map +1 -0
- package/build/engine/AttributePlanner.d.mts +12 -0
- package/build/engine/AttributePlanner.d.mts.map +1 -0
- package/build/engine/AttributePlanner.mjs +58 -0
- package/build/engine/AttributePlanner.mjs.map +1 -0
- package/build/engine/GuardEvaluator.d.mts +16 -0
- package/build/engine/GuardEvaluator.d.mts.map +1 -0
- package/build/engine/GuardEvaluator.mjs +38 -0
- package/build/engine/GuardEvaluator.mjs.map +1 -0
- package/build/index.d.mts +46 -0
- package/build/index.mjs +53 -0
- package/build/index.mjs.map +1 -0
- package/build/model/TemplateValidation.d.mts +8 -0
- package/build/model/TemplateValidation.d.mts.map +1 -0
- package/build/model/TemplateValidation.mjs +278 -0
- package/build/model/TemplateValidation.mjs.map +1 -0
- package/build/model/types.d.mts +120 -0
- package/build/model/types.d.mts.map +1 -0
- package/build/model/types.mjs +11 -0
- package/build/model/types.mjs.map +1 -0
- package/build/protocol/WorkflowMessageTypes.d.mts +19 -0
- package/build/protocol/WorkflowMessageTypes.d.mts.map +1 -0
- package/build/protocol/WorkflowMessageTypes.mjs +20 -0
- package/build/protocol/WorkflowMessageTypes.mjs.map +1 -0
- package/build/protocol/handlers/AdvanceHandler.d.mts +16 -0
- package/build/protocol/handlers/AdvanceHandler.d.mts.map +1 -0
- package/build/protocol/handlers/AdvanceHandler.mjs +181 -0
- package/build/protocol/handlers/AdvanceHandler.mjs.map +1 -0
- package/build/protocol/handlers/CancelHandler.d.mts +16 -0
- package/build/protocol/handlers/CancelHandler.d.mts.map +1 -0
- package/build/protocol/handlers/CancelHandler.mjs +103 -0
- package/build/protocol/handlers/CancelHandler.mjs.map +1 -0
- package/build/protocol/handlers/CompleteHandler.d.mts +16 -0
- package/build/protocol/handlers/CompleteHandler.d.mts.map +1 -0
- package/build/protocol/handlers/CompleteHandler.mjs +103 -0
- package/build/protocol/handlers/CompleteHandler.mjs.map +1 -0
- package/build/protocol/handlers/DiscoverHandler.d.mts +15 -0
- package/build/protocol/handlers/DiscoverHandler.d.mts.map +1 -0
- package/build/protocol/handlers/DiscoverHandler.mjs +73 -0
- package/build/protocol/handlers/DiscoverHandler.mjs.map +1 -0
- package/build/protocol/handlers/FetchTemplateHandler.d.mts +16 -0
- package/build/protocol/handlers/FetchTemplateHandler.d.mts.map +1 -0
- package/build/protocol/handlers/FetchTemplateHandler.mjs +64 -0
- package/build/protocol/handlers/FetchTemplateHandler.mjs.map +1 -0
- package/build/protocol/handlers/PauseHandler.d.mts +16 -0
- package/build/protocol/handlers/PauseHandler.d.mts.map +1 -0
- package/build/protocol/handlers/PauseHandler.mjs +103 -0
- package/build/protocol/handlers/PauseHandler.mjs.map +1 -0
- package/build/protocol/handlers/ProblemReportHandler.d.mts +11 -0
- package/build/protocol/handlers/ProblemReportHandler.d.mts.map +1 -0
- package/build/protocol/handlers/ProblemReportHandler.mjs +26 -0
- package/build/protocol/handlers/ProblemReportHandler.mjs.map +1 -0
- package/build/protocol/handlers/PublishTemplateHandler.d.mts +15 -0
- package/build/protocol/handlers/PublishTemplateHandler.d.mts.map +1 -0
- package/build/protocol/handlers/PublishTemplateHandler.mjs +47 -0
- package/build/protocol/handlers/PublishTemplateHandler.mjs.map +1 -0
- package/build/protocol/handlers/ResumeHandler.d.mts +16 -0
- package/build/protocol/handlers/ResumeHandler.d.mts.map +1 -0
- package/build/protocol/handlers/ResumeHandler.mjs +103 -0
- package/build/protocol/handlers/ResumeHandler.mjs.map +1 -0
- package/build/protocol/handlers/StartHandler.d.mts +16 -0
- package/build/protocol/handlers/StartHandler.d.mts.map +1 -0
- package/build/protocol/handlers/StartHandler.mjs +241 -0
- package/build/protocol/handlers/StartHandler.mjs.map +1 -0
- package/build/protocol/handlers/StatusHandler.d.mts +16 -0
- package/build/protocol/handlers/StatusHandler.d.mts.map +1 -0
- package/build/protocol/handlers/StatusHandler.mjs +174 -0
- package/build/protocol/handlers/StatusHandler.mjs.map +1 -0
- package/build/protocol/handlers/TemplateHandler.d.mts +14 -0
- package/build/protocol/handlers/TemplateHandler.d.mts.map +1 -0
- package/build/protocol/handlers/TemplateHandler.mjs +66 -0
- package/build/protocol/handlers/TemplateHandler.mjs.map +1 -0
- package/build/protocol/messages/AdvanceMessage.d.mts +24 -0
- package/build/protocol/messages/AdvanceMessage.d.mts.map +1 -0
- package/build/protocol/messages/AdvanceMessage.mjs +25 -0
- package/build/protocol/messages/AdvanceMessage.mjs.map +1 -0
- package/build/protocol/messages/CancelMessage.d.mts +20 -0
- package/build/protocol/messages/CancelMessage.d.mts.map +1 -0
- package/build/protocol/messages/CancelMessage.mjs +25 -0
- package/build/protocol/messages/CancelMessage.mjs.map +1 -0
- package/build/protocol/messages/CompleteMessage.d.mts +20 -0
- package/build/protocol/messages/CompleteMessage.d.mts.map +1 -0
- package/build/protocol/messages/CompleteMessage.mjs +25 -0
- package/build/protocol/messages/CompleteMessage.mjs.map +1 -0
- package/build/protocol/messages/DiscoverMessage.d.mts +29 -0
- package/build/protocol/messages/DiscoverMessage.d.mts.map +1 -0
- package/build/protocol/messages/DiscoverMessage.mjs +25 -0
- package/build/protocol/messages/DiscoverMessage.mjs.map +1 -0
- package/build/protocol/messages/FetchTemplateMessage.d.mts +21 -0
- package/build/protocol/messages/FetchTemplateMessage.d.mts.map +1 -0
- package/build/protocol/messages/FetchTemplateMessage.mjs +31 -0
- package/build/protocol/messages/FetchTemplateMessage.mjs.map +1 -0
- package/build/protocol/messages/PauseMessage.d.mts +20 -0
- package/build/protocol/messages/PauseMessage.d.mts.map +1 -0
- package/build/protocol/messages/PauseMessage.mjs +25 -0
- package/build/protocol/messages/PauseMessage.mjs.map +1 -0
- package/build/protocol/messages/ProblemReportMessage.d.mts +21 -0
- package/build/protocol/messages/ProblemReportMessage.d.mts.map +1 -0
- package/build/protocol/messages/ProblemReportMessage.mjs +25 -0
- package/build/protocol/messages/ProblemReportMessage.mjs.map +1 -0
- package/build/protocol/messages/PublishTemplateMessage.d.mts +23 -0
- package/build/protocol/messages/PublishTemplateMessage.d.mts.map +1 -0
- package/build/protocol/messages/PublishTemplateMessage.mjs +24 -0
- package/build/protocol/messages/PublishTemplateMessage.mjs.map +1 -0
- package/build/protocol/messages/ResumeMessage.d.mts +20 -0
- package/build/protocol/messages/ResumeMessage.d.mts.map +1 -0
- package/build/protocol/messages/ResumeMessage.mjs +25 -0
- package/build/protocol/messages/ResumeMessage.mjs.map +1 -0
- package/build/protocol/messages/StartMessage.d.mts +28 -0
- package/build/protocol/messages/StartMessage.d.mts.map +1 -0
- package/build/protocol/messages/StartMessage.mjs +25 -0
- package/build/protocol/messages/StartMessage.mjs.map +1 -0
- package/build/protocol/messages/StatusMessage.d.mts +35 -0
- package/build/protocol/messages/StatusMessage.d.mts.map +1 -0
- package/build/protocol/messages/StatusMessage.mjs +25 -0
- package/build/protocol/messages/StatusMessage.mjs.map +1 -0
- package/build/protocol/messages/StatusRequestMessage.d.mts +29 -0
- package/build/protocol/messages/StatusRequestMessage.d.mts.map +1 -0
- package/build/protocol/messages/StatusRequestMessage.mjs +25 -0
- package/build/protocol/messages/StatusRequestMessage.mjs.map +1 -0
- package/build/protocol/messages/TemplateMessage.d.mts +20 -0
- package/build/protocol/messages/TemplateMessage.d.mts.map +1 -0
- package/build/protocol/messages/TemplateMessage.mjs +25 -0
- package/build/protocol/messages/TemplateMessage.mjs.map +1 -0
- package/build/protocol/messages/WorkflowsMessage.d.mts +28 -0
- package/build/protocol/messages/WorkflowsMessage.d.mts.map +1 -0
- package/build/protocol/messages/WorkflowsMessage.mjs +25 -0
- package/build/protocol/messages/WorkflowsMessage.mjs.map +1 -0
- package/build/queue/CommandQueue.d.mts +22 -0
- package/build/queue/CommandQueue.d.mts.map +1 -0
- package/build/queue/CommandQueue.mjs +6 -0
- package/build/queue/CommandQueue.mjs.map +1 -0
- package/build/queue/PersistentCommandQueue.d.mts +56 -0
- package/build/queue/PersistentCommandQueue.d.mts.map +1 -0
- package/build/queue/PersistentCommandQueue.mjs +281 -0
- package/build/queue/PersistentCommandQueue.mjs.map +1 -0
- package/build/repository/WorkflowCommandRecord.d.mts +50 -0
- package/build/repository/WorkflowCommandRecord.d.mts.map +1 -0
- package/build/repository/WorkflowCommandRecord.mjs +48 -0
- package/build/repository/WorkflowCommandRecord.mjs.map +1 -0
- package/build/repository/WorkflowCommandRepository.d.mts +59 -0
- package/build/repository/WorkflowCommandRepository.d.mts.map +1 -0
- package/build/repository/WorkflowCommandRepository.mjs +136 -0
- package/build/repository/WorkflowCommandRepository.mjs.map +1 -0
- package/build/repository/WorkflowInstanceRecord.d.mts +65 -0
- package/build/repository/WorkflowInstanceRecord.d.mts.map +1 -0
- package/build/repository/WorkflowInstanceRecord.mjs +44 -0
- package/build/repository/WorkflowInstanceRecord.mjs.map +1 -0
- package/build/repository/WorkflowInstanceRepository.d.mts +15 -0
- package/build/repository/WorkflowInstanceRepository.d.mts.map +1 -0
- package/build/repository/WorkflowInstanceRepository.mjs +48 -0
- package/build/repository/WorkflowInstanceRepository.mjs.map +1 -0
- package/build/repository/WorkflowTemplateRecord.d.mts +27 -0
- package/build/repository/WorkflowTemplateRecord.d.mts.map +1 -0
- package/build/repository/WorkflowTemplateRecord.mjs +29 -0
- package/build/repository/WorkflowTemplateRecord.mjs.map +1 -0
- package/build/repository/WorkflowTemplateRepository.d.mts +11 -0
- package/build/repository/WorkflowTemplateRepository.d.mts.map +1 -0
- package/build/repository/WorkflowTemplateRepository.mjs +43 -0
- package/build/repository/WorkflowTemplateRepository.mjs.map +1 -0
- package/build/services/WorkflowService.d.mts +109 -0
- package/build/services/WorkflowService.d.mts.map +1 -0
- package/build/services/WorkflowService.mjs +640 -0
- package/build/services/WorkflowService.mjs.map +1 -0
- package/build/ui/UiFilter.mjs +144 -0
- package/build/ui/UiFilter.mjs.map +1 -0
- package/build/utils/peerMerge.mjs +49 -0
- package/build/utils/peerMerge.mjs.map +1 -0
- package/package.json +50 -3
|
@@ -0,0 +1,640 @@
|
|
|
1
|
+
import { __toCommonJS } from "../_virtual/_rolldown/runtime.mjs";
|
|
2
|
+
import { WorkflowModuleConfig } from "../WorkflowModuleConfig.mjs";
|
|
3
|
+
import { __decorateMetadata, init_decorateMetadata } from "../_virtual/_@oxc-project_runtime@0.112.0/helpers/decorateMetadata.mjs";
|
|
4
|
+
import { __decorate, init_decorate } from "../_virtual/_@oxc-project_runtime@0.112.0/helpers/decorate.mjs";
|
|
5
|
+
import { WorkflowInstanceRecord } from "../repository/WorkflowInstanceRecord.mjs";
|
|
6
|
+
import { WorkflowInstanceRepository } from "../repository/WorkflowInstanceRepository.mjs";
|
|
7
|
+
import { WorkflowTemplateRecord } from "../repository/WorkflowTemplateRecord.mjs";
|
|
8
|
+
import { WorkflowTemplateRepository } from "../repository/WorkflowTemplateRepository.mjs";
|
|
9
|
+
import { WorkflowEventTypes } from "../WorkflowEvents.mjs";
|
|
10
|
+
import { ActionRegistry, IssueCredentialMessageV2Action, IssueCredentialV2Action, LocalStateSetAction, PresentProofV2Action, ProposeCredentialV2Action, RequestCredentialV2Action } from "../actions/ActionRegistry.mjs";
|
|
11
|
+
import { GuardEvaluator, init_GuardEvaluator } from "../engine/GuardEvaluator.mjs";
|
|
12
|
+
import { validateTemplateJson, validateTemplateRefs } from "../model/TemplateValidation.mjs";
|
|
13
|
+
import { ensureArray, findSectionForState, transitionsFromState } from "../model/types.mjs";
|
|
14
|
+
import { CompleteMessage } from "../protocol/messages/CompleteMessage.mjs";
|
|
15
|
+
import { RESERVED_ARTIFACT_KEYS, sanitizePeerObject } from "../utils/peerMerge.mjs";
|
|
16
|
+
import { UiFilter_exports, init_UiFilter } from "../ui/UiFilter.mjs";
|
|
17
|
+
import { AgentConfig, AgentContext, EventEmitter, Hasher, TypedArrayEncoder, injectable } from "@credo-ts/core";
|
|
18
|
+
import { DidCommConnectionService, DidCommMessageSender, DidCommOutboundMessageContext, ReturnRouteTypes } from "@credo-ts/didcomm";
|
|
19
|
+
|
|
20
|
+
//#region src/services/WorkflowService.ts
|
|
21
|
+
init_GuardEvaluator();
|
|
22
|
+
init_decorateMetadata();
|
|
23
|
+
init_decorate();
|
|
24
|
+
var _ref, _ref2, _ref3, _ref4, _ref5;
|
|
25
|
+
const stableStringify = (obj) => {
|
|
26
|
+
const allKeys = [];
|
|
27
|
+
JSON.stringify(obj, (k, v) => {
|
|
28
|
+
allKeys.push(k);
|
|
29
|
+
return v;
|
|
30
|
+
});
|
|
31
|
+
allKeys.sort();
|
|
32
|
+
return JSON.stringify(obj, allKeys);
|
|
33
|
+
};
|
|
34
|
+
const sha256 = (input) => {
|
|
35
|
+
const hash = Hasher.hash(TypedArrayEncoder.fromUtf8String(input), "sha-256");
|
|
36
|
+
return TypedArrayEncoder.toHex(hash);
|
|
37
|
+
};
|
|
38
|
+
let WorkflowService = class WorkflowService {
|
|
39
|
+
constructor(templateRepo, instanceRepo, config, agentConfig, eventEmitter) {
|
|
40
|
+
this.templateRepo = templateRepo;
|
|
41
|
+
this.instanceRepo = instanceRepo;
|
|
42
|
+
this.config = config;
|
|
43
|
+
this.agentConfig = agentConfig;
|
|
44
|
+
this.eventEmitter = eventEmitter;
|
|
45
|
+
this.actions = new ActionRegistry();
|
|
46
|
+
this.actions.register(new LocalStateSetAction());
|
|
47
|
+
this.actions.register(new ProposeCredentialV2Action());
|
|
48
|
+
this.actions.register(new IssueCredentialV2Action());
|
|
49
|
+
this.actions.register(new RequestCredentialV2Action());
|
|
50
|
+
this.actions.register(new IssueCredentialMessageV2Action());
|
|
51
|
+
this.actions.register(new PresentProofV2Action());
|
|
52
|
+
}
|
|
53
|
+
registerActions(actions) {
|
|
54
|
+
for (const a of actions) this.actions.register(a);
|
|
55
|
+
}
|
|
56
|
+
async publishTemplate(agentContext, template) {
|
|
57
|
+
validateTemplateJson(template);
|
|
58
|
+
validateTemplateRefs(template);
|
|
59
|
+
const hash = sha256(stableStringify(template));
|
|
60
|
+
const existing = await this.templateRepo.findByTemplateIdAndVersion(agentContext, template.template_id, template.version);
|
|
61
|
+
if (existing) {
|
|
62
|
+
existing.template = template;
|
|
63
|
+
existing.hash = hash;
|
|
64
|
+
await this.templateRepo.update(agentContext, existing);
|
|
65
|
+
return existing;
|
|
66
|
+
}
|
|
67
|
+
const record = new WorkflowTemplateRecord({
|
|
68
|
+
template,
|
|
69
|
+
hash
|
|
70
|
+
});
|
|
71
|
+
await this.templateRepo.save(agentContext, record);
|
|
72
|
+
return record;
|
|
73
|
+
}
|
|
74
|
+
async start(agentContext, opts) {
|
|
75
|
+
if (opts.instance_id) try {
|
|
76
|
+
const existingByInstance = await this.instanceRepo.getByInstanceId(agentContext, opts.instance_id);
|
|
77
|
+
if (existingByInstance) return existingByInstance;
|
|
78
|
+
} catch {}
|
|
79
|
+
const tplRec = await this.templateRepo.findByTemplateIdAndVersion(agentContext, opts.template_id, opts.template_version);
|
|
80
|
+
if (!tplRec) throw this.problem("invalid_template", `template not found: ${opts.template_id}@${opts.template_version || "latest"}`);
|
|
81
|
+
const tpl = tplRec.template;
|
|
82
|
+
const startState = tpl.states.find((s) => s.type === "start")?.name || tpl.states[0]?.name;
|
|
83
|
+
if (!startState) throw this.problem("invalid_template", "no start state");
|
|
84
|
+
const policy = tpl.instance_policy;
|
|
85
|
+
const connectionId = opts.connection_id;
|
|
86
|
+
if (policy.mode === "singleton_per_connection") {
|
|
87
|
+
const existing = ensureArray(await this.instanceRepo.findByTemplateAndConnection(agentContext, tpl.template_id, connectionId)).shift();
|
|
88
|
+
if (existing) {
|
|
89
|
+
if (this.config.autoReturnExistingOnSingleton) return existing;
|
|
90
|
+
throw this.problem("already_exists", "instance already exists for template/connection");
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
let multiplicityKeyValue;
|
|
94
|
+
if (policy.mode === "multi_per_connection" && policy.multiplicity_key) {
|
|
95
|
+
multiplicityKeyValue = this.evalMultiplicity(policy.multiplicity_key, opts.context || {});
|
|
96
|
+
const dup = ensureArray(await this.instanceRepo.findByTemplateConnAndMultiplicity(agentContext, tpl.template_id, connectionId, multiplicityKeyValue)).shift();
|
|
97
|
+
if (dup) return dup;
|
|
98
|
+
}
|
|
99
|
+
let participants = opts.participants || {};
|
|
100
|
+
try {
|
|
101
|
+
if (connectionId) {
|
|
102
|
+
const conn = await agentContext.dependencyManager.resolve(DidCommConnectionService).getById(agentContext, connectionId);
|
|
103
|
+
const theirDid = conn?.theirDid;
|
|
104
|
+
const myDid = conn?.did || conn?.myDid;
|
|
105
|
+
if (theirDid) {
|
|
106
|
+
const holder = participants.holder || {};
|
|
107
|
+
if (!holder.did) holder.did = theirDid;
|
|
108
|
+
participants.holder = holder;
|
|
109
|
+
}
|
|
110
|
+
if (myDid) {
|
|
111
|
+
const issuer = participants.issuer || {};
|
|
112
|
+
if (!issuer.did) issuer.did = myDid;
|
|
113
|
+
participants.issuer = issuer;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
} catch {}
|
|
117
|
+
const instanceId = opts.instance_id || this.uuid();
|
|
118
|
+
const section = findSectionForState(tpl, startState);
|
|
119
|
+
const rec = new WorkflowInstanceRecord({
|
|
120
|
+
instanceId,
|
|
121
|
+
templateId: tpl.template_id,
|
|
122
|
+
templateVersion: tpl.version,
|
|
123
|
+
connectionId,
|
|
124
|
+
participants,
|
|
125
|
+
state: startState,
|
|
126
|
+
section,
|
|
127
|
+
context: { ...opts.context || {} },
|
|
128
|
+
artifacts: sanitizePeerObject(opts.artifacts, RESERVED_ARTIFACT_KEYS),
|
|
129
|
+
status: "active",
|
|
130
|
+
history: [],
|
|
131
|
+
multiplicityKeyValue,
|
|
132
|
+
idempotencyKeys: []
|
|
133
|
+
});
|
|
134
|
+
await this.instanceRepo.save(agentContext, rec);
|
|
135
|
+
try {
|
|
136
|
+
this.eventEmitter?.emit(agentContext, {
|
|
137
|
+
type: WorkflowEventTypes.WorkflowInstanceStateChanged,
|
|
138
|
+
payload: {
|
|
139
|
+
instanceRecord: rec,
|
|
140
|
+
previousState: null,
|
|
141
|
+
newState: rec.state,
|
|
142
|
+
event: "start",
|
|
143
|
+
actionKey: void 0,
|
|
144
|
+
msgId: void 0
|
|
145
|
+
}
|
|
146
|
+
});
|
|
147
|
+
} catch (_e) {}
|
|
148
|
+
return rec;
|
|
149
|
+
}
|
|
150
|
+
async advance(agentContext, opts, instanceRecord) {
|
|
151
|
+
let inst;
|
|
152
|
+
if (instanceRecord) inst = instanceRecord;
|
|
153
|
+
else try {
|
|
154
|
+
inst = await this.instanceRepo.getById(agentContext, opts.instance_id);
|
|
155
|
+
} catch {
|
|
156
|
+
const found = await this.instanceRepo.getByInstanceId(agentContext, opts.instance_id);
|
|
157
|
+
if (!found) throw this.problem("invalid_event", "instance not found");
|
|
158
|
+
inst = found;
|
|
159
|
+
}
|
|
160
|
+
const tplRec = await this.templateRepo.findByTemplateIdAndVersion(agentContext, inst.templateId, inst.templateVersion);
|
|
161
|
+
if (!tplRec) throw this.problem("invalid_template", "template not found for instance");
|
|
162
|
+
const tpl = tplRec.template;
|
|
163
|
+
if (inst.status === "paused") throw this.problem("forbidden", "instance is paused");
|
|
164
|
+
if (inst.status === "canceled") throw this.problem("forbidden", "instance is canceled");
|
|
165
|
+
if (inst.status === "completed") throw this.problem("invalid_event", "instance already completed");
|
|
166
|
+
if (opts.idempotency_key && inst.idempotencyKeys?.includes(opts.idempotency_key)) {
|
|
167
|
+
const prior = inst.idempotency?.find?.((i) => i.key === opts.idempotency_key);
|
|
168
|
+
if (prior && prior.event !== opts.event) throw this.problem("idempotency_conflict", "same key, different event");
|
|
169
|
+
return inst;
|
|
170
|
+
}
|
|
171
|
+
const candidates = transitionsFromState(tpl, inst.state).filter((t) => t.on === opts.event);
|
|
172
|
+
if (!candidates.length) throw this.problem("invalid_event", `no transition for event ${opts.event} from ${inst.state}`);
|
|
173
|
+
const env = GuardEvaluator.envFromInstance(this.toInstanceData(inst));
|
|
174
|
+
const enabled = candidates.filter((t) => GuardEvaluator.evalGuard(t.guard, env));
|
|
175
|
+
if (!enabled.length) throw this.problem("guard_failed", "guard evaluated false");
|
|
176
|
+
const t = enabled[0];
|
|
177
|
+
const logger = this.agentConfig.logger;
|
|
178
|
+
let artifactsDelta = {};
|
|
179
|
+
let messageId;
|
|
180
|
+
if (t.action) {
|
|
181
|
+
const def = tpl.actions.find((a) => a.key === t.action);
|
|
182
|
+
if (!def) throw this.problem("invalid_template", `action not defined: ${t.action}`);
|
|
183
|
+
const handler = this.actions.get(def.typeURI);
|
|
184
|
+
if (!handler) logger.warn(`[Workflow] no handler for action type ${def.typeURI}, skipping action and advancing`, {
|
|
185
|
+
actionKey: t.action,
|
|
186
|
+
typeURI: def.typeURI,
|
|
187
|
+
instance_id: inst.instanceId
|
|
188
|
+
});
|
|
189
|
+
else {
|
|
190
|
+
const exec = async () => handler.execute({
|
|
191
|
+
agentContext,
|
|
192
|
+
template: tpl,
|
|
193
|
+
instance: this.toInstanceData(inst),
|
|
194
|
+
action: def,
|
|
195
|
+
input: opts.input,
|
|
196
|
+
idempotency_key: opts.idempotency_key
|
|
197
|
+
});
|
|
198
|
+
const timeoutMs = this.config.actionTimeoutMs ?? 15e3;
|
|
199
|
+
const race = Promise.race([exec(), new Promise((_, rej) => setTimeout(() => rej(/* @__PURE__ */ new Error("action timeout")), timeoutMs))]);
|
|
200
|
+
let result;
|
|
201
|
+
try {
|
|
202
|
+
result = await race;
|
|
203
|
+
} catch (e) {
|
|
204
|
+
throw Object.assign(new Error(e.message || "action timeout"), { code: "action_error" });
|
|
205
|
+
}
|
|
206
|
+
artifactsDelta = result?.artifacts || {};
|
|
207
|
+
if (result?.contextMerge) inst.context = result.contextMerge;
|
|
208
|
+
if (result?.messageId) messageId = result.messageId;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
const fromState = inst.state;
|
|
212
|
+
try {
|
|
213
|
+
if ((await this.instanceRepo.getById(agentContext, inst.id)).state !== fromState) throw this.problem("state_conflict", "state changed concurrently");
|
|
214
|
+
} catch (_e) {}
|
|
215
|
+
const prevState = inst.state;
|
|
216
|
+
inst.history.push({
|
|
217
|
+
ts: (/* @__PURE__ */ new Date()).toISOString(),
|
|
218
|
+
event: opts.event,
|
|
219
|
+
from: inst.state,
|
|
220
|
+
to: t.to,
|
|
221
|
+
actionKey: t.action,
|
|
222
|
+
msg_id: messageId
|
|
223
|
+
});
|
|
224
|
+
inst.state = t.to;
|
|
225
|
+
inst.section = findSectionForState(tpl, t.to);
|
|
226
|
+
inst.artifacts = {
|
|
227
|
+
...inst.artifacts,
|
|
228
|
+
...artifactsDelta
|
|
229
|
+
};
|
|
230
|
+
if (opts.idempotency_key) {
|
|
231
|
+
inst.idempotencyKeys = [...inst.idempotencyKeys || [], opts.idempotency_key];
|
|
232
|
+
inst.idempotency = [...inst.idempotency || [], {
|
|
233
|
+
key: opts.idempotency_key,
|
|
234
|
+
event: opts.event,
|
|
235
|
+
to: t.to,
|
|
236
|
+
actionKey: t.action
|
|
237
|
+
}];
|
|
238
|
+
const limit = Math.max(1, this.config.idempotencyHistoryLimit ?? 100);
|
|
239
|
+
if ((inst.idempotencyKeys?.length || 0) > limit) inst.idempotencyKeys = inst.idempotencyKeys.slice(-limit);
|
|
240
|
+
if ((inst.idempotency?.length || 0) > limit) inst.idempotency = inst.idempotency.slice(-limit);
|
|
241
|
+
}
|
|
242
|
+
let reachedFinal = false;
|
|
243
|
+
try {
|
|
244
|
+
if (tpl.states.find((s) => s.name === t.to)?.type === "final") {
|
|
245
|
+
inst.status = "completed";
|
|
246
|
+
reachedFinal = true;
|
|
247
|
+
}
|
|
248
|
+
} catch (_e) {}
|
|
249
|
+
await this.instanceRepo.update(agentContext, inst);
|
|
250
|
+
try {
|
|
251
|
+
this.eventEmitter?.emit(agentContext, {
|
|
252
|
+
type: WorkflowEventTypes.WorkflowInstanceStateChanged,
|
|
253
|
+
payload: {
|
|
254
|
+
instanceRecord: inst,
|
|
255
|
+
previousState: prevState,
|
|
256
|
+
newState: inst.state,
|
|
257
|
+
event: opts.event,
|
|
258
|
+
actionKey: t.action,
|
|
259
|
+
msgId: messageId
|
|
260
|
+
}
|
|
261
|
+
});
|
|
262
|
+
} catch (_e) {}
|
|
263
|
+
if (reachedFinal) {
|
|
264
|
+
try {
|
|
265
|
+
this.eventEmitter?.emit(agentContext, {
|
|
266
|
+
type: WorkflowEventTypes.WorkflowInstanceStatusChanged,
|
|
267
|
+
payload: {
|
|
268
|
+
instanceRecord: inst,
|
|
269
|
+
previousStatus: "active",
|
|
270
|
+
newStatus: "completed"
|
|
271
|
+
}
|
|
272
|
+
});
|
|
273
|
+
} catch (_e) {}
|
|
274
|
+
try {
|
|
275
|
+
this.eventEmitter?.emit(agentContext, {
|
|
276
|
+
type: WorkflowEventTypes.WorkflowInstanceCompleted,
|
|
277
|
+
payload: {
|
|
278
|
+
instanceRecord: inst,
|
|
279
|
+
state: inst.state,
|
|
280
|
+
section: inst.section
|
|
281
|
+
}
|
|
282
|
+
});
|
|
283
|
+
} catch (_e) {}
|
|
284
|
+
await this.sendCompleteMessage(agentContext, inst);
|
|
285
|
+
}
|
|
286
|
+
return inst;
|
|
287
|
+
}
|
|
288
|
+
async status(agentContext, opts, instanceRecord) {
|
|
289
|
+
let inst;
|
|
290
|
+
if (instanceRecord) inst = instanceRecord;
|
|
291
|
+
else try {
|
|
292
|
+
inst = await this.instanceRepo.getById(agentContext, opts.instance_id);
|
|
293
|
+
} catch {
|
|
294
|
+
const found = await this.instanceRepo.getByInstanceId(agentContext, opts.instance_id);
|
|
295
|
+
if (!found) throw this.problem("invalid_event", "instance not found");
|
|
296
|
+
inst = found;
|
|
297
|
+
}
|
|
298
|
+
const tplRec = await this.templateRepo.findByTemplateIdAndVersion(agentContext, inst.templateId, inst.templateVersion);
|
|
299
|
+
if (!tplRec) throw this.problem("invalid_template", "template not found for instance");
|
|
300
|
+
const tpl = tplRec.template;
|
|
301
|
+
const instData = this.toInstanceData(inst);
|
|
302
|
+
const env = GuardEvaluator.envFromInstance(instData);
|
|
303
|
+
const allowed = transitionsFromState(tpl, inst.state).filter((t) => GuardEvaluator.evalGuard(t.guard, env)).map((t) => t.on);
|
|
304
|
+
const includeActions = opts.include_actions ?? true;
|
|
305
|
+
const includeUi = opts.include_ui ?? true;
|
|
306
|
+
const uiEnv = {
|
|
307
|
+
context: instData.context || {},
|
|
308
|
+
participants: instData.participants || {},
|
|
309
|
+
artifacts: instData.artifacts || {},
|
|
310
|
+
viewer: opts.viewer
|
|
311
|
+
};
|
|
312
|
+
const Ui = (init_UiFilter(), __toCommonJS(UiFilter_exports));
|
|
313
|
+
let derivedProfile = opts.ui_profile;
|
|
314
|
+
try {
|
|
315
|
+
const viewerRole = opts.viewer?.role;
|
|
316
|
+
const viewerKey = opts.viewer?.participantKey;
|
|
317
|
+
const viewerDid = opts.viewer?.did;
|
|
318
|
+
const mapKeyToProfile = (k) => {
|
|
319
|
+
if (!k) return void 0;
|
|
320
|
+
const key = k.toLowerCase();
|
|
321
|
+
if (key === "sender" || key === "issuer") return "sender";
|
|
322
|
+
if (key === "receiver" || key === "holder") return "receiver";
|
|
323
|
+
};
|
|
324
|
+
if (!derivedProfile) derivedProfile = mapKeyToProfile(viewerRole) || mapKeyToProfile(viewerKey);
|
|
325
|
+
if (!derivedProfile && viewerDid && instData?.participants && typeof instData.participants === "object") for (const [pKey, pVal] of Object.entries(instData.participants)) {
|
|
326
|
+
const did = pVal?.did;
|
|
327
|
+
if (did && did === viewerDid) {
|
|
328
|
+
derivedProfile = mapKeyToProfile(pKey);
|
|
329
|
+
if (derivedProfile) break;
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
if (!derivedProfile) {
|
|
333
|
+
let myDid;
|
|
334
|
+
if (instData?.connection_id) try {
|
|
335
|
+
const conn = await agentContext.dependencyManager.resolve(DidCommConnectionService).getById(agentContext, instData.connection_id);
|
|
336
|
+
myDid = conn?.did || conn?.myDid;
|
|
337
|
+
} catch {}
|
|
338
|
+
if (myDid && instData?.participants && typeof instData.participants === "object") for (const [pKey, pVal] of Object.entries(instData.participants)) {
|
|
339
|
+
const did = pVal?.did;
|
|
340
|
+
if (did && did === myDid) {
|
|
341
|
+
derivedProfile = mapKeyToProfile(pKey);
|
|
342
|
+
if (derivedProfile) break;
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
} catch {}
|
|
347
|
+
const selected = Ui.selectUi(tpl?.display_hints, inst.state, derivedProfile);
|
|
348
|
+
const filtered = Ui.filterAndBindUi(selected, uiEnv, opts.viewer, derivedProfile);
|
|
349
|
+
const menu = includeActions ? filtered.filter((i) => i?.type === "button" || i?.type === "submit-button").map((i) => ({
|
|
350
|
+
label: i?.label,
|
|
351
|
+
event: i?.event
|
|
352
|
+
})) : [];
|
|
353
|
+
const assets = Ui.collectAssets(filtered, tpl?.display_hints?.assets);
|
|
354
|
+
const ui = includeUi ? filtered : void 0;
|
|
355
|
+
let connection;
|
|
356
|
+
if (inst.connectionId) try {
|
|
357
|
+
const conn = await agentContext.dependencyManager.resolve(DidCommConnectionService).getById(agentContext, inst.connectionId);
|
|
358
|
+
connection = {
|
|
359
|
+
id: conn?.id,
|
|
360
|
+
myDid: conn?.did || conn?.myDid,
|
|
361
|
+
theirDid: conn?.theirDid
|
|
362
|
+
};
|
|
363
|
+
} catch {}
|
|
364
|
+
return {
|
|
365
|
+
instance_id: inst.instanceId,
|
|
366
|
+
template_id: inst.templateId,
|
|
367
|
+
template_version: inst.templateVersion,
|
|
368
|
+
connection_id: inst.connectionId,
|
|
369
|
+
connection,
|
|
370
|
+
participants: inst.participants,
|
|
371
|
+
context: inst.context,
|
|
372
|
+
status: inst.status,
|
|
373
|
+
createdAt: inst.createdAt,
|
|
374
|
+
updatedAt: inst?.updatedAt,
|
|
375
|
+
state: inst.state,
|
|
376
|
+
section: inst.section,
|
|
377
|
+
allowed_events: allowed,
|
|
378
|
+
action_menu: menu,
|
|
379
|
+
artifacts: inst.artifacts,
|
|
380
|
+
...includeUi ? { ui } : {},
|
|
381
|
+
...derivedProfile ? { ui_profile: derivedProfile } : {},
|
|
382
|
+
...assets ? { assets } : {}
|
|
383
|
+
};
|
|
384
|
+
}
|
|
385
|
+
async mergeStatusResponse(agentContext, body) {
|
|
386
|
+
const instanceId = body.instance_id;
|
|
387
|
+
if (!instanceId) return;
|
|
388
|
+
const inst = await this.instanceRepo.getByInstanceId(agentContext, instanceId).catch(() => null);
|
|
389
|
+
if (!inst) return;
|
|
390
|
+
let dirty = false;
|
|
391
|
+
const prevState = inst.state;
|
|
392
|
+
const newState = body.state;
|
|
393
|
+
const tplRec = await this.templateRepo.findByTemplateIdAndVersion(agentContext, inst.templateId, inst.templateVersion).catch(() => null);
|
|
394
|
+
const isKnownState = !tplRec || tplRec.template.states.some((s) => s.name === newState);
|
|
395
|
+
if (newState && newState !== inst.state && isKnownState) {
|
|
396
|
+
inst.state = newState;
|
|
397
|
+
dirty = true;
|
|
398
|
+
}
|
|
399
|
+
const ctx = body.context;
|
|
400
|
+
if (ctx && Object.keys(ctx).length > 0) {
|
|
401
|
+
inst.context = {
|
|
402
|
+
...inst.context || {},
|
|
403
|
+
...ctx
|
|
404
|
+
};
|
|
405
|
+
dirty = true;
|
|
406
|
+
}
|
|
407
|
+
const arts = body.artifacts;
|
|
408
|
+
if (arts && Object.keys(arts).length > 0) {
|
|
409
|
+
inst.artifacts = {
|
|
410
|
+
...inst.artifacts || {},
|
|
411
|
+
...arts
|
|
412
|
+
};
|
|
413
|
+
dirty = true;
|
|
414
|
+
}
|
|
415
|
+
if (!dirty) return;
|
|
416
|
+
await this.instanceRepo.update(agentContext, inst);
|
|
417
|
+
try {
|
|
418
|
+
this.eventEmitter?.emit(agentContext, {
|
|
419
|
+
type: WorkflowEventTypes.WorkflowInstanceStateChanged,
|
|
420
|
+
payload: {
|
|
421
|
+
instanceRecord: inst,
|
|
422
|
+
previousState: prevState,
|
|
423
|
+
newState: inst.state,
|
|
424
|
+
event: "status_merge"
|
|
425
|
+
}
|
|
426
|
+
});
|
|
427
|
+
} catch {}
|
|
428
|
+
}
|
|
429
|
+
async pause(agentContext, opts, instanceRecord) {
|
|
430
|
+
const inst = instanceRecord || await this.getInstanceByIdOrTag(agentContext, opts.instance_id);
|
|
431
|
+
const prev = inst.status;
|
|
432
|
+
inst.status = "paused";
|
|
433
|
+
await this.instanceRepo.update(agentContext, inst);
|
|
434
|
+
try {
|
|
435
|
+
this.eventEmitter?.emit(agentContext, {
|
|
436
|
+
type: WorkflowEventTypes.WorkflowInstanceStatusChanged,
|
|
437
|
+
payload: {
|
|
438
|
+
instanceRecord: inst,
|
|
439
|
+
previousStatus: prev,
|
|
440
|
+
newStatus: inst.status,
|
|
441
|
+
reason: opts.reason
|
|
442
|
+
}
|
|
443
|
+
});
|
|
444
|
+
} catch (_e) {}
|
|
445
|
+
return inst;
|
|
446
|
+
}
|
|
447
|
+
async resume(agentContext, opts, instanceRecord) {
|
|
448
|
+
const inst = instanceRecord || await this.getInstanceByIdOrTag(agentContext, opts.instance_id);
|
|
449
|
+
const prev = inst.status;
|
|
450
|
+
inst.status = "active";
|
|
451
|
+
await this.instanceRepo.update(agentContext, inst);
|
|
452
|
+
try {
|
|
453
|
+
this.eventEmitter?.emit(agentContext, {
|
|
454
|
+
type: WorkflowEventTypes.WorkflowInstanceStatusChanged,
|
|
455
|
+
payload: {
|
|
456
|
+
instanceRecord: inst,
|
|
457
|
+
previousStatus: prev,
|
|
458
|
+
newStatus: inst.status,
|
|
459
|
+
reason: opts.reason
|
|
460
|
+
}
|
|
461
|
+
});
|
|
462
|
+
} catch (_e) {}
|
|
463
|
+
return inst;
|
|
464
|
+
}
|
|
465
|
+
async cancel(agentContext, opts, instanceRecord) {
|
|
466
|
+
const inst = instanceRecord || await this.getInstanceByIdOrTag(agentContext, opts.instance_id);
|
|
467
|
+
const prev = inst.status;
|
|
468
|
+
inst.status = "canceled";
|
|
469
|
+
await this.instanceRepo.update(agentContext, inst);
|
|
470
|
+
try {
|
|
471
|
+
this.eventEmitter?.emit(agentContext, {
|
|
472
|
+
type: WorkflowEventTypes.WorkflowInstanceStatusChanged,
|
|
473
|
+
payload: {
|
|
474
|
+
instanceRecord: inst,
|
|
475
|
+
previousStatus: prev,
|
|
476
|
+
newStatus: inst.status,
|
|
477
|
+
reason: opts.reason
|
|
478
|
+
}
|
|
479
|
+
});
|
|
480
|
+
} catch (_e) {}
|
|
481
|
+
return inst;
|
|
482
|
+
}
|
|
483
|
+
async complete(agentContext, opts, instanceRecord) {
|
|
484
|
+
const inst = instanceRecord || await this.getInstanceByIdOrTag(agentContext, opts.instance_id);
|
|
485
|
+
const tplRec = await this.templateRepo.findByTemplateIdAndVersion(agentContext, inst.templateId, inst.templateVersion);
|
|
486
|
+
if (!tplRec) throw this.problem("invalid_template", "template not found for instance");
|
|
487
|
+
if (tplRec.template.states.find((s) => s.name === inst.state)?.type !== "final") throw this.problem("forbidden", "cannot complete: state is not final");
|
|
488
|
+
const prev = inst.status;
|
|
489
|
+
inst.status = "completed";
|
|
490
|
+
await this.instanceRepo.update(agentContext, inst);
|
|
491
|
+
try {
|
|
492
|
+
this.eventEmitter?.emit(agentContext, {
|
|
493
|
+
type: WorkflowEventTypes.WorkflowInstanceStatusChanged,
|
|
494
|
+
payload: {
|
|
495
|
+
instanceRecord: inst,
|
|
496
|
+
previousStatus: prev,
|
|
497
|
+
newStatus: inst.status,
|
|
498
|
+
reason: opts.reason
|
|
499
|
+
}
|
|
500
|
+
});
|
|
501
|
+
this.eventEmitter?.emit(agentContext, {
|
|
502
|
+
type: WorkflowEventTypes.WorkflowInstanceCompleted,
|
|
503
|
+
payload: {
|
|
504
|
+
instanceRecord: inst,
|
|
505
|
+
state: inst.state,
|
|
506
|
+
section: inst.section
|
|
507
|
+
}
|
|
508
|
+
});
|
|
509
|
+
} catch (_e) {}
|
|
510
|
+
return inst;
|
|
511
|
+
}
|
|
512
|
+
async autoAdvanceByConnection(agentContext, connectionId, event) {
|
|
513
|
+
const inst = await this.instanceRepo.findLatestByConnection(agentContext, connectionId);
|
|
514
|
+
if (!inst) {
|
|
515
|
+
this.agentConfig.logger.warn(`[autoAdvance] No workflow instance found for connection ${connectionId}, event=${event}`);
|
|
516
|
+
return;
|
|
517
|
+
}
|
|
518
|
+
this.agentConfig.logger.info(`[autoAdvance] Advancing workflow ${inst.instanceId} with event=${event} (connection=${connectionId})`);
|
|
519
|
+
try {
|
|
520
|
+
await this.advance(agentContext, {
|
|
521
|
+
instance_id: inst.instanceId,
|
|
522
|
+
event,
|
|
523
|
+
idempotency_key: `auto:${event}:${inst.instanceId}`
|
|
524
|
+
});
|
|
525
|
+
this.agentConfig.logger.info(`[autoAdvance] Successfully advanced workflow ${inst.instanceId} with event=${event}`);
|
|
526
|
+
} catch (e) {
|
|
527
|
+
this.agentConfig.logger.warn(`[autoAdvance] Failed to advance workflow ${inst.instanceId} with event=${event}: ${e.message}`);
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
evalMultiplicity(expr, context) {
|
|
531
|
+
try {
|
|
532
|
+
const env = {
|
|
533
|
+
context,
|
|
534
|
+
participants: {},
|
|
535
|
+
artifacts: {}
|
|
536
|
+
};
|
|
537
|
+
const val = GuardEvaluator.evalValue(expr, env);
|
|
538
|
+
return val != null ? String(val) : "";
|
|
539
|
+
} catch {
|
|
540
|
+
return "";
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
toInstanceData(rec) {
|
|
544
|
+
return {
|
|
545
|
+
instance_id: rec.instanceId,
|
|
546
|
+
template_id: rec.templateId,
|
|
547
|
+
template_version: rec.templateVersion,
|
|
548
|
+
connection_id: rec.connectionId,
|
|
549
|
+
participants: rec.participants,
|
|
550
|
+
state: rec.state,
|
|
551
|
+
section: rec.section,
|
|
552
|
+
context: rec.context,
|
|
553
|
+
artifacts: rec.artifacts,
|
|
554
|
+
status: rec.status,
|
|
555
|
+
history: rec.history,
|
|
556
|
+
multiplicityKeyValue: rec.multiplicityKeyValue,
|
|
557
|
+
idempotencyKeys: rec.idempotencyKeys
|
|
558
|
+
};
|
|
559
|
+
}
|
|
560
|
+
problem(code, comment) {
|
|
561
|
+
const err = new Error(comment);
|
|
562
|
+
err.code = code;
|
|
563
|
+
return err;
|
|
564
|
+
}
|
|
565
|
+
async getInstanceByIdOrTag(agentContext, instanceId) {
|
|
566
|
+
try {
|
|
567
|
+
return await this.instanceRepo.getById(agentContext, instanceId);
|
|
568
|
+
} catch {
|
|
569
|
+
const found = await this.instanceRepo.getByInstanceId(agentContext, instanceId);
|
|
570
|
+
if (!found) throw this.problem("invalid_event", "instance not found");
|
|
571
|
+
return found;
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
uuid() {
|
|
575
|
+
return `wf_${Math.random().toString(36).slice(2)}${Date.now().toString(36)}`;
|
|
576
|
+
}
|
|
577
|
+
validateTemplate(t) {
|
|
578
|
+
const fail = (msg) => {
|
|
579
|
+
throw this.problem("invalid_template", msg);
|
|
580
|
+
};
|
|
581
|
+
if (!t.template_id || !t.version || !t.title) fail("missing required fields");
|
|
582
|
+
if (!Array.isArray(t.states) || !t.states.length) fail("states required");
|
|
583
|
+
if (!Array.isArray(t.transitions)) fail("transitions required");
|
|
584
|
+
if (!Array.isArray(t.actions)) fail("actions required");
|
|
585
|
+
const stateNames = new Set(t.states.map((s) => s.name));
|
|
586
|
+
if (![...t.states].some((s) => s.type === "start")) fail("start state required");
|
|
587
|
+
for (const s of t.states) if (s.section && !t.sections?.some((sec) => sec.name === s.section)) fail(`state.section not found: ${s.section}`);
|
|
588
|
+
for (const tr of t.transitions) {
|
|
589
|
+
if (!stateNames.has(tr.from)) fail(`transition.from unknown: ${tr.from}`);
|
|
590
|
+
if (!stateNames.has(tr.to)) fail(`transition.to unknown: ${tr.to}`);
|
|
591
|
+
if (tr.action && !t.actions.some((a) => a.key === tr.action)) fail(`transition.action unknown: ${tr.action}`);
|
|
592
|
+
}
|
|
593
|
+
for (const a of t.actions) if ("profile_ref" in a && a.profile_ref) {
|
|
594
|
+
const pr = a.profile_ref;
|
|
595
|
+
if (pr.startsWith("cp.")) {
|
|
596
|
+
const key = pr.slice(3);
|
|
597
|
+
if (!t.catalog?.credential_profiles || !t.catalog.credential_profiles[key]) fail(`catalog.cp missing: ${key}`);
|
|
598
|
+
} else if (pr.startsWith("pp.")) {
|
|
599
|
+
const key = pr.slice(3);
|
|
600
|
+
if (!t.catalog?.proof_profiles || !t.catalog.proof_profiles[key]) fail(`catalog.pp missing: ${key}`);
|
|
601
|
+
} else fail(`invalid profile_ref: ${pr}`);
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
async sendCompleteMessage(agentContext, inst) {
|
|
605
|
+
try {
|
|
606
|
+
if (!inst.connectionId) return;
|
|
607
|
+
const connectionSvc = agentContext.dependencyManager.resolve(DidCommConnectionService);
|
|
608
|
+
const messageSender = agentContext.dependencyManager.resolve(DidCommMessageSender);
|
|
609
|
+
const connection = await connectionSvc.getById(agentContext, inst.connectionId);
|
|
610
|
+
const msg = new CompleteMessage({
|
|
611
|
+
thid: inst.instanceId,
|
|
612
|
+
body: {
|
|
613
|
+
instance_id: inst.instanceId,
|
|
614
|
+
reason: "state_final"
|
|
615
|
+
}
|
|
616
|
+
});
|
|
617
|
+
try {
|
|
618
|
+
msg.setReturnRouting(ReturnRouteTypes.thread, inst.instanceId);
|
|
619
|
+
} catch (_e) {}
|
|
620
|
+
const outbound = new DidCommOutboundMessageContext(msg, {
|
|
621
|
+
agentContext,
|
|
622
|
+
connection
|
|
623
|
+
});
|
|
624
|
+
await messageSender.sendMessage(outbound);
|
|
625
|
+
} catch (e) {
|
|
626
|
+
this.agentConfig.logger.debug(`Workflow complete notify error: ${e.message}`);
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
};
|
|
630
|
+
WorkflowService = __decorate([injectable(), __decorateMetadata("design:paramtypes", [
|
|
631
|
+
WorkflowTemplateRepository,
|
|
632
|
+
WorkflowInstanceRepository,
|
|
633
|
+
WorkflowModuleConfig,
|
|
634
|
+
AgentConfig,
|
|
635
|
+
EventEmitter
|
|
636
|
+
])], WorkflowService);
|
|
637
|
+
|
|
638
|
+
//#endregion
|
|
639
|
+
export { WorkflowService };
|
|
640
|
+
//# sourceMappingURL=WorkflowService.mjs.map
|