@sanqianx/claude-ai-workbench 0.13.2 → 0.13.4
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/LICENSE +201 -201
- package/README.md +156 -156
- package/apps/agent-terminal/app.css +652 -613
- package/apps/agent-terminal/app.js +143 -67
- package/apps/agent-terminal/conversation-composer.js +44 -0
- package/apps/agent-terminal/index.html +172 -170
- package/apps/agent-terminal/panel-host-proof.html +152 -152
- package/apps/agent-terminal/panel-proof-content.html +41 -41
- package/apps/agent-terminal/right-workspace-dock.css +189 -159
- package/apps/agent-terminal/right-workspace-dock.js +652 -521
- package/apps/agent-terminal/user-question-card.js +68 -0
- package/apps/ai-profile-settings-host/index.html +25 -25
- package/apps/ai-profile-settings-host/page.css +1 -1
- package/apps/ai-profile-settings-host/page.js +113 -113
- package/apps/minimal-test-host/README.md +15 -15
- package/apps/minimal-test-host/host.css +2 -2
- package/apps/minimal-test-host/host.js +35 -35
- package/apps/minimal-test-host/index.html +58 -58
- package/apps/minimal-test-host/optional-mock/fault-injection.js +15 -15
- package/docs/sdk/FILE_LAYOUT.md +31 -31
- package/docs/sdk/INTEGRATION.md +126 -126
- package/docs/sdk/README.md +21 -21
- package/docs/sdk/SESSION_EVENTS.md +15 -13
- package/docs/sdk/openapi.yaml +16 -1
- package/package.json +1 -1
- package/packages/client/README.md +29 -29
- package/packages/client/index.d.ts +122 -122
- package/packages/client/index.js +260 -260
- package/packages/client/package.json +2 -2
- package/packages/contracts/README.md +9 -9
- package/packages/contracts/index.d.ts +519 -517
- package/packages/contracts/index.js +312 -312
- package/packages/contracts/package.json +1 -1
- package/packages/core/README.md +8 -8
- package/packages/core/index.d.ts +76 -76
- package/packages/core/index.js +222 -222
- package/packages/core/package.json +2 -2
- package/packages/server/README.md +17 -17
- package/packages/server/bin/agent-terminal-server.js +42 -42
- package/packages/server/bin/claude-workbench.js +225 -225
- package/packages/server/index.d.ts +171 -171
- package/packages/server/index.js +230 -230
- package/packages/server/lib/agent-discovery.js +133 -133
- package/packages/server/lib/agent-registry.js +143 -143
- package/packages/server/lib/agent-spawn-env.js +17 -17
- package/packages/server/lib/attachment-store.js +78 -78
- package/packages/server/lib/bridges/app-server-bridge.js +373 -373
- package/packages/server/lib/bridges/opencode-server-bridge.js +506 -506
- package/packages/server/lib/capture-settings-store.js +35 -35
- package/packages/server/lib/claude-history-recovery.js +87 -87
- package/packages/server/lib/cli-session-recovery.js +28 -28
- package/packages/server/lib/context-resolver.js +55 -55
- package/packages/server/lib/conversation-capture.js +51 -51
- package/packages/server/lib/delegation-phase-store.js +357 -357
- package/packages/server/lib/delegation-store.js +474 -468
- package/packages/server/lib/diagnostics.js +32 -32
- package/packages/server/lib/drivers/cli-json-runner.js +974 -974
- package/packages/server/lib/drivers/codex-driver.js +1461 -1442
- package/packages/server/lib/drivers/opencode-driver.js +1554 -1554
- package/packages/server/lib/drivers/zcode-driver.js +1250 -1250
- package/packages/server/lib/drivers/zcode-profile-runtime.js +71 -71
- package/packages/server/lib/event-stream.js +138 -138
- package/packages/server/lib/folder-picker.js +623 -623
- package/packages/server/lib/http-handler.js +339 -339
- package/packages/server/lib/json-file.js +26 -26
- package/packages/server/lib/knowledge-context.js +33 -33
- package/packages/server/lib/model-detection.js +295 -295
- package/packages/server/lib/parent-context.js +44 -0
- package/packages/server/lib/profile-store.js +283 -283
- package/packages/server/lib/project-store.js +102 -102
- package/packages/server/lib/runtime-loader.js +120 -120
- package/packages/server/lib/session-archive-store.js +53 -53
- package/packages/server/lib/session-history.js +612 -612
- package/packages/server/lib/system-doctor.js +125 -125
- package/packages/server/lib/ui-preferences-store.js +48 -48
- package/packages/server/lib/user-questions.js +67 -0
- package/packages/server/lib/workbench-service.js +1696 -1675
- package/packages/server/package.json +2 -2
- package/packages/ui/README.md +30 -30
- package/packages/ui/ai-profile-settings.d.ts +45 -45
- package/packages/ui/ai-profile-settings.js +283 -283
- package/packages/ui/claude-terminal.d.ts +31 -31
- package/packages/ui/claude-terminal.js +343 -343
- package/packages/ui/index.d.ts +18 -18
- package/packages/ui/index.js +51 -51
- package/packages/ui/package.json +2 -2
- package/packages/ui/theme.css +13 -13
- package/src/assets/claude-prompts.json +21 -21
- package/src/backend/http/ai-profile-routes.js +61 -61
- package/src/backend/http/claude-routes.js +170 -170
- package/src/backend/http/node-helpers.js +45 -45
- package/src/backend/index.js +21 -21
- package/src/backend/lib/ai-adapter.js +144 -144
- package/src/backend/lib/ai-profile-store.js +259 -259
- package/src/backend/lib/ai-vendor-presets.js +83 -83
- package/src/backend/lib/ai-workspace.js +140 -140
- package/src/backend/lib/automation-config.js +253 -253
- package/src/backend/lib/claude-cli-runner.js +2408 -2392
- package/src/backend/lib/data-dir.js +169 -169
- package/src/backend/lib/llm-client.js +176 -176
- package/src/backend/lib/model-context-windows.js +82 -82
- package/src/backend/lib/native-task-control.js +217 -217
- package/src/backend/lib/native-task-history-index.js +475 -475
- package/src/backend/lib/native-task-history-reader.js +526 -526
- package/src/backend/lib/native-task-messages.js +457 -457
- package/src/backend/lib/native-task-registry.js +325 -325
- package/src/backend/lib/native-task-runtime.js +242 -242
- package/src/backend/lib/native-task-trace.js +440 -440
- package/src/backend/lib/native-task-transcript.js +393 -393
- package/src/backend/lib/prompt-registry.js +166 -166
- package/src/backend/lib/runtime-config.js +121 -121
- package/src/backend/lib/sdk-session-stream.js +143 -143
- package/src/backend/lib/sdk-turn-stream.js +16 -16
- package/src/backend/lib/store-scope.js +131 -131
- package/src/backend/lib/task-identity.js +104 -104
- package/src/backend/lib/task-state-projection.js +58 -58
- package/src/index.js +7 -7
- package/src/sdk-wave-a.ts +1 -1
- package/src/sdk-wave-b.ts +1 -1
- package/src/sdk-wave-c.ts +1 -1
- package/src/sdk-wave-d.ts +1 -1
- package/src/sdk-wave-e.ts +1 -1
|
@@ -1,283 +1,283 @@
|
|
|
1
|
-
(function (global) {
|
|
2
|
-
"use strict";
|
|
3
|
-
|
|
4
|
-
if (!global.customElements || !global.HTMLElement) return;
|
|
5
|
-
|
|
6
|
-
const escapeHtml = (value) => String(value == null ? "" : value).replace(/[&<>"']/g, (character) => ({
|
|
7
|
-
"&": "&", "<": "<", ">": ">", "\"": """, "'": "'",
|
|
8
|
-
})[character]);
|
|
9
|
-
const clone = (value) => JSON.parse(JSON.stringify(value));
|
|
10
|
-
const eventButton = (event) => event.composedPath().find((node) => node?.matches?.("button")) || null;
|
|
11
|
-
|
|
12
|
-
const PRESETS = [
|
|
13
|
-
{ id: "anthropic", name: "Anthropic", mark: "A", color: "#a85f3d", baseUrl: "https://api.anthropic.com", model: "claude-sonnet-4-5" },
|
|
14
|
-
{ id: "minimax", name: "MiniMax", mark: "M", color: "#6554c0", baseUrl: "https://api.minimaxi.com/anthropic", model: "MiniMax-M3" },
|
|
15
|
-
{ id: "glm", name: "智谱 GLM", mark: "G", color: "#2563c4", baseUrl: "https://open.bigmodel.cn/api/anthropic", model: "glm-4.6" },
|
|
16
|
-
{ id: "deepseek", name: "DeepSeek", mark: "D", color: "#3158c8", baseUrl: "https://api.deepseek.com/anthropic", model: "deepseek-chat" },
|
|
17
|
-
{ id: "custom", name: "自定义", mark: "+", color: "#667085", baseUrl: "", model: "" },
|
|
18
|
-
];
|
|
19
|
-
|
|
20
|
-
function newProfile() {
|
|
21
|
-
const id = `profile-${Date.now().toString(36)}`;
|
|
22
|
-
return {
|
|
23
|
-
id, name: "", provider: "", runtime: "claude-code", protocol: "anthropic-compatible", baseUrl: "",
|
|
24
|
-
credentialRef: `credential:${id}`, credentialConfigured: false, credentialMasked: "",
|
|
25
|
-
models: { default: "", fast: "", reasoning: "", coding: "", largeContext: "" },
|
|
26
|
-
contextWindow: 200000, timeoutMs: 300000, permissionMode: "default",
|
|
27
|
-
systemPrompt: { type: "preset", preset: "claude_code", content: "" }, enabled: true,
|
|
28
|
-
};
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
function hostOf(value) {
|
|
32
|
-
try { return new URL(value).hostname; } catch { return value || "未配置地址"; }
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
function presetFor(profile) {
|
|
36
|
-
const provider = String(profile?.provider || "").toLowerCase();
|
|
37
|
-
const direct = PRESETS.find((item) => item.id !== "custom" && (provider.includes(item.id) || provider.includes(item.name.toLowerCase())));
|
|
38
|
-
if (direct) return direct;
|
|
39
|
-
const host = hostOf(profile?.baseUrl).toLowerCase();
|
|
40
|
-
if (host.includes("minimax")) return PRESETS[1];
|
|
41
|
-
if (host.includes("bigmodel") || host.includes("zhipu")) return PRESETS[2];
|
|
42
|
-
if (host.includes("deepseek")) return PRESETS[3];
|
|
43
|
-
if (host === "api.anthropic.com") return PRESETS[0];
|
|
44
|
-
return PRESETS[4];
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
const common = `
|
|
48
|
-
:host{display:block;min-width:0;color:var(--caw-text,#182230);font:13px/1.45 Inter,ui-sans-serif,-apple-system,BlinkMacSystemFont,"Segoe UI","Microsoft YaHei",sans-serif}
|
|
49
|
-
*{box-sizing:border-box}button,input,select{font:inherit;color:inherit}button{cursor:pointer}.button{height:32px;padding:0 11px;border:1px solid var(--caw-line,#d7dfe8);border-radius:7px;background:var(--caw-panel,#fff);font-size:11px}.button:hover{border-color:var(--caw-line-strong,#c4ced9)}.button.primary{border-color:transparent;background:var(--caw-accent,#4f6f8f);color:#fff}.button:disabled{cursor:not-allowed;opacity:.5}
|
|
50
|
-
`;
|
|
51
|
-
|
|
52
|
-
class AiProfileList extends HTMLElement {
|
|
53
|
-
constructor() {
|
|
54
|
-
super();
|
|
55
|
-
this.attachShadow({ mode: "open" });
|
|
56
|
-
this._profiles = [];
|
|
57
|
-
this._busy = false;
|
|
58
|
-
this.shadowRoot.addEventListener("click", (event) => this.handleClick(event));
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
connectedCallback() { this.render(); }
|
|
62
|
-
set profiles(value) { this._profiles = Array.isArray(value) ? clone(value) : []; this.render(); }
|
|
63
|
-
get profiles() { return clone(this._profiles); }
|
|
64
|
-
set busy(value) { this._busy = Boolean(value); this.render(); }
|
|
65
|
-
|
|
66
|
-
render() {
|
|
67
|
-
if (!this.isConnected) return;
|
|
68
|
-
this.shadowRoot.innerHTML = `<style>${common}
|
|
69
|
-
.list{border:1px solid var(--caw-line);border-radius:9px;overflow:hidden;background:var(--caw-panel)}.row{display:grid;grid-template-columns:minmax(0,1fr) auto;align-items:center;gap:12px;padding:11px 12px;border-bottom:1px solid var(--caw-line)}.row:last-child{border-bottom:0}.identity{display:flex;align-items:center;gap:10px;min-width:0}.logo{display:grid;place-items:center;flex:none;width:30px;height:30px;border-radius:8px;color:#fff;font-size:11px;font-weight:800}.copy{min-width:0}.copy strong{display:block;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;font-size:12px}.meta{display:flex;gap:7px;margin-top:3px;color:var(--caw-muted);font-size:9px;white-space:nowrap;overflow:hidden}.meta span{overflow:hidden;text-overflow:ellipsis}.state{display:inline-flex;align-items:center;gap:4px;flex:none;color:var(--caw-muted);font-size:9px}.state.good{color:var(--caw-good)}.dot{width:6px;height:6px;border-radius:50%;background:currentColor}.actions{display:flex;align-items:center;gap:5px}.actions button{height:27px;padding:0 7px;border:1px solid transparent;border-radius:6px;background:transparent;color:var(--caw-muted);font-size:9px}.actions button:hover{border-color:var(--caw-line);background:var(--caw-panel-2);color:var(--caw-text)}.actions .remove:hover{color:var(--caw-bad)}.empty{padding:32px 16px;border:1px dashed var(--caw-line-strong);border-radius:9px;color:var(--caw-muted);text-align:center;font-size:10px}.empty strong{display:block;margin-bottom:4px;color:var(--caw-text);font-size:12px}@media(max-width:520px){.row{grid-template-columns:1fr}.actions{padding-left:40px}.state{display:none}}
|
|
70
|
-
</style>${this._profiles.length ? `<div class="list">${this._profiles.map((profile) => this.row(profile)).join("")}</div>` : '<div class="empty"><strong>暂无模型配置</strong>点击“新增”创建第一个模型。</div>'}`;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
row(profile) {
|
|
74
|
-
const preset = presetFor(profile);
|
|
75
|
-
return `<div class="row"><div class="identity"><span class="logo" style="background:${preset.color}">${escapeHtml(preset.mark)}</span><div class="copy"><strong>${escapeHtml(profile.name || profile.models?.default || "未命名模型")}</strong><div class="meta"><span>${escapeHtml(profile.models?.default || "未配置模型")}</span><span>·</span><span>${escapeHtml(hostOf(profile.baseUrl))}</span></div></div></div><div class="actions"><span class="state ${profile.credentialConfigured ? "good" : ""}"><i class="dot"></i>${profile.credentialConfigured ? "Key 已配置" : "缺少 Key"}</span><button data-action="test" data-id="${escapeHtml(profile.id)}" ${this._busy ? "disabled" : ""}>测试</button><button data-action="edit" data-id="${escapeHtml(profile.id)}" ${this._busy ? "disabled" : ""}>编辑</button><button class="remove" data-action="delete" data-id="${escapeHtml(profile.id)}" ${this._busy ? "disabled" : ""}>删除</button></div></div>`;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
handleClick(event) {
|
|
79
|
-
const button = eventButton(event);
|
|
80
|
-
if (!button?.dataset.action) return;
|
|
81
|
-
const profile = this._profiles.find((item) => item.id === button.dataset.id);
|
|
82
|
-
if (!profile) return;
|
|
83
|
-
this.dispatchEvent(new CustomEvent(`profile-${button.dataset.action}`, {
|
|
84
|
-
bubbles: true, composed: true, detail: { profile: clone(profile), credential: "" },
|
|
85
|
-
}));
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
class AiProfileEditor extends HTMLElement {
|
|
90
|
-
constructor() {
|
|
91
|
-
super();
|
|
92
|
-
this.attachShadow({ mode: "open" });
|
|
93
|
-
this._profile = newProfile();
|
|
94
|
-
this._credential = "";
|
|
95
|
-
this._editingCredential = true;
|
|
96
|
-
this._presetId = "custom";
|
|
97
|
-
this._status = "";
|
|
98
|
-
this._tone = "";
|
|
99
|
-
this._busy = false;
|
|
100
|
-
this.shadowRoot.addEventListener("click", (event) => this.handleClick(event));
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
connectedCallback() { this.render(); }
|
|
104
|
-
set profile(value) {
|
|
105
|
-
this._profile = clone(value || newProfile());
|
|
106
|
-
this._credential = "";
|
|
107
|
-
this._editingCredential = !this._profile.credentialConfigured;
|
|
108
|
-
this._presetId = presetFor(this._profile).id;
|
|
109
|
-
this.render();
|
|
110
|
-
}
|
|
111
|
-
get profile() { this.capture(); return clone(this._profile); }
|
|
112
|
-
set busy(value) { this.setBusy(value); }
|
|
113
|
-
setStatus(message, tone = "") { this._status = String(message || ""); this._tone = tone; this.render(true); }
|
|
114
|
-
setBusy(value, message) { this._busy = Boolean(value); if (message) this._status = message; this.render(true); }
|
|
115
|
-
|
|
116
|
-
capture() {
|
|
117
|
-
const form = this.shadowRoot?.querySelector("form");
|
|
118
|
-
if (!form) return;
|
|
119
|
-
const data = new FormData(form);
|
|
120
|
-
const model = String(data.get("model") || "").trim();
|
|
121
|
-
const id = this._profile.id;
|
|
122
|
-
if (this._editingCredential) this._credential = String(data.get("apiKey") || "");
|
|
123
|
-
this._profile = {
|
|
124
|
-
...this._profile,
|
|
125
|
-
id,
|
|
126
|
-
name: String(data.get("name") || "").trim(),
|
|
127
|
-
provider: String(data.get("provider") || "").trim(),
|
|
128
|
-
baseUrl: String(data.get("baseUrl") || "").trim().replace(/\/+$/, ""),
|
|
129
|
-
credentialRef: this._profile.credentialRef || `credential:${id}`,
|
|
130
|
-
runtime: "claude-code",
|
|
131
|
-
protocol: String(data.get("protocol") || "anthropic-compatible"),
|
|
132
|
-
models: { default: model, fast: model, reasoning: model, coding: model, largeContext: model },
|
|
133
|
-
timeoutMs: Number(data.get("timeoutMs") || 300000),
|
|
134
|
-
contextWindow: 200000,
|
|
135
|
-
permissionMode: "default",
|
|
136
|
-
systemPrompt: { type: "preset", preset: "claude_code", content: "" },
|
|
137
|
-
enabled: true,
|
|
138
|
-
};
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
credentialField(profile) {
|
|
142
|
-
if (profile.credentialConfigured && !this._editingCredential) {
|
|
143
|
-
const masked = profile.credentialMasked || "已安全保存";
|
|
144
|
-
return `<input type="text" value="${escapeHtml(masked)}" readonly aria-label="API Key 已保存"><button type="button" class="key-action" data-action="replace-key">更换</button>`;
|
|
145
|
-
}
|
|
146
|
-
return `<input name="apiKey" type="text" autocomplete="off" spellcheck="false" value="${escapeHtml(this._credential)}" placeholder="请输入 API Key" aria-label="API Key">${profile.credentialConfigured ? '<button type="button" class="key-action" data-action="cancel-key">取消更换</button>' : '<span class="key-state">输入可见</span>'}`;
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
render(preserve = false) {
|
|
150
|
-
if (!this.isConnected) return;
|
|
151
|
-
if (preserve) this.capture();
|
|
152
|
-
const profile = this._profile;
|
|
153
|
-
const exists = Boolean(profile.name || profile.provider || profile.baseUrl || profile.models?.default);
|
|
154
|
-
this.shadowRoot.innerHTML = `<style>${common}
|
|
155
|
-
.editor{border:1px solid var(--caw-line);border-radius:9px;background:var(--caw-panel);overflow:hidden}.head,.foot{display:flex;align-items:center;justify-content:space-between;gap:12px;padding:11px 13px}.head{border-bottom:1px solid var(--caw-line)}.head strong{font-size:12px}.head span{display:block;margin-top:2px;color:var(--caw-muted);font-size:9px}.body{padding:14px}.presets{display:flex;flex-wrap:wrap;gap:6px;margin-bottom:14px}.preset{display:flex;align-items:center;gap:6px;height:31px;padding:0 9px;border:1px solid var(--caw-line);border-radius:7px;background:var(--caw-panel-2);font-size:9px}.preset.active{border-color:var(--caw-accent);background:var(--caw-accent-soft)}.mark{display:grid;place-items:center;width:18px;height:18px;border-radius:5px;color:#fff;font-size:8px;font-weight:800}.fields{display:grid;grid-template-columns:1fr 1fr;gap:11px}label{display:grid;gap:5px;min-width:0;color:var(--caw-muted);font-size:9px}label.wide{grid-column:1/-1}input,select{width:100%;height:36px;padding:0 9px;border:1px solid var(--caw-line);border-radius:7px;background:var(--caw-panel-2);outline:0}input[readonly]{color:var(--caw-muted);background:var(--caw-panel)}input:focus,select:focus{border-color:var(--caw-accent);box-shadow:0 0 0 2px var(--caw-accent-soft)}.key{display:flex;gap:6px}.key input{flex:1}.key-state,.key-action{display:grid;place-items:center;flex:none;padding:0 9px;border:1px solid var(--caw-line);border-radius:7px;background:var(--caw-panel);color:var(--caw-muted);font-size:8px}.key-action:hover{border-color:var(--caw-accent);color:var(--caw-text)}details{margin-top:11px;border-top:1px solid var(--caw-line)}summary{padding-top:10px;color:var(--caw-muted);font-size:9px;cursor:pointer}.advanced{padding-top:10px}.foot{border-top:1px solid var(--caw-line)}.feedback{min-width:0;color:var(--caw-muted);font-size:9px;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}.feedback.good{color:var(--caw-good)}.feedback.bad{color:var(--caw-bad)}.actions{display:flex;gap:6px}@media(max-width:520px){.fields{grid-template-columns:1fr}label.wide{grid-column:auto}.presets{display:grid;grid-template-columns:1fr 1fr}}
|
|
156
|
-
</style><section class="editor"><header class="head"><div><strong>${exists ? "编辑模型" : "新增模型"}</strong><span>测试只验证当前内容,保存后配置才会生效</span></div><button class="button" data-action="cancel">返回</button></header><div class="body"><div class="presets">${PRESETS.map((item) => `<button type="button" class="preset ${item.id === this._presetId ? "active" : ""}" data-preset="${item.id}"><i class="mark" style="background:${item.color}">${escapeHtml(item.mark)}</i>${escapeHtml(item.name)}</button>`).join("")}</div><form><div class="fields"><label>配置名称<input name="name" value="${escapeHtml(profile.name || "")}" placeholder="例如 MiniMax 主配置"></label><label>供应商<input name="provider" value="${escapeHtml(profile.provider || "")}" placeholder="例如 MiniMax"></label><label class="wide">请求地址<input name="baseUrl" value="${escapeHtml(profile.baseUrl || "")}" placeholder="https://api.example.com/anthropic"></label><label class="wide">模型名称<input name="model" value="${escapeHtml(profile.models?.default || "")}" placeholder="例如 MiniMax-M3"></label><label class="wide">API Key<div class="key">${this.credentialField(profile)}</div></label></div><details><summary>高级设置</summary><div class="fields advanced"><label>API 协议<select name="protocol"><option value="anthropic-compatible" ${profile.protocol !== "openai-compatible" ? "selected" : ""}>Anthropic Compatible</option><option value="openai-compatible" ${profile.protocol === "openai-compatible" ? "selected" : ""}>OpenAI Compatible</option></select></label><label>请求超时<input name="timeoutMs" type="number" min="1000" value="${Number(profile.timeoutMs || 300000)}"></label></div></details></form></div><footer class="foot"><span class="feedback ${escapeHtml(this._tone)}">${escapeHtml(this._status)}</span><div class="actions"><button class="button" data-action="test" ${this._busy ? "disabled" : ""}>${this._busy ? "测试中…" : "测试连接"}</button><button class="button primary" data-action="save" ${this._busy ? "disabled" : ""}>保存</button></div></footer></section>`;
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
payload() {
|
|
160
|
-
this.capture();
|
|
161
|
-
const profile = this._profile;
|
|
162
|
-
if (!profile.name) throw new Error("请填写配置名称");
|
|
163
|
-
if (!profile.provider) throw new Error("请填写供应商");
|
|
164
|
-
if (!profile.baseUrl) throw new Error("请填写请求地址");
|
|
165
|
-
if (!profile.models.default) throw new Error("请填写模型名称");
|
|
166
|
-
if (!profile.credentialConfigured && !this._credential) throw new Error("请输入 API Key");
|
|
167
|
-
return { profile: clone(profile), credential: this._editingCredential ? this._credential : "" };
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
emit(name) {
|
|
171
|
-
try {
|
|
172
|
-
this.dispatchEvent(new CustomEvent(name, { bubbles: true, composed: true, detail: this.payload() }));
|
|
173
|
-
} catch (error) {
|
|
174
|
-
this.setStatus(error.message, "bad");
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
handleClick(event) {
|
|
179
|
-
const button = eventButton(event);
|
|
180
|
-
if (!button) return;
|
|
181
|
-
if (button.dataset.action === "cancel") this.dispatchEvent(new CustomEvent("profile-cancel", { bubbles: true, composed: true }));
|
|
182
|
-
if (button.dataset.action === "save") this.emit("profile-save");
|
|
183
|
-
if (button.dataset.action === "test") this.emit("profile-test");
|
|
184
|
-
if (button.dataset.action === "replace-key") {
|
|
185
|
-
this.capture();
|
|
186
|
-
this._editingCredential = true;
|
|
187
|
-
this._credential = "";
|
|
188
|
-
this._status = "";
|
|
189
|
-
this.render();
|
|
190
|
-
this.shadowRoot.querySelector('[name="apiKey"]')?.focus();
|
|
191
|
-
}
|
|
192
|
-
if (button.dataset.action === "cancel-key") {
|
|
193
|
-
this.capture();
|
|
194
|
-
this._editingCredential = false;
|
|
195
|
-
this._credential = "";
|
|
196
|
-
this.render();
|
|
197
|
-
}
|
|
198
|
-
if (button.dataset.preset) {
|
|
199
|
-
this.capture();
|
|
200
|
-
const preset = PRESETS.find((item) => item.id === button.dataset.preset) || PRESETS[4];
|
|
201
|
-
this._presetId = preset.id;
|
|
202
|
-
this._profile.provider = preset.id === "custom" ? "" : preset.name;
|
|
203
|
-
this._profile.name = preset.id === "custom" ? "" : `${preset.name} 配置`;
|
|
204
|
-
this._profile.baseUrl = preset.baseUrl;
|
|
205
|
-
this._profile.models = { default: preset.model, fast: preset.model, reasoning: preset.model, coding: preset.model, largeContext: preset.model };
|
|
206
|
-
this.render();
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
class AiProfileSettings extends HTMLElement {
|
|
212
|
-
constructor() {
|
|
213
|
-
super();
|
|
214
|
-
this.attachShadow({ mode: "open" });
|
|
215
|
-
this._profiles = [];
|
|
216
|
-
this._view = "list";
|
|
217
|
-
this._draft = null;
|
|
218
|
-
this._status = "";
|
|
219
|
-
this._tone = "";
|
|
220
|
-
this._busy = false;
|
|
221
|
-
this.shadowRoot.addEventListener("click", (event) => {
|
|
222
|
-
if (event.target.closest("[data-add]")) this.openNew();
|
|
223
|
-
});
|
|
224
|
-
this.addEventListener("profile-edit", (event) => {
|
|
225
|
-
event.stopPropagation();
|
|
226
|
-
this.openEditor(event.detail.profile);
|
|
227
|
-
});
|
|
228
|
-
this.addEventListener("profile-cancel", (event) => {
|
|
229
|
-
event.stopPropagation();
|
|
230
|
-
this.closeEditor();
|
|
231
|
-
});
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
connectedCallback() { this.render(); }
|
|
235
|
-
set profiles(value) { this._profiles = Array.isArray(value) ? clone(value) : []; this.render(); }
|
|
236
|
-
get profiles() { return clone(this._profiles); }
|
|
237
|
-
setStatus(message, tone = "") {
|
|
238
|
-
this._status = String(message || "");
|
|
239
|
-
this._tone = tone;
|
|
240
|
-
const feedback = this.shadowRoot?.querySelector(".feedback");
|
|
241
|
-
if (feedback) { feedback.textContent = this._status; feedback.className = `feedback ${tone}`; }
|
|
242
|
-
this.shadowRoot?.querySelector("ai-profile-editor")?.setStatus(this._status, tone);
|
|
243
|
-
}
|
|
244
|
-
setBusy(value, message) {
|
|
245
|
-
this._busy = Boolean(value);
|
|
246
|
-
if (message) this._status = message;
|
|
247
|
-
const list = this.shadowRoot?.querySelector("ai-profile-list");
|
|
248
|
-
if (list) list.busy = this._busy;
|
|
249
|
-
this.shadowRoot?.querySelector("ai-profile-editor")?.setBusy(this._busy, message);
|
|
250
|
-
}
|
|
251
|
-
openNew() { this._draft = newProfile(); this._view = "editor"; this._status = ""; this.render(); }
|
|
252
|
-
closeEditor() { this._view = "list"; this._draft = null; this.render(); }
|
|
253
|
-
openEditor(profile) { this._draft = clone(profile); this._view = "editor"; this._status = ""; this.render(); }
|
|
254
|
-
|
|
255
|
-
render() {
|
|
256
|
-
if (!this.isConnected) return;
|
|
257
|
-
this.shadowRoot.innerHTML = `<style>${common}.module{width:100%;min-width:0}.bar{display:flex;align-items:center;justify-content:space-between;gap:12px;margin-bottom:9px}.bar strong{font-size:13px}.feedback{min-width:0;color:var(--caw-muted);font-size:9px;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}.feedback.good{color:var(--caw-good)}.feedback.bad{color:var(--caw-bad)}.bar-actions{display:flex;align-items:center;gap:9px}</style><section class="module">${this._view === "list" ? `<div class="bar"><strong>AI 模型</strong><div class="bar-actions"><span class="feedback ${escapeHtml(this._tone)}">${escapeHtml(this._status)}</span><button class="button primary" data-add>新增</button></div></div><ai-profile-list></ai-profile-list>` : "<ai-profile-editor></ai-profile-editor>"}</section>`;
|
|
258
|
-
this.bind();
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
bind() {
|
|
262
|
-
const list = this.shadowRoot.querySelector("ai-profile-list");
|
|
263
|
-
if (list) {
|
|
264
|
-
list.profiles = this._profiles;
|
|
265
|
-
list.busy = this._busy;
|
|
266
|
-
list.addEventListener("profile-edit", (event) => this.openEditor(event.detail.profile));
|
|
267
|
-
}
|
|
268
|
-
const editor = this.shadowRoot.querySelector("ai-profile-editor");
|
|
269
|
-
if (editor) {
|
|
270
|
-
editor.profile = this._draft;
|
|
271
|
-
editor.setStatus(this._status, this._tone);
|
|
272
|
-
editor.setBusy(this._busy);
|
|
273
|
-
}
|
|
274
|
-
}
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
if (!global.customElements.get("ai-profile-list")) global.customElements.define("ai-profile-list", AiProfileList);
|
|
278
|
-
if (!global.customElements.get("ai-profile-editor")) global.customElements.define("ai-profile-editor", AiProfileEditor);
|
|
279
|
-
if (!global.customElements.get("ai-profile-settings")) global.customElements.define("ai-profile-settings", AiProfileSettings);
|
|
280
|
-
global.AiProfileList = AiProfileList;
|
|
281
|
-
global.AiProfileEditor = AiProfileEditor;
|
|
282
|
-
global.AiProfileSettings = AiProfileSettings;
|
|
283
|
-
})(typeof window !== "undefined" ? window : globalThis);
|
|
1
|
+
(function (global) {
|
|
2
|
+
"use strict";
|
|
3
|
+
|
|
4
|
+
if (!global.customElements || !global.HTMLElement) return;
|
|
5
|
+
|
|
6
|
+
const escapeHtml = (value) => String(value == null ? "" : value).replace(/[&<>"']/g, (character) => ({
|
|
7
|
+
"&": "&", "<": "<", ">": ">", "\"": """, "'": "'",
|
|
8
|
+
})[character]);
|
|
9
|
+
const clone = (value) => JSON.parse(JSON.stringify(value));
|
|
10
|
+
const eventButton = (event) => event.composedPath().find((node) => node?.matches?.("button")) || null;
|
|
11
|
+
|
|
12
|
+
const PRESETS = [
|
|
13
|
+
{ id: "anthropic", name: "Anthropic", mark: "A", color: "#a85f3d", baseUrl: "https://api.anthropic.com", model: "claude-sonnet-4-5" },
|
|
14
|
+
{ id: "minimax", name: "MiniMax", mark: "M", color: "#6554c0", baseUrl: "https://api.minimaxi.com/anthropic", model: "MiniMax-M3" },
|
|
15
|
+
{ id: "glm", name: "智谱 GLM", mark: "G", color: "#2563c4", baseUrl: "https://open.bigmodel.cn/api/anthropic", model: "glm-4.6" },
|
|
16
|
+
{ id: "deepseek", name: "DeepSeek", mark: "D", color: "#3158c8", baseUrl: "https://api.deepseek.com/anthropic", model: "deepseek-chat" },
|
|
17
|
+
{ id: "custom", name: "自定义", mark: "+", color: "#667085", baseUrl: "", model: "" },
|
|
18
|
+
];
|
|
19
|
+
|
|
20
|
+
function newProfile() {
|
|
21
|
+
const id = `profile-${Date.now().toString(36)}`;
|
|
22
|
+
return {
|
|
23
|
+
id, name: "", provider: "", runtime: "claude-code", protocol: "anthropic-compatible", baseUrl: "",
|
|
24
|
+
credentialRef: `credential:${id}`, credentialConfigured: false, credentialMasked: "",
|
|
25
|
+
models: { default: "", fast: "", reasoning: "", coding: "", largeContext: "" },
|
|
26
|
+
contextWindow: 200000, timeoutMs: 300000, permissionMode: "default",
|
|
27
|
+
systemPrompt: { type: "preset", preset: "claude_code", content: "" }, enabled: true,
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function hostOf(value) {
|
|
32
|
+
try { return new URL(value).hostname; } catch { return value || "未配置地址"; }
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function presetFor(profile) {
|
|
36
|
+
const provider = String(profile?.provider || "").toLowerCase();
|
|
37
|
+
const direct = PRESETS.find((item) => item.id !== "custom" && (provider.includes(item.id) || provider.includes(item.name.toLowerCase())));
|
|
38
|
+
if (direct) return direct;
|
|
39
|
+
const host = hostOf(profile?.baseUrl).toLowerCase();
|
|
40
|
+
if (host.includes("minimax")) return PRESETS[1];
|
|
41
|
+
if (host.includes("bigmodel") || host.includes("zhipu")) return PRESETS[2];
|
|
42
|
+
if (host.includes("deepseek")) return PRESETS[3];
|
|
43
|
+
if (host === "api.anthropic.com") return PRESETS[0];
|
|
44
|
+
return PRESETS[4];
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const common = `
|
|
48
|
+
:host{display:block;min-width:0;color:var(--caw-text,#182230);font:13px/1.45 Inter,ui-sans-serif,-apple-system,BlinkMacSystemFont,"Segoe UI","Microsoft YaHei",sans-serif}
|
|
49
|
+
*{box-sizing:border-box}button,input,select{font:inherit;color:inherit}button{cursor:pointer}.button{height:32px;padding:0 11px;border:1px solid var(--caw-line,#d7dfe8);border-radius:7px;background:var(--caw-panel,#fff);font-size:11px}.button:hover{border-color:var(--caw-line-strong,#c4ced9)}.button.primary{border-color:transparent;background:var(--caw-accent,#4f6f8f);color:#fff}.button:disabled{cursor:not-allowed;opacity:.5}
|
|
50
|
+
`;
|
|
51
|
+
|
|
52
|
+
class AiProfileList extends HTMLElement {
|
|
53
|
+
constructor() {
|
|
54
|
+
super();
|
|
55
|
+
this.attachShadow({ mode: "open" });
|
|
56
|
+
this._profiles = [];
|
|
57
|
+
this._busy = false;
|
|
58
|
+
this.shadowRoot.addEventListener("click", (event) => this.handleClick(event));
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
connectedCallback() { this.render(); }
|
|
62
|
+
set profiles(value) { this._profiles = Array.isArray(value) ? clone(value) : []; this.render(); }
|
|
63
|
+
get profiles() { return clone(this._profiles); }
|
|
64
|
+
set busy(value) { this._busy = Boolean(value); this.render(); }
|
|
65
|
+
|
|
66
|
+
render() {
|
|
67
|
+
if (!this.isConnected) return;
|
|
68
|
+
this.shadowRoot.innerHTML = `<style>${common}
|
|
69
|
+
.list{border:1px solid var(--caw-line);border-radius:9px;overflow:hidden;background:var(--caw-panel)}.row{display:grid;grid-template-columns:minmax(0,1fr) auto;align-items:center;gap:12px;padding:11px 12px;border-bottom:1px solid var(--caw-line)}.row:last-child{border-bottom:0}.identity{display:flex;align-items:center;gap:10px;min-width:0}.logo{display:grid;place-items:center;flex:none;width:30px;height:30px;border-radius:8px;color:#fff;font-size:11px;font-weight:800}.copy{min-width:0}.copy strong{display:block;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;font-size:12px}.meta{display:flex;gap:7px;margin-top:3px;color:var(--caw-muted);font-size:9px;white-space:nowrap;overflow:hidden}.meta span{overflow:hidden;text-overflow:ellipsis}.state{display:inline-flex;align-items:center;gap:4px;flex:none;color:var(--caw-muted);font-size:9px}.state.good{color:var(--caw-good)}.dot{width:6px;height:6px;border-radius:50%;background:currentColor}.actions{display:flex;align-items:center;gap:5px}.actions button{height:27px;padding:0 7px;border:1px solid transparent;border-radius:6px;background:transparent;color:var(--caw-muted);font-size:9px}.actions button:hover{border-color:var(--caw-line);background:var(--caw-panel-2);color:var(--caw-text)}.actions .remove:hover{color:var(--caw-bad)}.empty{padding:32px 16px;border:1px dashed var(--caw-line-strong);border-radius:9px;color:var(--caw-muted);text-align:center;font-size:10px}.empty strong{display:block;margin-bottom:4px;color:var(--caw-text);font-size:12px}@media(max-width:520px){.row{grid-template-columns:1fr}.actions{padding-left:40px}.state{display:none}}
|
|
70
|
+
</style>${this._profiles.length ? `<div class="list">${this._profiles.map((profile) => this.row(profile)).join("")}</div>` : '<div class="empty"><strong>暂无模型配置</strong>点击“新增”创建第一个模型。</div>'}`;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
row(profile) {
|
|
74
|
+
const preset = presetFor(profile);
|
|
75
|
+
return `<div class="row"><div class="identity"><span class="logo" style="background:${preset.color}">${escapeHtml(preset.mark)}</span><div class="copy"><strong>${escapeHtml(profile.name || profile.models?.default || "未命名模型")}</strong><div class="meta"><span>${escapeHtml(profile.models?.default || "未配置模型")}</span><span>·</span><span>${escapeHtml(hostOf(profile.baseUrl))}</span></div></div></div><div class="actions"><span class="state ${profile.credentialConfigured ? "good" : ""}"><i class="dot"></i>${profile.credentialConfigured ? "Key 已配置" : "缺少 Key"}</span><button data-action="test" data-id="${escapeHtml(profile.id)}" ${this._busy ? "disabled" : ""}>测试</button><button data-action="edit" data-id="${escapeHtml(profile.id)}" ${this._busy ? "disabled" : ""}>编辑</button><button class="remove" data-action="delete" data-id="${escapeHtml(profile.id)}" ${this._busy ? "disabled" : ""}>删除</button></div></div>`;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
handleClick(event) {
|
|
79
|
+
const button = eventButton(event);
|
|
80
|
+
if (!button?.dataset.action) return;
|
|
81
|
+
const profile = this._profiles.find((item) => item.id === button.dataset.id);
|
|
82
|
+
if (!profile) return;
|
|
83
|
+
this.dispatchEvent(new CustomEvent(`profile-${button.dataset.action}`, {
|
|
84
|
+
bubbles: true, composed: true, detail: { profile: clone(profile), credential: "" },
|
|
85
|
+
}));
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
class AiProfileEditor extends HTMLElement {
|
|
90
|
+
constructor() {
|
|
91
|
+
super();
|
|
92
|
+
this.attachShadow({ mode: "open" });
|
|
93
|
+
this._profile = newProfile();
|
|
94
|
+
this._credential = "";
|
|
95
|
+
this._editingCredential = true;
|
|
96
|
+
this._presetId = "custom";
|
|
97
|
+
this._status = "";
|
|
98
|
+
this._tone = "";
|
|
99
|
+
this._busy = false;
|
|
100
|
+
this.shadowRoot.addEventListener("click", (event) => this.handleClick(event));
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
connectedCallback() { this.render(); }
|
|
104
|
+
set profile(value) {
|
|
105
|
+
this._profile = clone(value || newProfile());
|
|
106
|
+
this._credential = "";
|
|
107
|
+
this._editingCredential = !this._profile.credentialConfigured;
|
|
108
|
+
this._presetId = presetFor(this._profile).id;
|
|
109
|
+
this.render();
|
|
110
|
+
}
|
|
111
|
+
get profile() { this.capture(); return clone(this._profile); }
|
|
112
|
+
set busy(value) { this.setBusy(value); }
|
|
113
|
+
setStatus(message, tone = "") { this._status = String(message || ""); this._tone = tone; this.render(true); }
|
|
114
|
+
setBusy(value, message) { this._busy = Boolean(value); if (message) this._status = message; this.render(true); }
|
|
115
|
+
|
|
116
|
+
capture() {
|
|
117
|
+
const form = this.shadowRoot?.querySelector("form");
|
|
118
|
+
if (!form) return;
|
|
119
|
+
const data = new FormData(form);
|
|
120
|
+
const model = String(data.get("model") || "").trim();
|
|
121
|
+
const id = this._profile.id;
|
|
122
|
+
if (this._editingCredential) this._credential = String(data.get("apiKey") || "");
|
|
123
|
+
this._profile = {
|
|
124
|
+
...this._profile,
|
|
125
|
+
id,
|
|
126
|
+
name: String(data.get("name") || "").trim(),
|
|
127
|
+
provider: String(data.get("provider") || "").trim(),
|
|
128
|
+
baseUrl: String(data.get("baseUrl") || "").trim().replace(/\/+$/, ""),
|
|
129
|
+
credentialRef: this._profile.credentialRef || `credential:${id}`,
|
|
130
|
+
runtime: "claude-code",
|
|
131
|
+
protocol: String(data.get("protocol") || "anthropic-compatible"),
|
|
132
|
+
models: { default: model, fast: model, reasoning: model, coding: model, largeContext: model },
|
|
133
|
+
timeoutMs: Number(data.get("timeoutMs") || 300000),
|
|
134
|
+
contextWindow: 200000,
|
|
135
|
+
permissionMode: "default",
|
|
136
|
+
systemPrompt: { type: "preset", preset: "claude_code", content: "" },
|
|
137
|
+
enabled: true,
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
credentialField(profile) {
|
|
142
|
+
if (profile.credentialConfigured && !this._editingCredential) {
|
|
143
|
+
const masked = profile.credentialMasked || "已安全保存";
|
|
144
|
+
return `<input type="text" value="${escapeHtml(masked)}" readonly aria-label="API Key 已保存"><button type="button" class="key-action" data-action="replace-key">更换</button>`;
|
|
145
|
+
}
|
|
146
|
+
return `<input name="apiKey" type="text" autocomplete="off" spellcheck="false" value="${escapeHtml(this._credential)}" placeholder="请输入 API Key" aria-label="API Key">${profile.credentialConfigured ? '<button type="button" class="key-action" data-action="cancel-key">取消更换</button>' : '<span class="key-state">输入可见</span>'}`;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
render(preserve = false) {
|
|
150
|
+
if (!this.isConnected) return;
|
|
151
|
+
if (preserve) this.capture();
|
|
152
|
+
const profile = this._profile;
|
|
153
|
+
const exists = Boolean(profile.name || profile.provider || profile.baseUrl || profile.models?.default);
|
|
154
|
+
this.shadowRoot.innerHTML = `<style>${common}
|
|
155
|
+
.editor{border:1px solid var(--caw-line);border-radius:9px;background:var(--caw-panel);overflow:hidden}.head,.foot{display:flex;align-items:center;justify-content:space-between;gap:12px;padding:11px 13px}.head{border-bottom:1px solid var(--caw-line)}.head strong{font-size:12px}.head span{display:block;margin-top:2px;color:var(--caw-muted);font-size:9px}.body{padding:14px}.presets{display:flex;flex-wrap:wrap;gap:6px;margin-bottom:14px}.preset{display:flex;align-items:center;gap:6px;height:31px;padding:0 9px;border:1px solid var(--caw-line);border-radius:7px;background:var(--caw-panel-2);font-size:9px}.preset.active{border-color:var(--caw-accent);background:var(--caw-accent-soft)}.mark{display:grid;place-items:center;width:18px;height:18px;border-radius:5px;color:#fff;font-size:8px;font-weight:800}.fields{display:grid;grid-template-columns:1fr 1fr;gap:11px}label{display:grid;gap:5px;min-width:0;color:var(--caw-muted);font-size:9px}label.wide{grid-column:1/-1}input,select{width:100%;height:36px;padding:0 9px;border:1px solid var(--caw-line);border-radius:7px;background:var(--caw-panel-2);outline:0}input[readonly]{color:var(--caw-muted);background:var(--caw-panel)}input:focus,select:focus{border-color:var(--caw-accent);box-shadow:0 0 0 2px var(--caw-accent-soft)}.key{display:flex;gap:6px}.key input{flex:1}.key-state,.key-action{display:grid;place-items:center;flex:none;padding:0 9px;border:1px solid var(--caw-line);border-radius:7px;background:var(--caw-panel);color:var(--caw-muted);font-size:8px}.key-action:hover{border-color:var(--caw-accent);color:var(--caw-text)}details{margin-top:11px;border-top:1px solid var(--caw-line)}summary{padding-top:10px;color:var(--caw-muted);font-size:9px;cursor:pointer}.advanced{padding-top:10px}.foot{border-top:1px solid var(--caw-line)}.feedback{min-width:0;color:var(--caw-muted);font-size:9px;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}.feedback.good{color:var(--caw-good)}.feedback.bad{color:var(--caw-bad)}.actions{display:flex;gap:6px}@media(max-width:520px){.fields{grid-template-columns:1fr}label.wide{grid-column:auto}.presets{display:grid;grid-template-columns:1fr 1fr}}
|
|
156
|
+
</style><section class="editor"><header class="head"><div><strong>${exists ? "编辑模型" : "新增模型"}</strong><span>测试只验证当前内容,保存后配置才会生效</span></div><button class="button" data-action="cancel">返回</button></header><div class="body"><div class="presets">${PRESETS.map((item) => `<button type="button" class="preset ${item.id === this._presetId ? "active" : ""}" data-preset="${item.id}"><i class="mark" style="background:${item.color}">${escapeHtml(item.mark)}</i>${escapeHtml(item.name)}</button>`).join("")}</div><form><div class="fields"><label>配置名称<input name="name" value="${escapeHtml(profile.name || "")}" placeholder="例如 MiniMax 主配置"></label><label>供应商<input name="provider" value="${escapeHtml(profile.provider || "")}" placeholder="例如 MiniMax"></label><label class="wide">请求地址<input name="baseUrl" value="${escapeHtml(profile.baseUrl || "")}" placeholder="https://api.example.com/anthropic"></label><label class="wide">模型名称<input name="model" value="${escapeHtml(profile.models?.default || "")}" placeholder="例如 MiniMax-M3"></label><label class="wide">API Key<div class="key">${this.credentialField(profile)}</div></label></div><details><summary>高级设置</summary><div class="fields advanced"><label>API 协议<select name="protocol"><option value="anthropic-compatible" ${profile.protocol !== "openai-compatible" ? "selected" : ""}>Anthropic Compatible</option><option value="openai-compatible" ${profile.protocol === "openai-compatible" ? "selected" : ""}>OpenAI Compatible</option></select></label><label>请求超时<input name="timeoutMs" type="number" min="1000" value="${Number(profile.timeoutMs || 300000)}"></label></div></details></form></div><footer class="foot"><span class="feedback ${escapeHtml(this._tone)}">${escapeHtml(this._status)}</span><div class="actions"><button class="button" data-action="test" ${this._busy ? "disabled" : ""}>${this._busy ? "测试中…" : "测试连接"}</button><button class="button primary" data-action="save" ${this._busy ? "disabled" : ""}>保存</button></div></footer></section>`;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
payload() {
|
|
160
|
+
this.capture();
|
|
161
|
+
const profile = this._profile;
|
|
162
|
+
if (!profile.name) throw new Error("请填写配置名称");
|
|
163
|
+
if (!profile.provider) throw new Error("请填写供应商");
|
|
164
|
+
if (!profile.baseUrl) throw new Error("请填写请求地址");
|
|
165
|
+
if (!profile.models.default) throw new Error("请填写模型名称");
|
|
166
|
+
if (!profile.credentialConfigured && !this._credential) throw new Error("请输入 API Key");
|
|
167
|
+
return { profile: clone(profile), credential: this._editingCredential ? this._credential : "" };
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
emit(name) {
|
|
171
|
+
try {
|
|
172
|
+
this.dispatchEvent(new CustomEvent(name, { bubbles: true, composed: true, detail: this.payload() }));
|
|
173
|
+
} catch (error) {
|
|
174
|
+
this.setStatus(error.message, "bad");
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
handleClick(event) {
|
|
179
|
+
const button = eventButton(event);
|
|
180
|
+
if (!button) return;
|
|
181
|
+
if (button.dataset.action === "cancel") this.dispatchEvent(new CustomEvent("profile-cancel", { bubbles: true, composed: true }));
|
|
182
|
+
if (button.dataset.action === "save") this.emit("profile-save");
|
|
183
|
+
if (button.dataset.action === "test") this.emit("profile-test");
|
|
184
|
+
if (button.dataset.action === "replace-key") {
|
|
185
|
+
this.capture();
|
|
186
|
+
this._editingCredential = true;
|
|
187
|
+
this._credential = "";
|
|
188
|
+
this._status = "";
|
|
189
|
+
this.render();
|
|
190
|
+
this.shadowRoot.querySelector('[name="apiKey"]')?.focus();
|
|
191
|
+
}
|
|
192
|
+
if (button.dataset.action === "cancel-key") {
|
|
193
|
+
this.capture();
|
|
194
|
+
this._editingCredential = false;
|
|
195
|
+
this._credential = "";
|
|
196
|
+
this.render();
|
|
197
|
+
}
|
|
198
|
+
if (button.dataset.preset) {
|
|
199
|
+
this.capture();
|
|
200
|
+
const preset = PRESETS.find((item) => item.id === button.dataset.preset) || PRESETS[4];
|
|
201
|
+
this._presetId = preset.id;
|
|
202
|
+
this._profile.provider = preset.id === "custom" ? "" : preset.name;
|
|
203
|
+
this._profile.name = preset.id === "custom" ? "" : `${preset.name} 配置`;
|
|
204
|
+
this._profile.baseUrl = preset.baseUrl;
|
|
205
|
+
this._profile.models = { default: preset.model, fast: preset.model, reasoning: preset.model, coding: preset.model, largeContext: preset.model };
|
|
206
|
+
this.render();
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
class AiProfileSettings extends HTMLElement {
|
|
212
|
+
constructor() {
|
|
213
|
+
super();
|
|
214
|
+
this.attachShadow({ mode: "open" });
|
|
215
|
+
this._profiles = [];
|
|
216
|
+
this._view = "list";
|
|
217
|
+
this._draft = null;
|
|
218
|
+
this._status = "";
|
|
219
|
+
this._tone = "";
|
|
220
|
+
this._busy = false;
|
|
221
|
+
this.shadowRoot.addEventListener("click", (event) => {
|
|
222
|
+
if (event.target.closest("[data-add]")) this.openNew();
|
|
223
|
+
});
|
|
224
|
+
this.addEventListener("profile-edit", (event) => {
|
|
225
|
+
event.stopPropagation();
|
|
226
|
+
this.openEditor(event.detail.profile);
|
|
227
|
+
});
|
|
228
|
+
this.addEventListener("profile-cancel", (event) => {
|
|
229
|
+
event.stopPropagation();
|
|
230
|
+
this.closeEditor();
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
connectedCallback() { this.render(); }
|
|
235
|
+
set profiles(value) { this._profiles = Array.isArray(value) ? clone(value) : []; this.render(); }
|
|
236
|
+
get profiles() { return clone(this._profiles); }
|
|
237
|
+
setStatus(message, tone = "") {
|
|
238
|
+
this._status = String(message || "");
|
|
239
|
+
this._tone = tone;
|
|
240
|
+
const feedback = this.shadowRoot?.querySelector(".feedback");
|
|
241
|
+
if (feedback) { feedback.textContent = this._status; feedback.className = `feedback ${tone}`; }
|
|
242
|
+
this.shadowRoot?.querySelector("ai-profile-editor")?.setStatus(this._status, tone);
|
|
243
|
+
}
|
|
244
|
+
setBusy(value, message) {
|
|
245
|
+
this._busy = Boolean(value);
|
|
246
|
+
if (message) this._status = message;
|
|
247
|
+
const list = this.shadowRoot?.querySelector("ai-profile-list");
|
|
248
|
+
if (list) list.busy = this._busy;
|
|
249
|
+
this.shadowRoot?.querySelector("ai-profile-editor")?.setBusy(this._busy, message);
|
|
250
|
+
}
|
|
251
|
+
openNew() { this._draft = newProfile(); this._view = "editor"; this._status = ""; this.render(); }
|
|
252
|
+
closeEditor() { this._view = "list"; this._draft = null; this.render(); }
|
|
253
|
+
openEditor(profile) { this._draft = clone(profile); this._view = "editor"; this._status = ""; this.render(); }
|
|
254
|
+
|
|
255
|
+
render() {
|
|
256
|
+
if (!this.isConnected) return;
|
|
257
|
+
this.shadowRoot.innerHTML = `<style>${common}.module{width:100%;min-width:0}.bar{display:flex;align-items:center;justify-content:space-between;gap:12px;margin-bottom:9px}.bar strong{font-size:13px}.feedback{min-width:0;color:var(--caw-muted);font-size:9px;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}.feedback.good{color:var(--caw-good)}.feedback.bad{color:var(--caw-bad)}.bar-actions{display:flex;align-items:center;gap:9px}</style><section class="module">${this._view === "list" ? `<div class="bar"><strong>AI 模型</strong><div class="bar-actions"><span class="feedback ${escapeHtml(this._tone)}">${escapeHtml(this._status)}</span><button class="button primary" data-add>新增</button></div></div><ai-profile-list></ai-profile-list>` : "<ai-profile-editor></ai-profile-editor>"}</section>`;
|
|
258
|
+
this.bind();
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
bind() {
|
|
262
|
+
const list = this.shadowRoot.querySelector("ai-profile-list");
|
|
263
|
+
if (list) {
|
|
264
|
+
list.profiles = this._profiles;
|
|
265
|
+
list.busy = this._busy;
|
|
266
|
+
list.addEventListener("profile-edit", (event) => this.openEditor(event.detail.profile));
|
|
267
|
+
}
|
|
268
|
+
const editor = this.shadowRoot.querySelector("ai-profile-editor");
|
|
269
|
+
if (editor) {
|
|
270
|
+
editor.profile = this._draft;
|
|
271
|
+
editor.setStatus(this._status, this._tone);
|
|
272
|
+
editor.setBusy(this._busy);
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
if (!global.customElements.get("ai-profile-list")) global.customElements.define("ai-profile-list", AiProfileList);
|
|
278
|
+
if (!global.customElements.get("ai-profile-editor")) global.customElements.define("ai-profile-editor", AiProfileEditor);
|
|
279
|
+
if (!global.customElements.get("ai-profile-settings")) global.customElements.define("ai-profile-settings", AiProfileSettings);
|
|
280
|
+
global.AiProfileList = AiProfileList;
|
|
281
|
+
global.AiProfileEditor = AiProfileEditor;
|
|
282
|
+
global.AiProfileSettings = AiProfileSettings;
|
|
283
|
+
})(typeof window !== "undefined" ? window : globalThis);
|