Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

5.1 KB
Raw Blame
import type {
  BuiltinMode,
  ModelInfo,
  Mode,
  PermissionMode,
  ProviderPublic,
  SessionThinkingLevel,
  ThinkingLevel,
} from "@pi-desktop/shared";
import {
  isSessionThinkingLevel,
  PERMISSION_MODES,
  sessionThinkingMenuLevels,
} from "@pi-desktop/shared";
import { sameComposerModelId } from "../../../lib/composer-models.ts";
import type { ComposerDraftFileReference, ComposerPluginPart } from "../../../lib/composer-smart-stop";
import { providerThinkingLevels } from "../../../lib/session-thinking.ts";

export const COMPOSER_MIN_HEIGHT_PX = 28;
export const COMPOSER_MAX_VISIBLE_ROWS = 7;

export const PLACEHOLDER_KEYS = {
  home: [
    "chat.placeholderHome",
    "chat.placeholderHomeHint",
    "chat.placeholderShortcut",
  ],
  docked: [
    "chat.placeholder",
    "chat.placeholderHint",
    "chat.placeholderShortcut",
  ],
} as const;

export const MODE_CYCLE: readonly Mode[] = ["agent", "plan", "goal"];

/** Builtin modes only; a user mode is labelled as written (features/modes). */
export const MODE_LABEL_KEYS: Record<BuiltinMode, string> = {
  agent: "settings.modeAgent",
  plan: "settings.modePlan",
  goal: "settings.modeGoal",
};

export { PERMISSION_MODE_I18N_KEYS } from "../../../lib/permission-mode-labels.ts";

export const THINKING_LEVELS: readonly ThinkingLevel[] = [
  "off",
  "minimal",
  "low",
  "medium",
  "high",
  "xhigh",
  "max",
];

export type ComposerPrefill = {
  text: string;
  token: number;
};

export type ComposerFileReference = {
  size?: number;
  localReference?: string;
  lineRange?: import("@pi-desktop/shared").FileLineRange;
  id: string;
  sessionId: string;
  path: string;
  name: string;
  kind: NonNullable<ComposerDraftFileReference["kind"]>;
  mimeType?: string;
  token?: string;
  plugin?: ComposerPluginPart;
};


export function isThinkingLevel(value: unknown): value is SessionThinkingLevel {
  return isSessionThinkingLevel(value);
}

export function isPermissionMode(value: unknown): value is PermissionMode {
  return (
    typeof value === "string" &&
    PERMISSION_MODES.includes(value as PermissionMode)
  );
}

/**
 * Preserve the current level when changing providers, but never carry a
 * reasoning level into a provider that cannot accept it.
 */
export function thinkingLevelForProvider(
  provider: ProviderPublic | null | undefined,
  current: SessionThinkingLevel,
): SessionThinkingLevel {
  const available = providerThinkingLevels(provider);
  if (!provider?.supportsReasoning) return "off";
  if (current === "omit") return "omit";
  if (available.includes(current)) return current;
  const requestedIndex = THINKING_LEVELS.indexOf(current);
  for (let index = requestedIndex; index < THINKING_LEVELS.length; index += 1) {
    const candidate = THINKING_LEVELS[index];
    if (available.includes(candidate)) return candidate;
  }
  for (let index = requestedIndex - 1; index >= 0; index -= 1) {
    const candidate = THINKING_LEVELS[index];
    if (available.includes(candidate)) return candidate;
  }
  return "off";
}

/**
 * Project the selected catalog model onto a provider for draft sessions.
 *
 * Persisted sessions receive these exact capabilities from Electron main.
 * Before the first message creates a session, the provider row only describes
 * its default model, so use the selected catalog record and exact model
 * binding when one is available.
 */
export function thinkingProviderForModel(
  provider: ProviderPublic | null | undefined,
  modelId: string | undefined,
  modelCatalog: readonly ModelInfo[] | undefined,
): ProviderPublic | null | undefined {
  if (!provider || !modelId) return provider;
  const model = modelCatalog?.find((candidate) => sameComposerModelId(candidate.modelId, modelId));

  const binding = provider.models.find((candidate) =>
    sameComposerModelId(candidate.id, modelId),
  );
  const configuredLevels = binding
    ? THINKING_LEVELS.filter((level) => binding.thinkingLevels.includes(level))
    : undefined;

  if (model?.catalogSource !== "models.dev") {
    // Discovery/user rows are not trusted capability matches.
    // A binding override still takes precedence when present.
    // An empty binding is the generic seed for an unknown model, not an
    // explicit disable; `off` is the persisted opt-out for that case.
    const unmatchedLevels = configuredLevels?.length ? configuredLevels : undefined;
    const supportsReasoning = unmatchedLevels
      ? unmatchedLevels.some((level) => level !== "off")
      : true;
    return {
      ...provider,
      supportsReasoning,
      supportedThinkingLevels:
        unmatchedLevels ?? [...THINKING_LEVELS],
    };
  }

  const supportsReasoning = configuredLevels
    ? configuredLevels.some((level) => level !== "off")
    : model.reasoning === true || model.capabilities.includes("reasoning");
  return {
    ...provider,
    supportsReasoning,
    supportedThinkingLevels:
      configuredLevels ?? model.supportedThinkingLevels ?? provider.supportedThinkingLevels,
  };
}

export function cssPixels(value: string): number {
  const parsed = Number.parseFloat(value);
  return Number.isFinite(parsed) ? parsed : 0;
}

export { sessionThinkingMenuLevels };