@oneie/sdk 0.7.0 → 0.9.0

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 (71) hide show
  1. package/README.md +333 -190
  2. package/dist/auth.d.ts +102 -0
  3. package/dist/auth.d.ts.map +1 -0
  4. package/dist/auth.js +154 -0
  5. package/dist/auth.js.map +1 -0
  6. package/dist/client.d.ts +221 -24
  7. package/dist/client.d.ts.map +1 -1
  8. package/dist/client.js +331 -154
  9. package/dist/client.js.map +1 -1
  10. package/dist/compile.d.ts +123 -0
  11. package/dist/compile.d.ts.map +1 -0
  12. package/dist/compile.js +652 -0
  13. package/dist/compile.js.map +1 -0
  14. package/dist/fetch.d.ts +40 -0
  15. package/dist/fetch.d.ts.map +1 -0
  16. package/dist/fetch.js +158 -0
  17. package/dist/fetch.js.map +1 -0
  18. package/dist/generated/types.d.ts +104 -0
  19. package/dist/generated/types.d.ts.map +1 -0
  20. package/dist/generated/types.js +5 -0
  21. package/dist/generated/types.js.map +1 -0
  22. package/dist/index.d.ts +8 -1
  23. package/dist/index.d.ts.map +1 -1
  24. package/dist/index.js +6 -1
  25. package/dist/index.js.map +1 -1
  26. package/dist/pay.d.ts +2 -2
  27. package/dist/pay.d.ts.map +1 -1
  28. package/dist/pay.js +5 -5
  29. package/dist/pay.js.map +1 -1
  30. package/dist/schemas.d.ts +39 -0
  31. package/dist/schemas.d.ts.map +1 -1
  32. package/dist/schemas.js +26 -0
  33. package/dist/schemas.js.map +1 -1
  34. package/dist/skills.d.ts +40 -0
  35. package/dist/skills.d.ts.map +1 -0
  36. package/dist/skills.js +16 -0
  37. package/dist/skills.js.map +1 -0
  38. package/dist/storage.d.ts +7 -8
  39. package/dist/storage.d.ts.map +1 -1
  40. package/dist/storage.js +61 -33
  41. package/dist/storage.js.map +1 -1
  42. package/dist/telemetry.d.ts +9 -0
  43. package/dist/telemetry.d.ts.map +1 -1
  44. package/dist/telemetry.js +11 -0
  45. package/dist/telemetry.js.map +1 -1
  46. package/dist/testing/index.d.ts.map +1 -1
  47. package/dist/testing/index.js +4 -3
  48. package/dist/testing/index.js.map +1 -1
  49. package/dist/types.d.ts +148 -20
  50. package/dist/types.d.ts.map +1 -1
  51. package/package.json +26 -17
  52. package/dist/react/context.d.ts +0 -11
  53. package/dist/react/context.d.ts.map +0 -1
  54. package/dist/react/context.js +0 -13
  55. package/dist/react/context.js.map +0 -1
  56. package/dist/react/hooks.d.ts +0 -104
  57. package/dist/react/hooks.d.ts.map +0 -1
  58. package/dist/react/hooks.js +0 -247
  59. package/dist/react/hooks.js.map +0 -1
  60. package/dist/react/index.d.ts +0 -6
  61. package/dist/react/index.d.ts.map +0 -1
  62. package/dist/react/index.js +0 -5
  63. package/dist/react/index.js.map +0 -1
  64. package/dist/react/optimistic.d.ts +0 -45
  65. package/dist/react/optimistic.d.ts.map +0 -1
  66. package/dist/react/optimistic.js +0 -52
  67. package/dist/react/optimistic.js.map +0 -1
  68. package/dist/react/stream.d.ts +0 -18
  69. package/dist/react/stream.d.ts.map +0 -1
  70. package/dist/react/stream.js +0 -60
  71. package/dist/react/stream.js.map +0 -1
package/dist/client.js CHANGED
@@ -1,4 +1,6 @@
1
1
  import { accept as payAccept, request as payRequest, status as payStatus } from "./pay.js";
2
+ import { skillsImport } from "./skills.js";
3
+ import { getToken, clearToken } from "./storage.js";
2
4
  import { AuthError, RateLimitError, SubstrateError, TimeoutError, ValidationError } from "./errors.js";
3
5
  import { resolveApiKey, resolveBaseUrl } from "./urls.js";
4
6
  import { emit } from "./telemetry.js";
@@ -41,9 +43,14 @@ async function withRetry(fn, config) {
41
43
  }
