import {
  getVersion
} from "./chunk-3SSU3OAB.js";
import {
  AiAssert,
  AiExtractElementInfo,
  AiLocateElement,
  AiLocateSection,
  callAiFn,
  callToGetJSONObject,
  describeUserPage,
  expandSearchArea,
  plan
} from "./chunk-ZPVQ5TDQ.js";

// src/ai-model/action-executor.ts
import { MIDSCENE_MODEL_NAME, getAIConfig } from "@midscene_spp/shared/env";
import { assert } from "@midscene_spp/shared/utils";
var Executor = class {
  constructor(name, options) {
    this.status = options?.tasks && options.tasks.length > 0 ? "pending" : "init";
    this.name = name;
    this.tasks = (options?.tasks || []).map(
      (item) => this.markTaskAsPending(item)
    );
    this.onTaskStart = options?.onTaskStart;
  }
  markTaskAsPending(task) {
    return {
      status: "pending",
      ...task
    };
  }
  async append(task) {
    assert(
      this.status !== "error",
      `executor is in error state, cannot append task
error=${this.latestErrorTask()?.error}
${this.latestErrorTask()?.errorStack}`
    );
    if (Array.isArray(task)) {
      this.tasks.push(...task.map((item) => this.markTaskAsPending(item)));
    } else {
      this.tasks.push(this.markTaskAsPending(task));
    }
    if (this.status !== "running") {
      this.status = "pending";
    }
  }
  async flush() {
    if (this.status === "init" && this.tasks.length > 0) {
      console.warn(
        "illegal state for executor, status is init but tasks are not empty"
      );
    }
    assert(this.status !== "running", "executor is already running");
    assert(this.status !== "completed", "executor is already completed");
    assert(this.status !== "error", "executor is in error state");
    const nextPendingIndex = this.tasks.findIndex(
      (task) => task.status === "pending"
    );
    if (nextPendingIndex < 0) {
      return;
    }
    this.status = "running";
    let taskIndex = nextPendingIndex;
    let successfullyCompleted = true;
    let previousFindOutput;
    while (taskIndex < this.tasks.length) {
      const task = this.tasks[taskIndex];
      assert(
        task.status === "pending",
        `task status should be pending, but got: ${task.status}`
      );
      task.timing = {
        start: Date.now()
      };
      try {
        task.status = "running";
        try {
          if (this.onTaskStart) {
            await this.onTaskStart(task);
          }
        } catch (e) {
          console.error("error in onTaskStart", e);
        }
        assert(
          ["Insight", "Action", "Planning"].indexOf(task.type) >= 0,
          `unsupported task type: ${task.type}`
        );
        const { executor, param } = task;
        assert(executor, `executor is required for task type: ${task.type}`);
        let returnValue;
        const executorContext = {
          task,
          element: previousFindOutput?.element
        };
        if (task.type === "Insight") {
          assert(
            task.subType === "Locate" || task.subType === "Query" || task.subType === "Assert" || task.subType === "Boolean" || task.subType === "Number" || task.subType === "String",
            `unsupported insight subType: ${task.subType}`
          );
          returnValue = await task.executor(param, executorContext);
          if (task.subType === "Locate") {
            previousFindOutput = returnValue?.output;
          }
        } else if (task.type === "Action" || task.type === "Planning") {
          returnValue = await task.executor(param, executorContext);
        } else {
          console.warn(
            `unsupported task type: ${task.type}, will try to execute it directly`
          );
          returnValue = await task.executor(param, executorContext);
        }
        Object.assign(task, returnValue);
        task.status = "finished";
        task.timing.end = Date.now();
        task.timing.cost = task.timing.end - task.timing.start;
        task.timing.aiCost = returnValue?.aiCost || 0;
        taskIndex++;
      } catch (e) {
        successfullyCompleted = false;
        task.error = e?.message || (typeof e === "string" ? e : "error-without-message");
        task.errorStack = e.stack;
        task.status = "failed";
        task.timing.end = Date.now();
        task.timing.cost = task.timing.end - task.timing.start;
        break;
      }
    }
    for (let i = taskIndex + 1; i < this.tasks.length; i++) {
      this.tasks[i].status = "cancelled";
    }
    if (successfullyCompleted) {
      this.status = "completed";
    } else {
      this.status = "error";
    }
    if (this.tasks.length) {
      const outputIndex = Math.min(taskIndex, this.tasks.length - 1);
      return this.tasks[outputIndex].output;
    }
  }
  isInErrorState() {
    return this.status === "error";
  }
  latestErrorTask() {
    if (this.status !== "error") {
      return null;
    }
    const errorTaskIndex = this.tasks.findIndex(
      (task) => task.status === "failed"
    );
    if (errorTaskIndex >= 0) {
      return this.tasks[errorTaskIndex];
    }
    return null;
  }
  dump() {
    const dumpData = {
      sdkVersion: getVersion(),
      model_name: getAIConfig(MIDSCENE_MODEL_NAME) || "",
      logTime: Date.now(),
      name: this.name,
      tasks: this.tasks
    };
    return dumpData;
  }
};

