feat(server): use new content reader (#13007)

partial fix AI-280
This commit is contained in:
DarkSky
2025-07-04 08:22:44 +08:00
committed by GitHub
parent 8ed7dea823
commit 2b7a8dcd8a
6 changed files with 48 additions and 15 deletions

View File

@@ -47,9 +47,14 @@ export class DocRpcController {
@Get('/workspaces/:workspaceId/docs/:docId/markdown')
async getDocMarkdown(
@Param('workspaceId') workspaceId: string,
@Param('docId') docId: string
@Param('docId') docId: string,
@Query('aiEditable') aiEditable?: string
) {
const result = await this.docReader.getDocMarkdown(workspaceId, docId);
const result = await this.docReader.getDocMarkdown(
workspaceId,
docId,
aiEditable === 'true'
);
if (!result) {
throw new NotFound('Doc not found');
}

View File

@@ -269,7 +269,11 @@ test('should return doc markdown success', async t => {
user,
});
const result = await docReader.getDocMarkdown(workspace.id, docSnapshot.id);
const result = await docReader.getDocMarkdown(
workspace.id,
docSnapshot.id,
false
);
t.snapshot(result);
});
@@ -279,6 +283,10 @@ test('should read markdown return null when doc not exists', async t => {
name: '',
});
const result = await docReader.getDocMarkdown(workspace.id, randomUUID());
const result = await docReader.getDocMarkdown(
workspace.id,
randomUUID(),
false
);
t.is(result, null);
});

View File

@@ -389,7 +389,11 @@ test('should return doc markdown success', async t => {
user,
});
const result = await docReader.getDocMarkdown(workspace.id, docSnapshot.id);
const result = await docReader.getDocMarkdown(
workspace.id,
docSnapshot.id,
false
);
t.snapshot(result);
});
@@ -401,6 +405,10 @@ test('should read markdown return null when doc not exists', async t => {
name: '',
});
const result = await docReader.getDocMarkdown(workspace.id, randomUUID());
const result = await docReader.getDocMarkdown(
workspace.id,
randomUUID(),
false
);
t.is(result, null);
});

View File

@@ -67,7 +67,8 @@ export abstract class DocReader {
abstract getDocMarkdown(
workspaceId: string,
docId: string
docId: string,
aiEditable: boolean
): Promise<DocMarkdown | null>;
abstract getDocDiff(
@@ -184,13 +185,19 @@ export class DatabaseDocReader extends DocReader {
async getDocMarkdown(
workspaceId: string,
docId: string
docId: string,
aiEditable: boolean
): Promise<DocMarkdown | null> {
const doc = await this.workspace.getDoc(workspaceId, docId);
if (!doc) {
return null;
}
return parseDocToMarkdownFromDocSnapshot(workspaceId, docId, doc.bin);
return parseDocToMarkdownFromDocSnapshot(
workspaceId,
docId,
doc.bin,
aiEditable
);
}
async getDocDiff(
@@ -328,9 +335,10 @@ export class RpcDocReader extends DatabaseDocReader {
override async getDocMarkdown(
workspaceId: string,
docId: string
docId: string,
aiEditable: boolean
): Promise<DocMarkdown | null> {
const url = `${this.config.docService.endpoint}/rpc/workspaces/${workspaceId}/docs/${docId}/markdown`;
const url = `${this.config.docService.endpoint}/rpc/workspaces/${workspaceId}/docs/${docId}/markdown?aiEditable=${aiEditable}`;
const accessToken = this.crypto.sign(docId);
try {
const res = await this.fetch(accessToken, url, 'GET');
@@ -349,7 +357,7 @@ export class RpcDocReader extends DatabaseDocReader {
err
);
// fallback to database doc reader if the error is not user friendly, like network error
return await super.getDocMarkdown(workspaceId, docId);
return await super.getDocMarkdown(workspaceId, docId, aiEditable);
}
}