@erdoai/cli 0.37.0 → 0.38.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 (2) hide show
  1. package/dist/index.js +156 -0
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -523,6 +523,28 @@ var ErdoClient = class {
523
523
  `/v1/activity/feed${qs ? `?${qs}` : ""}`
524
524
  );
525
525
  }
526
+ // --- org history ledger ---
527
+ // The append-only org history: consequential actions (approvals decided, pages
528
+ // published, permissions granted/revoked, flag changes, membership changes)
529
+ // with full actor attribution. Read-only, newest-first. since/until are
530
+ // RFC3339; verbs and resource are pre-resolved by the caller.
531
+ listActivityHistory(params) {
532
+ const q = new URLSearchParams();
533
+ if (params?.since) q.set("since", params.since);
534
+ if (params?.until) q.set("until", params.until);
535
+ if (params?.verbs) q.set("verbs", params.verbs);
536
+ if (params?.resource_type) q.set("resource_type", params.resource_type);
537
+ if (params?.resource_id) q.set("resource_id", params.resource_id);
538
+ if (params?.actor_user_id) q.set("actor_user_id", params.actor_user_id);
539
+ if (params?.actor_kind) q.set("actor_kind", params.actor_kind);
540
+ if (params?.limit) q.set("limit", String(params.limit));
541
+ if (params?.offset) q.set("offset", String(params.offset));
542
+ const qs = q.toString();
543
+ return this.request(
544
+ "GET",
545
+ `/v1/activity/history${qs ? `?${qs}` : ""}`
546
+ );
547
+ }
526
548
  // --- knowledge review queue ---
527
549
  listReviewItems(params) {
528
550
  const q = new URLSearchParams();
@@ -2398,6 +2420,140 @@ program.command("activity").alias("feed").description(
2398
2420
  }
2399
2421
  }
2400
2422
  );
