fix(settings): support type-safe thinkingLevel and propagate to AgentHarness

This commit is contained in:
yiling
2026-06-22 09:58:35 +08:00
parent c20ef0e4b6
commit bea81a207a
9 changed files with 151 additions and 13 deletions
@@ -127,6 +127,54 @@ describe('Agent Activities', () => {
expect(res.text).toBe('Error: API key not valid')
})
it('should pass thinkingLevel from agent config to createAgentSession', async () => {
const mockHarness = {
prompt: vi.fn().mockResolvedValue({
content: [],
usage: { input: 0, output: 0 },
}),
getThinkingLevel: vi.fn().mockReturnValue('high'),
}
const mockSession = {
getEntries: vi.fn().mockResolvedValue([]),
getStorage: vi.fn().mockReturnValue({ sessionId: 'mock-session-id' }),
}
vi.mocked(piAgent.createAgentSession).mockResolvedValue({
session: mockSession as unknown as Session<DatabaseSessionMetadata>,
harness: mockHarness as unknown as AgentHarness,
})
const context = {
agent: {
id: 'b1',
provider: { name: 'google' },
modelRef: { modelId: 'gemini' },
config: { thinkingLevel: 'high' },
},
dbProviders: [],
teamSkills: [],
allowedDomains: [],
} as unknown as AgentExecutionContext
await agentChatActivity({
teamId: 't1',
agentId: 'b1',
message: 'Hi',
imageUrls: [],
projectId: 'p1',
folderId: 'f1',
sessionId: 'mock-session-id',
context,
})
expect(piAgent.createAgentSession).toHaveBeenCalledWith(
expect.objectContaining({
thinkingLevel: 'high',
}),
)
})
it('should call autofillAiActivity and run autofill tool', async () => {
const mockHarness = {
prompt: vi.fn().mockResolvedValue({
+4
View File
@@ -97,12 +97,16 @@ If you need to create files in the local filesystem (for example, a temporary fi
systemPrompt += `\n\nYour current model supports the following input types: ${modelConfig.input.join(', ')}.`
}
const agentConfig = agent.config as PrismaJson.AgentConfig | null | undefined
const thinkingLevel = agentConfig?.thinkingLevel || 'off'
const { session, harness } = await createAgentSession({
teamId: params.teamId,
agentId: params.agentId,
providerName,
modelId,
systemPrompt,
thinkingLevel,
teamSkills: teamSkills.map((s) => ({
id: s.id,
name: s.name,
+72
View File
@@ -257,4 +257,76 @@ describe('Sandbox Network isolation integration', () => {
resetSpy.mockRestore()
}
})
it('should pass thinkingLevel to AgentHarness and default to off', async () => {
const team = await prisma.team.create({
data: { name: 'Test Team' },
})
await prisma.user.create({
data: {
id: 'test-agent-1',
name: 'Agent 1',
email: 'agent1@test.com',
type: 'agent',
},
})
await prisma.agent.create({
data: {
id: 'test-agent-1',
teamId: team.id,
type: 'chat',
config: { provider: 'google', model: 'gemini' },
},
})
await prisma.user.create({
data: {
id: 'test-agent-2',
name: 'Agent 2',
email: 'agent2@test.com',
type: 'agent',
},
})
await prisma.agent.create({
data: {
id: 'test-agent-2',
teamId: team.id,
type: 'chat',
config: { provider: 'google', model: 'gemini' },
},
})
const initializeSpy = vi.spyOn(SandboxManager, 'initialize').mockResolvedValue()
try {
// Call createAgentSession with thinkingLevel
const { harness: harnessHigh } = await createAgentSession({
teamId: team.id,
agentId: 'test-agent-1',
providerName: 'google',
modelId: 'gemini',
systemPrompt: 'prompt',
teamSkills: [],
allowedDomains: [],
providers: [],
thinkingLevel: 'high',
})
expect(harnessHigh.getThinkingLevel()).toBe('high')
// Call createAgentSession without thinkingLevel
const { harness: harnessDefault } = await createAgentSession({
teamId: team.id,
agentId: 'test-agent-2',
providerName: 'google',
modelId: 'gemini',
systemPrompt: 'prompt',
teamSkills: [],
allowedDomains: [],
providers: [],
})
expect(harnessDefault.getThinkingLevel()).toBe('off')
} finally {
initializeSpy.mockRestore()
}
})
})
+9 -1
View File
@@ -1,5 +1,10 @@
import { SandboxManager } from '@anthropic-ai/sandbox-runtime'
import { AgentHarness, Session, type AgentTool } from '@earendil-works/pi-agent-core'
import {
AgentHarness,
Session,
type AgentTool,
type ThinkingLevel,
} from '@earendil-works/pi-agent-core'
import { NodeExecutionEnv } from '@earendil-works/pi-agent-core/node'
import { getModel } from '@earendil-works/pi-ai'
import { Type, type TSchema } from '@sinclair/typebox'
@@ -29,6 +34,7 @@ export interface CreateAgentSessionParams {
userId?: string
userCommentId?: string | null
customTools?: AgentTool[]
thinkingLevel?: string
providers: Array<{
name: string
config: PrismaJson.ProviderConfig
@@ -53,6 +59,7 @@ export async function createAgentSession(params: CreateAgentSessionParams) {
userId,
userCommentId: passedUserCommentId,
customTools = [],
thinkingLevel,
} = params
const storage = await DatabaseSessionStorage.create({
@@ -233,6 +240,7 @@ export async function createAgentSession(params: CreateAgentSessionParams) {
env: new NodeExecutionEnv({ cwd: process.cwd() }),
session,
model,
thinkingLevel: (thinkingLevel || 'off') as ThinkingLevel,
systemPrompt: async () => {
let prompt = systemPrompt
+3 -3
View File
@@ -39,7 +39,7 @@ const route = new Hono<{ Variables: { user: User } }>()
avatar: (await getAvatarUrl(agent.user.image)) || undefined,
providerId: agent.providerId || undefined,
modelId: agent.modelId || undefined,
thinkingLevel: config.thinkingLevel || '',
thinkingLevel: config.thinkingLevel || 'off',
systemPrompt: config.systemPrompt,
soul: agent.soul || undefined,
skills: agent.skills.map((s) => ({
@@ -81,7 +81,7 @@ const route = new Hono<{ Variables: { user: User } }>()
avatar: (await getAvatarUrl(agent.user.image)) || undefined,
providerId: agent.providerId || undefined,
modelId: agent.modelId || undefined,
thinkingLevel: config.thinkingLevel || '',
thinkingLevel: config.thinkingLevel || 'off',
systemPrompt: config.systemPrompt,
soul: agent.soul || undefined,
skills: agent.skills.map((s) => ({
@@ -119,7 +119,7 @@ const route = new Hono<{ Variables: { user: User } }>()
avatar: (await getAvatarUrl(agent.user.image)) || undefined,
providerId: agent.providerId || undefined,
modelId: agent.modelId || undefined,
thinkingLevel: config.thinkingLevel || '',
thinkingLevel: config.thinkingLevel || 'off',
systemPrompt: config.systemPrompt,
soul: agent.soul || undefined,
skills: agent.skills.map((s) => ({
+1 -1
View File
@@ -27,7 +27,7 @@ declare global {
export interface AgentConfig {
provider: string
model: string
thinkingLevel?: string
thinkingLevel?: 'off' | 'minimal' | 'low' | 'medium' | 'high' | 'xhigh'
systemPrompt?: string
appendSystemPrompt?: string[]
}
+5 -2
View File
@@ -3,6 +3,9 @@ import { z } from 'zod'
export const agentTypeSchema = z.enum(['chat', 'autofill', 'embedding'])
export type AgentType = z.infer<typeof agentTypeSchema>
export const thinkingLevelSchema = z.enum(['off', 'minimal', 'low', 'medium', 'high', 'xhigh'])
export type ThinkingLevel = z.infer<typeof thinkingLevelSchema>
export const agentSkillSchema = z.object({
id: z.string().optional(),
skillId: z.string(),
@@ -23,7 +26,7 @@ export const agentInfoSchema = z.object({
avatar: z.string().optional(),
providerId: z.string().optional(),
modelId: z.string().optional(),
thinkingLevel: z.string().optional().default(''),
thinkingLevel: thinkingLevelSchema.optional().default('off'),
systemPrompt: z.string().optional(),
soul: z.string().optional(),
skills: z.array(agentSkillSchema).optional(),
@@ -37,7 +40,7 @@ const baseAgentRequest = z.object({
avatar: z.string().optional(),
providerId: z.string().optional(),
modelId: z.string().optional(),
thinkingLevel: z.string().optional().default(''),
thinkingLevel: thinkingLevelSchema.optional().default('off'),
systemPrompt: z.string().optional(),
soul: z.string().optional(),
skills: z.array(z.string()).optional(),
@@ -24,7 +24,7 @@ import { useForm, useStore } from '@tanstack/react-form'
import { Loader2, Puzzle } from 'lucide-react'
import { useEffect, useMemo } from 'react'
import { toast } from 'sonner'
import { AgentInfo, AgentType } from '@shumai/dtos'
import { AgentInfo, AgentType, ThinkingLevel, thinkingLevelSchema } from '@shumai/dtos'
import { Textarea } from '@/ui/components/ui/textarea'
import { ScrollArea } from '@/ui/components/ui/scroll-area'
import { z } from 'zod'
@@ -47,7 +47,7 @@ const agentFormSchema = z.object({
providerId: z.string().optional(),
modelId: z.string().optional(),
soul: z.string().optional(),
thinkingLevel: z.string().optional(),
thinkingLevel: thinkingLevelSchema.optional(),
systemPrompt: z.string().optional(),
skills: z.array(z.string()).optional(),
})
@@ -121,7 +121,7 @@ export function AgentFormDialog({
providerId: initialValues?.providerId || '',
modelId: initialValues?.modelId || '',
soul: initialValues?.soul || '',
thinkingLevel: initialValues?.thinkingLevel || 'medium',
thinkingLevel: initialValues?.thinkingLevel || 'off',
systemPrompt: initialValues?.systemPrompt || '',
skills: initialValues?.skills?.map((s) => s.skillId) || ([] as string[]),
} as AgentFormValues,
@@ -400,14 +400,17 @@ export function AgentFormDialog({
children={(field) => (
<Field>
<FieldLabel>Thinking Level</FieldLabel>
<Select value={field.state.value} onValueChange={field.handleChange}>
<Select value={field.state.value} onValueChange={(val) => field.handleChange(val as ThinkingLevel)}>
<SelectTrigger>
<SelectValue />
</SelectTrigger>
<SelectContent>
<SelectItem value="off">Off</SelectItem>
<SelectItem value="minimal">Minimal</SelectItem>
<SelectItem value="low">Low (Fast)</SelectItem>
<SelectItem value="medium">Medium (Balanced)</SelectItem>
<SelectItem value="high">High (Reasoning)</SelectItem>
<SelectItem value="xhigh">Extra High</SelectItem>
</SelectContent>
</Select>
</Field>
@@ -16,7 +16,7 @@ import {
} from 'lucide-react'
import { useState } from 'react'
import { toast } from 'sonner'
import { AgentInfo, AgentType } from '@shumai/dtos'
import { AgentInfo, AgentType, ThinkingLevel } from '@shumai/dtos'
import { AgentFormDialog } from './AgentFormDialog'
import {
DropdownMenu,
@@ -104,7 +104,7 @@ export function AgentsSettings({ teamId }: AgentsSettingsProps) {
avatar?: string
providerId?: string
modelId?: string
thinkingLevel?: string
thinkingLevel?: ThinkingLevel
systemPrompt?: string
soul?: string
skills?: string[]