@hashgraphonline/standards-sdk 0.0.83 → 0.0.85

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 (239) hide show
  1. package/dist/es/hcs-10/base-client.d.ts.map +1 -1
  2. package/dist/es/hcs-10/connections-manager.d.ts +203 -0
  3. package/dist/es/hcs-10/connections-manager.d.ts.map +1 -0
  4. package/dist/es/hcs-10/index.d.ts +1 -0
  5. package/dist/es/hcs-10/index.d.ts.map +1 -1
  6. package/dist/es/standards-sdk.es.js +13 -11
  7. package/dist/es/standards-sdk.es.js.map +1 -1
  8. package/dist/es/standards-sdk.es10.js +778 -115
  9. package/dist/es/standards-sdk.es10.js.map +1 -1
  10. package/dist/es/standards-sdk.es100.js +127 -138
  11. package/dist/es/standards-sdk.es100.js.map +1 -1
  12. package/dist/es/standards-sdk.es101.js +90 -22
  13. package/dist/es/standards-sdk.es101.js.map +1 -1
  14. package/dist/es/standards-sdk.es102.js +78 -787
  15. package/dist/es/standards-sdk.es102.js.map +1 -1
  16. package/dist/es/standards-sdk.es103.js +31 -3
  17. package/dist/es/standards-sdk.es103.js.map +1 -1
  18. package/dist/es/standards-sdk.es104.js +5 -142
  19. package/dist/es/standards-sdk.es104.js.map +1 -1
  20. package/dist/es/standards-sdk.es105.js +145 -160
  21. package/dist/es/standards-sdk.es105.js.map +1 -1
  22. package/dist/es/standards-sdk.es106.js +22 -33
  23. package/dist/es/standards-sdk.es106.js.map +1 -1
  24. package/dist/es/standards-sdk.es107.js +790 -30
  25. package/dist/es/standards-sdk.es107.js.map +1 -1
  26. package/dist/es/standards-sdk.es108.js +174 -9
  27. package/dist/es/standards-sdk.es108.js.map +1 -1
  28. package/dist/es/standards-sdk.es109.js +139 -30
  29. package/dist/es/standards-sdk.es109.js.map +1 -1
  30. package/dist/es/standards-sdk.es11.js +103 -58
  31. package/dist/es/standards-sdk.es11.js.map +1 -1
  32. package/dist/es/standards-sdk.es110.js +16 -414
  33. package/dist/es/standards-sdk.es110.js.map +1 -1
  34. package/dist/es/standards-sdk.es111.js +27 -2
  35. package/dist/es/standards-sdk.es111.js.map +1 -1
  36. package/dist/es/standards-sdk.es112.js +24 -118
  37. package/dist/es/standards-sdk.es112.js.map +1 -1
  38. package/dist/es/standards-sdk.es113.js +36 -19
  39. package/dist/es/standards-sdk.es113.js.map +1 -1
  40. package/dist/es/standards-sdk.es114.js +3 -26
  41. package/dist/es/standards-sdk.es114.js.map +1 -1
  42. package/dist/es/standards-sdk.es115.js +418 -25
  43. package/dist/es/standards-sdk.es115.js.map +1 -1
  44. package/dist/es/standards-sdk.es116.js +2 -38
  45. package/dist/es/standards-sdk.es116.js.map +1 -1
  46. package/dist/es/standards-sdk.es117.js +123 -0
  47. package/dist/es/standards-sdk.es117.js.map +1 -0
  48. package/dist/es/standards-sdk.es12.js +82 -628
  49. package/dist/es/standards-sdk.es12.js.map +1 -1
  50. package/dist/es/standards-sdk.es13.js +650 -85
  51. package/dist/es/standards-sdk.es13.js.map +1 -1
  52. package/dist/es/standards-sdk.es14.js +84 -58
  53. package/dist/es/standards-sdk.es14.js.map +1 -1
  54. package/dist/es/standards-sdk.es15.js +57 -20
  55. package/dist/es/standards-sdk.es15.js.map +1 -1
  56. package/dist/es/standards-sdk.es16.js +18 -156
  57. package/dist/es/standards-sdk.es16.js.map +1 -1
  58. package/dist/es/standards-sdk.es17.js +138 -385
  59. package/dist/es/standards-sdk.es17.js.map +1 -1
  60. package/dist/es/standards-sdk.es18.js +385 -315
  61. package/dist/es/standards-sdk.es18.js.map +1 -1
  62. package/dist/es/standards-sdk.es19.js +310 -128
  63. package/dist/es/standards-sdk.es19.js.map +1 -1
  64. package/dist/es/standards-sdk.es2.js +1 -12
  65. package/dist/es/standards-sdk.es2.js.map +1 -1
  66. package/dist/es/standards-sdk.es20.js +155 -7
  67. package/dist/es/standards-sdk.es20.js.map +1 -1
  68. package/dist/es/standards-sdk.es21.js +7 -1767
  69. package/dist/es/standards-sdk.es21.js.map +1 -1
  70. package/dist/es/standards-sdk.es22.js +1767 -36
  71. package/dist/es/standards-sdk.es22.js.map +1 -1
  72. package/dist/es/standards-sdk.es23.js +36 -48
  73. package/dist/es/standards-sdk.es23.js.map +1 -1
  74. package/dist/es/standards-sdk.es24.js +21 -7163
  75. package/dist/es/standards-sdk.es24.js.map +1 -1
  76. package/dist/es/standards-sdk.es25.js +7190 -3
  77. package/dist/es/standards-sdk.es25.js.map +1 -1
  78. package/dist/es/standards-sdk.es26.js +3 -2
  79. package/dist/es/standards-sdk.es26.js.map +1 -1
  80. package/dist/es/standards-sdk.es27.js +2 -4167
  81. package/dist/es/standards-sdk.es27.js.map +1 -1
  82. package/dist/es/standards-sdk.es28.js +10 -10
  83. package/dist/es/standards-sdk.es29.js +4167 -5
  84. package/dist/es/standards-sdk.es29.js.map +1 -1
  85. package/dist/es/standards-sdk.es3.js +1 -1
  86. package/dist/es/standards-sdk.es30.js +5 -101
  87. package/dist/es/standards-sdk.es30.js.map +1 -1
  88. package/dist/es/standards-sdk.es31.js +364 -2
  89. package/dist/es/standards-sdk.es31.js.map +1 -1
  90. package/dist/es/standards-sdk.es32.js +5 -15
  91. package/dist/es/standards-sdk.es32.js.map +1 -1
  92. package/dist/es/standards-sdk.es33.js +167 -397
  93. package/dist/es/standards-sdk.es33.js.map +1 -1
  94. package/dist/es/standards-sdk.es34.js +68 -351
  95. package/dist/es/standards-sdk.es34.js.map +1 -1
  96. package/dist/es/standards-sdk.es35.js +113 -5
  97. package/dist/es/standards-sdk.es35.js.map +1 -1
  98. package/dist/es/standards-sdk.es36.js +43 -170
  99. package/dist/es/standards-sdk.es36.js.map +1 -1
  100. package/dist/es/standards-sdk.es37.js +9 -80
  101. package/dist/es/standards-sdk.es37.js.map +1 -1
  102. package/dist/es/standards-sdk.es38.js +87 -104
  103. package/dist/es/standards-sdk.es38.js.map +1 -1
  104. package/dist/es/standards-sdk.es39.js +3 -51
  105. package/dist/es/standards-sdk.es39.js.map +1 -1
  106. package/dist/es/standards-sdk.es4.js +1 -1
  107. package/dist/es/standards-sdk.es40.js +2 -10
  108. package/dist/es/standards-sdk.es40.js.map +1 -1
  109. package/dist/es/standards-sdk.es41.js +102 -86
  110. package/dist/es/standards-sdk.es41.js.map +1 -1
  111. package/dist/es/standards-sdk.es42.js +73 -3
  112. package/dist/es/standards-sdk.es42.js.map +1 -1
  113. package/dist/es/standards-sdk.es43.js +6 -2
  114. package/dist/es/standards-sdk.es43.js.map +1 -1
  115. package/dist/es/standards-sdk.es44.js +4 -112
  116. package/dist/es/standards-sdk.es44.js.map +1 -1
  117. package/dist/es/standards-sdk.es45.js +216 -69
  118. package/dist/es/standards-sdk.es45.js.map +1 -1
  119. package/dist/es/standards-sdk.es46.js +58 -6
  120. package/dist/es/standards-sdk.es46.js.map +1 -1
  121. package/dist/es/standards-sdk.es47.js +69 -5
  122. package/dist/es/standards-sdk.es47.js.map +1 -1
  123. package/dist/es/standards-sdk.es48.js +3 -221
  124. package/dist/es/standards-sdk.es48.js.map +1 -1
  125. package/dist/es/standards-sdk.es49.js +158 -52
  126. package/dist/es/standards-sdk.es49.js.map +1 -1
  127. package/dist/es/standards-sdk.es5.js +6 -8
  128. package/dist/es/standards-sdk.es5.js.map +1 -1
  129. package/dist/es/standards-sdk.es50.js +32 -69
  130. package/dist/es/standards-sdk.es50.js.map +1 -1
  131. package/dist/es/standards-sdk.es51.js +63 -2282
  132. package/dist/es/standards-sdk.es51.js.map +1 -1
  133. package/dist/es/standards-sdk.es52.js +47 -161
  134. package/dist/es/standards-sdk.es52.js.map +1 -1
  135. package/dist/es/standards-sdk.es53.js +8 -7134
  136. package/dist/es/standards-sdk.es53.js.map +1 -1
  137. package/dist/es/standards-sdk.es54.js +65 -3
  138. package/dist/es/standards-sdk.es54.js.map +1 -1
  139. package/dist/es/standards-sdk.es55.js +6 -32
  140. package/dist/es/standards-sdk.es55.js.map +1 -1
  141. package/dist/es/standards-sdk.es56.js +13 -61
  142. package/dist/es/standards-sdk.es56.js.map +1 -1
  143. package/dist/es/standards-sdk.es57.js +7 -50
  144. package/dist/es/standards-sdk.es57.js.map +1 -1
  145. package/dist/es/standards-sdk.es58.js +45 -10
  146. package/dist/es/standards-sdk.es58.js.map +1 -1
  147. package/dist/es/standards-sdk.es59.js +2 -65
  148. package/dist/es/standards-sdk.es59.js.map +1 -1
  149. package/dist/es/standards-sdk.es60.js +135 -5
  150. package/dist/es/standards-sdk.es60.js.map +1 -1
  151. package/dist/es/standards-sdk.es61.js +170 -13
  152. package/dist/es/standards-sdk.es61.js.map +1 -1
  153. package/dist/es/standards-sdk.es62.js +101 -7
  154. package/dist/es/standards-sdk.es62.js.map +1 -1
  155. package/dist/es/standards-sdk.es63.js +2 -45
  156. package/dist/es/standards-sdk.es63.js.map +1 -1
  157. package/dist/es/standards-sdk.es64.js +16 -2
  158. package/dist/es/standards-sdk.es64.js.map +1 -1
  159. package/dist/es/standards-sdk.es65.js +404 -131
  160. package/dist/es/standards-sdk.es65.js.map +1 -1
  161. package/dist/es/standards-sdk.es66.js +12 -171
  162. package/dist/es/standards-sdk.es66.js.map +1 -1
  163. package/dist/es/standards-sdk.es67.js +13 -13
  164. package/dist/es/standards-sdk.es67.js.map +1 -1
  165. package/dist/es/standards-sdk.es68.js +32 -13
  166. package/dist/es/standards-sdk.es68.js.map +1 -1
  167. package/dist/es/standards-sdk.es69.js +15 -2
  168. package/dist/es/standards-sdk.es69.js.map +1 -1
  169. package/dist/es/standards-sdk.es7.js +8 -8
  170. package/dist/es/standards-sdk.es70.js +3 -31
  171. package/dist/es/standards-sdk.es70.js.map +1 -1
  172. package/dist/es/standards-sdk.es71.js +3 -14
  173. package/dist/es/standards-sdk.es71.js.map +1 -1
  174. package/dist/es/standards-sdk.es72.js +15 -3
  175. package/dist/es/standards-sdk.es72.js.map +1 -1
  176. package/dist/es/standards-sdk.es73.js +4 -3
  177. package/dist/es/standards-sdk.es73.js.map +1 -1
  178. package/dist/es/standards-sdk.es74.js +39 -16
  179. package/dist/es/standards-sdk.es74.js.map +1 -1
  180. package/dist/es/standards-sdk.es75.js +40 -5
  181. package/dist/es/standards-sdk.es75.js.map +1 -1
  182. package/dist/es/standards-sdk.es76.js +35 -37
  183. package/dist/es/standards-sdk.es76.js.map +1 -1
  184. package/dist/es/standards-sdk.es77.js +72 -34
  185. package/dist/es/standards-sdk.es77.js.map +1 -1
  186. package/dist/es/standards-sdk.es78.js +2281 -36
  187. package/dist/es/standards-sdk.es78.js.map +1 -1
  188. package/dist/es/standards-sdk.es79.js +2 -78
  189. package/dist/es/standards-sdk.es79.js.map +1 -1
  190. package/dist/es/standards-sdk.es8.js +8 -8
  191. package/dist/es/standards-sdk.es80.js +7136 -3
  192. package/dist/es/standards-sdk.es80.js.map +1 -1
  193. package/dist/es/standards-sdk.es81.js +3 -2
  194. package/dist/es/standards-sdk.es81.js.map +1 -1
  195. package/dist/es/standards-sdk.es82.js +2 -2
  196. package/dist/es/standards-sdk.es82.js.map +1 -1
  197. package/dist/es/standards-sdk.es83.js +2 -195
  198. package/dist/es/standards-sdk.es83.js.map +1 -1
  199. package/dist/es/standards-sdk.es84.js +31 -435
  200. package/dist/es/standards-sdk.es84.js.map +1 -1
  201. package/dist/es/standards-sdk.es85.js +31 -23
  202. package/dist/es/standards-sdk.es85.js.map +1 -1
  203. package/dist/es/standards-sdk.es86.js +9 -139
  204. package/dist/es/standards-sdk.es86.js.map +1 -1
  205. package/dist/es/standards-sdk.es87.js +32 -16
  206. package/dist/es/standards-sdk.es87.js.map +1 -1
  207. package/dist/es/standards-sdk.es88.js +189 -23
  208. package/dist/es/standards-sdk.es88.js.map +1 -1
  209. package/dist/es/standards-sdk.es89.js +433 -24
  210. package/dist/es/standards-sdk.es89.js.map +1 -1
  211. package/dist/es/standards-sdk.es9.js +2 -2
  212. package/dist/es/standards-sdk.es90.js +16 -12
  213. package/dist/es/standards-sdk.es90.js.map +1 -1
  214. package/dist/es/standards-sdk.es91.js +126 -31
  215. package/dist/es/standards-sdk.es91.js.map +1 -1
  216. package/dist/es/standards-sdk.es92.js +9 -9
  217. package/dist/es/standards-sdk.es92.js.map +1 -1
  218. package/dist/es/standards-sdk.es93.js +22 -54
  219. package/dist/es/standards-sdk.es93.js.map +1 -1
  220. package/dist/es/standards-sdk.es94.js +20 -1280
  221. package/dist/es/standards-sdk.es94.js.map +1 -1
  222. package/dist/es/standards-sdk.es95.js +17 -144
  223. package/dist/es/standards-sdk.es95.js.map +1 -1
  224. package/dist/es/standards-sdk.es96.js +39 -86
  225. package/dist/es/standards-sdk.es96.js.map +1 -1
  226. package/dist/es/standards-sdk.es97.js +13 -79
  227. package/dist/es/standards-sdk.es97.js.map +1 -1
  228. package/dist/es/standards-sdk.es98.js +57 -28
  229. package/dist/es/standards-sdk.es98.js.map +1 -1
  230. package/dist/es/standards-sdk.es99.js +1288 -5
  231. package/dist/es/standards-sdk.es99.js.map +1 -1
  232. package/dist/umd/hcs-10/base-client.d.ts.map +1 -1
  233. package/dist/umd/hcs-10/connections-manager.d.ts +203 -0
  234. package/dist/umd/hcs-10/connections-manager.d.ts.map +1 -0
  235. package/dist/umd/hcs-10/index.d.ts +1 -0
  236. package/dist/umd/hcs-10/index.d.ts.map +1 -1
  237. package/dist/umd/standards-sdk.umd.js +9 -9
  238. package/dist/umd/standards-sdk.umd.js.map +1 -1
  239. package/package.json +10 -4
