@midscene/core 1.7.3 → 1.7.5-beta-20260418223706.0

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 (41) hide show
  1. package/dist/es/agent/ui-utils.mjs +13 -1
  2. package/dist/es/agent/ui-utils.mjs.map +1 -1
  3. package/dist/es/agent/utils.mjs +1 -1
  4. package/dist/es/ai-model/service-caller/index.mjs +94 -78
  5. package/dist/es/ai-model/service-caller/index.mjs.map +1 -1
  6. package/dist/es/ai-model/service-caller/request-timeout.mjs +49 -0
  7. package/dist/es/ai-model/service-caller/request-timeout.mjs.map +1 -0
  8. package/dist/es/common.mjs +6 -1
  9. package/dist/es/common.mjs.map +1 -1
  10. package/dist/es/dump/screenshot-store.mjs +7 -6
  11. package/dist/es/dump/screenshot-store.mjs.map +1 -1
  12. package/dist/es/report-generator.mjs +21 -20
  13. package/dist/es/report-generator.mjs.map +1 -1
  14. package/dist/es/report.mjs +20 -1
  15. package/dist/es/report.mjs.map +1 -1
  16. package/dist/es/utils.mjs +2 -2
  17. package/dist/es/yaml/player.mjs +1 -16
  18. package/dist/es/yaml/player.mjs.map +1 -1
  19. package/dist/lib/agent/ui-utils.js +13 -1
  20. package/dist/lib/agent/ui-utils.js.map +1 -1
  21. package/dist/lib/agent/utils.js +1 -1
  22. package/dist/lib/ai-model/service-caller/index.js +94 -78
  23. package/dist/lib/ai-model/service-caller/index.js.map +1 -1
  24. package/dist/lib/ai-model/service-caller/request-timeout.js +95 -0
  25. package/dist/lib/ai-model/service-caller/request-timeout.js.map +1 -0
  26. package/dist/lib/common.js +6 -1
  27. package/dist/lib/common.js.map +1 -1
  28. package/dist/lib/dump/screenshot-store.js +6 -5
  29. package/dist/lib/dump/screenshot-store.js.map +1 -1
  30. package/dist/lib/report-generator.js +19 -18
  31. package/dist/lib/report-generator.js.map +1 -1
  32. package/dist/lib/report.js +19 -0
  33. package/dist/lib/report.js.map +1 -1
  34. package/dist/lib/types.js +2 -2
  35. package/dist/lib/utils.js +2 -2
  36. package/dist/lib/yaml/player.js +1 -16
  37. package/dist/lib/yaml/player.js.map +1 -1
  38. package/dist/types/ai-model/service-caller/request-timeout.d.ts +32 -0
  39. package/dist/types/dump/screenshot-store.d.ts +2 -2
  40. package/dist/types/report-generator.d.ts +5 -0
  41. package/package.json +2 -2
@@ -82,7 +82,19 @@ function paramStr(task) {
82
82
  if ('object' == typeof value) {
83
83
  const locateStr = locateParamStr(value);
84
84
  if (locateStr) return locateStr;
85
- return JSON.stringify(value, void 0, 2);
85
+ const entries = Object.entries(value);
86
+ if (0 === entries.length) return '';
87
+ const formatValue = (v)=>{
88
+ if ('string' == typeof v) return v;
89
+ if (null == v) return String(v);
90
+ if ('object' == typeof v) return JSON.stringify(v);
91
+ return String(v);
92
+ };
93
+ if (1 === entries.length) {
94
+ const [key, v] = entries[0];
95
+ return `${key}: ${formatValue(v)}`;
96
+ }
97
+ return entries.map(([key, v])=>`${key}: ${formatValue(v)}`).join(', ');
86
98
  }
87
99
  return String(value);
88
100
  }
