@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 +15 -7
- package/dist/v2.mjs +23 -26
- 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,12 +466,12 @@ 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
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
|
|
483
|
+
console.error("[Agent] Failed to create feedback table", error);
|
|
484
484
|
}
|
|
485
485
|
}
|
|
486
486
|
/**
|
|
487
|
-
*
|
|
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
|
|
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
|
|
493
|
+
function submitMessageFeedback(sql, messageId, rating, comment, now = /* @__PURE__ */ new Date()) {
|
|
494
494
|
try {
|
|
495
|
-
sql`INSERT INTO
|
|
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
|
|
502
|
+
console.error("[Agent] Failed to submit message feedback", error);
|
|
503
503
|
}
|
|
504
504
|
}
|
|
505
505
|
/**
|
|
506
|
-
* Gets the
|
|
506
|
+
* Gets the feedback for all messages.
|
|
507
507
|
* @param sql - The SQL function to use to execute the query.
|
|
508
|
-
* @returns A
|
|
508
|
+
* @returns A dictionary of message feedback keyed by message id.
|
|
509
509
|
*/
|
|
510
|
-
function
|
|
510
|
+
function getMessageFeedback(sql) {
|
|
511
511
|
try {
|
|
512
|
-
const
|
|
513
|
-
return Object.fromEntries(
|
|
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
|
-
|
|
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
|
-
*
|
|
541
|
-
* @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.
|
|
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: "
|
|
546
|
-
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);
|
|
547
544
|
}
|
|
548
545
|
/**
|
|
549
|
-
* Returns all message
|
|
550
|
-
* @returns All message
|
|
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
|
|
553
|
-
return
|
|
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
|