@sprucelabs/sprucebot-llm 13.0.4 → 13.0.6

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.
@@ -5,11 +5,16 @@ import { LlmAdapter, SendMessageOptions, SprucebotLlmBot } from '../../llm.types
5
5
  export default class OpenAiAdapter implements LlmAdapter {
6
6
  static Class: new (apiKey: string, options?: OpenAiAdapterOptions) => OpenAiAdapter;
7
7
  static OpenAI: typeof OpenAI;
8
+ static AbortController: {
9
+ new (): AbortController;
10
+ prototype: AbortController;
11
+ };
8
12
  private api;
9
13
  private log?;
10
14
  private model;
11
15
  private memoryLimit?;
12
16
  private reasoningEffort?;
17
+ private lastAbortController?;
13
18
  protected constructor(apiKey: string, options?: OpenAiAdapterOptions);
14
19
  static Adapter(apiKey: string, options?: OpenAiAdapterOptions): OpenAiAdapter;
15
20
  sendMessage(bot: SprucebotLlmBot, options?: SendMessageOptions): Promise<string>;
@@ -36,7 +36,12 @@ class OpenAiAdapter {
36
36
  if (reasoningEffort) {
37
37
  params.reasoning_effort = reasoningEffort;
38
38
  }
39
- const response = await this.api.chat.completions.create(params);
39
+ this.lastAbortController?.abort('Interrupted by new message');
40
+ this.lastAbortController = new OpenAiAdapter.AbortController();
41
+ const response = await this.api.chat.completions.create(params, {
42
+ signal: this.lastAbortController.signal,
43
+ });
44
+ delete this.lastAbortController;
40
45
  const message = response.choices?.[0]?.message?.content?.trim() ??
41
46
  exports.MESSAGE_RESPONSE_ERROR_MESSAGE;
42
47
  this.log?.info('Received response from OpenAI', message);
@@ -56,5 +61,6 @@ class OpenAiAdapter {
56
61
  }
57
62
  }
58
63
  OpenAiAdapter.OpenAI = openai_1.default;
64
+ OpenAiAdapter.AbortController = AbortController;
59
65
  exports.default = OpenAiAdapter;
60
66
  exports.MESSAGE_RESPONSE_ERROR_MESSAGE = "Oh no! Something went wrong and I can't talk right now!";
@@ -1,13 +1,15 @@
1
1
  import OpenAI, { ClientOptions } from 'openai';
2
+ import { RequestOptions } from 'openai/internal/request-options';
2
3
  import { ChatCompletion, ChatCompletionCreateParamsNonStreaming } from 'openai/resources';
3
4
  export default class SpyOpenAiModule extends OpenAI {
4
5
  static config?: ClientOptions;
5
6
  static lastSentCompletion?: ChatCompletionCreateParamsNonStreaming;
6
7
  static responseMessage: string | false;
8
+ static lastCompletionOptions?: RequestOptions | undefined;
7
9
  constructor(config: ClientOptions);
8
10
  chat: {
9
11
  completions: {
10
- create: (options: ChatCompletionCreateParamsNonStreaming) => Promise<Response>;
12
+ create: (completion: ChatCompletionCreateParamsNonStreaming, options?: RequestOptions) => Promise<Response>;
11
13
  };
12
14
  };
13
15
  private createCompletion;
@@ -15,8 +15,9 @@ class SpyOpenAiModule extends openai_1.default {
15
15
  };
16
16
  SpyOpenAiModule.config = config;
17
17
  }
18
- async createCompletion(options) {
19
- SpyOpenAiModule.lastSentCompletion = options;
18
+ async createCompletion(completion, options) {
19
+ SpyOpenAiModule.lastSentCompletion = completion;
20
+ SpyOpenAiModule.lastCompletionOptions = options;
20
21
  const choices = [];
21
22
  if (SpyOpenAiModule.responseMessage) {
22
23
  choices.push({
@@ -5,11 +5,16 @@ import { LlmAdapter, SendMessageOptions, SprucebotLlmBot } from '../../llm.types
5
5
  export default class OpenAiAdapter implements LlmAdapter {
6
6
  static Class: new (apiKey: string, options?: OpenAiAdapterOptions) => OpenAiAdapter;
7
7
  static OpenAI: typeof OpenAI;
8
+ static AbortController: {
9
+ new (): AbortController;
10
+ prototype: AbortController;
11
+ };
8
12
  private api;
9
13
  private log?;
10
14
  private model;
11
15
  private memoryLimit?;
12
16
  private reasoningEffort?;
17
+ private lastAbortController?;
13
18
  protected constructor(apiKey: string, options?: OpenAiAdapterOptions);
14
19
  static Adapter(apiKey: string, options?: OpenAiAdapterOptions): OpenAiAdapter;
15
20
  sendMessage(bot: SprucebotLlmBot, options?: SendMessageOptions): Promise<string>;
@@ -27,7 +27,7 @@ class OpenAiAdapter {
27
27
  }
28
28
  sendMessage(bot, options) {
29
29
  return __awaiter(this, void 0, void 0, function* () {
30
- var _a, _b, _c, _d, _e, _f, _g;
30
+ var _a, _b, _c, _d, _e, _f, _g, _h;
31
31
  const messageBuilder = OpenAiMessageBuilder.Builder(bot, {
32
32
  memoryLimit: this.memoryLimit,
33
33
  });
@@ -38,9 +38,14 @@ class OpenAiAdapter {
38
38
  if (reasoningEffort) {
39
39
  params.reasoning_effort = reasoningEffort;
40
40
  }
41
- const response = yield this.api.chat.completions.create(params);
42
- const message = (_f = (_e = (_d = (_c = (_b = response.choices) === null || _b === void 0 ? void 0 : _b[0]) === null || _c === void 0 ? void 0 : _c.message) === null || _d === void 0 ? void 0 : _d.content) === null || _e === void 0 ? void 0 : _e.trim()) !== null && _f !== void 0 ? _f : MESSAGE_RESPONSE_ERROR_MESSAGE;
43
- (_g = this.log) === null || _g === void 0 ? void 0 : _g.info('Received response from OpenAI', message);
41
+ (_b = this.lastAbortController) === null || _b === void 0 ? void 0 : _b.abort('Interrupted by new message');
42
+ this.lastAbortController = new OpenAiAdapter.AbortController();
43
+ const response = yield this.api.chat.completions.create(params, {
44
+ signal: this.lastAbortController.signal,
45
+ });
46
+ delete this.lastAbortController;
47
+ const message = (_g = (_f = (_e = (_d = (_c = response.choices) === null || _c === void 0 ? void 0 : _c[0]) === null || _d === void 0 ? void 0 : _d.message) === null || _e === void 0 ? void 0 : _e.content) === null || _f === void 0 ? void 0 : _f.trim()) !== null && _g !== void 0 ? _g : MESSAGE_RESPONSE_ERROR_MESSAGE;
48
+ (_h = this.log) === null || _h === void 0 ? void 0 : _h.info('Received response from OpenAI', message);
44
49
  return message;
45
50
  });
46
51
  }
@@ -59,5 +64,6 @@ class OpenAiAdapter {
59
64
  }
60
65
  }
61
66
  OpenAiAdapter.OpenAI = OpenAI;
67
+ OpenAiAdapter.AbortController = AbortController;
62
68
  export default OpenAiAdapter;
63
69
  export const MESSAGE_RESPONSE_ERROR_MESSAGE = "Oh no! Something went wrong and I can't talk right now!";
@@ -1,13 +1,15 @@
1
1
  import OpenAI, { ClientOptions } from 'openai';
2
+ import { RequestOptions } from 'openai/internal/request-options';
2
3
  import { ChatCompletion, ChatCompletionCreateParamsNonStreaming } from 'openai/resources';
3
4
  export default class SpyOpenAiModule extends OpenAI {
4
5
  static config?: ClientOptions;
5
6
  static lastSentCompletion?: ChatCompletionCreateParamsNonStreaming;
6
7
  static responseMessage: string | false;
8
+ static lastCompletionOptions?: RequestOptions | undefined;
7
9
  constructor(config: ClientOptions);
8
10
  chat: {
9
11
  completions: {
10
- create: (options: ChatCompletionCreateParamsNonStreaming) => Promise<Response>;
12
+ create: (completion: ChatCompletionCreateParamsNonStreaming, options?: RequestOptions) => Promise<Response>;
11
13
  };
12
14
  };
13
15
  private createCompletion;
@@ -19,9 +19,10 @@ class SpyOpenAiModule extends OpenAI {
19
19
  };
20
20
  SpyOpenAiModule.config = config;
21
21
  }
22
- createCompletion(options) {
22
+ createCompletion(completion, options) {
23
23
  return __awaiter(this, void 0, void 0, function* () {
24
- SpyOpenAiModule.lastSentCompletion = options;
24
+ SpyOpenAiModule.lastSentCompletion = completion;
25
+ SpyOpenAiModule.lastCompletionOptions = options;
25
26
  const choices = [];
26
27
  if (SpyOpenAiModule.responseMessage) {
27
28
  choices.push({
@@ -76,6 +76,8 @@ export type LlmCallbackParameter = {
76
76
  type: ('text' | 'number' | 'boolean' | 'dateMs' | 'dateTimeMs') | (string & {});
77
77
  isRequired?: boolean;
78
78
  description?: string;
79
+ isArray?: boolean;
80
+ minArrayLength?: number;
79
81
  } | (SelectFieldDefinition & {
80
82
  name: string;
81
83
  description?: string;
@@ -76,6 +76,8 @@ export type LlmCallbackParameter = {
76
76
  type: ('text' | 'number' | 'boolean' | 'dateMs' | 'dateTimeMs') | (string & {});
77
77
  isRequired?: boolean;
78
78
  description?: string;
79
+ isArray?: boolean;
80
+ minArrayLength?: number;
79
81
  } | (SelectFieldDefinition & {
80
82
  name: string;
81
83
  description?: string;
package/package.json CHANGED
@@ -8,7 +8,7 @@
8
8
  "eta"
9
9
  ]
10
10
  },
11
- "version": "13.0.4",
11
+ "version": "13.0.6",
12
12
  "files": [
13
13
  "build"
14
14
  ],