@@ -24,7 +24,8 @@ import {
2424 PREDEFINED_VARIABLES ,
2525 DEFAULT_CONTEXT_CONFIG ,
2626 CONTEXT_STORE_VERSION ,
27- CONTEXT_UI_LABELS
27+ CONTEXT_UI_LABELS ,
28+ DEFAULT_CONTEXT_MODE
2829} from './constants' ;
2930
3031/**
@@ -108,6 +109,7 @@ export class ContextRepoImpl implements ContextRepo {
108109 const defaultContext : ContextPackage = {
109110 id : DEFAULT_CONTEXT_CONFIG . id ,
110111 title : DEFAULT_CONTEXT_CONFIG . title ,
112+ mode : DEFAULT_CONTEXT_MODE ,
111113 version : DEFAULT_CONTEXT_CONFIG . version ,
112114 createdAt : now ,
113115 updatedAt : now ,
@@ -134,12 +136,29 @@ export class ContextRepoImpl implements ContextRepo {
134136
135137 try {
136138 const doc = JSON . parse ( data ) as ContextStoreDoc ;
137-
139+
138140 // 基础验证
139141 if ( ! doc . currentId || ! doc . contexts || typeof doc . contexts !== 'object' ) {
140142 throw new Error ( 'Invalid document structure' ) ;
141143 }
142144
145+ // 迁移逻辑:为旧文档的上下文补写 mode 字段
146+ let migrated = false ;
147+ for ( const ctx of Object . values ( doc . contexts ) ) {
148+ if ( ! ctx . mode ) {
149+ ctx . mode = DEFAULT_CONTEXT_MODE ;
150+ migrated = true ;
151+ }
152+ }
153+
154+ // 如果有迁移,需要保存回存储
155+ if ( migrated ) {
156+ await this . storage . setItem ( CONTEXT_STORE_KEY , JSON . stringify ( doc ) ) ;
157+ if ( process . env . NODE_ENV === 'development' ) {
158+ console . log ( '[ContextRepo] Migrated contexts to add mode field' ) ;
159+ }
160+ }
161+
143162 // 确保currentId对应的上下文存在
144163 if ( ! doc . contexts [ doc . currentId ] ) {
145164 // 修复:选择第一个可用的上下文
@@ -178,6 +197,7 @@ export class ContextRepoImpl implements ContextRepo {
178197 const defaultContext : ContextPackage = {
179198 id : DEFAULT_CONTEXT_CONFIG . id ,
180199 title : DEFAULT_CONTEXT_CONFIG . title ,
200+ mode : DEFAULT_CONTEXT_MODE ,
181201 version : DEFAULT_CONTEXT_CONFIG . version ,
182202 createdAt : now ,
183203 updatedAt : now ,
@@ -253,13 +273,14 @@ export class ContextRepoImpl implements ContextRepo {
253273 return { ...context } ;
254274 }
255275
256- async create ( meta ?: { title ?: string } ) : Promise < string > {
276+ async create ( meta ?: { title ?: string ; mode ?: import ( './types' ) . ContextMode } ) : Promise < string > {
257277 const id = generateId ( ) ;
258278 const now = getCurrentISOTime ( ) ;
259-
279+
260280 const newContext : ContextPackage = {
261281 id,
262282 title : meta ?. title || `${ CONTEXT_UI_LABELS . DEFAULT_TITLE_TEMPLATE } ${ new Date ( ) . toLocaleDateString ( ) } ` ,
283+ mode : meta ?. mode || DEFAULT_CONTEXT_MODE ,
263284 version : DEFAULT_CONTEXT_CONFIG . version ,
264285 createdAt : now ,
265286 updatedAt : now ,
@@ -279,18 +300,19 @@ export class ContextRepoImpl implements ContextRepo {
279300 return id ;
280301 }
281302
282- async duplicate ( id : string ) : Promise < string > {
303+ async duplicate ( id : string , options ?: { mode ?: import ( './types' ) . ContextMode } ) : Promise < string > {
283304 const originalCtx = await this . get ( id ) ; // 会抛出错误如果不存在
284305 const newId = generateId ( ) ;
285306 const now = getCurrentISOTime ( ) ;
286307
287308 // 复制时也需要清理变量
288309 const [ sanitizedVariables ] = sanitizeVariables ( originalCtx . variables ) ;
289-
310+
290311 const newContext : ContextPackage = {
291312 ...originalCtx ,
292313 id : newId ,
293314 title : `${ originalCtx . title } ${ CONTEXT_UI_LABELS . DUPLICATE_SUFFIX } ` ,
315+ mode : options ?. mode || originalCtx . mode || DEFAULT_CONTEXT_MODE ,
294316 variables : sanitizedVariables ,
295317 createdAt : now ,
296318 updatedAt : now
@@ -327,6 +349,7 @@ export class ContextRepoImpl implements ContextRepo {
327349
328350 const contextToSave : ContextPackage = {
329351 ...ctx ,
352+ mode : ctx . mode || DEFAULT_CONTEXT_MODE ,
330353 variables : sanitizedVariables ,
331354 updatedAt : getCurrentISOTime ( )
332355 } ;
@@ -372,6 +395,7 @@ export class ContextRepoImpl implements ContextRepo {
372395
373396 // 合并安全的更新字段
374397 Object . assign ( context , safeUpdate , {
398+ mode : patch . mode ?? context . mode ?? DEFAULT_CONTEXT_MODE ,
375399 variables : sanitizedVariables || context . variables ,
376400 updatedAt : getMonotonicISO ( context . updatedAt )
377401 } ) ;
@@ -471,6 +495,7 @@ export class ContextRepoImpl implements ContextRepo {
471495
472496 const contextToImport : ContextPackage = {
473497 ...ctx ,
498+ mode : ctx . mode || DEFAULT_CONTEXT_MODE ,
474499 variables : sanitizedVariables ,
475500 updatedAt : now
476501 } ;
@@ -506,6 +531,7 @@ export class ContextRepoImpl implements ContextRepo {
506531 const contextToImport : ContextPackage = {
507532 ...ctx ,
508533 id : finalId ,
534+ mode : ctx . mode || DEFAULT_CONTEXT_MODE ,
509535 variables : sanitizedVariables ,
510536 updatedAt : now
511537 } ;
@@ -535,13 +561,15 @@ export class ContextRepoImpl implements ContextRepo {
535561
536562 doc . contexts [ ctx . id ] = {
537563 ...ctx ,
564+ mode : ctx . mode || existingCtx . mode || DEFAULT_CONTEXT_MODE ,
538565 variables : mergedVariables ,
539566 updatedAt : now
540567 } ;
541568 } else {
542569 // 新ID:直接添加
543570 doc . contexts [ ctx . id ] = {
544571 ...ctx ,
572+ mode : ctx . mode || DEFAULT_CONTEXT_MODE ,
545573 variables : sanitizedVariables ,
546574 updatedAt : now
547575 } ;
0 commit comments