1+ import { isRecord } from "@openclaw/normalization-core/record-coerce" ;
12import { resolveStorePath } from "../../config/sessions/paths.js" ;
23import {
34 claimSessionSkillCaptureSignals ,
@@ -14,6 +15,7 @@ import { KeyedAsyncQueue } from "../../plugin-sdk/keyed-async-queue.js";
1415import { readWorkspaceSkillFile } from "../lifecycle/workspace-skill-write.js" ;
1516import { autoApplySkillProposal } from "../workshop/auto-apply.js" ;
1617import { resolveSkillWorkshopConfig } from "../workshop/config.js" ;
18+ import { selectCurrentSkillTurnMessages } from "../workshop/experience-review-prompt.js" ;
1719import { stripProposalFrontmatterForSkill } from "../workshop/frontmatter.js" ;
1820import {
1921 inspectSkillProposal ,
@@ -91,51 +93,41 @@ function readToolCallAction(value: unknown): string | undefined {
9193 return undefined ;
9294 }
9395 }
94- if ( ! input || typeof input !== "object" || Array . isArray ( input ) ) {
96+ if ( ! isRecord ( input ) ) {
9597 return undefined ;
9698 }
97- const action = ( input as { action ?: unknown } ) . action ;
99+ const action = input . action ;
98100 return typeof action === "string" ? action . trim ( ) . toLowerCase ( ) : undefined ;
99101}
100102
101- function isSkillWorkshopMutationBlock ( value : unknown ) : boolean {
102- if ( ! value || typeof value !== "object" || Array . isArray ( value ) ) {
103+ function isSkillWorkshopMutationBlock ( value : unknown ) : value is Record < string , unknown > {
104+ if ( ! isRecord ( value ) ) {
103105 return false ;
104106 }
105- const block = value as {
106- type ?: unknown ;
107- name ?: unknown ;
108- arguments ?: unknown ;
109- input ?: unknown ;
110- args ?: unknown ;
111- } ;
112- if ( ! TOOL_CALL_BLOCK_TYPES . has ( String ( block . type ) ) ) {
107+ if ( ! TOOL_CALL_BLOCK_TYPES . has ( String ( value . type ) ) ) {
113108 return false ;
114109 }
115- if ( typeof block . name !== "string" || block . name . trim ( ) . toLowerCase ( ) !== "skill_workshop" ) {
110+ if ( typeof value . name !== "string" || value . name . trim ( ) . toLowerCase ( ) !== "skill_workshop" ) {
116111 return false ;
117112 }
118- const action = readToolCallAction ( block . arguments ?? block . input ?? block . args ) ;
113+ const action = readToolCallAction ( value . arguments ?? value . input ?? value . args ) ;
119114 return action ? SKILL_WORKSHOP_MUTATING_ACTIONS . has ( action ) : false ;
120115}
121116
122117function hasUnfailedSkillWorkshopMutationCall ( messages : readonly unknown [ ] ) : boolean {
123118 const callIds = new Set < string > ( ) ;
124119 let hasUnidentifiedCall = false ;
125120 for ( const message of messages ) {
126- if ( ! message || typeof message !== "object" || Array . isArray ( message ) ) {
121+ if ( ! isRecord ( message ) ) {
127122 continue ;
128123 }
129- const content = ( message as { content ?: unknown } ) . content ;
124+ const content = message . content ;
130125 const blocks = Array . isArray ( content ) ? content : [ content ] ;
131126 for ( const block of blocks ) {
132127 if ( ! isSkillWorkshopMutationBlock ( block ) ) {
133128 continue ;
134129 }
135- const id =
136- block && typeof block === "object" && ! Array . isArray ( block )
137- ? ( block as { id ?: unknown } ) . id
138- : undefined ;
130+ const id = block . id ;
139131 if ( typeof id === "string" && id ) {
140132 callIds . add ( id ) ;
141133 } else {
@@ -147,36 +139,20 @@ function hasUnfailedSkillWorkshopMutationCall(messages: readonly unknown[]): boo
147139 return true ;
148140 }
149141 for ( const message of messages ) {
150- if ( ! message || typeof message !== "object" || Array . isArray ( message ) ) {
142+ if ( ! isRecord ( message ) ) {
151143 continue ;
152144 }
153- const result = message as { role ?: unknown ; toolCallId ?: unknown ; isError ?: unknown } ;
154145 if (
155- result . role === "toolResult" &&
156- result . isError === true &&
157- typeof result . toolCallId === "string"
146+ message . role === "toolResult" &&
147+ message . isError === true &&
148+ typeof message . toolCallId === "string"
158149 ) {
159- callIds . delete ( result . toolCallId ) ;
150+ callIds . delete ( message . toolCallId ) ;
160151 }
161152 }
162153 return callIds . size > 0 ;
163154}
164155
165- function currentTurnMessages ( messages : readonly unknown [ ] ) : readonly unknown [ ] {
166- for ( let index = messages . length - 1 ; index >= 0 ; index -= 1 ) {
167- const message = messages [ index ] ;
168- if (
169- message &&
170- typeof message === "object" &&
171- ! Array . isArray ( message ) &&
172- ( message as { role ?: unknown } ) . role === "user"
173- ) {
174- return messages . slice ( index ) ;
175- }
176- }
177- return messages ;
178- }
179-
180156function fingerprintInstructions ( instructions : readonly string [ ] ) : string {
181157 const normalized = instructions
182158 . map ( ( instruction ) => compactWhitespace ( instruction ) . toLowerCase ( ) )
@@ -239,7 +215,7 @@ export async function runSkillResearchAutoCapture(params: {
239215 // Proposals are workspace-scoped, so different sessions must not inspect and revise the same
240216 // pending draft concurrently from stale content.
241217 await skillCaptureQueue . enqueue ( workspaceDir , async ( ) => {
242- const turnMessages = currentTurnMessages ( params . event . messages ) ;
218+ const turnMessages = selectCurrentSkillTurnMessages ( params . event . messages ) ;
243219 if ( hasUnfailedSkillWorkshopMutationCall ( turnMessages ) ) {
244220 const signalHashes = extractDurableInstructions ( [ ...turnMessages ] ) . map ( ( instruction ) =>
245221 fingerprintInstructions ( [ instruction ] ) ,
0 commit comments