11// User turn transcript helpers extract user-turn text from session transcripts.
22import { mimeTypeFromFilePath } from "@openclaw/media-core/mime" ;
3+ import { asOptionalRecord } from "@openclaw/normalization-core/record-coerce" ;
4+ import { normalizeOptionalString } from "@openclaw/normalization-core/string-coerce" ;
35import type { AgentMessage } from "../../packages/agent-core/src/types.js" ;
46import {
57 persistSessionTranscriptTurn ,
@@ -35,22 +37,9 @@ export function buildRunUserTurnIdempotencyKey(runId: string): string {
3537 return `${ runId } :user` ;
3638}
3739
38- function normalizeOptionalText ( value : string | null | undefined ) : string | undefined {
39- const normalized = value ?. trim ( ) ;
40- return normalized ? normalized : undefined ;
41- }
42-
43- function normalizeTranscriptText ( value : string | null | undefined ) : string {
44- return value ?? "" ;
45- }
46-
4740// Select normalized text for persisted user turns.
4841export function resolvePersistedUserTurnText ( value : string | null | undefined ) : string | undefined {
49- const normalized = normalizeOptionalText ( value ) ;
50- if ( ! normalized ) {
51- return undefined ;
52- }
53- return normalized ;
42+ return normalizeOptionalString ( value ) ;
5443}
5544
5645function resolveTranscriptMediaType ( params : {
@@ -70,16 +59,16 @@ export function buildPersistedUserTurnMediaInputsFromFields(
7059
7160 const facts = readPersistedMediaFacts ( fields ) ?? [ ] ;
7261 const normalizedMedia = facts . map ( ( fact ) => {
73- const rawPath = normalizeOptionalText ( fact . path ) ;
62+ const rawPath = normalizeOptionalString ( fact . path ) ;
7463 const mediaPath = rawPath
75- ? resolveTranscriptMediaPath ( rawPath , normalizeOptionalText ( fact . workspaceDir ) )
64+ ? resolveTranscriptMediaPath ( rawPath , normalizeOptionalString ( fact . workspaceDir ) )
7665 : undefined ;
77- const url = normalizeOptionalText ( fact . url ) ;
66+ const url = normalizeOptionalString ( fact . url ) ;
7867 if ( ! mediaPath && ! url ) {
7968 return { } ;
8069 }
8170 const contentType = resolveTranscriptMediaType ( {
82- explicitType : normalizeOptionalText ( fact . contentType ) ,
71+ explicitType : normalizeOptionalString ( fact . contentType ) ,
8372 mediaPath,
8473 mediaUrl : url ,
8574 } ) ;
@@ -131,9 +120,9 @@ export function buildLateMediaAttachedProjection(message: AgentMessage): {
131120function buildUserTurnSenderMeta (
132121 sender : UserTurnInput [ "sender" ] ,
133122) : Record < string , string > | undefined {
134- const senderId = normalizeOptionalText ( sender ?. id ) ;
135- const senderName = normalizeOptionalText ( sender ?. name ) ;
136- const senderUsername = normalizeOptionalText ( sender ?. username ) ;
123+ const senderId = normalizeOptionalString ( sender ?. id ) ;
124+ const senderName = normalizeOptionalString ( sender ?. name ) ;
125+ const senderUsername = normalizeOptionalString ( sender ?. username ) ;
137126 if ( ! senderId && ! senderName && ! senderUsername ) {
138127 return undefined ;
139128 }
@@ -145,15 +134,12 @@ function buildUserTurnSenderMeta(
145134}
146135
147136function readOpenClawMessageMeta ( message : AgentMessage ) : Record < string , unknown > | undefined {
148- const meta = ( message as unknown as Record < string , unknown > ) [ "__openclaw" ] ;
149- return meta && typeof meta === "object" && ! Array . isArray ( meta )
150- ? ( meta as Record < string , unknown > )
151- : undefined ;
137+ return asOptionalRecord ( ( message as unknown as Record < string , unknown > ) [ "__openclaw" ] ) ;
152138}
153139
154140export function buildPersistedUserTurnMessage ( params : UserTurnInput ) : PersistedUserTurnMessage {
155141 const normalizedMedia = ( params . media ?? [ ] ) . map ( normalizeStructuredMediaEntryForTranscript ) ;
156- const text = normalizeTranscriptText ( params . text ) ;
142+ const text = params . text ?? "" ;
157143 // Storage is BARE (no timestamp prefix). The per-message timestamp is added
158144 // at the single LLM-boundary stamping site (normalizeMessagesForLlmBoundary),
159145 // derived from each message's own `timestamp` field, so the current turn and
@@ -338,10 +324,8 @@ export function preparePersistedUserTurnMessageForTranscriptWrite(
338324 originalMediaImageLayout === undefined ? undefined : structuredClone ( originalMediaImageLayout ) ;
339325 // Hooks receive the original message object and may mutate nested metadata in
340326 // place. Snapshot transport correlation before handing them that reference.
341- const transport =
342- originalTransport && typeof originalTransport === "object" && ! Array . isArray ( originalTransport )
343- ? { ...originalTransport }
344- : undefined ;
327+ const originalTransportRecord = asOptionalRecord ( originalTransport ) ;
328+ const transport = originalTransportRecord ? { ...originalTransportRecord } : undefined ;
345329 const nextMessage = params . beforeMessageWrite ( {
346330 message,
347331 ...( params . agentId ? { agentId : params . agentId } : { } ) ,
0 commit comments