@flow-state-dev/core/attachments
Classes
AttachmentTextTooLargeError
Thrown before a target that gets a PDF's text is called, when that text is
over ATTACHMENT_LIMITS.maxTextBytes. The text is never cut: the turn does
not run. The same PDF still goes whole to a target that reads PDFs.
kind: "text" is a text-like file over the cap: upload refuses one, so
this is the send's own check on a file stored some other way.
Extends
Error
Constructors
Constructor
new AttachmentTextTooLargeError(
filename,
size,
kind?
): AttachmentTextTooLargeError;
Parameters
filename
string | undefined
size
number
Bytes of the file's text.
kind?
"text" | "pdf"
Returns
Overrides
Error.constructor
Properties
cause?
cause?: unknown;
Inherited from
Error.cause
code
readonly code: "pdf_text_too_large" | "text_too_large";
filename
readonly filename: string | undefined;
message
message: string;
Inherited from
Error.message
name
name: string;
Inherited from
Error.name
size
readonly size: number;
Bytes of the file's text.
stack?
stack?: string;
Inherited from
Error.stack
Type Aliases
AttachmentAccepts
type AttachmentAccepts = ReadonlyArray<AttachmentMediaType>;
The media types one target (a model provider, a coding harness) reads as a file: images, and PDFs where it reads them. Each target fills its own. A text-like file goes to every target as text, and a PDF goes as its text to a target that lacks it (attachmentSentAs); an image a target lacks is refused in the conversation, never replaced by text.
AttachmentLimitRefusal
type AttachmentLimitRefusal =
| {
index: number;
reason: "type";
}
| {
index: number;
limit: number;
reason: "too-many-images" | "too-many-documents";
};
Why one message's attachments are over the limits set, and the first file that put them over.
AttachmentMediaType
type AttachmentMediaType =
| typeof ATTACHMENT_IMAGE_TYPES[number]
| typeof ATTACHMENT_DOCUMENT_TYPES[number]
| typeof ATTACHMENT_TEXT_TYPES[number];
A media type an attachment may have.
AttachmentResourceState
type AttachmentResourceState = z.infer<typeof attachmentResourceStateSchema>;
One entry of attachmentsCollection.
Variables
ATTACHMENT_ACCEPT
const ATTACHMENT_ACCEPT: string;
The <input accept> list for every file the list takes: its types, and the text extensions a browser may not type.
ATTACHMENT_DOCUMENT_TYPES
const ATTACHMENT_DOCUMENT_TYPES: readonly ["application/pdf"];
Document types a target may read as a file in v1: a target that doesn't gets a PDF's text instead.
ATTACHMENT_IMAGE_TYPES
const ATTACHMENT_IMAGE_TYPES: readonly ["image/png", "image/jpeg", "image/webp", "image/gif"];
Image types an attachment may have in v1.
ATTACHMENT_LIMITS
const ATTACHMENT_LIMITS: object;
The limits on what one message may carry. The composer refuses over them before send; the server refuses by name.
Type Declaration
documentsPerMessage
readonly documentsPerMessage: 5;
Documents in one message.
imagesPerMessage
readonly imagesPerMessage: 10;
Images in one message.
maxFileBytes
readonly maxFileBytes: number;
Bytes in one file, as picked.
maxPdfPages
readonly maxPdfPages: 100;
Pages in one PDF, counted on the server before any model call.
maxTextBytes
readonly maxTextBytes: number;
Bytes of text from one text-like file, or from a PDF's text sent to a target that can't read PDFs.
unclaimedTtlMs
readonly unclaimedTtlMs: number;
How long an upload no message has claimed is kept, at least.
ATTACHMENT_TEXT_TYPES
const ATTACHMENT_TEXT_TYPES: readonly ["text/plain", "text/markdown", "text/csv", "application/json", "application/yaml", "text/html", "application/xml"];
Text-like types an attachment may have in v1. Every target gets one as
text, headed by its name (formatAttachmentText), never as a file.
Source files are text/plain: the name says the language.
attachmentReferenceSchema
const attachmentReferenceSchema: z.ZodObject<{
data: z.ZodEffects<z.ZodString, string, string>;
filename: z.ZodOptional<z.ZodString>;
mediaType: z.ZodString;
type: z.ZodLiteral<"file">;
}, "strip", z.ZodTypeAny, {
data: string;
filename?: string;
mediaType: string;
type: "file";
}, {
data: string;
filename?: string;
mediaType: string;
type: "file";
}>;
A stored file's reference (FileContent whose data is fsd-blob:<id>): bytes never pass it.
attachmentResourceStateSchema
const attachmentResourceStateSchema: z.ZodObject<{
filename: z.ZodDefault<z.ZodNullable<z.ZodString>>;
mediaType: z.ZodDefault<z.ZodNullable<z.ZodString>>;
recordedAt: z.ZodDefault<z.ZodNullable<z.ZodNumber>>;
ref: z.ZodDefault<z.ZodNullable<z.ZodString>>;
requestId: z.ZodDefault<z.ZodNullable<z.ZodString>>;
}, "strip", z.ZodTypeAny, {
filename: string | null;
mediaType: string | null;
recordedAt: number | null;
ref: string | null;
requestId: string | null;
}, {
filename?: string | null;
mediaType?: string | null;
recordedAt?: number | null;
ref?: string | null;
requestId?: string | null;
}>;
One file a turn of the session sent, keyed by its blob id. Every field is nullable with a null default, so an entry written by an older or newer shape still parses.
ATTACHMENTS
const ATTACHMENTS: "attachments";
Accessor key AND storage prefix of the session's list of files.
attachmentsCapability
const attachmentsCapability: DefinedCapability & object;
Install on the generator that answers, beside recordAttachments before it:
const assistant = generator({ name: "assistant", model, history: true, uses: [attachmentsCapability] });
sequencer({ name: "chat", inputSchema }).tap(recordAttachments).step(assistant);
It declares the attachments list, so it installs it. A flow without it
keeps the newest-files window.
Type Declaration
__configInType?
readonly __configInType?: undefined;
resourcesType?
readonly resourcesType?: unknown;
sequencerStateType?
readonly sequencerStateType?: unknown;
sessionStateType?
readonly sessionStateType?: unknown;
stateSchemaType?
readonly stateSchemaType?: unknown;
targetStatesType?
readonly targetStatesType?: unknown;
attachmentsCollection
const attachmentsCollection: object & ResourceCollectionConfig & object;
The session's list of files: one entry per file, keyed by blob id.
writable: false, so no block or model tool edits or removes an entry; a
new key's create stays open, which is the block's one write. Lazy, because a
session is reused across turns and the block reads one key at a time. The
app reads the entries; the model does not.
Type Declaration
client
readonly client: object;
client.expose
readonly expose: readonly ["ref", "filename", "mediaType", "requestId", "recordedAt"];
client.state
readonly state: object;
client.state.read
readonly read: true;
pattern
readonly pattern: "attachments/*";
prefetchMode
readonly prefetchMode: "lazy";
scope
readonly scope: "session";
stateSchema
readonly stateSchema: z.ZodObject<{
filename: z.ZodDefault<z.ZodNullable<z.ZodString>>;
mediaType: z.ZodDefault<z.ZodNullable<z.ZodString>>;
recordedAt: z.ZodDefault<z.ZodNullable<z.ZodNumber>>;
ref: z.ZodDefault<z.ZodNullable<z.ZodString>>;
requestId: z.ZodDefault<z.ZodNullable<z.ZodString>>;
}, "strip", z.ZodTypeAny, {
filename: string | null;
mediaType: string | null;
recordedAt: number | null;
ref: string | null;
requestId: string | null;
}, {
filename?: string | null;
mediaType?: string | null;
recordedAt?: number | null;
ref?: string | null;
requestId?: string | null;
}>;
writable
readonly writable: false;
__brand
readonly __brand: "ResourceCollection";
ClientType
ClientType: Pick<{
filename: string | null;
mediaType: string | null;
recordedAt: number | null;
ref: string | null;
requestId: string | null;
}, "filename" | "mediaType" | "recordedAt" | "ref" | "requestId">;
StateType
StateType: object;
StateType.filename
filename: string | null;
StateType.mediaType
mediaType: string | null;
StateType.recordedAt
recordedAt: number | null;
StateType.ref
ref: string | null;
StateType.requestId
requestId: string | null;
readAttachment
const readAttachment: BlockDefinition;
The model's read of a document an earlier turn of this conversation sent,
by its id or by its name (matched case-insensitively against the session's
whole attachments list). Reads through the request's file reader, bound to
this session and person: a file that is not this conversation's answers
"not in this conversation", whatever id is passed.
Returns the file's reference, which the model call turns into its text; returns a sentence instead when there is nothing to read (no match, several matches, an image). A document whose text is over the cap is the tool's error.
recordAttachments
const recordAttachments: BlockDefinition;
Records the files this turn sent in the session's ATTACHMENTS list.
Add it to a flow as a .tap step; it declares the collection, so there is
nothing else to wire. A turn with no files writes nothing, and a file
already listed (a retried turn) is not written again.
sequencer({ name: "chat", inputSchema })
.tap(recordAttachments)
.step(assistant)
Functions
attachmentKind()
function attachmentKind(mediaType): "image" | "document" | undefined;
Whether an attachment of mediaType counts against the image or the document limit, or is refused.
Parameters
mediaType
string
Returns
"image" | "document" | undefined
attachmentLimitRefusal()
function attachmentLimitRefusal(attachments): AttachmentLimitRefusal | undefined;
Check one message's attachments against ATTACHMENT_LIMITS: every
file's type is on the list, and the images and documents are each within
their count. undefined when the message may be sent. The server refuses by
this check before any block runs; a composer can run the same check before
send.
Parameters
attachments
readonly object[]
Returns
AttachmentLimitRefusal | undefined
attachmentMediaType()
function attachmentMediaType(file): string;
The type to upload a picked file as: by its extension for a text-like
file, otherwise the type it came with, normalized. The result may be off
the list (.docx, HEIC); isAttachmentMediaType says whether it is.
Parameters
file
name
string
type
string
Returns
string
attachmentSentAs()
function attachmentSentAs(mediaType, accepts): "file" | "text" | undefined;
How a file of mediaType reaches a target that reads accepts as files:
file as the file itself, text as its text headed by its name (every
text-like file; a PDF the target can't read), or undefined when the
target can't take it at all.
Parameters
mediaType
string
accepts
Returns
"file" | "text" | undefined
formatAttachmentText()
function formatAttachmentText(filename, text): string;
A file's text as a target receives it: ### <filename>, then the text as is.
Parameters
filename
string | undefined
text
string
Returns
string
formatKilobytes()
function formatKilobytes(bytes): string;
A size as the composer and the server both name it: KB, rounded up, so a file one byte over a cap never reads as the cap.
Parameters
bytes
number
Returns
string
isAttachmentMediaType()
function isAttachmentMediaType(mediaType): mediaType is AttachmentMediaType;
True when an attachment may have mediaType. HEIC, and anything not listed, is refused.
Parameters
mediaType
string
Returns
mediaType is AttachmentMediaType
isBlobId()
function isBlobId(id): boolean;
True when id has the shape the server gives a blob.
Parameters
id
string
Returns
boolean
isTextAttachmentType()
function isTextAttachmentType(mediaType): boolean;
True when mediaType is a text-like type, which reaches every target as text.
Parameters
mediaType
string
Returns
boolean
normalizeMediaType()
function normalizeMediaType(mediaType): string;
image/PNG; charset=… → image/png.
Parameters
mediaType
string
Returns
string
parseBlobRef()
function parseBlobRef(data): string | undefined;
The blob id inside a reference, or undefined when data is not one.
Parameters
data
string
Returns
string | undefined
toBlobRef()
function toBlobRef(id): string;
The reference a stored file's FileContent.data carries: fsd-blob:<id>.
Parameters
id
string
Returns
string