@economic/agents 2.1.2 → 2.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/v2.d.mts CHANGED
@@ -2,7 +2,6 @@ import { t as JwtAuthConfig } from "./index-DzOC3mNl.mjs";
2
2
  import { JSONValue, LanguageModel, Tool as Tool$1, UIMessage } from "ai";
3
3
  import { Agent as Agent$1, Connection, ConnectionContext, SubAgentClass } from "agents";
4
4
  import { ChatResponseResult, Session, StepConfig, Think, ToolCallContext, ToolCallDecision, TurnConfig, TurnContext } from "@cloudflare/think";
5
-
6
5
  //#region src/server/v2/types.d.ts
7
6
  /**
8
7
  * The context object available throughout an agent's lifetime — passed via
@@ -118,6 +117,15 @@ declare abstract class Agent<RequestContext extends Record<string, unknown> = Re
118
117
  protected getUserContext?(jwtToken: string): Promise<UserContext>;
119
118
  }
120
119
  //#endregion
120
+ //#region src/server/v2/features/messages.d.ts
121
+ type MessageFeedback = {
122
+ message_id: string;
123
+ rating: number;
124
+ comment?: string;
125
+ created_at: number;
126
+ updated_at: number;
127
+ };
128
+ //#endregion
121
129
  //#region src/server/v2/agents/ChatAgent.d.ts
122
130
  declare abstract class ChatAgent<RequestContext extends Record<string, unknown> = Record<string, unknown>, UserContext extends Record<string, unknown> = Record<string, unknown>> extends Agent<RequestContext, UserContext> {
123
131
  initialState: AgentConnectionState;
@@ -125,17 +133,17 @@ declare abstract class ChatAgent<RequestContext extends Record<string, unknown>
125
133
  configureSession(session: Session): Session;
126
134
  onChatResponse(_result: ChatResponseResult): Promise<void>;
127
135
  /**
128
- * Rate a message by its id.
129
- * @param messageId - The id of the message to rate.
136
+ * Submit feedback for a message by its id.
137
+ * @param messageId - The id of the message to give feedback on.
130
138
  * @param rating - The rating to give the message. 1 = thumbs up, -1 = thumbs down.
131
139
  * @returns The message id and the rating.
132
140
  */
133
- rateMessage(messageId: string, rating: number, comment?: string): Promise<void>;
141
+ submitMessageFeedback(messageId: string, rating: number, comment?: string): Promise<void>;
134
142
  /**
135
- * Returns all message ratings for the current chat.
136
- * @returns All message ratings for the current chat.
143
+ * Returns all message feedback for the current chat.
144
+ * @returns All message feedback for the current chat.
137
145
  */
138
- getMessageRatings(): Promise<any>;
146
+ getMessageFeedback(): Promise<Record<string, MessageFeedback>>;
139
147
  }
140
148
  //#endregion
141
149
  //#region src/server/v2/features/chats.d.ts
package/dist/v2.mjs CHANGED
@@ -466,57 +466,51 @@ const createCompactFn = (model) => createCompactFunction({ summarize: (prompt) =
466
466
  prompt
467
467
  }).then((r) => r.text) });
468
468
  /**
469
- * Ensures that the ratings table exists.
469
+ * Ensures that the feedback table exists.
470
470
  * @param sql - The SQL function to use to execute the query.
471
471
  */
