Open History to see the last commit
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`. Probe failures stay typed so workspace snapshots do not
13 * cache an empty catalog; machine-level discovery recovers them to an empty
14 * list without degrading the provider.
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 Schema from "effect/Schema";
22import { ChildProcess } 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
29class GrokSkillsProbeError extends Schema.TaggedError<GrokSkillsProbeError>()(
30 "GrokSkillsProbeError",
31 {
32 stage: Schema.Literals(["spawn", "timeout", "exit", "decode"]),
33 cwd: Schema.optional(Schema.String),
34 exitCode: Schema.optional(Schema.Number),
35 cause: Schema.optional(Schema.Defect()),
36 },
37) {
38 override get message(): string {
39 const location = this.cwd === undefined ? "" : ` for '${this.cwd}'`;
40 const exitCode = this.exitCode === undefined ? "" : ` with exit code ${this.exitCode}`;
41 return `\`grok inspect --json\` failed during ${this.stage}${location}${exitCode}.`;
42 }
43}
44
45/**
46 * Map `grok inspect --json` output onto provider skills. Entries without a
47 * name or a filesystem path are skipped; `userInvocable: false` skills are
48 * kept but disabled so pickers that filter on `enabled` hide them.
49 */
50function decodeGrokInspectSkills(stdout: string): ReadonlyArray<ServerProviderSkill> | undefined {
51 let parsed: unknown;
52 try {
53 parsed = JSON.parse(stdout);
54 } catch {
55 return undefined;
56 }
57 if (typeof parsed !== "object" || parsed === null) {
58 return undefined;
59 }
60 const entries = (parsed as Record<string, unknown>).skills;
61 if (!Array.isArray(entries)) {
62 return undefined;
63 }
64
65 const skillsByName = new Map<string, ServerProviderSkill>();
66 for (const entry of entries) {
67 if (typeof entry !== "object" || entry === null) {
68 continue;
69 }
70 const record = entry as Record<string, unknown>;
71 const name = typeof record.name === "string" ? record.name.trim() : "";
72 const source =
73 typeof record.source === "object" && record.source !== null
74 ? (record.source as Record<string, unknown>)
75 : undefined;
76 const path = typeof source?.path === "string" ? source.path.trim() : "";
77 if (!name || !path) {
78 continue;
79 }
80 const scope = typeof source?.type === "string" ? source.type.trim() : "";
81 const description = typeof record.description === "string" ? record.description.trim() : "";
82 skillsByName.set(name, {
83 name,
84 path,
85 enabled: record.userInvocable !== false,
86 ...(scope ? { scope } : {}),
87 ...(description ? { description } : {}),
88 });
89 }
90
91 return [...skillsByName.values()].sort((left, right) => left.name.localeCompare(right.name));
92}
93
94/**
95 * Run `grok inspect --json` and map the reported catalog onto provider
96 * skills. Callers that need best-effort discovery can recover this effect to
97 * an empty list; workspace callers leave failures typed so they are not cached.
98 */
99export const discoverGrokSkills = Effect.fn("discoverGrokSkills")(function* (
100 grokSettings: Pick<GrokSettings, "binaryPath">,
101 environment: NodeJS.ProcessEnv = process.env,
102 cwd?: string,
103) {
104 const command = grokSettings.binaryPath || "grok";
105 const inspectResult = yield* Effect.gen(function* () {
106 const spawnCommand = yield* resolveSpawnCommand(command, ["inspect", "--json"], {
107 env: environment,
108 });
109 return yield* spawnAndCollect(
110 command,
111 ChildProcess.make(spawnCommand.command, spawnCommand.args, {
112 ...(cwd ? { cwd } : {}),
113 env: environment,
114 shell: spawnCommand.shell,
115 }),
116 );
117 }).pipe(
118 Effect.mapError(
119 (cause) =>
120 new GrokSkillsProbeError({
121 stage: "spawn",
122 ...(cwd ? { cwd } : {}),
123 cause,
124 }),
125 ),
126 Effect.timeoutOption(GROK_SKILLS_PROBE_TIMEOUT_MS),
127 );
128
129 if (Option.isNone(inspectResult)) {
130 return yield* new GrokSkillsProbeError({
131 stage: "timeout",
132 ...(cwd ? { cwd } : {}),
133 });
134 }
135 const output = inspectResult.value;
136 if (output.code !== 0) {
137 return yield* new GrokSkillsProbeError({
138 stage: "exit",
139 ...(cwd ? { cwd } : {}),
140 exitCode: output.code,
141 });
142 }
143 const skills = decodeGrokInspectSkills(output.stdout);
144 if (!skills) {
145 return yield* new GrokSkillsProbeError({
146 stage: "decode",
147 ...(cwd ? { cwd } : {}),
148 });
149 }
150 return skills;
151});