openloops 0.9.1 → 0.10.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/CHANGELOG.md +24 -0
- package/README.md +2 -0
- package/dist/clients/llm/llm-client.d.ts +1 -0
- package/dist/clients/llm/llm-client.js +6 -3
- package/dist/clients/llm/llm-client.mjs +6 -3
- package/dist/common/base/current-user.js +2 -1
- package/dist/common/base/current-user.mjs +2 -1
- package/dist/common/index.d.ts +3 -0
- package/dist/common/index.js +6 -0
- package/dist/common/index.mjs +4 -0
- package/dist/common/repositories/mongo-db.client.d.ts +1 -1
- package/dist/common/repositories/mongo-db.client.js +7 -6
- package/dist/common/repositories/mongo-db.client.mjs +7 -6
- package/dist/common/repositories/mongo-db.repository.d.ts +3 -2
- package/dist/common/repositories/mongo-db.repository.js +20 -16
- package/dist/common/repositories/mongo-db.repository.mjs +20 -16
- package/dist/common/utils/config-context.d.ts +3 -0
- package/dist/common/utils/config-context.js +33 -0
- package/dist/common/utils/config-context.mjs +8 -0
- package/dist/common/utils/secret-manager.d.ts +7 -5
- package/dist/common/utils/secret-manager.js +41 -28
- package/dist/common/utils/secret-manager.mjs +41 -28
- package/dist/services/authentication/repositories/refresh-token.repository.js +1 -1
- package/dist/services/authentication/repositories/refresh-token.repository.mjs +1 -1
- package/dist/services/authentication/repositories/revoked-token.repository.js +1 -1
- package/dist/services/authentication/repositories/revoked-token.repository.mjs +1 -1
- package/dist/services/automations/entities/recurrence.d.ts +11 -0
- package/dist/services/automations/entities/recurrence.js +26 -6
- package/dist/services/automations/entities/recurrence.mjs +23 -6
- package/dist/services/automations/repositories/automation-run.repository.js +1 -1
- package/dist/services/automations/repositories/automation-run.repository.mjs +1 -1
- package/dist/services/automations/repositories/automation.repository.js +1 -1
- package/dist/services/automations/repositories/automation.repository.mjs +1 -1
- package/dist/services/automations/runner/automation-runner.js +1 -0
- package/dist/services/automations/runner/automation-runner.mjs +1 -0
- package/dist/services/automations/utils/next-run.js +9 -0
- package/dist/services/automations/utils/next-run.mjs +10 -1
- package/dist/services/chats/repositories/chat.repository.js +1 -1
- package/dist/services/chats/repositories/chat.repository.mjs +1 -1
- package/dist/services/llmcalls/repositories/llmcall.repository.js +1 -1
- package/dist/services/llmcalls/repositories/llmcall.repository.mjs +1 -1
- package/dist/services/mcp-servers/repositories/mcpserver.repository.js +1 -1
- package/dist/services/mcp-servers/repositories/mcpserver.repository.mjs +1 -1
- package/dist/services/tasks/repositories/agent-action.repository.js +1 -1
- package/dist/services/tasks/repositories/agent-action.repository.mjs +1 -1
- package/dist/services/tasks/repositories/agent-task.repository.js +1 -1
- package/dist/services/tasks/repositories/agent-task.repository.mjs +1 -1
- package/dist/services/traces/repositories/agent-trace.repository.js +1 -1
- package/dist/services/traces/repositories/agent-trace.repository.mjs +1 -1
- package/dist/services/users/repositories/user.repository.js +1 -1
- package/dist/services/users/repositories/user.repository.mjs +1 -1
- package/dist/services/workspaces/repositories/workspace-item.repository.js +1 -1
- package/dist/services/workspaces/repositories/workspace-item.repository.mjs +1 -1
- package/docs/README.md +1 -1
- package/docs/automations.md +2 -0
- package/docs/configuration.md +30 -1
- package/docs/integration-guide.md +1 -1
- package/docs/llms.txt +1 -1
- package/examples/run-agent/run-agent-multi-tenant.ts +52 -0
- package/package.json +13 -1
|
@@ -27,7 +27,7 @@ var import_database_name = require("../../../common/utils/database-name.js");
|
|
|
27
27
|
var import_entities = require("../entities");
|
|
28
28
|
class UserRepository {
|
|
29
29
|
_ = new import_repositories.MongoRepository({
|
|
30
|
-
database:
|
|
30
|
+
database: import_database_name.getDatabaseName,
|
|
31
31
|
collection: "users",
|
|
32
32
|
scoped: true,
|
|
33
33
|
ownerField: "_id",
|
|
@@ -4,7 +4,7 @@ import { getDatabaseName } from "../../../common/utils/database-name.js";
|
|
|
4
4
|
import { User } from "../entities";
|
|
5
5
|
class UserRepository {
|
|
6
6
|
_ = new MongoRepository({
|
|
7
|
-
database: getDatabaseName
|
|
7
|
+
database: getDatabaseName,
|
|
8
8
|
collection: "users",
|
|
9
9
|
scoped: true,
|
|
10
10
|
ownerField: "_id",
|
|
@@ -26,7 +26,7 @@ var import_repositories = require("../../../common/repositories/index.js");
|
|
|
26
26
|
var import_database_name = require("../../../common/utils/database-name.js");
|
|
27
27
|
class WorkspaceItemRepository {
|
|
28
28
|
_ = new import_repositories.MongoRepository({
|
|
29
|
-
database:
|
|
29
|
+
database: import_database_name.getDatabaseName,
|
|
30
30
|
collection: "workspace_items",
|
|
31
31
|
scoped: true
|
|
32
32
|
});
|
|
@@ -3,7 +3,7 @@ import { MongoRepository } from "../../../common/repositories/index.js";
|
|
|
3
3
|
import { getDatabaseName } from "../../../common/utils/database-name.js";
|
|
4
4
|
class WorkspaceItemRepository {
|
|
5
5
|
_ = new MongoRepository({
|
|
6
|
-
database: getDatabaseName
|
|
6
|
+
database: getDatabaseName,
|
|
7
7
|
collection: "workspace_items",
|
|
8
8
|
scoped: true
|
|
9
9
|
});
|
package/docs/README.md
CHANGED
|
@@ -14,7 +14,7 @@ Start with the [README](../README.md) for what `openloops` is and how to run it.
|
|
|
14
14
|
| Page | What it covers |
|
|
15
15
|
| --- | --- |
|
|
16
16
|
| [The `Agent`](agent.md) | Options, running a turn, the result and its statuses, confirmations, autonomous mode, hooks and interruption. |
|
|
17
|
-
| [Configuration](configuration.md) | Environment variables, choosing the model and what is recorded. |
|
|
17
|
+
| [Configuration](configuration.md) | Environment variables, encrypted values, several agents in one process, choosing the model and what is recorded. |
|
|
18
18
|
| [Reporting errors](error-reporting.md) | Declare one function, once, to send the errors that the package catches to Sentry or to the tool you use: what it receives, what is reported, serverless and moving from `SENTRY_DSN`. |
|
|
19
19
|
| [Automations](automations.md) | Tasks that the agent runs by itself, on a schedule, when an event arrives or both: triggers, tokens, the queue, the history of runs and the runner that executes them. If you used `agentic-jobs`, read [the migration steps](automations.md#migrating-from-agentic-jobs). Updating to 0.5.0 breaks the shape of the triggers, read [the migration to 0.5.0](automations.md#migrating-to-050). |
|
|
20
20
|
| [Chats](chats.md) | The chat record, its title and messages and their `source`, listing and searching chats, reading and deleting them. Updating to 0.8.0 needs [a migration of the titles](chats.md#migrating-to-080). |
|
package/docs/automations.md
CHANGED
|
@@ -92,10 +92,12 @@ The recurrence of a `schedule` trigger is stored as a structured value, so a scr
|
|
|
92
92
|
| `daily` | `time` | `{ type: 'daily', time: '08:00' }` |
|
|
93
93
|
| `weekly` | `days` (1 is Monday, 7 is Sunday), `time` | `{ type: 'weekly', days: [1, 2, 3, 4, 5], time: '09:30' }` is Monday to Friday |
|
|
94
94
|
| `monthly` | `day` (1 to 31 or `'last'`), `time` | `{ type: 'monthly', day: 15, time: '12:00' }` |
|
|
95
|
+
| `interval` | `every` (whole number), `unit` (`'minutes'` or `'hours'`), `start` (`YYYY-MM-DDTHH:mm`, local time) | `{ type: 'interval', every: 90, unit: 'minutes', start: '2026-10-14T08:00' }` |
|
|
95
96
|
| `once` | `at` (`YYYY-MM-DDTHH:mm`, local time) | `{ type: 'once', at: '2026-12-25T10:00' }` |
|
|
96
97
|
|
|
97
98
|
- A monthly automation on day 29, 30 or 31 is skipped in the months that do not have that day. Use `'last'` for the last day of every month.
|
|
98
99
|
- A time that does not exist on a day because of daylight saving time runs at the first valid moment of that day, and a time that happens twice runs once.
|
|
100
|
+
- An `interval` counts real minutes or hours from `start`: it runs at `start`, then every `every` units after it, with no drift and not tied to the clock, so every 7 hours from 08:00 runs at 08:00, 15:00, 22:00, 05:00... Daylight saving time does not move it. A `start` in the past is fine, the next run is the next moment of the grid. Changing `start`, `every`, `unit` or the timezone changes the grid. The shortest interval is 5 minutes (`MIN_INTERVAL_MINUTES`).
|
|
99
101
|
- An automation of type `once` runs a single time and then has no next run.
|
|
100
102
|
|
|
101
103
|
### Previewing
|
package/docs/configuration.md
CHANGED
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
| `OPENLOOPS_DB_NAME` | no | Database name. Defaults to `openloops`. |
|
|
9
9
|
| `OPENLOOPS_MONGO_MAX_POOL_SIZE` | no | Most connections each process keeps open to MongoDB. Defaults to `3`, which suits serverless functions, where every instance handles one request at a time. Raise it for a long-running server. |
|
|
10
10
|
| `SECRET_MANAGER_KEY` | yes | Key used to decrypt values that start with `ENC:`. The library throws when it loads without it. |
|
|
11
|
+
| `SECRET_MANAGER_KEY_V<N>` | only if you use `ENC:v<N>:` values | Key of version `N` of the [versioned encrypted values](#encrypted-values). |
|
|
11
12
|
| `ANTHROPIC_API_KEY` | for the default model | API key of the default provider, `anthropic`. |
|
|
12
13
|
| `OPENAI_API_KEY`, `GEMINI_API_KEY` | for those providers | API keys of the other providers you choose per chat. |
|
|
13
14
|
| `JWT_SECRET` | for `login` and for event automations | Secret that signs the tokens of the sessions, of the reset of a password and of [event automations](automations.md#the-token). See [authentication](authentication.md). |
|
|
@@ -23,7 +24,35 @@
|
|
|
23
24
|
| `LANGFUSE_PUBLIC_KEY`, `LANGFUSE_SECRET_KEY`, `LANGFUSE_BASE_URL` | no | Traces every LLM call in Langfuse when all three are set. |
|
|
24
25
|
| `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
26
|
|
|
26
|
-
|
|
27
|
+
## Encrypted values
|
|
28
|
+
|
|
29
|
+
Any value can be stored encrypted. It is decrypted when it is read.
|
|
30
|
+
|
|
31
|
+
- `ENC:<payload>` is decrypted with `SECRET_MANAGER_KEY`.
|
|
32
|
+
- `ENC:v<N>:<payload>` is decrypted with `SECRET_MANAGER_KEY_V<N>`. Versions let you rotate the key: encrypt new values with the next version, keep the old key until nothing uses it, then remove it.
|
|
33
|
+
|
|
34
|
+
```ts
|
|
35
|
+
import { SecretManager } from 'openloops/common'
|
|
36
|
+
|
|
37
|
+
SecretManager.encrypt('sk-ant-...')
|
|
38
|
+
SecretManager.encrypt('sk-ant-...', 2)
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## One process, several agents
|
|
42
|
+
|
|
43
|
+
By default the configuration is read from the environment, which is one configuration per process. To serve several agents from the same process, for example one function that runs every agent of a platform, run each request with its own configuration:
|
|
44
|
+
|
|
45
|
+
```ts
|
|
46
|
+
import { runWithConfigs } from 'openloops/common'
|
|
47
|
+
|
|
48
|
+
await runWithConfigs(configsOfThisAgent, () => handleRequest(event))
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
`configsOfThisAgent` is an object with the same names as the variables of this page (`OPENLOOPS_DB_URI`, `OPENLOOPS_DB_NAME`, `JWT_SECRET`, `ANTHROPIC_API_KEY`...), and its values can be encrypted. Everything that runs inside the callback, including what it awaits, reads those values and never the environment, so a name that is missing from the object is empty and does not fall back to the environment. Outside of `runWithConfigs` nothing changes.
|
|
52
|
+
|
|
53
|
+
Each agent can use its own database, even in its own cluster: the repositories resolve the database name and the connection on every call, and the connections are shared by connection string. The API key of the provider is sent with each model call, so two agents never use each other's key. See the example [run-agent-multi-tenant](../examples/run-agent/run-agent-multi-tenant.ts).
|
|
54
|
+
|
|
55
|
+
Pass the configuration of a request to `runWithConfigs` before creating the `Agent` and before resolving the user, and do not keep the result of `secretManager.get` between requests.
|
|
27
56
|
|
|
28
57
|
## Which model a chat uses
|
|
29
58
|
|
|
@@ -130,7 +130,7 @@ runner.start()
|
|
|
130
130
|
- Add input guards for what your product must never receive, such as card numbers.
|
|
131
131
|
- Mark your own destructive tools with `destructive: true`. The agent asks for confirmation before running them. Do not rely on a tool being treated as destructive by default, because it is not.
|
|
132
132
|
- Use `strictMode: true` in the agent when the tools of an MCP server must be reviewed by the user on every change.
|
|
133
|
-
- Keep API keys and the secrets of [configuration](configuration.md) out of the code. Values can be stored encrypted with the `ENC:` prefix.
|
|
133
|
+
- Keep API keys and the secrets of [configuration](configuration.md) out of the code. Values can be stored encrypted with the `ENC:` prefix. To serve several agents from one process, see [one process, several agents](configuration.md#one-process-several-agents).
|
|
134
134
|
- Set limits on how many automations and how often they run in your own layer, since `openloops` does not limit them yet.
|
|
135
135
|
|
|
136
136
|
## Common mistakes
|
package/docs/llms.txt
CHANGED
|
@@ -13,7 +13,7 @@ Start with the integration guide, then read only the pages you need. All pages a
|
|
|
13
13
|
## Run an agent
|
|
14
14
|
|
|
15
15
|
- [The Agent](agent.md): options, running a turn, statuses, confirmations, autonomous mode
|
|
16
|
-
- [Configuration](configuration.md): environment variables, default model, timeouts
|
|
16
|
+
- [Configuration](configuration.md): environment variables, encrypted values, several agents in one process, default model, timeouts
|
|
17
17
|
- [Reporting errors](error-reporting.md): `setErrorReporter`, what it receives, what is reported, serverless and moving from `SENTRY_DSN`
|
|
18
18
|
- [Chats](chats.md): the chat record, its title, message source, listing, searching and deleting, and the migration to 0.8.0
|
|
19
19
|
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { Agent } from 'openloops/core'
|
|
2
|
+
import { Logger, runWithConfigs, SecretManager } from 'openloops/common'
|
|
3
|
+
import { CurrentUser } from 'openloops/base'
|
|
4
|
+
import { AgiLoop } from 'openloops/loops'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Serves two agents from the same process. Each one has its own database, its own JWT secret and its own model key, and the
|
|
8
|
+
* values are stored encrypted. Everything inside `runWithConfigs` reads that configuration and never the environment.
|
|
9
|
+
*/
|
|
10
|
+
const agents: Record<string, Record<string, string>> = {
|
|
11
|
+
acme: {
|
|
12
|
+
OPENLOOPS_DB_URI: 'mongodb://localhost:27017',
|
|
13
|
+
OPENLOOPS_DB_NAME: 'agent_acme',
|
|
14
|
+
JWT_SECRET: SecretManager.encrypt('acme-jwt-secret', 1),
|
|
15
|
+
ANTHROPIC_API_KEY: SecretManager.encrypt(process.env.ACME_ANTHROPIC_API_KEY as string, 1),
|
|
16
|
+
},
|
|
17
|
+
globex: {
|
|
18
|
+
OPENLOOPS_DB_URI: 'mongodb://localhost:27017',
|
|
19
|
+
OPENLOOPS_DB_NAME: 'agent_globex',
|
|
20
|
+
JWT_SECRET: SecretManager.encrypt('globex-jwt-secret', 1),
|
|
21
|
+
ANTHROPIC_API_KEY: SecretManager.encrypt(process.env.GLOBEX_ANTHROPIC_API_KEY as string, 1),
|
|
22
|
+
},
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
async function handleRequest (agentName: string, userId: string, message: string) {
|
|
26
|
+
return runWithConfigs(agents[agentName], async () => {
|
|
27
|
+
const currentUser = await CurrentUser.asyncFromDB(userId)
|
|
28
|
+
|
|
29
|
+
if (!currentUser) throw new Error('[Demo] current user is not defined')
|
|
30
|
+
|
|
31
|
+
const agent = new Agent({ loop: new AgiLoop() })
|
|
32
|
+
|
|
33
|
+
return agent.run({ input: { message }, currentUser })
|
|
34
|
+
})
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
async function runMultiTenant () {
|
|
38
|
+
const logger = new Logger()
|
|
39
|
+
|
|
40
|
+
try {
|
|
41
|
+
const [acme, globex] = await Promise.all([
|
|
42
|
+
handleRequest('acme', '6ab56894eb20887d1ef7942c', 'hi from acme'),
|
|
43
|
+
handleRequest('globex', '6ab56894eb20887d1ef7942d', 'hi from globex'),
|
|
44
|
+
])
|
|
45
|
+
|
|
46
|
+
logger.info({ acme, globex }, '[Demo] Result')
|
|
47
|
+
} catch (error: any) {
|
|
48
|
+
logger.error(error, '[Demo] Error')
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
runMultiTenant()
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "openloops",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.2",
|
|
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",
|
|
@@ -83,6 +83,18 @@
|
|
|
83
83
|
"types": "./dist/services/users/index.d.ts",
|
|
84
84
|
"default": "./dist/services/users/index.js"
|
|
85
85
|
},
|
|
86
|
+
"./tasks": {
|
|
87
|
+
"types": "./dist/services/tasks/index.d.ts",
|
|
88
|
+
"default": "./dist/services/tasks/index.js"
|
|
89
|
+
},
|
|
90
|
+
"./traces": {
|
|
91
|
+
"types": "./dist/services/traces/index.d.ts",
|
|
92
|
+
"default": "./dist/services/traces/index.js"
|
|
93
|
+
},
|
|
94
|
+
"./workspaces": {
|
|
95
|
+
"types": "./dist/services/workspaces/index.d.ts",
|
|
96
|
+
"default": "./dist/services/workspaces/index.js"
|
|
97
|
+
},
|
|
86
98
|
"./db": {
|
|
87
99
|
"types": "./dist/db/index.d.ts",
|
|
88
100
|
"default": "./dist/db/index.js"
|