@hashgraphonline/standards-sdk 0.0.44 → 0.0.46

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
@@ -58,12 +58,19 @@ The SDK includes demo implementations that showcase various features. Follow the
58
58
 
59
59
  ```bash
60
60
  # Run the inscribe demo
61
- npm run demo
61
+ npm run demo:inscribe
62
+ ```
62
63
 
64
+ ```bash
63
65
  # Run the HCS-10 AI agent communication demo
64
66
  npm run demo:hcs-10
65
67
  ```
66
68
 
69
+ ```bash
70
+ # Run the HCS-10 polling demo
71
+ npm run demo:polling-agent
72
+ ```
73
+
67
74
  ### Demo Descriptions
68
75
 
69
76
  #### Inscribe Demo
@@ -9,7 +9,7 @@ export interface HCS10Config {
9
9
  }
10
10
  export interface HCSMessage {
11
11
  p: 'hcs-10';
12
- op: 'connection_request' | 'connection_created' | 'message';
12
+ op: 'connection_request' | 'connection_created' | 'message' | 'close_connection';
13
13
  data: string;
14
14
  created?: Date;
15
15
  consensus_timestamp?: string;
@@ -24,6 +24,8 @@ export interface HCSMessage {
24
24
  connection_id?: number;
25
25
  sequence_number: number;
26
26
  operator_id?: string;
27
+ reason?: string;
28
+ close_method?: string;
27
29
  }
28
30
  export interface ProfileResponse {
29
31
  profile: any;
@@ -40,6 +42,25 @@ export declare abstract class HCS10BaseClient extends Registration {
40
42
  accountId: string;
41
43
  signer: any;
42
44
  };
45
+ /**
46
+ * Get a stream of messages from a connection topic
47
+ * @param topicId The connection topic ID to get messages from
48
+ * @returns A stream of filtered messages valid for connection topics
49
+ */
50
+ getMessageStream(topicId: string): Promise<{
51
+ messages: HCSMessage[];
52
+ }>;
53
+ /**
54
+ * Validates if an operator_id follows the correct format (agentTopicId@accountId)
55
+ * @param operatorId The operator ID to validate
56
+ * @returns True if the format is valid, false otherwise
57
+ */
58
+ protected isValidOperatorId(operatorId: string): boolean;
59
+ /**
60
+ * Get all messages from a topic
61
+ * @param topicId The topic ID to get messages from
62
+ * @returns All messages from the topic
63
+ */
43
64
  getMessages(topicId: string): Promise<{
44
65
  messages: HCSMessage[];
45
66
  }>;
@@ -23824,11 +23824,106 @@ class HCS10BaseClient extends Registration {
23824
23824
  this.logger
23825
23825
  );
23826
23826
  }
23827
+ /**
23828
+ * Get a stream of messages from a connection topic
23829
+ * @param topicId The connection topic ID to get messages from
23830
+ * @returns A stream of filtered messages valid for connection topics
23831
+ */
23832
+ async getMessageStream(topicId) {
23833
+ try {
23834
+ const messages = await this.mirrorNode.getTopicMessages(topicId);
23835
+ const validOps = ["message", "close_connection"];
23836
+ const filteredMessages = messages.filter((msg) => {
23837
+ if (msg.p !== "hcs-10" || !validOps.includes(msg.op)) {
23838
+ return false;
23839
+ }
23840
+ if (msg.op === "message") {
23841
+ if (!msg.data) {
23842
+ return false;
23843
+ }
23844
+ if (!msg.operator_id) {
23845
+ return false;
23846
+ }
23847
+ if (!this.isValidOperatorId(msg.operator_id)) {
23848
+ return false;
23849
+ }
23850
+ }
23851
+ if (msg.op === "close_connection") {
23852
+ if (!msg.operator_id) {
23853
+ return false;
23854
+ }
23855
+ if (!this.isValidOperatorId(msg.operator_id)) {
23856
+ return false;
23857
+ }
23858
+ }
23859
+ return true;
23860
+ });
23861
+ return {
23862
+ messages: filteredMessages
23863
+ };
23864
+ } catch (error) {
23865
+ if (this.logger) {
23866
+ this.logger.error(`Error fetching messages: ${error.message}`);
23867
+ }
23868
+ return { messages: [] };
23869
+ }
23870
+ }
23871
+ /**
23872
+ * Validates if an operator_id follows the correct format (agentTopicId@accountId)
23873
+ * @param operatorId The operator ID to validate
23874
+ * @returns True if the format is valid, false otherwise
23875
+ */
23876
+ isValidOperatorId(operatorId) {
23877
+ if (!operatorId) {
23878
+ return false;
23879
+ }
23880
+ const parts = operatorId.split("@");
23881
+ if (parts.length !== 2) {
23882
+ return false;
23883
+ }
23884
+ const agentTopicId = parts[0];
23885
+ const accountId = parts[1];
23886
+ if (!agentTopicId) {
23887
+ return false;
23888
+ }
23889
+ if (!accountId) {
23890
+ return false;
23891
+ }
23892
+ const hederaIdPattern = /^[0-9]+\.[0-9]+\.[0-9]+$/;
23893
+ if (!hederaIdPattern.test(accountId)) {
23894
+ return false;
23895
+ }
23896
+ if (!hederaIdPattern.test(agentTopicId)) {
23897
+ return false;
23898
+ }
23899
+ return true;
23900
+ }
23901
+ /**
23902
+ * Get all messages from a topic
23903
+ * @param topicId The topic ID to get messages from
23904
+ * @returns All messages from the topic
23905
+ */
23827
23906
  async getMessages(topicId) {
23828
23907
  try {
23829
23908
  const messages = await this.mirrorNode.getTopicMessages(topicId);
23909
+ const validatedMessages = messages.filter((msg) => {
23910
+ if (msg.p !== "hcs-10") {
23911
+ return false;
23912
+ }
23913
+ if (msg.op === "message") {
23914
+ if (!msg.data) {
23915
+ return false;
23916
+ }
23917
+ if (msg.operator_id) {
23918
+ if (!this.isValidOperatorId(msg.operator_id)) {
23919
+ return false;
23920
+ }
23921
+ }
23922
+ }
23923
+ return true;
23924
+ });
23830
23925
  return {
23831
- messages
23926
+ messages: validatedMessages
23832
23927
  };
23833
23928
  } catch (error) {
23834
23929
  if (this.logger) {