openclaw-plugin-model-fetch 0.2.5 → 0.2.7
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-CN.md +7 -1
- package/README.md +7 -1
- package/dist/contract.d.ts +11 -0
- package/dist/control-ui/{bfc77779ea04b85bc02fc16f096d90b1c281194d30a76ce570e7fd2afa6e95d2 → f8e122768109836f1d9d9a6b4d177c8e9fb5f8b0bc4778c799b566c3dd7c1fd3}/index.css +1 -1
- package/dist/control-ui/f8e122768109836f1d9d9a6b4d177c8e9fb5f8b0bc4778c799b566c3dd7c1fd3/index.js +163 -0
- package/dist/control-ui-tab.d.ts +31 -0
- package/dist/control-ui-tab.js +42 -0
- package/dist/index.js +2 -0
- package/dist/model-fetch.d.ts +31 -0
- package/dist/model-fetch.js +240 -0
- package/dist/operations.d.ts +1 -1
- package/dist/operations.js +106 -7
- package/dist/schemas.d.ts +37 -0
- package/dist/schemas.js +23 -0
- package/openclaw.plugin.json +7 -3
- package/package.json +2 -2
- package/dist/control-ui/bfc77779ea04b85bc02fc16f096d90b1c281194d30a76ce570e7fd2afa6e95d2/index.js +0 -163
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { OpenClawPluginApi } from "openclaw/plugin-sdk/core";
|
|
2
|
+
export declare const CONTROL_UI_TAB_ID = "model-fetch";
|
|
3
|
+
type ControlUiTabDescriptor = {
|
|
4
|
+
surface: "tab";
|
|
5
|
+
id: string;
|
|
6
|
+
label: string;
|
|
7
|
+
description?: string;
|
|
8
|
+
icon?: string;
|
|
9
|
+
group?: "control" | "agent";
|
|
10
|
+
order?: number;
|
|
11
|
+
requiredScopes?: string[];
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* Descriptor advertised to every Gateway dashboard client.
|
|
15
|
+
*
|
|
16
|
+
* The browser Control UI renders the plugin's own navigation entry, but the
|
|
17
|
+
* Android and iOS apps only learn about plugins from the `controlUiTabs`
|
|
18
|
+
* payload in the Gateway hello message. Without a descriptor the plugin is
|
|
19
|
+
* invisible on those clients.
|
|
20
|
+
*/
|
|
21
|
+
export declare const controlUiTabDescriptor: ControlUiTabDescriptor;
|
|
22
|
+
/**
|
|
23
|
+
* Register the tab descriptor on hosts that support it.
|
|
24
|
+
*
|
|
25
|
+
* `api.session.controls.registerControlUiDescriptor` is the current entry
|
|
26
|
+
* point; the flat `api.registerControlUiDescriptor` is the deprecated
|
|
27
|
+
* fallback kept for older OpenClaw releases. Hosts that support neither are
|
|
28
|
+
* left untouched instead of failing plugin activation.
|
|
29
|
+
*/
|
|
30
|
+
export declare function registerControlUiTab(api: OpenClawPluginApi): boolean;
|
|
31
|
+
export {};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
export const CONTROL_UI_TAB_ID = "model-fetch";
|
|
2
|
+
/**
|
|
3
|
+
* Descriptor advertised to every Gateway dashboard client.
|
|
4
|
+
*
|
|
5
|
+
* The browser Control UI renders the plugin's own navigation entry, but the
|
|
6
|
+
* Android and iOS apps only learn about plugins from the `controlUiTabs`
|
|
7
|
+
* payload in the Gateway hello message. Without a descriptor the plugin is
|
|
8
|
+
* invisible on those clients.
|
|
9
|
+
*/
|
|
10
|
+
export const controlUiTabDescriptor = {
|
|
11
|
+
surface: "tab",
|
|
12
|
+
id: CONTROL_UI_TAB_ID,
|
|
13
|
+
label: "Model Fetch",
|
|
14
|
+
description: "Fetch and refresh provider model lists into OpenClaw config.",
|
|
15
|
+
icon: "download",
|
|
16
|
+
group: "control",
|
|
17
|
+
order: 620,
|
|
18
|
+
requiredScopes: ["operator.read"],
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* Register the tab descriptor on hosts that support it.
|
|
22
|
+
*
|
|
23
|
+
* `api.session.controls.registerControlUiDescriptor` is the current entry
|
|
24
|
+
* point; the flat `api.registerControlUiDescriptor` is the deprecated
|
|
25
|
+
* fallback kept for older OpenClaw releases. Hosts that support neither are
|
|
26
|
+
* left untouched instead of failing plugin activation.
|
|
27
|
+
*/
|
|
28
|
+
export function registerControlUiTab(api) {
|
|
29
|
+
const controls = api.session?.controls;
|
|
30
|
+
const grouped = controls?.registerControlUiDescriptor;
|
|
31
|
+
if (typeof grouped === "function") {
|
|
32
|
+
grouped.call(controls, controlUiTabDescriptor);
|
|
33
|
+
return true;
|
|
34
|
+
}
|
|
35
|
+
const legacy = api
|
|
36
|
+
.registerControlUiDescriptor;
|
|
37
|
+
if (typeof legacy === "function") {
|
|
38
|
+
legacy.call(api, controlUiTabDescriptor);
|
|
39
|
+
return true;
|
|
40
|
+
}
|
|
41
|
+
return false;
|
|
42
|
+
}
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { defineToolPlugin } from "openclaw/plugin-sdk/tool-plugin";
|
|
2
|
+
import { registerControlUiTab } from "./control-ui-tab.js";
|
|
2
3
|
import { createModelFetchTool, MODEL_FETCH_TOOL_NAME, modelFetchConfigSchema, modelFetchParameters, } from "./model-fetch.js";
|
|
3
4
|
import { registerModelFetchOperations } from "./operations.js";
|
|
4
5
|
const entry = defineToolPlugin({
|
|
@@ -19,6 +20,7 @@ const entry = defineToolPlugin({
|
|
|
19
20
|
const registerTools = entry.register;
|
|
20
21
|
entry.register = (api) => {
|
|
21
22
|
registerTools?.(api);
|
|
23
|
+
registerControlUiTab(api);
|
|
22
24
|
registerModelFetchOperations(api, {
|
|
23
25
|
factoryConfig: api.pluginConfig,
|
|
24
26
|
});
|
package/dist/model-fetch.d.ts
CHANGED
|
@@ -60,6 +60,17 @@ export declare function mergeModels({ existing, selected, filter, }: {
|
|
|
60
60
|
selected: ModelDefinitionLike[];
|
|
61
61
|
filter: ModelFilterConfig;
|
|
62
62
|
}): ModelMergeResult;
|
|
63
|
+
/**
|
|
64
|
+
* Rewrites the explicit model references that name `fromRef` to `toRef`.
|
|
65
|
+
*
|
|
66
|
+
* The reference collector only reads, so the walk is repeated here with a
|
|
67
|
+
* writable setter. `forEachExplicitModelReference` reports the exact path of
|
|
68
|
+
* every reference it finds, so the setter mirrors that path shape.
|
|
69
|
+
*/
|
|
70
|
+
export declare function rewriteModelReferences(draft: unknown, fromRef: string, toRef: string): {
|
|
71
|
+
changed: boolean;
|
|
72
|
+
paths: string[];
|
|
73
|
+
};
|
|
63
74
|
/**
|
|
64
75
|
* Reports which of `modelIds` are still named by a non-agent model consumer,
|
|
65
76
|
* together with the config paths that reference them.
|
|
@@ -78,6 +89,26 @@ export declare function applyModelReferenceProtection({ existing, merged, provid
|
|
|
78
89
|
models: ModelDefinitionLike[];
|
|
79
90
|
protectedModels: ProtectedModel[];
|
|
80
91
|
};
|
|
92
|
+
/**
|
|
93
|
+
* Finds removal candidates that only differ from a surviving model by letter
|
|
94
|
+
* case.
|
|
95
|
+
*
|
|
96
|
+
* A provider list can hold both `auto/MiniMax-M3` and `auto/minimax-m3`. The
|
|
97
|
+
* filter keeps one of them and drops the other, so the two entries point at
|
|
98
|
+
* the same remote model and the deletion plan can say so instead of leaving
|
|
99
|
+
* the user with two look-alike ids.
|
|
100
|
+
*/
|
|
101
|
+
export declare function collectCaseVariantRelations(removedIds: readonly string[], survivingIds: readonly string[]): {
|
|
102
|
+
removedId: string;
|
|
103
|
+
survivingModelId: string;
|
|
104
|
+
}[];
|
|
105
|
+
/**
|
|
106
|
+
* The model OpenClaw falls back to when an agent has no explicit selection.
|
|
107
|
+
*
|
|
108
|
+
* Agents can override the default, so the first agent that names a primary
|
|
109
|
+
* model wins. The value is the bare `provider/model` reference.
|
|
110
|
+
*/
|
|
111
|
+
export declare function resolveDefaultModelRef(config: unknown): string | undefined;
|
|
81
112
|
export declare function buildModelsEndpoint(providerId: string, provider: ProviderLike): string;
|
|
82
113
|
export declare function mapFetchedRows(providerId: string, provider: ProviderLike, rows: unknown[]): ModelDefinitionLike[];
|
|
83
114
|
type GuardedFetch = (params: GuardedFetchOptions) => Promise<GuardedFetchResult>;
|
package/dist/model-fetch.js
CHANGED
|
@@ -413,6 +413,167 @@ function forEachExplicitModelReference(config, visit) {
|
|
|
413
413
|
}
|
|
414
414
|
}
|
|
415
415
|
}
|
|
416
|
+
/**
|
|
417
|
+
* Rewrites the explicit model references that name `fromRef` to `toRef`.
|
|
418
|
+
*
|
|
419
|
+
* The reference collector only reads, so the walk is repeated here with a
|
|
420
|
+
* writable setter. `forEachExplicitModelReference` reports the exact path of
|
|
421
|
+
* every reference it finds, so the setter mirrors that path shape.
|
|
422
|
+
*/
|
|
423
|
+
export function rewriteModelReferences(draft, fromRef, toRef) {
|
|
424
|
+
if (!isRecord(draft))
|
|
425
|
+
return { changed: false, paths: [] };
|
|
426
|
+
const source = fromRef.trim();
|
|
427
|
+
const target = toRef.trim();
|
|
428
|
+
if (!source || !target || source === target)
|
|
429
|
+
return { changed: false, paths: [] };
|
|
430
|
+
const changed = [];
|
|
431
|
+
const writeScalar = (container, field, basePath) => {
|
|
432
|
+
if (!isRecord(container))
|
|
433
|
+
return;
|
|
434
|
+
const current = container[field];
|
|
435
|
+
if (typeof current !== "string" || current.trim() !== source)
|
|
436
|
+
return;
|
|
437
|
+
container[field] = target;
|
|
438
|
+
changed.push(`${basePath}.${field}`);
|
|
439
|
+
};
|
|
440
|
+
const writeSetting = (container, field, basePath) => {
|
|
441
|
+
if (!isRecord(container))
|
|
442
|
+
return;
|
|
443
|
+
const current = container[field];
|
|
444
|
+
if (typeof current === "string") {
|
|
445
|
+
if (current.trim() !== source)
|
|
446
|
+
return;
|
|
447
|
+
container[field] = target;
|
|
448
|
+
changed.push(`${basePath}.${field}`);
|
|
449
|
+
return;
|
|
450
|
+
}
|
|
451
|
+
if (!isRecord(current))
|
|
452
|
+
return;
|
|
453
|
+
if (typeof current.primary === "string" && current.primary.trim() === source) {
|
|
454
|
+
current.primary = target;
|
|
455
|
+
changed.push(`${basePath}.${field}.primary`);
|
|
456
|
+
}
|
|
457
|
+
const fallbacks = current.fallbacks;
|
|
458
|
+
if (!Array.isArray(fallbacks))
|
|
459
|
+
return;
|
|
460
|
+
fallbacks.forEach((fallback, index) => {
|
|
461
|
+
if (typeof fallback !== "string" || fallback.trim() !== source)
|
|
462
|
+
return;
|
|
463
|
+
fallbacks[index] = target;
|
|
464
|
+
changed.push(`${basePath}.${field}.fallbacks[${index}]`);
|
|
465
|
+
});
|
|
466
|
+
};
|
|
467
|
+
const tools = isRecord(draft.tools) ? draft.tools : undefined;
|
|
468
|
+
if (tools) {
|
|
469
|
+
const exec = asOptionalRecord(tools.exec);
|
|
470
|
+
const reviewer = asOptionalRecord(exec?.reviewer);
|
|
471
|
+
if (isRecord(reviewer))
|
|
472
|
+
writeSetting(reviewer, "model", "tools.exec.reviewer");
|
|
473
|
+
if (isRecord(tools.media)) {
|
|
474
|
+
for (const capability of ["image", "audio", "video"]) {
|
|
475
|
+
const media = asOptionalRecord(tools.media[capability]);
|
|
476
|
+
if (!media)
|
|
477
|
+
continue;
|
|
478
|
+
writeScalar(media, "preferredModel", `tools.media.${capability}`);
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
const agents = isRecord(draft.agents) ? draft.agents : undefined;
|
|
483
|
+
const defaults = asOptionalRecord(agents?.defaults);
|
|
484
|
+
if (defaults)
|
|
485
|
+
rewriteAgentModelReferences(defaults, "agents.defaults", false, writeScalar, writeSetting);
|
|
486
|
+
if (agents && isRecord(agents.entries)) {
|
|
487
|
+
for (const agentId of Object.keys(agents.entries).sort((left, right) => left.localeCompare(right))) {
|
|
488
|
+
const entry = agents.entries[agentId];
|
|
489
|
+
if (!isRecord(entry))
|
|
490
|
+
continue;
|
|
491
|
+
rewriteAgentModelReferences(entry, `agents.entries.${agentId}`, true, writeScalar, writeSetting);
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
else if (agents && Array.isArray(agents.list)) {
|
|
495
|
+
agents.list.forEach((entry, index) => {
|
|
496
|
+
if (!isRecord(entry))
|
|
497
|
+
return;
|
|
498
|
+
rewriteAgentModelReferences(entry, `agents.list[${index}]`, true, writeScalar, writeSetting);
|
|
499
|
+
});
|
|
500
|
+
}
|
|
501
|
+
const tts = asOptionalRecord(draft.tts);
|
|
502
|
+
if (tts)
|
|
503
|
+
writeSetting(tts, "summaryModel", "tts");
|
|
504
|
+
const hooks = isRecord(draft.hooks) ? draft.hooks : undefined;
|
|
505
|
+
if (hooks) {
|
|
506
|
+
if (isRecord(hooks.gmail))
|
|
507
|
+
writeScalar(hooks.gmail, "model", "hooks.gmail");
|
|
508
|
+
if (Array.isArray(hooks.mappings)) {
|
|
509
|
+
hooks.mappings.forEach((mapping, index) => {
|
|
510
|
+
if (!isRecord(mapping))
|
|
511
|
+
return;
|
|
512
|
+
writeScalar(mapping, "model", `hooks.mappings[${index}]`);
|
|
513
|
+
});
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
const channels = isRecord(draft.channels) ? draft.channels : undefined;
|
|
517
|
+
if (channels) {
|
|
518
|
+
const modelByChannel = isRecord(channels.modelByChannel) ? channels.modelByChannel : undefined;
|
|
519
|
+
if (modelByChannel) {
|
|
520
|
+
for (const channelId of Object.keys(modelByChannel).sort((left, right) => left.localeCompare(right))) {
|
|
521
|
+
const channelMap = modelByChannel[channelId];
|
|
522
|
+
if (!isRecord(channelMap))
|
|
523
|
+
continue;
|
|
524
|
+
for (const targetId of Object.keys(channelMap).sort((left, right) => left.localeCompare(right))) {
|
|
525
|
+
writeSetting(channelMap, targetId, `channels.modelByChannel.${channelId}`);
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
const discord = asOptionalRecord(channels.discord);
|
|
530
|
+
if (discord) {
|
|
531
|
+
rewriteDiscordVoiceModelReferences(discord.voice, "channels.discord.voice", writeScalar, writeSetting);
|
|
532
|
+
if (isRecord(discord.accounts)) {
|
|
533
|
+
for (const accountId of Object.keys(discord.accounts).sort((left, right) => left.localeCompare(right))) {
|
|
534
|
+
rewriteDiscordVoiceModelReferences(asOptionalRecord(discord.accounts[accountId])?.voice, `channels.discord.accounts.${accountId}.voice`, writeScalar, writeSetting);
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
return { changed: changed.length > 0, paths: changed.sort((left, right) => left.localeCompare(right)) };
|
|
540
|
+
}
|
|
541
|
+
function rewriteAgentModelReferences(agent, basePath, includeEntrySelectors, writeScalar, writeSetting) {
|
|
542
|
+
writeSetting(agent, "model", basePath);
|
|
543
|
+
writeScalar(agent, "utilityModel", basePath);
|
|
544
|
+
writeScalar(agent, "decisionModel", basePath);
|
|
545
|
+
writeSetting(agent, "imageModel", basePath);
|
|
546
|
+
writeSetting(agent, "voiceModel", basePath);
|
|
547
|
+
writeSetting(agent, "pdfModel", basePath);
|
|
548
|
+
const mediaModels = asOptionalRecord(agent.mediaModels);
|
|
549
|
+
if (mediaModels) {
|
|
550
|
+
for (const capability of ["image", "video", "music"]) {
|
|
551
|
+
writeSetting(mediaModels, capability, `${basePath}.mediaModels`);
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
const cyberFailover = asOptionalRecord(asOptionalRecord(agent.embeddedAgent)?.cyberFailover);
|
|
555
|
+
if (cyberFailover)
|
|
556
|
+
writeSetting(cyberFailover, "model", `${basePath}.embeddedAgent.cyberFailover`);
|
|
557
|
+
writeSetting(asOptionalRecord(agent.heartbeat), "model", `${basePath}.heartbeat`);
|
|
558
|
+
writeSetting(asOptionalRecord(agent.subagents), "model", `${basePath}.subagents`);
|
|
559
|
+
const compaction = asOptionalRecord(agent.compaction);
|
|
560
|
+
if (compaction) {
|
|
561
|
+
writeSetting(compaction, "model", `${basePath}.compaction`);
|
|
562
|
+
writeSetting(asOptionalRecord(compaction.memoryFlush), "model", `${basePath}.compaction.memoryFlush`);
|
|
563
|
+
}
|
|
564
|
+
if (!includeEntrySelectors)
|
|
565
|
+
return;
|
|
566
|
+
const exec = asOptionalRecord(asOptionalRecord(agent.tools)?.exec);
|
|
567
|
+
writeSetting(asOptionalRecord(exec?.reviewer), "model", `${basePath}.tools.exec.reviewer`);
|
|
568
|
+
writeSetting(asOptionalRecord(agent.tts), "summaryModel", `${basePath}.tts`);
|
|
569
|
+
}
|
|
570
|
+
function rewriteDiscordVoiceModelReferences(voice, basePath, writeScalar, writeSetting) {
|
|
571
|
+
const voiceConfig = asOptionalRecord(voice);
|
|
572
|
+
if (!voiceConfig)
|
|
573
|
+
return;
|
|
574
|
+
writeSetting(voiceConfig, "model", basePath);
|
|
575
|
+
writeSetting(asOptionalRecord(voiceConfig.tts), "summaryModel", `${basePath}.tts`);
|
|
576
|
+
}
|
|
416
577
|
function visitAgentModelReferences(path, agent, includeEntrySelectors, visit) {
|
|
417
578
|
readModelRefSetting(agent.model, `${path}.model`, visit);
|
|
418
579
|
readModelRefScalar(agent.utilityModel, `${path}.utilityModel`, visit);
|
|
@@ -507,6 +668,85 @@ export function applyModelReferenceProtection({ existing, merged, providerId, co
|
|
|
507
668
|
}
|
|
508
669
|
return { models, protectedModels };
|
|
509
670
|
}
|
|
671
|
+
/**
|
|
672
|
+
* Finds removal candidates that only differ from a surviving model by letter
|
|
673
|
+
* case.
|
|
674
|
+
*
|
|
675
|
+
* A provider list can hold both `auto/MiniMax-M3` and `auto/minimax-m3`. The
|
|
676
|
+
* filter keeps one of them and drops the other, so the two entries point at
|
|
677
|
+
* the same remote model and the deletion plan can say so instead of leaving
|
|
678
|
+
* the user with two look-alike ids.
|
|
679
|
+
*/
|
|
680
|
+
export function collectCaseVariantRelations(removedIds, survivingIds) {
|
|
681
|
+
const byLowerId = new Map();
|
|
682
|
+
for (const id of survivingIds) {
|
|
683
|
+
const key = id.toLowerCase();
|
|
684
|
+
const bucket = byLowerId.get(key);
|
|
685
|
+
if (bucket)
|
|
686
|
+
bucket.push(id);
|
|
687
|
+
else
|
|
688
|
+
byLowerId.set(key, [id]);
|
|
689
|
+
}
|
|
690
|
+
const relations = [];
|
|
691
|
+
for (const removedId of removedIds) {
|
|
692
|
+
const bucket = byLowerId.get(removedId.toLowerCase());
|
|
693
|
+
if (!bucket)
|
|
694
|
+
continue;
|
|
695
|
+
const match = bucket.find((id) => id !== removedId);
|
|
696
|
+
if (!match)
|
|
697
|
+
continue;
|
|
698
|
+
relations.push({ removedId, survivingModelId: match });
|
|
699
|
+
}
|
|
700
|
+
return relations.sort((left, right) => left.removedId.localeCompare(right.removedId));
|
|
701
|
+
}
|
|
702
|
+
/**
|
|
703
|
+
* The model OpenClaw falls back to when an agent has no explicit selection.
|
|
704
|
+
*
|
|
705
|
+
* Agents can override the default, so the first agent that names a primary
|
|
706
|
+
* model wins. The value is the bare `provider/model` reference.
|
|
707
|
+
*/
|
|
708
|
+
export function resolveDefaultModelRef(config) {
|
|
709
|
+
if (!isRecord(config))
|
|
710
|
+
return undefined;
|
|
711
|
+
const agents = isRecord(config.agents) ? config.agents : undefined;
|
|
712
|
+
if (!agents)
|
|
713
|
+
return undefined;
|
|
714
|
+
const readPrimary = (agent) => {
|
|
715
|
+
if (!isRecord(agent))
|
|
716
|
+
return undefined;
|
|
717
|
+
const model = agent.model;
|
|
718
|
+
if (typeof model === "string") {
|
|
719
|
+
const trimmed = model.trim();
|
|
720
|
+
return trimmed ? trimmed : undefined;
|
|
721
|
+
}
|
|
722
|
+
if (!isRecord(model))
|
|
723
|
+
return undefined;
|
|
724
|
+
const primary = model.primary;
|
|
725
|
+
if (typeof primary !== "string")
|
|
726
|
+
return undefined;
|
|
727
|
+
const trimmed = primary.trim();
|
|
728
|
+
return trimmed ? trimmed : undefined;
|
|
729
|
+
};
|
|
730
|
+
const defaults = asOptionalRecord(agents.defaults);
|
|
731
|
+
const defaultPrimary = readPrimary(defaults);
|
|
732
|
+
if (defaultPrimary)
|
|
733
|
+
return defaultPrimary;
|
|
734
|
+
if (isRecord(agents.entries)) {
|
|
735
|
+
for (const agentId of Object.keys(agents.entries).sort((left, right) => left.localeCompare(right))) {
|
|
736
|
+
const primary = readPrimary(agents.entries[agentId]);
|
|
737
|
+
if (primary)
|
|
738
|
+
return primary;
|
|
739
|
+
}
|
|
740
|
+
}
|
|
741
|
+
if (Array.isArray(agents.list)) {
|
|
742
|
+
for (const entry of agents.list) {
|
|
743
|
+
const primary = readPrimary(entry);
|
|
744
|
+
if (primary)
|
|
745
|
+
return primary;
|
|
746
|
+
}
|
|
747
|
+
}
|
|
748
|
+
return undefined;
|
|
749
|
+
}
|
|
510
750
|
function normalizeBaseUrl(baseUrl) {
|
|
511
751
|
return baseUrl.trim().replace(/\/+$/, "");
|
|
512
752
|
}
|
package/dist/operations.d.ts
CHANGED
|
@@ -23,7 +23,7 @@ export declare function createModelFetchOperations({ api, factoryConfig, fetchMo
|
|
|
23
23
|
fetchModels: (providerId: string) => Promise<FetchModelsOutput>;
|
|
24
24
|
saveStrategy: ({ provider, filter, targetAgents, }: SaveStrategyInput) => Promise<SaveStrategyOutput>;
|
|
25
25
|
previewApply: (provider: string, scope?: SessionSelectionScope) => Promise<PreviewApplyOutput>;
|
|
26
|
-
applyStrategy: ({ provider, fingerprint, expectedRemovals, targetAgents, }: ApplyStrategyInput, scope?: SessionSelectionScope) => Promise<ApplyStrategyOutput>;
|
|
26
|
+
applyStrategy: ({ provider, fingerprint, expectedRemovals, referenceRewrites, targetAgents, }: ApplyStrategyInput, scope?: SessionSelectionScope) => Promise<ApplyStrategyOutput>;
|
|
27
27
|
refreshProvider: (params: ModelFetchParams) => Promise<{
|
|
28
28
|
provider: string;
|
|
29
29
|
fetched: number;
|
package/dist/operations.js
CHANGED
|
@@ -2,7 +2,35 @@ import { createHash } from "node:crypto";
|
|
|
2
2
|
import { Value } from "typebox/value";
|
|
3
3
|
import { getSessionEntryAsync, listSessionEntries, patchSessionEntry, } from "openclaw/plugin-sdk/session-store-runtime";
|
|
4
4
|
import { modelFetchContract } from "./contract.js";
|
|
5
|
-
import { applyModelReferenceProtection, collectModelReferenceUsages, fetchProviderModels, getProvider, mergeModels, resolveApiKey, resolveProviderFilter, resolveProviderTargetAgents, runModelFetch, selectFetchedModels, syncAgentModelEntries, syncConfiguredModelPolicyAllow, } from "./model-fetch.js";
|
|
5
|
+
import { applyModelReferenceProtection, collectCaseVariantRelations, collectModelReferenceUsages, fetchProviderModels, getProvider, mergeModels, resolveDefaultModelRef, resolveApiKey, resolveProviderFilter, resolveProviderTargetAgents, rewriteModelReferences, runModelFetch, selectFetchedModels, syncAgentModelEntries, syncConfiguredModelPolicyAllow, } from "./model-fetch.js";
|
|
6
|
+
/**
|
|
7
|
+
* The host exposes a single-attempt config mutation to plugins, so a write
|
|
8
|
+
* that loses the race against another writer (for example an OpenClaw upgrade
|
|
9
|
+
* rewriting the config file) fails outright. The host's own config writer
|
|
10
|
+
* retries these conflicts, so mirror that behaviour here.
|
|
11
|
+
*/
|
|
12
|
+
const CONFIG_MUTATION_MAX_ATTEMPTS = 5;
|
|
13
|
+
function isRetryableConfigConflict(error) {
|
|
14
|
+
if (!(error instanceof Error))
|
|
15
|
+
return false;
|
|
16
|
+
if (error.name !== "ConfigMutationConflictError")
|
|
17
|
+
return false;
|
|
18
|
+
return error.retryable !== false;
|
|
19
|
+
}
|
|
20
|
+
async function mutateConfigFileWithRetry(runtime, params) {
|
|
21
|
+
let lastError;
|
|
22
|
+
for (let attempt = 0; attempt < CONFIG_MUTATION_MAX_ATTEMPTS; attempt += 1) {
|
|
23
|
+
try {
|
|
24
|
+
return await runtime.config.mutateConfigFile(params);
|
|
25
|
+
}
|
|
26
|
+
catch (error) {
|
|
27
|
+
lastError = error;
|
|
28
|
+
if (!isRetryableConfigConflict(error))
|
|
29
|
+
throw error;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
throw lastError;
|
|
33
|
+
}
|
|
6
34
|
const AFFECTED_SESSION_LIMIT = 2_000;
|
|
7
35
|
const DEFAULT_AGENT_ID = "main";
|
|
8
36
|
function isRecord(value) {
|
|
@@ -41,6 +69,50 @@ function normalizeTargetAgents(targetAgents) {
|
|
|
41
69
|
function configPath(providerId) {
|
|
42
70
|
return `models.providers.${providerId}.models`;
|
|
43
71
|
}
|
|
72
|
+
/**
|
|
73
|
+
* Validates the requested reference rewrites against the plan.
|
|
74
|
+
*
|
|
75
|
+
* A rewrite may only retarget a model that the plan protects, and it may only
|
|
76
|
+
* point at a model that survives the replacement. Rewriting to a model that is
|
|
77
|
+
* itself removed would leave the config with a dangling reference, so the
|
|
78
|
+
* whole plan is rejected instead.
|
|
79
|
+
*/
|
|
80
|
+
function normalizeReferenceRewrites(requested, computed) {
|
|
81
|
+
if (!requested || requested.length === 0)
|
|
82
|
+
return [];
|
|
83
|
+
const protectedIds = new Set(computed.protectedModels.map((entry) => entry.modelId));
|
|
84
|
+
const survivingIds = new Set(computed.finalModels.map((model) => model.id));
|
|
85
|
+
const removedIds = new Set(computed.removedIds);
|
|
86
|
+
const providerPrefix = `${computed.providerId}/`;
|
|
87
|
+
const result = [];
|
|
88
|
+
const seen = new Set();
|
|
89
|
+
for (const entry of requested) {
|
|
90
|
+
const protectedModelId = entry.protectedModelId.trim();
|
|
91
|
+
if (!protectedIds.has(protectedModelId)) {
|
|
92
|
+
throw new Error(`引用改写目标 ${protectedModelId} 不在本次保留列表中,已取消写入。请重新预览。`);
|
|
93
|
+
}
|
|
94
|
+
if (seen.has(protectedModelId))
|
|
95
|
+
continue;
|
|
96
|
+
const targetRef = entry.targetRef.trim();
|
|
97
|
+
if (!targetRef.startsWith(providerPrefix)) {
|
|
98
|
+
throw new Error(`引用改写目标 ${targetRef} 不属于当前 Provider,已取消写入。请重新预览。`);
|
|
99
|
+
}
|
|
100
|
+
const targetId = targetRef.slice(providerPrefix.length);
|
|
101
|
+
if (!targetId) {
|
|
102
|
+
throw new Error(`引用改写目标 ${targetRef} 无效,已取消写入。请重新预览。`);
|
|
103
|
+
}
|
|
104
|
+
if (removedIds.has(targetId) || !survivingIds.has(targetId)) {
|
|
105
|
+
throw new Error(`引用改写目标 ${targetRef} 也会被删除,已取消写入。请重新预览。`);
|
|
106
|
+
}
|
|
107
|
+
seen.add(protectedModelId);
|
|
108
|
+
result.push({
|
|
109
|
+
protectedModelId,
|
|
110
|
+
fromRef: `${computed.providerId}/${protectedModelId}`,
|
|
111
|
+
toRef: targetRef,
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
return result;
|
|
115
|
+
}
|
|
44
116
|
function planFingerprint(input) {
|
|
45
117
|
return createHash("sha256")
|
|
46
118
|
.update(JSON.stringify({
|
|
@@ -372,6 +444,8 @@ export function createModelFetchOperations({ api, factoryConfig, fetchModels = f
|
|
|
372
444
|
removedIds,
|
|
373
445
|
protectedModels: protection.protectedModels,
|
|
374
446
|
});
|
|
447
|
+
const caseVariants = collectCaseVariantRelations(removedIds, protection.models.map((model) => model.id));
|
|
448
|
+
const defaultModelRef = resolveDefaultModelRef(config);
|
|
375
449
|
return {
|
|
376
450
|
providerId,
|
|
377
451
|
provider,
|
|
@@ -384,9 +458,13 @@ export function createModelFetchOperations({ api, factoryConfig, fetchModels = f
|
|
|
384
458
|
selectedIds: merge.selectedIds,
|
|
385
459
|
unchangedCount: merge.unchanged,
|
|
386
460
|
protectedModels: protection.protectedModels,
|
|
461
|
+
caseVariants,
|
|
462
|
+
defaultModelRef,
|
|
387
463
|
models,
|
|
388
464
|
fingerprint,
|
|
389
465
|
finalModels: protection.models,
|
|
466
|
+
existingModels: existing,
|
|
467
|
+
mergedModels: merge.models,
|
|
390
468
|
};
|
|
391
469
|
}
|
|
392
470
|
async function listProviders() {
|
|
@@ -438,7 +516,7 @@ export function createModelFetchOperations({ api, factoryConfig, fetchModels = f
|
|
|
438
516
|
const nextTargetAgents = targetAgents === undefined
|
|
439
517
|
? savedTargetAgents(provider, config)
|
|
440
518
|
: normalizeTargetAgents(targetAgents);
|
|
441
|
-
const result = await runtime
|
|
519
|
+
const result = await mutateConfigFileWithRetry(runtime, {
|
|
442
520
|
base: "source",
|
|
443
521
|
afterWrite: { mode: "auto" },
|
|
444
522
|
mutate: (draft) => {
|
|
@@ -467,6 +545,8 @@ export function createModelFetchOperations({ api, factoryConfig, fetchModels = f
|
|
|
467
545
|
addedIds: computed.addedIds,
|
|
468
546
|
removedIds: computed.removedIds,
|
|
469
547
|
protectedModels: computed.protectedModels,
|
|
548
|
+
caseVariants: computed.caseVariants,
|
|
549
|
+
defaultModelRef: computed.defaultModelRef,
|
|
470
550
|
selectedIds: computed.selectedIds,
|
|
471
551
|
unchangedCount: computed.unchangedCount,
|
|
472
552
|
affectedSessions,
|
|
@@ -477,7 +557,7 @@ export function createModelFetchOperations({ api, factoryConfig, fetchModels = f
|
|
|
477
557
|
models: computed.models,
|
|
478
558
|
};
|
|
479
559
|
}
|
|
480
|
-
async function applyStrategy({ provider, fingerprint, expectedRemovals, targetAgents, }, scope = {}) {
|
|
560
|
+
async function applyStrategy({ provider, fingerprint, expectedRemovals, referenceRewrites, targetAgents, }, scope = {}) {
|
|
481
561
|
const computed = await computePlan(provider);
|
|
482
562
|
if (computed.fingerprint !== fingerprint) {
|
|
483
563
|
throw new Error("模型列表或筛选策略已变化,请重新预览后再确认应用。");
|
|
@@ -485,9 +565,11 @@ export function createModelFetchOperations({ api, factoryConfig, fetchModels = f
|
|
|
485
565
|
if (!sameStringSets(computed.removedIds, expectedRemovals)) {
|
|
486
566
|
throw new Error("待删除模型与确认列表不一致,已取消写入。请重新预览。");
|
|
487
567
|
}
|
|
568
|
+
const rewrites = normalizeReferenceRewrites(referenceRewrites, computed);
|
|
488
569
|
const nextTargetAgents = normalizeTargetAgents(targetAgents ?? savedTargetAgents(provider));
|
|
489
570
|
let syncedAgents = [];
|
|
490
|
-
const
|
|
571
|
+
const rewrittenReferences = [];
|
|
572
|
+
const result = await mutateConfigFileWithRetry(runtime, {
|
|
491
573
|
base: "source",
|
|
492
574
|
afterWrite: { mode: "auto" },
|
|
493
575
|
mutate: (draft) => {
|
|
@@ -504,13 +586,29 @@ export function createModelFetchOperations({ api, factoryConfig, fetchModels = f
|
|
|
504
586
|
if (referenceProtectionDrift(config, provider, computed)) {
|
|
505
587
|
throw new Error("模型引用在确认后发生变化,已取消写入。请重新预览。");
|
|
506
588
|
}
|
|
507
|
-
|
|
589
|
+
for (const rewrite of rewrites) {
|
|
590
|
+
const result = rewriteModelReferences(config, rewrite.fromRef, rewrite.toRef);
|
|
591
|
+
if (result.changed)
|
|
592
|
+
rewrittenReferences.push(...result.paths);
|
|
593
|
+
}
|
|
594
|
+
// The rewrites may have released the references that kept a model in
|
|
595
|
+
// the list, so the final list is recomputed against the rewritten
|
|
596
|
+
// draft instead of reusing the previewed one.
|
|
597
|
+
const finalModels = rewrites.length > 0
|
|
598
|
+
? applyModelReferenceProtection({
|
|
599
|
+
existing: computed.existingModels,
|
|
600
|
+
merged: computed.mergedModels,
|
|
601
|
+
providerId: provider,
|
|
602
|
+
config,
|
|
603
|
+
}).models
|
|
604
|
+
: computed.finalModels;
|
|
605
|
+
target.models = finalModels;
|
|
508
606
|
ensurePluginEntry(config, provider, computed.filter, nextTargetAgents);
|
|
509
|
-
const syncResult = syncConfiguredModelPolicyAllow(config, provider,
|
|
607
|
+
const syncResult = syncConfiguredModelPolicyAllow(config, provider, finalModels, {
|
|
510
608
|
targetAgents: nextTargetAgents,
|
|
511
609
|
});
|
|
512
610
|
syncedAgents = syncResult.syncedAgents;
|
|
513
|
-
syncAgentModelEntries(config, provider,
|
|
611
|
+
syncAgentModelEntries(config, provider, finalModels, {
|
|
514
612
|
targetAgents: syncedAgents,
|
|
515
613
|
});
|
|
516
614
|
},
|
|
@@ -542,6 +640,7 @@ export function createModelFetchOperations({ api, factoryConfig, fetchModels = f
|
|
|
542
640
|
removed: computed.removedIds.length,
|
|
543
641
|
selectedIds: computed.selectedIds,
|
|
544
642
|
protectedModels: computed.protectedModels,
|
|
643
|
+
rewrittenReferences,
|
|
545
644
|
targetAgents: nextTargetAgents,
|
|
546
645
|
syncedAgents,
|
|
547
646
|
sessionSelectionResets: resetSessionKeys.length,
|
package/dist/schemas.d.ts
CHANGED
|
@@ -37,6 +37,27 @@ export declare const protectedModelSchema: Type.TObject<{
|
|
|
37
37
|
references: Type.TArray<Type.TString>;
|
|
38
38
|
}>;
|
|
39
39
|
export type ProtectedModel = Static<typeof protectedModelSchema>;
|
|
40
|
+
/**
|
|
41
|
+
* A removal candidate that only differs from a surviving model by letter case.
|
|
42
|
+
*/
|
|
43
|
+
export declare const caseVariantSchema: Type.TObject<{
|
|
44
|
+
removedId: Type.TString;
|
|
45
|
+
survivingModelId: Type.TString;
|
|
46
|
+
}>;
|
|
47
|
+
export type CaseVariant = Static<typeof caseVariantSchema>;
|
|
48
|
+
/**
|
|
49
|
+
* The replacement a protected model's references are rewritten to.
|
|
50
|
+
*
|
|
51
|
+
* `targetRef` is the full `provider/model` reference written into the config.
|
|
52
|
+
* `useDefaultModel` marks the case where the target is the agent default
|
|
53
|
+
* model, which the UI shows without naming a concrete provider model.
|
|
54
|
+
*/
|
|
55
|
+
export declare const referenceRewriteSchema: Type.TObject<{
|
|
56
|
+
protectedModelId: Type.TString;
|
|
57
|
+
targetRef: Type.TString;
|
|
58
|
+
useDefaultModel: Type.TBoolean;
|
|
59
|
+
}>;
|
|
60
|
+
export type ReferenceRewrite = Static<typeof referenceRewriteSchema>;
|
|
40
61
|
export declare const modelFetchResultSchema: Type.TObject<{
|
|
41
62
|
provider: Type.TString;
|
|
42
63
|
fetched: Type.TInteger;
|
|
@@ -171,6 +192,11 @@ export declare const applyPlanSchema: Type.TObject<{
|
|
|
171
192
|
ref: Type.TString;
|
|
172
193
|
references: Type.TArray<Type.TString>;
|
|
173
194
|
}>>;
|
|
195
|
+
caseVariants: Type.TArray<Type.TObject<{
|
|
196
|
+
removedId: Type.TString;
|
|
197
|
+
survivingModelId: Type.TString;
|
|
198
|
+
}>>;
|
|
199
|
+
defaultModelRef: Type.TOptional<Type.TString>;
|
|
174
200
|
selectedIds: Type.TArray<Type.TString>;
|
|
175
201
|
unchangedCount: Type.TInteger;
|
|
176
202
|
affectedSessions: Type.TArray<Type.TObject<{
|
|
@@ -193,6 +219,11 @@ export declare const previewApplyOutputSchema: Type.TObject<{
|
|
|
193
219
|
ref: Type.TString;
|
|
194
220
|
references: Type.TArray<Type.TString>;
|
|
195
221
|
}>>;
|
|
222
|
+
caseVariants: Type.TArray<Type.TObject<{
|
|
223
|
+
removedId: Type.TString;
|
|
224
|
+
survivingModelId: Type.TString;
|
|
225
|
+
}>>;
|
|
226
|
+
defaultModelRef: Type.TOptional<Type.TString>;
|
|
196
227
|
selectedIds: Type.TArray<Type.TString>;
|
|
197
228
|
unchangedCount: Type.TInteger;
|
|
198
229
|
affectedSessions: Type.TArray<Type.TObject<{
|
|
@@ -212,6 +243,11 @@ export declare const applyStrategyInputSchema: Type.TObject<{
|
|
|
212
243
|
provider: Type.TString;
|
|
213
244
|
fingerprint: Type.TString;
|
|
214
245
|
expectedRemovals: Type.TArray<Type.TString>;
|
|
246
|
+
referenceRewrites: Type.TOptional<Type.TArray<Type.TObject<{
|
|
247
|
+
protectedModelId: Type.TString;
|
|
248
|
+
targetRef: Type.TString;
|
|
249
|
+
useDefaultModel: Type.TBoolean;
|
|
250
|
+
}>>>;
|
|
215
251
|
targetAgents: Type.TOptional<Type.TArray<Type.TString>>;
|
|
216
252
|
}>;
|
|
217
253
|
export type ApplyStrategyInput = Static<typeof applyStrategyInputSchema>;
|
|
@@ -230,6 +266,7 @@ export declare const applyStrategyOutputSchema: Type.TObject<{
|
|
|
230
266
|
ref: Type.TString;
|
|
231
267
|
references: Type.TArray<Type.TString>;
|
|
232
268
|
}>>;
|
|
269
|
+
rewrittenReferences: Type.TArray<Type.TString>;
|
|
233
270
|
targetAgents: Type.TArray<Type.TString>;
|
|
234
271
|
syncedAgents: Type.TArray<Type.TString>;
|
|
235
272
|
sessionSelectionResets: Type.TInteger;
|