@justsilver/opencode-team 0.2.2-beta.1 → 0.2.2-beta.3
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/CHANGELOG.md +18 -0
- package/dist/model-ref.js +27 -0
- package/dist/rpc.js +104 -0
- package/dist/team-ids.js +2 -0
- package/dist/tui.js +21 -0
- package/dist/view/configure.js +77 -0
- package/dist/view/dashboard.js +1206 -0
- package/dist/view/model-config.js +1267 -0
- package/dist/view/model.js +335 -0
- package/dist/view/team.js +227 -0
- package/dist/view/ui.js +222 -0
- package/package.json +16 -4
- package/plugin/opencode-team/{prompts.ts → prompts.generated.ts} +23 -10
- package/plugin/opencode-team/team-agents.ts +7 -10
|
@@ -0,0 +1,335 @@
|
|
|
1
|
+
import { formatModelRef, isChatModel, parseModelRef } from "../model-ref.js";
|
|
2
|
+
import { TEAM_SUBAGENT_IDS } from "../team-ids.js";
|
|
3
|
+
export { formatModelRef, parseModelRef } from "../model-ref.js";
|
|
4
|
+
export const STATUS_LABELS = {
|
|
5
|
+
native: "原生",
|
|
6
|
+
inherit: "继承",
|
|
7
|
+
custom: "自定义"
|
|
8
|
+
};
|
|
9
|
+
export const SOURCE_LABELS = {
|
|
10
|
+
team: "团队策略",
|
|
11
|
+
agent: "Agent 配置",
|
|
12
|
+
none: "无独立模型"
|
|
13
|
+
};
|
|
14
|
+
/** 错误保留到下一次操作或被关闭,其余状态自动收起,避免状态栏长期占用内容高度。 */
|
|
15
|
+
export function feedbackDismissMs(tone) {
|
|
16
|
+
if (tone === "error") return 0;
|
|
17
|
+
if (tone === "warning") return 6000;
|
|
18
|
+
return 4000;
|
|
19
|
+
}
|
|
20
|
+
export function statusTone(status) {
|
|
21
|
+
return status === "custom" ? "accent" : "neutral";
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** 允许规则数量多,保持中性;只有拒绝与询问用语义色,避免权限区抢走模型信息的注意力。 */
|
|
25
|
+
export function effectTone(effect) {
|
|
26
|
+
if (effect === "deny") return "error";
|
|
27
|
+
if (effect === "ask") return "warning";
|
|
28
|
+
return "neutral";
|
|
29
|
+
}
|
|
30
|
+
/** 无 Variant 的模型没有可操作的第二栏,切换区域时始终留在模型列表。 */
|
|
31
|
+
export function nextRegion(region, hasVariants) {
|
|
32
|
+
if (!hasVariants) return "models";
|
|
33
|
+
return region === "models" ? "variants" : "models";
|
|
34
|
+
}
|
|
35
|
+
const PROMPT_UNAVAILABLE = "宿主未为该 Agent 提供独立 System Prompt。";
|
|
36
|
+
function preferredRank(id) {
|
|
37
|
+
const index = TEAM_SUBAGENT_IDS.indexOf(id);
|
|
38
|
+
return index === -1 ? TEAM_SUBAGENT_IDS.length : index;
|
|
39
|
+
}
|
|
40
|
+
export function hostModelRef(model) {
|
|
41
|
+
if (model === undefined) return undefined;
|
|
42
|
+
return formatModelRef({
|
|
43
|
+
providerID: model.providerID,
|
|
44
|
+
id: model.id,
|
|
45
|
+
variant: model.variant
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
function providerName(providers, providerID) {
|
|
49
|
+
const provider = providers.find(candidate => candidate.id === providerID);
|
|
50
|
+
const name = provider?.name?.trim();
|
|
51
|
+
return name === undefined || name === "" ? providerID : name;
|
|
52
|
+
}
|
|
53
|
+
export function providerLabel(providers, providerID) {
|
|
54
|
+
return providerName(providers, providerID);
|
|
55
|
+
}
|
|
56
|
+
export function modelRefOf(model) {
|
|
57
|
+
return `${model.providerID}/${model.id}`;
|
|
58
|
+
}
|
|
59
|
+
export function roleLabel(mode) {
|
|
60
|
+
if (mode === "primary") return "PRIMARY";
|
|
61
|
+
if (mode === "all") return "ALL";
|
|
62
|
+
return "SUBAGENT";
|
|
63
|
+
}
|
|
64
|
+
export function resolveModel(models, baseRef) {
|
|
65
|
+
return models.find(candidate => modelRefOf(candidate) === baseRef);
|
|
66
|
+
}
|
|
67
|
+
export function variantsFor(model) {
|
|
68
|
+
const seen = new Set();
|
|
69
|
+
const result = [];
|
|
70
|
+
for (const variant of model?.variants ?? []) {
|
|
71
|
+
if (variant.id === "" || seen.has(variant.id)) continue;
|
|
72
|
+
seen.add(variant.id);
|
|
73
|
+
result.push(variant.id);
|
|
74
|
+
}
|
|
75
|
+
return result;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** 搜索改变列表后,选中项必须落在可见模型上;没有可见模型时返回 undefined。 */
|
|
79
|
+
export function firstVisibleRef(models, current) {
|
|
80
|
+
if (models.some(model => modelRefOf(model) === current)) return current;
|
|
81
|
+
const first = models[0];
|
|
82
|
+
return first === undefined ? undefined : modelRefOf(first);
|
|
83
|
+
}
|
|
84
|
+
export function buildModelRef(baseRef, variant) {
|
|
85
|
+
return variant === undefined || variant === "" ? baseRef : `${baseRef}#${variant}`;
|
|
86
|
+
}
|
|
87
|
+
export function modelGroups(models, providers, query) {
|
|
88
|
+
const tokens = query.trim().toLowerCase().split(/\s+/).filter(Boolean);
|
|
89
|
+
const byProvider = new Map();
|
|
90
|
+
for (const model of models) {
|
|
91
|
+
if (!isChatModel(model)) continue;
|
|
92
|
+
const provider = providerLabel(providers, model.providerID);
|
|
93
|
+
const text = [model.name, model.id, model.providerID, provider, modelRefOf(model)].join(" ").toLowerCase();
|
|
94
|
+
if (!tokens.every(token => text.includes(token))) continue;
|
|
95
|
+
const group = byProvider.get(model.providerID) ?? {
|
|
96
|
+
name: provider,
|
|
97
|
+
models: []
|
|
98
|
+
};
|
|
99
|
+
group.models.push(model);
|
|
100
|
+
byProvider.set(model.providerID, group);
|
|
101
|
+
}
|
|
102
|
+
return [...byProvider.entries()].map(([providerID, group]) => ({
|
|
103
|
+
providerID,
|
|
104
|
+
providerName: group.name,
|
|
105
|
+
models: group.models.toSorted((left, right) => (left.name || left.id).localeCompare(right.name || right.id) || left.id.localeCompare(right.id))
|
|
106
|
+
})).toSorted((left, right) => left.providerName.localeCompare(right.providerName) || left.providerID.localeCompare(right.providerID));
|
|
107
|
+
}
|
|
108
|
+
export function modelConfigLayoutFor(width, height) {
|
|
109
|
+
return {
|
|
110
|
+
...layoutFor(width, height),
|
|
111
|
+
variantWidth: width >= 112 ? 38 : 32
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
export function describeModel(ref, mode, models, providers) {
|
|
115
|
+
if (ref === undefined) {
|
|
116
|
+
return {
|
|
117
|
+
label: mode === "primary" ? "OpenCode 原生选择" : "继承调用方模型",
|
|
118
|
+
variantLabel: "—"
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
const parsed = parseModelRef(ref);
|
|
122
|
+
if (parsed === undefined) {
|
|
123
|
+
return {
|
|
124
|
+
label: `${ref}(引用格式无效)`,
|
|
125
|
+
variantLabel: "—",
|
|
126
|
+
refLabel: ref
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
const model = models.find(candidate => candidate.providerID === parsed.providerID && candidate.id === parsed.id);
|
|
130
|
+
const variantLabel = parsed.variant ?? "默认(不附加变体)";
|
|
131
|
+
if (model === undefined) {
|
|
132
|
+
return {
|
|
133
|
+
label: `${ref}(当前目录不可用)`,
|
|
134
|
+
variantLabel,
|
|
135
|
+
refLabel: ref
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
const base = `${providerName(providers, parsed.providerID)} / ${model.name}`;
|
|
139
|
+
return {
|
|
140
|
+
label: parsed.variant === undefined ? base : `${base} · ${parsed.variant}`,
|
|
141
|
+
variantLabel,
|
|
142
|
+
refLabel: ref
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
function classifyModel(input) {
|
|
146
|
+
if (input.teamModel !== undefined) return {
|
|
147
|
+
status: "custom",
|
|
148
|
+
source: "team"
|
|
149
|
+
};
|
|
150
|
+
if (input.hostModel !== undefined) return {
|
|
151
|
+
status: "custom",
|
|
152
|
+
source: "agent"
|
|
153
|
+
};
|
|
154
|
+
if (input.mode === "primary") return {
|
|
155
|
+
status: "native",
|
|
156
|
+
source: "none"
|
|
157
|
+
};
|
|
158
|
+
return {
|
|
159
|
+
status: "inherit",
|
|
160
|
+
source: "none"
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
export function promptInfo(system) {
|
|
164
|
+
if (system === undefined) return {
|
|
165
|
+
available: false,
|
|
166
|
+
reason: PROMPT_UNAVAILABLE
|
|
167
|
+
};
|
|
168
|
+
const text = system.replace(/\r\n?/g, "\n");
|
|
169
|
+
if (text.trim() === "") return {
|
|
170
|
+
available: false,
|
|
171
|
+
reason: PROMPT_UNAVAILABLE
|
|
172
|
+
};
|
|
173
|
+
const lines = text.split("\n");
|
|
174
|
+
if (lines.length > 0 && lines.at(-1) === "") lines.pop();
|
|
175
|
+
return {
|
|
176
|
+
available: true,
|
|
177
|
+
text: lines.join("\n")
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
export function summarizePermissions(rules) {
|
|
181
|
+
if (rules === undefined || rules.length === 0) {
|
|
182
|
+
return {
|
|
183
|
+
total: 0,
|
|
184
|
+
allow: 0,
|
|
185
|
+
ask: 0,
|
|
186
|
+
deny: 0,
|
|
187
|
+
highlights: []
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
const seen = new Set();
|
|
191
|
+
const effective = [];
|
|
192
|
+
for (let index = rules.length - 1; index >= 0; index--) {
|
|
193
|
+
const rule = rules[index];
|
|
194
|
+
const key = `${rule.action}\u0000${rule.resource}`;
|
|
195
|
+
if (seen.has(key)) continue;
|
|
196
|
+
seen.add(key);
|
|
197
|
+
effective.push({
|
|
198
|
+
action: rule.action,
|
|
199
|
+
resource: rule.resource,
|
|
200
|
+
effect: rule.effect
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
effective.reverse();
|
|
204
|
+
let allow = 0;
|
|
205
|
+
let ask = 0;
|
|
206
|
+
let deny = 0;
|
|
207
|
+
for (const rule of effective) {
|
|
208
|
+
if (rule.effect === "allow") allow++;else if (rule.effect === "ask") ask++;else deny++;
|
|
209
|
+
}
|
|
210
|
+
const fallback = effective.find(rule => rule.action === "*" && rule.resource === "*");
|
|
211
|
+
const noteworthy = effective.filter(rule => rule.resource !== "*" || ["shell", "edit", "subagent", "question", "todowrite", "external_directory", "browser", "skill"].includes(rule.action));
|
|
212
|
+
const rank = effect => effect === "deny" ? 0 : effect === "ask" ? 1 : 2;
|
|
213
|
+
const highlights = noteworthy.toSorted((left, right) => rank(left.effect) - rank(right.effect)).slice(0, 4);
|
|
214
|
+
return {
|
|
215
|
+
total: effective.length,
|
|
216
|
+
allow,
|
|
217
|
+
ask,
|
|
218
|
+
deny,
|
|
219
|
+
...(fallback === undefined ? {} : {
|
|
220
|
+
defaultEffect: fallback.effect
|
|
221
|
+
}),
|
|
222
|
+
highlights
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
export function buildDashboardAgent(input, overrides, models, providers) {
|
|
226
|
+
const mode = input.mode ?? "subagent";
|
|
227
|
+
const teamModel = overrides[input.id];
|
|
228
|
+
const hostModel = hostModelRef(input.model);
|
|
229
|
+
const effectiveModel = teamModel ?? hostModel;
|
|
230
|
+
const {
|
|
231
|
+
status,
|
|
232
|
+
source
|
|
233
|
+
} = classifyModel({
|
|
234
|
+
mode,
|
|
235
|
+
...(teamModel === undefined ? {} : {
|
|
236
|
+
teamModel
|
|
237
|
+
}),
|
|
238
|
+
...(hostModel === undefined ? {} : {
|
|
239
|
+
hostModel
|
|
240
|
+
})
|
|
241
|
+
});
|
|
242
|
+
const described = describeModel(effectiveModel, mode, models, providers);
|
|
243
|
+
const prompt = input.known ? promptInfo(input.system) : {
|
|
244
|
+
available: false,
|
|
245
|
+
reason: "该 Agent 不在团队视图中,无法读取它的角色定义与 System Prompt。"
|
|
246
|
+
};
|
|
247
|
+
const description = input.known ? input.description?.trim() || undefined : "该 Agent 不在团队视图中;只能清除旧覆盖。";
|
|
248
|
+
const name = input.name?.trim() || input.id;
|
|
249
|
+
const searchText = [input.id, name, mode, status, STATUS_LABELS[status], SOURCE_LABELS[source], described.label, teamModel ?? "", hostModel ?? ""].join(" ").toLowerCase();
|
|
250
|
+
return {
|
|
251
|
+
id: input.id,
|
|
252
|
+
name,
|
|
253
|
+
mode,
|
|
254
|
+
known: input.known,
|
|
255
|
+
...(description === undefined ? {} : {
|
|
256
|
+
description
|
|
257
|
+
}),
|
|
258
|
+
...(teamModel === undefined ? {} : {
|
|
259
|
+
teamModel
|
|
260
|
+
}),
|
|
261
|
+
...(hostModel === undefined ? {} : {
|
|
262
|
+
hostModel
|
|
263
|
+
}),
|
|
264
|
+
...(effectiveModel === undefined ? {} : {
|
|
265
|
+
effectiveModel
|
|
266
|
+
}),
|
|
267
|
+
modelLabel: described.label,
|
|
268
|
+
...(described.refLabel === undefined ? {} : {
|
|
269
|
+
modelRefLabel: described.refLabel
|
|
270
|
+
}),
|
|
271
|
+
variantLabel: described.variantLabel,
|
|
272
|
+
status,
|
|
273
|
+
statusLabel: STATUS_LABELS[status],
|
|
274
|
+
source,
|
|
275
|
+
sourceLabel: SOURCE_LABELS[source],
|
|
276
|
+
canConfigure: input.known && mode !== "primary",
|
|
277
|
+
canRestore: teamModel !== undefined,
|
|
278
|
+
prompt,
|
|
279
|
+
permissions: summarizePermissions(input.permissions),
|
|
280
|
+
searchText
|
|
281
|
+
};
|
|
282
|
+
}
|
|
283
|
+
export function buildDashboardGroups(inputs, overrides, models, providers) {
|
|
284
|
+
const known = [];
|
|
285
|
+
const knownIDs = new Set();
|
|
286
|
+
for (const agent of inputs) {
|
|
287
|
+
if (agent.hidden) continue;
|
|
288
|
+
if (agent.mode === "primary" && agent.id !== "orchestrator") continue;
|
|
289
|
+
known.push(agent);
|
|
290
|
+
knownIDs.add(agent.id);
|
|
291
|
+
}
|
|
292
|
+
const primary = known.filter(agent => agent.mode === "primary").map(agent => buildDashboardAgent(agent, overrides, models, providers)).toSorted((left, right) => left.id.localeCompare(right.id));
|
|
293
|
+
const subagents = known.filter(agent => agent.mode !== "primary").map(agent => buildDashboardAgent(agent, overrides, models, providers)).toSorted((left, right) => preferredRank(left.id) - preferredRank(right.id) || left.id.localeCompare(right.id));
|
|
294
|
+
const unavailable = Object.keys(overrides).filter(id => !knownIDs.has(id)).toSorted().map(id => buildDashboardAgent({
|
|
295
|
+
id,
|
|
296
|
+
name: id,
|
|
297
|
+
mode: "subagent",
|
|
298
|
+
known: false
|
|
299
|
+
}, overrides, models, providers));
|
|
300
|
+
return [...(primary.length === 0 ? [] : [{
|
|
301
|
+
key: "primary",
|
|
302
|
+
title: "PRIMARY",
|
|
303
|
+
agents: primary
|
|
304
|
+
}]), ...(subagents.length === 0 ? [] : [{
|
|
305
|
+
key: "subagents",
|
|
306
|
+
title: `SUBAGENTS · ${subagents.length}`,
|
|
307
|
+
agents: subagents
|
|
308
|
+
}]), ...(unavailable.length === 0 ? [] : [{
|
|
309
|
+
key: "unavailable",
|
|
310
|
+
title: `已保存但不在团队视图 · ${unavailable.length}`,
|
|
311
|
+
agents: unavailable
|
|
312
|
+
}])];
|
|
313
|
+
}
|
|
314
|
+
export function filterDashboardGroups(groups, query) {
|
|
315
|
+
const tokens = query.trim().toLowerCase().split(/\s+/).filter(Boolean);
|
|
316
|
+
if (tokens.length === 0) return [...groups];
|
|
317
|
+
return groups.flatMap(group => {
|
|
318
|
+
const agents = group.agents.filter(agent => tokens.every(token => agent.searchText.includes(token)));
|
|
319
|
+
if (agents.length === 0) return [];
|
|
320
|
+
return [{
|
|
321
|
+
...group,
|
|
322
|
+
title: group.key === "subagents" ? `SUBAGENTS · ${agents.length}` : group.key === "unavailable" ? `已保存但不在团队视图 · ${agents.length}` : group.title,
|
|
323
|
+
agents
|
|
324
|
+
}];
|
|
325
|
+
});
|
|
326
|
+
}
|
|
327
|
+
export function layoutFor(width, height) {
|
|
328
|
+
return {
|
|
329
|
+
split: width >= 90,
|
|
330
|
+
compact: height < 20 || width < 100,
|
|
331
|
+
minimal: width < 50,
|
|
332
|
+
dialogHeight: Math.max(6, Math.min(30, height - 4)),
|
|
333
|
+
listWidth: width >= 112 ? 32 : 28
|
|
334
|
+
};
|
|
335
|
+
}
|
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
import { isChatModel } from "../model-ref.js";
|
|
2
|
+
import { teamRpc } from "../rpc.js";
|
|
3
|
+
export function currentLocation(ctx) {
|
|
4
|
+
return ctx.location ?? ctx.data.location.default();
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* 面板打开时重新同步 Agent 定义,System Prompt 与权限必须来自宿主当前生效值;
|
|
9
|
+
* 同步失败但有缓存时继续展示,并明确标注可能不是最新。
|
|
10
|
+
*/
|
|
11
|
+
export async function loadTeamDashboard(ctx, location) {
|
|
12
|
+
const state = await readState(ctx, location);
|
|
13
|
+
if ("problem" in state) return {
|
|
14
|
+
kind: "blocked",
|
|
15
|
+
message: state.problem
|
|
16
|
+
};
|
|
17
|
+
const summary = state.summary;
|
|
18
|
+
if (summary.conflict !== undefined) {
|
|
19
|
+
return {
|
|
20
|
+
kind: "blocked",
|
|
21
|
+
message: conflictMessage(summary)
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
const agents = await readAgents(ctx, location);
|
|
25
|
+
if (agents.message !== undefined) return {
|
|
26
|
+
kind: "blocked",
|
|
27
|
+
message: agents.message
|
|
28
|
+
};
|
|
29
|
+
const models = await readModels(ctx, location);
|
|
30
|
+
const providers = await readProviders(ctx, location);
|
|
31
|
+
const inputs = agents.agents.map(agent => ({
|
|
32
|
+
id: agent.id,
|
|
33
|
+
name: agent.name,
|
|
34
|
+
mode: agent.mode,
|
|
35
|
+
hidden: agent.hidden,
|
|
36
|
+
description: agent.description,
|
|
37
|
+
system: agent.system,
|
|
38
|
+
model: agent.model,
|
|
39
|
+
permissions: agent.permissions,
|
|
40
|
+
known: true
|
|
41
|
+
}));
|
|
42
|
+
return {
|
|
43
|
+
kind: "ready",
|
|
44
|
+
data: {
|
|
45
|
+
agents: inputs,
|
|
46
|
+
overrides: summary.models ?? {},
|
|
47
|
+
models: models.models,
|
|
48
|
+
providers,
|
|
49
|
+
...(models.error === undefined ? {} : {
|
|
50
|
+
modelError: models.error
|
|
51
|
+
}),
|
|
52
|
+
...(agents.warning === undefined ? {} : {
|
|
53
|
+
warning: agents.warning
|
|
54
|
+
}),
|
|
55
|
+
agentsSupported: summary.agentsSupported ?? false,
|
|
56
|
+
...(summary.agentsError === undefined ? {} : {
|
|
57
|
+
agentsError: summary.agentsError
|
|
58
|
+
})
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
export async function saveAgentModel(ctx, location, agent, model) {
|
|
63
|
+
if (model !== null) {
|
|
64
|
+
if (!agent.known) {
|
|
65
|
+
return {
|
|
66
|
+
saved: false,
|
|
67
|
+
tone: "warning",
|
|
68
|
+
message: "该 Agent 当前目录不可用,只能清除旧覆盖。"
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
if (agent.mode === "primary") {
|
|
72
|
+
return {
|
|
73
|
+
saved: false,
|
|
74
|
+
tone: "info",
|
|
75
|
+
message: "主代理模型由 OpenCode 会话选择,团队策略只覆盖子代理。"
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return saveModel(ctx, location, agent.id, agent.name, model, agent.hostModel);
|
|
80
|
+
}
|
|
81
|
+
export async function restoreAgentModel(ctx, location, agent) {
|
|
82
|
+
if (!agent.canRestore) {
|
|
83
|
+
return {
|
|
84
|
+
saved: false,
|
|
85
|
+
tone: "info",
|
|
86
|
+
message: `${agent.name} 没有团队模型覆盖,无需恢复。`
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
return saveAgentModel(ctx, location, agent, null);
|
|
90
|
+
}
|
|
91
|
+
async function readState(ctx, location) {
|
|
92
|
+
let result;
|
|
93
|
+
try {
|
|
94
|
+
result = await ctx.client.rpc(teamRpc).getTeamPolicy({}, {
|
|
95
|
+
location
|
|
96
|
+
});
|
|
97
|
+
} catch (error) {
|
|
98
|
+
return {
|
|
99
|
+
problem: `无法读取团队模型设置:${message(error)}`
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
if (!result.supported) {
|
|
103
|
+
return {
|
|
104
|
+
problem: "当前宿主不支持子代理模型覆盖(所需工具钩子不可用),团队模型功能不可用。"
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
if (!result.ok) return {
|
|
108
|
+
problem: `无法读取团队模型设置:${summarize(result)}`
|
|
109
|
+
};
|
|
110
|
+
return {
|
|
111
|
+
summary: result
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
async function readAgents(ctx, location) {
|
|
115
|
+
const collection = ctx.data.location.agent;
|
|
116
|
+
let warning;
|
|
117
|
+
try {
|
|
118
|
+
await collection.sync(location);
|
|
119
|
+
} catch (error) {
|
|
120
|
+
const cached = collection.list(location);
|
|
121
|
+
if (cached === undefined) return {
|
|
122
|
+
agents: [],
|
|
123
|
+
message: `无法读取 Agent 列表:${message(error)}`
|
|
124
|
+
};
|
|
125
|
+
warning = `Agent 定义同步失败,正在使用缓存,内容可能不是最新:${message(error)}`;
|
|
126
|
+
}
|
|
127
|
+
try {
|
|
128
|
+
const agents = collection.list(location);
|
|
129
|
+
if (agents === undefined) throw new Error("Agent 目录未返回数据");
|
|
130
|
+
return {
|
|
131
|
+
agents,
|
|
132
|
+
...(warning === undefined ? {} : {
|
|
133
|
+
warning
|
|
134
|
+
})
|
|
135
|
+
};
|
|
136
|
+
} catch (error) {
|
|
137
|
+
return {
|
|
138
|
+
agents: [],
|
|
139
|
+
message: `无法读取 Agent 列表:${message(error)}`
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
async function readModels(ctx, location, force = false) {
|
|
144
|
+
const collection = ctx.data.location.model;
|
|
145
|
+
try {
|
|
146
|
+
// `list` reads the per-location cache the host fills at boot; syncing only when it is
|
|
147
|
+
// absent avoids a network hop on every open while still distinguishing "not loaded"
|
|
148
|
+
// from a genuinely empty catalog.
|
|
149
|
+
if (force || collection.list(location) === undefined) await collection.sync(location);
|
|
150
|
+
const models = collection.list(location);
|
|
151
|
+
if (models === undefined) throw new Error("模型目录未返回数据");
|
|
152
|
+
return {
|
|
153
|
+
models: models.filter(isChatModel)
|
|
154
|
+
};
|
|
155
|
+
} catch (error) {
|
|
156
|
+
return {
|
|
157
|
+
models: [],
|
|
158
|
+
error: `无法读取模型列表:${message(error)}`
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** 打开模型配置前重新同步一次目录,覆盖首次加载失败或目录为空的情况。 */
|
|
164
|
+
export async function refreshModels(ctx, location) {
|
|
165
|
+
return readModels(ctx, location, true);
|
|
166
|
+
}
|
|
167
|
+
async function readProviders(ctx, location) {
|
|
168
|
+
const collection = ctx.data.location.provider;
|
|
169
|
+
try {
|
|
170
|
+
if (collection.list(location) === undefined) await collection.sync(location);
|
|
171
|
+
return collection.list(location) ?? [];
|
|
172
|
+
} catch {
|
|
173
|
+
return collection.list(location) ?? [];
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
function conflictMessage(state) {
|
|
177
|
+
const migration = state.migration;
|
|
178
|
+
if (migration?.status === "applied") {
|
|
179
|
+
const count = migration.agents?.length ?? 0;
|
|
180
|
+
const imported = count === 0 ? "当前没有可迁移的子代理" : `已迁移旧全局模型到 ${count} 个子代理`;
|
|
181
|
+
return `检测到旧版插件 ${state.conflict?.pluginID ?? ""} 仍在加载。${imported};请移除旧插件并完全重启 OpenCode,之后才能调整团队模型。`;
|
|
182
|
+
}
|
|
183
|
+
if (migration?.status === "empty") {
|
|
184
|
+
return `检测到旧版插件 ${state.conflict?.pluginID ?? ""} 仍在加载。旧插件没有启用模型覆盖;请移除旧插件并完全重启 OpenCode。`;
|
|
185
|
+
}
|
|
186
|
+
const reason = migration?.errors?.join("; ");
|
|
187
|
+
const label = migration?.status === "unavailable" ? "旧设置迁移不可用" : "旧设置迁移失败";
|
|
188
|
+
return `检测到旧版插件 ${state.conflict?.pluginID ?? ""} 仍在加载,它会覆盖按 Agent 的模型设置。请移除旧插件并完全重启 OpenCode。${reason === undefined ? "" : `(${label}:${reason})`}`;
|
|
189
|
+
}
|
|
190
|
+
async function saveModel(ctx, location, agentID, agentName, model, hostModel) {
|
|
191
|
+
let result;
|
|
192
|
+
try {
|
|
193
|
+
result = await ctx.client.rpc(teamRpc).setAgentModel({
|
|
194
|
+
agentID,
|
|
195
|
+
model
|
|
196
|
+
}, {
|
|
197
|
+
location
|
|
198
|
+
});
|
|
199
|
+
} catch (error) {
|
|
200
|
+
return {
|
|
201
|
+
saved: false,
|
|
202
|
+
tone: "error",
|
|
203
|
+
message: `保存 Agent 模型失败:${message(error)}`
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
if (!result.ok) {
|
|
207
|
+
return {
|
|
208
|
+
saved: false,
|
|
209
|
+
tone: "error",
|
|
210
|
+
message: `保存 Agent 模型失败:${summarize(result)}`
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
return {
|
|
214
|
+
saved: true,
|
|
215
|
+
tone: "success",
|
|
216
|
+
message: model === null ? hostModel === undefined ? `已恢复 ${agentName} 的 OpenCode 原生模型选择` : `已清除 ${agentName} 的团队覆盖,回到 Agent 配置的 ${hostModel}` : `${agentName} 的子代理模型已设为 ${result.models?.[agentID] ?? model}`,
|
|
217
|
+
...(result.models === undefined ? {} : {
|
|
218
|
+
models: result.models
|
|
219
|
+
})
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
function summarize(result) {
|
|
223
|
+
return (result.errors ?? []).join("; ") || "未知错误";
|
|
224
|
+
}
|
|
225
|
+
function message(error) {
|
|
226
|
+
return error instanceof Error ? error.message : String(error);
|
|
227
|
+
}
|