472
- function ensureRatingsTableExists(sql) {
472
+ function ensureFeedbackTableExists(sql) {
473
473
  try {
474
- sql`CREATE TABLE IF NOT EXISTS assistant_messages_ratings (
474
+ sql`CREATE TABLE IF NOT EXISTS assistant_messages_feedback (
475
475
  message_id TEXT NOT NULL,
476
- durable_object_name TEXT NOT NULL,
477
476
  rating INTEGER,
478
477
  comment TEXT,
479
- created_at TEXT NOT NULL,
480
- updated_at TEXT NOT NULL,
481
- PRIMARY KEY (message_id, durable_object_name)
478
+ created_at INTEGER NOT NULL,
479
+ updated_at INTEGER NOT NULL,
480
+ PRIMARY KEY (message_id)
482
481
  )`;
483
482
  } catch (error) {
484
- console.error("[Agent] Failed to create ratings table", error);
483
+ console.error("[Agent] Failed to create feedback table", error);
485
484
  }
486
485
  }
487
486
  /**
488
- * Rates a message.
487
+ * Submits feedback for a message.
489
488
  * @param sql - The SQL function to use to execute the query.
490
- * @param messageId - The ID of the message to rate.
491
- * @param durable_object_name - The name of the Durable Object to rate the message for.
489
+ * @param messageId - The ID of the message to give feedback on.
492
490
  * @param rating - The rating to give the message.
493
491
  * @param now - The date and time to use for the created_at and updated_at columns.
494
492
  */
495
- function rateMessage(sql, messageId, durable_object_name, rating, comment, now = /* @__PURE__ */ new Date()) {
493
+ function submitMessageFeedback(sql, messageId, rating, comment, now = /* @__PURE__ */ new Date()) {
496
494
  try {
497
- sql`INSERT INTO assistant_messages_ratings (message_id, durable_object_name, rating, comment, created_at, updated_at)
498
- VALUES (${messageId}, ${durable_object_name}, ${rating}, ${comment ?? null}, ${now.toISOString()}, ${now.toISOString()})
499
- ON CONFLICT (message_id, durable_object_name) DO UPDATE SET
495
+ sql`INSERT INTO assistant_messages_feedback (message_id, rating, comment, created_at, updated_at)
496
+ VALUES (${messageId}, ${rating}, ${comment ?? null}, ${now.getTime()}, ${now.getTime()})
497
+ ON CONFLICT (message_id) DO UPDATE SET
500
498
  rating = excluded.rating,
501
499
  comment = excluded.comment,
502
500
  updated_at = excluded.updated_at`;
503
501
  } catch (error) {
504
- console.error("[Agent] Failed to rate message", error);
502
+ console.error("[Agent] Failed to submit message feedback", error);
505
503
  }
506
504
  }
507
505
  /**
508
- * Gets the ratings for a message.
506
+ * Gets the feedback for all messages.
509
507
  * @param sql - The SQL function to use to execute the query.
510
- * @param durable_object_name - The name of the Durable Object to get the ratings for.
511
- * @returns A record of message IDs and their ratings.
508
+ * @returns A dictionary of message feedback keyed by message id.
512
509
  */
513
- function getMessageRatings(sql, durable_object_name) {
510
+ function getMessageFeedback(sql) {
514
511
  try {
515
- const ratings = sql`SELECT message_id, rating, comment FROM assistant_messages_ratings WHERE durable_object_name = ${durable_object_name}`;
516
- return Object.fromEntries(ratings.map((row) => [row.message_id, {
517
- rating: row.rating,
518
- comment: row.comment
519
- }]));
512
+ const feedback = sql`SELECT message_id, rating, comment, created_at, updated_at FROM assistant_messages_feedback`;
513
+ return Object.fromEntries(feedback.map((row) => [row.message_id, row]));
520
514
  } catch {
521
515
  return {};
522
516
  }
@@ -530,7 +524,7 @@ var ChatAgent = class extends Agent {
530
524
  };
531
525
  async onStart() {
532
526
  await super.onStart();
533
- ensureRatingsTableExists(this.sql.bind(this));
527
+ ensureFeedbackTableExists(this.sql.bind(this));
534
528
  }
535
529
  configureSession(session) {
536
530
  return super.configureSession(session).onCompaction(createCompactFn(this.getModel())).compactAfter(COMPACTION_TOKEN_THRESHOLD);
@@ -540,20 +534,20 @@ var ChatAgent = class extends Agent {
540
534
  if (parent?.recordChatTurn) await parent.recordChatTurn(this.name, this.messages);
541
535
  }
542
536
  /**
543
- * Rate a message by its id.
544
- * @param messageId - The id of the message to rate.
537
+ * Submit feedback for a message by its id.
538
+ * @param messageId - The id of the message to give feedback on.
545
539
  * @param rating - The rating to give the message. 1 = thumbs up, -1 = thumbs down.
546
540
  * @returns The message id and the rating.
547
541
  */
548
- @callable({ description: "Rate a message by its id" }) async rateMessage(messageId, rating, comment) {
549
- return rateMessage(this.sql.bind(this), messageId, this.name, rating, comment);
542
+ @callable({ description: "Submit feedback for a message by its id" }) async submitMessageFeedback(messageId, rating, comment) {
543
+ return submitMessageFeedback(this.sql.bind(this), messageId, rating, comment);
550
544
  }
551
545
  /**
552
- * Returns all message ratings for the current chat.
553
- * @returns All message ratings for the current chat.
546
+ * Returns all message feedback for the current chat.
547
+ * @returns All message feedback for the current chat.
554
548
  */
555
- @callable({ description: "Returns all message ratings for the current chat" }) async getMessageRatings() {
556
- return getMessageRatings(this.sql.bind(this), this.name);
549
+ @callable({ description: "Returns all message feedback for the current chat" }) async getMessageFeedback() {
550
+ return getMessageFeedback(this.sql.bind(this));
557
551
  }
558
552
  };
559
553
  //#endregion
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@economic/agents",
3
- "version": "2.1.2",
3
+ "version": "2.1.4",
4
4
  "description": "A starter for creating a TypeScript package.",
5
5
  "license": "MIT",
6
6
  "bin": {