mirror of
https://github.com/stablyai/orca.git
synced 2026-11-01 01:27:35 +00:00
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.
51 lines
1.6 KiB
TypeScript
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
|
|
})
|
|
})
|
|
}
|