mirror of
https://github.com/stablyai/orca.git
synced 2026-11-19 04:23:57 +00:00
94 lines
3.5 KiB
TypeScript
94 lines
3.5 KiB
TypeScript
// Derive reasoning disclosure/headlines from host start/end facts so desktop/mobile show the same lifecycle.
|
|
|
|
import { isRootAgentJournalItem } from '../../agent-session/journal/agent-session-journal-producer'
|
|
import { deriveNativeChatRowContent, nativeChatRowRendersContent } from './native-chat-row-content'
|
|
import { formatNativeChatDuration } from './native-chat-turn-status'
|
|
import type { NativeChatMessage } from '../native-chat-types'
|
|
|
|
/** An open reasoning block and the text it has so far. */
|
|
export type NativeChatLiveReasoning = { message: NativeChatMessage; markdown: string }
|
|
|
|
/** Newest content of the live working turn must be an open reasoning block. Only nativeChatLiveLine consumes it,
|
|
* so the block row hides exactly while the disclosing line draws. */
|
|
export function selectNativeChatLiveReasoning(
|
|
messages: readonly NativeChatMessage[],
|
|
inLiveWorkingTurn: (index: number) => boolean
|
|
): NativeChatLiveReasoning | null {
|
|
for (let index = messages.length - 1; index >= 0; index -= 1) {
|
|
const message = messages[index]
|
|
if (!message || !inLiveWorkingTurn(index)) {
|
|
continue
|
|
}
|
|
if (message.role === 'user') {
|
|
return null
|
|
}
|
|
// A notice is not newer content, and a subagent's reasoning draws in its own section.
|
|
if (message.role === 'system' || !isRootAgentJournalItem(message)) {
|
|
continue
|
|
}
|
|
if (message.role !== 'reasoning') {
|
|
if (!nativeChatRowRendersContent(message.blocks)) {
|
|
continue
|
|
}
|
|
return null
|
|
}
|
|
// Not `=== 'running'`: a host that keeps no lifecycle gets the same single live slot.
|
|
if (message.state === 'completed') {
|
|
return null
|
|
}
|
|
const { markdown } = deriveNativeChatRowContent(message.blocks)
|
|
return markdown.trim().length > 0 ? { message, markdown } : null
|
|
}
|
|
return null
|
|
}
|
|
|
|
/** One key per reasoning block, read by the live line and the finished row alike. */
|
|
export function nativeChatReasoningDisclosureKey(messageId: string): string {
|
|
return `reasoning:${messageId}`
|
|
}
|
|
|
|
export type NativeChatReasoningHeadline =
|
|
/** From a host that kept no lifecycle, or not yet ended: nothing is claimed. */
|
|
| { kind: 'reasoning' }
|
|
/** Ended, with no span the host saw. */
|
|
| { kind: 'thought' }
|
|
| { kind: 'thoughtFor'; duration: string }
|
|
|
|
/** `live`: the row is drawn inside its working turn or working subagent. */
|
|
export function nativeChatReasoningHeadline(
|
|
message: Pick<NativeChatMessage, 'state' | 'completedAt' | 'timestamp'>,
|
|
{ live }: { live: boolean }
|
|
): NativeChatReasoningHeadline {
|
|
// Not ended yet, so it claims no past tense.
|
|
if (message.state === undefined || (live && message.state !== 'completed')) {
|
|
return { kind: 'reasoning' }
|
|
}
|
|
// An open row in a turn that is no longer live ended unseen, so it claims no duration.
|
|
if (
|
|
message.state !== 'completed' ||
|
|
message.completedAt === undefined ||
|
|
message.timestamp === null
|
|
) {
|
|
return { kind: 'thought' }
|
|
}
|
|
return {
|
|
kind: 'thoughtFor',
|
|
duration: formatNativeChatDuration(
|
|
Math.max(1, (message.completedAt - message.timestamp) / 1000)
|
|
)
|
|
}
|
|
}
|
|
|
|
/** English copy for clients without a translation catalog; desktop translates the same three. */
|
|
const NATIVE_CHAT_REASONING_COPY = {
|
|
reasoning: 'Reasoning',
|
|
thought: 'Thought',
|
|
thoughtFor: (duration: string) => `Thought for ${duration}`
|
|
} as const
|
|
|
|
export function nativeChatReasoningHeadlineText(headline: NativeChatReasoningHeadline): string {
|
|
return headline.kind === 'thoughtFor'
|
|
? NATIVE_CHAT_REASONING_COPY.thoughtFor(headline.duration)
|
|
: NATIVE_CHAT_REASONING_COPY[headline.kind]
|
|
}
|