@midscene/core 1.8.0 → 1.8.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. package/dist/es/agent/agent.mjs +2 -3
  2. package/dist/es/agent/agent.mjs.map +1 -1
  3. package/dist/es/agent/task-builder.mjs +2 -1
  4. package/dist/es/agent/task-builder.mjs.map +1 -1
  5. package/dist/es/agent/tasks.mjs +4 -3
  6. package/dist/es/agent/tasks.mjs.map +1 -1
  7. package/dist/es/agent/usage-intent.mjs +18 -0
  8. package/dist/es/agent/usage-intent.mjs.map +1 -0
  9. package/dist/es/agent/utils.mjs +1 -1
  10. package/dist/es/ai-model/service-caller/codex-app-server.mjs +2 -1
  11. package/dist/es/ai-model/service-caller/codex-app-server.mjs.map +1 -1
  12. package/dist/es/ai-model/service-caller/index.mjs +3 -2
  13. package/dist/es/ai-model/service-caller/index.mjs.map +1 -1
  14. package/dist/es/device/index.mjs +169 -29
  15. package/dist/es/device/index.mjs.map +1 -1
  16. package/dist/es/types.mjs.map +1 -1
  17. package/dist/es/utils.mjs +2 -2
  18. package/dist/lib/agent/agent.js +2 -3
  19. package/dist/lib/agent/agent.js.map +1 -1
  20. package/dist/lib/agent/task-builder.js +2 -1
  21. package/dist/lib/agent/task-builder.js.map +1 -1
  22. package/dist/lib/agent/tasks.js +4 -3
  23. package/dist/lib/agent/tasks.js.map +1 -1
  24. package/dist/lib/agent/usage-intent.js +52 -0
  25. package/dist/lib/agent/usage-intent.js.map +1 -0
  26. package/dist/lib/agent/utils.js +1 -1
  27. package/dist/lib/ai-model/service-caller/codex-app-server.js +2 -1
  28. package/dist/lib/ai-model/service-caller/codex-app-server.js.map +1 -1
  29. package/dist/lib/ai-model/service-caller/index.js +3 -2
  30. package/dist/lib/ai-model/service-caller/index.js.map +1 -1
  31. package/dist/lib/device/index.js +174 -28
  32. package/dist/lib/device/index.js.map +1 -1
  33. package/dist/lib/types.js +2 -2
  34. package/dist/lib/types.js.map +1 -1
  35. package/dist/lib/utils.js +2 -2
  36. package/dist/types/agent/usage-intent.d.ts +3 -0
  37. package/dist/types/device/index.d.ts +157 -19
  38. package/dist/types/types.d.ts +8 -0
  39. package/package.json +2 -2
