Orca is the place for working with a fleet of agents (e.g. Grok Bot, Claude Code). Run any coding agent with your own subscription. Available on desktop, mobile and remote runtime.
TypeScript adeagent-ideai-agentsautomationbotclaude-codecodexghosttygrok-bothermes-agentmobile-appmuseopenclawopencodeorchestrationpiruntimeterminalworktreesyc-backed
import type { AgentJournalRenderItem } from '../../../src/shared/agent-session-journal-types'
import {
agentSessionPromptQuestions,
type AgentSessionQuestionAnswer
} from '../../../src/shared/agent-session-question-answer'
import { chatApprovalFromJournal } from '../../../src/shared/native-chat-interactive-prompt'
import type { MobileChatPermission } from './native-chat/input/mobile-native-chat-permission'
import type { MobileChatQuestion } from './native-chat/input/mobile-native-chat-question'
export type StructuredApprovalItem = AgentJournalRenderItem & {
body: Extract<AgentJournalRenderItem['body'], { kind: 'approval' }>
}
export type StructuredQuestionItem = AgentJournalRenderItem & {
body: Extract<AgentJournalRenderItem['body'], { kind: 'question' }>
}
export type StructuredPromptResponseTarget = {
itemId: string
expectedRevision: number
optionId: string
}
export type StructuredQuestionResponseTarget = {
itemId: string
expectedRevision: number
answer: AgentSessionQuestionAnswer
}
type PromptTokenPayload =
| {
kind: 'approval'
itemId: string
revision: number
optionId: string
}
| {
kind: 'question-option'
itemId: string
revision: number
questionId: string
optionId: string
}
| {
kind: 'question-free-text'
itemId: string
revision: number
questionId: string
}
const STRUCTURED_PROMPT_TOKEN_PREFIX = 'structured-agent-prompt:'
function encodePromptToken(payload: PromptTokenPayload): string {
return `${STRUCTURED_PROMPT_TOKEN_PREFIX}${encodeURIComponent(JSON.stringify(payload))}`
}
function decodePromptToken(value: string): PromptTokenPayload | null {
if (!value.startsWith(STRUCTURED_PROMPT_TOKEN_PREFIX)) {
return null
}
try {
const decoded = JSON.parse(
decodeURIComponent(value.slice(STRUCTURED_PROMPT_TOKEN_PREFIX.length))
) as Record<string, unknown>
if (
typeof decoded.itemId !== 'string' ||
typeof decoded.revision !== 'number' ||
!Number.isFinite(decoded.revision)
) {
return null
}
if (decoded.kind === 'approval' && typeof decoded.optionId === 'string') {
return {
kind: decoded.kind,
itemId: decoded.itemId,
revision: decoded.revision,
optionId: decoded.optionId
}
}
if (
decoded.kind === 'question-option' &&
typeof decoded.questionId === 'string' &&
typeof decoded.optionId === 'string'
) {
return {
kind: decoded.kind,
itemId: decoded.itemId,
revision: decoded.revision,
questionId: decoded.questionId,
optionId: decoded.optionId
}
}
if (decoded.kind === 'question-free-text' && typeof decoded.questionId === 'string') {
return {
kind: decoded.kind,
itemId: decoded.itemId,
revision: decoded.revision,
questionId: decoded.questionId
}
}
} catch {
return null
}
return null
}
function decodeQuestionFreeTextAnswer(value: string): {
payload: Extract<PromptTokenPayload, { kind: 'question-free-text' }>
answer: string
} | null {
if (!value.startsWith(STRUCTURED_PROMPT_TOKEN_PREFIX)) {
return null
}
const separator = value.indexOf(':', STRUCTURED_PROMPT_TOKEN_PREFIX.length)
if (separator === -1) {
return null
}
const payload = decodePromptToken(value.slice(0, separator))
if (payload?.kind !== 'question-free-text') {
return null
}
return { payload, answer: decodeURIComponent(value.slice(separator + 1)) }
}
export function projectStructuredPermission(
prompt: StructuredApprovalItem | null
): MobileChatPermission | null {
if (prompt?.body.kind !== 'approval') {
return null
}
return {
...chatApprovalFromJournal(prompt.body),
prompt: { itemId: prompt.itemId, expectedRevision: prompt.revision },
options: prompt.body.options.map((option) => ({
label: option.label,
send: encodePromptToken({
kind: 'approval',
itemId: prompt.itemId,
revision: prompt.revision,
optionId: option.id
})
}))
}
}
export function projectStructuredQuestion(
prompt: StructuredQuestionItem | null
): MobileChatQuestion | null {
if (prompt?.body.kind !== 'question') {
return null
}
if (prompt.body.questions) {
return {
question: prompt.body.question,
prompt: { itemId: prompt.itemId, expectedRevision: prompt.revision },
options: [],
multiSelect: false,
optionTokens: [],
questions: prompt.body.questions
}
}
const [question] = agentSessionPromptQuestions(prompt.body)
if (!question) {
return null
}
const optionDescriptions = prompt.body.options.map((option) => option.description)
return {
question: prompt.body.question,
prompt: { itemId: prompt.itemId, expectedRevision: prompt.revision },
options: prompt.body.options.map((option) => option.label),
...(optionDescriptions.some(Boolean) ? { optionDescriptions } : {}),
multiSelect: false,
allowOther: Boolean(prompt.body.freeTextQuestionId),
...(prompt.body.freeTextInput ? { freeTextInput: prompt.body.freeTextInput } : {}),
optionTokens: prompt.body.options.map((option) =>
encodePromptToken({
kind: 'question-option',
itemId: prompt.itemId,
revision: prompt.revision,
questionId: question.id,
optionId: option.id
})
),
...(prompt.body.freeTextQuestionId
? {
freeTextToken: encodePromptToken({
kind: 'question-free-text',
itemId: prompt.itemId,
revision: prompt.revision,
questionId: prompt.body.freeTextQuestionId
})
}
: {})
}
}
export function structuredApprovalResponseTarget(
response: string,
currentPrompt: StructuredApprovalItem | null
): StructuredPromptResponseTarget | null {
const token = decodePromptToken(response)
if (token?.kind === 'approval') {
return {
itemId: token.itemId,
expectedRevision: token.revision,
optionId: token.optionId
}
}
if (token) {
return null
}
const option = currentPrompt?.body.options.find(
(candidate) => candidate.id === response || candidate.label === response
)
return currentPrompt && option
? {
itemId: currentPrompt.itemId,
expectedRevision: currentPrompt.revision,
optionId: option.id
}
: null
}
export function structuredQuestionResponseTarget(
response: string,
currentPrompt: StructuredQuestionItem | null
): StructuredQuestionResponseTarget | null {
const token = decodePromptToken(response)
if (token?.kind === 'question-option') {
return {
itemId: token.itemId,
expectedRevision: token.revision,
answer: { questionId: token.questionId, optionIds: [token.optionId] }
}
}
if (token) {
return null
}
const freeText = decodeQuestionFreeTextAnswer(response)
if (freeText) {
const allowEmpty =
currentPrompt?.itemId === freeText.payload.itemId &&
currentPrompt.revision === freeText.payload.revision &&
currentPrompt.body.freeTextQuestionId === freeText.payload.questionId &&
currentPrompt.body.freeTextInput?.allowEmpty === true
const answer = allowEmpty ? freeText.answer : freeText.answer.trim()
return answer.length > 0 || allowEmpty
? {
itemId: freeText.payload.itemId,
expectedRevision: freeText.payload.revision,
answer: { questionId: freeText.payload.questionId, optionIds: [], other: answer }
}
: null
}
if (!currentPrompt) {
return null
}
const [question] = agentSessionPromptQuestions(currentPrompt.body)
const trimmed = response.trim()
const allowEmpty = currentPrompt.body.freeTextInput?.allowEmpty === true
const option = currentPrompt.body.options.find(
(candidate) => candidate.id === response || candidate.label === trimmed
)
if (question && option) {
return {
itemId: currentPrompt.itemId,
expectedRevision: currentPrompt.revision,
answer: { questionId: question.id, optionIds: [option.id] }
}
}
return question && currentPrompt.body.freeTextQuestionId && (trimmed || allowEmpty)
? {
itemId: currentPrompt.itemId,
expectedRevision: currentPrompt.revision,
answer: { questionId: question.id, optionIds: [], other: allowEmpty ? response : trimmed }
}
: null
}