@max1874/feishu 0.2.1 → 0.2.3
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/package.json +1 -1
- package/src/docx.ts +86 -11
package/package.json
CHANGED
package/src/docx.ts
CHANGED
|
@@ -65,9 +65,11 @@ const BLOCK_TYPE_NAMES: Record<number, string> = {
|
|
|
65
65
|
};
|
|
66
66
|
|
|
67
67
|
// Block types that cannot be created via documentBlockChildren.create API
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
68
|
+
// Note: Table (31) and TableCell (32) were previously excluded, but the convert API
|
|
69
|
+
// returns them with proper structure, so we attempt to create them directly.
|
|
70
|
+
// If creation fails, the error will be reported to the user.
|
|
71
|
+
const UNSUPPORTED_CREATE_TYPES = new Set<number>([
|
|
72
|
+
// Empty for now - let's try creating tables directly
|
|
71
73
|
]);
|
|
72
74
|
|
|
73
75
|
/** Clean blocks for insertion (remove unsupported types and read-only fields) */
|
|
@@ -83,7 +85,7 @@ function cleanBlocksForInsert(blocks: any[]): { cleaned: any[]; skipped: string[
|
|
|
83
85
|
return true;
|
|
84
86
|
})
|
|
85
87
|
.map((block) => {
|
|
86
|
-
// Remove
|
|
88
|
+
// Remove read-only fields from table blocks that might cause API errors
|
|
87
89
|
if (block.block_type === 31 && block.table?.merge_info) {
|
|
88
90
|
const { merge_info, ...tableRest } = block.table;
|
|
89
91
|
return { ...block, table: tableRest };
|
|
@@ -121,12 +123,34 @@ async function insertBlocks(
|
|
|
121
123
|
return { children: [], skipped };
|
|
122
124
|
}
|
|
123
125
|
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
126
|
+
// Log table blocks for debugging
|
|
127
|
+
const tableBlocks = cleaned.filter((b) => b.block_type === 31);
|
|
128
|
+
if (tableBlocks.length > 0) {
|
|
129
|
+
console.log(`[feishu_doc] Attempting to insert ${tableBlocks.length} table block(s)`);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
try {
|
|
133
|
+
const res = await client.docx.documentBlockChildren.create({
|
|
134
|
+
path: { document_id: docToken, block_id: blockId },
|
|
135
|
+
data: { children: cleaned },
|
|
136
|
+
});
|
|
137
|
+
if (res.code !== 0) {
|
|
138
|
+
throw new Error(`API error ${res.code}: ${res.msg}`);
|
|
139
|
+
}
|
|
140
|
+
return { children: res.data?.children ?? [], skipped };
|
|
141
|
+
} catch (err: any) {
|
|
142
|
+
// Enhanced error logging
|
|
143
|
+
const errMsg = err?.response?.data?.msg || err?.message || String(err);
|
|
144
|
+
const errCode = err?.response?.data?.code || err?.code;
|
|
145
|
+
console.error(`[feishu_doc] insertBlocks failed: code=${errCode}, msg=${errMsg}`);
|
|
146
|
+
|
|
147
|
+
// If table blocks exist, report them as problematic
|
|
148
|
+
if (tableBlocks.length > 0) {
|
|
149
|
+
console.error(`[feishu_doc] Table block structure sample:`, JSON.stringify(tableBlocks[0], null, 2).slice(0, 500));
|
|
150
|
+
throw new Error(`Table insertion failed (code=${errCode}): ${errMsg}. Tables may not be supported via this API.`);
|
|
151
|
+
}
|
|
152
|
+
throw err;
|
|
153
|
+
}
|
|
130
154
|
}
|
|
131
155
|
|
|
132
156
|
/** Delete all child blocks from a parent */
|
|
@@ -570,6 +594,32 @@ async function listAppScopes(client: Lark.Client) {
|
|
|
570
594
|
};
|
|
571
595
|
}
|
|
572
596
|
|
|
597
|
+
/** Debug: convert markdown and show resulting block structure */
|
|
598
|
+
async function debugConvertMarkdown(client: Lark.Client, markdown: string) {
|
|
599
|
+
const { blocks } = await convertMarkdown(client, markdown);
|
|
600
|
+
|
|
601
|
+
const blockTypeCounts: Record<string, number> = {};
|
|
602
|
+
for (const block of blocks) {
|
|
603
|
+
const typeName = BLOCK_TYPE_NAMES[block.block_type] || `type_${block.block_type}`;
|
|
604
|
+
blockTypeCounts[typeName] = (blockTypeCounts[typeName] || 0) + 1;
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
// Find table blocks and show their structure
|
|
608
|
+
const tableBlocks = blocks.filter((b) => b.block_type === 31);
|
|
609
|
+
let tableSample: any = null;
|
|
610
|
+
if (tableBlocks.length > 0) {
|
|
611
|
+
tableSample = tableBlocks[0];
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
return {
|
|
615
|
+
total_blocks: blocks.length,
|
|
616
|
+
block_types: blockTypeCounts,
|
|
617
|
+
has_tables: tableBlocks.length > 0,
|
|
618
|
+
table_count: tableBlocks.length,
|
|
619
|
+
table_sample: tableSample ? JSON.stringify(tableSample, null, 2) : null,
|
|
620
|
+
};
|
|
621
|
+
}
|
|
622
|
+
|
|
573
623
|
// ============ Schemas ============
|
|
574
624
|
|
|
575
625
|
const DocTokenSchema = Type.Object({
|
|
@@ -626,6 +676,10 @@ const SetPermissionSchema = Type.Object({
|
|
|
626
676
|
}),
|
|
627
677
|
});
|
|
628
678
|
|
|
679
|
+
const DebugConvertSchema = Type.Object({
|
|
680
|
+
content: Type.String({ description: "Markdown content to convert (for debugging block structure)" }),
|
|
681
|
+
});
|
|
682
|
+
|
|
629
683
|
// Wiki Schemas
|
|
630
684
|
const WikiTokenSchema = Type.Object({
|
|
631
685
|
wiki_token: Type.String({ description: "Wiki token (extract from URL /wiki/XXX)" }),
|
|
@@ -961,5 +1015,26 @@ export function registerFeishuDocTools(api: OpenClawPluginApi) {
|
|
|
961
1015
|
{ name: "feishu_app_scopes" },
|
|
962
1016
|
);
|
|
963
1017
|
|
|
964
|
-
|
|
1018
|
+
// Tool 15: feishu_debug_convert (for diagnosing markdown conversion issues)
|
|
1019
|
+
api.registerTool(
|
|
1020
|
+
{
|
|
1021
|
+
name: "feishu_debug_convert",
|
|
1022
|
+
label: "Feishu Debug Convert",
|
|
1023
|
+
description:
|
|
1024
|
+
"Debug tool: convert markdown to blocks and show the resulting structure. Use to diagnose why certain markdown (like tables) may fail to write.",
|
|
1025
|
+
parameters: DebugConvertSchema,
|
|
1026
|
+
async execute(_toolCallId, params) {
|
|
1027
|
+
const { content } = params as { content: string };
|
|
1028
|
+
try {
|
|
1029
|
+
const result = await debugConvertMarkdown(getClient(), content);
|
|
1030
|
+
return json(result);
|
|
1031
|
+
} catch (err) {
|
|
1032
|
+
return json({ error: err instanceof Error ? err.message : String(err) });
|
|
1033
|
+
}
|
|
1034
|
+
},
|
|
1035
|
+
},
|
|
1036
|
+
{ name: "feishu_debug_convert" },
|
|
1037
|
+
);
|
|
1038
|
+
|
|
1039
|
+
api.logger.info?.(`feishu_doc: Registered 15 document/wiki tools`);
|
|
965
1040
|
}
|