@sixzjd/dsh-workbuddy-connect 0.0.0-stage → 0.3.1
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 +21 -0
- package/README.en.md +162 -0
- package/README.md +202 -2
- package/assets/icon.svg +12 -0
- package/client.js +595 -0
- package/cordis.patch.yml +13 -0
- package/lib/adapter.js +340 -0
- package/lib/atrest.js +382 -0
- package/lib/auth.js +540 -0
- package/lib/catalog.js +226 -0
- package/lib/identity.js +309 -0
- package/lib/index.js +819 -0
- package/lib/paths.js +156 -0
- package/lib/shim.js +531 -0
- package/lib/upstream.js +1918 -0
- package/lib/variants.js +91 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +59 -4
package/client.js
ADDED
|
@@ -0,0 +1,595 @@
|
|
|
1
|
+
window.__ModuleLoader__.load({
|
|
2
|
+
id: "@sixzjd/dsh-workbuddy-connect",
|
|
3
|
+
factory: (require) => {
|
|
4
|
+
const React = require("react");
|
|
5
|
+
const h = React.createElement;
|
|
6
|
+
// The plugin page's own primitives, so a contributed action is the same
|
|
7
|
+
// component, size and icon as the page's built-in Update button rather than
|
|
8
|
+
// a lookalike that drifts when the page's design changes.
|
|
9
|
+
const {
|
|
10
|
+
Button,
|
|
11
|
+
IconRefreshOutlineRegular,
|
|
12
|
+
} = require("@deepseek-ai/dsh-client-ui-primitives");
|
|
13
|
+
window.__WB_ERR__ = window.__WB_ERR__ || [];
|
|
14
|
+
const note = (where, error) => {
|
|
15
|
+
try {
|
|
16
|
+
window.__WB_ERR__.push(where + ": " + String((error && error.stack) || error));
|
|
17
|
+
} catch {}
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
/** Host routes (literal here and in the host half: the two bundles build independently). */
|
|
21
|
+
const BASE = "/plugins/dsh-workbuddy-connect";
|
|
22
|
+
const STATUS_PATH = `${BASE}/status`;
|
|
23
|
+
const LOGIN_PATH = `${BASE}/login`;
|
|
24
|
+
const MODELS_PATH = `${BASE}/models`;
|
|
25
|
+
const AUTH_PATH = `${BASE}/auth`;
|
|
26
|
+
const UPDATE_PATH = `${BASE}/update`;
|
|
27
|
+
|
|
28
|
+
const zh = {
|
|
29
|
+
title: "WorkBuddy",
|
|
30
|
+
subtitle: "接入 WorkBuddy 模型(国际版 / 国内版)。",
|
|
31
|
+
signIn: "登录 WorkBuddy",
|
|
32
|
+
importDesktop: "从桌面 App 导入",
|
|
33
|
+
signInHint: "点击后在新标签页打开 WorkBuddy 授权页,完成登录后回到这里。",
|
|
34
|
+
waiting: "等待授权中…",
|
|
35
|
+
openLogin: "打开授权页面",
|
|
36
|
+
signedIn: "已登录",
|
|
37
|
+
signedOut: "未登录",
|
|
38
|
+
logout: "退出登录",
|
|
39
|
+
nickname: "账号",
|
|
40
|
+
expires: "凭据有效期",
|
|
41
|
+
domain: "域",
|
|
42
|
+
catalog: "模型目录",
|
|
43
|
+
catalogLive: "实时列表",
|
|
44
|
+
catalogSaved: "上次保存的列表",
|
|
45
|
+
catalogFallback: "内置列表",
|
|
46
|
+
catalogUpdated: "更新于",
|
|
47
|
+
catalogError: "最近刷新失败:",
|
|
48
|
+
refresh: "刷新目录",
|
|
49
|
+
refreshing: "刷新中…",
|
|
50
|
+
models: "模型开关",
|
|
51
|
+
modelsHint: "关闭的模型从 DSH 模型选择器中隐藏。",
|
|
52
|
+
search: "搜索模型 / ID",
|
|
53
|
+
enableAll: "全部开启",
|
|
54
|
+
disableAll: "全部关闭",
|
|
55
|
+
freeTag: "免费",
|
|
56
|
+
rateTag: "倍率",
|
|
57
|
+
ctxTag: "上下文",
|
|
58
|
+
imgTag: "图片",
|
|
59
|
+
effortTag: "推理",
|
|
60
|
+
maxWindow: "使用上游声明的最大上下文窗口",
|
|
61
|
+
maxWindowHint: "关闭时按上游的默认窗口发送请求(如 300K),开启时按最大窗口(如 1M)。",
|
|
62
|
+
credits: "额度",
|
|
63
|
+
creditsNone: "暂无额度信息",
|
|
64
|
+
creditsRemain: "剩余",
|
|
65
|
+
creditsTotal: "总量",
|
|
66
|
+
creditsUnlimited: "不限",
|
|
67
|
+
creditsEnd: "到期",
|
|
68
|
+
creditsError: "额度读取失败:",
|
|
69
|
+
identity: "客户端身份",
|
|
70
|
+
errorLoad: "无法读取连接器状态",
|
|
71
|
+
retry: "重试",
|
|
72
|
+
failed: "操作失败",
|
|
73
|
+
signedOutHint: "登录后才会出现模型分组。",
|
|
74
|
+
sessionStale: "上游拒绝了这份凭据:桌面 App 换过会话了。点「从桌面 App 导入」重新接入,或重新登录。",
|
|
75
|
+
updateTitle: "插件更新",
|
|
76
|
+
updateHint: "git 版本可以在这里一键检查并更新到上游最新提交;DSH 自己只给官方插件显示「更新」。",
|
|
77
|
+
accountRestricted: "上游已限制这个账号(不是凭据问题,重新登录或重新导入都无效)。只有申诉才能解除:在 WorkBuddy 里提交反馈。",
|
|
78
|
+
tabGlobal: "国际版",
|
|
79
|
+
tabCN: "国内版",
|
|
80
|
+
};
|
|
81
|
+
const en = {
|
|
82
|
+
title: "WorkBuddy",
|
|
83
|
+
subtitle: "WorkBuddy models (Global / CN) in DSH.",
|
|
84
|
+
signIn: "Sign in to WorkBuddy",
|
|
85
|
+
importDesktop: "Import from the desktop app",
|
|
86
|
+
signInHint: "Opens the WorkBuddy authorization page in a new tab; come back here when it is done.",
|
|
87
|
+
waiting: "waiting for authorization…",
|
|
88
|
+
openLogin: "Open the authorization page",
|
|
89
|
+
signedIn: "Signed in",
|
|
90
|
+
signedOut: "Signed out",
|
|
91
|
+
logout: "Sign out",
|
|
92
|
+
nickname: "Account",
|
|
93
|
+
expires: "Credential expires",
|
|
94
|
+
domain: "Domain",
|
|
95
|
+
catalog: "Model catalog",
|
|
96
|
+
catalogLive: "live list",
|
|
97
|
+
catalogSaved: "saved list",
|
|
98
|
+
catalogFallback: "built-in list",
|
|
99
|
+
catalogUpdated: "updated",
|
|
100
|
+
catalogError: "last refresh failed: ",
|
|
101
|
+
refresh: "Refresh catalog",
|
|
102
|
+
refreshing: "Refreshing…",
|
|
103
|
+
models: "Model toggles",
|
|
104
|
+
modelsHint: "Disabled models are hidden from DSH's model picker.",
|
|
105
|
+
search: "search models / id",
|
|
106
|
+
enableAll: "Enable all",
|
|
107
|
+
disableAll: "Disable all",
|
|
108
|
+
freeTag: "free",
|
|
109
|
+
rateTag: "rate",
|
|
110
|
+
ctxTag: "ctx",
|
|
111
|
+
imgTag: "image",
|
|
112
|
+
effortTag: "reasoning",
|
|
113
|
+
maxWindow: "Use the upstream's maximum context window",
|
|
114
|
+
maxWindowHint: "Off sends the upstream's default window (e.g. 300K); on sends the largest one (e.g. 1M).",
|
|
115
|
+
credits: "Quota",
|
|
116
|
+
creditsNone: "No quota information yet",
|
|
117
|
+
creditsRemain: "remaining",
|
|
118
|
+
creditsTotal: "total",
|
|
119
|
+
creditsUnlimited: "unlimited",
|
|
120
|
+
creditsEnd: "expires",
|
|
121
|
+
creditsError: "Could not read the quota: ",
|
|
122
|
+
identity: "client identity",
|
|
123
|
+
errorLoad: "Cannot read connector status",
|
|
124
|
+
retry: "Retry",
|
|
125
|
+
failed: "Action failed",
|
|
126
|
+
signedOutHint: "The model group appears after you sign in.",
|
|
127
|
+
sessionStale: "The upstream refused this credential — the desktop app rotated its session. Press “Import from the desktop app” again, or sign in once more.",
|
|
128
|
+
updateTitle: "Plugin update",
|
|
129
|
+
updateHint: "For a git install this checks upstream and updates to its latest commit in one click; DSH itself only shows Update for official plugins.",
|
|
130
|
+
accountRestricted: "The upstream has restricted this account — this is not a credential problem, so signing in or importing again will not help. Only an appeal lifts it: submit feedback in WorkBuddy.",
|
|
131
|
+
tabGlobal: "Global",
|
|
132
|
+
tabCN: "China",
|
|
133
|
+
};
|
|
134
|
+
const L = typeof navigator !== "undefined" && /^zh/i.test(navigator.language || "") ? zh : en;
|
|
135
|
+
|
|
136
|
+
const css = `
|
|
137
|
+
.wb-card{border:.5px solid var(--dsw-alias-border-l2,#dedee3);border-radius:var(--dsw-radius-panel,12px);background:var(--dsw-alias-bg-layer-1,#fff);overflow:hidden}
|
|
138
|
+
.wb-head{display:flex;align-items:center;gap:10px;width:100%;padding:14px 16px;border:0;background:transparent;color:inherit;font:inherit;text-align:left;cursor:pointer}
|
|
139
|
+
.wb-head:hover{background:var(--dsw-alias-interactive-bg-hover,#f1f1f3)}
|
|
140
|
+
.wb-title{font-size:14px;font-weight:600}
|
|
141
|
+
.wb-sub{color:var(--dsw-alias-label-secondary,#74747c);font-size:12px;margin-top:2px}
|
|
142
|
+
.wb-chev{margin-left:auto;color:var(--dsw-alias-label-tertiary,#9a9aa3);transition:transform .15s}
|
|
143
|
+
.wb-chev.wb-open{transform:rotate(90deg)}
|
|
144
|
+
.wb-tabs{display:flex;gap:4px;padding:3px;border-radius:var(--dsw-radius-md,8px);background:var(--dsw-alias-bg-layer-2,#f1f1f3);width:fit-content}
|
|
145
|
+
.wb-tab{display:flex;align-items:center;gap:6px;height:28px;padding:0 12px;border:0;border-radius:6px;background:transparent;color:var(--dsw-alias-label-secondary,#74747c);font:inherit;font-size:13px;cursor:pointer}
|
|
146
|
+
.wb-tab:hover{color:var(--dsw-alias-label-primary,#25252a)}
|
|
147
|
+
.wb-tab[aria-selected="true"]{background:var(--dsw-alias-bg-layer-1,#fff);color:var(--dsw-alias-label-primary,#25252a);box-shadow:0 .5px 2px rgba(0,0,0,.08)}
|
|
148
|
+
.wb-dot{width:7px;height:7px;border-radius:99px;background:var(--dsw-alias-label-tertiary,#9a9aa3);flex:none}
|
|
149
|
+
.wb-dot.wb-dot-on{background:var(--dsw-alias-state-success-primary,#2f9e44)}
|
|
150
|
+
.wb-dot.wb-dot-load{background:var(--dsw-alias-state-warning-primary,#d98416)}
|
|
151
|
+
.wb-body{padding:2px 16px 16px;display:flex;flex-direction:column;gap:14px}
|
|
152
|
+
.wb-row{display:flex;align-items:center;gap:8px;flex-wrap:wrap}
|
|
153
|
+
.wb-label{font-size:12px;font-weight:600;color:var(--dsw-alias-label-secondary,#74747c)}
|
|
154
|
+
.wb-badge{font-size:11px;padding:2px 8px;border-radius:99px;border:.5px solid var(--dsw-alias-border-l2,#dedee3);color:var(--dsw-alias-label-secondary,#74747c);white-space:nowrap}
|
|
155
|
+
.wb-badge.wb-good{color:var(--dsw-alias-state-business-primary,#4176e6);border-color:currentColor}
|
|
156
|
+
.wb-badge.wb-free{color:var(--dsw-alias-state-success-primary,#2f9e44);border-color:currentColor}
|
|
157
|
+
.wb-input,.wb-button{height:30px;padding:0 10px;color:var(--dsw-alias-label-primary,#25252a);background:var(--dsw-alias-bg-layer-1,#fff);border:.5px solid var(--dsw-alias-border-l4,#d4d6db);border-radius:var(--dsw-radius-md,8px);font:inherit;font-size:13px}
|
|
158
|
+
.wb-input{flex:1;min-width:160px}
|
|
159
|
+
.wb-update-action{display:inline-flex;align-items:center;gap:8px;min-width:0}
|
|
160
|
+
.wb-update-note{color:var(--dsw-alias-label-tertiary,#9a9aa3);font-size:12.5px;line-height:18px;white-space:nowrap}
|
|
161
|
+
.wb-update-note-info{color:var(--dsw-alias-state-business-primary,#4176e6)}
|
|
162
|
+
@media (max-width:720px){.wb-update-note{display:none}}
|
|
163
|
+
.wb-button{cursor:pointer;white-space:nowrap}
|
|
164
|
+
.wb-button:hover{background:var(--dsw-alias-interactive-bg-hover,#f1f1f3)}
|
|
165
|
+
.wb-button:disabled{opacity:.5;cursor:default}
|
|
166
|
+
.wb-button.wb-primary{background:var(--dsw-alias-state-business-primary,#4176e6);border-color:transparent;color:#fff}
|
|
167
|
+
.wb-button.wb-primary:hover{filter:brightness(1.05)}
|
|
168
|
+
.wb-note{font-size:11px;color:var(--dsw-alias-label-tertiary,#9a9aa3);line-height:1.5}
|
|
169
|
+
.wb-error{font-size:12px;color:var(--dsw-alias-state-error-primary,#bd4747)}
|
|
170
|
+
.wb-models{display:flex;flex-direction:column;gap:2px;max-height:280px;overflow:auto;border:.5px solid var(--dsw-alias-border-l2,#dedee3);border-radius:var(--dsw-radius-md,8px);padding:6px}
|
|
171
|
+
.wb-model{display:flex;align-items:center;gap:8px;padding:5px 8px;border-radius:6px;font-size:13px;flex-wrap:nowrap;min-width:0}
|
|
172
|
+
.wb-model:hover{background:var(--dsw-alias-interactive-bg-hover,#f1f1f3)}
|
|
173
|
+
.wb-model input{accent-color:var(--dsw-alias-state-business-primary,#4176e6);flex:none}
|
|
174
|
+
.wb-model-name{flex:1 1 auto;min-width:0;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}
|
|
175
|
+
.wb-model-id{flex:0 1 auto;min-width:0;max-width:38%;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;color:var(--dsw-alias-label-tertiary,#9a9aa3);font-size:11px}
|
|
176
|
+
.wb-model .wb-badge{flex:none}
|
|
177
|
+
.wb-quota{display:flex;flex-direction:column;gap:6px}
|
|
178
|
+
.wb-quota-row{display:flex;align-items:center;gap:8px;font-size:12px}
|
|
179
|
+
.wb-bar{flex:1;height:6px;border-radius:99px;background:var(--dsw-alias-border-l2,#dedee3);overflow:hidden}
|
|
180
|
+
.wb-bar span{display:block;height:100%;background:var(--dsw-alias-state-business-primary,#4176e6)}
|
|
181
|
+
.wb-check{display:flex;align-items:center;gap:8px;font-size:12px;color:var(--dsw-alias-label-secondary,#74747c);cursor:pointer}
|
|
182
|
+
.wb-check input{accent-color:var(--dsw-alias-state-business-primary,#4176e6)}
|
|
183
|
+
.wb-link{color:var(--dsw-alias-state-business-primary,#4176e6);text-decoration:none;cursor:pointer}
|
|
184
|
+
@media(prefers-reduced-motion:reduce){.wb-chev{transition:none}}
|
|
185
|
+
`;
|
|
186
|
+
|
|
187
|
+
const timeText = (ms) => {
|
|
188
|
+
if (typeof ms !== "number" || !Number.isFinite(ms) || ms <= 0) return ""
|
|
189
|
+
const d = new Date(ms)
|
|
190
|
+
return `${String(d.getMonth() + 1).padStart(2, "0")}-${String(d.getDate()).padStart(2, "0")} ${String(d.getHours()).padStart(2, "0")}:${String(d.getMinutes()).padStart(2, "0")}`
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/** 200,000 tokens reads as 200K, 1,048,576 as 1M — never 195K or 1024K. */
|
|
194
|
+
const ctxText = (n) => {
|
|
195
|
+
if (!Number.isFinite(n) || n <= 0) return ""
|
|
196
|
+
if (n >= 1e6) return `${Math.round(n / 1e5) / 10}M`
|
|
197
|
+
return `${Math.round(n / 1e3)}K`
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
const isStatus = (value) => typeof value === "object" && value !== null && !Array.isArray(value) && value.status === "ok"
|
|
201
|
+
|
|
202
|
+
/** One row of the model list: the toggle, the name, the id, and its badges. */
|
|
203
|
+
function modelRow(m, rowOptions) {
|
|
204
|
+
const { busy, setModelEnabled } = rowOptions
|
|
205
|
+
return h("label", { key: m.id, className: "wb-model" },
|
|
206
|
+
h("input", {
|
|
207
|
+
type: "checkbox",
|
|
208
|
+
checked: m.enabled !== false,
|
|
209
|
+
disabled: busy,
|
|
210
|
+
onChange: (e) => {
|
|
211
|
+
// The toggle names ONE model and its new state; it never sends a
|
|
212
|
+
// list derived from a snapshot of the page. A snapshot is stale
|
|
213
|
+
// the moment the catalog refreshes, and a complement computed from
|
|
214
|
+
// a stale one can disable every model but the one clicked.
|
|
215
|
+
void setModelEnabled(m.id, e.target.checked)
|
|
216
|
+
},
|
|
217
|
+
}),
|
|
218
|
+
h("span", { className: "wb-model-name", title: m.name }, m.name),
|
|
219
|
+
h("span", { className: "wb-model-id", title: m.id }, m.id),
|
|
220
|
+
m.free ? h("span", { className: "wb-badge wb-free" }, L.freeTag) : null,
|
|
221
|
+
m.rate ? h("span", { className: "wb-badge" }, `${L.rateTag} ${m.rate}`) : null,
|
|
222
|
+
m.rateUnknown ? h("span", { className: "wb-badge" }, "—") : null,
|
|
223
|
+
m.contextWindow ? h("span", { className: "wb-badge" }, `${L.ctxTag} ${ctxText(m.contextWindow)}`) : null)
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* One variant's section: sign-in, catalog, model toggles, quota.
|
|
228
|
+
*
|
|
229
|
+
* The status document carries both variants and belongs to the card
|
|
230
|
+
* shell, so switching tabs never refetches and never loses the other
|
|
231
|
+
* variant's data.
|
|
232
|
+
*/
|
|
233
|
+
function VariantSection(props) {
|
|
234
|
+
const { provider, status, load, loading } = props
|
|
235
|
+
const [busy, setBusy] = React.useState(false)
|
|
236
|
+
const [notice, setNotice] = React.useState(null)
|
|
237
|
+
const [query, setQuery] = React.useState("")
|
|
238
|
+
const [login, setLogin] = React.useState(null)
|
|
239
|
+
const mounted = React.useRef(true)
|
|
240
|
+
React.useEffect(() => {
|
|
241
|
+
mounted.current = true
|
|
242
|
+
return () => { mounted.current = false }
|
|
243
|
+
}, [])
|
|
244
|
+
|
|
245
|
+
const variant = status?.variants?.[provider]
|
|
246
|
+
|
|
247
|
+
const post = React.useCallback(async (path, body, key) => {
|
|
248
|
+
setBusy(true)
|
|
249
|
+
setNotice(null)
|
|
250
|
+
try {
|
|
251
|
+
const res = await fetch(path, {
|
|
252
|
+
method: "POST",
|
|
253
|
+
headers: { "content-type": "application/json", "x-dsh-workbuddy-connect-key": key ?? "" },
|
|
254
|
+
body: JSON.stringify(body),
|
|
255
|
+
})
|
|
256
|
+
if (!res.ok) throw new Error(String(res.status))
|
|
257
|
+
await load()
|
|
258
|
+
return true
|
|
259
|
+
} catch {
|
|
260
|
+
setNotice(L.failed)
|
|
261
|
+
return false
|
|
262
|
+
} finally {
|
|
263
|
+
setBusy(false)
|
|
264
|
+
}
|
|
265
|
+
}, [load])
|
|
266
|
+
|
|
267
|
+
/** Begin the device-authorization flow and start polling for its outcome. */
|
|
268
|
+
const beginLogin = React.useCallback(async () => {
|
|
269
|
+
setBusy(true)
|
|
270
|
+
setNotice(null)
|
|
271
|
+
try {
|
|
272
|
+
const res = await fetch(LOGIN_PATH, {
|
|
273
|
+
method: "POST",
|
|
274
|
+
headers: { "content-type": "application/json", "x-dsh-workbuddy-connect-key": status?.loginKey ?? "" },
|
|
275
|
+
body: JSON.stringify({ action: "begin", provider }),
|
|
276
|
+
})
|
|
277
|
+
if (!res.ok) throw new Error(String(res.status))
|
|
278
|
+
const doc = await res.json()
|
|
279
|
+
if (!mounted.current) return
|
|
280
|
+
if (doc?.status === "pending" && doc.authUrl) setLogin({ url: doc.authUrl, state: doc.state })
|
|
281
|
+
else setNotice(doc?.error ?? L.failed)
|
|
282
|
+
} catch {
|
|
283
|
+
if (mounted.current) setNotice(L.failed)
|
|
284
|
+
} finally {
|
|
285
|
+
if (mounted.current) setBusy(false)
|
|
286
|
+
}
|
|
287
|
+
}, [provider, status])
|
|
288
|
+
|
|
289
|
+
/** Adopt the session the WorkBuddy desktop app is already signed in to. */
|
|
290
|
+
const importDesktop = React.useCallback(async () => {
|
|
291
|
+
setBusy(true)
|
|
292
|
+
setNotice(null)
|
|
293
|
+
try {
|
|
294
|
+
const res = await fetch(AUTH_PATH, {
|
|
295
|
+
method: "POST",
|
|
296
|
+
headers: { "content-type": "application/json", "x-dsh-workbuddy-connect-key": status?.authKey ?? "" },
|
|
297
|
+
body: JSON.stringify({ action: "import-desktop", provider }),
|
|
298
|
+
})
|
|
299
|
+
if (!res.ok) throw new Error(String(res.status))
|
|
300
|
+
const doc = await res.json()
|
|
301
|
+
if (!mounted.current) return
|
|
302
|
+
if (doc?.ok === true) await load()
|
|
303
|
+
else setNotice(doc?.error ?? L.failed)
|
|
304
|
+
} catch {
|
|
305
|
+
if (mounted.current) setNotice(L.failed)
|
|
306
|
+
} finally {
|
|
307
|
+
if (mounted.current) setBusy(false)
|
|
308
|
+
}
|
|
309
|
+
}, [provider, status, load])
|
|
310
|
+
|
|
311
|
+
/** Poll the pending attempt until the human finishes in the browser. */
|
|
312
|
+
React.useEffect(() => {
|
|
313
|
+
if (!login) return undefined
|
|
314
|
+
let cancelled = false
|
|
315
|
+
const timer = setInterval(async () => {
|
|
316
|
+
try {
|
|
317
|
+
const res = await fetch(LOGIN_PATH, {
|
|
318
|
+
method: "POST",
|
|
319
|
+
headers: { "content-type": "application/json", "x-dsh-workbuddy-connect-key": status?.loginKey ?? "" },
|
|
320
|
+
body: JSON.stringify({ action: "poll", provider, state: login.state }),
|
|
321
|
+
})
|
|
322
|
+
if (!res.ok) throw new Error(String(res.status))
|
|
323
|
+
const doc = await res.json()
|
|
324
|
+
if (cancelled || !mounted.current) return
|
|
325
|
+
if (doc?.status === "complete") {
|
|
326
|
+
setLogin(null)
|
|
327
|
+
await load()
|
|
328
|
+
} else if (doc?.status === "error") {
|
|
329
|
+
setLogin(null)
|
|
330
|
+
setNotice(doc.error ?? L.failed)
|
|
331
|
+
}
|
|
332
|
+
} catch {
|
|
333
|
+
// A transient poll failure is not a failed login: keep polling.
|
|
334
|
+
}
|
|
335
|
+
}, 2500)
|
|
336
|
+
return () => { cancelled = true; clearInterval(timer) }
|
|
337
|
+
}, [login, provider, status, load])
|
|
338
|
+
|
|
339
|
+
const auth = variant?.auth
|
|
340
|
+
const catalog = variant?.catalog
|
|
341
|
+
const models = Array.isArray(catalog?.models) ? catalog.models : []
|
|
342
|
+
const shown = query.trim() === "" ? models : models.filter((m) => `${m.name} ${m.id}`.toLowerCase().includes(query.trim().toLowerCase()))
|
|
343
|
+
// Both mutations name what they change, never a list derived from the
|
|
344
|
+
// page: the server owns the catalog and computes the rest.
|
|
345
|
+
const setModelEnabled = (model, enabled) => post(MODELS_PATH, { action: "set-model", provider, model, enabled }, status?.modelsKey)
|
|
346
|
+
const setAllEnabled = (enabled) => post(MODELS_PATH, { action: "set-all", provider, enabled }, status?.modelsKey)
|
|
347
|
+
const credits = variant?.credits
|
|
348
|
+
const creditAccounts = Array.isArray(credits?.accounts) ? credits.accounts : []
|
|
349
|
+
const creditTotal = credits?.totalSize ?? creditAccounts.reduce((sum, a) => sum + (Number(a.size) || 0), 0)
|
|
350
|
+
|
|
351
|
+
return h(React.Fragment, null,
|
|
352
|
+
h("div", null,
|
|
353
|
+
h("div", { className: "wb-row" },
|
|
354
|
+
h("span", { className: "wb-label" }, auth?.state === "signed-in" ? L.signedIn : L.signedOut),
|
|
355
|
+
h("span", { className: `wb-badge${auth?.state === "signed-in" ? " wb-good" : ""}` },
|
|
356
|
+
auth?.state === "signed-in" ? (auth.nickname ?? variant?.appName) : L.signedOut),
|
|
357
|
+
auth?.state === "signed-in"
|
|
358
|
+
? h(React.Fragment, null,
|
|
359
|
+
h("button", { type: "button", className: "wb-button", disabled: busy, onClick: () => void post(AUTH_PATH, { action: "logout", provider }, status.authKey) }, L.logout),
|
|
360
|
+
// The desktop app re-signs in behind the plugin's back and
|
|
361
|
+
// rotates its own session, so adopting that session again has
|
|
362
|
+
// to stay reachable while signed in — otherwise a rotated
|
|
363
|
+
// credential can only be replaced by signing out first.
|
|
364
|
+
variant?.desktopAvailable === true
|
|
365
|
+
? h("button", { type: "button", className: "wb-button", disabled: busy, onClick: () => void importDesktop() }, L.importDesktop)
|
|
366
|
+
: null)
|
|
367
|
+
: h(React.Fragment, null,
|
|
368
|
+
h("button", { type: "button", className: "wb-button wb-primary", disabled: busy, onClick: () => void beginLogin() }, L.signIn),
|
|
369
|
+
variant?.desktopAvailable === true
|
|
370
|
+
? h("button", { type: "button", className: "wb-button", disabled: busy, onClick: () => void importDesktop() }, L.importDesktop)
|
|
371
|
+
: null)),
|
|
372
|
+
auth?.state === "signed-in" ? h("div", { className: "wb-note", style: { marginTop: 4 } },
|
|
373
|
+
[auth.domain ? `${L.domain}: ${auth.domain}` : null, auth.expiresAt ? ` · ${L.expires}: ${timeText(auth.expiresAt)}` : null].filter(Boolean).join("")) : null,
|
|
374
|
+
auth?.accountRestricted ? h("div", { className: "wb-error", style: { marginTop: 4 } },
|
|
375
|
+
L.accountRestricted,
|
|
376
|
+
h("div", { className: "wb-note", style: { marginTop: 2 } }, String(auth.accountRestricted))) : null,
|
|
377
|
+
auth?.sessionStale === true && !auth?.accountRestricted ? h("div", { className: "wb-error", style: { marginTop: 4 } }, L.sessionStale) : null,
|
|
378
|
+
auth?.state !== "signed-in" ? h("div", { className: "wb-note", style: { marginTop: 4 } }, L.signedOutHint) : null,
|
|
379
|
+
auth?.reason ? h("div", { className: "wb-error", style: { marginTop: 4 } }, auth.reason) : null,
|
|
380
|
+
login ? h("div", { className: "wb-row", style: { marginTop: 8 } },
|
|
381
|
+
h("span", { className: "wb-note" }, L.waiting),
|
|
382
|
+
h("a", { className: "wb-link", href: login.url, target: "_blank", rel: "noreferrer" }, L.openLogin)) : null),
|
|
383
|
+
variant?.auth?.state === "signed-in" ? h("div", null,
|
|
384
|
+
h("div", { className: "wb-row" },
|
|
385
|
+
h("span", { className: "wb-label" }, L.catalog),
|
|
386
|
+
h("span", { className: `wb-badge${catalog.source === "live" ? " wb-good" : ""}` },
|
|
387
|
+
catalog.source === "live" ? L.catalogLive : catalog.source === "saved" ? L.catalogSaved : L.catalogFallback,
|
|
388
|
+
catalog.fetchedAt ? ` · ${L.catalogUpdated} ${timeText(catalog.fetchedAt)}` : ""),
|
|
389
|
+
h("button", { type: "button", className: "wb-button", disabled: busy || loading, onClick: () => void post(MODELS_PATH, { action: "refresh", provider }, status.modelsKey) }, loading ? L.refreshing : L.refresh)),
|
|
390
|
+
catalog.error ? h("div", { className: "wb-error", style: { marginTop: 4 } }, L.catalogError + catalog.error) : null,
|
|
391
|
+
h("div", { className: "wb-row", style: { marginTop: 6 } },
|
|
392
|
+
h("label", { className: "wb-check" },
|
|
393
|
+
h("input", {
|
|
394
|
+
type: "checkbox",
|
|
395
|
+
checked: variant?.useMaximumContextWindow === true,
|
|
396
|
+
disabled: busy,
|
|
397
|
+
onChange: (e) => void post(MODELS_PATH, { action: "set-max-window", provider, enabled: e.target.checked }, status.modelsKey),
|
|
398
|
+
}),
|
|
399
|
+
h("span", null, L.maxWindow)),
|
|
400
|
+
h("span", { className: "wb-note" }, L.maxWindowHint)),
|
|
401
|
+
h("div", { className: "wb-row", style: { marginTop: 10 } },
|
|
402
|
+
h("span", { className: "wb-label" }, L.models),
|
|
403
|
+
h("input", { className: "wb-input", style: { maxWidth: 180, marginLeft: "auto" }, placeholder: L.search, value: query, onChange: (e) => setQuery(e.target.value) })),
|
|
404
|
+
h("div", { className: "wb-note", style: { margin: "4px 0 6px" } }, L.modelsHint),
|
|
405
|
+
h("div", { className: "wb-models" },
|
|
406
|
+
shown.length === 0
|
|
407
|
+
? h("div", { className: "wb-note" }, "—")
|
|
408
|
+
: shown.map((m) => modelRow(m, { busy, setModelEnabled }))),
|
|
409
|
+
h("div", { className: "wb-row", style: { marginTop: 6 } },
|
|
410
|
+
h("button", { type: "button", className: "wb-button", disabled: busy, onClick: () => void setAllEnabled(true) }, L.enableAll),
|
|
411
|
+
h("button", { type: "button", className: "wb-button", disabled: busy, onClick: () => void setAllEnabled(false) }, L.disableAll))) : null,
|
|
412
|
+
variant?.auth?.state === "signed-in" ? h("div", null,
|
|
413
|
+
h("div", { className: "wb-row" },
|
|
414
|
+
h("span", { className: "wb-label" }, L.credits),
|
|
415
|
+
variant?.creditsError ? h("span", { className: "wb-badge" }, L.creditsError + variant.creditsError) : null),
|
|
416
|
+
credits && creditAccounts.length > 0 ? h("div", { className: "wb-quota", style: { marginTop: 6 } },
|
|
417
|
+
creditAccounts.map((a, i) => {
|
|
418
|
+
const size = Number(a.size) || 0
|
|
419
|
+
const remain = Number(a.remain) || 0
|
|
420
|
+
const pct = size > 0 ? Math.max(0, Math.min(100, Math.round((remain / size) * 100))) : 0
|
|
421
|
+
return h("div", { key: `${a.packageName}-${i}`, className: "wb-quota-row" },
|
|
422
|
+
h("span", { style: { minWidth: 96, overflow: "hidden", textOverflow: "ellipsis", whiteSpace: "nowrap" }, title: a.packageName }, a.packageName),
|
|
423
|
+
h("span", { className: "wb-bar" }, h("span", { style: { width: `${pct}%` } })),
|
|
424
|
+
h("span", { style: { whiteSpace: "nowrap" } }, `${remain} / ${size}`),
|
|
425
|
+
a.packageEndTime ? h("span", { className: "wb-note" }, `${L.creditsEnd} ${a.packageEndTime}`) : null)
|
|
426
|
+
}),
|
|
427
|
+
creditTotal > 0 ? h("div", { className: "wb-note" }, `${L.creditsTotal}: ${creditTotal}`) : null)
|
|
428
|
+
: credits ? h("div", { className: "wb-note" }, L.creditsNone) : null) : null,
|
|
429
|
+
notice ? h("div", { className: "wb-error" }, notice) : null)
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
/**
|
|
433
|
+
* The one card for both WorkBuddy editions.
|
|
434
|
+
*
|
|
435
|
+
* One panel with a variant tab, the way the Qoder connector does it: the
|
|
436
|
+
* two editions share a card, a header and a status poll, and only the
|
|
437
|
+
* section under the active tab differs.
|
|
438
|
+
*/
|
|
439
|
+
function WorkBuddyCard() {
|
|
440
|
+
const [open, setOpen] = React.useState(false)
|
|
441
|
+
const [active, setActive] = React.useState("workbuddy-global")
|
|
442
|
+
const [status, setStatus] = React.useState(null)
|
|
443
|
+
const [loadError, setLoadError] = React.useState(null)
|
|
444
|
+
const [loading, setLoading] = React.useState(false)
|
|
445
|
+
const mounted = React.useRef(true)
|
|
446
|
+
React.useEffect(() => {
|
|
447
|
+
mounted.current = true
|
|
448
|
+
return () => { mounted.current = false }
|
|
449
|
+
}, [])
|
|
450
|
+
|
|
451
|
+
const load = React.useCallback(async () => {
|
|
452
|
+
setLoading(true)
|
|
453
|
+
try {
|
|
454
|
+
const res = await fetch(STATUS_PATH, { headers: { accept: "application/json" } })
|
|
455
|
+
if (!res.ok) throw new Error(String(res.status))
|
|
456
|
+
const doc = await res.json()
|
|
457
|
+
if (!mounted.current) return
|
|
458
|
+
if (isStatus(doc)) { setStatus(doc); setLoadError(null) }
|
|
459
|
+
else setLoadError(L.errorLoad)
|
|
460
|
+
} catch {
|
|
461
|
+
if (mounted.current) setLoadError(L.errorLoad)
|
|
462
|
+
} finally {
|
|
463
|
+
if (mounted.current) setLoading(false)
|
|
464
|
+
}
|
|
465
|
+
}, [])
|
|
466
|
+
|
|
467
|
+
// The status is cheap (it reads the cached catalog and the stored
|
|
468
|
+
// credential — never the upstream), and the collapsed header shows the
|
|
469
|
+
// sign-in state, so it is loaded whether the card is open or not.
|
|
470
|
+
React.useEffect(() => {
|
|
471
|
+
void load()
|
|
472
|
+
const timer = setInterval(() => void load(), 60_000)
|
|
473
|
+
return () => clearInterval(timer)
|
|
474
|
+
}, [load])
|
|
475
|
+
|
|
476
|
+
const variant = status?.variants?.[active]
|
|
477
|
+
/** A tab's dot: green when that edition is signed in, amber while loading. */
|
|
478
|
+
const dotClass = v => (v?.auth?.state === "signed-in" ? "wb-dot wb-dot-on" : status === null ? "wb-dot wb-dot-load" : "wb-dot")
|
|
479
|
+
|
|
480
|
+
return h("div", { className: "wb-card" },
|
|
481
|
+
h("style", null, css),
|
|
482
|
+
h("button", { type: "button", className: "wb-head", onClick: () => setOpen((v) => !v), "aria-expanded": open },
|
|
483
|
+
h("span", null,
|
|
484
|
+
h("span", { className: "wb-title" }, L.title),
|
|
485
|
+
h("div", { className: "wb-sub" }, L.subtitle)),
|
|
486
|
+
variant?.auth?.state === "signed-in"
|
|
487
|
+
? h("span", { className: "wb-badge wb-good" }, variant.auth.nickname ?? variant.displayName)
|
|
488
|
+
: status
|
|
489
|
+
? h("span", { className: "wb-badge" }, L.signedOut)
|
|
490
|
+
: null,
|
|
491
|
+
h("span", { className: `wb-chev${open ? " wb-open" : ""}`, "aria-hidden": "true" }, "›")),
|
|
492
|
+
open ? h("div", { className: "wb-body" },
|
|
493
|
+
h("div", { className: "wb-tabs", role: "tablist", "aria-label": L.title },
|
|
494
|
+
h("button", { type: "button", role: "tab", className: "wb-tab", "aria-selected": active === "workbuddy-global", onClick: () => setActive("workbuddy-global") },
|
|
495
|
+
h("span", { className: dotClass(status?.variants?.["workbuddy-global"]), "aria-hidden": "true" }),
|
|
496
|
+
h("span", null, L.tabGlobal)),
|
|
497
|
+
h("button", { type: "button", role: "tab", className: "wb-tab", "aria-selected": active === "workbuddy", onClick: () => setActive("workbuddy") },
|
|
498
|
+
h("span", { className: dotClass(status?.variants?.["workbuddy"]), "aria-hidden": "true" }),
|
|
499
|
+
h("span", null, L.tabCN))),
|
|
500
|
+
loadError && !status ? h("div", { className: "wb-error" }, loadError, " ", h("button", { type: "button", className: "wb-button", onClick: () => void load() }, L.retry)) : null,
|
|
501
|
+
status ? h(VariantSection, { provider: active, status, load, loading }) : h("div", { className: "wb-note" }, loading ? "…" : "—")) : null)
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
/**
|
|
505
|
+
* Join the shared 《插件设置》 block with the same rendezvous protocol the
|
|
506
|
+
* sibling connect plugins use: wait for the host's `settings.section`,
|
|
507
|
+
* attach to the existing container when one is registered, otherwise
|
|
508
|
+
* register it (falling back to attaching when a sibling wins the race).
|
|
509
|
+
*/
|
|
510
|
+
const SHARED_SECTION_SLOT = "settings.section"
|
|
511
|
+
const SHARED_SECTION_ID = "plugin-settings"
|
|
512
|
+
const SHARED_SECTION_ORDER = 900
|
|
513
|
+
const SHARED_ITEM_SLOT = "plugin-settings.item"
|
|
514
|
+
const SHARED_ITEM_ID = "dsh-workbuddy-connect"
|
|
515
|
+
const SHARED_SECTION_LABEL = "插件设置"
|
|
516
|
+
|
|
517
|
+
function PluginSettingsSection(props) {
|
|
518
|
+
return h("ul", {
|
|
519
|
+
style: { display: "flex", flexDirection: "column", gap: 6, padding: 0, listStyle: "none" },
|
|
520
|
+
}, props.renderSlot(SHARED_ITEM_SLOT, {}))
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
/** The package name this card belongs to, as the plugin page reports it. */
|
|
524
|
+
const PACKAGE_NAME = "@sixzjd/dsh-workbuddy-connect";
|
|
525
|
+
/** Names an existing profile may still declare from before the rename. */
|
|
526
|
+
const LEGACY_PACKAGE_NAMES = ["dsh-workbuddy-connect"];
|
|
527
|
+
|
|
528
|
+
const inject = ["slots"]
|
|
529
|
+
|
|
530
|
+
function apply(ctx) {
|
|
531
|
+
try {
|
|
532
|
+
ctx.slots.inject(SHARED_SECTION_SLOT, () => {
|
|
533
|
+
try {
|
|
534
|
+
/**
|
|
535
|
+
* Register the shared block unless a sibling already owns it.
|
|
536
|
+
*
|
|
537
|
+
* The registrant OWNS the child declaration `plugin-settings.item`,
|
|
538
|
+
* and a slot's entries and its declaration die together: disposing
|
|
539
|
+
* the owner collapses the child slot and cascades every sibling's
|
|
540
|
+
* card away with it. That is why disabling whichever connector
|
|
541
|
+
* happened to register the block first used to remove the whole
|
|
542
|
+
* 《插件设置》 entry, taking the still-enabled connectors' cards with
|
|
543
|
+
* it.
|
|
544
|
+
*/
|
|
545
|
+
const registerContainer = () => {
|
|
546
|
+
if (ctx.slots.entries(SHARED_SECTION_SLOT).some((entry) => entry.options?.id === SHARED_SECTION_ID)) return undefined
|
|
547
|
+
try {
|
|
548
|
+
return ctx.slots.register({
|
|
549
|
+
name: SHARED_SECTION_SLOT,
|
|
550
|
+
id: SHARED_SECTION_ID,
|
|
551
|
+
order: SHARED_SECTION_ORDER,
|
|
552
|
+
label: () => SHARED_SECTION_LABEL,
|
|
553
|
+
children: { [SHARED_ITEM_SLOT]: { kind: "list", scope: "root" } },
|
|
554
|
+
}, PluginSettingsSection)
|
|
555
|
+
} catch (error) {
|
|
556
|
+
// A sibling won the race between the check and the register.
|
|
557
|
+
console.error("[dsh-workbuddy-connect] shared plugin-settings block lost the race; attaching to the winner:", error)
|
|
558
|
+
note("container", error)
|
|
559
|
+
return undefined
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
// Depends on the ITEM slot, not the container: the item slot's
|
|
563
|
+
// declaration epoch is what changes when an owner collapses, so
|
|
564
|
+
// this re-fires and re-registers the card after a recovery.
|
|
565
|
+
const disposeItem = ctx.slots.inject(SHARED_ITEM_SLOT, () => ctx.slots.register({
|
|
566
|
+
name: SHARED_ITEM_SLOT,
|
|
567
|
+
id: SHARED_ITEM_ID,
|
|
568
|
+
order: 50,
|
|
569
|
+
}, () => h(WorkBuddyCard)))
|
|
570
|
+
let disposeContainer = registerContainer()
|
|
571
|
+
// Recovery: `settings.section` stays declared by the host, so this
|
|
572
|
+
// subscription outlives a sibling's collapse. Re-adopt the block so
|
|
573
|
+
// the survivors' cards come back without a page reload.
|
|
574
|
+
const unsubscribe = ctx.slots.subscribe(SHARED_SECTION_SLOT, () => {
|
|
575
|
+
disposeContainer = registerContainer() ?? disposeContainer
|
|
576
|
+
})
|
|
577
|
+
return () => {
|
|
578
|
+
unsubscribe()
|
|
579
|
+
disposeItem()
|
|
580
|
+
disposeContainer?.()
|
|
581
|
+
}
|
|
582
|
+
} catch (error) {
|
|
583
|
+
console.error("[dsh-workbuddy-connect] could not register the settings item:", error)
|
|
584
|
+
note("section-inject", error)
|
|
585
|
+
}
|
|
586
|
+
})
|
|
587
|
+
} catch (error) {
|
|
588
|
+
console.error("[dsh-workbuddy-connect] could not register the settings card:", error)
|
|
589
|
+
note("apply", error)
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
return { inject, apply }
|
|
594
|
+
},
|
|
595
|
+
})
|
package/cordis.patch.yml
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# dsh-workbuddy-connect bundle patch.
|
|
2
|
+
#
|
|
3
|
+
# One row registers both halves: the host's client-module scan resolves a row's
|
|
4
|
+
# specifier to its package root and reads `dsh.client` from package.json, so the
|
|
5
|
+
# browser card loads from the same row that mounts the host provider. The plugin
|
|
6
|
+
# registers the `workbuddy` / `workbuddy-ai` LLM routes itself (via
|
|
7
|
+
# ctx.llm.registerAdapter); this patch only makes Cordis load the bundle. It
|
|
8
|
+
# deliberately does NOT touch the profile's current default model — a fresh
|
|
9
|
+
# install adds the model groups without changing what the user is currently
|
|
10
|
+
# talking to.
|
|
11
|
+
- insert:
|
|
12
|
+
- id: llm-workbuddy
|
|
13
|
+
name: '@sixzjd/dsh-workbuddy-connect'
|