@@ -8,17 +8,15 @@ import * as Session from "./session"
88import { Agent } from "../agent/agent"
99import { Provider } from "@/provider/provider"
1010import { ModelID , ProviderID } from "../provider/schema"
11- import { type Tool as AITool , tool , jsonSchema , type ToolExecutionOptions , asSchema } from "ai"
11+ import { type Tool as AITool , tool , jsonSchema } from "ai"
1212import type { JSONSchema7 } from "@ai-sdk/provider"
1313import { SessionCompaction } from "./compaction"
1414import { Bus } from "../bus"
15- import { ProviderTransform } from "@/provider/transform"
1615import { SystemPrompt } from "./system"
1716import { Instruction } from "./instruction"
1817import { Plugin } from "../plugin"
1918import MAX_STEPS from "../session/prompt/max-steps.txt"
2019import { ToolRegistry } from "@/tool/registry"
21- import { ToolJsonSchema } from "@/tool/json-schema"
2220import { MCP } from "../mcp"
2321import { LSP } from "@/lsp/lsp"
2422import { ulid } from "ulid"
@@ -48,7 +46,6 @@ import * as EffectLogger from "@opencode-ai/core/effect/logger"
4846import { InstanceState } from "@/effect/instance-state"
4947import { TaskTool , type TaskPromptOps } from "@/tool/task"
5048import { SessionRunState } from "./run-state"
51- import { EffectBridge } from "@/effect/bridge"
5249import { RuntimeFlags } from "@/effect/runtime-flags"
5350import { EventV2 } from "@opencode-ai/core/event"
5451import { EventV2Bridge } from "@/event-v2-bridge"
@@ -63,6 +60,7 @@ import * as Database from "@/storage/db"
6360import { SessionTable } from "./session.sql"
6461import { referencePromptMetadata , referenceTextPart } from "./prompt/reference"
6562import { SessionReminders } from "./reminders"
63+ import { SessionTools } from "./tools"
6664import { LLMEvent } from "@opencode-ai/llm"
6765
6866// @ts -ignore
@@ -126,9 +124,6 @@ export const layer = Layer.effect(
126124 const references = yield * Reference . Service
127125 const events = yield * EventV2Bridge . Service
128126 const flags = yield * RuntimeFlags . Service
129- const runner = Effect . fn ( "SessionPrompt.runner" ) ( function * ( ) {
130- return yield * EffectBridge . make ( )
131- } )
132127 const ops = Effect . fn ( "SessionPrompt.ops" ) ( function * ( ) {
133128 return {
134129 cancel : ( sessionID : SessionID ) => cancel ( sessionID ) ,
@@ -301,186 +296,6 @@ export const layer = Layer.effect(
301296 . pipe ( Effect . catchCause ( ( cause ) => elog . error ( "failed to generate title" , { error : Cause . squash ( cause ) } ) ) )
302297 } )
303298
304- const resolveTools = Effect . fn ( "SessionPrompt.resolveTools" ) ( function * ( input : {
305- agent : Agent . Info
306- model : Provider . Model
307- session : Session . Info
308- tools ?: Record < string , boolean >
309- processor : Pick < SessionProcessor . Handle , "message" | "updateToolCall" | "completeToolCall" >
310- bypassAgentCheck : boolean
311- messages : MessageV2 . WithParts [ ]
312- } ) {
313- using _ = log . time ( "resolveTools" )
314- const tools : Record < string , AITool > = { }
315- const run = yield * runner ( )
316- const promptOps = yield * ops ( )
317-
318- const context = ( args : any , options : ToolExecutionOptions ) : Tool . Context => ( {
319- sessionID : input . session . id ,
320- abort : options . abortSignal ! ,
321- messageID : input . processor . message . id ,
322- callID : options . toolCallId ,
323- extra : { model : input . model , bypassAgentCheck : input . bypassAgentCheck , promptOps } ,
324- agent : input . agent . name ,
325- messages : input . messages ,
326- metadata : ( val ) =>
327- input . processor . updateToolCall ( options . toolCallId , ( match ) => {
328- if ( ! [ "running" , "pending" ] . includes ( match . state . status ) ) return match
329- return {
330- ...match ,
331- state : {
332- title : val . title ,
333- metadata : val . metadata ,
334- status : "running" ,
335- input : args ,
336- time : { start : Date . now ( ) } ,
337- } ,
338- }
339- } ) ,
340- ask : ( req ) =>
341- permission
342- . ask ( {
343- ...req ,
344- sessionID : input . session . id ,
345- tool : { messageID : input . processor . message . id , callID : options . toolCallId } ,
346- ruleset : Permission . merge ( input . agent . permission , input . session . permission ?? [ ] ) ,
347- } )
348- . pipe ( Effect . orDie ) ,
349- } )
350-
351- for ( const item of yield * registry . tools ( {
352- modelID : ModelID . make ( input . model . api . id ) ,
353- providerID : input . model . providerID ,
354- agent : input . agent ,
355- } ) ) {
356- const schema = ProviderTransform . schema ( input . model , ToolJsonSchema . fromTool ( item ) )
357- tools [ item . id ] = tool ( {
358- description : item . description ,
359- inputSchema : jsonSchema ( schema ) ,
360- execute ( args , options ) {
361- return run . promise (
362- Effect . gen ( function * ( ) {
363- const ctx = context ( args , options )
364- yield * plugin . trigger (
365- "tool.execute.before" ,
366- { tool : item . id , sessionID : ctx . sessionID , callID : ctx . callID } ,
367- { args } ,
368- )
369- const result = yield * item . execute ( args , ctx )
370- const output = {
371- ...result ,
372- attachments : result . attachments ?. map ( ( attachment ) => ( {
373- ...attachment ,
374- id : PartID . ascending ( ) ,
375- sessionID : ctx . sessionID ,
376- messageID : input . processor . message . id ,
377- } ) ) ,
378- }
379- yield * plugin . trigger (
380- "tool.execute.after" ,
381- { tool : item . id , sessionID : ctx . sessionID , callID : ctx . callID , args } ,
382- output ,
383- )
384- if ( options . abortSignal ?. aborted ) {
385- yield * input . processor . completeToolCall ( options . toolCallId , output )
386- }
387- return output
388- } ) ,
389- )
390- } ,
391- } )
392- }
393-
394- for ( const [ key , item ] of Object . entries ( yield * mcp . tools ( ) ) ) {
395- const execute = item . execute
396- if ( ! execute ) continue
397-
398- const schema = yield * Effect . promise ( ( ) => Promise . resolve ( asSchema ( item . inputSchema ) . jsonSchema ) )
399- const transformed = ProviderTransform . schema ( input . model , schema )
400- item . inputSchema = jsonSchema ( transformed )
401- item . execute = ( args , opts ) =>
402- run . promise (
403- Effect . gen ( function * ( ) {
404- const ctx = context ( args , opts )
405- yield * plugin . trigger (
406- "tool.execute.before" ,
407- { tool : key , sessionID : ctx . sessionID , callID : opts . toolCallId } ,
408- { args } ,
409- )
410- const result : Awaited < ReturnType < NonNullable < typeof execute > > > = yield * Effect . gen ( function * ( ) {
411- yield * ctx . ask ( { permission : key , metadata : { } , patterns : [ "*" ] , always : [ "*" ] } )
412- return yield * Effect . promise ( ( ) => execute ( args , opts ) )
413- } ) . pipe (
414- Effect . withSpan ( "Tool.execute" , {
415- attributes : {
416- "tool.name" : key ,
417- "tool.call_id" : opts . toolCallId ,
418- "session.id" : ctx . sessionID ,
419- "message.id" : input . processor . message . id ,
420- } ,
421- } ) ,
422- )
423- yield * plugin . trigger (
424- "tool.execute.after" ,
425- { tool : key , sessionID : ctx . sessionID , callID : opts . toolCallId , args } ,
426- result ,
427- )
428-
429- const textParts : string [ ] = [ ]
430- const attachments : Omit < MessageV2 . FilePart , "id" | "sessionID" | "messageID" > [ ] = [ ]
431- for ( const contentItem of result . content ) {
432- if ( contentItem . type === "text" ) textParts . push ( contentItem . text )
433- else if ( contentItem . type === "image" ) {
434- attachments . push ( {
435- type : "file" ,
436- mime : contentItem . mimeType ,
437- url : `data:${ contentItem . mimeType } ;base64,${ contentItem . data } ` ,
438- } )
439- } else if ( contentItem . type === "resource" ) {
440- const { resource } = contentItem
441- if ( resource . text ) textParts . push ( resource . text )
442- if ( resource . blob ) {
443- attachments . push ( {
444- type : "file" ,
445- mime : resource . mimeType ?? "application/octet-stream" ,
446- url : `data:${ resource . mimeType ?? "application/octet-stream" } ;base64,${ resource . blob } ` ,
447- filename : resource . uri ,
448- } )
449- }
450- }
451- }
452-
453- const truncated = yield * truncate . output ( textParts . join ( "\n\n" ) , { } , input . agent )
454- const metadata = {
455- ...result . metadata ,
456- truncated : truncated . truncated ,
457- ...( truncated . truncated && { outputPath : truncated . outputPath } ) ,
458- }
459-
460- const output = {
461- title : "" ,
462- metadata,
463- output : truncated . content ,
464- attachments : attachments . map ( ( attachment ) => ( {
465- ...attachment ,
466- id : PartID . ascending ( ) ,
467- sessionID : ctx . sessionID ,
468- messageID : input . processor . message . id ,
469- } ) ) ,
470- content : result . content ,
471- }
472- if ( opts . abortSignal ?. aborted ) {
473- yield * input . processor . completeToolCall ( opts . toolCallId , output )
474- }
475- return output
476- } ) ,
477- )
478- tools [ key ] = item
479- }
480-
481- return tools
482- } )
483-
484299 const handleSubtask = Effect . fn ( "SessionPrompt.handleSubtask" ) ( function * ( input : {
485300 task : MessageV2 . SubtaskPart
486301 model : Provider . Model
@@ -1552,16 +1367,23 @@ export const layer = Layer.effect(
15521367 const outcome : "break" | "continue" = yield * Effect . gen ( function * ( ) {
15531368 const lastUserMsg = msgs . findLast ( ( m ) => m . info . role === "user" )
15541369 const bypassAgentCheck = lastUserMsg ?. parts . some ( ( p ) => p . type === "agent" ) ?? false
1370+ const promptOps = yield * ops ( )
15551371
1556- const tools = yield * resolveTools ( {
1372+ const tools = yield * SessionTools . resolve ( {
15571373 agent,
15581374 session,
15591375 model,
1560- tools : lastUser . tools ,
15611376 processor : handle ,
15621377 bypassAgentCheck,
15631378 messages : msgs ,
1564- } )
1379+ promptOps,
1380+ } ) . pipe (
1381+ Effect . provideService ( Plugin . Service , plugin ) ,
1382+ Effect . provideService ( Permission . Service , permission ) ,
1383+ Effect . provideService ( ToolRegistry . Service , registry ) ,
1384+ Effect . provideService ( MCP . Service , mcp ) ,
1385+ Effect . provideService ( Truncate . Service , truncate ) ,
1386+ )
15651387
15661388 if ( lastUser . format ?. type === "json_schema" ) {
15671389 tools [ "StructuredOutput" ] = createStructuredOutputTool ( {
0 commit comments