@sprucelabs/sprucebot-llm 5.0.6 → 5.0.7

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.
@@ -1,5 +1,5 @@
1
- import { ErrorOptions as ISpruceErrorOptions } from '@sprucelabs/error';
2
- import { SpruceErrors } from './errors.types';
1
+ import { SpruceErrors } from "./errors.types";
2
+ import { ErrorOptions as ISpruceErrorOptions } from "@sprucelabs/error";
3
3
  export interface NoBotInstanceSetErrorOptions extends SpruceErrors.SprucebotLlm.NoBotInstanceSet, ISpruceErrorOptions {
4
4
  code: 'NO_BOT_INSTANCE_SET';
5
5
  }
@@ -5,7 +5,7 @@ const noBotInstanceSetSchema = {
5
5
  id: 'noBotInstanceSet',
6
6
  namespace: 'SprucebotLlm',
7
7
  name: 'No bot instance set',
8
- fields: {},
8
+ fields: {}
9
9
  };
10
10
  schema_1.SchemaRegistry.getInstance().trackSchema(noBotInstanceSetSchema);
11
11
  exports.default = noBotInstanceSetSchema;
@@ -1 +1 @@
1
- export { FieldDefinitions, FieldDefinitionMap, FieldValueTypeGeneratorMap, FieldMap, } from '@sprucelabs/schema';
1
+ export { FieldDefinitions, FieldDefinitionMap, FieldValueTypeGeneratorMap, FieldMap } from '@sprucelabs/schema';
@@ -5,20 +5,19 @@ const schema_1 = require("@sprucelabs/schema");
5
5
  const eta_1 = require("eta");
6
6
  const templates_1 = require("./templates");
