@economic/agents 2.2.2 → 2.2.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/index.d.mts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { n as JwtAuthConfig, t as routeAgentRequest } from "./route-agent-request-DmwIOBJS.mjs";
2
+ import { Agent as Agent$1, Connection, ConnectionContext, SubAgentClass } from "agents";
2
3
  import { ChatResponseResult, Session, StepConfig, Think, ToolCallContext, ToolCallDecision, TurnConfig, TurnContext } from "@cloudflare/think";
3
4
  import { JSONValue, LanguageModel, Tool as Tool$1, UIMessage } from "ai";
4
- import { Agent as Agent$1, Connection, ConnectionContext, SubAgentClass } from "agents";
5
5
  //#region src/server/types.d.ts
6
6
  /**
7
7
  * The context object available throughout an agent's lifetime — passed via
@@ -46,7 +46,6 @@ interface Skill<Context extends Record<string, unknown> = Record<string, unknown
46
46
  declare function skill<Context extends Record<string, unknown> = Record<string, unknown>>(definition: Skill<Context>): Skill<Context>;
47
47
  //#endregion
48
48
  //#region src/server/agents/Agent.d.ts
49
- declare function getCurrentToolContext(): any;
50
49
  declare abstract class Agent<RequestContext extends Record<string, unknown> = Record<string, unknown>, UserContext extends Record<string, unknown> = Record<string, unknown>> extends Think<Cloudflare.Env & AgentEnv, AgentConnectionState> {
51
50
  initialState: AgentConnectionState;
52
51
  protected clientIp?: string;
@@ -84,7 +83,6 @@ declare abstract class Agent<RequestContext extends Record<string, unknown> = Re
84
83
  * Returns the remote skills to be loaded from SKILLS_BUCKET.
85
84
  * @returns The remote skills to be loaded from SKILLS_BUCKET.
86
85
  */
87
- protected getRemoteSkills(): string[];
88
86
  /**
89
87
  * Returns the skills to load for the agent.
90
88
  * @returns The skills to load for the agent.
@@ -237,16 +235,31 @@ declare abstract class Assistant extends Agent$1<Cloudflare.Env & AgentEnv, Agen
237
235
  onStart(): void;
238
236
  onClose(): Promise<void>;
239
237
  onConnect(connection: Connection, ctx: ConnectionContext): Promise<void>;
238
+ createChat(): Promise<string>;
239
+ deleteChat(id: string): Promise<void>;
240
+ getChats(): Promise<Chat[]>;
241
+ recordChatTurn(durableObjectName: string, messages: UIMessage[]): Promise<void>;
242
+ private [DELETE_CHAT_CALLBACK];
243
+ private scheduleChatForAutoDeletion;
244
+ /**
245
+ * Self-registers this Assistant in the global `agent_registry`. An Assistant
246
+ * is a top-level DO keyed directly by user id, so the DO name *is* the actor.
247
+ *
248
+ * Runs pre-auth in onStart (the DO has already persisted state by this point,
249
+ * so it must be tracked regardless of whether auth later succeeds).
250
+ * Best-effort and idempotent: no-ops on conflict, retried on the next start.
251
+ */
252
+ private registerInstance;
240
253
  /**
241
254
  * Binding of the legacy v1 chat Durable Object class, used to migrate a
242
255
  * user's v1 chats into facets the first time they connect. Set this on the
243
256
  * concrete subclass to enable lazy v1 -> v2 migration; leave undefined to
244
257
  * disable it (e.g. for greenfield deployments with no v1 data).
245
258
  */
246
- protected migrationBinding?: {
259
+ protected getMigrationBinding(_env: Cloudflare.Env & AgentEnv): {
247
260
  binding: DurableObjectNamespace;
248
261
  db: D1Database;
249
- };
262
+ } | undefined;
250
263
  /** In-flight migration, shared across concurrent connections to this DO. */
251
264
  private _migrationPromise?;
252
265
  /**
@@ -257,21 +270,9 @@ declare abstract class Assistant extends Agent$1<Cloudflare.Env & AgentEnv, Agen
257
270
  */
258
271
  private ensureMigrated;
259
272
  private runMigration;
260
- createChat(): Promise<string>;
261
- deleteChat(id: string): Promise<void>;
262
- getChats(): Promise<Chat[]>;
263
- recordChatTurn(durableObjectName: string, messages: UIMessage[]): Promise<void>;
264
- private [DELETE_CHAT_CALLBACK];
265
- private scheduleChatForAutoDeletion;
266
- /**
267
- * Self-registers this Assistant in the global `agent_registry`. An Assistant
268
- * is a top-level DO keyed directly by user id, so the DO name *is* the actor.
269
- *
270
- * Runs pre-auth in onStart (the DO has already persisted state by this point,
271
- * so it must be tracked regardless of whether auth later succeeds).
272
- * Best-effort and idempotent: no-ops on conflict, retried on the next start.
273
- */
274
- private registerInstance;
275
273
  }
276
274
  //#endregion
