AkurAI Build
Menu

popagent

public

Latest change 6edc7e928c57dab23c0973ed3f7ee70b3ebb72d5 - Initial popagent baseline by AkurAI Build

import { storage } from "./storage";
import type { MemorySettings, MemorySettingsInput } from "./api-types";
import { retryableInit } from "./retryable-init";


export class MemorySettingsStore {
  readonly storage = storage;
  private readonly initializeOnce = retryableInit(() => this.initialize());

  init(): Promise<void> {
    return this.initializeOnce();
  }

  private async initialize() {
    await this.storage.init();
    const schema = await Bun.file(new URL("./memory-settings.sql", import.meta.url)).text();
    await this.storage.db.none(schema);
  }

  async get(): Promise<MemorySettings> {
    await this.init();
    return this.storage.db.one<MemorySettings>(`
      SELECT auto_compact AS "autoCompact",
        observation_tokens AS "observationTokens",
        reflection_tokens AS "reflectionTokens",
        recent_message_percent AS "recentMessagePercent",
        buffer_on_idle AS "bufferOnIdle",
        updated_at::text AS "updatedAt"
      FROM popagent_memory_settings WHERE singleton = TRUE
    `);
  }

  async update(input: MemorySettingsInput): Promise<MemorySettings> {
    await this.init();
    return this.storage.db.one<MemorySettings>(`
      UPDATE popagent_memory_settings
      SET auto_compact = $1,
        observation_tokens = $2,
        reflection_tokens = $3,
        recent_message_percent = $4,
        buffer_on_idle = $5,
        updated_at = NOW()
      WHERE singleton = TRUE
      RETURNING auto_compact AS "autoCompact",
        observation_tokens AS "observationTokens",
        reflection_tokens AS "reflectionTokens",
        recent_message_percent AS "recentMessagePercent",
        buffer_on_idle AS "bufferOnIdle",
        updated_at::text AS "updatedAt"
    `, [input.autoCompact, input.observationTokens, input.reflectionTokens, input.recentMessagePercent, input.bufferOnIdle]);
  }
}

export const memorySettings = new MemorySettingsStore();