@@ -23,6 +23,7 @@ import { camelCaseToKebabCase } from "./client";
2323import { MCPClientManager } from "./mcp/client" ;
2424// import type { MCPClientConnection } from "./mcp/client-connection";
2525import { DurableObjectOAuthClientProvider } from "./mcp/do-oauth-client-provider" ;
26+ import { genericObservability , type Observability } from "./observability" ;
2627
2728export type { Connection , ConnectionContext , WSMessage } from "partyserver" ;
2829
@@ -316,6 +317,11 @@ export class Agent<Env, State = unknown> extends Server<Env> {
316317 hibernate : true , // default to hibernate
317318 } ;
318319
320+ /**
321+ * The observability implementation to use for the Agent
322+ */
323+ observability ?: Observability = genericObservability ;
324+
319325 /**
320326 * Execute SQL queries against the Agent's database
321327 * @template T Type of the returned rows
@@ -460,6 +466,23 @@ export class Agent<Env, State = unknown> extends Server<Env> {
460466
461467 // For regular methods, execute and send response
462468 const result = await methodFn . apply ( this , args ) ;
469+
470+ this . observability ?. emit (
471+ {
472+ displayMessage : `RPC call to ${ method } ` ,
473+ id : nanoid ( ) ,
474+ payload : {
475+ args,
476+ method,
477+ streaming : metadata ?. streaming ,
478+ success : true ,
479+ } ,
480+ timestamp : Date . now ( ) ,
481+ type : "rpc" ,
482+ } ,
483+ this . ctx
484+ ) ;
485+
463486 const response : RPCResponse = {
464487 done : true ,
465488 id,
@@ -512,6 +535,18 @@ export class Agent<Env, State = unknown> extends Server<Env> {
512535 } )
513536 ) ;
514537
538+ this . observability ?. emit (
539+ {
540+ displayMessage : "Connection established" ,
541+ id : nanoid ( ) ,
542+ payload : {
543+ connectionId : connection . id ,
544+ } ,
545+ timestamp : Date . now ( ) ,
546+ type : "connect" ,
547+ } ,
548+ this . ctx
549+ ) ;
515550 return this . _tryCatch ( ( ) => _onConnect ( connection , ctx ) ) ;
516551 } , 20 ) ;
517552 }
@@ -561,6 +596,7 @@ export class Agent<Env, State = unknown> extends Server<Env> {
561596 state : State ,
562597 source : Connection | "server" = "server"
563598 ) {
599+ const previousState = this . _state ;
564600 this . _state = state ;
565601 this . sql `
566602 INSERT OR REPLACE INTO cf_agents_state (id, state)
@@ -582,6 +618,19 @@ export class Agent<Env, State = unknown> extends Server<Env> {
582618 return agentContext . run (
583619 { agent : this , connection, request } ,
584620 async ( ) => {
621+ this . observability ?. emit (
622+ {
623+ displayMessage : "State updated" ,
624+ id : nanoid ( ) ,
625+ payload : {
626+ previousState,
627+ state,
628+ } ,
629+ timestamp : Date . now ( ) ,
630+ type : "state:update" ,
631+ } ,
632+ this . ctx
633+ ) ;
585634 return this . onStateUpdate ( state , source ) ;
586635 }
587636 ) ;
@@ -677,6 +726,18 @@ export class Agent<Env, State = unknown> extends Server<Env> {
677726 ) : Promise < Schedule < T > > {
678727 const id = nanoid ( 9 ) ;
679728
729+ const emitScheduleCreate = ( schedule : Schedule < T > ) =>
730+ this . observability ?. emit (
731+ {
732+ displayMessage : `Schedule ${ schedule . id } created` ,
733+ id : nanoid ( ) ,
734+ payload : schedule ,
735+ timestamp : Date . now ( ) ,
736+ type : "schedule:create" ,
737+ } ,
738+ this . ctx
739+ ) ;
740+
680741 if ( typeof callback !== "string" ) {
681742 throw new Error ( "Callback must be a string" ) ;
682743 }
@@ -696,13 +757,17 @@ export class Agent<Env, State = unknown> extends Server<Env> {
696757
697758 await this . _scheduleNextAlarm ( ) ;
698759
699- return {
760+ const schedule : Schedule < T > = {
700761 callback : callback ,
701762 id,
702763 payload : payload as T ,
703764 time : timestamp ,
704765 type : "scheduled" ,
705766 } ;
767+
768+ emitScheduleCreate ( schedule ) ;
769+
770+ return schedule ;
706771 }
707772 if ( typeof when === "number" ) {
708773 const time = new Date ( Date . now ( ) + when * 1000 ) ;
@@ -717,14 +782,18 @@ export class Agent<Env, State = unknown> extends Server<Env> {
717782
718783 await this . _scheduleNextAlarm ( ) ;
719784
720- return {
785+ const schedule : Schedule < T > = {
721786 callback : callback ,
722787 delayInSeconds : when ,
723788 id,
724789 payload : payload as T ,
725790 time : timestamp ,
726791 type : "delayed" ,
727792 } ;
793+
794+ emitScheduleCreate ( schedule ) ;
795+
796+ return schedule ;
728797 }
729798 if ( typeof when === "string" ) {
730799 const nextExecutionTime = getNextCronTime ( when ) ;
@@ -739,14 +808,18 @@ export class Agent<Env, State = unknown> extends Server<Env> {
739808
740809 await this . _scheduleNextAlarm ( ) ;
741810
742- return {
811+ const schedule : Schedule < T > = {
743812 callback : callback ,
744813 cron : when ,
745814 id,
746815 payload : payload as T ,
747816 time : timestamp ,
748817 type : "cron" ,
749818 } ;
819+
820+ emitScheduleCreate ( schedule ) ;
821+
822+ return schedule ;
750823 }
751824 throw new Error ( "Invalid schedule type" ) ;
752825 }
@@ -822,6 +895,19 @@ export class Agent<Env, State = unknown> extends Server<Env> {
822895 * @returns true if the task was cancelled, false otherwise
823896 */
824897 async cancelSchedule ( id : string ) : Promise < boolean > {
898+ const schedule = await this . getSchedule ( id ) ;
899+ if ( schedule ) {
900+ this . observability ?. emit (
901+ {
902+ displayMessage : `Schedule ${ id } cancelled` ,
903+ id : nanoid ( ) ,
904+ payload : schedule ,
905+ timestamp : Date . now ( ) ,
906+ type : "schedule:cancel" ,
907+ } ,
908+ this . ctx
909+ ) ;
910+ }
825911 this . sql `DELETE FROM cf_agents_schedules WHERE id = ${ id } ` ;
826912
827913 await this . _scheduleNextAlarm ( ) ;
@@ -870,6 +956,17 @@ export class Agent<Env, State = unknown> extends Server<Env> {
870956 { agent : this , connection : undefined , request : undefined } ,
871957 async ( ) => {
872958 try {
959+ this . observability ?. emit (
960+ {
961+ displayMessage : `Schedule ${ row . id } executed` ,
962+ id : nanoid ( ) ,
963+ payload : row ,
964+ timestamp : Date . now ( ) ,
965+ type : "schedule:execute" ,
966+ } ,
967+ this . ctx
968+ ) ;
969+
873970 await (
874971 callback as (
875972 payload : unknown ,
@@ -914,6 +1011,17 @@ export class Agent<Env, State = unknown> extends Server<Env> {
9141011 await this . ctx . storage . deleteAlarm ( ) ;
9151012 await this . ctx . storage . deleteAll ( ) ;
9161013 this . ctx . abort ( "destroyed" ) ; // enforce that the agent is evicted
1014+
1015+ this . observability ?. emit (
1016+ {
1017+ displayMessage : "Agent destroyed" ,
1018+ id : nanoid ( ) ,
1019+ payload : { } ,
1020+ timestamp : Date . now ( ) ,
1021+ type : "destroy" ,
1022+ } ,
1023+ this . ctx
1024+ ) ;
9171025 }
9181026
9191027 /**
0 commit comments