7
7
  class PromptGenerator {
8
- bot;
9
- eta = new eta_1.Eta();
10
- log = process.env.SHOULD_LOG_GENERATED_PROMPTS === 'true'
11
- ? console.info
12
- : () => { };
13
- static Class;
14
- promptTemplate;
15
8
  constructor(bot, options) {
9
+ var _a;
10
+ this.eta = new eta_1.Eta();
11
+ this.log = process.env.SHOULD_LOG_GENERATED_PROMPTS === 'true'
12
+ ? console.info
13
+ : () => { };
16
14
  (0, schema_1.assertOptions)({ bot }, ['bot']);
17
15
  this.bot = bot;
18
- this.promptTemplate = options?.promptTemplate ?? templates_1.PROMPT_TEMPLATE;
16
+ this.promptTemplate = (_a = options === null || options === void 0 ? void 0 : options.promptTemplate) !== null && _a !== void 0 ? _a : templates_1.PROMPT_TEMPLATE;
19
17
  }
20
18
  static Generator(bot, options) {
21
- return new (this.Class ?? PromptGenerator)(bot, options);
19
+ var _a;
20
+ return new ((_a = this.Class) !== null && _a !== void 0 ? _a : PromptGenerator)(bot, options);
22
21
  }
23
22
  async generate() {
24
23
  const { stateSchema, state, ...rest } = this.bot.serialize();
@@ -38,7 +37,7 @@ class PromptGenerator {
38
37
  stateJson: null,
39
38
  };
40
39
  }
41
- const normalizedState = (0, schema_1.normalizeSchemaValues)(stateSchema, state ?? {}, {});
40
+ const normalizedState = (0, schema_1.normalizeSchemaValues)(stateSchema, state !== null && state !== void 0 ? state : {}, {});
42
41
  setUndefinedToNull(normalizedState);
43
42
  const stateSchemaJson = JSON.stringify(stateSchema);
44
43
  const stateJson = JSON.stringify(normalizedState);
@@ -8,17 +8,11 @@ const schema_1 = require("@sprucelabs/schema");
8
8
  const llm_types_1 = require("../llm.types");
9
9
  const ResponseParser_1 = __importDefault(require("../parsingResponses/ResponseParser"));
10
10
  class SprucebotLlmBotImpl extends mercury_event_emitter_1.AbstractEventEmitter {
11
- static messageMemoryLimit = 10;
12
- adapter;
13
- youAre;
14
- stateSchema;
15
- state;
16
- isDone = false;
17
- messages = [];
18
- skill;
19
11
  constructor(options) {
20
12
  const { adapter, youAre, stateSchema, state, skill } = options;
21
13
  super(llm_types_1.llmEventContract);
14
+ this.isDone = false;
15
+ this.messages = [];
22
16
  this.adapter = adapter;
23
17
  this.youAre = youAre;
24
18
  this.stateSchema = stateSchema;
@@ -40,34 +34,36 @@ class SprucebotLlmBotImpl extends mercury_event_emitter_1.AbstractEventEmitter {
40
34
  return this.isDone;
41
35
  }
42
36
  serialize() {
43
- const skill = this.skill?.serialize();
37
+ var _a, _b, _c;
38
+ const skill = (_a = this.skill) === null || _a === void 0 ? void 0 : _a.serialize();
44
39
  return {
45
40
  youAre: this.youAre,
46
- stateSchema: this.stateSchema ?? skill?.stateSchema,
47
- state: this.state ?? skill?.state,
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,
48
43
  messages: this.messages,
49
44
  skill,
50
45
  };
51
46
  }
52
47
  async sendMessage(message) {
48
+ var _a, _b;
53
49
  (0, schema_1.assertOptions)({ message }, ['message']);
54
50
  this.trackMessage({
55
51
  from: 'Me',
56
52
  message,
57
53
  });
58
- const serializedSkill = this.skill?.serialize();
54
+ const serializedSkill = (_a = this.skill) === null || _a === void 0 ? void 0 : _a.serialize();
59
55
  const response = await this.adapter.sendMessage(this, {
60
- model: serializedSkill?.model,
61
- promptTemplate: serializedSkill?.promptTemplate,
56
+ model: serializedSkill === null || serializedSkill === void 0 ? void 0 : serializedSkill.model,
57
+ promptTemplate: serializedSkill === null || serializedSkill === void 0 ? void 0 : serializedSkill.promptTemplate,
62
58
  });
63
59
  const parser = ResponseParser_1.default.getInstance();
64
- const { isDone, message: parsedResponse, state, } = await parser.parse(response, serializedSkill?.callbacks);
60
+ const { isDone, message: parsedResponse, state, } = await parser.parse(response, serializedSkill === null || serializedSkill === void 0 ? void 0 : serializedSkill.callbacks);
65
61
  this.isDone = isDone;
66
62
  if (this.stateSchema && state) {
67
63
  await this.updateState(state);
68
64
  }
69
65
  else if (state) {
70
- await this.skill?.updateState(state);
66
+ await ((_b = this.skill) === null || _b === void 0 ? void 0 : _b.updateState(state));
71
67
  }
72
68
  this.trackMessage({
73
69
  from: 'You',
@@ -90,4 +86,5 @@ class SprucebotLlmBotImpl extends mercury_event_emitter_1.AbstractEventEmitter {
90
86
  this.isDone = false;
91
87
  }
92
88
  }
89
+ SprucebotLlmBotImpl.messageMemoryLimit = 10;
93
90
  exports.default = SprucebotLlmBotImpl;
@@ -8,20 +8,16 @@ const SpruceError_1 = __importDefault(require("../errors/SpruceError"));
8
8
  const SprucebotLlmBotImpl_1 = __importDefault(require("./SprucebotLlmBotImpl"));
9
9
  const SprucebotLlmSkillImpl_1 = __importDefault(require("./SprucebotLlmSkillImpl"));
10
10
  class SprucebotLlmFactory {
11
- instance;
12
- static FactoryClass;
13
- static BotClass;
14
- static SkillClass;
15
11
  Bot(options) {
12
+ var _a;
16
13
  (0, schema_1.assertOptions)(options, ['youAre', 'adapter']);
17
14
  const { Class } = options;
18
- return new (Class ??
19
- SprucebotLlmFactory.BotClass ??
20
- SprucebotLlmBotImpl_1.default)(options);
15
+ return new ((_a = Class !== null && Class !== void 0 ? Class : SprucebotLlmFactory.BotClass) !== null && _a !== void 0 ? _a : SprucebotLlmBotImpl_1.default)(options);
21
16
  }
22
17
  Skill(options) {
18
+ var _a;
23
19
  (0, schema_1.assertOptions)(options, ['yourJobIfYouChooseToAcceptItIs']);
24
- return new (SprucebotLlmFactory.SkillClass ?? SprucebotLlmSkillImpl_1.default)(options);
20
+ return new ((_a = SprucebotLlmFactory.SkillClass) !== null && _a !== void 0 ? _a : SprucebotLlmSkillImpl_1.default)(options);
25
21
  }
26
22
  getBotInstance() {
27
23
  if (!this.instance) {
@@ -35,7 +31,8 @@ class SprucebotLlmFactory {
35
31
  this.instance = bot;
36
32
  }
37
33
  static Factory() {
38
- return new (this.FactoryClass ?? this)();
34
+ var _a;
35
+ return new ((_a = this.FactoryClass) !== null && _a !== void 0 ? _a : this)();
39
36
  }
40
37
  static reset() {
41
38
  this.BotClass = undefined;
@@ -4,11 +4,9 @@ const mercury_event_emitter_1 = require("@sprucelabs/mercury-event-emitter");
4
4
  const schema_1 = require("@sprucelabs/schema");
5
5
  const llm_types_1 = require("../llm.types");
6
6
  class SprucebotLlmSkillImpl extends mercury_event_emitter_1.AbstractEventEmitter {
7
- options;
8
- state = {};
9
- stateSchema;
10
7
  constructor(options) {
11
8
  super(llm_types_1.llmEventContract);
9
+ this.state = {};
12
10
  const { state, stateSchema, ...rest } = options;
13
11
  this.options = { ...rest, stateSchema };
14
12
  this.stateSchema = stateSchema;
@@ -8,28 +8,27 @@ const schema_1 = require("@sprucelabs/schema");
8
8
  const openai_1 = require("openai");
9
9
  const PromptGenerator_1 = __importDefault(require("../PromptGenerator"));
10
10
  class OpenAiAdapter {
11
- static Configuration = openai_1.Configuration;
12
- static OpenAIApi = openai_1.OpenAIApi;
13
- api;
14
11
  constructor(apiKey) {
15
12
  (0, schema_1.assertOptions)({ apiKey }, ['apiKey']);
16
13
  const config = new OpenAiAdapter.Configuration({ apiKey });
17
14
  this.api = new OpenAiAdapter.OpenAIApi(config);
18
15
  }
19
16
  async sendMessage(bot, options) {
17
+ var _a, _b, _c, _d;
20
18
  const generator = PromptGenerator_1.default.Generator(bot, {
21
- promptTemplate: options?.promptTemplate,
19
+ promptTemplate: options === null || options === void 0 ? void 0 : options.promptTemplate,
22
20
  });
23
21
  const prompt = await generator.generate();
24
22
  const response = await this.api.createCompletion({
25
23
  prompt,
26
- model: options?.model ?? 'text-davinci-003',
24
+ model: (_a = options === null || options === void 0 ? void 0 : options.model) !== null && _a !== void 0 ? _a : 'text-davinci-003',
27
25
  max_tokens: 250,
28
26
  stop: ['__Me__:'],
29
27
  });
30
- return (response.data.choices[0]?.text?.trim() ??
31
- exports.MESSAGE_RESPONSE_ERROR_MESSAGE);
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);
32
29
  }
33
30
  }
34
31
  exports.OpenAiAdapter = OpenAiAdapter;
32
+ OpenAiAdapter.Configuration = openai_1.Configuration;
33
+ OpenAiAdapter.OpenAIApi = openai_1.OpenAIApi;
35
34
  exports.MESSAGE_RESPONSE_ERROR_MESSAGE = "Oh no! Something went wrong and I can't talk right now!";
@@ -2,10 +2,6 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const openai_1 = require("openai");
4
4
  class SpyOpenAiApi extends openai_1.OpenAIApi {
5
- static config;
6
- static lastMessage;
7
- static lastModel;
8
- static responseMessage = 'hello!';
9
5
  constructor(config) {
10
6
  super(config);
11
7
  SpyOpenAiApi.config = config;
@@ -34,4 +30,5 @@ class SpyOpenAiApi extends openai_1.OpenAIApi {
34
30
  };
35
31
  }
36
32
  }
33
+ SpyOpenAiApi.responseMessage = 'hello!';
37
34
  exports.default = SpyOpenAiApi;
@@ -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?.code) {
12
+ switch (options === null || options === void 0 ? void 0 : 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;
@@ -1,5 +1,5 @@
1
- import { ErrorOptions as ISpruceErrorOptions } from '@sprucelabs/error';
2
- import { SpruceErrors } from './errors.types';
1
+ import { SpruceErrors } from "./errors.types";
2
+ import { ErrorOptions as ISpruceErrorOptions } from "@sprucelabs/error";
3
3
  export interface NoBotInstanceSetErrorOptions extends SpruceErrors.SprucebotLlm.NoBotInstanceSet, ISpruceErrorOptions {
4
4
  code: 'NO_BOT_INSTANCE_SET';
5
5
  }
@@ -6,7 +6,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const templates_1 = require("../bots/templates");
7
7
  const renderPlaceholder_1 = __importDefault(require("./renderPlaceholder"));
8
8
  class ResponseParser {
9
- static instance = new ResponseParser();
10
9
  static setInstance(parser) {
11
10
  this.instance = parser;
12
11
  }
@@ -34,8 +33,9 @@ class ResponseParser {
34
33
  };
35
34
  }
36
35
  async invokeCallback(callbacks, key, message) {
37
- const v = await callbacks?.[key]?.cb();
38
- message = message.replace((0, renderPlaceholder_1.default)(key), v ?? '').trim();
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();
39
39
  return message;
40
40
  }
41
41
  doesIncludeDoneToken(response) {
@@ -45,8 +45,9 @@ class ResponseParser {
45
45
  const ESCAPED_BOUNDARY = templates_1.STATE_BOUNDARY.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
46
46
  const searchRegex = new RegExp(`${ESCAPED_BOUNDARY}(.*?)${ESCAPED_BOUNDARY}`);
47
47
  const stateMatches = message.match(searchRegex);
48
- const match = stateMatches?.[1];
49
- return { match, fullMatch: stateMatches?.[0] };
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] };
50
50
  }
51
51
  }
52
+ ResponseParser.instance = new ResponseParser();
52
53
  exports.default = ResponseParser;
package/package.json CHANGED
@@ -10,7 +10,7 @@
10
10
  "openai"
11
11
  ]
12
12
  },
13
- "version": "5.0.6",
13
+ "version": "5.0.7",
14
14
  "files": [
15
15
  "build"
16
16
  ],
@@ -49,32 +49,30 @@
49
49
  "test": "jest",
50
50
  "update.dependencies": "yarn run clean.dependencies && yarn",
51
51
  "watch.build.dev": "tsc-watch --sourceMap --onCompilationComplete 'yarn run post.watch.build'",
52
- "watch.lint": "concurrently 'yarn run lint' \"chokidar 'src/**/*' -c 'yarn run lint.tsc'\"",
53
52
  "watch.rebuild": "yarn run clean.all && yarn install && yarn run watch.build.dev",
54
53
  "watch.tsc": "tsc -w",
55
54
  "chat": "node ./build/chat.js"
56
55
  },
57
56
  "dependencies": {
58
- "@sprucelabs/error": "^6.0.9",
59
- "@sprucelabs/mercury-event-emitter": "^42.0.9",
60
- "@sprucelabs/mercury-types": "^47.0.8",
61
- "@sprucelabs/schema": "^30.0.13",
57
+ "@sprucelabs/error": "^6.0.11",
58
+ "@sprucelabs/mercury-event-emitter": "^42.0.11",
59
+ "@sprucelabs/mercury-types": "^47.0.11",
60
+ "@sprucelabs/schema": "^30.0.15",
62
61
  "eta": "^3.4.0",
63
62
  "openai": "^3.3.0"
64
63
  },
65
64
  "devDependencies": {
66
- "@sprucelabs/esm-postbuild": "^6.0.9",
67
- "@sprucelabs/jest-json-reporter": "^8.0.8",
68
- "@sprucelabs/resolve-path-aliases": "^2.0.11",
65
+ "@sprucelabs/esm-postbuild": "^6.0.11",
66
+ "@sprucelabs/jest-json-reporter": "^8.0.11",
67
+ "@sprucelabs/resolve-path-aliases": "^2.0.13",
69
68
  "@sprucelabs/semantic-release": "^5.0.1",
70
- "@sprucelabs/test": "^9.0.7",
69
+ "@sprucelabs/test": "^9.0.9",
71
70
  "@sprucelabs/test-utils": "^5.0.10",
72
71
  "@types/node": "^20.12.7",
73
72
  "chokidar-cli": "^3.0.0",
74
- "concurrently": "^8.2.2",
75
73
  "dotenv": "^16.4.5",
76
74
  "eslint": "^9.1.1",
77
- "eslint-config-spruce": "^11.2.12",
75
+ "eslint-config-spruce": "^11.2.13",
78
76
  "jest": "^29.7.0",
79
77
  "jest-circus": "^29.7.0",
80
78
  "prettier": "^3.2.5",