ohmyswitch 0.1.0 → 0.2.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/oms.mjs +410 -162
- package/package.json +2 -2
package/oms.mjs
CHANGED
|
@@ -589,7 +589,7 @@ function renderSaved(result, out, message) {
|
|
|
589
589
|
if (result.rewritten.length) out.hint(`Rewrote ${listing(result.rewritten.map((harness) => harness.name))}.`);
|
|
590
590
|
warnProblems(out, result.failures);
|
|
591
591
|
}
|
|
592
|
-
const list$
|
|
592
|
+
const list$3 = command({
|
|
593
593
|
name: "list",
|
|
594
594
|
aliases: ["ls"],
|
|
595
595
|
summary: "List a provider's models",
|
|
@@ -599,8 +599,8 @@ const list$2 = command({
|
|
|
599
599
|
},
|
|
600
600
|
async run({ provider, remote }, scope) {
|
|
601
601
|
const ctx = await scope.services();
|
|
602
|
-
const { models } = ctx.providers.get(provider.id);
|
|
603
|
-
if (!remote) return
|
|
602
|
+
const { models, effectiveModels } = ctx.providers.get(provider.id);
|
|
603
|
+
if (!remote) return effectiveModels;
|
|
604
604
|
let listed;
|
|
605
605
|
try {
|
|
606
606
|
({listed} = await scope.progress((stage) => ctx.providers.modelChanges(provider.id, { onStage: modelStages(provider, stage) })));
|
|
@@ -711,7 +711,7 @@ const add$1 = command({
|
|
|
711
711
|
},
|
|
712
712
|
failed: (result) => result.failures.length > 0
|
|
713
713
|
});
|
|
714
|
-
const remove$
|
|
714
|
+
const remove$2 = command({
|
|
715
715
|
name: "remove",
|
|
716
716
|
aliases: ["rm"],
|
|
717
717
|
summary: "Remove models from a provider",
|
|
@@ -784,10 +784,10 @@ const model$1 = group({
|
|
|
784
784
|
aliases: ["models"],
|
|
785
785
|
summary: "Manage a provider's models",
|
|
786
786
|
children: [
|
|
787
|
-
list$
|
|
787
|
+
list$3,
|
|
788
788
|
pick$1,
|
|
789
789
|
add$1,
|
|
790
|
-
remove$
|
|
790
|
+
remove$2,
|
|
791
791
|
sync,
|
|
792
792
|
command({
|
|
793
793
|
name: "review",
|
|
@@ -870,122 +870,6 @@ const model$1 = group({
|
|
|
870
870
|
]
|
|
871
871
|
});
|
|
872
872
|
//#endregion
|
|
873
|
-
//#region src/cli/commands/paths.ts
|
|
874
|
-
const paths = command({
|
|
875
|
-
name: "paths",
|
|
876
|
-
summary: "Show where ohmyswitch keeps its files",
|
|
877
|
-
params: {},
|
|
878
|
-
run(_values, scope) {
|
|
879
|
-
const { dirs } = scope.session.config;
|
|
880
|
-
return {
|
|
881
|
-
...dirs,
|
|
882
|
-
plugins: join(dirs.config, "plugins.yml"),
|
|
883
|
-
pluginPatches: join(dirs.config, "plugins.patch.yml"),
|
|
884
|
-
store: join(dirs.data, "store.json")
|
|
885
|
-
};
|
|
886
|
-
},
|
|
887
|
-
render(result, out) {
|
|
888
|
-
out.fields([
|
|
889
|
-
["Providers", result.store],
|
|
890
|
-
["Plugins", result.plugins],
|
|
891
|
-
["Plugin patches", result.pluginPatches],
|
|
892
|
-
["Cache", result.cache],
|
|
893
|
-
["Logs (GUI)", result.logs]
|
|
894
|
-
]);
|
|
895
|
-
}
|
|
896
|
-
});
|
|
897
|
-
//#endregion
|
|
898
|
-
//#region src/cli/commands/plugin.ts
|
|
899
|
-
const lockedReason = "decided by plugins.patch.yml";
|
|
900
|
-
function stateOf(entry) {
|
|
901
|
-
switch (entry.state) {
|
|
902
|
-
case "active": return "enabled";
|
|
903
|
-
case "disabled": return "disabled";
|
|
904
|
-
case "failed": return `failed${entry.error ? `: ${entry.error}` : ""}`;
|
|
905
|
-
default: return "loading";
|
|
906
|
-
}
|
|
907
|
-
}
|
|
908
|
-
function warnFileErrors(out, plugins) {
|
|
909
|
-
if (plugins.fileError) out.warn(`${plugins.path} cannot be read: ${plugins.fileError}`);
|
|
910
|
-
if (plugins.patchError) out.warn(`${plugins.patchPath} cannot be read: ${plugins.patchError}`);
|
|
911
|
-
}
|
|
912
|
-
const list$1 = command({
|
|
913
|
-
name: "list",
|
|
914
|
-
aliases: ["ls"],
|
|
915
|
-
summary: "List plugins",
|
|
916
|
-
params: {},
|
|
917
|
-
async run(_values, scope) {
|
|
918
|
-
return (await scope.services()).plugins.list();
|
|
919
|
-
},
|
|
920
|
-
render(plugins, out) {
|
|
921
|
-
warnFileErrors(out, plugins);
|
|
922
|
-
out.table([
|
|
923
|
-
"PLUGIN",
|
|
924
|
-
"ID",
|
|
925
|
-
"STATE",
|
|
926
|
-
"NOTE"
|
|
927
|
-
], plugins.entries.map((entry) => [
|
|
928
|
-
entry.title,
|
|
929
|
-
entry.id,
|
|
930
|
-
stateOf(entry),
|
|
931
|
-
entry.locked ? lockedReason : ""
|
|
932
|
-
]));
|
|
933
|
-
}
|
|
934
|
-
});
|
|
935
|
-
async function pluginChoices(scope) {
|
|
936
|
-
const { entries } = await (await scope.services()).plugins.list();
|
|
937
|
-
return entries.map((entry) => ({
|
|
938
|
-
value: entry,
|
|
939
|
-
keys: [
|
|
940
|
-
entry.id,
|
|
941
|
-
entry.title,
|
|
942
|
-
entry.name
|
|
943
|
-
],
|
|
944
|
-
label: entry.title,
|
|
945
|
-
hint: stateOf(entry),
|
|
946
|
-
disabled: entry.locked ? lockedReason : void 0
|
|
947
|
-
}));
|
|
948
|
-
}
|
|
949
|
-
function switchCommand(enabled) {
|
|
950
|
-
return command({
|
|
951
|
-
name: enabled ? "enable" : "disable",
|
|
952
|
-
summary: `${enabled ? "Enable" : "Disable"} a plugin`,
|
|
953
|
-
params: { plugin: select$1({
|
|
954
|
-
summary: "Plugin",
|
|
955
|
-
positional: true,
|
|
956
|
-
placeholder: "plugin",
|
|
957
|
-
details: "Its id or name",
|
|
958
|
-
empty: "Every plugin is decided by plugins.patch.yml.",
|
|
959
|
-
choices: pluginChoices
|
|
960
|
-
}) },
|
|
961
|
-
async run({ plugin }, scope) {
|
|
962
|
-
const ctx = await scope.services();
|
|
963
|
-
const plugins = await ctx.plugins.list();
|
|
964
|
-
const broken = plugins.fileError ? plugins.path : plugins.patchError ? plugins.patchPath : void 0;
|
|
965
|
-
if (broken) throw new CliError("precondition_failed", `${broken} has errors; fix it first`);
|
|
966
|
-
await ctx.plugins.setEnabled(plugin.id, enabled);
|
|
967
|
-
return {
|
|
968
|
-
id: plugin.id,
|
|
969
|
-
title: plugin.title,
|
|
970
|
-
enabled
|
|
971
|
-
};
|
|
972
|
-
},
|
|
973
|
-
render(result, out) {
|
|
974
|
-
out.success(`${enabled ? "Enabled" : "Disabled"} ${result.title}.`);
|
|
975
|
-
}
|
|
976
|
-
});
|
|
977
|
-
}
|
|
978
|
-
const plugin = group({
|
|
979
|
-
name: "plugin",
|
|
980
|
-
aliases: ["plugins"],
|
|
981
|
-
summary: "Enable or disable plugins",
|
|
982
|
-
children: [
|
|
983
|
-
list$1,
|
|
984
|
-
switchCommand(true),
|
|
985
|
-
switchCommand(false)
|
|
986
|
-
]
|
|
987
|
-
});
|
|
988
|
-
//#endregion
|
|
989
873
|
//#region node_modules/.pnpm/zod@4.6.7/node_modules/zod/v4/core/util.js
|
|
990
874
|
function getEnumValues(entries) {
|
|
991
875
|
const numericValues = Object.values(entries).filter((v) => typeof v === "number");
|
|
@@ -6640,36 +6524,6 @@ function datetime(params) {
|
|
|
6640
6524
|
return /* @__PURE__ */ _isoDateTime(ZodISODateTime, params);
|
|
6641
6525
|
}
|
|
6642
6526
|
//#endregion
|
|
6643
|
-
//#region src/cli/framework/validate.ts
|
|
6644
|
-
const unitOf = (origin) => {
|
|
6645
|
-
if (origin === "string") return " characters";
|
|
6646
|
-
if (origin === "array" || origin === "set") return " items";
|
|
6647
|
-
return "";
|
|
6648
|
-
};
|
|
6649
|
-
/**
|
|
6650
|
-
* An English description of a validation issue. Core schemas carry messages for the GUI; the CLI words its own
|
|
6651
|
-
* from the issue code, so it reads the same in every command.
|
|
6652
|
-
*/
|
|
6653
|
-
function describeIssue(issue) {
|
|
6654
|
-
switch (issue.code) {
|
|
6655
|
-
case "too_small":
|
|
6656
|
-
if (issue.origin === "string" && Number(issue.minimum) <= 1) return "must not be empty";
|
|
6657
|
-
return `must be at least ${issue.minimum}${unitOf(issue.origin)}`;
|
|
6658
|
-
case "too_big": return `must be at most ${issue.maximum}${unitOf(issue.origin)}`;
|
|
6659
|
-
case "invalid_format": return issue.format === "url" ? "must be an http or https URL" : `must be a valid ${issue.format}`;
|
|
6660
|
-
case "invalid_type": return `must be ${issue.expected === "array" ? "a list" : `a ${issue.expected}`}`;
|
|
6661
|
-
case "invalid_value": return `must be one of ${issue.values.map(String).join(", ")}`;
|
|
6662
|
-
default: return "is invalid";
|
|
6663
|
-
}
|
|
6664
|
-
}
|
|
6665
|
-
/** Parse one parameter value with a core schema, or throw a ParamError describing the first issue. */
|
|
6666
|
-
function parseWith(schema, value) {
|
|
6667
|
-
const result = schema.safeParse(value);
|
|
6668
|
-
if (result.success) return result.data;
|
|
6669
|
-
const [issue] = result.error.issues;
|
|
6670
|
-
throw new ParamError(issue ? describeIssue(issue) : "is invalid");
|
|
6671
|
-
}
|
|
6672
|
-
//#endregion
|
|
6673
6527
|
//#region src/core/harnesses/types.ts
|
|
6674
6528
|
/**
|
|
6675
6529
|
* A harness id, such as `opencode`. Harnesses are registered at runtime, so the set is open; stored applications of
|
|
@@ -6740,6 +6594,13 @@ const modelInfo = object({
|
|
|
6740
6594
|
input: array(_enum(modelInputs)).optional(),
|
|
6741
6595
|
toolCall: boolean().optional()
|
|
6742
6596
|
});
|
|
6597
|
+
/** Both override scopes are limited to display name and token limits. */
|
|
6598
|
+
const modelOverride = modelInfo.pick({
|
|
6599
|
+
name: true,
|
|
6600
|
+
contextWindow: true,
|
|
6601
|
+
maxOutput: true
|
|
6602
|
+
});
|
|
6603
|
+
const providerModelOverride = modelOverride;
|
|
6743
6604
|
/** A provider as persisted. The API key is stored in plain text; harness configs hold it in plain text anyway. */
|
|
6744
6605
|
const storedProvider = object({
|
|
6745
6606
|
id: uuid(),
|
|
@@ -6750,7 +6611,10 @@ const storedProvider = object({
|
|
|
6750
6611
|
apiKey: string(),
|
|
6751
6612
|
/** An image or web page to take the icon from; unset uses the Base URL's site. */
|
|
6752
6613
|
iconUrl: url({ protocol: /^https?$/ }).optional(),
|
|
6614
|
+
/** Metadata snapshots; discovery and models.dev sync never modify the separate overrides. */
|
|
6753
6615
|
models: array(modelInfo),
|
|
6616
|
+
/** Kept when a model is deselected, so selecting it again restores its explicit settings. */
|
|
6617
|
+
modelOverrides: record$2(string().min(1).max(256), providerModelOverride).default({}),
|
|
6754
6618
|
/**
|
|
6755
6619
|
* The model ids the provider's model list held when the user last reviewed it. Models listed since are offered
|
|
6756
6620
|
* as new, and saved models that were listed but no longer are offered for removal. Unset until the list is first
|
|
@@ -6764,6 +6628,18 @@ const storedProvider = object({
|
|
|
6764
6628
|
harnesses: record$2(harnessId, object({ key: string().min(1).max(64) })).default({}),
|
|
6765
6629
|
createdAt: datetime()
|
|
6766
6630
|
});
|
|
6631
|
+
/** Apply explicit fields in order: snapshot → global → provider. */
|
|
6632
|
+
function effectiveModel(model, globalOverrides, providerOverrides = {}) {
|
|
6633
|
+
let result = model;
|
|
6634
|
+
for (const overrides of [globalOverrides, providerOverrides]) {
|
|
6635
|
+
const override = Object.hasOwn(overrides, model.id) ? overrides[model.id] : void 0;
|
|
6636
|
+
if (override) result = {
|
|
6637
|
+
...result,
|
|
6638
|
+
...Object.fromEntries(Object.entries(override).filter(([, value]) => value !== void 0))
|
|
6639
|
+
};
|
|
6640
|
+
}
|
|
6641
|
+
return result;
|
|
6642
|
+
}
|
|
6767
6643
|
//#endregion
|
|
6768
6644
|
//#region src/core/providers/schemas.ts
|
|
6769
6645
|
const providerIdInput = object({ providerId: uuid() });
|
|
@@ -6790,6 +6666,16 @@ const updateProviderInput = connectionInput.extend(providerIdInput.shape);
|
|
|
6790
6666
|
object({ providerIds: array(uuid()).max(1e3).refine((ids) => new Set(ids).size === ids.length, { error: "供应商顺序中有重复项" }) });
|
|
6791
6667
|
providerIdInput.extend({ models: array(modelInfo).max(2e3).transform((models) => [...new Map(models.map((model) => [model.id, model])).values()]) });
|
|
6792
6668
|
const modelId$1 = string().min(1).max(256);
|
|
6669
|
+
object({
|
|
6670
|
+
modelId: modelId$1,
|
|
6671
|
+
override: modelOverride.refine((value) => Object.values(value).some((field) => field !== void 0), { error: "请至少填写一项覆盖信息" })
|
|
6672
|
+
});
|
|
6673
|
+
object({ modelId: modelId$1 });
|
|
6674
|
+
/** Replace one model's provider overrides. An empty object restores inheritance for every field. */
|
|
6675
|
+
const providerModelOverrideInput = providerIdInput.extend({
|
|
6676
|
+
modelId: modelId$1,
|
|
6677
|
+
override: providerModelOverride.strict()
|
|
6678
|
+
});
|
|
6793
6679
|
providerIdInput.extend({
|
|
6794
6680
|
listed: array(modelInfo).max(2e3),
|
|
6795
6681
|
selected: array(modelId$1).max(2e3).transform((ids) => [...new Set(ids)])
|
|
@@ -6804,6 +6690,292 @@ object({ candidates: array(object({ sources: array(object({
|
|
|
6804
6690
|
key: string().min(1).max(64)
|
|
6805
6691
|
})).min(1).max(16) })).min(1, { error: "请选择要导入的供应商" }).max(200) });
|
|
6806
6692
|
//#endregion
|
|
6693
|
+
//#region src/cli/commands/override.ts
|
|
6694
|
+
const provider$1 = () => select$1({
|
|
6695
|
+
summary: "Override only this provider (default: global)",
|
|
6696
|
+
optional: true,
|
|
6697
|
+
ask: false,
|
|
6698
|
+
placeholder: "provider",
|
|
6699
|
+
details: "Its name or id",
|
|
6700
|
+
empty: noProviders,
|
|
6701
|
+
choices: providerChoices
|
|
6702
|
+
});
|
|
6703
|
+
const id = () => text$1({
|
|
6704
|
+
summary: "Model id (exact match)",
|
|
6705
|
+
positional: true,
|
|
6706
|
+
placeholder: "model",
|
|
6707
|
+
parse: (raw) => {
|
|
6708
|
+
if (!raw.trim() || raw.length > 256) throw new ParamError("Model id must contain 1 to 256 characters.");
|
|
6709
|
+
return raw;
|
|
6710
|
+
}
|
|
6711
|
+
});
|
|
6712
|
+
const tokens = (summary) => text$1({
|
|
6713
|
+
summary,
|
|
6714
|
+
optional: true,
|
|
6715
|
+
ask: false,
|
|
6716
|
+
parse: (raw) => {
|
|
6717
|
+
if (!/^\d+$/.test(raw) || !Number.isSafeInteger(Number(raw)) || Number(raw) < 1) throw new ParamError("Must be a positive integer.");
|
|
6718
|
+
return Number(raw);
|
|
6719
|
+
}
|
|
6720
|
+
});
|
|
6721
|
+
const override = group({
|
|
6722
|
+
name: "override",
|
|
6723
|
+
summary: "Manage global and provider model overrides",
|
|
6724
|
+
children: [
|
|
6725
|
+
command({
|
|
6726
|
+
name: "list",
|
|
6727
|
+
summary: "List global or provider model overrides",
|
|
6728
|
+
params: { provider: provider$1() },
|
|
6729
|
+
async run({ provider }, scope) {
|
|
6730
|
+
const ctx = await scope.services();
|
|
6731
|
+
return provider ? ctx.providers.get(provider.id).modelOverrides : ctx.providers.modelOverrides();
|
|
6732
|
+
},
|
|
6733
|
+
render(result, out) {
|
|
6734
|
+
out.table([
|
|
6735
|
+
"MODEL",
|
|
6736
|
+
"NAME",
|
|
6737
|
+
"CONTEXT",
|
|
6738
|
+
"OUTPUT"
|
|
6739
|
+
], Object.entries(result).map(([model, value]) => [
|
|
6740
|
+
model,
|
|
6741
|
+
value.name ?? "-",
|
|
6742
|
+
value.contextWindow?.toString() ?? "-",
|
|
6743
|
+
value.maxOutput?.toString() ?? "-"
|
|
6744
|
+
]));
|
|
6745
|
+
}
|
|
6746
|
+
}),
|
|
6747
|
+
command({
|
|
6748
|
+
name: "set",
|
|
6749
|
+
summary: "Replace the entire override for a model",
|
|
6750
|
+
params: {
|
|
6751
|
+
provider: provider$1(),
|
|
6752
|
+
model: id(),
|
|
6753
|
+
name: text$1({
|
|
6754
|
+
summary: "Display name",
|
|
6755
|
+
optional: true,
|
|
6756
|
+
ask: false,
|
|
6757
|
+
parse: (raw) => {
|
|
6758
|
+
if (!raw.trim() || raw.length > 256) throw new ParamError("Name must contain 1 to 256 characters.");
|
|
6759
|
+
return raw.trim();
|
|
6760
|
+
}
|
|
6761
|
+
}),
|
|
6762
|
+
context: tokens("Maximum context in tokens"),
|
|
6763
|
+
output: tokens("Maximum output in tokens")
|
|
6764
|
+
},
|
|
6765
|
+
async run({ provider, model, name, context, output }, scope) {
|
|
6766
|
+
const override = {
|
|
6767
|
+
...name !== void 0 && { name },
|
|
6768
|
+
...context !== void 0 && { contextWindow: context },
|
|
6769
|
+
...output !== void 0 && { maxOutput: output }
|
|
6770
|
+
};
|
|
6771
|
+
if (!Object.keys(override).length) throw new CliError("usage", "Specify at least one override field, or use \"override remove\" to restore inheritance.");
|
|
6772
|
+
const ctx = await scope.services();
|
|
6773
|
+
if (!provider) return ctx.providers.setModelOverride({
|
|
6774
|
+
modelId: model,
|
|
6775
|
+
override
|
|
6776
|
+
});
|
|
6777
|
+
if (!ctx.providers.get(provider.id).models.some((item) => item.id === model)) throw new CliError("usage", "This model is not saved in the provider. Add or select it first.");
|
|
6778
|
+
const saved = await ctx.providers.setProviderModelOverride(providerModelOverrideInput.parse({
|
|
6779
|
+
providerId: provider.id,
|
|
6780
|
+
modelId: model,
|
|
6781
|
+
override
|
|
6782
|
+
}));
|
|
6783
|
+
return {
|
|
6784
|
+
affected: 1,
|
|
6785
|
+
failures: saved.harnessFailures.map((failure) => ({
|
|
6786
|
+
providerId: saved.id,
|
|
6787
|
+
providerName: saved.name,
|
|
6788
|
+
...failure
|
|
6789
|
+
}))
|
|
6790
|
+
};
|
|
6791
|
+
},
|
|
6792
|
+
render(result, out) {
|
|
6793
|
+
out.success(`Saved override for ${result.affected} providers.`);
|
|
6794
|
+
for (const failure of result.failures) out.warn(`${failure.providerName} / ${failure.harnessId}: ${failure.message}`);
|
|
6795
|
+
},
|
|
6796
|
+
failed: (result) => result.failures.length > 0
|
|
6797
|
+
}),
|
|
6798
|
+
command({
|
|
6799
|
+
name: "remove",
|
|
6800
|
+
aliases: ["rm"],
|
|
6801
|
+
summary: "Remove an override and restore inheritance",
|
|
6802
|
+
params: {
|
|
6803
|
+
provider: provider$1(),
|
|
6804
|
+
model: id()
|
|
6805
|
+
},
|
|
6806
|
+
confirm: ({ provider, model }) => `Remove the ${provider ? `provider "${provider.name}"` : "global"} override for "${model}"?`,
|
|
6807
|
+
async run({ provider, model }, scope) {
|
|
6808
|
+
const ctx = await scope.services();
|
|
6809
|
+
if (!provider) return ctx.providers.removeModelOverride(model);
|
|
6810
|
+
const saved = await ctx.providers.setProviderModelOverride(providerModelOverrideInput.parse({
|
|
6811
|
+
providerId: provider.id,
|
|
6812
|
+
modelId: model,
|
|
6813
|
+
override: {}
|
|
6814
|
+
}));
|
|
6815
|
+
return {
|
|
6816
|
+
affected: 1,
|
|
6817
|
+
failures: saved.harnessFailures.map((failure) => ({
|
|
6818
|
+
providerId: saved.id,
|
|
6819
|
+
providerName: saved.name,
|
|
6820
|
+
...failure
|
|
6821
|
+
}))
|
|
6822
|
+
};
|
|
6823
|
+
},
|
|
6824
|
+
render(result, out) {
|
|
6825
|
+
out.success(`Removed override for ${result.affected} providers.`);
|
|
6826
|
+
for (const failure of result.failures) out.warn(`${failure.providerName} / ${failure.harnessId}: ${failure.message}`);
|
|
6827
|
+
},
|
|
6828
|
+
failed: (result) => result.failures.length > 0
|
|
6829
|
+
})
|
|
6830
|
+
]
|
|
6831
|
+
});
|
|
6832
|
+
//#endregion
|
|
6833
|
+
//#region src/cli/commands/paths.ts
|
|
6834
|
+
const paths = command({
|
|
6835
|
+
name: "paths",
|
|
6836
|
+
summary: "Show where ohmyswitch keeps its files",
|
|
6837
|
+
params: {},
|
|
6838
|
+
run(_values, scope) {
|
|
6839
|
+
const { dirs } = scope.session.config;
|
|
6840
|
+
return {
|
|
6841
|
+
...dirs,
|
|
6842
|
+
plugins: join(dirs.config, "plugins.yml"),
|
|
6843
|
+
pluginPatches: join(dirs.config, "plugins.patch.yml"),
|
|
6844
|
+
store: join(dirs.data, "store.json")
|
|
6845
|
+
};
|
|
6846
|
+
},
|
|
6847
|
+
render(result, out) {
|
|
6848
|
+
out.fields([
|
|
6849
|
+
["Providers", result.store],
|
|
6850
|
+
["Plugins", result.plugins],
|
|
6851
|
+
["Plugin patches", result.pluginPatches],
|
|
6852
|
+
["Cache", result.cache],
|
|
6853
|
+
["Logs (GUI)", result.logs]
|
|
6854
|
+
]);
|
|
6855
|
+
}
|
|
6856
|
+
});
|
|
6857
|
+
//#endregion
|
|
6858
|
+
//#region src/cli/commands/plugin.ts
|
|
6859
|
+
const lockedReason = "decided by plugins.patch.yml";
|
|
6860
|
+
function stateOf(entry) {
|
|
6861
|
+
switch (entry.state) {
|
|
6862
|
+
case "active": return "enabled";
|
|
6863
|
+
case "disabled": return "disabled";
|
|
6864
|
+
case "failed": return `failed${entry.error ? `: ${entry.error}` : ""}`;
|
|
6865
|
+
default: return "loading";
|
|
6866
|
+
}
|
|
6867
|
+
}
|
|
6868
|
+
function warnFileErrors(out, plugins) {
|
|
6869
|
+
if (plugins.fileError) out.warn(`${plugins.path} cannot be read: ${plugins.fileError}`);
|
|
6870
|
+
if (plugins.patchError) out.warn(`${plugins.patchPath} cannot be read: ${plugins.patchError}`);
|
|
6871
|
+
}
|
|
6872
|
+
const list$1 = command({
|
|
6873
|
+
name: "list",
|
|
6874
|
+
aliases: ["ls"],
|
|
6875
|
+
summary: "List plugins",
|
|
6876
|
+
params: {},
|
|
6877
|
+
async run(_values, scope) {
|
|
6878
|
+
return (await scope.services()).plugins.list();
|
|
6879
|
+
},
|
|
6880
|
+
render(plugins, out) {
|
|
6881
|
+
warnFileErrors(out, plugins);
|
|
6882
|
+
out.table([
|
|
6883
|
+
"PLUGIN",
|
|
6884
|
+
"ID",
|
|
6885
|
+
"STATE",
|
|
6886
|
+
"NOTE"
|
|
6887
|
+
], plugins.entries.map((entry) => [
|
|
6888
|
+
entry.title,
|
|
6889
|
+
entry.id,
|
|
6890
|
+
stateOf(entry),
|
|
6891
|
+
entry.locked ? lockedReason : ""
|
|
6892
|
+
]));
|
|
6893
|
+
}
|
|
6894
|
+
});
|
|
6895
|
+
async function pluginChoices(scope) {
|
|
6896
|
+
const { entries } = await (await scope.services()).plugins.list();
|
|
6897
|
+
return entries.map((entry) => ({
|
|
6898
|
+
value: entry,
|
|
6899
|
+
keys: [
|
|
6900
|
+
entry.id,
|
|
6901
|
+
entry.title,
|
|
6902
|
+
entry.name
|
|
6903
|
+
],
|
|
6904
|
+
label: entry.title,
|
|
6905
|
+
hint: stateOf(entry),
|
|
6906
|
+
disabled: entry.locked ? lockedReason : void 0
|
|
6907
|
+
}));
|
|
6908
|
+
}
|
|
6909
|
+
function switchCommand(enabled) {
|
|
6910
|
+
return command({
|
|
6911
|
+
name: enabled ? "enable" : "disable",
|
|
6912
|
+
summary: `${enabled ? "Enable" : "Disable"} a plugin`,
|
|
6913
|
+
params: { plugin: select$1({
|
|
6914
|
+
summary: "Plugin",
|
|
6915
|
+
positional: true,
|
|
6916
|
+
placeholder: "plugin",
|
|
6917
|
+
details: "Its id or name",
|
|
6918
|
+
empty: "Every plugin is decided by plugins.patch.yml.",
|
|
6919
|
+
choices: pluginChoices
|
|
6920
|
+
}) },
|
|
6921
|
+
async run({ plugin }, scope) {
|
|
6922
|
+
const ctx = await scope.services();
|
|
6923
|
+
const plugins = await ctx.plugins.list();
|
|
6924
|
+
const broken = plugins.fileError ? plugins.path : plugins.patchError ? plugins.patchPath : void 0;
|
|
6925
|
+
if (broken) throw new CliError("precondition_failed", `${broken} has errors; fix it first`);
|
|
6926
|
+
await ctx.plugins.setEnabled(plugin.id, enabled);
|
|
6927
|
+
return {
|
|
6928
|
+
id: plugin.id,
|
|
6929
|
+
title: plugin.title,
|
|
6930
|
+
enabled
|
|
6931
|
+
};
|
|
6932
|
+
},
|
|
6933
|
+
render(result, out) {
|
|
6934
|
+
out.success(`${enabled ? "Enabled" : "Disabled"} ${result.title}.`);
|
|
6935
|
+
}
|
|
6936
|
+
});
|
|
6937
|
+
}
|
|
6938
|
+
const plugin = group({
|
|
6939
|
+
name: "plugin",
|
|
6940
|
+
aliases: ["plugins"],
|
|
6941
|
+
summary: "Enable or disable plugins",
|
|
6942
|
+
children: [
|
|
6943
|
+
list$1,
|
|
6944
|
+
switchCommand(true),
|
|
6945
|
+
switchCommand(false)
|
|
6946
|
+
]
|
|
6947
|
+
});
|
|
6948
|
+
//#endregion
|
|
6949
|
+
//#region src/cli/framework/validate.ts
|
|
6950
|
+
const unitOf = (origin) => {
|
|
6951
|
+
if (origin === "string") return " characters";
|
|
6952
|
+
if (origin === "array" || origin === "set") return " items";
|
|
6953
|
+
return "";
|
|
6954
|
+
};
|
|
6955
|
+
/**
|
|
6956
|
+
* An English description of a validation issue. Core schemas carry messages for the GUI; the CLI words its own
|
|
6957
|
+
* from the issue code, so it reads the same in every command.
|
|
6958
|
+
*/
|
|
6959
|
+
function describeIssue(issue) {
|
|
6960
|
+
switch (issue.code) {
|
|
6961
|
+
case "too_small":
|
|
6962
|
+
if (issue.origin === "string" && Number(issue.minimum) <= 1) return "must not be empty";
|
|
6963
|
+
return `must be at least ${issue.minimum}${unitOf(issue.origin)}`;
|
|
6964
|
+
case "too_big": return `must be at most ${issue.maximum}${unitOf(issue.origin)}`;
|
|
6965
|
+
case "invalid_format": return issue.format === "url" ? "must be an http or https URL" : `must be a valid ${issue.format}`;
|
|
6966
|
+
case "invalid_type": return `must be ${issue.expected === "array" ? "a list" : `a ${issue.expected}`}`;
|
|
6967
|
+
case "invalid_value": return `must be one of ${issue.values.map(String).join(", ")}`;
|
|
6968
|
+
default: return "is invalid";
|
|
6969
|
+
}
|
|
6970
|
+
}
|
|
6971
|
+
/** Parse one parameter value with a core schema, or throw a ParamError describing the first issue. */
|
|
6972
|
+
function parseWith(schema, value) {
|
|
6973
|
+
const result = schema.safeParse(value);
|
|
6974
|
+
if (result.success) return result.data;
|
|
6975
|
+
const [issue] = result.error.issues;
|
|
6976
|
+
throw new ParamError(issue ? describeIssue(issue) : "is invalid");
|
|
6977
|
+
}
|
|
6978
|
+
//#endregion
|
|
6807
6979
|
//#region src/cli/commands/provider.ts
|
|
6808
6980
|
const modelId = string().trim().min(1).max(256);
|
|
6809
6981
|
const chosen = (scope) => scope.values.provider;
|
|
@@ -6867,7 +7039,7 @@ const show = command({
|
|
|
6867
7039
|
key,
|
|
6868
7040
|
state
|
|
6869
7041
|
})),
|
|
6870
|
-
models: detail.
|
|
7042
|
+
models: detail.effectiveModels
|
|
6871
7043
|
};
|
|
6872
7044
|
},
|
|
6873
7045
|
render(provider, out) {
|
|
@@ -7169,6 +7341,7 @@ const commands = group({
|
|
|
7169
7341
|
]
|
|
7170
7342
|
}),
|
|
7171
7343
|
model$1,
|
|
7344
|
+
override,
|
|
7172
7345
|
...applicationCommands,
|
|
7173
7346
|
harness,
|
|
7174
7347
|
plugin,
|
|
@@ -7233,7 +7406,7 @@ function appDirs(home, env, platform) {
|
|
|
7233
7406
|
function loadConfig() {
|
|
7234
7407
|
const home = homedir();
|
|
7235
7408
|
return {
|
|
7236
|
-
version: "0.
|
|
7409
|
+
version: "0.2.0",
|
|
7237
7410
|
dirs: appDirs(home, process.env, process.platform),
|
|
7238
7411
|
home,
|
|
7239
7412
|
env: { ...process.env }
|
|
@@ -13452,7 +13625,7 @@ var HarnessService = class extends Service {
|
|
|
13452
13625
|
}
|
|
13453
13626
|
for (const provider of applied) {
|
|
13454
13627
|
const { key } = provider.harnesses[harness.id];
|
|
13455
|
-
const state = files ? files.reduce((worst, { file, entries }) => worseState(worst, entryState(file, provider, key, entries)), "applied") : "unreadable";
|
|
13628
|
+
const state = files ? files.reduce((worst, { file, entries }) => worseState(worst, entryState(file, this.effective(provider), key, entries)), "applied") : "unreadable";
|
|
13456
13629
|
result[provider.id] = [...result[provider.id] ?? [], {
|
|
13457
13630
|
harnessId: harness.id,
|
|
13458
13631
|
key,
|
|
@@ -13471,7 +13644,7 @@ var HarnessService = class extends Service {
|
|
|
13471
13644
|
}
|
|
13472
13645
|
async applyTo(providerId, harness, replace) {
|
|
13473
13646
|
const harnessId = harness.id;
|
|
13474
|
-
const provider = this.provider(providerId);
|
|
13647
|
+
const provider = this.effective(this.provider(providerId));
|
|
13475
13648
|
if (!await isDirectory(harness.homeDir(this.environment))) throw new AppError("precondition_failed", `未发现 ${harness.name}`);
|
|
13476
13649
|
if (harness.unsupportedEndpoints?.includes(provider.endpoint)) throw new AppError("precondition_failed", `${harness.name} 不支持这个供应商的端点`);
|
|
13477
13650
|
const files = await harness.configFiles(this.environment);
|
|
@@ -13545,7 +13718,8 @@ var HarnessService = class extends Service {
|
|
|
13545
13718
|
});
|
|
13546
13719
|
}
|
|
13547
13720
|
/** Rewrite a provider in every harness it is applied to, after it changed. Failures are reported, not thrown. */
|
|
13548
|
-
async syncProvider(
|
|
13721
|
+
async syncProvider(original) {
|
|
13722
|
+
const provider = this.effective(original);
|
|
13549
13723
|
const failures = [];
|
|
13550
13724
|
for (const [harnessId, application] of this.applied(provider)) try {
|
|
13551
13725
|
await this.track(harnessId, async (harness) => {
|
|
@@ -13723,6 +13897,12 @@ var HarnessService = class extends Service {
|
|
|
13723
13897
|
return harness.homeDir(this.environment);
|
|
13724
13898
|
}
|
|
13725
13899
|
}
|
|
13900
|
+
effective(provider) {
|
|
13901
|
+
return {
|
|
13902
|
+
...provider,
|
|
13903
|
+
models: provider.models.map((model) => effectiveModel(model, this.ctx.store.data.modelOverrides, provider.modelOverrides))
|
|
13904
|
+
};
|
|
13905
|
+
}
|
|
13726
13906
|
/** Registered harnesses in display order. Harnesses are independent of each other, so they are sorted by name. */
|
|
13727
13907
|
harnesses() {
|
|
13728
13908
|
return [...this.registrations.values()].map((registration) => registration.harness).sort((a, b) => a.name.localeCompare(b.name) || a.id.localeCompare(b.id));
|
|
@@ -14273,7 +14453,7 @@ function push(map, key, entry) {
|
|
|
14273
14453
|
}
|
|
14274
14454
|
//#endregion
|
|
14275
14455
|
//#region src/core/models-dev/service.ts
|
|
14276
|
-
const maxAgeMs =
|
|
14456
|
+
const maxAgeMs = 36e5;
|
|
14277
14457
|
const maxBytes = 33554432;
|
|
14278
14458
|
const cacheFile = object({
|
|
14279
14459
|
/** Bumped whenever the reduced entry shape changes, so older caches are downloaded again. */
|
|
@@ -26260,15 +26440,74 @@ var ProviderService = class extends Service {
|
|
|
26260
26440
|
list() {
|
|
26261
26441
|
return this.ctx.store.data.providers.map((provider) => this.summary(provider));
|
|
26262
26442
|
}
|
|
26443
|
+
/** Model ids saved across providers, without connection details or credentials. */
|
|
26444
|
+
modelIds() {
|
|
26445
|
+
return [...new Set(this.ctx.store.data.providers.flatMap((provider) => provider.models.map((model) => model.id)))].sort((a, b) => a.localeCompare(b));
|
|
26446
|
+
}
|
|
26263
26447
|
/** The detail view of a provider, with its models. */
|
|
26264
26448
|
get(providerId) {
|
|
26265
26449
|
const provider = this.find(this.ctx.store.data.providers, providerId);
|
|
26266
26450
|
return {
|
|
26267
26451
|
...this.summary(provider),
|
|
26268
26452
|
models: provider.models,
|
|
26453
|
+
modelOverrides: provider.modelOverrides,
|
|
26454
|
+
globalModelOverrides: this.ctx.store.data.modelOverrides,
|
|
26455
|
+
inheritedModels: provider.models.map((model) => effectiveModel(model, this.ctx.store.data.modelOverrides)),
|
|
26456
|
+
effectiveModels: provider.models.map((model) => effectiveModel(model, this.ctx.store.data.modelOverrides, provider.modelOverrides)),
|
|
26269
26457
|
listedModels: provider.listedModels
|
|
26270
26458
|
};
|
|
26271
26459
|
}
|
|
26460
|
+
modelOverrides() {
|
|
26461
|
+
return this.ctx.store.data.modelOverrides;
|
|
26462
|
+
}
|
|
26463
|
+
async setModelOverride(input) {
|
|
26464
|
+
const providers = await this.ctx.store.update((draft) => {
|
|
26465
|
+
Object.defineProperty(draft.modelOverrides, input.modelId, {
|
|
26466
|
+
value: input.override,
|
|
26467
|
+
enumerable: true,
|
|
26468
|
+
configurable: true,
|
|
26469
|
+
writable: true
|
|
26470
|
+
});
|
|
26471
|
+
return draft.providers.filter((provider) => provider.models.some((model) => model.id === input.modelId));
|
|
26472
|
+
});
|
|
26473
|
+
return this.syncOverrideProviders(providers);
|
|
26474
|
+
}
|
|
26475
|
+
async removeModelOverride(modelId) {
|
|
26476
|
+
const providers = await this.ctx.store.update((draft) => {
|
|
26477
|
+
delete draft.modelOverrides[modelId];
|
|
26478
|
+
return draft.providers.filter((provider) => provider.models.some((model) => model.id === modelId));
|
|
26479
|
+
});
|
|
26480
|
+
return this.syncOverrideProviders(providers);
|
|
26481
|
+
}
|
|
26482
|
+
/** Replace local overrides without touching the snapshot; {} restores inheritance. */
|
|
26483
|
+
setProviderModelOverride(input) {
|
|
26484
|
+
return this.save(input.providerId, (provider) => {
|
|
26485
|
+
const fields = Object.entries(input.override).filter(([, value]) => value !== void 0);
|
|
26486
|
+
if (!fields.length) {
|
|
26487
|
+
delete provider.modelOverrides[input.modelId];
|
|
26488
|
+
return;
|
|
26489
|
+
}
|
|
26490
|
+
if (!provider.models.some((model) => model.id === input.modelId)) throw new AppError("not_found", "模型不存在或已移除");
|
|
26491
|
+
Object.defineProperty(provider.modelOverrides, input.modelId, {
|
|
26492
|
+
value: Object.fromEntries(fields),
|
|
26493
|
+
enumerable: true,
|
|
26494
|
+
configurable: true,
|
|
26495
|
+
writable: true
|
|
26496
|
+
});
|
|
26497
|
+
});
|
|
26498
|
+
}
|
|
26499
|
+
async syncOverrideProviders(providers) {
|
|
26500
|
+
const failures = [];
|
|
26501
|
+
for (const provider of providers) for (const failure of await this.ctx.harnesses.syncProvider(provider)) failures.push({
|
|
26502
|
+
providerId: provider.id,
|
|
26503
|
+
providerName: provider.name,
|
|
26504
|
+
...failure
|
|
26505
|
+
});
|
|
26506
|
+
return {
|
|
26507
|
+
affected: providers.length,
|
|
26508
|
+
failures
|
|
26509
|
+
};
|
|
26510
|
+
}
|
|
26272
26511
|
/** The connection fields for the editor, the only place the API key is returned. */
|
|
26273
26512
|
connection(providerId) {
|
|
26274
26513
|
const { name, baseUrl, apiKey, endpoint, iconUrl } = this.find(this.ctx.store.data.providers, providerId);
|
|
@@ -26400,6 +26639,7 @@ var ProviderService = class extends Service {
|
|
|
26400
26639
|
name,
|
|
26401
26640
|
...connection,
|
|
26402
26641
|
models,
|
|
26642
|
+
modelOverrides: {},
|
|
26403
26643
|
harnesses: applications,
|
|
26404
26644
|
createdAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
26405
26645
|
};
|
|
@@ -26467,6 +26707,7 @@ var ProviderService = class extends Service {
|
|
|
26467
26707
|
apiKey: input.apiKey,
|
|
26468
26708
|
...input.iconUrl ? { iconUrl: input.iconUrl } : {},
|
|
26469
26709
|
models,
|
|
26710
|
+
modelOverrides: {},
|
|
26470
26711
|
...listed ? { listedModels: listed.map((model) => model.id) } : {},
|
|
26471
26712
|
harnesses: {},
|
|
26472
26713
|
createdAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
@@ -26606,15 +26847,17 @@ var ProviderService = class extends Service {
|
|
|
26606
26847
|
//#endregion
|
|
26607
26848
|
//#region src/core/store/store.ts
|
|
26608
26849
|
const storeFile = object({
|
|
26609
|
-
version: literal(
|
|
26610
|
-
providers: array(storedProvider)
|
|
26850
|
+
version: literal(5),
|
|
26851
|
+
providers: array(storedProvider),
|
|
26852
|
+
modelOverrides: record$2(string().min(1).max(256), modelOverride)
|
|
26611
26853
|
});
|
|
26612
26854
|
const fileName = "store.json";
|
|
26613
26855
|
/** Several writes in a row are read once, after the file has been quiet this long. */
|
|
26614
26856
|
const reloadDelayMs = 100;
|
|
26615
26857
|
const emptyStore = () => ({
|
|
26616
|
-
version:
|
|
26617
|
-
providers: []
|
|
26858
|
+
version: 5,
|
|
26859
|
+
providers: [],
|
|
26860
|
+
modelOverrides: {}
|
|
26618
26861
|
});
|
|
26619
26862
|
/**
|
|
26620
26863
|
* Upgrade an older store file in place, one version at a time. A version newer than this build is left alone,
|
|
@@ -26625,6 +26868,11 @@ function migrate(raw) {
|
|
|
26625
26868
|
const data = raw;
|
|
26626
26869
|
if (data.version === 1) data.version = 2;
|
|
26627
26870
|
if (data.version === 2) data.version = 3;
|
|
26871
|
+
if (data.version === 3) Object.assign(data, {
|
|
26872
|
+
version: 4,
|
|
26873
|
+
modelOverrides: {}
|
|
26874
|
+
});
|
|
26875
|
+
if (data.version === 4) data.version = 5;
|
|
26628
26876
|
return data;
|
|
26629
26877
|
}
|
|
26630
26878
|
/**
|
package/package.json
CHANGED