@sprucelabs/sprucebot-llm 15.0.2 → 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.
@@ -11,7 +11,7 @@
11
11
  "error"
12
12
  ],
13
13
  "regressionproof": {
14
- "enabled": true,
14
+ "enabled": false,
15
15
  "projectName": "sprucebot-llm"
16
16
  }
17
17
  }
@@ -16,7 +16,8 @@ export default class SprucebotLlmBotImpl<StateSchema extends Schema = Schema, St
16
16
  markAsDone(): void;
17
17
  getIsDone(): boolean;
18
18
  serialize(): SerializedBot<StateSchema, State>;
19
- sendMessage(message: SendMessage, cb?: MessageResponseCallback): Promise<string>;
19
+ unserialize(serialized: SerializedBot<StateSchema, State>): void;
20
+ sendMessage(message: SendMessage, cb?: MessageResponseCallback): Promise<string | null>;
20
21
  private optionallyUpdateState;
21
22
  private trackMessage;
22
23
  updateState(updates: Partial<State>): Promise<void>;
@@ -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,7 +16,7 @@ export default class TurnRequest {
16
16
  optionallyUpdateState: (state?: Record<string, any>) => Promise<void>;
17
17
  });
18
18
  cancel(): void;
19
- sendMessage(llmMessage: LlmMessage, cb?: MessageResponseCallback): Promise<string>;
19
+ sendMessage(llmMessage: LlmMessage, cb?: MessageResponseCallback): Promise<string | null>;
20
20
  private sendMessageToAdapter;
21
21
  private parseResponse;
22
22
  }
@@ -26,7 +26,7 @@ class TurnRequest {
26
26
  if (this.isCancelled) {
27
27
  return '';
28
28
  }
29
- let parsedMessage;
29
+ let parsedMessage = null;
30
30
  let isDone;
31
31
  let state;
32
32
  let callbackResults;
@@ -16,7 +16,8 @@ export default class SprucebotLlmBotImpl<StateSchema extends Schema = Schema, St
16
16
  markAsDone(): void;
17
17
  getIsDone(): boolean;
18
18
  serialize(): SerializedBot<StateSchema, State>;
19
- sendMessage(message: SendMessage, cb?: MessageResponseCallback): Promise<string>;
19
+ unserialize(serialized: SerializedBot<StateSchema, State>): void;
20
+ sendMessage(message: SendMessage, cb?: MessageResponseCallback): Promise<string | null>;
20
21
  private optionallyUpdateState;
21
22
  private trackMessage;
22
23
  updateState(updates: Partial<State>): Promise<void>;
@@ -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
  }
@@ -16,7 +16,7 @@ export default class TurnRequest {
16
16
  optionallyUpdateState: (state?: Record<string, any>) => Promise<void>;
17
17
  });
18
18
  cancel(): void;
19
- sendMessage(llmMessage: LlmMessage, cb?: MessageResponseCallback): Promise<string>;
19
+ sendMessage(llmMessage: LlmMessage, cb?: MessageResponseCallback): Promise<string | null>;
20
20
  private sendMessageToAdapter;
21
21
  private parseResponse;
22
22
  }
@@ -32,7 +32,7 @@ export default class TurnRequest {
32
32
  if (this.isCancelled) {
33
33
  return '';
34
34
  }
35
- let parsedMessage;
35
+ let parsedMessage = null;
36
36
  let isDone;
37
37
  let state;
38
38
  let callbackResults;
@@ -9,8 +9,9 @@ export interface BotOptions<StateSchema extends Schema = Schema, State extends S
9
9
  export interface SprucebotLlmBot<StateSchema extends Schema = Schema, State extends SchemaValues<StateSchema> = SchemaValues<StateSchema>> extends MercuryEventEmitter<LlmEventContract> {
10
10
  markAsDone(): void;
11
11
  getIsDone(): boolean;
12
- sendMessage(message: SendMessage, responseCb?: MessageResponseCallback): Promise<string>;
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
  }
@@ -96,7 +98,7 @@ export type LlmCallbackParameter = {
96
98
  name: string;
97
99
  description?: string;
98
100
  });
