dsh-balance-hub 0.2.0 → 0.2.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/README.md +3 -3
- package/client.js +38 -38
- package/host/adapters/antigravity.js +235 -260
- package/host/config.js +1 -1
- package/host/credentials.js +26 -15
- package/host/registry.js +5 -16
- package/host/scheduler.js +6 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -10,18 +10,18 @@
|
|
|
10
10
|
- **腾讯混元**(腾讯云计费 OpenAPI `DescribeAccountBalance`,使用 SecretId / SecretKey)
|
|
11
11
|
- **字节火山 API**(费用中心 `QueryBalanceAcct`,AK / SK)& **字节火山 Coding Plan**(方舟 `GetCodingPlanUsage`,AK / SK)
|
|
12
12
|
- **小米 MiMo**(API Key)
|
|
13
|
-
- **谷歌 Antigravity
|
|
13
|
+
- **谷歌 Antigravity**(直接调用官方 `agy -p /usage --output-format json` 获取 Gemini / Argon / Claude 5h & 7d 配额,不依赖第三方插件)
|
|
14
14
|
|
|
15
15
|
## 安装与使用
|
|
16
16
|
|
|
17
17
|
```bash
|
|
18
|
-
# 通过 DSH CLI 安装(npm
|
|
18
|
+
# 通过 DSH CLI 安装(npm 包)
|
|
19
19
|
dsh plugin add dsh-balance-hub
|
|
20
20
|
```
|
|
21
21
|
|
|
22
22
|
1. 在 DSH 中打开 **Plugins → Add plugin**,输入 npm 包名 `dsh-balance-hub`、GitHub 仓库 `https://github.com/d1019296026/dsh-balance-hub` 或本地目录 `dsh-balance-hub`。
|
|
23
23
|
2. 启用后,左侧栏会出现「余额」图标,点击打开面板。
|
|
24
|
-
3. 展开对应供应商行即可录入凭据并点击「保存并测试」(谷歌 Antigravity
|
|
24
|
+
3. 展开对应供应商行即可录入凭据并点击「保存并测试」(谷歌 Antigravity 会自动检测本机官方 `agy` CLI)。所有密钥存储在 DSH 凭据库(`balance-hub/<provider>`)中,不会写入配置文件或日志,也不会通过 HTTP 接口或工具结果返回。
|
|
25
25
|
4. 在面板顶部可开启或关闭**定时查询**,支持 5 / 15 / 30 / 60 分钟快捷预设或 1–1440 分钟自定义间隔,配置保存在 `~/.dsh/balance-hub-schedule.json`。
|
|
26
26
|
5. 在新对话中可直接让智能体查询各账户余额与配额(内部调用 `balance_query` 工具)。
|
|
27
27
|
|
package/client.js
CHANGED
|
@@ -70,13 +70,13 @@ window.__ModuleLoader__.load({
|
|
|
70
70
|
codingUsed: '已用',
|
|
71
71
|
codingRemaining: '剩余',
|
|
72
72
|
notSubscribed: '未订阅 Coding Plan',
|
|
73
|
-
codingPlanNote: '说明:智谱、MiniMax、Kimi、字节火山均支持同时展示 API 余额与 Coding Plan 配额;谷歌 Antigravity
|
|
74
|
-
antigravityHint: '
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
autoLinked: '
|
|
73
|
+
codingPlanNote: '说明:智谱、MiniMax、Kimi、字节火山均支持同时展示 API 余额与 Coding Plan 配额;谷歌 Antigravity 直接调用官方 agy CLI (/usage) 查询配额;阿里百炼 Coding Plan 官方未提供额度查询 API。',
|
|
74
|
+
antigravityHint: '直接调用本机官方 Google Antigravity CLI(agy -p /usage --output-format json)获取 5h 与周额度,不依赖其他插件。未开启 TUN 代理时可填写代理 URL。',
|
|
75
|
+
agyPathLabel: 'agy 可执行路径(可选)',
|
|
76
|
+
agyPathPlaceholder: '留空自动查找系统 agy(或 %LOCALAPPDATA%\\agy\\bin\\agy.exe)',
|
|
77
|
+
proxyUrlLabel: '代理 URL(可选)',
|
|
78
|
+
proxyUrlPlaceholder: '如 http://127.0.0.1:7897(留空跟随系统网络)',
|
|
79
|
+
autoLinked: '官方 agy CLI',
|
|
80
80
|
resetToAuto: '恢复自动检测',
|
|
81
81
|
scheduleTitle: '定时查询',
|
|
82
82
|
scheduleOn: '已开启',
|
|
@@ -155,13 +155,13 @@ window.__ModuleLoader__.load({
|
|
|
155
155
|
codingUsed: 'used',
|
|
156
156
|
codingRemaining: 'left',
|
|
157
157
|
notSubscribed: 'Not subscribed',
|
|
158
|
-
codingPlanNote: 'Note: Zhipu, MiniMax, Kimi, and Volcano Engine show API balance and Coding Plan quotas side by side; Google Antigravity
|
|
159
|
-
antigravityHint: '
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
autoLinked: '
|
|
158
|
+
codingPlanNote: 'Note: Zhipu, MiniMax, Kimi, and Volcano Engine show API balance and Coding Plan quotas side by side; Google Antigravity invokes official agy CLI (/usage) directly; Bailian Coding Plan has no public quota API.',
|
|
159
|
+
antigravityHint: 'Directly invokes the official Google Antigravity CLI (agy -p /usage --output-format json) for 5h & weekly quotas, independent of any other plugin.',
|
|
160
|
+
agyPathLabel: 'agy executable path (optional)',
|
|
161
|
+
agyPathPlaceholder: 'Leave blank to auto-detect agy on PATH / LocalAppData',
|
|
162
|
+
proxyUrlLabel: 'Proxy URL (optional)',
|
|
163
|
+
proxyUrlPlaceholder: 'e.g. http://127.0.0.1:7897 (optional)',
|
|
164
|
+
autoLinked: 'Official agy CLI',
|
|
165
165
|
resetToAuto: 'Reset to auto',
|
|
166
166
|
scheduleTitle: 'Scheduled refresh',
|
|
167
167
|
scheduleOn: 'On',
|
|
@@ -714,8 +714,8 @@ window.__ModuleLoader__.load({
|
|
|
714
714
|
const [apiKey, setApiKey] = useState('');
|
|
715
715
|
const [accessKey, setAccessKey] = useState('');
|
|
716
716
|
const [secretKey, setSecretKey] = useState('');
|
|
717
|
-
const [
|
|
718
|
-
const [
|
|
717
|
+
const [agyPath, setAgyPath] = useState(cfg?.autoDetected ? '' : (cfg?.agyPath || ''));
|
|
718
|
+
const [proxyUrl, setProxyUrl] = useState(cfg?.proxyUrl || '');
|
|
719
719
|
const isVolc = row.provider === 'volcengine' || row.provider === 'volcengine-coding';
|
|
720
720
|
const [region, setRegion] = useState(cfg?.region || (isVolc ? 'cn-beijing' : ''));
|
|
721
721
|
const [baseUrl, setBaseUrl] = useState(cfg?.baseUrl || '');
|
|
@@ -726,8 +726,8 @@ window.__ModuleLoader__.load({
|
|
|
726
726
|
setMode(resolveSupportedMode(cfg));
|
|
727
727
|
setRegion(cfg?.region || (isVolc ? 'cn-beijing' : ''));
|
|
728
728
|
setBaseUrl(cfg?.baseUrl || '');
|
|
729
|
-
|
|
730
|
-
|
|
729
|
+
setAgyPath(cfg?.autoDetected ? '' : (cfg?.agyPath || ''));
|
|
730
|
+
setProxyUrl(cfg?.proxyUrl || '');
|
|
731
731
|
}, [cfg, row.provider, isVolc]);
|
|
732
732
|
|
|
733
733
|
const presets = BASE_URL_PRESETS[row.provider];
|
|
@@ -762,8 +762,8 @@ window.__ModuleLoader__.load({
|
|
|
762
762
|
const payload = isAntigravity
|
|
763
763
|
? {
|
|
764
764
|
kind: 'grant',
|
|
765
|
-
|
|
766
|
-
|
|
765
|
+
agyPath: agyPath.trim() || undefined,
|
|
766
|
+
proxyUrl: proxyUrl.trim() || undefined,
|
|
767
767
|
}
|
|
768
768
|
: mode === 'grant'
|
|
769
769
|
? {
|
|
@@ -797,8 +797,8 @@ window.__ModuleLoader__.load({
|
|
|
797
797
|
setAccessKey('');
|
|
798
798
|
setSecretKey('');
|
|
799
799
|
setBaseUrl('');
|
|
800
|
-
|
|
801
|
-
|
|
800
|
+
setAgyPath('');
|
|
801
|
+
setProxyUrl('');
|
|
802
802
|
setFeedback({ ok: true, text: t('clearedSuccess') });
|
|
803
803
|
} catch (err) {
|
|
804
804
|
setFeedback({ ok: false, text: err.message });
|
|
@@ -884,25 +884,25 @@ window.__ModuleLoader__.load({
|
|
|
884
884
|
h(
|
|
885
885
|
'div',
|
|
886
886
|
{ className: 'bh-field' },
|
|
887
|
-
h('label', { className: 'bh-label' }, t('
|
|
887
|
+
h('label', { className: 'bh-label' }, t('agyPathLabel')),
|
|
888
888
|
h('input', {
|
|
889
889
|
className: 'bh-input',
|
|
890
890
|
type: 'text',
|
|
891
|
-
value:
|
|
892
|
-
placeholder: t('
|
|
893
|
-
onChange: (e) =>
|
|
891
|
+
value: agyPath,
|
|
892
|
+
placeholder: t('agyPathPlaceholder'),
|
|
893
|
+
onChange: (e) => setAgyPath(e.target.value),
|
|
894
894
|
}),
|
|
895
895
|
),
|
|
896
896
|
h(
|
|
897
897
|
'div',
|
|
898
898
|
{ className: 'bh-field' },
|
|
899
|
-
h('label', { className: 'bh-label' }, t('
|
|
899
|
+
h('label', { className: 'bh-label' }, t('proxyUrlLabel')),
|
|
900
900
|
h('input', {
|
|
901
901
|
className: 'bh-input',
|
|
902
902
|
type: 'text',
|
|
903
|
-
value:
|
|
904
|
-
placeholder: t('
|
|
905
|
-
onChange: (e) =>
|
|
903
|
+
value: proxyUrl,
|
|
904
|
+
placeholder: t('proxyUrlPlaceholder'),
|
|
905
|
+
onChange: (e) => setProxyUrl(e.target.value),
|
|
906
906
|
}),
|
|
907
907
|
),
|
|
908
908
|
)
|
|
@@ -996,25 +996,25 @@ window.__ModuleLoader__.load({
|
|
|
996
996
|
h(
|
|
997
997
|
'div',
|
|
998
998
|
{ className: 'bh-field' },
|
|
999
|
-
h('label', { className: 'bh-label' }, t('
|
|
999
|
+
h('label', { className: 'bh-label' }, t('agyPathLabel')),
|
|
1000
1000
|
h('input', {
|
|
1001
1001
|
className: 'bh-input',
|
|
1002
1002
|
type: 'text',
|
|
1003
|
-
value:
|
|
1004
|
-
placeholder: t('
|
|
1005
|
-
onChange: (e) =>
|
|
1003
|
+
value: agyPath,
|
|
1004
|
+
placeholder: t('agyPathPlaceholder'),
|
|
1005
|
+
onChange: (e) => setAgyPath(e.target.value),
|
|
1006
1006
|
}),
|
|
1007
1007
|
),
|
|
1008
1008
|
h(
|
|
1009
1009
|
'div',
|
|
1010
1010
|
{ className: 'bh-field' },
|
|
1011
|
-
h('label', { className: 'bh-label' }, t('
|
|
1011
|
+
h('label', { className: 'bh-label' }, t('proxyUrlLabel')),
|
|
1012
1012
|
h('input', {
|
|
1013
1013
|
className: 'bh-input',
|
|
1014
1014
|
type: 'text',
|
|
1015
|
-
value:
|
|
1016
|
-
placeholder: t('
|
|
1017
|
-
onChange: (e) =>
|
|
1015
|
+
value: proxyUrl,
|
|
1016
|
+
placeholder: t('proxyUrlPlaceholder'),
|
|
1017
|
+
onChange: (e) => setProxyUrl(e.target.value),
|
|
1018
1018
|
}),
|
|
1019
1019
|
),
|
|
1020
1020
|
)
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
+
import { execFile } from 'node:child_process';
|
|
1
2
|
import { existsSync } from 'node:fs';
|
|
2
|
-
import { readFile } from 'node:fs/promises';
|
|
3
3
|
import { homedir } from 'node:os';
|
|
4
|
-
import { join, resolve } from 'node:path';
|
|
4
|
+
import { delimiter, join, resolve } from 'node:path';
|
|
5
5
|
|
|
6
6
|
import { BalanceError, redact } from '../result.js';
|
|
7
7
|
import { parseEpochMs, percentUsed, quotaDetail } from './coding-plan.js';
|
|
@@ -11,236 +11,194 @@ export const meta = {
|
|
|
11
11
|
label: '谷歌 Antigravity',
|
|
12
12
|
credKind: 'grant',
|
|
13
13
|
supportedKinds: ['grant'],
|
|
14
|
-
defaultOrigin: '
|
|
15
|
-
hosts: ['
|
|
14
|
+
defaultOrigin: 'https://daily-cloudcode-pa.googleapis.com',
|
|
15
|
+
hosts: ['daily-cloudcode-pa.googleapis.com', 'cloudcode-pa.googleapis.com'],
|
|
16
16
|
};
|
|
17
17
|
|
|
18
|
-
const
|
|
19
|
-
const
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
18
|
+
const CLI_TIMEOUT_MS = 35_000;
|
|
19
|
+
const CLI_MAX_BUFFER = 2 * 1024 * 1024;
|
|
20
|
+
|
|
21
|
+
function officialBinaryCandidates() {
|
|
22
|
+
const candidates = [];
|
|
23
|
+
const home = homedir();
|
|
24
|
+
if (process.platform === 'win32') {
|
|
25
|
+
const localAppData = process.env.LOCALAPPDATA || join(home, 'AppData', 'Local');
|
|
26
|
+
candidates.push(join(localAppData, 'agy', 'bin', 'agy.exe'));
|
|
27
|
+
candidates.push(join(localAppData, 'Programs', 'agy', 'agy.exe'));
|
|
28
|
+
} else {
|
|
29
|
+
candidates.push(join(home, '.local', 'bin', 'agy'));
|
|
30
|
+
candidates.push('/usr/local/bin/agy');
|
|
31
|
+
candidates.push('/opt/homebrew/bin/agy');
|
|
32
|
+
}
|
|
33
|
+
const pathEnv = process.env.PATH || '';
|
|
34
|
+
const names = process.platform === 'win32' ? ['agy.exe', 'agy.cmd', 'agy'] : ['agy'];
|
|
35
|
+
for (const dir of pathEnv.split(delimiter)) {
|
|
36
|
+
const trimmed = dir.trim();
|
|
37
|
+
if (!trimmed) continue;
|
|
38
|
+
for (const binName of names) {
|
|
39
|
+
candidates.push(join(trimmed, binName));
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return candidates;
|
|
29
43
|
}
|
|
30
44
|
|
|
31
45
|
/**
|
|
32
|
-
* Resolve the
|
|
33
|
-
* When config.
|
|
34
|
-
*
|
|
46
|
+
* Resolve the official Google Antigravity CLI (`agy`) binary path.
|
|
47
|
+
* When `config.antigravityCliBin` is explicitly set to `''` and no customPath is passed,
|
|
48
|
+
* automatic binary discovery is disabled (useful for deterministic unit tests).
|
|
35
49
|
* @param {string | undefined} customPath
|
|
36
50
|
* @param {Record<string, any> | undefined} config
|
|
37
51
|
* @returns {string}
|
|
38
52
|
*/
|
|
39
|
-
export function
|
|
53
|
+
export function resolveAgyBinary(customPath, config) {
|
|
40
54
|
if (typeof customPath === 'string' && customPath.trim()) {
|
|
41
55
|
return resolve(customPath.trim());
|
|
42
56
|
}
|
|
43
|
-
if (config && Object.prototype.hasOwnProperty.call(config, '
|
|
44
|
-
return typeof config.
|
|
45
|
-
? resolve(config.
|
|
57
|
+
if (config && Object.prototype.hasOwnProperty.call(config, 'antigravityCliBin')) {
|
|
58
|
+
return typeof config.antigravityCliBin === 'string' && config.antigravityCliBin.trim()
|
|
59
|
+
? resolve(config.antigravityCliBin.trim())
|
|
46
60
|
: '';
|
|
47
61
|
}
|
|
48
|
-
|
|
62
|
+
const fromEnv = typeof process.env.AGY_BIN === 'string' ? process.env.AGY_BIN.trim() : '';
|
|
63
|
+
if (fromEnv && existsSync(fromEnv)) return resolve(fromEnv);
|
|
64
|
+
for (const candidate of officialBinaryCandidates()) {
|
|
65
|
+
try {
|
|
66
|
+
if (existsSync(candidate)) return candidate;
|
|
67
|
+
} catch {
|
|
68
|
+
// Ignore unreadable PATH entries.
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return '';
|
|
49
72
|
}
|
|
50
73
|
|
|
51
74
|
/**
|
|
52
|
-
* Check whether
|
|
75
|
+
* Check whether the official `agy` CLI binary exists locally.
|
|
53
76
|
* @param {string | undefined} customPath
|
|
54
77
|
* @param {Record<string, any> | undefined} config
|
|
55
78
|
* @returns {boolean}
|
|
56
79
|
*/
|
|
57
|
-
export function
|
|
58
|
-
const
|
|
59
|
-
if (!
|
|
80
|
+
export function hasOfficialAgyCli(customPath, config) {
|
|
81
|
+
const bin = resolveAgyBinary(customPath, config);
|
|
82
|
+
if (!bin) return false;
|
|
60
83
|
try {
|
|
61
|
-
return existsSync(
|
|
84
|
+
return existsSync(bin);
|
|
62
85
|
} catch {
|
|
63
86
|
return false;
|
|
64
87
|
}
|
|
65
88
|
}
|
|
66
89
|
|
|
67
|
-
function
|
|
68
|
-
|
|
69
|
-
const
|
|
70
|
-
if (
|
|
71
|
-
if (
|
|
72
|
-
if (
|
|
73
|
-
|
|
74
|
-
return tier.trim();
|
|
90
|
+
function classifyGroupLabel(groupName, bucketId) {
|
|
91
|
+
const id = String(bucketId || '').toLowerCase();
|
|
92
|
+
const name = String(groupName || '').toLowerCase();
|
|
93
|
+
if (id.includes('argon') || name.includes('argon')) return 'Argon';
|
|
94
|
+
if (id.startsWith('gemini-') || name.includes('gemini')) return 'Gemini';
|
|
95
|
+
if (id.startsWith('3p-') || name.includes('claude') || name.includes('gpt')) return 'Claude';
|
|
96
|
+
return String(groupName || 'Quota').trim() || 'Quota';
|
|
75
97
|
}
|
|
76
98
|
|
|
77
|
-
function
|
|
78
|
-
|
|
79
|
-
const
|
|
80
|
-
const
|
|
81
|
-
|
|
82
|
-
return
|
|
83
|
-
|
|
84
|
-
used: usedPct,
|
|
85
|
-
total: 100,
|
|
86
|
-
usedPercent: usedPct,
|
|
87
|
-
...(resetAt !== undefined ? { resetAt } : {}),
|
|
88
|
-
};
|
|
99
|
+
function classifyWindowLabel(windowValue, bucketId, bucketName) {
|
|
100
|
+
const w = String(windowValue || '').toLowerCase();
|
|
101
|
+
const id = String(bucketId || '').toLowerCase();
|
|
102
|
+
const name = String(bucketName || '').toLowerCase();
|
|
103
|
+
if (w === '5h' || id.endsWith('-5h') || name.includes('five hour') || name.includes('5-hour')) return '5h';
|
|
104
|
+
if (w === 'weekly' || id.endsWith('-weekly') || name.includes('weekly')) return '7d';
|
|
105
|
+
return w || id || 'window';
|
|
89
106
|
}
|
|
90
107
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
const windows = [];
|
|
94
|
-
|
|
95
|
-
const g = quotas.google;
|
|
96
|
-
if (g && typeof g === 'object') {
|
|
97
|
-
const w5h = makeFractionWindow('Gemini 5h', g.remainingFraction, g.resetTime);
|
|
98
|
-
if (w5h) windows.push(w5h);
|
|
99
|
-
const w7d = makeFractionWindow('Gemini 7d', g.weeklyFraction, g.weeklyResetTime);
|
|
100
|
-
if (w7d) windows.push(w7d);
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
const argon = quotas.argon;
|
|
104
|
-
if (argon && typeof argon === 'object') {
|
|
105
|
-
const hasArgonFrac = typeof argon.weeklyFraction === 'number' || typeof argon.remainingFraction === 'number';
|
|
106
|
-
if (hasArgonFrac) {
|
|
107
|
-
const frac = typeof argon.weeklyFraction === 'number' ? argon.weeklyFraction : argon.remainingFraction;
|
|
108
|
-
const reset = argon.weeklyResetTime ?? argon.resetTime;
|
|
109
|
-
const wArgon = makeFractionWindow('Argon 7d', frac, reset);
|
|
110
|
-
if (wArgon) windows.push(wArgon);
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
const claude = quotas.anthropic && typeof quotas.anthropic === 'object'
|
|
115
|
-
? quotas.anthropic
|
|
116
|
-
: quotas.openai && typeof quotas.openai === 'object'
|
|
117
|
-
? quotas.openai
|
|
118
|
-
: undefined;
|
|
119
|
-
if (claude) {
|
|
120
|
-
const w5h = makeFractionWindow('Claude 5h', claude.remainingFraction, claude.resetTime);
|
|
121
|
-
if (w5h) windows.push(w5h);
|
|
122
|
-
const w7d = makeFractionWindow('Claude 7d', claude.weeklyFraction, claude.weeklyResetTime);
|
|
123
|
-
if (w7d) windows.push(w7d);
|
|
124
|
-
}
|
|
108
|
+
const GROUP_ORDER = { Gemini: 0, Argon: 1, Claude: 2 };
|
|
109
|
+
const WINDOW_ORDER = { '5h': 0, '7d': 1 };
|
|
125
110
|
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
function pickTargetAccount(accounts, poolData, accountFilter) {
|
|
143
|
-
const filter = typeof accountFilter === 'string' ? accountFilter.trim().toLowerCase() : '';
|
|
144
|
-
if (filter) {
|
|
145
|
-
const match = accounts.find(
|
|
146
|
-
(a) =>
|
|
147
|
-
String(a?.id || '').toLowerCase() === filter ||
|
|
148
|
-
String(a?.email || '').toLowerCase() === filter ||
|
|
149
|
-
String(a?.alias || '').toLowerCase() === filter,
|
|
150
|
-
);
|
|
151
|
-
if (!match) {
|
|
152
|
-
throw new BalanceError('unconfigured', 'ACCOUNT_NOT_FOUND', `Account "${accountFilter}" not found in Antigravity pool`, false);
|
|
153
|
-
}
|
|
154
|
-
return match;
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
const enabled = accounts.filter((a) => a && a.enabled !== false);
|
|
158
|
-
const candidates = enabled.length > 0 ? enabled : accounts;
|
|
159
|
-
const preferredIds = [
|
|
160
|
-
poolData?.resolvedActiveAccountIds?.google,
|
|
161
|
-
poolData?.activeAccountIds?.google,
|
|
162
|
-
poolData?.lastActiveAccountId,
|
|
163
|
-
poolData?.primaryAccountId,
|
|
164
|
-
].filter((v) => typeof v === 'string' && v);
|
|
165
|
-
|
|
166
|
-
for (const id of preferredIds) {
|
|
167
|
-
const hit = candidates.find((a) => a.id === id && a.quotas && Object.keys(a.quotas).length > 0);
|
|
168
|
-
if (hit) return hit;
|
|
169
|
-
}
|
|
170
|
-
return candidates.find((a) => a.quotas && Object.keys(a.quotas).length > 0) ?? candidates[0];
|
|
111
|
+
function makeWindow(groupShort, winShort, remainingFraction, resetTime) {
|
|
112
|
+
if (typeof remainingFraction !== 'number' || !Number.isFinite(remainingFraction)) return undefined;
|
|
113
|
+
const clamped = Math.min(Math.max(remainingFraction, 0), 1);
|
|
114
|
+
const usedPercent = percentUsed((1 - clamped) * 100);
|
|
115
|
+
const resetAt = parseEpochMs(resetTime);
|
|
116
|
+
return {
|
|
117
|
+
groupOrder: GROUP_ORDER[groupShort] ?? 9,
|
|
118
|
+
winOrder: WINDOW_ORDER[winShort] ?? 9,
|
|
119
|
+
window: {
|
|
120
|
+
label: `${groupShort} ${winShort}`,
|
|
121
|
+
used: usedPercent,
|
|
122
|
+
total: 100,
|
|
123
|
+
usedPercent,
|
|
124
|
+
...(resetAt !== undefined ? { resetAt } : {}),
|
|
125
|
+
},
|
|
126
|
+
};
|
|
171
127
|
}
|
|
172
128
|
|
|
173
|
-
function
|
|
174
|
-
|
|
175
|
-
const
|
|
176
|
-
const
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
129
|
+
function parseTabResponseFallback(responseText) {
|
|
130
|
+
if (typeof responseText !== 'string' || !responseText.trim()) return [];
|
|
131
|
+
const entries = [];
|
|
132
|
+
for (const rawLine of responseText.split(/\r?\n/)) {
|
|
133
|
+
const line = rawLine.trim();
|
|
134
|
+
if (!line) continue;
|
|
135
|
+
const cols = line.split('\t');
|
|
136
|
+
if (cols.length < 3) continue;
|
|
137
|
+
const [groupName, winName, pctText, resetIso] = cols;
|
|
138
|
+
const match = /(\d+(?:\.\d+)?)\s*%/.exec(pctText);
|
|
139
|
+
if (!match) continue;
|
|
140
|
+
const remPct = Number(match[1]);
|
|
141
|
+
if (!Number.isFinite(remPct)) continue;
|
|
142
|
+
const groupShort = classifyGroupLabel(groupName, '');
|
|
143
|
+
const winShort = classifyWindowLabel('', '', winName);
|
|
144
|
+
const item = makeWindow(groupShort, winShort, remPct / 100, resetIso);
|
|
145
|
+
if (item) entries.push(item);
|
|
180
146
|
}
|
|
181
|
-
|
|
182
|
-
return `${name} (${tier}) · ${parts.join(' / ')}`;
|
|
147
|
+
return entries;
|
|
183
148
|
}
|
|
184
149
|
|
|
185
150
|
/**
|
|
186
|
-
* Parse
|
|
187
|
-
* @param {any}
|
|
188
|
-
* @param {{ accountFilter?: string, sourceLabel?: string }} [options]
|
|
151
|
+
* Parse official `agy -p /usage --output-format json` payload into normalized balance-hub windows.
|
|
152
|
+
* @param {any} payload
|
|
189
153
|
*/
|
|
190
|
-
export function
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
throw new BalanceError('parse_error', 'INVALID_POOL_DATA', 'Invalid Antigravity pool snapshot', false);
|
|
194
|
-
}
|
|
195
|
-
if (accounts.length === 0) {
|
|
196
|
-
throw new BalanceError('unconfigured', 'NO_ACCOUNTS', 'Antigravity 账号池中暂无已登录账号', false);
|
|
154
|
+
export function parseAgyUsageOutput(payload) {
|
|
155
|
+
if (!payload || typeof payload !== 'object') {
|
|
156
|
+
throw new BalanceError('parse_error', 'INVALID_AGY_OUTPUT', 'Official agy CLI returned invalid JSON', false);
|
|
197
157
|
}
|
|
198
158
|
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
throw new BalanceError(
|
|
204
|
-
'auth_failed',
|
|
205
|
-
'AGY_AUTH_REQUIRED',
|
|
206
|
-
redact(acc.authError || 'Google Antigravity 账号需重新登录'),
|
|
207
|
-
false,
|
|
208
|
-
);
|
|
159
|
+
if (payload.status && String(payload.status).toUpperCase() !== 'SUCCESS') {
|
|
160
|
+
const msg = redact(payload.error || payload.message || payload.response || `agy status: ${payload.status}`);
|
|
161
|
+
if (/auth|login|sign\s*in|credential|unauthenticated|401/i.test(msg)) {
|
|
162
|
+
throw new BalanceError('auth_failed', 'AGY_AUTH_REQUIRED', msg, false);
|
|
209
163
|
}
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
164
|
+
throw new BalanceError('upstream_error', 'AGY_CLI_ERROR', msg, true);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
const groups = Array.isArray(payload?.command?.data?.groups) ? payload.command.data.groups : [];
|
|
168
|
+
const collected = [];
|
|
169
|
+
|
|
170
|
+
for (const group of groups) {
|
|
171
|
+
if (!group || typeof group !== 'object') continue;
|
|
172
|
+
const buckets = Array.isArray(group.buckets) ? group.buckets : [];
|
|
173
|
+
for (const bucket of buckets) {
|
|
174
|
+
if (!bucket || typeof bucket !== 'object') continue;
|
|
175
|
+
const groupShort = classifyGroupLabel(group.name, bucket.id);
|
|
176
|
+
const winShort = classifyWindowLabel(bucket.window, bucket.id, bucket.name);
|
|
177
|
+
const item = makeWindow(groupShort, winShort, bucket.remaining_fraction, bucket.reset_time);
|
|
178
|
+
if (item) collected.push(item);
|
|
217
179
|
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
const finalItems = collected.length > 0 ? collected : parseTabResponseFallback(payload.response);
|
|
183
|
+
if (finalItems.length === 0) {
|
|
218
184
|
throw new BalanceError(
|
|
219
185
|
'parse_error',
|
|
220
186
|
'NO_QUOTA_WINDOWS',
|
|
221
|
-
'
|
|
187
|
+
'Official agy CLI /usage returned no quota buckets',
|
|
222
188
|
true,
|
|
223
189
|
);
|
|
224
190
|
}
|
|
225
191
|
|
|
226
|
-
|
|
227
|
-
const
|
|
228
|
-
const accountLabel = acc.email || acc.alias || acc.id;
|
|
229
|
-
const latestUpdated = maxQuotaUpdatedAt({ accounts: [acc] });
|
|
192
|
+
finalItems.sort((a, b) => (a.groupOrder - b.groupOrder) || (a.winOrder - b.winOrder));
|
|
193
|
+
const windows = finalItems.map((item) => item.window);
|
|
230
194
|
|
|
231
195
|
const extra = {
|
|
232
|
-
|
|
233
|
-
planType: tier,
|
|
234
|
-
account: accountLabel,
|
|
235
|
-
accounts: `${enabledCount}/${accounts.length}`,
|
|
236
|
-
source: sourceLabel,
|
|
237
|
-
...(latestUpdated > 0 ? { quotaUpdatedAt: new Date(latestUpdated).toISOString() } : {}),
|
|
196
|
+
source: 'agy CLI (/usage)',
|
|
238
197
|
};
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
});
|
|
198
|
+
for (const group of groups) {
|
|
199
|
+
if (group?.name && group?.description) {
|
|
200
|
+
extra[String(group.name)] = String(group.description);
|
|
201
|
+
}
|
|
244
202
|
}
|
|
245
203
|
|
|
246
204
|
return {
|
|
@@ -251,106 +209,123 @@ export function parseAntigravityPool(poolData, { accountFilter, sourceLabel = 'p
|
|
|
251
209
|
};
|
|
252
210
|
}
|
|
253
211
|
|
|
254
|
-
function
|
|
255
|
-
return new Promise((
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
212
|
+
function defaultExecCli(binPath, args, { env, timeoutMs, signal }) {
|
|
213
|
+
return new Promise((resolveExec, rejectExec) => {
|
|
214
|
+
execFile(
|
|
215
|
+
binPath,
|
|
216
|
+
args,
|
|
217
|
+
{
|
|
218
|
+
env,
|
|
219
|
+
timeout: timeoutMs,
|
|
220
|
+
maxBuffer: CLI_MAX_BUFFER,
|
|
221
|
+
windowsHide: true,
|
|
222
|
+
signal,
|
|
223
|
+
},
|
|
224
|
+
(err, stdout, stderr) => {
|
|
225
|
+
if (err) {
|
|
226
|
+
rejectExec(Object.assign(err, { stdout, stderr }));
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
229
|
+
resolveExec({ stdout, stderr });
|
|
230
|
+
},
|
|
231
|
+
);
|
|
269
232
|
});
|
|
270
233
|
}
|
|
271
234
|
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
235
|
+
function extractLastJsonObject(text) {
|
|
236
|
+
const lines = String(text || '')
|
|
237
|
+
.split(/\r?\n/)
|
|
238
|
+
.map((l) => l.trim())
|
|
239
|
+
.filter(Boolean);
|
|
240
|
+
for (let i = lines.length - 1; i >= 0; i -= 1) {
|
|
241
|
+
if (lines[i].startsWith('{') && lines[i].endsWith('}')) {
|
|
242
|
+
try {
|
|
243
|
+
return JSON.parse(lines[i]);
|
|
244
|
+
} catch {
|
|
245
|
+
// Keep scanning previous lines if a line looked JSON-like.
|
|
246
|
+
}
|
|
247
|
+
}
|
|
284
248
|
}
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
timeoutMs: Math.min(ctx.timeoutMs ?? 5_000, 5_000),
|
|
291
|
-
maxBytes: LOCAL_MAX_BYTES,
|
|
292
|
-
signal: ctx.signal,
|
|
293
|
-
});
|
|
294
|
-
if (status >= 200 && status < 300 && json && Array.isArray(json.accounts)) {
|
|
295
|
-
return json;
|
|
249
|
+
const trimmed = String(text || '').trim();
|
|
250
|
+
const firstBrace = trimmed.indexOf('{');
|
|
251
|
+
const lastBrace = trimmed.lastIndexOf('}');
|
|
252
|
+
if (firstBrace >= 0 && lastBrace > firstBrace) {
|
|
253
|
+
return JSON.parse(trimmed.slice(firstBrace, lastBrace + 1));
|
|
296
254
|
}
|
|
297
|
-
|
|
255
|
+
throw new BalanceError('parse_error', 'MISSING_AGY_JSON', 'Official agy CLI did not emit JSON output', true);
|
|
298
256
|
}
|
|
299
257
|
|
|
300
258
|
/**
|
|
301
|
-
* Query Google Antigravity plan quotas via
|
|
259
|
+
* Query Google Antigravity plan quotas directly via official `agy -p /usage --output-format json`.
|
|
302
260
|
* @param {any} record
|
|
303
261
|
* @param {any} ctx
|
|
304
262
|
*/
|
|
305
263
|
export async function query(record, ctx) {
|
|
306
264
|
const payload = record?.kind === 'grant' && record.payload && typeof record.payload === 'object' ? record.payload : {};
|
|
307
|
-
const
|
|
308
|
-
const
|
|
265
|
+
const customBin = typeof payload.agyPath === 'string' && payload.agyPath.trim() ? payload.agyPath.trim() : undefined;
|
|
266
|
+
const binPath = resolveAgyBinary(customBin, ctx.config);
|
|
309
267
|
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
method: 'POST',
|
|
319
|
-
headers: { 'Content-Type': 'application/json' },
|
|
320
|
-
body: '{}',
|
|
321
|
-
timeoutMs: 3_000,
|
|
322
|
-
maxBytes: LOCAL_MAX_BYTES,
|
|
323
|
-
signal: ctx.signal,
|
|
324
|
-
}).catch(() => null);
|
|
268
|
+
if (!binPath || (!ctx.execCli && !existsSync(binPath))) {
|
|
269
|
+
throw new BalanceError(
|
|
270
|
+
'unconfigured',
|
|
271
|
+
'AGY_BIN_NOT_FOUND',
|
|
272
|
+
'未找到官方 agy CLI 可执行文件,请先安装 Google Antigravity CLI 或在配置中指定 agy 路径',
|
|
273
|
+
false,
|
|
274
|
+
);
|
|
275
|
+
}
|
|
325
276
|
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
if (maxQuotaUpdatedAt(refreshed) > beforeUpdated) break;
|
|
333
|
-
}
|
|
334
|
-
}
|
|
335
|
-
}
|
|
336
|
-
if (poolData) {
|
|
337
|
-
return parseAntigravityPool(poolData, { accountFilter, sourceLabel: 'agy-link HTTP' });
|
|
338
|
-
}
|
|
277
|
+
const env = { ...process.env };
|
|
278
|
+
const proxyUrl = typeof payload.proxyUrl === 'string' && payload.proxyUrl.trim() ? payload.proxyUrl.trim() : '';
|
|
279
|
+
if (proxyUrl) {
|
|
280
|
+
let parsedProxy;
|
|
281
|
+
try {
|
|
282
|
+
parsedProxy = new URL(proxyUrl);
|
|
339
283
|
} catch {
|
|
340
|
-
|
|
284
|
+
throw new BalanceError('unsupported', 'INVALID_PROXY_URL', 'Invalid proxy URL', false);
|
|
285
|
+
}
|
|
286
|
+
if (!['http:', 'https:', 'socks5:', 'socks5h:', 'socks4:'].includes(parsedProxy.protocol)) {
|
|
287
|
+
throw new BalanceError('unsupported', 'INVALID_PROXY_URL', 'Unsupported proxy protocol', false);
|
|
341
288
|
}
|
|
289
|
+
env.HTTP_PROXY = proxyUrl;
|
|
290
|
+
env.HTTPS_PROXY = proxyUrl;
|
|
291
|
+
env.ALL_PROXY = proxyUrl;
|
|
342
292
|
}
|
|
343
293
|
|
|
344
|
-
const
|
|
345
|
-
const
|
|
346
|
-
|
|
347
|
-
|
|
294
|
+
const execCli = typeof ctx.execCli === 'function' ? ctx.execCli : defaultExecCli;
|
|
295
|
+
const timeoutMs = Math.max(Number(ctx.timeoutMs) || 15_000, CLI_TIMEOUT_MS);
|
|
296
|
+
const args = ['-p', '/usage', '--output-format', 'json', '--print-timeout', '25s'];
|
|
297
|
+
|
|
298
|
+
let result;
|
|
299
|
+
try {
|
|
300
|
+
result = await execCli(binPath, args, {
|
|
301
|
+
env,
|
|
302
|
+
timeoutMs,
|
|
303
|
+
signal: ctx.signal,
|
|
304
|
+
});
|
|
305
|
+
} catch (err) {
|
|
306
|
+
const combined = redact(`${err?.message || ''}\n${err?.stderr || ''}\n${err?.stdout || ''}`);
|
|
307
|
+
if (err?.code === 'ENOENT') {
|
|
308
|
+
throw new BalanceError('unconfigured', 'AGY_BIN_NOT_FOUND', `官方 agy CLI 不存在: ${binPath}`, false);
|
|
309
|
+
}
|
|
310
|
+
if (err?.killed || err?.code === 'ETIMEDOUT' || err?.name === 'AbortError' || ctx.signal?.aborted) {
|
|
311
|
+
throw new BalanceError('timeout', 'AGY_TIMEOUT', '调用官方 agy /usage 超时,请检查网络或代理设置', true);
|
|
312
|
+
}
|
|
313
|
+
if (/auth|login|sign\s*in|unauthenticated|credential|401|token/i.test(combined)) {
|
|
314
|
+
throw new BalanceError('auth_failed', 'AGY_AUTH_REQUIRED', `官方 agy CLI 尚未登录或凭证已失效: ${combined}`, false);
|
|
315
|
+
}
|
|
316
|
+
if (/econn|enotfound|etimedout|network|tls|proxy|reset|socket/i.test(combined)) {
|
|
317
|
+
throw new BalanceError('network_error', 'AGY_NETWORK_ERROR', `连接 Google 服务失败: ${combined}`, true);
|
|
318
|
+
}
|
|
319
|
+
throw new BalanceError('upstream_error', 'AGY_CLI_FAILED', combined || '官方 agy CLI 执行失败', true);
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
let parsedJson;
|
|
323
|
+
try {
|
|
324
|
+
parsedJson = extractLastJsonObject(result?.stdout);
|
|
325
|
+
} catch (err) {
|
|
326
|
+
if (err instanceof BalanceError) throw err;
|
|
327
|
+
throw new BalanceError('parse_error', 'INVALID_AGY_JSON', '无法解析官方 agy /usage 返回的 JSON', false);
|
|
348
328
|
}
|
|
349
329
|
|
|
350
|
-
|
|
351
|
-
'unconfigured',
|
|
352
|
-
'AGY_POOL_NOT_FOUND',
|
|
353
|
-
'未检测到 dsh-agy-link 账号池文件(pool.json),请先在 Antigravity 插件登录账号或指定 pool.json 路径',
|
|
354
|
-
false,
|
|
355
|
-
);
|
|
330
|
+
return parseAgyUsageOutput(parsedJson);
|
|
356
331
|
}
|
package/host/config.js
CHANGED
|
@@ -32,7 +32,7 @@ export function normalizeConfig(raw) {
|
|
|
32
32
|
warnBalanceThreshold: Math.max(0, typeof cfg.warnBalanceThreshold === 'number' && Number.isFinite(cfg.warnBalanceThreshold) ? cfg.warnBalanceThreshold : 50),
|
|
33
33
|
lowBalanceThreshold: Math.max(0, typeof cfg.lowBalanceThreshold === 'number' && Number.isFinite(cfg.lowBalanceThreshold) ? cfg.lowBalanceThreshold : 10),
|
|
34
34
|
disabledProviders,
|
|
35
|
-
...(Object.prototype.hasOwnProperty.call(cfg, '
|
|
35
|
+
...(Object.prototype.hasOwnProperty.call(cfg, 'antigravityCliBin') ? { antigravityCliBin: cfg.antigravityCliBin } : {}),
|
|
36
36
|
...(typeof cfg.scheduleFilePath === 'string' && cfg.scheduleFilePath.trim() ? { scheduleFilePath: cfg.scheduleFilePath.trim() } : {}),
|
|
37
37
|
schedule,
|
|
38
38
|
};
|
package/host/credentials.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { hasOfficialAgyCli, resolveAgyBinary } from './adapters/antigravity.js';
|
|
2
2
|
import { PROVIDER_ORDER } from './providers.js';
|
|
3
3
|
import { BalanceError, resolveBaseUrl } from './result.js';
|
|
4
4
|
|
|
@@ -42,8 +42,8 @@ export async function describeAllCredentials(ctx, adapters, config) {
|
|
|
42
42
|
const info = await ctx.credentials.describeRecord(key);
|
|
43
43
|
let baseUrl;
|
|
44
44
|
let region;
|
|
45
|
-
let
|
|
46
|
-
let
|
|
45
|
+
let agyPath;
|
|
46
|
+
let proxyUrl;
|
|
47
47
|
if (info?.configured) {
|
|
48
48
|
const record = await ctx.credentials.readRecord(key);
|
|
49
49
|
if (record?.kind === 'api-key') {
|
|
@@ -51,13 +51,13 @@ export async function describeAllCredentials(ctx, adapters, config) {
|
|
|
51
51
|
} else if (record?.kind === 'grant' && record.payload && typeof record.payload === 'object') {
|
|
52
52
|
baseUrl = typeof record.payload.baseUrl === 'string' ? record.payload.baseUrl : undefined;
|
|
53
53
|
region = typeof record.payload.region === 'string' ? record.payload.region : undefined;
|
|
54
|
-
|
|
55
|
-
|
|
54
|
+
agyPath = typeof record.payload.agyPath === 'string' ? record.payload.agyPath : undefined;
|
|
55
|
+
proxyUrl = typeof record.payload.proxyUrl === 'string' ? record.payload.proxyUrl : undefined;
|
|
56
56
|
}
|
|
57
57
|
}
|
|
58
|
-
const autoDetected = id === 'antigravity' && !info?.configured &&
|
|
59
|
-
if (autoDetected && !
|
|
60
|
-
|
|
58
|
+
const autoDetected = id === 'antigravity' && !info?.configured && hasOfficialAgyCli(undefined, config);
|
|
59
|
+
if (autoDetected && !agyPath) {
|
|
60
|
+
agyPath = resolveAgyBinary(undefined, config) || undefined;
|
|
61
61
|
}
|
|
62
62
|
rows.push({
|
|
63
63
|
id,
|
|
@@ -71,8 +71,8 @@ export async function describeAllCredentials(ctx, adapters, config) {
|
|
|
71
71
|
writable: info?.writable ?? true,
|
|
72
72
|
baseUrl,
|
|
73
73
|
region,
|
|
74
|
-
|
|
75
|
-
|
|
74
|
+
agyPath,
|
|
75
|
+
proxyUrl,
|
|
76
76
|
});
|
|
77
77
|
}
|
|
78
78
|
return rows;
|
|
@@ -114,11 +114,22 @@ export async function saveProviderCredential(ctx, id, adapter, input) {
|
|
|
114
114
|
}
|
|
115
115
|
|
|
116
116
|
if (id === 'antigravity') {
|
|
117
|
-
const
|
|
118
|
-
const
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
117
|
+
const agyPath = typeof input.agyPath === 'string' ? input.agyPath.trim() : '';
|
|
118
|
+
const proxyUrl = typeof input.proxyUrl === 'string' ? input.proxyUrl.trim() : '';
|
|
119
|
+
if (proxyUrl) {
|
|
120
|
+
let parsedProxy;
|
|
121
|
+
try {
|
|
122
|
+
parsedProxy = new URL(proxyUrl);
|
|
123
|
+
} catch {
|
|
124
|
+
throw new BalanceError('unsupported', 'INVALID_PROXY_URL', 'Proxy URL must be a valid URL', false);
|
|
125
|
+
}
|
|
126
|
+
if (!['http:', 'https:', 'socks5:', 'socks5h:', 'socks4:'].includes(parsedProxy.protocol)) {
|
|
127
|
+
throw new BalanceError('unsupported', 'INVALID_PROXY_URL', 'Unsupported proxy protocol', false);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
const payload = { source: 'agy-cli' };
|
|
131
|
+
if (agyPath) payload.agyPath = agyPath;
|
|
132
|
+
if (proxyUrl) payload.proxyUrl = proxyUrl;
|
|
122
133
|
return { kind: 'grant', payload };
|
|
123
134
|
}
|
|
124
135
|
|
package/host/registry.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { hasOfficialAgyCli } from './adapters/antigravity.js';
|
|
2
2
|
import { PROVIDER_ORDER } from './providers.js';
|
|
3
3
|
import { describeAllCredentials, readProviderCredential } from './credentials.js';
|
|
4
4
|
import { fetchJson } from './http.js';
|
|
@@ -17,16 +17,6 @@ export function createRegistry({ ctx, config, adapters }) {
|
|
|
17
17
|
let revision = 0;
|
|
18
18
|
let scheduleProvider = null;
|
|
19
19
|
|
|
20
|
-
function resolveLocalPort() {
|
|
21
|
-
try {
|
|
22
|
-
const ws = typeof ctx.get === 'function' ? ctx.get('webServer') : ctx.webServer;
|
|
23
|
-
const port = ws?.port;
|
|
24
|
-
return typeof port === 'number' && port > 0 ? port : undefined;
|
|
25
|
-
} catch {
|
|
26
|
-
return undefined;
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
|
|
30
20
|
function currentSchedule() {
|
|
31
21
|
return typeof scheduleProvider === 'function' ? scheduleProvider() : undefined;
|
|
32
22
|
}
|
|
@@ -43,7 +33,7 @@ export function createRegistry({ ctx, config, adapters }) {
|
|
|
43
33
|
function hasUsableSecret(id, record) {
|
|
44
34
|
if (id === 'antigravity') {
|
|
45
35
|
if (record?.kind === 'grant' && record.payload && typeof record.payload === 'object') return true;
|
|
46
|
-
return
|
|
36
|
+
return hasOfficialAgyCli(undefined, config);
|
|
47
37
|
}
|
|
48
38
|
if (!record) return false;
|
|
49
39
|
if (record.kind === 'api-key') return Boolean(typeof record.key === 'string' && record.key.trim());
|
|
@@ -90,7 +80,7 @@ export function createRegistry({ ctx, config, adapters }) {
|
|
|
90
80
|
label: adapters[id].meta.label,
|
|
91
81
|
status: 'unconfigured',
|
|
92
82
|
fetchedAt: 0,
|
|
93
|
-
source: id === 'antigravity' ? 'agy-
|
|
83
|
+
source: id === 'antigravity' ? 'agy-cli' : cfg.kind === 'api-key' ? 'api-key' : 'cloud-aksk',
|
|
94
84
|
};
|
|
95
85
|
});
|
|
96
86
|
|
|
@@ -150,14 +140,13 @@ export function createRegistry({ ctx, config, adapters }) {
|
|
|
150
140
|
return unconfigured;
|
|
151
141
|
}
|
|
152
142
|
|
|
153
|
-
const effectiveRecord = record ?? { kind: 'grant', payload: { source: 'agy-
|
|
143
|
+
const effectiveRecord = record ?? { kind: 'grant', payload: { source: 'agy-cli' } };
|
|
154
144
|
let outcome;
|
|
155
145
|
try {
|
|
156
146
|
outcome = await adapter.query(effectiveRecord, {
|
|
157
147
|
signal,
|
|
158
148
|
force,
|
|
159
149
|
config,
|
|
160
|
-
localPort: resolveLocalPort(),
|
|
161
150
|
timeoutMs: config.requestTimeoutMs,
|
|
162
151
|
maxBytes: config.maxResponseBytes,
|
|
163
152
|
fetchJson,
|
|
@@ -183,7 +172,7 @@ export function createRegistry({ ctx, config, adapters }) {
|
|
|
183
172
|
label: adapter.meta.label,
|
|
184
173
|
fetchedAt: Date.now(),
|
|
185
174
|
latencyMs: Date.now() - startedAt,
|
|
186
|
-
source: id === 'antigravity' ? 'agy-
|
|
175
|
+
source: id === 'antigravity' ? 'agy-cli' : effectiveRecord.kind === 'api-key' ? 'api-key' : 'cloud-aksk',
|
|
187
176
|
};
|
|
188
177
|
|
|
189
178
|
if (finalResult.status === 'ok' || finalResult.status === 'unconfigured') {
|
package/host/scheduler.js
CHANGED
|
@@ -1,9 +1,14 @@
|
|
|
1
1
|
import { existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from 'node:fs';
|
|
2
|
+
import { homedir } from 'node:os';
|
|
2
3
|
import { dirname, join, resolve } from 'node:path';
|
|
3
4
|
|
|
4
|
-
import { resolveDshHome } from './adapters/antigravity.js';
|
|
5
5
|
import { BalanceError, redact } from './result.js';
|
|
6
6
|
|
|
7
|
+
function resolveDshHome() {
|
|
8
|
+
const fromEnv = typeof process.env.DSH_HOME === 'string' ? process.env.DSH_HOME.trim() : '';
|
|
9
|
+
return fromEnv || join(homedir(), '.dsh');
|
|
10
|
+
}
|
|
11
|
+
|
|
7
12
|
export const MIN_INTERVAL_MINUTES = 1;
|
|
8
13
|
export const MAX_INTERVAL_MINUTES = 1440;
|
|
9
14
|
export const DEFAULT_INTERVAL_MINUTES = 30;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-balance-hub",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Query recharge balances and Plan quotas of 9 AI providers (including Coding Plans, Google Antigravity, and scheduled refresh) in one sidebar panel and via a balance_query tool.",
|
|
6
6
|
"license": "MIT",
|