275
+ //#region src/server/index.d.ts
276
+ declare function getCurrentToolContext(): any;
277
+ //#endregion
277
278
  export { Agent, type AgentConnectionState, type AgentConnectionStatus, type AgentConnectionType, type AgentEnv, Assistant, ChatAgent, type FacetStub, type LegacyChatStub, type LegacyMessageFeedback, type MigrationDeps, type Skill, type Tool, type ToolContext, type ToolSet, getCurrentToolContext, migrateUserFromV1, routeAgentRequest, skill, tool };
package/dist/index.mjs CHANGED
@@ -1,8 +1,7 @@
1
1
  import { i as verifyJwt, n as createAgentTracer, r as extractTokenFromConnectRequest, t as routeAgentRequest } from "./route-agent-request-DKDvDYnR.mjs";
2
+ import { Agent as Agent$1, callable, getCurrentAgent } from "agents";
2
3
  import { Think } from "@cloudflare/think";
3
4
  import { Output, convertToModelMessages, generateText, jsonSchema, pruneMessages, tool as tool$1 } from "ai";
4
- import { Agent as Agent$1, callable, getCurrentAgent } from "agents";
5
- import { R2SkillProvider } from "agents/experimental/memory/session";
6
5
  import { nanoid } from "nanoid";
7
6
  import { createCompactFunction } from "agents/experimental/memory/utils";
8
7
  //#region src/server/util/tools.ts
@@ -56,9 +55,6 @@ async function registerAgent(db, input) {
56
55
  }
57
56
  //#endregion
58
57
  //#region src/server/agents/Agent.ts
