typeclaw 0.51.0 → 0.52.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 (202) hide show
  1. package/dist/types/agent/bash-sandbox-env.d.ts +2 -0
  2. package/dist/types/agent/bash-sandbox.d.ts +4 -0
  3. package/dist/types/agent/index.d.ts +5 -0
  4. package/dist/types/agent/multimodal/look-at.d.ts +1 -1
  5. package/dist/types/agent/plugin-tools.d.ts +4 -0
  6. package/dist/types/agent/prompt-cache-key.d.ts +1 -0
  7. package/dist/types/agent/session-tail.d.ts +12 -0
  8. package/dist/types/agent/subagents.d.ts +5 -0
  9. package/dist/types/agent/system-prompt-composition.d.ts +1 -0
  10. package/dist/types/agent/system-prompt.d.ts +2 -0
  11. package/dist/types/agent/tools/channel-disengage.d.ts +1 -1
  12. package/dist/types/agent/tools/channel-edit.d.ts +1 -1
  13. package/dist/types/agent/tools/channel-fetch-attachment.d.ts +1 -1
  14. package/dist/types/agent/tools/channel-handoff.d.ts +1 -1
  15. package/dist/types/agent/tools/channel-history.d.ts +1 -1
  16. package/dist/types/agent/tools/channel-react.d.ts +1 -1
  17. package/dist/types/agent/tools/channel-read.d.ts +10 -2
  18. package/dist/types/agent/tools/channel-reply.d.ts +1 -1
  19. package/dist/types/agent/tools/channel-send.d.ts +1 -1
  20. package/dist/types/agent/tools/channel-session-search.d.ts +1 -1
  21. package/dist/types/agent/tools/grant-role.d.ts +1 -1
  22. package/dist/types/agent/tools/post-github-review.d.ts +1 -1
  23. package/dist/types/agent/tools/restart.d.ts +1 -1
  24. package/dist/types/agent/tools/skip-response.d.ts +1 -1
  25. package/dist/types/agent/tools/spawn-subagent.d.ts +2 -1
  26. package/dist/types/agent/tools/stream-snapshot.d.ts +1 -1
  27. package/dist/types/agent/tools/subagent-cancel.d.ts +1 -1
  28. package/dist/types/agent/tools/subagent-output.d.ts +1 -1
  29. package/dist/types/agent/tools/todo/index.d.ts +1 -1
  30. package/dist/types/agent/tools/webfetch/adaptive-html.d.ts +3 -3
  31. package/dist/types/agent/tools/webfetch/strategies/readability-process.d.ts +48 -0
  32. package/dist/types/agent/tools/webfetch/tool.d.ts +2 -2
  33. package/dist/types/agent/tools/websearch.d.ts +1 -1
  34. package/dist/types/channels/adapters/github/auth-app.d.ts +25 -0
  35. package/dist/types/channels/adapters/github/auth-pat.d.ts +17 -0
  36. package/dist/types/channels/adapters/github/auth.d.ts +24 -0
  37. package/dist/types/channels/github-token-bridge.d.ts +4 -0
  38. package/dist/types/channels/lost-background-context.d.ts +2 -0
  39. package/dist/types/channels/persistence.d.ts +1 -0
  40. package/dist/types/channels/router.d.ts +5 -1
  41. package/dist/types/channels/types.d.ts +1 -0
  42. package/dist/types/coding-agent/tool.d.ts +1 -1
  43. package/dist/types/config/config.d.ts +20 -11
  44. package/dist/types/config/providers.d.ts +47 -0
  45. package/dist/types/eval/sandbox.d.ts +2 -0
  46. package/dist/types/git/resolve-identity.d.ts +2 -0
  47. package/dist/types/init/dockerfile.d.ts +1 -0
  48. package/dist/types/init/gitignore.d.ts +1 -1
  49. package/dist/types/permissions/builtins.d.ts +1 -0
  50. package/dist/types/plugin/types.d.ts +3 -0
  51. package/dist/types/sandbox/canonical-secrets.d.ts +2 -2
  52. package/dist/types/sandbox/coding-agent.d.ts +1 -1
  53. package/dist/types/sandbox/dependency-bins.d.ts +1 -0
  54. package/dist/types/sandbox/dev-source.d.ts +2 -2
  55. package/dist/types/sandbox/hidden-paths.d.ts +1 -0
  56. package/dist/types/sandbox/index.d.ts +1 -1
  57. package/dist/types/sandbox/policy.d.ts +3 -0
  58. package/dist/types/typevault/cli-path.d.ts +2 -0
  59. package/dist/types/typevault/envelope-store.d.ts +11 -0
  60. package/dist/types/typevault/lifecycle.d.ts +13 -0
  61. package/dist/types/typevault/vault.d.ts +27 -0
  62. package/package.json +8 -7
  63. package/scripts/test.ts +252 -0
  64. package/src/agent/bash-sandbox-env.ts +33 -1
  65. package/src/agent/bash-sandbox.ts +20 -6
  66. package/src/agent/index.ts +120 -31
  67. package/src/agent/multimodal/look-at.ts +1 -1
  68. package/src/agent/plugin-tools.ts +35 -1
  69. package/src/agent/prompt-cache-key.ts +26 -3
  70. package/src/agent/session-origin.ts +39 -18
  71. package/src/agent/session-tail.ts +28 -0
  72. package/src/agent/subagents.ts +21 -2
  73. package/src/agent/system-prompt-composition.ts +18 -0
  74. package/src/agent/system-prompt.ts +39 -1
  75. package/src/agent/tool-error-remediation.ts +18 -3
  76. package/src/agent/tools/channel-disengage.ts +1 -1
  77. package/src/agent/tools/channel-edit.ts +1 -1
  78. package/src/agent/tools/channel-fetch-attachment.ts +1 -1
  79. package/src/agent/tools/channel-handoff.ts +3 -2
  80. package/src/agent/tools/channel-history.ts +2 -1
  81. package/src/agent/tools/channel-react.ts +1 -1
  82. package/src/agent/tools/channel-read.ts +36 -3
  83. package/src/agent/tools/channel-reply.ts +1 -1
  84. package/src/agent/tools/channel-send.ts +4 -3
  85. package/src/agent/tools/channel-session-search.ts +13 -4
  86. package/src/agent/tools/grant-role.ts +1 -1
  87. package/src/agent/tools/post-github-review.ts +1 -1
  88. package/src/agent/tools/restart.ts +1 -1
  89. package/src/agent/tools/skip-response.ts +1 -1
  90. package/src/agent/tools/spawn-subagent.ts +15 -1
  91. package/src/agent/tools/stream-snapshot.ts +1 -1
  92. package/src/agent/tools/subagent-cancel.ts +1 -1
  93. package/src/agent/tools/subagent-output.ts +1 -1
  94. package/src/agent/tools/todo/index.ts +1 -1
  95. package/src/agent/tools/webfetch/adaptive-html.ts +6 -4
  96. package/src/agent/tools/webfetch/strategies/readability-child.ts +25 -0
  97. package/src/agent/tools/webfetch/strategies/readability-process.ts +185 -0
  98. package/src/agent/tools/webfetch/tool.ts +6 -5
  99. package/src/agent/tools/websearch.ts +1 -1
  100. package/src/browser/dockerfile.ts +1 -1
  101. package/src/browser/fill-secret.ts +92 -35
  102. package/src/browser/registry.ts +6 -6
  103. package/src/bundled-plugins/agent-messenger-cli-auth/command.ts +211 -0
  104. package/src/bundled-plugins/agent-messenger-cli-auth/index.ts +186 -0
  105. package/src/bundled-plugins/backup/index.ts +17 -2
  106. package/src/bundled-plugins/backup/runner.ts +37 -2
  107. package/src/bundled-plugins/browser-agent/browser-agent.ts +51 -14
  108. package/src/bundled-plugins/browser-agent/index.ts +12 -5
  109. package/src/bundled-plugins/managed-browser/index.ts +25 -2
  110. package/src/bundled-plugins/managed-browser/skills/browserskill/browserskill/SKILL.md +13 -5
  111. package/src/bundled-plugins/managed-browser/skills/omowright/omowright/SKILL.md +56 -5
  112. package/src/bundled-plugins/memory/README.md +23 -2
  113. package/src/bundled-plugins/memory/append-fragment.ts +133 -0
  114. package/src/bundled-plugins/memory/append-tool.ts +80 -149
  115. package/src/bundled-plugins/memory/dreaming.ts +2 -0
  116. package/src/bundled-plugins/memory/index.ts +33 -6
  117. package/src/bundled-plugins/memory/memory-logger.ts +133 -11
  118. package/src/bundled-plugins/memory/references/store-reference-tool.ts +56 -32
  119. package/src/bundled-plugins/memory/remember-tool.ts +81 -0
  120. package/src/bundled-plugins/memory/search-tool-hybrid.ts +91 -0
  121. package/src/bundled-plugins/memory/search-tool.ts +58 -12
  122. package/src/bundled-plugins/memory/stream-events.ts +2 -0
  123. package/src/bundled-plugins/memory/transcript-cursor.ts +30 -0
  124. package/src/bundled-plugins/memory/transcript-preload.ts +131 -26
  125. package/src/bundled-plugins/memory/transcript-view.ts +36 -0
  126. package/src/bundled-plugins/memory/watermark.ts +19 -10
  127. package/src/bundled-plugins/reviewer/reviewer.ts +20 -0
  128. package/src/bundled-plugins/security/policies/outbound-secret-scan.ts +3 -6
  129. package/src/bundled-plugins/security/policies/outbound-text.ts +20 -0
  130. package/src/bundled-plugins/security/policies/system-prompt-leak.ts +2 -7
  131. package/src/bundled-plugins/typevault/grants.ts +119 -0
  132. package/src/bundled-plugins/typevault/index.ts +152 -0
  133. package/src/bundled-plugins/typevault/request.ts +87 -0
  134. package/src/bundled-plugins/typevault/runtime.ts +45 -0
  135. package/src/bundled-plugins/typevault/skills/typevault/SKILL.md +65 -0
  136. package/src/channels/adapter-halt.ts +17 -0
  137. package/src/channels/adapters/github/history.ts +23 -2
  138. package/src/channels/adapters/github/inbound.ts +60 -1
  139. package/src/channels/adapters/github/index.ts +27 -5
  140. package/src/channels/adapters/github/reconcile-open-prs.ts +7 -1
  141. package/src/channels/adapters/kakaotalk-author-resolver.ts +7 -5
  142. package/src/channels/adapters/kakaotalk-channel-resolver.ts +45 -15
  143. package/src/channels/adapters/kakaotalk-classify.ts +7 -9
  144. package/src/channels/adapters/kakaotalk-list.ts +62 -0
  145. package/src/channels/adapters/kakaotalk-member-cache.ts +67 -0
  146. package/src/channels/adapters/kakaotalk-membership.ts +27 -10
  147. package/src/channels/adapters/kakaotalk-mention.ts +61 -0
  148. package/src/channels/adapters/kakaotalk.ts +159 -125
  149. package/src/channels/adapters/teams-classify.ts +18 -8
  150. package/src/channels/github-token-bridge.ts +13 -0
  151. package/src/channels/lost-background-context.ts +46 -0
  152. package/src/channels/manager.ts +52 -3
  153. package/src/channels/persistence.ts +5 -0
  154. package/src/channels/router.ts +120 -21
  155. package/src/channels/schema.ts +1 -1
  156. package/src/channels/types.ts +3 -0
  157. package/src/coding-agent/tool.ts +1 -1
  158. package/src/config/config.ts +20 -1
  159. package/src/config/providers.ts +65 -0
  160. package/src/container/start.ts +15 -2
  161. package/src/doctor/channel-checks.ts +33 -0
  162. package/src/doctor/checks.ts +12 -2
  163. package/src/eval/sandbox.ts +3 -0
  164. package/src/eval/tool.ts +1 -0
  165. package/src/git/resolve-identity.ts +67 -2
  166. package/src/git/system-commit.ts +7 -0
  167. package/src/init/dockerfile.ts +5 -1
  168. package/src/init/gitignore.ts +3 -3
  169. package/src/init/index.ts +1 -0
  170. package/src/init/models-dev.ts +2 -0
  171. package/src/init/validate-api-key.ts +5 -0
  172. package/src/migrations/secrets-v1-to-v2.ts +2 -0
  173. package/src/permissions/builtins.ts +8 -0
  174. package/src/plugin/types.ts +6 -0
  175. package/src/run/bundled-plugins.ts +12 -0
  176. package/src/run/channel-session-factory.ts +3 -0
  177. package/src/run/index.ts +13 -2
  178. package/src/sandbox/build.ts +1 -0
  179. package/src/sandbox/canonical-secrets.ts +6 -0
  180. package/src/sandbox/dependency-bins.ts +13 -1
  181. package/src/sandbox/dev-source.ts +21 -12
  182. package/src/sandbox/hidden-paths.ts +1 -1
  183. package/src/sandbox/index.ts +1 -0
  184. package/src/sandbox/policy.ts +5 -0
  185. package/src/secrets/hydrate.ts +4 -1
  186. package/src/secrets/storage.ts +10 -1
  187. package/src/server/command-runner.ts +4 -0
  188. package/src/server/index.ts +8 -1
  189. package/src/skills/typeclaw-config/SKILL.md +16 -0
  190. package/src/skills/typeclaw-memory/SKILL.md +3 -1
  191. package/src/skills/typeclaw-permissions/SKILL.md +8 -8
  192. package/src/skills/typeclaw-troubleshooting/SKILL.md +18 -1
  193. package/src/tunnels/active.ts +14 -0
  194. package/src/typevault/bash-cli.ts +18 -0
  195. package/src/typevault/cli-path.ts +11 -0
  196. package/src/typevault/envelope-helper.ts +17 -0
  197. package/src/typevault/envelope-store.ts +98 -0
  198. package/src/typevault/lifecycle.ts +132 -0
  199. package/src/typevault/prompt-tunnel.ts +33 -0
  200. package/src/typevault/redaction.ts +37 -0
  201. package/src/typevault/vault.ts +283 -0
  202. package/typeclaw.schema.json +40 -10
