@sprucelabs/sprucebot-llm 5.0.556 → 5.0.558

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.
@@ -6,18 +6,16 @@ const eta_1 = require("eta");
6
6
  const templates_1 = require("./templates");
7
7
  class PromptGenerator {
8
8
  constructor(bot, options) {
9
- var _a;
10
9
  this.eta = new eta_1.Eta();
11
10
  this.log = process.env.SHOULD_LOG_GENERATED_PROMPTS === 'true'
12
11
  ? console.info
13
12
  : () => { };
14
13
  (0, schema_1.assertOptions)({ bot }, ['bot']);
15
14
  this.bot = bot;
16
- this.promptTemplate = (_a = options === null || options === void 0 ? void 0 : options.promptTemplate) !== null && _a !== void 0 ? _a : templates_1.PROMPT_TEMPLATE;
15
+ this.promptTemplate = options?.promptTemplate ?? templates_1.PROMPT_TEMPLATE;
17
16
  }
18
17
  static Generator(bot, options) {
19
- var _a;
20
- return new ((_a = this.Class) !== null && _a !== void 0 ? _a : PromptGenerator)(bot, options);
18
+ return new (this.Class ?? PromptGenerator)(bot, options);
21
19
  }
22
20
  async generate() {
23
21
  const { stateSchema, state, ...rest } = this.bot.serialize();
@@ -37,7 +35,7 @@ class PromptGenerator {
37
35
  stateJson: null,
38
36
  };
39
37
  }
40
- const normalizedState = (0, schema_1.normalizeSchemaValues)(stateSchema, state !== null && state !== void 0 ? state : {}, {});
38
+ const normalizedState = (0, schema_1.normalizeSchemaValues)(stateSchema, state ?? {}, {});
41
39
  setUndefinedToNull(normalizedState);
42
40
  const stateSchemaJson = JSON.stringify(stateSchema);
43
41
  const stateJson = JSON.stringify(normalizedState);
@@ -34,36 +34,34 @@ class SprucebotLlmBotImpl extends mercury_event_emitter_1.AbstractEventEmitter {
34
34
  return this.isDone;
35
35
  }
36
36
  serialize() {
37
- var _a, _b, _c;
38
- const skill = (_a = this.skill) === null || _a === void 0 ? void 0 : _a.serialize();
37
+ const skill = this.skill?.serialize();
39
38
  return {
40
39
  youAre: this.youAre,
41
- stateSchema: (_b = this.stateSchema) !== null && _b !== void 0 ? _b : skill === null || skill === void 0 ? void 0 : skill.stateSchema,
42
- state: (_c = this.state) !== null && _c !== void 0 ? _c : skill === null || skill === void 0 ? void 0 : skill.state,
40
+ stateSchema: this.stateSchema ?? skill?.stateSchema,
41
+ state: this.state ?? skill?.state,
43
42
  messages: this.messages,
44
43
  skill,
45
44
  };
46
45
  }
47
46
  async sendMessage(message) {
48
- var _a, _b;
49
47
  (0, schema_1.assertOptions)({ message }, ['message']);
50
48
  this.trackMessage({
51
49
  from: 'Me',
52
50
  message,
53
51
  });
54
- const serializedSkill = (_a = this.skill) === null || _a === void 0 ? void 0 : _a.serialize();
52
+ const serializedSkill = this.skill?.serialize();
55
53
  const response = await this.adapter.sendMessage(this, {
56
- model: serializedSkill === null || serializedSkill === void 0 ? void 0 : serializedSkill.model,
57
- promptTemplate: serializedSkill === null || serializedSkill === void 0 ? void 0 : serializedSkill.promptTemplate,
54
+ model: serializedSkill?.model,
55
+ promptTemplate: serializedSkill?.promptTemplate,
58
56
  });
59
57
  const parser = ResponseParser_1.default.getInstance();
60
- const { isDone, message: parsedResponse, state, } = await parser.parse(response, serializedSkill === null || serializedSkill === void 0 ? void 0 : serializedSkill.callbacks);
58
+ const { isDone, message: parsedResponse, state, } = await parser.parse(response, serializedSkill?.callbacks);
61
59
  this.isDone = isDone;
62
60
  if (this.stateSchema && state) {
63
61
  await this.updateState(state);
64
62
  }
65
63
  else if (state) {
66
- await ((_b = this.skill) === null || _b === void 0 ? void 0 : _b.updateState(state));
64
+ await this.skill?.updateState(state);
67
65
  }
68
66
  this.trackMessage({
69
67
  from: 'You',
@@ -9,15 +9,15 @@ const SprucebotLlmBotImpl_1 = __importDefault(require("./SprucebotLlmBotImpl"));
9
9
  const SprucebotLlmSkillImpl_1 = __importDefault(require("./SprucebotLlmSkillImpl"));
10
10
  class SprucebotLlmFactory {
11
11
  Bot(options) {
12
- var _a;
13
12
  (0, schema_1.assertOptions)(options, ['youAre', 'adapter']);
14
13
  const { Class } = options;
15
- return new ((_a = Class !== null && Class !== void 0 ? Class : SprucebotLlmFactory.BotClass) !== null && _a !== void 0 ? _a : SprucebotLlmBotImpl_1.default)(options);
14
+ return new (Class ??
15
+ SprucebotLlmFactory.BotClass ??
16
+ SprucebotLlmBotImpl_1.default)(options);
16
17
  }
17
18
  Skill(options) {
18
- var _a;
19
19
  (0, schema_1.assertOptions)(options, ['yourJobIfYouChooseToAcceptItIs']);
20
- return new ((_a = SprucebotLlmFactory.SkillClass) !== null && _a !== void 0 ? _a : SprucebotLlmSkillImpl_1.default)(options);
20
+ return new (SprucebotLlmFactory.SkillClass ?? SprucebotLlmSkillImpl_1.default)(options);
21
21
  }
22
22
  getBotInstance() {
23
23
  if (!this.instance) {
@@ -31,8 +31,7 @@ class SprucebotLlmFactory {
31
31
  this.instance = bot;
32
32
  }
33
33
  static Factory() {
34
- var _a;
35
- return new ((_a = this.FactoryClass) !== null && _a !== void 0 ? _a : this)();
34
+ return new (this.FactoryClass ?? this)();
36
35
  }
37
36
  static reset() {
38
37
  this.BotClass = undefined;
@@ -14,18 +14,18 @@ class OpenAiAdapter {
14
14
  this.api = new OpenAiAdapter.OpenAIApi(config);
15
15
  }
16
16
  async sendMessage(bot, options) {
17
- var _a, _b, _c, _d;
18
17
  const generator = PromptGenerator_1.default.Generator(bot, {
19
- promptTemplate: options === null || options === void 0 ? void 0 : options.promptTemplate,
18
+ promptTemplate: options?.promptTemplate,
20
19
  });
21
20
  const prompt = await generator.generate();
22
21
  const response = await this.api.createCompletion({
23
22
  prompt,
24
- model: (_a = options === null || options === void 0 ? void 0 : options.model) !== null && _a !== void 0 ? _a : 'text-davinci-003',
23
+ model: options?.model ?? 'text-davinci-003',
25
24
  max_tokens: 250,
26
25
  stop: ['__Me__:'],
27
26
  });
28
- return ((_d = (_c = (_b = response.data.choices[0]) === null || _b === void 0 ? void 0 : _b.text) === null || _c === void 0 ? void 0 : _c.trim()) !== null && _d !== void 0 ? _d : exports.MESSAGE_RESPONSE_ERROR_MESSAGE);
27
+ return (response.data.choices[0]?.text?.trim() ??
28
+ exports.MESSAGE_RESPONSE_ERROR_MESSAGE);
29
29
  }
30
30
  }
31
31
  exports.OpenAiAdapter = OpenAiAdapter;
@@ -9,7 +9,7 @@ class SpruceError extends error_1.default {
9
9
  friendlyMessage() {
10
10
  const { options } = this;
11
11
  let message;
12
- switch (options === null || options === void 0 ? void 0 : options.code) {
12
+ switch (options?.code) {
13
13
  case 'NO_BOT_INSTANCE_SET':
14
14
  message = `You must create a bot and set it using 'SprucebotLlmFactory.setInstance(bot)' before you can get an instance of it.`;
15
15
  break;
@@ -33,9 +33,8 @@ class ResponseParser {
33
33
  };
34
34
  }
35
35
  async invokeCallback(callbacks, key, message) {
36
- var _a;
37
- const v = await ((_a = callbacks === null || callbacks === void 0 ? void 0 : callbacks[key]) === null || _a === void 0 ? void 0 : _a.cb());
38
- message = message.replace((0, renderPlaceholder_1.default)(key), v !== null && v !== void 0 ? v : '').trim();
36
+ const v = await callbacks?.[key]?.cb();
37
+ message = message.replace((0, renderPlaceholder_1.default)(key), v ?? '').trim();
39
38
  return message;
40
39
  }
41
40
  doesIncludeDoneToken(response) {
@@ -45,8 +44,8 @@ class ResponseParser {
45
44
  const ESCAPED_BOUNDARY = templates_1.STATE_BOUNDARY.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
46
45
  const searchRegex = new RegExp(`${ESCAPED_BOUNDARY}(.*?)${ESCAPED_BOUNDARY}`);
47
46
  const stateMatches = message.match(searchRegex);
48
- const match = stateMatches === null || stateMatches === void 0 ? void 0 : stateMatches[1];
49
- return { match, fullMatch: stateMatches === null || stateMatches === void 0 ? void 0 : stateMatches[0] };
47
+ const match = stateMatches?.[1];
48
+ return { match, fullMatch: stateMatches?.[0] };
50
49
  }
51
50
  }
52
51
  ResponseParser.instance = new ResponseParser();
package/package.json CHANGED
@@ -10,7 +10,7 @@
10
10
  "openai"
11
11
  ]
12
12
  },
13
- "version": "5.0.556",
13
+ "version": "5.0.558",
14
14
  "files": [
15
15
  "build"
16
16
  ],
@@ -54,21 +54,21 @@
54
54
  "chat": "node ./build/chat.js"
55
55
  },
56
56
  "dependencies": {
57
- "@sprucelabs/error": "^6.0.477",
58
- "@sprucelabs/mercury-event-emitter": "^42.0.527",
59
- "@sprucelabs/mercury-types": "^47.0.528",
60
- "@sprucelabs/schema": "^30.0.486",
57
+ "@sprucelabs/error": "^6.0.480",
58
+ "@sprucelabs/mercury-event-emitter": "^42.0.529",
59
+ "@sprucelabs/mercury-types": "^47.0.530",
60
+ "@sprucelabs/schema": "^30.0.489",
61
61
  "eta": "^3.5.0",
62
62
  "openai": "^3.3.0"
63
63
  },
64
64
  "devDependencies": {
65
- "@sprucelabs/esm-postbuild": "^6.0.461",
66
- "@sprucelabs/jest-json-reporter": "^8.0.477",
67
- "@sprucelabs/resolve-path-aliases": "^2.0.456",
65
+ "@sprucelabs/esm-postbuild": "^6.0.463",
66
+ "@sprucelabs/jest-json-reporter": "^8.0.481",
67
+ "@sprucelabs/resolve-path-aliases": "^2.0.458",
68
68
  "@sprucelabs/semantic-release": "^5.0.2",
69
- "@sprucelabs/test": "^9.0.50",
70
- "@sprucelabs/test-utils": "^5.1.427",
71
- "@types/node": "^22.7.5",
69
+ "@sprucelabs/test": "^9.0.51",
70
+ "@sprucelabs/test-utils": "^5.1.433",
71
+ "@types/node": "^22.7.6",
72
72
  "chokidar-cli": "^3.0.0",
73
73
  "dotenv": "^16.4.5",
74
74
  "eslint": "^9.12.0",