@swirls/looms 0.24.0 → 0.26.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/CHANGELOG.md +34 -0
- package/dist/actor/cell.d.ts +3 -1
- package/dist/actor/cell.js +1 -0
- package/dist/actor/host.d.ts +3 -1
- package/dist/actor/host.js +2 -1
- package/dist/actor/routing.d.ts +6 -3
- package/dist/actor/routing.js +6 -4
- package/dist/agent/agent-session.js +2 -2
- package/dist/agent/debugger/agent-chat.js +4 -3
- package/dist/agent/definitions.d.ts +3 -1
- package/dist/agent/effects.js +5 -3
- package/dist/agent/module.d.ts +730 -730
- package/dist/agent/threads.js +4 -1
- package/dist/approval/effects.d.ts +26 -26
- package/dist/approval/module.d.ts +52 -52
- package/dist/approval/steps.d.ts +3 -1
- package/dist/approval/steps.js +2 -2
- package/dist/client/client.d.ts +7 -1
- package/dist/client/client.js +5 -2
- package/dist/cloudflare/durable-object.d.ts +3 -1
- package/dist/cloudflare/durable-object.js +1 -0
- package/dist/cloudflare/queue-consumer.d.ts +3 -1
- package/dist/cloudflare/queue-consumer.js +2 -1
- package/dist/cloudflare/router.d.ts +3 -1
- package/dist/cloudflare/router.js +1 -1
- package/dist/core/compose.d.ts +8 -2
- package/dist/core/compose.js +6 -3
- package/dist/core/effects.d.ts +3 -0
- package/dist/core/envelope.d.ts +2 -0
- package/dist/core/envelope.js +2 -2
- package/dist/core/event-codec.d.ts +4 -3
- package/dist/core/event-codec.js +7 -7
- package/dist/core/fold.d.ts +3 -0
- package/dist/core/fold.js +3 -1
- package/dist/core/fork.js +1 -1
- package/dist/core/ids.d.ts +70 -5
- package/dist/core/ids.js +164 -41
- package/dist/core/snapshots.d.ts +2 -0
- package/dist/core/snapshots.js +2 -1
- package/dist/core/thread.d.ts +8 -0
- package/dist/debugger/app/assets/index-D_FKxPrN.js +23 -0
- package/dist/debugger/app/assets/routes-CkwzFejg.js +47 -0
- package/dist/debugger/app/index.html +1 -1
- package/dist/debugger/context.d.ts +4 -1
- package/dist/debugger/context.js +4 -4
- package/dist/debugger/shell/debugger-shell.d.ts +4 -1
- package/dist/debugger/shell/debugger-shell.js +2 -2
- package/dist/debugger/shell/use-start-run.d.ts +1 -1
- package/dist/debugger/shell/use-start-run.js +1 -2
- package/dist/evaluator/steps.d.ts +2 -2
- package/dist/evaluator/steps.js +3 -3
- package/dist/react/react.d.ts +8 -3
- package/dist/react/react.js +15 -2
- package/dist/react/store.d.ts +5 -1
- package/dist/react/store.js +5 -4
- package/dist/runtime/cancellation.js +5 -5
- package/dist/runtime/deadline-transition-planner.d.ts +1 -0
- package/dist/runtime/deadline-transition-planner.js +8 -7
- package/dist/runtime/effect-admission.js +1 -0
- package/dist/runtime/helpers.d.ts +6 -5
- package/dist/runtime/helpers.js +8 -8
- package/dist/runtime/local-effect-executor.js +15 -10
- package/dist/runtime/looms.d.ts +6 -1
- package/dist/runtime/looms.js +4 -1
- package/dist/runtime/retry-policy.d.ts +2 -1
- package/dist/runtime/retry-policy.js +2 -1
- package/dist/runtime/runtime.js +10 -10
- package/dist/runtime/server/routes/runs.js +5 -5
- package/dist/runtime/types.d.ts +7 -1
- package/dist/runtime/wake-engine.js +10 -9
- package/dist/runtime/worker-callbacks.js +9 -8
- package/dist/runtime/worker-effect-dispatch.d.ts +3 -2
- package/dist/runtime/worker-effect-dispatch.js +7 -5
- package/dist/swarm/board.d.ts +292 -0
- package/dist/swarm/board.js +123 -0
- package/dist/swarm/conformance.d.ts +15 -0
- package/dist/swarm/conformance.js +225 -0
- package/dist/swarm/definitions.d.ts +94 -0
- package/dist/swarm/definitions.js +51 -0
- package/dist/swarm/effects.d.ts +4720 -0
- package/dist/swarm/effects.js +296 -0
- package/dist/swarm/ensure.d.ts +16 -0
- package/dist/swarm/ensure.js +24 -0
- package/dist/swarm/events.d.ts +240 -0
- package/dist/swarm/events.js +49 -0
- package/dist/swarm/index.d.ts +15 -0
- package/dist/swarm/index.js +15 -0
- package/dist/swarm/memory-board.d.ts +10 -0
- package/dist/swarm/memory-board.js +344 -0
- package/dist/swarm/module.d.ts +9593 -0
- package/dist/swarm/module.js +94 -0
- package/dist/swarm/projections.d.ts +60 -0
- package/dist/swarm/projections.js +102 -0
- package/dist/swarm/prompt.d.ts +4 -0
- package/dist/swarm/prompt.js +35 -0
- package/dist/swarm/scope.d.ts +357 -0
- package/dist/swarm/scope.js +8 -0
- package/dist/swarm/services.d.ts +29 -0
- package/dist/swarm/services.js +40 -0
- package/dist/swarm/signals.d.ts +14 -0
- package/dist/swarm/signals.js +15 -0
- package/dist/swarm/steps.d.ts +15 -0
- package/dist/swarm/steps.js +27 -0
- package/dist/swarm/threads.d.ts +41 -0
- package/dist/swarm/threads.js +244 -0
- package/dist/swarm/tools.d.ts +20 -0
- package/dist/swarm/tools.js +100 -0
- package/dist/workflow/effects.js +6 -5
- package/dist/workflow/graph.d.ts +3 -3
- package/dist/workflow/graph.js +5 -5
- package/dist/workflow/module.d.ts +8 -2
- package/dist/workflow/threads.d.ts +14 -2
- package/dist/workflow/threads.js +54 -44
- package/package.json +7 -2
- package/dist/debugger/app/assets/index-CxbIGkpY.js +0 -22
- package/dist/debugger/app/assets/routes-Ch30UGik.js +0 -47
package/dist/core/ids.d.ts
CHANGED
|
@@ -1,13 +1,77 @@
|
|
|
1
|
+
/** The ids a format draws fresh: one per event, run, thread, or wait. */
|
|
2
|
+
export type DrawnIdKind = 'event' | 'run' | 'thread' | 'wait';
|
|
3
|
+
/** The ids a format derives from parts, so replay names the same child every time. */
|
|
4
|
+
export type DerivedIdKind = 'run' | 'thread';
|
|
5
|
+
export type IdPart = string | number;
|
|
6
|
+
/**
|
|
7
|
+
* How a process names events, runs, threads, and waits. The default is {@link typeIds}.
|
|
8
|
+
*
|
|
9
|
+
* Two rules hold for every id a format returns:
|
|
10
|
+
*
|
|
11
|
+
* - It is a non-empty string.
|
|
12
|
+
* - It contains no `:`. Effect ids are `<threadId>:<tag>`.
|
|
13
|
+
*
|
|
14
|
+
* `derive` must also be a pure function of `kind` and `parts`: reducers call it on every replay
|
|
15
|
+
* and must name the same child each time. Looms cannot check this.
|
|
16
|
+
*/
|
|
17
|
+
export interface LoomsIds {
|
|
18
|
+
/** A fresh id that no earlier call returned. */
|
|
19
|
+
create(kind: DrawnIdKind): string;
|
|
20
|
+
/** The same id for the same `kind` and `parts`, on every call, in every process. */
|
|
21
|
+
derive(kind: DerivedIdKind, parts: readonly IdPart[]): string;
|
|
22
|
+
}
|
|
23
|
+
declare const InvalidIdFormatError_base: new <A extends Record<string, any> = {}>(args: import("effect/Types").VoidIfEmpty<{ readonly [P in keyof A as P extends "_tag" ? never : P]: A[P]; }>) => import("effect/Cause").YieldableError & {
|
|
24
|
+
readonly _tag: "InvalidIdFormatError";
|
|
25
|
+
} & Readonly<A>;
|
|
26
|
+
export declare class InvalidIdFormatError extends InvalidIdFormatError_base<{
|
|
27
|
+
readonly message: string;
|
|
28
|
+
readonly rule: 'non-empty' | 'no-colon';
|
|
29
|
+
readonly id: unknown;
|
|
30
|
+
}> {
|
|
31
|
+
}
|
|
32
|
+
/** The 26-character TypeID suffix for a UUID's 16 bytes: 128 bits, left-padded to 130. */
|
|
33
|
+
export declare function encodeTypeIdSuffix(uuid: Uint8Array): string;
|
|
34
|
+
/** `<prefix>_<suffix>` for a UUID's 16 bytes, per TypeID 0.3. */
|
|
35
|
+
export declare function typeId(prefix: string, uuid: Uint8Array): string;
|
|
36
|
+
/**
|
|
37
|
+
* UUIDv7 bytes (RFC 9562): a 48-bit millisecond timestamp, then random bits. Ids drawn in the same
|
|
38
|
+
* millisecond, or after the clock steps back, increment the previous random bits (section 6.2,
|
|
39
|
+
* method 2), so every id this process draws sorts after the one before it.
|
|
40
|
+
*/
|
|
41
|
+
export declare function uuidV7(): Uint8Array;
|
|
42
|
+
/**
|
|
43
|
+
* The default id format: [TypeID](https://github.com/jetify-com/typeid) 0.3. Ids are `evt_`,
|
|
44
|
+
* `run_`, `thr_`, or `wait_` followed by 26 characters. Drawn ids carry a UUIDv7 and sort by
|
|
45
|
+
* creation time within a process; derived ids carry a UUIDv8 hashed from their parts.
|
|
46
|
+
*/
|
|
47
|
+
export declare const typeIds: LoomsIds;
|
|
48
|
+
/**
|
|
49
|
+
* The format an `ids` option names, or {@link typeIds} without one. A custom format is tried once
|
|
50
|
+
* for every kind here, and every id it returns later is checked against the same rules; a
|
|
51
|
+
* violation throws {@link InvalidIdFormatError}.
|
|
52
|
+
*/
|
|
53
|
+
export declare function resolveIds(ids?: LoomsIds): LoomsIds;
|
|
54
|
+
/** A fresh event id in the default format. A configured format is `ids.create('event')`. */
|
|
1
55
|
export declare function createEventId(): string;
|
|
56
|
+
/** A fresh run id in the default format. A configured format is `ids.create('run')`. */
|
|
2
57
|
export declare function createRunId(): string;
|
|
58
|
+
/** A fresh thread id in the default format. A configured format is `ids.create('thread')`. */
|
|
3
59
|
export declare function createThreadId(): string;
|
|
4
|
-
/** A stable `run_` id derived from `parts`, so a retry launches the same child run. */
|
|
5
|
-
export declare function deterministicRunId(...parts: readonly (string | number)[]): string;
|
|
6
60
|
/**
|
|
7
|
-
* A
|
|
8
|
-
*
|
|
61
|
+
* A stable run id in the default format, so a retry launches the same child run. A configured
|
|
62
|
+
* format is `ctx.ids.derive('run', parts)`.
|
|
63
|
+
*/
|
|
64
|
+
export declare function deterministicRunId(...parts: readonly IdPart[]): string;
|
|
65
|
+
/**
|
|
66
|
+
* A thread id in the default format derived only from `parts`, so a reducer that replays the same
|
|
67
|
+
* events names the same child thread every time. A configured format is
|
|
68
|
+
* `ctx.ids.derive('thread', parts)`.
|
|
69
|
+
*/
|
|
70
|
+
export declare function deterministicThreadId(...parts: readonly IdPart[]): string;
|
|
71
|
+
/**
|
|
72
|
+
* A wait id. With `scope` and `tag` it is the composed key `wait_<scope>_<tag>` and draws nothing.
|
|
73
|
+
* Without a tag it draws from the default format; a configured format is `ids.create('wait')`.
|
|
9
74
|
*/
|
|
10
|
-
export declare function deterministicThreadId(...parts: readonly (string | number)[]): string;
|
|
11
75
|
export declare function createWaitId(scope?: string, tag?: string | number): string;
|
|
12
76
|
export declare function createEffectId(threadId: string, causingSeqOrTag: number | string, index?: number): string;
|
|
13
77
|
export declare function parseEffectId(effectId: string): {
|
|
@@ -16,3 +80,4 @@ export declare function parseEffectId(effectId: string): {
|
|
|
16
80
|
tag?: string;
|
|
17
81
|
index?: number;
|
|
18
82
|
} | null;
|
|
83
|
+
export {};
|
package/dist/core/ids.js
CHANGED
|
@@ -1,63 +1,186 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
counter += 1;
|
|
5
|
-
const timestamp = DateTime.toEpochMillis(DateTime.nowUnsafe());
|
|
6
|
-
// Synchronous ID factories need process-independent entropy without an Effect runtime.
|
|
7
|
-
// oxlint-disable-next-line effecttsgo/crypto-random-uuid
|
|
8
|
-
const entropy = globalThis.crypto.randomUUID().replaceAll('-', '');
|
|
9
|
-
return `${timestamp.toString(36)}_${counter.toString(36)}_${entropy}`;
|
|
1
|
+
import { sha256 } from '@noble/hashes/sha2.js';
|
|
2
|
+
import { Data, DateTime, Predicate } from 'effect';
|
|
3
|
+
export class InvalidIdFormatError extends Data.TaggedError('InvalidIdFormatError') {
|
|
10
4
|
}
|
|
5
|
+
const TYPEID_PREFIX = {
|
|
6
|
+
event: 'evt',
|
|
7
|
+
run: 'run',
|
|
8
|
+
thread: 'thr',
|
|
9
|
+
wait: 'wait',
|
|
10
|
+
};
|
|
11
|
+
const CROCKFORD = '0123456789abcdefghjkmnpqrstvwxyz';
|
|
12
|
+
/** The 26-character TypeID suffix for a UUID's 16 bytes: 128 bits, left-padded to 130. */
|
|
13
|
+
export function encodeTypeIdSuffix(uuid) {
|
|
14
|
+
if (uuid.length !== 16) {
|
|
15
|
+
throw new RangeError(`A TypeID suffix encodes 16 bytes, got ${uuid.length}`);
|
|
16
|
+
}
|
|
17
|
+
let value = 0n;
|
|
18
|
+
for (const byte of uuid) {
|
|
19
|
+
value = (value << 8n) | BigInt(byte);
|
|
20
|
+
}
|
|
21
|
+
let suffix = '';
|
|
22
|
+
for (let index = 0; index < 26; index++) {
|
|
23
|
+
suffix = CROCKFORD.charAt(Number(value & 31n)) + suffix;
|
|
24
|
+
value >>= 5n;
|
|
25
|
+
}
|
|
26
|
+
return suffix;
|
|
27
|
+
}
|
|
28
|
+
/** `<prefix>_<suffix>` for a UUID's 16 bytes, per TypeID 0.3. */
|
|
29
|
+
export function typeId(prefix, uuid) {
|
|
30
|
+
return `${prefix}_${encodeTypeIdSuffix(uuid)}`;
|
|
31
|
+
}
|
|
32
|
+
const RANDOM_BYTES = 10;
|
|
33
|
+
let lastTimestamp = -1;
|
|
34
|
+
const lastRandom = new Uint8Array(RANDOM_BYTES);
|
|
35
|
+
/** How many bits of each `lastRandom` byte the UUID carries: 4 + 8 for rand_a, 6 + 56 for rand_b. */
|
|
36
|
+
const RANDOM_MASKS = [0x0f, 0xff, 0x3f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff];
|
|
37
|
+
function drawRandom() {
|
|
38
|
+
globalThis.crypto.getRandomValues(lastRandom);
|
|
39
|
+
lastRandom[2] = lastRandom[2] & 0x3f;
|
|
40
|
+
// Clear the top bit so a burst within one millisecond has room to count up.
|
|
41
|
+
lastRandom[0] = lastRandom[0] & 0x07;
|
|
42
|
+
}
|
|
43
|
+
/** Add one to the 74 random bits. Returns false when they wrap. */
|
|
44
|
+
function incrementRandom() {
|
|
45
|
+
for (let index = RANDOM_BYTES - 1; index >= 0; index--) {
|
|
46
|
+
lastRandom[index] = (lastRandom[index] + 1) & RANDOM_MASKS[index];
|
|
47
|
+
if (lastRandom[index] !== 0) {
|
|
48
|
+
return true;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* UUIDv7 bytes (RFC 9562): a 48-bit millisecond timestamp, then random bits. Ids drawn in the same
|
|
55
|
+
* millisecond, or after the clock steps back, increment the previous random bits (section 6.2,
|
|
56
|
+
* method 2), so every id this process draws sorts after the one before it.
|
|
57
|
+
*/
|
|
58
|
+
export function uuidV7() {
|
|
59
|
+
const now = DateTime.toEpochMillis(DateTime.nowUnsafe());
|
|
60
|
+
if (now > lastTimestamp) {
|
|
61
|
+
lastTimestamp = now;
|
|
62
|
+
drawRandom();
|
|
63
|
+
}
|
|
64
|
+
else if (!incrementRandom()) {
|
|
65
|
+
lastTimestamp += 1;
|
|
66
|
+
drawRandom();
|
|
67
|
+
}
|
|
68
|
+
const bytes = new Uint8Array(16);
|
|
69
|
+
let timestamp = lastTimestamp;
|
|
70
|
+
for (let index = 5; index >= 0; index--) {
|
|
71
|
+
bytes[index] = timestamp % 256;
|
|
72
|
+
timestamp = Math.floor(timestamp / 256);
|
|
73
|
+
}
|
|
74
|
+
bytes[6] = 0x70 | lastRandom[0];
|
|
75
|
+
bytes[7] = lastRandom[1];
|
|
76
|
+
bytes[8] = 0x80 | lastRandom[2];
|
|
77
|
+
bytes.set(lastRandom.subarray(3), 9);
|
|
78
|
+
return bytes;
|
|
79
|
+
}
|
|
80
|
+
const encoder = new TextEncoder();
|
|
81
|
+
/**
|
|
82
|
+
* UUIDv8 bytes whose 122 free bits are the leading bits of SHA-256 over `kind` and `parts`. Parts
|
|
83
|
+
* are hashed as JSON, so `['a:b', 'c']` and `['a', 'b:c']` differ, and so do `1` and `'1'`.
|
|
84
|
+
*/
|
|
85
|
+
function derivedUuid(kind, parts) {
|
|
86
|
+
const bytes = sha256(encoder.encode(`${kind}\n${JSON.stringify(parts)}`)).slice(0, 16);
|
|
87
|
+
bytes[6] = 0x80 | (bytes[6] & 0x0f);
|
|
88
|
+
bytes[8] = 0x80 | (bytes[8] & 0x3f);
|
|
89
|
+
return bytes;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* The default id format: [TypeID](https://github.com/jetify-com/typeid) 0.3. Ids are `evt_`,
|
|
93
|
+
* `run_`, `thr_`, or `wait_` followed by 26 characters. Drawn ids carry a UUIDv7 and sort by
|
|
94
|
+
* creation time within a process; derived ids carry a UUIDv8 hashed from their parts.
|
|
95
|
+
*/
|
|
96
|
+
export const typeIds = {
|
|
97
|
+
create: (kind) => typeId(TYPEID_PREFIX[kind], uuidV7()),
|
|
98
|
+
derive: (kind, parts) => typeId(TYPEID_PREFIX[kind], derivedUuid(kind, parts)),
|
|
99
|
+
};
|
|
100
|
+
function checkId(id, source) {
|
|
101
|
+
if (!Predicate.isString(id) || id.length === 0) {
|
|
102
|
+
throw new InvalidIdFormatError({
|
|
103
|
+
rule: 'non-empty',
|
|
104
|
+
id,
|
|
105
|
+
message: `Invalid id format: ${source} must return a non-empty string, got ${JSON.stringify(id)}`,
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
if (id.includes(':')) {
|
|
109
|
+
throw new InvalidIdFormatError({
|
|
110
|
+
rule: 'no-colon',
|
|
111
|
+
id,
|
|
112
|
+
message: `Invalid id format: ${source} returned ${JSON.stringify(id)}, which contains ":". Ids must not contain ":" because effect ids are "<threadId>:<tag>"`,
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
return id;
|
|
116
|
+
}
|
|
117
|
+
const checked = new WeakSet([typeIds]);
|
|
118
|
+
const DRAWN_KINDS = ['event', 'run', 'thread', 'wait'];
|
|
119
|
+
const DERIVED_KINDS = ['run', 'thread'];
|
|
120
|
+
/**
|
|
121
|
+
* The format an `ids` option names, or {@link typeIds} without one. A custom format is tried once
|
|
122
|
+
* for every kind here, and every id it returns later is checked against the same rules; a
|
|
123
|
+
* violation throws {@link InvalidIdFormatError}.
|
|
124
|
+
*/
|
|
125
|
+
export function resolveIds(ids) {
|
|
126
|
+
if (ids === undefined) {
|
|
127
|
+
return typeIds;
|
|
128
|
+
}
|
|
129
|
+
if (checked.has(ids)) {
|
|
130
|
+
return ids;
|
|
131
|
+
}
|
|
132
|
+
const resolved = {
|
|
133
|
+
create: (kind) => checkId(ids.create(kind), `ids.create('${kind}')`),
|
|
134
|
+
derive: (kind, parts) => checkId(ids.derive(kind, parts), `ids.derive('${kind}', parts)`),
|
|
135
|
+
};
|
|
136
|
+
for (const kind of DRAWN_KINDS) {
|
|
137
|
+
resolved.create(kind);
|
|
138
|
+
}
|
|
139
|
+
for (const kind of DERIVED_KINDS) {
|
|
140
|
+
resolved.derive(kind, ['looms', 0]);
|
|
141
|
+
}
|
|
142
|
+
checked.add(resolved);
|
|
143
|
+
return resolved;
|
|
144
|
+
}
|
|
145
|
+
/** A fresh event id in the default format. A configured format is `ids.create('event')`. */
|
|
11
146
|
export function createEventId() {
|
|
12
|
-
return
|
|
147
|
+
return typeIds.create('event');
|
|
13
148
|
}
|
|
149
|
+
/** A fresh run id in the default format. A configured format is `ids.create('run')`. */
|
|
14
150
|
export function createRunId() {
|
|
15
|
-
return
|
|
151
|
+
return typeIds.create('run');
|
|
16
152
|
}
|
|
153
|
+
/** A fresh thread id in the default format. A configured format is `ids.create('thread')`. */
|
|
17
154
|
export function createThreadId() {
|
|
18
|
-
return
|
|
19
|
-
}
|
|
20
|
-
function fnv1a32(value, seed) {
|
|
21
|
-
let hash = seed >>> 0;
|
|
22
|
-
for (let index = 0; index < value.length; index++) {
|
|
23
|
-
hash ^= value.charCodeAt(index);
|
|
24
|
-
hash = Math.imul(hash, 0x01000193);
|
|
25
|
-
}
|
|
26
|
-
return (hash >>> 0).toString(16).padStart(8, '0');
|
|
155
|
+
return typeIds.create('thread');
|
|
27
156
|
}
|
|
28
|
-
/**
|
|
157
|
+
/**
|
|
158
|
+
* A stable run id in the default format, so a retry launches the same child run. A configured
|
|
159
|
+
* format is `ctx.ids.derive('run', parts)`.
|
|
160
|
+
*/
|
|
29
161
|
export function deterministicRunId(...parts) {
|
|
30
|
-
return
|
|
162
|
+
return typeIds.derive('run', parts);
|
|
31
163
|
}
|
|
32
164
|
/**
|
|
33
|
-
* A
|
|
34
|
-
* child thread every time.
|
|
165
|
+
* A thread id in the default format derived only from `parts`, so a reducer that replays the same
|
|
166
|
+
* events names the same child thread every time. A configured format is
|
|
167
|
+
* `ctx.ids.derive('thread', parts)`.
|
|
35
168
|
*/
|
|
36
169
|
export function deterministicThreadId(...parts) {
|
|
37
|
-
|
|
38
|
-
let reversed = '';
|
|
39
|
-
for (let index = source.length - 1; index >= 0; index--) {
|
|
40
|
-
reversed += source.charAt(index);
|
|
41
|
-
}
|
|
42
|
-
const hex = [
|
|
43
|
-
fnv1a32(source, 0x811c9dc5),
|
|
44
|
-
fnv1a32(source, 0x9e3779b9),
|
|
45
|
-
fnv1a32(reversed, 0x85ebca6b),
|
|
46
|
-
fnv1a32(reversed, 0xc2b2ae35),
|
|
47
|
-
].join('');
|
|
48
|
-
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-5${hex.slice(13, 16)}-${((Number.parseInt(hex.slice(16, 18), 16) & 0x3f) |
|
|
49
|
-
0x80)
|
|
50
|
-
.toString(16)
|
|
51
|
-
.padStart(2, '0')}${hex.slice(18, 20)}-${hex.slice(20)}`;
|
|
170
|
+
return typeIds.derive('thread', parts);
|
|
52
171
|
}
|
|
172
|
+
/**
|
|
173
|
+
* A wait id. With `scope` and `tag` it is the composed key `wait_<scope>_<tag>` and draws nothing.
|
|
174
|
+
* Without a tag it draws from the default format; a configured format is `ids.create('wait')`.
|
|
175
|
+
*/
|
|
53
176
|
export function createWaitId(scope, tag) {
|
|
54
177
|
if (scope !== undefined && tag !== undefined) {
|
|
55
178
|
return `wait_${scope}_${tag}`;
|
|
56
179
|
}
|
|
57
180
|
if (scope !== undefined) {
|
|
58
|
-
return `wait_${scope}_${
|
|
181
|
+
return `wait_${scope}_${encodeTypeIdSuffix(uuidV7())}`;
|
|
59
182
|
}
|
|
60
|
-
return
|
|
183
|
+
return typeIds.create('wait');
|
|
61
184
|
}
|
|
62
185
|
export function createEffectId(threadId, causingSeqOrTag, index) {
|
|
63
186
|
if (Predicate.isString(causingSeqOrTag)) {
|
package/dist/core/snapshots.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Schema } from 'effect';
|
|
2
2
|
import { type EventEnvelope } from './envelope';
|
|
3
3
|
import { type FoldRegistry } from './fold';
|
|
4
|
+
import type { LoomsIds } from './ids';
|
|
4
5
|
import { type RunState } from './state';
|
|
5
6
|
import { type JsonValue } from './types';
|
|
6
7
|
export declare const DEFAULT_SNAPSHOT_EVERY = 200;
|
|
@@ -23,6 +24,7 @@ export declare function buildSnapshotEvent(runId: string, events: readonly Event
|
|
|
23
24
|
export declare function buildSnapshotMarker(runId: string, cursor: number, stateHash: string, options?: {
|
|
24
25
|
state?: RunState;
|
|
25
26
|
maxInlineBytes?: number;
|
|
27
|
+
ids?: LoomsIds;
|
|
26
28
|
}): EventEnvelope;
|
|
27
29
|
export declare function foldFromSnapshots(events: readonly EventEnvelope[], registry: FoldRegistry, options?: {
|
|
28
30
|
runId?: string;
|
package/dist/core/snapshots.js
CHANGED
|
@@ -34,6 +34,7 @@ export function buildSnapshotEvent(runId, events, registry, options) {
|
|
|
34
34
|
const last = events[events.length - 1];
|
|
35
35
|
return buildSnapshotMarker(runId, last?.seq ?? 0, hashRunState(state), {
|
|
36
36
|
state: options?.includeState ? state : undefined,
|
|
37
|
+
ids: registry.ids,
|
|
37
38
|
});
|
|
38
39
|
}
|
|
39
40
|
/**
|
|
@@ -55,7 +56,7 @@ export function buildSnapshotMarker(runId, cursor, stateHash, options) {
|
|
|
55
56
|
payload,
|
|
56
57
|
threadId: null,
|
|
57
58
|
origin: { type: 'system' },
|
|
58
|
-
});
|
|
59
|
+
}, { ids: options?.ids });
|
|
59
60
|
}
|
|
60
61
|
export function foldFromSnapshots(events, registry, options) {
|
|
61
62
|
const runId = options?.runId ?? events[0]?.runId ?? 'unknown';
|
package/dist/core/thread.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { RuntimeEffect } from './effects';
|
|
2
2
|
import type { EventEnvelope } from './envelope';
|
|
3
|
+
import type { LoomsIds } from './ids';
|
|
3
4
|
import type { InferDefinedSchema, InferSchemaOutput, SchemaInput } from './schema';
|
|
4
5
|
import type { ThreadStatus, JsonValue } from './types';
|
|
5
6
|
export interface StartContext<TInput = JsonValue> {
|
|
@@ -9,11 +10,18 @@ export interface StartContext<TInput = JsonValue> {
|
|
|
9
10
|
readonly definitionName: string;
|
|
10
11
|
readonly definitionVersion: string;
|
|
11
12
|
readonly input: TInput;
|
|
13
|
+
/** The id format this registry was composed with. */
|
|
14
|
+
readonly ids: LoomsIds;
|
|
12
15
|
}
|
|
13
16
|
export interface ThreadContext {
|
|
14
17
|
readonly runId: string;
|
|
15
18
|
readonly threadId: string;
|
|
16
19
|
readonly parentThreadId: string | null;
|
|
20
|
+
/**
|
|
21
|
+
* The id format this registry was composed with. Name a child with `ids.derive`, never
|
|
22
|
+
* `ids.create`: a reducer runs again on every replay.
|
|
23
|
+
*/
|
|
24
|
+
readonly ids: LoomsIds;
|
|
17
25
|
}
|
|
18
26
|
export interface ThreadDefinition<S = any> {
|
|
19
27
|
readonly kind: string;
|