| 18 | import { canUserUseRuntimeSync } from "./runtime-grants.ts"; |
| 19 | |
| 20 | export function enqueueNativeTaskSync(input: EnqueueTaskInput): QueuedTaskRecord | null { |
| 21 | const db = getDatabase(); |
| 22 | const workspaceId = input.workspaceId ?? DEFAULT_WORKSPACE_ID; |
| 23 | const binding = readEmployeeRuntimeBindingSync(input.assignee, workspaceId); |
| 24 | if (!binding) { |
| 25 | return null; |
| 26 | } |
| 27 | |
| 28 | const now = new Date().toISOString(); |
| 29 | const queueId = `queue-${randomLikeId()}`; |
| 30 | const payload = { |
| 31 | taskId: input.taskId, |
| 32 | assignee: input.assignee, |
| 33 | title: input.title, |
| 34 | channel: input.channel, |
| 35 | priority: input.priority, |
| 36 | ...(input.metadata ?? {}), |
| 37 | requester: |
| 38 | input.requestedByUserId || input.requestedByDisplayName |
| 39 | ? { |
| 40 | userId: input.requestedByUserId, |
| 41 | displayName: input.requestedByDisplayName, |
| 42 | } |
| 43 | : undefined, |
| 44 | }; |
| 45 | const routerSession = resolveRouterSessionForTaskSync({ |
| 46 | id: queueId, |
| 47 | workspaceId, |
| 48 | agentId: input.assignee, |
| 49 | triggerType: input.triggerType ?? "manual", |
| 50 | inputJson: JSON.stringify(payload), |
| 51 | issueId: input.taskId, |
| 52 | }); |
| 53 | |
| 54 | db.prepare( |
| 55 | `INSERT INTO agent_task_queue ( |
| 56 | id, |
| 57 | workspace_id, |
| 58 | agent_id, |
| 59 | runtime_id, |
| 60 | router_session_id, |
| 61 | issue_id, |
| 62 | trigger_type, |
| 63 | priority, |
| 64 | status, |
| 65 | input_json, |
| 66 | requested_by_user_id, |
| 67 | requested_by_display_name, |
| 68 | queued_at, |
| 69 | created_at, |
| 70 | updated_at |
| 71 | ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, 'queued', ?, ?, ?, ?, ?, ?)`, |
| 72 | ).run( |
| 73 | queueId, |
| 74 | workspaceId, |
| 75 | input.assignee, |
| 76 | binding.runtimeId, |
| 77 | routerSession.id, |