openloops 0.7.6 → 0.8.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 +66 -11
- package/README.md +5 -8
- package/dist/clients/llm/llm-error.d.ts +1 -1
- package/dist/clients/llm/llm-error.js +12 -0
- package/dist/clients/llm/llm-error.mjs +12 -0
- package/dist/common/error/error-reporter.d.ts +11 -0
- package/dist/common/error/error-reporter.js +36 -0
- package/dist/common/error/error-reporter.mjs +11 -0
- package/dist/common/error/index.d.ts +1 -0
- package/dist/common/error/index.js +3 -1
- package/dist/common/error/index.mjs +1 -0
- package/dist/common/error/reportError.js +10 -0
- package/dist/common/error/reportError.mjs +10 -0
- package/dist/common/error/sentry-client.d.ts +2 -2
- package/dist/common/error/sentry-client.js +19 -24
- package/dist/common/error/sentry-client.mjs +19 -14
- package/dist/common/helpers/index.d.ts +1 -0
- package/dist/common/helpers/index.js +3 -1
- package/dist/common/helpers/index.mjs +1 -0
- package/dist/common/helpers/sanitize-string.js +5 -15
- package/dist/common/helpers/sanitize-string.mjs +5 -5
- package/dist/common/helpers/strip-blocks.d.ts +1 -0
- package/dist/common/helpers/strip-blocks.js +45 -0
- package/dist/common/helpers/strip-blocks.mjs +21 -0
- package/dist/common/index.d.ts +2 -0
- package/dist/common/index.js +9 -0
- package/dist/common/index.mjs +4 -0
- package/dist/common/logger/pino.js +12 -3
- package/dist/common/logger/pino.mjs +12 -3
- package/dist/common/repositories/mongo-db.client.js +1 -1
- package/dist/common/repositories/mongo-db.client.mjs +1 -1
- package/dist/common/repositories/mongo-db.repository.js +1 -1
- package/dist/common/repositories/mongo-db.repository.mjs +1 -1
- package/dist/common/utils/secret-manager.js +1 -1
- package/dist/common/utils/secret-manager.mjs +1 -1
- package/dist/db/index.d.ts +1 -0
- package/dist/db/index.js +3 -1
- package/dist/db/index.mjs +1 -0
- package/dist/db/migrations/chat-titles.d.ts +10 -0
- package/dist/db/migrations/chat-titles.js +55 -0
- package/dist/db/migrations/chat-titles.mjs +31 -0
- package/dist/harness/agent-state.d.ts +0 -1
- package/dist/harness/agent.d.ts +1 -0
- package/dist/harness/agent.js +9 -0
- package/dist/harness/agent.mjs +9 -0
- package/dist/loops/agi/loop.js +4 -2
- package/dist/loops/agi/loop.mjs +4 -2
- package/dist/loops/shared/skills/fast-responder.d.ts +1 -0
- package/dist/loops/shared/skills/fast-responder.js +4 -2
- package/dist/loops/shared/skills/fast-responder.mjs +4 -2
- package/dist/services/automations/runner/automation-runner.js +6 -1
- package/dist/services/automations/runner/automation-runner.mjs +6 -1
- package/dist/services/automations/use-cases/list-automation-runs-by-automation.use-case.d.ts +2 -1
- package/dist/services/automations/use-cases/list-automation-runs-by-automation.use-case.js +2 -2
- package/dist/services/automations/use-cases/list-automation-runs-by-automation.use-case.mjs +2 -2
- package/dist/services/automations/use-cases/sync-automation-run.use-case.d.ts +1 -0
- package/dist/services/automations/use-cases/sync-automation-run.use-case.js +6 -2
- package/dist/services/automations/use-cases/sync-automation-run.use-case.mjs +5 -2
- package/dist/services/chats/entities/chat.entity.d.ts +3 -0
- package/dist/services/chats/entities/chat.entity.js +8 -0
- package/dist/services/chats/entities/chat.entity.mjs +8 -0
- package/dist/services/chats/use-cases/get-chat-by-id.use-case.js +1 -5
- package/dist/services/chats/use-cases/get-chat-by-id.use-case.mjs +1 -5
- package/dist/services/chats/use-cases/list-chats-by-user.use-case.d.ts +2 -1
- package/dist/services/chats/use-cases/list-chats-by-user.use-case.js +3 -2
- package/dist/services/chats/use-cases/list-chats-by-user.use-case.mjs +3 -2
- package/dist/services/chats/utils/title.d.ts +2 -0
- package/dist/services/chats/utils/title.js +37 -0
- package/dist/services/chats/utils/title.mjs +12 -0
- package/dist/tools/core/fetch-url/html-to-text.js +3 -20
- package/dist/tools/core/fetch-url/html-to-text.mjs +3 -20
- package/docs/README.md +2 -1
- package/docs/agent.md +20 -3
- package/docs/automations.md +3 -3
- package/docs/chats.md +41 -8
- package/docs/configuration.md +6 -2
- package/docs/error-reporting.md +219 -0
- package/docs/integration-guide.md +6 -2
- package/docs/llms.txt +2 -1
- package/docs/loops.md +1 -1
- package/examples/chats/search-chats.ts +30 -0
- package/examples/errors/error-reporting.ts +75 -0
- package/package.json +23 -12
- package/dist/services/chats/utils/deprecated-activity.d.ts +0 -1
- package/dist/services/chats/utils/deprecated-activity.js +0 -60
- package/dist/services/chats/utils/deprecated-activity.mjs +0 -36
package/docs/chats.md
CHANGED
|
@@ -10,16 +10,11 @@ import { createChat, getChatById, listChatsByUser, updateChat, deleteChat } from
|
|
|
10
10
|
|
|
11
11
|
| Field | Meaning |
|
|
12
12
|
| --- | --- |
|
|
13
|
-
| `
|
|
13
|
+
| `title` | A short text that names the chat. See [the title](#the-title). |
|
|
14
|
+
| `state` | `status`, detected `language`, `activeTaskId`, `lastError` and `stopRequested`. See [agent](agent.md#the-result) for the statuses. Follow the activity of a run with [`agent.onActivity`](agent.md#activity-updates), it is not part of the chat. |
|
|
14
15
|
| `settings` | `modelProvider` and `modelName` (the model the chat uses, saved on every run), `thinkingEffort` (only when a run sent it), `loopName`, `executionMode`, `notifyOnCompletion` and `isPrivateSession`. |
|
|
15
16
|
| `messages` | The conversation. Each message has `role` (`user` or `assistant`), `source`, `content`, `answerId` and `excludeFromContext`. |
|
|
16
17
|
|
|
17
|
-
### `state.currentActivity` is deprecated
|
|
18
|
-
|
|
19
|
-
Since 0.3.0 the activity of a run is no longer saved with the chat, and `currentActivity` will be removed in 0.4.0. Follow a run with [`agent.onActivity`](agent.md#activity-updates), which tells every change as it happens.
|
|
20
|
-
|
|
21
|
-
For the clients that still poll the chat and read `state.currentActivity`, `getChatById` adds a generic word to it, such as `Thinking...` or `Cooking...`, only while the run is `processing`. It is one word per chat that changes every few seconds. It says nothing about what the agent is really doing, and it is not there in any other status. Move those clients to the listener before 0.4.0.
|
|
22
|
-
|
|
23
18
|
Every assistant message of one answer shares an `answerId`. It is the id you pass as `messageId` to [usage](usage.md).
|
|
24
19
|
|
|
25
20
|
`role` says on which side of the conversation a message is, and `source` says who produced it:
|
|
@@ -57,7 +52,45 @@ const interactive = await listChatsByUser({ executionMode: 'interactive', curren
|
|
|
57
52
|
const found = await listChatsByUser({ search: 'netflix', currentUser })
|
|
58
53
|
```
|
|
59
54
|
|
|
60
|
-
`search` is optional. It returns the chats
|
|
55
|
+
`search` is optional. It returns the chats whose title or at least one message contains the text, ignoring case, in the same order and with the same pagination. The text is matched literally, so characters such as `(` or `.` have no special meaning. It reads the messages without an index, which is fine for the chats of one user. An empty text is ignored.
|
|
56
|
+
|
|
57
|
+
```typescript
|
|
58
|
+
const history = await listChatsByUser({ search: 'netflix', select: { title: 1, updatedAt: 1 }, currentUser })
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
`select` is optional and limits the fields that come back, as in `getChatById`. Without it every chat comes complete, with all its messages. A history list only needs a few fields, and `_id` always comes. `search` does not depend on `select`: it still looks in the messages when they are not returned, and the chat that the user picks is loaded with `getChatById`. A history without messages cannot show the first message of a chat that has no title, so [migrate the old chats](#migrating-to-080).
|
|
62
|
+
|
|
63
|
+
## The title
|
|
64
|
+
|
|
65
|
+
In an interactive run the first node, `fast_responder`, writes a title for a chat that has none: at most 50 characters, in the language of the first message. The loop saves it on one line, without tags and cut to 80 characters, and never replaces a title that already exists. If the model returns nothing usable the chat stays without a title and the next message asks again. An autonomous run skips that node, so set the title yourself when you create its chat if you want one.
|
|
66
|
+
|
|
67
|
+
```typescript
|
|
68
|
+
await updateChat({ id: chatId, payload: { title: 'Trip to Lisbon' }, currentUser })
|
|
69
|
+
|
|
70
|
+
await updateChat({ id: chatId, payload: { settings: { notifyOnCompletion: true } }, unset: { title: '' }, currentUser })
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
The first call renames the chat, and the agent keeps that title from then on. The second removes the title, so the next message gets a new one.
|
|
74
|
+
|
|
75
|
+
## Migrating to 0.8.0
|
|
76
|
+
|
|
77
|
+
Chats saved with a version before 0.8.0 have no title. **Run `migrateChatTitles` once per database when you update.** It gives each chat without a title its first message of the user, on one line, without HTML tags and cut to 80 characters. Without it those chats stay untitled until they receive another message, and a history that lists with `select` has nothing to show for them, because it does not read the messages.
|
|
78
|
+
|
|
79
|
+
1. **Take a backup**, and run it with `dryRun` first. It only counts.
|
|
80
|
+
|
|
81
|
+
```typescript
|
|
82
|
+
import { migrateChatTitles } from 'openloops/db'
|
|
83
|
+
|
|
84
|
+
const report = await migrateChatTitles(db, { dryRun: true })
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
The report has `titled` (chats that get a title) and `skipped` (chats without a message of the user, which the first message they receive will title).
|
|
88
|
+
2. **Run it for real**, with `dryRun: false` or without the option. It can be run again without changing anything, because it only touches chats without a title, and it skips the deleted ones. In a checkout of this repository you can run `yarn migrate:chat-titles --dry-run` and `yarn migrate:chat-titles`, which read `OPENLOOPS_DB_URI` and `OPENLOOPS_DB_NAME`.
|
|
89
|
+
3. **Show something for a chat with no title**, such as "Untitled": a new chat has none until its first message is processed, a chat whose title the model did not return gets it in the next message, and an autonomous chat has none unless you set it.
|
|
90
|
+
4. **Stop reading `state.currentActivity`.** It is gone from the chat and `getChatById` no longer adds a generic word while a run is `processing`. Follow the run with [`agent.onActivity`](agent.md#activity-updates) and use `state.status` to know whether it is still going. The versions of your app that still poll the chat need to be updated before you update `openloops`.
|
|
91
|
+
5. **Handle the new error codes** in `state.lastError.code`: `LLM_AUTH_FAILED`, `LLM_CONTEXT_TOO_LONG`, `LLM_CONTENT_FILTERED` and `LLM_INCOMPLETE_RESPONSE` used to be `LLM_FAILED`. See [the list](agent.md#error-handling).
|
|
92
|
+
|
|
93
|
+
The changelog also lists two smaller changes of this version: `sanitizeString` no longer decodes HTML entities, and `pino-pretty` is now a dev dependency.
|
|
61
94
|
|
|
62
95
|
## Getting one chat
|
|
63
96
|
|
package/docs/configuration.md
CHANGED
|
@@ -19,9 +19,9 @@
|
|
|
19
19
|
| `LLM_FALLBACK_PROVIDER`, `LLM_FALLBACK_MODEL` | no | The model to use when nothing else chooses one, for the whole deployment. Set both or neither. See [Which model a chat uses](#which-model-a-chat-uses). |
|
|
20
20
|
| `LLM_CALL_TIMEOUT_MS` | no | Longest time to wait for one attempt of a model call, in milliseconds. Defaults to `60000`, and calls with the `high` thinking effort get twice as much. See [Timeouts](#timeouts). |
|
|
21
21
|
| `MCP_TOOL_TIMEOUT_MS` | no | Longest time to wait for a tool of an MCP server, in milliseconds. Defaults to `60000`. See [Timeouts](#timeouts). |
|
|
22
|
-
| `SENTRY_DSN` | no | Errors are reported to Sentry when set, unless `IS_LOCAL` is `true` or `NODE_ENV` is `development`. |
|
|
22
|
+
| `SENTRY_DSN` | no | **Deprecated.** Errors are reported to Sentry when set and no reporter is registered, unless `IS_LOCAL` is `true` or `NODE_ENV` is `development`. It needs `@sentry/node` installed in your project. Use [`setErrorReporter`](error-reporting.md) instead. |
|
|
23
23
|
| `LANGFUSE_PUBLIC_KEY`, `LANGFUSE_SECRET_KEY`, `LANGFUSE_BASE_URL` | no | Traces every LLM call in Langfuse when all three are set. |
|
|
24
|
-
| `IS_LOCAL`, `NODE_ENV` | no | Local development switches. |
|
|
24
|
+
| `IS_LOCAL`, `NODE_ENV` | no | Local development switches. With `IS_LOCAL` set to `true`, or `NODE_ENV` set to `development`, the logger prints colored logs if `pino-pretty` is installed in your project (it is a dev dependency of this package, so install it yourself: `yarn add -D pino-pretty`), and plain JSON otherwise. |
|
|
25
25
|
|
|
26
26
|
Any value can be stored encrypted by prefixing it with `ENC:`. It is decrypted with `SECRET_MANAGER_KEY` when it is read.
|
|
27
27
|
|
|
@@ -95,6 +95,10 @@ await client.close()
|
|
|
95
95
|
|
|
96
96
|
It covers `llm_calls`, `chats`, `agent_tasks`, `agent_actions`, `agent_traces`, `workspace_items`, `mcp_servers`, `automations` and `automation_runs`. It is safe to run again: an index that already exists is left as it is. Collections of your own application are not included.
|
|
97
97
|
|
|
98
|
+
## Reporting errors
|
|
99
|
+
|
|
100
|
+
The package does not choose a tool to report errors: you declare one function, once, and it receives the errors that the package catches. Read [reporting errors](error-reporting.md).
|
|
101
|
+
|
|
98
102
|
## What is recorded
|
|
99
103
|
|
|
100
104
|
- Every LLM call, with its response, tokens and cost. See [usage](usage.md).
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
# Reporting errors
|
|
2
|
+
|
|
3
|
+
`openloops` does not choose a tool to report errors. You declare one function, once, and the package calls it every time it catches an error that would otherwise only be written in the log. What the function does is up to you: Sentry, Datadog, Slack, a file.
|
|
4
|
+
|
|
5
|
+
- [Declaring the reporter](#declaring-the-reporter)
|
|
6
|
+
- [What you do not have to do](#what-you-do-not-have-to-do)
|
|
7
|
+
- [A run that fails](#a-run-that-fails)
|
|
8
|
+
- [What the reporter receives](#what-the-reporter-receives)
|
|
9
|
+
- [What is reported and what is not](#what-is-reported-and-what-is-not)
|
|
10
|
+
- [Rules of the reporter](#rules-of-the-reporter)
|
|
11
|
+
- [Filtering and adding context](#filtering-and-adding-context)
|
|
12
|
+
- [Lambdas and other serverless deployments](#lambdas-and-other-serverless-deployments)
|
|
13
|
+
- [Tests](#tests)
|
|
14
|
+
- [Moving from `SENTRY_DSN`](#moving-from-sentry_dsn)
|
|
15
|
+
|
|
16
|
+
## Declaring the reporter
|
|
17
|
+
|
|
18
|
+
```typescript
|
|
19
|
+
import * as Sentry from '@sentry/node'
|
|
20
|
+
import { setErrorReporter } from 'openloops/common'
|
|
21
|
+
|
|
22
|
+
Sentry.init({ dsn: process.env.MY_SENTRY_DSN })
|
|
23
|
+
|
|
24
|
+
setErrorReporter((error, details) => {
|
|
25
|
+
Sentry.withScope(scope => {
|
|
26
|
+
scope.setTag('source', details.source)
|
|
27
|
+
scope.setExtras(details)
|
|
28
|
+
Sentry.captureException(error)
|
|
29
|
+
})
|
|
30
|
+
})
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
**Declare it once.** Do it when the process starts, before the first request: at the top of the file that starts your server, or in a file of its own that your server imports first. That is all the setup there is.
|
|
34
|
+
|
|
35
|
+
- It is global to the process. Every `Agent`, every use case and the runner of the [automations](automations.md) that run in that process use it, with nothing to pass to them.
|
|
36
|
+
- Do not declare it inside a request, a use case or next to each `Agent`. It would work, but it would be a function replaced on every request for nothing.
|
|
37
|
+
- If you declare it twice, the second one replaces the first. There is only one reporter at a time.
|
|
38
|
+
- `setErrorReporter(undefined)` removes it. Without a reporter, the errors only go to the log, unless you still use the deprecated [`SENTRY_DSN`](#moving-from-sentry_dsn).
|
|
39
|
+
- A deployment with several processes (several servers, or lambdas) needs the declaration in each process. See [lambdas](#lambdas-and-other-serverless-deployments).
|
|
40
|
+
|
|
41
|
+
## What you do not have to do
|
|
42
|
+
|
|
43
|
+
After the declaration the rest of your code stays as it was. There is no option in the `Agent`, no wrapper around the use cases and no `try/catch` added only to report.
|
|
44
|
+
|
|
45
|
+
```typescript
|
|
46
|
+
import './error-reporting'
|
|
47
|
+
import { Agent } from 'openloops/core'
|
|
48
|
+
import { AgiLoop } from 'openloops/loops'
|
|
49
|
+
|
|
50
|
+
const agent = new Agent({ loop: new AgiLoop() })
|
|
51
|
+
|
|
52
|
+
const result = await agent.run({ input: { message: 'hi, how are you?' }, currentUser })
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
The first line is the file that declares the reporter. If this run fails, the reporter is called without anything else in this code. The [example](../examples/errors/error-reporting.ts) has the declaration at the top and the code that runs below it, in one file.
|
|
56
|
+
|
|
57
|
+
## A run that fails
|
|
58
|
+
|
|
59
|
+
Take a run whose provider rejects the API key. `agent.run` does not throw: it saves the chat as `failed`, returns the message for the user in `result.error` and calls the reporter. Your reporter receives something like this (the `code` depends on what the provider answers):
|
|
60
|
+
|
|
61
|
+
```typescript
|
|
62
|
+
error // the LLMError, with the message of the provider
|
|
63
|
+
details // { source: 'agent', code: 'LLM_AUTH_FAILED', chatId: '6abc454162624f9e01929cd8', userId: '6ab56894eb20887d1ef7942c' }
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
The chat is saved before the reporter is called, so a slow reporter never delays that. A run that goes well never calls the reporter.
|
|
67
|
+
|
|
68
|
+
## What the reporter receives
|
|
69
|
+
|
|
70
|
+
The function has two parameters:
|
|
71
|
+
|
|
72
|
+
| Parameter | Meaning |
|
|
73
|
+
| --- | --- |
|
|
74
|
+
| `error` | The `Error` that was caught, or an `HttpError` if the code threw one. A text becomes an `Error`. |
|
|
75
|
+
| `details` | An object that tells where it happened. It always has `source`. The other fields appear when they exist. |
|
|
76
|
+
|
|
77
|
+
The fields of `details`:
|
|
78
|
+
|
|
79
|
+
| Field | Meaning |
|
|
80
|
+
| --- | --- |
|
|
81
|
+
| `source` | Where the error was caught: `agent`, `automations`, `mongo` or `openloops`. See the next table. |
|
|
82
|
+
| `code` | For a failure of the model, the code that is also saved in `state.lastError.code`, such as `LLM_RATE_LIMITED` or `LLM_CONTEXT_TOO_LONG`. See [error handling](agent.md#error-handling). |
|
|
83
|
+
| `chatId`, `userId` | The chat and the user of the run, when there is one. |
|
|
84
|
+
| `collection` | In `mongo`, the collection of the call that failed. |
|
|
85
|
+
| `stage` | Where in the process it failed, in `agent` and in `automations`. See the next table. |
|
|
86
|
+
| `guard` | In `agent`, the name of the guard that threw. |
|
|
87
|
+
| `automationId`, `runId` | In `automations`, the automation and the run. |
|
|
88
|
+
| `listener` | In `agent`, `activity` when a listener of `onActivity` threw. |
|
|
89
|
+
|
|
90
|
+
The values of `source`:
|
|
91
|
+
|
|
92
|
+
| `source` | What happened | Extra fields |
|
|
93
|
+
| --- | --- | --- |
|
|
94
|
+
| `agent` | A run failed, a listener of `onActivity` threw, a [guard](guardrails.md) threw, or the agent could not save the traces or the failed state of the chat. `stage` is `guard`, `traces` or `persist` in the last three, and it is missing when the run itself failed. | `chatId`, `userId`, `code` if the model failed, `stage`, `guard` and `listener` as they apply. |
|
|
95
|
+
| `automations` | The runner of the [automations](automations.md#the-runner) caught an error. `stage` is `sync` (checking the open runs), `tick` (looking for what is due), `run` (a run threw) or `onRunFinished` (your own callback threw). | `stage`, and `automationId` and `runId` in `run`. |
|
|
96
|
+
| `mongo` | A call to the database failed, or the connection did. | `collection` in a call. |
|
|
97
|
+
| `openloops` | Any other error that is reported without more detail. | |
|
|
98
|
+
|
|
99
|
+
## What is reported and what is not
|
|
100
|
+
|
|
101
|
+
The reporter gets what the package catches **by itself**: places where, if it did not report, nobody would ever know.
|
|
102
|
+
|
|
103
|
+
- **A failed run.** `agent.run` returns the error instead of throwing it, so without the reporter you would only see it if you read `result.error`.
|
|
104
|
+
- **A listener of `onActivity` that throws.** The run goes on, so the error would be lost.
|
|
105
|
+
- **A guard that throws.** The request is closed with a generic reply, so the real error would only be in the log.
|
|
106
|
+
- **Traces or the failed state that cannot be saved.** The run does not stop for them, so you would lose them without knowing.
|
|
107
|
+
- **The runner of the automations.** It runs in the background, with nobody waiting for an answer.
|
|
108
|
+
- **A failed call to the database.** The repository reports it and then throws it. You receive it in both places, see below.
|
|
109
|
+
|
|
110
|
+
What is not reported are the errors that the use cases throw, such as a missing resource (`404`), a conflict (`409`) or invalid data (`400`). Those reach the code that called the use case, and that code answers your client and decides whether it is worth reporting.
|
|
111
|
+
|
|
112
|
+
```typescript
|
|
113
|
+
try {
|
|
114
|
+
await updateChat({ id, payload: { title }, currentUser })
|
|
115
|
+
} catch (error) {
|
|
116
|
+
res.status(error.statusCode ?? 500).json({ message: error.errorMessage })
|
|
117
|
+
}
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
**A failure of the database arrives twice** if you report it in your own `catch` too: once from the repository, with `source: 'mongo'`, and once from your code. Choose one. The simplest is to report in the reporter only, and in your `catch` only answer your client.
|
|
121
|
+
|
|
122
|
+
## Rules of the reporter
|
|
123
|
+
|
|
124
|
+
- **It is awaited.** `openloops` waits for the function to finish, so an `async` reporter that sends the event to a service holds the caller until it ends. Keep it quick, or do not `await` the slow part if you do not need to.
|
|
125
|
+
- **It is protected.** An error thrown by the reporter, synchronous or not, is written in the log and ignored. A reporter that fails never breaks a run or a use case.
|
|
126
|
+
- **It receives only `error` and `details`.** The package does not pass the request, the messages of the user or the arguments of the tools.
|
|
127
|
+
- **It receives more than you may want.** Failures that are not a problem of yours, such as `LLM_RATE_LIMITED` or `LLM_QUOTA_EXCEEDED`, arrive too. Filter them, see the next section.
|
|
128
|
+
|
|
129
|
+
## Filtering and adding context
|
|
130
|
+
|
|
131
|
+
Because the reporter is a plain function, filtering is an `if`:
|
|
132
|
+
|
|
133
|
+
```typescript
|
|
134
|
+
setErrorReporter((error, details) => {
|
|
135
|
+
if (details.code === 'LLM_RATE_LIMITED' || details.code === 'LLM_QUOTA_EXCEEDED') return
|
|
136
|
+
if (details.source === 'automations' && details.stage === 'onRunFinished') return
|
|
137
|
+
|
|
138
|
+
Sentry.withScope(scope => {
|
|
139
|
+
scope.setTag('source', details.source)
|
|
140
|
+
if (details.userId) scope.setUser({ id: details.userId })
|
|
141
|
+
scope.setExtras(details)
|
|
142
|
+
Sentry.captureException(error)
|
|
143
|
+
})
|
|
144
|
+
})
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
Send `userId` and `chatId` if your privacy rules allow it: they are the way to find the chat that failed. `details` never carries the messages of the user or the arguments of the tools.
|
|
148
|
+
|
|
149
|
+
## Lambdas and other serverless deployments
|
|
150
|
+
|
|
151
|
+
Every function is its own process and does not share memory with the others. A declaration in one of them does not reach the rest, so each handler that uses `openloops` must run the declaration.
|
|
152
|
+
|
|
153
|
+
The simple way is a file of its own that declares the reporter, `error-reporting-lambda.ts`:
|
|
154
|
+
|
|
155
|
+
```typescript
|
|
156
|
+
import * as Sentry from '@sentry/node'
|
|
157
|
+
import { setErrorReporter } from 'openloops/common'
|
|
158
|
+
|
|
159
|
+
Sentry.init({ dsn: process.env.MY_SENTRY_DSN })
|
|
160
|
+
|
|
161
|
+
setErrorReporter(async (error, details) => {
|
|
162
|
+
Sentry.withScope(scope => {
|
|
163
|
+
scope.setTag('source', details.source)
|
|
164
|
+
scope.setExtras(details)
|
|
165
|
+
Sentry.captureException(error)
|
|
166
|
+
})
|
|
167
|
+
|
|
168
|
+
await Sentry.flush(2000)
|
|
169
|
+
})
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
Imported at the top of every handler:
|
|
173
|
+
|
|
174
|
+
```typescript
|
|
175
|
+
import './error-reporting-lambda'
|
|
176
|
+
import { listChatsByUser } from 'openloops/chats'
|
|
177
|
+
|
|
178
|
+
export const handler = async (event: any) => {
|
|
179
|
+
const result = await listChatsByUser({ options: { page: 1, limit: 10 }, currentUser })
|
|
180
|
+
|
|
181
|
+
return { statusCode: 200, body: JSON.stringify(result) }
|
|
182
|
+
}
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
- Importing the same file from many handlers is fine: it runs once in each process.
|
|
186
|
+
- Put the import in a module that every file that calls `openloops` already imports, if you have one (a helper that builds the current user, for example), and you will not forget it in a new handler.
|
|
187
|
+
- A lambda can be frozen as soon as the handler returns. Wait for your tool to send the event inside the reporter (with Sentry, `await Sentry.flush(2000)`), because `openloops` awaits the reporter and that keeps the handler alive until then.
|
|
188
|
+
|
|
189
|
+
## Tests
|
|
190
|
+
|
|
191
|
+
The reporter lives in the process, so a test that declares one must remove it:
|
|
192
|
+
|
|
193
|
+
```typescript
|
|
194
|
+
const reporter = jest.fn()
|
|
195
|
+
|
|
196
|
+
beforeEach(() => setErrorReporter(reporter))
|
|
197
|
+
afterEach(() => setErrorReporter(undefined))
|
|
198
|
+
|
|
199
|
+
it('reports a failed run', async () => {
|
|
200
|
+
await agent.run({ input: { message: 'hi' }, currentUser })
|
|
201
|
+
|
|
202
|
+
expect(reporter).toHaveBeenCalledWith(expect.any(Error), expect.objectContaining({ source: 'agent' }))
|
|
203
|
+
})
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
## Moving from `SENTRY_DSN`
|
|
207
|
+
|
|
208
|
+
Reporting to Sentry with `SENTRY_DSN` still works, but it is deprecated and will be removed in a future version. Two things changed:
|
|
209
|
+
|
|
210
|
+
- `@sentry/node` is no longer installed with the package. It is an optional peer dependency, loaded only when `SENTRY_DSN` is set and no reporter is registered. If you rely on `SENTRY_DSN`, install it in your project: `yarn add @sentry/node`.
|
|
211
|
+
- The package no longer starts Sentry when it is imported. It starts it the first time it has an error to report, and it warns once in the log that this way is deprecated.
|
|
212
|
+
|
|
213
|
+
To move to the reporter:
|
|
214
|
+
|
|
215
|
+
1. Install `@sentry/node` in your project and call `Sentry.init` yourself.
|
|
216
|
+
2. Declare the reporter as shown [above](#declaring-the-reporter).
|
|
217
|
+
3. Remove `SENTRY_DSN` from the environment of `openloops`.
|
|
218
|
+
|
|
219
|
+
A registered reporter turns the old way off, so nothing is sent twice while you move. If your application already starts Sentry by itself, this also ends the second start that `openloops` made when `SENTRY_DSN` was set.
|
|
@@ -33,7 +33,7 @@ The use cases check who owns every record, in the repository, with the `currentU
|
|
|
33
33
|
| --- | --- |
|
|
34
34
|
| Send a message | `new Agent({ loop, tools, guards }).run({ input: { message, chatId? }, currentUser })` |
|
|
35
35
|
| Stop a running message | `Agent.interruptExecution(chatId, currentUser)` |
|
|
36
|
-
| List, read, delete chats | `listChatsByUser` (with `executionMode` to separate interactive from autonomous), `getChatById`, `deleteChat` |
|
|
36
|
+
| List, read, delete chats | `listChatsByUser` (with `executionMode` to separate interactive from autonomous, `search` for the title and the messages, and `select` for a light history list), `getChatById`, `updateChat` to rename a chat with `title`, `deleteChat` |
|
|
37
37
|
| Poll the state of a chat | `getChatById({ id, select: { state: 1 } })` |
|
|
38
38
|
| Manage MCP servers | `createMcpServer`, `listMcpServersByUser`, `getMcpServerById`, `updateMcpServer`, `deleteMcpServer`, `verifyMcpServerConnection` |
|
|
39
39
|
| Approve or block an MCP server from a screen | `decideMcpServerTrust` |
|
|
@@ -84,10 +84,14 @@ Show `data.messages`, and look at `data.state.status`:
|
|
|
84
84
|
| `processing` | A run is in progress. Show a loading state and offer to stop it. |
|
|
85
85
|
| `awaiting_user_input` | The agent asked a question. The answer is sent as a normal message in the same chat. |
|
|
86
86
|
| `awaiting_user_confirmation` | The agent needs an approval, for a destructive tool or for an MCP server. The question is the last message, and the answer is sent as a normal message in the same chat. |
|
|
87
|
-
| `failed` | Show `data.state.lastError.message` and let the user try again. |
|
|
87
|
+
| `failed` | Show `data.state.lastError.message` and let the user try again, unless `lastError.isRetryable` is `false`. `lastError.code` says what failed, see [error handling](agent.md#error-handling). |
|
|
88
88
|
|
|
89
89
|
Messages have a `source` (`user`, `model` or `harness`) and some have `excludeFromContext`. Messages with `source: 'harness'` are written by the system, for example a guardrail reply or an approval question, and you may want to style them differently. See [chats](chats.md).
|
|
90
90
|
|
|
91
|
+
## Reporting errors
|
|
92
|
+
|
|
93
|
+
Register one reporter when the process starts, before the first request, with `setErrorReporter` from `openloops/common`, and send what it receives to Sentry or to the tool you use. A failed run, a failed database call and the errors of the automations runner come to it. In a serverless deployment, import it at the top of each handler that uses `openloops`. Read [reporting errors](error-reporting.md).
|
|
94
|
+
|
|
91
95
|
## MCP servers
|
|
92
96
|
|
|
93
97
|
Your users register their own servers, and every server must be approved before the agent runs any of its tools. The approval normally happens in the chat. If you also want a screen, call `decideMcpServerTrust` with the hash of the manifest the user reviewed (`history[0].hash`). See [MCP servers](mcp-servers.md).
|
package/docs/llms.txt
CHANGED
|
@@ -14,7 +14,8 @@ Start with the integration guide, then read only the pages you need. All pages a
|
|
|
14
14
|
|
|
15
15
|
- [The Agent](agent.md): options, running a turn, statuses, confirmations, autonomous mode
|
|
16
16
|
- [Configuration](configuration.md): environment variables, default model, timeouts
|
|
17
|
-
- [
|
|
17
|
+
- [Reporting errors](error-reporting.md): `setErrorReporter`, what it receives, what is reported, serverless and moving from `SENTRY_DSN`
|
|
18
|
+
- [Chats](chats.md): the chat record, its title, message source, listing, searching and deleting, and the migration to 0.8.0
|
|
18
19
|
|
|
19
20
|
## Shape the agent
|
|
20
21
|
|
package/docs/loops.md
CHANGED
|
@@ -13,7 +13,7 @@ import { AgiLoop } from 'openloops/loops'
|
|
|
13
13
|
|
|
14
14
|
| Node | What it does |
|
|
15
15
|
| --- | --- |
|
|
16
|
-
| `fast_responder` | Initial node. Answers directly when no tools or memory are needed, and when it cannot, says in one sentence what the agent will do. It is skipped in an autonomous run. |
|
|
16
|
+
| `fast_responder` | Initial node. Answers directly when no tools or memory are needed, and when it cannot, says in one sentence what the agent will do. It also writes the [title](chats.md#the-title) of a chat that has none. It is skipped in an autonomous run. |
|
|
17
17
|
| `reasoning_engine` | Reads the situation or the results of the last actions, rewrites its working state, decides the next actions, reports the language of the conversation and says in one sentence what is happening until its next answer. |
|
|
18
18
|
| `action_engine` | Runs the actions the reasoning engine asked for, asks the user for confirmation when needed, delivers the final answer. |
|
|
19
19
|
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { Logger } from 'openloops/common'
|
|
2
|
+
import { CurrentUser } from 'openloops/base'
|
|
3
|
+
import { getChatById, listChatsByUser } from 'openloops/chats'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Searches the history of the user. `search` looks in the title and in the messages, and `select` returns only what a list needs,
|
|
7
|
+
* so the messages do not travel. Once the user picks a chat, it is loaded complete.
|
|
8
|
+
*/
|
|
9
|
+
async function searchChats () {
|
|
10
|
+
const logger = new Logger()
|
|
11
|
+
const currentUser = await CurrentUser.asyncFromDB('6ab56894eb20887d1ef7942c')
|
|
12
|
+
|
|
13
|
+
const history = await listChatsByUser({
|
|
14
|
+
search: 'netflix',
|
|
15
|
+
select: { title: 1, updatedAt: 1 },
|
|
16
|
+
options: { page: 1, limit: 10 },
|
|
17
|
+
currentUser,
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
logger.info(history.items.map(chat => ({ id: chat._id, title: chat.title ?? 'Untitled', updatedAt: chat.updatedAt })), '[Demo] History')
|
|
21
|
+
|
|
22
|
+
const picked = history.items[0]?._id
|
|
23
|
+
if (!picked) return
|
|
24
|
+
|
|
25
|
+
const chat = await getChatById({ id: String(picked), currentUser })
|
|
26
|
+
|
|
27
|
+
logger.info(chat?.messages, '[Demo] Messages')
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
searchChats()
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import * as Sentry from '@sentry/node'
|
|
2
|
+
import { Agent } from 'openloops/core'
|
|
3
|
+
import { Logger, setErrorReporter } from 'openloops/common'
|
|
4
|
+
import { CurrentUser } from 'openloops/base'
|
|
5
|
+
import { AgiLoop } from 'openloops/loops'
|
|
6
|
+
import { listChatsByUser } from 'openloops/chats'
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* 1. DECLARE THE REPORTER, ONCE.
|
|
10
|
+
*
|
|
11
|
+
* Do it when the process starts and before the first request: at the top of the file that starts your server, or in a file that
|
|
12
|
+
* your server imports first. It is global to the process, so there is nothing to pass to the `Agent`, to the use cases or to the
|
|
13
|
+
* runner of the automations: they find the reporter by themselves. If you declare it twice, the second replaces the first.
|
|
14
|
+
*
|
|
15
|
+
* The reporter receives the error and `details`. `details.source` is always there (`agent`, `mongo`, `automations` or
|
|
16
|
+
* `openloops`), and `code`, `chatId` and `userId` come when they exist.
|
|
17
|
+
*/
|
|
18
|
+
Sentry.init({ dsn: process.env.MY_SENTRY_DSN })
|
|
19
|
+
|
|
20
|
+
setErrorReporter((error, details) => {
|
|
21
|
+
if (details.code === 'LLM_RATE_LIMITED' || details.code === 'LLM_QUOTA_EXCEEDED') return
|
|
22
|
+
|
|
23
|
+
Sentry.withScope(scope => {
|
|
24
|
+
scope.setTag('source', details.source)
|
|
25
|
+
if (details.userId) scope.setUser({ id: details.userId })
|
|
26
|
+
scope.setExtras(details)
|
|
27
|
+
Sentry.captureException(error)
|
|
28
|
+
})
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* 2. RUN YOUR CODE AS ALWAYS.
|
|
33
|
+
*
|
|
34
|
+
* Nothing below mentions the reporter. There is no option in the `Agent`, no wrapper around the use case and no try/catch added
|
|
35
|
+
* to report.
|
|
36
|
+
*/
|
|
37
|
+
async function main () {
|
|
38
|
+
const logger = new Logger()
|
|
39
|
+
const currentUser = await CurrentUser.asyncFromDB('6ab56894eb20887d1ef7942c')
|
|
40
|
+
|
|
41
|
+
if (!currentUser) throw new Error('[Demo] current user is not defined')
|
|
42
|
+
|
|
43
|
+
const agent = new Agent({ loop: new AgiLoop() })
|
|
44
|
+
|
|
45
|
+
const result = await agent.run({
|
|
46
|
+
input: {
|
|
47
|
+
message: 'hi, how are you?',
|
|
48
|
+
},
|
|
49
|
+
currentUser,
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* `agent.run` does not throw. If the run fails, for example because the provider rejects the API key, openloops saves the
|
|
54
|
+
* chat as `failed`, returns the message for the user in `result.error` and calls the reporter above with
|
|
55
|
+
* `{ source: 'agent', code: 'LLM_AUTH_FAILED', chatId, userId }` (the code depends on what the provider answers).
|
|
56
|
+
* A run that goes well never calls the reporter.
|
|
57
|
+
*/
|
|
58
|
+
logger.info({ error: result.error, status: result.data?.state?.status }, '[Demo] Run')
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* A use case throws its errors and they reach this `catch`: you decide what to answer to your client. What the reporter
|
|
62
|
+
* receives is what openloops catches by itself. A failed call to the database is one of those cases: the repository calls
|
|
63
|
+
* the reporter with `{ source: 'mongo', collection: 'chats' }` and then throws the error. If this `catch` reported it too,
|
|
64
|
+
* Sentry would get it twice, so report the errors of the database in one of the two places.
|
|
65
|
+
*/
|
|
66
|
+
try {
|
|
67
|
+
const chats = await listChatsByUser({ options: { page: 1, limit: 10 }, currentUser })
|
|
68
|
+
|
|
69
|
+
logger.info(chats.items.map(chat => ({ id: chat._id, status: chat.state?.status })), '[Demo] Chats')
|
|
70
|
+
} catch (error: any) {
|
|
71
|
+
logger.error(error, '[Demo] The list could not be read')
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
main()
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "openloops",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.1",
|
|
4
4
|
"description": "Open source harness for AI agents: a TypeScript runtime with persistence, tools, MCP trust, approvals and autonomous automations, where the loop is pluggable",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "Jose Barcos",
|
|
@@ -8,6 +8,14 @@
|
|
|
8
8
|
"url": "https://github.com/jbarcosdev"
|
|
9
9
|
},
|
|
10
10
|
"license": "Apache-2.0",
|
|
11
|
+
"homepage": "https://openloops.xyz",
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "git+https://github.com/jbarcosdev/openloops.git"
|
|
15
|
+
},
|
|
16
|
+
"bugs": {
|
|
17
|
+
"url": "https://github.com/jbarcosdev/openloops/issues"
|
|
18
|
+
},
|
|
11
19
|
"main": "dist/index.js",
|
|
12
20
|
"types": "dist/index.d.ts",
|
|
13
21
|
"exports": {
|
|
@@ -96,6 +104,7 @@
|
|
|
96
104
|
"test:watch": "jest --watch",
|
|
97
105
|
"test:coverage": "jest --coverage",
|
|
98
106
|
"migrate:automations": "ts-node scripts/migrate-automations.ts",
|
|
107
|
+
"migrate:chat-titles": "ts-node scripts/migrate-chat-titles.ts",
|
|
99
108
|
"eval": "ts-node --project evals/tsconfig.json evals/run.ts",
|
|
100
109
|
"eval:models": "ts-node --project evals/tsconfig.json evals/run.ts models",
|
|
101
110
|
"eval:skills": "ts-node --project evals/tsconfig.json evals/run.ts skills",
|
|
@@ -106,9 +115,8 @@
|
|
|
106
115
|
"eval:list": "ts-node --project evals/tsconfig.json evals/run.ts list"
|
|
107
116
|
},
|
|
108
117
|
"dependencies": {
|
|
109
|
-
"@earendil-works/pi-ai": "^1.
|
|
118
|
+
"@earendil-works/pi-ai": "^1.5.0",
|
|
110
119
|
"@modelcontextprotocol/sdk": "^1.32.2",
|
|
111
|
-
"@sentry/node": "^11.10.0",
|
|
112
120
|
"bcryptjs": "^3.0.3",
|
|
113
121
|
"croner": "^11.0.0",
|
|
114
122
|
"cryptr": "^6.4.0",
|
|
@@ -117,28 +125,31 @@
|
|
|
117
125
|
"jsonwebtoken": "^9.0.3",
|
|
118
126
|
"langfuse": "^3.39.2",
|
|
119
127
|
"mongodb": "^7.8.0",
|
|
120
|
-
"pino": "^10.4.0"
|
|
121
|
-
"pino-pretty": "^13.2.0",
|
|
122
|
-
"sanitize-html": "^2.18.1"
|
|
128
|
+
"pino": "^10.4.0"
|
|
123
129
|
},
|
|
124
130
|
"devDependencies": {
|
|
131
|
+
"@sentry/node": "^11.11.0",
|
|
125
132
|
"@types/jest": "^30.0.0",
|
|
126
133
|
"@types/node": "^26.6.6",
|
|
127
|
-
"@types/sanitize-html": "^2.16.2",
|
|
128
134
|
"jest": "^30.5.2",
|
|
135
|
+
"pino-pretty": "^13.2.0",
|
|
129
136
|
"ts-jest": "^29.4.15",
|
|
130
|
-
"tsc-alias": "^1.9.
|
|
137
|
+
"tsc-alias": "^1.9.10",
|
|
131
138
|
"tsconfig-paths": "^4.2.0",
|
|
132
139
|
"tsup": "^9.0.0",
|
|
133
140
|
"typescript": "^6.0.3"
|
|
134
141
|
},
|
|
142
|
+
"peerDependencies": {
|
|
143
|
+
"@sentry/node": "^11.11.0"
|
|
144
|
+
},
|
|
145
|
+
"peerDependenciesMeta": {
|
|
146
|
+
"@sentry/node": {
|
|
147
|
+
"optional": true
|
|
148
|
+
}
|
|
149
|
+
},
|
|
135
150
|
"engines": {
|
|
136
151
|
"node": ">=22.0.0"
|
|
137
152
|
},
|
|
138
|
-
"repository": {
|
|
139
|
-
"type": "git",
|
|
140
|
-
"url": "git+https://github.com/jbarcosdev/openloops.git"
|
|
141
|
-
},
|
|
142
153
|
"keywords": [
|
|
143
154
|
"ai-agent",
|
|
144
155
|
"ai-agents",
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare function deprecatedActivity(chatId: string, now?: number): string;
|
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
var deprecated_activity_exports = {};
|
|
20
|
-
__export(deprecated_activity_exports, {
|
|
21
|
-
deprecatedActivity: () => deprecatedActivity
|
|
22
|
-
});
|
|
23
|
-
module.exports = __toCommonJS(deprecated_activity_exports);
|
|
24
|
-
const WORDS = [
|
|
25
|
-
"Thinking",
|
|
26
|
-
"Computing",
|
|
27
|
-
"Pondering",
|
|
28
|
-
"Analyzing",
|
|
29
|
-
"Crunching",
|
|
30
|
-
"Reviewing",
|
|
31
|
-
"Checking",
|
|
32
|
-
"Working on it",
|
|
33
|
-
"Reasoning",
|
|
34
|
-
"Digging in",
|
|
35
|
-
"Gathering",
|
|
36
|
-
"Sorting",
|
|
37
|
-
"Calculating",
|
|
38
|
-
"Connecting the dots",
|
|
39
|
-
"Exploring",
|
|
40
|
-
"Piecing it together",
|
|
41
|
-
"Figuring it out",
|
|
42
|
-
"Untangling",
|
|
43
|
-
"Weighing options",
|
|
44
|
-
"Cooking"
|
|
45
|
-
];
|
|
46
|
-
const WINDOW_IN_MS = 4e3;
|
|
47
|
-
function hash(text) {
|
|
48
|
-
let value = 5381;
|
|
49
|
-
for (const character of text) value = (value << 5) + value + character.charCodeAt(0) >>> 0;
|
|
50
|
-
return value;
|
|
51
|
-
}
|
|
52
|
-
function deprecatedActivity(chatId, now = Date.now()) {
|
|
53
|
-
const window = Math.floor(now / WINDOW_IN_MS);
|
|
54
|
-
const index = hash(`${chatId}:${window}`) % WORDS.length;
|
|
55
|
-
return `${WORDS[index]}...`;
|
|
56
|
-
}
|
|
57
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
58
|
-
0 && (module.exports = {
|
|
59
|
-
deprecatedActivity
|
|
60
|
-
});
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
const WORDS = [
|
|
2
|
-
"Thinking",
|
|
3
|
-
"Computing",
|
|
4
|
-
"Pondering",
|
|
5
|
-
"Analyzing",
|
|
6
|
-
"Crunching",
|
|
7
|
-
"Reviewing",
|
|
8
|
-
"Checking",
|
|
9
|
-
"Working on it",
|
|
10
|
-
"Reasoning",
|
|
11
|
-
"Digging in",
|
|
12
|
-
"Gathering",
|
|
13
|
-
"Sorting",
|
|
14
|
-
"Calculating",
|
|
15
|
-
"Connecting the dots",
|
|
16
|
-
"Exploring",
|
|
17
|
-
"Piecing it together",
|
|
18
|
-
"Figuring it out",
|
|
19
|
-
"Untangling",
|
|
20
|
-
"Weighing options",
|
|
21
|
-
"Cooking"
|
|
22
|
-
];
|
|
23
|
-
const WINDOW_IN_MS = 4e3;
|
|
24
|
-
function hash(text) {
|
|
25
|
-
let value = 5381;
|
|
26
|
-
for (const character of text) value = (value << 5) + value + character.charCodeAt(0) >>> 0;
|
|
27
|
-
return value;
|
|
28
|
-
}
|
|
29
|
-
function deprecatedActivity(chatId, now = Date.now()) {
|
|
30
|
-
const window = Math.floor(now / WINDOW_IN_MS);
|
|
31
|
-
const index = hash(`${chatId}:${window}`) % WORDS.length;
|
|
32
|
-
return `${WORDS[index]}...`;
|
|
33
|
-
}
|
|
34
|
-
export {
|
|
35
|
-
deprecatedActivity
|
|
36
|
-
};
|