@@ -1,152 +1,815 @@
1
- import { InboundTopicType } from "./standards-sdk.es13.js";
2
- import { Logger } from "./standards-sdk.es14.js";
3
- class AgentBuilder {
4
- constructor() {
5
- this.config = {};
6
- this.logger = Logger.getInstance({
7
- module: "AgentBuilder"
8
- });
9
- }
10
- setName(name) {
11
- this.config.name = name;
12
- return this;
13
- }
14
- setAlias(alias) {
15
- this.config.alias = alias;
16
- return this;
1
+ import { Logger } from "./standards-sdk.es15.js";
2
+ class ConnectionsManager {
3
+ /**
4
+ * Creates a new ConnectionsManager instance
5
+ */
6
+ constructor(options) {
7
+ this.connections = /* @__PURE__ */ new Map();
8
+ this.pendingRequests = /* @__PURE__ */ new Map();
9
+ this.profileCache = /* @__PURE__ */ new Map();
10
+ this.filterPendingAccountIds = /* @__PURE__ */ new Set();
11
+ const loggerOptions = {
12
+ module: "ConnectionsManager",
13
+ level: options?.logLevel || "info",
14
+ prettyPrint: true
15
+ };
16
+ this.logger = new Logger(loggerOptions);
17
+ if (options?.filterPendingAccountIds) {
18
+ this.filterPendingAccountIds = new Set(options.filterPendingAccountIds);
19
+ }
20
+ if (!options.baseClient) {
21
+ throw new Error("ConnectionsManager requires a baseClient to operate");
22
+ }
23
+ this.baseClient = options.baseClient;
17
24
  }
18
- setBio(bio) {
19
- this.config.bio = bio;
20
- return this;
25
+ /**
26
+ * Fetches and processes connection data using the configured client
27
+ * @param accountId - The account ID to fetch connection data for
28
+ * @returns A promise that resolves to an array of Connection objects
29
+ */
30
+ async fetchConnectionData(accountId) {
31
+ try {
32
+ const topicInfo = await this.baseClient.retrieveCommunicationTopics(
33
+ accountId
34
+ );
35
+ const isValidTopicId = (topicId) => {
36
+ return Boolean(topicId) && !topicId.includes(":");
37
+ };
38
+ if (!isValidTopicId(topicInfo.inboundTopic) || !isValidTopicId(topicInfo.outboundTopic)) {
39
+ this.logger.warn(
40
+ "Invalid topic IDs detected in retrieved communication topics"
41
+ );
42
+ return this.getAllConnections();
43
+ }
44
+ const [outboundMessagesResult, inboundMessagesResult] = await Promise.all(
45
+ [
46
+ this.baseClient.getMessages(topicInfo.outboundTopic),
47
+ this.baseClient.getMessages(topicInfo.inboundTopic)
48
+ ]
49
+ );
50
+ this.processOutboundMessages(
51
+ outboundMessagesResult.messages || [],
52
+ accountId
53
+ );
54
+ this.processInboundMessages(inboundMessagesResult.messages || []);
55
+ const pendingCount = Array.from(this.connections.values()).filter(
56
+ (conn) => conn.status === "pending" || conn.isPending
57
+ ).length;
58
+ this.logger.debug(
59
+ `Processed ${outboundMessagesResult.messages?.length || 0} outbound and ${inboundMessagesResult.messages?.length || 0} inbound messages. Found ${pendingCount} pending connections.`
60
+ );
61
+ await this.checkTargetInboundTopicsForConfirmations();
62
+ await this.checkOutboundRequestsForConfirmations();
63
+ await this.fetchProfilesForConnections(accountId);
64
+ await this.fetchConnectionActivity();
65
+ return this.getAllConnections();
66
+ } catch (error) {
67
+ this.logger.error("Error fetching connection data:", error);
68
+ return this.getAllConnections();
69
+ }
21
70
  }
22
71
  /**
23
- * @deprecated Use setBio instead
72
+ * Checks target agent inbound topics to find confirmations for pending requests
73
+ * that might not be visible in our local messages
24
74
  */
25
- setDescription(description) {
26
- this.config.bio = description;
27
- return this;
75
+ async checkTargetInboundTopicsForConfirmations() {
76
+ const pendingConnections = Array.from(this.connections.values()).filter(
77
+ (conn) => (conn.isPending || conn.status === "pending") && conn.targetInboundTopicId
78
+ );
79
+ if (pendingConnections.length === 0) {
80
+ return;
81
+ }
82
+ const pendingRequestsByTarget = /* @__PURE__ */ new Map();
83
+ pendingConnections.forEach((conn) => {
84
+ if (conn.targetInboundTopicId) {
85
+ const requests = pendingRequestsByTarget.get(conn.targetInboundTopicId) || [];
86
+ requests.push(conn);
87
+ pendingRequestsByTarget.set(conn.targetInboundTopicId, requests);
88
+ }
89
+ });
90
+ const MAX_FETCH_ATTEMPTS = 2;
91
+ const FETCH_DELAY_MS = 500;
92
+ for (const [
93
+ targetInboundTopicId,
94
+ requests
95
+ ] of pendingRequestsByTarget.entries()) {
96
+ for (let attempt = 1; attempt <= MAX_FETCH_ATTEMPTS; attempt++) {
97
+ try {
98
+ const targetMessagesResult = await this.baseClient.getMessages(
99
+ targetInboundTopicId
100
+ );
101
+ const targetMessages = targetMessagesResult.messages || [];
102
+ let confirmedAny = false;
103
+ for (const conn of requests) {
104
+ const requestId = conn.connectionRequestId;
105
+ if (!requestId) {
106
+ continue;
107
+ }
108
+ const confirmationMsg = targetMessages.find((msg) => {
109
+ if (msg.op !== "connection_created" || !msg.connection_topic_id) {
110
+ return false;
111
+ }
112
+ if (msg.connection_id !== requestId) {
113
+ return false;
114
+ }
115
+ if (conn.uniqueRequestKey) {
116
+ const keyParts = conn.uniqueRequestKey.split(":");
117
+ if (keyParts.length > 1) {
118
+ const operatorIdPart = keyParts[1];
119
+ if (msg.operator_id && msg.operator_id === operatorIdPart) {
120
+ return true;
121
+ }
122
+ if (msg.connected_account_id === conn.targetAccountId) {
123
+ return true;
124
+ }
125
+ }
126
+ }
127
+ return true;
128
+ });
129
+ if (confirmationMsg?.connection_topic_id) {
130
+ confirmedAny = true;
131
+ const connectionTopicId = confirmationMsg.connection_topic_id;
132
+ let pendingKey = conn.uniqueRequestKey;
133
+ const newConnection = {
134
+ connectionTopicId,
135
+ targetAccountId: conn.targetAccountId,
136
+ targetAgentName: conn.targetAgentName,
137
+ targetInboundTopicId: conn.targetInboundTopicId,
138
+ status: "established",
139
+ isPending: false,
140
+ needsConfirmation: false,
141
+ created: new Date(confirmationMsg.created || conn.created),
142
+ profileInfo: conn.profileInfo,
143
+ connectionRequestId: requestId,
144
+ uniqueRequestKey: conn.uniqueRequestKey,
145
+ originTopicId: conn.originTopicId,
146
+ processed: conn.processed
147
+ };
148
+ this.connections.set(connectionTopicId, newConnection);
149
+ if (pendingKey) {
150
+ this.connections.delete(pendingKey);
151
+ }
152
+ this.logger.debug(
153
+ `Confirmed connection in target inbound topic: ${connectionTopicId}`
154
+ );
155
+ }
156
+ }
157
+ if (confirmedAny || attempt === MAX_FETCH_ATTEMPTS) {
158
+ break;
159
+ }
160
+ await new Promise((resolve) => setTimeout(resolve, FETCH_DELAY_MS));
161
+ } catch (error) {
162
+ this.logger.debug(
163
+ `Error fetching target inbound topic ${targetInboundTopicId}:`,
164
+ error
165
+ );
166
+ if (attempt === MAX_FETCH_ATTEMPTS) {
167
+ break;
168
+ }
169
+ await new Promise((resolve) => setTimeout(resolve, FETCH_DELAY_MS));
170
+ }
171
+ }
172
+ }
28
173
  }
29
- setCapabilities(capabilities) {
30
- this.config.capabilities = capabilities;
31
- return this;
174
+ /**
175
+ * Checks target agents' inbound topics for confirmations of our outbound connection requests
176
+ * This complements checkTargetInboundTopicsForConfirmations by looking for confirmations
177
+ * that might have been sent to the target agent's inbound topic rather than our own
178
+ */
179
+ async checkOutboundRequestsForConfirmations() {
180
+ const allConnections = Array.from(this.connections.values());
181
+ this.logger.info(`Total connections in map: ${allConnections.length}`);
182
+ const pendingByStatus = allConnections.filter(
183
+ (conn) => conn.status === "pending"
184
+ );
185
+ this.logger.info(
186
+ `Connections with status='pending': ${pendingByStatus.length}`
187
+ );
188
+ const pendingConnections = allConnections.filter(
189
+ (conn) => conn.status === "pending"
190
+ );
191
+ if (!Boolean(pendingConnections?.length)) {
192
+ this.logger.info("No pending connections found");
193
+ return;
194
+ }
195
+ for (const conn of pendingConnections) {
196
+ this.logger.debug(
197
+ `Processing pending connection: ${conn.connectionTopicId}`
198
+ );
199
+ if (!conn.targetAccountId) {
200
+ this.logger.debug(
201
+ `Skipping connection ${conn.connectionTopicId} - no targetAccountId`
202
+ );
203
+ continue;
204
+ }
205
+ let targetInboundTopicId = conn.targetInboundTopicId;
206
+ if (!targetInboundTopicId) {
207
+ try {
208
+ const profileResponse = await this.baseClient.retrieveProfile(
209
+ conn.targetAccountId
210
+ );
211
+ if (profileResponse?.profile?.inboundTopicId) {
212
+ targetInboundTopicId = profileResponse.profile.inboundTopicId;
213
+ this.connections.set(conn.connectionTopicId, {
214
+ ...conn,
215
+ targetInboundTopicId
216
+ });
217
+ this.logger.debug(
218
+ `Updated connection ${conn.connectionTopicId} with inbound topic ID: ${targetInboundTopicId}`
219
+ );
220
+ } else {
221
+ this.logger.debug(
222
+ `Couldn't get inbound topic ID for account ${conn.targetAccountId}`
223
+ );
224
+ continue;
225
+ }
226
+ } catch (error) {
227
+ this.logger.debug(
228
+ `Error fetching profile for ${conn.targetAccountId}: ${error}`
229
+ );
230
+ continue;
231
+ }
232
+ }
233
+ if (!targetInboundTopicId || targetInboundTopicId.includes(":")) {
234
+ this.logger.debug(
235
+ `Skipping invalid inbound topic format: ${targetInboundTopicId}`
236
+ );
237
+ continue;
238
+ }
239
+ const requestId = conn.connectionRequestId || conn.inboundRequestId;
240
+ if (!requestId) {
241
+ this.logger.debug(
242
+ `Skipping connection ${conn.connectionTopicId} - no request ID`
243
+ );
244
+ continue;
245
+ }
246
+ try {
247
+ this.logger.debug(
248
+ `Checking for confirmations on topic ${targetInboundTopicId} for request ID ${requestId}`
249
+ );
250
+ const targetMessagesResult = await this.baseClient.getMessages(
251
+ targetInboundTopicId
252
+ );
253
+ const targetMessages = targetMessagesResult.messages || [];
254
+ const confirmationMsg = targetMessages.find(
255
+ (msg) => msg.op === "connection_created" && msg.connection_id === requestId && msg.connection_topic_id
256
+ );
257
+ if (confirmationMsg?.connection_topic_id) {
258
+ const connectionTopicId = confirmationMsg.connection_topic_id;
259
+ this.logger.info(
260
+ `Found confirmation for request #${requestId} to ${conn.targetAccountId} on their inbound topic`
261
+ );
262
+ this.connections.set(conn.connectionTopicId, {
263
+ ...conn,
264
+ connectionTopicId,
265
+ status: "established",
266
+ isPending: false,
267
+ needsConfirmation: false,
268
+ created: new Date(confirmationMsg.created || conn.created),
269
+ lastActivity: new Date(confirmationMsg.created || conn.created)
270
+ });
271
+ } else {
272
+ this.logger.debug(
273
+ `No confirmation found for request ID ${requestId} on topic ${targetInboundTopicId}`
274
+ );
275
+ }
276
+ } catch (error) {
277
+ this.logger.warn(
278
+ `Error checking for confirmations on target inbound topic for ${conn.targetAccountId}: ${error}`
279
+ );
280
+ }
281
+ }
32
282
  }
33
283
  /**
34
- * @deprecated Use setType instead
284
+ * Fetches profiles for all connected accounts
285
+ * @param accountId - The account ID making the request
35
286
  */
36
- setAgentType(type) {
37
- if (!this.config.metadata) {
38
- this.config.metadata = { type };
39
- } else {
40
- this.config.metadata.type = type;
287
+ async fetchProfilesForConnections(accountId) {
288
+ const targetAccountIds = /* @__PURE__ */ new Set();
289
+ for (const connection of this.connections.values()) {
290
+ if (connection.targetAccountId && !this.profileCache.has(connection.targetAccountId)) {
291
+ targetAccountIds.add(connection.targetAccountId);
292
+ }
41
293
  }
42
- return this;
294
+ const accountIdPromises = Array.from(targetAccountIds).map(
295
+ async (targetId) => {
296
+ try {
297
+ const profileResponse = await this.baseClient.retrieveProfile(
298
+ targetId
299
+ );
300
+ if (profileResponse.success && profileResponse.profile) {
301
+ this.addProfileInfo(targetId, profileResponse.profile);
302
+ this.updatePendingConnectionsWithProfileInfo(
303
+ targetId,
304
+ profileResponse.profile
305
+ );
306
+ }
307
+ } catch (error) {
308
+ this.logger.debug(`Failed to fetch profile for ${targetId}:`, error);
309
+ }
310
+ }
311
+ );
312
+ await Promise.allSettled(accountIdPromises);
43
313
  }
44
- setType(type) {
45
- if (!this.config.metadata) {
46
- this.config.metadata = { type };
47
- } else {
48
- this.config.metadata.type = type;
314
+ /**
315
+ * Updates pending connections with inbound topic IDs from profile info
316
+ * @param accountId - The account ID to update connections for
317
+ * @param profile - The profile containing the inbound topic ID
318
+ */
319
+ updatePendingConnectionsWithProfileInfo(accountId, profile) {
320
+ const pendingConnections = Array.from(this.connections.values()).filter(
321
+ (conn) => conn.targetAccountId === accountId && (conn.isPending || conn.needsConfirmation) && !conn.targetInboundTopicId
322
+ );
323
+ if (pendingConnections.length > 0 && profile.inboundTopicId) {
324
+ for (const conn of pendingConnections) {
325
+ const updatedConn = {
326
+ ...conn,
327
+ targetInboundTopicId: profile.inboundTopicId
328
+ };
329
+ this.connections.set(conn.connectionTopicId, updatedConn);
330
+ }
49
331
  }
50
- return this;
51
332
  }
52
- setModel(model) {
53
- if (!this.config.metadata) {
54
- this.config.metadata = { type: "manual" };
333
+ /**
334
+ * Fetches activity from active connection topics
335
+ * Updates the lastActivity timestamp for each connection based on latest messages
336
+ * @returns Promise that resolves when all activity has been fetched
337
+ */
338
+ async fetchConnectionActivity() {
339
+ const activeConnections = this.getActiveConnections();
340
+ const validConnections = activeConnections.filter((connection) => {
341
+ const topicId = connection.connectionTopicId;
342
+ if (!topicId || topicId.includes(":") || !topicId.match(/^0\.0\.\d+$/)) {
343
+ this.logger.debug(
344
+ `Skipping activity fetch for invalid topic ID format: ${topicId}`
345
+ );
346
+ return false;
347
+ }
348
+ return true;
349
+ });
350
+ const activityPromises = validConnections.map(async (connection) => {
351
+ try {
352
+ const topicId = connection.connectionTopicId;
353
+ const messagesResult = await this.baseClient.getMessages(topicId);
354
+ if (messagesResult?.messages?.length > 0) {
355
+ this.processConnectionMessages(topicId, messagesResult.messages);
356
+ }
357
+ } catch (error) {
358
+ this.logger.debug(
359
+ `Failed to fetch activity for ${connection.connectionTopicId}:`,
360
+ error
361
+ );
362
+ }
363
+ });
364
+ await Promise.allSettled(activityPromises);
365
+ }
366
+ /**
367
+ * Checks if an account should be filtered, taking into account existing established connections
368
+ * @param accountId - The account ID to check
369
+ * @returns True if the account should be filtered, false otherwise
370
+ */
371
+ shouldFilterAccount(accountId) {
372
+ if (!this.filterPendingAccountIds.has(accountId)) {
373
+ return false;
374
+ }
375
+ if (this.hasEstablishedConnectionWithAccount(accountId)) {
376
+ return false;
55
377
  }
56
- this.config.metadata.model = model;
57
- return this;
378
+ return true;
58
379
  }
59
- setCreator(creator) {
60
- if (!this.config.metadata) {
61
- this.config.metadata = { type: "manual" };
380
+ /**
381
+ * Process outbound messages to track connection requests and confirmations
382
+ * @param messages - The messages to process
383
+ * @param accountId - The account ID that sent the messages
384
+ * @returns Array of connections after processing
385
+ */
386
+ processOutboundMessages(messages, accountId) {
387
+ if (!Boolean(messages?.length)) {
388
+ return Array.from(this.connections.values());
62
389
  }
63
- this.config.metadata.creator = creator;
64
- return this;
390
+ const requestMessages = messages.filter(
391
+ (msg) => msg.op === "connection_request" && msg.connection_request_id
392
+ );
393
+ for (const msg of requestMessages) {
394
+ const requestId = msg.connection_request_id;
395
+ const operatorId = msg.operator_id || "";
396
+ const targetAccountId = this.baseClient.extractAccountFromOperatorId(operatorId);
397
+ const targetInboundTopicId = this.baseClient.extractTopicFromOperatorId(operatorId);
398
+ if (this.shouldFilterAccount(targetAccountId)) {
399
+ this.logger.debug(
400
+ `Filtering out outbound request to account: ${targetAccountId}`
401
+ );
402
+ continue;
403
+ }
404
+ const isAlreadyConfirmed = Array.from(this.connections.values()).some(
405
+ (conn) => conn.connectionRequestId === requestId && !conn.isPending && conn.targetAccountId === targetAccountId
406
+ );
407
+ const pendingKey = `req-${requestId}:${operatorId}`;
408
+ if (!isAlreadyConfirmed && !this.pendingRequests.has(pendingKey)) {
409
+ const pendingRequest = {
410
+ id: requestId,
411
+ requesterId: accountId,
412
+ requesterTopicId: msg.outbound_topic_id || "",
413
+ targetAccountId,
414
+ targetTopicId: targetInboundTopicId,
415
+ operatorId,
416
+ sequenceNumber: msg.sequence_number,
417
+ created: msg.created || /* @__PURE__ */ new Date(),
418
+ memo: msg.m,
419
+ status: "pending"
420
+ };
421
+ this.pendingRequests.set(pendingKey, pendingRequest);
422
+ if (!this.connections.has(pendingKey)) {
423
+ const pendingConnection = {
424
+ connectionTopicId: pendingKey,
425
+ targetAccountId,
426
+ targetInboundTopicId,
427
+ status: "pending",
428
+ isPending: true,
429
+ needsConfirmation: false,
430
+ created: msg.created || /* @__PURE__ */ new Date(),
431
+ connectionRequestId: requestId,
432
+ uniqueRequestKey: pendingKey,
433
+ originTopicId: msg.outbound_topic_id || "",
434
+ processed: false
435
+ };
436
+ this.connections.set(pendingKey, pendingConnection);
437
+ }
438
+ }
439
+ }
440
+ const confirmationMessages = messages.filter(
441
+ (msg) => msg.op === "connection_created" && msg.connection_topic_id && msg.connection_request_id
442
+ );
443
+ for (const msg of confirmationMessages) {
444
+ const requestId = msg.connection_request_id;
445
+ const connectionTopicId = msg.connection_topic_id;
446
+ const targetAccountId = this.baseClient.extractAccountFromOperatorId(
447
+ msg.operator_id || ""
448
+ );
449
+ if (this.shouldFilterAccount(targetAccountId)) {
450
+ this.logger.debug(
451
+ `Filtering out outbound confirmation to account: ${targetAccountId}`
452
+ );
453
+ continue;
454
+ }
455
+ const pendingKey = `req-${requestId}:${msg.operator_id}`;
456
+ const pendingRequest = this.pendingRequests.get(pendingKey);
457
+ if (pendingRequest) {
458
+ pendingRequest.status = "confirmed";
459
+ }
460
+ if (this.connections.has(pendingKey)) {
461
+ this.connections.delete(pendingKey);
462
+ }
463
+ if (!this.connections.has(connectionTopicId)) {
464
+ this.connections.set(connectionTopicId, {
465
+ connectionTopicId,
466
+ targetAccountId,
467
+ status: "established",
468
+ isPending: false,
469
+ needsConfirmation: false,
470
+ created: msg.created || /* @__PURE__ */ new Date(),
471
+ connectionRequestId: requestId,
472
+ confirmedRequestId: msg.confirmed_request_id,
473
+ requesterOutboundTopicId: msg.outbound_topic_id,
474
+ uniqueRequestKey: pendingKey,
475
+ originTopicId: msg.outbound_topic_id || "",
476
+ processed: false
477
+ });
478
+ } else {
479
+ const conn = this.connections.get(connectionTopicId);
480
+ this.connections.set(connectionTopicId, {
481
+ ...conn,
482
+ status: "established",
483
+ isPending: false,
484
+ needsConfirmation: false,
485
+ connectionRequestId: requestId,
486
+ confirmedRequestId: msg.confirmed_request_id,
487
+ requesterOutboundTopicId: msg.outbound_topic_id,
488
+ uniqueRequestKey: pendingKey,
489
+ originTopicId: msg.outbound_topic_id || "",
490
+ processed: false
491
+ });
492
+ }
493
+ }
494
+ const closedMessages = messages.filter(
495
+ (msg) => msg.op === "connection_closed" || msg.op === "close_connection" && msg.connection_topic_id
496
+ );
497
+ for (const msg of closedMessages) {
498
+ const connectionTopicId = msg.connection_topic_id;
499
+ if (this.connections.has(connectionTopicId)) {
500
+ const conn = this.connections.get(connectionTopicId);
501
+ if (this.shouldFilterAccount(conn.targetAccountId) && conn.status !== "established") {
502
+ continue;
503
+ }
504
+ const uniqueKey = msg.connection_request_id && msg.operator_id ? `req-${msg.connection_request_id}:${msg.operator_id}` : void 0;
505
+ this.connections.set(connectionTopicId, {
506
+ ...conn,
507
+ status: "closed",
508
+ isPending: false,
509
+ needsConfirmation: false,
510
+ lastActivity: msg.created || /* @__PURE__ */ new Date(),
511
+ closedReason: msg.reason,
512
+ closeMethod: msg.close_method,
513
+ uniqueRequestKey: uniqueKey,
514
+ originTopicId: conn.originTopicId,
515
+ processed: false
516
+ });
517
+ }
518
+ }
519
+ return Array.from(this.connections.values()).filter(
520
+ (conn) => conn.status === "established" || conn.status === "closed" || !this.filterPendingAccountIds.has(conn.targetAccountId)
521
+ );
65
522
  }
66
- addSocial(platform, handle) {
67
- if (!this.config.metadata) {
68
- this.config.metadata = { type: "manual" };
523
+ /**
524
+ * Process inbound messages to track connection requests and confirmations
525
+ * @param messages - The messages to process
526
+ * @returns Array of connections after processing
527
+ */
528
+ processInboundMessages(messages) {
529
+ if (!Boolean(messages?.length)) {
530
+ return Array.from(this.connections.values());
69
531
  }
70
- if (!this.config.metadata.socials) {
71
- this.config.metadata.socials = {};
532
+ const requestMessages = messages.filter(
533
+ (msg) => msg.op === "connection_request" && msg.sequence_number
534
+ );
535
+ for (const msg of requestMessages) {
536
+ const sequenceNumber = msg.sequence_number;
537
+ const operatorId = msg.operator_id || "";
538
+ const requestorAccountId = this.baseClient.extractAccountFromOperatorId(operatorId);
539
+ const requestorTopicId = this.baseClient.extractTopicFromOperatorId(operatorId);
540
+ if (this.shouldFilterAccount(requestorAccountId)) {
541
+ this.logger.debug(
542
+ `Filtering out request from account: ${requestorAccountId}`
543
+ );
544
+ continue;
545
+ }
546
+ const needsConfirmKey = `inb-${sequenceNumber}:${operatorId}`;
547
+ if (!this.connections.has(needsConfirmKey)) {
548
+ this.connections.set(needsConfirmKey, {
549
+ connectionTopicId: needsConfirmKey,
550
+ targetAccountId: requestorAccountId,
551
+ targetInboundTopicId: requestorTopicId,
552
+ status: "needs_confirmation",
553
+ isPending: false,
554
+ needsConfirmation: true,
555
+ created: msg.created || /* @__PURE__ */ new Date(),
556
+ inboundRequestId: sequenceNumber,
557
+ uniqueRequestKey: needsConfirmKey,
558
+ originTopicId: requestorTopicId,
559
+ processed: false
560
+ });
561
+ }
72
562
  }
73
- this.config.metadata.socials[platform] = handle;
74
- return this;
563
+ const confirmationMessages = messages.filter(
564
+ (msg) => msg.op === "connection_created" && msg.connection_topic_id && msg.connection_id
565
+ );
566
+ for (const msg of confirmationMessages) {
567
+ const sequenceNumber = msg.connection_id;
568
+ const connectionTopicId = msg.connection_topic_id;
569
+ const connectedAccountId = msg.connected_account_id || "";
570
+ const operatorId = msg.operator_id || "";
571
+ if (this.shouldFilterAccount(connectedAccountId)) {
572
+ this.logger.debug(
573
+ `Filtering out confirmation for account: ${connectedAccountId}`
574
+ );
575
+ continue;
576
+ }
577
+ const needsConfirmKey = `inb-${sequenceNumber}:${operatorId}`;
578
+ if (this.connections.has(needsConfirmKey)) {
579
+ this.connections.delete(needsConfirmKey);
580
+ }
581
+ if (!this.connections.has(connectionTopicId)) {
582
+ this.connections.set(connectionTopicId, {
583
+ connectionTopicId,
584
+ targetAccountId: connectedAccountId,
585
+ status: "established",
586
+ isPending: false,
587
+ needsConfirmation: false,
588
+ created: msg.created || /* @__PURE__ */ new Date(),
589
+ inboundRequestId: sequenceNumber,
590
+ uniqueRequestKey: needsConfirmKey,
591
+ originTopicId: msg.connection_topic_id,
592
+ processed: false
593
+ });
594
+ } else {
595
+ const conn = this.connections.get(connectionTopicId);
596
+ this.connections.set(connectionTopicId, {
597
+ ...conn,
598
+ status: "established",
599
+ isPending: false,
600
+ needsConfirmation: false,
601
+ inboundRequestId: sequenceNumber,
602
+ uniqueRequestKey: needsConfirmKey,
603
+ originTopicId: msg.connection_topic_id,
604
+ processed: false
605
+ });
606
+ }
607
+ }
608
+ return Array.from(this.connections.values()).filter(
609
+ (conn) => conn.status === "established" || conn.status === "closed" || !this.filterPendingAccountIds.has(conn.targetAccountId)
610
+ );
75
611
  }
76
- addProperty(key, value) {
77
- if (!this.config.metadata) {
78
- this.config.metadata = { type: "manual" };
612
+ /**
613
+ * Process connection topic messages to update last activity time
614
+ * @param connectionTopicId - The topic ID of the connection
615
+ * @param messages - The messages to process
616
+ * @returns The updated connection or undefined if not found
617
+ */
618
+ processConnectionMessages(connectionTopicId, messages) {
619
+ if (!messages || messages.length === 0 || !this.connections.has(connectionTopicId)) {
620
+ return this.connections.get(connectionTopicId);
621
+ }
622
+ const latestMessage = messages.filter((m) => m.created).sort((a, b) => {
623
+ const dateA = a.created ? new Date(a.created).getTime() : 0;
624
+ const dateB = b.created ? new Date(b.created).getTime() : 0;
625
+ return dateB - dateA;
626
+ })[0];
627
+ if (latestMessage?.created) {
628
+ const conn = this.connections.get(connectionTopicId);
629
+ this.connections.set(connectionTopicId, {
630
+ ...conn,
631
+ lastActivity: latestMessage.created
632
+ });
79
633
  }
80
- if (!this.config.metadata.properties) {
81
- this.config.metadata.properties = {};
634
+ const closeMessage = messages.find((msg) => msg.op === "close_connection");
635
+ if (closeMessage) {
636
+ const conn = this.connections.get(connectionTopicId);
637
+ this.connections.set(connectionTopicId, {
638
+ ...conn,
639
+ status: "closed",
640
+ lastActivity: closeMessage.created || /* @__PURE__ */ new Date(),
641
+ closedReason: closeMessage.reason,
642
+ closeMethod: "explicit"
643
+ });
82
644
  }
83
- this.config.metadata.properties[key] = value;
84
- return this;
645
+ return this.connections.get(connectionTopicId);
85
646
  }
86
- setMetadata(metadata) {
87
- this.config.metadata = metadata;
88
- return this;
647
+ /**
648
+ * Adds or updates profile information for a connection
649
+ * @param accountId - The account ID to add profile info for
650
+ * @param profile - The profile information
651
+ */
652
+ addProfileInfo(accountId, profile) {
653
+ this.profileCache.set(accountId, profile);
654
+ const matchingConnections = Array.from(this.connections.values()).filter(
655
+ (conn) => conn.targetAccountId === accountId
656
+ );
657
+ for (const conn of matchingConnections) {
658
+ this.connections.set(conn.connectionTopicId, {
659
+ ...conn,
660
+ profileInfo: profile,
661
+ targetAgentName: profile.display_name,
662
+ targetInboundTopicId: profile.inboundTopicId,
663
+ targetOutboundTopicId: profile.outboundTopicId
664
+ });
665
+ }
89
666
  }
90
- setProfilePicture(pfpBuffer, pfpFileName) {
91
- this.config.pfpBuffer = pfpBuffer;
92
- this.config.pfpFileName = pfpFileName;
93
- return this;
667
+ /**
668
+ * Gets all connections
669
+ * @returns Array of all connections that should be visible
670
+ */
671
+ getAllConnections() {
672
+ const connections = Array.from(this.connections.values()).filter(
673
+ (conn) => conn.status === "established" || conn.status === "closed" || !this.filterPendingAccountIds.has(conn.targetAccountId) || this.hasEstablishedConnectionWithAccount(conn.targetAccountId)
674
+ );
675
+ return connections;
94
676
  }
95
- setExistingProfilePicture(pfpTopicId) {
96
- this.config.existingPfpTopicId = pfpTopicId;
97
- return this;
677
+ /**
678
+ * Gets all pending connection requests
679
+ * @returns Array of pending connection requests
680
+ */
681
+ getPendingRequests() {
682
+ const pendingConnections = Array.from(this.connections.values()).filter(
683
+ (conn) => {
684
+ return conn.isPending && (!this.filterPendingAccountIds.has(conn.targetAccountId) || this.hasEstablishedConnectionWithAccount(conn.targetAccountId));
685
+ }
686
+ );
687
+ return pendingConnections;
98
688
  }
99
- setNetwork(network) {
100
- this.config.network = network;
101
- return this;
689
+ /**
690
+ * Helper method to check if there's an established connection with an account
691
+ * @param accountId - The account ID to check
692
+ * @returns True if there's an established connection, false otherwise
693
+ */
694
+ hasEstablishedConnectionWithAccount(accountId) {
695
+ return Array.from(this.connections.values()).some(
696
+ (conn) => conn.targetAccountId === accountId && conn.status === "established"
697
+ );
102
698
  }
103
- setInboundTopicType(inboundTopicType) {
104
- this.config.inboundTopicType = inboundTopicType;
105
- return this;
699
+ /**
700
+ * Gets all active (established) connections
701
+ * @returns Array of active connections
702
+ */
703
+ getActiveConnections() {
704
+ return Array.from(this.connections.values()).filter(
705
+ (conn) => conn.status === "established"
706
+ );
106
707
  }
107
- setFeeConfig(feeConfigBuilder) {
108
- this.config.feeConfig = feeConfigBuilder;
109
- return this;
708
+ /**
709
+ * Gets all connections needing confirmation
710
+ * @returns Array of connections needing confirmation
711
+ */
712
+ getConnectionsNeedingConfirmation() {
713
+ return Array.from(this.connections.values()).filter(
714
+ (conn) => conn.needsConfirmation && (!this.filterPendingAccountIds.has(conn.targetAccountId) || this.hasEstablishedConnectionWithAccount(conn.targetAccountId))
715
+ );
110
716
  }
111
- setConnectionFeeConfig(feeConfigBuilder) {
112
- this.config.connectionFeeConfig = feeConfigBuilder;
113
- return this;
717
+ /**
718
+ * Gets a connection by its topic ID
719
+ * @param connectionTopicId - The topic ID to look up
720
+ * @returns The connection with the given topic ID, or undefined if not found
721
+ */
722
+ getConnectionByTopicId(connectionTopicId) {
723
+ return this.connections.get(connectionTopicId);
114
724
  }
115
- setExistingAccount(accountId, privateKey) {
116
- this.config.existingAccount = { accountId, privateKey };
117
- return this;
725
+ /**
726
+ * Gets a connection by account ID
727
+ * @param accountId - The account ID to look up
728
+ * @returns The connection with the given account ID, or undefined if not found
729
+ */
730
+ getConnectionByAccountId(accountId) {
731
+ return Array.from(this.connections.values()).find(
732
+ (conn) => conn.targetAccountId === accountId && conn.status === "established"
733
+ );
118
734
  }
119
- build() {
120
- if (!this.config.name) {
121
- throw new Error("Agent display name is required");
122
- }
123
- if (!this.config.bio) {
124
- this.logger?.warn("Agent description is not set");
125
- }
126
- if (!this.config.pfpBuffer && !this.config.existingPfpTopicId) {
127
- this.logger.warn("No profile picture provided or referenced.");
128
- }
129
- if (!this.config.network) {
130
- throw new Error("Network is required");
131
- }
132
- if (!this.config.inboundTopicType) {
133
- this.config.inboundTopicType = InboundTopicType.PUBLIC;
134
- }
135
- if (!this.config.capabilities) {
136
- this.config.capabilities = [];
137
- }
138
- if (!this.config.metadata) {
139
- this.config.metadata = { type: "manual" };
140
- } else if (!this.config.metadata.type) {
141
- this.config.metadata.type = "manual";
735
+ /**
736
+ * Gets all connections for a specific account ID
737
+ * @param accountId - The account ID to look up
738
+ * @returns Array of connections for the given account ID
739
+ */
740
+ getConnectionsByAccountId(accountId) {
741
+ return Array.from(this.connections.values()).filter(
742
+ (conn) => conn.targetAccountId === accountId
743
+ );
744
+ }
745
+ /**
746
+ * Updates or adds a connection
747
+ * @param connection - The connection to update or add
748
+ */
749
+ updateOrAddConnection(connection) {
750
+ this.connections.set(connection.connectionTopicId, connection);
751
+ }
752
+ /**
753
+ * Clears all tracked connections and requests
754
+ */
755
+ clearAll() {
756
+ this.connections.clear();
757
+ this.pendingRequests.clear();
758
+ }
759
+ /**
760
+ * Checks if a given connection request has been processed already
761
+ * This uses a combination of topic ID and request ID to uniquely identify requests
762
+ *
763
+ * @param inboundTopicId - The inbound topic ID where the request was received
764
+ * @param requestId - The sequence number (request ID)
765
+ * @returns True if this specific request has been processed, false otherwise
766
+ */
767
+ isConnectionRequestProcessed(inboundTopicId, requestId) {
768
+ for (const conn of this.connections.values()) {
769
+ if (conn.originTopicId === inboundTopicId && conn.inboundRequestId === requestId && conn.processed) {
770
+ return true;
771
+ }
772
+ if (conn.originTopicId === inboundTopicId && conn.connectionRequestId === requestId && conn.processed) {
773
+ return true;
774
+ }
142
775
  }
143
- if (this.config.inboundTopicType === InboundTopicType.FEE_BASED && !this.config.feeConfig) {
144
- throw new Error("Fee configuration is required for fee-based topics");
776
+ return false;
777
+ }
778
+ /**
779
+ * Marks a specific connection request as processed
780
+ *
781
+ * @param inboundTopicId - The inbound topic ID where the request was received
782
+ * @param requestId - The sequence number (request ID)
783
+ * @returns True if a matching connection was found and marked, false otherwise
784
+ */
785
+ markConnectionRequestProcessed(inboundTopicId, requestId) {
786
+ let found = false;
787
+ for (const [key, conn] of this.connections.entries()) {
788
+ if (conn.originTopicId === inboundTopicId && conn.inboundRequestId === requestId) {
789
+ this.connections.set(key, {
790
+ ...conn,
791
+ processed: true
792
+ });
793
+ found = true;
794
+ this.logger.debug(
795
+ `Marked inbound connection request #${requestId} on topic ${inboundTopicId} as processed`
796
+ );
797
+ }
798
+ if (conn.originTopicId === inboundTopicId && conn.connectionRequestId === requestId) {
799
+ this.connections.set(key, {
800
+ ...conn,
801
+ processed: true
802
+ });
803
+ found = true;
804
+ this.logger.debug(
805
+ `Marked outbound connection request #${requestId} on topic ${inboundTopicId} as processed`
806
+ );
807
+ }
145
808
  }
146
- return this.config;
809
+ return found;
147
810
  }
148
811
  }
149
812
  export {
150
- AgentBuilder
813
+ ConnectionsManager
151
814
  };
152
815
  //# sourceMappingURL=standards-sdk.es10.js.map