Skip to content

Open source CapCut alternative.

TypeScript

feat(app): add files.write, multi-line shell_exec, and login-shell PATH

David Led3e553c387d2
diff --git a/apps/Donkey/Sources/DonkeyHarness/DonkeyCommandLayer.swift b/apps/Donkey/Sources/DonkeyHarness/DonkeyCommandLayer.swift
index ede31cc6..bfcece18 100644
--- a/apps/Donkey/Sources/DonkeyHarness/DonkeyCommandLayer.swift
+++ b/apps/Donkey/Sources/DonkeyHarness/DonkeyCommandLayer.swift
@@ -31,9 +31,9 @@ public enum DonkeyCommandLayer {
HarnessToolDescriptor(
name: Command.shellExec.rawValue,
pluginID: pluginID,
- summary: "Run a single-line shell command on the user's Mac and return its output. This is your primary, expert tool: prefer it for finding files (`mdfind`, `ls -t`, `find`), launching or quitting apps (`open -a \"App Name\"`, `osascript -e 'quit app \"App Name\"'`), reading state (`date`, `pmset -g batt`, `system_profiler`), and changing settings (`defaults write`, `networksetup -set…`). Read-only commands run immediately; anything that changes state asks the user for one-time or always-allow consent first, so you may freely propose it. Destructive or privileged commands (`sudo`, `rm`, `dd`, piping into a shell) ask every time.",
+ summary: "Run a shell command on the user's Mac and return its output. This is your primary, expert tool: prefer it for finding files (`mdfind`, `ls -t`, `find`), launching or quitting apps (`open -a \"App Name\"`, `osascript -e 'quit app \"App Name\"'`), reading state (`date`, `pmset -g batt`, `system_profiler`), and changing settings (`defaults write`, `networksetup -set…`). Multi-line commands are fine; it runs from the user's home directory, so use ABSOLUTE paths when reading or writing files. To write file content (a subtitle file, a script, a note), prefer files.write over a `cat`/`echo` heredoc. Read-only commands run immediately; anything that changes state asks the user for one-time or always-allow consent first, so you may freely propose it. Destructive or privileged commands (`sudo`, `rm`, `dd`, piping into a shell) ask every time.",
inputSchema: [
- "command": "A safe, single-line shell command, e.g. `open -a \"App Name\"`, `osascript -e '…'`, or `date`.",
+ "command": "A shell command, e.g. `open -a \"App Name\"`, `osascript -e '…'`, or `date`. May span multiple lines.",
"timeoutSeconds": "Time budget for known-slow commands (default 12, max 120)."
],
optionalInputKeys: ["timeoutSeconds"],
diff --git a/apps/Donkey/Sources/DonkeyHarness/GenericHarnessBuiltInToolExecutors.swift b/apps/Donkey/Sources/DonkeyHarness/GenericHarnessBuiltInToolExecutors.swift
index eee4023e..f3db417f 100644
--- a/apps/Donkey/Sources/DonkeyHarness/GenericHarnessBuiltInToolExecutors.swift
+++ b/apps/Donkey/Sources/DonkeyHarness/GenericHarnessBuiltInToolExecutors.swift
@@ -478,6 +478,8 @@ public enum BuiltInHarnessToolExecutors {
return await webAutomate(context, services: services)
case "files.describe":
return await filesDescribe(context, services: services)
+ case "files.write":
+ return await filesWrite(context, services: services)
case "image.edit":
return await imageGenerate(context, services: services, requiresInput: true)
case "image.generate":
diff --git a/apps/Donkey/Sources/DonkeyHarness/GenericHarnessFileTools.swift b/apps/Donkey/Sources/DonkeyHarness/GenericHarnessFileTools.swift
index 46ac81cc..83e80cca 100644
--- a/apps/Donkey/Sources/DonkeyHarness/GenericHarnessFileTools.swift
+++ b/apps/Donkey/Sources/DonkeyHarness/GenericHarnessFileTools.swift
@@ -5,11 +5,14 @@ import Foundation
/// operation from general primitives — `llm.generate` to decide names/labels/tags from the
/// understanding, `shell_exec` (`mv`, `sips`, `ffmpeg`) to apply it, `state.verify` to confirm.
///
-/// There is deliberately NO per-operation file tool. Understanding is a real primitive because reading
-/// content across modalities is hard; the operation ("rename these", "resize those") is just the model
-/// reasoning over that understanding, the same way it reasons about any other goal. A `files.rename`
-/// or `files.suggest_names` tool would hardcode one ask into the registry — exactly what the planner's
-/// general reasoning is supposed to replace.
+/// The two file primitives are deliberately general, not per-operation. `files.describe` is the read
+/// side (understanding content across modalities is hard); `files.write` is the write side (persisting
+/// composed text to a path the planner controls). Both are hard primitives the planner composes from —
+/// the inverse of each other. What stays OUT of the registry is the per-operation tool: a `files.rename`
+/// or `files.suggest_names` would hardcode one ask, which is exactly the reasoning `files.describe` +
+/// `llm.generate` + `shell_exec` already cover. "Write this text here" is not such an ask: there is no
+/// other primitive for it, and without it the planner cannot persist multi-line content (a subtitle
+/// file, a script, a note) at all.
public enum FileToolSupport {
static let defaultMaxFiles = 50
static let hardMaxFiles = 200
@@ -122,4 +125,81 @@ extension BuiltInHarnessToolExecutors {
]
)
}
+
+ /// `files.write`: write text `content` to `path` (overwrite by default, or append), creating any
+ /// missing parent directories. The write side of the file primitives — how the planner persists
+ /// anything it composed when the content is multi-line or too long to inline in a shell command.
+ static func filesWrite(
+ _ context: HarnessToolExecutionContext,
+ services: HarnessBuiltInToolServices
+ ) async -> HarnessToolResult {
+ func reject(_ message: String) -> HarnessToolResult {
+ HarnessToolResult(
+ callID: context.call.id,
+ toolName: context.call.name,
+ status: .invalidInput,
+ summary: message,
+ metadata: ["executor": "builtInGeneric", "reason": "invalidInput"]
+ )
+ }
+
+ guard
+ let rawPath = context.call.input["path"]?.trimmingCharacters(in: .whitespacesAndNewlines),
+ !rawPath.isEmpty
+ else {
+ return reject("files.write requires a `path`.")
+ }
+ guard let content = context.call.input["content"] else {
+ return reject("files.write requires `content` (an empty string is allowed).")
+ }
+ let mode = context.call.input["mode"]?
+ .trimmingCharacters(in: .whitespacesAndNewlines).lowercased() ?? "overwrite"
+ guard mode == "overwrite" || mode == "append" else {
+ return reject("files.write `mode` must be \"overwrite\" or \"append\".")
+ }
+ let append = mode == "append"
+
+ let url = URL(fileURLWithPath: (rawPath as NSString).expandingTildeInPath).standardizedFileURL
+ let fileManager = FileManager.default
+ do {
+ try fileManager.createDirectory(
+ at: url.deletingLastPathComponent(),
+ withIntermediateDirectories: true
+ )
+ let newData = Data(content.utf8)
+ if append, let existing = try? Data(contentsOf: url) {
+ try (existing + newData).write(to: url, options: .atomic)
+ } else {
+ try newData.write(to: url, options: .atomic)
+ }
+ } catch {
+ return HarnessToolResult(
+ callID: context.call.id,
+ toolName: context.call.name,
+ status: .failed,
+ summary: "files.write failed: \(error.localizedDescription)",
+ metadata: ["executor": "builtInGeneric", "reason": "writeFailed", "path": url.path]
+ )
+ }
+
+ let bytes = content.utf8.count
+ return HarnessToolResult(
+ callID: context.call.id,
+ toolName: context.call.name,
+ status: .succeeded,
+ summary: "\(append ? "Appended" : "Wrote") \(bytes) byte(s) to \(url.path).",
+ observations: HarnessObservationDelta(
+ facts: [
+ "files.write.path": url.path,
+ "lastAcceptedTool": context.call.name
+ ]
+ ),
+ metadata: [
+ "executor": "builtInGeneric",
+ "path": url.path,
+ "bytes": String(bytes),
+ "mode": mode
+ ]
+ )
+ }
}
diff --git a/apps/Donkey/Sources/DonkeyHarness/GenericHarnessToolRegistry.swift b/apps/Donkey/Sources/DonkeyHarness/GenericHarnessToolRegistry.swift
index 688051e0..f4c300e4 100644
--- a/apps/Donkey/Sources/DonkeyHarness/GenericHarnessToolRegistry.swift
+++ b/apps/Donkey/Sources/DonkeyHarness/GenericHarnessToolRegistry.swift
@@ -544,6 +544,25 @@ public enum BuiltInHarnessToolCatalog {
verification: ["the returned understanding covers the requested files"],
metadata: [HarnessToolDescriptor.resultIsEvidenceMetadataKey: "true"]
),
+ descriptor(
+ "files.write",
+ pluginID: "core.files",
+ summary: "Write text to a file: pass an absolute `path` and the full `content`, and it is saved (creating any missing parent folders). This is how you persist anything you composed when the content is multi-line or too long to inline in a shell command — a subtitle (.srt) file, a script, a config, a note, a document body. Use it instead of a `cat`/`echo` heredoc in shell_exec. Overwrites by default; pass mode=append to add to the end. Pair it with llm.generate (which returns text) to write generated content exactly where you need it, then act on the file (e.g. `ffmpeg -vf subtitles=…`).",
+ input: [
+ "path": "Absolute destination path, e.g. /Users/you/Downloads/ko.srt (a leading ~ is expanded).",
+ "content": "The full text to write.",
+ "mode": "\"overwrite\" (default) replaces the file; \"append\" adds to the end."
+ ],
+ output: [
+ "path": "The absolute path written.",
+ "bytes": "How many bytes were written."
+ ],
+ optionalInputKeys: ["mode"],
+ permissions: [],
+ safety: .reversible,
+ verification: ["the file exists at the returned path with the intended content"],
+ metadata: [HarnessToolDescriptor.resultIsEvidenceMetadataKey: "true"]
+ ),
descriptor(
"image.edit",
pluginID: "core.image",
diff --git a/apps/Donkey/Sources/DonkeyRuntime/DonkeyCommandBackends.swift b/apps/Donkey/Sources/DonkeyRuntime/DonkeyCommandBackends.swift
index a41b721f..59cd89e8 100644
--- a/apps/Donkey/Sources/DonkeyRuntime/DonkeyCommandBackends.swift
+++ b/apps/Donkey/Sources/DonkeyRuntime/DonkeyCommandBackends.swift
@@ -510,17 +510,14 @@ public enum DonkeyCommandBackends {
private static func shellExec(_ context: HarnessToolExecutionContext) async -> HarnessToolResult {
guard let command = trimmed(context.call.input["command"]) ?? trimmed(context.call.input["cmd"]) else {
- return invalidInput(context, "shell_exec requires a one-line `command`.")
- }
- guard !command.contains("\n"), !command.contains("\r") else {
- return invalidInput(context, "shell_exec only runs a single-line command.")
+ return invalidInput(context, "shell_exec requires a `command`.")
}
guard command.count <= shellCommandMaxLength else {
return invalidInput(
context,
"shell_exec command is \(command.count) chars; the limit is \(shellCommandMaxLength). "
- + "Don't inline large content — generate it to a file with llm.generate (toFile=true), "
- + "then run a short command that reads from that file."
+ + "Don't inline large content into a command — write it straight to a file with "
+ + "files.write (path + content), then run a short command that reads from that file."
)
}