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.
- package/dist/types/agent/bash-sandbox-env.d.ts +2 -0
- package/dist/types/agent/bash-sandbox.d.ts +4 -0
- package/dist/types/agent/index.d.ts +5 -0
- package/dist/types/agent/multimodal/look-at.d.ts +1 -1
- package/dist/types/agent/plugin-tools.d.ts +4 -0
- package/dist/types/agent/prompt-cache-key.d.ts +1 -0
- package/dist/types/agent/session-tail.d.ts +12 -0
- package/dist/types/agent/subagents.d.ts +5 -0
- package/dist/types/agent/system-prompt-composition.d.ts +1 -0
- package/dist/types/agent/system-prompt.d.ts +2 -0
- package/dist/types/agent/tools/channel-disengage.d.ts +1 -1
- package/dist/types/agent/tools/channel-edit.d.ts +1 -1
- package/dist/types/agent/tools/channel-fetch-attachment.d.ts +1 -1
- package/dist/types/agent/tools/channel-handoff.d.ts +1 -1
- package/dist/types/agent/tools/channel-history.d.ts +1 -1
- package/dist/types/agent/tools/channel-react.d.ts +1 -1
- package/dist/types/agent/tools/channel-read.d.ts +10 -2
- package/dist/types/agent/tools/channel-reply.d.ts +1 -1
- package/dist/types/agent/tools/channel-send.d.ts +1 -1
- package/dist/types/agent/tools/channel-session-search.d.ts +1 -1
- package/dist/types/agent/tools/grant-role.d.ts +1 -1
- package/dist/types/agent/tools/post-github-review.d.ts +1 -1
- package/dist/types/agent/tools/restart.d.ts +1 -1
- package/dist/types/agent/tools/skip-response.d.ts +1 -1
- package/dist/types/agent/tools/spawn-subagent.d.ts +2 -1
- package/dist/types/agent/tools/stream-snapshot.d.ts +1 -1
- package/dist/types/agent/tools/subagent-cancel.d.ts +1 -1
- package/dist/types/agent/tools/subagent-output.d.ts +1 -1
- package/dist/types/agent/tools/todo/index.d.ts +1 -1
- package/dist/types/agent/tools/webfetch/adaptive-html.d.ts +3 -3
- package/dist/types/agent/tools/webfetch/strategies/readability-process.d.ts +48 -0
- package/dist/types/agent/tools/webfetch/tool.d.ts +2 -2
- package/dist/types/agent/tools/websearch.d.ts +1 -1
- package/dist/types/channels/adapters/github/auth-app.d.ts +25 -0
- package/dist/types/channels/adapters/github/auth-pat.d.ts +17 -0
- package/dist/types/channels/adapters/github/auth.d.ts +24 -0
- package/dist/types/channels/github-token-bridge.d.ts +4 -0
- package/dist/types/channels/lost-background-context.d.ts +2 -0
- package/dist/types/channels/persistence.d.ts +1 -0
- package/dist/types/channels/router.d.ts +5 -1
- package/dist/types/channels/types.d.ts +1 -0
- package/dist/types/coding-agent/tool.d.ts +1 -1
- package/dist/types/config/config.d.ts +20 -11
- package/dist/types/config/providers.d.ts +47 -0
- package/dist/types/eval/sandbox.d.ts +2 -0
- package/dist/types/git/resolve-identity.d.ts +2 -0
- package/dist/types/init/dockerfile.d.ts +1 -0
- package/dist/types/init/gitignore.d.ts +1 -1
- package/dist/types/permissions/builtins.d.ts +1 -0
- package/dist/types/plugin/types.d.ts +3 -0
- package/dist/types/sandbox/canonical-secrets.d.ts +2 -2
- package/dist/types/sandbox/coding-agent.d.ts +1 -1
- package/dist/types/sandbox/dependency-bins.d.ts +1 -0
- package/dist/types/sandbox/dev-source.d.ts +2 -2
- package/dist/types/sandbox/hidden-paths.d.ts +1 -0
- package/dist/types/sandbox/index.d.ts +1 -1
- package/dist/types/sandbox/policy.d.ts +3 -0
- package/dist/types/typevault/cli-path.d.ts +2 -0
- package/dist/types/typevault/envelope-store.d.ts +11 -0
- package/dist/types/typevault/lifecycle.d.ts +13 -0
- package/dist/types/typevault/vault.d.ts +27 -0
- package/package.json +8 -7
- package/scripts/test.ts +252 -0
- package/src/agent/bash-sandbox-env.ts +33 -1
- package/src/agent/bash-sandbox.ts +20 -6
- package/src/agent/index.ts +120 -31
- package/src/agent/multimodal/look-at.ts +1 -1
- package/src/agent/plugin-tools.ts +35 -1
- package/src/agent/prompt-cache-key.ts +26 -3
- package/src/agent/session-origin.ts +39 -18
- package/src/agent/session-tail.ts +28 -0
- package/src/agent/subagents.ts +21 -2
- package/src/agent/system-prompt-composition.ts +18 -0
- package/src/agent/system-prompt.ts +39 -1
- package/src/agent/tool-error-remediation.ts +18 -3
- package/src/agent/tools/channel-disengage.ts +1 -1
- package/src/agent/tools/channel-edit.ts +1 -1
- package/src/agent/tools/channel-fetch-attachment.ts +1 -1
- package/src/agent/tools/channel-handoff.ts +3 -2
- package/src/agent/tools/channel-history.ts +2 -1
- package/src/agent/tools/channel-react.ts +1 -1
- package/src/agent/tools/channel-read.ts +36 -3
- package/src/agent/tools/channel-reply.ts +1 -1
- package/src/agent/tools/channel-send.ts +4 -3
- package/src/agent/tools/channel-session-search.ts +13 -4
- package/src/agent/tools/grant-role.ts +1 -1
- package/src/agent/tools/post-github-review.ts +1 -1
- package/src/agent/tools/restart.ts +1 -1
- package/src/agent/tools/skip-response.ts +1 -1
- package/src/agent/tools/spawn-subagent.ts +15 -1
- package/src/agent/tools/stream-snapshot.ts +1 -1
- package/src/agent/tools/subagent-cancel.ts +1 -1
- package/src/agent/tools/subagent-output.ts +1 -1
- package/src/agent/tools/todo/index.ts +1 -1
- package/src/agent/tools/webfetch/adaptive-html.ts +6 -4
- package/src/agent/tools/webfetch/strategies/readability-child.ts +25 -0
- package/src/agent/tools/webfetch/strategies/readability-process.ts +185 -0
- package/src/agent/tools/webfetch/tool.ts +6 -5
- package/src/agent/tools/websearch.ts +1 -1
- package/src/browser/dockerfile.ts +1 -1
- package/src/browser/fill-secret.ts +92 -35
- package/src/browser/registry.ts +6 -6
- package/src/bundled-plugins/agent-messenger-cli-auth/command.ts +211 -0
- package/src/bundled-plugins/agent-messenger-cli-auth/index.ts +186 -0
- package/src/bundled-plugins/backup/index.ts +17 -2
- package/src/bundled-plugins/backup/runner.ts +37 -2
- package/src/bundled-plugins/browser-agent/browser-agent.ts +51 -14
- package/src/bundled-plugins/browser-agent/index.ts +12 -5
- package/src/bundled-plugins/managed-browser/index.ts +25 -2
- package/src/bundled-plugins/managed-browser/skills/browserskill/browserskill/SKILL.md +13 -5
- package/src/bundled-plugins/managed-browser/skills/omowright/omowright/SKILL.md +56 -5
- package/src/bundled-plugins/memory/README.md +23 -2
- package/src/bundled-plugins/memory/append-fragment.ts +133 -0
- package/src/bundled-plugins/memory/append-tool.ts +80 -149
- package/src/bundled-plugins/memory/dreaming.ts +2 -0
- package/src/bundled-plugins/memory/index.ts +33 -6
- package/src/bundled-plugins/memory/memory-logger.ts +133 -11
- package/src/bundled-plugins/memory/references/store-reference-tool.ts +56 -32
- package/src/bundled-plugins/memory/remember-tool.ts +81 -0
- package/src/bundled-plugins/memory/search-tool-hybrid.ts +91 -0
- package/src/bundled-plugins/memory/search-tool.ts +58 -12
- package/src/bundled-plugins/memory/stream-events.ts +2 -0
- package/src/bundled-plugins/memory/transcript-cursor.ts +30 -0
- package/src/bundled-plugins/memory/transcript-preload.ts +131 -26
- package/src/bundled-plugins/memory/transcript-view.ts +36 -0
- package/src/bundled-plugins/memory/watermark.ts +19 -10
- package/src/bundled-plugins/reviewer/reviewer.ts +20 -0
- package/src/bundled-plugins/security/policies/outbound-secret-scan.ts +3 -6
- package/src/bundled-plugins/security/policies/outbound-text.ts +20 -0
- package/src/bundled-plugins/security/policies/system-prompt-leak.ts +2 -7
- package/src/bundled-plugins/typevault/grants.ts +119 -0
- package/src/bundled-plugins/typevault/index.ts +152 -0
- package/src/bundled-plugins/typevault/request.ts +87 -0
- package/src/bundled-plugins/typevault/runtime.ts +45 -0
- package/src/bundled-plugins/typevault/skills/typevault/SKILL.md +65 -0
- package/src/channels/adapter-halt.ts +17 -0
- package/src/channels/adapters/github/history.ts +23 -2
- package/src/channels/adapters/github/inbound.ts +60 -1
- package/src/channels/adapters/github/index.ts +27 -5
- package/src/channels/adapters/github/reconcile-open-prs.ts +7 -1
- package/src/channels/adapters/kakaotalk-author-resolver.ts +7 -5
- package/src/channels/adapters/kakaotalk-channel-resolver.ts +45 -15
- package/src/channels/adapters/kakaotalk-classify.ts +7 -9
- package/src/channels/adapters/kakaotalk-list.ts +62 -0
- package/src/channels/adapters/kakaotalk-member-cache.ts +67 -0
- package/src/channels/adapters/kakaotalk-membership.ts +27 -10
- package/src/channels/adapters/kakaotalk-mention.ts +61 -0
- package/src/channels/adapters/kakaotalk.ts +159 -125
- package/src/channels/adapters/teams-classify.ts +18 -8
- package/src/channels/github-token-bridge.ts +13 -0
- package/src/channels/lost-background-context.ts +46 -0
- package/src/channels/manager.ts +52 -3
- package/src/channels/persistence.ts +5 -0
- package/src/channels/router.ts +120 -21
- package/src/channels/schema.ts +1 -1
- package/src/channels/types.ts +3 -0
- package/src/coding-agent/tool.ts +1 -1
- package/src/config/config.ts +20 -1
- package/src/config/providers.ts +65 -0
- package/src/container/start.ts +15 -2
- package/src/doctor/channel-checks.ts +33 -0
- package/src/doctor/checks.ts +12 -2
- package/src/eval/sandbox.ts +3 -0
- package/src/eval/tool.ts +1 -0
- package/src/git/resolve-identity.ts +67 -2
- package/src/git/system-commit.ts +7 -0
- package/src/init/dockerfile.ts +5 -1
- package/src/init/gitignore.ts +3 -3
- package/src/init/index.ts +1 -0
- package/src/init/models-dev.ts +2 -0
- package/src/init/validate-api-key.ts +5 -0
- package/src/migrations/secrets-v1-to-v2.ts +2 -0
- package/src/permissions/builtins.ts +8 -0
- package/src/plugin/types.ts +6 -0
- package/src/run/bundled-plugins.ts +12 -0
- package/src/run/channel-session-factory.ts +3 -0
- package/src/run/index.ts +13 -2
- package/src/sandbox/build.ts +1 -0
- package/src/sandbox/canonical-secrets.ts +6 -0
- package/src/sandbox/dependency-bins.ts +13 -1
- package/src/sandbox/dev-source.ts +21 -12
- package/src/sandbox/hidden-paths.ts +1 -1
- package/src/sandbox/index.ts +1 -0
- package/src/sandbox/policy.ts +5 -0
- package/src/secrets/hydrate.ts +4 -1
- package/src/secrets/storage.ts +10 -1
- package/src/server/command-runner.ts +4 -0
- package/src/server/index.ts +8 -1
- package/src/skills/typeclaw-config/SKILL.md +16 -0
- package/src/skills/typeclaw-memory/SKILL.md +3 -1
- package/src/skills/typeclaw-permissions/SKILL.md +8 -8
- package/src/skills/typeclaw-troubleshooting/SKILL.md +18 -1
- package/src/tunnels/active.ts +14 -0
- package/src/typevault/bash-cli.ts +18 -0
- package/src/typevault/cli-path.ts +11 -0
- package/src/typevault/envelope-helper.ts +17 -0
- package/src/typevault/envelope-store.ts +98 -0
- package/src/typevault/lifecycle.ts +132 -0
- package/src/typevault/prompt-tunnel.ts +33 -0
- package/src/typevault/redaction.ts +37 -0
- package/src/typevault/vault.ts +283 -0
- 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 {
|
|
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
|
|
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 "${
|
|
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
|
|
28
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
73
|
-
|
|
74
|
-
|
|
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
|
|
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
|
-
|
|
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
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
if (
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
}
|
|
200
|
-
|
|
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> {
|
package/src/browser/registry.ts
CHANGED
|
@@ -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
|
|
49
|
+
const OMOWRIGHT_VERSION = '0.3.0'
|
|
50
50
|
const BROWSER_USE_VERSION = '0.13.11'
|
|
51
|
-
const BSK_VERSION = '0.
|
|
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: `
|
|
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: '
|
|
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: '
|
|
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: '
|
|
89
|
+
sha256: 'f965474d8ad61cfb819b5123f4355135ee2afa9c4f742bbbe9e4f463dc7d5fa3',
|
|
90
90
|
},
|
|
91
91
|
},
|
|
92
92
|
},
|