openclaw-plugin-model-fetch 0.2.9 → 0.2.11
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 +1 -1
- package/README.md +1 -1
- package/dist/agent-tool.d.ts +20 -0
- package/dist/agent-tool.js +118 -0
- package/dist/control-ui/{5e7c52c3c0fc8c04a2bffdd2a1cb8ab395800f892ba686381c4918e58bbe6736 → da0bba63a04b1e6c889671add02041d1452ec068e8cc9ae624aba6a5c8429f40}/index.js +5 -5
- package/dist/index.js +2 -3
- package/dist/model-fetch.d.ts +2 -22
- package/dist/model-fetch.js +100 -319
- package/dist/model-replacement.d.ts +29 -0
- package/dist/model-replacement.js +117 -0
- package/dist/operations.d.ts +2 -14
- package/dist/operations.js +25 -364
- package/dist/session-cleanup.d.ts +23 -0
- package/dist/session-cleanup.js +187 -0
- package/openclaw.plugin.json +3 -3
- package/package.json +1 -1
- package/dist/control-ui-tab.d.ts +0 -31
- package/dist/control-ui-tab.js +0 -42
- /package/dist/control-ui/{5e7c52c3c0fc8c04a2bffdd2a1cb8ab395800f892ba686381c4918e58bbe6736 → da0bba63a04b1e6c889671add02041d1452ec068e8cc9ae624aba6a5c8429f40}/index.css +0 -0
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
import * as sessionStoreRuntime from "openclaw/plugin-sdk/session-store-runtime";
|
|
2
|
+
import { listConfiguredAgentEntries } from "./model-fetch.js";
|
|
3
|
+
const AFFECTED_SESSION_LIMIT = 2_000;
|
|
4
|
+
const DEFAULT_AGENT_ID = "main";
|
|
5
|
+
export function createModelFetchSessionCleanup({ currentConfig, sessionStore: sessionStoreOverrides, } = {}) {
|
|
6
|
+
const sessionStore = {
|
|
7
|
+
getSessionEntryAsync: sessionStoreRuntime.getSessionEntryAsync,
|
|
8
|
+
patchSessionEntry: sessionStoreRuntime.patchSessionEntry,
|
|
9
|
+
listSessionEntries: sessionStoreRuntime.listSessionEntriesAsync,
|
|
10
|
+
...sessionStoreOverrides,
|
|
11
|
+
};
|
|
12
|
+
function configuredAgentIds() {
|
|
13
|
+
return currentConfig
|
|
14
|
+
? listConfiguredAgentEntries(currentConfig()).map(({ id }) => id)
|
|
15
|
+
: [];
|
|
16
|
+
}
|
|
17
|
+
function staleOverrideMatches(entry, providerId, removedIds) {
|
|
18
|
+
if (entry.providerOverride) {
|
|
19
|
+
return entry.providerOverride === providerId && removedIds.includes(entry.modelOverride ?? "");
|
|
20
|
+
}
|
|
21
|
+
if (entry.modelOverride) {
|
|
22
|
+
return removedIds.some((modelId) => entry.modelOverride === `${providerId}/${modelId}`);
|
|
23
|
+
}
|
|
24
|
+
return entry.modelProvider === providerId && removedIds.includes(entry.model ?? "");
|
|
25
|
+
}
|
|
26
|
+
/** Session ownership comes only from the leading agent key segment. */
|
|
27
|
+
function parseAgentIdFromSessionKey(sessionKey) {
|
|
28
|
+
const raw = sessionKey?.trim() ?? "";
|
|
29
|
+
if (raw.length < 7 || raw.slice(0, 6).toLowerCase() !== "agent:")
|
|
30
|
+
return undefined;
|
|
31
|
+
const separator = raw.indexOf(":", 6);
|
|
32
|
+
if (separator === -1)
|
|
33
|
+
return undefined;
|
|
34
|
+
const agentId = raw.slice(6, separator).trim();
|
|
35
|
+
const rest = raw.slice(separator + 1);
|
|
36
|
+
if (!agentId || !rest || rest.startsWith(":"))
|
|
37
|
+
return undefined;
|
|
38
|
+
return agentId;
|
|
39
|
+
}
|
|
40
|
+
function staleSelectionResetUpdate(entry, providerId, removedIds) {
|
|
41
|
+
const runtimeModelMatches = entry.modelProvider === providerId && removedIds.includes(entry.model ?? "");
|
|
42
|
+
return {
|
|
43
|
+
providerOverride: undefined,
|
|
44
|
+
modelOverride: undefined,
|
|
45
|
+
modelOverrideSource: undefined,
|
|
46
|
+
modelOverrideRouteResolution: undefined,
|
|
47
|
+
modelOverrideFallbackOriginProvider: undefined,
|
|
48
|
+
modelOverrideFallbackOriginModel: undefined,
|
|
49
|
+
...(runtimeModelMatches
|
|
50
|
+
? {
|
|
51
|
+
model: undefined,
|
|
52
|
+
modelProvider: undefined,
|
|
53
|
+
contextTokens: undefined,
|
|
54
|
+
contextTokensSource: undefined,
|
|
55
|
+
contextBudgetStatus: undefined,
|
|
56
|
+
}
|
|
57
|
+
: {}),
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
/** Discover related agent stores without creating writable database lifecycles. */
|
|
61
|
+
async function collectSessionEntries(agentIds) {
|
|
62
|
+
const candidates = [];
|
|
63
|
+
const visited = new Set();
|
|
64
|
+
const queue = [...agentIds];
|
|
65
|
+
while (queue.length > 0) {
|
|
66
|
+
const agentId = (queue.shift() ?? "").trim();
|
|
67
|
+
if (!agentId)
|
|
68
|
+
continue;
|
|
69
|
+
const normalized = agentId.toLowerCase();
|
|
70
|
+
if (visited.has(normalized))
|
|
71
|
+
continue;
|
|
72
|
+
visited.add(normalized);
|
|
73
|
+
let summaries = [];
|
|
74
|
+
try {
|
|
75
|
+
summaries = await sessionStore.listSessionEntries({ agentId });
|
|
76
|
+
}
|
|
77
|
+
catch {
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
for (const summary of summaries ?? []) {
|
|
81
|
+
const sessionKey = summary?.sessionKey;
|
|
82
|
+
if (!sessionKey)
|
|
83
|
+
continue;
|
|
84
|
+
const parsedAgentId = parseAgentIdFromSessionKey(sessionKey);
|
|
85
|
+
candidates.push({
|
|
86
|
+
sessionKey,
|
|
87
|
+
agentId: parsedAgentId ?? agentId,
|
|
88
|
+
entry: summary.entry,
|
|
89
|
+
});
|
|
90
|
+
if (parsedAgentId && !visited.has(parsedAgentId.toLowerCase())) {
|
|
91
|
+
queue.push(parsedAgentId);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
return candidates;
|
|
96
|
+
}
|
|
97
|
+
function collectAffectedSessions(candidates, providerId, removedIds) {
|
|
98
|
+
const affected = [];
|
|
99
|
+
const seen = new Set();
|
|
100
|
+
for (const candidate of candidates) {
|
|
101
|
+
if (affected.length >= AFFECTED_SESSION_LIMIT)
|
|
102
|
+
break;
|
|
103
|
+
const entry = candidate.entry;
|
|
104
|
+
if (!entry || entry.modelSelectionLocked === true)
|
|
105
|
+
continue;
|
|
106
|
+
if (!staleOverrideMatches(entry, providerId, removedIds))
|
|
107
|
+
continue;
|
|
108
|
+
const dedupeKey = `${candidate.agentId.toLowerCase()}::${candidate.sessionKey}`;
|
|
109
|
+
if (seen.has(dedupeKey))
|
|
110
|
+
continue;
|
|
111
|
+
seen.add(dedupeKey);
|
|
112
|
+
affected.push({
|
|
113
|
+
sessionKey: candidate.sessionKey,
|
|
114
|
+
agentId: candidate.agentId,
|
|
115
|
+
label: entry.label || entry.displayName || candidate.sessionKey,
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
return affected;
|
|
119
|
+
}
|
|
120
|
+
async function affectedSessions(providerId, removedIds, scope = {}) {
|
|
121
|
+
if (removedIds.length === 0)
|
|
122
|
+
return [];
|
|
123
|
+
const agentIds = new Set([DEFAULT_AGENT_ID]);
|
|
124
|
+
const scopeAgentId = scope.agentId?.trim();
|
|
125
|
+
if (scopeAgentId)
|
|
126
|
+
agentIds.add(scopeAgentId);
|
|
127
|
+
const scopedKeyAgentId = scope.sessionKey
|
|
128
|
+
? parseAgentIdFromSessionKey(scope.sessionKey)
|
|
129
|
+
: undefined;
|
|
130
|
+
if (scopedKeyAgentId)
|
|
131
|
+
agentIds.add(scopedKeyAgentId);
|
|
132
|
+
for (const agentId of configuredAgentIds())
|
|
133
|
+
agentIds.add(agentId);
|
|
134
|
+
const candidates = await collectSessionEntries([...agentIds]);
|
|
135
|
+
const scopedSessionKey = scope.sessionKey?.trim();
|
|
136
|
+
const scopedOwnerId = scopeAgentId ?? scopedKeyAgentId;
|
|
137
|
+
if (scopedSessionKey && scopedOwnerId) {
|
|
138
|
+
const known = candidates.some((candidate) => candidate.sessionKey === scopedSessionKey);
|
|
139
|
+
if (!known) {
|
|
140
|
+
try {
|
|
141
|
+
const entry = await sessionStore.getSessionEntryAsync({
|
|
142
|
+
sessionKey: scopedSessionKey,
|
|
143
|
+
agentId: scopedOwnerId,
|
|
144
|
+
});
|
|
145
|
+
if (entry) {
|
|
146
|
+
candidates.push({ sessionKey: scopedSessionKey, agentId: scopedOwnerId, entry });
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
catch {
|
|
150
|
+
// The optional calling session must not block fleet cleanup.
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
return collectAffectedSessions(candidates, providerId, removedIds);
|
|
155
|
+
}
|
|
156
|
+
async function reset({ providerId, removedIds, scope, }) {
|
|
157
|
+
const resetSessionKeys = [];
|
|
158
|
+
if (removedIds.length === 0)
|
|
159
|
+
return resetSessionKeys;
|
|
160
|
+
const candidates = await affectedSessions(providerId, removedIds, scope);
|
|
161
|
+
for (const candidate of candidates) {
|
|
162
|
+
try {
|
|
163
|
+
const latest = await sessionStore.getSessionEntryAsync({
|
|
164
|
+
sessionKey: candidate.sessionKey,
|
|
165
|
+
agentId: candidate.agentId,
|
|
166
|
+
});
|
|
167
|
+
if (!latest || latest.modelSelectionLocked === true)
|
|
168
|
+
continue;
|
|
169
|
+
if (!staleOverrideMatches(latest, providerId, removedIds))
|
|
170
|
+
continue;
|
|
171
|
+
const updated = await sessionStore.patchSessionEntry({
|
|
172
|
+
sessionKey: candidate.sessionKey,
|
|
173
|
+
agentId: candidate.agentId,
|
|
174
|
+
preserveActivity: true,
|
|
175
|
+
update: () => staleSelectionResetUpdate(latest, providerId, removedIds),
|
|
176
|
+
});
|
|
177
|
+
if (updated)
|
|
178
|
+
resetSessionKeys.push(candidate.sessionKey);
|
|
179
|
+
}
|
|
180
|
+
catch {
|
|
181
|
+
// A single failing session must not abort the remaining cleanups.
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
return resetSessionKeys;
|
|
185
|
+
}
|
|
186
|
+
return { affectedSessions, reset };
|
|
187
|
+
}
|
package/openclaw.plugin.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"id": "model-fetch",
|
|
3
3
|
"name": "Model Fetch",
|
|
4
4
|
"description": "Fetch and refresh configured provider model lists into OpenClaw config with independent per-provider filters.",
|
|
5
|
-
"version": "0.2.
|
|
5
|
+
"version": "0.2.11",
|
|
6
6
|
"configSchema": {
|
|
7
7
|
"type": "object",
|
|
8
8
|
"properties": {
|
|
@@ -65,9 +65,9 @@
|
|
|
65
65
|
"navigation"
|
|
66
66
|
],
|
|
67
67
|
"controlUi": {
|
|
68
|
-
"entry": "dist/control-ui/
|
|
68
|
+
"entry": "dist/control-ui/da0bba63a04b1e6c889671add02041d1452ec068e8cc9ae624aba6a5c8429f40/index.js",
|
|
69
69
|
"styles": [
|
|
70
|
-
"dist/control-ui/
|
|
70
|
+
"dist/control-ui/da0bba63a04b1e6c889671add02041d1452ec068e8cc9ae624aba6a5c8429f40/index.css"
|
|
71
71
|
]
|
|
72
72
|
},
|
|
73
73
|
"contracts": {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "openclaw-plugin-model-fetch",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.11",
|
|
4
4
|
"description": "OpenClaw plugin tool that refreshes configured provider model lists into OpenClaw config with independent per-provider filters.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
package/dist/control-ui-tab.d.ts
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
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 {};
|
package/dist/control-ui-tab.js
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
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
|
-
}
|