@sixb/agent 0.2.3 → 0.2.5
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/README.md +3 -3
- package/dist/agent-prompt.d.ts.map +1 -1
- package/dist/artifacts/layout-check/layout-check.d.ts +21 -24
- package/dist/artifacts/layout-check/layout-check.d.ts.map +1 -1
- package/dist/artifacts/publish-tool.d.ts +26 -44
- package/dist/artifacts/publish-tool.d.ts.map +1 -1
- package/dist/ask-tool.d.ts +62 -0
- package/dist/ask-tool.d.ts.map +1 -0
- package/dist/attachments.d.ts +1 -1
- package/dist/attachments.d.ts.map +1 -1
- package/dist/background-commands.d.ts +3 -3
- package/dist/background-commands.d.ts.map +1 -1
- package/dist/background-notices.d.ts.map +1 -1
- package/dist/child-files.d.ts +1 -1
- package/dist/child-questions.d.ts +1 -1
- package/dist/index.d.ts +1 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +10457 -7971
- package/dist/index.js.map +46 -38
- package/dist/run-agent-turn.d.ts +3 -14
- package/dist/run-agent-turn.d.ts.map +1 -1
- package/dist/run-conversation-turn.d.ts.map +1 -1
- package/dist/run-environment.d.ts +4 -1
- package/dist/run-environment.d.ts.map +1 -1
- package/dist/run-subagent.d.ts +1 -1
- package/dist/run-subagent.d.ts.map +1 -1
- package/dist/sandbox-api-context.d.ts.map +1 -1
- package/dist/search-script.d.ts +7 -0
- package/dist/search-script.d.ts.map +1 -0
- package/dist/skills/built-in.d.ts.map +1 -1
- package/dist/subagent-tools.d.ts +20 -1
- package/dist/subagent-tools.d.ts.map +1 -1
- package/dist/tools/background.d.ts +50 -18
- package/dist/tools/background.d.ts.map +1 -1
- package/dist/tools/bash.d.ts +1 -0
- package/dist/tools/bash.d.ts.map +1 -1
- package/dist/tools/edit-text.d.ts +22 -0
- package/dist/tools/edit-text.d.ts.map +1 -0
- package/dist/tools/edit.d.ts +54 -0
- package/dist/tools/edit.d.ts.map +1 -0
- package/dist/tools/file-writes.d.ts +52 -0
- package/dist/tools/file-writes.d.ts.map +1 -0
- package/dist/tools/glob.d.ts +41 -0
- package/dist/tools/glob.d.ts.map +1 -0
- package/dist/tools/grep.d.ts +64 -0
- package/dist/tools/grep.d.ts.map +1 -0
- package/dist/tools/model-spec.d.ts.map +1 -1
- package/dist/tools/read.d.ts +16 -3
- package/dist/tools/read.d.ts.map +1 -1
- package/dist/tools/sandbox-search.d.ts +26 -0
- package/dist/tools/sandbox-search.d.ts.map +1 -0
- package/dist/tools/search/command.d.ts +15 -0
- package/dist/tools/search/command.d.ts.map +1 -0
- package/dist/tools/search/gitignore.d.ts +28 -0
- package/dist/tools/search/gitignore.d.ts.map +1 -0
- package/dist/tools/search/glob.d.ts +15 -0
- package/dist/tools/search/glob.d.ts.map +1 -0
- package/dist/tools/search/main.d.ts +2 -0
- package/dist/tools/search/main.d.ts.map +1 -0
- package/dist/tools/search/match.d.ts +30 -0
- package/dist/tools/search/match.d.ts.map +1 -0
- package/dist/tools/search/pattern.d.ts +19 -0
- package/dist/tools/search/pattern.d.ts.map +1 -0
- package/dist/tools/search/read.d.ts +62 -0
- package/dist/tools/search/read.d.ts.map +1 -0
- package/dist/tools/search/request.d.ts +80 -0
- package/dist/tools/search/request.d.ts.map +1 -0
- package/dist/tools/search/search.d.ts +4 -0
- package/dist/tools/search/search.d.ts.map +1 -0
- package/dist/tools/search/walk.d.ts +43 -0
- package/dist/tools/search/walk.d.ts.map +1 -0
- package/dist/tools/tool-outputs.d.ts.map +1 -1
- package/dist/tools/view-file.d.ts +20 -23
- package/dist/tools/view-file.d.ts.map +1 -1
- package/dist/tools/web-fetch/tool.d.ts +1 -0
- package/dist/tools/web-fetch/tool.d.ts.map +1 -1
- package/dist/tools/web-search.d.ts +1 -0
- package/dist/tools/web-search.d.ts.map +1 -1
- package/dist/tools/write.d.ts +36 -0
- package/dist/tools/write.d.ts.map +1 -0
- package/dist/turn-inbox.d.ts +75 -0
- package/dist/turn-inbox.d.ts.map +1 -0
- package/package.json +6 -4
- package/src/agent-prompt.ts +10 -9
- package/src/agent-runtime/preflight.ts +1 -0
- package/src/artifacts/layout-check/check.cjs +176 -95
- package/src/artifacts/layout-check/layout-check.ts +118 -76
- package/src/artifacts/publish-tool.ts +123 -192
- package/src/ask-tool.ts +223 -0
- package/src/attachments.ts +7 -6
- package/src/background-commands.ts +5 -5
- package/src/background-notices.ts +5 -12
- package/src/child-files.ts +1 -1
- package/src/child-questions.ts +1 -1
- package/src/context-compaction.ts +4 -1
- package/src/generated/search.mjs +985 -0
- package/src/index.ts +1 -2
- package/src/run-agent-turn.ts +99 -133
- package/src/run-child.ts +1 -1
- package/src/run-conversation-turn.ts +8 -5
- package/src/run-environment.ts +40 -21
- package/src/run-subagent.ts +4 -4
- package/src/sandbox-api-context.ts +3 -0
- package/src/search-script-assets.d.ts +5 -0
- package/src/search-script.ts +9 -0
- package/src/skills/built-in.ts +16 -0
- package/src/skills/sixb-artifacts/SKILL.md +13 -13
- package/src/skills/sixb-artifacts/canvas.md +5 -4
- package/src/skills/sixb-artifacts/document.md +92 -5
- package/src/skills/sixb-artifacts/examples/document/service-proposal/design.css +114 -0
- package/src/skills/sixb-artifacts/examples/document/service-proposal/document.html +159 -0
- package/src/skills/sixb-artifacts/examples/document/service-proposal/images/fieldwise.svg +1 -0
- package/src/skills/sixb-artifacts/examples/document/service-proposal/images/harbor-pine.svg +1 -0
- package/src/skills/sixb-artifacts/page.md +2 -2
- package/src/skills/sixb-artifacts/queries.md +2 -1
- package/src/skills/sixb-artifacts/slides.md +2 -2
- package/src/subagent-tools.ts +115 -81
- package/src/tools/background.ts +227 -67
- package/src/tools/bash.ts +2 -1
- package/src/tools/edit-text.ts +241 -0
- package/src/tools/edit.ts +166 -0
- package/src/tools/file-writes.ts +270 -0
- package/src/tools/glob.ts +124 -0
- package/src/tools/grep.ts +190 -0
- package/src/tools/model-spec.ts +13 -13
- package/src/tools/read.ts +75 -9
- package/src/tools/sandbox-search.ts +219 -0
- package/src/tools/search/command.ts +116 -0
- package/src/tools/search/gitignore.ts +101 -0
- package/src/tools/search/glob.ts +90 -0
- package/src/tools/search/main.ts +9 -0
- package/src/tools/search/match.ts +278 -0
- package/src/tools/search/pattern.ts +178 -0
- package/src/tools/search/read.ts +237 -0
- package/src/tools/search/request.ts +101 -0
- package/src/tools/search/search.ts +297 -0
- package/src/tools/search/walk.ts +145 -0
- package/src/tools/tool-outputs.ts +9 -4
- package/src/tools/view-file.ts +78 -80
- package/src/tools/web-fetch/tool.ts +2 -1
- package/src/tools/web-search.ts +1 -0
- package/src/tools/write.ts +95 -0
- package/src/turn-inbox.ts +283 -0
- package/dist/artifacts/board-previews.d.ts +0 -41
- package/dist/artifacts/board-previews.d.ts.map +0 -1
- package/dist/ask-parent-tool.d.ts +0 -36
- package/dist/ask-parent-tool.d.ts.map +0 -1
- package/src/artifacts/board-previews.ts +0 -218
- package/src/ask-parent-tool.ts +0 -100
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import { posix } from "node:path"
|
|
2
|
+
import type { JsonObject, JsonValue, ModelTool } from "@sixb/core/models"
|
|
3
|
+
import { agentToolErrorText } from "../model-adapters"
|
|
4
|
+
import type { AgentSandboxHandle } from "../sandbox-handle"
|
|
5
|
+
import { editText } from "./edit-text"
|
|
6
|
+
import {
|
|
7
|
+
commitSandboxFile,
|
|
8
|
+
type FileReadLedger,
|
|
9
|
+
type LoadedSandboxFile,
|
|
10
|
+
loadSandboxFile,
|
|
11
|
+
publicError,
|
|
12
|
+
} from "./file-writes"
|
|
13
|
+
|
|
14
|
+
export const EDIT_TOOL_SPEC = {
|
|
15
|
+
name: "edit",
|
|
16
|
+
strict: true,
|
|
17
|
+
description:
|
|
18
|
+
"Replace text in a file. old_string must match the file exactly, once, unless replace_all is true. Read the file first: an edit refuses a file that changed since you read it. Returns the change as a diff, so there is no need to read the file again to check it.",
|
|
19
|
+
inputSchema: {
|
|
20
|
+
type: "object",
|
|
21
|
+
properties: {
|
|
22
|
+
path: {
|
|
23
|
+
type: "string",
|
|
24
|
+
description:
|
|
25
|
+
"Path relative to the working directory, or an absolute path in the run's context directory.",
|
|
26
|
+
},
|
|
27
|
+
old_string: { type: "string", description: "The exact text to replace." },
|
|
28
|
+
new_string: { type: "string", description: "The text to put in its place." },
|
|
29
|
+
replace_all: {
|
|
30
|
+
type: "boolean",
|
|
31
|
+
description: "Replace every match of old_string. Default: false.",
|
|
32
|
+
},
|
|
33
|
+
},
|
|
34
|
+
required: ["path", "old_string", "new_string"],
|
|
35
|
+
additionalProperties: false,
|
|
36
|
+
} satisfies JsonObject,
|
|
37
|
+
} as const
|
|
38
|
+
|
|
39
|
+
export interface EditToolInput {
|
|
40
|
+
readonly path: string
|
|
41
|
+
readonly old_string: string
|
|
42
|
+
readonly new_string: string
|
|
43
|
+
readonly replace_all?: boolean
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export interface EditToolOutput {
|
|
47
|
+
readonly path: string
|
|
48
|
+
readonly replaced: number
|
|
49
|
+
/** Unified diff of the change, with line numbers; cut past a few thousand characters. */
|
|
50
|
+
readonly diff: string
|
|
51
|
+
/** Lines the change adds and removes, all of them, however short the diff was cut. */
|
|
52
|
+
readonly added: number
|
|
53
|
+
readonly removed: number
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function createEditTool(
|
|
57
|
+
resolveSandbox: () => Promise<AgentSandboxHandle>,
|
|
58
|
+
ledger: FileReadLedger
|
|
59
|
+
): ModelTool<EditToolInput> {
|
|
60
|
+
return {
|
|
61
|
+
...EDIT_TOOL_SPEC,
|
|
62
|
+
parseInput: parseEditInput,
|
|
63
|
+
async execute(input, { signal }): Promise<JsonValue> {
|
|
64
|
+
const handle = await resolveSandbox()
|
|
65
|
+
const loaded = await loadSandboxFile(handle, { path: input.path, tool: "edit", signal })
|
|
66
|
+
if (loaded.kind === "missing") {
|
|
67
|
+
throw publicError("edit", `'${input.path}' does not exist. Create it with write.`)
|
|
68
|
+
}
|
|
69
|
+
assertReadAsIs(ledger, loaded, input.path, "edit")
|
|
70
|
+
|
|
71
|
+
let text: string
|
|
72
|
+
try {
|
|
73
|
+
text = new TextDecoder("utf-8", { fatal: true }).decode(loaded.bytes)
|
|
74
|
+
} catch {
|
|
75
|
+
throw publicError("edit", `'${input.path}' is not UTF-8 text.`)
|
|
76
|
+
}
|
|
77
|
+
const edited = editText(text, {
|
|
78
|
+
old: input.old_string,
|
|
79
|
+
new: input.new_string,
|
|
80
|
+
all: input.replace_all === true,
|
|
81
|
+
})
|
|
82
|
+
if (!edited.ok) throw publicError("edit", edited.reason)
|
|
83
|
+
|
|
84
|
+
const written = await commitSandboxFile(handle, {
|
|
85
|
+
path: loaded.realPath,
|
|
86
|
+
expected: loaded.fingerprint,
|
|
87
|
+
bytes: new TextEncoder().encode(edited.text),
|
|
88
|
+
tool: "edit",
|
|
89
|
+
signal,
|
|
90
|
+
})
|
|
91
|
+
ledger.record(written.realPath, written.fingerprint)
|
|
92
|
+
return {
|
|
93
|
+
path: input.path,
|
|
94
|
+
replaced: edited.replaced,
|
|
95
|
+
diff: edited.diff,
|
|
96
|
+
added: edited.added,
|
|
97
|
+
removed: edited.removed,
|
|
98
|
+
} satisfies EditToolOutput
|
|
99
|
+
},
|
|
100
|
+
errorText: agentToolErrorText,
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** An existing file is changed only as the run last read or wrote it. */
|
|
105
|
+
export function assertReadAsIs(
|
|
106
|
+
ledger: FileReadLedger,
|
|
107
|
+
loaded: Extract<LoadedSandboxFile, { kind: "file" }>,
|
|
108
|
+
path: string,
|
|
109
|
+
tool: string
|
|
110
|
+
): void {
|
|
111
|
+
const seen = ledger.get(loaded.realPath)
|
|
112
|
+
if (seen === undefined) {
|
|
113
|
+
throw publicError(tool, `read '${path}' with the read tool before changing it.`)
|
|
114
|
+
}
|
|
115
|
+
if (seen !== loaded.fingerprint) {
|
|
116
|
+
throw publicError(
|
|
117
|
+
tool,
|
|
118
|
+
`'${path}' changed since you read it. Read it again, then make your change.`
|
|
119
|
+
)
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function parseEditInput(value: unknown): EditToolInput {
|
|
124
|
+
const input = objectInput(value, "edit", ["path", "old_string", "new_string", "replace_all"])
|
|
125
|
+
const path = pathInput(input.path, "edit")
|
|
126
|
+
if (typeof input.old_string !== "string" || input.old_string === "") {
|
|
127
|
+
throw publicError("edit", "old_string must be the non-empty text to replace.")
|
|
128
|
+
}
|
|
129
|
+
if (typeof input.new_string !== "string") {
|
|
130
|
+
throw publicError("edit", "new_string must be a string.")
|
|
131
|
+
}
|
|
132
|
+
if (input.old_string === input.new_string) {
|
|
133
|
+
throw publicError("edit", "new_string is the same as old_string: there is nothing to change.")
|
|
134
|
+
}
|
|
135
|
+
if (input.replace_all !== undefined && typeof input.replace_all !== "boolean") {
|
|
136
|
+
throw publicError("edit", "replace_all must be true or false.")
|
|
137
|
+
}
|
|
138
|
+
return {
|
|
139
|
+
path,
|
|
140
|
+
old_string: input.old_string,
|
|
141
|
+
new_string: input.new_string,
|
|
142
|
+
...(input.replace_all === undefined ? {} : { replace_all: input.replace_all }),
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export function objectInput(
|
|
147
|
+
value: unknown,
|
|
148
|
+
tool: string,
|
|
149
|
+
keys: readonly string[]
|
|
150
|
+
): Record<string, unknown> {
|
|
151
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
152
|
+
throw publicError(tool, "input must be an object.")
|
|
153
|
+
}
|
|
154
|
+
const input = value as Record<string, unknown>
|
|
155
|
+
for (const key of Object.keys(input)) {
|
|
156
|
+
if (!keys.includes(key)) throw publicError(tool, `input has an unknown property '${key}'.`)
|
|
157
|
+
}
|
|
158
|
+
return input
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
export function pathInput(value: unknown, tool: string): string {
|
|
162
|
+
if (typeof value !== "string" || !value.trim() || value.includes("\0")) {
|
|
163
|
+
throw publicError(tool, "path must be a non-empty path.")
|
|
164
|
+
}
|
|
165
|
+
return posix.normalize(value)
|
|
166
|
+
}
|
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
import { AgentToolPublicError, type CommandResult } from "@sixb/core"
|
|
2
|
+
import { AgentToolExecutionError } from "../errors"
|
|
3
|
+
import { type AgentSandboxHandle, contextPath } from "../sandbox-handle"
|
|
4
|
+
|
|
5
|
+
/** The largest file `edit` changes and `write` writes: a whole file passes through the harness. */
|
|
6
|
+
export const MAX_EDITED_FILE_BYTES = 1024 * 1024
|
|
7
|
+
|
|
8
|
+
const COMMAND_TIMEOUT_MS = 30_000
|
|
9
|
+
/**
|
|
10
|
+
* Contents travel as base64 in an environment variable, a few calls for a large file: Linux caps
|
|
11
|
+
* one variable at 128 KiB, and a sandbox has no stdin or file API that reaches every root.
|
|
12
|
+
*/
|
|
13
|
+
const CHUNK_CHARACTERS = 96 * 1024
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* What a run has read, by real path: the file's fingerprint (`cksum`: CRC and size) when it read
|
|
17
|
+
* it. `edit` and `write` change an existing file only when it is the file the run read, so a change
|
|
18
|
+
* made since, by the user, another process or the run's own `bash`, is never overwritten blind.
|
|
19
|
+
*/
|
|
20
|
+
export class FileReadLedger {
|
|
21
|
+
readonly #fingerprints = new Map<string, string>()
|
|
22
|
+
|
|
23
|
+
record(realPath: string, fingerprint: string): void {
|
|
24
|
+
this.#fingerprints.set(realPath, fingerprint)
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
get(realPath: string): string | undefined {
|
|
28
|
+
return this.#fingerprints.get(realPath)
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export type LoadedSandboxFile =
|
|
33
|
+
| { readonly kind: "missing" }
|
|
34
|
+
| {
|
|
35
|
+
readonly kind: "file"
|
|
36
|
+
readonly realPath: string
|
|
37
|
+
readonly fingerprint: string
|
|
38
|
+
readonly bytes: Uint8Array
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Read a whole file for `edit` or `write`: where it really is, its fingerprint and its bytes. */
|
|
42
|
+
export async function loadSandboxFile(
|
|
43
|
+
handle: AgentSandboxHandle,
|
|
44
|
+
input: { readonly path: string; readonly tool: string; readonly signal: AbortSignal }
|
|
45
|
+
): Promise<LoadedSandboxFile> {
|
|
46
|
+
const result = await run(handle, input.tool, input.signal, LOAD_SCRIPT, [
|
|
47
|
+
input.path,
|
|
48
|
+
contextPath(handle),
|
|
49
|
+
String(MAX_EDITED_FILE_BYTES),
|
|
50
|
+
])
|
|
51
|
+
if (result.exitCode === 10) return { kind: "missing" }
|
|
52
|
+
if (result.exitCode !== 0) throw pathError(input.tool, input.path, result)
|
|
53
|
+
|
|
54
|
+
const [fingerprint = "", realPath = "", ...contents] = result.stdout.split("\n")
|
|
55
|
+
const bytes = Buffer.from(contents.join(""), "base64")
|
|
56
|
+
if (!isFingerprint(fingerprint) || bytes.byteLength !== Number(fingerprint.split(":")[1])) {
|
|
57
|
+
throw publicError(input.tool, `'${input.path}' changed while it was being read; try again.`)
|
|
58
|
+
}
|
|
59
|
+
return {
|
|
60
|
+
kind: "file",
|
|
61
|
+
realPath: realPath || input.path,
|
|
62
|
+
fingerprint,
|
|
63
|
+
bytes: new Uint8Array(bytes.buffer, bytes.byteOffset, bytes.byteLength),
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Write `bytes` to `path` when the file there is still `expected` (a fingerprint, or `missing`
|
|
69
|
+
* for a file to create), keeping an existing file's mode. Missing parent directories are created
|
|
70
|
+
* inside the run's roots. Returns where the file is and its new fingerprint.
|
|
71
|
+
*/
|
|
72
|
+
export async function commitSandboxFile(
|
|
73
|
+
handle: AgentSandboxHandle,
|
|
74
|
+
input: {
|
|
75
|
+
readonly path: string
|
|
76
|
+
readonly expected: string
|
|
77
|
+
readonly bytes: Uint8Array
|
|
78
|
+
readonly tool: string
|
|
79
|
+
readonly signal: AbortSignal
|
|
80
|
+
}
|
|
81
|
+
): Promise<{ readonly realPath: string; readonly fingerprint: string }> {
|
|
82
|
+
const encoded = Buffer.from(input.bytes).toString("base64")
|
|
83
|
+
let stage = ""
|
|
84
|
+
let offset = 0
|
|
85
|
+
for (; offset + CHUNK_CHARACTERS < encoded.length; offset += CHUNK_CHARACTERS) {
|
|
86
|
+
const result = await run(
|
|
87
|
+
handle,
|
|
88
|
+
input.tool,
|
|
89
|
+
input.signal,
|
|
90
|
+
STAGE_SCRIPT,
|
|
91
|
+
[stage],
|
|
92
|
+
encoded.slice(offset, offset + CHUNK_CHARACTERS)
|
|
93
|
+
)
|
|
94
|
+
if (result.exitCode !== 0 || !result.stdout.trim()) {
|
|
95
|
+
throw new AgentToolExecutionError(input.tool, {
|
|
96
|
+
cause: new Error(result.stderr.trim() || `Staging exited with code ${result.exitCode}.`),
|
|
97
|
+
})
|
|
98
|
+
}
|
|
99
|
+
stage = result.stdout.trim()
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const result = await run(
|
|
103
|
+
handle,
|
|
104
|
+
input.tool,
|
|
105
|
+
input.signal,
|
|
106
|
+
COMMIT_SCRIPT,
|
|
107
|
+
[input.path, contextPath(handle), input.expected, stage],
|
|
108
|
+
encoded.slice(offset)
|
|
109
|
+
)
|
|
110
|
+
if (result.exitCode === 15) {
|
|
111
|
+
throw publicError(
|
|
112
|
+
input.tool,
|
|
113
|
+
result.stdout.trim() === "missing"
|
|
114
|
+
? `'${input.path}' was deleted after you read it.`
|
|
115
|
+
: `'${input.path}' changed after you read it. Read it again, then make your change.`
|
|
116
|
+
)
|
|
117
|
+
}
|
|
118
|
+
if (result.exitCode !== 0) throw pathError(input.tool, input.path, result)
|
|
119
|
+
|
|
120
|
+
const [fingerprint = "", realPath = ""] = result.stdout.split("\n")
|
|
121
|
+
if (!isFingerprint(fingerprint)) {
|
|
122
|
+
throw new AgentToolExecutionError(input.tool, {
|
|
123
|
+
cause: new Error(`Unexpected output: ${result.stdout.slice(0, 200)}`),
|
|
124
|
+
})
|
|
125
|
+
}
|
|
126
|
+
return { realPath: realPath || input.path, fingerprint }
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export function publicError(tool: string, message: string): AgentToolPublicError {
|
|
130
|
+
return new AgentToolPublicError(`[SixbAgent] ${tool}: ${message}`)
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
async function run(
|
|
134
|
+
handle: AgentSandboxHandle,
|
|
135
|
+
tool: string,
|
|
136
|
+
signal: AbortSignal,
|
|
137
|
+
script: string,
|
|
138
|
+
args: readonly string[],
|
|
139
|
+
chunk?: string
|
|
140
|
+
): Promise<CommandResult> {
|
|
141
|
+
let result: CommandResult
|
|
142
|
+
try {
|
|
143
|
+
result = await handle.sandbox.runCommand("bash", ["-c", script, `sixb-${tool}`, ...args], {
|
|
144
|
+
cwd: handle.sandbox.workingDirectory,
|
|
145
|
+
env: { ...(handle.env ?? {}), ...(chunk === undefined ? {} : { SIXB_CHUNK: chunk }) },
|
|
146
|
+
timeout: COMMAND_TIMEOUT_MS,
|
|
147
|
+
signal,
|
|
148
|
+
})
|
|
149
|
+
} catch (error) {
|
|
150
|
+
signal.throwIfAborted()
|
|
151
|
+
throw new AgentToolExecutionError(tool, { cause: error })
|
|
152
|
+
}
|
|
153
|
+
signal.throwIfAborted()
|
|
154
|
+
return result
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function pathError(tool: string, path: string, result: CommandResult): Error {
|
|
158
|
+
switch (result.exitCode) {
|
|
159
|
+
case 10:
|
|
160
|
+
return publicError(tool, `'${path}' does not exist.`)
|
|
161
|
+
case 11:
|
|
162
|
+
return publicError(
|
|
163
|
+
tool,
|
|
164
|
+
`'${path}' resolves outside the working directory and the run's context directory.`
|
|
165
|
+
)
|
|
166
|
+
case 12:
|
|
167
|
+
return publicError(tool, `'${path}' is a directory, not a file.`)
|
|
168
|
+
case 13:
|
|
169
|
+
return publicError(tool, `'${path}' is not a regular file.`)
|
|
170
|
+
case 14:
|
|
171
|
+
return publicError(
|
|
172
|
+
tool,
|
|
173
|
+
`'${path}' is larger than ${MAX_EDITED_FILE_BYTES / 1024 / 1024} MiB: change it with bash.`
|
|
174
|
+
)
|
|
175
|
+
default:
|
|
176
|
+
return new AgentToolExecutionError(tool, {
|
|
177
|
+
cause: new Error(
|
|
178
|
+
result.stderr.trim() || `Sandbox command exited with code ${result.exitCode}.`
|
|
179
|
+
),
|
|
180
|
+
})
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
export function isFingerprint(value: string): boolean {
|
|
185
|
+
return /^\d+:\d+$/.test(value)
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Bash functions shared with `read`: a file's fingerprint as `crc:size` from `cksum`, and a real
|
|
190
|
+
* path on one line (empty when the path holds a newline, which then goes unrecorded). Neither
|
|
191
|
+
* needs `tr` or `awk`, which some sandbox images lack.
|
|
192
|
+
*/
|
|
193
|
+
export const FINGERPRINT_FUNCTIONS = `fingerprint() {
|
|
194
|
+
local sum rest
|
|
195
|
+
sum="$(cksum < "$1")" || return 1
|
|
196
|
+
rest="\${sum#* }"
|
|
197
|
+
printf '%s:%s\\n' "\${sum%% *}" "\${rest%% *}"
|
|
198
|
+
}
|
|
199
|
+
print_path() {
|
|
200
|
+
case "$1" in *$'\\n'*) printf '\\n' ;; *) printf '%s\\n' "$1" ;; esac
|
|
201
|
+
}`
|
|
202
|
+
|
|
203
|
+
// The run's roots, and the requested path made absolute.
|
|
204
|
+
const ROOTS = `root="$(pwd -P)"
|
|
205
|
+
context="$root"
|
|
206
|
+
if [ -d "$2" ]; then context="$(cd -P -- "$2" && pwd)"; fi
|
|
207
|
+
inside() { case "$1" in "$root"/*|"$context"/*) return 0 ;; esac; return 1; }
|
|
208
|
+
case "$1" in /*) requested="$1" ;; *) requested="$root/$1" ;; esac
|
|
209
|
+
${FINGERPRINT_FUNCTIONS}`
|
|
210
|
+
|
|
211
|
+
// Prints the fingerprint, the real path, then the contents as base64 over as many lines as it takes.
|
|
212
|
+
const LOAD_SCRIPT = `set -u
|
|
213
|
+
${ROOTS}
|
|
214
|
+
[ -e "$requested" ] || exit 10
|
|
215
|
+
target="$(realpath "$requested")" || exit 10
|
|
216
|
+
inside "$target" || exit 11
|
|
217
|
+
[ ! -d "$target" ] || exit 12
|
|
218
|
+
[ -f "$target" ] || exit 13
|
|
219
|
+
sum="$(fingerprint "$target")" || exit 19
|
|
220
|
+
[ "\${sum#*:}" -le "$3" ] || exit 14
|
|
221
|
+
printf '%s\\n' "$sum"
|
|
222
|
+
print_path "$target"
|
|
223
|
+
base64 < "$target"`
|
|
224
|
+
|
|
225
|
+
// Appends one chunk of base64 to a staging file, made on the first call; prints its path.
|
|
226
|
+
const STAGE_SCRIPT = `set -eu
|
|
227
|
+
stage="\${1:-}"
|
|
228
|
+
if [ -z "$stage" ]; then stage="$(mktemp "\${TMPDIR:-/tmp}/sixb-write.XXXXXX")"; fi
|
|
229
|
+
printf '%s' "$SIXB_CHUNK" >> "$stage"
|
|
230
|
+
printf '%s' "$stage"`
|
|
231
|
+
|
|
232
|
+
// Writes when the file is still what the run expects; exit 15 prints what it is instead. Decodes
|
|
233
|
+
// in full before touching the file, and writes through it (cat >) so its mode and links stay.
|
|
234
|
+
const COMMIT_SCRIPT = `set -u
|
|
235
|
+
${ROOTS}
|
|
236
|
+
if [ -L "$requested" ] && [ ! -e "$requested" ]; then exit 11; fi
|
|
237
|
+
if [ -e "$requested" ]; then
|
|
238
|
+
target="$(realpath "$requested")" || exit 19
|
|
239
|
+
inside "$target" || exit 11
|
|
240
|
+
[ ! -d "$target" ] || exit 12
|
|
241
|
+
[ -f "$target" ] || exit 13
|
|
242
|
+
current="$(fingerprint "$target")" || exit 19
|
|
243
|
+
else
|
|
244
|
+
dir="$(dirname -- "$requested")"
|
|
245
|
+
while [ ! -d "$dir" ]; do dir="$(dirname -- "$dir")"; done
|
|
246
|
+
inside "$(cd -P -- "$dir" && pwd)/" || exit 11
|
|
247
|
+
mkdir -p -- "$(dirname -- "$requested")" || exit 19
|
|
248
|
+
parent="$(cd -P -- "$(dirname -- "$requested")" && pwd)"
|
|
249
|
+
inside "$parent/" || exit 11
|
|
250
|
+
target="$parent/$(basename -- "$requested")"
|
|
251
|
+
current=missing
|
|
252
|
+
fi
|
|
253
|
+
stage="\${4:-}"
|
|
254
|
+
if [ "$current" != "$3" ]; then
|
|
255
|
+
[ -z "$stage" ] || rm -f "$stage"
|
|
256
|
+
printf '%s' "$current"
|
|
257
|
+
exit 15
|
|
258
|
+
fi
|
|
259
|
+
decoded="$(mktemp "\${TMPDIR:-/tmp}/sixb-write.XXXXXX")" || exit 19
|
|
260
|
+
if [ -n "$stage" ]; then
|
|
261
|
+
printf '%s' "\${SIXB_CHUNK:-}" >> "$stage"
|
|
262
|
+
base64 -d < "$stage" > "$decoded" || { rm -f "$stage" "$decoded"; exit 19; }
|
|
263
|
+
rm -f "$stage"
|
|
264
|
+
else
|
|
265
|
+
printf '%s' "\${SIXB_CHUNK:-}" | base64 -d > "$decoded" || { rm -f "$decoded"; exit 19; }
|
|
266
|
+
fi
|
|
267
|
+
cat "$decoded" > "$target" || { rm -f "$decoded"; exit 19; }
|
|
268
|
+
rm -f "$decoded"
|
|
269
|
+
fingerprint "$target"
|
|
270
|
+
print_path "$target"`
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import type { JsonObject, JsonValue, ModelTool } from "@sixb/core/models"
|
|
2
|
+
import { agentToolErrorText } from "../model-adapters"
|
|
3
|
+
import type { AgentSandboxHandle } from "../sandbox-handle"
|
|
4
|
+
import { objectInput } from "./edit"
|
|
5
|
+
import { publicError } from "./file-writes"
|
|
6
|
+
import {
|
|
7
|
+
INCOMPLETE_SEARCH_FOOTER,
|
|
8
|
+
isRecord,
|
|
9
|
+
optionalPathInput,
|
|
10
|
+
runSandboxSearch,
|
|
11
|
+
searchUnavailable,
|
|
12
|
+
} from "./sandbox-search"
|
|
13
|
+
import type { GlobSearchResult } from "./search/request"
|
|
14
|
+
|
|
15
|
+
export const GLOB_TOOL_SPEC = {
|
|
16
|
+
name: "glob",
|
|
17
|
+
strict: true,
|
|
18
|
+
description:
|
|
19
|
+
"Find files by name. A pattern without / matches at any depth (*.md finds every Markdown file); one with / matches from path (src/**/*.ts). Supports *, **, ?, [abc] and {a,b}. Skips .git and what .gitignore excludes, and hidden files and node_modules unless the pattern names them. Returns at most 100 paths, sorted. Prefer this over bash to find files.",
|
|
20
|
+
inputSchema: {
|
|
21
|
+
type: "object",
|
|
22
|
+
properties: {
|
|
23
|
+
pattern: { type: "string", description: "Glob pattern, relative to path." },
|
|
24
|
+
path: {
|
|
25
|
+
type: "string",
|
|
26
|
+
description:
|
|
27
|
+
"Folder to search: relative to the working directory, or an absolute path in the run's context directory. Default: the working directory.",
|
|
28
|
+
},
|
|
29
|
+
},
|
|
30
|
+
required: ["pattern"],
|
|
31
|
+
additionalProperties: false,
|
|
32
|
+
} satisfies JsonObject,
|
|
33
|
+
} as const
|
|
34
|
+
|
|
35
|
+
export interface GlobToolInput {
|
|
36
|
+
readonly pattern: string
|
|
37
|
+
readonly path?: string
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface GlobToolOutput {
|
|
41
|
+
readonly pattern: string
|
|
42
|
+
/** As the call named it; `.` for the working directory. */
|
|
43
|
+
readonly path: string
|
|
44
|
+
/** Relative to the working directory when under it, absolute otherwise. */
|
|
45
|
+
readonly files: readonly string[]
|
|
46
|
+
/** More files match than the 100 listed. */
|
|
47
|
+
readonly truncated: boolean
|
|
48
|
+
/** The search stopped at its deadline. */
|
|
49
|
+
readonly incomplete?: true
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function createGlobTool(
|
|
53
|
+
resolveSandbox: () => Promise<AgentSandboxHandle>
|
|
54
|
+
): ModelTool<GlobToolInput> {
|
|
55
|
+
return {
|
|
56
|
+
...GLOB_TOOL_SPEC,
|
|
57
|
+
parseInput: parseGlobInput,
|
|
58
|
+
async execute(input, { signal }): Promise<JsonValue> {
|
|
59
|
+
const handle = await resolveSandbox()
|
|
60
|
+
const result = await runSandboxSearch(handle, { tool: "glob", ...input }, signal)
|
|
61
|
+
if (!isGlobResult(result)) {
|
|
62
|
+
throw searchUnavailable(
|
|
63
|
+
"glob",
|
|
64
|
+
"its output could not be read",
|
|
65
|
+
new Error("Invalid sandbox output.")
|
|
66
|
+
)
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return {
|
|
70
|
+
pattern: input.pattern,
|
|
71
|
+
path: input.path ?? ".",
|
|
72
|
+
files: result.files,
|
|
73
|
+
truncated: result.truncated,
|
|
74
|
+
...(result.incomplete ? { incomplete: true } : {}),
|
|
75
|
+
} satisfies GlobToolOutput
|
|
76
|
+
},
|
|
77
|
+
toModelOutput(output) {
|
|
78
|
+
return { type: "text", value: globModelText(output as unknown as GlobToolOutput) }
|
|
79
|
+
},
|
|
80
|
+
errorText: agentToolErrorText,
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** One path per line, then what the list leaves out. */
|
|
85
|
+
export function globModelText(output: GlobToolOutput): string {
|
|
86
|
+
const footers = [
|
|
87
|
+
...(output.truncated
|
|
88
|
+
? [`(${output.files.length} files shown, more match: narrow the pattern or the path)`]
|
|
89
|
+
: []),
|
|
90
|
+
...(output.incomplete ? [INCOMPLETE_SEARCH_FOOTER] : []),
|
|
91
|
+
]
|
|
92
|
+
const list = output.files.length > 0 ? output.files.join("\n") : "No files found"
|
|
93
|
+
return [list, ...footers].join("\n\n")
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function parseGlobInput(value: unknown): GlobToolInput {
|
|
97
|
+
const input = objectInput(value, "glob", ["pattern", "path"])
|
|
98
|
+
if (typeof input.pattern !== "string" || input.pattern === "") {
|
|
99
|
+
throw publicError("glob", "pattern must be a non-empty glob pattern.")
|
|
100
|
+
}
|
|
101
|
+
// Either would find nothing, and the model would read that nothing matches.
|
|
102
|
+
if (input.pattern.startsWith("/")) {
|
|
103
|
+
throw publicError(
|
|
104
|
+
"glob",
|
|
105
|
+
"pattern is relative to path: pass the folder as path and the rest as pattern."
|
|
106
|
+
)
|
|
107
|
+
}
|
|
108
|
+
if (input.pattern.endsWith("/")) {
|
|
109
|
+
throw publicError("glob", "glob finds files: drop the trailing /, or read the folder.")
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const path = optionalPathInput(input.path, "glob")
|
|
113
|
+
return { pattern: input.pattern, ...(path === undefined ? {} : { path }) }
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function isGlobResult(value: unknown): value is GlobSearchResult {
|
|
117
|
+
return (
|
|
118
|
+
isRecord(value) &&
|
|
119
|
+
Array.isArray(value.files) &&
|
|
120
|
+
value.files.every((file) => typeof file === "string") &&
|
|
121
|
+
typeof value.truncated === "boolean" &&
|
|
122
|
+
(value.incomplete === undefined || value.incomplete === true)
|
|
123
|
+
)
|
|
124
|
+
}
|