// src/ai-model/prompt/describe.ts
import { getPreferredLanguage } from "@midscene_spp/shared/env";
var elementDescriberInstruction = () => {
  return `Tell what is the content of the element wrapped by the read rectangle in the screenshot. Your description is expected to be used to precisely locate the element from other similar elements on screenshot. Use ${getPreferredLanguage()} in the description.

Please follow the following rules:
1. The description should be start with a brief description, like "a button for confirming the action".

2. Include these information in the description to distinguish the element from its siblings and other similar elements, as much as possible:
- The text of the element, like "with text 'Confirm'"
- What the element looks like if it's an image, like "with image '...'"
- The relative position of the element, like "on the left of ..., around ..."
- How to distinguish the element from its siblings elements, like "it is the icon instead of the text"

3. Do NOT mention the red rectangle in the description.

4. Use the error field to describe the unexpected situations, if any. If not, put null.

Return in JSON:
{
  "description": "[{brief description}]: {text of the element} {image of the element} {relative position of the element} ... ",
  "error"?: "..."
}`;
};

// src/insight/index.ts
import {
  MIDSCENE_FORCE_DEEP_THINK,
  MIDSCENE_USE_QWEN_VL,
  getAIConfigInBoolean,
  vlLocateMode as vlLocateMode2
} from "@midscene_spp/shared/env";
import { compositeElementInfoImg, cropByRect } from "@midscene_spp/shared/img";
import { getDebug } from "@midscene_spp/shared/logger";
import { assert as assert2 } from "@midscene_spp/shared/utils";

// src/insight/utils.ts
import {
  MIDSCENE_MODEL_NAME as MIDSCENE_MODEL_NAME2,
  getAIConfig as getAIConfig2,
  uiTarsModelVersion,
  vlLocateMode
} from "@midscene_spp/shared/env";
import { uuid } from "@midscene_spp/shared/utils";
function emitInsightDump(data, dumpSubscriber) {
  let modelDescription = "";
  if (vlLocateMode()) {
    const uiTarsModelVer = uiTarsModelVersion();
    if (uiTarsModelVer) {
      modelDescription = `UI-TARS=${uiTarsModelVer}`;
    } else {
      modelDescription = `${vlLocateMode()} mode`;
    }
  }
  const baseData = {
    sdkVersion: getVersion(),
    logTime: Date.now(),
    model_name: getAIConfig2(MIDSCENE_MODEL_NAME2) || "",
    model_description: modelDescription
  };
  const finalData = {
    logId: uuid(),
    ...baseData,
    ...data
  };
  dumpSubscriber?.(finalData);
}

