@caretakerai/agent 0.0.37 → 0.0.38-beta.1

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 +25 -23
  2. 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 { content } = res;
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,54 +164,48 @@ class Agent {
156
164
  return newActivities;
157
165
  }
158
166
  async transformAndExecute() {
159
- let outputHistory = [];
167
+ const outputHistory = [];
160
168
  const retryErrors = [];
161
169
  // 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
- }
170
+ const history = [...this.history];
167
171
  for (let i = 0; i < this.maxRetries; ++i) {
168
- const combinedHistory = [...history, ...outputHistory];
172
+ let inputHistory = [...history, ...outputHistory];
169
173
  // 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);
174
+ const latestActionIndex = inputHistory.findLastIndex(activity => activity.kind === activity_1.ActivityKind.Action);
175
+ const latestObservationIndex = inputHistory.findLastIndex(activity => activity.kind === activity_1.ActivityKind.Observation);
172
176
  // Execute action if latest action has later index than latest observation
173
177
  if (latestActionIndex > latestObservationIndex) {
174
178
  try {
175
179
  // Get action source
176
- const { input: source } = combinedHistory[latestActionIndex];
180
+ const { input: source } = inputHistory[latestActionIndex];
177
181
  // Prefer custom executor is specified
178
182
  const result = this.executor
179
183
  ? await this.executor(source)
180
184
  : 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
185
  if (result.errors) {
187
186
  retryErrors.push(...result.errors);
188
- continue;
187
+ }
188
+ else {
189
+ outputHistory.push({
190
+ kind: activity_1.ActivityKind.Observation,
191
+ input: (0, yaml_1.stringify)(result),
192
+ });
189
193
  }
190
194
  // Continue to next iteration to check for more actions or generate new activities
191
195
  continue;
192
196
  }
193
197
  catch (e) {
194
198
  const err = e;
195
- outputHistory.push({
196
- kind: activity_1.ActivityKind.Observation,
197
- input: err.toString(),
198
- });
199
199
  const message = `Retry ${i + 1} due to action error: ${err}`;
200
200
  this.logger.debug(message);
201
201
  retryErrors.push(err);
202
202
  continue;
203
203
  }
204
204
  }
205
- // Generate new activities if no action needs execution
206
- const inputHistory = [...history, ...outputHistory];
205
+ // Apply transformers to input history before LLM inference
206
+ for (const transformer of this.inputTransformers) {
207
+ inputHistory = await transformer.transform(inputHistory);
208
+ }
207
209
  try {
208
210
  let newActivities = await this.transform(inputHistory);
209
211
  // Apply output transformers to generated activities
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@caretakerai/agent",
3
- "version": "0.0.37",
3
+ "version": "0.0.38-beta.1",
4
4
  "description": "Single framework for building text-agents",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",