import type { ReasoningLevel, ThinkLevel } from "../../auto-reply/thinking.js";
import type { MemoryCitationsMode } from "../../config/types.memory.js";
import type { PermissionLevel } from "../../security/permission-level.js";
import type { EmbeddedContextFile } from "../pi-embedded/pi-embedded-helpers.js";
import type { ResolvedTimeFormat } from "../runtime-utils/date-time.js";
import type { SystemPromptUserAwareness, UserContext } from "../system-prompt/user-context.js";
/** The five conceptual layers of the prompt, from broadest to narrowest scope. */
export type SectionLayer = "platform" | "agent-type" | "identity" | "user" | "session";
/**
 * Controls which hardcoded sections are included in the system prompt.
 * - "full": All sections (default, for main agent)
 * - "minimal": Reduced sections (Tooling, Workspace, Runtime) - used for subagents
 * - "none": Bare identity line only
 *
 * Re-exported here to avoid circular dependency with system-prompt.ts.
 */
export type PromptMode = "full" | "minimal" | "none";
/**
 * The full parameter shape accepted by buildAgentSystemPrompt().
 * Extracted as a named interface so both the old function and the new
 * section system can reference it without circular imports.
 */
export interface BuildAgentSystemPromptParams {
    workspaceDir: string;
    defaultThinkLevel?: ThinkLevel;
    reasoningLevel?: ReasoningLevel;
    extraSystemPrompt?: string;
    ownerNumbers?: string[];
    reasoningTagHint?: boolean;
    toolNames?: string[];
    toolSummaries?: Record<string, string>;
    modelAliasLines?: string[];
    userTimezone?: string;
    userTime?: string;
    userTimeFormat?: ResolvedTimeFormat;
    contextFiles?: EmbeddedContextFile[];
    skillsPrompt?: string;
    heartbeatPrompt?: string;
    docsPath?: string;
    workspaceNotes?: string[];
    ttsHint?: string;
    promptMode?: PromptMode;
    runtimeInfo?: {
        agentId?: string;
        host?: string;
        os?: string;
        arch?: string;
        node?: string;
        model?: string;
        defaultModel?: string;
        shell?: string;
        channel?: string;
        capabilities?: string[];
        repoRoot?: string;
    };
    messageToolHints?: string[];
    sandboxInfo?: {
        enabled: boolean;
        workspaceDir?: string;
        containerWorkspaceDir?: string;
        workspaceAccess?: "none" | "ro" | "rw";
        agentWorkspaceMount?: string;
        browserBridgeUrl?: string;
        browserNoVncUrl?: string;
        hostBrowserAllowed?: boolean;
        elevated?: {
            allowed: boolean;
            defaultLevel: "on" | "off" | "ask" | "full";
        };
    };
    reactionGuidance?: {
        level: "minimal" | "extensive";
        channel: string;
    };
    memoryCitationsMode?: MemoryCitationsMode;
    senderPermissionLevel?: PermissionLevel;
    chatType?: "direct" | "group" | "channel";
    userContext?: UserContext;
    systemPromptUserAwareness?: SystemPromptUserAwareness;
    /**
     * Controls user context injection and memory behavior for public vs personal agents.
     * - 'personal': Full user-specific context (default)
     * - 'public': Suppress personal context, use generalized memory guidance
     */
    contextMode?: "personal" | "public";
}
/**
 * The render context passed to every section's render function.
 * Wraps ALL derived values currently computed at the top of buildAgentSystemPrompt().
 */
export interface SectionRenderContext {
    params: BuildAgentSystemPromptParams;
    toolLines: string[];
    availableTools: Set<string>;
    resolveToolName: (normalized: string) => string;
    hasGateway: boolean;
    readToolName: string;
    execToolName: string;
    processToolName: string;
    extraSystemPrompt: string | undefined;
    ownerLine: string | undefined;
    reasoningHint: string | undefined;
    reasoningLevel: ReasoningLevel;
    userTimezone: string | undefined;
    skillsPrompt: string | undefined;
    heartbeatPrompt: string | undefined;
    runtimeChannel: string | undefined;
    runtimeCapabilities: string[];
    runtimeCapabilitiesLower: Set<string>;
    inlineButtonsEnabled: boolean;
    messageChannelOptions: string;
    promptMode: PromptMode;
    isMinimal: boolean;
    /** True when the agent is in public mode (contextMode === "public"). */
    isPublicAgent: boolean;
    sandboxContainerWorkspace: string | undefined;
    sanitizedWorkspaceDir: string;
    sanitizedSandboxContainerWorkspace: string;
    displayWorkspaceDir: string;
    workspaceGuidance: string;
    workspaceNotes: string[];
}
/**
 * A prompt section definition.
 *
 * Sections declare their inclusion criteria (modes, required tools) and
 * rendering logic. The registry collects them and assembles the final prompt.
 */
/**
 * Phase D-0e/f — where this section's content comes from. Used in the
 * prompt-preview viewer to help operators understand what to edit when a
 * section's output is wrong.
 *
 * - "static":    Hardcoded boilerplate prose owned by the section definition.
 * - "file":      Loaded from agent workspace files (bootstrap, project context).
 * - "generated": Produced at request time from runtime state (skills scan,
 *                memory subsystem, workspace filesystem, current time).
 * - "config":    User-configurable per-agent settings (personality, sandbox,
 *                permissions, heartbeats).
 * - "custom":    User-authored YAML section from Phase 19 customisation.
 */
export type PromptSectionSource = "static" | "file" | "generated" | "config" | "custom";
export interface PromptSectionDefinition {
    /** Kebab-case identifier, e.g. "safety", "memory-recall" (per D-03). */
    id: string;
    /** Which layer this section belongs to (per D-01). */
    layer: SectionLayer;
    /** Assembly position -- lower numbers come first. Use gaps of 100 for built-in sections. */
    order: number;
    /** Which prompt modes include this section (per D-04). */
    modes: PromptMode[];
    /** ALL of these tools must be present for the section to be included. */
    requiredTools?: string[];
    /** ANY ONE of these tools must be present for the section to be included. */
    anyOfTools?: string[];
    /** Whether this section's output is stable across turns for prompt caching (per D-07). */
    cacheable: boolean;
    /** The markdown header this section produces, e.g. "## Safety". Used by prompt-cache.ts migration. */
    header?: string;
    /** Phase D-0e/f — where this section's content originates. Falls back to
     * a static SOURCE_MAP in registry.ts when omitted. */
    source?: PromptSectionSource;
    /**
     * Phase D-0f-1.5 — Tier-1 prose seed for "Initialize from default" UX.
     * Sections that have pure prose with no conditional branches or runtime
     * interpolation can opt in by setting this. When set, the hub modal
     * offers a button that writes this string to the section's .md file
     * to bootstrap editing. Sections with conditionals MUST NOT set this —
     * baking one branch's output would silently break the other branches.
     *
     * Variant-aware: for sections with variants, use `templateTexts` instead.
     */
    templateText?: string;
    /**
     * Phase D-0f-1.5 — variant-keyed seeds. Use this when the section has
     * branches (e.g. safety.full vs safety.minimal). Keys are variant
     * suffixes; the "default" key seeds the no-variant file.
     */
    templateTexts?: Record<string, string>;
    /** Render function. Returns lines of the section. Return [] to self-exclude (per D-04). */
    render: (ctx: SectionRenderContext) => string[];
}
