mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-25 00:39:25 +00:00
Merge pull request #2 from shumaiOne/fix/skill-env-host-fallback
fix(skill): fallback to host environment only if skill config value is empty
This commit is contained in:
@@ -5,14 +5,14 @@ import { s3Service } from '@/services/s3/s3'
|
||||
import { SandboxManager } from '@anthropic-ai/sandbox-runtime'
|
||||
import { getModel, type ImageContent, type TextContent } from '@mariozechner/pi-ai'
|
||||
import {
|
||||
AuthStorage,
|
||||
createAgentSession,
|
||||
DefaultResourceLoader,
|
||||
defineTool,
|
||||
ModelRegistry,
|
||||
SessionManager,
|
||||
SettingsManager,
|
||||
type ToolDefinition,
|
||||
AuthStorage,
|
||||
createAgentSession,
|
||||
DefaultResourceLoader,
|
||||
defineTool,
|
||||
ModelRegistry,
|
||||
SessionManager,
|
||||
SettingsManager,
|
||||
type ToolDefinition,
|
||||
} from '@mariozechner/pi-coding-agent'
|
||||
import { Type, type TSchema } from '@sinclair/typebox'
|
||||
import * as fs from 'fs'
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
|
||||
import { prisma } from '@/db'
|
||||
import { setupTestDbHooks } from '@/db-test-hooks'
|
||||
import { createReadSkillTool } from './read-skill'
|
||||
@@ -131,4 +131,176 @@ describe('readSkillTool', () => {
|
||||
expect(s3Service.getObject).toHaveBeenCalled()
|
||||
expect(fs.writeFileSync).toHaveBeenCalledWith(expect.stringContaining('.hash'), 'new-hash')
|
||||
})
|
||||
|
||||
describe('environment variables', () => {
|
||||
let originalEnv: NodeJS.ProcessEnv
|
||||
|
||||
beforeEach(() => {
|
||||
originalEnv = { ...process.env }
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
process.env = originalEnv
|
||||
})
|
||||
|
||||
it('should use user-configured value if it is non-empty', async () => {
|
||||
const team = await prisma.team.create({ data: { name: 'Test Team' } })
|
||||
const skill = await prisma.skill.create({
|
||||
data: {
|
||||
name: 'Skill Env Test 1',
|
||||
assetId: 'asset1',
|
||||
hash: 'hash1',
|
||||
teamId: team.id,
|
||||
config: {
|
||||
environmentVariables: [{ name: 'CONFIGURED_VAR', default: 'user-val' }],
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
delete process.env.CONFIGURED_VAR
|
||||
|
||||
vi.spyOn(fs, 'existsSync').mockReturnValue(true)
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- mocking node fs readFileSync which has complex overloaded signatures
|
||||
vi.spyOn(fs, 'readFileSync').mockImplementation((path: any) => {
|
||||
if (path.toString().endsWith('.hash')) return 'hash1'
|
||||
if (path.toString().endsWith('SKILL.md')) return '# Content'
|
||||
return ''
|
||||
})
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const readSkillTool = createReadSkillTool(mockSessionManager as any)
|
||||
await readSkillTool.execute(
|
||||
'1',
|
||||
{ skillId: skill.id },
|
||||
undefined,
|
||||
undefined,
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
{} as unknown as any,
|
||||
)
|
||||
|
||||
expect(mockSessionManager.addSkillEnvs).toHaveBeenCalledWith({
|
||||
CONFIGURED_VAR: 'user-val',
|
||||
})
|
||||
})
|
||||
|
||||
it('should favor user-configured value even if host environment variable is also defined', async () => {
|
||||
const team = await prisma.team.create({ data: { name: 'Test Team' } })
|
||||
const skill = await prisma.skill.create({
|
||||
data: {
|
||||
name: 'Skill Env Test 2',
|
||||
assetId: 'asset1',
|
||||
hash: 'hash2',
|
||||
teamId: team.id,
|
||||
config: {
|
||||
environmentVariables: [{ name: 'OVERRIDDEN_VAR', default: 'user-val' }],
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
process.env.OVERRIDDEN_VAR = 'host-val'
|
||||
|
||||
vi.spyOn(fs, 'existsSync').mockReturnValue(true)
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- mocking node fs readFileSync which has complex overloaded signatures
|
||||
vi.spyOn(fs, 'readFileSync').mockImplementation((path: any) => {
|
||||
if (path.toString().endsWith('.hash')) return 'hash2'
|
||||
if (path.toString().endsWith('SKILL.md')) return '# Content'
|
||||
return ''
|
||||
})
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const readSkillTool = createReadSkillTool(mockSessionManager as any)
|
||||
await readSkillTool.execute(
|
||||
'1',
|
||||
{ skillId: skill.id },
|
||||
undefined,
|
||||
undefined,
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
{} as unknown as any,
|
||||
)
|
||||
|
||||
expect(mockSessionManager.addSkillEnvs).toHaveBeenCalledWith({
|
||||
OVERRIDDEN_VAR: 'user-val',
|
||||
})
|
||||
})
|
||||
|
||||
it('should fall back to host environment variable if user-configured value is empty string', async () => {
|
||||
const team = await prisma.team.create({ data: { name: 'Test Team' } })
|
||||
const skill = await prisma.skill.create({
|
||||
data: {
|
||||
name: 'Skill Env Test 3',
|
||||
assetId: 'asset1',
|
||||
hash: 'hash3',
|
||||
teamId: team.id,
|
||||
config: {
|
||||
environmentVariables: [{ name: 'FALLBACK_VAR', default: '' }],
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
process.env.FALLBACK_VAR = 'host-val'
|
||||
|
||||
vi.spyOn(fs, 'existsSync').mockReturnValue(true)
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- mocking node fs readFileSync which has complex overloaded signatures
|
||||
vi.spyOn(fs, 'readFileSync').mockImplementation((path: any) => {
|
||||
if (path.toString().endsWith('.hash')) return 'hash3'
|
||||
if (path.toString().endsWith('SKILL.md')) return '# Content'
|
||||
return ''
|
||||
})
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const readSkillTool = createReadSkillTool(mockSessionManager as any)
|
||||
await readSkillTool.execute(
|
||||
'1',
|
||||
{ skillId: skill.id },
|
||||
undefined,
|
||||
undefined,
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
{} as unknown as any,
|
||||
)
|
||||
|
||||
expect(mockSessionManager.addSkillEnvs).toHaveBeenCalledWith({
|
||||
FALLBACK_VAR: 'host-val',
|
||||
})
|
||||
})
|
||||
|
||||
it('should fall back to host environment variable if user-configured value is undefined', async () => {
|
||||
const team = await prisma.team.create({ data: { name: 'Test Team' } })
|
||||
const skill = await prisma.skill.create({
|
||||
data: {
|
||||
name: 'Skill Env Test 4',
|
||||
assetId: 'asset1',
|
||||
hash: 'hash4',
|
||||
teamId: team.id,
|
||||
config: {
|
||||
environmentVariables: [{ name: 'FALLBACK_VAR_UNDEF' }],
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
process.env.FALLBACK_VAR_UNDEF = 'host-val2'
|
||||
|
||||
vi.spyOn(fs, 'existsSync').mockReturnValue(true)
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- mocking node fs readFileSync which has complex overloaded signatures
|
||||
vi.spyOn(fs, 'readFileSync').mockImplementation((path: any) => {
|
||||
if (path.toString().endsWith('.hash')) return 'hash4'
|
||||
if (path.toString().endsWith('SKILL.md')) return '# Content'
|
||||
return ''
|
||||
})
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const readSkillTool = createReadSkillTool(mockSessionManager as any)
|
||||
await readSkillTool.execute(
|
||||
'1',
|
||||
{ skillId: skill.id },
|
||||
undefined,
|
||||
undefined,
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
{} as unknown as any,
|
||||
)
|
||||
|
||||
expect(mockSessionManager.addSkillEnvs).toHaveBeenCalledWith({
|
||||
FALLBACK_VAR_UNDEF: 'host-val2',
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -35,7 +35,10 @@ export const createReadSkillTool = (sessionManager: DatabaseSessionManager) =>
|
||||
if (config?.environmentVariables && Array.isArray(config.environmentVariables)) {
|
||||
const envs: Record<string, string> = {}
|
||||
for (const envVar of config.environmentVariables) {
|
||||
const value = process.env[envVar.name] || envVar.default
|
||||
const value =
|
||||
envVar.default !== undefined && envVar.default !== null && envVar.default !== ''
|
||||
? envVar.default
|
||||
: process.env[envVar.name]
|
||||
if (value !== undefined) {
|
||||
envs[envVar.name] = value
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user