AkurAI Build
Menu

popagent

public

Latest change dc4b371554df3a78c40c29085ead1db3de7e24f8 - Add model routing policy, companyStatus tool, Markdown channel output, compact Runtime settings by AkurAI Build

import { runtimeModelId, type AgentRuntimeSettings, type AgentRuntimeSettingsInput } from "./api-types";
import { retryableInit } from "./retryable-init";
import { storage } from "./storage";

const RUNTIME_SETTINGS_COLUMNS = `
  default_model AS "defaultModel",
  model_source AS "modelSource",
  model_routing AS "modelRouting",
  supervisor_max_steps AS "supervisorMaxSteps",
  specialist_max_steps AS "specialistMaxSteps",
  tool_concurrency AS "toolConcurrency",
  delegation_context_messages AS "delegationContextMessages",
  delegation_result_characters AS "delegationResultCharacters",
  max_processor_retries AS "maxProcessorRetries",
  final_response_feedback AS "finalResponseFeedback",
  delegation_failure_feedback AS "delegationFailureFeedback",
  delegation_result_truncation_marker AS "delegationResultTruncationMarker",
  task_concurrency AS "taskConcurrency",
  task_poll_interval_ms AS "taskPollIntervalMs",
  task_timeout_ms AS "taskTimeoutMs",
  task_stale_after_ms AS "taskStaleAfterMs",
  updated_at::text AS "updatedAt"
`;

export class AgentRuntimeSettingsStore {
  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("./agent-runtime-settings.sql", import.meta.url)).text();
    await this.storage.db.none(schema);
  }

  async get(): Promise<AgentRuntimeSettings> {
    await this.init();
    return this.storage.db.one<AgentRuntimeSettings>(`
      SELECT ${RUNTIME_SETTINGS_COLUMNS}
      FROM popagent_agent_runtime_settings
      WHERE singleton = TRUE
    `);
  }

  async update(input: AgentRuntimeSettingsInput): Promise<AgentRuntimeSettings> {
    await this.init();
    return this.storage.db.one<AgentRuntimeSettings>(`
      UPDATE popagent_agent_runtime_settings
      SET default_model = $1,
          model_source = $2,
          supervisor_max_steps = $3,
          specialist_max_steps = $4,
          tool_concurrency = $5,
          delegation_context_messages = $6,
          delegation_result_characters = $7,
          max_processor_retries = $8,
          final_response_feedback = $9,
          delegation_failure_feedback = $10,
          delegation_result_truncation_marker = $11,
          task_concurrency = $12,
          task_poll_interval_ms = $13,
          task_timeout_ms = $14,
          task_stale_after_ms = $15,
          model_routing = $16::jsonb,
          updated_at = NOW()
      WHERE singleton = TRUE
      RETURNING ${RUNTIME_SETTINGS_COLUMNS}
    `, [
      runtimeModelId(input.defaultModel, input.modelSource),
      input.modelSource,
      input.supervisorMaxSteps,
      input.specialistMaxSteps,
      input.toolConcurrency,
      input.delegationContextMessages,
      input.delegationResultCharacters,
      input.maxProcessorRetries,
      input.finalResponseFeedback,
      input.delegationFailureFeedback,
      input.delegationResultTruncationMarker,
      input.taskConcurrency,
      input.taskPollIntervalMs,
      input.taskTimeoutMs,
      input.taskStaleAfterMs,
      JSON.stringify(input.modelRouting),
    ]);
  }
}

export const agentRuntimeSettings = new AgentRuntimeSettingsStore();