Skip to content

Commit 9d02617

Browse files
committed
feat(task): pass abort signal to condense metadata
1 parent 60a0dcf commit 9d02617

2 files changed

Lines changed: 458 additions & 6 deletions

File tree

src/core/task/Task.ts

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1555,6 +1555,11 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
15551555
const metadata: ApiHandlerCreateMessageMetadata = {
15561556
mode,
15571557
taskId: this.taskId,
1558+
...(this.currentRequestAbortController?.signal
1559+
? {
1560+
abortSignal: this.currentRequestAbortController.signal,
1561+
}
1562+
: {}),
15581563
...(allTools.length > 0
15591564
? {
15601565
tools: allTools,
@@ -3763,6 +3768,11 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
37633768
const metadata: ApiHandlerCreateMessageMetadata = {
37643769
mode,
37653770
taskId: this.taskId,
3771+
...(this.currentRequestAbortController?.signal
3772+
? {
3773+
abortSignal: this.currentRequestAbortController.signal,
3774+
}
3775+
: {}),
37663776
...(allTools.length > 0
37673777
? {
37683778
tools: allTools,
@@ -3979,6 +3989,11 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
39793989
const contextMgmtMetadata: ApiHandlerCreateMessageMetadata = {
39803990
mode,
39813991
taskId: this.taskId,
3992+
...(this.currentRequestAbortController?.signal
3993+
? {
3994+
abortSignal: this.currentRequestAbortController.signal,
3995+
}
3996+
: {}),
39823997
...(contextMgmtTools.length > 0
39833998
? {
39843999
tools: contextMgmtTools,
@@ -4141,10 +4156,15 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
41414156

41424157
const shouldIncludeTools = allTools.length > 0
41434158

4159+
// Create an AbortController to allow cancelling the request mid-stream
4160+
this.currentRequestAbortController = new AbortController()
4161+
const abortSignal = this.currentRequestAbortController.signal
4162+
41444163
const metadata: ApiHandlerCreateMessageMetadata = {
41454164
mode: mode,
41464165
taskId: this.taskId,
41474166
suppressPreviousResponseId: this.skipPrevResponseIdOnce,
4167+
abortSignal,
41484168
// Include tools whenever they are present.
41494169
...(shouldIncludeTools
41504170
? {
@@ -4157,11 +4177,6 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
41574177
}
41584178
: {}),
41594179
}
4160-
4161-
// Create an AbortController to allow cancelling the request mid-stream
4162-
this.currentRequestAbortController = new AbortController()
4163-
const abortSignal = this.currentRequestAbortController.signal
4164-
metadata.abortSignal = abortSignal
41654180
// Reset the flag after using it
41664181
this.skipPrevResponseIdOnce = false
41674182

@@ -4200,9 +4215,12 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
42004215
this.isWaitingForFirstChunk = false
42014216
} catch (error) {
42024217
this.isWaitingForFirstChunk = false
4203-
this.currentRequestAbortController = undefined
42044218
const isContextWindowExceededError = checkContextWindowExceededError(error)
42054219

4220+
if (!isContextWindowExceededError) {
4221+
this.currentRequestAbortController = undefined
4222+
}
4223+
42064224
// If it's a context window error and we haven't exceeded max retries for this error type
42074225
if (isContextWindowExceededError && retryAttempt < MAX_CONTEXT_WINDOW_RETRIES) {
42084226
console.warn(

0 commit comments

Comments
 (0)