feat(agent): strip and restore skill content and preserve envs on session rebuild

This commit is contained in:
yiling
2026-06-01 14:34:23 +08:00
parent 29530771af
commit df57c9d2b9
5 changed files with 270 additions and 76 deletions
+24
View File
@@ -5,6 +5,7 @@ import { getModel } from '@earendil-works/pi-ai'
import { Type, type TSchema } from '@sinclair/typebox'
import * as fs from 'fs'
import * as path from 'path'
import { agentService } from '@/services/agent/agent'
import { DatabaseSessionStorage } from './database-session-storage'
import { analyzeAssetMediaTool } from './tools/analyze-asset-media'
import { createCreateFileTool } from './tools/create-file'
@@ -129,6 +130,29 @@ export async function createAgentSession(params: CreateAgentSessionParams) {
Object.assign(skillEnvs, envs)
}
// Restore env variables from previously loaded skills in this session
if (sessionId) {
try {
const entries = await storage.getEntries()
for (const entry of entries) {
if (
entry.type === 'message' &&
entry.message.role === 'toolResult' &&
entry.message.toolName === 'read_skill' &&
!entry.message.isError
) {
const details = entry.message.details as { skillId?: string } | undefined
if (details?.skillId) {
const envs = await agentService.getSkillEnvs(details.skillId)
onEnvsAdded(envs)
}
}
}
} catch (err) {
console.error('Failed to restore skill environment variables:', err)
}
}
const sandboxedBash = createSandboxedBashTool(process.cwd(), skillEnvs)
const readSkill = createReadSkillTool(onEnvsAdded)