Skip to content

Linger is An Open-Source Personal Agent That Runs Fully on Your PC. We aim to break Big Tech’s grip on AI by bringing powerful local AI to everyday computers through efficient model–harness co-design, so everyone can run their own.

2.4 KB
Raw Blame
import path from "node:path";
import { loadInstance } from "@linger/local-installation/instance";
import { localModelsEnvironment } from "@linger/local-installation/local-models";
import { readByokEnvironment } from "@linger/local-installation/byok-source";
import {
  LOCALMODELS_TOOL_ENVIRONMENT,
  localmodelsComputerTool,
  localmodelsToolEnvironment,
} from "@linger/local-installation/localmodels-computer";
import { pinnedToolSource } from "@linger/local-installation/localmodels-tool";
import {
  runtimeEnvironmentFromDescriptors,
  type RuntimeDescriptor,
} from "@linger/local-installation/managed-runtimes";
import { managedRuntimeEnvironment } from "@linger/local-installation/managed-codex";
import { readSetupChoice } from "@linger/local-installation/setup-selection";
import type { components } from "@linger/wire-contracts/linger-installer/types";

const RUNTIME_ENVIRONMENT = [
  "LINGER_CODEX_PATH",
  "LINGER_CLAUDE_PATH",
  "LINGER_PI_PATH",
  "CODEX_HOME",
  "CLAUDE_CONFIG_DIR",
  "PI_CODING_AGENT_DIR",
  ...LOCALMODELS_TOOL_ENVIRONMENT,
];

/**
 * What an external Computer (the desktop's) needs to find the runtime and
 * the localmodels tool this installer manages: the paths the CLI's Computer
 * gets, without the rest of its environment.
 */
export async function instanceRuntime(
  home: string,
  name: string,
  options: { runtimeDescriptors?: RuntimeDescriptor[] } = {},
): Promise<components["schemas"]["InstanceRuntime"] | undefined> {
  const value = await loadInstance({ name, home });
  if (!value) return undefined;
  const setupChoice = await readSetupChoice({ name, home });
  const byok = await readByokEnvironment(home);
  const managedEnv =
    options.runtimeDescriptors !== undefined
      ? runtimeEnvironmentFromDescriptors(options.runtimeDescriptors)
      : await managedRuntimeEnvironment(home, value.root);
  const found: Record<string, string | undefined> = {
    ...managedEnv,
    ...(await localModelsEnvironment(value)),
    ...(byok?.mode === "byok" ? byok.environment : {}),
    ...localmodelsToolEnvironment(
      await localmodelsComputerTool(home, pinnedToolSource()),
    ),
  };
  const environment: Record<string, string> = {};
  for (const key of RUNTIME_ENVIRONMENT) {
    const entry = found[key];
    if (entry && path.isAbsolute(entry)) environment[key] = entry;
  }
  return { ...(setupChoice ? { setupChoice } : {}), environment };
}