|
| 1 | +# Copyright AGNTCY Contributors (https://github.com/agntcy) |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +from google.protobuf.json_format import MessageToJson |
| 5 | + |
| 6 | +from agntcy.dir_sdk.client import Client |
| 7 | +from agntcy.dir_sdk.models import core_v1, search_v1, routing_v1 |
| 8 | + |
| 9 | + |
| 10 | +def generate_record(name): |
| 11 | + return core_v1.Record( |
| 12 | + data={ |
| 13 | + "name": name, |
| 14 | + "version": "v1.0.0", |
| 15 | + "schema_version": "0.8.0", |
| 16 | + "description": "My example agent", |
| 17 | + "authors": ["AGNTCY"], |
| 18 | + "created_at": "2025-03-19T17:06:37Z", |
| 19 | + "skills": [ |
| 20 | + { |
| 21 | + "name": "natural_language_processing/natural_language_generation/text_completion", |
| 22 | + "id": 10201 |
| 23 | + }, |
| 24 | + { |
| 25 | + "name": "natural_language_processing/analytical_reasoning/problem_solving", |
| 26 | + "id": 10702 |
| 27 | + } |
| 28 | + ], |
| 29 | + "locators": [ |
| 30 | + { |
| 31 | + "type": "docker_image", |
| 32 | + "url": "https://ghcr.io/agntcy/marketing-strategy" |
| 33 | + } |
| 34 | + ], |
| 35 | + "domains": [ |
| 36 | + { |
| 37 | + "name": "technology/networking", |
| 38 | + "id": 103 |
| 39 | + } |
| 40 | + ], |
| 41 | + "modules": [ |
| 42 | + { |
| 43 | + "name": "integration/a2a", |
| 44 | + "id": 203, |
| 45 | + "data": { |
| 46 | + "protocol_version": "lightweight orchestra moral", |
| 47 | + "card_data": "centres", |
| 48 | + "capabilities": [ |
| 49 | + "state_transition_history", |
| 50 | + "push_notifications" |
| 51 | + ], |
| 52 | + "transports": [ |
| 53 | + "grpc", |
| 54 | + "http" |
| 55 | + ], |
| 56 | + "output_modes": [ |
| 57 | + "text/html" |
| 58 | + ] |
| 59 | + } |
| 60 | + } |
| 61 | + ] |
| 62 | + }, |
| 63 | + ) |
| 64 | + |
| 65 | + |
| 66 | +def main() -> None: |
| 67 | + client = Client() |
| 68 | + |
| 69 | + records = [generate_record(x) for x in ["example-record", "example-record2"]] |
| 70 | + |
| 71 | + # Push objects to the store |
| 72 | + refs = client.push(records) |
| 73 | + |
| 74 | + for ref in refs: |
| 75 | + print("Pushed object ref:", ref.cid) |
| 76 | + |
| 77 | + # Pull objects from the store |
| 78 | + pulled_records = client.pull(refs) |
| 79 | + |
| 80 | + for pulled_record in pulled_records: |
| 81 | + print("Pulled object data:", MessageToJson(pulled_record)) |
| 82 | + |
| 83 | + # Lookup the object |
| 84 | + metadatas = client.lookup(refs) |
| 85 | + |
| 86 | + for metadata in metadatas: |
| 87 | + print("Lookup object metadata:", MessageToJson(metadata)) |
| 88 | + |
| 89 | + # Publish the object |
| 90 | + record_refs = routing_v1.RecordRefs(refs=[refs[0]]) |
| 91 | + publish_request = routing_v1.PublishRequest(record_refs=record_refs) |
| 92 | + client.publish(publish_request) |
| 93 | + print("Object published.") |
| 94 | + |
| 95 | + # List objects in the store |
| 96 | + query = routing_v1.RecordQuery( |
| 97 | + type=routing_v1.RECORD_QUERY_TYPE_SKILL, |
| 98 | + value="/skills/Natural Language Processing/Text Completion", |
| 99 | + ) |
| 100 | + |
| 101 | + list_request = routing_v1.ListRequest(queries=[query]) |
| 102 | + objects = list(client.list(list_request)) |
| 103 | + |
| 104 | + for o in objects: |
| 105 | + print("Listed object:", MessageToJson(o)) |
| 106 | + |
| 107 | + # Search objects |
| 108 | + search_query = search_v1.RecordQuery( |
| 109 | + type=search_v1.RECORD_QUERY_TYPE_VERSION, value="v1.*", |
| 110 | + ) |
| 111 | + |
| 112 | + search_request = search_v1.SearchCIDsRequest(queries=[search_query], limit=3) |
| 113 | + objects = list(client.search_cids(search_request)) |
| 114 | + |
| 115 | + print("Searched objects:",objects) |
| 116 | + |
| 117 | + # Unpublish the object |
| 118 | + record_refs = routing_v1.RecordRefs(refs=[refs[0]]) |
| 119 | + unpublish_request = routing_v1.UnpublishRequest(record_refs=record_refs) |
| 120 | + client.unpublish(unpublish_request) |
| 121 | + print("Object unpublished.") |
| 122 | + |
| 123 | + # Delete the object |
| 124 | + client.delete(refs) |
| 125 | + print("Objects are deleted.") |
| 126 | + |
| 127 | + |
| 128 | +if __name__ == "__main__": |
| 129 | + main() |
0 commit comments