AkurAI Build
Menu

popagent

public

Latest change 7f0ff66d6d9fb6468416c58bee46bd3d08169501 - Checkpoint browser channels and memory work by AkurAI Build

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

const MEMORY_SETTINGS_COLUMNS = `auto_compact AS "autoCompact",
  observation_tokens AS "observationTokens",
  reflection_tokens AS "reflectionTokens",
  recent_message_percent AS "recentMessagePercent",
  async_buffering AS "asyncBuffering",
  buffer_interval_percent AS "bufferIntervalPercent",
  buffer_on_idle AS "bufferOnIdle",
  observation_block_percent AS "observationBlockPercent",
  reflection_buffer_percent AS "reflectionBufferPercent",
  reflection_block_percent AS "reflectionBlockPercent",
  optimize_observer_context AS "optimizeObserverContext",
  previous_observer_tokens AS "previousObserverTokens",
  retrieval_enabled AS "retrievalEnabled",
  retrieval_scope AS "retrievalScope",
  temporal_markers AS "temporalMarkers",
  activate_after_idle AS "activateAfterIdle",
  activate_on_provider_change AS "activateOnProviderChange",
  share_token_budget AS "shareTokenBudget",
  observe_attachments AS "observeAttachments",
  observation_instruction AS "observationInstruction",
  reflection_instruction AS "reflectionInstruction",
  updated_at::text AS "updatedAt"`;

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 ${MEMORY_SETTINGS_COLUMNS}
      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,
          async_buffering = $5,
          buffer_interval_percent = $6,
          buffer_on_idle = $7,
          observation_block_percent = $8,
          reflection_buffer_percent = $9,
          reflection_block_percent = $10,
          optimize_observer_context = $11,
          previous_observer_tokens = $12,
          retrieval_enabled = $13,
          retrieval_scope = $14,
          temporal_markers = $15,
          activate_after_idle = $16,
          activate_on_provider_change = $17,
          share_token_budget = $18,
          observe_attachments = $19,
          observation_instruction = $20,
          reflection_instruction = $21,
          updated_at = NOW()
      WHERE singleton = TRUE
      RETURNING ${MEMORY_SETTINGS_COLUMNS}
    `, [
      input.autoCompact,
      input.observationTokens,
      input.reflectionTokens,
      input.recentMessagePercent,
      input.asyncBuffering,
      input.bufferIntervalPercent,
      input.bufferOnIdle,
      input.observationBlockPercent,
      input.reflectionBufferPercent,
      input.reflectionBlockPercent,
      input.optimizeObserverContext,
      input.previousObserverTokens,
      input.retrievalEnabled,
      input.retrievalScope,
      input.temporalMarkers,
      input.activateAfterIdle,
      input.activateOnProviderChange,
      input.shareTokenBudget,
      input.observeAttachments,
      input.observationInstruction,
      input.reflectionInstruction,
    ]);
  }
}

export const memorySettings = new MemorySettingsStore();