Skip to content

feat(feishu): add IM platform adapter - #46

Merged
contrueCT merged 19 commits into
mainfrom
task/feishu-im-platform
May 10, 2026
Merged

feat(feishu): add IM platform adapter#46
contrueCT merged 19 commits into
mainfrom
task/feishu-im-platform

Conversation

@contrueCT

@contrueCT contrueCT commented May 4, 2026

Copy link
Copy Markdown
Owner

描述 / Summary

本 PR 为 platform-feishu 增加 Feishu IM 平台接入层的可测试基础闭环,使飞书 IM 能力进入 Nine1Bot 多平台适配架构,而不是继续扩展旧的 packages/nine1bot/src/feishu 临时实现。

本阶段重点完成平台级 IM 架构、会话管理、Controller Bridge、Reply Sink、卡片交互、buffer / abort / thread 并行和 streaming card 能力。当前 PR 不切换生产 websocket,不删除旧 Feishu 主实现;旧 feishu.enabled 场景下新 IM background service 保持 staged/degraded,避免双连接。

类型 / Type of change

  • 新功能 (feature)
  • Bug 修复 (bugfix)
  • 文档 (docs)
  • 重构 (refactor)
  • 其他 (describe):

关联的 issue(如果有)/ Related issues

暂无。

变更点 / What changed

  • 新增通用 Platform Background Service lifecycle 接入点,并让 Feishu IM 通过 platform-feishu contribution 声明后台服务。
  • 在 Feishu 平台 descriptor 中注册 IM 配置项,覆盖 enabled、账号、secret ref、消息缓冲、DM / 群聊策略、回复策略、streaming card 等设置。
  • 新增 packages/platform-feishu/src/im IM 子模块,包含 config normalize、gateway 抽象、parse、dedup、gate、route key、binding store、history、message buffer 等基础逻辑。
  • 新增 FeishuIMSessionManager,支持 DM / group / thread route 隔离、session binding 复用、/new/cwd/project、消息缓冲、busy reject、abort 和 thread 并行。
  • 新增 FeishuControllerBridge,通过公开 HTTP Controller API 创建 session、发送消息、读取 project/session、abort turn,不反向 import Nine1Bot 产品层或 opencode server 内部实现。
  • 新增 Reply Sink 和简化卡片能力,支持 agent 输出、final/error/timeout、permission/question、/control、项目切换、当前 cwd/session 展示等交互。
  • 新增 streaming card 能力,支持节流更新、CardKit native streaming、message patch fallback、最终文本/卡片降级、tool-use 状态展示和相关 telemetry。
  • 保持旧 packages/nine1bot/src/feishu 生产入口不变,本 PR 只提供新 IM 能力的 staged 可测试实现。

如何测试 / How to test

  1. 运行 Feishu 平台包测试:
    bun test packages/platform-feishu/test
  2. 运行 Feishu 平台包类型检查:
    cd packages/platform-feishu && bun run typecheck
  3. 运行平台协议类型检查:
    cd packages/platform-protocol && bun run typecheck
  4. 运行 Nine1Bot 后端类型检查:
    cd packages/nine1bot && bun run typecheck
  5. 预期结果:上述测试和类型检查均通过;旧 Feishu 入口未被切换,新 IM service 在 legacy feishu.enabled 场景下保持 staged/degraded,不抢占 websocket。

截图(UI 变更时提供)/ Screenshots for UI changed

暂无。本 PR 主要是平台 descriptor 驱动的配置项与状态卡片接入,没有新增定制 UI 页面。

Checklist(合并前请确认) / Checklist

  • 本地已通过测试
  • 新配置均通过 Feishu 平台 descriptor 注册到多平台配置页
  • secret 配置使用 secret ref,不在 imAccounts 中接受明文 appSecret
  • 新 IM background service 不在旧 feishu.enabled 场景下抢占 websocket
  • platform-feishu 不反向依赖 Nine1Bot 产品层或 opencode server 内部实现

备注 / Notes for reviewers

  • 建议重点审查 packages/platform-feishu/src/im/session-manager.tsreply-sink.tsstreaming-card-controller.tsnode/reply-client.ts
  • 本 PR 是 Feishu IM 新链路的 staged implementation,不包含生产 cutover;真实 websocket 接入和旧 Feishu 实现删除应在后续独立阶段进行。
  • Streaming card 的 fallback 顺序为 CardKit streaming -> message patch -> 单次降级提示 + 最终文本/卡片。
  • turn.abort 卡片 action 要求 account / route / session / turn 完整匹配,避免旧卡片误操作当前 turn。

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces a staged Feishu/Lark IM adapter layer (background service + session manager + controller bridge + reply rendering) and wires a generic “platform background services” lifecycle into the core platform manager/orchestrator, while keeping the legacy Feishu websocket entrypoint unchanged.

Changes:

  • Extend @nine1bot/platform-protocol and PlatformAdapterManager to support per-platform background services (start/stop + status reporting).
  • Add Feishu IM skeleton: config normalization/validation, gateway/parse/dedup/gating, route keys + bindings, session manager (buffering/busy/abort/control commands), HTTP controller bridge, and reply sink with cards + streaming CardKit support + telemetry.
  • Add/extend tests for Feishu IM components and platform manager background service lifecycle.

Reviewed changes

