@@ -377,37 +377,39 @@ interface ReminderData {
377377 channel: string ;
378378}
379379
380- // Schedule a one-time reminder in 2 hours
381- async scheduleReminder (userId : string , message : string ) {
382- const twoHoursFromNow = new Date (Date .now () + 2 * 60 * 60 * 1000 );
383-
384- const schedule = await this .schedule <ReminderData >(
385- twoHoursFromNow ,
386- ' sendReminder' ,
387- { userId , message , channel: ' email' }
388- );
380+ export class YourAgent extends Agent {
381+ // Schedule a one-time reminder in 2 hours
382+ async scheduleReminder(userId : string , message : string ) {
383+ const twoHoursFromNow = new Date (Date .now () + 2 * 60 * 60 * 1000 );
384+
385+ const schedule = await this .schedule <ReminderData >(
386+ twoHoursFromNow ,
387+ ' sendReminder' ,
388+ { userId , message , channel: ' email' }
389+ );
389390
390- console .log (` Scheduled reminder with ID: ${schedule .id } ` );
391- return schedule .id ;
392- }
391+ console .log (` Scheduled reminder with ID: ${schedule .id } ` );
392+ return schedule .id ;
393+ }
393394
394- // Schedule a recurring daily task using cron
395- async scheduleDailyReport () {
396- // Run at 08:00 AM every day
397- const schedule = await this .schedule (
398- ' 0 8 * * *' , // Cron expression: minute hour day month weekday
399- ' generateDailyReport' ,
400- { reportType: ' daily-summary' }
401- );
395+ // Schedule a recurring daily task using cron
396+ async scheduleDailyReport() {
397+ // Run at 08:00 AM every day
398+ const schedule = await this .schedule (
399+ ' 0 8 * * *' , // Cron expression: minute hour day month weekday
400+ ' generateDailyReport' ,
401+ { reportType: ' daily-summary' }
402+ );
402403
403- console .log (` Scheduled daily report with ID: ${schedule .id } ` );
404- return schedule .id ;
405- }
404+ console .log (` Scheduled daily report with ID: ${schedule .id } ` );
405+ return schedule .id ;
406+ }
406407
407- // Method that will be called when the scheduled task runs
408- async sendReminder (data : ReminderData ) {
409- console .log (` Sending reminder to ${data .userId }: ${data .message } ` );
410- // Add code to send the actual notification
408+ // Method that will be called when the scheduled task runs
409+ async sendReminder(data : ReminderData ) {
410+ console .log (` Sending reminder to ${data .userId }: ${data .message } ` );
411+ // Add code to send the actual notification
412+ }
411413}
412414```
413415
@@ -455,30 +457,32 @@ type Schedule<T = any> = {
455457<TypeScriptExample >
456458
457459``` ts
458- // Example of managing scheduled tasks
459- async viewAndManageSchedules () {
460- // Get all scheduled tasks
461- const allSchedules = this .getSchedules ();
462- console .log (` Total scheduled tasks: ${allSchedules .length } ` );
463-
464- // Get tasks scheduled for a specific time range
465- const upcomingSchedules = this .getSchedules ({
466- timeRange: {
467- start: new Date (),
468- end: new Date (Date .now () + 24 * 60 * 60 * 1000 ) // Next 24 hours
469- }
470- });
460+ export class YourAgent extends Agent {
461+ // Example of managing scheduled tasks
462+ async viewAndManageSchedules() {
463+ // Get all scheduled tasks
464+ const allSchedules = this .getSchedules ();
465+ console .log (` Total scheduled tasks: ${allSchedules .length } ` );
466+
467+ // Get tasks scheduled for a specific time range
468+ const upcomingSchedules = this .getSchedules ({
469+ timeRange: {
470+ start: new Date (),
471+ end: new Date (Date .now () + 24 * 60 * 60 * 1000 ) // Next 24 hours
472+ }
473+ });
471474
472- // Get a specific task by ID
473- const taskId = " task-123" ;
474- const specificTask = await this .getSchedule (taskId );
475+ // Get a specific task by ID
476+ const taskId = " task-123" ;
477+ const specificTask = await this .getSchedule (taskId );
475478
476- if (specificTask ) {
477- console .log (` Found task: ${specificTask .callback } at ${new Date (specificTask .time )} ` );
479+ if (specificTask ) {
480+ console .log (` Found task: ${specificTask .callback } at ${new Date (specificTask .time )} ` );
478481
479- // Cancel a scheduled task
480- const cancelled = await this .cancelSchedule (taskId );
481- console .log (` Task cancelled: ${cancelled } ` );
482+ // Cancel a scheduled task
483+ const cancelled = await this .cancelSchedule (taskId );
484+ console .log (` Task cancelled: ${cancelled } ` );
485+ }
482486 }
483487}
484488```
@@ -516,43 +520,44 @@ interface User {
516520 created_at: number ;
517521}
518522
519- // Inside your Agent class
520- async setupDatabase () {
521- // Create a table if it doesn't exist
522- this .sql `
523- CREATE TABLE IF NOT EXISTS users (
524- id TEXT PRIMARY KEY,
525- name TEXT NOT NULL,
526- email TEXT UNIQUE,
527- created_at INTEGER
528- )
529- ` ;
530- }
523+ export class YourAgent extends Agent {
524+ async setupDatabase() {
525+ // Create a table if it doesn't exist
526+ this .sql `
527+ CREATE TABLE IF NOT EXISTS users (
528+ id TEXT PRIMARY KEY,
529+ name TEXT NOT NULL,
530+ email TEXT UNIQUE,
531+ created_at INTEGER
532+ )
533+ ` ;
534+ }
531535
532- async createUser (id : string , name : string , email : string ) {
533- // Insert a new user
534- this .sql `
535- INSERT INTO users (id, name, email, created_at)
536- VALUES (${id }, ${name }, ${email }, ${Date .now ()})
537- ` ;
538- }
536+ async createUser(id : string , name : string , email : string ) {
537+ // Insert a new user
538+ this .sql `
539+ INSERT INTO users (id, name, email, created_at)
540+ VALUES (${id }, ${name }, ${email }, ${Date .now ()})
541+ ` ;
542+ }
539543
540- async getUserById (id : string ): Promise < User | null > {
541- // Query a user by ID
542- const users = this .sql <User >`
543- SELECT * FROM users WHERE id = ${id }
544- ` ;
544+ async getUserById(id : string ): Promise <User | null > {
545+ // Query a user by ID
546+ const users = this .sql <User >`
547+ SELECT * FROM users WHERE id = ${id }
548+ ` ;
545549
546- return users.length ? users[0 ] : null ;
547- }
550+ return users .length ? users [0 ] : null ;
551+ }
548552
549- async searchUsers (term : string ): Promise < User []> {
550- // Search users with a wildcard
551- return this.sql<User>`
552- SELECT * FROM users
553- WHERE name LIKE ${' %' + term + ' %' } OR email LIKE ${' %' + term + ' %' }
554- ORDER BY created_at DESC
555- ` ;
553+ async searchUsers(term : string ): Promise <User []> {
554+ // Search users with a wildcard
555+ return this .sql <User >`
556+ SELECT * FROM users
557+ WHERE name LIKE ${' %' + term + ' %' } OR email LIKE ${' %' + term + ' %' }
558+ ORDER BY created_at DESC
559+ ` ;
560+ }
556561}
557562```
558563
0 commit comments