@coda-console/grok 1.5.0 → 1.6.0
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 +1 -1
- package/package.json +3 -3
- package/scripts/quota.mjs +10 -10
- package/scripts/resources.mjs +11 -0
- package/scripts/setup.mjs +2 -2
- package/scripts/panel.mjs +0 -11
package/README.md
CHANGED
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"id": "grok",
|
|
4
4
|
"displayName": "Grok",
|
|
5
5
|
"description": "用 Grok CLI 跑会话",
|
|
6
|
-
"version": "1.
|
|
6
|
+
"version": "1.6.0",
|
|
7
7
|
"categories": [
|
|
8
8
|
"Agent"
|
|
9
9
|
],
|
|
@@ -31,11 +31,11 @@
|
|
|
31
31
|
"setupHint": "请在「资源」里为运行机添加 Grok。"
|
|
32
32
|
}
|
|
33
33
|
],
|
|
34
|
-
"
|
|
34
|
+
"resources": [
|
|
35
35
|
{
|
|
36
36
|
"id": "account",
|
|
37
37
|
"title": "Grok",
|
|
38
|
-
"command": "node \"$CODA_PLUGIN_PATH/scripts/
|
|
38
|
+
"command": "node \"$CODA_PLUGIN_PATH/scripts/resources.mjs\"",
|
|
39
39
|
"maxAge": "PT5M",
|
|
40
40
|
"signIn": {
|
|
41
41
|
"command": "g=\"$HOME/.grok/bin/grok\"; [ -x \"$g\" ] || g=grok; BROWSER=/usr/bin/true NO_COLOR=1 exec \"$g\" login --device-auth",
|
package/scripts/quota.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { createHash } from 'node:crypto';
|
|
2
2
|
|
|
3
|
-
// What the「Grok」
|
|
3
|
+
// What the「Grok」resource type shows (docs/plugins.md「内置插件:Grok」): who is signed in and the plan's coding credits, as the
|
|
4
4
|
// CLI itself answers them (`_x.ai/auth/check_subscription`, `_x.ai/auth/info`, `_x.ai/billing`: what its `/usage` reads).
|
|
5
5
|
const line = (value, max = 80) => typeof value === 'string' ? [...value.replace(/\s+/g, ' ').trim()].slice(0, max).join('') : '';
|
|
6
6
|
const instant = value => typeof value === 'string' && Number.isFinite(Date.parse(value)) ? new Date(value).toISOString() : null;
|
|
@@ -25,22 +25,22 @@ export function windows(document) {
|
|
|
25
25
|
const used = typeof config.creditUsagePercent === 'number' && Number.isFinite(config.creditUsagePercent) ? Math.min(100, Math.max(0, config.creditUsagePercent))
|
|
26
26
|
: limit > 0 ? Math.min(100, cents(config.used) / limit * 100) : 0;
|
|
27
27
|
const type = config.currentPeriod?.type, left = Math.round(100 - used), resets = instant(config.currentPeriod?.end ?? config.billingPeriodEnd);
|
|
28
|
-
const meters = [{
|
|
29
|
-
value: `剩余 ${left}%`, fraction: left / 100, ...(resets ? { time: resets
|
|
28
|
+
const meters = [{ label: type === 'USAGE_PERIOD_TYPE_WEEKLY' ? '7 天' : type === 'USAGE_PERIOD_TYPE_MONTHLY' ? '月度' : '额度',
|
|
29
|
+
value: `剩余 ${left}%`, fraction: left / 100, ...(resets ? { time: resets } : {}) }];
|
|
30
30
|
// A zero amount still arrives as `{}`; the older shape leaves on-demand spending out, and it is what went past the allowance.
|
|
31
31
|
const cap = cents(config.onDemandCap);
|
|
32
32
|
const onDemand = config.onDemandUsed != null ? cents(config.onDemandUsed) : Math.max(0, cents(config.used) - limit);
|
|
33
33
|
if (cap > 0) {
|
|
34
34
|
const share = Math.round(Math.max(0, cap - onDemand) / cap * 100);
|
|
35
|
-
meters.push({
|
|
35
|
+
meters.push({ label: '按需用量', value: `剩余 ${share}%`, fraction: share / 100 });
|
|
36
36
|
}
|
|
37
37
|
return meters;
|
|
38
38
|
}
|
|
39
|
-
// The
|
|
40
|
-
export function
|
|
39
|
+
// The resources from what the CLI answered: the login, its name and its credits; none when no one is signed in.
|
|
40
|
+
export function resources([subscription, info, billing]) {
|
|
41
41
|
const who = account(subscription, info);
|
|
42
|
-
if (!who) return {
|
|
43
|
-
const
|
|
44
|
-
return {
|
|
45
|
-
|
|
42
|
+
if (!who) return { resources: [] };
|
|
43
|
+
const values = windows(billing);
|
|
44
|
+
return { resources: [{ name: who.name || 'Grok', ...(who.id ? { identity: `grok:${who.id}` } : {}), values,
|
|
45
|
+
...(values.length ? {} : { error: '暂时无法获取额度:Grok 没有给出额度' }) }] };
|
|
46
46
|
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// The「Grok」resources, printed once as `{ resources }`. No login file here shows nothing (the type is then offered under
|
|
2
|
+
// 「添加资源」); a CLI that cannot be asked fails the reading, which keeps the last one shown.
|
|
3
|
+
import { statSync } from 'node:fs';
|
|
4
|
+
import { join } from 'node:path';
|
|
5
|
+
import { grokAsk, grokHome } from './ask.mjs';
|
|
6
|
+
import { resources } from './quota.mjs';
|
|
7
|
+
|
|
8
|
+
try { statSync(join(grokHome(), 'auth.json')); }
|
|
9
|
+
catch (error) { if (error.code === 'ENOENT') { console.log('{"resources":[]}'); process.exit(0); } console.error(`读不了 Grok 的登录:${error.code}`); process.exit(1); }
|
|
10
|
+
try { console.log(JSON.stringify(resources(await grokAsk(['_x.ai/auth/check_subscription', '_x.ai/auth/info', '_x.ai/billing'])))); }
|
|
11
|
+
catch (error) { console.error(error.message); process.exit(1); }
|
package/scripts/setup.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// The plugin's setup (`contributes.setup`), which Coda runs on each machine when the user installs or updates the plugin: it
|
|
2
2
|
// ends well once the machine can run the agent. Without a Grok CLI that runs, or with one older than MINIMUM (the CLI whose
|
|
3
3
|
// `agent --no-leader stdio` and `_x.ai/yolo_mode_changed` the agent entry was checked against), Grok's own installer puts
|
|
4
|
-
// the latest in ~/.grok/bin, where the agent looks first, as the「Grok」
|
|
4
|
+
// the latest in ~/.grok/bin, where the agent looks first, as the「Grok」type's install does; a newer one is kept. Why the
|
|
5
5
|
// machine is not ready is said in one line on standard error.
|
|
6
6
|
import { spawnSync } from 'node:child_process';
|
|
7
7
|
import { realpathSync } from 'node:fs';
|
|
@@ -35,7 +35,7 @@ if (process.argv[1] && realpathSync(process.argv[1]) === realpathSync(fileURLToP
|
|
|
35
35
|
const before = needed(grokBinary());
|
|
36
36
|
if (before.action !== 'none') {
|
|
37
37
|
// What the installer prints goes to standard error with the rest of its story.
|
|
38
|
-
// As the
|
|
38
|
+
// As the type's `install`: without SHELL it leaves the shell's configuration alone.
|
|
39
39
|
const installed = spawnSync('bash', ['-c', 'set -o pipefail; curl -fsSL https://x.ai/cli/install.sh | bash'], { stdio: ['ignore', 2, 2],
|
|
40
40
|
env: { ...process.env, SHELL: '', PATH: `${join(homedir(), '.grok/bin')}:${process.env.PATH ?? ''}` } });
|
|
41
41
|
if (installed.status !== 0) { console.error(`${before.reason},安装失败(退出码 ${installed.status ?? installed.signal})`); process.exit(1); }
|
package/scripts/panel.mjs
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
// The「Grok」panel's reading, printed once as `{ items }`. No login file here shows nothing (the panel is then offered under
|
|
2
|
-
// 「添加资源」); a CLI that cannot be asked fails the reading, which keeps the last one shown.
|
|
3
|
-
import { statSync } from 'node:fs';
|
|
4
|
-
import { join } from 'node:path';
|
|
5
|
-
import { grokAsk, grokHome } from './ask.mjs';
|
|
6
|
-
import { panel } from './quota.mjs';
|
|
7
|
-
|
|
8
|
-
try { statSync(join(grokHome(), 'auth.json')); }
|
|
9
|
-
catch (error) { if (error.code === 'ENOENT') { console.log('{"items":[]}'); process.exit(0); } console.error(`读不了 Grok 的登录:${error.code}`); process.exit(1); }
|
|
10
|
-
try { console.log(JSON.stringify(panel(await grokAsk(['_x.ai/auth/check_subscription', '_x.ai/auth/info', '_x.ai/billing'])))); }
|
|
11
|
-
catch (error) { console.error(error.message); process.exit(1); }
|