@arvo-tools/agentic 1.2.17 → 2.0.0

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.
Files changed (48) hide show
  1. package/dist/Agent/AgentDefaults.d.ts +4 -1
  2. package/dist/Agent/AgentDefaults.d.ts.map +1 -1
  3. package/dist/Agent/AgentDefaults.js +110 -73
  4. package/dist/Agent/AgentDefaults.js.map +1 -1
  5. package/dist/Agent/agentLoop.d.ts +19 -17
  6. package/dist/Agent/agentLoop.d.ts.map +1 -1
  7. package/dist/Agent/agentLoop.js +239 -120
  8. package/dist/Agent/agentLoop.js.map +1 -1
  9. package/dist/Agent/index.d.ts +91 -39
  10. package/dist/Agent/index.d.ts.map +1 -1
  11. package/dist/Agent/index.js +218 -146
  12. package/dist/Agent/index.js.map +1 -1
  13. package/dist/Agent/schema.d.ts +326 -22
  14. package/dist/Agent/schema.d.ts.map +1 -1
  15. package/dist/Agent/schema.js +23 -1
  16. package/dist/Agent/schema.js.map +1 -1
  17. package/dist/Agent/stream/schema.d.ts +39 -39
  18. package/dist/Agent/types.d.ts +170 -79
  19. package/dist/Agent/types.d.ts.map +1 -1
  20. package/dist/Agent/utils.d.ts +6 -0
  21. package/dist/Agent/utils.d.ts.map +1 -1
  22. package/dist/Agent/utils.js +10 -1
  23. package/dist/Agent/utils.js.map +1 -1
  24. package/dist/AgentTool/index.d.ts +2 -3
  25. package/dist/AgentTool/index.d.ts.map +1 -1
  26. package/dist/AgentTool/index.js +11 -7
  27. package/dist/AgentTool/index.js.map +1 -1
  28. package/dist/AgentTool/types.d.ts +27 -9
  29. package/dist/AgentTool/types.d.ts.map +1 -1
  30. package/dist/Integrations/MCPClient.d.ts +2 -2
  31. package/dist/Integrations/MCPClient.js +2 -2
  32. package/dist/Integrations/anthropic/index.d.ts.map +1 -1
  33. package/dist/Integrations/anthropic/index.js +3 -3
  34. package/dist/Integrations/anthropic/index.js.map +1 -1
  35. package/dist/Integrations/openai/index.d.ts.map +1 -1
  36. package/dist/Integrations/openai/index.js +3 -3
  37. package/dist/Integrations/openai/index.js.map +1 -1
  38. package/dist/Integrations/types.d.ts +89 -15
  39. package/dist/Integrations/types.d.ts.map +1 -1
  40. package/dist/Integrations/types.js +47 -0
  41. package/dist/Integrations/types.js.map +1 -1
  42. package/dist/SimplePermissionManager/contract.d.ts +4 -4
  43. package/dist/SimplePermissionManager/index.d.ts +12 -12
  44. package/dist/index.d.ts +3 -3
  45. package/dist/index.d.ts.map +1 -1
  46. package/dist/index.js +2 -1
  47. package/dist/index.js.map +1 -1
  48. package/package.json +6 -6
@@ -57,6 +57,17 @@ var __rest = (this && this.__rest) || function (s, e) {
57
57
  }
58
58
  return t;
59
59
  };
60
+ var __values = (this && this.__values) || function(o) {
61
+ var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
62
+ if (m) return m.call(o);
63
+ if (o && typeof o.length === "number") return {
64
+ next: function () {
65
+ if (o && i >= o.length) o = void 0;
66
+ return { value: o && o[i++], done: !o };
67
+ }
68
+ };
69
+ throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
70
+ };
60
71
  var __read = (this && this.__read) || function (o, n) {
61
72
  var m = typeof Symbol === "function" && o[Symbol.iterator];
62
73
  if (!m) return o;
@@ -82,23 +93,14 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
82
93
  }
83
94
  return to.concat(ar || Array.prototype.slice.call(from));
84
95
  };
85
- var __values = (this && this.__values) || function(o) {
86
- var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
87
- if (m) return m.call(o);
88
- if (o && typeof o.length === "number") return {
89
- next: function () {
90
- if (o && i >= o.length) o = void 0;
91
- return { value: o && o[i++], done: !o };
92
- }
93
- };
94
- throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
95
- };
96
96
  Object.defineProperty(exports, "__esModule", { value: true });
97
97
  exports.createArvoAgent = void 0;
98
98
  var arvo_core_1 = require("arvo-core");
99
99
  var arvo_event_handler_1 = require("arvo-event-handler");
100
100
  var uuid_1 = require("uuid");
101
+ var AgentDefaults_js_1 = require("./AgentDefaults.js");
101
102
  var agentLoop_js_1 = require("./agentLoop.js");
103
+ var schema_js_1 = require("./schema.js");
102
104
  var utils_js_1 = require("./stream/utils.js");
103
105
  var utils_js_2 = require("./utils.js");