99
- export type MessageResponseCallback = (message: string) => void;
101
+ export type MessageResponseCallback = (message: string | null) => void;
100
102
  export type SendMessage = string | SendMessageWithImage;
101
103
  export interface SendMessageWithImage {
102
104
  imageDescription: string;
@@ -111,6 +113,6 @@ export interface ResponseParser {
111
113
  export interface ParsedResponse {
112
114
  isDone: boolean;
113
115
  state?: Record<string, any>;
114
- message: string;
116
+ message: string | null;
115
117
  callbackResults?: SendMessage;
116
118
  }
@@ -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
  }
@@ -17,13 +17,26 @@ export default class ResponseParserV2 {
17
17
  let callbackResults = undefined;
18
18
  const hasCallbacks = message.includes('@callback');
19
19
  if (hasCallbacks) {
20
- callbackResults = yield this.invokeCallbacks(message, callbacks);
20
+ const { callbackResults: c, message: m } = yield this.invokeCallbacks(message, callbacks);
21
+ callbackResults = c;
22
+ message = m;
21
23
  }
22
24
  const hasState = response.includes('@updateState');
23
- if (hasState) {
25
+ if (hasState && message) {
24
26
  const stateMatch = message.match(/@updateState\s+({[\s\S]*?})\n?/);
25
27
  if (stateMatch && stateMatch[1]) {
26
- 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
+ }
27
40
  message = message.replace(stateMatch[0], '').trim();
28
41
  }
29
42
  }
@@ -37,37 +50,54 @@ export default class ResponseParserV2 {
37
50
  }
38
51
  invokeCallbacks(message, callbacks) {
39
52
  return __awaiter(this, void 0, void 0, function* () {
53
+ let callbackStrippedMessage = message;
40
54
  let callbackResults = '';
41
55
  const matches = [...message.matchAll(/^@callback\s+({.*})$/gm)];
42
56
  for (const match of matches) {
43
57
  const callbackData = match && match[1] ? JSON.parse(match[1]) : null;
44
58
  const { name, options } = callbackData || {};
59
+ debugger;
60
+ if (match) {
61
+ const parts = callbackStrippedMessage.split(match[0]);
62
+ callbackStrippedMessage = parts
63
+ .map((s) => s.trim())
64
+ .join('')
65
+ .trim();
66
+ }
45
67
  try {
46
68
  const callback = callbacks === null || callbacks === void 0 ? void 0 : callbacks[name];
47
69
  if (callback === null || callback === void 0 ? void 0 : callback.parameters) {
48
70
  validateAndNormalizeCallbackOptions(callback.parameters, options);
49
71
  }
50
72
  const results = yield (callback === null || callback === void 0 ? void 0 : callback.cb(options));
51
- callbackResults += `@results ${JSON.stringify({
73
+ callbackResults += this.renderCallbackResults({
52
74
  name,
53
75
  results,
54
- })}\n`;
76
+ });
55
77
  }
56
78
  catch (err) {
57
- callbackResults += `@results ${JSON.stringify({
79
+ callbackResults += this.renderCallbackResults({
58
80
  name,
59
81
  error: err,
60
- })}\n`;
82
+ });
61
83
  }
62
84
  }
63
85
  callbackResults = callbackResults.trim();
64
- return callbackResults;
86
+ return {
87
+ callbackResults,
88
+ message: callbackStrippedMessage.length > 0
89
+ ? callbackStrippedMessage
90
+ : null,
91
+ };
65
92
  });
66
93
  }
94
+ renderCallbackResults(callbackOptions) {
95
+ return `@results ${JSON.stringify(callbackOptions)}\n`;
96
+ }
67
97
  getStateUpdateInstructions() {
68
- 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.';
69
99
  }