42
44
  async function req(baseUrl, path, init = {}, apiKey) {
43
45
  const headers = { "Content-Type": "application/json" };
44
- if (apiKey)
45
- headers["Authorization"] = `Bearer ${apiKey}`;
46
+ // Prefer explicit API key; fall back to persisted session token.
47
+ const bearer = apiKey ?? getToken();
48
+ if (bearer)
49
+ headers["Authorization"] = `Bearer ${bearer}`;
46
50
  const res = await fetch(`${baseUrl}${path}`, { ...init, headers });
51
+ // On 401, the persisted token is no longer valid — clear it so the caller gets a clean state.
52
+ if (res.status === 401)
53
+ clearToken();
47
54
  if (!res.ok)
48
55
  throwForStatus(res.status, init.method ?? "GET", path);
49
56
  return res.json();
@@ -55,6 +62,8 @@ export class SubstrateClient {
55
62
  validateMode;
56
63
  /** Pay module — accept, request, status. Bound to this client's baseUrl + apiKey. */
57
64
  pay;
65
+ /** Skills module — import a skill by ref or content. */
66
+ skills;
58
67
  constructor(cfg = {}) {
59
68
  this.baseUrl = resolveBaseUrl(cfg.baseUrl);
60
69
  this.apiKey = resolveApiKey(cfg.apiKey);
@@ -66,6 +75,10 @@ export class SubstrateClient {
66
75
  request: (opts) => payRequest(opts, cfg),
67
76
  status: (ref) => payStatus(ref, cfg),
68
77
  };
78
+ // Bind skills module methods to this client's resolved baseUrl + apiKey
79
+ this.skills = {
80
+ import: (opts) => skillsImport({ baseUrl: this.baseUrl, apiKey: this.apiKey ?? "" }, opts),
81
+ };
69
82
  }
70
83
  static fromApiKey(key, baseUrl) {
71
84
  return new SubstrateClient({ apiKey: key, baseUrl });
@@ -73,14 +86,17 @@ export class SubstrateClient {
73
86
  r(path, init = {}) {
74
87
  return withRetry(() => req(this.baseUrl, path, init, this.apiKey), this.retryConfig);
75
88
  }
76
- async signal(sender, receiver, data) {
77
- const result = await this.r("/api/signal", { method: "POST", body: JSON.stringify({ sender, receiver, data }) });
89
+ async signal(receiver, data) {
90
+ const result = await this.r(`/api/signal/${encodeURIComponent(receiver)}`, { method: "POST", body: JSON.stringify({ data }) });
78
91
  emit("toolkit:sdk:signal", ["sdk", "method-signal", result.success ? "200" : "error"]);
79
92
  return result;
80
93
  }
81
- async ask(receiver, data, timeout, from) {
94
+ async emitAgentEvent(slug, agentId, event, payload) {
95
+ return this.r("/api/agent-events", { method: "POST", body: JSON.stringify({ slug, agentId, event: `custom:${event}`, payload }) });
96
+ }
97
+ async ask(receiver, data, timeoutMs) {
82
98
  const t = Date.now();
83
- const raw = await this.r("/api/ask", { method: "POST", body: JSON.stringify({ receiver, data, timeout, from }) });
99
+ const raw = await this.r(`/api/ask/${encodeURIComponent(receiver)}`, { method: "POST", body: JSON.stringify({ data, timeoutMs }) });
84
100
  const kind = "result" in raw ? "result"
85
101
  : "timeout" in raw ? "timeout"
86
102
  : "dissolved" in raw ? "dissolved"
@@ -89,96 +105,306 @@ export class SubstrateClient {
89
105
  emit("toolkit:sdk:ask", ["sdk", "method-ask", `outcome-${kind}`], { latencyMs: Date.now() - t });
90
106
  return result;
91
107
  }
92
- async mark(edge, scores) {
93
- const result = await this.r("/api/loop/mark-dims", { method: "POST", body: JSON.stringify({ edge, ...(scores ?? { fit: 1, form: 1, truth: 1, taste: 1 }) }) });
108
+ async mark(edge, strength = 1) {
109
+ const result = await this.r(`/api/mark/${encodeURIComponent(edge)}`, { method: "POST", body: JSON.stringify({ strength }) });
94
110
  emit("toolkit:sdk:mark", ["sdk", "method-mark"]);
95
111
  return result;
96
112
  }
97
- async warn(edge, scores) {
98
- const result = await this.r("/api/loop/mark-dims", { method: "POST", body: JSON.stringify({ edge, ...(scores ?? { fit: 0, form: 0, truth: 0, taste: 0 }) }) });
113
+ async warn(edge, strength = 1) {
114
+ const result = await this.r(`/api/warn/${encodeURIComponent(edge)}`, { method: "POST", body: JSON.stringify({ strength }) });
99
115
  emit("toolkit:sdk:warn", ["sdk", "method-warn"]);
100
116
  return result;
101
117
  }
102
- async fade(trailRate, resistanceRate) {
103
- const result = await this.r("/api/decay-cycle", { method: "POST", body: JSON.stringify({ trailRate, resistanceRate }) });
118
+ async fade(rate) {
119
+ const result = await this.r("/api/fade", { method: "POST", body: JSON.stringify({ rate }) });
104
120
  emit("toolkit:sdk:fade", ["sdk", "method-fade"]);
105
121
  return result;
106
122
  }
107
- async highways(limit = 10) {
108
- const result = await this.r(`/api/loop/highways?limit=${limit}`);
123
+ async highways(limit = 50, from) {
124
+ const params = new URLSearchParams({ limit: String(limit) });
125
+ if (from)
126
+ params.set("from", from);
127
+ const result = await this.r(`/api/export/highways?${params}`);
109
128
  emit("toolkit:sdk:highways", ["sdk", "method-highways"]);
110
129
  return result;
111
130
  }
112
- async recall(status) {
113
- const qs = status ? `?status=${encodeURIComponent(status)}` : "";
114
- const result = await this.r(`/api/hypotheses${qs}`);
131
+ async recall(status, search, limit = 20) {
132
+ const params = new URLSearchParams({ limit: String(limit) });
133
+ if (status)
134
+ params.set("status", status);
135
+ if (search)
136
+ params.set("search", search);
137
+ const result = await this.r(`/api/learning?${params}`);
115
138
  emit("toolkit:sdk:recall", ["sdk", "method-recall"]);
116
139
  return result;
117
140
  }
118
- async reveal(uid) {
119
- const result = await this.r(`/api/memory/reveal/${encodeURIComponent(uid)}`);
141
+ async reveal(uid, field) {
142
+ const qs = field ? `?field=${encodeURIComponent(field)}` : "";
143
+ const result = await this.r(`/api/pii/reveal/${encodeURIComponent(uid)}${qs}`);
120
144
  emit("toolkit:sdk:reveal", ["sdk", "method-reveal"]);
121
145
  return result;
122
146
  }
123
- async forget(uid) {
124
- const result = await this.r(`/api/memory/forget/${encodeURIComponent(uid)}`, { method: "DELETE" });
147
+ async forget(uid, source) {
148
+ const result = await this.r("/api/forget", { method: "POST", body: JSON.stringify({ actorId: uid, source: source ?? "sdk" }) });
125
149
  emit("toolkit:sdk:forget", ["sdk", "method-forget"]);
126
150
  return result;
127
151
  }
128
- async frontier(uid) {
129
- const result = await this.r(`/api/memory/frontier/${encodeURIComponent(uid)}`);
152
+ async frontier(limit = 50) {
153
+ const result = await this.r(`/api/frontiers?limit=${limit}`);
130
154
  emit("toolkit:sdk:frontier", ["sdk", "method-frontier"]);
131
155
  return result;
132
156
  }
133
- async know() {
134
- const result = await this.r("/api/tick");
135
- emit("toolkit:sdk:know", ["sdk", "method-know", "stage:advocate"]);
157
+ async follow(tag) {
158
+ const result = await this.r(`/api/follow?tag=${encodeURIComponent(tag)}`);
159
+ emit("toolkit:sdk:follow", ["sdk", "method-follow"]);
136
160
  return result;
137
161
  }
162
+ /** @deprecated No server-side tick endpoint. Use fade() on a schedule instead. */
163
+ async know() {
164
+ return {};
165
+ }
138
166
  async walletFor(uid) {
139
- const result = await this.r(`/api/identity/${encodeURIComponent(uid)}/address`);
167
+ const result = await this.ask("identity:address", { uid });
140
168
  emit("toolkit:sdk:walletFor", ["sdk", "method-walletFor", "stage:wallet"]);
141
- return { uid: result.uid, address: result.address };
169
+ return "result" in result && result.result ? result.result : { uid, address: "" };
142
170
  }
143
171
  async signIn(opts) {
144
- const result = await this.r("/api/auth/sign-in/email", { method: "POST", body: JSON.stringify(opts) });
172
+ const result = await this.ask("auth:sign-in", opts);
145
173
  emit("toolkit:sdk:signIn", ["sdk", "method-signIn", "stage:sign-in:human"]);
146
- return result;
174
+ return "result" in result && result.result ? result.result : { sessionId: "", userId: "" };
147
175
  }
148
176
  async signOut() {
149
- await this.r("/api/auth/sign-out", { method: "POST", body: JSON.stringify({}) });
177
+ await this.signal("auth:sign-out");
150
178
  emit("toolkit:sdk:signOut", ["sdk", "method-signOut", "stage:sign-in:human"]);
151
179
  }
152
180
  async join(opts) {
153
- const result = await this.r("/api/board/join", { method: "POST", body: JSON.stringify(opts) });
181
+ const result = await this.ask("board:join", opts);
154
182
  emit("toolkit:sdk:join", ["sdk", "method-join", "stage:join-board"]);
183
+ return "result" in result && result.result ? result.result : { ok: false, uid: opts.uid, group: opts.group ?? "", role: "" };
184
+ }
185
+ async createGroup(opts) {
186
+ const result = await this.r("/api/groups", { method: "POST", body: JSON.stringify(opts) });
187
+ emit("toolkit:sdk:createGroup", ["sdk", "method-createGroup", "stage:groups"]);
188
+ return result;
189
+ }
190
+ async listGroups() {
191
+ const result = await this.r("/api/groups");
192
+ emit("toolkit:sdk:listGroups", ["sdk", "method-listGroups", "stage:groups"]);
193
+ return result;
194
+ }
195
+ async joinGroup(gid) {
196
+ await this.signal("groups:join", { gid });
197
+ emit("toolkit:sdk:joinGroup", ["sdk", "method-joinGroup", "stage:groups"]);
198
+ return { ok: true, gid, role: "member" };
199
+ }
200
+ async leaveGroup(gid) {
201
+ await this.signal("groups:leave", { gid });
202
+ emit("toolkit:sdk:leaveGroup", ["sdk", "method-leaveGroup", "stage:groups"]);
203
+ return { ok: true };
204
+ }
205
+ async groupMembers(gid) {
206
+ const result = await this.ask("groups:members", { gid });
207
+ emit("toolkit:sdk:groupMembers", ["sdk", "method-groupMembers", "stage:groups"]);
208
+ return "result" in result && result.result ? result.result : { gid, members: [] };
209
+ }
210
+ async inviteMember(gid, uid, role = "member") {
211
+ await this.signal("groups:invite", { gid, uid, role });
212
+ emit("toolkit:sdk:inviteMember", ["sdk", "method-inviteMember", "stage:groups"]);
213
+ return { ok: true };
214
+ }
215
+ async bridge(from, to) {
216
+ const result = await this.ask("paths:bridge", { from, to });
217
+ emit("toolkit:sdk:bridge", ["sdk", "method-bridge", "stage:groups"]);
218
+ return "result" in result && result.result ? result.result : {};
219
+ }
220
+ async inbox(uid, opts) {
221
+ const result = await this.ask(`inbox:${uid}`, opts);
222
+ emit("toolkit:sdk:inbox", ["sdk", "method-inbox", "stage:groups"]);
223
+ return "result" in result && result.result ? result.result : { uid, signals: [] };
224
+ }
225
+ /**
226
+ * Publish an agent.md to a workspace. The content is the full markdown
227
+ * (frontmatter + body). The server writes it to R2 at
228
+ * `{slug}/agents/{name}.md` and the next chat request loads it as the
229
+ * system prompt.
230
+ *
231
+ * Auth: this method requires the SDK to be configured with a token whose
232
+ * scope includes the target slug (use `<slug>:<token>` for owner-scoped
233
+ * publishes, or the SERVER_SECRET for CI/internal tooling).
234
+ *
235
+ * Mirrors the CLI flow: `oneie agent publish --slug <slug> <path>`.
236
+ */
237
+ async publishAgent(opts) {
238
+ const result = await this.r("/api/agents/publish", { method: "POST", body: JSON.stringify(opts) });
239
+ emit("toolkit:sdk:publishAgent", ["sdk", "method-publishAgent", "stage:author:publish"]);
240
+ return result;
241
+ }
242
+ /**
243
+ * Pull a previously published agent.md back from the workspace. Returns
244
+ * the file content verbatim — caller decides whether to write to disk,
245
+ * diff, or hand to compileAgent. Auth: same Bearer scope as publishAgent.
246
+ */
247
+ async pullAgent(opts) {
248
+ const qs = `slug=${encodeURIComponent(opts.slug)}&name=${encodeURIComponent(opts.name)}`;
249
+ const result = await this.r(`/api/agents/publish?${qs}`, { method: "GET" });
250
+ emit("toolkit:sdk:pullAgent", ["sdk", "method-pullAgent", "stage:author:pull"]);
251
+ return result;
252
+ }
253
+ /**
254
+ * Unpublish — remove a previously uploaded agent from the workspace.
255
+ * Idempotent: deleting a non-existent agent returns ok with removed=false.
256
+ */
257
+ async unpublishAgent(opts) {
258
+ const qs = `slug=${encodeURIComponent(opts.slug)}&name=${encodeURIComponent(opts.name)}`;
259
+ const result = await this.r(`/api/agents/publish?${qs}`, { method: "DELETE" });
260
+ emit("toolkit:sdk:unpublishAgent", ["sdk", "method-unpublishAgent", "stage:author:unpublish"]);
155
261
  return result;
156
262
  }
157
263
  async deployOnBehalf(opts) {
158
- const result = await this.r("/api/agents/deploy-on-behalf", { method: "POST", body: JSON.stringify(opts) });
264
+ const result = await this.ask("agents:deploy-on-behalf", opts);
159
265
  emit("toolkit:sdk:deployOnBehalf", ["sdk", "method-deployOnBehalf", "stage:team-deploy:on-behalf"]);
266
+ return "result" in result && result.result ? result.result : { ok: false, uid: "", owner: opts.owner, inheritedPaths: [] };
267
+ }
268
+ async sub(opts) {
269
+ await this.signal("subscriptions:register", opts);
270
+ emit("toolkit:sdk:sub", ["sdk", "method-sub", "stage:subscribe"]);
271
+ return { ok: true };
272
+ }
273
+ /** @deprecated Use sub() */
274
+ subscribe = this.sub;
275
+ async select(tag) {
276
+ const qs = tag ? `?tag=${encodeURIComponent(tag)}` : "";
277
+ const result = await this.r(`/api/select${qs}`);
278
+ emit("toolkit:sdk:select", ["sdk", "method-select"]);
160
279
  return result;
161
280
  }
162
- async subscribe(opts) {
163
- const result = await this.r("/api/subscribe", { method: "POST", body: JSON.stringify(opts) });
164
- emit("toolkit:sdk:subscribe", ["stage:list"]);
165
- return result;
281
+ groups(filters) {
282
+ const self = this;
283
+ const q = new URLSearchParams();
284
+ if (filters?.type)
285
+ q.set('type', filters.type);
286
+ if (filters?.after)
287
+ q.set('after', filters.after);
288
+ if (filters?.limit)
289
+ q.set('limit', String(filters.limit));
290
+ const iter = (async function* () {
291
+ const data = await self.r(`/api/export/groups${q.toString() ? `?${q}` : ''}`);
292
+ yield* (data.groups ?? []);
293
+ })();
294
+ return Object.assign(iter, {
295
+ get: (gid) => self.r(`/api/export/groups/${encodeURIComponent(gid)}`)
296
+ });
166
297
  }
167
- async select() {
168
- const result = await this.r("/api/loop/stage", { method: "POST", body: JSON.stringify({}) });
169
- emit("toolkit:sdk:select", ["sdk", "method-select"]);
170
- return result;
298
+ actors(filters) {
299
+ const self = this;
300
+ const q = new URLSearchParams();
301
+ if (filters?.type)
302
+ q.set('type', filters.type);
303
+ if (filters?.tag)
304
+ q.set('tag', filters.tag);
305
+ if (filters?.after)
306
+ q.set('after', filters.after);
307
+ if (filters?.limit)
308
+ q.set('limit', String(filters.limit));
309
+ const iter = (async function* () {
310
+ const data = await self.r(`/api/export/actors${q.toString() ? `?${q}` : ''}`);
311
+ yield* (data.actors ?? []);
312
+ })();
313
+ return Object.assign(iter, {
314
+ get: (aid) => self.r(`/api/actors/${encodeURIComponent(aid)}`)
315
+ });
316
+ }
317
+ things(filters) {
318
+ const self = this;
319
+ const q = new URLSearchParams();
320
+ if (filters?.type)
321
+ q.set('type', filters.type);
322
+ if (filters?.tag)
323
+ q.set('tag', filters.tag);
324
+ if (filters?.after)
325
+ q.set('after', filters.after);
326
+ if (filters?.limit)
327
+ q.set('limit', String(filters.limit));
328
+ const iter = (async function* () {
329
+ const data = await self.r(`/api/things${q.toString() ? `?${q}` : ''}`);
330
+ yield* (data.things ?? []);
331
+ })();
332
+ return Object.assign(iter, {
333
+ get: (tid) => self.r(`/api/things/${encodeURIComponent(tid)}`)
334
+ });
335
+ }
336
+ paths(filters) {
337
+ const self = this;
338
+ const q = new URLSearchParams();
339
+ if (filters?.source)
340
+ q.set('source', filters.source);
341
+ if (filters?.target)
342
+ q.set('target', filters.target);
343
+ if (filters?.minStrength)
344
+ q.set('minStrength', String(filters.minStrength));
345
+ if (filters?.after)
346
+ q.set('after', filters.after);
347
+ if (filters?.limit)
348
+ q.set('limit', String(filters.limit));
349
+ const iter = (async function* () {
350
+ const data = await self.r(`/api/export/paths${q.toString() ? `?${q}` : ''}`);
351
+ yield* (data.paths ?? []);
352
+ })();
353
+ return Object.assign(iter, {
354
+ get: (edge) => self.r(`/api/export/paths/${encodeURIComponent(edge)}`)
355
+ });
356
+ }
357
+ events(filters) {
358
+ const self = this;
359
+ const q = new URLSearchParams();
360
+ if (filters?.actor)
361
+ q.set('actor', filters.actor);
362
+ if (filters?.tag)
363
+ q.set('tag', filters.tag);
364
+ if (filters?.from)
365
+ q.set('from', filters.from);
366
+ if (filters?.to)
367
+ q.set('to', filters.to);
368
+ if (filters?.limit)
369
+ q.set('limit', String(filters.limit));
370
+ const iter = (async function* () {
371
+ const data = await self.r(`/api/events${q.toString() ? `?${q}` : ''}`);
372
+ yield* (data.events ?? []);
373
+ })();
374
+ return Object.assign(iter, {
375
+ get: (id) => self.r(`/api/events/${encodeURIComponent(id)}`)
376
+ });
377
+ }
378
+ learning(filters) {
379
+ const self = this;
380
+ const q = new URLSearchParams();
381
+ if (filters?.status)
382
+ q.set('status', filters.status);
383
+ if (filters?.actor)
384
+ q.set('actor', filters.actor);
385
+ if (filters?.tag)
386
+ q.set('tag', filters.tag);
387
+ if (filters?.limit)
388
+ q.set('limit', String(filters.limit));
389
+ const iter = (async function* () {
390
+ const data = await self.r(`/api/learning${q.toString() ? `?${q}` : ''}`);
391
+ yield* (data.hypotheses ?? []);
392
+ })();
393
+ return Object.assign(iter, {
394
+ get: (hid) => self.r(`/api/learning/${encodeURIComponent(hid)}`)
395
+ });
171
396
  }
172
397
  async authAgent(opts = {}) {
173
- const result = await this.r("/api/auth/agent", { method: "POST", body: JSON.stringify(opts) });
174
- emit("toolkit:sdk:authAgent", ["sdk", "method-authAgent", result.returning ? "returning" : "new", "stage:sign-in:agent"]);
175
- return result;
398
+ const result = await this.ask("auth:agent", opts);
399
+ const out = "result" in result && result.result ? result.result : { uid: "", name: "", kind: "agent", wallet: null, apiKey: "", keyId: "", returning: false };
400
+ emit("toolkit:sdk:authAgent", ["sdk", "method-authAgent", out.returning ? "returning" : "new", "stage:sign-in:agent"]);
401
+ return out;
176
402
  }
177
403
  async syncAgent(input) {
178
- const body = typeof input === "string" ? { markdown: input } : input;
179
- const result = await this.r("/api/agents/sync", { method: "POST", body: JSON.stringify(body) });
404
+ const data = typeof input === "string" ? { markdown: input } : input;
405
+ const result = await this.ask("agents:sync", data);
180
406
  emit("toolkit:sdk:syncAgent", ["sdk", "method-syncAgent", "stage:team-deploy"]);
181
- return result;
407
+ return "result" in result && result.result ? result.result : { ok: false, uid: "", skills: [] };
182
408
  }
183
409
  async discover(skill, limit = 10) {
184
410
  const result = await this.r(`/api/agents/discover?skill=${encodeURIComponent(skill)}&limit=${limit}`);
@@ -186,44 +412,44 @@ export class SubstrateClient {
186
412
  return result;
187
413
  }
188
414
  async register(uid, opts = {}) {
189
- const result = await this.r("/api/agents/register", { method: "POST", body: JSON.stringify({ uid, ...opts }) });
415
+ const result = await this.ask("agents:register", { uid, ...opts });
190
416
  emit("toolkit:sdk:register", ["sdk", "method-register", "stage:sell"]);
191
- return result;
417
+ return "result" in result && result.result ? result.result : { ok: false, uid, status: "pending", kind: "agent", wallet: null, walletLinked: false, capabilities: 0 };
192
418
  }
193
419
  async payWeight(from, to, task, amount) {
194
- const result = await this.r("/api/pay", { method: "POST", body: JSON.stringify({ from, to, task, amount }) });
420
+ const result = await this.ask("pay:weight", { from, to, task, amount });
195
421
  emit("toolkit:sdk:payWeight", ["sdk", "method-payWeight", "stage:buy"]);
196
- return result;
422
+ return "result" in result && result.result ? result.result : { ok: false, from, to, task, amount, sui: null };
197
423
  }
198
424
  async claw(name, opts = {}) {
199
- const result = await this.r("/api/claw", { method: "POST", body: JSON.stringify({ name, ...opts }) });
425
+ const result = await this.ask(`claw:${name}`, opts);
200
426
  emit("toolkit:sdk:claw", ["sdk", "method-claw"]);
201
- return result;
427
+ return "result" in result && result.result ? result.result : { ok: true };
202
428
  }
203
429
  async commend(uid) {
204
- const result = await this.r(`/api/agents/${encodeURIComponent(uid)}/commend`, { method: "POST", body: JSON.stringify({}) });
430
+ await this.signal("agents:commend", { uid });
205
431
  emit("toolkit:sdk:commend", ["sdk", "method-commend"]);
206
- return result;
432
+ return { ok: true, id: uid, action: "commend" };
207
433
  }
208
434
  async flag(uid) {
209
- const result = await this.r(`/api/agents/${encodeURIComponent(uid)}/flag`, { method: "POST", body: JSON.stringify({}) });
435
+ await this.signal("agents:flag", { uid });
210
436
  emit("toolkit:sdk:flag", ["sdk", "method-flag"]);
211
- return result;
437
+ return { ok: true, id: uid, action: "flag" };
212
438
  }
213
439
  async status(uid, active) {
214
- const result = await this.r(`/api/agents/${encodeURIComponent(uid)}/status`, { method: "POST", body: JSON.stringify({ status: active ? "active" : "inactive" }) });
440
+ await this.signal("agents:status", { uid, status: active ? "active" : "inactive" });
215
441
  emit("toolkit:sdk:status", ["sdk", "method-status"]);
216
- return result;
442
+ return { ok: true, id: uid, status: active ? "active" : "inactive" };
217
443
  }
218
444
  async capabilities(uid) {
219
- const result = await this.r(`/api/agents/${encodeURIComponent(uid)}/capabilities`);
445
+ const result = await this.ask("agents:capabilities", { uid });
220
446
  emit("toolkit:sdk:capabilities", ["sdk", "method-capabilities"]);
221
- return result;
447
+ return "result" in result && result.result ? result.result : [];
222
448
  }
223
449
  async stats() {
224
- const result = await this.r("/api/stats");
450
+ const result = await this.ask("stats:current");
225
451
  emit("toolkit:sdk:stats", ["sdk", "method-stats"]);
226
- return result;
452
+ return "result" in result && result.result ? result.result : { units: { total: 0, proven: 0, atRisk: 0 }, skills: { total: 0 }, highways: { count: 0, totalEdges: 0 }, revenue: { total: 0, gdp: 0 }, signals: { total: 0, recent: 0 }, timestamp: new Date().toISOString() };
227
453
  }
228
454
  async health() {
229
455
  const result = await this.r("/api/health");
@@ -231,44 +457,29 @@ export class SubstrateClient {
231
457
  return result;
232
458
  }
233
459
  async usage() {
234
- const result = await this.r("/api/dashboard/usage");
460
+ const result = await this.ask("dashboard:usage");
235
461
  emit("toolkit:sdk:usage", ["sdk", "method-usage"]);
236
- return result;
462
+ return "result" in result && result.result ? result.result : {};
237
463
  }
238
464
  async hire(providerUid, skillId, opts) {
239
- const result = await this.r("/api/buy/hire", {
240
- method: "POST",
241
- body: JSON.stringify({ providerUid, skillId, ...opts }),
242
- });
465
+ const result = await this.ask("market:hire", { providerUid, skillId, ...opts });
243
466
  emit("toolkit:sdk:hire", ["sdk", "method-hire"]);
244
- return result;
467
+ return "result" in result && result.result ? result.result : { status: 402, code: "dissolved", escrow_template: {}, expires_at: "" };
245
468
  }
246
469
  async bounty(opts) {
247
- const result = await this.r("/api/market/bounty", {
248
- method: "POST",
249
- body: JSON.stringify(opts),
250
- });
470
+ const result = await this.ask("market:bounty", opts);
251
471
  emit("toolkit:sdk:bounty", ["sdk", "method-bounty"]);
252
- return result;
472
+ return "result" in result && result.result ? result.result : { id: "", data: {} };
253
473
  }
254
474
  async bounties(query) {
255
- const params = new URLSearchParams();
256
- if (query?.seller)
257
- params.set("seller", query.seller);
258
- if (query?.poster)
259
- params.set("poster", query.poster);
260
- const qs = params.size ? `?${params}` : "";
261
- const result = await this.r(`/api/market/bounty${qs}`);
475
+ const result = await this.ask("market:bounties", query);
262
476
  emit("toolkit:sdk:bounties", ["sdk", "method-bounties"]);
263
- return result;
477
+ return "result" in result && result.result ? result.result : [];
264
478
  }
265
479
  async publish(opts) {
266
- const result = await this.r("/api/capabilities/publish", {
267
- method: "POST",
268
- body: JSON.stringify(opts),
269
- });
480
+ const result = await this.ask("capabilities:publish", opts);
270
481
  emit("toolkit:sdk:publish", ["sdk", "method-publish"]);
271
- return result;
482
+ return "result" in result && result.result ? result.result : { ok: true, sid: "", scope: "" };
272
483
  }
273
484
  async revenue() {
274
485
  const result = await this.r("/api/revenue");
@@ -285,44 +496,50 @@ export class SubstrateClient {
285
496
  emit("toolkit:sdk:listAgents", ["sdk", "method-listAgents"]);
286
497
  return result;
287
498
  }
288
- async closeLoop(session, outcome, opts) {
289
- const result = await this.r("/api/loop/close", {
499
+ async agentHistory(opts) {
500
+ const params = new URLSearchParams({ slug: opts.slug, name: opts.name });
501
+ const result = await this.r(`/api/agents/history?${params}`);
502
+ emit("toolkit:sdk:agentHistory", ["sdk", "method-agentHistory"]);
503
+ return result;
504
+ }
505
+ async rollbackAgent(opts) {
506
+ const result = await this.r("/api/agents/rollback", {
290
507
  method: "POST",
291
- body: JSON.stringify({ session, outcome, ...opts }),
508
+ body: JSON.stringify(opts),
292
509
  });
293
- emit("toolkit:sdk:closeLoop", ["sdk", "method-closeLoop"]);
510
+ emit("toolkit:sdk:rollbackAgent", ["sdk", "method-rollbackAgent"]);
511
+ return result;
512
+ }
513
+ async listSkills(opts) {
514
+ const params = new URLSearchParams({ slug: opts.slug });
515
+ const result = await this.r(`/api/skills/workspace?${params}`);
516
+ emit("toolkit:sdk:listSkills", ["sdk", "method-listSkills"]);
517
+ return result;
518
+ }
519
+ async unimportSkill(opts) {
520
+ const result = await this.r(`/api/skill/${encodeURIComponent(opts.name)}?slug=${encodeURIComponent(opts.slug)}`, { method: "DELETE" });
521
+ emit("toolkit:sdk:unimportSkill", ["sdk", "method-unimportSkill"]);
294
522
  return result;
295
523
  }
524
+ async closeLoop(session, outcome, opts) {
525
+ const result = await this.ask("loop:close", { session, outcome, ...opts });
526
+ emit("toolkit:sdk:closeLoop", ["sdk", "method-closeLoop"]);
527
+ return "result" in result && result.result ? result.result : { ok: true, stages: [], highways: [] };
528
+ }
296
529
  async signals(opts) {
297
- const params = new URLSearchParams();
298
- if (opts?.limit !== undefined)
299
- params.set("limit", String(opts.limit));
300
- if (opts?.since !== undefined)
301
- params.set("since", String(opts.since));
302
- if (opts?.from !== undefined)
303
- params.set("from", String(opts.from));
304
- if (opts?.to !== undefined)
305
- params.set("to", String(opts.to));
306
- const qs = params.size ? `?${params}` : "";
307
- const result = await this.r(`/api/signals${qs}`);
530
+ const result = await this.ask("signals:list", opts);
308
531
  emit("toolkit:sdk:signals", ["sdk", "method-signals"]);
309
- return result;
532
+ return "result" in result && result.result ? result.result : [];
310
533
  }
311
534
  async state() {
312
- const result = await this.r("/api/state");
535
+ const result = await this.ask("world:state");
313
536
  emit("toolkit:sdk:state", ["sdk", "method-state"]);
314
- return result;
537
+ return "result" in result && result.result ? result.result : { units: [], edges: [], highways: [], tags: [], tagMap: {}, stats: {} };
315
538
  }
316
539
  async listMarket(opts) {
317
- const params = new URLSearchParams();
318
- if (opts?.tag)
319
- params.set("tag", opts.tag);
320
- if (opts?.maxPrice !== undefined)
321
- params.set("maxPrice", String(opts.maxPrice));
322
- const qs = params.size ? `?${params}` : "";
323
- const result = await this.r(`/api/market/list${qs}`);
540
+ const result = await this.ask("market:list", opts);
324
541
  emit("toolkit:sdk:listMarket", ["sdk", "method-listMarket"]);
325
- return result;
542
+ return "result" in result && result.result ? result.result : { capabilities: [] };
326
543
  }
327
544
  async chat(messages, opts) {
328
545
  const url = `${this.baseUrl}/api/chat`;
@@ -339,45 +556,5 @@ export class SubstrateClient {
339
556
  emit("toolkit:sdk:chat", ["sdk", "method-chat"]);
340
557
  return res.body;
341
558
  }
342
- async *streamState() {
343
- const url = `${this.baseUrl}/api/stream`;
344
- const headers = {};
345
- if (this.apiKey)
346
- headers["Authorization"] = `Bearer ${this.apiKey}`;
347
- const res = await fetch(url, { headers });
348
- if (!res.ok || !res.body)
349
- throw new SubstrateError(`stream failed: ${res.status}`, res.status);
350
- emit("toolkit:sdk:streamState", ["sdk", "method-streamState"]);
351
- const reader = res.body.getReader();
352
- const decoder = new TextDecoder();
353
- let buf = "";
354
- let currentEvent = "connected";
355
- try {
356
- while (true) {
357
- const { done, value } = await reader.read();
358
- if (done)
359
- break;
360
- buf += decoder.decode(value, { stream: true });
361
- const lines = buf.split("\n");
362
- buf = lines.pop() ?? "";
363
- for (const line of lines) {
364
- if (line.startsWith("event:"))
365
- currentEvent = line.slice(6).trim();
366
- else if (line.startsWith("data:")) {
367
- try {
368
- yield { event: currentEvent, data: JSON.parse(line.slice(5).trim()) };
369
- }
370
- catch {
371
- yield { event: currentEvent, data: line.slice(5).trim() };
372
- }
373
- currentEvent = "connected";
374
- }
375
- }
376
- }
377
- }
378
- finally {
379
- reader.releaseLock();
380
- }
381
- }
382
559
  }
383
560
  //# sourceMappingURL=client.js.map