@seaveyon/harness-switch 0.14.0 → 0.15.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/harness-switch.js
CHANGED
|
@@ -7015,7 +7015,7 @@ var require_public_api = __commonJS((exports) => {
|
|
|
7015
7015
|
|
|
7016
7016
|
// src/app.ts
|
|
7017
7017
|
import { existsSync as existsSync4 } from "fs";
|
|
7018
|
-
import { join as
|
|
7018
|
+
import { join as join12 } from "path";
|
|
7019
7019
|
|
|
7020
7020
|
// ../../node_modules/hono/dist/compose.js
|
|
7021
7021
|
var compose = (middleware, onError, onNotFound) => {
|
|
@@ -9337,6 +9337,12 @@ class FileService {
|
|
|
9337
9337
|
writeUserFile(file, text) {
|
|
9338
9338
|
this.write(file, text, this.modeOf(file));
|
|
9339
9339
|
}
|
|
9340
|
+
writeUserSecretFile(file, text) {
|
|
9341
|
+
this.write(file, text, 384, {
|
|
9342
|
+
uid: this.environment.currentUser.uid,
|
|
9343
|
+
gid: this.environment.currentUser.gid
|
|
9344
|
+
});
|
|
9345
|
+
}
|
|
9340
9346
|
readJson(file, fallback) {
|
|
9341
9347
|
try {
|
|
9342
9348
|
return JSON.parse(this.readText(file));
|
|
@@ -9376,10 +9382,9 @@ class FileService {
|
|
|
9376
9382
|
return [];
|
|
9377
9383
|
}
|
|
9378
9384
|
}
|
|
9379
|
-
write(file, text, mode) {
|
|
9385
|
+
write(file, text, mode, owner = this.ownerOf(file)) {
|
|
9380
9386
|
this.ensureDir(dirname2(file));
|
|
9381
9387
|
const tmp = `${file}.${process.pid}.${randomBytes(4).toString("hex")}.tmp`;
|
|
9382
|
-
const owner = this.ownerOf(file);
|
|
9383
9388
|
writeFileSync(tmp, text, { encoding: "utf8", mode });
|
|
9384
9389
|
this.applyOwner(tmp, owner.uid, owner.gid);
|
|
9385
9390
|
renameSync(tmp, file);
|
|
@@ -11212,33 +11217,51 @@ var SETTINGS = "settings";
|
|
|
11212
11217
|
var BASE_URL_VAR = "ANTHROPIC_BASE_URL";
|
|
11213
11218
|
var MODEL_VAR = "ANTHROPIC_MODEL";
|
|
11214
11219
|
var AUTH_VARS = ["ANTHROPIC_AUTH_TOKEN", "ANTHROPIC_API_KEY"];
|
|
11220
|
+
var ONE_M_SUFFIX = "[1m]";
|
|
11215
11221
|
var MODEL_MAPPINGS = [
|
|
11216
|
-
|
|
11217
|
-
"haikuModel",
|
|
11218
|
-
"ANTHROPIC_DEFAULT_HAIKU_MODEL",
|
|
11219
|
-
"haikuModelName",
|
|
11220
|
-
"ANTHROPIC_DEFAULT_HAIKU_MODEL_NAME"
|
|
11221
|
-
|
|
11222
|
-
|
|
11223
|
-
"sonnetModel",
|
|
11224
|
-
"ANTHROPIC_DEFAULT_SONNET_MODEL",
|
|
11225
|
-
"sonnetModelName",
|
|
11226
|
-
"ANTHROPIC_DEFAULT_SONNET_MODEL_NAME"
|
|
11227
|
-
|
|
11228
|
-
|
|
11229
|
-
|
|
11230
|
-
"
|
|
11231
|
-
"
|
|
11232
|
-
"
|
|
11233
|
-
|
|
11234
|
-
|
|
11235
|
-
|
|
11236
|
-
|
|
11237
|
-
"
|
|
11238
|
-
"
|
|
11239
|
-
|
|
11240
|
-
|
|
11222
|
+
{
|
|
11223
|
+
field: "haikuModel",
|
|
11224
|
+
envVar: "ANTHROPIC_DEFAULT_HAIKU_MODEL",
|
|
11225
|
+
nameField: "haikuModelName",
|
|
11226
|
+
nameEnvVar: "ANTHROPIC_DEFAULT_HAIKU_MODEL_NAME"
|
|
11227
|
+
},
|
|
11228
|
+
{
|
|
11229
|
+
field: "sonnetModel",
|
|
11230
|
+
envVar: "ANTHROPIC_DEFAULT_SONNET_MODEL",
|
|
11231
|
+
nameField: "sonnetModelName",
|
|
11232
|
+
nameEnvVar: "ANTHROPIC_DEFAULT_SONNET_MODEL_NAME",
|
|
11233
|
+
oneMField: "sonnetModel1m"
|
|
11234
|
+
},
|
|
11235
|
+
{
|
|
11236
|
+
field: "opusModel",
|
|
11237
|
+
envVar: "ANTHROPIC_DEFAULT_OPUS_MODEL",
|
|
11238
|
+
nameField: "opusModelName",
|
|
11239
|
+
nameEnvVar: "ANTHROPIC_DEFAULT_OPUS_MODEL_NAME",
|
|
11240
|
+
oneMField: "opusModel1m"
|
|
11241
|
+
},
|
|
11242
|
+
{
|
|
11243
|
+
field: "fableModel",
|
|
11244
|
+
envVar: "ANTHROPIC_DEFAULT_FABLE_MODEL",
|
|
11245
|
+
nameField: "fableModelName",
|
|
11246
|
+
nameEnvVar: "ANTHROPIC_DEFAULT_FABLE_MODEL_NAME",
|
|
11247
|
+
oneMField: "fableModel1m"
|
|
11248
|
+
},
|
|
11249
|
+
{ field: "subagentModel", envVar: "CLAUDE_CODE_SUBAGENT_MODEL", oneMField: "subagentModel1m" }
|
|
11241
11250
|
];
|
|
11251
|
+
var ONE_M_OPTIONS = [
|
|
11252
|
+
{ value: "false", label: "\u5173\u95ED" },
|
|
11253
|
+
{ value: "true", label: "\u5F00\u542F" }
|
|
11254
|
+
];
|
|
11255
|
+
function oneMField(role, key) {
|
|
11256
|
+
return {
|
|
11257
|
+
key,
|
|
11258
|
+
label: `${role} \u58F0\u660E\u652F\u6301 1M`,
|
|
11259
|
+
kind: "select",
|
|
11260
|
+
defaultValue: "false",
|
|
11261
|
+
options: ONE_M_OPTIONS,
|
|
11262
|
+
help: "\u5728\u6A21\u578B ID \u672B\u5C3E\u8FFD\u52A0 [1m]\uFF0C\u5411 Claude Code \u58F0\u660E 1M \u4E0A\u4E0B\u6587\uFF1BClaude Code \u8BF7\u6C42\u4E0A\u6E38\u524D\u4F1A\u53BB\u6389\u8BE5\u540E\u7F00\u3002\u4EC5\u5728\u8BE5\u6A21\u578B\u786E\u5B9E\u652F\u6301 1M \u65F6\u5F00\u542F\u3002"
|
|
11263
|
+
};
|
|
11264
|
+
}
|
|
11242
11265
|
|
|
11243
11266
|
class ClaudeAdapter {
|
|
11244
11267
|
environment;
|
|
@@ -11248,7 +11271,7 @@ class ClaudeAdapter {
|
|
|
11248
11271
|
BASE_URL_VAR,
|
|
11249
11272
|
...AUTH_VARS,
|
|
11250
11273
|
MODEL_VAR,
|
|
11251
|
-
...MODEL_MAPPINGS.flatMap((
|
|
11274
|
+
...MODEL_MAPPINGS.flatMap(({ envVar, nameEnvVar }) => nameEnvVar ? [envVar, nameEnvVar] : [envVar])
|
|
11252
11275
|
];
|
|
11253
11276
|
fields = [
|
|
11254
11277
|
{
|
|
@@ -11293,6 +11316,7 @@ class ClaudeAdapter {
|
|
|
11293
11316
|
placeholder: "\u7559\u7A7A\u5219\u4F7F\u7528 Sonnet \u6A21\u578B ID",
|
|
11294
11317
|
help: "\u5199\u5165 ANTHROPIC_DEFAULT_SONNET_MODEL_NAME\uFF1B\u7559\u7A7A\u65F6 Claude Code \u9ED8\u8BA4\u663E\u793A\u5BF9\u5E94\u6A21\u578B ID\u3002"
|
|
11295
11318
|
},
|
|
11319
|
+
oneMField("Sonnet", "sonnetModel1m"),
|
|
11296
11320
|
{
|
|
11297
11321
|
key: "opusModel",
|
|
11298
11322
|
label: "Opus \u6A21\u578B\u6620\u5C04",
|
|
@@ -11308,6 +11332,7 @@ class ClaudeAdapter {
|
|
|
11308
11332
|
placeholder: "\u7559\u7A7A\u5219\u4F7F\u7528 Opus \u6A21\u578B ID",
|
|
11309
11333
|
help: "\u5199\u5165 ANTHROPIC_DEFAULT_OPUS_MODEL_NAME\uFF1B\u7559\u7A7A\u65F6 Claude Code \u9ED8\u8BA4\u663E\u793A\u5BF9\u5E94\u6A21\u578B ID\u3002"
|
|
11310
11334
|
},
|
|
11335
|
+
oneMField("Opus", "opusModel1m"),
|
|
11311
11336
|
{
|
|
11312
11337
|
key: "fableModel",
|
|
11313
11338
|
label: "Fable \u6A21\u578B\u6620\u5C04\uFF08\u53EF\u9009\uFF09",
|
|
@@ -11322,6 +11347,7 @@ class ClaudeAdapter {
|
|
|
11322
11347
|
placeholder: "\u7559\u7A7A\u5219\u4F7F\u7528 Fable \u6A21\u578B ID",
|
|
11323
11348
|
help: "\u5199\u5165 ANTHROPIC_DEFAULT_FABLE_MODEL_NAME\uFF1B\u7559\u7A7A\u65F6 Claude Code \u9ED8\u8BA4\u663E\u793A\u5BF9\u5E94\u6A21\u578B ID\u3002"
|
|
11324
11349
|
},
|
|
11350
|
+
oneMField("Fable", "fableModel1m"),
|
|
11325
11351
|
{
|
|
11326
11352
|
key: "subagentModel",
|
|
11327
11353
|
label: "\u5B50\u4EE3\u7406\u6A21\u578B\uFF08\u53EF\u9009\uFF09",
|
|
@@ -11329,6 +11355,7 @@ class ClaudeAdapter {
|
|
|
11329
11355
|
placeholder: "\u4F8B\u5982\uFF1Aglm-5-air",
|
|
11330
11356
|
help: "\u5199\u5165 CLAUDE_CODE_SUBAGENT_MODEL\uFF0C\u53EF\u8BA9\u5B50\u4EE3\u7406\u4F7F\u7528\u66F4\u5FEB\u6216\u6210\u672C\u66F4\u4F4E\u7684\u6A21\u578B\u3002"
|
|
11331
11357
|
},
|
|
11358
|
+
oneMField("\u5B50\u4EE3\u7406", "subagentModel1m"),
|
|
11332
11359
|
{
|
|
11333
11360
|
key: "extraEnv",
|
|
11334
11361
|
label: "\u8FFD\u52A0\u73AF\u5883\u53D8\u91CF\uFF08\u53EF\u9009\uFF09",
|
|
@@ -11357,9 +11384,9 @@ API_TIMEOUT_MS=3000000`,
|
|
|
11357
11384
|
if (profile.model) {
|
|
11358
11385
|
vars[MODEL_VAR] = profile.model;
|
|
11359
11386
|
}
|
|
11360
|
-
for (const
|
|
11387
|
+
for (const { field, envVar, nameField, nameEnvVar, oneMField: flagField } of MODEL_MAPPINGS) {
|
|
11361
11388
|
if (profile.extras[field]) {
|
|
11362
|
-
vars[envVar] = profile.extras[field];
|
|
11389
|
+
vars[envVar] = withOneM(profile.extras[field], profile.extras[flagField ?? ""]);
|
|
11363
11390
|
}
|
|
11364
11391
|
if (nameField && nameEnvVar && profile.extras[nameField]) {
|
|
11365
11392
|
vars[nameEnvVar] = profile.extras[nameField];
|
|
@@ -11383,10 +11410,10 @@ API_TIMEOUT_MS=3000000`,
|
|
|
11383
11410
|
} else {
|
|
11384
11411
|
delete env[MODEL_VAR];
|
|
11385
11412
|
}
|
|
11386
|
-
for (const
|
|
11413
|
+
for (const { field, envVar, nameField, nameEnvVar, oneMField: flagField } of MODEL_MAPPINGS) {
|
|
11387
11414
|
const value = profile.extras[field]?.trim();
|
|
11388
11415
|
if (value) {
|
|
11389
|
-
env[envVar] = value;
|
|
11416
|
+
env[envVar] = withOneM(value, profile.extras[flagField ?? ""]);
|
|
11390
11417
|
} else {
|
|
11391
11418
|
delete env[envVar];
|
|
11392
11419
|
}
|
|
@@ -11412,7 +11439,7 @@ API_TIMEOUT_MS=3000000`,
|
|
|
11412
11439
|
for (const authVar of AUTH_VARS) {
|
|
11413
11440
|
delete env[authVar];
|
|
11414
11441
|
}
|
|
11415
|
-
for (const
|
|
11442
|
+
for (const { envVar, nameEnvVar } of MODEL_MAPPINGS) {
|
|
11416
11443
|
delete env[envVar];
|
|
11417
11444
|
if (nameEnvVar)
|
|
11418
11445
|
delete env[nameEnvVar];
|
|
@@ -11436,10 +11463,14 @@ API_TIMEOUT_MS=3000000`,
|
|
|
11436
11463
|
apiKey: apiKey || profile.apiKey,
|
|
11437
11464
|
extras: {
|
|
11438
11465
|
...profile.extras,
|
|
11439
|
-
...Object.fromEntries(MODEL_MAPPINGS.flatMap((
|
|
11440
|
-
|
|
11441
|
-
|
|
11442
|
-
|
|
11466
|
+
...Object.fromEntries(MODEL_MAPPINGS.flatMap(({ field, envVar, nameField, nameEnvVar, oneMField: flagField }) => {
|
|
11467
|
+
const value = readString(env, envVar);
|
|
11468
|
+
return [
|
|
11469
|
+
[field, withoutOneM(value)],
|
|
11470
|
+
...nameField && nameEnvVar ? [[nameField, readString(env, nameEnvVar)]] : [],
|
|
11471
|
+
...flagField ? [[flagField, value.endsWith(ONE_M_SUFFIX) ? "true" : "false"]] : []
|
|
11472
|
+
];
|
|
11473
|
+
}))
|
|
11443
11474
|
}
|
|
11444
11475
|
};
|
|
11445
11476
|
}
|
|
@@ -11448,6 +11479,13 @@ API_TIMEOUT_MS=3000000`,
|
|
|
11448
11479
|
return AUTH_VARS.includes(configured) ? configured : AUTH_VARS[0];
|
|
11449
11480
|
}
|
|
11450
11481
|
}
|
|
11482
|
+
function withOneM(value, enabled) {
|
|
11483
|
+
const model = withoutOneM(value.trim());
|
|
11484
|
+
return enabled === "true" && model ? `${model}${ONE_M_SUFFIX}` : model;
|
|
11485
|
+
}
|
|
11486
|
+
function withoutOneM(value) {
|
|
11487
|
+
return value.endsWith(ONE_M_SUFFIX) ? value.slice(0, -ONE_M_SUFFIX.length) : value;
|
|
11488
|
+
}
|
|
11451
11489
|
function safeParse(text) {
|
|
11452
11490
|
try {
|
|
11453
11491
|
return parseJsonObject(text);
|
|
@@ -12231,8 +12269,13 @@ class LiveWriteService {
|
|
|
12231
12269
|
this.log = log;
|
|
12232
12270
|
}
|
|
12233
12271
|
apply(harness, profile, writes) {
|
|
12234
|
-
|
|
12272
|
+
this.transaction(harness, profile, writes, () => {
|
|
12235
12273
|
return;
|
|
12274
|
+
});
|
|
12275
|
+
}
|
|
12276
|
+
transaction(harness, profile, writes, operation) {
|
|
12277
|
+
if (writes.length === 0) {
|
|
12278
|
+
return operation();
|
|
12236
12279
|
}
|
|
12237
12280
|
for (const write2 of writes) {
|
|
12238
12281
|
assertParsable(write2.format, write2.path, write2.content);
|
|
@@ -12245,22 +12288,34 @@ class LiveWriteService {
|
|
|
12245
12288
|
const written = [];
|
|
12246
12289
|
try {
|
|
12247
12290
|
for (const [index, write2] of writes.entries()) {
|
|
12248
|
-
this.
|
|
12249
|
-
written.push(snapshots[index]);
|
|
12291
|
+
this.write(write2);
|
|
12292
|
+
written.push({ snapshot: snapshots[index], secret: write2.secret === true });
|
|
12250
12293
|
}
|
|
12294
|
+
return operation();
|
|
12251
12295
|
} catch (error) {
|
|
12252
12296
|
this.rollback(written);
|
|
12253
12297
|
throw error;
|
|
12254
12298
|
}
|
|
12255
12299
|
}
|
|
12300
|
+
write(write2) {
|
|
12301
|
+
if (write2.secret) {
|
|
12302
|
+
this.files.writeUserSecretFile(write2.path, write2.content);
|
|
12303
|
+
return;
|
|
12304
|
+
}
|
|
12305
|
+
this.files.writeUserFile(write2.path, write2.content);
|
|
12306
|
+
}
|
|
12256
12307
|
rollback(written) {
|
|
12257
|
-
for (const snapshot of written.toReversed()) {
|
|
12308
|
+
for (const { snapshot, secret } of written.toReversed()) {
|
|
12258
12309
|
try {
|
|
12259
12310
|
if (snapshot.content === undefined) {
|
|
12260
12311
|
this.files.remove(snapshot.path);
|
|
12261
12312
|
continue;
|
|
12262
12313
|
}
|
|
12263
|
-
|
|
12314
|
+
if (secret) {
|
|
12315
|
+
this.files.writeUserSecretFile(snapshot.path, snapshot.content);
|
|
12316
|
+
} else {
|
|
12317
|
+
this.files.writeUserFile(snapshot.path, snapshot.content);
|
|
12318
|
+
}
|
|
12264
12319
|
} catch (error) {
|
|
12265
12320
|
this.log.error(`rollback failed for ${snapshot.path}`, error);
|
|
12266
12321
|
}
|
|
@@ -13095,7 +13150,14 @@ class DriftService {
|
|
|
13095
13150
|
return preview.map((target) => this.classify({ key: target.key, label: target.label, path: target.path, format: target.format }, target.content, target.currentContent ?? null));
|
|
13096
13151
|
}
|
|
13097
13152
|
officialFiles(harness) {
|
|
13098
|
-
const
|
|
13153
|
+
const adapter = this.adapters.get(harness);
|
|
13154
|
+
let targets;
|
|
13155
|
+
try {
|
|
13156
|
+
const writes = this.activation.expectedWrites(harness);
|
|
13157
|
+
targets = adapter.targets().filter((target) => writes.some((write2) => write2.path === target.path));
|
|
13158
|
+
} catch {
|
|
13159
|
+
targets = adapter.targets().filter((target) => target.key !== "auth");
|
|
13160
|
+
}
|
|
13099
13161
|
return targets.map((target) => {
|
|
13100
13162
|
const live = this.files.readOptional(target.path) ?? null;
|
|
13101
13163
|
const base = {
|
|
@@ -13559,12 +13621,126 @@ async function readBody2(read) {
|
|
|
13559
13621
|
|
|
13560
13622
|
// src/services/transfer.ts
|
|
13561
13623
|
import { createCipheriv as createCipheriv2, createDecipheriv as createDecipheriv2, randomBytes as randomBytes4, scryptSync } from "crypto";
|
|
13624
|
+
|
|
13625
|
+
// src/services/codex-login-cache.ts
|
|
13626
|
+
import { lstatSync } from "fs";
|
|
13627
|
+
import { join as join11 } from "path";
|
|
13628
|
+
var AUTH_FILE = "auth.json";
|
|
13629
|
+
var MAX_CACHE_BYTES = 1024 * 1024;
|
|
13630
|
+
var ICodexLoginCacheService = createDecorator("codexLoginCacheService");
|
|
13631
|
+
var _dec = [
|
|
13632
|
+
inject(IEnvironmentService, IFileService)
|
|
13633
|
+
];
|
|
13634
|
+
var _init = __decoratorStart(undefined);
|
|
13635
|
+
|
|
13636
|
+
class CodexLoginCacheService {
|
|
13637
|
+
environment;
|
|
13638
|
+
files;
|
|
13639
|
+
constructor(environment, files) {
|
|
13640
|
+
this.environment = environment;
|
|
13641
|
+
this.files = files;
|
|
13642
|
+
}
|
|
13643
|
+
exists() {
|
|
13644
|
+
const file = this.path();
|
|
13645
|
+
this.assertDirectory(false);
|
|
13646
|
+
return this.assertRegularFile(file, false);
|
|
13647
|
+
}
|
|
13648
|
+
readOptional() {
|
|
13649
|
+
const file = this.path();
|
|
13650
|
+
this.assertDirectory(false);
|
|
13651
|
+
if (!this.assertRegularFile(file, false)) {
|
|
13652
|
+
return;
|
|
13653
|
+
}
|
|
13654
|
+
const content = this.files.readOptional(file);
|
|
13655
|
+
if (content === undefined) {
|
|
13656
|
+
return;
|
|
13657
|
+
}
|
|
13658
|
+
this.validate(content);
|
|
13659
|
+
return content;
|
|
13660
|
+
}
|
|
13661
|
+
validate(content) {
|
|
13662
|
+
if (Buffer.byteLength(content, "utf8") > MAX_CACHE_BYTES) {
|
|
13663
|
+
throw new HttpError(400, "Codex \u767B\u5F55\u7F13\u5B58\u8FC7\u5927\uFF0C\u62D2\u7EDD\u8FC1\u79FB");
|
|
13664
|
+
}
|
|
13665
|
+
try {
|
|
13666
|
+
const value = JSON.parse(content);
|
|
13667
|
+
if (!isPlainObject2(value)) {
|
|
13668
|
+
throw new Error("not an object");
|
|
13669
|
+
}
|
|
13670
|
+
} catch {
|
|
13671
|
+
throw new HttpError(400, "Codex \u767B\u5F55\u7F13\u5B58\u4E0D\u662F\u6709\u6548\u7684 JSON \u5BF9\u8C61\uFF0C\u62D2\u7EDD\u8FC1\u79FB");
|
|
13672
|
+
}
|
|
13673
|
+
}
|
|
13674
|
+
prepareWrite(content) {
|
|
13675
|
+
this.validate(content);
|
|
13676
|
+
const directory = this.assertDirectory(true);
|
|
13677
|
+
const path = join11(directory, AUTH_FILE);
|
|
13678
|
+
this.assertRegularFile(path, false);
|
|
13679
|
+
return { path, content };
|
|
13680
|
+
}
|
|
13681
|
+
write(content) {
|
|
13682
|
+
const write2 = this.prepareWrite(content);
|
|
13683
|
+
this.files.writeUserSecretFile(write2.path, write2.content);
|
|
13684
|
+
}
|
|
13685
|
+
path() {
|
|
13686
|
+
return join11(this.environment.harnessHomes.codex, AUTH_FILE);
|
|
13687
|
+
}
|
|
13688
|
+
assertDirectory(create) {
|
|
13689
|
+
const directory = this.environment.harnessHomes.codex;
|
|
13690
|
+
try {
|
|
13691
|
+
const stat2 = lstatSync(directory);
|
|
13692
|
+
if (stat2.isSymbolicLink() || !stat2.isDirectory()) {
|
|
13693
|
+
throw new HttpError(400, "Codex \u914D\u7F6E\u76EE\u5F55\u5FC5\u987B\u662F\u666E\u901A\u76EE\u5F55\uFF0C\u62D2\u7EDD\u8FC1\u79FB\u767B\u5F55\u7F13\u5B58");
|
|
13694
|
+
}
|
|
13695
|
+
return directory;
|
|
13696
|
+
} catch (error2) {
|
|
13697
|
+
if (error2.code !== "ENOENT") {
|
|
13698
|
+
throw error2;
|
|
13699
|
+
}
|
|
13700
|
+
if (!create) {
|
|
13701
|
+
return directory;
|
|
13702
|
+
}
|
|
13703
|
+
this.files.ensureDir(directory);
|
|
13704
|
+
const created = lstatSync(directory);
|
|
13705
|
+
if (created.isSymbolicLink() || !created.isDirectory()) {
|
|
13706
|
+
throw new HttpError(400, "Codex \u914D\u7F6E\u76EE\u5F55\u5FC5\u987B\u662F\u666E\u901A\u76EE\u5F55\uFF0C\u62D2\u7EDD\u8FC1\u79FB\u767B\u5F55\u7F13\u5B58");
|
|
13707
|
+
}
|
|
13708
|
+
return directory;
|
|
13709
|
+
}
|
|
13710
|
+
}
|
|
13711
|
+
assertRegularFile(file, required) {
|
|
13712
|
+
try {
|
|
13713
|
+
const stat2 = lstatSync(file);
|
|
13714
|
+
if (stat2.isSymbolicLink() || !stat2.isFile()) {
|
|
13715
|
+
throw new HttpError(400, "Codex \u767B\u5F55\u7F13\u5B58\u5FC5\u987B\u662F\u666E\u901A\u6587\u4EF6\uFF0C\u62D2\u7EDD\u8FC1\u79FB");
|
|
13716
|
+
}
|
|
13717
|
+
if (stat2.size > MAX_CACHE_BYTES) {
|
|
13718
|
+
throw new HttpError(400, "Codex \u767B\u5F55\u7F13\u5B58\u8FC7\u5927\uFF0C\u62D2\u7EDD\u8FC1\u79FB");
|
|
13719
|
+
}
|
|
13720
|
+
return true;
|
|
13721
|
+
} catch (error2) {
|
|
13722
|
+
if (error2.code === "ENOENT" && !required) {
|
|
13723
|
+
return false;
|
|
13724
|
+
}
|
|
13725
|
+
throw error2;
|
|
13726
|
+
}
|
|
13727
|
+
}
|
|
13728
|
+
}
|
|
13729
|
+
CodexLoginCacheService = __decorateElement(_init, 0, "CodexLoginCacheService", _dec, CodexLoginCacheService);
|
|
13730
|
+
__runInitializers(_init, 1, CodexLoginCacheService);
|
|
13731
|
+
__decoratorMetadata(_init, CodexLoginCacheService);
|
|
13732
|
+
let _CodexLoginCacheService = CodexLoginCacheService;
|
|
13733
|
+
function isPlainObject2(value) {
|
|
13734
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
13735
|
+
}
|
|
13736
|
+
|
|
13737
|
+
// src/services/transfer.ts
|
|
13562
13738
|
var FORMAT = "harness-switch-encrypted-export";
|
|
13563
13739
|
var VERSION = 1;
|
|
13564
13740
|
var MAX_ENCRYPTED_BYTES = 10 * 1024 * 1024;
|
|
13565
13741
|
var ITransferService = createDecorator("transferService");
|
|
13566
13742
|
var _dec = [
|
|
13567
|
-
inject(IEnvironmentService, IFileService, ICryptoService, IAdapterRegistry, IActivationService, IVaultService)
|
|
13743
|
+
inject(IEnvironmentService, IFileService, ICryptoService, ICodexLoginCacheService, ILiveWriteService, IAdapterRegistry, IActivationService, IVaultService)
|
|
13568
13744
|
];
|
|
13569
13745
|
var _init = __decoratorStart(undefined);
|
|
13570
13746
|
|
|
@@ -13572,19 +13748,30 @@ class TransferService {
|
|
|
13572
13748
|
environment;
|
|
13573
13749
|
files;
|
|
13574
13750
|
crypto;
|
|
13751
|
+
codexLoginCache;
|
|
13752
|
+
liveWrite;
|
|
13575
13753
|
adapters;
|
|
13576
13754
|
activation;
|
|
13577
13755
|
vault;
|
|
13578
|
-
constructor(environment, files, crypto2, adapters, activation, vault) {
|
|
13756
|
+
constructor(environment, files, crypto2, codexLoginCache, liveWrite, adapters, activation, vault) {
|
|
13579
13757
|
this.environment = environment;
|
|
13580
13758
|
this.files = files;
|
|
13581
13759
|
this.crypto = crypto2;
|
|
13760
|
+
this.codexLoginCache = codexLoginCache;
|
|
13761
|
+
this.liveWrite = liveWrite;
|
|
13582
13762
|
this.adapters = adapters;
|
|
13583
13763
|
this.activation = activation;
|
|
13584
13764
|
this.vault = vault;
|
|
13585
13765
|
}
|
|
13586
|
-
|
|
13766
|
+
exportPreview() {
|
|
13767
|
+
return { codexLoginCacheAvailable: this.codexLoginCache.exists() };
|
|
13768
|
+
}
|
|
13769
|
+
exportAll(passphrase, includeCodexLoginCache) {
|
|
13587
13770
|
this.assertPassphrase(passphrase);
|
|
13771
|
+
const codexLoginCache = includeCodexLoginCache ? this.codexLoginCache.readOptional() : undefined;
|
|
13772
|
+
if (includeCodexLoginCache && codexLoginCache === undefined) {
|
|
13773
|
+
throw new HttpError(400, "\u5F53\u524D\u7528\u6237\u6CA1\u6709\u53EF\u5BFC\u51FA\u7684 Codex \u767B\u5F55\u7F13\u5B58");
|
|
13774
|
+
}
|
|
13588
13775
|
const store = this.readStore();
|
|
13589
13776
|
const profiles = [];
|
|
13590
13777
|
for (const harness of HARNESS_IDS) {
|
|
@@ -13616,12 +13803,15 @@ class TransferService {
|
|
|
13616
13803
|
version: VERSION,
|
|
13617
13804
|
exportedAt: new Date().toISOString(),
|
|
13618
13805
|
profiles,
|
|
13619
|
-
active
|
|
13806
|
+
active,
|
|
13807
|
+
codexLoginCache
|
|
13620
13808
|
}, passphrase);
|
|
13621
13809
|
}
|
|
13622
|
-
preview(envelope, passphrase) {
|
|
13810
|
+
preview(envelope, passphrase, conflictPolicy, restoreActive) {
|
|
13811
|
+
this.assertConflictPolicy(conflictPolicy);
|
|
13623
13812
|
const payload = this.decrypt(envelope, passphrase);
|
|
13624
|
-
const
|
|
13813
|
+
const existing = this.readStore();
|
|
13814
|
+
const plan = this.planImport(payload, existing, conflictPolicy, restoreActive);
|
|
13625
13815
|
return {
|
|
13626
13816
|
exportedAt: payload.exportedAt,
|
|
13627
13817
|
profileCount: payload.profiles.length,
|
|
@@ -13629,35 +13819,41 @@ class TransferService {
|
|
|
13629
13819
|
harness,
|
|
13630
13820
|
profiles: payload.profiles.filter((profile) => profile.harness === harness).length
|
|
13631
13821
|
})).filter((item) => item.profiles > 0),
|
|
13632
|
-
conflicts: payload.profiles.filter((profile) =>
|
|
13633
|
-
activeCount: payload.active.length
|
|
13822
|
+
conflicts: payload.profiles.filter((profile) => existing[profile.harness]?.[profile.name] !== undefined).map(({ harness, name }) => ({ harness, name })),
|
|
13823
|
+
activeCount: payload.active.length,
|
|
13824
|
+
conflictPolicy,
|
|
13825
|
+
restoreActive,
|
|
13826
|
+
codexActivationAuthEffect: plan.codexActivationAuthEffect,
|
|
13827
|
+
codexLoginCache: {
|
|
13828
|
+
available: payload.codexLoginCache !== undefined,
|
|
13829
|
+
targetExists: this.codexLoginCache.exists()
|
|
13830
|
+
}
|
|
13634
13831
|
};
|
|
13635
13832
|
}
|
|
13636
|
-
importAll(envelope, passphrase, conflictPolicy, restoreActive) {
|
|
13637
|
-
|
|
13638
|
-
throw new HttpError(400, "invalid conflict policy");
|
|
13639
|
-
}
|
|
13833
|
+
importAll(envelope, passphrase, conflictPolicy, restoreActive, migrateCodexLoginCache) {
|
|
13834
|
+
this.assertConflictPolicy(conflictPolicy);
|
|
13640
13835
|
const payload = this.decrypt(envelope, passphrase);
|
|
13641
|
-
|
|
13642
|
-
|
|
13643
|
-
|
|
13644
|
-
|
|
13645
|
-
|
|
13646
|
-
|
|
13647
|
-
|
|
13648
|
-
|
|
13649
|
-
if (exists && conflictPolicy === "skip") {
|
|
13650
|
-
skipped++;
|
|
13651
|
-
continue;
|
|
13836
|
+
if (migrateCodexLoginCache && payload.codexLoginCache === undefined) {
|
|
13837
|
+
throw new HttpError(400, "\u5BFC\u51FA\u5305\u4E0D\u5305\u542B\u53EF\u8FC1\u79FB\u7684 Codex \u767B\u5F55\u7F13\u5B58");
|
|
13838
|
+
}
|
|
13839
|
+
const cacheWrite = migrateCodexLoginCache ? [
|
|
13840
|
+
{
|
|
13841
|
+
...this.codexLoginCache.prepareWrite(payload.codexLoginCache),
|
|
13842
|
+
format: "json",
|
|
13843
|
+
secret: true
|
|
13652
13844
|
}
|
|
13653
|
-
|
|
13654
|
-
|
|
13655
|
-
|
|
13656
|
-
|
|
13657
|
-
|
|
13845
|
+
] : [];
|
|
13846
|
+
const plan = this.planImport(payload, this.readStore(), conflictPolicy, restoreActive);
|
|
13847
|
+
const profilesPath = this.environment.files.profiles;
|
|
13848
|
+
const profileSnapshot = this.files.readOptional(profilesPath);
|
|
13849
|
+
this.liveWrite.transaction("codex", "\u5BFC\u5165\u767B\u5F55\u7F13\u5B58", cacheWrite, () => {
|
|
13850
|
+
try {
|
|
13851
|
+
this.files.writeJson(profilesPath, plan.store);
|
|
13852
|
+
} catch (error2) {
|
|
13853
|
+
this.restore(profilesPath, profileSnapshot);
|
|
13854
|
+
throw error2;
|
|
13658
13855
|
}
|
|
13659
|
-
}
|
|
13660
|
-
this.files.writeJson(this.environment.files.profiles, store);
|
|
13856
|
+
});
|
|
13661
13857
|
const warnings = [];
|
|
13662
13858
|
let activeRestored = 0;
|
|
13663
13859
|
if (restoreActive) {
|
|
@@ -13665,7 +13861,7 @@ class TransferService {
|
|
|
13665
13861
|
try {
|
|
13666
13862
|
if (active.official) {
|
|
13667
13863
|
this.activation.activateOfficial(active.harness);
|
|
13668
|
-
} else if (store[active.harness]?.[active.name]) {
|
|
13864
|
+
} else if (plan.store[active.harness]?.[active.name]) {
|
|
13669
13865
|
this.activation.activate(active.harness, active.name);
|
|
13670
13866
|
} else {
|
|
13671
13867
|
throw new Error("\u5BF9\u5E94\u914D\u7F6E\u4E0D\u5B58\u5728");
|
|
@@ -13676,7 +13872,15 @@ class TransferService {
|
|
|
13676
13872
|
}
|
|
13677
13873
|
}
|
|
13678
13874
|
}
|
|
13679
|
-
return {
|
|
13875
|
+
return {
|
|
13876
|
+
ok: true,
|
|
13877
|
+
imported: plan.imported,
|
|
13878
|
+
overwritten: plan.overwritten,
|
|
13879
|
+
skipped: plan.skipped,
|
|
13880
|
+
activeRestored,
|
|
13881
|
+
codexLoginCacheMigrated: cacheWrite.length > 0,
|
|
13882
|
+
warnings
|
|
13883
|
+
};
|
|
13680
13884
|
}
|
|
13681
13885
|
decrypt(envelope, passphrase) {
|
|
13682
13886
|
this.assertPassphrase(passphrase);
|
|
@@ -13697,8 +13901,93 @@ class TransferService {
|
|
|
13697
13901
|
throw new HttpError(400, "\u5BFC\u51FA\u6587\u4EF6\u4E2D\u7684\u6FC0\u6D3B\u72B6\u6001\u65E0\u6548");
|
|
13698
13902
|
}
|
|
13699
13903
|
}
|
|
13904
|
+
if (payload.codexLoginCache !== undefined) {
|
|
13905
|
+
if (typeof payload.codexLoginCache !== "string") {
|
|
13906
|
+
throw new HttpError(400, "\u5BFC\u51FA\u6587\u4EF6\u4E2D\u7684 Codex \u767B\u5F55\u7F13\u5B58\u65E0\u6548");
|
|
13907
|
+
}
|
|
13908
|
+
this.codexLoginCache.validate(payload.codexLoginCache);
|
|
13909
|
+
}
|
|
13910
|
+
this.validatePayload(payload);
|
|
13700
13911
|
return payload;
|
|
13701
13912
|
}
|
|
13913
|
+
assertConflictPolicy(conflictPolicy) {
|
|
13914
|
+
if (conflictPolicy !== "skip" && conflictPolicy !== "overwrite") {
|
|
13915
|
+
throw new HttpError(400, "invalid conflict policy");
|
|
13916
|
+
}
|
|
13917
|
+
}
|
|
13918
|
+
validatePayload(payload) {
|
|
13919
|
+
const profileIds = new Set;
|
|
13920
|
+
for (const profile of payload.profiles) {
|
|
13921
|
+
this.validateProfile(profile);
|
|
13922
|
+
const id = profileId(profile.harness, profile.name);
|
|
13923
|
+
if (profileIds.has(id)) {
|
|
13924
|
+
throw new HttpError(400, "\u5BFC\u51FA\u6587\u4EF6\u5305\u542B\u91CD\u590D\u7684\u914D\u7F6E");
|
|
13925
|
+
}
|
|
13926
|
+
profileIds.add(id);
|
|
13927
|
+
}
|
|
13928
|
+
const activeHarnesses = new Set;
|
|
13929
|
+
for (const active of payload.active) {
|
|
13930
|
+
if (activeHarnesses.has(active.harness)) {
|
|
13931
|
+
throw new HttpError(400, "\u5BFC\u51FA\u6587\u4EF6\u5305\u542B\u91CD\u590D\u7684\u6FC0\u6D3B\u72B6\u6001");
|
|
13932
|
+
}
|
|
13933
|
+
activeHarnesses.add(active.harness);
|
|
13934
|
+
const adapter = this.adapters.get(active.harness);
|
|
13935
|
+
if (active.official) {
|
|
13936
|
+
if (active.name !== "\u5B98\u65B9\u767B\u5F55" || !adapter.renderOfficial) {
|
|
13937
|
+
throw new HttpError(400, "\u5BFC\u51FA\u6587\u4EF6\u4E2D\u7684\u5B98\u65B9\u767B\u5F55\u72B6\u6001\u65E0\u6548");
|
|
13938
|
+
}
|
|
13939
|
+
} else if (!profileIds.has(profileId(active.harness, active.name))) {
|
|
13940
|
+
throw new HttpError(400, "\u5BFC\u51FA\u6587\u4EF6\u4E2D\u7684\u6FC0\u6D3B\u72B6\u6001\u5F15\u7528\u4E86\u4E0D\u5B58\u5728\u7684\u914D\u7F6E");
|
|
13941
|
+
}
|
|
13942
|
+
}
|
|
13943
|
+
}
|
|
13944
|
+
planImport(payload, existing, conflictPolicy, restoreActive) {
|
|
13945
|
+
const store = structuredClone(existing);
|
|
13946
|
+
let imported = 0;
|
|
13947
|
+
let overwritten = 0;
|
|
13948
|
+
let skipped = 0;
|
|
13949
|
+
for (const profile of payload.profiles) {
|
|
13950
|
+
store[profile.harness] ||= {};
|
|
13951
|
+
const exists = store[profile.harness][profile.name] !== undefined;
|
|
13952
|
+
if (exists && conflictPolicy === "skip") {
|
|
13953
|
+
skipped++;
|
|
13954
|
+
continue;
|
|
13955
|
+
}
|
|
13956
|
+
store[profile.harness][profile.name] = this.toStored(profile);
|
|
13957
|
+
if (exists) {
|
|
13958
|
+
overwritten++;
|
|
13959
|
+
} else {
|
|
13960
|
+
imported++;
|
|
13961
|
+
}
|
|
13962
|
+
}
|
|
13963
|
+
return {
|
|
13964
|
+
store,
|
|
13965
|
+
imported,
|
|
13966
|
+
overwritten,
|
|
13967
|
+
skipped,
|
|
13968
|
+
codexActivationAuthEffect: this.codexActivationAuthEffect(payload, store, restoreActive)
|
|
13969
|
+
};
|
|
13970
|
+
}
|
|
13971
|
+
codexActivationAuthEffect(payload, store, restoreActive) {
|
|
13972
|
+
if (!restoreActive) {
|
|
13973
|
+
return "none";
|
|
13974
|
+
}
|
|
13975
|
+
const active = payload.active.find((item) => item.harness === "codex");
|
|
13976
|
+
if (!active) {
|
|
13977
|
+
return "none";
|
|
13978
|
+
}
|
|
13979
|
+
if (active.official) {
|
|
13980
|
+
return "official-cleanup";
|
|
13981
|
+
}
|
|
13982
|
+
const profile = store.codex?.[active.name];
|
|
13983
|
+
if (!profile) {
|
|
13984
|
+
return "none";
|
|
13985
|
+
}
|
|
13986
|
+
if (profile.overrides?.auth !== undefined) {
|
|
13987
|
+
return "auth-override";
|
|
13988
|
+
}
|
|
13989
|
+
return profile.extras?.authMode === "openai_auth" ? "openai-api-key" : "none";
|
|
13990
|
+
}
|
|
13702
13991
|
validateProfile(profile) {
|
|
13703
13992
|
if (!profile.name || profile.name.length > 120 || /[\\/]/.test(profile.name)) {
|
|
13704
13993
|
throw new HttpError(400, "\u5BFC\u51FA\u6587\u4EF6\u5305\u542B\u65E0\u6548\u7684\u914D\u7F6E\u540D\u79F0");
|
|
@@ -13725,6 +14014,14 @@ class TransferService {
|
|
|
13725
14014
|
readStore() {
|
|
13726
14015
|
return this.files.readJsonStrict(this.environment.files.profiles, {});
|
|
13727
14016
|
}
|
|
14017
|
+
restore(path, snapshot) {
|
|
14018
|
+
try {
|
|
14019
|
+
if (snapshot === undefined)
|
|
14020
|
+
this.files.remove(path);
|
|
14021
|
+
else
|
|
14022
|
+
this.files.writeSecure(path, snapshot);
|
|
14023
|
+
} catch {}
|
|
14024
|
+
}
|
|
13728
14025
|
resolveKey(stored) {
|
|
13729
14026
|
if (!stored.provider_id) {
|
|
13730
14027
|
return this.crypto.decrypt(stored.api_key);
|
|
@@ -13781,6 +14078,9 @@ function decrypt(envelope, passphrase) {
|
|
|
13781
14078
|
throw new HttpError(400, "\u8FC1\u79FB\u5BC6\u7801\u9519\u8BEF\u6216\u5BFC\u51FA\u6587\u4EF6\u5DF2\u635F\u574F");
|
|
13782
14079
|
}
|
|
13783
14080
|
}
|
|
14081
|
+
function profileId(harness, name) {
|
|
14082
|
+
return `${harness}\x00${name}`;
|
|
14083
|
+
}
|
|
13784
14084
|
function isStringRecord(value) {
|
|
13785
14085
|
return typeof value === "object" && value !== null && !Array.isArray(value) && Object.values(value).every((item) => typeof item === "string");
|
|
13786
14086
|
}
|
|
@@ -13803,17 +14103,18 @@ function isPortableActive(value) {
|
|
|
13803
14103
|
function createTransferRoutes(services) {
|
|
13804
14104
|
const app = new Hono2;
|
|
13805
14105
|
const transfer = services.get(ITransferService);
|
|
14106
|
+
app.get("/export/preview", (c) => c.json(transfer.exportPreview()));
|
|
13806
14107
|
app.post("/export", async (c) => {
|
|
13807
14108
|
const body = await readBody3(c.req.json.bind(c.req));
|
|
13808
|
-
return c.json(transfer.exportAll(String(body.passphrase ?? "")));
|
|
14109
|
+
return c.json(transfer.exportAll(String(body.passphrase ?? ""), body.includeCodexLoginCache === true));
|
|
13809
14110
|
});
|
|
13810
14111
|
app.post("/preview", async (c) => {
|
|
13811
14112
|
const body = await readBody3(c.req.json.bind(c.req));
|
|
13812
|
-
return c.json(transfer.preview(requireEnvelope(body.envelope), String(body.passphrase ?? "")));
|
|
14113
|
+
return c.json(transfer.preview(requireEnvelope(body.envelope), String(body.passphrase ?? ""), body.conflictPolicy ?? "skip", body.restoreActive === true));
|
|
13813
14114
|
});
|
|
13814
14115
|
app.post("/import", async (c) => {
|
|
13815
14116
|
const body = await readBody3(c.req.json.bind(c.req));
|
|
13816
|
-
return c.json(transfer.importAll(requireEnvelope(body.envelope), String(body.passphrase ?? ""), body.conflictPolicy ?? "skip", body.restoreActive === true));
|
|
14117
|
+
return c.json(transfer.importAll(requireEnvelope(body.envelope), String(body.passphrase ?? ""), body.conflictPolicy ?? "skip", body.restoreActive === true, body.migrateCodexLoginCache === true));
|
|
13817
14118
|
});
|
|
13818
14119
|
return app;
|
|
13819
14120
|
}
|
|
@@ -13841,9 +14142,10 @@ function createUpdateRoutes(_services) {
|
|
|
13841
14142
|
}
|
|
13842
14143
|
|
|
13843
14144
|
// src/services/user-sync.ts
|
|
14145
|
+
import { isDeepStrictEqual } from "util";
|
|
13844
14146
|
var IUserSyncService = createDecorator("userSyncService");
|
|
13845
14147
|
var _dec = [
|
|
13846
|
-
inject(IEnvironmentService, IUserService, IFileService, ICryptoService)
|
|
14148
|
+
inject(IEnvironmentService, IUserService, IFileService, ICryptoService, ICodexLoginCacheService, ILiveWriteService, IActivationService)
|
|
13847
14149
|
];
|
|
13848
14150
|
var _init = __decoratorStart(undefined);
|
|
13849
14151
|
|
|
@@ -13852,24 +14154,31 @@ class UserSyncService {
|
|
|
13852
14154
|
users;
|
|
13853
14155
|
files;
|
|
13854
14156
|
crypto;
|
|
13855
|
-
|
|
14157
|
+
codexLoginCache;
|
|
14158
|
+
liveWrite;
|
|
14159
|
+
activation;
|
|
14160
|
+
constructor(environment, users, files, crypto2, codexLoginCache, liveWrite, activation) {
|
|
13856
14161
|
this.environment = environment;
|
|
13857
14162
|
this.users = users;
|
|
13858
14163
|
this.files = files;
|
|
13859
14164
|
this.crypto = crypto2;
|
|
14165
|
+
this.codexLoginCache = codexLoginCache;
|
|
14166
|
+
this.liveWrite = liveWrite;
|
|
14167
|
+
this.activation = activation;
|
|
13860
14168
|
}
|
|
13861
14169
|
preview(sourceUsername) {
|
|
13862
14170
|
const target = this.environment.currentUser;
|
|
13863
14171
|
const source = this.requireSource(sourceUsername, target);
|
|
13864
14172
|
const portable = this.readPortable(source);
|
|
13865
|
-
const
|
|
14173
|
+
const targetPortable = this.readPortable(target);
|
|
13866
14174
|
const conflicts = [];
|
|
13867
14175
|
let profileCount = 0;
|
|
13868
14176
|
const referencedProviders = new Set;
|
|
13869
14177
|
for (const [harness, profiles] of Object.entries(portable.profiles)) {
|
|
13870
14178
|
for (const [name, profile] of Object.entries(profiles)) {
|
|
13871
14179
|
profileCount++;
|
|
13872
|
-
|
|
14180
|
+
const targetProfile = targetPortable.profiles[harness]?.[name];
|
|
14181
|
+
if (targetProfile && !this.profilesEqual(profile, portable.providers, targetProfile, targetPortable.providers)) {
|
|
13873
14182
|
conflicts.push({ harness, name });
|
|
13874
14183
|
}
|
|
13875
14184
|
if (profile.provider_id)
|
|
@@ -13881,16 +14190,34 @@ class UserSyncService {
|
|
|
13881
14190
|
targetUser: target.username,
|
|
13882
14191
|
profileCount,
|
|
13883
14192
|
providerCount: referencedProviders.size,
|
|
13884
|
-
conflicts
|
|
14193
|
+
conflicts,
|
|
14194
|
+
codexLoginCache: {
|
|
14195
|
+
available: this.environment.runAsUser(source, () => this.codexLoginCache.exists()),
|
|
14196
|
+
targetExists: this.codexLoginCache.exists()
|
|
14197
|
+
}
|
|
13885
14198
|
};
|
|
13886
14199
|
}
|
|
13887
|
-
sync(sourceUsername, conflictPolicy) {
|
|
14200
|
+
sync(sourceUsername, conflictPolicy, migrateCodexLoginCache, overwriteHarnesses = []) {
|
|
13888
14201
|
if (conflictPolicy !== "skip" && conflictPolicy !== "overwrite") {
|
|
13889
14202
|
throw new HttpError(400, "invalid conflict policy");
|
|
13890
14203
|
}
|
|
13891
14204
|
const target = this.environment.currentUser;
|
|
13892
14205
|
const source = this.requireSource(sourceUsername, target);
|
|
14206
|
+
const selectiveOverwrites = new Set(overwriteHarnesses);
|
|
14207
|
+
const overwriteAll = conflictPolicy === "overwrite";
|
|
13893
14208
|
const portable = this.readPortable(source);
|
|
14209
|
+
const targetPortable = this.readPortable(target);
|
|
14210
|
+
const cacheContent = migrateCodexLoginCache ? this.environment.runAsUser(source, () => this.codexLoginCache.readOptional()) : undefined;
|
|
14211
|
+
if (migrateCodexLoginCache && cacheContent === undefined) {
|
|
14212
|
+
throw new HttpError(400, "\u6765\u6E90\u7528\u6237\u6CA1\u6709\u53EF\u8FC1\u79FB\u7684 Codex \u767B\u5F55\u7F13\u5B58");
|
|
14213
|
+
}
|
|
14214
|
+
const cacheWrite = cacheContent ? [
|
|
14215
|
+
{
|
|
14216
|
+
...this.codexLoginCache.prepareWrite(cacheContent),
|
|
14217
|
+
format: "json",
|
|
14218
|
+
secret: true
|
|
14219
|
+
}
|
|
14220
|
+
] : [];
|
|
13894
14221
|
const profilesPath = this.environment.files.profiles;
|
|
13895
14222
|
const vaultPath = this.environment.files.vault;
|
|
13896
14223
|
const profileSnapshot = this.files.readOptional(profilesPath);
|
|
@@ -13899,6 +14226,7 @@ class UserSyncService {
|
|
|
13899
14226
|
const targetVault = structuredClone(this.readVault());
|
|
13900
14227
|
const active = this.files.readJson(this.environment.files.active, {});
|
|
13901
14228
|
const warnings = [];
|
|
14229
|
+
const activeProfilesToReapply = new Map;
|
|
13902
14230
|
let imported = 0;
|
|
13903
14231
|
let overwritten = 0;
|
|
13904
14232
|
let skipped = 0;
|
|
@@ -13906,7 +14234,10 @@ class UserSyncService {
|
|
|
13906
14234
|
for (const [harness, profiles] of Object.entries(portable.profiles)) {
|
|
13907
14235
|
for (const [name, profile] of Object.entries(profiles)) {
|
|
13908
14236
|
const exists = targetProfiles[harness]?.[name] !== undefined;
|
|
13909
|
-
|
|
14237
|
+
const targetProfile = targetPortable.profiles[harness]?.[name];
|
|
14238
|
+
const unchanged = targetProfile !== undefined && this.profilesEqual(profile, portable.providers, targetProfile, targetPortable.providers);
|
|
14239
|
+
const overwrite = overwriteAll || selectiveOverwrites.has(harness);
|
|
14240
|
+
if (unchanged || exists && !overwrite) {
|
|
13910
14241
|
skipped++;
|
|
13911
14242
|
continue;
|
|
13912
14243
|
}
|
|
@@ -13914,7 +14245,7 @@ class UserSyncService {
|
|
|
13914
14245
|
if (exists) {
|
|
13915
14246
|
overwritten++;
|
|
13916
14247
|
if (active[harness]?.name === name) {
|
|
13917
|
-
|
|
14248
|
+
activeProfilesToReapply.set(harness, name);
|
|
13918
14249
|
}
|
|
13919
14250
|
} else
|
|
13920
14251
|
imported++;
|
|
@@ -13953,13 +14284,23 @@ class UserSyncService {
|
|
|
13953
14284
|
updated_at: new Date().toISOString()
|
|
13954
14285
|
};
|
|
13955
14286
|
}
|
|
13956
|
-
|
|
13957
|
-
|
|
13958
|
-
|
|
13959
|
-
|
|
13960
|
-
|
|
13961
|
-
|
|
13962
|
-
|
|
14287
|
+
this.liveWrite.transaction("codex", `\u540C\u6B65-${source.username}`, cacheWrite, () => {
|
|
14288
|
+
try {
|
|
14289
|
+
this.files.writeJson(vaultPath, targetVault);
|
|
14290
|
+
this.files.writeJson(profilesPath, targetProfiles);
|
|
14291
|
+
} catch (error2) {
|
|
14292
|
+
this.restore(vaultPath, vaultSnapshot);
|
|
14293
|
+
this.restore(profilesPath, profileSnapshot);
|
|
14294
|
+
throw error2;
|
|
14295
|
+
}
|
|
14296
|
+
});
|
|
14297
|
+
for (const [harness, name] of activeProfilesToReapply) {
|
|
14298
|
+
try {
|
|
14299
|
+
const result = this.activation.activate(harness, name);
|
|
14300
|
+
warnings.push(...result.warnings.map((warning) => `${harness}/${name}: ${warning}`));
|
|
14301
|
+
} catch (error2) {
|
|
14302
|
+
warnings.push(`${harness}/${name} \u5DF2\u66F4\u65B0\u914D\u7F6E\u5E93\uFF0C\u4F46\u81EA\u52A8\u91CD\u65B0\u6FC0\u6D3B\u5931\u8D25\uFF1A${error2 instanceof Error ? error2.message : String(error2)}`);
|
|
14303
|
+
}
|
|
13963
14304
|
}
|
|
13964
14305
|
return {
|
|
13965
14306
|
ok: true,
|
|
@@ -13969,6 +14310,7 @@ class UserSyncService {
|
|
|
13969
14310
|
overwritten,
|
|
13970
14311
|
skipped,
|
|
13971
14312
|
providersCopied: providerMap.size,
|
|
14313
|
+
codexLoginCacheMigrated: cacheWrite.length > 0,
|
|
13972
14314
|
warnings
|
|
13973
14315
|
};
|
|
13974
14316
|
}
|
|
@@ -13985,16 +14327,18 @@ class UserSyncService {
|
|
|
13985
14327
|
const vault = this.readVault();
|
|
13986
14328
|
const portableProviders = {};
|
|
13987
14329
|
for (const [id, entry] of Object.entries(vault.entries)) {
|
|
13988
|
-
|
|
14330
|
+
const { api_key: encryptedKey, ...provider } = entry;
|
|
14331
|
+
portableProviders[id] = { ...provider, apiKey: this.crypto.decrypt(encryptedKey) };
|
|
13989
14332
|
}
|
|
13990
14333
|
const portableProfiles = {};
|
|
13991
14334
|
for (const [harness, entries] of Object.entries(profiles)) {
|
|
13992
14335
|
portableProfiles[harness] = {};
|
|
13993
14336
|
for (const [name, profile] of Object.entries(entries)) {
|
|
14337
|
+
const { api_key: encryptedKey, ...storedProfile } = profile;
|
|
13994
14338
|
const providerKey = profile.provider_id ? portableProviders[profile.provider_id]?.apiKey : undefined;
|
|
13995
14339
|
portableProfiles[harness][name] = {
|
|
13996
|
-
...
|
|
13997
|
-
apiKey: providerKey ?? this.crypto.decrypt(
|
|
14340
|
+
...storedProfile,
|
|
14341
|
+
apiKey: providerKey ?? this.crypto.decrypt(encryptedKey)
|
|
13998
14342
|
};
|
|
13999
14343
|
}
|
|
14000
14344
|
}
|
|
@@ -14004,6 +14348,20 @@ class UserSyncService {
|
|
|
14004
14348
|
readProfiles() {
|
|
14005
14349
|
return this.files.readJsonStrict(this.environment.files.profiles, {});
|
|
14006
14350
|
}
|
|
14351
|
+
profilesEqual(source, sourceProviders, target, targetProviders) {
|
|
14352
|
+
return isDeepStrictEqual(this.comparableProfile(source, sourceProviders), this.comparableProfile(target, targetProviders));
|
|
14353
|
+
}
|
|
14354
|
+
comparableProfile(profile, providers) {
|
|
14355
|
+
const { updated_at: _updatedAt, provider_id: providerId, ...content } = profile;
|
|
14356
|
+
const provider = providerId ? providers[providerId] : undefined;
|
|
14357
|
+
const comparableProvider = provider ? {
|
|
14358
|
+
name: provider.name,
|
|
14359
|
+
notes: provider.notes,
|
|
14360
|
+
endpoints: provider.endpoints,
|
|
14361
|
+
apiKey: provider.apiKey
|
|
14362
|
+
} : providerId ? { missing: true } : undefined;
|
|
14363
|
+
return { ...content, provider: comparableProvider };
|
|
14364
|
+
}
|
|
14007
14365
|
readVault() {
|
|
14008
14366
|
return this.files.readJsonStrict(this.environment.files.vault, {
|
|
14009
14367
|
version: 1,
|
|
@@ -14060,7 +14418,7 @@ function createUserRoutes(services) {
|
|
|
14060
14418
|
});
|
|
14061
14419
|
app.post("/sync", async (c) => {
|
|
14062
14420
|
const body = await readBody4(c.req.json.bind(c.req));
|
|
14063
|
-
return c.json(sync.sync(String(body.sourceUser ?? ""), body.conflictPolicy ?? "skip"));
|
|
14421
|
+
return c.json(sync.sync(String(body.sourceUser ?? ""), body.conflictPolicy ?? "skip", body.migrateCodexLoginCache === true, parseOverwriteHarnesses(body.overwriteHarnesses)));
|
|
14064
14422
|
});
|
|
14065
14423
|
app.post("/:username/select", (c) => {
|
|
14066
14424
|
const user = users.require(decodeURIComponent(c.req.param("username")));
|
|
@@ -14077,6 +14435,14 @@ async function readBody4(read) {
|
|
|
14077
14435
|
throw new HttpError(400, "invalid json");
|
|
14078
14436
|
});
|
|
14079
14437
|
}
|
|
14438
|
+
function parseOverwriteHarnesses(value) {
|
|
14439
|
+
if (value === undefined)
|
|
14440
|
+
return [];
|
|
14441
|
+
if (!Array.isArray(value) || value.some((item) => !isHarnessId(item))) {
|
|
14442
|
+
throw new HttpError(400, "invalid overwrite harnesses");
|
|
14443
|
+
}
|
|
14444
|
+
return [...new Set(value)];
|
|
14445
|
+
}
|
|
14080
14446
|
|
|
14081
14447
|
// src/app.ts
|
|
14082
14448
|
function createApp(services) {
|
|
@@ -14120,11 +14486,11 @@ function createApp(services) {
|
|
|
14120
14486
|
if (c.req.path.startsWith("/api") || c.req.path === "/healthz") {
|
|
14121
14487
|
return next();
|
|
14122
14488
|
}
|
|
14123
|
-
const assetPath =
|
|
14489
|
+
const assetPath = join12(publicDir, c.req.path.replace(/^\//, ""));
|
|
14124
14490
|
if (c.req.path !== "/" && existsSync4(assetPath)) {
|
|
14125
14491
|
return serveStatic2({ root: publicDir })(c, next);
|
|
14126
14492
|
}
|
|
14127
|
-
const index =
|
|
14493
|
+
const index = join12(publicDir, "index.html");
|
|
14128
14494
|
if (existsSync4(index)) {
|
|
14129
14495
|
c.header("Cache-Control", "no-store");
|
|
14130
14496
|
return c.html(await Bun.file(index).text());
|
|
@@ -14151,6 +14517,7 @@ function createServices() {
|
|
|
14151
14517
|
collection.set(IUserService, new SyncDescriptor(UserService));
|
|
14152
14518
|
collection.set(IUserSyncService, new SyncDescriptor(UserSyncService));
|
|
14153
14519
|
collection.set(IFileService, new SyncDescriptor(FileService));
|
|
14520
|
+
collection.set(ICodexLoginCacheService, new SyncDescriptor(CodexLoginCacheService));
|
|
14154
14521
|
collection.set(ICryptoService, new SyncDescriptor(CryptoService));
|
|
14155
14522
|
collection.set(IAuthService, new SyncDescriptor(AuthService));
|
|
14156
14523
|
collection.set(IHarnessRegistry, new SyncDescriptor(HarnessRegistry));
|
|
@@ -14221,25 +14588,25 @@ import { createInterface } from "readline/promises";
|
|
|
14221
14588
|
|
|
14222
14589
|
// src/cli/client.ts
|
|
14223
14590
|
import { readFileSync as readFileSync4 } from "fs";
|
|
14224
|
-
import { join as
|
|
14591
|
+
import { join as join14 } from "path";
|
|
14225
14592
|
|
|
14226
14593
|
// src/daemon.ts
|
|
14227
14594
|
import { spawn as spawn2 } from "child_process";
|
|
14228
14595
|
import { mkdirSync as mkdirSync4, openSync as openSync2, readFileSync as readFileSync3, unlinkSync, writeFileSync as writeFileSync2 } from "fs";
|
|
14229
14596
|
import { homedir as homedir3 } from "os";
|
|
14230
|
-
import { join as
|
|
14597
|
+
import { join as join13, resolve } from "path";
|
|
14231
14598
|
var DEFAULT_HOST = "127.0.0.1";
|
|
14232
14599
|
var DEFAULT_PORT = 8787;
|
|
14233
14600
|
var STOP_TIMEOUT_MS = 5000;
|
|
14234
14601
|
var START_GRACE_MS = 500;
|
|
14235
14602
|
function daemonDataDir() {
|
|
14236
|
-
return process.env.HSW_DATA_DIR ||
|
|
14603
|
+
return process.env.HSW_DATA_DIR || join13(homedir3(), ".harness-switch");
|
|
14237
14604
|
}
|
|
14238
14605
|
function daemonPidFile() {
|
|
14239
|
-
return
|
|
14606
|
+
return join13(daemonDataDir(), "daemon.pid");
|
|
14240
14607
|
}
|
|
14241
14608
|
function daemonLogFile() {
|
|
14242
|
-
return
|
|
14609
|
+
return join13(daemonDataDir(), "daemon.log");
|
|
14243
14610
|
}
|
|
14244
14611
|
function readPid() {
|
|
14245
14612
|
try {
|
|
@@ -14304,7 +14671,7 @@ function printStatus() {
|
|
|
14304
14671
|
console.log(`harness-switch daemon: ${running ? `running (pid ${pid})` : "not running"}`);
|
|
14305
14672
|
console.log(` url: http://${host}:${port}`);
|
|
14306
14673
|
console.log(` log: ${daemonLogFile()}`);
|
|
14307
|
-
console.log(` password: ${
|
|
14674
|
+
console.log(` password: ${join13(daemonDataDir(), "web_password")}`);
|
|
14308
14675
|
console.log(` data: ${daemonDataDir()}`);
|
|
14309
14676
|
}
|
|
14310
14677
|
async function daemonize() {
|
|
@@ -14342,7 +14709,7 @@ async function daemonize() {
|
|
|
14342
14709
|
console.log(` url: http://${host}:${port}`);
|
|
14343
14710
|
console.log(` log: ${logPath}`);
|
|
14344
14711
|
console.log(" manage: harness-switch status | harness-switch stop");
|
|
14345
|
-
console.log(` password (first run): ${
|
|
14712
|
+
console.log(` password (first run): ${join13(daemonDataDir(), "web_password")}`);
|
|
14346
14713
|
}
|
|
14347
14714
|
function usage() {
|
|
14348
14715
|
return [
|
|
@@ -14409,7 +14776,7 @@ function resolveBaseUrl() {
|
|
|
14409
14776
|
return process.env.HSW_URL || `http://127.0.0.1:${process.env.PORT || 8787}`;
|
|
14410
14777
|
}
|
|
14411
14778
|
function readWebPassword() {
|
|
14412
|
-
const file =
|
|
14779
|
+
const file = join14(daemonDataDir(), "web_password");
|
|
14413
14780
|
try {
|
|
14414
14781
|
return readFileSync4(file, "utf8").trim();
|
|
14415
14782
|
} catch {
|
|
@@ -14509,6 +14876,7 @@ function cliUsage() {
|
|
|
14509
14876
|
" --harness H limit doctor to one harness",
|
|
14510
14877
|
" --user USER run list/providers/doctor/plan/activate for this Unix user",
|
|
14511
14878
|
" --overwrite overwrite same-name profiles during sync (default: skip)",
|
|
14879
|
+
" --copy-codex-auth copy the source user\u2019s Codex auth.json during sync",
|
|
14512
14880
|
"",
|
|
14513
14881
|
"environment:",
|
|
14514
14882
|
" HSW_URL base URL of the local service (default http://127.0.0.1:8787)",
|
|
@@ -14578,15 +14946,17 @@ async function cmdSync(client, flags, json) {
|
|
|
14578
14946
|
}
|
|
14579
14947
|
await selectUser(client, target);
|
|
14580
14948
|
const conflictPolicy = hasFlag(flags, "overwrite") ? "overwrite" : "skip";
|
|
14949
|
+
const copyCodexAuth = hasFlag(flags, "copy-codex-auth");
|
|
14581
14950
|
const payload = await client.post("/api/users/sync", {
|
|
14582
14951
|
sourceUser: source,
|
|
14583
|
-
conflictPolicy
|
|
14952
|
+
conflictPolicy,
|
|
14953
|
+
migrateCodexLoginCache: copyCodexAuth
|
|
14584
14954
|
});
|
|
14585
14955
|
if (json === "json") {
|
|
14586
14956
|
printJson(payload);
|
|
14587
14957
|
} else {
|
|
14588
14958
|
console.log(`\u5DF2\u4ECE ${source} \u540C\u6B65\u5230 ${target}`);
|
|
14589
|
-
console.log(`\u65B0\u589E=${payload.imported} \u8986\u76D6=${payload.overwritten} \u8DF3\u8FC7=${payload.skipped} \u51ED\u636E=${payload.providersCopied}`);
|
|
14959
|
+
console.log(`\u65B0\u589E=${payload.imported} \u8986\u76D6=${payload.overwritten} \u8DF3\u8FC7=${payload.skipped} \u51ED\u636E=${payload.providersCopied} Codex\u767B\u5F55\u7F13\u5B58=${payload.codexLoginCacheMigrated ? "\u5DF2\u8FC1\u79FB" : "\u672A\u8FC1\u79FB"}`);
|
|
14590
14960
|
}
|
|
14591
14961
|
return 0;
|
|
14592
14962
|
}
|