mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-12-02 14:04:50 +00:00
Merge pull request #253 from shumaiOne/refactor/replace-deprecated-get-model
refactor(agent): load models directly from database without pi fallback
This commit is contained in:
@@ -5,7 +5,7 @@ import { type SessionTreeEntry } from '@earendil-works/pi-agent-core'
|
|||||||
import { describe, expect, it, vi, afterEach } from 'vitest'
|
import { describe, expect, it, vi, afterEach } from 'vitest'
|
||||||
import { DatabaseSessionStorage } from './database-session-storage'
|
import { DatabaseSessionStorage } from './database-session-storage'
|
||||||
import { agentService } from '@shumai/core/src/agent/agent'
|
import { agentService } from '@shumai/core/src/agent/agent'
|
||||||
import { createAgentSession } from './index'
|
import { createAgentSession, type DbProviderInfo } from './index'
|
||||||
import * as sandboxedBashModule from './tools/sandboxed-bash'
|
import * as sandboxedBashModule from './tools/sandboxed-bash'
|
||||||
import * as analyzeImageModule from './tools/analyze-image'
|
import * as analyzeImageModule from './tools/analyze-image'
|
||||||
import * as screenshotModule from './tools/screenshot'
|
import * as screenshotModule from './tools/screenshot'
|
||||||
@@ -25,6 +25,20 @@ describe('DatabaseSessionStorage', () => {
|
|||||||
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
|
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const mockProviders: DbProviderInfo[] = [
|
||||||
|
{
|
||||||
|
name: 'test-provider',
|
||||||
|
config: { api: 'openai-responses', apiKey: 'TEST_KEY' },
|
||||||
|
models: [
|
||||||
|
{
|
||||||
|
modelId: 'test-model',
|
||||||
|
name: 'Test Model',
|
||||||
|
config: mockModelConfig,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
]
|
||||||
|
|
||||||
const setupTestData = async () => {
|
const setupTestData = async () => {
|
||||||
const team = await prisma.team.create({ data: { name: 'Test Team' } })
|
const team = await prisma.team.create({ data: { name: 'Test Team' } })
|
||||||
const provider = await prisma.provider.create({
|
const provider = await prisma.provider.create({
|
||||||
@@ -340,7 +354,7 @@ describe('DatabaseSessionStorage', () => {
|
|||||||
allowedDomains: [],
|
allowedDomains: [],
|
||||||
sessionId: sessionId,
|
sessionId: sessionId,
|
||||||
userId: user.id,
|
userId: user.id,
|
||||||
providers: [],
|
providers: mockProviders,
|
||||||
})
|
})
|
||||||
|
|
||||||
// 7. Verify skill environment variables are restored.
|
// 7. Verify skill environment variables are restored.
|
||||||
@@ -453,7 +467,7 @@ describe('DatabaseSessionStorage', () => {
|
|||||||
sessionId: sessionId,
|
sessionId: sessionId,
|
||||||
userId: user.id,
|
userId: user.id,
|
||||||
userCommentId: 'new-comment-456',
|
userCommentId: 'new-comment-456',
|
||||||
providers: [],
|
providers: mockProviders,
|
||||||
})
|
})
|
||||||
|
|
||||||
// 4. Assert that the tools were instantiated using the new comment ID and user ID
|
// 4. Assert that the tools were instantiated using the new comment ID and user ID
|
||||||
@@ -507,7 +521,7 @@ describe('DatabaseSessionStorage', () => {
|
|||||||
sessionId: sessionId,
|
sessionId: sessionId,
|
||||||
userId: user.id,
|
userId: user.id,
|
||||||
userCommentId: 'new-comment-456',
|
userCommentId: 'new-comment-456',
|
||||||
providers: [],
|
providers: mockProviders,
|
||||||
})
|
})
|
||||||
|
|
||||||
// 4. Assert that the tools were instantiated using the new comment ID and user ID
|
// 4. Assert that the tools were instantiated using the new comment ID and user ID
|
||||||
|
|||||||
@@ -28,6 +28,27 @@ vi.mock('node:child_process', async () => {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const mockProviders: DbProviderInfo[] = [
|
||||||
|
{
|
||||||
|
name: 'google',
|
||||||
|
config: { api: 'google-generative-ai', apiKey: 'GOOGLE_API_KEY' },
|
||||||
|
models: [
|
||||||
|
{
|
||||||
|
modelId: 'gemini',
|
||||||
|
name: 'Gemini',
|
||||||
|
config: {
|
||||||
|
api: 'google-generative-ai',
|
||||||
|
reasoning: false,
|
||||||
|
input: ['text'],
|
||||||
|
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
|
||||||
|
contextWindow: 8192,
|
||||||
|
maxTokens: 4096,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
]
|
||||||
|
|
||||||
describe('formatSkillsForPrompt', () => {
|
describe('formatSkillsForPrompt', () => {
|
||||||
it('should return empty string if skills array is empty', () => {
|
it('should return empty string if skills array is empty', () => {
|
||||||
expect(formatSkillsForPrompt([])).toBe('')
|
expect(formatSkillsForPrompt([])).toBe('')
|
||||||
@@ -104,7 +125,7 @@ describe('Sandbox Network isolation integration', () => {
|
|||||||
systemPrompt: 'prompt',
|
systemPrompt: 'prompt',
|
||||||
teamSkills: [],
|
teamSkills: [],
|
||||||
allowedDomains: [],
|
allowedDomains: [],
|
||||||
providers: [],
|
providers: mockProviders,
|
||||||
})
|
})
|
||||||
|
|
||||||
// 4. Verify SandboxManager.initialize was called and extract the callback
|
// 4. Verify SandboxManager.initialize was called and extract the callback
|
||||||
@@ -232,7 +253,7 @@ describe('Sandbox Network isolation integration', () => {
|
|||||||
systemPrompt: 'prompt',
|
systemPrompt: 'prompt',
|
||||||
teamSkills: [],
|
teamSkills: [],
|
||||||
allowedDomains: [],
|
allowedDomains: [],
|
||||||
providers: [],
|
providers: mockProviders,
|
||||||
})
|
})
|
||||||
|
|
||||||
// 3. Call createAgentSession for Session 2
|
// 3. Call createAgentSession for Session 2
|
||||||
@@ -244,7 +265,7 @@ describe('Sandbox Network isolation integration', () => {
|
|||||||
systemPrompt: 'prompt',
|
systemPrompt: 'prompt',
|
||||||
teamSkills: [],
|
teamSkills: [],
|
||||||
allowedDomains: [],
|
allowedDomains: [],
|
||||||
providers: [],
|
providers: mockProviders,
|
||||||
})
|
})
|
||||||
|
|
||||||
expect(registeredCallback).toBeDefined()
|
expect(registeredCallback).toBeDefined()
|
||||||
@@ -314,7 +335,7 @@ describe('Sandbox Network isolation integration', () => {
|
|||||||
systemPrompt: 'prompt',
|
systemPrompt: 'prompt',
|
||||||
teamSkills: [],
|
teamSkills: [],
|
||||||
allowedDomains: [],
|
allowedDomains: [],
|
||||||
providers: [],
|
providers: mockProviders,
|
||||||
thinkingLevel: 'high',
|
thinkingLevel: 'high',
|
||||||
})
|
})
|
||||||
expect(harnessHigh.getThinkingLevel()).toBe('high')
|
expect(harnessHigh.getThinkingLevel()).toBe('high')
|
||||||
@@ -328,7 +349,7 @@ describe('Sandbox Network isolation integration', () => {
|
|||||||
systemPrompt: 'prompt',
|
systemPrompt: 'prompt',
|
||||||
teamSkills: [],
|
teamSkills: [],
|
||||||
allowedDomains: [],
|
allowedDomains: [],
|
||||||
providers: [],
|
providers: mockProviders,
|
||||||
})
|
})
|
||||||
expect(harnessDefault.getThinkingLevel()).toBe('off')
|
expect(harnessDefault.getThinkingLevel()).toBe('off')
|
||||||
} finally {
|
} finally {
|
||||||
@@ -367,7 +388,7 @@ describe('Sandbox Network isolation integration', () => {
|
|||||||
systemPrompt: 'prompt',
|
systemPrompt: 'prompt',
|
||||||
teamSkills: [],
|
teamSkills: [],
|
||||||
allowedDomains: [],
|
allowedDomains: [],
|
||||||
providers: [],
|
providers: mockProviders,
|
||||||
maxRetries: 5,
|
maxRetries: 5,
|
||||||
baseDelayMs: 1500,
|
baseDelayMs: 1500,
|
||||||
})
|
})
|
||||||
@@ -410,7 +431,7 @@ describe('Sandbox Network isolation integration', () => {
|
|||||||
systemPrompt: 'prompt',
|
systemPrompt: 'prompt',
|
||||||
teamSkills: [],
|
teamSkills: [],
|
||||||
allowedDomains: [],
|
allowedDomains: [],
|
||||||
providers: [],
|
providers: mockProviders,
|
||||||
})
|
})
|
||||||
const tools = harness.getTools()
|
const tools = harness.getTools()
|
||||||
const bashTool = tools.find((t) => t.name === 'bash')
|
const bashTool = tools.find((t) => t.name === 'bash')
|
||||||
@@ -453,6 +474,12 @@ describe('getModelFromDb', () => {
|
|||||||
expect(model?.provider).toBe('google')
|
expect(model?.provider).toBe('google')
|
||||||
expect(model?.api).toBe('google-generative-ai')
|
expect(model?.api).toBe('google-generative-ai')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('should throw an error when provider or model is not found in database configuration', () => {
|
||||||
|
expect(() => getModelFromDb([], 'google', 'non-existent')).toThrow(
|
||||||
|
'Provider "google" or model "non-existent" not found in database configuration',
|
||||||
|
)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('createAgentSession sandbox options', () => {
|
describe('createAgentSession sandbox options', () => {
|
||||||
@@ -497,7 +524,7 @@ describe('createAgentSession sandbox options', () => {
|
|||||||
systemPrompt: 'prompt',
|
systemPrompt: 'prompt',
|
||||||
teamSkills: [],
|
teamSkills: [],
|
||||||
allowedDomains: [],
|
allowedDomains: [],
|
||||||
providers: [],
|
providers: mockProviders,
|
||||||
})
|
})
|
||||||
expect(initializeSpy).toHaveBeenCalledWith(
|
expect(initializeSpy).toHaveBeenCalledWith(
|
||||||
expect.objectContaining({ enableWeakerNestedSandbox: false }),
|
expect.objectContaining({ enableWeakerNestedSandbox: false }),
|
||||||
@@ -514,7 +541,7 @@ describe('createAgentSession sandbox options', () => {
|
|||||||
systemPrompt: 'prompt',
|
systemPrompt: 'prompt',
|
||||||
teamSkills: [],
|
teamSkills: [],
|
||||||
allowedDomains: [],
|
allowedDomains: [],
|
||||||
providers: [],
|
providers: mockProviders,
|
||||||
})
|
})
|
||||||
expect(initializeSpy).toHaveBeenLastCalledWith(
|
expect(initializeSpy).toHaveBeenLastCalledWith(
|
||||||
expect.objectContaining({ enableWeakerNestedSandbox: true }),
|
expect.objectContaining({ enableWeakerNestedSandbox: true }),
|
||||||
|
|||||||
+19
-31
@@ -6,7 +6,7 @@ import {
|
|||||||
type ThinkingLevel,
|
type ThinkingLevel,
|
||||||
} from '@earendil-works/pi-agent-core'
|
} from '@earendil-works/pi-agent-core'
|
||||||
import { NodeExecutionEnv } from '@earendil-works/pi-agent-core/node'
|
import { NodeExecutionEnv } from '@earendil-works/pi-agent-core/node'
|
||||||
import { getModel } from '@earendil-works/pi-ai/compat'
|
import type { Api, Model } from '@earendil-works/pi-ai'
|
||||||
import { agentService } from '@shumai/core/src/agent/agent'
|
import { agentService } from '@shumai/core/src/agent/agent'
|
||||||
import { prisma } from '@shumai/db'
|
import { prisma } from '@shumai/db'
|
||||||
import { Type, type TSchema } from '@sinclair/typebox'
|
import { Type, type TSchema } from '@sinclair/typebox'
|
||||||
@@ -42,33 +42,23 @@ export function getModelFromDb(providers: DbProviderInfo[], providerName: string
|
|||||||
const dbProvider = providers.find((p) => p.name === providerName)
|
const dbProvider = providers.find((p) => p.name === providerName)
|
||||||
const dbModel = dbProvider?.models.find((m) => m.modelId === modelId)
|
const dbModel = dbProvider?.models.find((m) => m.modelId === modelId)
|
||||||
|
|
||||||
if (!dbProvider || !dbModel) return undefined
|
if (!dbProvider || !dbModel) {
|
||||||
|
throw new Error(
|
||||||
|
`Provider "${providerName}" or model "${modelId}" not found in database configuration`,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
// Model contains complex internal types from pi-ai
|
const m = {
|
||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
id: modelId,
|
||||||
let m: any
|
name: dbModel.name || modelId,
|
||||||
try {
|
provider: providerName,
|
||||||
// Try to get built-in model as template
|
api: 'openai-responses',
|
||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
baseUrl: '',
|
||||||
const builtIn = (getModel as any)(providerName, modelId)
|
reasoning: false,
|
||||||
if (!builtIn) {
|
input: ['text'],
|
||||||
throw new Error(`Model ${modelId} is not a built-in model`)
|
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
|
||||||
}
|
contextWindow: 4096,
|
||||||
m = { ...builtIn }
|
maxTokens: 4096,
|
||||||
} catch {
|
|
||||||
// Not a built-in model
|
|
||||||
m = {
|
|
||||||
id: modelId,
|
|
||||||
name: dbModel.name || modelId,
|
|
||||||
provider: providerName,
|
|
||||||
api: 'openai-responses',
|
|
||||||
baseUrl: '',
|
|
||||||
reasoning: false,
|
|
||||||
input: ['text'],
|
|
||||||
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
|
|
||||||
contextWindow: 4096,
|
|
||||||
maxTokens: 4096,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Override with database config
|
// Override with database config
|
||||||
@@ -82,7 +72,7 @@ export function getModelFromDb(providers: DbProviderInfo[], providerName: string
|
|||||||
if (dbModel.name) {
|
if (dbModel.name) {
|
||||||
m.name = dbModel.name
|
m.name = dbModel.name
|
||||||
}
|
}
|
||||||
return m
|
return m as Model<Api>
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getApiKeyAndHeadersFromDb(providers: DbProviderInfo[], providerName: string) {
|
export function getApiKeyAndHeadersFromDb(providers: DbProviderInfo[], providerName: string) {
|
||||||
@@ -151,9 +141,7 @@ export async function createAgentSession(params: CreateAgentSessionParams) {
|
|||||||
const agentDir = path.join(process.cwd(), '.pi', 'agents', agentId)
|
const agentDir = path.join(process.cwd(), '.pi', 'agents', agentId)
|
||||||
if (!fs.existsSync(agentDir)) fs.mkdirSync(agentDir, { recursive: true })
|
if (!fs.existsSync(agentDir)) fs.mkdirSync(agentDir, { recursive: true })
|
||||||
|
|
||||||
const model =
|
const model = getModelFromDb(params.providers, providerName, modelId)
|
||||||
getModelFromDb(params.providers, providerName, modelId) ||
|
|
||||||
(getModel as unknown as (p: string, m: string) => unknown)(providerName, modelId)
|
|
||||||
|
|
||||||
const piDir = path.join(process.cwd(), '.pi')
|
const piDir = path.join(process.cwd(), '.pi')
|
||||||
if (!fs.existsSync(piDir)) fs.mkdirSync(piDir, { recursive: true })
|
if (!fs.existsSync(piDir)) fs.mkdirSync(piDir, { recursive: true })
|
||||||
|
|||||||
Reference in New Issue
Block a user