@actagent/feishu 2026.6.2
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/README.md +11 -0
- package/actagent.plugin.json +224 -0
- package/api.ts +33 -0
- package/channel-entry.ts +21 -0
- package/channel-plugin-api.ts +2 -0
- package/contract-api.ts +17 -0
- package/index.ts +83 -0
- package/legacy-state-migrations-api.ts +2 -0
- package/npm-shrinkwrap.json +539 -0
- package/package.json +64 -0
- package/runtime-api.ts +58 -0
- package/runtime-setter-api.ts +3 -0
- package/secret-contract-api.ts +6 -0
- package/security-contract-api.ts +2 -0
- package/session-key-api.ts +2 -0
- package/setup-api.ts +4 -0
- package/setup-entry.test.ts +33 -0
- package/setup-entry.ts +25 -0
- package/skills/feishu-doc/SKILL.md +211 -0
- package/skills/feishu-doc/references/block-types.md +103 -0
- package/skills/feishu-drive/SKILL.md +97 -0
- package/skills/feishu-perm/SKILL.md +119 -0
- package/skills/feishu-wiki/SKILL.md +113 -0
- package/src/accounts.test.ts +481 -0
- package/src/accounts.ts +380 -0
- package/src/agent-config.ts +22 -0
- package/src/app-registration.test.ts +62 -0
- package/src/app-registration.ts +355 -0
- package/src/approval-auth.test.ts +25 -0
- package/src/approval-auth.ts +26 -0
- package/src/async.test.ts +68 -0
- package/src/async.ts +109 -0
- package/src/audio-preflight.runtime.ts +10 -0
- package/src/bitable.test.ts +174 -0
- package/src/bitable.ts +781 -0
- package/src/bot-content.ts +488 -0
- package/src/bot-group-name.test.ts +148 -0
- package/src/bot-runtime-api.ts +13 -0
- package/src/bot-sender-name.test.ts +68 -0
- package/src/bot-sender-name.ts +137 -0
- package/src/bot.broadcast.test.ts +643 -0
- package/src/bot.card-action.test.ts +647 -0
- package/src/bot.checkBotMentioned.test.ts +266 -0
- package/src/bot.helpers.test.ts +136 -0
- package/src/bot.stripBotMention.test.ts +127 -0
- package/src/bot.test.ts +3817 -0
- package/src/bot.ts +1788 -0
- package/src/card-action.ts +515 -0
- package/src/card-interaction.test.ts +132 -0
- package/src/card-interaction.ts +160 -0
- package/src/card-test-helpers.ts +55 -0
- package/src/card-ux-approval.ts +66 -0
- package/src/card-ux-launcher.test.ts +126 -0
- package/src/card-ux-launcher.ts +136 -0
- package/src/card-ux-shared.ts +34 -0
- package/src/channel-runtime-api.ts +17 -0
- package/src/channel.runtime.ts +48 -0
- package/src/channel.test.ts +1337 -0
- package/src/channel.ts +1401 -0
- package/src/chat-schema.ts +30 -0
- package/src/chat.test.ts +295 -0
- package/src/chat.ts +198 -0
- package/src/client-timeout.ts +44 -0
- package/src/client.test.ts +463 -0
- package/src/client.ts +263 -0
- package/src/comment-dispatcher-runtime-api.ts +7 -0
- package/src/comment-dispatcher.test.ts +186 -0
- package/src/comment-dispatcher.ts +108 -0
- package/src/comment-handler-runtime-api.ts +4 -0
- package/src/comment-handler.test.ts +588 -0
- package/src/comment-handler.ts +304 -0
- package/src/comment-reaction.test.ts +139 -0
- package/src/comment-reaction.ts +260 -0
- package/src/comment-shared.test.ts +184 -0
- package/src/comment-shared.ts +405 -0
- package/src/comment-target.ts +45 -0
- package/src/config-schema.test.ts +327 -0
- package/src/config-schema.ts +338 -0
- package/src/conversation-id.test.ts +19 -0
- package/src/conversation-id.ts +199 -0
- package/src/dedup-migrations.test.ts +90 -0
- package/src/dedup-migrations.ts +103 -0
- package/src/dedup.test.ts +95 -0
- package/src/dedup.ts +304 -0
- package/src/dedupe-key.ts +68 -0
- package/src/directory.static.ts +62 -0
- package/src/directory.test.ts +142 -0
- package/src/directory.ts +125 -0
- package/src/doc-schema.ts +183 -0
- package/src/doctor.test.ts +382 -0
- package/src/doctor.ts +876 -0
- package/src/docx-batch-insert.test.ts +117 -0
- package/src/docx-batch-insert.ts +223 -0
- package/src/docx-color-text.ts +154 -0
- package/src/docx-table-ops.test.ts +54 -0
- package/src/docx-table-ops.ts +316 -0
- package/src/docx-types.ts +39 -0
- package/src/docx.account-selection.test.ts +96 -0
- package/src/docx.test.ts +706 -0
- package/src/docx.ts +1598 -0
- package/src/drive-schema.ts +93 -0
- package/src/drive.test.ts +1240 -0
- package/src/drive.ts +830 -0
- package/src/dynamic-agent.test.ts +156 -0
- package/src/dynamic-agent.ts +144 -0
- package/src/event-types.ts +46 -0
- package/src/external-keys.test.ts +21 -0
- package/src/external-keys.ts +20 -0
- package/src/lifecycle.test-support.ts +223 -0
- package/src/media.test.ts +956 -0
- package/src/media.ts +1106 -0
- package/src/mention-target.types.ts +6 -0
- package/src/mention.ts +115 -0
- package/src/message-action-contract.ts +14 -0
- package/src/monitor-state-runtime-api.ts +8 -0
- package/src/monitor-transport-runtime-api.ts +11 -0
- package/src/monitor.account.ts +501 -0
- package/src/monitor.acp-init-failure.lifecycle.test-support.ts +215 -0
- package/src/monitor.bot-identity.ts +87 -0
- package/src/monitor.bot-menu-handler.ts +164 -0
- package/src/monitor.bot-menu.lifecycle.test-support.ts +221 -0
- package/src/monitor.bot-menu.test.ts +200 -0
- package/src/monitor.broadcast.reply-once.lifecycle.test-support.ts +265 -0
- package/src/monitor.card-action.lifecycle.test-support.ts +418 -0
- package/src/monitor.cleanup.test.ts +384 -0
- package/src/monitor.comment-notice-handler.ts +106 -0
- package/src/monitor.comment.test.ts +968 -0
- package/src/monitor.comment.ts +1386 -0
- package/src/monitor.lifecycle.test.ts +5 -0
- package/src/monitor.message-handler.ts +346 -0
- package/src/monitor.reaction.test.ts +770 -0
- package/src/monitor.startup.test.ts +232 -0
- package/src/monitor.startup.ts +76 -0
- package/src/monitor.state.defaults.test.ts +47 -0
- package/src/monitor.state.ts +171 -0
- package/src/monitor.synthetic-error.ts +19 -0
- package/src/monitor.test-mocks.ts +47 -0
- package/src/monitor.transport.ts +451 -0
- package/src/monitor.ts +104 -0
- package/src/monitor.webhook-e2e.test.ts +284 -0
- package/src/monitor.webhook-security.test.ts +394 -0
- package/src/monitor.webhook.test-helpers.ts +138 -0
- package/src/outbound-runtime-api.ts +2 -0
- package/src/outbound.test.ts +1255 -0
- package/src/outbound.ts +742 -0
- package/src/perm-schema.ts +53 -0
- package/src/perm.ts +171 -0
- package/src/pins.ts +109 -0
- package/src/policy.test.ts +224 -0
- package/src/policy.ts +322 -0
- package/src/post.test.ts +106 -0
- package/src/post.ts +276 -0
- package/src/presentation-card.ts +204 -0
- package/src/probe.test.ts +310 -0
- package/src/probe.ts +181 -0
- package/src/processing-claims.ts +60 -0
- package/src/qr-terminal.ts +2 -0
- package/src/reactions.ts +124 -0
- package/src/reasoning-preview.test.ts +114 -0
- package/src/reasoning-preview.ts +29 -0
- package/src/reply-dispatcher-runtime-api.ts +8 -0
- package/src/reply-dispatcher.test.ts +2009 -0
- package/src/reply-dispatcher.ts +865 -0
- package/src/runtime.ts +10 -0
- package/src/secret-contract.ts +146 -0
- package/src/secret-input.ts +2 -0
- package/src/security-audit-shared.ts +70 -0
- package/src/security-audit.test.ts +60 -0
- package/src/security-audit.ts +2 -0
- package/src/send-result.ts +81 -0
- package/src/send-target.test.ts +87 -0
- package/src/send-target.ts +36 -0
- package/src/send.reply-fallback.test.ts +418 -0
- package/src/send.test.ts +661 -0
- package/src/send.ts +860 -0
- package/src/sequential-key.test.ts +73 -0
- package/src/sequential-key.ts +29 -0
- package/src/sequential-queue.test.ts +184 -0
- package/src/sequential-queue.ts +90 -0
- package/src/session-conversation.ts +42 -0
- package/src/session-route.ts +49 -0
- package/src/setup-core.ts +52 -0
- package/src/setup-surface.test.ts +485 -0
- package/src/setup-surface.ts +620 -0
- package/src/streaming-card.test.ts +549 -0
- package/src/streaming-card.ts +611 -0
- package/src/subagent-hooks.test.ts +632 -0
- package/src/subagent-hooks.ts +414 -0
- package/src/targets.ts +98 -0
- package/src/test-support/lifecycle-test-support.ts +459 -0
- package/src/thread-bindings.test.ts +181 -0
- package/src/thread-bindings.ts +332 -0
- package/src/tool-account-routing.test.ts +419 -0
- package/src/tool-account.test.ts +45 -0
- package/src/tool-account.ts +98 -0
- package/src/tool-factory-test-harness.ts +83 -0
- package/src/tool-result.test.ts +33 -0
- package/src/tool-result.ts +17 -0
- package/src/tools-config.test.ts +52 -0
- package/src/tools-config.ts +29 -0
- package/src/types.ts +111 -0
- package/src/typing.test.ts +145 -0
- package/src/typing.ts +215 -0
- package/src/wiki-schema.ts +70 -0
- package/src/wiki.ts +271 -0
- package/subagent-hooks-api.ts +22 -0
- package/tsconfig.json +16 -0
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
// Feishu tests cover docx batch insert plugin behavior.
|
|
2
|
+
import type * as Lark from "@larksuiteoapi/node-sdk";
|
|
3
|
+
import { describe, expect, it, vi } from "vitest";
|
|
4
|
+
import { BATCH_SIZE, insertBlocksInBatches } from "./docx-batch-insert.js";
|
|
5
|
+
import type { FeishuDocxBlock } from "./docx-types.js";
|
|
6
|
+
|
|
7
|
+
type InsertBlocksClient = Parameters<typeof insertBlocksInBatches>[0];
|
|
8
|
+
type DocxDescendantCreate = Lark.Client["docx"]["documentBlockDescendant"]["create"];
|
|
9
|
+
type DocxDescendantCreateParams = Parameters<DocxDescendantCreate>[0];
|
|
10
|
+
type DocxDescendantCreateResponse = Awaited<ReturnType<DocxDescendantCreate>>;
|
|
11
|
+
type RequiredDocxDescendantCreateParams = NonNullable<DocxDescendantCreateParams> & {
|
|
12
|
+
data: NonNullable<NonNullable<DocxDescendantCreateParams>["data"]>;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
function createDocxDescendantClient(create: DocxDescendantCreate): InsertBlocksClient {
|
|
16
|
+
return {
|
|
17
|
+
docx: {
|
|
18
|
+
documentBlockDescendant: {
|
|
19
|
+
create,
|
|
20
|
+
},
|
|
21
|
+
},
|
|
22
|
+
} as InsertBlocksClient;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function createCountingIterable<T>(values: T[]) {
|
|
26
|
+
let iterations = 0;
|
|
27
|
+
return {
|
|
28
|
+
values: {
|
|
29
|
+
*[Symbol.iterator]() {
|
|
30
|
+
iterations += 1;
|
|
31
|
+
yield* values;
|
|
32
|
+
},
|
|
33
|
+
},
|
|
34
|
+
getIterations: () => iterations,
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function createSuccessfulDocxDescendantCreateMock() {
|
|
39
|
+
return vi.fn(
|
|
40
|
+
async (params?: DocxDescendantCreateParams): Promise<DocxDescendantCreateResponse> => ({
|
|
41
|
+
code: 0,
|
|
42
|
+
data: {
|
|
43
|
+
children: (params?.data?.children_id ?? []).map((id) => ({
|
|
44
|
+
block_id: id,
|
|
45
|
+
block_type: 2,
|
|
46
|
+
})),
|
|
47
|
+
},
|
|
48
|
+
}),
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function createCallParams(
|
|
53
|
+
createMock: ReturnType<typeof createSuccessfulDocxDescendantCreateMock>,
|
|
54
|
+
index = 0,
|
|
55
|
+
): RequiredDocxDescendantCreateParams {
|
|
56
|
+
const call = createMock.mock.calls.at(index);
|
|
57
|
+
if (!call) {
|
|
58
|
+
throw new Error(`Expected DOCX descendant create call ${index}`);
|
|
59
|
+
}
|
|
60
|
+
const params = call.at(0);
|
|
61
|
+
if (!params) {
|
|
62
|
+
throw new Error(`Expected DOCX descendant create params ${index}`);
|
|
63
|
+
}
|
|
64
|
+
if (!params.data) {
|
|
65
|
+
throw new Error(`Expected DOCX descendant create data ${index}`);
|
|
66
|
+
}
|
|
67
|
+
return params as RequiredDocxDescendantCreateParams;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
describe("insertBlocksInBatches", () => {
|
|
71
|
+
it("builds the source block map once for large flat trees", async () => {
|
|
72
|
+
const blockCount = BATCH_SIZE + 200;
|
|
73
|
+
const blocks = Array.from({ length: blockCount }, (_, index) => ({
|
|
74
|
+
block_id: `block_${index}`,
|
|
75
|
+
block_type: 2,
|
|
76
|
+
}));
|
|
77
|
+
const counting = createCountingIterable(blocks);
|
|
78
|
+
const createMock = createSuccessfulDocxDescendantCreateMock();
|
|
79
|
+
const client = createDocxDescendantClient((params) => createMock(params));
|
|
80
|
+
|
|
81
|
+
const result = await insertBlocksInBatches(
|
|
82
|
+
client,
|
|
83
|
+
"doc_1",
|
|
84
|
+
Array.from(counting.values),
|
|
85
|
+
blocks.map((block) => block.block_id),
|
|
86
|
+
);
|
|
87
|
+
|
|
88
|
+
expect(counting.getIterations()).toBe(1);
|
|
89
|
+
expect(createMock).toHaveBeenCalledTimes(2);
|
|
90
|
+
expect(createCallParams(createMock).data.children_id).toHaveLength(BATCH_SIZE);
|
|
91
|
+
expect(createCallParams(createMock, 1).data.children_id).toHaveLength(200);
|
|
92
|
+
expect(result.children).toHaveLength(blockCount);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it("keeps nested descendants grouped with their root blocks", async () => {
|
|
96
|
+
const createMock = createSuccessfulDocxDescendantCreateMock();
|
|
97
|
+
const client = createDocxDescendantClient((params) => createMock(params));
|
|
98
|
+
const blocks: FeishuDocxBlock[] = [
|
|
99
|
+
{ block_id: "root_a", block_type: 1, children: ["child_a"] },
|
|
100
|
+
{ block_id: "child_a", block_type: 2 },
|
|
101
|
+
{ block_id: "root_b", block_type: 1, children: ["child_b"] },
|
|
102
|
+
{ block_id: "child_b", block_type: 2 },
|
|
103
|
+
];
|
|
104
|
+
|
|
105
|
+
await insertBlocksInBatches(client, "doc_1", blocks, ["root_a", "root_b"]);
|
|
106
|
+
|
|
107
|
+
expect(createMock).toHaveBeenCalledTimes(1);
|
|
108
|
+
const createParams = createCallParams(createMock);
|
|
109
|
+
expect(createParams.data.children_id).toEqual(["root_a", "root_b"]);
|
|
110
|
+
expect(createParams.data.descendants.map((block) => block.block_id ?? "")).toEqual([
|
|
111
|
+
"root_a",
|
|
112
|
+
"child_a",
|
|
113
|
+
"root_b",
|
|
114
|
+
"child_b",
|
|
115
|
+
]);
|
|
116
|
+
});
|
|
117
|
+
});
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Batch insertion for large Feishu documents (>1000 blocks).
|
|
3
|
+
*
|
|
4
|
+
* The Feishu Descendant API has a limit of 1000 blocks per request.
|
|
5
|
+
* This module handles splitting large documents into batches while
|
|
6
|
+
* preserving parent-child relationships between blocks.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import type * as Lark from "@larksuiteoapi/node-sdk";
|
|
10
|
+
import { readStringValue } from "actagent/plugin-sdk/string-coerce-runtime";
|
|
11
|
+
import { cleanBlocksForDescendant } from "./docx-table-ops.js";
|
|
12
|
+
import type { FeishuDocxBlock, FeishuDocxBlockChild } from "./docx-types.js";
|
|
13
|
+
|
|
14
|
+
export const BATCH_SIZE = 1000; // Feishu API limit per request
|
|
15
|
+
|
|
16
|
+
type Logger = { info?: (msg: string) => void };
|
|
17
|
+
|
|
18
|
+
type DocxDescendantCreatePayload = NonNullable<
|
|
19
|
+
Parameters<Lark.Client["docx"]["documentBlockDescendant"]["create"]>[0]
|
|
20
|
+
>;
|
|
21
|
+
type DocxDescendantCreateBlock = NonNullable<
|
|
22
|
+
NonNullable<DocxDescendantCreatePayload["data"]>["descendants"]
|
|
23
|
+
>[number];
|
|
24
|
+
|
|
25
|
+
function normalizeChildIds(children: string[] | string | undefined): string[] | undefined {
|
|
26
|
+
if (Array.isArray(children)) {
|
|
27
|
+
return children;
|
|
28
|
+
}
|
|
29
|
+
const child = readStringValue(children);
|
|
30
|
+
return child ? [child] : undefined;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function toDescendantBlock(block: FeishuDocxBlock): DocxDescendantCreateBlock {
|
|
34
|
+
const children = normalizeChildIds(block.children);
|
|
35
|
+
return {
|
|
36
|
+
...block,
|
|
37
|
+
...(children ? { children } : {}),
|
|
38
|
+
} as DocxDescendantCreateBlock;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Collect all descendant blocks for a given first-level block ID.
|
|
43
|
+
* Recursively traverses the block tree to gather all children.
|
|
44
|
+
*/
|
|
45
|
+
function collectDescendants(
|
|
46
|
+
blockMap: Map<string, FeishuDocxBlock>,
|
|
47
|
+
rootId: string,
|
|
48
|
+
): FeishuDocxBlock[] {
|
|
49
|
+
const result: FeishuDocxBlock[] = [];
|
|
50
|
+
const visited = new Set<string>();
|
|
51
|
+
|
|
52
|
+
function collect(blockId: string) {
|
|
53
|
+
if (visited.has(blockId)) {
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
visited.add(blockId);
|
|
57
|
+
|
|
58
|
+
const block = blockMap.get(blockId);
|
|
59
|
+
if (!block) {
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
result.push(block);
|
|
64
|
+
|
|
65
|
+
// Recursively collect children
|
|
66
|
+
const children = block.children;
|
|
67
|
+
if (Array.isArray(children)) {
|
|
68
|
+
for (const childId of children) {
|
|
69
|
+
collect(childId);
|
|
70
|
+
}
|
|
71
|
+
} else if (typeof children === "string") {
|
|
72
|
+
collect(children);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
collect(rootId);
|
|
77
|
+
|
|
78
|
+
return result;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Insert a single batch of blocks using Descendant API.
|
|
83
|
+
*
|
|
84
|
+
* @param parentBlockId - Parent block to insert into (defaults to docToken)
|
|
85
|
+
* @param index - Position within parent's children (-1 = end)
|
|
86
|
+
*/
|
|
87
|
+
async function insertBatch(
|
|
88
|
+
client: Lark.Client,
|
|
89
|
+
docToken: string,
|
|
90
|
+
blocks: FeishuDocxBlock[],
|
|
91
|
+
firstLevelBlockIds: string[],
|
|
92
|
+
parentBlockId: string = docToken,
|
|
93
|
+
index = -1,
|
|
94
|
+
): Promise<FeishuDocxBlockChild[]> {
|
|
95
|
+
const descendants = cleanBlocksForDescendant(blocks);
|
|
96
|
+
|
|
97
|
+
if (descendants.length === 0) {
|
|
98
|
+
return [];
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const res = await client.docx.documentBlockDescendant.create({
|
|
102
|
+
path: { document_id: docToken, block_id: parentBlockId },
|
|
103
|
+
data: {
|
|
104
|
+
children_id: firstLevelBlockIds,
|
|
105
|
+
descendants: descendants.map(toDescendantBlock),
|
|
106
|
+
index,
|
|
107
|
+
},
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
if (res.code !== 0) {
|
|
111
|
+
throw new Error(`${res.msg} (code: ${res.code})`);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
return res.data?.children ?? [];
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Insert blocks in batches for large documents (>1000 blocks).
|
|
119
|
+
*
|
|
120
|
+
* Batches are split to ensure BOTH children_id AND descendants
|
|
121
|
+
* arrays stay under the 1000 block API limit.
|
|
122
|
+
*
|
|
123
|
+
* @param client - Feishu API client
|
|
124
|
+
* @param docToken - Document ID
|
|
125
|
+
* @param blocks - All blocks from Convert API
|
|
126
|
+
* @param firstLevelBlockIds - IDs of top-level blocks to insert
|
|
127
|
+
* @param logger - Optional logger for progress updates
|
|
128
|
+
* @param parentBlockId - Parent block to insert into (defaults to docToken = document root)
|
|
129
|
+
* @param startIndex - Starting position within parent (-1 = end). For multi-batch inserts,
|
|
130
|
+
* each batch advances this by the number of first-level IDs inserted so far.
|
|
131
|
+
* @returns Inserted children blocks and any skipped block IDs
|
|
132
|
+
*/
|
|
133
|
+
export async function insertBlocksInBatches(
|
|
134
|
+
client: Lark.Client,
|
|
135
|
+
docToken: string,
|
|
136
|
+
blocks: FeishuDocxBlock[],
|
|
137
|
+
firstLevelBlockIds: string[],
|
|
138
|
+
logger?: Logger,
|
|
139
|
+
parentBlockId: string = docToken,
|
|
140
|
+
startIndex = -1,
|
|
141
|
+
): Promise<{ children: FeishuDocxBlockChild[]; skipped: string[] }> {
|
|
142
|
+
const allChildren: FeishuDocxBlockChild[] = [];
|
|
143
|
+
|
|
144
|
+
// Build batches ensuring each batch has ≤1000 total descendants
|
|
145
|
+
const batches: Array<{ firstLevelIds: string[]; blocks: FeishuDocxBlock[] }> = [];
|
|
146
|
+
let currentBatch: { firstLevelIds: string[]; blocks: FeishuDocxBlock[] } = {
|
|
147
|
+
firstLevelIds: [],
|
|
148
|
+
blocks: [],
|
|
149
|
+
};
|
|
150
|
+
const usedBlockIds = new Set<string>();
|
|
151
|
+
const blockMap = new Map<string, FeishuDocxBlock>();
|
|
152
|
+
for (const block of blocks) {
|
|
153
|
+
if (block.block_id) {
|
|
154
|
+
blockMap.set(block.block_id, block);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
for (const firstLevelId of firstLevelBlockIds) {
|
|
159
|
+
const descendants = collectDescendants(blockMap, firstLevelId);
|
|
160
|
+
const newBlocks = descendants.filter((b) => b.block_id && !usedBlockIds.has(b.block_id));
|
|
161
|
+
|
|
162
|
+
// A single block whose subtree exceeds the API limit cannot be split
|
|
163
|
+
// (a table or other compound block must be inserted atomically).
|
|
164
|
+
if (newBlocks.length > BATCH_SIZE) {
|
|
165
|
+
throw new Error(
|
|
166
|
+
`Block "${firstLevelId}" has ${newBlocks.length} descendants, which exceeds the ` +
|
|
167
|
+
`Feishu API limit of ${BATCH_SIZE} blocks per request. ` +
|
|
168
|
+
`Please split the content into smaller sections.`,
|
|
169
|
+
);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// If adding this first-level block would exceed limit, start new batch
|
|
173
|
+
if (
|
|
174
|
+
currentBatch.blocks.length + newBlocks.length > BATCH_SIZE &&
|
|
175
|
+
currentBatch.blocks.length > 0
|
|
176
|
+
) {
|
|
177
|
+
batches.push(currentBatch);
|
|
178
|
+
currentBatch = { firstLevelIds: [], blocks: [] };
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// Add to current batch
|
|
182
|
+
currentBatch.firstLevelIds.push(firstLevelId);
|
|
183
|
+
for (const block of newBlocks) {
|
|
184
|
+
currentBatch.blocks.push(block);
|
|
185
|
+
if (block.block_id) {
|
|
186
|
+
usedBlockIds.add(block.block_id);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
// Don't forget the last batch
|
|
192
|
+
if (currentBatch.blocks.length > 0) {
|
|
193
|
+
batches.push(currentBatch);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// Insert each batch, advancing index for position-aware inserts.
|
|
197
|
+
// When startIndex == -1 (append to end), each batch appends after the previous.
|
|
198
|
+
// When startIndex >= 0, each batch starts at startIndex + count of first-level IDs already inserted.
|
|
199
|
+
let currentIndex = startIndex;
|
|
200
|
+
for (let i = 0; i < batches.length; i++) {
|
|
201
|
+
const batch = batches[i];
|
|
202
|
+
logger?.info?.(
|
|
203
|
+
`feishu_doc: Inserting batch ${i + 1}/${batches.length} (${batch.blocks.length} blocks)...`,
|
|
204
|
+
);
|
|
205
|
+
|
|
206
|
+
const children = await insertBatch(
|
|
207
|
+
client,
|
|
208
|
+
docToken,
|
|
209
|
+
batch.blocks,
|
|
210
|
+
batch.firstLevelIds,
|
|
211
|
+
parentBlockId,
|
|
212
|
+
currentIndex,
|
|
213
|
+
);
|
|
214
|
+
allChildren.push(...children);
|
|
215
|
+
|
|
216
|
+
// Advance index only for explicit positions; -1 always means "after last inserted"
|
|
217
|
+
if (currentIndex !== -1) {
|
|
218
|
+
currentIndex += batch.firstLevelIds.length;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
return { children: allChildren, skipped: [] };
|
|
223
|
+
}
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Colored text support for Feishu documents.
|
|
3
|
+
*
|
|
4
|
+
* Parses a simple color markup syntax and updates a text block
|
|
5
|
+
* with native Feishu text_run color styles.
|
|
6
|
+
*
|
|
7
|
+
* Syntax: [color]text[/color]
|
|
8
|
+
* Supported colors: red, orange, yellow, green, blue, purple, grey
|
|
9
|
+
*
|
|
10
|
+
* Example:
|
|
11
|
+
* "Revenue [green]+15%[/green] YoY, Costs [red]-3%[/red]"
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import type * as Lark from "@larksuiteoapi/node-sdk";
|
|
15
|
+
import { normalizeLowercaseStringOrEmpty } from "actagent/plugin-sdk/string-coerce-runtime";
|
|
16
|
+
|
|
17
|
+
// Feishu text_color values (1-7)
|
|
18
|
+
const TEXT_COLOR: Record<string, number> = {
|
|
19
|
+
red: 1, // Pink (closest to red in Feishu)
|
|
20
|
+
orange: 2,
|
|
21
|
+
yellow: 3,
|
|
22
|
+
green: 4,
|
|
23
|
+
blue: 5,
|
|
24
|
+
purple: 6,
|
|
25
|
+
grey: 7,
|
|
26
|
+
gray: 7,
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
// Feishu background_color values (1-15)
|
|
30
|
+
const BACKGROUND_COLOR: Record<string, number> = {
|
|
31
|
+
red: 1,
|
|
32
|
+
orange: 2,
|
|
33
|
+
yellow: 3,
|
|
34
|
+
green: 4,
|
|
35
|
+
blue: 5,
|
|
36
|
+
purple: 6,
|
|
37
|
+
grey: 7,
|
|
38
|
+
gray: 7,
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
interface Segment {
|
|
42
|
+
text: string;
|
|
43
|
+
textColor?: number;
|
|
44
|
+
bgColor?: number;
|
|
45
|
+
bold?: boolean;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
type DocxPatchPayload = NonNullable<Parameters<Lark.Client["docx"]["documentBlock"]["patch"]>[0]>;
|
|
49
|
+
type DocxTextElement = NonNullable<
|
|
50
|
+
NonNullable<NonNullable<DocxPatchPayload["data"]>["update_text_elements"]>["elements"]
|
|
51
|
+
>[number];
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Parse color markup into segments.
|
|
55
|
+
*
|
|
56
|
+
* Supports:
|
|
57
|
+
* [red]text[/red] → red text
|
|
58
|
+
* [bg:yellow]text[/bg] → yellow background
|
|
59
|
+
* [bold]text[/bold] → bold
|
|
60
|
+
* [green bold]text[/green] → green + bold
|
|
61
|
+
*/
|
|
62
|
+
function parseColorMarkup(content: string): Segment[] {
|
|
63
|
+
const segments: Segment[] = [];
|
|
64
|
+
// Only [known_tag]...[/...] pairs are treated as markup. Using an open
|
|
65
|
+
// pattern like \[([^\]]+)\] would match any bracket token — e.g. [Q1] —
|
|
66
|
+
// and cause it to consume a later real closing tag ([/red]), silently
|
|
67
|
+
// corrupting the surrounding styled spans. Restricting the opening tag to
|
|
68
|
+
// the set of recognised colour/style names prevents that: [Q1] does not
|
|
69
|
+
// match the tag alternative and each of its characters falls through to the
|
|
70
|
+
// plain-text alternatives instead.
|
|
71
|
+
//
|
|
72
|
+
// Closing tag name is still not validated against the opening tag:
|
|
73
|
+
// [red]text[/green] is treated as [red]text[/red] — opening style applies
|
|
74
|
+
// and the closing tag is consumed regardless of its name.
|
|
75
|
+
const KNOWN = "(?:bg:[a-z]+|bold|red|orange|yellow|green|blue|purple|gr[ae]y)";
|
|
76
|
+
const tagPattern = new RegExp(
|
|
77
|
+
`\\[(${KNOWN}(?:\\s+${KNOWN})*)\\](.*?)\\[\\/(?:[^\\]]+)\\]|([^[]+|\\[)`,
|
|
78
|
+
"gis",
|
|
79
|
+
);
|
|
80
|
+
let match;
|
|
81
|
+
|
|
82
|
+
while ((match = tagPattern.exec(content)) !== null) {
|
|
83
|
+
if (match[3] !== undefined) {
|
|
84
|
+
// Plain text segment
|
|
85
|
+
if (match[3]) {
|
|
86
|
+
segments.push({ text: match[3] });
|
|
87
|
+
}
|
|
88
|
+
} else {
|
|
89
|
+
// Tagged segment
|
|
90
|
+
const tagStr = normalizeLowercaseStringOrEmpty(match[1]);
|
|
91
|
+
const text = match[2];
|
|
92
|
+
const tags = tagStr.split(/\s+/);
|
|
93
|
+
|
|
94
|
+
const segment: Segment = { text };
|
|
95
|
+
|
|
96
|
+
for (const tag of tags) {
|
|
97
|
+
if (tag.startsWith("bg:")) {
|
|
98
|
+
const color = tag.slice(3);
|
|
99
|
+
if (BACKGROUND_COLOR[color]) {
|
|
100
|
+
segment.bgColor = BACKGROUND_COLOR[color];
|
|
101
|
+
}
|
|
102
|
+
} else if (tag === "bold") {
|
|
103
|
+
segment.bold = true;
|
|
104
|
+
} else if (TEXT_COLOR[tag]) {
|
|
105
|
+
segment.textColor = TEXT_COLOR[tag];
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
if (text) {
|
|
110
|
+
segments.push(segment);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
return segments;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Update a text block with colored segments.
|
|
120
|
+
*/
|
|
121
|
+
export async function updateColorText(
|
|
122
|
+
client: Lark.Client,
|
|
123
|
+
docToken: string,
|
|
124
|
+
blockId: string,
|
|
125
|
+
content: string,
|
|
126
|
+
) {
|
|
127
|
+
const segments = parseColorMarkup(content);
|
|
128
|
+
|
|
129
|
+
const elements: DocxTextElement[] = segments.map((seg) => ({
|
|
130
|
+
text_run: {
|
|
131
|
+
content: seg.text,
|
|
132
|
+
text_element_style: {
|
|
133
|
+
...(seg.textColor && { text_color: seg.textColor }),
|
|
134
|
+
...(seg.bgColor && { background_color: seg.bgColor }),
|
|
135
|
+
...(seg.bold && { bold: true }),
|
|
136
|
+
},
|
|
137
|
+
},
|
|
138
|
+
}));
|
|
139
|
+
|
|
140
|
+
const res = await client.docx.documentBlock.patch({
|
|
141
|
+
path: { document_id: docToken, block_id: blockId },
|
|
142
|
+
data: { update_text_elements: { elements } },
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
if (res.code !== 0) {
|
|
146
|
+
throw new Error(res.msg);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
return {
|
|
150
|
+
success: true,
|
|
151
|
+
segments: segments.length,
|
|
152
|
+
block: res.data?.block,
|
|
153
|
+
};
|
|
154
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
// Feishu tests cover docx table ops plugin behavior.
|
|
2
|
+
import { describe, expect, it } from "vitest";
|
|
3
|
+
import { cleanBlocksForDescendant } from "./docx-table-ops.js";
|
|
4
|
+
|
|
5
|
+
describe("cleanBlocksForDescendant", () => {
|
|
6
|
+
it("removes parent links and read-only table fields while normalizing table cells", () => {
|
|
7
|
+
const blocks = [
|
|
8
|
+
{
|
|
9
|
+
block_id: "table-1",
|
|
10
|
+
parent_id: "parent-1",
|
|
11
|
+
block_type: 31,
|
|
12
|
+
children: "cell-1",
|
|
13
|
+
table: {
|
|
14
|
+
property: {
|
|
15
|
+
row_size: 1,
|
|
16
|
+
column_size: 1,
|
|
17
|
+
column_width: [240],
|
|
18
|
+
},
|
|
19
|
+
cells: ["cell-1"],
|
|
20
|
+
merge_info: [{ row_span: 1, col_span: 1 }],
|
|
21
|
+
},
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
block_id: "cell-1",
|
|
25
|
+
parent_id: "table-1",
|
|
26
|
+
block_type: 32,
|
|
27
|
+
children: "text-1",
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
block_id: "text-1",
|
|
31
|
+
parent_id: "cell-1",
|
|
32
|
+
block_type: 2,
|
|
33
|
+
text: {
|
|
34
|
+
elements: [{ text_run: { content: "hello" } }],
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
];
|
|
38
|
+
|
|
39
|
+
const cleaned = cleanBlocksForDescendant(blocks);
|
|
40
|
+
|
|
41
|
+
expect(cleaned[0]).not.toHaveProperty("parent_id");
|
|
42
|
+
expect(cleaned[1]).not.toHaveProperty("parent_id");
|
|
43
|
+
expect(cleaned[2]).not.toHaveProperty("parent_id");
|
|
44
|
+
|
|
45
|
+
expect(cleaned[0]?.table).toEqual({
|
|
46
|
+
property: {
|
|
47
|
+
row_size: 1,
|
|
48
|
+
column_size: 1,
|
|
49
|
+
column_width: [240],
|
|
50
|
+
},
|
|
51
|
+
});
|
|
52
|
+
expect(cleaned[1]?.children).toEqual(["text-1"]);
|
|
53
|
+
});
|
|
54
|
+
});
|