@koreanhole/opencodex 3.2.33 → 3.2.35
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
CHANGED
|
@@ -317,6 +317,7 @@ export async function runClineTurn(
|
|
|
317
317
|
try {
|
|
318
318
|
staged = await (deps.stageTurn ?? stageClineTurn)({
|
|
319
319
|
providerSettingsPath,
|
|
320
|
+
sourceEnv,
|
|
320
321
|
catalog: catalog.tools,
|
|
321
322
|
...(catalog.emittedNameMap.size > 0 ? { mcpLaunch: mcpLaunch() } : {}),
|
|
322
323
|
...(sessionRoot ? { root: sessionRoot } : {}),
|
|
@@ -381,6 +382,7 @@ export async function runClineTurn(
|
|
|
381
382
|
maxToolCalls: 16,
|
|
382
383
|
requireToolCall: catalog.requireToolCall,
|
|
383
384
|
incoming,
|
|
385
|
+
...(staged.diagnostics?.length ? { diagnostics: staged.diagnostics } : {}),
|
|
384
386
|
onVendorSession: sessionId => { vendorSessionId ??= sessionId; },
|
|
385
387
|
}, event => {
|
|
386
388
|
if ((event.type === "done" || event.type === "error" || event.type === "incomplete") && !terminal) terminal = event;
|
|
@@ -60,6 +60,7 @@ export interface ClineJsonTurnInput {
|
|
|
60
60
|
maxToolCalls: number;
|
|
61
61
|
requireToolCall: boolean;
|
|
62
62
|
incoming: IncomingMeta;
|
|
63
|
+
diagnostics?: readonly string[];
|
|
63
64
|
/** First vendor session id reported by the turn; enables continuation storage. */
|
|
64
65
|
onVendorSession?: (sessionId: string) => void;
|
|
65
66
|
}
|
|
@@ -428,8 +429,18 @@ export async function runClineJsonTurn(
|
|
|
428
429
|
let timeoutTimer: ReturnType<typeof setTimeout> | undefined;
|
|
429
430
|
let killTimer: ReturnType<typeof setTimeout> | undefined;
|
|
430
431
|
|
|
432
|
+
const withDiagnostics = (event: Extract<AdapterEvent, { type: "error" }>): Extract<AdapterEvent, { type: "error" }> => {
|
|
433
|
+
const notes = input.diagnostics?.filter(Boolean) ?? [];
|
|
434
|
+
if (notes.length === 0) return event;
|
|
435
|
+
if (event.code !== "cline_cli_not_logged_in" && event.message !== "Cline JSON ended without a terminal result.") {
|
|
436
|
+
return event;
|
|
437
|
+
}
|
|
438
|
+
const detail = redactSecretString(notes.join("; ")).slice(0, 768);
|
|
439
|
+
return { ...event, message: `${event.message} (${detail})` };
|
|
440
|
+
};
|
|
441
|
+
|
|
431
442
|
const deliver = (event: AdapterEvent): void => {
|
|
432
|
-
const
|
|
443
|
+
const candidate = event.type === "done" && input.requireToolCall && state.toolCalls === 0
|
|
433
444
|
? {
|
|
434
445
|
type: "error" as const,
|
|
435
446
|
message: "Cline returned no tool call for a request that requires one.",
|
|
@@ -439,6 +450,7 @@ export async function runClineJsonTurn(
|
|
|
439
450
|
retryable: false,
|
|
440
451
|
}
|
|
441
452
|
: event;
|
|
453
|
+
const terminal = candidate.type === "error" ? withDiagnostics(candidate) : candidate;
|
|
442
454
|
if (terminal.type === "done" || terminal.type === "error" || terminal.type === "incomplete") {
|
|
443
455
|
if (terminalEmitted) return;
|
|
444
456
|
terminalEmitted = true;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { accessSync, constants, readdirSync, statSync } from "node:fs";
|
|
2
|
+
import { chmod, copyFile, link, mkdir, mkdtemp, readFile, rm, stat, symlink, writeFile } from "node:fs/promises";
|
|
2
3
|
import { homedir, tmpdir } from "node:os";
|
|
3
4
|
import { basename, dirname, join, resolve } from "node:path";
|
|
4
5
|
import { baseScopedEnv } from "../env";
|
|
@@ -59,11 +60,13 @@ export interface ClineStagedTurn {
|
|
|
59
60
|
workspaceDir: string;
|
|
60
61
|
hooksDir: string;
|
|
61
62
|
providerSettingsPath: string;
|
|
63
|
+
hasProviderSettings?: boolean;
|
|
62
64
|
legacyGlobalStatePath: string;
|
|
63
65
|
legacySecretsPath?: string;
|
|
64
66
|
globalSettingsPath: string;
|
|
65
67
|
mcpSettingsPath: string;
|
|
66
68
|
catalogPath: string;
|
|
69
|
+
diagnostics?: string[];
|
|
67
70
|
cleanup: () => Promise<void>;
|
|
68
71
|
}
|
|
69
72
|
|
|
@@ -201,15 +204,221 @@ function parseClineSelectionDocument(parsed: unknown): ClineSelectionCandidate {
|
|
|
201
204
|
);
|
|
202
205
|
}
|
|
203
206
|
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
207
|
+
const CLINE_DATA_AUTH_FILES = [
|
|
208
|
+
"secrets.json",
|
|
209
|
+
"globalState.json",
|
|
210
|
+
"auth.json",
|
|
211
|
+
"credentials.json",
|
|
212
|
+
"accounts.json",
|
|
213
|
+
"oauth.json",
|
|
214
|
+
"oauth_creds.json",
|
|
215
|
+
"tokens.json",
|
|
216
|
+
".env",
|
|
217
|
+
] as const;
|
|
218
|
+
const CLINE_CONFIG_AUTH_FILES = ["providers.json", ...CLINE_DATA_AUTH_FILES] as const;
|
|
219
|
+
const CLINE_OWNED_SETTINGS_FILES = new Set(["providers.json", "global-settings.json", "cline_mcp_settings.json"]);
|
|
220
|
+
|
|
221
|
+
const CLINE_FORWARDED_PROXY_KEYS = [
|
|
222
|
+
"HTTP_PROXY",
|
|
223
|
+
"HTTPS_PROXY",
|
|
224
|
+
"ALL_PROXY",
|
|
225
|
+
"NO_PROXY",
|
|
226
|
+
"http_proxy",
|
|
227
|
+
"https_proxy",
|
|
228
|
+
"no_proxy",
|
|
229
|
+
] as const;
|
|
230
|
+
|
|
231
|
+
const CLINE_FORWARDED_AUTH_KEYS = [
|
|
232
|
+
"CLINE_API_KEY",
|
|
233
|
+
"CLINE_PROVIDER",
|
|
234
|
+
"CLINE_MODEL",
|
|
235
|
+
"CLINE_MODEL_ID",
|
|
236
|
+
"CLINE_BASE_URL",
|
|
237
|
+
"ANTHROPIC_API_KEY",
|
|
238
|
+
"ANTHROPIC_BASE_URL",
|
|
239
|
+
"OPENAI_API_KEY",
|
|
240
|
+
"OPENAI_BASE_URL",
|
|
241
|
+
"OPENAI_ORG_ID",
|
|
242
|
+
"OPENROUTER_API_KEY",
|
|
243
|
+
"OPENROUTER_BASE_URL",
|
|
244
|
+
"GEMINI_API_KEY",
|
|
245
|
+
"GOOGLE_API_KEY",
|
|
246
|
+
"GOOGLE_GEMINI_BASE_URL",
|
|
247
|
+
"GOOGLE_APPLICATION_CREDENTIALS",
|
|
248
|
+
"GOOGLE_CLOUD_PROJECT",
|
|
249
|
+
"GOOGLE_CLOUD_LOCATION",
|
|
250
|
+
"GOOGLE_GENAI_USE_VERTEXAI",
|
|
251
|
+
"GOOGLE_GENAI_USE_GCA",
|
|
252
|
+
"DEEPSEEK_API_KEY",
|
|
253
|
+
"MISTRAL_API_KEY",
|
|
254
|
+
"XAI_API_KEY",
|
|
255
|
+
"GROQ_API_KEY",
|
|
256
|
+
"TOGETHER_API_KEY",
|
|
257
|
+
"FIREWORKS_API_KEY",
|
|
258
|
+
"CEREBRAS_API_KEY",
|
|
259
|
+
"SAMBANOVA_API_KEY",
|
|
260
|
+
"MOONSHOT_API_KEY",
|
|
261
|
+
"DASHSCOPE_API_KEY",
|
|
262
|
+
"ZHIPU_API_KEY",
|
|
263
|
+
"AWS_ACCESS_KEY_ID",
|
|
264
|
+
"AWS_SECRET_ACCESS_KEY",
|
|
265
|
+
"AWS_SESSION_TOKEN",
|
|
266
|
+
"AWS_REGION",
|
|
267
|
+
"AWS_DEFAULT_REGION",
|
|
268
|
+
"AWS_PROFILE",
|
|
269
|
+
"AWS_BEARER_TOKEN_BEDROCK",
|
|
270
|
+
"AZURE_OPENAI_API_KEY",
|
|
271
|
+
"AZURE_OPENAI_ENDPOINT",
|
|
272
|
+
"AZURE_OPENAI_API_VERSION",
|
|
273
|
+
"OLLAMA_HOST",
|
|
274
|
+
"OLLAMA_API_KEY",
|
|
275
|
+
"LMSTUDIO_BASE_URL",
|
|
276
|
+
] as const;
|
|
277
|
+
|
|
278
|
+
const CLINE_CA_FILE_KEYS = ["NODE_EXTRA_CA_CERTS", "SSL_CERT_FILE"] as const;
|
|
279
|
+
const CLINE_CA_DIR_KEYS = ["SSL_CERT_DIR"] as const;
|
|
280
|
+
|
|
281
|
+
export interface ClineCredentialCandidate {
|
|
282
|
+
label: string;
|
|
283
|
+
key: string;
|
|
284
|
+
configDir: string;
|
|
285
|
+
dataDir: string;
|
|
286
|
+
settingsDir: string;
|
|
287
|
+
providerSettingsPath: string;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
export interface ClineCredentialFile {
|
|
291
|
+
relArea: "settings" | "data" | "config";
|
|
292
|
+
name: string;
|
|
293
|
+
sourcePath: string;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
export interface ClineCredentialSource {
|
|
297
|
+
candidate: ClineCredentialCandidate;
|
|
298
|
+
usableFiles: ClineCredentialFile[];
|
|
299
|
+
probes: Array<ClineCredentialCandidate & { usableCount: number }>;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
function readablePath(path: string, kind: "file" | "directory"): boolean {
|
|
303
|
+
try {
|
|
304
|
+
accessSync(path, constants.R_OK);
|
|
305
|
+
const info = statSync(path);
|
|
306
|
+
return kind === "file" ? info.isFile() : info.isDirectory();
|
|
307
|
+
} catch {
|
|
308
|
+
return false;
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
function isUsableAuthFile(path: string): boolean {
|
|
313
|
+
try {
|
|
314
|
+
accessSync(path, constants.R_OK);
|
|
315
|
+
const info = statSync(path);
|
|
316
|
+
return info.isFile() && info.size > 0;
|
|
317
|
+
} catch {
|
|
318
|
+
return false;
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
function candidateFromProviderPath(label: string, key: string, providerPath: string): ClineCredentialCandidate {
|
|
323
|
+
const resolved = resolve(providerPath);
|
|
324
|
+
const settingsDir = dirname(resolved);
|
|
325
|
+
const dataDir = basename(settingsDir) === "settings" ? dirname(settingsDir) : settingsDir;
|
|
326
|
+
const configDir = basename(dataDir) === "data" ? dirname(dataDir) : dataDir;
|
|
327
|
+
return { label, key, configDir, dataDir, settingsDir, providerSettingsPath: resolved };
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
function candidateFromDataDir(label: string, key: string, dataDir: string): ClineCredentialCandidate {
|
|
331
|
+
const resolved = resolve(dataDir);
|
|
332
|
+
const configDir = basename(resolved) === "data" ? dirname(resolved) : resolved;
|
|
333
|
+
const settingsDir = join(resolved, "settings");
|
|
334
|
+
return { label, key, configDir, dataDir: resolved, settingsDir, providerSettingsPath: join(settingsDir, "providers.json") };
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
function candidateFromConfigDir(label: string, key: string, configDir: string): ClineCredentialCandidate {
|
|
338
|
+
const resolved = resolve(configDir);
|
|
339
|
+
const dataDir = join(resolved, "data");
|
|
340
|
+
const settingsDir = join(dataDir, "settings");
|
|
341
|
+
return { label, key, configDir: resolved, dataDir, settingsDir, providerSettingsPath: join(settingsDir, "providers.json") };
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
function collectCredentialFiles(candidate: ClineCredentialCandidate): ClineCredentialFile[] {
|
|
345
|
+
const found: ClineCredentialFile[] = [];
|
|
346
|
+
const seen = new Set<string>();
|
|
347
|
+
const add = (relArea: ClineCredentialFile["relArea"], name: string, sourcePath: string): void => {
|
|
348
|
+
const resolved = resolve(sourcePath);
|
|
349
|
+
if (seen.has(resolved) || !isUsableAuthFile(resolved)) return;
|
|
350
|
+
seen.add(resolved);
|
|
351
|
+
found.push({ relArea, name, sourcePath: resolved });
|
|
352
|
+
};
|
|
353
|
+
add("settings", "providers.json", candidate.providerSettingsPath);
|
|
354
|
+
try {
|
|
355
|
+
for (const entry of readdirSync(candidate.settingsDir, { withFileTypes: true })) {
|
|
356
|
+
if (!entry.name.endsWith(".json") || CLINE_OWNED_SETTINGS_FILES.has(entry.name)) continue;
|
|
357
|
+
if (entry.isFile() || entry.isSymbolicLink()) {
|
|
358
|
+
add("settings", entry.name, join(candidate.settingsDir, entry.name));
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
} catch {
|
|
362
|
+
// Missing or unreadable settings dir.
|
|
363
|
+
}
|
|
364
|
+
for (const name of CLINE_DATA_AUTH_FILES) {
|
|
365
|
+
add("data", name, join(candidate.dataDir, name));
|
|
366
|
+
}
|
|
367
|
+
for (const name of CLINE_CONFIG_AUTH_FILES) {
|
|
368
|
+
add("config", name, join(candidate.configDir, name));
|
|
369
|
+
}
|
|
370
|
+
return found;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/** Probe env-configured and standard Cline homes, preferring the first location with usable credentials. */
|
|
374
|
+
export function resolveClineCredentialSource(
|
|
375
|
+
env: Record<string, string | undefined> = process.env,
|
|
376
|
+
explicitProviderPath?: string,
|
|
377
|
+
): ClineCredentialSource {
|
|
378
|
+
const raw: ClineCredentialCandidate[] = [];
|
|
379
|
+
const explicitArg = nonEmpty(explicitProviderPath);
|
|
380
|
+
if (explicitArg) raw.push(candidateFromProviderPath("explicit provider", "providerSettingsPath", explicitArg));
|
|
381
|
+
const explicitEnv = nonEmpty(env.CLINE_PROVIDER_SETTINGS_PATH);
|
|
382
|
+
if (explicitEnv) raw.push(candidateFromProviderPath("env provider", "CLINE_PROVIDER_SETTINGS_PATH", explicitEnv));
|
|
208
383
|
const dataDir = nonEmpty(env.CLINE_DATA_DIR);
|
|
209
|
-
if (dataDir)
|
|
384
|
+
if (dataDir) raw.push(candidateFromDataDir("env data", "CLINE_DATA_DIR", dataDir));
|
|
210
385
|
const clineDir = nonEmpty(env.CLINE_DIR);
|
|
211
|
-
if (clineDir)
|
|
212
|
-
|
|
386
|
+
if (clineDir) raw.push(candidateFromConfigDir("env dir", "CLINE_DIR", clineDir));
|
|
387
|
+
const home = nonEmpty(env.HOME);
|
|
388
|
+
if (home) raw.push(candidateFromConfigDir("HOME", "HOME", join(home, ".cline")));
|
|
389
|
+
const userProfile = nonEmpty(env.USERPROFILE);
|
|
390
|
+
if (userProfile) raw.push(candidateFromConfigDir("USERPROFILE", "USERPROFILE", join(userProfile, ".cline")));
|
|
391
|
+
if (!home && !userProfile) raw.push(candidateFromConfigDir("homedir", "homedir()", join(homedir(), ".cline")));
|
|
392
|
+
const xdgConfig = nonEmpty(env.XDG_CONFIG_HOME);
|
|
393
|
+
if (xdgConfig) raw.push(candidateFromConfigDir("XDG_CONFIG_HOME", "XDG_CONFIG_HOME", join(xdgConfig, "cline")));
|
|
394
|
+
const appData = nonEmpty(env.APPDATA);
|
|
395
|
+
if (appData) raw.push(candidateFromConfigDir("APPDATA", "APPDATA", join(appData, "cline")));
|
|
396
|
+
const localAppData = nonEmpty(env.LOCALAPPDATA);
|
|
397
|
+
if (localAppData) raw.push(candidateFromConfigDir("LOCALAPPDATA", "LOCALAPPDATA", join(localAppData, "cline")));
|
|
398
|
+
|
|
399
|
+
const unique: ClineCredentialCandidate[] = [];
|
|
400
|
+
const seen = new Set<string>();
|
|
401
|
+
for (const candidate of raw) {
|
|
402
|
+
const key = `${candidate.providerSettingsPath}\u0000${candidate.dataDir}`;
|
|
403
|
+
if (seen.has(key)) continue;
|
|
404
|
+
seen.add(key);
|
|
405
|
+
unique.push(candidate);
|
|
406
|
+
}
|
|
407
|
+
const probed = unique.map(candidate => {
|
|
408
|
+
const files = collectCredentialFiles(candidate);
|
|
409
|
+
return { candidate, files };
|
|
410
|
+
});
|
|
411
|
+
const selected = probed.find(entry => entry.files.length > 0) ?? probed[0]!;
|
|
412
|
+
return {
|
|
413
|
+
candidate: selected.candidate,
|
|
414
|
+
usableFiles: selected.files,
|
|
415
|
+
probes: probed.map(entry => ({ ...entry.candidate, usableCount: entry.files.length })),
|
|
416
|
+
};
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
/** Cline's provider store follows its documented env overrides and authenticated home defaults. */
|
|
420
|
+
export function resolveClineProviderSettingsPath(env: Record<string, string | undefined> = process.env): string {
|
|
421
|
+
return resolveClineCredentialSource(env).candidate.providerSettingsPath;
|
|
213
422
|
}
|
|
214
423
|
|
|
215
424
|
function isLoopbackUrl(value: string): boolean {
|
|
@@ -323,17 +532,30 @@ function legacyClineSafetyState(value: unknown): Record<string, unknown> {
|
|
|
323
532
|
};
|
|
324
533
|
}
|
|
325
534
|
|
|
535
|
+
async function readOptionalFile(paths: readonly string[]): Promise<Buffer | undefined> {
|
|
536
|
+
for (const path of paths) {
|
|
537
|
+
try {
|
|
538
|
+
return await readFile(path);
|
|
539
|
+
} catch (error) {
|
|
540
|
+
if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error;
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
return undefined;
|
|
544
|
+
}
|
|
545
|
+
|
|
326
546
|
async function stageLegacyClineState(
|
|
327
|
-
|
|
547
|
+
candidate: ClineCredentialCandidate,
|
|
328
548
|
dataDir: string,
|
|
329
549
|
): Promise<{ globalStatePath: string; secretsPath?: string }> {
|
|
330
|
-
const settingsDir = dirname(providerSettingsPath);
|
|
331
|
-
const sourceDataDir = basename(settingsDir) === "settings" ? dirname(settingsDir) : undefined;
|
|
332
550
|
const globalStatePath = join(dataDir, "globalState.json");
|
|
551
|
+
const rawState = await readOptionalFile([
|
|
552
|
+
join(candidate.dataDir, "globalState.json"),
|
|
553
|
+
join(candidate.configDir, "globalState.json"),
|
|
554
|
+
]);
|
|
333
555
|
let sourceState: unknown;
|
|
334
|
-
if (
|
|
556
|
+
if (rawState) {
|
|
335
557
|
try {
|
|
336
|
-
sourceState = JSON.parse(
|
|
558
|
+
sourceState = JSON.parse(rawState.toString("utf8"));
|
|
337
559
|
} catch {
|
|
338
560
|
sourceState = undefined;
|
|
339
561
|
}
|
|
@@ -345,14 +567,13 @@ async function stageLegacyClineState(
|
|
|
345
567
|
});
|
|
346
568
|
|
|
347
569
|
let secretsPath: string | undefined;
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
}
|
|
570
|
+
const secretsBytes = await readOptionalFile([
|
|
571
|
+
join(candidate.dataDir, "secrets.json"),
|
|
572
|
+
join(candidate.configDir, "secrets.json"),
|
|
573
|
+
]);
|
|
574
|
+
if (secretsBytes) {
|
|
575
|
+
secretsPath = join(dataDir, "secrets.json");
|
|
576
|
+
await writeFile(secretsPath, secretsBytes, { mode: 0o600, flag: "wx" });
|
|
356
577
|
}
|
|
357
578
|
return { globalStatePath, ...(secretsPath ? { secretsPath } : {}) };
|
|
358
579
|
}
|
|
@@ -383,44 +604,82 @@ function clineMcpSettings(launch: ClineMcpLaunch | undefined, catalogPath: strin
|
|
|
383
604
|
};
|
|
384
605
|
}
|
|
385
606
|
|
|
386
|
-
|
|
387
|
-
async function linkClineFile(target: string, path: string): Promise<void> {
|
|
607
|
+
async function isStagedRegularFile(path: string): Promise<boolean> {
|
|
388
608
|
try {
|
|
389
|
-
await
|
|
609
|
+
return (await stat(path)).isFile();
|
|
390
610
|
} catch {
|
|
391
|
-
|
|
392
|
-
await link(resolve(target), path);
|
|
393
|
-
} catch {
|
|
394
|
-
// Best-effort: the CLINE_* path environment remains the primary contract.
|
|
395
|
-
}
|
|
611
|
+
return false;
|
|
396
612
|
}
|
|
397
613
|
}
|
|
398
614
|
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
615
|
+
export interface StageClineFileDeps {
|
|
616
|
+
symlink?: typeof symlink;
|
|
617
|
+
link?: typeof link;
|
|
618
|
+
copyFile?: typeof copyFile;
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
/** Expose a staged file via symlink, hardlink, or private 0600 copy fallback. */
|
|
622
|
+
export async function stageClineFile(
|
|
623
|
+
sourcePath: string,
|
|
624
|
+
targetPath: string,
|
|
625
|
+
deps: StageClineFileDeps = {},
|
|
626
|
+
): Promise<"symlink" | "hardlink" | "copy" | undefined> {
|
|
627
|
+
if (resolve(sourcePath) === resolve(targetPath)) return "copy";
|
|
628
|
+
const symlinkFn = deps.symlink ?? symlink;
|
|
629
|
+
const linkFn = deps.link ?? link;
|
|
630
|
+
const copyFileFn = deps.copyFile ?? copyFile;
|
|
631
|
+
await rm(targetPath, { recursive: true, force: true }).catch(() => undefined);
|
|
402
632
|
try {
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
633
|
+
await symlinkFn(resolve(sourcePath), targetPath);
|
|
634
|
+
if (await isStagedRegularFile(targetPath)) return "symlink";
|
|
635
|
+
} catch {
|
|
636
|
+
// Fall through to hardlink.
|
|
407
637
|
}
|
|
638
|
+
await rm(targetPath, { recursive: true, force: true }).catch(() => undefined);
|
|
639
|
+
try {
|
|
640
|
+
await linkFn(resolve(sourcePath), targetPath);
|
|
641
|
+
if (await isStagedRegularFile(targetPath)) return "hardlink";
|
|
642
|
+
} catch {
|
|
643
|
+
// Fall through to private copy.
|
|
644
|
+
}
|
|
645
|
+
await rm(targetPath, { recursive: true, force: true }).catch(() => undefined);
|
|
646
|
+
try {
|
|
647
|
+
await copyFileFn(sourcePath, targetPath, constants.COPYFILE_EXCL);
|
|
648
|
+
await chmod(targetPath, 0o600).catch(() => undefined);
|
|
649
|
+
if (await isStagedRegularFile(targetPath)) return "copy";
|
|
650
|
+
} catch {
|
|
651
|
+
// Best-effort: the CLINE_* path environment remains the primary contract.
|
|
652
|
+
}
|
|
653
|
+
await rm(targetPath, { recursive: true, force: true }).catch(() => undefined);
|
|
654
|
+
return undefined;
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
/** Copy the auth source into the private turn root so vendor refreshes cannot mutate it. */
|
|
658
|
+
async function copyClineProviderSettings(sources: readonly string[], target: string): Promise<boolean> {
|
|
659
|
+
const bytes = await readOptionalFile(sources);
|
|
660
|
+
if (!bytes) return false;
|
|
408
661
|
await writeFile(target, bytes, { mode: 0o600, flag: "wx" });
|
|
409
662
|
return true;
|
|
410
663
|
}
|
|
411
664
|
|
|
412
665
|
/**
|
|
413
666
|
* A fork that ignores CLINE_PROVIDER_SETTINGS_PATH still looks at ~/.cline or
|
|
414
|
-
* CLINE_DIR/data/settings.
|
|
667
|
+
* CLINE_DIR/data/settings. Stage the private copies and settings there.
|
|
415
668
|
*/
|
|
416
669
|
async function linkClineSettings(input: {
|
|
417
670
|
dataDir: string;
|
|
418
671
|
configDir: string;
|
|
419
672
|
homeDir: string;
|
|
420
673
|
providerSettingsPath?: string;
|
|
674
|
+
legacyGlobalStatePath?: string;
|
|
675
|
+
legacySecretsPath?: string;
|
|
421
676
|
globalSettingsPath: string;
|
|
422
677
|
mcpSettingsPath: string;
|
|
423
|
-
}): Promise<
|
|
678
|
+
}): Promise<string | undefined> {
|
|
679
|
+
let method: "symlink" | "hardlink" | "copy" | undefined;
|
|
680
|
+
const record = (used: typeof method): void => {
|
|
681
|
+
method ??= used;
|
|
682
|
+
};
|
|
424
683
|
const targets = [
|
|
425
684
|
{ dir: join(input.dataDir, "settings"), settings: false },
|
|
426
685
|
{ dir: join(input.configDir, "data", "settings"), settings: true },
|
|
@@ -428,22 +687,95 @@ async function linkClineSettings(input: {
|
|
|
428
687
|
];
|
|
429
688
|
for (const target of targets) {
|
|
430
689
|
if (input.providerSettingsPath) {
|
|
431
|
-
await
|
|
690
|
+
record(await stageClineFile(input.providerSettingsPath, join(target.dir, "providers.json")));
|
|
432
691
|
}
|
|
433
692
|
if (!target.settings) continue;
|
|
434
|
-
await
|
|
435
|
-
await
|
|
693
|
+
await stageClineFile(input.globalSettingsPath, join(target.dir, "global-settings.json"));
|
|
694
|
+
await stageClineFile(input.mcpSettingsPath, join(target.dir, "cline_mcp_settings.json"));
|
|
695
|
+
}
|
|
696
|
+
const legacyDirs = [input.dataDir, join(input.homeDir, ".cline", "data")];
|
|
697
|
+
for (const dir of legacyDirs) {
|
|
698
|
+
if (input.legacyGlobalStatePath) {
|
|
699
|
+
record(await stageClineFile(input.legacyGlobalStatePath, join(dir, "globalState.json")));
|
|
700
|
+
}
|
|
701
|
+
if (input.legacySecretsPath) {
|
|
702
|
+
record(await stageClineFile(input.legacySecretsPath, join(dir, "secrets.json")));
|
|
703
|
+
}
|
|
436
704
|
}
|
|
705
|
+
return method;
|
|
706
|
+
}
|
|
707
|
+
|
|
708
|
+
async function stageExtraCredentialFiles(
|
|
709
|
+
source: ClineCredentialSource,
|
|
710
|
+
dirs: { configDir: string; dataDir: string; homeDir: string },
|
|
711
|
+
skip: ReadonlySet<string>,
|
|
712
|
+
perTurnPaths: string[],
|
|
713
|
+
): Promise<string[]> {
|
|
714
|
+
const stagedNames: string[] = [];
|
|
715
|
+
for (const file of source.usableFiles) {
|
|
716
|
+
if (skip.has(file.sourcePath)) continue;
|
|
717
|
+
if (file.relArea === "settings" && file.name === "providers.json") continue;
|
|
718
|
+
if ((file.relArea === "data" || file.relArea === "config") && (file.name === "globalState.json" || file.name === "secrets.json")) {
|
|
719
|
+
continue;
|
|
720
|
+
}
|
|
721
|
+
const bytes = await readOptionalFile([file.sourcePath]);
|
|
722
|
+
if (!bytes) continue;
|
|
723
|
+
if (file.relArea === "settings") {
|
|
724
|
+
const privateCopy = join(dirs.configDir, "data", "settings", file.name);
|
|
725
|
+
await writeFile(privateCopy, bytes, { mode: 0o600, flag: "wx" });
|
|
726
|
+
await stageClineFile(privateCopy, join(dirs.dataDir, "settings", file.name));
|
|
727
|
+
await stageClineFile(privateCopy, join(dirs.homeDir, ".cline", "data", "settings", file.name));
|
|
728
|
+
stagedNames.push(`settings/${file.name}`);
|
|
729
|
+
continue;
|
|
730
|
+
}
|
|
731
|
+
if (file.relArea === "data") {
|
|
732
|
+
const privateCopy = join(dirs.configDir, "data", file.name);
|
|
733
|
+
await writeFile(privateCopy, bytes, { mode: 0o600, flag: "wx" });
|
|
734
|
+
const dataTarget = join(dirs.dataDir, file.name);
|
|
735
|
+
perTurnPaths.push(dataTarget);
|
|
736
|
+
await stageClineFile(privateCopy, dataTarget);
|
|
737
|
+
await stageClineFile(privateCopy, join(dirs.homeDir, ".cline", "data", file.name));
|
|
738
|
+
stagedNames.push(`data/${file.name}`);
|
|
739
|
+
continue;
|
|
740
|
+
}
|
|
741
|
+
const privateCopy = join(dirs.configDir, file.name);
|
|
742
|
+
await writeFile(privateCopy, bytes, { mode: 0o600, flag: "wx" });
|
|
743
|
+
await stageClineFile(privateCopy, join(dirs.homeDir, ".cline", file.name));
|
|
744
|
+
if (file.name !== "providers.json") {
|
|
745
|
+
const dataTarget = join(dirs.dataDir, file.name);
|
|
746
|
+
perTurnPaths.push(dataTarget);
|
|
747
|
+
await stageClineFile(privateCopy, dataTarget);
|
|
748
|
+
}
|
|
749
|
+
stagedNames.push(file.name);
|
|
750
|
+
}
|
|
751
|
+
return stagedNames;
|
|
752
|
+
}
|
|
753
|
+
|
|
754
|
+
function clineCredentialDiagnostic(
|
|
755
|
+
source: ClineCredentialSource,
|
|
756
|
+
stagedItems: readonly string[],
|
|
757
|
+
method: string | undefined,
|
|
758
|
+
): string {
|
|
759
|
+
const probed = source.probes
|
|
760
|
+
.map(probe => `${probe.label} ${probe.configDir} (${probe.key}; ${probe.usableCount > 0 ? `${probe.usableCount} file(s)` : "empty"})`)
|
|
761
|
+
.join("; ");
|
|
762
|
+
const staged = stagedItems.length > 0
|
|
763
|
+
? `staged ${stagedItems.join(", ")}${method ? ` (${method})` : ""}`
|
|
764
|
+
: "no credential files staged";
|
|
765
|
+
return `Cline credentials: using ${source.candidate.label} ${source.candidate.configDir} (${source.candidate.key}); ${staged}; probed ${probed}`;
|
|
437
766
|
}
|
|
438
767
|
|
|
439
768
|
/** Stage all Cline paths the proxy controls; the vendor provider store remains the auth source. */
|
|
440
769
|
export async function stageClineTurn(options: {
|
|
441
770
|
providerSettingsPath: string;
|
|
771
|
+
sourceEnv?: NodeJS.ProcessEnv;
|
|
442
772
|
catalog: readonly Record<string, unknown>[];
|
|
443
773
|
mcpLaunch?: ClineMcpLaunch;
|
|
444
774
|
/** Persistent conversation root. When set, per-turn state is recreated and vendor data survives. */
|
|
445
775
|
root?: string;
|
|
446
776
|
}): Promise<ClineStagedTurn> {
|
|
777
|
+
const credentialSource = resolveClineCredentialSource(options.sourceEnv ?? process.env, options.providerSettingsPath);
|
|
778
|
+
const sourceCandidate = credentialSource.candidate;
|
|
447
779
|
const persistent = Boolean(options.root);
|
|
448
780
|
const root = options.root ?? await mkdtemp(join(tmpdir(), "ocx-cline-cli-"));
|
|
449
781
|
const configDir = join(root, "config");
|
|
@@ -457,10 +789,18 @@ export async function stageClineTurn(options: {
|
|
|
457
789
|
const providerSettingsPath = join(homeDir, ".cline", "data", "settings", "providers.json");
|
|
458
790
|
const catalogPath = join(root, "tool-catalog.json");
|
|
459
791
|
|
|
460
|
-
const recreatedPaths = [
|
|
792
|
+
const recreatedPaths = [
|
|
793
|
+
configDir,
|
|
794
|
+
homeDir,
|
|
795
|
+
hooksDir,
|
|
796
|
+
logDir,
|
|
797
|
+
join(dataDir, "settings"),
|
|
798
|
+
...CLINE_DATA_AUTH_FILES.map(name => join(dataDir, name)),
|
|
799
|
+
catalogPath,
|
|
800
|
+
];
|
|
461
801
|
const perTurnPaths = [...recreatedPaths, join(root, "prompt.txt")];
|
|
462
802
|
const cleanup = async (): Promise<void> => {
|
|
463
|
-
const targets = persistent ? perTurnPaths : [root];
|
|
803
|
+
const targets = persistent ? [...new Set(perTurnPaths)] : [root];
|
|
464
804
|
await Promise.all(targets.map(path => rm(path, { recursive: true, force: true }).catch(() => undefined)));
|
|
465
805
|
};
|
|
466
806
|
|
|
@@ -486,16 +826,41 @@ export async function stageClineTurn(options: {
|
|
|
486
826
|
writeFile(mcpSettingsPath, `${JSON.stringify(clineMcpSettings(options.mcpLaunch, catalogPath), null, 2)}\n`, { encoding: "utf8", mode: 0o600, flag: "wx" }),
|
|
487
827
|
writeFile(catalogPath, JSON.stringify(options.catalog), { encoding: "utf8", mode: 0o600, flag: "wx" }),
|
|
488
828
|
]);
|
|
489
|
-
const legacyState = await stageLegacyClineState(
|
|
490
|
-
const
|
|
491
|
-
|
|
829
|
+
const legacyState = await stageLegacyClineState(sourceCandidate, join(configDir, "data"));
|
|
830
|
+
const providerSources = [
|
|
831
|
+
sourceCandidate.providerSettingsPath,
|
|
832
|
+
join(sourceCandidate.configDir, "providers.json"),
|
|
833
|
+
];
|
|
834
|
+
const copiedProviderStore = await copyClineProviderSettings(providerSources, providerSettingsPath);
|
|
835
|
+
const linkMethod = await linkClineSettings({
|
|
492
836
|
dataDir,
|
|
493
837
|
configDir,
|
|
494
838
|
homeDir,
|
|
495
839
|
...(copiedProviderStore ? { providerSettingsPath } : {}),
|
|
840
|
+
legacyGlobalStatePath: legacyState.globalStatePath,
|
|
841
|
+
...(legacyState.secretsPath ? { legacySecretsPath: legacyState.secretsPath } : {}),
|
|
496
842
|
globalSettingsPath,
|
|
497
843
|
mcpSettingsPath,
|
|
498
844
|
});
|
|
845
|
+
const handledPaths = new Set([
|
|
846
|
+
...providerSources.map(path => resolve(path)),
|
|
847
|
+
resolve(join(sourceCandidate.dataDir, "globalState.json")),
|
|
848
|
+
resolve(join(sourceCandidate.configDir, "globalState.json")),
|
|
849
|
+
resolve(join(sourceCandidate.dataDir, "secrets.json")),
|
|
850
|
+
resolve(join(sourceCandidate.configDir, "secrets.json")),
|
|
851
|
+
]);
|
|
852
|
+
const extraStaged = await stageExtraCredentialFiles(
|
|
853
|
+
credentialSource,
|
|
854
|
+
{ configDir, dataDir, homeDir },
|
|
855
|
+
handledPaths,
|
|
856
|
+
perTurnPaths,
|
|
857
|
+
);
|
|
858
|
+
const stagedItems = [
|
|
859
|
+
...(copiedProviderStore ? ["providers.json"] : []),
|
|
860
|
+
...(legacyState.secretsPath ? ["secrets.json"] : []),
|
|
861
|
+
"globalState.json",
|
|
862
|
+
...extraStaged,
|
|
863
|
+
];
|
|
499
864
|
return {
|
|
500
865
|
root,
|
|
501
866
|
configDir,
|
|
@@ -504,11 +869,13 @@ export async function stageClineTurn(options: {
|
|
|
504
869
|
workspaceDir,
|
|
505
870
|
hooksDir,
|
|
506
871
|
providerSettingsPath,
|
|
872
|
+
hasProviderSettings: copiedProviderStore,
|
|
507
873
|
legacyGlobalStatePath: legacyState.globalStatePath,
|
|
508
874
|
...(legacyState.secretsPath ? { legacySecretsPath: legacyState.secretsPath } : {}),
|
|
509
875
|
globalSettingsPath,
|
|
510
876
|
mcpSettingsPath,
|
|
511
877
|
catalogPath,
|
|
878
|
+
diagnostics: [clineCredentialDiagnostic(credentialSource, stagedItems, linkMethod)],
|
|
512
879
|
cleanup,
|
|
513
880
|
};
|
|
514
881
|
} catch (error) {
|
|
@@ -552,11 +919,33 @@ export async function mergeClineDisabledTools(
|
|
|
552
919
|
}
|
|
553
920
|
|
|
554
921
|
export function buildClineChildEnv(staged: ClineStagedTurn, sourceEnv: NodeJS.ProcessEnv = process.env): Record<string, string> {
|
|
555
|
-
const env = baseScopedEnv();
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
922
|
+
const env = baseScopedEnv(sourceEnv);
|
|
923
|
+
const realHome = nonEmpty(sourceEnv.HOME) ?? nonEmpty(sourceEnv.USERPROFILE) ?? homedir();
|
|
924
|
+
if (realHome) {
|
|
925
|
+
env.HOME = realHome;
|
|
926
|
+
env.USERPROFILE = realHome;
|
|
927
|
+
}
|
|
928
|
+
for (const key of [
|
|
929
|
+
"XDG_CONFIG_HOME",
|
|
930
|
+
"XDG_CACHE_HOME",
|
|
931
|
+
"XDG_DATA_HOME",
|
|
932
|
+
"XDG_RUNTIME_DIR",
|
|
933
|
+
"DBUS_SESSION_BUS_ADDRESS",
|
|
934
|
+
"GNOME_KEYRING_CONTROL",
|
|
935
|
+
...CLINE_FORWARDED_PROXY_KEYS,
|
|
936
|
+
...CLINE_FORWARDED_AUTH_KEYS,
|
|
937
|
+
]) {
|
|
938
|
+
const value = nonEmpty(sourceEnv[key]);
|
|
939
|
+
if (value) env[key] = value;
|
|
940
|
+
}
|
|
941
|
+
for (const key of CLINE_CA_FILE_KEYS) {
|
|
942
|
+
const value = nonEmpty(sourceEnv[key]);
|
|
943
|
+
if (value && readablePath(value, "file")) env[key] = value;
|
|
944
|
+
}
|
|
945
|
+
for (const key of CLINE_CA_DIR_KEYS) {
|
|
946
|
+
const value = nonEmpty(sourceEnv[key]);
|
|
947
|
+
if (value && readablePath(value, "directory")) env[key] = value;
|
|
948
|
+
}
|
|
560
949
|
const user = sourceEnv.USER ?? sourceEnv.LOGNAME;
|
|
561
950
|
if (user) env.USER = user;
|
|
562
951
|
if (sourceEnv.LOGNAME) env.LOGNAME = sourceEnv.LOGNAME;
|
|
@@ -567,7 +956,7 @@ export function buildClineChildEnv(staged: ClineStagedTurn, sourceEnv: NodeJS.Pr
|
|
|
567
956
|
CLINE_SESSION_DATA_DIR: join(staged.dataDir, "sessions"),
|
|
568
957
|
CLINE_TEAM_DATA_DIR: join(staged.dataDir, "teams"),
|
|
569
958
|
CLINE_SANDBOX_DATA_DIR: staged.dataDir,
|
|
570
|
-
CLINE_PROVIDER_SETTINGS_PATH: staged.providerSettingsPath,
|
|
959
|
+
...(staged.hasProviderSettings !== false ? { CLINE_PROVIDER_SETTINGS_PATH: staged.providerSettingsPath } : {}),
|
|
571
960
|
CLINE_GLOBAL_SETTINGS_PATH: staged.globalSettingsPath,
|
|
572
961
|
CLINE_HOOKS_DIR: staged.hooksDir,
|
|
573
962
|
CLINE_MCP_SETTINGS_PATH: staged.mcpSettingsPath,
|
|
@@ -79,7 +79,7 @@ function safeMessage(value: unknown): string {
|
|
|
79
79
|
}
|
|
80
80
|
|
|
81
81
|
function classifyError(message: string): { status: number; errorType: string; code: string; retryable: boolean } {
|
|
82
|
-
if (/not logged in|please (?:run )?\/?login|sign in|authentication (?:failed|required)|unauthorized|invalid (?:api )?key/i.test(message)) {
|
|
82
|
+
if (/not logged in|please (?:run )?\/?login|sign in|authentication (?:failed|required)|unauthorized|invalid (?:api )?key|interactive auth|auth setup/i.test(message)) {
|
|
83
83
|
return { status: 401, errorType: "authentication_error", code: "cline_cli_not_logged_in", retryable: false };
|
|
84
84
|
}
|
|
85
85
|
if (/rate limit|too many requests|quota|credit|usage limit|daily limit/i.test(message)) {
|