@@ -1 +1 @@
1
- {"version":3,"file":"ai-model/service-caller/index.mjs","sources":["../../../../src/ai-model/service-caller/index.ts"],"sourcesContent":["import type { AIUsageInfo } from '@/types';\nimport type { CodeGenerationChunk, StreamingCallback } from '@/types';\n\n// Error class that preserves usage and rawResponse when AI call parsing fails\nexport class AIResponseParseError extends Error {\n usage?: AIUsageInfo;\n rawResponse: string;\n\n constructor(message: string, rawResponse: string, usage?: AIUsageInfo) {\n super(message);\n this.name = 'AIResponseParseError';\n this.rawResponse = rawResponse;\n this.usage = usage;\n }\n}\nimport {\n type IModelConfig,\n MIDSCENE_LANGFUSE_DEBUG,\n MIDSCENE_LANGSMITH_DEBUG,\n MIDSCENE_MODEL_MAX_TOKENS,\n OPENAI_MAX_TOKENS,\n type TModelFamily,\n type UITarsModelVersion,\n globalConfigManager,\n} from '@midscene/shared/env';\n\nimport { getDebug } from '@midscene/shared/logger';\nimport { assert, ifInBrowser } from '@midscene/shared/utils';\nimport { jsonrepair } from 'jsonrepair';\nimport OpenAI from 'openai';\nimport type { ChatCompletionMessageParam } from 'openai/resources/index';\nimport type { Stream } from 'openai/streaming';\nimport type { AIArgs } from '../../common';\nimport { isAutoGLM, isUITars } from '../auto-glm/util';\nimport {\n callAIWithCodexAppServer,\n isCodexAppServerProvider,\n} from './codex-app-server';\nimport { shouldForceOriginalImageDetail } from './image-detail';\nimport {\n buildRequestAbortSignal,\n isHardTimeoutError,\n resolveEffectiveTimeoutMs,\n} from './request-timeout';\n\nasync function createChatClient({\n modelConfig,\n}: {\n modelConfig: IModelConfig;\n}): Promise<{\n completion: OpenAI.Chat.Completions;\n modelName: string;\n modelDescription: string;\n uiTarsModelVersion?: UITarsModelVersion;\n modelFamily: TModelFamily | undefined;\n}> {\n const {\n socksProxy,\n httpProxy,\n modelName,\n openaiBaseURL,\n openaiApiKey,\n openaiExtraConfig,\n modelDescription,\n uiTarsModelVersion,\n modelFamily,\n createOpenAIClient,\n timeout,\n } = modelConfig;\n\n let proxyAgent: any = undefined;\n const warnClient = getDebug('ai:call', { console: true });\n const debugProxy = getDebug('ai:call:proxy');\n const warnProxy = getDebug('ai:call:proxy', { console: true });\n\n // Helper function to sanitize proxy URL for logging (remove credentials)\n // Uses URL API instead of regex to avoid ReDoS vulnerabilities\n const sanitizeProxyUrl = (url: string): string => {\n try {\n const parsed = new URL(url);\n if (parsed.username) {\n // Keep username for debugging, hide password for security\n parsed.password = '****';\n return parsed.href;\n }\n return url;\n } catch {\n // If URL parsing fails, return original URL (will be caught later)\n return url;\n }\n };\n\n if (httpProxy) {\n debugProxy('using http proxy', sanitizeProxyUrl(httpProxy));\n if (ifInBrowser) {\n warnProxy(\n 'HTTP proxy is configured but not supported in browser environment',\n );\n } else {\n // Dynamic import with variable to avoid bundler static analysis\n const moduleName = 'undici';\n const { ProxyAgent } = await import(moduleName);\n proxyAgent = new ProxyAgent({\n uri: httpProxy,\n // Note: authentication is handled via the URI (e.g., http://user:pass@proxy.com:8080)\n });\n }\n } else if (socksProxy) {\n debugProxy('using socks proxy', sanitizeProxyUrl(socksProxy));\n if (ifInBrowser) {\n warnProxy(\n 'SOCKS proxy is configured but not supported in browser environment',\n );\n } else {\n try {\n // Dynamic import with variable to avoid bundler static analysis\n const moduleName = 'fetch-socks';\n const { socksDispatcher } = await import(moduleName);\n // Parse SOCKS proxy URL (e.g., socks5://127.0.0.1:1080)\n const proxyUrl = new URL(socksProxy);\n\n // Validate hostname\n if (!proxyUrl.hostname) {\n throw new Error('SOCKS proxy URL must include a valid hostname');\n }\n\n // Validate and parse port\n const port = Number.parseInt(proxyUrl.port, 10);\n if (!proxyUrl.port || Number.isNaN(port)) {\n throw new Error('SOCKS proxy URL must include a valid port');\n }\n\n // Parse SOCKS version from protocol\n const protocol = proxyUrl.protocol.replace(':', '');\n const socksType =\n protocol === 'socks4' ? 4 : protocol === 'socks5' ? 5 : 5;\n\n proxyAgent = socksDispatcher({\n type: socksType,\n host: proxyUrl.hostname,\n port,\n ...(proxyUrl.username\n ? {\n userId: decodeURIComponent(proxyUrl.username),\n password: decodeURIComponent(proxyUrl.password || ''),\n }\n : {}),\n });\n debugProxy('socks proxy configured successfully', {\n type: socksType,\n host: proxyUrl.hostname,\n port: port,\n });\n } catch (error) {\n warnProxy('Failed to configure SOCKS proxy:', error);\n throw new Error(\n `Invalid SOCKS proxy URL: ${socksProxy}. Expected format: socks4://host:port, socks5://host:port, or with authentication: socks5://user:pass@host:port`,\n );\n }\n }\n }\n\n const effectiveTimeoutMs = resolveEffectiveTimeoutMs({ timeout });\n const openAIOptions = {\n baseURL: openaiBaseURL,\n apiKey: openaiApiKey,\n // Use fetchOptions.dispatcher for fetch-based SDK instead of httpAgent\n // Note: Type assertion needed due to undici version mismatch between dependencies\n ...(proxyAgent ? { fetchOptions: { dispatcher: proxyAgent as any } } : {}),\n ...openaiExtraConfig,\n // Midscene already handles retries in callAI(), so disable SDK-level retries\n // to avoid duplicate attempts and duplicated backoff latency.\n maxRetries: 0,\n // When disabled (timeoutMs === null) fall through to the SDK default so\n // only the caller-provided abortSignal can cancel the request.\n ...(effectiveTimeoutMs !== null ? { timeout: effectiveTimeoutMs } : {}),\n dangerouslyAllowBrowser: true,\n };\n\n const baseOpenAI = new OpenAI(openAIOptions);\n\n let openai: OpenAI = baseOpenAI;\n\n // LangSmith wrapper\n if (\n openai &&\n globalConfigManager.getEnvConfigInBoolean(MIDSCENE_LANGSMITH_DEBUG)\n ) {\n if (ifInBrowser) {\n throw new Error('langsmith is not supported in browser');\n }\n warnClient('DEBUGGING MODE: langsmith wrapper enabled');\n // Use variable to prevent static analysis by bundlers\n const langsmithModule = 'langsmith/wrappers';\n const { wrapOpenAI } = await import(langsmithModule);\n openai = wrapOpenAI(openai);\n }\n\n // Langfuse wrapper\n if (\n openai &&\n globalConfigManager.getEnvConfigInBoolean(MIDSCENE_LANGFUSE_DEBUG)\n ) {\n if (ifInBrowser) {\n throw new Error('langfuse is not supported in browser');\n }\n warnClient('DEBUGGING MODE: langfuse wrapper enabled');\n // Use variable to prevent static analysis by bundlers\n const langfuseModule = '@langfuse/openai';\n const { observeOpenAI } = await import(langfuseModule);\n openai = observeOpenAI(openai);\n }\n\n if (createOpenAIClient) {\n const wrappedClient = await createOpenAIClient(baseOpenAI, openAIOptions);\n\n if (wrappedClient) {\n openai = wrappedClient as OpenAI;\n }\n }\n\n return {\n completion: openai.chat.completions,\n modelName,\n modelDescription,\n uiTarsModelVersion,\n modelFamily,\n };\n}\n\nexport async function callAI(\n messages: ChatCompletionMessageParam[],\n modelConfig: IModelConfig,\n options?: {\n stream?: boolean;\n onChunk?: StreamingCallback;\n abortSignal?: AbortSignal;\n },\n): Promise<{\n content: string;\n reasoning_content?: string;\n usage?: AIUsageInfo;\n isStreamed: boolean;\n}> {\n if (isCodexAppServerProvider(modelConfig.openaiBaseURL)) {\n if (\n !modelConfig.modelFamily &&\n hasExplicitReasoningConfig({\n reasoningEnabled: modelConfig.reasoningEnabled,\n reasoningEffort: modelConfig.reasoningEffort,\n reasoningBudget: modelConfig.reasoningBudget,\n })\n ) {\n throw new Error(\n 'Reasoning config requires MIDSCENE_MODEL_FAMILY. Set MIDSCENE_MODEL_FAMILY when using MIDSCENE_MODEL_REASONING_ENABLED / MIDSCENE_MODEL_REASONING_EFFORT / MIDSCENE_MODEL_REASONING_BUDGET.',\n );\n }\n\n return callAIWithCodexAppServer(messages, modelConfig, {\n stream: options?.stream,\n onChunk: options?.onChunk,\n reasoningEnabled: modelConfig.reasoningEnabled,\n abortSignal: options?.abortSignal,\n });\n }\n\n const {\n completion,\n modelName,\n modelDescription,\n uiTarsModelVersion,\n modelFamily,\n } = await createChatClient({\n modelConfig,\n });\n const effectiveTimeoutMs = resolveEffectiveTimeoutMs(modelConfig);\n\n const extraBody = modelConfig.extraBody;\n\n const maxTokens =\n globalConfigManager.getEnvConfigValueAsNumber(MIDSCENE_MODEL_MAX_TOKENS) ??\n globalConfigManager.getEnvConfigValueAsNumber(OPENAI_MAX_TOKENS);\n const debugCall = getDebug('ai:call');\n const warnCall = getDebug('ai:call', { console: true });\n const debugProfileStats = getDebug('ai:profile:stats');\n const debugProfileDetail = getDebug('ai:profile:detail');\n\n const startTime = Date.now();\n\n const temperature = (() => {\n if (modelFamily === 'gpt-5') {\n debugCall('temperature is ignored for gpt-5');\n return undefined;\n }\n return modelConfig.temperature ?? 0;\n })();\n\n const isStreaming = options?.stream && options?.onChunk;\n let content: string | undefined;\n let accumulated = '';\n let accumulatedReasoning = '';\n let usage: OpenAI.CompletionUsage | undefined;\n let timeCost: number | undefined;\n let requestId: string | null | undefined;\n\n const hasUsableText = (value: string | null | undefined): value is string =>\n typeof value === 'string' && value.trim().length > 0;\n\n const buildUsageInfo = (\n usageData?: OpenAI.CompletionUsage,\n requestId?: string | null,\n ) => {\n if (!usageData) return undefined;\n\n const cachedInputTokens = (\n usageData as { prompt_tokens_details?: { cached_tokens?: number } }\n )?.prompt_tokens_details?.cached_tokens;\n\n return {\n prompt_tokens: usageData.prompt_tokens ?? 0,\n completion_tokens: usageData.completion_tokens ?? 0,\n total_tokens: usageData.total_tokens ?? 0,\n cached_input: cachedInputTokens ?? 0,\n time_cost: timeCost ?? 0,\n model_name: modelName,\n model_description: modelDescription,\n intent: modelConfig.intent,\n request_id: requestId ?? undefined,\n } satisfies AIUsageInfo;\n };\n\n const commonConfig = {\n temperature,\n stream: !!isStreaming,\n max_tokens: maxTokens,\n ...(modelFamily === 'qwen2.5-vl' // qwen vl v2 specific config\n ? {\n vl_high_resolution_images: true,\n }\n : {}),\n };\n\n if (isAutoGLM(modelFamily)) {\n (commonConfig as unknown as Record<string, number>).top_p = 0.85;\n (commonConfig as unknown as Record<string, number>).frequency_penalty = 0.2;\n }\n\n const {\n config: reasoningEffortConfig,\n debugMessage: reasoningEffortDebugMessage,\n } = resolveReasoningConfig({\n reasoningEnabled: modelConfig.reasoningEnabled,\n reasoningEffort: modelConfig.reasoningEffort,\n reasoningBudget: modelConfig.reasoningBudget,\n modelFamily,\n });\n if (reasoningEffortDebugMessage) {\n debugCall(reasoningEffortDebugMessage);\n }\n\n const shouldUseOriginalImageDetail =\n shouldForceOriginalImageDetail(modelConfig);\n\n // For default-intent GPT-5 calls, request original image detail to preserve\n // screenshot resolution for localization-sensitive tasks.\n const messagesWithImageDetail: ChatCompletionMessageParam[] = (() => {\n if (!shouldUseOriginalImageDetail) {\n return messages;\n }\n\n return messages.map((msg) => {\n if (!Array.isArray(msg.content)) {\n return msg;\n }\n\n const content = msg.content.map((part) => {\n if (part && part.type === 'image_url' && part.image_url?.url) {\n return {\n ...part,\n image_url: {\n ...part.image_url,\n detail: 'original',\n },\n };\n }\n return part;\n });\n\n return {\n ...msg,\n content,\n } as ChatCompletionMessageParam;\n });\n })();\n\n try {\n debugCall(\n `sending ${isStreaming ? 'streaming ' : ''}request to ${modelName}`,\n );\n\n if (isStreaming) {\n const { signal: streamSignal, cleanup: cleanupStreamSignal } =\n buildRequestAbortSignal(effectiveTimeoutMs, options?.abortSignal);\n try {\n const stream = (await completion.create(\n {\n model: modelName,\n messages: messagesWithImageDetail,\n ...commonConfig,\n ...reasoningEffortConfig,\n ...extraBody,\n },\n {\n stream: true,\n signal: streamSignal,\n },\n )) as Stream<OpenAI.Chat.Completions.ChatCompletionChunk> & {\n _request_id?: string | null;\n };\n\n requestId = stream._request_id;\n\n for await (const chunk of stream) {\n const content = chunk.choices?.[0]?.delta?.content || '';\n const reasoning_content =\n (chunk.choices?.[0]?.delta as any)?.reasoning_content || '';\n\n // Check for usage info in any chunk (OpenAI provides usage in separate chunks)\n if (chunk.usage) {\n usage = chunk.usage;\n }\n\n if (content || reasoning_content) {\n accumulated += content;\n accumulatedReasoning += reasoning_content;\n const chunkData: CodeGenerationChunk = {\n content,\n reasoning_content,\n accumulated,\n isComplete: false,\n usage: undefined,\n };\n options.onChunk!(chunkData);\n }\n\n // Check if stream is complete\n if (chunk.choices?.[0]?.finish_reason) {\n timeCost = Date.now() - startTime;\n\n // If usage is not available from the stream, provide a basic usage info\n if (!usage) {\n // Estimate token counts based on content length (rough approximation)\n const estimatedTokens = Math.max(\n 1,\n Math.floor(accumulated.length / 4),\n );\n usage = {\n prompt_tokens: estimatedTokens,\n completion_tokens: estimatedTokens,\n total_tokens: estimatedTokens * 2,\n };\n }\n\n // Send final chunk\n const finalChunk: CodeGenerationChunk = {\n content: '',\n accumulated,\n reasoning_content: '',\n isComplete: true,\n usage: buildUsageInfo(usage, requestId),\n };\n options.onChunk!(finalChunk);\n break;\n }\n }\n } finally {\n cleanupStreamSignal();\n }\n content = accumulated;\n debugProfileStats(\n `streaming model, ${modelName}, mode, ${modelFamily || 'default'}, cost-ms, ${timeCost}, temperature, ${temperature ?? ''}`,\n );\n } else {\n // Non-streaming with retry logic\n const retryCount = modelConfig.retryCount ?? 1;\n const retryInterval = modelConfig.retryInterval ?? 2000;\n const maxAttempts = retryCount + 1; // retryCount=1 means 2 total attempts (1 initial + 1 retry)\n\n let lastError: Error | undefined;\n\n for (let attempt = 1; attempt <= maxAttempts; attempt++) {\n const { signal: attemptSignal, cleanup: cleanupAttemptSignal } =\n buildRequestAbortSignal(effectiveTimeoutMs, options?.abortSignal);\n try {\n const result = await completion.create(\n {\n model: modelName,\n messages: messagesWithImageDetail,\n ...commonConfig,\n ...reasoningEffortConfig,\n ...extraBody,\n } as any,\n { signal: attemptSignal },\n );\n\n timeCost = Date.now() - startTime;\n\n debugProfileStats(\n `model, ${modelName}, mode, ${modelFamily || 'default'}, ui-tars-version, ${uiTarsModelVersion}, prompt-tokens, ${result.usage?.prompt_tokens || ''}, completion-tokens, ${result.usage?.completion_tokens || ''}, total-tokens, ${result.usage?.total_tokens || ''}, cost-ms, ${timeCost}, requestId, ${result._request_id || ''}, temperature, ${temperature ?? ''}`,\n );\n\n debugProfileDetail(\n `model usage detail: ${JSON.stringify(result.usage)}`,\n );\n\n if (!result.choices) {\n throw new Error(\n `invalid response from LLM service: ${JSON.stringify(result)}`,\n );\n }\n\n content = result.choices[0].message.content!;\n accumulatedReasoning =\n (result.choices[0].message as any)?.reasoning_content || '';\n usage = result.usage;\n requestId = result._request_id;\n\n if (!hasUsableText(content) && hasUsableText(accumulatedReasoning)) {\n warnCall('empty content from AI model, using reasoning content');\n content = accumulatedReasoning;\n }\n\n if (!hasUsableText(content)) {\n throw new AIResponseParseError(\n 'empty content from AI model',\n JSON.stringify(result),\n buildUsageInfo(usage, requestId),\n );\n }\n\n break; // Success, exit retry loop\n } catch (error) {\n lastError = error as Error;\n const wasHardTimeout = isHardTimeoutError(lastError);\n if (wasHardTimeout) {\n warnCall(\n `AI call hit hard timeout (${effectiveTimeoutMs}ms, attempt ${attempt}/${maxAttempts}, model ${modelName}, intent ${modelConfig.intent})`,\n );\n }\n // Do not retry if the request was aborted by the caller\n if (options?.abortSignal?.aborted) {\n break;\n }\n if (attempt < maxAttempts) {\n warnCall(\n `AI call failed (attempt ${attempt}/${maxAttempts}), retrying in ${retryInterval}ms... Error: ${lastError.message}`,\n );\n await new Promise((resolve) => setTimeout(resolve, retryInterval));\n }\n } finally {\n cleanupAttemptSignal();\n }\n }\n\n if (!content) {\n throw lastError;\n }\n }\n\n debugCall(`response reasoning content: ${accumulatedReasoning}`);\n debugCall(`response content: ${content}`);\n\n // Ensure we always have usage info for streaming responses\n if (isStreaming && !usage) {\n // Estimate token counts based on content length (rough approximation)\n const estimatedTokens = Math.max(\n 1,\n Math.floor((content || '').length / 4),\n );\n usage = {\n prompt_tokens: estimatedTokens,\n completion_tokens: estimatedTokens,\n total_tokens: estimatedTokens * 2,\n } as OpenAI.CompletionUsage;\n }\n\n return {\n content: content || '',\n reasoning_content: accumulatedReasoning || undefined,\n usage: buildUsageInfo(usage, requestId),\n isStreamed: !!isStreaming,\n };\n } catch (e: any) {\n warnCall('call AI error', e);\n\n if (e instanceof AIResponseParseError) {\n throw e;\n }\n\n const newError = new Error(\n `failed to call ${isStreaming ? 'streaming ' : ''}AI model service (${modelName}): ${e.message}\\nTrouble shooting: https://midscenejs.com/model-provider.html`,\n {\n cause: e,\n },\n );\n throw newError;\n }\n}\n\nexport async function callAIWithObjectResponse<T>(\n messages: ChatCompletionMessageParam[],\n modelConfig: IModelConfig,\n options?: {\n abortSignal?: AbortSignal;\n },\n): Promise<{\n content: T;\n contentString: string;\n usage?: AIUsageInfo;\n reasoning_content?: string;\n}> {\n const response = await callAI(messages, modelConfig, {\n abortSignal: options?.abortSignal,\n });\n assert(response, 'empty response');\n const modelFamily = modelConfig.modelFamily;\n const jsonContent = safeParseJson(response.content, modelFamily);\n if (typeof jsonContent !== 'object') {\n throw new AIResponseParseError(\n `failed to parse json response from model (${modelConfig.modelName}): ${response.content}`,\n response.content,\n response.usage,\n );\n }\n return {\n content: jsonContent,\n contentString: response.content,\n usage: response.usage,\n reasoning_content: response.reasoning_content,\n };\n}\n\nexport async function callAIWithStringResponse(\n msgs: AIArgs,\n modelConfig: IModelConfig,\n options?: {\n abortSignal?: AbortSignal;\n },\n): Promise<{ content: string; usage?: AIUsageInfo }> {\n const { content, usage } = await callAI(msgs, modelConfig, {\n abortSignal: options?.abortSignal,\n });\n return { content, usage };\n}\n\nexport function extractJSONFromCodeBlock(response: string) {\n try {\n // First, try to match a JSON object directly in the response\n const jsonMatch = response.match(/^\\s*(\\{[\\s\\S]*\\})\\s*$/);\n if (jsonMatch) {\n return jsonMatch[1];\n }\n\n // If no direct JSON object is found, try to extract JSON from a code block\n const codeBlockMatch = response.match(\n /```(?:json)?\\s*(\\{[\\s\\S]*?\\})\\s*```/,\n );\n if (codeBlockMatch) {\n return codeBlockMatch[1];\n }\n\n // If no code block is found, try to find a JSON-like structure in the text\n const jsonLikeMatch = response.match(/\\{[\\s\\S]*\\}/);\n if (jsonLikeMatch) {\n return jsonLikeMatch[0];\n }\n } catch {}\n // If no JSON-like structure is found, return the original response\n return response;\n}\n\nexport function preprocessDoubaoBboxJson(input: string) {\n if (input.includes('bbox')) {\n // when its values like 940 445 969 490, replace all /\\d+\\s+\\d+/g with /$1,$2/g\n while (/\\d+\\s+\\d+/.test(input)) {\n input = input.replace(/(\\d+)\\s+(\\d+)/g, '$1,$2');\n }\n }\n return input;\n}\n\nfunction hasExplicitReasoningConfig({\n reasoningEnabled,\n reasoningEffort,\n reasoningBudget,\n}: {\n reasoningEnabled?: boolean;\n reasoningEffort?: string;\n reasoningBudget?: number;\n}): boolean {\n return (\n reasoningEnabled !== undefined ||\n !!reasoningEffort ||\n reasoningBudget !== undefined\n );\n}\n\nconst SUPPORTED_REASONING_FAMILIES = [\n 'qwen3-vl',\n 'qwen3.5',\n 'qwen3.6',\n 'doubao-vision',\n 'doubao-seed',\n 'glm-v',\n] as const satisfies readonly TModelFamily[];\n\ntype SupportedReasoningFamily = (typeof SUPPORTED_REASONING_FAMILIES)[number];\n\nfunction isSupportedReasoningFamily(\n family: TModelFamily | undefined,\n): family is SupportedReasoningFamily {\n return (\n !!family &&\n (SUPPORTED_REASONING_FAMILIES as readonly TModelFamily[]).includes(family)\n );\n}\n\nfunction supportedReasoningFamilyNames(): string {\n return SUPPORTED_REASONING_FAMILIES.join(', ');\n}\n\nexport function resolveReasoningConfig({\n reasoningEnabled,\n reasoningEffort,\n reasoningBudget,\n modelFamily,\n}: {\n reasoningEnabled?: boolean;\n reasoningEffort?: string;\n reasoningBudget?: number;\n modelFamily?: TModelFamily;\n}): {\n config: Record<string, unknown>;\n debugMessage?: string;\n} {\n const hasExplicitConfig = hasExplicitReasoningConfig({\n reasoningEnabled,\n reasoningEffort,\n reasoningBudget,\n });\n\n if (hasExplicitConfig) {\n if (!modelFamily) {\n throw new Error(\n `Reasoning config requires MIDSCENE_MODEL_FAMILY. Set MIDSCENE_MODEL_FAMILY to a supported family such as ${supportedReasoningFamilyNames()}, or remove MIDSCENE_MODEL_REASONING_ENABLED / MIDSCENE_MODEL_REASONING_EFFORT / MIDSCENE_MODEL_REASONING_BUDGET.`,\n );\n }\n\n // GPT-5 over Chat Completions is intentionally unsupported here because\n // its reasoning effort compatibility varies by model version.\n if (!isSupportedReasoningFamily(modelFamily)) {\n throw new Error(\n `Reasoning config is not supported for model family \"${modelFamily}\". Use a supported family such as ${supportedReasoningFamilyNames()}, or remove MIDSCENE_MODEL_REASONING_ENABLED / MIDSCENE_MODEL_REASONING_EFFORT / MIDSCENE_MODEL_REASONING_BUDGET.`,\n );\n }\n } else if (!isSupportedReasoningFamily(modelFamily)) {\n return { config: {} };\n }\n\n const effectiveReasoningEnabled = reasoningEnabled ?? false;\n\n const debugMessages: string[] = [];\n const config: Record<string, unknown> = {};\n\n if (\n modelFamily === 'qwen3-vl' ||\n modelFamily === 'qwen3.5' ||\n modelFamily === 'qwen3.6'\n ) {\n // reasoningEnabled → enable_thinking\n config.enable_thinking = effectiveReasoningEnabled;\n debugMessages.push(`enable_thinking=${effectiveReasoningEnabled}`);\n // reasoningBudget → thinking_budget\n if (reasoningBudget !== undefined) {\n config.thinking_budget = reasoningBudget;\n debugMessages.push(`thinking_budget=${reasoningBudget}`);\n }\n // reasoningEffort is ignored for qwen\n } else if (modelFamily === 'doubao-vision' || modelFamily === 'doubao-seed') {\n // reasoningEnabled → thinking.type\n config.thinking = {\n type: effectiveReasoningEnabled ? 'enabled' : 'disabled',\n };\n debugMessages.push(\n `thinking.type=${effectiveReasoningEnabled ? 'enabled' : 'disabled'}`,\n );\n // reasoningEffort → reasoning_effort\n if (reasoningEffort) {\n config.reasoning_effort = reasoningEffort;\n debugMessages.push(`reasoning_effort=\"${reasoningEffort}\"`);\n }\n // reasoningBudget is ignored for doubao\n } else if (modelFamily === 'glm-v') {\n // reasoningEnabled → thinking.type\n config.thinking = {\n type: effectiveReasoningEnabled ? 'enabled' : 'disabled',\n };\n debugMessages.push(\n `thinking.type=${effectiveReasoningEnabled ? 'enabled' : 'disabled'}`,\n );\n // reasoningEffort and reasoningBudget are ignored for glm-v\n }\n\n return {\n config,\n debugMessage: debugMessages.length\n ? `reasoning config for ${modelFamily}: ${debugMessages.join(', ')}`\n : undefined,\n };\n}\n\n/**\n * Normalize a parsed JSON object by trimming whitespace from:\n * 1. All object keys (e.g., \" prompt \" -> \"prompt\")\n * 2. All string values (e.g., \" Tap \" -> \"Tap\")\n * This handles LLM output that may include leading/trailing spaces.\n */\nfunction normalizeJsonObject(obj: any): any {\n // Handle null and undefined\n if (obj === null || obj === undefined) {\n return obj;\n }\n\n // Handle arrays - recursively normalize each element\n if (Array.isArray(obj)) {\n return obj.map((item) => normalizeJsonObject(item));\n }\n\n // Handle objects\n if (typeof obj === 'object') {\n const normalized: any = {};\n\n for (const [key, value] of Object.entries(obj)) {\n // Trim the key to remove leading/trailing spaces\n const trimmedKey = key.trim();\n\n // Recursively normalize the value\n let normalizedValue = normalizeJsonObject(value);\n\n // Trim all string values\n if (typeof normalizedValue === 'string') {\n normalizedValue = normalizedValue.trim();\n }\n\n normalized[trimmedKey] = normalizedValue;\n }\n\n return normalized;\n }\n\n // Handle primitive strings\n if (typeof obj === 'string') {\n return obj.trim();\n }\n\n // Return other primitives as-is\n return obj;\n}\n\nexport function safeParseJson(\n input: string,\n modelFamily: TModelFamily | undefined,\n) {\n const cleanJsonString = extractJSONFromCodeBlock(input);\n // match the point\n if (cleanJsonString?.match(/\\((\\d+),(\\d+)\\)/)) {\n return cleanJsonString\n .match(/\\((\\d+),(\\d+)\\)/)\n ?.slice(1)\n .map(Number);\n }\n\n let parsed: any;\n let lastError: unknown;\n try {\n parsed = JSON.parse(cleanJsonString);\n return normalizeJsonObject(parsed);\n } catch (error) {\n lastError = error;\n }\n try {\n parsed = JSON.parse(jsonrepair(cleanJsonString));\n return normalizeJsonObject(parsed);\n } catch (error) {\n lastError = error;\n }\n\n if (\n modelFamily === 'doubao-vision' ||\n modelFamily === 'doubao-seed' ||\n isUITars(modelFamily)\n ) {\n const jsonString = preprocessDoubaoBboxJson(cleanJsonString);\n try {\n parsed = JSON.parse(jsonrepair(jsonString));\n return normalizeJsonObject(parsed);\n } catch (error) {\n lastError = error;\n }\n }\n throw Error(\n `failed to parse LLM response into JSON. Error - ${String(\n lastError ?? 'unknown error',\n )}. Response - \\n ${input}`,\n );\n}\n"],"names":["AIResponseParseError","Error","message","rawResponse","usage","createChatClient","modelConfig","socksProxy","httpProxy","modelName","openaiBaseURL","openaiApiKey","openaiExtraConfig","modelDescription","uiTarsModelVersion","modelFamily","createOpenAIClient","timeout","proxyAgent","warnClient","getDebug","debugProxy","warnProxy","sanitizeProxyUrl","url","parsed","URL","ifInBrowser","moduleName","ProxyAgent","socksDispatcher","proxyUrl","port","Number","protocol","socksType","decodeURIComponent","error","effectiveTimeoutMs","resolveEffectiveTimeoutMs","openAIOptions","baseOpenAI","OpenAI","openai","globalConfigManager","MIDSCENE_LANGSMITH_DEBUG","langsmithModule","wrapOpenAI","MIDSCENE_LANGFUSE_DEBUG","langfuseModule","observeOpenAI","wrappedClient","callAI","messages","options","isCodexAppServerProvider","hasExplicitReasoningConfig","callAIWithCodexAppServer","completion","extraBody","maxTokens","MIDSCENE_MODEL_MAX_TOKENS","OPENAI_MAX_TOKENS","debugCall","warnCall","debugProfileStats","debugProfileDetail","startTime","Date","temperature","isStreaming","content","accumulated","accumulatedReasoning","timeCost","requestId","hasUsableText","value","buildUsageInfo","usageData","cachedInputTokens","undefined","commonConfig","isAutoGLM","reasoningEffortConfig","reasoningEffortDebugMessage","resolveReasoningConfig","shouldUseOriginalImageDetail","shouldForceOriginalImageDetail","messagesWithImageDetail","msg","Array","part","streamSignal","cleanupStreamSignal","buildRequestAbortSignal","stream","chunk","reasoning_content","chunkData","estimatedTokens","Math","finalChunk","retryCount","retryInterval","maxAttempts","lastError","attempt","attemptSignal","cleanupAttemptSignal","result","JSON","wasHardTimeout","isHardTimeoutError","Promise","resolve","setTimeout","e","newError","callAIWithObjectResponse","response","assert","jsonContent","safeParseJson","callAIWithStringResponse","msgs","extractJSONFromCodeBlock","jsonMatch","codeBlockMatch","jsonLikeMatch","preprocessDoubaoBboxJson","input","reasoningEnabled","reasoningEffort","reasoningBudget","SUPPORTED_REASONING_FAMILIES","isSupportedReasoningFamily","family","supportedReasoningFamilyNames","hasExplicitConfig","effectiveReasoningEnabled","debugMessages","config","normalizeJsonObject","obj","item","normalized","key","Object","trimmedKey","normalizedValue","cleanJsonString","jsonrepair","isUITars","jsonString","String"],"mappings":";;;;;;;;;;;;;;;;;;;AAIO,MAAMA,6BAA6BC;IAIxC,YAAYC,OAAe,EAAEC,WAAmB,EAAEC,KAAmB,CAAE;QACrE,KAAK,CAACF,UAJR,yCACA;QAIE,IAAI,CAAC,IAAI,GAAG;QACZ,IAAI,CAAC,WAAW,GAAGC;QACnB,IAAI,CAAC,KAAK,GAAGC;IACf;AACF;AA+BA,eAAeC,iBAAiB,EAC9BC,WAAW,EAGZ;IAOC,MAAM,EACJC,UAAU,EACVC,SAAS,EACTC,SAAS,EACTC,aAAa,EACbC,YAAY,EACZC,iBAAiB,EACjBC,gBAAgB,EAChBC,kBAAkB,EAClBC,WAAW,EACXC,kBAAkB,EAClBC,OAAO,EACR,GAAGX;IAEJ,IAAIY;IACJ,MAAMC,aAAaC,SAAS,WAAW;QAAE,SAAS;IAAK;IACvD,MAAMC,aAAaD,SAAS;IAC5B,MAAME,YAAYF,SAAS,iBAAiB;QAAE,SAAS;IAAK;IAI5D,MAAMG,mBAAmB,CAACC;QACxB,IAAI;YACF,MAAMC,SAAS,IAAIC,IAAIF;YACvB,IAAIC,OAAO,QAAQ,EAAE;gBAEnBA,OAAO,QAAQ,GAAG;gBAClB,OAAOA,OAAO,IAAI;YACpB;YACA,OAAOD;QACT,EAAE,OAAM;YAEN,OAAOA;QACT;IACF;IAEA,IAAIhB,WAAW;QACba,WAAW,oBAAoBE,iBAAiBf;QAChD,IAAImB,aACFL,UACE;aAEG;YAEL,MAAMM,aAAa;YACnB,MAAM,EAAEC,UAAU,EAAE,GAAG,MAAM,MAAM,CAACD;YACpCV,aAAa,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J,KAAK,CAAC,oBACL,MAAM,GACP,IAAItH;IAGT,IAAIR;IACJ,IAAIiF;IACJ,IAAI;QACFjF,SAASsF,KAAK,KAAK,CAACwC;QACpB,OAAOR,oBAAoBtH;IAC7B,EAAE,OAAOY,OAAO;QACdqE,YAAYrE;IACd;IACA,IAAI;QACFZ,SAASsF,KAAK,KAAK,CAACyC,WAAWD;QAC/B,OAAOR,oBAAoBtH;IAC7B,EAAE,OAAOY,OAAO;QACdqE,YAAYrE;IACd;IAEA,IACEtB,AAAgB,oBAAhBA,eACAA,AAAgB,kBAAhBA,eACA0I,SAAS1I,cACT;QACA,MAAM2I,aAAaxB,yBAAyBqB;QAC5C,IAAI;YACF9H,SAASsF,KAAK,KAAK,CAACyC,WAAWE;YAC/B,OAAOX,oBAAoBtH;QAC7B,EAAE,OAAOY,OAAO;YACdqE,YAAYrE;QACd;IACF;IACA,MAAMpC,MACJ,CAAC,gDAAgD,EAAE0J,OACjDjD,aAAa,iBACb,gBAAgB,EAAEyB,OAAO;AAE/B"}
1
+ {"version":3,"file":"ai-model/service-caller/index.mjs","sources":["../../../../src/ai-model/service-caller/index.ts"],"sourcesContent":["import type { AIUsageInfo } from '@/types';\nimport type { CodeGenerationChunk, StreamingCallback } from '@/types';\n\n// Error class that preserves usage and rawResponse when AI call parsing fails\nexport class AIResponseParseError extends Error {\n usage?: AIUsageInfo;\n rawResponse: string;\n\n constructor(message: string, rawResponse: string, usage?: AIUsageInfo) {\n super(message);\n this.name = 'AIResponseParseError';\n this.rawResponse = rawResponse;\n this.usage = usage;\n }\n}\nimport {\n type IModelConfig,\n MIDSCENE_LANGFUSE_DEBUG,\n MIDSCENE_LANGSMITH_DEBUG,\n MIDSCENE_MODEL_MAX_TOKENS,\n OPENAI_MAX_TOKENS,\n type TModelFamily,\n type UITarsModelVersion,\n globalConfigManager,\n} from '@midscene/shared/env';\n\nimport { getDebug } from '@midscene/shared/logger';\nimport { assert, ifInBrowser } from '@midscene/shared/utils';\nimport { jsonrepair } from 'jsonrepair';\nimport OpenAI from 'openai';\nimport type { ChatCompletionMessageParam } from 'openai/resources/index';\nimport type { Stream } from 'openai/streaming';\nimport type { AIArgs } from '../../common';\nimport { isAutoGLM, isUITars } from '../auto-glm/util';\nimport {\n callAIWithCodexAppServer,\n isCodexAppServerProvider,\n} from './codex-app-server';\nimport { shouldForceOriginalImageDetail } from './image-detail';\nimport {\n buildRequestAbortSignal,\n isHardTimeoutError,\n resolveEffectiveTimeoutMs,\n} from './request-timeout';\n\nasync function createChatClient({\n modelConfig,\n}: {\n modelConfig: IModelConfig;\n}): Promise<{\n completion: OpenAI.Chat.Completions;\n modelName: string;\n modelDescription: string;\n uiTarsModelVersion?: UITarsModelVersion;\n modelFamily: TModelFamily | undefined;\n}> {\n const {\n socksProxy,\n httpProxy,\n modelName,\n openaiBaseURL,\n openaiApiKey,\n openaiExtraConfig,\n modelDescription,\n uiTarsModelVersion,\n modelFamily,\n createOpenAIClient,\n timeout,\n } = modelConfig;\n\n let proxyAgent: any = undefined;\n const warnClient = getDebug('ai:call', { console: true });\n const debugProxy = getDebug('ai:call:proxy');\n const warnProxy = getDebug('ai:call:proxy', { console: true });\n\n // Helper function to sanitize proxy URL for logging (remove credentials)\n // Uses URL API instead of regex to avoid ReDoS vulnerabilities\n const sanitizeProxyUrl = (url: string): string => {\n try {\n const parsed = new URL(url);\n if (parsed.username) {\n // Keep username for debugging, hide password for security\n parsed.password = '****';\n return parsed.href;\n }\n return url;\n } catch {\n // If URL parsing fails, return original URL (will be caught later)\n return url;\n }\n };\n\n if (httpProxy) {\n debugProxy('using http proxy', sanitizeProxyUrl(httpProxy));\n if (ifInBrowser) {\n warnProxy(\n 'HTTP proxy is configured but not supported in browser environment',\n );\n } else {\n // Dynamic import with variable to avoid bundler static analysis\n const moduleName = 'undici';\n const { ProxyAgent } = await import(moduleName);\n proxyAgent = new ProxyAgent({\n uri: httpProxy,\n // Note: authentication is handled via the URI (e.g., http://user:pass@proxy.com:8080)\n });\n }\n } else if (socksProxy) {\n debugProxy('using socks proxy', sanitizeProxyUrl(socksProxy));\n if (ifInBrowser) {\n warnProxy(\n 'SOCKS proxy is configured but not supported in browser environment',\n );\n } else {\n try {\n // Dynamic import with variable to avoid bundler static analysis\n const moduleName = 'fetch-socks';\n const { socksDispatcher } = await import(moduleName);\n // Parse SOCKS proxy URL (e.g., socks5://127.0.0.1:1080)\n const proxyUrl = new URL(socksProxy);\n\n // Validate hostname\n if (!proxyUrl.hostname) {\n throw new Error('SOCKS proxy URL must include a valid hostname');\n }\n\n // Validate and parse port\n const port = Number.parseInt(proxyUrl.port, 10);\n if (!proxyUrl.port || Number.isNaN(port)) {\n throw new Error('SOCKS proxy URL must include a valid port');\n }\n\n // Parse SOCKS version from protocol\n const protocol = proxyUrl.protocol.replace(':', '');\n const socksType =\n protocol === 'socks4' ? 4 : protocol === 'socks5' ? 5 : 5;\n\n proxyAgent = socksDispatcher({\n type: socksType,\n host: proxyUrl.hostname,\n port,\n ...(proxyUrl.username\n ? {\n userId: decodeURIComponent(proxyUrl.username),\n password: decodeURIComponent(proxyUrl.password || ''),\n }\n : {}),\n });\n debugProxy('socks proxy configured successfully', {\n type: socksType,\n host: proxyUrl.hostname,\n port: port,\n });\n } catch (error) {\n warnProxy('Failed to configure SOCKS proxy:', error);\n throw new Error(\n `Invalid SOCKS proxy URL: ${socksProxy}. Expected format: socks4://host:port, socks5://host:port, or with authentication: socks5://user:pass@host:port`,\n );\n }\n }\n }\n\n const effectiveTimeoutMs = resolveEffectiveTimeoutMs({ timeout });\n const openAIOptions = {\n baseURL: openaiBaseURL,\n apiKey: openaiApiKey,\n // Use fetchOptions.dispatcher for fetch-based SDK instead of httpAgent\n // Note: Type assertion needed due to undici version mismatch between dependencies\n ...(proxyAgent ? { fetchOptions: { dispatcher: proxyAgent as any } } : {}),\n ...openaiExtraConfig,\n // Midscene already handles retries in callAI(), so disable SDK-level retries\n // to avoid duplicate attempts and duplicated backoff latency.\n maxRetries: 0,\n // When disabled (timeoutMs === null) fall through to the SDK default so\n // only the caller-provided abortSignal can cancel the request.\n ...(effectiveTimeoutMs !== null ? { timeout: effectiveTimeoutMs } : {}),\n dangerouslyAllowBrowser: true,\n };\n\n const baseOpenAI = new OpenAI(openAIOptions);\n\n let openai: OpenAI = baseOpenAI;\n\n // LangSmith wrapper\n if (\n openai &&\n globalConfigManager.getEnvConfigInBoolean(MIDSCENE_LANGSMITH_DEBUG)\n ) {\n if (ifInBrowser) {\n throw new Error('langsmith is not supported in browser');\n }\n warnClient('DEBUGGING MODE: langsmith wrapper enabled');\n // Use variable to prevent static analysis by bundlers\n const langsmithModule = 'langsmith/wrappers';\n const { wrapOpenAI } = await import(langsmithModule);\n openai = wrapOpenAI(openai);\n }\n\n // Langfuse wrapper\n if (\n openai &&\n globalConfigManager.getEnvConfigInBoolean(MIDSCENE_LANGFUSE_DEBUG)\n ) {\n if (ifInBrowser) {\n throw new Error('langfuse is not supported in browser');\n }\n warnClient('DEBUGGING MODE: langfuse wrapper enabled');\n // Use variable to prevent static analysis by bundlers\n const langfuseModule = '@langfuse/openai';\n const { observeOpenAI } = await import(langfuseModule);\n openai = observeOpenAI(openai);\n }\n\n if (createOpenAIClient) {\n const wrappedClient = await createOpenAIClient(baseOpenAI, openAIOptions);\n\n if (wrappedClient) {\n openai = wrappedClient as OpenAI;\n }\n }\n\n return {\n completion: openai.chat.completions,\n modelName,\n modelDescription,\n uiTarsModelVersion,\n modelFamily,\n };\n}\n\nexport async function callAI(\n messages: ChatCompletionMessageParam[],\n modelConfig: IModelConfig,\n options?: {\n stream?: boolean;\n onChunk?: StreamingCallback;\n abortSignal?: AbortSignal;\n },\n): Promise<{\n content: string;\n reasoning_content?: string;\n usage?: AIUsageInfo;\n isStreamed: boolean;\n}> {\n if (isCodexAppServerProvider(modelConfig.openaiBaseURL)) {\n if (\n !modelConfig.modelFamily &&\n hasExplicitReasoningConfig({\n reasoningEnabled: modelConfig.reasoningEnabled,\n reasoningEffort: modelConfig.reasoningEffort,\n reasoningBudget: modelConfig.reasoningBudget,\n })\n ) {\n throw new Error(\n 'Reasoning config requires MIDSCENE_MODEL_FAMILY. Set MIDSCENE_MODEL_FAMILY when using MIDSCENE_MODEL_REASONING_ENABLED / MIDSCENE_MODEL_REASONING_EFFORT / MIDSCENE_MODEL_REASONING_BUDGET.',\n );\n }\n\n return callAIWithCodexAppServer(messages, modelConfig, {\n stream: options?.stream,\n onChunk: options?.onChunk,\n reasoningEnabled: modelConfig.reasoningEnabled,\n abortSignal: options?.abortSignal,\n });\n }\n\n const {\n completion,\n modelName,\n modelDescription,\n uiTarsModelVersion,\n modelFamily,\n } = await createChatClient({\n modelConfig,\n });\n const effectiveTimeoutMs = resolveEffectiveTimeoutMs(modelConfig);\n\n const extraBody = modelConfig.extraBody;\n\n const maxTokens =\n globalConfigManager.getEnvConfigValueAsNumber(MIDSCENE_MODEL_MAX_TOKENS) ??\n globalConfigManager.getEnvConfigValueAsNumber(OPENAI_MAX_TOKENS);\n const debugCall = getDebug('ai:call');\n const warnCall = getDebug('ai:call', { console: true });\n const debugProfileStats = getDebug('ai:profile:stats');\n const debugProfileDetail = getDebug('ai:profile:detail');\n\n const startTime = Date.now();\n\n const temperature = (() => {\n if (modelFamily === 'gpt-5') {\n debugCall('temperature is ignored for gpt-5');\n return undefined;\n }\n return modelConfig.temperature ?? 0;\n })();\n\n const isStreaming = options?.stream && options?.onChunk;\n let content: string | undefined;\n let accumulated = '';\n let accumulatedReasoning = '';\n let usage: OpenAI.CompletionUsage | undefined;\n let timeCost: number | undefined;\n let requestId: string | null | undefined;\n\n const hasUsableText = (value: string | null | undefined): value is string =>\n typeof value === 'string' && value.trim().length > 0;\n\n const buildUsageInfo = (\n usageData?: OpenAI.CompletionUsage,\n requestId?: string | null,\n ) => {\n if (!usageData) return undefined;\n\n const cachedInputTokens = (\n usageData as { prompt_tokens_details?: { cached_tokens?: number } }\n )?.prompt_tokens_details?.cached_tokens;\n\n return {\n prompt_tokens: usageData.prompt_tokens ?? 0,\n completion_tokens: usageData.completion_tokens ?? 0,\n total_tokens: usageData.total_tokens ?? 0,\n cached_input: cachedInputTokens ?? 0,\n time_cost: timeCost ?? 0,\n model_name: modelName,\n model_description: modelDescription,\n slot: modelConfig.slot,\n intent: undefined,\n request_id: requestId ?? undefined,\n } satisfies AIUsageInfo;\n };\n\n const commonConfig = {\n temperature,\n stream: !!isStreaming,\n max_tokens: maxTokens,\n ...(modelFamily === 'qwen2.5-vl' // qwen vl v2 specific config\n ? {\n vl_high_resolution_images: true,\n }\n : {}),\n };\n\n if (isAutoGLM(modelFamily)) {\n (commonConfig as unknown as Record<string, number>).top_p = 0.85;\n (commonConfig as unknown as Record<string, number>).frequency_penalty = 0.2;\n }\n\n const {\n config: reasoningEffortConfig,\n debugMessage: reasoningEffortDebugMessage,\n } = resolveReasoningConfig({\n reasoningEnabled: modelConfig.reasoningEnabled,\n reasoningEffort: modelConfig.reasoningEffort,\n reasoningBudget: modelConfig.reasoningBudget,\n modelFamily,\n });\n if (reasoningEffortDebugMessage) {\n debugCall(reasoningEffortDebugMessage);\n }\n\n const shouldUseOriginalImageDetail =\n shouldForceOriginalImageDetail(modelConfig);\n\n // For default-intent GPT-5 calls, request original image detail to preserve\n // screenshot resolution for localization-sensitive tasks.\n const messagesWithImageDetail: ChatCompletionMessageParam[] = (() => {\n if (!shouldUseOriginalImageDetail) {\n return messages;\n }\n\n return messages.map((msg) => {\n if (!Array.isArray(msg.content)) {\n return msg;\n }\n\n const content = msg.content.map((part) => {\n if (part && part.type === 'image_url' && part.image_url?.url) {\n return {\n ...part,\n image_url: {\n ...part.image_url,\n detail: 'original',\n },\n };\n }\n return part;\n });\n\n return {\n ...msg,\n content,\n } as ChatCompletionMessageParam;\n });\n })();\n\n try {\n debugCall(\n `sending ${isStreaming ? 'streaming ' : ''}request to ${modelName}`,\n );\n\n if (isStreaming) {\n const { signal: streamSignal, cleanup: cleanupStreamSignal } =\n buildRequestAbortSignal(effectiveTimeoutMs, options?.abortSignal);\n try {\n const stream = (await completion.create(\n {\n model: modelName,\n messages: messagesWithImageDetail,\n ...commonConfig,\n ...reasoningEffortConfig,\n ...extraBody,\n },\n {\n stream: true,\n signal: streamSignal,\n },\n )) as Stream<OpenAI.Chat.Completions.ChatCompletionChunk> & {\n _request_id?: string | null;\n };\n\n requestId = stream._request_id;\n\n for await (const chunk of stream) {\n const content = chunk.choices?.[0]?.delta?.content || '';\n const reasoning_content =\n (chunk.choices?.[0]?.delta as any)?.reasoning_content || '';\n\n // Check for usage info in any chunk (OpenAI provides usage in separate chunks)\n if (chunk.usage) {\n usage = chunk.usage;\n }\n\n if (content || reasoning_content) {\n accumulated += content;\n accumulatedReasoning += reasoning_content;\n const chunkData: CodeGenerationChunk = {\n content,\n reasoning_content,\n accumulated,\n isComplete: false,\n usage: undefined,\n };\n options.onChunk!(chunkData);\n }\n\n // Check if stream is complete\n if (chunk.choices?.[0]?.finish_reason) {\n timeCost = Date.now() - startTime;\n\n // If usage is not available from the stream, provide a basic usage info\n if (!usage) {\n // Estimate token counts based on content length (rough approximation)\n const estimatedTokens = Math.max(\n 1,\n Math.floor(accumulated.length / 4),\n );\n usage = {\n prompt_tokens: estimatedTokens,\n completion_tokens: estimatedTokens,\n total_tokens: estimatedTokens * 2,\n };\n }\n\n // Send final chunk\n const finalChunk: CodeGenerationChunk = {\n content: '',\n accumulated,\n reasoning_content: '',\n isComplete: true,\n usage: buildUsageInfo(usage, requestId),\n };\n options.onChunk!(finalChunk);\n break;\n }\n }\n } finally {\n cleanupStreamSignal();\n }\n content = accumulated;\n debugProfileStats(\n `streaming model, ${modelName}, mode, ${modelFamily || 'default'}, cost-ms, ${timeCost}, temperature, ${temperature ?? ''}`,\n );\n } else {\n // Non-streaming with retry logic\n const retryCount = modelConfig.retryCount ?? 1;\n const retryInterval = modelConfig.retryInterval ?? 2000;\n const maxAttempts = retryCount + 1; // retryCount=1 means 2 total attempts (1 initial + 1 retry)\n\n let lastError: Error | undefined;\n\n for (let attempt = 1; attempt <= maxAttempts; attempt++) {\n const { signal: attemptSignal, cleanup: cleanupAttemptSignal } =\n buildRequestAbortSignal(effectiveTimeoutMs, options?.abortSignal);\n try {\n const result = await completion.create(\n {\n model: modelName,\n messages: messagesWithImageDetail,\n ...commonConfig,\n ...reasoningEffortConfig,\n ...extraBody,\n } as any,\n { signal: attemptSignal },\n );\n\n timeCost = Date.now() - startTime;\n\n debugProfileStats(\n `model, ${modelName}, mode, ${modelFamily || 'default'}, ui-tars-version, ${uiTarsModelVersion}, prompt-tokens, ${result.usage?.prompt_tokens || ''}, completion-tokens, ${result.usage?.completion_tokens || ''}, total-tokens, ${result.usage?.total_tokens || ''}, cost-ms, ${timeCost}, requestId, ${result._request_id || ''}, temperature, ${temperature ?? ''}`,\n );\n\n debugProfileDetail(\n `model usage detail: ${JSON.stringify(result.usage)}`,\n );\n\n if (!result.choices) {\n throw new Error(\n `invalid response from LLM service: ${JSON.stringify(result)}`,\n );\n }\n\n content = result.choices[0].message.content!;\n accumulatedReasoning =\n (result.choices[0].message as any)?.reasoning_content || '';\n usage = result.usage;\n requestId = result._request_id;\n\n if (!hasUsableText(content) && hasUsableText(accumulatedReasoning)) {\n warnCall('empty content from AI model, using reasoning content');\n content = accumulatedReasoning;\n }\n\n if (!hasUsableText(content)) {\n throw new AIResponseParseError(\n 'empty content from AI model',\n JSON.stringify(result),\n buildUsageInfo(usage, requestId),\n );\n }\n\n break; // Success, exit retry loop\n } catch (error) {\n lastError = error as Error;\n const wasHardTimeout = isHardTimeoutError(lastError);\n if (wasHardTimeout) {\n warnCall(\n `AI call hit hard timeout (${effectiveTimeoutMs}ms, attempt ${attempt}/${maxAttempts}, model ${modelName}, slot ${modelConfig.slot})`,\n );\n }\n // Do not retry if the request was aborted by the caller\n if (options?.abortSignal?.aborted) {\n break;\n }\n if (attempt < maxAttempts) {\n warnCall(\n `AI call failed (attempt ${attempt}/${maxAttempts}), retrying in ${retryInterval}ms... Error: ${lastError.message}`,\n );\n await new Promise((resolve) => setTimeout(resolve, retryInterval));\n }\n } finally {\n cleanupAttemptSignal();\n }\n }\n\n if (!content) {\n throw lastError;\n }\n }\n\n debugCall(`response reasoning content: ${accumulatedReasoning}`);\n debugCall(`response content: ${content}`);\n\n // Ensure we always have usage info for streaming responses\n if (isStreaming && !usage) {\n // Estimate token counts based on content length (rough approximation)\n const estimatedTokens = Math.max(\n 1,\n Math.floor((content || '').length / 4),\n );\n usage = {\n prompt_tokens: estimatedTokens,\n completion_tokens: estimatedTokens,\n total_tokens: estimatedTokens * 2,\n } as OpenAI.CompletionUsage;\n }\n\n return {\n content: content || '',\n reasoning_content: accumulatedReasoning || undefined,\n usage: buildUsageInfo(usage, requestId),\n isStreamed: !!isStreaming,\n };\n } catch (e: any) {\n warnCall('call AI error', e);\n\n if (e instanceof AIResponseParseError) {\n throw e;\n }\n\n const newError = new Error(\n `failed to call ${isStreaming ? 'streaming ' : ''}AI model service (${modelName}): ${e.message}\\nTrouble shooting: https://midscenejs.com/model-provider.html`,\n {\n cause: e,\n },\n );\n throw newError;\n }\n}\n\nexport async function callAIWithObjectResponse<T>(\n messages: ChatCompletionMessageParam[],\n modelConfig: IModelConfig,\n options?: {\n abortSignal?: AbortSignal;\n },\n): Promise<{\n content: T;\n contentString: string;\n usage?: AIUsageInfo;\n reasoning_content?: string;\n}> {\n const response = await callAI(messages, modelConfig, {\n abortSignal: options?.abortSignal,\n });\n assert(response, 'empty response');\n const modelFamily = modelConfig.modelFamily;\n const jsonContent = safeParseJson(response.content, modelFamily);\n if (typeof jsonContent !== 'object') {\n throw new AIResponseParseError(\n `failed to parse json response from model (${modelConfig.modelName}): ${response.content}`,\n response.content,\n response.usage,\n );\n }\n return {\n content: jsonContent,\n contentString: response.content,\n usage: response.usage,\n reasoning_content: response.reasoning_content,\n };\n}\n\nexport async function callAIWithStringResponse(\n msgs: AIArgs,\n modelConfig: IModelConfig,\n options?: {\n abortSignal?: AbortSignal;\n },\n): Promise<{ content: string; usage?: AIUsageInfo }> {\n const { content, usage } = await callAI(msgs, modelConfig, {\n abortSignal: options?.abortSignal,\n });\n return { content, usage };\n}\n\nexport function extractJSONFromCodeBlock(response: string) {\n try {\n // First, try to match a JSON object directly in the response\n const jsonMatch = response.match(/^\\s*(\\{[\\s\\S]*\\})\\s*$/);\n if (jsonMatch) {\n return jsonMatch[1];\n }\n\n // If no direct JSON object is found, try to extract JSON from a code block\n const codeBlockMatch = response.match(\n /```(?:json)?\\s*(\\{[\\s\\S]*?\\})\\s*```/,\n );\n if (codeBlockMatch) {\n return codeBlockMatch[1];\n }\n\n // If no code block is found, try to find a JSON-like structure in the text\n const jsonLikeMatch = response.match(/\\{[\\s\\S]*\\}/);\n if (jsonLikeMatch) {\n return jsonLikeMatch[0];\n }\n } catch {}\n // If no JSON-like structure is found, return the original response\n return response;\n}\n\nexport function preprocessDoubaoBboxJson(input: string) {\n if (input.includes('bbox')) {\n // when its values like 940 445 969 490, replace all /\\d+\\s+\\d+/g with /$1,$2/g\n while (/\\d+\\s+\\d+/.test(input)) {\n input = input.replace(/(\\d+)\\s+(\\d+)/g, '$1,$2');\n }\n }\n return input;\n}\n\nfunction hasExplicitReasoningConfig({\n reasoningEnabled,\n reasoningEffort,\n reasoningBudget,\n}: {\n reasoningEnabled?: boolean;\n reasoningEffort?: string;\n reasoningBudget?: number;\n}): boolean {\n return (\n reasoningEnabled !== undefined ||\n !!reasoningEffort ||\n reasoningBudget !== undefined\n );\n}\n\nconst SUPPORTED_REASONING_FAMILIES = [\n 'qwen3-vl',\n 'qwen3.5',\n 'qwen3.6',\n 'doubao-vision',\n 'doubao-seed',\n 'glm-v',\n] as const satisfies readonly TModelFamily[];\n\ntype SupportedReasoningFamily = (typeof SUPPORTED_REASONING_FAMILIES)[number];\n\nfunction isSupportedReasoningFamily(\n family: TModelFamily | undefined,\n): family is SupportedReasoningFamily {\n return (\n !!family &&\n (SUPPORTED_REASONING_FAMILIES as readonly TModelFamily[]).includes(family)\n );\n}\n\nfunction supportedReasoningFamilyNames(): string {\n return SUPPORTED_REASONING_FAMILIES.join(', ');\n}\n\nexport function resolveReasoningConfig({\n reasoningEnabled,\n reasoningEffort,\n reasoningBudget,\n modelFamily,\n}: {\n reasoningEnabled?: boolean;\n reasoningEffort?: string;\n reasoningBudget?: number;\n modelFamily?: TModelFamily;\n}): {\n config: Record<string, unknown>;\n debugMessage?: string;\n} {\n const hasExplicitConfig = hasExplicitReasoningConfig({\n reasoningEnabled,\n reasoningEffort,\n reasoningBudget,\n });\n\n if (hasExplicitConfig) {\n if (!modelFamily) {\n throw new Error(\n `Reasoning config requires MIDSCENE_MODEL_FAMILY. Set MIDSCENE_MODEL_FAMILY to a supported family such as ${supportedReasoningFamilyNames()}, or remove MIDSCENE_MODEL_REASONING_ENABLED / MIDSCENE_MODEL_REASONING_EFFORT / MIDSCENE_MODEL_REASONING_BUDGET.`,\n );\n }\n\n // GPT-5 over Chat Completions is intentionally unsupported here because\n // its reasoning effort compatibility varies by model version.\n if (!isSupportedReasoningFamily(modelFamily)) {\n throw new Error(\n `Reasoning config is not supported for model family \"${modelFamily}\". Use a supported family such as ${supportedReasoningFamilyNames()}, or remove MIDSCENE_MODEL_REASONING_ENABLED / MIDSCENE_MODEL_REASONING_EFFORT / MIDSCENE_MODEL_REASONING_BUDGET.`,\n );\n }\n } else if (!isSupportedReasoningFamily(modelFamily)) {\n return { config: {} };\n }\n\n const effectiveReasoningEnabled = reasoningEnabled ?? false;\n\n const debugMessages: string[] = [];\n const config: Record<string, unknown> = {};\n\n if (\n modelFamily === 'qwen3-vl' ||\n modelFamily === 'qwen3.5' ||\n modelFamily === 'qwen3.6'\n ) {\n // reasoningEnabled → enable_thinking\n config.enable_thinking = effectiveReasoningEnabled;\n debugMessages.push(`enable_thinking=${effectiveReasoningEnabled}`);\n // reasoningBudget → thinking_budget\n if (reasoningBudget !== undefined) {\n config.thinking_budget = reasoningBudget;\n debugMessages.push(`thinking_budget=${reasoningBudget}`);\n }\n // reasoningEffort is ignored for qwen\n } else if (modelFamily === 'doubao-vision' || modelFamily === 'doubao-seed') {\n // reasoningEnabled → thinking.type\n config.thinking = {\n type: effectiveReasoningEnabled ? 'enabled' : 'disabled',\n };\n debugMessages.push(\n `thinking.type=${effectiveReasoningEnabled ? 'enabled' : 'disabled'}`,\n );\n // reasoningEffort → reasoning_effort\n if (reasoningEffort) {\n config.reasoning_effort = reasoningEffort;\n debugMessages.push(`reasoning_effort=\"${reasoningEffort}\"`);\n }\n // reasoningBudget is ignored for doubao\n } else if (modelFamily === 'glm-v') {\n // reasoningEnabled → thinking.type\n config.thinking = {\n type: effectiveReasoningEnabled ? 'enabled' : 'disabled',\n };\n debugMessages.push(\n `thinking.type=${effectiveReasoningEnabled ? 'enabled' : 'disabled'}`,\n );\n // reasoningEffort and reasoningBudget are ignored for glm-v\n }\n\n return {\n config,\n debugMessage: debugMessages.length\n ? `reasoning config for ${modelFamily}: ${debugMessages.join(', ')}`\n : undefined,\n };\n}\n\n/**\n * Normalize a parsed JSON object by trimming whitespace from:\n * 1. All object keys (e.g., \" prompt \" -> \"prompt\")\n * 2. All string values (e.g., \" Tap \" -> \"Tap\")\n * This handles LLM output that may include leading/trailing spaces.\n */\nfunction normalizeJsonObject(obj: any): any {\n // Handle null and undefined\n if (obj === null || obj === undefined) {\n return obj;\n }\n\n // Handle arrays - recursively normalize each element\n if (Array.isArray(obj)) {\n return obj.map((item) => normalizeJsonObject(item));\n }\n\n // Handle objects\n if (typeof obj === 'object') {\n const normalized: any = {};\n\n for (const [key, value] of Object.entries(obj)) {\n // Trim the key to remove leading/trailing spaces\n const trimmedKey = key.trim();\n\n // Recursively normalize the value\n let normalizedValue = normalizeJsonObject(value);\n\n // Trim all string values\n if (typeof normalizedValue === 'string') {\n normalizedValue = normalizedValue.trim();\n }\n\n normalized[trimmedKey] = normalizedValue;\n }\n\n return normalized;\n }\n\n // Handle primitive strings\n if (typeof obj === 'string') {\n return obj.trim();\n }\n\n // Return other primitives as-is\n return obj;\n}\n\nexport function safeParseJson(\n input: string,\n modelFamily: TModelFamily | undefined,\n) {\n const cleanJsonString = extractJSONFromCodeBlock(input);\n // match the point\n if (cleanJsonString?.match(/\\((\\d+),(\\d+)\\)/)) {\n return cleanJsonString\n .match(/\\((\\d+),(\\d+)\\)/)\n ?.slice(1)\n .map(Number);\n }\n\n let parsed: any;\n let lastError: unknown;\n try {\n parsed = JSON.parse(cleanJsonString);\n return normalizeJsonObject(parsed);\n } catch (error) {\n lastError = error;\n }\n try {\n parsed = JSON.parse(jsonrepair(cleanJsonString));\n return normalizeJsonObject(parsed);\n } catch (error) {\n lastError = error;\n }\n\n if (\n modelFamily === 'doubao-vision' ||\n modelFamily === 'doubao-seed' ||\n isUITars(modelFamily)\n ) {\n const jsonString = preprocessDoubaoBboxJson(cleanJsonString);\n try {\n parsed = JSON.parse(jsonrepair(jsonString));\n return normalizeJsonObject(parsed);\n } catch (error) {\n lastError = error;\n }\n }\n throw Error(\n `failed to parse LLM response into JSON. Error - ${String(\n lastError ?? 'unknown error',\n )}. Response - \\n ${input}`,\n );\n}\n"],"names":["AIResponseParseError","Error","message","rawResponse","usage","createChatClient","modelConfig","socksProxy","httpProxy","modelName","openaiBaseURL","openaiApiKey","openaiExtraConfig","modelDescription","uiTarsModelVersion","modelFamily","createOpenAIClient","timeout","proxyAgent","warnClient","getDebug","debugProxy","warnProxy","sanitizeProxyUrl","url","parsed","URL","ifInBrowser","moduleName","ProxyAgent","socksDispatcher","proxyUrl","port","Number","protocol","socksType","decodeURIComponent","error","effectiveTimeoutMs","resolveEffectiveTimeoutMs","openAIOptions","baseOpenAI","OpenAI","openai","globalConfigManager","MIDSCENE_LANGSMITH_DEBUG","langsmithModule","wrapOpenAI","MIDSCENE_LANGFUSE_DEBUG","langfuseModule","observeOpenAI","wrappedClient","callAI","messages","options","isCodexAppServerProvider","hasExplicitReasoningConfig","callAIWithCodexAppServer","completion","extraBody","maxTokens","MIDSCENE_MODEL_MAX_TOKENS","OPENAI_MAX_TOKENS","debugCall","warnCall","debugProfileStats","debugProfileDetail","startTime","Date","temperature","isStreaming","content","accumulated","accumulatedReasoning","timeCost","requestId","hasUsableText","value","buildUsageInfo","usageData","cachedInputTokens","undefined","commonConfig","isAutoGLM","reasoningEffortConfig","reasoningEffortDebugMessage","resolveReasoningConfig","shouldUseOriginalImageDetail","shouldForceOriginalImageDetail","messagesWithImageDetail","msg","Array","part","streamSignal","cleanupStreamSignal","buildRequestAbortSignal","stream","chunk","reasoning_content","chunkData","estimatedTokens","Math","finalChunk","retryCount","retryInterval","maxAttempts","lastError","attempt","attemptSignal","cleanupAttemptSignal","result","JSON","wasHardTimeout","isHardTimeoutError","Promise","resolve","setTimeout","e","newError","callAIWithObjectResponse","response","assert","jsonContent","safeParseJson","callAIWithStringResponse","msgs","extractJSONFromCodeBlock","jsonMatch","codeBlockMatch","jsonLikeMatch","preprocessDoubaoBboxJson","input","reasoningEnabled","reasoningEffort","reasoningBudget","SUPPORTED_REASONING_FAMILIES","isSupportedReasoningFamily","family","supportedReasoningFamilyNames","hasExplicitConfig","effectiveReasoningEnabled","debugMessages","config","normalizeJsonObject","obj","item","normalized","key","Object","trimmedKey","normalizedValue","cleanJsonString","jsonrepair","isUITars","jsonString","String"],"mappings":";;;;;;;;;;;;;;;;;;;AAIO,MAAMA,6BAA6BC;IAIxC,YAAYC,OAAe,EAAEC,WAAmB,EAAEC,KAAmB,CAAE;QACrE,KAAK,CAACF,UAJR,yCACA;QAIE,IAAI,CAAC,IAAI,GAAG;QACZ,IAAI,CAAC,WAAW,GAAGC;QACnB,IAAI,CAAC,KAAK,GAAGC;IACf;AACF;AA+BA,eAAeC,iBAAiB,EAC9BC,WAAW,EAGZ;IAOC,MAAM,EACJC,UAAU,EACVC,SAAS,EACTC,SAAS,EACTC,aAAa,EACbC,YAAY,EACZC,iBAAiB,EACjBC,gBAAgB,EAChBC,kBAAkB,EAClBC,WAAW,EACXC,kBAAkB,EAClBC,OAAO,EACR,GAAGX;IAEJ,IAAIY;IACJ,MAAMC,aAAaC,SAAS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,KAAkB;QAC3B,MAAME,aAAkB,CAAC;QAEzB,KAAK,MAAM,CAACC,KAAKtE,MAAM,IAAIuE,OAAO,OAAO,CAACJ,KAAM;YAE9C,MAAMK,aAAaF,IAAI,IAAI;YAG3B,IAAIG,kBAAkBP,oBAAoBlE;YAG1C,IAAI,AAA2B,YAA3B,OAAOyE,iBACTA,kBAAkBA,gBAAgB,IAAI;YAGxCJ,UAAU,CAACG,WAAW,GAAGC;QAC3B;QAEA,OAAOJ;IACT;IAGA,IAAI,AAAe,YAAf,OAAOF,KACT,OAAOA,IAAI,IAAI;IAIjB,OAAOA;AACT;AAEO,SAASrB,cACdQ,KAAa,EACbpH,WAAqC;IAErC,MAAMwI,kBAAkBzB,yBAAyBK;IAEjD,IAAIoB,iBAAiB,MAAM,oBACzB,OAAOA,gBACJ,KAAK,CAAC,oBACL,MAAM,GACP,IAAItH;IAGT,IAAIR;IACJ,IAAIiF;IACJ,IAAI;QACFjF,SAASsF,KAAK,KAAK,CAACwC;QACpB,OAAOR,oBAAoBtH;IAC7B,EAAE,OAAOY,OAAO;QACdqE,YAAYrE;IACd;IACA,IAAI;QACFZ,SAASsF,KAAK,KAAK,CAACyC,WAAWD;QAC/B,OAAOR,oBAAoBtH;IAC7B,EAAE,OAAOY,OAAO;QACdqE,YAAYrE;IACd;IAEA,IACEtB,AAAgB,oBAAhBA,eACAA,AAAgB,kBAAhBA,eACA0I,SAAS1I,cACT;QACA,MAAM2I,aAAaxB,yBAAyBqB;QAC5C,IAAI;YACF9H,SAASsF,KAAK,KAAK,CAACyC,WAAWE;YAC/B,OAAOX,oBAAoBtH;QAC7B,EAAE,OAAOY,OAAO;YACdqE,YAAYrE;QACd;IACF;IACA,MAAMpC,MACJ,CAAC,gDAAgD,EAAE0J,OACjDjD,aAAa,iBACb,gBAAgB,EAAEyB,OAAO;AAE/B"}
@@ -14,13 +14,33 @@ function _define_property(obj, key, value) {
14
14
  class AbstractInterface {
15
15
  constructor(){
16
16
  _define_property(this, "mjpegStreamUrl", void 0);
17
+ _define_property(this, "inputPrimitives", void 0);
17
18
  }
18
19
  }
19
20
  const defineAction = (config)=>config;
21
+ function pointFromLocate(locate, missingMessage) {
22
+ if (!locate) throw new Error(missingMessage);
23
+ return {
24
+ x: locate.center[0],
25
+ y: locate.center[1]
26
+ };
27
+ }
28
+ function defineLocatedPointAction(config) {
29
+ return defineAction({
30
+ name: config.name,
31
+ description: config.description,
32
+ interfaceAlias: config.interfaceAlias,
33
+ paramSchema: config.paramSchema,
34
+ sample: config.sample,
35
+ call: async (param)=>{
36
+ await config.call(pointFromLocate(param.locate, config.missingLocateMessage), param);
37
+ }
38
+ });
39
+ }
20
40
  const actionTapParamSchema = z.object({
21
41
  locate: getMidsceneLocationSchema().describe('The element to be tapped')
22
42
  });
23
- const defineActionTap = (call)=>defineAction({
43
+ const defineActionTap = (tap)=>defineLocatedPointAction({
24
44
  name: 'Tap',
25
45
  description: 'Tap the element',
26
46
  interfaceAlias: 'aiTap',
@@ -30,12 +50,15 @@ const defineActionTap = (call)=>defineAction({
30
50
  prompt: 'the "Submit" button'
31
51
  }
32
52
  },
33
- call
53
+ missingLocateMessage: 'Element not found, cannot tap',
54
+ call: async (point)=>{
55
+ await tap(point);
56
+ }
34
57
  });
35
58
  const actionRightClickParamSchema = z.object({
36
59
  locate: getMidsceneLocationSchema().describe('The element to be right clicked')
37
60
  });
38
- const defineActionRightClick = (call)=>defineAction({
61
+ const defineActionRightClick = (rightClick)=>defineLocatedPointAction({
39
62
  name: 'RightClick',
40
63
  description: 'Right click the element',
41
64
  interfaceAlias: 'aiRightClick',
@@ -45,12 +68,15 @@ const defineActionRightClick = (call)=>defineAction({
45
68
  prompt: 'the file icon on the desktop'
46
69
  }
47
70
  },
48
- call
71
+ missingLocateMessage: 'Element not found, cannot right click',
72
+ call: async (point)=>{
73
+ await rightClick(point);
74
+ }
49
75
  });
50
76
  const actionDoubleClickParamSchema = z.object({
51
77
  locate: getMidsceneLocationSchema().describe('The element to be double clicked')
52
78
  });
53
- const defineActionDoubleClick = (call)=>defineAction({
79
+ const defineActionDoubleClick = (doubleClick)=>defineLocatedPointAction({
54
80
  name: 'DoubleClick',
55
81
  description: 'Double click the element',
56
82
  interfaceAlias: 'aiDoubleClick',
@@ -60,12 +86,15 @@ const defineActionDoubleClick = (call)=>defineAction({
60
86
  prompt: 'the folder icon'
61
87
  }
62
88
  },
63
- call
89
+ missingLocateMessage: 'Element not found, cannot double click',
90
+ call: async (point)=>{
91
+ await doubleClick(point);
92
+ }
64
93
  });
65
94
  const actionHoverParamSchema = z.object({
66
95
  locate: getMidsceneLocationSchema().describe('The element to be hovered')
67
96
  });
68
- const defineActionHover = (call)=>defineAction({
97
+ const defineActionHover = (hover)=>defineLocatedPointAction({
69
98
  name: 'Hover',
70
99
  description: 'Move the mouse to the element',
71
100
  interfaceAlias: 'aiHover',
@@ -75,7 +104,10 @@ const defineActionHover = (call)=>defineAction({
75
104
  prompt: 'the navigation menu item "Products"'
76
105
  }
77
106
  },
78
- call
107
+ missingLocateMessage: 'Element not found, cannot hover',
108
+ call: async (point)=>{
109
+ await hover(point);
110
+ }
79
111
  });
80
112
  const inputLocateDescription = 'the position of the placeholder or text content in the target input field. If there is no content, locate the center of the input field.';
81
113
  const actionInputParamSchema = z.object({
@@ -88,9 +120,10 @@ const actionInputParamSchema = z.object({
88
120
  'replace',
89
121
  'clear',
90
122
  'typeOnly'
91
- ]).default('replace').describe('Input mode: "replace" (default) - clear the field and input the value; "typeOnly" - type the value directly without clearing the field first; "clear" - clear the field without inputting new text.')
123
+ ]).default('replace').describe('Input mode: "replace" (default) - clear the field and input the value; "typeOnly" - type the value directly without clearing the field first; "clear" - clear the field without inputting new text.'),
124
+ autoDismissKeyboard: z.boolean().optional().describe('If true, the keyboard will be dismissed after the input is completed. Do not set it unless the user asks you to do so.')
92
125
  });
93
- const defineActionInput = (call)=>defineAction({
126
+ const defineActionInput = (keyboard)=>defineAction({
94
127
  name: 'Input',
95
128
  description: 'Input the value into the element',
96
129
  interfaceAlias: 'aiInput',
@@ -101,16 +134,22 @@ const defineActionInput = (call)=>defineAction({
101
134
  prompt: 'the email input field'
102
135
  }
103
136
  },
104
- call: (param)=>{
137
+ call: async (param)=>{
105
138
  if ('append' === param.mode) param.mode = 'typeOnly';
106
- return call(param);
139
+ if ('clear' === param.mode) return void await keyboard.clearInput(param.locate);
140
+ if (!param || !param.value) return;
141
+ await keyboard.typeText(param.value, {
142
+ target: param.locate,
143
+ replace: 'typeOnly' !== param.mode,
144
+ autoDismissKeyboard: param.autoDismissKeyboard
145
+ });
107
146
  }
108
147
  });
109
148
  const actionKeyboardPressParamSchema = z.object({
110
149
  locate: getMidsceneLocationSchema().describe('The element to be clicked before pressing the key').optional(),
111
150
  keyName: z.string().describe("The key to be pressed. Use '+' for key combinations, e.g., 'Control+A', 'Shift+Enter'")
112
151
  });
113
- const defineActionKeyboardPress = (call)=>defineAction({
152
+ const defineActionKeyboardPress = (keyboardPress)=>defineAction({
114
153
  name: 'KeyboardPress',
115
154
  description: 'Press a key or key combination, like "Enter", "Tab", "Escape", or "Control+A", "Shift+Enter". Do not use this to type text.',
116
155
  interfaceAlias: 'aiKeyboardPress',
@@ -118,7 +157,11 @@ const defineActionKeyboardPress = (call)=>defineAction({
118
157
  sample: {
119
158
  keyName: 'Enter'
120
159
  },
121
- call
160
+ call: async (param)=>{
161
+ await keyboardPress(param.keyName, {
162
+ target: param.locate
163
+ });
164
+ }
122
165
  });
123
166
  const actionScrollParamSchema = z.object({
124
167
  scrollType: z["enum"]([
@@ -137,7 +180,7 @@ const actionScrollParamSchema = z.object({
137
180
  distance: z.number().nullable().optional().describe('The distance in pixels to scroll'),
138
181
  locate: getMidsceneLocationSchema().optional().describe('Describe the target element to be scrolled on, like "the table" or "the list" or "the content area" or "the scrollable area". Do NOT provide a general intent like "scroll to find some element"')
139
182
  });
140
- const defineActionScroll = (call)=>defineAction({
183
+ const defineActionScroll = (scroll)=>defineAction({
141
184
  name: 'Scroll',
142
185
  description: 'Scroll the page or a scrollable element to browse content. This is the preferred way to scroll on all platforms, including mobile. Supports scrollToBottom/scrollToTop for boundary navigation. Default: direction `down`, scrollType `singleAction`, distance `null`.',
143
186
  interfaceAlias: 'aiScroll',
@@ -149,13 +192,15 @@ const defineActionScroll = (call)=>defineAction({
149
192
  prompt: 'the center of the product list area'
150
193
  }
151
194
  },
152
- call
195
+ call: async (param)=>{
196
+ await scroll(param);
197
+ }
153
198
  });
154
199
  const actionDragAndDropParamSchema = z.object({
155
200
  from: getMidsceneLocationSchema().describe('The position to be dragged'),
156
201
  to: getMidsceneLocationSchema().describe('The position to be dropped')
157
202
  });
158
- const defineActionDragAndDrop = (call)=>defineAction({
203
+ const defineActionDragAndDrop = (dragAndDrop)=>defineAction({
159
204
  name: 'DragAndDrop',
160
205
  description: 'Pick up a specific UI element and move it to a new position (e.g., reorder a card, move a file into a folder, sort list items). The element itself moves with your finger/mouse.',
161
206
  interfaceAlias: 'aiDragAndDrop',
@@ -168,13 +213,25 @@ const defineActionDragAndDrop = (call)=>defineAction({
168
213
  prompt: 'the upload drop zone'
169
214
  }
170
215
  },
171
- call
216
+ call: async (param)=>{
217
+ const from = param.from;
218
+ const to = param.to;
219
+ if (!from) throw new Error('missing "from" param for drag and drop');
220
+ if (!to) throw new Error('missing "to" param for drag and drop');
221
+ await dragAndDrop({
222
+ x: from.center[0],
223
+ y: from.center[1]
224
+ }, {
225
+ x: to.center[0],
226
+ y: to.center[1]
227
+ });
228
+ }
172
229
  });
173
230
  const ActionLongPressParamSchema = z.object({
174
231
  locate: getMidsceneLocationSchema().describe('The element to be long pressed'),
175
232
  duration: z.number().optional().describe('Long press duration in milliseconds')
176
233
  });
177
- const defineActionLongPress = (call)=>defineAction({
234
+ const defineActionLongPress = (longPress)=>defineLocatedPointAction({
178
235
  name: 'LongPress',
179
236
  description: 'Long press the element',
180
237
  interfaceAlias: 'aiLongPress',
@@ -184,7 +241,12 @@ const defineActionLongPress = (call)=>defineAction({
184
241
  prompt: 'the message bubble'
185
242
  }
186
243
  },
187
- call
244
+ missingLocateMessage: 'LongPress requires an element to be located',
245
+ call: async (point, param)=>{
246
+ await longPress(point, {
247
+ duration: param.duration
248
+ });
249
+ }
188
250
  });
189
251
  const ActionSwipeParamSchema = z.object({
190
252
  start: getMidsceneLocationSchema().optional().describe('Starting point of the swipe gesture, if not specified, the center of the page will be used'),
@@ -234,7 +296,7 @@ function normalizeMobileSwipeParam(param, screenSize) {
234
296
  repeatCount
235
297
  };
236
298
  }
237
- const defineActionSwipe = (call)=>defineAction({
299
+ const defineActionSwipe = (config)=>defineAction({
238
300
  name: 'Swipe',
239
301
  description: 'Perform a touch gesture for interactions beyond regular scrolling (e.g., flip pages in a carousel, dismiss a notification, swipe-to-delete a list item). For regular content scrolling, use Scroll instead. Use "distance" + "direction" for relative movement, or "end" for precise endpoint.',
240
302
  paramSchema: ActionSwipeParamSchema,
@@ -246,12 +308,17 @@ const defineActionSwipe = (call)=>defineAction({
246
308
  prompt: 'upper edge of the screen'
247
309
  }
248
310
  },
249
- call
311
+ call: async (param)=>{
312
+ const { startPoint, endPoint, duration, repeatCount } = normalizeMobileSwipeParam(param, await config.size());
313
+ for(let i = 0; i < repeatCount; i++)await config.swipe(startPoint, endPoint, {
314
+ duration
315
+ });
316
+ }
250
317
  });
251
318
  const actionClearInputParamSchema = z.object({
252
319
  locate: getMidsceneLocationSchema().describe('The input field to be cleared').optional()
253
320
  });
254
- const defineActionClearInput = (call)=>defineAction({
321
+ const defineActionClearInput = (clearInput)=>defineAction({
255
322
  name: 'ClearInput',
256
323
  description: inputLocateDescription,
257
324
  interfaceAlias: 'aiClearInput',
@@ -261,7 +328,9 @@ const defineActionClearInput = (call)=>defineAction({
261
328
  prompt: 'the search input field'
262
329
  }
263
330
  },
264
- call
331
+ call: async (param)=>{
332
+ await clearInput(param.locate);
333
+ }
265
334
  });
266
335
  const actionCursorMoveParamSchema = z.object({
267
336
  direction: z["enum"]([
@@ -270,7 +339,7 @@ const actionCursorMoveParamSchema = z.object({
270
339
  ]).describe('The direction to move the cursor'),
271
340
  times: z.number().int().min(1).default(1).describe('The number of times to move the cursor in the specified direction')
272
341
  });
273
- const defineActionCursorMove = (call)=>defineAction({
342
+ const defineActionCursorMove = (config)=>defineAction({
274
343
  name: 'CursorMove',
275
344
  description: 'Move the text cursor (caret) left or right within an input field or text area. Use this to reposition the cursor without selecting text.',
276
345
  paramSchema: actionCursorMoveParamSchema,
@@ -278,7 +347,16 @@ const defineActionCursorMove = (call)=>defineAction({
278
347
  direction: 'left',
279
348
  times: 3
280
349
  },
281
- call
350
+ call: async (param)=>{
351
+ const times = param.times ?? 1;
352
+ if (config.keyboard.cursorMove) return void await config.keyboard.cursorMove(param.direction, times);
353
+ const wait = config.sleep ?? ((timeMs)=>new Promise((resolve)=>setTimeout(resolve, timeMs)));
354
+ const arrowKey = 'left' === param.direction ? 'ArrowLeft' : 'ArrowRight';
355
+ for(let i = 0; i < times; i++){
356
+ await config.keyboard.keyboardPress(arrowKey);
357
+ await wait(100);
358
+ }
359
+ }
282
360
  });
283
361
  const ActionPinchParamSchema = z.object({
284
362
  locate: getMidsceneLocationSchema().optional().describe('The element to pinch on. If not specified, the center of the screen will be used'),
@@ -289,7 +367,9 @@ const ActionPinchParamSchema = z.object({
289
367
  distance: z.number().positive().optional().describe('How far each finger moves in pixels. Defaults to a quarter of the shorter screen dimension.'),
290
368
  duration: z.number().default(500).optional().describe('Duration of the pinch gesture in milliseconds')
291
369
  });
292
- const defineActionPinch = (call)=>defineAction({
370
+ const defineActionPinch = (config)=>{
371
+ if (!config.pinch) return;
372
+ return defineAction({
293
373
  name: 'Pinch',
294
374
  description: 'Perform a two-finger pinch gesture. Use direction "in" to pinch fingers together (zoom out), or "out" to spread fingers apart (zoom in). Optionally specify distance for how far each finger moves.',
295
375
  interfaceAlias: 'aiPinch',
@@ -301,8 +381,19 @@ const defineActionPinch = (call)=>defineAction({
301
381
  direction: 'out',
302
382
  distance: 200
303
383
  },
304
- call
384
+ call: async (param)=>{
385
+ const { centerX, centerY, startDistance, endDistance, duration } = normalizePinchParam(param, await config.size());
386
+ await config.pinch?.({
387
+ x: centerX,
388
+ y: centerY
389
+ }, {
390
+ startDistance,
391
+ endDistance,
392
+ duration
393
+ });
394
+ }
305
395
  });
396
+ };
306
397
  function normalizePinchParam(param, screenSize) {
307
398
  const { width, height } = screenSize;
308
399
  const element = param.locate;
@@ -321,6 +412,55 @@ function normalizePinchParam(param, screenSize) {
321
412
  duration
322
413
  };
323
414
  }
415
+ function defineSystemInputAction(config, call) {
416
+ return defineAction({
417
+ name: config.name,
418
+ description: config.description,
419
+ interfaceAlias: config.interfaceAlias,
420
+ delayBeforeRunner: config.delayBeforeRunner,
421
+ delayAfterRunner: config.delayAfterRunner,
422
+ call
423
+ });
424
+ }
425
+ function defineActionsFromInputPrimitives(input, options = {}) {
426
+ const actions = [];
427
+ const { pointer, keyboard, scroll, touch, system } = input;
428
+ if (pointer) {
429
+ actions.push(defineActionTap(pointer.tap));
430
+ if (pointer.doubleClick) actions.push(defineActionDoubleClick(pointer.doubleClick));
431
+ if (pointer.rightClick) actions.push(defineActionRightClick(pointer.rightClick));
432
+ if (pointer.hover) actions.push(defineActionHover(pointer.hover));
433
+ if (pointer.dragAndDrop) actions.push(defineActionDragAndDrop(pointer.dragAndDrop));
434
+ if (pointer.longPress) actions.push(defineActionLongPress(pointer.longPress));
435
+ }
436
+ if (keyboard) actions.push(defineActionInput(keyboard), defineActionClearInput(keyboard.clearInput), defineActionKeyboardPress(keyboard.keyboardPress), defineActionCursorMove({
437
+ keyboard,
438
+ sleep: options.sleep
439
+ }));
440
+ if (scroll) actions.push(defineActionScroll(scroll.scroll));
441
+ if (touch?.swipe && options.size && false !== options.includeSwipe) actions.push(defineActionSwipe({
442
+ swipe: touch.swipe,
443
+ size: options.size
444
+ }));
445
+ if (touch?.pinch && options.size && false !== options.includePinch) actions.push(defineActionPinch({
446
+ pinch: touch.pinch,
447
+ size: options.size
448
+ }));
449
+ if (system && options.systemActions) {
450
+ const { systemActions } = options;
451
+ if (system.backButton && systemActions.backButton) actions.push(defineSystemInputAction(systemActions.backButton, system.backButton));
452
+ if (system.homeButton && systemActions.homeButton) actions.push(defineSystemInputAction(systemActions.homeButton, system.homeButton));
453
+ if (system.recentAppsButton && systemActions.recentAppsButton) actions.push(defineSystemInputAction(systemActions.recentAppsButton, system.recentAppsButton));
454
+ }
455
+ return actions.filter((action)=>Boolean(action));
456
+ }
457
+ function createDefaultMobileActions(context) {
458
+ return defineActionsFromInputPrimitives(context.input, {
459
+ size: context.size,
460
+ sleep: context.sleep,
461
+ systemActions: context.systemActions
462
+ });
463
+ }
324
464
  const ActionSleepParamSchema = z.object({
325
465
  timeMs: z.number().default(1000).optional().describe('Sleep duration in milliseconds, defaults to 1000ms (1 second)')
326
466
  });
@@ -337,6 +477,6 @@ const defineActionSleep = ()=>defineAction({
337
477
  await new Promise((resolve)=>setTimeout(resolve, duration));
338
478
  }
339
479
  });
340
- export { AbstractInterface, ActionLongPressParamSchema, ActionPinchParamSchema, ActionSleepParamSchema, ActionSwipeParamSchema, actionClearInputParamSchema, actionCursorMoveParamSchema, actionDoubleClickParamSchema, actionDragAndDropParamSchema, actionHoverParamSchema, actionInputParamSchema, actionKeyboardPressParamSchema, actionRightClickParamSchema, actionScrollParamSchema, actionTapParamSchema, defineAction, defineActionClearInput, defineActionCursorMove, defineActionDoubleClick, defineActionDragAndDrop, defineActionHover, defineActionInput, defineActionKeyboardPress, defineActionLongPress, defineActionPinch, defineActionRightClick, defineActionScroll, defineActionSleep, defineActionSwipe, defineActionTap, normalizeMobileSwipeParam, normalizePinchParam };
480
+ export { AbstractInterface, ActionLongPressParamSchema, ActionPinchParamSchema, ActionSleepParamSchema, ActionSwipeParamSchema, actionClearInputParamSchema, actionCursorMoveParamSchema, actionDoubleClickParamSchema, actionDragAndDropParamSchema, actionHoverParamSchema, actionInputParamSchema, actionKeyboardPressParamSchema, actionRightClickParamSchema, actionScrollParamSchema, actionTapParamSchema, createDefaultMobileActions, defineAction, defineActionClearInput, defineActionCursorMove, defineActionDoubleClick, defineActionDragAndDrop, defineActionHover, defineActionInput, defineActionKeyboardPress, defineActionLongPress, defineActionPinch, defineActionRightClick, defineActionScroll, defineActionSleep, defineActionSwipe, defineActionTap, defineActionsFromInputPrimitives, normalizeMobileSwipeParam, normalizePinchParam };
341
481
 
342
482
  //# sourceMappingURL=index.mjs.map