openloops 0.9.0 → 0.9.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.
package/CHANGELOG.md CHANGED
@@ -2,6 +2,13 @@
2
2
 
3
3
  All notable changes to `openloops` are written here, newest first. The format follows [Keep a Changelog](https://keepachangelog.com) and the versions follow [semantic versioning](https://semver.org), where a minor version can still break the API while the package is in beta. Changes before 0.4.0 are not recorded here, see the commits and the tags.
4
4
 
5
+ ## 0.9.1
6
+
7
+ ### Changed
8
+
9
+ - A run blocked by `usageLimit` that continues an existing chat (`chatId`) now saves the message of the user in it, with no answer, and returns the chat in `data`. Before, nothing was saved and `data` was `undefined`. It still calls no guard, no model and no loop, the status of the chat does not change, a chat with no messages takes the message as its title, and the next run reads the saved message, so the user can say `continue` when the limit is lifted. With no `chatId`, or with one that does not exist, nothing is saved. Read [usage limit](docs/agent.md#usage-limit).
10
+ - The type of `AgentState.currentActivity` is `string`. It listed `'running'` and `'stopped'`, values from the activity that was removed in 0.8.0.
11
+
5
12
  ## 0.9.0
6
13
 
7
14
  ### Added
@@ -11,7 +11,7 @@ export interface AgentError {
11
11
  isRetryable: boolean;
12
12
  timestamp: Date;
13
13
  }
14
- type CurrentActivity = 'running' | 'stopped' | (string & {});
14
+ type CurrentActivity = string;
15
15
  export type ActivityListener = (activity: string) => void;
16
16
  export interface AgentStateProps {
17
17
  status?: AgentStatus;
@@ -119,6 +119,7 @@ export declare class Agent {
119
119
  private resolveConfirmation;
120
120
  private postRunner;
121
121
  private reportFailure;
122
+ private saveBlockedMessage;
122
123
  private usageLimitResult;
123
124
  private failRun;
124
125
  private fingerprint;
@@ -191,7 +191,7 @@ class Agent {
191
191
  try {
192
192
  await (0, import_usage_limit.checkUsageLimit)(usageLimit, currentUser);
193
193
  } catch (error) {
194
- if (error instanceof import_usage_limit.UsageLimitError) return this.usageLimitResult(error);
194
+ if (error instanceof import_usage_limit.UsageLimitError) return this.usageLimitResult(error, await this.saveBlockedMessage(chatId));
195
195
  throw error;
196
196
  }
197
197
  }
@@ -405,6 +405,22 @@ Ref: ${guard.ref}`, { source: "harness", excludeFromContext: true });
405
405
  async reportFailure(error, details = {}) {
406
406
  await (0, import_error.reportError)(error, { source: "agent", chatId: this.currentChat?._id?.toString(), userId: this.currentUser?.userId, ...details });
407
407
  }
408
+ async saveBlockedMessage(chatId) {
409
+ if (!chatId) return void 0;
410
+ try {
411
+ const data = await (0, import_chats.getChatById)({ id: chatId, currentUser: this.currentUser });
412
+ if (!data?._id) return void 0;
413
+ this.currentChat = import_chat.Chat.fromJSON(data);
414
+ if (!this.currentChat.messages?.length) this.currentChat.setTitle(this.currentMessage);
415
+ this.currentChat.pushMessage({ role: "user", content: this.currentMessage, source: "user" });
416
+ await this.saveChat({ resetStop: true });
417
+ return this.currentChat;
418
+ } catch (error) {
419
+ this.logger.error({ error: error?.message ?? error }, "[AGENT] Failed to save the message of a run blocked by the usage limit");
420
+ await this.reportFailure(error, { stage: "blocked-message" });
421
+ return void 0;
422
+ }
423
+ }
408
424
  usageLimitResult(error, data) {
409
425
  return { data, error: error.userMessage, errorCode: error.code, usageLimit: { used: error.used, limit: error.limit, period: error.period } };
410
426
  }
@@ -175,7 +175,7 @@ class Agent {
175
175
  try {
176
176
  await checkUsageLimit(usageLimit, currentUser);
177
177
  } catch (error) {
178
- if (error instanceof UsageLimitError) return this.usageLimitResult(error);
178
+ if (error instanceof UsageLimitError) return this.usageLimitResult(error, await this.saveBlockedMessage(chatId));
179
179
  throw error;
180
180
  }
181
181
  }
@@ -389,6 +389,22 @@ Ref: ${guard.ref}`, { source: "harness", excludeFromContext: true });
389
389
  async reportFailure(error, details = {}) {
390
390
  await reportError(error, { source: "agent", chatId: this.currentChat?._id?.toString(), userId: this.currentUser?.userId, ...details });
391
391
  }
392
+ async saveBlockedMessage(chatId) {
393
+ if (!chatId) return void 0;
394
+ try {
395
+ const data = await getChatById({ id: chatId, currentUser: this.currentUser });
396
+ if (!data?._id) return void 0;
397
+ this.currentChat = Chat.fromJSON(data);
398
+ if (!this.currentChat.messages?.length) this.currentChat.setTitle(this.currentMessage);
399
+ this.currentChat.pushMessage({ role: "user", content: this.currentMessage, source: "user" });
400
+ await this.saveChat({ resetStop: true });
401
+ return this.currentChat;
402
+ } catch (error) {
403
+ this.logger.error({ error: error?.message ?? error }, "[AGENT] Failed to save the message of a run blocked by the usage limit");
404
+ await this.reportFailure(error, { stage: "blocked-message" });
405
+ return void 0;
406
+ }
407
+ }
392
408
  usageLimitResult(error, data) {
393
409
  return { data, error: error.userMessage, errorCode: error.code, usageLimit: { used: error.used, limit: error.limit, period: error.period } };
394
410
  }
package/docs/agent.md CHANGED
@@ -90,15 +90,16 @@ const result = await agent.run({
90
90
 
91
91
  `period` is required and can be `day`, `week` (Monday to Sunday) or `month`. The period is calculated in the time zone of the user (UTC when it has none) and the spend is the cost of all the model calls of that user in it, from any chat and of any kind. The limit itself belongs to your application: `openloops` only checks it.
92
92
 
93
- Before the run starts, if the spend is equal to or greater than `maxCostUsd`, the run does not start and nothing is saved. The result has no `data` and carries the details:
93
+ Before the run starts, if the spend is equal to or greater than `maxCostUsd`, the run does not start: no guard, no model call and no loop run. When the run continues an existing chat (`chatId`), the message of the user is saved in it with no answer, the status of the chat does not change, and the chat is returned in `data`. A chat with no messages takes that message as its title. With no `chatId`, or with one that does not exist, nothing is saved and `data` is `undefined`. The result carries the details:
94
94
 
95
95
  ```typescript
96
+ result.data
96
97
  result.error
97
98
  result.errorCode
98
99
  result.usageLimit
99
100
  ```
100
101
 
101
- `errorCode` is `USAGE_LIMIT_REACHED` and `usageLimit` is `{ used, limit, period }`. This is an expected rejection, so it is not sent to the [error reporter](error-reporting.md).
102
+ `errorCode` is `USAGE_LIMIT_REACHED` and `usageLimit` is `{ used, limit, period }`. This is an expected rejection, so it is not sent to the [error reporter](error-reporting.md). The message that was saved is part of the history of the chat, so when the limit is lifted the next run reads it and the user can just say `continue`.
102
103
 
103
104
  A run that starts under the limit finishes even if it goes over it, so the last run of a period can spend a little more than `maxCostUsd`. To stop a run between the iterations of the loop, create the agent with `stopOnUsageLimit: true`. The chat is then marked as `failed` with `USAGE_LIMIT_REACHED` (not retryable) in `data.state.lastError`, and the result carries the same `errorCode` and `usageLimit`. It costs one query per iteration.
104
105
 
@@ -25,8 +25,9 @@ async function runAgentWithUsageLimit () {
25
25
  * 2. PASS IT IN THE RUN.
26
26
  *
27
27
  * Before the run starts, `openloops` adds the cost of every model call of the user in the period, from any chat. If the total
28
- * is equal to or greater than `maxCostUsd`, the run does not start and nothing is saved. A run that starts under the limit
29
- * finishes, even if it goes over it.
28
+ * is equal to or greater than `maxCostUsd`, the run does not start: nothing calls the model. When the run continues a chat
29
+ * (`chatId`), the message is saved in it with no answer, so the user can say `continue` when the limit is lifted. A run that
30
+ * starts under the limit finishes, even if it goes over it.
30
31
  */
31
32
  const result = await agent.run({
32
33
  input: { message: 'hi, how are you?' },
@@ -37,8 +38,8 @@ async function runAgentWithUsageLimit () {
37
38
  /**
38
39
  * 3. HANDLE THE REJECTION.
39
40
  *
40
- * `agent.run` does not throw. A blocked run comes back with `errorCode: 'USAGE_LIMIT_REACHED'`, no `data` and the amounts in
41
- * `usageLimit`. Answer your client with the status that fits, for example 429, and let the app show its own message. This
41
+ * `agent.run` does not throw. A blocked run comes back with `errorCode: 'USAGE_LIMIT_REACHED'`, the amounts in `usageLimit`
42
+ * and, when it continued a chat, the chat in `data` with the saved message. Answer your client with the status that fits, for example 429, and let the app show its own message. This
42
43
  * rejection is expected, so it is not sent to the error reporter.
43
44
  */
44
45
  if (result.errorCode === 'USAGE_LIMIT_REACHED') {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "openloops",
3
- "version": "0.9.0",
3
+ "version": "0.9.1",
4
4
  "description": "Open source harness for building multitenant AI agents for your SaaS: a TypeScript runtime with persistence, tools, MCP trust, approvals and autonomous automations",
5
5
  "author": {
6
6
  "name": "Jose Barcos",