@@ -1 +1 @@
1
- {"version":3,"file":"agent/ui-utils.mjs","sources":["../../../src/agent/ui-utils.ts"],"sourcesContent":["import type {\n DetailedLocateParam,\n ExecutionTask,\n ExecutionTaskAction,\n ExecutionTaskInsightAssertion,\n ExecutionTaskInsightQuery,\n ExecutionTaskPlanning,\n ExecutionTaskPlanningLocate,\n PullParam,\n ScrollParam,\n} from '@/types';\n\nexport function typeStr(task: ExecutionTask) {\n // // For Action tasks with subType, show \"Action Space / subType\"\n // if (task.type === 'Action' && task.subType) {\n // return `Action Space / ${task.subType}`;\n // }\n\n // // For all other cases with subType, show \"type / subType\"\n // if (task.subType) {\n // return `${task.type} / ${task.subType}`;\n // }\n\n // No subType, just show type\n return task.subType || task.type;\n}\n\nexport function locateParamStr(locate?: DetailedLocateParam | string): string {\n if (!locate) {\n return '';\n }\n\n if (typeof locate === 'string') {\n return locate;\n }\n\n if (typeof locate === 'object') {\n // Check for nested prompt.prompt (Planning Locate tasks)\n if (\n typeof locate.prompt === 'object' &&\n locate.prompt !== null &&\n locate.prompt.prompt\n ) {\n const prompt = locate.prompt.prompt;\n return prompt;\n }\n\n // Check for direct prompt string\n if (typeof locate.prompt === 'string') {\n return locate.prompt;\n }\n\n // Check for description field (Action Space tasks like Tap, Hover)\n if (typeof (locate as any).description === 'string') {\n return (locate as any).description;\n }\n }\n\n return '';\n}\n\nexport function scrollParamStr(scrollParam?: ScrollParam) {\n if (!scrollParam) {\n return '';\n }\n return `${scrollParam.direction || 'down'}, ${scrollParam.scrollType || 'singleAction'}, ${scrollParam.distance || 'distance-not-set'}`;\n}\n\nexport function pullParamStr(pullParam?: PullParam) {\n if (!pullParam) {\n return '';\n }\n const parts: string[] = [];\n parts.push(`direction: ${pullParam.direction || 'down'}`);\n if (pullParam.distance) {\n parts.push(`distance: ${pullParam.distance}`);\n }\n if (pullParam.duration) {\n parts.push(`duration: ${pullParam.duration}ms`);\n }\n return parts.join(', ');\n}\n\nexport function extractInsightParam(taskParam: any): {\n content: string;\n images?: Array<{ name: string; url: string }>;\n} {\n if (!taskParam) {\n return { content: '' };\n }\n\n // Helper to extract images from multimodalPrompt\n const extractImages = (source: any) => {\n return source?.multimodalPrompt?.images &&\n Array.isArray(source.multimodalPrompt.images)\n ? source.multimodalPrompt.images\n : undefined;\n };\n\n // Helper to stringify if needed\n const toContent = (value: any) =>\n typeof value === 'string' ? value : JSON.stringify(value);\n\n // Extract from demand\n if (taskParam.demand) {\n return {\n content: toContent(taskParam.demand),\n images: extractImages(taskParam),\n };\n }\n\n // Extract from assertion\n if (taskParam.assertion) {\n return {\n content: toContent(taskParam.assertion),\n images: extractImages(taskParam),\n };\n }\n\n // Extract from dataDemand\n if (taskParam.dataDemand) {\n const { dataDemand } = taskParam;\n\n if (typeof dataDemand === 'string') {\n return { content: dataDemand };\n }\n\n if (typeof dataDemand === 'object') {\n return {\n content: toContent(dataDemand.demand || dataDemand),\n images: extractImages(dataDemand),\n };\n }\n }\n\n return { content: '' };\n}\n\nexport function taskTitleStr(\n type:\n | 'Tap'\n | 'Hover'\n | 'Input'\n | 'RightClick'\n | 'KeyboardPress'\n | 'Scroll'\n | 'Act'\n | 'Query'\n | 'Assert'\n | 'WaitFor'\n | 'Locate'\n | 'Boolean'\n | 'Number'\n | 'String',\n prompt: string,\n) {\n if (prompt) {\n return `${type} - ${prompt}`;\n }\n return type;\n}\n\nexport function paramStr(task: ExecutionTask) {\n let value: string | undefined | object;\n if (task.type === 'Planning') {\n if (task.subType === 'Locate') {\n value = locateParamStr((task as ExecutionTaskPlanningLocate)?.param);\n } else {\n // Prefer AI-generated output.log over user input\n const planTask = task as ExecutionTaskPlanning;\n value = planTask.output?.log || planTask.param?.userInstruction;\n }\n }\n\n if (task.type === 'Insight') {\n value = extractInsightParam((task as any)?.param).content;\n }\n\n if (task.type === 'Action Space') {\n const locate = (task as ExecutionTaskAction)?.param?.locate;\n const locateStr = locate ? locateParamStr(locate) : '';\n\n value = task.thought || '';\n if (typeof (task as ExecutionTaskAction)?.param?.timeMs === 'number') {\n value = `${(task as ExecutionTaskAction)?.param?.timeMs}ms`;\n } else if (\n typeof (task as ExecutionTaskAction)?.param?.scrollType === 'string'\n ) {\n value = scrollParamStr((task as ExecutionTaskAction)?.param);\n } else if (\n typeof (task as ExecutionTaskAction)?.param?.direction === 'string' &&\n (task as ExecutionTaskAction)?.subType === 'PullGesture'\n ) {\n value = pullParamStr((task as ExecutionTaskAction)?.param);\n } else if (\n typeof (task as ExecutionTaskAction)?.param?.value !== 'undefined'\n ) {\n value = (task as ExecutionTaskAction)?.param?.value;\n } else if (\n (task as ExecutionTaskAction)?.param &&\n typeof (task as ExecutionTaskAction)?.param === 'object' &&\n Object.keys((task as ExecutionTaskAction)?.param || {}).length > 0\n ) {\n // General parameter handling for actions with custom parameters\n // (e.g., runWdaRequest, runAdbShell)\n value = (task as ExecutionTaskAction)?.param;\n }\n\n if (locateStr) {\n if (value && typeof value !== 'object') {\n value = `${locateStr} - ${value}`;\n } else {\n value = locateStr;\n }\n }\n }\n\n if (typeof value === 'undefined') return '';\n\n if (typeof value === 'string') return value;\n\n if (typeof value === 'object') {\n const locateStr = locateParamStr(value as any);\n if (locateStr) {\n return locateStr;\n }\n return JSON.stringify(value, undefined, 2);\n }\n\n return String(value);\n}\n"],"names":["typeStr","task","locateParamStr","locate","prompt","scrollParamStr","scrollParam","pullParamStr","pullParam","parts","extractInsightParam","taskParam","extractImages","source","Array","undefined","toContent","value","JSON","dataDemand","taskTitleStr","type","paramStr","planTask","locateStr","Object","String"],"mappings":"AAYO,SAASA,QAAQC,IAAmB;IAYzC,OAAOA,KAAK,OAAO,IAAIA,KAAK,IAAI;AAClC;AAEO,SAASC,eAAeC,MAAqC;IAClE,IAAI,CAACA,QACH,OAAO;IAGT,IAAI,AAAkB,YAAlB,OAAOA,QACT,OAAOA;IAGT,IAAI,AAAkB,YAAlB,OAAOA,QAAqB;QAE9B,IACE,AAAyB,YAAzB,OAAOA,OAAO,MAAM,IACpBA,AAAkB,SAAlBA,OAAO,MAAM,IACbA,OAAO,MAAM,CAAC,MAAM,EACpB;YACA,MAAMC,SAASD,OAAO,MAAM,CAAC,MAAM;YACnC,OAAOC;QACT;QAGA,IAAI,AAAyB,YAAzB,OAAOD,OAAO,MAAM,EACtB,OAAOA,OAAO,MAAM;QAItB,IAAI,AAAuC,YAAvC,OAAQA,OAAe,WAAW,EACpC,OAAQA,OAAe,WAAW;IAEtC;IAEA,OAAO;AACT;AAEO,SAASE,eAAeC,WAAyB;IACtD,IAAI,CAACA,aACH,OAAO;IAET,OAAO,GAAGA,YAAY,SAAS,IAAI,OAAO,EAAE,EAAEA,YAAY,UAAU,IAAI,eAAe,EAAE,EAAEA,YAAY,QAAQ,IAAI,oBAAoB;AACzI;AAEO,SAASC,aAAaC,SAAqB;IAChD,IAAI,CAACA,WACH,OAAO;IAET,MAAMC,QAAkB,EAAE;IAC1BA,MAAM,IAAI,CAAC,CAAC,WAAW,EAAED,UA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AOF,QAAW;IAC1C;IAEA,OAAOW,OAAOT;AAChB"}
1
+ {"version":3,"file":"agent/ui-utils.mjs","sources":["../../../src/agent/ui-utils.ts"],"sourcesContent":["import type {\n DetailedLocateParam,\n ExecutionTask,\n ExecutionTaskAction,\n ExecutionTaskInsightAssertion,\n ExecutionTaskInsightQuery,\n ExecutionTaskPlanning,\n ExecutionTaskPlanningLocate,\n PullParam,\n ScrollParam,\n} from '@/types';\n\nexport function typeStr(task: ExecutionTask) {\n // // For Action tasks with subType, show \"Action Space / subType\"\n // if (task.type === 'Action' && task.subType) {\n // return `Action Space / ${task.subType}`;\n // }\n\n // // For all other cases with subType, show \"type / subType\"\n // if (task.subType) {\n // return `${task.type} / ${task.subType}`;\n // }\n\n // No subType, just show type\n return task.subType || task.type;\n}\n\nexport function locateParamStr(locate?: DetailedLocateParam | string): string {\n if (!locate) {\n return '';\n }\n\n if (typeof locate === 'string') {\n return locate;\n }\n\n if (typeof locate === 'object') {\n // Check for nested prompt.prompt (Planning Locate tasks)\n if (\n typeof locate.prompt === 'object' &&\n locate.prompt !== null &&\n locate.prompt.prompt\n ) {\n const prompt = locate.prompt.prompt;\n return prompt;\n }\n\n // Check for direct prompt string\n if (typeof locate.prompt === 'string') {\n return locate.prompt;\n }\n\n // Check for description field (Action Space tasks like Tap, Hover)\n if (typeof (locate as any).description === 'string') {\n return (locate as any).description;\n }\n }\n\n return '';\n}\n\nexport function scrollParamStr(scrollParam?: ScrollParam) {\n if (!scrollParam) {\n return '';\n }\n return `${scrollParam.direction || 'down'}, ${scrollParam.scrollType || 'singleAction'}, ${scrollParam.distance || 'distance-not-set'}`;\n}\n\nexport function pullParamStr(pullParam?: PullParam) {\n if (!pullParam) {\n return '';\n }\n const parts: string[] = [];\n parts.push(`direction: ${pullParam.direction || 'down'}`);\n if (pullParam.distance) {\n parts.push(`distance: ${pullParam.distance}`);\n }\n if (pullParam.duration) {\n parts.push(`duration: ${pullParam.duration}ms`);\n }\n return parts.join(', ');\n}\n\nexport function extractInsightParam(taskParam: any): {\n content: string;\n images?: Array<{ name: string; url: string }>;\n} {\n if (!taskParam) {\n return { content: '' };\n }\n\n // Helper to extract images from multimodalPrompt\n const extractImages = (source: any) => {\n return source?.multimodalPrompt?.images &&\n Array.isArray(source.multimodalPrompt.images)\n ? source.multimodalPrompt.images\n : undefined;\n };\n\n // Helper to stringify if needed\n const toContent = (value: any) =>\n typeof value === 'string' ? value : JSON.stringify(value);\n\n // Extract from demand\n if (taskParam.demand) {\n return {\n content: toContent(taskParam.demand),\n images: extractImages(taskParam),\n };\n }\n\n // Extract from assertion\n if (taskParam.assertion) {\n return {\n content: toContent(taskParam.assertion),\n images: extractImages(taskParam),\n };\n }\n\n // Extract from dataDemand\n if (taskParam.dataDemand) {\n const { dataDemand } = taskParam;\n\n if (typeof dataDemand === 'string') {\n return { content: dataDemand };\n }\n\n if (typeof dataDemand === 'object') {\n return {\n content: toContent(dataDemand.demand || dataDemand),\n images: extractImages(dataDemand),\n };\n }\n }\n\n return { content: '' };\n}\n\nexport function taskTitleStr(\n type:\n | 'Tap'\n | 'Hover'\n | 'Input'\n | 'RightClick'\n | 'KeyboardPress'\n | 'Scroll'\n | 'Act'\n | 'Query'\n | 'Assert'\n | 'WaitFor'\n | 'Locate'\n | 'Boolean'\n | 'Number'\n | 'String',\n prompt: string,\n) {\n if (prompt) {\n return `${type} - ${prompt}`;\n }\n return type;\n}\n\nexport function paramStr(task: ExecutionTask) {\n let value: string | undefined | object;\n if (task.type === 'Planning') {\n if (task.subType === 'Locate') {\n value = locateParamStr((task as ExecutionTaskPlanningLocate)?.param);\n } else {\n // Prefer AI-generated output.log over user input\n const planTask = task as ExecutionTaskPlanning;\n value = planTask.output?.log || planTask.param?.userInstruction;\n }\n }\n\n if (task.type === 'Insight') {\n value = extractInsightParam((task as any)?.param).content;\n }\n\n if (task.type === 'Action Space') {\n const locate = (task as ExecutionTaskAction)?.param?.locate;\n const locateStr = locate ? locateParamStr(locate) : '';\n\n value = task.thought || '';\n if (typeof (task as ExecutionTaskAction)?.param?.timeMs === 'number') {\n value = `${(task as ExecutionTaskAction)?.param?.timeMs}ms`;\n } else if (\n typeof (task as ExecutionTaskAction)?.param?.scrollType === 'string'\n ) {\n value = scrollParamStr((task as ExecutionTaskAction)?.param);\n } else if (\n typeof (task as ExecutionTaskAction)?.param?.direction === 'string' &&\n (task as ExecutionTaskAction)?.subType === 'PullGesture'\n ) {\n value = pullParamStr((task as ExecutionTaskAction)?.param);\n } else if (\n typeof (task as ExecutionTaskAction)?.param?.value !== 'undefined'\n ) {\n value = (task as ExecutionTaskAction)?.param?.value;\n } else if (\n (task as ExecutionTaskAction)?.param &&\n typeof (task as ExecutionTaskAction)?.param === 'object' &&\n Object.keys((task as ExecutionTaskAction)?.param || {}).length > 0\n ) {\n // General parameter handling for actions with custom parameters\n // (e.g., runWdaRequest, runAdbShell)\n value = (task as ExecutionTaskAction)?.param;\n }\n\n if (locateStr) {\n if (value && typeof value !== 'object') {\n value = `${locateStr} - ${value}`;\n } else {\n value = locateStr;\n }\n }\n }\n\n if (typeof value === 'undefined') return '';\n\n if (typeof value === 'string') return value;\n\n if (typeof value === 'object') {\n const locateStr = locateParamStr(value as any);\n if (locateStr) {\n return locateStr;\n }\n // Flatten `{key: \"raw value\"}` into `key: raw value` instead of emitting\n // a pretty-printed JSON string. JSON.stringify would escape every inner\n // quote as `\\\"`, and the UI renders the result as plain text — so a\n // command like `grep -E \"version\"` ends up shown with the literal\n // backslashes, which is both noisy and confusing for users.\n const entries = Object.entries(value as Record<string, unknown>);\n if (entries.length === 0) {\n return '';\n }\n const formatValue = (v: unknown): string => {\n if (typeof v === 'string') return v;\n if (v === null || v === undefined) return String(v);\n if (typeof v === 'object') return JSON.stringify(v);\n return String(v);\n };\n if (entries.length === 1) {\n const [key, v] = entries[0];\n return `${key}: ${formatValue(v)}`;\n }\n return entries.map(([key, v]) => `${key}: ${formatValue(v)}`).join(', ');\n }\n\n return String(value);\n}\n"],"names":["typeStr","task","locateParamStr","locate","prompt","scrollParamStr","scrollParam","pullParamStr","pullParam","parts","extractInsightParam","taskParam","extractImages","source","Array","undefined","toContent","value","JSON","dataDemand","taskTitleStr","type","paramStr","planTask","locateStr","Object","entries","formatValue","v","String","key"],"mappings":"AAYO,SAASA,QAAQC,IAAmB;IAYzC,OAAOA,KAAK,OAAO,IAAIA,KAAK,IAAI;AAClC;AAEO,SAASC,eAAeC,MAAqC;IAClE,IAAI,CAACA,QACH,OAAO;IAGT,IAAI,AAAkB,YAAlB,OAAOA,QACT,OAAOA;IAGT,IAAI,AAAkB,YAAlB,OAAOA,QAAqB;QAE9B,IACE,AAAyB,YAAzB,OAAOA,OAAO,MAAM,IACpBA,AAAkB,SAAlBA,OAAO,MAAM,IACbA,OAAO,MAAM,CAAC,MAAM,EACpB;YACA,MAAMC,SAASD,OAAO,MAAM,CAAC,MAAM;YACnC,OAAOC;QACT;QAGA,IAAI,AAAyB,YAAzB,OAAOD,OAAO,MAAM,EACtB,OAAOA,OAAO,MAAM;QAItB,IAAI,AAAuC,YAAvC,OAAQA,OAAe,WAAW,EACpC,OAAQA,OAAe,WAAW;IAEtC;IAEA,OAAO;AACT;AAEO,SAASE,eAAeC,WAAyB;IACtD,IAAI,CAACA,aACH,OAAO;IAET,OAAO,GAAGA,YAAY,SAAS,IAAI,OAAO,EAAE,EAAEA,YAAY,UAAU,IAAI,eAAe,EAAE,EAAEA,YAAY,QAAQ,IAAI,oBAAoB;AACzI;AAEO,SAASC,aAAaC,SAAqB;IAChD,IAAI,CAACA,WACH,OAAO;IAET,MAAMC,QAAkB,EAAE;IAC1BA,MAAM,IAAI,CAAC,CAAC,WAAW,EAAED,UAAU,SAAS,IAAI,QAAQ;IACxD,IAAIA,UAAU,QAAQ,EACpBC,MAAM,IAAI,CAAC,CAAC,UAAU,EAAED,UAAU,QAAQ,EAAE;IAE9C,IAAIA,UAAU,QAAQ,EACpBC,MAAM,IAAI,CAAC,CAAC,UAAU,EAAED,UAAU,QAAQ,CA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AAOA,GAAgB,OAAOV,KAAK,SAAS,CAACU;YACjD,OAAOC,OAAOD;QAChB;QACA,IAAIF,AAAmB,MAAnBA,QAAQ,MAAM,EAAQ;YACxB,MAAM,CAACI,KAAKF,EAAE,GAAGF,OAAO,CAAC,EAAE;YAC3B,OAAO,GAAGI,IAAI,EAAE,EAAEH,YAAYC,IAAI;QACpC;QACA,OAAOF,QAAQ,GAAG,CAAC,CAAC,CAACI,KAAKF,EAAE,GAAK,GAAGE,IAAI,EAAE,EAAEH,YAAYC,IAAI,EAAE,IAAI,CAAC;IACrE;IAEA,OAAOC,OAAOZ;AAChB"}
@@ -123,7 +123,7 @@ async function matchElementFromCache(context, cacheEntry, cachePrompt, cacheable
123
123
  return;
124
124
  }
