@iqai/adk 0.5.2 → 0.5.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.mjs CHANGED
@@ -3394,6 +3394,10 @@ var EventActions = class {
3394
3394
  * a compaction of events within the specified timestamp range.
3395
3395
  */
3396
3396
  compaction;
3397
+ /**
3398
+ * The invocation id to rewind to. This is only set for rewind event.
3399
+ */
3400
+ rewindBeforeInvocationId;
3397
3401
  /**
3398
3402
  * Constructor for EventActions
3399
3403
  */
@@ -3405,6 +3409,7 @@ var EventActions = class {
3405
3409
  this.escalate = options.escalate;
3406
3410
  this.requestedAuthConfigs = options.requestedAuthConfigs;
3407
3411
  this.compaction = options.compaction;
3412
+ this.rewindBeforeInvocationId = options.rewindBeforeInvocationId;
3408
3413
  }
3409
3414
  };
3410
3415
 
@@ -4302,6 +4307,7 @@ __export(tools_exports, {
4302
4307
  McpNearAgent: () => McpNearAgent,
4303
4308
  McpNearIntents: () => McpNearIntents,
4304
4309
  McpOdos: () => McpOdos,
4310
+ McpPolymarket: () => McpPolymarket,
4305
4311
  McpSamplingHandler: () => McpSamplingHandler,
4306
4312
  McpTelegram: () => McpTelegram,
4307
4313
  McpToolset: () => McpToolset,
@@ -6470,6 +6476,14 @@ function McpUpbit(config = {}) {
6470
6476
  );
6471
6477
  return new McpToolset(mcpConfig);
6472
6478
  }
6479
+ function McpPolymarket(config = {}) {
6480
+ const mcpConfig = createMcpConfig(
6481
+ "Polymarket MCP Client",
6482
+ "@iqai/mcp-polymarket",
6483
+ config
6484
+ );
6485
+ return new McpToolset(mcpConfig);
6486
+ }
6473
6487
  function McpFilesystem(config = {}) {
6474
6488
  const mcpConfig = createMcpConfig(
6475
6489
  "Filesystem MCP Client",
@@ -8468,8 +8482,30 @@ function rearrangeEventsForLatestFunctionResponse(events) {
8468
8482
  return resultEvents;
8469
8483
  }
8470
8484
  function getContents(currentBranch, events, agentName = "") {
8485
+ const invocationIdToIndex = /* @__PURE__ */ new Map();
8486
+ for (let idx = 0; idx < events.length; idx++) {
8487
+ if (events[idx].invocationId) {
8488
+ invocationIdToIndex.set(events[idx].invocationId, idx);
8489
+ }
8490
+ }
8491
+ const rewindFilteredEvents = [];
8492
+ let i = events.length - 1;
8493
+ while (i >= 0) {
8494
+ const event = events[i];
8495
+ if (event.actions?.rewindBeforeInvocationId) {
8496
+ const rewindInvocationId = event.actions.rewindBeforeInvocationId;
8497
+ const rewindIndex = invocationIdToIndex.get(rewindInvocationId);
8498
+ if (rewindIndex !== void 0 && rewindIndex < i) {
8499
+ i = rewindIndex;
8500
+ }
8501
+ } else {
8502
+ rewindFilteredEvents.push(event);
8503
+ }
8504
+ i--;
8505
+ }
8506
+ rewindFilteredEvents.reverse();
8471
8507
  const filteredEvents = [];
8472
- for (const event of events) {
8508
+ for (const event of rewindFilteredEvents) {
8473
8509
  if (!event.content || !event.content.role || !event.content.parts || event.content.parts.length === 0) {
8474
8510
  continue;
8475
8511
  }
@@ -10184,6 +10220,48 @@ var RunConfig = class {
10184
10220
  }
10185
10221
  };
10186
10222
 
10223
+ // src/artifacts/artifact-util.ts
10224
+ var SESSION_SCOPED_ARTIFACT_URI_RE = /^artifact:\/\/apps\/([^/]+)\/users\/([^/]+)\/sessions\/([^/]+)\/artifacts\/([^/]+)\/versions\/(\d+)$/;
10225
+ var USER_SCOPED_ARTIFACT_URI_RE = /^artifact:\/\/apps\/([^/]+)\/users\/([^/]+)\/artifacts\/([^/]+)\/versions\/(\d+)$/;
10226
+ function parseArtifactUri(uri) {
10227
+ if (!uri || !uri.startsWith("artifact://")) {
10228
+ return null;
10229
+ }
10230
+ let match = SESSION_SCOPED_ARTIFACT_URI_RE.exec(uri);
10231
+ if (match) {
10232
+ return {
10233
+ appName: match[1],
10234
+ userId: match[2],
10235
+ sessionId: match[3],
10236
+ filename: match[4],
10237
+ version: Number.parseInt(match[5], 10)
10238
+ };
10239
+ }
10240
+ match = USER_SCOPED_ARTIFACT_URI_RE.exec(uri);
10241
+ if (match) {
10242
+ return {
10243
+ appName: match[1],
10244
+ userId: match[2],
10245
+ sessionId: void 0,
10246
+ filename: match[3],
10247
+ version: Number.parseInt(match[4], 10)
10248
+ };
10249
+ }
10250
+ return null;
10251
+ }
10252
+ function getArtifactUri(args) {
10253
+ const { appName, userId, filename, version, sessionId } = args;
10254
+ if (sessionId) {
10255
+ return `artifact://apps/${appName}/users/${userId}/sessions/${sessionId}/artifacts/${filename}/versions/${version}`;
10256
+ }
10257
+ return `artifact://apps/${appName}/users/${userId}/artifacts/${filename}/versions/${version}`;
10258
+ }
10259
+ function isArtifactRef(artifact) {
10260
+ return Boolean(
10261
+ artifact.fileData?.fileUri && artifact.fileData.fileUri.startsWith("artifact://")
10262
+ );
10263
+ }
10264
+
10187
10265
  // src/artifacts/in-memory-artifact-service.ts
10188
10266
  var InMemoryArtifactService = class {
10189
10267
  artifacts = /* @__PURE__ */ new Map();
@@ -10224,7 +10302,29 @@ var InMemoryArtifactService = class {
10224
10302
  if (targetVersion < 0 || targetVersion >= versions.length) {
10225
10303
  return null;
10226
10304
  }
10227
- return versions[targetVersion];
10305
+ const artifactEntry = versions[targetVersion];
10306
+ if (!artifactEntry) {
10307
+ return null;
10308
+ }
10309
+ if (isArtifactRef(artifactEntry)) {
10310
+ const parsedUri = parseArtifactUri(artifactEntry.fileData?.fileUri || "");
10311
+ if (!parsedUri) {
10312
+ throw new Error(
10313
+ `Invalid artifact reference URI: ${artifactEntry.fileData?.fileUri}`
10314
+ );
10315
+ }
10316
+ return await this.loadArtifact({
10317
+ appName: parsedUri.appName,
10318
+ userId: parsedUri.userId,
10319
+ sessionId: parsedUri.sessionId || sessionId,
10320
+ filename: parsedUri.filename,
10321
+ version: parsedUri.version
10322
+ });
10323
+ }
10324
+ if (!artifactEntry.text && (!artifactEntry.inlineData?.data || artifactEntry.inlineData.data.length === 0) && !artifactEntry.fileData) {
10325
+ return null;
10326
+ }
10327
+ return artifactEntry;
10228
10328
  }
10229
10329
  async listArtifactKeys(args) {
10230
10330
  const { appName, userId, sessionId } = args;
@@ -10409,11 +10509,16 @@ var BaseSessionService = class {
10409
10509
  return;
10410
10510
  }
10411
10511
  for (const key in event.actions.stateDelta) {
10412
- if (Object.prototype.hasOwnProperty.call(event.actions.stateDelta, key)) {
10512
+ if (Object.hasOwn(event.actions.stateDelta, key)) {
10413
10513
  if (key.startsWith("temp_")) {
10414
10514
  continue;
10415
10515
  }
10416
- session.state[key] = event.actions.stateDelta[key];
10516
+ const value = event.actions.stateDelta[key];
10517
+ if (value === null || value === void 0) {
10518
+ delete session.state[key];
10519
+ } else {
10520
+ session.state[key] = value;
10521
+ }
10417
10522
  }
10418
10523
  }
10419
10524
  }
@@ -10961,6 +11066,142 @@ var Runner = class {
10961
11066
  }
10962
11067
  return void 0;
10963
11068
  }
11069
+ async rewind(args) {
11070
+ const { userId, sessionId, rewindBeforeInvocationId } = args;
11071
+ const session = await this.sessionService.getSession(
11072
+ this.appName,
11073
+ userId,
11074
+ sessionId
11075
+ );
11076
+ if (!session) {
11077
+ throw new Error(`Session not found: ${sessionId}`);
11078
+ }
11079
+ let rewindEventIndex = -1;
11080
+ for (let i = 0; i < session.events.length; i++) {
11081
+ if (session.events[i].invocationId === rewindBeforeInvocationId) {
11082
+ rewindEventIndex = i;
11083
+ break;
11084
+ }
11085
+ }
11086
+ if (rewindEventIndex === -1) {
11087
+ throw new Error(`Invocation ID not found: ${rewindBeforeInvocationId}`);
11088
+ }
11089
+ const stateDelta = await this._computeStateDeltaForRewind(
11090
+ session,
11091
+ rewindEventIndex
11092
+ );
11093
+ const artifactDelta = await this._computeArtifactDeltaForRewind(
11094
+ session,
11095
+ rewindEventIndex
11096
+ );
11097
+ const rewindEvent = new Event({
11098
+ invocationId: newInvocationContextId(),
11099
+ author: "user",
11100
+ actions: new EventActions({
11101
+ rewindBeforeInvocationId,
11102
+ stateDelta,
11103
+ artifactDelta
11104
+ })
11105
+ });
11106
+ this.logger.info(
11107
+ "Rewinding session to invocation:",
11108
+ rewindBeforeInvocationId
11109
+ );
11110
+ await this.sessionService.appendEvent(session, rewindEvent);
11111
+ }
11112
+ async _computeStateDeltaForRewind(session, rewindEventIndex) {
11113
+ const stateAtRewindPoint = {};
11114
+ for (let i = 0; i < rewindEventIndex; i++) {
11115
+ const event = session.events[i];
11116
+ if (event.actions?.stateDelta) {
11117
+ for (const [k, v] of Object.entries(event.actions.stateDelta)) {
11118
+ if (k.startsWith("app:") || k.startsWith("user:")) {
11119
+ continue;
11120
+ }
11121
+ if (v === null || v === void 0) {
11122
+ delete stateAtRewindPoint[k];
11123
+ } else {
11124
+ stateAtRewindPoint[k] = v;
11125
+ }
11126
+ }
11127
+ }
11128
+ }
11129
+ const currentState = session.state;
11130
+ const rewindStateDelta = {};
11131
+ for (const [key, valueAtRewind] of Object.entries(stateAtRewindPoint)) {
11132
+ if (!(key in currentState) || currentState[key] !== valueAtRewind) {
11133
+ rewindStateDelta[key] = valueAtRewind;
11134
+ }
11135
+ }
11136
+ for (const key of Object.keys(currentState)) {
11137
+ if (key.startsWith("app:") || key.startsWith("user:")) {
11138
+ continue;
11139
+ }
11140
+ if (!(key in stateAtRewindPoint)) {
11141
+ rewindStateDelta[key] = null;
11142
+ }
11143
+ }
11144
+ return rewindStateDelta;
11145
+ }
11146
+ async _computeArtifactDeltaForRewind(session, rewindEventIndex) {
11147
+ if (!this.artifactService) {
11148
+ return {};
11149
+ }
11150
+ const versionsAtRewindPoint = {};
11151
+ for (let i = 0; i < rewindEventIndex; i++) {
11152
+ const event = session.events[i];
11153
+ if (event.actions?.artifactDelta) {
11154
+ Object.assign(versionsAtRewindPoint, event.actions.artifactDelta);
11155
+ }
11156
+ }
11157
+ const currentVersions = {};
11158
+ for (const event of session.events) {
11159
+ if (event.actions?.artifactDelta) {
11160
+ Object.assign(currentVersions, event.actions.artifactDelta);
11161
+ }
11162
+ }
11163
+ const rewindArtifactDelta = {};
11164
+ for (const [filename, vn] of Object.entries(currentVersions)) {
11165
+ if (filename.startsWith("user:")) {
11166
+ continue;
11167
+ }
11168
+ const vt = versionsAtRewindPoint[filename];
11169
+ if (vt === vn) {
11170
+ continue;
11171
+ }
11172
+ let artifact;
11173
+ if (vt === void 0 || vt === null) {
11174
+ artifact = {
11175
+ inlineData: {
11176
+ mimeType: "application/octet-stream",
11177
+ data: ""
11178
+ }
11179
+ };
11180
+ } else {
11181
+ const artifactUri = getArtifactUri({
11182
+ appName: this.appName,
11183
+ userId: session.userId,
11184
+ sessionId: session.id,
11185
+ filename,
11186
+ version: vt
11187
+ });
11188
+ artifact = {
11189
+ fileData: {
11190
+ fileUri: artifactUri
11191
+ }
11192
+ };
11193
+ }
11194
+ const newVersion = await this.artifactService.saveArtifact({
11195
+ appName: this.appName,
11196
+ userId: session.userId,
11197
+ sessionId: session.id,
11198
+ filename,
11199
+ artifact
11200
+ });
11201
+ rewindArtifactDelta[filename] = newVersion;
11202
+ }
11203
+ return rewindArtifactDelta;
11204
+ }
10964
11205
  };
10965
11206
  var InMemoryRunner = class extends Runner {
10966
11207
  /**
@@ -11664,6 +11905,9 @@ var AgentBuilder = class _AgentBuilder {
11664
11905
  ...params,
11665
11906
  runConfig: params.runConfig ?? runConfig
11666
11907
  });
11908
+ },
11909
+ rewind(params) {
11910
+ return baseRunner.rewind(params);
11667
11911
  }
11668
11912
  };
11669
11913
  }
@@ -14204,6 +14448,7 @@ export {
14204
14448
  McpNearAgent,
14205
14449
  McpNearIntents,
14206
14450
  McpOdos,
14451
+ McpPolymarket,
14207
14452
  McpSamplingHandler,
14208
14453
  McpTelegram,
14209
14454
  McpToolset,
@@ -14257,6 +14502,7 @@ export {
14257
14502
  createTool,
14258
14503
  generateAuthEvent,
14259
14504
  generateClientFunctionCallId,
14505
+ getArtifactUri,
14260
14506
  getLongRunningFunctionCalls,
14261
14507
  getMcpTools,
14262
14508
  handleFunctionCallsAsync,
@@ -14265,6 +14511,7 @@ export {
14265
14511
  initializeTelemetry,
14266
14512
  injectSessionState,
14267
14513
  requestProcessor6 as instructionsRequestProcessor,
14514
+ isArtifactRef,
14268
14515
  isEnhancedAuthConfig,
14269
14516
  jsonSchemaToDeclaration,
14270
14517
  mcpSchemaToParameters,
@@ -14274,6 +14521,7 @@ export {
14274
14521
  requestProcessor7 as nlPlanningRequestProcessor,
14275
14522
  responseProcessor2 as nlPlanningResponseProcessor,
14276
14523
  normalizeJsonSchema,
14524
+ parseArtifactUri,
14277
14525
  populateClientFunctionCallId,
14278
14526
  registerProviders,
14279
14527
  removeClientFunctionCallId,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@iqai/adk",
3
- "version": "0.5.2",
3
+ "version": "0.5.4",
4
4
  "description": "Agent Development Kit for TypeScript with multi-provider LLM support",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",