picocode-core 0.9.125 → 0.9.127

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.
Files changed (2) hide show
  1. package/package.json +3 -2
  2. package/src/tools/read.js +41 -6
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "picocode-core",
3
- "version": "0.9.125",
3
+ "version": "0.9.127",
4
4
  "description": "The agent runtime behind pico: sessions, tools, subagents, MCP, memory, and model access",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -28,6 +28,7 @@
28
28
  "@prsm/ai": "^1.6.1",
29
29
  "diff": "^7.0.0",
30
30
  "fast-glob": "^3.3.0",
31
- "proper-lockfile": "^4.1.2"
31
+ "proper-lockfile": "^4.1.2",
32
+ "unpdf": "^1.8.1"
32
33
  }
33
34
  }
package/src/tools/read.js CHANGED
@@ -1,10 +1,44 @@
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'
5
+ import { extractText } from 'unpdf'
4
6
 
5
7
  const MAX_LINES = 2000
6
8
  const MAX_LINE_LENGTH = 2000
7
9
 
10
+ const isPdf = (buffer) => buffer.subarray(0, 5).toString('latin1') === '%PDF-'
11
+
12
+ // a pdf reads as its extracted text, one page after another, so the same
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))
33
+ const lines = []
34
+ pages.forEach((page, i) => {
35
+ if (i) lines.push('')
36
+ lines.push(`[page ${i + 1} of ${pages.length}]`)
37
+ lines.push(...page)
38
+ })
39
+ return lines
40
+ }
41
+
8
42
  function isBinary(buffer) {
9
43
  const len = Math.min(buffer.length, 8000)
10
44
  for (let i = 0; i < len; i++) {
@@ -27,20 +61,21 @@ export function createRead({ cwd, recorder, tracker }) {
27
61
  const full = resolve(cwd, path)
28
62
  recorder.extra({ title: path })
29
63
  const buf = await readFile(full)
30
- if (isBinary(buf)) throw new Error(`${path} is a binary file`)
31
- const lines = buf.toString('utf-8').split('\n')
64
+ const lines = isPdf(buf) ? await pdfLines(full, buf) : null
65
+ if (!lines && isBinary(buf)) throw new Error(`${path} is a binary file`)
66
+ const source = lines ?? buf.toString('utf-8').split('\n')
32
67
 
33
68
  const start = Math.max(0, offset - 1)
34
69
  const count = Math.min(limit, MAX_LINES)
35
- const sliced = lines.slice(start, start + count)
70
+ const sliced = source.slice(start, start + count)
36
71
  const numbered = sliced
37
72
  .map((line, i) => `${start + i + 1}\t${line.length > MAX_LINE_LENGTH ? line.slice(0, MAX_LINE_LENGTH) + '…' : line}`)
38
73
  .join('\n')
39
74
 
40
75
  recorder.extra({ fullOutput: sliced.join('\n') })
41
- const result = { content: numbered, totalLines: lines.length }
42
- if (start + count < lines.length) {
43
- result.note = `showing lines ${start + 1}-${start + sliced.length} of ${lines.length}`
76
+ const result = { content: numbered, totalLines: source.length }
77
+ if (start + count < source.length) {
78
+ result.note = `showing lines ${start + 1}-${start + sliced.length} of ${source.length}`
44
79
  }
45
80
  const context = tracker.check(full)
46
81
  if (context.length) result.context_from_agents_md = context