@caretakerai/agent 0.0.36 → 0.0.38-beta.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.
Files changed (2) hide show
  1. package/dist/agent.js +78 -42
  2. package/package.json +1 -1
package/dist/agent.js CHANGED
@@ -159,67 +159,103 @@ class Agent {
159
159
  let outputHistory = [];
160
160
  const retryErrors = [];
161
161
  // Prepare chat messages
162
- let history = [...this.history];
163
- // Apply transformers to input history
164
- for (const transformer of this.inputTransformers) {
165
- history = await transformer.transform(history);
166
- }
162
+ const history = [...this.history];
167
163
  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) {
164
+ // Find the latest action and observation indices
165
+ const latestActionIndex = history.findLastIndex(activity => activity.kind === activity_1.ActivityKind.Action);
166
+ const latestObservationIndex = history.findLastIndex(activity => activity.kind === activity_1.ActivityKind.Observation);
167
+ // Execute action if latest action has later index than latest observation
168
+ if (latestActionIndex > latestObservationIndex) {
171
169
  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);
170
+ // Get action source
171
+ const { input: source } = history[latestActionIndex];
172
+ // Prefer custom executor is specified
173
+ const result = this.executor
174
+ ? await this.executor(source)
175
+ : await (0, graphql_1.graphql)({ schema: this.schema, source });
176
+ // Recreate output history with only the latest observation
177
+ outputHistory = [{
178
+ kind: activity_1.ActivityKind.Observation,
179
+ input: (0, yaml_1.stringify)(result),
180
+ }];
181
+ if (result.errors) {
182
+ retryErrors.push(...result.errors);
183
+ continue;
176
184
  }
177
- outputHistory.push(...newActivities);
185
+ // Continue to next iteration to check for more actions or generate new activities
186
+ continue;
178
187
  }
179
188
  catch (e) {
180
189
  const err = e;
181
- this.logger.warn(err.message);
190
+ // Recreate output history with only the latest observation
191
+ outputHistory = [{
192
+ kind: activity_1.ActivityKind.Observation,
193
+ input: err.toString(),
194
+ }];
195
+ const message = `Retry ${i + 1} due to action error: ${err}`;
196
+ this.logger.debug(message);
182
197
  retryErrors.push(err);
183
- this.logger.debug(`Retry ${i + 1} due to malformed output: ${err.message}`);
184
198
  continue;
185
199
  }
186
200
  }
187
- // Skip action execution if last activity is not an action
188
- if ([...inputHistory, ...outputHistory].at(-1)?.kind !== activity_1.ActivityKind.Action) {
189
- return;
201
+ // Generate new activities if no action needs execution
202
+ let inputHistory = [...history, ...outputHistory];
203
+ // Apply transformers to input history before LLM inference
204
+ for (const transformer of this.inputTransformers) {
205
+ inputHistory = await transformer.transform(inputHistory);
190
206
  }
191
- // Execute action
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.36",
3
+ "version": "0.0.38-beta.0",
4
4
  "description": "Single framework for building text-agents",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",