import { requireNativeModule } from "expo";
import {
type ComposerContextClipboardFragment,
type ComposerContextRecord,
PROVIDER_SEND_TURN_MAX_ATTACHMENTS,
PROVIDER_SEND_TURN_MAX_FILE_BYTES,
PROVIDER_SEND_TURN_MAX_IMAGE_BYTES,
COMPOSER_CONTEXT_MAX_RECORDS,
type EnvironmentId,
} from "@t3tools/contracts";
import {
decodeComposerContextClipboardHtml,
decodeComposerContextFragment,
encodeComposerContextFragment,
} from "@t3tools/shared/composerContextClipboard";
import {
threadContextsOutsideEnvironment,
threadLinkContextReferences,
} from "@t3tools/shared/composerContextReferences";
import { executeAtomQuery, squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime";
import { resolveAssetUrl } from "@t3tools/client-runtime/state/assets";
import * as Option from "effect/Option";
import { appAtomRegistry } from "../state/atom-registry";
import { assetEnvironment } from "../state/assets";
import { environmentSession } from "../state/session";
import { environmentThreadShells } from "../state/threads";
import { workspaceConnections } from "../state/workspace";
import { downloadAttachmentForPreview } from "./attachmentDownload";
import {
persistComposerAttachmentFile,
removePersistedComposerAttachmentFile,
type DraftComposerAttachment,
} from "./composerImages";
import { referencedComposerContext, reidentifyComposerContext } from "./composerContext";
import { uuidv4 } from "./uuid";
import {
findLocalComposerClipboardAttachment,
waitForComposerDraftsLoaded,
} from "../state/use-composer-drafts";
import { loadLocalAttachmentPreview } from "./localAttachmentPreview";
export interface NativeContextClipboard {
readonly text: string;
readonly fragment: string;
readonly html: string;
}
function checkAborted(signal: AbortSignal): void {
if (signal.aborted) throw new Error("Context import cancelled");
}
const nativeClipboard = () =>
requireNativeModule<{
writeContextClipboard: (text: string, fragment: string) => Promise<void>;
}>("T3ComposerEditor");
export function writeComposerContextClipboard(
text: string,
fragment: ComposerContextClipboardFragment,
): Promise<void> {
const encoded = encodeComposerContextFragment(fragment);
if (!encoded)
return Promise.reject(
new Error("This context selection is too large to copy. Select fewer items."),
);
return nativeClipboard().writeContextClipboard(text, encoded);
}
export async function importComposerContextClipboard(
input: NativeContextClipboard,
existingCount: number,
signal: AbortSignal,
existingContextCount = 0,
destinationEnvironmentId?: EnvironmentId,
/** The machines the destination links to, whose threads its agent can read too. */
linkedEnvironments: ReadonlyMap<EnvironmentId, string> = new Map(),
) {
const copied =
decodeComposerContextFragment(input.fragment) ?? decodeComposerContextClipboardHtml(input.html);
// A thread link becomes a chip only where this agent can read its machine; others stay links.
const threads = threadLinkContextReferences(
input.text,
copied?.records ?? [],
(ref) => appAtomRegistry.get(environmentThreadShells.threadShellAtom(ref))?.title,
(record) =>
destinationEnvironmentId === undefined ||
record.environmentId === destinationEnvironmentId ||
linkedEnvironments.has(record.environmentId)
? record.contextId
: undefined,
);
const source =
copied?.source ??
(threads.records[0] ? { environmentId: threads.records[0].environmentId } : null);
if (!source) return null;
const fragment = {
source,
records: [
...(copied?.records ?? []).filter((record) =>
threads.records.every((thread) => thread.contextId !== record.contextId),
),
...threads.records,
],
};
const selected = referencedComposerContext(threads.text, {
version: 1,
records: fragment.records,
});
const imported = reidentifyComposerContext(threads.text, selected?.records ?? [], uuidv4);
const skippedThreads =
destinationEnvironmentId === undefined
? []
: threadContextsOutsideEnvironment({
text: imported.text,
records: imported.context.records,
environmentId: destinationEnvironmentId,
linkedEnvironments,
});
const skippedIds = new Set(skippedThreads.map((record) => record.contextId));
const retained = imported.context.records.filter((record) => !skippedIds.has(record.contextId));
if (existingContextCount + retained.length > COMPOSER_CONTEXT_MAX_RECORDS)
throw new Error("Remove some context items from the draft before pasting more.");
const attachments: DraftComposerAttachment[] = [];
const records: ComposerContextRecord[] = [];
const failures: string[] = [];
try {
for (const record of retained) {
checkAborted(signal);
if (!("attachmentId" in record)) {
records.push(record);
continue;
}
try {
if (existingCount + attachments.length >= PROVIDER_SEND_TURN_MAX_ATTACHMENTS)
throw new Error("Attachment limit reached");
const file = await importAttachment(record, fragment.source.environmentId, signal);
attachments.push(file);
records.push({ ...record, attachmentId: file.id });
} catch (error) {
if (signal.aborted) throw error;
failures.push(record.name);
}
}
// The last copy can be cancelled after its own check; a cancelled import must not return files nobody will release.
checkAborted(signal);
return {
text: imported.text,
context: { version: 1 as const, records },
attachments,
failures,
skippedThreads: skippedThreads.map((record) => record.title),
};
} catch (error) {
await Promise.all(
attachments.map((attachment) =>
attachment.fileUri
? removePersistedComposerAttachmentFile(attachment.fileUri)
: Promise.resolve(),
),
);
throw error;
}
}
async function importAttachment(
record: Extract<ComposerContextRecord, { attachmentId: string }>,
environmentId: EnvironmentId,
signal: AbortSignal,
): Promise<DraftComposerAttachment> {
await waitForComposerDraftsLoaded();
checkAborted(signal);
const local = findLocalComposerClipboardAttachment(
appAtomRegistry
.get(workspaceConnections.environmentsAtom)
.map((environment) => environment.environmentId),
environmentId,
record.attachmentId,
);
if (local) {
if (!local.fileUri && local.type === "image" && local.dataUrl)
return {
...local,
id: uuidv4(),
uploadedAttachmentId: undefined,
uploadEnvironmentId: undefined,
};
if (local.fileUri) {
const preview = await loadLocalAttachmentPreview(
{ ...local, fileUri: local.fileUri },
signal,
);
if (!preview) throw new Error("Context import cancelled");
try {
return await persistImportedAttachment(record, preview.uri, signal);
} finally {
preview.dispose();
}
}
}
const connection = appAtomRegistry.get(
environmentSession.preparedConnectionValueAtom(environmentId),
);
if (Option.isNone(connection)) throw new Error("Reconnect to the source environment");
const result = await executeAtomQuery(
appAtomRegistry,
assetEnvironment.createUrl({
environmentId,
input: {
resource: { _tag: "attachment", attachmentId: record.attachmentId, fileName: record.name },
},
}),
{ refresh: true, reportFailure: false },
);
if (result._tag === "Failure") throw squashAtomCommandFailure(result);
checkAborted(signal);
const url = resolveAssetUrl(connection.value.httpBaseUrl, result.value.relativeUrl);
if (!url) throw new Error("Attachment URL unavailable");
const temporary = await downloadAttachmentForPreview({
attachment: { name: record.name, mimeType: record.mimeType },
url,
signal,
});
if (!temporary) throw new Error("Attachment import cancelled");
try {
return await persistImportedAttachment(record, temporary.uri, signal);
} finally {
temporary.dispose();
}
}
async function persistImportedAttachment(
record: Extract<ComposerContextRecord, { attachmentId: string }>,
uri: string,
signal: AbortSignal,
): Promise<DraftComposerAttachment> {
const fileUri = await persistComposerAttachmentFile(
uri,
record.name,
record.kind === "image"
? PROVIDER_SEND_TURN_MAX_IMAGE_BYTES
: PROVIDER_SEND_TURN_MAX_FILE_BYTES,
);
if (signal.aborted) {
await removePersistedComposerAttachmentFile(fileUri);
checkAborted(signal);
}
const { File } = await import("expo-file-system");
const common = {
id: uuidv4(),
fileUri,
name: record.name,
mimeType: record.mimeType,
sizeBytes: new File(fileUri).size,
};
return record.kind === "image"
? { ...common, type: "image", previewUri: fileUri }
: { ...common, type: "file" };
}