@ynhcj/xiaoyi-channel 0.0.81-beta → 0.0.83-beta
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/dist/src/bot.js +1 -0
- package/dist/src/file-download.js +3 -6
- package/dist/src/file-upload.js +52 -5
- package/dist/src/outbound.js +2 -7
- package/dist/src/provider.js +27 -4
- package/dist/src/tools/xiaoyi-add-collection-tool.js +5 -1
- package/dist/src/tools/xiaoyi-collection-tool.js +1 -0
- package/package.json +1 -1
package/dist/src/bot.js
CHANGED
|
@@ -177,6 +177,7 @@ export async function handleXYMessage(params) {
|
|
|
177
177
|
const fileParts = extractFileParts(parsed.parts);
|
|
178
178
|
// Download files to local disk
|
|
179
179
|
const downloadedFiles = await downloadFilesFromParts(fileParts);
|
|
180
|
+
console.log("Downloaded files:", JSON.stringify(downloadedFiles, null, 2));
|
|
180
181
|
const mediaPayload = buildXYMediaPayload(downloadedFiles);
|
|
181
182
|
// Resolve envelope format options (following feishu pattern)
|
|
182
183
|
const envelopeOptions = core.channel.reply.resolveEnvelopeFormatOptions(cfg);
|
|
@@ -2,12 +2,10 @@
|
|
|
2
2
|
import fetch from "node-fetch";
|
|
3
3
|
import fs from "fs/promises";
|
|
4
4
|
import path from "path";
|
|
5
|
-
import { logger } from "./utils/logger.js";
|
|
6
5
|
/**
|
|
7
6
|
* Download a file from URL to local path.
|
|
8
7
|
*/
|
|
9
8
|
export async function downloadFile(url, destPath) {
|
|
10
|
-
logger.debug(`Downloading file from ${url} to ${destPath}`);
|
|
11
9
|
const controller = new AbortController();
|
|
12
10
|
const timeout = setTimeout(() => controller.abort(), 30000); // 30 seconds timeout
|
|
13
11
|
try {
|
|
@@ -18,14 +16,13 @@ export async function downloadFile(url, destPath) {
|
|
|
18
16
|
const arrayBuffer = await response.arrayBuffer();
|
|
19
17
|
const buffer = Buffer.from(arrayBuffer);
|
|
20
18
|
await fs.writeFile(destPath, buffer);
|
|
21
|
-
logger.debug(`File downloaded successfully: ${destPath}`);
|
|
22
19
|
}
|
|
23
20
|
catch (error) {
|
|
24
21
|
if (error.name === 'AbortError') {
|
|
25
|
-
|
|
22
|
+
console.log(`Download timeout (30s) for ${url}`);
|
|
26
23
|
throw new Error(`Download timeout after 30 seconds`);
|
|
27
24
|
}
|
|
28
|
-
|
|
25
|
+
console.log(`Failed to download file from ${url}:`);
|
|
29
26
|
throw error;
|
|
30
27
|
}
|
|
31
28
|
finally {
|
|
@@ -54,7 +51,7 @@ export async function downloadFilesFromParts(fileParts, tempDir = "/tmp/xy_chann
|
|
|
54
51
|
});
|
|
55
52
|
}
|
|
56
53
|
catch (error) {
|
|
57
|
-
|
|
54
|
+
console.log(`Failed to download file ${name}:`);
|
|
58
55
|
// Continue with other files
|
|
59
56
|
}
|
|
60
57
|
}
|
package/dist/src/file-upload.js
CHANGED
|
@@ -2,8 +2,25 @@
|
|
|
2
2
|
// OSMS file upload implementation
|
|
3
3
|
import fetch from "node-fetch";
|
|
4
4
|
import fs from "fs/promises";
|
|
5
|
+
import os from "os";
|
|
5
6
|
import path from "path";
|
|
6
7
|
import { calculateSHA256 } from "./utils/crypto.js";
|
|
8
|
+
function isRemoteUrl(filePath) {
|
|
9
|
+
return filePath.startsWith("http://") || filePath.startsWith("https://");
|
|
10
|
+
}
|
|
11
|
+
async function downloadToTempFile(url) {
|
|
12
|
+
console.log(`[XY File Upload] Downloading remote file: ${url}`);
|
|
13
|
+
const response = await fetch(url);
|
|
14
|
+
if (!response.ok) {
|
|
15
|
+
throw new Error(`Failed to download remote file: HTTP ${response.status}`);
|
|
16
|
+
}
|
|
17
|
+
const buffer = await response.buffer();
|
|
18
|
+
const urlFileName = path.basename(new URL(url).pathname) || "download";
|
|
19
|
+
const tempPath = path.join(os.tmpdir(), `xy-upload-${Date.now()}-${urlFileName}`);
|
|
20
|
+
await fs.writeFile(tempPath, buffer);
|
|
21
|
+
console.log(`[XY File Upload] Downloaded to temp file: ${tempPath}`);
|
|
22
|
+
return tempPath;
|
|
23
|
+
}
|
|
7
24
|
/**
|
|
8
25
|
* Service for uploading files to XY file storage.
|
|
9
26
|
* Implements three-phase upload: prepare → upload → complete.
|
|
@@ -23,10 +40,17 @@ export class XYFileUploadService {
|
|
|
23
40
|
*/
|
|
24
41
|
async uploadFile(filePath, objectType = "TEMPORARY_MATERIAL_DOC") {
|
|
25
42
|
console.log(`[XY File Upload] Starting file upload: ${filePath}`);
|
|
43
|
+
let localFilePath = filePath;
|
|
44
|
+
let isTempFile = false;
|
|
26
45
|
try {
|
|
46
|
+
// Handle remote URLs by downloading first
|
|
47
|
+
if (isRemoteUrl(filePath)) {
|
|
48
|
+
localFilePath = await downloadToTempFile(filePath);
|
|
49
|
+
isTempFile = true;
|
|
50
|
+
}
|
|
27
51
|
// Read file
|
|
28
|
-
const fileBuffer = await fs.readFile(
|
|
29
|
-
const fileName = path.basename(
|
|
52
|
+
const fileBuffer = await fs.readFile(localFilePath);
|
|
53
|
+
const fileName = path.basename(localFilePath);
|
|
30
54
|
const fileSha256 = calculateSHA256(fileBuffer);
|
|
31
55
|
const fileSize = fileBuffer.length;
|
|
32
56
|
// Phase 1: Prepare
|
|
@@ -96,7 +120,15 @@ export class XYFileUploadService {
|
|
|
96
120
|
}
|
|
97
121
|
catch (error) {
|
|
98
122
|
console.error(`[XY File Upload] File upload failed for ${filePath}:`, error);
|
|
99
|
-
|
|
123
|
+
throw error;
|
|
124
|
+
}
|
|
125
|
+
finally {
|
|
126
|
+
if (isTempFile) {
|
|
127
|
+
try {
|
|
128
|
+
await fs.unlink(localFilePath);
|
|
129
|
+
}
|
|
130
|
+
catch { }
|
|
131
|
+
}
|
|
100
132
|
}
|
|
101
133
|
}
|
|
102
134
|
/**
|
|
@@ -104,10 +136,17 @@ export class XYFileUploadService {
|
|
|
104
136
|
* Uses completeAndQuery endpoint to get the file URL directly.
|
|
105
137
|
*/
|
|
106
138
|
async uploadFileAndGetUrl(filePath, objectType = "TEMPORARY_MATERIAL_DOC") {
|
|
139
|
+
let localFilePath = filePath;
|
|
140
|
+
let isTempFile = false;
|
|
107
141
|
try {
|
|
142
|
+
// Handle remote URLs by downloading first
|
|
143
|
+
if (isRemoteUrl(filePath)) {
|
|
144
|
+
localFilePath = await downloadToTempFile(filePath);
|
|
145
|
+
isTempFile = true;
|
|
146
|
+
}
|
|
108
147
|
// Read file
|
|
109
|
-
const fileBuffer = await fs.readFile(
|
|
110
|
-
const fileName = path.basename(
|
|
148
|
+
const fileBuffer = await fs.readFile(localFilePath);
|
|
149
|
+
const fileName = path.basename(localFilePath);
|
|
111
150
|
const fileSha256 = calculateSHA256(fileBuffer);
|
|
112
151
|
const fileSize = fileBuffer.length;
|
|
113
152
|
// Phase 1: Prepare
|
|
@@ -186,6 +225,14 @@ export class XYFileUploadService {
|
|
|
186
225
|
console.error(`[XY File Upload] File upload with URL retrieval failed for ${filePath}:`, error);
|
|
187
226
|
throw error;
|
|
188
227
|
}
|
|
228
|
+
finally {
|
|
229
|
+
if (isTempFile) {
|
|
230
|
+
try {
|
|
231
|
+
await fs.unlink(localFilePath);
|
|
232
|
+
}
|
|
233
|
+
catch { }
|
|
234
|
+
}
|
|
235
|
+
}
|
|
189
236
|
}
|
|
190
237
|
/**
|
|
191
238
|
* Upload multiple files and return their file IDs.
|
package/dist/src/outbound.js
CHANGED
|
@@ -174,14 +174,9 @@ export const xyOutbound = {
|
|
|
174
174
|
}
|
|
175
175
|
// Upload file
|
|
176
176
|
const fileId = await uploadService.uploadFile(mediaUrl);
|
|
177
|
-
// Check if fileId is empty
|
|
177
|
+
// Check if fileId is empty (should not happen if uploadFile throws on failure)
|
|
178
178
|
if (!fileId) {
|
|
179
|
-
|
|
180
|
-
return {
|
|
181
|
-
channel: "xiaoyi-channel",
|
|
182
|
-
messageId: "",
|
|
183
|
-
chatId: to,
|
|
184
|
-
};
|
|
179
|
+
throw new Error(`File upload returned empty fileId for: ${mediaUrl}`);
|
|
185
180
|
}
|
|
186
181
|
console.log(`[xyOutbound.sendMedia] File uploaded:`, {
|
|
187
182
|
fileId,
|
package/dist/src/provider.js
CHANGED
|
@@ -101,11 +101,34 @@ export const xiaoyiProvider = {
|
|
|
101
101
|
if (context.systemPrompt) {
|
|
102
102
|
console.log(`[xiaoyiprovider] system prompt length: ${context.systemPrompt.length}`);
|
|
103
103
|
}
|
|
104
|
-
//
|
|
104
|
+
// 在发送给模型前,优化 systemPrompt 结构
|
|
105
105
|
if (context.systemPrompt) {
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
106
|
+
let sp = context.systemPrompt;
|
|
107
|
+
const beforeLen = sp.length;
|
|
108
|
+
// 删除 ## Tooling 与 TOOLS.md 声明之间的内容
|
|
109
|
+
sp = sp.replace(/(## Tooling)[\s\S]*?(TOOLS\.md does not control tool availability; it is user guidance for how to use external tools\.)/, "$1\n\n$2");
|
|
110
|
+
// (1) 提取 <available_skills>...</available_skills> 作为第一部分
|
|
111
|
+
const skillsMatch = sp.match(/<available_skills>[\s\S]*?<\/available_skills>/);
|
|
112
|
+
const part1 = skillsMatch ? skillsMatch[0] : '';
|
|
113
|
+
// (2) 提取 # SOUL.md - Who You Are 到 # TOOLS.md - Local Notes 之前的内容作为第二部分
|
|
114
|
+
const soulMatch = sp.match(/(# SOUL\.md - Who You Are[\s\S]*?)(?=# TOOLS\.md - Local Notes)/);
|
|
115
|
+
const part2 = soulMatch ? soulMatch[1].trim() : '';
|
|
116
|
+
if (part1 || part2) {
|
|
117
|
+
// 从原始位置删除已提取的部分
|
|
118
|
+
if (skillsMatch)
|
|
119
|
+
sp = sp.replace(skillsMatch[0], '');
|
|
120
|
+
if (soulMatch)
|
|
121
|
+
sp = sp.replace(soulMatch[1], '');
|
|
122
|
+
// 清理多余空行
|
|
123
|
+
sp = sp.replace(/\n{3,}/g, '\n\n');
|
|
124
|
+
// (3) 将 第二部分 + 第一部分 插入到 ## Runtime 上面
|
|
125
|
+
const combined = (part2 + '\n\n' + part1).trim();
|
|
126
|
+
if (combined && sp.includes('## Runtime')) {
|
|
127
|
+
sp = sp.replace('## Runtime', combined + '\n\n## Runtime');
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
console.log(`[xiaoyiprovider] system prompt optimized: ${beforeLen} -> ${sp.length}`);
|
|
131
|
+
context.systemPrompt = sp;
|
|
109
132
|
}
|
|
110
133
|
const stream = await underlying(model, context, {
|
|
111
134
|
...options,
|
|
@@ -45,7 +45,11 @@ export const xiaoyiAddCollectionTool = {
|
|
|
45
45
|
},
|
|
46
46
|
uri: {
|
|
47
47
|
type: "string",
|
|
48
|
-
description:
|
|
48
|
+
description: `必填字段(IMAGE/FILE类型时)。图片或文件的地址链接。
|
|
49
|
+
uri具备三种严格的格式
|
|
50
|
+
(1) http或者https开头的公网链接,一般是用户通过联网搜索获取的文件地址,不允许自行编造,此类链接填入时必须确保真实
|
|
51
|
+
(2) file://开头的链接,此类链接必须来源于query_collection结果或者search_file或者search_photo_gallery的结果,不允许自行编造
|
|
52
|
+
(3) 本地路径例,如/tmp/xy_channel/xxx,此类路径是文件保存在当前openclaw运行环境的本地目录,可以直接填入,不要擅自拼接http://或者file://前缀`,
|
|
49
53
|
},
|
|
50
54
|
sourceAppBundleName: {
|
|
51
55
|
type: "string",
|
|
@@ -25,6 +25,7 @@ export const xiaoyiCollectionTool = {
|
|
|
25
25
|
a. 操作超时时间为60秒,请勿重复调用此工具
|
|
26
26
|
b. 如果遇到各类调用失败场景,最多只能重试一次,不可以重复调用多次。
|
|
27
27
|
c. 调用工具前需认真检查调用参数是否满足工具要求
|
|
28
|
+
d. 如果用户希望获取文件,可以使用upload_file工具完成文件上传
|
|
28
29
|
|
|
29
30
|
回复约束:如果工具返回没有授权或者其他报错,只需要完整描述没有授权或者其他报错内容即可,不需要主动给用户提供解决方案,例如告诉用户如何授权,如何解决报错等都是不需要的,请严格遵守。
|
|
30
31
|
`,
|