2423
+ function parseWhen(v) {
2424
+ const dur = /^(\d+)\s*([smhdw])$/.exec(v.trim());
2425
+ if (dur) {
2426
+ const n = parseInt(dur[1], 10);
2427
+ const unitMs = {
2428
+ s: 1e3,
2429
+ m: 6e4,
2430
+ h: 36e5,
2431
+ d: 864e5,
2432
+ w: 6048e5
2433
+ };
2434
+ return new Date(Date.now() - n * unitMs[dur[2]]).toISOString();
2435
+ }
2436
+ const t = Date.parse(v);
2437
+ if (Number.isNaN(t)) {
2438
+ throw new Error(`can't parse time "${v}" \u2014 use a duration like 2h/3d or an RFC3339 timestamp`);
2439
+ }
2440
+ return new Date(t).toISOString();
2441
+ }
2442
+ function shortId(id) {
2443
+ return id.length > 10 ? `${id.slice(0, 8)}\u2026` : id;
2444
+ }
2445
+ function hhmm(iso) {
2446
+ const d = new Date(iso);
2447
+ if (Number.isNaN(d.getTime())) return "??:??";
2448
+ const h = String(d.getHours()).padStart(2, "0");
2449
+ const m = String(d.getMinutes()).padStart(2, "0");
2450
+ return `${h}:${m}`;
2451
+ }
2452
+ function historyActor(e) {
2453
+ switch (e.actor_kind) {
2454
+ case "agent":
2455
+ return e.agent_key || "an agent";
2456
+ case "system":
2457
+ return "Erdo (system)";
2458
+ case "user":
2459
+ return e.actor_user_id ? shortId(e.actor_user_id) : "a user";
2460
+ case "api_token":
2461
+ return "an API token";
2462
+ case "scoped_token":
2463
+ return "a scoped token";
2464
+ default:
2465
+ return e.actor_kind || "someone";
2466
+ }
2467
+ }
2468
+ function historyResource(e) {
2469
+ if (e.resource_title) return e.resource_title;
2470
+ if (e.resource_type && e.resource_id) return `${e.resource_type}/${shortId(e.resource_id)}`;
2471
+ return e.resource_type || "";
2472
+ }
2473
+ function historySentence(e) {
2474
+ const res = historyResource(e);
2475
+ const summary = (e.summary || "").replace(/\s+/g, " ").trim();
2476
+ const head = [historyActor(e), summary].filter(Boolean).join(" ");
2477
+ return res ? `${head} \u2014 ${res}` : head;
2478
+ }
2479
+ function historyAuthMarker(e) {
2480
+ if (e.authorized_by_approval_id) {
2481
+ return ` \xB7 authorized by approval ${shortId(e.authorized_by_approval_id)}`;
2482
+ }
2483
+ if (e.actor_kind === "agent" || e.actor_kind === "system") return " \xB7 autonomous";
2484
+ return "";
2485
+ }
2486
+ function renderHistory(events) {
2487
+ const order = [];
2488
+ const groups = /* @__PURE__ */ new Map();
2489
+ let standalone = 0;
2490
+ for (const e of events) {
2491
+ const key = e.authorized_by_approval_id || e.run_id || `__solo_${standalone++}`;
2492
+ if (!groups.has(key)) {
2493
+ groups.set(key, []);
2494
+ order.push(key);
2495
+ }
2496
+ groups.get(key).push(e);
2497
+ }
2498
+ for (const key of order) {
2499
+ const group = groups.get(key);
2500
+ const ordered = [...group].sort(
2501
+ (a, b) => Date.parse(a.occurred_at) - Date.parse(b.occurred_at)
2502
+ );
2503
+ const [head, ...rest] = ordered;
2504
+ console.log(`${hhmm(head.occurred_at)} ${historySentence(head)}${historyAuthMarker(head)}`);
2505
+ for (const c of rest) {
2506
+ console.log(` ${hhmm(c.occurred_at)} ${historySentence(c)}`);
2507
+ }
2508
+ }
2509
+ }
2510
+ program.command("history").description("The org history \u2014 who did what, when, under whose authority").option("--since <when>", "start of the window: a duration (2h, 3d) or RFC3339 (default 24h)", "24h").option("--until <when>", "end of the window: a duration or RFC3339 timestamp").option("--actor-kind <kind>", "user | agent | system | api_token | scoped_token").option("--actor <user-id>", "only actions by this user id").option("--resource <type:id>", "only actions on this resource, e.g. page:<id>").option("--verb <v>", "comma-separated verbs, e.g. approval.decided,page.published").option("-n, --limit <n>", "max events (default 50, max 200)", (v) => parseInt(v, 10)).option("--json", "raw events JSON").action(
2511
+ async (opts) => {
2512
+ try {
2513
+ const params = {};
2514
+ params.since = parseWhen(opts.since || "24h");
2515
+ if (opts.until) params.until = parseWhen(opts.until);
2516
+ if (opts.actorKind) params.actor_kind = opts.actorKind;
2517
+ if (opts.actor) params.actor_user_id = opts.actor;
2518
+ if (opts.verb) params.verbs = opts.verb;
2519
+ if (opts.limit) params.limit = opts.limit;
2520
+ if (opts.resource) {
2521
+ const idx = opts.resource.indexOf(":");
2522
+ if (idx === -1) {
2523
+ throw new Error(`--resource must be <type:id>, e.g. page:<id> (got "${opts.resource}")`);
2524
+ }
2525
+ params.resource_type = opts.resource.slice(0, idx);
2526
+ params.resource_id = opts.resource.slice(idx + 1);
2527
+ }
2528
+ const res = await new ErdoClient().listActivityHistory(params);
2529
+ if (opts.json) {
2530
+ print(res);
2531
+ return;
2532
+ }
2533
+ const events = res.events ?? [];
2534
+ if (events.length === 0) {
2535
+ const filters = [
2536
+ `since ${opts.since || "24h"}`,
2537
+ opts.until ? `until ${opts.until}` : "",
2538
+ opts.actorKind ? `actor-kind ${opts.actorKind}` : "",
2539
+ opts.actor ? `actor ${opts.actor}` : "",
2540
+ opts.resource ? `resource ${opts.resource}` : "",
2541
+ opts.verb ? `verb ${opts.verb}` : ""
2542
+ ].filter(Boolean).join(", ");
2543
+ console.log(`No history for: ${filters}.`);
2544
+ console.log(
2545
+ "History is append-only and starts when the ledger was enabled for this org \u2014 earlier actions aren't recorded here."
2546
+ );
2547
+ return;
2548
+ }
2549
+ renderHistory(events);
2550
+ console.log(`
2551
+ ${events.length} event${events.length === 1 ? "" : "s"}.`);
2552
+ } catch (e) {
2553
+ fail(e);
2554
+ }
2555
+ }
2556
+ );
2401
2557
  var reviewsCmd = program.command("reviews").description(
2402
2558
  "The Knowledge Review queue \u2014 knowledge patches the critic proposes, investigations, and deduplicated failure signals awaiting a human decision"
2403
2559
  );
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@erdoai/cli",
3
- "version": "0.37.0",
3
+ "version": "0.38.0",
4
4
  "description": "Erdo CLI — drive datasets, pages, and evals from the terminal or CI",
5
5
  "type": "module",
6
6
  "bin": {