@cloudbase/agent-agents 0.0.6 → 0.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -1873,6 +1873,11 @@ var AgentExecutor = class {
1873
1873
  const customHandler = this.config.controlFlow?.customHandler;
1874
1874
  while (maxIterations === void 0 || iteration < maxIterations) {
1875
1875
  iteration++;
1876
+ if (options?.signal?.aborted) {
1877
+ throw new Error(
1878
+ `Run aborted: ${options.signal.reason || "AbortSignal was aborted"}`
1879
+ );
1880
+ }
1876
1881
  const controlResult = await this.checkControlFlow(
1877
1882
  customHandler,
1878
1883
  context,
@@ -1937,7 +1942,8 @@ var AgentExecutor = class {
1937
1942
  );
1938
1943
  const streamResult = await this.processStreamingResponse(
1939
1944
  llmResponse,
1940
- eventEmitter
1945
+ eventEmitter,
1946
+ options
1941
1947
  );
1942
1948
  const assistantMessage = await this.saveAssistantMessage(
1943
1949
  streamResult.content,
@@ -2309,6 +2315,9 @@ var AgentExecutor = class {
2309
2315
  requestParams.tools = toolDefinitions;
2310
2316
  requestParams.tool_choice = "auto";
2311
2317
  }
2318
+ if (options?.signal) {
2319
+ requestParams.signal = options.signal;
2320
+ }
2312
2321
  return this.modelProvider.chat.completions.stream(requestParams);
2313
2322
  }
2314
2323
  /**
@@ -4100,6 +4109,17 @@ var Agent = class {
4100
4109
  }
4101
4110
  }
4102
4111
  };
4112
+ if (options?.signal?.aborted) {
4113
+ throw new ExecutionError(
4114
+ "Run aborted before start",
4115
+ { reason: options.signal.reason || "AbortSignal was already aborted" }
4116
+ );
4117
+ }
4118
+ const abortHandler = () => {
4119
+ this.isRunning = false;
4120
+ this.state.status = "idle";
4121
+ };
4122
+ options?.signal?.addEventListener("abort", abortHandler);
4103
4123
  eventEmitter.emit("RUN_STARTED" /* RUN_STARTED */, {
4104
4124
  type: "RUN_STARTED" /* RUN_STARTED */,
4105
4125
  threadId: conversationId,
@@ -4113,6 +4133,12 @@ var Agent = class {
4113
4133
  this.state.status = "running";
4114
4134
  this.state.runId = runId;
4115
4135
  this.state.conversationId = conversationId;
4136
+ if (options?.signal?.aborted) {
4137
+ throw new ExecutionError(
4138
+ "Run aborted",
4139
+ { reason: options.signal.reason || "AbortSignal was aborted" }
4140
+ );
4141
+ }
4116
4142
  if (options?.resume) {
4117
4143
  try {
4118
4144
  const resumePayload = JSON.parse(options.resume.payload);
@@ -4132,12 +4158,24 @@ var Agent = class {
4132
4158
  state: this.state
4133
4159
  });
4134
4160
  const context = await this.createRunContext(input, state, options);
4161
+ if (options?.signal?.aborted) {
4162
+ throw new ExecutionError(
4163
+ "Run aborted",
4164
+ { reason: options.signal.reason || "AbortSignal was aborted" }
4165
+ );
4166
+ }
4135
4167
  const result = await this.executeAgentCore(
4136
4168
  eventEmitter,
4137
4169
  context,
4138
4170
  validatedOptions,
4139
4171
  true
4140
4172
  );
4173
+ if (options?.signal?.aborted) {
4174
+ throw new ExecutionError(
4175
+ "Run aborted",
4176
+ { reason: options.signal.reason || "AbortSignal was aborted" }
4177
+ );
4178
+ }
4141
4179
  if (result && result.metadata && result.metadata.outcome !== "interrupt") {
4142
4180
  eventEmitter.emit("RUN_FINISHED" /* RUN_FINISHED */, {
4143
4181
  type: "RUN_FINISHED" /* RUN_FINISHED */,
@@ -4166,6 +4204,10 @@ var Agent = class {
4166
4204
  this.isRunning = false;
4167
4205
  this.state.status = "error";
4168
4206
  throw agentError;
4207
+ } finally {
4208
+ if (options?.signal) {
4209
+ options.signal.removeEventListener("abort", abortHandler);
4210
+ }
4169
4211
  }
4170
4212
  }
4171
4213
  // Streaming run method - Returns AsyncIterator (no RxJS dependency)
@@ -4977,8 +5019,12 @@ var OpenAIProvider = class extends BaseModelProvider {
4977
5019
  async createCompletion(params) {
4978
5020
  try {
4979
5021
  const openaiParams = this.convertToOpenAIParams(params);
5022
+ const requestOptions = {};
5023
+ if (params.signal) {
5024
+ requestOptions.signal = params.signal;
5025
+ }
4980
5026
  const response = await this.withRetry(async () => {
4981
- return await this.client.chat.completions.create(openaiParams);
5027
+ return await this.client.chat.completions.create(openaiParams, requestOptions);
4982
5028
  });
4983
5029
  return this.convertFromOpenAIResponse(response);
4984
5030
  } catch (error) {
@@ -4992,10 +5038,14 @@ var OpenAIProvider = class extends BaseModelProvider {
4992
5038
  async *createStream(params) {
4993
5039
  try {
4994
5040
  const openaiParams = this.convertToOpenAIParams(params);
5041
+ const requestOptions = {};
5042
+ if (params.signal) {
5043
+ requestOptions.signal = params.signal;
5044
+ }
4995
5045
  const stream = await this.client.chat.completions.create({
4996
5046
  ...openaiParams,
4997
5047
  stream: true
4998
- });
5048
+ }, requestOptions);
4999
5049
  if (Symbol.asyncIterator in stream) {
5000
5050
  for await (const chunk of stream) {
5001
5051
  yield this.convertFromOpenAIChunk(chunk);
@@ -5062,6 +5112,9 @@ var OpenAIProvider = class extends BaseModelProvider {
5062
5112
  // Handle OpenAI-specific errors
5063
5113
  handleOpenAIError(error) {
5064
5114
  if (error instanceof Error) {
5115
+ if (error.name === "AbortError" || error.message.includes("abort")) {
5116
+ return this.createError("Request aborted", "aborted", error);
5117
+ }
5065
5118
  if (error.message.includes("401") || error.message.includes("Unauthorized")) {
5066
5119
  return this.createError("Invalid API key", "authentication", error);
5067
5120
  }
@@ -5223,8 +5276,12 @@ var AnthropicProvider = class extends BaseModelProvider {
5223
5276
  async createCompletion(params) {
5224
5277
  try {
5225
5278
  const anthropicParams = this.convertToAnthropicParams(params);
5279
+ const requestOptions = {};
5280
+ if (params.signal) {
5281
+ requestOptions.signal = params.signal;
5282
+ }
5226
5283
  const response = await this.withRetry(async () => {
5227
- return await this.client.messages.create(anthropicParams);
5284
+ return await this.client.messages.create(anthropicParams, requestOptions);
5228
5285
  });
5229
5286
  return this.convertFromAnthropicResponse(response, params.model);
5230
5287
  } catch (error) {
@@ -5239,7 +5296,11 @@ var AnthropicProvider = class extends BaseModelProvider {
5239
5296
  try {
5240
5297
  const anthropicParams = this.convertToAnthropicParams(params);
5241
5298
  anthropicParams.stream = true;
5242
- const stream = await this.client.messages.create(anthropicParams);
5299
+ const requestOptions = {};
5300
+ if (params.signal) {
5301
+ requestOptions.signal = params.signal;
5302
+ }
5303
+ const stream = await this.client.messages.create(anthropicParams, requestOptions);
5243
5304
  if (Symbol.asyncIterator in stream) {
5244
5305
  for await (const chunk of stream) {
5245
5306
  yield this.convertFromAnthropicChunk(chunk, params.model);
@@ -5345,6 +5406,9 @@ var AnthropicProvider = class extends BaseModelProvider {
5345
5406
  // Handle Anthropic-specific errors
5346
5407
  handleAnthropicError(error) {
5347
5408
  if (error instanceof Error) {
5409
+ if (error.name === "AbortError" || error.message.includes("abort")) {
5410
+ return this.createError("Request aborted", "aborted", error);
5411
+ }
5348
5412
  if (error.message.includes("401") || error.message.includes("Unauthorized")) {
5349
5413
  return this.createError("Invalid API key", "authentication", error);
5350
5414
  }