@@ -12,6 +12,7 @@ import { Environment } from "../environment/index.js"
1212import { FileAccess } from "../file-access.js"
1313import { FileMutation } from "../file-mutation.js"
1414import { FileWrite } from "../file-write.js"
15+ import { Flag } from "../flag.js"
1516import { makeLocationNode } from "@opencode/util/effect/app-node"
1617import { InstructionDiscovery } from "../instruction-discovery.js"
1718import { Instructions } from "../instructions/index.js"
@@ -22,6 +23,8 @@ import { McpTool } from "../tool/mcp.js"
2223import { ReferenceInstructions } from "../reference/instructions.js"
2324import { SkillInstructions } from "../skill/instructions.js"
2425import { Tool } from "../tool.js"
26+ import { modelTools } from "../tool/model-policy.js"
27+ import { WebSearch } from "../websearch.js"
2528import { AgentNotFoundError } from "./error.js"
2629import { SessionHistory } from "./history.js"
2730import { InstructionEntry } from "./instruction-entry.js"
@@ -86,6 +89,7 @@ const layer = Layer.effect(
8689 const db = ( yield * Database . Service ) . db
8790 const discovery = yield * InstructionDiscovery . Service
8891 const entries = yield * InstructionEntry . Service
92+ const flags = yield * Flag . Service
8993 const location = yield * Location . Service
9094 const mcpInstructions = yield * McpInstructions . Service
9195 const mcpTools = yield * McpTool . Service
@@ -95,7 +99,8 @@ const layer = Layer.effect(
9599 const skillInstructions = yield * SkillInstructions . Service
96100 const store = yield * SessionStore . Service
97101 const registry = yield * Tool . Service
98- const codeMode = yield * CodeModeTool . services
102+ const services = yield * CodeModeTool . services
103+ const websearch = yield * WebSearch . Service
99104
100105 const resolveModel = (
101106 session : SessionSchema . Info ,
@@ -150,14 +155,27 @@ const layer = Layer.effect(
150155 if ( ! agent . info ) return yield * new AgentNotFoundError ( { sessionID : session . id , agent : session . agent ?? agent . id } )
151156 // Session permissions narrow discovery the same way they narrow the tool snapshot.
152157 const permissions = Permission . merge ( agent . info . permissions , session . permissions ?? [ ] )
158+ const [ selected , hasWebSearch , fullCodeMode ] = yield * Effect . all (
159+ [
160+ models . select ( session , model . available ) . pipe ( Effect . orElseSucceed ( ( ) => undefined ) ) ,
161+ websearch . available ( ) ,
162+ flags . enabled ( "full_codemode" ) ,
163+ ] ,
164+ { concurrency : "unbounded" } ,
165+ )
166+ const fallbackRef = session . model ?? agent . info . model
167+ const policyModel = selected ?. info ?? ( fallbackRef && Model . Info . default ( fallbackRef . providerID , fallbackRef . id ) )
168+ const tools = yield * registry . snapshot ( permissions , {
169+ services,
170+ tools : modelTools ( { model : policyModel , websearch : hasWebSearch , fullCodeMode } ) ,
171+ } )
153172 const loaded = yield * Effect . all (
154173 {
155- tools : registry . snapshot ( permissions , codeMode ) ,
156174 builtins : builtins . load ( ) ,
157175 discovery : discovery . load ( ) ,
158176 skills : skillInstructions . load ( permissions ) ,
159177 references : referenceInstructions . load ( ) ,
160- mcp : mcpInstructions . load ( permissions ) ,
178+ mcp : mcpInstructions . load ( tools ) ,
161179 entries : entries . load ( sessionID ) ,
162180 } ,
163181 { concurrency : "unbounded" } ,
@@ -169,15 +187,15 @@ const layer = Layer.effect(
169187 // prompt-cache prefix: user-level catalog and guidance, then project instructions, then
170188 // the date and environment, which vary by day and directory.
171189 instructions : Instructions . combine ( [
172- CodeModeInstructions . make ( loaded . tools . codeModeCatalog ) ,
190+ CodeModeInstructions . make ( tools . codeModeCatalog ) ,
173191 loaded . mcp ,
174192 loaded . references ,
175193 loaded . skills ,
176194 loaded . discovery ,
177195 loaded . builtins ,
178196 loaded . entries ,
179197 ] ) ,
180- tools : loaded . tools ,
198+ tools,
181199 }
182200 } )
183201
@@ -229,6 +247,7 @@ export const node = makeLocationNode({
229247 FileAccess . node ,
230248 FileMutation . node ,
231249 FileWrite . node ,
250+ Flag . node ,
232251 InstructionBuiltIns . node ,
233252 InstructionDiscovery . node ,
234253 InstructionEntry . node ,
@@ -241,5 +260,6 @@ export const node = makeLocationNode({
241260 SessionStore . node ,
242261 SkillInstructions . node ,
243262 Tool . node ,
263+ WebSearch . node ,
244264 ] ,
245265} )
0 commit comments