@sprucelabs/sprucebot-llm 15.0.3 → 15.1.0

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.
@@ -16,6 +16,7 @@ export default class SprucebotLlmBotImpl<StateSchema extends Schema = Schema, St
16
16
  markAsDone(): void;
17
17
  getIsDone(): boolean;
18
18
  serialize(): SerializedBot<StateSchema, State>;
19
+ unserialize(serialized: SerializedBot<StateSchema, State>): void;
19
20
  sendMessage(message: SendMessage, cb?: MessageResponseCallback): Promise<string | null>;
20
21
  private optionallyUpdateState;
21
22
  private trackMessage;
@@ -44,6 +44,10 @@ class SprucebotLlmBotImpl extends mercury_event_emitter_1.AbstractEventEmitter {
44
44
  skill,
45
45
  };
46
46
  }
47
+ unserialize(serialized) {
48
+ this.state = serialized.state;
49
+ serialized.skill && this.skill?.unserialize(serialized.skill);
50
+ }
47
51
  async sendMessage(message, cb) {
48
52
  (0, schema_1.assertOptions)({ message }, ['message']);
49
53
  const llmMessage = {
@@ -12,4 +12,5 @@ export default class SprucebotLlmSkillImpl<StateSchema extends Schema = Schema,
12
12
  serialize(): SerializedSkill<StateSchema, State>;
13
13
  silentlySetState(state: Partial<State>): void;
14
14
  getStateSchema(): StateSchema | undefined;
15
+ unserialize(serialized: SerializedSkill<StateSchema, State>): void;
15
16
  }
@@ -43,5 +43,10 @@ class SprucebotLlmSkillImpl extends mercury_event_emitter_1.AbstractEventEmitter
43
43
  getStateSchema() {
44
44
  return this.stateSchema;
45
45
  }
46
+ unserialize(serialized) {
47
+ const { state, ...options } = serialized;
48
+ this.state = state;
49
+ this.options = options;
50
+ }
46
51
  }
47
52
  exports.default = SprucebotLlmSkillImpl;
@@ -16,6 +16,7 @@ export default class SprucebotLlmBotImpl<StateSchema extends Schema = Schema, St
16
16
  markAsDone(): void;
17
17
  getIsDone(): boolean;
18
18
  serialize(): SerializedBot<StateSchema, State>;
19
+ unserialize(serialized: SerializedBot<StateSchema, State>): void;
19
20
  sendMessage(message: SendMessage, cb?: MessageResponseCallback): Promise<string | null>;
20
21
  private optionallyUpdateState;
21
22
  private trackMessage;
@@ -46,6 +46,11 @@ class SprucebotLlmBotImpl extends AbstractEventEmitter {
46
46
  skill,
47
47
  };
48
48
  }
