feat: upload composer images on attach

Attachments used to ride the turn-start command as base64 data URLs:
send carried the bytes, the stash re-encoded them into localStorage,
and a 10MB image meant a 14M-char string in a single ws frame.

Images now upload the moment they are attached. A ws RPC mints a
pending-<uuid> id plus a signed, expiring upload URL (mirroring signed
asset GETs, so it works against any environment); the browser POSTs the
compressed bytes to it with progress and abort; the turn-start command
carries id references only. The Normalizer renames pending files to
their thread segment at send, resolving by uuid so retries after a
partial send are idempotent. Never-sent uploads are deleted on chip
removal and swept after 30 days.

Breaking: the dataUrl upload variant is deleted with no compatibility
path. RN mobile compiles with image attach gated off behind
IMAGE_ATTACH_ENABLED and tells the user an update is needed; drafts and
stash persist id references (v9/v3 storage, old payloads purged).

Co-Authored-By: Claude Fable 5 <[email protected]>
This commit is contained in:
Theo BrowneandClaude Fable 5 committed 2026-08-13 17:51:12 -07:00
1 parent 5304f3e9d4
commit f695b6f5bf
38 files changed
+2266 -1281

No files matched your search

+69 -2
View File
@@ -1,7 +1,7 @@
import * as Schema from "effect/Schema";
import { ThreadId, TrimmedNonEmptyString } from "./baseSchemas.ts";
import { ProjectFaviconPath } from "./orchestration.ts";
import { NonNegativeInt, ThreadId, TrimmedNonEmptyString } from "./baseSchemas.ts";
import { PROVIDER_SEND_TURN_MAX_IMAGE_BYTES, ProjectFaviconPath } from "./orchestration.ts";
const ASSET_PATH_MAX_LENGTH = 1024;
@@ -36,6 +36,73 @@ export const AssetCreateUrlResult = Schema.Struct({
});
export type AssetCreateUrlResult = typeof AssetCreateUrlResult.Type;
// ── Attachment upload (upload-on-attach) ────────────────────────────────
//
// Attachments upload the moment they are added to the composer, before any
// thread exists. The client asks for a signed upload URL over ws (which
// carries auth), then PUTs raw bytes to it over HTTP. The returned id is
// `pending-<uuid>`; the turn-start Normalizer re-scopes it to the thread.
/** How long a minted upload URL stays valid. */
export const ATTACHMENT_UPLOAD_URL_TTL_MS = 10 * 60_000;
const AttachmentIdSchema = TrimmedNonEmptyString.check(Schema.isMaxLength(256));
export const AttachmentCreateUploadUrlInput = Schema.Struct({
name: TrimmedNonEmptyString.check(Schema.isMaxLength(255)),
mimeType: TrimmedNonEmptyString.check(Schema.isMaxLength(100), Schema.isPattern(/^image\//i)),
/**
* The exact byte length the client will upload. The upload route rejects a
* body that does not match, so a truncated or padded transfer can never
* become a "ready" attachment.
*/
sizeBytes: NonNegativeInt.check(
Schema.isGreaterThanOrEqualTo(1),
Schema.isLessThanOrEqualTo(PROVIDER_SEND_TURN_MAX_IMAGE_BYTES),
),
});
export type AttachmentCreateUploadUrlInput = typeof AttachmentCreateUploadUrlInput.Type;
export const AttachmentCreateUploadUrlResult = Schema.Struct({
attachmentId: AttachmentIdSchema,
relativeUrl: TrimmedNonEmptyString.check(Schema.isMaxLength(4096)),
expiresAt: Schema.Number,
});
export type AttachmentCreateUploadUrlResult = typeof AttachmentCreateUploadUrlResult.Type;
export const AttachmentDeleteInput = Schema.Struct({
attachmentId: AttachmentIdSchema,
});
export type AttachmentDeleteInput = typeof AttachmentDeleteInput.Type;
export class AttachmentUploadRequestError extends Schema.TaggedErrorClass<AttachmentUploadRequestError>()(
"AttachmentUploadRequestError",
{
detail: Schema.String,
},
) {
override get message(): string {
return this.detail;
}
}
export class AttachmentUploadSigningKeyError extends Schema.TaggedErrorClass<AttachmentUploadSigningKeyError>()(
"AttachmentUploadSigningKeyError",
{
cause: Schema.Defect(),
},
) {
override get message(): string {
return "Failed to load the attachment upload signing key.";
}
}
export const AttachmentUploadError = Schema.Union([
AttachmentUploadRequestError,
AttachmentUploadSigningKeyError,
]);
export type AttachmentUploadError = typeof AttachmentUploadError.Type;
export class AssetWorkspaceContextNotFoundError extends Schema.TaggedErrorClass<AssetWorkspaceContextNotFoundError>()(
"AssetWorkspaceContextNotFoundError",
{
+4 -15
View File
@@ -144,7 +144,6 @@ export type ProviderUserInputAnswers = typeof ProviderUserInputAnswers.Type;
export const PROVIDER_SEND_TURN_MAX_INPUT_CHARS = 120_000;
export const PROVIDER_SEND_TURN_MAX_ATTACHMENTS = 8;
export const PROVIDER_SEND_TURN_MAX_IMAGE_BYTES = 10 * 1024 * 1024;
const PROVIDER_SEND_TURN_MAX_IMAGE_DATA_URL_CHARS = 14_000_000;
const CHAT_ATTACHMENT_ID_MAX_CHARS = 128;
// Correlation id is command id by design in this model.
export const CorrelationId = CommandId;
@@ -165,21 +164,8 @@ export const ChatImageAttachment = Schema.Struct({
});
export type ChatImageAttachment = typeof ChatImageAttachment.Type;
const UploadChatImageAttachment = Schema.Struct({
type: Schema.Literal("image"),
name: TrimmedNonEmptyString.check(Schema.isMaxLength(255)),
mimeType: TrimmedNonEmptyString.check(Schema.isMaxLength(100), Schema.isPattern(/^image\//i)),
sizeBytes: NonNegativeInt.check(Schema.isLessThanOrEqualTo(PROVIDER_SEND_TURN_MAX_IMAGE_BYTES)),
dataUrl: TrimmedNonEmptyString.check(
Schema.isMaxLength(PROVIDER_SEND_TURN_MAX_IMAGE_DATA_URL_CHARS),
),
});
export type UploadChatImageAttachment = typeof UploadChatImageAttachment.Type;
export const ChatAttachment = Schema.Union([ChatImageAttachment]);
export type ChatAttachment = typeof ChatAttachment.Type;
const UploadChatAttachment = Schema.Union([UploadChatImageAttachment]);
export type UploadChatAttachment = typeof UploadChatAttachment.Type;
export const ProjectScriptIcon = Schema.Literals([
"play",
@@ -837,7 +823,10 @@ const ClientThreadTurnStartCommand = Schema.Struct({
messageId: MessageId,
role: Schema.Literal("user"),
text: Schema.String,
attachments: Schema.Array(UploadChatAttachment),
// Id references to attachments already uploaded via the signed upload
// URL flow (`attachments.createUploadUrl` + HTTP PUT). Bytes never ride
// this command; the Normalizer re-scopes pending ids to the thread.
attachments: Schema.Array(ChatAttachment),
}),
modelSelection: Schema.optional(ModelSelection),
titleSeed: Schema.optional(TrimmedNonEmptyString),
+31 -1
View File
@@ -18,7 +18,16 @@ import {
FilesystemBrowseResult,
FilesystemBrowseError,
} from "./filesystem.ts";
import { AssetAccessError, AssetCreateUrlInput, AssetCreateUrlResult } from "./assets.ts";
import {
AssetAccessError,
AssetCreateUrlInput,
AssetCreateUrlResult,
AttachmentCreateUploadUrlInput,
AttachmentCreateUploadUrlResult,
AttachmentDeleteInput,
AttachmentUploadRequestError,
AttachmentUploadSigningKeyError,
} from "./assets.ts";
import {
GitActionProgressEvent,
VcsSwitchRefInput,
@@ -208,6 +217,10 @@ export const WS_METHODS = {
filesystemBrowse: "filesystem.browse",
assetsCreateUrl: "assets.createUrl",
// Attachment upload methods
attachmentsCreateUploadUrl: "attachments.createUploadUrl",
attachmentsDelete: "attachments.delete",
// VCS methods
vcsPull: "vcs.pull",
vcsRefreshStatus: "vcs.refreshStatus",
@@ -655,6 +668,21 @@ export const WsAssetsCreateUrlRpc = Rpc.make(WS_METHODS.assetsCreateUrl, {
error: Schema.Union([AssetAccessError, EnvironmentAuthorizationError]),
});
export const WsAttachmentsCreateUploadUrlRpc = Rpc.make(WS_METHODS.attachmentsCreateUploadUrl, {
payload: AttachmentCreateUploadUrlInput,
success: AttachmentCreateUploadUrlResult,
error: Schema.Union([
AttachmentUploadRequestError,
AttachmentUploadSigningKeyError,
EnvironmentAuthorizationError,
]),
});
export const WsAttachmentsDeleteRpc = Rpc.make(WS_METHODS.attachmentsDelete, {
payload: AttachmentDeleteInput,
error: Schema.Union([AttachmentUploadRequestError, EnvironmentAuthorizationError]),
});
export const WsSubscribeVcsStatusRpc = Rpc.make(WS_METHODS.subscribeVcsStatus, {
payload: VcsStatusInput,
success: VcsStatusStreamEvent,
@@ -1021,6 +1049,8 @@ export const WsRpcGroup = RpcGroup.make(
WsShellOpenInEditorRpc,
WsFilesystemBrowseRpc,
WsAssetsCreateUrlRpc,
WsAttachmentsCreateUploadUrlRpc,
WsAttachmentsDeleteRpc,
WsSubscribeVcsStatusRpc,
WsVcsPullRpc,
WsVcsRefreshStatusRpc,