收口 MinerU OCR 任务链与上下文
This commit is contained in:
@@ -412,6 +412,84 @@ export function createSidebarPageAiTargetRuntime(context) {
|
||||
});
|
||||
}
|
||||
|
||||
function pageAiOcrEligibleResourceKind(value) {
|
||||
var normalized = String(value || '').trim().toLowerCase();
|
||||
return normalized === 'image' || normalized === 'pdf' || normalized === 'attachment' || normalized === 'resource';
|
||||
}
|
||||
|
||||
function pageAiOcrEligiblePath(value) {
|
||||
var path = String(value || '').trim().toLowerCase();
|
||||
return /\.(png|jpg|jpeg|webp|bmp|tif|tiff|pdf)$/.test(path);
|
||||
}
|
||||
|
||||
function pageAiOcrBodyPreview(markdown) {
|
||||
var body = String(markdown || '').replace(/^---\n[\s\S]*?\n---\n?/, '').trim();
|
||||
return body.slice(0, 1600);
|
||||
}
|
||||
|
||||
async function fetchPageAiOcrSidecarContext(editorTarget) {
|
||||
if (currentSourceKind() !== 'local_folder') return null;
|
||||
var target = editorTarget && typeof editorTarget === 'object' ? editorTarget : {};
|
||||
var workspacePath = target.workspacePath && typeof target.workspacePath === 'object' ? target.workspacePath : {};
|
||||
var relativePath = String(workspacePath.relativePath || target.path || '').trim();
|
||||
var resourceKind = String(target.resourceKind || target.editorKind || workspacePath.resourceKind || '').trim();
|
||||
if (!relativePath || !pageAiOcrEligiblePath(relativePath) || !pageAiOcrEligibleResourceKind(resourceKind)) return null;
|
||||
var rootUri = String(workspacePath.rootUri || currentRootUri() || '').trim();
|
||||
if (!rootUri) return null;
|
||||
try {
|
||||
var statusUrl = new URL('/api/local-folder/ocr/status', window.location.origin);
|
||||
statusUrl.searchParams.set('rootUri', rootUri);
|
||||
statusUrl.searchParams.set('sourceRootRelativePath', relativePath);
|
||||
var statusResponse = await fetch(statusUrl.toString(), { cache: 'no-store', headers: { accept: 'application/json' } });
|
||||
var statusPayload = await statusResponse.json().catch(function() { return null; });
|
||||
var job = statusResponse.ok && statusPayload && statusPayload.ok === true ? statusPayload.job : null;
|
||||
var ocrPath = String(job && job.ocrRootRelativePath || '').trim();
|
||||
if (!ocrPath || String(job.status || '') === 'failed') return null;
|
||||
var readUrl = new URL('/api/local-folder/ocr/read', window.location.origin);
|
||||
readUrl.searchParams.set('rootUri', rootUri);
|
||||
readUrl.searchParams.set('ocrRootRelativePath', ocrPath);
|
||||
var readResponse = await fetch(readUrl.toString(), { cache: 'no-store', headers: { accept: 'application/json' } });
|
||||
var readPayload = await readResponse.json().catch(function() { return null; });
|
||||
if (!readResponse.ok || !readPayload || readPayload.ok !== true) return null;
|
||||
return {
|
||||
schema: 'mnote.local_ocr_context.v1',
|
||||
source: 'local_ocr_sidecar',
|
||||
sourceRootRelativePath: relativePath,
|
||||
ocrRootRelativePath: ocrPath,
|
||||
status: String(job.status || ''),
|
||||
stale: job.stale === true,
|
||||
provider: String(job.provider || ''),
|
||||
modelVersion: String(job.modelVersion || ''),
|
||||
plainTextPreview: pageAiOcrBodyPreview(readPayload.markdown || ''),
|
||||
};
|
||||
} catch (_) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
async function pageAiEnrichOcrContextRefs(contextRefs, agentTargetPackage, editorTarget) {
|
||||
var ocrContext = await fetchPageAiOcrSidecarContext(editorTarget);
|
||||
if (!ocrContext) return { contextRefs, agentTargetPackage };
|
||||
var nextRefs = pageAiNormalizeArray(contextRefs).map(function(ref) {
|
||||
if (ref && ref.kind === 'active_editor') return Object.assign({}, ref, { ocrContext: ocrContext });
|
||||
return ref;
|
||||
});
|
||||
var nextPackage = agentTargetPackage && typeof agentTargetPackage === 'object'
|
||||
? Object.assign({}, agentTargetPackage, { ocrContext: ocrContext })
|
||||
: agentTargetPackage;
|
||||
if (nextPackage && nextPackage.currentFile && typeof nextPackage.currentFile === 'object') {
|
||||
nextPackage.currentFile = Object.assign({}, nextPackage.currentFile, { ocrRootRelativePath: ocrContext.ocrRootRelativePath });
|
||||
}
|
||||
if (nextPackage && Array.isArray(nextPackage.targets)) {
|
||||
nextPackage.targets = nextPackage.targets.map(function(target, index) {
|
||||
return index === 0 && target && typeof target === 'object'
|
||||
? Object.assign({}, target, { ocrContext: ocrContext })
|
||||
: target;
|
||||
});
|
||||
}
|
||||
return { contextRefs: nextRefs, agentTargetPackage: nextPackage };
|
||||
}
|
||||
|
||||
function pageAiBuildAllowedRoots() {
|
||||
return pageAiNormalizeArray(pageUiState.pageAiAllowedRoots).map(function(root) {
|
||||
return {
|
||||
@@ -733,6 +811,7 @@ export function createSidebarPageAiTargetRuntime(context) {
|
||||
pageAiBuildAgentTargetPackage,
|
||||
pageAiBuildAllowedRoots,
|
||||
pageAiBuildContextRefs,
|
||||
pageAiEnrichOcrContextRefs,
|
||||
pageAiBuildRunTargetSnapshot,
|
||||
pageAiCloneJson,
|
||||
pageAiContextKindsFromRefs,
|
||||
|
||||
Reference in New Issue
Block a user