dsh-go-codearts 0.0.0-stage → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cordis.patch.yml +6 -0
- package/lib/index.js +516 -0
- package/package.json +72 -4
- package/package.json.bak-pub +72 -0
- package/vendor/dsh-codearts/LICENSE +21 -0
- package/vendor/dsh-codearts/lib/auth-T5qnGSPp.mjs +1533 -0
- package/vendor/dsh-codearts/lib/bin.mjs +360 -0
- package/vendor/dsh-codearts/lib/estimate-BePoD-Np.mjs +113 -0
- package/vendor/dsh-codearts/lib/index.mjs +4387 -0
- package/vendor/dsh-codearts/lib/keeper-BWHQDFAh.mjs +334 -0
- package/vendor/dsh-codearts/lib/keeper-main.mjs +304 -0
- package/vendor/dsh-codearts/lib/models-CgKCibpk.mjs +377 -0
- package/vendor/dsh-codearts/lib/openai-completions-Cn5ZQvOO.mjs +19392 -0
- package/vendor/dsh-codearts/lib/rolldown-runtime-B4iAMlE-.mjs +35 -0
- package/vendor/dsh-codearts/lib/upstream-Cr5tUI75.mjs +2363 -0
- package/vendor/dsh-codearts/lib/version-Bienp51v.mjs +541 -0
- package/vendor/dsh-codearts/package.json +7 -0
- package/README.md +0 -3
package/cordis.patch.yml
ADDED
package/lib/index.js
ADDED
|
@@ -0,0 +1,516 @@
|
|
|
1
|
+
// 手机端的 CodeArts 登录入口:dsh-codearts 的设置卡片是 web 组件,Flutter 界面里看不到,
|
|
2
|
+
// 这里把登录、账号状态、今日用量搬到原生胶囊和面板里。
|
|
3
|
+
import { readFile, readdir, writeFile } from 'node:fs/promises'
|
|
4
|
+
import { readFileSync, writeFileSync } from 'node:fs'
|
|
5
|
+
import { join, dirname } from 'node:path'
|
|
6
|
+
import { homedir } from 'node:os'
|
|
7
|
+
import { pathToFileURL, fileURLToPath } from 'node:url'
|
|
8
|
+
|
|
9
|
+
export const name = 'local-codearts'
|
|
10
|
+
|
|
11
|
+
function dshHome() {
|
|
12
|
+
const env = process.env.DSH_HOME
|
|
13
|
+
return env !== undefined && env !== '' ? env : join(homedir(), '.dsh')
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
let modCache = null
|
|
17
|
+
/** 优先用插件自带的 vendor 副本(解压即用),没有再退回宿主 profile 里装的。 */
|
|
18
|
+
async function codearts() {
|
|
19
|
+
if (modCache !== null) return modCache
|
|
20
|
+
const candidates = [join(dirname(dirname(fileURLToPath(import.meta.url))), 'vendor', 'dsh-codearts', 'lib')]
|
|
21
|
+
const profilesDir = join(dshHome(), 'profiles')
|
|
22
|
+
let profiles = []
|
|
23
|
+
try {
|
|
24
|
+
profiles = await readdir(profilesDir)
|
|
25
|
+
} catch {}
|
|
26
|
+
for (const profile of profiles) candidates.push(join(profilesDir, profile, 'node_modules', 'dsh-codearts', 'lib'))
|
|
27
|
+
for (const lib of candidates) {
|
|
28
|
+
let files = []
|
|
29
|
+
try {
|
|
30
|
+
files = await readdir(lib)
|
|
31
|
+
} catch {
|
|
32
|
+
continue
|
|
33
|
+
}
|
|
34
|
+
const auth = files.find((f) => /^auth-.*\.mjs$/.test(f))
|
|
35
|
+
const upstream = files.find((f) => /^upstream-.*\.mjs$/.test(f))
|
|
36
|
+
if (auth === undefined || upstream === undefined) continue
|
|
37
|
+
modCache = {
|
|
38
|
+
mod: await import(pathToFileURL(join(lib, auth)).href),
|
|
39
|
+
up: await import(pathToFileURL(join(lib, upstream)).href),
|
|
40
|
+
lib,
|
|
41
|
+
}
|
|
42
|
+
return modCache
|
|
43
|
+
}
|
|
44
|
+
throw new Error('插件自带的 dsh-codearts 副本损坏')
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const SEL_FILE = () => join(dshHome(), '.codearts-account.json')
|
|
48
|
+
function loadCurrentId() {
|
|
49
|
+
try { return String(JSON.parse(readFileSync(SEL_FILE(), 'utf8')).account ?? '') } catch { return '' }
|
|
50
|
+
}
|
|
51
|
+
function saveCurrentId(id) {
|
|
52
|
+
try { writeFileSync(SEL_FILE(), JSON.stringify({ account: id }), 'utf8') } catch {}
|
|
53
|
+
}
|
|
54
|
+
let currentId = loadCurrentId()
|
|
55
|
+
let logoutArmed = ''
|
|
56
|
+
|
|
57
|
+
/** 从账号列表里挑当前账号:显式选择优先,否则用上游 selected,否则第一个。 */
|
|
58
|
+
function pickAccount(accounts) {
|
|
59
|
+
if (currentId !== '') return accounts.find((a) => String(a.id) === currentId) ?? accounts.find((a) => a.selected === true) ?? accounts[0]
|
|
60
|
+
return accounts.find((a) => a.selected === true) ?? accounts[0]
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
async function readAccounts() {
|
|
64
|
+
try {
|
|
65
|
+
const doc = JSON.parse(await readFile(join(dshHome(), '.codearts-accounts.json'), 'utf8'))
|
|
66
|
+
const order = Array.isArray(doc.order) ? doc.order : Object.keys(doc.accounts ?? {})
|
|
67
|
+
return order.filter((id) => doc.accounts?.[id] !== undefined).map((id) => ({ id, ...doc.accounts[id] }))
|
|
68
|
+
} catch {
|
|
69
|
+
return []
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
async function removeAccountFile(id) {
|
|
74
|
+
const file = join(dshHome(), '.codearts-accounts.json')
|
|
75
|
+
const doc = JSON.parse(await readFile(file, 'utf8'))
|
|
76
|
+
delete doc.accounts?.[id]
|
|
77
|
+
if (Array.isArray(doc.order)) doc.order = doc.order.filter((x) => x !== id)
|
|
78
|
+
await writeFile(file, JSON.stringify(doc, null, 2), 'utf8')
|
|
79
|
+
if (currentId === id) { currentId = ''; saveCurrentId('') }
|
|
80
|
+
creditCache.at = 0
|
|
81
|
+
lastCheckin.at = 0
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function todayKey() {
|
|
85
|
+
const d = new Date()
|
|
86
|
+
const pad = (n) => String(n).padStart(2, '0')
|
|
87
|
+
return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}`
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
async function readUsage() {
|
|
91
|
+
try {
|
|
92
|
+
const doc = JSON.parse(await readFile(join(dshHome(), '.codearts-usage.json'), 'utf8'))
|
|
93
|
+
return doc.days?.[todayKey()] ?? {}
|
|
94
|
+
} catch {
|
|
95
|
+
return {}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function clip(text, limit) {
|
|
100
|
+
const str = String(text ?? '')
|
|
101
|
+
return str.length <= limit ? str : `${str.slice(0, Math.max(0, limit - 1))}…`
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function fmtTokens(n) {
|
|
105
|
+
if (!Number.isFinite(n) || n <= 0) return '0'
|
|
106
|
+
if (n < 1000) return String(n)
|
|
107
|
+
if (n < 1_000_000) return `${(n / 1000).toFixed(1)}k`
|
|
108
|
+
return `${(n / 1_000_000).toFixed(2)}M`
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function expiryText(iso) {
|
|
112
|
+
if (typeof iso !== 'string') return '未知'
|
|
113
|
+
const at = Date.parse(iso)
|
|
114
|
+
if (!Number.isFinite(at)) return '未知'
|
|
115
|
+
const left = at - Date.now()
|
|
116
|
+
if (left <= 0) return '已过期'
|
|
117
|
+
const min = Math.round(left / 60_000)
|
|
118
|
+
return min >= 90 ? `${Math.round(min / 60)} 小时后` : `${min} 分钟后`
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
let stripEmit = null
|
|
122
|
+
let stripLocale = 'zh'
|
|
123
|
+
|
|
124
|
+
async function pushStrip(force) {
|
|
125
|
+
const emit = stripEmit
|
|
126
|
+
if (typeof emit !== 'function') return
|
|
127
|
+
const zh = !stripLocale.startsWith('en')
|
|
128
|
+
const accounts = await readAccounts()
|
|
129
|
+
if (accounts.length === 0) {
|
|
130
|
+
emit({ text: zh ? '码道未登录' : 'CodeArts off', tone: 'warn' })
|
|
131
|
+
return
|
|
132
|
+
}
|
|
133
|
+
const c = await fetchCredits(force === true, pickAccount(accounts)?.id)
|
|
134
|
+
if (c.value !== null) {
|
|
135
|
+
const text = zh ? `码道 余 ${fmtCredit(c.value.remain)} 积分` : `CodeArts ${fmtCredit(c.value.remain)} left`
|
|
136
|
+
emit({ text: text.slice(0, 24), tone: c.value.remain <= 0 ? 'warn' : 'good' })
|
|
137
|
+
return
|
|
138
|
+
}
|
|
139
|
+
const usage = await readUsage()
|
|
140
|
+
const n = usage.requests ?? 0
|
|
141
|
+
emit({ text: zh ? `码道 今日 ${n} 次` : `CodeArts ${n} today`, tone: 'good' })
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
let session = null
|
|
145
|
+
|
|
146
|
+
async function startLogin(zh) {
|
|
147
|
+
if (session !== null) {
|
|
148
|
+
return { link: session.url, toast: zh ? '授权页已经打开了,去浏览器登录' : 'The sign-in page is already open' }
|
|
149
|
+
}
|
|
150
|
+
const { mod } = await codearts()
|
|
151
|
+
const start = mod.g ?? mod.startLogin
|
|
152
|
+
const s = await start()
|
|
153
|
+
session = s
|
|
154
|
+
s.promise
|
|
155
|
+
.then(async (token) => {
|
|
156
|
+
const Store = mod.t ?? mod.CodeArtsAccountStore
|
|
157
|
+
const accountsPath = (mod.i ?? mod.codeartsAccountsPath)(dshHome())
|
|
158
|
+
const legacyPath = (mod.a ?? mod.codeartsAuthPath)(dshHome())
|
|
159
|
+
const store = new Store(accountsPath, legacyPath)
|
|
160
|
+
await store.upsertFromOAuth(token)
|
|
161
|
+
session = null
|
|
162
|
+
await pushStrip()
|
|
163
|
+
})
|
|
164
|
+
.catch(() => {
|
|
165
|
+
session = null
|
|
166
|
+
})
|
|
167
|
+
return { link: s.url, toast: zh ? '在浏览器里登录后会自动记住' : 'Sign in, then it is saved' }
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
let lastCheckin = { at: 0, state: '', amount: 0 }
|
|
171
|
+
|
|
172
|
+
async function claimState(force, accountId) {
|
|
173
|
+
const acct = accountId ?? pickAccount(await readAccounts())?.id
|
|
174
|
+
if (!force && lastCheckin.account === acct && Date.now() - lastCheckin.at < 60_000) return lastCheckin
|
|
175
|
+
const accounts = await readAccounts()
|
|
176
|
+
if (accounts.length === 0) return { at: Date.now(), state: 'not-logged-in', amount: 0 }
|
|
177
|
+
try {
|
|
178
|
+
const { up } = await codearts()
|
|
179
|
+
const Store = mod0().t
|
|
180
|
+
const store = new Store((mod0().i)(dshHome()), (mod0().a)(dshHome()))
|
|
181
|
+
const cred = await store.resolve(acct ?? accounts[0].id)
|
|
182
|
+
const campaigns = await up.f(cred)
|
|
183
|
+
const daily = up.j(campaigns)
|
|
184
|
+
if (daily === undefined) return lastCheckin = { at: Date.now(), account: acct, state: 'unavailable', amount: 0 }
|
|
185
|
+
const state = daily.claimable === true ? 'claimable' : (up.T(daily.status) ? 'already' : 'unavailable')
|
|
186
|
+
return lastCheckin = { at: Date.now(), account: acct, state, amount: Number(daily.amount ?? 0) }
|
|
187
|
+
} catch {
|
|
188
|
+
return lastCheckin = { at: Date.now(), account: acct, state: 'error', amount: 0 }
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
function mod0() {
|
|
193
|
+
return modCache.mod
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
async function checkin(event) {
|
|
197
|
+
const zh = !String(event?.locale ?? '').startsWith('en')
|
|
198
|
+
const accounts = await readAccounts()
|
|
199
|
+
if (accounts.length === 0) {
|
|
200
|
+
return { open: 'panel:main', toast: zh ? '还没登录码道,先进面板登录' : 'Sign in first' }
|
|
201
|
+
}
|
|
202
|
+
try {
|
|
203
|
+
const acct = pickAccount(accounts)?.id
|
|
204
|
+
const pre = await claimState(false, acct)
|
|
205
|
+
if (pre.state === 'already') return { toast: zh ? '今天已经签过了' : 'Already checked in today' }
|
|
206
|
+
if (pre.state === 'not-logged-in') return { open: 'panel:main', toast: zh ? '还没登录码道' : 'Not signed in' }
|
|
207
|
+
const { up } = await codearts()
|
|
208
|
+
const Store = mod0().t
|
|
209
|
+
const store = new Store((mod0().i)(dshHome()), (mod0().a)(dshHome()))
|
|
210
|
+
const cred = await store.resolve(acct ?? accounts[0].id)
|
|
211
|
+
const answer = await up.a(cred)
|
|
212
|
+
if (answer.state === 'claimed') {
|
|
213
|
+
lastCheckin = { at: Date.now(), account: acct, state: 'already', amount: 0 }
|
|
214
|
+
await pushStrip(true)
|
|
215
|
+
return { toast: zh ? `签到成功,+${Number(answer.amount ?? 0)} 积分` : `Checked in +${Number(answer.amount ?? 0)}` }
|
|
216
|
+
}
|
|
217
|
+
if (answer.state === 'already') {
|
|
218
|
+
lastCheckin = { at: Date.now(), account: acct, state: 'already', amount: 0 }
|
|
219
|
+
return { toast: zh ? '今天已经签过了' : 'Already checked in today' }
|
|
220
|
+
}
|
|
221
|
+
return { toast: zh ? `签到没成功:${String(answer.message || answer.state).slice(0, 16)}` : 'Check-in failed' }
|
|
222
|
+
} catch (error) {
|
|
223
|
+
return { toast: zh ? `签到失败:${String(error?.message ?? error).slice(0, 16)}` : 'Check-in failed' }
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
// 自动签到开关:写在 DSH_HOME,默认开启(保持现在的行为)。
|
|
228
|
+
const AUTO_FILE = () => join(dshHome(), '.codearts-auto-checkin.json')
|
|
229
|
+
async function autoEnabled() {
|
|
230
|
+
try {
|
|
231
|
+
const doc = JSON.parse(await readFile(AUTO_FILE(), 'utf8'))
|
|
232
|
+
return doc.enabled !== false
|
|
233
|
+
} catch {
|
|
234
|
+
return true
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
async function setAutoEnabled(on) {
|
|
238
|
+
try {
|
|
239
|
+
await writeFile(AUTO_FILE(), JSON.stringify({ enabled: on }, null, 2), 'utf8')
|
|
240
|
+
} catch {}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
// 自动签到:引擎在线时每 30 分钟检查一次,按本机日期去重;静默领取,只写自己账号。
|
|
244
|
+
let autoCheckinDay = ''
|
|
245
|
+
async function autoCheckin() {
|
|
246
|
+
if (!(await autoEnabled())) return
|
|
247
|
+
const d = new Date()
|
|
248
|
+
const key = `${d.getFullYear()}-${d.getMonth() + 1}-${d.getDate()}`
|
|
249
|
+
if (key === autoCheckinDay) return
|
|
250
|
+
const accounts = await readAccounts()
|
|
251
|
+
if (accounts.length === 0) return
|
|
252
|
+
const acct = pickAccount(accounts)?.id
|
|
253
|
+
try {
|
|
254
|
+
const pre = await claimState(true, acct)
|
|
255
|
+
if (pre.state === 'already') {
|
|
256
|
+
autoCheckinDay = key
|
|
257
|
+
return
|
|
258
|
+
}
|
|
259
|
+
if (pre.state !== 'claimable') return
|
|
260
|
+
const { up } = await codearts()
|
|
261
|
+
const Store = mod0().t
|
|
262
|
+
const store = new Store((mod0().i)(dshHome()), (mod0().a)(dshHome()))
|
|
263
|
+
const cred = await store.resolve(acct ?? accounts[0].id)
|
|
264
|
+
const answer = await up.a(cred)
|
|
265
|
+
if (answer.state === 'claimed' || answer.state === 'already') {
|
|
266
|
+
autoCheckinDay = key
|
|
267
|
+
await pushStrip(true)
|
|
268
|
+
}
|
|
269
|
+
} catch {}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
// 今日是否有调用记录:决定面板里画柱状图还是显示一句人话(无数据时 App 自己会画「暂无数据」)。
|
|
273
|
+
let hasHourData = false
|
|
274
|
+
|
|
275
|
+
export function apply(ctx) {
|
|
276
|
+
ctx.inject(['dshGo'], (ctx) => {
|
|
277
|
+
const forceLogin = async (zh) => {
|
|
278
|
+
try {
|
|
279
|
+
return await startLogin(zh)
|
|
280
|
+
} catch (error) {
|
|
281
|
+
return { toast: zh ? `登录没起来:${String(error?.message ?? error).slice(0, 24)}` : 'Sign-in did not start' }
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
const openCredits = async (zh) => {
|
|
285
|
+
if ((await readAccounts()).length > 0) return { open: 'panel:main', toast: zh ? '码道已登录,积分在这里' : 'Signed in; credits here' }
|
|
286
|
+
try {
|
|
287
|
+
return await startLogin(zh)
|
|
288
|
+
} catch (error) {
|
|
289
|
+
return { toast: zh ? `登录没起来:${String(error?.message ?? error).slice(0, 24)}` : 'Sign-in did not start' }
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
ctx.dshGo.on('login', async (scope, event) => openCredits(!String(event.locale ?? '').startsWith('en')))
|
|
293
|
+
|
|
294
|
+
ctx.dshGo.strip('state', (scope, emit, caps) => {
|
|
295
|
+
stripEmit = typeof emit === 'function' ? emit : null
|
|
296
|
+
stripLocale = String(caps?.locale ?? 'zh')
|
|
297
|
+
void pushStrip()
|
|
298
|
+
void autoCheckin()
|
|
299
|
+
const timer = setInterval(() => void pushStrip(), 60_000)
|
|
300
|
+
const autoTimer = setInterval(() => void autoCheckin(), 30 * 60_000)
|
|
301
|
+
return () => {
|
|
302
|
+
clearInterval(timer)
|
|
303
|
+
clearInterval(autoTimer)
|
|
304
|
+
stripEmit = null
|
|
305
|
+
}
|
|
306
|
+
})
|
|
307
|
+
|
|
308
|
+
ctx.dshGo.panel('main', {
|
|
309
|
+
view: (scope, caps) => {
|
|
310
|
+
const zh = !String(caps?.locale ?? 'zh').startsWith('en')
|
|
311
|
+
return {
|
|
312
|
+
type: 'stack',
|
|
313
|
+
title: zh ? '码道用量' : 'CodeArts usage',
|
|
314
|
+
children: [
|
|
315
|
+
{ type: 'metric', label: zh ? '剩余积分' : 'Credits left', value: { $: 'data.creditText' }, tone: { $: 'data.creditTone' }, caption: { $: 'data.creditCaption' } },
|
|
316
|
+
{ type: 'progress', value: { $: 'data.usedPct' }, max: 100, label: zh ? '已用额度' : 'Used', caption: { $: 'data.usedCaption' } },
|
|
317
|
+
{ type: 'keyValue', items: [
|
|
318
|
+
[zh ? '套餐' : 'Plan', { $: 'data.planText' }],
|
|
319
|
+
[zh ? '积分总量' : 'Total', { $: 'data.totalText' }],
|
|
320
|
+
[zh ? '已用' : 'Used', { $: 'data.usedText' }],
|
|
321
|
+
[zh ? '即将过期' : 'Expiring', { $: 'data.expiringText' }],
|
|
322
|
+
[zh ? '账号' : 'Account', { $: 'data.accountLabel' }],
|
|
323
|
+
[zh ? '有效期' : 'Token expires', { $: 'data.expiryText' }],
|
|
324
|
+
[zh ? '今日请求' : 'Requests today', { $: 'data.requestText' }],
|
|
325
|
+
[zh ? '今日 tokens' : 'Tokens today', { $: 'data.tokenText' }],
|
|
326
|
+
[zh ? '今日签到' : 'Check-in', { $: 'data.checkinText' }],
|
|
327
|
+
] },
|
|
328
|
+
hasHourData
|
|
329
|
+
? { type: 'chart', kind: 'bar', categories: { $: 'data.hourCats' }, series: [{ values: { $: 'data.hourVals' } }], unit: 'tokens', caption: zh ? '今日按小时(本机账本)' : 'today by hour' }
|
|
330
|
+
: { type: 'text', text: zh ? '今日无调用记录' : 'No calls today', tone: 'muted' },
|
|
331
|
+
{ type: 'text', text: { $: 'data.accountNote' }, tone: 'muted' },
|
|
332
|
+
{ type: 'list', items: { $: 'data.accountItems' } },
|
|
333
|
+
{ type: 'actions', items: [
|
|
334
|
+
{ label: zh ? '刷新' : 'Refresh', action: 'refresh', style: 'primary' },
|
|
335
|
+
{ label: zh ? '签到' : 'Check in', action: 'checkin' },
|
|
336
|
+
{ label: zh ? '加账号' : 'Add account', action: 'add' },
|
|
337
|
+
{ label: zh ? '退出账号' : 'Sign out', action: 'logout' },
|
|
338
|
+
] },
|
|
339
|
+
{ type: 'text', text: { $: 'data.autoNote' }, tone: 'muted' },
|
|
340
|
+
{ type: 'options', id: 'autoToggle', multi: true, options: [{ label: { $: 'data.autoLabel' }, value: 'on' }], selected: { $: 'data.autoSelected' } },
|
|
341
|
+
],
|
|
342
|
+
}
|
|
343
|
+
},
|
|
344
|
+
data: (scope, emit, caps) => {
|
|
345
|
+
const zh = !String(caps?.locale ?? 'zh').startsWith('en')
|
|
346
|
+
let stopped = false
|
|
347
|
+
const tick = async () => {
|
|
348
|
+
if (stopped) return
|
|
349
|
+
try {
|
|
350
|
+
const accounts = await readAccounts()
|
|
351
|
+
const usage = await readUsage()
|
|
352
|
+
const a = pickAccount(accounts)
|
|
353
|
+
const c = await fetchCredits(false, a?.id)
|
|
354
|
+
const hours = usage.hours ?? {}
|
|
355
|
+
const keys = Object.keys(hours).sort((x, y) => Number(x) - Number(y))
|
|
356
|
+
hasHourData = keys.length > 0
|
|
357
|
+
const v = c.value
|
|
358
|
+
const ck = await claimState(false, a?.id)
|
|
359
|
+
const checkinText = ck.state === 'already' ? (zh ? '已签' : 'done') : ck.state === 'claimable' ? (zh ? '可领' : 'ready') : ck.state === 'not-logged-in' ? '—' : '—'
|
|
360
|
+
const usedPct = v !== null && v.total > 0 ? Math.min(100, Math.round((v.used / v.total) * 1000) / 10) : 0
|
|
361
|
+
if (stopped) return
|
|
362
|
+
emit({
|
|
363
|
+
creditText: v !== null ? fmtCredit(v.remain) : '—',
|
|
364
|
+
creditTone: v === null ? 'warn' : v.remain <= 0 ? 'bad' : 'good',
|
|
365
|
+
creditCaption: v !== null ? (zh ? `共 ${fmtCredit(v.total)} 积分` : `of ${fmtCredit(v.total)}`) : (c.error === 'not-signed-in' ? (zh ? '未登录' : 'signed out') : (zh ? `取不到:${c.error}` : c.error)),
|
|
366
|
+
usedPct,
|
|
367
|
+
usedCaption: v !== null ? `${fmtCredit(v.used)} / ${fmtCredit(v.total)}` : '—',
|
|
368
|
+
planText: v !== null && v.plan !== '' ? v.plan : '—',
|
|
369
|
+
totalText: v !== null ? fmtCredit(v.total) : '—',
|
|
370
|
+
usedText: v !== null ? fmtCredit(v.used) : '—',
|
|
371
|
+
expiringText: v !== null ? fmtCredit(v.expiring) : '—',
|
|
372
|
+
accountLabel: a === undefined ? '—' : String((a.userName || a.label || a.id) ?? '').slice(0, 16),
|
|
373
|
+
expiryText: a === undefined ? '—' : expiryText(a.expiration),
|
|
374
|
+
requestText: String(usage.requests ?? 0),
|
|
375
|
+
tokenText: fmtTokens(usage.totalTokens ?? 0),
|
|
376
|
+
checkinText,
|
|
377
|
+
hourCats: keys.map((k) => `${k}时`),
|
|
378
|
+
hourVals: keys.map((k) => hours[k]?.totalTokens ?? 0),
|
|
379
|
+
accountNote: accounts.length > 1
|
|
380
|
+
? (zh ? '点一行切换账号,积分和签到都跟着换。' : 'Tap a row to switch accounts.')
|
|
381
|
+
: (zh ? `本机 ${accounts.length} 个账号;点「加账号」可再登一个。` : `${accounts.length} account(s). Tap "Add account" for another.`),
|
|
382
|
+
accountItems: accounts.map((x) => {
|
|
383
|
+
const on = String(x.id) === String(a?.id ?? '')
|
|
384
|
+
return {
|
|
385
|
+
title: clip(String(x.userName || x.label || x.id), 24),
|
|
386
|
+
subtitle: on ? (zh ? '当前使用' : 'in use') : (zh ? '点一下切换' : 'tap to switch'),
|
|
387
|
+
trailing: on ? (zh ? '当前' : 'now') : (zh ? '切换' : 'switch'),
|
|
388
|
+
tone: on ? 'good' : 'muted',
|
|
389
|
+
action: { id: 'switch-account', args: { account: String(x.id) } },
|
|
390
|
+
}
|
|
391
|
+
}),
|
|
392
|
+
autoNote: zh ? '自动签到:引擎在线时每天检查一次,勾选后会自动领取当日积分。' : 'Auto check-in daily while the engine runs.',
|
|
393
|
+
autoLabel: zh ? '开启自动签到' : 'Enable auto check-in',
|
|
394
|
+
autoSelected: (await autoEnabled()) ? ['on'] : [],
|
|
395
|
+
})
|
|
396
|
+
} catch {}
|
|
397
|
+
}
|
|
398
|
+
void tick()
|
|
399
|
+
const timer = setInterval(() => void tick(), 3000)
|
|
400
|
+
return () => { stopped = true; clearInterval(timer) }
|
|
401
|
+
},
|
|
402
|
+
actions: {
|
|
403
|
+
'switch-account': (scope, event) => {
|
|
404
|
+
const zh = !String(event?.locale ?? 'zh').startsWith('en')
|
|
405
|
+
const picked = String(event?.value?.account ?? '')
|
|
406
|
+
if (picked === '') return { toast: zh ? '没拿到账号' : 'No account' }
|
|
407
|
+
currentId = picked
|
|
408
|
+
saveCurrentId(picked)
|
|
409
|
+
creditCache.at = 0
|
|
410
|
+
lastCheckin.at = 0
|
|
411
|
+
void pushStrip(true)
|
|
412
|
+
return { toast: clip(zh ? '已切换账号,正在更新' : 'Switched, updating', 40) }
|
|
413
|
+
},
|
|
414
|
+
logout: async (scope, event) => {
|
|
415
|
+
const zh = !String(event?.locale ?? 'zh').startsWith('en')
|
|
416
|
+
const accounts = await readAccounts()
|
|
417
|
+
const target = pickAccount(accounts)?.id
|
|
418
|
+
if (target === undefined) return { toast: zh ? '没有可退出的账号' : 'No account' }
|
|
419
|
+
if (logoutArmed !== target) {
|
|
420
|
+
logoutArmed = target
|
|
421
|
+
setTimeout(() => { if (logoutArmed === target) logoutArmed = '' }, 8000)
|
|
422
|
+
return { toast: clip(zh ? '再点一次「退出账号」确认' : 'Tap again to confirm', 40) }
|
|
423
|
+
}
|
|
424
|
+
logoutArmed = ''
|
|
425
|
+
try {
|
|
426
|
+
await removeAccountFile(target)
|
|
427
|
+
await pushStrip(true)
|
|
428
|
+
return { toast: clip(zh ? '已退出该账号(本机登录态已删)' : 'Signed out', 40) }
|
|
429
|
+
} catch (error) {
|
|
430
|
+
return { toast: clip(zh ? `退出失败:${String(error?.message ?? error).slice(0, 12)}` : 'Sign out failed', 40) }
|
|
431
|
+
}
|
|
432
|
+
},
|
|
433
|
+
add: (scope, event) => forceLogin(!String(event?.locale ?? 'zh').startsWith('en')),
|
|
434
|
+
login: (scope, event) => forceLogin(!String(event?.locale ?? 'zh').startsWith('en')),
|
|
435
|
+
checkin: (scope, event) => checkin(event),
|
|
436
|
+
autoToggle: async (scope, event) => {
|
|
437
|
+
const zh = !String(event?.locale ?? 'zh').startsWith('en')
|
|
438
|
+
const raw = event?.value?.value
|
|
439
|
+
const on = Array.isArray(raw) ? raw.includes('on') : raw === 'on'
|
|
440
|
+
await setAutoEnabled(on)
|
|
441
|
+
return { refresh: true, toast: zh ? (on ? '已开启自动签到' : '已关闭自动签到') : (on ? 'Auto check-in on' : 'Auto check-in off') }
|
|
442
|
+
},
|
|
443
|
+
refresh: async () => {
|
|
444
|
+
await fetchCredits(true)
|
|
445
|
+
return { refresh: true }
|
|
446
|
+
},
|
|
447
|
+
},
|
|
448
|
+
})
|
|
449
|
+
|
|
450
|
+
})
|
|
451
|
+
}
|
|
452
|
+
// ── 积分与用量:现问上游 /snap-manager/v1/statistics/plugin(需 AK/SK 签名)──
|
|
453
|
+
|
|
454
|
+
let creditCache = { at: 0, value: null, error: '' }
|
|
455
|
+
const CREDIT_TTL = 5 * 60_000
|
|
456
|
+
|
|
457
|
+
function num(v) {
|
|
458
|
+
const n = Number(v)
|
|
459
|
+
return Number.isFinite(n) ? n : 0
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
async function fetchCredits(force, accountId) {
|
|
463
|
+
const acct = accountId ?? pickAccount(await readAccounts())?.id ?? ''
|
|
464
|
+
const fresh = Date.now() - creditCache.at < CREDIT_TTL && creditCache.account === acct
|
|
465
|
+
if (!force && fresh) return creditCache
|
|
466
|
+
try {
|
|
467
|
+
const { up } = await codearts()
|
|
468
|
+
const cred = await currentCredential(acct)
|
|
469
|
+
if (cred === null) {
|
|
470
|
+
creditCache = { at: Date.now(), account: acct, value: null, error: 'not-signed-in' }
|
|
471
|
+
return creditCache
|
|
472
|
+
}
|
|
473
|
+
const res = await up.L(`${up.n}/snap-manager/v1/statistics/plugin`, cred)
|
|
474
|
+
const text = String(res?.text ?? '')
|
|
475
|
+
const body = JSON.parse(text)
|
|
476
|
+
const metrics = Array.isArray(body?.metrics) ? body.metrics : []
|
|
477
|
+
const total = metrics.find((m) => m?.name === 'usageTotalPackageCredit')
|
|
478
|
+
if (total === undefined) throw new Error('上游没有返回积分指标')
|
|
479
|
+
creditCache = {
|
|
480
|
+
at: Date.now(),
|
|
481
|
+
account: acct,
|
|
482
|
+
error: '',
|
|
483
|
+
value: {
|
|
484
|
+
total: num(total.package_credit_amount),
|
|
485
|
+
used: num(total.package_credit_used),
|
|
486
|
+
remain: num(total.package_credit_remain ?? total.package_credit_available),
|
|
487
|
+
expiring: num(total.package_credit_expiring_amount),
|
|
488
|
+
plan: String(body?.package?.package_name_cn ?? body?.package?.package_name_en ?? ''),
|
|
489
|
+
periodEnd: String(body?.end_date ?? ''),
|
|
490
|
+
},
|
|
491
|
+
}
|
|
492
|
+
return creditCache
|
|
493
|
+
} catch (error) {
|
|
494
|
+
creditCache = { at: Date.now(), account: acct, value: creditCache.value, error: String(error?.message ?? error).slice(0, 80) }
|
|
495
|
+
return creditCache
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
function fmtCredit(n) {
|
|
500
|
+
if (!Number.isFinite(n)) return '—'
|
|
501
|
+
if (n >= 10000) return `${(n / 1000).toFixed(1)}k`
|
|
502
|
+
return n % 1 === 0 ? String(n) : n.toFixed(2)
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
async function currentCredential(accountId) {
|
|
506
|
+
const { mod } = await codearts()
|
|
507
|
+
const Store = mod.t ?? mod.CodeArtsAccountStore
|
|
508
|
+
const accountsPath = (mod.i ?? mod.codeartsAccountsPath)(dshHome())
|
|
509
|
+
const legacyPath = (mod.a ?? mod.codeartsAuthPath)(dshHome())
|
|
510
|
+
const store = new Store(accountsPath, legacyPath)
|
|
511
|
+
const ids = await store.ids()
|
|
512
|
+
if (ids.length === 0) return null
|
|
513
|
+
const target = accountId !== undefined && ids.includes(accountId) ? accountId : ids[0]
|
|
514
|
+
// resolve() 会按需续期并回填 AK/SK;current() 只读旧 token,直接拿去签名会 400。
|
|
515
|
+
return (await store.resolve(target)) ?? null
|
|
516
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,74 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-go-codearts",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "lib/index.js",
|
|
6
|
+
"dsh": {
|
|
7
|
+
"bundle": {
|
|
8
|
+
"patch": "./cordis.patch.yml"
|
|
9
|
+
}
|
|
10
|
+
},
|
|
11
|
+
"dependencies": {
|
|
12
|
+
"dsh-codearts": "^1.4.17"
|
|
13
|
+
},
|
|
14
|
+
"engines": {
|
|
15
|
+
"dsh": ">=0.2.1-rc.2 <0.3.0-0"
|
|
16
|
+
},
|
|
17
|
+
"dshGo": {
|
|
18
|
+
"v": 1,
|
|
19
|
+
"title": {
|
|
20
|
+
"zh": "码道",
|
|
21
|
+
"en": "CodeArts"
|
|
22
|
+
},
|
|
23
|
+
"icon": "cloud",
|
|
24
|
+
"contributes": {
|
|
25
|
+
"composer.actions": [
|
|
26
|
+
{
|
|
27
|
+
"id": "login",
|
|
28
|
+
"label": {
|
|
29
|
+
"zh": "码道",
|
|
30
|
+
"en": "CodeArts"
|
|
31
|
+
},
|
|
32
|
+
"icon": "account"
|
|
33
|
+
}
|
|
34
|
+
],
|
|
35
|
+
"composer.strip": [
|
|
36
|
+
{
|
|
37
|
+
"id": "state",
|
|
38
|
+
"icon": "chart",
|
|
39
|
+
"panel": "main"
|
|
40
|
+
}
|
|
41
|
+
],
|
|
42
|
+
"panels": [
|
|
43
|
+
{
|
|
44
|
+
"id": "main",
|
|
45
|
+
"title": {
|
|
46
|
+
"zh": "码道账号",
|
|
47
|
+
"en": "CodeArts"
|
|
48
|
+
},
|
|
49
|
+
"icon": "cloud",
|
|
50
|
+
"placement": [
|
|
51
|
+
"session.tab",
|
|
52
|
+
"features"
|
|
53
|
+
]
|
|
54
|
+
}
|
|
55
|
+
]
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
"description": "Huawei Cloud CodeArts credits, daily check-in and sign-in as native dsh-go pills and panels. Self-contained with vendor core.",
|
|
59
|
+
"author": "YG",
|
|
60
|
+
"license": "MIT",
|
|
61
|
+
"homepage": "https://github.com/YG/dsh-go-codearts",
|
|
62
|
+
"repository": {
|
|
63
|
+
"type": "git",
|
|
64
|
+
"url": "git+https://github.com/YG/dsh-go-codearts.git"
|
|
65
|
+
},
|
|
66
|
+
"keywords": [
|
|
67
|
+
"dsh-plugin",
|
|
68
|
+
"dsh-go",
|
|
69
|
+
"codearts",
|
|
70
|
+
"huawei",
|
|
71
|
+
"credits",
|
|
72
|
+
"checkin"
|
|
73
|
+
]
|
|
74
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "local-codearts",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"private": true,
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "lib/index.js",
|
|
7
|
+
"dsh": {
|
|
8
|
+
"bundle": {
|
|
9
|
+
"patch": "./cordis.patch.yml"
|
|
10
|
+
}
|
|
11
|
+
},
|
|
12
|
+
"engines": {
|
|
13
|
+
"dsh": ">=0.2.1-rc.2 <0.3.0-0"
|
|
14
|
+
},
|
|
15
|
+
"dshGo": {
|
|
16
|
+
"v": 1,
|
|
17
|
+
"title": {
|
|
18
|
+
"zh": "码道",
|
|
19
|
+
"en": "CodeArts"
|
|
20
|
+
},
|
|
21
|
+
"icon": "cloud",
|
|
22
|
+
"contributes": {
|
|
23
|
+
"composer.actions": [
|
|
24
|
+
{
|
|
25
|
+
"id": "login",
|
|
26
|
+
"label": {
|
|
27
|
+
"zh": "码道",
|
|
28
|
+
"en": "CodeArts"
|
|
29
|
+
},
|
|
30
|
+
"icon": "account"
|
|
31
|
+
}
|
|
32
|
+
],
|
|
33
|
+
"composer.strip": [
|
|
34
|
+
{
|
|
35
|
+
"id": "state",
|
|
36
|
+
"icon": "chart",
|
|
37
|
+
"panel": "main"
|
|
38
|
+
}
|
|
39
|
+
],
|
|
40
|
+
"panels": [
|
|
41
|
+
{
|
|
42
|
+
"id": "main",
|
|
43
|
+
"title": {
|
|
44
|
+
"zh": "码道账号",
|
|
45
|
+
"en": "CodeArts"
|
|
46
|
+
},
|
|
47
|
+
"icon": "cloud",
|
|
48
|
+
"placement": [
|
|
49
|
+
"session.tab",
|
|
50
|
+
"features"
|
|
51
|
+
]
|
|
52
|
+
}
|
|
53
|
+
]
|
|
54
|
+
}
|
|
55
|
+
},
|
|
56
|
+
"description": "Huawei Cloud CodeArts credits, daily check-in and sign-in as native dsh-go pills and panels. Self-contained with vendor core.",
|
|
57
|
+
"author": "YG",
|
|
58
|
+
"license": "MIT",
|
|
59
|
+
"homepage": "https://github.com/YG/dsh-go-codearts",
|
|
60
|
+
"repository": {
|
|
61
|
+
"type": "git",
|
|
62
|
+
"url": "git+https://github.com/YG/dsh-go-codearts.git"
|
|
63
|
+
},
|
|
64
|
+
"keywords": [
|
|
65
|
+
"dsh-plugin",
|
|
66
|
+
"dsh-go",
|
|
67
|
+
"codearts",
|
|
68
|
+
"huawei",
|
|
69
|
+
"credits",
|
|
70
|
+
"checkin"
|
|
71
|
+
]
|
|
72
|
+
}
|