index.d.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. import { ChainedCommands } from '@tiptap/core/dist/packages/core/src/types';
  2. import { ChainedCommands as ChainedCommands_2 } from '@tiptap/core';
  3. import { Editor } from '@tiptap/core';
  4. import { EditorEvents } from '@tiptap/core';
  5. import { EditorOptions } from '@tiptap/core';
  6. import { Extensions } from '@tiptap/core';
  7. import { Fragment } from 'prosemirror-model';
  8. import { Instance } from 'tippy.js';
  9. import { JSONContent } from '@tiptap/core';
  10. import { SingleCommands } from '@tiptap/core';
  11. import { Transaction } from '@tiptap/pm/state';
  12. declare class AbstractMenuButton extends HTMLElement implements AiEditorEventListener {
  13. template: string;
  14. editor?: InnerEditor;
  15. options?: AiEditorOptions;
  16. alwaysEnabledButtons: string[];
  17. protected constructor();
  18. protected registerClickListener(): void;
  19. connectedCallback(): void;
  20. onClick(commands: ChainedCommands): void;
  21. onCreate(props: EditorEvents["create"], options: AiEditorOptions): void;
  22. onTransaction(event: EditorEvents["transaction"]): void;
  23. onActive(editor: Editor): boolean;
  24. onEditableChange(editable: boolean): void;
  25. }
  26. export declare type AIBubbleMenuItem = {
  27. prompt: string;
  28. icon: string;
  29. title: string;
  30. } | string;
  31. export declare interface AiClient {
  32. start: (payload: string) => void;
  33. stop: () => void;
  34. }
  35. export declare interface AiClientListener {
  36. onStart: (aiClient: AiClient) => void;
  37. onStop: () => void;
  38. onMessage: (bodyString: string) => void;
  39. }
  40. export declare class AiEditor {
  41. customLayout: boolean;
  42. innerEditor: InnerEditor;
  43. container: HTMLDivElement;
  44. header: Header;
  45. mainEl: HTMLDivElement;
  46. footer: Footer;
  47. options: AiEditorOptions;
  48. eventComponents: AiEditorEventListener[];
  49. constructor(_: AiEditorOptions);
  50. private initI18nAndInnerEditor;
  51. protected initInnerEditor(): void;
  52. protected getExtensions(): Extensions;
  53. protected onCreate(props: EditorEvents['create']): void;
  54. protected onTransaction(transEvent: EditorEvents['transaction']): void;
  55. getHtml(): string;
  56. getJson(): JSONContent;
  57. getText(): string;
  58. getSelectedText(): string;
  59. getMarkdown(): any;
  60. getOptions(): AiEditorOptions;
  61. getAttributes(name: string): Record<string, any>;
  62. setAttributes(name: string, attributes: Record<string, any>): void;
  63. isActive(nameOrAttrs: any, attrs?: {}): boolean;
  64. commands(): SingleCommands;
  65. commandsChain(): ChainedCommands_2;
  66. getOutline(): any[];
  67. focus(): this;
  68. focusPos(pos: number): this;
  69. focusStart(): this;
  70. focusEnd(): this;
  71. isFocused(): boolean;
  72. blur(): this;
  73. insert(content: any): this;
  74. insertMarkdown(content: string): this;
  75. setEditable(editable: boolean): this;
  76. setContent(content: string): this;
  77. setMarkdownContent(content: string): this;
  78. clear(): this;
  79. isEmpty(): boolean;
  80. changeLang(lang: string): this;
  81. changeTheme(theme?: "dark" | "light"): void;
  82. removeRetention(): this;
  83. destroy(): void;
  84. isDestroyed(): boolean;
  85. }
  86. export declare interface AiEditorEvent {
  87. type: string;
  88. value: any;
  89. }
  90. export declare interface AiEditorEventListener {
  91. onCreate: (props: EditorEvents['create'], options: AiEditorOptions) => void;
  92. onTransaction: (props: EditorEvents['transaction']) => void;
  93. onEditableChange: (editable: boolean) => void;
  94. onEvent?: (event: AiEditorEvent) => void;
  95. }
  96. export declare type AiEditorOptions = {
  97. element: string | Element;
  98. content?: string;
  99. contentIsMarkdown?: boolean;
  100. contentRetention?: boolean;
  101. contentRetentionKey?: string;
  102. lang?: string;
  103. editable?: boolean;
  104. i18n?: Record<string, Record<string, string>>;
  105. placeholder?: string;
  106. theme?: "light" | "dark";
  107. onMentionQuery?: (query: string) => any[] | Promise<any[]>;
  108. onCreateBefore?: (editor: AiEditor, extensions: Extensions) => void | Extensions;
  109. onCreated?: (editor: AiEditor) => void;
  110. onChange?: (editor: AiEditor) => void;
  111. onTransaction?: (editor: AiEditor, transaction: Transaction) => void;
  112. onFocus?: (editor: AiEditor) => void;
  113. onBlur?: (editor: AiEditor) => void;
  114. onDestroy?: (editor: AiEditor) => void;
  115. onSave?: (editor: AiEditor) => boolean;
  116. onFullscreen?: (isFullscreen: boolean) => void;
  117. toolbarKeys?: (string | CustomMenu | MenuGroup)[];
  118. alwaysEnabledToolbarKeys?: string[];
  119. toolbarExcludeKeys?: DefaultToolbarKey[];
  120. toolbarSize?: 'small' | 'medium' | 'large';
  121. draggable?: boolean;
  122. htmlPasteConfig?: HtmlPasteConfig;
  123. codeBlock?: {
  124. languages?: LanguageItem[];
  125. codeExplainPrompt?: string;
  126. codeCommentsPrompt?: string;
  127. };
  128. textSelectionBubbleMenu?: {
  129. enable?: boolean;
  130. elementTagName?: string;
  131. items?: (string | BubbleMenuItem)[];
  132. };
  133. link?: {
  134. autolink?: boolean;
  135. rel?: string;
  136. class?: string;
  137. bubbleMenuItems?: (string | BubbleMenuItem)[];
  138. };
  139. container?: {
  140. defaultType?: string;
  141. typeItems?: string[];
  142. };
  143. uploader?: Uploader;
  144. image?: {
  145. customMenuInvoke?: (editor: AiEditor) => void;
  146. uploadUrl?: string;
  147. uploadHeaders?: (() => Record<string, any>) | Record<string, any>;
  148. uploadFormName?: string;
  149. uploader?: Uploader;
  150. uploaderEvent?: UploaderEvent;
  151. defaultSize?: number;
  152. allowBase64?: boolean;
  153. bubbleMenuEnable?: boolean;
  154. bubbleMenuItems?: (string | BubbleMenuItem)[];
  155. };
  156. video?: {
  157. customMenuInvoke?: (editor: AiEditor) => void;
  158. uploadUrl?: string;
  159. uploadHeaders?: (() => Record<string, any>) | Record<string, any>;
  160. uploadFormName?: string;
  161. uploader?: Uploader;
  162. uploaderEvent?: UploaderEvent;
  163. };
  164. attachment?: {
  165. customMenuInvoke?: (editor: AiEditor) => void;
  166. uploadUrl?: string;
  167. uploadHeaders?: (() => Record<string, any>) | Record<string, any>;
  168. uploadFormName?: string;
  169. uploader?: Uploader;
  170. uploaderEvent?: UploaderEvent;
  171. };
  172. fontFamily?: {
  173. values: NameAndValue[];
  174. };
  175. fontSize?: {
  176. defaultValue?: number;
  177. values?: NameAndValue[];
  178. };
  179. lineHeight?: {
  180. values?: string[];
  181. };
  182. emoji?: {
  183. values?: string[];
  184. };
  185. textCounter?: (text: string) => number;
  186. ai?: AiGlobalConfig;
  187. } & Partial<Omit<EditorOptions, "element">>;
  188. export declare interface AiGlobalConfig {
  189. models: Record<string, AiModelConfig>;
  190. modelFactory?: AiModelFactory;
  191. onTokenConsume?: (modelName: string, modelConfig: AiModelConfig, count: number) => void;
  192. onCreateClientUrl?: (modelName: string, modelConfig: AiModelConfig, onSuccess: (url: string) => void, onFailure: () => void) => void;
  193. bubblePanelEnable?: boolean;
  194. bubblePanelModel?: string;
  195. bubblePanelMenus?: AIBubbleMenuItem[];
  196. bubblePanelIcon?: string;
  197. menus?: AiMenu[];
  198. commands?: AiMenu[];
  199. translate?: {
  200. prompt?: (language: string, selectText: string) => string;
  201. translateMenuItems?: TranslateMenuItem[];
  202. };
  203. codeBlock?: {
  204. codeComments?: {
  205. model: string;
  206. prompt: string;
  207. };
  208. codeExplain?: {
  209. model: string;
  210. prompt: string;
  211. };
  212. };
  213. }
  214. export declare interface AiMenu {
  215. icon: string;
  216. name: string;
  217. prompt?: string;
  218. text?: "selected" | "focusBefore";
  219. model?: string;
  220. onClick?: (event: MouseEvent, editor: AiEditor) => void;
  221. children?: AiMenu[];
  222. }
  223. export declare interface AiMessage {
  224. role: string;
  225. content: string;
  226. index: number;
  227. status: 0 | 1 | 2;
  228. }
  229. export declare interface AiMessageListener {
  230. onStart: (aiClient: AiClient) => void;
  231. onStop: () => void;
  232. onMessage: (message: AiMessage) => void;
  233. }
  234. declare abstract class AiModel {
  235. editor: InnerEditor;
  236. globalConfig: AiGlobalConfig;
  237. aiModelName: string;
  238. aiModelConfig: AiModelConfig;
  239. protected constructor(editor: InnerEditor, globalConfig: AiGlobalConfig, aiModelName: string);
  240. chatWithPayload(payload: any, listener: AiMessageListener): void;
  241. chat(selectedText: string, prompt: string, listener: AiMessageListener): void;
  242. /**
  243. * 创建客户端链接 URL
  244. */
  245. abstract createAiClientUrl(): string;
  246. /**
  247. * 创建客户端
  248. */
  249. abstract createAiClient(url: string, listener: AiMessageListener): AiClient;
  250. /**
  251. * 封装消息,把 prompt 转换为协议需要的格式
  252. * @param prompt
  253. */
  254. abstract wrapPayload(prompt: string): any;
  255. }
  256. declare interface AiModelConfig {
  257. temperature?: number;
  258. maxTokens?: number;
  259. }
  260. export declare interface AiModelFactory {
  261. create: (name: string, editor: Editor, globalConfig: AiGlobalConfig) => AiModel;
  262. }
  263. export declare class AiModelManager {
  264. private static models;
  265. static init(editor: InnerEditor, globalConfig: AiGlobalConfig): void;
  266. static get(modelName: string): AiModel;
  267. static set(modelName: string, aiModel: AiModel): void;
  268. }
  269. export declare type BubbleMenuItem = {
  270. id: string;
  271. title?: string;
  272. icon: string;
  273. holder?: any;
  274. isActive?: (editor: AiEditor) => boolean;
  275. onInit?: (editor: AiEditor, tippyInstance: Instance, parentEle: HTMLElement) => any;
  276. onClick?: (editor: AiEditor, tippyInstance: Instance, parentEle: HTMLElement, holder: any) => void;
  277. };
  278. export declare class CustomAiModel extends AiModel {
  279. constructor(editor: InnerEditor, globalConfig: AiGlobalConfig);
  280. createAiClient(url: string, listener: AiMessageListener): AiClient;
  281. wrapPayload(promptMessage: string): string;
  282. createAiClientUrl(): string;
  283. }
  284. export declare interface CustomAiModelConfig extends AiModelConfig {
  285. url: (() => string) | string;
  286. method?: string;
  287. headers?: () => Record<string, any> | undefined;
  288. wrapPayload: (prompt: string) => string;
  289. parseMessage: (bodyString: string) => AiMessage | undefined;
  290. protocol: "sse" | "websocket" | "http";
  291. }
  292. export declare interface CustomMenu {
  293. id?: string;
  294. className?: string;
  295. icon?: string;
  296. html?: string;
  297. tip?: string;
  298. onClick?: (event: MouseEvent, editor: AiEditor) => void;
  299. onCreate?: (button: HTMLElement, editor: AiEditor) => void;
  300. }
  301. declare type DefaultToolbarKey = (typeof defaultToolbarKeys)[number];
  302. declare const defaultToolbarKeys: string[];
  303. declare class Footer extends HTMLElement implements AiEditorEventListener {
  304. count: number;
  305. draggable: boolean;
  306. constructor();
  307. initDraggable(draggable?: boolean): void;
  308. updateCharacters(): void;
  309. onCreate(event: EditorEvents["create"], _: AiEditorOptions): void;
  310. onTransaction(event: EditorEvents["transaction"]): void;
  311. onEditableChange(editable: boolean): void;
  312. }
  313. declare class Header extends HTMLElement implements AiEditorEventListener {
  314. menuButtons: AbstractMenuButton[];
  315. constructor();
  316. connectedCallback(): void;
  317. onCreate(event: EditorEvents["create"], options: AiEditorOptions): void;
  318. onTransaction(event: EditorEvents["transaction"]): void;
  319. onEditableChange(editable: boolean): void;
  320. }
  321. export declare interface HtmlPasteConfig {
  322. pasteAsText?: boolean;
  323. pasteClean?: boolean;
  324. removeEmptyParagraphs?: boolean;
  325. pasteProcessor?: (html: string) => string;
  326. }
  327. export declare class InnerEditor extends Editor {
  328. linwitDocEditor: AiEditor;
  329. constructor(linwitDocEditor: AiEditor, options?: Partial<EditorOptions>);
  330. parseHtml(html: string): Fragment;
  331. parseMarkdown(markdown: string): Fragment;
  332. insertMarkdown(markdown: string): void;
  333. }
  334. declare type LanguageItem = {
  335. name: string;
  336. value: string;
  337. alias?: string[];
  338. };
  339. export declare interface MenuGroup {
  340. title?: string;
  341. icon?: string;
  342. toolbarKeys: (string | CustomMenu | MenuGroup)[];
  343. }
  344. export declare interface NameAndValue {
  345. name: string;
  346. value: any;
  347. }
  348. export declare class OpenaiAiModel extends AiModel {
  349. constructor(editor: InnerEditor, globalConfig: AiGlobalConfig);
  350. createAiClient(url: string, listener: AiMessageListener): AiClient;
  351. wrapPayload(prompt: string): string;
  352. createAiClientUrl(): string;
  353. }
  354. export declare interface OpenaiModelConfig extends AiModelConfig {
  355. endpoint?: string;
  356. customUrl?: string | (() => string);
  357. apiKey?: string;
  358. model: string;
  359. }
  360. export declare class SparkAiModel extends AiModel {
  361. constructor(editor: InnerEditor, globalConfig: AiGlobalConfig);
  362. createAiClient(url: string, listener: AiMessageListener): AiClient;
  363. wrapPayload(promptMessage: string): string;
  364. getDomain(sparkAiModelConfig: SparkAiModelConfig): "4.0Ultra" | "generalv3.5" | "generalv3" | "generalv2" | "lite" | "general";
  365. createAiClientUrl(): string;
  366. }
  367. export declare interface SparkAiModelConfig extends AiModelConfig {
  368. appId: string;
  369. apiKey: string;
  370. apiSecret: string;
  371. protocol?: string;
  372. version?: string;
  373. }
  374. export declare type TranslateMenuItem = {
  375. title: string;
  376. language?: string;
  377. } | string;
  378. export declare type Uploader = (file: File, uploadUrl: string, headers: Record<string, any>, formName: string) => Promise<Record<string, any>>;
  379. export declare interface UploaderEvent {
  380. onUploadBefore?: (file: File, uploadUrl: string, headers: Record<string, any>) => void | boolean;
  381. onSuccess?: (file: File, response: any) => any;
  382. onFailed?: (file: File, response: any) => void;
  383. onError?: (file: File, err: any) => void;
  384. }
  385. export declare class WenXinAiModel extends AiModel {
  386. constructor(editor: InnerEditor, globalConfig: AiGlobalConfig);
  387. createAiClient(url: string, listener: AiMessageListener): AiClient;
  388. wrapPayload(prompt: string): string;
  389. createAiClientUrl(): string;
  390. }
  391. export declare interface WenXinAiModelConfig extends AiModelConfig {
  392. access_token: string;
  393. protocol?: string;
  394. version?: string;
  395. }
  396. export { }