@@ -0,0 +1,185 @@
1
+ import { readFileSync } from 'node:fs'
2
+ import { join } from 'node:path'
3
+
4
+ import type { ReadabilityDetailedResult } from './readability'
5
+
6
+ export type ReadabilityChildRequest = { html: string; url: string }
7
+ export type ReadabilityChildResponse = { ok: true; result: ReadabilityDetailedResult } | { ok: false; message: string }
8
+
9
+ export type ReadabilityChildOutcome =
10
+ | { kind: 'ok'; result: ReadabilityDetailedResult }
11
+ | { kind: 'error'; message: string }
12
+ | { kind: 'memory'; childRssBytes: number | null; availableBytes: number | null }
13
+ | { kind: 'aborted' }
14
+ | { kind: 'crashed'; exitCode: number | null; signalCode: string | null }
15
+
16
+ type StopReason = Extract<ReadabilityChildOutcome, { kind: 'memory' | 'aborted' }>
17
+
18
+ export type MemoryProbe = {
19
+ readChildRssBytes: (pid: number) => number | null
20
+ readAvailableBytes: () => number | null
21
+ }
22
+
23
+ export type ReadabilityChildOptions = {
24
+ signal?: AbortSignal
25
+ probe?: MemoryProbe
26
+ onSpawn?: (pid: number) => void
27
+ }
28
+
29
+ export const MEMORY_RESERVE_BYTES = 512 * 1024 * 1024
30
+ export const MIN_KILLABLE_CHILD_RSS_BYTES = 256 * 1024 * 1024
31
+ const POLL_INTERVAL_MS = 100
32
+ const CHILD_ENTRY = join(import.meta.dir, 'readability-child.ts')
33
+
34
+ export const READABILITY_MEMORY_MESSAGE =
35
+ 'Readability extraction stopped: parsing this page would exhaust the memory available to the agent. Browser retrieval may be needed for this page.'
36
+ export const READABILITY_CRASH_MESSAGE = 'Readability extraction failed: the page parser exited unexpectedly.'
37
+ export const READABILITY_ABORT_MESSAGE = 'Readability extraction was aborted.'
38
+
39
+ // jsdom can allocate tens of gigabytes inside one synchronous getComputedStyle()
40
+ // call (a 289KB Starlight docs page reached 24-32GB), and memory it frees is not
41
+ // returned to the OS, so every in-process parse ratchets the runtime's RSS. A
42
+ // disposable child process returns all of it on exit and can be killed mid-call,
43
+ // which neither the abort signal nor any in-process limit can do.
44
+ export async function applyReadabilityIsolated(
45
+ html: string,
46
+ url: string,
47
+ signal?: AbortSignal,
48
+ ): Promise<ReadabilityDetailedResult> {
49
+ const outcome = await runReadabilityChild({ html, url }, signal === undefined ? {} : { signal })
50
+ switch (outcome.kind) {
51
+ case 'ok':
52
+ return outcome.result
53
+ case 'error':
54
+ throw new Error(outcome.message)
55
+ case 'memory':
56
+ console.warn(
57
+ `[webfetch] readability parser stopped near memory exhaustion host=${hostOf(url)} child_rss_mb=${toMb(outcome.childRssBytes)} available_mb=${toMb(outcome.availableBytes)}`,
58
+ )
59
+ throw new Error(READABILITY_MEMORY_MESSAGE)
60
+ case 'aborted':
61
+ throw new Error(READABILITY_ABORT_MESSAGE)
62
+ case 'crashed':
63
+ console.warn(
64
+ `[webfetch] readability parser exited unexpectedly host=${hostOf(url)} exit_code=${outcome.exitCode} signal=${outcome.signalCode}`,
65
+ )
66
+ throw new Error(READABILITY_CRASH_MESSAGE)
67
+ }
68
+ }
69
+
70
+ export async function runReadabilityChild(
71
+ request: ReadabilityChildRequest,
72
+ options: ReadabilityChildOptions = {},
73
+ ): Promise<ReadabilityChildOutcome> {
74
+ const { signal } = options
75
+ if (signal?.aborted) return { kind: 'aborted' }
76
+ const probe = options.probe ?? defaultMemoryProbe
77
+
78
+ const child = Bun.spawn([process.execPath, '--smol', CHILD_ENTRY], {
79
+ stdin: new Blob([JSON.stringify(request)]),
80
+ stdout: 'pipe',
81
+ stderr: 'inherit',
82
+ env: {},
83
+ })
84
+
85
+ let stop = null as StopReason | null
86
+ const kill = (reason: StopReason): void => {
87
+ if (stop !== null) return
88
+ stop = reason
89
+ child.kill('SIGKILL')
90
+ }
91
+ const onAbort = (): void => kill({ kind: 'aborted' })
92
+ signal?.addEventListener('abort', onAbort, { once: true })
93
+ const watchdog = setInterval(() => {
94
+ const childRssBytes = probe.readChildRssBytes(child.pid)
95
+ const availableBytes = probe.readAvailableBytes()
96
+ if (shouldStopChild(childRssBytes, availableBytes)) kill({ kind: 'memory', childRssBytes, availableBytes })
97
+ }, POLL_INTERVAL_MS)
98
+ options.onSpawn?.(child.pid)
99
+ if (signal?.aborted) onAbort()
100
+
101
+ try {
102
+ const [stdout] = await Promise.all([new Response(child.stdout).text(), child.exited])
103
+ if (stop !== null) return stop
104
+ if (child.exitCode !== 0) {
105
+ // Anything that SIGKILLs the parser without us asking is the kernel OOM
106
+ // killer: the child raised its own oom_score_adj so it is chosen first.
107
+ if (child.signalCode === 'SIGKILL') return { kind: 'memory', childRssBytes: null, availableBytes: null }
108
+ return { kind: 'crashed', exitCode: child.exitCode, signalCode: child.signalCode }
109
+ }
110
+ const response = parseResponse(stdout)
111
+ if (response === null) return { kind: 'crashed', exitCode: child.exitCode, signalCode: child.signalCode }
112
+ return response.ok ? { kind: 'ok', result: response.result } : { kind: 'error', message: response.message }
113
+ } finally {
114
+ clearInterval(watchdog)
115
+ signal?.removeEventListener('abort', onAbort)
116
+ }
117
+ }
118
+
119
+ // Stopping only when the machine is about to run out keeps every parse that
120
+ // succeeds today succeeding: a parse is cut short only where it would otherwise
121
+ // have taken the agent runtime down with it. The RSS floor keeps a normal-sized
122
+ // parse alive when something else is what is exhausting memory.
123
+ export function shouldStopChild(childRssBytes: number | null, availableBytes: number | null): boolean {
124
+ if (childRssBytes === null || availableBytes === null) return false
125
+ return childRssBytes >= MIN_KILLABLE_CHILD_RSS_BYTES && availableBytes < MEMORY_RESERVE_BYTES
126
+ }
127
+
128
+ export const defaultMemoryProbe: MemoryProbe = {
129
+ readChildRssBytes: (pid) => {
130
+ if (process.platform !== 'linux') return null
131
+ const status = readOptional(`/proc/${pid}/status`)
132
+ const kb = status?.match(/^VmRSS:\s+(\d+)\s+kB$/m)?.[1]
133
+ return kb === undefined ? null : Number(kb) * 1024
134
+ },
135
+ readAvailableBytes: () => (process.platform === 'linux' ? readLinuxAvailableBytes() : null),
136
+ }
137
+
138
+ // The tighter of two ceilings: the container's cgroup limit, and the host or VM
139
+ // itself. Both matter because sibling containers can exhaust a shared Docker VM
140
+ // while every container is still under its own limit, which is how this OOM
141
+ // actually happened.
142
+ export function readLinuxAvailableBytes(root = ''): number | null {
143
+ const candidates: number[] = []
144
+ const meminfoKb = readOptional(`${root}/proc/meminfo`)?.match(/^MemAvailable:\s+(\d+)\s+kB$/m)?.[1]
145
+ if (meminfoKb !== undefined) candidates.push(Number(meminfoKb) * 1024)
146
+
147
+ const max = readOptional(`${root}/sys/fs/cgroup/memory.max`)?.trim()
148
+ const current = readOptional(`${root}/sys/fs/cgroup/memory.current`)?.trim()
149
+ if (max !== undefined && max !== 'max' && current !== undefined) {
150
+ // Inactive page cache is reclaimable before the cgroup OOMs, the same
151
+ // adjustment `docker stats` applies.
152
+ const inactiveFile = readOptional(`${root}/sys/fs/cgroup/memory.stat`)?.match(/^inactive_file (\d+)$/m)?.[1]
153
+ candidates.push(Number(max) - Number(current) + Number(inactiveFile ?? 0))
154
+ }
155
+
156
+ return candidates.length === 0 ? null : Math.min(...candidates)
157
+ }
158
+
159
+ function parseResponse(stdout: string): ReadabilityChildResponse | null {
160
+ try {
161
+ return JSON.parse(stdout) as ReadabilityChildResponse
162
+ } catch {
163
+ return null
164
+ }
165
+ }
166
+
167
+ function readOptional(path: string): string | undefined {
168
+ try {
169
+ return readFileSync(path, 'utf8')
170
+ } catch {
171
+ return undefined
172
+ }
173
+ }
174
+
175
+ function hostOf(url: string): string {
176
+ try {
177
+ return new URL(url).host
178
+ } catch {
179
+ return 'invalid-url'
180
+ }
181
+ }
182
+
183
+ function toMb(bytes: number | null): string {
184
+ return bytes === null ? '?' : String(Math.round(bytes / (1024 * 1024)))
185
+ }
@@ -1,12 +1,12 @@
1
- import { Type } from '@earendil-works/pi-ai'
2
1
  import { defineTool } from '@earendil-works/pi-coding-agent'
