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.

3.9 KB
Raw Blame
import { describe, expect, it } from 'vitest'
import type { AgentJournalRenderItem } from './agent-session-journal-types'
import {
  firstStructuredAgentSessionPrompt,
  STRUCTURED_CHAT_NAME_PROMPT_LIMIT
} from './structured-agent-session-first-prompt'

function message(
  text: string,
  overrides: Partial<AgentJournalRenderItem> = {}
): AgentJournalRenderItem {
  return {
    itemId: 'message',
    sequence: 1,
    revision: 1,
    observedAt: 1,
    body: { kind: 'message', role: 'user', blocks: [{ type: 'text', text }] },
    ...overrides
  }
}

describe('first structured chat prompt', () => {
  it('reads the first root prose, skipping commands, subagents, and empty messages', () => {
    const command = message('/compact', {
      body: {
        kind: 'message',
        role: 'user',
        blocks: [{ type: 'text', text: '/compact' }],
        command: { name: 'compact' }
      }
    })
    expect(
      firstStructuredAgentSessionPrompt([
        command,
        message('Child prompt', { agentId: 'child' }),
        message(''),
        message('Assistant', {
          body: {
            kind: 'message',
            role: 'assistant',
            blocks: [{ type: 'text', text: 'Assistant' }]
          }
        }),
        message(' first user prompt '),
        message('later user prompt')
      ])
    ).toBe('first user prompt')
  })

  it('bounds a multi-block prompt before joining it', () => {
    const prompt = message('', {
      body: {
        kind: 'message',
        role: 'user',
        blocks: [
          { type: 'text', text: 'x'.repeat(STRUCTURED_CHAT_NAME_PROMPT_LIMIT - 2) },
          { type: 'text', text: 'yz'.repeat(100_000) }
        ]
      }
    })
    const result = firstStructuredAgentSessionPrompt([prompt])
    expect(result).toHaveLength(STRUCTURED_CHAT_NAME_PROMPT_LIMIT)
    expect(result.endsWith('\ny')).toBe(true)
  })

  it('clips a single prompt block without splitting a Unicode character', () => {
    const prefix = 'A'.repeat(STRUCTURED_CHAT_NAME_PROMPT_LIMIT - 1)
    expect(firstStructuredAgentSessionPrompt([message(`${prefix}𠮷`)])).toBe(prefix)
  })

  it('clips a later prompt block without splitting a Unicode character', () => {
    const prefix = 'A'.repeat(STRUCTURED_CHAT_NAME_PROMPT_LIMIT - 2)
    const prompt = message('', {
      body: {
        kind: 'message',
        role: 'user',
        blocks: [
          { type: 'text', text: prefix },
          { type: 'text', text: '𠮷' }
        ]
      }
    })
    expect(firstStructuredAgentSessionPrompt([prompt])).toBe(prefix)
  })

  it('names a chat by what was asked, not by where its images sat', () => {
    const withImages = (text: string, images: number, itemId: string) =>
      message('', {
        itemId,
        body: {
          kind: 'message',
          role: 'user',
          blocks: [
            ...Array.from({ length: images }, () => ({
              type: 'image-ref' as const,
              path: '/tmp/a.png'
            })),
            { type: 'text', text }
          ]
        }
      })
    expect(
      firstStructuredAgentSessionPrompt([
        withImages('[Image #1] ', 1, 'first'),
        withImages('compare [Image #1] with [Image #2]', 2, 'second')
      ])
    ).toBe('compare  with')
    // A marker the user typed, with no image of that number, is their text.
    expect(firstStructuredAgentSessionPrompt([message('Explain [Image #1]')])).toBe(
      'Explain [Image #1]'
    )
  })

  it('skips punctuation-only entries in favor of the first text request', () => {
    expect(
      firstStructuredAgentSessionPrompt([message('?!'), message('修复登录'), message('Later')])
    ).toBe('修复登录')
    expect(firstStructuredAgentSessionPrompt([message('?!')])).toBe('')
  })

  it('retains a URL-only first request', () => {
    const url = 'https://example.test/issues/123'
    expect(firstStructuredAgentSessionPrompt([message(url), message('Later')])).toBe(url)
  })
})