Repository navigation
refactor(server): share attachment message intake #10580
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
juliusmarminge
merged 3 commits into
agents/mcp-shared/project-inputs
from
agents/mcp-shared/attachment-intake
Sep 7, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 134 additions & 0 deletions
134
apps/server/src/orchestration-v2/ThreadMessageIntake.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| 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 "OrchestratorProjectionError": | ||
| case "OrchestratorProviderAdapterError": | ||
| case "OrchestratorCommandPreviouslyRejectedError": | ||
| case "OrchestratorCommandIdConflictError": | ||
| return true; | ||
| default: | ||
| return false; | ||
| } | ||
| } | ||
| const isOrchestratorError = Schema.is(Orchestrator.OrchestratorV2Error); | ||
|
|
||
| const releaseUnusedClaims = Effect.fn("ThreadMessageIntake.releaseUnusedClaims")(function* ( | ||
| claimed: AttachmentClaims.ClaimedAttachments, | ||
| accepted: ReadonlyArray<ChatAttachment>, | ||
| ) { | ||
| if (claimed.claimedPaths.length === 0) return; | ||
| const config = yield* ServerConfig.ServerConfig; | ||
| const retained = new Set( | ||
| accepted.map((attachment) => | ||
| resolveAttachmentPath({ | ||
| attachmentsDir: config.attachmentsDir, | ||
| attachment, | ||
| }), | ||
| ), | ||
| ); | ||
| yield* AttachmentClaims.releaseClaimedAttachments( | ||
| claimed.claimedPaths.filter((path) => !retained.has(path)), | ||
| ); | ||
| }); | ||
|
|
||
| export const dispatchCommand = Effect.fn("ThreadMessageIntake.dispatchCommand")(function* ( | ||
| command: OrchestrationV2Command, | ||
| ) { | ||
| const threads = yield* ThreadManagement.ThreadManagementService; | ||
| if (command.type !== "message.dispatch") return yield* threads.dispatch(command); | ||
| const claimed = yield* AttachmentClaims.claimPendingAttachments({ | ||
| threadId: command.threadId, | ||
| attachments: command.attachments, | ||
| }); | ||
| return yield* threads.dispatch({ ...command, attachments: claimed.attachments }).pipe( | ||
| Effect.tap((result) => | ||
| releaseUnusedClaims( | ||
| claimed, | ||
| 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, 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; | ||
| 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 }, | ||
| }) | ||
| .pipe( | ||
| Effect.tap((result) => | ||
| releaseUnusedClaims( | ||
| claimed, | ||
| 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; | ||
| }), | ||
| ); | ||
| }); | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.