@seaveyon/harness-switch 0.11.0 → 0.12.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/README.md +54 -0
- package/dist/harness-switch.js +1283 -179
- package/package.json +1 -1
- package/public/assets/main.11878989.css +2 -0
- package/public/assets/main.814a1870.js +1 -0
- package/public/index.html +1 -1
- package/public/assets/main.4d3b93cd.css +0 -2
- package/public/assets/main.b997c8aa.js +0 -1
package/dist/harness-switch.js
CHANGED
|
@@ -9203,7 +9203,8 @@ class EnvironmentService {
|
|
|
9203
9203
|
password: join2(this.dataDir, "web_password"),
|
|
9204
9204
|
key: join2(this.dataDir, "aes-256-gcm.key"),
|
|
9205
9205
|
env: join2(this.dataDir, "env.sh"),
|
|
9206
|
-
sessions: join2(this.dataDir, "sessions.json")
|
|
9206
|
+
sessions: join2(this.dataDir, "sessions.json"),
|
|
9207
|
+
vault: join2(this.dataDir, "vault.json")
|
|
9207
9208
|
};
|
|
9208
9209
|
backupsDir = join2(this.dataDir, "backups");
|
|
9209
9210
|
backupRetainCount = Math.max(1, Number(process.env.HSW_BACKUP_RETAIN || 10));
|
|
@@ -9766,8 +9767,105 @@ function createBackupRoutes(services) {
|
|
|
9766
9767
|
return app;
|
|
9767
9768
|
}
|
|
9768
9769
|
|
|
9769
|
-
// src/services/
|
|
9770
|
+
// src/services/doctor.ts
|
|
9771
|
+
import { accessSync, constants, statSync as statSync2 } from "fs";
|
|
9772
|
+
import { dirname as dirname3 } from "path";
|
|
9773
|
+
|
|
9774
|
+
// src/update.ts
|
|
9775
|
+
import { spawn } from "child_process";
|
|
9776
|
+
import { mkdirSync as mkdirSync3, openSync } from "fs";
|
|
9777
|
+
import { homedir as homedir2 } from "os";
|
|
9778
|
+
import { join as join5 } from "path";
|
|
9779
|
+
|
|
9780
|
+
// src/version.ts
|
|
9770
9781
|
import { join as join4 } from "path";
|
|
9782
|
+
var cached;
|
|
9783
|
+
async function packageInfo() {
|
|
9784
|
+
if (cached)
|
|
9785
|
+
return cached;
|
|
9786
|
+
try {
|
|
9787
|
+
const pkg = JSON.parse(await Bun.file(join4(import.meta.dir, "../package.json")).text());
|
|
9788
|
+
cached = {
|
|
9789
|
+
name: typeof pkg.name === "string" ? pkg.name : "harness-switch",
|
|
9790
|
+
version: typeof pkg.version === "string" ? pkg.version : "0.0.0"
|
|
9791
|
+
};
|
|
9792
|
+
} catch {
|
|
9793
|
+
cached = { name: "harness-switch", version: "0.0.0" };
|
|
9794
|
+
}
|
|
9795
|
+
return cached;
|
|
9796
|
+
}
|
|
9797
|
+
function serverVersion() {
|
|
9798
|
+
return packageInfo().then((info) => info.version);
|
|
9799
|
+
}
|
|
9800
|
+
function packageName() {
|
|
9801
|
+
return packageInfo().then((info) => info.name);
|
|
9802
|
+
}
|
|
9803
|
+
|
|
9804
|
+
// src/update.ts
|
|
9805
|
+
var REGISTRY_URL = "https://registry.npmjs.org/";
|
|
9806
|
+
var CHECK_TIMEOUT_MS = 5000;
|
|
9807
|
+
var CACHE_TTL_MS = 5 * 60000;
|
|
9808
|
+
var cached2;
|
|
9809
|
+
function compareVersions(a, b) {
|
|
9810
|
+
const pa = a.split(".").map(Number);
|
|
9811
|
+
const pb = b.split(".").map(Number);
|
|
9812
|
+
const len = Math.max(pa.length, pb.length);
|
|
9813
|
+
for (let i = 0;i < len; i++) {
|
|
9814
|
+
const da = pa[i] ?? 0;
|
|
9815
|
+
const db = pb[i] ?? 0;
|
|
9816
|
+
if (da !== db)
|
|
9817
|
+
return da > db ? 1 : -1;
|
|
9818
|
+
}
|
|
9819
|
+
return 0;
|
|
9820
|
+
}
|
|
9821
|
+
async function checkForUpdate(force = false) {
|
|
9822
|
+
const current = await serverVersion();
|
|
9823
|
+
if (!force && cached2 && Date.now() - cached2.at < CACHE_TTL_MS) {
|
|
9824
|
+
return {
|
|
9825
|
+
current,
|
|
9826
|
+
latest: cached2.latest,
|
|
9827
|
+
updateAvailable: cached2.latest !== null && compareVersions(cached2.latest, current) > 0
|
|
9828
|
+
};
|
|
9829
|
+
}
|
|
9830
|
+
try {
|
|
9831
|
+
const name = encodeURIComponent(await packageName());
|
|
9832
|
+
const response = await fetch(`${REGISTRY_URL}${name}/latest`, {
|
|
9833
|
+
signal: AbortSignal.timeout(CHECK_TIMEOUT_MS)
|
|
9834
|
+
});
|
|
9835
|
+
if (!response.ok) {
|
|
9836
|
+
throw new Error(`registry responded ${response.status}`);
|
|
9837
|
+
}
|
|
9838
|
+
const payload = await response.json();
|
|
9839
|
+
const latest = typeof payload.version === "string" ? payload.version : null;
|
|
9840
|
+
cached2 = { latest, at: Date.now() };
|
|
9841
|
+
return {
|
|
9842
|
+
current,
|
|
9843
|
+
latest,
|
|
9844
|
+
updateAvailable: latest !== null && compareVersions(latest, current) > 0
|
|
9845
|
+
};
|
|
9846
|
+
} catch {
|
|
9847
|
+
cached2 = { latest: null, at: Date.now() };
|
|
9848
|
+
return { current, latest: null, updateAvailable: false };
|
|
9849
|
+
}
|
|
9850
|
+
}
|
|
9851
|
+
async function triggerUpdate() {
|
|
9852
|
+
if (process.env.HSW_UPDATE_SPAWN === "0") {
|
|
9853
|
+
return;
|
|
9854
|
+
}
|
|
9855
|
+
const name = await packageName();
|
|
9856
|
+
const dataDir = process.env.HSW_DATA_DIR || join5(homedir2(), ".harness-switch");
|
|
9857
|
+
mkdirSync3(dataDir, { recursive: true, mode: 448 });
|
|
9858
|
+
const logFd = openSync(join5(dataDir, "update.log"), "w");
|
|
9859
|
+
const child = spawn(process.execPath, ["x", `${name}@latest`], {
|
|
9860
|
+
detached: true,
|
|
9861
|
+
stdio: ["ignore", logFd, logFd],
|
|
9862
|
+
env: process.env
|
|
9863
|
+
});
|
|
9864
|
+
child.unref();
|
|
9865
|
+
}
|
|
9866
|
+
|
|
9867
|
+
// src/services/adapters/claude.ts
|
|
9868
|
+
import { join as join6 } from "path";
|
|
9771
9869
|
|
|
9772
9870
|
// ../../node_modules/smol-toml/dist/date.js
|
|
9773
9871
|
/*!
|
|
@@ -11012,7 +11110,7 @@ API_TIMEOUT_MS=3000000`,
|
|
|
11012
11110
|
{
|
|
11013
11111
|
key: SETTINGS,
|
|
11014
11112
|
label: "settings.json",
|
|
11015
|
-
path:
|
|
11113
|
+
path: join6(this.environment.harnessHomes.claude, "settings.json"),
|
|
11016
11114
|
format: "json"
|
|
11017
11115
|
}
|
|
11018
11116
|
];
|
|
@@ -11126,7 +11224,7 @@ function parseEnvLines(raw2) {
|
|
|
11126
11224
|
}
|
|
11127
11225
|
|
|
11128
11226
|
// src/services/adapters/codex.ts
|
|
11129
|
-
import { join as
|
|
11227
|
+
import { join as join7 } from "path";
|
|
11130
11228
|
var CONFIG = "config";
|
|
11131
11229
|
var AUTH = "auth";
|
|
11132
11230
|
var DEFAULT_ENV_KEY = "OPENAI_API_KEY";
|
|
@@ -11193,13 +11291,13 @@ class CodexAdapter {
|
|
|
11193
11291
|
{
|
|
11194
11292
|
key: CONFIG,
|
|
11195
11293
|
label: "config.toml",
|
|
11196
|
-
path:
|
|
11294
|
+
path: join7(this.environment.harnessHomes.codex, "config.toml"),
|
|
11197
11295
|
format: "toml"
|
|
11198
11296
|
},
|
|
11199
11297
|
{
|
|
11200
11298
|
key: AUTH,
|
|
11201
11299
|
label: "auth.json\uFF08\u4EC5 auth.json \u6A21\u5F0F\uFF09",
|
|
11202
|
-
path:
|
|
11300
|
+
path: join7(this.environment.harnessHomes.codex, "auth.json"),
|
|
11203
11301
|
format: "json"
|
|
11204
11302
|
}
|
|
11205
11303
|
];
|
|
@@ -11299,7 +11397,7 @@ function safeJson(text) {
|
|
|
11299
11397
|
}
|
|
11300
11398
|
|
|
11301
11399
|
// src/services/adapters/dsh.ts
|
|
11302
|
-
import { join as
|
|
11400
|
+
import { join as join8 } from "path";
|
|
11303
11401
|
var SETTINGS2 = "settings";
|
|
11304
11402
|
var CREDENTIALS = "credentials";
|
|
11305
11403
|
var DEFAULT_CONTEXT = 262144;
|
|
@@ -11365,13 +11463,13 @@ class DshAdapter {
|
|
|
11365
11463
|
{
|
|
11366
11464
|
key: SETTINGS2,
|
|
11367
11465
|
label: "settings.yaml",
|
|
11368
|
-
path:
|
|
11466
|
+
path: join8(this.environment.harnessHomes.dsh, "settings.yaml"),
|
|
11369
11467
|
format: "yaml"
|
|
11370
11468
|
},
|
|
11371
11469
|
{
|
|
11372
11470
|
key: CREDENTIALS,
|
|
11373
11471
|
label: ".credentials.yaml",
|
|
11374
|
-
path:
|
|
11472
|
+
path: join8(this.environment.harnessHomes.dsh, ".credentials.yaml"),
|
|
11375
11473
|
format: "yaml"
|
|
11376
11474
|
}
|
|
11377
11475
|
];
|
|
@@ -11507,7 +11605,7 @@ function reasoningEffortsString(value, fallback) {
|
|
|
11507
11605
|
}
|
|
11508
11606
|
|
|
11509
11607
|
// src/services/adapters/kimi.ts
|
|
11510
|
-
import { join as
|
|
11608
|
+
import { join as join9 } from "path";
|
|
11511
11609
|
var CONFIG2 = "config";
|
|
11512
11610
|
var DEFAULT_CONTEXT2 = 262144;
|
|
11513
11611
|
|
|
@@ -11553,7 +11651,7 @@ class KimiAdapter {
|
|
|
11553
11651
|
{
|
|
11554
11652
|
key: CONFIG2,
|
|
11555
11653
|
label: "config.toml",
|
|
11556
|
-
path:
|
|
11654
|
+
path: join9(this.environment.harnessHomes.kimiCode, "config.toml"),
|
|
11557
11655
|
format: "toml"
|
|
11558
11656
|
}
|
|
11559
11657
|
];
|
|
@@ -11639,7 +11737,7 @@ class KimiAdapter {
|
|
|
11639
11737
|
}
|
|
11640
11738
|
|
|
11641
11739
|
// src/services/adapters/pi.ts
|
|
11642
|
-
import { join as
|
|
11740
|
+
import { join as join10 } from "path";
|
|
11643
11741
|
var MODELS = "models";
|
|
11644
11742
|
var SETTINGS3 = "settings";
|
|
11645
11743
|
var DEFAULT_CONTEXT3 = 200000;
|
|
@@ -11713,13 +11811,13 @@ class PiAdapter {
|
|
|
11713
11811
|
{
|
|
11714
11812
|
key: MODELS,
|
|
11715
11813
|
label: "models.json",
|
|
11716
|
-
path:
|
|
11814
|
+
path: join10(this.environment.harnessHomes.piAgent, "models.json"),
|
|
11717
11815
|
format: "json"
|
|
11718
11816
|
},
|
|
11719
11817
|
{
|
|
11720
11818
|
key: SETTINGS3,
|
|
11721
11819
|
label: "settings.json",
|
|
11722
|
-
path:
|
|
11820
|
+
path: join10(this.environment.harnessHomes.piAgent, "settings.json"),
|
|
11723
11821
|
format: "json"
|
|
11724
11822
|
}
|
|
11725
11823
|
];
|
|
@@ -11952,10 +12050,194 @@ class HarnessRegistry {
|
|
|
11952
12050
|
}
|
|
11953
12051
|
}
|
|
11954
12052
|
|
|
12053
|
+
// src/services/vault.ts
|
|
12054
|
+
var IVaultService = createDecorator("vaultService");
|
|
12055
|
+
var STORE_VERSION2 = 1;
|
|
12056
|
+
var _dec = [
|
|
12057
|
+
inject(IEnvironmentService, IFileService, ICryptoService)
|
|
12058
|
+
];
|
|
12059
|
+
var _init = __decoratorStart(undefined);
|
|
12060
|
+
|
|
12061
|
+
class VaultService {
|
|
12062
|
+
environment;
|
|
12063
|
+
files;
|
|
12064
|
+
crypto;
|
|
12065
|
+
constructor(environment, files, crypto2) {
|
|
12066
|
+
this.environment = environment;
|
|
12067
|
+
this.files = files;
|
|
12068
|
+
this.crypto = crypto2;
|
|
12069
|
+
}
|
|
12070
|
+
list() {
|
|
12071
|
+
return Object.values(this.read().entries).map((entry) => this.toPublic(entry));
|
|
12072
|
+
}
|
|
12073
|
+
get(id) {
|
|
12074
|
+
return this.toPublic(this.require(id));
|
|
12075
|
+
}
|
|
12076
|
+
create(input) {
|
|
12077
|
+
const name = String(input.name ?? "").trim();
|
|
12078
|
+
if (!name) {
|
|
12079
|
+
throw new HttpError(400, "name is required");
|
|
12080
|
+
}
|
|
12081
|
+
const apiKey = String(input.apiKey ?? "");
|
|
12082
|
+
if (!apiKey) {
|
|
12083
|
+
throw new HttpError(400, "apiKey is required");
|
|
12084
|
+
}
|
|
12085
|
+
const endpoints = this.validateEndpoints(input.endpoints);
|
|
12086
|
+
const id = this.nextId(name);
|
|
12087
|
+
const now = new Date().toISOString();
|
|
12088
|
+
const store = this.read();
|
|
12089
|
+
store.entries[id] = {
|
|
12090
|
+
id,
|
|
12091
|
+
name,
|
|
12092
|
+
api_key: this.crypto.encrypt(apiKey),
|
|
12093
|
+
notes: input.notes?.trim() || undefined,
|
|
12094
|
+
endpoints,
|
|
12095
|
+
created_at: now,
|
|
12096
|
+
updated_at: now
|
|
12097
|
+
};
|
|
12098
|
+
this.write(store);
|
|
12099
|
+
return this.toPublic(store.entries[id]);
|
|
12100
|
+
}
|
|
12101
|
+
update(id, input) {
|
|
12102
|
+
const store = this.read();
|
|
12103
|
+
const entry = store.entries[id];
|
|
12104
|
+
if (!entry) {
|
|
12105
|
+
throw new HttpError(404, "provider not found");
|
|
12106
|
+
}
|
|
12107
|
+
if (input.name !== undefined) {
|
|
12108
|
+
const name = String(input.name).trim();
|
|
12109
|
+
if (!name) {
|
|
12110
|
+
throw new HttpError(400, "name is required");
|
|
12111
|
+
}
|
|
12112
|
+
entry.name = name;
|
|
12113
|
+
}
|
|
12114
|
+
if (input.apiKey !== undefined) {
|
|
12115
|
+
const apiKey = String(input.apiKey).trim();
|
|
12116
|
+
if (apiKey) {
|
|
12117
|
+
entry.api_key = this.crypto.encrypt(apiKey);
|
|
12118
|
+
}
|
|
12119
|
+
}
|
|
12120
|
+
if (input.notes !== undefined) {
|
|
12121
|
+
entry.notes = input.notes.trim() || undefined;
|
|
12122
|
+
}
|
|
12123
|
+
if (input.endpoints !== undefined) {
|
|
12124
|
+
entry.endpoints = this.validateEndpoints(input.endpoints);
|
|
12125
|
+
}
|
|
12126
|
+
entry.updated_at = new Date().toISOString();
|
|
12127
|
+
this.write(store);
|
|
12128
|
+
const affected = this.references(id);
|
|
12129
|
+
return { provider: this.toPublic(entry), affected };
|
|
12130
|
+
}
|
|
12131
|
+
remove(id) {
|
|
12132
|
+
const store = this.read();
|
|
12133
|
+
if (!store.entries[id]) {
|
|
12134
|
+
throw new HttpError(404, "provider not found");
|
|
12135
|
+
}
|
|
12136
|
+
const references = this.references(id);
|
|
12137
|
+
if (references.length > 0) {
|
|
12138
|
+
throw new HttpError(409, `Provider \u6B63\u88AB ${references.length} \u4E2A\u914D\u7F6E\u5F15\u7528\uFF0C\u8BF7\u5148\u79FB\u9664\u8FD9\u4E9B\u5F15\u7528\u518D\u5220\u9664`);
|
|
12139
|
+
}
|
|
12140
|
+
delete store.entries[id];
|
|
12141
|
+
this.write(store);
|
|
12142
|
+
}
|
|
12143
|
+
decrypt(id) {
|
|
12144
|
+
const entry = this.require(id);
|
|
12145
|
+
const plain = this.crypto.decrypt(entry.api_key);
|
|
12146
|
+
if (!plain) {
|
|
12147
|
+
throw new HttpError(500, `provider ${id} \u7684\u51ED\u636E\u65E0\u6CD5\u89E3\u5BC6`);
|
|
12148
|
+
}
|
|
12149
|
+
return plain;
|
|
12150
|
+
}
|
|
12151
|
+
references(id) {
|
|
12152
|
+
const store = this.readProfileStore();
|
|
12153
|
+
const found = [];
|
|
12154
|
+
for (const [harness, profiles] of Object.entries(store)) {
|
|
12155
|
+
for (const [name, profile] of Object.entries(profiles)) {
|
|
12156
|
+
if (profile.provider_id === id) {
|
|
12157
|
+
found.push({ harness, name });
|
|
12158
|
+
}
|
|
12159
|
+
}
|
|
12160
|
+
}
|
|
12161
|
+
return found;
|
|
12162
|
+
}
|
|
12163
|
+
require(id) {
|
|
12164
|
+
const entry = this.read().entries[id];
|
|
12165
|
+
if (!entry) {
|
|
12166
|
+
throw new HttpError(404, "provider not found");
|
|
12167
|
+
}
|
|
12168
|
+
return entry;
|
|
12169
|
+
}
|
|
12170
|
+
read() {
|
|
12171
|
+
return this.files.readJsonStrict(this.environment.files.vault, {
|
|
12172
|
+
version: STORE_VERSION2,
|
|
12173
|
+
entries: {}
|
|
12174
|
+
});
|
|
12175
|
+
}
|
|
12176
|
+
readProfileStore() {
|
|
12177
|
+
return this.files.readJsonStrict(this.environment.files.profiles, {});
|
|
12178
|
+
}
|
|
12179
|
+
write(store) {
|
|
12180
|
+
this.files.writeJson(this.environment.files.vault, store);
|
|
12181
|
+
}
|
|
12182
|
+
toPublic(entry) {
|
|
12183
|
+
return {
|
|
12184
|
+
id: entry.id,
|
|
12185
|
+
name: entry.name,
|
|
12186
|
+
notes: entry.notes,
|
|
12187
|
+
endpoints: entry.endpoints,
|
|
12188
|
+
apiKeyConfigured: true,
|
|
12189
|
+
updatedAt: entry.updated_at
|
|
12190
|
+
};
|
|
12191
|
+
}
|
|
12192
|
+
validateEndpoints(endpoints) {
|
|
12193
|
+
if (endpoints === undefined) {
|
|
12194
|
+
return [];
|
|
12195
|
+
}
|
|
12196
|
+
if (!Array.isArray(endpoints)) {
|
|
12197
|
+
throw new HttpError(400, "endpoints must be an array");
|
|
12198
|
+
}
|
|
12199
|
+
const seen = new Set;
|
|
12200
|
+
return endpoints.map((endpoint) => {
|
|
12201
|
+
const key = String(endpoint?.key ?? "").trim();
|
|
12202
|
+
if (!key) {
|
|
12203
|
+
throw new HttpError(400, "endpoint key is required");
|
|
12204
|
+
}
|
|
12205
|
+
if (key.includes("/") || key.includes("\\") || key.length > 60) {
|
|
12206
|
+
throw new HttpError(400, "endpoint key contains invalid characters");
|
|
12207
|
+
}
|
|
12208
|
+
if (seen.has(key)) {
|
|
12209
|
+
throw new HttpError(400, `duplicate endpoint ${key}`);
|
|
12210
|
+
}
|
|
12211
|
+
seen.add(key);
|
|
12212
|
+
const baseUrl = String(endpoint.baseUrl ?? "").trim();
|
|
12213
|
+
if (!baseUrl) {
|
|
12214
|
+
throw new HttpError(400, `endpoint ${key} requires a baseUrl`);
|
|
12215
|
+
}
|
|
12216
|
+
return { key, label: endpoint.label?.trim() || key, baseUrl };
|
|
12217
|
+
});
|
|
12218
|
+
}
|
|
12219
|
+
nextId(name) {
|
|
12220
|
+
const base = name.toLowerCase().replace(/[^a-z0-9_-]+/g, "-").replace(/^-+|-+$/g, "").slice(0, 40);
|
|
12221
|
+
const slug = base || `provider-${Date.now().toString(36)}`;
|
|
12222
|
+
const store = this.read();
|
|
12223
|
+
let candidate = slug;
|
|
12224
|
+
let suffix = 1;
|
|
12225
|
+
while (store.entries[candidate]) {
|
|
12226
|
+
candidate = `${slug}-${suffix}`;
|
|
12227
|
+
suffix += 1;
|
|
12228
|
+
}
|
|
12229
|
+
return candidate;
|
|
12230
|
+
}
|
|
12231
|
+
}
|
|
12232
|
+
VaultService = __decorateElement(_init, 0, "VaultService", _dec, VaultService);
|
|
12233
|
+
__runInitializers(_init, 1, VaultService);
|
|
12234
|
+
__decoratorMetadata(_init, VaultService);
|
|
12235
|
+
let _VaultService = VaultService;
|
|
12236
|
+
|
|
11955
12237
|
// src/services/profiles.ts
|
|
11956
12238
|
var IProfileService = createDecorator("profileService");
|
|
11957
12239
|
var _dec = [
|
|
11958
|
-
inject(IEnvironmentService, IFileService, ICryptoService, IHarnessRegistry, IAdapterRegistry)
|
|
12240
|
+
inject(IEnvironmentService, IFileService, ICryptoService, IHarnessRegistry, IAdapterRegistry, IVaultService, ILogService)
|
|
11959
12241
|
];
|
|
11960
12242
|
var _init = __decoratorStart(undefined);
|
|
11961
12243
|
|
|
@@ -11965,12 +12247,16 @@ class ProfileService {
|
|
|
11965
12247
|
crypto;
|
|
11966
12248
|
harnesses;
|
|
11967
12249
|
adapters;
|
|
11968
|
-
|
|
12250
|
+
vault;
|
|
12251
|
+
log;
|
|
12252
|
+
constructor(environment, files, crypto2, harnesses, adapters, vault, log) {
|
|
11969
12253
|
this.environment = environment;
|
|
11970
12254
|
this.files = files;
|
|
11971
12255
|
this.crypto = crypto2;
|
|
11972
12256
|
this.harnesses = harnesses;
|
|
11973
12257
|
this.adapters = adapters;
|
|
12258
|
+
this.vault = vault;
|
|
12259
|
+
this.log = log;
|
|
11974
12260
|
}
|
|
11975
12261
|
list(harness) {
|
|
11976
12262
|
const store = this.read();
|
|
@@ -11998,17 +12284,51 @@ class ProfileService {
|
|
|
11998
12284
|
if (!isCreate && sourceName !== name && store[harness][name]) {
|
|
11999
12285
|
throw new HttpError(409, "profile already exists");
|
|
12000
12286
|
}
|
|
12001
|
-
const
|
|
12287
|
+
const hasProvider = input.providerId !== undefined;
|
|
12288
|
+
const providerId = input.providerId?.trim() || undefined;
|
|
12289
|
+
const providerEndpoint = input.providerEndpoint?.trim() || undefined;
|
|
12290
|
+
const hasInlineKey = input.apiKey !== undefined && input.apiKey.trim() !== "";
|
|
12291
|
+
const inlineKey = (input.apiKey ?? "").trim();
|
|
12292
|
+
let nextProviderId = prior?.provider_id;
|
|
12293
|
+
let nextProviderEndpoint = prior?.provider_endpoint;
|
|
12294
|
+
let apiKey = "";
|
|
12295
|
+
if (hasProvider && providerId) {
|
|
12296
|
+
const entry = this.vault.get(providerId);
|
|
12297
|
+
if (providerEndpoint) {
|
|
12298
|
+
const endpoint = entry.endpoints.find((candidate) => candidate.key === providerEndpoint);
|
|
12299
|
+
if (!endpoint) {
|
|
12300
|
+
throw new HttpError(400, `endpoint ${providerEndpoint} not found`);
|
|
12301
|
+
}
|
|
12302
|
+
nextProviderEndpoint = providerEndpoint;
|
|
12303
|
+
} else {
|
|
12304
|
+
nextProviderEndpoint = undefined;
|
|
12305
|
+
}
|
|
12306
|
+
nextProviderId = providerId;
|
|
12307
|
+
apiKey = this.vault.decrypt(providerId);
|
|
12308
|
+
} else if (hasProvider && !providerId) {
|
|
12309
|
+
nextProviderId = undefined;
|
|
12310
|
+
nextProviderEndpoint = undefined;
|
|
12311
|
+
apiKey = hasInlineKey ? inlineKey : prior ? this.crypto.decrypt(prior.api_key) : "";
|
|
12312
|
+
} else if (hasInlineKey) {
|
|
12313
|
+
nextProviderId = undefined;
|
|
12314
|
+
nextProviderEndpoint = undefined;
|
|
12315
|
+
apiKey = inlineKey;
|
|
12316
|
+
} else if (prior) {
|
|
12317
|
+
apiKey = prior.provider_id ? this.vault.decrypt(prior.provider_id) : this.crypto.decrypt(prior.api_key);
|
|
12318
|
+
}
|
|
12002
12319
|
if (isCreate && !apiKey) {
|
|
12003
12320
|
throw new HttpError(400, "apiKey is required");
|
|
12004
12321
|
}
|
|
12322
|
+
const baseUrl = this.resolveBaseUrl((input.baseUrl ?? prior?.base_url ?? "").trim(), providerId || nextProviderId, providerEndpoint || nextProviderEndpoint);
|
|
12005
12323
|
const next = {
|
|
12006
|
-
base_url:
|
|
12324
|
+
base_url: baseUrl,
|
|
12007
12325
|
api_key: this.crypto.encrypt(apiKey),
|
|
12008
12326
|
model: (input.model ?? prior?.model ?? "").trim(),
|
|
12009
12327
|
notes: input.notes ?? prior?.notes ?? "",
|
|
12010
12328
|
extras: input.extras ?? prior?.extras ?? {},
|
|
12011
12329
|
overrides: input.overrides ?? prior?.overrides ?? {},
|
|
12330
|
+
provider_id: nextProviderId,
|
|
12331
|
+
provider_endpoint: nextProviderEndpoint,
|
|
12012
12332
|
updated_at: new Date().toISOString()
|
|
12013
12333
|
};
|
|
12014
12334
|
this.adapters.get(harness).validate?.({
|
|
@@ -12040,8 +12360,8 @@ class ProfileService {
|
|
|
12040
12360
|
}
|
|
12041
12361
|
return {
|
|
12042
12362
|
name,
|
|
12043
|
-
baseUrl: stored.base_url || "",
|
|
12044
|
-
apiKey: this.
|
|
12363
|
+
baseUrl: this.resolveBaseUrl(stored.base_url || "", stored.provider_id, stored.provider_endpoint),
|
|
12364
|
+
apiKey: this.resolveKey(stored),
|
|
12045
12365
|
model: stored.model || "",
|
|
12046
12366
|
extras: stored.extras ?? {},
|
|
12047
12367
|
overrides: stored.overrides ?? {}
|
|
@@ -12059,7 +12379,7 @@ class ProfileService {
|
|
|
12059
12379
|
if (values.model !== undefined) {
|
|
12060
12380
|
stored.model = values.model;
|
|
12061
12381
|
}
|
|
12062
|
-
if (values.apiKey) {
|
|
12382
|
+
if (values.apiKey && !stored.provider_id) {
|
|
12063
12383
|
stored.api_key = this.crypto.encrypt(values.apiKey);
|
|
12064
12384
|
}
|
|
12065
12385
|
if (values.extras !== undefined) {
|
|
@@ -12067,6 +12387,27 @@ class ProfileService {
|
|
|
12067
12387
|
}
|
|
12068
12388
|
this.files.writeJson(this.environment.files.profiles, store);
|
|
12069
12389
|
}
|
|
12390
|
+
sweepVaultCache(vaultId, apiKey, endpoints) {
|
|
12391
|
+
const store = this.read();
|
|
12392
|
+
let updated = 0;
|
|
12393
|
+
for (const profiles of Object.values(store)) {
|
|
12394
|
+
for (const stored of Object.values(profiles)) {
|
|
12395
|
+
if (stored.provider_id !== vaultId) {
|
|
12396
|
+
continue;
|
|
12397
|
+
}
|
|
12398
|
+
stored.api_key = this.crypto.encrypt(apiKey);
|
|
12399
|
+
const endpoint = stored.provider_endpoint !== undefined ? endpoints.find((candidate) => candidate.key === stored.provider_endpoint) : undefined;
|
|
12400
|
+
if (endpoint) {
|
|
12401
|
+
stored.base_url = endpoint.baseUrl;
|
|
12402
|
+
}
|
|
12403
|
+
updated++;
|
|
12404
|
+
}
|
|
12405
|
+
}
|
|
12406
|
+
if (updated > 0) {
|
|
12407
|
+
this.files.writeJson(this.environment.files.profiles, store);
|
|
12408
|
+
}
|
|
12409
|
+
return updated;
|
|
12410
|
+
}
|
|
12070
12411
|
read() {
|
|
12071
12412
|
return this.files.readJsonStrict(this.environment.files.profiles, {});
|
|
12072
12413
|
}
|
|
@@ -12079,9 +12420,43 @@ class ProfileService {
|
|
|
12079
12420
|
notes: profile.notes || "",
|
|
12080
12421
|
extras: profile.extras ?? {},
|
|
12081
12422
|
overriddenTargets: Object.keys(profile.overrides ?? {}),
|
|
12423
|
+
providerId: profile.provider_id,
|
|
12424
|
+
providerEndpoint: profile.provider_endpoint,
|
|
12082
12425
|
updatedAt: profile.updated_at || ""
|
|
12083
12426
|
};
|
|
12084
12427
|
}
|
|
12428
|
+
resolveKey(stored) {
|
|
12429
|
+
if (!stored.provider_id) {
|
|
12430
|
+
return this.crypto.decrypt(stored.api_key);
|
|
12431
|
+
}
|
|
12432
|
+
try {
|
|
12433
|
+
return this.vault.decrypt(stored.provider_id);
|
|
12434
|
+
} catch {
|
|
12435
|
+
this.log.warn(`profile ${stored.provider_id} \u5F15\u7528\u7684 provider \u4E0D\u5B58\u5728\uFF0C\u56DE\u9000\u5230\u5185\u8054\u7F13\u5B58\u51ED\u636E`);
|
|
12436
|
+
return this.crypto.decrypt(stored.api_key);
|
|
12437
|
+
}
|
|
12438
|
+
}
|
|
12439
|
+
resolveBaseUrl(fallback, providerId, providerEndpoint) {
|
|
12440
|
+
if (!providerId || !providerEndpoint) {
|
|
12441
|
+
return fallback;
|
|
12442
|
+
}
|
|
12443
|
+
let entry;
|
|
12444
|
+
try {
|
|
12445
|
+
entry = this.vault.get(providerId);
|
|
12446
|
+
} catch {
|
|
12447
|
+
this.log.warn(`profile \u5F15\u7528\u7684 provider ${providerId} \u4E0D\u5B58\u5728\uFF0C\u56DE\u9000\u5230\u5185\u8054 base_url`);
|
|
12448
|
+
return fallback;
|
|
12449
|
+
}
|
|
12450
|
+
const endpoint = entry.endpoints.find((candidate) => candidate.key === providerEndpoint);
|
|
12451
|
+
if (endpoint) {
|
|
12452
|
+
return endpoint.baseUrl;
|
|
12453
|
+
}
|
|
12454
|
+
if (entry.endpoints.length > 0) {
|
|
12455
|
+
this.log.warn(`endpoint ${providerEndpoint} \u5DF2\u4E0D\u5B58\u5728\uFF0C\u56DE\u9000\u5230 provider ${providerId} \u7684\u9996\u4E2A endpoint`);
|
|
12456
|
+
return entry.endpoints[0].baseUrl;
|
|
12457
|
+
}
|
|
12458
|
+
return fallback;
|
|
12459
|
+
}
|
|
12085
12460
|
assertName(name) {
|
|
12086
12461
|
if (!name) {
|
|
12087
12462
|
throw new HttpError(400, "name is required");
|
|
@@ -12142,7 +12517,7 @@ class ActivationService {
|
|
|
12142
12517
|
const warnings = [];
|
|
12143
12518
|
const active = this.read();
|
|
12144
12519
|
this.backfillPrevious(adapter, active[harness], name, warnings);
|
|
12145
|
-
const writes = this.
|
|
12520
|
+
const writes = this.expectedNamedWrites(harness, name);
|
|
12146
12521
|
this.liveWrite.apply(harness, name, writes);
|
|
12147
12522
|
active[harness] = {
|
|
12148
12523
|
name: profile.name,
|
|
@@ -12173,16 +12548,8 @@ class ActivationService {
|
|
|
12173
12548
|
if (!alreadyOfficial) {
|
|
12174
12549
|
this.backfillPrevious(adapter, previous, "__official__", warnings);
|
|
12175
12550
|
}
|
|
12176
|
-
const
|
|
12177
|
-
|
|
12178
|
-
baseUrl: previous.base_url,
|
|
12179
|
-
apiKey: previous.api_key,
|
|
12180
|
-
model: previous.model,
|
|
12181
|
-
extras: previous.extras ?? {}
|
|
12182
|
-
} : undefined;
|
|
12183
|
-
const targets = adapter.targets();
|
|
12184
|
-
const rendered = adapter.renderOfficial(profile, this.readCurrent(targets));
|
|
12185
|
-
this.liveWrite.apply(harness, "\u5B98\u65B9\u767B\u5F55", this.toWrites(targets, rendered));
|
|
12551
|
+
const writes = this.expectedOfficialWrites(harness);
|
|
12552
|
+
this.liveWrite.apply(harness, "\u5B98\u65B9\u767B\u5F55", writes);
|
|
12186
12553
|
active[harness] = {
|
|
12187
12554
|
name: "\u5B98\u65B9\u767B\u5F55",
|
|
12188
12555
|
base_url: "",
|
|
@@ -12266,7 +12633,19 @@ class ActivationService {
|
|
|
12266
12633
|
warnings.push(`\u672A\u80FD\u628A ${previous.name} \u7684\u73B0\u6709\u914D\u7F6E\u56DE\u586B\u4FDD\u5B58`);
|
|
12267
12634
|
}
|
|
12268
12635
|
}
|
|
12269
|
-
|
|
12636
|
+
expectedWrites(harness) {
|
|
12637
|
+
const active = this.read()[harness];
|
|
12638
|
+
if (!active) {
|
|
12639
|
+
throw new HttpError(400, "\u8BE5\u5DE5\u5177\u672A\u6FC0\u6D3B\u4EFB\u4F55\u914D\u7F6E\uFF0C\u65E0\u53EF\u6E32\u67D3\u7684\u5185\u5BB9");
|
|
12640
|
+
}
|
|
12641
|
+
if (active.official === true) {
|
|
12642
|
+
return this.expectedOfficialWrites(harness);
|
|
12643
|
+
}
|
|
12644
|
+
return this.expectedNamedWrites(harness, active.name);
|
|
12645
|
+
}
|
|
12646
|
+
expectedNamedWrites(harness, name) {
|
|
12647
|
+
const adapter = this.adapters.get(harness);
|
|
12648
|
+
const profile = this.profiles.decrypt(harness, name);
|
|
12270
12649
|
const targets = adapter.targets();
|
|
12271
12650
|
const rendered = adapter.render(profile, this.readCurrent(targets));
|
|
12272
12651
|
for (const [key, content] of Object.entries(profile.overrides)) {
|
|
@@ -12276,6 +12655,23 @@ class ActivationService {
|
|
|
12276
12655
|
}
|
|
12277
12656
|
return this.toWrites(targets, rendered);
|
|
12278
12657
|
}
|
|
12658
|
+
expectedOfficialWrites(harness) {
|
|
12659
|
+
const adapter = this.adapters.get(harness);
|
|
12660
|
+
if (!adapter.renderOfficial) {
|
|
12661
|
+
throw new HttpError(400, `${this.harnesses.label(harness)} \u4E0D\u652F\u6301\u5B98\u65B9\u8D26\u53F7\u767B\u5F55\u6A21\u5F0F`);
|
|
12662
|
+
}
|
|
12663
|
+
const active = this.read()[harness];
|
|
12664
|
+
const profile = active && active.official !== true ? {
|
|
12665
|
+
name: active.name,
|
|
12666
|
+
baseUrl: active.base_url,
|
|
12667
|
+
apiKey: active.api_key,
|
|
12668
|
+
model: active.model,
|
|
12669
|
+
extras: active.extras ?? {}
|
|
12670
|
+
} : undefined;
|
|
12671
|
+
const targets = adapter.targets();
|
|
12672
|
+
const rendered = adapter.renderOfficial(profile, this.readCurrent(targets));
|
|
12673
|
+
return this.toWrites(targets, rendered);
|
|
12674
|
+
}
|
|
12279
12675
|
toWrites(targets, rendered) {
|
|
12280
12676
|
return Object.entries(rendered).map(([key, content]) => {
|
|
12281
12677
|
const target = this.requireTarget(targets, key);
|
|
@@ -12348,47 +12744,437 @@ function shellQuote(value) {
|
|
|
12348
12744
|
return `'${String(value || "").replace(/'/g, `'"'"'`)}'`;
|
|
12349
12745
|
}
|
|
12350
12746
|
|
|
12351
|
-
// src/
|
|
12352
|
-
|
|
12353
|
-
|
|
12354
|
-
|
|
12355
|
-
|
|
12356
|
-
|
|
12357
|
-
|
|
12358
|
-
|
|
12359
|
-
|
|
12360
|
-
|
|
12361
|
-
|
|
12362
|
-
|
|
12363
|
-
|
|
12364
|
-
|
|
12365
|
-
|
|
12366
|
-
|
|
12367
|
-
|
|
12368
|
-
|
|
12369
|
-
|
|
12370
|
-
|
|
12371
|
-
|
|
12372
|
-
|
|
12373
|
-
|
|
12374
|
-
|
|
12375
|
-
|
|
12376
|
-
|
|
12377
|
-
|
|
12378
|
-
|
|
12379
|
-
}
|
|
12380
|
-
|
|
12381
|
-
|
|
12382
|
-
|
|
12383
|
-
|
|
12384
|
-
const
|
|
12385
|
-
|
|
12747
|
+
// src/services/drift.ts
|
|
12748
|
+
var IDriftService = createDecorator("driftService");
|
|
12749
|
+
var SEVERITY = {
|
|
12750
|
+
unknown: 0,
|
|
12751
|
+
"in-sync": 1,
|
|
12752
|
+
drifted: 2,
|
|
12753
|
+
missing: 3,
|
|
12754
|
+
invalid: 4
|
|
12755
|
+
};
|
|
12756
|
+
var _dec = [
|
|
12757
|
+
inject(IActivationService, IAdapterRegistry, IProfileService, IFileService, ILiveWriteService, IHarnessRegistry)
|
|
12758
|
+
];
|
|
12759
|
+
var _init = __decoratorStart(undefined);
|
|
12760
|
+
|
|
12761
|
+
class DriftService {
|
|
12762
|
+
activation;
|
|
12763
|
+
adapters;
|
|
12764
|
+
profiles;
|
|
12765
|
+
files;
|
|
12766
|
+
liveWrite;
|
|
12767
|
+
harnesses;
|
|
12768
|
+
constructor(activation, adapters, profiles, files, liveWrite, harnesses) {
|
|
12769
|
+
this.activation = activation;
|
|
12770
|
+
this.adapters = adapters;
|
|
12771
|
+
this.profiles = profiles;
|
|
12772
|
+
this.files = files;
|
|
12773
|
+
this.liveWrite = liveWrite;
|
|
12774
|
+
this.harnesses = harnesses;
|
|
12775
|
+
}
|
|
12776
|
+
inspectAll() {
|
|
12777
|
+
return this.harnesses.list().map((item) => this.inspect(item.id));
|
|
12778
|
+
}
|
|
12779
|
+
inspect(harness) {
|
|
12780
|
+
const active = this.activation.getActive(harness);
|
|
12781
|
+
if (!active) {
|
|
12782
|
+
return { harness, status: "unknown", active: false, files: [] };
|
|
12783
|
+
}
|
|
12784
|
+
const files = active.official === true ? this.officialFiles(harness) : this.namedFiles(harness);
|
|
12785
|
+
return { harness, status: this.overall(files), active: true, files };
|
|
12786
|
+
}
|
|
12787
|
+
reapply(harness) {
|
|
12788
|
+
const active = this.activation.getActive(harness);
|
|
12789
|
+
if (!active) {
|
|
12790
|
+
throw new HttpError(400, "\u8BE5\u5DE5\u5177\u672A\u6FC0\u6D3B\u4EFB\u4F55\u914D\u7F6E\uFF0C\u65E0\u53EF\u91CD\u5E94\u7528\u7684\u5185\u5BB9");
|
|
12791
|
+
}
|
|
12792
|
+
const writes = this.activation.expectedWrites(harness);
|
|
12793
|
+
this.liveWrite.apply(harness, active.official === true ? "\u5B98\u65B9\u767B\u5F55" : active.name, writes);
|
|
12794
|
+
return { ok: true, files: this.inspect(harness).files };
|
|
12795
|
+
}
|
|
12796
|
+
adopt(harness) {
|
|
12797
|
+
const active = this.activation.getActive(harness);
|
|
12798
|
+
if (!active) {
|
|
12799
|
+
throw new HttpError(400, "\u8BE5\u5DE5\u5177\u672A\u6FC0\u6D3B\u4EFB\u4F55\u914D\u7F6E\uFF0C\u65E0\u53EF\u91C7\u7EB3\u7684\u5185\u5BB9");
|
|
12800
|
+
}
|
|
12801
|
+
if (active.official === true) {
|
|
12802
|
+
throw new HttpError(400, "\u5B98\u65B9\u767B\u5F55\u6A21\u5F0F\u4E0B\u6CA1\u6709\u53EF\u91C7\u7EB3\u7684\u914D\u7F6E");
|
|
12803
|
+
}
|
|
12804
|
+
const adapter = this.adapters.get(harness);
|
|
12805
|
+
if (!adapter.backfill) {
|
|
12806
|
+
throw new HttpError(400, "\u8BE5\u5DE5\u5177\u4E0D\u652F\u6301\u4ECE live \u6587\u4EF6\u56DE\u8BFB\u914D\u7F6E");
|
|
12807
|
+
}
|
|
12808
|
+
const profile = this.profiles.decrypt(harness, active.name);
|
|
12809
|
+
if (Object.keys(profile.overrides).length > 0) {
|
|
12810
|
+
throw new HttpError(409, "\u5B58\u5728\u624B\u52A8 override \u7684\u6587\u4EF6\uFF0C\u81EA\u52A8\u91C7\u7EB3\u4F1A\u4E22\u5F03\u4F60\u7684\u624B\u5DE5\u914D\u7F6E\uFF1B\u8BF7\u5148\u6062\u590D\u4E3A\u81EA\u52A8\u751F\u6210\u518D\u91C7\u7EB3");
|
|
12811
|
+
}
|
|
12812
|
+
const targets = adapter.targets();
|
|
12813
|
+
const current = this.readCurrent(targets);
|
|
12814
|
+
for (const target of targets) {
|
|
12815
|
+
const content = current[target.key];
|
|
12816
|
+
if (content !== undefined) {
|
|
12817
|
+
assertParsable(target.format, target.path, content);
|
|
12818
|
+
}
|
|
12819
|
+
}
|
|
12820
|
+
const values = adapter.backfill(profile, current);
|
|
12821
|
+
this.profiles.applyBackfill(harness, active.name, values);
|
|
12822
|
+
return { ok: true, summary: this.inspect(harness), warnings: [] };
|
|
12823
|
+
}
|
|
12824
|
+
namedFiles(harness) {
|
|
12825
|
+
const targets = this.adapters.get(harness).targets();
|
|
12826
|
+
let preview;
|
|
12827
|
+
try {
|
|
12828
|
+
preview = this.activation.preview(harness, this.activation.getActive(harness).name);
|
|
12829
|
+
} catch {
|
|
12830
|
+
return targets.map((target) => ({
|
|
12831
|
+
key: target.key,
|
|
12832
|
+
label: target.label,
|
|
12833
|
+
path: target.path,
|
|
12834
|
+
format: target.format,
|
|
12835
|
+
expectedContent: null,
|
|
12836
|
+
currentContent: this.files.readOptional(target.path) ?? null,
|
|
12837
|
+
status: "invalid"
|
|
12838
|
+
}));
|
|
12839
|
+
}
|
|
12840
|
+
return preview.map((target) => this.classify({ key: target.key, label: target.label, path: target.path, format: target.format }, target.content, target.currentContent ?? null));
|
|
12841
|
+
}
|
|
12842
|
+
officialFiles(harness) {
|
|
12843
|
+
const targets = this.adapters.get(harness).targets();
|
|
12844
|
+
return targets.map((target) => {
|
|
12845
|
+
const live = this.files.readOptional(target.path) ?? null;
|
|
12846
|
+
const base = {
|
|
12847
|
+
key: target.key,
|
|
12848
|
+
label: target.label,
|
|
12849
|
+
path: target.path,
|
|
12850
|
+
format: target.format,
|
|
12851
|
+
expectedContent: null,
|
|
12852
|
+
currentContent: live
|
|
12853
|
+
};
|
|
12854
|
+
if (live === null) {
|
|
12855
|
+
return { ...base, status: "missing" };
|
|
12856
|
+
}
|
|
12857
|
+
if (target.format === "text") {
|
|
12858
|
+
return { ...base, status: "unknown" };
|
|
12859
|
+
}
|
|
12860
|
+
try {
|
|
12861
|
+
parseValue2(target.format, live);
|
|
12862
|
+
return { ...base, status: "in-sync" };
|
|
12863
|
+
} catch {
|
|
12864
|
+
return { ...base, status: "invalid" };
|
|
12865
|
+
}
|
|
12866
|
+
});
|
|
12867
|
+
}
|
|
12868
|
+
classify(target, expected, live) {
|
|
12869
|
+
const base = {
|
|
12870
|
+
key: target.key,
|
|
12871
|
+
label: target.label,
|
|
12872
|
+
path: target.path,
|
|
12873
|
+
format: target.format,
|
|
12874
|
+
expectedContent: expected,
|
|
12875
|
+
currentContent: live
|
|
12876
|
+
};
|
|
12877
|
+
if (live === null) {
|
|
12878
|
+
return { ...base, status: "missing" };
|
|
12879
|
+
}
|
|
12880
|
+
try {
|
|
12881
|
+
const equal = semanticEqual(target.format, expected, live);
|
|
12882
|
+
return { ...base, status: equal ? "in-sync" : "drifted" };
|
|
12883
|
+
} catch {
|
|
12884
|
+
return { ...base, status: "invalid" };
|
|
12885
|
+
}
|
|
12886
|
+
}
|
|
12887
|
+
overall(files) {
|
|
12888
|
+
let worst = files.length === 0 ? "in-sync" : "unknown";
|
|
12889
|
+
for (const file of files) {
|
|
12890
|
+
if (SEVERITY[file.status] > SEVERITY[worst]) {
|
|
12891
|
+
worst = file.status;
|
|
12892
|
+
}
|
|
12893
|
+
}
|
|
12894
|
+
return worst;
|
|
12895
|
+
}
|
|
12896
|
+
readCurrent(targets) {
|
|
12897
|
+
const current = {};
|
|
12898
|
+
for (const target of targets) {
|
|
12899
|
+
current[target.key] = this.files.readOptional(target.path);
|
|
12900
|
+
}
|
|
12901
|
+
return current;
|
|
12902
|
+
}
|
|
12903
|
+
}
|
|
12904
|
+
DriftService = __decorateElement(_init, 0, "DriftService", _dec, DriftService);
|
|
12905
|
+
__runInitializers(_init, 1, DriftService);
|
|
12906
|
+
__decoratorMetadata(_init, DriftService);
|
|
12907
|
+
let _DriftService = DriftService;
|
|
12908
|
+
function semanticEqual(format, expected, live) {
|
|
12909
|
+
if (format === "text") {
|
|
12910
|
+
return expected === live;
|
|
12911
|
+
}
|
|
12912
|
+
return deepEqual(parseValue2(format, expected), parseValue2(format, live));
|
|
12913
|
+
}
|
|
12914
|
+
function parseValue2(format, text) {
|
|
12915
|
+
if (!text?.trim()) {
|
|
12916
|
+
return {};
|
|
12917
|
+
}
|
|
12918
|
+
if (format === "json") {
|
|
12919
|
+
return JSON.parse(text);
|
|
12920
|
+
}
|
|
12921
|
+
if (format === "toml") {
|
|
12922
|
+
return parse2(text);
|
|
12923
|
+
}
|
|
12924
|
+
if (format === "yaml") {
|
|
12925
|
+
const document = $parseDocument(text);
|
|
12926
|
+
if (document.errors.length > 0) {
|
|
12927
|
+
throw document.errors[0];
|
|
12928
|
+
}
|
|
12929
|
+
return document.toJS() ?? {};
|
|
12930
|
+
}
|
|
12931
|
+
return text;
|
|
12932
|
+
}
|
|
12933
|
+
function deepEqual(left, right) {
|
|
12934
|
+
if (left === right) {
|
|
12935
|
+
return true;
|
|
12936
|
+
}
|
|
12937
|
+
if (typeof left !== typeof right || left === null || right === null) {
|
|
12938
|
+
return false;
|
|
12939
|
+
}
|
|
12940
|
+
if (Array.isArray(left) || Array.isArray(right)) {
|
|
12941
|
+
if (!Array.isArray(left) || !Array.isArray(right) || left.length !== right.length) {
|
|
12942
|
+
return false;
|
|
12943
|
+
}
|
|
12944
|
+
return left.every((value, index) => deepEqual(value, right[index]));
|
|
12945
|
+
}
|
|
12946
|
+
if (typeof left === "object" && typeof right === "object") {
|
|
12947
|
+
const leftKeys = Object.keys(left);
|
|
12948
|
+
const rightKeys = Object.keys(right);
|
|
12949
|
+
if (leftKeys.length !== rightKeys.length) {
|
|
12950
|
+
return false;
|
|
12951
|
+
}
|
|
12952
|
+
return leftKeys.every((key) => Object.prototype.hasOwnProperty.call(right, key) && deepEqual(left[key], right[key]));
|
|
12953
|
+
}
|
|
12954
|
+
return false;
|
|
12955
|
+
}
|
|
12956
|
+
|
|
12957
|
+
// src/services/doctor.ts
|
|
12958
|
+
var IDoctorService = createDecorator("doctorService");
|
|
12959
|
+
var BIN_NAMES = {
|
|
12960
|
+
claude: "claude",
|
|
12961
|
+
codex: "codex",
|
|
12962
|
+
kimi: "kimi",
|
|
12963
|
+
pi: "pi",
|
|
12964
|
+
dsh: "dsh"
|
|
12965
|
+
};
|
|
12966
|
+
var _dec = [
|
|
12967
|
+
inject(IEnvironmentService, IFileService, IHarnessRegistry, IAdapterRegistry, IActivationService, IDriftService)
|
|
12968
|
+
];
|
|
12969
|
+
var _init = __decoratorStart(undefined);
|
|
12970
|
+
|
|
12971
|
+
class DoctorService {
|
|
12972
|
+
environment;
|
|
12973
|
+
files;
|
|
12974
|
+
harnesses;
|
|
12975
|
+
adapters;
|
|
12976
|
+
activation;
|
|
12977
|
+
drift;
|
|
12978
|
+
constructor(environment, files, harnesses, adapters, activation, drift) {
|
|
12979
|
+
this.environment = environment;
|
|
12980
|
+
this.files = files;
|
|
12981
|
+
this.harnesses = harnesses;
|
|
12982
|
+
this.adapters = adapters;
|
|
12983
|
+
this.activation = activation;
|
|
12984
|
+
this.drift = drift;
|
|
12985
|
+
}
|
|
12986
|
+
async run(options = {}) {
|
|
12987
|
+
const harnessIds = options.harness ? [this.harnesses.require(options.harness)] : this.harnesses.list().map((item) => item.id);
|
|
12988
|
+
const items = [];
|
|
12989
|
+
for (const harness of harnessIds) {
|
|
12990
|
+
items.push({ harness, checks: this.harnessChecks(harness, options.probe === true) });
|
|
12991
|
+
}
|
|
12992
|
+
const update = await checkForUpdate();
|
|
12993
|
+
return { items, updatedAvailable: update.updateAvailable };
|
|
12994
|
+
}
|
|
12995
|
+
harnessChecks(harness, probe) {
|
|
12996
|
+
const checks = [];
|
|
12997
|
+
const bin = BIN_NAMES[harness];
|
|
12998
|
+
checks.push(this.commandExists(bin) ? ok(`${harness}.install`, `\u5DF2\u627E\u5230\u53EF\u6267\u884C\u6587\u4EF6 ${bin}`) : error(`${harness}.install`, `\u672A\u627E\u5230\u53EF\u6267\u884C\u6587\u4EF6 ${bin}\uFF08PATH \u4E2D\u4E0D\u5B58\u5728\uFF09\uFF0C\u5DE5\u5177\u53EF\u80FD\u672A\u5B89\u88C5`));
|
|
12999
|
+
const targets = this.adapters.get(harness).targets();
|
|
13000
|
+
for (const target of targets) {
|
|
13001
|
+
const dir = dirname3(target.path);
|
|
13002
|
+
const present = this.files.exists(dir);
|
|
13003
|
+
checks.push(present ? ok(`${harness}.configDir.${target.key}`, `${target.label} \u6240\u5728\u76EE\u5F55\u5B58\u5728\uFF08${dir}\uFF09`) : error(`${harness}.configDir.${target.key}`, `${target.label} \u6240\u5728\u76EE\u5F55\u4E0D\u5B58\u5728\uFF08${dir}\uFF09\uFF0C\u65E0\u6CD5\u5199\u5165\u914D\u7F6E`));
|
|
13004
|
+
}
|
|
13005
|
+
for (const target of targets) {
|
|
13006
|
+
const path = target.path;
|
|
13007
|
+
const exists = this.files.exists(path);
|
|
13008
|
+
if (!exists) {
|
|
13009
|
+
checks.push(warn(`${harness}.files.${target.key}`, `${target.label} \u4E0D\u5B58\u5728\uFF08${path}\uFF09`));
|
|
13010
|
+
continue;
|
|
13011
|
+
}
|
|
13012
|
+
if (!this.isReadable(path)) {
|
|
13013
|
+
checks.push(error(`${harness}.files.${target.key}`, `${target.label} \u4E0D\u53EF\u8BFB\uFF08${path}\uFF09`));
|
|
13014
|
+
continue;
|
|
13015
|
+
}
|
|
13016
|
+
if (!this.isWritable(path)) {
|
|
13017
|
+
checks.push(warn(`${harness}.files.${target.key}`, `${target.label} \u4E0D\u53EF\u5199\uFF08${path}\uFF09`));
|
|
13018
|
+
continue;
|
|
13019
|
+
}
|
|
13020
|
+
const mode = statSync2(path).mode & 511;
|
|
13021
|
+
if ((mode & 63) !== 0) {
|
|
13022
|
+
checks.push(warn(`${harness}.files.${target.key}`, `${target.label} \u5B58\u5728\u53EF\u8BFB\u5199\uFF0C\u4F46\u6743\u9650\u4E3A ${octal(mode)}\uFF0Cgroup/other \u53EF\u8BFB\uFF0C\u5EFA\u8BAE 0600`, { mode }));
|
|
13023
|
+
} else {
|
|
13024
|
+
checks.push(ok(`${harness}.files.${target.key}`, `${target.label} \u5B58\u5728\u4E14\u53EF\u8BFB\u53EF\u5199\uFF08${path}\uFF09`));
|
|
13025
|
+
}
|
|
13026
|
+
}
|
|
13027
|
+
for (const target of targets) {
|
|
13028
|
+
const content = this.files.readOptional(target.path);
|
|
13029
|
+
if (content === undefined) {
|
|
13030
|
+
continue;
|
|
13031
|
+
}
|
|
13032
|
+
try {
|
|
13033
|
+
assertParsable(target.format, target.path, content);
|
|
13034
|
+
checks.push(ok(`${harness}.parse.${target.key}`, `${target.label} \u89E3\u6790\u6B63\u5E38`));
|
|
13035
|
+
} catch (err) {
|
|
13036
|
+
checks.push(error(`${harness}.parse.${target.key}`, `${target.label} \u65E0\u6CD5\u89E3\u6790\uFF1A${err.message}`));
|
|
13037
|
+
}
|
|
13038
|
+
}
|
|
13039
|
+
const summary = this.drift.inspect(harness);
|
|
13040
|
+
if (!summary.active) {
|
|
13041
|
+
checks.push(ok(`${harness}.drift`, "\u672A\u6FC0\u6D3B\u4EFB\u4F55\u914D\u7F6E\uFF0C\u65E0\u6F02\u79FB\u53EF\u68C0\u67E5"));
|
|
13042
|
+
} else if (summary.status === "invalid") {
|
|
13043
|
+
checks.push(error(`${harness}.drift`, "live \u6587\u4EF6\u5B58\u5728\u65E0\u6CD5\u89E3\u6790\u7684\u5185\u5BB9"));
|
|
13044
|
+
} else if (summary.status === "drifted" || summary.status === "missing") {
|
|
13045
|
+
const affected = summary.files.filter((file) => file.status !== "in-sync").length;
|
|
13046
|
+
checks.push(warn(`${harness}.drift`, `${affected} \u4E2A\u6587\u4EF6\u4E0E\u6FC0\u6D3B\u914D\u7F6E\u4E0D\u4E00\u81F4\uFF08${summary.status}\uFF09`));
|
|
13047
|
+
} else {
|
|
13048
|
+
checks.push(ok(`${harness}.drift`, "live \u6587\u4EF6\u4E0E\u6FC0\u6D3B\u914D\u7F6E\u4E00\u81F4"));
|
|
13049
|
+
}
|
|
13050
|
+
if (probe) {
|
|
13051
|
+
const active = this.activation.getActive(harness);
|
|
13052
|
+
checks.push(unknown(`${harness}.probe`, "\u8FDE\u901A\u6027\u63A2\u6D4B\u5728 MVP \u4E2D\u9ED8\u8BA4\u5173\u95ED\uFF0C\u672A\u53D1\u8D77\u7F51\u7EDC\u8BF7\u6C42", {
|
|
13053
|
+
baseUrl: active?.baseUrl ?? null,
|
|
13054
|
+
enabled: false
|
|
13055
|
+
}));
|
|
13056
|
+
}
|
|
13057
|
+
return checks;
|
|
13058
|
+
}
|
|
13059
|
+
commandExists(bin) {
|
|
13060
|
+
try {
|
|
13061
|
+
const result = Bun.spawnSync(["/bin/sh", "-c", `command -v ${bin}`], {
|
|
13062
|
+
env: process.env
|
|
13063
|
+
});
|
|
13064
|
+
return result.exitCode === 0 && result.stdout.toString().trim().length > 0;
|
|
13065
|
+
} catch {
|
|
13066
|
+
return false;
|
|
13067
|
+
}
|
|
13068
|
+
}
|
|
13069
|
+
isReadable(path) {
|
|
13070
|
+
try {
|
|
13071
|
+
accessSync(path, constants.R_OK);
|
|
13072
|
+
return true;
|
|
13073
|
+
} catch {
|
|
13074
|
+
return false;
|
|
13075
|
+
}
|
|
13076
|
+
}
|
|
13077
|
+
isWritable(path) {
|
|
13078
|
+
try {
|
|
13079
|
+
accessSync(path, constants.W_OK);
|
|
13080
|
+
return true;
|
|
13081
|
+
} catch {
|
|
13082
|
+
return false;
|
|
13083
|
+
}
|
|
13084
|
+
}
|
|
13085
|
+
}
|
|
13086
|
+
DoctorService = __decorateElement(_init, 0, "DoctorService", _dec, DoctorService);
|
|
13087
|
+
__runInitializers(_init, 1, DoctorService);
|
|
13088
|
+
__decoratorMetadata(_init, DoctorService);
|
|
13089
|
+
let _DoctorService = DoctorService;
|
|
13090
|
+
function makeCheck(id, label, status, detail) {
|
|
13091
|
+
const detailObject = detail === undefined ? { message: label } : { message: label, ...detail };
|
|
13092
|
+
return { id, label, status, detail: detailObject };
|
|
13093
|
+
}
|
|
13094
|
+
function ok(id, message, detail) {
|
|
13095
|
+
return makeCheck(id, message, "ok", detail);
|
|
13096
|
+
}
|
|
13097
|
+
function warn(id, message, detail) {
|
|
13098
|
+
return makeCheck(id, message, "warn", detail);
|
|
13099
|
+
}
|
|
13100
|
+
function error(id, message, detail) {
|
|
13101
|
+
return makeCheck(id, message, "error", detail);
|
|
13102
|
+
}
|
|
13103
|
+
function unknown(id, message, detail) {
|
|
13104
|
+
return makeCheck(id, message, "unknown", detail);
|
|
13105
|
+
}
|
|
13106
|
+
function octal(mode) {
|
|
13107
|
+
return mode.toString(8).padStart(4, "0");
|
|
13108
|
+
}
|
|
13109
|
+
|
|
13110
|
+
// src/http/routes/doctor.ts
|
|
13111
|
+
function createDoctorRoutes(services) {
|
|
13112
|
+
const app = new Hono2;
|
|
13113
|
+
const doctor = services.get(IDoctorService);
|
|
13114
|
+
app.get("/", async (c) => {
|
|
13115
|
+
const probe = c.req.query("probe") === "1";
|
|
13116
|
+
const harnessParam = c.req.query("harness");
|
|
13117
|
+
const harness = harnessParam && harnessParam.length > 0 ? harnessParam : undefined;
|
|
13118
|
+
return c.json(await doctor.run({ probe, harness }));
|
|
13119
|
+
});
|
|
13120
|
+
return app;
|
|
13121
|
+
}
|
|
13122
|
+
|
|
13123
|
+
// src/http/routes/drift.ts
|
|
13124
|
+
function createDriftRoutes(services) {
|
|
13125
|
+
const app = new Hono2;
|
|
13126
|
+
const drift = services.get(IDriftService);
|
|
13127
|
+
const harnesses = services.get(IHarnessRegistry);
|
|
13128
|
+
app.get("/", (c) => c.json({ items: drift.inspectAll() }));
|
|
13129
|
+
app.get("/:harnessId", (c) => c.json(drift.inspect(harnesses.require(c.req.param("harnessId")))));
|
|
13130
|
+
app.post("/:harnessId/reapply", (c) => c.json(drift.reapply(harnesses.require(c.req.param("harnessId")))));
|
|
13131
|
+
app.post("/:harnessId/adopt", (c) => c.json(drift.adopt(harnesses.require(c.req.param("harnessId")))));
|
|
13132
|
+
return app;
|
|
13133
|
+
}
|
|
13134
|
+
|
|
13135
|
+
// src/http/routes/harnesses.ts
|
|
13136
|
+
function createHarnessRoutes(services) {
|
|
13137
|
+
const app = new Hono2;
|
|
13138
|
+
const harnesses = services.get(IHarnessRegistry);
|
|
13139
|
+
const profiles = services.get(IProfileService);
|
|
13140
|
+
const activation = services.get(IActivationService);
|
|
13141
|
+
const adapters = services.get(IAdapterRegistry);
|
|
13142
|
+
const environment = services.get(IEnvironmentService);
|
|
13143
|
+
const files = services.get(IFileService);
|
|
13144
|
+
const log = services.get(ILogService);
|
|
13145
|
+
function summary(id) {
|
|
13146
|
+
const adapter = adapters.get(id);
|
|
13147
|
+
return {
|
|
13148
|
+
id,
|
|
13149
|
+
label: harnesses.label(id),
|
|
13150
|
+
mode: adapter.mode,
|
|
13151
|
+
active: activation.getActive(id),
|
|
13152
|
+
profiles: profiles.list(id),
|
|
13153
|
+
fields: adapter.fields,
|
|
13154
|
+
targets: adapter.targets(),
|
|
13155
|
+
envVars: adapter.envVarNames,
|
|
13156
|
+
envNote: adapter.envNote,
|
|
13157
|
+
supportsOfficialAuth: adapter.renderOfficial !== undefined
|
|
13158
|
+
};
|
|
13159
|
+
}
|
|
13160
|
+
app.get("/", (c) => c.json({
|
|
13161
|
+
envFile: environment.files.env,
|
|
13162
|
+
items: harnesses.list().map((item) => summary(item.id))
|
|
13163
|
+
}));
|
|
13164
|
+
app.get("/:harnessId", (c) => c.json(summary(harnesses.require(c.req.param("harnessId")))));
|
|
13165
|
+
app.post("/:harnessId/profiles", async (c) => {
|
|
13166
|
+
const harnessId = harnesses.require(c.req.param("harnessId"));
|
|
13167
|
+
const body = await readBody(c.req.json.bind(c.req));
|
|
13168
|
+
const profile = profiles.upsert(harnessId, {
|
|
13169
|
+
name: String(body.name ?? ""),
|
|
12386
13170
|
baseUrl: body.baseUrl,
|
|
12387
13171
|
apiKey: body.apiKey,
|
|
12388
13172
|
model: body.model,
|
|
12389
13173
|
notes: body.notes,
|
|
12390
13174
|
extras: body.extras,
|
|
12391
|
-
overrides: body.overrides
|
|
13175
|
+
overrides: body.overrides,
|
|
13176
|
+
providerId: body.providerId,
|
|
13177
|
+
providerEndpoint: body.providerEndpoint
|
|
12392
13178
|
}, true);
|
|
12393
13179
|
return c.json(profile, 201);
|
|
12394
13180
|
});
|
|
@@ -12408,12 +13194,14 @@ function createHarnessRoutes(services) {
|
|
|
12408
13194
|
model: body.model,
|
|
12409
13195
|
notes: body.notes,
|
|
12410
13196
|
extras: body.extras,
|
|
12411
|
-
overrides: body.overrides
|
|
13197
|
+
overrides: body.overrides,
|
|
13198
|
+
providerId: body.providerId,
|
|
13199
|
+
providerEndpoint: body.providerEndpoint
|
|
12412
13200
|
}, false);
|
|
12413
13201
|
persisted = true;
|
|
12414
13202
|
activation.reconcileProfileUpdate(harnessId, name, profile.name);
|
|
12415
13203
|
return c.json(profile);
|
|
12416
|
-
} catch (
|
|
13204
|
+
} catch (error2) {
|
|
12417
13205
|
if (persisted) {
|
|
12418
13206
|
restoreProfileStore(files, environment.files.profiles, snapshot);
|
|
12419
13207
|
if (wasActive) {
|
|
@@ -12424,7 +13212,7 @@ function createHarnessRoutes(services) {
|
|
|
12424
13212
|
}
|
|
12425
13213
|
}
|
|
12426
13214
|
}
|
|
12427
|
-
throw
|
|
13215
|
+
throw error2;
|
|
12428
13216
|
}
|
|
12429
13217
|
});
|
|
12430
13218
|
app.delete("/:harnessId/profiles/:name", (c) => {
|
|
@@ -12465,6 +13253,54 @@ function restoreProfileStore(files, path, snapshot) {
|
|
|
12465
13253
|
files.writeSecure(path, snapshot);
|
|
12466
13254
|
}
|
|
12467
13255
|
|
|
13256
|
+
// src/http/routes/providers.ts
|
|
13257
|
+
function createProviderRoutes(services) {
|
|
13258
|
+
const app = new Hono2;
|
|
13259
|
+
const vault = services.get(IVaultService);
|
|
13260
|
+
const profiles = services.get(IProfileService);
|
|
13261
|
+
const activation = services.get(IActivationService);
|
|
13262
|
+
const log = services.get(ILogService);
|
|
13263
|
+
app.get("/", (c) => c.json({ items: vault.list() }));
|
|
13264
|
+
app.get("/:id", (c) => c.json(vault.get(decodeURIComponent(c.req.param("id")))));
|
|
13265
|
+
app.post("/", async (c) => {
|
|
13266
|
+
const body = await readBody2(c.req.json.bind(c.req));
|
|
13267
|
+
return c.json({ provider: vault.create(body), warnings: [] }, 201);
|
|
13268
|
+
});
|
|
13269
|
+
app.patch("/:id", async (c) => {
|
|
13270
|
+
const id = decodeURIComponent(c.req.param("id"));
|
|
13271
|
+
const body = await readBody2(c.req.json.bind(c.req));
|
|
13272
|
+
const { provider, affected } = vault.update(id, body);
|
|
13273
|
+
profiles.sweepVaultCache(id, vault.decrypt(id), provider.endpoints);
|
|
13274
|
+
const warnings = [];
|
|
13275
|
+
for (const ref of affected) {
|
|
13276
|
+
if (activation.getActive(ref.harness)?.name !== ref.name) {
|
|
13277
|
+
continue;
|
|
13278
|
+
}
|
|
13279
|
+
const profile = profiles.get(ref.harness, ref.name);
|
|
13280
|
+
if (profile?.providerEndpoint && !provider.endpoints.some((ep) => ep.key === profile.providerEndpoint)) {
|
|
13281
|
+
warnings.push(`${ref.harness}/${ref.name}: endpoint ${profile.providerEndpoint} \u5DF2\u4E0D\u5B58\u5728\uFF0C\u5C06\u56DE\u9000\u5230 provider \u9996\u4E2A endpoint`);
|
|
13282
|
+
}
|
|
13283
|
+
try {
|
|
13284
|
+
activation.activate(ref.harness, ref.name);
|
|
13285
|
+
} catch (error2) {
|
|
13286
|
+
log.error(`providers update: failed to re-apply ${ref.harness}/${ref.name}`, error2);
|
|
13287
|
+
warnings.push(`${ref.harness}/${ref.name} \u91CD\u65B0\u5E94\u7528\u5931\u8D25\uFF1A${error2.message}`);
|
|
13288
|
+
}
|
|
13289
|
+
}
|
|
13290
|
+
return c.json({ provider, warnings });
|
|
13291
|
+
});
|
|
13292
|
+
app.delete("/:id", (c) => {
|
|
13293
|
+
vault.remove(decodeURIComponent(c.req.param("id")));
|
|
13294
|
+
return c.json({ ok: true });
|
|
13295
|
+
});
|
|
13296
|
+
return app;
|
|
13297
|
+
}
|
|
13298
|
+
async function readBody2(read) {
|
|
13299
|
+
return read().catch(() => {
|
|
13300
|
+
throw new HttpError(400, "invalid json");
|
|
13301
|
+
});
|
|
13302
|
+
}
|
|
13303
|
+
|
|
12468
13304
|
// src/services/transfer.ts
|
|
12469
13305
|
import { createCipheriv as createCipheriv2, createDecipheriv as createDecipheriv2, randomBytes as randomBytes4, scryptSync } from "crypto";
|
|
12470
13306
|
var FORMAT = "harness-switch-encrypted-export";
|
|
@@ -12472,7 +13308,7 @@ var VERSION = 1;
|
|
|
12472
13308
|
var MAX_ENCRYPTED_BYTES = 10 * 1024 * 1024;
|
|
12473
13309
|
var ITransferService = createDecorator("transferService");
|
|
12474
13310
|
var _dec = [
|
|
12475
|
-
inject(IEnvironmentService, IFileService, ICryptoService, IAdapterRegistry, IActivationService)
|
|
13311
|
+
inject(IEnvironmentService, IFileService, ICryptoService, IAdapterRegistry, IActivationService, IVaultService)
|
|
12476
13312
|
];
|
|
12477
13313
|
var _init = __decoratorStart(undefined);
|
|
12478
13314
|
|
|
@@ -12482,12 +13318,14 @@ class TransferService {
|
|
|
12482
13318
|
crypto;
|
|
12483
13319
|
adapters;
|
|
12484
13320
|
activation;
|
|
12485
|
-
|
|
13321
|
+
vault;
|
|
13322
|
+
constructor(environment, files, crypto2, adapters, activation, vault) {
|
|
12486
13323
|
this.environment = environment;
|
|
12487
13324
|
this.files = files;
|
|
12488
13325
|
this.crypto = crypto2;
|
|
12489
13326
|
this.adapters = adapters;
|
|
12490
13327
|
this.activation = activation;
|
|
13328
|
+
this.vault = vault;
|
|
12491
13329
|
}
|
|
12492
13330
|
exportAll(passphrase) {
|
|
12493
13331
|
this.assertPassphrase(passphrase);
|
|
@@ -12499,7 +13337,7 @@ class TransferService {
|
|
|
12499
13337
|
harness,
|
|
12500
13338
|
name,
|
|
12501
13339
|
baseUrl: stored.base_url || "",
|
|
12502
|
-
apiKey: this.
|
|
13340
|
+
apiKey: this.resolveKey(stored),
|
|
12503
13341
|
model: stored.model || "",
|
|
12504
13342
|
notes: stored.notes || "",
|
|
12505
13343
|
extras: stored.extras ?? {},
|
|
@@ -12577,8 +13415,8 @@ class TransferService {
|
|
|
12577
13415
|
throw new Error("\u5BF9\u5E94\u914D\u7F6E\u4E0D\u5B58\u5728");
|
|
12578
13416
|
}
|
|
12579
13417
|
activeRestored++;
|
|
12580
|
-
} catch (
|
|
12581
|
-
warnings.push(`${active.harness}/${active.name} \u672A\u80FD\u6062\u590D\u6FC0\u6D3B\u72B6\u6001\uFF1A${
|
|
13418
|
+
} catch (error2) {
|
|
13419
|
+
warnings.push(`${active.harness}/${active.name} \u672A\u80FD\u6062\u590D\u6FC0\u6D3B\u72B6\u6001\uFF1A${error2.message}`);
|
|
12582
13420
|
}
|
|
12583
13421
|
}
|
|
12584
13422
|
}
|
|
@@ -12631,6 +13469,16 @@ class TransferService {
|
|
|
12631
13469
|
readStore() {
|
|
12632
13470
|
return this.files.readJsonStrict(this.environment.files.profiles, {});
|
|
12633
13471
|
}
|
|
13472
|
+
resolveKey(stored) {
|
|
13473
|
+
if (!stored.provider_id) {
|
|
13474
|
+
return this.crypto.decrypt(stored.api_key);
|
|
13475
|
+
}
|
|
13476
|
+
try {
|
|
13477
|
+
return this.vault.decrypt(stored.provider_id);
|
|
13478
|
+
} catch {
|
|
13479
|
+
return this.crypto.decrypt(stored.api_key);
|
|
13480
|
+
}
|
|
13481
|
+
}
|
|
12634
13482
|
assertPassphrase(passphrase) {
|
|
12635
13483
|
if (passphrase.length < 8) {
|
|
12636
13484
|
throw new HttpError(400, "\u8FC1\u79FB\u5BC6\u7801\u81F3\u5C11\u9700\u8981 8 \u4E2A\u5B57\u7B26");
|
|
@@ -12700,15 +13548,15 @@ function createTransferRoutes(services) {
|
|
|
12700
13548
|
const app = new Hono2;
|
|
12701
13549
|
const transfer = services.get(ITransferService);
|
|
12702
13550
|
app.post("/export", async (c) => {
|
|
12703
|
-
const body = await
|
|
13551
|
+
const body = await readBody3(c.req.json.bind(c.req));
|
|
12704
13552
|
return c.json(transfer.exportAll(String(body.passphrase ?? "")));
|
|
12705
13553
|
});
|
|
12706
13554
|
app.post("/preview", async (c) => {
|
|
12707
|
-
const body = await
|
|
13555
|
+
const body = await readBody3(c.req.json.bind(c.req));
|
|
12708
13556
|
return c.json(transfer.preview(requireEnvelope(body.envelope), String(body.passphrase ?? "")));
|
|
12709
13557
|
});
|
|
12710
13558
|
app.post("/import", async (c) => {
|
|
12711
|
-
const body = await
|
|
13559
|
+
const body = await readBody3(c.req.json.bind(c.req));
|
|
12712
13560
|
return c.json(transfer.importAll(requireEnvelope(body.envelope), String(body.passphrase ?? ""), body.conflictPolicy ?? "skip", body.restoreActive === true));
|
|
12713
13561
|
});
|
|
12714
13562
|
return app;
|
|
@@ -12719,104 +13567,12 @@ function requireEnvelope(envelope) {
|
|
|
12719
13567
|
}
|
|
12720
13568
|
return envelope;
|
|
12721
13569
|
}
|
|
12722
|
-
async function
|
|
13570
|
+
async function readBody3(read) {
|
|
12723
13571
|
return read().catch(() => {
|
|
12724
13572
|
throw new HttpError(400, "invalid json");
|
|
12725
13573
|
});
|
|
12726
13574
|
}
|
|
12727
13575
|
|
|
12728
|
-
// src/update.ts
|
|
12729
|
-
import { spawn } from "child_process";
|
|
12730
|
-
import { mkdirSync as mkdirSync3, openSync } from "fs";
|
|
12731
|
-
import { homedir as homedir2 } from "os";
|
|
12732
|
-
import { join as join10 } from "path";
|
|
12733
|
-
|
|
12734
|
-
// src/version.ts
|
|
12735
|
-
import { join as join9 } from "path";
|
|
12736
|
-
var cached;
|
|
12737
|
-
async function packageInfo() {
|
|
12738
|
-
if (cached)
|
|
12739
|
-
return cached;
|
|
12740
|
-
try {
|
|
12741
|
-
const pkg = JSON.parse(await Bun.file(join9(import.meta.dir, "../package.json")).text());
|
|
12742
|
-
cached = {
|
|
12743
|
-
name: typeof pkg.name === "string" ? pkg.name : "harness-switch",
|
|
12744
|
-
version: typeof pkg.version === "string" ? pkg.version : "0.0.0"
|
|
12745
|
-
};
|
|
12746
|
-
} catch {
|
|
12747
|
-
cached = { name: "harness-switch", version: "0.0.0" };
|
|
12748
|
-
}
|
|
12749
|
-
return cached;
|
|
12750
|
-
}
|
|
12751
|
-
function serverVersion() {
|
|
12752
|
-
return packageInfo().then((info) => info.version);
|
|
12753
|
-
}
|
|
12754
|
-
function packageName() {
|
|
12755
|
-
return packageInfo().then((info) => info.name);
|
|
12756
|
-
}
|
|
12757
|
-
|
|
12758
|
-
// src/update.ts
|
|
12759
|
-
var REGISTRY_URL = "https://registry.npmjs.org/";
|
|
12760
|
-
var CHECK_TIMEOUT_MS = 5000;
|
|
12761
|
-
var CACHE_TTL_MS = 5 * 60000;
|
|
12762
|
-
var cached2;
|
|
12763
|
-
function compareVersions(a, b) {
|
|
12764
|
-
const pa = a.split(".").map(Number);
|
|
12765
|
-
const pb = b.split(".").map(Number);
|
|
12766
|
-
const len = Math.max(pa.length, pb.length);
|
|
12767
|
-
for (let i = 0;i < len; i++) {
|
|
12768
|
-
const da = pa[i] ?? 0;
|
|
12769
|
-
const db = pb[i] ?? 0;
|
|
12770
|
-
if (da !== db)
|
|
12771
|
-
return da > db ? 1 : -1;
|
|
12772
|
-
}
|
|
12773
|
-
return 0;
|
|
12774
|
-
}
|
|
12775
|
-
async function checkForUpdate(force = false) {
|
|
12776
|
-
const current = await serverVersion();
|
|
12777
|
-
if (!force && cached2 && Date.now() - cached2.at < CACHE_TTL_MS) {
|
|
12778
|
-
return {
|
|
12779
|
-
current,
|
|
12780
|
-
latest: cached2.latest,
|
|
12781
|
-
updateAvailable: cached2.latest !== null && compareVersions(cached2.latest, current) > 0
|
|
12782
|
-
};
|
|
12783
|
-
}
|
|
12784
|
-
try {
|
|
12785
|
-
const name = encodeURIComponent(await packageName());
|
|
12786
|
-
const response = await fetch(`${REGISTRY_URL}${name}/latest`, {
|
|
12787
|
-
signal: AbortSignal.timeout(CHECK_TIMEOUT_MS)
|
|
12788
|
-
});
|
|
12789
|
-
if (!response.ok) {
|
|
12790
|
-
throw new Error(`registry responded ${response.status}`);
|
|
12791
|
-
}
|
|
12792
|
-
const payload = await response.json();
|
|
12793
|
-
const latest = typeof payload.version === "string" ? payload.version : null;
|
|
12794
|
-
cached2 = { latest, at: Date.now() };
|
|
12795
|
-
return {
|
|
12796
|
-
current,
|
|
12797
|
-
latest,
|
|
12798
|
-
updateAvailable: latest !== null && compareVersions(latest, current) > 0
|
|
12799
|
-
};
|
|
12800
|
-
} catch {
|
|
12801
|
-
return { current, latest: null, updateAvailable: false };
|
|
12802
|
-
}
|
|
12803
|
-
}
|
|
12804
|
-
async function triggerUpdate() {
|
|
12805
|
-
if (process.env.HSW_UPDATE_SPAWN === "0") {
|
|
12806
|
-
return;
|
|
12807
|
-
}
|
|
12808
|
-
const name = await packageName();
|
|
12809
|
-
const dataDir = process.env.HSW_DATA_DIR || join10(homedir2(), ".harness-switch");
|
|
12810
|
-
mkdirSync3(dataDir, { recursive: true, mode: 448 });
|
|
12811
|
-
const logFd = openSync(join10(dataDir, "update.log"), "w");
|
|
12812
|
-
const child = spawn(process.execPath, ["x", `${name}@latest`], {
|
|
12813
|
-
detached: true,
|
|
12814
|
-
stdio: ["ignore", logFd, logFd],
|
|
12815
|
-
env: process.env
|
|
12816
|
-
});
|
|
12817
|
-
child.unref();
|
|
12818
|
-
}
|
|
12819
|
-
|
|
12820
13576
|
// src/http/routes/update.ts
|
|
12821
13577
|
function createUpdateRoutes(_services) {
|
|
12822
13578
|
const api = new Hono2;
|
|
@@ -12850,6 +13606,15 @@ function createApp(services) {
|
|
|
12850
13606
|
api.use("/update/*", createAuthGuard(services));
|
|
12851
13607
|
api.use("/update", createAuthGuard(services));
|
|
12852
13608
|
api.route("/update", createUpdateRoutes(services));
|
|
13609
|
+
api.use("/providers/*", createAuthGuard(services));
|
|
13610
|
+
api.use("/providers", createAuthGuard(services));
|
|
13611
|
+
api.route("/providers", createProviderRoutes(services));
|
|
13612
|
+
api.use("/doctor/*", createAuthGuard(services));
|
|
13613
|
+
api.use("/doctor", createAuthGuard(services));
|
|
13614
|
+
api.route("/doctor", createDoctorRoutes(services));
|
|
13615
|
+
api.use("/drift/*", createAuthGuard(services));
|
|
13616
|
+
api.use("/drift", createAuthGuard(services));
|
|
13617
|
+
api.route("/drift", createDriftRoutes(services));
|
|
12853
13618
|
app.route("/api", api);
|
|
12854
13619
|
const publicDir = environment.publicDir;
|
|
12855
13620
|
if (existsSync3(publicDir)) {
|
|
@@ -12871,11 +13636,11 @@ function createApp(services) {
|
|
|
12871
13636
|
});
|
|
12872
13637
|
}
|
|
12873
13638
|
app.notFound((c) => c.json({ error: "not found" }, 404));
|
|
12874
|
-
app.onError((
|
|
12875
|
-
if (
|
|
12876
|
-
return c.json({ error:
|
|
13639
|
+
app.onError((error2, c) => {
|
|
13640
|
+
if (error2 instanceof HttpError) {
|
|
13641
|
+
return c.json({ error: error2.message }, error2.status);
|
|
12877
13642
|
}
|
|
12878
|
-
log.error("unhandled error",
|
|
13643
|
+
log.error("unhandled error", error2);
|
|
12879
13644
|
return c.json({ error: "internal server error" }, 500);
|
|
12880
13645
|
});
|
|
12881
13646
|
return app;
|
|
@@ -12893,12 +13658,64 @@ function createServices() {
|
|
|
12893
13658
|
collection.set(IAdapterRegistry, new SyncDescriptor(AdapterRegistry));
|
|
12894
13659
|
collection.set(IBackupService, new SyncDescriptor(BackupService));
|
|
12895
13660
|
collection.set(ILiveWriteService, new SyncDescriptor(LiveWriteService));
|
|
13661
|
+
collection.set(IVaultService, new SyncDescriptor(VaultService));
|
|
12896
13662
|
collection.set(IProfileService, new SyncDescriptor(ProfileService));
|
|
12897
13663
|
collection.set(IActivationService, new SyncDescriptor(ActivationService));
|
|
12898
13664
|
collection.set(ITransferService, new SyncDescriptor(TransferService));
|
|
13665
|
+
collection.set(IDriftService, new SyncDescriptor(DriftService));
|
|
13666
|
+
collection.set(IDoctorService, new SyncDescriptor(DoctorService));
|
|
12899
13667
|
return new InstantiationService(collection);
|
|
12900
13668
|
}
|
|
12901
13669
|
|
|
13670
|
+
// src/cli/args.ts
|
|
13671
|
+
function parseArgs(argv) {
|
|
13672
|
+
const flags = {};
|
|
13673
|
+
const positional = [];
|
|
13674
|
+
for (const arg of argv) {
|
|
13675
|
+
if (arg.startsWith("--")) {
|
|
13676
|
+
const body = arg.slice(2);
|
|
13677
|
+
const separator = body.indexOf("=");
|
|
13678
|
+
if (separator !== -1) {
|
|
13679
|
+
flags[body.slice(0, separator)] = body.slice(separator + 1);
|
|
13680
|
+
} else {
|
|
13681
|
+
flags[body] = true;
|
|
13682
|
+
}
|
|
13683
|
+
} else {
|
|
13684
|
+
positional.push(arg);
|
|
13685
|
+
}
|
|
13686
|
+
}
|
|
13687
|
+
return { flags, positional };
|
|
13688
|
+
}
|
|
13689
|
+
function flagValue(flags, name, fallback = "") {
|
|
13690
|
+
const value = flags[name];
|
|
13691
|
+
return typeof value === "string" ? value : fallback;
|
|
13692
|
+
}
|
|
13693
|
+
function hasFlag(flags, name) {
|
|
13694
|
+
return flags[name] === true || typeof flags[name] === "string" && flags[name] !== "";
|
|
13695
|
+
}
|
|
13696
|
+
function requirePositional(positional, index, what) {
|
|
13697
|
+
const value = positional[index];
|
|
13698
|
+
if (!value) {
|
|
13699
|
+
throw new CliError(`\u7F3A\u5C11\u53C2\u6570\uFF1A${what}`);
|
|
13700
|
+
}
|
|
13701
|
+
return value;
|
|
13702
|
+
}
|
|
13703
|
+
|
|
13704
|
+
class CliError extends Error {
|
|
13705
|
+
constructor(message) {
|
|
13706
|
+
super(message);
|
|
13707
|
+
this.name = "CliError";
|
|
13708
|
+
}
|
|
13709
|
+
}
|
|
13710
|
+
|
|
13711
|
+
// src/cli/commands.ts
|
|
13712
|
+
import { stdin, stdout } from "process";
|
|
13713
|
+
import { createInterface } from "readline/promises";
|
|
13714
|
+
|
|
13715
|
+
// src/cli/client.ts
|
|
13716
|
+
import { readFileSync as readFileSync3 } from "fs";
|
|
13717
|
+
import { join as join13 } from "path";
|
|
13718
|
+
|
|
12902
13719
|
// src/daemon.ts
|
|
12903
13720
|
import { spawn as spawn2 } from "child_process";
|
|
12904
13721
|
import { mkdirSync as mkdirSync4, openSync as openSync2, readFileSync as readFileSync2, unlinkSync, writeFileSync as writeFileSync2 } from "fs";
|
|
@@ -13021,7 +13838,291 @@ async function daemonize() {
|
|
|
13021
13838
|
console.log(` password (first run): ${join12(daemonDataDir(), "web_password")}`);
|
|
13022
13839
|
}
|
|
13023
13840
|
function usage() {
|
|
13024
|
-
return
|
|
13841
|
+
return [
|
|
13842
|
+
"usage: harness-switch [server|daemon|status|stop]",
|
|
13843
|
+
" harness-switch [list|providers|doctor|plan|activate] [options]",
|
|
13844
|
+
" CLI commands run against the local data directory; add --json for machine output"
|
|
13845
|
+
].join(`
|
|
13846
|
+
`);
|
|
13847
|
+
}
|
|
13848
|
+
|
|
13849
|
+
// src/cli/client.ts
|
|
13850
|
+
class CliClient {
|
|
13851
|
+
baseUrl;
|
|
13852
|
+
password;
|
|
13853
|
+
cookie = "";
|
|
13854
|
+
constructor(baseUrl, password) {
|
|
13855
|
+
this.baseUrl = baseUrl;
|
|
13856
|
+
this.password = password;
|
|
13857
|
+
}
|
|
13858
|
+
async login() {
|
|
13859
|
+
const response = await this.fetch("/api/auth/login", {
|
|
13860
|
+
method: "POST",
|
|
13861
|
+
headers: { "Content-Type": "application/json" },
|
|
13862
|
+
body: JSON.stringify({ password: this.password })
|
|
13863
|
+
});
|
|
13864
|
+
const setCookie2 = response.headers.get("set-cookie") ?? "";
|
|
13865
|
+
const match2 = /hsw_session=([^;]+)/.exec(setCookie2);
|
|
13866
|
+
if (!response.ok || !match2) {
|
|
13867
|
+
throw new CliError(response.ok ? "\u767B\u5F55\u5931\u8D25\uFF1A\u670D\u52A1\u7AEF\u672A\u8FD4\u56DE\u4F1A\u8BDD" : `\u767B\u5F55\u5931\u8D25\uFF1A${await errorMessage(response)}`);
|
|
13868
|
+
}
|
|
13869
|
+
this.cookie = match2[1];
|
|
13870
|
+
}
|
|
13871
|
+
async get(path) {
|
|
13872
|
+
return this.request("GET", path);
|
|
13873
|
+
}
|
|
13874
|
+
async post(path, body) {
|
|
13875
|
+
return this.request("POST", path, body);
|
|
13876
|
+
}
|
|
13877
|
+
async request(method, path, body) {
|
|
13878
|
+
const response = await this.fetch(path, {
|
|
13879
|
+
method,
|
|
13880
|
+
headers: {
|
|
13881
|
+
Cookie: `hsw_session=${this.cookie}`,
|
|
13882
|
+
...body !== undefined ? { "Content-Type": "application/json" } : {}
|
|
13883
|
+
},
|
|
13884
|
+
body: body !== undefined ? JSON.stringify(body) : undefined
|
|
13885
|
+
});
|
|
13886
|
+
const payload = await response.json().catch(() => null);
|
|
13887
|
+
if (!response.ok) {
|
|
13888
|
+
const message = payload && typeof payload.error === "string" ? payload.error : `\u8BF7\u6C42\u5931\u8D25\uFF1AHTTP ${response.status}`;
|
|
13889
|
+
throw new CliError(message);
|
|
13890
|
+
}
|
|
13891
|
+
return payload;
|
|
13892
|
+
}
|
|
13893
|
+
async fetch(path, init) {
|
|
13894
|
+
try {
|
|
13895
|
+
return await fetch(`${this.baseUrl}${path}`, init);
|
|
13896
|
+
} catch {
|
|
13897
|
+
throw new CliError(`\u65E0\u6CD5\u8FDE\u63A5\u672C\u5730\u670D\u52A1 ${this.baseUrl}\uFF0C\u8BF7\u5148\u542F\u52A8\u670D\u52A1\uFF08harness-switch daemon \u6216 server\uFF09`);
|
|
13898
|
+
}
|
|
13899
|
+
}
|
|
13900
|
+
}
|
|
13901
|
+
function resolveBaseUrl() {
|
|
13902
|
+
return process.env.HSW_URL || `http://127.0.0.1:${process.env.PORT || 8787}`;
|
|
13903
|
+
}
|
|
13904
|
+
function readWebPassword() {
|
|
13905
|
+
const file = join13(daemonDataDir(), "web_password");
|
|
13906
|
+
try {
|
|
13907
|
+
return readFileSync3(file, "utf8").trim();
|
|
13908
|
+
} catch {
|
|
13909
|
+
throw new CliError(`\u672A\u627E\u5230 ${file}\u3002\u8BF7\u5148\u8FD0\u884C\u4E00\u6B21\u670D\u52A1\uFF08harness-switch daemon \u6216 server\uFF09\u751F\u6210\u5BC6\u7801\uFF0C\u518D\u4F7F\u7528 CLI\u3002`);
|
|
13910
|
+
}
|
|
13911
|
+
}
|
|
13912
|
+
async function errorMessage(response) {
|
|
13913
|
+
try {
|
|
13914
|
+
const payload = await response.json();
|
|
13915
|
+
if (typeof payload.error === "string") {
|
|
13916
|
+
return payload.error;
|
|
13917
|
+
}
|
|
13918
|
+
} catch {}
|
|
13919
|
+
return `HTTP ${response.status}`;
|
|
13920
|
+
}
|
|
13921
|
+
|
|
13922
|
+
// src/cli/output.ts
|
|
13923
|
+
function printJson(value) {
|
|
13924
|
+
console.log(JSON.stringify(value, null, 2));
|
|
13925
|
+
}
|
|
13926
|
+
function printListHuman(payload) {
|
|
13927
|
+
console.log(`env file: ${payload.envFile}`);
|
|
13928
|
+
for (const item of payload.items) {
|
|
13929
|
+
const active = item.active ? item.active.official ? "\u5B98\u65B9\u767B\u5F55" : item.active.name : "-";
|
|
13930
|
+
console.log(`${item.id.padEnd(8)} ${item.label.padEnd(14)} ${item.mode.padEnd(9)} active=${active} profiles=${item.profiles.length}`);
|
|
13931
|
+
}
|
|
13932
|
+
}
|
|
13933
|
+
function printProvidersHuman(items) {
|
|
13934
|
+
if (items.length === 0) {
|
|
13935
|
+
console.log("(no providers)");
|
|
13936
|
+
return;
|
|
13937
|
+
}
|
|
13938
|
+
for (const entry of items) {
|
|
13939
|
+
const endpoints = entry.endpoints.map((endpoint) => endpoint.key).join(",") || "-";
|
|
13940
|
+
const configured = entry.apiKeyConfigured ? "key=set" : "key=missing";
|
|
13941
|
+
console.log(`${entry.id.padEnd(24)} ${entry.name.padEnd(20)} ${configured.padEnd(9)} endpoints=${endpoints}`);
|
|
13942
|
+
}
|
|
13943
|
+
}
|
|
13944
|
+
function printDoctorHuman(report) {
|
|
13945
|
+
console.log(`harness-switch doctor updateAvailable=${report.updatedAvailable}`);
|
|
13946
|
+
for (const item of report.items) {
|
|
13947
|
+
console.log("");
|
|
13948
|
+
console.log(`${item.harness}:`);
|
|
13949
|
+
for (const check of item.checks) {
|
|
13950
|
+
printCheck(check);
|
|
13951
|
+
}
|
|
13952
|
+
}
|
|
13953
|
+
}
|
|
13954
|
+
function printCheck(check) {
|
|
13955
|
+
const tag = check.status === "ok" ? "[ok]" : check.status === "warn" ? "[warn]" : check.status === "error" ? "[error]" : "[unknown]";
|
|
13956
|
+
const message = detailMessage(check.detail) ?? check.label;
|
|
13957
|
+
console.log(` ${tag} ${message}`);
|
|
13958
|
+
}
|
|
13959
|
+
function detailMessage(detail) {
|
|
13960
|
+
if (typeof detail === "object" && detail !== null) {
|
|
13961
|
+
const message = detail.message;
|
|
13962
|
+
if (typeof message === "string") {
|
|
13963
|
+
return message;
|
|
13964
|
+
}
|
|
13965
|
+
}
|
|
13966
|
+
return;
|
|
13967
|
+
}
|
|
13968
|
+
function printPlanHuman(harness, profile, targets) {
|
|
13969
|
+
console.log(`plan ${harness}/${profile}:`);
|
|
13970
|
+
for (const target of targets) {
|
|
13971
|
+
const changed = target.content !== target.currentContent;
|
|
13972
|
+
const marker = target.overridden ? " (override)" : changed ? " (\u5C06\u5199\u5165)" : " (\u65E0\u53D8\u66F4)";
|
|
13973
|
+
console.log(`- ${target.path}${marker}`);
|
|
13974
|
+
}
|
|
13975
|
+
console.log("");
|
|
13976
|
+
console.log("\u5185\u5BB9\u5305\u542B API key\uFF0C\u8BF7\u4EC5\u5728\u53EF\u4FE1\u7EC8\u7AEF\u67E5\u770B\u3002");
|
|
13977
|
+
}
|
|
13978
|
+
function printActivateHuman(harness, profile, result) {
|
|
13979
|
+
console.log(`\u5DF2\u6FC0\u6D3B ${harness}/${profile}`);
|
|
13980
|
+
console.log(`env: ${result.envFile}`);
|
|
13981
|
+
for (const warning of result.warnings) {
|
|
13982
|
+
console.log(`warning: ${warning}`);
|
|
13983
|
+
}
|
|
13984
|
+
}
|
|
13985
|
+
function cliUsage() {
|
|
13986
|
+
return [
|
|
13987
|
+
"usage: harness-switch <command> [options]",
|
|
13988
|
+
"",
|
|
13989
|
+
"commands:",
|
|
13990
|
+
" list list harnesses, active profiles and live targets",
|
|
13991
|
+
" providers list Provider Vault entries",
|
|
13992
|
+
" doctor [--probe] [--harness H] run diagnostics (add --probe for /models checks)",
|
|
13993
|
+
" plan <harness> <profile> show the exact content activation would write",
|
|
13994
|
+
" activate <harness> <profile> activate a profile (add --yes to skip the prompt)",
|
|
13995
|
+
"",
|
|
13996
|
+
"options:",
|
|
13997
|
+
" --json machine-readable JSON output",
|
|
13998
|
+
" --yes skip confirmation for activate",
|
|
13999
|
+
" --probe run the /models connectivity probe in doctor",
|
|
14000
|
+
" --harness H limit doctor to one harness",
|
|
14001
|
+
"",
|
|
14002
|
+
"environment:",
|
|
14003
|
+
" HSW_URL base URL of the local service (default http://127.0.0.1:8787)",
|
|
14004
|
+
" PORT port override when HSW_URL is unset",
|
|
14005
|
+
" HSW_DATA_DIR data directory holding web_password (default ~/.harness-switch)"
|
|
14006
|
+
].join(`
|
|
14007
|
+
`);
|
|
14008
|
+
}
|
|
14009
|
+
|
|
14010
|
+
// src/cli/commands.ts
|
|
14011
|
+
async function runCli(command, positional, flags) {
|
|
14012
|
+
const json = hasFlag(flags, "json") ? "json" : "human";
|
|
14013
|
+
let client;
|
|
14014
|
+
try {
|
|
14015
|
+
client = new CliClient(resolveBaseUrl(), readWebPassword());
|
|
14016
|
+
await client.login();
|
|
14017
|
+
} catch (error2) {
|
|
14018
|
+
return fail(error2, json);
|
|
14019
|
+
}
|
|
14020
|
+
try {
|
|
14021
|
+
switch (command) {
|
|
14022
|
+
case "list":
|
|
14023
|
+
return await cmdList(client, json);
|
|
14024
|
+
case "providers":
|
|
14025
|
+
return await cmdProviders(client, json);
|
|
14026
|
+
case "doctor":
|
|
14027
|
+
return await cmdDoctor(client, flags, json);
|
|
14028
|
+
case "plan":
|
|
14029
|
+
return await cmdPlan(client, positional, json);
|
|
14030
|
+
case "activate":
|
|
14031
|
+
return await cmdActivate(client, positional, flags, json);
|
|
14032
|
+
default:
|
|
14033
|
+
console.error(cliUsage());
|
|
14034
|
+
throw new CliError(`unknown command: ${command}`);
|
|
14035
|
+
}
|
|
14036
|
+
} catch (error2) {
|
|
14037
|
+
return fail(error2, json);
|
|
14038
|
+
}
|
|
14039
|
+
}
|
|
14040
|
+
async function cmdList(client, json) {
|
|
14041
|
+
const payload = await client.get("/api/harnesses");
|
|
14042
|
+
if (json === "json") {
|
|
14043
|
+
printJson(payload);
|
|
14044
|
+
} else {
|
|
14045
|
+
printListHuman(payload);
|
|
14046
|
+
}
|
|
14047
|
+
return 0;
|
|
14048
|
+
}
|
|
14049
|
+
async function cmdProviders(client, json) {
|
|
14050
|
+
const payload = await client.get("/api/providers");
|
|
14051
|
+
if (json === "json") {
|
|
14052
|
+
printJson(payload);
|
|
14053
|
+
} else {
|
|
14054
|
+
printProvidersHuman(payload.items);
|
|
14055
|
+
}
|
|
14056
|
+
return 0;
|
|
14057
|
+
}
|
|
14058
|
+
async function cmdDoctor(client, flags, json) {
|
|
14059
|
+
const query = new URLSearchParams;
|
|
14060
|
+
if (hasFlag(flags, "probe")) {
|
|
14061
|
+
query.set("probe", "1");
|
|
14062
|
+
}
|
|
14063
|
+
const harness = flagValue(flags, "harness");
|
|
14064
|
+
if (harness) {
|
|
14065
|
+
query.set("harness", harness);
|
|
14066
|
+
}
|
|
14067
|
+
const suffix = query.size > 0 ? `?${query.toString()}` : "";
|
|
14068
|
+
const payload = await client.get(`/api/doctor${suffix}`);
|
|
14069
|
+
if (json === "json") {
|
|
14070
|
+
printJson(payload);
|
|
14071
|
+
} else {
|
|
14072
|
+
printDoctorHuman(payload);
|
|
14073
|
+
}
|
|
14074
|
+
return 0;
|
|
14075
|
+
}
|
|
14076
|
+
async function cmdPlan(client, positional, json) {
|
|
14077
|
+
const harness = requirePositional(positional, 0, "harness");
|
|
14078
|
+
const profile = requirePositional(positional, 1, "profile");
|
|
14079
|
+
const payload = await client.get(`/api/harnesses/${encodeURIComponent(harness)}/profiles/${encodeURIComponent(profile)}/preview`);
|
|
14080
|
+
if (json === "json") {
|
|
14081
|
+
printJson({ harness, profile, ...payload });
|
|
14082
|
+
} else {
|
|
14083
|
+
printPlanHuman(harness, profile, payload.targets);
|
|
14084
|
+
}
|
|
14085
|
+
return 0;
|
|
14086
|
+
}
|
|
14087
|
+
async function cmdActivate(client, positional, flags, json) {
|
|
14088
|
+
const harness = requirePositional(positional, 0, "harness");
|
|
14089
|
+
const profile = requirePositional(positional, 1, "profile");
|
|
14090
|
+
if (!hasFlag(flags, "yes")) {
|
|
14091
|
+
if (!stdin.isTTY) {
|
|
14092
|
+
throw new CliError("\u975E\u4EA4\u4E92\u5F0F\u7EC8\u7AEF\u9700\u8981 --yes \u786E\u8BA4\u624D\u80FD\u6FC0\u6D3B");
|
|
14093
|
+
}
|
|
14094
|
+
const preview = await client.get(`/api/harnesses/${encodeURIComponent(harness)}/profiles/${encodeURIComponent(profile)}/preview`);
|
|
14095
|
+
if (json === "human") {
|
|
14096
|
+
printPlanHuman(harness, profile, preview.targets);
|
|
14097
|
+
}
|
|
14098
|
+
const readline = createInterface({ input: stdin, output: stdout });
|
|
14099
|
+
const answer = (await readline.question(`\u786E\u8BA4\u6FC0\u6D3B ${harness}/${profile}\uFF1F[y/N] `)).trim().toLowerCase();
|
|
14100
|
+
readline.close();
|
|
14101
|
+
if (answer !== "y" && answer !== "yes") {
|
|
14102
|
+
if (json === "json") {
|
|
14103
|
+
printJson({ cancelled: true });
|
|
14104
|
+
} else {
|
|
14105
|
+
console.log("\u5DF2\u53D6\u6D88");
|
|
14106
|
+
}
|
|
14107
|
+
return 0;
|
|
14108
|
+
}
|
|
14109
|
+
}
|
|
14110
|
+
const payload = await client.post(`/api/harnesses/${encodeURIComponent(harness)}/profiles/${encodeURIComponent(profile)}/activate`);
|
|
14111
|
+
if (json === "json") {
|
|
14112
|
+
printJson({ harness, profile, ...payload });
|
|
14113
|
+
} else {
|
|
14114
|
+
printActivateHuman(harness, profile, payload);
|
|
14115
|
+
}
|
|
14116
|
+
return 0;
|
|
14117
|
+
}
|
|
14118
|
+
function fail(error2, json) {
|
|
14119
|
+
const message = error2 instanceof Error ? error2.message : String(error2);
|
|
14120
|
+
if (json === "json") {
|
|
14121
|
+
printJson({ error: { code: 1, message } });
|
|
14122
|
+
} else {
|
|
14123
|
+
console.error(`error: ${message}`);
|
|
14124
|
+
}
|
|
14125
|
+
return 1;
|
|
13025
14126
|
}
|
|
13026
14127
|
|
|
13027
14128
|
// src/main.ts
|
|
@@ -13041,13 +14142,16 @@ async function runServer() {
|
|
|
13041
14142
|
log.info(`data directory: ${environment.dataDir}`);
|
|
13042
14143
|
log.info(`listening on http://${server.hostname}:${server.port}`);
|
|
13043
14144
|
}
|
|
13044
|
-
var command = process.argv
|
|
14145
|
+
var [command, ...rest] = process.argv.slice(2);
|
|
13045
14146
|
if (command === "server") {
|
|
13046
14147
|
await runServer();
|
|
13047
14148
|
} else if (command === "stop") {
|
|
13048
14149
|
await stopDaemon();
|
|
13049
14150
|
} else if (command === "status") {
|
|
13050
14151
|
printStatus();
|
|
14152
|
+
} else if (command === "list" || command === "providers" || command === "doctor" || command === "plan" || command === "activate") {
|
|
14153
|
+
const { flags, positional } = parseArgs(rest);
|
|
14154
|
+
process.exitCode = await runCli(command, positional, flags);
|
|
13051
14155
|
} else if (command === undefined || command === "daemon") {
|
|
13052
14156
|
await daemonize();
|
|
13053
14157
|
} else {
|