11import { asString } from "./threadNormalize" ;
22
3+ const SIDEBAR_HIDDEN_SUBAGENT_KINDS = new Set ( [ "memory_consolidation" ] ) ;
4+
35function asRecord ( value : unknown ) : Record < string , unknown > | null {
46 if ( ! value || typeof value !== "object" ) {
57 return null ;
68 }
79 return value as Record < string , unknown > ;
810}
911
12+ function normalizeSubagentKind ( value : string ) : string {
13+ const normalized = value
14+ . trim ( )
15+ . toLowerCase ( )
16+ . replace ( / [ \s - ] / g, "_" ) ;
17+ if ( normalized . startsWith ( "subagent_" ) ) {
18+ return normalized . slice ( "subagent_" . length ) ;
19+ }
20+ if ( normalized . startsWith ( "sub_agent_" ) ) {
21+ return normalized . slice ( "sub_agent_" . length ) ;
22+ }
23+ return normalized ;
24+ }
25+
26+ function getSubagentKind ( source : unknown ) : string | null {
27+ if ( typeof source === "string" ) {
28+ const normalized = normalizeSubagentKind ( source ) ;
29+ return normalized || null ;
30+ }
31+
32+ const sourceRecord = asRecord ( source ) ;
33+ if ( ! sourceRecord ) {
34+ return null ;
35+ }
36+
37+ const subAgentRaw =
38+ sourceRecord . subAgent ?? sourceRecord . sub_agent ?? sourceRecord . subagent ;
39+ if ( typeof subAgentRaw === "string" ) {
40+ const normalized = normalizeSubagentKind ( subAgentRaw ) ;
41+ return normalized || null ;
42+ }
43+
44+ const subAgentRecord = asRecord ( subAgentRaw ) ;
45+ if ( ! subAgentRecord ) {
46+ return null ;
47+ }
48+
49+ const explicitKind = asString (
50+ subAgentRecord . kind ??
51+ subAgentRecord . type ??
52+ subAgentRecord . name ??
53+ subAgentRecord . id ,
54+ ) ;
55+ if ( explicitKind ) {
56+ const normalized = normalizeSubagentKind ( explicitKind ) ;
57+ return normalized || null ;
58+ }
59+
60+ const candidateKeys = Object . keys ( subAgentRecord ) . filter (
61+ ( key ) => key !== "thread_spawn" && key !== "threadSpawn" ,
62+ ) ;
63+ if ( candidateKeys . length !== 1 ) {
64+ return null ;
65+ }
66+ const normalized = normalizeSubagentKind ( candidateKeys [ 0 ] ?? "" ) ;
67+ return normalized || null ;
68+ }
69+
1070export function isSubagentThreadSource ( source : unknown ) : boolean {
1171 if ( typeof source === "string" ) {
1272 const normalized = source . trim ( ) . toLowerCase ( ) ;
@@ -29,6 +89,14 @@ export function isSubagentThreadSource(source: unknown): boolean {
2989 return typeof subAgent === "object" ;
3090}
3191
92+ export function shouldHideSubagentThreadFromSidebar ( source : unknown ) : boolean {
93+ const subagentKind = getSubagentKind ( source ) ;
94+ if ( ! subagentKind ) {
95+ return false ;
96+ }
97+ return SIDEBAR_HIDDEN_SUBAGENT_KINDS . has ( subagentKind ) ;
98+ }
99+
32100export function getParentThreadIdFromSource ( source : unknown ) : string | null {
33101 const sourceRecord = asRecord ( source ) ;
34102 if ( ! sourceRecord ) {
0 commit comments