@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.
Files changed (71) hide show
  1. package/README.md +54 -2
  2. package/package.json +51 -4
  3. package/server/acp/catalog.ts +111 -0
  4. package/server/acp/connection.ts +233 -0
  5. package/server/acp/fake-agent.ts +408 -0
  6. package/server/acp/protocol.ts +167 -0
  7. package/server/acp/run.ts +139 -0
  8. package/server/acp/session.ts +1249 -0
  9. package/server/attachments.ts +115 -0
  10. package/server/changes.ts +239 -0
  11. package/server/cli.ts +362 -0
  12. package/server/compact.ts +361 -0
  13. package/server/config.ts +176 -0
  14. package/server/db.ts +462 -0
  15. package/server/dev.ts +8 -0
  16. package/server/diff.ts +190 -0
  17. package/server/engines.ts +63 -0
  18. package/server/files.ts +254 -0
  19. package/server/git.ts +319 -0
  20. package/server/history.ts +425 -0
  21. package/server/http/attachments.ts +63 -0
  22. package/server/http/browse.ts +108 -0
  23. package/server/http/chats.ts +627 -0
  24. package/server/http/closing.ts +14 -0
  25. package/server/http/files.ts +115 -0
  26. package/server/http/git.ts +84 -0
  27. package/server/http/preview.ts +21 -0
  28. package/server/http/providers.ts +200 -0
  29. package/server/http/reply.ts +38 -0
  30. package/server/hub.ts +521 -0
  31. package/server/install.ts +169 -0
  32. package/server/jobs.ts +664 -0
  33. package/server/linkPreview.ts +172 -0
  34. package/server/loop.ts +328 -0
  35. package/server/main.ts +353 -0
  36. package/server/probe.ts +34 -0
  37. package/server/process.ts +333 -0
  38. package/server/prompt.ts +196 -0
  39. package/server/providers/fake.ts +100 -0
  40. package/server/providers/opencode.ts +468 -0
  41. package/server/providers/registry.ts +149 -0
  42. package/server/providers/store.ts +210 -0
  43. package/server/segments.ts +248 -0
  44. package/server/sse.ts +93 -0
  45. package/server/tools/bash.ts +222 -0
  46. package/server/tools/edit.ts +254 -0
  47. package/server/tools/glob.ts +63 -0
  48. package/server/tools/grep.ts +122 -0
  49. package/server/tools/ignore.ts +54 -0
  50. package/server/tools/index.ts +129 -0
  51. package/server/tools/jail.ts +85 -0
  52. package/server/tools/jobs.ts +293 -0
  53. package/server/tools/read.ts +248 -0
  54. package/server/tools/write.ts +70 -0
  55. package/server/tray.ps1 +191 -0
  56. package/server/turn.ts +663 -0
  57. package/server/watch.ts +94 -0
  58. package/shared/acpConfig.ts +113 -0
  59. package/shared/links.ts +122 -0
  60. package/shared/types.ts +392 -0
  61. package/web/dist/app.css +1 -0
  62. package/web/dist/assets/codicon-mxdk0fnd.ttf +0 -0
  63. package/web/dist/index.html +28 -0
  64. package/web/dist/main.css +1 -0
  65. package/web/dist/main.js +1366 -0
  66. package/web/dist/tokens.css +1 -0
  67. package/web/dist/workers/css.worker.js +97 -0
  68. package/web/dist/workers/editor.worker.js +30 -0
  69. package/web/dist/workers/html.worker.js +300 -0
  70. package/web/dist/workers/json.worker.js +62 -0
  71. package/web/dist/workers/ts.worker.js +67733 -0
