Files
orca/cloud/apps/connector/src/queue-delivery.ts
T
Jinwoo Hong d4a5609d30 refactor(connector): provider-neutral connector core with Slack as its first adapter (#34088)
Moves routing, machine choice, held starts, the post queue, prompt cards and owner controls out of the Slack files into a provider-neutral connector core, with Slack as the first adapter behind one adapter type. Conversation records and delivery ids are keyed by source, so Slack and Discord ids cannot collide. Behaviour-neutral for Slack apart from a slack: delivery-id prefix and a 403 (not 404) from the reply route when Slack is off. Staging's connector database is reset after deploy.
2026-10-26 18:41:17 -04:00

51 lines
1.6 KiB
TypeScript

import type { CloudDatabase } from '@orca-cloud/cloud-database'
import { lockConnectorHost, type ConnectorHosts } from './connector-hosts.js'
import type { ConnectorInbox, DeliveryBody, EnqueueResult } from './connector-inbox.js'
import type { ConnectorThreads, NewThread } from './connector-threads.js'
export type QueueResult = EnqueueResult | 'unknown_host' | 'not_this_hosts_thread'
// queueDelivery inside a caller's transaction.
export type QueueInTransaction = (
transaction: CloudDatabase,
input: Parameters<typeof queueDelivery>[1]
) => ReturnType<typeof queueDelivery>
// One host-locked transaction finds the host, binds the thread and queues, so per-host seq is
// assigned in order.
export async function queueDelivery(
deps: {
database: CloudDatabase
inbox: ConnectorInbox
threads: ConnectorThreads
hosts: ConnectorHosts
},
input: {
thread: Omit<NewThread, 'accountId'>
deliveryId: string
messageId?: string
body: DeliveryBody
held?: boolean
}
): Promise<QueueResult> {
const { hostId, conversation } = input.thread
return await deps.database.transaction(async (transaction) => {
await lockConnectorHost(transaction, hostId)
const accountId = await deps.hosts.accountOf(transaction, hostId)
if (!accountId) {
return 'unknown_host'
}
if (!(await deps.threads.bind(transaction, { ...input.thread, accountId }))) {
return 'not_this_hosts_thread'
}
return await deps.inbox.enqueue(transaction, {
hostId,
conversation,
deliveryId: input.deliveryId,
messageId: input.messageId,
body: input.body,
held: input.held ?? false
})
})
}