fix: make skill scripts work when installed via npm (#460)

Co-authored-by: Letta <noreply@letta.com>
This commit is contained in:
Charles Packer
2026-01-04 17:38:34 -08:00
committed by GitHub
parent 8f32e1f7f6
commit 56b40465dd
7 changed files with 318 additions and 66 deletions

View File

@@ -26,6 +26,24 @@ function getRipgrepBinDir(): string | undefined {
}
}
/**
* Get the node_modules directory containing this package's dependencies.
* Skill scripts use createRequire with NODE_PATH to resolve dependencies.
*/
function getPackageNodeModulesDir(): string | undefined {
try {
const __filename = fileURLToPath(import.meta.url);
const require = createRequire(__filename);
// Find where letta-client is installed
const clientPath = require.resolve("@letta-ai/letta-client");
// Extract node_modules path: /a/b/node_modules/@letta-ai/letta-client/... -> /a/b/node_modules
const match = clientPath.match(/^(.+[/\\]node_modules)[/\\]/);
return match ? match[1] : undefined;
} catch {
return undefined;
}
}
/**
* Get enhanced environment variables for shell execution.
* Includes bundled tools (like ripgrep) in PATH and Letta context for skill scripts.
@@ -59,5 +77,15 @@ export function getShellEnv(): NodeJS.ProcessEnv {
}
}
// Add NODE_PATH for skill scripts to resolve @letta-ai/letta-client
// ES modules don't respect NODE_PATH, but createRequire does
const nodeModulesDir = getPackageNodeModulesDir();
if (nodeModulesDir) {
const currentNodePath = env.NODE_PATH || "";
env.NODE_PATH = currentNodePath
? `${nodeModulesDir}${path.delimiter}${currentNodePath}`
: nodeModulesDir;
}
return env;
}