picocode-core 0.9.126 → 0.9.128
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/tools/read.js +25 -8
package/package.json
CHANGED
package/src/tools/read.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { describeParam } from './recorder.js'
|
|
2
2
|
import { readFile } from 'node:fs/promises'
|
|
3
3
|
import { resolve } from 'node:path'
|
|
4
|
+
import { execFile } from 'node:child_process'
|
|
4
5
|
import { extractText } from 'unpdf'
|
|
5
6
|
|
|
6
7
|
const MAX_LINES = 2000
|
|
@@ -9,15 +10,31 @@ const MAX_LINE_LENGTH = 2000
|
|
|
9
10
|
const isPdf = (buffer) => buffer.subarray(0, 5).toString('latin1') === '%PDF-'
|
|
10
11
|
|
|
11
12
|
// a pdf reads as its extracted text, one page after another, so the same
|
|
12
|
-
// offset and limit window applies
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
// offset and limit window applies. poppler's pdftotext keeps the page
|
|
14
|
+
// layout (labels beside their values, columns in order) so it is preferred
|
|
15
|
+
// when installed; unpdf is the bundled fallback
|
|
16
|
+
function pdftotext(full) {
|
|
17
|
+
return new Promise((resolve) => {
|
|
18
|
+
execFile('pdftotext', ['-layout', full, '-'], { maxBuffer: 64 * 1024 * 1024 }, (error, stdout) => {
|
|
19
|
+
if (error) return resolve(null)
|
|
20
|
+
const pages = stdout.replace(/\f$/, '').split('\f')
|
|
21
|
+
resolve(pages.map((page) => page.replace(/\s+$/, '').split('\n')))
|
|
22
|
+
})
|
|
23
|
+
})
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
async function unpdfPages(buffer) {
|
|
27
|
+
const { text } = await extractText(new Uint8Array(buffer), { mergePages: false })
|
|
28
|
+
return (Array.isArray(text) ? text : [text]).map((page) => String(page).split('\n'))
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
async function pdfLines(full, buffer) {
|
|
32
|
+
const pages = (await pdftotext(full)) ?? (await unpdfPages(buffer))
|
|
16
33
|
const lines = []
|
|
17
34
|
pages.forEach((page, i) => {
|
|
18
35
|
if (i) lines.push('')
|
|
19
|
-
lines.push(`[page ${i + 1} of ${
|
|
20
|
-
lines.push(...
|
|
36
|
+
lines.push(`[page ${i + 1} of ${pages.length}]`)
|
|
37
|
+
lines.push(...page)
|
|
21
38
|
})
|
|
22
39
|
return lines
|
|
23
40
|
}
|
|
@@ -33,7 +50,7 @@ function isBinary(buffer) {
|
|
|
33
50
|
export function createRead({ cwd, recorder, tracker }) {
|
|
34
51
|
return {
|
|
35
52
|
name: 'read',
|
|
36
|
-
description: 'Read a file. Returns line-numbered content. Use offset/limit for large files.',
|
|
53
|
+
description: 'Read a file. Returns line-numbered content; pdf files return their extracted text with page markers. Use offset/limit for large files.',
|
|
37
54
|
schema: {
|
|
38
55
|
description: describeParam,
|
|
39
56
|
path: { type: 'string', description: 'file path, relative to the working directory or absolute' },
|
|
@@ -44,7 +61,7 @@ export function createRead({ cwd, recorder, tracker }) {
|
|
|
44
61
|
const full = resolve(cwd, path)
|
|
45
62
|
recorder.extra({ title: path })
|
|
46
63
|
const buf = await readFile(full)
|
|
47
|
-
const lines = isPdf(buf) ? await pdfLines(buf) : null
|
|
64
|
+
const lines = isPdf(buf) ? await pdfLines(full, buf) : null
|
|
48
65
|
if (!lines && isBinary(buf)) throw new Error(`${path} is a binary file`)
|
|
49
66
|
const source = lines ?? buf.toString('utf-8').split('\n')
|
|
50
67
|
|