Skip to content

feishu_doc write action fails to clear all blocks on large documents (missing pagination in clearDocumentContent) #67252

@pxivory-max

Description

@pxivory-max

Bug Description

feishu_doc write action does not fully replace document content when the document has more than ~50 top-level blocks. Instead of replacing all content, it only deletes the first page of blocks and appends new content after the remaining old blocks.

Root Cause

In api.js line 1993, clearDocumentContent() calls client.docx.documentBlock.list() without pagination. The Feishu API returns results in pages (default ~50 blocks per page), but the function only processes the first page:

async function clearDocumentContent(client, docToken) {
  const existing = await client.docx.documentBlock.list({ path: { document_id: docToken } });
  // ^ Only fetches first page! No page_token loop
  const childIds = existing.data?.items
    ?.filter((b) => b.parent_id === docToken && b.block_type !== 1)
    .map((b) => b.block_id) ?? [];
  if (childIds.length > 0) {
    const res = await client.docx.documentBlockChildren.batchDelete({
      path: { document_id: docToken, block_id: docToken },
      data: { start_index: 0, end_index: childIds.length }
    });
    if (res.code !== 0) throw new Error(res.msg);
  }
  return childIds.length;
}

Expected Behavior

write should fully replace all document content regardless of document size.

Suggested Fix

Add pagination loop to clearDocumentContent:

async function clearDocumentContent(client, docToken) {
  let items = [];
  let pageToken;
  do {
    const res = await client.docx.documentBlock.list({
      path: { document_id: docToken },
      params: pageToken ? { page_token: pageToken } : {}
    });
    if (res.code !== 0) throw new Error(res.msg);
    items.push(...(res.data?.items ?? []));
    pageToken = res.data?.page_token ?? undefined;
  } while (pageToken);

  const childIds = items
    .filter((b) => b.parent_id === docToken && b.block_type !== 1)
    .map((b) => b.block_id) ?? [];
  
  if (childIds.length > 0) {
    const res = await client.docx.documentBlockChildren.batchDelete({
      path: { document_id: docToken, block_id: docToken },
      data: { start_index: 0, end_index: childIds.length }
    });
    if (res.code !== 0) throw new Error(res.msg);
  }
  return childIds.length;
}

Reproduction

  1. Create a Feishu document with 100+ top-level blocks (e.g., a long report with many sections, lists, quotes)
  2. Call feishu_doc with action: "write" and new content
  3. Observe: old content remains in the document after the first ~50 blocks, with new content appended after it

Workaround

Create a new document and use write (for first chunk) + append (for subsequent chunks) instead of writing to an existing large document.

Environment

  • OpenClaw version: 2026.4.7
  • Feishu plugin: bundled
  • File: dist/extensions/feishu/api.js line 1993

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions