mirror of
https://github.com/bitbaum/writingkit.git
synced 2026-12-04 05:37:40 +01:00
Throw everything in (thoughts, speech, a pasted chat, links, documents, pictures) and press one button. composeArticle reads links and documents as cited sources, places the writer's pictures as captioned figures, marks and fills illustration spots through the app's finder, keeps a chart only if its numbers are in the material, and repairs one bad answer. PileComposer is the front door; the result opens in ArticleComposer. Claude-Session: https://claude.ai/code/session_019MDtmsN9mz8QfGDCrNhY9p Co-authored-by: Claude <noreply@anthropic.com>
228 lines
8.7 KiB
TypeScript
228 lines
8.7 KiB
TypeScript
/**
|
|
* Compose an article from a pile: whatever the writer dumped (notes, a pasted
|
|
* chat, half-sentences, links) becomes one cohesive, titled article, grounded
|
|
* in the pages those links point to and citing them as [S1], [S2], ….
|
|
*
|
|
* WHY HERE
|
|
* --------
|
|
* On 15 Nov 2026 a writer pasted a chat and links into OrangeCat's article
|
|
* brief and was refused; once the length cap was fixed, the draft would still
|
|
* have treated the links as plain text — no product read them. BiasLens had
|
|
* the pieces (a source reader, numbered evidence, citation clean-up, repair
|
|
* once) spread over its investigation and rewrite code. This module is the one
|
|
* copy every product uses.
|
|
*
|
|
* No provider, endpoint or fetch lives here (README "Ownership"): the host
|
|
* passes `read(url)` (ai-kit's `readPage`, or BiasLens's PDF-aware reader) and
|
|
* `complete(messages)` (its ai-kit chain). Everything else — splitting the
|
|
* pile, numbering sources, the prompt contract, checking the answer, dropping
|
|
* citations to sources that do not exist, one repair — is pure and tested here.
|
|
*/
|
|
export declare const COMPOSE_LIMITS: {
|
|
/** Characters of pile read; past this the pile is clipped (never refused). */
|
|
readonly pile: 60000;
|
|
/** Links read per compose. */
|
|
readonly sources: 8;
|
|
/** Characters of each source given to the model. */
|
|
readonly sourceChars: 8000;
|
|
readonly body: 60000;
|
|
readonly title: 160;
|
|
readonly excerpt: 300;
|
|
/** The writer's own pictures placed per compose. */
|
|
readonly images: 12;
|
|
/** Pictures the model may ask to be found or made. */
|
|
readonly illustrations: 4;
|
|
};
|
|
/**
|
|
* One thing the writer threw in. A plain string pile is the same as a single
|
|
* text item (links inside it are found and read).
|
|
*/
|
|
export type PileItem = {
|
|
kind: "text";
|
|
text: string; /** From the microphone: rambling, fillers. */
|
|
spoken?: boolean;
|
|
} | {
|
|
kind: "link";
|
|
url: string;
|
|
}
|
|
/** A pasted or uploaded document (PDF text, an email), already as text. */
|
|
| {
|
|
kind: "document";
|
|
name: string;
|
|
text: string;
|
|
} | {
|
|
kind: "image";
|
|
/** Where the picture is served from (the host has stored it). */
|
|
src: string;
|
|
/** What it shows, if the writer or the host already said. */
|
|
description?: string;
|
|
credit?: string;
|
|
license?: string;
|
|
};
|
|
/** A picture of the writer's, keyed I1… for the model. */
|
|
export interface PileImage {
|
|
key: string;
|
|
src: string;
|
|
description: string;
|
|
credit?: string;
|
|
license?: string;
|
|
}
|
|
/** A place where the article asks for a picture nobody supplied yet. */
|
|
export interface IllustrationSlot {
|
|
/** Words to search a photo library with. */
|
|
query: string;
|
|
/** The caption the picture should carry. */
|
|
caption: string;
|
|
}
|
|
/** A picture the host found or made for a slot. */
|
|
export interface FoundImage {
|
|
src: string;
|
|
alt: string;
|
|
credit?: string;
|
|
license?: string;
|
|
}
|
|
export interface PileParts {
|
|
/** The writer's own words, links removed where they stood alone on a line. */
|
|
notes: string;
|
|
/** Distinct http(s) links in the order they appeared, at most `COMPOSE_LIMITS.sources`. */
|
|
urls: string[];
|
|
/** Links found beyond the limit (not read). */
|
|
skippedUrls: string[];
|
|
}
|
|
/** Split a dump into the writer's notes and the links to read. */
|
|
export declare function splitPile(pile: string): PileParts;
|
|
/** What the host's reader returns for one link. */
|
|
export type SourceRead = {
|
|
ok: true;
|
|
url: string;
|
|
title?: string;
|
|
text: string;
|
|
} | {
|
|
ok: false;
|
|
url: string;
|
|
reason: string;
|
|
};
|
|
export interface Source {
|
|
/** Citation key, "S1"… */
|
|
key: string;
|
|
url: string;
|
|
title: string;
|
|
text: string;
|
|
}
|
|
export interface FailedSource {
|
|
url: string;
|
|
/** In words, from the reader ("the site needs a login", "timed out"). */
|
|
reason: string;
|
|
}
|
|
/** Number the sources that were read; keep the ones that failed, with why. */
|
|
export declare function numberSources(reads: readonly SourceRead[]): {
|
|
sources: Source[];
|
|
failed: FailedSource[];
|
|
};
|
|
export interface ComposeInput {
|
|
/** The writer's notes (from `splitPile`). */
|
|
notes: string;
|
|
sources: readonly Source[];
|
|
/** The writer's pictures, described. */
|
|
images?: readonly PileImage[];
|
|
/** Output language label, e.g. "English". */
|
|
language?: string;
|
|
/** Host context about the writer: voice samples, profile. Material, not facts to state. */
|
|
voice?: string;
|
|
/** Desired length or style, if the writer said one ("short", "1500 words"). */
|
|
length?: string;
|
|
/** A working title the writer already typed. */
|
|
title?: string;
|
|
}
|
|
export declare function composeMessages(input: ComposeInput): ({
|
|
role: "system";
|
|
content: string;
|
|
} | {
|
|
role: "user";
|
|
content: string;
|
|
})[];
|
|
export interface ComposedDraft {
|
|
title: string;
|
|
excerpt: string;
|
|
/** Markdown with the writer's pictures placed as figures. */
|
|
body: string;
|
|
/** Sources the body actually cites, in key order. */
|
|
cited: Source[];
|
|
/** Pictures the article asks for that nobody supplied yet, in order. */
|
|
illustrations: IllustrationSlot[];
|
|
/** Charts removed because they drew numbers not in the material, or did not parse. */
|
|
droppedCharts: number;
|
|
}
|
|
/** Marker a host can find again to fill a picture later: `[[FIND: query | caption]]`. */
|
|
export declare const FIND_MARKER: RegExp;
|
|
export interface ParseComposeOptions {
|
|
fallbackTitle?: string;
|
|
images?: readonly PileImage[];
|
|
/** All material a chart may draw numbers from: notes and source texts. */
|
|
material?: string;
|
|
}
|
|
/**
|
|
* The model's answer, checked and finished:
|
|
* - citations to keys that are not sources are removed (a made-up [S9] is
|
|
* worse than none);
|
|
* - [[I#]] lines become figures with their captions; the writer's pictures the
|
|
* model left out are placed after the lede — a picture is never lost;
|
|
* - [[FIND: …]] lines stay in the body as markers and are listed, for the host
|
|
* to fill or to offer "find a picture" at that spot;
|
|
* - a ```chart that does not parse, or draws a number the material does not
|
|
* contain, is removed.
|
|
* Null when it is not an article.
|
|
*/
|
|
export declare function parseComposeAnswer(raw: string, sources: readonly Source[], opts?: ParseComposeOptions | string): ComposedDraft | null;
|
|
/**
|
|
* Put found pictures into the [[FIND: …]] spots, in order. A spot whose picture
|
|
* is `null` keeps its marker, so the host can still offer "find a picture" there.
|
|
*/
|
|
export declare function fillIllustrations(body: string, found: readonly (FoundImage | null)[]): string;
|
|
/** Remove the [[FIND: …]] markers still in a body (before publishing). */
|
|
export declare function stripIllustrationMarkers(body: string): string;
|
|
/** Markdown list of the cited sources, to append under the article. */
|
|
export declare function sourcesSection(cited: readonly Source[], heading?: string): string;
|
|
/** Normalise a pile: a string becomes one text item; links inside text are found. */
|
|
export declare function pileItems(pile: string | readonly PileItem[]): PileItem[];
|
|
export type ComposeMessage = ReturnType<typeof composeMessages>[number];
|
|
export interface ComposeDeps {
|
|
/** Read one link. Should never throw; a throw is recorded as a failed source. */
|
|
read: (url: string, signal?: AbortSignal) => Promise<SourceRead>;
|
|
/** One model call; returns the raw text, or null when no model answered. */
|
|
complete: (messages: ComposeMessage[], signal?: AbortSignal) => Promise<string | null>;
|
|
/**
|
|
* Say what a picture shows (ai-kit vision). Optional: without it, a picture
|
|
* with no description is placed with a neutral caption.
|
|
*/
|
|
describe?: (src: string, signal?: AbortSignal) => Promise<string | null>;
|
|
/**
|
|
* Find or make a picture for a slot (a photo library, image generation).
|
|
* Optional: without it, slots stay as markers and are returned.
|
|
*/
|
|
illustrate?: (slot: IllustrationSlot, signal?: AbortSignal) => Promise<FoundImage | null>;
|
|
}
|
|
export type ComposeResult = {
|
|
ok: true;
|
|
draft: ComposedDraft;
|
|
sources: Source[];
|
|
failed: FailedSource[];
|
|
skippedUrls: string[];
|
|
/** Model calls made (2 means the first answer was repaired). */
|
|
attempts: number;
|
|
} | {
|
|
ok: false;
|
|
/** In words, with what to do next. */
|
|
problem: string;
|
|
failed: FailedSource[];
|
|
};
|
|
/**
|
|
* The whole flow: gather the pile (text, speech, links, documents, pictures),
|
|
* read the links and describe the pictures in parallel, compose, check, repair
|
|
* once, then fill the picture spots. Never throws for a model, reader or
|
|
* picture failure; the result says what happened and what the writer can do.
|
|
*/
|
|
export declare function composeArticle(pile: string | readonly PileItem[], deps: ComposeDeps, opts?: Omit<ComposeInput, "notes" | "sources" | "images"> & {
|
|
signal?: AbortSignal;
|
|
}): Promise<ComposeResult>;
|