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 { rebaseWorkspaceAttachmentOrigins } from './workspace-attachment-origins'
import type { WorkspaceAttachment } from './worktree/types'
import {
  getWorkspaceAttachmentDedupeKey,
  matchesWorkspaceAttachmentIdentity,
  normalizeWorkspaceAttachments
} from './workspace-attachment-normalization'

/** Mutation-only snapshot; never persisted with workspace metadata. */
export type WorkspaceAttachmentMutation = {
  linkedItemsBase?: WorkspaceAttachment[]
  linkedItemsSelectionChanged?: boolean
}

export function mergeWorkspaceAttachmentMutation(
  base: readonly WorkspaceAttachment[],
  current: readonly WorkspaceAttachment[],
  requested: readonly WorkspaceAttachment[]
): WorkspaceAttachment[] {
  const currentKeys = new Set(current.map(getWorkspaceAttachmentDedupeKey))
  const baseKey = (item: WorkspaceAttachment): string => {
    const key = getWorkspaceAttachmentDedupeKey(item)
    if (currentKeys.has(key)) {
      return key
    }
    const matches = current.filter((candidate) =>
      matchesWorkspaceAttachmentIdentity(candidate, item)
    )
    const requestedVersions = requested.filter((candidate) =>
      matchesWorkspaceAttachmentIdentity(candidate, item)
    )
    if (
      matches.length === 1 &&
      (requestedVersions.length === 0 ||
        requestedVersions.some(
          (candidate) =>
            matchesWorkspaceAttachmentIdentity(matches[0], candidate) ||
            matchesWorkspaceAttachmentIdentity(candidate, matches[0])
        ))
    ) {
      return getWorkspaceAttachmentDedupeKey(matches[0])
    }
    return key
  }
  const mutationKey = (item: WorkspaceAttachment): string => {
    const key = getWorkspaceAttachmentDedupeKey(item)
    if (currentKeys.has(key)) {
      return key
    }
    const previous = base.filter((candidate) => matchesWorkspaceAttachmentIdentity(item, candidate))
    const original = previous.length === 1 ? previous[0] : undefined
    return original &&
      requested.filter((candidate) => matchesWorkspaceAttachmentIdentity(candidate, original))
        .length === 1
      ? baseKey(original)
      : key
  }
  const baseByKey = new Map(base.map((item) => [baseKey(item), item]))
  const requestedByKey = new Map(requested.map((item) => [mutationKey(item), item]))
  const retained = current
    .filter((item) => {
      const key = getWorkspaceAttachmentDedupeKey(item)
      return !baseByKey.has(key) || requestedByKey.has(key)
    })
    .map((item) => {
      const key = getWorkspaceAttachmentDedupeKey(item)
      const changed = requestedByKey.get(key)
      if (!changed || JSON.stringify(baseByKey.get(key)) === JSON.stringify(changed)) {
        return item
      }
      const previous = baseByKey.get(key)
      if (!previous) {
        return {
          ...item,
          ...(changed.origins !== undefined
            ? {
                origins: rebaseWorkspaceAttachmentOrigins(item.origins, undefined, changed.origins)
              }
            : {})
        }
      }
      const fields = [
        'title',
        'url',
        'repoId',
        'identifier',
        'linearIdentifier',
        'jiraIdentifier',
        'linearWorkspaceId',
        'linearOrganizationUrlKey',
        'taskSourceContext'
      ] as const
      const patch = Object.fromEntries(
        fields
          .filter((field) => JSON.stringify(previous?.[field]) !== JSON.stringify(changed[field]))
          .map((field) => [field, changed[field]])
      )
      return {
        ...item,
        ...patch,
        ...(changed.origins !== undefined
          ? {
              origins: rebaseWorkspaceAttachmentOrigins(
                item.origins,
                previous?.origins,
                changed.origins
              )
            }
          : {})
      }
    })
  const additions = requested.filter((item) => {
    const key = mutationKey(item)
    return !baseByKey.has(key) && !currentKeys.has(key)
  })
  return normalizeWorkspaceAttachments([...retained, ...additions])
}