@harperfast/agent 0.11.4 → 0.11.5

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/agent.js +52 -81
  2. package/package.json +1 -2
package/dist/agent.js CHANGED
@@ -2435,57 +2435,64 @@ Stack: ${String(err.stack).split("\n").slice(0, 8).join("\n")}` : "";
2435
2435
 
2436
2436
  // utils/sessions/DiskSession.ts
2437
2437
  import { MemorySession } from "@openai/agents";
2438
- import sqlite3 from "sqlite3";
2438
+ import { existsSync as existsSync8 } from "fs";
2439
+ import { mkdir as mkdir2, readFile as readFile6, rename, writeFile as writeFile4 } from "fs/promises";
2440
+ import { dirname as dirname5 } from "path";
2439
2441
  var DiskSession = class extends MemorySession {
2440
- db;
2442
+ filePath;
2441
2443
  ready;
2442
- useJsonb = false;
2443
- constructor(dbPath, options) {
2444
+ constructor(filePath, options) {
2444
2445
  super(options);
2445
- this.db = new sqlite3.Database(dbPath);
2446
+ this.filePath = filePath;
2446
2447
  this.ready = this.init(options);
2447
2448
  }
2448
2449
  async init(options) {
2449
- await this.run(`
2450
- CREATE TABLE IF NOT EXISTS session_items (
2451
- id INTEGER PRIMARY KEY AUTOINCREMENT,
2452
- sessionId TEXT,
2453
- data JSONB
2454
- )
2455
- `);
2456
- try {
2457
- await this.get("SELECT jsonb('{}')");
2458
- this.useJsonb = true;
2459
- } catch {
2460
- this.useJsonb = false;
2461
- }
2450
+ const storage = await this.loadStorage();
2462
2451
  let sessionId = this.sessionId;
2463
2452
  if (!options?.sessionId) {
2464
- const row = await this.get("SELECT DISTINCT sessionId FROM session_items LIMIT 1");
2465
- if (row) {
2466
- sessionId = row.sessionId;
2453
+ const sessionIds = Object.keys(storage.sessions);
2454
+ if (sessionIds.length > 0) {
2455
+ sessionId = sessionIds[0];
2467
2456
  this.sessionId = sessionId;
2468
2457
  }
2469
2458
  }
2470
2459
  if (!sessionId) {
2471
2460
  sessionId = this.sessionId || "default-session";
2472
2461
  }
2473
- const rows = await this.all(
2474
- "SELECT json(data) AS data FROM session_items WHERE sessionId = ? ORDER BY id ASC",
2475
- sessionId
2476
- );
2477
- if (rows.length > 0) {
2478
- const itemsFromDb = rows.map((row) => JSON.parse(row.data));
2479
- this.items = itemsFromDb;
2462
+ const itemsFromStorage = storage.sessions[sessionId];
2463
+ if (itemsFromStorage) {
2464
+ this.items = itemsFromStorage;
2480
2465
  } else {
2481
2466
  const items = this.items || [];
2482
2467
  if (items.length > 0) {
2483
- const sql = this.useJsonb ? "INSERT INTO session_items (sessionId, data) VALUES (?, jsonb(?))" : "INSERT INTO session_items (sessionId, data) VALUES (?, ?)";
2484
- for (const item of items) {
2485
- await this.run(sql, sessionId, JSON.stringify(item));
2486
- }
2468
+ await this.updateStorage((s) => {
2469
+ s.sessions[sessionId] = items;
2470
+ });
2471
+ }
2472
+ }
2473
+ }
2474
+ async loadStorage() {
2475
+ if (existsSync8(this.filePath)) {
2476
+ try {
2477
+ const data = await readFile6(this.filePath, "utf-8");
2478
+ return JSON.parse(data);
2479
+ } catch (e) {
2480
+ console.error(`Failed to read session file ${this.filePath}:`, e);
2487
2481
  }
2488
2482
  }
2483
+ return { sessions: {} };
2484
+ }
2485
+ async updateStorage(update) {
2486
+ const storage = await this.loadStorage();
2487
+ update(storage);
2488
+ const dir = dirname5(this.filePath);
2489
+ if (!existsSync8(dir)) {
2490
+ await mkdir2(dir, { recursive: true });
2491
+ }
2492
+ const data = JSON.stringify(storage, null, 2);
2493
+ const tempPath = `${this.filePath}.tmp`;
2494
+ await writeFile4(tempPath, data, "utf-8");
2495
+ await rename(tempPath, this.filePath);
2489
2496
  }
2490
2497
  async getSessionId() {
2491
2498
  await this.ready;
@@ -2499,28 +2506,23 @@ var DiskSession = class extends MemorySession {
2499
2506
  await this.ready;
2500
2507
  await super.addItems(items);
2501
2508
  const sessionId = await this.getSessionId();
2502
- const sql = this.useJsonb ? "INSERT INTO session_items (sessionId, data) VALUES (?, jsonb(?))" : "INSERT INTO session_items (sessionId, data) VALUES (?, ?)";
2503
- for (const item of items) {
2504
- await this.run(sql, sessionId, JSON.stringify(item));
2505
- }
2509
+ await this.updateStorage((storage) => {
2510
+ if (!storage.sessions[sessionId]) {
2511
+ storage.sessions[sessionId] = [];
2512
+ }
2513
+ storage.sessions[sessionId].push(...items);
2514
+ });
2506
2515
  }
2507
2516
  async popItem() {
2508
2517
  await this.ready;
2509
2518
  const item = await super.popItem();
2510
2519
  if (item) {
2511
2520
  const sessionId = await this.getSessionId();
2512
- await this.run(
2513
- `
2514
- DELETE FROM session_items
2515
- WHERE id = (
2516
- SELECT id FROM session_items
2517
- WHERE sessionId = ?
2518
- ORDER BY id DESC
2519
- LIMIT 1
2520
- )
2521
- `,
2522
- sessionId
2523
- );
2521
+ await this.updateStorage((storage) => {
2522
+ if (storage.sessions[sessionId]) {
2523
+ storage.sessions[sessionId].pop();
2524
+ }
2525
+ });
2524
2526
  }
2525
2527
  return item;
2526
2528
  }
@@ -2528,39 +2530,8 @@ var DiskSession = class extends MemorySession {
2528
2530
  await this.ready;
2529
2531
  await super.clearSession();
2530
2532
  const sessionId = await this.getSessionId();
2531
- await this.run("DELETE FROM session_items WHERE sessionId = ?", sessionId);
2532
- }
2533
- run(sql, ...params) {
2534
- return new Promise((resolve2, reject) => {
2535
- this.db.run(sql, ...params, (err) => {
2536
- if (err) {
2537
- reject(err);
2538
- } else {
2539
- resolve2();
2540
- }
2541
- });
2542
- });
2543
- }
2544
- get(sql, ...params) {
2545
- return new Promise((resolve2, reject) => {
2546
- this.db.get(sql, ...params, (err, row) => {
2547
- if (err) {
2548
- reject(err);
2549
- } else {
2550
- resolve2(row);
2551
- }
2552
- });
2553
- });
2554
- }
2555
- all(sql, ...params) {
2556
- return new Promise((resolve2, reject) => {
2557
- this.db.all(sql, ...params, (err, rows) => {
2558
- if (err) {
2559
- reject(err);
2560
- } else {
2561
- resolve2(rows);
2562
- }
2563
- });
2533
+ await this.updateStorage((storage) => {
2534
+ delete storage.sessions[sessionId];
2564
2535
  });
2565
2536
  }
2566
2537
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@harperfast/agent",
3
3
  "description": "AI to help you with Harper app management",
4
- "version": "0.11.4",
4
+ "version": "0.11.5",
5
5
  "main": "dist/agent.js",
6
6
  "repository": "github:HarperFast/harper-agent",
7
7
  "bugs": {
@@ -52,7 +52,6 @@
52
52
  "cross-spawn": "^7.0.6",
53
53
  "dotenv": "^17.2.3",
54
54
  "ollama-ai-provider-v2": "^3.0.3",
55
- "sqlite3": "^5.1.7",
56
55
  "zod": "^4.3.6"
57
56
  },
58
57
  "devDependencies": {