@oneie/sdk 0.8.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 (72) hide show
  1. package/LICENSE +18 -0
  2. package/README.md +333 -190
  3. package/dist/auth.d.ts +102 -0
  4. package/dist/auth.d.ts.map +1 -0
  5. package/dist/auth.js +154 -0
  6. package/dist/auth.js.map +1 -0
  7. package/dist/client.d.ts +197 -30
  8. package/dist/client.d.ts.map +1 -1
  9. package/dist/client.js +303 -172
  10. package/dist/client.js.map +1 -1
  11. package/dist/compile.d.ts +123 -0
  12. package/dist/compile.d.ts.map +1 -0
  13. package/dist/compile.js +652 -0
  14. package/dist/compile.js.map +1 -0
  15. package/dist/fetch.d.ts +40 -0
  16. package/dist/fetch.d.ts.map +1 -0
  17. package/dist/fetch.js +158 -0
  18. package/dist/fetch.js.map +1 -0
  19. package/dist/generated/types.d.ts +104 -0
  20. package/dist/generated/types.d.ts.map +1 -0
  21. package/dist/generated/types.js +5 -0
  22. package/dist/generated/types.js.map +1 -0
  23. package/dist/index.d.ts +8 -1
  24. package/dist/index.d.ts.map +1 -1
  25. package/dist/index.js +6 -1
  26. package/dist/index.js.map +1 -1
  27. package/dist/pay.d.ts +2 -2
  28. package/dist/pay.d.ts.map +1 -1
  29. package/dist/pay.js +5 -5
  30. package/dist/pay.js.map +1 -1
  31. package/dist/schemas.d.ts +39 -0
  32. package/dist/schemas.d.ts.map +1 -1
  33. package/dist/schemas.js +26 -0
  34. package/dist/schemas.js.map +1 -1
  35. package/dist/skills.d.ts +40 -0
  36. package/dist/skills.d.ts.map +1 -0
  37. package/dist/skills.js +16 -0
  38. package/dist/skills.js.map +1 -0
  39. package/dist/storage.d.ts +7 -8
  40. package/dist/storage.d.ts.map +1 -1
  41. package/dist/storage.js +61 -33
  42. package/dist/storage.js.map +1 -1
  43. package/dist/telemetry.d.ts +9 -0
  44. package/dist/telemetry.d.ts.map +1 -1
  45. package/dist/telemetry.js +11 -0
  46. package/dist/telemetry.js.map +1 -1
  47. package/dist/testing/index.d.ts.map +1 -1
  48. package/dist/testing/index.js +4 -3
  49. package/dist/testing/index.js.map +1 -1
  50. package/dist/types.d.ts +100 -20
  51. package/dist/types.d.ts.map +1 -1
  52. package/package.json +26 -17
  53. package/dist/react/context.d.ts +0 -11
  54. package/dist/react/context.d.ts.map +0 -1
  55. package/dist/react/context.js +0 -13
  56. package/dist/react/context.js.map +0 -1
  57. package/dist/react/hooks.d.ts +0 -104
  58. package/dist/react/hooks.d.ts.map +0 -1
  59. package/dist/react/hooks.js +0 -247
  60. package/dist/react/hooks.js.map +0 -1
  61. package/dist/react/index.d.ts +0 -6
  62. package/dist/react/index.d.ts.map +0 -1
  63. package/dist/react/index.js +0 -5
  64. package/dist/react/index.js.map +0 -1
  65. package/dist/react/optimistic.d.ts +0 -45
  66. package/dist/react/optimistic.d.ts.map +0 -1
  67. package/dist/react/optimistic.js +0 -52
  68. package/dist/react/optimistic.js.map +0 -1
  69. package/dist/react/stream.d.ts +0 -18
  70. package/dist/react/stream.d.ts.map +0 -1
  71. package/dist/react/stream.js +0 -60
  72. 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,70 +105,82 @@ 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"]);
155
- return result;
183
+ return "result" in result && result.result ? result.result : { ok: false, uid: opts.uid, group: opts.group ?? "", role: "" };
156
184
  }
