@primusclaw/personal-agent-runtime 0.4.0 → 0.6.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(
@@ -2844,6 +2876,51 @@ var SqliteAssistantButtonRepository = class {
2844
2876
  moveConfirmCard(cardId, state, options) {
2845
2877
  return moveCard(this.ctx.db, "assistant_confirm_cards", cardId, state, options);
2846
2878
  }
2879
+ addChoiceCard(userId, choice) {
2880
+ const now = this.ctx.now();
2881
+ this.ctx.db.run("DELETE FROM assistant_choice_cards WHERE created_at < ?", now - ASSISTANT_MESSAGE_KEEP_SECONDS);
2882
+ const cardId = tokenUrlsafe(this.ctx.random, 12);
2883
+ this.ctx.db.run(
2884
+ "INSERT INTO assistant_choice_cards (card_id, aad_id, text, options_json, multiple, lang, state, created_at) VALUES (?, ?, ?, ?, ?, ?, 'open', ?)",
2885
+ cardId,
2886
+ userId,
2887
+ choice.text,
2888
+ pyJsonDumps(choice.options),
2889
+ flag(choice.multiple),
2890
+ choice.lang,
2891
+ now
2892
+ );
2893
+ return cardId;
2894
+ }
2895
+ getChoiceCard(cardId) {
2896
+ const row = this.ctx.db.get(
2897
+ "SELECT card_id, aad_id, text, options_json, multiple, chosen_json, lang, state, created_at FROM assistant_choice_cards WHERE card_id = ? AND created_at >= ?",
2898
+ cardId,
2899
+ this.ctx.now() - ASSISTANT_MESSAGE_KEEP_SECONDS
2900
+ );
2901
+ if (!row) return null;
2902
+ return {
2903
+ cardId: col.str(row, "card_id"),
2904
+ userId: col.str(row, "aad_id"),
2905
+ text: col.str(row, "text"),
2906
+ options: jsonStrings(col.str(row, "options_json") || "[]"),
2907
+ multiple: col.bool(row, "multiple"),
2908
+ chosen: jsonStrings(col.str(row, "chosen_json") || "[]"),
2909
+ lang: col.str(row, "lang"),
2910
+ state: col.str(row, "state"),
2911
+ createdAt: col.num(row, "created_at")
2912
+ };
2913
+ }
2914
+ claimChoiceCard(cardId, chosen) {
2915
+ return this.ctx.db.run(
2916
+ "UPDATE assistant_choice_cards SET state = 'busy', chosen_json = ? WHERE card_id = ? AND state = 'open'",
2917
+ pyJsonDumps(chosen),
2918
+ cardId
2919
+ ) === 1;
2920
+ }
2921
+ moveChoiceCard(cardId, state, options) {
2922
+ return moveCard(this.ctx.db, "assistant_choice_cards", cardId, state, options);
2923
+ }
2847
2924
  };
2848
2925
  var SqliteAssistantMessageRepository = class {
2849
2926
  constructor(ctx) {
@@ -3394,6 +3471,7 @@ var PERSON_TABLES = [
3394
3471
  "auto_replies",
3395
3472
  "assistant_action_cards",
3396
3473
  "assistant_confirm_cards",
3474
+ "assistant_choice_cards",
3397
3475
  "observed_groups",
3398
3476
  "observe_cards",
3399
3477
  "pending_requests"
@@ -3683,7 +3761,11 @@ var ADDED_COLUMNS = [
3683
3761
  "approvals",
3684
3762
  [
3685
3763
  ["card_sent", "INTEGER NOT NULL DEFAULT 1"],
3686
- ["card_json", "TEXT"]
3764
+ ["card_json", "TEXT"],
3765
+ ["card_id", "TEXT NOT NULL DEFAULT ''"],
3766
+ // '' = the channel never said the card's id
3767
+ ["questions_json", "TEXT NOT NULL DEFAULT ''"]
3768
+ // '' = not a turn's question card
3687
3769
  ]
3688
3770
  ],
3689
3771
  ["notify_subs", [["types_version", "INTEGER NOT NULL DEFAULT 0"]]],
@@ -109,6 +109,9 @@ CREATE TABLE IF NOT EXISTS assistant_action_cards (
109
109
  CREATE TABLE IF NOT EXISTS assistant_confirm_cards (
110
110
  card_id TEXT PRIMARY KEY, aad_id TEXT NOT NULL, confirmation_id TEXT NOT NULL, kind TEXT NOT NULL, label TEXT NOT NULL DEFAULT '',
111
111
  state TEXT NOT NULL DEFAULT 'open', created_at REAL NOT NULL);
112
+ CREATE TABLE IF NOT EXISTS assistant_choice_cards (
113
+ card_id TEXT PRIMARY KEY, aad_id TEXT NOT NULL, text TEXT NOT NULL DEFAULT '', options_json TEXT NOT NULL, multiple INTEGER NOT NULL,
114
+ chosen_json TEXT NOT NULL DEFAULT '[]', lang TEXT NOT NULL DEFAULT '', state TEXT NOT NULL DEFAULT 'open', created_at REAL NOT NULL);
112
115
  CREATE TABLE IF NOT EXISTS asker_follows (
113
116
  item_id TEXT PRIMARY KEY, batch_id TEXT NOT NULL, review_json TEXT NOT NULL, reference_json TEXT NOT NULL,
114
117
  said TEXT NOT NULL DEFAULT '', reviewer_told INTEGER NOT NULL DEFAULT 0, deadline REAL NOT NULL, created_at REAL NOT NULL);
@@ -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;
@@ -1,6 +1,6 @@
1
1
  /** The assistant's cards and messages on SQLite (tables `assistant_cards`, `auto_replies`, `assistant_action_cards`,
2
- * `assistant_confirm_cards`, `assistant_messages`). */
3
- import { type ActionCard, type AssistantButtonRepository, type AssistantCard, type AssistantCardRepository, type AssistantMessage, type AssistantMessageRepository, type CardState, type ConfirmCard, type JsonObject, type MoveOptions, type PlacedCard } from "@primusclaw/personal-agent";
2
+ * `assistant_confirm_cards`, `assistant_choice_cards`, `assistant_messages`). */
3
+ import { type ActionCard, type AssistantButtonRepository, type AssistantCard, type AssistantCardRepository, type AssistantMessage, type AssistantMessageRepository, type CardState, type ChoiceCard, type ConfirmCard, type JsonObject, type MoveOptions, type PlacedCard } from "@primusclaw/personal-agent";
4
4
  import { type StoreContext } from "./context.js";
5
5
  export declare class SqliteAssistantCardRepository implements AssistantCardRepository {
6
6
  private readonly ctx;
@@ -32,6 +32,15 @@ export declare class SqliteAssistantButtonRepository implements AssistantButtonR
32
32
  addConfirmCard(userId: string, confirmationId: string, kind: string, label?: string): string;
33
33
  getConfirmCard(cardId: string): ConfirmCard | null;
34
34
  moveConfirmCard(cardId: string, state: CardState, options?: MoveOptions): boolean;
35
+ addChoiceCard(userId: string, choice: {
36
+ readonly text: string;
37
+ readonly options: readonly string[];
38
+ readonly multiple: boolean;
39
+ readonly lang: string;
40
+ }): string;
41
+ getChoiceCard(cardId: string): ChoiceCard | null;
42
+ claimChoiceCard(cardId: string, chosen: readonly string[]): boolean;
43
+ moveChoiceCard(cardId: string, state: CardState, options?: MoveOptions): boolean;
35
44
  }
36
45
  export declare class SqliteAssistantMessageRepository implements AssistantMessageRepository {
37
46
  private readonly ctx;
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.6.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.6.0",
33
33
  "fastify": "^5.12.8",
34
34
  "zod": "^4.6.7"
35
35
  }