Files
orcs-code/src/utils/inProcessTeammateHelpers.ts
did:key:z6MkqDnb7Siv3Cwj7pGJq4T5EsUisECqR8KpnDLwcaZq5TPr d2542c9a62 asdf
Squash the current repository state back into one baseline commit while
preserving the README reframing and repository contents.

Constraint: User explicitly requested a single squashed commit with subject "asdf"
Confidence: high
Scope-risk: broad
Reversibility: clean
Directive: This commit intentionally rewrites published history; coordinate before future force-pushes
Tested: git status clean; local history rewritten to one commit; force-pushed main to origin and instructkr
Not-tested: Fresh clone verification after push
2026-03-31 03:34:03 -07:00

103 lines
2.9 KiB
TypeScript

/**
* In-Process Teammate Helpers
*
* Helper functions for in-process teammate integration.
* Provides utilities to:
* - Find task ID by agent name
* - Handle plan approval responses
* - Update awaitingPlanApproval state
* - Detect permission-related messages
*/
import type { AppState } from '../state/AppState.js'
import {
type InProcessTeammateTaskState,
isInProcessTeammateTask,
} from '../tasks/InProcessTeammateTask/types.js'
import { updateTaskState } from './task/framework.js'
import {
isPermissionResponse,
isSandboxPermissionResponse,
type PlanApprovalResponseMessage,
} from './teammateMailbox.js'
type SetAppState = (updater: (prev: AppState) => AppState) => void
/**
* Find the task ID for an in-process teammate by agent name.
*
* @param agentName - The agent name (e.g., "researcher")
* @param appState - Current AppState
* @returns Task ID if found, undefined otherwise
*/
export function findInProcessTeammateTaskId(
agentName: string,
appState: AppState,
): string | undefined {
for (const task of Object.values(appState.tasks)) {
if (
isInProcessTeammateTask(task) &&
task.identity.agentName === agentName
) {
return task.id
}
}
return undefined
}
/**
* Set awaitingPlanApproval state for an in-process teammate.
*
* @param taskId - Task ID of the in-process teammate
* @param setAppState - AppState setter
* @param awaiting - Whether teammate is awaiting plan approval
*/
export function setAwaitingPlanApproval(
taskId: string,
setAppState: SetAppState,
awaiting: boolean,
): void {
updateTaskState<InProcessTeammateTaskState>(taskId, setAppState, task => ({
...task,
awaitingPlanApproval: awaiting,
}))
}
/**
* Handle plan approval response for an in-process teammate.
* Called by the message callback when a plan_approval_response arrives.
*
* This resets awaitingPlanApproval to false. The permissionMode from the
* response is handled separately by the agent loop (Task #11).
*
* @param taskId - Task ID of the in-process teammate
* @param _response - The plan approval response message (for future use)
* @param setAppState - AppState setter
*/
export function handlePlanApprovalResponse(
taskId: string,
_response: PlanApprovalResponseMessage,
setAppState: SetAppState,
): void {
setAwaitingPlanApproval(taskId, setAppState, false)
}
// ============ Permission Delegation Helpers ============
/**
* Check if a message is a permission-related response.
* Used by in-process teammate message handlers to detect and process
* permission responses from the team leader.
*
* Handles both tool permissions and sandbox (network host) permissions.
*
* @param messageText - The raw message text to check
* @returns true if the message is a permission response
*/
export function isPermissionRelatedResponse(messageText: string): boolean {
return (
!!isPermissionResponse(messageText) ||
!!isSandboxPermissionResponse(messageText)
)
}