@caretakerai/agent 0.0.38-beta.0 → 0.0.38-beta.2
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.
- package/dist/agent.js +33 -34
- package/package.json +1 -1
package/dist/agent.js
CHANGED
|
@@ -134,7 +134,15 @@ class Agent {
|
|
|
134
134
|
...history,
|
|
135
135
|
], this.transformers);
|
|
136
136
|
const res = await this.llm.invoke(messages);
|
|
137
|
-
let
|
|
137
|
+
let content = res.content;
|
|
138
|
+
if (typeof content !== 'string') {
|
|
139
|
+
const messageContent = content;
|
|
140
|
+
const text = messageContent.find(c => c.type === 'text');
|
|
141
|
+
if (!text) {
|
|
142
|
+
throw new AgentError('No text content found in the response.');
|
|
143
|
+
}
|
|
144
|
+
content = text.text;
|
|
145
|
+
}
|
|
138
146
|
const { response_metadata } = res;
|
|
139
147
|
const newActivities = [];
|
|
140
148
|
if (response_metadata?.finish_reason == 'length') {
|
|
@@ -156,50 +164,44 @@ class Agent {
|
|
|
156
164
|
return newActivities;
|
|
157
165
|
}
|
|
158
166
|
async transformAndExecute() {
|
|
159
|
-
|
|
167
|
+
const outputHistory = [];
|
|
160
168
|
const retryErrors = [];
|
|
161
169
|
// Prepare chat messages
|
|
162
170
|
const history = [...this.history];
|
|
163
171
|
for (let i = 0; i < this.maxRetries; ++i) {
|
|
172
|
+
let inputHistory = [...history, ...outputHistory];
|
|
164
173
|
// Find the latest action and observation indices
|
|
165
|
-
const latestActionIndex =
|
|
166
|
-
const latestObservationIndex =
|
|
174
|
+
const latestActionIndex = inputHistory.findLastIndex(activity => activity.kind === activity_1.ActivityKind.Action);
|
|
175
|
+
const latestObservationIndex = inputHistory.findLastIndex(activity => activity.kind === activity_1.ActivityKind.Observation);
|
|
167
176
|
// Execute action if latest action has later index than latest observation
|
|
168
177
|
if (latestActionIndex > latestObservationIndex) {
|
|
169
178
|
try {
|
|
170
179
|
// Get action source
|
|
171
|
-
const { input: source } =
|
|
180
|
+
const { input: source } = inputHistory[latestActionIndex];
|
|
172
181
|
// Prefer custom executor is specified
|
|
173
182
|
const result = this.executor
|
|
174
183
|
? await this.executor(source)
|
|
175
184
|
: 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
185
|
if (result.errors) {
|
|
182
186
|
retryErrors.push(...result.errors);
|
|
183
|
-
|
|
187
|
+
}
|
|
188
|
+
else {
|
|
189
|
+
outputHistory.push({
|
|
190
|
+
kind: activity_1.ActivityKind.Observation,
|
|
191
|
+
input: (0, yaml_1.stringify)(result),
|
|
192
|
+
});
|
|
184
193
|
}
|
|
185
194
|
// Continue to next iteration to check for more actions or generate new activities
|
|
186
195
|
continue;
|
|
187
196
|
}
|
|
188
197
|
catch (e) {
|
|
189
198
|
const err = e;
|
|
190
|
-
// Recreate output history with only the latest observation
|
|
191
|
-
outputHistory = [{
|
|
192
|
-
kind: activity_1.ActivityKind.Observation,
|
|
193
|
-
input: err.toString(),
|
|
194
|
-
}];
|
|
195
199
|
const message = `Retry ${i + 1} due to action error: ${err}`;
|
|
196
200
|
this.logger.debug(message);
|
|
197
201
|
retryErrors.push(err);
|
|
198
202
|
continue;
|
|
199
203
|
}
|
|
200
204
|
}
|
|
201
|
-
// Generate new activities if no action needs execution
|
|
202
|
-
let inputHistory = [...history, ...outputHistory];
|
|
203
205
|
// Apply transformers to input history before LLM inference
|
|
204
206
|
for (const transformer of this.inputTransformers) {
|
|
205
207
|
inputHistory = await transformer.transform(inputHistory);
|
|
@@ -219,14 +221,9 @@ class Agent {
|
|
|
219
221
|
const result = this.executor
|
|
220
222
|
? await this.executor(action.input)
|
|
221
223
|
: await (0, graphql_1.graphql)({ schema: this.schema, source: action.input });
|
|
222
|
-
//
|
|
223
|
-
outputHistory.push({
|
|
224
|
-
kind: activity_1.ActivityKind.Observation,
|
|
225
|
-
input: (0, yaml_1.stringify)(result),
|
|
226
|
-
});
|
|
224
|
+
// Throw error if there are any errors and push the agent to retry generation
|
|
227
225
|
if (result.errors) {
|
|
228
|
-
|
|
229
|
-
break; // Stop executing further actions if one has errors
|
|
226
|
+
throw new AgentRetryError('Action error', result.errors.map(e => new Error(e.message))); // Stop executing further actions if one has errors
|
|
230
227
|
}
|
|
231
228
|
}
|
|
232
229
|
catch (e) {
|
|
@@ -237,8 +234,14 @@ class Agent {
|
|
|
237
234
|
});
|
|
238
235
|
const message = `Retry ${i + 1} due to action error: ${err}`;
|
|
239
236
|
this.logger.debug(message);
|
|
240
|
-
|
|
241
|
-
|
|
237
|
+
// Add errors to retry errors if they are not already present
|
|
238
|
+
if (err instanceof AgentRetryError) {
|
|
239
|
+
retryErrors.push(...err.errors);
|
|
240
|
+
}
|
|
241
|
+
else {
|
|
242
|
+
retryErrors.push(err);
|
|
243
|
+
}
|
|
244
|
+
throw err; // Stop executing further actions if one has errors
|
|
242
245
|
}
|
|
243
246
|
}
|
|
244
247
|
}
|
|
@@ -249,13 +252,9 @@ class Agent {
|
|
|
249
252
|
this.logger.debug(`Retry ${i + 1} due to malformed output: ${err.message}`);
|
|
250
253
|
continue;
|
|
251
254
|
}
|
|
252
|
-
//
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
// Add iteration activities to the agent history and finish iteration
|
|
256
|
-
this.addActivities(...outputHistory);
|
|
257
|
-
return;
|
|
258
|
-
}
|
|
255
|
+
// Add iteration activities to the agent history and finish iteration
|
|
256
|
+
this.addActivities(...outputHistory);
|
|
257
|
+
return;
|
|
259
258
|
}
|
|
260
259
|
throw new AgentRetryError('Max number of retries reached.', retryErrors);
|
|
261
260
|
}
|