Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

GrokSkills.ts

119 lines4.4 KB

1/**
2 * GrokSkills — skill discovery for the `$` picker via `grok inspect --json`.
3 *
4 * Unlike Claude Code, the Grok CLI reports its full skill catalog itself:
5 * `grok inspect --json` returns `skills[]` with `name`, `description`,
6 * `source.type` (`user` / `project` / `bundled` / `plugin`), `source.path`
7 * (the absolute `SKILL.md` path), and `userInvocable`. Asking the CLI beats
8 * scanning the filesystem because the catalog honors Grok's own skill config
9 * (ignore lists, disabled skills) and includes plugin skills, which live
10 * three levels deep under `~/.grok/installed-plugins/` where a flat scan
11 * cannot see them. This mirrors how the Codex app-server reports skills over
12 * `skills/list`. Discovery is best-effort: an older CLI without `inspect`,
13 * a timeout, or malformed output yields an empty list, never a degraded
14 * provider snapshot.
15 *
16 * @module provider/Drivers/GrokSkills
17 */
18import type { GrokSettings, ServerProviderSkill } from "@t3tools/contracts";
19import * as Effect from "effect/Effect";
20import * as Option from "effect/Option";
21import * as Result from "effect/Result";
22import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process";
23import { resolveSpawnCommand } from "@t3tools/shared/shell";
24​
25import { spawnAndCollect } from "../providerSnapshot.ts";
26​
27const GROK_SKILLS_PROBE_TIMEOUT_MS = 4_000;
28​
29/**
30 * Map `grok inspect --json` output onto provider skills. Entries without a
31 * name or a filesystem path are skipped; `userInvocable: false` skills are
32 * kept but disabled so pickers that filter on `enabled` hide them.
33 */
34export function parseGrokInspectSkills(stdout: string): ReadonlyArray<ServerProviderSkill> {
35 let parsed: unknown;
36 try {
37 parsed = JSON.parse(stdout);
38 } catch {
39 return [];
40 }
41 if (typeof parsed !== "object" || parsed === null) {
42 return [];
43 }
44 const entries = (parsed as Record<string, unknown>).skills;
45 if (!Array.isArray(entries)) {
46 return [];
47 }
48​
49 const skillsByName = new Map<string, ServerProviderSkill>();
50 for (const entry of entries) {
51 if (typeof entry !== "object" || entry === null) {
52 continue;
53 }
54 const record = entry as Record<string, unknown>;
55 const name = typeof record.name === "string" ? record.name.trim() : "";
56 const source =
57 typeof record.source === "object" && record.source !== null
58 ? (record.source as Record<string, unknown>)
59 : undefined;
60 const path = typeof source?.path === "string" ? source.path.trim() : "";
61 if (!name || !path) {
62 continue;
63 }
64 const scope = typeof source?.type === "string" ? source.type.trim() : "";
65 const description = typeof record.description === "string" ? record.description.trim() : "";
66 skillsByName.set(name, {
67 name,
68 path,
69 enabled: record.userInvocable !== false,
70 ...(scope ? { scope } : {}),
71 ...(description ? { description } : {}),
72 });
73 }
74​
75 return [...skillsByName.values()].sort((left, right) => left.name.localeCompare(right.name));
77​
78/**
79 * Run `grok inspect --json` and map the reported catalog onto provider
80 * skills. Never fails: any spawn error, non-zero exit, or timeout resolves
81 * to an empty list.
82 */
83export const discoverGrokSkills = Effect.fn("discoverGrokSkills")(function* (
84 grokSettings: Pick<GrokSettings, "binaryPath">,
85 environment: NodeJS.ProcessEnv = process.env,
86 cwd?: string,
87): Effect.fn.Return<
88 ReadonlyArray<ServerProviderSkill>,
89 never,
90 ChildProcessSpawner.ChildProcessSpawner
91> {
92 const command = grokSettings.binaryPath || "grok";
93 const inspectResult = yield* Effect.gen(function* () {
94 const spawnCommand = yield* resolveSpawnCommand(command, ["inspect", "--json"], {
95 env: environment,
96 });
97 return yield* spawnAndCollect(
98 command,
99 ChildProcess.make(spawnCommand.command, spawnCommand.args, {
100 ...(cwd ? { cwd } : {}),
101 env: environment,
102 shell: spawnCommand.shell,
103 }),
104 );
105 }).pipe(Effect.timeoutOption(GROK_SKILLS_PROBE_TIMEOUT_MS), Effect.result);
106​
107 if (Result.isFailure(inspectResult) || Option.isNone(inspectResult.success)) {
108 yield* Effect.logDebug("Grok skill discovery failed; continuing without skills.");
109 return [];
110 }
111 const output = inspectResult.success.value;
112 if (output.code !== 0) {
113 yield* Effect.logDebug("Grok skill discovery exited non-zero; continuing without skills.", {
114 exitCode: output.code,
115 });
116 return [];
117 }
118 return parseGrokInspectSkills(output.stdout);
119});