@@ -6,6 +6,11 @@ import type {
66 OpenKeyedStoreOptions ,
77 PluginStateKeyedStore ,
88} from "openclaw/plugin-sdk/plugin-state-runtime" ;
9+ import {
10+ asNullableRecord ,
11+ normalizeOptionalString ,
12+ normalizeUniqueTrimmedStringList ,
13+ } from "openclaw/plugin-sdk/string-coerce-runtime" ;
914import pMap , { pMapSkip } from "p-map" ;
1015import { walkMemoryWikiDirectory } from "./bounded-walk.js" ;
1116
@@ -113,13 +118,6 @@ function cloneImportRunRecord(record: ChatGptImportRunRecord): ChatGptImportRunR
113118 } ;
114119}
115120
116- function asRecord ( value : unknown ) : Record < string , unknown > | null {
117- if ( ! value || typeof value !== "object" || Array . isArray ( value ) ) {
118- return null ;
119- }
120- return value as Record < string , unknown > ;
121- }
122-
123121function normalizeImportRunEntries ( value : unknown ) : ChatGptImportRunEntry [ ] {
124122 if ( ! Array . isArray ( value ) ) {
125123 return [ ] ;
@@ -129,23 +127,17 @@ function normalizeImportRunEntries(value: unknown): ChatGptImportRunEntry[] {
129127 const entryPath = raw . trim ( ) ;
130128 return entryPath ? [ { path : entryPath } ] : [ ] ;
131129 }
132- const entry = asRecord ( raw ) ;
130+ const entry = asNullableRecord ( raw ) ;
133131 if ( ! entry ) {
134132 return [ ] ;
135133 }
136134 const entryPath = typeof entry . path === "string" ? entry . path . trim ( ) : "" ;
137135 if ( ! entryPath ) {
138136 return [ ] ;
139137 }
140- const snapshotPath =
141- typeof entry . snapshotPath === "string" && entry . snapshotPath . trim ( )
142- ? entry . snapshotPath . trim ( )
143- : undefined ;
144- const contentHash =
145- typeof entry . contentHash === "string" && entry . contentHash . trim ( )
146- ? entry . contentHash . trim ( )
147- : undefined ;
148- const recoveryPaths = normalizeStringArray ( entry . recoveryPaths ) ;
138+ const snapshotPath = normalizeOptionalString ( entry . snapshotPath ) ;
139+ const contentHash = normalizeOptionalString ( entry . contentHash ) ;
140+ const recoveryPaths = normalizeUniqueTrimmedStringList ( entry . recoveryPaths ) ;
149141 return [
150142 {
151143 path : entryPath ,
@@ -157,33 +149,19 @@ function normalizeImportRunEntries(value: unknown): ChatGptImportRunEntry[] {
157149 } ) ;
158150}
159151
160- function normalizeStringArray ( value : unknown ) : string [ ] {
161- if ( ! Array . isArray ( value ) ) {
162- return [ ] ;
163- }
164- return [
165- ...new Set (
166- value
167- . filter ( ( entry ) : entry is string => typeof entry === "string" )
168- . map ( ( entry ) => entry . trim ( ) )
169- . filter ( Boolean ) ,
170- ) ,
171- ] ;
172- }
173-
174152function asNonNegativeInteger ( value : unknown ) : number {
175153 return typeof value === "number" && Number . isFinite ( value ) ? Math . max ( 0 , Math . floor ( value ) ) : 0 ;
176154}
177155
178156function normalizeMemoryWikiImportRunRecord ( raw : unknown ) : ChatGptImportRunRecord | null {
179- const record = asRecord ( raw ) ;
157+ const record = asNullableRecord ( raw ) ;
180158 if ( ! record ) {
181159 return null ;
182160 }
183- const runId = typeof record . runId === "string" ? record . runId . trim ( ) : "" ;
184- const exportPath = typeof record . exportPath === "string" ? record . exportPath . trim ( ) : "" ;
185- const sourcePath = typeof record . sourcePath === "string" ? record . sourcePath . trim ( ) : "" ;
186- const appliedAt = typeof record . appliedAt === "string" ? record . appliedAt . trim ( ) : "" ;
161+ const runId = normalizeOptionalString ( record . runId ) ?? "" ;
162+ const exportPath = normalizeOptionalString ( record . exportPath ) ?? "" ;
163+ const sourcePath = normalizeOptionalString ( record . sourcePath ) ?? "" ;
164+ const appliedAt = normalizeOptionalString ( record . appliedAt ) ?? "" ;
187165 if (
188166 record . version !== 1 ||
189167 record . importType !== "chatgpt" ||
@@ -194,19 +172,9 @@ function normalizeMemoryWikiImportRunRecord(raw: unknown): ChatGptImportRunRecor
194172 ) {
195173 return null ;
196174 }
197- const rolledBackAt =
198- typeof record . rolledBackAt === "string" && record . rolledBackAt . trim ( )
199- ? record . rolledBackAt . trim ( )
200- : undefined ;
201- const rollbackStartedAt =
202- typeof record . rollbackStartedAt === "string" && record . rollbackStartedAt . trim ( )
203- ? record . rollbackStartedAt . trim ( )
204- : undefined ;
205- const rollbackTargetsFinalizedAt =
206- typeof record . rollbackTargetsFinalizedAt === "string" &&
207- record . rollbackTargetsFinalizedAt . trim ( )
208- ? record . rollbackTargetsFinalizedAt . trim ( )
209- : undefined ;
175+ const rolledBackAt = normalizeOptionalString ( record . rolledBackAt ) ;
176+ const rollbackStartedAt = normalizeOptionalString ( record . rollbackStartedAt ) ;
177+ const rollbackTargetsFinalizedAt = normalizeOptionalString ( record . rollbackTargetsFinalizedAt ) ;
210178 return {
211179 version : 1 ,
212180 runId,
@@ -227,7 +195,7 @@ function normalizeMemoryWikiImportRunRecord(raw: unknown): ChatGptImportRunRecor
227195}
228196
229197function normalizeMetaRecord ( raw : unknown ) : MemoryWikiImportRunMetaStateRecord | null {
230- const record = asRecord ( raw ) ;
198+ const record = asNullableRecord ( raw ) ;
231199 if ( ! record || record . kind !== "meta" ) {
232200 return null ;
233201 }
@@ -247,7 +215,7 @@ function normalizeMetaRecord(raw: unknown): MemoryWikiImportRunMetaStateRecord |
247215}
248216
249217function normalizePathRecord ( raw : unknown ) : MemoryWikiImportRunPathStateRecord | null {
250- const record = asRecord ( raw ) ;
218+ const record = asNullableRecord ( raw ) ;
251219 if (
252220 ! record ||
253221 ( record . kind !== "created-path" && record . kind !== "updated-path" ) ||
@@ -259,15 +227,9 @@ function normalizePathRecord(raw: unknown): MemoryWikiImportRunPathStateRecord |
259227 ) {
260228 return null ;
261229 }
262- const snapshotPath =
263- typeof record . snapshotPath === "string" && record . snapshotPath . trim ( )
264- ? record . snapshotPath . trim ( )
265- : undefined ;
266- const contentHash =
267- typeof record . contentHash === "string" && record . contentHash . trim ( )
268- ? record . contentHash . trim ( )
269- : undefined ;
270- const recoveryPaths = normalizeStringArray ( record . recoveryPaths ) ;
230+ const snapshotPath = normalizeOptionalString ( record . snapshotPath ) ;
231+ const contentHash = normalizeOptionalString ( record . contentHash ) ;
232+ const recoveryPaths = normalizeUniqueTrimmedStringList ( record . recoveryPaths ) ;
271233 return {
272234 kind : record . kind ,
273235 vaultRootKey : record . vaultRootKey ,
@@ -545,7 +507,7 @@ export async function readLegacyMemoryWikiImportRunRecords(
545507 ? "include"
546508 : "skip" ,
547509 } ) . catch ( ( error : unknown ) => {
548- const code = asRecord ( error ) ?. code ;
510+ const code = asNullableRecord ( error ) ?. code ;
549511 if ( code === "ENOENT" ) {
550512 return [ ] ;
551513 }
0 commit comments