sync: update from private repo (6fcb0d0)
This commit is contained in:
+24
-12
@@ -7,10 +7,20 @@ import type { ToolContext, ToolResult } from './core.js';
|
||||
const BUILTIN_PIECES_DIR = resolve(process.cwd(), 'pieces');
|
||||
const VALID_NAME = /^[a-z0-9-]+$/;
|
||||
|
||||
function findPiecePath(name: string, customDir: string | undefined): string | null {
|
||||
if (customDir) {
|
||||
const customPath = join(customDir, `${name}.yaml`);
|
||||
if (existsSync(customPath)) return customPath;
|
||||
/** Normalise `string | string[] | undefined` → `string[]` (may be empty). */
|
||||
function toCustomDirs(customDir: string | string[] | undefined): string[] {
|
||||
if (!customDir) return [];
|
||||
return Array.isArray(customDir) ? customDir : [customDir];
|
||||
}
|
||||
|
||||
/**
|
||||
* Search all custom dirs in order, then fall back to built-in.
|
||||
* Returns the first path that exists, or null.
|
||||
*/
|
||||
function findPiecePath(name: string, customDir: string | string[] | undefined): string | null {
|
||||
for (const d of toCustomDirs(customDir)) {
|
||||
const p = join(d, `${name}.yaml`);
|
||||
if (existsSync(p)) return p;
|
||||
}
|
||||
const builtinPath = join(BUILTIN_PIECES_DIR, `${name}.yaml`);
|
||||
if (existsSync(builtinPath)) return builtinPath;
|
||||
@@ -19,17 +29,16 @@ function findPiecePath(name: string, customDir: string | undefined): string | nu
|
||||
|
||||
/**
|
||||
* A piece is "built-in" when it lives only under the bundled BUILTIN_PIECES_DIR
|
||||
* (no override in customDir). Built-ins are git-tracked and shipped with the
|
||||
* (no override in any custom dir). Built-ins are git-tracked and shipped with the
|
||||
* app — letting the LLM rewrite them in place corrupts the install (a real
|
||||
* incident: the agent silently replaced game-tweet-generator with a version
|
||||
* missing max_movements, making every subsequent run abort instantly). The
|
||||
* LLM should use CreatePiece with a new name to derive a customized variant
|
||||
* instead.
|
||||
*/
|
||||
function isBuiltinOnly(name: string, customDir: string | undefined): boolean {
|
||||
if (customDir) {
|
||||
const customPath = join(customDir, `${name}.yaml`);
|
||||
if (existsSync(customPath)) return false;
|
||||
function isBuiltinOnly(name: string, customDir: string | string[] | undefined): boolean {
|
||||
for (const d of toCustomDirs(customDir)) {
|
||||
if (existsSync(join(d, `${name}.yaml`))) return false;
|
||||
}
|
||||
const builtinPath = join(BUILTIN_PIECES_DIR, `${name}.yaml`);
|
||||
return existsSync(builtinPath);
|
||||
@@ -156,7 +165,9 @@ function executeListPieces(ctx: ToolContext): ToolResult {
|
||||
const seen = new Set<string>();
|
||||
const pieces: Array<{ name: string; description: string; keywords: string[]; custom: boolean }> = [];
|
||||
const dirs: Array<{ dir: string; custom: boolean }> = [];
|
||||
if (ctx.customPiecesDir && existsSync(ctx.customPiecesDir)) dirs.push({ dir: ctx.customPiecesDir, custom: true });
|
||||
for (const d of toCustomDirs(ctx.customPiecesDir)) {
|
||||
if (existsSync(d)) dirs.push({ dir: d, custom: true });
|
||||
}
|
||||
dirs.push({ dir: BUILTIN_PIECES_DIR, custom: false });
|
||||
|
||||
for (const { dir, custom } of dirs) {
|
||||
@@ -231,8 +242,9 @@ function executeCreatePiece(input: Record<string, unknown>, ctx: ToolContext): T
|
||||
return { output: `Piece "${piece.name}" already exists. Use UpdatePiece to modify it.`, isError: true };
|
||||
}
|
||||
|
||||
// カスタムディレクトリがあればそこに、なければ builtin に書き込み
|
||||
const targetDir = ctx.customPiecesDir ?? BUILTIN_PIECES_DIR;
|
||||
// Write to the FIRST custom dir (per-user dir), or fall back to builtin if no custom dirs.
|
||||
const customDirs = toCustomDirs(ctx.customPiecesDir);
|
||||
const targetDir = customDirs[0] ?? BUILTIN_PIECES_DIR;
|
||||
mkdirSync(targetDir, { recursive: true });
|
||||
const filePath = join(targetDir, `${piece.name}.yaml`);
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user