Skip to content

AI 接入(React) ​

AI 能力统一走插件体系:插件注册 AiAction,右键菜单自动出现入口,弹窗交互由组件层渲染。AI 操作通过统一的 SDK 接口执行。

方式一:配置接入(推荐) ​

用 createAiAdapter 一份配置即可接入 OpenAI 兼容的大模型,无需手写请求:

tsx
import { DocEditor } from '@sword-editor/react'
import { createAiAdapter } from '@sword-editor/react'

const ai = createAiAdapter({
  endpoint: 'https://api.openai.com/v1/chat/completions',
  apiKey: 'sk-...',
  model: 'gpt-4o',
  systemPrompt: '你是资深编辑,输出简洁专业的中文。',
  imageModel: 'dall-e-3',
})

export default () => (
  <DocEditor value={markdown} onChange={setMarkdown} ai={ai} />
)

覆盖 DeepSeek / Moonshot / 通义 / 本地 Ollama 等兼容服务:

ts
const ai = createAiAdapter({
  endpoint: 'https://api.deepseek.com/v1/chat/completions',
  apiKey: process.env.DEEPSEEK_KEY!,
  model: 'deepseek-chat',
})

配置项 AiClientConfig ​

字段类型说明
endpointstringchat/completions 端点(必填)
apiKeystringAPI Key(Authorization: Bearer)
modelstring模型名(必填)
headersRecord<string, string>额外请求头
systemPromptstring系统提示词
fetchtypeof fetch自定义 fetch(SSR / 代理 / 测试)
imageEndpointstring图片生成端点,缺省由 endpoint 推导
imageModelstring图片模型,默认 dall-e-3
imageSizestring图片尺寸,默认 1024x1024

方式二:函数适配器 ​

不想依赖 OpenAI 兼容协议,直接给三个函数(均可选,只注入需要的):

tsx
const ai = {
  generate: (instruction: string) => Promise.resolve('# 生成结果'),
  polish: (text: string, instruction: string) => Promise.resolve(text),
  image: (instruction: string) => Promise.resolve('/img.png'),
}

<DocEditor ai={ai} />

方式三:自定义 AiAction(插件) ​

注册自己的 AI 操作,自定义执行与结果应用:

tsx
import { definePlugin } from '@sword-editor/react'
import type { AiAction } from '@sword-editor/react'

const myAi: AiAction = {
  id: 'ai.translate',
  label: 'AI 翻译',
  icon: 'sparkles',
  kind: 'polish',
  run: async ({ block, instruction }) => ({
    text: await translate(block?.text ?? '', instruction),
  }),
}

const plugin = definePlugin({
  name: 'my.ai',
  install(ctx) {
    ctx.registerAiAction(myAi)
  },
})

<DocEditor plugins={[plugin]} />

AI 结果应用规则 ​

插件未提供 apply 时,按 kind 走内置逻辑:

kind结果字段应用方式
generatetext(Markdown)转为块列表,插入触发块处
polishtext替换触发块的文本
imageimage(地址)在触发块后插入图片块

@sword-editor/doc-editor · Vue 3 / React