/** * Help me write templates (HTP-6383) — practice-shared AI templates in the * RichTextEditor. * * Runtime pattern: real consuming apps mount this in production (surfaced via * `src/patterns/index.ts` and the `./patterns/help-me-write-templates` * subpath). Templates are an AI action of the editor itself, next to * spelling and auto-format: a Templates segment in the AI pill and in the * selection bubble (`aiTemplates`). Picking one rewrites the selection, else * the whole note, then this pattern offers Undo. The editor keeps the * patient AI-consent gate and resume-after-grant for all of it. * * Parts, usable on their own or wired together by `useHelpMeWriteTemplates`: * - `HelpMeWriteTemplatesBar` — the Help-me-write bar (`renderHelpMeWrite`): * a free prompt with an optional mic, the draft and Insert. * - `AiTemplateManager` — the dialog to list, create, edit, delete and * reorder templates. * - `AiCreditsExhaustedNotice`: the out-of-credits state the bar shows when * an AI call rejects with `AiCreditsExhaustedError`, from the bar or the * pill. It lives in `src/components/ai-credits-notice/` (shared with * VoiceControlPanel) and is re-exported here, with `AiCreditsInfo`. * - `HelpMeWriteUndoNotice` — the success notice with Undo after a template * rewrote the note. * * The host owns the templates (list + persistence) and the AI call. The kit * never talks to a backend. */ import { type ReactNode } from 'react'; import { type AiCreditsInfo } from '../../components/ai-credits-notice'; import type { HelpMeWriteRenderContext, RichTextEditorAiApplied, RichTextEditorAiTemplates, RichTextEditorProps } from '../../components/rich-text-editor'; /** A practice-shared AI template. */ export interface AiTemplate { id: string; /** Up to 100 characters, unique within the practice. */ name: string; /** One line under the name in the Templates menu. */ description?: string; /** The instructions sent to the AI, up to 4000 characters. */ prompt: string; /** * @deprecated The list order is the menu order: the first template is the * one most people use. Ignored; reorder with `onReorderTemplates`. */ isDefault?: boolean; } /** What the manager hands `onSaveTemplate`: `id` is absent for a new one. */ export type AiTemplateDraft = Omit & { id?: string; }; /** One Help-me-write request. `template` is absent for a free prompt. */ export interface HelpMeWriteRequest { template?: AiTemplate; /** The free prompt, or extra instructions for a template. May be ''. */ prompt: string; /** * The context, as plain text. For a template, the text it rewrites. For * the free prompt, what the draft is about. From the AI pill it is the * whole note; from the selection bubble, the selected text. */ sourceText: string; /** Where `sourceText` came from. */ scope?: 'document' | 'selection'; signal: AbortSignal; } /** * Throw (or reject with) this from `onGenerate` when the practice is out of * AI credits (the platform endpoint's 402), and the bar shows the * out-of-credits notice instead of a generic error. */ export declare class AiCreditsExhaustedError extends Error { constructor(message?: string); } export { AiCreditsExhaustedNotice, type AiCreditsExhaustedNoticeProps, type AiCreditsInfo, } from '../../components/ai-credits-notice'; export type HelpMeWriteApplyMode = 'replace' | 'below' | 'insert'; /** Fired after a draft lands in the editor. */ export interface HelpMeWriteApplied { mode: HelpMeWriteApplyMode; template?: AiTemplate; /** The editor's HTML before the change. */ previousHtml: string; /** Puts `previousHtml` back in the editor (what Undo does). */ undo: () => void; } export interface HelpMeWriteTemplatesBarProps { /** What `RichTextEditor`'s `renderHelpMeWrite` hands its host. */ context: HelpMeWriteRenderContext; /** * @deprecated Templates run from the editor's AI pill (`aiTemplates`), * not from this bar. Ignored. */ templates?: AiTemplate[]; /** The AI call. Reject with `AiCreditsExhaustedError` when out of credits. */ onGenerate: (request: HelpMeWriteRequest) => Promise; /** @deprecated The manager opens from the pill's Templates menu. Ignored. */ onManageTemplates?: (mode: 'list' | 'create') => void; /** After a draft lands. */ onApplied?: (applied: HelpMeWriteApplied) => void; /** Shown in the out-of-credits notice. */ credits?: AiCreditsInfo; /** Show the mic in the prompt when the editor has `onDictate`. Default true. */ enableDictation?: boolean; /** Ignore Escape (e.g. while the manager dialog above it is open). */ suspendClose?: boolean; } /** * The Help-me-write bar, docked in the editor's AI slot: a free prompt with * an optional mic, then the draft and Insert. It also shows the * out-of-credits notice, including when a pill action ran out. */ export declare function HelpMeWriteTemplatesBar({ context, onGenerate, onApplied, credits, enableDictation, suspendClose, }: HelpMeWriteTemplatesBarProps): React.JSX.Element; export interface AiTemplateManagerProps { open: boolean; onOpenChange: (open: boolean) => void; /** Open on the list (default) or straight on a new-template form. */ initialMode?: 'list' | 'create'; templates: AiTemplate[]; /** Persist a new (no `id`) or edited template. A rejection keeps the form open. */ onSaveTemplate: (draft: AiTemplateDraft) => void | Promise; /** Omit to hide Delete. */ onDeleteTemplate?: (id: string) => void | Promise; /** * Persist a new order (every template id, first to last): the order of * the Templates menu. Omit to hide the drag handles. */ onReorderTemplates?: (orderedIds: string[]) => void | Promise; } /** List, create, edit and delete the practice's templates. */ export declare function AiTemplateManager({ open, onOpenChange, initialMode, templates, onSaveTemplate, onDeleteTemplate, onReorderTemplates, }: AiTemplateManagerProps): React.JSX.Element; export interface HelpMeWriteUndoNoticeProps { /** The change to offer Undo for; `null` hides the notice. */ applied: HelpMeWriteApplied | null; onUndo: () => void; onDismiss: () => void; } /** "Note rewritten with …" plus Undo; takes focus when it appears. */ export declare function HelpMeWriteUndoNotice({ applied, onUndo, onDismiss, }: HelpMeWriteUndoNoticeProps): React.JSX.Element | null; export interface UseHelpMeWriteTemplatesOptions { templates: AiTemplate[]; /** The AI call, for both templates (from the pill) and the free prompt. */ onGenerate: HelpMeWriteTemplatesBarProps['onGenerate']; /** Enables the manager (create / edit). Omit for read-only template users. */ onSaveTemplate?: AiTemplateManagerProps['onSaveTemplate']; /** Enables Delete in the manager. */ onDeleteTemplate?: AiTemplateManagerProps['onDeleteTemplate']; /** Enables drag-and-drop reordering in the manager (the menu order). */ onReorderTemplates?: AiTemplateManagerProps['onReorderTemplates']; credits?: AiCreditsInfo; /** Mic in the bar's prompt when the editor has `onDictate`. Default true. */ enableDictation?: boolean; /** Also told about every applied draft (e.g. for analytics). */ onApplied?: (applied: HelpMeWriteApplied) => void; /** * Shows the AI-credits/bundles hover card on every AI trigger the editor * has (this bar's own segment included, via `editorProps`). One wiring * covers both the bar's existing out-of-credits notice (`credits`, shown * only once the practice actually runs out) and this always-available * hover preview. */ creditsNotice?: RichTextEditorProps['aiCreditsNotice']; } /** The `RichTextEditor` props the hook drives; spread them on the editor. */ export interface HelpMeWriteTemplatesEditorProps { renderHelpMeWrite: (context: HelpMeWriteRenderContext) => ReactNode; aiTemplates: RichTextEditorAiTemplates; onAiApplied: (applied: RichTextEditorAiApplied) => void; aiCreditsNotice?: RichTextEditorProps['aiCreditsNotice']; } export interface UseHelpMeWriteTemplatesResult extends HelpMeWriteTemplatesEditorProps { /** All three editor props at once: ``. */ editorProps: HelpMeWriteTemplatesEditorProps; /** Mount once, anywhere (a portalled dialog); `null` without `onSaveTemplate`. */ manager: ReactNode; /** Place where the Undo notice should show, e.g. above the editor. */ undoNotice: ReactNode; openManager: (mode?: 'list' | 'create') => void; } /** * Wires templates, the Help-me-write bar, the manager and Undo into the * editor. Templates run from the editor's AI pill and selection bubble; * the bar is the free prompt. Usage: * * const hmw = useHelpMeWriteTemplates({ templates, onGenerate, onSaveTemplate }); * {hmw.undoNotice} * * {hmw.manager} */ export declare function useHelpMeWriteTemplates({ templates, onGenerate, onSaveTemplate, onDeleteTemplate, onReorderTemplates, credits, enableDictation, onApplied, creditsNotice, }: UseHelpMeWriteTemplatesOptions): UseHelpMeWriteTemplatesResult; //# sourceMappingURL=help-me-write-templates.d.ts.map