ZhaoShanGeng commited on
Commit
9fc6da8
·
1 Parent(s): 3376324

refactor: 移除工具调用图片处理,新增 getDefaultIp 函数

Browse files

修改文件:
- src/utils/utils.js:移除 transformToolOutputForImages 函数,新增 getDefaultIp 函数获取本机 IP

Files changed (1) hide show
  1. src/utils/utils.js +16 -66
src/utils/utils.js CHANGED
@@ -1,7 +1,7 @@
1
  import config from '../config/config.js';
2
  import tokenManager from '../auth/token_manager.js';
3
  import { generateRequestId } from './idGenerator.js';
4
- import { saveBase64Image } from './imageStorage.js';
5
  import { getReasoningSignature, getToolSignature } from './thoughtSignatureCache.js';
6
  import { setToolNameMapping } from './toolNameCache.js';
7
 
@@ -59,64 +59,6 @@ function extractImagesFromContent(content) {
59
 
60
  return result;
61
  }
62
-
63
- // 尝试从工具调用返回的 JSON 字符串中提取图片,
64
- // 保存到本地图床并将 data 替换为 URL,同时附带 markdown 字段。
65
- function transformToolOutputForImages(rawContent) {
66
- if (!rawContent) return rawContent;
67
-
68
- let obj = rawContent;
69
- let isString = false;
70
-
71
- if (typeof rawContent === 'string') {
72
- isString = true;
73
- try {
74
- obj = JSON.parse(rawContent);
75
- } catch {
76
- // 不是 JSON,直接返回原始内容
77
- return rawContent;
78
- }
79
- }
80
-
81
- if (!obj || typeof obj !== 'object') {
82
- return rawContent;
83
- }
84
-
85
- const response = obj.response;
86
- const contents = response?.content;
87
- if (!Array.isArray(contents)) {
88
- return rawContent;
89
- }
90
-
91
- const markdownBlocks = [];
92
-
93
- for (const item of contents) {
94
- if (item && item.type === 'image' && item.data && item.mimeType) {
95
- try {
96
- const url = saveBase64Image(item.data, item.mimeType);
97
- // 去掉大体积的 base64,改为 URL
98
- delete item.data;
99
- item.url = url;
100
-
101
- const alt = item.alt || 'image';
102
- markdownBlocks.push(`![${alt}](${url})`);
103
- } catch {
104
- // 单张图片保存失败时忽略,继续处理其它内容
105
- }
106
- }
107
- }
108
-
109
- if (markdownBlocks.length > 0) {
110
- const markdown = markdownBlocks.join('\n\n');
111
- if (typeof obj.markdown === 'string' && obj.markdown.trim()) {
112
- obj.markdown += `\n\n${markdown}`;
113
- } else {
114
- obj.markdown = markdown;
115
- }
116
- }
117
-
118
- return isString ? JSON.stringify(obj) : obj;
119
- }
120
  function handleUserMessage(extracted, antigravityMessages){
121
  antigravityMessages.push({
122
  role: "user",
@@ -240,17 +182,12 @@ function handleToolCall(message, antigravityMessages){
240
  }
241
 
242
  const lastMessage = antigravityMessages[antigravityMessages.length - 1];
243
-
244
- // 尝试从工具输出中提取并持久化图片,返回值仍保持为字符串/原始格式,
245
- // 但内部的图片 data 会被替换为图床 URL,并附带 markdown 字段。
246
- const transformedContent = transformToolOutputForImages(message.content);
247
-
248
  const functionResponse = {
249
  functionResponse: {
250
  id: message.tool_call_id,
251
  name: functionName,
252
  response: {
253
- output: transformedContent
254
  }
255
  }
256
  };
@@ -533,8 +470,21 @@ function prepareImageRequest(requestBody) {
533
 
534
  return requestBody;
535
  }
 
 
 
 
 
 
 
 
 
 
 
 
536
  export{
537
  generateRequestId,
538
  generateRequestBody,
539
- prepareImageRequest
 
540
  }
 
1
  import config from '../config/config.js';
2
  import tokenManager from '../auth/token_manager.js';
3
  import { generateRequestId } from './idGenerator.js';
4
+ import os from 'os';
5
  import { getReasoningSignature, getToolSignature } from './thoughtSignatureCache.js';
6
  import { setToolNameMapping } from './toolNameCache.js';
7
 
 
59
 
60
  return result;
61
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
  function handleUserMessage(extracted, antigravityMessages){
63
  antigravityMessages.push({
64
  role: "user",
 
182
  }
183
 
184
  const lastMessage = antigravityMessages[antigravityMessages.length - 1];
 
 
 
 
 
185
  const functionResponse = {
186
  functionResponse: {
187
  id: message.tool_call_id,
188
  name: functionName,
189
  response: {
190
+ output: message.content
191
  }
192
  }
193
  };
 
470
 
471
  return requestBody;
472
  }
473
+
474
+ function getDefaultIp(){
475
+ const interfaces = os.networkInterfaces();
476
+ for (const iface of Object.values(interfaces)){
477
+ for (const inter of iface){
478
+ if (inter.family === 'IPv4' && !inter.internal){
479
+ return inter.address;
480
+ }
481
+ }
482
+ }
483
+ return '127.0.0.1';
484
+ }
485
  export{
486
  generateRequestId,
487
  generateRequestBody,
488
+ prepareImageRequest,
489
+ getDefaultIp
490
  }