Files
chatkit/dist/attachments.d.ts
CatoandClaude e690b2254a Messenger: people talking to people, and to the agents among them (0.29.0) (#82)
* Messenger: people talking to people, and to the agents among them (0.26.0)

OrangeCat's direct messages were built in the app, so what was wrong with
them could not be fixed for the next product: an agent's answer in a
conversation between two people looked like the other person's, nothing
said an agent was taking part, top bar and a two-row box took half a
phone, and nothing could be attached. This is the messenger as a package.

@bitbaum/chatkit/messenger: Messenger (the whole screen) and its parts
useMessenger, ConversationList, MessageThread, MessageAttachments. The app
implements MessengerTransport; the package has no database, websocket or
storage. styles/messenger.css is the embedded sheet plus the Messenger's
section, without ChatApp.

- An agent is told from a person by the shape of its face, an Agent mark
  with whom it answered for, and an outlined bubble; the top bar says it
  is in the conversation; naming it shows that it is thinking, and says
  so if it never answers.
- Files: Camera, Photos, Videos, Files, checked against the attachment
  policy before an upload starts, photos shrunk in the browser, progress
  and Try again per file, a full-screen viewer, a film drawn as its still.
- The thread: runs, day lines, a New line, ticks from readState, a failed
  message kept with Try again, earlier messages loaded above with the
  reader's place kept, one bubble per message however often it arrives.
- Composer: layout="inline", attachmentsAlone, attach.take, attach.accept,
  a "videos" source, files dropped onto the box. Defaults unchanged.

The state is one pure function (messengerReducer), tested in Node.
test/browser-messenger.mjs drives demo/messenger.html at 390px, 320px
dark and 1280px. It found two defects before any product did: a read mark
that cleared the badge without telling the server, and a page of history
that pulled the reader to the end of the thread.

threadkit 0.1.2 (attachment policy, mention matcher, readState) is merged
and tagged but not on npm, so those rules are carried word for word in
src/thread-rules.ts with threadkit's own tests; the file says when it goes.

Co-Authored-By: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmsmMXvM981mcszxEA6

* test(messenger): wait for the opened conversation before reading its thread

Side by side, the conversation that was open can still be on screen with a
"New" line of its own, so the check read the old thread on a slow runner.

Co-Authored-By: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmsmMXvM981mcszxEA6

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-11-12 16:26:03 +01:00

192 lines
9.9 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* What a composer can attach, and the wire shape an app's API receives.
*
* From loki `lib/loki/attachments.ts`. The limits are defaults, not law — an
* app passes its own to `useAttachments` when its model or API differs.
*/
export type TextAttachment = {
kind: "text";
name: string;
content: string;
};
export type ImageAttachment = {
kind: "image";
name: string;
mimeType: string;
dataBase64: string;
};
export type Attachment = TextAttachment | ImageAttachment;
/** Client-only preview URL — never sent to the API. */
export type StagedAttachment = Attachment & {
previewUrl?: string;
};
export type AttachmentLimits = {
/** Files per message. */
maxFiles: number;
/** Characters per text file (it is inlined into a prompt). */
maxTextChars: number;
/** Raw image bytes before base64 — checked AFTER any downscale. */
maxImageBytes: number;
/**
* Longest edge, in pixels, an image is shrunk to in the browser before it is
* staged. 0 = send the original. A phone screenshot is ~1290×2796 and several
* megabytes; vision models bill by tile and read a chat bubble just as well
* at 1024px, so shrinking first is cheaper for the reader AND means the full
* screenshot never leaves the device. Shrunk images are re-encoded as JPEG
* (a screenshot re-encoded as PNG is often larger), stepping quality down
* until they fit `maxImageBytes`. From heidi `downscale.ts`.
*/
maxImageEdge: number;
};
/**
* What a reader is told about the file it is asked to turn into attachments.
* `images` and `room` are counted from what is already staged (and from the
* files read before this one, which are read one at a time), so a reader that
* produces several pictures can stop at the app's own ceiling.
*/
export type FileReadContext = {
limits: AttachmentLimits;
notes: AttachmentNotes;
/** Images already staged. */
images: number;
/** How many more attachments fit under `limits.maxFiles`. */
room: number;
/** Say something while a slow read runs ("Reading deck.pdf…"). Replaced by
* the result's `note`, or cleared when there is none. */
progress: (note: string) => void;
};
/**
* What a reader made of a file: the attachments to stage (none is fine) and
* the sentence the person reads under the box (why nothing, or what was left
* out). Never thrown — a rejected file must not lose the draft next to it.
*/
export type FileReadResult = {
attachments: StagedAttachment[];
note?: string;
};
/**
* The app's own reading of a picked, pasted or dropped file, for kinds the
* default reader refuses (a PDF; see `@bitbaum/chatkit/pdf`) or reads
* differently. Return `undefined` to leave the file to chatkit's reader. A
* thrown error becomes the "could not read" note.
*/
export type AttachmentReader = (file: File, ctx: FileReadContext) => Promise<FileReadResult | undefined> | FileReadResult | undefined;
/** The composer's `attach` option: the limits, and optionally a reader. */
export type AttachOptions = Partial<AttachmentLimits> & {
readFile?: AttachmentReader;
/** Which sources the paperclip offers (default: camera, photos, files). An
* app whose backend reads text and not pictures passes `["files"]`: no
* Camera or Photos row is drawn, and a picture that arrives by paste or
* drop is refused with the `wrongType` note, never staged and silently
* dropped by the server. */
sources?: readonly AttachSource[];
/** What a source asks the phone for, where the default is not it: a
* messenger's Files takes office documents and archives, a model's does
* not. One family per source (`acceptFamilies`), for the reason
* `ATTACH_SOURCE_INPUT` gives. */
accept?: Partial<Record<AttachSource, string>>;
/**
* The app takes the files itself. Everything picked, pasted or dropped is
* handed over as it came — nothing is read into the page, nothing is staged
* in the box, and a recording is a file like any other, not dictation. For
* a box whose files go to storage, not into a prompt (a messenger): the app
* uploads them, shows them in `header`, and counts them with
* `heldAttachments`.
*/
take?: (files: File[]) => void;
};
/** Whether pictures can be attached under these options' `sources`. */
export declare function acceptsImages(sources: readonly AttachSource[] | undefined): boolean;
export declare const DEFAULT_ATTACHMENT_LIMITS: AttachmentLimits;
/** Why a file did not attach — every one a label, so an app can say it in its
* reader's language. */
export type AttachmentNotes = {
wrongType: (name: string) => string;
imageTooLarge: (name: string, maxMb: number) => string;
textTooLarge: (name: string, maxThousandChars: number) => string;
unreadable: (name: string) => string;
tooMany: (max: number) => string;
};
export declare const DEFAULT_ATTACHMENT_NOTES: AttachmentNotes;
/**
* The size an image is drawn at to fit `maxEdge` on its longest side. Never
* enlarges; 0 (or less) means "leave it". Pure, so it is tested without a DOM.
*/
export declare function fitWithin(width: number, height: number, maxEdge: number): {
width: number;
height: number;
scaled: boolean;
};
/** Bytes a base64 data URL decodes to — close enough to compare to a limit. */
export declare function dataUrlBytes(dataUrl: string): number;
export declare function isImageMime(mime: string): boolean;
/**
* Whether a file is text the model can read inline. The Files picker asks for
* documents (FILES_ACCEPT) but a picker may still hand over anything, so the
* decision about what can actually be sent stays here, after the pick, where
* a PDF gets a sentence instead of being read as binary noise and pasted into
* a prompt.
*/
export declare function isTextFile(name: string, mime: string): boolean;
/**
* Where an attachment comes from. Three sources, three inputs, because a phone
* answers ONE input with a mixed `accept` ("image/*,text/*,audio/*") by
* opening a chooser of capture apps — Camera, Camera, Recorder, "Photos &
* Videos" — in which the screenshot you came to send is three levels down,
* and the file browser is not offered at all. Measured on Android/Brave,
* 2026-10-05; it is what made attaching to Loki "impossible".
*
* - camera: `capture` opens the camera straight away.
* - photos: `image/*` alone opens the system photo picker (Screenshots is its
* first album), not a chooser.
* - videos: `video/*` alone, the same picker filtered to films. Its own
* source, not a wider Photos: a second family on one input is the mixed
* `accept` this type exists to prevent. Offered only where asked for
* (`sources`), since most boxes send words to a model, not films to people.
* - files: `FILES_ACCEPT`, documents only (PDF and text), opens the real
* document browser — Downloads, Drive, any folder. It used to be no `accept`
* at all, which Chrome answered with that browser but Brave on Android
* (2026-10-27, OrangeCat) answered with the capture chooser above, Files
* missing: an empty `accept` means "anything", and "anything" includes the
* camera and the recorder. Naming only document types takes every capture
* app off the table on both. The type check still happens after the pick,
* in `isTextFile` and the app's own reader (`@bitbaum/chatkit/pdf`).
*/
export type AttachSource = "camera" | "photos" | "videos" | "files";
/**
* What the Files source asks the phone for: PDFs and text, one "document"
* family. MIME types first (what Android and iOS map to their pickers), then
* the extensions a desktop file dialog filters by. Only types every platform
* knows: an extension a phone cannot map can widen the request back to
* "anything" — the capture chooser this exists to avoid.
*/
export declare const FILES_ACCEPT = "application/pdf,text/*,application/json,.pdf,.txt,.md,.markdown,.csv,.tsv,.json";
/**
* What a messenger's Files source asks for: documents a person sends another
* person — PDF, text, office files, an archive. Still one "document" family,
* so a phone still answers with its file browser and not a capture chooser.
*/
export declare const MESSAGE_FILES_ACCEPT = "application/pdf,text/plain,text/csv,text/markdown,application/json,application/zip,application/rtf,application/msword,application/vnd.ms-excel,application/vnd.ms-powerpoint,application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.openxmlformats-officedocument.presentationml.presentation,application/vnd.oasis.opendocument.text,application/vnd.oasis.opendocument.spreadsheet,application/epub+zip,.pdf,.txt,.md,.csv,.json,.zip,.rtf,.doc,.docx,.xls,.xlsx,.ppt,.pptx,.odt,.ods,.epub";
export declare const ATTACH_SOURCE_INPUT: Record<AttachSource, {
accept?: string;
capture?: "environment";
}>;
/** What kind of picker a file `accept` asks a phone for. */
export type AcceptFamily = "image" | "audio" | "video" | "document";
/**
* The families an `accept` value spans. More than one is the bug
* `ATTACH_SOURCE_INPUT` exists to prevent: a phone answers a mixed accept with
* a chooser of capture apps instead of the picker the person needed. Pure, so
* the fleet's file-input check (`chatkit-check-file-inputs`) and the tests
* share one definition.
*/
export declare function acceptFamilies(accept: string): AcceptFamily[];
/** `data:image/png;base64,AAAA` → `AAAA`: FileReader hands back a data URL,
* the wire carries raw base64. */
export declare function stripDataUrlBase64(dataUrl: string): string;
/** Identity for dedupe and removal: the same name and bytes are the same
* attachment however they arrived (picker, paste, drop). */
export declare function attachmentKey(a: Attachment): string;
/** Strip client-only fields: what the API validates. */
export declare function toWire(staged: StagedAttachment[]): Attachment[];
//# sourceMappingURL=attachments.d.ts.map