@mumbrane/leto 0.0.0-stage → 0.19.0

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 (56) hide show
  1. package/README.md +395 -2
  2. package/bin/leto.js +8 -0
  3. package/evals/core/suite.json +249 -0
  4. package/index.js +12 -0
  5. package/package.json +43 -4
  6. package/src/agent.js +572 -0
  7. package/src/agents.js +68 -0
  8. package/src/anthropic.js +204 -0
  9. package/src/browser.js +427 -0
  10. package/src/btw.js +56 -0
  11. package/src/checkpoints.js +90 -0
  12. package/src/ci.js +115 -0
  13. package/src/cli.js +1926 -0
  14. package/src/client.js +111 -0
  15. package/src/config.js +205 -0
  16. package/src/documents.js +198 -0
  17. package/src/eval.js +289 -0
  18. package/src/hooks.js +115 -0
  19. package/src/ide.js +231 -0
  20. package/src/input.js +148 -0
  21. package/src/jobs.js +97 -0
  22. package/src/legacy.js +41 -0
  23. package/src/lsp.js +401 -0
  24. package/src/markdown.js +153 -0
  25. package/src/mcp.js +346 -0
  26. package/src/memory.js +136 -0
  27. package/src/orchestrator.js +141 -0
  28. package/src/patching.js +185 -0
  29. package/src/permissions.js +75 -0
  30. package/src/plugins.js +118 -0
  31. package/src/policy.js +74 -0
  32. package/src/pricing.js +67 -0
  33. package/src/processes.js +139 -0
  34. package/src/prompt.js +134 -0
  35. package/src/prompts.js +189 -0
  36. package/src/providers.js +59 -0
  37. package/src/repomap.js +211 -0
  38. package/src/sandbox.js +90 -0
  39. package/src/sdk.js +220 -0
  40. package/src/selftool.js +238 -0
  41. package/src/serve.js +233 -0
  42. package/src/session.js +168 -0
  43. package/src/setup.js +157 -0
  44. package/src/skills.js +125 -0
  45. package/src/theme.js +185 -0
  46. package/src/toolcalls.js +257 -0
  47. package/src/tools.js +976 -0
  48. package/src/tui.js +1179 -0
  49. package/src/ui.js +454 -0
  50. package/src/update.js +33 -0
  51. package/src/usage.js +83 -0
  52. package/src/verify.js +131 -0
  53. package/src/version.js +3 -0
  54. package/src/worktree.js +87 -0
  55. package/vscode/extension.js +132 -0
  56. package/vscode/package.json +21 -0
