@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 +15 -7
- package/dist/v2.mjs +28 -34
- package/package.json +1 -1
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
|
-
*
|
|
129
|
-
* @param messageId - The id of the message to
|
|
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
|
-
|
|
141
|
+
submitMessageFeedback(messageId: string, rating: number, comment?: string): Promise<void>;
|
|
134
142
|
/**
|
|
135
|
-
* Returns all message
|
|
136
|
-
* @returns All message
|
|
143
|
+
* Returns all message feedback for the current chat.
|
|
144
|
+
* @returns All message feedback for the current chat.
|
|
137
145
|
*/
|
|
138
|
-
|
|
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
|
|
469
|
+
* Ensures that the feedback table exists.
|
|
470
470
|
* @param sql - The SQL function to use to execute the query.
|
|
471
471
|
*/
|
|
472
|
-
function
|
|
472
|
+
function ensureFeedbackTableExists(sql) {
|
|
473
473
|
try {
|
|
474
|
-
sql`CREATE TABLE IF NOT EXISTS
|
|
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
|
|
480
|
-
updated_at
|
|
481
|
-
PRIMARY KEY (message_id
|
|
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
|
|
483
|
+
console.error("[Agent] Failed to create feedback table", error);
|
|
485
484
|
}
|
|
486
485
|
}
|
|
487
486
|
/**
|
|
488
|
-
*
|
|
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
|
|
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
|
|
493
|
+
function submitMessageFeedback(sql, messageId, rating, comment, now = /* @__PURE__ */ new Date()) {
|
|
496
494
|
try {
|
|
497
|
-
sql`INSERT INTO
|
|
498
|
-
VALUES (${messageId}, ${
|
|
499
|
-
ON CONFLICT (message_id
|
|
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
|
|
502
|
+
console.error("[Agent] Failed to submit message feedback", error);
|
|
505
503
|
}
|
|
506
504
|
}
|
|
507
505
|
/**
|
|
508
|
-
* Gets the
|
|
506
|
+
* Gets the feedback for all messages.
|
|
509
507
|
* @param sql - The SQL function to use to execute the query.
|
|
510
|
-
* @
|
|
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
|
|
510
|
+
function getMessageFeedback(sql) {
|
|
514
511
|
try {
|
|
515
|
-
const
|
|
516
|
-
return Object.fromEntries(
|
|
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
|
-
|
|
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
|
-
*
|
|
544
|
-
* @param messageId - The id of the message to
|
|
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: "
|
|
549
|
-
return
|
|
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
|
|
553
|
-
* @returns All message
|
|
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
|
|
556
|
-
return
|
|
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
|