104
106
  /**
@@ -109,19 +111,36 @@ var utils_js_2 = require("./utils.js");
109
111
  * consuming zero resources between event processing cycles while maintaining conversation
110
112
  * state in persistent memory.
111
113
  *
112
- * @remarks
113
- * The agent operates on a start-stop-resume execution model where it receives an event, invokes
114
- * the LLM with available tools (internal Typescript functions, MCP external sources, or Arvo services),
115
- * and either continues immediately for synchronous tools or suspends execution for service calls
116
- * until responses arrive. When the LLM requests multiple tools simultaneously, priority-based orchestration
117
- * ensures only the highest-priority batch executes (enabling "human-approval-first" patterns), while contract
118
- * versioning enforces that you provide complete handler implementations for all defined versions
119
- * (enabling safe evolution of prompts, models, and output schemas across v1, v2, etc.). The optional permission
120
- * manager adds deterministic authorization outside the LLM's control—blocked tools trigger permission request events,
121
- * the agent suspends until external approval, then retries with updated permissions, creating a security layer
122
- * immune to prompt injection.
123
-
124
- * @param param - Configuration object defining the agent's contracts, tools, memory backend,
114
+ * @remark
115
+ * **Execution Model:**
116
+ * The agent follows a start-stop-resume pattern. On initialization, it builds context from
117
+ * the input event, then enters a ReAct (Reason+Act) cognitive loop. When calling Arvo services,
118
+ * it persists state to memory and suspends, enabling any worker to resume it later. This
119
+ * eliminates long-running processes and enables horizontal scaling.
120
+ *
121
+ * **Tool Ecosystem:**
122
+ * - **Internal Tools:** Synchronous functions for fast, CPU-bound operations.
123
+ * - **MCP Tools:** External tools via Model Context Protocol (filesystem, databases, APIs).
124
+ * - **Arvo Services:** Asynchronous event-driven services that trigger suspension.
125
+ *
126
+ * Both Internal and MCP tools execute synchronously within the loop, while Arvo service calls
127
+ * cause the agent to emit events and suspend until responses arrive.
128
+ *
129
+ * **Priority Batch Execution:**
130
+ * When the LLM requests multiple tools, they are sorted by priority. Only the highest-priority
131
+ * batch executes; lower-priority calls are dropped. This enforces safety guardrails (e.g.,
132
+ * requiring human approval before destructive actions).
133
+ *
134
+ * **Permission Management:**
135
+ * Tools can be placed under permission policy via `explicitPermissionRequired`. The permission
136
+ * manager evaluates each tool call as APPROVED (execute), DENIED (block permanently), or
137
+ * REQUESTABLE (block and emit permission request). Permission state persists across suspensions.
138
+ *
139
+ * **Self-Correction:**
140
+ * If the LLM's outputs fails contract schema validation, the error is fed back and the
141
+ * agent retries, enabling automatic repair of malformed responses or tools calls.
142
+ *
143
+ * @param param - Configuration object defining the agent's contracts, tools, memory backend,
125
144
  * LLM integration, and version-specific behavior handlers.
126
145
  *
127
146
  * @returns An ArvoResumable instance that participates in the event fabric as a standard
@@ -149,16 +168,17 @@ var utils_js_2 = require("./utils.js");
149
168
  * name: 'check_time',
150
169
  * description: 'Returns current server time in ISO format',
151
170
  * input: z.object({}),
152
- * output: z.object({ time: z.string() }),
153
- * fn: async () => ({ time: new Date().toISOString() })
171
+ * fn: async () => ({ data: { time: new Date().toISOString() } })
154
172
  * })
155
173
  * },
156
- * llm: openaiLLMIntegration(new OpenAI(), { model: 'gpt-4o' }),
174
+ * inferenceConfig: {
175
+ * llm: openaiLLMIntegration(new OpenAI(), { model: 'gpt-4o' }),
176
+ * },
157
177
  * memory: memory,
158
- * permissionManager: new ToolPermissionManager(),
178
+ * permissionManager: new SimplePermissionManager(),
159
179
  * handler: {
160
180
  * '1.0.0': {
161
- * permissionPolicy: async ({ services }) => [
181
+ * explicitPermissionRequired: async ({ services }) => [
162
182
  * services.billing.name // Require permission for billing calls
163
183
  * ],
164
184
  * context: AgentDefaults.CONTEXT_BUILDER(async ({ tools }) =>
@@ -172,19 +192,34 @@ var utils_js_2 = require("./utils.js");
172
192
  * ```
173
193
  */
