{"version":3,"file":"agent/insight.mjs","sources":["../../../src/agent/insight.ts"],"sourcesContent":["import type { TUserPrompt } from '@/ai-model/index';\nimport type { ModelRuntime } from '@/ai-model/models';\nimport type {\n  AgentAssertResult,\n  AssertOptions,\n  InsightAPI,\n  QueryOptions,\n  ServiceExtractParam,\n  UIContext,\n} from '@/types';\nimport { TaskExecutionError, type TaskExecutor } from './tasks';\nimport { parsePrompt } from './utils';\n\nconst defaultQueryOptions: QueryOptions = {\n  domIncluded: false,\n  screenshotIncluded: true,\n};\n\ntype InsightTaskExecutor = Pick<TaskExecutor, 'createTypeQueryExecution'>;\n\n/** Execute read-only AI operations against either live or fixed UI context. */\nexport class Insight implements InsightAPI {\n  constructor(\n    private readonly taskExecutor: InsightTaskExecutor,\n    private readonly resolveModelRuntime: () => ModelRuntime,\n    private readonly getUIContext?: () => UIContext,\n  ) {}\n\n  private executionOptions(\n    options?: AssertOptions,\n    includeAbortSignal = false,\n  ): { abortSignal?: AbortSignal; uiContext?: UIContext } | undefined {\n    const uiContext = this.getUIContext?.();\n    if (!uiContext && !includeAbortSignal && !options?.abortSignal) {\n      return undefined;\n    }\n    return {\n      ...(includeAbortSignal || options?.abortSignal\n        ? { abortSignal: options?.abortSignal }\n        : {}),\n      ...(uiContext ? { uiContext } : {}),\n    };\n  }\n\n  async aiQuery<ReturnType = any>(\n    demand: ServiceExtractParam,\n    options: QueryOptions = defaultQueryOptions,\n  ): Promise<ReturnType> {\n    const modelRuntime = this.resolveModelRuntime();\n    const executionOptions = this.executionOptions();\n    const { output } = executionOptions\n      ? await this.taskExecutor.createTypeQueryExecution(\n          'Query',\n          demand,\n          modelRuntime,\n          options,\n          undefined,\n          executionOptions,\n        )\n      : await this.taskExecutor.createTypeQueryExecution(\n          'Query',\n          demand,\n          modelRuntime,\n          options,\n        );\n    return output as ReturnType;\n  }\n\n  async aiBoolean(\n    prompt: TUserPrompt,\n    options: QueryOptions = defaultQueryOptions,\n  ): Promise<boolean> {\n    return this.queryPrimitive('Boolean', prompt, options);\n  }\n\n  async aiNumber(\n    prompt: TUserPrompt,\n    options: QueryOptions = defaultQueryOptions,\n  ): Promise<number> {\n    return this.queryPrimitive('Number', prompt, options);\n  }\n\n  async aiString(\n    prompt: TUserPrompt,\n    options: QueryOptions = defaultQueryOptions,\n  ): Promise<string> {\n    return this.queryPrimitive('String', prompt, options);\n  }\n\n  async aiAsk(\n    prompt: TUserPrompt,\n    options: QueryOptions = defaultQueryOptions,\n  ): Promise<string> {\n    return this.aiString(prompt, options);\n  }\n\n  private async queryPrimitive<ReturnType>(\n    type: 'Boolean' | 'Number' | 'String',\n    prompt: TUserPrompt,\n    options: QueryOptions,\n  ): Promise<ReturnType> {\n    const { textPrompt, multimodalPrompt } = parsePrompt(prompt);\n    const { output } = await this.taskExecutor.createTypeQueryExecution(\n      type,\n      textPrompt,\n      this.resolveModelRuntime(),\n      options,\n      multimodalPrompt,\n      this.executionOptions(),\n    );\n    return output as ReturnType;\n  }\n\n  async aiAssert(\n    assertion: TUserPrompt,\n    message?: string,\n    options?: AssertOptions,\n  ): Promise<AgentAssertResult | undefined> {\n    const serviceOptions: QueryOptions = {\n      domIncluded: options?.domIncluded ?? defaultQueryOptions.domIncluded,\n      screenshotIncluded:\n        options?.screenshotIncluded ?? defaultQueryOptions.screenshotIncluded,\n      ...(options?.context !== undefined ? { context: options.context } : {}),\n    };\n    const { textPrompt, multimodalPrompt } = parsePrompt(assertion);\n    const assertionText =\n      typeof assertion === 'string' ? assertion : assertion.prompt;\n\n    try {\n      const { output, thought } =\n        await this.taskExecutor.createTypeQueryExecution<boolean>(\n          'Assert',\n          textPrompt,\n          this.resolveModelRuntime(),\n          serviceOptions,\n          multimodalPrompt,\n          this.executionOptions(options, true),\n        );\n\n      const pass = Boolean(output);\n      const failureMessage = pass\n        ? undefined\n        : `Assertion failed: ${message || assertionText}\\nReason: ${thought || '(no_reason)'}`;\n\n      if (options?.keepRawResponse) {\n        return { pass, thought, message: failureMessage };\n      }\n      if (!pass) {\n        throw new Error(failureMessage);\n      }\n    } catch (error) {\n      if (error instanceof TaskExecutionError) {\n        const thought = error.task?.thought;\n        const diagnosticMessage =\n          error.task?.errorMessage || error.cause.message;\n        const reason = thought || diagnosticMessage || '(no_reason)';\n        const failureMessage = `Assertion failed: ${message || assertionText}\\nReason: ${reason}`;\n\n        if (options?.keepRawResponse) {\n          return { pass: false, thought, message: failureMessage };\n        }\n        throw new Error(failureMessage, { cause: error.cause });\n      }\n      throw error;\n    }\n  }\n}\n"],"names":["defaultQueryOptions","Insight","options","includeAbortSignal","uiContext","demand","modelRuntime","executionOptions","output","undefined","prompt","type","textPrompt","multimodalPrompt","parsePrompt","assertion","message","serviceOptions","assertionText","thought","pass","Boolean","failureMessage","Error","error","TaskExecutionError","diagnosticMessage","reason","taskExecutor","resolveModelRuntime","getUIContext"],"mappings":";;;;;;;;;;;;AAaA,MAAMA,sBAAoC;IACxC,aAAa;IACb,oBAAoB;AACtB;AAKO,MAAMC;IAOH,iBACNC,OAAuB,EACvBC,qBAAqB,KAAK,EACwC;QAClE,MAAMC,YAAY,IAAI,CAAC,YAAY;QACnC,IAAI,CAACA,aAAa,CAACD,sBAAsB,CAACD,SAAS,aACjD;QAEF,OAAO;YACL,GAAIC,sBAAsBD,SAAS,cAC/B;gBAAE,aAAaA,SAAS;YAAY,IACpC,CAAC,CAAC;YACN,GAAIE,YAAY;gBAAEA;YAAU,IAAI,CAAC,CAAC;QACpC;IACF;IAEA,MAAM,QACJC,MAA2B,EAC3BH,UAAwBF,mBAAmB,EACtB;QACrB,MAAMM,eAAe,IAAI,CAAC,mBAAmB;QAC7C,MAAMC,mBAAmB,IAAI,CAAC,gBAAgB;QAC9C,MAAM,EAAEC,MAAM,EAAE,GAAGD,mBACf,MAAM,IAAI,CAAC,YAAY,CAAC,wBAAwB,CAC9C,SACAF,QACAC,cACAJ,SACAO,QACAF,oBAEF,MAAM,IAAI,CAAC,YAAY,CAAC,wBAAwB,CAC9C,SACAF,QACAC,cACAJ;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