dsh-wtq-pool 0.0.0-stage → 1.0.2
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 +63 -0
- package/LICENSE +21 -0
- package/README.en.md +161 -0
- package/README.md +160 -2
- package/cordis.patch.yml +6 -0
- package/lib/client.js +712 -0
- package/lib/index.d.ts +163 -0
- package/lib/index.js +11627 -0
- package/package.json +119 -4
- package/renderer/index.html +194 -0
- package/renderer/panel.css +1171 -0
- package/renderer/panel.js +3339 -0
|
@@ -0,0 +1,3339 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* dsh-wtq-pool - panel logic.
|
|
5
|
+
*
|
|
6
|
+
* The panel owns no data and makes no network request: everything it shows
|
|
7
|
+
* comes from the plugin's main process through `window.pluginBridge.invoke`,
|
|
8
|
+
* and every channel name matches a case in `onPanelInvoke` (main.js).
|
|
9
|
+
*
|
|
10
|
+
* All API-derived text is written with textContent / createElement, never
|
|
11
|
+
* innerHTML, so a model id or an upstream error can never inject markup.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
// ------------------------------------------------------------------ bridge
|
|
15
|
+
|
|
16
|
+
var bridge = window.pluginBridge || null;
|
|
17
|
+
|
|
18
|
+
function invoke(channel, payload) {
|
|
19
|
+
if (!bridge || typeof bridge.invoke !== "function") {
|
|
20
|
+
return Promise.reject(new Error("panel bridge unavailable"));
|
|
21
|
+
}
|
|
22
|
+
return bridge.invoke(channel, payload || {});
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// ------------------------------------------------------------------ locale
|
|
26
|
+
|
|
27
|
+
var EN = {
|
|
28
|
+
"tab.pool": "Pool",
|
|
29
|
+
"tab.models": "Models",
|
|
30
|
+
"tab.check": "Connection check",
|
|
31
|
+
"host.missing": "The host has not registered {count} model group(s) from this plugin yet: reload the plugin so it re-reads the declaration.",
|
|
32
|
+
"btn.rescan": "Detect accounts again",
|
|
33
|
+
"btn.reset": "Clear all cooldowns",
|
|
34
|
+
"btn.refresh": "Refresh",
|
|
35
|
+
"btn.checkin": "Check in",
|
|
36
|
+
"btn.checkinDone": "Checked in today",
|
|
37
|
+
"btn.save": "Save",
|
|
38
|
+
"btn.discard": "Discard",
|
|
39
|
+
"btn.doctor": "Run connection check",
|
|
40
|
+
"btn.copy": "Copy report",
|
|
41
|
+
"copy.ok": "copied",
|
|
42
|
+
"copy.failedSelected": "copy failed - report selected, press Ctrl+C",
|
|
43
|
+
"copy.failed": "copy failed",
|
|
44
|
+
"region.cn": "China",
|
|
45
|
+
"region.park": "Disable {region}",
|
|
46
|
+
"region.unpark": "Enable {region}",
|
|
47
|
+
"region.parked": "{region} disabled",
|
|
48
|
+
"region.parkedShort": "disabled",
|
|
49
|
+
"region.global": "Global",
|
|
50
|
+
"region.cnShort": "CN",
|
|
51
|
+
"region.globalShort": "Global",
|
|
52
|
+
"pool.summary": "{count} account(s) · {cooling} cooling",
|
|
53
|
+
"pool.healthy": "Rate limits automatically switch to another available account.",
|
|
54
|
+
"pool.allCooling": "Every account is rate-limited right now; requests pause until a cooldown lifts.",
|
|
55
|
+
"pool.checkinAll": "Check in every account",
|
|
56
|
+
"pool.checkinAllHint": "Collect every account's daily check-in. The streak bonus and task rewards run with the scheduled pass.",
|
|
57
|
+
"pool.checkinAllBusy": "Checking in…",
|
|
58
|
+
"pool.checkinAllDone": "Checked in {count} account(s)",
|
|
59
|
+
"pool.checkinAllNone": "Every account is already checked in today",
|
|
60
|
+
"pool.emptyRegion": "No {region} account is signed in yet.",
|
|
61
|
+
"pool.emptyHint": "Sign in to that edition in the WorkBuddy desktop app, then press “Detect accounts again”.",
|
|
62
|
+
"pool.sourceOff": "The WorkBuddy source is switched off in plugin settings.",
|
|
63
|
+
"trae.title": "Trae",
|
|
64
|
+
"trae.gateway": "{region} gateway on port {port}",
|
|
65
|
+
"trae.models": "{count} model(s)",
|
|
66
|
+
"trae.noModels": "no models published yet",
|
|
67
|
+
"trae.off": "The Trae source is switched off in plugin settings.",
|
|
68
|
+
"trae.fallback": "using a spare port; the provider row still names {port}",
|
|
69
|
+
"qoder.title": "Qoder",
|
|
70
|
+
"qoder.gateway": "{region} gateway on port {port}",
|
|
71
|
+
"qoder.models": "{count} model(s)",
|
|
72
|
+
"qoder.noModels": "no models published yet",
|
|
73
|
+
"qoder.off": "The Qoder source is switched off in plugin settings.",
|
|
74
|
+
"source.enable": "Enable {source}",
|
|
75
|
+
"source.disable": "Disable {source}",
|
|
76
|
+
"source.noAccounts": "no local sign-in found",
|
|
77
|
+
"source.traeSlots": "Two domestic slots only (Trae CN, TRAE SOLO CN); signing in with another account overwrites the previous one.",
|
|
78
|
+
"source.qoderSlots": "One sign-in per install; signing in with another account overwrites the previous one.",
|
|
79
|
+
"source.traeCheckin": "Daily check-in is one per device: once any account has claimed today, the other accounts show “already checked in on this device”.",
|
|
80
|
+
"source.expired": "token expired",
|
|
81
|
+
"workbuddy.title": "WorkBuddy",
|
|
82
|
+
"models.noGateway": "This source has no gateway yet",
|
|
83
|
+
"models.noDirectory": "This source's model list could not be read",
|
|
84
|
+
"source.accountCount": "{count} account(s)",
|
|
85
|
+
"source.checkinDone": "Daily reward collected",
|
|
86
|
+
"source.checkinSkipped": "Already collected today",
|
|
87
|
+
"source.planTitle": "Plan",
|
|
88
|
+
"source.planTrial": "trial",
|
|
89
|
+
"source.quotaUnknown": "usage unavailable",
|
|
90
|
+
"source.checkedIn": "Checked in today",
|
|
91
|
+
"source.claimable": "Can claim today",
|
|
92
|
+
"source.noCampaign": "no daily check-in",
|
|
93
|
+
"source.claimableAt": "Can claim after {time}",
|
|
94
|
+
"source.deviceCheckedIn": "This device checked in today",
|
|
95
|
+
"qoder.fallback": "using a spare port; the provider row still names {port}",
|
|
96
|
+
"badge.next": "next up",
|
|
97
|
+
"badge.cooling": "cooling",
|
|
98
|
+
"badge.healthy": "healthy",
|
|
99
|
+
"acct.token": "token {time}",
|
|
100
|
+
"acct.tokenExpiring": "token expires in {min} min",
|
|
101
|
+
"time.minutes": "{count} min",
|
|
102
|
+
"time.hours": "{count} h",
|
|
103
|
+
"time.days": "{count} d",
|
|
104
|
+
"acct.noExpiry": "no token expiry reported",
|
|
105
|
+
"acct.coolUntil": "cooling until {time}",
|
|
106
|
+
"acct.hits": "{hits} rate-limit hit(s)",
|
|
107
|
+
"acct.choose": "Use credits from",
|
|
108
|
+
"acct.automatic": "Automatic (follow account usage)",
|
|
109
|
+
"acct.choiceHint": "The picked account goes first; a rate limit switches account until the cooldown lifts. Automatic follows the usage mode.",
|
|
110
|
+
"acct.unavailable": "Selected account unavailable (automatic fallback)",
|
|
111
|
+
"acct.choiceCredits": "{label} · {credits} credits",
|
|
112
|
+
"badge.preferred": "selected",
|
|
113
|
+
"acct.on": "Enabled",
|
|
114
|
+
"acct.off": "Switched off",
|
|
115
|
+
"acct.switchOn": "Use this account again",
|
|
116
|
+
"acct.switchOff": "Keep this account out of the pool for now",
|
|
117
|
+
"acct.offHint": "Switched off: requests never use this account until you switch it back on.",
|
|
118
|
+
"acct.remove": "Remove",
|
|
119
|
+
"acct.removeHint": "Removes the account for good. Restore it under “Removed accounts”.",
|
|
120
|
+
"acct.stateError": "Could not change this account: {message}",
|
|
121
|
+
"pool.switchedOff": "{count} switched off",
|
|
122
|
+
"removed.title": "Removed accounts",
|
|
123
|
+
"removed.hint": "These accounts are never used, and detecting accounts again will not bring them back.",
|
|
124
|
+
"removed.restore": "Restore",
|
|
125
|
+
"acct.modelCool": "{model} cooling until {time}",
|
|
126
|
+
"credits.title": "Credits",
|
|
127
|
+
"credits.unavailable": "credits unavailable",
|
|
128
|
+
"credits.package": "{remain} / {size}",
|
|
129
|
+
"credits.monthly": "monthly",
|
|
130
|
+
"credits.refreshAt": "refreshes {time}",
|
|
131
|
+
"credits.expiresAt": "expires {date}",
|
|
132
|
+
"credits.validDays": "valid {days} days after claiming",
|
|
133
|
+
"credits.roundExpires": "this round expires {date}",
|
|
134
|
+
"credits.noExpiry": "does not expire",
|
|
135
|
+
"credits.expiringSoon": "{amount} credits expire within 3 days",
|
|
136
|
+
"credits.planQuota": "Plan credits",
|
|
137
|
+
"credits.addOnQuota": "Add-on credits",
|
|
138
|
+
"credits.expand": "Expand",
|
|
139
|
+
"credits.collapse": "Collapse",
|
|
140
|
+
"credits.keepAtLeast": "Keep at least",
|
|
141
|
+
"credits.keepUnit": "credits",
|
|
142
|
+
"credits.keepSave": "Save",
|
|
143
|
+
"credits.keepHint": "Below this balance the account is left alone and the credits stay for you.",
|
|
144
|
+
"credits.keepBadge": "keep {credits}",
|
|
145
|
+
"checkin.title": "Daily check-in",
|
|
146
|
+
"checkin.streak": "{days}-day streak",
|
|
147
|
+
"checkin.daily": "+{credit} credits/day",
|
|
148
|
+
"checkin.bonus": "day {days} bonus +{credit}",
|
|
149
|
+
"checkin.claimed": "claimed +{credit} credits",
|
|
150
|
+
"checkin.inactive": "check-in is not available for this account",
|
|
151
|
+
"checkin.claiming": "Checking in…",
|
|
152
|
+
"models.serviceModels": "Models for this service",
|
|
153
|
+
"models.fetchModelList": "Fetch list",
|
|
154
|
+
"models.modelsLoading": "Fetching model list…",
|
|
155
|
+
"models.searchModel": "Search models…",
|
|
156
|
+
"models.selectAll": "Select all",
|
|
157
|
+
"models.deselectAll": "Deselect all",
|
|
158
|
+
"models.modelConfigurations": "Model settings",
|
|
159
|
+
"models.searchChosen": "Search added models…",
|
|
160
|
+
"models.noModelsChosen": "No model selected yet. Tick one in the model list.",
|
|
161
|
+
"models.noChosenMatch": "No added model matches",
|
|
162
|
+
"models.removeModel": "Remove model",
|
|
163
|
+
"models.advanced": "Advanced",
|
|
164
|
+
"models.alias": "Alias",
|
|
165
|
+
"models.aliasPlaceholder": "e.g. fast",
|
|
166
|
+
"models.aliasHelp": "Shown wherever the model is named; the request still uses the model ID.",
|
|
167
|
+
"models.window": "Context window",
|
|
168
|
+
"models.windowFollows": "Follows the catalog; editing pins your value.",
|
|
169
|
+
"models.maxOutput": "Max output",
|
|
170
|
+
"models.outputHelp": "The largest reply this model may produce.",
|
|
171
|
+
"models.levels": "Thinking levels",
|
|
172
|
+
"models.defaultLevel": "Default thinking level",
|
|
173
|
+
"models.capabilities": "Attachments",
|
|
174
|
+
"models.imageInput": "Images",
|
|
175
|
+
"models.empty": "No model in the catalog yet.",
|
|
176
|
+
"models.noMatch": "No model matches the filter.",
|
|
177
|
+
"models.capped": "not published",
|
|
178
|
+
"models.cappedHint": "One provider publishes at most {count} models; a tick past that never reaches the model picker.",
|
|
179
|
+
"models.zeroEnabled": "Enable at least one model before saving.",
|
|
180
|
+
"models.dirty": "Unsaved changes",
|
|
181
|
+
"models.saving": "Saving…",
|
|
182
|
+
"models.saved": "Model selection saved",
|
|
183
|
+
"models.applyHint": "Saved. If the model picker does not follow, reload the plugin.",
|
|
184
|
+
"models.saveError": "Could not save: {message}",
|
|
185
|
+
"models.reload": "The model list changed. Reload the plugin to sync it.",
|
|
186
|
+
"models.addressDrift": "The published address differs from the one this run bound. Reload the plugin.",
|
|
187
|
+
"dist.title": "Account usage",
|
|
188
|
+
"dist.priority": "Priority",
|
|
189
|
+
"dist.roundRobin": "Round-robin",
|
|
190
|
+
"dist.sticky": "Per conversation",
|
|
191
|
+
"dist.expiry": "Prefer expiring credits",
|
|
192
|
+
"dist.hintPriority": "Drain one account first, then move on to the next.",
|
|
193
|
+
"dist.hintRoundRobin": "Spread the spend evenly across every account.",
|
|
194
|
+
"dist.hintSticky": "Keep one conversation on one account. A new conversation takes the next account.",
|
|
195
|
+
"dist.hintExpiry": "Use one-off credits that are about to expire before the rest.",
|
|
196
|
+
"dist.hintSourcePriority": "Keeps using one account; a picked account is always the one used.",
|
|
197
|
+
"dist.hintSourceRoundRobin": "Each request moves to the next account, spreading the spend; a picked account always wins.",
|
|
198
|
+
"dist.hintSourceExpiry": "Use one-off credits that are about to expire first. A chosen account still takes priority.",
|
|
199
|
+
"auto.title": "Daily check-in",
|
|
200
|
+
"auto.on": "On",
|
|
201
|
+
"auto.off": "Off",
|
|
202
|
+
"auto.hours": "Hours",
|
|
203
|
+
"auto.hoursHint": "Beijing time (UTC+8), e.g. 9 or 9,21",
|
|
204
|
+
"auto.badHours": "Hours must be whole hours 0–23, for example 9 or 9,21.",
|
|
205
|
+
"auto.hoursSave": "Save hours",
|
|
206
|
+
"auto.runNow": "Run now",
|
|
207
|
+
"auto.runNowHint": "Runs the same pass the scheduled hour runs, and reports what it did.",
|
|
208
|
+
"auto.running": "Running…",
|
|
209
|
+
"auto.lastManual": "Last run {slot} (manual): {count} account(s)",
|
|
210
|
+
"auto.lastAuto": "Last run {slot}: {count} account(s)",
|
|
211
|
+
"auto.lastFailedAt": "Last run {slot} failed: {message}",
|
|
212
|
+
"auto.neverRun": "Has not run yet",
|
|
213
|
+
"auto.rowWorkbuddy": "WorkBuddy",
|
|
214
|
+
"auto.rowTravel": "Travel",
|
|
215
|
+
"auto.rowQoder": "Qoder",
|
|
216
|
+
"auto.rowTrae": "Trae",
|
|
217
|
+
"auto.rowCounts": "{label}: {claimed} collected · {skipped} skipped · {failed} failed",
|
|
218
|
+
"auto.rowGrowth": "Streak and tasks: {streak} streak tier(s) · {tasks} task(s) · +{credit} credits · {failed} failed",
|
|
219
|
+
"auto.rowTravelCounts": "{label}: {departed} sent · {claimed} claimed · {adopted} adopted",
|
|
220
|
+
"auto.hint": "Claims the daily reward of all three sources (WorkBuddy / Trae / Qoder), the streak bonus and task rewards of domestic WorkBuddy accounts, and runs the travel patrol in one pass; today's is skipped, a missed hour runs at the next start.",
|
|
221
|
+
"composer.title": "Composer credits",
|
|
222
|
+
"composer.on": "On",
|
|
223
|
+
"composer.off": "Off",
|
|
224
|
+
"composer.label": "Show the active account's credits in the composer",
|
|
225
|
+
"composer.hint": "When the selected model belongs to this plugin, its account's credits appear on the left of the composer. Click to switch accounts.",
|
|
226
|
+
"composer.show": "Show credits",
|
|
227
|
+
"composer.hide": "Hide credits",
|
|
228
|
+
"check.title": "Connection check",
|
|
229
|
+
"check.empty": "Run the check to see whether a chat can reach WorkBuddy, Trae or Qoder.",
|
|
230
|
+
"check.pass": "PASS",
|
|
231
|
+
"check.fail": "FAIL",
|
|
232
|
+
"foot.shim": "endpoint {url}",
|
|
233
|
+
"foot.shimOff": "endpoint not listening",
|
|
234
|
+
"foot.rosterStale": "{region} needs attention: {detail}",
|
|
235
|
+
"foot.github": "GitHub",
|
|
236
|
+
"foot.npm": "npm",
|
|
237
|
+
"foot.docs": "Docs",
|
|
238
|
+
"foot.license": "License",
|
|
239
|
+
"err.bridge": "The panel bridge is unavailable.",
|
|
240
|
+
"tag.free": "free",
|
|
241
|
+
"tag.rate": "x{rate}",
|
|
242
|
+
"tag.promoNight": "free until {hour}",
|
|
243
|
+
"tag.promoLater": "free from {hour}",
|
|
244
|
+
"state.loading": "Loading…",
|
|
245
|
+
"state.refreshing": "checking…",
|
|
246
|
+
"probe.pending": "reading…",
|
|
247
|
+
"btn.refreshing": "Refreshing…",
|
|
248
|
+
"models.staleSelection": "{region} had models that no longer exist, so every model is shown. Tick and save again.",
|
|
249
|
+
};
|
|
250
|
+
|
|
251
|
+
var ZH = {
|
|
252
|
+
"tab.pool": "账号池",
|
|
253
|
+
"tab.models": "模型",
|
|
254
|
+
"tab.check": "连接检查",
|
|
255
|
+
"host.missing": "宿主还没有注册本插件的 {count} 个模型组:重新加载插件让它重读声明。",
|
|
256
|
+
"btn.rescan": "重新检测账号",
|
|
257
|
+
"btn.reset": "清除所有冷却",
|
|
258
|
+
"btn.refresh": "刷新",
|
|
259
|
+
"btn.checkin": "签到",
|
|
260
|
+
"btn.checkinDone": "今日已签到",
|
|
261
|
+
"btn.save": "保存",
|
|
262
|
+
"btn.discard": "放弃",
|
|
263
|
+
"btn.doctor": "运行连接检查",
|
|
264
|
+
"btn.copy": "复制报告",
|
|
265
|
+
"copy.ok": "已复制",
|
|
266
|
+
"copy.failedSelected": "复制失败,已选中报告,按 Ctrl+C",
|
|
267
|
+
"copy.failed": "复制失败",
|
|
268
|
+
"region.cn": "国内版",
|
|
269
|
+
"region.park": "停用{region}",
|
|
270
|
+
"region.unpark": "启用{region}",
|
|
271
|
+
"region.parked": "已停用{region}",
|
|
272
|
+
"region.parkedShort": "已停用",
|
|
273
|
+
"region.global": "国际版",
|
|
274
|
+
"region.cnShort": "国内版",
|
|
275
|
+
"region.globalShort": "国际版",
|
|
276
|
+
"pool.summary": "{count} 个账号 · {cooling} 个冷却中",
|
|
277
|
+
"pool.healthy": "限流时会自动切换到其他可用账号。",
|
|
278
|
+
"pool.allCooling": "所有账号当前都被限流,请求会等到有账号冷却结束。",
|
|
279
|
+
"pool.checkinAll": "全部签到",
|
|
280
|
+
"pool.checkinAllHint": "一次领完所有账号今天的签到。连登奖励和任务奖励随自动签到、立即运行领取。",
|
|
281
|
+
"pool.checkinAllBusy": "签到中…",
|
|
282
|
+
"pool.checkinAllDone": "已签到 {count} 个账号",
|
|
283
|
+
"pool.checkinAllNone": "所有账号今天都已签到",
|
|
284
|
+
"pool.emptyRegion": "{region}这一侧还没有登录的账号。",
|
|
285
|
+
"pool.emptyHint": "请在该版本的 WorkBuddy 桌面 App 里登录,再点「重新检测账号」。",
|
|
286
|
+
"pool.sourceOff": "WorkBuddy 来源已在插件设置里关闭。",
|
|
287
|
+
"trae.title": "Trae",
|
|
288
|
+
"trae.gateway": "{region}网关,端口 {port}",
|
|
289
|
+
"trae.models": "{count} 个模型",
|
|
290
|
+
"trae.noModels": "还没有发布模型",
|
|
291
|
+
"trae.off": "Trae 来源已在插件设置里关闭。",
|
|
292
|
+
"trae.fallback": "用的是备用端口;供应商行仍指向 {port}",
|
|
293
|
+
"qoder.title": "Qoder",
|
|
294
|
+
"qoder.gateway": "{region}网关,端口 {port}",
|
|
295
|
+
"qoder.models": "{count} 个模型",
|
|
296
|
+
"qoder.noModels": "还没有发布模型",
|
|
297
|
+
"qoder.off": "Qoder 来源已在插件设置里关闭。",
|
|
298
|
+
"source.enable": "启用{source}",
|
|
299
|
+
"source.disable": "停用{source}",
|
|
300
|
+
"source.traeSlots": "国内版只有 Trae CN 与 TRAE SOLO CN 两个登录位;换号登录会覆盖前一个账号。",
|
|
301
|
+
"source.qoderSlots": "每个安装只保存一个登录;在 App 里重新登录另一个账号会覆盖前一个。",
|
|
302
|
+
"source.traeCheckin": "每日签到一台设备只能领一次:本机今天已经领过,其他账号会显示「本机今日已签到」。",
|
|
303
|
+
"source.expired": "令牌已过期",
|
|
304
|
+
"workbuddy.title": "WorkBuddy",
|
|
305
|
+
"models.noGateway": "该来源还没有网关",
|
|
306
|
+
"models.noDirectory": "该来源的模型列表读不出来",
|
|
307
|
+
"source.accountCount": "{count} 个账号",
|
|
308
|
+
"source.checkinDone": "已领取今日奖励",
|
|
309
|
+
"source.planTitle": "套餐",
|
|
310
|
+
"source.planTrial": "试用中",
|
|
311
|
+
"source.checkinSkipped": "今天已经领过了",
|
|
312
|
+
"source.quotaUnknown": "额度数据不可用",
|
|
313
|
+
"source.checkedIn": "今日已签到",
|
|
314
|
+
"source.claimable": "今日可签到",
|
|
315
|
+
"source.noAccounts": "未找到本机登录",
|
|
316
|
+
"source.noCampaign": "该账号无签到活动",
|
|
317
|
+
"source.deviceCheckedIn": "本机今日已签到",
|
|
318
|
+
"qoder.fallback": "用的是备用端口;供应商行仍指向 {port}",
|
|
319
|
+
"badge.next": "下一个",
|
|
320
|
+
"source.claimableAt": "{time} 后可签到",
|
|
321
|
+
"badge.cooling": "冷却中",
|
|
322
|
+
"badge.healthy": "可用",
|
|
323
|
+
"acct.token": "令牌 {time}",
|
|
324
|
+
"acct.tokenExpiring": "令牌将在 {min} 分钟后过期",
|
|
325
|
+
"time.minutes": "{count} 分钟",
|
|
326
|
+
"time.hours": "{count} 小时",
|
|
327
|
+
"time.days": "{count} 天",
|
|
328
|
+
"acct.noExpiry": "令牌没有报告有效期",
|
|
329
|
+
"acct.coolUntil": "冷却至 {time}",
|
|
330
|
+
"acct.hits": "已触发 {hits} 次限流",
|
|
331
|
+
"acct.choose": "使用哪个账号的额度",
|
|
332
|
+
"acct.automatic": "自动选择(按使用方式)",
|
|
333
|
+
"acct.choiceHint": "优先用所选账号,限流时临时换号;自动选择则按使用方式分配。",
|
|
334
|
+
"acct.unavailable": "所选账号暂不可用(自动换号)",
|
|
335
|
+
"acct.choiceCredits": "{label} · {credits} 积分",
|
|
336
|
+
"badge.preferred": "已选",
|
|
337
|
+
"acct.on": "启用中",
|
|
338
|
+
"acct.off": "已停用",
|
|
339
|
+
"acct.switchOn": "重新使用该账号",
|
|
340
|
+
"acct.switchOff": "暂时不使用该账号",
|
|
341
|
+
"acct.offHint": "已停用:在重新启用之前,请求不会使用该账号。",
|
|
342
|
+
"acct.remove": "移除",
|
|
343
|
+
"acct.removeHint": "永久移出账号池;可在「已移除的账号」里恢复。",
|
|
344
|
+
"acct.stateError": "账号设置失败:{message}",
|
|
345
|
+
"pool.switchedOff": "{count} 个已停用",
|
|
346
|
+
"removed.title": "已移除的账号",
|
|
347
|
+
"removed.hint": "这些账号不会被使用,重新检测也不会回到池中。",
|
|
348
|
+
"removed.restore": "恢复",
|
|
349
|
+
"acct.modelCool": "{model} 冷却至 {time}",
|
|
350
|
+
"credits.title": "积分",
|
|
351
|
+
"credits.unavailable": "积分读取失败",
|
|
352
|
+
"credits.package": "{remain} / {size}",
|
|
353
|
+
"credits.monthly": "月包",
|
|
354
|
+
"credits.refreshAt": "{time} 刷新",
|
|
355
|
+
"credits.expiresAt": "{date} 过期",
|
|
356
|
+
"credits.validDays": "领取后 {days} 天有效",
|
|
357
|
+
"credits.roundExpires": "本轮 {date} 过期",
|
|
358
|
+
"credits.noExpiry": "不过期",
|
|
359
|
+
"credits.expiringSoon": "{amount} 积分将在 3 天内过期",
|
|
360
|
+
"credits.planQuota": "套餐内积分",
|
|
361
|
+
"credits.addOnQuota": "资源包积分",
|
|
362
|
+
"credits.expand": "展开",
|
|
363
|
+
"credits.collapse": "收起",
|
|
364
|
+
"credits.keepAtLeast": "至少保留",
|
|
365
|
+
"credits.keepUnit": "积分",
|
|
366
|
+
"credits.keepSave": "保存",
|
|
367
|
+
"credits.keepHint": "余额降到这个数就不再使用该账号,剩下的留给你自己用。",
|
|
368
|
+
"credits.keepBadge": "保留 {credits}",
|
|
369
|
+
"checkin.title": "每日签到",
|
|
370
|
+
"checkin.streak": "连签 {days} 天",
|
|
371
|
+
"checkin.daily": "每天 +{credit} 积分",
|
|
372
|
+
"checkin.bonus": "第 {days} 天额外 +{credit}",
|
|
373
|
+
"checkin.claimed": "已领取 +{credit} 积分",
|
|
374
|
+
"checkin.inactive": "该账号没有签到活动",
|
|
375
|
+
"checkin.claiming": "签到中…",
|
|
376
|
+
"models.serviceModels": "该服务的模型",
|
|
377
|
+
"models.fetchModelList": "获取列表",
|
|
378
|
+
"models.modelsLoading": "正在获取模型列表…",
|
|
379
|
+
"models.searchModel": "搜索模型…",
|
|
380
|
+
"models.selectAll": "全选",
|
|
381
|
+
"models.deselectAll": "取消全选",
|
|
382
|
+
"models.modelConfigurations": "模型设置",
|
|
383
|
+
"models.searchChosen": "搜索已添加模型…",
|
|
384
|
+
"models.noModelsChosen": "尚未选择模型。请在模型列表中勾选。",
|
|
385
|
+
"models.noChosenMatch": "没有匹配的已添加模型",
|
|
386
|
+
"models.removeModel": "移除模型",
|
|
387
|
+
"models.advanced": "高级",
|
|
388
|
+
"models.alias": "别名",
|
|
389
|
+
"models.aliasPlaceholder": "例如 fast",
|
|
390
|
+
"models.aliasHelp": "在显示模型名称的地方生效;请求仍使用模型 ID。",
|
|
391
|
+
"models.window": "上下文窗口",
|
|
392
|
+
"models.windowFollows": "跟随目录;手动修改后会固定为你的值。",
|
|
393
|
+
"models.maxOutput": "最大输出",
|
|
394
|
+
"models.outputHelp": "该模型单次回复的最大长度。",
|
|
395
|
+
"models.levels": "思考等级",
|
|
396
|
+
"models.defaultLevel": "默认思考等级",
|
|
397
|
+
"models.capabilities": "附件",
|
|
398
|
+
"models.imageInput": "图片",
|
|
399
|
+
"models.empty": "目录里还没有模型。",
|
|
400
|
+
"models.noMatch": "没有匹配的模型。",
|
|
401
|
+
"models.capped": "未发布",
|
|
402
|
+
"models.cappedHint": "一个模型组最多发布 {count} 个模型,多勾的不会出现在模型选择器里。",
|
|
403
|
+
"models.zeroEnabled": "至少启用一个模型才能保存。",
|
|
404
|
+
"models.dirty": "有未保存的修改",
|
|
405
|
+
"models.saving": "保存中…",
|
|
406
|
+
"models.saved": "模型选择已保存",
|
|
407
|
+
"models.applyHint": "已保存。模型选择器没同步时,重新加载插件。",
|
|
408
|
+
"models.saveError": "保存失败:{message}",
|
|
409
|
+
"models.reload": "模型列表有变化。重新加载插件后同步。",
|
|
410
|
+
"models.addressDrift": "对外公布的地址与本次运行的地址不一致。重新加载插件。",
|
|
411
|
+
"dist.title": "账号使用方式",
|
|
412
|
+
"dist.priority": "优先用一个",
|
|
413
|
+
"dist.roundRobin": "轮流使用",
|
|
414
|
+
"dist.sticky": "同一对话固定",
|
|
415
|
+
"dist.expiry": "优先使用临期积分",
|
|
416
|
+
"dist.hintPriority": "先用完一个账号的额度,再换下一个。",
|
|
417
|
+
"dist.hintRoundRobin": "把消耗均摊到每个账号上。",
|
|
418
|
+
"dist.hintSticky": "同一个对话一直用一个账号,新对话才换下一个。",
|
|
419
|
+
"dist.hintExpiry": "优先消耗即将到期的一次性积分。",
|
|
420
|
+
"dist.hintSourcePriority": "一直用同一个账号;已选具体账号时始终用它。",
|
|
421
|
+
"dist.hintSourceRoundRobin": "每个请求换下一个账号,摊平消耗;已选具体账号时以它为准。",
|
|
422
|
+
"dist.hintSourceExpiry": "优先消耗即将到期的一次性积分;已指定账号时仍以该账号为准。",
|
|
423
|
+
"auto.title": "自动签到",
|
|
424
|
+
"auto.on": "开启",
|
|
425
|
+
"auto.off": "关闭",
|
|
426
|
+
"composer.title": "输入框积分",
|
|
427
|
+
"composer.on": "已开启",
|
|
428
|
+
"composer.off": "已关闭",
|
|
429
|
+
"composer.label": "在输入框工具栏显示当前账号积分",
|
|
430
|
+
"composer.hint": "开启后,选中本插件的模型时,输入框左侧显示正在使用的账号积分。点开可以换号。",
|
|
431
|
+
"composer.show": "显示积分",
|
|
432
|
+
"composer.hide": "隐藏积分",
|
|
433
|
+
"auto.hours": "时间",
|
|
434
|
+
"auto.hoursHint": "按北京时间(UTC+8),如 9 或 9,21",
|
|
435
|
+
"auto.hoursSave": "保存时间",
|
|
436
|
+
"auto.runNow": "立即运行",
|
|
437
|
+
"auto.runNowHint": "执行与定时相同的一轮,并报告结果。",
|
|
438
|
+
"auto.running": "运行中…",
|
|
439
|
+
"auto.lastManual": "上次 {slot}(手动):领取 {count} 个账号",
|
|
440
|
+
"auto.lastAuto": "上次 {slot}:领取 {count} 个账号",
|
|
441
|
+
"auto.lastFailedAt": "上次 {slot} 失败:{message}",
|
|
442
|
+
"auto.neverRun": "还没运行过",
|
|
443
|
+
"auto.rowWorkbuddy": "WorkBuddy",
|
|
444
|
+
"auto.rowTravel": "猫猫旅行",
|
|
445
|
+
"auto.rowQoder": "Qoder",
|
|
446
|
+
"auto.rowTrae": "Trae",
|
|
447
|
+
"auto.rowCounts": "{label}:领取 {claimed} · 跳过 {skipped} · 失败 {failed}",
|
|
448
|
+
"auto.rowGrowth": "连登与任务:连登 {streak} 档 · 任务 {tasks} 个 · +{credit} 积分 · 失败 {failed}",
|
|
449
|
+
"auto.rowTravelCounts": "{label}:派出 {departed} · 领奖 {claimed} · 领养 {adopted}",
|
|
450
|
+
"auto.hint": "时间到后一次性领取三家(WorkBuddy / Trae / Qoder)的签到奖励、WorkBuddy 国内账号的连登奖励和任务奖励,并顺带完成猫猫旅行;今天已领过的跳过,错过的到下次启动补领。",
|
|
451
|
+
"auto.badHours": "时间请填 0–23 的整数小时,例如 9 或 9,21。",
|
|
452
|
+
"check.title": "连接检查",
|
|
453
|
+
"check.empty": "运行检查可以看到对话能否连上 WorkBuddy、Trae 或 Qoder。",
|
|
454
|
+
"check.pass": "通过",
|
|
455
|
+
"check.fail": "未通过",
|
|
456
|
+
"foot.shim": "端点 {url}",
|
|
457
|
+
"foot.shimOff": "端点未监听",
|
|
458
|
+
"foot.rosterStale": "{region} 需要注意:{detail}",
|
|
459
|
+
"foot.github": "GitHub",
|
|
460
|
+
"foot.npm": "npm",
|
|
461
|
+
"foot.docs": "文档",
|
|
462
|
+
"foot.license": "许可",
|
|
463
|
+
"err.bridge": "面板桥不可用。",
|
|
464
|
+
"tag.free": "免费",
|
|
465
|
+
"tag.rate": "x{rate}",
|
|
466
|
+
"tag.promoNight": "夜间免费 · 到 {hour}",
|
|
467
|
+
"tag.promoLater": "{hour} 起免费",
|
|
468
|
+
"state.loading": "加载中…",
|
|
469
|
+
"state.refreshing": "正在检测…",
|
|
470
|
+
"probe.pending": "读取中…",
|
|
471
|
+
"btn.refreshing": "刷新中…",
|
|
472
|
+
"models.staleSelection": "{region}保存的选择里有已下线的模型,现按全部模型显示。请重新勾选并保存。",
|
|
473
|
+
};
|
|
474
|
+
|
|
475
|
+
var locale = String(document.documentElement.lang || navigator.language || "zh-CN");
|
|
476
|
+
var TABLE = locale.toLowerCase().indexOf("zh") === 0 ? ZH : EN;
|
|
477
|
+
function t(key, vars) {
|
|
478
|
+
var text = TABLE[key] !== undefined ? TABLE[key] : (EN[key] !== undefined ? EN[key] : key);
|
|
479
|
+
if (!vars) return text;
|
|
480
|
+
return text.replace(/\{(\w+)\}/g, function (match, name) {
|
|
481
|
+
return vars[name] === undefined ? match : String(vars[name]);
|
|
482
|
+
});
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
// ---------------------------------------------------------------- helpers
|
|
486
|
+
|
|
487
|
+
function $(id) { return document.getElementById(id); }
|
|
488
|
+
|
|
489
|
+
function el(tag, className, text) {
|
|
490
|
+
var node = document.createElement(tag);
|
|
491
|
+
if (className) node.className = className;
|
|
492
|
+
if (text !== undefined && text !== null) node.textContent = String(text);
|
|
493
|
+
return node;
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
function clear(node) {
|
|
497
|
+
while (node.firstChild) node.removeChild(node.firstChild);
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
function show(node, visible) { node.hidden = !visible; }
|
|
501
|
+
|
|
502
|
+
function num(value, fallback) {
|
|
503
|
+
var n = Number(value);
|
|
504
|
+
return Number.isFinite(n) ? n : fallback;
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
function formatStamp(iso) {
|
|
508
|
+
if (!iso) return "";
|
|
509
|
+
var d = new Date(iso);
|
|
510
|
+
if (Number.isNaN(d.getTime())) return String(iso);
|
|
511
|
+
return String(d.getHours()).padStart(2, "0") + ":" + String(d.getMinutes()).padStart(2, "0");
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
function formatRelative(iso) {
|
|
515
|
+
if (!iso) return "";
|
|
516
|
+
var d = new Date(iso).getTime();
|
|
517
|
+
if (!Number.isFinite(d)) return "";
|
|
518
|
+
var ms = d - Date.now();
|
|
519
|
+
if (!Number.isFinite(ms)) return "";
|
|
520
|
+
// Through the tables: a bare "42 min" inside a translated sentence read as two
|
|
521
|
+
// languages in one line. The caller handles the past case itself, so this is
|
|
522
|
+
// only ever asked about a future instant.
|
|
523
|
+
var mins = Math.ceil(ms / 60000);
|
|
524
|
+
var hours = Math.round(mins / 60);
|
|
525
|
+
if (hours < 1) return t("time.minutes", { count: mins });
|
|
526
|
+
if (hours < 48) return t("time.hours", { count: hours });
|
|
527
|
+
return t("time.days", { count: Math.round(hours / 24) });
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
function formatDayStamp(ms) {
|
|
531
|
+
var d = new Date(ms);
|
|
532
|
+
if (Number.isNaN(d.getTime())) return "";
|
|
533
|
+
// A pool can be good for a year or more, and "10-21" alone reads as this
|
|
534
|
+
// year; the year shows up only when it is not the one on screen now.
|
|
535
|
+
var now = new Date();
|
|
536
|
+
var text = String(d.getMonth() + 1) + "-" + String(d.getDate());
|
|
537
|
+
return d.getFullYear() === now.getFullYear() ? text : d.getFullYear() + "-" + text;
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
function minutesUntil(iso) {
|
|
541
|
+
if (!iso) return null;
|
|
542
|
+
var d = new Date(iso).getTime();
|
|
543
|
+
if (!Number.isFinite(d)) return null;
|
|
544
|
+
return Math.round((d - Date.now()) / 60000);
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
/**
|
|
548
|
+
* The token line under an account's name.
|
|
549
|
+
*
|
|
550
|
+
* An expiry already in the past is not "expires in 0 min" — the token is dead,
|
|
551
|
+
* and saying it will expire in no time at all sends the user looking for a
|
|
552
|
+
* countdown that never arrives. Clamping the sign away is what produced that
|
|
553
|
+
* wording, so a past date gets its own branch.
|
|
554
|
+
*/
|
|
555
|
+
function tokenChip(iso) {
|
|
556
|
+
var mins = minutesUntil(iso);
|
|
557
|
+
if (mins === null) return el("span", null, t("acct.noExpiry"));
|
|
558
|
+
if (mins < 0) return el("span", "finding-fail", t("source.expired"));
|
|
559
|
+
if (mins <= 10) return el("span", "finding-fail", t("acct.tokenExpiring", { min: mins }));
|
|
560
|
+
return el("span", null, t("acct.token", { time: formatRelative(iso) }));
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
/**
|
|
564
|
+
* The chips one roster row carries: what the model costs, and — when a
|
|
565
|
+
* promotion covers it — whether its free window is open now or opens later.
|
|
566
|
+
*
|
|
567
|
+
* `tone` picks the colour; the caller renders each one as a badge.
|
|
568
|
+
*/
|
|
569
|
+
function tagOf(model) {
|
|
570
|
+
var out = [];
|
|
571
|
+
if (model.multiplier === 0) out.push({ text: t("tag.free"), tone: "free" });
|
|
572
|
+
else if (typeof model.multiplier === "number") {
|
|
573
|
+
out.push({ text: t("tag.rate", { rate: model.multiplier.toFixed(2) }), tone: "" });
|
|
574
|
+
}
|
|
575
|
+
var promo = model.promo;
|
|
576
|
+
if (promo && promo.kind === "night") {
|
|
577
|
+
out.push({ text: t("tag.promoNight", { hour: clockHour(promo.untilHour) }), tone: "free" });
|
|
578
|
+
} else if (promo && promo.kind === "night-later") {
|
|
579
|
+
out.push({ text: t("tag.promoLater", { hour: clockHour(promo.nextHour) }), tone: "" });
|
|
580
|
+
}
|
|
581
|
+
return out;
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
/** "08:00" for a clock hour, which is how a window edge reads to a person. */
|
|
585
|
+
function clockHour(hour) {
|
|
586
|
+
var value = Math.round(Number(hour));
|
|
587
|
+
if (!Number.isFinite(value)) return "";
|
|
588
|
+
value = Math.max(0, Math.min(23, value));
|
|
589
|
+
return (value < 10 ? "0" : "") + value + ":00";
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
/**
|
|
593
|
+
* Show the banner. A failure reaching the panel arrives wrapped in whatever
|
|
594
|
+
* envelope the bridge used; the reason itself is what the user needs, so the
|
|
595
|
+
* envelope is stripped here once instead of at every call site.
|
|
596
|
+
*/
|
|
597
|
+
function note(message, kind) {
|
|
598
|
+
var banner = $("banner");
|
|
599
|
+
if (!message) { show(banner, false); banner.textContent = ""; return; }
|
|
600
|
+
banner.textContent = String(message)
|
|
601
|
+
.replace(/^Error invoking remote method '[^']*':\s*/, "")
|
|
602
|
+
.replace(/^Error:\s*/, "");
|
|
603
|
+
banner.className = kind === "warn" ? "banner warn" : "banner";
|
|
604
|
+
show(banner, true);
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
function announce(message) {
|
|
608
|
+
var live = $("live");
|
|
609
|
+
live.textContent = "";
|
|
610
|
+
window.setTimeout(function () { live.textContent = message; }, 20);
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
// ------------------------------------------------------------------- state
|
|
614
|
+
|
|
615
|
+
var state = null;
|
|
616
|
+
var activeRegion = "cn";
|
|
617
|
+
/** Trae's and Qoder's own region rows; WorkBuddy's stays in `activeRegion`. */
|
|
618
|
+
var activeSourceRegion = { trae: "cn", qoder: "cn" };
|
|
619
|
+
|
|
620
|
+
/** The region one source's page currently shows. */
|
|
621
|
+
function activeRegionOf(source) {
|
|
622
|
+
return source === "workbuddy" ? activeRegion : (activeSourceRegion[source] || "cn");
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
/** Remember one source's region; the draft follows it (see renderModels). */
|
|
626
|
+
function setActiveRegion(source, region) {
|
|
627
|
+
if (source === "workbuddy") activeRegion = region;
|
|
628
|
+
else activeSourceRegion[source] = region;
|
|
629
|
+
}
|
|
630
|
+
/**
|
|
631
|
+
* Unsaved model edits, one per page (source + region).
|
|
632
|
+
*
|
|
633
|
+
* A single slot threw the edits away the moment the user looked at another page:
|
|
634
|
+
* the draft is rebuilt from the saved selection whenever the key changes, so the
|
|
635
|
+
* ticks were gone on the way back and nothing said so. Inside a slot the key is
|
|
636
|
+
* still the id set the draft was built from, so a catalog that gains or loses a
|
|
637
|
+
* model rebuilds it rather than reusing a shape it knows nothing about.
|
|
638
|
+
*/
|
|
639
|
+
var drafts = {};
|
|
640
|
+
/** model id -> { enabled, alias, window, output, images, levels, defaultLevel, open } */
|
|
641
|
+
var draft = null;
|
|
642
|
+
/** The page `draft` belongs to, so discarding drops that page's edits alone. */
|
|
643
|
+
var draftSlot = null;
|
|
644
|
+
var busy = null; // id of the control awaiting a response
|
|
645
|
+
var checkinBusy = null;
|
|
646
|
+
var doctorResult = null;
|
|
647
|
+
/** Account ids whose credit package list the user opened. Absent = collapsed. */
|
|
648
|
+
var openCredits = {};
|
|
649
|
+
/** The warning the header banner is currently showing, so it can be cleared. */
|
|
650
|
+
var headerWarning = "";
|
|
651
|
+
var reserveDrafts = {};
|
|
652
|
+
|
|
653
|
+
// ----------------------------------------------------------------- render
|
|
654
|
+
|
|
655
|
+
function regionLabel(region, short) {
|
|
656
|
+
if (region === "global") return t(short ? "region.globalShort" : "region.global");
|
|
657
|
+
return t(short ? "region.cnShort" : "region.cn");
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
function accountsOf(region) {
|
|
661
|
+
return (state && state.accounts ? state.accounts : []).filter(function (a) { return a.region === region; });
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
|
|
665
|
+
|
|
666
|
+
function modelsOf(region) {
|
|
667
|
+
return (state && state.models ? state.models : []).filter(function (m) { return m.region === region; });
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
function renderHeader() {
|
|
671
|
+
var accounts = state ? state.accounts || [] : [];
|
|
672
|
+
var cooling = accounts.filter(function (a) { return a.cooling; }).length;
|
|
673
|
+
var healthy = accounts.length > 0 && cooling < accounts.length;
|
|
674
|
+
var refreshing = state !== null && state.refreshing === true;
|
|
675
|
+
var dot = $("healthDot");
|
|
676
|
+
dot.className = "dot " + (state === null ? "idle" : (healthy ? "ok" : "idle")) + (refreshing ? " busy" : "");
|
|
677
|
+
// The progress line hangs off the fixed header, so the poll loop never
|
|
678
|
+
// restarts its animation — the class only comes and goes.
|
|
679
|
+
$("topBar").classList.toggle("refreshing", refreshing);
|
|
680
|
+
|
|
681
|
+
$("barTitle").textContent = "WTQ Pool";
|
|
682
|
+
var sub = state === null
|
|
683
|
+
? t("state.loading")
|
|
684
|
+
: t("pool.summary", { count: accounts.length, cooling: cooling });
|
|
685
|
+
// The first paint comes from cache while detection runs behind it, so say so
|
|
686
|
+
// instead of leaving the user to wonder why the numbers are not final yet.
|
|
687
|
+
if (state && state.refreshing === true) sub = sub + " · " + t("state.refreshing");
|
|
688
|
+
$("barSub").textContent = sub;
|
|
689
|
+
|
|
690
|
+
$("rescanBtn").textContent = t("btn.rescan");
|
|
691
|
+
$("resetBtn").textContent = t("btn.reset");
|
|
692
|
+
$("refreshBtn").textContent = busy === "refresh" ? t("btn.refreshing") : t("btn.refresh");
|
|
693
|
+
$("rescanBtn").disabled = busy !== null;
|
|
694
|
+
$("resetBtn").disabled = busy !== null;
|
|
695
|
+
$("refreshBtn").disabled = busy !== null;
|
|
696
|
+
// The host discovers a provider's models from this plugin's endpoint, so a
|
|
697
|
+
// changed list usually lands by itself; the banner still fires when the
|
|
698
|
+
// declaration could not be written at all, or when the saved selection no
|
|
699
|
+
// longer matches the live roster (which is why a save can look ignored).
|
|
700
|
+
//
|
|
701
|
+
// Address drift outranks both: it is the state that surfaces to the user as
|
|
702
|
+
// net::ERR_CONNECTION_REFUSED, and a model issue is invisible until it is
|
|
703
|
+
// fixed anyway.
|
|
704
|
+
var stale = state && state.staleSelectionRegions ? state.staleSelectionRegions : [];
|
|
705
|
+
var warning = state && state.addressDrift === true ? t("models.addressDrift")
|
|
706
|
+
: state && state.declaration && state.declaration.ok === false ? t("models.reload")
|
|
707
|
+
: stale.length > 0 ? t("models.staleSelection", { region: regionLabel(stale[0], false) })
|
|
708
|
+
: "";
|
|
709
|
+
// Cleared, not just set: these conditions are all fixed from outside the panel
|
|
710
|
+
// - a restart, a re-save - so a banner left standing after one of them goes
|
|
711
|
+
// away would keep naming a problem the state document no longer reports. Only
|
|
712
|
+
// the banner this pass itself last showed is cleared, so a message another
|
|
713
|
+
// action put up is not wiped by the poll landing behind it.
|
|
714
|
+
if (warning !== headerWarning) {
|
|
715
|
+
headerWarning = warning;
|
|
716
|
+
note(warning === "" ? null : warning, "warn");
|
|
717
|
+
}
|
|
718
|
+
}
|
|
719
|
+
|
|
720
|
+
/** Whether one source is switched on in the document; an absent flag means off. */
|
|
721
|
+
function sourceOn(source) {
|
|
722
|
+
return state !== null && (state.sources || {})[source] === true;
|
|
723
|
+
}
|
|
724
|
+
/** The selected source: the top-level tab, one page of every view at a time. */
|
|
725
|
+
var activeSource = "workbuddy";
|
|
726
|
+
|
|
727
|
+
/**
|
|
728
|
+
* Mirror the source tabs onto everything that is per source: the pool's three
|
|
729
|
+
* groups and the one model page. The model page reads the selected source
|
|
730
|
+
* itself (see modelPage), so switching a tab only has to name it.
|
|
731
|
+
*/
|
|
732
|
+
function renderSourceTabs() {
|
|
733
|
+
var tabs = document.querySelectorAll("#sourceTabs .tab");
|
|
734
|
+
for (var i = 0; i < tabs.length; i += 1) {
|
|
735
|
+
var on = tabs[i].getAttribute("data-source") === activeSource;
|
|
736
|
+
tabs[i].classList.toggle("active", on);
|
|
737
|
+
tabs[i].setAttribute("aria-selected", on ? "true" : "false");
|
|
738
|
+
}
|
|
739
|
+
var groups = document.querySelectorAll("#poolView .source-group");
|
|
740
|
+
for (var j = 0; j < groups.length; j += 1) {
|
|
741
|
+
groups[j].hidden = groups[j].getAttribute("data-source") !== activeSource;
|
|
742
|
+
}
|
|
743
|
+
// The source tab owns the models view too, and a source tab click does not
|
|
744
|
+
// run the whole render, so the model page and the footer - which names the
|
|
745
|
+
// endpoint of the source on screen - are repainted from here.
|
|
746
|
+
renderModelRegionTabs();
|
|
747
|
+
renderModels();
|
|
748
|
+
renderFooter();
|
|
749
|
+
}
|
|
750
|
+
|
|
751
|
+
/**
|
|
752
|
+
* Every region a source can have. The document lists only the active ones, and
|
|
753
|
+
* a parked region has to stay visible - otherwise its own switch is the only
|
|
754
|
+
* way back and it would have disappeared with the region.
|
|
755
|
+
*/
|
|
756
|
+
var SOURCE_REGIONS = { workbuddy: ["cn", "global"], trae: ["cn", "ai"], qoder: ["cn", "global"] };
|
|
757
|
+
|
|
758
|
+
/** Each source's display name, so its switch says which source it turns off. */
|
|
759
|
+
var SOURCE_TITLES = {
|
|
760
|
+
workbuddy: "workbuddy.title",
|
|
761
|
+
trae: "trae.title",
|
|
762
|
+
qoder: "qoder.title",
|
|
763
|
+
};
|
|
764
|
+
|
|
765
|
+
/** Whether the user parked one region (国内版 / 国际版) of one source. */
|
|
766
|
+
function regionParked(source, region) {
|
|
767
|
+
var parked = state && state.disabledRegions ? state.disabledRegions : [];
|
|
768
|
+
return parked.indexOf(source + ":" + region) >= 0;
|
|
769
|
+
}
|
|
770
|
+
|
|
771
|
+
/** Park or unpark one source region; the plugin stops or starts that gateway. */
|
|
772
|
+
function setRegionEnabled(source, region, enabled) {
|
|
773
|
+
return runBusy("region", function () {
|
|
774
|
+
return invoke("xd.setRegionEnabled", { source: source, region: region, enabled: enabled });
|
|
775
|
+
}).catch(function () {});
|
|
776
|
+
}
|
|
777
|
+
|
|
778
|
+
/** The 停用 / 启用 button a region carries at the bottom right of its content. */
|
|
779
|
+
function regionToggle(source, region) {
|
|
780
|
+
var parked = regionParked(source, region);
|
|
781
|
+
var name = sourceRegionLabel(region);
|
|
782
|
+
var button = el("button", "small " + (parked ? "primary" : "park"),
|
|
783
|
+
parked ? t("region.unpark", { region: name }) : t("region.park", { region: name }));
|
|
784
|
+
button.type = "button";
|
|
785
|
+
button.disabled = busy !== null;
|
|
786
|
+
button.addEventListener("click", function () { setRegionEnabled(source, region, parked); });
|
|
787
|
+
return button;
|
|
788
|
+
}
|
|
789
|
+
|
|
790
|
+
/** What a parked region shows instead of its accounts and models. */
|
|
791
|
+
function parkedRegionBlock(source, region) {
|
|
792
|
+
var block = el("div", "empty");
|
|
793
|
+
block.appendChild(el("p", null, t("region.parked", { region: sourceRegionLabel(region) })));
|
|
794
|
+
block.appendChild(regionToggle(source, region));
|
|
795
|
+
return block;
|
|
796
|
+
}
|
|
797
|
+
|
|
798
|
+
/** The bottom of one region's content: the switch that parks it. */
|
|
799
|
+
function regionFoot(source, region) {
|
|
800
|
+
var foot = el("div", "region-foot");
|
|
801
|
+
foot.appendChild(regionToggle(source, region));
|
|
802
|
+
return foot;
|
|
803
|
+
}
|
|
804
|
+
|
|
805
|
+
/**
|
|
806
|
+
* The bottom of one source's content: the switch that turns the whole source
|
|
807
|
+
* off. It sits with the region switch rather than beside the title, where one
|
|
808
|
+
* stray click switched three sources' worth of gateways off.
|
|
809
|
+
*/
|
|
810
|
+
function sourceFoot(source) {
|
|
811
|
+
var foot = el("div", "source-switch");
|
|
812
|
+
foot.appendChild(sourceToggle(source, state !== null && (state.sources || {})[source] === true));
|
|
813
|
+
return foot;
|
|
814
|
+
}
|
|
815
|
+
|
|
816
|
+
/**
|
|
817
|
+
* The region pills of one source: one entry per active region, with that
|
|
818
|
+
* region's account count. Clicking one shows that region's accounts (and, for
|
|
819
|
+
* WorkBuddy, that region's model roster).
|
|
820
|
+
*/
|
|
821
|
+
function fillRegionNav(nav, source, regions, accounts, active) {
|
|
822
|
+
clear(nav);
|
|
823
|
+
regions.forEach(function (region) {
|
|
824
|
+
var on = region === active;
|
|
825
|
+
var button = el("button", on ? "active" : "");
|
|
826
|
+
button.type = "button";
|
|
827
|
+
button.disabled = busy !== null;
|
|
828
|
+
button.setAttribute("role", "tab");
|
|
829
|
+
button.textContent = sourceRegionLabel(region);
|
|
830
|
+
if (regionParked(source, region)) button.appendChild(el("span", "muted", " " + t("region.parkedShort")));
|
|
831
|
+
var count = accounts.filter(function (account) { return account.region === region; }).length;
|
|
832
|
+
button.appendChild(el("span", "muted", " " + count));
|
|
833
|
+
button.addEventListener("click", function () {
|
|
834
|
+
setActiveRegion(source, region);
|
|
835
|
+
render();
|
|
836
|
+
});
|
|
837
|
+
nav.appendChild(button);
|
|
838
|
+
});
|
|
839
|
+
return nav;
|
|
840
|
+
}
|
|
841
|
+
|
|
842
|
+
/** A fresh region row for a page that is rebuilt on every render. */
|
|
843
|
+
function sourceRegionNav(source, regions, accounts) {
|
|
844
|
+
return fillRegionNav(el("nav", "regions"), source, regions, accounts, activeRegionOf(source));
|
|
845
|
+
}
|
|
846
|
+
|
|
847
|
+
/**
|
|
848
|
+
* Which of this source region's accounts serves its requests.
|
|
849
|
+
*
|
|
850
|
+
* Same shape as the pool's own picker: automatic, or one named account. The
|
|
851
|
+
* gateways re-read their credentials per request, so the choice applies from
|
|
852
|
+
* the next request on, without a reload.
|
|
853
|
+
*/
|
|
854
|
+
function sourcePreferredPicker(source, region, accounts) {
|
|
855
|
+
var card = el("div", "card");
|
|
856
|
+
var id = "sourceAccountChoice-" + source + "-" + region;
|
|
857
|
+
var label = el("label", "field-label", t("acct.choose"));
|
|
858
|
+
label.htmlFor = id;
|
|
859
|
+
var picker = el("select", "text-input");
|
|
860
|
+
picker.id = id;
|
|
861
|
+
picker.setAttribute("aria-label", t("acct.choose"));
|
|
862
|
+
var automatic = el("option", null, t("acct.automatic"));
|
|
863
|
+
automatic.value = "";
|
|
864
|
+
picker.appendChild(automatic);
|
|
865
|
+
accounts.forEach(function (account) {
|
|
866
|
+
var option = el("option", null, account.label);
|
|
867
|
+
option.value = account.label;
|
|
868
|
+
picker.appendChild(option);
|
|
869
|
+
});
|
|
870
|
+
var preferred = ((state[source] || {}).preferred || {})[region] || "";
|
|
871
|
+
if (preferred !== "" && !accounts.some(function (account) { return account.label === preferred; })) {
|
|
872
|
+
var missing = el("option", null, t("acct.unavailable"));
|
|
873
|
+
missing.value = preferred;
|
|
874
|
+
picker.appendChild(missing);
|
|
875
|
+
}
|
|
876
|
+
picker.value = preferred;
|
|
877
|
+
picker.disabled = busy !== null || accounts.length === 0;
|
|
878
|
+
picker.addEventListener("change", function () { setPreferredSourceAccount(source, region, picker.value); });
|
|
879
|
+
card.appendChild(label);
|
|
880
|
+
card.appendChild(picker);
|
|
881
|
+
card.appendChild(distributionControl(source));
|
|
882
|
+
return card;
|
|
883
|
+
}
|
|
884
|
+
|
|
885
|
+
/** Remember which account serves one source region; "" means automatic. */
|
|
886
|
+
function setPreferredSourceAccount(source, region, label) {
|
|
887
|
+
return runBusy("source-account", function () {
|
|
888
|
+
return invoke("xd.preferSourceAccount", { source: source, region: region, label: label });
|
|
889
|
+
}).catch(function () {});
|
|
890
|
+
}
|
|
891
|
+
function sourceRegionLabel(region) {
|
|
892
|
+
return region === "cn" ? t("region.cn") : t("region.global");
|
|
893
|
+
}
|
|
894
|
+
|
|
895
|
+
/** WorkBuddy's region row, in the pool page's own markup. */
|
|
896
|
+
function renderRegionTabs() {
|
|
897
|
+
// Every region the source has, not only the running ones: a parked region has
|
|
898
|
+
// to keep its own pill, or its switch would be unreachable.
|
|
899
|
+
var regions = sourceOn("workbuddy") ? SOURCE_REGIONS.workbuddy : [];
|
|
900
|
+
if (regions.length === 0) { activeRegion = ""; clear($("regionTabs")); return; }
|
|
901
|
+
if (regions.indexOf(activeRegion) < 0) activeRegion = regions[0];
|
|
902
|
+
fillRegionNav($("regionTabs"), "workbuddy", regions, (state && state.accounts) || [], activeRegion);
|
|
903
|
+
}
|
|
904
|
+
|
|
905
|
+
/**
|
|
906
|
+
* The selected source's region row, on the model page.
|
|
907
|
+
*
|
|
908
|
+
* The editor works on one region's roster, so the region has to be switchable
|
|
909
|
+
* where the roster is read, not only on the pool page - and it is the selected
|
|
910
|
+
* source's row, because the page now serves all three.
|
|
911
|
+
*/
|
|
912
|
+
function renderModelRegionTabs() {
|
|
913
|
+
if (!sourceOn(activeSource)) { clear($("modelRegionTabs")); return; }
|
|
914
|
+
var source = activeSource;
|
|
915
|
+
var accounts = source === "workbuddy"
|
|
916
|
+
? ((state && state.accounts) || [])
|
|
917
|
+
: (((state || {})[source] || {}).accounts || []);
|
|
918
|
+
fillRegionNav($("modelRegionTabs"), source, SOURCE_REGIONS[source], accounts, activeRegionOf(source));
|
|
919
|
+
}
|
|
920
|
+
function renderWorkbuddyHead() {
|
|
921
|
+
var host = $("workbuddyHead");
|
|
922
|
+
clear(host);
|
|
923
|
+
if (state === null) return;
|
|
924
|
+
var accounts = state.accounts || [];
|
|
925
|
+
var cooling = accounts.filter(function (account) { return account.cooling; }).length;
|
|
926
|
+
var off = accounts.filter(function (account) { return account.disabled === true; }).length;
|
|
927
|
+
host.appendChild(el("strong", null, t("workbuddy.title")));
|
|
928
|
+
host.appendChild(el("span", "muted", t("pool.summary", { count: accounts.length, cooling: cooling })));
|
|
929
|
+
|
|
930
|
+
// Collecting every reward is the reason a multi-account pool exists, so it is
|
|
931
|
+
// one button rather than a click per account card.
|
|
932
|
+
var claimable = accounts.filter(function (account) {
|
|
933
|
+
return account.disabled !== true && account.checkin && account.checkin.active === true
|
|
934
|
+
&& account.checkin.todayCheckedIn !== true;
|
|
935
|
+
}).length;
|
|
936
|
+
var allButton = el("button", "ghost small checkin-all ic-calendar" + (checkinBusy === "all" ? " is-busy" : ""),
|
|
937
|
+
checkinBusy === "all" ? t("pool.checkinAllBusy") : t("pool.checkinAll"));
|
|
938
|
+
allButton.type = "button";
|
|
939
|
+
allButton.title = t("pool.checkinAllHint");
|
|
940
|
+
allButton.disabled = busy !== null || checkinBusy !== null || claimable === 0;
|
|
941
|
+
allButton.addEventListener("click", claimAllCheckins);
|
|
942
|
+
host.appendChild(allButton);
|
|
943
|
+
|
|
944
|
+
if (off > 0) host.appendChild(el("span", "muted", t("pool.switchedOff", { count: off })));
|
|
945
|
+
}
|
|
946
|
+
|
|
947
|
+
/** The pool's health line, minus the head row's summary and check-in button. */
|
|
948
|
+
function renderPoolStrip() {
|
|
949
|
+
var strip = $("poolStrip");
|
|
950
|
+
clear(strip);
|
|
951
|
+
var accounts = accountsOf(activeRegion);
|
|
952
|
+
var cooling = accounts.filter(function (a) { return a.cooling; }).length;
|
|
953
|
+
var off = accounts.filter(function (a) { return a.disabled === true; }).length;
|
|
954
|
+
// Only the accounts that can serve are counted: a switched-off account is not
|
|
955
|
+
// rate-limited, so counting it here would read "every account is rate-limited"
|
|
956
|
+
// over a row that also said "1 switched off". A pool whose serving accounts are
|
|
957
|
+
// all cooling really does pause, and one with nothing switched on says so on
|
|
958
|
+
// the line below rather than here.
|
|
959
|
+
var serving = accounts.length - off;
|
|
960
|
+
var noteText = accounts.length === 0
|
|
961
|
+
? ""
|
|
962
|
+
: (serving > 0 && cooling >= serving ? t("pool.allCooling") : t("pool.healthy"));
|
|
963
|
+
if (noteText) strip.appendChild(el("span", "muted", noteText));
|
|
964
|
+
if (off > 0) strip.appendChild(el("span", "muted", t("pool.switchedOff", { count: off })));
|
|
965
|
+
}
|
|
966
|
+
|
|
967
|
+
/**
|
|
968
|
+
* The button that turns a whole source off.
|
|
969
|
+
*
|
|
970
|
+
* It sits at the bottom of that source's content rather than beside the title,
|
|
971
|
+
* where a stray click cannot switch the gateways off, and it names the source so
|
|
972
|
+
* it cannot be read as a region's switch.
|
|
973
|
+
*/
|
|
974
|
+
function sourceToggle(source, on) {
|
|
975
|
+
var label = t(on ? "source.disable" : "source.enable", { source: t(SOURCE_TITLES[source]) });
|
|
976
|
+
var button = el("button", "small " + (on ? "park" : "primary"), label);
|
|
977
|
+
button.type = "button";
|
|
978
|
+
button.setAttribute("aria-pressed", on ? "true" : "false");
|
|
979
|
+
button.disabled = busy !== null;
|
|
980
|
+
button.addEventListener("click", function () { setSource(source, !on); });
|
|
981
|
+
return button;
|
|
982
|
+
}
|
|
983
|
+
|
|
984
|
+
/** Shared head row for one non-pool source: its name and its own check-in pool. */
|
|
985
|
+
function sourceGroupHead(source, titleKey, count, claimable, busyHere) {
|
|
986
|
+
var head = el("div", "source-head");
|
|
987
|
+
head.appendChild(el("strong", null, t(titleKey)));
|
|
988
|
+
head.appendChild(el("span", "muted", t("source.accountCount", { count: count })));
|
|
989
|
+
var allButton = el("button", "ghost small checkin-all ic-calendar" + (busyHere ? " is-busy" : ""),
|
|
990
|
+
busyHere ? t("pool.checkinAllBusy") : t("pool.checkinAll"));
|
|
991
|
+
allButton.type = "button";
|
|
992
|
+
allButton.title = t("pool.checkinAllHint");
|
|
993
|
+
allButton.disabled = busy !== null || checkinBusy !== null || claimable === 0
|
|
994
|
+
|| (state.sources || {})[source] !== true;
|
|
995
|
+
allButton.addEventListener("click", function () { claimSourceCheckins(source); });
|
|
996
|
+
head.appendChild(allButton);
|
|
997
|
+
return head;
|
|
998
|
+
}
|
|
999
|
+
|
|
1000
|
+
/** One non-pool account card: its own quota figure and today's check-in. */
|
|
1001
|
+
function sourceAccountCard(account, kind) {
|
|
1002
|
+
var card = el("div", "card account" + (account.expired ? " card-off" : ""));
|
|
1003
|
+
|
|
1004
|
+
var head = el("div", "card-head");
|
|
1005
|
+
head.appendChild(el("span", "acct-label", account.label));
|
|
1006
|
+
if (account.email) head.appendChild(el("span", "muted", account.email));
|
|
1007
|
+
// Which app this sign-in came from: one Trae account keeps a copy in every
|
|
1008
|
+
// app it was signed into, and the scanner folds them into a single row.
|
|
1009
|
+
if (account.app) head.appendChild(el("span", "muted", account.app));
|
|
1010
|
+
head.appendChild(el("span", "region-tag", kind === "trae"
|
|
1011
|
+
? (account.region === "ai" ? t("region.global") : t("region.cn"))
|
|
1012
|
+
: (account.region === "global" ? t("region.global") : t("region.cn"))));
|
|
1013
|
+
head.appendChild(el("span", "badge " + (account.expired ? "off" : "ok"),
|
|
1014
|
+
account.expired ? t("source.expired") : t("badge.healthy")));
|
|
1015
|
+
card.appendChild(head);
|
|
1016
|
+
|
|
1017
|
+
var meta = el("div", "model-meta");
|
|
1018
|
+
// Not beside a badge that already says the token is expired.
|
|
1019
|
+
if (account.expired !== true) meta.appendChild(tokenChip(account.expiresAt));
|
|
1020
|
+
card.appendChild(meta);
|
|
1021
|
+
|
|
1022
|
+
// Quota reads like the pool's own card and is drawn by the same code: every
|
|
1023
|
+
// credit pool is a row with its own expiry, which is the only place the
|
|
1024
|
+
// check-in reward's date exists. Trae's international region bills by plan
|
|
1025
|
+
// rather than by credits, so its card shows the plan name instead.
|
|
1026
|
+
var usage = account.usage || {};
|
|
1027
|
+
if (usage.credits) {
|
|
1028
|
+
// `keepEmpty`: a source account shows every pool the route named — Qoder's
|
|
1029
|
+
// 0/0 plan included, because that row is the one that says it never
|
|
1030
|
+
// expires. The expansion is the same one the pool's cards have.
|
|
1031
|
+
card.appendChild(creditsBlock(usage.credits, sourceCheckinKey(kind, account), { keepEmpty: true }));
|
|
1032
|
+
} else {
|
|
1033
|
+
var plan = usage.plan || null;
|
|
1034
|
+
var quota = el("div", "credits");
|
|
1035
|
+
var quotaHead = el("div", "credits-head static");
|
|
1036
|
+
quotaHead.appendChild(el("span", "card-title", t("source.planTitle")));
|
|
1037
|
+
if (plan && plan.name) {
|
|
1038
|
+
quotaHead.appendChild(el("span", "credit-total", plan.name));
|
|
1039
|
+
if (plan.inTrial) quotaHead.appendChild(el("span", "badge free", t("source.planTrial")));
|
|
1040
|
+
} else {
|
|
1041
|
+
quotaHead.appendChild(el("span", "muted", t("source.quotaUnknown")));
|
|
1042
|
+
}
|
|
1043
|
+
quota.appendChild(quotaHead);
|
|
1044
|
+
card.appendChild(quota);
|
|
1045
|
+
}
|
|
1046
|
+
|
|
1047
|
+
var checkin = account.checkin;
|
|
1048
|
+
if (checkin) {
|
|
1049
|
+
var row = el("div", "checkin");
|
|
1050
|
+
row.appendChild(el("span", "card-title", t("checkin.title")));
|
|
1051
|
+
// Trae allows one check-in per device per day, so an account can be unclaimed
|
|
1052
|
+
// while this machine's turn is already spent: the badge and the button then
|
|
1053
|
+
// say so instead of offering a click the upstream will refuse (9095).
|
|
1054
|
+
var doneToday = checkin.todayCheckedIn === true;
|
|
1055
|
+
var deviceDone = doneToday !== true && checkin.deviceCheckedIn === true;
|
|
1056
|
+
// Qoder issues its daily round at 10:00 Beijing, so before then the card
|
|
1057
|
+
// names the hour instead of claiming there is no check-in at all.
|
|
1058
|
+
var opensLabel = doneToday || deviceDone || typeof checkin.opensAtHour !== "number"
|
|
1059
|
+
? ""
|
|
1060
|
+
: t("source.claimableAt", { time: clockHour(checkin.opensAtHour) });
|
|
1061
|
+
var badgeClass = "badge " + (doneToday ? "free" : deviceDone || opensLabel !== "" ? "next" : checkin.active ? "next" : "off");
|
|
1062
|
+
row.appendChild(el("span", badgeClass, doneToday
|
|
1063
|
+
? t("source.checkedIn")
|
|
1064
|
+
: deviceDone ? t("source.deviceCheckedIn")
|
|
1065
|
+
: opensLabel !== "" ? opensLabel
|
|
1066
|
+
: checkin.active ? t("source.claimable") : t("source.noCampaign")));
|
|
1067
|
+
if (!doneToday && checkin.active && checkin.amount > 0) {
|
|
1068
|
+
row.appendChild(el("span", "muted", checkin.amount + " "
|
|
1069
|
+
+ (checkin.unit === undefined || checkin.unit === "credits" ? t("credits.keepUnit") : checkin.unit)));
|
|
1070
|
+
}
|
|
1071
|
+
// The per-account claim the pool's own cards have; the source-wide button in
|
|
1072
|
+
// the head stays the one-click path for every account of this source.
|
|
1073
|
+
var busyHere = checkinBusy === sourceCheckinKey(kind, account);
|
|
1074
|
+
var idleLabel = doneToday ? t("btn.checkinDone")
|
|
1075
|
+
: deviceDone ? t("source.deviceCheckedIn")
|
|
1076
|
+
: opensLabel !== "" ? opensLabel
|
|
1077
|
+
: t("btn.checkin");
|
|
1078
|
+
var claim = el("button", "primary small" + (busyHere ? " is-busy" : ""),
|
|
1079
|
+
busyHere ? t("checkin.claiming") : idleLabel);
|
|
1080
|
+
claim.type = "button";
|
|
1081
|
+
claim.disabled = busyHere || checkinBusy !== null || busy !== null
|
|
1082
|
+
|| checkin.active !== true || doneToday || deviceDone || opensLabel !== "";
|
|
1083
|
+
claim.addEventListener("click", function () { claimSourceCheckin(kind, account); });
|
|
1084
|
+
row.appendChild(claim);
|
|
1085
|
+
card.appendChild(row);
|
|
1086
|
+
}
|
|
1087
|
+
return card;
|
|
1088
|
+
}
|
|
1089
|
+
|
|
1090
|
+
/** One Trae/Qoder gateway row flattened for the model pickers. */
|
|
1091
|
+
function traeQoderGatewayRows() {
|
|
1092
|
+
var out = [];
|
|
1093
|
+
var trae = (state && state.trae) || {};
|
|
1094
|
+
var qoder = (state && state.qoder) || {};
|
|
1095
|
+
(Array.isArray(trae.gateways) ? trae.gateways : []).forEach(function (gateway) {
|
|
1096
|
+
out.push({ source: "trae", key: "trae:" + gateway.region, region: gateway.region, port: gateway.port,
|
|
1097
|
+
roster: Array.isArray(gateway.roster) ? gateway.roster : [], selection: gateway.selection || null, lastError: gateway.lastError });
|
|
1098
|
+
});
|
|
1099
|
+
(Array.isArray(qoder.gateways) ? qoder.gateways : []).forEach(function (gateway) {
|
|
1100
|
+
out.push({ source: "qoder", key: "qoder:" + gateway.region, region: gateway.region, port: gateway.port,
|
|
1101
|
+
roster: Array.isArray(gateway.roster) ? gateway.roster : [], selection: gateway.selection || null, lastError: gateway.lastError });
|
|
1102
|
+
});
|
|
1103
|
+
return out;
|
|
1104
|
+
}
|
|
1105
|
+
|
|
1106
|
+
/** The one gateway row serving a source's region; null when it has none. */
|
|
1107
|
+
function sourceGatewayRow(source, region) {
|
|
1108
|
+
var rows = traeQoderGatewayRows().filter(function (row) {
|
|
1109
|
+
return row.source === source && row.region === region;
|
|
1110
|
+
});
|
|
1111
|
+
return rows.length > 0 ? rows[0] : null;
|
|
1112
|
+
}
|
|
1113
|
+
|
|
1114
|
+
/** One gateway's column title: its region, then the port it serves on. */
|
|
1115
|
+
function gatewayLabel(row) {
|
|
1116
|
+
var region = row.source === "trae"
|
|
1117
|
+
? (row.region === "ai" ? t("region.global") : t("region.cn"))
|
|
1118
|
+
: (row.region === "global" ? t("region.global") : t("region.cn"));
|
|
1119
|
+
return region + " · " + row.port;
|
|
1120
|
+
}
|
|
1121
|
+
|
|
1122
|
+
/**
|
|
1123
|
+
* What the model page is editing: the selected source's selected region.
|
|
1124
|
+
*
|
|
1125
|
+
* One page serves all three sources, so everything that differs between them
|
|
1126
|
+
* is answered here rather than inside the renderers - which roster the left
|
|
1127
|
+
* column lists, what is stored for it, which key the save writes - and Trae
|
|
1128
|
+
* and Qoder get the pool's own editor instead of a second copy of it.
|
|
1129
|
+
*/
|
|
1130
|
+
function modelPage() {
|
|
1131
|
+
var source = activeSource;
|
|
1132
|
+
// The plugin reports how many models one provider may declare, so the panel
|
|
1133
|
+
// never keeps a number of its own that a later change could disagree with.
|
|
1134
|
+
var cap = Number(state && state.modelCap) > 0 ? Number(state.modelCap) : 0;
|
|
1135
|
+
if (source === "workbuddy") {
|
|
1136
|
+
return {
|
|
1137
|
+
source: source,
|
|
1138
|
+
region: activeRegion,
|
|
1139
|
+
label: t("models.serviceModels"),
|
|
1140
|
+
list: modelsOf(activeRegion),
|
|
1141
|
+
selection: ((state || {}).selectionByRegion || {})[activeRegion] || {},
|
|
1142
|
+
key: activeRegion,
|
|
1143
|
+
error: "",
|
|
1144
|
+
cap: cap,
|
|
1145
|
+
};
|
|
1146
|
+
}
|
|
1147
|
+
var row = sourceGatewayRow(source, activeRegionOf(source));
|
|
1148
|
+
if (row === null) {
|
|
1149
|
+
return { source: source, region: activeRegionOf(source), label: "", list: [], selection: {}, key: "", error: "", cap: cap };
|
|
1150
|
+
}
|
|
1151
|
+
return {
|
|
1152
|
+
source: source,
|
|
1153
|
+
region: row.region,
|
|
1154
|
+
label: gatewayLabel(row),
|
|
1155
|
+
list: row.roster,
|
|
1156
|
+
selection: row.selection || {},
|
|
1157
|
+
key: row.key,
|
|
1158
|
+
error: row.lastError || "",
|
|
1159
|
+
cap: cap,
|
|
1160
|
+
};
|
|
1161
|
+
}
|
|
1162
|
+
|
|
1163
|
+
/**
|
|
1164
|
+
* Per-source strings and rules for the two non-pool sections.
|
|
1165
|
+
*
|
|
1166
|
+
* Trae and Qoder differ only in their wording and in which regions have a daily
|
|
1167
|
+
* check-in, so they share one renderer instead of two copies kept in step.
|
|
1168
|
+
*/
|
|
1169
|
+
var SOURCE_SECTIONS = {
|
|
1170
|
+
trae: { title: "trae.title", off: "trae.off", gateway: "trae.gateway", models: "trae.models",
|
|
1171
|
+
noModels: "trae.noModels", fallback: "trae.fallback", checkinRegions: ["cn"],
|
|
1172
|
+
note: "source.traeSlots", checkinNote: "source.traeCheckin" },
|
|
1173
|
+
qoder: { title: "qoder.title", off: "qoder.off", gateway: "qoder.gateway", models: "qoder.models",
|
|
1174
|
+
noModels: "qoder.noModels", fallback: "qoder.fallback", checkinRegions: ["cn", "global"],
|
|
1175
|
+
note: "source.qoderSlots" },
|
|
1176
|
+
};
|
|
1177
|
+
|
|
1178
|
+
/**
|
|
1179
|
+
* One non-pool source's section: its region row, that region's account cards
|
|
1180
|
+
* with their own quota and check-in, then that region's gateway, and at the
|
|
1181
|
+
* bottom the switches - the region's own, and the source's.
|
|
1182
|
+
*/
|
|
1183
|
+
function renderSourceStrip(source) {
|
|
1184
|
+
var host = $(source + "Strip");
|
|
1185
|
+
clear(host);
|
|
1186
|
+
if (state === null) return;
|
|
1187
|
+
var section = SOURCE_SECTIONS[source];
|
|
1188
|
+
var info = state[source] || {};
|
|
1189
|
+
var gateways = Array.isArray(info.gateways) ? info.gateways : [];
|
|
1190
|
+
var accounts = Array.isArray(info.accounts) ? info.accounts : [];
|
|
1191
|
+
var regions = SOURCE_REGIONS[source];
|
|
1192
|
+
var on = sourceOn(source);
|
|
1193
|
+
var claimable = accounts.filter(function (account) {
|
|
1194
|
+
return !account.expired && section.checkinRegions.indexOf(account.region) >= 0 && account.checkin
|
|
1195
|
+
&& account.checkin.active === true && account.checkin.todayCheckedIn !== true
|
|
1196
|
+
// A device that already claimed today leaves the other accounts nothing
|
|
1197
|
+
// to collect, so they must not be counted here either - the single
|
|
1198
|
+
// account rows grey their button out for the same reason.
|
|
1199
|
+
&& account.checkin.deviceCheckedIn !== true;
|
|
1200
|
+
}).length;
|
|
1201
|
+
host.appendChild(sourceGroupHead(source, section.title, accounts.length, on ? claimable : 0, checkinBusy === source));
|
|
1202
|
+
if (!on) {
|
|
1203
|
+
host.appendChild(el("p", "hint", t(section.off)));
|
|
1204
|
+
// The switch stays here while the source is off, because that is the only
|
|
1205
|
+
// state in which it is needed.
|
|
1206
|
+
host.appendChild(sourceFoot(source));
|
|
1207
|
+
return;
|
|
1208
|
+
}
|
|
1209
|
+
if (regions.indexOf(activeRegionOf(source)) < 0) setActiveRegion(source, regions[0]);
|
|
1210
|
+
var region = activeRegionOf(source);
|
|
1211
|
+
host.appendChild(sourceRegionNav(source, regions, accounts));
|
|
1212
|
+
// One short line about this source's own sign-in rules, where the accounts are.
|
|
1213
|
+
if (section.note) host.appendChild(el("p", "hint", t(section.note)));
|
|
1214
|
+
if (section.checkinNote) host.appendChild(el("p", "hint", t(section.checkinNote)));
|
|
1215
|
+
if (regionParked(source, region)) {
|
|
1216
|
+
host.appendChild(parkedRegionBlock(source, region));
|
|
1217
|
+
host.appendChild(sourceFoot(source));
|
|
1218
|
+
return;
|
|
1219
|
+
}
|
|
1220
|
+
|
|
1221
|
+
var mine = accounts.filter(function (account) { return account.region === region; });
|
|
1222
|
+
// Which account serves this region: the pool has the same control, so a source
|
|
1223
|
+
// with more than one sign-in in a region is not stuck with the first one.
|
|
1224
|
+
host.appendChild(sourcePreferredPicker(source, region, mine));
|
|
1225
|
+
if (mine.length === 0) host.appendChild(el("p", "hint muted", t("source.noAccounts")));
|
|
1226
|
+
if (mine.length > 0) {
|
|
1227
|
+
var list = el("div", "source-accounts");
|
|
1228
|
+
mine.forEach(function (account) { list.appendChild(sourceAccountCard(account, source)); });
|
|
1229
|
+
host.appendChild(list);
|
|
1230
|
+
}
|
|
1231
|
+
|
|
1232
|
+
// Only this region's gateway: the other one sits behind its own region tab.
|
|
1233
|
+
var shown = gateways.filter(function (gateway) { return gateway.region === region; });
|
|
1234
|
+
if (shown.length > 0) {
|
|
1235
|
+
var foot = el("div", "source-foot");
|
|
1236
|
+
shown.forEach(function (gateway) {
|
|
1237
|
+
var parts = [t(section.gateway, { region: sourceRegionLabel(gateway.region), port: gateway.port }),
|
|
1238
|
+
gateway.models > 0 ? t(section.models, { count: gateway.models }) : t(section.noModels)];
|
|
1239
|
+
if (gateway.portFallback) parts.push(t(section.fallback, { port: gateway.port }));
|
|
1240
|
+
if (gateway.lastError) parts.push(gateway.lastError);
|
|
1241
|
+
foot.appendChild(el("span", "muted", parts.join(" · ")));
|
|
1242
|
+
});
|
|
1243
|
+
host.appendChild(foot);
|
|
1244
|
+
}
|
|
1245
|
+
// The region's own switch sits at the bottom of its content.
|
|
1246
|
+
host.appendChild(regionFoot(source, region));
|
|
1247
|
+
host.appendChild(sourceFoot(source));
|
|
1248
|
+
}
|
|
1249
|
+
|
|
1250
|
+
/** Collect today's reward for just one source's pool. */
|
|
1251
|
+
function claimSourceCheckins(source) {
|
|
1252
|
+
if (busy !== null || checkinBusy !== null) return Promise.resolve();
|
|
1253
|
+
checkinBusy = source;
|
|
1254
|
+
render();
|
|
1255
|
+
return invoke("xd.checkinAll", { source: source }).then(function (payload) {
|
|
1256
|
+
checkinBusy = null;
|
|
1257
|
+
if (payload && payload.state) adopt(payload.state);
|
|
1258
|
+
note(null);
|
|
1259
|
+
render();
|
|
1260
|
+
if (payload && payload.running === true) {
|
|
1261
|
+
// The pass outlived the plugin's answer and is still collecting: the
|
|
1262
|
+
// command palette reports it when it lands.
|
|
1263
|
+
announce(t("pool.checkinAllBusy"));
|
|
1264
|
+
return;
|
|
1265
|
+
}
|
|
1266
|
+
var counts = payload ? payload[source] : null;
|
|
1267
|
+
var claimed = counts && typeof counts.claimed === "number" ? counts.claimed : 0;
|
|
1268
|
+
announce(claimed > 0 ? t("pool.checkinAllDone", { count: claimed }) : t("pool.checkinAllNone"));
|
|
1269
|
+
}).catch(function (error) {
|
|
1270
|
+
checkinBusy = null;
|
|
1271
|
+
note(String(error && error.message ? error.message : error));
|
|
1272
|
+
render();
|
|
1273
|
+
});
|
|
1274
|
+
}
|
|
1275
|
+
|
|
1276
|
+
/** The busy key of one source account row: source, region and label together. */
|
|
1277
|
+
function sourceCheckinKey(source, account) {
|
|
1278
|
+
return source + ":" + account.region + ":" + account.label;
|
|
1279
|
+
}
|
|
1280
|
+
|
|
1281
|
+
/**
|
|
1282
|
+
* Collect one Trae/Qoder account's reward.
|
|
1283
|
+
*
|
|
1284
|
+
* The row names the account (source, region, label); the plugin re-reads the
|
|
1285
|
+
* account's own status before claiming, so a second click cannot collect twice.
|
|
1286
|
+
*/
|
|
1287
|
+
function claimSourceCheckin(source, account) {
|
|
1288
|
+
if (busy !== null || checkinBusy !== null) return Promise.resolve();
|
|
1289
|
+
checkinBusy = sourceCheckinKey(source, account);
|
|
1290
|
+
render();
|
|
1291
|
+
return invoke("xd.sourceCheckin", { source: source, label: account.label, region: account.region })
|
|
1292
|
+
.then(function (payload) {
|
|
1293
|
+
checkinBusy = null;
|
|
1294
|
+
if (payload && payload.state) adopt(payload.state);
|
|
1295
|
+
var result = payload && payload.result ? payload.result : {};
|
|
1296
|
+
// A skipped claim can carry the upstream's own reason (Trae answers 9095
|
|
1297
|
+
// "this device already checked in today"), which is worth reading.
|
|
1298
|
+
if (result.outcome === "claimed") announce(t("source.checkinDone"));
|
|
1299
|
+
else if (result.deviceCheckedIn === true) announce(t("source.deviceCheckedIn"));
|
|
1300
|
+
else announce(result.reason || t("source.checkinSkipped"));
|
|
1301
|
+
render();
|
|
1302
|
+
}).catch(function (error) {
|
|
1303
|
+
checkinBusy = null;
|
|
1304
|
+
note(String(error && error.message ? error.message : error));
|
|
1305
|
+
render();
|
|
1306
|
+
});
|
|
1307
|
+
}
|
|
1308
|
+
|
|
1309
|
+
/**
|
|
1310
|
+
* The account-usage control of one source, built into that source's account
|
|
1311
|
+
* picker so the two choices sit together: which account serves this region,
|
|
1312
|
+
* and how requests spread while that choice is automatic.
|
|
1313
|
+
*
|
|
1314
|
+
* WorkBuddy's pool offers four modes; a Trae/Qoder gateway only reads an
|
|
1315
|
+
* ordered credential list, so those two offer the modes their ordering can
|
|
1316
|
+
* honour.
|
|
1317
|
+
*/
|
|
1318
|
+
function distributionControl(source) {
|
|
1319
|
+
var block = el("div", "dist-inline");
|
|
1320
|
+
block.appendChild(el("div", "field-label", t("dist.title")));
|
|
1321
|
+
var workbuddy = source === "workbuddy";
|
|
1322
|
+
var modes = workbuddy
|
|
1323
|
+
? [["priority", "dist.priority"], ["round-robin", "dist.roundRobin"], ["expiry", "dist.expiry"], ["sticky", "dist.sticky"]]
|
|
1324
|
+
: [["priority", "dist.priority"], ["round-robin", "dist.roundRobin"], ["expiry", "dist.expiry"]];
|
|
1325
|
+
var current = (workbuddy
|
|
1326
|
+
? (state && state.distribution)
|
|
1327
|
+
: (state && state[source] && state[source].distribution)) || "priority";
|
|
1328
|
+
var seg = el("div", "seg");
|
|
1329
|
+
modes.forEach(function (pair) {
|
|
1330
|
+
var button = el("button", current === pair[0] ? "active" : "", t(pair[1]));
|
|
1331
|
+
button.type = "button";
|
|
1332
|
+
button.id = "distribution-" + source + "-" + pair[0];
|
|
1333
|
+
button.setAttribute("aria-pressed", current === pair[0] ? "true" : "false");
|
|
1334
|
+
button.disabled = busy !== null;
|
|
1335
|
+
button.addEventListener("click", function () { setDistribution(source, pair[0]); });
|
|
1336
|
+
seg.appendChild(button);
|
|
1337
|
+
});
|
|
1338
|
+
block.appendChild(seg);
|
|
1339
|
+
var hintKeys = workbuddy
|
|
1340
|
+
? { "round-robin": "dist.hintRoundRobin", expiry: "dist.hintExpiry", sticky: "dist.hintSticky" }
|
|
1341
|
+
: { "round-robin": "dist.hintSourceRoundRobin", expiry: "dist.hintSourceExpiry" };
|
|
1342
|
+
block.appendChild(el("p", "hint", t(hintKeys[current] || (workbuddy ? "dist.hintPriority" : "dist.hintSourcePriority"))));
|
|
1343
|
+
return block;
|
|
1344
|
+
}
|
|
1345
|
+
|
|
1346
|
+
|
|
1347
|
+
/**
|
|
1348
|
+
* The daily automatic check-in: one switch, one list of hours, one status line.
|
|
1349
|
+
*
|
|
1350
|
+
* The hours box is left alone while it has focus, so a state refresh (the panel
|
|
1351
|
+
* polls while a background pass runs) never eats what is being typed.
|
|
1352
|
+
*
|
|
1353
|
+
* The on/off buttons are static nodes for a sharper reason: rebuilt on every
|
|
1354
|
+
* render, a button destroyed while it holds focus loses it, and the browser then
|
|
1355
|
+
* scrolls the replacement into view — the shake every click produced. So they are
|
|
1356
|
+
* painted once here and only their state is updated.
|
|
1357
|
+
*/
|
|
1358
|
+
function renderAutoCheckin() {
|
|
1359
|
+
$("autoTitle").textContent = t("auto.title");
|
|
1360
|
+
var auto = (state && state.autoCheckin) || { enabled: false, hours: [9] };
|
|
1361
|
+
$("autoState").textContent = auto.enabled === true ? t("auto.on") : t("auto.off");
|
|
1362
|
+
var host = $("autoControl");
|
|
1363
|
+
// Built once; afterwards only the state moves.
|
|
1364
|
+
if (host.childElementCount === 0) {
|
|
1365
|
+
[[true, "auto.on"], [false, "auto.off"]].forEach(function (pair) {
|
|
1366
|
+
var button = el("button", "", t(pair[1]));
|
|
1367
|
+
button.type = "button";
|
|
1368
|
+
button.setAttribute("data-auto", String(pair[0]));
|
|
1369
|
+
button.addEventListener("click", function () {
|
|
1370
|
+
setAutoCheckin(pair[0], parseCheckinHours($("autoHours").value));
|
|
1371
|
+
});
|
|
1372
|
+
host.appendChild(button);
|
|
1373
|
+
});
|
|
1374
|
+
}
|
|
1375
|
+
Array.prototype.forEach.call(host.children, function (button) {
|
|
1376
|
+
var on = button.getAttribute("data-auto") === "true";
|
|
1377
|
+
// The wording is repainted here rather than only at creation: these nodes
|
|
1378
|
+
// survive a language switch, and text set once would stay in the old one.
|
|
1379
|
+
button.textContent = t(on ? "auto.on" : "auto.off");
|
|
1380
|
+
button.className = auto.enabled === on ? "active" : "";
|
|
1381
|
+
button.setAttribute("aria-pressed", auto.enabled === on ? "true" : "false");
|
|
1382
|
+
button.disabled = busy !== null;
|
|
1383
|
+
});
|
|
1384
|
+
|
|
1385
|
+
$("autoHoursLabel").textContent = t("auto.hours");
|
|
1386
|
+
var input = $("autoHours");
|
|
1387
|
+
input.placeholder = t("auto.hoursHint");
|
|
1388
|
+
if (document.activeElement !== input) input.value = (auto.hours || []).join(",");
|
|
1389
|
+
var save = $("autoHoursSave");
|
|
1390
|
+
save.textContent = t("auto.hoursSave");
|
|
1391
|
+
save.disabled = busy !== null;
|
|
1392
|
+
// Not bound here: this button is a static node, so a listener added on every
|
|
1393
|
+
// render would outlive it and stack up - one click firing one request per
|
|
1394
|
+
// render the panel had ever drawn. The handler reads the hours field when it
|
|
1395
|
+
// runs, so binding it once in `bind` is enough.
|
|
1396
|
+
var run = $("autoRunNow");
|
|
1397
|
+
run.textContent = busy === "checkin" ? t("auto.running") : t("auto.runNow");
|
|
1398
|
+
run.title = t("auto.runNowHint");
|
|
1399
|
+
run.disabled = busy !== null;
|
|
1400
|
+
$("autoHint").textContent = t("auto.hint");
|
|
1401
|
+
paintAutoNote($("autoNote"), auto);
|
|
1402
|
+
}
|
|
1403
|
+
|
|
1404
|
+
/**
|
|
1405
|
+
* The composer readout switch, on its own card.
|
|
1406
|
+
*
|
|
1407
|
+
* A button rather than a checkbox: the button names the action it performs, so
|
|
1408
|
+
* "显示积分" while it is off and "隐藏积分" while it is on — which says both
|
|
1409
|
+
* what pressing it will do and which way it currently stands, and leaves no
|
|
1410
|
+
* second label in the card head saying the same thing again.
|
|
1411
|
+
*
|
|
1412
|
+
* Only painted here. The click handler lives in `bind`, because the button is a
|
|
1413
|
+
* static node: a listener added on every render would outlive the render that
|
|
1414
|
+
* added it and stack up, one request per pass the panel had ever drawn.
|
|
1415
|
+
*/
|
|
1416
|
+
function renderComposerPoints() {
|
|
1417
|
+
var on = state !== null && state.showComposerPoints === true;
|
|
1418
|
+
$("composerTitle").textContent = t("composer.title");
|
|
1419
|
+
$("composerState").textContent = on ? t("composer.on") : t("composer.off");
|
|
1420
|
+
var button = $("showComposerPoints");
|
|
1421
|
+
button.textContent = t(on ? "composer.hide" : "composer.show");
|
|
1422
|
+
button.className = "small" + (on ? " on" : "");
|
|
1423
|
+
button.disabled = busy !== null;
|
|
1424
|
+
$("composerPointsText").textContent = t("composer.label");
|
|
1425
|
+
$("composerPointsHint").textContent = t("composer.hint");
|
|
1426
|
+
}
|
|
1427
|
+
|
|
1428
|
+
/**
|
|
1429
|
+
* The check-in log: one row per source, each with a state dot.
|
|
1430
|
+
*
|
|
1431
|
+
* A single line with separators was unreadable once three sources reported, so
|
|
1432
|
+
* each source gets its own row. The dot carries the outcome at a glance — green
|
|
1433
|
+
* when something was collected, amber when it was skipped, red when it failed —
|
|
1434
|
+
* and the numbers stay on the row for anyone who wants them.
|
|
1435
|
+
*
|
|
1436
|
+
* A manual run is marked as one: the scheduled pass fires at an hour nobody is
|
|
1437
|
+
* watching, so the row says which kind of run it is describing.
|
|
1438
|
+
*/
|
|
1439
|
+
function autoNoteRows(auto) {
|
|
1440
|
+
var last = auto.last;
|
|
1441
|
+
if (!last) return [{ text: t("auto.neverRun"), tone: "" }];
|
|
1442
|
+
// Opening stub: sources have not answered yet. A finished "0 accounts" row
|
|
1443
|
+
// here is what "run now" painted and then never replaced.
|
|
1444
|
+
if (last.pending === true && last.travel == null && last.qoder == null && last.trae == null) {
|
|
1445
|
+
return [{ text: t("auto.running"), tone: "" }];
|
|
1446
|
+
}
|
|
1447
|
+
var slot = String(last.slot).replace("T", " ");
|
|
1448
|
+
if (last.error) return [{ text: t("auto.lastFailedAt", { slot: slot, message: last.error }), tone: "fail" }];
|
|
1449
|
+
var rows = [];
|
|
1450
|
+
if (last.pending === true) rows.push({ text: t("auto.running"), tone: "" });
|
|
1451
|
+
rows.push({
|
|
1452
|
+
text: t(last.manual === true ? "auto.lastManual" : "auto.lastAuto",
|
|
1453
|
+
{ slot: slot, count: Number(last.claimed) || 0 }),
|
|
1454
|
+
tone: last.pending === true ? "" : "ok",
|
|
1455
|
+
});
|
|
1456
|
+
var push = function (label, counts) {
|
|
1457
|
+
if (!counts) return;
|
|
1458
|
+
var claimed = Number(counts.claimed) || 0;
|
|
1459
|
+
var failed = Number(counts.failed) || 0;
|
|
1460
|
+
var skipped = Number(counts.skipped) || 0;
|
|
1461
|
+
rows.push({
|
|
1462
|
+
text: t("auto.rowCounts", {
|
|
1463
|
+
label: label,
|
|
1464
|
+
claimed: claimed,
|
|
1465
|
+
skipped: skipped,
|
|
1466
|
+
failed: failed,
|
|
1467
|
+
}),
|
|
1468
|
+
tone: failed > 0 ? "fail" : claimed > 0 ? "ok" : skipped > 0 ? "skip" : "",
|
|
1469
|
+
});
|
|
1470
|
+
};
|
|
1471
|
+
var pushTravel = function (counts) {
|
|
1472
|
+
if (!counts) return;
|
|
1473
|
+
var claimed = Number(counts.claimed) || 0;
|
|
1474
|
+
var adopted = Number(counts.adopted) || 0;
|
|
1475
|
+
rows.push({
|
|
1476
|
+
text: t("auto.rowTravelCounts", {
|
|
1477
|
+
label: t("auto.rowTravel"),
|
|
1478
|
+
departed: Number(counts.departed) || 0,
|
|
1479
|
+
claimed: claimed,
|
|
1480
|
+
adopted: adopted,
|
|
1481
|
+
}),
|
|
1482
|
+
tone: claimed > 0 || adopted > 0 ? "ok" : "",
|
|
1483
|
+
});
|
|
1484
|
+
};
|
|
1485
|
+
push(t("auto.rowWorkbuddy"), { claimed: last.claimed, skipped: last.skipped, failed: last.failed });
|
|
1486
|
+
pushTravel(last.travel);
|
|
1487
|
+
if (last.growth) {
|
|
1488
|
+
var growth = last.growth;
|
|
1489
|
+
var streakTiers = Number(growth.streak) || 0;
|
|
1490
|
+
var taskCount = Number(growth.tasks) || 0;
|
|
1491
|
+
rows.push({
|
|
1492
|
+
text: t("auto.rowGrowth", {
|
|
1493
|
+
streak: streakTiers,
|
|
1494
|
+
tasks: taskCount,
|
|
1495
|
+
credit: Number(growth.credit) || 0,
|
|
1496
|
+
failed: Number(growth.failed) || 0,
|
|
1497
|
+
}),
|
|
1498
|
+
tone: Number(growth.failed) > 0 ? "fail" : streakTiers > 0 || taskCount > 0 ? "ok" : "",
|
|
1499
|
+
});
|
|
1500
|
+
}
|
|
1501
|
+
push(t("auto.rowQoder"), last.qoder);
|
|
1502
|
+
push(t("auto.rowTrae"), last.trae);
|
|
1503
|
+
return rows;
|
|
1504
|
+
}
|
|
1505
|
+
|
|
1506
|
+
/** Paint the check-in log as rows, so the caller does not build markup inline. */
|
|
1507
|
+
function paintAutoNote(host, auto) {
|
|
1508
|
+
clear(host);
|
|
1509
|
+
autoNoteRows(auto).forEach(function (row) {
|
|
1510
|
+
var line = el("div", "log-line" + (row.tone ? " " + row.tone : ""));
|
|
1511
|
+
line.appendChild(el("span", "log-dot"));
|
|
1512
|
+
line.appendChild(el("span", "log-text", row.text));
|
|
1513
|
+
host.appendChild(line);
|
|
1514
|
+
});
|
|
1515
|
+
// The log is a scroll region capped at a few rows, so a fresh pass would
|
|
1516
|
+
// otherwise leave the newest line - the one the user just waited for - below
|
|
1517
|
+
// the fold.
|
|
1518
|
+
host.scrollTop = host.scrollHeight;
|
|
1519
|
+
}
|
|
1520
|
+
function renderAccounts() {
|
|
1521
|
+
var host = $("accounts");
|
|
1522
|
+
// Every control in here repaints this column, and the column has no scroller
|
|
1523
|
+
// of its own — it rides in the document. Without recording the offset first,
|
|
1524
|
+
// expanding a credits detail near the bottom of a long list flings the page
|
|
1525
|
+
// back to the first card. Same record-and-restore the roster pane uses.
|
|
1526
|
+
var keep = placeOf(host);
|
|
1527
|
+
paintAccounts(host);
|
|
1528
|
+
restorePlace(host, keep);
|
|
1529
|
+
}
|
|
1530
|
+
|
|
1531
|
+
function paintAccounts(host) {
|
|
1532
|
+
clear(host);
|
|
1533
|
+
if (state === null) return;
|
|
1534
|
+
var accounts = accountsOf(activeRegion);
|
|
1535
|
+
// A parked region has no accounts to show: it says so, and offers its switch.
|
|
1536
|
+
if (activeRegion !== "" && regionParked("workbuddy", activeRegion)) {
|
|
1537
|
+
host.appendChild(parkedRegionBlock("workbuddy", activeRegion));
|
|
1538
|
+
host.appendChild(sourceFoot("workbuddy"));
|
|
1539
|
+
return;
|
|
1540
|
+
}
|
|
1541
|
+
var preferred = (state && state.preferredAccountIds && state.preferredAccountIds[activeRegion]) || "";
|
|
1542
|
+
var choice = el("div", "card");
|
|
1543
|
+
var label = el("label", "field-label", t("acct.choose"));
|
|
1544
|
+
label.htmlFor = "accountChoice";
|
|
1545
|
+
var picker = el("select", "text-input");
|
|
1546
|
+
picker.id = "accountChoice";
|
|
1547
|
+
picker.setAttribute("aria-label", t("acct.choose"));
|
|
1548
|
+
var automatic = el("option", null, t("acct.automatic"));
|
|
1549
|
+
automatic.value = "";
|
|
1550
|
+
picker.appendChild(automatic);
|
|
1551
|
+
// A switched-off account cannot be the preferred one: the pool would keep
|
|
1552
|
+
// asking for an id it is not allowed to hand out.
|
|
1553
|
+
var selectable = accounts.filter(function (account) { return account.disabled !== true; });
|
|
1554
|
+
selectable.forEach(function (account) {
|
|
1555
|
+
var title = account.label;
|
|
1556
|
+
if (account.credits && Number.isFinite(account.credits.total)) {
|
|
1557
|
+
title = t("acct.choiceCredits", { label: title, credits: account.credits.total.toLocaleString() });
|
|
1558
|
+
}
|
|
1559
|
+
var option = el("option", null, title);
|
|
1560
|
+
option.value = account.id;
|
|
1561
|
+
picker.appendChild(option);
|
|
1562
|
+
});
|
|
1563
|
+
if (preferred && !selectable.some(function (account) { return account.id === preferred; })) {
|
|
1564
|
+
var missing = el("option", null, t("acct.unavailable"));
|
|
1565
|
+
missing.value = preferred;
|
|
1566
|
+
picker.appendChild(missing);
|
|
1567
|
+
}
|
|
1568
|
+
picker.value = preferred;
|
|
1569
|
+
picker.disabled = busy !== null || (selectable.length === 0 && preferred === "");
|
|
1570
|
+
picker.addEventListener("change", function () { setPreferredAccount(picker.value); });
|
|
1571
|
+
choice.appendChild(label);
|
|
1572
|
+
choice.appendChild(picker);
|
|
1573
|
+
choice.appendChild(el("p", "hint", t("acct.choiceHint")));
|
|
1574
|
+
choice.appendChild(distributionControl("workbuddy"));
|
|
1575
|
+
host.appendChild(choice);
|
|
1576
|
+
if (accounts.length === 0) {
|
|
1577
|
+
var empty = el("div", "empty");
|
|
1578
|
+
if (!sourceOn("workbuddy")) {
|
|
1579
|
+
empty.appendChild(el("p", null, t("pool.sourceOff")));
|
|
1580
|
+
} else {
|
|
1581
|
+
empty.appendChild(el("p", null, t("pool.emptyRegion", { region: regionLabel(activeRegion, false) })));
|
|
1582
|
+
empty.appendChild(el("p", "hint", t("pool.emptyHint")));
|
|
1583
|
+
}
|
|
1584
|
+
host.appendChild(empty);
|
|
1585
|
+
renderRemoved(host);
|
|
1586
|
+
if (activeRegion !== "") host.appendChild(regionFoot("workbuddy", activeRegion));
|
|
1587
|
+
host.appendChild(sourceFoot("workbuddy"));
|
|
1588
|
+
return;
|
|
1589
|
+
}
|
|
1590
|
+
accounts.forEach(function (account) { host.appendChild(accountCard(account)); });
|
|
1591
|
+
renderRemoved(host);
|
|
1592
|
+
// The region's own switch sits at the bottom of its content.
|
|
1593
|
+
if (activeRegion !== "") host.appendChild(regionFoot("workbuddy", activeRegion));
|
|
1594
|
+
host.appendChild(sourceFoot("workbuddy"));
|
|
1595
|
+
}
|
|
1596
|
+
|
|
1597
|
+
/**
|
|
1598
|
+
* Accounts the user removed for good, each with a button that brings it back.
|
|
1599
|
+
*
|
|
1600
|
+
* Shown because "remove" would otherwise be a one-way door: the account stops
|
|
1601
|
+
* being scanned, so there is nothing left in the account list to click.
|
|
1602
|
+
*/
|
|
1603
|
+
function renderRemoved(host) {
|
|
1604
|
+
var removed = (state && state.removedAccounts ? state.removedAccounts : []).filter(function (entry) {
|
|
1605
|
+
return entry.region === activeRegion;
|
|
1606
|
+
});
|
|
1607
|
+
if (removed.length === 0) return;
|
|
1608
|
+
var card = el("div", "card removed");
|
|
1609
|
+
var head = el("div", "card-head");
|
|
1610
|
+
head.appendChild(el("span", "card-title", t("removed.title")));
|
|
1611
|
+
head.appendChild(el("span", "count-chip", String(removed.length)));
|
|
1612
|
+
card.appendChild(head);
|
|
1613
|
+
card.appendChild(el("p", "hint", t("removed.hint")));
|
|
1614
|
+
removed.forEach(function (entry) {
|
|
1615
|
+
var row = el("div", "removed-row");
|
|
1616
|
+
row.appendChild(el("span", "removed-label", entry.label));
|
|
1617
|
+
var restore = el("button", "pane-action", t("removed.restore"));
|
|
1618
|
+
restore.type = "button";
|
|
1619
|
+
restore.disabled = busy !== null;
|
|
1620
|
+
restore.addEventListener("click", function () { setAccountState(entry.id, "restored"); });
|
|
1621
|
+
row.appendChild(restore);
|
|
1622
|
+
card.appendChild(row);
|
|
1623
|
+
});
|
|
1624
|
+
host.appendChild(card);
|
|
1625
|
+
}
|
|
1626
|
+
|
|
1627
|
+
function accountCard(account) {
|
|
1628
|
+
var off = account.disabled === true;
|
|
1629
|
+
var card = el("div", "card account" + (off ? " card-off" : ""));
|
|
1630
|
+
|
|
1631
|
+
var head = el("div", "card-head");
|
|
1632
|
+
head.appendChild(el("span", "acct-label", account.label));
|
|
1633
|
+
head.appendChild(el("span", "region-tag", regionLabel(account.region, true)));
|
|
1634
|
+
if (account.active) {
|
|
1635
|
+
head.appendChild(el("span", "badge next", t("badge.next")));
|
|
1636
|
+
}
|
|
1637
|
+
if (account.preferred) head.appendChild(el("span", "badge next", t("badge.preferred")));
|
|
1638
|
+
head.appendChild(el("span", "badge " + (off ? "off" : account.cooling ? "cool" : "ok"),
|
|
1639
|
+
off ? t("acct.off") : account.cooling ? t("badge.cooling") : t("badge.healthy")));
|
|
1640
|
+
|
|
1641
|
+
// "Not right now" and "not mine any more" are different decisions, so they
|
|
1642
|
+
// stay separate controls: a switch the user flips, and a plain remove button.
|
|
1643
|
+
var controls = el("div", "acct-controls");
|
|
1644
|
+
var toggle = el("button", "acct-toggle" + (off ? "" : " on"));
|
|
1645
|
+
toggle.type = "button";
|
|
1646
|
+
toggle.setAttribute("role", "switch");
|
|
1647
|
+
toggle.setAttribute("aria-checked", off ? "false" : "true");
|
|
1648
|
+
toggle.title = off ? t("acct.switchOn") : t("acct.switchOff");
|
|
1649
|
+
toggle.disabled = busy !== null;
|
|
1650
|
+
toggle.appendChild(el("span", "acct-toggle-dot"));
|
|
1651
|
+
toggle.appendChild(el("span", null, off ? t("acct.off") : t("acct.on")));
|
|
1652
|
+
toggle.addEventListener("click", function () { setAccountState(account.id, off ? "enabled" : "disabled"); });
|
|
1653
|
+
controls.appendChild(toggle);
|
|
1654
|
+
|
|
1655
|
+
var removeBtn = el("button", "acct-remove", t("acct.remove"));
|
|
1656
|
+
removeBtn.type = "button";
|
|
1657
|
+
removeBtn.title = t("acct.removeHint");
|
|
1658
|
+
removeBtn.disabled = busy !== null;
|
|
1659
|
+
removeBtn.addEventListener("click", function () { setAccountState(account.id, "removed"); });
|
|
1660
|
+
controls.appendChild(removeBtn);
|
|
1661
|
+
head.appendChild(controls);
|
|
1662
|
+
card.appendChild(head);
|
|
1663
|
+
|
|
1664
|
+
if (off) card.appendChild(el("div", "hint muted", t("acct.offHint")));
|
|
1665
|
+
|
|
1666
|
+
var meta = el("div", "model-meta");
|
|
1667
|
+
// Not beside a badge that already says the token is expired.
|
|
1668
|
+
if (account.expired !== true) meta.appendChild(tokenChip(account.expiresAt));
|
|
1669
|
+
if (account.cooling && account.cooldownUntil) {
|
|
1670
|
+
meta.appendChild(el("span", "finding-fail", t("acct.coolUntil", { time: formatStamp(account.cooldownUntil) })));
|
|
1671
|
+
}
|
|
1672
|
+
if (num(account.rateLimitHits, 0) > 0) {
|
|
1673
|
+
meta.appendChild(el("span", null, t("acct.hits", { hits: account.rateLimitHits })));
|
|
1674
|
+
}
|
|
1675
|
+
card.appendChild(meta);
|
|
1676
|
+
|
|
1677
|
+
(account.modelCooldowns || []).forEach(function (mc) {
|
|
1678
|
+
card.appendChild(el("div", "hint", t("acct.modelCool", { model: mc.modelId, time: formatStamp(mc.until) })));
|
|
1679
|
+
});
|
|
1680
|
+
|
|
1681
|
+
// A cold cache on the fast path: the main process has not read this
|
|
1682
|
+
// account's billing data yet, so say so instead of showing nothing.
|
|
1683
|
+
if (account.probePending === true) {
|
|
1684
|
+
card.appendChild(el("div", "hint muted", t("probe.pending")));
|
|
1685
|
+
} else if (account.creditsError) {
|
|
1686
|
+
card.appendChild(el("div", "hint finding-fail", t("credits.unavailable") + " — " + account.creditsError));
|
|
1687
|
+
} else if (account.credits) {
|
|
1688
|
+
card.appendChild(creditsBlock(account.credits, account.id, { floor: account }));
|
|
1689
|
+
}
|
|
1690
|
+
|
|
1691
|
+
card.appendChild(checkinBlock(account));
|
|
1692
|
+
return card;
|
|
1693
|
+
}
|
|
1694
|
+
|
|
1695
|
+
/**
|
|
1696
|
+
* One account's credits: a head that keeps the total, a body that lists every
|
|
1697
|
+
* pool with its own expiry, collapsed until asked for.
|
|
1698
|
+
*
|
|
1699
|
+
* A pool usually holds several accounts and each one carries a handful of
|
|
1700
|
+
* packages, so an always-open list pushes the second account off the screen for
|
|
1701
|
+
* numbers nobody reads twice. The header keeps what the eye wants — the total —
|
|
1702
|
+
* and the rest waits behind it. The open set lives in `openCredits`, keyed by
|
|
1703
|
+
* whatever identifies the account: a pool's id, or a source row's own key.
|
|
1704
|
+
*
|
|
1705
|
+
* A pool account and a source account read the same, so both come through here.
|
|
1706
|
+
* Two differences are passed in rather than branched on: the floor input is a
|
|
1707
|
+
* pool setting a source account does not have, and a pool hides a pack that has
|
|
1708
|
+
* nothing left in it while a source account shows every pool the route named —
|
|
1709
|
+
* Qoder's plan on a free account is 0/0, and that row is the one that says it
|
|
1710
|
+
* never expires.
|
|
1711
|
+
*/
|
|
1712
|
+
function creditsBlock(credits, key, options) {
|
|
1713
|
+
var settings = options || {};
|
|
1714
|
+
var open = openCredits[key] === true;
|
|
1715
|
+
var wrap = el("div", "credits" + (open ? " open" : ""));
|
|
1716
|
+
|
|
1717
|
+
// Same rule as the upstream parser: a pack with nothing left in it is not
|
|
1718
|
+
// shown and not counted. `remain`, not `size` — a drained 50-credit month has
|
|
1719
|
+
// a size of 50 and would otherwise survive this filter as an empty row.
|
|
1720
|
+
var packages = (credits.packages || []).filter(function (pack) {
|
|
1721
|
+
return settings.keepEmpty === true || !(typeof pack.remain === "number" && pack.remain <= 0);
|
|
1722
|
+
});
|
|
1723
|
+
|
|
1724
|
+
var head = el("button", "credits-head");
|
|
1725
|
+
head.type = "button";
|
|
1726
|
+
head.setAttribute("aria-expanded", open ? "true" : "false");
|
|
1727
|
+
head.title = open ? t("credits.collapse") : t("credits.expand");
|
|
1728
|
+
head.appendChild(el("span", "card-title", t("credits.title")));
|
|
1729
|
+
if (typeof credits.total === "number") {
|
|
1730
|
+
head.appendChild(el("span", "credit-total", formatCredits(credits.total)));
|
|
1731
|
+
}
|
|
1732
|
+
if (packages.length > 0) {
|
|
1733
|
+
head.appendChild(el("span", "count-chip", String(packages.length)));
|
|
1734
|
+
}
|
|
1735
|
+
if (settings.floor) {
|
|
1736
|
+
var reserve = num(settings.floor.creditReserve, 0);
|
|
1737
|
+
if (reserve > 0) {
|
|
1738
|
+
head.appendChild(el("span", "badge keep", t("credits.keepBadge", { credits: reserve.toLocaleString() })));
|
|
1739
|
+
}
|
|
1740
|
+
}
|
|
1741
|
+
head.appendChild(el("span", "caret", "›"));
|
|
1742
|
+
wrap.appendChild(head);
|
|
1743
|
+
|
|
1744
|
+
head.addEventListener("click", function () {
|
|
1745
|
+
// Toggled in place, not repainted: a rebuild removes the very button the
|
|
1746
|
+
// click used, and the page then forgets where it was scrolled to — the
|
|
1747
|
+
// accounts column has no scroller of its own. The state is read back on
|
|
1748
|
+
// every click, because this handler outlives the render that built it.
|
|
1749
|
+
var shown = openCredits[key] !== true;
|
|
1750
|
+
openCredits[key] = shown;
|
|
1751
|
+
wrap.className = "credits" + (shown ? " open" : "");
|
|
1752
|
+
head.setAttribute("aria-expanded", shown ? "true" : "false");
|
|
1753
|
+
head.title = shown ? t("credits.collapse") : t("credits.expand");
|
|
1754
|
+
body.hidden = !shown;
|
|
1755
|
+
});
|
|
1756
|
+
var body = el("div", "credits-body");
|
|
1757
|
+
body.hidden = !open;
|
|
1758
|
+
packages.forEach(function (pack) { body.appendChild(packRow(pack)); });
|
|
1759
|
+
if (typeof credits.expiringSoon === "number" && credits.expiringSoon > 0) {
|
|
1760
|
+
body.appendChild(el("div", "hint warn",
|
|
1761
|
+
t("credits.expiringSoon", { amount: credits.expiringSoon.toLocaleString() })));
|
|
1762
|
+
}
|
|
1763
|
+
// The floor is an account setting, not a package detail, so it stays available
|
|
1764
|
+
// even when the billing read came back empty.
|
|
1765
|
+
if (settings.floor) body.appendChild(reserveRow(settings.floor));
|
|
1766
|
+
wrap.appendChild(body);
|
|
1767
|
+
return wrap;
|
|
1768
|
+
}
|
|
1769
|
+
|
|
1770
|
+
/**
|
|
1771
|
+
* The credit floor for one account: an explicit save, not a blur side effect.
|
|
1772
|
+
*
|
|
1773
|
+
* The field is a draft; Save only lights up once the number differs from what
|
|
1774
|
+
* the host last reported, and the outcome is shown next to the button instead of
|
|
1775
|
+
* being written silently.
|
|
1776
|
+
*/
|
|
1777
|
+
function reserveRow(account) {
|
|
1778
|
+
var saved = num(account.creditReserve, 0);
|
|
1779
|
+
var wrap = el("div", "reserve");
|
|
1780
|
+
var label = el("label", "reserve-label", t("credits.keepAtLeast"));
|
|
1781
|
+
var input = document.createElement("input");
|
|
1782
|
+
input.type = "number";
|
|
1783
|
+
input.min = "0";
|
|
1784
|
+
input.step = "1";
|
|
1785
|
+
input.className = "reserve-input";
|
|
1786
|
+
input.id = "reserve-" + account.id;
|
|
1787
|
+
input.value = reserveDrafts[account.id] === undefined ? String(saved) : reserveDrafts[account.id];
|
|
1788
|
+
input.setAttribute("aria-label", t("credits.keepAtLeast"));
|
|
1789
|
+
input.disabled = busy !== null;
|
|
1790
|
+
var unit = el("span", "reserve-unit", t("credits.keepUnit"));
|
|
1791
|
+
var button = el("button", "reserve-save", t("credits.keepSave"));
|
|
1792
|
+
button.type = "button";
|
|
1793
|
+
button.id = "reserve-save-" + account.id;
|
|
1794
|
+
|
|
1795
|
+
var parsed = Number(input.value);
|
|
1796
|
+
var next = Number.isFinite(parsed) && parsed > 0 ? Math.floor(parsed) : 0;
|
|
1797
|
+
var dirty = next !== saved;
|
|
1798
|
+
button.disabled = busy !== null || !dirty;
|
|
1799
|
+
input.addEventListener("input", function () {
|
|
1800
|
+
reserveDrafts[account.id] = input.value;
|
|
1801
|
+
var value = Number(input.value);
|
|
1802
|
+
next = Number.isFinite(value) && value > 0 ? Math.floor(value) : 0;
|
|
1803
|
+
button.disabled = busy !== null || next === saved;
|
|
1804
|
+
});
|
|
1805
|
+
button.addEventListener("click", function () { setCreditReserve(account.id, next); });
|
|
1806
|
+
input.addEventListener("keydown", function (event) {
|
|
1807
|
+
if (event.key === "Enter" && !button.disabled) setCreditReserve(account.id, next);
|
|
1808
|
+
});
|
|
1809
|
+
|
|
1810
|
+
wrap.appendChild(label);
|
|
1811
|
+
wrap.appendChild(input);
|
|
1812
|
+
wrap.appendChild(unit);
|
|
1813
|
+
wrap.appendChild(button);
|
|
1814
|
+
wrap.appendChild(el("p", "hint", t("credits.keepHint")));
|
|
1815
|
+
return wrap;
|
|
1816
|
+
}
|
|
1817
|
+
|
|
1818
|
+
/** One package line: name, remaining/size, and when it refreshes or expires. */
|
|
1819
|
+
function packRow(pack) {
|
|
1820
|
+
var row = el("div", "pack");
|
|
1821
|
+
// A pool the upstream does not name arrives as a locale key instead of a
|
|
1822
|
+
// name, so the row still reads in the reader's language.
|
|
1823
|
+
row.appendChild(el("span", "pack-name", pack.nameKey === undefined ? (pack.packageName || "—") : t(pack.nameKey)));
|
|
1824
|
+
var remain = pack.remain === undefined ? "?" : String(pack.remain);
|
|
1825
|
+
var size = pack.size === undefined ? "?" : String(pack.size);
|
|
1826
|
+
row.appendChild(el("span", "pack-num", t("credits.package", { remain: remain, size: size })));
|
|
1827
|
+
row.appendChild(packWhen(pack));
|
|
1828
|
+
return row;
|
|
1829
|
+
}
|
|
1830
|
+
|
|
1831
|
+
/**
|
|
1832
|
+
* The window an expiry is worth flagging, shared by a pack's own date and the
|
|
1833
|
+
* account-wide one so both light up together.
|
|
1834
|
+
*/
|
|
1835
|
+
var PACK_SOON_MS = 3 * 24 * 60 * 60 * 1000;
|
|
1836
|
+
|
|
1837
|
+
/**
|
|
1838
|
+
* The deadline that matters for one package.
|
|
1839
|
+
*
|
|
1840
|
+
* A monthly pack never expires — it refills — so its cycle rollover is the
|
|
1841
|
+
* date worth showing; a one-off pack gets its expiry, flagged while it is
|
|
1842
|
+
* inside the three-day window the summary counts. Some pools state a rule
|
|
1843
|
+
* instead of a date ("valid N days after the claim"), and one says it never
|
|
1844
|
+
* expires at all; both are answers, so neither renders as a blank.
|
|
1845
|
+
*
|
|
1846
|
+
* A pool the upstream only reports as a sum still gets a date when the newest
|
|
1847
|
+
* round that fed it states one: the check-in pool is a pile of rounds, each
|
|
1848
|
+
* living a fixed number of days from its own claim, and the latest round's
|
|
1849
|
+
* date is the only one the API answers for. It is labelled as that round's, so
|
|
1850
|
+
* the row never claims the whole pool dies on that day.
|
|
1851
|
+
*/
|
|
1852
|
+
function packWhen(pack) {
|
|
1853
|
+
if (typeof pack.roundExpiresAtMs === "number" && pack.roundExpiresAtMs > 0) {
|
|
1854
|
+
// Only a round that is still live can be "soon". Without the lower bound an
|
|
1855
|
+
// expiry already in the past rendered as expiring, permanently, so a row
|
|
1856
|
+
// nobody can spend kept its warning badge.
|
|
1857
|
+
var roundSoon = pack.roundExpiresAtMs > Date.now()
|
|
1858
|
+
&& pack.roundExpiresAtMs - Date.now() < PACK_SOON_MS;
|
|
1859
|
+
return el("span", roundSoon ? "pack-when pack-soon" : "pack-when",
|
|
1860
|
+
t("credits.roundExpires", { date: formatDayStamp(pack.roundExpiresAtMs) }));
|
|
1861
|
+
}
|
|
1862
|
+
if (typeof pack.validDays === "number" && pack.validDays > 0) {
|
|
1863
|
+
return el("span", "pack-when", t("credits.validDays", { days: pack.validDays }));
|
|
1864
|
+
}
|
|
1865
|
+
if (pack.monthly === true) {
|
|
1866
|
+
if (typeof pack.refreshAtMs !== "number" || pack.refreshAtMs <= 0) {
|
|
1867
|
+
return el("span", "pack-when", t("credits.monthly"));
|
|
1868
|
+
}
|
|
1869
|
+
return el("span", "pack-when", t("credits.refreshAt", { time: formatDayStamp(pack.refreshAtMs) }));
|
|
1870
|
+
}
|
|
1871
|
+
if (pack.neverExpires === true) return el("span", "pack-when", t("credits.noExpiry"));
|
|
1872
|
+
if (typeof pack.expiresAtMs !== "number" || pack.expiresAtMs <= 0) return el("span", "pack-when", "");
|
|
1873
|
+
// Same lower bound as the round above: an expiry already in the past is not
|
|
1874
|
+
// "soon", it is spent, and without this a stale row kept its warning badge.
|
|
1875
|
+
var soon = pack.expiresAtMs > Date.now() && pack.expiresAtMs - Date.now() < PACK_SOON_MS;
|
|
1876
|
+
return el("span", soon ? "pack-when pack-soon" : "pack-when", t("credits.expiresAt", {
|
|
1877
|
+
date: formatDayStamp(pack.expiresAtMs),
|
|
1878
|
+
}));
|
|
1879
|
+
}
|
|
1880
|
+
|
|
1881
|
+
function checkinBlock(account) {
|
|
1882
|
+
var wrap = el("div", "checkin");
|
|
1883
|
+
wrap.appendChild(el("span", "card-title", t("checkin.title")));
|
|
1884
|
+
|
|
1885
|
+
if (account.checkinError) {
|
|
1886
|
+
wrap.appendChild(el("span", "hint finding-fail", account.checkinError));
|
|
1887
|
+
return wrap;
|
|
1888
|
+
}
|
|
1889
|
+
var checkin = account.checkin;
|
|
1890
|
+
if (!checkin) {
|
|
1891
|
+
wrap.appendChild(el("span", "muted", t("checkin.inactive")));
|
|
1892
|
+
return wrap;
|
|
1893
|
+
}
|
|
1894
|
+
|
|
1895
|
+
if (checkin.streakDays > 0) wrap.appendChild(el("span", "muted", t("checkin.streak", { days: checkin.streakDays })));
|
|
1896
|
+
if (checkin.dailyCredit > 0) wrap.appendChild(el("span", "muted", t("checkin.daily", { credit: checkin.dailyCredit })));
|
|
1897
|
+
// The milestone line names the day the bonus lands on, which is what upstream
|
|
1898
|
+
// reports as `nextStreakDay`; the bonus is granted when that day arrives.
|
|
1899
|
+
if (checkin.isStreakDay && checkin.streakBonusCredit > 0) {
|
|
1900
|
+
var milestone = checkin.nextStreakDay > 0 ? checkin.nextStreakDay : checkin.streakDays;
|
|
1901
|
+
wrap.appendChild(el("span", "badge free", t("checkin.bonus", { days: milestone, credit: checkin.streakBonusCredit })));
|
|
1902
|
+
}
|
|
1903
|
+
|
|
1904
|
+
if (!checkin.active) {
|
|
1905
|
+
wrap.appendChild(el("span", "muted", t("checkin.inactive")));
|
|
1906
|
+
return wrap;
|
|
1907
|
+
}
|
|
1908
|
+
|
|
1909
|
+
var busyHere = checkinBusy === account.id;
|
|
1910
|
+
// Upstream ships the button's own label when the campaign renames it.
|
|
1911
|
+
var idleLabel = checkin.todayCheckedIn ? t("btn.checkinDone") : t("btn.checkin");
|
|
1912
|
+
var button = el("button", "primary small" + (busyHere ? " is-busy" : ""), busyHere ? t("checkin.claiming") : idleLabel);
|
|
1913
|
+
button.type = "button";
|
|
1914
|
+
// A whole-pool claim is in flight: this account is covered by it, so the
|
|
1915
|
+
// per-account button must not fire a second claim.
|
|
1916
|
+
button.disabled = checkin.todayCheckedIn || busyHere || busy !== null || checkinBusy !== null;
|
|
1917
|
+
button.addEventListener("click", function () { claimCheckin(account.id); });
|
|
1918
|
+
wrap.appendChild(button);
|
|
1919
|
+
|
|
1920
|
+
if (checkin.todayCheckedIn && checkin.todayCredit > 0) {
|
|
1921
|
+
wrap.appendChild(el("span", "muted", t("checkin.claimed", { credit: checkin.todayCredit })));
|
|
1922
|
+
}
|
|
1923
|
+
return wrap;
|
|
1924
|
+
}
|
|
1925
|
+
|
|
1926
|
+
/**
|
|
1927
|
+
* Where a control sat, and what would identify it after a rebuild.
|
|
1928
|
+
*
|
|
1929
|
+
* Rebuilding a column replaces every control in it — including the one a click
|
|
1930
|
+
* just focused. Chromium answers that removal by dropping the column's scroll
|
|
1931
|
+
* to the top and focus to <body>, so ticking a row near the end of a long
|
|
1932
|
+
* roster flings the pane back to the first row. Record the scroller offset and
|
|
1933
|
+
* where focus sat, then look both up again once the host is repopulated. A
|
|
1934
|
+
* column with no scroller of its own (the accounts one) rides along in the
|
|
1935
|
+
* document, and that offset travels with it too.
|
|
1936
|
+
*/
|
|
1937
|
+
function placeOf(host) {
|
|
1938
|
+
var active = document.activeElement;
|
|
1939
|
+
var keep = { line: host.scrollTop, page: pageScroll(), place: null };
|
|
1940
|
+
if (!active || !host.contains(active)) return keep;
|
|
1941
|
+
keep.place = controlRecord(active, host);
|
|
1942
|
+
return keep;
|
|
1943
|
+
}
|
|
1944
|
+
|
|
1945
|
+
/**
|
|
1946
|
+
* The record that finds `node` again inside `host` after a rebuild.
|
|
1947
|
+
*
|
|
1948
|
+
* By identity where the control has an id, by position where the population is
|
|
1949
|
+
* unchanged, and by label otherwise — most controls here are built fresh on
|
|
1950
|
+
* every render and never carry an id, so the label is the only handle a later
|
|
1951
|
+
* pass has on the one the user actually pressed. Null for a control that is
|
|
1952
|
+
* none of the three kinds this panel focuses.
|
|
1953
|
+
*/
|
|
1954
|
+
function controlRecord(node, host) {
|
|
1955
|
+
var kind = node.tagName === "INPUT" ? "input"
|
|
1956
|
+
: node.tagName === "SELECT" ? "select"
|
|
1957
|
+
: node.tagName === "BUTTON" ? "button" : null;
|
|
1958
|
+
if (!kind) return null;
|
|
1959
|
+
var group = host.querySelectorAll(kind);
|
|
1960
|
+
// A whole-panel render changes how many buttons exist, so an index recorded
|
|
1961
|
+
// before it can point at a different control afterwards — the focus would land
|
|
1962
|
+
// on the wrong button, which reads as the panel jumping somewhere the user
|
|
1963
|
+
// never clicked. The id, and failing that the label, is what survives that.
|
|
1964
|
+
var id = typeof node.id === "string" && node.id !== "" ? node.id : "";
|
|
1965
|
+
return {
|
|
1966
|
+
kind: kind,
|
|
1967
|
+
at: Array.prototype.indexOf.call(group, node),
|
|
1968
|
+
total: group.length,
|
|
1969
|
+
id: id,
|
|
1970
|
+
label: id === "" ? (node.textContent || "").trim() : "",
|
|
1971
|
+
};
|
|
1972
|
+
}
|
|
1973
|
+
|
|
1974
|
+
/**
|
|
1975
|
+
* The control a record names, or null when this pass did not rebuild it.
|
|
1976
|
+
*
|
|
1977
|
+
* The id is matched by scanning the group rather than by building a `#id`
|
|
1978
|
+
* selector: the ids here are derived from account ids, and one holding a
|
|
1979
|
+
* character CSS reads specially would make the selector throw mid-render.
|
|
1980
|
+
*/
|
|
1981
|
+
function locateControl(host, place) {
|
|
1982
|
+
if (!place) return null;
|
|
1983
|
+
var group = host.querySelectorAll(place.kind);
|
|
1984
|
+
if (place.id !== "") {
|
|
1985
|
+
for (var at = 0; at < group.length; at += 1) {
|
|
1986
|
+
if (group[at].id === place.id) return group[at];
|
|
1987
|
+
}
|
|
1988
|
+
}
|
|
1989
|
+
if (group.length === place.total) return group[place.at] || null;
|
|
1990
|
+
if (place.label === "") return null;
|
|
1991
|
+
for (var i = 0; i < group.length; i += 1) {
|
|
1992
|
+
if ((group[i].textContent || "").trim() === place.label) return group[i];
|
|
1993
|
+
}
|
|
1994
|
+
return null;
|
|
1995
|
+
}
|
|
1996
|
+
|
|
1997
|
+
function restorePlace(host, keep) {
|
|
1998
|
+
host.scrollTop = keep.line;
|
|
1999
|
+
pageScrollTo(keep.page);
|
|
2000
|
+
if (!keep.place) return;
|
|
2001
|
+
var again = locateControl(host, keep.place);
|
|
2002
|
+
if (!again || !again.focus) return;
|
|
2003
|
+
// A control disabled by the very render pass that just ran cannot take focus:
|
|
2004
|
+
// the browser refuses it, the focus falls to the body, and the panel reads as
|
|
2005
|
+
// having jumped. There is no synchronous way around it — clearing `disabled`
|
|
2006
|
+
// for the focus call does not make the element focusable, and neither does a
|
|
2007
|
+
// temporary `tabindex` — so the focus is simply left where it is in that case.
|
|
2008
|
+
// The shake itself is still gone, because the scroll position is restored
|
|
2009
|
+
// above; what is lost is the focus ring on a control the user just pressed and
|
|
2010
|
+
// which is now inert anyway.
|
|
2011
|
+
if (again.disabled === true) return;
|
|
2012
|
+
again.focus({ preventScroll: true });
|
|
2013
|
+
}
|
|
2014
|
+
|
|
2015
|
+
/**
|
|
2016
|
+
* The control the user last pressed, so a render pass that disabled it can hand
|
|
2017
|
+
* the focus back once it is live again. See `render`.
|
|
2018
|
+
*/
|
|
2019
|
+
var lastPressed = null;
|
|
2020
|
+
|
|
2021
|
+
/**
|
|
2022
|
+
* Record a press, for the focus hand-back in `render`.
|
|
2023
|
+
*
|
|
2024
|
+
* The whole locator is kept, not just the id: most controls here are built
|
|
2025
|
+
* fresh on every render and never carry one, so an id-only memory silently
|
|
2026
|
+
* skipped exactly the buttons the hand-back exists for.
|
|
2027
|
+
*/
|
|
2028
|
+
function notePress(node) {
|
|
2029
|
+
var body = document.body;
|
|
2030
|
+
lastPressed = node && body && body.contains(node) ? controlRecord(node, body) : null;
|
|
2031
|
+
}
|
|
2032
|
+
|
|
2033
|
+
/** The document's own offset: a column with no scroller rides along in it. */
|
|
2034
|
+
function pageScroll() {
|
|
2035
|
+
var root = document.scrollingElement || document.documentElement || document.body;
|
|
2036
|
+
return root && typeof root.scrollTop === "number" ? root.scrollTop : 0;
|
|
2037
|
+
}
|
|
2038
|
+
|
|
2039
|
+
function pageScrollTo(top) {
|
|
2040
|
+
var root = document.scrollingElement || document.documentElement || document.body;
|
|
2041
|
+
if (root) root.scrollTop = top;
|
|
2042
|
+
}
|
|
2043
|
+
|
|
2044
|
+
/**
|
|
2045
|
+
* The roster view: one checkbox per model, the host's own layout.
|
|
2046
|
+
*
|
|
2047
|
+
* The heading carries a select-all box (indeterminate while only some rows are
|
|
2048
|
+
* ticked), the fetch button - the pool only, because a scan is what re-reads
|
|
2049
|
+
* its catalog - and a search restricted to id / name / alias.
|
|
2050
|
+
*/
|
|
2051
|
+
function renderModels() {
|
|
2052
|
+
var page = modelPage();
|
|
2053
|
+
var list = page.list;
|
|
2054
|
+
// The draft is keyed on the gateway and the exact set of ids it was built
|
|
2055
|
+
// from: the first render runs before any state has arrived, and a catalog
|
|
2056
|
+
// that gains or loses a model has to rebuild it, while a plain refresh of
|
|
2057
|
+
// the same list leaves the user's unsaved edits alone. Each page keeps its
|
|
2058
|
+
// own slot, so looking at another region and coming back returns to the ticks
|
|
2059
|
+
// that were left there rather than to the saved selection.
|
|
2060
|
+
var key = page.source + "\u0000" + page.key + "\u0000"
|
|
2061
|
+
+ list.map(function (model) { return model.id; }).sort().join("\u0000");
|
|
2062
|
+
var slot = page.source + "\u0000" + page.key;
|
|
2063
|
+
if (drafts[slot] === undefined || drafts[slot].key !== key) {
|
|
2064
|
+
drafts[slot] = { key: key, draft: draftFor(list, page.selection) };
|
|
2065
|
+
}
|
|
2066
|
+
draft = drafts[slot].draft;
|
|
2067
|
+
draftSlot = slot;
|
|
2068
|
+
|
|
2069
|
+
$("modelListTitle").textContent = page.label;
|
|
2070
|
+
show($("modelFetchBtn"), page.source === "workbuddy");
|
|
2071
|
+
$("modelFetchBtn").textContent = busy === "fetch" ? t("models.modelsLoading") : t("models.fetchModelList");
|
|
2072
|
+
$("modelFetchBtn").disabled = busy !== null;
|
|
2073
|
+
$("modelSearch").placeholder = t("models.searchModel");
|
|
2074
|
+
var filter = $("modelSearch").value.trim().toLowerCase();
|
|
2075
|
+
var host = $("models");
|
|
2076
|
+
var keep = placeOf(host);
|
|
2077
|
+
clear(host);
|
|
2078
|
+
var published = publishedModels(list, page.cap);
|
|
2079
|
+
paintPageHint(page, published);
|
|
2080
|
+
|
|
2081
|
+
if (list.length === 0) {
|
|
2082
|
+
host.appendChild(regionParked(page.source, page.region)
|
|
2083
|
+
? parkedRegionBlock(page.source, page.region)
|
|
2084
|
+
: el("div", "empty", emptyRosterText(page)));
|
|
2085
|
+
$("modelSelectAll").checked = false;
|
|
2086
|
+
$("modelSelectAll").indeterminate = false;
|
|
2087
|
+
$("modelSelectAll").disabled = true;
|
|
2088
|
+
restorePlace(host, keep);
|
|
2089
|
+
renderChosen();
|
|
2090
|
+
return;
|
|
2091
|
+
}
|
|
2092
|
+
|
|
2093
|
+
var shown = list.filter(function (model) {
|
|
2094
|
+
if (!filter) return true;
|
|
2095
|
+
return String(model.id).toLowerCase().indexOf(filter) >= 0
|
|
2096
|
+
|| String(model.name).toLowerCase().indexOf(filter) >= 0
|
|
2097
|
+
|| String(model.alias || "").toLowerCase().indexOf(filter) >= 0;
|
|
2098
|
+
});
|
|
2099
|
+
if (shown.length === 0) host.appendChild(el("div", "empty", t("models.noMatch")));
|
|
2100
|
+
|
|
2101
|
+
// The header box reflects the visible rows, matching the host: clearing the
|
|
2102
|
+
// search and ticking the box selects everything, filtering narrows it.
|
|
2103
|
+
var shownEnabled = shown.filter(function (model) { return draft[model.id] && draft[model.id].enabled; }).length;
|
|
2104
|
+
var selectAll = $("modelSelectAll");
|
|
2105
|
+
selectAll.disabled = busy !== null;
|
|
2106
|
+
selectAll.checked = shown.length > 0 && shownEnabled === shown.length;
|
|
2107
|
+
selectAll.indeterminate = shownEnabled > 0 && shownEnabled < shown.length;
|
|
2108
|
+
selectAll.title = selectAll.checked ? t("models.deselectAll") : t("models.selectAll");
|
|
2109
|
+
selectAll.setAttribute("aria-label", selectAll.title);
|
|
2110
|
+
|
|
2111
|
+
shown.forEach(function (model) { host.appendChild(modelListRow(model, published)); });
|
|
2112
|
+
restorePlace(host, keep);
|
|
2113
|
+
renderChosen();
|
|
2114
|
+
}
|
|
2115
|
+
|
|
2116
|
+
/** What an empty roster says, which is not the same for all three sources. */
|
|
2117
|
+
function emptyRosterText(page) {
|
|
2118
|
+
if (!sourceOn(page.source)) {
|
|
2119
|
+
return t(page.source === "workbuddy" ? "pool.sourceOff" : SOURCE_SECTIONS[page.source].off);
|
|
2120
|
+
}
|
|
2121
|
+
if (page.source === "workbuddy") return t("models.empty");
|
|
2122
|
+
// An empty roster that carries a directory error is a failed read, not a
|
|
2123
|
+
// missing gateway: the two need opposite remedies, so they get their own
|
|
2124
|
+
// sentences instead of sharing one that is always half wrong.
|
|
2125
|
+
return page.error !== "" ? t("models.noDirectory") : t("models.noGateway");
|
|
2126
|
+
}
|
|
2127
|
+
|
|
2128
|
+
/**
|
|
2129
|
+
* The line under the columns: a gateway's own last error, or the note that
|
|
2130
|
+
* says how many models one provider may declare. Empty for a healthy pool.
|
|
2131
|
+
*/
|
|
2132
|
+
function paintPageHint(page, published) {
|
|
2133
|
+
var hint = $("modelPageHint");
|
|
2134
|
+
if (page.error !== "") {
|
|
2135
|
+
hint.className = "hint warn";
|
|
2136
|
+
hint.textContent = page.error;
|
|
2137
|
+
} else if (page.cap > 0 && Object.keys(published).length >= page.cap) {
|
|
2138
|
+
hint.className = "hint";
|
|
2139
|
+
hint.textContent = t("models.cappedHint", { count: page.cap });
|
|
2140
|
+
} else {
|
|
2141
|
+
hint.className = "hint";
|
|
2142
|
+
hint.textContent = "";
|
|
2143
|
+
}
|
|
2144
|
+
}
|
|
2145
|
+
|
|
2146
|
+
/** The ids the desktop will really list: the ticked ones, in roster order, capped. */
|
|
2147
|
+
function publishedModels(list, cap) {
|
|
2148
|
+
var out = {};
|
|
2149
|
+
var count = 0;
|
|
2150
|
+
for (var i = 0; i < list.length && (cap <= 0 || count < cap); i += 1) {
|
|
2151
|
+
var entry = draft[list[i].id];
|
|
2152
|
+
if (entry && entry.enabled) {
|
|
2153
|
+
out[list[i].id] = true;
|
|
2154
|
+
count += 1;
|
|
2155
|
+
}
|
|
2156
|
+
}
|
|
2157
|
+
return out;
|
|
2158
|
+
}
|
|
2159
|
+
|
|
2160
|
+
/** One row of the roster: a checkbox, the model's own name, its id and limits. */
|
|
2161
|
+
function modelListRow(model, published) {
|
|
2162
|
+
var entry = draft[model.id];
|
|
2163
|
+
var row = el("li", "row");
|
|
2164
|
+
var label = el("label", "row-label");
|
|
2165
|
+
|
|
2166
|
+
var box = document.createElement("input");
|
|
2167
|
+
box.type = "checkbox";
|
|
2168
|
+
box.className = "row-check";
|
|
2169
|
+
box.checked = entry.enabled;
|
|
2170
|
+
box.disabled = busy !== null;
|
|
2171
|
+
box.addEventListener("change", function () {
|
|
2172
|
+
entry.enabled = box.checked;
|
|
2173
|
+
renderModels();
|
|
2174
|
+
});
|
|
2175
|
+
label.appendChild(box);
|
|
2176
|
+
|
|
2177
|
+
var copy = el("span", "row-copy");
|
|
2178
|
+
// The name only. Qoder's ids are internal codes (`qmodel_38max`) and the
|
|
2179
|
+
// others repeat the name, so a second column of text never told the user
|
|
2180
|
+
// anything the row did not already say.
|
|
2181
|
+
var lead = el("span", "row-id", modelDisplay(model) || model.id);
|
|
2182
|
+
lead.title = modelDisplay(model) || model.id;
|
|
2183
|
+
copy.appendChild(lead);
|
|
2184
|
+
label.appendChild(copy);
|
|
2185
|
+
|
|
2186
|
+
var limits = el("span", "row-limits",
|
|
2187
|
+
formatTokens(effectiveWindow(model, entry)) + " · " + formatTokens(effectiveOutput(model, entry)));
|
|
2188
|
+
label.appendChild(limits);
|
|
2189
|
+
|
|
2190
|
+
var chips = tagOf(model);
|
|
2191
|
+
// A tick the desktop will never show is still a tick, so the row names the
|
|
2192
|
+
// reason instead of letting it look like a model that was dropped. An
|
|
2193
|
+
// unticked row is unpublished because it is unticked, not because of the cap.
|
|
2194
|
+
if (entry.enabled && published[model.id] !== true) chips.push({ text: t("models.capped"), tone: "" });
|
|
2195
|
+
if (chips.length > 0) {
|
|
2196
|
+
var chipHost = el("span", "row-tags");
|
|
2197
|
+
chips.forEach(function (chip) {
|
|
2198
|
+
chipHost.appendChild(el("span", "badge " + chip.tone, chip.text));
|
|
2199
|
+
});
|
|
2200
|
+
label.appendChild(chipHost);
|
|
2201
|
+
}
|
|
2202
|
+
|
|
2203
|
+
// Only an enabled model has a sheet to open, which is what the second view is.
|
|
2204
|
+
if (entry.enabled) {
|
|
2205
|
+
label.appendChild(el("span", "row-open-hint", "›"));
|
|
2206
|
+
label.classList.add("is-openable");
|
|
2207
|
+
label.addEventListener("click", function (event) {
|
|
2208
|
+
if (event.target instanceof HTMLInputElement) return;
|
|
2209
|
+
// The row is a <label>, whose default activation would also flip the
|
|
2210
|
+
// checkbox: opening the sheet must not simultaneously disable the model.
|
|
2211
|
+
event.preventDefault();
|
|
2212
|
+
openModelSheet(model.id);
|
|
2213
|
+
});
|
|
2214
|
+
}
|
|
2215
|
+
|
|
2216
|
+
row.appendChild(label);
|
|
2217
|
+
return row;
|
|
2218
|
+
}
|
|
2219
|
+
|
|
2220
|
+
/**
|
|
2221
|
+
* Open one model's sheet.
|
|
2222
|
+
*
|
|
2223
|
+
* Both columns stay on screen, so this only marks which model is expanded and
|
|
2224
|
+
* scrolls its row into view; nothing is hidden.
|
|
2225
|
+
*/
|
|
2226
|
+
function openModelSheet(id) {
|
|
2227
|
+
var entry = draft[id];
|
|
2228
|
+
if (!entry || !entry.enabled) return;
|
|
2229
|
+
entry.open = true;
|
|
2230
|
+
selectedModelId = id;
|
|
2231
|
+
renderChosen();
|
|
2232
|
+
var node = $("chosen");
|
|
2233
|
+
if (node && node.scrollIntoView) node.scrollIntoView({ block: "nearest" });
|
|
2234
|
+
}
|
|
2235
|
+
|
|
2236
|
+
/** The sheet view: every enabled model's editor, the selected one expanded. */
|
|
2237
|
+
function renderChosen() {
|
|
2238
|
+
$("modelEditTitle").textContent = t("models.modelConfigurations");
|
|
2239
|
+
$("chosenSearch").placeholder = t("models.searchChosen");
|
|
2240
|
+
|
|
2241
|
+
var page = modelPage();
|
|
2242
|
+
var list = page.list;
|
|
2243
|
+
renderSaveBar(isDirty(list, page.selection));
|
|
2244
|
+
var chosen = list.filter(function (model) { return draft[model.id] && draft[model.id].enabled; });
|
|
2245
|
+
$("modelEditCount").textContent = String(chosen.length);
|
|
2246
|
+
|
|
2247
|
+
var host = $("chosen");
|
|
2248
|
+
var keep = placeOf(host);
|
|
2249
|
+
clear(host);
|
|
2250
|
+
if (chosen.length === 0) {
|
|
2251
|
+
host.appendChild(el("div", "empty", t("models.noModelsChosen")));
|
|
2252
|
+
restorePlace(host, keep);
|
|
2253
|
+
return;
|
|
2254
|
+
}
|
|
2255
|
+
|
|
2256
|
+
var filter = $("chosenSearch").value.trim().toLowerCase();
|
|
2257
|
+
var shown = chosen.filter(function (model) {
|
|
2258
|
+
if (!filter) return true;
|
|
2259
|
+
return String(model.id).toLowerCase().indexOf(filter) >= 0
|
|
2260
|
+
|| String(modelDisplay(model)).toLowerCase().indexOf(filter) >= 0;
|
|
2261
|
+
});
|
|
2262
|
+
if (shown.length === 0) {
|
|
2263
|
+
host.appendChild(el("div", "empty", t("models.noChosenMatch")));
|
|
2264
|
+
restorePlace(host, keep);
|
|
2265
|
+
return;
|
|
2266
|
+
}
|
|
2267
|
+
|
|
2268
|
+
var host_ul = el("ul", "chosen-list");
|
|
2269
|
+
shown.forEach(function (model) { host_ul.appendChild(modelSheetRow(model)); });
|
|
2270
|
+
host.appendChild(host_ul);
|
|
2271
|
+
if (busy !== null) {
|
|
2272
|
+
host.querySelectorAll("input, select, button").forEach(function (control) { control.disabled = true; });
|
|
2273
|
+
}
|
|
2274
|
+
restorePlace(host, keep);
|
|
2275
|
+
}
|
|
2276
|
+
|
|
2277
|
+
/** The name a model shows everywhere: the user's alias when set, else the catalog's. */
|
|
2278
|
+
function modelDisplay(model) {
|
|
2279
|
+
var alias = typeof model.alias === "string" ? model.alias.trim() : "";
|
|
2280
|
+
return alias !== "" ? alias : model.name;
|
|
2281
|
+
}
|
|
2282
|
+
|
|
2283
|
+
/** One model's sheet row: summary line plus the collapsible field body. */
|
|
2284
|
+
function modelSheetRow(model) {
|
|
2285
|
+
var entry = draft[model.id];
|
|
2286
|
+
var expanded = entry.open === true;
|
|
2287
|
+
var row = el("li", "chosen-row");
|
|
2288
|
+
|
|
2289
|
+
var head = el("div", "chosen-head");
|
|
2290
|
+
// The name, the same as the roster row: the id it is filed under is not shown
|
|
2291
|
+
// anywhere in the panel, and the limits after it carry the settings.
|
|
2292
|
+
var display = modelDisplay(model) || model.id;
|
|
2293
|
+
var headId = el("span", "chosen-id", display);
|
|
2294
|
+
headId.title = display;
|
|
2295
|
+
head.appendChild(headId);
|
|
2296
|
+
head.appendChild(el("span", "chosen-limits",
|
|
2297
|
+
formatTokens(effectiveWindow(model, entry)) + " · " + formatTokens(effectiveOutput(model, entry))));
|
|
2298
|
+
|
|
2299
|
+
var advanced = el("button", expanded ? "chosen-advanced on" : "chosen-advanced", t("models.advanced"));
|
|
2300
|
+
advanced.type = "button";
|
|
2301
|
+
advanced.setAttribute("aria-expanded", expanded ? "true" : "false");
|
|
2302
|
+
advanced.addEventListener("click", function () {
|
|
2303
|
+
entry.open = !entry.open;
|
|
2304
|
+
renderChosen();
|
|
2305
|
+
});
|
|
2306
|
+
head.appendChild(advanced);
|
|
2307
|
+
|
|
2308
|
+
var remove = el("button", "chosen-remove", "×");
|
|
2309
|
+
remove.type = "button";
|
|
2310
|
+
remove.title = t("models.removeModel");
|
|
2311
|
+
remove.setAttribute("aria-label", t("models.removeModel"));
|
|
2312
|
+
remove.addEventListener("click", function () {
|
|
2313
|
+
entry.enabled = false;
|
|
2314
|
+
entry.open = false;
|
|
2315
|
+
// Removing the model being edited returns to the roster.
|
|
2316
|
+
if (selectedModelId === model.id) selectedModelId = null;
|
|
2317
|
+
renderModels();
|
|
2318
|
+
});
|
|
2319
|
+
head.appendChild(remove);
|
|
2320
|
+
row.appendChild(head);
|
|
2321
|
+
|
|
2322
|
+
if (!expanded) return row;
|
|
2323
|
+
row.appendChild(modelSheetBody(model, entry));
|
|
2324
|
+
return row;
|
|
2325
|
+
}
|
|
2326
|
+
|
|
2327
|
+
/** The model whose sheet is open, or null while the roster is showing. */
|
|
2328
|
+
var selectedModelId = null;
|
|
2329
|
+
|
|
2330
|
+
/**
|
|
2331
|
+
* The presets the host's own model editor offers, so a value picked here lands
|
|
2332
|
+
* on the same ladder. Kept in this order: the chips render left to right.
|
|
2333
|
+
*/
|
|
2334
|
+
var CONTEXT_WINDOW_PRESETS = [
|
|
2335
|
+
{ label: "128k", tokens: 128000 },
|
|
2336
|
+
{ label: "256k", tokens: 256000 },
|
|
2337
|
+
{ label: "312k", tokens: 312000 },
|
|
2338
|
+
{ label: "500k", tokens: 500000 },
|
|
2339
|
+
{ label: "1M", tokens: 1000000 },
|
|
2340
|
+
];
|
|
2341
|
+
var MAX_OUTPUT_PRESETS = [
|
|
2342
|
+
{ label: "4k", tokens: 4000 },
|
|
2343
|
+
{ label: "8k", tokens: 8000 },
|
|
2344
|
+
{ label: "16k", tokens: 16000 },
|
|
2345
|
+
{ label: "32k", tokens: 32000 },
|
|
2346
|
+
{ label: "128k", tokens: 128000 },
|
|
2347
|
+
];
|
|
2348
|
+
/** Every level the host's reasoning menu can carry, in its own order. */
|
|
2349
|
+
var THINKING_LEVELS = ["off", "minimal", "low", "medium", "high", "xhigh", "max"];
|
|
2350
|
+
|
|
2351
|
+
/** A fresh editable draft built from the server's document for one region. */
|
|
2352
|
+
function draftFor(list, selection) {
|
|
2353
|
+
var enabled = selection.enabledModelIds;
|
|
2354
|
+
var overrides = selection.overrides || {};
|
|
2355
|
+
var out = {};
|
|
2356
|
+
list.forEach(function (model) {
|
|
2357
|
+
var saved = overrides[model.id] || {};
|
|
2358
|
+
out[model.id] = {
|
|
2359
|
+
enabled: enabled === undefined || enabled.indexOf(model.id) >= 0,
|
|
2360
|
+
// An absent field means "follow the catalog", which the editor shows as
|
|
2361
|
+
// the untouched state rather than a value of its own.
|
|
2362
|
+
alias: typeof saved.alias === "string" ? saved.alias : "",
|
|
2363
|
+
window: typeof saved.contextWindow === "number" ? saved.contextWindow : null,
|
|
2364
|
+
output: typeof saved.maxOutputTokens === "number" ? saved.maxOutputTokens : null,
|
|
2365
|
+
images: typeof saved.supportsImages === "boolean" ? saved.supportsImages : null,
|
|
2366
|
+
levels: Array.isArray(saved.thinkingLevels) ? saved.thinkingLevels.slice() : null,
|
|
2367
|
+
defaultLevel: typeof saved.defaultThinkingLevel === "string" ? saved.defaultThinkingLevel : null,
|
|
2368
|
+
open: false,
|
|
2369
|
+
};
|
|
2370
|
+
});
|
|
2371
|
+
// The sheet reopens on whatever it was showing, when that model still exists.
|
|
2372
|
+
if (selectedModelId && out[selectedModelId]) out[selectedModelId].open = true;
|
|
2373
|
+
return out;
|
|
2374
|
+
}
|
|
2375
|
+
|
|
2376
|
+
/** The window a model currently advertises, override first. */
|
|
2377
|
+
function effectiveWindow(model, entry) {
|
|
2378
|
+
return entry.window === null ? model.nativeContextWindow : entry.window;
|
|
2379
|
+
}
|
|
2380
|
+
function effectiveOutput(model, entry) {
|
|
2381
|
+
return entry.output === null ? model.nativeMaxOutputTokens : entry.output;
|
|
2382
|
+
}
|
|
2383
|
+
function effectiveImages(model, entry) {
|
|
2384
|
+
return entry.images === null ? model.nativeSupportsImages === true : entry.images;
|
|
2385
|
+
}
|
|
2386
|
+
/** The levels a model offers: the user's set, else the catalog's. */
|
|
2387
|
+
function effectiveLevels(model, entry) {
|
|
2388
|
+
if (entry.levels !== null) return entry.levels;
|
|
2389
|
+
return Array.isArray(model.nativeThinkingLevels) ? model.nativeThinkingLevels : [];
|
|
2390
|
+
}
|
|
2391
|
+
|
|
2392
|
+
/** True when the draft differs from what the main process last reported. */
|
|
2393
|
+
function isDirty(list, selection) {
|
|
2394
|
+
var enabled = selection.enabledModelIds;
|
|
2395
|
+
var overrides = selection.overrides || {};
|
|
2396
|
+
for (var i = 0; i < list.length; i += 1) {
|
|
2397
|
+
var model = list[i];
|
|
2398
|
+
var entry = draft[model.id];
|
|
2399
|
+
if (!entry) continue;
|
|
2400
|
+
if (entry.enabled !== (enabled === undefined || enabled.indexOf(model.id) >= 0)) return true;
|
|
2401
|
+
var saved = overrides[model.id] || {};
|
|
2402
|
+
if (entry.alias !== (typeof saved.alias === "string" ? saved.alias : "")) return true;
|
|
2403
|
+
if (entry.window !== (typeof saved.contextWindow === "number" ? saved.contextWindow : null)) return true;
|
|
2404
|
+
if (entry.output !== (typeof saved.maxOutputTokens === "number" ? saved.maxOutputTokens : null)) return true;
|
|
2405
|
+
if (entry.images !== (typeof saved.supportsImages === "boolean" ? saved.supportsImages : null)) return true;
|
|
2406
|
+
if ((entry.levels === null ? null : entry.levels.join(","))
|
|
2407
|
+
!== (Array.isArray(saved.thinkingLevels) ? saved.thinkingLevels.join(",") : null)) return true;
|
|
2408
|
+
if (entry.defaultLevel !== (typeof saved.defaultThinkingLevel === "string" ? saved.defaultThinkingLevel : null)) return true;
|
|
2409
|
+
}
|
|
2410
|
+
return false;
|
|
2411
|
+
}
|
|
2412
|
+
|
|
2413
|
+
/** A preset chip row: the chip matching the current value is highlighted. */
|
|
2414
|
+
function presetRow(presets, value, onPick) {
|
|
2415
|
+
var wrap = el("div", "preset-row");
|
|
2416
|
+
presets.forEach(function (preset) {
|
|
2417
|
+
var chip = el("button", preset.tokens === value ? "preset active" : "preset", preset.label);
|
|
2418
|
+
chip.type = "button";
|
|
2419
|
+
chip.setAttribute("aria-pressed", preset.tokens === value ? "true" : "false");
|
|
2420
|
+
chip.addEventListener("click", function () { onPick(preset.tokens); });
|
|
2421
|
+
wrap.appendChild(chip);
|
|
2422
|
+
});
|
|
2423
|
+
return wrap;
|
|
2424
|
+
}
|
|
2425
|
+
|
|
2426
|
+
/** A labelled field with an optional "?" hint, mirroring the host's layout. */
|
|
2427
|
+
function field(labelKey, helpKey, control) {
|
|
2428
|
+
var wrap = el("div", "field");
|
|
2429
|
+
var head = el("div", "field-head");
|
|
2430
|
+
head.appendChild(el("span", "field-label", t(labelKey)));
|
|
2431
|
+
if (helpKey) {
|
|
2432
|
+
var hint = el("span", "field-help", "?");
|
|
2433
|
+
hint.title = t(helpKey);
|
|
2434
|
+
hint.setAttribute("aria-label", t(helpKey));
|
|
2435
|
+
head.appendChild(hint);
|
|
2436
|
+
}
|
|
2437
|
+
wrap.appendChild(head);
|
|
2438
|
+
wrap.appendChild(control);
|
|
2439
|
+
return wrap;
|
|
2440
|
+
}
|
|
2441
|
+
|
|
2442
|
+
/** A checkbox row: one box, its label, and a change handler. */
|
|
2443
|
+
function checkRow(labelKey, checked, onChange) {
|
|
2444
|
+
var label = el("label", "check");
|
|
2445
|
+
var box = document.createElement("input");
|
|
2446
|
+
box.type = "checkbox";
|
|
2447
|
+
box.checked = checked;
|
|
2448
|
+
box.addEventListener("change", function () { onChange(box.checked); });
|
|
2449
|
+
label.appendChild(box);
|
|
2450
|
+
label.appendChild(el("span", null, t(labelKey)));
|
|
2451
|
+
|
|
2452
|
+
return label;
|
|
2453
|
+
}
|
|
2454
|
+
|
|
2455
|
+
/**
|
|
2456
|
+
* The editable fields of one model, as the host's sheet lays them out.
|
|
2457
|
+
*
|
|
2458
|
+
* Nothing here writes through: every control mutates the draft, and Save sends
|
|
2459
|
+
* the whole set. That is what makes the save bar meaningful and lets a field be
|
|
2460
|
+
* put back to "follow the catalog" by clearing it.
|
|
2461
|
+
*/
|
|
2462
|
+
function modelSheetBody(model, entry) {
|
|
2463
|
+
var body = el("div", "sheet-body");
|
|
2464
|
+
|
|
2465
|
+
// Alias
|
|
2466
|
+
var aliasInput = document.createElement("input");
|
|
2467
|
+
aliasInput.type = "text";
|
|
2468
|
+
aliasInput.className = "text-input";
|
|
2469
|
+
aliasInput.value = entry.alias;
|
|
2470
|
+
aliasInput.placeholder = t("models.aliasPlaceholder");
|
|
2471
|
+
// `main.js` parseOverride() rejects a longer alias outright, and one bad
|
|
2472
|
+
// alias voids the WHOLE selection — so cap the field here, where the user can
|
|
2473
|
+
// see it, instead of letting them lose every model's edits at Save.
|
|
2474
|
+
aliasInput.maxLength = 120;
|
|
2475
|
+
// The save bar tracks the draft, so typing an alias has to refresh it: an
|
|
2476
|
+
// edit that never reveals Save would look like it was not recorded.
|
|
2477
|
+
aliasInput.addEventListener("input", function () {
|
|
2478
|
+
entry.alias = aliasInput.value;
|
|
2479
|
+
paintSaveBar();
|
|
2480
|
+
});
|
|
2481
|
+
body.appendChild(field("models.alias", "models.aliasHelp", aliasInput));
|
|
2482
|
+
|
|
2483
|
+
// Context window and max output, side by side on a wide enough panel.
|
|
2484
|
+
var windowInput = numberInput(effectiveWindow(model, entry), function (next) {
|
|
2485
|
+
// A value equal to the catalog's is stored as "no override", so the row
|
|
2486
|
+
// keeps following the catalog instead of pinning today's number.
|
|
2487
|
+
entry.window = next === model.nativeContextWindow ? null : next;
|
|
2488
|
+
renderChosen();
|
|
2489
|
+
});
|
|
2490
|
+
var windowCol = field("models.window", "models.windowFollows", windowInput);
|
|
2491
|
+
windowCol.insertBefore(presetRow(CONTEXT_WINDOW_PRESETS, effectiveWindow(model, entry), function (tokens) {
|
|
2492
|
+
entry.window = tokens === model.nativeContextWindow ? null : tokens;
|
|
2493
|
+
renderChosen();
|
|
2494
|
+
}), windowInput);
|
|
2495
|
+
|
|
2496
|
+
var outputInput = numberInput(effectiveOutput(model, entry), function (next) {
|
|
2497
|
+
entry.output = next === model.nativeMaxOutputTokens ? null : next;
|
|
2498
|
+
renderChosen();
|
|
2499
|
+
});
|
|
2500
|
+
var outputCol = field("models.maxOutput", "models.outputHelp", outputInput);
|
|
2501
|
+
outputCol.insertBefore(presetRow(MAX_OUTPUT_PRESETS, effectiveOutput(model, entry), function (tokens) {
|
|
2502
|
+
entry.output = tokens === model.nativeMaxOutputTokens ? null : tokens;
|
|
2503
|
+
renderChosen();
|
|
2504
|
+
}), outputInput);
|
|
2505
|
+
|
|
2506
|
+
var pair = el("div", "field-pair");
|
|
2507
|
+
pair.appendChild(windowCol);
|
|
2508
|
+
pair.appendChild(outputCol);
|
|
2509
|
+
body.appendChild(pair);
|
|
2510
|
+
|
|
2511
|
+
// Thinking levels: only a model whose catalog offers a reason menu gets the
|
|
2512
|
+
// control. Trae and Qoder report no levels at all, so a menu there could
|
|
2513
|
+
// never take effect.
|
|
2514
|
+
var levels = effectiveLevels(model, entry);
|
|
2515
|
+
if (levels.length > 0) {
|
|
2516
|
+
var levelsWrap = el("div", "field");
|
|
2517
|
+
var levelsHead = el("div", "field-head");
|
|
2518
|
+
levelsHead.appendChild(el("span", "field-label", t("models.levels")));
|
|
2519
|
+
|
|
2520
|
+
var defaultWrap = el("span", "default-level");
|
|
2521
|
+
defaultWrap.appendChild(el("span", "field-label", t("models.defaultLevel")));
|
|
2522
|
+
var defaultSelect = document.createElement("select");
|
|
2523
|
+
defaultSelect.className = "select";
|
|
2524
|
+
THINKING_LEVELS.forEach(function (level) {
|
|
2525
|
+
if (levels.indexOf(level) < 0) return;
|
|
2526
|
+
var option = document.createElement("option");
|
|
2527
|
+
option.value = level;
|
|
2528
|
+
option.textContent = level;
|
|
2529
|
+
defaultSelect.appendChild(option);
|
|
2530
|
+
});
|
|
2531
|
+
var chosenDefault = entry.defaultLevel !== null ? entry.defaultLevel : (levels[0] || "off");
|
|
2532
|
+
if (levels.indexOf(chosenDefault) < 0) chosenDefault = levels[0] || "off";
|
|
2533
|
+
defaultSelect.value = chosenDefault;
|
|
2534
|
+
defaultSelect.addEventListener("change", function () {
|
|
2535
|
+
entry.defaultLevel = defaultSelect.value;
|
|
2536
|
+
paintSaveBar();
|
|
2537
|
+
});
|
|
2538
|
+
defaultWrap.appendChild(defaultSelect);
|
|
2539
|
+
levelsHead.appendChild(defaultWrap);
|
|
2540
|
+
levelsWrap.appendChild(levelsHead);
|
|
2541
|
+
|
|
2542
|
+
var levelRow = el("div", "level-row");
|
|
2543
|
+
THINKING_LEVELS.forEach(function (level) {
|
|
2544
|
+
var on = levels.indexOf(level) >= 0;
|
|
2545
|
+
var chip = el("button", on ? "level active" : "level", level);
|
|
2546
|
+
chip.type = "button";
|
|
2547
|
+
chip.setAttribute("aria-pressed", on ? "true" : "false");
|
|
2548
|
+
chip.addEventListener("click", function () {
|
|
2549
|
+
var next = levels.slice();
|
|
2550
|
+
var at = next.indexOf(level);
|
|
2551
|
+
if (at >= 0) next.splice(at, 1);
|
|
2552
|
+
else next.push(level);
|
|
2553
|
+
next = THINKING_LEVELS.filter(function (candidate) { return next.indexOf(candidate) >= 0; });
|
|
2554
|
+
// An empty set would mean "no reason menu", which the host cannot
|
|
2555
|
+
// express, so clearing every chip restores the catalog's own set.
|
|
2556
|
+
entry.levels = next.length === 0 ? null : next;
|
|
2557
|
+
if (entry.defaultLevel !== null && entry.levels !== null && entry.levels.indexOf(entry.defaultLevel) < 0) {
|
|
2558
|
+
entry.defaultLevel = null;
|
|
2559
|
+
}
|
|
2560
|
+
renderChosen();
|
|
2561
|
+
});
|
|
2562
|
+
levelRow.appendChild(chip);
|
|
2563
|
+
});
|
|
2564
|
+
levelsWrap.appendChild(levelRow);
|
|
2565
|
+
body.appendChild(levelsWrap);
|
|
2566
|
+
}
|
|
2567
|
+
|
|
2568
|
+
// Attachments
|
|
2569
|
+
var attach = el("div", "field");
|
|
2570
|
+
attach.appendChild(el("div", "field-head", t("models.capabilities")));
|
|
2571
|
+
var attachRow = el("div", "check-row");
|
|
2572
|
+
attachRow.appendChild(checkRow("models.imageInput", effectiveImages(model, entry), function (next) {
|
|
2573
|
+
entry.images = next === model.nativeSupportsImages ? null : next;
|
|
2574
|
+
renderChosen();
|
|
2575
|
+
}));
|
|
2576
|
+
// Three host-only capabilities are deliberately absent rather than shown as
|
|
2577
|
+
// disabled ticks the host never reads (crates/host-core/src/plugins/providers.rs)
|
|
2578
|
+
// — a control that cannot act is worse than no control.
|
|
2579
|
+
attach.appendChild(attachRow);
|
|
2580
|
+
body.appendChild(attach);
|
|
2581
|
+
|
|
2582
|
+
return body;
|
|
2583
|
+
}
|
|
2584
|
+
|
|
2585
|
+
|
|
2586
|
+
/**
|
|
2587
|
+
* The ceiling the host enforces on a token budget (`MAX_TOKEN_BUDGET`).
|
|
2588
|
+
*
|
|
2589
|
+
* A number past it invalidates the whole selection payload there, so the save
|
|
2590
|
+
* drops every edit in this pass rather than clamping one field. Keeping the
|
|
2591
|
+
* bound in the browser means the field itself refuses the value.
|
|
2592
|
+
*/
|
|
2593
|
+
var MAX_TOKEN_BUDGET = 0xffffffff;
|
|
2594
|
+
function numberInput(value, onChange) {
|
|
2595
|
+
var input = document.createElement("input");
|
|
2596
|
+
input.type = "number";
|
|
2597
|
+
input.className = "num-input";
|
|
2598
|
+
input.max = String(MAX_TOKEN_BUDGET);
|
|
2599
|
+
input.min = "1";
|
|
2600
|
+
input.step = "1000";
|
|
2601
|
+
input.value = String(value);
|
|
2602
|
+
input.addEventListener("change", function () {
|
|
2603
|
+
var next = Number(input.value);
|
|
2604
|
+
if (!Number.isFinite(next) || next <= 0 || next > MAX_TOKEN_BUDGET) {
|
|
2605
|
+
input.value = String(value);
|
|
2606
|
+
return;
|
|
2607
|
+
}
|
|
2608
|
+
onChange(Math.round(next));
|
|
2609
|
+
});
|
|
2610
|
+
return input;
|
|
2611
|
+
}
|
|
2612
|
+
|
|
2613
|
+
/** Trim trailing zeros from a decimal string: "1.50" -> "1.5", "1.00" -> "1". */
|
|
2614
|
+
function trimFraction(value) {
|
|
2615
|
+
if (value.indexOf(".") < 0) return value;
|
|
2616
|
+
return value.replace(/0+$/, "").replace(/\.$/, "");
|
|
2617
|
+
}
|
|
2618
|
+
|
|
2619
|
+
/**
|
|
2620
|
+
* A token count, formatted the way the model sheet reads it.
|
|
2621
|
+
*
|
|
2622
|
+
* One decimal at the K scale and two at the M scale, because 1,000,000 /
|
|
2623
|
+
* 1,048,576 / 1,050,000 are indistinguishable at one decimal and rounding them
|
|
2624
|
+
* together would overstate the smaller ones.
|
|
2625
|
+
*/
|
|
2626
|
+
function formatTokens(tokens) {
|
|
2627
|
+
var n = Number(tokens);
|
|
2628
|
+
if (!Number.isFinite(n) || n <= 0) return "—";
|
|
2629
|
+
if (n < 1000) return String(n);
|
|
2630
|
+
var thousands = n / 1000;
|
|
2631
|
+
if (Number(thousands.toFixed(1)) < 1000) {
|
|
2632
|
+
return trimFraction(thousands.toFixed(1)) + "K";
|
|
2633
|
+
}
|
|
2634
|
+
return trimFraction((n / 1000000).toFixed(2)) + "M";
|
|
2635
|
+
}
|
|
2636
|
+
|
|
2637
|
+
/**
|
|
2638
|
+
* A credit balance as one figure, for the source cards' quota row.
|
|
2639
|
+
*
|
|
2640
|
+
* Whole balances read as the pool's own credit total does; a balance with a
|
|
2641
|
+
* fractional part (Trae reports tenths) keeps two decimals rather than being
|
|
2642
|
+
* rounded away in front of the user.
|
|
2643
|
+
*/
|
|
2644
|
+
function formatCredits(value) {
|
|
2645
|
+
var n = Number(value);
|
|
2646
|
+
if (!Number.isFinite(n)) return "—";
|
|
2647
|
+
return Number.isInteger(n) ? n.toLocaleString() : n.toLocaleString(undefined, { maximumFractionDigits: 2 });
|
|
2648
|
+
}
|
|
2649
|
+
|
|
2650
|
+
/**
|
|
2651
|
+
* Show or hide the save bar and keep its buttons in step with `busy`.
|
|
2652
|
+
*
|
|
2653
|
+
* Both branches have to write the button state. A save that makes the draft
|
|
2654
|
+
* clean (deselecting a model, say) hides the bar on the very render that ends
|
|
2655
|
+
* the save, so an early return here would leave the button reading "saving"
|
|
2656
|
+
* and disabled forever - the data lands, but the panel never says so.
|
|
2657
|
+
*/
|
|
2658
|
+
function renderSaveBar(dirty) {
|
|
2659
|
+
var saving = busy === "save";
|
|
2660
|
+
show($("savebar"), dirty === true);
|
|
2661
|
+
$("saveBtn").textContent = saving ? t("models.saving") : t("btn.save");
|
|
2662
|
+
$("discardBtn").textContent = t("btn.discard");
|
|
2663
|
+
$("saveBtn").disabled = busy !== null;
|
|
2664
|
+
$("discardBtn").disabled = busy !== null;
|
|
2665
|
+
if (dirty === true) $("saveNote").textContent = t("models.dirty");
|
|
2666
|
+
}
|
|
2667
|
+
/** Repaint the save bar from the page's own stored selection, not the pool's. */
|
|
2668
|
+
function paintSaveBar() {
|
|
2669
|
+
var page = modelPage();
|
|
2670
|
+
renderSaveBar(isDirty(page.list, page.selection));
|
|
2671
|
+
}
|
|
2672
|
+
|
|
2673
|
+
/**
|
|
2674
|
+
* The gateways are up and this plugin declared them, but the host refused the
|
|
2675
|
+
* registration - the usual case being a reload that raced the last declaration.
|
|
2676
|
+
* The state document carries the ids that are still missing, and a plugin
|
|
2677
|
+
* reload re-runs the registration, so that is what the line asks for. Nothing
|
|
2678
|
+
* is published by the panel to fix it.
|
|
2679
|
+
*/
|
|
2680
|
+
function renderHostHint() {
|
|
2681
|
+
var missing = state && Array.isArray(state.unpublishedDeclarations) ? state.unpublishedDeclarations : [];
|
|
2682
|
+
show($("hostHint"), missing.length > 0);
|
|
2683
|
+
if (missing.length === 0) return;
|
|
2684
|
+
$("hostHint").textContent = t("host.missing", { count: missing.length });
|
|
2685
|
+
}
|
|
2686
|
+
|
|
2687
|
+
function renderDoctor() {
|
|
2688
|
+
$("checkTitle").textContent = t("check.title");
|
|
2689
|
+
$("doctorBtn").textContent = t("btn.doctor");
|
|
2690
|
+
$("copyBtn").textContent = t("btn.copy");
|
|
2691
|
+
$("doctorBtn").disabled = busy !== null;
|
|
2692
|
+
$("copyBtn").disabled = doctorResult === null;
|
|
2693
|
+
|
|
2694
|
+
var host = $("findings");
|
|
2695
|
+
clear(host);
|
|
2696
|
+
if (doctorResult === null) {
|
|
2697
|
+
host.appendChild(el("div", "hint", t("check.empty")));
|
|
2698
|
+
$("report").textContent = "";
|
|
2699
|
+
return;
|
|
2700
|
+
}
|
|
2701
|
+
(doctorResult.findings || []).forEach(function (finding) {
|
|
2702
|
+
var row = el("div", "finding");
|
|
2703
|
+
row.appendChild(el("span", finding.ok ? "finding-pass" : "finding-fail", t(finding.ok ? "check.pass" : "check.fail")));
|
|
2704
|
+
row.appendChild(el("span", "finding-id", finding.id));
|
|
2705
|
+
row.appendChild(el("span", "finding-detail", finding.detail));
|
|
2706
|
+
host.appendChild(row);
|
|
2707
|
+
});
|
|
2708
|
+
$("report").textContent = doctorResult.report || "";
|
|
2709
|
+
}
|
|
2710
|
+
|
|
2711
|
+
/**
|
|
2712
|
+
* The endpoint the footer names: the pool's own shim for WorkBuddy, that
|
|
2713
|
+
* source's gateway for Trae and Qoder. A WorkBuddy address says nothing about
|
|
2714
|
+
* the model list above it, which is what the page is about.
|
|
2715
|
+
*/
|
|
2716
|
+
function footerEndpoint() {
|
|
2717
|
+
if (activeSource === "workbuddy") {
|
|
2718
|
+
var shim = state.shims ? state.shims[activeRegion] : null;
|
|
2719
|
+
return shim && shim.running
|
|
2720
|
+
? { on: true, text: t("foot.shim", { url: shim.baseUrl }) }
|
|
2721
|
+
: { on: false, text: t("foot.shimOff") };
|
|
2722
|
+
}
|
|
2723
|
+
var row = sourceGatewayRow(activeSource, activeRegionOf(activeSource));
|
|
2724
|
+
return row
|
|
2725
|
+
? { on: true, text: t("foot.shim", { url: "http://127.0.0.1:" + row.port + "/v1" }) }
|
|
2726
|
+
: { on: false, text: t("foot.shimOff") };
|
|
2727
|
+
}
|
|
2728
|
+
|
|
2729
|
+
function renderFooter() {
|
|
2730
|
+
var foot = $("foot");
|
|
2731
|
+
clear(foot);
|
|
2732
|
+
if (state === null) return;
|
|
2733
|
+
var endpoint = footerEndpoint();
|
|
2734
|
+
foot.appendChild(el("span", endpoint.on ? null : "muted", endpoint.text));
|
|
2735
|
+
// Either the gateway could not sign in, or it is still serving a roster from
|
|
2736
|
+
// an earlier read. Both are worth saying out loud; the message leads with the
|
|
2737
|
+
// reason so it does not have to guess which of the two it is. Only the pool's
|
|
2738
|
+
// gateway carries one, and only while the pool is on screen: the other two
|
|
2739
|
+
// report theirs in their own strip.
|
|
2740
|
+
var rosterWarning = activeSource === "workbuddy" && state.warnings ? state.warnings[activeRegion] : undefined;
|
|
2741
|
+
if (rosterWarning) {
|
|
2742
|
+
foot.appendChild(el("span", "muted",
|
|
2743
|
+
t("foot.rosterStale", { region: regionLabel(activeRegion, false), detail: rosterWarning })));
|
|
2744
|
+
}
|
|
2745
|
+
}
|
|
2746
|
+
|
|
2747
|
+
function render() {
|
|
2748
|
+
// Every render pass rebuilds most of the panel, so the element holding focus
|
|
2749
|
+
// is destroyed and the browser scrolls its replacement into view — the shake a
|
|
2750
|
+
// click produces. Record what had focus and where the page was, and put both
|
|
2751
|
+
// back afterwards. The same guard the account and model columns already use,
|
|
2752
|
+
// lifted to the whole panel.
|
|
2753
|
+
var keep = placeOf(document.body);
|
|
2754
|
+
renderHeader();
|
|
2755
|
+
renderAutoCheckin();
|
|
2756
|
+
renderComposerPoints();
|
|
2757
|
+
renderHostHint();
|
|
2758
|
+
renderRegionTabs();
|
|
2759
|
+
renderModelRegionTabs();
|
|
2760
|
+
renderWorkbuddyHead();
|
|
2761
|
+
renderPoolStrip();
|
|
2762
|
+
renderSourceStrip("trae");
|
|
2763
|
+
renderSourceStrip("qoder");
|
|
2764
|
+
renderAccounts();
|
|
2765
|
+
renderModels();
|
|
2766
|
+
renderDoctor();
|
|
2767
|
+
renderFooter();
|
|
2768
|
+
restorePlace(document.body, keep);
|
|
2769
|
+
// A control the pass itself disabled cannot be refocused — the browser refuses
|
|
2770
|
+
// focus on a disabled element, and no amount of clearing the flag inside one
|
|
2771
|
+
// task changes that. So the control the user last pressed is remembered, and
|
|
2772
|
+
// when the busy pass ends the focus is handed back to it. Without this the
|
|
2773
|
+
// focus sat on the body after every click, and keyboard users lost their place.
|
|
2774
|
+
// The memory is cleared once the focus is back, so a later click on empty space
|
|
2775
|
+
// stays where it was instead of being pulled onto that button by the next poll.
|
|
2776
|
+
if (lastPressed !== null && document.activeElement === document.body) {
|
|
2777
|
+
var back = locateControl(document.body, lastPressed);
|
|
2778
|
+
if (back && back.disabled === false && back.focus) {
|
|
2779
|
+
back.focus({ preventScroll: true });
|
|
2780
|
+
lastPressed = null;
|
|
2781
|
+
}
|
|
2782
|
+
}
|
|
2783
|
+
}
|
|
2784
|
+
|
|
2785
|
+
// ---------------------------------------------------------------- actions
|
|
2786
|
+
|
|
2787
|
+
var stateRevision = 0;
|
|
2788
|
+
|
|
2789
|
+
function adopt(next) {
|
|
2790
|
+
if (next && typeof next === "object") {
|
|
2791
|
+
state = next;
|
|
2792
|
+
stateRevision += 1;
|
|
2793
|
+
}
|
|
2794
|
+
}
|
|
2795
|
+
|
|
2796
|
+
function runBusy(id, work) {
|
|
2797
|
+
if (busy !== null) return Promise.reject(new Error("panel is busy"));
|
|
2798
|
+
notePress(document.activeElement);
|
|
2799
|
+
busy = id;
|
|
2800
|
+
render();
|
|
2801
|
+
return work().then(function (result) {
|
|
2802
|
+
busy = null;
|
|
2803
|
+
if (result && result.accounts) adopt(result);
|
|
2804
|
+
note(null);
|
|
2805
|
+
render();
|
|
2806
|
+
pollCheckinLog(0);
|
|
2807
|
+
return result;
|
|
2808
|
+
}).catch(function (error) {
|
|
2809
|
+
busy = null;
|
|
2810
|
+
note(String(error && error.message ? error.message : error));
|
|
2811
|
+
render();
|
|
2812
|
+
pollCheckinLog(0);
|
|
2813
|
+
throw error;
|
|
2814
|
+
});
|
|
2815
|
+
}
|
|
2816
|
+
|
|
2817
|
+
/**
|
|
2818
|
+
* Panel poll timer for the background detection pass.
|
|
2819
|
+
*
|
|
2820
|
+
* The main process answers an open from cache and refreshes behind it, so the
|
|
2821
|
+
* panel has to come back for the refreshed document rather than assume the
|
|
2822
|
+
* first answer was final.
|
|
2823
|
+
*/
|
|
2824
|
+
var refreshPoll = null;
|
|
2825
|
+
var REFRESH_POLL_MS = 1200;
|
|
2826
|
+
|
|
2827
|
+
function stopPolling() {
|
|
2828
|
+
if (refreshPoll !== null) {
|
|
2829
|
+
window.clearTimeout(refreshPoll);
|
|
2830
|
+
refreshPoll = null;
|
|
2831
|
+
}
|
|
2832
|
+
}
|
|
2833
|
+
|
|
2834
|
+
/**
|
|
2835
|
+
* Adopt one document, then keep asking while the main process reports that a
|
|
2836
|
+
* detection pass is still running. Bounded so a permanently failing refresh
|
|
2837
|
+
* cannot spin forever.
|
|
2838
|
+
*/
|
|
2839
|
+
function adoptAndMaybePoll(next, attempts) {
|
|
2840
|
+
adopt(next);
|
|
2841
|
+
render();
|
|
2842
|
+
if (!next || next.refreshing !== true) { stopPolling(); return; }
|
|
2843
|
+
if (attempts >= 15) { stopPolling(); return; }
|
|
2844
|
+
stopPolling();
|
|
2845
|
+
refreshPoll = window.setTimeout(function () {
|
|
2846
|
+
refreshPoll = null;
|
|
2847
|
+
var revision = stateRevision;
|
|
2848
|
+
invoke("xd.state").then(function (again) {
|
|
2849
|
+
if (revision !== stateRevision) {
|
|
2850
|
+
if (state && state.refreshing === true) adoptAndMaybePoll(state, attempts + 1);
|
|
2851
|
+
return;
|
|
2852
|
+
}
|
|
2853
|
+
adoptAndMaybePoll(again, attempts + 1);
|
|
2854
|
+
}).catch(function () { stopPolling(); });
|
|
2855
|
+
}, REFRESH_POLL_MS);
|
|
2856
|
+
}
|
|
2857
|
+
|
|
2858
|
+
var checkinPoll = null;
|
|
2859
|
+
var CHECKIN_POLL_MS = 2000;
|
|
2860
|
+
var CHECKIN_POLL_LIMIT = 90;
|
|
2861
|
+
|
|
2862
|
+
function stopCheckinPoll() {
|
|
2863
|
+
if (checkinPoll !== null) {
|
|
2864
|
+
window.clearTimeout(checkinPoll);
|
|
2865
|
+
checkinPoll = null;
|
|
2866
|
+
}
|
|
2867
|
+
}
|
|
2868
|
+
|
|
2869
|
+
/** Keep reading the log while a check-in pass is still marked pending. */
|
|
2870
|
+
function pollCheckinLog(attempts) {
|
|
2871
|
+
var last = state && state.autoCheckin && state.autoCheckin.last;
|
|
2872
|
+
if (!last || last.pending !== true) { stopCheckinPoll(); return; }
|
|
2873
|
+
if (busy !== null && busy !== "checkin") { stopCheckinPoll(); return; }
|
|
2874
|
+
if (attempts >= CHECKIN_POLL_LIMIT) { stopCheckinPoll(); return; }
|
|
2875
|
+
if (checkinPoll !== null) return;
|
|
2876
|
+
checkinPoll = window.setTimeout(function () {
|
|
2877
|
+
checkinPoll = null;
|
|
2878
|
+
var revision = stateRevision;
|
|
2879
|
+
invoke("xd.state").then(function (again) {
|
|
2880
|
+
if (revision !== stateRevision) { pollCheckinLog(attempts + 1); return; }
|
|
2881
|
+
if (busy !== null && busy !== "checkin") return;
|
|
2882
|
+
adopt(again);
|
|
2883
|
+
render();
|
|
2884
|
+
pollCheckinLog(attempts + 1);
|
|
2885
|
+
}).catch(function () { stopCheckinPoll(); });
|
|
2886
|
+
}, CHECKIN_POLL_MS);
|
|
2887
|
+
}
|
|
2888
|
+
|
|
2889
|
+
function refresh() {
|
|
2890
|
+
return runBusy("refresh", function () {
|
|
2891
|
+
// The Refresh button asks for a completed detection pass, not the cache.
|
|
2892
|
+
return invoke("xd.state", { wait: true });
|
|
2893
|
+
}).catch(function () {});
|
|
2894
|
+
}
|
|
2895
|
+
|
|
2896
|
+
function rescan() {
|
|
2897
|
+
return runBusy("rescan", function () { return invoke("xd.rescan"); })
|
|
2898
|
+
.then(function (next) { announce(t("pool.summary", {
|
|
2899
|
+
count: (next.accounts || []).length,
|
|
2900
|
+
cooling: (next.accounts || []).filter(function (a) { return a.cooling; }).length,
|
|
2901
|
+
})); })
|
|
2902
|
+
.catch(function () {});
|
|
2903
|
+
}
|
|
2904
|
+
|
|
2905
|
+
function clearCooldowns() {
|
|
2906
|
+
return runBusy("reset", function () { return invoke("xd.resetCooldowns"); }).catch(function () {});
|
|
2907
|
+
}
|
|
2908
|
+
|
|
2909
|
+
function setPreferredAccount(accountId) {
|
|
2910
|
+
return runBusy("account", function () {
|
|
2911
|
+
return invoke("xd.preferAccount", { region: activeRegion, accountId: accountId });
|
|
2912
|
+
}).catch(function () {});
|
|
2913
|
+
}
|
|
2914
|
+
|
|
2915
|
+
/**
|
|
2916
|
+
* Switch one account off, on, out of the pool, or back into it.
|
|
2917
|
+
*
|
|
2918
|
+
* Every branch answers with the whole status document, so the panel redraws
|
|
2919
|
+
* from what was actually stored rather than from what was clicked.
|
|
2920
|
+
*/
|
|
2921
|
+
function setAccountState(accountId, next) {
|
|
2922
|
+
if (busy !== null) return Promise.resolve();
|
|
2923
|
+
return runBusy("account", function () {
|
|
2924
|
+
return invoke("xd.setAccountState", { region: activeRegion, accountId: accountId, state: next });
|
|
2925
|
+
}).catch(function (error) {
|
|
2926
|
+
note(t("acct.stateError", { message: error && error.message ? error.message : String(error) }));
|
|
2927
|
+
});
|
|
2928
|
+
}
|
|
2929
|
+
|
|
2930
|
+
/** Save the credit floor for one account; 0 clears it. */
|
|
2931
|
+
function setCreditReserve(accountId, reserve) {
|
|
2932
|
+
if (busy !== null) return Promise.resolve();
|
|
2933
|
+
return runBusy("reserve", function () {
|
|
2934
|
+
return invoke("xd.setCreditReserve", { region: activeRegion, accountId: accountId, reserve: reserve }).then(function (next) {
|
|
2935
|
+
delete reserveDrafts[accountId];
|
|
2936
|
+
return next;
|
|
2937
|
+
});
|
|
2938
|
+
}).catch(function (error) {
|
|
2939
|
+
note(t("acct.stateError", { message: error && error.message ? error.message : String(error) }));
|
|
2940
|
+
});
|
|
2941
|
+
}
|
|
2942
|
+
|
|
2943
|
+
/** Save one source's usage mode; WorkBuddy's pool applies it live. */
|
|
2944
|
+
function setDistribution(source, next) {
|
|
2945
|
+
var workbuddy = source === "workbuddy";
|
|
2946
|
+
return runBusy("distribution", function () {
|
|
2947
|
+
return invoke(
|
|
2948
|
+
workbuddy ? "xd.distribution" : "xd.sourceDistribution",
|
|
2949
|
+
workbuddy ? { region: activeRegion, distribution: next } : { source: source, distribution: next }
|
|
2950
|
+
);
|
|
2951
|
+
}).catch(function () {});
|
|
2952
|
+
}
|
|
2953
|
+
|
|
2954
|
+
function setComposerPoints(enabled) {
|
|
2955
|
+
return runBusy("composer", function () {
|
|
2956
|
+
return invoke("xd.setComposerPoints", { enabled: enabled === true });
|
|
2957
|
+
}).catch(function () {});
|
|
2958
|
+
}
|
|
2959
|
+
|
|
2960
|
+
/** Switch one source (workbuddy / trae / qoder) on or off; the answer is the refreshed document. */
|
|
2961
|
+
function setSource(source, enabled) {
|
|
2962
|
+
return runBusy("source", function () {
|
|
2963
|
+
return invoke("xd.setSource", { source: source, enabled: enabled });
|
|
2964
|
+
}).catch(function () {});
|
|
2965
|
+
}
|
|
2966
|
+
|
|
2967
|
+
|
|
2968
|
+
/**
|
|
2969
|
+
* Read the hours box: a comma-separated list of whole hours, or null when
|
|
2970
|
+
* something in it is not an hour. An empty box means "keep the default", which
|
|
2971
|
+
* is what the background side does with an empty list too.
|
|
2972
|
+
*/
|
|
2973
|
+
function parseCheckinHours(text) {
|
|
2974
|
+
var parts = String(text).split(",").map(function (part) { return part.trim(); })
|
|
2975
|
+
.filter(function (part) { return part !== ""; });
|
|
2976
|
+
var hours = [];
|
|
2977
|
+
for (var i = 0; i < parts.length; i += 1) {
|
|
2978
|
+
if (!/^\d{1,2}$/.test(parts[i])) return null;
|
|
2979
|
+
var hour = Number(parts[i]);
|
|
2980
|
+
if (hour > 23) return null;
|
|
2981
|
+
if (hours.indexOf(hour) < 0) hours.push(hour);
|
|
2982
|
+
}
|
|
2983
|
+
return hours;
|
|
2984
|
+
}
|
|
2985
|
+
|
|
2986
|
+
/** Switch the daily automatic check-in, or move its hours, in one request. */
|
|
2987
|
+
function setAutoCheckin(enabled, hours) {
|
|
2988
|
+
if (hours === null) {
|
|
2989
|
+
// Only switching ON needs a readable hour list. Switching OFF must not: the
|
|
2990
|
+
// box can hold stale invalid text from an earlier edit, and bailing out there
|
|
2991
|
+
// leaves the timer running while the panel reports the schedule as off.
|
|
2992
|
+
if (enabled === true) {
|
|
2993
|
+
note(t("auto.badHours"));
|
|
2994
|
+
return Promise.resolve();
|
|
2995
|
+
}
|
|
2996
|
+
var current = state && state.autoCheckin && state.autoCheckin.hours;
|
|
2997
|
+
hours = Array.isArray(current) ? current : [];
|
|
2998
|
+
}
|
|
2999
|
+
return runBusy("autoCheckin", function () {
|
|
3000
|
+
return invoke("xd.setAutoCheckin", { enabled: enabled === true, hours: hours });
|
|
3001
|
+
}).catch(function () {});
|
|
3002
|
+
}
|
|
3003
|
+
function claimCheckin(accountId) {
|
|
3004
|
+
// The same guard the other three claim paths carry: a claim started on top of
|
|
3005
|
+
// a rescan or a refresh is two passes writing the same document.
|
|
3006
|
+
if (busy !== null || checkinBusy !== null) return Promise.resolve();
|
|
3007
|
+
checkinBusy = accountId;
|
|
3008
|
+
render();
|
|
3009
|
+
return invoke("xd.checkin", { accountId: accountId }).then(function (payload) {
|
|
3010
|
+
checkinBusy = null;
|
|
3011
|
+
if (payload && payload.state) adopt(payload.state);
|
|
3012
|
+
note(null);
|
|
3013
|
+
render();
|
|
3014
|
+
if (payload && payload.result && payload.result.credit > 0) {
|
|
3015
|
+
announce(t("checkin.claimed", { credit: payload.result.credit }));
|
|
3016
|
+
}
|
|
3017
|
+
}).catch(function (error) {
|
|
3018
|
+
checkinBusy = null;
|
|
3019
|
+
note(String(error && error.message ? error.message : error));
|
|
3020
|
+
render();
|
|
3021
|
+
});
|
|
3022
|
+
}
|
|
3023
|
+
|
|
3024
|
+
/** Collect every account's daily reward in one request. */
|
|
3025
|
+
function claimAllCheckins() {
|
|
3026
|
+
if (busy !== null || checkinBusy !== null) return Promise.resolve();
|
|
3027
|
+
checkinBusy = "all";
|
|
3028
|
+
render();
|
|
3029
|
+
return invoke("xd.checkinAll", { source: "workbuddy" }).then(function (payload) {
|
|
3030
|
+
checkinBusy = null;
|
|
3031
|
+
if (payload && payload.state) adopt(payload.state);
|
|
3032
|
+
note(null);
|
|
3033
|
+
render();
|
|
3034
|
+
if (payload && payload.running === true) {
|
|
3035
|
+
announce(t("pool.checkinAllBusy"));
|
|
3036
|
+
return;
|
|
3037
|
+
}
|
|
3038
|
+
var claimed = payload && typeof payload.claimed === "number" ? payload.claimed : 0;
|
|
3039
|
+
announce(claimed > 0 ? t("pool.checkinAllDone", { count: claimed }) : t("pool.checkinAllNone"));
|
|
3040
|
+
}).catch(function (error) {
|
|
3041
|
+
checkinBusy = null;
|
|
3042
|
+
note(String(error && error.message ? error.message : error));
|
|
3043
|
+
render();
|
|
3044
|
+
});
|
|
3045
|
+
}
|
|
3046
|
+
|
|
3047
|
+
/**
|
|
3048
|
+
* Tick or untick every row the current filter shows.
|
|
3049
|
+
*
|
|
3050
|
+
* Scoped to the visible rows on purpose: the header box sits above a filtered
|
|
3051
|
+
* list, so "select all" that touched hidden rows would contradict what is on
|
|
3052
|
+
* screen (the host does the same).
|
|
3053
|
+
*/
|
|
3054
|
+
function toggleAllVisible() {
|
|
3055
|
+
var wanted = $("modelSelectAll").checked;
|
|
3056
|
+
var filter = $("modelSearch").value.trim().toLowerCase();
|
|
3057
|
+
modelPage().list.forEach(function (model) {
|
|
3058
|
+
if (filter) {
|
|
3059
|
+
var hit = String(model.id).toLowerCase().indexOf(filter) >= 0
|
|
3060
|
+
|| String(model.name).toLowerCase().indexOf(filter) >= 0
|
|
3061
|
+
|| String(model.alias || "").toLowerCase().indexOf(filter) >= 0;
|
|
3062
|
+
if (!hit) return;
|
|
3063
|
+
}
|
|
3064
|
+
var entry = draft[model.id];
|
|
3065
|
+
if (entry) entry.enabled = wanted;
|
|
3066
|
+
});
|
|
3067
|
+
renderModels();
|
|
3068
|
+
}
|
|
3069
|
+
|
|
3070
|
+
/**
|
|
3071
|
+
* Fetch the pool's list: re-read the desktop sign-ins, then re-read its
|
|
3072
|
+
* catalog from the upstream.
|
|
3073
|
+
*
|
|
3074
|
+
* Both halves matter here. The pool's roster comes from WorkBuddy's own
|
|
3075
|
+
* catalogue, which is read per signed-in account, so a new account and a
|
|
3076
|
+
* changed upstream roster are discovered by the same gesture - which is what
|
|
3077
|
+
* the button reads as. Trae and Qoder re-read their own directory on their
|
|
3078
|
+
* clock, so this button is theirs to hide, not to fake.
|
|
3079
|
+
*/
|
|
3080
|
+
|
|
3081
|
+
function fetchModelList() {
|
|
3082
|
+
return runBusy("fetch", function () { return invoke("xd.rescan"); }).catch(function () {});
|
|
3083
|
+
}
|
|
3084
|
+
function discardDraft() {
|
|
3085
|
+
// Only the page in view: another page's unsaved edits are not this button's to
|
|
3086
|
+
// throw away.
|
|
3087
|
+
if (draftSlot !== null) delete drafts[draftSlot];
|
|
3088
|
+
draft = null;
|
|
3089
|
+
draftSlot = null;
|
|
3090
|
+
renderModels();
|
|
3091
|
+
note(null);
|
|
3092
|
+
}
|
|
3093
|
+
|
|
3094
|
+
function saveModels() {
|
|
3095
|
+
if (busy !== null) return;
|
|
3096
|
+
var page = modelPage();
|
|
3097
|
+
var submittedSlot = draftSlot;
|
|
3098
|
+
var enabledModelIds = [];
|
|
3099
|
+
var overrides = {};
|
|
3100
|
+
page.list.forEach(function (model) {
|
|
3101
|
+
var entry = draft[model.id];
|
|
3102
|
+
if (!entry) return;
|
|
3103
|
+
if (entry.enabled) enabledModelIds.push(model.id);
|
|
3104
|
+
// Only a field that differs from the catalog is stored: an untouched row
|
|
3105
|
+
// must not freeze today's catalog numbers into the settings file.
|
|
3106
|
+
var override = {};
|
|
3107
|
+
if (entry.alias !== "") override.alias = entry.alias.trim();
|
|
3108
|
+
if (entry.window !== null) override.contextWindow = entry.window;
|
|
3109
|
+
if (entry.output !== null) override.maxOutputTokens = entry.output;
|
|
3110
|
+
if (entry.images !== null) override.supportsImages = entry.images;
|
|
3111
|
+
if (entry.levels !== null) override.thinkingLevels = entry.levels;
|
|
3112
|
+
if (entry.defaultLevel !== null) override.defaultThinkingLevel = entry.defaultLevel;
|
|
3113
|
+
if (Object.keys(override).length > 0) overrides[model.id] = override;
|
|
3114
|
+
});
|
|
3115
|
+
|
|
3116
|
+
if (enabledModelIds.length === 0) {
|
|
3117
|
+
note(t("models.zeroEnabled"));
|
|
3118
|
+
return;
|
|
3119
|
+
}
|
|
3120
|
+
|
|
3121
|
+
busy = "save";
|
|
3122
|
+
render();
|
|
3123
|
+
invoke("xd.saveModels", {
|
|
3124
|
+
gateway: page.key,
|
|
3125
|
+
selection: { enabledModelIds: enabledModelIds, overrides: overrides },
|
|
3126
|
+
}).then(function (next) {
|
|
3127
|
+
busy = null;
|
|
3128
|
+
adopt(next);
|
|
3129
|
+
// Saved, so this page's slot goes: coming back rebuilds from what was just
|
|
3130
|
+
// stored rather than from a draft that is no longer pending.
|
|
3131
|
+
if (submittedSlot !== null) delete drafts[submittedSlot];
|
|
3132
|
+
draft = null;
|
|
3133
|
+
draftSlot = null;
|
|
3134
|
+
render();
|
|
3135
|
+
announce(t("models.saved"));
|
|
3136
|
+
}).catch(function (error) {
|
|
3137
|
+
busy = null;
|
|
3138
|
+
note(t("models.saveError", { message: String(error && error.message ? error.message : error) }));
|
|
3139
|
+
render();
|
|
3140
|
+
});
|
|
3141
|
+
}
|
|
3142
|
+
|
|
3143
|
+
function runDoctor() {
|
|
3144
|
+
return runBusy("doctor", function () { return invoke("xd.doctor"); }).then(function (result) {
|
|
3145
|
+
doctorResult = result;
|
|
3146
|
+
render();
|
|
3147
|
+
}).catch(function () {});
|
|
3148
|
+
}
|
|
3149
|
+
|
|
3150
|
+
/**
|
|
3151
|
+
* Copy the doctor report.
|
|
3152
|
+
*
|
|
3153
|
+
* The browser's own clipboard is the only writer here: this page is served over
|
|
3154
|
+
* a loopback address, which is a secure context, and the click that got here is
|
|
3155
|
+
* the user gesture it needs. When it is refused the report is selected in the
|
|
3156
|
+
* page instead, so Ctrl+C still gets it out.
|
|
3157
|
+
*/
|
|
3158
|
+
function copyReport() {
|
|
3159
|
+
if (doctorResult === null) return;
|
|
3160
|
+
var text = doctorResult.report || "";
|
|
3161
|
+
// Takes the key to show, not a boolean: the two failure cases need different
|
|
3162
|
+
// words, and translating here keeps every message in the dictionary.
|
|
3163
|
+
var done = function (key) { $("copyHint").textContent = t(key); };
|
|
3164
|
+
// The report is a <pre>, which has neither select() nor setSelectionRange():
|
|
3165
|
+
// both are input-only. A Range over its contents is what actually selects
|
|
3166
|
+
// rendered text, and that is the only way to make the hint's promise true.
|
|
3167
|
+
var selectReport = function () {
|
|
3168
|
+
var box = $("report");
|
|
3169
|
+
if (!box || typeof window.getSelection !== "function" || typeof document.createRange !== "function") return false;
|
|
3170
|
+
try {
|
|
3171
|
+
var range = document.createRange();
|
|
3172
|
+
range.selectNodeContents(box);
|
|
3173
|
+
var selection = window.getSelection();
|
|
3174
|
+
selection.removeAllRanges();
|
|
3175
|
+
selection.addRange(range);
|
|
3176
|
+
return selection.toString() !== "";
|
|
3177
|
+
} catch (error) {
|
|
3178
|
+
return false;
|
|
3179
|
+
}
|
|
3180
|
+
};
|
|
3181
|
+
// The hint claims the report is selected, so say so only when it really is:
|
|
3182
|
+
// an unselected report would send the user looking for something that is
|
|
3183
|
+
// not there.
|
|
3184
|
+
var failed = function () { done(selectReport() ? "copy.failedSelected" : "copy.failed"); };
|
|
3185
|
+
if (navigator.clipboard && typeof navigator.clipboard.writeText === "function") {
|
|
3186
|
+
navigator.clipboard.writeText(text).then(function () { done("copy.ok"); }, failed);
|
|
3187
|
+
return;
|
|
3188
|
+
}
|
|
3189
|
+
failed();
|
|
3190
|
+
}
|
|
3191
|
+
|
|
3192
|
+
// -------------------------------------------------------------------- tabs
|
|
3193
|
+
|
|
3194
|
+
function selectTab(name) {
|
|
3195
|
+
var views = { pool: "poolView", models: "modelsView", check: "checkView" };
|
|
3196
|
+
Object.keys(views).forEach(function (key) {
|
|
3197
|
+
show($(views[key]), key === name);
|
|
3198
|
+
});
|
|
3199
|
+
var tabs = document.querySelectorAll("#viewTabs .subtab");
|
|
3200
|
+
for (var i = 0; i < tabs.length; i += 1) {
|
|
3201
|
+
var isActive = tabs[i].getAttribute("data-tab") === name;
|
|
3202
|
+
tabs[i].className = isActive ? "subtab active" : "subtab";
|
|
3203
|
+
tabs[i].setAttribute("aria-selected", isActive ? "true" : "false");
|
|
3204
|
+
}
|
|
3205
|
+
if (name === "check" && doctorResult === null) runDoctor();
|
|
3206
|
+
}
|
|
3207
|
+
|
|
3208
|
+
function labelTabs() {
|
|
3209
|
+
var tabs = document.querySelectorAll("#viewTabs .subtab");
|
|
3210
|
+
for (var i = 0; i < tabs.length; i += 1) {
|
|
3211
|
+
var key = tabs[i].getAttribute("data-tab");
|
|
3212
|
+
tabs[i].textContent = t("tab." + key);
|
|
3213
|
+
}
|
|
3214
|
+
// The footer links are static markup, so their labels follow the locale here
|
|
3215
|
+
// rather than through the render pass.
|
|
3216
|
+
var labels = document.querySelectorAll(".foot-links [data-foot-label]");
|
|
3217
|
+
for (var j = 0; j < labels.length; j += 1) {
|
|
3218
|
+
labels[j].textContent = t("foot." + labels[j].getAttribute("data-foot-label"));
|
|
3219
|
+
}
|
|
3220
|
+
}
|
|
3221
|
+
|
|
3222
|
+
// -------------------------------------------------------------------- init
|
|
3223
|
+
|
|
3224
|
+
function bind() {
|
|
3225
|
+
$("refreshBtn").addEventListener("click", refresh);
|
|
3226
|
+
$("rescanBtn").addEventListener("click", rescan);
|
|
3227
|
+
$("resetBtn").addEventListener("click", clearCooldowns);
|
|
3228
|
+
$("doctorBtn").addEventListener("click", runDoctor);
|
|
3229
|
+
$("copyBtn").addEventListener("click", copyReport);
|
|
3230
|
+
$("saveBtn").addEventListener("click", saveModels);
|
|
3231
|
+
$("discardBtn").addEventListener("click", discardDraft);
|
|
3232
|
+
$("modelSearch").addEventListener("input", renderModels);
|
|
3233
|
+
$("chosenSearch").addEventListener("input", renderChosen);
|
|
3234
|
+
$("modelSelectAll").addEventListener("change", toggleAllVisible);
|
|
3235
|
+
$("modelFetchBtn").addEventListener("click", fetchModelList);
|
|
3236
|
+
// The composer readout switch: bound once here because the button is a static
|
|
3237
|
+
// node, and the new state is the opposite of what the last render said.
|
|
3238
|
+
var composerToggle = $("showComposerPoints");
|
|
3239
|
+
if (composerToggle) {
|
|
3240
|
+
composerToggle.addEventListener("click", function () {
|
|
3241
|
+
setComposerPoints(state === null || state.showComposerPoints !== true);
|
|
3242
|
+
});
|
|
3243
|
+
}
|
|
3244
|
+
// Bound once, with the state read at click time: this is a static node, so the
|
|
3245
|
+
// handler cannot live in the render pass that redraws it.
|
|
3246
|
+
$("autoHoursSave").addEventListener("click", function () {
|
|
3247
|
+
var auto = (state && state.autoCheckin) || { enabled: false };
|
|
3248
|
+
setAutoCheckin(auto.enabled === true, parseCheckinHours($("autoHours").value));
|
|
3249
|
+
});
|
|
3250
|
+
// The manual pass runs the same work the scheduled hour runs, so it is busy for
|
|
3251
|
+
// as long as that takes — the button reports it rather than sitting idle.
|
|
3252
|
+
$("autoRunNow").addEventListener("click", function () {
|
|
3253
|
+
runBusy("checkin", function () { return invoke("xd.runCheckinNow"); })
|
|
3254
|
+
.then(function () { pollCheckinLog(0); })
|
|
3255
|
+
.catch(function () {});
|
|
3256
|
+
});
|
|
3257
|
+
|
|
3258
|
+
// One listener for every control in the panel, rather than one per button: the
|
|
3259
|
+
// render pass rebuilds most of them, so per-button listeners would have to be
|
|
3260
|
+
// re-attached on every pass. Capture phase, so it runs before the button's own
|
|
3261
|
+
// handler and records the press even when that handler is about to disable it.
|
|
3262
|
+
document.addEventListener("pointerdown", function (event) {
|
|
3263
|
+
var node = event.target;
|
|
3264
|
+
while (node && node !== document.body) {
|
|
3265
|
+
if (node.tagName === "BUTTON" || node.tagName === "INPUT" || node.tagName === "SELECT") {
|
|
3266
|
+
notePress(node);
|
|
3267
|
+
return;
|
|
3268
|
+
}
|
|
3269
|
+
node = node.parentNode;
|
|
3270
|
+
}
|
|
3271
|
+
}, true);
|
|
3272
|
+
|
|
3273
|
+
var viewTabs = document.querySelectorAll("#viewTabs .subtab");
|
|
3274
|
+
for (var i = 0; i < viewTabs.length; i += 1) {
|
|
3275
|
+
(function (button) {
|
|
3276
|
+
button.addEventListener("click", function () { selectTab(button.getAttribute("data-tab")); });
|
|
3277
|
+
})(viewTabs[i]);
|
|
3278
|
+
}
|
|
3279
|
+
|
|
3280
|
+
// A source tab owns both the pool page and the models page, so switching it
|
|
3281
|
+
// re-renders whichever of the two is on screen.
|
|
3282
|
+
var sourceTabs = document.querySelectorAll("#sourceTabs .tab");
|
|
3283
|
+
for (var j = 0; j < sourceTabs.length; j += 1) {
|
|
3284
|
+
(function (button) {
|
|
3285
|
+
button.addEventListener("click", function () {
|
|
3286
|
+
activeSource = button.getAttribute("data-source");
|
|
3287
|
+
renderSourceTabs();
|
|
3288
|
+
});
|
|
3289
|
+
})(sourceTabs[j]);
|
|
3290
|
+
}
|
|
3291
|
+
}
|
|
3292
|
+
|
|
3293
|
+
function boot() {
|
|
3294
|
+
labelTabs();
|
|
3295
|
+
bind();
|
|
3296
|
+
if (bridge === null) {
|
|
3297
|
+
note(t("err.bridge"));
|
|
3298
|
+
return;
|
|
3299
|
+
}
|
|
3300
|
+
// The frame bridge reports the host's selected theme and locale.
|
|
3301
|
+
function applyAppearance(appearance) {
|
|
3302
|
+
var base = appearance && appearance.base;
|
|
3303
|
+
if (base === "light" || base === "dark") {
|
|
3304
|
+
document.documentElement.dataset.base = base;
|
|
3305
|
+
}
|
|
3306
|
+
if (!appearance || typeof appearance.locale !== "string") return;
|
|
3307
|
+
locale = appearance.locale;
|
|
3308
|
+
TABLE = locale.toLowerCase().indexOf("zh") === 0 ? ZH : EN;
|
|
3309
|
+
document.documentElement.lang = locale;
|
|
3310
|
+
labelTabs();
|
|
3311
|
+
render();
|
|
3312
|
+
}
|
|
3313
|
+
if (typeof bridge.on === "function") bridge.on("appearance:changed", applyAppearance);
|
|
3314
|
+
invoke("app.getAppearance").then(applyAppearance).catch(function () {});
|
|
3315
|
+
// First paint comes from cache; the poll below picks up the refresh the main
|
|
3316
|
+
// process runs behind it, which is what keeps opening the panel fast.
|
|
3317
|
+
// A first read can also fail while the plugin is still activating: the panel
|
|
3318
|
+
// route exists from the moment the entry applies, but its host half may not
|
|
3319
|
+
// have finished starting. A few bounded retries cover that window; anything
|
|
3320
|
+
// still failing after them is a real error and renders as one.
|
|
3321
|
+
var bootAttempts = 0;
|
|
3322
|
+
function requestFirstState() {
|
|
3323
|
+
invoke("xd.state").then(function (next) {
|
|
3324
|
+
adoptAndMaybePoll(next, 0);
|
|
3325
|
+
pollCheckinLog(0);
|
|
3326
|
+
}).catch(function (error) {
|
|
3327
|
+
bootAttempts += 1;
|
|
3328
|
+
if (bootAttempts < 4) {
|
|
3329
|
+
setTimeout(requestFirstState, 600);
|
|
3330
|
+
return;
|
|
3331
|
+
}
|
|
3332
|
+
note(String(error && error.message ? error.message : error));
|
|
3333
|
+
render();
|
|
3334
|
+
});
|
|
3335
|
+
}
|
|
3336
|
+
requestFirstState();
|
|
3337
|
+
}
|
|
3338
|
+
|
|
3339
|
+
boot();
|