125
125
  }
126
- const getMidsceneVersion = ()=>"1.7.3";
126
+ const getMidsceneVersion = ()=>"1.7.5-beta-20260418223706.0";
127
127
  const parsePrompt = (prompt)=>{
128
128
  if ('string' == typeof prompt) return {
129
129
  textPrompt: prompt,
@@ -6,6 +6,7 @@ import openai_0 from "openai";
6
6
  import { isAutoGLM, isUITars } from "../auto-glm/util.mjs";
7
7
  import { callAIWithCodexAppServer, isCodexAppServerProvider } from "./codex-app-server.mjs";
8
8
  import { shouldForceOriginalImageDetail } from "./image-detail.mjs";
9
+ import { buildRequestAbortSignal, isHardTimeoutError, resolveEffectiveTimeoutMs } from "./request-timeout.mjs";
9
10
  function _define_property(obj, key, value) {
10
11
  if (key in obj) Object.defineProperty(obj, key, {
11
12
  value: value,
@@ -87,6 +88,9 @@ async function createChatClient({ modelConfig }) {
87
88
  throw new Error(`Invalid SOCKS proxy URL: ${socksProxy}. Expected format: socks4://host:port, socks5://host:port, or with authentication: socks5://user:pass@host:port`);
88
89
  }
89
90
  }
91
+ const effectiveTimeoutMs = resolveEffectiveTimeoutMs({
92
+ timeout
93
+ });
90
94
  const openAIOptions = {
91
95
  baseURL: openaiBaseURL,
92
96
  apiKey: openaiApiKey,
@@ -96,8 +100,9 @@ async function createChatClient({ modelConfig }) {
96
100
  }
97
101
  } : {},
98
102
  ...openaiExtraConfig,
99
- ...'number' == typeof timeout ? {
100
- timeout
103
+ maxRetries: 0,
104
+ ...null !== effectiveTimeoutMs ? {
105
+ timeout: effectiveTimeoutMs
101
106
  } : {},
102
107
  dangerouslyAllowBrowser: true
103
108
  };
@@ -134,6 +139,7 @@ async function callAI(messages, modelConfig, options) {
134
139
  const { completion, modelName, modelDescription, uiTarsModelVersion, modelFamily } = await createChatClient({
135
140
  modelConfig
136
141
  });
142
+ const effectiveTimeoutMs = resolveEffectiveTimeoutMs(modelConfig);
137
143
  const extraBody = modelConfig.extraBody;
138
144
  const maxTokens = globalConfigManager.getEnvConfigValueAsNumber(MIDSCENE_MODEL_MAX_TOKENS) ?? globalConfigManager.getEnvConfigValueAsNumber(OPENAI_MAX_TOKENS);
139
145
  const debugCall = getDebug('ai:call');
@@ -220,55 +226,58 @@ async function callAI(messages, modelConfig, options) {
220
226
  try {
221
227
  debugCall(`sending ${isStreaming ? 'streaming ' : ''}request to ${modelName}`);
222
228
  if (isStreaming) {
223
- const stream = await completion.create({
224
- model: modelName,
225
- messages: messagesWithImageDetail,
226
- ...commonConfig,
227
- ...reasoningEffortConfig,
228
- ...extraBody
229
- }, {
230
- stream: true,
231
- ...options?.abortSignal ? {
232
- signal: options.abortSignal
233
- } : {}
234
- });
235
- requestId = stream._request_id;
236
- for await (const chunk of stream){
237
- const content = chunk.choices?.[0]?.delta?.content || '';
238
- const reasoning_content = chunk.choices?.[0]?.delta?.reasoning_content || '';
239
- if (chunk.usage) usage = chunk.usage;
240
- if (content || reasoning_content) {
241
- accumulated += content;
242
- accumulatedReasoning += reasoning_content;
243
- const chunkData = {
244
- content,
245
- reasoning_content,
246
- accumulated,
247
- isComplete: false,
248
- usage: void 0
249
- };
250
- options.onChunk(chunkData);
251
- }
252
- if (chunk.choices?.[0]?.finish_reason) {
253
- timeCost = Date.now() - startTime;
254
- if (!usage) {
255
- const estimatedTokens = Math.max(1, Math.floor(accumulated.length / 4));
256
- usage = {
257
- prompt_tokens: estimatedTokens,
258
- completion_tokens: estimatedTokens,
259
- total_tokens: 2 * estimatedTokens
229
+ const { signal: streamSignal, cleanup: cleanupStreamSignal } = buildRequestAbortSignal(effectiveTimeoutMs, options?.abortSignal);
230
+ try {
231
+ const stream = await completion.create({
232
+ model: modelName,
233
+ messages: messagesWithImageDetail,
234
+ ...commonConfig,
235
+ ...reasoningEffortConfig,
236
+ ...extraBody
237
+ }, {
238
+ stream: true,
239
+ signal: streamSignal
240
+ });
241
+ requestId = stream._request_id;
242
+ for await (const chunk of stream){
243
+ const content = chunk.choices?.[0]?.delta?.content || '';
244
+ const reasoning_content = chunk.choices?.[0]?.delta?.reasoning_content || '';
245
+ if (chunk.usage) usage = chunk.usage;
246
+ if (content || reasoning_content) {
247
+ accumulated += content;
248
+ accumulatedReasoning += reasoning_content;
249
+ const chunkData = {
250
+ content,
251
+ reasoning_content,
252
+ accumulated,
253
+ isComplete: false,
254
+ usage: void 0
260
255
  };
256
+ options.onChunk(chunkData);
257
+ }
258
+ if (chunk.choices?.[0]?.finish_reason) {
259
+ timeCost = Date.now() - startTime;
260
+ if (!usage) {
261
+ const estimatedTokens = Math.max(1, Math.floor(accumulated.length / 4));
262
+ usage = {
263
+ prompt_tokens: estimatedTokens,
264
+ completion_tokens: estimatedTokens,
265
+ total_tokens: 2 * estimatedTokens
266
+ };
267
+ }
268
+ const finalChunk = {
269
+ content: '',
270
+ accumulated,
271
+ reasoning_content: '',
272
+ isComplete: true,
273
+ usage: buildUsageInfo(usage, requestId)
274
+ };
275
+ options.onChunk(finalChunk);
276
+ break;
261
277
  }
262
- const finalChunk = {
263
- content: '',
264
- accumulated,
265
- reasoning_content: '',
266
- isComplete: true,
267
- usage: buildUsageInfo(usage, requestId)
268
- };
269
- options.onChunk(finalChunk);
270
- break;
271
278
  }
279
+ } finally{
280
+ cleanupStreamSignal();
272
281
  }
273
282
  content = accumulated;
274
283
  debugProfileStats(`streaming model, ${modelName}, mode, ${modelFamily || 'default'}, cost-ms, ${timeCost}, temperature, ${temperature ?? ''}`);
@@ -277,36 +286,43 @@ async function callAI(messages, modelConfig, options) {
277
286
  const retryInterval = modelConfig.retryInterval ?? 2000;
278
287
  const maxAttempts = retryCount + 1;
279
288
  let lastError;
280
- for(let attempt = 1; attempt <= maxAttempts; attempt++)try {
281
- const result = await completion.create({
282
- model: modelName,
283
- messages: messagesWithImageDetail,
284
- ...commonConfig,
285
- ...reasoningEffortConfig,
286
- ...extraBody
287
- }, options?.abortSignal ? {
288
- signal: options.abortSignal
289
- } : void 0);
290
- timeCost = Date.now() - startTime;
291
- debugProfileStats(`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 ?? ''}`);
292
- debugProfileDetail(`model usage detail: ${JSON.stringify(result.usage)}`);
293
- if (!result.choices) throw new Error(`invalid response from LLM service: ${JSON.stringify(result)}`);
294
- content = result.choices[0].message.content;
295
- accumulatedReasoning = result.choices[0].message?.reasoning_content || '';
296
- usage = result.usage;
297
- requestId = result._request_id;
298
- if (!hasUsableText(content) && hasUsableText(accumulatedReasoning)) {
299
- warnCall('empty content from AI model, using reasoning content');
300
- content = accumulatedReasoning;
301
- }
302
- if (!hasUsableText(content)) throw new AIResponseParseError('empty content from AI model', JSON.stringify(result), buildUsageInfo(usage, requestId));
303
- break;
304
- } catch (error) {
305
- lastError = error;
306
- if (options?.abortSignal?.aborted) break;
307
- if (attempt < maxAttempts) {
308
- warnCall(`AI call failed (attempt ${attempt}/${maxAttempts}), retrying in ${retryInterval}ms... Error: ${lastError.message}`);
309
- await new Promise((resolve)=>setTimeout(resolve, retryInterval));
289
+ for(let attempt = 1; attempt <= maxAttempts; attempt++){
290
+ const { signal: attemptSignal, cleanup: cleanupAttemptSignal } = buildRequestAbortSignal(effectiveTimeoutMs, options?.abortSignal);
291
+ try {
292
+ const result = await completion.create({
293
+ model: modelName,
294
+ messages: messagesWithImageDetail,
295
+ ...commonConfig,
296
+ ...reasoningEffortConfig,
297
+ ...extraBody
298
+ }, {
299
+ signal: attemptSignal
300
+ });
301
+ timeCost = Date.now() - startTime;
302
+ debugProfileStats(`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 ?? ''}`);
303
+ debugProfileDetail(`model usage detail: ${JSON.stringify(result.usage)}`);
304
+ if (!result.choices) throw new Error(`invalid response from LLM service: ${JSON.stringify(result)}`);
305
+ content = result.choices[0].message.content;
306
+ accumulatedReasoning = result.choices[0].message?.reasoning_content || '';
307
+ usage = result.usage;
308
+ requestId = result._request_id;
309
+ if (!hasUsableText(content) && hasUsableText(accumulatedReasoning)) {
310
+ warnCall('empty content from AI model, using reasoning content');
311
+ content = accumulatedReasoning;
312
+ }
313
+ if (!hasUsableText(content)) throw new AIResponseParseError('empty content from AI model', JSON.stringify(result), buildUsageInfo(usage, requestId));
314
+ break;
315
+ } catch (error) {
316
+ lastError = error;
317
+ const wasHardTimeout = isHardTimeoutError(lastError);
318
+ if (wasHardTimeout) warnCall(`AI call hit hard timeout (${effectiveTimeoutMs}ms, attempt ${attempt}/${maxAttempts}, model ${modelName}, intent ${modelConfig.intent})`);
319
+ if (options?.abortSignal?.aborted) break;
320
+ if (attempt < maxAttempts) {
321
+ warnCall(`AI call failed (attempt ${attempt}/${maxAttempts}), retrying in ${retryInterval}ms... Error: ${lastError.message}`);
322
+ await new Promise((resolve)=>setTimeout(resolve, retryInterval));
323
+ }
324
+ } finally{
325
+ cleanupAttemptSignal();
310
326
  }
311
327
  }
312
328
  if (!content) throw lastError;
@@ -1 +1 @@
1
- {"version":3,"file":"ai-model/service-caller/index.mjs","sources":["../../../../src/ai-model/service-caller/index.ts"],"sourcesContent":["import type { AIUsageInfo, DeepThinkOption } 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';\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 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 ...(typeof timeout === 'number' ? { timeout } : {}),\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 deepThink?: DeepThinkOption;\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 return callAIWithCodexAppServer(messages, modelConfig, options);\n }\n\n const {\n completion,\n modelName,\n modelDescription,\n uiTarsModelVersion,\n modelFamily,\n } = await createChatClient({\n modelConfig,\n });\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 // Merge deepThink (per-request boolean) with reasoning config (model-level)\n // deepThink takes priority as a per-request override for reasoningEnabled\n const mergedEnableReasoning = (() => {\n const normalizedDeepThink =\n options?.deepThink === 'unset' ? undefined : options?.deepThink;\n if (normalizedDeepThink === true) return true;\n if (normalizedDeepThink === false) return false;\n return modelConfig.reasoningEnabled;\n })();\n\n const {\n config: reasoningEffortConfig,\n debugMessage: reasoningEffortDebugMessage,\n warningMessage,\n } = resolveReasoningConfig({\n reasoningEnabled: mergedEnableReasoning,\n reasoningEffort: modelConfig.reasoningEffort,\n reasoningBudget: modelConfig.reasoningBudget,\n modelFamily,\n });\n if (reasoningEffortDebugMessage) {\n debugCall(reasoningEffortDebugMessage);\n }\n if (warningMessage) {\n warnCall(warningMessage);\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 stream = (await completion.create(\n {\n model: modelName,\n messages: messagesWithImageDetail,\n ...commonConfig,\n ...reasoningEffortConfig,\n ...extraBody,\n },\n {\n stream: true,\n ...(options?.abortSignal ? { signal: options.abortSignal } : {}),\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 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 try {\n const result = await completion.create(\n {\n model: modelName,\n messages: messagesWithImageDetail,\n ...commonConfig,\n ...reasoningEffortConfig,\n ...extraBody,\n } as any,\n options?.abortSignal ? { signal: options.abortSignal } : undefined,\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 // 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 }\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 deepThink?: DeepThinkOption;\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 deepThink: options?.deepThink,\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\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 warningMessage?: string;\n} {\n // No reasoning params set at all\n if (\n reasoningEnabled === undefined &&\n !reasoningEffort &&\n reasoningBudget === undefined\n ) {\n return { config: {} };\n }\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 if (reasoningEnabled !== undefined) {\n config.enable_thinking = reasoningEnabled;\n debugMessages.push(`enable_thinking=${reasoningEnabled}`);\n }\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 if (reasoningEnabled !== undefined) {\n config.thinking = {\n type: reasoningEnabled ? 'enabled' : 'disabled',\n };\n debugMessages.push(\n `thinking.type=${reasoningEnabled ? 'enabled' : 'disabled'}`,\n );\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 if (reasoningEnabled !== undefined) {\n config.thinking = {\n type: reasoningEnabled ? 'enabled' : 'disabled',\n };\n debugMessages.push(\n `thinking.type=${reasoningEnabled ? 'enabled' : 'disabled'}`,\n );\n }\n // reasoningEffort and reasoningBudget are ignored for glm-v\n } else if (modelFamily === 'gpt-5') {\n // reasoningEffort → reasoning.effort\n config.reasoning = undefined;\n debugMessages.push('reasoning config is ignored for gpt-5');\n // if (reasoningEffort) {\n // config.reasoning = { effort: reasoningEffort };\n // debugMessages.push(`reasoning.effort=\"${reasoningEffort}\"`);\n // } else if (reasoningEnabled === true) {\n // config.reasoning = { effort: 'high' };\n // debugMessages.push('reasoning.effort=\"high\" (from reasoningEnabled)');\n // } else if (reasoningEnabled === false) {\n // config.reasoning = { effort: 'low' };\n // debugMessages.push('reasoning.effort=\"low\" (from reasoningEnabled)');\n // }\n // reasoningBudget is ignored for gpt-5\n } else if (!modelFamily) {\n return {\n config: {},\n debugMessage: 'reasoning config ignored: no model_family configured',\n warningMessage:\n 'Reasoning config is set but no model_family is configured. Set MIDSCENE_MODEL_FAMILY to enable reasoning config pass-through.',\n };\n } else {\n // For unknown model families, pass reasoning_effort directly as a best-effort default\n if (reasoningEffort) {\n config.reasoning_effort = reasoningEffort;\n debugMessages.push(`reasoning_effort=\"${reasoningEffort}\"`);\n }\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","openAIOptions","baseOpenAI","OpenAI","openai","globalConfigManager","MIDSCENE_LANGSMITH_DEBUG","langsmithModule","wrapOpenAI","MIDSCENE_LANGFUSE_DEBUG","langfuseModule","observeOpenAI","wrappedClient","callAI","messages","options","isCodexAppServerProvider","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","mergedEnableReasoning","normalizedDeepThink","reasoningEffortConfig","reasoningEffortDebugMessage","warningMessage","resolveReasoningConfig","shouldUseOriginalImageDetail","shouldForceOriginalImageDetail","messagesWithImageDetail","msg","Array","part","stream","chunk","reasoning_content","chunkData","estimatedTokens","Math","finalChunk","retryCount","retryInterval","maxAttempts","lastError","attempt","result","JSON","Promise","resolve","setTimeout","e","newError","callAIWithObjectResponse","response","assert","jsonContent","safeParseJson","callAIWithStringResponse","msgs","extractJSONFromCodeBlock","jsonMatch","codeBlockMatch","jsonLikeMatch","preprocessDoubaoBboxJson","input","reasoningEnabled","reasoningEffort","reasoningBudget","debugMessages","config","normalizeJsonObject","obj","item","normalized","key","Object","trimmedKey","normalizedValue","cleanJ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;YAG1C,IAAI,AAA2B,YAA3B,OAAO+D,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,SAASf,cACdQ,KAAa,EACb7G,WAAqC;IAErC,MAAM2H,kBAAkBnB,yBAAyBK;IAEjD,IAAIc,iBAAiB,MAAM,oBACzB,OAAOA,gBACJ,KAAK,CAAC,oBACL,MAAM,GACP,IAAIzG;IAGT,IAAIR;IACJ,IAAI8E;IACJ,IAAI;QACF9E,SAASiF,KAAK,KAAK,CAACgC;QACpB,OAAOR,oBAAoBzG;IAC7B,EAAE,OAAOY,OAAO;QACdkE,YAAYlE;IACd;IACA,IAAI;QACFZ,SAASiF,KAAK,KAAK,CAACiC,WAAWD;QAC/B,OAAOR,oBAAoBzG;IAC7B,EAAE,OAAOY,OAAO;QACdkE,YAAYlE;IACd;IAEA,IACEtB,AAAgB,oBAAhBA,eACAA,AAAgB,kBAAhBA,eACA6H,SAAS7H,cACT;QACA,MAAM8H,aAAalB,yBAAyBe;QAC5C,IAAI;YACFjH,SAASiF,KAAK,KAAK,CAACiC,WAAWE;YAC/B,OAAOX,oBAAoBzG;QAC7B,EAAE,OAAOY,OAAO;YACdkE,YAAYlE;QACd;IACF;IACA,MAAMpC,MACJ,CAAC,gDAAgD,EAAE6I,OACjDvC,aAAa,iBACb,gBAAgB,EAAEqB,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, DeepThinkOption } 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 deepThink?: DeepThinkOption;\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 return callAIWithCodexAppServer(messages, modelConfig, options);\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 // Merge deepThink (per-request boolean) with reasoning config (model-level)\n // deepThink takes priority as a per-request override for reasoningEnabled\n const mergedEnableReasoning = (() => {\n const normalizedDeepThink =\n options?.deepThink === 'unset' ? undefined : options?.deepThink;\n if (normalizedDeepThink === true) return true;\n if (normalizedDeepThink === false) return false;\n return modelConfig.reasoningEnabled;\n })();\n\n const {\n config: reasoningEffortConfig,\n debugMessage: reasoningEffortDebugMessage,\n warningMessage,\n } = resolveReasoningConfig({\n reasoningEnabled: mergedEnableReasoning,\n reasoningEffort: modelConfig.reasoningEffort,\n reasoningBudget: modelConfig.reasoningBudget,\n modelFamily,\n });\n if (reasoningEffortDebugMessage) {\n debugCall(reasoningEffortDebugMessage);\n }\n if (warningMessage) {\n warnCall(warningMessage);\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 deepThink?: DeepThinkOption;\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 deepThink: options?.deepThink,\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\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 warningMessage?: string;\n} {\n // No reasoning params set at all\n if (\n reasoningEnabled === undefined &&\n !reasoningEffort &&\n reasoningBudget === undefined\n ) {\n return { config: {} };\n }\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 if (reasoningEnabled !== undefined) {\n config.enable_thinking = reasoningEnabled;\n debugMessages.push(`enable_thinking=${reasoningEnabled}`);\n }\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 if (reasoningEnabled !== undefined) {\n config.thinking = {\n type: reasoningEnabled ? 'enabled' : 'disabled',\n };\n debugMessages.push(\n `thinking.type=${reasoningEnabled ? 'enabled' : 'disabled'}`,\n );\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 if (reasoningEnabled !== undefined) {\n config.thinking = {\n type: reasoningEnabled ? 'enabled' : 'disabled',\n };\n debugMessages.push(\n `thinking.type=${reasoningEnabled ? 'enabled' : 'disabled'}`,\n );\n }\n // reasoningEffort and reasoningBudget are ignored for glm-v\n } else if (modelFamily === 'gpt-5') {\n // reasoningEffort → reasoning.effort\n config.reasoning = undefined;\n debugMessages.push('reasoning config is ignored for gpt-5');\n // if (reasoningEffort) {\n // config.reasoning = { effort: reasoningEffort };\n // debugMessages.push(`reasoning.effort=\"${reasoningEffort}\"`);\n // } else if (reasoningEnabled === true) {\n // config.reasoning = { effort: 'high' };\n // debugMessages.push('reasoning.effort=\"high\" (from reasoningEnabled)');\n // } else if (reasoningEnabled === false) {\n // config.reasoning = { effort: 'low' };\n // debugMessages.push('reasoning.effort=\"low\" (from reasoningEnabled)');\n // }\n // reasoningBudget is ignored for gpt-5\n } else if (!modelFamily) {\n return {\n config: {},\n debugMessage: 'reasoning config ignored: no model_family configured',\n warningMessage:\n 'Reasoning config is set but no model_family is configured. Set MIDSCENE_MODEL_FAMILY to enable reasoning config pass-through.',\n };\n } else {\n // For unknown model families, pass reasoning_effort directly as a best-effort default\n if (reasoningEffort) {\n config.reasoning_effort = reasoningEffort;\n debugMessages.push(`reasoning_effort=\"${reasoningEffort}\"`);\n }\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","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","mergedEnableReasoning","normalizedDeepThink","reasoningEffortConfig","reasoningEffortDebugMessage","warningMessage","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","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;YACp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AOA,gBACJ,KAAK,CAAC,oBACL,MAAM,GACP,IAAIlH;IAGT,IAAIR;IACJ,IAAImF;IACJ,IAAI;QACFnF,SAASwF,KAAK,KAAK,CAACkC;QACpB,OAAOR,oBAAoBlH;IAC7B,EAAE,OAAOY,OAAO;QACduE,YAAYvE;IACd;IACA,IAAI;QACFZ,SAASwF,KAAK,KAAK,CAACmC,WAAWD;QAC/B,OAAOR,oBAAoBlH;IAC7B,EAAE,OAAOY,OAAO;QACduE,YAAYvE;IACd;IAEA,IACEtB,AAAgB,oBAAhBA,eACAA,AAAgB,kBAAhBA,eACAsI,SAAStI,cACT;QACA,MAAMuI,aAAalB,yBAAyBe;QAC5C,IAAI;YACF1H,SAASwF,KAAK,KAAK,CAACmC,WAAWE;YAC/B,OAAOX,oBAAoBlH;QAC7B,EAAE,OAAOY,OAAO;YACduE,YAAYvE;QACd;IACF;IACA,MAAMpC,MACJ,CAAC,gDAAgD,EAAEsJ,OACjD3C,aAAa,iBACb,gBAAgB,EAAEyB,OAAO;AAE/B"}
@@ -0,0 +1,49 @@
1
+ const DEFAULT_AI_CALL_TIMEOUT_MS = 180000;
2
+ const AI_CALL_HARD_TIMEOUT_CODE = 'AI_CALL_HARD_TIMEOUT';
3
+ function resolveEffectiveTimeoutMs(modelConfig) {
4
+ const { timeout } = modelConfig;
5
+ if ('number' != typeof timeout) return DEFAULT_AI_CALL_TIMEOUT_MS;
6
+ if (timeout <= 0) return null;
7
+ return timeout;
8
+ }
9
+ function isHardTimeoutError(err) {
10
+ if (!err || 'object' != typeof err) return false;
11
+ const code = err.code;
12
+ if (code === AI_CALL_HARD_TIMEOUT_CODE) return true;
13
+ const cause = err.cause;
14
+ if (cause && 'object' == typeof cause && cause.code === AI_CALL_HARD_TIMEOUT_CODE) return true;
15
+ return false;
16
+ }
17
+ function buildRequestAbortSignal(timeoutMs, userSignal) {
18
+ const controller = new AbortController();
19
+ if (userSignal?.aborted) {
20
+ controller.abort(userSignal.reason);
21
+ return {
22
+ signal: controller.signal,
23
+ cleanup: ()=>{}
24
+ };
25
+ }
26
+ let timer;
27
+ if (null !== timeoutMs) {
28
+ timer = setTimeout(()=>{
29
+ const err = new Error(`AI call hard timeout after ${timeoutMs}ms (full request time exceeded)`);
30
+ err.code = AI_CALL_HARD_TIMEOUT_CODE;
31
+ controller.abort(err);
32
+ }, timeoutMs);
33
+ if ('function' == typeof timer.unref) timer.unref();
34
+ }
35
+ const onUserAbort = userSignal ? ()=>controller.abort(userSignal.reason) : void 0;
36
+ if (userSignal && onUserAbort) userSignal.addEventListener('abort', onUserAbort, {
37
+ once: true
38
+ });
39
+ return {
40
+ signal: controller.signal,
41
+ cleanup: ()=>{
42
+ if (timer) clearTimeout(timer);
43
+ if (userSignal && onUserAbort) userSignal.removeEventListener('abort', onUserAbort);
44
+ }
45
+ };
46
+ }
47
+ export { AI_CALL_HARD_TIMEOUT_CODE, DEFAULT_AI_CALL_TIMEOUT_MS, buildRequestAbortSignal, isHardTimeoutError, resolveEffectiveTimeoutMs };
48
+
49
+ //# sourceMappingURL=request-timeout.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ai-model/service-caller/request-timeout.mjs","sources":["../../../../src/ai-model/service-caller/request-timeout.ts"],"sourcesContent":["import type { IModelConfig } from '@midscene/shared/env';\n\n/**\n * Default hard timeout (ms) applied to every AI HTTP call.\n *\n * We need an end-to-end timeout for the whole request lifecycle, not just the\n * time until response headers arrive. Some providers can return headers\n * quickly and then stall while the body is still being read.\n *\n * Override per intent via `MIDSCENE_MODEL_TIMEOUT`,\n * `MIDSCENE_INSIGHT_MODEL_TIMEOUT`, or `MIDSCENE_PLANNING_MODEL_TIMEOUT`.\n * Set the env var (or `modelConfig.timeout`) to `0` to disable the hard\n * timeout entirely; only a caller-provided `abortSignal` will cancel the\n * request in that case.\n */\nexport const DEFAULT_AI_CALL_TIMEOUT_MS = 180_000;\n\n/** Identifying code set on the AbortError raised by our hard timeout. */\nexport const AI_CALL_HARD_TIMEOUT_CODE = 'AI_CALL_HARD_TIMEOUT';\n\n/**\n * Resolve the hard request timeout for an AI call.\n * Returns `null` when the user explicitly opted out (`timeout === 0`).\n */\nexport function resolveEffectiveTimeoutMs(\n modelConfig: Pick<IModelConfig, 'timeout'>,\n): number | null {\n const { timeout } = modelConfig;\n if (typeof timeout !== 'number') return DEFAULT_AI_CALL_TIMEOUT_MS;\n if (timeout <= 0) return null;\n return timeout;\n}\n\n/**\n * True if the error was raised by our hard-timeout AbortSignal (vs any other\n * abort/network/HTTP error). Used to drive observability without having to\n * string-match the message.\n */\nexport function isHardTimeoutError(err: unknown): boolean {\n if (!err || typeof err !== 'object') return false;\n const code = (err as { code?: unknown }).code;\n if (code === AI_CALL_HARD_TIMEOUT_CODE) return true;\n const cause = (err as { cause?: unknown }).cause;\n if (\n cause &&\n typeof cause === 'object' &&\n (cause as { code?: unknown }).code === AI_CALL_HARD_TIMEOUT_CODE\n ) {\n return true;\n }\n return false;\n}\n\n// Wires a hard timeout into the abort signal passed to fetch so the request\n// is actually cancelled even if the provider/client timeout only covers part\n// of the request. Honours any abortSignal supplied by the caller. Passing\n// `null` for `timeoutMs` disables the hard timeout and only forwards the user\n// signal.\nexport function buildRequestAbortSignal(\n timeoutMs: number | null,\n userSignal?: AbortSignal,\n): { signal: AbortSignal; cleanup: () => void } {\n const controller = new AbortController();\n\n if (userSignal?.aborted) {\n controller.abort(userSignal.reason);\n return { signal: controller.signal, cleanup: () => {} };\n }\n\n let timer: ReturnType<typeof setTimeout> | undefined;\n if (timeoutMs !== null) {\n timer = setTimeout(() => {\n const err = new Error(\n `AI call hard timeout after ${timeoutMs}ms (full request time exceeded)`,\n ) as Error & { code?: string };\n err.code = AI_CALL_HARD_TIMEOUT_CODE;\n controller.abort(err);\n }, timeoutMs);\n if (typeof (timer as { unref?: () => void }).unref === 'function') {\n (timer as { unref: () => void }).unref();\n }\n }\n\n const onUserAbort = userSignal\n ? () => controller.abort(userSignal.reason)\n : undefined;\n if (userSignal && onUserAbort) {\n userSignal.addEventListener('abort', onUserAbort, { once: true });\n }\n\n return {\n signal: controller.signal,\n cleanup: () => {\n if (timer) clearTimeout(timer);\n if (userSignal && onUserAbort) {\n userSignal.removeEventListener('abort', onUserAbort);\n }\n },\n };\n}\n"],"names":["DEFAULT_AI_CALL_TIMEOUT_MS","AI_CALL_HARD_TIMEOUT_CODE","resolveEffectiveTimeoutMs","modelConfig","timeout","isHardTimeoutError","err","code","cause","buildRequestAbortSignal","timeoutMs","userSignal","controller","AbortController","timer","setTimeout","Error","onUserAbort","undefined","clearTimeout"],"mappings":"AAeO,MAAMA,6BAA6B;AAGnC,MAAMC,4BAA4B;AAMlC,SAASC,0BACdC,WAA0C;IAE1C,MAAM,EAAEC,OAAO,EAAE,GAAGD;IACpB,IAAI,AAAmB,YAAnB,OAAOC,SAAsB,OAAOJ;IACxC,IAAII,WAAW,GAAG,OAAO;IACzB,OAAOA;AACT;AAOO,SAASC,mBAAmBC,GAAY;IAC7C,IAAI,CAACA,OAAO,AAAe,YAAf,OAAOA,KAAkB,OAAO;IAC5C,MAAMC,OAAQD,IAA2B,IAAI;IAC7C,IAAIC,SAASN,2BAA2B,OAAO;IAC/C,MAAMO,QAASF,IAA4B,KAAK;IAChD,IACEE,SACA,AAAiB,YAAjB,OAAOA,SACNA,MAA6B,IAAI,KAAKP,2BAEvC,OAAO;IAET,OAAO;AACT;AAOO,SAASQ,wBACdC,SAAwB,EACxBC,UAAwB;IAExB,MAAMC,aAAa,IAAIC;IAEvB,IAAIF,YAAY,SAAS;QACvBC,WAAW,KAAK,CAACD,WAAW,MAAM;QAClC,OAAO;YAAE,QAAQC,WAAW,MAAM;YAAE,SAAS,KAAO;QAAE;IACxD;IAEA,IAAIE;IACJ,IAAIJ,AAAc,SAAdA,WAAoB;QACtBI,QAAQC,WAAW;YACjB,MAAMT,MAAM,IAAIU,MACd,CAAC,2BAA2B,EAAEN,UAAU,+BAA+B,CAAC;YAE1EJ,IAAI,IAAI,GAAGL;YACXW,WAAW,KAAK,CAACN;QACnB,GAAGI;QACH,IAAI,AAAmD,cAAnD,OAAQI,MAAiC,KAAK,EAC/CA,MAAgC,KAAK;IAE1C;IAEA,MAAMG,cAAcN,aAChB,IAAMC,WAAW,KAAK,CAACD,WAAW,MAAM,IACxCO;IACJ,IAAIP,cAAcM,aAChBN,WAAW,gBAAgB,CAAC,SAASM,aAAa;QAAE,MAAM;IAAK;IAGjE,OAAO;QACL,QAAQL,WAAW,MAAM;QACzB,SAAS;YACP,IAAIE,OAAOK,aAAaL;YACxB,IAAIH,cAAcM,aAChBN,WAAW,mBAAmB,CAAC,SAASM;QAE5C;IACF;AACF"}
@@ -223,7 +223,12 @@ function buildYamlFlowFromPlans(plans, actionSpace) {
223
223
  }
224
224
  const flowKey = action.interfaceAlias || verb;
225
225
  const flowParam = action.paramSchema ? dumpActionParam(plan.param || {}, action.paramSchema) : {};
226
- const flowItem = {
226
+ const shortcutField = 'Launch' === action.name || 'launch' === action.interfaceAlias ? 'uri' : 'Terminate' === action.name || 'terminate' === action.interfaceAlias ? 'uri' : 'RunAdbShell' === action.name || 'runAdbShell' === action.interfaceAlias ? 'command' : void 0;
227
+ const shortcutKeys = shortcutField ? Object.keys(flowParam) : [];
228
+ const canInlineShortcut = shortcutField && 1 === shortcutKeys.length && shortcutKeys[0] === shortcutField && 'string' == typeof flowParam[shortcutField];
229
+ const flowItem = canInlineShortcut ? {
230
+ [flowKey]: flowParam[shortcutField]
231
+ } : {
227
232
  [flowKey]: '',
228
233
  ...flowParam
229
234
  };