2
+ import { Type } from 'typebox'
3
3
 
4
4
  import { extractAdaptiveHtml, validateAdaptiveHtmlComplexity } from './adaptive-html'
5
5
  import { fetchWithLimits, normalizeUrl, parseMimeType, WebFetchError } from './fetch'
6
6
  import { applyGrep, GrepError } from './strategies/grep'
7
7
  import { applyJq, JqError } from './strategies/jq'
8
8
  import { applyRaw } from './strategies/raw'
9
- import { applyReadability } from './strategies/readability'
9
+ import { applyReadabilityIsolated } from './strategies/readability-process'
10
10
  import { applySelector, SelectorError } from './strategies/selector'
11
11
  import { applySnapshot } from './strategies/snapshot'
12
12
  import {
@@ -135,7 +135,7 @@ export const webFetchTool = defineTool({
135
135
  let extractionAttempts: HtmlExtractionAttempt[] | undefined
136
136
  try {
137
137
  if (strategy === 'readability' && autoDetected) {
138
- const extracted = await extractAdaptiveHtml(response.body, response.finalUrl)
138
+ const extracted = await extractAdaptiveHtml(response.body, response.finalUrl, undefined, signal)
139
139
  extractionAttempts = extracted.attempts
140
140
  if (extracted.kind === 'error') {
141
141
  return errorResult(normalizedUrl, extracted.message, {
@@ -165,7 +165,7 @@ export const webFetchTool = defineTool({
165
165
  })
166
166
  }
167
167
  }
168
- output = await runStrategy(strategy, response.body, response.finalUrl, params)
168
+ output = await runStrategy(strategy, response.body, response.finalUrl, params, signal)
169
169
  }
170
170
  } catch (error) {
171
171
  const message = error instanceof Error ? error.message : String(error)
@@ -262,12 +262,13 @@ async function runStrategy(
262
262
  body: string,
263
263
  url: string,
264
264
  params: WebFetchParams,
265
+ signal: AbortSignal | undefined,
265
266
  ): Promise<string> {
266
267
  switch (strategy) {
267
268
  case 'raw':
268
269
  return applyRaw(body)
269
270
  case 'readability':
270
- return await applyReadability(body, url)
271
+ return (await applyReadabilityIsolated(body, url, signal)).output
271
272
  case 'jq':
272
273
  try {
273
274
  return await applyJq(body, params.query ?? '')
@@ -1,5 +1,5 @@
1
- import { Type } from '@earendil-works/pi-ai'
2
1
  import { defineTool } from '@earendil-works/pi-coding-agent'
2
+ import { Type } from 'typebox'
3
3
 
4
4
  import { CurlImpersonateError, isCurlExitTimeout } from './curl-impersonate'
5
5
  import { ddgSearch, DdgCaptchaError, type DdgResult } from './ddg'
@@ -35,7 +35,7 @@ RUN mkdir -p ${root} \\
35
35
  && cd ${root} \\
36
36
  && printf '{}' > package.json \\
37
37
  && BUN_INSTALL_CACHE_DIR=/tmp/typeclaw-browser-bun-cache bun add ${spec} \\
38
- && printf '%s\\n' 'export * from "${root}/node_modules/${id}/src/index.js"' > ${module} \\
38
+ && printf '%s\\n' 'export * from "${id}"' > ${module} \\
39
39
  && rm -rf /tmp/typeclaw-browser-bun-cache \\
40
40
  && chmod -R a+rX ${root} \\
41
41
  && bun -e 'await import("${module}")'`
@@ -4,6 +4,9 @@
4
4
 
5
5
  const COMMAND_TIMEOUT_MS = 10_000
6
6
  const DEFAULT_NAVIGATION_TIMEOUT_MS = 15_000
7
+ // How long a real Enter gets to start a navigation before the form is
8
+ // submitted directly instead.
9
+ const ENTER_GRACE_MS = 2_000
7
10
 
8
11
  export type FillSecretOptions = {
9
12
  cdpUrl: string
@@ -24,13 +27,8 @@ export async function fillSecretIntoFocusedField(options: FillSecretOptions): Pr
24
27
  try {
25
28
  const target = await pickTarget(cdp, options.url)
26
29
  if ('reason' in target) return { status: 'refused', reason: target.reason }
27
- const { sessionId } = (await cdp.send('Target.attachToTarget', { targetId: target.targetId, flatten: true })) as {
28
- sessionId: string
29
- }
30
- // Background tabs (omowright opens them) report no focus; emulate it so
31
- // keyboard input reaches the focused element without stealing the window.
32
- await cdp.send('Emulation.setFocusEmulationEnabled', { enabled: true }, sessionId)
33
- const focused = await inspectFocus(cdp, sessionId)
30
+ const sessionId = target.sessionId ?? (await attachWithFocus(cdp, target.targetId))
31
+ const focused = target.focus ?? (await inspectFocus(cdp, sessionId))
34
32
  if (focused.kind === 'none') {
35
33
  return {
36
34
  status: 'refused',
@@ -44,13 +42,40 @@ export async function fillSecretIntoFocusedField(options: FillSecretOptions): Pr
44
42
  if (!options.submit) return { status: 'filled', url: target.url, field: focused.field }
45
43
 
46
44
  await cdp.send('Page.enable', {}, sessionId)
45
+ const timeoutMs = options.navigationTimeoutMs ?? DEFAULT_NAVIGATION_TIMEOUT_MS
46
+ const { frameTree } = (await cdp.send('Page.getFrameTree', {}, sessionId)) as {
47
+ frameTree: { frame: { id: string } }
48
+ }
47
49
  const navigated = cdp.waitForEvent(
48
50
  'Page.frameNavigated',
49
51
  sessionId,
50
52
  (params) => (params as { frame?: { parentId?: string } }).frame?.parentId === undefined,
51
- options.navigationTimeoutMs ?? DEFAULT_NAVIGATION_TIMEOUT_MS,
53
+ timeoutMs,
54
+ )
55
+ const startedLoading = cdp.waitForEvent(
56
+ 'Page.frameStartedLoading',
57
+ sessionId,
58
+ (params) => (params as { frameId?: string }).frameId === frameTree.frame.id,
59
+ Math.min(ENTER_GRACE_MS, timeoutMs / 2),
52
60
  )
53
- await submitField(cdp, sessionId, focused.objectId)
61
+ // Enter first: script-driven logins (encrypt-then-submit from a keyup or
62
+ // button handler) cancel a direct form submit, while a real keystroke
63
+ // runs their handlers and still triggers implicit submission elsewhere.
64
+ // Once a load starts the old document (and the field handle) may be gone,
65
+ // so it is only probed after the grace period passes with no load. The
66
+ // submit-seen check and the fallback submit run in one browser-side call
67
+ // so a handler that submits late in the grace period is not doubled.
68
+ await callOnField(cdp, sessionId, focused.objectId, WATCH_SUBMIT_FUNCTION)
69
+ await pressEnter(cdp, sessionId)
70
+ if (!(await startedLoading)) {
71
+ try {
72
+ await callOnField(cdp, sessionId, focused.objectId, SUBMIT_UNLESS_SEEN_FUNCTION)
73
+ } catch (error) {
74
+ // A navigation that began right after the grace period destroys the
75
+ // handle; that is a successful submit, not a failure.
76
+ if (!(await navigated)) throw error
77
+ }
78
+ }
54
79
  if (await navigated) return { status: 'submitted', url: target.url, field: focused.field }
55
80
  } catch (error) {
56
81
  // The value may already be in the field; a failed step must not leave it there.
@@ -69,18 +94,43 @@ export async function fillSecretIntoFocusedField(options: FillSecretOptions): Pr
69
94
 
70
95
  type TargetInfo = { targetId: string; type: string; url: string }
71
96
 
72
- async function pickTarget(
73
- cdp: CdpConnection,
74
- url: string | undefined,
75
- ): Promise<{ targetId: string; url: string } | { reason: string }> {
97
+ type PickedTarget = { targetId: string; url: string; sessionId?: string; focus?: FocusInfo & { kind: 'editable' } }
98
+
99
+ async function pickTarget(cdp: CdpConnection, url: string | undefined): Promise<PickedTarget | { reason: string }> {
76
100
  const { targetInfos } = (await cdp.send('Target.getTargets')) as { targetInfos: TargetInfo[] }
77
101
  const pages = targetInfos.filter((t) => t.type === 'page' && !/^(devtools|chrome|chrome-extension):/.test(t.url))
78
- const matches = url === undefined ? pages : pages.filter((t) => t.url === url || t.url.startsWith(url))
102
+ const prefixed = url === undefined ? pages : pages.filter((t) => t.url === url || t.url.startsWith(url))
103
+ // An exact URL names one tab even when it is also a prefix of its siblings'
104
+ // URLs (`/` vs `/login`).
105
+ const exact = url === undefined ? [] : prefixed.filter((t) => t.url === url)
106
+ const matches = exact.length > 0 ? exact : prefixed
79
107
  if (matches.length === 1) return matches[0]!
80
108
  if (matches.length === 0) {
81
109
  return { reason: url === undefined ? 'the shared browser has no open tab' : `no open tab matches ${url}` }
82
110
  }
83
- return { reason: `several tabs match; pass \`url\` to pick one of: ${matches.map((t) => t.url).join(', ')}` }
111
+ // Duplicate tabs of one site are common after a retry opens a fresh tab, and
112
+ // no URL can tell them apart. The secret goes into the focused field, so the
113
+ // one tab whose focus sits in an editable field is the one just clicked.
114
+ const focused: PickedTarget[] = []
115
+ for (const t of matches) {
116
+ const sessionId = await attachWithFocus(cdp, t.targetId)
117
+ const focus = await inspectFocus(cdp, sessionId)
118
+ if (focus.kind === 'editable') focused.push({ targetId: t.targetId, url: t.url, sessionId, focus })
119
+ }
120
+ if (focused.length === 1) return focused[0]!
121
+ return {
122
+ reason: `several tabs match and ${focused.length === 0 ? 'none has' : 'more than one has'} a focused editable field; click the field first, or close the duplicate tabs, or pass the exact \`url\` of one of: ${matches.map((t) => t.url).join(', ')}`,
123
+ }
124
+ }
125
+
126
+ async function attachWithFocus(cdp: CdpConnection, targetId: string): Promise<string> {
127
+ const { sessionId } = (await cdp.send('Target.attachToTarget', { targetId, flatten: true })) as {
128
+ sessionId: string
129
+ }
130
+ // Background tabs (omowright opens them) report no focus; emulate it so
131
+ // keyboard input reaches the focused element without stealing the window.
132
+ await cdp.send('Emulation.setFocusEmulationEnabled', { enabled: true }, sessionId)
133
+ return sessionId
84
134
  }
85
135
 
86
136
  type FocusInfo = { kind: 'editable'; field: string; objectId?: string } | { kind: 'none'; field?: string }
@@ -177,27 +227,34 @@ async function pressCommand(cdp: CdpConnection, sessionId: string, command: stri
177
227
  await cdp.send('Input.dispatchKeyEvent', { type: 'keyUp' }, sessionId)
178
228
  }
179
229
 
230
+ const WATCH_SUBMIT_FUNCTION = `function () {
231
+ const form = this.form
232
+ if (!form) return
233
+ form.__typeclawSubmitSeen = false
234
+ form.addEventListener('submit', () => { form.__typeclawSubmitSeen = true }, { capture: true, once: true })
235
+ }`
236
+
180
237
  // A form without a submit button and with several fields ignores Enter (HTML
181
- // implicit submission), so submit the owning form directly when there is one;
182
- // script-driven logins with no form still get the Enter keystroke.
183
- async function submitField(cdp: CdpConnection, sessionId: string, objectId: string | undefined): Promise<void> {
184
- if (objectId !== undefined) {
185
- const submitted = (await cdp.send(
186
- 'Runtime.callFunctionOn',
187
- {
188
- objectId,
189
- functionDeclaration: `function () {
190
- if (!this.form) return false
191
- this.form.requestSubmit()
192
- return true
193
- }`,
194
- returnByValue: true,
195
- },
196
- sessionId,
197
- )) as { result: { value?: boolean } }
198
- if (submitted.result.value === true) return
199
- }
200
- await pressEnter(cdp, sessionId)
238
+ // implicit submission), so it is submitted directly when Enter did nothing.
239
+ const SUBMIT_UNLESS_SEEN_FUNCTION = `function () {
240
+ const form = this.form
241
+ if (!this.isConnected || !form || form.__typeclawSubmitSeen === true) return
242
+ form.requestSubmit()
243
+ }`
244
+
245
+ async function callOnField(
246
+ cdp: CdpConnection,
247
+ sessionId: string,
248
+ objectId: string | undefined,
249
+ functionDeclaration: string,
250
+ ): Promise<unknown> {
251
+ if (objectId === undefined) return undefined
252
+ const result = (await cdp.send(
253
+ 'Runtime.callFunctionOn',
254
+ { objectId, functionDeclaration, returnByValue: true },
255
+ sessionId,
256
+ )) as { result?: { value?: unknown } }
257
+ return result.result?.value
201
258
  }
202
259
 
203
260
  async function pressEnter(cdp: CdpConnection, sessionId: string): Promise<void> {
@@ -46,9 +46,9 @@ export const BSK_DAEMON_PORT = 52800
46
46
 
47
47
  export const OMOWRIGHT_MODULE = `${BROWSER_ROOT}/omowright/index.js`
48
48
 
49
- const OMOWRIGHT_COMMIT = 'a3f0c2b54fc41804beca7eb6ab26ee54804742a9'
49
+ const OMOWRIGHT_VERSION = '0.3.0'
50
50
  const BROWSER_USE_VERSION = '0.13.11'
51
- const BSK_VERSION = '0.3.3'
51
+ const BSK_VERSION = '0.4.2'
52
52
  const BSK_RELEASES = 'https://github.com/Tencent/BrowserSkill/releases/download'
53
53
 
54
54
  export const BROWSER_ENGINES: Record<BrowserEngineId, BrowserEngineSpec> = {
@@ -59,7 +59,7 @@ export const BROWSER_ENGINES: Record<BrowserEngineId, BrowserEngineSpec> = {
59
59
  },
60
60
  omowright: {
61
61
  id: 'omowright',
62
- install: { kind: 'bun-global', spec: `github:code-yeongyu/omowright#${OMOWRIGHT_COMMIT}` },
62
+ install: { kind: 'bun-global', spec: `omowright@${OMOWRIGHT_VERSION}` },
63
63
  env: (cdpUrl) => ({ OMOWRIGHT_CDP_URL: cdpUrl }),
64
64
  },
65
65
  'browser-use': {
@@ -75,18 +75,18 @@ export const BROWSER_EXTENSIONS: Record<BrowserExtensionId, BrowserExtensionSpec
75
75
  version: BSK_VERSION,
76
76
  zip: {
77
77
  url: `${BSK_RELEASES}/ext-v${BSK_VERSION}/browser-skill-extension-v${BSK_VERSION}-chrome.zip`,
78
- sha256: '10ef76e68f352893fa22dd4cce2f1b96cb90ed957b815d656e13180d07d3c6ea',
78
+ sha256: '5360ef01ec8fcd3550215e12c417d8b98814bff0e54d478e6b867d693c1d4d76',
79
79
  },
80
80
  cli: {
81
81
  name: 'bsk',
82
82
  urlByArch: {
83
83
  amd64: {
84
84
  url: `${BSK_RELEASES}/cli-v${BSK_VERSION}/bsk-v${BSK_VERSION}-x86_64-unknown-linux-musl.tar.gz`,
85
- sha256: '141b73489bec99d768cf18042919f0e054c9f614e6ecdfd754028d2b606e9106',
85
+ sha256: '5286ccdc104e52200c47b4a492c6e043069e5bf1e638db8fdbde62a9d4939979',
86
86
  },
87
87
  arm64: {
88
88
  url: `${BSK_RELEASES}/cli-v${BSK_VERSION}/bsk-v${BSK_VERSION}-aarch64-unknown-linux-musl.tar.gz`,
89
- sha256: '747e4c416da46650c7d8127a47dd8f08e813e0f69366c4db1af226d0b63a575d',
89
+ sha256: 'f965474d8ad61cfb819b5123f4355135ee2afa9c4f742bbbe9e4f463dc7d5fa3',
90
90
  },
91
91
  },
92
92
  },