@@ -0,0 +1,210 @@
1
+ import type { Database } from 'bun:sqlite'
2
+ import type { Config } from '../config'
3
+
4
+ export const PROVIDER_IDS = ['opencode-go', 'openai', 'acp:cursor'] as const
5
+
6
+ export type ProviderKind = 'opencode-go' | 'openai' | 'acp'
7
+
8
+ export type ProviderRow = {
9
+ id: string
10
+ kind: ProviderKind
11
+ label: string
12
+ base_url: string | null
13
+ api_key: string | null
14
+ command: string | null
15
+ args_json: string | null
16
+ cwd: string | null
17
+ enabled: number
18
+ }
19
+
20
+ export type ProviderWrite = {
21
+ label?: string
22
+ baseUrl?: string | null
23
+ apiKey?: string | null
24
+ command?: string | null
25
+ args?: string[] | null
26
+ cwd?: string | null
27
+ enabled?: boolean
28
+ }
29
+
30
+ /** Command, args and cwd saved for the Cursor slot. An absent field keeps the config value. */
31
+ export type AcpOverride = {
32
+ command?: string
33
+ args?: string[]
34
+ cwd?: string
35
+ }
36
+
37
+ const DEFAULT_MODEL_KEY = 'default_model'
38
+
39
+ const providerColumns = `id, kind, label, base_url, api_key, command, args_json, cwd, enabled`
40
+
41
+ export function listProviders(db: Database): ProviderRow[] {
42
+ return db
43
+ .query<ProviderRow, []>(
44
+ `SELECT ${providerColumns} FROM providers
45
+ ORDER BY CASE id WHEN 'opencode-go' THEN 0 WHEN 'openai' THEN 1 ELSE 2 END`,
46
+ )
47
+ .all()
48
+ }
49
+
50
+ export function getProvider(db: Database, id: string): ProviderRow | null {
51
+ return (
52
+ db.query<ProviderRow, [string]>(`SELECT ${providerColumns} FROM providers WHERE id = ?`).get(id) ?? null
53
+ )
54
+ }
55
+
56
+ export function updateProvider(db: Database, id: string, patch: ProviderWrite): ProviderRow | null {
57
+ if (!getProvider(db, id)) return null
58
+ const sets: string[] = []
59
+ const values: Array<string | number | null> = []
60
+ if (patch.label !== undefined) {
61
+ sets.push('label = ?')
62
+ values.push(patch.label)
63
+ }
64
+ if (patch.baseUrl !== undefined) {
65
+ sets.push('base_url = ?')
66
+ values.push(patch.baseUrl)
67
+ }
68
+ if (patch.apiKey !== undefined) {
69
+ sets.push('api_key = ?')
70
+ values.push(patch.apiKey)
71
+ }
72
+ if (patch.command !== undefined) {
73
+ sets.push('command = ?')
74
+ values.push(patch.command)
75
+ }
76
+ if (patch.args !== undefined) {
77
+ sets.push('args_json = ?')
78
+ values.push(patch.args === null ? null : JSON.stringify(patch.args))
79
+ }
80
+ if (patch.cwd !== undefined) {
81
+ sets.push('cwd = ?')
82
+ values.push(patch.cwd)
83
+ }
84
+ if (patch.enabled !== undefined) {
85
+ sets.push('enabled = ?')
86
+ values.push(patch.enabled ? 1 : 0)
87
+ }
88
+ if (sets.length > 0) {
89
+ values.push(id)
90
+ db.run(`UPDATE providers SET ${sets.join(', ')} WHERE id = ?`, values)
91
+ }
92
+ return getProvider(db, id)
93
+ }
94
+
95
+ export function listModels(db: Database, providerId: string): string[] {
96
+ return db
97
+ .query<{ model_id: string }, [string]>(
98
+ 'SELECT model_id FROM provider_models WHERE provider_id = ? ORDER BY model_id',
99
+ )
100
+ .all(providerId)
101
+ .map((row) => row.model_id)
102
+ }
103
+
104
+ /** A model id is the bare name. The chat stores `<provider>/<id>`, so a slash here would be a second prefix. */
105
+ export function modelIdOk(modelId: string): boolean {
106
+ const trimmed = modelId.trim()
107
+ return trimmed.length > 0 && !trimmed.includes('/') && !/\s/.test(trimmed)
108
+ }
109
+
110
+ export function addModel(db: Database, providerId: string, modelId: string): boolean {
111
+ if (!getProvider(db, providerId) || !modelIdOk(modelId)) return false
112
+ db.run('INSERT OR IGNORE INTO provider_models (provider_id, model_id) VALUES (?, ?)', [
113
+ providerId,
114
+ modelId.trim(),
115
+ ])
116
+ return true
117
+ }
118
+
119
+ export function removeModel(db: Database, providerId: string, modelId: string): boolean {
120
+ return db.run('DELETE FROM provider_models WHERE provider_id = ? AND model_id = ?', [providerId, modelId]).changes > 0
121
+ }
122
+
123
+ export function replaceModels(db: Database, providerId: string, modelIds: string[]): void {
124
+ const unique = [...new Set(modelIds.map((id) => id.trim()).filter(modelIdOk))]
125
+ db.transaction(() => {
126
+ db.run('DELETE FROM provider_models WHERE provider_id = ?', [providerId])
127
+ for (const modelId of unique) {
128
+ db.run('INSERT INTO provider_models (provider_id, model_id) VALUES (?, ?)', [providerId, modelId])
129
+ }
130
+ })()
131
+ }
132
+
133
+ export function getSetting(db: Database, key: string): string | null {
134
+ const row = db.query<{ value: string }, [string]>('SELECT value FROM settings WHERE key = ?').get(key)
135
+ return row?.value ?? null
136
+ }
137
+
138
+ export function setSetting(db: Database, key: string, value: string | null): void {
139
+ if (value === null || value === '') {
140
+ db.run('DELETE FROM settings WHERE key = ?', [key])
141
+ return
142
+ }
143
+ db.run(
144
+ `INSERT INTO settings (key, value) VALUES (?, ?)
145
+ ON CONFLICT(key) DO UPDATE SET value = excluded.value`,
146
+ [key, value],
147
+ )
148
+ }
149
+
150
+ /** Model for a new native chat: the saved choice, or `config.model` when the page has not set one. */
151
+ export function defaultModelId(db: Database, config: Pick<Config, 'model'>): string {
152
+ return savedDefaultModel(db) ?? config.model
153
+ }
154
+
155
+ export function savedDefaultModel(db: Database): string | null {
156
+ return getSetting(db, DEFAULT_MODEL_KEY)
157
+ }
158
+
159
+ export function setDefaultModel(db: Database, model: string | null): void {
160
+ setSetting(db, DEFAULT_MODEL_KEY, model)
161
+ }
162
+
163
+ export function providerEnabled(row: Pick<ProviderRow, 'enabled'> | null): boolean {
164
+ return row?.enabled !== 0
165
+ }
166
+
167
+ export function acpOverride(db: Database): AcpOverride {
168
+ const slot = getProvider(db, 'acp:cursor')
169
+ if (!slot) return {}
170
+ const override: AcpOverride = {}
171
+ if (slot.command) override.command = slot.command
172
+ const args = parseArgs(slot.args_json)
173
+ if (args) override.args = args
174
+ if (slot.cwd) override.cwd = slot.cwd
175
+ return override
176
+ }
177
+
178
+ export function parseArgs(value: string | null): string[] | null {
179
+ if (value === null) return null
180
+ try {
181
+ const parsed = JSON.parse(value) as unknown
182
+ if (!Array.isArray(parsed) || !parsed.every((item) => typeof item === 'string')) return null
183
+ return parsed
184
+ } catch {
185
+ return null
186
+ }
187
+ }
188
+
189
+ /**
190
+ * OpenAI-shaped `{ data: [{ id }] }`, a bare array of those objects, or an array of ids.
191
+ * Anything else is a foreign shape: the caller keeps the list it already has.
192
+ */
193
+ export function parseModelIds(body: unknown): string[] | null {
194
+ const rows = Array.isArray(body)
195
+ ? body
196
+ : isRecord(body) && Array.isArray(body.data)
197
+ ? body.data
198
+ : null
199
+ if (!rows) return null
200
+ const ids: string[] = []
201
+ for (const row of rows) {
202
+ const id = typeof row === 'string' ? row : isRecord(row) && typeof row.id === 'string' ? row.id : ''
203
+ if (modelIdOk(id)) ids.push(id.trim())
204
+ }
205
+ return ids
206
+ }
207
+
208
+ function isRecord(value: unknown): value is Record<string, unknown> {
209
+ return typeof value === 'object' && value !== null
210
+ }
@@ -0,0 +1,248 @@
1
+ import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs'
2
+ import { join } from 'node:path'
3
+ import { pointerHome } from './config'
4
+ import type { MessageRow } from './db'
5
+
6
+ /**
7
+ * Turns that compaction removed from the model context, written to disk as a readable record.
8
+ *
9
+ * Cursor does the same: the model keeps a summary plus a pointer to an archive, and looks up
10
+ * details with `read` or `grep` instead of carrying them in the window. Its own segments are a
11
+ * 700 KB verbatim dump per file; that is too much to be useful, so this keeps a compact
12
+ * transcript: prompts and answers whole, tool calls as one line, tool results cut.
13
+ */
14
+
15
+ /** A single tool result longer than this is cut with a note; details stay recoverable by re-running. */
16
+ export const TOOL_RESULT_CHARS = 1_000
17
+
18
+ /** A hard ceiling per segment. Older rows are dropped from the tail of the transcript, not the header. */
19
+ export const MAX_SEGMENT_BYTES = 512 * 1024
20
+
21
+ const INDEX_HEADER = [
22
+ '# Compaction Segment Index',
23
+ '',
24
+ '| Segment | File | Rows | Approx bytes | First prompt |',
25
+ '|---|---|---|---|---|',
26
+ '',
27
+ ].join('\n')
28
+
29
+ /** Per-chat archive directory; one level per chat so two chats never share a segment number. */
30
+ export function archiveDir(chatId: string): string {
31
+ return join(pointerHome(), 'chats', chatId, 'compaction')
32
+ }
33
+
34
+ export type TurnStats = {
35
+ rows: number
36
+ byRole: Record<string, number>
37
+ tools: Record<string, number>
38
+ files: string[]
39
+ toolErrors: number
40
+ }
41
+
42
+ /**
43
+ * What the conversation did, counted rather than described. The summarizer gets these numbers
44
+ * so it can say "read 118 times" instead of writing prose that loses the shape of the work.
45
+ */
46
+ export function turnStats(rows: MessageRow[]): TurnStats {
47
+ const stats: TurnStats = { rows: rows.length, byRole: {}, tools: {}, files: [], toolErrors: 0 }
48
+ const files = new Set<string>()
49
+ for (const row of rows) {
50
+ stats.byRole[row.role] = (stats.byRole[row.role] ?? 0) + 1
51
+ const content = parse(row.content_json)
52
+ if (row.role === 'assistant' && isRecord(content) && Array.isArray(content.blocks)) {
53
+ for (const block of content.blocks) {
54
+ if (!isRecord(block) || block.type !== 'tool_call') continue
55
+ const name = typeof block.name === 'string' ? block.name : 'unknown'
56
+ stats.tools[name] = (stats.tools[name] ?? 0) + 1
57
+ const args = block.args
58
+ if (isRecord(args) && typeof args.path === 'string') files.add(args.path)
59
+ }
60
+ }
61
+ if (row.role === 'tool' && isRecord(content) && content.isError === true) stats.toolErrors += 1
62
+ }
63
+ stats.files = [...files].sort()
64
+ return stats
65
+ }
66
+
67
+ /** The statistics block prepended to the summarization request. */
68
+ export function statisticsBlock(stats: TurnStats): string {
69
+ const roles = Object.entries(stats.byRole)
70
+ .sort(([a], [b]) => a.localeCompare(b))
71
+ .map(([role, count]) => `${role} ${count}`)
72
+ .join(', ')
73
+ const tools = Object.entries(stats.tools)
74
+ .sort(([, a], [, b]) => b - a)
75
+ .map(([name, count]) => `${name} ${count}`)
76
+ .join(', ')
77
+ const lines = ['<conversation_statistics>', `Rows: ${stats.rows} (${roles})`]
78
+ if (tools !== '') lines.push(`Tool calls: ${tools}`)
79
+ if (stats.files.length > 0) lines.push(`Files touched (${stats.files.length}): ${stats.files.join(', ')}`)
80
+ lines.push(`Tool errors: ${stats.toolErrors}`)
81
+ lines.push('</conversation_statistics>')
82
+ return lines.join('\n')
83
+ }
84
+
85
+ export type SegmentInput = {
86
+ chatId: string
87
+ rows: MessageRow[]
88
+ stats: TurnStats
89
+ summary: string
90
+ /** Test seam; production writes under the pointer home. */
91
+ dir?: string
92
+ now?: Date
93
+ }
94
+
95
+ export type SegmentResult = { file: string; indexPath: string; bytes: number; index: number }
96
+
97
+ /**
98
+ * Writes one segment and appends it to the chat's index. Returns null when the archive could
99
+ * not be written: compaction must still happen, the summary just loses its pointer.
100
+ */
101
+ export function writeSegment(input: SegmentInput): SegmentResult | null {
102
+ try {
103
+ const dir = input.dir ?? archiveDir(input.chatId)
104
+ mkdirSync(dir, { recursive: true })
105
+ const index = nextIndex(dir)
106
+ const padded = String(index).padStart(3, '0')
107
+ const file = join(dir, `segment_${padded}.md`)
108
+ const body = renderSegment({ ...input, index })
109
+ const bytes = Buffer.byteLength(body, 'utf8')
110
+ writeFileSync(file, body, 'utf8')
111
+ const indexPath = join(dir, 'INDEX.md')
112
+ const fullFile = slashes(file)
113
+ const row = `| ${padded} | ${fullFile} | ${input.stats.rows} | ${bytes} | ${firstPrompt(input.rows)} |\n`
114
+ const previous = existsSync(indexPath) ? readFileSync(indexPath, 'utf8') : INDEX_HEADER
115
+ writeFileSync(indexPath, `${previous}${row}`, 'utf8')
116
+ return { file: fullFile, indexPath: slashes(indexPath), bytes, index }
117
+ } catch {
118
+ return null
119
+ }
120
+ }
121
+
122
+ function nextIndex(dir: string): number {
123
+ let highest = 0
124
+ for (const name of new Bun.Glob('segment_*.md').scanSync({ cwd: dir })) {
125
+ const match = /^segment_(\d+)\.md$/.exec(name)
126
+ if (match) highest = Math.max(highest, Number(match[1]))
127
+ }
128
+ return highest + 1
129
+ }
130
+
131
+ function renderSegment(input: SegmentInput & { index: number }): string {
132
+ const stamp = (input.now ?? new Date()).toISOString()
133
+ const parts: string[] = [
134
+ '# HISTORICAL -- DO NOT EDIT',
135
+ `# Record of compaction segment ${String(input.index).padStart(3, '0')} from this same chat.`,
136
+ '# Use read or grep to look up details; do not edit. These turns are no longer in the model context.',
137
+ '',
138
+ '## Segment metadata',
139
+ `- Created: ${stamp}`,
140
+ `- Rows summarized: ${input.stats.rows}`,
141
+ '',
142
+ '## Turn statistics',
143
+ '',
144
+ statisticsBlock(input.stats)
145
+ .replace('<conversation_statistics>', '')
146
+ .replace('</conversation_statistics>', '')
147
+ .trimEnd(),
148
+ '',
149
+ '## Summary',
150
+ '',
151
+ input.summary.trim(),
152
+ '',
153
+ '## Transcript',
154
+ '',
155
+ ]
156
+ let used = parts.join('\n').length
157
+ for (const [position, row] of input.rows.entries()) {
158
+ if (used >= MAX_SEGMENT_BYTES) {
159
+ parts.push(`_[${
160
+ input.rows.length - position
161
+ } later rows omitted: the segment reached its ${MAX_SEGMENT_BYTES} byte ceiling.]_`)
162
+ break
163
+ }
164
+ const rendered = renderRow(row, position + 1)
165
+ used += rendered.length
166
+ parts.push(rendered)
167
+ }
168
+ return `${parts.join('\n')}\n`
169
+ }
170
+
171
+ function renderRow(row: MessageRow, number: number): string {
172
+ const content = parse(row.content_json)
173
+ const lines: string[] = [`### ${number} ${row.role}`]
174
+ if (row.role === 'user' || row.role === 'notice') {
175
+ lines.push('', textOf(content))
176
+ return lines.join('\n')
177
+ }
178
+ if (row.role === 'assistant' && isRecord(content) && Array.isArray(content.blocks)) {
179
+ const text = content.blocks
180
+ .filter((block): block is Record<string, unknown> => isRecord(block) && block.type === 'text')
181
+ .map((block) => (typeof block.text === 'string' ? block.text : ''))
182
+ .join('')
183
+ if (text.trim() !== '') lines.push('', text)
184
+ for (const block of content.blocks) {
185
+ if (!isRecord(block) || block.type !== 'tool_call') continue
186
+ const name = typeof block.name === 'string' ? block.name : 'unknown'
187
+ lines.push(`- ${name} ${oneLine(block.args)}`)
188
+ }
189
+ return lines.join('\n')
190
+ }
191
+ if (row.role === 'tool' && isRecord(content)) {
192
+ const failed = content.isError === true
193
+ const text = typeof content.text === 'string' ? content.text : ''
194
+ lines[0] = `### ${number} tool${failed ? ' (error)' : ''}`
195
+ lines.push('', cut(text))
196
+ return lines.join('\n')
197
+ }
198
+ lines.push('', textOf(content))
199
+ return lines.join('\n')
200
+ }
201
+
202
+ function cut(text: string): string {
203
+ if (text.length <= TOOL_RESULT_CHARS) return text
204
+ return `${text.slice(0, TOOL_RESULT_CHARS)}\n_[... ${text.length - TOOL_RESULT_CHARS} more characters]_`
205
+ }
206
+
207
+ function oneLine(value: unknown): string {
208
+ try {
209
+ const text = JSON.stringify(value) ?? ''
210
+ return text.length > 200 ? `${text.slice(0, 200)}...` : text
211
+ } catch {
212
+ return String(value)
213
+ }
214
+ }
215
+
216
+ function firstPrompt(rows: MessageRow[]): string {
217
+ for (const row of rows) {
218
+ if (row.role !== 'user') continue
219
+ const parsed = parse(row.content_json)
220
+ if (isRecord(parsed) && parsed.compacted === true) continue
221
+ const text = textOf(parsed)
222
+ .replace(/\s+/g, ' ')
223
+ .replaceAll('|', '\\|')
224
+ .trim()
225
+ if (text !== '') return text.length > 60 ? `${text.slice(0, 60)}...` : text
226
+ }
227
+ return '-'
228
+ }
229
+
230
+ function textOf(value: unknown): string {
231
+ return isRecord(value) && typeof value.text === 'string' ? value.text : ''
232
+ }
233
+
234
+ function parse(text: string): unknown {
235
+ try {
236
+ return JSON.parse(text)
237
+ } catch {
238
+ return undefined
239
+ }
240
+ }
241
+
242
+ function isRecord(value: unknown): value is Record<string, unknown> {
243
+ return typeof value === 'object' && value !== null
244
+ }
245
+
246
+ function slashes(path: string): string {
247
+ return path.replaceAll('\\', '/')
248
+ }
package/server/sse.ts ADDED
@@ -0,0 +1,93 @@
1
+ /** Payload of each SSE event, after `data:` lines are joined. A trailing event without a blank line is flushed at EOF. */
2
+ export async function* readSSE(
3
+ stream: ReadableStream<Uint8Array>,
4
+ signal?: AbortSignal,
5
+ ): AsyncGenerator<string> {
6
+ const reader = stream.getReader()
7
+ const decoder = new TextDecoder()
8
+ let pending = ''
9
+ let dataLines: string[] = []
10
+
11
+ const failIfAborted = () => {
12
+ if (signal?.aborted) throw abortError()
13
+ }
14
+
15
+ try {
16
+ while (true) {
17
+ failIfAborted()
18
+ const { done, value } = await readOrAbort(reader, signal)
19
+ if (done) {
20
+ pending += decoder.decode()
21
+ const line = pending.endsWith('\r') ? pending.slice(0, -1) : pending
22
+ pending = ''
23
+ if (line.startsWith('data:')) {
24
+ let payload = line.slice(5)
25
+ if (payload.startsWith(' ')) payload = payload.slice(1)
26
+ dataLines.push(payload)
27
+ }
28
+ if (dataLines.length > 0) {
29
+ yield dataLines.join('\n')
30
+ dataLines = []
31
+ }
32
+ return
33
+ }
34
+ pending += decoder.decode(value, { stream: true })
35
+ while (true) {
36
+ const nl = pending.indexOf('\n')
37
+ if (nl < 0) break
38
+ let line = pending.slice(0, nl)
39
+ pending = pending.slice(nl + 1)
40
+ if (line.endsWith('\r')) line = line.slice(0, -1)
41
+ if (line === '') {
42
+ if (dataLines.length > 0) {
43
+ yield dataLines.join('\n')
44
+ dataLines = []
45
+ }
46
+ continue
47
+ }
48
+ if (line.startsWith(':')) continue
49
+ if (line.startsWith('data:')) {
50
+ let payload = line.slice(5)
51
+ if (payload.startsWith(' ')) payload = payload.slice(1)
52
+ dataLines.push(payload)
53
+ }
54
+ }
55
+ }
56
+ } finally {
57
+ try {
58
+ reader.releaseLock()
59
+ } catch {
60
+ // The reader is already cancelled when the caller aborts.
61
+ }
62
+ }
63
+ }
64
+
65
+ function readOrAbort(reader: ReadableStreamDefaultReader<Uint8Array>, signal?: AbortSignal) {
66
+ const read = reader.read()
67
+ if (!signal) return read
68
+ return new Promise<Awaited<typeof read>>((resolve, reject) => {
69
+ const onAbort = () => {
70
+ void reader.cancel(abortError()).catch(() => {})
71
+ reject(abortError())
72
+ }
73
+ if (signal.aborted) {
74
+ onAbort()
75
+ return
76
+ }
77
+ signal.addEventListener('abort', onAbort, { once: true })
78
+ read.then(
79
+ (result) => {
80
+ signal.removeEventListener('abort', onAbort)
81
+ resolve(result)
82
+ },
83
+ (error) => {
84
+ signal.removeEventListener('abort', onAbort)
85
+ reject(error)
86
+ },
87
+ )
88
+ })
89
+ }
90
+
91
+ function abortError(): DOMException {
92
+ return new DOMException('The operation was aborted.', 'AbortError')
93
+ }