59
- function getCurrentToolContext() {
60
- return getCurrentAgent().agent._lastBody;
61
- }
62
58
  var Agent = class extends Think {
63
59
  initialState = {
64
60
  status: "connecting",
@@ -156,12 +152,6 @@ var Agent = class extends Think {
156
152
  let configuredSession = session.withContext("soul", { provider: { get: async () => {
157
153
  return this.getSystemPrompt(this._requestContext !== void 0 ? this._buildToolContext() : void 0);
158
154
  } } }).withContext("critical-rules", { provider: { get: async () => SECURITY_RULES_PROMPT } }).withContext("turn-protocol-rules", { provider: { get: async () => TURN_PROTOCOL_RULES_PROMPT } });
159
- const remoteSkills = this.getRemoteSkills();
160
- if (remoteSkills.length) if (this.env.SKILLS_BUCKET) configuredSession = configuredSession.withContext("skills", { provider: new R2SkillProvider(this.env.SKILLS_BUCKET, {
161
- prefix: "skills/",
162
- keys: remoteSkills
163
- }) });
164
- else console.error("[Agent] Connection rejected: Remote skills defined, but no SKILLS_BUCKET R2 binding found");
165
155
  const localSkills = this.getSkills();
166
156
  if (localSkills.length) configuredSession = configuredSession.withContext("local-skills", { provider: new LocalSkillProvider(localSkills) });
167
157
  return configuredSession.withCachedPrompt();
@@ -244,9 +234,6 @@ var Agent = class extends Think {
244
234
  * Returns the remote skills to be loaded from SKILLS_BUCKET.
245
235
  * @returns The remote skills to be loaded from SKILLS_BUCKET.
246
236
  */
247
- getRemoteSkills() {
248
- return [];
249
- }
250
237
  /**
251
238
  * Returns the skills to load for the agent.
252
239
  * @returns The skills to load for the agent.
@@ -572,51 +559,6 @@ var Assistant = class extends Agent$1 {
572
559
  subAgentName: this.agent.name
573
560
  });
574
561
  }
575
- /**
576
- * Binding of the legacy v1 chat Durable Object class, used to migrate a
577
- * user's v1 chats into facets the first time they connect. Set this on the
578
- * concrete subclass to enable lazy v1 -> v2 migration; leave undefined to
579
- * disable it (e.g. for greenfield deployments with no v1 data).
580
- */
581
- migrationBinding;
582
- /** In-flight migration, shared across concurrent connections to this DO. */
583
- _migrationPromise;
584
- /**
585
- * Runs the lazy v1 -> v2 migration for this user. Concurrent connections to
586
- * this DO share a single in-flight run. Idempotency across runs/restarts is
587
- * handled by `migrateUserFromV1` deleting each chat's v1 `conversations` row,
588
- * so an already-migrated chat is never re-enumerated.
589
- */
590
- async ensureMigrated() {
591
- if (!this.migrationBinding) return;
592
- this._migrationPromise ??= this.runMigration().finally(() => {
593
- this._migrationPromise = void 0;
594
- });
595
- await this._migrationPromise;
596
- }
597
- async runMigration() {
598
- if (!this.migrationBinding) return;
599
- try {
600
- const result = await migrateUserFromV1({
601
- sql: this.sql.bind(this),
602
- db: this.migrationBinding.db,
603
- userId: this.name,
604
- durableObjectBinding: this.migrationBinding.binding,
605
- createFacet: async (chatId) => {
606
- return await this.subAgent(this.agent, chatId);
607
- }
608
- });
609
- if (result.migrated > 0 || result.failed > 0) console.info("[Assistant] v1 -> v2 migration complete", {
610
- userId: this.name,
611
- ...result
612
- });
613
- } catch (error) {
614
- console.error("[Assistant] v1 -> v2 migration failed", {
615
- userId: this.name,
616
- error
617
- });
618
- }
619
- }
620
562
  @callable() async createChat() {
621
563
  const id = nanoid();
622
564
  const now = Date.now();
@@ -665,6 +607,52 @@ var Assistant = class extends Agent$1 {
665
607
  console.error("[Assistant] Failed to register in agent_registry", error);
666
608
  }
667
609
  }
610
+ /**
611
+ * Binding of the legacy v1 chat Durable Object class, used to migrate a
612
+ * user's v1 chats into facets the first time they connect. Set this on the
613
+ * concrete subclass to enable lazy v1 -> v2 migration; leave undefined to
614
+ * disable it (e.g. for greenfield deployments with no v1 data).
615
+ */
616
+ getMigrationBinding(_env) {}
617
+ /** In-flight migration, shared across concurrent connections to this DO. */
618
+ _migrationPromise;
619
+ /**
620
+ * Runs the lazy v1 -> v2 migration for this user. Concurrent connections to
621
+ * this DO share a single in-flight run. Idempotency across runs/restarts is
622
+ * handled by `migrateUserFromV1` deleting each chat's v1 `conversations` row,
623
+ * so an already-migrated chat is never re-enumerated.
624
+ */
625
+ async ensureMigrated() {
626
+ if (!this.getMigrationBinding(this.env)) return;
627
+ this._migrationPromise ??= this.runMigration().finally(() => {
628
+ this._migrationPromise = void 0;
629
+ });
630
+ await this._migrationPromise;
631
+ }
632
+ async runMigration() {
633
+ const migrationBinding = this.getMigrationBinding(this.env);
634
+ if (!migrationBinding) return;
635
+ try {
636
+ const result = await migrateUserFromV1({
637
+ sql: this.sql.bind(this),
638
+ db: migrationBinding.db,
639
+ userId: this.name,
640
+ durableObjectBinding: migrationBinding.binding,
641
+ createFacet: async (chatId) => {
642
+ return await this.subAgent(this.agent, chatId);
643
+ }
644
+ });
645
+ if (result.migrated > 0 || result.failed > 0) console.info("[Assistant] v1 -> v2 migration complete", {
646
+ userId: this.name,
647
+ ...result
648
+ });
649
+ } catch (error) {
650
+ console.error("[Assistant] v1 -> v2 migration failed", {
651
+ userId: this.name,
652
+ error
653
+ });
654
+ }
655
+ }
668
656
  };
669
657
  //#endregion
670
658
  //#region src/server/features/messages.ts
@@ -787,4 +775,9 @@ function skill(definition) {
787
775
  return definition;
788
776
  }
789
777
  //#endregion
778
+ //#region src/server/index.ts
779
+ function getCurrentToolContext() {
780
+ return getCurrentAgent().agent._lastBody;
781
+ }
782
+ //#endregion
790
783
  export { Agent, Assistant, ChatAgent, getCurrentToolContext, migrateUserFromV1, routeAgentRequest, skill, tool };
package/dist/v1.d.mts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { n as JwtAuthConfig, t as routeAgentRequest } from "./route-agent-request-DmwIOBJS.mjs";
2
- import { LanguageModel, StreamTextOnFinishCallback, ToolSet, UIMessage, generateText, streamText } from "ai";
3
2
  import { Agent as Agent$1, Connection, ConnectionContext } from "agents";
3
+ import { LanguageModel, StreamTextOnFinishCallback, ToolSet, UIMessage, generateText, streamText } from "ai";
4
4
  import { AIChatAgent, ChatResponseResult, OnChatMessageOptions } from "@cloudflare/ai-chat";
5
5
 
6
6
  //#region src/server/features/skills/index.d.ts
package/dist/v1.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  import { i as verifyJwt, n as createAgentTracer, r as extractTokenFromConnectRequest, t as routeAgentRequest } from "./route-agent-request-DKDvDYnR.mjs";
2
- import { Output, convertToModelMessages, generateText, jsonSchema, stepCountIs, streamText, tool } from "ai";
3
2
  import { Agent as Agent$1, callable, getCurrentAgent } from "agents";
3
+ import { Output, convertToModelMessages, generateText, jsonSchema, stepCountIs, streamText, tool } from "ai";
4
4
  import { AIChatAgent } from "@cloudflare/ai-chat";
5
5
  //#region src/server/features/skills/index.ts
6
6
  const TOOL_NAME_ACTIVATE_SKILL = "activate_skill";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@economic/agents",
3
- "version": "2.2.2",
3
+ "version": "2.2.4",
4
4
  "description": "A starter for creating a TypeScript package.",
5
5
  "license": "MIT",
6
6
  "bin": {