feat(pi): materialize effective plugin worker tools
This commit is contained in:
@@ -1,16 +1,18 @@
|
||||
import { mkdir, rename, stat } from 'node:fs/promises';
|
||||
import path from 'node:path';
|
||||
import {
|
||||
BUNDLED_CODING_SKILL_IDS,
|
||||
type BundledCodingSkillId,
|
||||
} from '../../../shared/coding-skills';
|
||||
import { atomicWriteJson, atomicWriteText } from '../../coding-projects/atomic-json';
|
||||
import { validateSessionKey } from '../../coding-projects/conversation-store';
|
||||
import { resolveBundledCodingPluginRootPaths } from '../../coding-plugins/manifest';
|
||||
import type { PiProviderSelection } from './provider-config';
|
||||
import type { PiManagedInputRevision } from './managed-input-revision';
|
||||
|
||||
const MANAGED_SEGMENT_PATTERN = /^[A-Za-z0-9][A-Za-z0-9_-]{0,127}$/;
|
||||
|
||||
export interface PiSkillEntry {
|
||||
id: string;
|
||||
entryPath: string;
|
||||
}
|
||||
|
||||
export interface BundledCodingSkillsPathInput {
|
||||
isPackaged: boolean;
|
||||
resourcesPath: string;
|
||||
@@ -33,7 +35,8 @@ export interface MaterializePiAgentResourcesOptions {
|
||||
projectId: string;
|
||||
agentId: string;
|
||||
prompt: string;
|
||||
skillIds: readonly string[];
|
||||
skillEntries: readonly PiSkillEntry[];
|
||||
catalogRevision: number;
|
||||
bundledSkillsDir: string;
|
||||
revision: PiManagedInputRevision;
|
||||
}
|
||||
@@ -43,7 +46,9 @@ export interface PiAgentResourceManifest {
|
||||
projectId: string;
|
||||
agentId: string;
|
||||
promptFile: string;
|
||||
skillIds: BundledCodingSkillId[];
|
||||
skillIds: string[];
|
||||
skillEntries: PiSkillEntry[];
|
||||
catalogRevision: number;
|
||||
revision: PiManagedInputRevision;
|
||||
}
|
||||
|
||||
@@ -52,13 +57,17 @@ export interface PiAgentResourceSnapshot {
|
||||
projectSessionsDir: string;
|
||||
promptPath: string;
|
||||
manifestPath: string;
|
||||
skillIds: BundledCodingSkillId[];
|
||||
skillIds: string[];
|
||||
skillEntries: PiSkillEntry[];
|
||||
skillPaths: string[];
|
||||
catalogRevision: number;
|
||||
revision: PiManagedInputRevision;
|
||||
summary: {
|
||||
projectId: string;
|
||||
agentId: string;
|
||||
skillIds: BundledCodingSkillId[];
|
||||
skillIds: string[];
|
||||
skillEntries: PiSkillEntry[];
|
||||
catalogRevision: number;
|
||||
revision: PiManagedInputRevision;
|
||||
};
|
||||
}
|
||||
@@ -130,33 +139,85 @@ export async function archivePiConversationSession(input: {
|
||||
}
|
||||
}
|
||||
|
||||
function normalizeSkillIds(skillIds: readonly string[]): BundledCodingSkillId[] {
|
||||
const result: BundledCodingSkillId[] = [];
|
||||
function normalizeSkillEntries(skillEntries: readonly PiSkillEntry[]): PiSkillEntry[] {
|
||||
const result: PiSkillEntry[] = [];
|
||||
const seen = new Set<string>();
|
||||
for (const rawSkillId of skillIds) {
|
||||
const skillId = rawSkillId.trim();
|
||||
if (!BUNDLED_CODING_SKILL_IDS.includes(skillId as BundledCodingSkillId)) {
|
||||
throw new Error(`Unknown bundled coding skill: ${skillId || '(empty)'}`);
|
||||
for (const rawEntry of skillEntries) {
|
||||
if (!rawEntry || typeof rawEntry.id !== 'string' || typeof rawEntry.entryPath !== 'string') {
|
||||
throw new Error('Effective coding Skill entry is invalid');
|
||||
}
|
||||
const skillId = rawEntry.id.trim();
|
||||
const entryPath = rawEntry.entryPath.trim().replaceAll('\\', '/');
|
||||
if (!skillId || !entryPath) throw new Error('Effective coding Skill entry is invalid');
|
||||
if (entryPath.startsWith('/') || /^[A-Za-z]:\//u.test(entryPath)
|
||||
|| /^[A-Za-z][A-Za-z0-9+.-]*:/u.test(entryPath)) {
|
||||
throw new Error(`Effective coding Skill entry path must be relative: ${entryPath}`);
|
||||
}
|
||||
const relative = path.posix.normalize(entryPath);
|
||||
if (!relative || relative === '.' || relative === '..' || relative.startsWith('../')) {
|
||||
throw new Error(`Effective coding Skill entry path escapes its package: ${entryPath}`);
|
||||
}
|
||||
if (seen.has(skillId)) continue;
|
||||
seen.add(skillId);
|
||||
result.push(skillId as BundledCodingSkillId);
|
||||
result.push({ id: skillId, entryPath: relative });
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
function catalogRevision(value: number): number {
|
||||
if (!Number.isSafeInteger(value) || value < 0) {
|
||||
throw new Error('Catalog revision must be a non-negative safe integer');
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
function pathWithin(root: string, entryPath: string): string | null {
|
||||
const resolvedRoot = path.resolve(root);
|
||||
const resolved = path.resolve(resolvedRoot, ...entryPath.split('/'));
|
||||
const relative = path.relative(resolvedRoot, resolved);
|
||||
if (!relative || relative === '..' || relative.startsWith(`..${path.sep}`) || path.isAbsolute(relative)) {
|
||||
return null;
|
||||
}
|
||||
return resolved;
|
||||
}
|
||||
|
||||
async function resolveSkillEntryPath(
|
||||
bundledSkillsDir: string,
|
||||
entry: PiSkillEntry,
|
||||
): Promise<string> {
|
||||
const roots = [
|
||||
path.resolve(bundledSkillsDir),
|
||||
...resolveBundledCodingPluginRootPaths(path.join(
|
||||
path.dirname(path.resolve(bundledSkillsDir)),
|
||||
'coding-plugins',
|
||||
)),
|
||||
];
|
||||
for (const root of roots) {
|
||||
const candidate = pathWithin(root, entry.entryPath);
|
||||
if (!candidate) continue;
|
||||
try {
|
||||
const metadata = await stat(candidate);
|
||||
if (metadata.isFile()) return candidate;
|
||||
} catch (error) {
|
||||
if ((error as NodeJS.ErrnoException).code !== 'ENOENT') throw error;
|
||||
}
|
||||
}
|
||||
throw new Error(`Bundled coding Skill entry is not a file: ${entry.entryPath}`);
|
||||
}
|
||||
|
||||
export async function resolveExplicitCodingSkillPaths(
|
||||
bundledSkillsDir: string,
|
||||
skillIds: readonly string[],
|
||||
): Promise<{ skillIds: BundledCodingSkillId[]; skillPaths: string[] }> {
|
||||
const normalizedSkillIds = normalizeSkillIds(skillIds);
|
||||
const root = path.resolve(bundledSkillsDir);
|
||||
const skillPaths = normalizedSkillIds.map((skillId) => path.join(root, skillId, 'SKILL.md'));
|
||||
await Promise.all(skillPaths.map(async (skillPath) => {
|
||||
const metadata = await stat(skillPath);
|
||||
if (!metadata.isFile()) throw new Error(`Bundled coding skill entry is not a file: ${skillPath}`);
|
||||
}));
|
||||
return { skillIds: normalizedSkillIds, skillPaths };
|
||||
skillEntries: readonly PiSkillEntry[],
|
||||
): Promise<{ skillIds: string[]; skillEntries: PiSkillEntry[]; skillPaths: string[] }> {
|
||||
const normalizedEntries = normalizeSkillEntries(skillEntries);
|
||||
const skillPaths = await Promise.all(normalizedEntries.map((entry) => (
|
||||
resolveSkillEntryPath(bundledSkillsDir, entry)
|
||||
)));
|
||||
return {
|
||||
skillIds: normalizedEntries.map(({ id }) => id),
|
||||
skillEntries: normalizedEntries,
|
||||
skillPaths,
|
||||
};
|
||||
}
|
||||
|
||||
export async function materializePiAgentResources(
|
||||
@@ -171,10 +232,11 @@ export async function materializePiAgentResources(
|
||||
mkdir(projectSessionsDir, { recursive: true }),
|
||||
mkdir(projectPromptsDir, { recursive: true }),
|
||||
]);
|
||||
const { skillIds, skillPaths } = await resolveExplicitCodingSkillPaths(
|
||||
const { skillIds, skillEntries, skillPaths } = await resolveExplicitCodingSkillPaths(
|
||||
options.bundledSkillsDir,
|
||||
options.skillIds,
|
||||
options.skillEntries,
|
||||
);
|
||||
const resolvedCatalogRevision = catalogRevision(options.catalogRevision);
|
||||
const promptPath = path.join(projectPromptsDir, `${agentId}.md`);
|
||||
const manifestPath = path.join(projectPromptsDir, `${agentId}.manifest.json`);
|
||||
const manifest: PiAgentResourceManifest = {
|
||||
@@ -183,6 +245,8 @@ export async function materializePiAgentResources(
|
||||
agentId,
|
||||
promptFile: path.basename(promptPath),
|
||||
skillIds: [...skillIds],
|
||||
skillEntries: structuredClone(skillEntries),
|
||||
catalogRevision: resolvedCatalogRevision,
|
||||
revision: { ...options.revision },
|
||||
};
|
||||
await atomicWriteText(promptPath, options.prompt);
|
||||
@@ -193,12 +257,16 @@ export async function materializePiAgentResources(
|
||||
promptPath,
|
||||
manifestPath,
|
||||
skillIds: [...skillIds],
|
||||
skillEntries: structuredClone(skillEntries),
|
||||
skillPaths,
|
||||
catalogRevision: resolvedCatalogRevision,
|
||||
revision: { ...options.revision },
|
||||
summary: {
|
||||
projectId,
|
||||
agentId,
|
||||
skillIds: [...skillIds],
|
||||
skillEntries: structuredClone(skillEntries),
|
||||
catalogRevision: resolvedCatalogRevision,
|
||||
revision: { ...options.revision },
|
||||
},
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user