From df57c9d2b97eb0ff2b53a04dce40225cce0f80b7 Mon Sep 17 00:00:00 2001 From: yiling Date: Mon, 1 Jun 2026 14:34:23 +0800 Subject: [PATCH] feat(agent): strip and restore skill content and preserve envs on session rebuild --- src/agent/database-session-storage.test.ts | 105 +++++++++++++++++++++ src/agent/database-session-storage.ts | 52 +++++++++- src/agent/index.ts | 24 +++++ src/agent/tools/read-skill.ts | 80 ++-------------- src/services/agent/agent.ts | 85 +++++++++++++++++ 5 files changed, 270 insertions(+), 76 deletions(-) diff --git a/src/agent/database-session-storage.test.ts b/src/agent/database-session-storage.test.ts index 6cc0e6a2..ccf0afe7 100644 --- a/src/agent/database-session-storage.test.ts +++ b/src/agent/database-session-storage.test.ts @@ -4,6 +4,9 @@ import { s3Service } from '@/services/s3/s3' import { type SessionTreeEntry } from '@earendil-works/pi-agent-core' import { describe, expect, it, vi } from 'vitest' import { DatabaseSessionStorage } from './database-session-storage' +import { agentService } from '@/services/agent/agent' +import { createAgentSession } from './index' +import * as sandboxedBashModule from './tools/sandboxed-bash' describe('DatabaseSessionStorage', () => { setupTestDbHooks() @@ -230,6 +233,108 @@ describe('DatabaseSessionStorage', () => { ) }) + it('should strip and reinject skill content', async () => { + const { agent } = await setupTestData() + const storage = await DatabaseSessionStorage.create({ agentId: agent.id }) + + const getSkillContentSpy = vi + .spyOn(agentService, 'getSkillContent') + .mockResolvedValue('# Mocked Skill Content') + + const entry: SessionTreeEntry = { + type: 'message', + id: 'msg-skill', + parentId: null, + timestamp: new Date().toISOString(), + message: { + role: 'toolResult', + toolCallId: 'call-2', + toolName: 'read_skill', + isError: false, + timestamp: Date.now(), + content: [{ type: 'text', text: 'original-skill-content' }], + details: { + skillId: 'some-skill-id', + }, + }, + } + + await storage.appendEntry(entry) + + // Verify saved entry is stripped + const record = await prisma.agentSessionEntry.findUnique({ where: { id: 'msg-skill' } }) + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const savedEntry = record?.entry as any + expect(savedEntry.message.content[0].text).toBe('__SKILL_CONTENT__') + + // Verify retrieval reinjects data + const retrieved = await storage.getEntry('msg-skill') + expect(getSkillContentSpy).toHaveBeenCalledWith('some-skill-id') + // eslint-disable-next-line @typescript-eslint/no-explicit-any + expect((retrieved as any).message.content[0].text).toBe('# Mocked Skill Content') + }) + + it('should restore environment variables when resuming session', async () => { + const { agent, user } = await setupTestData() + + // 1. Create a session first to get a sessionId + const initialStorage = await DatabaseSessionStorage.create({ agentId: agent.id }) + const sessionId = initialStorage.sessionId + + // 2. Append a successful read_skill entry + const entry: SessionTreeEntry = { + type: 'message', + id: 'msg-skill-env', + parentId: null, + timestamp: new Date().toISOString(), + message: { + role: 'toolResult', + toolCallId: 'call-env-1', + toolName: 'read_skill', + isError: false, + timestamp: Date.now(), + content: [{ type: 'text', text: 'skill content' }], + details: { + skillId: 'env-skill-id', + }, + }, + } + await initialStorage.appendEntry(entry) + + // 3. Mock getSkillEnvs to return environment variables + const getSkillEnvsSpy = vi.spyOn(agentService, 'getSkillEnvs').mockResolvedValue({ + MOCK_ENV_VAR: 'mocked-value', + }) + + // 4. Mock agentService.getSkillContent to avoid actual download throws + vi.spyOn(agentService, 'getSkillContent').mockResolvedValue('# Skill') + + // 5. Spy on createSandboxedBashTool + const createSandboxedBashToolSpy = vi.spyOn(sandboxedBashModule, 'createSandboxedBashTool') + + // 6. Resume the session via createAgentSession + await createAgentSession({ + agentId: agent.id, + providerName: 'test-provider', + modelId: 'test-model', + systemPrompt: 'Test prompt', + teamSkills: [], + allowedDomains: [], + sessionId: sessionId, + userId: user.id, + providers: [], + }) + + // 7. Verify skill environment variables are restored. + expect(getSkillEnvsSpy).toHaveBeenCalledWith('env-skill-id') + expect(createSandboxedBashToolSpy).toHaveBeenCalledWith( + expect.any(String), + expect.objectContaining({ + MOCK_ENV_VAR: 'mocked-value', + }), + ) + }) + it('should retrieve label', async () => { const { agent } = await setupTestData() const storage = await DatabaseSessionStorage.create({ agentId: agent.id }) diff --git a/src/agent/database-session-storage.ts b/src/agent/database-session-storage.ts index 20f62901..57d6e642 100644 --- a/src/agent/database-session-storage.ts +++ b/src/agent/database-session-storage.ts @@ -8,6 +8,7 @@ import { type SessionTreeEntry, type SessionTreeEntryBase, } from '@earendil-works/pi-agent-core' +import { agentService } from '@/services/agent/agent' export interface DatabaseSessionMetadata extends SessionMetadata { agentId: string @@ -54,11 +55,13 @@ export class DatabaseSessionStorage implements SessionStorage { const strippedEntry = structuredClone(entry) - // Strip image data before saving to DB + // Strip image data and skill content before saving to DB if (strippedEntry.type === 'message') { const msg = strippedEntry.message if (msg.role === 'toolResult') { - const details = msg.details as { sourceKeys?: string[] } | undefined + const details = msg.details as { sourceKeys?: string[]; skillId?: string } | undefined + + // Strip S3 image data if (Array.isArray(msg.content) && details?.sourceKeys) { const sourceKeys = details.sourceKeys let keyIdx = 0 @@ -69,6 +72,23 @@ export class DatabaseSessionStorage implements SessionStorage { + if ( + item.type === 'text' && + item.text && + !item.text.startsWith('Skill with ID') && + !item.text.startsWith('Skill downloaded but') && + !item.text.startsWith('Error reading skill') + ) { + item.text = '__SKILL_CONTENT__' + } + }) + } + } } } @@ -91,6 +111,7 @@ export class DatabaseSessionStorage implements SessionStorage for (const entry of entries) { await this.reinjectImageDataAsync(entry) + await this.reinjectSkillContentAsync(entry) } return entries } @@ -158,6 +180,7 @@ export class DatabaseSessionStorage implements SessionStorage r.entry as unknown as SessionTreeEntry) for (const entry of entries) { await this.reinjectImageDataAsync(entry) + await this.reinjectSkillContentAsync(entry) } return entries } @@ -200,6 +224,30 @@ export class DatabaseSessionStorage implements SessionStorage = {} - for (const envVar of config.environmentVariables) { - const value = - envVar.default !== undefined && envVar.default !== null && envVar.default !== '' - ? envVar.default - : process.env[envVar.name] - if (value !== undefined) { - envs[envVar.name] = value - } - } - onEnvsAdded(envs) - } + // Capture environment variables using agentService + const envs = await agentService.getSkillEnvs(params.skillId) + onEnvsAdded(envs) - const skillDir = path.join(process.cwd(), '.pi', 'skills', skill.id) - const hashFile = path.join(skillDir, '.hash') - let needsDownload = true + // Retrieve skill content using agentService + const skillMdContent = await agentService.getSkillContent(params.skillId) - if (fs.existsSync(hashFile)) { - const localHash = fs.readFileSync(hashFile, 'utf8') - if (localHash === skill.hash) { - needsDownload = false - } - } - - if (needsDownload) { - if (fs.existsSync(skillDir)) { - fs.rmSync(skillDir, { recursive: true, force: true }) - } - fs.mkdirSync(skillDir, { recursive: true }) - - const asset = await prisma.asset.findUnique({ - where: { id: skill.assetId }, - include: { storageKey: true }, - }) - - if (!asset || !asset.storageKey?.key) { - throw new Error('Skill asset not found or has no key') - } - - const { buffer: zipBuffer } = await s3Service.getObject( - process.env.S3_BUCKET || 'shumai', - asset.storageKey.key, - ) - - const zip = new AdmZip(zipBuffer) - zip.extractAllTo(skillDir, true) - - fs.writeFileSync(hashFile, skill.hash) - } - - // Read SKILL.md - let skillMdPath = path.join(skillDir, 'SKILL.md') - if (!fs.existsSync(skillMdPath)) { - skillMdPath = path.join(skillDir, 'skill.md') - } - - if (!fs.existsSync(skillMdPath)) { - return { - content: [ - { - type: 'text', - text: `Skill downloaded but SKILL.md not found in ${skillDir}`, - }, - ], - details: { skillId: params.skillId }, - } - } - - const skillMdContent = fs.readFileSync(skillMdPath, 'utf8') return { content: [{ type: 'text', text: skillMdContent }], details: { skillId: params.skillId }, diff --git a/src/services/agent/agent.ts b/src/services/agent/agent.ts index 0004510b..46cd3feb 100644 --- a/src/services/agent/agent.ts +++ b/src/services/agent/agent.ts @@ -6,10 +6,95 @@ import { UpdateAgentParams, } from '@/dtos/agent' import '@/prisma-json-types' +import { s3Service } from '@/services/s3/s3' +import AdmZip from 'adm-zip' +import * as fs from 'fs' +import * as path from 'path' export class AgentService { constructor(private readonly prismaClient: typeof prisma = prisma) {} + async getSkillContent(skillId: string): Promise { + const skill = await this.prismaClient.skill.findUnique({ + where: { id: skillId }, + }) + + if (!skill) { + throw new Error(`Skill with ID ${skillId} not found.`) + } + + const skillDir = path.join(process.cwd(), '.pi', 'skills', skill.id) + const hashFile = path.join(skillDir, '.hash') + let needsDownload = true + + if (fs.existsSync(hashFile)) { + const localHash = fs.readFileSync(hashFile, 'utf8') + if (localHash === skill.hash) { + needsDownload = false + } + } + + if (needsDownload) { + if (fs.existsSync(skillDir)) { + fs.rmSync(skillDir, { recursive: true, force: true }) + } + fs.mkdirSync(skillDir, { recursive: true }) + + const asset = await this.prismaClient.asset.findUnique({ + where: { id: skill.assetId }, + include: { storageKey: true }, + }) + + if (!asset || !asset.storageKey?.key) { + throw new Error('Skill asset not found or has no key') + } + + const { buffer: zipBuffer } = await s3Service.getObject( + process.env.S3_BUCKET || 'shumai', + asset.storageKey.key, + ) + + const zip = new AdmZip(zipBuffer) + zip.extractAllTo(skillDir, true) + + fs.writeFileSync(hashFile, skill.hash) + } + + // Read SKILL.md + let skillMdPath = path.join(skillDir, 'SKILL.md') + if (!fs.existsSync(skillMdPath)) { + skillMdPath = path.join(skillDir, 'skill.md') + } + + if (!fs.existsSync(skillMdPath)) { + throw new Error(`Skill downloaded but SKILL.md not found in ${skillDir}`) + } + + return fs.readFileSync(skillMdPath, 'utf8') + } + + async getSkillEnvs(skillId: string): Promise> { + const skill = await this.prismaClient.skill.findUnique({ + where: { id: skillId }, + }) + if (!skill) return {} + + const config = skill.config as unknown as PrismaJson.SkillConfig + const envs: Record = {} + if (config?.environmentVariables && Array.isArray(config.environmentVariables)) { + for (const envVar of config.environmentVariables) { + const value = + envVar.default !== undefined && envVar.default !== null && envVar.default !== '' + ? envVar.default + : process.env[envVar.name] + if (value !== undefined) { + envs[envVar.name] = value + } + } + } + return envs + } + async createAgent(params: CreateAgentParams) { const { teamId,