@yanzc/claude-pro 0.8.0 → 0.9.1

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 (57) hide show
  1. package/README.md +342 -318
  2. package/package.json +1 -2
  3. package/src/agent.js +80 -14
  4. package/src/agents.js +3 -2
  5. package/src/api.js +39 -34
  6. package/src/browser/broker.js +367 -0
  7. package/src/browser/cdp.js +114 -0
  8. package/src/browser/render.js +242 -0
  9. package/src/browser/session.js +948 -0
  10. package/src/browser/snapshot.js +212 -0
  11. package/src/cache.js +1 -1
  12. package/src/checkpoints.js +1 -0
  13. package/src/commands.js +169 -67
  14. package/src/compaction.js +75 -0
  15. package/src/config.js +145 -144
  16. package/src/context.js +45 -18
  17. package/src/filestate.js +3 -2
  18. package/src/gateway.js +208 -164
  19. package/src/hooks.js +2 -2
  20. package/src/keepalive.js +1 -1
  21. package/src/main.js +52 -40
  22. package/src/mcp.js +2 -2
  23. package/src/peers.js +1 -10
  24. package/src/print.js +9 -3
  25. package/src/prompt-profiles.js +0 -6
  26. package/src/prompts.js +7 -63
  27. package/src/ratelimit.js +2 -4
  28. package/src/remote.js +76 -14
  29. package/src/runtime.js +33 -10
  30. package/src/selfcheck.js +3 -3
  31. package/src/session.js +93 -3
  32. package/src/supervisor.js +10 -1
  33. package/src/tools/bash.js +18 -17
  34. package/src/tools/browser.js +365 -0
  35. package/src/tools/code.js +430 -0
  36. package/src/tools/effort.js +3 -4
  37. package/src/tools/files.js +368 -45
  38. package/src/tools/index.js +9 -1
  39. package/src/tools/notebook.js +11 -4
  40. package/src/tools/processes.js +274 -0
  41. package/src/tools/search.js +33 -8
  42. package/src/tools/web.js +316 -46
  43. package/src/trace.js +0 -4
  44. package/src/ui/ansi.js +92 -13
  45. package/src/ui/dialogs.js +16 -54
  46. package/src/ui/editor.js +231 -34
  47. package/src/ui/markdown.js +1 -2
  48. package/src/ui/render.js +1 -2
  49. package/src/ui/repl.js +335 -101
  50. package/src/ui/screen.js +139 -31
  51. package/src/updater.js +135 -165
  52. package/src/utils/clipboard.js +5 -3
  53. package/src/utils/dirs.js +136 -0
  54. package/src/utils/encoding.js +23 -2
  55. package/src/utils/paths.js +15 -17
  56. package/src/utils/shell.js +66 -11
  57. package/.env.example +0 -29
