@dnotifier-realtime/dnotifier 1.1.17 → 1.1.20
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/dist/cjs/dnotifier.js +1 -1
- package/dist/cjs/providers/openai.js +1 -1
- package/dist/cjs/transport/httpTransport.js +1 -1
- package/dist/cjs/transport/websocketTransport.js +1 -1
- package/dist/cjs/utils/buffer.js +1 -1
- package/dist/cjs/utils/frame.js +1 -1
- package/dist/cjs/utils/id.js +1 -1
- package/dist/cjs/utils/payload.js +1 -1
- package/dist/config/endpoints.d.ts +2 -2
- package/dist/config/endpoints.d.ts.map +1 -1
- package/dist/config/endpoints.js +1 -1
- package/dist/dnotifier.cjs +496 -76
- package/dist/dnotifier.d.ts +28 -0
- package/dist/dnotifier.d.ts.map +1 -1
- package/dist/dnotifier.js +1 -1
- package/dist/providers/openai.js +1 -1
- package/dist/transport/httpTransport.js +1 -1
- package/dist/transport/websocketTransport.js +1 -1
- package/dist/utils/buffer.js +1 -1
- package/dist/utils/frame.js +1 -1
- package/dist/utils/id.js +1 -1
- package/dist/utils/logs.js +1 -1
- package/dist/utils/payload.js +1 -1
- package/dist/workflow/agent.js +1 -1
- package/dist/workflow/context.d.ts +8 -0
- package/dist/workflow/context.d.ts.map +1 -1
- package/dist/workflow/context.js +1 -1
- package/dist/workflow/errors.js +1 -1
- package/dist/workflow/graphParents.js +1 -1
- package/dist/workflow/ids.js +1 -1
- package/dist/workflow/index.d.ts +1 -0
- package/dist/workflow/index.js +1 -1
- package/dist/workflow/jsonPayload.js +1 -1
- package/dist/workflow/mockObservabilityClient.js +1 -1
- package/dist/workflow/observability.js +1 -1
- package/dist/workflow/remoteAgentClient.d.ts +30 -0
- package/dist/workflow/remoteAgentClient.d.ts.map +1 -0
- package/dist/workflow/remoteAgentClient.js +1 -0
- package/dist/workflow/remoteAgentProtocol.d.ts +50 -0
- package/dist/workflow/remoteAgentProtocol.d.ts.map +1 -0
- package/dist/workflow/remoteAgentProtocol.js +1 -0
- package/dist/workflow/runner.js +1 -1
- package/dist/workflow/stepTypes.js +1 -1
- package/dist/workflow/workflow.d.ts +15 -3
- package/dist/workflow/workflow.d.ts.map +1 -1
- package/dist/workflow/workflow.js +1 -1
- package/dist/workflow/workflowObservabilityClient.js +1 -1
- package/package.json +2 -1
package/dist/dnotifier.cjs
CHANGED
|
@@ -239,7 +239,7 @@ var WebsocketTransport = class extends import_events.default {
|
|
|
239
239
|
};
|
|
240
240
|
|
|
241
241
|
// src/config/endpoints.js
|
|
242
|
-
var baseHost = "https://
|
|
242
|
+
var baseHost = "https://api.dnotifier.com";
|
|
243
243
|
var wsHost = baseHost.replace(/^http/, "ws");
|
|
244
244
|
var logHost = `${baseHost}/ecs`;
|
|
245
245
|
var workflowSdkBase = `${logHost}/api/v1/log/sdk/workflow`;
|
|
@@ -457,74 +457,138 @@ function defineAgent(config) {
|
|
|
457
457
|
return new Agent(config);
|
|
458
458
|
}
|
|
459
459
|
|
|
460
|
-
// src/workflow/
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
if (trimmed.length > 100) {
|
|
470
|
-
throw new WorkflowError("Workflow description must be at most 100 characters");
|
|
471
|
-
}
|
|
472
|
-
return trimmed.length > 0 ? trimmed : void 0;
|
|
460
|
+
// src/workflow/remoteAgentProtocol.js
|
|
461
|
+
var REMOTE_AGENT_KIND = "dnotifier.remoteAgent";
|
|
462
|
+
var REMOTE_AGENT_OP = {
|
|
463
|
+
INVOKE: "invoke",
|
|
464
|
+
RESULT: "result",
|
|
465
|
+
ERROR: "error"
|
|
466
|
+
};
|
|
467
|
+
function isRemoteAgentEnvelope(data) {
|
|
468
|
+
return data != null && typeof data === "object" && data.__dnotifier === REMOTE_AGENT_KIND && typeof data.op === "string";
|
|
473
469
|
}
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
}
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
return
|
|
519
|
-
}
|
|
520
|
-
/**
|
|
521
|
-
* @param {string} name
|
|
522
|
-
* @returns {import("./agent.js").Agent | undefined}
|
|
523
|
-
*/
|
|
524
|
-
getAgent(name) {
|
|
525
|
-
return this._agents.get(name);
|
|
470
|
+
function isRemoteAgentRef(value) {
|
|
471
|
+
return value != null && typeof value === "object" && typeof value.receiverId === "string" && value.receiverId.trim() !== "" && typeof value.run !== "function";
|
|
472
|
+
}
|
|
473
|
+
function createInvokeEnvelope(params) {
|
|
474
|
+
return {
|
|
475
|
+
__dnotifier: REMOTE_AGENT_KIND,
|
|
476
|
+
op: REMOTE_AGENT_OP.INVOKE,
|
|
477
|
+
correlationId: params.correlationId,
|
|
478
|
+
agentName: params.agentName,
|
|
479
|
+
input: params.input ?? null,
|
|
480
|
+
state: params.state ?? null,
|
|
481
|
+
trace: params.trace ?? null,
|
|
482
|
+
replyTo: params.replyTo
|
|
483
|
+
};
|
|
484
|
+
}
|
|
485
|
+
function createResultEnvelope(params) {
|
|
486
|
+
return {
|
|
487
|
+
__dnotifier: REMOTE_AGENT_KIND,
|
|
488
|
+
op: REMOTE_AGENT_OP.RESULT,
|
|
489
|
+
correlationId: params.correlationId,
|
|
490
|
+
output: params.output ?? null,
|
|
491
|
+
state: params.state ?? null
|
|
492
|
+
};
|
|
493
|
+
}
|
|
494
|
+
function createErrorEnvelope(params) {
|
|
495
|
+
return {
|
|
496
|
+
__dnotifier: REMOTE_AGENT_KIND,
|
|
497
|
+
op: REMOTE_AGENT_OP.ERROR,
|
|
498
|
+
correlationId: params.correlationId,
|
|
499
|
+
error: { message: params.message }
|
|
500
|
+
};
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
// src/workflow/remoteAgentClient.js
|
|
504
|
+
var DEFAULT_TIMEOUT_MS = 12e4;
|
|
505
|
+
function correlationId() {
|
|
506
|
+
const c = typeof globalThis !== "undefined" ? globalThis.crypto : void 0;
|
|
507
|
+
if (c && typeof c.randomUUID === "function") return c.randomUUID();
|
|
508
|
+
if (c && c.getRandomValues) {
|
|
509
|
+
const bytes = new Uint8Array(16);
|
|
510
|
+
c.getRandomValues(bytes);
|
|
511
|
+
bytes[6] = bytes[6] & 15 | 64;
|
|
512
|
+
bytes[8] = bytes[8] & 63 | 128;
|
|
513
|
+
const hex = [...bytes].map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
514
|
+
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
|
|
526
515
|
}
|
|
527
|
-
}
|
|
516
|
+
return `corr_${Date.now()}_${Math.random().toString(36).slice(2, 10)}`;
|
|
517
|
+
}
|
|
518
|
+
function bareUserId(appId, prefixedSender) {
|
|
519
|
+
if (!prefixedSender || typeof prefixedSender !== "string") return "";
|
|
520
|
+
const prefix = `${appId}:`;
|
|
521
|
+
if (prefixedSender.startsWith(prefix)) {
|
|
522
|
+
return prefixedSender.slice(prefix.length);
|
|
523
|
+
}
|
|
524
|
+
const idx = prefixedSender.indexOf(":");
|
|
525
|
+
if (idx >= 0) return prefixedSender.slice(idx + 1);
|
|
526
|
+
return prefixedSender;
|
|
527
|
+
}
|
|
528
|
+
async function invokeRemoteAgent(params) {
|
|
529
|
+
const {
|
|
530
|
+
notifier,
|
|
531
|
+
senderId,
|
|
532
|
+
receiverId: receiverId2,
|
|
533
|
+
agentName,
|
|
534
|
+
input,
|
|
535
|
+
state,
|
|
536
|
+
trace = null,
|
|
537
|
+
timeoutMs = DEFAULT_TIMEOUT_MS
|
|
538
|
+
} = params;
|
|
539
|
+
if (!notifier?.isConnected) {
|
|
540
|
+
throw new WorkflowError("Remote agent invoke requires a connected notifier");
|
|
541
|
+
}
|
|
542
|
+
if (!receiverId2) {
|
|
543
|
+
throw new WorkflowError(`Remote agent "${agentName}" is missing receiverId`);
|
|
544
|
+
}
|
|
545
|
+
const id = correlationId();
|
|
546
|
+
const envelope = createInvokeEnvelope({
|
|
547
|
+
correlationId: id,
|
|
548
|
+
agentName,
|
|
549
|
+
input,
|
|
550
|
+
state: state ?? null,
|
|
551
|
+
trace,
|
|
552
|
+
replyTo: senderId
|
|
553
|
+
});
|
|
554
|
+
const resultPromise = notifier._awaitRemoteAgentReply(id, {
|
|
555
|
+
timeoutMs,
|
|
556
|
+
agentName
|
|
557
|
+
});
|
|
558
|
+
await notifier.send({
|
|
559
|
+
senderId,
|
|
560
|
+
receiverId: receiverId2,
|
|
561
|
+
data: envelope,
|
|
562
|
+
saveHistory: false
|
|
563
|
+
});
|
|
564
|
+
return resultPromise;
|
|
565
|
+
}
|
|
566
|
+
async function sendRemoteAgentResult(notifier, params) {
|
|
567
|
+
await notifier.send({
|
|
568
|
+
senderId: params.senderId,
|
|
569
|
+
receiverId: params.receiverId,
|
|
570
|
+
data: createResultEnvelope({
|
|
571
|
+
correlationId: params.correlationId,
|
|
572
|
+
output: params.output,
|
|
573
|
+
state: params.state ?? null
|
|
574
|
+
}),
|
|
575
|
+
saveHistory: false
|
|
576
|
+
});
|
|
577
|
+
}
|
|
578
|
+
async function sendRemoteAgentError(notifier, params) {
|
|
579
|
+
await notifier.send({
|
|
580
|
+
senderId: params.senderId,
|
|
581
|
+
receiverId: params.receiverId,
|
|
582
|
+
data: createErrorEnvelope({
|
|
583
|
+
correlationId: params.correlationId,
|
|
584
|
+
message: params.message
|
|
585
|
+
}),
|
|
586
|
+
saveHistory: false
|
|
587
|
+
});
|
|
588
|
+
}
|
|
589
|
+
function isRemoteAgentReply(data) {
|
|
590
|
+
return data != null && typeof data === "object" && (data.op === REMOTE_AGENT_OP.RESULT || data.op === REMOTE_AGENT_OP.ERROR);
|
|
591
|
+
}
|
|
528
592
|
|
|
529
593
|
// src/workflow/graphParents.js
|
|
530
594
|
function formatParentStepId(value) {
|
|
@@ -813,12 +877,13 @@ var WorkflowContext = class _WorkflowContext {
|
|
|
813
877
|
* branchKey?: string,
|
|
814
878
|
* siblingIndex?: number,
|
|
815
879
|
* parentStepId?: string | null,
|
|
880
|
+
* timeoutMs?: number,
|
|
816
881
|
* }} [options]
|
|
817
882
|
* @returns {Promise<{ output: *, stepId: string | null }>}
|
|
818
883
|
*/
|
|
819
884
|
async _runAgentObserved(name, options = {}) {
|
|
820
|
-
const
|
|
821
|
-
if (!
|
|
885
|
+
const registration = this.workflow.getAgentRegistration(name);
|
|
886
|
+
if (!registration) {
|
|
822
887
|
throw new WorkflowError(`Agent not found: ${name}`);
|
|
823
888
|
}
|
|
824
889
|
const childInput = options.input !== void 0 ? options.input : this.input;
|
|
@@ -834,6 +899,29 @@ var WorkflowContext = class _WorkflowContext {
|
|
|
834
899
|
let output = null;
|
|
835
900
|
let error = null;
|
|
836
901
|
try {
|
|
902
|
+
if (isRemoteAgentRef(registration)) {
|
|
903
|
+
const trace = this._executionId ? {
|
|
904
|
+
executionId: this._executionId,
|
|
905
|
+
workflowId: this.workflow.name,
|
|
906
|
+
parentStepId: agentStepId ?? this._currentStepId ?? this._chainHeadStepId ?? this._traceParentStepId ?? this._entryStepId ?? null
|
|
907
|
+
} : null;
|
|
908
|
+
const remote = await invokeRemoteAgent({
|
|
909
|
+
notifier: this.notifier,
|
|
910
|
+
senderId: this.senderId,
|
|
911
|
+
receiverId: registration.receiverId,
|
|
912
|
+
agentName: name,
|
|
913
|
+
input: childInput,
|
|
914
|
+
state: this.state,
|
|
915
|
+
trace,
|
|
916
|
+
timeoutMs: options.timeoutMs
|
|
917
|
+
});
|
|
918
|
+
if (remote.state && typeof remote.state === "object") {
|
|
919
|
+
Object.assign(this.state, remote.state);
|
|
920
|
+
}
|
|
921
|
+
output = remote.output;
|
|
922
|
+
return { output, stepId: agentStepId };
|
|
923
|
+
}
|
|
924
|
+
const agent = registration;
|
|
837
925
|
const childCtx = new _WorkflowContext({
|
|
838
926
|
notifier: this.notifier,
|
|
839
927
|
senderId: this.senderId,
|
|
@@ -890,6 +978,7 @@ var WorkflowContext = class _WorkflowContext {
|
|
|
890
978
|
* branchKey?: string,
|
|
891
979
|
* siblingIndex?: number,
|
|
892
980
|
* parentStepId?: string | null,
|
|
981
|
+
* timeoutMs?: number,
|
|
893
982
|
* }} [options]
|
|
894
983
|
*/
|
|
895
984
|
async runAgent(name, options = {}) {
|
|
@@ -899,6 +988,20 @@ var WorkflowContext = class _WorkflowContext {
|
|
|
899
988
|
}
|
|
900
989
|
return output;
|
|
901
990
|
}
|
|
991
|
+
/**
|
|
992
|
+
* Wait for the next inbound realtime message (non-protocol).
|
|
993
|
+
* Use WebSocket transport. Useful mid-agent for human/service follow-ups.
|
|
994
|
+
* @param {{ timeoutMs?: number }} [options]
|
|
995
|
+
* @returns {Promise<{ metadata: object, payload: * }>}
|
|
996
|
+
*/
|
|
997
|
+
async waitForMessage(options = {}) {
|
|
998
|
+
if (!this.notifier || typeof this.notifier._waitForInboundMessage !== "function") {
|
|
999
|
+
throw new WorkflowError("waitForMessage requires a DNotifier instance");
|
|
1000
|
+
}
|
|
1001
|
+
return this.notifier._waitForInboundMessage({
|
|
1002
|
+
timeoutMs: options.timeoutMs
|
|
1003
|
+
});
|
|
1004
|
+
}
|
|
902
1005
|
/**
|
|
903
1006
|
* @param {Array<string | { name: string, input?: * }> | Record<string, *>} spec
|
|
904
1007
|
* @param {{ label?: string }} [options]
|
|
@@ -1384,6 +1487,179 @@ function createWorkflowObservability(params) {
|
|
|
1384
1487
|
return new WorkflowObservability(params);
|
|
1385
1488
|
}
|
|
1386
1489
|
|
|
1490
|
+
// src/workflow/workflow.js
|
|
1491
|
+
function normalizeDescription(description) {
|
|
1492
|
+
if (description === void 0 || description === null || description === "") {
|
|
1493
|
+
return void 0;
|
|
1494
|
+
}
|
|
1495
|
+
if (typeof description !== "string") {
|
|
1496
|
+
throw new WorkflowError("Workflow description must be a string");
|
|
1497
|
+
}
|
|
1498
|
+
const trimmed = description.trim();
|
|
1499
|
+
if (trimmed.length > 100) {
|
|
1500
|
+
throw new WorkflowError("Workflow description must be at most 100 characters");
|
|
1501
|
+
}
|
|
1502
|
+
return trimmed.length > 0 ? trimmed : void 0;
|
|
1503
|
+
}
|
|
1504
|
+
var Workflow = class {
|
|
1505
|
+
/**
|
|
1506
|
+
* @param {{
|
|
1507
|
+
* name: string,
|
|
1508
|
+
* description?: string,
|
|
1509
|
+
* observability?: boolean,
|
|
1510
|
+
* entry: (ctx: import("./context.js").WorkflowContext) => Promise<*>,
|
|
1511
|
+
* }} options
|
|
1512
|
+
*/
|
|
1513
|
+
constructor({ name, description, observability = false, entry }) {
|
|
1514
|
+
if (!name || typeof name !== "string") {
|
|
1515
|
+
throw new WorkflowError("Workflow name is required");
|
|
1516
|
+
}
|
|
1517
|
+
if (typeof entry !== "function") {
|
|
1518
|
+
throw new WorkflowError("Workflow entry must be a function");
|
|
1519
|
+
}
|
|
1520
|
+
this.name = name.trim();
|
|
1521
|
+
this.description = normalizeDescription(description);
|
|
1522
|
+
this.observability = Boolean(observability);
|
|
1523
|
+
this.entry = entry;
|
|
1524
|
+
this._agents = /* @__PURE__ */ new Map();
|
|
1525
|
+
this._registeredWithObservability = false;
|
|
1526
|
+
this._listenNotifier = null;
|
|
1527
|
+
}
|
|
1528
|
+
/**
|
|
1529
|
+
* @param {string} name
|
|
1530
|
+
* @param {AgentRegistration} agentOrRemote
|
|
1531
|
+
*/
|
|
1532
|
+
registerAgent(name, agentOrRemote) {
|
|
1533
|
+
if (!name || typeof name !== "string") {
|
|
1534
|
+
throw new WorkflowError("Agent registration name is required");
|
|
1535
|
+
}
|
|
1536
|
+
if (isRemoteAgentRef(agentOrRemote)) {
|
|
1537
|
+
this._agents.set(name, { receiverId: agentOrRemote.receiverId.trim() });
|
|
1538
|
+
return this;
|
|
1539
|
+
}
|
|
1540
|
+
if (!agentOrRemote || typeof agentOrRemote.run !== "function") {
|
|
1541
|
+
throw new WorkflowError(
|
|
1542
|
+
`Invalid agent for "${name}" (expected Agent or { receiverId })`
|
|
1543
|
+
);
|
|
1544
|
+
}
|
|
1545
|
+
this._agents.set(name, agentOrRemote);
|
|
1546
|
+
return this;
|
|
1547
|
+
}
|
|
1548
|
+
/**
|
|
1549
|
+
* @param {Record<string, AgentRegistration>} agents
|
|
1550
|
+
*/
|
|
1551
|
+
registerAgents(agents) {
|
|
1552
|
+
for (const [name, agent] of Object.entries(agents)) {
|
|
1553
|
+
this.registerAgent(name, agent);
|
|
1554
|
+
}
|
|
1555
|
+
return this;
|
|
1556
|
+
}
|
|
1557
|
+
/**
|
|
1558
|
+
* Local Agent only (undefined if missing or remote).
|
|
1559
|
+
* @param {string} name
|
|
1560
|
+
* @returns {import("./agent.js").Agent | undefined}
|
|
1561
|
+
*/
|
|
1562
|
+
getAgent(name) {
|
|
1563
|
+
const reg = this._agents.get(name);
|
|
1564
|
+
if (!reg || isRemoteAgentRef(reg)) return void 0;
|
|
1565
|
+
return reg;
|
|
1566
|
+
}
|
|
1567
|
+
/**
|
|
1568
|
+
* @param {string} name
|
|
1569
|
+
* @returns {AgentRegistration | undefined}
|
|
1570
|
+
*/
|
|
1571
|
+
getAgentRegistration(name) {
|
|
1572
|
+
return this._agents.get(name);
|
|
1573
|
+
}
|
|
1574
|
+
/**
|
|
1575
|
+
* @param {string} name
|
|
1576
|
+
* @returns {boolean}
|
|
1577
|
+
*/
|
|
1578
|
+
hasLocalAgent(name) {
|
|
1579
|
+
return Boolean(this.getAgent(name));
|
|
1580
|
+
}
|
|
1581
|
+
/**
|
|
1582
|
+
* Answer remote `runAgent` invokes for local agents on this workflow.
|
|
1583
|
+
* Uses WorkflowContext → ctx.runAgent (agents are never called directly).
|
|
1584
|
+
* Requires WebSocket transport so replies can be sent/received.
|
|
1585
|
+
* @param {import("../dnotifier.js").DNotifier} notifier
|
|
1586
|
+
*/
|
|
1587
|
+
listen(notifier) {
|
|
1588
|
+
if (!notifier) {
|
|
1589
|
+
throw new WorkflowError("workflow.listen requires a DNotifier instance");
|
|
1590
|
+
}
|
|
1591
|
+
if (this._listenNotifier && this._listenNotifier !== notifier) {
|
|
1592
|
+
this._listenNotifier._unregisterWorkflowListener(this);
|
|
1593
|
+
}
|
|
1594
|
+
this._listenNotifier = notifier;
|
|
1595
|
+
notifier._registerWorkflowListener(this);
|
|
1596
|
+
return this;
|
|
1597
|
+
}
|
|
1598
|
+
/**
|
|
1599
|
+
* @internal
|
|
1600
|
+
* @param {import("../dnotifier.js").DNotifier} notifier
|
|
1601
|
+
* @param {{ metadata: object, payload: * }} msg
|
|
1602
|
+
* @param {object} data
|
|
1603
|
+
*/
|
|
1604
|
+
async _handleRemoteInvoke(notifier, msg, data) {
|
|
1605
|
+
const agentName = data.agentName;
|
|
1606
|
+
const correlationId2 = data.correlationId;
|
|
1607
|
+
const replyTo = typeof data.replyTo === "string" && data.replyTo.trim() || bareUserId(notifier.appId, msg?.metadata?.sender);
|
|
1608
|
+
if (!correlationId2 || !replyTo) {
|
|
1609
|
+
return;
|
|
1610
|
+
}
|
|
1611
|
+
if (!this.hasLocalAgent(agentName)) {
|
|
1612
|
+
await sendRemoteAgentError(notifier, {
|
|
1613
|
+
senderId: notifier.userId,
|
|
1614
|
+
receiverId: replyTo,
|
|
1615
|
+
correlationId: correlationId2,
|
|
1616
|
+
message: `Agent not hosted: ${agentName}`
|
|
1617
|
+
});
|
|
1618
|
+
return;
|
|
1619
|
+
}
|
|
1620
|
+
const inboundState = data.state && typeof data.state === "object" && !Array.isArray(data.state) ? { ...data.state } : {};
|
|
1621
|
+
let observability = null;
|
|
1622
|
+
const executionId = data.trace?.executionId ?? null;
|
|
1623
|
+
if (executionId && this.observability) {
|
|
1624
|
+
observability = createWorkflowObservability({
|
|
1625
|
+
appId: notifier.appId,
|
|
1626
|
+
token: notifier.authToken ?? null,
|
|
1627
|
+
appendOnly: true
|
|
1628
|
+
});
|
|
1629
|
+
await observability.registerWorkflow(this);
|
|
1630
|
+
}
|
|
1631
|
+
const ctx = new WorkflowContext({
|
|
1632
|
+
notifier,
|
|
1633
|
+
senderId: notifier.userId,
|
|
1634
|
+
workflow: this,
|
|
1635
|
+
input: data.input ?? null,
|
|
1636
|
+
state: inboundState,
|
|
1637
|
+
observability,
|
|
1638
|
+
executionId,
|
|
1639
|
+
traceParentStepId: data.trace?.parentStepId ?? null
|
|
1640
|
+
});
|
|
1641
|
+
try {
|
|
1642
|
+
const output = await ctx.runAgent(agentName, {
|
|
1643
|
+
input: data.input !== void 0 ? data.input : ctx.input
|
|
1644
|
+
});
|
|
1645
|
+
await sendRemoteAgentResult(notifier, {
|
|
1646
|
+
senderId: notifier.userId,
|
|
1647
|
+
receiverId: replyTo,
|
|
1648
|
+
correlationId: correlationId2,
|
|
1649
|
+
output,
|
|
1650
|
+
state: ctx.state
|
|
1651
|
+
});
|
|
1652
|
+
} catch (err) {
|
|
1653
|
+
await sendRemoteAgentError(notifier, {
|
|
1654
|
+
senderId: notifier.userId,
|
|
1655
|
+
receiverId: replyTo,
|
|
1656
|
+
correlationId: correlationId2,
|
|
1657
|
+
message: err?.message ?? String(err)
|
|
1658
|
+
});
|
|
1659
|
+
}
|
|
1660
|
+
}
|
|
1661
|
+
};
|
|
1662
|
+
|
|
1387
1663
|
// src/workflow/runner.js
|
|
1388
1664
|
function snapshot2(value) {
|
|
1389
1665
|
try {
|
|
@@ -1558,6 +1834,9 @@ var DNotifier = class {
|
|
|
1558
1834
|
});
|
|
1559
1835
|
this.onDisconnectedCallback = onDisconnected || (() => {
|
|
1560
1836
|
});
|
|
1837
|
+
this._remoteAgentPending = /* @__PURE__ */ new Map();
|
|
1838
|
+
this._waitForMessageWaiters = [];
|
|
1839
|
+
this._workflowListeners = [];
|
|
1561
1840
|
this.isConnected = false;
|
|
1562
1841
|
this.aiEnabled = false;
|
|
1563
1842
|
this.messageSizeLimit = 32 * 1024;
|
|
@@ -1669,7 +1948,7 @@ var DNotifier = class {
|
|
|
1669
1948
|
console.error("Session step completion failed:", err);
|
|
1670
1949
|
}
|
|
1671
1950
|
}
|
|
1672
|
-
this.
|
|
1951
|
+
this._dispatchInboundMessage({
|
|
1673
1952
|
metadata: {
|
|
1674
1953
|
id: packet.id,
|
|
1675
1954
|
type: packet.type ?? packet.sender,
|
|
@@ -1699,7 +1978,7 @@ var DNotifier = class {
|
|
|
1699
1978
|
console.error("Session step completion failed:", err);
|
|
1700
1979
|
});
|
|
1701
1980
|
}
|
|
1702
|
-
this.
|
|
1981
|
+
this._dispatchInboundMessage({
|
|
1703
1982
|
metadata: {
|
|
1704
1983
|
id: packet.id,
|
|
1705
1984
|
type: packet.type ?? packet.sender,
|
|
@@ -1731,6 +2010,137 @@ var DNotifier = class {
|
|
|
1731
2010
|
});
|
|
1732
2011
|
}
|
|
1733
2012
|
}
|
|
2013
|
+
/**
|
|
2014
|
+
* @internal Route inbound WS messages: remote-agent protocol, waitForMessage, then user onMessage.
|
|
2015
|
+
* @param {{ metadata: object, payload: * }} msg
|
|
2016
|
+
*/
|
|
2017
|
+
_dispatchInboundMessage(msg) {
|
|
2018
|
+
let data = null;
|
|
2019
|
+
try {
|
|
2020
|
+
data = msg?.payload?.toJSON?.() ?? null;
|
|
2021
|
+
} catch {
|
|
2022
|
+
data = null;
|
|
2023
|
+
}
|
|
2024
|
+
if (isRemoteAgentEnvelope(data) && isRemoteAgentReply(data)) {
|
|
2025
|
+
const pending = this._remoteAgentPending.get(data.correlationId);
|
|
2026
|
+
if (pending) {
|
|
2027
|
+
clearTimeout(pending.timer);
|
|
2028
|
+
this._remoteAgentPending.delete(data.correlationId);
|
|
2029
|
+
if (data.op === REMOTE_AGENT_OP.ERROR) {
|
|
2030
|
+
pending.reject(
|
|
2031
|
+
new WorkflowError(
|
|
2032
|
+
data.error?.message ?? "Remote agent failed",
|
|
2033
|
+
{ agentName: pending.agentName }
|
|
2034
|
+
)
|
|
2035
|
+
);
|
|
2036
|
+
} else {
|
|
2037
|
+
pending.resolve({
|
|
2038
|
+
output: data.output,
|
|
2039
|
+
state: data.state && typeof data.state === "object" ? data.state : null
|
|
2040
|
+
});
|
|
2041
|
+
}
|
|
2042
|
+
return;
|
|
2043
|
+
}
|
|
2044
|
+
}
|
|
2045
|
+
if (isRemoteAgentEnvelope(data) && data.op === REMOTE_AGENT_OP.INVOKE) {
|
|
2046
|
+
const workflow = this._workflowListeners.find(
|
|
2047
|
+
(wf) => wf.hasLocalAgent(data.agentName)
|
|
2048
|
+
);
|
|
2049
|
+
if (workflow) {
|
|
2050
|
+
Promise.resolve().then(() => workflow._handleRemoteInvoke(this, msg, data)).catch((err) => {
|
|
2051
|
+
console.error("Remote agent invoke failed:", err);
|
|
2052
|
+
});
|
|
2053
|
+
return;
|
|
2054
|
+
}
|
|
2055
|
+
const replyTo = typeof data.replyTo === "string" && data.replyTo.trim() || bareUserId(this.appId, msg?.metadata?.sender);
|
|
2056
|
+
if (data.correlationId && replyTo) {
|
|
2057
|
+
sendRemoteAgentError(this, {
|
|
2058
|
+
senderId: this.userId,
|
|
2059
|
+
receiverId: replyTo,
|
|
2060
|
+
correlationId: data.correlationId,
|
|
2061
|
+
message: `Agent not hosted: ${data.agentName}`
|
|
2062
|
+
}).catch((err) => {
|
|
2063
|
+
console.error("Remote agent error reply failed:", err);
|
|
2064
|
+
});
|
|
2065
|
+
}
|
|
2066
|
+
return;
|
|
2067
|
+
}
|
|
2068
|
+
if (isRemoteAgentEnvelope(data)) {
|
|
2069
|
+
return;
|
|
2070
|
+
}
|
|
2071
|
+
if (this._waitForMessageWaiters.length > 0) {
|
|
2072
|
+
const waiter = this._waitForMessageWaiters.shift();
|
|
2073
|
+
clearTimeout(waiter.timer);
|
|
2074
|
+
waiter.resolve(msg);
|
|
2075
|
+
return;
|
|
2076
|
+
}
|
|
2077
|
+
this.onMessageCallback(msg);
|
|
2078
|
+
}
|
|
2079
|
+
/**
|
|
2080
|
+
* @internal
|
|
2081
|
+
* @param {string} correlationId
|
|
2082
|
+
* @param {{ timeoutMs?: number, agentName?: string }} [options]
|
|
2083
|
+
*/
|
|
2084
|
+
_awaitRemoteAgentReply(correlationId2, options = {}) {
|
|
2085
|
+
const timeoutMs = options.timeoutMs != null ? options.timeoutMs : DEFAULT_TIMEOUT_MS;
|
|
2086
|
+
return new Promise((resolve, reject) => {
|
|
2087
|
+
const timer = setTimeout(() => {
|
|
2088
|
+
this._remoteAgentPending.delete(correlationId2);
|
|
2089
|
+
reject(
|
|
2090
|
+
new WorkflowError(
|
|
2091
|
+
`Remote agent timed out after ${timeoutMs}ms${options.agentName ? `: ${options.agentName}` : ""}`,
|
|
2092
|
+
{ agentName: options.agentName }
|
|
2093
|
+
)
|
|
2094
|
+
);
|
|
2095
|
+
}, timeoutMs);
|
|
2096
|
+
this._remoteAgentPending.set(correlationId2, {
|
|
2097
|
+
resolve,
|
|
2098
|
+
reject,
|
|
2099
|
+
timer,
|
|
2100
|
+
agentName: options.agentName
|
|
2101
|
+
});
|
|
2102
|
+
});
|
|
2103
|
+
}
|
|
2104
|
+
/**
|
|
2105
|
+
* @internal
|
|
2106
|
+
* @param {{ timeoutMs?: number }} [options]
|
|
2107
|
+
*/
|
|
2108
|
+
_waitForInboundMessage(options = {}) {
|
|
2109
|
+
const timeoutMs = options.timeoutMs != null ? options.timeoutMs : DEFAULT_TIMEOUT_MS;
|
|
2110
|
+
const entry = { resolve: () => {
|
|
2111
|
+
}, reject: () => {
|
|
2112
|
+
}, timer: null };
|
|
2113
|
+
entry.timer = setTimeout(() => {
|
|
2114
|
+
const idx = this._waitForMessageWaiters.indexOf(entry);
|
|
2115
|
+
if (idx >= 0) this._waitForMessageWaiters.splice(idx, 1);
|
|
2116
|
+
entry.reject(
|
|
2117
|
+
new WorkflowError(`waitForMessage timed out after ${timeoutMs}ms`)
|
|
2118
|
+
);
|
|
2119
|
+
}, timeoutMs);
|
|
2120
|
+
return new Promise((resolve, reject) => {
|
|
2121
|
+
entry.resolve = resolve;
|
|
2122
|
+
entry.reject = reject;
|
|
2123
|
+
this._waitForMessageWaiters.push(entry);
|
|
2124
|
+
});
|
|
2125
|
+
}
|
|
2126
|
+
/**
|
|
2127
|
+
* @internal
|
|
2128
|
+
* @param {import("./workflow/workflow.js").Workflow} workflow
|
|
2129
|
+
*/
|
|
2130
|
+
_registerWorkflowListener(workflow) {
|
|
2131
|
+
if (!this._workflowListeners.includes(workflow)) {
|
|
2132
|
+
this._workflowListeners.push(workflow);
|
|
2133
|
+
}
|
|
2134
|
+
}
|
|
2135
|
+
/**
|
|
2136
|
+
* @internal
|
|
2137
|
+
* @param {import("./workflow/workflow.js").Workflow} workflow
|
|
2138
|
+
*/
|
|
2139
|
+
_unregisterWorkflowListener(workflow) {
|
|
2140
|
+
this._workflowListeners = this._workflowListeners.filter(
|
|
2141
|
+
(wf) => wf !== workflow
|
|
2142
|
+
);
|
|
2143
|
+
}
|
|
1734
2144
|
/**
|
|
1735
2145
|
* Returns the plan limits from the last successful authentication.
|
|
1736
2146
|
* @returns {{ messagesHardLimit: number, maxRequestsPerMonth: number, maxAIWordsPerMonth: number, knowledgeBaseMaxWords: number, aiEnabled: boolean, maxUsers: number, maxRowsPerUser: number } | null}
|
|
@@ -2211,13 +2621,20 @@ var DNotifier = class {
|
|
|
2211
2621
|
}
|
|
2212
2622
|
/**
|
|
2213
2623
|
* Resume a workflow context on a remote server (append-only observability).
|
|
2624
|
+
* Prefer `workflow.listen(notifier)` for hosting agents — that path uses
|
|
2625
|
+
* the host Workflow's registrations and `ctx.runAgent`.
|
|
2214
2626
|
* @param {{
|
|
2215
2627
|
* executionId: string,
|
|
2216
2628
|
* workflowId: string,
|
|
2217
2629
|
* parentStepId?: string | null,
|
|
2218
2630
|
* }} traceCtx
|
|
2631
|
+
* @param {{
|
|
2632
|
+
* workflow?: import("./workflow/workflow.js").Workflow,
|
|
2633
|
+
* input?: *,
|
|
2634
|
+
* state?: object,
|
|
2635
|
+
* }} [options]
|
|
2219
2636
|
*/
|
|
2220
|
-
async resumeWorkflowContext(traceCtx) {
|
|
2637
|
+
async resumeWorkflowContext(traceCtx, options = {}) {
|
|
2221
2638
|
if (!traceCtx?.executionId || !traceCtx?.workflowId) {
|
|
2222
2639
|
throw new Error("traceCtx requires executionId and workflowId");
|
|
2223
2640
|
}
|
|
@@ -2226,19 +2643,22 @@ var DNotifier = class {
|
|
|
2226
2643
|
token: this.authToken ?? null,
|
|
2227
2644
|
appendOnly: true
|
|
2228
2645
|
});
|
|
2229
|
-
const workflow = new Workflow({
|
|
2646
|
+
const workflow = options.workflow ?? new Workflow({
|
|
2230
2647
|
name: traceCtx.workflowId,
|
|
2231
2648
|
observability: true,
|
|
2232
2649
|
entry: async () => {
|
|
2233
2650
|
}
|
|
2234
2651
|
});
|
|
2235
|
-
|
|
2652
|
+
if (workflow.observability) {
|
|
2653
|
+
await observability.registerWorkflow(workflow);
|
|
2654
|
+
}
|
|
2236
2655
|
return new WorkflowContext({
|
|
2237
2656
|
notifier: this,
|
|
2238
2657
|
senderId: this.userId,
|
|
2239
2658
|
workflow,
|
|
2240
|
-
input: null,
|
|
2241
|
-
|
|
2659
|
+
input: options.input ?? null,
|
|
2660
|
+
state: options.state && typeof options.state === "object" ? { ...options.state } : {},
|
|
2661
|
+
observability: workflow.observability ? observability : null,
|
|
2242
2662
|
executionId: traceCtx.executionId,
|
|
2243
2663
|
traceParentStepId: traceCtx.parentStepId ?? null
|
|
2244
2664
|
});
|