@@ -41,7 +41,7 @@ import {
41
41
} from '../yaml/index' ;
42
42
43
43
import type { AbstractInterface } from '@/device' ;
44
- import type { AgentOpt } from '@/types' ;
44
+ import type { AgentOpt , CacheConfig } from '@/types' ;
45
45
import {
46
46
MIDSCENE_CACHE ,
47
47
ModelConfigManager ,
@@ -81,6 +81,17 @@ const defaultInsightExtractOption: InsightExtractOption = {
81
81
screenshotIncluded : true ,
82
82
} ;
83
83
84
+ type CacheStrategy = NonNullable < CacheConfig [ 'strategy' ] > ;
85
+
86
+ const CACHE_STRATEGIES : readonly CacheStrategy [ ] = [ 'read-only' , 'read-write' ] ;
87
+
88
+ const isValidCacheStrategy = ( strategy : string ) : strategy is CacheStrategy =>
89
+ CACHE_STRATEGIES . some ( ( value ) => value === strategy ) ;
90
+
91
+ const CACHE_STRATEGY_VALUES = CACHE_STRATEGIES . map (
92
+ ( value ) => `"${ value } "` ,
93
+ ) . join ( ', ' ) ;
94
+
84
95
export class Agent <
85
96
InterfaceType extends AbstractInterface = AbstractInterface ,
86
97
> {
@@ -1079,9 +1090,26 @@ export class Agent<
1079
1090
) ;
1080
1091
}
1081
1092
const id = config . id ;
1082
- // Default strategy is 'read-write'
1083
- const strategy = config . strategy ?? 'read-write' ;
1084
- const isReadOnly = strategy === 'read-only' ;
1093
+ const rawStrategy = config . strategy as unknown ;
1094
+ let strategyValue : string ;
1095
+
1096
+ if ( rawStrategy === undefined ) {
1097
+ strategyValue = 'read-write' ;
1098
+ } else if ( typeof rawStrategy === 'string' ) {
1099
+ strategyValue = rawStrategy ;
1100
+ } else {
1101
+ throw new Error (
1102
+ `cache.strategy must be a string when provided, but received type ${ typeof rawStrategy } ` ,
1103
+ ) ;
1104
+ }
1105
+
1106
+ if ( ! isValidCacheStrategy ( strategyValue ) ) {
1107
+ throw new Error (
1108
+ `cache.strategy must be one of ${ CACHE_STRATEGY_VALUES } , but received "${ strategyValue } "` ,
1109
+ ) ;
1110
+ }
1111
+
1112
+ const isReadOnly = strategyValue === 'read-only' ;
1085
1113
1086
1114
return {
1087
1115
id,
0 commit comments