Files
LLeMbas-CLI/src/project/attach.ts
T
HomerandClaude Opus 5.5 fbefe7f844
ci / check (push) Canceled after 0s
Harness spec 3.2.0 in the CLI: effort levels, reasoning round-trip, no absolute paths
Levels (models/schema.json levels): provider/effort.ts levelsOf/levelFields/
readLevel — `off` only with thinking_switch and sends enable_thinking false
and no effort, `default` for a model that takes no efforts and sends
nothing, a level not offered is read as the start level. The engine's
effort is a level; /effort shows nothing for one level, a Thinking switch
for [off, X], a list otherwise; the status bar follows. /v1/models'
thinking_switch, efforts [] and capacity.parallel are read
(lembas/webui.ts); session/capacity.ts counts parallel slots.

Reasoning (context.md §8): an assistant message of the current turn
carries reasoning_content (provider/common.ts turnReasoning,
openai-chat.ts); step-boundary deliveries, nudges and the wrap-up are
marked turn: false and do not start a turn.

Paths (context.md §11): the <env> block names the project and, started
below the root, that directory relative to it (assemble.ts envLines);
instruction files and @ attachments are named from the root or as ~/…;
file tools and bash resolve relative paths against the project root.

The team rules (team/team.ts) and conformance runners for levels,
reasoning, paths and team; the team runtime follows.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-09 08:36:55 +00:00

89 lines
4.1 KiB
TypeScript

// @references in a prompt: `@src/a.ts`, `@src/a.ts#10-20`, `@docs/`. Each one that exists is
// attached to the message the model gets — files line-numbered exactly as the read tool shows
// them, and counted as read so the model may edit them at once. The prompt text is unchanged.
import { readdirSync, readFileSync, statSync } from "node:fs"
import { join, resolve } from "node:path"
import { promptPath } from "../prompt/assemble.ts"
import type { ImagePart } from "../provider/types.ts"
import type { ToolContext } from "../tool/tool.ts"
import { isImage, loadImage } from "./image.ts"
// A space or a # in a name is written with a backslash before it (`@my\ notes.md`,
// `@a\#b.md`): an unescaped # starts a line range.
const REF = /(?:^|\s)@((?:\\[ #]|[^\s#])+)(?:#(\d+)(?:-(\d+))?)?/g
/** A path as an @ mention writes it: spaces and # escaped, so it reads back as the same path. */
export function escapeMention(path: string): string {
return path.replace(/([ #])/g, "\\$1")
}
const MAX_LINES = 2000
const MAX_BYTES = 200_000
export interface Attachment {
path: string
/** Text for the model — a file, a listing, or why an image was not attached. */
text: string
image?: ImagePart
}
/** `allow`: which paths may be read at all (over ACP: inside the session and the device's
* roots, nothing protected); a mention it refuses stays plain text, as one that does not exist. */
export function attachmentsFor(text: string, ctx: Pick<ToolContext, "root" | "cwd" | "readFiles" | "fileStamps">, vision = false, allow?: (abs: string) => boolean): Attachment[] {
const out: Attachment[] = []
const seen = new Set<string>()
for (const m of text.matchAll(REF)) {
const rel = m[1]!.replace(/\\([ #])/g, "$1").replace(/[.,;:!?)]+$/, "")
const abs = resolve(ctx.cwd, rel)
if (allow && !allow(abs)) continue
// Named to the model as its tools read paths: from the project root, ~/… under home (context.md §11).
const named = promptPath(ctx.root, abs) + (rel.endsWith("/") && abs !== "/" ? "/" : "")
const key = `${abs}#${m[2] ?? ""}-${m[3] ?? ""}`
if (seen.has(key)) continue
seen.add(key)
let st
try {
st = statSync(abs)
} catch {
continue // not a path: an email address, a handle — leave it as text
}
if (st.isDirectory()) {
const entries = readdirSync(abs, { withFileTypes: true })
.map((e) => (e.isDirectory() ? `${e.name}/` : e.name))
.sort()
out.push({ path: rel, text: `<directory path="${named}">\n${entries.join("\n")}\n</directory>` })
continue
}
if (isImage(abs)) {
if (!vision) {
out.push({ path: rel, text: `<image path="${named}">(not attached: this model has no vision)</image>` })
continue
}
const img = loadImage(abs)
out.push(typeof img === "string" ? { path: rel, text: `<image path="${named}">(not attached: ${img})</image>` } : { path: rel, text: `<image path="${named}"/>`, image: img })
continue
}
if (st.size > MAX_BYTES && !m[2]) {
out.push({ path: rel, text: `<file path="${named}">\n(${st.size} bytes — too large to attach whole; read it in parts, or reference a range like @${named}#1-200)\n</file>` })
continue
}
const raw = readFileSync(abs)
if (raw.subarray(0, 8192).includes(0)) {
out.push({ path: rel, text: `<file path="${named}">\n(binary file, ${st.size} bytes)\n</file>` })
continue
}
const lines = raw.toString("utf8").split("\n")
if (lines.length > 1 && lines[lines.length - 1] === "") lines.pop()
const from = Math.max(1, Number(m[2] ?? 1))
const to = Math.min(lines.length, Number(m[3] ?? (m[2] ? m[2] : from + MAX_LINES - 1)))
const body = lines.slice(from - 1, to).map((l, i) => `${from + i}: ${l}`).join("\n")
const range = m[2] ? ` lines="${from}-${to}"` : ""
const more = !m[2] && lines.length > to ? `\n(${lines.length - to} more lines — read with offset ${to + 1})` : ""
out.push({ path: rel, text: `<file path="${named}"${range}>\n${body}${more}\n</file>` })
if (!m[2]) {
ctx.readFiles.add(abs)
ctx.fileStamps.set(abs, st.mtimeMs)
}
}
return out
}