157
185
  async createGroup(opts) {
158
186
  const result = await this.r("/api/groups", { method: "POST", body: JSON.stringify(opts) });
@@ -165,66 +193,218 @@ export class SubstrateClient {
165
193
  return result;
166
194
  }
167
195
  async joinGroup(gid) {
168
- const result = await this.r("/api/groups/join", { method: "POST", body: JSON.stringify({ gid }) });
196
+ await this.signal("groups:join", { gid });
169
197
  emit("toolkit:sdk:joinGroup", ["sdk", "method-joinGroup", "stage:groups"]);
170
- return result;
198
+ return { ok: true, gid, role: "member" };
171
199
  }
172
200
  async leaveGroup(gid) {
173
- const result = await this.r("/api/groups/leave", { method: "POST", body: JSON.stringify({ gid }) });
201
+ await this.signal("groups:leave", { gid });
174
202
  emit("toolkit:sdk:leaveGroup", ["sdk", "method-leaveGroup", "stage:groups"]);
175
- return result;
203
+ return { ok: true };
176
204
  }
177
205
  async groupMembers(gid) {
178
- const result = await this.r(`/api/groups/${encodeURIComponent(gid)}/members`);
206
+ const result = await this.ask("groups:members", { gid });
179
207
  emit("toolkit:sdk:groupMembers", ["sdk", "method-groupMembers", "stage:groups"]);
180
- return result;
208
+ return "result" in result && result.result ? result.result : { gid, members: [] };
181
209
  }
182
210
  async inviteMember(gid, uid, role = "member") {
183
- const result = await this.r(`/api/groups/${encodeURIComponent(gid)}/invite`, { method: "POST", body: JSON.stringify({ uid, role }) });
211
+ await this.signal("groups:invite", { gid, uid, role });
184
212
  emit("toolkit:sdk:inviteMember", ["sdk", "method-inviteMember", "stage:groups"]);
185
- return result;
213
+ return { ok: true };
186
214
  }
187
215
  async bridge(from, to) {
188
- const result = await this.r("/api/paths/bridge", { method: "POST", body: JSON.stringify({ from, to }) });
216
+ const result = await this.ask("paths:bridge", { from, to });
189
217
  emit("toolkit:sdk:bridge", ["sdk", "method-bridge", "stage:groups"]);
190
- return result;
218
+ return "result" in result && result.result ? result.result : {};
191
219
  }
192
220
  async inbox(uid, opts) {
193
- const params = new URLSearchParams();
194
- if (opts?.limit !== undefined)
195
- params.set("limit", String(opts.limit));
196
- if (opts?.before !== undefined)
197
- params.set("before", opts.before);
198
- const qs = params.size ? `?${params}` : "";
199
- const result = await this.r(`/api/inbox/${encodeURIComponent(uid)}${qs}`);
221
+ const result = await this.ask(`inbox:${uid}`, opts);
200
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"]);
201
261
  return result;
202
262
  }
203
263
  async deployOnBehalf(opts) {
204
- 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);
205
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"]);
206
279
  return result;
207
280
  }
208
- async subscribe(opts) {
209
- const result = await this.r("/api/subscribe", { method: "POST", body: JSON.stringify({ uid: opts.receiver, tags: opts.tags, scope: opts.scope }) });
210
- emit("toolkit:sdk:subscribe", ["sdk", "method-subscribe", "stage:subscribe"]);
211
- 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
+ });
212
297
  }
213
- async select() {
214
- const result = await this.r("/api/loop/stage", { method: "POST", body: JSON.stringify({}) });
215
- emit("toolkit:sdk:select", ["sdk", "method-select"]);
216
- 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
+ });
217
396
  }
218
397
  async authAgent(opts = {}) {
219
- const result = await this.r("/api/auth/agent", { method: "POST", body: JSON.stringify(opts) });
220
- emit("toolkit:sdk:authAgent", ["sdk", "method-authAgent", result.returning ? "returning" : "new", "stage:sign-in:agent"]);
221
- 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;
222
402
  }
223
403
  async syncAgent(input) {
224
- const body = typeof input === "string" ? { markdown: input } : input;
225
- 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);
226
406
  emit("toolkit:sdk:syncAgent", ["sdk", "method-syncAgent", "stage:team-deploy"]);
227
- return result;
407
+ return "result" in result && result.result ? result.result : { ok: false, uid: "", skills: [] };
228
408
  }
229
409
  async discover(skill, limit = 10) {
230
410
  const result = await this.r(`/api/agents/discover?skill=${encodeURIComponent(skill)}&limit=${limit}`);
@@ -232,44 +412,44 @@ export class SubstrateClient {
232
412
  return result;
233
413
  }
234
414
  async register(uid, opts = {}) {
235
- 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 });
236
416
  emit("toolkit:sdk:register", ["sdk", "method-register", "stage:sell"]);
237
- return result;
417
+ return "result" in result && result.result ? result.result : { ok: false, uid, status: "pending", kind: "agent", wallet: null, walletLinked: false, capabilities: 0 };
238
418
  }
239
419
  async payWeight(from, to, task, amount) {
240
- 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 });
241
421
  emit("toolkit:sdk:payWeight", ["sdk", "method-payWeight", "stage:buy"]);