49
+ unserialize(serialized) {
50
+ var _a;
51
+ this.state = serialized.state;
52
+ serialized.skill && ((_a = this.skill) === null || _a === void 0 ? void 0 : _a.unserialize(serialized.skill));
53
+ }
49
54
  sendMessage(message, cb) {
50
55
  return __awaiter(this, void 0, void 0, function* () {
51
56
  var _a;
@@ -12,4 +12,5 @@ export default class SprucebotLlmSkillImpl<StateSchema extends Schema = Schema,
12
12
  serialize(): SerializedSkill<StateSchema, State>;
13
13
  silentlySetState(state: Partial<State>): void;
14
14
  getStateSchema(): StateSchema | undefined;
15
+ unserialize(serialized: SerializedSkill<StateSchema, State>): void;
15
16
  }
@@ -53,4 +53,9 @@ export default class SprucebotLlmSkillImpl extends AbstractEventEmitter {
53
53
  getStateSchema() {
54
54
  return this.stateSchema;
55
55
  }
56
+ unserialize(serialized) {
57
+ const { state } = serialized, options = __rest(serialized, ["state"]);
58
+ this.state = state;
59
+ this.options = options;
60
+ }
56
61
  }
@@ -11,6 +11,7 @@ export interface SprucebotLlmBot<StateSchema extends Schema = Schema, State exte
11
11
  getIsDone(): boolean;
12
12
  sendMessage(message: SendMessage, responseCb?: MessageResponseCallback): Promise<string | null>;
13
13
  serialize(): SerializedBot<StateSchema, State>;
14
+ unserialize(serialized: SerializedBot<StateSchema, State>): void;
14
15
  updateState(state: Partial<State>): Promise<void>;
15
16
  setSkill(skill: SprucebotLLmSkill<any>): void;
16
17
  clearMessageHistory(): void;
@@ -73,6 +74,7 @@ export interface SkillOptions<StateSchema extends Schema = Schema, State extends
73
74
  export interface SprucebotLLmSkill<StateSchema extends Schema = Schema, State extends SchemaValues<StateSchema> = SchemaValues<StateSchema>> extends MercuryEventEmitter<LlmEventContract> {
74
75
  getState(): Partial<State> | undefined;
75
76
  serialize(): SerializedSkill<StateSchema, State>;
77
+ unserialize(serialized: SerializedSkill<StateSchema, State>): void;
76
78
  updateState(state: Partial<State>): Promise<void>;
77
79
  setModel(model: string): void;
78
80
  }
@@ -2,6 +2,7 @@ import { ResponseParser, LlmCallbackMap, ParsedResponse } from '../llm.types';
2
2
  export default class ResponseParserV2 implements ResponseParser {
3
3
  parse(response: string, callbacks?: LlmCallbackMap): Promise<ParsedResponse>;
4
4
  private invokeCallbacks;
5
+ private renderCallbackResults;
5
6
  getStateUpdateInstructions(): string;
6
7
  getFunctionCallInstructions(): string;
7
8
  }
@@ -25,7 +25,18 @@ export default class ResponseParserV2 {
25
25
  if (hasState && message) {
26
26
  const stateMatch = message.match(/@updateState\s+({[\s\S]*?})\n?/);
27
27
  if (stateMatch && stateMatch[1]) {
28
- state = JSON.parse(stateMatch[1]);
28
+ try {
29
+ state = JSON.parse(stateMatch[1]);
30
+ }
31
+ catch (err) {
32
+ if (!callbackResults) {
33
+ callbackResults = '';
34
+ }
35
+ callbackResults += this.renderCallbackResults({
36
+ error: err,
37
+ name: 'updateState',
38
+ });
39
+ }
29
40
  message = message.replace(stateMatch[0], '').trim();
30
41
  }
31
42
  }
@@ -59,16 +70,16 @@ export default class ResponseParserV2 {
59
70
  validateAndNormalizeCallbackOptions(callback.parameters, options);
60
71
  }
61
72
  const results = yield (callback === null || callback === void 0 ? void 0 : callback.cb(options));
62
- callbackResults += `@results ${JSON.stringify({
73
+ callbackResults += this.renderCallbackResults({
63
74
  name,
64
75
  results,
65
- })}\n`;
76
+ });
66
77
  }
67
78
  catch (err) {
68
- callbackResults += `@results ${JSON.stringify({
79
+ callbackResults += this.renderCallbackResults({
69
80
  name,
70
81
  error: err,
71
- })}\n`;
82
+ });
72
83
  }
73
84
  }
74
85
  callbackResults = callbackResults.trim();
@@ -80,10 +91,13 @@ export default class ResponseParserV2 {
80
91
  };
81
92
  });
82
93
  }
94
+ renderCallbackResults(callbackOptions) {
95
+ return `@results ${JSON.stringify(callbackOptions)}\n`;
96
+ }
83
97
  getStateUpdateInstructions() {
84
- return 'Updating state works similar to all function calls. Use the following syntax:\n@updateState { "updates": "here" }\n. Make sure to json encode only the fields you want to change. You can update state once and do it at the end of any messages you send.';
98
+ return 'Updating state works similar to all function calls. Use the following syntax:\n@updateState { "updates": "here" }\n. Make sure to json encode only the fields you want to change. You can update state once and do it at the end of any messages you send. NOTE: Keep json on a single line to avoid parsing issues.';
85
99
  }
86
100
  getFunctionCallInstructions() {
87
- return `A function call is done using the following syntax:\n@callback { "name": "callbackName", "options": {} }\nMake sure to json encode the options and include the name of the callback you want to call. You can call as many callbacks as you want in a single response by including multiple @callback lines.`;
101
+ return `A function call is done using the following syntax:\n@callback { "name": "callbackName", "options": {} }\nMake sure to json encode the options and include the name of the callback you want to call. You can call as many callbacks as you want in a single response by including multiple @callback lines. NOTE: Keep json on a single line to avoid parsing issues.`;
88
102
  }
89
103
  }
@@ -11,6 +11,7 @@ export interface SprucebotLlmBot<StateSchema extends Schema = Schema, State exte
11
11
  getIsDone(): boolean;
12
12
  sendMessage(message: SendMessage, responseCb?: MessageResponseCallback): Promise<string | null>;
13
13
  serialize(): SerializedBot<StateSchema, State>;
14
+ unserialize(serialized: SerializedBot<StateSchema, State>): void;
14
15
  updateState(state: Partial<State>): Promise<void>;
