Skip to content

Commit 0369bd3

Browse files
committed
fix(knowledge): 修复内容文件读取时的编码和错误提示
- 为知识块添加和更新命令的读取内容文件函数添加了可选的inline flag参数 - 更新readUtf8TextFile以支持根据inline flag生成更友好的ENOENT错误提示 - 确保内容读取时使用严格的UTF-8编码解码方式 - 如果读取文件失败,提供文件路径和权限相关的详细错误信息 - 校验知识块内容长度时保持一致的错误处理逻辑
1 parent 92a978a commit 0369bd3

3 files changed

Lines changed: 24 additions & 9 deletions

File tree

packages/commands/src/commands/knowledge/chunk-add.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,9 @@ export default defineCommand({
121121
field = parseFieldEntries(flags.field);
122122
} else {
123123
const content =
124-
flags.contentFile !== undefined ? readUtf8TextFile(flags.contentFile) : flags.content;
124+
flags.contentFile !== undefined
125+
? readUtf8TextFile(flags.contentFile, "--content")
126+
: flags.content;
125127
if (typeof content === "string" && content.length > 6000) {
126128
throw new BailianError("Chunk content must be at most 6000 characters", ExitCode.USAGE);
127129
}

packages/commands/src/commands/knowledge/chunk-update.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,9 @@ export default defineCommand({
122122
// dry-run also reads the file and validates (rehearsal semantics); the read-back
123123
// request is only made outside dry-run and when no new content is given
124124
let content =
125-
flags.contentFile !== undefined ? readUtf8TextFile(flags.contentFile) : flags.content;
125+
flags.contentFile !== undefined
126+
? readUtf8TextFile(flags.contentFile, "--content")
127+
: flags.content;
126128
if (content !== undefined && (content.length < 10 || content.length > 6000)) {
127129
throw new BailianError("Chunk content must be 10-6000 characters", ExitCode.USAGE);
128130
}

packages/commands/src/commands/knowledge/upload-support.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -192,22 +192,33 @@ const DOCUMENT_EXTENSIONS = new Set([
192192
]);
193193

194194
/**
195-
* Read a UTF-8 plain-text file (--content-file for chunk add/update).
195+
* Read a UTF-8 plain-text file.
196196
* Support is defined by content, not extension: any UTF-8 text is valid.
197197
* Strict decode failure → USAGE, with a hint pointing to the document upload
198198
* flow when the extension is a document format; file I/O failure → GENERAL + errno.
199+
*
200+
* @param inlineAlternativeFlag When provided, an ENOENT on a value that looks
201+
* like inline text (no path separator / no extension) appends a hint pointing
202+
* the user to this flag instead. Pass the inline-text flag name (e.g.
203+
* `"--content"`) only from commands that have a file-vs-inline choice.
199204
*/
200-
export function readUtf8TextFile(filePath: string): string {
205+
export function readUtf8TextFile(filePath: string, inlineAlternativeFlag?: string): string {
201206
let fileBuffer: Buffer;
202207
try {
203208
fileBuffer = readFileSync(filePath);
204209
} catch (error) {
205210
const errno = (error as { code?: string }).code ?? "unknown";
206-
throw new BailianError(
207-
`Cannot read file: ${filePath}`,
208-
ExitCode.GENERAL,
209-
`File system error (${errno}) — check the path and permissions.`,
210-
);
211+
let hint = `File system error (${errno}) — check the path and permissions.`;
212+
if (
213+
inlineAlternativeFlag &&
214+
errno === "ENOENT" &&
215+
!extname(filePath) &&
216+
!filePath.includes("/") &&
217+
!filePath.includes("\\")
218+
) {
219+
hint += ` If you meant to pass text content directly, use ${inlineAlternativeFlag} instead of --content-file.`;
220+
}
221+
throw new BailianError(`Cannot read file: ${filePath}`, ExitCode.GENERAL, hint);
211222
}
212223
try {
213224
return new TextDecoder("utf-8", { fatal: true }).decode(fileBuffer);

0 commit comments

Comments
 (0)