mirror of
https://github.com/stablyai/orca.git
synced 2026-10-30 16:47:34 +00:00
49 lines
1.6 KiB
TypeScript
49 lines
1.6 KiB
TypeScript
import type { AgentJournalRenderItem } from './agent-session-journal-types'
|
|
import { isRootAgentJournalItem } from './agent-session-journal-producer'
|
|
import { isStructuredAgentSessionCommandEntry } from './structured-agent-session-command-entry'
|
|
import { sliceAtCodeUnitLimit } from './surrogate-safe-text-slice'
|
|
import { stripSentImagePromptMarkers } from './native-chat-image-transcript-markers'
|
|
|
|
export const STRUCTURED_CHAT_NAME_PROMPT_LIMIT = 4000
|
|
|
|
export function hasStructuredChatPromptText(prompt: string): boolean {
|
|
return /[\p{L}\p{N}]/u.test(prompt)
|
|
}
|
|
|
|
export function firstStructuredAgentSessionPrompt(
|
|
items: readonly AgentJournalRenderItem[]
|
|
): string {
|
|
for (const item of items) {
|
|
const body = item.body
|
|
if (
|
|
body.kind !== 'message' ||
|
|
body.role !== 'user' ||
|
|
!isRootAgentJournalItem(item) ||
|
|
isStructuredAgentSessionCommandEntry(body)
|
|
) {
|
|
continue
|
|
}
|
|
let prompt = ''
|
|
const imageCount = body.blocks.filter((block) => block.type === 'image-ref').length
|
|
for (const block of body.blocks) {
|
|
if (block.type === 'text') {
|
|
if (prompt) {
|
|
prompt += '\n'
|
|
}
|
|
// A title names what was asked; `[Image #N]` only says where an image sat.
|
|
prompt += sliceAtCodeUnitLimit(
|
|
stripSentImagePromptMarkers(block.text, imageCount),
|
|
STRUCTURED_CHAT_NAME_PROMPT_LIMIT - prompt.length
|
|
)
|
|
if (prompt.length >= STRUCTURED_CHAT_NAME_PROMPT_LIMIT) {
|
|
break
|
|
}
|
|
}
|
|
}
|
|
if (hasStructuredChatPromptText(prompt)) {
|
|
return prompt.trim()
|
|
}
|
|
}
|
|
return ''
|
|
}
|