package/src/btw.js ADDED
@@ -0,0 +1,56 @@
1
+ // /btw: a quick side question, answered from what leto knows right now,
2
+ // without adding anything to the conversation or interrupting the work.
3
+ //
4
+ // It sends the same system prompt, tools and history as the main agent (so
5
+ // on Claude it reads almost everything from the prompt cache), plus the
6
+ // question, with tool use turned off. The answer is shown and then forgotten.
7
+ import { streamChat } from './client.js';
8
+ import { isNativeAnthropic, streamAnthropic } from './anthropic.js';
9
+ import { toolSchemas } from './tools.js';
10
+
11
+ // History up to the last complete step: a reply still waiting for its tool
12
+ // results can't be followed by a new question.
13
+ export function settledHistory(messages) {
14
+ let end = messages.length;
15
+ for (let i = messages.length - 1; i >= 0; i--) {
16
+ const m = messages[i];
17
+ if (m.role === 'assistant' && m.tool_calls?.length) {
18
+ const answered = new Set(messages.slice(i + 1).filter((x) => x.role === 'tool').map((x) => x.tool_call_id));
19
+ if (!m.tool_calls.every((c) => answered.has(c.id))) end = i;
20
+ break;
21
+ }
22
+ }
23
+ const out = messages.slice(0, end);
24
+ // A trailing user message (the turn in progress) is kept: the question goes after it.
25
+ return out;
26
+ }
27
+
28
+ const NOTE = '[Side question from the user, asked with /btw while you work. Answer only this side question, not any earlier request still in progress, briefly and directly from what you already know in this conversation; you cannot use tools for this. This exchange is not part of the main task: after it, the main work continues as before.]';
29
+
30
+ export async function askBtw(agent, question, { onText, signal } = {}) {
31
+ const history = [{ role: 'system', content: agent.system }, ...settledHistory(agent.session.messages)];
32
+ // After tool results or a user message, Claude's API merges this into the same user turn; that's fine.
33
+ history.push({ role: 'user', content: `${NOTE}\n\n${question}` });
34
+ const tools = toolSchemas((n) => agent.allowsTool(n));
35
+ const config = { ...agent.config, toolChoice: 'none' };
36
+ let text = '';
37
+ if (isNativeAnthropic(config)) {
38
+ for await (const ev of streamAnthropic({ config, history, tools, signal })) {
39
+ if (ev.type === 'text') {
40
+ text += ev.text;
41
+ onText?.(ev.text);
42
+ } else if (ev.type === 'done') agent.usage.add(ev.message.model ?? config.model, ev.message.usage);
43
+ }
44
+ } else {
45
+ const messages = history.map(({ images, documents, anthropic, ...m }) => m);
46
+ for await (const ev of streamChat({ config, messages, tools: config.toolMode === 'text' ? [] : tools, signal })) {
47
+ if (ev.usage) agent.usage.add(config.model, ev.usage);
48
+ const d = ev.choices?.[0]?.delta?.content;
49
+ if (d) {
50
+ text += d;
51
+ onText?.(d);
52
+ }
53
+ }
54
+ }
55
+ return text.trim();
56
+ }
@@ -0,0 +1,90 @@
1
+ // File checkpoints: before leto writes or edits a file, the original is
2
+ // saved. /undo puts files back the way they were before a turn. Stored next to
3
+ // the session log (<session>.checkpoints/), so undo works after a restart.
4
+ import fs from 'node:fs';
5
+ import path from 'node:path';
6
+
7
+ export class Checkpoints {
8
+ constructor(sessionFile) {
9
+ this.dir = sessionFile ? sessionFile.replace(/\.jsonl$/, '.checkpoints') : null;
10
+ this.turns = [];
11
+ this.current = null;
12
+ this.blobs = 0;
13
+ if (this.dir) {
14
+ try {
15
+ const index = JSON.parse(fs.readFileSync(path.join(this.dir, 'index.json'), 'utf8'));
16
+ this.turns = index.turns ?? [];
17
+ this.blobs = index.blobs ?? 0;
18
+ } catch {}
19
+ }
20
+ }
21
+
22
+ // `at` is the index of the user message that started this turn.
23
+ beginTurn(label, at) {
24
+ this.current = { id: (this.turns.at(-1)?.id ?? 0) + 1, label: String(label).replace(/\s+/g, ' ').slice(0, 80), time: Date.now(), at, files: {} };
25
+ }
26
+
27
+ // Undo every turn that started at or after message `index`.
28
+ restoreFrom(index) {
29
+ const first = this.turns.find((t) => typeof t.at === 'number' && t.at >= index);
30
+ return first ? this.undo(first.id) : [];
31
+ }
32
+
33
+ // Save a file's current state the first time a turn touches it.
34
+ record(file) {
35
+ if (!this.current) this.beginTurn('(changes)');
36
+ const abs = path.resolve(file);
37
+ if (abs in this.current.files) return;
38
+ let blob = null;
39
+ if (fs.existsSync(abs) && fs.statSync(abs).isFile()) {
40
+ blob = `${++this.blobs}.bin`;
41
+ if (this.dir) {
42
+ fs.mkdirSync(this.dir, { recursive: true });
43
+ fs.copyFileSync(abs, path.join(this.dir, blob));
44
+ } else {
45
+ (this.memory ??= new Map()).set(blob, fs.readFileSync(abs));
46
+ }
47
+ }
48
+ this.current.files[abs] = blob; // null means the file didn't exist
49
+ if (!this.turns.includes(this.current)) this.turns.push(this.current);
50
+ this.save();
51
+ }
52
+
53
+ endTurn() {
54
+ this.current = null;
55
+ }
56
+
57
+ list() {
58
+ return this.turns.map((t) => ({ id: t.id, label: t.label, time: t.time, files: Object.keys(t.files) }));
59
+ }
60
+
61
+ // Undo the most recent turn with changes (or every turn back to `toId`).
62
+ undo(toId) {
63
+ const target = toId ?? this.turns.at(-1)?.id;
64
+ if (target === undefined) return [];
65
+ const restored = new Map();
66
+ while (this.turns.length && this.turns.at(-1).id >= target) {
67
+ const turn = this.turns.pop();
68
+ for (const [file, blob] of Object.entries(turn.files)) {
69
+ if (blob === null) {
70
+ fs.rmSync(file, { force: true });
71
+ restored.set(file, 'removed');
72
+ } else {
73
+ fs.mkdirSync(path.dirname(file), { recursive: true });
74
+ const data = this.dir ? fs.readFileSync(path.join(this.dir, blob)) : this.memory.get(blob);
75
+ fs.writeFileSync(file, data);
76
+ restored.set(file, 'restored');
77
+ }
78
+ }
79
+ }
80
+ this.current = null;
81
+ this.save();
82
+ return [...restored].map(([file, action]) => ({ file, action }));
83
+ }
84
+
85
+ save() {
86
+ if (!this.dir) return;
87
+ fs.mkdirSync(this.dir, { recursive: true });
88
+ fs.writeFileSync(path.join(this.dir, 'index.json'), JSON.stringify({ turns: this.turns, blobs: this.blobs }));
89
+ }
90
+ }
package/src/ci.js ADDED
@@ -0,0 +1,115 @@
1
+ // `leto ci init`: a GitHub Actions workflow that runs leto on your repo.
2
+ //
3
+ // - Comment "@leto <request>" on an issue or pull request: leto does the
4
+ // work, pushes a branch, opens (or updates) a pull request, and replies
5
+ // with what it did.
6
+ // - Run it by hand from the Actions tab with a prompt.
7
+ // Only people with write access (owners, members, collaborators) can trigger
8
+ // it, and the comment text is passed through an environment variable, never
9
+ // pasted into a shell command.
10
+ import fs from 'node:fs';
11
+ import path from 'node:path';
12
+
13
+ export const WORKFLOW = `# leto: comment "@leto <what to do>" on an issue or PR.
14
+ # Needs repository secrets LETO_API_KEY (and optionally LETO_PROVIDER, LETO_MODEL).
15
+ name: leto
16
+
17
+ on:
18
+ issue_comment:
19
+ types: [created]
20
+ pull_request_review_comment:
21
+ types: [created]
22
+ workflow_dispatch:
23
+ inputs:
24
+ prompt:
25
+ description: What should leto do?
26
+ required: true
27
+
28
+ permissions:
29
+ contents: write
30
+ pull-requests: write
31
+ issues: write
32
+
33
+ concurrency:
34
+ group: leto-\${{ github.event.issue.number || github.event.pull_request.number || github.run_id }}
35
+ cancel-in-progress: false
36
+
37
+ jobs:
38
+ leto:
39
+ if: >-
40
+ github.event_name == 'workflow_dispatch' ||
41
+ (contains(github.event.comment.body, '@leto') &&
42
+ contains(fromJSON('["OWNER","MEMBER","COLLABORATOR"]'), github.event.comment.author_association))
43
+ runs-on: ubuntu-latest
44
+ timeout-minutes: 60
45
+ steps:
46
+ - uses: actions/checkout@v4
47
+ with:
48
+ fetch-depth: 0
49
+
50
+ - name: Check out the pull request branch
51
+ if: github.event.issue.pull_request || github.event.pull_request
52
+ env:
53
+ GH_TOKEN: \${{ github.token }}
54
+ PR: \${{ github.event.issue.number || github.event.pull_request.number }}
55
+ run: gh pr checkout "$PR"
56
+
57
+ - uses: actions/setup-node@v4
58
+ with:
59
+ node-version: 22
60
+
61
+ - name: Install leto
62
+ run: npm install -g @mumbrane/leto
63
+
64
+ - name: Run leto
65
+ id: run
66
+ env:
67
+ LETO_PROVIDER: \${{ secrets.LETO_PROVIDER || 'anthropic' }}
68
+ LETO_API_KEY: \${{ secrets.LETO_API_KEY }}
69
+ LETO_MODEL: \${{ secrets.LETO_MODEL }}
70
+ LETO_NO_UPDATE_CHECK: '1'
71
+ COMMENT: \${{ github.event.comment.body }}
72
+ INPUT_PROMPT: \${{ github.event.inputs.prompt }}
73
+ TITLE: \${{ github.event.issue.title || github.event.pull_request.title }}
74
+ BODY: \${{ github.event.issue.body || github.event.pull_request.body }}
75
+ run: |
76
+ REQUEST="\${INPUT_PROMPT:-\${COMMENT#*@leto}}"
77
+ {
78
+ printf 'You are running in GitHub Actions on %s.\\n' "$GITHUB_REPOSITORY"
79
+ [ -n "$TITLE" ] && printf '\\nIssue/PR title: %s\\n\\nIssue/PR description:\\n%s\\n' "$TITLE" "$BODY"
80
+ printf '\\nRequest:\\n%s\\n\\nMake the change, run the relevant tests, and do not commit or push: the workflow does that. End with a short summary of what you changed.\\n' "$REQUEST"
81
+ } > "$RUNNER_TEMP/prompt.txt"
82
+ set +e
83
+ leto -p --yolo --json --max-cost "\${LETO_MAX_COST:-5}" < "$RUNNER_TEMP/prompt.txt" > "$RUNNER_TEMP/result.json"
84
+ echo "exit=$?" >> "$GITHUB_OUTPUT"
85
+ node -e 'const r=JSON.parse(require("fs").readFileSync(process.argv[1],"utf8"));require("fs").writeFileSync(process.argv[2],(r.result||r.error||"(no output)")+(r.cost_usd!=null?"\\n\\n<sub>leto · "+r.model+" · $"+r.cost_usd.toFixed(2)+" · "+r.tool_calls+" tool calls</sub>":""))' "$RUNNER_TEMP/result.json" "$RUNNER_TEMP/summary.md" || echo "leto did not produce a result." > "$RUNNER_TEMP/summary.md"
86
+
87
+ - name: Push changes and reply
88
+ env:
89
+ GH_TOKEN: \${{ github.token }}
90
+ NUMBER: \${{ github.event.issue.number || github.event.pull_request.number }}
91
+ IS_PR: \${{ github.event.issue.pull_request != null || github.event.pull_request != null }}
92
+ run: |
93
+ git config user.name "leto[bot]"
94
+ git config user.email "leto[bot]@users.noreply.github.com"
95
+ if [ -n "$(git status --porcelain)" ]; then
96
+ git add -A
97
+ if [ "$IS_PR" = "true" ]; then
98
+ git commit -qm "leto: address review comment" && git push -q
99
+ else
100
+ BRANCH="leto/\${NUMBER:-run-$GITHUB_RUN_ID}"
101
+ git checkout -qb "$BRANCH"
102
+ git commit -qm "leto: \${NUMBER:+#$NUMBER }changes" && git push -q -u origin "$BRANCH" --force
103
+ gh pr create --fill --head "$BRANCH" --body-file "$RUNNER_TEMP/summary.md" \${NUMBER:+--title "leto: fix #$NUMBER"} || true
104
+ fi
105
+ fi
106
+ if [ -n "$NUMBER" ]; then gh issue comment "$NUMBER" --body-file "$RUNNER_TEMP/summary.md"; else cat "$RUNNER_TEMP/summary.md" >> "$GITHUB_STEP_SUMMARY"; fi
107
+ `;
108
+
109
+ export function initWorkflow(cwd) {
110
+ const file = path.join(cwd, '.github', 'workflows', 'leto.yml');
111
+ if (fs.existsSync(file)) throw new Error(`${file} already exists.`);
112
+ fs.mkdirSync(path.dirname(file), { recursive: true });
113
+ fs.writeFileSync(file, WORKFLOW);
114
+ return file;
115
+ }