feat(im,datasource): support Lark, Feishu's international edition

Lark and Feishu are the same product deployed on two isolated clouds
(open.larksuite.com and open.feishu.cn). WeKnora touches both from two
places — the IM bot and the Wiki data-source connector — and this brings
Lark to parity across both. Neither API surface differs, so rather than
duplicate either implementation, each is parametrized by a Region and
registered twice.

IM (internal/im/feishu):
  Every API call routes through a.api(), which prefixes the region's
  host, and the WebSocket client passes larkws.WithDomain — the SDK
  defaults to open.feishu.cn, so Lark long connections would otherwise
  fail to authenticate.

Data source (internal/datasource/connector/feishu):
  Lark was half-supported: the client honoured a base_url override and a
  LarkBaseURL constant existed, but the connector registered only as
  "feishu" and the UI never exposed base_url, so Lark wikis could not be
  configured at all. Lark is now its own connector type. base_url is kept
  as an explicit override so data sources that pointed a "feishu"
  connector at open.larksuite.com keep working.

Bugs found and fixed along the way:

- Wiki resource links were hardcoded to https://feishu.cn/wiki/{token} in
  four places, so a Lark wiki linked into the wrong cloud entirely.
- Bot identity is now platform-prefixed (feishu:cli_x vs lark:cli_x).
  BotIdentity carries a unique index, so without this a Lark channel
  would be rejected as a duplicate of an unrelated Feishu bot sharing an
  app_id.
- The card image cache is now keyed by app_id. image_keys are issued per
  app on one cloud; a Feishu key handed to a Lark app fails the card
  update with code=200570.
- The datasource doc claimed Lark "adapts automatically" — it could not,
  since the UI had no way to reach base_url.

Permissions are where the clouds genuinely diverge. Identical APIs do not
mean identical permission catalogues: aily:file:read, aily:file:write and
corehr:file:download exist on Feishu but not on Lark, so pasting Feishu's
scope JSON into Lark fails to import. That JSON is also over-broad (it
covers products WeKnora never calls) and simultaneously incomplete (it
omits the drive:*/docx:* scopes the connector needs). Lark therefore gets
its own list, derived from the 17 endpoints the code actually calls and
checked identifier-by-identifier against Lark's own API reference, plus a
full Feishu→Lark mapping table marking each scope verified, unverified,
or absent. Both features use tenant_access_token only — no user_access_
token exists anywhere in the repo — so the user dimension stays empty.

Known limitation: Region.ThinkingText makes Lark's initial card
placeholder English, but once streaming starts the think-block headers
come from the shared MarkdownThinkStyle and are Chinese on every platform
(Lark, Slack, Telegram, Mattermost alike). Fixing that means threading a
locale through FormatIMDisplayContent, which has no platform context — a
cross-platform change, not a Lark one. Documented as such.

The channel-creation 400 message is now derived from validIMPlatforms so
the list cannot drift as platforms are added.

