Skip to content

armgit / orca

Public Imported from GitHub
0

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.

1.6 KB
Raw Blame
import type { AgentJournalRenderItem } from './agent-session-journal-types'
import { isRootAgentJournalItem } from './agent-session-journal-producer'
import { isStructuredAgentSessionCommandEntry } from './structured-agent-session-command-entry'
import { sliceAtCodeUnitLimit } from './surrogate-safe-text-slice'
import { stripSentImagePromptMarkers } from './native-chat/transcript/native-chat-image-transcript-markers'

export const STRUCTURED_CHAT_NAME_PROMPT_LIMIT = 4000

export function hasStructuredChatPromptText(prompt: string): boolean {
  return /[\p{L}\p{N}]/u.test(prompt)
}

export function firstStructuredAgentSessionPrompt(
  items: readonly AgentJournalRenderItem[]
): string {
  for (const item of items) {
    const body = item.body
    if (
      body.kind !== 'message' ||
      body.role !== 'user' ||
      !isRootAgentJournalItem(item) ||
      isStructuredAgentSessionCommandEntry(body)
    ) {
      continue
    }
    let prompt = ''
    const imageCount = body.blocks.filter((block) => block.type === 'image-ref').length
    for (const block of body.blocks) {
      if (block.type === 'text') {
        if (prompt) {
          prompt += '\n'
        }
        // A title names what was asked; `[Image #N]` only says where an image sat.
        prompt += sliceAtCodeUnitLimit(
          stripSentImagePromptMarkers(block.text, imageCount),
          STRUCTURED_CHAT_NAME_PROMPT_LIMIT - prompt.length
        )
        if (prompt.length >= STRUCTURED_CHAT_NAME_PROMPT_LIMIT) {
          break
        }
      }
    }
    if (hasStructuredChatPromptText(prompt)) {
      return prompt.trim()
    }
  }
  return ''
}