Skip to content

armgit / mso

Public Imported from GitHub
0

Browser-based graphical shell and control plane for a Linux server you own — real terminal, file manager, live metrics and BYOK AI in one mobile-first workspace. Next.js 16 · React 19 · node-pty · Tailwind v4 · shadcn/ui. Public alpha, self-hosted.

2.9 KB
Raw Blame
import { artifactLocation } from "./artifact-paths";
import { archiveAgentSession, pruneAgentSessionArchives } from "./session-archive";
import { principalHash, readSessionFile } from "./session-files";
import { compactSessionContext, MAX_EVENTS, sessionContextTokens } from "./session-policy";
import { retainSessionEvents } from "./session-sequence";
import { appendSemanticSessionEvent } from "./session-semantic";
import type { AgentSession, AgentSessionEvent, AgentSessionSummary } from "./session-types";
import { agentSessionLabel } from "./session-name";

export function summary(record: AgentSession): AgentSessionSummary {
  return {
    id: record.id,
    source: record.source,
    name: record.name,
    label: agentSessionLabel(record.name, record.title, record.cwd),
    title: record.title,
    titleSource: record.titleSource,
    createdAt: record.createdAt,
    updatedAt: record.updatedAt,
    turnActive: Boolean(record.turnStartedAt),
    ...(record.resumedFrom ? { resumedFrom: record.resumedFrom } : {}),
    ...(record.cwd ? { cwd: record.cwd } : {}),
    ...(record.parentSessionId
      ? { parentSessionId: record.parentSessionId }
      : {}),
    estimatedTokens: record.estimatedTokens,
    lifetimeEstimatedTokens: record.lifetimeEstimatedTokens,
    compactThresholdTokens: record.compactThresholdTokens,
    compactionCount: record.compactionCount,
    archiveCount: record.archiveCount,
    ...(record.lastCompactedAt
      ? { lastCompactedAt: record.lastCompactedAt }
      : {}),
    ...(record.lastArchivedAt ? { lastArchivedAt: record.lastArchivedAt } : {}),
    eventCount: record.events.length,
    historyTurns: record.history.length,
    artifacts: artifactLocation(record),
  };
}

export async function compactIfNeeded(
  record: AgentSession,
  reason: string,
): Promise<AgentSession> {
  record.estimatedTokens = sessionContextTokens(record);
  if (record.estimatedTokens < record.compactThresholdTokens) return record;
  const now = new Date();
  await archiveAgentSession(record, reason, now);
  let next = compactSessionContext(record, now.toISOString());
  const archived: AgentSessionEvent = {
    at: now.toISOString(),
    kind: "archived",
    detail: `${reason}; 30-day retention`,
  };
  const retained = retainSessionEvents(appendSemanticSessionEvent(next.events, archived), next.eventSeqBase, MAX_EVENTS);
  next = {
    ...next,
    events: retained.events,
    eventSeqBase: retained.eventSeqBase,
    archiveCount: record.archiveCount + 1,
    lastArchivedAt: now.toISOString(),
  };
  next.estimatedTokens = sessionContextTokens(next);
  void pruneAgentSessionArchives().catch(() => undefined);
  return next;
}

export async function requireOwned(
  principal: string,
  id: string,
): Promise<AgentSession> {
  const record = await readSessionFile(id);
  if (!record || record.principalHash !== principalHash(principal))
    throw new Error("agent session not found for this client");
  return record;
}