70
100
  getFunctionCallInstructions() {
71
- 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.`;
72
102
  }
73
103
  }
@@ -9,8 +9,9 @@ export interface BotOptions<StateSchema extends Schema = Schema, State extends S
9
9
  export interface SprucebotLlmBot<StateSchema extends Schema = Schema, State extends SchemaValues<StateSchema> = SchemaValues<StateSchema>> extends MercuryEventEmitter<LlmEventContract> {
10
10
  markAsDone(): void;
11
11
  getIsDone(): boolean;
12
- sendMessage(message: SendMessage, responseCb?: MessageResponseCallback): Promise<string>;
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
  }
@@ -96,7 +98,7 @@ export type LlmCallbackParameter = {
96
98
  name: string;
97
99
  description?: string;
98
100
  });
99
- export type MessageResponseCallback = (message: string) => void;
101
+ export type MessageResponseCallback = (message: string | null) => void;
100
102
  export type SendMessage = string | SendMessageWithImage;
101
103
  export interface SendMessageWithImage {
102
104
  imageDescription: string;
@@ -111,6 +113,6 @@ export interface ResponseParser {
111
113
  export interface ParsedResponse {
112
114
  isDone: boolean;
113
115
  state?: Record<string, any>;
114
- message: string;
116
+ message: string | null;
115
117
  callbackResults?: SendMessage;
116
118
  }
@@ -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
  }
@@ -12,13 +12,26 @@ class ResponseParserV2 {
12
12
  let callbackResults = undefined;
13
13
  const hasCallbacks = message.includes('@callback');
14
14
  if (hasCallbacks) {
15
- callbackResults = await this.invokeCallbacks(message, callbacks);
15
+ const { callbackResults: c, message: m } = await this.invokeCallbacks(message, callbacks);
16
+ callbackResults = c;
17
+ message = m;
16
18
  }
17
19
  const hasState = response.includes('@updateState');
18
- if (hasState) {
20
+ if (hasState && message) {
19
21
  const stateMatch = message.match(/@updateState\s+({[\s\S]*?})\n?/);
20
22
  if (stateMatch && stateMatch[1]) {
21
- 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
+ }
22
35
  message = message.replace(stateMatch[0], '').trim();
23
36
  }
24
37
  }
@@ -30,37 +43,54 @@ class ResponseParserV2 {
30
43
  };
31
44
  }
32
45
  async invokeCallbacks(message, callbacks) {
46
+ let callbackStrippedMessage = message;
33
47
  let callbackResults = '';
34
48
  const matches = [...message.matchAll(/^@callback\s+({.*})$/gm)];
35
49
  for (const match of matches) {
36
50
  const callbackData = match && match[1] ? JSON.parse(match[1]) : null;
37
51
  const { name, options } = callbackData || {};
52
+ debugger;
53
+ if (match) {
54
+ const parts = callbackStrippedMessage.split(match[0]);
55
+ callbackStrippedMessage = parts
56
+ .map((s) => s.trim())
57
+ .join('')
58
+ .trim();
59
+ }
38
60
  try {
39
61
  const callback = callbacks?.[name];
40
62
  if (callback?.parameters) {
41
63
  (0, validateAndNormalizeCallbackOptions_1.default)(callback.parameters, options);
42
64
  }
43
65
  const results = await callback?.cb(options);
44
- callbackResults += `@results ${JSON.stringify({
66
+ callbackResults += this.renderCallbackResults({
45
67
  name,
46
68
  results,
47
- })}\n`;
69
+ });
48
70
  }
49
71
  catch (err) {
50
- callbackResults += `@results ${JSON.stringify({
72
+ callbackResults += this.renderCallbackResults({
51
73
  name,
52
74
  error: err,
53
- })}\n`;
75
+ });
54
76
  }
55
77
  }
56
78
  callbackResults = callbackResults.trim();
57
- return callbackResults;
79
+ return {
80
+ callbackResults,
81
+ message: callbackStrippedMessage.length > 0
82
+ ? callbackStrippedMessage
83
+ : null,
84
+ };
85
+ }
86
+ renderCallbackResults(callbackOptions) {
87
+ return `@results ${JSON.stringify(callbackOptions)}\n`;
58
88
  }
59
89
  getStateUpdateInstructions() {
60
- 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.';
61
91
  }
62
92
  getFunctionCallInstructions() {
63
- 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.`;
64
94
  }
65
95
  }
66
96
  exports.default = ResponseParserV2;
package/package.json CHANGED
@@ -8,7 +8,7 @@
8
8
  "eta"
9
9
  ]
10
10
  },
11
- "version": "15.0.2",
11
+ "version": "15.1.0",
12
12
  "files": [
13
13
  "build"
14
14
  ],