// src/insight/index.ts
var debug = getDebug("ai:insight");
var Insight = class {
  constructor(context, opt) {
    this.aiVendorFn = callAiFn;
    assert2(context, "context is required for Insight");
    if (typeof context === "function") {
      this.contextRetrieverFn = context;
    } else {
      this.contextRetrieverFn = () => Promise.resolve(context);
    }
    if (typeof opt?.aiVendorFn !== "undefined") {
      this.aiVendorFn = opt.aiVendorFn;
    }
    if (typeof opt?.taskInfo !== "undefined") {
      this.taskInfo = opt.taskInfo;
    }
  }
  async locate(query, opt) {
    const { callAI } = opt || {};
    const queryPrompt = typeof query === "string" ? query : query.prompt;
    assert2(queryPrompt, "query is required for locate");
    const dumpSubscriber = this.onceDumpUpdatedFn;
    this.onceDumpUpdatedFn = void 0;
    assert2(typeof query === "object", "query should be an object for locate");
    const globalDeepThinkSwitch = getAIConfigInBoolean(
      MIDSCENE_FORCE_DEEP_THINK
    );
    if (globalDeepThinkSwitch) {
      debug("globalDeepThinkSwitch", globalDeepThinkSwitch);
    }
    let searchAreaPrompt;
    if (query.deepThink || globalDeepThinkSwitch) {
      searchAreaPrompt = query.prompt;
    }
    if (searchAreaPrompt && !vlLocateMode2()) {
      console.warn(
        'The "deepThink" feature is not supported with multimodal LLM. Please config VL model for Midscene. https://midscenejs.com/choose-a-model'
      );
      searchAreaPrompt = void 0;
    }
    const context = opt?.context || await this.contextRetrieverFn("locate");
    let searchArea = void 0;
    let searchAreaRawResponse = void 0;
    let searchAreaUsage = void 0;
    let searchAreaResponse = void 0;
    if (searchAreaPrompt) {
      searchAreaResponse = await AiLocateSection({
        context,
        sectionDescription: searchAreaPrompt
      });
      assert2(
        searchAreaResponse.rect,
        `cannot find search area for "${searchAreaPrompt}"${searchAreaResponse.error ? `: ${searchAreaResponse.error}` : ""}`
      );
      searchAreaRawResponse = searchAreaResponse.rawResponse;
      searchAreaUsage = searchAreaResponse.usage;
      searchArea = searchAreaResponse.rect;
    }
    const startTime = Date.now();
    const { parseResult, rect, elementById, rawResponse, usage } = await AiLocateElement({
      callAI: callAI || this.aiVendorFn,
      context,
      targetElementDescription: queryPrompt,
      searchConfig: searchAreaResponse
    });
    const timeCost = Date.now() - startTime;
    const taskInfo = {
      ...this.taskInfo ? this.taskInfo : {},
      durationMs: timeCost,
      rawResponse: JSON.stringify(rawResponse),
      formatResponse: JSON.stringify(parseResult),
      usage,
      searchArea,
      searchAreaRawResponse,
      searchAreaUsage
    };
    let errorLog;
    if (parseResult.errors?.length) {
      errorLog = `AI model failed to locate: 
${parseResult.errors.join("\n")}`;
    }
    const dumpData = {
      type: "locate",
      userQuery: {
        element: queryPrompt
      },
      matchedElement: [],
      matchedRect: rect,
      data: null,
      taskInfo,
      deepThink: !!searchArea,
      error: errorLog
    };
    const elements = [];
    (parseResult.elements || []).forEach((item) => {
      if ("id" in item) {
        const element = elementById(item?.id);
        if (!element) {
          console.warn(
            `locate: cannot find element id=${item.id}. Maybe an unstable response from AI model`
          );
          return;
        }
        elements.push(element);
      }
    });
    emitInsightDump(
      {
        ...dumpData,
        matchedElement: elements
      },
      dumpSubscriber
    );
    if (errorLog) {
      throw new Error(errorLog);
    }
    assert2(
      elements.length <= 1,
      `locate: multiple elements found, length = ${elements.length}`
    );
    if (elements.length === 1) {
      return {
        element: {
          id: elements[0].id,
          indexId: elements[0].indexId,
          center: elements[0].center,
          rect: elements[0].rect,
          xpaths: elements[0].xpaths || [],
          attributes: elements[0].attributes
        },
        rect
      };
    }
    return {
      element: null,
      rect
    };
  }
  async extract(dataDemand) {
    assert2(
      typeof dataDemand === "object" || typeof dataDemand === "string",
      `dataDemand should be object or string, but get ${typeof dataDemand}`
    );
    const dumpSubscriber = this.onceDumpUpdatedFn;
    this.onceDumpUpdatedFn = void 0;
    const context = await this.contextRetrieverFn("extract");
    const startTime = Date.now();
    const { parseResult, usage } = await AiExtractElementInfo({
      context,
      dataQuery: dataDemand
    });
    const timeCost = Date.now() - startTime;
    const taskInfo = {
      ...this.taskInfo ? this.taskInfo : {},
      durationMs: timeCost,
      rawResponse: JSON.stringify(parseResult)
    };
    let errorLog;
    if (parseResult.errors?.length) {
      errorLog = `AI response error: 
${parseResult.errors.join("\n")}`;
    }
    const dumpData = {
      type: "extract",
      userQuery: {
        dataDemand
      },
      matchedElement: [],
      data: null,
      taskInfo,
      error: errorLog
    };
    const { data } = parseResult || {};
    emitInsightDump(
      {
        ...dumpData,
        data
      },
      dumpSubscriber
    );
    if (errorLog && !data) {
      throw new Error(errorLog);
    }
    return {
      data,
      usage
    };
  }
  async assert(assertion) {
    if (typeof assertion !== "string") {
      throw new Error(
        "This is the assert method for Midscene, the first argument should be a string. If you want to use the assert method from Node.js, please import it from the Node.js assert module."
      );
    }
    const dumpSubscriber = this.onceDumpUpdatedFn;
    this.onceDumpUpdatedFn = void 0;
    const context = await this.contextRetrieverFn("assert");
    const startTime = Date.now();
    const assertResult = await AiAssert({
      assertion,
      context
    });
    const timeCost = Date.now() - startTime;
    const taskInfo = {
      ...this.taskInfo ? this.taskInfo : {},
      durationMs: timeCost,
      rawResponse: JSON.stringify(assertResult.content)
    };
    const { thought, pass } = assertResult.content;
    const dumpData = {
      type: "assert",
      userQuery: {
        assertion
      },
      matchedElement: [],
      data: null,
      taskInfo,
      assertionPass: pass,
      assertionThought: thought,
      error: pass ? void 0 : thought
    };
    emitInsightDump(dumpData, dumpSubscriber);
    return {
      pass,
      thought,
      usage: assertResult.usage
    };
  }
  async describe(target, opt) {
    assert2(target, "target is required for insight.describe");
    const context = await this.contextRetrieverFn("describe");
    const { screenshotBase64 } = context;
    assert2(screenshotBase64, "screenshot is required for insight.describe");
    const systemPrompt = elementDescriberInstruction();
    const defaultRectSize = 30;
    const targetRect = Array.isArray(target) ? {
      left: Math.floor(target[0] - defaultRectSize / 2),
      top: Math.floor(target[1] - defaultRectSize / 2),
      width: defaultRectSize,
      height: defaultRectSize
    } : target;
    let imagePayload = await compositeElementInfoImg({
      inputImgBase64: screenshotBase64,
      elementsPositionInfo: [
        {
          rect: targetRect
        }
      ],
      borderThickness: 3
    });
    if (opt?.deepThink) {
      const searchArea = expandSearchArea(targetRect, context.size);
      debug("describe: set searchArea", searchArea);
      imagePayload = await cropByRect(
        imagePayload,
        searchArea,
        getAIConfigInBoolean(MIDSCENE_USE_QWEN_VL)
      );
    }
    const msgs = [
      { role: "system", content: systemPrompt },
      {
        role: "user",
        content: [
          {
            type: "image_url",
            image_url: {
              url: imagePayload,
              detail: "high"
            }
          }
        ]
      }
    ];
    const callAIFn = this.aiVendorFn || callToGetJSONObject;
    const res = await callAIFn(msgs, 4 /* DESCRIBE_ELEMENT */);
    const { content } = res;
    assert2(!content.error, `describe failed: ${content.error}`);
    assert2(content.description, "failed to describe the element");
    return content;
  }
};

// src/index.ts
import { getAIConfig as getAIConfig3, MIDSCENE_MODEL_NAME as MIDSCENE_MODEL_NAME3 } from "@midscene_spp/shared/env";
var src_default = Insight;
export {
  AiAssert,
  AiLocateElement,
  Executor,
  Insight,
  MIDSCENE_MODEL_NAME3 as MIDSCENE_MODEL_NAME,
  src_default as default,
  describeUserPage,
  getAIConfig3 as getAIConfig,
  getVersion,
  plan
};

//# sourceMappingURL=index.js.map