Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
41737ab838 | ||
|
|
a36220157e | ||
|
|
d0f0d9a005 |
No files matched your search
@@ -0,0 +1,81 @@
|
||||
import { assert, it } from "@effect/vitest";
|
||||
import { CommandId, MessageId, ThreadId } from "@t3tools/contracts";
|
||||
import { DateTime, Effect, Layer, Option, Ref, Stream } from "effect";
|
||||
import { TestClock } from "effect/testing";
|
||||
|
||||
import { SqlitePersistenceMemory } from "../../persistence/Layers/Sqlite.ts";
|
||||
import {
|
||||
OrchestrationCommandReceiptRepository,
|
||||
type OrchestrationCommandReceipt,
|
||||
} from "../../persistence/Services/OrchestrationCommandReceipts.ts";
|
||||
import { OrchestrationEventStore } from "../../persistence/Services/OrchestrationEventStore.ts";
|
||||
import { createEmptyReadModel } from "../projector.ts";
|
||||
import { OrchestrationEngineService } from "../Services/OrchestrationEngine.ts";
|
||||
import { OrchestrationProjectionPipeline } from "../Services/ProjectionPipeline.ts";
|
||||
import { ProjectionSnapshotQuery } from "../Services/ProjectionSnapshotQuery.ts";
|
||||
import { OrchestrationEngineLive } from "./OrchestrationEngine.ts";
|
||||
|
||||
it.effect("uses TestClock-backed DateTime for rejected command receipts", () =>
|
||||
Effect.gen(function* () {
|
||||
const capturedReceipt = yield* Ref.make<Option.Option<OrchestrationCommandReceipt>>(
|
||||
Option.none(),
|
||||
);
|
||||
const fixedNow = DateTime.makeUnsafe("2026-05-08T16:30:00.000Z");
|
||||
const fixedNowIso = DateTime.formatIso(fixedNow);
|
||||
|
||||
const receiptRepositoryLayer = Layer.mock(OrchestrationCommandReceiptRepository)({
|
||||
getByCommandId: () => Effect.succeed(Option.none()),
|
||||
upsert: (receipt) => Ref.set(capturedReceipt, Option.some(receipt)),
|
||||
});
|
||||
|
||||
const testLayer = OrchestrationEngineLive.pipe(
|
||||
Layer.provide(
|
||||
Layer.mock(ProjectionSnapshotQuery)({
|
||||
getCommandReadModel: () => Effect.succeed(createEmptyReadModel(fixedNowIso)),
|
||||
}),
|
||||
),
|
||||
Layer.provide(
|
||||
Layer.mock(OrchestrationProjectionPipeline)({
|
||||
bootstrap: Effect.void,
|
||||
}),
|
||||
),
|
||||
Layer.provide(
|
||||
Layer.mock(OrchestrationEventStore)({
|
||||
readFromSequence: () => Stream.empty,
|
||||
}),
|
||||
),
|
||||
Layer.provide(receiptRepositoryLayer),
|
||||
Layer.provide(SqlitePersistenceMemory),
|
||||
Layer.provideMerge(TestClock.layer()),
|
||||
);
|
||||
|
||||
yield* Effect.gen(function* () {
|
||||
yield* TestClock.setTime(DateTime.toEpochMillis(fixedNow));
|
||||
const engine = yield* OrchestrationEngineService;
|
||||
const result = yield* Effect.result(
|
||||
engine.dispatch({
|
||||
type: "thread.turn.start",
|
||||
commandId: CommandId.make("cmd-clock-rejected"),
|
||||
threadId: ThreadId.make("thread-missing-clock"),
|
||||
message: {
|
||||
messageId: MessageId.make("msg-clock-rejected"),
|
||||
role: "user",
|
||||
text: "hello",
|
||||
attachments: [],
|
||||
},
|
||||
interactionMode: "default",
|
||||
runtimeMode: "approval-required",
|
||||
createdAt: fixedNowIso,
|
||||
}),
|
||||
);
|
||||
|
||||
assert.equal(result._tag, "Failure");
|
||||
const receipt = yield* Ref.get(capturedReceipt);
|
||||
assert.isTrue(Option.isSome(receipt));
|
||||
if (Option.isNone(receipt)) {
|
||||
return;
|
||||
}
|
||||
assert.equal(receipt.value.acceptedAt, fixedNowIso);
|
||||
}).pipe(Effect.provide(testLayer));
|
||||
}),
|
||||
);
|
||||
@@ -7,6 +7,7 @@ import type {
|
||||
import { OrchestrationCommand } from "@t3tools/contracts";
|
||||
import {
|
||||
Cause,
|
||||
DateTime,
|
||||
Deferred,
|
||||
Duration,
|
||||
Effect,
|
||||
@@ -51,6 +52,9 @@ interface CommandEnvelope {
|
||||
startedAtMs: number;
|
||||
}
|
||||
|
||||
const currentIso = DateTime.now.pipe(Effect.map(DateTime.formatIso));
|
||||
const currentEpochMillis = DateTime.now.pipe(Effect.map(DateTime.toEpochMillis));
|
||||
|
||||
function commandToAggregateRef(command: OrchestrationCommand): {
|
||||
readonly aggregateKind: "project" | "thread";
|
||||
readonly aggregateId: ProjectId | ThreadId;
|
||||
@@ -78,7 +82,7 @@ const makeOrchestrationEngine = Effect.gen(function* () {
|
||||
const projectionPipeline = yield* OrchestrationProjectionPipeline;
|
||||
const projectionSnapshotQuery = yield* ProjectionSnapshotQuery;
|
||||
|
||||
let commandReadModel = createEmptyReadModel(new Date().toISOString());
|
||||
let commandReadModel = createEmptyReadModel(yield* currentIso);
|
||||
|
||||
const commandQueue = yield* Queue.unbounded<CommandEnvelope>();
|
||||
const eventPubSub = yield* PubSub.unbounded<OrchestrationEvent>();
|
||||
@@ -97,7 +101,6 @@ const makeOrchestrationEngine = Effect.gen(function* () {
|
||||
|
||||
const processEnvelope = (envelope: CommandEnvelope): Effect.Effect<void> => {
|
||||
const dispatchStartSequence = commandReadModel.snapshotSequence;
|
||||
const processingStartedAtMs = Date.now();
|
||||
const aggregateRef = commandToAggregateRef(envelope.command);
|
||||
const baseMetricAttributes = {
|
||||
commandType: envelope.command.type,
|
||||
@@ -118,164 +121,164 @@ const makeOrchestrationEngine = Effect.gen(function* () {
|
||||
}
|
||||
});
|
||||
|
||||
return Effect.exit(
|
||||
Effect.gen(function* () {
|
||||
yield* Effect.annotateCurrentSpan({
|
||||
"orchestration.command_id": envelope.command.commandId,
|
||||
"orchestration.command_type": envelope.command.type,
|
||||
"orchestration.aggregate_kind": aggregateRef.aggregateKind,
|
||||
"orchestration.aggregate_id": aggregateRef.aggregateId,
|
||||
});
|
||||
|
||||
const existingReceipt = yield* commandReceiptRepository.getByCommandId({
|
||||
commandId: envelope.command.commandId,
|
||||
});
|
||||
if (Option.isSome(existingReceipt)) {
|
||||
if (existingReceipt.value.status === "accepted") {
|
||||
return {
|
||||
sequence: existingReceipt.value.resultSequence,
|
||||
};
|
||||
}
|
||||
return yield* new OrchestrationCommandPreviouslyRejectedError({
|
||||
commandId: envelope.command.commandId,
|
||||
detail: existingReceipt.value.error ?? "Previously rejected.",
|
||||
});
|
||||
}
|
||||
|
||||
const eventBase = yield* decideOrchestrationCommand({
|
||||
command: envelope.command,
|
||||
readModel: commandReadModel,
|
||||
});
|
||||
const eventBases = Array.isArray(eventBase) ? eventBase : [eventBase];
|
||||
const committedCommand = yield* sql
|
||||
.withTransaction(
|
||||
Effect.gen(function* () {
|
||||
const committedEvents: OrchestrationEvent[] = [];
|
||||
let nextCommandReadModel = commandReadModel;
|
||||
|
||||
for (const nextEvent of eventBases) {
|
||||
const savedEvent = yield* eventStore.append(nextEvent);
|
||||
nextCommandReadModel = yield* projectEvent(nextCommandReadModel, savedEvent);
|
||||
yield* projectionPipeline.projectEvent(savedEvent);
|
||||
committedEvents.push(savedEvent);
|
||||
}
|
||||
|
||||
const lastSavedEvent = committedEvents.at(-1) ?? null;
|
||||
if (lastSavedEvent === null) {
|
||||
return yield* new OrchestrationCommandInvariantError({
|
||||
commandType: envelope.command.type,
|
||||
detail: "Command produced no events.",
|
||||
});
|
||||
}
|
||||
|
||||
yield* commandReceiptRepository.upsert({
|
||||
commandId: envelope.command.commandId,
|
||||
aggregateKind: lastSavedEvent.aggregateKind,
|
||||
aggregateId: lastSavedEvent.aggregateId,
|
||||
acceptedAt: lastSavedEvent.occurredAt,
|
||||
resultSequence: lastSavedEvent.sequence,
|
||||
status: "accepted",
|
||||
error: null,
|
||||
});
|
||||
|
||||
return {
|
||||
committedEvents,
|
||||
lastSequence: lastSavedEvent.sequence,
|
||||
nextCommandReadModel,
|
||||
} as const;
|
||||
}),
|
||||
)
|
||||
.pipe(
|
||||
Effect.catchTag("SqlError", (sqlError) =>
|
||||
Effect.fail(
|
||||
toPersistenceSqlError("OrchestrationEngine.processEnvelope:transaction")(sqlError),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
commandReadModel = committedCommand.nextCommandReadModel;
|
||||
for (const [index, event] of committedCommand.committedEvents.entries()) {
|
||||
yield* PubSub.publish(eventPubSub, event);
|
||||
if (index === 0) {
|
||||
yield* Metric.update(
|
||||
Metric.withAttributes(
|
||||
orchestrationCommandAckDuration,
|
||||
metricAttributes({
|
||||
...baseMetricAttributes,
|
||||
ackEventType: event.type,
|
||||
}),
|
||||
),
|
||||
Duration.millis(Math.max(0, Date.now() - envelope.startedAtMs)),
|
||||
);
|
||||
}
|
||||
}
|
||||
return { sequence: committedCommand.lastSequence };
|
||||
}).pipe(Effect.withSpan(`orchestration.command.${envelope.command.type}`)),
|
||||
).pipe(
|
||||
Effect.flatMap((exit) =>
|
||||
return Effect.gen(function* () {
|
||||
const processingStartedAtMs = yield* currentEpochMillis;
|
||||
const exit = yield* Effect.exit(
|
||||
Effect.gen(function* () {
|
||||
const outcome = Exit.isSuccess(exit)
|
||||
? "success"
|
||||
: Cause.hasInterruptsOnly(exit.cause)
|
||||
? "interrupt"
|
||||
: "failure";
|
||||
yield* Metric.update(
|
||||
Metric.withAttributes(
|
||||
orchestrationCommandDuration,
|
||||
metricAttributes(baseMetricAttributes),
|
||||
),
|
||||
Duration.millis(Math.max(0, Date.now() - processingStartedAtMs)),
|
||||
);
|
||||
yield* Metric.update(
|
||||
Metric.withAttributes(
|
||||
orchestrationCommandsTotal,
|
||||
metricAttributes({
|
||||
...baseMetricAttributes,
|
||||
outcome,
|
||||
}),
|
||||
),
|
||||
1,
|
||||
);
|
||||
yield* Effect.annotateCurrentSpan({
|
||||
"orchestration.command_id": envelope.command.commandId,
|
||||
"orchestration.command_type": envelope.command.type,
|
||||
"orchestration.aggregate_kind": aggregateRef.aggregateKind,
|
||||
"orchestration.aggregate_id": aggregateRef.aggregateId,
|
||||
});
|
||||
|
||||
if (Exit.isSuccess(exit)) {
|
||||
yield* Deferred.succeed(envelope.result, exit.value);
|
||||
return;
|
||||
const existingReceipt = yield* commandReceiptRepository.getByCommandId({
|
||||
commandId: envelope.command.commandId,
|
||||
});
|
||||
if (Option.isSome(existingReceipt)) {
|
||||
if (existingReceipt.value.status === "accepted") {
|
||||
return {
|
||||
sequence: existingReceipt.value.resultSequence,
|
||||
};
|
||||
}
|
||||
return yield* new OrchestrationCommandPreviouslyRejectedError({
|
||||
commandId: envelope.command.commandId,
|
||||
detail: existingReceipt.value.error ?? "Previously rejected.",
|
||||
});
|
||||
}
|
||||
|
||||
const error = Cause.squash(exit.cause) as OrchestrationDispatchError;
|
||||
if (!Schema.is(OrchestrationCommandPreviouslyRejectedError)(error)) {
|
||||
yield* reconcileReadModelAfterDispatchFailure.pipe(
|
||||
Effect.catch(() =>
|
||||
Effect.logWarning(
|
||||
"failed to reconcile orchestration read model after dispatch failure",
|
||||
).pipe(
|
||||
Effect.annotateLogs({
|
||||
commandId: envelope.command.commandId,
|
||||
snapshotSequence: commandReadModel.snapshotSequence,
|
||||
}),
|
||||
const eventBase = yield* decideOrchestrationCommand({
|
||||
command: envelope.command,
|
||||
readModel: commandReadModel,
|
||||
});
|
||||
const eventBases = Array.isArray(eventBase) ? eventBase : [eventBase];
|
||||
const committedCommand = yield* sql
|
||||
.withTransaction(
|
||||
Effect.gen(function* () {
|
||||
const committedEvents: OrchestrationEvent[] = [];
|
||||
let nextCommandReadModel = commandReadModel;
|
||||
|
||||
for (const nextEvent of eventBases) {
|
||||
const savedEvent = yield* eventStore.append(nextEvent);
|
||||
nextCommandReadModel = yield* projectEvent(nextCommandReadModel, savedEvent);
|
||||
yield* projectionPipeline.projectEvent(savedEvent);
|
||||
committedEvents.push(savedEvent);
|
||||
}
|
||||
|
||||
const lastSavedEvent = committedEvents.at(-1) ?? null;
|
||||
if (lastSavedEvent === null) {
|
||||
return yield* new OrchestrationCommandInvariantError({
|
||||
commandType: envelope.command.type,
|
||||
detail: "Command produced no events.",
|
||||
});
|
||||
}
|
||||
|
||||
yield* commandReceiptRepository.upsert({
|
||||
commandId: envelope.command.commandId,
|
||||
aggregateKind: lastSavedEvent.aggregateKind,
|
||||
aggregateId: lastSavedEvent.aggregateId,
|
||||
acceptedAt: lastSavedEvent.occurredAt,
|
||||
resultSequence: lastSavedEvent.sequence,
|
||||
status: "accepted",
|
||||
error: null,
|
||||
});
|
||||
|
||||
return {
|
||||
committedEvents,
|
||||
lastSequence: lastSavedEvent.sequence,
|
||||
nextCommandReadModel,
|
||||
} as const;
|
||||
}),
|
||||
)
|
||||
.pipe(
|
||||
Effect.catchTag("SqlError", (sqlError) =>
|
||||
Effect.fail(
|
||||
toPersistenceSqlError("OrchestrationEngine.processEnvelope:transaction")(
|
||||
sqlError,
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
if (Schema.is(OrchestrationCommandInvariantError)(error)) {
|
||||
yield* commandReceiptRepository
|
||||
.upsert({
|
||||
commandId: envelope.command.commandId,
|
||||
aggregateKind: aggregateRef.aggregateKind,
|
||||
aggregateId: aggregateRef.aggregateId,
|
||||
acceptedAt: new Date().toISOString(),
|
||||
resultSequence: commandReadModel.snapshotSequence,
|
||||
status: "rejected",
|
||||
error: error.message,
|
||||
})
|
||||
.pipe(Effect.catch(() => Effect.void));
|
||||
commandReadModel = committedCommand.nextCommandReadModel;
|
||||
for (const [index, event] of committedCommand.committedEvents.entries()) {
|
||||
yield* PubSub.publish(eventPubSub, event);
|
||||
if (index === 0) {
|
||||
const acknowledgedAtMs = yield* currentEpochMillis;
|
||||
yield* Metric.update(
|
||||
Metric.withAttributes(
|
||||
orchestrationCommandAckDuration,
|
||||
metricAttributes({
|
||||
...baseMetricAttributes,
|
||||
ackEventType: event.type,
|
||||
}),
|
||||
),
|
||||
Duration.millis(Math.max(0, acknowledgedAtMs - envelope.startedAtMs)),
|
||||
);
|
||||
}
|
||||
}
|
||||
return { sequence: committedCommand.lastSequence };
|
||||
}).pipe(Effect.withSpan(`orchestration.command.${envelope.command.type}`)),
|
||||
);
|
||||
const outcome = Exit.isSuccess(exit)
|
||||
? "success"
|
||||
: Cause.hasInterruptsOnly(exit.cause)
|
||||
? "interrupt"
|
||||
: "failure";
|
||||
const processedAtMs = yield* currentEpochMillis;
|
||||
yield* Metric.update(
|
||||
Metric.withAttributes(orchestrationCommandDuration, metricAttributes(baseMetricAttributes)),
|
||||
Duration.millis(Math.max(0, processedAtMs - processingStartedAtMs)),
|
||||
);
|
||||
yield* Metric.update(
|
||||
Metric.withAttributes(
|
||||
orchestrationCommandsTotal,
|
||||
metricAttributes({
|
||||
...baseMetricAttributes,
|
||||
outcome,
|
||||
}),
|
||||
),
|
||||
1,
|
||||
);
|
||||
|
||||
yield* Deferred.fail(envelope.result, error);
|
||||
}),
|
||||
),
|
||||
);
|
||||
if (Exit.isSuccess(exit)) {
|
||||
yield* Deferred.succeed(envelope.result, exit.value);
|
||||
return;
|
||||
}
|
||||
|
||||
const error = Cause.squash(exit.cause) as OrchestrationDispatchError;
|
||||
if (!Schema.is(OrchestrationCommandPreviouslyRejectedError)(error)) {
|
||||
yield* reconcileReadModelAfterDispatchFailure.pipe(
|
||||
Effect.catch(() =>
|
||||
Effect.logWarning(
|
||||
"failed to reconcile orchestration read model after dispatch failure",
|
||||
).pipe(
|
||||
Effect.annotateLogs({
|
||||
commandId: envelope.command.commandId,
|
||||
snapshotSequence: commandReadModel.snapshotSequence,
|
||||
}),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
if (Schema.is(OrchestrationCommandInvariantError)(error)) {
|
||||
const rejectedAt = yield* currentIso;
|
||||
yield* commandReceiptRepository
|
||||
.upsert({
|
||||
commandId: envelope.command.commandId,
|
||||
aggregateKind: aggregateRef.aggregateKind,
|
||||
aggregateId: aggregateRef.aggregateId,
|
||||
acceptedAt: rejectedAt,
|
||||
resultSequence: commandReadModel.snapshotSequence,
|
||||
status: "rejected",
|
||||
error: error.message,
|
||||
})
|
||||
.pipe(Effect.catch(() => Effect.void));
|
||||
}
|
||||
}
|
||||
|
||||
yield* Deferred.fail(envelope.result, error);
|
||||
});
|
||||
};
|
||||
|
||||
yield* projectionPipeline.bootstrap;
|
||||
@@ -293,7 +296,8 @@ const makeOrchestrationEngine = Effect.gen(function* () {
|
||||
const dispatch: OrchestrationEngineShape["dispatch"] = (command) =>
|
||||
Effect.gen(function* () {
|
||||
const result = yield* Deferred.make<{ sequence: number }, OrchestrationDispatchError>();
|
||||
yield* Queue.offer(commandQueue, { command, result, startedAtMs: Date.now() });
|
||||
const startedAtMs = yield* currentEpochMillis;
|
||||
yield* Queue.offer(commandQueue, { command, result, startedAtMs });
|
||||
return yield* Deferred.await(result);
|
||||
});
|
||||
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
import { assert, it } from "@effect/vitest";
|
||||
import { CommandId, ProjectId, type OrchestrationEvent } from "@t3tools/contracts";
|
||||
import { Effect, Random } from "effect";
|
||||
|
||||
import { decideOrchestrationCommand } from "./decider.ts";
|
||||
import { createEmptyReadModel } from "./projector.ts";
|
||||
|
||||
const createdAt = "2026-05-08T16:00:00.000Z";
|
||||
type PlannedEvent = Omit<OrchestrationEvent, "sequence">;
|
||||
|
||||
const decideProjectCreatedEventId = decideOrchestrationCommand({
|
||||
command: {
|
||||
type: "project.create",
|
||||
commandId: CommandId.make("cmd-deterministic-event-id"),
|
||||
projectId: ProjectId.make("project-deterministic-event-id"),
|
||||
title: "Deterministic",
|
||||
workspaceRoot: "/tmp/deterministic",
|
||||
createdAt,
|
||||
},
|
||||
readModel: createEmptyReadModel(createdAt),
|
||||
}).pipe(
|
||||
Effect.map((event) => {
|
||||
const events = (Array.isArray(event) ? event : [event]) as ReadonlyArray<PlannedEvent>;
|
||||
const firstEvent = events[0];
|
||||
if (firstEvent) {
|
||||
return firstEvent.eventId;
|
||||
}
|
||||
throw new Error("project.create should emit one event");
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("uses Effect Random for deterministic event ids", () =>
|
||||
Effect.gen(function* () {
|
||||
const first = yield* decideProjectCreatedEventId.pipe(Random.withSeed("decider-seed"));
|
||||
const second = yield* decideProjectCreatedEventId.pipe(Random.withSeed("decider-seed"));
|
||||
|
||||
assert.equal(first, second);
|
||||
}),
|
||||
);
|
||||
@@ -1,9 +1,10 @@
|
||||
import type {
|
||||
OrchestrationCommand,
|
||||
OrchestrationEvent,
|
||||
OrchestrationReadModel,
|
||||
import {
|
||||
EventId,
|
||||
type OrchestrationCommand,
|
||||
type OrchestrationEvent,
|
||||
type OrchestrationReadModel,
|
||||
} from "@t3tools/contracts";
|
||||
import { DateTime, Effect } from "effect";
|
||||
import { DateTime, Effect, Random } from "effect";
|
||||
|
||||
import { OrchestrationCommandInvariantError } from "./Errors.ts";
|
||||
import {
|
||||
@@ -19,16 +20,17 @@ import { projectEvent } from "./projector.ts";
|
||||
|
||||
const currentIso = DateTime.now.pipe(Effect.map(DateTime.formatIso));
|
||||
|
||||
function withEventBase(
|
||||
const makeEventBase = Effect.fn("orchestration.makeEventBase")(function* (
|
||||
input: Pick<OrchestrationCommand, "commandId"> & {
|
||||
readonly aggregateKind: OrchestrationEvent["aggregateKind"];
|
||||
readonly aggregateId: OrchestrationEvent["aggregateId"];
|
||||
readonly occurredAt: string;
|
||||
readonly metadata?: OrchestrationEvent["metadata"];
|
||||
},
|
||||
): Omit<OrchestrationEvent, "sequence" | "type" | "payload"> {
|
||||
) {
|
||||
const eventId = yield* Random.nextUUIDv4;
|
||||
return {
|
||||
eventId: crypto.randomUUID() as OrchestrationEvent["eventId"],
|
||||
eventId: EventId.make(eventId),
|
||||
aggregateKind: input.aggregateKind,
|
||||
aggregateId: input.aggregateId,
|
||||
occurredAt: input.occurredAt,
|
||||
@@ -37,7 +39,7 @@ function withEventBase(
|
||||
correlationId: input.commandId,
|
||||
metadata: input.metadata ?? {},
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
type PlannedOrchestrationEvent = Omit<OrchestrationEvent, "sequence">;
|
||||
|
||||
@@ -91,12 +93,12 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand"
|
||||
});
|
||||
|
||||
return {
|
||||
...withEventBase({
|
||||
...(yield* makeEventBase({
|
||||
aggregateKind: "project",
|
||||
aggregateId: command.projectId,
|
||||
occurredAt: command.createdAt,
|
||||
commandId: command.commandId,
|
||||
}),
|
||||
})),
|
||||
type: "project.created",
|
||||
payload: {
|
||||
projectId: command.projectId,
|
||||
@@ -118,12 +120,12 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand"
|
||||
});
|
||||
const occurredAt = yield* currentIso;
|
||||
return {
|
||||
...withEventBase({
|
||||
...(yield* makeEventBase({
|
||||
aggregateKind: "project",
|
||||
aggregateId: command.projectId,
|
||||
occurredAt,
|
||||
commandId: command.commandId,
|
||||
}),
|
||||
})),
|
||||
type: "project.meta-updated",
|
||||
payload: {
|
||||
projectId: command.projectId,
|
||||
@@ -175,12 +177,12 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand"
|
||||
|
||||
const occurredAt = yield* currentIso;
|
||||
return {
|
||||
...withEventBase({
|
||||
...(yield* makeEventBase({
|
||||
aggregateKind: "project",
|
||||
aggregateId: command.projectId,
|
||||
occurredAt,
|
||||
commandId: command.commandId,
|
||||
}),
|
||||
})),
|
||||
type: "project.deleted" as const,
|
||||
payload: {
|
||||
projectId: command.projectId,
|
||||
@@ -201,12 +203,12 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand"
|
||||
threadId: command.threadId,
|
||||
});
|
||||
return {
|
||||
...withEventBase({
|
||||
...(yield* makeEventBase({
|
||||
aggregateKind: "thread",
|
||||
aggregateId: command.threadId,
|
||||
occurredAt: command.createdAt,
|
||||
commandId: command.commandId,
|
||||
}),
|
||||
})),
|
||||
type: "thread.created",
|
||||
payload: {
|
||||
threadId: command.threadId,
|
||||
@@ -231,12 +233,12 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand"
|
||||
});
|
||||
const occurredAt = yield* currentIso;
|
||||
return {
|
||||
...withEventBase({
|
||||
...(yield* makeEventBase({
|
||||
aggregateKind: "thread",
|
||||
aggregateId: command.threadId,
|
||||
occurredAt,
|
||||
commandId: command.commandId,
|
||||
}),
|
||||
})),
|
||||
type: "thread.deleted",
|
||||
payload: {
|
||||
threadId: command.threadId,
|
||||
@@ -253,12 +255,12 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand"
|
||||
});
|
||||
const occurredAt = yield* currentIso;
|
||||
return {
|
||||
...withEventBase({
|
||||
...(yield* makeEventBase({
|
||||
aggregateKind: "thread",
|
||||
aggregateId: command.threadId,
|
||||
occurredAt,
|
||||
commandId: command.commandId,
|
||||
}),
|
||||
})),
|
||||
type: "thread.archived",
|
||||
payload: {
|
||||
threadId: command.threadId,
|
||||
@@ -276,12 +278,12 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand"
|
||||
});
|
||||
const occurredAt = yield* currentIso;
|
||||
return {
|
||||
...withEventBase({
|
||||
...(yield* makeEventBase({
|
||||
aggregateKind: "thread",
|
||||
aggregateId: command.threadId,
|
||||
occurredAt,
|
||||
commandId: command.commandId,
|
||||
}),
|
||||
})),
|
||||
type: "thread.unarchived",
|
||||
payload: {
|
||||
threadId: command.threadId,
|
||||
@@ -298,12 +300,12 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand"
|
||||
});
|
||||
const occurredAt = yield* currentIso;
|
||||
return {
|
||||
...withEventBase({
|
||||
...(yield* makeEventBase({
|
||||
aggregateKind: "thread",
|
||||
aggregateId: command.threadId,
|
||||
occurredAt,
|
||||
commandId: command.commandId,
|
||||
}),
|
||||
})),
|
||||
type: "thread.meta-updated",
|
||||
payload: {
|
||||
threadId: command.threadId,
|
||||
@@ -326,12 +328,12 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand"
|
||||
});
|
||||
const occurredAt = yield* currentIso;
|
||||
return {
|
||||
...withEventBase({
|
||||
...(yield* makeEventBase({
|
||||
aggregateKind: "thread",
|
||||
aggregateId: command.threadId,
|
||||
occurredAt,
|
||||
commandId: command.commandId,
|
||||
}),
|
||||
})),
|
||||
type: "thread.runtime-mode-set",
|
||||
payload: {
|
||||
threadId: command.threadId,
|
||||
@@ -349,12 +351,12 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand"
|
||||
});
|
||||
const occurredAt = yield* currentIso;
|
||||
return {
|
||||
...withEventBase({
|
||||
...(yield* makeEventBase({
|
||||
aggregateKind: "thread",
|
||||
aggregateId: command.threadId,
|
||||
occurredAt,
|
||||
commandId: command.commandId,
|
||||
}),
|
||||
})),
|
||||
type: "thread.interaction-mode-set",
|
||||
payload: {
|
||||
threadId: command.threadId,
|
||||
@@ -395,12 +397,12 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand"
|
||||
});
|
||||
}
|
||||
const userMessageEvent: Omit<OrchestrationEvent, "sequence"> = {
|
||||
...withEventBase({
|
||||
...(yield* makeEventBase({
|
||||
aggregateKind: "thread",
|
||||
aggregateId: command.threadId,
|
||||
occurredAt: command.createdAt,
|
||||
commandId: command.commandId,
|
||||
}),
|
||||
})),
|
||||
type: "thread.message-sent",
|
||||
payload: {
|
||||
threadId: command.threadId,
|
||||
@@ -415,12 +417,12 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand"
|
||||
},
|
||||
};
|
||||
const turnStartRequestedEvent: Omit<OrchestrationEvent, "sequence"> = {
|
||||
...withEventBase({
|
||||
...(yield* makeEventBase({
|
||||
aggregateKind: "thread",
|
||||
aggregateId: command.threadId,
|
||||
occurredAt: command.createdAt,
|
||||
commandId: command.commandId,
|
||||
}),
|
||||
})),
|
||||
causationEventId: userMessageEvent.eventId,
|
||||
type: "thread.turn-start-requested",
|
||||
payload: {
|
||||
@@ -446,12 +448,12 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand"
|
||||
threadId: command.threadId,
|
||||
});
|
||||
return {
|
||||
...withEventBase({
|
||||
...(yield* makeEventBase({
|
||||
aggregateKind: "thread",
|
||||
aggregateId: command.threadId,
|
||||
occurredAt: command.createdAt,
|
||||
commandId: command.commandId,
|
||||
}),
|
||||
})),
|
||||
type: "thread.turn-interrupt-requested",
|
||||
payload: {
|
||||
threadId: command.threadId,
|
||||
@@ -468,7 +470,7 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand"
|
||||
threadId: command.threadId,
|
||||
});
|
||||
return {
|
||||
...withEventBase({
|
||||
...(yield* makeEventBase({
|
||||
aggregateKind: "thread",
|
||||
aggregateId: command.threadId,
|
||||
occurredAt: command.createdAt,
|
||||
@@ -476,7 +478,7 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand"
|
||||
metadata: {
|
||||
requestId: command.requestId,
|
||||
},
|
||||
}),
|
||||
})),
|
||||
type: "thread.approval-response-requested",
|
||||
payload: {
|
||||
threadId: command.threadId,
|
||||
@@ -494,7 +496,7 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand"
|
||||
threadId: command.threadId,
|
||||
});
|
||||
return {
|
||||
...withEventBase({
|
||||
...(yield* makeEventBase({
|
||||
aggregateKind: "thread",
|
||||
aggregateId: command.threadId,
|
||||
occurredAt: command.createdAt,
|
||||
@@ -502,7 +504,7 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand"
|
||||
metadata: {
|
||||
requestId: command.requestId,
|
||||
},
|
||||
}),
|
||||
})),
|
||||
type: "thread.user-input-response-requested",
|
||||
payload: {
|
||||
threadId: command.threadId,
|
||||
@@ -520,12 +522,12 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand"
|
||||
threadId: command.threadId,
|
||||
});
|
||||
return {
|
||||
...withEventBase({
|
||||
...(yield* makeEventBase({
|
||||
aggregateKind: "thread",
|
||||
aggregateId: command.threadId,
|
||||
occurredAt: command.createdAt,
|
||||
commandId: command.commandId,
|
||||
}),
|
||||
})),
|
||||
type: "thread.checkpoint-revert-requested",
|
||||
payload: {
|
||||
threadId: command.threadId,
|
||||
@@ -542,12 +544,12 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand"
|
||||
threadId: command.threadId,
|
||||
});
|
||||
return {
|
||||
...withEventBase({
|
||||
...(yield* makeEventBase({
|
||||
aggregateKind: "thread",
|
||||
aggregateId: command.threadId,
|
||||
occurredAt: command.createdAt,
|
||||
commandId: command.commandId,
|
||||
}),
|
||||
})),
|
||||
type: "thread.session-stop-requested",
|
||||
payload: {
|
||||
threadId: command.threadId,
|
||||
@@ -563,13 +565,13 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand"
|
||||
threadId: command.threadId,
|
||||
});
|
||||
return {
|
||||
...withEventBase({
|
||||
...(yield* makeEventBase({
|
||||
aggregateKind: "thread",
|
||||
aggregateId: command.threadId,
|
||||
occurredAt: command.createdAt,
|
||||
commandId: command.commandId,
|
||||
metadata: {},
|
||||
}),
|
||||
})),
|
||||
type: "thread.session-set",
|
||||
payload: {
|
||||
threadId: command.threadId,
|
||||
@@ -585,12 +587,12 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand"
|
||||
threadId: command.threadId,
|
||||
});
|
||||
return {
|
||||
...withEventBase({
|
||||
...(yield* makeEventBase({
|
||||
aggregateKind: "thread",
|
||||
aggregateId: command.threadId,
|
||||
occurredAt: command.createdAt,
|
||||
commandId: command.commandId,
|
||||
}),
|
||||
})),
|
||||
type: "thread.message-sent",
|
||||
payload: {
|
||||
threadId: command.threadId,
|
||||
@@ -612,12 +614,12 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand"
|
||||
threadId: command.threadId,
|
||||
});
|
||||
return {
|
||||
...withEventBase({
|
||||
...(yield* makeEventBase({
|
||||
aggregateKind: "thread",
|
||||
aggregateId: command.threadId,
|
||||
occurredAt: command.createdAt,
|
||||
commandId: command.commandId,
|
||||
}),
|
||||
})),
|
||||
type: "thread.message-sent",
|
||||
payload: {
|
||||
threadId: command.threadId,
|
||||
@@ -639,12 +641,12 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand"
|
||||
threadId: command.threadId,
|
||||
});
|
||||
return {
|
||||
...withEventBase({
|
||||
...(yield* makeEventBase({
|
||||
aggregateKind: "thread",
|
||||
aggregateId: command.threadId,
|
||||
occurredAt: command.createdAt,
|
||||
commandId: command.commandId,
|
||||
}),
|
||||
})),
|
||||
type: "thread.proposed-plan-upserted",
|
||||
payload: {
|
||||
threadId: command.threadId,
|
||||
@@ -660,12 +662,12 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand"
|
||||
threadId: command.threadId,
|
||||
});
|
||||
return {
|
||||
...withEventBase({
|
||||
...(yield* makeEventBase({
|
||||
aggregateKind: "thread",
|
||||
aggregateId: command.threadId,
|
||||
occurredAt: command.createdAt,
|
||||
commandId: command.commandId,
|
||||
}),
|
||||
})),
|
||||
type: "thread.turn-diff-completed",
|
||||
payload: {
|
||||
threadId: command.threadId,
|
||||
@@ -687,12 +689,12 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand"
|
||||
threadId: command.threadId,
|
||||
});
|
||||
return {
|
||||
...withEventBase({
|
||||
...(yield* makeEventBase({
|
||||
aggregateKind: "thread",
|
||||
aggregateId: command.threadId,
|
||||
occurredAt: command.createdAt,
|
||||
commandId: command.commandId,
|
||||
}),
|
||||
})),
|
||||
type: "thread.reverted",
|
||||
payload: {
|
||||
threadId: command.threadId,
|
||||
@@ -716,13 +718,13 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand"
|
||||
.requestId as OrchestrationEvent["metadata"]["requestId"])
|
||||
: undefined;
|
||||
return {
|
||||
...withEventBase({
|
||||
...(yield* makeEventBase({
|
||||
aggregateKind: "thread",
|
||||
aggregateId: command.threadId,
|
||||
occurredAt: command.createdAt,
|
||||
commandId: command.commandId,
|
||||
...(requestId !== undefined ? { metadata: { requestId } } : {}),
|
||||
}),
|
||||
})),
|
||||
type: "thread.activity-appended",
|
||||
payload: {
|
||||
threadId: command.threadId,
|
||||
|
||||
Reference in new issue
Block a user