@cortexkit/aft-opencode 0.18.4 → 0.19.1
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/bg-notifications.d.ts.map +1 -1
- package/dist/configure-warnings.d.ts +31 -0
- package/dist/configure-warnings.d.ts.map +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +18049 -17794
- package/dist/logger.d.ts +18 -0
- package/dist/logger.d.ts.map +1 -1
- package/dist/notifications.d.ts.map +1 -1
- package/dist/shared/last-assistant-model.d.ts +51 -0
- package/dist/shared/last-assistant-model.d.ts.map +1 -0
- package/dist/shared/session-directory.d.ts +32 -0
- package/dist/shared/session-directory.d.ts.map +1 -0
- package/dist/tools/_shared.d.ts +26 -4
- package/dist/tools/_shared.d.ts.map +1 -1
- package/dist/tools/bash.d.ts.map +1 -1
- package/dist/tools/hoisted.d.ts +9 -1
- package/dist/tools/hoisted.d.ts.map +1 -1
- package/dist/tools/reading.d.ts +8 -2
- package/dist/tools/reading.d.ts.map +1 -1
- package/dist/tui.js +7 -6
- package/dist/types.d.ts +1 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/workflow-hints.d.ts.map +1 -1
- package/package.json +7 -6
- package/src/logger.ts +19 -0
- package/src/shared/last-assistant-model.ts +179 -0
- package/src/shared/session-directory.ts +152 -0
- package/dist/bridge.d.ts +0 -122
- package/dist/bridge.d.ts.map +0 -1
- package/dist/downloader.d.ts +0 -35
- package/dist/downloader.d.ts.map +0 -1
- package/dist/onnx-runtime.d.ts +0 -53
- package/dist/onnx-runtime.d.ts.map +0 -1
- package/dist/platform.d.ts +0 -21
- package/dist/platform.d.ts.map +0 -1
- package/dist/pool.d.ts +0 -66
- package/dist/pool.d.ts.map +0 -1
- package/dist/resolver.d.ts +0 -36
- package/dist/resolver.d.ts.map +0 -1
- package/dist/shared/last-user-model.d.ts +0 -38
- package/dist/shared/last-user-model.d.ts.map +0 -1
- package/dist/shared/url-fetch.d.ts +0 -33
- package/dist/shared/url-fetch.d.ts.map +0 -1
- package/src/shared/last-user-model.ts +0 -133
- package/src/shared/url-fetch.ts +0 -465
|
@@ -1,133 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Cache-busting fix: every PromptInput we send to OpenCode starts a new
|
|
3
|
-
* assistant turn (via `noReply: false`) or stores a new user message
|
|
4
|
-
* (via `noReply: true`). OpenCode's `createUserMessage` (in
|
|
5
|
-
* `packages/opencode/src/session/prompt.ts`) only preserves the user's
|
|
6
|
-
* model variant if we pass it explicitly — otherwise it falls back to
|
|
7
|
-
* `agent.variant` or undefined, silently switching the next assistant
|
|
8
|
-
* turn off the variant the human chose. That switch evicts the
|
|
9
|
-
* provider's prefix cache on every notification.
|
|
10
|
-
*
|
|
11
|
-
* This module fetches the last real user message's `{ providerID,
|
|
12
|
-
* modelID, variant }` and exposes it so prompt callers can include it
|
|
13
|
-
* verbatim in `PromptInput.model` + `PromptInput.variant`. Results are
|
|
14
|
-
* memoised per session for a short window so a batch of warnings or
|
|
15
|
-
* bg-completions only hits the messages API once.
|
|
16
|
-
*/
|
|
17
|
-
|
|
18
|
-
interface SessionMessageInfo {
|
|
19
|
-
id?: string;
|
|
20
|
-
role?: string;
|
|
21
|
-
parts?: Array<{ ignored?: boolean }>;
|
|
22
|
-
model?: {
|
|
23
|
-
providerID?: string;
|
|
24
|
-
modelID?: string;
|
|
25
|
-
variant?: string;
|
|
26
|
-
};
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
interface SessionMessage {
|
|
30
|
-
info?: SessionMessageInfo;
|
|
31
|
-
parts?: Array<{ ignored?: boolean }>;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
interface OpenCodeClientShape {
|
|
35
|
-
session?: {
|
|
36
|
-
messages?: (input: { path: { id: string } }) => Promise<{ data?: SessionMessage[] }>;
|
|
37
|
-
};
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
export interface LastUserModel {
|
|
41
|
-
providerID: string;
|
|
42
|
-
modelID: string;
|
|
43
|
-
variant?: string;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
interface CacheEntry {
|
|
47
|
-
expiresAt: number;
|
|
48
|
-
model: LastUserModel | null;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
const CACHE_TTL_MS = 5_000;
|
|
52
|
-
const CACHE_MAX_ENTRIES = 100;
|
|
53
|
-
const cache = new Map<string, CacheEntry>();
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* Returns the most recent real user message's model + variant for the
|
|
57
|
-
* session, or `null` when the session has no user messages or the
|
|
58
|
-
* client doesn't expose `session.messages`. Result is cached for
|
|
59
|
-
* {@link CACHE_TTL_MS} ms per session — long enough to coalesce a
|
|
60
|
-
* batch of notifications, short enough that an actual variant change
|
|
61
|
-
* inside the conversation is picked up promptly.
|
|
62
|
-
*
|
|
63
|
-
* NOTE: Skips synthetic user messages we just produced ourselves
|
|
64
|
-
* (those whose only parts are `ignored: true`). Otherwise our own
|
|
65
|
-
* announcement could pin the variant after a single round-trip and
|
|
66
|
-
* defeat the fix.
|
|
67
|
-
*/
|
|
68
|
-
export async function getLastUserModel(
|
|
69
|
-
client: unknown,
|
|
70
|
-
sessionId: string,
|
|
71
|
-
): Promise<LastUserModel | null> {
|
|
72
|
-
const now = Date.now();
|
|
73
|
-
const cached = cache.get(sessionId);
|
|
74
|
-
if (cached && cached.expiresAt > now) {
|
|
75
|
-
cache.delete(sessionId);
|
|
76
|
-
cache.set(sessionId, cached);
|
|
77
|
-
return cached.model;
|
|
78
|
-
}
|
|
79
|
-
if (cached) cache.delete(sessionId);
|
|
80
|
-
|
|
81
|
-
const c = client as OpenCodeClientShape;
|
|
82
|
-
const fetcher = c?.session?.messages;
|
|
83
|
-
if (typeof fetcher !== "function") {
|
|
84
|
-
return null;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
let messages: SessionMessage[];
|
|
88
|
-
try {
|
|
89
|
-
const result = await fetcher({ path: { id: sessionId } });
|
|
90
|
-
messages = result?.data ?? [];
|
|
91
|
-
} catch {
|
|
92
|
-
// Don't cache failures — a transient API error shouldn't pin the
|
|
93
|
-
// session's variant resolution to "unknown" for 5 s.
|
|
94
|
-
return null;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
for (let i = messages.length - 1; i >= 0; i--) {
|
|
98
|
-
const info = messages[i]?.info;
|
|
99
|
-
if (info?.role !== "user") continue;
|
|
100
|
-
if (isSyntheticIgnoredMessage(messages[i])) continue;
|
|
101
|
-
const model = info.model;
|
|
102
|
-
if (!model?.providerID || !model?.modelID) continue;
|
|
103
|
-
const resolved: LastUserModel = {
|
|
104
|
-
providerID: model.providerID,
|
|
105
|
-
modelID: model.modelID,
|
|
106
|
-
...(model.variant ? { variant: model.variant } : {}),
|
|
107
|
-
};
|
|
108
|
-
setCache(sessionId, resolved);
|
|
109
|
-
return resolved;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
return null;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
function setCache(sessionId: string, model: LastUserModel | null): void {
|
|
116
|
-
if (cache.has(sessionId)) cache.delete(sessionId);
|
|
117
|
-
cache.set(sessionId, { expiresAt: Date.now() + CACHE_TTL_MS, model });
|
|
118
|
-
while (cache.size > CACHE_MAX_ENTRIES) {
|
|
119
|
-
const oldest = cache.keys().next().value;
|
|
120
|
-
if (typeof oldest !== "string") break;
|
|
121
|
-
cache.delete(oldest);
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
function isSyntheticIgnoredMessage(message: SessionMessage | undefined): boolean {
|
|
126
|
-
const parts = message?.info?.parts ?? message?.parts;
|
|
127
|
-
return Array.isArray(parts) && parts.length > 0 && parts.every((part) => part?.ignored === true);
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
/** Test-only: drop the cache so unit tests can simulate fresh sessions. */
|
|
131
|
-
export function __resetLastUserModelCacheForTests(): void {
|
|
132
|
-
cache.clear();
|
|
133
|
-
}
|
package/src/shared/url-fetch.ts
DELETED
|
@@ -1,465 +0,0 @@
|
|
|
1
|
-
import { createHash } from "node:crypto";
|
|
2
|
-
import { lookup } from "node:dns/promises";
|
|
3
|
-
import {
|
|
4
|
-
existsSync,
|
|
5
|
-
mkdirSync,
|
|
6
|
-
readdirSync,
|
|
7
|
-
readFileSync,
|
|
8
|
-
unlinkSync,
|
|
9
|
-
writeFileSync,
|
|
10
|
-
} from "node:fs";
|
|
11
|
-
import { isIP } from "node:net";
|
|
12
|
-
import { join } from "node:path";
|
|
13
|
-
import { Agent, type Dispatcher, fetch as undiciFetch } from "undici";
|
|
14
|
-
import { log, warn } from "../logger";
|
|
15
|
-
|
|
16
|
-
/** Max response body size (10 MB) */
|
|
17
|
-
const MAX_RESPONSE_BYTES = 10 * 1024 * 1024;
|
|
18
|
-
/** Cache TTL: 1 day */
|
|
19
|
-
const CACHE_TTL_MS = 24 * 60 * 60 * 1000;
|
|
20
|
-
/** Fetch timeout: 30 seconds */
|
|
21
|
-
const FETCH_TIMEOUT_MS = 30_000;
|
|
22
|
-
const MAX_REDIRECTS = 5;
|
|
23
|
-
|
|
24
|
-
interface FetchUrlOptions {
|
|
25
|
-
allowPrivate?: boolean;
|
|
26
|
-
/** Test-only injection point. Production fetches use undici with DNS pinning. */
|
|
27
|
-
fetchImpl?: FetchImpl;
|
|
28
|
-
/** Test-only injection point. Production DNS resolution uses node:dns/promises.lookup. */
|
|
29
|
-
lookup?: LookupFn;
|
|
30
|
-
/** Test-only injection point for observing the DNS-pinned dispatcher. */
|
|
31
|
-
dispatcherFactory?: (validatedIp: string) => Dispatcher;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
type LookupFn = typeof lookup;
|
|
35
|
-
interface FetchInit {
|
|
36
|
-
signal?: AbortSignal;
|
|
37
|
-
redirect?: "manual";
|
|
38
|
-
dispatcher?: Dispatcher;
|
|
39
|
-
headers?: Record<string, string>;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
type FetchImpl = (input: string, init: FetchInit) => Promise<Response>;
|
|
43
|
-
|
|
44
|
-
interface CacheMeta {
|
|
45
|
-
url: string;
|
|
46
|
-
contentType: string;
|
|
47
|
-
extension: string;
|
|
48
|
-
fetchedAt: number;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
function cacheDir(storageDir: string): string {
|
|
52
|
-
return join(storageDir, "url_cache");
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
function hashUrl(url: string): string {
|
|
56
|
-
return createHash("sha256").update(url).digest("hex").slice(0, 16);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
function metaPath(storageDir: string, hash: string): string {
|
|
60
|
-
return join(cacheDir(storageDir), `${hash}.meta.json`);
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
function contentPath(storageDir: string, hash: string, extension: string): string {
|
|
64
|
-
return join(cacheDir(storageDir), `${hash}${extension}`);
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
/** Exported for unit tests. */
|
|
68
|
-
export function _isPrivateIpv4(address: string): boolean {
|
|
69
|
-
const parts = address.split(".").map((part) => Number.parseInt(part, 10));
|
|
70
|
-
if (parts.length !== 4 || parts.some((part) => !Number.isInteger(part))) return true;
|
|
71
|
-
const [a, b] = parts;
|
|
72
|
-
return (
|
|
73
|
-
a === 0 || // 0.0.0.0/8 — wildcard, blocked to prevent local-host bypass
|
|
74
|
-
a === 10 ||
|
|
75
|
-
a === 127 ||
|
|
76
|
-
(a === 172 && b >= 16 && b <= 31) ||
|
|
77
|
-
(a === 192 && b === 168) ||
|
|
78
|
-
(a === 169 && b === 254) ||
|
|
79
|
-
a >= 224 // multicast (224/4) and reserved (240/4)
|
|
80
|
-
);
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* Expand a possibly-compressed IPv6 address to 8 hextets (numbers).
|
|
85
|
-
* Returns null on parse failure.
|
|
86
|
-
*/
|
|
87
|
-
function expandIpv6(addr: string): number[] | null {
|
|
88
|
-
if (addr === "::") return [0, 0, 0, 0, 0, 0, 0, 0];
|
|
89
|
-
// Reject more than one "::"
|
|
90
|
-
const dcMatches = addr.match(/::/g);
|
|
91
|
-
if (dcMatches && dcMatches.length > 1) return null;
|
|
92
|
-
|
|
93
|
-
// Handle dotted-quad tail by converting to two hextets.
|
|
94
|
-
let normalized = addr;
|
|
95
|
-
const lastColon = normalized.lastIndexOf(":");
|
|
96
|
-
if (lastColon !== -1) {
|
|
97
|
-
const tail = normalized.slice(lastColon + 1);
|
|
98
|
-
if (tail.includes(".")) {
|
|
99
|
-
const octets = tail.split(".").map((p) => Number.parseInt(p, 10));
|
|
100
|
-
if (octets.length !== 4 || octets.some((o) => Number.isNaN(o) || o < 0 || o > 255)) {
|
|
101
|
-
return null;
|
|
102
|
-
}
|
|
103
|
-
const h1 = ((octets[0] << 8) | octets[1]).toString(16);
|
|
104
|
-
const h2 = ((octets[2] << 8) | octets[3]).toString(16);
|
|
105
|
-
normalized = `${normalized.slice(0, lastColon)}:${h1}:${h2}`;
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
let parts: string[];
|
|
110
|
-
if (normalized.includes("::")) {
|
|
111
|
-
const [left, right] = normalized.split("::");
|
|
112
|
-
const leftParts = left ? left.split(":") : [];
|
|
113
|
-
const rightParts = right ? right.split(":") : [];
|
|
114
|
-
const fill = 8 - leftParts.length - rightParts.length;
|
|
115
|
-
if (fill < 0) return null;
|
|
116
|
-
parts = [...leftParts, ...Array(fill).fill("0"), ...rightParts];
|
|
117
|
-
} else {
|
|
118
|
-
parts = normalized.split(":");
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
if (parts.length !== 8) return null;
|
|
122
|
-
const hextets = parts.map((p) => {
|
|
123
|
-
const n = Number.parseInt(p, 16);
|
|
124
|
-
return Number.isNaN(n) || n < 0 || n > 0xffff ? -1 : n;
|
|
125
|
-
});
|
|
126
|
-
if (hextets.some((h) => h === -1)) return null;
|
|
127
|
-
return hextets;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
function isPrivateIp(address: string): boolean {
|
|
131
|
-
if (!address.includes(":")) {
|
|
132
|
-
return _isPrivateIpv4(address);
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
const lower = address.toLowerCase();
|
|
136
|
-
// Strip optional zone identifier (fe80::1%eth0 → fe80::1).
|
|
137
|
-
const noZone = lower.split("%")[0];
|
|
138
|
-
|
|
139
|
-
// Expand to 8 hextets so we can reliably detect IPv4-mapped/compatible
|
|
140
|
-
// forms even after URL canonicalization (e.g. [::ffff:127.0.0.1] →
|
|
141
|
-
// [::ffff:7f00:1]).
|
|
142
|
-
const hextets = expandIpv6(noZone);
|
|
143
|
-
if (hextets) {
|
|
144
|
-
// IPv4-mapped (::ffff:X.X.X.X) and IPv4-compatible (::X.X.X.X) — extract
|
|
145
|
-
// the embedded IPv4 from the last two hextets and check against IPv4 ranges.
|
|
146
|
-
const top6Zero = hextets.slice(0, 6).every((h) => h === 0);
|
|
147
|
-
const isMapped =
|
|
148
|
-
hextets[0] === 0 &&
|
|
149
|
-
hextets[1] === 0 &&
|
|
150
|
-
hextets[2] === 0 &&
|
|
151
|
-
hextets[3] === 0 &&
|
|
152
|
-
hextets[4] === 0 &&
|
|
153
|
-
hextets[5] === 0xffff;
|
|
154
|
-
if (isMapped || top6Zero) {
|
|
155
|
-
const a = (hextets[6] >> 8) & 0xff;
|
|
156
|
-
const b = hextets[6] & 0xff;
|
|
157
|
-
const c = (hextets[7] >> 8) & 0xff;
|
|
158
|
-
const d = hextets[7] & 0xff;
|
|
159
|
-
const ipv4 = `${a}.${b}.${c}.${d}`;
|
|
160
|
-
// ::1 (loopback) and :: (unspecified) both fall into top6Zero and last
|
|
161
|
-
// hextet 0 or 1 — _isPrivateIpv4 already blocks 0.0.0.0 and 127/8.
|
|
162
|
-
if (top6Zero && hextets[6] === 0 && hextets[7] <= 1) {
|
|
163
|
-
return true;
|
|
164
|
-
}
|
|
165
|
-
return _isPrivateIpv4(ipv4);
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
const firstHextet = hextets[0];
|
|
169
|
-
return (
|
|
170
|
-
// fe80::/10 link-local
|
|
171
|
-
(firstHextet >= 0xfe80 && firstHextet <= 0xfebf) ||
|
|
172
|
-
// fc00::/7 unique local
|
|
173
|
-
(firstHextet >= 0xfc00 && firstHextet <= 0xfdff) ||
|
|
174
|
-
// ff00::/8 multicast
|
|
175
|
-
firstHextet >= 0xff00
|
|
176
|
-
);
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
// Could not expand — be conservative and block.
|
|
180
|
-
return true;
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
async function assertPublicUrl(
|
|
184
|
-
url: URL,
|
|
185
|
-
allowPrivate: boolean,
|
|
186
|
-
dnsLookup: LookupFn = lookup,
|
|
187
|
-
): Promise<string | undefined> {
|
|
188
|
-
if (url.protocol !== "http:" && url.protocol !== "https:") {
|
|
189
|
-
throw new Error(`Only http:// and https:// URLs are supported, got: ${url.protocol}`);
|
|
190
|
-
}
|
|
191
|
-
if (allowPrivate) return undefined;
|
|
192
|
-
|
|
193
|
-
// URL.hostname returns IPv6 literals wrapped in brackets ("[::1]") per WHATWG.
|
|
194
|
-
// Strip them before checking the address.
|
|
195
|
-
const hostname = url.hostname.replace(/^\[|\]$/g, "");
|
|
196
|
-
|
|
197
|
-
// If the hostname is already a literal IP, check it directly — skip DNS so we
|
|
198
|
-
// also catch malformed IPv6 forms (::127.0.0.1, ::ffff:127.0.0.1) that some
|
|
199
|
-
// resolvers reject as "not found" instead of returning the embedded IPv4.
|
|
200
|
-
if (isIP(hostname) || hostname.includes(":")) {
|
|
201
|
-
if (isPrivateIp(hostname)) {
|
|
202
|
-
throw new Error(`Blocked private URL host ${url.hostname} (${hostname})`);
|
|
203
|
-
}
|
|
204
|
-
return hostname;
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
const addresses = await dnsLookup(hostname, { all: true, verbatim: true });
|
|
208
|
-
for (const { address } of addresses) {
|
|
209
|
-
if (isPrivateIp(address)) {
|
|
210
|
-
throw new Error(`Blocked private URL host ${url.hostname} (${address})`);
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
if (addresses.length === 0) {
|
|
214
|
-
throw new Error(`Failed to resolve URL host ${url.hostname}`);
|
|
215
|
-
}
|
|
216
|
-
return addresses[0].address;
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
function createPinnedDispatcher(validatedIp: string): Dispatcher {
|
|
220
|
-
return new Agent({
|
|
221
|
-
connect: {
|
|
222
|
-
lookup: (_hostname, _opts, callback) => {
|
|
223
|
-
callback(null, validatedIp, validatedIp.includes(":") ? 6 : 4);
|
|
224
|
-
},
|
|
225
|
-
},
|
|
226
|
-
});
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
function resolveRedirectUrl(currentUrl: URL, location: string | null): URL {
|
|
230
|
-
if (!location) {
|
|
231
|
-
throw new Error(`Redirect from ${currentUrl.href} missing Location header`);
|
|
232
|
-
}
|
|
233
|
-
return new URL(location, currentUrl);
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
async function fetchWithRedirects(
|
|
237
|
-
startUrl: URL,
|
|
238
|
-
allowPrivate: boolean,
|
|
239
|
-
deps: Pick<FetchUrlOptions, "dispatcherFactory" | "fetchImpl" | "lookup"> = {},
|
|
240
|
-
): Promise<Response> {
|
|
241
|
-
let currentUrl = startUrl;
|
|
242
|
-
const fetchImpl = deps.fetchImpl ?? (undiciFetch as FetchImpl);
|
|
243
|
-
const dnsLookup = deps.lookup ?? lookup;
|
|
244
|
-
|
|
245
|
-
for (let redirectCount = 0; redirectCount <= MAX_REDIRECTS; redirectCount++) {
|
|
246
|
-
const validatedIp = await assertPublicUrl(currentUrl, allowPrivate, dnsLookup);
|
|
247
|
-
const dispatcher = validatedIp
|
|
248
|
-
? (deps.dispatcherFactory ?? createPinnedDispatcher)(validatedIp)
|
|
249
|
-
: undefined;
|
|
250
|
-
|
|
251
|
-
const controller = new AbortController();
|
|
252
|
-
const timer = setTimeout(() => controller.abort(), FETCH_TIMEOUT_MS);
|
|
253
|
-
let response: Response;
|
|
254
|
-
try {
|
|
255
|
-
response = await fetchImpl(currentUrl.href, {
|
|
256
|
-
signal: controller.signal,
|
|
257
|
-
redirect: "manual",
|
|
258
|
-
dispatcher,
|
|
259
|
-
headers: {
|
|
260
|
-
"user-agent": "aft-opencode-plugin",
|
|
261
|
-
// Prioritize markdown for content-negotiating servers (GitHub API, many docs sites).
|
|
262
|
-
// `application/vnd.github.raw` is GitHub's custom type — returns raw markdown from
|
|
263
|
-
// repo file and readme endpoints. Falls back to HTML if markdown is not available.
|
|
264
|
-
accept:
|
|
265
|
-
"application/vnd.github.raw, text/markdown, text/x-markdown, text/html;q=0.9, text/plain;q=0.5",
|
|
266
|
-
},
|
|
267
|
-
});
|
|
268
|
-
} catch (err) {
|
|
269
|
-
throw new Error(`Failed to fetch ${currentUrl.href}: ${(err as Error).message}`);
|
|
270
|
-
} finally {
|
|
271
|
-
clearTimeout(timer);
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
if (response.status < 300 || response.status >= 400) {
|
|
275
|
-
return response;
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
if (redirectCount === MAX_REDIRECTS) {
|
|
279
|
-
throw new Error(`Too many redirects fetching ${startUrl.href}`);
|
|
280
|
-
}
|
|
281
|
-
currentUrl = resolveRedirectUrl(currentUrl, response.headers.get("location"));
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
throw new Error(`Too many redirects fetching ${startUrl.href}`);
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
/**
|
|
288
|
-
* Map a Content-Type header to a file extension AFT can parse.
|
|
289
|
-
* Returns null for unsupported types.
|
|
290
|
-
*/
|
|
291
|
-
function resolveExtension(contentType: string): string | null {
|
|
292
|
-
// Normalize: strip parameters (after `;`) AND pick the first value if the server
|
|
293
|
-
// echoed back a comma-separated list (GitHub API does this for /readme endpoints).
|
|
294
|
-
const lower = contentType.toLowerCase().split(";")[0].split(",")[0].trim();
|
|
295
|
-
if (
|
|
296
|
-
lower === "text/html" ||
|
|
297
|
-
lower === "application/xhtml+xml" ||
|
|
298
|
-
lower === "application/vnd.github.html" ||
|
|
299
|
-
lower === "application/vnd.github+html"
|
|
300
|
-
) {
|
|
301
|
-
return ".html";
|
|
302
|
-
}
|
|
303
|
-
if (
|
|
304
|
-
lower === "text/markdown" ||
|
|
305
|
-
lower === "text/x-markdown" ||
|
|
306
|
-
lower === "application/markdown" ||
|
|
307
|
-
// GitHub API raw content type — returns raw markdown for README endpoints
|
|
308
|
-
lower === "application/vnd.github.raw" ||
|
|
309
|
-
lower === "application/vnd.github+raw" ||
|
|
310
|
-
lower === "application/vnd.github.v3.raw"
|
|
311
|
-
) {
|
|
312
|
-
return ".md";
|
|
313
|
-
}
|
|
314
|
-
if (lower === "text/plain") {
|
|
315
|
-
// treat plain text as markdown so aft_outline can show headings if present
|
|
316
|
-
return ".md";
|
|
317
|
-
}
|
|
318
|
-
return null;
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
/**
|
|
322
|
-
* Fetch a URL to a cached temp file. Uses disk cache with 1-day TTL.
|
|
323
|
-
* Returns the cached file path the Rust outline/zoom command can read.
|
|
324
|
-
* Throws on errors (invalid URL, network failure, unsupported content type, oversized body).
|
|
325
|
-
*/
|
|
326
|
-
export async function fetchUrlToTempFile(
|
|
327
|
-
url: string,
|
|
328
|
-
storageDir: string,
|
|
329
|
-
options: FetchUrlOptions = {},
|
|
330
|
-
): Promise<string> {
|
|
331
|
-
let parsed: URL;
|
|
332
|
-
try {
|
|
333
|
-
parsed = new URL(url);
|
|
334
|
-
} catch {
|
|
335
|
-
throw new Error(`Invalid URL: ${url}`);
|
|
336
|
-
}
|
|
337
|
-
if (parsed.protocol !== "http:" && parsed.protocol !== "https:") {
|
|
338
|
-
throw new Error(`Only http:// and https:// URLs are supported, got: ${parsed.protocol}`);
|
|
339
|
-
}
|
|
340
|
-
const allowPrivate = options.allowPrivate === true;
|
|
341
|
-
|
|
342
|
-
const dir = cacheDir(storageDir);
|
|
343
|
-
mkdirSync(dir, { recursive: true });
|
|
344
|
-
|
|
345
|
-
const hash = hashUrl(url);
|
|
346
|
-
const metaFile = metaPath(storageDir, hash);
|
|
347
|
-
|
|
348
|
-
// Check cache
|
|
349
|
-
if (existsSync(metaFile)) {
|
|
350
|
-
try {
|
|
351
|
-
const meta = JSON.parse(readFileSync(metaFile, "utf8")) as CacheMeta;
|
|
352
|
-
const age = Date.now() - meta.fetchedAt;
|
|
353
|
-
const cached = contentPath(storageDir, hash, meta.extension);
|
|
354
|
-
if (age < CACHE_TTL_MS && existsSync(cached)) {
|
|
355
|
-
log(`URL cache hit: ${url} (${Math.round(age / 1000)}s old)`);
|
|
356
|
-
return cached;
|
|
357
|
-
}
|
|
358
|
-
} catch {
|
|
359
|
-
// corrupted meta, re-fetch
|
|
360
|
-
}
|
|
361
|
-
}
|
|
362
|
-
|
|
363
|
-
log(`Fetching URL: ${url}`);
|
|
364
|
-
const response = await fetchWithRedirects(parsed, allowPrivate, options);
|
|
365
|
-
|
|
366
|
-
if (!response.ok) {
|
|
367
|
-
throw new Error(`HTTP ${response.status} ${response.statusText} fetching ${url}`);
|
|
368
|
-
}
|
|
369
|
-
|
|
370
|
-
const contentType = response.headers.get("content-type") || "text/plain";
|
|
371
|
-
const extension = resolveExtension(contentType);
|
|
372
|
-
if (!extension) {
|
|
373
|
-
throw new Error(
|
|
374
|
-
`Unsupported content type '${contentType}' for ${url}. Supported: text/html, text/markdown, text/plain`,
|
|
375
|
-
);
|
|
376
|
-
}
|
|
377
|
-
|
|
378
|
-
const lengthHeader = response.headers.get("content-length");
|
|
379
|
-
if (lengthHeader) {
|
|
380
|
-
const length = Number.parseInt(lengthHeader, 10);
|
|
381
|
-
if (Number.isFinite(length) && length > MAX_RESPONSE_BYTES) {
|
|
382
|
-
throw new Error(`Response too large: ${length} bytes (max ${MAX_RESPONSE_BYTES})`);
|
|
383
|
-
}
|
|
384
|
-
}
|
|
385
|
-
|
|
386
|
-
// Stream with size cap
|
|
387
|
-
const reader = response.body?.getReader();
|
|
388
|
-
if (!reader) {
|
|
389
|
-
throw new Error(`Failed to read response body for ${url}`);
|
|
390
|
-
}
|
|
391
|
-
const chunks: Uint8Array[] = [];
|
|
392
|
-
let total = 0;
|
|
393
|
-
while (true) {
|
|
394
|
-
const { done, value } = await reader.read();
|
|
395
|
-
if (done) break;
|
|
396
|
-
if (value) {
|
|
397
|
-
total += value.length;
|
|
398
|
-
if (total > MAX_RESPONSE_BYTES) {
|
|
399
|
-
reader.cancel().catch(() => {});
|
|
400
|
-
throw new Error(`Response exceeded ${MAX_RESPONSE_BYTES} bytes, aborted`);
|
|
401
|
-
}
|
|
402
|
-
chunks.push(value);
|
|
403
|
-
}
|
|
404
|
-
}
|
|
405
|
-
|
|
406
|
-
// Write content and meta atomically
|
|
407
|
-
const body = Buffer.concat(chunks);
|
|
408
|
-
const contentFile = contentPath(storageDir, hash, extension);
|
|
409
|
-
const tmpContent = `${contentFile}.tmp-${process.pid}`;
|
|
410
|
-
writeFileSync(tmpContent, body);
|
|
411
|
-
const { renameSync } = await import("node:fs");
|
|
412
|
-
renameSync(tmpContent, contentFile);
|
|
413
|
-
|
|
414
|
-
const meta: CacheMeta = {
|
|
415
|
-
url,
|
|
416
|
-
contentType,
|
|
417
|
-
extension,
|
|
418
|
-
fetchedAt: Date.now(),
|
|
419
|
-
};
|
|
420
|
-
const tmpMeta = `${metaFile}.tmp-${process.pid}`;
|
|
421
|
-
writeFileSync(tmpMeta, JSON.stringify(meta));
|
|
422
|
-
renameSync(tmpMeta, metaFile);
|
|
423
|
-
|
|
424
|
-
log(`URL cached (${total} bytes): ${url}`);
|
|
425
|
-
return contentFile;
|
|
426
|
-
}
|
|
427
|
-
|
|
428
|
-
/**
|
|
429
|
-
* Remove cache entries older than TTL. Called periodically at plugin startup.
|
|
430
|
-
*/
|
|
431
|
-
export function cleanupUrlCache(storageDir: string): void {
|
|
432
|
-
const dir = cacheDir(storageDir);
|
|
433
|
-
if (!existsSync(dir)) return;
|
|
434
|
-
|
|
435
|
-
let removed = 0;
|
|
436
|
-
try {
|
|
437
|
-
for (const entry of readdirSync(dir)) {
|
|
438
|
-
if (!entry.endsWith(".meta.json")) continue;
|
|
439
|
-
const metaFile = join(dir, entry);
|
|
440
|
-
try {
|
|
441
|
-
const meta = JSON.parse(readFileSync(metaFile, "utf8")) as CacheMeta;
|
|
442
|
-
const age = Date.now() - meta.fetchedAt;
|
|
443
|
-
if (age > CACHE_TTL_MS) {
|
|
444
|
-
const hash = entry.slice(0, -".meta.json".length);
|
|
445
|
-
const content = contentPath(storageDir, hash, meta.extension);
|
|
446
|
-
if (existsSync(content)) unlinkSync(content);
|
|
447
|
-
unlinkSync(metaFile);
|
|
448
|
-
removed++;
|
|
449
|
-
}
|
|
450
|
-
} catch {
|
|
451
|
-
// corrupted meta, remove it too
|
|
452
|
-
try {
|
|
453
|
-
unlinkSync(metaFile);
|
|
454
|
-
removed++;
|
|
455
|
-
} catch {}
|
|
456
|
-
}
|
|
457
|
-
}
|
|
458
|
-
} catch (err) {
|
|
459
|
-
warn(`URL cache cleanup failed: ${(err as Error).message}`);
|
|
460
|
-
return;
|
|
461
|
-
}
|
|
462
|
-
if (removed > 0) {
|
|
463
|
-
log(`URL cache cleanup: removed ${removed} stale entries`);
|
|
464
|
-
}
|
|
465
|
-
}
|