@primusclaw/personal-agent-runtime 0.4.0 → 0.5.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/index.js CHANGED
@@ -2517,7 +2517,8 @@ import {
2517
2517
 
2518
2518
  // packages/runtime/src/sqlite/approval-repository.ts
2519
2519
  import {
2520
- APPROVAL_TTL_SECONDS
2520
+ APPROVAL_TTL_SECONDS,
2521
+ QUESTION_TOOL
2521
2522
  } from "@primusclaw/personal-agent";
2522
2523
 
2523
2524
  // packages/runtime/src/sqlite/py-json.ts
@@ -2568,6 +2569,15 @@ function storedObject(text3) {
2568
2569
  }
2569
2570
 
2570
2571
  // packages/runtime/src/sqlite/approval-repository.ts
2572
+ function storedQuestions(text3) {
2573
+ if (!text3) return [];
2574
+ try {
2575
+ const parsed = JSON.parse(text3);
2576
+ return Array.isArray(parsed) ? parsed.filter((q) => typeof q === "object" && q !== null && !Array.isArray(q)) : [];
2577
+ } catch {
2578
+ return [];
2579
+ }
2580
+ }
2571
2581
  var key = (sessionId, toolUseId) => JSON.stringify([sessionId, toolUseId]);
2572
2582
  var SqliteApprovalRepository = class {
2573
2583
  constructor(ctx) {
@@ -2579,22 +2589,24 @@ var SqliteApprovalRepository = class {
2579
2589
  const now = this.ctx.now();
2580
2590
  this.ctx.db.run("DELETE FROM approvals WHERE expires_at < ?", now - APPROVAL_TTL_SECONDS);
2581
2591
  const recorded = this.ctx.db.run(
2582
- "INSERT OR IGNORE INTO approvals (session_id, tool_use_id, aad_id, tool, expires_at, created_at, card_sent, card_json) VALUES (?, ?, ?, ?, ?, ?, 0, ?)",
2592
+ "INSERT OR IGNORE INTO approvals (session_id, tool_use_id, aad_id, tool, expires_at, created_at, card_sent, card_json, questions_json) VALUES (?, ?, ?, ?, ?, ?, 0, ?, ?)",
2583
2593
  approval.sessionId,
2584
2594
  approval.toolUseId,
2585
2595
  approval.userId,
2586
2596
  approval.tool,
2587
2597
  approval.expiresAt,
2588
2598
  now,
2589
- approval.envelope === void 0 || approval.envelope === null ? null : pyJsonDumps(approval.envelope)
2599
+ approval.envelope === void 0 || approval.envelope === null ? null : pyJsonDumps(approval.envelope),
2600
+ approval.questions?.length ? JSON.stringify(approval.questions) : ""
2590
2601
  ) === 1;
2591
2602
  if (recorded) this.#sending.add(key(approval.sessionId, approval.toolUseId));
2592
2603
  return recorded;
2593
2604
  }
2594
- approvalSent(sessionId, toolUseId) {
2605
+ approvalSent(sessionId, toolUseId, cardId = "") {
2595
2606
  this.#sending.delete(key(sessionId, toolUseId));
2596
2607
  this.ctx.db.run(
2597
- "UPDATE approvals SET card_sent = 1, card_json = NULL WHERE session_id = ? AND tool_use_id = ?",
2608
+ "UPDATE approvals SET card_sent = 1, card_json = NULL, card_id = ? WHERE session_id = ? AND tool_use_id = ?",
2609
+ cardId,
2598
2610
  sessionId,
2599
2611
  toolUseId
2600
2612
  );
@@ -2654,6 +2666,26 @@ var SqliteApprovalRepository = class {
2654
2666
  state: col.str(row, "state")
2655
2667
  };
2656
2668
  }
2669
+ pendingQuestion(sessionId, userId) {
2670
+ const row = this.ctx.db.get(
2671
+ "SELECT tool_use_id, expires_at, questions_json, card_id FROM approvals WHERE session_id = ? AND aad_id = ? AND tool = ? AND state = 'pending' AND card_sent = 1 AND expires_at > ? AND questions_json != '' ORDER BY created_at DESC LIMIT 1",
2672
+ sessionId,
2673
+ userId,
2674
+ QUESTION_TOOL,
2675
+ this.ctx.now()
2676
+ );
2677
+ if (!row) return null;
2678
+ const questions = storedQuestions(col.str(row, "questions_json"));
2679
+ if (questions.length === 0) return null;
2680
+ return {
2681
+ sessionId,
2682
+ toolUseId: col.str(row, "tool_use_id"),
2683
+ userId,
2684
+ expiresAt: col.num(row, "expires_at"),
2685
+ questions,
2686
+ cardId: col.str(row, "card_id")
2687
+ };
2688
+ }
2657
2689
  closeApproval(sessionId, toolUseId, state, options = {}) {
2658
2690
  const onlyIfPending = options.onlyIfPending ?? true;
2659
2691
  return this.ctx.db.run(
@@ -3683,7 +3715,11 @@ var ADDED_COLUMNS = [
3683
3715
  "approvals",
3684
3716
  [
3685
3717
  ["card_sent", "INTEGER NOT NULL DEFAULT 1"],
3686
- ["card_json", "TEXT"]
3718
+ ["card_json", "TEXT"],
3719
+ ["card_id", "TEXT NOT NULL DEFAULT ''"],
3720
+ // '' = the channel never said the card's id
3721
+ ["questions_json", "TEXT NOT NULL DEFAULT ''"]
3722
+ // '' = not a turn's question card
3687
3723
  ]
3688
3724
  ],
3689
3725
  ["notify_subs", [["types_version", "INTEGER NOT NULL DEFAULT 0"]]],
@@ -1,18 +1,19 @@
1
1
  /** ApprovalRepository on SQLite (table `approvals`). Which cards are being sent right now is kept in this process only;
2
2
  * what outlives a crash is the `card_sent` column: a card recorded and not marked sent is sent by the next process. */
3
- import { type Approval, type ApprovalRepository, type ApprovalState, type NewApproval, type UnsentCard } from "@primusclaw/personal-agent";
3
+ import { type Approval, type ApprovalRepository, type ApprovalState, type NewApproval, type PendingQuestion, type UnsentCard } from "@primusclaw/personal-agent";
4
4
  import type { StoreContext } from "./context.js";
5
5
  export declare class SqliteApprovalRepository implements ApprovalRepository {
6
6
  #private;
7
7
  private readonly ctx;
8
8
  constructor(ctx: StoreContext);
9
9
  saveApproval(approval: NewApproval): boolean;
10
- approvalSent(sessionId: string, toolUseId: string): void;
10
+ approvalSent(sessionId: string, toolUseId: string, cardId?: string): void;
11
11
  approvalInFlight(sessionId: string, toolUseId: string): boolean;
12
12
  forgetApproval(sessionId: string, toolUseId: string): void;
13
13
  claimUnsentApprovals(): UnsentCard[];
14
14
  approvalSendFailed(sessionId: string, toolUseId: string): void;
15
15
  getApproval(sessionId: string, toolUseId: string): Approval | null;
16
+ pendingQuestion(sessionId: string, userId: string): PendingQuestion | null;
16
17
  closeApproval(sessionId: string, toolUseId: string, state: ApprovalState, options?: {
17
18
  readonly onlyIfPending?: boolean;
18
19
  }): boolean;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@primusclaw/personal-agent-runtime",
3
- "version": "0.4.0",
3
+ "version": "0.5.0",
4
4
  "description": "Node adapters for the personal agent's ports: clock, randomness, logging (and, as the port lands, storage, Claw, metrics, HTTP).",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -29,7 +29,7 @@
29
29
  "access": "public"
30
30
  },
31
31
  "dependencies": {
32
- "@primusclaw/personal-agent": "0.4.0",
32
+ "@primusclaw/personal-agent": "0.5.0",
33
33
  "fastify": "^5.12.8",
34
34
  "zod": "^4.6.7"
35
35
  }