Repository navigation
feat(server): list Grok CLI skills in the $ picker #6518
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,206 @@ | ||
| import { describe, expect, it } from "@effect/vitest"; | ||
| import * as Duration from "effect/Duration"; | ||
| import * as Effect from "effect/Effect"; | ||
| import * as Fiber from "effect/Fiber"; | ||
| import * as Layer from "effect/Layer"; | ||
| import * as Sink from "effect/Sink"; | ||
| import * as Stream from "effect/Stream"; | ||
| import { TestClock } from "effect/testing"; | ||
| import { ChildProcessSpawner } from "effect/unstable/process"; | ||
|
|
||
| import { DEFAULT_TIMEOUT_MS } from "../providerSnapshot.ts"; | ||
| import { discoverGrokSkills, parseGrokInspectSkills } from "./GrokSkills.ts"; | ||
|
|
||
| const hangingInspectSpawner = ChildProcessSpawner.make(() => | ||
| Effect.succeed( | ||
| ChildProcessSpawner.makeHandle({ | ||
| pid: ChildProcessSpawner.ProcessId(1), | ||
| exitCode: Effect.never, | ||
| isRunning: Effect.succeed(true), | ||
| kill: () => Effect.void, | ||
| unref: Effect.succeed(Effect.void), | ||
| stdin: Sink.drain, | ||
| stdout: Stream.empty, | ||
| stderr: Stream.empty, | ||
| all: Stream.empty, | ||
| getInputFd: () => Sink.drain, | ||
| getOutputFd: () => Stream.empty, | ||
| }), | ||
| ), | ||
| ); | ||
|
|
||
| const inspectSpawner = (stdout: string, code: number) => | ||
| ChildProcessSpawner.make(() => | ||
| Effect.succeed( | ||
| ChildProcessSpawner.makeHandle({ | ||
| pid: ChildProcessSpawner.ProcessId(1), | ||
| exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(code)), | ||
| isRunning: Effect.succeed(false), | ||
| kill: () => Effect.void, | ||
| unref: Effect.succeed(Effect.void), | ||
| stdin: Sink.drain, | ||
| stdout: Stream.encodeText(Stream.make(stdout)), | ||
| stderr: Stream.empty, | ||
| all: Stream.empty, | ||
| getInputFd: () => Sink.drain, | ||
| getOutputFd: () => Stream.empty, | ||
| }), | ||
| ), | ||
| ); | ||
|
|
||
| describe("parseGrokInspectSkills", () => { | ||
| it("maps inspect JSON onto ServerProviderSkill rows", () => { | ||
| expect( | ||
| parseGrokInspectSkills({ | ||
| skills: [ | ||
| { | ||
| name: "handoff-session", | ||
| description: "Write a session handoff.", | ||
| userInvocable: true, | ||
| source: { type: "user", path: "/tmp/.grok/skills/handoff-session/SKILL.md" }, | ||
| }, | ||
| { | ||
| name: "ce-plan", | ||
| description: "Create structured plans.", | ||
| userInvocable: true, | ||
| source: { | ||
| type: "plugin", | ||
| plugin_name: "compound-engineering", | ||
| path: "/tmp/plugins/ce-plan/SKILL.md", | ||
| }, | ||
| }, | ||
| ], | ||
| }), | ||
| ).toEqual([ | ||
| { | ||
| name: "handoff-session", | ||
| description: "Write a session handoff.", | ||
| path: "/tmp/.grok/skills/handoff-session/SKILL.md", | ||
| scope: "user", | ||
| enabled: true, | ||
| }, | ||
| { | ||
| name: "ce-plan", | ||
| description: "Create structured plans.", | ||
| path: "/tmp/plugins/ce-plan/SKILL.md", | ||
| scope: "plugin:compound-engineering", | ||
| enabled: true, | ||
| }, | ||
| ]); | ||
| }); | ||
|
|
||
| it("skips malformed rows and keeps the first name on collisions", () => { | ||
| expect( | ||
| parseGrokInspectSkills({ | ||
| skills: [ | ||
| { | ||
| name: "review", | ||
| source: { type: "project", path: "/repo/.grok/skills/review/SKILL.md" }, | ||
| }, | ||
| { name: "review", source: { type: "user", path: "/home/.grok/skills/review/SKILL.md" } }, | ||
| { name: "broken" }, | ||
| null, | ||
| "nope", | ||
| ], | ||
| }), | ||
| ).toEqual([ | ||
| { | ||
| name: "review", | ||
| path: "/repo/.grok/skills/review/SKILL.md", | ||
| scope: "project", | ||
| enabled: true, | ||
| }, | ||
| ]); | ||
| }); | ||
|
|
||
| it("marks non-invocable or disabled skills as disabled", () => { | ||
| const skills = parseGrokInspectSkills({ | ||
| skills: [ | ||
| { | ||
| name: "hidden", | ||
| userInvocable: false, | ||
| source: { type: "bundled", path: "/bundled/hidden/SKILL.md" }, | ||
| }, | ||
| { | ||
| name: "off", | ||
| disabled: true, | ||
| source: { type: "user", path: "/home/.grok/skills/off/SKILL.md" }, | ||
| }, | ||
| ], | ||
| }); | ||
| expect(skills.map((skill) => ({ name: skill.name, enabled: skill.enabled }))).toEqual([ | ||
| { name: "hidden", enabled: false }, | ||
| { name: "off", enabled: false }, | ||
| ]); | ||
| }); | ||
|
|
||
| it("returns an empty list for junk input", () => { | ||
| expect(parseGrokInspectSkills(null)).toEqual([]); | ||
| expect(parseGrokInspectSkills({})).toEqual([]); | ||
| expect(parseGrokInspectSkills({ skills: "nope" })).toEqual([]); | ||
| }); | ||
| }); | ||
|
|
||
| describe("discoverGrokSkills", () => { | ||
| it.effect("returns [] when inspect starts but never exits", () => | ||
| Effect.gen(function* () { | ||
| const fiber = yield* discoverGrokSkills({ binaryPath: process.execPath }).pipe( | ||
| Effect.provide( | ||
| Layer.succeed(ChildProcessSpawner.ChildProcessSpawner, hangingInspectSpawner), | ||
| ), | ||
| Effect.forkChild, | ||
| ); | ||
| yield* Effect.yieldNow; | ||
| yield* TestClock.adjust(Duration.millis(DEFAULT_TIMEOUT_MS)); | ||
| expect(yield* Fiber.join(fiber)).toEqual([]); | ||
| }), | ||
| ); | ||
|
|
||
| it.effect("keeps a successful inspect catalog", () => | ||
| Effect.gen(function* () { | ||
| const skills = yield* discoverGrokSkills({ binaryPath: process.execPath }).pipe( | ||
| Effect.provide( | ||
| Layer.succeed( | ||
| ChildProcessSpawner.ChildProcessSpawner, | ||
| inspectSpawner( | ||
| JSON.stringify({ | ||
| skills: [ | ||
| { | ||
| name: "handoff-session", | ||
| description: "Write a session handoff.", | ||
| userInvocable: true, | ||
| source: { type: "user", path: "/tmp/.grok/skills/handoff-session/SKILL.md" }, | ||
| }, | ||
| ], | ||
| }), | ||
| 0, | ||
| ), | ||
| ), | ||
| ), | ||
| ); | ||
| expect(skills).toEqual([ | ||
| { | ||
| name: "handoff-session", | ||
| description: "Write a session handoff.", | ||
| path: "/tmp/.grok/skills/handoff-session/SKILL.md", | ||
| scope: "user", | ||
| enabled: true, | ||
| }, | ||
| ]); | ||
| }), | ||
| ); | ||
|
|
||
| it.effect("returns [] when inspect exits non-zero", () => | ||
| Effect.gen(function* () { | ||
| const skills = yield* discoverGrokSkills({ binaryPath: process.execPath }).pipe( | ||
| Effect.provide( | ||
| Layer.succeed( | ||
| ChildProcessSpawner.ChildProcessSpawner, | ||
| inspectSpawner("inspect failed: secret-token-value", 2), | ||
| ), | ||
| ), | ||
| ); | ||
| expect(skills).toEqual([]); | ||
| }), | ||
| ); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| /** | ||
| * GrokSkills — surface Grok Build skills on the provider snapshot for the `$` | ||
| * picker. The CLI already discovers skills (`grok inspect --json`); T3 does | ||
| * not walk the filesystem a second time. | ||
| * | ||
| * @module provider/Drivers/GrokSkills | ||
| */ | ||
| import type { GrokSettings, ServerProviderSkill } from "@t3tools/contracts"; | ||
| import * as Effect from "effect/Effect"; | ||
| import * as Option from "effect/Option"; | ||
| import * as Schema from "effect/Schema"; | ||
| import * as ChildProcess from "effect/unstable/process/ChildProcess"; | ||
| import { resolveSpawnCommand } from "@t3tools/shared/shell"; | ||
|
|
||
| import { DEFAULT_TIMEOUT_MS, spawnAndCollect } from "../providerSnapshot.ts"; | ||
|
|
||
| const UnknownFromJsonString = Schema.fromJsonString(Schema.Unknown); | ||
|
|
||
| type GrokInspectSkillSource = { | ||
| readonly type?: unknown; | ||
| readonly path?: unknown; | ||
| readonly plugin_name?: unknown; | ||
| }; | ||
|
|
||
| type GrokInspectSkill = { | ||
| readonly name?: unknown; | ||
| readonly description?: unknown; | ||
| readonly userInvocable?: unknown; | ||
| readonly disabled?: unknown; | ||
| readonly source?: GrokInspectSkillSource; | ||
| }; | ||
|
|
||
| function asTrimmedString(value: unknown): string | undefined { | ||
| if (typeof value !== "string") return undefined; | ||
| const trimmed = value.trim(); | ||
| return trimmed.length > 0 ? trimmed : undefined; | ||
| } | ||
|
|
||
| function inspectScope(source: GrokInspectSkillSource | undefined): string | undefined { | ||
| const type = asTrimmedString(source?.type); | ||
| if (!type) return undefined; | ||
| const pluginName = asTrimmedString(source?.plugin_name); | ||
| return type === "plugin" && pluginName ? `plugin:${pluginName}` : type; | ||
| } | ||
|
|
||
| export function parseGrokInspectSkills(input: unknown): ReadonlyArray<ServerProviderSkill> { | ||
| if (typeof input !== "object" || input === null) { | ||
| return []; | ||
| } | ||
| const rawSkills = (input as { readonly skills?: unknown }).skills; | ||
| if (!Array.isArray(rawSkills)) { | ||
| return []; | ||
| } | ||
|
|
||
| const skills: ServerProviderSkill[] = []; | ||
| const seen = new Set<string>(); | ||
| for (const entry of rawSkills) { | ||
| if (typeof entry !== "object" || entry === null) { | ||
| continue; | ||
| } | ||
| const skill = entry as GrokInspectSkill; | ||
| const name = asTrimmedString(skill.name); | ||
| const path = asTrimmedString(skill.source?.path); | ||
| if (!name || !path || seen.has(name)) { | ||
| continue; | ||
| } | ||
| seen.add(name); | ||
| const description = asTrimmedString(skill.description); | ||
| const scope = inspectScope(skill.source); | ||
| const enabled = skill.userInvocable !== false && skill.disabled !== true; | ||
| skills.push({ | ||
| name, | ||
| path, | ||
| enabled, | ||
| ...(description ? { description } : {}), | ||
| ...(scope ? { scope } : {}), | ||
| }); | ||
| } | ||
| return skills; | ||
| } | ||
|
|
||
| /** | ||
| * Ask the Grok CLI what skills it would load in this cwd. Failure is empty: | ||
| * a broken inspect must not take the provider snapshot down. | ||
| */ | ||
| export const discoverGrokSkills = Effect.fn("discoverGrokSkills")(function* ( | ||
| grokSettings: Pick<GrokSettings, "binaryPath">, | ||
| environment: NodeJS.ProcessEnv = process.env, | ||
| cwd?: string, | ||
| ) { | ||
| const command = grokSettings.binaryPath || "grok"; | ||
| const spawnCommand = yield* resolveSpawnCommand(command, ["inspect", "--json"], { | ||
| env: environment, | ||
| }).pipe(Effect.orElseSucceed(() => null)); | ||
| if (!spawnCommand) { | ||
| return []; | ||
| } | ||
|
|
||
| const result = yield* spawnAndCollect( | ||
| command, | ||
| ChildProcess.make(spawnCommand.command, spawnCommand.args, { | ||
| env: environment, | ||
| shell: spawnCommand.shell, | ||
| ...(cwd ? { cwd } : {}), | ||
| }), | ||
| ).pipe( | ||
| Effect.timeoutOption(DEFAULT_TIMEOUT_MS), | ||
| Effect.map((inspect) => (Option.isSome(inspect) ? inspect.value : null)), | ||
| Effect.orElseSucceed(() => null), | ||
| ); | ||
| if (!result || result.code !== 0) { | ||
| return []; | ||
| } | ||
|
|
||
| const parsed = yield* Schema.decodeUnknownEffect(UnknownFromJsonString)(result.stdout).pipe( | ||
| Effect.orElseSucceed(() => null), | ||
| ); | ||
| if (parsed === null) { | ||
| return []; | ||
| } | ||
| return parseGrokInspectSkills(parsed); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.