-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Expand file tree
/
Copy pathscheduler.ts
More file actions
827 lines (739 loc) · 24.7 KB
/
scheduler.ts
File metadata and controls
827 lines (739 loc) · 24.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
/**
* @license
* Copyright 2026 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import type { Config } from '../config/config.js';
import type { MessageBus } from '../confirmation-bus/message-bus.js';
import { SchedulerStateManager } from './state-manager.js';
import { resolveConfirmation } from './confirmation.js';
import { checkPolicy, updatePolicy, getPolicyDenialError } from './policy.js';
import { ToolExecutor } from './tool-executor.js';
import { ToolModificationHandler } from './tool-modifier.js';
import {
type ToolCallRequestInfo,
type ToolCall,
type ToolCallResponseInfo,
type CompletedToolCall,
type ExecutingToolCall,
type ValidatingToolCall,
type ErroredToolCall,
type SuccessfulToolCall,
CoreToolCallStatus,
type ScheduledToolCall,
} from './types.js';
import { ToolErrorType } from '../tools/tool-error.js';
import { PolicyDecision, ApprovalMode } from '../policy/types.js';
import { pauseForStepThrough } from './step-through.js';
import {
ToolConfirmationOutcome,
type AnyDeclarativeTool,
Kind,
} from '../tools/tools.js';
import { getToolSuggestion } from '../utils/tool-utils.js';
import { runInDevTraceSpan } from '../telemetry/trace.js';
import { logToolCall } from '../telemetry/loggers.js';
import { ToolCallEvent } from '../telemetry/types.js';
import type { EditorType } from '../utils/editor.js';
import {
MessageBusType,
type SerializableConfirmationDetails,
type ToolConfirmationRequest,
} from '../confirmation-bus/types.js';
import { runWithToolCallContext } from '../utils/toolCallContext.js';
import {
coreEvents,
CoreEvent,
type McpProgressPayload,
} from '../utils/events.js';
import { GeminiCliOperation } from '../telemetry/constants.js';
interface SchedulerQueueItem {
requests: ToolCallRequestInfo[];
signal: AbortSignal;
resolve: (results: CompletedToolCall[]) => void;
reject: (reason?: Error) => void;
}
export interface SchedulerOptions {
config: Config;
messageBus: MessageBus;
getPreferredEditor: () => EditorType | undefined;
schedulerId: string;
parentCallId?: string;
onWaitingForConfirmation?: (waiting: boolean) => void;
}
const createErrorResponse = (
request: ToolCallRequestInfo,
error: Error,
errorType: ToolErrorType | undefined,
): ToolCallResponseInfo => ({
callId: request.callId,
error,
responseParts: [
{
functionResponse: {
id: request.callId,
name: request.name,
response: { error: error.message },
},
},
],
resultDisplay: error.message,
errorType,
contentLength: error.message.length,
});
/**
* Event-Driven Orchestrator for Tool Execution.
* Coordinates execution via state updates and event listening.
*/
export class Scheduler {
// Tracks which MessageBus instances have the legacy listener attached to prevent duplicates.
private static subscribedMessageBuses = new WeakSet<MessageBus>();
private readonly state: SchedulerStateManager;
private readonly executor: ToolExecutor;
private readonly modifier: ToolModificationHandler;
private readonly config: Config;
private readonly messageBus: MessageBus;
private readonly getPreferredEditor: () => EditorType | undefined;
private readonly schedulerId: string;
private readonly parentCallId?: string;
private readonly onWaitingForConfirmation?: (waiting: boolean) => void;
private isProcessing = false;
private isCancelling = false;
private readonly requestQueue: SchedulerQueueItem[] = [];
/** Tracks how many tool calls have been stepped through in the current batch. */
private stepIndex = 0;
/** Total tool calls enqueued in the current batch, used for the step counter. */
private stepTotal = 0;
constructor(options: SchedulerOptions) {
this.config = options.config;
this.messageBus = options.messageBus;
this.getPreferredEditor = options.getPreferredEditor;
this.schedulerId = options.schedulerId;
this.parentCallId = options.parentCallId;
this.onWaitingForConfirmation = options.onWaitingForConfirmation;
this.state = new SchedulerStateManager(
this.messageBus,
this.schedulerId,
(call) => logToolCall(this.config, new ToolCallEvent(call)),
);
this.executor = new ToolExecutor(this.config);
this.modifier = new ToolModificationHandler();
this.setupMessageBusListener(this.messageBus);
coreEvents.on(CoreEvent.McpProgress, this.handleMcpProgress);
}
dispose(): void {
coreEvents.off(CoreEvent.McpProgress, this.handleMcpProgress);
}
private readonly handleMcpProgress = (payload: McpProgressPayload) => {
const { callId } = payload;
const call = this.state.getToolCall(callId);
if (!call || call.status !== CoreToolCallStatus.Executing) {
return;
}
const validTotal =
payload.total !== undefined &&
Number.isFinite(payload.total) &&
payload.total > 0
? payload.total
: undefined;
this.state.updateStatus(callId, CoreToolCallStatus.Executing, {
progressMessage: payload.message,
progressPercent: validTotal
? Math.min(100, (payload.progress / validTotal) * 100)
: undefined,
progress: payload.progress,
progressTotal: validTotal,
});
};
private setupMessageBusListener(messageBus: MessageBus): void {
if (Scheduler.subscribedMessageBuses.has(messageBus)) {
return;
}
// TODO: Optimize policy checks. Currently, tools check policy via
// MessageBus even though the Scheduler already checked it.
messageBus.subscribe(
MessageBusType.TOOL_CONFIRMATION_REQUEST,
async (request: ToolConfirmationRequest) => {
await messageBus.publish({
type: MessageBusType.TOOL_CONFIRMATION_RESPONSE,
correlationId: request.correlationId,
confirmed: false,
requiresUserConfirmation: true,
});
},
);
Scheduler.subscribedMessageBuses.add(messageBus);
}
/**
* Schedules a batch of tool calls.
* @returns A promise that resolves with the results of the completed batch.
*/
async schedule(
request: ToolCallRequestInfo | ToolCallRequestInfo[],
signal: AbortSignal,
): Promise<CompletedToolCall[]> {
return runInDevTraceSpan(
{ operation: GeminiCliOperation.ScheduleToolCalls },
async ({ metadata: spanMetadata }) => {
const requests = Array.isArray(request) ? request : [request];
spanMetadata.input = requests;
let toolCallResponse: CompletedToolCall[] = [];
if (this.isProcessing || this.state.isActive) {
toolCallResponse = await this._enqueueRequest(requests, signal);
} else {
toolCallResponse = await this._startBatch(requests, signal);
}
spanMetadata.output = toolCallResponse;
return toolCallResponse;
},
);
}
private _enqueueRequest(
requests: ToolCallRequestInfo[],
signal: AbortSignal,
): Promise<CompletedToolCall[]> {
return new Promise<CompletedToolCall[]>((resolve, reject) => {
const abortHandler = () => {
const index = this.requestQueue.findIndex(
(item) => item.requests === requests,
);
if (index > -1) {
this.requestQueue.splice(index, 1);
reject(new Error('Tool call cancelled while in queue.'));
}
};
if (signal.aborted) {
reject(new Error('Operation cancelled'));
return;
}
signal.addEventListener('abort', abortHandler, { once: true });
this.requestQueue.push({
requests,
signal,
resolve: (results) => {
signal.removeEventListener('abort', abortHandler);
resolve(results);
},
reject: (err) => {
signal.removeEventListener('abort', abortHandler);
reject(err);
},
});
});
}
cancelAll(): void {
if (this.isCancelling) return;
this.isCancelling = true;
// Clear scheduler request queue
while (this.requestQueue.length > 0) {
const next = this.requestQueue.shift();
next?.reject(new Error('Operation cancelled by user'));
}
// Cancel active calls
const activeCalls = this.state.allActiveCalls;
for (const activeCall of activeCalls) {
if (!this.isTerminal(activeCall.status)) {
this.state.updateStatus(
activeCall.request.callId,
CoreToolCallStatus.Cancelled,
'Operation cancelled by user',
);
}
}
// Clear queue
this.state.cancelAllQueued('Operation cancelled by user');
}
get completedCalls(): CompletedToolCall[] {
return this.state.completedBatch;
}
private isTerminal(status: string) {
return (
status === CoreToolCallStatus.Success ||
status === CoreToolCallStatus.Error ||
status === CoreToolCallStatus.Cancelled
);
}
// --- Phase 1: Ingestion & Resolution ---
private async _startBatch(
requests: ToolCallRequestInfo[],
signal: AbortSignal,
): Promise<CompletedToolCall[]> {
this.isProcessing = true;
this.isCancelling = false;
this.state.clearBatch();
this.stepIndex = 0;
this.stepTotal = requests.length;
const currentApprovalMode = this.config.getApprovalMode();
try {
const toolRegistry = this.config.getToolRegistry();
const newCalls: ToolCall[] = requests.map((request) => {
const enrichedRequest: ToolCallRequestInfo = {
...request,
schedulerId: this.schedulerId,
parentCallId: this.parentCallId,
};
const tool = toolRegistry.getTool(request.name);
if (!tool) {
return {
...this._createToolNotFoundErroredToolCall(
enrichedRequest,
toolRegistry.getAllToolNames(),
),
approvalMode: currentApprovalMode,
};
}
return this._validateAndCreateToolCall(
enrichedRequest,
tool,
currentApprovalMode,
);
});
this.state.enqueue(newCalls);
await this._processQueue(signal);
return this.state.completedBatch;
} finally {
this.isProcessing = false;
this.state.clearBatch();
this._processNextInRequestQueue();
}
}
private _createToolNotFoundErroredToolCall(
request: ToolCallRequestInfo,
toolNames: string[],
): ErroredToolCall {
const suggestion = getToolSuggestion(request.name, toolNames);
return {
status: CoreToolCallStatus.Error,
request,
response: createErrorResponse(
request,
new Error(`Tool "${request.name}" not found.${suggestion}`),
ToolErrorType.TOOL_NOT_REGISTERED,
),
durationMs: 0,
schedulerId: this.schedulerId,
};
}
private _validateAndCreateToolCall(
request: ToolCallRequestInfo,
tool: AnyDeclarativeTool,
approvalMode: ApprovalMode,
): ValidatingToolCall | ErroredToolCall {
return runWithToolCallContext(
{
callId: request.callId,
schedulerId: this.schedulerId,
parentCallId: this.parentCallId,
},
() => {
try {
const invocation = tool.build(request.args);
return {
status: CoreToolCallStatus.Validating,
request,
tool,
invocation,
startTime: Date.now(),
schedulerId: this.schedulerId,
approvalMode,
};
} catch (e) {
return {
status: CoreToolCallStatus.Error,
request,
tool,
response: createErrorResponse(
request,
e instanceof Error ? e : new Error(String(e)),
ToolErrorType.INVALID_TOOL_PARAMS,
),
durationMs: 0,
schedulerId: this.schedulerId,
approvalMode,
};
}
},
);
}
// --- Phase 2: Processing Loop ---
private async _processQueue(signal: AbortSignal): Promise<void> {
while (this.state.queueLength > 0 || this.state.isActive) {
const shouldContinue = await this._processNextItem(signal);
if (!shouldContinue) break;
}
}
/**
* Processes the next item in the queue.
* @returns true if the loop should continue, false if it should terminate.
*/
private async _processNextItem(signal: AbortSignal): Promise<boolean> {
if (signal.aborted || this.isCancelling) {
this.state.cancelAllQueued('Operation cancelled');
return false;
}
const initialStatuses = new Map(
this.state.allActiveCalls.map((c) => [c.request.callId, c.status]),
);
if (!this.state.isActive) {
const next = this.state.dequeue();
if (!next) return false;
if (next.status === CoreToolCallStatus.Error) {
this.state.updateStatus(
next.request.callId,
CoreToolCallStatus.Error,
next.response,
);
this.state.finalizeCall(next.request.callId);
return true;
}
// If the first tool is parallelizable, batch all contiguous parallelizable tools.
if (this._isParallelizable(next.tool)) {
while (this.state.queueLength > 0) {
const peeked = this.state.peekQueue();
if (peeked && this._isParallelizable(peeked.tool)) {
this.state.dequeue();
} else {
break;
}
}
}
}
// Now we have one or more active calls. Move them through the lifecycle
// as much as possible in this iteration.
// 1. Process all 'validating' calls (Policy & Confirmation)
let activeCalls = this.state.allActiveCalls;
const validatingCalls = activeCalls.filter(
(c): c is ValidatingToolCall =>
c.status === CoreToolCallStatus.Validating,
);
if (validatingCalls.length > 0) {
await Promise.all(
validatingCalls.map((c) => this._processValidatingCall(c, signal)),
);
}
// 2. Execute scheduled calls
// Refresh activeCalls as status might have changed to 'scheduled'
activeCalls = this.state.allActiveCalls;
const scheduledCalls = activeCalls.filter(
(c): c is ScheduledToolCall => c.status === CoreToolCallStatus.Scheduled,
);
// We only execute if ALL active calls are in a ready state (scheduled or terminal)
const allReady = activeCalls.every(
(c) =>
c.status === CoreToolCallStatus.Scheduled || this.isTerminal(c.status),
);
let madeProgress = false;
if (allReady && scheduledCalls.length > 0) {
const execResults = await Promise.all(
scheduledCalls.map((c) => this._execute(c, signal)),
);
madeProgress = execResults.some((res) => res);
}
// 3. Finalize terminal calls
activeCalls = this.state.allActiveCalls;
for (const call of activeCalls) {
if (this.isTerminal(call.status)) {
this.state.finalizeCall(call.request.callId);
madeProgress = true;
}
}
// Check if any calls changed status during this iteration (excluding terminal finalization)
const currentStatuses = new Map(
activeCalls.map((c) => [c.request.callId, c.status]),
);
const anyStatusChanged = Array.from(initialStatuses.entries()).some(
([id, status]) => currentStatuses.get(id) !== status,
);
if (madeProgress || anyStatusChanged) {
return true;
}
// If we have active calls but NONE of them progressed, check if we are waiting for external events.
// States that are 'waiting' from the loop's perspective: awaiting_approval, executing.
const isWaitingForExternal = activeCalls.some(
(c) =>
c.status === CoreToolCallStatus.AwaitingApproval ||
c.status === CoreToolCallStatus.Executing,
);
if (isWaitingForExternal && this.state.isActive) {
// Yield to the event loop to allow external events (tool completion, user input) to progress.
await new Promise((resolve) => queueMicrotask(() => resolve(true)));
return true;
}
// If we are here, we have active calls (likely Validating or Scheduled) but none progressed.
// This is a stuck state.
return false;
}
private _isParallelizable(tool?: AnyDeclarativeTool): boolean {
if (!tool) return false;
return tool.isReadOnly || tool.kind === Kind.Agent;
}
private async _processValidatingCall(
active: ValidatingToolCall,
signal: AbortSignal,
): Promise<void> {
try {
await this._processToolCall(active, signal);
} catch (error) {
const err = error instanceof Error ? error : new Error(String(error));
// If the signal aborted while we were waiting on something, treat as
// cancelled. Otherwise, it's a genuine unhandled system exception.
if (signal.aborted || err.name === 'AbortError') {
this.state.updateStatus(
active.request.callId,
CoreToolCallStatus.Cancelled,
'Operation cancelled',
);
} else {
this.state.updateStatus(
active.request.callId,
CoreToolCallStatus.Error,
createErrorResponse(
active.request,
err,
ToolErrorType.UNHANDLED_EXCEPTION,
),
);
}
}
}
// --- Phase 3: Single Call Orchestration ---
private async _processToolCall(
toolCall: ValidatingToolCall,
signal: AbortSignal,
): Promise<void> {
const callId = toolCall.request.callId;
// Policy & Security
const { decision, rule } = await checkPolicy(toolCall, this.config);
if (decision === PolicyDecision.DENY) {
const { errorMessage, errorType } = getPolicyDenialError(
this.config,
rule,
);
this.state.updateStatus(
callId,
CoreToolCallStatus.Error,
createErrorResponse(
toolCall.request,
new Error(errorMessage),
errorType,
),
);
return;
}
// User Confirmation Loop
let outcome = ToolConfirmationOutcome.ProceedOnce;
let lastDetails: SerializableConfirmationDetails | undefined;
if (decision === PolicyDecision.ASK_USER) {
const result = await resolveConfirmation(toolCall, signal, {
config: this.config,
messageBus: this.messageBus,
state: this.state,
modifier: this.modifier,
getPreferredEditor: this.getPreferredEditor,
schedulerId: this.schedulerId,
onWaitingForConfirmation: this.onWaitingForConfirmation,
});
outcome = result.outcome;
lastDetails = result.lastDetails;
}
this.state.setOutcome(callId, outcome);
// Handle Policy Updates
if (decision === PolicyDecision.ASK_USER && outcome) {
await updatePolicy(toolCall.tool, outcome, lastDetails, {
config: this.config,
messageBus: this.messageBus,
});
}
// Handle cancellation (cascades to entire batch)
if (outcome === ToolConfirmationOutcome.Cancel) {
this.state.updateStatus(
callId,
CoreToolCallStatus.Cancelled,
'User denied execution.',
);
this.state.cancelAllQueued('User cancelled operation');
return; // Skip execution
}
this.state.updateStatus(callId, CoreToolCallStatus.Scheduled);
}
// --- Sub-phase Handlers ---
/**
* Executes the tool and records the result. Returns true if a new tool call was added.
*/
private async _execute(
toolCall: ScheduledToolCall,
signal: AbortSignal,
): Promise<boolean> {
const callId = toolCall.request.callId;
if (signal.aborted) {
this.state.updateStatus(
callId,
CoreToolCallStatus.Cancelled,
'Operation cancelled',
);
return false;
}
// Step-through mode: pause before every tool call and await user approval.
if (this.config.getApprovalMode() === ApprovalMode.STEP) {
this.stepIndex += 1;
const action = await pauseForStepThrough(
toolCall,
signal,
this.messageBus,
this.stepIndex,
Math.max(this.stepTotal, this.stepIndex),
);
switch (action) {
case 'skip': {
// Return an empty successful result without executing the tool.
this.state.updateStatus(callId, CoreToolCallStatus.Success, {
callId,
responseParts: [
{
functionResponse: {
id: callId,
name: toolCall.request.name,
response: {
output: '(skipped by user in step-through mode)',
},
},
},
],
resultDisplay: '(skipped)',
error: undefined,
errorType: undefined,
});
return false;
}
case 'cancel': {
this.state.updateStatus(
callId,
CoreToolCallStatus.Cancelled,
'Step-through cancelled by user.',
);
this.state.cancelAllQueued('Step-through cancelled by user');
return false;
}
case 'continue': {
// Exit step-through mode and resume normal execution for this and all
// subsequent tool calls.
this.config.setApprovalMode(ApprovalMode.DEFAULT);
break;
}
case 'next':
default:
checkExhaustive(action);
}
}
this.state.updateStatus(callId, CoreToolCallStatus.Executing);
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
const activeCall = this.state.getToolCall(callId) as ExecutingToolCall;
const result = await runWithToolCallContext(
{
callId: activeCall.request.callId,
schedulerId: this.schedulerId,
parentCallId: this.parentCallId,
},
() =>
this.executor.execute({
call: activeCall,
signal,
outputUpdateHandler: (id, out) =>
this.state.updateStatus(id, CoreToolCallStatus.Executing, {
liveOutput: out,
}),
onUpdateToolCall: (updated) => {
if (
updated.status === CoreToolCallStatus.Executing &&
updated.pid
) {
this.state.updateStatus(callId, CoreToolCallStatus.Executing, {
pid: updated.pid,
});
}
},
}),
);
if (
(result.status === CoreToolCallStatus.Success ||
result.status === CoreToolCallStatus.Error) &&
result.tailToolCallRequest
) {
// Log the intermediate tool call before it gets replaced.
const intermediateCall: SuccessfulToolCall | ErroredToolCall = {
request: activeCall.request,
tool: activeCall.tool,
invocation: activeCall.invocation,
status: result.status,
response: result.response,
durationMs: activeCall.startTime
? Date.now() - activeCall.startTime
: undefined,
outcome: activeCall.outcome,
schedulerId: this.schedulerId,
};
logToolCall(this.config, new ToolCallEvent(intermediateCall));
const tailRequest = result.tailToolCallRequest;
const originalCallId = result.request.callId;
const originalRequestName =
result.request.originalRequestName || result.request.name;
const newTool = this.config.getToolRegistry().getTool(tailRequest.name);
const newRequest: ToolCallRequestInfo = {
callId: originalCallId,
name: tailRequest.name,
args: tailRequest.args,
originalRequestName,
isClientInitiated: result.request.isClientInitiated,
prompt_id: result.request.prompt_id,
schedulerId: this.schedulerId,
};
if (!newTool) {
// Enqueue an errored tool call
const errorCall = this._createToolNotFoundErroredToolCall(
newRequest,
this.config.getToolRegistry().getAllToolNames(),
);
this.state.replaceActiveCallWithTailCall(callId, errorCall);
} else {
// Enqueue a validating tool call for the new tail tool
const validatingCall = this._validateAndCreateToolCall(
newRequest,
newTool,
activeCall.approvalMode ?? this.config.getApprovalMode(),
);
this.state.replaceActiveCallWithTailCall(callId, validatingCall);
}
// Loop continues, picking up the new tail call at the front of the queue.
this.stepTotal += 1;
return true;
}
if (result.status === CoreToolCallStatus.Success) {
this.state.updateStatus(
callId,
CoreToolCallStatus.Success,
result.response,
);
} else if (result.status === CoreToolCallStatus.Cancelled) {
this.state.updateStatus(
callId,
CoreToolCallStatus.Cancelled,
result.response,
);
} else {
this.state.updateStatus(
callId,
CoreToolCallStatus.Error,
result.response,
);
}
return false;
}
private _processNextInRequestQueue() {
if (this.requestQueue.length > 0) {
const next = this.requestQueue.shift()!;
this.schedule(next.requests, next.signal)
.then(next.resolve)
.catch(next.reject);
}
}
}