15
16
  setSkill(skill: SprucebotLLmSkill<any>): void;
16
17
  clearMessageHistory(): void;
@@ -73,6 +74,7 @@ export interface SkillOptions<StateSchema extends Schema = Schema, State extends
73
74
  export interface SprucebotLLmSkill<StateSchema extends Schema = Schema, State extends SchemaValues<StateSchema> = SchemaValues<StateSchema>> extends MercuryEventEmitter<LlmEventContract> {
74
75
  getState(): Partial<State> | undefined;
75
76
  serialize(): SerializedSkill<StateSchema, State>;
77
+ unserialize(serialized: SerializedSkill<StateSchema, State>): void;
76
78
  updateState(state: Partial<State>): Promise<void>;
77
79
  setModel(model: string): void;
78
80
  }
@@ -2,6 +2,7 @@ import { ResponseParser, LlmCallbackMap, ParsedResponse } from '../llm.types';
2
2
  export default class ResponseParserV2 implements ResponseParser {
3
3
  parse(response: string, callbacks?: LlmCallbackMap): Promise<ParsedResponse>;
4
4
  private invokeCallbacks;
5
+ private renderCallbackResults;
5
6
  getStateUpdateInstructions(): string;
6
7
  getFunctionCallInstructions(): string;
7
8
  }
@@ -20,7 +20,18 @@ class ResponseParserV2 {
20
20
  if (hasState && message) {
21
21
  const stateMatch = message.match(/@updateState\s+({[\s\S]*?})\n?/);
22
22
  if (stateMatch && stateMatch[1]) {
23
- state = JSON.parse(stateMatch[1]);
23
+ try {
24
+ state = JSON.parse(stateMatch[1]);
25
+ }
26
+ catch (err) {
27
+ if (!callbackResults) {
28
+ callbackResults = '';
29
+ }
30
+ callbackResults += this.renderCallbackResults({
31
+ error: err,
32
+ name: 'updateState',
33
+ });
34
+ }
24
35
  message = message.replace(stateMatch[0], '').trim();
25
36
  }
26
37
  }
@@ -52,16 +63,16 @@ class ResponseParserV2 {
52
63
  (0, validateAndNormalizeCallbackOptions_1.default)(callback.parameters, options);
53
64
  }
54
65
  const results = await callback?.cb(options);
55
- callbackResults += `@results ${JSON.stringify({
66
+ callbackResults += this.renderCallbackResults({
56
67
  name,
57
68
  results,
58
- })}\n`;
69
+ });
59
70
  }
60
71
  catch (err) {
61
- callbackResults += `@results ${JSON.stringify({
72
+ callbackResults += this.renderCallbackResults({
62
73
  name,
63
74
  error: err,
64
- })}\n`;
75
+ });
65
76
  }
66
77
  }
67
78
  callbackResults = callbackResults.trim();
@@ -72,11 +83,14 @@ class ResponseParserV2 {
72
83
  : null,
73
84
  };
74
85
  }
86
+ renderCallbackResults(callbackOptions) {
87
+ return `@results ${JSON.stringify(callbackOptions)}\n`;
88
+ }
75
89
  getStateUpdateInstructions() {
76
- return 'Updating state works similar to all function calls. Use the following syntax:\n@updateState { "updates": "here" }\n. Make sure to json encode only the fields you want to change. You can update state once and do it at the end of any messages you send.';
90
+ return 'Updating state works similar to all function calls. Use the following syntax:\n@updateState { "updates": "here" }\n. Make sure to json encode only the fields you want to change. You can update state once and do it at the end of any messages you send. NOTE: Keep json on a single line to avoid parsing issues.';
77
91
  }
78
92
  getFunctionCallInstructions() {
79
- return `A function call is done using the following syntax:\n@callback { "name": "callbackName", "options": {} }\nMake sure to json encode the options and include the name of the callback you want to call. You can call as many callbacks as you want in a single response by including multiple @callback lines.`;
93
+ return `A function call is done using the following syntax:\n@callback { "name": "callbackName", "options": {} }\nMake sure to json encode the options and include the name of the callback you want to call. You can call as many callbacks as you want in a single response by including multiple @callback lines. NOTE: Keep json on a single line to avoid parsing issues.`;
80
94
  }
81
95
  }
82
96
  exports.default = ResponseParserV2;
package/package.json CHANGED
@@ -8,7 +8,7 @@
8
8
  "eta"
9
9
  ]
10
10
  },
11
- "version": "15.0.3",
11
+ "version": "15.1.0",
12
12
  "files": [
13
13
  "build"
14
14
  ],