Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript
7.0 KB
Raw Blame
import { describe, expect, it } from 'vitest'
import type { ComposerImageAttachment, ComposerReferenceAttachment } from '../../uiTypes'
import {
  buildChatMessageAttachments,
  buildResponseFileAttachments,
  parseChatMessageDisplay,
} from '../chatMessageAttachments'

describe('chat message attachments', () => {
  it('parses legacy attached context text into display attachments', () => {
    const parsed = parseChatMessageDisplay(
      'what is the price?\n\n[Attached context]\n- mcp-coingecko-remote'
    )

    expect(parsed.content).toBe('what is the price?')
    expect(parsed.attachments).toMatchObject([{ type: 'connector', label: 'mcp-coingecko-remote' }])
  })

  it('preserves dotted agent file labels from composer prompt context', () => {
    const parsed = parseChatMessageDisplay(
      [
        'send this invitation',
        '',
        'USER-ATTACHED CONTEXT: The user selected these capabilities/files for this message. Prefer them when they are relevant to the request.',
        'Agent Files: assets/invite.png, data.json. Use clerum__context_files_list and clerum__context_files_read to inspect these paths before relying on their contents.',
      ].join('\n')
    )

    expect(parsed.content).toBe('send this invitation')
    expect(parsed.attachments).toMatchObject([
      { type: 'agent_file', label: 'invite.png' },
      { type: 'agent_file', label: 'data.json' },
    ])
  })

  it('restores friendly global file labels after composer prompt content reloads', () => {
    const parsed = parseChatMessageDisplay(
      [
        'summarize this report',
        '',
        'USER-ATTACHED CONTEXT: The user selected these capabilities/files for this message. Prefer them when they are relevant to the request.',
        'Global Files: quarterly-report.pdf (gfs://main/0123456789abcdef). These files were explicitly selected by the user. Use clerum__gfs_resolve for each gfs:// URI.',
      ].join('\n')
    )

    expect(parsed.content).toBe('summarize this report')
    expect(parsed.attachments).toMatchObject([
      { type: 'global_file', label: 'quarterly-report.pdf' },
    ])
  })

  it('uses the basename for legacy raw global file URI attachments', () => {
    const parsed = parseChatMessageDisplay(
      [
        'inspect this',
        'USER-ATTACHED CONTEXT: The user selected these capabilities/files for this message. Prefer them when they are relevant to the request.',
        'Global Files: gfs://main/archive/legacy-report.pdf. These files were explicitly selected by the user.',
      ].join('\n')
    )

    expect(parsed.attachments).toMatchObject([{ type: 'global_file', label: 'legacy-report.pdf' }])
  })

  const HEADER =
    'USER-ATTACHED CONTEXT: The user selected these capabilities/files for this message. Prefer them when they are relevant to the request.'

  it('reads quoted lists whose names contain commas and sentence breaks', () => {
    const parsed = parseChatMessageDisplay(
      [
        'compare these',
        '',
        HEADER,
        'Agent Files: "assets/x, y.png". Use clerum__context_files_list and clerum__context_files_read to inspect these paths before relying on their contents.',
        'Global Files: "a, b.md", "c. d.pdf", "gfs://main/named-like-a-uri.pdf". These files were explicitly selected by the user.',
      ].join('\n')
    )

    expect(parsed.content).toBe('compare these')
    expect(parsed.attachments).toMatchObject([
      { type: 'agent_file', label: 'x, y.png' },
      { type: 'global_file', label: 'a, b.md' },
      { type: 'global_file', label: 'c. d.pdf' },
      // A quoted Global Files entry is the label as written, not a URI.
      { type: 'global_file', label: 'gfs://main/named-like-a-uri.pdf' },
    ])
  })

  it('reads a stored pre-#666 list whose first entry merely starts with a quote', () => {
    const parsed = parseChatMessageDisplay(
      [
        'summarize both',
        '',
        HEADER,
        'Global Files: "draft".md (gfs://main/0123), notes.md (gfs://main/abcd). These files were explicitly selected by the user.',
      ].join('\n')
    )

    expect(parsed.attachments).toMatchObject([
      { type: 'global_file', label: '"draft".md' },
      { type: 'global_file', label: 'notes.md' },
    ])
  })

  it('shows no entries for a quoted list it cannot delimit', () => {
    const parsed = parseChatMessageDisplay(
      [
        'check',
        '',
        HEADER,
        'Connectors: "github". Use MCP tools whose prefix before "__" exactly matches one of these connector names.',
        'Global Files: "a.md" "b.md". These files were explicitly selected by the user.',
        'Plugins: "unterminated. Use workflow tools for these plugin names.',
      ].join('\n')
    )

    // Witness: the well-formed line in the same section was read.
    expect(parsed.attachments).toMatchObject([{ type: 'connector', label: 'github' }])
    expect(parsed.attachments).toHaveLength(1)
  })

  it('builds display attachments in composer insertion order', () => {
    const references: ComposerReferenceAttachment[] = [
      {
        id: 'connector:first',
        type: 'connector',
        name: 'first',
        label: 'first',
        addedOrder: 1,
      },
      {
        id: 'connector:second',
        type: 'connector',
        name: 'second',
        label: 'second',
        addedOrder: 3,
      },
    ]
    const images: ComposerImageAttachment[] = [
      {
        id: 'image:logo',
        name: 'logo.png',
        mimeType: 'image/png',
        dataBase64: 'abc',
        sizeBytes: 1024,
        previewDataUrl: 'blob:logo',
        addedOrder: 2,
      },
    ]

    expect(
      buildChatMessageAttachments(images, references, []).map(attachment => attachment.label)
    ).toEqual(['first', 'logo.png', 'second'])
  })

  it('keeps the image bytes on uploaded_file display attachments (BUG-176 chip preview)', () => {
    const images: ComposerImageAttachment[] = [
      {
        id: 'image:shot',
        name: 'shot.png',
        mimeType: 'image/png',
        dataBase64: 'AQID',
        sizeBytes: 3,
        previewDataUrl: 'blob:shot',
      },
    ]

    expect(buildChatMessageAttachments(images, [], [])[0]).toMatchObject({
      type: 'uploaded_file',
      label: 'shot.png',
      filename: 'shot.png',
      mimeType: 'image/png',
      encoding: 'base64',
      dataBase64: 'AQID',
      sizeBytes: 3,
    })
  })

  it('builds downloadable generated-file attachments from task responses', () => {
    const attachments = buildResponseFileAttachments({
      attachments: [
        {
          kind: 'file',
          filename: 'research-summary.pdf',
          mimeType: 'application/pdf',
          encoding: 'base64',
          dataBase64: 'JVBERi0x',
          sizeBytes: 14600,
        },
      ],
    })

    expect(attachments).toEqual([
      expect.objectContaining({
        type: 'response_file',
        label: 'research-summary.pdf',
        filename: 'research-summary.pdf',
        mimeType: 'application/pdf',
        encoding: 'base64',
        dataBase64: 'JVBERi0x',
        sizeBytes: 14600,
      }),
    ])
  })
})