@@ -10,6 +10,11 @@ import {
1010} from "./projectManifestContract.js" ;
1111import { getProjectAdapter } from "./projectSystemAdapters.js" ;
1212import { cloneValue , safeString } from "./projectSystemValueUtils.js" ;
13+ import {
14+ buildProjectToolIntegration ,
15+ normalizeToolStateForProjectManifest ,
16+ unwrapToolStateForAdapter
17+ } from "./projectToolIntegration.js" ;
1318
1419function readStorage ( ) {
1520 try {
@@ -87,15 +92,23 @@ export function createProjectSystemController(options = {}) {
8792 currentManifest . workspace . lastOpenTool = toolId ;
8893 currentManifest . updatedAt = new Date ( ) . toISOString ( ) ;
8994 currentManifest . sharedReferences = captureSharedReferenceSnapshot ( ) ;
95+ currentManifest . tools = currentManifest . tools && typeof currentManifest . tools === "object"
96+ ? currentManifest . tools
97+ : { } ;
9098
9199 if ( toolAdapter . ready ) {
92- currentManifest . tools [ toolId ] = toolAdapter . captureState ( ) ;
100+ currentManifest . tools [ toolId ] = normalizeToolStateForProjectManifest (
101+ toolId ,
102+ toolAdapter . captureState ( )
103+ ) ;
93104 const adapterName = safeString ( toolAdapter . getProjectName ?. ( ) , "" ) ;
94105 if ( adapterName ) {
95106 currentManifest . name = adapterName ;
96107 }
97108 }
98109
110+ currentManifest . toolIntegration = buildProjectToolIntegration ( currentManifest . tools ) ;
111+
99112 return migrateProjectManifest ( currentManifest ) ;
100113 }
101114
@@ -156,7 +169,7 @@ export function createProjectSystemController(options = {}) {
156169 const manifest = ensureProjectManifest ( ) ;
157170 const toolState = manifest . tools ?. [ toolId ] ;
158171 if ( toolState ) {
159- toolAdapter . applyState ( cloneValue ( toolState ) ) ;
172+ toolAdapter . applyState ( cloneValue ( unwrapToolStateForAdapter ( toolId , toolState ) ) ) ;
160173 onStatus ( buildStatusSummary ( validateProjectManifest ( manifest ) ) ) ;
161174 }
162175 state . appliedInitialState = true ;
@@ -172,8 +185,10 @@ export function createProjectSystemController(options = {}) {
172185 toolId
173186 } ) ;
174187 if ( toolAdapter . ready ) {
175- nextManifest . tools [ toolId ] = toolAdapter . createDefaultState ( nextName ) ;
176- toolAdapter . applyState ( cloneValue ( nextManifest . tools [ toolId ] ) ) ;
188+ const defaultState = normalizeToolStateForProjectManifest ( toolId , toolAdapter . createDefaultState ( nextName ) ) ;
189+ nextManifest . tools [ toolId ] = defaultState ;
190+ nextManifest . toolIntegration = buildProjectToolIntegration ( nextManifest . tools ) ;
191+ toolAdapter . applyState ( cloneValue ( unwrapToolStateForAdapter ( toolId , defaultState ) ) ) ;
177192 }
178193 state . manifest = nextManifest ;
179194 state . appliedInitialState = true ;
@@ -192,9 +207,12 @@ export function createProjectSystemController(options = {}) {
192207 state . manifest = nextManifest ;
193208 const toolAdapter = adapter ( ) ;
194209 if ( toolAdapter . ready ) {
195- const nextToolState = nextManifest . tools ?. [ toolId ] || toolAdapter . createDefaultState ( nextManifest . name ) ;
196- toolAdapter . applyState ( cloneValue ( nextToolState ) ) ;
210+ const nextToolState = nextManifest . tools ?. [ toolId ]
211+ ? normalizeToolStateForProjectManifest ( toolId , nextManifest . tools [ toolId ] )
212+ : normalizeToolStateForProjectManifest ( toolId , toolAdapter . createDefaultState ( nextManifest . name ) ) ;
213+ toolAdapter . applyState ( cloneValue ( unwrapToolStateForAdapter ( toolId , nextToolState ) ) ) ;
197214 nextManifest . tools [ toolId ] = cloneValue ( nextToolState ) ;
215+ nextManifest . toolIntegration = buildProjectToolIntegration ( nextManifest . tools ) ;
198216 }
199217 state . appliedInitialState = true ;
200218 markSaved ( "open-project" ) ;
@@ -224,8 +242,10 @@ export function createProjectSystemController(options = {}) {
224242 toolId
225243 } ) ;
226244 if ( toolAdapter . ready ) {
227- nextManifest . tools [ toolId ] = toolAdapter . createDefaultState ( fallbackName ) ;
228- toolAdapter . applyState ( cloneValue ( nextManifest . tools [ toolId ] ) ) ;
245+ const defaultState = normalizeToolStateForProjectManifest ( toolId , toolAdapter . createDefaultState ( fallbackName ) ) ;
246+ nextManifest . tools [ toolId ] = defaultState ;
247+ nextManifest . toolIntegration = buildProjectToolIntegration ( nextManifest . tools ) ;
248+ toolAdapter . applyState ( cloneValue ( unwrapToolStateForAdapter ( toolId , defaultState ) ) ) ;
229249 }
230250 state . manifest = nextManifest ;
231251 state . appliedInitialState = true ;
0 commit comments