@@ -156,6 +156,10 @@ export abstract class ConversationContext<T> {
156156 }
157157}
158158
159+ interface AgentFunctionDefinition extends Host . AidaClient . FunctionDeclaration {
160+ method : ( ...args : any [ ] ) => Record < string , unknown > ;
161+ }
162+
159163export abstract class AiAgent < T > {
160164 static validTemperature ( temperature : number | undefined ) : number | undefined {
161165 return typeof temperature === 'number' && temperature >= 0 ? temperature : undefined ;
@@ -171,6 +175,7 @@ export abstract class AiAgent<T> {
171175 abstract readonly clientFeature : Host . AidaClient . ClientFeature ;
172176 abstract readonly userTier : string | undefined ;
173177 abstract handleContextDetails ( select : ConversationContext < T > | null ) : AsyncGenerator < ContextResponse , void , void > ;
178+ functionDefinitions : AgentFunctionDefinition [ ] | undefined ;
174179 #generatedFromHistory = false ;
175180
176181 /**
@@ -231,6 +236,16 @@ export abstract class AiAgent<T> {
231236 return this . #generatedFromHistory;
232237 }
233238
239+ get functionDeclarations ( ) : Host . AidaClient . FunctionDeclaration [ ] | undefined {
240+ return this . functionDefinitions ?. map ( call => {
241+ return {
242+ name : call . name ,
243+ description : call . description ,
244+ parameters : call . parameters ,
245+ } satisfies Host . AidaClient . FunctionDeclaration ;
246+ } ) ;
247+ }
248+
234249 serialized ( ) : SerializedAgent {
235250 return {
236251 id : this . id ,
@@ -256,8 +271,17 @@ export abstract class AiAgent<T> {
256271 for await ( rawResponse of this . #aidaClient. fetch ( request , options ) ) {
257272 response = rawResponse . explanation ;
258273 rpcId = rawResponse . metadata . rpcGlobalId ?? rpcId ;
274+
275+ if ( rawResponse . functionCall ) {
276+ throw new Error ( 'Function calling not supported yet' ) ;
277+ }
278+
259279 const parsedResponse = this . parseResponse ( response ) ;
260- yield { rpcId, parsedResponse, completed : rawResponse . completed } ;
280+ yield {
281+ rpcId,
282+ parsedResponse,
283+ completed : rawResponse . completed ,
284+ } ;
261285 }
262286
263287 debugLog ( {
@@ -273,16 +297,22 @@ export abstract class AiAgent<T> {
273297 localStorage . setItem ( 'freestylerStructuredLog' , JSON . stringify ( this . #structuredLog) ) ;
274298 }
275299
276- buildRequest ( opts : BuildRequestOptions ) : Host . AidaClient . AidaRequest {
277- const currentMessage = { parts : [ { text : opts . text } ] , role : Host . AidaClient . Role . USER } ;
300+ buildRequest ( part : Host . AidaClient . Part ) : Host . AidaClient . AidaRequest {
301+ const currentMessage : Host . AidaClient . Content = {
302+ parts : [ part ] ,
303+ role : Host . AidaClient . Role . USER ,
304+ } ;
278305 const history = this . #chatHistoryForAida;
306+ const declarations = this . functionDeclarations ;
279307 const request : Host . AidaClient . AidaRequest = {
308+ client : Host . AidaClient . CLIENT_NAME ,
280309 // eslint-disable-next-line @typescript-eslint/naming-convention
281310 current_message : currentMessage ,
282311 preamble : this . preamble ,
283312 // eslint-disable-next-line @typescript-eslint/naming-convention
284313 historical_contexts : history . length ? history : undefined ,
285- client : Host . AidaClient . CLIENT_NAME ,
314+ // eslint-disable-next-line @typescript-eslint/naming-convention
315+ ...( declarations ? { function_declarations : declarations } : { } ) ,
286316 options : {
287317 temperature : AiAgent . validTemperature ( this . options . temperature ) ,
288318 // eslint-disable-next-line @typescript-eslint/naming-convention
@@ -364,14 +394,18 @@ STOP`;
364394 flushCurrentStep ( ) ;
365395 history . push ( {
366396 role : Host . AidaClient . Role . USER ,
367- parts : [ { text : data . query } ] ,
397+ parts : [ {
398+ text : data . query ,
399+ } ] ,
368400 } ) ;
369401 break ;
370402 }
371403 case ResponseType . ANSWER :
372404 history . push ( {
373405 role : Host . AidaClient . Role . MODEL ,
374- parts : [ { text : this . formatParsedAnswer ( { answer : data . text } ) } ] ,
406+ parts : [ {
407+ text : this . formatParsedAnswer ( { answer : data . text } ) ,
408+ } ] ,
375409 } ) ;
376410 break ;
377411 case ResponseType . TITLE :
0 commit comments