@pixerate/editor 0.21.0 → 0.23.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +100 -13
- package/THIRD_PARTY_NOTICES.md +88 -0
- package/dist/agent/index.cjs +86 -111
- package/dist/agent/index.cjs.map +1 -1
- package/dist/agent/index.d.cts +69 -211
- package/dist/agent/index.d.ts +69 -211
- package/dist/agent/index.js +77 -102
- package/dist/agent/index.js.map +1 -1
- package/dist/arrangeShortcuts-B8O3f8-P.d.cts +184 -0
- package/dist/arrangeShortcuts-sLlD2rSX.d.ts +184 -0
- package/dist/canvas/index.cjs +2112 -60
- package/dist/canvas/index.cjs.map +1 -1
- package/dist/canvas/index.d.cts +712 -181
- package/dist/canvas/index.d.ts +712 -181
- package/dist/canvas/index.js +2105 -53
- package/dist/canvas/index.js.map +1 -1
- package/dist/chunk-26BVFKHU.cjs +453 -0
- package/dist/chunk-26BVFKHU.cjs.map +1 -0
- package/dist/chunk-2H7ZHPEJ.js +762 -0
- package/dist/chunk-2H7ZHPEJ.js.map +1 -0
- package/dist/chunk-7DWMJCAT.js +453 -0
- package/dist/chunk-7DWMJCAT.js.map +1 -0
- package/dist/{chunk-TVRC4KVT.cjs → chunk-AA4ZEVCC.cjs} +845 -90
- package/dist/chunk-AA4ZEVCC.cjs.map +1 -0
- package/dist/chunk-ARRTP33I.js +101 -0
- package/dist/chunk-ARRTP33I.js.map +1 -0
- package/dist/{chunk-LA7PEGU3.cjs → chunk-CL37JGFW.cjs} +4 -4
- package/dist/{chunk-LA7PEGU3.cjs.map → chunk-CL37JGFW.cjs.map} +1 -1
- package/dist/chunk-CPOKFNBG.cjs +101 -0
- package/dist/chunk-CPOKFNBG.cjs.map +1 -0
- package/dist/chunk-G2I4QJOT.cjs +762 -0
- package/dist/chunk-G2I4QJOT.cjs.map +1 -0
- package/dist/{chunk-4QPLEUL2.cjs → chunk-GVSMRPHB.cjs} +1046 -54
- package/dist/chunk-GVSMRPHB.cjs.map +1 -0
- package/dist/{chunk-KYRZDWM4.js → chunk-JJDLPEWL.js} +2 -2
- package/dist/{chunk-GVW4P7BG.js → chunk-K3R5NG2D.js} +448 -54
- package/dist/chunk-K3R5NG2D.js.map +1 -0
- package/dist/{chunk-3TYMNULT.js → chunk-MJZBNWG7.js} +807 -52
- package/dist/chunk-MJZBNWG7.js.map +1 -0
- package/dist/{chunk-A7DKIA72.js → chunk-QBJ22YZR.js} +1059 -93
- package/dist/chunk-QBJ22YZR.js.map +1 -0
- package/dist/{chunk-CYFJCKC4.js → chunk-QGHGJ56O.js} +997 -5
- package/dist/chunk-QGHGJ56O.js.map +1 -0
- package/dist/{chunk-JYRIOFEN.cjs → chunk-RIKBJLS2.cjs} +1064 -98
- package/dist/chunk-RIKBJLS2.cjs.map +1 -0
- package/dist/{chunk-U6NDQFOK.cjs → chunk-XDMXXC4Z.cjs} +569 -175
- package/dist/chunk-XDMXXC4Z.cjs.map +1 -0
- package/dist/commands/index.cjs +860 -0
- package/dist/commands/index.cjs.map +1 -0
- package/dist/commands/index.d.cts +592 -0
- package/dist/commands/index.d.ts +592 -0
- package/dist/commands/index.js +860 -0
- package/dist/commands/index.js.map +1 -0
- package/dist/{controller-OUdxPY-X.d.ts → controller-pePCJLT1.d.cts} +216 -11
- package/dist/{controller-OUdxPY-X.d.cts → controller-pePCJLT1.d.ts} +216 -11
- package/dist/extensions/index.cjs +14 -4
- package/dist/extensions/index.cjs.map +1 -1
- package/dist/extensions/index.d.cts +3 -3
- package/dist/extensions/index.d.ts +3 -3
- package/dist/extensions/index.js +13 -3
- package/dist/history/index.cjs +12 -2
- package/dist/history/index.cjs.map +1 -1
- package/dist/history/index.d.cts +211 -7
- package/dist/history/index.d.ts +211 -7
- package/dist/history/index.js +13 -3
- package/dist/image-editor/index.cjs +418 -13
- package/dist/image-editor/index.cjs.map +1 -1
- package/dist/image-editor/index.d.cts +154 -3
- package/dist/image-editor/index.d.ts +154 -3
- package/dist/image-editor/index.js +417 -12
- package/dist/image-editor/index.js.map +1 -1
- package/dist/{index-k3mFbPx_.d.cts → index-CCr_z9kF.d.cts} +3 -3
- package/dist/{index-Ng1pG7JE.d.ts → index-Ck6ajwXh.d.ts} +3 -3
- package/dist/index.cjs +275 -83
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +296 -47
- package/dist/index.d.ts +296 -47
- package/dist/index.js +266 -74
- package/dist/index.js.map +1 -1
- package/dist/registry-D5-R90p0.d.cts +209 -0
- package/dist/registry-D5-R90p0.d.ts +209 -0
- package/dist/{registry-iAHbqkYt.d.cts → registry-GJI4gToU.d.cts} +15 -4
- package/dist/{registry-iAHbqkYt.d.ts → registry-GJI4gToU.d.ts} +15 -4
- package/dist/serializers/index.cjs +3 -3
- package/dist/serializers/index.d.cts +1 -1
- package/dist/serializers/index.d.ts +1 -1
- package/dist/serializers/index.js +2 -2
- package/dist/spreadsheet/index.cjs +23 -3
- package/dist/spreadsheet/index.cjs.map +1 -1
- package/dist/spreadsheet/index.d.cts +97 -10
- package/dist/spreadsheet/index.d.ts +97 -10
- package/dist/spreadsheet/index.js +22 -2
- package/dist/{track-changes-G8JcWB8b.d.cts → track-changes-BSF58RK0.d.cts} +92 -9
- package/dist/{track-changes-G8JcWB8b.d.ts → track-changes-BSF58RK0.d.ts} +92 -9
- package/dist/{types-DMe0vFB1.d.cts → types-DX4Mh4SL.d.cts} +1 -1
- package/dist/{types-DMe0vFB1.d.ts → types-DX4Mh4SL.d.ts} +1 -1
- package/dist/types-GT5a5YAX.d.cts +86 -0
- package/dist/types-GT5a5YAX.d.ts +86 -0
- package/package.json +8 -2
- package/dist/chunk-2TWGLC5K.cjs +0 -262
- package/dist/chunk-2TWGLC5K.cjs.map +0 -1
- package/dist/chunk-3TYMNULT.js.map +0 -1
- package/dist/chunk-4QPLEUL2.cjs.map +0 -1
- package/dist/chunk-A7DKIA72.js.map +0 -1
- package/dist/chunk-CYFJCKC4.js.map +0 -1
- package/dist/chunk-GVW4P7BG.js.map +0 -1
- package/dist/chunk-JYRIOFEN.cjs.map +0 -1
- package/dist/chunk-KAADNAPF.js +0 -262
- package/dist/chunk-KAADNAPF.js.map +0 -1
- package/dist/chunk-TVRC4KVT.cjs.map +0 -1
- package/dist/chunk-U6NDQFOK.cjs.map +0 -1
- package/dist/types-BZgcDsC5.d.cts +0 -42
- package/dist/types-BZgcDsC5.d.ts +0 -42
- /package/dist/{chunk-KYRZDWM4.js.map → chunk-JJDLPEWL.js.map} +0 -0
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/controller.ts","../src/dirty.ts","../src/import/controller.ts"],"sourcesContent":["import { Editor, EditorOptions, Content, Extensions } from \"@tiptap/core\";\nimport { Slice } from \"@tiptap/pm/model\";\nimport { Token, tokenizePrompt } from \"./grammar\";\nimport {\n plainTextToTipTapHtml,\n plainTextToSlice,\n getEditorText,\n markdownToTipTapHtml,\n getEditorMarkdown,\n} from \"./serializers\";\n\nexport interface EditorControllerOptions extends Partial<\n Omit<EditorOptions, \"element\" | \"content\">\n> {\n element?: Element | HTMLElement | null;\n content?: string | Content;\n plainTextMode?: boolean;\n markdownMode?: boolean;\n extensions?: Extensions;\n onPlainTextChange?: (text: string) => void;\n onMarkdownChange?: (markdown: string) => void;\n onTokensChange?: (tokens: Token[]) => void;\n onSelectionChange?: (range: { from: number; to: number } | null) => void;\n}\n\n/**\n * UI-agnostic controller managing the lifecycle, commands, and plain-text/token\n * synchronization of a rich text or prompt editor.\n */\nexport class EditorController {\n public readonly editor: Editor;\n private readonly plainTextMode: boolean;\n private readonly markdownMode: boolean;\n private readonly onPlainTextChange?: (text: string) => void;\n private readonly onMarkdownChange?: (markdown: string) => void;\n private readonly onTokensChange?: (tokens: Token[]) => void;\n private readonly onSelectionChange?: (\n range: { from: number; to: number } | null,\n ) => void;\n private baselineContent: string;\n\n constructor(options: EditorControllerOptions = {}) {\n const {\n element,\n content,\n plainTextMode = false,\n markdownMode = false,\n extensions = [],\n onPlainTextChange,\n onMarkdownChange,\n onTokensChange,\n onSelectionChange,\n ...editorOptions\n } = options;\n\n this.plainTextMode = plainTextMode;\n this.markdownMode = markdownMode;\n this.onPlainTextChange = onPlainTextChange;\n this.onMarkdownChange = onMarkdownChange;\n this.onTokensChange = onTokensChange;\n this.onSelectionChange = onSelectionChange;\n\n const initialContent =\n plainTextMode && typeof content === \"string\"\n ? plainTextToTipTapHtml(content)\n : markdownMode && typeof content === \"string\"\n ? markdownToTipTapHtml(content)\n : content;\n\n this.editor = new Editor({\n element: element || undefined,\n content: initialContent,\n extensions,\n ...editorOptions,\n onUpdate: (props) => {\n editorOptions.onUpdate?.(props);\n this.handleUpdate();\n },\n onSelectionUpdate: (props) => {\n editorOptions.onSelectionUpdate?.(props);\n this.handleSelectionUpdate();\n },\n });\n\n if (plainTextMode) {\n this.enforcePlainTextClipboard();\n }\n\n this.baselineContent = this.getCurrentContentString();\n }\n\n private enforcePlainTextClipboard() {\n // Opt-in rich clipboard / file import must not bypass the plain-text invariant.\n const richClipboard = this.editor.storage?.richClipboard;\n if (richClipboard) richClipboard.plainTextMode = true;\n const fileImport = this.editor.extensionManager?.extensions.find(\n (ext) => ext.name === \"fileImport\",\n );\n if (fileImport) fileImport.options.asPlainText = true;\n\n this.editor.setOptions({\n editorProps: {\n ...this.editor.options.editorProps,\n clipboardTextParser: (\n text: string,\n context: any,\n _plain: boolean,\n view?: any,\n ) => {\n const schema =\n context?.doc?.type?.schema ||\n context?.schema ||\n view?.state?.schema ||\n this.editor.schema;\n\n if (!schema) return Slice.empty;\n\n return plainTextToSlice(text, schema);\n },\n },\n });\n }\n\n private handleUpdate() {\n if (this.plainTextMode || this.onPlainTextChange || this.onTokensChange) {\n const text = this.getPlainText();\n this.onPlainTextChange?.(text);\n\n if (this.onTokensChange) {\n const tokens = tokenizePrompt(text);\n this.onTokensChange(tokens);\n }\n }\n\n if (this.markdownMode || this.onMarkdownChange) {\n const markdown = this.getMarkdown();\n this.onMarkdownChange?.(markdown);\n }\n }\n\n private handleSelectionUpdate() {\n if (this.onSelectionChange) {\n const { from, to } = this.editor.state.selection;\n this.onSelectionChange({ from, to });\n }\n }\n\n public getPlainText(): string {\n return getEditorText(this.editor);\n }\n\n public setPlainText(text: string, emitUpdate = true) {\n const html = plainTextToTipTapHtml(text);\n this.editor.commands.setContent(html, emitUpdate);\n }\n\n public getMarkdown(): string {\n return getEditorMarkdown(this.editor);\n }\n\n public setMarkdown(markdown: string, emitUpdate = true) {\n if (this.editor.storage?.markdown?.parser) {\n this.editor.commands.setContent(markdown, emitUpdate);\n } else {\n const html = markdownToTipTapHtml(markdown);\n this.editor.commands.setContent(html, emitUpdate);\n }\n }\n\n /**\n * Replaces `from`..`to` with literal text: HTML in `text` is not parsed, and\n * newlines split paragraphs.\n */\n private insertPlainText(from: number, to: number, text: string) {\n const { state, view } = this.editor;\n const tr = text.includes(\"\\n\") || text.includes(\"\\r\")\n ? state.tr.replace(from, to, plainTextToSlice(text, state.schema))\n : text\n ? state.tr.insertText(text, from, to)\n : state.tr.delete(from, to);\n view.dispatch(tr.scrollIntoView());\n }\n\n /** Inserts text literally at the selection; HTML in `text` is not parsed. */\n public insertText(text: string) {\n const { from, to } = this.editor.state.selection;\n this.insertPlainText(from, to, text);\n }\n\n /** Inserts text literally at `pos`; HTML in `text` is not parsed. */\n public insertTextAt(pos: number, text: string) {\n this.insertPlainText(pos, pos, text);\n this.editor.commands.focus();\n }\n\n /** Replaces a range with literal text; HTML in `text` is not parsed. */\n public replaceRange(from: number, to: number, text: string) {\n this.insertPlainText(from, to, text);\n this.editor.commands.focus();\n }\n\n public getCurrentContentString(): string {\n if (this.plainTextMode) return this.getPlainText();\n if (this.markdownMode) return this.getMarkdown();\n return this.editor.getHTML();\n }\n\n public isDirty(): boolean {\n return this.getCurrentContentString() !== this.baselineContent;\n }\n\n public setCheckpoint(content?: string): void {\n this.baselineContent =\n content !== undefined ? content : this.getCurrentContentString();\n }\n\n public resetDirty(): void {\n this.setCheckpoint();\n }\n\n public getBaselineContent(): string {\n return this.baselineContent;\n }\n\n public destroy() {\n this.editor.destroy();\n }\n}\n\n/**\n * Creates and initializes an EditorController instance.\n */\nexport function createEditor(\n options: EditorControllerOptions = {},\n): EditorController {\n return new EditorController(options);\n}\n","/**\n * Compares two values for structural equality.\n * Handles primitives, objects, arrays, and null/undefined.\n */\nexport function defaultIsEqual<T>(a: T, b: T): boolean {\n if (Object.is(a, b)) return true;\n if (\n typeof a !== \"object\" ||\n a === null ||\n typeof b !== \"object\" ||\n b === null\n ) {\n return false;\n }\n\n // Fast path for arrays\n if (Array.isArray(a) && Array.isArray(b)) {\n if (a.length !== b.length) return false;\n for (let i = 0; i < a.length; i++) {\n if (!defaultIsEqual(a[i], b[i])) return false;\n }\n return true;\n }\n if (Array.isArray(a) !== Array.isArray(b)) {\n return false;\n }\n\n const keysA = Object.keys(a) as (keyof T)[];\n const keysB = Object.keys(b) as (keyof T)[];\n\n if (keysA.length !== keysB.length) return false;\n\n for (const key of keysA) {\n if (!Object.prototype.hasOwnProperty.call(b, key)) return false;\n if (!defaultIsEqual(a[key], b[key])) return false;\n }\n\n return true;\n}\n\nexport interface DirtyTrackerOptions<T> {\n isEqual?: (a: T, b: T) => boolean;\n}\n\n/**\n * Tracks a baseline value and determines if the current state is dirty (modified).\n */\nexport class DirtyTracker<T = unknown> {\n private baseline: T;\n private readonly isEqual: (a: T, b: T) => boolean;\n\n constructor(\n initialBaseline: T,\n options?: DirtyTrackerOptions<T> | ((a: T, b: T) => boolean),\n ) {\n this.baseline = initialBaseline;\n if (typeof options === \"function\") {\n this.isEqual = options;\n } else {\n this.isEqual = options?.isEqual ?? defaultIsEqual;\n }\n }\n\n /**\n * Returns current baseline value.\n */\n public getBaseline(): T {\n return this.baseline;\n }\n\n /**\n * Sets a new baseline value without triggering any dirty state.\n */\n public setBaseline(value: T): void {\n this.baseline = value;\n }\n\n /**\n * Checks whether the given value differs from the baseline.\n */\n public isDirty(current: T): boolean {\n return !this.isEqual(this.baseline, current);\n }\n\n /**\n * Resets the baseline to the provided current value (or clears dirty status).\n */\n public reset(current: T): void {\n this.baseline = current;\n }\n}\n\n/**\n * Creates a DirtyTracker instance.\n */\nexport function createDirtyTracker<T>(\n initialBaseline: T,\n options?: DirtyTrackerOptions<T> | ((a: T, b: T) => boolean),\n): DirtyTracker<T> {\n return new DirtyTracker(initialBaseline, options);\n}\n","import { defaultImportRegistry, type DocumentImport } from \"./adapters\";\nimport {\n importFilesIntoEditor,\n type ImportEditorTarget,\n type ImportFilesIntoEditorOptions,\n} from \"./editor\";\nimport type { FileLike, ImportOutcome } from \"./registry\";\n\n/**\n * Opens the browser file picker and resolves with the chosen files (an empty\n * array when the dialog is cancelled). `accept` defaults to every extension\n * and MIME type the default registry can import as a document.\n */\nexport function pickImportFiles(options: { accept?: string; multiple?: boolean } = {}): Promise<File[]> {\n if (typeof document === \"undefined\") return Promise.resolve([]);\n return new Promise((resolve) => {\n const input = document.createElement(\"input\");\n input.type = \"file\";\n input.accept = options.accept ?? defaultImportRegistry.accept(\"document\");\n input.multiple = options.multiple ?? true;\n input.style.display = \"none\";\n const finish = (files: File[]) => {\n input.remove();\n resolve(files);\n };\n input.addEventListener(\"change\", () => finish(Array.from(input.files ?? [])), { once: true });\n input.addEventListener(\"cancel\", () => finish([]), { once: true });\n document.body.appendChild(input);\n input.click();\n });\n}\n\nexport interface FileImportState {\n isImporting: boolean;\n /** Last import error (cleared when a new import starts). */\n error: Error | null;\n /** Outcomes of the most recent import. */\n lastOutcomes: ImportOutcome<DocumentImport>[];\n}\n\nexport interface FileImportControllerOptions extends Omit<ImportFilesIntoEditorOptions, \"at\"> {\n /** Allow choosing several files in the picker (default `true`). */\n multiple?: boolean;\n}\n\n/**\n * Framework-agnostic state for a file-import button: tracks `isImporting` /\n * `error`, opens the file picker and inserts the imported content into the\n * editor returned by `getTarget`. React's `useFileImport` and Svelte's\n * `createFileImport` wrap it.\n */\nexport class FileImportController {\n private state: FileImportState = { isImporting: false, error: null, lastOutcomes: [] };\n private readonly listeners = new Set<(state: FileImportState) => void>();\n private pending = 0;\n\n constructor(\n private readonly getTarget: () => ImportEditorTarget | null | undefined,\n private options: FileImportControllerOptions = {},\n ) {}\n\n setOptions(options: FileImportControllerOptions): void {\n this.options = options;\n }\n\n /** `accept` attribute value for document imports. */\n get accept(): string {\n return (this.options.registry ?? defaultImportRegistry).accept(\"document\");\n }\n\n getState = (): FileImportState => this.state;\n\n subscribe = (listener: (state: FileImportState) => void): (() => void) => {\n this.listeners.add(listener);\n return () => {\n this.listeners.delete(listener);\n };\n };\n\n private setState(patch: Partial<FileImportState>) {\n this.state = { ...this.state, ...patch };\n this.listeners.forEach((listener) => listener(this.state));\n }\n\n /** Imports files into the editor at `at` (default: the selection). */\n importFiles = async (\n files: Iterable<FileLike> | ArrayLike<FileLike>,\n at?: number,\n ): Promise<ImportOutcome<DocumentImport>[]> => {\n const target = this.getTarget();\n const list = Array.from(files as ArrayLike<FileLike>);\n if (!target || list.length === 0) return [];\n this.pending++;\n this.setState({ isImporting: true, error: null });\n let error: Error | null = null;\n try {\n const outcomes = await importFilesIntoEditor(target, list, {\n ...this.options,\n at,\n onError: (err, file) => {\n error = err;\n this.options.onError?.(err, file);\n },\n });\n this.pending--;\n this.setState({ isImporting: this.pending > 0, error, lastOutcomes: outcomes });\n return outcomes;\n } catch (err) {\n this.pending--;\n this.setState({ isImporting: this.pending > 0, error: err instanceof Error ? err : new Error(String(err)) });\n return [];\n }\n };\n\n /** Opens the file picker and imports the chosen files at the selection. */\n openFilePicker = async (): Promise<ImportOutcome<DocumentImport>[]> => {\n const files = await pickImportFiles({ accept: this.accept, multiple: this.options.multiple });\n return this.importFiles(files);\n };\n}\n\nexport function createFileImportController(\n getTarget: () => ImportEditorTarget | null | undefined,\n options?: FileImportControllerOptions,\n): FileImportController {\n return new FileImportController(getTarget, options);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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AK;AACL,WAAK,SAAS,EAAE,aAAa,KAAK,UAAU,GAAG,OAAO,cAAc,SAAS,CAAC;AAC9E,aAAO;AAAA,IACT,SAAS,KAAK;AACZ,WAAK;AACL,WAAK,SAAS,EAAE,aAAa,KAAK,UAAU,GAAG,OAAO,eAAe,QAAQ,MAAM,IAAI,MAAM,OAAO,GAAG,CAAC,EAAE,CAAC;AAC3G,aAAO,CAAC;AAAA,IACV;AAAA,EACF;AAAA;AAAA,EAGA,iBAAiB,YAAsD;AACrE,UAAM,QAAQ,MAAM,gBAAgB,EAAE,QAAQ,KAAK,QAAQ,UAAU,KAAK,QAAQ,SAAS,CAAC;AAC5F,WAAO,KAAK,YAAY,KAAK;AAAA,EAC/B;AACF;AAEO,SAAS,2BACd,WACA,SACsB;AACtB,SAAO,IAAI,qBAAqB,WAAW,OAAO;AACpD;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../src/controller.ts","../src/import/controller.ts","../src/import/pdf.ts","../src/import/zip-export.ts"],"sourcesContent":["import { Editor, EditorOptions, Content, Extensions } from \"@tiptap/core\";\nimport { Slice } from \"@tiptap/pm/model\";\nimport { Token, tokenizePrompt } from \"./grammar\";\nimport {\n plainTextToTipTapHtml,\n plainTextToSlice,\n getEditorText,\n markdownToTipTapHtml,\n getEditorMarkdown,\n} from \"./serializers\";\n\nexport interface EditorControllerOptions extends Partial<\n Omit<EditorOptions, \"element\" | \"content\">\n> {\n element?: Element | HTMLElement | null;\n content?: string | Content;\n plainTextMode?: boolean;\n markdownMode?: boolean;\n extensions?: Extensions;\n onPlainTextChange?: (text: string) => void;\n onMarkdownChange?: (markdown: string) => void;\n onTokensChange?: (tokens: Token[]) => void;\n onSelectionChange?: (range: { from: number; to: number } | null) => void;\n}\n\n/**\n * UI-agnostic controller managing the lifecycle, commands, and plain-text/token\n * synchronization of a rich text or prompt editor.\n */\nexport class EditorController {\n public readonly editor: Editor;\n private readonly plainTextMode: boolean;\n private readonly markdownMode: boolean;\n private readonly onPlainTextChange?: (text: string) => void;\n private readonly onMarkdownChange?: (markdown: string) => void;\n private readonly onTokensChange?: (tokens: Token[]) => void;\n private readonly onSelectionChange?: (\n range: { from: number; to: number } | null,\n ) => void;\n private baselineContent: string;\n\n constructor(options: EditorControllerOptions = {}) {\n const {\n element,\n content,\n plainTextMode = false,\n markdownMode = false,\n extensions = [],\n onPlainTextChange,\n onMarkdownChange,\n onTokensChange,\n onSelectionChange,\n ...editorOptions\n } = options;\n\n this.plainTextMode = plainTextMode;\n this.markdownMode = markdownMode;\n this.onPlainTextChange = onPlainTextChange;\n this.onMarkdownChange = onMarkdownChange;\n this.onTokensChange = onTokensChange;\n this.onSelectionChange = onSelectionChange;\n\n const initialContent =\n plainTextMode && typeof content === \"string\"\n ? plainTextToTipTapHtml(content)\n : markdownMode && typeof content === \"string\"\n ? markdownToTipTapHtml(content)\n : content;\n\n this.editor = new Editor({\n element: element || undefined,\n content: initialContent,\n extensions,\n ...editorOptions,\n onUpdate: (props) => {\n editorOptions.onUpdate?.(props);\n this.handleUpdate();\n },\n onSelectionUpdate: (props) => {\n editorOptions.onSelectionUpdate?.(props);\n this.handleSelectionUpdate();\n },\n });\n\n if (plainTextMode) {\n this.enforcePlainTextClipboard();\n }\n\n this.baselineContent = this.getCurrentContentString();\n }\n\n private enforcePlainTextClipboard() {\n // Opt-in rich clipboard / file import must not bypass the plain-text invariant.\n const richClipboard = this.editor.storage?.richClipboard;\n if (richClipboard) richClipboard.plainTextMode = true;\n const fileImport = this.editor.extensionManager?.extensions.find(\n (ext) => ext.name === \"fileImport\",\n );\n if (fileImport) fileImport.options.asPlainText = true;\n\n this.editor.setOptions({\n editorProps: {\n ...this.editor.options.editorProps,\n clipboardTextParser: (\n text: string,\n context: any,\n _plain: boolean,\n view?: any,\n ) => {\n const schema =\n context?.doc?.type?.schema ||\n context?.schema ||\n view?.state?.schema ||\n this.editor.schema;\n\n if (!schema) return Slice.empty;\n\n return plainTextToSlice(text, schema);\n },\n },\n });\n }\n\n private handleUpdate() {\n if (this.plainTextMode || this.onPlainTextChange || this.onTokensChange) {\n const text = this.getPlainText();\n this.onPlainTextChange?.(text);\n\n if (this.onTokensChange) {\n const tokens = tokenizePrompt(text);\n this.onTokensChange(tokens);\n }\n }\n\n if (this.markdownMode || this.onMarkdownChange) {\n const markdown = this.getMarkdown();\n this.onMarkdownChange?.(markdown);\n }\n }\n\n private handleSelectionUpdate() {\n if (this.onSelectionChange) {\n const { from, to } = this.editor.state.selection;\n this.onSelectionChange({ from, to });\n }\n }\n\n public getPlainText(): string {\n return getEditorText(this.editor);\n }\n\n public setPlainText(text: string, emitUpdate = true) {\n const html = plainTextToTipTapHtml(text);\n this.editor.commands.setContent(html, emitUpdate);\n }\n\n public getMarkdown(): string {\n return getEditorMarkdown(this.editor);\n }\n\n public setMarkdown(markdown: string, emitUpdate = true) {\n if (this.editor.storage?.markdown?.parser) {\n this.editor.commands.setContent(markdown, emitUpdate);\n } else {\n const html = markdownToTipTapHtml(markdown);\n this.editor.commands.setContent(html, emitUpdate);\n }\n }\n\n /**\n * Replaces `from`..`to` with literal text: HTML in `text` is not parsed, and\n * newlines split paragraphs.\n */\n private insertPlainText(from: number, to: number, text: string) {\n const { state, view } = this.editor;\n const tr = text.includes(\"\\n\") || text.includes(\"\\r\")\n ? state.tr.replace(from, to, plainTextToSlice(text, state.schema))\n : text\n ? state.tr.insertText(text, from, to)\n : state.tr.delete(from, to);\n view.dispatch(tr.scrollIntoView());\n }\n\n /** Inserts text literally at the selection; HTML in `text` is not parsed. */\n public insertText(text: string) {\n const { from, to } = this.editor.state.selection;\n this.insertPlainText(from, to, text);\n }\n\n /** Inserts text literally at `pos`; HTML in `text` is not parsed. */\n public insertTextAt(pos: number, text: string) {\n this.insertPlainText(pos, pos, text);\n this.editor.commands.focus();\n }\n\n /** Replaces a range with literal text; HTML in `text` is not parsed. */\n public replaceRange(from: number, to: number, text: string) {\n this.insertPlainText(from, to, text);\n this.editor.commands.focus();\n }\n\n public getCurrentContentString(): string {\n if (this.plainTextMode) return this.getPlainText();\n if (this.markdownMode) return this.getMarkdown();\n return this.editor.getHTML();\n }\n\n public isDirty(): boolean {\n return this.getCurrentContentString() !== this.baselineContent;\n }\n\n public setCheckpoint(content?: string): void {\n this.baselineContent =\n content !== undefined ? content : this.getCurrentContentString();\n }\n\n public resetDirty(): void {\n this.setCheckpoint();\n }\n\n public getBaselineContent(): string {\n return this.baselineContent;\n }\n\n public destroy() {\n this.editor.destroy();\n }\n}\n\n/**\n * Creates and initializes an EditorController instance.\n */\nexport function createEditor(\n options: EditorControllerOptions = {},\n): EditorController {\n return new EditorController(options);\n}\n","import { defaultImportRegistry, type DocumentImport } from \"./adapters\";\nimport {\n importFilesIntoEditor,\n type ImportEditorTarget,\n type ImportFilesIntoEditorOptions,\n} from \"./editor\";\nimport type { FileLike, ImportOutcome } from \"./registry\";\n\n/**\n * Opens the browser file picker and resolves with the chosen files (an empty\n * array when the dialog is cancelled). `accept` defaults to every extension\n * and MIME type the default registry can import as a document.\n */\nexport function pickImportFiles(options: { accept?: string; multiple?: boolean } = {}): Promise<File[]> {\n if (typeof document === \"undefined\") return Promise.resolve([]);\n return new Promise((resolve) => {\n const input = document.createElement(\"input\");\n input.type = \"file\";\n input.accept = options.accept ?? defaultImportRegistry.accept(\"document\");\n input.multiple = options.multiple ?? true;\n input.style.display = \"none\";\n const finish = (files: File[]) => {\n input.remove();\n resolve(files);\n };\n input.addEventListener(\"change\", () => finish(Array.from(input.files ?? [])), { once: true });\n input.addEventListener(\"cancel\", () => finish([]), { once: true });\n document.body.appendChild(input);\n input.click();\n });\n}\n\nexport interface FileImportState {\n isImporting: boolean;\n /** Last import error (cleared when a new import starts). */\n error: Error | null;\n /** Outcomes of the most recent import. */\n lastOutcomes: ImportOutcome<DocumentImport>[];\n}\n\nexport interface FileImportControllerOptions extends Omit<ImportFilesIntoEditorOptions, \"at\"> {\n /** Allow choosing several files in the picker (default `true`). */\n multiple?: boolean;\n}\n\n/**\n * Framework-agnostic state for a file-import button: tracks `isImporting` /\n * `error`, opens the file picker and inserts the imported content into the\n * editor returned by `getTarget`. React's `useFileImport` and Svelte's\n * `createFileImport` wrap it.\n */\nexport class FileImportController {\n private state: FileImportState = { isImporting: false, error: null, lastOutcomes: [] };\n private readonly listeners = new Set<(state: FileImportState) => void>();\n private pending = 0;\n\n constructor(\n private readonly getTarget: () => ImportEditorTarget | null | undefined,\n private options: FileImportControllerOptions = {},\n ) {}\n\n setOptions(options: FileImportControllerOptions): void {\n this.options = options;\n }\n\n /** `accept` attribute value for document imports. */\n get accept(): string {\n return (this.options.registry ?? defaultImportRegistry).accept(\"document\");\n }\n\n getState = (): FileImportState => this.state;\n\n subscribe = (listener: (state: FileImportState) => void): (() => void) => {\n this.listeners.add(listener);\n return () => {\n this.listeners.delete(listener);\n };\n };\n\n private setState(patch: Partial<FileImportState>) {\n this.state = { ...this.state, ...patch };\n this.listeners.forEach((listener) => listener(this.state));\n }\n\n /** Imports files into the editor at `at` (default: the selection). */\n importFiles = async (\n files: Iterable<FileLike> | ArrayLike<FileLike>,\n at?: number,\n ): Promise<ImportOutcome<DocumentImport>[]> => {\n const target = this.getTarget();\n const list = Array.from(files as ArrayLike<FileLike>);\n if (!target || list.length === 0) return [];\n this.pending++;\n this.setState({ isImporting: true, error: null });\n let error: Error | null = null;\n try {\n const outcomes = await importFilesIntoEditor(target, list, {\n ...this.options,\n at,\n onError: (err, file) => {\n error = err;\n this.options.onError?.(err, file);\n },\n });\n this.pending--;\n this.setState({ isImporting: this.pending > 0, error, lastOutcomes: outcomes });\n return outcomes;\n } catch (err) {\n this.pending--;\n this.setState({ isImporting: this.pending > 0, error: err instanceof Error ? err : new Error(String(err)) });\n return [];\n }\n };\n\n /** Opens the file picker and imports the chosen files at the selection. */\n openFilePicker = async (): Promise<ImportOutcome<DocumentImport>[]> => {\n const files = await pickImportFiles({ accept: this.accept, multiple: this.options.multiple });\n return this.importFiles(files);\n };\n}\n\nexport function createFileImportController(\n getTarget: () => ImportEditorTarget | null | undefined,\n options?: FileImportControllerOptions,\n): FileImportController {\n return new FileImportController(getTarget, options);\n}\n","import { plainTextToParagraphHtml } from \"../serializers/html\";\nimport type { DocumentImportAdapter } from \"./adapters\";\n\n/**\n * PDF text import through an injected pdf.js-compatible library. The editor\n * ships no PDF parser; consumers that already bundle `pdfjs-dist` register\n * the adapter themselves:\n *\n * ```ts\n * import * as pdfjs from \"pdfjs-dist\";\n * registerImportAdapter(createPdfImportAdapter(pdfjs));\n * ```\n *\n * Text items are grouped into lines by baseline (`transform[5]`) and lines\n * into paragraphs by vertical gaps larger than the page's typical line\n * spacing (or blank `hasEOL` lines). Only text is imported: no images,\n * tables, links or styling.\n */\n\n/** A text item from `page.getTextContent()` (marked-content items have no `str`). */\nexport interface PdfJsTextItem {\n str: string;\n /** `[a, b, c, d, x, y]` transform; `x` / `y` are the baseline origin. */\n transform?: readonly number[];\n width?: number;\n height?: number;\n /** pdf.js sets this when a line break follows the item. */\n hasEOL?: boolean;\n}\n\nexport interface PdfJsTextContent {\n items: ReadonlyArray<PdfJsTextItem | { type: string }>;\n}\n\nexport interface PdfJsPage {\n getTextContent(): Promise<PdfJsTextContent>;\n cleanup?(): unknown;\n}\n\nexport interface PdfJsDocument {\n numPages: number;\n getPage(pageNumber: number): Promise<PdfJsPage>;\n destroy?(): unknown;\n}\n\n/** The subset of the pdf.js API the adapter uses (`pdfjs-dist` satisfies it). */\nexport interface PdfJsLike {\n getDocument(source: { data: Uint8Array; [option: string]: unknown }): { promise: Promise<PdfJsDocument> };\n}\n\nexport interface PdfTextOptions {\n /**\n * A line gap larger than this multiple of the page's typical line spacing\n * starts a new paragraph (default `1.4`).\n */\n paragraphGap?: number;\n}\n\nexport interface PdfImportOptions extends PdfTextOptions {\n /** Adapter id (default `\"pdf\"`). */\n id?: string;\n /** Import at most this many pages (default: all). */\n maxPages?: number;\n /** Extra `getDocument` parameters, e.g. `{ disableFontFace: true }`. */\n documentParams?: Record<string, unknown>;\n}\n\ninterface Line {\n text: string;\n y: number;\n height: number;\n /** A blank line (empty `hasEOL` item) follows this one. */\n blankAfter: boolean;\n}\n\nconst isTextItem = (item: PdfJsTextItem | { type: string }): item is PdfJsTextItem =>\n typeof (item as PdfJsTextItem).str === \"string\";\n\nfunction median(values: number[]): number {\n if (!values.length) return 0;\n const sorted = [...values].sort((a, b) => a - b);\n const mid = sorted.length >> 1;\n return sorted.length % 2 ? sorted[mid] : (sorted[mid - 1] + sorted[mid]) / 2;\n}\n\nfunction groupLines(items: ReadonlyArray<PdfJsTextItem | { type: string }>): Line[] {\n const lines: Line[] = [];\n let current: (Line & { endX?: number }) | null = null;\n let breakNext = false;\n for (const item of items) {\n if (!isTextItem(item)) continue;\n const x = item.transform?.[4] ?? 0;\n const y: number = item.transform?.[5] ?? (current?.y ?? 0);\n const height: number = item.height || Math.abs(item.transform?.[3] ?? 0) || current?.height || 10;\n const tolerance = Math.max(1, height * 0.5);\n if (!item.str) {\n // Empty items mark line ends; two in a row mean a blank line.\n if (item.hasEOL) {\n if (breakNext && current) current.blankAfter = true;\n breakNext = true;\n }\n continue;\n }\n if (!current || breakNext || Math.abs(y - current.y) > tolerance) {\n current = { text: \"\", y, height, blankAfter: false };\n lines.push(current);\n } else if (current.endX !== undefined && x - current.endX > height * 0.15) {\n // A horizontal gap without an explicit space item.\n if (!/\\s$/.test(current.text) && !/^\\s/.test(item.str)) current.text += \" \";\n }\n current.text += item.str;\n current.height = Math.max(current.height, height);\n current.endX = item.width !== undefined ? x + item.width : undefined;\n breakNext = !!item.hasEOL;\n }\n return lines;\n}\n\n/**\n * Groups one page's pdf.js text items into paragraphs. Exposed for custom\n * adapters and tests.\n */\nexport function pdfTextItemsToParagraphs(\n items: ReadonlyArray<PdfJsTextItem | { type: string }>,\n options: PdfTextOptions = {},\n): string[] {\n const lines = groupLines(items).filter((line) => line.text.trim() || line.blankAfter);\n const gaps: number[] = [];\n for (let i = 1; i < lines.length; i++) {\n const gap = lines[i - 1].y - lines[i].y;\n if (gap > 0) gaps.push(gap);\n }\n const typical = median(gaps);\n const factor = options.paragraphGap ?? 1.4;\n\n const paragraphs: string[] = [];\n let current: string[] = [];\n const flush = () => {\n const text = current.join(\" \").replace(/\\s+/g, \" \").trim();\n if (text) paragraphs.push(text);\n current = [];\n };\n lines.forEach((line, i) => {\n if (i > 0) {\n const prev = lines[i - 1];\n const gap = prev.y - line.y;\n const bigGap = typical > 0 ? gap > typical * factor : gap > prev.height * 1.8;\n // A line above the previous one (gap < 0) starts a new column or block.\n if (prev.blankAfter || bigGap || gap < -prev.height * 0.5) flush();\n }\n const text = line.text.trim();\n if (!text) return;\n if (current.length && /\\w-$/.test(current[current.length - 1]) && /^[a-z]/.test(text)) {\n // Re-join words hyphenated across lines.\n current[current.length - 1] = current[current.length - 1].slice(0, -1) + text;\n } else {\n current.push(text);\n }\n });\n flush();\n return paragraphs;\n}\n\n/** Creates a PDF `document` adapter backed by an injected pdf.js-compatible library. */\nexport function createPdfImportAdapter(pdfjs: PdfJsLike, options: PdfImportOptions = {}): DocumentImportAdapter {\n return {\n id: options.id ?? \"pdf\",\n label: \"PDF\",\n mimeTypes: [\"application/pdf\"],\n extensions: [\".pdf\"],\n target: \"document\",\n async import(_input, context) {\n // pdf.js may transfer (detach) the buffer it receives, so pass a copy.\n const data = new Uint8Array((await context.readArrayBuffer()).slice(0));\n const doc = await pdfjs.getDocument({ ...options.documentParams, data }).promise;\n const paragraphs: string[] = [];\n try {\n const pages = Math.min(doc.numPages, options.maxPages ?? Infinity);\n for (let n = 1; n <= pages; n++) {\n const page = await doc.getPage(n);\n const content = await page.getTextContent();\n paragraphs.push(...pdfTextItemsToParagraphs(content.items, options));\n page.cleanup?.();\n }\n } finally {\n await doc.destroy?.();\n }\n const text = paragraphs.join(\"\\n\");\n return { html: plainTextToParagraphHtml(text), text };\n },\n };\n}\n","import { escapeHtml } from \"../serializers/plain-text\";\nimport { defaultImportRegistry, type DocumentImport, type DocumentImportAdapter } from \"./adapters\";\nimport { ImportError, type ImportAdapterRegistry } from \"./registry\";\nimport { readZip, type ZipReadOptions } from \"./zip\";\n\n/**\n * Zipped document exports (Notion, Confluence, Obsidian, static-site dumps):\n * a `.zip` of markdown / HTML / text / DOCX files. Each entry is imported\n * with the registry's other `document` adapters and the results are\n * concatenated, with a heading per file.\n *\n * Not registered by default: `.zip` is a generic container, and claiming\n * every dropped archive as a document would steal drops from other handlers\n * (e.g. attachment uploads). Opt in with\n * `registerImportAdapter(zipImportAdapter)`.\n */\n\nexport interface ZipDocumentImport extends DocumentImport {\n /** Imported entries in document order, with the adapter that handled each. */\n files: { name: string; adapterId: string }[];\n /** Entries that were left out, with the reason. */\n skipped: { name: string; reason: string }[];\n}\n\nexport interface ZipImportOptions {\n /** Adapter id (default `\"zip\"`). */\n id?: string;\n /**\n * Registry used for the entries. Defaults to the registry running the\n * import (`context.registry`), then the default registry.\n */\n registry?: ImportAdapterRegistry;\n /** Add a heading with each file's title before its content (default `true`). */\n headings?: boolean;\n /** Heading level for file titles (default `2`). */\n headingLevel?: 1 | 2 | 3 | 4 | 5 | 6;\n /** Import at most this many files (default 500); the rest are reported as skipped. */\n maxFiles?: number;\n /** Archive limits (default: 100 MiB extracted, 5 000 entries). */\n zip?: ZipReadOptions;\n}\n\nconst INDEX_NAMES = /^(index|readme|home|_index|start)$/i;\n\nconst baseName = (path: string) => path.slice(path.lastIndexOf(\"/\") + 1);\nconst stem = (name: string) => name.replace(/\\.[^./]+$/, \"\");\n\n/** File title: the name without extension, path or a trailing Notion-style id. */\nexport function zipEntryTitle(path: string): string {\n const title = stem(baseName(path))\n .replace(/\\s+[0-9a-f]{32}$/i, \"\")\n .replace(/[_]+/g, \" \")\n .trim();\n return title || baseName(path);\n}\n\n/**\n * Orders entries for import: index / README files first (shallowest first),\n * then by path with natural number ordering (`2` before `10`).\n */\nexport function sortZipEntryNames(names: readonly string[]): string[] {\n const isIndex = (name: string) => INDEX_NAMES.test(stem(baseName(name)));\n const depth = (name: string) => name.split(\"/\").length;\n return [...names].sort((a, b) => {\n const ai = isIndex(a);\n const bi = isIndex(b);\n if (ai !== bi) return ai ? -1 : 1;\n if (ai && bi && depth(a) !== depth(b)) return depth(a) - depth(b);\n return a.localeCompare(b, undefined, { numeric: true, sensitivity: \"base\" });\n });\n}\n\nconst isJunk = (name: string) =>\n name.startsWith(\"__MACOSX/\") || name.split(\"/\").some((segment) => segment.startsWith(\".\"));\n\n/** Creates a `document` adapter for zipped exports. */\nexport function createZipImportAdapter(options: ZipImportOptions = {}): DocumentImportAdapter {\n const id = options.id ?? \"zip\";\n return {\n id,\n label: \"Zipped documents\",\n mimeTypes: [\"application/zip\", \"application/x-zip-compressed\"],\n extensions: [\".zip\"],\n target: \"document\",\n async import(_input, context): Promise<ZipDocumentImport> {\n const registry = options.registry ?? context.registry ?? defaultImportRegistry;\n const zip = await readZip(await context.readArrayBuffer(), {\n maxTotalSize: 100 * 1024 * 1024,\n maxEntries: 5_000,\n ...options.zip,\n });\n const maxFiles = options.maxFiles ?? 500;\n const level = options.headingLevel ?? 2;\n const files: ZipDocumentImport[\"files\"] = [];\n const skipped: ZipDocumentImport[\"skipped\"] = [];\n let html = \"\";\n let text = \"\";\n\n const names = zip.entries.filter((entry) => !entry.isDirectory).map((entry) => entry.name);\n for (const name of sortZipEntryNames(names)) {\n if (isJunk(name)) continue;\n const adapter = registry.find({ fileName: name, target: \"document\" });\n if (!adapter) {\n skipped.push({ name, reason: \"unsupported type\" });\n continue;\n }\n if (adapter.id === id || adapter.extensions.some((ext) => ext.toLowerCase() === \".zip\")) {\n skipped.push({ name, reason: \"nested archive\" });\n continue;\n }\n if (files.length >= maxFiles) {\n skipped.push({ name, reason: \"file limit reached\" });\n continue;\n }\n try {\n const outcome = await registry.importFile<DocumentImport>(await zip.read(name), {\n fileName: name,\n adapterId: adapter.id,\n });\n const title = zipEntryTitle(name);\n if (options.headings !== false) {\n html += `<h${level}>${escapeHtml(title)}</h${level}>`;\n text += `${text ? \"\\n\" : \"\"}${title}\\n`;\n } else if (text) {\n text += \"\\n\";\n }\n html += outcome.result.html.trim();\n text += outcome.result.text;\n files.push({ name, adapterId: adapter.id });\n } catch (error) {\n // Archive-wide limits stop the whole import; other failures skip the entry.\n const code = (error as { code?: string })?.code;\n if (code === \"too_large\") throw error;\n skipped.push({ name, reason: error instanceof Error ? error.message : String(error) });\n }\n }\n if (!files.length) {\n throw new ImportError(\"invalid_file\", \"The archive contains no importable documents.\", {\n fileName: context.fileName,\n });\n }\n return { html, text, files, skipped };\n },\n };\n}\n\n/** Zipped-export adapter with default options. Opt in with `registerImportAdapter(zipImportAdapter)`. */\nexport const zipImportAdapter: DocumentImportAdapter = createZipImportAdapter();\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,SAAS,cAAkD;AAC3D,SAAS,aAAa;AA4Bf,IAAM,mBAAN,MAAuB;AAAA,EACZ;AAAA,EACC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EAGT;AAAA,EAER,YAAY,UAAmC,CAAC,GAAG;AACjD,UAAM;AAAA,MACJ;AAAA,MACA;AAAA,MACA,gBAAgB;AAAA,MAChB,eAAe;AAAA,MACf,aAAa,CAAC;AAAA,MACd;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA,GAAG;AAAA,IACL,IAAI;AAEJ,SAAK,gBAAgB;AACrB,SAAK,eAAe;AACpB,SAAK,oBAAoB;AACzB,SAAK,mBAAmB;AACxB,SAAK,iBAAiB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[]}
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* JSON-Schema-lite: the subset of JSON Schema used by MCP tool `inputSchema`s,
|
|
3
|
+
* plus a small validator for tool-call arguments. Unknown keywords are ignored.
|
|
4
|
+
*/
|
|
5
|
+
type JsonSchemaType = 'string' | 'number' | 'integer' | 'boolean' | 'object' | 'array' | 'null';
|
|
6
|
+
interface JsonSchema {
|
|
7
|
+
type?: JsonSchemaType | readonly JsonSchemaType[];
|
|
8
|
+
description?: string;
|
|
9
|
+
title?: string;
|
|
10
|
+
enum?: readonly unknown[];
|
|
11
|
+
const?: unknown;
|
|
12
|
+
default?: unknown;
|
|
13
|
+
properties?: {
|
|
14
|
+
readonly [key: string]: JsonSchema;
|
|
15
|
+
};
|
|
16
|
+
required?: readonly string[];
|
|
17
|
+
additionalProperties?: boolean | JsonSchema;
|
|
18
|
+
items?: JsonSchema;
|
|
19
|
+
minItems?: number;
|
|
20
|
+
maxItems?: number;
|
|
21
|
+
minimum?: number;
|
|
22
|
+
maximum?: number;
|
|
23
|
+
minLength?: number;
|
|
24
|
+
maxLength?: number;
|
|
25
|
+
pattern?: string;
|
|
26
|
+
[keyword: string]: unknown;
|
|
27
|
+
}
|
|
28
|
+
interface SchemaValidationError {
|
|
29
|
+
/** JSON-pointer-like path to the offending value, e.g. `/node/id` (`""` is the root). */
|
|
30
|
+
path: string;
|
|
31
|
+
message: string;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Validates `value` against `schema` and returns every error found (an empty
|
|
35
|
+
* array means valid). Supports `type` (incl. arrays of types), `enum`, `const`,
|
|
36
|
+
* `properties`, `required`, `additionalProperties`, `items`, `minItems`,
|
|
37
|
+
* `maxItems`, `minimum`, `maximum`, `minLength`, `maxLength` and `pattern`.
|
|
38
|
+
*/
|
|
39
|
+
declare function validateJsonSchema(value: unknown, schema: JsonSchema | undefined, path?: string): SchemaValidationError[];
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* MCP-shaped tool definition, as returned by `tools/list`.
|
|
43
|
+
*/
|
|
44
|
+
interface McpToolDefinition {
|
|
45
|
+
name: string;
|
|
46
|
+
title?: string;
|
|
47
|
+
description?: string;
|
|
48
|
+
inputSchema: JsonSchema & {
|
|
49
|
+
type: 'object';
|
|
50
|
+
};
|
|
51
|
+
outputSchema?: JsonSchema;
|
|
52
|
+
annotations?: {
|
|
53
|
+
title?: string;
|
|
54
|
+
readOnlyHint?: boolean;
|
|
55
|
+
destructiveHint?: boolean;
|
|
56
|
+
idempotentHint?: boolean;
|
|
57
|
+
openWorldHint?: boolean;
|
|
58
|
+
[key: string]: unknown;
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
type McpToolContent = {
|
|
62
|
+
type: 'text';
|
|
63
|
+
text: string;
|
|
64
|
+
} | {
|
|
65
|
+
type: 'image';
|
|
66
|
+
data: string;
|
|
67
|
+
mimeType: string;
|
|
68
|
+
} | {
|
|
69
|
+
type: string;
|
|
70
|
+
[key: string]: unknown;
|
|
71
|
+
};
|
|
72
|
+
/** MCP `tools/call` result. */
|
|
73
|
+
interface McpToolResult {
|
|
74
|
+
content: McpToolContent[];
|
|
75
|
+
structuredContent?: Record<string, unknown>;
|
|
76
|
+
isError?: boolean;
|
|
77
|
+
}
|
|
78
|
+
/** MCP `tools/list` result. */
|
|
79
|
+
interface McpListToolsResult {
|
|
80
|
+
tools: McpToolDefinition[];
|
|
81
|
+
}
|
|
82
|
+
interface ToolCallContext {
|
|
83
|
+
/** Name the tool was called by (possibly namespaced). */
|
|
84
|
+
name: string;
|
|
85
|
+
/** Id of the provider that owns the tool. */
|
|
86
|
+
providerId: string;
|
|
87
|
+
signal?: AbortSignal;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Handler return values are normalized into an {@link McpToolResult}: a full
|
|
91
|
+
* result (`{ content: [...] }`) is passed through, a string becomes a text
|
|
92
|
+
* block, `undefined` becomes an empty result, and anything else is returned
|
|
93
|
+
* as `structuredContent` (plain objects) plus a JSON text block.
|
|
94
|
+
*/
|
|
95
|
+
type AgentToolHandler = (args: Record<string, any>, context: ToolCallContext) => unknown | Promise<unknown>;
|
|
96
|
+
interface AgentTool extends McpToolDefinition {
|
|
97
|
+
handler: AgentToolHandler;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* A source of tools, typically one per mounted editor, spreadsheet or canvas.
|
|
101
|
+
*/
|
|
102
|
+
interface AgentToolProvider {
|
|
103
|
+
/** Unique id within a registry, e.g. `doc-1`. Used as the namespace on collisions. */
|
|
104
|
+
id: string;
|
|
105
|
+
/** Always prefix this provider's tools with `<namespace>__`. */
|
|
106
|
+
namespace?: string;
|
|
107
|
+
/** Tools, or a function returning the current tools (re-read on every `listTools`). */
|
|
108
|
+
tools: readonly AgentTool[] | (() => readonly AgentTool[]);
|
|
109
|
+
/** Lets the provider signal that its tool list changed; returns an unsubscribe function. */
|
|
110
|
+
subscribe?: (notify: () => void) => () => void;
|
|
111
|
+
}
|
|
112
|
+
type ToolErrorCode = 'unknown_tool' | 'invalid_arguments' | 'tool_failed';
|
|
113
|
+
/** Structured error carried in `structuredContent.error` of a failed call. */
|
|
114
|
+
interface ToolCallError {
|
|
115
|
+
code: ToolErrorCode;
|
|
116
|
+
message: string;
|
|
117
|
+
tool: string;
|
|
118
|
+
details?: SchemaValidationError[];
|
|
119
|
+
}
|
|
120
|
+
declare class ToolRegistryError extends Error {
|
|
121
|
+
readonly code: 'duplicate_provider' | 'name_collision' | 'invalid_tool';
|
|
122
|
+
constructor(code: ToolRegistryError['code'], message: string);
|
|
123
|
+
}
|
|
124
|
+
interface ToolRegistryOptions {
|
|
125
|
+
/**
|
|
126
|
+
* What to do when two providers contribute the same tool name:
|
|
127
|
+
* `namespace` (default) exposes every colliding tool as `<providerId>__<name>`;
|
|
128
|
+
* `error` makes `register` throw.
|
|
129
|
+
*/
|
|
130
|
+
onCollision?: 'namespace' | 'error';
|
|
131
|
+
/** Coalescing window for `listChanged` notifications in ms (default 0: same tick). */
|
|
132
|
+
listChangedDelayMs?: number;
|
|
133
|
+
/** Validate arguments against `inputSchema` before calling handlers (default `true`). */
|
|
134
|
+
validateArguments?: boolean;
|
|
135
|
+
}
|
|
136
|
+
/** Separator between namespace and tool name; valid in MCP and LLM tool-name charsets. */
|
|
137
|
+
declare const TOOL_NAMESPACE_SEPARATOR = "__";
|
|
138
|
+
/** Normalizes a handler's return value into an MCP `tools/call` result. */
|
|
139
|
+
declare function toToolResult(value: unknown): McpToolResult;
|
|
140
|
+
/**
|
|
141
|
+
* Transport-agnostic registry of agent tools contributed by live providers.
|
|
142
|
+
*
|
|
143
|
+
* `listTools()` and `callTool()` return MCP `tools/list` / `tools/call`
|
|
144
|
+
* result shapes, and `onListChanged` fires (coalesced) whenever the exposed
|
|
145
|
+
* tool list changes, mirroring MCP's `notifications/tools/list_changed`, so an
|
|
146
|
+
* MCP server can be a thin wrapper around it.
|
|
147
|
+
*/
|
|
148
|
+
declare class ToolRegistry {
|
|
149
|
+
/** MCP server capabilities this registry supports. */
|
|
150
|
+
readonly capabilities: {
|
|
151
|
+
readonly tools: {
|
|
152
|
+
readonly listChanged: true;
|
|
153
|
+
};
|
|
154
|
+
};
|
|
155
|
+
private readonly entries;
|
|
156
|
+
private readonly listeners;
|
|
157
|
+
private readonly options;
|
|
158
|
+
private timer;
|
|
159
|
+
private lastSignature;
|
|
160
|
+
constructor(options?: ToolRegistryOptions);
|
|
161
|
+
/**
|
|
162
|
+
* Registers a provider and returns a function that unregisters it.
|
|
163
|
+
* Throws a {@link ToolRegistryError} for a duplicate provider id, an invalid
|
|
164
|
+
* tool name, or (with `onCollision: 'error'`) a tool name collision.
|
|
165
|
+
*/
|
|
166
|
+
register(provider: AgentToolProvider): () => void;
|
|
167
|
+
/** Removes a provider by id or instance. Returns whether it was registered. */
|
|
168
|
+
unregister(providerOrId: string | AgentToolProvider): boolean;
|
|
169
|
+
/** Registered provider ids in registration order. */
|
|
170
|
+
providerIds(): string[];
|
|
171
|
+
/**
|
|
172
|
+
* Resolves exposed names. Tools are ordered by provider registration, then
|
|
173
|
+
* by their order within the provider.
|
|
174
|
+
*/
|
|
175
|
+
private resolve;
|
|
176
|
+
/** MCP `tools/list` result for the current providers. */
|
|
177
|
+
listTools(): McpListToolsResult;
|
|
178
|
+
/** Whether a tool with this exposed name exists. */
|
|
179
|
+
hasTool(name: string): boolean;
|
|
180
|
+
/** Looks up a tool definition by exposed name. */
|
|
181
|
+
getTool(name: string): McpToolDefinition | undefined;
|
|
182
|
+
/**
|
|
183
|
+
* Calls a tool by exposed name and returns an MCP `tools/call` result.
|
|
184
|
+
* Never rejects: unknown tools, invalid arguments and handler errors come
|
|
185
|
+
* back as `isError: true` results with a {@link ToolCallError} in
|
|
186
|
+
* `structuredContent.error` (MCP transports may map `unknown_tool` to a
|
|
187
|
+
* JSON-RPC `-32602` error instead).
|
|
188
|
+
*/
|
|
189
|
+
callTool(name: string, args?: Record<string, unknown>, options?: {
|
|
190
|
+
signal?: AbortSignal;
|
|
191
|
+
}): Promise<McpToolResult>;
|
|
192
|
+
/**
|
|
193
|
+
* Subscribes to tool-list changes. Notifications are coalesced: many
|
|
194
|
+
* registrations or provider signals within the delay window produce one
|
|
195
|
+
* call, and none if the exposed list ends up unchanged (e.g. a provider
|
|
196
|
+
* unmounted and remounted). Returns an unsubscribe function.
|
|
197
|
+
*/
|
|
198
|
+
onListChanged(listener: (tools: McpToolDefinition[]) => void): () => void;
|
|
199
|
+
/** Schedules a coalesced `listChanged` check; providers may call it directly. */
|
|
200
|
+
notifyListChanged(): void;
|
|
201
|
+
/** Runs a pending `listChanged` check immediately. */
|
|
202
|
+
flush(): void;
|
|
203
|
+
private signature;
|
|
204
|
+
/** Unregisters every provider and listener and cancels pending notifications. */
|
|
205
|
+
dispose(): void;
|
|
206
|
+
}
|
|
207
|
+
declare function createToolRegistry(options?: ToolRegistryOptions): ToolRegistry;
|
|
208
|
+
|
|
209
|
+
export { type AgentToolProvider as A, type JsonSchema as J, type McpListToolsResult as M, type SchemaValidationError as S, TOOL_NAMESPACE_SEPARATOR as T, type AgentTool as a, type AgentToolHandler as b, type JsonSchemaType as c, type McpToolContent as d, type McpToolDefinition as e, type McpToolResult as f, type ToolCallContext as g, type ToolCallError as h, type ToolErrorCode as i, ToolRegistry as j, ToolRegistryError as k, type ToolRegistryOptions as l, createToolRegistry as m, toToolResult as t, validateJsonSchema as v };
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* JSON-Schema-lite: the subset of JSON Schema used by MCP tool `inputSchema`s,
|
|
3
|
+
* plus a small validator for tool-call arguments. Unknown keywords are ignored.
|
|
4
|
+
*/
|
|
5
|
+
type JsonSchemaType = 'string' | 'number' | 'integer' | 'boolean' | 'object' | 'array' | 'null';
|
|
6
|
+
interface JsonSchema {
|
|
7
|
+
type?: JsonSchemaType | readonly JsonSchemaType[];
|
|
8
|
+
description?: string;
|
|
9
|
+
title?: string;
|
|
10
|
+
enum?: readonly unknown[];
|
|
11
|
+
const?: unknown;
|
|
12
|
+
default?: unknown;
|
|
13
|
+
properties?: {
|
|
14
|
+
readonly [key: string]: JsonSchema;
|
|
15
|
+
};
|
|
16
|
+
required?: readonly string[];
|
|
17
|
+
additionalProperties?: boolean | JsonSchema;
|
|
18
|
+
items?: JsonSchema;
|
|
19
|
+
minItems?: number;
|
|
20
|
+
maxItems?: number;
|
|
21
|
+
minimum?: number;
|
|
22
|
+
maximum?: number;
|
|
23
|
+
minLength?: number;
|
|
24
|
+
maxLength?: number;
|
|
25
|
+
pattern?: string;
|
|
26
|
+
[keyword: string]: unknown;
|
|
27
|
+
}
|
|
28
|
+
interface SchemaValidationError {
|
|
29
|
+
/** JSON-pointer-like path to the offending value, e.g. `/node/id` (`""` is the root). */
|
|
30
|
+
path: string;
|
|
31
|
+
message: string;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Validates `value` against `schema` and returns every error found (an empty
|
|
35
|
+
* array means valid). Supports `type` (incl. arrays of types), `enum`, `const`,
|
|
36
|
+
* `properties`, `required`, `additionalProperties`, `items`, `minItems`,
|
|
37
|
+
* `maxItems`, `minimum`, `maximum`, `minLength`, `maxLength` and `pattern`.
|
|
38
|
+
*/
|
|
39
|
+
declare function validateJsonSchema(value: unknown, schema: JsonSchema | undefined, path?: string): SchemaValidationError[];
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* MCP-shaped tool definition, as returned by `tools/list`.
|
|
43
|
+
*/
|
|
44
|
+
interface McpToolDefinition {
|
|
45
|
+
name: string;
|
|
46
|
+
title?: string;
|
|
47
|
+
description?: string;
|
|
48
|
+
inputSchema: JsonSchema & {
|
|
49
|
+
type: 'object';
|
|
50
|
+
};
|
|
51
|
+
outputSchema?: JsonSchema;
|
|
52
|
+
annotations?: {
|
|
53
|
+
title?: string;
|
|
54
|
+
readOnlyHint?: boolean;
|
|
55
|
+
destructiveHint?: boolean;
|
|
56
|
+
idempotentHint?: boolean;
|
|
57
|
+
openWorldHint?: boolean;
|
|
58
|
+
[key: string]: unknown;
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
type McpToolContent = {
|
|
62
|
+
type: 'text';
|
|
63
|
+
text: string;
|
|
64
|
+
} | {
|
|
65
|
+
type: 'image';
|
|
66
|
+
data: string;
|
|
67
|
+
mimeType: string;
|
|
68
|
+
} | {
|
|
69
|
+
type: string;
|
|
70
|
+
[key: string]: unknown;
|
|
71
|
+
};
|
|
72
|
+
/** MCP `tools/call` result. */
|
|
73
|
+
interface McpToolResult {
|
|
74
|
+
content: McpToolContent[];
|
|
75
|
+
structuredContent?: Record<string, unknown>;
|
|
76
|
+
isError?: boolean;
|
|
77
|
+
}
|
|
78
|
+
/** MCP `tools/list` result. */
|
|
79
|
+
interface McpListToolsResult {
|
|
80
|
+
tools: McpToolDefinition[];
|
|
81
|
+
}
|
|
82
|
+
interface ToolCallContext {
|
|
83
|
+
/** Name the tool was called by (possibly namespaced). */
|
|
84
|
+
name: string;
|
|
85
|
+
/** Id of the provider that owns the tool. */
|
|
86
|
+
providerId: string;
|
|
87
|
+
signal?: AbortSignal;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Handler return values are normalized into an {@link McpToolResult}: a full
|
|
91
|
+
* result (`{ content: [...] }`) is passed through, a string becomes a text
|
|
92
|
+
* block, `undefined` becomes an empty result, and anything else is returned
|
|
93
|
+
* as `structuredContent` (plain objects) plus a JSON text block.
|
|
94
|
+
*/
|
|
95
|
+
type AgentToolHandler = (args: Record<string, any>, context: ToolCallContext) => unknown | Promise<unknown>;
|
|
96
|
+
interface AgentTool extends McpToolDefinition {
|
|
97
|
+
handler: AgentToolHandler;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* A source of tools, typically one per mounted editor, spreadsheet or canvas.
|
|
101
|
+
*/
|
|
102
|
+
interface AgentToolProvider {
|
|
103
|
+
/** Unique id within a registry, e.g. `doc-1`. Used as the namespace on collisions. */
|
|
104
|
+
id: string;
|
|
105
|
+
/** Always prefix this provider's tools with `<namespace>__`. */
|
|
106
|
+
namespace?: string;
|
|
107
|
+
/** Tools, or a function returning the current tools (re-read on every `listTools`). */
|
|
108
|
+
tools: readonly AgentTool[] | (() => readonly AgentTool[]);
|
|
109
|
+
/** Lets the provider signal that its tool list changed; returns an unsubscribe function. */
|
|
110
|
+
subscribe?: (notify: () => void) => () => void;
|
|
111
|
+
}
|
|
112
|
+
type ToolErrorCode = 'unknown_tool' | 'invalid_arguments' | 'tool_failed';
|
|
113
|
+
/** Structured error carried in `structuredContent.error` of a failed call. */
|
|
114
|
+
interface ToolCallError {
|
|
115
|
+
code: ToolErrorCode;
|
|
116
|
+
message: string;
|
|
117
|
+
tool: string;
|
|
118
|
+
details?: SchemaValidationError[];
|
|
119
|
+
}
|
|
120
|
+
declare class ToolRegistryError extends Error {
|
|
121
|
+
readonly code: 'duplicate_provider' | 'name_collision' | 'invalid_tool';
|
|
122
|
+
constructor(code: ToolRegistryError['code'], message: string);
|
|
123
|
+
}
|
|
124
|
+
interface ToolRegistryOptions {
|
|
125
|
+
/**
|
|
126
|
+
* What to do when two providers contribute the same tool name:
|
|
127
|
+
* `namespace` (default) exposes every colliding tool as `<providerId>__<name>`;
|
|
128
|
+
* `error` makes `register` throw.
|
|
129
|
+
*/
|
|
130
|
+
onCollision?: 'namespace' | 'error';
|
|
131
|
+
/** Coalescing window for `listChanged` notifications in ms (default 0: same tick). */
|
|
132
|
+
listChangedDelayMs?: number;
|
|
133
|
+
/** Validate arguments against `inputSchema` before calling handlers (default `true`). */
|
|
134
|
+
validateArguments?: boolean;
|
|
135
|
+
}
|
|
136
|
+
/** Separator between namespace and tool name; valid in MCP and LLM tool-name charsets. */
|
|
137
|
+
declare const TOOL_NAMESPACE_SEPARATOR = "__";
|
|
138
|
+
/** Normalizes a handler's return value into an MCP `tools/call` result. */
|
|
139
|
+
declare function toToolResult(value: unknown): McpToolResult;
|
|
140
|
+
/**
|
|
141
|
+
* Transport-agnostic registry of agent tools contributed by live providers.
|
|
142
|
+
*
|
|
143
|
+
* `listTools()` and `callTool()` return MCP `tools/list` / `tools/call`
|
|
144
|
+
* result shapes, and `onListChanged` fires (coalesced) whenever the exposed
|
|
145
|
+
* tool list changes, mirroring MCP's `notifications/tools/list_changed`, so an
|
|
146
|
+
* MCP server can be a thin wrapper around it.
|
|
147
|
+
*/
|
|
148
|
+
declare class ToolRegistry {
|
|
149
|
+
/** MCP server capabilities this registry supports. */
|
|
150
|
+
readonly capabilities: {
|
|
151
|
+
readonly tools: {
|
|
152
|
+
readonly listChanged: true;
|
|
153
|
+
};
|
|
154
|
+
};
|
|
155
|
+
private readonly entries;
|
|
156
|
+
private readonly listeners;
|
|
157
|
+
private readonly options;
|
|
158
|
+
private timer;
|
|
159
|
+
private lastSignature;
|
|
160
|
+
constructor(options?: ToolRegistryOptions);
|
|
161
|
+
/**
|
|
162
|
+
* Registers a provider and returns a function that unregisters it.
|
|
163
|
+
* Throws a {@link ToolRegistryError} for a duplicate provider id, an invalid
|
|
164
|
+
* tool name, or (with `onCollision: 'error'`) a tool name collision.
|
|
165
|
+
*/
|
|
166
|
+
register(provider: AgentToolProvider): () => void;
|
|
167
|
+
/** Removes a provider by id or instance. Returns whether it was registered. */
|
|
168
|
+
unregister(providerOrId: string | AgentToolProvider): boolean;
|
|
169
|
+
/** Registered provider ids in registration order. */
|
|
170
|
+
providerIds(): string[];
|
|
171
|
+
/**
|
|
172
|
+
* Resolves exposed names. Tools are ordered by provider registration, then
|
|
173
|
+
* by their order within the provider.
|
|
174
|
+
*/
|
|
175
|
+
private resolve;
|
|
176
|
+
/** MCP `tools/list` result for the current providers. */
|
|
177
|
+
listTools(): McpListToolsResult;
|
|
178
|
+
/** Whether a tool with this exposed name exists. */
|
|
179
|
+
hasTool(name: string): boolean;
|
|
180
|
+
/** Looks up a tool definition by exposed name. */
|
|
181
|
+
getTool(name: string): McpToolDefinition | undefined;
|
|
182
|
+
/**
|
|
183
|
+
* Calls a tool by exposed name and returns an MCP `tools/call` result.
|
|
184
|
+
* Never rejects: unknown tools, invalid arguments and handler errors come
|
|
185
|
+
* back as `isError: true` results with a {@link ToolCallError} in
|
|
186
|
+
* `structuredContent.error` (MCP transports may map `unknown_tool` to a
|
|
187
|
+
* JSON-RPC `-32602` error instead).
|
|
188
|
+
*/
|
|
189
|
+
callTool(name: string, args?: Record<string, unknown>, options?: {
|
|
190
|
+
signal?: AbortSignal;
|
|
191
|
+
}): Promise<McpToolResult>;
|
|
192
|
+
/**
|
|
193
|
+
* Subscribes to tool-list changes. Notifications are coalesced: many
|
|
194
|
+
* registrations or provider signals within the delay window produce one
|
|
195
|
+
* call, and none if the exposed list ends up unchanged (e.g. a provider
|
|
196
|
+
* unmounted and remounted). Returns an unsubscribe function.
|
|
197
|
+
*/
|
|
198
|
+
onListChanged(listener: (tools: McpToolDefinition[]) => void): () => void;
|
|
199
|
+
/** Schedules a coalesced `listChanged` check; providers may call it directly. */
|
|
200
|
+
notifyListChanged(): void;
|
|
201
|
+
/** Runs a pending `listChanged` check immediately. */
|
|
202
|
+
flush(): void;
|
|
203
|
+
private signature;
|
|
204
|
+
/** Unregisters every provider and listener and cancels pending notifications. */
|
|
205
|
+
dispose(): void;
|
|
206
|
+
}
|
|
207
|
+
declare function createToolRegistry(options?: ToolRegistryOptions): ToolRegistry;
|
|
208
|
+
|
|
209
|
+
export { type AgentToolProvider as A, type JsonSchema as J, type McpListToolsResult as M, type SchemaValidationError as S, TOOL_NAMESPACE_SEPARATOR as T, type AgentTool as a, type AgentToolHandler as b, type JsonSchemaType as c, type McpToolContent as d, type McpToolDefinition as e, type McpToolResult as f, type ToolCallContext as g, type ToolCallError as h, type ToolErrorCode as i, ToolRegistry as j, ToolRegistryError as k, type ToolRegistryOptions as l, createToolRegistry as m, toToolResult as t, validateJsonSchema as v };
|
|
@@ -4,9 +4,10 @@
|
|
|
4
4
|
* import pipelines of document apps, where each source format declares the
|
|
5
5
|
* MIME types and extensions it accepts.
|
|
6
6
|
*
|
|
7
|
-
* Built-in adapters cover markdown, HTML, plain text and
|
|
8
|
-
* `document` target.
|
|
9
|
-
* targets (e.g. CSV → `spreadsheet`) plug in
|
|
7
|
+
* Built-in adapters cover markdown, HTML, plain text, RTF and DOCX for the
|
|
8
|
+
* `document` target. PDF (`createPdfImportAdapter`), zipped exports
|
|
9
|
+
* (`zipImportAdapter`) and other targets (e.g. CSV → `spreadsheet`) plug in
|
|
10
|
+
* with `registerImportAdapter`.
|
|
10
11
|
*/
|
|
11
12
|
/** What an import produces content for. Custom targets are plain strings. */
|
|
12
13
|
type ImportTarget = "document" | "spreadsheet" | (string & {});
|
|
@@ -29,6 +30,11 @@ interface ImportContext {
|
|
|
29
30
|
readText(): Promise<string>;
|
|
30
31
|
/** Reads the input as binary data (strings are UTF-8 encoded). */
|
|
31
32
|
readArrayBuffer(): Promise<ArrayBuffer>;
|
|
33
|
+
/**
|
|
34
|
+
* The registry running the import, so container adapters (e.g. zipped
|
|
35
|
+
* exports) can import nested files with the same adapters.
|
|
36
|
+
*/
|
|
37
|
+
registry?: ImportAdapterRegistry;
|
|
32
38
|
}
|
|
33
39
|
interface ImportAdapter<TResult = unknown> {
|
|
34
40
|
/** Unique id, e.g. `markdown`, `html`, `csv`. */
|
|
@@ -67,7 +73,11 @@ interface ImportOutcome<TResult = unknown> {
|
|
|
67
73
|
mimeType?: string;
|
|
68
74
|
result: TResult;
|
|
69
75
|
}
|
|
70
|
-
type ImportErrorCode = "unsupported_type" | "unknown_adapter" | "adapter_failed"
|
|
76
|
+
type ImportErrorCode = "unsupported_type" | "unknown_adapter" | "adapter_failed"
|
|
77
|
+
/** The file is damaged or not the format its name/type claims (e.g. a `.docx` without `word/document.xml`). */
|
|
78
|
+
| "invalid_file"
|
|
79
|
+
/** A platform API the adapter needs (e.g. `DOMParser`) is missing. */
|
|
80
|
+
| "unsupported_environment";
|
|
71
81
|
declare class ImportError extends Error {
|
|
72
82
|
readonly code: ImportErrorCode;
|
|
73
83
|
readonly fileName?: string;
|
|
@@ -85,6 +95,7 @@ declare function createImportContext(input: ImportInput, details: {
|
|
|
85
95
|
fileName?: string;
|
|
86
96
|
mimeType?: string;
|
|
87
97
|
target: ImportTarget;
|
|
98
|
+
registry?: ImportAdapterRegistry;
|
|
88
99
|
}): ImportContext;
|
|
89
100
|
/**
|
|
90
101
|
* A set of import adapters with type matching. Use the default registry via
|
|
@@ -4,9 +4,10 @@
|
|
|
4
4
|
* import pipelines of document apps, where each source format declares the
|
|
5
5
|
* MIME types and extensions it accepts.
|
|
6
6
|
*
|
|
7
|
-
* Built-in adapters cover markdown, HTML, plain text and
|
|
8
|
-
* `document` target.
|
|
9
|
-
* targets (e.g. CSV → `spreadsheet`) plug in
|
|
7
|
+
* Built-in adapters cover markdown, HTML, plain text, RTF and DOCX for the
|
|
8
|
+
* `document` target. PDF (`createPdfImportAdapter`), zipped exports
|
|
9
|
+
* (`zipImportAdapter`) and other targets (e.g. CSV → `spreadsheet`) plug in
|
|
10
|
+
* with `registerImportAdapter`.
|
|
10
11
|
*/
|
|
11
12
|
/** What an import produces content for. Custom targets are plain strings. */
|
|
12
13
|
type ImportTarget = "document" | "spreadsheet" | (string & {});
|
|
@@ -29,6 +30,11 @@ interface ImportContext {
|
|
|
29
30
|
readText(): Promise<string>;
|
|
30
31
|
/** Reads the input as binary data (strings are UTF-8 encoded). */
|
|
31
32
|
readArrayBuffer(): Promise<ArrayBuffer>;
|
|
33
|
+
/**
|
|
34
|
+
* The registry running the import, so container adapters (e.g. zipped
|
|
35
|
+
* exports) can import nested files with the same adapters.
|
|
36
|
+
*/
|
|
37
|
+
registry?: ImportAdapterRegistry;
|
|
32
38
|
}
|
|
33
39
|
interface ImportAdapter<TResult = unknown> {
|
|
34
40
|
/** Unique id, e.g. `markdown`, `html`, `csv`. */
|
|
@@ -67,7 +73,11 @@ interface ImportOutcome<TResult = unknown> {
|
|
|
67
73
|
mimeType?: string;
|
|
68
74
|
result: TResult;
|
|
69
75
|
}
|
|
70
|
-
type ImportErrorCode = "unsupported_type" | "unknown_adapter" | "adapter_failed"
|
|
76
|
+
type ImportErrorCode = "unsupported_type" | "unknown_adapter" | "adapter_failed"
|
|
77
|
+
/** The file is damaged or not the format its name/type claims (e.g. a `.docx` without `word/document.xml`). */
|
|
78
|
+
| "invalid_file"
|
|
79
|
+
/** A platform API the adapter needs (e.g. `DOMParser`) is missing. */
|
|
80
|
+
| "unsupported_environment";
|
|
71
81
|
declare class ImportError extends Error {
|
|
72
82
|
readonly code: ImportErrorCode;
|
|
73
83
|
readonly fileName?: string;
|
|
@@ -85,6 +95,7 @@ declare function createImportContext(input: ImportInput, details: {
|
|
|
85
95
|
fileName?: string;
|
|
86
96
|
mimeType?: string;
|
|
87
97
|
target: ImportTarget;
|
|
98
|
+
registry?: ImportAdapterRegistry;
|
|
88
99
|
}): ImportContext;
|
|
89
100
|
/**
|
|
90
101
|
* A set of import adapters with type matching. Use the default registry via
|