mirror of
https://github.com/stablyai/orca.git
synced 2026-11-02 10:08:13 +00:00
* Keep clear in the same chat and reset its provider context * Derive clear pauses only from journal membership and cover older rewind clients * Point queued command rewind test at the structured hook * Group queue consume errors and rewind replay assertions
66 lines
1.9 KiB
TypeScript
66 lines
1.9 KiB
TypeScript
import type {
|
|
AgentJournalItemBody,
|
|
AgentJournalRenderItem,
|
|
AgentJournalSubmission
|
|
} from './agent-session-journal-types'
|
|
import { isAgentSessionProviderContextBoundary } from './agent-session-provider-context'
|
|
import type { NativeChatMessage } from './native-chat-types'
|
|
|
|
export function isAgentSessionContextClear(body: AgentJournalItemBody | undefined): boolean {
|
|
return body?.kind === 'status' && isAgentSessionProviderContextBoundary(body.contextClear)
|
|
}
|
|
|
|
export function isNativeChatContextClear(message: NativeChatMessage): boolean {
|
|
return (
|
|
message.role === 'system' &&
|
|
message.blocks.some(
|
|
(block) => block.type === 'text' && isAgentSessionProviderContextBoundary(block.contextClear)
|
|
)
|
|
)
|
|
}
|
|
|
|
export function agentSessionContextSequenceFor(
|
|
sequence: number,
|
|
boundaries: readonly number[]
|
|
): number {
|
|
let left = 0
|
|
let right = boundaries.length
|
|
while (left < right) {
|
|
const middle = Math.floor((left + right) / 2)
|
|
if (boundaries[middle] < sequence) {
|
|
left = middle + 1
|
|
} else {
|
|
right = middle
|
|
}
|
|
}
|
|
return left > 0 ? boundaries[left - 1] : 0
|
|
}
|
|
|
|
export function latestAgentSessionContextClearSequence(
|
|
items: Iterable<Pick<AgentJournalRenderItem, 'sequence' | 'body'>>
|
|
): number {
|
|
let sequence = 0
|
|
for (const item of items) {
|
|
if (item.sequence > sequence && isAgentSessionContextClear(item.body)) {
|
|
sequence = item.sequence
|
|
}
|
|
}
|
|
return sequence
|
|
}
|
|
|
|
export function agentSessionCurrentContextRows(
|
|
items: readonly AgentJournalRenderItem[],
|
|
submissions: readonly AgentJournalSubmission[] = [],
|
|
minimumSequence = 0
|
|
) {
|
|
const sequence = Math.max(minimumSequence, latestAgentSessionContextClearSequence(items))
|
|
return sequence > 0
|
|
? {
|
|
items: items.filter((item) => item.sequence > sequence),
|
|
submissions: submissions.filter(
|
|
(submission) => (submission.acceptedSequence ?? 0) > sequence
|
|
)
|
|
}
|
|
: { items, submissions }
|
|
}
|