Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
394 changes: 348 additions & 46 deletions src/agent.rs

Large diffs are not rendered by default.

34 changes: 25 additions & 9 deletions ts/agent_attachments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,20 @@

import { readOwn } from "./angular";
import { cleanMediaId, findMediaContainer, isTrashedTile, legacyMedia } from "./dom";
import { uploadMediaFields } from "./frontend_builds";
import { frontendBuild, readBuildLabel, uploadMediaFields } from "./frontend_builds";
import { agentAttachmentCompatibility } from "./frontend_compatibility";
import { bindMediaUpload, uploadMediaFiles, waitForMedia } from "./project_upload";
import type { MediaUploadExport, UploadProgress } from "./project_upload";
import type { FlowMediaPayload, FlowTile } from "./types";

/** Tile that can supply a reference in the active Flow project. */
interface AttachmentTile extends FlowTile {
export interface AttachmentTile extends FlowTile {
/** Asset ID shown in the project. */
id: string;
/** ID of the primary image or video. */
primaryMediaId?: string;
/** Edit and generation media variants stored on the tile. */
history?: FlowMediaPayload[];
/** Upsampled video media variants stored on the tile. */
yn?: FlowMediaPayload[];
/** Type of asset. */
type: string;
}
Expand Down Expand Up @@ -89,7 +87,7 @@ function isMediaTile(tile: AttachmentTile): boolean {
}

/** Returns true when a media variant payload has finished generating or uploading. */
function isReadyVariant(variant: FlowMediaPayload | undefined): boolean {
export function isReadyVariant(variant: FlowMediaPayload | undefined): boolean {
if (!variant || typeof variant !== "object") return true;
const source = readOwn(variant, "source");
const uploaded = readOwn(variant, uploadMediaFields().uploaded);
Expand All @@ -100,7 +98,7 @@ function isReadyVariant(variant: FlowMediaPayload | undefined): boolean {
return status === undefined || status === "success";
}

/** Finds the media variant matching `mediaId` in `history`, `yn`, or the primary container. */
/** Finds the media variant matching `mediaId` in `history`, upsampled variants, or the primary container. */
function findVariantByMediaId(
tile: AttachmentTile,
mediaId: string,
Expand All @@ -112,15 +110,18 @@ function findVariantByMediaId(
? tile.history.find((item) => cleanMediaId(readOwn(item, "id")) === wanted)
: undefined;
if (inHistory) return inHistory;
const inUpsampled = Array.isArray(tile.yn)
? tile.yn.find((item) => cleanMediaId(readOwn(item, "id")) === wanted)
const upsampledKey =
frontendBuild(readBuildLabel())?.operations.agent_attachments?.fields.upsampled ?? "yn";
const upsampled = readOwn(tile, upsampledKey);
const inUpsampled = Array.isArray(upsampled)
? (upsampled as FlowMediaPayload[]).find((item) => cleanMediaId(readOwn(item, "id")) === wanted)
: undefined;
if (inUpsampled) return inUpsampled;
return primary && cleanMediaId(readOwn(primary, "id")) === wanted ? primary : undefined;
}

/** Resolves a tile and optional media variant matching a requested project asset or media ID. */
function resolveRequestedAsset(
export function resolveRequestedAsset(
tiles: AttachmentTile[],
requestedId: string,
): { tile: AttachmentTile; variant?: FlowMediaPayload; variantId?: string } | undefined {
Expand Down Expand Up @@ -214,6 +215,21 @@ function referenceKey(reference: AttachmentReference, characterField: string): s
: `media:${reference.mediaId}`;
}

/** Reads the current project tiles from the bound page store when available. */
export function readAgentProjectTiles(page: Record<string, unknown>): AttachmentTile[] {
const format = frontendBuild(readBuildLabel())?.operations.agent_attachments;
if (!format) return [];
const store = readOwn(page, format.store);
const tiles = readOwn(store, format.tiles);
if (typeof tiles !== "function") return [];
try {
const items: unknown = tiles.call(store);
return Array.isArray(items) ? (items as AttachmentTile[]) : [];
} catch {
return [];
}
}

/** Resolves project references, uploads local files, and builds the native Agent message. */
export async function buildAttachmentMessage(input: AttachmentInput) {
const compatibility = agentAttachmentCompatibility();
Expand Down
Loading
Loading