mirror of
https://github.com/multica-ai/multica.git
synced 2026-07-28 22:17:48 +02:00
* feat(integrations): add platform-agnostic channel foundation Introduce server/internal/integrations/channel — the contract every inbound IM integration implements, so the core never learns a platform's event JSON. Four pieces: - Channel interface (Type/Connect/Disconnect/Send/Capabilities) + Factory + Config (channel_type + opaque JSON blob, maps to channel_installation). - Normalized InboundMessage/OutboundMessage envelopes + Source/MediaRef/ ReplyCtx/MsgType/ChatType. Envelope holds only cross-platform-true fields; platform specifics live in Raw, read only by the adapter. - Capability bitmask: declaration only, no degrade logic in core. - Registry: Type->Factory map, last-writer-wins, concurrency-safe. Pure package (no DB/network/platform deps). Foundation for MUL-3515; the lark cutover + lark_*->channel_* generalization land in follow-up PRs. MUL-3515 Co-authored-by: multica-agent <github@multica.ai> * feat(channel): generalize lark_* tables into channel_* (DB layer) Migration 123 creates channel_installation / channel_user_binding / channel_chat_session_binding / channel_inbound_message_dedup / channel_inbound_audit / channel_outbound_card_message / channel_binding_token. Each carries a channel_type discriminator and a JSONB config for platform-specific identifiers/credentials; cross-platform columns stay flat. Existing Feishu rows are backfilled (channel_type= 'feishu', app_secret_encrypted via base64). NO foreign keys / cascades (MUL-3515 §4) — integrity moves to the app layer in the cutover. queries/channel.sql ports the lark query surface to channel_*, JSONB-aware, plus DeleteChannelUserBindingsByWorkspaceMember / DeleteChannelChatSessionBindingBySession for the app-layer cleanup that replaces the removed cascades. lark_* tables/queries are left in place here and removed once the Go cutover lands, so this commit ships green on its own. Verified: sqlc generate, go build ./..., full migrate chain (1..123) on Postgres 17, and a real-data backfill spot-check (base64 round-trip, NULL-strip, functional unique index on (channel_type, app_id)). MUL-3515 Co-authored-by: multica-agent <github@multica.ai> * fix(channel): name app_id query param + multi-IM install key + null-safe binding merge Addresses review on MUL-3515 (PR #4412): - GetChannelInstallationByAppID: explicitly name params and cast app_id to ::text so sqlc emits AppID string. A bare $2 next to `config ->> 'app_id'` was mis-attributed to the JSONB config column, generating Config []byte. - channel_installation uniqueness -> (workspace_id, agent_id, channel_type), with the UpsertChannelInstallation conflict key matched. Lets one agent hold one installation per IM (feishu + slack + ...) instead of a later install clobbering an earlier one. Behaviorally identical in the current feishu-only world; "one agent, at most one IM overall" stays an app-layer rule per MUL-3515 §4, not a DB constraint. - CreateChannelUserBinding merges jsonb_strip_nulls(EXCLUDED.config) so a re-bind carrying {"union_id": null} no longer erases an already-captured union_id, restoring the old COALESCE(EXCLUDED.union_id, ...) semantics. Regenerated with sqlc v1.31.1. Verified on PG17: re-install replaces in place, feishu+slack coexist, null re-bind keeps union_id, real union_id wins. Co-authored-by: multica-agent <github@multica.ai> * feat(lark): channel-backed Feishu store + fix base64 backfill wrapping Cutover step 1 of switching the lark Go code from lark_* onto the channel_* tables (MUL-3515). Introduces the JSONB config boundary the rest of the cutover sits on, and fixes a latent backfill bug surfaced while building it. - migration 123: strip newlines from the app_secret_encrypted base64 backfill. PostgreSQL encode(...,'base64') MIME-wraps at 76 chars, and a secretbox- sealed ~72-byte secret exceeds that. Go's encoding/json decodes a JSON string into []byte with base64.StdEncoding, which rejects embedded newlines, so without the strip every migrated installation would fail to decrypt its app secret once reads move to channel_installation.config. - store.go: flat domain types (Installation / UserBinding / ChatSessionBinding) with field parity to the retired db.Lark* rows, plus the feishu config codec. Row->domain mappers decode the JSONB config; the secret decoder is whitespace-tolerant so legacy MIME-wrapped data still round-trips, while the encoder emits unwrapped base64. Binding config encodes an absent union_id as "{}" so the upsert's jsonb_strip_nulls merge never clobbers a stored union_id. - store_test.go: 72-byte secret round-trip, MIME-wrapped tolerance, optional null-strip, and flat-column preservation. Verified on PG17. Field parity keeps the upcoming ~190 db.LarkInstallation call sites a mechanical rename. No call sites switched yet; behavior unchanged. Co-authored-by: multica-agent <github@multica.ai> * feat(lark): route inbound integration onto channel_* + explicit membership checks Cutover step 2 (MUL-3515): switch the Feishu Go code from the lark_* queries to channel_* via a ChannelStore adapter, and replace the removed member foreign key with explicit application-layer membership checks. No user-visible behavior change. - channel_store.go: ChannelStore embeds *db.Queries and SHADOWS the ~24 lark query methods with channel_*-backed equivalents, keeping the db.Lark* signatures so the dispatcher/hub/services and their ~20k lines of tests stay untouched; the feishu JSONB config is (de)coded by store.go. Adds IsWorkspaceMember and a tx-aware WithTx. Only production wiring swaps *db.Queries for *ChannelStore. - Membership re-check (§4 removed the lark_user_binding -> member FK, so a binding row no longer proves current membership): * the dispatcher inbound identity step verifies membership after the binding lookup; a former member's stale binding is dropped as non_workspace_member + audited and never reaches chat_session (§4.3 safety property). * RedeemAndBind and BindInstallerTx replace the now-dead FK (23503) branch with an explicit IsWorkspaceMember gate, preserving the existing ErrBindingNotWorkspaceMember outcome without burning the token. - router wires the ChannelStore into the patcher, typing indicator, dispatcher, hub, and the union_id/region backfills; constructor-based services wrap *db.Queries internally so their signatures and nil-check tests are unchanged. Verified: go build ./... ; go vet ; gofmt ; go test -race ./internal/integrations/... (full lark suite green unchanged + new membership drop/error tests). Adapter field mappings (secret base64, union_id RMW, chat-id/open-id remaps, dedup, token, card) checked end-to-end against a PG17 channel_* schema. lark_* tables and queries remain (unused at runtime) until the S3 cleanup-hooks and S4 drop-tables/rename commits. Co-authored-by: multica-agent <github@multica.ai> * fix(channel): renumber generalization migration 123 -> 124 main merged 123_issue_stage after this branch forked, so the branch's 123_channel_generalization now collides on the migration number. The runner keys schema_migrations by full version string and would still apply both, but a duplicate number is a merge hazard and convention violation, so move the channel migration to the next free slot (124). issue_stage (ALTER issue ADD COLUMN stage) and the channel generalization touch disjoint tables; verified on PG17 that 123_issue_stage applies cleanly on a DB already carrying 124_channel_generalization, so the two are order-independent. sqlc regenerated (v1.31.1): only the migration-number comment changed. MUL-3515 Co-authored-by: multica-agent <github@multica.ai> * feat(channel): prune channel bindings on member removal + chat session delete MUL-3515 §4 dropped every channel_* foreign key, so the old ON DELETE CASCADE that cleared a user's channel_user_binding when they left a workspace, and a chat's channel_chat_session_binding when its chat_session was deleted, no longer fires. Re-establish that integrity in the application layer, inside the existing transactions: revokeAndRemoveMember -> DeleteChannelUserBindingsByWorkspaceMember, DeleteChatSession -> DeleteChannelChatSessionBindingBySession. Adds real-DB tests for both paths, including a scoping check that a remaining member's binding survives the prune. Verified on PG17: both new tests plus the existing revocation tests and the full handler package pass. MUL-3515 Co-authored-by: multica-agent <github@multica.ai> * fix(channel): scope Lark/Feishu store reads to channel_type='feishu' The S2 cutover routed the Feishu integration onto channel_*, but the Lark-facing ChannelStore wrappers read installation / chat-session-binding / outbound-card rows across ALL channel_type values. Once a second IM exists, that would let the Lark hub supervise a non-Feishu installation, the Lark install list show it, /lark/installations/{id} revoke another channel's row, and the outbound patcher / typing indicator act on a non-Feishu chat binding or card. Add a channel_type predicate to the six read/list channel queries and pass channelTypeFeishu from every wrapper: GetChannelInstallation, GetChannelInstallationInWorkspace, ListChannelInstallationsByWorkspace, ListActiveChannelInstallations, GetChannelChatSessionBindingBySession, GetChannelOutboundCardByTask. The S3 cleanup deletes (DeleteChannelUserBindingsByWorkspaceMember / DeleteChannelChatSessionBindingBySession) stay all-channel on purpose: a member leaving or a chat_session being deleted should clear every IM's binding. Adds a real-DB test that seeds a Slack installation/binding/card next to the Feishu ones and asserts the Lark wrappers never return them. MUL-3515 Co-authored-by: multica-agent <github@multica.ai> * refactor(channel): replace db.Lark* translation layer with lark domain types S2 introduced ChannelStore as a translation layer that read/wrote channel_* but kept the retired db.Lark* struct/param shapes so the dispatcher/hub/services and their ~20k lines of tests did not have to change. This collapses that layer: the store now takes and returns the package's flat domain types (Installation, UserBinding, ChatSessionBinding, InboundMessageDedup, BindingTokenRow, OutboundCardMessage) and the *Params types in params.go, with channel-neutral field names (ChannelUserID / ChannelChatID / ...). All call sites, fakes, and tests move to the domain types. No behavior change: only channel_* is read/written (as before); db.Lark* is now unused, and the lark_* tables + queries/lark.sql are removed in the next commit. Verified on PG17: go build / vet / gofmt clean, go test -race ./internal/integrations/... green (the ~20k-line fake suite), and the lark + handler suites pass. MUL-3515 Co-authored-by: multica-agent <github@multica.ai> * refactor(channel): drop lark_* tables and queries (remove old path) The Go cutover (previous commit) moved the lark package entirely onto channel_* and the domain types, leaving the lark_* tables, queries/lark.sql, and the generated db.Lark* models unused. Remove them per the design (§5: replace, do not keep both): migration 125 drops the seven lark_* tables (data already lives in channel_* since migration 124), and queries/lark.sql is deleted + sqlc regenerated, removing the db.Lark* models and lark query methods. The 125 down recreates the authoritative pre-drop schema (bot_union_id, region, per-installation dedup PK, thread-reply columns). Verified on PG17: fresh migrate up ends with lark_* gone + channel_* present; isolated 125 down/up round-trips correctly; go build / vet / gofmt clean; go test -race ./internal/integrations/... and the handler suite pass. MUL-3515 Co-authored-by: multica-agent <github@multica.ai> * fix(migrations): remove trailing blank line at EOF of 125 down migration git diff --check flagged a blank line at EOF of 125_drop_lark_tables.down.sql (a pg_dump-generation artifact). Whitespace only; the recreate SQL is unchanged. MUL-3515 Co-authored-by: multica-agent <github@multica.ai> * refactor(channel): defer lark_* table drop to a follow-up migration Preflight deploy review: dropping lark_* in the same release that cuts over (old migration 125) is not rollback/rolling-safe — the v0.3.27 release still reads lark_*, so a rolling deploy or a post-deploy code rollback would hit "relation does not exist". Remove the drop and keep the old tables for one release (standard expand/contract): migration 124 already backfilled lark_* -> channel_*, the new code reads/writes only channel_*, and the physical drop moves to a separate cleanup migration once this ships and is observed. The lark_* tables remain in the schema, so sqlc regenerates the (now unused) db.Lark* models; queries/lark.sql stays deleted (the new code uses channel_*). No code path reads lark_* — only the destructive drop is deferred, keeping the design's no-compat-layer / no-dual-write rule while being deploy-safe. MUL-3515 Co-authored-by: multica-agent <github@multica.ai> * fix(channel): skip orphaned installations in hub-boot active scan Preflight deploy review: channel_installation dropped the workspace/agent FK (MUL-3515 §4), so unlike lark_installation it does not cascade away when its workspace is deleted or its agent is hard-deleted (e.g. runtime teardown). The hub-boot query then keeps opening a WebSocket for a bot whose owner is gone. JOIN ListActiveChannelInstallations to live workspace + agent so an orphaned installation is never connected, uniformly for every deletion path. The JOIN matches the old ON DELETE CASCADE semantics (row existence, not agent archival), so an archived-but-present agent's installation is still listed; the orphaned row's encrypted secret is thereby never decrypted/used. Tests: a real-DB handler test asserts a deleted-workspace/agent installation and a non-Feishu one are both excluded; the lark scope test's active-list assertion moved there since the JOIN now needs real workspace/agent fixtures. (Physically deleting dormant orphaned channel rows on workspace/agent deletion is a separate app-layer-cleanup follow-up.) MUL-3515 Co-authored-by: multica-agent <github@multica.ai> * docs(channel): document non-rolling cutover constraint for the lark->channel migration Elon deploy review: keeping the lark_* tables (deferred drop) stops old v0.3.27 code from crashing, but is not full expand/contract. Migration 124 is a one-time backfill; afterwards new code runs on channel_* (lease + dedup on channel_*) while pre-cutover code runs on lark_* (lease + dedup on lark_*). If both run concurrently during a rolling deploy, each side claims the same Feishu bot's WS lease on its own table and double-processes inbound events. This release therefore requires a NON-ROLLING cutover (stop the old hub before applying migration 124 + starting new code; rollback is not lossless once new code writes channel_*). Documented where deployers/reviewers see it: migration 124 header gains a ROLLOUT note; the channel_store.go header is corrected (lark_* tables are retained one release for rollback safety, not "gone"; the store still never touches them). Comment-only — no schema/codegen/behavior change. MUL-3515 Co-authored-by: multica-agent <github@multica.ai> * feat(lark): add MULTICA_LARK_HUB_DISABLED switch for the channel cutover The lark_*->channel_* cutover needs a way to make the Feishu bot briefly unavailable WITHOUT taking down the whole multica-api process — the Lark hub is a goroutine inside it, not a separate Deployment. MULTICA_LARK_HUB_DISABLED=true parks the hub at startup: the API serves HTTP normally but never claims a WS lease or opens a Feishu connection. Rollout (see migration 124 ROLLOUT note): ship the new release with the flag SET so new pods run API-only while old pods (hub on lark_*) drain during the rolling deploy — the two hubs never overlap. After the old pods are gone and migration 124 has run, flip the flag off; the new hub comes up on channel_*. The old backend does NOT need this switch — its hub stops when k8s terminates the old pods, not via a flag. Nil-ing LarkHub reuses the existing not-configured path so both the startup start and the shutdown join skip it. MUL-3515 Co-authored-by: multica-agent <github@multica.ai> * docs(channel): point migration 124 ROLLOUT note at the hub-disable switch Refine the rollout note to use MULTICA_LARK_HUB_DISABLED for a bot-only cutover (new pods serve API with the hub parked while old pods drain; flip the switch off after the migration), instead of the earlier whole-API recreate. Comment-only. MUL-3515 Co-authored-by: multica-agent <github@multica.ai> * docs(channel): fix migration 124 rollout order and document self-host cutover The previous ROLLOUT note shipped the new (channel_*) build before running migration 124, so the channel_*-backed HTTP paths (installation list/install/revoke, chat-session delete, member revoke) would 500 in the window between new-pod boot and the deferred migration. Restate the runbook around two explicit invariants — channel_* must exist before the new build serves those paths, and the old/new hubs must never overlap — and order the steps so channel_* is created first (park old hub -> snapshot -> deploy parked new build -> unpark). Document that default self-host (entrypoint migrate + single-replica Recreate) satisfies both invariants automatically and needs no manual steps; only prd / multi-replica rolling self-host needs the switch procedure. Clarify in main.go that the hub-park switch is generation-agnostic (parks whichever hub the build carries), which is what enables the preparatory release. Refs MUL-3515 Co-authored-by: multica-agent <github@multica.ai> --------- Co-authored-by: J <j@multica.ai> Co-authored-by: multica-agent <github@multica.ai>
319 lines
14 KiB
SQL
319 lines
14 KiB
SQL
-- Generalize the Feishu/Lark-specific integration tables into
|
|
-- platform-agnostic channel_* tables (MUL-3515, parent MUL-3506). Each
|
|
-- lark_* table gains a `channel_type` discriminator and moves its
|
|
-- platform-specific identifiers/config into a JSONB `config` column; the
|
|
-- cross-platform columns stay flat. Existing Feishu rows are backfilled
|
|
-- with channel_type='feishu'.
|
|
--
|
|
-- Two hard rules from the design:
|
|
--
|
|
-- * NO foreign keys and NO cascades (MUL-3515 §4). The lark_* tables
|
|
-- leaned on composite FKs to enforce "a binding's workspace matches
|
|
-- its installation" and "a binding dies when workspace membership is
|
|
-- revoked / a chat_session is deleted". Those integrity rules now
|
|
-- live in the application layer (the cutover PR adds the membership
|
|
-- check + cleanup). The columns are kept so the app can still join,
|
|
-- but the database enforces nothing.
|
|
--
|
|
-- * The lark_* tables are NOT dropped here — that happens in a later
|
|
-- migration once the Go cutover has landed, so this migration can
|
|
-- ship green on its own. This migration only ADDS channel_* and
|
|
-- copies the data forward.
|
|
--
|
|
-- * ROLLOUT. This backfill is a one-time copy and the Lark hub is an
|
|
-- in-process goroutine, so a cutover has TWO independent invariants:
|
|
-- (a) channel_* must exist BEFORE any new (channel_*) build serves the
|
|
-- HTTP paths that touch it — installation list/install/revoke,
|
|
-- chat-session delete, member revoke — or those paths 500.
|
|
-- (b) the OLD (lark_*) hub and the NEW (channel_*) hub must never run at
|
|
-- once: each would claim the same Feishu bot's WS lease on its own
|
|
-- table and open a duplicate connection, double-processing inbound
|
|
-- events (duplicate messages / /issue / runs). The two table sets
|
|
-- never cross-deduplicate.
|
|
--
|
|
-- SELF-HOST (Docker Compose / Helm) satisfies both automatically and needs
|
|
-- NO flags or manual steps. The backend entrypoint runs `migrate up`
|
|
-- before the server starts, so channel_* exists before the new build
|
|
-- serves (a); the deployment is single-replica `Recreate`, so the old pod
|
|
-- (and its hub) fully stops before the new pod starts (b). A normal
|
|
-- version upgrade is a clean cutover. Only a self-host re-tuned to
|
|
-- multi-replica RollingUpdate needs the prd procedure below.
|
|
--
|
|
-- PRD (rolling multica-api, maxUnavailable:0) overlaps old and new pods,
|
|
-- so use the MULTICA_LARK_HUB_DISABLED switch (cmd/server/main.go) to park
|
|
-- a hub while the API stays up. For a clean, drift-free cutover:
|
|
-- 1. Pre-release the hub-park switch on the CURRENT build and set
|
|
-- MULTICA_LARK_HUB_DISABLED=true. The old hub stops everywhere (no
|
|
-- more lease/dedup/binding/thread writes to lark_*); the API stays up.
|
|
-- 2. Run this migration — a clean snapshot, no live hub writing lark_*.
|
|
-- 3. Deploy the channel build with the switch still ON. channel_* already
|
|
-- exists (step 2), so the new HTTP paths never 500, and the new hub
|
|
-- stays parked while old pods drain.
|
|
-- 4. Flip MULTICA_LARK_HUB_DISABLED off — the new hub comes up on
|
|
-- channel_*. Only the Feishu bot is unavailable across steps 1-4; the
|
|
-- API stays up throughout.
|
|
-- The earlier "ship new code, THEN migrate after pods drain" order is
|
|
-- wrong: it serves channel_* HTTP before channel_* exists, violating (a).
|
|
-- Rollback to a pre-cutover build is not lossless once the new hub has
|
|
-- written Feishu state into channel_*. See the PR "Deployment / rollout"
|
|
-- section for the full runbook (incl. a lower-effort single-deploy variant
|
|
-- that trades a small transient drift for one fewer release).
|
|
--
|
|
-- app_secret_encrypted is BYTEA; it is carried into the JSONB config as a
|
|
-- base64 string. PostgreSQL's encode(...,'base64') MIME-wraps the output
|
|
-- with a newline every 76 chars, and a secretbox-sealed app secret (~72
|
|
-- bytes) exceeds that, so we strip the newlines: Go's encoding/json decodes
|
|
-- a base64 string into a []byte field with base64.StdEncoding, which rejects
|
|
-- embedded newlines. Stripping keeps the bytea -> JSON -> []byte round-trip
|
|
-- symmetric (the Go writer emits unwrapped base64 too) and the ciphertext is
|
|
-- never stored in plaintext.
|
|
|
|
-- =====================
|
|
-- channel_installation
|
|
-- =====================
|
|
CREATE TABLE channel_installation (
|
|
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
|
workspace_id UUID NOT NULL,
|
|
agent_id UUID NOT NULL,
|
|
channel_type TEXT NOT NULL,
|
|
-- Platform-specific identifiers/config. For feishu:
|
|
-- app_id, app_secret_encrypted (base64), tenant_key, bot_open_id,
|
|
-- bot_union_id, region.
|
|
config JSONB NOT NULL DEFAULT '{}'::jsonb,
|
|
status TEXT NOT NULL DEFAULT 'active'
|
|
CHECK (status IN ('active', 'revoked')),
|
|
ws_lease_token TEXT,
|
|
ws_lease_expires_at TIMESTAMPTZ,
|
|
installer_user_id UUID NOT NULL,
|
|
installed_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
|
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
|
updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
|
-- One installation per (agent, channel_type): an agent may connect more
|
|
-- than one IM at once (feishu + slack + ...), but only one of each kind.
|
|
-- The old lark_installation had UNIQUE(workspace_id, agent_id) because
|
|
-- feishu was the only channel; with a channel_type discriminator the
|
|
-- natural generalization adds it to the key. In the current feishu-only
|
|
-- world this is behaviorally identical (one row per agent). If the
|
|
-- product later wants "one agent, at most one IM regardless of type",
|
|
-- that is an application-layer rule (MUL-3515 §4), not a DB constraint.
|
|
UNIQUE (workspace_id, agent_id, channel_type)
|
|
);
|
|
|
|
CREATE INDEX idx_channel_installation_workspace ON channel_installation(workspace_id);
|
|
CREATE INDEX idx_channel_installation_agent ON channel_installation(agent_id);
|
|
CREATE INDEX idx_channel_installation_lease ON channel_installation(ws_lease_expires_at)
|
|
WHERE status = 'active';
|
|
-- Routing key. Inbound events carry only the platform app identifier
|
|
-- (Feishu app_id); the dispatcher routes on (channel_type, app_id). The
|
|
-- functional unique index replaces the old global UNIQUE(app_id) and is
|
|
-- scoped per channel_type. Rows without an app_id (a future channel that
|
|
-- routes differently) store JSON null here, and Postgres allows many
|
|
-- NULLs in a unique index, so they do not collide.
|
|
CREATE UNIQUE INDEX idx_channel_installation_type_appid
|
|
ON channel_installation(channel_type, (config ->> 'app_id'));
|
|
|
|
INSERT INTO channel_installation (
|
|
id, workspace_id, agent_id, channel_type, config, status,
|
|
ws_lease_token, ws_lease_expires_at, installer_user_id,
|
|
installed_at, created_at, updated_at
|
|
)
|
|
SELECT
|
|
id, workspace_id, agent_id, 'feishu',
|
|
jsonb_strip_nulls(jsonb_build_object(
|
|
'app_id', app_id,
|
|
'app_secret_encrypted', replace(encode(app_secret_encrypted, 'base64'), E'\n', ''),
|
|
'tenant_key', tenant_key,
|
|
'bot_open_id', bot_open_id,
|
|
'bot_union_id', bot_union_id,
|
|
'region', region
|
|
)),
|
|
status, ws_lease_token, ws_lease_expires_at, installer_user_id,
|
|
installed_at, created_at, updated_at
|
|
FROM lark_installation;
|
|
|
|
-- =====================
|
|
-- channel_user_binding
|
|
-- =====================
|
|
-- channel_user_id is the platform-native, per-installation user id
|
|
-- (Feishu open_id). union_id and any other secondary identity goes in
|
|
-- config. The member-FK that used to make a row's existence proof of
|
|
-- workspace membership is gone; the cutover PR validates membership in
|
|
-- the identity check and prunes bindings on member removal.
|
|
CREATE TABLE channel_user_binding (
|
|
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
|
workspace_id UUID NOT NULL,
|
|
multica_user_id UUID NOT NULL,
|
|
installation_id UUID NOT NULL,
|
|
channel_type TEXT NOT NULL,
|
|
channel_user_id TEXT NOT NULL,
|
|
config JSONB NOT NULL DEFAULT '{}'::jsonb,
|
|
bound_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
|
UNIQUE (installation_id, channel_user_id)
|
|
);
|
|
|
|
CREATE INDEX idx_channel_user_binding_user
|
|
ON channel_user_binding(multica_user_id, workspace_id);
|
|
CREATE INDEX idx_channel_user_binding_workspace_user
|
|
ON channel_user_binding(workspace_id, channel_user_id);
|
|
|
|
INSERT INTO channel_user_binding (
|
|
id, workspace_id, multica_user_id, installation_id,
|
|
channel_type, channel_user_id, config, bound_at
|
|
)
|
|
SELECT
|
|
id, workspace_id, multica_user_id, installation_id,
|
|
'feishu', lark_open_id,
|
|
jsonb_strip_nulls(jsonb_build_object('union_id', union_id)),
|
|
bound_at
|
|
FROM lark_user_binding;
|
|
|
|
-- =====================
|
|
-- channel_chat_session_binding
|
|
-- =====================
|
|
CREATE TABLE channel_chat_session_binding (
|
|
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
|
chat_session_id UUID NOT NULL,
|
|
installation_id UUID NOT NULL,
|
|
channel_type TEXT NOT NULL,
|
|
channel_chat_id TEXT NOT NULL,
|
|
chat_type TEXT NOT NULL
|
|
CHECK (chat_type IN ('p2p', 'group')),
|
|
-- Most-recent inbound trigger, so the decoupled outbound patcher can
|
|
-- thread its reply back into the originating topic. Nullable; a NULL
|
|
-- thread id keeps the chat-level send path.
|
|
last_message_id TEXT,
|
|
last_thread_id TEXT,
|
|
config JSONB NOT NULL DEFAULT '{}'::jsonb,
|
|
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
|
UNIQUE (installation_id, channel_chat_id),
|
|
UNIQUE (chat_session_id)
|
|
);
|
|
|
|
CREATE INDEX idx_channel_chat_session_binding_session
|
|
ON channel_chat_session_binding(chat_session_id);
|
|
|
|
INSERT INTO channel_chat_session_binding (
|
|
id, chat_session_id, installation_id, channel_type,
|
|
channel_chat_id, chat_type, last_message_id, last_thread_id, created_at
|
|
)
|
|
SELECT
|
|
id, chat_session_id, installation_id, 'feishu',
|
|
lark_chat_id, lark_chat_type, last_lark_message_id, last_lark_thread_id, created_at
|
|
FROM lark_chat_session_binding;
|
|
|
|
-- =====================
|
|
-- channel_inbound_message_dedup
|
|
-- =====================
|
|
-- Two-phase idempotency with owner fencing, unchanged in shape from
|
|
-- lark_inbound_message_dedup (keyed per installation + message). Transient
|
|
-- 24h cache; copied forward for completeness.
|
|
CREATE TABLE channel_inbound_message_dedup (
|
|
installation_id UUID NOT NULL,
|
|
message_id TEXT NOT NULL,
|
|
received_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
|
processed_at TIMESTAMPTZ,
|
|
claim_token UUID NOT NULL DEFAULT gen_random_uuid(),
|
|
PRIMARY KEY (installation_id, message_id)
|
|
);
|
|
|
|
CREATE INDEX idx_channel_inbound_dedup_received
|
|
ON channel_inbound_message_dedup(received_at);
|
|
|
|
INSERT INTO channel_inbound_message_dedup (
|
|
installation_id, message_id, received_at, processed_at, claim_token
|
|
)
|
|
SELECT installation_id, message_id, received_at, processed_at, claim_token
|
|
FROM lark_inbound_message_dedup;
|
|
|
|
-- =====================
|
|
-- channel_inbound_audit
|
|
-- =====================
|
|
-- Non-content drop audit. installation_id is nullable (the old ON DELETE
|
|
-- SET NULL is now just a nullable column the app may leave NULL).
|
|
CREATE TABLE channel_inbound_audit (
|
|
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
|
installation_id UUID,
|
|
channel_type TEXT NOT NULL,
|
|
channel_chat_id TEXT,
|
|
event_type TEXT NOT NULL,
|
|
channel_event_id TEXT,
|
|
channel_message_id TEXT,
|
|
drop_reason TEXT NOT NULL,
|
|
received_at TIMESTAMPTZ NOT NULL DEFAULT now()
|
|
);
|
|
|
|
CREATE INDEX idx_channel_inbound_audit_installation
|
|
ON channel_inbound_audit(installation_id, received_at DESC);
|
|
CREATE INDEX idx_channel_inbound_audit_reason
|
|
ON channel_inbound_audit(drop_reason, received_at DESC);
|
|
|
|
INSERT INTO channel_inbound_audit (
|
|
id, installation_id, channel_type, channel_chat_id, event_type,
|
|
channel_event_id, channel_message_id, drop_reason, received_at
|
|
)
|
|
SELECT
|
|
id, installation_id, 'feishu', lark_chat_id, event_type,
|
|
lark_event_id, lark_message_id, drop_reason, received_at
|
|
FROM lark_inbound_audit;
|
|
|
|
-- =====================
|
|
-- channel_outbound_card_message
|
|
-- =====================
|
|
CREATE TABLE channel_outbound_card_message (
|
|
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
|
chat_session_id UUID NOT NULL,
|
|
task_id UUID,
|
|
channel_type TEXT NOT NULL,
|
|
channel_chat_id TEXT NOT NULL,
|
|
channel_card_message_id TEXT NOT NULL,
|
|
status TEXT NOT NULL DEFAULT 'pending'
|
|
CHECK (status IN ('pending', 'streaming', 'final', 'error')),
|
|
last_patched_at TIMESTAMPTZ,
|
|
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
|
|
);
|
|
|
|
CREATE UNIQUE INDEX idx_channel_outbound_card_task
|
|
ON channel_outbound_card_message(task_id)
|
|
WHERE task_id IS NOT NULL;
|
|
CREATE INDEX idx_channel_outbound_card_session
|
|
ON channel_outbound_card_message(chat_session_id, created_at DESC);
|
|
|
|
INSERT INTO channel_outbound_card_message (
|
|
id, chat_session_id, task_id, channel_type, channel_chat_id,
|
|
channel_card_message_id, status, last_patched_at, created_at
|
|
)
|
|
SELECT
|
|
id, chat_session_id, task_id, 'feishu', lark_chat_id,
|
|
lark_card_message_id, status, last_patched_at, created_at
|
|
FROM lark_outbound_card_message;
|
|
|
|
-- =====================
|
|
-- channel_binding_token
|
|
-- =====================
|
|
CREATE TABLE channel_binding_token (
|
|
token_hash TEXT PRIMARY KEY,
|
|
workspace_id UUID NOT NULL,
|
|
installation_id UUID NOT NULL,
|
|
channel_type TEXT NOT NULL,
|
|
channel_user_id TEXT NOT NULL,
|
|
expires_at TIMESTAMPTZ NOT NULL,
|
|
consumed_at TIMESTAMPTZ,
|
|
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
|
-- Keep the product TTL cap in lockstep with channel.BindingTokenTTL
|
|
-- (15 minutes), same as the old lark_binding_token CHECK.
|
|
CONSTRAINT channel_binding_token_ttl_cap
|
|
CHECK (expires_at <= created_at + INTERVAL '15 minutes')
|
|
);
|
|
|
|
CREATE INDEX idx_channel_binding_token_installation
|
|
ON channel_binding_token(installation_id, expires_at);
|
|
|
|
INSERT INTO channel_binding_token (
|
|
token_hash, workspace_id, installation_id, channel_type,
|
|
channel_user_id, expires_at, consumed_at, created_at
|
|
)
|
|
SELECT
|
|
token_hash, workspace_id, installation_id, 'feishu',
|
|
lark_open_id, expires_at, consumed_at, created_at
|
|
FROM lark_binding_token;
|