Tests: region invariants and per-region URL construction on both sides,
the Feishu/Lark bot-identity split, platform-to-channel mapping, base_url
override precedence, an end-to-end SendReply against a fake Open Platform
(token fetch, reply, thread-unsupported fallback, hard-error surfacing),
and a ListResources drive asserting wiki links never leak the other
cloud's host.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
qilifan
2026-07-16 23:34:53 +08:00
committed by lyingbug
parent 3edd9c2c84
commit 9afb8b7b68
41 changed files with 1187 additions and 160 deletions
@@ -384,7 +384,7 @@ func TestParseFeishuConfig(t *testing.T) {
"app_secret": "sec1",
"base_url": "https://open.feishu.cn",
},
})
}, RegionFeishu)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
@@ -394,7 +394,7 @@ func TestParseFeishuConfig(t *testing.T) {
})
t.Run("nil config", func(t *testing.T) {
_, err := parseFeishuConfig(nil)
_, err := parseFeishuConfig(nil, RegionFeishu)
if err == nil {
t.Fatal("expected error for nil config")
}
@@ -406,7 +406,7 @@ func TestParseFeishuConfig(t *testing.T) {
"app_id": "id1",
// missing app_secret
},
})
}, RegionFeishu)
if err == nil {
t.Fatal("expected error for missing app_secret")
}
@@ -418,7 +418,7 @@ func TestParseFeishuConfig(t *testing.T) {
// ──────────────────────────────────────────────────────────────────────
func TestConnectorType(t *testing.T) {
c := NewConnector()
c := NewConnector(RegionFeishu)
if c.Type() != types.ConnectorTypeFeishu {
t.Errorf("Type() = %q, want %q", c.Type(), types.ConnectorTypeFeishu)
}
@@ -428,7 +428,7 @@ func TestConnectorValidate(t *testing.T) {
ts, cfg := fakeFeishu(nil)
defer ts.Close()
c := NewConnector()
c := NewConnector(RegionFeishu)
err := c.Validate(context.Background(), makeConfig(cfg, nil))
if err != nil {
t.Fatalf("Validate() error: %v", err)
@@ -436,7 +436,7 @@ func TestConnectorValidate(t *testing.T) {
}
func TestConnectorValidate_BadCredentials(t *testing.T) {
c := NewConnector()
c := NewConnector(RegionFeishu)
err := c.Validate(context.Background(), &types.DataSourceConfig{
Credentials: map[string]interface{}{
"app_id": "bad",
@@ -453,7 +453,7 @@ func TestConnectorListResources(t *testing.T) {
ts, cfg := fakeFeishu(nil)
defer ts.Close()
c := NewConnector()
c := NewConnector(RegionFeishu)
resources, err := c.ListResources(context.Background(), makeConfig(cfg, nil), "")
if err != nil {
t.Fatalf("ListResources() error: %v", err)
@@ -492,7 +492,7 @@ func TestConnectorListResources_LazyLoadsOneLevel(t *testing.T) {
ts, cfg := fakeFeishuHierarchy(topNodes, childNodes, "")
defer ts.Close()
c := NewConnector()
c := NewConnector(RegionFeishu)
// Root listing: only the space, no descendants.
spaces, err := c.ListResources(context.Background(), makeConfig(cfg, nil), "")
@@ -555,7 +555,7 @@ func TestConnectorResolveResourceAncestors(t *testing.T) {
ts, cfg := fakeFeishuHierarchy(topNodes, childNodes, "")
defer ts.Close()
c := NewConnector()
c := NewConnector(RegionFeishu)
// A deeply nested node resolves to its space plus every intermediate parent.
ancestors, err := c.ResolveResourceAncestors(
@@ -615,7 +615,7 @@ func TestFetchAll_DocxNode(t *testing.T) {
ts, cfg := fakeFeishu(nodes)
defer ts.Close()
c := NewConnector()
c := NewConnector(RegionFeishu)
items, err := c.FetchAll(context.Background(), makeConfig(cfg, []string{"space1"}), []string{"space1"})
if err != nil {
t.Fatalf("FetchAll() error: %v", err)
@@ -657,7 +657,7 @@ func TestFetchAll_SheetNode(t *testing.T) {
ts, cfg := fakeFeishu(nodes)
defer ts.Close()
c := NewConnector()
c := NewConnector(RegionFeishu)
items, err := c.FetchAll(context.Background(), makeConfig(cfg, []string{"space1"}), []string{"space1"})
if err != nil {
t.Fatalf("FetchAll() error: %v", err)
@@ -682,7 +682,7 @@ func TestFetchAll_BitableNode(t *testing.T) {
ts, cfg := fakeFeishu(nodes)
defer ts.Close()
c := NewConnector()
c := NewConnector(RegionFeishu)
items, err := c.FetchAll(context.Background(), makeConfig(cfg, []string{"space1"}), []string{"space1"})
if err != nil {
t.Fatalf("FetchAll() error: %v", err)
@@ -706,7 +706,7 @@ func TestFetchAll_FileNode(t *testing.T) {
ts, cfg := fakeFeishu(nodes)
defer ts.Close()
c := NewConnector()
c := NewConnector(RegionFeishu)
items, err := c.FetchAll(context.Background(), makeConfig(cfg, []string{"space1"}), []string{"space1"})
if err != nil {
t.Fatalf("FetchAll() error: %v", err)
@@ -736,7 +736,7 @@ func TestFetchAll_SkipsMindnoteAndSlides(t *testing.T) {
ts, cfg := fakeFeishu(nodes)
defer ts.Close()
c := NewConnector()
c := NewConnector(RegionFeishu)
items, err := c.FetchAll(context.Background(), makeConfig(cfg, []string{"space1"}), []string{"space1"})
if err != nil {
t.Fatalf("FetchAll() error: %v", err)
@@ -760,7 +760,7 @@ func TestFetchAll_MixedTypes(t *testing.T) {
ts, cfg := fakeFeishu(nodes)
defer ts.Close()
c := NewConnector()
c := NewConnector(RegionFeishu)
items, err := c.FetchAll(context.Background(), makeConfig(cfg, []string{"space1"}), []string{"space1"})
if err != nil {
t.Fatalf("FetchAll() error: %v", err)
@@ -783,7 +783,8 @@ func TestFetchAll_ChildNodeListErrorReturnsPartialItems(t *testing.T) {
ts, cfg := fakeFeishuWithChildFailure(nodes, "nt-parent")
defer ts.Close()
items, err := NewConnector().FetchAll(context.Background(), makeConfig(cfg, []string{"space1"}), []string{"space1"})
conn := NewConnector(RegionFeishu)
items, err := conn.FetchAll(context.Background(), makeConfig(cfg, []string{"space1"}), []string{"space1"})
if err != nil {
t.Fatalf("FetchAll must not abort when one child listing fails: %v", err)
}
@@ -838,7 +839,8 @@ func TestFetchAll_WikiNodeResourceSyncsSelectedSubtree(t *testing.T) {
defer ts.Close()
resourceID := "space1:nt-root"
items, err := NewConnector().FetchAll(context.Background(), makeConfig(cfg, []string{resourceID}), []string{resourceID})
conn := NewConnector(RegionFeishu)
items, err := conn.FetchAll(context.Background(), makeConfig(cfg, []string{resourceID}), []string{resourceID})
if err != nil {
t.Fatalf("FetchAll() error: %v", err)
}
@@ -876,7 +878,7 @@ func TestFetchIncremental_FirstSync(t *testing.T) {
ts, cfg := fakeFeishu(nodes)
defer ts.Close()
c := NewConnector()
c := NewConnector(RegionFeishu)
dsConfig := makeConfig(cfg, []string{"space1"})
// First sync with no cursor → all items should be fetched
@@ -903,7 +905,7 @@ func TestFetchIncremental_NoChanges(t *testing.T) {
ts, cfg := fakeFeishu(nodes)
defer ts.Close()
c := NewConnector()
c := NewConnector(RegionFeishu)
dsConfig := makeConfig(cfg, []string{"space1"})
// First sync
@@ -931,7 +933,7 @@ func TestFetchIncremental_DetectsDeleted(t *testing.T) {
}
ts, cfg := fakeFeishu(allNodes)
c := NewConnector()
c := NewConnector(RegionFeishu)
dsConfig := makeConfig(cfg, []string{"space1"})
_, cursor1, err := c.FetchIncremental(context.Background(), dsConfig, nil)
@@ -971,7 +973,7 @@ func TestFetchIncremental_NoResourceIDs(t *testing.T) {
ts, cfg := fakeFeishu(nil)
defer ts.Close()
c := NewConnector()
c := NewConnector(RegionFeishu)
dsConfig := makeConfig(cfg, nil) // no resource IDs
dsConfig.ResourceIDs = nil
@@ -992,7 +994,8 @@ func TestFetchIncremental_ChildNodeListErrorReturnsPartialItemsAndCursor(t *test
ts, cfg := fakeFeishuWithChildFailure(nodes, "nt-parent")
defer ts.Close()
items, cursor, err := NewConnector().FetchIncremental(context.Background(), makeConfig(cfg, []string{"space1"}), nil)
conn := NewConnector(RegionFeishu)
items, cursor, err := conn.FetchIncremental(context.Background(), makeConfig(cfg, []string{"space1"}), nil)
if err != nil {
t.Fatalf("FetchIncremental must not abort when one child listing fails: %v", err)
}
@@ -1029,7 +1032,8 @@ func TestFetchIncremental_ChildNodeListErrorDoesNotDeletePreviouslySeenChildren(
}
ts, cfg := fakeFeishuHierarchy(firstNodes, firstChildren, "")
firstItems, cursor, err := NewConnector().FetchIncremental(context.Background(), makeConfig(cfg, []string{"space1"}), nil)
conn := NewConnector(RegionFeishu)
firstItems, cursor, err := conn.FetchIncremental(context.Background(), makeConfig(cfg, []string{"space1"}), nil)
if err != nil {
t.Fatalf("first sync error: %v", err)
}
@@ -1044,7 +1048,7 @@ func TestFetchIncremental_ChildNodeListErrorDoesNotDeletePreviouslySeenChildren(
ts2, cfg2 := fakeFeishuHierarchy(secondNodes, nil, "nt-parent")
defer ts2.Close()
items, nextCursor, err := NewConnector().FetchIncremental(context.Background(), makeConfig(cfg2, []string{"space1"}), cursor)
items, nextCursor, err := conn.FetchIncremental(context.Background(), makeConfig(cfg2, []string{"space1"}), cursor)
if err != nil {
t.Fatalf("partial second sync error: %v", err)
}