@@ -12,12 +12,24 @@ import {createEvent, Event} from '../events/event.js';
1212import { CallbackContext } from './callback_context.js' ;
1313import { InvocationContext } from './invocation_context.js' ;
1414
15- type SingleAgentCallback = ( context : CallbackContext ) =>
16- Promise < Content | undefined > | ( Content | undefined ) ;
15+ /**
16+ * A single callback function for an agent.
17+ */
18+ export type SingleAgentCallback = (
19+ context : CallbackContext ,
20+ ) => Promise < Content | undefined > | ( Content | undefined ) ;
1721
18- type BeforeAgentCallback = SingleAgentCallback | SingleAgentCallback [ ] ;
22+ /**
23+ * Type for before agent callbacks, which can be a single callback or
24+ * an array of callbacks.
25+ */
26+ export type BeforeAgentCallback = SingleAgentCallback | SingleAgentCallback [ ] ;
1927
20- type AfterAgentCallback = SingleAgentCallback | SingleAgentCallback [ ] ;
28+ /**
29+ * Type for after agent callbacks, which can be a single callback or
30+ * an array of callbacks.
31+ */
32+ export type AfterAgentCallback = SingleAgentCallback | SingleAgentCallback [ ] ;
2133
2234/**
2335 * The config of a base agent.
@@ -43,9 +55,12 @@ const BASE_AGENT_SIGNATURE_SYMBOL = Symbol.for('google.adk.baseAgent');
4355 * @returns True if the object is an instance of BaseAgent, false otherwise.
4456 */
4557export function isBaseAgent ( obj : unknown ) : obj is BaseAgent {
46- return typeof obj === 'object' && obj !== null &&
47- BASE_AGENT_SIGNATURE_SYMBOL in obj &&
48- obj [ BASE_AGENT_SIGNATURE_SYMBOL ] === true ;
58+ return (
59+ typeof obj === 'object' &&
60+ obj !== null &&
61+ BASE_AGENT_SIGNATURE_SYMBOL in obj &&
62+ obj [ BASE_AGENT_SIGNATURE_SYMBOL ] === true
63+ ) ;
4964}
5065
5166/**
@@ -55,7 +70,7 @@ export abstract class BaseAgent {
5570 /**
5671 * A unique symbol to identify ADK agent classes.
5772 */
58- readonly [ BASE_AGENT_SIGNATURE_SYMBOL ] = true ;
73+ readonly [ BASE_AGENT_SIGNATURE_SYMBOL ] = true ;
5974
6075 /**
6176 * The agent's name.
@@ -129,8 +144,9 @@ export abstract class BaseAgent {
129144 this . parentAgent = config . parentAgent ;
130145 this . subAgents = config . subAgents || [ ] ;
131146 this . rootAgent = getRootAgent ( this ) ;
132- this . beforeAgentCallback =
133- getCannonicalCallback ( config . beforeAgentCallback ) ;
147+ this . beforeAgentCallback = getCannonicalCallback (
148+ config . beforeAgentCallback ,
149+ ) ;
134150 this . afterAgentCallback = getCannonicalCallback ( config . afterAgentCallback ) ;
135151
136152 this . setParentAgentForSubAgents ( ) ;
@@ -143,16 +159,17 @@ export abstract class BaseAgent {
143159 * @yields The events generated by the agent.
144160 * @returns An AsyncGenerator that yields the events generated by the agent.
145161 */
146- async *
147- runAsync ( parentContext : InvocationContext ) :
148- AsyncGenerator < Event , void , void > {
149- const span = trace . getTracer ( 'gcp.vertex.agent' )
150- . startSpan ( `agent_run [${ this . name } ]` ) ;
162+ async * runAsync (
163+ parentContext : InvocationContext ,
164+ ) : AsyncGenerator < Event , void , void > {
165+ const span = trace
166+ . getTracer ( 'gcp.vertex.agent' )
167+ . startSpan ( `agent_run [${ this . name } ]` ) ;
151168 try {
152169 const context = this . createInvocationContext ( parentContext ) ;
153170
154171 const beforeAgentCallbackEvent =
155- await this . handleBeforeAgentCallback ( context ) ;
172+ await this . handleBeforeAgentCallback ( context ) ;
156173 if ( beforeAgentCallbackEvent ) {
157174 yield beforeAgentCallbackEvent ;
158175 }
@@ -170,7 +187,7 @@ export abstract class BaseAgent {
170187 }
171188
172189 const afterAgentCallbackEvent =
173- await this . handleAfterAgentCallback ( context ) ;
190+ await this . handleAfterAgentCallback ( context ) ;
174191 if ( afterAgentCallbackEvent ) {
175192 yield afterAgentCallbackEvent ;
176193 }
@@ -186,11 +203,12 @@ export abstract class BaseAgent {
186203 * @yields The events generated by the agent.
187204 * @returns An AsyncGenerator that yields the events generated by the agent.
188205 */
189- async *
190- runLive ( parentContext : InvocationContext ) :
191- AsyncGenerator < Event , void , void > {
192- const span = trace . getTracer ( 'gcp.vertex.agent' )
193- . startSpan ( `agent_run [${ this . name } ]` ) ;
206+ async * runLive (
207+ parentContext : InvocationContext ,
208+ ) : AsyncGenerator < Event , void , void > {
209+ const span = trace
210+ . getTracer ( 'gcp.vertex.agent' )
211+ . startSpan ( `agent_run [${ this . name } ]` ) ;
194212 try {
195213 // TODO(b/425992518): Implement live mode.
196214 throw new Error ( 'Live mode is not implemented yet.' ) ;
@@ -206,8 +224,9 @@ export abstract class BaseAgent {
206224 * @yields The events generated by the agent.
207225 * @returns An AsyncGenerator that yields the events generated by the agent.
208226 */
209- protected abstract runAsyncImpl ( context : InvocationContext ) :
210- AsyncGenerator < Event , void , void > ;
227+ protected abstract runAsyncImpl (
228+ context : InvocationContext ,
229+ ) : AsyncGenerator < Event , void , void > ;
211230
212231 /**
213232 * Core logic to run this agent via video/audio-based conversation.
@@ -216,16 +235,17 @@ export abstract class BaseAgent {
216235 * @yields The events generated by the agent.
217236 * @returns An AsyncGenerator that yields the events generated by the agent.
218237 */
219- protected abstract runLiveImpl ( context : InvocationContext ) :
220- AsyncGenerator < Event , void , void > ;
238+ protected abstract runLiveImpl (
239+ context : InvocationContext ,
240+ ) : AsyncGenerator < Event , void , void > ;
221241
222242 /**
223243 * Finds the agent with the given name in this agent and its descendants.
224244 *
225245 * @param name The name of the agent to find.
226246 * @return The agent with the given name, or undefined if not found.
227247 */
228- findAgent ( name : string ) : BaseAgent | undefined {
248+ findAgent ( name : string ) : BaseAgent | undefined {
229249 if ( this . name === name ) {
230250 return this ;
231251 }
@@ -239,7 +259,7 @@ export abstract class BaseAgent {
239259 * @param name The name of the agent to find.
240260 * @return The agent with the given name, or undefined if not found.
241261 */
242- findSubAgent ( name : string ) : BaseAgent | undefined {
262+ findSubAgent ( name : string ) : BaseAgent | undefined {
243263 for ( const subAgent of this . subAgents ) {
244264 const result = subAgent . findAgent ( name ) ;
245265 if ( result ) {
@@ -256,8 +276,9 @@ export abstract class BaseAgent {
256276 * @param parentContext The invocation context of the parent agent.
257277 * @return The invocation context for this agent.
258278 */
259- protected createInvocationContext ( parentContext : InvocationContext ) :
260- InvocationContext {
279+ protected createInvocationContext (
280+ parentContext : InvocationContext ,
281+ ) : InvocationContext {
261282 return new InvocationContext ( {
262283 ...parentContext ,
263284 agent : this ,
@@ -272,7 +293,8 @@ export abstract class BaseAgent {
272293 * generated.
273294 */
274295 protected async handleBeforeAgentCallback (
275- invocationContext : InvocationContext ) : Promise < Event | undefined > {
296+ invocationContext : InvocationContext ,
297+ ) : Promise < Event | undefined > {
276298 if ( this . beforeAgentCallback . length === 0 ) {
277299 return undefined ;
278300 }
@@ -314,7 +336,8 @@ export abstract class BaseAgent {
314336 * generated.
315337 */
316338 protected async handleAfterAgentCallback (
317- invocationContext : InvocationContext ) : Promise < Event | undefined > {
339+ invocationContext : InvocationContext ,
340+ ) : Promise < Event | undefined > {
318341 if ( this . afterAgentCallback . length === 0 ) {
319342 return undefined ;
320343 }
@@ -349,9 +372,13 @@ export abstract class BaseAgent {
349372 private setParentAgentForSubAgents ( ) : void {
350373 for ( const subAgent of this . subAgents ) {
351374 if ( subAgent . parentAgent ) {
352- throw new Error ( `Agent "${
353- subAgent . name } " already has a parent agent, current parent: "${
354- subAgent . parentAgent . name } ", trying to add: "${ this . name } "`) ;
375+ throw new Error (
376+ `Agent "${
377+ subAgent . name
378+ } " already has a parent agent, current parent: "${
379+ subAgent . parentAgent . name
380+ } ", trying to add: "${ this . name } "`,
381+ ) ;
355382 }
356383
357384 subAgent . parentAgent = this ;
@@ -367,13 +394,17 @@ export abstract class BaseAgent {
367394 */
368395function validateAgentName ( name : string ) : string {
369396 if ( ! isIdentifier ( name ) ) {
370- throw new Error ( `Found invalid agent name: "${
371- name } ". Agent name must be a valid identifier. It should start with a letter (a-z, A-Z) or an underscore (_), and can only contain letters, digits (0-9), and underscores.`) ;
397+ throw new Error (
398+ `Found invalid agent name: "${
399+ name
400+ } ". Agent name must be a valid identifier. It should start with a letter (a-z, A-Z) or an underscore (_), and can only contain letters, digits (0-9), and underscores.`,
401+ ) ;
372402 }
373403
374404 if ( name === 'user' ) {
375405 throw new Error (
376- `Agent name cannot be 'user'. 'user' is reserved for end-user's input.` ) ;
406+ `Agent name cannot be 'user'. 'user' is reserved for end-user's input.` ,
407+ ) ;
377408 }
378409
379410 return name ;
@@ -410,10 +441,10 @@ function getRootAgent(rootAgent: BaseAgent): BaseAgent {
410441 * callback from.
411442 * @return The canonical callback.
412443 */
413- export function getCannonicalCallback < T > ( callbacks ?: T | T [ ] ) : T [ ] {
444+ export function getCannonicalCallback < T > ( callbacks ?: T | T [ ] ) : T [ ] {
414445 if ( ! callbacks ) {
415446 return [ ] ;
416447 }
417448
418449 return Array . isArray ( callbacks ) ? callbacks : [ callbacks ] ;
419- }
450+ }
0 commit comments