import * as _midscene_spp_core from '@midscene_spp/core';
import { Insight, ExecutionTaskProgressOptions, PlanningAction, ExecutionTaskApply, Executor, MidsceneYamlFlowItem, InsightExtractParam, InsightAssertionResponse, PlanningActionParamWaitFor, OnTaskStartTip, GroupedActionDump, InsightAction, ExecutionDump, LocateOption, PlanningActionParamScroll, LocatorValidatorOption, AgentDescribeElementAtPointResult, LocateValidatorResult, LocateResultElement, AgentAssertOpt, AgentWaitForOpt } from '@midscene_spp/core';
import { W as WebPage, d as WebElementInfo, e as WebUIContext } from './page-1e107f16.js';
import { ChatCompletionMessageParam } from '@midscene_spp/core/ai-model';
import 'playwright';
import '@midscene_spp/shared/extractor';
import 'puppeteer';
import '@midscene_spp/shared/logger';
import '@midscene_spp/shared/constants';

interface PlanningCache {
    type: 'plan';
    prompt: string;
    yamlWorkflow: string;
}
interface LocateCache {
    type: 'locate';
    prompt: string;
    xpaths: string[];
}
interface MatchCacheResult<T extends PlanningCache | LocateCache> {
    cacheContent: T;
    updateFn: (cb: (cache: T) => void) => void;
}
type CacheFileContent = {
    midsceneVersion: string;
    cacheId: string;
    caches: Array<PlanningCache | LocateCache>;
};
declare class TaskCache {
    cacheId: string;
    cacheFilePath?: string;
    cache: CacheFileContent;
    isCacheResultUsed: boolean;
    cacheOriginalLength: number;
    private matchedCacheIndices;
    constructor(cacheId: string, isCacheResultUsed: boolean, cacheFilePath?: string);
    matchCache(prompt: string, type: 'plan' | 'locate'): MatchCacheResult<PlanningCache | LocateCache> | undefined;
    matchPlanCache(prompt: string): MatchCacheResult<PlanningCache> | undefined;
    matchLocateCache(prompt: string): MatchCacheResult<LocateCache> | undefined;
    appendCache(cache: PlanningCache | LocateCache): void;
    loadCacheFromFile(): CacheFileContent | undefined;
    flushCacheToFile(): void;
    updateOrAppendCacheRecord(newRecord: PlanningCache | LocateCache, cachedRecord?: MatchCacheResult<PlanningCache | LocateCache>): void;
}

interface ExecutionResult<OutputType = any> {
    output: OutputType;
    executor: Executor;
}
declare class PageTaskExecutor {
    page: WebPage;
    insight: Insight<WebElementInfo, WebUIContext>;
    taskCache?: TaskCache;
    conversationHistory: ChatCompletionMessageParam[];
    onTaskStartCallback?: ExecutionTaskProgressOptions['onTaskStart'];
    constructor(page: WebPage, insight: Insight<WebElementInfo, WebUIContext>, opts: {
        taskCache?: TaskCache;
        onTaskStart?: ExecutionTaskProgressOptions['onTaskStart'];
    });
    private recordScreenshot;
    private getElementXpath;
    private prependExecutorWithScreenshot;
    convertPlanToExecutable(plans: PlanningAction[], opts?: {
        cacheable?: boolean;
    }): Promise<{
        tasks: ExecutionTaskApply<any, any, any, any>[];
    }>;
    private setupPlanningContext;
    loadYamlFlowAsPlanning(userInstruction: string, yamlString: string): Promise<{
        executor: Executor;
    }>;
    private planningTaskFromPrompt;
    private planningTaskToGoal;
    runPlans(title: string, plans: PlanningAction[], opts?: {
        cacheable?: boolean;
    }): Promise<ExecutionResult>;
    action(userPrompt: string, actionContext?: string, opts?: {
        cacheable?: boolean;
    }): Promise<ExecutionResult<{
        yamlFlow?: MidsceneYamlFlowItem[];
    } | undefined>>;
    actionToGoal(userPrompt: string, opts?: {
        cacheable?: boolean;
    }): Promise<ExecutionResult<{
        yamlFlow?: MidsceneYamlFlowItem[];
    } | undefined>>;
    private createTypeQueryTask;
    query(demand: InsightExtractParam): Promise<ExecutionResult>;
    boolean(prompt: string): Promise<ExecutionResult<boolean>>;
    number(prompt: string): Promise<ExecutionResult<number>>;
    string(prompt: string): Promise<ExecutionResult<string>>;
    assert(assertion: string): Promise<ExecutionResult<InsightAssertionResponse>>;
    /**
     * Append a message to the conversation history
     * For user messages with images:
     * - Keep max 4 user image messages in history
     * - Remove oldest user image message when limit reached
     * For assistant messages:
     * - Simply append to history
     * @param conversationHistory Message to append
     */
    private appendConversationHistory;
    private appendErrorPlan;
    waitFor(assertion: string, opt: PlanningActionParamWaitFor): Promise<ExecutionResult<void>>;
}

