@@ -13,10 +13,12 @@ type WorkspaceSkillSymlinkWritePolicy = {
1313 allowWrites : boolean ;
1414 allowedTargetRealPaths : readonly string [ ] ;
1515} ;
16+ type WorkspaceSkillSupportFileWrite = { path : string ; content : string } ;
1617
17- type WorkspaceSkillSupportFileWrite = {
18- path : string ;
19- content : string ;
18+ type WorkspaceSkillWriteTargetParams = {
19+ workspaceDir : string ;
20+ filePath : string ;
21+ symlinkPolicy : WorkspaceSkillSymlinkWritePolicy ;
2022} ;
2123
2224type PreviousSupportFile = { path : string ; existed : boolean ; previousContent ?: string } ;
@@ -96,6 +98,12 @@ export async function readWorkspaceSupportFile(params: {
9698 return read . buffer . toString ( "utf8" ) ;
9799}
98100
101+ export async function assertWorkspaceSkillWriteTarget (
102+ params : WorkspaceSkillWriteTargetParams ,
103+ ) : Promise < void > {
104+ await resolveWorkspaceSkillWriteTarget ( params ) ;
105+ }
106+
99107export async function writeWorkspaceSkill ( params : {
100108 workspaceDir : string ;
101109 skillDir : string ;
@@ -106,7 +114,6 @@ export async function writeWorkspaceSkill(params: {
106114 symlinkPolicy : WorkspaceSkillSymlinkWritePolicy ;
107115} ) : Promise < void > {
108116 assertInsideWorkspace ( params . workspaceDir , params . skillDir , "skill directory" ) ;
109- assertInsideWorkspace ( params . workspaceDir , params . skillFile , "skill file" ) ;
110117 const supportFiles = normalizeSupportFiles ( params . supportFiles ?? [ ] ) ;
111118 const previousSupportFiles = await prepareWorkspaceSkillWrite ( {
112119 mode : params . mode ,
@@ -188,22 +195,19 @@ async function prepareWorkspaceSkillWrite(params: {
188195 filePath,
189196 symlinkPolicy : params . symlinkPolicy ,
190197 } ) ;
191- const previousContent = await readWorkspaceSupportFile ( {
192- skillDir : params . skillDir ,
193- relativePath : file . path ,
194- } ) ;
195- if ( params . mode === "create" && previousContent !== null ) {
196- throw new Error (
197- `Target support file already exists: ${ path . join ( params . skillDir , file . path ) } ` ,
198+ if ( params . mode === "update" ) {
199+ const previousContent = await readWorkspaceSupportFile ( {
200+ skillDir : params . skillDir ,
201+ relativePath : file . path ,
202+ } ) ;
203+ previousSupportFiles . push (
204+ previousContent === null
205+ ? { path : file . path , existed : false }
206+ : { path : file . path , existed : true , previousContent } ,
198207 ) ;
199208 }
200- previousSupportFiles . push (
201- previousContent === null
202- ? { path : file . path , existed : false }
203- : { path : file . path , existed : true , previousContent } ,
204- ) ;
205209 }
206- return params . mode === "update" ? previousSupportFiles : [ ] ;
210+ return previousSupportFiles ;
207211}
208212
209213async function writeWorkspaceFile ( params : {
@@ -268,11 +272,9 @@ async function restoreSupportFilesAfterFailedWrite(params: {
268272 ) ;
269273}
270274
271- async function resolveWorkspaceSkillWriteTarget ( params : {
272- workspaceDir : string ;
273- filePath : string ;
274- symlinkPolicy : WorkspaceSkillSymlinkWritePolicy ;
275- } ) : Promise < { rootDir : string ; relativePath : string } > {
275+ async function resolveWorkspaceSkillWriteTarget (
276+ params : WorkspaceSkillWriteTargetParams ,
277+ ) : Promise < { rootDir : string ; relativePath : string } > {
276278 assertInsideWorkspace ( params . workspaceDir , params . filePath , "skill file" ) ;
277279 const workspaceDir = path . resolve ( params . workspaceDir ) ;
278280 const filePath = path . resolve ( params . filePath ) ;
0 commit comments