@rpascene/core 0.30.8
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.
- package/LICENSE +21 -0
- package/README.md +9 -0
- package/dist/es/agent/agent.mjs +636 -0
- package/dist/es/agent/agent.mjs.map +1 -0
- package/dist/es/agent/common.mjs +0 -0
- package/dist/es/agent/index.mjs +6 -0
- package/dist/es/agent/task-cache.mjs +184 -0
- package/dist/es/agent/task-cache.mjs.map +1 -0
- package/dist/es/agent/tasks.mjs +666 -0
- package/dist/es/agent/tasks.mjs.map +1 -0
- package/dist/es/agent/ui-utils.mjs +72 -0
- package/dist/es/agent/ui-utils.mjs.map +1 -0
- package/dist/es/agent/utils.mjs +162 -0
- package/dist/es/agent/utils.mjs.map +1 -0
- package/dist/es/ai-model/action-executor.mjs +129 -0
- package/dist/es/ai-model/action-executor.mjs.map +1 -0
- package/dist/es/ai-model/common.mjs +355 -0
- package/dist/es/ai-model/common.mjs.map +1 -0
- package/dist/es/ai-model/conversation-history.mjs +58 -0
- package/dist/es/ai-model/conversation-history.mjs.map +1 -0
- package/dist/es/ai-model/index.mjs +11 -0
- package/dist/es/ai-model/inspect.mjs +286 -0
- package/dist/es/ai-model/inspect.mjs.map +1 -0
- package/dist/es/ai-model/llm-planning.mjs +140 -0
- package/dist/es/ai-model/llm-planning.mjs.map +1 -0
- package/dist/es/ai-model/prompt/assertion.mjs +31 -0
- package/dist/es/ai-model/prompt/assertion.mjs.map +1 -0
- package/dist/es/ai-model/prompt/common.mjs +7 -0
- package/dist/es/ai-model/prompt/common.mjs.map +1 -0
- package/dist/es/ai-model/prompt/describe.mjs +44 -0
- package/dist/es/ai-model/prompt/describe.mjs.map +1 -0
- package/dist/es/ai-model/prompt/extraction.mjs +140 -0
- package/dist/es/ai-model/prompt/extraction.mjs.map +1 -0
- package/dist/es/ai-model/prompt/llm-locator.mjs +275 -0
- package/dist/es/ai-model/prompt/llm-locator.mjs.map +1 -0
- package/dist/es/ai-model/prompt/llm-planning.mjs +367 -0
- package/dist/es/ai-model/prompt/llm-planning.mjs.map +1 -0
- package/dist/es/ai-model/prompt/llm-section-locator.mjs +47 -0
- package/dist/es/ai-model/prompt/llm-section-locator.mjs.map +1 -0
- package/dist/es/ai-model/prompt/playwright-generator.mjs +117 -0
- package/dist/es/ai-model/prompt/playwright-generator.mjs.map +1 -0
- package/dist/es/ai-model/prompt/ui-tars-locator.mjs +34 -0
- package/dist/es/ai-model/prompt/ui-tars-locator.mjs.map +1 -0
- package/dist/es/ai-model/prompt/ui-tars-planning.mjs +36 -0
- package/dist/es/ai-model/prompt/ui-tars-planning.mjs.map +1 -0
- package/dist/es/ai-model/prompt/util.mjs +124 -0
- package/dist/es/ai-model/prompt/util.mjs.map +1 -0
- package/dist/es/ai-model/prompt/yaml-generator.mjs +219 -0
- package/dist/es/ai-model/prompt/yaml-generator.mjs.map +1 -0
- package/dist/es/ai-model/service-caller/index.mjs +537 -0
- package/dist/es/ai-model/service-caller/index.mjs.map +1 -0
- package/dist/es/ai-model/ui-tars-planning.mjs +201 -0
- package/dist/es/ai-model/ui-tars-planning.mjs.map +1 -0
- package/dist/es/device/index.mjs +152 -0
- package/dist/es/device/index.mjs.map +1 -0
- package/dist/es/image/index.mjs +2 -0
- package/dist/es/index.mjs +11 -0
- package/dist/es/index.mjs.map +1 -0
- package/dist/es/insight/index.mjs +233 -0
- package/dist/es/insight/index.mjs.map +1 -0
- package/dist/es/insight/utils.mjs +15 -0
- package/dist/es/insight/utils.mjs.map +1 -0
- package/dist/es/report.mjs +88 -0
- package/dist/es/report.mjs.map +1 -0
- package/dist/es/tree.mjs +2 -0
- package/dist/es/types.mjs +11 -0
- package/dist/es/types.mjs.map +1 -0
- package/dist/es/utils.mjs +204 -0
- package/dist/es/utils.mjs.map +1 -0
- package/dist/es/yaml/builder.mjs +13 -0
- package/dist/es/yaml/builder.mjs.map +1 -0
- package/dist/es/yaml/index.mjs +3 -0
- package/dist/es/yaml/player.mjs +372 -0
- package/dist/es/yaml/player.mjs.map +1 -0
- package/dist/es/yaml/utils.mjs +73 -0
- package/dist/es/yaml/utils.mjs.map +1 -0
- package/dist/es/yaml.mjs +0 -0
- package/dist/lib/agent/agent.js +683 -0
- package/dist/lib/agent/agent.js.map +1 -0
- package/dist/lib/agent/common.js +5 -0
- package/dist/lib/agent/index.js +81 -0
- package/dist/lib/agent/index.js.map +1 -0
- package/dist/lib/agent/task-cache.js +236 -0
- package/dist/lib/agent/task-cache.js.map +1 -0
- package/dist/lib/agent/tasks.js +703 -0
- package/dist/lib/agent/tasks.js.map +1 -0
- package/dist/lib/agent/ui-utils.js +121 -0
- package/dist/lib/agent/ui-utils.js.map +1 -0
- package/dist/lib/agent/utils.js +233 -0
- package/dist/lib/agent/utils.js.map +1 -0
- package/dist/lib/ai-model/action-executor.js +163 -0
- package/dist/lib/ai-model/action-executor.js.map +1 -0
- package/dist/lib/ai-model/common.js +461 -0
- package/dist/lib/ai-model/common.js.map +1 -0
- package/dist/lib/ai-model/conversation-history.js +92 -0
- package/dist/lib/ai-model/conversation-history.js.map +1 -0
- package/dist/lib/ai-model/index.js +131 -0
- package/dist/lib/ai-model/index.js.map +1 -0
- package/dist/lib/ai-model/inspect.js +326 -0
- package/dist/lib/ai-model/inspect.js.map +1 -0
- package/dist/lib/ai-model/llm-planning.js +174 -0
- package/dist/lib/ai-model/llm-planning.js.map +1 -0
- package/dist/lib/ai-model/prompt/assertion.js +65 -0
- package/dist/lib/ai-model/prompt/assertion.js.map +1 -0
- package/dist/lib/ai-model/prompt/common.js +41 -0
- package/dist/lib/ai-model/prompt/common.js.map +1 -0
- package/dist/lib/ai-model/prompt/describe.js +78 -0
- package/dist/lib/ai-model/prompt/describe.js.map +1 -0
- package/dist/lib/ai-model/prompt/extraction.js +180 -0
- package/dist/lib/ai-model/prompt/extraction.js.map +1 -0
- package/dist/lib/ai-model/prompt/llm-locator.js +315 -0
- package/dist/lib/ai-model/prompt/llm-locator.js.map +1 -0
- package/dist/lib/ai-model/prompt/llm-planning.js +407 -0
- package/dist/lib/ai-model/prompt/llm-planning.js.map +1 -0
- package/dist/lib/ai-model/prompt/llm-section-locator.js +84 -0
- package/dist/lib/ai-model/prompt/llm-section-locator.js.map +1 -0
- package/dist/lib/ai-model/prompt/playwright-generator.js +178 -0
- package/dist/lib/ai-model/prompt/playwright-generator.js.map +1 -0
- package/dist/lib/ai-model/prompt/ui-tars-locator.js +68 -0
- package/dist/lib/ai-model/prompt/ui-tars-locator.js.map +1 -0
- package/dist/lib/ai-model/prompt/ui-tars-planning.js +73 -0
- package/dist/lib/ai-model/prompt/ui-tars-planning.js.map +1 -0
- package/dist/lib/ai-model/prompt/util.js +176 -0
- package/dist/lib/ai-model/prompt/util.js.map +1 -0
- package/dist/lib/ai-model/prompt/yaml-generator.js +280 -0
- package/dist/lib/ai-model/prompt/yaml-generator.js.map +1 -0
- package/dist/lib/ai-model/service-caller/index.js +623 -0
- package/dist/lib/ai-model/service-caller/index.js.map +1 -0
- package/dist/lib/ai-model/ui-tars-planning.js +238 -0
- package/dist/lib/ai-model/ui-tars-planning.js.map +1 -0
- package/dist/lib/device/index.js +255 -0
- package/dist/lib/device/index.js.map +1 -0
- package/dist/lib/image/index.js +56 -0
- package/dist/lib/image/index.js.map +1 -0
- package/dist/lib/index.js +103 -0
- package/dist/lib/index.js.map +1 -0
- package/dist/lib/insight/index.js +267 -0
- package/dist/lib/insight/index.js.map +1 -0
- package/dist/lib/insight/utils.js +49 -0
- package/dist/lib/insight/utils.js.map +1 -0
- package/dist/lib/report.js +122 -0
- package/dist/lib/report.js.map +1 -0
- package/dist/lib/tree.js +44 -0
- package/dist/lib/tree.js.map +1 -0
- package/dist/lib/types.js +82 -0
- package/dist/lib/types.js.map +1 -0
- package/dist/lib/utils.js +283 -0
- package/dist/lib/utils.js.map +1 -0
- package/dist/lib/yaml/builder.js +57 -0
- package/dist/lib/yaml/builder.js.map +1 -0
- package/dist/lib/yaml/index.js +80 -0
- package/dist/lib/yaml/index.js.map +1 -0
- package/dist/lib/yaml/player.js +406 -0
- package/dist/lib/yaml/player.js.map +1 -0
- package/dist/lib/yaml/utils.js +126 -0
- package/dist/lib/yaml/utils.js.map +1 -0
- package/dist/lib/yaml.js +20 -0
- package/dist/lib/yaml.js.map +1 -0
- package/dist/types/agent/agent.d.ts +156 -0
- package/dist/types/agent/common.d.ts +0 -0
- package/dist/types/agent/index.d.ts +9 -0
- package/dist/types/agent/task-cache.d.ts +48 -0
- package/dist/types/agent/tasks.d.ts +48 -0
- package/dist/types/agent/ui-utils.d.ts +7 -0
- package/dist/types/agent/utils.d.ts +52 -0
- package/dist/types/ai-model/action-executor.d.ts +19 -0
- package/dist/types/ai-model/common.d.ts +569 -0
- package/dist/types/ai-model/conversation-history.d.ts +18 -0
- package/dist/types/ai-model/index.d.ts +13 -0
- package/dist/types/ai-model/inspect.d.ts +46 -0
- package/dist/types/ai-model/llm-planning.d.ts +11 -0
- package/dist/types/ai-model/prompt/assertion.d.ts +2 -0
- package/dist/types/ai-model/prompt/common.d.ts +2 -0
- package/dist/types/ai-model/prompt/describe.d.ts +1 -0
- package/dist/types/ai-model/prompt/extraction.d.ts +4 -0
- package/dist/types/ai-model/prompt/llm-locator.d.ts +9 -0
- package/dist/types/ai-model/prompt/llm-planning.d.ts +9 -0
- package/dist/types/ai-model/prompt/llm-section-locator.d.ts +6 -0
- package/dist/types/ai-model/prompt/playwright-generator.d.ts +26 -0
- package/dist/types/ai-model/prompt/ui-tars-locator.d.ts +1 -0
- package/dist/types/ai-model/prompt/ui-tars-planning.d.ts +2 -0
- package/dist/types/ai-model/prompt/util.d.ts +47 -0
- package/dist/types/ai-model/prompt/yaml-generator.d.ts +100 -0
- package/dist/types/ai-model/service-caller/index.d.ts +48 -0
- package/dist/types/ai-model/ui-tars-planning.d.ts +59 -0
- package/dist/types/device/index.d.ts +2158 -0
- package/dist/types/image/index.d.ts +1 -0
- package/dist/types/index.d.ts +12 -0
- package/dist/types/insight/index.d.ts +31 -0
- package/dist/types/insight/utils.d.ts +2 -0
- package/dist/types/report.d.ts +12 -0
- package/dist/types/tree.d.ts +1 -0
- package/dist/types/types.d.ts +414 -0
- package/dist/types/utils.d.ts +40 -0
- package/dist/types/yaml/builder.d.ts +2 -0
- package/dist/types/yaml/index.d.ts +3 -0
- package/dist/types/yaml/player.d.ts +34 -0
- package/dist/types/yaml/utils.d.ts +9 -0
- package/dist/types/yaml.d.ts +178 -0
- package/package.json +108 -0
|
@@ -0,0 +1,623 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __webpack_modules__ = {
|
|
3
|
+
"langsmith/wrappers": function(module) {
|
|
4
|
+
module.exports = import("langsmith/wrappers").then(function(module) {
|
|
5
|
+
return module;
|
|
6
|
+
});
|
|
7
|
+
}
|
|
8
|
+
};
|
|
9
|
+
var __webpack_module_cache__ = {};
|
|
10
|
+
function __webpack_require__(moduleId) {
|
|
11
|
+
var cachedModule = __webpack_module_cache__[moduleId];
|
|
12
|
+
if (void 0 !== cachedModule) return cachedModule.exports;
|
|
13
|
+
var module = __webpack_module_cache__[moduleId] = {
|
|
14
|
+
exports: {}
|
|
15
|
+
};
|
|
16
|
+
__webpack_modules__[moduleId](module, module.exports, __webpack_require__);
|
|
17
|
+
return module.exports;
|
|
18
|
+
}
|
|
19
|
+
(()=>{
|
|
20
|
+
__webpack_require__.n = (module)=>{
|
|
21
|
+
var getter = module && module.__esModule ? ()=>module['default'] : ()=>module;
|
|
22
|
+
__webpack_require__.d(getter, {
|
|
23
|
+
a: getter
|
|
24
|
+
});
|
|
25
|
+
return getter;
|
|
26
|
+
};
|
|
27
|
+
})();
|
|
28
|
+
(()=>{
|
|
29
|
+
__webpack_require__.d = (exports1, definition)=>{
|
|
30
|
+
for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
|
|
31
|
+
enumerable: true,
|
|
32
|
+
get: definition[key]
|
|
33
|
+
});
|
|
34
|
+
};
|
|
35
|
+
})();
|
|
36
|
+
(()=>{
|
|
37
|
+
__webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
|
|
38
|
+
})();
|
|
39
|
+
(()=>{
|
|
40
|
+
__webpack_require__.r = (exports1)=>{
|
|
41
|
+
if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
|
42
|
+
value: 'Module'
|
|
43
|
+
});
|
|
44
|
+
Object.defineProperty(exports1, '__esModule', {
|
|
45
|
+
value: true
|
|
46
|
+
});
|
|
47
|
+
};
|
|
48
|
+
})();
|
|
49
|
+
var __webpack_exports__ = {};
|
|
50
|
+
(()=>{
|
|
51
|
+
__webpack_require__.r(__webpack_exports__);
|
|
52
|
+
__webpack_require__.d(__webpack_exports__, {
|
|
53
|
+
preprocessDoubaoBboxJson: ()=>preprocessDoubaoBboxJson,
|
|
54
|
+
callAIWithObjectResponse: ()=>callAIWithObjectResponse,
|
|
55
|
+
getResponseFormat: ()=>getResponseFormat,
|
|
56
|
+
safeParseJson: ()=>safeParseJson,
|
|
57
|
+
callAI: ()=>callAI,
|
|
58
|
+
extractJSONFromCodeBlock: ()=>extractJSONFromCodeBlock,
|
|
59
|
+
callAIWithStringResponse: ()=>callAIWithStringResponse,
|
|
60
|
+
defaultYhtConfig: ()=>defaultYhtConfig,
|
|
61
|
+
callAIOld: ()=>callAIOld
|
|
62
|
+
});
|
|
63
|
+
const external_types_js_namespaceObject = require("../../types.js");
|
|
64
|
+
const sdk_namespaceObject = require("@anthropic-ai/sdk");
|
|
65
|
+
const identity_namespaceObject = require("@azure/identity");
|
|
66
|
+
const env_namespaceObject = require("@rpascene/shared/env");
|
|
67
|
+
const img_namespaceObject = require("@rpascene/shared/img");
|
|
68
|
+
const logger_namespaceObject = require("@rpascene/shared/logger");
|
|
69
|
+
const utils_namespaceObject = require("@rpascene/shared/utils");
|
|
70
|
+
const external_https_proxy_agent_namespaceObject = require("https-proxy-agent");
|
|
71
|
+
const external_jsonrepair_namespaceObject = require("jsonrepair");
|
|
72
|
+
const external_openai_namespaceObject = require("openai");
|
|
73
|
+
var external_openai_default = /*#__PURE__*/ __webpack_require__.n(external_openai_namespaceObject);
|
|
74
|
+
const external_socks_proxy_agent_namespaceObject = require("socks-proxy-agent");
|
|
75
|
+
const external_common_js_namespaceObject = require("../common.js");
|
|
76
|
+
const assertion_js_namespaceObject = require("../prompt/assertion.js");
|
|
77
|
+
const llm_locator_js_namespaceObject = require("../prompt/llm-locator.js");
|
|
78
|
+
const llm_planning_js_namespaceObject = require("../prompt/llm-planning.js");
|
|
79
|
+
const defaultYhtConfig = {
|
|
80
|
+
domain: 'APILink',
|
|
81
|
+
yht_access_token: '',
|
|
82
|
+
chatType: 14,
|
|
83
|
+
model: 'doubao-seed-1-6-vision-250815',
|
|
84
|
+
modelCategory: 2,
|
|
85
|
+
stream: 0,
|
|
86
|
+
temperature: 0.95,
|
|
87
|
+
top_p: 0.7,
|
|
88
|
+
baseURL: 'https://c2.yonyoucloud.com/iuap-aip-service/report/rest/api/aiService/chat'
|
|
89
|
+
};
|
|
90
|
+
async function createChatClient({ AIActionTypeValue, modelConfig }) {
|
|
91
|
+
const { socksProxy, httpProxy, modelName, openaiBaseURL, openaiApiKey, openaiExtraConfig, openaiUseAzureDeprecated, useAzureOpenai, azureOpenaiScope, azureOpenaiKey, azureOpenaiEndpoint, azureOpenaiApiVersion, azureOpenaiDeployment, azureExtraConfig, useAnthropicSdk, anthropicApiKey, modelDescription, uiTarsModelVersion: uiTarsVersion, vlMode } = modelConfig;
|
|
92
|
+
let openai;
|
|
93
|
+
let proxyAgent;
|
|
94
|
+
const debugProxy = (0, logger_namespaceObject.getDebug)('ai:call:proxy');
|
|
95
|
+
if (httpProxy) {
|
|
96
|
+
debugProxy('using http proxy', httpProxy);
|
|
97
|
+
proxyAgent = new external_https_proxy_agent_namespaceObject.HttpsProxyAgent(httpProxy);
|
|
98
|
+
} else if (socksProxy) {
|
|
99
|
+
debugProxy('using socks proxy', socksProxy);
|
|
100
|
+
proxyAgent = new external_socks_proxy_agent_namespaceObject.SocksProxyAgent(socksProxy);
|
|
101
|
+
}
|
|
102
|
+
if (openaiUseAzureDeprecated) openai = new external_openai_namespaceObject.AzureOpenAI({
|
|
103
|
+
baseURL: openaiBaseURL,
|
|
104
|
+
apiKey: openaiApiKey,
|
|
105
|
+
httpAgent: proxyAgent,
|
|
106
|
+
...openaiExtraConfig,
|
|
107
|
+
dangerouslyAllowBrowser: true
|
|
108
|
+
});
|
|
109
|
+
else if (useAzureOpenai) {
|
|
110
|
+
let tokenProvider;
|
|
111
|
+
if (azureOpenaiScope) {
|
|
112
|
+
(0, utils_namespaceObject.assert)(!utils_namespaceObject.ifInBrowser, 'Azure OpenAI is not supported in browser with Rpascene.');
|
|
113
|
+
const credential = new identity_namespaceObject.DefaultAzureCredential();
|
|
114
|
+
tokenProvider = (0, identity_namespaceObject.getBearerTokenProvider)(credential, azureOpenaiScope);
|
|
115
|
+
openai = new external_openai_namespaceObject.AzureOpenAI({
|
|
116
|
+
azureADTokenProvider: tokenProvider,
|
|
117
|
+
endpoint: azureOpenaiEndpoint,
|
|
118
|
+
apiVersion: azureOpenaiApiVersion,
|
|
119
|
+
deployment: azureOpenaiDeployment,
|
|
120
|
+
...openaiExtraConfig,
|
|
121
|
+
...azureExtraConfig
|
|
122
|
+
});
|
|
123
|
+
} else openai = new external_openai_namespaceObject.AzureOpenAI({
|
|
124
|
+
apiKey: azureOpenaiKey,
|
|
125
|
+
endpoint: azureOpenaiEndpoint,
|
|
126
|
+
apiVersion: azureOpenaiApiVersion,
|
|
127
|
+
deployment: azureOpenaiDeployment,
|
|
128
|
+
dangerouslyAllowBrowser: true,
|
|
129
|
+
...openaiExtraConfig,
|
|
130
|
+
...azureExtraConfig
|
|
131
|
+
});
|
|
132
|
+
} else if (!useAnthropicSdk) openai = new (external_openai_default())({
|
|
133
|
+
baseURL: openaiBaseURL,
|
|
134
|
+
apiKey: openaiApiKey,
|
|
135
|
+
httpAgent: proxyAgent,
|
|
136
|
+
...openaiExtraConfig,
|
|
137
|
+
defaultHeaders: {
|
|
138
|
+
...(null == openaiExtraConfig ? void 0 : openaiExtraConfig.defaultHeaders) || {},
|
|
139
|
+
[env_namespaceObject.RPASCENE_API_TYPE]: AIActionTypeValue.toString()
|
|
140
|
+
},
|
|
141
|
+
dangerouslyAllowBrowser: true
|
|
142
|
+
});
|
|
143
|
+
if (openai && env_namespaceObject.globalConfigManager.getEnvConfigInBoolean(env_namespaceObject.RPASCENE_LANGSMITH_DEBUG)) {
|
|
144
|
+
if (utils_namespaceObject.ifInBrowser) throw new Error('langsmith is not supported in browser');
|
|
145
|
+
console.log('DEBUGGING MODE: langsmith wrapper enabled');
|
|
146
|
+
const { wrapOpenAI } = await Promise.resolve().then(__webpack_require__.bind(__webpack_require__, "langsmith/wrappers"));
|
|
147
|
+
openai = wrapOpenAI(openai);
|
|
148
|
+
}
|
|
149
|
+
if (void 0 !== openai) return {
|
|
150
|
+
completion: openai.chat.completions,
|
|
151
|
+
style: 'openai',
|
|
152
|
+
modelName,
|
|
153
|
+
modelDescription,
|
|
154
|
+
uiTarsVersion,
|
|
155
|
+
vlMode
|
|
156
|
+
};
|
|
157
|
+
if (useAnthropicSdk) openai = new sdk_namespaceObject.Anthropic({
|
|
158
|
+
apiKey: anthropicApiKey,
|
|
159
|
+
httpAgent: proxyAgent,
|
|
160
|
+
dangerouslyAllowBrowser: true
|
|
161
|
+
});
|
|
162
|
+
if (void 0 !== openai && openai.messages) return {
|
|
163
|
+
completion: openai.messages,
|
|
164
|
+
style: 'anthropic',
|
|
165
|
+
modelName,
|
|
166
|
+
modelDescription,
|
|
167
|
+
uiTarsVersion,
|
|
168
|
+
vlMode
|
|
169
|
+
};
|
|
170
|
+
throw new Error('Openai SDK or Anthropic SDK is not initialized');
|
|
171
|
+
}
|
|
172
|
+
async function callAIOld(messages, AIActionTypeValue, modelConfig, options) {
|
|
173
|
+
const { completion, style, modelName, modelDescription, uiTarsVersion, vlMode } = await createChatClient({
|
|
174
|
+
AIActionTypeValue,
|
|
175
|
+
modelConfig
|
|
176
|
+
});
|
|
177
|
+
const responseFormat = getResponseFormat(modelName, AIActionTypeValue);
|
|
178
|
+
const maxTokens = env_namespaceObject.globalConfigManager.getEnvConfigValue(env_namespaceObject.OPENAI_MAX_TOKENS);
|
|
179
|
+
const debugCall = (0, logger_namespaceObject.getDebug)('ai:call');
|
|
180
|
+
const debugProfileStats = (0, logger_namespaceObject.getDebug)('ai:profile:stats');
|
|
181
|
+
const debugProfileDetail = (0, logger_namespaceObject.getDebug)('ai:profile:detail');
|
|
182
|
+
const startTime = Date.now();
|
|
183
|
+
const isStreaming = (null == options ? void 0 : options.stream) && (null == options ? void 0 : options.onChunk);
|
|
184
|
+
let content;
|
|
185
|
+
let accumulated = '';
|
|
186
|
+
let usage;
|
|
187
|
+
let timeCost;
|
|
188
|
+
const commonConfig = {
|
|
189
|
+
temperature: 'vlm-ui-tars' === vlMode ? 0.0 : 0.1,
|
|
190
|
+
stream: !!isStreaming,
|
|
191
|
+
max_tokens: 'number' == typeof maxTokens ? maxTokens : Number.parseInt(maxTokens || '2048', 10),
|
|
192
|
+
...'qwen-vl' === vlMode || 'qwen3-vl' === vlMode ? {
|
|
193
|
+
vl_high_resolution_images: true
|
|
194
|
+
} : {}
|
|
195
|
+
};
|
|
196
|
+
try {
|
|
197
|
+
if ('openai' === style) {
|
|
198
|
+
debugCall(`sending ${isStreaming ? 'streaming ' : ''}request to ${modelName}`);
|
|
199
|
+
if (isStreaming) {
|
|
200
|
+
const stream = await completion.create({
|
|
201
|
+
model: modelName,
|
|
202
|
+
messages,
|
|
203
|
+
response_format: responseFormat,
|
|
204
|
+
...commonConfig
|
|
205
|
+
}, {
|
|
206
|
+
stream: true
|
|
207
|
+
});
|
|
208
|
+
for await (const chunk of stream){
|
|
209
|
+
var _chunk_choices__delta, _chunk_choices_, _chunk_choices, _chunk_choices__delta1, _chunk_choices_1, _chunk_choices1, _chunk_choices_2, _chunk_choices2;
|
|
210
|
+
const content = (null == (_chunk_choices = chunk.choices) ? void 0 : null == (_chunk_choices_ = _chunk_choices[0]) ? void 0 : null == (_chunk_choices__delta = _chunk_choices_.delta) ? void 0 : _chunk_choices__delta.content) || '';
|
|
211
|
+
const reasoning_content = (null == (_chunk_choices1 = chunk.choices) ? void 0 : null == (_chunk_choices_1 = _chunk_choices1[0]) ? void 0 : null == (_chunk_choices__delta1 = _chunk_choices_1.delta) ? void 0 : _chunk_choices__delta1.reasoning_content) || '';
|
|
212
|
+
if (chunk.usage) usage = chunk.usage;
|
|
213
|
+
if (content || reasoning_content) {
|
|
214
|
+
accumulated += content;
|
|
215
|
+
const chunkData = {
|
|
216
|
+
content,
|
|
217
|
+
reasoning_content,
|
|
218
|
+
accumulated,
|
|
219
|
+
isComplete: false,
|
|
220
|
+
usage: void 0
|
|
221
|
+
};
|
|
222
|
+
options.onChunk(chunkData);
|
|
223
|
+
}
|
|
224
|
+
if (null == (_chunk_choices2 = chunk.choices) ? void 0 : null == (_chunk_choices_2 = _chunk_choices2[0]) ? void 0 : _chunk_choices_2.finish_reason) {
|
|
225
|
+
timeCost = Date.now() - startTime;
|
|
226
|
+
if (!usage) {
|
|
227
|
+
const estimatedTokens = Math.max(1, Math.floor(accumulated.length / 4));
|
|
228
|
+
usage = {
|
|
229
|
+
prompt_tokens: estimatedTokens,
|
|
230
|
+
completion_tokens: estimatedTokens,
|
|
231
|
+
total_tokens: 2 * estimatedTokens
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
const finalChunk = {
|
|
235
|
+
content: '',
|
|
236
|
+
accumulated,
|
|
237
|
+
reasoning_content: '',
|
|
238
|
+
isComplete: true,
|
|
239
|
+
usage: {
|
|
240
|
+
prompt_tokens: usage.prompt_tokens ?? 0,
|
|
241
|
+
completion_tokens: usage.completion_tokens ?? 0,
|
|
242
|
+
total_tokens: usage.total_tokens ?? 0,
|
|
243
|
+
time_cost: timeCost ?? 0,
|
|
244
|
+
model_name: modelName,
|
|
245
|
+
model_description: modelDescription,
|
|
246
|
+
intent: modelConfig.intent
|
|
247
|
+
}
|
|
248
|
+
};
|
|
249
|
+
options.onChunk(finalChunk);
|
|
250
|
+
break;
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
content = accumulated;
|
|
254
|
+
debugProfileStats(`streaming model, ${modelName}, mode, ${vlMode || 'default'}, cost-ms, ${timeCost}`);
|
|
255
|
+
} else {
|
|
256
|
+
var _result_usage, _result_usage1, _result_usage2;
|
|
257
|
+
const result = await completion.create({
|
|
258
|
+
model: modelName,
|
|
259
|
+
messages,
|
|
260
|
+
response_format: responseFormat,
|
|
261
|
+
...commonConfig
|
|
262
|
+
});
|
|
263
|
+
timeCost = Date.now() - startTime;
|
|
264
|
+
debugProfileStats(`model, ${modelName}, mode, ${vlMode || 'default'}, ui-tars-version, ${uiTarsVersion}, prompt-tokens, ${(null == (_result_usage = result.usage) ? void 0 : _result_usage.prompt_tokens) || ''}, completion-tokens, ${(null == (_result_usage1 = result.usage) ? void 0 : _result_usage1.completion_tokens) || ''}, total-tokens, ${(null == (_result_usage2 = result.usage) ? void 0 : _result_usage2.total_tokens) || ''}, cost-ms, ${timeCost}, requestId, ${result._request_id || ''}`);
|
|
265
|
+
debugProfileDetail(`model usage detail: ${JSON.stringify(result.usage)}`);
|
|
266
|
+
(0, utils_namespaceObject.assert)(result.choices, `invalid response from LLM service: ${JSON.stringify(result)}`);
|
|
267
|
+
content = result.choices[0].message.content;
|
|
268
|
+
usage = result.usage;
|
|
269
|
+
}
|
|
270
|
+
debugCall(`response: ${content}`);
|
|
271
|
+
(0, utils_namespaceObject.assert)(content, 'empty content');
|
|
272
|
+
} else if ('anthropic' === style) {
|
|
273
|
+
const convertImageContent = (content)=>{
|
|
274
|
+
if ('image_url' === content.type) {
|
|
275
|
+
const imgBase64 = content.image_url.url;
|
|
276
|
+
(0, utils_namespaceObject.assert)(imgBase64, 'image_url is required');
|
|
277
|
+
const { mimeType, body } = (0, img_namespaceObject.parseBase64)(content.image_url.url);
|
|
278
|
+
return {
|
|
279
|
+
source: {
|
|
280
|
+
type: 'base64',
|
|
281
|
+
media_type: mimeType,
|
|
282
|
+
data: body
|
|
283
|
+
},
|
|
284
|
+
type: 'image'
|
|
285
|
+
};
|
|
286
|
+
}
|
|
287
|
+
return content;
|
|
288
|
+
};
|
|
289
|
+
if (isStreaming) {
|
|
290
|
+
const stream = await completion.create({
|
|
291
|
+
model: modelName,
|
|
292
|
+
system: 'You are a versatile professional in software UI automation',
|
|
293
|
+
messages: messages.map((m)=>({
|
|
294
|
+
role: 'user',
|
|
295
|
+
content: Array.isArray(m.content) ? m.content.map(convertImageContent) : m.content
|
|
296
|
+
})),
|
|
297
|
+
response_format: responseFormat,
|
|
298
|
+
...commonConfig
|
|
299
|
+
});
|
|
300
|
+
for await (const chunk of stream){
|
|
301
|
+
var _chunk_delta;
|
|
302
|
+
const content = (null == (_chunk_delta = chunk.delta) ? void 0 : _chunk_delta.text) || '';
|
|
303
|
+
if (content) {
|
|
304
|
+
accumulated += content;
|
|
305
|
+
const chunkData = {
|
|
306
|
+
content,
|
|
307
|
+
accumulated,
|
|
308
|
+
reasoning_content: '',
|
|
309
|
+
isComplete: false,
|
|
310
|
+
usage: void 0
|
|
311
|
+
};
|
|
312
|
+
options.onChunk(chunkData);
|
|
313
|
+
}
|
|
314
|
+
if ('message_stop' === chunk.type) {
|
|
315
|
+
timeCost = Date.now() - startTime;
|
|
316
|
+
const anthropicUsage = chunk.usage;
|
|
317
|
+
const finalChunk = {
|
|
318
|
+
content: '',
|
|
319
|
+
accumulated,
|
|
320
|
+
reasoning_content: '',
|
|
321
|
+
isComplete: true,
|
|
322
|
+
usage: anthropicUsage ? {
|
|
323
|
+
prompt_tokens: anthropicUsage.input_tokens ?? 0,
|
|
324
|
+
completion_tokens: anthropicUsage.output_tokens ?? 0,
|
|
325
|
+
total_tokens: (anthropicUsage.input_tokens ?? 0) + (anthropicUsage.output_tokens ?? 0),
|
|
326
|
+
time_cost: timeCost ?? 0,
|
|
327
|
+
model_name: modelName,
|
|
328
|
+
model_description: modelDescription,
|
|
329
|
+
intent: modelConfig.intent
|
|
330
|
+
} : void 0
|
|
331
|
+
};
|
|
332
|
+
options.onChunk(finalChunk);
|
|
333
|
+
break;
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
content = accumulated;
|
|
337
|
+
} else {
|
|
338
|
+
const result = await completion.create({
|
|
339
|
+
model: modelName,
|
|
340
|
+
system: 'You are a versatile professional in software UI automation',
|
|
341
|
+
messages: messages.map((m)=>({
|
|
342
|
+
role: 'user',
|
|
343
|
+
content: Array.isArray(m.content) ? m.content.map(convertImageContent) : m.content
|
|
344
|
+
})),
|
|
345
|
+
response_format: responseFormat,
|
|
346
|
+
...commonConfig
|
|
347
|
+
});
|
|
348
|
+
timeCost = Date.now() - startTime;
|
|
349
|
+
content = result.content[0].text;
|
|
350
|
+
usage = result.usage;
|
|
351
|
+
}
|
|
352
|
+
(0, utils_namespaceObject.assert)(content, 'empty content');
|
|
353
|
+
}
|
|
354
|
+
if (isStreaming && !usage) {
|
|
355
|
+
const estimatedTokens = Math.max(1, Math.floor((content || '').length / 4));
|
|
356
|
+
usage = {
|
|
357
|
+
prompt_tokens: estimatedTokens,
|
|
358
|
+
completion_tokens: estimatedTokens,
|
|
359
|
+
total_tokens: 2 * estimatedTokens
|
|
360
|
+
};
|
|
361
|
+
}
|
|
362
|
+
return {
|
|
363
|
+
content: content || '',
|
|
364
|
+
usage: usage ? {
|
|
365
|
+
prompt_tokens: usage.prompt_tokens ?? 0,
|
|
366
|
+
completion_tokens: usage.completion_tokens ?? 0,
|
|
367
|
+
total_tokens: usage.total_tokens ?? 0,
|
|
368
|
+
time_cost: timeCost ?? 0,
|
|
369
|
+
model_name: modelName,
|
|
370
|
+
model_description: modelDescription,
|
|
371
|
+
intent: modelConfig.intent
|
|
372
|
+
} : void 0,
|
|
373
|
+
isStreamed: !!isStreaming
|
|
374
|
+
};
|
|
375
|
+
} catch (e) {
|
|
376
|
+
console.error(' call AI error', e);
|
|
377
|
+
const newError = new Error(`failed to call ${isStreaming ? 'streaming ' : ''}AI model service: ${e.message}. Trouble shooting: https://rpascenejs.com/model-provider.html`, {
|
|
378
|
+
cause: e
|
|
379
|
+
});
|
|
380
|
+
throw newError;
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
const getResponseFormat = (modelName, AIActionTypeValue)=>{
|
|
384
|
+
let responseFormat;
|
|
385
|
+
if (modelName.includes('gpt-4')) switch(AIActionTypeValue){
|
|
386
|
+
case external_common_js_namespaceObject.AIActionType.ASSERT:
|
|
387
|
+
responseFormat = assertion_js_namespaceObject.assertSchema;
|
|
388
|
+
break;
|
|
389
|
+
case external_common_js_namespaceObject.AIActionType.INSPECT_ELEMENT:
|
|
390
|
+
responseFormat = llm_locator_js_namespaceObject.locatorSchema;
|
|
391
|
+
break;
|
|
392
|
+
case external_common_js_namespaceObject.AIActionType.PLAN:
|
|
393
|
+
responseFormat = llm_planning_js_namespaceObject.planSchema;
|
|
394
|
+
break;
|
|
395
|
+
case external_common_js_namespaceObject.AIActionType.EXTRACT_DATA:
|
|
396
|
+
case external_common_js_namespaceObject.AIActionType.DESCRIBE_ELEMENT:
|
|
397
|
+
responseFormat = {
|
|
398
|
+
type: external_types_js_namespaceObject.AIResponseFormat.JSON
|
|
399
|
+
};
|
|
400
|
+
break;
|
|
401
|
+
case external_common_js_namespaceObject.AIActionType.TEXT:
|
|
402
|
+
responseFormat = void 0;
|
|
403
|
+
break;
|
|
404
|
+
}
|
|
405
|
+
if ('gpt-4o-2024-05-13' === modelName && AIActionTypeValue !== external_common_js_namespaceObject.AIActionType.TEXT) responseFormat = {
|
|
406
|
+
type: external_types_js_namespaceObject.AIResponseFormat.JSON
|
|
407
|
+
};
|
|
408
|
+
return responseFormat;
|
|
409
|
+
};
|
|
410
|
+
async function callAIWithObjectResponse(messages, AIActionTypeValue, modelConfig) {
|
|
411
|
+
const response = await callAI(messages, AIActionTypeValue, modelConfig);
|
|
412
|
+
(0, utils_namespaceObject.assert)(response, 'empty response');
|
|
413
|
+
const vlMode = modelConfig.vlMode;
|
|
414
|
+
const jsonContent = safeParseJson(response.content, vlMode);
|
|
415
|
+
return {
|
|
416
|
+
content: jsonContent,
|
|
417
|
+
usage: response.usage
|
|
418
|
+
};
|
|
419
|
+
}
|
|
420
|
+
async function callAIWithStringResponse(msgs, AIActionTypeValue, modelConfig) {
|
|
421
|
+
const { content, usage } = await callAI(msgs, AIActionTypeValue, modelConfig);
|
|
422
|
+
return {
|
|
423
|
+
content,
|
|
424
|
+
usage
|
|
425
|
+
};
|
|
426
|
+
}
|
|
427
|
+
function extractJSONFromCodeBlock(response) {
|
|
428
|
+
try {
|
|
429
|
+
const jsonMatch = response.match(/^\s*(\{[\s\S]*\})\s*$/);
|
|
430
|
+
if (jsonMatch) return jsonMatch[1];
|
|
431
|
+
const codeBlockMatch = response.match(/```(?:json)?\s*(\{[\s\S]*?\})\s*```/);
|
|
432
|
+
if (codeBlockMatch) return codeBlockMatch[1];
|
|
433
|
+
const jsonLikeMatch = response.match(/\{[\s\S]*\}/);
|
|
434
|
+
if (jsonLikeMatch) return jsonLikeMatch[0];
|
|
435
|
+
} catch {}
|
|
436
|
+
return response;
|
|
437
|
+
}
|
|
438
|
+
function preprocessDoubaoBboxJson(input) {
|
|
439
|
+
if (input.includes('bbox')) while(/\d+\s+\d+/.test(input))input = input.replace(/(\d+)\s+(\d+)/g, '$1,$2');
|
|
440
|
+
return input;
|
|
441
|
+
}
|
|
442
|
+
function safeParseJson(input, vlMode) {
|
|
443
|
+
const cleanJsonString = extractJSONFromCodeBlock(input);
|
|
444
|
+
if (null == cleanJsonString ? void 0 : cleanJsonString.match(/\((\d+),(\d+)\)/)) {
|
|
445
|
+
var _cleanJsonString_match;
|
|
446
|
+
return null == (_cleanJsonString_match = cleanJsonString.match(/\((\d+),(\d+)\)/)) ? void 0 : _cleanJsonString_match.slice(1).map(Number);
|
|
447
|
+
}
|
|
448
|
+
try {
|
|
449
|
+
return JSON.parse(cleanJsonString);
|
|
450
|
+
} catch {}
|
|
451
|
+
try {
|
|
452
|
+
return JSON.parse((0, external_jsonrepair_namespaceObject.jsonrepair)(cleanJsonString));
|
|
453
|
+
} catch (e) {}
|
|
454
|
+
if ('doubao-vision' === vlMode || 'vlm-ui-tars' === vlMode) {
|
|
455
|
+
const jsonString = preprocessDoubaoBboxJson(cleanJsonString);
|
|
456
|
+
return JSON.parse((0, external_jsonrepair_namespaceObject.jsonrepair)(jsonString));
|
|
457
|
+
}
|
|
458
|
+
throw Error(`failed to parse json response: ${input}`);
|
|
459
|
+
}
|
|
460
|
+
async function callAI(messages, AIActionTypeValue, modelConfig, options) {
|
|
461
|
+
const allEnvConfig = env_namespaceObject.globalConfigManager.getAllEnvConfig();
|
|
462
|
+
const YhtConfig = allEnvConfig || defaultYhtConfig;
|
|
463
|
+
const debugCall = (0, logger_namespaceObject.getDebug)('ai:call:yht');
|
|
464
|
+
const debugProfileStats = (0, logger_namespaceObject.getDebug)('ai:profile:stats:yht');
|
|
465
|
+
const debugProfileDetail = (0, logger_namespaceObject.getDebug)('ai:profile:detail:yht');
|
|
466
|
+
const startTime = Date.now();
|
|
467
|
+
const config = YhtConfig;
|
|
468
|
+
const isStreaming = (null == options ? void 0 : options.stream) && (null == options ? void 0 : options.onChunk);
|
|
469
|
+
try {
|
|
470
|
+
var _result_usage, _result_result, _result_usage1, _result_usage2, _result_usage3;
|
|
471
|
+
debugCall(`\u{51C6}\u{5907}\u{8C03}\u{7528}AI\u{670D}\u{52A1}\u{FF0C}\u{6A21}\u{578B}: ${config.model}, \u{52A8}\u{4F5C}\u{7C7B}\u{578B}: ${AIActionTypeValue}`);
|
|
472
|
+
const yhtMessages = convertToYhtMessageFormat(messages);
|
|
473
|
+
debugProfileDetail("\u8F6C\u6362\u540E\u7684\u6D88\u606F\u683C\u5F0F:", JSON.stringify(yhtMessages));
|
|
474
|
+
const requestBody = {
|
|
475
|
+
domain: config.domain,
|
|
476
|
+
messages: yhtMessages,
|
|
477
|
+
chatType: config.chatType,
|
|
478
|
+
model: config.model,
|
|
479
|
+
modelCategory: config.modelCategory,
|
|
480
|
+
stream: isStreaming ? 1 : config.stream,
|
|
481
|
+
temperature: config.temperature,
|
|
482
|
+
top_p: config.top_p,
|
|
483
|
+
extraParams: config.extraParams,
|
|
484
|
+
topic: config.topic
|
|
485
|
+
};
|
|
486
|
+
debugCall("\u53D1\u9001\u8BF7\u6C42\u5230AI\u670D\u52A1");
|
|
487
|
+
const response = await fetch(config.baseURL || defaultYhtConfig.baseURL, {
|
|
488
|
+
method: 'POST',
|
|
489
|
+
headers: {
|
|
490
|
+
'Content-Type': 'application/json',
|
|
491
|
+
yht_access_token: config.yht_access_token
|
|
492
|
+
},
|
|
493
|
+
body: JSON.stringify(requestBody)
|
|
494
|
+
});
|
|
495
|
+
if (!response.ok) {
|
|
496
|
+
const errorText = await response.text();
|
|
497
|
+
debugCall(`\u{7528}\u{53CB}AI\u{670D}\u{52A1}\u{8FD4}\u{56DE}\u{9519}\u{8BEF}: ${response.status} ${errorText}`);
|
|
498
|
+
throw new Error(`\u{7528}\u{53CB}AI\u{670D}\u{52A1}\u{8FD4}\u{56DE}\u{9519}\u{8BEF}: ${response.status} ${errorText}`);
|
|
499
|
+
}
|
|
500
|
+
const responseText = await response.text();
|
|
501
|
+
debugProfileDetail("\u7528\u53CBAI\u670D\u52A1\u539F\u59CB\u54CD\u5E94:", responseText);
|
|
502
|
+
let result;
|
|
503
|
+
try {
|
|
504
|
+
result = JSON.parse(responseText);
|
|
505
|
+
} catch (parseError) {
|
|
506
|
+
debugCall("JSON\u89E3\u6790\u5931\u8D25\uFF0C\u5C1D\u8BD5\u68C0\u67E5\u662F\u5426\u4E3AXML\u683C\u5F0F");
|
|
507
|
+
const processedContent = extractContentFromResponse(responseText);
|
|
508
|
+
result = {
|
|
509
|
+
result: {
|
|
510
|
+
content: processedContent
|
|
511
|
+
},
|
|
512
|
+
usage: void 0
|
|
513
|
+
};
|
|
514
|
+
}
|
|
515
|
+
const timeCost = Date.now() - startTime;
|
|
516
|
+
debugProfileStats(`\u{7528}\u{53CB}\u{6A21}\u{578B}\u{8C03}\u{7528}\u{5B8C}\u{6210}\u{FF0C}\u{8017}\u{65F6}: ${timeCost}ms, token\u{4F7F}\u{7528}: ${(null == (_result_usage = result.usage) ? void 0 : _result_usage.total_tokens) || 'unknown'}`);
|
|
517
|
+
debugProfileDetail(`\u{7528}\u{53CB}\u{6A21}\u{578B}\u{4F7F}\u{7528}\u{8BE6}\u{60C5}: ${JSON.stringify(result.usage)}`);
|
|
518
|
+
let content = (null == (_result_result = result.result) ? void 0 : _result_result.content) || '';
|
|
519
|
+
content = extractContentFromResponse(content);
|
|
520
|
+
debugCall(`\u{7528}\u{53CB}AI\u{670D}\u{52A1}\u{54CD}\u{5E94}\u{5185}\u{5BB9}: ${content}`);
|
|
521
|
+
(0, utils_namespaceObject.assert)(content, "\u7528\u53CBAI\u670D\u52A1\u8FD4\u56DE\u7A7A\u5185\u5BB9");
|
|
522
|
+
const usage = {
|
|
523
|
+
prompt_tokens: (null == (_result_usage1 = result.usage) ? void 0 : _result_usage1.prompt_tokens) || 0,
|
|
524
|
+
completion_tokens: (null == (_result_usage2 = result.usage) ? void 0 : _result_usage2.completion_tokens) || 0,
|
|
525
|
+
total_tokens: (null == (_result_usage3 = result.usage) ? void 0 : _result_usage3.total_tokens) || 0,
|
|
526
|
+
time_cost: timeCost,
|
|
527
|
+
model_name: config.model || '',
|
|
528
|
+
model_description: "\u7528\u53CBAI\u670D\u52A1",
|
|
529
|
+
intent: modelConfig.intent
|
|
530
|
+
};
|
|
531
|
+
return {
|
|
532
|
+
content,
|
|
533
|
+
usage,
|
|
534
|
+
isStreamed: false
|
|
535
|
+
};
|
|
536
|
+
} catch (e) {
|
|
537
|
+
const errorMsg = e.message || "\u672A\u77E5\u9519\u8BEF";
|
|
538
|
+
debugCall(`\u{7528}\u{53CB}AI\u{8C03}\u{7528}\u{9519}\u{8BEF}: ${errorMsg}`);
|
|
539
|
+
console.error("\u7528\u53CBAI\u8C03\u7528\u9519\u8BEF:", e);
|
|
540
|
+
let enhancedErrorMsg = `\u{7528}\u{53CB}AI\u{670D}\u{52A1}\u{8C03}\u{7528}\u{5931}\u{8D25}: ${errorMsg}`;
|
|
541
|
+
if (errorMsg.includes('fetch')) enhancedErrorMsg += "\u3002\u8BF7\u68C0\u67E5\u7F51\u7EDC\u8FDE\u63A5\u6216API\u5730\u5740\u662F\u5426\u6B63\u786E\u3002";
|
|
542
|
+
else if (errorMsg.includes('token')) enhancedErrorMsg += "\u3002\u8BF7\u68C0\u67E5\u7528\u53CB\u8BBF\u95EE\u4EE4\u724C\u662F\u5426\u6709\u6548\u3002";
|
|
543
|
+
const newError = new Error(enhancedErrorMsg, {
|
|
544
|
+
cause: e
|
|
545
|
+
});
|
|
546
|
+
throw newError;
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
function extractContentFromResponse(response) {
|
|
550
|
+
try {
|
|
551
|
+
if (response.includes('<json>') && response.includes('</json>')) {
|
|
552
|
+
const xmlMatch = response.match(/<json>([\s\S]*?)<\/json>/);
|
|
553
|
+
if (xmlMatch && xmlMatch[1]) return xmlMatch[1].trim();
|
|
554
|
+
}
|
|
555
|
+
return extractJSONFromCodeBlock(response);
|
|
556
|
+
} catch (error) {
|
|
557
|
+
console.warn("\u4ECE\u54CD\u5E94\u4E2D\u63D0\u53D6\u5185\u5BB9\u65F6\u51FA\u9519:", error);
|
|
558
|
+
return response;
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
function convertToYhtMessageFormat(messages) {
|
|
562
|
+
return messages.map((msg)=>{
|
|
563
|
+
const yhtMessage = {
|
|
564
|
+
role: 'system' === msg.role ? 'system' : 'user'
|
|
565
|
+
};
|
|
566
|
+
if ('string' == typeof msg.content) yhtMessage.vlContent = [
|
|
567
|
+
{
|
|
568
|
+
type: 'text',
|
|
569
|
+
text: msg.content
|
|
570
|
+
}
|
|
571
|
+
];
|
|
572
|
+
else if (Array.isArray(msg.content)) {
|
|
573
|
+
const textParts = msg.content.filter((part)=>'text' === part.type);
|
|
574
|
+
const imageParts = msg.content.filter((part)=>'image_url' === part.type);
|
|
575
|
+
if (imageParts.length > 0) yhtMessage.vlContent = [
|
|
576
|
+
{
|
|
577
|
+
type: 'text',
|
|
578
|
+
text: textParts.map((part)=>part.text).join(' ') || "\u8BF7\u5206\u6790\u56FE\u7247\u5185\u5BB9"
|
|
579
|
+
},
|
|
580
|
+
...imageParts.map((part)=>({
|
|
581
|
+
type: 'image_url',
|
|
582
|
+
image_url: {
|
|
583
|
+
url: part.image_url.url,
|
|
584
|
+
detail: 'high'
|
|
585
|
+
}
|
|
586
|
+
}))
|
|
587
|
+
];
|
|
588
|
+
else if (textParts.length > 0) yhtMessage.vlContent = [
|
|
589
|
+
{
|
|
590
|
+
type: 'text',
|
|
591
|
+
text: textParts.map((part)=>part.text).join(' ')
|
|
592
|
+
}
|
|
593
|
+
];
|
|
594
|
+
}
|
|
595
|
+
return yhtMessage;
|
|
596
|
+
});
|
|
597
|
+
}
|
|
598
|
+
})();
|
|
599
|
+
exports.callAI = __webpack_exports__.callAI;
|
|
600
|
+
exports.callAIOld = __webpack_exports__.callAIOld;
|
|
601
|
+
exports.callAIWithObjectResponse = __webpack_exports__.callAIWithObjectResponse;
|
|
602
|
+
exports.callAIWithStringResponse = __webpack_exports__.callAIWithStringResponse;
|
|
603
|
+
exports.defaultYhtConfig = __webpack_exports__.defaultYhtConfig;
|
|
604
|
+
exports.extractJSONFromCodeBlock = __webpack_exports__.extractJSONFromCodeBlock;
|
|
605
|
+
exports.getResponseFormat = __webpack_exports__.getResponseFormat;
|
|
606
|
+
exports.preprocessDoubaoBboxJson = __webpack_exports__.preprocessDoubaoBboxJson;
|
|
607
|
+
exports.safeParseJson = __webpack_exports__.safeParseJson;
|
|
608
|
+
for(var __webpack_i__ in __webpack_exports__)if (-1 === [
|
|
609
|
+
"callAI",
|
|
610
|
+
"callAIOld",
|
|
611
|
+
"callAIWithObjectResponse",
|
|
612
|
+
"callAIWithStringResponse",
|
|
613
|
+
"defaultYhtConfig",
|
|
614
|
+
"extractJSONFromCodeBlock",
|
|
615
|
+
"getResponseFormat",
|
|
616
|
+
"preprocessDoubaoBboxJson",
|
|
617
|
+
"safeParseJson"
|
|
618
|
+
].indexOf(__webpack_i__)) exports[__webpack_i__] = __webpack_exports__[__webpack_i__];
|
|
619
|
+
Object.defineProperty(exports, '__esModule', {
|
|
620
|
+
value: true
|
|
621
|
+
});
|
|
622
|
+
|
|
623
|
+
//# sourceMappingURL=index.js.map
|