Recover first prompt for resume sessions

This commit is contained in:
weishu
2026-05-20 20:35:03 +08:00
parent 856af6d8b2
commit 5264599908
4 changed files with 103 additions and 8 deletions
+27 -4
View File
@@ -48,6 +48,28 @@ const SYSTEM_INJECTION_PREFIXES = [
'<system-reminder>',
]
function extractRawUserTextContent(content: unknown): string | null {
if (typeof content === 'string') {
return content
}
if (!Array.isArray(content)) {
return null
}
const parts = content
.map((block) => {
if (!block || typeof block !== 'object' || Array.isArray(block)) return null
const record = block as Record<string, unknown>
return record.type === 'text' && typeof record.text === 'string'
? record.text
: null
})
.filter((text): text is string => text !== null)
return parts.length > 0 ? parts.join('\n') : null
}
/**
* Returns true if a JSONL message should be classified as a user-role message
* (i.e., text typed by a real human) rather than an agent-role message.
@@ -58,13 +80,14 @@ const SYSTEM_INJECTION_PREFIXES = [
* genuine user messages, so the only reliable signal is the message content
* itself: injected messages always start with a well-known XML tag.
*/
export function isExternalUserMessage(body: RawJSONLines): body is Extract<RawJSONLines, { type: 'user' }> & { message: { content: string } } {
export function isExternalUserMessage(body: RawJSONLines): body is Extract<RawJSONLines, { type: 'user' }> {
if (body.type !== 'user') return false
if (typeof body.message.content !== 'string') return false
const text = extractRawUserTextContent(body.message.content)
if (text === null) return false
if (body.isSidechain === true) return false
if (body.isMeta === true) return false
const trimmed = body.message.content.trimStart()
const trimmed = text.trimStart()
for (const prefix of SYSTEM_INJECTION_PREFIXES) {
if (trimmed.startsWith(prefix)) return false
}
@@ -440,7 +463,7 @@ export class ApiSessionClient extends EventEmitter {
role: 'user',
content: {
type: 'text',
text: body.message.content
text: extractRawUserTextContent(body.message.content) ?? ''
},
meta: {
sentFrom: 'cli'