@nodetool-ai/kernel 0.7.0-rc.11
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/actor.d.ts +110 -0
- package/dist/actor.d.ts.map +1 -0
- package/dist/actor.js +430 -0
- package/dist/actor.js.map +1 -0
- package/dist/channel.d.ts +46 -0
- package/dist/channel.d.ts.map +1 -0
- package/dist/channel.js +191 -0
- package/dist/channel.js.map +1 -0
- package/dist/durable-inbox.d.ts +82 -0
- package/dist/durable-inbox.d.ts.map +1 -0
- package/dist/durable-inbox.js +158 -0
- package/dist/durable-inbox.js.map +1 -0
- package/dist/graph-utils.d.ts +60 -0
- package/dist/graph-utils.d.ts.map +1 -0
- package/dist/graph-utils.js +249 -0
- package/dist/graph-utils.js.map +1 -0
- package/dist/graph.d.ts +153 -0
- package/dist/graph.d.ts.map +1 -0
- package/dist/graph.js +656 -0
- package/dist/graph.js.map +1 -0
- package/dist/inbox.d.ts +109 -0
- package/dist/inbox.d.ts.map +1 -0
- package/dist/inbox.js +310 -0
- package/dist/inbox.js.map +1 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +16 -0
- package/dist/index.js.map +1 -0
- package/dist/io.d.ts +92 -0
- package/dist/io.d.ts.map +1 -0
- package/dist/io.js +128 -0
- package/dist/io.js.map +1 -0
- package/dist/runner.d.ts +221 -0
- package/dist/runner.d.ts.map +1 -0
- package/dist/runner.js +1018 -0
- package/dist/runner.js.map +1 -0
- package/dist/suspendable.d.ts +68 -0
- package/dist/suspendable.d.ts.map +1 -0
- package/dist/suspendable.js +108 -0
- package/dist/suspendable.js.map +1 -0
- package/dist/trigger-manager.d.ts +107 -0
- package/dist/trigger-manager.d.ts.map +1 -0
- package/dist/trigger-manager.js +214 -0
- package/dist/trigger-manager.js.map +1 -0
- package/dist/trigger-wakeup.d.ts +54 -0
- package/dist/trigger-wakeup.d.ts.map +1 -0
- package/dist/trigger-wakeup.js +99 -0
- package/dist/trigger-wakeup.js.map +1 -0
- package/dist/trigger.d.ts +57 -0
- package/dist/trigger.d.ts.map +1 -0
- package/dist/trigger.js +145 -0
- package/dist/trigger.js.map +1 -0
- package/package.json +49 -0
package/dist/channel.js
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Streaming channels for named, many-to-many, graph-independent communication.
|
|
3
|
+
*
|
|
4
|
+
* Port of src/nodetool/workflows/channel.py.
|
|
5
|
+
*
|
|
6
|
+
* Architecture:
|
|
7
|
+
* - Queue-per-Subscriber: Each subscriber gets its own buffer for isolation.
|
|
8
|
+
* - Broadcast Pattern: Publishing pushes to all subscriber buffers.
|
|
9
|
+
* - Backpressure via buffer limits per subscriber queue.
|
|
10
|
+
*/
|
|
11
|
+
import { createLogger } from "@nodetool-ai/config";
|
|
12
|
+
const log = createLogger("nodetool.kernel.channel");
|
|
13
|
+
function deferred() {
|
|
14
|
+
let resolve;
|
|
15
|
+
const promise = new Promise((res) => {
|
|
16
|
+
resolve = res;
|
|
17
|
+
});
|
|
18
|
+
return { promise, resolve };
|
|
19
|
+
}
|
|
20
|
+
// ---------------------------------------------------------------------------
|
|
21
|
+
// Stop sentinel
|
|
22
|
+
// ---------------------------------------------------------------------------
|
|
23
|
+
const STOP_SIGNAL = Symbol("STOP_SIGNAL");
|
|
24
|
+
// ---------------------------------------------------------------------------
|
|
25
|
+
// Channel
|
|
26
|
+
// ---------------------------------------------------------------------------
|
|
27
|
+
export class Channel {
|
|
28
|
+
name;
|
|
29
|
+
_subscribers = new Map();
|
|
30
|
+
_closed = false;
|
|
31
|
+
_messageType;
|
|
32
|
+
constructor(name, _bufferLimit = 100, messageType) {
|
|
33
|
+
this.name = name;
|
|
34
|
+
this._messageType = messageType;
|
|
35
|
+
}
|
|
36
|
+
get messageType() {
|
|
37
|
+
return this._messageType;
|
|
38
|
+
}
|
|
39
|
+
get isClosed() {
|
|
40
|
+
return this._closed;
|
|
41
|
+
}
|
|
42
|
+
async publish(item) {
|
|
43
|
+
if (this._closed) {
|
|
44
|
+
log.error("Publish to closed channel", { channel: this.name });
|
|
45
|
+
throw new Error(`Channel ${this.name} is closed`);
|
|
46
|
+
}
|
|
47
|
+
if (this._messageType !== undefined &&
|
|
48
|
+
!(item instanceof this._messageType)) {
|
|
49
|
+
throw new TypeError(`Channel '${this.name}' expects messages of type ${this._messageType.name}, got ${typeof item}`);
|
|
50
|
+
}
|
|
51
|
+
for (const sub of this._subscribers.values()) {
|
|
52
|
+
sub.buffer.push(item);
|
|
53
|
+
// Wake any waiting consumer
|
|
54
|
+
for (const w of sub.waiters.splice(0)) {
|
|
55
|
+
w.resolve();
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
async *subscribe(subscriberId) {
|
|
60
|
+
if (this._closed)
|
|
61
|
+
return;
|
|
62
|
+
const sub = { buffer: [], waiters: [] };
|
|
63
|
+
this._subscribers.set(subscriberId, sub);
|
|
64
|
+
try {
|
|
65
|
+
while (true) {
|
|
66
|
+
if (sub.buffer.length > 0) {
|
|
67
|
+
const item = sub.buffer.shift();
|
|
68
|
+
if (item === STOP_SIGNAL)
|
|
69
|
+
break;
|
|
70
|
+
yield item;
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
// Nothing buffered – wait for data
|
|
74
|
+
if (this._closed)
|
|
75
|
+
break;
|
|
76
|
+
const d = deferred();
|
|
77
|
+
sub.waiters.push(d);
|
|
78
|
+
await d.promise;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
finally {
|
|
82
|
+
this._subscribers.delete(subscriberId);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
async close() {
|
|
86
|
+
this._closed = true;
|
|
87
|
+
for (const sub of this._subscribers.values()) {
|
|
88
|
+
sub.buffer.push(STOP_SIGNAL);
|
|
89
|
+
for (const w of sub.waiters.splice(0)) {
|
|
90
|
+
w.resolve();
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
getStats() {
|
|
95
|
+
return {
|
|
96
|
+
name: this.name,
|
|
97
|
+
subscriberCount: this._subscribers.size,
|
|
98
|
+
isClosed: this._closed,
|
|
99
|
+
messageType: this._messageType
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
// ---------------------------------------------------------------------------
|
|
104
|
+
// ChannelManager
|
|
105
|
+
// ---------------------------------------------------------------------------
|
|
106
|
+
export class ChannelManager {
|
|
107
|
+
_channels = new Map();
|
|
108
|
+
_channelTypes = new Map();
|
|
109
|
+
getChannel(name) {
|
|
110
|
+
return this._channels.get(name);
|
|
111
|
+
}
|
|
112
|
+
async createChannel(name, bufferLimit = 100, replace = false, messageType) {
|
|
113
|
+
if (this._channels.has(name) && !replace) {
|
|
114
|
+
log.error("Channel already exists", { channel: name });
|
|
115
|
+
throw new Error(`Channel '${name}' already exists`);
|
|
116
|
+
}
|
|
117
|
+
if (this._channels.has(name)) {
|
|
118
|
+
await this._channels.get(name).close();
|
|
119
|
+
}
|
|
120
|
+
log.debug("Channel created", { channel: name, bufferLimit });
|
|
121
|
+
const channel = new Channel(name, bufferLimit, messageType);
|
|
122
|
+
this._channels.set(name, channel);
|
|
123
|
+
if (messageType !== undefined) {
|
|
124
|
+
this._channelTypes.set(name, messageType);
|
|
125
|
+
}
|
|
126
|
+
else {
|
|
127
|
+
this._channelTypes.delete(name);
|
|
128
|
+
}
|
|
129
|
+
return channel;
|
|
130
|
+
}
|
|
131
|
+
async getOrCreateChannel(name, bufferLimit = 100, messageType) {
|
|
132
|
+
if (!this._channels.has(name)) {
|
|
133
|
+
const channel = new Channel(name, bufferLimit, messageType);
|
|
134
|
+
this._channels.set(name, channel);
|
|
135
|
+
if (messageType !== undefined) {
|
|
136
|
+
this._channelTypes.set(name, messageType);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
else {
|
|
140
|
+
const existingType = this._channelTypes.get(name);
|
|
141
|
+
if (messageType !== undefined &&
|
|
142
|
+
existingType !== undefined &&
|
|
143
|
+
messageType !== existingType) {
|
|
144
|
+
throw new TypeError(`Channel '${name}' has type ${existingType.name}, but ${messageType.name} was requested`);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
return this._channels.get(name);
|
|
148
|
+
}
|
|
149
|
+
getChannelType(name) {
|
|
150
|
+
return this._channelTypes.get(name);
|
|
151
|
+
}
|
|
152
|
+
async publish(name, item, bufferLimit = 100) {
|
|
153
|
+
const channel = await this.getOrCreateChannel(name, bufferLimit);
|
|
154
|
+
await channel.publish(item);
|
|
155
|
+
}
|
|
156
|
+
async publishTyped(name, item, messageType, bufferLimit = 100) {
|
|
157
|
+
const channel = await this.getOrCreateChannel(name, bufferLimit, messageType);
|
|
158
|
+
await channel.publish(item);
|
|
159
|
+
}
|
|
160
|
+
async *subscribe(name, subscriberId, bufferLimit = 100) {
|
|
161
|
+
const channel = await this.getOrCreateChannel(name, bufferLimit);
|
|
162
|
+
yield* channel.subscribe(subscriberId);
|
|
163
|
+
}
|
|
164
|
+
async *subscribeTyped(name, subscriberId, messageType, bufferLimit = 100) {
|
|
165
|
+
const channel = await this.getOrCreateChannel(name, bufferLimit, messageType);
|
|
166
|
+
yield* channel.subscribe(subscriberId);
|
|
167
|
+
}
|
|
168
|
+
async closeChannel(name) {
|
|
169
|
+
const channel = this._channels.get(name);
|
|
170
|
+
if (channel) {
|
|
171
|
+
log.debug("Channel closed", { channel: name });
|
|
172
|
+
await channel.close();
|
|
173
|
+
this._channels.delete(name);
|
|
174
|
+
this._channelTypes.delete(name);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
async closeAll() {
|
|
178
|
+
for (const channel of this._channels.values()) {
|
|
179
|
+
await channel.close();
|
|
180
|
+
}
|
|
181
|
+
this._channels.clear();
|
|
182
|
+
this._channelTypes.clear();
|
|
183
|
+
}
|
|
184
|
+
listChannels() {
|
|
185
|
+
return Array.from(this._channels.keys());
|
|
186
|
+
}
|
|
187
|
+
getAllStats() {
|
|
188
|
+
return Array.from(this._channels.values()).map((ch) => ch.getStats());
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
//# sourceMappingURL=channel.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"channel.js","sourceRoot":"","sources":["../src/channel.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAEnD,MAAM,GAAG,GAAG,YAAY,CAAC,yBAAyB,CAAC,CAAC;AAWpD,SAAS,QAAQ;IACf,IAAI,OAA0B,CAAC;IAC/B,MAAM,OAAO,GAAG,IAAI,OAAO,CAAI,CAAC,GAAG,EAAE,EAAE;QACrC,OAAO,GAAG,GAAG,CAAC;IAChB,CAAC,CAAC,CAAC;IACH,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,CAAC;AAC9B,CAAC;AAED,8EAA8E;AAC9E,gBAAgB;AAChB,8EAA8E;AAE9E,MAAM,WAAW,GAAkB,MAAM,CAAC,aAAa,CAAC,CAAC;AAsBzD,8EAA8E;AAC9E,UAAU;AACV,8EAA8E;AAE9E,MAAM,OAAO,OAAO;IACT,IAAI,CAAS;IACd,YAAY,GAAG,IAAI,GAAG,EAA8B,CAAC;IACrD,OAAO,GAAG,KAAK,CAAC;IAChB,YAAY,CAAiC;IAErD,YACE,IAAY,EACZ,YAAY,GAAG,GAAG,EAClB,WAA2C;QAE3C,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,YAAY,GAAG,WAAW,CAAC;IAClC,CAAC;IAED,IAAI,WAAW;QACb,OAAO,IAAI,CAAC,YAAY,CAAC;IAC3B,CAAC;IAED,IAAI,QAAQ;QACV,OAAO,IAAI,CAAC,OAAO,CAAC;IACtB,CAAC;IAED,KAAK,CAAC,OAAO,CAAC,IAAO;QACnB,IAAI,IAAI,CAAC,OAAO,EAAE,CAAC;YACjB,GAAG,CAAC,KAAK,CAAC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QACT,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,MAAM,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,EAAE,EAAE,EAAE,CAAC,EAAE,CAAC,QAAQ,EAAE,CAAC,CAAC;IACxE,CAAC;CACF"}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Durable inbox for reliable message delivery.
|
|
3
|
+
*
|
|
4
|
+
* Port of src/nodetool/workflows/durable_inbox.py.
|
|
5
|
+
*
|
|
6
|
+
* Provides at-least-once delivery with idempotency guarantees.
|
|
7
|
+
* The default implementation is in-memory; production deployments
|
|
8
|
+
* can swap in a database-backed adapter.
|
|
9
|
+
*/
|
|
10
|
+
export interface DurableMessage {
|
|
11
|
+
id: string;
|
|
12
|
+
runId: string;
|
|
13
|
+
nodeId: string;
|
|
14
|
+
handle: string;
|
|
15
|
+
messageId: string;
|
|
16
|
+
seq: number;
|
|
17
|
+
payload: unknown;
|
|
18
|
+
payloadRef?: string;
|
|
19
|
+
status: "pending" | "consumed";
|
|
20
|
+
createdAt: Date;
|
|
21
|
+
consumedAt?: Date;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Storage backend interface for durable inbox messages.
|
|
25
|
+
*/
|
|
26
|
+
export interface DurableInboxStore {
|
|
27
|
+
findByMessageId(messageId: string): Promise<DurableMessage | null>;
|
|
28
|
+
save(message: DurableMessage): Promise<void>;
|
|
29
|
+
findPending(runId: string, nodeId: string, handle: string, limit: number, minSeq?: number): Promise<DurableMessage[]>;
|
|
30
|
+
getMaxSeq(runId: string, nodeId: string, handle: string): Promise<number>;
|
|
31
|
+
markConsumed(messageId: string): Promise<void>;
|
|
32
|
+
deleteConsumed(runId: string, nodeId: string, handle: string, olderThanSeq: number): Promise<number>;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* In-memory implementation of DurableInboxStore.
|
|
36
|
+
* Suitable for single-process / testing scenarios.
|
|
37
|
+
*/
|
|
38
|
+
export declare class MemoryDurableInboxStore implements DurableInboxStore {
|
|
39
|
+
private messages;
|
|
40
|
+
findByMessageId(messageId: string): Promise<DurableMessage | null>;
|
|
41
|
+
save(message: DurableMessage): Promise<void>;
|
|
42
|
+
findPending(runId: string, nodeId: string, handle: string, limit: number, minSeq?: number): Promise<DurableMessage[]>;
|
|
43
|
+
getMaxSeq(runId: string, nodeId: string, handle: string): Promise<number>;
|
|
44
|
+
markConsumed(messageId: string): Promise<void>;
|
|
45
|
+
deleteConsumed(runId: string, nodeId: string, handle: string, olderThanSeq: number): Promise<number>;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Durable inbox for a specific node in a workflow run.
|
|
49
|
+
* Provides idempotent message delivery with sequencing.
|
|
50
|
+
*/
|
|
51
|
+
export declare class DurableInbox {
|
|
52
|
+
readonly runId: string;
|
|
53
|
+
readonly nodeId: string;
|
|
54
|
+
private store;
|
|
55
|
+
constructor(runId: string, nodeId: string, store?: DurableInboxStore);
|
|
56
|
+
/**
|
|
57
|
+
* Generate a deterministic message ID from the addressing tuple.
|
|
58
|
+
*/
|
|
59
|
+
static generateMessageId(runId: string, nodeId: string, handle: string, seq: number): string;
|
|
60
|
+
/**
|
|
61
|
+
* Append a message to the inbox (idempotent).
|
|
62
|
+
* If messageId already exists, returns existing without error.
|
|
63
|
+
*/
|
|
64
|
+
append(handle: string, payload: unknown, messageId?: string, payloadRef?: string): Promise<DurableMessage>;
|
|
65
|
+
/**
|
|
66
|
+
* Get pending messages for a handle in sequence order.
|
|
67
|
+
*/
|
|
68
|
+
getPending(handle: string, limit?: number, minSeq?: number): Promise<DurableMessage[]>;
|
|
69
|
+
/**
|
|
70
|
+
* Mark a message as consumed.
|
|
71
|
+
*/
|
|
72
|
+
markConsumed(message: DurableMessage): Promise<void>;
|
|
73
|
+
/**
|
|
74
|
+
* Get the maximum sequence number for a handle.
|
|
75
|
+
*/
|
|
76
|
+
getMaxSeq(handle: string): Promise<number>;
|
|
77
|
+
/**
|
|
78
|
+
* Clean up consumed messages older than a given sequence.
|
|
79
|
+
*/
|
|
80
|
+
cleanupConsumed(handle: string, olderThanSeq: number): Promise<number>;
|
|
81
|
+
}
|
|
82
|
+
//# sourceMappingURL=durable-inbox.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"durable-inbox.d.ts","sourceRoot":"","sources":["../src/durable-inbox.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAOH,MAAM,WAAW,cAAc;IAC7B,EAAE,EAAE,MAAM,CAAC;IACX,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,MAAM,CAAC;IACf,SAAS,EAAE,MAAM,CAAC;IAClB,GAAG,EAAE,MAAM,CAAC;IACZ,OAAO,EAAE,OAAO,CAAC;IACjB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,MAAM,EAAE,SAAS,GAAG,UAAU,CAAC;IAC/B,SAAS,EAAE,IAAI,CAAC;IAChB,UAAU,CAAC,EAAE,IAAI,CAAC;CACnB;AAED;;GAEG;AACH,MAAM,WAAW,iBAAiB;IAChC,eAAe,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,cAAc,GAAG,IAAI,CAAC,CAAC;IACnE,IAAI,CAAC,OAAO,EAAE,cAAc,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAC7C,WAAW,CACT,KAAK,EAAE,MAAM,EACb,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM,EACd,KAAK,EAAE,MAAM,EACb,MAAM,CAAC,EAAE,MAAM,GACd,OAAO,CAAC,cAAc,EAAE,CAAC,CAAC;IAC7B,SAAS,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;IAC1E,YAAY,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,C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AAM;IAKT;;;OAGG;IACG,MAAM,CACV,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,OAAO,EAChB,SAAS,CAAC,EAAE,MAAM,EAClB,UAAU,CAAC,EAAE,MAAM,GAClB,OAAO,CAAC,cAAc,CAAC;IAuC1B;;OAEG;IACG,UAAU,CACd,MAAM,EAAE,MAAM,EACd,KAAK,SAAM,EACX,MAAM,SAAI,GACT,OAAO,CAAC,cAAc,EAAE,CAAC;IAU5B;;OAEG;IACG,YAAY,CAAC,OAAO,EAAE,cAAc,GAAG,OAAO,CAAC,IAAI,CAAC;IAS1D;;OAEG;IACG,SAAS,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAIhD;;OAEG;IACG,eAAe,CAAC,MAAM,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;CAiB7E"}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Durable inbox for reliable message delivery.
|
|
3
|
+
*
|
|
4
|
+
* Port of src/nodetool/workflows/durable_inbox.py.
|
|
5
|
+
*
|
|
6
|
+
* Provides at-least-once delivery with idempotency guarantees.
|
|
7
|
+
* The default implementation is in-memory; production deployments
|
|
8
|
+
* can swap in a database-backed adapter.
|
|
9
|
+
*/
|
|
10
|
+
import { createHash } from "node:crypto";
|
|
11
|
+
import { createLogger } from "@nodetool-ai/config";
|
|
12
|
+
const log = createLogger("nodetool.kernel.durable-inbox");
|
|
13
|
+
/**
|
|
14
|
+
* In-memory implementation of DurableInboxStore.
|
|
15
|
+
* Suitable for single-process / testing scenarios.
|
|
16
|
+
*/
|
|
17
|
+
export class MemoryDurableInboxStore {
|
|
18
|
+
messages = [];
|
|
19
|
+
async findByMessageId(messageId) {
|
|
20
|
+
return this.messages.find((m) => m.messageId === messageId) ?? null;
|
|
21
|
+
}
|
|
22
|
+
async save(message) {
|
|
23
|
+
this.messages.push(message);
|
|
24
|
+
}
|
|
25
|
+
async findPending(runId, nodeId, handle, limit, minSeq = 0) {
|
|
26
|
+
return this.messages
|
|
27
|
+
.filter((m) => m.runId === runId &&
|
|
28
|
+
m.nodeId === nodeId &&
|
|
29
|
+
m.handle === handle &&
|
|
30
|
+
m.status === "pending" &&
|
|
31
|
+
m.seq >= minSeq)
|
|
32
|
+
.sort((a, b) => a.seq - b.seq)
|
|
33
|
+
.slice(0, limit);
|
|
34
|
+
}
|
|
35
|
+
async getMaxSeq(runId, nodeId, handle) {
|
|
36
|
+
let max = 0;
|
|
37
|
+
for (const m of this.messages) {
|
|
38
|
+
if (m.runId === runId &&
|
|
39
|
+
m.nodeId === nodeId &&
|
|
40
|
+
m.handle === handle &&
|
|
41
|
+
m.seq > max) {
|
|
42
|
+
max = m.seq;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return max;
|
|
46
|
+
}
|
|
47
|
+
async markConsumed(messageId) {
|
|
48
|
+
const msg = this.messages.find((m) => m.messageId === messageId);
|
|
49
|
+
if (msg) {
|
|
50
|
+
msg.status = "consumed";
|
|
51
|
+
msg.consumedAt = new Date();
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
async deleteConsumed(runId, nodeId, handle, olderThanSeq) {
|
|
55
|
+
const before = this.messages.length;
|
|
56
|
+
this.messages = this.messages.filter((m) => !(m.runId === runId &&
|
|
57
|
+
m.nodeId === nodeId &&
|
|
58
|
+
m.handle === handle &&
|
|
59
|
+
m.status === "consumed" &&
|
|
60
|
+
m.seq < olderThanSeq));
|
|
61
|
+
return before - this.messages.length;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Durable inbox for a specific node in a workflow run.
|
|
66
|
+
* Provides idempotent message delivery with sequencing.
|
|
67
|
+
*/
|
|
68
|
+
export class DurableInbox {
|
|
69
|
+
runId;
|
|
70
|
+
nodeId;
|
|
71
|
+
store;
|
|
72
|
+
constructor(runId, nodeId, store) {
|
|
73
|
+
this.runId = runId;
|
|
74
|
+
this.nodeId = nodeId;
|
|
75
|
+
this.store = store ?? new MemoryDurableInboxStore();
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Generate a deterministic message ID from the addressing tuple.
|
|
79
|
+
*/
|
|
80
|
+
static generateMessageId(runId, nodeId, handle, seq) {
|
|
81
|
+
const key = `${runId}:${nodeId}:${handle}:${seq}`;
|
|
82
|
+
return createHash("sha256").update(key).digest("hex").slice(0, 16);
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Append a message to the inbox (idempotent).
|
|
86
|
+
* If messageId already exists, returns existing without error.
|
|
87
|
+
*/
|
|
88
|
+
async append(handle, payload, messageId, payloadRef) {
|
|
89
|
+
const nextSeq = (await this.store.getMaxSeq(this.runId, this.nodeId, handle)) + 1;
|
|
90
|
+
const finalId = messageId ??
|
|
91
|
+
DurableInbox.generateMessageId(this.runId, this.nodeId, handle, nextSeq);
|
|
92
|
+
const existing = await this.store.findByMessageId(finalId);
|
|
93
|
+
if (existing) {
|
|
94
|
+
log.debug("Message already exists (idempotent)", { messageId: finalId });
|
|
95
|
+
return existing;
|
|
96
|
+
}
|
|
97
|
+
const message = {
|
|
98
|
+
id: finalId,
|
|
99
|
+
runId: this.runId,
|
|
100
|
+
nodeId: this.nodeId,
|
|
101
|
+
handle,
|
|
102
|
+
messageId: finalId,
|
|
103
|
+
seq: nextSeq,
|
|
104
|
+
payload,
|
|
105
|
+
payloadRef,
|
|
106
|
+
status: "pending",
|
|
107
|
+
createdAt: new Date()
|
|
108
|
+
};
|
|
109
|
+
await this.store.save(message);
|
|
110
|
+
log.debug("Appended message to inbox", {
|
|
111
|
+
messageId: finalId,
|
|
112
|
+
runId: this.runId,
|
|
113
|
+
nodeId: this.nodeId,
|
|
114
|
+
handle,
|
|
115
|
+
seq: nextSeq
|
|
116
|
+
});
|
|
117
|
+
return message;
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Get pending messages for a handle in sequence order.
|
|
121
|
+
*/
|
|
122
|
+
async getPending(handle, limit = 100, minSeq = 0) {
|
|
123
|
+
return this.store.findPending(this.runId, this.nodeId, handle, limit, minSeq);
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Mark a message as consumed.
|
|
127
|
+
*/
|
|
128
|
+
async markConsumed(message) {
|
|
129
|
+
await this.store.markConsumed(message.messageId);
|
|
130
|
+
log.debug("Marked message as consumed", {
|
|
131
|
+
messageId: message.messageId,
|
|
132
|
+
handle: message.handle,
|
|
133
|
+
seq: message.seq
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Get the maximum sequence number for a handle.
|
|
138
|
+
*/
|
|
139
|
+
async getMaxSeq(handle) {
|
|
140
|
+
return this.store.getMaxSeq(this.runId, this.nodeId, handle);
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Clean up consumed messages older than a given sequence.
|
|
144
|
+
*/
|
|
145
|
+
async cleanupConsumed(handle, olderThanSeq) {
|
|
146
|
+
const count = await this.store.deleteConsumed(this.runId, this.nodeId, handle, olderThanSeq);
|
|
147
|
+
if (count > 0) {
|
|
148
|
+
log.info("Cleaned up consumed messages", {
|
|
149
|
+
count,
|
|
150
|
+
runId: this.runId,
|
|
151
|
+
nodeId: this.nodeId,
|
|
152
|
+
handle
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
return count;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
//# sourceMappingURL=durable-inbox.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"durable-inbox.js","sourceRoot":"","sources":["../src/durable-inbox.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAEnD,MAAM,GAAG,GAAG,YAAY,CAAC,+BAA+B,CAAC,CAAC;AAuC1D;;;GAGG;AACH,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;gBACvC,KAAK;gBACL,KAAK,EAAE,IAAI,CAAC,KAAK;gBACjB,MAAM,EAAE,IAAI,CAAC,MAAM;gBACnB,MAAM;aACP,CAAC,CAAC;QACL,CAAC;QACD,OAAO,KAAK,CAAC;IACf,CAAC;CACF"}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Graph utilities for node and edge operations.
|
|
3
|
+
*
|
|
4
|
+
* Port of src/nodetool/workflows/graph_utils.py.
|
|
5
|
+
*/
|
|
6
|
+
import type { Edge, GraphData, NodeDescriptor } from "@nodetool-ai/protocol";
|
|
7
|
+
import { Graph } from "./graph.js";
|
|
8
|
+
/**
|
|
9
|
+
* Find a node by ID or throw.
|
|
10
|
+
*/
|
|
11
|
+
export declare function findNodeOrThrow(graph: Graph, nodeId: string): NodeDescriptor;
|
|
12
|
+
/**
|
|
13
|
+
* Returns mapping of targetHandle -> source output type string.
|
|
14
|
+
*
|
|
15
|
+
* For each incoming edge to nodeId, looks up the source node's outputs
|
|
16
|
+
* to find the type of the sourceHandle.
|
|
17
|
+
*/
|
|
18
|
+
export declare function getNodeInputTypes(graph: Graph, nodeId: string): Record<string, string | undefined>;
|
|
19
|
+
/**
|
|
20
|
+
* BFS downstream subgraph starting from (nodeId, sourceHandle).
|
|
21
|
+
*
|
|
22
|
+
* Returns the initial edges from nodeId/sourceHandle, plus all
|
|
23
|
+
* reachable downstream nodes and edges.
|
|
24
|
+
*/
|
|
25
|
+
export declare function getDownstreamSubgraph(graph: Graph, nodeId: string, sourceHandle: string): {
|
|
26
|
+
initialEdges: Edge[];
|
|
27
|
+
nodes: NodeDescriptor[];
|
|
28
|
+
edges: Edge[];
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* Returns true if a node is marked as bypassed via ui_properties.bypassed.
|
|
32
|
+
*/
|
|
33
|
+
export declare function isNodeBypassed(node: NodeDescriptor): boolean;
|
|
34
|
+
/**
|
|
35
|
+
* Rewrite a graph to route around nodes marked with
|
|
36
|
+
* `ui_properties.bypassed === true`.
|
|
37
|
+
*
|
|
38
|
+
* For each bypassed node and each of its outgoing data edges, we look for
|
|
39
|
+
* an incoming data edge whose source output type is compatible with the
|
|
40
|
+
* outgoing edge's target input type. If one is found, a new edge is
|
|
41
|
+
* created directly from the upstream source to the downstream target,
|
|
42
|
+
* preserving the downstream handle and any edge metadata (id, ui_properties,
|
|
43
|
+
* edge_type). Outgoing edges with no type-compatible input are dropped.
|
|
44
|
+
*
|
|
45
|
+
* When multiple incoming edges would match an outgoing edge, preference is
|
|
46
|
+
* given to an incoming edge whose targetHandle matches the outgoing edge's
|
|
47
|
+
* sourceHandle (name match), falling back to the first type-compatible one.
|
|
48
|
+
*
|
|
49
|
+
* Chained bypassed nodes (A → B(bypassed) → C(bypassed) → D) are handled
|
|
50
|
+
* by iterating until no further bypassed node can be rewritten — each
|
|
51
|
+
* iteration replaces bypassed sources with their already-resolved upstream
|
|
52
|
+
* source so that a chain collapses into a direct edge from the first
|
|
53
|
+
* non-bypassed source to the first non-bypassed target.
|
|
54
|
+
*
|
|
55
|
+
* Control edges (edge_type === "control") touching a bypassed node are
|
|
56
|
+
* dropped: bypassing intentionally removes the node from execution, so
|
|
57
|
+
* control connections to/from it no longer have a meaningful target.
|
|
58
|
+
*/
|
|
59
|
+
export declare function rewriteBypassedNodes(data: GraphData): GraphData;
|
|
60
|
+
//# sourceMappingURL=graph-utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"graph-utils.d.ts","sourceRoot":"","sources":["../src/graph-utils.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,IAAI,EAAE,SAAS,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AAE7E,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAEnC;;GAEG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,GAAG,cAAc,CAM5E;AAED;;;;;GAKG;AACH,wBAAgB,iBAAiB,CAC/B,KAAK,EAAE,KAAK,EACZ,MAAM,EAAE,MAAM,GACb,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC,CAapC;AAED;;;;;GAKG;AACH,wBAAgB,qBAAqB,CACnC,KAAK,EAAE,KAAK,EACZ,MAAM,EAAE,MAAM,EACd,YAAY,EAAE,MAAM,GACnB;IAAE,YAAY,EAAE,IAAI,EAAE,CAAC;IAAC,KAAK,EAAE,cAAc,EAAE,CAAC;IAAC,KAAK,EAAE,IAAI,EAAE,CAAA;CAAE,CA2ClE;AAMD;;GAEG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,cAAc,GAAG,OAAO,CAI5D;AA4DD;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,wBAAgB,oBAAoB,CAAC,IAAI,EAAE,SAAS,GAAG,SAAS,CA0G/D"}
|