adp-openclaw 0.0.43 → 0.0.44
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/package.json +1 -1
- package/src/adp-upload-tool.ts +62 -36
package/package.json
CHANGED
package/src/adp-upload-tool.ts
CHANGED
|
@@ -66,42 +66,65 @@ const REQUEST_TIMEOUT_MS = 30000;
|
|
|
66
66
|
|
|
67
67
|
// ==================== 工具函数 ====================
|
|
68
68
|
|
|
69
|
+
/** 扩展名到 MIME 类型的映射 */
|
|
70
|
+
const EXT_TO_MIME_MAP: Record<string, string> = {
|
|
71
|
+
".txt": "text/plain",
|
|
72
|
+
".md": "text/markdown",
|
|
73
|
+
".json": "application/json",
|
|
74
|
+
".html": "text/html",
|
|
75
|
+
".css": "text/css",
|
|
76
|
+
".js": "text/javascript",
|
|
77
|
+
".ts": "text/x.typescript",
|
|
78
|
+
".xml": "application/xml",
|
|
79
|
+
".yaml": "application/yaml",
|
|
80
|
+
".yml": "application/yaml",
|
|
81
|
+
".pdf": "application/pdf",
|
|
82
|
+
".png": "image/png",
|
|
83
|
+
".jpg": "image/jpeg",
|
|
84
|
+
".jpeg": "image/jpeg",
|
|
85
|
+
".gif": "image/gif",
|
|
86
|
+
".webp": "image/webp",
|
|
87
|
+
".svg": "image/svg+xml",
|
|
88
|
+
".csv": "text/csv",
|
|
89
|
+
".zip": "application/zip",
|
|
90
|
+
".doc": "application/msword",
|
|
91
|
+
".docx": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
|
92
|
+
".xls": "application/vnd.ms-excel",
|
|
93
|
+
".xlsx": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
|
94
|
+
".ppt": "application/vnd.ms-powerpoint",
|
|
95
|
+
".pptx": "application/vnd.openxmlformats-officedocument.presentationml.presentation",
|
|
96
|
+
".mp3": "audio/mpeg",
|
|
97
|
+
".mp4": "video/mp4",
|
|
98
|
+
".wav": "audio/wav",
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
/** MIME 类型到扩展名的映射(反向映射) */
|
|
102
|
+
const MIME_TO_EXT_MAP: Record<string, string> = Object.fromEntries(
|
|
103
|
+
Object.entries(EXT_TO_MIME_MAP).map(([ext, mime]) => [mime, ext.slice(1)]) // 去掉 "." 前缀
|
|
104
|
+
);
|
|
105
|
+
|
|
69
106
|
/**
|
|
70
107
|
* 根据文件扩展名推断 MIME 类型
|
|
71
|
-
|
|
108
|
+
*/
|
|
72
109
|
function inferMimeType(fileName: string): string {
|
|
73
110
|
const ext = extname(fileName).toLowerCase();
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
".svg": "image/svg+xml",
|
|
92
|
-
".csv": "text/csv",
|
|
93
|
-
".zip": "application/zip",
|
|
94
|
-
".doc": "application/msword",
|
|
95
|
-
".docx": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
|
96
|
-
".xls": "application/vnd.ms-excel",
|
|
97
|
-
".xlsx": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
|
98
|
-
".ppt": "application/vnd.ms-powerpoint",
|
|
99
|
-
".pptx": "application/vnd.openxmlformats-officedocument.presentationml.presentation",
|
|
100
|
-
".mp3": "audio/mpeg",
|
|
101
|
-
".mp4": "video/mp4",
|
|
102
|
-
".wav": "audio/wav",
|
|
103
|
-
};
|
|
104
|
-
return mimeMap[ext] || "application/octet-stream";
|
|
111
|
+
return EXT_TO_MIME_MAP[ext] || "application/octet-stream";
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* 把 MIME 类型或扩展名转换为纯扩展名(不带点)
|
|
116
|
+
* 服务端 API 期望的是文件扩展名,而不是 MIME 类型
|
|
117
|
+
*/
|
|
118
|
+
function toFileExtension(fileTypeOrMime: string): string {
|
|
119
|
+
if (!fileTypeOrMime) return "";
|
|
120
|
+
|
|
121
|
+
// 如果已经是扩展名格式(不包含 /),直接返回(去掉可能的前导点)
|
|
122
|
+
if (!fileTypeOrMime.includes("/")) {
|
|
123
|
+
return fileTypeOrMime.replace(/^\./, "");
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// 如果是 MIME 类型,转换为扩展名
|
|
127
|
+
return MIME_TO_EXT_MAP[fileTypeOrMime] || fileTypeOrMime;
|
|
105
128
|
}
|
|
106
129
|
|
|
107
130
|
/**
|
|
@@ -163,13 +186,16 @@ export async function getStorageCredential(
|
|
|
163
186
|
token: string,
|
|
164
187
|
fileType?: string
|
|
165
188
|
): Promise<DescribeRemoteBotStorageCredentialRsp> {
|
|
189
|
+
// 把 MIME 类型转换为文件扩展名,服务端 API 期望的是扩展名
|
|
190
|
+
const fileExtension = toFileExtension(fileType || "");
|
|
191
|
+
|
|
166
192
|
const requestBody: DescribeRemoteBotStorageCredentialReq = {
|
|
167
193
|
token,
|
|
168
|
-
file_type:
|
|
194
|
+
file_type: fileExtension,
|
|
169
195
|
};
|
|
170
196
|
|
|
171
197
|
console.log(`[ADP-UPLOAD] Calling credential API: ${CREDENTIAL_API_URL}`);
|
|
172
|
-
console.log(`[ADP-UPLOAD] Request body: token=${token.substring(0, 10)}..., file_type=${fileType || ""}`);
|
|
198
|
+
console.log(`[ADP-UPLOAD] Request body: token=${token.substring(0, 10)}..., file_type=${fileExtension} (original: ${fileType || ""})`);
|
|
173
199
|
|
|
174
200
|
const response = await fetchWithTimeout(
|
|
175
201
|
CREDENTIAL_API_URL,
|
|
@@ -236,7 +262,7 @@ export async function uploadFileToCos(
|
|
|
236
262
|
const fileUrl = credential.file_url;
|
|
237
263
|
|
|
238
264
|
console.log(`[ADP-UPLOAD] upload_url: ${uploadUrl.substring(0, 100)}...`);
|
|
239
|
-
console.log(`[ADP-UPLOAD] file_url (download): ${fileUrl
|
|
265
|
+
console.log(`[ADP-UPLOAD] file_url (download): ${fileUrl}...`);
|
|
240
266
|
|
|
241
267
|
// 4. 执行上传(直接PUT到预签名URL,不需要额外签名)
|
|
242
268
|
const response = await fetchWithTimeout(
|
|
@@ -257,7 +283,7 @@ export async function uploadFileToCos(
|
|
|
257
283
|
throw new Error(`上传文件失败: ${response.status} ${errorText}`);
|
|
258
284
|
}
|
|
259
285
|
|
|
260
|
-
console.log(`[ADP-UPLOAD] Upload success, returning file_url: ${fileUrl
|
|
286
|
+
console.log(`[ADP-UPLOAD] Upload success, returning file_url: ${fileUrl}...`);
|
|
261
287
|
|
|
262
288
|
// 5. 返回下载URL
|
|
263
289
|
return fileUrl;
|