-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Fix skill autocomplete fallback #1325
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
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,148 @@ | ||
| import fs from "node:fs/promises"; | ||
| import path from "node:path"; | ||
|
|
||
| import type { DiscoveredPluginManifest, DiscoveredPluginRoot } from "./types"; | ||
|
|
||
| const PLUGINS_ENV_VAR = "T3CODE_PLUGIN_DIRS"; | ||
| const DEFAULT_LOCAL_PLUGINS_DIR = "plugins"; | ||
| const PLUGIN_MANIFEST_FILE = "t3-plugin.json"; | ||
|
|
||
| interface RawPluginManifest { | ||
| readonly id?: unknown; | ||
| readonly name?: unknown; | ||
| readonly version?: unknown; | ||
| readonly hostApiVersion?: unknown; | ||
| readonly enabled?: unknown; | ||
| readonly serverEntry?: unknown; | ||
| readonly webEntry?: unknown; | ||
| } | ||
|
|
||
| function trimNonEmpty(value: unknown): string | null { | ||
| if (typeof value !== "string") { | ||
| return null; | ||
| } | ||
| const trimmed = value.trim(); | ||
| return trimmed.length > 0 ? trimmed : null; | ||
| } | ||
|
|
||
| async function pathExists(candidatePath: string): Promise<boolean> { | ||
| try { | ||
| await fs.access(candidatePath); | ||
| return true; | ||
| } catch { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| async function isDirectory(candidatePath: string): Promise<boolean> { | ||
| try { | ||
| return (await fs.stat(candidatePath)).isDirectory(); | ||
| } catch { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| function normalizePluginRoots(cwd: string): string[] { | ||
| const configuredRoots = (process.env[PLUGINS_ENV_VAR] ?? "") | ||
| .split(path.delimiter) | ||
| .map((value) => value.trim()) | ||
| .filter((value) => value.length > 0) | ||
| .map((value) => path.resolve(value)); | ||
| const localRoot = path.resolve(cwd, DEFAULT_LOCAL_PLUGINS_DIR); | ||
| return Array.from(new Set([localRoot, ...configuredRoots])); | ||
| } | ||
|
|
||
| async function discoverRootCandidates(rootPath: string): Promise<DiscoveredPluginRoot[]> { | ||
| if (!(await isDirectory(rootPath))) { | ||
| return []; | ||
| } | ||
|
|
||
| const directManifestPath = path.join(rootPath, PLUGIN_MANIFEST_FILE); | ||
| if (await pathExists(directManifestPath)) { | ||
| return [{ rootDir: rootPath, manifestPath: directManifestPath }]; | ||
| } | ||
|
|
||
| const entries = await fs.readdir(rootPath, { withFileTypes: true }).catch(() => []); | ||
| const childCandidates = entries | ||
| .filter((entry) => entry.isDirectory()) | ||
| .map((entry) => ({ | ||
| rootDir: path.join(rootPath, entry.name), | ||
| manifestPath: path.join(rootPath, entry.name, PLUGIN_MANIFEST_FILE), | ||
| })); | ||
|
|
||
| const existingCandidates = await Promise.all( | ||
| childCandidates.map(async (candidate) => | ||
| (await pathExists(candidate.manifestPath)) ? candidate : null, | ||
| ), | ||
| ); | ||
|
|
||
| return existingCandidates.filter( | ||
| (candidate): candidate is DiscoveredPluginRoot => candidate !== null, | ||
| ); | ||
| } | ||
|
|
||
| export async function discoverPluginRoots(cwd: string): Promise<DiscoveredPluginRoot[]> { | ||
| const rootCandidates = await Promise.all( | ||
| normalizePluginRoots(cwd).map((rootPath) => discoverRootCandidates(rootPath)), | ||
| ); | ||
|
|
||
| const flatCandidates = rootCandidates.flat(); | ||
| const existingCandidates = await Promise.all( | ||
| flatCandidates.map(async (candidate) => | ||
| (await pathExists(candidate.manifestPath)) ? candidate : null, | ||
| ), | ||
| ); | ||
|
|
||
| return existingCandidates.filter( | ||
| (candidate): candidate is DiscoveredPluginRoot => candidate !== null, | ||
| ); | ||
| } | ||
|
|
||
| export async function loadPluginManifest( | ||
| root: DiscoveredPluginRoot, | ||
| ): Promise<DiscoveredPluginManifest> { | ||
| const rawManifest = await fs | ||
| .readFile(root.manifestPath, "utf8") | ||
| .then((contents) => JSON.parse(contents) as RawPluginManifest) | ||
| .catch(() => ({}) as RawPluginManifest); | ||
|
|
||
| const fallbackId = path.basename(root.rootDir); | ||
| const id = trimNonEmpty(rawManifest.id) ?? fallbackId; | ||
| const name = trimNonEmpty(rawManifest.name) ?? id; | ||
| const version = trimNonEmpty(rawManifest.version) ?? "0.0.0"; | ||
| const hostApiVersion = trimNonEmpty(rawManifest.hostApiVersion) ?? "unknown"; | ||
| const enabled = rawManifest.enabled !== false; | ||
| const serverEntry = trimNonEmpty(rawManifest.serverEntry) ?? "dist/server.js"; | ||
| const webEntry = trimNonEmpty(rawManifest.webEntry) ?? "dist/web.js"; | ||
| const serverEntryPath = (await pathExists(path.resolve(root.rootDir, serverEntry))) | ||
| ? path.resolve(root.rootDir, serverEntry) | ||
| : null; | ||
| const webEntryPath = (await pathExists(path.resolve(root.rootDir, webEntry))) | ||
| ? path.resolve(root.rootDir, webEntry) | ||
| : null; | ||
|
|
||
| let error: string | null = null; | ||
| if (!trimNonEmpty(rawManifest.id)) { | ||
| error = "Plugin manifest is missing a valid 'id'."; | ||
| } else if (!trimNonEmpty(rawManifest.name)) { | ||
| error = "Plugin manifest is missing a valid 'name'."; | ||
| } else if (!trimNonEmpty(rawManifest.version)) { | ||
| error = "Plugin manifest is missing a valid 'version'."; | ||
| } else if (!trimNonEmpty(rawManifest.hostApiVersion)) { | ||
| error = "Plugin manifest is missing a valid 'hostApiVersion'."; | ||
| } | ||
|
|
||
| return { | ||
| id, | ||
| name, | ||
| version, | ||
| hostApiVersion, | ||
| enabled, | ||
| compatible: hostApiVersion === "1" && error === null, | ||
| rootDir: root.rootDir, | ||
| manifestPath: root.manifestPath, | ||
| serverEntryPath, | ||
| webEntryPath, | ||
| error, | ||
| }; | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡 Medium
plugins/discovery.ts:45Line 50 resolves relative paths from the environment variable against
process.cwd()rather than thecwdparameter. When a caller passes acwddifferent fromprocess.cwd(), relative paths in the environment variable resolve inconsistently withlocalRootwhich correctly usespath.resolve(cwd, DEFAULT_LOCAL_PLUGINS_DIR). Consider changingpath.resolve(value)topath.resolve(cwd, value)so all paths resolve relative to the same base.🚀 Reply "fix it for me" or copy this AI Prompt for your agent: