服务器评论

赋予您的 AI 代理读取、编写和编辑文档中评论的能力,完全在服务器端完成。

查看 GitHub 上的源码

继续阅读 AI 代理聊天机器人指南

本指南接续 AI 代理聊天机器人指南。请先阅读它。

启用评论工具

要启用评论工具,获取服务器 AI 工具包 API 的工具定义时,传递 getThreadseditThreads 选项。

import { getAuthHeaders } from '@/lib/server-ai-toolkit/get-auth-headers'

const response = await fetch(`${apiBaseUrl}/v4/ai/toolkit/fetch-tools`, {
  method: 'POST',
  headers: await getAuthHeaders(),
  body: JSON.stringify({
    editorContext,
    tools: {
      // 禁用 tiptap 编辑工具,使 AI 不能编辑文档,
      // 只能添加评论
      tiptapEdit: false,
      // 启用评论工具
      getThreads: true,
      editThreads: true,
    },
  }),
})

执行评论工具

评论工具需要一个 Tiptap Cloud 文档,因为线程和评论存储在 Tiptap Document Server 上。传递 document: { type: 'cloud', id },这样 AI Server 就可以自动从 Document Server 获取它。

请求体

在请求体中包含云文档引用:

  • document.id (string,必填):文档的 ID。
  • user (string,可选):创建评论的用户 ID。若省略,评论将不关联用户。
const result = await fetch(`${apiBaseUrl}/v4/ai/toolkit/execute-tool`, {
  method: 'POST',
  headers: await getAuthHeaders('your-document-id'),
  body: JSON.stringify({
    editorContext,
    // 引用 Tiptap Cloud 文档
    document: {
      type: 'cloud',
      id: 'your-document-id',
    },
    user: 'ai-assistant',
    tool: {
      name: 'editThreads',
      input: {},
    },
  }),
})

最终效果

结果是一个简单但完善的 AI 聊天机器人应用:

查看 GitHub 上的源码

下一步