@arc402/sdk 0.3.0 → 0.3.1
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/package.json +1 -1
- package/src/endpoint.ts +142 -0
- package/src/index.ts +2 -0
package/package.json
CHANGED
package/src/endpoint.ts
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HTTP endpoint helpers — resolve an agent's endpoint from AgentRegistry
|
|
3
|
+
* and notify it after onchain events (hire, handshake).
|
|
4
|
+
*/
|
|
5
|
+
import { ethers } from "ethers";
|
|
6
|
+
import { AGENT_REGISTRY_ABI } from "./contracts";
|
|
7
|
+
|
|
8
|
+
export const DEFAULT_REGISTRY_ADDRESS = "0xD5c2851B00090c92Ba7F4723FB548bb30C9B6865";
|
|
9
|
+
|
|
10
|
+
export interface EndpointNotifyResult {
|
|
11
|
+
ok: boolean;
|
|
12
|
+
status?: number;
|
|
13
|
+
error?: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Reads an agent's public HTTP endpoint from AgentRegistry.
|
|
18
|
+
* Returns an empty string if the agent is not registered or has no endpoint.
|
|
19
|
+
*/
|
|
20
|
+
export async function resolveEndpoint(
|
|
21
|
+
agentAddress: string,
|
|
22
|
+
provider: ethers.Provider,
|
|
23
|
+
registryAddress = DEFAULT_REGISTRY_ADDRESS
|
|
24
|
+
): Promise<string> {
|
|
25
|
+
const registry = new ethers.Contract(registryAddress, AGENT_REGISTRY_ABI, provider);
|
|
26
|
+
const agentData = await registry.getAgent(agentAddress);
|
|
27
|
+
return (agentData.endpoint as string) ?? "";
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* POSTs a JSON payload to `${endpoint}${path}`.
|
|
32
|
+
* Returns { ok, status } on success, { ok: false, error } on failure.
|
|
33
|
+
* Never throws.
|
|
34
|
+
*/
|
|
35
|
+
export async function notifyEndpoint(
|
|
36
|
+
endpoint: string,
|
|
37
|
+
path: string,
|
|
38
|
+
payload: Record<string, unknown>
|
|
39
|
+
): Promise<EndpointNotifyResult> {
|
|
40
|
+
if (!endpoint) return { ok: false, error: "no endpoint" };
|
|
41
|
+
try {
|
|
42
|
+
const res = await fetch(`${endpoint}${path}`, {
|
|
43
|
+
method: "POST",
|
|
44
|
+
headers: { "Content-Type": "application/json" },
|
|
45
|
+
body: JSON.stringify(payload),
|
|
46
|
+
});
|
|
47
|
+
return { ok: res.ok, status: res.status };
|
|
48
|
+
} catch (err) {
|
|
49
|
+
return { ok: false, error: err instanceof Error ? err.message : String(err) };
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Convenience: resolve an agent's endpoint then POST to /hire.
|
|
55
|
+
*/
|
|
56
|
+
export async function notifyHire(
|
|
57
|
+
agentAddress: string,
|
|
58
|
+
proposal: Record<string, unknown>,
|
|
59
|
+
provider: ethers.Provider,
|
|
60
|
+
registryAddress = DEFAULT_REGISTRY_ADDRESS
|
|
61
|
+
): Promise<EndpointNotifyResult> {
|
|
62
|
+
const endpoint = await resolveEndpoint(agentAddress, provider, registryAddress);
|
|
63
|
+
return notifyEndpoint(endpoint, "/hire", proposal);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Convenience: resolve an agent's endpoint then POST to /handshake.
|
|
68
|
+
*/
|
|
69
|
+
export async function notifyHandshake(
|
|
70
|
+
agentAddress: string,
|
|
71
|
+
payload: Record<string, unknown>,
|
|
72
|
+
provider: ethers.Provider,
|
|
73
|
+
registryAddress = DEFAULT_REGISTRY_ADDRESS
|
|
74
|
+
): Promise<EndpointNotifyResult> {
|
|
75
|
+
const endpoint = await resolveEndpoint(agentAddress, provider, registryAddress);
|
|
76
|
+
return notifyEndpoint(endpoint, "/handshake", payload);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Convenience: resolve an agent's endpoint then POST to /hire/accepted.
|
|
81
|
+
*/
|
|
82
|
+
export async function notifyHireAccepted(
|
|
83
|
+
agentAddress: string,
|
|
84
|
+
payload: Record<string, unknown>,
|
|
85
|
+
provider: ethers.Provider,
|
|
86
|
+
registryAddress = DEFAULT_REGISTRY_ADDRESS
|
|
87
|
+
): Promise<EndpointNotifyResult> {
|
|
88
|
+
const endpoint = await resolveEndpoint(agentAddress, provider, registryAddress);
|
|
89
|
+
return notifyEndpoint(endpoint, "/hire/accepted", payload);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Convenience: resolve an agent's endpoint then POST to /delivery.
|
|
94
|
+
*/
|
|
95
|
+
export async function notifyDelivery(
|
|
96
|
+
agentAddress: string,
|
|
97
|
+
payload: Record<string, unknown>,
|
|
98
|
+
provider: ethers.Provider,
|
|
99
|
+
registryAddress = DEFAULT_REGISTRY_ADDRESS
|
|
100
|
+
): Promise<EndpointNotifyResult> {
|
|
101
|
+
const endpoint = await resolveEndpoint(agentAddress, provider, registryAddress);
|
|
102
|
+
return notifyEndpoint(endpoint, "/delivery", payload);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Convenience: resolve an agent's endpoint then POST to /delivery/accepted.
|
|
107
|
+
*/
|
|
108
|
+
export async function notifyDeliveryAccepted(
|
|
109
|
+
agentAddress: string,
|
|
110
|
+
payload: Record<string, unknown>,
|
|
111
|
+
provider: ethers.Provider,
|
|
112
|
+
registryAddress = DEFAULT_REGISTRY_ADDRESS
|
|
113
|
+
): Promise<EndpointNotifyResult> {
|
|
114
|
+
const endpoint = await resolveEndpoint(agentAddress, provider, registryAddress);
|
|
115
|
+
return notifyEndpoint(endpoint, "/delivery/accepted", payload);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Convenience: resolve an agent's endpoint then POST to /dispute.
|
|
120
|
+
*/
|
|
121
|
+
export async function notifyDispute(
|
|
122
|
+
agentAddress: string,
|
|
123
|
+
payload: Record<string, unknown>,
|
|
124
|
+
provider: ethers.Provider,
|
|
125
|
+
registryAddress = DEFAULT_REGISTRY_ADDRESS
|
|
126
|
+
): Promise<EndpointNotifyResult> {
|
|
127
|
+
const endpoint = await resolveEndpoint(agentAddress, provider, registryAddress);
|
|
128
|
+
return notifyEndpoint(endpoint, "/dispute", payload);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Convenience: resolve an agent's endpoint then POST to /message.
|
|
133
|
+
*/
|
|
134
|
+
export async function notifyMessage(
|
|
135
|
+
agentAddress: string,
|
|
136
|
+
payload: Record<string, unknown>,
|
|
137
|
+
provider: ethers.Provider,
|
|
138
|
+
registryAddress = DEFAULT_REGISTRY_ADDRESS
|
|
139
|
+
): Promise<EndpointNotifyResult> {
|
|
140
|
+
const endpoint = await resolveEndpoint(agentAddress, provider, registryAddress);
|
|
141
|
+
return notifyEndpoint(endpoint, "/message", payload);
|
|
142
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -29,3 +29,5 @@ export { buildMetadata, validateMetadata, encodeMetadata, decodeMetadata, upload
|
|
|
29
29
|
export type { AgentMetadata, AgentMetadataModel, AgentMetadataTraining, AgentMetadataPricing, AgentMetadataSla, AgentMetadataContact, AgentMetadataSecurity } from "./metadata";
|
|
30
30
|
export { ColdStartClient } from "./coldstart";
|
|
31
31
|
export { MigrationClient } from "./migration";
|
|
32
|
+
export { resolveEndpoint, notifyEndpoint, notifyHire, notifyHandshake, notifyHireAccepted, notifyDelivery, notifyDeliveryAccepted, notifyDispute, notifyMessage, DEFAULT_REGISTRY_ADDRESS } from "./endpoint";
|
|
33
|
+
export type { EndpointNotifyResult } from "./endpoint";
|