@yuridulin/pointer 0.0.0-stage → 0.1.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/README.md +54 -2
- package/package.json +51 -4
- package/server/acp/catalog.ts +111 -0
- package/server/acp/connection.ts +233 -0
- package/server/acp/fake-agent.ts +408 -0
- package/server/acp/protocol.ts +167 -0
- package/server/acp/run.ts +139 -0
- package/server/acp/session.ts +1249 -0
- package/server/attachments.ts +115 -0
- package/server/changes.ts +239 -0
- package/server/cli.ts +362 -0
- package/server/compact.ts +361 -0
- package/server/config.ts +176 -0
- package/server/db.ts +462 -0
- package/server/dev.ts +8 -0
- package/server/diff.ts +190 -0
- package/server/engines.ts +63 -0
- package/server/files.ts +254 -0
- package/server/git.ts +319 -0
- package/server/history.ts +425 -0
- package/server/http/attachments.ts +63 -0
- package/server/http/browse.ts +108 -0
- package/server/http/chats.ts +627 -0
- package/server/http/closing.ts +14 -0
- package/server/http/files.ts +115 -0
- package/server/http/git.ts +84 -0
- package/server/http/preview.ts +21 -0
- package/server/http/providers.ts +200 -0
- package/server/http/reply.ts +38 -0
- package/server/hub.ts +521 -0
- package/server/install.ts +169 -0
- package/server/jobs.ts +664 -0
- package/server/linkPreview.ts +172 -0
- package/server/loop.ts +328 -0
- package/server/main.ts +353 -0
- package/server/probe.ts +34 -0
- package/server/process.ts +333 -0
- package/server/prompt.ts +196 -0
- package/server/providers/fake.ts +100 -0
- package/server/providers/opencode.ts +468 -0
- package/server/providers/registry.ts +149 -0
- package/server/providers/store.ts +210 -0
- package/server/segments.ts +248 -0
- package/server/sse.ts +93 -0
- package/server/tools/bash.ts +222 -0
- package/server/tools/edit.ts +254 -0
- package/server/tools/glob.ts +63 -0
- package/server/tools/grep.ts +122 -0
- package/server/tools/ignore.ts +54 -0
- package/server/tools/index.ts +129 -0
- package/server/tools/jail.ts +85 -0
- package/server/tools/jobs.ts +293 -0
- package/server/tools/read.ts +248 -0
- package/server/tools/write.ts +70 -0
- package/server/tray.ps1 +191 -0
- package/server/turn.ts +663 -0
- package/server/watch.ts +94 -0
- package/shared/acpConfig.ts +113 -0
- package/shared/links.ts +122 -0
- package/shared/types.ts +392 -0
- package/web/dist/app.css +1 -0
- package/web/dist/assets/codicon-mxdk0fnd.ttf +0 -0
- package/web/dist/index.html +28 -0
- package/web/dist/main.css +1 -0
- package/web/dist/main.js +1366 -0
- package/web/dist/tokens.css +1 -0
- package/web/dist/workers/css.worker.js +97 -0
- package/web/dist/workers/editor.worker.js +30 -0
- package/web/dist/workers/html.worker.js +300 -0
- package/web/dist/workers/json.worker.js +62 -0
- package/web/dist/workers/ts.worker.js +67733 -0
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import type { LinkPreview } from '../shared/links'
|
|
2
|
+
|
|
3
|
+
const CACHE_TTL = 10 * 60 * 1000
|
|
4
|
+
const CACHE_LIMIT = 100
|
|
5
|
+
const BODY_LIMIT = 256 * 1024
|
|
6
|
+
|
|
7
|
+
const cache = new Map<string, { data: LinkPreview; at: number }>()
|
|
8
|
+
|
|
9
|
+
function decodeEntities(value: string): string {
|
|
10
|
+
return value
|
|
11
|
+
.replace(/&#x([0-9a-f]+);/gi, (_, code: string) => String.fromCodePoint(Number.parseInt(code, 16)))
|
|
12
|
+
.replace(/&#(\d+);/g, (_, code: string) => String.fromCodePoint(Number.parseInt(code, 10)))
|
|
13
|
+
.replace(/ /gi, ' ')
|
|
14
|
+
.replace(/"/gi, '"')
|
|
15
|
+
.replace(/'|'/gi, "'")
|
|
16
|
+
.replace(/</gi, '<')
|
|
17
|
+
.replace(/>/gi, '>')
|
|
18
|
+
.replace(/&/gi, '&')
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function clean(value: string): string {
|
|
22
|
+
return decodeEntities(value).replace(/\s+/g, ' ').trim()
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function attributes(tag: string): Map<string, string> {
|
|
26
|
+
const result = new Map<string, string>()
|
|
27
|
+
const pattern = /([a-zA-Z_:][-a-zA-Z0-9_:.]*)\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s>]+))/g
|
|
28
|
+
for (let match = pattern.exec(tag); match !== null; match = pattern.exec(tag)) {
|
|
29
|
+
const name = (match[1] ?? '').toLowerCase()
|
|
30
|
+
const value = match[2] ?? match[3] ?? match[4] ?? ''
|
|
31
|
+
result.set(name, value)
|
|
32
|
+
}
|
|
33
|
+
return result
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function absolute(value: string, baseUrl: string): string | null {
|
|
37
|
+
try {
|
|
38
|
+
const url = new URL(value, baseUrl)
|
|
39
|
+
return url.protocol === 'http:' || url.protocol === 'https:' ? url.toString() : null
|
|
40
|
+
} catch {
|
|
41
|
+
return null
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function hostnameOf(url: string): string {
|
|
46
|
+
try {
|
|
47
|
+
return new URL(url).hostname
|
|
48
|
+
} catch {
|
|
49
|
+
return ''
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function parsePreviewHtml(html: string, baseUrl: string): LinkPreview {
|
|
54
|
+
const hostname = hostnameOf(baseUrl)
|
|
55
|
+
let pageTitle = ''
|
|
56
|
+
let ogTitle = ''
|
|
57
|
+
let description = ''
|
|
58
|
+
let image: string | null = null
|
|
59
|
+
let icon: string | null = null
|
|
60
|
+
let site = ''
|
|
61
|
+
|
|
62
|
+
const titleMatch = /<title\b[^>]*>([\s\S]*?)<\/title>/i.exec(html)
|
|
63
|
+
if (titleMatch) pageTitle = clean(titleMatch[1] ?? '')
|
|
64
|
+
|
|
65
|
+
const metaPattern = /<meta\b[^>]*>/gi
|
|
66
|
+
for (let match = metaPattern.exec(html); match !== null; match = metaPattern.exec(html)) {
|
|
67
|
+
const attrs = attributes(match[0])
|
|
68
|
+
const key = (attrs.get('property') ?? attrs.get('name') ?? '').toLowerCase()
|
|
69
|
+
const content = clean(attrs.get('content') ?? '')
|
|
70
|
+
if (content === '') continue
|
|
71
|
+
if (key === 'og:title' && ogTitle === '') ogTitle = content
|
|
72
|
+
else if (key === 'og:description' && description === '') description = content
|
|
73
|
+
else if (key === 'og:image' && image === null) image = absolute(content, baseUrl)
|
|
74
|
+
else if (key === 'og:site_name' && site === '') site = content
|
|
75
|
+
else if (key === 'description' && description === '') description = content
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const linkPattern = /<link\b[^>]*>/gi
|
|
79
|
+
for (let match = linkPattern.exec(html); match !== null; match = linkPattern.exec(html)) {
|
|
80
|
+
const attrs = attributes(match[0])
|
|
81
|
+
const rel = (attrs.get('rel') ?? '').toLowerCase().split(/\s+/)
|
|
82
|
+
if (!rel.includes('icon')) continue
|
|
83
|
+
const href = attrs.get('href') ?? ''
|
|
84
|
+
if (href !== '') {
|
|
85
|
+
icon = absolute(href, baseUrl)
|
|
86
|
+
if (icon !== null) break
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const title = ogTitle || pageTitle
|
|
91
|
+
|
|
92
|
+
return {
|
|
93
|
+
url: baseUrl,
|
|
94
|
+
title: (title || hostname).slice(0, 200),
|
|
95
|
+
description: description.slice(0, 500),
|
|
96
|
+
image,
|
|
97
|
+
icon,
|
|
98
|
+
site: (site || hostname).slice(0, 200),
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function minimal(url: string): LinkPreview {
|
|
103
|
+
return {
|
|
104
|
+
url,
|
|
105
|
+
title: '',
|
|
106
|
+
description: '',
|
|
107
|
+
image: null,
|
|
108
|
+
icon: null,
|
|
109
|
+
site: hostnameOf(url),
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
async function readBody(response: Response): Promise<string> {
|
|
114
|
+
const reader = response.body?.getReader()
|
|
115
|
+
if (!reader) return ''
|
|
116
|
+
const chunks: Uint8Array[] = []
|
|
117
|
+
let total = 0
|
|
118
|
+
for (;;) {
|
|
119
|
+
const { done, value } = await reader.read()
|
|
120
|
+
if (done) break
|
|
121
|
+
if (value === undefined) continue
|
|
122
|
+
const room = BODY_LIMIT - total
|
|
123
|
+
if (room <= 0) {
|
|
124
|
+
await reader.cancel()
|
|
125
|
+
break
|
|
126
|
+
}
|
|
127
|
+
const chunk = value.byteLength > room ? value.subarray(0, room) : value
|
|
128
|
+
chunks.push(chunk)
|
|
129
|
+
total += chunk.byteLength
|
|
130
|
+
if (total >= BODY_LIMIT) {
|
|
131
|
+
await reader.cancel()
|
|
132
|
+
break
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
const bytes = new Uint8Array(total)
|
|
136
|
+
let offset = 0
|
|
137
|
+
for (const chunk of chunks) {
|
|
138
|
+
bytes.set(chunk, offset)
|
|
139
|
+
offset += chunk.byteLength
|
|
140
|
+
}
|
|
141
|
+
return new TextDecoder().decode(bytes)
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export async function fetchPreview(url: string): Promise<LinkPreview> {
|
|
145
|
+
const now = Date.now()
|
|
146
|
+
const hit = cache.get(url)
|
|
147
|
+
if (hit !== undefined && now - hit.at < CACHE_TTL) return hit.data
|
|
148
|
+
|
|
149
|
+
let result: LinkPreview
|
|
150
|
+
try {
|
|
151
|
+
const response = await fetch(url, {
|
|
152
|
+
redirect: 'follow',
|
|
153
|
+
signal: AbortSignal.timeout(5000),
|
|
154
|
+
headers: { 'User-Agent': 'pointer/1.0', Accept: 'text/html,application/xhtml+xml' },
|
|
155
|
+
})
|
|
156
|
+
const type = response.headers.get('content-type') ?? ''
|
|
157
|
+
if (!response.ok || !type.toLowerCase().includes('text/html')) {
|
|
158
|
+
result = minimal(url)
|
|
159
|
+
} else {
|
|
160
|
+
result = parsePreviewHtml(await readBody(response), response.url || url)
|
|
161
|
+
}
|
|
162
|
+
} catch {
|
|
163
|
+
result = minimal(url)
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
if (cache.size >= CACHE_LIMIT) {
|
|
167
|
+
const oldest = cache.keys().next().value
|
|
168
|
+
if (oldest !== undefined) cache.delete(oldest)
|
|
169
|
+
}
|
|
170
|
+
cache.set(url, { data: result, at: now })
|
|
171
|
+
return result
|
|
172
|
+
}
|
package/server/loop.ts
ADDED
|
@@ -0,0 +1,328 @@
|
|
|
1
|
+
import type { RunStatus, StopReason, ToolCallBlock, Usage } from '../shared/types'
|
|
2
|
+
import { isOverflowError } from './compact'
|
|
3
|
+
import { createQueueWake, type QueueWake } from './jobs'
|
|
4
|
+
import { ABORTED, INTERRUPTED } from './history'
|
|
5
|
+
import type { ToolResult } from './tools'
|
|
6
|
+
|
|
7
|
+
export type LoopStep = {
|
|
8
|
+
called: boolean
|
|
9
|
+
stopReason: StopReason | null
|
|
10
|
+
assistantText: string | null
|
|
11
|
+
notice: string | null
|
|
12
|
+
usage: Usage | null
|
|
13
|
+
toolCalls: ToolCallBlock[]
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export type RunOutcome = Omit<LoopStep, 'toolCalls'>
|
|
17
|
+
|
|
18
|
+
export type ExecuteTool = (call: ToolCallBlock, signal: AbortSignal | undefined) => Promise<ToolResult>
|
|
19
|
+
|
|
20
|
+
export type Run = {
|
|
21
|
+
runId: string
|
|
22
|
+
abort: AbortController
|
|
23
|
+
queue: string[]
|
|
24
|
+
interruptText: string | null
|
|
25
|
+
/** Wakes a `wait` tool when a message enters the queue. */
|
|
26
|
+
wake: QueueWake
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export type RunHooks = {
|
|
30
|
+
maxSteps: number
|
|
31
|
+
started: () => void
|
|
32
|
+
begin: () => void
|
|
33
|
+
step: () => Promise<LoopStep>
|
|
34
|
+
execute: ExecuteTool
|
|
35
|
+
appendUser: (text: string) => void
|
|
36
|
+
appendTool: (call: ToolCallBlock, result: ToolResult) => void
|
|
37
|
+
closeDangling: (reason: string) => void
|
|
38
|
+
/** `intoContext` keeps the row for the reader but also sends its text to the model. */
|
|
39
|
+
notice: (kind: string, text: string, options?: { intoContext?: boolean }) => void
|
|
40
|
+
queueChanged: (queue: string[]) => void
|
|
41
|
+
finished: (status: RunStatus) => void
|
|
42
|
+
/**
|
|
43
|
+
* Compaction on the step boundary. Called with the usage of the step just finished and
|
|
44
|
+
* with `force` when the step failed on context overflow. Returns a notice text when it
|
|
45
|
+
* compacted, so the loop can report it and retry the step.
|
|
46
|
+
*/
|
|
47
|
+
autoCompact?: (usage: Usage | null, force: boolean) => Promise<string | null>
|
|
48
|
+
/** Writes background-job notices that arrived since the last boundary. */
|
|
49
|
+
flushJobs?: () => void
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export type RunFactory = (run: Run, text: string) => RunHooks
|
|
53
|
+
|
|
54
|
+
export type PostResult = { runId: string; queued: boolean }
|
|
55
|
+
|
|
56
|
+
/** dsh keeps 10 calls of one step in flight; 1 would make the step sequential. */
|
|
57
|
+
export const MAX_PARALLEL_TOOL_CALLS = 10
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Calls that must not share a step with another call. `write` and `edit` compare against
|
|
61
|
+
* the file on disk; two of them in one batch would race on that file.
|
|
62
|
+
*/
|
|
63
|
+
const EXCLUSIVE_TOOLS = new Set(['write', 'edit'])
|
|
64
|
+
|
|
65
|
+
/** The next batch: an exclusive call alone, otherwise up to the cap of independent calls. */
|
|
66
|
+
function batchAt(calls: ToolCallBlock[], start: number): ToolCallBlock[] {
|
|
67
|
+
const first = calls[start]
|
|
68
|
+
if (!first) return []
|
|
69
|
+
if (EXCLUSIVE_TOOLS.has(first.name)) return [first]
|
|
70
|
+
const batch: ToolCallBlock[] = []
|
|
71
|
+
for (let index = start; index < calls.length && batch.length < MAX_PARALLEL_TOOL_CALLS; index++) {
|
|
72
|
+
const call = calls[index]!
|
|
73
|
+
if (EXCLUSIVE_TOOLS.has(call.name)) break
|
|
74
|
+
batch.push(call)
|
|
75
|
+
}
|
|
76
|
+
return batch
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const activeRuns = new Map<string, Run>()
|
|
80
|
+
const live = new Map<string, { hooks: RunHooks; factory: RunFactory }>()
|
|
81
|
+
|
|
82
|
+
export function activeRun(chatId: string): Run | undefined {
|
|
83
|
+
return activeRuns.get(chatId)
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export function activeRunCount(): number {
|
|
87
|
+
return activeRuns.size
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Starts a run, or returns null when the chat is already running. The run writes its first
|
|
92
|
+
* user row in `begin` and then drives the cycle below.
|
|
93
|
+
*/
|
|
94
|
+
export function startRun(chatId: string, text: string, factory: RunFactory): string | null {
|
|
95
|
+
if (activeRuns.has(chatId)) return null
|
|
96
|
+
const run: Run = {
|
|
97
|
+
runId: crypto.randomUUID(),
|
|
98
|
+
abort: new AbortController(),
|
|
99
|
+
queue: [],
|
|
100
|
+
interruptText: null,
|
|
101
|
+
wake: createQueueWake(),
|
|
102
|
+
}
|
|
103
|
+
activeRuns.set(chatId, run)
|
|
104
|
+
let hooks: RunHooks | undefined
|
|
105
|
+
let announced = false
|
|
106
|
+
try {
|
|
107
|
+
hooks = factory(run, text)
|
|
108
|
+
live.set(chatId, { hooks, factory })
|
|
109
|
+
hooks.started()
|
|
110
|
+
announced = true
|
|
111
|
+
hooks.begin()
|
|
112
|
+
} catch (error) {
|
|
113
|
+
activeRuns.delete(chatId)
|
|
114
|
+
live.delete(chatId)
|
|
115
|
+
if (announced && hooks) {
|
|
116
|
+
try {
|
|
117
|
+
hooks.finished('error')
|
|
118
|
+
} catch {}
|
|
119
|
+
}
|
|
120
|
+
throw error
|
|
121
|
+
}
|
|
122
|
+
void drive(chatId, run, hooks, factory)
|
|
123
|
+
return run.runId
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
async function drive(chatId: string, run: Run, hooks: RunHooks, factory: RunFactory): Promise<void> {
|
|
127
|
+
let status: RunStatus = 'error'
|
|
128
|
+
try {
|
|
129
|
+
const outcome = await runLoop({
|
|
130
|
+
signal: run.abort.signal,
|
|
131
|
+
maxSteps: hooks.maxSteps,
|
|
132
|
+
queue: run.queue,
|
|
133
|
+
appendUser: hooks.appendUser,
|
|
134
|
+
step: hooks.step,
|
|
135
|
+
execute: hooks.execute,
|
|
136
|
+
appendTool: hooks.appendTool,
|
|
137
|
+
closeDangling: hooks.closeDangling,
|
|
138
|
+
notice: hooks.notice,
|
|
139
|
+
queueChanged: hooks.queueChanged,
|
|
140
|
+
autoCompact: hooks.autoCompact,
|
|
141
|
+
flushJobs: hooks.flushJobs,
|
|
142
|
+
queueDrained: () => run.wake.clear(),
|
|
143
|
+
})
|
|
144
|
+
status = !outcome.called || outcome.stopReason === 'error' ? 'error' : 'idle'
|
|
145
|
+
} catch {
|
|
146
|
+
status = 'error'
|
|
147
|
+
} finally {
|
|
148
|
+
if (run.queue.length > 0) {
|
|
149
|
+
// An error or the step cap can end the run while messages still wait in the queue.
|
|
150
|
+
// They stay in the history as user rows instead of vanishing with the run object.
|
|
151
|
+
for (const text of run.queue) {
|
|
152
|
+
try {
|
|
153
|
+
hooks.appendUser(text)
|
|
154
|
+
} catch {
|
|
155
|
+
// One failed row must not block the rest of the queue or the status update.
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
run.queue.length = 0
|
|
159
|
+
hooks.queueChanged(run.queue)
|
|
160
|
+
}
|
|
161
|
+
const next = run.interruptText
|
|
162
|
+
activeRuns.delete(chatId)
|
|
163
|
+
live.delete(chatId)
|
|
164
|
+
// The new run is started before any idle status goes out: `running` does not blink.
|
|
165
|
+
if (next !== null) startRun(chatId, next, factory)
|
|
166
|
+
else hooks.finished(status)
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** A plain post queues behind the current turn; an interrupt stops it and leaves text for a new run. */
|
|
171
|
+
export function postMessage(chatId: string, text: string, interrupt: boolean, factory: RunFactory): PostResult {
|
|
172
|
+
const run = activeRuns.get(chatId)
|
|
173
|
+
if (!run) {
|
|
174
|
+
const runId = startRun(chatId, text, factory)
|
|
175
|
+
if (runId === null) throw new Error('Прогон не начался')
|
|
176
|
+
return { runId, queued: false }
|
|
177
|
+
}
|
|
178
|
+
if (interrupt || run.abort.signal.aborted) {
|
|
179
|
+
run.interruptText = run.interruptText === null ? text : `${run.interruptText}\n\n${text}`
|
|
180
|
+
abortRun(chatId)
|
|
181
|
+
return { runId: run.runId, queued: false }
|
|
182
|
+
}
|
|
183
|
+
run.queue.push(text)
|
|
184
|
+
run.wake.notify()
|
|
185
|
+
live.get(chatId)?.hooks.queueChanged(run.queue)
|
|
186
|
+
return { runId: run.runId, queued: true }
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
export function abortRun(chatId: string): boolean {
|
|
190
|
+
const run = activeRuns.get(chatId)
|
|
191
|
+
if (!run) return false
|
|
192
|
+
run.queue.length = 0
|
|
193
|
+
live.get(chatId)?.hooks.queueChanged(run.queue)
|
|
194
|
+
run.abort.abort()
|
|
195
|
+
return true
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
export async function runLoop(input: {
|
|
199
|
+
signal?: AbortSignal
|
|
200
|
+
maxSteps: number
|
|
201
|
+
queue: string[]
|
|
202
|
+
appendUser: (text: string) => void
|
|
203
|
+
step: () => Promise<LoopStep>
|
|
204
|
+
execute: ExecuteTool
|
|
205
|
+
appendTool: (call: ToolCallBlock, result: ToolResult) => void
|
|
206
|
+
closeDangling: (reason: string) => void
|
|
207
|
+
/** `intoContext` keeps the row for the reader but also sends its text to the model. */
|
|
208
|
+
notice: (kind: string, text: string, options?: { intoContext?: boolean }) => void
|
|
209
|
+
queueChanged?: (queue: string[]) => void
|
|
210
|
+
autoCompact?: (usage: Usage | null, force: boolean) => Promise<string | null>
|
|
211
|
+
flushJobs?: () => void
|
|
212
|
+
queueDrained?: () => void
|
|
213
|
+
}): Promise<RunOutcome> {
|
|
214
|
+
const signal = input.signal
|
|
215
|
+
const queue = input.queue
|
|
216
|
+
const execute = input.execute
|
|
217
|
+
let step = 0
|
|
218
|
+
/** Overflow recovery is one attempt per run, as in pi: a second overflow is reported, not looped. */
|
|
219
|
+
let overflowRetried = false
|
|
220
|
+
let last: LoopStep = {
|
|
221
|
+
called: false,
|
|
222
|
+
stopReason: null,
|
|
223
|
+
assistantText: null,
|
|
224
|
+
notice: null,
|
|
225
|
+
usage: null,
|
|
226
|
+
toolCalls: [],
|
|
227
|
+
}
|
|
228
|
+
try {
|
|
229
|
+
for (;;) {
|
|
230
|
+
if (signal?.aborted) break
|
|
231
|
+
const queued = queue.splice(0)
|
|
232
|
+
input.queueDrained?.()
|
|
233
|
+
if (queued.length > 0) {
|
|
234
|
+
step = 0
|
|
235
|
+
for (const text of queued) input.appendUser(text)
|
|
236
|
+
input.queueChanged?.(queue)
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
if (step === input.maxSteps) {
|
|
240
|
+
input.notice(
|
|
241
|
+
'max_steps',
|
|
242
|
+
`Достигнут потолок шагов: ${input.maxSteps}. Прогон остановлен; продолжи только по новому сообщению`,
|
|
243
|
+
{ intoContext: true },
|
|
244
|
+
)
|
|
245
|
+
break
|
|
246
|
+
}
|
|
247
|
+
step += 1
|
|
248
|
+
|
|
249
|
+
last = await input.step()
|
|
250
|
+
if (signal?.aborted) break
|
|
251
|
+
|
|
252
|
+
// The provider refused the request because it did not fit the window: compact once and
|
|
253
|
+
// re-send the same step instead of ending the run with an opaque error.
|
|
254
|
+
if (input.autoCompact && last.stopReason === 'error' && !overflowRetried && isOverflowError(last.notice)) {
|
|
255
|
+
const notice = await input.autoCompact(last.usage, true)
|
|
256
|
+
if (notice !== null) {
|
|
257
|
+
overflowRetried = true
|
|
258
|
+
// The compaction summary row already enters the model context; the notice is for the UI log.
|
|
259
|
+
input.notice('compacted', notice)
|
|
260
|
+
step -= 1
|
|
261
|
+
continue
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
if (!last.called || last.stopReason === 'error') break
|
|
265
|
+
|
|
266
|
+
const calls =
|
|
267
|
+
last.stopReason === 'tool_use' || last.stopReason === 'max_tokens' ? last.toolCalls : []
|
|
268
|
+
|
|
269
|
+
// Independent calls of one step run together, capped as in dsh. A write or edit waits
|
|
270
|
+
// for the batch before it and runs alone; two such calls in one step would race.
|
|
271
|
+
// Results become rows in call order, not in the order the tools happened to finish.
|
|
272
|
+
const runCall = async (call: ToolCallBlock): Promise<ToolResult> => {
|
|
273
|
+
if (signal?.aborted) return { text: ABORTED, isError: true }
|
|
274
|
+
try {
|
|
275
|
+
const result = await execute(call, signal)
|
|
276
|
+
return signal?.aborted ? { text: ABORTED, isError: true } : result
|
|
277
|
+
} catch (error) {
|
|
278
|
+
return signal?.aborted
|
|
279
|
+
? { text: ABORTED, isError: true }
|
|
280
|
+
: { text: error instanceof Error ? error.message : String(error), isError: true }
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
let index = 0
|
|
285
|
+
while (index < calls.length) {
|
|
286
|
+
if (signal?.aborted) break
|
|
287
|
+
const batch = batchAt(calls, index)
|
|
288
|
+
const results = await Promise.all(batch.map(runCall))
|
|
289
|
+
for (let offset = 0; offset < batch.length; offset++) {
|
|
290
|
+
input.appendTool(batch[offset]!, results[offset]!)
|
|
291
|
+
}
|
|
292
|
+
index += batch.length
|
|
293
|
+
}
|
|
294
|
+
if (signal?.aborted) break
|
|
295
|
+
|
|
296
|
+
if (last.stopReason === 'max_tokens' && calls.length > 0 && last.notice) {
|
|
297
|
+
input.notice('max_tokens', last.notice, { intoContext: true })
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
// Compaction on the step boundary: runs after the step's tool rows are in the log, so
|
|
301
|
+
// repair sees no open calls and the summary never splits an assistant from its tools.
|
|
302
|
+
if (input.autoCompact) {
|
|
303
|
+
const notice = await input.autoCompact(last.usage, false)
|
|
304
|
+
// The summary row is already in the model context; keep the notice for the reader only
|
|
305
|
+
// so a synthetic user message does not land after the current step's tool results.
|
|
306
|
+
if (notice !== null) input.notice('compacted', notice)
|
|
307
|
+
}
|
|
308
|
+
const continues = calls.length > 0 || queue.length > 0
|
|
309
|
+
// Job completions are written after all tool rows of this step, like the max_tokens
|
|
310
|
+
// notice: an `intoContext` row between an assistant and its tools breaks history repair.
|
|
311
|
+
// The final step does not flush: the run is about to end and the notice would never
|
|
312
|
+
// reach a model call. It stays pending and starts a follow-up run from the idle chat.
|
|
313
|
+
if (continues && !signal?.aborted) input.flushJobs?.()
|
|
314
|
+
if (signal?.aborted) break
|
|
315
|
+
|
|
316
|
+
if (!continues) break
|
|
317
|
+
}
|
|
318
|
+
} finally {
|
|
319
|
+
input.closeDangling(signal?.aborted ? ABORTED : INTERRUPTED)
|
|
320
|
+
}
|
|
321
|
+
return {
|
|
322
|
+
called: last.called,
|
|
323
|
+
stopReason: last.stopReason,
|
|
324
|
+
assistantText: last.assistantText,
|
|
325
|
+
notice: last.notice,
|
|
326
|
+
usage: last.usage,
|
|
327
|
+
}
|
|
328
|
+
}
|