repositories
loading repo index
repositories
loading repo index
repository
loading code, commits, and activity
public Clawd ADK gateway launch mirror
stars
latest
clone command
git clone gitlawb://did:key:z6Mkq5mY...iFZ5/my-project-publ...git clone gitlawb://did:key:z6Mkq5mY.../my-project-publ...2fa351d6docs: add automaton and perps launch sources16d ago| #1 | /** |
| #2 | * Social Client Factory |
| #3 | * |
| #4 | * Creates a SocialClient for the automaton runtime. |
| #5 | * Self-contained: uses viem for signing and fetch for HTTP. |
| #6 | */ |
| #7 | import { keccak256, toBytes, } from "viem"; |
| #8 | /** |
| #9 | * Create a SocialClient wired to the agent's wallet. |
| #10 | */ |
| #11 | export function createSocialClient(relayUrl, account) { |
| #12 | const baseUrl = relayUrl.replace(/\/$/, ""); |
| #13 | return { |
| #14 | send: async (to, content, replyTo) => { |
| #15 | const signedAt = new Date().toISOString(); |
| #16 | const contentHash = keccak256(toBytes(content)); |
| #17 | const canonical = `CLAWD Runtime:send:${to.toLowerCase()}:${contentHash}:${signedAt}`; |
| #18 | const signature = await account.signMessage({ message: canonical }); |
| #19 | const res = await fetch(`${baseUrl}/v1/messages`, { |
| #20 | method: "POST", |
| #21 | headers: { "Content-Type": "application/json" }, |
| #22 | body: JSON.stringify({ |
| #23 | from: account.address.toLowerCase(), |
| #24 | to: to.toLowerCase(), |
| #25 | content, |
| #26 | signature, |
| #27 | signed_at: signedAt, |
| #28 | reply_to: replyTo, |
| #29 | }), |
| #30 | }); |
| #31 | if (!res.ok) { |
| #32 | const err = await res.json().catch(() => ({ error: res.statusText })); |
| #33 | throw new Error(`Send failed (${res.status}): ${err.error || res.statusText}`); |
| #34 | } |
| #35 | const data = (await res.json()); |
| #36 | return { id: data.id }; |
| #37 | }, |
| #38 | poll: async (cursor, limit) => { |
| #39 | const timestamp = new Date().toISOString(); |
| #40 | const canonical = `CLAWD Runtime:poll:${account.address.toLowerCase()}:${timestamp}`; |
| #41 | const signature = await account.signMessage({ message: canonical }); |
| #42 | const res = await fetch(`${baseUrl}/v1/messages/poll`, { |
| #43 | method: "POST", |
| #44 | headers: { |
| #45 | "Content-Type": "application/json", |
| #46 | "X-Wallet-Address": account.address.toLowerCase(), |
| #47 | "X-Signature": signature, |
| #48 | "X-Timestamp": timestamp, |
| #49 | }, |
| #50 | body: JSON.stringify({ cursor, limit }), |
| #51 | }); |
| #52 | if (!res.ok) { |
| #53 | const err = await res.json().catch(() => ({ error: res.statusText })); |
| #54 | throw new Error(`Poll failed (${res.status}): ${err.error || res.statusText}`); |
| #55 | } |
| #56 | const data = (await res.json()); |
| #57 | return { |
| #58 | messages: data.messages.map((m) => ({ |
| #59 | id: m.id, |
| #60 | from: m.from, |
| #61 | to: m.to, |
| #62 | content: m.content, |
| #63 | signedAt: m.signedAt, |
| #64 | createdAt: m.createdAt, |
| #65 | replyTo: m.replyTo, |
| #66 | })), |
| #67 | nextCursor: data.next_cursor, |
| #68 | }; |
| #69 | }, |
| #70 | unreadCount: async () => { |
| #71 | const timestamp = new Date().toISOString(); |
| #72 | const canonical = `CLAWD Runtime:poll:${account.address.toLowerCase()}:${timestamp}`; |
| #73 | const signature = await account.signMessage({ message: canonical }); |
| #74 | const res = await fetch(`${baseUrl}/v1/messages/count`, { |
| #75 | method: "GET", |
| #76 | headers: { |
| #77 | "X-Wallet-Address": account.address.toLowerCase(), |
| #78 | "X-Signature": signature, |
| #79 | "X-Timestamp": timestamp, |
| #80 | }, |
| #81 | }); |
| #82 | if (!res.ok) |
| #83 | return 0; |
| #84 | const data = (await res.json()); |
| #85 | return data.unread; |
| #86 | }, |
| #87 | }; |
| #88 | } |
| #89 | //# sourceMappingURL=client.js.map |