@caretakerai/agent 0.0.38-beta.0 → 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 +20 -18
  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,50 +164,44 @@ 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
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 = history.findLastIndex(activity => activity.kind === activity_1.ActivityKind.Action);
166
- const latestObservationIndex = history.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);
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 } = history[latestActionIndex];
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
- continue;
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);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@caretakerai/agent",
3
- "version": "0.0.38-beta.0",
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",