|
| 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 | + */ |
| 18 | +import type { GrokSettings, ServerProviderSkill } from "@t3tools/contracts"; |
| 19 | +import * as Effect from "effect/Effect"; |
| 20 | +import * as Option from "effect/Option"; |
| 21 | +import * as Result from "effect/Result"; |
| 22 | +import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process"; |
| 23 | +import { resolveSpawnCommand } from "@t3tools/shared/shell"; |
| 24 | + |
| 25 | +import { spawnAndCollect } from "../providerSnapshot.ts"; |
| 26 | + |
| 27 | +const 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 | + */ |
| 34 | +export 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)); |
| 76 | +} |
| 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 | + */ |
| 83 | +export const discoverGrokSkills = Effect.fn("discoverGrokSkills")(function* ( |
| 84 | + grokSettings: Pick<GrokSettings, "binaryPath">, |
| 85 | + environment: NodeJS.ProcessEnv = process.env, |
| 86 | +): Effect.fn.Return< |
| 87 | + ReadonlyArray<ServerProviderSkill>, |
| 88 | + never, |
| 89 | + ChildProcessSpawner.ChildProcessSpawner |
| 90 | +> { |
| 91 | + const command = grokSettings.binaryPath || "grok"; |
| 92 | + const inspectResult = yield* Effect.gen(function* () { |
| 93 | + const spawnCommand = yield* resolveSpawnCommand(command, ["inspect", "--json"], { |
| 94 | + env: environment, |
| 95 | + }); |
| 96 | + return yield* spawnAndCollect( |
| 97 | + command, |
| 98 | + ChildProcess.make(spawnCommand.command, spawnCommand.args, { |
| 99 | + env: environment, |
| 100 | + shell: spawnCommand.shell, |
| 101 | + }), |
| 102 | + ); |
| 103 | + }).pipe(Effect.timeoutOption(GROK_SKILLS_PROBE_TIMEOUT_MS), Effect.result); |
| 104 | + |
| 105 | + if (Result.isFailure(inspectResult) || Option.isNone(inspectResult.success)) { |
| 106 | + yield* Effect.logDebug("Grok skill discovery failed; continuing without skills."); |
| 107 | + return []; |
| 108 | + } |
| 109 | + const output = inspectResult.success.value; |
| 110 | + if (output.code !== 0) { |
| 111 | + yield* Effect.logDebug("Grok skill discovery exited non-zero; continuing without skills.", { |
| 112 | + exitCode: output.code, |
| 113 | + }); |
| 114 | + return []; |
| 115 | + } |
| 116 | + return parseGrokInspectSkills(output.stdout); |
| 117 | +}); |
0 commit comments