mirror of
https://github.com/stablyai/orca.git
synced 2026-11-19 04:23:57 +00:00
* Show changed files directly in chat turns * Use the fixture frame for the diff interaction * Keep changed filenames readable and preserve file actions * Keep unconfirmed file changes in tool activity * Capture changed files in completed transcript fixtures * Preserve patch data with authoritative history status * Keep mobile diff interaction coverage beside its card * Assert the decoded patch line contract * Exercise full returned history in catch-up coverage * test(chat): target file summary when reopening patches
163 lines
5.1 KiB
TypeScript
163 lines
5.1 KiB
TypeScript
import type { NativeChatEditFile } from './native-chat-edit-model'
|
|
import type {
|
|
NativeChatMessage,
|
|
NativeChatToolCallBlock,
|
|
NativeChatToolResultBlock
|
|
} from '../native-chat-types'
|
|
import { buildEditSummaries, normalizedToolEditFiles } from './native-chat-edit-cards'
|
|
|
|
export type NativeChatDiffTarget = {
|
|
messageId: string
|
|
editKey: string
|
|
fileIndex: number
|
|
/** The subagent sections the row sits in, outermost first, which a reveal opens. */
|
|
subagentSections?: readonly string[]
|
|
}
|
|
|
|
export type NativeChatDiffReveal = NativeChatDiffTarget & { requestId: number }
|
|
|
|
export type NativeChatTurnDiffEdit = {
|
|
call: NativeChatToolCallBlock
|
|
result: NativeChatToolResultBlock | undefined
|
|
fileIndex: number
|
|
target: NativeChatDiffTarget
|
|
order: number
|
|
}
|
|
|
|
export type NativeChatTurnDiffFile = {
|
|
path: string
|
|
added: number
|
|
removed: number
|
|
truncated: boolean
|
|
target: NativeChatDiffTarget
|
|
edits: NativeChatTurnDiffEdit[]
|
|
}
|
|
|
|
export type NativeChatTurnDiff = {
|
|
files: NativeChatTurnDiffFile[]
|
|
added: number
|
|
removed: number
|
|
truncated: boolean
|
|
}
|
|
|
|
/** Resolve a patch only when its inline card is opened. */
|
|
export function nativeChatTurnDiffEditFile(
|
|
edit: NativeChatTurnDiffEdit
|
|
): NativeChatEditFile | undefined {
|
|
return normalizedToolEditFiles(edit.call, edit.result)?.[edit.fileIndex]
|
|
}
|
|
|
|
export function nativeChatTurnDiffs(
|
|
messages: readonly NativeChatMessage[],
|
|
turnKeys: readonly (string | undefined)[],
|
|
subagentSectionsOf?: ReadonlyMap<string, readonly string[]>,
|
|
/** A turn only partly loaded, whose totals would read as the whole turn's. */
|
|
partialTurnKey?: string
|
|
): Map<string, NativeChatTurnDiff> {
|
|
const turns = new Map<string, Map<string, NativeChatTurnDiffFile>>()
|
|
let editOrder = 0
|
|
for (const [index, message] of messages.entries()) {
|
|
const turnKey = turnKeys[index]
|
|
if (!turnKey) {
|
|
continue
|
|
}
|
|
const sections = subagentSectionsOf?.get(message.id)
|
|
for (const [call, edit] of buildEditSummaries(message.blocks)) {
|
|
let files = turns.get(turnKey)
|
|
if (!files) {
|
|
files = new Map()
|
|
turns.set(turnKey, files)
|
|
}
|
|
for (const [fileIndex, file] of edit.files.entries()) {
|
|
const previous = files.get(file.path)
|
|
const renamed =
|
|
file.oldPath && file.oldPath !== file.path ? files.get(file.oldPath) : undefined
|
|
if (renamed) {
|
|
files.delete(renamed.path)
|
|
}
|
|
const target: NativeChatDiffTarget = {
|
|
messageId: message.id,
|
|
editKey: edit.key,
|
|
fileIndex,
|
|
...(sections === undefined ? {} : { subagentSections: sections })
|
|
}
|
|
const entry = previous ??
|
|
renamed ?? {
|
|
path: file.path,
|
|
added: 0,
|
|
removed: 0,
|
|
truncated: false,
|
|
target,
|
|
edits: []
|
|
}
|
|
if (renamed && previous) {
|
|
entry.added += renamed.added
|
|
entry.removed += renamed.removed
|
|
entry.truncated ||= renamed.truncated
|
|
entry.edits.push(...renamed.edits)
|
|
entry.edits.sort((left, right) => left.order - right.order)
|
|
}
|
|
entry.path = file.path
|
|
entry.added += file.added
|
|
entry.removed += file.removed
|
|
entry.truncated ||= file.truncated
|
|
entry.target = target
|
|
entry.edits.push({ call, result: edit.result, fileIndex, target, order: editOrder++ })
|
|
files.set(file.path, entry)
|
|
}
|
|
}
|
|
}
|
|
return new Map(
|
|
Array.from(turns, ([key, byPath]) => {
|
|
const files = Array.from(byPath.values())
|
|
return [
|
|
key,
|
|
{
|
|
files,
|
|
added: files.reduce((sum, file) => sum + file.added, 0),
|
|
removed: files.reduce((sum, file) => sum + file.removed, 0),
|
|
truncated: key === partialTurnKey || files.some((file) => file.truncated)
|
|
}
|
|
]
|
|
})
|
|
)
|
|
}
|
|
|
|
export const NATIVE_CHAT_TURN_DIFF_FILE_LIMIT = 20
|
|
|
|
/** Compare recorded inputs so unrelated streaming turns do not repaint existing outcome cards. */
|
|
export function sameNativeChatTurnDiff(
|
|
left: NativeChatTurnDiff | undefined,
|
|
right: NativeChatTurnDiff | undefined
|
|
): boolean {
|
|
return (
|
|
left === right ||
|
|
(!!left &&
|
|
!!right &&
|
|
left.added === right.added &&
|
|
left.removed === right.removed &&
|
|
left.truncated === right.truncated &&
|
|
left.files.length === right.files.length &&
|
|
left.files.every((file, index) => {
|
|
const other = right.files[index]!
|
|
return (
|
|
file.path === other.path &&
|
|
file.added === other.added &&
|
|
file.removed === other.removed &&
|
|
file.truncated === other.truncated &&
|
|
file.edits.length === other.edits.length &&
|
|
file.edits.every((edit, editIndex) => {
|
|
const candidate = other.edits[editIndex]!
|
|
return (
|
|
edit.call === candidate.call &&
|
|
edit.result === candidate.result &&
|
|
edit.fileIndex === candidate.fileIndex &&
|
|
edit.target.messageId === candidate.target.messageId &&
|
|
edit.target.editKey === candidate.target.editKey
|
|
)
|
|
})
|
|
)
|
|
}))
|
|
)
|
|
}
|