11import { resolveModelAgentRuntimeMetadata } from "../agents/agent-runtime-metadata.js" ;
22import { DEFAULT_CONTEXT_TOKENS } from "../agents/defaults.js" ;
3+ import { resolveRuntimePolicySessionKey } from "../auto-reply/reply/runtime-policy-session-key.js" ;
4+ import { normalizeChatType } from "../channels/chat-type.js" ;
35import { getRuntimeConfig } from "../config/config.js" ;
46import { loadSessionStore , resolveSessionTotalTokens } from "../config/sessions.js" ;
57import type { SessionEntry } from "../config/sessions/types.js" ;
@@ -10,7 +12,10 @@ import { type RuntimeEnv, writeRuntimeJson } from "../runtime.js";
1012import { classifySessionKind , type SessionKind } from "../sessions/classify-session-kind.js" ;
1113import { isAcpSessionKey } from "../sessions/session-key-utils.js" ;
1214import { createLazyImportLoader } from "../shared/lazy-promise.js" ;
13- import { normalizeOptionalLowercaseString } from "../shared/string-coerce.js" ;
15+ import {
16+ normalizeOptionalLowercaseString ,
17+ normalizeOptionalString ,
18+ } from "../shared/string-coerce.js" ;
1419import { resolveAgentRuntimeLabel } from "../status/agent-runtime-label.js" ;
1520import { isRich , theme } from "../terminal/theme.js" ;
1621import { resolveSessionStoreTargetsOrExit } from "./session-store-targets.js" ;
@@ -220,6 +225,76 @@ function toJsonSessionRow(row: SessionRow): Omit<SessionRow, "runtimeLabel"> {
220225 return jsonRow ;
221226}
222227
228+ function stripChannelRecipientPrefix (
229+ value : string | undefined ,
230+ channel : string | undefined ,
231+ ) : string | undefined {
232+ const raw = normalizeOptionalString ( value ) ;
233+ const normalizedChannel = normalizeOptionalLowercaseString ( channel ) ;
234+ if ( ! raw || ! normalizedChannel ) {
235+ return raw ;
236+ }
237+ const prefix = `${ normalizedChannel } :` ;
238+ if ( ! raw . toLowerCase ( ) . startsWith ( prefix ) ) {
239+ return raw ;
240+ }
241+ const stripped = raw . slice ( prefix . length ) ;
242+ const topicMarkerIndex = stripped . toLowerCase ( ) . indexOf ( ":topic:" ) ;
243+ return topicMarkerIndex >= 0 ? stripped . slice ( 0 , topicMarkerIndex ) : stripped ;
244+ }
245+
246+ function resolveDisplayRuntimePolicySessionKey ( params : {
247+ cfg : OpenClawConfig ;
248+ key : string ;
249+ entry : SessionEntry ;
250+ } ) : string | undefined {
251+ const { cfg, entry, key } = params ;
252+ const origin = entry . origin ;
253+ const deliveryContext = entry . deliveryContext ;
254+ const chatType = normalizeChatType ( origin ?. chatType ?? entry . chatType ) ;
255+ if ( chatType !== "direct" ) {
256+ return undefined ;
257+ }
258+
259+ const channel = normalizeOptionalString (
260+ origin ?. provider ??
261+ deliveryContext ?. channel ??
262+ entry . lastChannel ??
263+ entry . channel ??
264+ origin ?. surface ,
265+ ) ;
266+ const to = normalizeOptionalString ( origin ?. to ?? deliveryContext ?. to ?? entry . lastTo ) ;
267+ const from = normalizeOptionalString ( origin ?. from ) ;
268+ const nativeDirectUserId = normalizeOptionalString ( origin ?. nativeDirectUserId ) ;
269+ const peerId =
270+ nativeDirectUserId ??
271+ stripChannelRecipientPrefix ( to , channel ) ??
272+ stripChannelRecipientPrefix ( from , channel ) ;
273+
274+ const runtimePolicySessionKey = resolveRuntimePolicySessionKey ( {
275+ cfg,
276+ sessionKey : key ,
277+ ctx : {
278+ SessionKey : key ,
279+ Provider : channel ,
280+ Surface : normalizeOptionalString ( origin ?. surface ) ,
281+ AccountId : normalizeOptionalString (
282+ origin ?. accountId ?? deliveryContext ?. accountId ?? entry . lastAccountId ,
283+ ) ,
284+ ChatType : chatType ,
285+ NativeDirectUserId : nativeDirectUserId ,
286+ SenderId : peerId ,
287+ OriginatingTo : to ,
288+ From : from ,
289+ To : to ,
290+ } ,
291+ } ) ;
292+
293+ return runtimePolicySessionKey && runtimePolicySessionKey !== key
294+ ? runtimePolicySessionKey
295+ : undefined ;
296+ }
297+
223298export async function sessionsCommand (
224299 opts : {
225300 json ?: boolean ;
@@ -303,6 +378,11 @@ export async function sessionsCommand(
303378 acpRuntime,
304379 agentRuntime,
305380 kind : classifySessionKind ( row . key , store [ row . key ] ) ,
381+ runtimePolicySessionKey : resolveDisplayRuntimePolicySessionKey ( {
382+ cfg,
383+ key : row . key ,
384+ entry,
385+ } ) ,
306386 runtimeLabel : resolveSessionRuntimeLabel ( {
307387 cfg,
308388 entry,
0 commit comments