+36
![github-actions[bot]](/assets/img/avatar_default.png)








Julius Marminge
maria-rcks
Bilal Bakr
shivam
Vitalii Yehorov
Jake Leventhal
Claude Opus 5
Alex Southwell
github-actions[bot]
Nicholas Wasmiller
PB
Exotic
Yash Singh
Devin
Theo Browne
Gabriel De Andrade
Dara Adedeji
scratchyone
Dominic Roy
chukfinley
Primož Ajdišek
benthecarman
NaveDanan
aaditagrawal
Aditya Garud
Nick Anisimov
MacKinley Smith
Yordis Prieto
t3-code[bot]
AKolenda
Guillermo Casanova
Otavio Salvador
Shirish Pothi
Ishaan Kothari
Bob Fowler
Anton Bezdenezhnykh
ValeraZSD
Ephraim
Ryan Ilano
Alex
maco
Cursor
Tristan Knight
PR Batch Tester
oliver
kamkm
de34391427
Co-authored-by: maria-rcks <[email protected]> Co-authored-by: Bilal Bakr <[email protected]> Co-authored-by: shivam <[email protected]> Co-authored-by: Vitalii Yehorov <[email protected]> Co-authored-by: Jake Leventhal <[email protected]> Co-authored-by: Claude Opus 5 (1M context) <[email protected]> Co-authored-by: Alex Southwell <[email protected]> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Nicholas Wasmiller <[email protected]> Co-authored-by: PB <[email protected]> Co-authored-by: Exotic <[email protected]> Co-authored-by: Yash Singh <[email protected]> Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: Theo Browne <[email protected]> Co-authored-by: Gabriel De Andrade <[email protected]> Co-authored-by: Dara Adedeji <[email protected]> Co-authored-by: scratchyone <[email protected]> Co-authored-by: Dominic Roy <[email protected]> Co-authored-by: chukfinley <[email protected]> Co-authored-by: Primož Ajdišek <[email protected]> Co-authored-by: benthecarman <[email protected]> Co-authored-by: NaveDanan <[email protected]> Co-authored-by: aaditagrawal <[email protected]> Co-authored-by: Aditya Garud <[email protected]> Co-authored-by: Nick Anisimov <[email protected]> Co-authored-by: MacKinley Smith <[email protected]> Co-authored-by: Yordis Prieto <[email protected]> Co-authored-by: t3-code[bot] <269035359+t3-code[bot]@users.noreply.github.com> Co-authored-by: AKolenda <[email protected]> Co-authored-by: Guillermo Casanova <[email protected]> Co-authored-by: Otavio Salvador <[email protected]> Co-authored-by: Shirish Pothi <[email protected]> Co-authored-by: Ishaan Kothari <[email protected]> Co-authored-by: Bob Fowler <[email protected]> Co-authored-by: Anton Bezdenezhnykh <[email protected]> Co-authored-by: ValeraZSD <[email protected]> Co-authored-by: Ephraim <[email protected]> Co-authored-by: Ryan Ilano <[email protected]> Co-authored-by: Alex <[email protected]> Co-authored-by: maco <[email protected]> Co-authored-by: Cursor <[email protected]> Co-authored-by: Tristan Knight <[email protected]> Co-authored-by: PR Batch Tester <[email protected]> Co-authored-by: oliver <[email protected]> Co-authored-by: kamkm <[email protected]> Signed-off-by: Yordis Prieto <[email protected]>
240 lines
9.2 KiB
TypeScript
240 lines
9.2 KiB
TypeScript
import { remapComposerContextAttachments } from "@t3tools/shared/composerContextReferences";
|
|
import { appendUserInputAttachmentPaths } from "../provider/userInputAttachments.ts";
|
|
import type { ChatAttachment, OrchestrationV2Command } from "@t3tools/contracts";
|
|
import * as Effect from "effect/Effect";
|
|
import * as Schema from "effect/Schema";
|
|
import { resolveAttachmentPath } from "../attachmentStore.ts";
|
|
import * as ServerConfig from "../config.ts";
|
|
import * as Orchestrator from "./Orchestrator.ts";
|
|
|
|
import * as AttachmentClaims from "./AttachmentClaims.ts";
|
|
import * as ThreadLaunch from "./ThreadLaunchService.ts";
|
|
import * as ThreadManagement from "./ThreadManagementService.ts";
|
|
|
|
// These dispatcher failures occur in receipt validation or planning, before
|
|
// commitCommand. Generic dispatch errors can follow a commit and remain uncertain.
|
|
function dispatchWasNotAccepted(
|
|
error: Orchestrator.OrchestratorV2Error | ThreadManagement.ThreadManagementError,
|
|
) {
|
|
switch (error._tag) {
|
|
case "OrchestratorCommandRejectedError":
|
|
case "OrchestratorProjectionError":
|
|
case "OrchestratorProviderAdapterError":
|
|
case "OrchestratorCommandPreviouslyRejectedError":
|
|
case "OrchestratorCommandIdConflictError":
|
|
case "OrchestratorSubagentThreadReadOnlyError":
|
|
return true;
|
|
default:
|
|
return false;
|
|
}
|
|
}
|
|
const isOrchestratorError = Schema.is(Orchestrator.OrchestratorV2Error);
|
|
|
|
const releaseUnusedClaims = Effect.fn("ThreadMessageIntake.releaseUnusedClaims")(function* (
|
|
claimedPaths: ReadonlyArray<string>,
|
|
accepted: ReadonlyArray<ChatAttachment>,
|
|
) {
|
|
if (claimedPaths.length === 0) return;
|
|
const config = yield* ServerConfig.ServerConfig;
|
|
const retained = new Set(
|
|
accepted.map((attachment) =>
|
|
resolveAttachmentPath({
|
|
attachmentsDir: config.attachmentsDir,
|
|
attachment,
|
|
}),
|
|
),
|
|
);
|
|
yield* AttachmentClaims.releaseClaimedAttachments(
|
|
claimedPaths.filter((path) => !retained.has(path)),
|
|
);
|
|
});
|
|
|
|
export const dispatchCommand = Effect.fn("ThreadMessageIntake.dispatchCommand")(function* (
|
|
command: OrchestrationV2Command,
|
|
) {
|
|
const threads = yield* ThreadManagement.ThreadManagementService;
|
|
if (command.type === "runtime-request.respond" && command.attachmentsByQuestionId) {
|
|
const config = yield* ServerConfig.ServerConfig;
|
|
const incomingByQuestionId = command.attachmentsByQuestionId;
|
|
yield* AttachmentClaims.validateAttachmentLimits(Object.values(incomingByQuestionId).flat());
|
|
// Claims accumulate across questions, so all of preparation shares one
|
|
// rollback boundary: any failure before dispatch removes every new copy.
|
|
const claimedPaths: string[] = [];
|
|
const prepared = yield* Effect.gen(function* () {
|
|
const attachmentsByQuestionId: import("@t3tools/contracts").UserInputAttachments = {};
|
|
for (const [questionId, attachments] of Object.entries(incomingByQuestionId)) {
|
|
const claimed = yield* AttachmentClaims.claimPendingAttachments({
|
|
threadId: command.threadId,
|
|
attachments,
|
|
});
|
|
claimedPaths.push(...claimed.claimedPaths);
|
|
Object.defineProperty(attachmentsByQuestionId, questionId, {
|
|
value: claimed.attachments,
|
|
enumerable: true,
|
|
});
|
|
}
|
|
const answers = yield* appendUserInputAttachmentPaths({
|
|
answers: command.answers ?? {},
|
|
attachmentsByQuestionId,
|
|
attachmentsDir: config.attachmentsDir,
|
|
}).pipe(
|
|
Effect.mapError(
|
|
(cause) => new AttachmentClaims.AttachmentClaimError({ message: cause.issue }),
|
|
),
|
|
);
|
|
return { answers, attachmentsByQuestionId };
|
|
}).pipe(Effect.onError(() => AttachmentClaims.releaseClaimedAttachments(claimedPaths)));
|
|
return yield* threads
|
|
.dispatch({
|
|
...command,
|
|
answers: prepared.answers,
|
|
attachmentsByQuestionId: prepared.attachmentsByQuestionId,
|
|
})
|
|
.pipe(
|
|
Effect.tap((result) => {
|
|
// A replayed receipt reports the first attempt's answer, so this
|
|
// attempt's copies go unreferenced and are released. The resolved
|
|
// turn item proves the respond was applied; questionAnswer is only
|
|
// recorded when the accepted command carried attachments, so its
|
|
// absence means the accepted answer referenced no copies. With no
|
|
// resolved item at all the outcome is ambiguous and everything stays.
|
|
const answeredItems = result.storedEvents.flatMap(({ event }) =>
|
|
event.type === "turn-item.updated" &&
|
|
event.payload.type === "user_input_request" &&
|
|
event.payload.requestId === command.requestId
|
|
? [event.payload]
|
|
: [],
|
|
);
|
|
return answeredItems.length > 0
|
|
? releaseUnusedClaims(
|
|
claimedPaths,
|
|
answeredItems.flatMap((item) =>
|
|
item.questionAnswer === undefined
|
|
? []
|
|
: Object.values(item.questionAnswer.attachmentsByQuestionId).flat(),
|
|
),
|
|
)
|
|
: Effect.void;
|
|
}),
|
|
Effect.tapError((error) =>
|
|
dispatchWasNotAccepted(error)
|
|
? AttachmentClaims.releaseClaimedAttachments(claimedPaths)
|
|
: Effect.void,
|
|
),
|
|
);
|
|
}
|
|
if (
|
|
command.type !== "message.dispatch" &&
|
|
(command.type !== "queued-run.edit" || command.attachments === undefined)
|
|
)
|
|
return yield* threads.dispatch(command);
|
|
const claimed = yield* AttachmentClaims.claimPendingAttachments({
|
|
threadId: command.threadId,
|
|
attachments: command.attachments ?? [],
|
|
});
|
|
return yield* threads
|
|
.dispatch({
|
|
...command,
|
|
attachments: claimed.attachments,
|
|
...(command.context
|
|
? {
|
|
context: remapComposerContextAttachments(
|
|
command.context,
|
|
command.attachments ?? [],
|
|
claimed.attachments,
|
|
),
|
|
}
|
|
: {}),
|
|
})
|
|
.pipe(
|
|
Effect.tap((result) =>
|
|
releaseUnusedClaims(
|
|
claimed.claimedPaths,
|
|
result.storedEvents.flatMap(({ event }) =>
|
|
event.type === "message.updated" ? event.payload.attachments : [],
|
|
),
|
|
),
|
|
),
|
|
Effect.tapError((error) =>
|
|
dispatchWasNotAccepted(error)
|
|
? AttachmentClaims.releaseClaimedAttachments(claimed.claimedPaths)
|
|
: Effect.void,
|
|
),
|
|
);
|
|
});
|
|
|
|
export const sendToThread = Effect.fn("ThreadMessageIntake.sendToThread")(function* (
|
|
input: ThreadManagement.ThreadManagementSendInput,
|
|
) {
|
|
const threads = yield* ThreadManagement.ThreadManagementService;
|
|
const claimed = yield* AttachmentClaims.claimPendingAttachments(input);
|
|
return yield* threads.sendToThread({ ...input, attachments: claimed.attachments }).pipe(
|
|
Effect.tap((result) => releaseUnusedClaims(claimed.claimedPaths, result.message.attachments)),
|
|
Effect.tapError((error) =>
|
|
dispatchWasNotAccepted(error)
|
|
? AttachmentClaims.releaseClaimedAttachments(claimed.claimedPaths)
|
|
: Effect.void,
|
|
),
|
|
);
|
|
});
|
|
|
|
export const launchThread = Effect.fn("ThreadMessageIntake.launchThread")(function* (
|
|
input: ThreadLaunch.ThreadLaunchInput,
|
|
) {
|
|
const launches = yield* ThreadLaunch.ThreadLaunchService;
|
|
yield* AttachmentClaims.validateAttachmentLimits(input.initialMessage?.attachments ?? []);
|
|
if (!input.initialMessage?.attachments.some(AttachmentClaims.attachmentIsPendingUpload)) {
|
|
return yield* launches.launch(input);
|
|
}
|
|
if (input.threadId === undefined) {
|
|
return yield* new AttachmentClaims.AttachmentClaimError({
|
|
message: "Uploaded attachments need a thread id at launch.",
|
|
});
|
|
}
|
|
const claimed = yield* AttachmentClaims.claimPendingAttachments({
|
|
threadId: input.threadId,
|
|
attachments: input.initialMessage.attachments,
|
|
});
|
|
return yield* launches
|
|
.launch({
|
|
...input,
|
|
initialMessage: {
|
|
...input.initialMessage,
|
|
attachments: claimed.attachments,
|
|
...(input.initialMessage.context
|
|
? {
|
|
context: remapComposerContextAttachments(
|
|
input.initialMessage.context,
|
|
input.initialMessage.attachments,
|
|
claimed.attachments,
|
|
),
|
|
}
|
|
: {}),
|
|
},
|
|
})
|
|
.pipe(
|
|
Effect.tap((result) =>
|
|
releaseUnusedClaims(
|
|
claimed.claimedPaths,
|
|
result.projection.messages.flatMap((message) => message.attachments),
|
|
),
|
|
),
|
|
Effect.tapError((error) => {
|
|
// Project/receipt reads precede message dispatch. The create-thread error
|
|
// also wraps post-message projection reads, so its tag alone is not proof.
|
|
const notAccepted =
|
|
error.operation === "resolve-project" ||
|
|
error.operation === "read-receipt" ||
|
|
((error.operation === "create-thread" || error.operation === "dispatch-message") &&
|
|
isOrchestratorError(error.cause) &&
|
|
// Projection errors under create-thread can occur after the message commit.
|
|
(error.operation !== "create-thread" ||
|
|
error.cause._tag !== "OrchestratorProjectionError") &&
|
|
dispatchWasNotAccepted(error.cause));
|
|
return notAccepted
|
|
? AttachmentClaims.releaseClaimedAttachments(claimed.claimedPaths)
|
|
: Effect.void;
|
|
}),
|
|
);
|
|
});
|