@dipertq/dsh-openviking-status 0.2.2 → 0.3.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 +1 -1
- package/lib/client.cjs +656 -12
- package/lib/client.cjs.map +1 -1
- package/lib/client.d.cts +219 -3
- package/lib/client.js +656 -12
- package/lib/client.js.map +1 -1
- package/lib/index.js +159 -1
- package/lib/index.js.map +1 -1
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -899,9 +899,49 @@ function getCandidateSessionIds(sessionId) {
|
|
|
899
899
|
}
|
|
900
900
|
return Array.from(new Set(candidates));
|
|
901
901
|
}
|
|
902
|
+
var TASKS_LIMIT_CAP = 200;
|
|
903
|
+
var SESSION_COMMIT_TASK_TYPE = "session_commit";
|
|
904
|
+
function clampLimit(raw, fallback = TASKS_LIMIT_CAP) {
|
|
905
|
+
const n = typeof raw === "string" ? Number.parseInt(raw, 10) : Number(raw);
|
|
906
|
+
if (!Number.isFinite(n) || n <= 0) return fallback;
|
|
907
|
+
return Math.min(TASKS_LIMIT_CAP, Math.max(1, Math.floor(n)));
|
|
908
|
+
}
|
|
909
|
+
function extractTaskId(body) {
|
|
910
|
+
if (!body || typeof body !== "object") return null;
|
|
911
|
+
const containers = [
|
|
912
|
+
body,
|
|
913
|
+
body.result,
|
|
914
|
+
body.data
|
|
915
|
+
];
|
|
916
|
+
for (const c of containers) {
|
|
917
|
+
if (!c || typeof c !== "object") continue;
|
|
918
|
+
const id = c.task_id ?? c.id;
|
|
919
|
+
if (typeof id === "string" && id.trim()) return id.trim();
|
|
920
|
+
}
|
|
921
|
+
return null;
|
|
922
|
+
}
|
|
923
|
+
async function resolveResourceId(endpoint, headers, sessionId, cache) {
|
|
924
|
+
const key = sessionId.trim();
|
|
925
|
+
const cached = cache?.get(key);
|
|
926
|
+
if (cached) return cached;
|
|
927
|
+
for (const candidateId of getCandidateSessionIds(sessionId)) {
|
|
928
|
+
const daemonRes = await fetch(
|
|
929
|
+
`${endpoint}/api/v1/sessions/${encodeURIComponent(candidateId)}`,
|
|
930
|
+
{ method: "GET", headers }
|
|
931
|
+
).catch(() => null);
|
|
932
|
+
if (!daemonRes) continue;
|
|
933
|
+
if (daemonRes.status === 404) continue;
|
|
934
|
+
if (daemonRes.ok) {
|
|
935
|
+
cache?.set(key, candidateId);
|
|
936
|
+
return candidateId;
|
|
937
|
+
}
|
|
938
|
+
}
|
|
939
|
+
return null;
|
|
940
|
+
}
|
|
902
941
|
function apply(ctx, config) {
|
|
903
942
|
let currentSettings = () => config ?? {};
|
|
904
943
|
let settingsService = null;
|
|
944
|
+
const resourceIdCache = /* @__PURE__ */ new Map();
|
|
905
945
|
ctx.inject?.(["settings"], (settingsCtx) => {
|
|
906
946
|
settingsService = settingsCtx.settings;
|
|
907
947
|
settingsCtx.settings?.installSection(
|
|
@@ -1176,7 +1216,13 @@ function apply(ctx, config) {
|
|
|
1176
1216
|
sendJson(res, 200, { ok: false, error: String(errorMsg) });
|
|
1177
1217
|
return;
|
|
1178
1218
|
}
|
|
1179
|
-
|
|
1219
|
+
const okBody = await daemonRes.json().catch(() => null);
|
|
1220
|
+
const taskId = extractTaskId(okBody);
|
|
1221
|
+
sendJson(res, 200, {
|
|
1222
|
+
ok: true,
|
|
1223
|
+
...taskId ? { task_id: taskId } : {},
|
|
1224
|
+
resource_id: candidateId
|
|
1225
|
+
});
|
|
1180
1226
|
return;
|
|
1181
1227
|
}
|
|
1182
1228
|
sendJson(res, 200, {
|
|
@@ -1189,6 +1235,118 @@ function apply(ctx, config) {
|
|
|
1189
1235
|
}
|
|
1190
1236
|
});
|
|
1191
1237
|
}, "openviking-status: session commit proxy route");
|
|
1238
|
+
ctx.effect?.(() => {
|
|
1239
|
+
return ctx.webServer?.register({
|
|
1240
|
+
kind: "exact",
|
|
1241
|
+
path: `${API_PREFIX}/tasks`,
|
|
1242
|
+
handler: async (req, res) => {
|
|
1243
|
+
try {
|
|
1244
|
+
const url = new URL(req.url || "/", "http://localhost");
|
|
1245
|
+
const sessionId = url.searchParams.get("session");
|
|
1246
|
+
if (!sessionId || !sessionId.trim()) {
|
|
1247
|
+
sendJson(res, 400, { status: "missing", error: "Missing session" });
|
|
1248
|
+
return;
|
|
1249
|
+
}
|
|
1250
|
+
const effective = resolveEffective();
|
|
1251
|
+
const headers = getHeaders(effective.apiKey);
|
|
1252
|
+
const resourceId = await resolveResourceId(
|
|
1253
|
+
effective.endpoint,
|
|
1254
|
+
headers,
|
|
1255
|
+
sessionId.trim(),
|
|
1256
|
+
resourceIdCache
|
|
1257
|
+
);
|
|
1258
|
+
if (!resourceId) {
|
|
1259
|
+
sendJson(res, 200, { status: "missing", tasks: [] });
|
|
1260
|
+
return;
|
|
1261
|
+
}
|
|
1262
|
+
const limit = clampLimit(url.searchParams.get("limit"));
|
|
1263
|
+
const query = new URLSearchParams({
|
|
1264
|
+
resource_id: resourceId,
|
|
1265
|
+
task_type: SESSION_COMMIT_TASK_TYPE,
|
|
1266
|
+
limit: String(limit)
|
|
1267
|
+
});
|
|
1268
|
+
const daemonRes = await fetch(
|
|
1269
|
+
`${effective.endpoint}/api/v1/tasks?${query.toString()}`,
|
|
1270
|
+
{ method: "GET", headers }
|
|
1271
|
+
).catch(() => null);
|
|
1272
|
+
if (!daemonRes) {
|
|
1273
|
+
sendJson(res, 200, { status: "unreachable", tasks: [] });
|
|
1274
|
+
return;
|
|
1275
|
+
}
|
|
1276
|
+
if (daemonRes.status === 401 || daemonRes.status === 403) {
|
|
1277
|
+
sendJson(res, 200, { status: "unauthorized", tasks: [] });
|
|
1278
|
+
return;
|
|
1279
|
+
}
|
|
1280
|
+
if (!daemonRes.ok) {
|
|
1281
|
+
sendJson(res, 200, {
|
|
1282
|
+
status: "error",
|
|
1283
|
+
detail: `HTTP ${daemonRes.status}`,
|
|
1284
|
+
tasks: []
|
|
1285
|
+
});
|
|
1286
|
+
return;
|
|
1287
|
+
}
|
|
1288
|
+
const data = await daemonRes.json().catch(() => ({}));
|
|
1289
|
+
const items = data?.items ?? data?.tasks ?? data?.result?.items ?? (Array.isArray(data) ? data : []);
|
|
1290
|
+
sendJson(res, 200, {
|
|
1291
|
+
status: "ok",
|
|
1292
|
+
resource_id: resourceId,
|
|
1293
|
+
tasks: Array.isArray(items) ? items : []
|
|
1294
|
+
});
|
|
1295
|
+
} catch (err) {
|
|
1296
|
+
sendJson(res, 200, { status: "unreachable", detail: String(err) });
|
|
1297
|
+
}
|
|
1298
|
+
}
|
|
1299
|
+
});
|
|
1300
|
+
}, "openviking-status: tasks proxy route");
|
|
1301
|
+
ctx.effect?.(() => {
|
|
1302
|
+
return ctx.webServer?.register({
|
|
1303
|
+
kind: "exact",
|
|
1304
|
+
path: `${API_PREFIX}/task`,
|
|
1305
|
+
handler: async (req, res) => {
|
|
1306
|
+
try {
|
|
1307
|
+
const url = new URL(req.url || "/", "http://localhost");
|
|
1308
|
+
const taskId = url.searchParams.get("id");
|
|
1309
|
+
if (!taskId || !taskId.trim()) {
|
|
1310
|
+
sendJson(res, 400, { status: "missing", error: "Missing id" });
|
|
1311
|
+
return;
|
|
1312
|
+
}
|
|
1313
|
+
const withEvents = url.searchParams.get("events") === "1" || url.searchParams.get("events") === "true";
|
|
1314
|
+
const effective = resolveEffective();
|
|
1315
|
+
const query = withEvents ? "?include_events=true" : "";
|
|
1316
|
+
const daemonRes = await fetch(
|
|
1317
|
+
`${effective.endpoint}/api/v1/tasks/${encodeURIComponent(
|
|
1318
|
+
taskId.trim()
|
|
1319
|
+
)}${query}`,
|
|
1320
|
+
{ method: "GET", headers: getHeaders(effective.apiKey) }
|
|
1321
|
+
).catch(() => null);
|
|
1322
|
+
if (!daemonRes) {
|
|
1323
|
+
sendJson(res, 200, { status: "unreachable" });
|
|
1324
|
+
return;
|
|
1325
|
+
}
|
|
1326
|
+
if (daemonRes.status === 404) {
|
|
1327
|
+
sendJson(res, 200, { status: "missing" });
|
|
1328
|
+
return;
|
|
1329
|
+
}
|
|
1330
|
+
if (daemonRes.status === 401 || daemonRes.status === 403) {
|
|
1331
|
+
sendJson(res, 200, { status: "unauthorized" });
|
|
1332
|
+
return;
|
|
1333
|
+
}
|
|
1334
|
+
if (!daemonRes.ok) {
|
|
1335
|
+
sendJson(res, 200, {
|
|
1336
|
+
status: "error",
|
|
1337
|
+
detail: `HTTP ${daemonRes.status}`
|
|
1338
|
+
});
|
|
1339
|
+
return;
|
|
1340
|
+
}
|
|
1341
|
+
const data = await daemonRes.json().catch(() => ({}));
|
|
1342
|
+
const task = data?.result ?? data?.data ?? data;
|
|
1343
|
+
sendJson(res, 200, { status: "ok", task });
|
|
1344
|
+
} catch (err) {
|
|
1345
|
+
sendJson(res, 200, { status: "unreachable", detail: String(err) });
|
|
1346
|
+
}
|
|
1347
|
+
}
|
|
1348
|
+
});
|
|
1349
|
+
}, "openviking-status: task detail proxy route");
|
|
1192
1350
|
}
|
|
1193
1351
|
export {
|
|
1194
1352
|
Config,
|