174
194
  var createArvoAgent = function (_a) {
175
- var _b;
176
- var contracts = _a.contracts, memory = _a.memory, handler = _a.handler, llm = _a.llm, mcp = _a.mcp, _c = _a.maxToolInteractions, maxToolInteractions = _c === void 0 ? 5 : _c, _d = _a.llmResponseType, llmResponseType = _d === void 0 ? 'text' : _d, tools = _a.tools, onStream = _a.onStream, permissionManager = _a.permissionManager, defaultEventEmissionDomains = _a.defaultEventEmissionDomains;
177
- var serviceContracts = Object.fromEntries(Object.entries(contracts.services).map(function (_a) {
178
- var _b = __read(_a, 2), key = _b[0], contract = _b[1].contract;
179
- return [key, contract];
180
- }));
195
+ var e_1, _b, _c;
196
+ var contracts = _a.contracts, memory = _a.memory, handler = _a.handler, inferenceConfig = _a.inferenceConfig, mcp = _a.mcp, _d = _a.maxAgentCycles, maxAgentCycles = _d === void 0 ? 5 : _d, tools = _a.tools, onStream = _a.onStream, permissionManager = _a.permissionManager, defaultEventEmissionDomains = _a.defaultEventEmissionDomains;
197
+ // biome-ignore lint/suspicious/noExplicitAny: Needs to be general
198
+ var serviceContracts = {};
199
+ var serviceTransformers = {};
200
+ try {
201
+ for (var _e = __values(Object.entries(contracts.services)), _f = _e.next(); !_f.done; _f = _e.next()) {
202
+ var _g = __read(_f.value, 2), key = _g[0], _h = _g[1], contract = _h.contract, transformer = _h.transformer;
203
+ serviceContracts[key] = contract;
204
+ if (transformer) {
205
+ serviceTransformers[contract.accepts.type] = transformer;
206
+ }
207
+ }
208
+ }
209
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
210
+ finally {
211
+ try {
212
+ if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
213
+ }
214
+ finally { if (e_1) throw e_1.error; }
215
+ }
181
216
  var serviceTypeToDomainMap = Object.fromEntries(Object.values(contracts.services)
182
217
  .filter(function (item) { var _a; return (_a = item.domains) === null || _a === void 0 ? void 0 : _a.length; })
183
218
  .map(function (item) { return [item.contract.accepts.type, item.domains]; }));
184
219
  if ((Object.keys(serviceContracts).length > 0 || permissionManager) && !memory) {
185
220
  // If permissions manager or service contracts are defined and
186
- // memory is not defined then that is not allowed at by adding
187
- // these it will automatically imply that sometime in its lifecycle,
221
+ // memory is not defined then that is not allowed as by adding
222
+ // the permission manager it will automatically imply that sometime in its lifecycle,
188
223
  // the Agent will need event-driven coordinations and will create a
189
224
  // suspension boundary
190
225
  throw new arvo_event_handler_1.ConfigViolation((0, arvo_core_1.cleanString)("\n ArvoAgent<".concat(contracts.self.type, "> configuration error.\n\n This agent is configured with capabilities that can introduce a suspension boundary\n (service contracts and/or a permission manager), but no memory backend was provided.\n\n Why this matters:\n - Service contracts and permission workflows may cause the agent to emit an event and suspend.\n - Suspending requires persisting conversation state so the agent can resume correctly.\n\n How to fix:\n - Provide a memory backend (e.g. SimpleMachineMemory or any implementation of IMachineMemory), or\n - Remove service contracts and the permission manager if this agent is intended\n to run as a single synchronous execution without suspension.\n ")));
@@ -192,7 +227,7 @@ var createArvoAgent = function (_a) {
192
227
  return (0, arvo_event_handler_1.createArvoResumable)({
193
228
  contracts: {
194
229
  self: contracts.self,
195
- services: __assign(__assign({}, serviceContracts), (permissionManager ? (_b = {}, _b["pm-".concat((0, uuid_1.v4)())] = permissionManager.contract, _b) : {})),
230
+ services: __assign(__assign({}, serviceContracts), (permissionManager ? (_c = {}, _c["pm-".concat((0, uuid_1.v4)())] = permissionManager.contract, _c) : {})),
196
231
  },
197
232
  memory: memory !== null && memory !== void 0 ? memory : new arvo_event_handler_1.SimpleMachineMemory(),
198
233
  types: {
@@ -203,13 +238,14 @@ var createArvoAgent = function (_a) {
203
238
  handler: Object.fromEntries(Object.keys(contracts.self.versions).map(function (ver) { return [
204
239
  ver,
205
240
  (function (_a) { return __awaiter(void 0, [_a], void 0, function (_b) {
206
- var otelInfo, agentEventStreamer, contextBuilder, outputBuilder, thisVersionLlmIntegration, versionLlmResponseType, selfVersionedContract, outputFormat, permissionManagerContext, serviceTools, mcpTools, internalTools, permissionPolicy, toolInteraction, _c, parentSubject$$, inputData, llmContext, response_1, resumableContextToPersist_1, resumedContext, messages, _d, _e, _f, toolUseId, _g, type, data, response_2, resumableContextToPersist_2, e_1;
241
+ var context, otelInfo, agentEventStreamer, contextBuilder, outputBuilder, thisVersionLlmIntegration, versionLlmResponseType, selfVersionedContract, outputFormat, permissionManagerContext, preInferenceHook, postInferenceHook, serviceTools, mcpTools, internalTools, permissionPolicy, agentCycles, _c, parentSubject$$, inputData, llmContext, response_1, resumableContextToPersist_1, resumedContext, messages, _d, _e, _f, toolUseId, _g, type, responseEventType, data, transformedResult, e_2_1, response_2, resumableContextToPersist_2, e_3;
207
242
  var e_2, _h;
208
- var _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24;
209
- var span = _b.span, input = _b.input, context = _b.context, service = _b.service;
210
- return __generator(this, function (_25) {
211
- switch (_25.label) {
243
+ var _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, _31, _32, _33, _34, _35, _36, _37, _38, _39, _40, _41, _42, _43;
244
+ var span = _b.span, input = _b.input, _context = _b.context, service = _b.service;
245
+ return __generator(this, function (_44) {
246
+ switch (_44.label) {
212
247
  case 0:
248
+ context = _context ? schema_js_1.AgentStateSchema.parse(_context) : null;
213
249
  otelInfo = {
214
250
  span: span,
215
251
  headers: (0, arvo_core_1.getOtelHeaderFromSpan)(span),
@@ -232,37 +268,39 @@ var createArvoAgent = function (_a) {
232
268
  (0, arvo_core_1.exceptionToSpan)(e, span);
233
269
  }
234
270
  };
235
- _25.label = 1;
271
+ _44.label = 1;
236
272
  case 1:
237
- _25.trys.push([1, 14, 16, 18]);
238
- contextBuilder = (_j = handler[ver]) === null || _j === void 0 ? void 0 : _j.context;
239
- outputBuilder = (_k = handler[ver]) === null || _k === void 0 ? void 0 : _k.output;
240
- thisVersionLlmIntegration = (_m = (_l = handler[ver]) === null || _l === void 0 ? void 0 : _l.llm) !== null && _m !== void 0 ? _m : llm;
241
- versionLlmResponseType = (_p = (_o = handler[ver]) === null || _o === void 0 ? void 0 : _o.llmResponseType) !== null && _p !== void 0 ? _p : llmResponseType;
273
+ _44.trys.push([1, 23, 25, 27]);
274
+ contextBuilder = (_k = (_j = handler[ver]) === null || _j === void 0 ? void 0 : _j.context) !== null && _k !== void 0 ? _k : AgentDefaults_js_1.AgentDefaults.CONTEXT_BUILDER();
275
+ outputBuilder = (_m = (_l = handler[ver]) === null || _l === void 0 ? void 0 : _l.output) !== null && _m !== void 0 ? _m : AgentDefaults_js_1.AgentDefaults.OUTPUT_BUILDER;
276
+ thisVersionLlmIntegration = (_q = (_p = (_o = handler[ver]) === null || _o === void 0 ? void 0 : _o.inferenceConfig) === null || _p === void 0 ? void 0 : _p.llm) !== null && _q !== void 0 ? _q : inferenceConfig === null || inferenceConfig === void 0 ? void 0 : inferenceConfig.llm;
277
+ versionLlmResponseType = (_u = (_t = (_s = (_r = handler[ver]) === null || _r === void 0 ? void 0 : _r.inferenceConfig) === null || _s === void 0 ? void 0 : _s.responseType) !== null && _t !== void 0 ? _t : inferenceConfig.responseType) !== null && _u !== void 0 ? _u : 'text';
242
278
  selfVersionedContract = contracts.self.version(ver);
243
279
  outputFormat = selfVersionedContract.emits[selfVersionedContract.metadata.completeEventType];
244
280
  permissionManagerContext = {
245
- subject: (_r = (_q = context === null || context === void 0 ? void 0 : context.currentSubject) !== null && _q !== void 0 ? _q : input === null || input === void 0 ? void 0 : input.subject) !== null && _r !== void 0 ? _r : 'unknown',
246
- accesscontrol: (_t = (_s = context === null || context === void 0 ? void 0 : context.initEventAccessControl) !== null && _s !== void 0 ? _s : input === null || input === void 0 ? void 0 : input.accesscontrol) !== null && _t !== void 0 ? _t : null,
281
+ subject: (_w = (_v = context === null || context === void 0 ? void 0 : context.currentSubject) !== null && _v !== void 0 ? _v : input === null || input === void 0 ? void 0 : input.subject) !== null && _w !== void 0 ? _w : 'unknown',
282
+ accesscontrol: (_y = (_x = context === null || context === void 0 ? void 0 : context.initEventAccessControl) !== null && _x !== void 0 ? _x : input === null || input === void 0 ? void 0 : input.accesscontrol) !== null && _y !== void 0 ? _y : null,
247
283
  name: contracts.self.type,
248
284
  };
285
+ preInferenceHook = (_4 = (_2 = (_1 = (_0 = (_z = handler[ver]) === null || _z === void 0 ? void 0 : _z.inferenceConfig) === null || _0 === void 0 ? void 0 : _0.hooks) === null || _1 === void 0 ? void 0 : _1.preInference) !== null && _2 !== void 0 ? _2 : (_3 = inferenceConfig === null || inferenceConfig === void 0 ? void 0 : inferenceConfig.hooks) === null || _3 === void 0 ? void 0 : _3.preInference) !== null && _4 !== void 0 ? _4 : null;
286
+ postInferenceHook = (_10 = (_8 = (_7 = (_6 = (_5 = handler[ver]) === null || _5 === void 0 ? void 0 : _5.inferenceConfig) === null || _6 === void 0 ? void 0 : _6.hooks) === null || _7 === void 0 ? void 0 : _7.postInference) !== null && _8 !== void 0 ? _8 : (_9 = inferenceConfig === null || inferenceConfig === void 0 ? void 0 : inferenceConfig.hooks) === null || _9 === void 0 ? void 0 : _9.postInference) !== null && _10 !== void 0 ? _10 : null;
249
287
  return [4 /*yield*/, (mcp === null || mcp === void 0 ? void 0 : mcp.connect({ otelInfo: otelInfo }))];
250
288
  case 2:
251
- _25.sent();
289
+ _44.sent();
252
290
  serviceTools = (0, utils_js_2.generateServiceToolDefinitions)(contracts.services);
253
291
  return [4 /*yield*/, (0, utils_js_2.generateMcpToolDefinitions)(mcp !== null && mcp !== void 0 ? mcp : null, { otelInfo: otelInfo })];
254
292
  case 3:
255
- mcpTools = _25.sent();
293
+ mcpTools = _44.sent();
256
294
  internalTools = (0, utils_js_2.generateAgentInternalToolDefinitions)(tools !== null && tools !== void 0 ? tools : {});
257
- return [4 /*yield*/, ((_v = (_u = handler[ver]) === null || _u === void 0 ? void 0 : _u.explicitPermissionRequired) === null || _v === void 0 ? void 0 : _v.call(_u, {
295
+ return [4 /*yield*/, ((_12 = (_11 = handler[ver]) === null || _11 === void 0 ? void 0 : _11.explicitPermissionRequired) === null || _12 === void 0 ? void 0 : _12.call(_11, {
258
296
  services: serviceTools,
259
297
  mcp: mcpTools,
260
298
  tools: internalTools,
261
299
  }))];
262
300
  case 4:
263
- permissionPolicy = (_w = (_25.sent())) !== null && _w !== void 0 ? _w : [];
264
- toolInteraction = (_x = context === null || context === void 0 ? void 0 : context.toolInteractions) !== null && _x !== void 0 ? _x : {
265
- max: maxToolInteractions,
301
+ permissionPolicy = (_13 = (_44.sent())) !== null && _13 !== void 0 ? _13 : [];
302
+ agentCycles = (_14 = context === null || context === void 0 ? void 0 : context.agentCycles) !== null && _14 !== void 0 ? _14 : {
303
+ max: maxAgentCycles,
266
304
  current: 0,
267
305
  };
268
306
  if (!input) return [3 /*break*/, 8];
@@ -275,12 +313,12 @@ var createArvoAgent = function (_a) {
275
313
  selfContract: selfVersionedContract,
276
314
  })];
277
315
  case 5:
278
- llmContext = (_y = (_25.sent())) !== null && _y !== void 0 ? _y : null;
316
+ llmContext = (_15 = (_44.sent())) !== null && _15 !== void 0 ? _15 : null;
279
317
  return [4 /*yield*/, (0, agentLoop_js_1.agentLoop)({
280
318
  permissionManagerContext: permissionManagerContext,
281
319
  initLifecycle: 'init',
282
- system: (_z = llmContext === null || llmContext === void 0 ? void 0 : llmContext.system) !== null && _z !== void 0 ? _z : null,
283
- messages: (((_0 = llmContext === null || llmContext === void 0 ? void 0 : llmContext.messages) === null || _0 === void 0 ? void 0 : _0.length)
320
+ system: (_16 = llmContext === null || llmContext === void 0 ? void 0 : llmContext.system) !== null && _16 !== void 0 ? _16 : null,
321
+ messages: (((_17 = llmContext === null || llmContext === void 0 ? void 0 : llmContext.messages) === null || _17 === void 0 ? void 0 : _17.length)
284
322
  ? llmContext.messages
285
323
  : [
286
324
  {
@@ -289,13 +327,15 @@ var createArvoAgent = function (_a) {
289
327
  seenCount: 0,
290
328
  },
291
329
  ]).map(function (item) { var _a; return (__assign(__assign({}, item), { seenCount: (_a = item.seenCount) !== null && _a !== void 0 ? _a : 0 })); }),
292
- tools: Object.values(__assign(__assign(__assign({}, mcpTools), serviceTools), internalTools)),
330
+ tools: (0, utils_js_2.applyToolEnablement)(Object.values(__assign(__assign(__assign({}, mcpTools), serviceTools), internalTools)), (_18 = llmContext === null || llmContext === void 0 ? void 0 : llmContext.enabledTools) !== null && _18 !== void 0 ? _18 : {}),
331
+ preInferenceHook: preInferenceHook,
332
+ postInferenceHook: postInferenceHook,
293
333
  outputFormat: outputFormat,
294
334
  outputBuilder: outputBuilder,
295
335
  llmResponseType: versionLlmResponseType,
296
336
  llm: thisVersionLlmIntegration,
297
337
  mcp: mcp !== null && mcp !== void 0 ? mcp : null,
298
- toolInteraction: toolInteraction,
338
+ agentCycles: agentCycles,
299
339
  currentTotalExecutionUnits: 0,
300
340
  onStream: agentEventStreamer,
301
341
  currentTotalUsageTokens: {
@@ -306,16 +346,17 @@ var createArvoAgent = function (_a) {
306
346
  permissionPolicy: permissionPolicy,
307
347
  }, { otelInfo: otelInfo })];
308
348
  case 6:
309
- response_1 = _25.sent();
349
+ response_1 = _44.sent();
310
350
  resumableContextToPersist_1 = {
311
- initEventAccessControl: (_1 = input.accesscontrol) !== null && _1 !== void 0 ? _1 : null,
351
+ initEventAccessControl: (_19 = input.accesscontrol) !== null && _19 !== void 0 ? _19 : null,
312
352
  currentSubject: input.subject,
313
- system: (_2 = llmContext === null || llmContext === void 0 ? void 0 : llmContext.system) !== null && _2 !== void 0 ? _2 : null,
353
+ enabledTools: (_20 = llmContext === null || llmContext === void 0 ? void 0 : llmContext.enabledTools) !== null && _20 !== void 0 ? _20 : {},
354
+ system: (_21 = llmContext === null || llmContext === void 0 ? void 0 : llmContext.system) !== null && _21 !== void 0 ? _21 : null,
314
355
  messages: response_1.messages,
315
- toolInteractions: response_1.toolInteractions,
316
- awaitingToolCalls: Object.fromEntries(((_3 = response_1.toolCalls) !== null && _3 !== void 0 ? _3 : []).map(function (item) { return [
356
+ agentCycles: response_1.agentCycles,
357
+ awaitingToolCalls: Object.fromEntries(((_22 = response_1.toolCalls) !== null && _22 !== void 0 ? _22 : []).map(function (item) { return [
317
358
  item.toolUseId,
318
- { type: item.name, data: null },
359
+ { type: item.name, responseEventType: null, data: null },
319
360
  ]; })),
320
361
  totalExecutionUnits: response_1.executionUnits,
321
362
  totalTokenUsage: response_1.tokenUsage,
@@ -337,14 +378,14 @@ var createArvoAgent = function (_a) {
337
378
  }),
338
379
  }];
339
380
  }
340
- return [4 /*yield*/, ((_4 = permissionManager === null || permissionManager === void 0 ? void 0 : permissionManager.cleanup) === null || _4 === void 0 ? void 0 : _4.call(permissionManager, {
381
+ return [4 /*yield*/, ((_23 = permissionManager === null || permissionManager === void 0 ? void 0 : permissionManager.cleanup) === null || _23 === void 0 ? void 0 : _23.call(permissionManager, {
341
382
  source: permissionManagerContext,
342
383
  config: { otelInfo: otelInfo },
343
384
  }))];
344
385
  case 7:
345
- _25.sent();
386
+ _44.sent();
346
387
  return [2 /*return*/, {
347
- context: resumableContextToPersist_1,
388
+ context: schema_js_1.AgentStateSchema.parse(resumableContextToPersist_1),
348
389
  output: __assign({ __executionunits: response_1.executionUnits }, response_1.output),
349
390
  }];
350
391
  case 8:
@@ -355,7 +396,9 @@ var createArvoAgent = function (_a) {
355
396
  if (!((service === null || service === void 0 ? void 0 : service.parentid) && resumedContext.awaitingToolCalls[service.parentid])) return [3 /*break*/, 11];
356
397
  // biome-ignore lint/style/noNonNullAssertion: It cannot be null. The if clause does already
357
398
  resumedContext.awaitingToolCalls[service.parentid].data = service.data;
358
- if (!(service.type === ((_7 = (_6 = (_5 = permissionManager === null || permissionManager === void 0 ? void 0 : permissionManager.contract) === null || _5 === void 0 ? void 0 : _5.emitList) === null || _6 === void 0 ? void 0 : _6[0]) === null || _7 === void 0 ? void 0 : _7.type))) return [3 /*break*/, 10];
399
+ // biome-ignore lint/style/noNonNullAssertion: It cannot be null. The if clause does already
400
+ resumedContext.awaitingToolCalls[service.parentid].responseEventType = service.type;
401
+ if (!(service.type === ((_26 = (_25 = (_24 = permissionManager === null || permissionManager === void 0 ? void 0 : permissionManager.contract) === null || _24 === void 0 ? void 0 : _24.emitList) === null || _25 === void 0 ? void 0 : _25[0]) === null || _26 === void 0 ? void 0 : _26.type))) return [3 /*break*/, 10];
359
402
  return [4 /*yield*/, (permissionManager === null || permissionManager === void 0 ? void 0 : permissionManager.set({
360
403
  source: permissionManagerContext,
361
404
  // biome-ignore lint/suspicious/noExplicitAny: Type casting here is weird
@@ -363,73 +406,102 @@ var createArvoAgent = function (_a) {
363
406
  config: { otelInfo: otelInfo },
364
407
  }))];
365
408
  case 9:
366
- _25.sent();
367
- _25.label = 10;
409
+ _44.sent();
410
+ _44.label = 10;
368
411
  case 10:
369
- if (service.type === ((_9 = (_8 = permissionManager === null || permissionManager === void 0 ? void 0 : permissionManager.contract) === null || _8 === void 0 ? void 0 : _8.systemError) === null || _9 === void 0 ? void 0 : _9.type)) {
370
- throw new Error((0, arvo_core_1.cleanString)("\n [Critical] The agent's attempt to request permission via ".concat((_11 = (_10 = permissionManager === null || permissionManager === void 0 ? void 0 : permissionManager.contract) === null || _10 === void 0 ? void 0 : _10.accepts) === null || _11 === void 0 ? void 0 : _11.type, "\n failed with error: ").concat(JSON.stringify(service.data), "\n ")));
412
+ if (service.type === ((_28 = (_27 = permissionManager === null || permissionManager === void 0 ? void 0 : permissionManager.contract) === null || _27 === void 0 ? void 0 : _27.systemError) === null || _28 === void 0 ? void 0 : _28.type)) {
413
+ throw new Error((0, arvo_core_1.cleanString)("\n [Critical] The agent's attempt to request permission via ".concat((_30 = (_29 = permissionManager === null || permissionManager === void 0 ? void 0 : permissionManager.contract) === null || _29 === void 0 ? void 0 : _29.accepts) === null || _30 === void 0 ? void 0 : _30.type, "\n failed with error: ").concat(JSON.stringify(service.data), "\n ")));
371
414
  }
372
- _25.label = 11;
415
+ _44.label = 11;
373
416
  case 11:
374
417
  if (Object.values(resumedContext.awaitingToolCalls).some(function (item) { return item.data === null; })) {
375
- return [2 /*return*/, { context: resumedContext }];
418
+ return [2 /*return*/, { context: schema_js_1.AgentStateSchema.parse(resumedContext) }];
376
419
  }
377
420
  messages = __spreadArray([], __read(resumedContext.messages), false);
378
- try {
379
- for (_d = __values(Object.entries(resumedContext.awaitingToolCalls)), _e = _d.next(); !_e.done; _e = _d.next()) {
380
- _f = __read(_e.value, 2), toolUseId = _f[0], _g = _f[1], type = _g.type, data = _g.data;
381
- if (type === ((_13 = (_12 = permissionManager === null || permissionManager === void 0 ? void 0 : permissionManager.contract) === null || _12 === void 0 ? void 0 : _12.accepts) === null || _13 === void 0 ? void 0 : _13.type)) {
382
- messages.push({
383
- role: 'user',
384
- content: {
385
- type: 'text',
386
- content: "The response of the permission request. ".concat(JSON.stringify(data !== null && data !== void 0 ? data : {})),
387
- },
388
- seenCount: 0,
389
- });
390
- continue;
391
- }
392
- messages.push({
393
- role: 'user',
394
- content: {
395
- type: 'tool_result',
396
- toolUseId: toolUseId,
397
- content: JSON.stringify(data !== null && data !== void 0 ? data : {}),
398
- },
399
- seenCount: 0,
400
- });
401
- }
421
+ _44.label = 12;
422
+ case 12:
423
+ _44.trys.push([12, 18, 19, 20]);
424
+ _d = __values(Object.entries(resumedContext.awaitingToolCalls)), _e = _d.next();
425
+ _44.label = 13;
426
+ case 13:
427
+ if (!!_e.done) return [3 /*break*/, 17];
428
+ _f = __read(_e.value, 2), toolUseId = _f[0], _g = _f[1], type = _g.type, responseEventType = _g.responseEventType, data = _g.data;
429
+ if (type === ((_32 = (_31 = permissionManager === null || permissionManager === void 0 ? void 0 : permissionManager.contract) === null || _31 === void 0 ? void 0 : _31.accepts) === null || _32 === void 0 ? void 0 : _32.type)) {
430
+ messages.push({
431
+ role: 'user',
432
+ content: {
433
+ type: 'text',
434
+ content: "The response of the permission request. ".concat(JSON.stringify(data !== null && data !== void 0 ? data : {})),
435
+ },
436
+ seenCount: 0,
437
+ });
438
+ return [3 /*break*/, 16];
402
439
  }
403
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
404
- finally {
405
- try {
406
- if (_e && !_e.done && (_h = _d.return)) _h.call(_d);
407
- }
408
- finally { if (e_2) throw e_2.error; }
440
+ if (!(serviceTransformers[type] && responseEventType && data)) return [3 /*break*/, 15];
441
+ return [4 /*yield*/, serviceTransformers[type]({
442
+ type: responseEventType,
443
+ data: data,
444
+ toolUseId: toolUseId,
445
+ })];
446
+ case 14:
447
+ transformedResult = _44.sent();
448
+ if (Array.isArray(transformedResult)) {
449
+ messages = __spreadArray(__spreadArray([], __read(messages), false), __read(transformedResult), false);
409
450
  }
410
- return [4 /*yield*/, (0, agentLoop_js_1.agentLoop)({
411
- permissionManagerContext: permissionManagerContext,
412
- initLifecycle: 'tool_result',
413
- system: (_14 = resumedContext.system) !== null && _14 !== void 0 ? _14 : null,
414
- messages: messages,
415
- tools: Object.values(__assign(__assign(__assign({}, mcpTools), serviceTools), internalTools)),
416
- outputFormat: outputFormat,
417
- outputBuilder: outputBuilder,
418
- llmResponseType: versionLlmResponseType,
419
- llm: thisVersionLlmIntegration,
420
- mcp: mcp !== null && mcp !== void 0 ? mcp : null,
421
- toolInteraction: toolInteraction,
422
- currentTotalExecutionUnits: resumedContext.totalExecutionUnits,
423
- onStream: agentEventStreamer,
424
- currentTotalUsageTokens: resumedContext.totalTokenUsage,
425
- permissionManager: permissionManager !== null && permissionManager !== void 0 ? permissionManager : null,
426
- permissionPolicy: permissionPolicy,
427
- }, { otelInfo: otelInfo })];
428
- case 12:
429
- response_2 = _25.sent();
430
- resumableContextToPersist_2 = __assign(__assign({}, resumedContext), { messages: response_2.messages, toolInteractions: response_2.toolInteractions, awaitingToolCalls: Object.fromEntries(((_15 = response_2.toolCalls) !== null && _15 !== void 0 ? _15 : []).map(function (item) { return [
451
+ else {
452
+ messages.push(transformedResult);
453
+ }
454
+ return [3 /*break*/, 16];
455
+ case 15:
456
+ messages.push({
457
+ role: 'user',
458
+ content: {
459
+ type: 'tool_result',
460
+ toolUseId: toolUseId,
461
+ content: JSON.stringify(data !== null && data !== void 0 ? data : {}),
462
+ },
463
+ seenCount: 0,
464
+ });
465
+ _44.label = 16;
466
+ case 16:
467
+ _e = _d.next();
468
+ return [3 /*break*/, 13];
469
+ case 17: return [3 /*break*/, 20];
470
+ case 18:
471
+ e_2_1 = _44.sent();
472
+ e_2 = { error: e_2_1 };
473
+ return [3 /*break*/, 20];
474
+ case 19:
475
+ try {
476
+ if (_e && !_e.done && (_h = _d.return)) _h.call(_d);
477
+ }
478
+ finally { if (e_2) throw e_2.error; }
479
+ return [7 /*endfinally*/];
480
+ case 20: return [4 /*yield*/, (0, agentLoop_js_1.agentLoop)({
481
+ permissionManagerContext: permissionManagerContext,
482
+ initLifecycle: 'tool_result',
483
+ system: (_33 = resumedContext.system) !== null && _33 !== void 0 ? _33 : null,
484
+ messages: messages,
485
+ tools: (0, utils_js_2.applyToolEnablement)(Object.values(__assign(__assign(__assign({}, mcpTools), serviceTools), internalTools)), resumedContext.enabledTools),
486
+ outputFormat: outputFormat,
487
+ preInferenceHook: preInferenceHook,
488
+ postInferenceHook: postInferenceHook,
489
+ outputBuilder: outputBuilder,
490
+ llmResponseType: versionLlmResponseType,
491
+ llm: thisVersionLlmIntegration,
492
+ mcp: mcp !== null && mcp !== void 0 ? mcp : null,
493
+ agentCycles: agentCycles,
494
+ currentTotalExecutionUnits: resumedContext.totalExecutionUnits,
495
+ onStream: agentEventStreamer,
496
+ currentTotalUsageTokens: resumedContext.totalTokenUsage,
497
+ permissionManager: permissionManager !== null && permissionManager !== void 0 ? permissionManager : null,
498
+ permissionPolicy: permissionPolicy,
499
+ }, { otelInfo: otelInfo })];
500
+ case 21:
501
+ response_2 = _44.sent();
502
+ resumableContextToPersist_2 = __assign(__assign({}, resumedContext), { messages: response_2.messages, agentCycles: response_2.agentCycles, awaitingToolCalls: Object.fromEntries(((_34 = response_2.toolCalls) !== null && _34 !== void 0 ? _34 : []).map(function (item) { return [
431
503
  item.toolUseId,
432
- { type: item.name, data: null },
504
+ { type: item.name, data: null, responseEventType: null },
433
505
  ]; })), totalExecutionUnits: response_2.executionUnits, totalTokenUsage: response_2.tokenUsage });
434
506
  if (response_2.toolCalls) {
435
507
  return [2 /*return*/, {
@@ -448,36 +520,36 @@ var createArvoAgent = function (_a) {
448
520
  }),
449
521
  }];
450
522
  }
451
- return [4 /*yield*/, ((_16 = permissionManager === null || permissionManager === void 0 ? void 0 : permissionManager.cleanup) === null || _16 === void 0 ? void 0 : _16.call(permissionManager, {
523
+ return [4 /*yield*/, ((_35 = permissionManager === null || permissionManager === void 0 ? void 0 : permissionManager.cleanup) === null || _35 === void 0 ? void 0 : _35.call(permissionManager, {
452
524
  source: permissionManagerContext,
453
525
  config: { otelInfo: otelInfo },
454
526
  }))];
455
- case 13:
456
- _25.sent();
527
+ case 22:
528
+ _44.sent();
457
529
  return [2 /*return*/, {
458
- context: resumableContextToPersist_2,
530
+ context: schema_js_1.AgentStateSchema.parse(resumableContextToPersist_2),
459
531
  output: __assign({ __executionunits: response_2.executionUnits }, response_2.output),
460
532
  }];
461
- case 14:
462
- e_1 = _25.sent();
533
+ case 23:
534
+ e_3 = _44.sent();
463
535
  // Add correct otelinfo object here
464
- return [4 /*yield*/, ((_17 = permissionManager === null || permissionManager === void 0 ? void 0 : permissionManager.cleanup) === null || _17 === void 0 ? void 0 : _17.call(permissionManager, {
536
+ return [4 /*yield*/, ((_36 = permissionManager === null || permissionManager === void 0 ? void 0 : permissionManager.cleanup) === null || _36 === void 0 ? void 0 : _36.call(permissionManager, {
465
537
  source: {
466
- subject: (_20 = (_19 = (_18 = context === null || context === void 0 ? void 0 : context.currentSubject) !== null && _18 !== void 0 ? _18 : input === null || input === void 0 ? void 0 : input.subject) !== null && _19 !== void 0 ? _19 : service === null || service === void 0 ? void 0 : service.subject) !== null && _20 !== void 0 ? _20 : 'unknown',
467
- accesscontrol: (_23 = (_22 = (_21 = context === null || context === void 0 ? void 0 : context.initEventAccessControl) !== null && _21 !== void 0 ? _21 : input === null || input === void 0 ? void 0 : input.accesscontrol) !== null && _22 !== void 0 ? _22 : service === null || service === void 0 ? void 0 : service.accesscontrol) !== null && _23 !== void 0 ? _23 : null,
538
+ subject: (_39 = (_38 = (_37 = context === null || context === void 0 ? void 0 : context.currentSubject) !== null && _37 !== void 0 ? _37 : input === null || input === void 0 ? void 0 : input.subject) !== null && _38 !== void 0 ? _38 : service === null || service === void 0 ? void 0 : service.subject) !== null && _39 !== void 0 ? _39 : 'unknown',
539
+ accesscontrol: (_42 = (_41 = (_40 = context === null || context === void 0 ? void 0 : context.initEventAccessControl) !== null && _40 !== void 0 ? _40 : input === null || input === void 0 ? void 0 : input.accesscontrol) !== null && _41 !== void 0 ? _41 : service === null || service === void 0 ? void 0 : service.accesscontrol) !== null && _42 !== void 0 ? _42 : null,
468
540
  name: contracts.self.type,
469
541
  },
470
542
  config: { otelInfo: otelInfo },
471
543
  }))];
472
- case 15:
544
+ case 24:
473
545
  // Add correct otelinfo object here
474
- _25.sent();
475
- throw e_1;
476
- case 16: return [4 /*yield*/, ((_24 = mcp === null || mcp === void 0 ? void 0 : mcp.disconnect({ otelInfo: otelInfo })) === null || _24 === void 0 ? void 0 : _24.catch(console.error))];
477
- case 17:
478
- _25.sent();
546
+ _44.sent();
547
+ throw e_3;
548
+ case 25: return [4 /*yield*/, ((_43 = mcp === null || mcp === void 0 ? void 0 : mcp.disconnect({ otelInfo: otelInfo })) === null || _43 === void 0 ? void 0 : _43.catch(console.error))];
549
+ case 26:
550
+ _44.sent();
479
551
  return [7 /*endfinally*/];
480
- case 18: return [2 /*return*/];
552
+ case 27: return [2 /*return*/];
481
553
  }
482
554
  });
483
555
  }); }),