11import { getToolHostEntryById } from "./toolHostManifest.js" ;
2+ import {
3+ removeToolHostSharedContextById ,
4+ writeToolHostSharedContext
5+ } from "./toolHostSharedContext.js" ;
26
37function normalizeToolId ( toolId ) {
48 return typeof toolId === "string" ? toolId . trim ( ) : "" ;
59}
610
7- function buildHostLaunchUrl ( toolEntry , config = { } ) {
11+ function buildHostLaunchUrl ( toolEntry , config = { } , hostContextId = "" ) {
812 const url = new URL ( toolEntry . launchPath , window . location . href ) ;
913 url . searchParams . set ( "hosted" , "1" ) ;
1014 url . searchParams . set ( "hostToolId" , toolEntry . id ) ;
15+ if ( hostContextId ) {
16+ url . searchParams . set ( "hostContextId" , hostContextId ) ;
17+ }
1118
1219 if ( config && typeof config === "object" ) {
1320 Object . entries ( config ) . forEach ( ( [ key , value ] ) => {
14- if ( value === undefined || value === null ) {
21+ if ( key === "state" || key === "sharedContext" ) {
22+ return ;
23+ }
24+ if ( value === undefined || value === null || typeof value === "object" ) {
1525 return ;
1626 }
1727 url . searchParams . set ( `hostConfig_${ key } ` , String ( value ) ) ;
@@ -92,6 +102,9 @@ export function createToolHostRuntime(options = {}) {
92102 previous . frame . removeAttribute ( "src" ) ;
93103 mountContainer . removeChild ( previous . frame ) ;
94104 }
105+ if ( previous . hostContextId ) {
106+ removeToolHostSharedContextById ( previous . hostContextId ) ;
107+ }
95108 onStatus ( `Unmounted ${ previous . tool . displayName } (${ reason } , destroy=${ destroyStatus } ).` ) ;
96109 onUnmounted ( previous . tool , reason , destroyStatus ) ;
97110 return true ;
@@ -118,7 +131,16 @@ export function createToolHostRuntime(options = {}) {
118131
119132 mountSequence += 1 ;
120133 const sequenceId = mountSequence ;
121- const sourceUrl = buildHostLaunchUrl ( toolEntry , config ) ;
134+ const sharedContext = config . sharedContext && typeof config . sharedContext === "object" ? config . sharedContext : { } ;
135+ const hostContext = writeToolHostSharedContext ( {
136+ toolId : toolEntry . id ,
137+ source : typeof config . source === "string" ? config . source : "" ,
138+ requestedAt : typeof config . requestedAt === "string" ? config . requestedAt : new Date ( ) . toISOString ( ) ,
139+ sharedContext,
140+ state : Object . prototype . hasOwnProperty . call ( config , "state" ) ? config . state : null
141+ } ) ;
142+ const hostContextId = hostContext ?. contextId || "" ;
143+ const sourceUrl = buildHostLaunchUrl ( toolEntry , config , hostContextId ) ;
122144 const frame = createHostFrame ( toolEntry , sourceUrl ) ;
123145 frame . addEventListener ( "load" , ( ) => {
124146 if ( ! currentMount || currentMount . mountSequence !== sequenceId ) {
@@ -141,7 +163,8 @@ export function createToolHostRuntime(options = {}) {
141163 frame,
142164 sourceUrl,
143165 mountedAt : new Date ( ) . toISOString ( ) ,
144- mountSequence : sequenceId
166+ mountSequence : sequenceId ,
167+ hostContextId
145168 } ;
146169
147170 onStatus ( `Mounting ${ toolEntry . displayName } ...` ) ;
0 commit comments