@okxweb3/a2a-node 0.0.2-beta-38940d220c-260609234729 → 0.0.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/README.md CHANGED
@@ -50,24 +50,25 @@ okx-a2a xmtp-send \
50
50
  --job-id <jobId> \
51
51
  --session-agent-id <sessionAgentId> \
52
52
  --my-agent-id <localAgentId> \
53
+ --to-agent-id <remoteAgentId> \
53
54
  --message <content>
54
55
  ```
55
56
 
56
57
  The same command works before and after the job JSON has `xmtpGroupId`.
57
58
  If `xmtpGroupId` exists, the CLI reuses that group. If it does not exist,
58
- the CLI uses `jobId + localAgentId` to infer the provider, creates the group,
59
- and persists it to the job JSON.
59
+ pass `--to-agent-id` or `--to-address`; the CLI resolves agent metadata through
60
+ the local cache first, then falls back to supported onchainos lookup commands.
60
61
  `sessionAgentId` selects `okx-agent-task-{jobId}-{agentId}.json` when a job
61
62
  has multiple active agent conversations.
62
63
 
63
- Optional target override:
64
+ Address target:
64
65
 
65
66
  ```bash
66
67
  okx-a2a xmtp-send \
67
68
  --job-id <jobId> \
68
69
  --session-agent-id <sessionAgentId> \
69
70
  --my-agent-id <localAgentId> \
70
- --to-agent-id <remoteAgentId> \
71
+ --to-address <remoteXmtpAddress> \
71
72
  --message <content>
