mirror of
https://github.com/multica-ai/multica.git
synced 2026-07-06 14:00:09 +02:00
* feat(slack): Socket Mode channel.Channel adapter (MUL-3516) First slice of the Slack adapter: implements channel.Channel (Type/Connect/Disconnect/Send/Capabilities) over Slack Socket Mode, normalizes inbound events to channel.InboundMessage (DM, channel @mention, thread reply; bot-loop + edit/delete guards), decodes the per-installation config/secret blob, and registers the Factory under TypeSlack. No engine, core, or channel_* schema change. Unit-tested (translation, capabilities, config decode, chunking, Send via httptest). Resolvers + engine wiring + Block Kit binding replier follow. Co-authored-by: multica-agent <github@multica.ai> * fix(slack): address adapter review (MUL-3516) - Propagate InboundHandler errors through dispatchEventsAPI/handleSocketEvent to Connect so an infra failure tears down the connection for Supervisor reconnect/backoff instead of being silently swallowed (ACK still happens first). - Capabilities: declare only CapText | CapThreadReply; drop CapRichCard/CapAttachment/CapMessageEdit until those Send paths are wired. - slackChatType: map mpim (multi-party DM) to group, not p2p, so the 'must address bot' filter applies; only 1:1 im is p2p. - Document the group-addressing decision: explicit @bot mention required in groups; mention-free thread continuation deferred to the session-aware layer. - Tests: handler-error propagation, slackChatType table, mpim-requires-mention, capabilities negative assertions. Co-authored-by: multica-agent <github@multica.ai> * refactor(channel): shared channel-agnostic ChatSession service (MUL-3516) Extract the session/append//issue machinery — currently locked inside the Feishu-pinned lark.chatSessionService — into a shared engine.ChatSession parameterized by channel_type + session titles, so every IM adapter reuses it instead of re-implementing it. Logic is verbatim (find-or-create session+binding with unique-violation race re-read; append+touch+reply-target+in-tx dedup Mark; /issue parse with bare-command previous-message fallback) but channel-neutral: command-parse source is supplied by the adapter (enrichment is platform-specific). Backed by a narrow SessionQueries interface so it is unit-tested with an in-memory fake (no DB). /issue parser moved to engine.ParseIssueCommand. Next: migrate Feishu onto it and wire Slack's ResolverSet, removing the lark duplicate. Co-authored-by: multica-agent <github@multica.ai> * fix(channel): decouple session binding key from outbound target (MUL-3516) Addresses Elon's round-2 review. engine.ChatSession.EnsureSession previously keyed the binding on a raw chat id (EnsureSessionInput.ChatID), so a resolver wiring Slack straight through would collapse every @bot thread in one channel into a single chat_session and overwrite last_thread_id. Make the API un-misusable: - EnsureSessionInput.ChatID -> BindingKey: the explicit session-isolation key (Feishu: chat id; Slack DM: channel id; Slack channel: channel id + thread root), documented so a raw threaded-platform chat id is never passed straight through. - Add EnsureSessionInput.BindingConfig (opaque) persisted on the binding's config column, so the real outbound channel/thread is preserved when BindingKey is composite — outbound routing stays separate from the isolation key. - channel.sql CreateChannelChatSessionBinding now writes config (additive, uses the existing NOT NULL column; lark caller passes '{}', no schema change, no Feishu regression). - Tests: TestEnsureSession_ThreadRootIsolation (two thread roots in one channel -> two sessions; same root reuses) and TestEnsureSession_StoresBindingConfig. No production wiring change yet (per review, the not-yet-wired shared service is an accepted preparatory state); this makes the API correct before Feishu/Slack are migrated onto it. Co-authored-by: multica-agent <github@multica.ai> * feat(slack): Slack ResolverSet with thread-root session isolation (MUL-3516) Wires Slack into the channel-agnostic engine.Router via a ResolverSet built on the generic channel_* queries (installation route by team_id, identity + workspace-membership recheck, two-phase dedup, audit) plus the shared engine.ChatSession. No new query, no schema change. slackSessionRouting is the per-message isolation rule (Elon round-2 / Niko round-3): a DM is one session per channel; a channel/group message is isolated by thread root (key = channel:threadRoot, root = inbound thread_ts or the message ts for a top-level @mention), so two @bot threads in one channel are two sessions. The real channel id rides in BindingConfig for outbound; the reply thread is returned separately. Tests cover DM/channel/thread routing, config, and that distinct thread roots isolate while a same-thread follow-up reuses its key. Not yet wired into router.go (still a preparatory commit, per review); Feishu migration onto the shared service, router/config wiring, and the Slack outbound path follow. Co-authored-by: multica-agent <github@multica.ai> * feat(slack): Markdown->mrkdwn outbound formatting (MUL-3516) Slack renders mrkdwn, not Markdown, so an unconverted agent reply shows literal ** , ## and [text](url). Add formatMrkdwn — a faithful Go port of Hermes Agent's slack format_message (MIT) — and apply it in slackChannel.Send before chunking/posting. Protects fenced+inline code, converted links, and existing Slack entities behind placeholders; converts headers/bold/italic/strike/links; escapes control chars. Unit tests cover each construct plus fenced-code protection and a link nested in bold. Co-authored-by: multica-agent <github@multica.ai> * docs(slack): preserve Hermes MIT notice for ported mrkdwn converter (MUL-3516) Addresses Niko's review. formatMrkdwn is a substantial port of Hermes Agent's slack format_message; MIT requires preserving the copyright + permission notice. Add the full Hermes MIT copyright/permission notice + source URL as a header on mrkdwn.go (no repo-level third-party notice file exists, and the header cannot get separated from the ported code). Also add the suggested Send-layer regression test (TestSend_AppliesMrkdwn) that pins the wiring: slackChannel.Send converts Markdown to mrkdwn before posting. Co-authored-by: multica-agent <github@multica.ai> * refactor(lark): migrate Feishu onto shared engine.ChatSession, drop duplicate (MUL-3516) Completes 'every IM reuses one shared session service' and removes the dual-path the reviewers flagged as temporary. Feishu's ResolverSet now drives the channel-agnostic engine.ChatSession (channel_type=feishu, Lark session titles preserved) instead of the Feishu-specific lark.chatSessionService, which is deleted. Behavior is unchanged: engine.ChatSession is the verbatim port of the old logic and is unit-tested; the new Feishu binder param-mapping (BindingKey=chat id, CommandText=un-enriched CommandBody from Raw) is covered by feishu_resolvers_test.go. - Delete chat_service.go (chatSessionService + helpers) and issue_command.go/_test.go (parser now engine.ParseIssueCommand). Relocate the shared TxStarter interface to tx.go (still used by binding-token + registration services). - chat.go keeps only the AuditLogger seam; remove the now-dead ChatSessionService / EnsureChatSessionParams / AppendUserMessageParams / AppendResult / IssueCommand types. - router.go constructs engine.NewChatSession for Feishu; inbound_enricher_test + doc.go updated. make-test parity: go build ./..., go vet, gofmt, and go test ./internal/integrations/{lark,channel/...,slack} all pass (full Feishu suite green). Co-authored-by: multica-agent <github@multica.ai> * feat(slack): wire Slack adapter + ResolverSet + outbound into router (MUL-3516) Activates the full Slack pipeline, gated by MULTICA_SLACK_SECRET_KEY (the bot/app-token decryption key). When unset the block is skipped, so existing deployments are unaffected and Feishu is untouched. - router.go registers slack.RegisterSlack (Socket Mode connect/send Factory) + channelRouter.Register(TypeSlack, NewSlackResolverSet) (inbound pipeline) + slack.NewOutbound(...).Register(bus) (outbound). - New slack/outbound.go: an EventChatDone subscriber mirroring the Feishu Patcher. It finds the Slack chat binding for the finished session, recovers the real channel from the binding config (the channel_chat_id may be a composite thread-isolation key) + the reply thread from last_thread_id, and posts via slackChannel.Send (reusing formatMrkdwn / chunking / threading). Sessions with no Slack binding are ignored, so it coexists with the Feishu Patcher on the shared bus. - Tests: posts to the bound channel/thread with the real channel id; ignores non-Slack sessions, empty completions, revoked installations, and non-chat events. Slack now shares engine.ChatSession, channel_* tables, IssueService and TaskService with Feishu. Remaining: config-driven installation provisioning (an operator currently creates the channel_type='slack' row; the config block shape — which workspace/agent — is a product decision) and a live end-to-end smoke. go build ./..., go vet, gofmt, and go test ./internal/integrations/{slack,channel/...,lark} all pass. Co-authored-by: multica-agent <github@multica.ai> --------- Co-authored-by: J <j@multica.ai> Co-authored-by: multica-agent <github@multica.ai>
378 lines
16 KiB
SQL
378 lines
16 KiB
SQL
-- Platform-agnostic inbound channel queries (MUL-3515). These operate on
|
|
-- the channel_* tables created in migration 124. Each installation carries
|
|
-- a `channel_type` discriminator and a JSONB `config` blob for
|
|
-- platform-specific identifiers/credentials; the cross-platform columns
|
|
-- stay flat. The Go layer owns building/parsing config — these queries
|
|
-- treat it as opaque JSON except for the routing index on config->>'app_id'.
|
|
--
|
|
-- No foreign keys exist on these tables (MUL-3515 §4): the integrity the
|
|
-- old composite FKs enforced (binding workspace matches installation;
|
|
-- binding dies with membership / chat_session) is maintained in the
|
|
-- application layer via the membership check in the inbound identity step
|
|
-- and the *DeleteChannel*BindingsBy* cleanup queries below.
|
|
|
|
-- =====================
|
|
-- channel_installation
|
|
-- =====================
|
|
|
|
-- name: UpsertChannelInstallation :one
|
|
-- Install / re-install path. `config` is the opaque per-channel JSONB the
|
|
-- Go layer assembles (for feishu: app_id, app_secret_encrypted, tenant_key,
|
|
-- bot_open_id, bot_union_id, region). Re-installing the same agent on the
|
|
-- same channel_type replaces the whole config and forces status back to
|
|
-- 'active'. The conflict key is (workspace_id, agent_id, channel_type) so an
|
|
-- agent may hold one installation per channel_type (feishu + slack + ...)
|
|
-- without one install clobbering another. The WS lease is intentionally NOT
|
|
-- reset here — the inbound hub owns lease lifecycle.
|
|
INSERT INTO channel_installation (
|
|
workspace_id, agent_id, channel_type, config, installer_user_id
|
|
) VALUES (
|
|
$1, $2, $3, $4, $5
|
|
)
|
|
ON CONFLICT (workspace_id, agent_id, channel_type) DO UPDATE SET
|
|
channel_type = EXCLUDED.channel_type,
|
|
config = EXCLUDED.config,
|
|
installer_user_id = EXCLUDED.installer_user_id,
|
|
status = 'active',
|
|
installed_at = now(),
|
|
updated_at = now()
|
|
RETURNING *;
|
|
|
|
-- name: GetChannelInstallation :one
|
|
-- Scoped by channel_type: a per-channel caller (e.g. the Feishu store)
|
|
-- must never resolve another channel's installation by guessing its UUID.
|
|
SELECT * FROM channel_installation
|
|
WHERE id = sqlc.arg('id') AND channel_type = sqlc.arg('channel_type');
|
|
|
|
-- name: GetChannelInstallationInWorkspace :one
|
|
SELECT * FROM channel_installation
|
|
WHERE id = sqlc.arg('id')
|
|
AND workspace_id = sqlc.arg('workspace_id')
|
|
AND channel_type = sqlc.arg('channel_type');
|
|
|
|
-- name: GetChannelInstallationByAppID :one
|
|
-- Inbound routing. The platform event carries only the channel's app
|
|
-- identifier (Feishu app_id); the dispatcher's installation resolver routes
|
|
-- on (channel_type, config->>'app_id'). Backed by the functional unique
|
|
-- index idx_channel_installation_type_appid.
|
|
--
|
|
-- Both params are named + explicitly typed: `config ->> 'app_id'` makes sqlc
|
|
-- attribute a bare `$2` to the JSONB `config` column (it would emit
|
|
-- `Config []byte`), so we pin the app_id arg to ::text to get AppID string.
|
|
SELECT * FROM channel_installation
|
|
WHERE channel_type = sqlc.arg('channel_type')
|
|
AND config ->> 'app_id' = sqlc.arg('app_id')::text;
|
|
|
|
-- name: ListChannelInstallationsByWorkspace :many
|
|
-- Scoped by channel_type so a per-channel management surface (e.g. the Lark
|
|
-- installation list) only ever sees its own platform's installations.
|
|
SELECT * FROM channel_installation
|
|
WHERE workspace_id = sqlc.arg('workspace_id')
|
|
AND channel_type = sqlc.arg('channel_type')
|
|
ORDER BY created_at ASC;
|
|
|
|
-- name: ListActiveChannelInstallations :many
|
|
-- Boot path for a per-channel-type inbound hub: every active installation of
|
|
-- the given channel_type, so a hub claims leases and opens connections only
|
|
-- for its own platform and never supervises another channel's installation.
|
|
--
|
|
-- The JOINs require the owning workspace and agent rows to still exist.
|
|
-- channel_installation has no FK (MUL-3515 §4), so unlike the old
|
|
-- lark_installation (which cascaded away on workspace/agent deletion) an
|
|
-- installation can be orphaned when its workspace is deleted or its agent is
|
|
-- hard-deleted (e.g. runtime teardown). Without this guard the hub would keep
|
|
-- opening a WebSocket for a bot whose workspace/agent is gone. The JOIN matches
|
|
-- the old ON DELETE CASCADE semantics: it filters on row existence, not agent
|
|
-- archival, so an archived-but-present agent's installation is still listed.
|
|
SELECT ci.* FROM channel_installation ci
|
|
JOIN workspace w ON w.id = ci.workspace_id
|
|
JOIN agent a ON a.id = ci.agent_id
|
|
WHERE ci.status = 'active'
|
|
AND ci.channel_type = sqlc.arg('channel_type')
|
|
ORDER BY ci.created_at ASC;
|
|
|
|
-- name: ListAllActiveChannelInstallations :many
|
|
-- Boot path for the channel-agnostic engine Supervisor (MUL-3620): every
|
|
-- active installation across ALL channel types, so one Supervisor drives every
|
|
-- platform's connections rather than a per-platform hub. This is the de-
|
|
-- hardcoded counterpart of ListActiveChannelInstallations — the Supervisor
|
|
-- routes each row to its registered channel.Factory by channel_type, so it
|
|
-- never needs to know which platforms exist. Same orphan guard as the per-type
|
|
-- query: the workspace + agent JOINs drop installations whose owning rows are
|
|
-- gone (channel_installation has no FK, MUL-3515 §4), matching the old ON
|
|
-- DELETE CASCADE semantics (row existence, not agent archival).
|
|
SELECT ci.* FROM channel_installation ci
|
|
JOIN workspace w ON w.id = ci.workspace_id
|
|
JOIN agent a ON a.id = ci.agent_id
|
|
WHERE ci.status = 'active'
|
|
ORDER BY ci.created_at ASC;
|
|
|
|
-- name: SetChannelInstallationStatus :exec
|
|
UPDATE channel_installation
|
|
SET status = $2, updated_at = now()
|
|
WHERE id = $1;
|
|
|
|
-- name: SetChannelInstallationConfig :exec
|
|
-- Replaces the whole config blob for one installation. Used by the
|
|
-- operator backfills (e.g. setting a freshly-fetched bot_union_id) that
|
|
-- read-modify-write the JSON in Go and persist it back atomically by id.
|
|
UPDATE channel_installation
|
|
SET config = $2, updated_at = now()
|
|
WHERE id = $1;
|
|
|
|
-- name: BackfillChannelInstallationRegionToFeishuLark :execrows
|
|
-- Operator repair, feishu-only: flip every feishu installation still
|
|
-- carrying region='feishu' to 'lark'. Called only on deployments whose
|
|
-- legacy global base-URL override pointed at Lark international. Idempotent.
|
|
UPDATE channel_installation
|
|
SET config = jsonb_set(config, '{region}', '"lark"'),
|
|
updated_at = now()
|
|
WHERE channel_type = 'feishu'
|
|
AND config ->> 'region' = 'feishu';
|
|
|
|
-- name: AcquireChannelWSLease :one
|
|
-- Atomically claims the WebSocket lease. CAS predicate accepts when no
|
|
-- holder exists, the holder expired, or the holder is us (renewal).
|
|
UPDATE channel_installation
|
|
SET ws_lease_token = sqlc.arg('new_token'),
|
|
ws_lease_expires_at = sqlc.arg('new_expires_at'),
|
|
updated_at = now()
|
|
WHERE id = sqlc.arg('id')
|
|
AND status = 'active'
|
|
AND (
|
|
ws_lease_token IS NULL
|
|
OR ws_lease_expires_at < now()
|
|
OR ws_lease_token = sqlc.arg('new_token')
|
|
)
|
|
RETURNING *;
|
|
|
|
-- name: ReleaseChannelWSLease :exec
|
|
-- Drops the lease iff we are still the holder.
|
|
UPDATE channel_installation
|
|
SET ws_lease_token = NULL,
|
|
ws_lease_expires_at = NULL,
|
|
updated_at = now()
|
|
WHERE id = $1
|
|
AND ws_lease_token = sqlc.arg('current_token');
|
|
|
|
-- =====================
|
|
-- channel_user_binding
|
|
-- =====================
|
|
|
|
-- name: CreateChannelUserBinding :one
|
|
-- Records that a platform user id (per-installation; Feishu open_id) maps
|
|
-- to a Multica user. The old composite member-FK is gone, so this no
|
|
-- longer fails when the redeemer is not a workspace member — the caller
|
|
-- (BindingTokenService.RedeemAndBind) validates membership explicitly
|
|
-- before calling. ON CONFLICT DO UPDATE is still gated on multica_user_id
|
|
-- matching, so a second redeemer cannot steal an already-bound user id;
|
|
-- a cross-user conflict updates zero rows and the caller maps that to
|
|
-- ErrBindingAlreadyAssigned. config carries secondary identity (union_id).
|
|
INSERT INTO channel_user_binding (
|
|
workspace_id, multica_user_id, installation_id,
|
|
channel_type, channel_user_id, config
|
|
) VALUES (
|
|
$1, $2, $3, $4, $5, $6
|
|
)
|
|
ON CONFLICT (installation_id, channel_user_id) DO UPDATE SET
|
|
-- jsonb_strip_nulls(EXCLUDED.config) preserves the old lark semantics
|
|
-- `union_id = COALESCE(EXCLUDED.union_id, lark_user_binding.union_id)`:
|
|
-- a re-bind that carries `{"union_id": null}` (or omits the key) must NOT
|
|
-- erase a union_id we already captured. Only non-null incoming keys win.
|
|
config = channel_user_binding.config || jsonb_strip_nulls(EXCLUDED.config),
|
|
bound_at = now()
|
|
WHERE channel_user_binding.multica_user_id = EXCLUDED.multica_user_id
|
|
RETURNING *;
|
|
|
|
-- name: GetChannelUserBindingByUserID :one
|
|
-- The inbound identity lookup: does this platform user id map to a Multica
|
|
-- user for this installation? With the member-FK removed, a row's
|
|
-- existence no longer proves current workspace membership — the dispatcher
|
|
-- re-checks membership after this lookup.
|
|
SELECT * FROM channel_user_binding
|
|
WHERE installation_id = $1 AND channel_user_id = $2;
|
|
|
|
-- name: DeleteChannelUserBindingsByWorkspaceMember :exec
|
|
-- Application-layer integrity (replaces the old member-FK ON DELETE
|
|
-- CASCADE): prune every binding for a user who has been removed from a
|
|
-- workspace, across all installations in that workspace.
|
|
DELETE FROM channel_user_binding
|
|
WHERE workspace_id = $1 AND multica_user_id = $2;
|
|
|
|
-- =====================
|
|
-- channel_chat_session_binding
|
|
-- =====================
|
|
|
|
-- name: CreateChannelChatSessionBinding :one
|
|
-- channel_chat_id is the session-isolation key (one chat_session per
|
|
-- (installation_id, channel_chat_id)): Feishu passes the chat id; Slack passes
|
|
-- a stable key that, for channels, includes the thread root so each @bot thread
|
|
-- is its own session. config carries any platform-specific outbound routing the
|
|
-- key alone does not (e.g. Slack's real channel_id when the key is composite);
|
|
-- it is opaque to the shared session service.
|
|
INSERT INTO channel_chat_session_binding (
|
|
chat_session_id, installation_id, channel_type, channel_chat_id, chat_type, config
|
|
) VALUES (
|
|
$1, $2, $3, $4, $5, $6
|
|
)
|
|
RETURNING *;
|
|
|
|
-- name: GetChannelChatSessionBinding :one
|
|
-- Lookup-by-channel-chat: the inbound dispatcher finds the existing
|
|
-- chat_session before deciding whether to create one.
|
|
SELECT * FROM channel_chat_session_binding
|
|
WHERE installation_id = $1 AND channel_chat_id = $2;
|
|
|
|
-- name: GetChannelChatSessionBindingBySession :one
|
|
-- Reverse lookup for the outbound patcher: given a chat_session_id, find
|
|
-- its channel binding to know which (installation, chat_id) to send to.
|
|
-- Scoped by channel_type so a future non-Feishu binding on the same
|
|
-- chat_session is never treated as a Feishu reply target.
|
|
SELECT * FROM channel_chat_session_binding
|
|
WHERE chat_session_id = sqlc.arg('chat_session_id')
|
|
AND channel_type = sqlc.arg('channel_type');
|
|
|
|
-- name: UpdateChannelChatSessionBindingReplyTarget :exec
|
|
-- Records the most recent inbound trigger message + thread so the decoupled
|
|
-- outbound patcher can thread its reply back into the originating topic.
|
|
UPDATE channel_chat_session_binding
|
|
SET last_message_id = sqlc.narg('last_message_id'),
|
|
last_thread_id = sqlc.narg('last_thread_id')
|
|
WHERE chat_session_id = $1;
|
|
|
|
-- name: DeleteChannelChatSessionBindingBySession :exec
|
|
-- Application-layer integrity (replaces the old chat_session-FK ON DELETE
|
|
-- CASCADE): drop the binding when its chat_session is deleted.
|
|
DELETE FROM channel_chat_session_binding
|
|
WHERE chat_session_id = $1;
|
|
|
|
-- =====================
|
|
-- channel_inbound_message_dedup
|
|
-- =====================
|
|
|
|
-- name: ClaimChannelInboundDedup :one
|
|
-- Two-phase idempotency gate with owner fencing. Returns the row when a
|
|
-- claim is acquired (fresh insert, or stale-reclaim of an in-flight claim
|
|
-- older than 60s); returns no rows when terminal (processed) or actively
|
|
-- in-flight. Every claim mints a fresh claim_token; Mark/Release are
|
|
-- fenced on it. See the table comment in migration 124 / the lark
|
|
-- predecessor for the full invariant set.
|
|
INSERT INTO channel_inbound_message_dedup (installation_id, message_id, claim_token)
|
|
VALUES ($1, $2, gen_random_uuid())
|
|
ON CONFLICT (installation_id, message_id) DO UPDATE
|
|
SET received_at = now(),
|
|
claim_token = gen_random_uuid()
|
|
WHERE channel_inbound_message_dedup.processed_at IS NULL
|
|
AND channel_inbound_message_dedup.received_at < now() - INTERVAL '60 seconds'
|
|
RETURNING installation_id, message_id, received_at, processed_at, claim_token;
|
|
|
|
-- name: MarkChannelInboundDedupProcessed :execrows
|
|
-- Locks a claim in as permanently processed after a durable outcome.
|
|
-- Invoked inside the chat_message tx (via qtx) on the ingest path so the
|
|
-- durable write and the Mark commit atomically. Token mismatch returns
|
|
-- zero rows (a reclaim happened); the caller rolls back its in-tx write.
|
|
UPDATE channel_inbound_message_dedup
|
|
SET processed_at = now()
|
|
WHERE installation_id = $1
|
|
AND message_id = $2
|
|
AND claim_token = $3
|
|
AND processed_at IS NULL;
|
|
|
|
-- name: ReleaseChannelInboundDedup :execrows
|
|
-- Releases an in-flight claim when an infra error occurred before any
|
|
-- durable side effect, so a retry can re-acquire immediately. Fenced on
|
|
-- processed_at IS NULL and claim_token.
|
|
DELETE FROM channel_inbound_message_dedup
|
|
WHERE installation_id = $1
|
|
AND message_id = $2
|
|
AND claim_token = $3
|
|
AND processed_at IS NULL;
|
|
|
|
-- name: PurgeChannelInboundDedup :exec
|
|
-- Vacuum job: remove dedup rows older than the supplied cutoff (e.g. 24h).
|
|
DELETE FROM channel_inbound_message_dedup
|
|
WHERE received_at < $1;
|
|
|
|
-- =====================
|
|
-- channel_inbound_audit
|
|
-- =====================
|
|
|
|
-- name: RecordChannelInboundDrop :exec
|
|
-- The only write path for dropped events. Deliberately carries no body
|
|
-- column — only routing / identity / drop_reason / timestamp.
|
|
INSERT INTO channel_inbound_audit (
|
|
installation_id, channel_type, channel_chat_id, event_type,
|
|
channel_event_id, channel_message_id, drop_reason
|
|
) VALUES (
|
|
sqlc.narg('installation_id'),
|
|
$1,
|
|
sqlc.narg('channel_chat_id'),
|
|
$2,
|
|
sqlc.narg('channel_event_id'),
|
|
sqlc.narg('channel_message_id'),
|
|
$3
|
|
);
|
|
|
|
-- name: ListChannelInboundAuditByInstallation :many
|
|
SELECT * FROM channel_inbound_audit
|
|
WHERE installation_id = $1
|
|
ORDER BY received_at DESC
|
|
LIMIT $2 OFFSET $3;
|
|
|
|
-- =====================
|
|
-- channel_outbound_card_message
|
|
-- =====================
|
|
|
|
-- name: CreateChannelOutboundCardMessage :one
|
|
INSERT INTO channel_outbound_card_message (
|
|
chat_session_id, task_id, channel_type, channel_chat_id,
|
|
channel_card_message_id, status
|
|
) VALUES (
|
|
$1, sqlc.narg('task_id'), $2, $3, $4, $5
|
|
)
|
|
RETURNING *;
|
|
|
|
-- name: GetChannelOutboundCardByTask :one
|
|
-- The partial unique index on (task_id) WHERE task_id IS NOT NULL
|
|
-- guarantees at most one row. Scoped by channel_type so a future non-Feishu
|
|
-- card for the same task is not patched as a Feishu card.
|
|
SELECT * FROM channel_outbound_card_message
|
|
WHERE task_id = sqlc.arg('task_id')
|
|
AND channel_type = sqlc.arg('channel_type');
|
|
|
|
-- name: UpdateChannelOutboundCardStatus :exec
|
|
UPDATE channel_outbound_card_message
|
|
SET status = $2,
|
|
last_patched_at = now()
|
|
WHERE id = $1;
|
|
|
|
-- =====================
|
|
-- channel_binding_token
|
|
-- =====================
|
|
|
|
-- name: CreateChannelBindingToken :one
|
|
-- Mints a single-use binding token for an unbound platform user. TTL cap
|
|
-- (15 min) enforced by the table CHECK in lockstep with
|
|
-- channel.BindingTokenTTL. The HASH is stored, never the raw token.
|
|
INSERT INTO channel_binding_token (
|
|
token_hash, workspace_id, installation_id, channel_type,
|
|
channel_user_id, expires_at
|
|
) VALUES (
|
|
$1, $2, $3, $4, $5, $6
|
|
)
|
|
RETURNING *;
|
|
|
|
-- name: ConsumeChannelBindingToken :one
|
|
-- Atomic redemption: returns the row only if the hash exists, is
|
|
-- unconsumed, and unexpired. Two simultaneous redemptions cannot both win.
|
|
UPDATE channel_binding_token
|
|
SET consumed_at = now()
|
|
WHERE token_hash = $1
|
|
AND consumed_at IS NULL
|
|
AND expires_at > now()
|
|
RETURNING *;
|
|
|
|
-- name: PurgeExpiredChannelBindingTokens :exec
|
|
DELETE FROM channel_binding_token
|
|
WHERE expires_at < $1;
|