|
| 1 | +// altimate_change start - LLM-based dynamic skill selection |
| 2 | +import { generateObject } from "ai" |
| 3 | +import type { LanguageModelV2 } from "@openrouter/ai-sdk-provider" |
| 4 | +import z from "zod" |
| 5 | +import { Provider } from "../provider/provider" |
| 6 | +import { Log } from "../util/log" |
| 7 | +import type { Skill } from "../skill" |
| 8 | +import type { Fingerprint } from "./fingerprint" |
| 9 | + |
| 10 | +const log = Log.create({ service: "skill-selector" }) |
| 11 | + |
| 12 | +const TIMEOUT_MS = 3_000 |
| 13 | +const MAX_SKILLS = 15 |
| 14 | + |
| 15 | +// Session cache keyed by working directory — invalidates if project changes. |
| 16 | +let cachedResult: Skill.Info[] | undefined |
| 17 | +let cachedCwd: string | undefined |
| 18 | + |
| 19 | +/** Reset the session cache (exported for testing) */ |
| 20 | +export function resetSkillSelectorCache(): void { |
| 21 | + cachedResult = undefined |
| 22 | + cachedCwd = undefined |
| 23 | +} |
| 24 | + |
| 25 | +export interface SkillSelectorDeps { |
| 26 | + resolveModel: () => Promise<LanguageModelV2 | undefined> |
| 27 | + generate: (params: { |
| 28 | + model: LanguageModelV2 |
| 29 | + temperature: number |
| 30 | + schema: z.ZodType |
| 31 | + messages: Array<{ role: "system" | "user"; content: string }> |
| 32 | + }) => Promise<{ object: { selected: string[] } }> |
| 33 | +} |
| 34 | + |
| 35 | +async function defaultResolveModel(): Promise<LanguageModelV2 | undefined> { |
| 36 | + try { |
| 37 | + const { providerID, modelID } = await Provider.defaultModel() |
| 38 | + const model = await Provider.getModel(providerID, modelID) |
| 39 | + return Provider.getLanguage(model) |
| 40 | + } catch { |
| 41 | + return undefined |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +const defaultDeps: SkillSelectorDeps = { |
| 46 | + resolveModel: defaultResolveModel, |
| 47 | + generate: generateObject as any, |
| 48 | +} |
| 49 | + |
| 50 | +/** |
| 51 | + * Use the configured model to select relevant skills based on the project fingerprint. |
| 52 | + * Results are cached for the session — the LLM is only called once. |
| 53 | + * |
| 54 | + * Graceful fallback: returns ALL skills on any failure (matches pre-feature behavior). |
| 55 | + */ |
| 56 | +export async function selectSkillsWithLLM( |
| 57 | + skills: Skill.Info[], |
| 58 | + fingerprint: Fingerprint.Result | undefined, |
| 59 | + deps?: SkillSelectorDeps, |
| 60 | +): Promise<Skill.Info[]> { |
| 61 | + // Return cached result if cwd hasn't changed (0ms) |
| 62 | + const cwd = fingerprint?.cwd |
| 63 | + if (cachedResult && cwd === cachedCwd) { |
| 64 | + log.info("returning cached skill selection", { |
| 65 | + count: cachedResult.length, |
| 66 | + }) |
| 67 | + return cachedResult |
| 68 | + } |
| 69 | + |
| 70 | + const { resolveModel, generate } = deps ?? defaultDeps |
| 71 | + |
| 72 | + function cache(result: Skill.Info[]): Skill.Info[] { |
| 73 | + cachedResult = result |
| 74 | + cachedCwd = cwd |
| 75 | + return result |
| 76 | + } |
| 77 | + |
| 78 | + try { |
| 79 | + const model = await resolveModel() |
| 80 | + if (!model) { |
| 81 | + log.info("no small model available, returning all skills") |
| 82 | + return cache(skills) |
| 83 | + } |
| 84 | + |
| 85 | + // Build compact skill list for the prompt |
| 86 | + const skillList = skills.map((s) => ({ |
| 87 | + name: s.name, |
| 88 | + description: s.description, |
| 89 | + })) |
| 90 | + |
| 91 | + const envContext = |
| 92 | + fingerprint && fingerprint.tags.length > 0 |
| 93 | + ? fingerprint.tags.join(", ") |
| 94 | + : "none detected" |
| 95 | + |
| 96 | + const params = { |
| 97 | + model, |
| 98 | + temperature: 0, |
| 99 | + schema: z.object({ selected: z.array(z.string()) }), |
| 100 | + messages: [ |
| 101 | + { |
| 102 | + role: "system" as const, |
| 103 | + content: [ |
| 104 | + "You are a skill selector for a coding assistant.", |
| 105 | + "Given a project environment and available skills, select which skills are relevant for this project.", |
| 106 | + "Return ONLY skill names the user likely needs. Select 0-15 skills.", |
| 107 | + "Prefer fewer, more relevant skills over many loosely related ones.", |
| 108 | + ].join("\n"), |
| 109 | + }, |
| 110 | + { |
| 111 | + role: "user" as const, |
| 112 | + content: [ |
| 113 | + `Project environment: ${envContext}`, |
| 114 | + "", |
| 115 | + `Available skills: ${JSON.stringify(skillList)}`, |
| 116 | + ].join("\n"), |
| 117 | + }, |
| 118 | + ], |
| 119 | + } |
| 120 | + |
| 121 | + const result = await Promise.race([ |
| 122 | + generate(params), |
| 123 | + new Promise<never>((_, reject) => |
| 124 | + setTimeout(() => reject(new Error("skill selection timeout")), TIMEOUT_MS), |
| 125 | + ), |
| 126 | + ]) |
| 127 | + |
| 128 | + const selected = result.object.selected.slice(0, MAX_SKILLS) |
| 129 | + |
| 130 | + // Zero-selection guard |
| 131 | + if (selected.length === 0) { |
| 132 | + log.info("LLM returned zero skills, returning all") |
| 133 | + return cache(skills) |
| 134 | + } |
| 135 | + |
| 136 | + // Filter skills by returned names |
| 137 | + const selectedSet = new Set(selected) |
| 138 | + const matched = skills.filter((s) => selectedSet.has(s.name)) |
| 139 | + |
| 140 | + // If no valid matches (LLM returned non-existent names), return all |
| 141 | + if (matched.length === 0) { |
| 142 | + log.info("LLM returned no valid skill names, returning all") |
| 143 | + return cache(skills) |
| 144 | + } |
| 145 | + |
| 146 | + log.info("selected skills", { |
| 147 | + count: matched.length, |
| 148 | + names: matched.map((s) => s.name), |
| 149 | + }) |
| 150 | + return cache(matched) |
| 151 | + } catch (e) { |
| 152 | + log.info("skill selection failed, returning all skills", { |
| 153 | + error: e instanceof Error ? e.message : String(e), |
| 154 | + }) |
| 155 | + return cache(skills) |
| 156 | + } |
| 157 | +} |
| 158 | +// altimate_change end |
0 commit comments