interface PageAgentOpt {
    forceSameTabNavigation?: boolean;
    testId?: string;
    cacheId?: string;
    groupName?: string;
    groupDescription?: string;
    generateReport?: boolean;
    autoPrintReportMsg?: boolean;
    onTaskStartTip?: OnTaskStartTip;
    aiActionContext?: string;
    waitForNavigationTimeout?: number;
    waitForNetworkIdleTimeout?: number;
}
declare class PageAgent<PageType extends WebPage = WebPage> {
    page: PageType;
    insight: Insight<WebElementInfo, WebUIContext>;
    dump: GroupedActionDump;
    reportFile?: string | null;
    reportFileName?: string;
    taskExecutor: PageTaskExecutor;
    opts: PageAgentOpt;
    /**
     * If true, the agent will not perform any actions
     */
    dryMode: boolean;
    onTaskStartTip?: OnTaskStartTip;
    taskCache?: TaskCache;
    constructor(page: PageType, opts?: PageAgentOpt);
    getUIContext(action?: InsightAction): Promise<WebUIContext>;
    setAIActionContext(prompt: string): Promise<void>;
    resetDump(): GroupedActionDump;
    appendExecutionDump(execution: ExecutionDump): void;
    dumpDataString(): string;
    reportHTMLString(): string;
    writeOutActionDumps(): void;
    private callbackOnTaskStartTip;
    private afterTaskRunning;
    private buildDetailedLocateParam;
    aiTap(locatePrompt: string, opt?: LocateOption): Promise<any>;
    aiHover(locatePrompt: string, opt?: LocateOption): Promise<any>;
    aiInput(value: string, locatePrompt: string, opt?: LocateOption): Promise<any>;
    aiKeyboardPress(keyName: string, locatePrompt?: string, opt?: LocateOption): Promise<any>;
    aiScroll(scrollParam: PlanningActionParamScroll, locatePrompt?: string, opt?: LocateOption): Promise<any>;
    aiAction(taskPrompt: string, opt?: {
        cacheable?: boolean;
    }): Promise<{
        result: Record<string, any>;
    } | {
        yamlFlow?: _midscene_spp_core.MidsceneYamlFlowItem[];
    } | undefined>;
    aiQuery(demand: any): Promise<any>;
    aiBoolean(prompt: string): Promise<boolean>;
    aiNumber(prompt: string): Promise<number>;
    aiString(prompt: string): Promise<string>;
    describeElementAtPoint(center: [number, number], opt?: {
        verifyPrompt?: boolean;
        retryLimit?: number;
        deepThink?: boolean;
    } & LocatorValidatorOption): Promise<AgentDescribeElementAtPointResult>;
    verifyLocator(prompt: string, locateOpt: LocateOption | undefined, expectCenter: [number, number], verifyLocateOption?: LocatorValidatorOption): Promise<LocateValidatorResult>;
    aiLocate(prompt: string, opt?: LocateOption): Promise<Pick<LocateResultElement, "rect" | "center">>;
    aiAssert(assertion: string, msg?: string, opt?: AgentAssertOpt): Promise<_midscene_spp_core.InsightAssertionResponse | undefined>;
    aiWaitFor(assertion: string, opt?: AgentWaitForOpt): Promise<void>;
    ai(taskPrompt: string, type?: string): Promise<any>;
    runYaml(yamlScriptContent: string): Promise<{
        result: Record<string, any>;
    }>;
    evaluateJavaScript(script: string): Promise<any>;
    destroy(): Promise<void>;
}

export { PageAgent, type PageAgentOpt };
