@economic/agents 2.14.1 → 2.14.2

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/index.d.mts CHANGED
@@ -519,6 +519,8 @@ declare abstract class ChatAgent<RequestContext extends Record<string, unknown>
519
519
  * returned immediately and each event reaches the client as it is produced.
520
520
  */
521
521
  private streamMessage;
522
+ private streamToolApproval;
523
+ private streamTurn;
522
524
  /**
523
525
  * Submit feedback for a message by its id.
524
526
  * @param messageId - The id of the message to give feedback on.
package/dist/index.mjs CHANGED
@@ -4,6 +4,7 @@ import { Output, convertToModelMessages, generateText, jsonSchema, pruneMessages
4
4
  import { createRemoteJWKSet, decodeJwt, errors, jwtVerify } from "jose";
5
5
  import { BasicTracerProvider, SimpleSpanProcessor } from "@opentelemetry/sdk-trace-base";
6
6
  import { genericObservability } from "agents/observability";
7
+ import { applyToolUpdate, toolApprovalUpdate } from "agents/chat";
7
8
  import { createCompactFunction, estimateMessageTokens, estimateStringTokens, sanitizeToolPairs, truncateOlderMessages } from "agents/experimental/memory/utils";
8
9
  import { nanoid } from "nanoid";
9
10
  //#region src/server/features/auth/index.ts
@@ -2846,6 +2847,22 @@ var ChatAgent = class extends Agent {
2846
2847
  if (action === "getMessages" && request.method === "GET") return Response.json(await this.getMessages());
2847
2848
  if (action === "getInvocableSkills" && request.method === "GET") return Response.json(await this.getInvocableSkills(auth.userContext));
2848
2849
  if (action === "sendMessage" && request.method === "POST") return this.streamMessage(request);
2850
+ if (action === "submitToolApproval" && request.method === "POST") return this.streamToolApproval(request);
2851
+ if (action === "getMessageFeedback" && request.method === "GET") return Response.json(await this.getMessageFeedback());
2852
+ if (action === "submitMessageFeedback" && request.method === "POST") {
2853
+ let payload;
2854
+ try {
2855
+ payload = await request.json();
2856
+ } catch {
2857
+ return Response.json({ error: "Request body must be valid JSON" }, { status: 400 });
2858
+ }
2859
+ if (!payload || typeof payload !== "object" || Array.isArray(payload)) return Response.json({ error: "Request body must be an object" }, { status: 400 });
2860
+ const { messageId, rating, comment } = payload;
2861
+ if (!messageId) return Response.json({ error: "Message ID is required" }, { status: 400 });
2862
+ if (rating !== -1 && rating !== 0 && rating !== 1) return Response.json({ error: "Rating must be -1, 0, or 1" }, { status: 400 });
2863
+ if (comment !== void 0 && typeof comment !== "string") return Response.json({ error: "Comment must be a string" }, { status: 400 });
2864
+ return Response.json(await this.submitMessageFeedback(messageId, rating, comment));
2865
+ }
2849
2866
  if (action === "cancelChat" && request.method === "POST") {
2850
2867
  const { requestId } = await request.json();
2851
2868
  this.cancelChat(requestId);
@@ -2882,6 +2899,39 @@ var ChatAgent = class extends Agent {
2882
2899
  this._turn.context = this.createToolContextForCurrentTurn(context);
2883
2900
  return [message];
2884
2901
  } : message;
2902
+ return this.streamTurn(input);
2903
+ }
2904
+ async streamToolApproval(request) {
2905
+ let payload;
2906
+ try {
2907
+ payload = await request.json();
2908
+ } catch {
2909
+ return Response.json({ error: "Request body must be valid JSON" }, { status: 400 });
2910
+ }
2911
+ if (!payload || typeof payload !== "object" || Array.isArray(payload)) return Response.json({ error: "Request body must be an object" }, { status: 400 });
2912
+ const { toolCallId, approved, context } = payload;
2913
+ if (!toolCallId) return Response.json({ error: "Tool call ID is required" }, { status: 400 });
2914
+ if (typeof approved !== "boolean") return Response.json({ error: "Approved must be a boolean" }, { status: 400 });
2915
+ if (context !== void 0 && (context === null || typeof context !== "object" || Array.isArray(context))) return Response.json({ error: "Context must be an object" }, { status: 400 });
2916
+ return this.streamTurn((messages) => {
2917
+ if (context) this._turn.context = this.createToolContextForCurrentTurn(context);
2918
+ const update = toolApprovalUpdate(toolCallId, approved);
2919
+ let found = false;
2920
+ const updatedMessages = messages.map((message) => {
2921
+ if (found || message.role !== "assistant") return message;
2922
+ const result = applyToolUpdate(message.parts, update);
2923
+ if (!result) return message;
2924
+ found = true;
2925
+ return {
2926
+ ...message,
2927
+ parts: result.parts
2928
+ };
2929
+ });
2930
+ if (!found) throw new Error(`No pending tool approval found for ${toolCallId}`);
2931
+ return updatedMessages;
2932
+ });
2933
+ }
2934
+ streamTurn(input) {
2885
2935
  const encoder = new TextEncoder();
2886
2936
  const readable = new ReadableStream({ start: async (controller) => {
2887
2937
  const send = (event, data) => controller.enqueue(encoder.encode(`event: ${event}\ndata: ${data}\n\n`));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@economic/agents",
3
- "version": "2.14.1",
3
+ "version": "2.14.2",
4
4
  "description": "A starter for creating a TypeScript package.",
5
5
  "license": "MIT",
6
6
  "files": [