72
73
  ```
73
74
 
package/dist/cli.js CHANGED
@@ -18099,6 +18099,7 @@ var init_sentry_logger = __esm({
18099
18099
  "stage",
18100
18100
  "status",
18101
18101
  "subcommand",
18102
+ "taskId",
18102
18103
  "type"
18103
18104
  ]);
18104
18105
  SENTRY_FINGERPRINT_KEYS = [
@@ -18420,7 +18421,7 @@ var init_sentry_config = __esm({
18420
18421
  environment = process.env.SENTRY_ENV === "dev" ? "dev" : "prod";
18421
18422
  SENTRY_CONFIG = {
18422
18423
  projectName: "okx/openclaw-okx-a2a-extension",
18423
- release: "0.0.2-beta-38940d220c-260609234729",
18424
+ release: "0.0.4",
18424
18425
  environment
18425
18426
  };
18426
18427
  }
@@ -79915,57 +79916,6 @@ function createEmptyTaskFile(jobId, sessionAgentId) {
79915
79916
  messages: []
79916
79917
  };
79917
79918
  }
79918
- function sameAddress(left, right) {
79919
- return !!left && !!right && left.toLowerCase() === right.toLowerCase();
79920
- }
79921
- function isNonEmptyString(value) {
79922
- return typeof value === "string" && value.length > 0;
79923
- }
79924
- function extractJsonObjects(raw) {
79925
- const objects = [];
79926
- let start = -1;
79927
- let depth = 0;
79928
- let inString = false;
79929
- let escaped = false;
79930
- for (let i = 0; i < raw.length; i++) {
79931
- const char = raw[i];
79932
- if (start === -1) {
79933
- if (char === "{") {
79934
- start = i;
79935
- depth = 1;
79936
- inString = false;
79937
- escaped = false;
79938
- }
79939
- continue;
79940
- }
79941
- if (inString) {
79942
- if (escaped) {
79943
- escaped = false;
79944
- } else if (char === "\\") {
79945
- escaped = true;
79946
- } else if (char === '"') {
79947
- inString = false;
79948
- }
79949
- continue;
79950
- }
79951
- if (char === '"') {
79952
- inString = true;
79953
- } else if (char === "{") {
79954
- depth++;
79955
- } else if (char === "}") {
79956
- depth--;
79957
- if (depth === 0) {
79958
- const candidate = raw.slice(start, i + 1);
79959
- const parsed = parseJson(candidate);
79960
- if (parsed) {
79961
- objects.push(parsed);
79962
- }
79963
- start = -1;
79964
- }
79965
- }
79966
- }
79967
- return objects;
79968
- }
79969
79919
  function selectReplyTarget(messages, replyToMessageId) {
79970
79920
  if (replyToMessageId) {
79971
79921
  const exact = messages.find((message) => message.id === replyToMessageId);
@@ -80140,176 +80090,100 @@ function inferMyXmtpAddress(params) {
80140
80090
  }
80141
80091
  throw new Error("Cannot infer local XMTP address; pass --my-agent-id <id>");
80142
80092
  }
80143
- function findDataObject(raw, predicate) {
80144
- const objects = extractJsonObjects(raw);
80145
- for (let i = objects.length - 1; i >= 0; i--) {
80146
- const object = objects[i];
80147
- if (!isPlainObject(object)) {
80148
- continue;
80093
+ async function resolveAgentByIdFromCacheOrNetwork(service, agentId) {
80094
+ const local = service.getAgentByAgentId(agentId);
80095
+ if (local) {
80096
+ resolvedAgentByIdCache.set(local.agentId, local);
80097
+ if (local.communicationAddress) {
80098
+ resolvedAgentByAddressCache.set(local.communicationAddress.toLowerCase(), local);
80149
80099
  }
80150
- const data = isPlainObject(object.data) ? object.data : object;
80151
- if (predicate(data)) {
80152
- return data;
80100
+ return local;
80101
+ }
80102
+ const cached = resolvedAgentByIdCache.get(agentId);
80103
+ if (cached) {
80104
+ return cached;
80105
+ }
80106
+ const [remote] = await fetchAgentsByIds([agentId]);
80107
+ if (remote?.agentId) {
80108
+ resolvedAgentByIdCache.set(remote.agentId, remote);
80109
+ if (remote.communicationAddress) {
80110
+ resolvedAgentByAddressCache.set(remote.communicationAddress.toLowerCase(), remote);
80153
80111
  }
80154
80112
  }
80155
- return null;
80113
+ return remote;
80156
80114
  }
80157
- async function fetchTaskStatus(jobId, myAgentId) {
80158
- const { stdout, stderr } = await exec([
80159
- "agent",
80160
- "status",
80161
- jobId,
80162
- "--agent-id",
80163
- myAgentId
80164
- ]);
80165
- const data = findDataObject(`${stdout}
80166
- ${stderr}`, (item) => {
80167
- return readString(item.jobId)?.toLowerCase() === jobId.toLowerCase();
80168
- });
80169
- if (!data) {
80170
- return null;
80115
+ async function resolveAgentByAddressFromCacheOrNetwork(service, communicationAddress) {
80116
+ const local = service.getAgentByAddress(communicationAddress);
80117
+ if (local) {
80118
+ resolvedAgentByIdCache.set(local.agentId, local);
80119
+ resolvedAgentByAddressCache.set(local.communicationAddress.toLowerCase(), local);
80120
+ return local;
80171
80121
  }
80172
- return {
80173
- jobId: readString(data.jobId) ?? void 0,
80174
- providerAgentId: readString(data.providerAgentId),
80175
- providerAgentAddress: readString(data.providerAgentAddress),
80176
- designatedProvider: readString(data.designatedProvider)
80177
- };
80178
- }
80179
- async function fetchTaskRecommendations(jobId, myAgentId) {
80180
- const { stdout, stderr } = await exec([
80181
- "agent",
80182
- "recommend",
80183
- jobId,
80184
- "--agent-id",
80185
- myAgentId
80186
- ]);
80187
- const data = findDataObject(`${stdout}
80188
- ${stderr}`, (item) => Array.isArray(item.recommendations));
80189
- const recommendations = Array.isArray(data?.recommendations) ? data.recommendations : [];
80190
- return recommendations.filter(isPlainObject).map((item) => ({
80191
- providerAgentId: readString(item.providerAgentId),
80192
- providerAddress: readString(item.providerAddress)
80193
- }));
80194
- }
80195
- async function searchAgents(query) {
80196
- const { stdout, stderr } = await exec([
80197
- "agent",
80198
- "search",
80199
- "--query",
80200
- query,
80201
- "--page",
80202
- "1",
80203
- "--page-size",
80204
- "10"
80205
- ]);
80206
- const data = findDataObject(`${stdout}
80207
- ${stderr}`, (item) => {
80208
- return Array.isArray(item.list);
80209
- });
80210
- const list = Array.isArray(data?.list) ? data.list : [];
80211
- return list.filter(isPlainObject).map((item) => ({
80212
- agentId: readString(item.agentId) ?? void 0,
80213
- communicationAddress: readString(item.communicationAddress) ?? void 0
80214
- }));
80215
- }
80216
- async function fetchAgentByWalletAddress(walletAddress) {
80217
- const candidates = await searchAgents(walletAddress);
80218
- const ids = candidates.map((item) => item.agentId).filter(isNonEmptyString);
80219
- if (ids.length === 0) {
80220
- return void 0;
80122
+ const cached = resolvedAgentByAddressCache.get(communicationAddress.toLowerCase());
80123
+ if (cached) {
80124
+ return cached;
80125
+ }
80126
+ const remote = await fetchAgentByAddress(communicationAddress);
80127
+ if (remote?.agentId) {
80128
+ resolvedAgentByIdCache.set(remote.agentId, remote);
80129
+ if (remote.communicationAddress) {
80130
+ resolvedAgentByAddressCache.set(remote.communicationAddress.toLowerCase(), remote);
80131
+ }
80221
80132
  }
80222
- const agents = await fetchAgentsByIds(ids);
80223
- return agents.find((agent) => {
80224
- return sameAddress(agent.agentWalletAddress, walletAddress) || sameAddress(agent.ownerAddress, walletAddress);
80225
- }) ?? agents[0];
80133
+ return remote;
80226
80134
  }
80227
- async function resolveRemoteAgentFromJob(jobId, myAgentId) {
80135
+ async function resolveRemoteByAgentId(params) {
80136
+ const { service, agentId, fallbackAddress, logContext } = params;
80137
+ let remoteAgent;
80228
80138
  try {
80229
- const status = await fetchTaskStatus(jobId, myAgentId);
80230
- if (status?.providerAgentId) {
80231
- const [agent] = await fetchAgentsByIds([status.providerAgentId]);
80232
- if (agent?.communicationAddress) {
80233
- return agent;
80234
- }
80235
- }
80236
- const providerWallet = status?.providerAgentAddress ?? status?.designatedProvider;
80237
- if (providerWallet) {
80238
- const agent = await fetchAgentByWalletAddress(providerWallet);
80239
- if (agent?.communicationAddress) {
80240
- return agent;
80241
- }
80242
- }
80139
+ remoteAgent = await resolveAgentByIdFromCacheOrNetwork(service, agentId);
80243
80140
  } catch (err2) {
80244
- console.log(
80245
- `[okx-agent-task] failed to infer provider from task status job=${jobId} myAgentId=${myAgentId}:`,
80246
- err2
80247
- );
80141
+ if (!fallbackAddress) {
80142
+ throw err2;
80143
+ }
80144
+ logRecipientStatusLookupFailed(logContext, err2);
80248
80145
  }
80249
- try {
80250
- const recommendations = await fetchTaskRecommendations(jobId, myAgentId);
80251
- for (const recommendation of recommendations) {
80252
- if (recommendation.providerAgentId) {
80253
- const [agent] = await fetchAgentsByIds([recommendation.providerAgentId]);
80254
- if (agent?.communicationAddress) {
80255
- return agent;
80256
- }
80257
- }
80258
- if (recommendation.providerAddress) {
80259
- const agent = await fetchAgentByWalletAddress(recommendation.providerAddress);
80260
- if (agent?.communicationAddress) {
80261
- return agent;
80262
- }
80263
- }
80146
+ if (!remoteAgent?.communicationAddress && fallbackAddress) {
80147
+ try {
80148
+ const fallbackAgent = await resolveAgentByAddressFromCacheOrNetwork(service, fallbackAddress);
80149
+ remoteAgent = fallbackAgent ?? remoteAgent;
80150
+ } catch (err2) {
80151
+ console.log(
80152
+ `[okx-agent-task] remote agent lookup failed for ${fallbackAddress}, continuing without receiverAgentId:`,
80153
+ err2
80154
+ );
80264
80155
  }
80265
- } catch (err2) {
80266
- console.log(
80267
- `[okx-agent-task] failed to infer provider from recommendations job=${jobId} myAgentId=${myAgentId}:`,
80268
- err2
80269
- );
80270
80156
  }
80271
- return void 0;
80157
+ if (!remoteAgent?.communicationAddress && !fallbackAddress) {
80158
+ throw new Error(`No communication address found for toAgentId=${agentId}`);
80159
+ }
80160
+ assertRecipientAvailable(remoteAgent);
80161
+ return {
80162
+ toXmtpAddress: remoteAgent?.communicationAddress ?? fallbackAddress,
80163
+ toAgentId: remoteAgent?.agentId ?? agentId,
80164
+ remoteAgent
80165
+ };
80272
80166
  }
80273
80167
  async function resolveRemote(params) {
80274
80168
  const { command, file, service } = params;
80275
80169
  if (command.toAgentId) {
80276
- let remoteAgent;
80277
- try {
80278
- [remoteAgent] = await fetchAgentsByIds([command.toAgentId]);
80279
- } catch (err2) {
80280
- const fallbackAddress = command.toXmtpAddress ?? file.toXmtpAddress;
80281
- if (!fallbackAddress) {
80282
- throw err2;
80283
- }
80284
- logRecipientStatusLookupFailed(`toAgentId=${command.toAgentId}`, err2);
80285
- return {
80286
- toXmtpAddress: fallbackAddress,
80287
- toAgentId: command.toAgentId,
80288
- remoteAgent: void 0
80289
- };
80290
- }
80291
- if (!remoteAgent?.communicationAddress) {
80292
- throw new Error(`No communication address found for toAgentId=${command.toAgentId}`);
80293
- }
80294
- assertRecipientAvailable(remoteAgent);
80295
- return {
80296
- toXmtpAddress: remoteAgent.communicationAddress,
80297
- toAgentId: remoteAgent.agentId,
80298
- remoteAgent
80299
- };
80170
+ return resolveRemoteByAgentId({
80171
+ service,
80172
+ agentId: command.toAgentId,
80173
+ fallbackAddress: command.toXmtpAddress ?? file.toXmtpAddress,
80174
+ logContext: `toAgentId=${command.toAgentId}`
80175
+ });
80300
80176
  }
80301
80177
  const toXmtpAddress = command.toXmtpAddress ?? file.toXmtpAddress;
80302
80178
  if (toXmtpAddress) {
80303
- let remoteAgent = service.getAgentByAddress(toXmtpAddress);
80304
- if (!remoteAgent) {
80305
- try {
80306
- remoteAgent = await fetchAgentByAddress(toXmtpAddress);
80307
- } catch (err2) {
80308
- console.log(
80309
- `[okx-agent-task] remote agent lookup failed for ${toXmtpAddress}, continuing without receiverAgentId:`,
80310
- err2
80311
- );
80312
- }
80179
+ let remoteAgent;
80180
+ try {
80181
+ remoteAgent = await resolveAgentByAddressFromCacheOrNetwork(service, toXmtpAddress);
80182
+ } catch (err2) {
80183
+ console.log(
80184
+ `[okx-agent-task] remote agent lookup failed for ${toXmtpAddress}, continuing without receiverAgentId:`,
80185
+ err2
80186
+ );
80313
80187
  }
80314
80188
  assertRecipientAvailable(remoteAgent);
80315
80189
  return {
@@ -80318,59 +80192,29 @@ async function resolveRemote(params) {
80318
80192
  remoteAgent
80319
80193
  };
80320
80194
  }
80321
- if (command.myAgentId) {
80322
- const remoteAgent = await resolveRemoteAgentFromJob(command.jobId, command.myAgentId);
80323
- if (remoteAgent?.communicationAddress) {
80324
- assertRecipientAvailable(remoteAgent);
80325
- return {
80326
- toXmtpAddress: remoteAgent.communicationAddress,
80327
- toAgentId: remoteAgent.agentId,
80328
- remoteAgent
80329
- };
80330
- }
80331
- }
80332
80195
  throw new Error(
80333
- "job JSON has no xmtpGroupId and provider could not be inferred; pass --my-agent-id <id> or optionally --to-agent-id/--to-address"
80196
+ "job JSON has no xmtpGroupId or remote address; pass --to-agent-id or --to-address"
80334
80197
  );
80335
80198
  }
80336
80199
  async function resolveRemoteWithoutFile(params) {
80337
80200
  const { command, service } = params;
80338
80201
  if (command.toAgentId) {
80202
+ return resolveRemoteByAgentId({
80203
+ service,
80204
+ agentId: command.toAgentId,
80205
+ fallbackAddress: command.toXmtpAddress,
80206
+ logContext: `toAgentId=${command.toAgentId}`
80207
+ });
80208
+ }
80209
+ if (command.toXmtpAddress) {
80339
80210
  let remoteAgent;
80340
80211
  try {
80341
- [remoteAgent] = await fetchAgentsByIds([command.toAgentId]);
80212
+ remoteAgent = await resolveAgentByAddressFromCacheOrNetwork(service, command.toXmtpAddress);
80342
80213
  } catch (err2) {
80343
- if (!command.toXmtpAddress) {
80344
- throw err2;
80345
- }
80346
- logRecipientStatusLookupFailed(`toAgentId=${command.toAgentId}`, err2);
80347
- return {
80348
- toXmtpAddress: command.toXmtpAddress,
80349
- toAgentId: command.toAgentId,
80350
- remoteAgent: void 0
80351
- };
80352
- }
80353
- if (!remoteAgent?.communicationAddress) {
80354
- throw new Error(`No communication address found for toAgentId=${command.toAgentId}`);
80355
- }
80356
- assertRecipientAvailable(remoteAgent);
80357
- return {
80358
- toXmtpAddress: remoteAgent.communicationAddress,
80359
- toAgentId: remoteAgent.agentId,
80360
- remoteAgent
80361
- };
80362
- }
80363
- if (command.toXmtpAddress) {
80364
- let remoteAgent = service.getAgentByAddress(command.toXmtpAddress);
80365
- if (!remoteAgent) {
80366
- try {
80367
- remoteAgent = await fetchAgentByAddress(command.toXmtpAddress);
80368
- } catch (err2) {
80369
- console.log(
80370
- `[okx-agent-task] remote agent lookup failed for ${command.toXmtpAddress}, continuing without receiverAgentId:`,
80371
- err2
80372
- );
80373
- }
80214
+ console.log(
80215
+ `[okx-agent-task] remote agent lookup failed for ${command.toXmtpAddress}, continuing without receiverAgentId:`,
80216
+ err2
80217
+ );
80374
80218
  }
80375
80219
  assertRecipientAvailable(remoteAgent);
80376
80220
  return {
@@ -80508,16 +80352,14 @@ async function ensureGroupSession(params) {
80508
80352
  let remoteAgent;
80509
80353
  if (stored.toAgentId) {
80510
80354
  try {
80511
- [remoteAgent] = await fetchAgentsByIds([stored.toAgentId]);
80355
+ remoteAgent = await resolveAgentByIdFromCacheOrNetwork(service, stored.toAgentId);
80512
80356
  } catch (err2) {
80513
80357
  logRecipientStatusLookupFailed(`stored.toAgentId=${stored.toAgentId}`, err2);
80514
80358
  }
80515
- } else {
80516
- remoteAgent = service.getAgentByAddress(stored.toXmtpAddress);
80517
80359
  }
80518
80360
  if (!remoteAgent) {
80519
80361
  try {
80520
- remoteAgent = await fetchAgentByAddress(stored.toXmtpAddress);
80362
+ remoteAgent = await resolveAgentByAddressFromCacheOrNetwork(service, stored.toXmtpAddress);
80521
80363
  } catch {
80522
80364
  remoteAgent = void 0;
80523
80365
  }
@@ -80815,7 +80657,7 @@ async function handleXmtpSendCommand(params) {
80815
80657
  });
80816
80658
  return { messageId };
80817
80659
  }
80818
- var import_node_crypto6, TASK_MIN_VERSION, sqliteGroupSessionPromises;
80660
+ var import_node_crypto6, TASK_MIN_VERSION, sqliteGroupSessionPromises, resolvedAgentByIdCache, resolvedAgentByAddressCache;
80819
80661
  var init_xmtp_send = __esm({
80820
80662
  "src/xmtp-send.ts"() {
80821
80663
  "use strict";
@@ -80824,12 +80666,13 @@ var init_xmtp_send = __esm({
80824
80666
  init_envelope();
80825
80667
  init_concurrency2();
80826
80668
  init_xmtp_sdk();
80827
- init_bin();
80828
80669
  init_onchainos();
80829
80670
  init_session_store();
80830
80671
  init_agent_message_notice();
80831
80672
  TASK_MIN_VERSION = 1;
80832
80673
  sqliteGroupSessionPromises = /* @__PURE__ */ new Map();
80674
+ resolvedAgentByIdCache = /* @__PURE__ */ new Map();
80675
+ resolvedAgentByAddressCache = /* @__PURE__ */ new Map();
80833
80676
  }
80834
80677
  });
80835
80678
 
@@ -84791,12 +84634,12 @@ async function runListenerWithLock(options, paths) {
84791
84634
  }));
84792
84635
  }
84793
84636
  });
84794
- service.setPluginVersion("0.0.2-beta-38940d220c-260609234729");
84637
+ service.setPluginVersion("0.0.4");
84795
84638
  await service.init();
84796
84639
  const pluginVersionStatus = service.pluginVersionStatus;
84797
84640
  if (pluginVersionStatus.unavailable) {
84798
84641
  throw new Error(
84799
- `@okxweb3/a2a-node v${"0.0.2-beta-38940d220c-260609234729"} is below the required minimum v${pluginVersionStatus.minVersion}`
84642
+ `@okxweb3/a2a-node v${"0.0.4"} is below the required minimum v${pluginVersionStatus.minVersion}`
84800
84643
  );
84801
84644
  }
84802
84645
  const systemConfig = service.getSystemConfig();
@@ -84814,7 +84657,7 @@ async function runListenerWithLock(options, paths) {
84814
84657
  onchainosAgentId: "*",
84815
84658
  reason: "system-config missing sentryDsn",
84816
84659
  pluginId: "@okxweb3/a2a-node",
84817
- pluginVersion: "0.0.2-beta-38940d220c-260609234729"
84660
+ pluginVersion: "0.0.4"
84818
84661
  });
84819
84662
  }
84820
84663
  console.log(
package/dist/index.js CHANGED
@@ -66785,6 +66785,7 @@ var SENTRY_TAG_KEYS = /* @__PURE__ */ new Set([
66785
66785
  "stage",
66786
66786
  "status",
66787
66787
  "subcommand",
66788
+ "taskId",
66788
66789
  "type"
66789
66790
  ]);
66790
66791
  var SENTRY_FINGERPRINT_KEYS = [
@@ -81034,57 +81035,6 @@ function createEmptyTaskFile(jobId, sessionAgentId) {
81034
81035
  messages: []
81035
81036
  };
81036
81037
  }
81037
- function sameAddress(left, right) {
81038
- return !!left && !!right && left.toLowerCase() === right.toLowerCase();
81039
- }
81040
- function isNonEmptyString(value) {
81041
- return typeof value === "string" && value.length > 0;
81042
- }
81043
- function extractJsonObjects(raw) {
81044
- const objects = [];
81045
- let start = -1;
81046
- let depth = 0;
81047
- let inString = false;
81048
- let escaped = false;
81049
- for (let i = 0; i < raw.length; i++) {
81050
- const char = raw[i];
81051
- if (start === -1) {
81052
- if (char === "{") {
81053
- start = i;
81054
- depth = 1;
81055
- inString = false;
81056
- escaped = false;
81057
- }
81058
- continue;
81059
- }
81060
- if (inString) {
81061
- if (escaped) {
81062
- escaped = false;
81063
- } else if (char === "\\") {
81064
- escaped = true;
81065
- } else if (char === '"') {
81066
- inString = false;
81067
- }
81068
- continue;
81069
- }
81070
- if (char === '"') {
81071
- inString = true;
81072
- } else if (char === "{") {
81073
- depth++;
81074
- } else if (char === "}") {
81075
- depth--;
81076
- if (depth === 0) {
81077
- const candidate = raw.slice(start, i + 1);
81078
- const parsed = parseJson(candidate);
81079
- if (parsed) {
81080
- objects.push(parsed);
81081
- }
81082
- start = -1;
81083
- }
81084
- }
81085
- }
81086
- return objects;
81087
- }
81088
81038
  function selectReplyTarget(messages, replyToMessageId) {
81089
81039
  if (replyToMessageId) {
81090
81040
  const exact = messages.find((message) => message.id === replyToMessageId);
@@ -81259,176 +81209,102 @@ function inferMyXmtpAddress(params) {
81259
81209
  }
81260
81210
  throw new Error("Cannot infer local XMTP address; pass --my-agent-id <id>");
81261
81211
  }
81262
- function findDataObject(raw, predicate) {
81263
- const objects = extractJsonObjects(raw);
81264
- for (let i = objects.length - 1; i >= 0; i--) {
81265
- const object = objects[i];
81266
- if (!isPlainObject(object)) {
81267
- continue;
81212
+ var resolvedAgentByIdCache = /* @__PURE__ */ new Map();
81213
+ var resolvedAgentByAddressCache = /* @__PURE__ */ new Map();
81214
+ async function resolveAgentByIdFromCacheOrNetwork(service, agentId) {
81215
+ const local = service.getAgentByAgentId(agentId);
81216
+ if (local) {
81217
+ resolvedAgentByIdCache.set(local.agentId, local);
81218
+ if (local.communicationAddress) {
81219
+ resolvedAgentByAddressCache.set(local.communicationAddress.toLowerCase(), local);
81268
81220
  }
81269
- const data = isPlainObject(object.data) ? object.data : object;
81270
- if (predicate(data)) {
81271
- return data;
81221
+ return local;
81222
+ }
81223
+ const cached = resolvedAgentByIdCache.get(agentId);
81224
+ if (cached) {
81225
+ return cached;
81226
+ }
81227
+ const [remote] = await fetchAgentsByIds([agentId]);
81228
+ if (remote?.agentId) {
81229
+ resolvedAgentByIdCache.set(remote.agentId, remote);
81230
+ if (remote.communicationAddress) {
81231
+ resolvedAgentByAddressCache.set(remote.communicationAddress.toLowerCase(), remote);
81272
81232
  }
81273
81233
  }
81274
- return null;
81234
+ return remote;
81275
81235
  }
81276
- async function fetchTaskStatus(jobId, myAgentId) {
81277
- const { stdout, stderr } = await exec([
81278
- "agent",
81279
- "status",
81280
- jobId,
81281
- "--agent-id",
81282
- myAgentId
81283
- ]);
81284
- const data = findDataObject(`${stdout}
81285
- ${stderr}`, (item) => {
81286
- return readString(item.jobId)?.toLowerCase() === jobId.toLowerCase();
81287
- });
81288
- if (!data) {
81289
- return null;
81236
+ async function resolveAgentByAddressFromCacheOrNetwork(service, communicationAddress) {
81237
+ const local = service.getAgentByAddress(communicationAddress);
81238
+ if (local) {
81239
+ resolvedAgentByIdCache.set(local.agentId, local);
81240
+ resolvedAgentByAddressCache.set(local.communicationAddress.toLowerCase(), local);
81241
+ return local;
81290
81242
  }
81291
- return {
81292
- jobId: readString(data.jobId) ?? void 0,
81293
- providerAgentId: readString(data.providerAgentId),
81294
- providerAgentAddress: readString(data.providerAgentAddress),
81295
- designatedProvider: readString(data.designatedProvider)
81296
- };
81297
- }
81298
- async function fetchTaskRecommendations(jobId, myAgentId) {
81299
- const { stdout, stderr } = await exec([
81300
- "agent",
81301
- "recommend",
81302
- jobId,
81303
- "--agent-id",
81304
- myAgentId
81305
- ]);
81306
- const data = findDataObject(`${stdout}
81307
- ${stderr}`, (item) => Array.isArray(item.recommendations));
81308
- const recommendations = Array.isArray(data?.recommendations) ? data.recommendations : [];
81309
- return recommendations.filter(isPlainObject).map((item) => ({
81310
- providerAgentId: readString(item.providerAgentId),
81311
- providerAddress: readString(item.providerAddress)
81312
- }));
81313
- }
81314
- async function searchAgents(query) {
81315
- const { stdout, stderr } = await exec([
81316
- "agent",
81317
- "search",
81318
- "--query",
81319
- query,
81320
- "--page",
81321
- "1",
81322
- "--page-size",
81323
- "10"
81324
- ]);
81325
- const data = findDataObject(`${stdout}
81326
- ${stderr}`, (item) => {
81327
- return Array.isArray(item.list);
81328
- });
81329
- const list = Array.isArray(data?.list) ? data.list : [];
81330
- return list.filter(isPlainObject).map((item) => ({
81331
- agentId: readString(item.agentId) ?? void 0,
81332
- communicationAddress: readString(item.communicationAddress) ?? void 0
81333
- }));
81334
- }
81335
- async function fetchAgentByWalletAddress(walletAddress) {
81336
- const candidates = await searchAgents(walletAddress);
81337
- const ids = candidates.map((item) => item.agentId).filter(isNonEmptyString);
81338
- if (ids.length === 0) {
81339
- return void 0;
81243
+ const cached = resolvedAgentByAddressCache.get(communicationAddress.toLowerCase());
81244
+ if (cached) {
81245
+ return cached;
81246
+ }
81247
+ const remote = await fetchAgentByAddress(communicationAddress);
81248
+ if (remote?.agentId) {
81249
+ resolvedAgentByIdCache.set(remote.agentId, remote);
81250
+ if (remote.communicationAddress) {
81251
+ resolvedAgentByAddressCache.set(remote.communicationAddress.toLowerCase(), remote);
81252
+ }
81340
81253
  }
81341
- const agents = await fetchAgentsByIds(ids);
81342
- return agents.find((agent) => {
81343
- return sameAddress(agent.agentWalletAddress, walletAddress) || sameAddress(agent.ownerAddress, walletAddress);
81344
- }) ?? agents[0];
81254
+ return remote;
81345
81255
  }
81346
- async function resolveRemoteAgentFromJob(jobId, myAgentId) {
81256
+ async function resolveRemoteByAgentId(params) {
81257
+ const { service, agentId, fallbackAddress, logContext } = params;
81258
+ let remoteAgent;
81347
81259
  try {
81348
- const status = await fetchTaskStatus(jobId, myAgentId);
81349
- if (status?.providerAgentId) {
81350
- const [agent] = await fetchAgentsByIds([status.providerAgentId]);
81351
- if (agent?.communicationAddress) {
81352
- return agent;
81353
- }
81354
- }
81355
- const providerWallet = status?.providerAgentAddress ?? status?.designatedProvider;
81356
- if (providerWallet) {
81357
- const agent = await fetchAgentByWalletAddress(providerWallet);
81358
- if (agent?.communicationAddress) {
81359
- return agent;
81360
- }
81361
- }
81260
+ remoteAgent = await resolveAgentByIdFromCacheOrNetwork(service, agentId);
81362
81261
  } catch (err2) {
81363
- console.log(
81364
- `[okx-agent-task] failed to infer provider from task status job=${jobId} myAgentId=${myAgentId}:`,
81365
- err2
81366
- );
81262
+ if (!fallbackAddress) {
81263
+ throw err2;
81264
+ }
81265
+ logRecipientStatusLookupFailed(logContext, err2);
81367
81266
  }
81368
- try {
81369
- const recommendations = await fetchTaskRecommendations(jobId, myAgentId);
81370
- for (const recommendation of recommendations) {
81371
- if (recommendation.providerAgentId) {
81372
- const [agent] = await fetchAgentsByIds([recommendation.providerAgentId]);
81373
- if (agent?.communicationAddress) {
81374
- return agent;
81375
- }
81376
- }
81377
- if (recommendation.providerAddress) {
81378
- const agent = await fetchAgentByWalletAddress(recommendation.providerAddress);
81379
- if (agent?.communicationAddress) {
81380
- return agent;
81381
- }
81382
- }
81267
+ if (!remoteAgent?.communicationAddress && fallbackAddress) {
81268
+ try {
81269
+ const fallbackAgent = await resolveAgentByAddressFromCacheOrNetwork(service, fallbackAddress);
81270
+ remoteAgent = fallbackAgent ?? remoteAgent;
81271
+ } catch (err2) {
81272
+ console.log(
81273
+ `[okx-agent-task] remote agent lookup failed for ${fallbackAddress}, continuing without receiverAgentId:`,
81274
+ err2
81275
+ );
81383
81276
  }
81384
- } catch (err2) {
81385
- console.log(
81386
- `[okx-agent-task] failed to infer provider from recommendations job=${jobId} myAgentId=${myAgentId}:`,
81387
- err2
81388
- );
81389
81277
  }
81390
- return void 0;
81278
+ if (!remoteAgent?.communicationAddress && !fallbackAddress) {
81279
+ throw new Error(`No communication address found for toAgentId=${agentId}`);
81280
+ }
81281
+ assertRecipientAvailable(remoteAgent);
81282
+ return {
81283
+ toXmtpAddress: remoteAgent?.communicationAddress ?? fallbackAddress,
81284
+ toAgentId: remoteAgent?.agentId ?? agentId,
81285
+ remoteAgent
81286
+ };
81391
81287
  }
81392
81288
  async function resolveRemote(params) {
81393
81289
  const { command, file, service } = params;
81394
81290
  if (command.toAgentId) {
81395
- let remoteAgent;
81396
- try {
81397
- [remoteAgent] = await fetchAgentsByIds([command.toAgentId]);
81398
- } catch (err2) {
81399
- const fallbackAddress = command.toXmtpAddress ?? file.toXmtpAddress;
81400
- if (!fallbackAddress) {
81401
- throw err2;
81402
- }
81403
- logRecipientStatusLookupFailed(`toAgentId=${command.toAgentId}`, err2);
81404
- return {
81405
- toXmtpAddress: fallbackAddress,
81406
- toAgentId: command.toAgentId,
81407
- remoteAgent: void 0
81408
- };
81409
- }
81410
- if (!remoteAgent?.communicationAddress) {
81411
- throw new Error(`No communication address found for toAgentId=${command.toAgentId}`);
81412
- }
81413
- assertRecipientAvailable(remoteAgent);
81414
- return {
81415
- toXmtpAddress: remoteAgent.communicationAddress,
81416
- toAgentId: remoteAgent.agentId,
81417
- remoteAgent
81418
- };
81291
+ return resolveRemoteByAgentId({
81292
+ service,
81293
+ agentId: command.toAgentId,
81294
+ fallbackAddress: command.toXmtpAddress ?? file.toXmtpAddress,
81295
+ logContext: `toAgentId=${command.toAgentId}`
81296
+ });
81419
81297
  }
81420
81298
  const toXmtpAddress = command.toXmtpAddress ?? file.toXmtpAddress;
81421
81299
  if (toXmtpAddress) {
81422
- let remoteAgent = service.getAgentByAddress(toXmtpAddress);
81423
- if (!remoteAgent) {
81424
- try {
81425
- remoteAgent = await fetchAgentByAddress(toXmtpAddress);
81426
- } catch (err2) {
81427
- console.log(
81428
- `[okx-agent-task] remote agent lookup failed for ${toXmtpAddress}, continuing without receiverAgentId:`,
81429
- err2
81430
- );
81431
- }
81300
+ let remoteAgent;
81301
+ try {
81302
+ remoteAgent = await resolveAgentByAddressFromCacheOrNetwork(service, toXmtpAddress);
81303
+ } catch (err2) {
81304
+ console.log(
81305
+ `[okx-agent-task] remote agent lookup failed for ${toXmtpAddress}, continuing without receiverAgentId:`,
81306
+ err2
81307
+ );
81432
81308
  }
81433
81309
  assertRecipientAvailable(remoteAgent);
81434
81310
  return {
@@ -81437,59 +81313,29 @@ async function resolveRemote(params) {
81437
81313
  remoteAgent
81438
81314
  };
81439
81315
  }
81440
- if (command.myAgentId) {
81441
- const remoteAgent = await resolveRemoteAgentFromJob(command.jobId, command.myAgentId);
81442
- if (remoteAgent?.communicationAddress) {
81443
- assertRecipientAvailable(remoteAgent);
81444
- return {
81445
- toXmtpAddress: remoteAgent.communicationAddress,
81446
- toAgentId: remoteAgent.agentId,
81447
- remoteAgent
81448
- };
81449
- }
81450
- }
81451
81316
  throw new Error(
81452
- "job JSON has no xmtpGroupId and provider could not be inferred; pass --my-agent-id <id> or optionally --to-agent-id/--to-address"
81317
+ "job JSON has no xmtpGroupId or remote address; pass --to-agent-id or --to-address"
81453
81318
  );
81454
81319
  }
81455
81320
  async function resolveRemoteWithoutFile(params) {
81456
81321
  const { command, service } = params;
81457
81322
  if (command.toAgentId) {
81323
+ return resolveRemoteByAgentId({
81324
+ service,
81325
+ agentId: command.toAgentId,
81326
+ fallbackAddress: command.toXmtpAddress,
81327
+ logContext: `toAgentId=${command.toAgentId}`
81328
+ });
81329
+ }
81330
+ if (command.toXmtpAddress) {
81458
81331
  let remoteAgent;
81459
81332
  try {
81460
- [remoteAgent] = await fetchAgentsByIds([command.toAgentId]);
81333
+ remoteAgent = await resolveAgentByAddressFromCacheOrNetwork(service, command.toXmtpAddress);
81461
81334
  } catch (err2) {
81462
- if (!command.toXmtpAddress) {
81463
- throw err2;
81464
- }
81465
- logRecipientStatusLookupFailed(`toAgentId=${command.toAgentId}`, err2);
81466
- return {
81467
- toXmtpAddress: command.toXmtpAddress,
81468
- toAgentId: command.toAgentId,
81469
- remoteAgent: void 0
81470
- };
81471
- }
81472
- if (!remoteAgent?.communicationAddress) {
81473
- throw new Error(`No communication address found for toAgentId=${command.toAgentId}`);
81474
- }
81475
- assertRecipientAvailable(remoteAgent);
81476
- return {
81477
- toXmtpAddress: remoteAgent.communicationAddress,
81478
- toAgentId: remoteAgent.agentId,
81479
- remoteAgent
81480
- };
81481
- }
81482
- if (command.toXmtpAddress) {
81483
- let remoteAgent = service.getAgentByAddress(command.toXmtpAddress);
81484
- if (!remoteAgent) {
81485
- try {
81486
- remoteAgent = await fetchAgentByAddress(command.toXmtpAddress);
81487
- } catch (err2) {
81488
- console.log(
81489
- `[okx-agent-task] remote agent lookup failed for ${command.toXmtpAddress}, continuing without receiverAgentId:`,
81490
- err2
81491
- );
81492
- }
81335
+ console.log(
81336
+ `[okx-agent-task] remote agent lookup failed for ${command.toXmtpAddress}, continuing without receiverAgentId:`,
81337
+ err2
81338
+ );
81493
81339
  }
81494
81340
  assertRecipientAvailable(remoteAgent);
81495
81341
  return {
@@ -81627,16 +81473,14 @@ async function ensureGroupSession(params) {
81627
81473
  let remoteAgent;
81628
81474
  if (stored.toAgentId) {
81629
81475
  try {
81630
- [remoteAgent] = await fetchAgentsByIds([stored.toAgentId]);
81476
+ remoteAgent = await resolveAgentByIdFromCacheOrNetwork(service, stored.toAgentId);
81631
81477
  } catch (err2) {
81632
81478
  logRecipientStatusLookupFailed(`stored.toAgentId=${stored.toAgentId}`, err2);
81633
81479
  }
81634
- } else {
81635
- remoteAgent = service.getAgentByAddress(stored.toXmtpAddress);
81636
81480
  }
81637
81481
  if (!remoteAgent) {
81638
81482
  try {
81639
- remoteAgent = await fetchAgentByAddress(stored.toXmtpAddress);
81483
+ remoteAgent = await resolveAgentByAddressFromCacheOrNetwork(service, stored.toXmtpAddress);
81640
81484
  } catch {
81641
81485
  remoteAgent = void 0;
81642
81486
  }
@@ -83393,7 +83237,7 @@ function startUserAttentionWatcherCoordinator(options) {
83393
83237
  var environment = process.env.SENTRY_ENV === "dev" ? "dev" : "prod";
83394
83238
  var SENTRY_CONFIG = {
83395
83239
  projectName: "okx/openclaw-okx-a2a-extension",
83396
- release: "0.0.2-beta-38940d220c-260609234729",
83240
+ release: "0.0.4",
83397
83241
  environment
83398
83242
  };
83399
83243
 
@@ -83512,12 +83356,12 @@ async function runListenerWithLock(options, paths) {
83512
83356
  }));
83513
83357
  }
83514
83358
  });
83515
- service.setPluginVersion("0.0.2-beta-38940d220c-260609234729");
83359
+ service.setPluginVersion("0.0.4");
83516
83360
  await service.init();
83517
83361
  const pluginVersionStatus = service.pluginVersionStatus;
83518
83362
  if (pluginVersionStatus.unavailable) {
83519
83363
  throw new Error(
83520
- `@okxweb3/a2a-node v${"0.0.2-beta-38940d220c-260609234729"} is below the required minimum v${pluginVersionStatus.minVersion}`
83364
+ `@okxweb3/a2a-node v${"0.0.4"} is below the required minimum v${pluginVersionStatus.minVersion}`
83521
83365
  );
83522
83366
  }
83523
83367
  const systemConfig = service.getSystemConfig();
@@ -83535,7 +83379,7 @@ async function runListenerWithLock(options, paths) {
83535
83379
  onchainosAgentId: "*",
83536
83380
  reason: "system-config missing sentryDsn",
83537
83381
  pluginId: "@okxweb3/a2a-node",
83538
- pluginVersion: "0.0.2-beta-38940d220c-260609234729"
83382
+ pluginVersion: "0.0.4"
83539
83383
  });
83540
83384
  }
83541
83385
  console.log(
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@okxweb3/a2a-node",
3
- "version": "0.0.2-beta-38940d220c-260609234729",
3
+ "version": "0.0.4",
4
4
  "description": "Host-agnostic Node CLI for E2E encrypted agent-to-agent communication via XMTP",
5
5
  "main": "dist/index.js",
6
6
  "bin": {