@economic/agents 2.1.3 → 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,12 +466,12 @@ 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
476
  rating INTEGER,
477
477
  comment TEXT,
@@ -480,40 +480,37 @@ function ensureRatingsTableExists(sql) {
480
480
  PRIMARY KEY (message_id)
481
481
  )`;
482
482
  } catch (error) {
483
- console.error("[Agent] Failed to create ratings table", error);
483
+ console.error("[Agent] Failed to create feedback table", error);
484
484
  }
485
485
  }
486
486
  /**
487
- * Rates a message.
487
+ * Submits feedback for a message.
488
488
  * @param sql - The SQL function to use to execute the query.
489
- * @param messageId - The ID of the message to rate.
489
+ * @param messageId - The ID of the message to give feedback on.
490
490
  * @param rating - The rating to give the message.
491
491
  * @param now - The date and time to use for the created_at and updated_at columns.
492
492
  */
493
- function rateMessage(sql, messageId, rating, comment, now = /* @__PURE__ */ new Date()) {
493
+ function submitMessageFeedback(sql, messageId, rating, comment, now = /* @__PURE__ */ new Date()) {
494
494
  try {
495
- sql`INSERT INTO assistant_messages_ratings (message_id, rating, comment, created_at, updated_at)
495
+ sql`INSERT INTO assistant_messages_feedback (message_id, rating, comment, created_at, updated_at)
496
496
  VALUES (${messageId}, ${rating}, ${comment ?? null}, ${now.getTime()}, ${now.getTime()})
497
497
  ON CONFLICT (message_id) DO UPDATE SET
498
498
  rating = excluded.rating,
499
499
  comment = excluded.comment,
500
500
  updated_at = excluded.updated_at`;
501
501
  } catch (error) {
502
- console.error("[Agent] Failed to rate message", error);
502
+ console.error("[Agent] Failed to submit message feedback", error);
503
503
  }
504
504
  }
505
505
  /**
506
- * Gets the ratings for a message.
506
+ * Gets the feedback for all messages.
507
507
  * @param sql - The SQL function to use to execute the query.
508
- * @returns A record of message IDs and their ratings.
508
+ * @returns A dictionary of message feedback keyed by message id.
509
509
  */
510
- function getMessageRatings(sql) {
510
+ function getMessageFeedback(sql) {
511
511
  try {
512
- const ratings = sql`SELECT message_id, rating, comment FROM assistant_messages_ratings`;
513
- return Object.fromEntries(ratings.map((row) => [row.message_id, {
514
- rating: row.rating,
515
- comment: row.comment
516
- }]));
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]));
517
514
  } catch {
518
515
  return {};
519
516
  }
@@ -527,7 +524,7 @@ var ChatAgent = class extends Agent {
527
524
  };
528
525
  async onStart() {
529
526
  await super.onStart();
530
- ensureRatingsTableExists(this.sql.bind(this));
527
+ ensureFeedbackTableExists(this.sql.bind(this));
531
528
  }
532
529
  configureSession(session) {
533
530
  return super.configureSession(session).onCompaction(createCompactFn(this.getModel())).compactAfter(COMPACTION_TOKEN_THRESHOLD);
@@ -537,20 +534,20 @@ var ChatAgent = class extends Agent {
537
534
  if (parent?.recordChatTurn) await parent.recordChatTurn(this.name, this.messages);
538
535
  }
539
536
  /**
540
- * Rate a message by its id.
541
- * @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.
542
539
  * @param rating - The rating to give the message. 1 = thumbs up, -1 = thumbs down.
543
540
  * @returns The message id and the rating.
544
541
  */
545
- @callable({ description: "Rate a message by its id" }) async rateMessage(messageId, rating, comment) {
546
- return rateMessage(this.sql.bind(this), messageId, 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);
547
544
  }
548
545
  /**
549
- * Returns all message ratings for the current chat.
550
- * @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.
551
548
  */
552
- @callable({ description: "Returns all message ratings for the current chat" }) async getMessageRatings() {
553
- return getMessageRatings(this.sql.bind(this));
549
+ @callable({ description: "Returns all message feedback for the current chat" }) async getMessageFeedback() {
550
+ return getMessageFeedback(this.sql.bind(this));
554
551
  }
555
552
  };
556
553
  //#endregion
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@economic/agents",
3
- "version": "2.1.3",
3
+ "version": "2.1.4",
4
4
  "description": "A starter for creating a TypeScript package.",
5
5
  "license": "MIT",
6
6
  "bin": {