(runtime: AgentRuntimeRecord, queuedTask: QueuedTaskRecord)
| 912 | } |
| 913 | |
| 914 | async function executeQueuedTask(runtime: AgentRuntimeRecord, queuedTask: QueuedTaskRecord): Promise<void> { |
| 915 | const task = startQueuedTaskSync(queuedTask.id); |
| 916 | writeWorkspaceStateSync(markChannelDocumentRunStepRunningSync(task.id, task.workspaceId), task.workspaceId); |
| 917 | const payload = parseTaskPayload(task); |
| 918 | |
| 919 | const budgetCheck = checkAllBudgetsForAgentSync( |
| 920 | payload.assignee ?? task.agentId, |
| 921 | payload.channelName ?? payload.channel, |
| 922 | task.workspaceId, |
| 923 | ); |
| 924 | if (budgetCheck.status === "exceeded" && budgetCheck.action === "pause") { |
| 925 | const pct = Math.round(budgetCheck.percentUsed * 100); |
| 926 | const msg = `Budget exceeded (${pct}% of $${budgetCheck.budget.limitUsd.toFixed(2)}). Task paused.`; |
| 927 | appendTaskMessageSync({ taskId: task.id, type: "status", content: msg }); |
| 928 | failQueuedTaskSync({ taskId: task.id, errorText: msg }); |
| 929 | if (payload.taskId) updateTaskStatusSync(payload.taskId, "blocked", task.workspaceId); |
| 930 | return; |
| 931 | } |
| 932 | const workspaceState = readWorkspaceStateSync(task.workspaceId); |
| 933 | const agentProfile = workspaceState.activeEmployees.find((employee: ActiveEmployee) => |
| 934 | sameValue(employee.name, payload.assignee ?? task.agentId), |
| 935 | ); |
| 936 | const compatibleDirectChannelName = |
| 937 | payload.contactId && !payload.channelName |
| 938 | ? resolveCompatibleDirectChannelRecord(workspaceState, payload.contactId)?.name |
| 939 | : undefined; |
| 940 | const effectiveChannelName = payload.channelName ?? compatibleDirectChannelName; |
| 941 | const effectivePayload = |
| 942 | effectiveChannelName && payload.contactId && !payload.channelName |
| 943 | ? { |
| 944 | ...payload, |
| 945 | channelName: effectiveChannelName, |
| 946 | channelMessage: payload.channelMessage, |
| 947 | } |
| 948 | : payload; |
| 949 | const contactContext = |
| 950 | payload.contactId ? buildContactAgentContext(workspaceState, payload.contactId) : undefined; |
| 951 | const channelThreadId = |
| 952 | resolveConversationThreadId({ |
| 953 | triggerType: task.triggerType, |
| 954 | payload: { |
| 955 | channel: payload.channel, |
| 956 | channelName: effectiveChannelName, |
| 957 | contactId: payload.contactId, |
| 958 | }, |
| 959 | }); |
| 960 | appendTaskMessageSync({ |
| 961 | taskId: task.id, |
| 962 | type: "status", |
| 963 | content: `Task started on ${runtime.name}.`, |
| 964 | }); |
| 965 | |
| 966 | if (payload.taskId) { |
| 967 | updateTaskStatusSync(payload.taskId, "in_progress", task.workspaceId); |
| 968 | } |
| 969 | |
| 970 | const workDir = resolveWorkspaceTaskWorkDir({ |
| 971 | workspaceId: task.workspaceId, |
no test coverage detected