@@ -100,6 +100,11 @@ export class TaskCache {
100
100
101
101
private usedCacheItems : Map < string , Set < number > > = new Map ( ) ;
102
102
103
+ private currentCycleCacheState : Map <
104
+ string ,
105
+ { count : number ; usedIndices : number [ ] }
106
+ > = new Map ( ) ;
107
+
103
108
constructor ( page : WebPage , opts ?: { cacheId ?: string } ) {
104
109
this . midscenePkgInfo = getRunningPkgInfo ( ) ;
105
110
this . cacheId = replaceIllegalPathCharsAndSpace ( opts ?. cacheId || '' ) ;
@@ -116,7 +121,7 @@ export class TaskCache {
116
121
pkgName : this . midscenePkgInfo ?. name || '' ,
117
122
pkgVersion : this . midscenePkgInfo ?. version || '' ,
118
123
cacheId : this . cacheId ,
119
- aiTasks : [ ] ,
124
+ aiTasks : JSON . parse ( JSON . stringify ( this . cache . aiTasks || [ ] ) ) ,
120
125
} ;
121
126
}
122
127
@@ -126,30 +131,104 @@ export class TaskCache {
126
131
}
127
132
128
133
const usedIndices = this . usedCacheItems . get ( aiActionPrompt ) ! ;
129
- const currentCount = usedIndices . size ;
130
134
const { aiTasks = [ ] } = this . cache || { aiTasks : [ ] } ;
131
135
136
+ // 初始化或获取当前循环状态
137
+ if ( ! this . currentCycleCacheState . has ( aiActionPrompt ) ) {
138
+ this . currentCycleCacheState . set ( aiActionPrompt , {
139
+ count : 0 ,
140
+ usedIndices : [ ] ,
141
+ } ) ;
142
+ }
143
+
144
+ // 获取并更新计数
145
+ const cacheState = this . currentCycleCacheState . get ( aiActionPrompt ) ! ;
146
+ const currentCount = cacheState . count ;
147
+ cacheState . count ++ ;
148
+
149
+ debug ( 'current prompt [%s] count: %d' , aiActionPrompt , currentCount + 1 ) ;
150
+
132
151
let matchIndex = - 1 ;
133
152
let matchItem = null ;
134
153
154
+ // find unused cache item in order
135
155
for ( let i = 0 ; i < aiTasks . length ; i ++ ) {
136
156
const item = aiTasks [ i ] ;
137
157
if ( item . prompt === aiActionPrompt ) {
138
158
if ( ! usedIndices . has ( i ) && item . tasks && item . tasks . length > 0 ) {
139
159
matchIndex = i ;
140
160
matchItem = item ;
141
161
usedIndices . add ( i ) ;
142
- debug ( '找到未使用的缓存项 : %s, 索引 : %d' , aiActionPrompt , i ) ;
162
+ debug ( 'find unused cache item : %s, index : %d' , aiActionPrompt , i ) ;
143
163
break ;
144
164
}
145
165
}
146
166
}
147
167
148
- const newCacheGroup : AiTasks = [ ] ;
149
- this . newCache . aiTasks . push ( {
150
- prompt : aiActionPrompt ,
151
- tasks : newCacheGroup ,
152
- } ) ;
168
+ // get all cache groups with same prompt in new cache
169
+ const promptGroups = this . newCache . aiTasks . filter (
170
+ ( item ) => item . prompt === aiActionPrompt ,
171
+ ) ;
172
+
173
+ const usedGroupIndices = cacheState . usedIndices ;
174
+ let targetGroupIndex : number ;
175
+ let newCacheGroup : AiTasks ;
176
+
177
+ // if current count is less than the number of existing cache groups, use the corresponding index cache group
178
+ if ( currentCount < promptGroups . length ) {
179
+ // find the index of promptGroups[currentCount] in newCache.aiTasks
180
+ targetGroupIndex = this . newCache . aiTasks . findIndex (
181
+ ( item , index ) =>
182
+ item . prompt === aiActionPrompt &&
183
+ ! usedGroupIndices . includes ( index ) && // if the index is not used
184
+ usedGroupIndices . length === currentCount , // if the index is the index in current cycle, ensure the order of used cache groups
185
+ ) ;
186
+
187
+ if ( targetGroupIndex === - 1 ) {
188
+ debug (
189
+ 'no suitable cache group, create new cache group: prompt [%s], current count: %d' ,
190
+ aiActionPrompt ,
191
+ currentCount + 1 ,
192
+ ) ;
193
+ this . newCache . aiTasks . push ( {
194
+ prompt : aiActionPrompt ,
195
+ tasks : [ ] ,
196
+ } ) ;
197
+ targetGroupIndex = this . newCache . aiTasks . length - 1 ;
198
+ usedGroupIndices . push ( targetGroupIndex ) ;
199
+ newCacheGroup = this . newCache . aiTasks [ targetGroupIndex ] . tasks ;
200
+ debug (
201
+ 'create new cache group as fallback: prompt [%s], index: %d, current count: %d' ,
202
+ aiActionPrompt ,
203
+ targetGroupIndex ,
204
+ currentCount + 1 ,
205
+ ) ;
206
+ } else {
207
+ usedGroupIndices . push ( targetGroupIndex ) ;
208
+ newCacheGroup = this . newCache . aiTasks [ targetGroupIndex ] . tasks ;
209
+ debug (
210
+ 'use existing cache group: prompt [%s], index: %d, current count: %d' ,
211
+ aiActionPrompt ,
212
+ targetGroupIndex ,
213
+ currentCount + 1 ,
214
+ ) ;
215
+ }
216
+ } else {
217
+ // if current count is greater than the number of existing cache groups, create a new cache group
218
+ this . newCache . aiTasks . push ( {
219
+ prompt : aiActionPrompt ,
220
+ tasks : [ ] ,
221
+ } ) ;
222
+ targetGroupIndex = this . newCache . aiTasks . length - 1 ;
223
+ usedGroupIndices . push ( targetGroupIndex ) ;
224
+ newCacheGroup = this . newCache . aiTasks [ targetGroupIndex ] . tasks ;
225
+ debug (
226
+ 'create new cache group: prompt [%s], index: %d, current count: %d' ,
227
+ aiActionPrompt ,
228
+ targetGroupIndex ,
229
+ currentCount + 1 ,
230
+ ) ;
231
+ }
153
232
154
233
return {
155
234
matchCache : async < T extends 'plan' | 'locate' | 'ui-tars-plan' > (
@@ -194,10 +273,11 @@ export class TaskCache {
194
273
saveCache : ( cache : PlanTask | LocateTask | UITarsPlanTask ) => {
195
274
newCacheGroup . push ( cache ) ;
196
275
debug (
197
- 'saving cache to file, type: %s, cacheId: %s, index: %d' ,
276
+ 'save cache to file, type: %s, cacheId: %s, prompt index: %d, current count : %d' ,
198
277
cache . type ,
199
278
this . cacheId ,
200
- currentCount ,
279
+ targetGroupIndex ,
280
+ currentCount + 1 ,
201
281
) ;
202
282
this . writeCacheToFile ( ) ;
203
283
} ,
0 commit comments