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.

2.3 KB
Raw Blame
import { formatNativeChatFileReference } from '../../../../../src/shared/agent-image-paste'
import {
  MobileFileTooLargeError,
  type PickedMobileFile
} from '../../../platform/media-picker-contract'
import type { AgentSessionAttachmentUploadCommitResult } from '../../../../../src/shared/agent-session-attachments'

export type PendingNativeChatFile = {
  readonly id: string
  readonly name: string
  readonly mime: string
  readonly byteLength: number
  readonly path: string | null
}

export function nativeChatTextWithFileReferences(
  text: string,
  files: readonly { readonly path: string }[]
): string {
  if (files.length === 0) {
    return text
  }
  const joined = files.map((file) => formatNativeChatFileReference(file.path)).join(' ')
  return text.trim().length === 0 ? joined : `${text.trimEnd()}\n${joined}`
}

function nativeChatFileAttachFailureMessage(error: unknown, fileName?: string): string {
  if (error instanceof MobileFileTooLargeError) {
    return error.message
  }
  return fileName ? `Couldn't upload ${fileName}` : "Couldn't attach that file"
}

// Keep successfully uploaded files when a later pick/upload fails; only the failing chip is removed.
export async function uploadPickedNativeChatFiles(args: {
  canContinue?: () => boolean
  picked: AsyncIterable<PickedMobileFile>
  upload: (file: PickedMobileFile) => Promise<AgentSessionAttachmentUploadCommitResult>
  nextId: () => string
  onPicked: (file: PendingNativeChatFile) => void
  onStored: (id: string, stored: AgentSessionAttachmentUploadCommitResult) => void
  onFailed: (id: string) => void
  onError: (message: string) => void
}): Promise<void> {
  try {
    if (args.canContinue?.() === false) {
      return
    }
    for await (const file of args.picked) {
      if (args.canContinue?.() === false) {
        break
      }
      const id = args.nextId()
      args.onPicked({
        id,
        name: file.name,
        mime: file.mime,
        byteLength: file.byteLength,
        path: null
      })
      try {
        args.onStored(id, await args.upload(file))
      } catch (error) {
        args.onFailed(id)
        args.onError(nativeChatFileAttachFailureMessage(error, file.name))
      }
      if (args.canContinue?.() === false) {
        break
      }
    }
  } catch (error) {
    args.onError(nativeChatFileAttachFailureMessage(error))
  }
}