diff --git a/packages/api/src/agents/skillFiles.spec.ts b/packages/api/src/agents/skillFiles.spec.ts index 5c6dd875c4..e38560b009 100644 --- a/packages/api/src/agents/skillFiles.spec.ts +++ b/packages/api/src/agents/skillFiles.spec.ts @@ -14,6 +14,7 @@ jest.mock('./run', () => ({ import { Readable } from 'stream'; import { Types } from 'mongoose'; +import { logger } from '@librechat/data-schemas'; import { createCodeApiUploadRegistry } from '~/utils'; import { primeInvokedSkills, primeInvokedSkillsForProfiles, primeSkillFiles } from './skillFiles'; import type { @@ -933,6 +934,51 @@ describe('primeSkillFiles — resource identity propagation', () => { jest.clearAllMocks(); }); + it('names the file whose stream failed and uploads the rest of the bundle', async () => { + const missingPathError = Object.assign(new Error('ENOENT: /srv/skills/secret/missing.dotx'), { + code: 'ENOENT', + }); + const getDownloadStream = jest.fn(async (_req: unknown, filepath: string) => { + if (filepath.endsWith('missing.dotx')) { + throw missingPathError; + } + return Readable.from(Buffer.from('style')); + }); + const deps = makeSkillFilesDeps({ + skillFiles: [ + { + relativePath: 'masters/missing.dotx', + filename: 'missing.dotx', + filepath: '/srv/skills/secret/missing.dotx', + source: 'deployment', + bytes: 16, + }, + { + relativePath: 'references/style.md', + filename: 'style.md', + filepath: '/srv/skills/secret/style.md', + source: 'deployment', + bytes: 5, + }, + ], + getStrategyFunctions: jest.fn().mockReturnValue({ getDownloadStream }), + }); + + const result = await primeSkillFiles(deps); + + const { files } = (deps.batchUploadCodeEnvFiles as jest.Mock).mock.calls[0][0]; + expect(files.map((file: { filename: string }) => file.filename)).toEqual([ + 'skills/brand-guidelines/SKILL.md', + 'skills/brand-guidelines/references/style.md', + ]); + expect(result?.storage_session_id).toBe('session-fresh'); + expect(logger.error).toHaveBeenCalledTimes(1); + expect(logger.error).toHaveBeenCalledWith('[primeSkillFiles] Failed to get stream:', { + relativePath: 'masters/missing.dotx', + type: 'Error', + }); + }); + it('fresh-upload path: emits resource_id=skill._id, kind=skill, version on each file', async () => { const deps = makeSkillFilesDeps({ skillFiles: [ diff --git a/packages/api/src/agents/skillFiles.ts b/packages/api/src/agents/skillFiles.ts index 8c5d0a420b..e81c671aa4 100644 --- a/packages/api/src/agents/skillFiles.ts +++ b/packages/api/src/agents/skillFiles.ts @@ -244,11 +244,14 @@ async function collectSkillUploadFiles( } signal.throwIfAborted(); } - for (const result of streamResults) { + for (const [index, result] of streamResults.entries()) { if (result.status === 'fulfilled' && result.value) { filesToUpload.push(result.value); } else if (result.status === 'rejected') { - logger.error('[primeSkillFiles] Failed to get stream:', getSafeErrorMetadata(result.reason)); + logger.error('[primeSkillFiles] Failed to get stream:', { + relativePath: skillFiles[index].relativePath, + ...getSafeErrorMetadata(result.reason), + }); } } diff --git a/packages/api/src/skills/__tests__/deployment.test.ts b/packages/api/src/skills/__tests__/deployment.test.ts index 10eef4cb00..754268e9ab 100644 --- a/packages/api/src/skills/__tests__/deployment.test.ts +++ b/packages/api/src/skills/__tests__/deployment.test.ts @@ -8,6 +8,7 @@ import type { DeploymentSkillBaseMethods } from '../deployment'; import { DEPLOYMENT_SKILLS_DIR_ENV, createDeploymentSkillMethods, + getDeploymentSkillDownloadStream, getDeploymentSkillIds, initializeDeploymentSkills, loadDeploymentSkillsFromDirectory, @@ -102,6 +103,67 @@ describe('resolveDeploymentSkillDirectory', () => { }); }); +describe('getDeploymentSkillDownloadStream', () => { + async function loadGuide(): Promise { + const root = await makeTempRoot(); + const skillDir = await writeDeploymentSkill(root); + await initializeDeploymentSkills({ projectRoot: root, env: {} }); + return path.join(skillDir, 'references', 'guide.txt'); + } + + it('streams a registered file that still exists', async () => { + const guide = await loadGuide(); + + const stream = await getDeploymentSkillDownloadStream(guide); + + const chunks: Buffer[] = []; + for await (const chunk of stream) { + chunks.push(Buffer.from(chunk)); + } + expect(Buffer.concat(chunks).toString('utf8')).toBe('reference notes'); + }); + + it('rejects when a registered file was deleted after the registry loaded', async () => { + const guide = await loadGuide(); + await fs.promises.rm(guide); + + await expect(getDeploymentSkillDownloadStream(guide)).rejects.toMatchObject({ + code: 'ENOENT', + }); + }); + + it('rejects a path the registry does not know', async () => { + const guide = await loadGuide(); + + await expect(getDeploymentSkillDownloadStream(`${guide}.unregistered`)).rejects.toThrow( + 'Deployment skill file is not registered', + ); + }); + + it('keeps serving a file deleted after its stream was opened', async () => { + const guide = await loadGuide(); + + const stream = await getDeploymentSkillDownloadStream(guide); + await fs.promises.rm(guide); + + const chunks: Buffer[] = []; + for await (const chunk of stream) { + chunks.push(Buffer.from(chunk)); + } + expect(Buffer.concat(chunks).toString('utf8')).toBe('reference notes'); + }); + + it('closes the file when the stream is destroyed before it is read', async () => { + const guide = await loadGuide(); + + const stream = (await getDeploymentSkillDownloadStream(guide)) as fs.ReadStream; + const closed = new Promise((resolve) => stream.once('close', () => resolve())); + stream.destroy(); + + await expect(closed).resolves.toBeUndefined(); + }); +}); + describe('loadDeploymentSkillsFromDirectory', () => { it('treats a missing default directory as an empty deployment catalog', async () => { const root = await makeTempRoot(); diff --git a/packages/api/src/skills/deployment.ts b/packages/api/src/skills/deployment.ts index db5a121205..d43f0e77fd 100644 --- a/packages/api/src/skills/deployment.ts +++ b/packages/api/src/skills/deployment.ts @@ -392,7 +392,10 @@ export async function getDeploymentSkillDownloadStream( if (!registry.hasFilePath(filepath)) { throw new Error('Deployment skill file is not registered'); } - return fs.createReadStream(filepath); + /* The registry outlives the files it lists, and `fs.createReadStream` only reports a + * missing path as a stream error once a consumer is reading. Opening first rejects here. */ + const handle = await fs.promises.open(filepath, 'r'); + return handle.createReadStream(); } export function resolveDeploymentSkillDirectory(