@caretakerai/agent 0.0.35 → 0.0.37

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.
@@ -22,9 +22,24 @@ function stringify(activities, transformers) {
22
22
  }
23
23
  const message = transformer.stringify(activity);
24
24
  const lastMessage = messages.at(-1);
25
+ // Convert string content MessageContentComplex array of text messages to allow mixing text and other message types
26
+ if (typeof message.content === 'string') {
27
+ message.content = [{
28
+ type: 'text',
29
+ text: message.content
30
+ }];
31
+ }
25
32
  // Combine with previous message if same role
26
33
  if (lastMessage?.role === message.role) {
27
- lastMessage.content = `${lastMessage.content}${exports.ACTIVITY_SEP}${message.content}`;
34
+ const lastMessageContent = lastMessage.content;
35
+ const messageContent = message.content;
36
+ // If both messages are text, combine them with ACTIVITY_SEP
37
+ if (lastMessageContent[0].type === 'text' && messageContent[0].type === 'text') {
38
+ lastMessageContent[0].text = `${lastMessageContent[0].text}${exports.ACTIVITY_SEP}${messageContent[0].text}`;
39
+ }
40
+ else {
41
+ lastMessage.content = [...lastMessageContent, ...messageContent];
42
+ }
28
43
  return messages;
29
44
  }
30
45
  // Otherwise add as new message
package/dist/agent.js CHANGED
@@ -165,61 +165,97 @@ class Agent {
165
165
  history = await transformer.transform(history);
166
166
  }
167
167
  for (let i = 0; i < this.maxRetries; ++i) {
168
- const inputHistory = [...history, ...outputHistory];
169
- // Action activities should be executed to produce Observation before next LLM iteration
170
- if (inputHistory.at(-1)?.kind !== activity_1.ActivityKind.Action) {
168
+ const combinedHistory = [...history, ...outputHistory];
169
+ // Find the latest action and observation indices
170
+ const latestActionIndex = combinedHistory.findLastIndex(activity => activity.kind === activity_1.ActivityKind.Action);
171
+ const latestObservationIndex = combinedHistory.findLastIndex(activity => activity.kind === activity_1.ActivityKind.Observation);
172
+ // Execute action if latest action has later index than latest observation
173
+ if (latestActionIndex > latestObservationIndex) {
171
174
  try {
172
- let newActivities = await this.transform(inputHistory);
173
- // Apply output transformers to generated activities
174
- for (const transformer of this.outputTransformers) {
175
- newActivities = await transformer.transform(newActivities);
175
+ // Get action source
176
+ const { input: source } = combinedHistory[latestActionIndex];
177
+ // Prefer custom executor is specified
178
+ const result = this.executor
179
+ ? await this.executor(source)
180
+ : await (0, graphql_1.graphql)({ schema: this.schema, source });
181
+ // Add new observation to the iteration history
182
+ outputHistory.push({
183
+ kind: activity_1.ActivityKind.Observation,
184
+ input: (0, yaml_1.stringify)(result),
185
+ });
186
+ if (result.errors) {
187
+ retryErrors.push(...result.errors);
188
+ continue;
176
189
  }
177
- outputHistory.push(...newActivities);
190
+ // Continue to next iteration to check for more actions or generate new activities
191
+ continue;
178
192
  }
179
193
  catch (e) {
180
194
  const err = e;
181
- this.logger.warn(err.message);
195
+ outputHistory.push({
196
+ kind: activity_1.ActivityKind.Observation,
197
+ input: err.toString(),
198
+ });
199
+ const message = `Retry ${i + 1} due to action error: ${err}`;
200
+ this.logger.debug(message);
182
201
  retryErrors.push(err);
183
- this.logger.debug(`Retry ${i + 1} due to malformed output: ${err.message}`);
184
202
  continue;
185
203
  }
186
204
  }
187
- // Skip action execution if last activity is not an action
188
- if ([...inputHistory, ...outputHistory].at(-1)?.kind !== activity_1.ActivityKind.Action) {
189
- return;
190
- }
191
- // Execute action
205
+ // Generate new activities if no action needs execution
206
+ const inputHistory = [...history, ...outputHistory];
192
207
  try {
193
- // Get action source
194
- const { input: source } = [...inputHistory, ...outputHistory].at(-1);
195
- // Prefer custom executor is specified
196
- const result = this.executor
197
- ? await this.executor(source)
198
- : await (0, graphql_1.graphql)({ schema: this.schema, source });
199
- // Add new observation to the iteration history
200
- outputHistory.push({
201
- kind: activity_1.ActivityKind.Observation,
202
- input: (0, yaml_1.stringify)(result),
203
- });
204
- if (result.errors) {
205
- retryErrors.push(...result.errors);
206
- continue;
208
+ let newActivities = await this.transform(inputHistory);
209
+ // Apply output transformers to generated activities
210
+ for (const transformer of this.outputTransformers) {
211
+ newActivities = await transformer.transform(newActivities);
212
+ }
213
+ outputHistory.push(...newActivities);
214
+ // Execute all actions generated in newActivities regardless of index
215
+ const actionsToExecute = newActivities.filter(activity => activity.kind === activity_1.ActivityKind.Action);
216
+ for (const action of actionsToExecute) {
217
+ try {
218
+ // Prefer custom executor is specified
219
+ const result = this.executor
220
+ ? await this.executor(action.input)
221
+ : await (0, graphql_1.graphql)({ schema: this.schema, source: action.input });
222
+ // Add new observation to the iteration history
223
+ outputHistory.push({
224
+ kind: activity_1.ActivityKind.Observation,
225
+ input: (0, yaml_1.stringify)(result),
226
+ });
227
+ if (result.errors) {
228
+ retryErrors.push(...result.errors);
229
+ break; // Stop executing further actions if one has errors
230
+ }
231
+ }
232
+ catch (e) {
233
+ const err = e;
234
+ outputHistory.push({
235
+ kind: activity_1.ActivityKind.Observation,
236
+ input: err.toString(),
237
+ });
238
+ const message = `Retry ${i + 1} due to action error: ${err}`;
239
+ this.logger.debug(message);
240
+ retryErrors.push(err);
241
+ break; // Stop executing further actions if one has errors
242
+ }
207
243
  }
208
- // Add iteration activities to the agent history and finish iteration
209
- this.addActivities(...outputHistory);
210
- return;
211
244
  }
212
245
  catch (e) {
213
246
  const err = e;
214
- outputHistory.push({
215
- kind: activity_1.ActivityKind.Observation,
216
- input: err.toString(),
217
- });
218
- const message = `Retry ${i + 1} due to action error: ${err}`;
219
- this.logger.debug(message);
247
+ this.logger.warn(err.message);
220
248
  retryErrors.push(err);
249
+ this.logger.debug(`Retry ${i + 1} due to malformed output: ${err.message}`);
221
250
  continue;
222
251
  }
252
+ // If no action needs execution and last activity is not an action, finish iteration
253
+ const finalHistory = [...history, ...outputHistory];
254
+ if (finalHistory.at(-1)?.kind !== activity_1.ActivityKind.Action) {
255
+ // Add iteration activities to the agent history and finish iteration
256
+ this.addActivities(...outputHistory);
257
+ return;
258
+ }
223
259
  }
224
260
  throw new AgentRetryError('Max number of retries reached.', retryErrors);
225
261
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@caretakerai/agent",
3
- "version": "0.0.35",
3
+ "version": "0.0.37",
4
4
  "description": "Single framework for building text-agents",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",