@@ -0,0 +1,212 @@
1
+ // A page as the model reads it: the browser's own accessibility tree (CDP Accessibility.getFullAXTree, shadow DOM
2
+ // included, hidden elements left out), cut down to what can be acted on and what gives it structure, one line per
3
+ // element, each with a ref (e + the element's backend node id, stable while the element lives) to act on it.
4
+
5
+ // Roles that are acted on. Their children are not shown: their text is the name.
6
+ const INTERACTIVE = new Set([
7
+ 'button', 'link', 'textbox', 'searchbox', 'combobox', 'checkbox', 'radio', 'switch', 'slider', 'spinbutton',
8
+ 'menuitem', 'menuitemcheckbox', 'menuitemradio', 'tab', 'option', 'treeitem', 'listbox', 'ListBoxOption',
9
+ 'DisclosureTriangle', 'PopUpButton', 'ToggleButton', 'ColorWell', 'Date', 'DateTime', 'InputTime',
10
+ ]);
11
+ // Containers shown when something inside them is: they tell where an element is (a dialog, the main area, a post).
12
+ const CONTAINERS = new Set([
13
+ 'dialog', 'alertdialog', 'navigation', 'main', 'banner', 'contentinfo', 'complementary', 'search', 'form', 'region',
14
+ 'article', 'tablist', 'menu', 'menubar', 'toolbar', 'tree', 'grid', 'table', 'Iframe',
15
+ ]);
16
+ // Shown with their text even when nothing inside them is interactive: what the page says just now.
17
+ const MESSAGES = new Set(['alert', 'status', 'heading']);
18
+ const FRIENDLY = { DisclosureTriangle: 'button', PopUpButton: 'combobox', ToggleButton: 'button', ListBoxOption: 'option', Iframe: 'iframe' };
19
+ const VALUED = new Set(['textbox', 'searchbox', 'combobox', 'spinbutton', 'slider']);
20
+ const CHECKABLE = new Set(['checkbox', 'radio', 'switch', 'menuitemcheckbox', 'menuitemradio']);
21
+
22
+ export const snapshotLimits = { nameChars: 100, valueChars: 100, textChars: 300 };
23
+
24
+ const clean = (s) => String(s ?? '').replace(/[\u200b-\u200d\ufeff]/g, '').replace(/\s+/g, ' ').trim();
25
+ const cut = (s, n) => (s.length > n ? `${s.slice(0, n - 1)}…` : s);
26
+ const quote = (s) => `"${s.replace(/\\/g, '\\\\').replace(/"/g, '\\"')}"`;
27
+ const prop = (node, name) => node.properties?.find((p) => p.name === name)?.value?.value;
28
+
29
+ // The text inside a node (its StaticText descendants), for an editor or a message whose name is empty.
30
+ function innerText(node, byId, max = 400) {
31
+ let out = '';
32
+ const walk = (n) => {
33
+ if (!n || out.length > max) return;
34
+ if (n.role?.value === 'StaticText') { out += `${out ? ' ' : ''}${n.name?.value || ''}`; return; }
35
+ for (const id of n.childIds || []) walk(byId.get(id));
36
+ };
37
+ walk(node);
38
+ return clean(out);
39
+ }
40
+
41
+ function shortUrl(url, base) {
42
+ try {
43
+ const u = new URL(url);
44
+ const b = base ? new URL(base) : null;
45
+ return b && u.origin === b.origin ? `${u.pathname}${u.search}${u.hash}` : url;
46
+ } catch { return url; }
47
+ }
48
+
49
+ // One line for a kept node.
50
+ function describe(node, role, byId, { full, baseUrl }) {
51
+ const shown = FRIENDLY[role] || role;
52
+ let name = clean(node.name?.value);
53
+ if (!name && MESSAGES.has(role) && role !== 'heading') name = innerText(node, byId);
54
+ let s = `- ${shown}${name ? ` ${quote(cut(name, snapshotLimits.nameChars))}` : ''}`;
55
+ if (node.backendDOMNodeId) s += ` [ref=e${node.backendDOMNodeId}]`;
56
+ const level = prop(node, 'level');
57
+ if (role === 'heading' && level) s += ` [level=${level}]`;
58
+ if (prop(node, 'disabled')) s += ' [disabled]';
59
+ const checked = prop(node, 'checked');
60
+ if (CHECKABLE.has(role)) s += checked === 'true' || checked === true ? ' [checked]' : checked === 'mixed' ? ' [checked=mixed]' : ' [unchecked]';
61
+ const pressed = prop(node, 'pressed');
62
+ if (pressed === 'true' || pressed === true) s += ' [pressed]';
63
+ const expanded = prop(node, 'expanded');
64
+ if (expanded !== undefined && role !== 'combobox') s += expanded ? ' [expanded]' : ' [collapsed]';
65
+ if (prop(node, 'selected') && (role === 'tab' || role === 'option' || role === 'treeitem')) s += ' [selected]';
66
+ if (prop(node, 'focused') && role !== 'RootWebArea') s += ' [focused]';
67
+ if (prop(node, 'invalid') === 'true') s += ' [invalid]';
68
+ if (VALUED.has(role)) {
69
+ let value = clean(node.value?.value);
70
+ if (!value && prop(node, 'editable') === 'richtext') value = innerText(node, byId);
71
+ if (value) s += `: ${cut(value, snapshotLimits.valueChars)}`;
72
+ }
73
+ if (full && role === 'link') {
74
+ const url = prop(node, 'url');
75
+ if (url) s += ` → ${cut(shortUrl(url, baseUrl), 120)}`;
76
+ }
77
+ return s;
78
+ }
79
+
80
+ // The snapshot as lines: [{text, ref, depth, kind}]. `trees`: {main: nodes, frames: Map(ownerBackendId -> nodes)},
81
+ // the frames being the iframes that run in the page's process (a cross-site iframe has its own and is marked so).
82
+ // `full`: also the text of the page and the links' addresses. `root`: a backend node id to show only that part.
83
+ export function snapshotLines(trees, { full = false, root = null, baseUrl = '' } = {}) {
84
+ const lines = [];
85
+ const indexes = [];
86
+ const index = (nodes) => {
87
+ const byId = new Map(nodes.map((n) => [n.nodeId, n]));
88
+ indexes.push(byId);
89
+ return byId;
90
+ };
91
+ const visitTree = (nodes, depth, start) => {
92
+ if (!nodes?.length) return;
93
+ const byId = index(nodes);
94
+ let begin = start;
95
+ if (!begin) begin = nodes.find((n) => !n.parentId) || nodes[0];
96
+ visit(begin, byId, depth);
97
+ };
98
+ const visit = (node, byId, depth) => {
99
+ if (!node) return;
100
+ const role = node.role?.value || '';
101
+ if (role === 'InlineTextBox' || role === 'LineBreak' || role === 'ListMarker') return;
102
+ if (role === 'StaticText') {
103
+ if (!full || node.ignored) return;
104
+ const text = clean(node.name?.value);
105
+ if (!text) return;
106
+ const last = lines[lines.length - 1];
107
+ // the text of one paragraph comes in pieces (a link or a bold word in between): one line for the run
108
+ if (last?.kind === 'text' && last.depth === depth && last.parent === node.parentId) {
109
+ last.raw = `${last.raw} ${text}`;
110
+ last.text = `- text: ${quote(cut(last.raw, snapshotLimits.textChars))}`;
111
+ } else {
112
+ lines.push({ text: `- text: ${quote(cut(text, snapshotLimits.textChars))}`, raw: text, ref: null, depth, kind: 'text', parent: node.parentId });
113
+ }
114
+ return;
115
+ }
116
+ const interactive = INTERACTIVE.has(role) || (prop(node, 'editable') && prop(node, 'focusable') && !prop(node, 'readonly') && role !== 'RootWebArea' && role !== 'StaticText' && byId.get(node.parentId) && !prop(byId.get(node.parentId), 'editable'));
117
+ const container = CONTAINERS.has(role) && (role !== 'region' && role !== 'form' ? true : Boolean(clean(node.name?.value)));
118
+ const message = MESSAGES.has(role);
119
+ const image = full && (role === 'img' || role === 'image') && clean(node.name?.value);
120
+ const kept = !node.ignored && (interactive || container || message || image);
121
+ const at = lines.length;
122
+ if (kept) lines.push({ text: describe(node, interactive && !INTERACTIVE.has(role) ? 'textbox' : role, byId, { full, baseUrl }), ref: node.backendDOMNodeId ? `e${node.backendDOMNodeId}` : null, depth, kind: interactive ? 'control' : container ? 'container' : 'message' });
123
+ if (kept && (interactive || message || image) && role !== 'listbox' && role !== 'treeitem') return;
124
+ const childDepth = kept ? depth + 1 : depth;
125
+ if (role === 'Iframe' && node.backendDOMNodeId) {
126
+ const frame = trees.frames?.get(node.backendDOMNodeId);
127
+ if (frame === 'remote') lines.push({ text: '- (a cross-site frame: its content is not shown)', ref: null, depth: childDepth, kind: 'message' });
128
+ else if (frame) visitTree(frame, childDepth);
129
+ } else {
130
+ for (const id of node.childIds || []) visit(byId.get(id), byId, childDepth);
131
+ }
132
+ // a container with nothing to show inside is left out
133
+ if (kept && container && lines.length === at + 1) lines.pop();
134
+ };
135
+ if (root) {
136
+ const all = [trees.main, ...(trees.frames ? [...trees.frames.values()].filter(Array.isArray) : [])];
137
+ for (const nodes of all) {
138
+ const start = nodes?.find((n) => n.backendDOMNodeId === root);
139
+ if (start) { visitTree(nodes, 0, start); break; }
140
+ }
141
+ } else {
142
+ visitTree(trees.main, 0);
143
+ }
144
+ return lines.map(({ text, ref, depth, kind }) => ({ text, ref, depth, kind }));
145
+ }
146
+
147
+ export const renderLines = (lines) => lines.map((l) => `${' '.repeat(l.depth)}${l.text}`).join('\n');
148
+
149
+ // What changed between two snapshots of a page (by ref, else by text): the added and changed lines in the order of
150
+ // the new snapshot (indented as there), and how many lines are gone. The focus moving is no change: every click
151
+ // moves it.
152
+ export function diffLines(before, after) {
153
+ // a line without a ref (text) is known by what it says, and the same text twice by its count: a second "Saved"
154
+ // is a change
155
+ const keyed = (lines) => {
156
+ const count = new Map();
157
+ return lines.map((l) => {
158
+ if (l.ref) return [l.ref, l];
159
+ const k = `${l.depth}:${l.text}`;
160
+ const n = (count.get(k) || 0) + 1;
161
+ count.set(k, n);
162
+ return [`${k}#${n}`, l];
163
+ });
164
+ };
165
+ const same = (t) => t.replace(' [focused]', '');
166
+ const oldKeyed = keyed(before);
167
+ const old = new Map(oldKeyed.map(([k, l]) => [k, same(l.text)]));
168
+ const seen = new Set();
169
+ const out = [];
170
+ for (const [k, l] of keyed(after)) {
171
+ seen.add(k);
172
+ if (!old.has(k)) out.push({ ...l, mark: '+' });
173
+ else if (old.get(k) !== same(l.text)) out.push({ ...l, mark: '~' });
174
+ }
175
+ const gone = oldKeyed.filter(([k]) => !seen.has(k)).map(([, l]) => l);
176
+ return { changed: out, gone };
177
+ }
178
+
179
+ export function renderDiff({ changed, gone }) {
180
+ const lines = changed.map((l) => `${l.mark} ${' '.repeat(l.depth)}${l.text}`);
181
+ if (gone.length) {
182
+ const names = gone.slice(0, 5).map((l) => cut(l.text.replace(/^- /, '').replace(/ \[ref=e\d+\]/, ''), 60)).join('; ');
183
+ lines.push(`- ${gone.length} line${gone.length === 1 ? '' : 's'} gone${names ? ` (${names}${gone.length > 5 ? '; …' : ''})` : ''}`);
184
+ }
185
+ return lines.join('\n');
186
+ }
187
+
188
+ // The lines that contain `query` (text, or /regex/flags), each under the containers it is in.
189
+ export function findLines(lines, query, max = 30) {
190
+ let test;
191
+ const re = /^\/(.+)\/([a-z]*)$/.exec(query);
192
+ if (re) {
193
+ const flags = re[2].replace(/[gy]/g, ''); // test() with g or y goes on from the last match: lines were skipped
194
+ const rx = new RegExp(re[1], flags.includes('i') ? flags : `${flags}i`);
195
+ test = (s) => rx.test(s);
196
+ } else {
197
+ const q = query.toLowerCase();
198
+ test = (s) => s.toLowerCase().includes(q);
199
+ }
200
+ const out = [];
201
+ let matches = 0;
202
+ for (let i = 0; i < lines.length; i++) {
203
+ if (!test(lines[i].text.replace(/ \[ref=e\d+\]/, ''))) continue;
204
+ if (++matches > max) continue;
205
+ const path = [];
206
+ for (let j = i - 1, d = lines[i].depth; j >= 0 && d > 0; j--) {
207
+ if (lines[j].depth < d) { path.unshift(lines[j].text.replace(/^- /, '')); d = lines[j].depth; }
208
+ }
209
+ out.push(`${lines[i].text}${path.length ? ` (in: ${path.map((p) => cut(p, 60)).join(' › ')})` : ''}`);
210
+ }
211
+ return { text: out.join('\n'), matches };
212
+ }
package/src/cache.js CHANGED
@@ -1,7 +1,7 @@
1
1
  // Prompt caching, the way Claude Code does it (https://code.claude.com/docs/en/prompt-caching):
2
2
  // - cache_control markers go on the tools, the system prompt and the newest message (see api.js);
3
3
  // - there are two TTL buckets: "main" is the conversation itself, "other" is everything else
4
- // (sub-agents, compaction, side queries). Each is 5 minutes by default, or 1 hour when asked for;
4
+ // (sub-agents, compaction, side queries). The conversation defaults to 1 hour, everything else to 5 minutes;
5
5
  // - the whole thing can be switched off;
6
6
  // - what the API reports (cache_read_input_tokens, cache_creation_input_tokens) is tracked here, so
7
7
  // that /cost can say how much of the input came from the cache and why a request missed it.
@@ -190,6 +190,7 @@ export function rewind(agent, store, id, mode = 'both') {
190
190
  }
191
191
  if (mode !== 'code') {
192
192
  agent.replaceMessages(agent.messages.slice(0, entry.messageIndex));
193
+ agent.fileState.clear(); // what was read after that point is gone from the conversation: Write wants it read again
193
194
  agent.todos = [];
194
195
  agent.lastContextTokens = estimateTokens(agent.messages);
195
196
  agent.tokensSinceUsage = 0;