11/** Human-readable formatter for `openclaw message` action results. */
2- import { normalizeOptionalString } from "@openclaw/normalization-core/string-coerce" ;
32import { normalizeStringEntries } from "@openclaw/normalization-core/string-normalization" ;
43import { getTerminalTableWidth , renderTable } from "../../packages/terminal-core/src/table.js" ;
54import { isRich , theme } from "../../packages/terminal-core/src/theme.js" ;
@@ -8,6 +7,7 @@ import type { ChannelId } from "../channels/plugins/types.public.js";
87import type { OutboundDeliveryResult } from "../infra/outbound/deliver.js" ;
98import { formatGatewaySummary , formatOutboundDeliverySummary } from "../infra/outbound/format.js" ;
109import {
10+ resolveMessageActionMessageId ,
1111 resolveMessageActionOutcome ,
1212 type MessageActionResult ,
1313} from "../infra/outbound/message-action-contracts.js" ;
@@ -17,24 +17,17 @@ import { shortenText } from "./text-format.js";
1717const resolveChannelLabel = ( channel : ChannelId ) =>
1818 getLoadedChannelPlugin ( channel ) ?. meta . label ?? channel ;
1919
20- function extractMessageId ( payload : unknown ) : string | null {
21- if ( ! payload || typeof payload !== "object" ) {
22- return null ;
23- }
24- const direct = ( payload as { messageId ?: unknown } ) . messageId ;
25- const directId = normalizeOptionalString ( direct ) ;
26- if ( directId ) {
27- return directId ;
28- }
29- const result = ( payload as { result ?: unknown } ) . result ;
30- if ( result && typeof result === "object" ) {
31- const nested = ( result as { messageId ?: unknown } ) . messageId ;
32- const nestedId = normalizeOptionalString ( nested ) ;
33- if ( nestedId ) {
34- return nestedId ;
20+ function firstNonemptyString (
21+ record : Record < string , unknown > | undefined ,
22+ ...keys : string [ ]
23+ ) : string {
24+ for ( const key of keys ) {
25+ const value = typeof record ?. [ key ] === "string" && record ?. [ key ] ;
26+ if ( value ) {
27+ return value as string ; // SAFETY: Preserve the old second accessor read after its string check.
3528 }
3629 }
37- return null ;
30+ return "" ;
3831}
3932
4033type FormatOpts = {
@@ -43,6 +36,25 @@ type FormatOpts = {
4336 displayLimit ?: number ;
4437} ;
4538
39+ function renderSummaryValue ( value : unknown ) : string {
40+ if ( value == null ) {
41+ return "null" ;
42+ }
43+ if ( Array . isArray ( value ) ) {
44+ return `${ value . length } items` ;
45+ }
46+ switch ( typeof value ) {
47+ case "string" :
48+ case "number" :
49+ case "boolean" :
50+ case "bigint" :
51+ case "symbol" :
52+ return String ( value ) ;
53+ default :
54+ return typeof value ;
55+ }
56+ }
57+
4658function renderObjectSummary ( payload : unknown , opts : FormatOpts ) : string {
4759 if ( ! payload || typeof payload !== "object" ) {
4860 return String ( payload ) ;
@@ -54,29 +66,7 @@ function renderObjectSummary(payload: unknown, opts: FormatOpts): string {
5466 }
5567
5668 const rows = keys . slice ( 0 , 20 ) . map ( ( k ) => {
57- const v = obj [ k ] ;
58- const value =
59- v == null
60- ? "null"
61- : Array . isArray ( v )
62- ? `${ v . length } items`
63- : typeof v === "object"
64- ? "object"
65- : typeof v === "string"
66- ? v
67- : typeof v === "number"
68- ? String ( v )
69- : typeof v === "boolean"
70- ? v
71- ? "true"
72- : "false"
73- : typeof v === "bigint"
74- ? v . toString ( )
75- : typeof v === "symbol"
76- ? v . toString ( )
77- : typeof v === "function"
78- ? "function"
79- : "unknown" ;
69+ const value = renderSummaryValue ( obj [ k ] ) ;
8070 return { Key : k , Value : shortenText ( value , 96 ) } ;
8171 } ) ;
8272 return renderTable ( {
@@ -93,25 +83,14 @@ function renderMessageList(messages: unknown[], opts: FormatOpts, emptyLabel: st
9383 const cap = opts . displayLimit ?? 25 ;
9484 const rows = messages . slice ( 0 , cap ) . map ( ( m ) => {
9585 const msg = m as Record < string , unknown > ;
96- const id =
97- ( typeof msg . id === "string" && msg . id ) ||
98- ( typeof msg . ts === "string" && msg . ts ) ||
99- ( typeof msg . messageId === "string" && msg . messageId ) ||
100- "" ;
86+ const id = firstNonemptyString ( msg , "id" , "ts" , "messageId" ) ;
10187 const authorObj = msg . author as Record < string , unknown > | undefined ;
10288 const author =
103- ( typeof msg . authorTag === "string" && msg . authorTag ) ||
104- ( typeof authorObj ?. username === "string" && authorObj . username ) ||
105- ( typeof msg . user === "string" && msg . user ) ||
106- "" ;
107- const time =
108- ( typeof msg . timestamp === "string" && msg . timestamp ) ||
109- ( typeof msg . ts === "string" && msg . ts ) ||
110- "" ;
111- const text =
112- ( typeof msg . content === "string" && msg . content ) ||
113- ( typeof msg . text === "string" && msg . text ) ||
114- "" ;
89+ firstNonemptyString ( msg , "authorTag" ) ||
90+ firstNonemptyString ( authorObj , "username" ) ||
91+ firstNonemptyString ( msg , "user" ) ;
92+ const time = firstNonemptyString ( msg , "timestamp" , "ts" ) ;
93+ const text = firstNonemptyString ( msg , "content" , "text" ) ;
11594 return {
11695 Time : shortenText ( time , 28 ) ,
11796 Author : shortenText ( author , 22 ) ,
@@ -169,10 +148,7 @@ function renderReactions(payload: unknown, opts: FormatOpts): string | null {
169148 const entry = r as Record < string , unknown > ;
170149 const emojiObj = entry . emoji as Record < string , unknown > | undefined ;
171150 const emoji =
172- ( typeof emojiObj ?. raw === "string" && emojiObj . raw ) ||
173- ( typeof entry . name === "string" && entry . name ) ||
174- ( typeof entry . emoji === "string" && entry . emoji ) ||
175- "" ;
151+ firstNonemptyString ( emojiObj , "raw" ) || firstNonemptyString ( entry , "name" , "emoji" ) ;
176152 const count = typeof entry . count === "number" ? String ( entry . count ) : "" ;
177153 const userList = Array . isArray ( entry . users )
178154 ? ( entry . users as unknown [ ] )
@@ -185,12 +161,7 @@ function renderReactions(payload: unknown, opts: FormatOpts): string | null {
185161 return "" ;
186162 }
187163 const user = u as Record < string , unknown > ;
188- return (
189- ( typeof user . tag === "string" && user . tag ) ||
190- ( typeof user . username === "string" && user . username ) ||
191- ( typeof user . id === "string" && user . id ) ||
192- ""
193- ) ;
164+ return firstNonemptyString ( user , "tag" , "username" , "id" ) ;
194165 } )
195166 . filter ( Boolean )
196167 : [ ] ;
@@ -309,7 +280,7 @@ export function formatMessageCliText(
309280 }
310281
311282 const label = resolveChannelLabel ( result . channel ) ;
312- const msgId = extractMessageId ( result . payload ) ;
283+ const msgId = resolveMessageActionMessageId ( result . payload ) ;
313284 return [ ok ( `✅ Sent via ${ label } .${ msgId ? ` Message ID: ${ msgId } ` : "" } ` ) ] ;
314285 }
315286
@@ -350,7 +321,7 @@ export function formatMessageCliText(
350321 }
351322
352323 const label = resolveChannelLabel ( result . channel ) ;
353- const msgId = extractMessageId ( result . payload ) ;
324+ const msgId = resolveMessageActionMessageId ( result . payload ) ;
354325 return [ ok ( `✅ Poll sent via ${ label } .${ msgId ? ` Message ID: ${ msgId } ` : "" } ` ) ] ;
355326 }
356327
0 commit comments