242
- return result;
422
+ return "result" in result && result.result ? result.result : { ok: false, from, to, task, amount, sui: null };
243
423
  }
244
424
  async claw(name, opts = {}) {
245
- const result = await this.r("/api/claw", { method: "POST", body: JSON.stringify({ name, ...opts }) });
425
+ const result = await this.ask(`claw:${name}`, opts);
246
426
  emit("toolkit:sdk:claw", ["sdk", "method-claw"]);
247
- return result;
427
+ return "result" in result && result.result ? result.result : { ok: true };
248
428
  }
249
429
  async commend(uid) {
250
- const result = await this.r(`/api/agents/${encodeURIComponent(uid)}/commend`, { method: "POST", body: JSON.stringify({}) });
430
+ await this.signal("agents:commend", { uid });
251
431
  emit("toolkit:sdk:commend", ["sdk", "method-commend"]);
252
- return result;
432
+ return { ok: true, id: uid, action: "commend" };
253
433
  }
254
434
  async flag(uid) {
255
- const result = await this.r(`/api/agents/${encodeURIComponent(uid)}/flag`, { method: "POST", body: JSON.stringify({}) });
435
+ await this.signal("agents:flag", { uid });
256
436
  emit("toolkit:sdk:flag", ["sdk", "method-flag"]);
257
- return result;
437
+ return { ok: true, id: uid, action: "flag" };
258
438
  }
259
439
  async status(uid, active) {
260
- 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" });
261
441
  emit("toolkit:sdk:status", ["sdk", "method-status"]);
262
- return result;
442
+ return { ok: true, id: uid, status: active ? "active" : "inactive" };
263
443
  }
264
444
  async capabilities(uid) {
265
- const result = await this.r(`/api/agents/${encodeURIComponent(uid)}/capabilities`);
445
+ const result = await this.ask("agents:capabilities", { uid });
266
446
  emit("toolkit:sdk:capabilities", ["sdk", "method-capabilities"]);
267
- return result;
447
+ return "result" in result && result.result ? result.result : [];
268
448
  }
269
449
  async stats() {
270
- const result = await this.r("/api/stats");
450
+ const result = await this.ask("stats:current");
271
451
  emit("toolkit:sdk:stats", ["sdk", "method-stats"]);
272
- 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() };
273
453
  }
274
454
  async health() {
275
455
  const result = await this.r("/api/health");
@@ -277,44 +457,29 @@ export class SubstrateClient {
277
457
  return result;
278
458
  }
279
459
  async usage() {
280
- const result = await this.r("/api/dashboard/usage");
460
+ const result = await this.ask("dashboard:usage");
281
461
  emit("toolkit:sdk:usage", ["sdk", "method-usage"]);
282
- return result;
462
+ return "result" in result && result.result ? result.result : {};
283
463
  }
284
464
  async hire(providerUid, skillId, opts) {
285
- const result = await this.r("/api/buy/hire", {
286
- method: "POST",
287
- body: JSON.stringify({ providerUid, skillId, ...opts }),
288
- });
465
+ const result = await this.ask("market:hire", { providerUid, skillId, ...opts });
289
466
  emit("toolkit:sdk:hire", ["sdk", "method-hire"]);
290
- return result;
467
+ return "result" in result && result.result ? result.result : { status: 402, code: "dissolved", escrow_template: {}, expires_at: "" };
291
468
  }
292
469
  async bounty(opts) {
293
- const result = await this.r("/api/market/bounty", {
294
- method: "POST",
295
- body: JSON.stringify(opts),
296
- });
470
+ const result = await this.ask("market:bounty", opts);
297
471
  emit("toolkit:sdk:bounty", ["sdk", "method-bounty"]);
298
- return result;
472
+ return "result" in result && result.result ? result.result : { id: "", data: {} };
299
473
  }
300
474
  async bounties(query) {
301
- const params = new URLSearchParams();
302
- if (query?.seller)
303
- params.set("seller", query.seller);
304
- if (query?.poster)
305
- params.set("poster", query.poster);
306
- const qs = params.size ? `?${params}` : "";
307
- const result = await this.r(`/api/market/bounty${qs}`);
475
+ const result = await this.ask("market:bounties", query);
308
476
  emit("toolkit:sdk:bounties", ["sdk", "method-bounties"]);
309
- return result;
477
+ return "result" in result && result.result ? result.result : [];
310
478
  }
311
479
  async publish(opts) {
312
- const result = await this.r("/api/capabilities/publish", {
313
- method: "POST",
314
- body: JSON.stringify(opts),
315
- });
480
+ const result = await this.ask("capabilities:publish", opts);
316
481
  emit("toolkit:sdk:publish", ["sdk", "method-publish"]);
317
- return result;
482
+ return "result" in result && result.result ? result.result : { ok: true, sid: "", scope: "" };
318
483
  }
319
484
  async revenue() {
320
485
  const result = await this.r("/api/revenue");
@@ -331,44 +496,50 @@ export class SubstrateClient {
331
496
  emit("toolkit:sdk:listAgents", ["sdk", "method-listAgents"]);
332
497
  return result;
333
498
  }
334
- async closeLoop(session, outcome, opts) {
335
- 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", {
336
507
  method: "POST",
337
- body: JSON.stringify({ session, outcome, ...opts }),
508
+ body: JSON.stringify(opts),
338
509
  });
339
- emit("toolkit:sdk:closeLoop", ["sdk", "method-closeLoop"]);
510
+ emit("toolkit:sdk:rollbackAgent", ["sdk", "method-rollbackAgent"]);
340
511
  return result;
341
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"]);
522
+ return result;
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
+ }
342
529
  async signals(opts) {
343
- const params = new URLSearchParams();
344
- if (opts?.limit !== undefined)
345
- params.set("limit", String(opts.limit));
346
- if (opts?.since !== undefined)
347
- params.set("since", String(opts.since));
348
- if (opts?.from !== undefined)
349
- params.set("from", String(opts.from));
350
- if (opts?.to !== undefined)
351
- params.set("to", String(opts.to));
352
- const qs = params.size ? `?${params}` : "";
353
- const result = await this.r(`/api/signals${qs}`);
530
+ const result = await this.ask("signals:list", opts);
354
531
  emit("toolkit:sdk:signals", ["sdk", "method-signals"]);
355
- return result;
532
+ return "result" in result && result.result ? result.result : [];
356
533
  }
