@@ -127,6 +127,21 @@ export const enum AgentType {
127127
128128const MAX_STEP = 10 ;
129129
130+ export abstract class ConversationContext < T > {
131+ abstract getOrigin ( ) : string ;
132+ abstract getItem ( ) : T ;
133+ isOriginAllowed ( agentOrigin : string | undefined ) : boolean {
134+ if ( ! agentOrigin ) {
135+ return true ;
136+ }
137+ // Currently does not handle opaque origins because they
138+ // are not available to DevTools, instead checks
139+ // that serialization of the origin is the same
140+ // https://html.spec.whatwg.org/#ascii-serialisation-of-an-origin.
141+ return this . getOrigin ( ) === agentOrigin ;
142+ }
143+ }
144+
130145export abstract class AiAgent < T > {
131146 static validTemperature ( temperature : number | undefined ) : number | undefined {
132147 return typeof temperature === 'number' && temperature >= 0 ? temperature : undefined ;
@@ -136,11 +151,12 @@ export abstract class AiAgent<T> {
136151 readonly #sessionId: string = crypto . randomUUID ( ) ;
137152 #aidaClient: Host . AidaClient . AidaClient ;
138153 #serverSideLoggingEnabled: boolean ;
154+ #origin?: string ;
139155 abstract readonly preamble : string ;
140156 abstract readonly options : AidaRequestOptions ;
141157 abstract readonly clientFeature : Host . AidaClient . ClientFeature ;
142158 abstract readonly userTier : string | undefined ;
143- abstract handleContextDetails ( select : T | null ) : AsyncGenerator < ContextResponse , void , void > ;
159+ abstract handleContextDetails ( select : ConversationContext < T > | null ) : AsyncGenerator < ContextResponse , void , void > ;
144160
145161 /**
146162 * Mapping between the unique request id and
@@ -165,6 +181,10 @@ export abstract class AiAgent<T> {
165181 return this . #history. size <= 0 ;
166182 }
167183
184+ get origin ( ) : string | undefined {
185+ return this . #origin;
186+ }
187+
168188 get title ( ) : string | undefined {
169189 return [ ...this . #history. values ( ) ]
170190 . flat ( )
@@ -248,7 +268,7 @@ export abstract class AiAgent<T> {
248268 throw new Error ( 'Unexpected action found' ) ;
249269 }
250270
251- async enhanceQuery ( query : string , selected : T | null ) : Promise < string > ;
271+ async enhanceQuery ( query : string , selected : ConversationContext < T > | null ) : Promise < string > ;
252272 async enhanceQuery ( query : string ) : Promise < string > {
253273 return query ;
254274 }
@@ -359,8 +379,12 @@ STOP`;
359379
360380 #runId = 0 ;
361381 async * run ( query : string , options : {
362- signal ?: AbortSignal , selected : T | null ,
382+ signal ?: AbortSignal , selected : ConversationContext < T > | null ,
363383 } ) : AsyncGenerator < ResponseData , void , void > {
384+ // First context set on the agent determines its origin from now on.
385+ if ( options . selected && this . #origin === undefined && options . selected ) {
386+ this . #origin = options . selected . getOrigin ( ) ;
387+ }
364388 const id = this . #runId++ ;
365389
366390 const response = {
0 commit comments