@synap-core/cli 1.6.1 → 1.7.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.
- package/dist/commands/data.js +16 -9
- package/dist/commands/data.js.map +1 -1
- package/dist/commands/discover.d.ts +18 -0
- package/dist/commands/discover.js +71 -0
- package/dist/commands/discover.js.map +1 -0
- package/dist/commands/knowledge.js +9 -5
- package/dist/commands/knowledge.js.map +1 -1
- package/dist/index.js +11 -0
- package/dist/index.js.map +1 -1
- package/dist/lib/hub-client.d.ts +8 -0
- package/dist/lib/hub-client.js +14 -0
- package/dist/lib/hub-client.js.map +1 -1
- package/dist/lib/pod.d.ts +8 -0
- package/dist/lib/pod.js +9 -1
- package/dist/lib/pod.js.map +1 -1
- package/dist/lib/targets.js +31 -7
- package/dist/lib/targets.js.map +1 -1
- package/node_modules/@synap/hub-rest-client/dist/index.cjs +881 -0
- package/node_modules/@synap/hub-rest-client/dist/index.d.cts +907 -0
- package/node_modules/@synap/hub-rest-client/dist/index.d.ts +907 -0
- package/node_modules/@synap/hub-rest-client/dist/index.js +851 -0
- package/node_modules/@synap/hub-rest-client/package.json +45 -0
- package/node_modules/@synap/hub-rest-client/src/client.ts +1143 -0
- package/node_modules/@synap/hub-rest-client/src/errors.ts +30 -0
- package/node_modules/@synap/hub-rest-client/src/index.ts +111 -0
- package/node_modules/@synap/hub-rest-client/src/setup.ts +77 -0
- package/node_modules/@synap/hub-rest-client/src/types.ts +639 -0
- package/package.json +8 -2
- package/skills/synap/SKILL.md +19 -20
|
@@ -0,0 +1,1143 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HubRestClient — typed HTTP client for the Synap Hub Protocol REST API.
|
|
3
|
+
*
|
|
4
|
+
* Uses the native `fetch` API (Node.js >= 18, browsers, Deno, Bun).
|
|
5
|
+
* Zero runtime dependencies.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* const client = new HubRestClient({
|
|
10
|
+
* podUrl: "https://my-pod.synap.live",
|
|
11
|
+
* apiKey: "synap_hub_live_...",
|
|
12
|
+
* });
|
|
13
|
+
*
|
|
14
|
+
* const entities = await client.searchEntities("meeting notes", { profileSlug: "note" });
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import { HubApiError } from "./errors.js";
|
|
19
|
+
import type {
|
|
20
|
+
HubEntity,
|
|
21
|
+
HubChannel,
|
|
22
|
+
HubWorkspace,
|
|
23
|
+
HubUser,
|
|
24
|
+
HubMemoryResult,
|
|
25
|
+
HubListResponse,
|
|
26
|
+
HubSingleResponse,
|
|
27
|
+
HubWorkspacesListResponse,
|
|
28
|
+
CreateEntityInput,
|
|
29
|
+
UpdateEntityInput,
|
|
30
|
+
StoreMemoryInput,
|
|
31
|
+
SendToChannelInput,
|
|
32
|
+
CaptureProposal,
|
|
33
|
+
CaptureStructureResponse,
|
|
34
|
+
CaptureExecuteInput,
|
|
35
|
+
CaptureExecuteResponse,
|
|
36
|
+
HubDocument,
|
|
37
|
+
HubRelation,
|
|
38
|
+
HubGraphResult,
|
|
39
|
+
HubConnectionsResult,
|
|
40
|
+
HubProfile,
|
|
41
|
+
HubPropertyDef,
|
|
42
|
+
HubDiscoverResult,
|
|
43
|
+
HubThread,
|
|
44
|
+
HubMessage,
|
|
45
|
+
HubThreadContext,
|
|
46
|
+
HubProposal,
|
|
47
|
+
HubView,
|
|
48
|
+
HubSearchResult,
|
|
49
|
+
HubCommand,
|
|
50
|
+
HubAgentUser,
|
|
51
|
+
HubUserContext,
|
|
52
|
+
HubGovernanceResult,
|
|
53
|
+
CreateThreadInput,
|
|
54
|
+
CreateRelationInput,
|
|
55
|
+
CreateViewInput,
|
|
56
|
+
ExecuteCommandInput,
|
|
57
|
+
CreateDocumentInput,
|
|
58
|
+
HubAutomation,
|
|
59
|
+
CreateAutomationInput,
|
|
60
|
+
UpdateAutomationInput,
|
|
61
|
+
AutomationStatus,
|
|
62
|
+
ReactionKind,
|
|
63
|
+
ReactionLens,
|
|
64
|
+
HubReactionEvent,
|
|
65
|
+
CreateNotificationInput,
|
|
66
|
+
HubWebhookDelivery,
|
|
67
|
+
} from "./types.js";
|
|
68
|
+
|
|
69
|
+
export interface HubRestClientConfig {
|
|
70
|
+
/** Pod URL, e.g. https://my-pod.synap.live */
|
|
71
|
+
podUrl: string;
|
|
72
|
+
/** Hub Protocol API key (Bearer token) */
|
|
73
|
+
apiKey: string;
|
|
74
|
+
/** Default workspace ID — used when not specified per call */
|
|
75
|
+
workspaceId?: string;
|
|
76
|
+
/** Optional request timeout in ms (default: 30000) */
|
|
77
|
+
timeoutMs?: number;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function normalizeUrl(url: string): string {
|
|
81
|
+
return url.replace(/\/$/, "");
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function formatHubErrorMessage(
|
|
85
|
+
status: number,
|
|
86
|
+
statusText: string,
|
|
87
|
+
errorBody: unknown
|
|
88
|
+
): string {
|
|
89
|
+
let detail = "";
|
|
90
|
+
if (errorBody && typeof errorBody === "object" && "error" in errorBody) {
|
|
91
|
+
detail = String((errorBody as { error: unknown }).error);
|
|
92
|
+
}
|
|
93
|
+
const base = `Hub API error: ${status} ${statusText}`;
|
|
94
|
+
let msg = detail ? `${base} — ${detail}` : base;
|
|
95
|
+
if (status === 403 && /hub-protocol\.write/i.test(detail)) {
|
|
96
|
+
msg +=
|
|
97
|
+
" Create or reconnect an API key that includes the hub-protocol.write scope (Settings → API keys on your pod).";
|
|
98
|
+
}
|
|
99
|
+
return msg;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function unwrapList<T>(result: T[] | HubListResponse<T>): T[] {
|
|
103
|
+
return Array.isArray(result)
|
|
104
|
+
? result
|
|
105
|
+
: ((result as HubListResponse<T>).data ?? []);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** Hub GET /workspaces returns `{ workspaces }`, not `{ data }`. */
|
|
109
|
+
function unwrapWorkspacesResponse(
|
|
110
|
+
result:
|
|
111
|
+
| HubWorkspace[]
|
|
112
|
+
| HubListResponse<HubWorkspace>
|
|
113
|
+
| HubWorkspacesListResponse
|
|
114
|
+
): HubWorkspace[] {
|
|
115
|
+
if (Array.isArray(result)) return result;
|
|
116
|
+
if (result && typeof result === "object" && "workspaces" in result) {
|
|
117
|
+
const w = (result as HubWorkspacesListResponse).workspaces;
|
|
118
|
+
return Array.isArray(w) ? w : [];
|
|
119
|
+
}
|
|
120
|
+
return unwrapList(result as HubWorkspace[] | HubListResponse<HubWorkspace>);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function unwrapSingle<T>(result: T | HubSingleResponse<T>): T {
|
|
124
|
+
if (result && typeof result === "object" && "data" in result) {
|
|
125
|
+
return (result as HubSingleResponse<T>).data;
|
|
126
|
+
}
|
|
127
|
+
return result as T;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export class HubRestClient {
|
|
131
|
+
private readonly base: string;
|
|
132
|
+
private readonly headers: Record<string, string>;
|
|
133
|
+
private readonly timeoutMs: number;
|
|
134
|
+
readonly workspaceId: string | undefined;
|
|
135
|
+
|
|
136
|
+
/** Cached from GET /users/me — avoids repeated identity calls. */
|
|
137
|
+
private resolvedUserId: string | null = null;
|
|
138
|
+
|
|
139
|
+
constructor(config: HubRestClientConfig) {
|
|
140
|
+
this.base = normalizeUrl(config.podUrl);
|
|
141
|
+
this.headers = {
|
|
142
|
+
Authorization: `Bearer ${config.apiKey}`,
|
|
143
|
+
"Content-Type": "application/json",
|
|
144
|
+
};
|
|
145
|
+
this.workspaceId = config.workspaceId;
|
|
146
|
+
this.timeoutMs = config.timeoutMs ?? 30_000;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** User id for the current API key (Hub REST requires userId on several GETs). */
|
|
150
|
+
private async resolveUserId(): Promise<string> {
|
|
151
|
+
if (this.resolvedUserId) return this.resolvedUserId;
|
|
152
|
+
const me = await this.getMe();
|
|
153
|
+
this.resolvedUserId = me.id;
|
|
154
|
+
return me.id;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
private async request<T>(
|
|
158
|
+
method: string,
|
|
159
|
+
path: string,
|
|
160
|
+
body?: unknown,
|
|
161
|
+
signal?: AbortSignal
|
|
162
|
+
): Promise<T> {
|
|
163
|
+
const url = `${this.base}${path}`;
|
|
164
|
+
const timeout = AbortSignal.timeout(this.timeoutMs);
|
|
165
|
+
const combined = signal ? AbortSignal.any([signal, timeout]) : timeout;
|
|
166
|
+
|
|
167
|
+
const res = await fetch(url, {
|
|
168
|
+
method,
|
|
169
|
+
headers: this.headers,
|
|
170
|
+
body: body !== undefined ? JSON.stringify(body) : undefined,
|
|
171
|
+
signal: combined,
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
if (!res.ok) {
|
|
175
|
+
const errorBody = await res.json().catch(() => ({}));
|
|
176
|
+
throw new HubApiError(
|
|
177
|
+
formatHubErrorMessage(res.status, res.statusText, errorBody),
|
|
178
|
+
res.status,
|
|
179
|
+
errorBody
|
|
180
|
+
);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
return res.json() as Promise<T>;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// ─── Identity ─────────────────────────────────────────────────────────────
|
|
187
|
+
|
|
188
|
+
async getMe(): Promise<HubUser> {
|
|
189
|
+
return this.request<HubUser>("GET", "/api/hub/users/me");
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
async getWorkspaces(): Promise<HubWorkspace[]> {
|
|
193
|
+
const result = await this.request<
|
|
194
|
+
HubWorkspace[] | HubListResponse<HubWorkspace> | HubWorkspacesListResponse
|
|
195
|
+
>("GET", "/api/hub/workspaces");
|
|
196
|
+
return unwrapWorkspacesResponse(result);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
async provisionAgentWorkspace(input: {
|
|
200
|
+
agentUserId: string;
|
|
201
|
+
workspaceName?: string;
|
|
202
|
+
}): Promise<{ workspaceId: string; created: boolean }> {
|
|
203
|
+
return this.request<{ workspaceId: string; created: boolean }>(
|
|
204
|
+
"POST",
|
|
205
|
+
"/api/hub/workspaces/provision-agent",
|
|
206
|
+
input
|
|
207
|
+
);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Get full activity context for a user — recent entities, active threads, workspace summary.
|
|
212
|
+
* Use at session start to orient the agent to the user's current state.
|
|
213
|
+
*/
|
|
214
|
+
async getUserContext(
|
|
215
|
+
userId: string,
|
|
216
|
+
options?: { workspaceId?: string }
|
|
217
|
+
): Promise<HubUserContext> {
|
|
218
|
+
const params = new URLSearchParams();
|
|
219
|
+
const wsId = options?.workspaceId ?? this.workspaceId;
|
|
220
|
+
if (wsId) params.set("workspaceId", wsId);
|
|
221
|
+
const qs = params.toString() ? `?${params}` : "";
|
|
222
|
+
return this.request<HubUserContext>(
|
|
223
|
+
"GET",
|
|
224
|
+
`/api/hub/users/${userId}/context${qs}`
|
|
225
|
+
);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
// ─── Entities ─────────────────────────────────────────────────────────────
|
|
229
|
+
|
|
230
|
+
async searchEntities(
|
|
231
|
+
query: string,
|
|
232
|
+
options?: {
|
|
233
|
+
profileSlug?: string;
|
|
234
|
+
workspaceId?: string;
|
|
235
|
+
/**
|
|
236
|
+
* "workspace" (default) — applies the client workspaceId filter.
|
|
237
|
+
* "all" — omits workspaceId entirely so results span all workspaces.
|
|
238
|
+
*/
|
|
239
|
+
scope?: "workspace" | "all";
|
|
240
|
+
limit?: number;
|
|
241
|
+
},
|
|
242
|
+
signal?: AbortSignal
|
|
243
|
+
): Promise<HubEntity[]> {
|
|
244
|
+
const params = new URLSearchParams({ q: query });
|
|
245
|
+
if (options?.profileSlug) params.set("profileSlug", options.profileSlug);
|
|
246
|
+
// When scope === "all", intentionally omit workspaceId for cross-workspace search.
|
|
247
|
+
if (options?.scope !== "all") {
|
|
248
|
+
const wsId = options?.workspaceId ?? this.workspaceId;
|
|
249
|
+
if (wsId) params.set("workspaceId", wsId);
|
|
250
|
+
}
|
|
251
|
+
if (options?.limit) params.set("limit", String(options.limit));
|
|
252
|
+
|
|
253
|
+
const result = await this.request<HubEntity[] | HubListResponse<HubEntity>>(
|
|
254
|
+
"GET",
|
|
255
|
+
`/api/hub/entities?${params}`,
|
|
256
|
+
undefined,
|
|
257
|
+
signal
|
|
258
|
+
);
|
|
259
|
+
return unwrapList(result);
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
async getEntity(id: string): Promise<HubEntity> {
|
|
263
|
+
const result = await this.request<HubEntity | HubSingleResponse<HubEntity>>(
|
|
264
|
+
"GET",
|
|
265
|
+
`/api/hub/entities/${id}`
|
|
266
|
+
);
|
|
267
|
+
return unwrapSingle(result);
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
async getRecentEntities(options?: {
|
|
271
|
+
profileSlug?: string;
|
|
272
|
+
workspaceId?: string;
|
|
273
|
+
limit?: number;
|
|
274
|
+
/** "all" — omits workspaceId so results span all workspaces the user can access. */
|
|
275
|
+
scope?: "workspace" | "all";
|
|
276
|
+
}): Promise<HubEntity[]> {
|
|
277
|
+
const params = new URLSearchParams({
|
|
278
|
+
sort: "updatedAt:desc",
|
|
279
|
+
limit: String(options?.limit ?? 20),
|
|
280
|
+
});
|
|
281
|
+
if (options?.profileSlug) params.set("profileSlug", options.profileSlug);
|
|
282
|
+
if (options?.scope !== "all") {
|
|
283
|
+
const wsId = options?.workspaceId ?? this.workspaceId;
|
|
284
|
+
if (wsId) params.set("workspaceId", wsId);
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
const result = await this.request<HubEntity[] | HubListResponse<HubEntity>>(
|
|
288
|
+
"GET",
|
|
289
|
+
`/api/hub/entities?${params}`
|
|
290
|
+
);
|
|
291
|
+
return unwrapList(result);
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
async createEntity(input: CreateEntityInput): Promise<HubGovernanceResult> {
|
|
295
|
+
return this.request<HubGovernanceResult>("POST", "/api/hub/entities", {
|
|
296
|
+
...input,
|
|
297
|
+
workspaceId: input.workspaceId ?? this.workspaceId,
|
|
298
|
+
});
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
async updateEntity(
|
|
302
|
+
id: string,
|
|
303
|
+
input: UpdateEntityInput
|
|
304
|
+
): Promise<HubEntity | HubGovernanceResult> {
|
|
305
|
+
// Backend may return a full entity (approved) or a governance envelope
|
|
306
|
+
// (proposed/denied). Callers MUST check `status` first when governance
|
|
307
|
+
// is likely — e.g., agent-owned workspaces, destructive updates.
|
|
308
|
+
return this.request<HubEntity | HubGovernanceResult>(
|
|
309
|
+
"PATCH",
|
|
310
|
+
`/api/hub/entities/${id}`,
|
|
311
|
+
input
|
|
312
|
+
);
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
// ─── Unified Search ───────────────────────────────────────────────────────
|
|
316
|
+
|
|
317
|
+
/**
|
|
318
|
+
* Unified full-text search across entities, documents, and views.
|
|
319
|
+
* Use when you don't know the content type. For entity-only search use searchEntities().
|
|
320
|
+
*
|
|
321
|
+
* Note: The backend GET /search requires userId as a query param; this method resolves
|
|
322
|
+
* the current user automatically.
|
|
323
|
+
*/
|
|
324
|
+
async search(
|
|
325
|
+
query: string,
|
|
326
|
+
options?: {
|
|
327
|
+
collections?: Array<"entities" | "documents" | "views">;
|
|
328
|
+
workspaceId?: string;
|
|
329
|
+
limit?: number;
|
|
330
|
+
},
|
|
331
|
+
signal?: AbortSignal
|
|
332
|
+
): Promise<HubSearchResult> {
|
|
333
|
+
const userId = await this.resolveUserId();
|
|
334
|
+
const wsId = options?.workspaceId ?? this.workspaceId;
|
|
335
|
+
const params = new URLSearchParams({ userId, query });
|
|
336
|
+
if (wsId) params.set("workspaceId", wsId);
|
|
337
|
+
if (options?.collections?.length)
|
|
338
|
+
params.set("collections", options.collections.join(","));
|
|
339
|
+
if (options?.limit) params.set("limit", String(options.limit));
|
|
340
|
+
return this.request<HubSearchResult>(
|
|
341
|
+
"GET",
|
|
342
|
+
`/api/hub/search?${params}`,
|
|
343
|
+
undefined,
|
|
344
|
+
signal
|
|
345
|
+
);
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
// ─── Relations & Graph ────────────────────────────────────────────────────
|
|
349
|
+
|
|
350
|
+
/**
|
|
351
|
+
* Get all relations for an entity — inbound and outbound.
|
|
352
|
+
* Use to discover connections before graph traversal.
|
|
353
|
+
*
|
|
354
|
+
* Note: The backend GET /relations requires both userId and workspaceId.
|
|
355
|
+
* This method resolves userId automatically; workspaceId falls back to client default.
|
|
356
|
+
*/
|
|
357
|
+
async getRelations(
|
|
358
|
+
entityId: string,
|
|
359
|
+
options?: { workspaceId?: string }
|
|
360
|
+
): Promise<HubRelation[]> {
|
|
361
|
+
const userId = await this.resolveUserId();
|
|
362
|
+
const wsId = options?.workspaceId ?? this.workspaceId;
|
|
363
|
+
if (!wsId) throw new Error("workspaceId is required for getRelations");
|
|
364
|
+
const params = new URLSearchParams({ userId, workspaceId: wsId, entityId });
|
|
365
|
+
const result = await this.request<
|
|
366
|
+
HubRelation[] | HubListResponse<HubRelation>
|
|
367
|
+
>("GET", `/api/hub/relations?${params}`);
|
|
368
|
+
return unwrapList(result);
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
/**
|
|
372
|
+
* Create a typed relation between two entities.
|
|
373
|
+
* Type is a free string — conventions: "related_to", "parent_of", "child_of",
|
|
374
|
+
* "belongs_to", "authored_by", "depends_on", "references".
|
|
375
|
+
* Goes through governance — may return "proposed".
|
|
376
|
+
*/
|
|
377
|
+
async createRelation(
|
|
378
|
+
input: CreateRelationInput
|
|
379
|
+
): Promise<HubGovernanceResult> {
|
|
380
|
+
const userId = input.userId ?? (await this.resolveUserId());
|
|
381
|
+
const wsId = input.workspaceId ?? this.workspaceId;
|
|
382
|
+
if (!wsId) throw new Error("workspaceId is required for createRelation");
|
|
383
|
+
return this.request<HubGovernanceResult>("POST", "/api/hub/relations", {
|
|
384
|
+
userId,
|
|
385
|
+
workspaceId: wsId,
|
|
386
|
+
sourceEntityId: input.sourceEntityId,
|
|
387
|
+
targetEntityId: input.targetEntityId,
|
|
388
|
+
type: input.type,
|
|
389
|
+
});
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
/**
|
|
393
|
+
* Delete a relation by ID (get from getRelations()).
|
|
394
|
+
*/
|
|
395
|
+
async deleteRelation(relationId: string): Promise<void> {
|
|
396
|
+
const userId = await this.resolveUserId();
|
|
397
|
+
await this.request<unknown>("DELETE", `/api/hub/relations/${relationId}`, {
|
|
398
|
+
userId,
|
|
399
|
+
});
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
/**
|
|
403
|
+
* Traverse the knowledge graph from an entity using BFS.
|
|
404
|
+
* Returns nodes and edges up to maxDepth hops away.
|
|
405
|
+
* maxDepth: 1=direct neighbors, 2=neighborhood (recommended), 3=extended (expensive).
|
|
406
|
+
*
|
|
407
|
+
* @example
|
|
408
|
+
* const graph = await client.traverseGraph(projectId, { maxDepth: 2 });
|
|
409
|
+
* const tasks = graph.nodes.filter(n => n.profileSlug === "task");
|
|
410
|
+
*/
|
|
411
|
+
async traverseGraph(
|
|
412
|
+
entityId: string,
|
|
413
|
+
options?: { maxDepth?: number; workspaceId?: string }
|
|
414
|
+
): Promise<HubGraphResult> {
|
|
415
|
+
const userId = await this.resolveUserId();
|
|
416
|
+
const params = new URLSearchParams({
|
|
417
|
+
userId,
|
|
418
|
+
startEntityId: entityId,
|
|
419
|
+
maxDepth: String(options?.maxDepth ?? 2),
|
|
420
|
+
});
|
|
421
|
+
const wsId = options?.workspaceId ?? this.workspaceId;
|
|
422
|
+
if (wsId) params.set("workspaceId", wsId);
|
|
423
|
+
return this.request<HubGraphResult>(
|
|
424
|
+
"GET",
|
|
425
|
+
`/api/hub/graph/traverse?${params}`
|
|
426
|
+
);
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
/**
|
|
430
|
+
* Unified view of everything connected to an entity. Merges three sources:
|
|
431
|
+
* 1. Graph relations — explicit rows in the relations table (both directions)
|
|
432
|
+
* 2. Structural links — entities whose `entity_id` properties point to this entity
|
|
433
|
+
* 3. Thread connections — chat threads that touched this entity
|
|
434
|
+
*
|
|
435
|
+
* Prefer this over `getRelations()` / `traverseGraph()` when you want the complete
|
|
436
|
+
* picture — those only see the relations table and miss property-based links that
|
|
437
|
+
* haven't been synced (notably custom profiles without a `relationDefId` mapping).
|
|
438
|
+
*
|
|
439
|
+
* Each connection carries a `source` field so callers can filter by origin.
|
|
440
|
+
*
|
|
441
|
+
* @example
|
|
442
|
+
* const { connections } = await client.getConnections(entityId);
|
|
443
|
+
* const tasks = connections.filter(c => c.entity?.profileSlug === "task");
|
|
444
|
+
*/
|
|
445
|
+
async getConnections(
|
|
446
|
+
entityId: string,
|
|
447
|
+
options?: { workspaceId?: string; limit?: number }
|
|
448
|
+
): Promise<HubConnectionsResult> {
|
|
449
|
+
const userId = await this.resolveUserId();
|
|
450
|
+
const wsId = options?.workspaceId ?? this.workspaceId;
|
|
451
|
+
const params = new URLSearchParams({ userId });
|
|
452
|
+
if (wsId) params.set("workspaceId", wsId);
|
|
453
|
+
if (options?.limit) params.set("limit", String(options.limit));
|
|
454
|
+
return this.request<HubConnectionsResult>(
|
|
455
|
+
"GET",
|
|
456
|
+
`/api/hub/entities/${entityId}/connections?${params}`
|
|
457
|
+
);
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
// ─── Profiles & Schema ────────────────────────────────────────────────────
|
|
461
|
+
|
|
462
|
+
/**
|
|
463
|
+
* List all entity profile types in the workspace.
|
|
464
|
+
* Always call before creating entities to discover what types are available.
|
|
465
|
+
* Returns system profiles (always present) + custom workspace profiles.
|
|
466
|
+
*/
|
|
467
|
+
async listProfiles(workspaceId: string): Promise<HubProfile[]> {
|
|
468
|
+
const userId = await this.resolveUserId();
|
|
469
|
+
const params = new URLSearchParams({ userId, workspaceId });
|
|
470
|
+
const result = await this.request<
|
|
471
|
+
HubProfile[] | HubListResponse<HubProfile>
|
|
472
|
+
>("GET", `/api/hub/profiles?${params}`);
|
|
473
|
+
return unwrapList(result);
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
/**
|
|
477
|
+
* List property definitions for a workspace, optionally filtered by profile.
|
|
478
|
+
*/
|
|
479
|
+
async listPropertyDefs(
|
|
480
|
+
workspaceId: string,
|
|
481
|
+
options?: { profileSlug?: string }
|
|
482
|
+
): Promise<HubPropertyDef[]> {
|
|
483
|
+
const userId = await this.resolveUserId();
|
|
484
|
+
const params = new URLSearchParams({ userId, workspaceId });
|
|
485
|
+
if (options?.profileSlug) params.set("profileId", options.profileSlug);
|
|
486
|
+
const result = await this.request<
|
|
487
|
+
HubPropertyDef[] | HubListResponse<HubPropertyDef>
|
|
488
|
+
>("GET", `/api/hub/property-defs?${params}`);
|
|
489
|
+
return unwrapList(result);
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
/**
|
|
493
|
+
* Runtime discovery — profiles with property schemas + command tree.
|
|
494
|
+
*
|
|
495
|
+
* Call once per session at session start. Returns ground-truth profile
|
|
496
|
+
* schemas (including custom workspace profiles) and the canonical CLI
|
|
497
|
+
* command map. Replaces static skill file profile descriptions.
|
|
498
|
+
*/
|
|
499
|
+
async discover(workspaceId?: string): Promise<HubDiscoverResult> {
|
|
500
|
+
const userId = await this.resolveUserId();
|
|
501
|
+
const wsId = workspaceId ?? this.workspaceId ?? "";
|
|
502
|
+
const params = new URLSearchParams({ userId, workspaceId: wsId });
|
|
503
|
+
return this.request<HubDiscoverResult>(
|
|
504
|
+
"GET",
|
|
505
|
+
`/api/hub/discover?${params}`
|
|
506
|
+
);
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
// ─── Threads & Channels ───────────────────────────────────────────────────
|
|
510
|
+
|
|
511
|
+
/**
|
|
512
|
+
* List threads (channels) accessible to a user.
|
|
513
|
+
*/
|
|
514
|
+
async listThreads(
|
|
515
|
+
userId: string,
|
|
516
|
+
options?: { workspaceId?: string; type?: string }
|
|
517
|
+
): Promise<HubThread[]> {
|
|
518
|
+
const wsId = options?.workspaceId ?? this.workspaceId;
|
|
519
|
+
const params = new URLSearchParams({ userId });
|
|
520
|
+
if (wsId) params.set("workspaceId", wsId);
|
|
521
|
+
if (options?.type) params.set("type", options.type);
|
|
522
|
+
const result = await this.request<HubThread[] | HubListResponse<HubThread>>(
|
|
523
|
+
"GET",
|
|
524
|
+
`/api/hub/threads?${params}`
|
|
525
|
+
);
|
|
526
|
+
return unwrapList(result);
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
/**
|
|
530
|
+
* Get the user's personal channel — their private AI conversation thread.
|
|
531
|
+
* Use as default destination for messages and proactive posts.
|
|
532
|
+
*
|
|
533
|
+
* Note: The backend GET /channels/personal requires both userId and workspaceId.
|
|
534
|
+
*/
|
|
535
|
+
async getPersonalChannel(
|
|
536
|
+
userId: string,
|
|
537
|
+
workspaceId?: string
|
|
538
|
+
): Promise<HubThread> {
|
|
539
|
+
const wsId = workspaceId ?? this.workspaceId;
|
|
540
|
+
if (!wsId)
|
|
541
|
+
throw new Error("workspaceId is required for getPersonalChannel");
|
|
542
|
+
const params = new URLSearchParams({ userId, workspaceId: wsId });
|
|
543
|
+
return this.request<HubThread>(
|
|
544
|
+
"GET",
|
|
545
|
+
`/api/hub/channels/personal?${params}`
|
|
546
|
+
);
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
/**
|
|
550
|
+
* Create a new thread. Pass entityId to auto-link on creation.
|
|
551
|
+
*
|
|
552
|
+
* Note: The backend POST /threads requires workspaceId in the body.
|
|
553
|
+
*/
|
|
554
|
+
async createThread(input: CreateThreadInput): Promise<HubThread> {
|
|
555
|
+
const userId = input.userId ?? (await this.resolveUserId());
|
|
556
|
+
const wsId = input.workspaceId ?? this.workspaceId;
|
|
557
|
+
if (!wsId) throw new Error("workspaceId is required for createThread");
|
|
558
|
+
return this.request<HubThread>("POST", "/api/hub/threads", {
|
|
559
|
+
userId,
|
|
560
|
+
workspaceId: wsId,
|
|
561
|
+
title: input.name,
|
|
562
|
+
agentType: input.agentType,
|
|
563
|
+
contextObjectType: input.entityId
|
|
564
|
+
? "entity"
|
|
565
|
+
: input.documentId
|
|
566
|
+
? "document"
|
|
567
|
+
: undefined,
|
|
568
|
+
contextObjectId: input.entityId ?? input.documentId,
|
|
569
|
+
});
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
/**
|
|
573
|
+
* Get full thread context: messages + all linked entities and documents.
|
|
574
|
+
* Call before sending a message to orient the AI with conversation history.
|
|
575
|
+
*/
|
|
576
|
+
async getThreadContext(threadId: string): Promise<HubThreadContext> {
|
|
577
|
+
return this.request<HubThreadContext>(
|
|
578
|
+
"GET",
|
|
579
|
+
`/api/hub/threads/${threadId}/context`
|
|
580
|
+
);
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
/**
|
|
584
|
+
* Get messages in a thread.
|
|
585
|
+
*/
|
|
586
|
+
async getMessages(
|
|
587
|
+
threadId: string,
|
|
588
|
+
_options?: { limit?: number; before?: string }
|
|
589
|
+
): Promise<HubMessage[]> {
|
|
590
|
+
// The backend GET /threads/:threadId/messages does not accept query params in
|
|
591
|
+
// the current implementation — returns all messages ordered by timestamp.
|
|
592
|
+
const result = await this.request<
|
|
593
|
+
HubMessage[] | HubListResponse<HubMessage>
|
|
594
|
+
>("GET", `/api/hub/threads/${threadId}/messages`);
|
|
595
|
+
return unwrapList(result);
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
/**
|
|
599
|
+
* Link an entity to a thread so it appears in thread context for AI.
|
|
600
|
+
*/
|
|
601
|
+
async linkEntityToThread(threadId: string, entityId: string): Promise<void> {
|
|
602
|
+
const userId = await this.resolveUserId();
|
|
603
|
+
await this.request<unknown>(
|
|
604
|
+
"POST",
|
|
605
|
+
`/api/hub/threads/${threadId}/link-entity`,
|
|
606
|
+
{ userId, entityId }
|
|
607
|
+
);
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
/**
|
|
611
|
+
* Link a document to a thread.
|
|
612
|
+
*/
|
|
613
|
+
async linkDocumentToThread(
|
|
614
|
+
threadId: string,
|
|
615
|
+
documentId: string
|
|
616
|
+
): Promise<void> {
|
|
617
|
+
const userId = await this.resolveUserId();
|
|
618
|
+
await this.request<unknown>(
|
|
619
|
+
"POST",
|
|
620
|
+
`/api/hub/threads/${threadId}/link-document`,
|
|
621
|
+
{ userId, documentId }
|
|
622
|
+
);
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
/**
|
|
626
|
+
* Get research branches of a thread — parallel AI investigations.
|
|
627
|
+
*/
|
|
628
|
+
async getThreadBranches(
|
|
629
|
+
threadId: string
|
|
630
|
+
): Promise<
|
|
631
|
+
Array<{ channelId: string; branchPurpose: string | null; status: string }>
|
|
632
|
+
> {
|
|
633
|
+
const result = await this.request<{
|
|
634
|
+
branches: Array<{
|
|
635
|
+
channelId: string;
|
|
636
|
+
branchPurpose: string | null;
|
|
637
|
+
status: string;
|
|
638
|
+
}>;
|
|
639
|
+
}>("GET", `/api/hub/threads/${threadId}/branches`);
|
|
640
|
+
return result.branches ?? [];
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
// ─── Memory ───────────────────────────────────────────────────────────────
|
|
644
|
+
|
|
645
|
+
async storeMemory(input: StoreMemoryInput): Promise<{ id: string }> {
|
|
646
|
+
const userId = await this.resolveUserId();
|
|
647
|
+
const fact =
|
|
648
|
+
input.context && String(input.context).trim().length > 0
|
|
649
|
+
? `[${String(input.context).trim()}] ${input.fact}`
|
|
650
|
+
: input.fact;
|
|
651
|
+
return this.request<{ id: string }>("POST", "/api/hub/memory", {
|
|
652
|
+
userId,
|
|
653
|
+
fact,
|
|
654
|
+
});
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
async recallMemory(
|
|
658
|
+
query: string,
|
|
659
|
+
options?: { workspaceId?: string; limit?: number }
|
|
660
|
+
): Promise<HubMemoryResult[]> {
|
|
661
|
+
const wsId = options?.workspaceId ?? this.workspaceId;
|
|
662
|
+
const userId = await this.resolveUserId();
|
|
663
|
+
const params = new URLSearchParams({
|
|
664
|
+
userId,
|
|
665
|
+
query,
|
|
666
|
+
limit: String(options?.limit ?? 10),
|
|
667
|
+
});
|
|
668
|
+
if (wsId) params.set("workspaceId", wsId);
|
|
669
|
+
|
|
670
|
+
const result = await this.request<
|
|
671
|
+
HubMemoryResult[] | HubListResponse<HubMemoryResult>
|
|
672
|
+
>("GET", `/api/hub/memory?${params}`);
|
|
673
|
+
return unwrapList(result);
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
/**
|
|
677
|
+
* Delete a stored memory fact by ID.
|
|
678
|
+
*/
|
|
679
|
+
async deleteMemory(memoryId: string): Promise<void> {
|
|
680
|
+
const userId = await this.resolveUserId();
|
|
681
|
+
const params = new URLSearchParams({ userId });
|
|
682
|
+
await this.request<unknown>(
|
|
683
|
+
"DELETE",
|
|
684
|
+
`/api/hub/memory/${memoryId}?${params}`
|
|
685
|
+
);
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
// ─── Channels (Hub REST: channels are listed via GET /threads) ────────────
|
|
689
|
+
|
|
690
|
+
async getChannels(options?: { workspaceId?: string }): Promise<HubChannel[]> {
|
|
691
|
+
const wsId = options?.workspaceId ?? this.workspaceId;
|
|
692
|
+
const userId = await this.resolveUserId();
|
|
693
|
+
const params = new URLSearchParams({ userId });
|
|
694
|
+
if (wsId) params.set("workspaceId", wsId);
|
|
695
|
+
|
|
696
|
+
const result = await this.request<
|
|
697
|
+
HubChannel[] | HubListResponse<HubChannel>
|
|
698
|
+
>("GET", `/api/hub/threads?${params}`);
|
|
699
|
+
return unwrapList(result);
|
|
700
|
+
}
|
|
701
|
+
|
|
702
|
+
async sendToChannel(input: SendToChannelInput): Promise<{ id: string }> {
|
|
703
|
+
const userId = input.userId ?? (await this.resolveUserId());
|
|
704
|
+
return this.request<{ id: string }>(
|
|
705
|
+
"POST",
|
|
706
|
+
`/api/hub/threads/${input.channelId}/messages`,
|
|
707
|
+
{
|
|
708
|
+
role: input.role ?? "user",
|
|
709
|
+
content: input.content,
|
|
710
|
+
userId,
|
|
711
|
+
...(input.autoRespond !== undefined
|
|
712
|
+
? { autoRespond: input.autoRespond }
|
|
713
|
+
: {}),
|
|
714
|
+
}
|
|
715
|
+
);
|
|
716
|
+
}
|
|
717
|
+
|
|
718
|
+
// ─── Proposals ────────────────────────────────────────────────────────────
|
|
719
|
+
|
|
720
|
+
/**
|
|
721
|
+
* List proposals — pending AI writes awaiting human review.
|
|
722
|
+
* Filter by status: "pending" (needs review), "approved", "rejected".
|
|
723
|
+
*/
|
|
724
|
+
async listProposals(options?: {
|
|
725
|
+
status?: "pending" | "approved" | "rejected";
|
|
726
|
+
workspaceId?: string;
|
|
727
|
+
limit?: number;
|
|
728
|
+
}): Promise<HubProposal[]> {
|
|
729
|
+
const userId = await this.resolveUserId();
|
|
730
|
+
const wsId = options?.workspaceId ?? this.workspaceId;
|
|
731
|
+
const params = new URLSearchParams({ userId });
|
|
732
|
+
if (options?.status) params.set("status", options.status);
|
|
733
|
+
if (wsId) params.set("workspaceId", wsId);
|
|
734
|
+
if (options?.limit) params.set("limit", String(options.limit));
|
|
735
|
+
const result = await this.request<
|
|
736
|
+
HubProposal[] | HubListResponse<HubProposal>
|
|
737
|
+
>("GET", `/api/hub/proposals?${params}`);
|
|
738
|
+
return unwrapList(result);
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
/**
|
|
742
|
+
* Approve or reject a proposal.
|
|
743
|
+
*
|
|
744
|
+
* Note: The backend PATCH /proposals/:id is an AI-revision endpoint that updates
|
|
745
|
+
* the proposal data/summary, not a review (approve/reject) endpoint.
|
|
746
|
+
* Use this to update proposal data before human review.
|
|
747
|
+
*/
|
|
748
|
+
async reviewProposal(
|
|
749
|
+
proposalId: string,
|
|
750
|
+
decision: "approved" | "rejected",
|
|
751
|
+
reason?: string
|
|
752
|
+
): Promise<HubProposal> {
|
|
753
|
+
return this.request<HubProposal>(
|
|
754
|
+
"PATCH",
|
|
755
|
+
`/api/hub/proposals/${proposalId}`,
|
|
756
|
+
{
|
|
757
|
+
data: { status: decision },
|
|
758
|
+
summary: reason,
|
|
759
|
+
}
|
|
760
|
+
);
|
|
761
|
+
}
|
|
762
|
+
|
|
763
|
+
// ─── Views ────────────────────────────────────────────────────────────────
|
|
764
|
+
|
|
765
|
+
/**
|
|
766
|
+
* List data views in a workspace.
|
|
767
|
+
*/
|
|
768
|
+
async listViews(
|
|
769
|
+
workspaceId: string,
|
|
770
|
+
options?: { profileSlug?: string }
|
|
771
|
+
): Promise<HubView[]> {
|
|
772
|
+
const userId = await this.resolveUserId();
|
|
773
|
+
const params = new URLSearchParams({ userId, workspaceId });
|
|
774
|
+
if (options?.profileSlug) params.set("profileId", options.profileSlug);
|
|
775
|
+
const result = await this.request<HubView[] | HubListResponse<HubView>>(
|
|
776
|
+
"GET",
|
|
777
|
+
`/api/hub/views?${params}`
|
|
778
|
+
);
|
|
779
|
+
return unwrapList(result);
|
|
780
|
+
}
|
|
781
|
+
|
|
782
|
+
/**
|
|
783
|
+
* Create a new view. Goes through governance.
|
|
784
|
+
*/
|
|
785
|
+
async createView(input: CreateViewInput): Promise<HubGovernanceResult> {
|
|
786
|
+
const userId = input.userId ?? (await this.resolveUserId());
|
|
787
|
+
return this.request<HubGovernanceResult>("POST", "/api/hub/views", {
|
|
788
|
+
userId,
|
|
789
|
+
workspaceId: input.workspaceId ?? this.workspaceId,
|
|
790
|
+
name: input.name,
|
|
791
|
+
type: input.type,
|
|
792
|
+
profileId: input.profileSlug,
|
|
793
|
+
config: input.config,
|
|
794
|
+
});
|
|
795
|
+
}
|
|
796
|
+
|
|
797
|
+
// ─── Documents ────────────────────────────────────────────────────────────
|
|
798
|
+
|
|
799
|
+
/**
|
|
800
|
+
* Get a document by ID with full markdown content.
|
|
801
|
+
*/
|
|
802
|
+
async getDocument(documentId: string): Promise<HubDocument> {
|
|
803
|
+
const userId = await this.resolveUserId();
|
|
804
|
+
const params = new URLSearchParams({ userId });
|
|
805
|
+
return this.request<HubDocument>(
|
|
806
|
+
"GET",
|
|
807
|
+
`/api/hub/documents/${documentId}?${params}`
|
|
808
|
+
);
|
|
809
|
+
}
|
|
810
|
+
|
|
811
|
+
/**
|
|
812
|
+
* Create a document. Use for long-form content: meeting notes, research, writeups.
|
|
813
|
+
* Goes through governance.
|
|
814
|
+
*/
|
|
815
|
+
async createDocument(
|
|
816
|
+
input: CreateDocumentInput & { workspaceId?: string }
|
|
817
|
+
): Promise<HubGovernanceResult> {
|
|
818
|
+
const userId = await this.resolveUserId();
|
|
819
|
+
return this.request<HubGovernanceResult>("POST", "/api/hub/documents", {
|
|
820
|
+
userId,
|
|
821
|
+
workspaceId: input.workspaceId ?? this.workspaceId,
|
|
822
|
+
title: input.title,
|
|
823
|
+
content: input.content ?? "",
|
|
824
|
+
});
|
|
825
|
+
}
|
|
826
|
+
|
|
827
|
+
// ─── Commands & Agents ────────────────────────────────────────────────────
|
|
828
|
+
|
|
829
|
+
/**
|
|
830
|
+
* List available commands (automation shortcuts) in the workspace.
|
|
831
|
+
*/
|
|
832
|
+
async listCommands(workspaceId?: string): Promise<HubCommand[]> {
|
|
833
|
+
const wsId = workspaceId ?? this.workspaceId;
|
|
834
|
+
const params = new URLSearchParams();
|
|
835
|
+
if (wsId) params.set("workspaceId", wsId);
|
|
836
|
+
const qs = params.toString() ? `?${params}` : "";
|
|
837
|
+
const result = await this.request<
|
|
838
|
+
HubCommand[] | HubListResponse<HubCommand>
|
|
839
|
+
>("GET", `/api/hub/commands${qs}`);
|
|
840
|
+
return unwrapList(result);
|
|
841
|
+
}
|
|
842
|
+
|
|
843
|
+
/**
|
|
844
|
+
* Execute a command by slug.
|
|
845
|
+
*
|
|
846
|
+
* Note: The backend POST /commands/execute uses a `command` field (the shell command
|
|
847
|
+
* string) and `userId`, not a `slug`. This maps ExecuteCommandInput.slug to `command`.
|
|
848
|
+
*/
|
|
849
|
+
async executeCommand(
|
|
850
|
+
input: ExecuteCommandInput
|
|
851
|
+
): Promise<{ status: string; result?: unknown }> {
|
|
852
|
+
const userId = input.userId ?? (await this.resolveUserId());
|
|
853
|
+
return this.request<{ status: string; result?: unknown }>(
|
|
854
|
+
"POST",
|
|
855
|
+
"/api/hub/commands/execute",
|
|
856
|
+
{
|
|
857
|
+
command: input.slug,
|
|
858
|
+
userId,
|
|
859
|
+
workspaceId: input.workspaceId ?? this.workspaceId,
|
|
860
|
+
...(input.parameters ? { parameters: input.parameters } : {}),
|
|
861
|
+
}
|
|
862
|
+
);
|
|
863
|
+
}
|
|
864
|
+
|
|
865
|
+
/**
|
|
866
|
+
* List agent users provisioned in the workspace.
|
|
867
|
+
*/
|
|
868
|
+
async listAgentUsers(workspaceId?: string): Promise<HubAgentUser[]> {
|
|
869
|
+
const wsId = workspaceId ?? this.workspaceId;
|
|
870
|
+
const params = new URLSearchParams();
|
|
871
|
+
if (wsId) params.set("workspaceId", wsId);
|
|
872
|
+
const qs = params.toString() ? `?${params}` : "";
|
|
873
|
+
const result = await this.request<
|
|
874
|
+
HubAgentUser[] | HubListResponse<HubAgentUser>
|
|
875
|
+
>("GET", `/api/hub/agent-users${qs}`);
|
|
876
|
+
return unwrapList(result);
|
|
877
|
+
}
|
|
878
|
+
|
|
879
|
+
// ─── Proactive posting ────────────────────────────────────────────────────
|
|
880
|
+
|
|
881
|
+
/**
|
|
882
|
+
* Post a proactive message to the user's personal channel.
|
|
883
|
+
* For AI-initiated insights and summaries. Rate-limited: 3/hour, 10/day.
|
|
884
|
+
* proactiveType must be one of: insight, suggestion, alert, nudge,
|
|
885
|
+
* morning_briefing, weekly_digest, health_check.
|
|
886
|
+
*/
|
|
887
|
+
async postProactive(
|
|
888
|
+
userId: string,
|
|
889
|
+
content: string,
|
|
890
|
+
options?: { workspaceId?: string; type?: string }
|
|
891
|
+
): Promise<{ id: string }> {
|
|
892
|
+
const wsId = options?.workspaceId ?? this.workspaceId;
|
|
893
|
+
if (!wsId) throw new Error("workspaceId is required for postProactive");
|
|
894
|
+
return this.request<{ id: string }>("POST", "/api/hub/proactive/post", {
|
|
895
|
+
userId,
|
|
896
|
+
workspaceId: wsId,
|
|
897
|
+
content,
|
|
898
|
+
proactiveType: options?.type ?? "insight",
|
|
899
|
+
});
|
|
900
|
+
}
|
|
901
|
+
|
|
902
|
+
// ─── Capture pipeline ─────────────────────────────────────────────────────
|
|
903
|
+
|
|
904
|
+
async captureStructure(input: {
|
|
905
|
+
text: string;
|
|
906
|
+
url?: string;
|
|
907
|
+
workspaceId?: string;
|
|
908
|
+
previousEntities?: CaptureProposal[];
|
|
909
|
+
}): Promise<CaptureStructureResponse> {
|
|
910
|
+
const userId = await this.resolveUserId();
|
|
911
|
+
return this.request<CaptureStructureResponse>(
|
|
912
|
+
"POST",
|
|
913
|
+
"/api/hub/capture/structure",
|
|
914
|
+
{
|
|
915
|
+
userId,
|
|
916
|
+
text: input.text,
|
|
917
|
+
url: input.url,
|
|
918
|
+
workspaceId: input.workspaceId ?? this.workspaceId,
|
|
919
|
+
previousEntities: input.previousEntities,
|
|
920
|
+
}
|
|
921
|
+
);
|
|
922
|
+
}
|
|
923
|
+
|
|
924
|
+
async captureExecute(
|
|
925
|
+
input: CaptureExecuteInput & { workspaceId?: string }
|
|
926
|
+
): Promise<CaptureExecuteResponse> {
|
|
927
|
+
const userId = await this.resolveUserId();
|
|
928
|
+
return this.request<CaptureExecuteResponse>(
|
|
929
|
+
"POST",
|
|
930
|
+
"/api/hub/capture/execute",
|
|
931
|
+
{
|
|
932
|
+
userId,
|
|
933
|
+
entities: input.entities,
|
|
934
|
+
relations: input.relations ?? [],
|
|
935
|
+
workspaceId: input.workspaceId ?? this.workspaceId,
|
|
936
|
+
}
|
|
937
|
+
);
|
|
938
|
+
}
|
|
939
|
+
|
|
940
|
+
// ─── Automations ───────────────────────────────────────────────────────────
|
|
941
|
+
|
|
942
|
+
/**
|
|
943
|
+
* List automations for the current user, optionally filtered by workspace and status.
|
|
944
|
+
*/
|
|
945
|
+
async listAutomations(options?: {
|
|
946
|
+
workspaceId?: string;
|
|
947
|
+
status?: AutomationStatus;
|
|
948
|
+
limit?: number;
|
|
949
|
+
}): Promise<HubAutomation[]> {
|
|
950
|
+
const userId = await this.resolveUserId();
|
|
951
|
+
const wsId = options?.workspaceId ?? this.workspaceId;
|
|
952
|
+
const params = new URLSearchParams({ userId });
|
|
953
|
+
if (wsId) params.set("workspaceId", wsId);
|
|
954
|
+
if (options?.status) params.set("status", options.status);
|
|
955
|
+
if (options?.limit) params.set("limit", String(options.limit));
|
|
956
|
+
const result = await this.request<
|
|
957
|
+
HubAutomation[] | HubListResponse<HubAutomation>
|
|
958
|
+
>("GET", `/api/hub/automations?${params}`);
|
|
959
|
+
return unwrapList(result);
|
|
960
|
+
}
|
|
961
|
+
|
|
962
|
+
/**
|
|
963
|
+
* Get a single automation by ID.
|
|
964
|
+
*/
|
|
965
|
+
async getAutomation(
|
|
966
|
+
automationId: string,
|
|
967
|
+
options?: { workspaceId?: string }
|
|
968
|
+
): Promise<HubAutomation> {
|
|
969
|
+
const userId = await this.resolveUserId();
|
|
970
|
+
const wsId = options?.workspaceId ?? this.workspaceId;
|
|
971
|
+
const params = new URLSearchParams({ userId });
|
|
972
|
+
if (wsId) params.set("workspaceId", wsId);
|
|
973
|
+
return this.request<HubAutomation>(
|
|
974
|
+
"GET",
|
|
975
|
+
`/api/hub/automations/${automationId}?${params}`
|
|
976
|
+
);
|
|
977
|
+
}
|
|
978
|
+
|
|
979
|
+
/**
|
|
980
|
+
* Create an automation. Defaults to status=draft.
|
|
981
|
+
* Use activateAutomation() to enable it.
|
|
982
|
+
*/
|
|
983
|
+
async createAutomation(input: CreateAutomationInput): Promise<HubAutomation> {
|
|
984
|
+
const userId = input.userId ?? (await this.resolveUserId());
|
|
985
|
+
const wsId = input.workspaceId ?? this.workspaceId;
|
|
986
|
+
return this.request<HubAutomation>("POST", "/api/hub/automations/create", {
|
|
987
|
+
...input,
|
|
988
|
+
userId,
|
|
989
|
+
workspaceId: wsId ?? null,
|
|
990
|
+
});
|
|
991
|
+
}
|
|
992
|
+
|
|
993
|
+
/**
|
|
994
|
+
* Update an automation's definition or metadata.
|
|
995
|
+
*/
|
|
996
|
+
async updateAutomation(
|
|
997
|
+
automationId: string,
|
|
998
|
+
input: UpdateAutomationInput
|
|
999
|
+
): Promise<HubAutomation> {
|
|
1000
|
+
const userId = input.userId ?? (await this.resolveUserId());
|
|
1001
|
+
const wsId = input.workspaceId ?? this.workspaceId;
|
|
1002
|
+
if (!wsId) throw new Error("workspaceId is required for updateAutomation");
|
|
1003
|
+
return this.request<HubAutomation>(
|
|
1004
|
+
"PATCH",
|
|
1005
|
+
`/api/hub/automations/${automationId}`,
|
|
1006
|
+
{ ...input, userId, workspaceId: wsId }
|
|
1007
|
+
);
|
|
1008
|
+
}
|
|
1009
|
+
|
|
1010
|
+
/**
|
|
1011
|
+
* Manually trigger an automation once with an optional payload.
|
|
1012
|
+
* Bypasses the automation's normal trigger config.
|
|
1013
|
+
*/
|
|
1014
|
+
async triggerAutomation(
|
|
1015
|
+
automationId: string,
|
|
1016
|
+
options?: {
|
|
1017
|
+
payload?: Record<string, unknown>;
|
|
1018
|
+
workspaceId?: string;
|
|
1019
|
+
}
|
|
1020
|
+
): Promise<{ status: string; runId?: string; result?: unknown }> {
|
|
1021
|
+
const userId = await this.resolveUserId();
|
|
1022
|
+
const wsId = options?.workspaceId ?? this.workspaceId;
|
|
1023
|
+
return this.request<{ status: string; runId?: string; result?: unknown }>(
|
|
1024
|
+
"POST",
|
|
1025
|
+
`/api/hub/automations/${automationId}/trigger`,
|
|
1026
|
+
{ userId, workspaceId: wsId ?? null, payload: options?.payload }
|
|
1027
|
+
);
|
|
1028
|
+
}
|
|
1029
|
+
|
|
1030
|
+
/**
|
|
1031
|
+
* Activate a draft or paused automation (sets status=active).
|
|
1032
|
+
*/
|
|
1033
|
+
async activateAutomation(
|
|
1034
|
+
automationId: string,
|
|
1035
|
+
options?: { workspaceId?: string }
|
|
1036
|
+
): Promise<HubAutomation> {
|
|
1037
|
+
const userId = await this.resolveUserId();
|
|
1038
|
+
const wsId = options?.workspaceId ?? this.workspaceId;
|
|
1039
|
+
if (!wsId)
|
|
1040
|
+
throw new Error("workspaceId is required for activateAutomation");
|
|
1041
|
+
return this.request<HubAutomation>(
|
|
1042
|
+
"POST",
|
|
1043
|
+
`/api/hub/automations/${automationId}/activate`,
|
|
1044
|
+
{ userId, workspaceId: wsId }
|
|
1045
|
+
);
|
|
1046
|
+
}
|
|
1047
|
+
|
|
1048
|
+
/**
|
|
1049
|
+
* Pause an active automation (sets status=paused).
|
|
1050
|
+
*/
|
|
1051
|
+
async pauseAutomation(
|
|
1052
|
+
automationId: string,
|
|
1053
|
+
options?: { workspaceId?: string }
|
|
1054
|
+
): Promise<HubAutomation> {
|
|
1055
|
+
const userId = await this.resolveUserId();
|
|
1056
|
+
const wsId = options?.workspaceId ?? this.workspaceId;
|
|
1057
|
+
if (!wsId) throw new Error("workspaceId is required for pauseAutomation");
|
|
1058
|
+
return this.request<HubAutomation>(
|
|
1059
|
+
"POST",
|
|
1060
|
+
`/api/hub/automations/${automationId}/pause`,
|
|
1061
|
+
{ userId, workspaceId: wsId }
|
|
1062
|
+
);
|
|
1063
|
+
}
|
|
1064
|
+
|
|
1065
|
+
// ─── Subscriptions / Reactions (Pulse) ────────────────────────────────────
|
|
1066
|
+
|
|
1067
|
+
/**
|
|
1068
|
+
* List the user-wide Pulse feed — the timestamp-sorted union of reactive events.
|
|
1069
|
+
* Call getSubscriptionFanout() on an individual event for its dense reactions[].
|
|
1070
|
+
*/
|
|
1071
|
+
async listSubscriptions(options?: {
|
|
1072
|
+
workspaceId?: string;
|
|
1073
|
+
kind?: ReactionKind;
|
|
1074
|
+
eventType?: string;
|
|
1075
|
+
lens?: ReactionLens;
|
|
1076
|
+
limit?: number;
|
|
1077
|
+
}): Promise<HubReactionEvent[]> {
|
|
1078
|
+
const wsId = options?.workspaceId ?? this.workspaceId;
|
|
1079
|
+
const params = new URLSearchParams();
|
|
1080
|
+
if (wsId) params.set("workspaceId", wsId);
|
|
1081
|
+
if (options?.kind) params.set("kind", options.kind);
|
|
1082
|
+
if (options?.eventType) params.set("eventType", options.eventType);
|
|
1083
|
+
if (options?.lens) params.set("lens", options.lens);
|
|
1084
|
+
if (options?.limit) params.set("limit", String(options.limit));
|
|
1085
|
+
const qs = params.toString() ? `?${params}` : "";
|
|
1086
|
+
const result = await this.request<
|
|
1087
|
+
HubReactionEvent[] | HubListResponse<HubReactionEvent>
|
|
1088
|
+
>("GET", `/api/hub/subscriptions${qs}`);
|
|
1089
|
+
return unwrapList(result);
|
|
1090
|
+
}
|
|
1091
|
+
|
|
1092
|
+
/**
|
|
1093
|
+
* Get the reaction fan-out for a single event — full reactions[] populated.
|
|
1094
|
+
*/
|
|
1095
|
+
async getSubscriptionFanout(
|
|
1096
|
+
eventId: string,
|
|
1097
|
+
options?: { lens?: ReactionLens }
|
|
1098
|
+
): Promise<HubReactionEvent> {
|
|
1099
|
+
const params = new URLSearchParams();
|
|
1100
|
+
if (options?.lens) params.set("lens", options.lens);
|
|
1101
|
+
const qs = params.toString() ? `?${params}` : "";
|
|
1102
|
+
return this.request<HubReactionEvent>(
|
|
1103
|
+
"GET",
|
|
1104
|
+
`/api/hub/subscriptions/${eventId}/fanout${qs}`
|
|
1105
|
+
);
|
|
1106
|
+
}
|
|
1107
|
+
|
|
1108
|
+
// ─── Notifications ─────────────────────────────────────────────────────────
|
|
1109
|
+
|
|
1110
|
+
/**
|
|
1111
|
+
* Persist a notification and emit notification:new to the frontend.
|
|
1112
|
+
* Use for IS-originated events (skill.triggered, agent actions, etc.).
|
|
1113
|
+
* Backend-originated notifications (vault, proposals) use NotificationService directly.
|
|
1114
|
+
*/
|
|
1115
|
+
async createNotification(
|
|
1116
|
+
input: CreateNotificationInput
|
|
1117
|
+
): Promise<{ id: string }> {
|
|
1118
|
+
return this.request<{ id: string }>(
|
|
1119
|
+
"POST",
|
|
1120
|
+
"/api/hub/notifications",
|
|
1121
|
+
input
|
|
1122
|
+
);
|
|
1123
|
+
}
|
|
1124
|
+
|
|
1125
|
+
// ─── Webhooks ──────────────────────────────────────────────────────────────
|
|
1126
|
+
|
|
1127
|
+
/**
|
|
1128
|
+
* List delivery log for a webhook subscription.
|
|
1129
|
+
* Powers the Reactions Health tab and replay flows.
|
|
1130
|
+
*/
|
|
1131
|
+
async getWebhookDeliveries(
|
|
1132
|
+
subscriptionId: string,
|
|
1133
|
+
options?: { limit?: number }
|
|
1134
|
+
): Promise<HubWebhookDelivery[]> {
|
|
1135
|
+
const params = new URLSearchParams();
|
|
1136
|
+
if (options?.limit) params.set("limit", String(options.limit));
|
|
1137
|
+
const qs = params.toString() ? `?${params}` : "";
|
|
1138
|
+
const result = await this.request<
|
|
1139
|
+
HubWebhookDelivery[] | HubListResponse<HubWebhookDelivery>
|
|
1140
|
+
>("GET", `/api/hub/webhooks/${subscriptionId}/deliveries${qs}`);
|
|
1141
|
+
return unwrapList(result);
|
|
1142
|
+
}
|
|
1143
|
+
}
|