@gdrl/kronos-lib 0.1.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/README.md +208 -0
- package/dist/index.d.mts +336 -0
- package/dist/index.d.ts +336 -0
- package/dist/index.js +748 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +713 -0
- package/dist/index.mjs.map +1 -0
- package/dist/react/index.d.mts +162 -0
- package/dist/react/index.d.ts +162 -0
- package/dist/react/index.js +869 -0
- package/dist/react/index.js.map +1 -0
- package/dist/react/index.mjs +847 -0
- package/dist/react/index.mjs.map +1 -0
- package/dist/types-Gt5ZdRxW.d.mts +364 -0
- package/dist/types-Gt5ZdRxW.d.ts +364 -0
- package/package.json +55 -0
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,713 @@
|
|
|
1
|
+
// src/errors.ts
|
|
2
|
+
var KronosError = class _KronosError extends Error {
|
|
3
|
+
constructor(message, status, code, body) {
|
|
4
|
+
super(message);
|
|
5
|
+
this.status = status;
|
|
6
|
+
this.code = code;
|
|
7
|
+
this.body = body;
|
|
8
|
+
this.name = "KronosError";
|
|
9
|
+
Object.setPrototypeOf(this, _KronosError.prototype);
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
var KronosBadRequestError = class _KronosBadRequestError extends KronosError {
|
|
13
|
+
constructor(message, body) {
|
|
14
|
+
super(message, 400, "bad_request", body);
|
|
15
|
+
this.name = "KronosBadRequestError";
|
|
16
|
+
Object.setPrototypeOf(this, _KronosBadRequestError.prototype);
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
var KronosUnauthorizedError = class _KronosUnauthorizedError extends KronosError {
|
|
20
|
+
constructor(message, body) {
|
|
21
|
+
super(message, 401, "unauthorized", body);
|
|
22
|
+
this.name = "KronosUnauthorizedError";
|
|
23
|
+
Object.setPrototypeOf(this, _KronosUnauthorizedError.prototype);
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
var KronosForbiddenError = class _KronosForbiddenError extends KronosError {
|
|
27
|
+
constructor(message, body) {
|
|
28
|
+
super(message, 403, "forbidden", body);
|
|
29
|
+
this.name = "KronosForbiddenError";
|
|
30
|
+
Object.setPrototypeOf(this, _KronosForbiddenError.prototype);
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
var KronosNotFoundError = class _KronosNotFoundError extends KronosError {
|
|
34
|
+
constructor(message, body) {
|
|
35
|
+
super(message, 404, "not_found", body);
|
|
36
|
+
this.name = "KronosNotFoundError";
|
|
37
|
+
Object.setPrototypeOf(this, _KronosNotFoundError.prototype);
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
var KronosServerError = class _KronosServerError extends KronosError {
|
|
41
|
+
constructor(message, status, body) {
|
|
42
|
+
super(message, status, "server_error", body);
|
|
43
|
+
this.name = "KronosServerError";
|
|
44
|
+
Object.setPrototypeOf(this, _KronosServerError.prototype);
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
// src/utils.ts
|
|
49
|
+
function generateIdempotencyKey() {
|
|
50
|
+
if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") {
|
|
51
|
+
return crypto.randomUUID();
|
|
52
|
+
}
|
|
53
|
+
return `idem_${Date.now()}_${Math.random().toString(36).slice(2, 11)}`;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// src/client.ts
|
|
57
|
+
var WORKER_URL = "https://api.kronos.ai";
|
|
58
|
+
var MASTRA_URL = "https://mastra.kronos.ai";
|
|
59
|
+
var INTERNAL_CONTEXT_GRAPH_SKILL_NAME = "__internal_context_graph__";
|
|
60
|
+
var KronosClient = class {
|
|
61
|
+
constructor(config) {
|
|
62
|
+
if (!config.apiKey) {
|
|
63
|
+
throw new Error("KronosClient: apiKey is required in constructor");
|
|
64
|
+
}
|
|
65
|
+
if (!config.appId) {
|
|
66
|
+
throw new Error("KronosClient: appId is required in constructor");
|
|
67
|
+
}
|
|
68
|
+
this.workerUrl = WORKER_URL.replace(/\/$/, "");
|
|
69
|
+
this.mastraUrl = MASTRA_URL.replace(/\/$/, "");
|
|
70
|
+
this.apiKey = config.apiKey;
|
|
71
|
+
this.appId = config.appId;
|
|
72
|
+
console.log("[KronosClient] \u2705 KronosClient initialized:", {
|
|
73
|
+
appId: this.appId,
|
|
74
|
+
workerUrl: this.workerUrl,
|
|
75
|
+
mastraUrl: this.mastraUrl,
|
|
76
|
+
hasApiKey: !!this.apiKey
|
|
77
|
+
});
|
|
78
|
+
this.healthCheck().catch(() => {
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Perform a health check to verify the client can connect to the Kronos worker
|
|
83
|
+
* This is called automatically during initialization
|
|
84
|
+
*/
|
|
85
|
+
async healthCheck() {
|
|
86
|
+
try {
|
|
87
|
+
const response = await fetch(`${this.workerUrl}/health`, {
|
|
88
|
+
method: "GET",
|
|
89
|
+
headers: {
|
|
90
|
+
"Content-Type": "application/json"
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
if (response.ok) {
|
|
94
|
+
const data = await response.json();
|
|
95
|
+
if (data.status === "ok") {
|
|
96
|
+
console.log("[KronosClient] \u2705 Health check passed - Client is connected and working");
|
|
97
|
+
} else {
|
|
98
|
+
console.warn("[KronosClient] \u26A0\uFE0F Health check returned unexpected status:", data);
|
|
99
|
+
}
|
|
100
|
+
} else {
|
|
101
|
+
console.warn(`[KronosClient] \u26A0\uFE0F Health check failed with status ${response.status}. Client may not be working correctly.`);
|
|
102
|
+
}
|
|
103
|
+
} catch (error) {
|
|
104
|
+
console.warn("[KronosClient] \u26A0\uFE0F Health check failed - Unable to connect to Kronos worker:", error.message);
|
|
105
|
+
console.warn("[KronosClient] Please verify that:");
|
|
106
|
+
console.warn("[KronosClient] 1. The worker URL is correct:", this.workerUrl);
|
|
107
|
+
console.warn("[KronosClient] 2. The worker is running and accessible");
|
|
108
|
+
console.warn("[KronosClient] 3. Network connectivity is available");
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
// ---------------------------------------------------------------------------
|
|
112
|
+
// Worker request helper (uses Bearer auth)
|
|
113
|
+
// ---------------------------------------------------------------------------
|
|
114
|
+
async workerFetch(path, options = {}) {
|
|
115
|
+
const { json, idempotencyKey, method } = options;
|
|
116
|
+
const headers = {
|
|
117
|
+
"Content-Type": "application/json",
|
|
118
|
+
Authorization: `Bearer ${this.apiKey}`
|
|
119
|
+
};
|
|
120
|
+
if (idempotencyKey) headers["Idempotency-Key"] = idempotencyKey;
|
|
121
|
+
const res = await fetch(`${this.workerUrl}${path}`, {
|
|
122
|
+
method: method ?? (json ? "POST" : "GET"),
|
|
123
|
+
headers,
|
|
124
|
+
body: json ? JSON.stringify(json) : void 0
|
|
125
|
+
});
|
|
126
|
+
return this.handleResponse(res);
|
|
127
|
+
}
|
|
128
|
+
// ---------------------------------------------------------------------------
|
|
129
|
+
// Mastra request helper (no Bearer auth per current API)
|
|
130
|
+
// ---------------------------------------------------------------------------
|
|
131
|
+
async mastraFetch(path, options = {}) {
|
|
132
|
+
const { json, method } = options;
|
|
133
|
+
const headers = {
|
|
134
|
+
"Content-Type": "application/json"
|
|
135
|
+
};
|
|
136
|
+
const res = await fetch(`${this.mastraUrl}${path}`, {
|
|
137
|
+
method: method ?? (json ? "POST" : "GET"),
|
|
138
|
+
headers,
|
|
139
|
+
body: json ? JSON.stringify(json) : void 0
|
|
140
|
+
});
|
|
141
|
+
return this.handleResponse(res);
|
|
142
|
+
}
|
|
143
|
+
async handleResponse(res) {
|
|
144
|
+
const text = await res.text();
|
|
145
|
+
let body = null;
|
|
146
|
+
try {
|
|
147
|
+
body = text ? JSON.parse(text) : null;
|
|
148
|
+
} catch {
|
|
149
|
+
body = text;
|
|
150
|
+
}
|
|
151
|
+
if (!res.ok) {
|
|
152
|
+
const errBody = body;
|
|
153
|
+
const message = errBody && typeof errBody.error === "string" ? errBody.error : `Request failed with status ${res.status}`;
|
|
154
|
+
switch (res.status) {
|
|
155
|
+
case 400:
|
|
156
|
+
throw new KronosBadRequestError(message, body);
|
|
157
|
+
case 401:
|
|
158
|
+
throw new KronosUnauthorizedError(message, body);
|
|
159
|
+
case 403:
|
|
160
|
+
throw new KronosForbiddenError(message, body);
|
|
161
|
+
case 404:
|
|
162
|
+
throw new KronosNotFoundError(message, body);
|
|
163
|
+
default:
|
|
164
|
+
if (res.status >= 500) {
|
|
165
|
+
throw new KronosServerError(message, res.status, body);
|
|
166
|
+
}
|
|
167
|
+
throw new KronosError(message, res.status, void 0, body);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
return body ?? null;
|
|
171
|
+
}
|
|
172
|
+
// ---------------------------------------------------------------------------
|
|
173
|
+
// Ingest API
|
|
174
|
+
// ---------------------------------------------------------------------------
|
|
175
|
+
/**
|
|
176
|
+
* Submit a document for ingestion.
|
|
177
|
+
* @param params - source_type, content, optional metadata, addToMemory, and idempotencyKey
|
|
178
|
+
* @returns ingest_id
|
|
179
|
+
*/
|
|
180
|
+
async ingest(params) {
|
|
181
|
+
const idempotencyKey = params.idempotencyKey ?? generateIdempotencyKey();
|
|
182
|
+
const body = {
|
|
183
|
+
app_id: this.appId,
|
|
184
|
+
tenant_user_id: params.tenant_user_id,
|
|
185
|
+
source_type: params.source_type,
|
|
186
|
+
priority: params.priority,
|
|
187
|
+
addToMemory: params.addToMemory,
|
|
188
|
+
content: params.content,
|
|
189
|
+
metadata: params.metadata
|
|
190
|
+
};
|
|
191
|
+
return this.workerFetch("/v1/ingest", {
|
|
192
|
+
method: "POST",
|
|
193
|
+
json: body,
|
|
194
|
+
idempotencyKey
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Get the status of an ingest.
|
|
199
|
+
*/
|
|
200
|
+
async getIngestStatus(ingestId) {
|
|
201
|
+
return this.workerFetch(`/v1/ingest/${ingestId}`, {
|
|
202
|
+
method: "GET"
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
// ---------------------------------------------------------------------------
|
|
206
|
+
// MCP Server API
|
|
207
|
+
// ---------------------------------------------------------------------------
|
|
208
|
+
/**
|
|
209
|
+
* Create an MCP server scoped to the given scopes.
|
|
210
|
+
* Use connectMCPServer() to get a short-lived access token for MCP auth.
|
|
211
|
+
*/
|
|
212
|
+
async createMCPServer(params) {
|
|
213
|
+
const body = {
|
|
214
|
+
app_id: this.appId,
|
|
215
|
+
tenant_user_id: params.tenant_user_id,
|
|
216
|
+
scope_ids: params.scope_ids ?? [],
|
|
217
|
+
name: params.name,
|
|
218
|
+
description: params.description,
|
|
219
|
+
options: {
|
|
220
|
+
exposeSkillsTool: params.options?.exposeSkillsTool ?? true,
|
|
221
|
+
exposeContextGraphTool: params.options?.exposeContextGraphTool ?? false
|
|
222
|
+
}
|
|
223
|
+
};
|
|
224
|
+
const res = await this.workerFetch("/v1/mcp-servers", { method: "POST", json: body });
|
|
225
|
+
return {
|
|
226
|
+
mcp_server_id: res.mcp_server_id,
|
|
227
|
+
url: res.url.startsWith("http") ? res.url : `${this.workerUrl}${res.url}`,
|
|
228
|
+
status: res.status,
|
|
229
|
+
name: res.name,
|
|
230
|
+
description: res.description,
|
|
231
|
+
scope_ids: res.scope_ids,
|
|
232
|
+
tool_capabilities: res.tool_capabilities ?? {
|
|
233
|
+
skills: true,
|
|
234
|
+
context_graph: false
|
|
235
|
+
}
|
|
236
|
+
};
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* List MCP servers for a user. Returns metadata only (no token).
|
|
240
|
+
*/
|
|
241
|
+
async listMCPServers(params) {
|
|
242
|
+
const path = `/v1/mcp-servers?${new URLSearchParams({
|
|
243
|
+
app_id: this.appId,
|
|
244
|
+
tenant_user_id: params.tenant_user_id
|
|
245
|
+
}).toString()}`;
|
|
246
|
+
return this.workerFetch(path, { method: "GET" });
|
|
247
|
+
}
|
|
248
|
+
/**
|
|
249
|
+
* Get MCP server details for a user by server ID. Returns metadata only (no token).
|
|
250
|
+
*/
|
|
251
|
+
async getMCPServer(params) {
|
|
252
|
+
const path = `/v1/mcp-servers/${params.serverId}?${new URLSearchParams({
|
|
253
|
+
app_id: this.appId,
|
|
254
|
+
tenant_user_id: params.tenant_user_id
|
|
255
|
+
}).toString()}`;
|
|
256
|
+
return this.workerFetch(path, { method: "GET" });
|
|
257
|
+
}
|
|
258
|
+
async updateMCPServer(params) {
|
|
259
|
+
const body = {
|
|
260
|
+
app_id: this.appId,
|
|
261
|
+
tenant_user_id: params.tenant_user_id,
|
|
262
|
+
tool_capabilities: params.tool_capabilities
|
|
263
|
+
};
|
|
264
|
+
return this.workerFetch(`/v1/mcp-servers/${params.serverId}`, {
|
|
265
|
+
method: "PUT",
|
|
266
|
+
json: body
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
* Connect to an MCP server by ID and receive a short-lived access token.
|
|
271
|
+
* Use the returned access_token as Bearer token when calling the MCP endpoint.
|
|
272
|
+
*/
|
|
273
|
+
async connectMCPServer(params) {
|
|
274
|
+
const body = {
|
|
275
|
+
app_id: this.appId,
|
|
276
|
+
tenant_user_id: params.tenant_user_id
|
|
277
|
+
};
|
|
278
|
+
const res = await this.workerFetch(
|
|
279
|
+
`/v1/mcp-servers/${params.serverId}/connect`,
|
|
280
|
+
{ method: "POST", json: body }
|
|
281
|
+
);
|
|
282
|
+
return {
|
|
283
|
+
...res,
|
|
284
|
+
url: res.url.startsWith("http") ? res.url : `${this.workerUrl}${res.url}`
|
|
285
|
+
};
|
|
286
|
+
}
|
|
287
|
+
/**
|
|
288
|
+
* Rotate the token for an MCP server. The new token is returned only on rotate.
|
|
289
|
+
*/
|
|
290
|
+
async rotateMCPServerToken(params) {
|
|
291
|
+
return this.workerFetch(
|
|
292
|
+
`/v1/mcp-servers/${params.serverId}/rotate-token`,
|
|
293
|
+
{
|
|
294
|
+
method: "POST",
|
|
295
|
+
json: { app_id: this.appId, tenant_user_id: params.tenant_user_id }
|
|
296
|
+
}
|
|
297
|
+
);
|
|
298
|
+
}
|
|
299
|
+
/**
|
|
300
|
+
* Get memory stats for a user (total claim count across entire memory graph).
|
|
301
|
+
* Worker caches the response for 5 minutes per user.
|
|
302
|
+
*/
|
|
303
|
+
async getMemoryStats(params) {
|
|
304
|
+
const path = `/v1/memory-stats?${new URLSearchParams({
|
|
305
|
+
app_id: this.appId,
|
|
306
|
+
tenant_user_id: params.tenant_user_id
|
|
307
|
+
}).toString()}`;
|
|
308
|
+
return this.workerFetch(path, { method: "GET" });
|
|
309
|
+
}
|
|
310
|
+
/**
|
|
311
|
+
* Read the current scratchsheet document for a user.
|
|
312
|
+
*/
|
|
313
|
+
async getScratchsheet(params) {
|
|
314
|
+
const path = `/v1/scratchsheet?${new URLSearchParams({
|
|
315
|
+
app_id: this.appId,
|
|
316
|
+
tenant_user_id: params.tenant_user_id
|
|
317
|
+
}).toString()}`;
|
|
318
|
+
return this.workerFetch(path, { method: "GET" });
|
|
319
|
+
}
|
|
320
|
+
/**
|
|
321
|
+
* List scopes for a user. Returns full scope details. Cached 5 min per user.
|
|
322
|
+
*/
|
|
323
|
+
async listScopes(params) {
|
|
324
|
+
const path = `/v1/scopes?${new URLSearchParams({
|
|
325
|
+
app_id: this.appId,
|
|
326
|
+
tenant_user_id: params.tenant_user_id
|
|
327
|
+
}).toString()}`;
|
|
328
|
+
return this.workerFetch(path, { method: "GET" });
|
|
329
|
+
}
|
|
330
|
+
/**
|
|
331
|
+
* Get a single scope by ID. Returns full scope details.
|
|
332
|
+
*/
|
|
333
|
+
async getScope(params) {
|
|
334
|
+
const path = `/v1/scopes/${encodeURIComponent(params.scope_id)}?${new URLSearchParams({
|
|
335
|
+
app_id: this.appId,
|
|
336
|
+
tenant_user_id: params.tenant_user_id
|
|
337
|
+
}).toString()}`;
|
|
338
|
+
return this.workerFetch(path, { method: "GET" });
|
|
339
|
+
}
|
|
340
|
+
// ---------------------------------------------------------------------------
|
|
341
|
+
// Scope API (Mastra)
|
|
342
|
+
// ---------------------------------------------------------------------------
|
|
343
|
+
/**
|
|
344
|
+
* Create a scope. A backfill job runs asynchronously to populate it.
|
|
345
|
+
* If spec.allowed_source_types is omitted, it defaults to ['all'].
|
|
346
|
+
* Uses the Worker so the scopes list cache is invalidated after create.
|
|
347
|
+
*/
|
|
348
|
+
async createScope(params) {
|
|
349
|
+
const spec = {
|
|
350
|
+
...params.spec,
|
|
351
|
+
allowed_source_types: params.spec.allowed_source_types ?? ["all"]
|
|
352
|
+
};
|
|
353
|
+
const body = {
|
|
354
|
+
app_id: this.appId,
|
|
355
|
+
tenant_user_id: params.tenant_user_id,
|
|
356
|
+
spec,
|
|
357
|
+
created_by: params.created_by,
|
|
358
|
+
description: params.description
|
|
359
|
+
};
|
|
360
|
+
return this.workerFetch("/v1/scopes", {
|
|
361
|
+
method: "POST",
|
|
362
|
+
json: body
|
|
363
|
+
});
|
|
364
|
+
}
|
|
365
|
+
/**
|
|
366
|
+
* Update a scope. Only provided fields are updated.
|
|
367
|
+
* If include_query, exclude_query, match_mode, time_range, or allowed_source_types change,
|
|
368
|
+
* a backfill job runs to recompute membership (response includes backfill_job_id).
|
|
369
|
+
* Otherwise only DB and Neo4j metadata are updated.
|
|
370
|
+
*/
|
|
371
|
+
async updateScope(params) {
|
|
372
|
+
const { scope_id, tenant_user_id, ...patch } = params;
|
|
373
|
+
const body = {};
|
|
374
|
+
if (patch.name !== void 0) body.name = patch.name;
|
|
375
|
+
if (patch.description !== void 0) body.description = patch.description;
|
|
376
|
+
if (patch.include_query !== void 0) body.include_query = patch.include_query;
|
|
377
|
+
if (patch.exclude_query !== void 0) body.exclude_query = patch.exclude_query;
|
|
378
|
+
if (patch.match_mode !== void 0) body.match_mode = patch.match_mode;
|
|
379
|
+
if (patch.time_range !== void 0) body.time_range = patch.time_range;
|
|
380
|
+
if (patch.allowed_source_types !== void 0) body.allowed_source_types = patch.allowed_source_types;
|
|
381
|
+
const path = `/v1/scopes/${encodeURIComponent(scope_id)}?${new URLSearchParams({
|
|
382
|
+
app_id: this.appId,
|
|
383
|
+
tenant_user_id
|
|
384
|
+
}).toString()}`;
|
|
385
|
+
return this.workerFetch(path, {
|
|
386
|
+
method: "PATCH",
|
|
387
|
+
json: body
|
|
388
|
+
});
|
|
389
|
+
}
|
|
390
|
+
/**
|
|
391
|
+
* Soft-delete a scope. Invalidates the scopes list cache for that user.
|
|
392
|
+
*/
|
|
393
|
+
async deleteScope(params) {
|
|
394
|
+
const path = `/v1/scopes/${encodeURIComponent(params.scope_id)}?${new URLSearchParams({
|
|
395
|
+
app_id: this.appId,
|
|
396
|
+
tenant_user_id: params.tenant_user_id
|
|
397
|
+
}).toString()}`;
|
|
398
|
+
return this.workerFetch(path, { method: "DELETE" });
|
|
399
|
+
}
|
|
400
|
+
// ---------------------------------------------------------------------------
|
|
401
|
+
// Webhook Verification API (Worker)
|
|
402
|
+
// ---------------------------------------------------------------------------
|
|
403
|
+
/**
|
|
404
|
+
* Verify a webhook secret for a trigger.
|
|
405
|
+
* This method calls the Kronos worker API to verify the webhook secret.
|
|
406
|
+
*
|
|
407
|
+
* @param params - app_id, tenant_user_id, trigger_id and received_secret to verify
|
|
408
|
+
* @returns true if secret is valid, false otherwise
|
|
409
|
+
*/
|
|
410
|
+
async verifyWebhookSecret(params) {
|
|
411
|
+
const body = {
|
|
412
|
+
trigger_id: params.trigger_id,
|
|
413
|
+
app_id: this.appId,
|
|
414
|
+
tenant_user_id: params.tenant_user_id,
|
|
415
|
+
received_secret: params.received_secret
|
|
416
|
+
};
|
|
417
|
+
try {
|
|
418
|
+
const response = await this.workerFetch("/v1/triggers/verify-webhook", {
|
|
419
|
+
method: "POST",
|
|
420
|
+
json: body
|
|
421
|
+
});
|
|
422
|
+
return response.valid;
|
|
423
|
+
} catch (error) {
|
|
424
|
+
console.error("[KronosClient] Webhook verification error:", error);
|
|
425
|
+
return false;
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
// ---------------------------------------------------------------------------
|
|
429
|
+
// Trigger Management API (Worker)
|
|
430
|
+
// ---------------------------------------------------------------------------
|
|
431
|
+
/**
|
|
432
|
+
* List triggers for a tenant user.
|
|
433
|
+
*/
|
|
434
|
+
async listTriggers(params) {
|
|
435
|
+
const query = new URLSearchParams({
|
|
436
|
+
app_id: this.appId,
|
|
437
|
+
tenant_user_id: params.tenant_user_id
|
|
438
|
+
});
|
|
439
|
+
return this.workerFetch(`/v1/triggers?${query.toString()}`, {
|
|
440
|
+
method: "GET"
|
|
441
|
+
});
|
|
442
|
+
}
|
|
443
|
+
/**
|
|
444
|
+
* Create a trigger.
|
|
445
|
+
* Supports NL creation (trigger_description) or manual creation (trigger_type/trigger_spec).
|
|
446
|
+
*
|
|
447
|
+
* @returns CreateTriggerResponse (accepted for NL, trigger_id for manual)
|
|
448
|
+
*/
|
|
449
|
+
async createTrigger(params) {
|
|
450
|
+
const body = {
|
|
451
|
+
app_id: this.appId,
|
|
452
|
+
tenant_user_id: params.tenant_user_id,
|
|
453
|
+
webhook_url: params.webhook_url,
|
|
454
|
+
webhook_secret: params.webhook_secret,
|
|
455
|
+
title: params.title,
|
|
456
|
+
action_description: params.action_description,
|
|
457
|
+
skill_id: params.skill_id,
|
|
458
|
+
skill_version_id: params.skill_version_id,
|
|
459
|
+
trigger_description: params.trigger_description,
|
|
460
|
+
trigger_type: params.trigger_type,
|
|
461
|
+
trigger_spec: params.trigger_spec,
|
|
462
|
+
next_run_at: params.next_run_at,
|
|
463
|
+
on_triggered: params.on_triggered,
|
|
464
|
+
metadata: params.metadata
|
|
465
|
+
};
|
|
466
|
+
return this.workerFetch("/v1/triggers", {
|
|
467
|
+
method: "POST",
|
|
468
|
+
json: body,
|
|
469
|
+
idempotencyKey: params.idempotencyKey
|
|
470
|
+
});
|
|
471
|
+
}
|
|
472
|
+
/**
|
|
473
|
+
* Get details for a specific trigger.
|
|
474
|
+
* Uses listTriggers and filters by trigger_id.
|
|
475
|
+
*/
|
|
476
|
+
async getTriggerDetails(params) {
|
|
477
|
+
const response = await this.listTriggers({ tenant_user_id: params.tenant_user_id });
|
|
478
|
+
const trigger = response.triggers.find((t) => t.trigger_id === params.trigger_id);
|
|
479
|
+
if (!trigger) {
|
|
480
|
+
throw new KronosNotFoundError(`Trigger not found: ${params.trigger_id}`);
|
|
481
|
+
}
|
|
482
|
+
return trigger;
|
|
483
|
+
}
|
|
484
|
+
/**
|
|
485
|
+
* Update a trigger.
|
|
486
|
+
* Updates the webhook_url, action_description, status, or metadata of an existing trigger.
|
|
487
|
+
*
|
|
488
|
+
* @param params - app_id, tenant_user_id, trigger_id and fields to update
|
|
489
|
+
* @returns UpdateTriggerResponse with success status
|
|
490
|
+
*/
|
|
491
|
+
async updateTrigger(params) {
|
|
492
|
+
const body = {
|
|
493
|
+
app_id: this.appId,
|
|
494
|
+
tenant_user_id: params.tenant_user_id,
|
|
495
|
+
webhook_url: params.webhook_url,
|
|
496
|
+
title: params.title,
|
|
497
|
+
trigger_type: params.trigger_type,
|
|
498
|
+
trigger_spec: params.trigger_spec,
|
|
499
|
+
next_run_at: params.next_run_at,
|
|
500
|
+
action_description: params.action_description,
|
|
501
|
+
skill_id: params.skill_id,
|
|
502
|
+
skill_version_id: params.skill_version_id,
|
|
503
|
+
on_triggered: params.on_triggered,
|
|
504
|
+
status: params.status,
|
|
505
|
+
metadata: params.metadata
|
|
506
|
+
};
|
|
507
|
+
return this.workerFetch(`/v1/triggers/${params.trigger_id}`, {
|
|
508
|
+
method: "PUT",
|
|
509
|
+
json: body
|
|
510
|
+
});
|
|
511
|
+
}
|
|
512
|
+
/**
|
|
513
|
+
* Delete a trigger.
|
|
514
|
+
*/
|
|
515
|
+
async deleteTrigger(params) {
|
|
516
|
+
const query = new URLSearchParams({
|
|
517
|
+
app_id: this.appId,
|
|
518
|
+
tenant_user_id: params.tenant_user_id
|
|
519
|
+
});
|
|
520
|
+
return this.workerFetch(
|
|
521
|
+
`/v1/triggers/${encodeURIComponent(params.trigger_id)}?${query.toString()}`,
|
|
522
|
+
{ method: "DELETE" }
|
|
523
|
+
);
|
|
524
|
+
}
|
|
525
|
+
// ---------------------------------------------------------------------------
|
|
526
|
+
// Frontend Tokens (Server-side only)
|
|
527
|
+
// ---------------------------------------------------------------------------
|
|
528
|
+
/**
|
|
529
|
+
* Issue a short-lived, user-scoped frontend token.
|
|
530
|
+
* The browser can use this token directly against the Kronos worker for
|
|
531
|
+
* allowed operations (e.g. trigger CRUD) without needing the app API key.
|
|
532
|
+
*
|
|
533
|
+
* This method must be called from a trusted server environment.
|
|
534
|
+
*/
|
|
535
|
+
async createFrontendToken(params) {
|
|
536
|
+
const res = await this.workerFetch(
|
|
537
|
+
"/v1/frontend-tokens",
|
|
538
|
+
{
|
|
539
|
+
method: "POST",
|
|
540
|
+
json: {
|
|
541
|
+
app_id: this.appId,
|
|
542
|
+
tenant_user_id: params.tenantUserId,
|
|
543
|
+
ttl_seconds: params.ttlSeconds,
|
|
544
|
+
ops: params.ops
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
);
|
|
548
|
+
return { token: res.token, expiresAt: res.expires_at };
|
|
549
|
+
}
|
|
550
|
+
// ---------------------------------------------------------------------------
|
|
551
|
+
// Skills API (Worker)
|
|
552
|
+
// ---------------------------------------------------------------------------
|
|
553
|
+
/**
|
|
554
|
+
* Canonical skills API with operation-based contract.
|
|
555
|
+
*/
|
|
556
|
+
async manageSkill(params) {
|
|
557
|
+
const idempotencyKey = params.operation === "create" || params.operation === "update" ? params.idempotencyKey : void 0;
|
|
558
|
+
return this.workerFetch("/v1/skills/manage", {
|
|
559
|
+
method: "POST",
|
|
560
|
+
json: {
|
|
561
|
+
...params,
|
|
562
|
+
app_id: this.appId
|
|
563
|
+
},
|
|
564
|
+
idempotencyKey
|
|
565
|
+
});
|
|
566
|
+
}
|
|
567
|
+
async resolveContextGraphSkillId(tenantUserId) {
|
|
568
|
+
const listed = await this.manageSkill({
|
|
569
|
+
operation: "list",
|
|
570
|
+
tenant_user_id: tenantUserId,
|
|
571
|
+
include_internal: true
|
|
572
|
+
});
|
|
573
|
+
const match = (listed.skills ?? []).find(
|
|
574
|
+
(skill) => skill.name === INTERNAL_CONTEXT_GRAPH_SKILL_NAME
|
|
575
|
+
);
|
|
576
|
+
return match?.skill_id ?? null;
|
|
577
|
+
}
|
|
578
|
+
parseContextGraphProcedures(files) {
|
|
579
|
+
return files.filter((file) => file.path.startsWith("procedures/") && file.path.endsWith(".md")).map((file) => {
|
|
580
|
+
const content = file.content ?? "";
|
|
581
|
+
const normalized = content.replace(/\r\n/g, "\n");
|
|
582
|
+
if (normalized.startsWith("---\n")) {
|
|
583
|
+
const endIndex = normalized.indexOf("\n---\n", 4);
|
|
584
|
+
if (endIndex !== -1) {
|
|
585
|
+
const frontmatter = normalized.slice(4, endIndex);
|
|
586
|
+
const metadata = {};
|
|
587
|
+
for (const line of frontmatter.split("\n")) {
|
|
588
|
+
const separatorIndex = line.indexOf(":");
|
|
589
|
+
if (separatorIndex === -1) continue;
|
|
590
|
+
const key = line.slice(0, separatorIndex).trim();
|
|
591
|
+
const value = line.slice(separatorIndex + 1).trim();
|
|
592
|
+
if (key && value) {
|
|
593
|
+
metadata[key] = value;
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
return {
|
|
597
|
+
title: metadata.title || (file.path.split("/").pop() ?? file.path),
|
|
598
|
+
description: metadata.description || "",
|
|
599
|
+
path: file.path
|
|
600
|
+
};
|
|
601
|
+
}
|
|
602
|
+
}
|
|
603
|
+
const lines = content.split(/\r?\n/);
|
|
604
|
+
const titleLine = lines.find((line) => line.startsWith("# "));
|
|
605
|
+
const title = titleLine ? titleLine.replace(/^#\s+/, "").trim() : file.path.split("/").pop() ?? file.path;
|
|
606
|
+
const description = lines.slice(1).map((line) => line.trim()).find((line) => line.length > 0 && !line.startsWith("#")) ?? "";
|
|
607
|
+
return {
|
|
608
|
+
title,
|
|
609
|
+
description,
|
|
610
|
+
path: file.path
|
|
611
|
+
};
|
|
612
|
+
}).sort((a, b) => a.path.localeCompare(b.path));
|
|
613
|
+
}
|
|
614
|
+
async getContextGraph(params) {
|
|
615
|
+
const skillId = await this.resolveContextGraphSkillId(params.tenant_user_id);
|
|
616
|
+
if (!skillId) {
|
|
617
|
+
return {
|
|
618
|
+
skill_id: null,
|
|
619
|
+
procedures: []
|
|
620
|
+
};
|
|
621
|
+
}
|
|
622
|
+
const treeResult = await this.manageSkill({
|
|
623
|
+
operation: "read",
|
|
624
|
+
tenant_user_id: params.tenant_user_id,
|
|
625
|
+
skill_id: skillId,
|
|
626
|
+
mode: "tree"
|
|
627
|
+
});
|
|
628
|
+
const procedurePaths = (treeResult.tree ?? []).filter((node) => node.kind === "file" && node.path.startsWith("procedures/") && node.path.endsWith(".md")).map((node) => node.path);
|
|
629
|
+
if (procedurePaths.length === 0) {
|
|
630
|
+
return {
|
|
631
|
+
skill_id: skillId,
|
|
632
|
+
procedures: []
|
|
633
|
+
};
|
|
634
|
+
}
|
|
635
|
+
const filesResult = await this.manageSkill({
|
|
636
|
+
operation: "read",
|
|
637
|
+
tenant_user_id: params.tenant_user_id,
|
|
638
|
+
skill_id: skillId,
|
|
639
|
+
mode: "files",
|
|
640
|
+
files: procedurePaths,
|
|
641
|
+
fileContent: "full"
|
|
642
|
+
});
|
|
643
|
+
return {
|
|
644
|
+
skill_id: skillId,
|
|
645
|
+
procedures: this.parseContextGraphProcedures(filesResult.files ?? [])
|
|
646
|
+
};
|
|
647
|
+
}
|
|
648
|
+
async readContextGraph(params) {
|
|
649
|
+
const skillId = await this.resolveContextGraphSkillId(params.tenant_user_id);
|
|
650
|
+
const requestedPath = params.path?.trim() || "SKILL.md";
|
|
651
|
+
if (!skillId) {
|
|
652
|
+
return {
|
|
653
|
+
skill_id: null,
|
|
654
|
+
path: requestedPath,
|
|
655
|
+
content: null
|
|
656
|
+
};
|
|
657
|
+
}
|
|
658
|
+
if (requestedPath === "SKILL.md") {
|
|
659
|
+
const result2 = await this.manageSkill({
|
|
660
|
+
operation: "read",
|
|
661
|
+
tenant_user_id: params.tenant_user_id,
|
|
662
|
+
skill_id: skillId,
|
|
663
|
+
mode: "instructions"
|
|
664
|
+
});
|
|
665
|
+
return {
|
|
666
|
+
skill_id: skillId,
|
|
667
|
+
path: "SKILL.md",
|
|
668
|
+
content: result2.instructions ?? null
|
|
669
|
+
};
|
|
670
|
+
}
|
|
671
|
+
const result = await this.manageSkill({
|
|
672
|
+
operation: "read",
|
|
673
|
+
tenant_user_id: params.tenant_user_id,
|
|
674
|
+
skill_id: skillId,
|
|
675
|
+
mode: "files",
|
|
676
|
+
files: [requestedPath],
|
|
677
|
+
fileContent: "full"
|
|
678
|
+
});
|
|
679
|
+
const file = (result.files ?? []).find((entry) => entry.path === requestedPath);
|
|
680
|
+
return {
|
|
681
|
+
skill_id: skillId,
|
|
682
|
+
path: requestedPath,
|
|
683
|
+
content: file?.content ?? null
|
|
684
|
+
};
|
|
685
|
+
}
|
|
686
|
+
};
|
|
687
|
+
|
|
688
|
+
// src/webhook-verification.ts
|
|
689
|
+
function verifyWebhookSecret(receivedSecret, storedSecret) {
|
|
690
|
+
if (!receivedSecret || !storedSecret) {
|
|
691
|
+
return false;
|
|
692
|
+
}
|
|
693
|
+
if (receivedSecret.length !== storedSecret.length) {
|
|
694
|
+
return false;
|
|
695
|
+
}
|
|
696
|
+
let result = 0;
|
|
697
|
+
for (let i = 0; i < receivedSecret.length; i++) {
|
|
698
|
+
result |= receivedSecret.charCodeAt(i) ^ storedSecret.charCodeAt(i);
|
|
699
|
+
}
|
|
700
|
+
return result === 0;
|
|
701
|
+
}
|
|
702
|
+
export {
|
|
703
|
+
KronosBadRequestError,
|
|
704
|
+
KronosClient,
|
|
705
|
+
KronosError,
|
|
706
|
+
KronosForbiddenError,
|
|
707
|
+
KronosNotFoundError,
|
|
708
|
+
KronosServerError,
|
|
709
|
+
KronosUnauthorizedError,
|
|
710
|
+
generateIdempotencyKey,
|
|
711
|
+
verifyWebhookSecret
|
|
712
|
+
};
|
|
713
|
+
//# sourceMappingURL=index.mjs.map
|