Copilot reviewed 38 out of 38 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
packages/platform-protocol/src/index.ts Adds background service + controller bridge types to the platform protocol.
packages/platform-feishu/test/feishu-platform.test.ts Asserts Feishu descriptor exposes IM reply-related settings.
packages/platform-feishu/test/feishu-im.test.ts Tests IM config normalization/validation, parsing, gating, dedup, routing, bindings, and legacy-staging status.
packages/platform-feishu/test/feishu-im-session-manager.test.ts Tests session routing, buffering/flush, abort, busy behavior, thread parallelism, and control commands.
packages/platform-feishu/test/feishu-im-reply.test.ts Tests reply sink behaviors (text/card/streaming-card), CardKit streaming, fallbacks, interactions, and telemetry.
packages/platform-feishu/test/feishu-im-controller-bridge.test.ts Tests the HTTP controller bridge mapping to public controller APIs, including 409 busy responses.
packages/platform-feishu/src/runtime.ts Registers IM config fields/actions; merges IM runtime status into overall Feishu status.
packages/platform-feishu/src/node.ts Exports Feishu IM node helpers (controller bridge + reply client).
packages/platform-feishu/src/index.ts Re-exports IM module from the package entrypoint.
packages/platform-feishu/src/im/types.ts Defines Feishu IM domain types (config, routing, control results, runtime snapshots).
packages/platform-feishu/src/im/streaming-card-controller.ts Implements streaming card transport (CardKit → patch → text) with throttling and sanitization.
packages/platform-feishu/src/im/store/binding-store.ts Adds an in-memory binding store for route→session bindings.
packages/platform-feishu/src/im/session-manager.ts Implements message buffering, session reuse/reset, abort, control commands, and busy semantics.
packages/platform-feishu/src/im/runtime.ts Implements staged Feishu IM background service + runtime status snapshotting/cards.
packages/platform-feishu/src/im/route.ts Implements route-key construction/serialization/parsing for DM/group/thread.
packages/platform-feishu/src/im/reply-telemetry.ts Tracks IM reply runtime counters and last-error metadata for status display.
packages/platform-feishu/src/im/reply-sink.ts Bridges controller runtime events to Feishu replies (text/cards/streaming + interactions).
packages/platform-feishu/src/im/reply-coordinator.ts Provides factories for reply sinks and immediate replies from session-manager results.
packages/platform-feishu/src/im/reply-client.ts Defines reply client interface and an in-memory implementation for tests.
packages/platform-feishu/src/im/node/reply-client.ts Implements a Node reply client using Lark/Feishu SDK APIs (message + CardKit).
packages/platform-feishu/src/im/node/http-controller-bridge.ts Implements controller bridge over HTTP + SSE event subscription.
packages/platform-feishu/src/im/interactions.ts Implements card action payloads, parsing/validation, and interaction answering.
packages/platform-feishu/src/im/index.ts Public export surface for the Feishu IM module.
packages/platform-feishu/src/im/inbound/parse.ts Parses incoming Feishu IM events into normalized message objects.
packages/platform-feishu/src/im/inbound/gate.ts Implements gating policy (allowlist, DM/group policy, mention-only).
packages/platform-feishu/src/im/history.ts Implements in-memory group history store (TTL + limit).
packages/platform-feishu/src/im/gateway.ts Adds a minimal gateway handle abstraction (start/stop/inject).
packages/platform-feishu/src/im/dedup.ts Adds an event deduplicator with TTL and max-size pruning.
packages/platform-feishu/src/im/controller-bridge.ts Defines the controller bridge interface + capabilities and helper utilities.
packages/platform-feishu/src/im/config.ts Adds IM config normalization, validation, defaults, and secret-ref enforcement.
packages/platform-feishu/src/im/cards.ts Renders Feishu cards (turn/control/permission/question/streaming + CardKit v2).
packages/platform-feishu/src/im/buffer/message-buffer.ts Adds per-route message buffering with flush/max timers and snapshots.
packages/platform-feishu/src/im/abort.ts Defines abort keywords and normalization helpers.
packages/platform-feishu/package.json Adds @larksuiteoapi/node-sdk dependency.
packages/nine1bot/src/platform/manager.ts Adds background service start/stop lifecycle and runtime status application.
packages/nine1bot/src/platform/manager.test.ts Adds tests for starting/stopping/restarting background services.
packages/nine1bot/src/platform/builtin.ts Exposes helpers to start/stop background services for built-in platforms.
packages/nine1bot/src/launcher/orchestrator.ts Starts/stops platform background services during app launch/shutdown.
Comments suppressed due to low confidence (1)

packages/nine1bot/src/platform/manager.ts:221

  • configure() triggers stopBackgroundServices() without awaiting it. Because stopBackgroundServices() clears the registry before awaiting the individual handle.stop() calls, a subsequent startBackgroundServices() (or another reconfigure) can start new services while old ones are still stopping, leaving overlapping background services running briefly. Consider making the stop operation serialized (e.g. track an in-flight stop promise that startBackgroundServices()/configure() await) or making configure async so it can await a full stop.
  configure(config: PlatformManagerConfig) {
    void this.stopBackgroundServices()
    this.unregisterRuntimeAdapters()
    this.config = normalizeConfig(config)
    this.rebuildRecords()
  }

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread packages/platform-feishu/src/platform-runtime.ts
Comment thread packages/platform-feishu/src/im/inbound/parse.ts Outdated
Comment thread packages/platform-feishu/src/im/node/http-controller-bridge.ts
Comment thread packages/platform-feishu/src/im/node/http-controller-bridge.ts
@contrueCT
contrueCT merged commit 183c39b into main May 10, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants