@oh-my-pi/pi-utils 15.0.0 → 15.0.2
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/package.json +5 -5
- package/src/abortable.ts +0 -13
- package/src/fetch-retry.ts +297 -0
- package/src/index.ts +2 -1
- package/src/prompt.ts +51 -1
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"type": "module",
|
|
3
3
|
"name": "@oh-my-pi/pi-utils",
|
|
4
|
-
"version": "15.0.
|
|
4
|
+
"version": "15.0.2",
|
|
5
5
|
"description": "Shared utilities for pi packages",
|
|
6
|
-
"homepage": "https://
|
|
6
|
+
"homepage": "https://omp.sh",
|
|
7
7
|
"author": "Can Boluk",
|
|
8
8
|
"license": "MIT",
|
|
9
9
|
"repository": {
|
|
@@ -37,11 +37,11 @@
|
|
|
37
37
|
"winston-daily-rotate-file": "^5.0.0"
|
|
38
38
|
},
|
|
39
39
|
"devDependencies": {
|
|
40
|
-
"@types/bun": "^1.3.
|
|
41
|
-
"@oh-my-pi/pi-natives": "15.0.
|
|
40
|
+
"@types/bun": "^1.3.14",
|
|
41
|
+
"@oh-my-pi/pi-natives": "15.0.2"
|
|
42
42
|
},
|
|
43
43
|
"engines": {
|
|
44
|
-
"bun": ">=1.3.
|
|
44
|
+
"bun": ">=1.3.14"
|
|
45
45
|
},
|
|
46
46
|
"files": [
|
|
47
47
|
"src"
|
package/src/abortable.ts
CHANGED
|
@@ -10,19 +10,6 @@ export class AbortError extends Error {
|
|
|
10
10
|
}
|
|
11
11
|
}
|
|
12
12
|
|
|
13
|
-
/**
|
|
14
|
-
* Sleep for a given number of milliseconds, respecting abort signal.
|
|
15
|
-
*
|
|
16
|
-
* Uses setTimeout (not Bun.sleep) so that vitest fake timers can intercept it in tests.
|
|
17
|
-
*/
|
|
18
|
-
export function abortableSleep(ms: number, signal?: AbortSignal): Promise<void> {
|
|
19
|
-
return untilAborted(signal, () => {
|
|
20
|
-
const { promise, resolve } = Promise.withResolvers<void>();
|
|
21
|
-
setTimeout(resolve, ms);
|
|
22
|
-
return promise;
|
|
23
|
-
});
|
|
24
|
-
}
|
|
25
|
-
|
|
26
13
|
/**
|
|
27
14
|
* Creates an abortable stream from a given stream and signal.
|
|
28
15
|
*
|
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
import { scheduler } from "node:timers/promises";
|
|
2
|
+
|
|
3
|
+
// "reset after 1h2m3s" / "10m15s" / "39s"
|
|
4
|
+
const QUOTA_RESET_PATTERN = /reset after (?:(\d+)h)?(?:(\d+)m)?(\d+(?:\.\d+)?)s/i;
|
|
5
|
+
// "Please retry in 250ms" / "Please retry in 12s"
|
|
6
|
+
const PLEASE_RETRY_PATTERN = /Please retry in ([0-9.]+)(ms|s)/i;
|
|
7
|
+
// JSON field: "retryDelay": "34.074824224s"
|
|
8
|
+
const RETRY_DELAY_FIELD_PATTERN = /"retryDelay":\s*"([0-9.]+)(ms|s)"/i;
|
|
9
|
+
// "try again in 250ms" / "try again in 12s" / "try again in 12sec"
|
|
10
|
+
const TRY_AGAIN_PATTERN = /try again in\s+(\d+(?:\.\d+)?)\s*(ms|s)(?:ec)?/i;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Server-suggested retry delay extraction. Merges the patterns historically used
|
|
14
|
+
* by the OpenAI Codex and Google Gemini retry helpers.
|
|
15
|
+
*
|
|
16
|
+
* Header sources (checked in order):
|
|
17
|
+
* - `Retry-After` (numeric seconds, or HTTP date)
|
|
18
|
+
* - `x-ratelimit-reset` (Unix epoch seconds)
|
|
19
|
+
* - `x-ratelimit-reset-after` (seconds)
|
|
20
|
+
*
|
|
21
|
+
* Body patterns:
|
|
22
|
+
* - `Your quota will reset after 18h31m10s` / `10m15s` / `39s`
|
|
23
|
+
* - `Please retry in 250ms` / `Please retry in 12s`
|
|
24
|
+
* - `"retryDelay": "34.074824224s"` (JSON error detail field)
|
|
25
|
+
* - `try again in 250ms` / `try again in 12s` / `try again in 12sec`
|
|
26
|
+
*
|
|
27
|
+
* Returns `undefined` if no signal is found.
|
|
28
|
+
*/
|
|
29
|
+
export function extractRetryHint(source: Response | Headers | null | undefined, body?: string): number | undefined {
|
|
30
|
+
const headers = source instanceof Headers ? source : (source?.headers ?? undefined);
|
|
31
|
+
if (headers) {
|
|
32
|
+
const retryAfter = headers.get("retry-after");
|
|
33
|
+
if (retryAfter) {
|
|
34
|
+
const seconds = Number(retryAfter);
|
|
35
|
+
if (Number.isFinite(seconds)) return Math.max(0, seconds * 1000);
|
|
36
|
+
const parsedDate = Date.parse(retryAfter);
|
|
37
|
+
if (!Number.isNaN(parsedDate)) return Math.max(0, parsedDate - Date.now());
|
|
38
|
+
}
|
|
39
|
+
const rateLimitReset = headers.get("x-ratelimit-reset");
|
|
40
|
+
if (rateLimitReset) {
|
|
41
|
+
const resetSeconds = Number.parseInt(rateLimitReset, 10);
|
|
42
|
+
if (!Number.isNaN(resetSeconds)) {
|
|
43
|
+
const delta = resetSeconds * 1000 - Date.now();
|
|
44
|
+
if (delta > 0) return delta;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
const rateLimitResetAfter = headers.get("x-ratelimit-reset-after");
|
|
48
|
+
if (rateLimitResetAfter) {
|
|
49
|
+
const seconds = Number(rateLimitResetAfter);
|
|
50
|
+
if (Number.isFinite(seconds) && seconds > 0) return seconds * 1000;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (!body) return undefined;
|
|
55
|
+
|
|
56
|
+
const quotaMatch = QUOTA_RESET_PATTERN.exec(body);
|
|
57
|
+
if (quotaMatch) {
|
|
58
|
+
const hours = quotaMatch[1] ? Number.parseInt(quotaMatch[1], 10) : 0;
|
|
59
|
+
const minutes = quotaMatch[2] ? Number.parseInt(quotaMatch[2], 10) : 0;
|
|
60
|
+
const seconds = Number.parseFloat(quotaMatch[3]!);
|
|
61
|
+
if (!Number.isNaN(seconds)) {
|
|
62
|
+
const totalMs = ((hours * 60 + minutes) * 60 + seconds) * 1000;
|
|
63
|
+
if (totalMs > 0) return totalMs;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
for (const pattern of [PLEASE_RETRY_PATTERN, RETRY_DELAY_FIELD_PATTERN, TRY_AGAIN_PATTERN]) {
|
|
67
|
+
const match = pattern.exec(body);
|
|
68
|
+
if (match?.[1]) {
|
|
69
|
+
const value = Number.parseFloat(match[1]);
|
|
70
|
+
if (Number.isFinite(value) && value > 0) {
|
|
71
|
+
return match[2]!.toLowerCase() === "ms" ? value : value * 1000;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return undefined;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export interface FetchWithRetryOptions extends RequestInit {
|
|
79
|
+
/** Total fetch attempts (initial + retries). Default `5`. */
|
|
80
|
+
maxAttempts?: number;
|
|
81
|
+
/**
|
|
82
|
+
* Per-delay cap. Server-provided `Retry-After` hints exceeding this return
|
|
83
|
+
* the current response immediately — caller deals with the `!response.ok`.
|
|
84
|
+
* Default `60_000`.
|
|
85
|
+
*/
|
|
86
|
+
maxDelayMs?: number;
|
|
87
|
+
/**
|
|
88
|
+
* Fallback delay schedule when no server hint is present. Number, array
|
|
89
|
+
* (indexed by attempt, clamped to last), or function. Default exponential
|
|
90
|
+
* `500ms * 2 ** attempt` capped at `maxDelayMs`.
|
|
91
|
+
*/
|
|
92
|
+
defaultDelayMs?: number | readonly number[] | ((attempt: number) => number);
|
|
93
|
+
/**
|
|
94
|
+
* Optional per-attempt overlay merged into the base `RequestInit` each try.
|
|
95
|
+
* Headers from the overlay shallow-merge over the base. Useful for auth
|
|
96
|
+
* token refresh or user-agent rotation.
|
|
97
|
+
*/
|
|
98
|
+
prepareInit?: (attempt: number) => RequestInit | Promise<RequestInit>;
|
|
99
|
+
/**
|
|
100
|
+
* Optional `fetch` implementation override. Defaults to `globalThis.fetch`.
|
|
101
|
+
* Useful for routing requests through a proxy, instrumented transport, or
|
|
102
|
+
* mock during tests.
|
|
103
|
+
*/
|
|
104
|
+
fetch?: (input: string | URL | Request, init?: RequestInit) => Promise<Response>;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const DEFAULT_MAX_DELAY_MS = 60_000;
|
|
108
|
+
const DEFAULT_MAX_ATTEMPTS = 5;
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Fetch with bounded retries and sensible defaults. Retries on any
|
|
112
|
+
* `isRetryableStatus` (5xx, 408, 429) and on transient network errors. Server
|
|
113
|
+
* `Retry-After`/quota hints are honoured up to `maxDelayMs`; a hint that exceeds
|
|
114
|
+
* the cap returns the current response so the caller can fail fast. Aborts on
|
|
115
|
+
* `init.signal` propagate as `"Request was aborted"`.
|
|
116
|
+
*
|
|
117
|
+
* The caller is responsible for inspecting `!response.ok` once the call returns.
|
|
118
|
+
*/
|
|
119
|
+
export async function fetchWithRetry(
|
|
120
|
+
url: string | URL | ((attempt: number) => string | URL),
|
|
121
|
+
options: FetchWithRetryOptions = {},
|
|
122
|
+
): Promise<Response> {
|
|
123
|
+
const {
|
|
124
|
+
maxAttempts = DEFAULT_MAX_ATTEMPTS,
|
|
125
|
+
maxDelayMs = DEFAULT_MAX_DELAY_MS,
|
|
126
|
+
defaultDelayMs,
|
|
127
|
+
prepareInit,
|
|
128
|
+
fetch: fetchImpl = fetch,
|
|
129
|
+
...baseInit
|
|
130
|
+
} = options;
|
|
131
|
+
const signal = baseInit.signal as AbortSignal | undefined;
|
|
132
|
+
|
|
133
|
+
for (let attempt = 0; ; attempt++) {
|
|
134
|
+
if (signal?.aborted) throw new Error("Request was aborted");
|
|
135
|
+
const requestUrl = typeof url === "function" ? url(attempt) : url;
|
|
136
|
+
const init = prepareInit ? mergeInit(baseInit, await prepareInit(attempt)) : baseInit;
|
|
137
|
+
|
|
138
|
+
let response: Response;
|
|
139
|
+
try {
|
|
140
|
+
response = await fetchImpl(requestUrl, init);
|
|
141
|
+
} catch (error) {
|
|
142
|
+
if (signal?.aborted) throw new Error("Request was aborted");
|
|
143
|
+
const wrapped = wrapNetworkError(error);
|
|
144
|
+
if (attempt + 1 >= maxAttempts) throw wrapped;
|
|
145
|
+
await scheduler.wait(resolveDefaultDelay(defaultDelayMs, attempt, maxDelayMs), { signal });
|
|
146
|
+
continue;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
if (!isRetryableStatus(response.status)) return response;
|
|
150
|
+
if (attempt + 1 >= maxAttempts) return response;
|
|
151
|
+
|
|
152
|
+
const hint = extractRetryHint(response, await response.clone().text());
|
|
153
|
+
if (hint !== undefined && hint > maxDelayMs) return response;
|
|
154
|
+
|
|
155
|
+
const delayMs = Math.min(hint ?? resolveDefaultDelay(defaultDelayMs, attempt, maxDelayMs), maxDelayMs);
|
|
156
|
+
await scheduler.wait(delayMs, { signal });
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
function mergeInit(base: RequestInit, overlay: RequestInit): RequestInit {
|
|
161
|
+
const merged: RequestInit = { ...base, ...overlay };
|
|
162
|
+
if (base.headers || overlay.headers) {
|
|
163
|
+
const baseHeaders = new Headers(base.headers ?? undefined);
|
|
164
|
+
const overlayHeaders = new Headers(overlay.headers ?? undefined);
|
|
165
|
+
overlayHeaders.forEach((value, key) => {
|
|
166
|
+
baseHeaders.set(key, value);
|
|
167
|
+
});
|
|
168
|
+
merged.headers = baseHeaders;
|
|
169
|
+
}
|
|
170
|
+
return merged;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function wrapNetworkError(error: unknown): Error {
|
|
174
|
+
if (error instanceof Error) {
|
|
175
|
+
if (error.name === "AbortError" || error.message === "Request was aborted") {
|
|
176
|
+
return new Error("Request was aborted");
|
|
177
|
+
}
|
|
178
|
+
if (error.message === "fetch failed" && error.cause instanceof Error) {
|
|
179
|
+
return new Error(`Network error: ${error.cause.message}`);
|
|
180
|
+
}
|
|
181
|
+
return error;
|
|
182
|
+
}
|
|
183
|
+
return new Error(String(error));
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
function resolveDefaultDelay(
|
|
187
|
+
option: FetchWithRetryOptions["defaultDelayMs"],
|
|
188
|
+
attempt: number,
|
|
189
|
+
maxDelayMs: number,
|
|
190
|
+
): number {
|
|
191
|
+
if (option === undefined) return Math.min(500 * 2 ** attempt, maxDelayMs);
|
|
192
|
+
if (typeof option === "number") return Math.min(option, maxDelayMs);
|
|
193
|
+
if (typeof option === "function") return Math.min(option(attempt), maxDelayMs);
|
|
194
|
+
return Math.min(option[Math.min(attempt, option.length - 1)] ?? 0, maxDelayMs);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Inspect an arbitrary error value (or its `cause` chain, up to depth 2) for an
|
|
199
|
+
* HTTP status code. Reads `status`, `statusCode`, and `response.status` fields,
|
|
200
|
+
* coerces string values, and falls back to scanning the error message for
|
|
201
|
+
* common patterns like `error (429)` or `HTTP 503`.
|
|
202
|
+
*/
|
|
203
|
+
export function extractHttpStatusFromError(error: unknown): number | undefined {
|
|
204
|
+
return extractHttpStatusFromErrorInternal(error, 0);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
type HttpErrorLike = {
|
|
208
|
+
message?: string;
|
|
209
|
+
name?: string;
|
|
210
|
+
status?: number | string;
|
|
211
|
+
statusCode?: number | string;
|
|
212
|
+
response?: { status?: number | string };
|
|
213
|
+
cause?: unknown;
|
|
214
|
+
};
|
|
215
|
+
|
|
216
|
+
function extractHttpStatusFromErrorInternal(error: unknown, depth: number): number | undefined {
|
|
217
|
+
if (!error || typeof error !== "object" || depth > 2) return undefined;
|
|
218
|
+
const info = error as HttpErrorLike;
|
|
219
|
+
const rawStatus = info.status ?? info.statusCode ?? info.response?.status;
|
|
220
|
+
|
|
221
|
+
let status: number | undefined;
|
|
222
|
+
if (typeof rawStatus === "number" && Number.isFinite(rawStatus)) {
|
|
223
|
+
status = rawStatus;
|
|
224
|
+
} else if (typeof rawStatus === "string") {
|
|
225
|
+
const parsed = Number(rawStatus);
|
|
226
|
+
if (Number.isFinite(parsed)) status = parsed;
|
|
227
|
+
}
|
|
228
|
+
if (status !== undefined && status >= 100 && status <= 599) return status;
|
|
229
|
+
|
|
230
|
+
if (info.message) {
|
|
231
|
+
const extracted = extractStatusFromMessage(info.message);
|
|
232
|
+
if (extracted !== undefined) return extracted;
|
|
233
|
+
}
|
|
234
|
+
if (info.cause) return extractHttpStatusFromErrorInternal(info.cause, depth + 1);
|
|
235
|
+
return undefined;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
const STATUS_MESSAGE_PATTERNS = [
|
|
239
|
+
/error\s*\((\d{3})\)/i,
|
|
240
|
+
/status\s*[:=]?\s*(\d{3})/i,
|
|
241
|
+
/\bhttp\s*(\d{3})\b/i,
|
|
242
|
+
/\b(\d{3})\s*(?:status|error)\b/i,
|
|
243
|
+
] as const;
|
|
244
|
+
|
|
245
|
+
function extractStatusFromMessage(message: string): number | undefined {
|
|
246
|
+
for (const pattern of STATUS_MESSAGE_PATTERNS) {
|
|
247
|
+
const match = pattern.exec(message);
|
|
248
|
+
if (!match) continue;
|
|
249
|
+
const value = Number(match[1]);
|
|
250
|
+
if (Number.isFinite(value) && value >= 100 && value <= 599) return value;
|
|
251
|
+
}
|
|
252
|
+
return undefined;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* `true` if the given HTTP status code is one we treat as transient: 408
|
|
257
|
+
* (Request Timeout), 429 (Too Many Requests), or any 5xx (server error).
|
|
258
|
+
*/
|
|
259
|
+
export function isRetryableStatus(status: number): boolean {
|
|
260
|
+
return status >= 500 || status === 408 || status === 429;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* `true` if the message describes an unexpected socket closure — Bun and some
|
|
265
|
+
* proxies surface these for any HTTP/2 stream reset.
|
|
266
|
+
*/
|
|
267
|
+
export function isUnexpectedSocketCloseMessage(message: string): boolean {
|
|
268
|
+
return /\b(?:the\s+)?socket connection (?:was )?closed unexpectedly\b/i.test(message);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
const TRANSIENT_MESSAGE_PATTERN =
|
|
272
|
+
/overloaded|rate.?limit|too many requests|service.?unavailable|server error|internal error|connection.?error|unable to connect|fetch failed|network error|stream stall|other side closed/i;
|
|
273
|
+
|
|
274
|
+
const VALIDATION_MESSAGE_PATTERN =
|
|
275
|
+
/invalid|validation|bad request|unsupported|schema|missing required|not found|unauthorized|forbidden/i;
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* Identify errors that should be retried: aborts/timeouts in the error name or
|
|
279
|
+
* message, retryable HTTP statuses (see `isRetryableStatus`), unexpected socket
|
|
280
|
+
* closes, and the standard transient phrases. 4xx statuses other than 408/429
|
|
281
|
+
* and validation-shaped messages short-circuit to `false`.
|
|
282
|
+
*/
|
|
283
|
+
export function isRetryableError(error: unknown): boolean {
|
|
284
|
+
const info = error as { message?: string; name?: string } | null;
|
|
285
|
+
const message = info?.message ?? "";
|
|
286
|
+
const name = info?.name ?? "";
|
|
287
|
+
if (name === "AbortError" || /timeout|timed out|aborted/i.test(message)) return true;
|
|
288
|
+
|
|
289
|
+
const status = extractHttpStatusFromError(error);
|
|
290
|
+
if (status !== undefined) {
|
|
291
|
+
if (isRetryableStatus(status)) return true;
|
|
292
|
+
if (status >= 400 && status < 500) return false;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
if (VALIDATION_MESSAGE_PATTERN.test(message)) return false;
|
|
296
|
+
return isUnexpectedSocketCloseMessage(message) || TRANSIENT_MESSAGE_PATTERN.test(message);
|
|
297
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { createAbortableStream, once, untilAborted } from "./abortable";
|
|
2
2
|
export * from "./async";
|
|
3
3
|
export * from "./color";
|
|
4
4
|
export * from "./dirs";
|
|
5
5
|
export * from "./env";
|
|
6
|
+
export * from "./fetch-retry";
|
|
6
7
|
export * from "./format";
|
|
7
8
|
export * from "./frontmatter";
|
|
8
9
|
export * from "./fs-error";
|
package/src/prompt.ts
CHANGED
|
@@ -66,6 +66,13 @@ function compactTableSep(line: string): string {
|
|
|
66
66
|
return `|${normalized.join("|")}|`;
|
|
67
67
|
}
|
|
68
68
|
|
|
69
|
+
const HTML_COMMENT_OPEN = "<!--";
|
|
70
|
+
const HTML_COMMENT_CLOSE = "-->";
|
|
71
|
+
|
|
72
|
+
type HtmlCommentState = {
|
|
73
|
+
inHtmlComment: boolean;
|
|
74
|
+
};
|
|
75
|
+
|
|
69
76
|
function replaceCommonAsciiSymbols(line: string): string {
|
|
70
77
|
return line
|
|
71
78
|
.replace(/\.{3}/g, "…")
|
|
@@ -77,6 +84,47 @@ function replaceCommonAsciiSymbols(line: string): string {
|
|
|
77
84
|
.replace(/>=/g, "≥");
|
|
78
85
|
}
|
|
79
86
|
|
|
87
|
+
function replaceCommonAsciiSymbolsOutsideHtmlComments(line: string, state: HtmlCommentState): string {
|
|
88
|
+
if (!state.inHtmlComment && !line.includes(HTML_COMMENT_OPEN) && !line.includes(HTML_COMMENT_CLOSE)) {
|
|
89
|
+
return replaceCommonAsciiSymbols(line);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
let result = "";
|
|
93
|
+
let cursor = 0;
|
|
94
|
+
|
|
95
|
+
while (cursor < line.length) {
|
|
96
|
+
if (state.inHtmlComment) {
|
|
97
|
+
const closeIndex = line.indexOf(HTML_COMMENT_CLOSE, cursor);
|
|
98
|
+
if (closeIndex === -1) {
|
|
99
|
+
return result + line.slice(cursor);
|
|
100
|
+
}
|
|
101
|
+
result += line.slice(cursor, closeIndex + HTML_COMMENT_CLOSE.length);
|
|
102
|
+
cursor = closeIndex + HTML_COMMENT_CLOSE.length;
|
|
103
|
+
state.inHtmlComment = false;
|
|
104
|
+
continue;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const openIndex = line.indexOf(HTML_COMMENT_OPEN, cursor);
|
|
108
|
+
if (openIndex === -1) {
|
|
109
|
+
result += replaceCommonAsciiSymbols(line.slice(cursor));
|
|
110
|
+
return result;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
result += replaceCommonAsciiSymbols(line.slice(cursor, openIndex));
|
|
114
|
+
const closeIndex = line.indexOf(HTML_COMMENT_CLOSE, openIndex + HTML_COMMENT_OPEN.length);
|
|
115
|
+
if (closeIndex === -1) {
|
|
116
|
+
result += line.slice(openIndex);
|
|
117
|
+
state.inHtmlComment = true;
|
|
118
|
+
return result;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
result += line.slice(openIndex, closeIndex + HTML_COMMENT_CLOSE.length);
|
|
122
|
+
cursor = closeIndex + HTML_COMMENT_CLOSE.length;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
return result;
|
|
126
|
+
}
|
|
127
|
+
|
|
80
128
|
export function format(content: string, options: PromptFormatOptions = {}): string {
|
|
81
129
|
const {
|
|
82
130
|
renderPhase = "post-render",
|
|
@@ -87,6 +135,8 @@ export function format(content: string, options: PromptFormatOptions = {}): stri
|
|
|
87
135
|
const lines = content.split("\n");
|
|
88
136
|
const result: string[] = [];
|
|
89
137
|
let inCodeBlock = false;
|
|
138
|
+
|
|
139
|
+
const htmlCommentState: HtmlCommentState = { inHtmlComment: false };
|
|
90
140
|
const topLevelTags: string[] = [];
|
|
91
141
|
|
|
92
142
|
for (let i = 0; i < lines.length; i++) {
|
|
@@ -104,7 +154,7 @@ export function format(content: string, options: PromptFormatOptions = {}): stri
|
|
|
104
154
|
}
|
|
105
155
|
|
|
106
156
|
if (replaceAsciiSymbols) {
|
|
107
|
-
line =
|
|
157
|
+
line = replaceCommonAsciiSymbolsOutsideHtmlComments(line, htmlCommentState);
|
|
108
158
|
}
|
|
109
159
|
trimmedStart = line.trimStart();
|
|
110
160
|
const trimmed = line.trim();
|