22 CommandId ,
33 type EnvironmentId ,
44 NodeId ,
5+ OrchestratorMcpCapabilitiesInput ,
6+ OrchestratorMcpCapabilitiesResult ,
57 OrchestratorMcpFailure ,
68 type OrchestratorMcpDelegateTaskInput ,
79 type OrchestratorMcpTaskCancelResult ,
@@ -27,7 +29,7 @@ import * as Option from "effect/Option";
2729import * as Schedule from "effect/Schedule" ;
2830import * as Scope from "effect/Scope" ;
2931import * as Stream from "effect/Stream" ;
30- import type * as Tool from "effect/ai/Tool" ;
32+ import * as Tool from "effect/ai/Tool" ;
3133import { withExecutedModelNote } from "@t3tools/provider-core/server/subagentProjection" ;
3234
3335import type * as McpInvocationContext from "../mcp/McpInvocationContext.ts" ;
@@ -66,6 +68,14 @@ export const FollowRetrySchedule = Context.Reference<Schedule.Schedule<unknown>>
6668
6769type ThreadScope = McpInvocationContext . McpThreadInvocationScope ;
6870
71+ /** `orchestrator_capabilities` there. Its toolkit imports this module, so it is described here. */
72+ const RemoteCapabilitiesTool = Tool . make ( "orchestrator_capabilities" , {
73+ parameters : OrchestratorMcpCapabilitiesInput ,
74+ success : OrchestratorMcpCapabilitiesResult ,
75+ failure : OrchestratorMcpFailure ,
76+ failureMode : "return" ,
77+ } ) ;
78+
6979/**
7080 * `delegate_task` to a linked environment. The task's child is an ordinary
7181 * thread there, launched through the link; the parent here records the task
@@ -202,6 +212,45 @@ const make = Effect.gen(function* () {
202212 } ) ;
203213 } ) ;
204214
215+ /** The instance there of the driver the caller named, preferring the one this thread runs on, as a task here does. */
216+ const resolveRemoteInstance = Effect . fn ( "RemoteDelegation.resolveRemoteInstance" ) ( function * (
217+ scope : ThreadScope ,
218+ environmentId : EnvironmentId ,
219+ requested : {
220+ readonly driverKind : ProviderDriverKind ;
221+ readonly providerInstanceId : ProviderInstanceId | undefined ;
222+ readonly inheritedInstanceId : ProviderInstanceId ;
223+ } ,
224+ ) {
225+ const { providers } = yield * forwarding . call ( scope , RemoteCapabilitiesTool , environmentId , { } ) ;
226+ if ( requested . providerInstanceId !== undefined ) {
227+ const named = providers . find (
228+ ( provider ) => provider . providerInstanceId === requested . providerInstanceId ,
229+ ) ;
230+ if ( named !== undefined && named . driverKind !== requested . driverKind ) {
231+ return yield * new OrchestratorMcpFailure ( {
232+ code : "invalid_request" ,
233+ message : `Provider instance ${ named . providerInstanceId } there uses driver ${ named . driverKind } , not ${ requested . driverKind } .` ,
234+ } ) ;
235+ }
236+ return requested . providerInstanceId ;
237+ }
238+ const candidates = providers . filter (
239+ ( provider ) => provider . driverKind === requested . driverKind && provider . canRunChildTask ,
240+ ) ;
241+ const chosen =
242+ candidates . find (
243+ ( provider ) => provider . providerInstanceId === requested . inheritedInstanceId ,
244+ ) ?? candidates [ 0 ] ;
245+ if ( chosen === undefined ) {
246+ return yield * new OrchestratorMcpFailure ( {
247+ code : "provider_unavailable" ,
248+ message : `No instance of driver ${ requested . driverKind } there can run a child task. orchestrator_capabilities with this environmentId lists its providers.` ,
249+ } ) ;
250+ }
251+ return chosen . providerInstanceId ;
252+ } ) ;
253+
205254 const delegate : RemoteDelegation [ "Service" ] [ "delegate" ] = Effect . fn ( "RemoteDelegation.delegate" ) (
206255 function * ( scope , input ) {
207256 const parent = yield * threads . getThreadRecords ( scope . thread . threadId , [ "runs" ] ) . pipe (
@@ -240,7 +289,14 @@ const make = Effect.gen(function* () {
240289 // The provider and model inherit as they do for a task here. Another
241290 // provider there has no model to inherit, so the caller names one.
242291 const inherited = parent . thread . modelSelection ;
243- const instanceId = target . providerInstanceId ?? inherited . instanceId ;
292+ const instanceId =
293+ target . driverKind === undefined
294+ ? ( target . providerInstanceId ?? inherited . instanceId )
295+ : yield * resolveRemoteInstance ( scope , target . environmentId , {
296+ driverKind : target . driverKind ,
297+ providerInstanceId : target . providerInstanceId ,
298+ inheritedInstanceId : inherited . instanceId ,
299+ } ) ;
244300 const model =
245301 target . model ?? ( instanceId === inherited . instanceId ? inherited . model : undefined ) ;
246302 if ( model === undefined ) {
0 commit comments