357
534
  async state() {
358
- const result = await this.r("/api/state");
535
+ const result = await this.ask("world:state");
359
536
  emit("toolkit:sdk:state", ["sdk", "method-state"]);
360
- return result;
537
+ return "result" in result && result.result ? result.result : { units: [], edges: [], highways: [], tags: [], tagMap: {}, stats: {} };
361
538
  }
362
539
  async listMarket(opts) {
363
- const params = new URLSearchParams();
364
- if (opts?.tag)
365
- params.set("tag", opts.tag);
366
- if (opts?.maxPrice !== undefined)
367
- params.set("maxPrice", String(opts.maxPrice));
368
- const qs = params.size ? `?${params}` : "";
369
- const result = await this.r(`/api/market/list${qs}`);
540
+ const result = await this.ask("market:list", opts);
370
541
  emit("toolkit:sdk:listMarket", ["sdk", "method-listMarket"]);
371
- return result;
542
+ return "result" in result && result.result ? result.result : { capabilities: [] };
372
543
  }
373
544
  async chat(messages, opts) {
374
545
  const url = `${this.baseUrl}/api/chat`;
@@ -385,45 +556,5 @@ export class SubstrateClient {
385
556
  emit("toolkit:sdk:chat", ["sdk", "method-chat"]);
386
557
  return res.body;
387
558
  }
388
- async *streamState() {
389
- const url = `${this.baseUrl}/api/stream`;
390
- const headers = {};
391
- if (this.apiKey)
392
- headers["Authorization"] = `Bearer ${this.apiKey}`;
393
- const res = await fetch(url, { headers });
394
- if (!res.ok || !res.body)
395
- throw new SubstrateError(`stream failed: ${res.status}`, res.status);
396
- emit("toolkit:sdk:streamState", ["sdk", "method-streamState"]);
397
- const reader = res.body.getReader();
398
- const decoder = new TextDecoder();
399
- let buf = "";
400
- let currentEvent = "connected";
401
- try {
402
- while (true) {
403
- const { done, value } = await reader.read();
404
- if (done)
405
- break;
406
- buf += decoder.decode(value, { stream: true });
407
- const lines = buf.split("\n");
408
- buf = lines.pop() ?? "";
409
- for (const line of lines) {
410
- if (line.startsWith("event:"))
411
- currentEvent = line.slice(6).trim();
412
- else if (line.startsWith("data:")) {
413
- try {
414
- yield { event: currentEvent, data: JSON.parse(line.slice(5).trim()) };
415
- }
416
- catch {
417
- yield { event: currentEvent, data: line.slice(5).trim() };
418
- }
419
- currentEvent = "connected";
420
- }
421
- }
422
- }
423
- }
424
- finally {
425
- reader.releaseLock();
426
- }
427
- }
428
559
  }
429
560
  //# sourceMappingURL=client.js.map