openloops 0.1.38 → 0.1.39
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/README.md +21 -2
- package/dist/services/agentic-jobs/entities/agentic-job-run.entity.d.ts +42 -0
- package/dist/services/agentic-jobs/entities/agentic-job-run.entity.js +103 -0
- package/dist/services/agentic-jobs/entities/agentic-job-run.entity.mjs +80 -0
- package/dist/services/agentic-jobs/entities/agentic-job.entity.d.ts +39 -0
- package/dist/services/agentic-jobs/entities/agentic-job.entity.js +113 -0
- package/dist/services/agentic-jobs/entities/agentic-job.entity.mjs +90 -0
- package/dist/services/agentic-jobs/entities/index.d.ts +3 -0
- package/dist/services/agentic-jobs/entities/index.js +26 -0
- package/dist/services/agentic-jobs/entities/index.mjs +3 -0
- package/dist/services/agentic-jobs/entities/recurrence.d.ts +18 -0
- package/dist/services/agentic-jobs/entities/recurrence.js +84 -0
- package/dist/services/agentic-jobs/entities/recurrence.mjs +59 -0
- package/dist/services/agentic-jobs/index.d.ts +4 -0
- package/dist/services/agentic-jobs/index.js +28 -0
- package/dist/services/agentic-jobs/index.mjs +4 -0
- package/dist/services/agentic-jobs/module.d.ts +31 -0
- package/dist/services/agentic-jobs/module.js +132 -0
- package/dist/services/agentic-jobs/module.mjs +95 -0
- package/dist/services/agentic-jobs/repositories/agentic-job-run.repository.d.ts +11 -0
- package/dist/services/agentic-jobs/repositories/agentic-job-run.repository.js +71 -0
- package/dist/services/agentic-jobs/repositories/agentic-job-run.repository.mjs +42 -0
- package/dist/services/agentic-jobs/repositories/agentic-job.repository.d.ts +27 -0
- package/dist/services/agentic-jobs/repositories/agentic-job.repository.js +127 -0
- package/dist/services/agentic-jobs/repositories/agentic-job.repository.mjs +98 -0
- package/dist/services/agentic-jobs/repositories/index.d.ts +2 -0
- package/dist/services/agentic-jobs/repositories/index.js +24 -0
- package/dist/services/agentic-jobs/repositories/index.mjs +2 -0
- package/dist/services/agentic-jobs/runner/agentic-job-runner.d.ts +48 -0
- package/dist/services/agentic-jobs/runner/agentic-job-runner.js +181 -0
- package/dist/services/agentic-jobs/runner/agentic-job-runner.mjs +157 -0
- package/dist/services/agentic-jobs/use-cases/attach-agentic-job-run-chat.use-case.d.ts +15 -0
- package/dist/services/agentic-jobs/use-cases/attach-agentic-job-run-chat.use-case.js +58 -0
- package/dist/services/agentic-jobs/use-cases/attach-agentic-job-run-chat.use-case.mjs +29 -0
- package/dist/services/agentic-jobs/use-cases/claim-due-agentic-jobs.use-case.d.ts +29 -0
- package/dist/services/agentic-jobs/use-cases/claim-due-agentic-jobs.use-case.js +110 -0
- package/dist/services/agentic-jobs/use-cases/claim-due-agentic-jobs.use-case.mjs +81 -0
- package/dist/services/agentic-jobs/use-cases/create-agentic-job.use-case.d.ts +14 -0
- package/dist/services/agentic-jobs/use-cases/create-agentic-job.use-case.js +73 -0
- package/dist/services/agentic-jobs/use-cases/create-agentic-job.use-case.mjs +44 -0
- package/dist/services/agentic-jobs/use-cases/delete-agentic-job.use-case.d.ts +15 -0
- package/dist/services/agentic-jobs/use-cases/delete-agentic-job.use-case.js +65 -0
- package/dist/services/agentic-jobs/use-cases/delete-agentic-job.use-case.mjs +36 -0
- package/dist/services/agentic-jobs/use-cases/finish-agentic-job-run.use-case.d.ts +19 -0
- package/dist/services/agentic-jobs/use-cases/finish-agentic-job-run.use-case.js +75 -0
- package/dist/services/agentic-jobs/use-cases/finish-agentic-job-run.use-case.mjs +46 -0
- package/dist/services/agentic-jobs/use-cases/get-agentic-job-by-id.use-case.d.ts +14 -0
- package/dist/services/agentic-jobs/use-cases/get-agentic-job-by-id.use-case.js +55 -0
- package/dist/services/agentic-jobs/use-cases/get-agentic-job-by-id.use-case.mjs +26 -0
- package/dist/services/agentic-jobs/use-cases/list-agentic-job-runs-by-job.use-case.d.ts +16 -0
- package/dist/services/agentic-jobs/use-cases/list-agentic-job-runs-by-job.use-case.js +57 -0
- package/dist/services/agentic-jobs/use-cases/list-agentic-job-runs-by-job.use-case.mjs +28 -0
- package/dist/services/agentic-jobs/use-cases/list-agentic-job-runs-by-user.use-case.d.ts +16 -0
- package/dist/services/agentic-jobs/use-cases/list-agentic-job-runs-by-user.use-case.js +57 -0
- package/dist/services/agentic-jobs/use-cases/list-agentic-job-runs-by-user.use-case.mjs +28 -0
- package/dist/services/agentic-jobs/use-cases/list-agentic-jobs-by-user.use-case.d.ts +15 -0
- package/dist/services/agentic-jobs/use-cases/list-agentic-jobs-by-user.use-case.js +57 -0
- package/dist/services/agentic-jobs/use-cases/list-agentic-jobs-by-user.use-case.mjs +28 -0
- package/dist/services/agentic-jobs/use-cases/preview-agentic-job-runs.use-case.d.ts +15 -0
- package/dist/services/agentic-jobs/use-cases/preview-agentic-job-runs.use-case.js +52 -0
- package/dist/services/agentic-jobs/use-cases/preview-agentic-job-runs.use-case.mjs +23 -0
- package/dist/services/agentic-jobs/use-cases/renew-agentic-job-lease.use-case.d.ts +18 -0
- package/dist/services/agentic-jobs/use-cases/renew-agentic-job-lease.use-case.js +58 -0
- package/dist/services/agentic-jobs/use-cases/renew-agentic-job-lease.use-case.mjs +29 -0
- package/dist/services/agentic-jobs/use-cases/start-agentic-job-run.use-case.d.ts +21 -0
- package/dist/services/agentic-jobs/use-cases/start-agentic-job-run.use-case.js +67 -0
- package/dist/services/agentic-jobs/use-cases/start-agentic-job-run.use-case.mjs +38 -0
- package/dist/services/agentic-jobs/use-cases/sync-agentic-job-run.use-case.d.ts +20 -0
- package/dist/services/agentic-jobs/use-cases/sync-agentic-job-run.use-case.js +86 -0
- package/dist/services/agentic-jobs/use-cases/sync-agentic-job-run.use-case.mjs +56 -0
- package/dist/services/agentic-jobs/use-cases/sync-agentic-job-runs.use-case.d.ts +19 -0
- package/dist/services/agentic-jobs/use-cases/sync-agentic-job-runs.use-case.js +68 -0
- package/dist/services/agentic-jobs/use-cases/sync-agentic-job-runs.use-case.mjs +39 -0
- package/dist/services/agentic-jobs/use-cases/update-agentic-job.use-case.d.ts +15 -0
- package/dist/services/agentic-jobs/use-cases/update-agentic-job.use-case.js +83 -0
- package/dist/services/agentic-jobs/use-cases/update-agentic-job.use-case.mjs +54 -0
- package/dist/services/agentic-jobs/utils/job-fields.d.ts +14 -0
- package/dist/services/agentic-jobs/utils/job-fields.js +61 -0
- package/dist/services/agentic-jobs/utils/job-fields.mjs +37 -0
- package/dist/services/agentic-jobs/utils/next-run.d.ts +4 -0
- package/dist/services/agentic-jobs/utils/next-run.js +62 -0
- package/dist/services/agentic-jobs/utils/next-run.mjs +37 -0
- package/dist/services/agentic-jobs/utils/open-run.d.ts +12 -0
- package/dist/services/agentic-jobs/utils/open-run.js +47 -0
- package/dist/services/agentic-jobs/utils/open-run.mjs +24 -0
- package/dist/services/agentic-jobs/utils/settings.d.ts +2 -0
- package/dist/services/agentic-jobs/utils/settings.js +34 -0
- package/dist/services/agentic-jobs/utils/settings.mjs +10 -0
- package/docs/README.md +46 -0
- package/docs/agent.md +201 -0
- package/docs/agentic-jobs.md +207 -0
- package/docs/authentication.md +40 -0
- package/docs/chats.md +88 -0
- package/docs/configuration.md +69 -0
- package/docs/guardrails.md +71 -0
- package/docs/harness.md +81 -0
- package/docs/integration-guide.md +138 -0
- package/docs/llms.txt +35 -0
- package/docs/loops.md +147 -0
- package/docs/mcp-servers.md +148 -0
- package/docs/skills.md +104 -0
- package/docs/tools.md +90 -0
- package/docs/usage.md +86 -0
- package/docs/users.md +85 -0
- package/examples/agentic-jobs/create-agentic-job.ts +24 -0
- package/examples/agentic-jobs/list-agentic-job-runs.ts +28 -0
- package/examples/agentic-jobs/list-agentic-jobs.ts +25 -0
- package/examples/agentic-jobs/preview-agentic-job-runs.ts +16 -0
- package/examples/agentic-jobs/run-agentic-job-now.ts +19 -0
- package/examples/agentic-jobs/run-agentic-job-worker.ts +29 -0
- package/examples/agentic-jobs/update-agentic-job.ts +36 -0
- package/examples/authentication/login.ts +17 -0
- package/examples/chats/delete-chat.ts +32 -0
- package/examples/chats/get-chat-by-id.ts +32 -0
- package/examples/chats/list-autonomous-chats.ts +23 -0
- package/examples/chats/list-chats.ts +20 -0
- package/examples/guardrails/custom-guard.ts +11 -0
- package/examples/loops/custom-loop.ts +50 -0
- package/examples/mcps/create-mcp.ts +24 -0
- package/examples/mcps/decide-mcp.ts +40 -0
- package/examples/mcps/delete-mcp.ts +32 -0
- package/examples/mcps/get-mcp-by-id.ts +17 -0
- package/examples/mcps/list-all-mcps.ts +14 -0
- package/examples/mcps/list-user-mcps.ts +23 -0
- package/examples/mcps/update-mcp.ts +23 -0
- package/examples/mcps/verify-mcp.ts +17 -0
- package/examples/run-agent/interrupt-agent.ts +16 -0
- package/examples/run-agent/run-agent-autonomous.ts +34 -0
- package/examples/run-agent/run-agent-strict-mode.ts +30 -0
- package/examples/run-agent/run-agent-with-confirmation.ts +49 -0
- package/examples/run-agent/run-agent-with-guards.ts +33 -0
- package/examples/run-agent/run-agent-with-hooks.ts +55 -0
- package/examples/run-agent/run-agent-with-identity.ts +36 -0
- package/examples/run-agent/run-agent-with-model.ts +33 -0
- package/examples/run-agent/run-agent-with-tools.ts +31 -0
- package/examples/tools/basic-tool.ts +35 -0
- package/examples/tools/destructive-tool.ts +34 -0
- package/examples/tools/web-search-tool.ts +37 -0
- package/examples/usage/get-chat-usage.ts +22 -0
- package/examples/usage/get-message-usage.ts +22 -0
- package/examples/usage/get-usage-stats-by-range.ts +23 -0
- package/examples/usage/get-usage-stats.ts +18 -0
- package/examples/users/create-user.ts +19 -0
- package/examples/users/delete-user.ts +36 -0
- package/examples/users/get-user-by-id.ts +18 -0
- package/examples/users/list-users.ts +18 -0
- package/examples/users/update-user.ts +22 -0
- package/package.json +9 -2
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
# Integration guide
|
|
2
|
+
|
|
3
|
+
This page is for whoever builds the backend that uses `openloops`, a person or a coding agent. It tells you what to write. The other pages explain each piece in detail, and the [index](README.md) lists them.
|
|
4
|
+
|
|
5
|
+
## What the package is, and what it is not
|
|
6
|
+
|
|
7
|
+
`openloops` gives you a runtime for agents and a set of use cases that read and write MongoDB. It does **not** include an HTTP server, routes, controllers, sessions or authorization. Your backend owns those, and calls `openloops` from its own handlers.
|
|
8
|
+
|
|
9
|
+
Requirements: Node 22 or newer and a MongoDB database. Install it with `npm install openloops` or `yarn add openloops`, set the environment variables of [configuration](configuration.md), and import from the subpaths (`openloops/core`, `openloops/mcps`, and so on).
|
|
10
|
+
|
|
11
|
+
## Tell your coding agent where this is
|
|
12
|
+
|
|
13
|
+
A coding agent only finds this documentation if something points it here. Add this to the `CLAUDE.md` or `AGENTS.md` of your backend, once:
|
|
14
|
+
|
|
15
|
+
```
|
|
16
|
+
This project uses the `openloops` package. Before writing code that uses it, read
|
|
17
|
+
`node_modules/openloops/docs/integration-guide.md`. Its full documentation is in
|
|
18
|
+
`node_modules/openloops/docs` and its examples are in `node_modules/openloops/examples`.
|
|
19
|
+
The package has no controllers: the authorization of every endpoint is the responsibility of this backend.
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## The rule that matters most: authorization is yours
|
|
23
|
+
|
|
24
|
+
The use cases trust the `currentUser` you pass and the ids you give them.
|
|
25
|
+
|
|
26
|
+
- Functions that list by user (`listChatsByUser`, `listMcpServersByUser`, `listAgenticJobsByUser`, `listAgenticJobRunsByUser`) only return records of that user.
|
|
27
|
+
- Functions that take an **id** (`getChatById`, `updateChat`, `deleteChat`, `getMcpServerById`, `updateMcpServer`, `deleteMcpServer`, `decideMcpServerTrust`, `getAgenticJobById`, `updateAgenticJob`, `deleteAgenticJob`, `listAgenticJobRunsByJob`, `runner.runNow`) **do not check who owns the record**.
|
|
28
|
+
|
|
29
|
+
So, before calling any of the second kind, load the record and check that `ownerId` is the user of the request. Do it in one place, a middleware or a helper, and never pass an id from a request straight to a use case.
|
|
30
|
+
|
|
31
|
+
Also note that `getChatById` returns `{}` in `data` when the chat does not exist, so check for `data._id`.
|
|
32
|
+
|
|
33
|
+
## Which endpoint calls what
|
|
34
|
+
|
|
35
|
+
| Your endpoint | `openloops` call |
|
|
36
|
+
| --- | --- |
|
|
37
|
+
| Send a message | `new Agent({ loop, tools, guards }).run({ input: { message, chatId? }, currentUser })` |
|
|
38
|
+
| Stop a running message | `Agent.interruptExecution(chatId, currentUser)` |
|
|
39
|
+
| List, read, delete chats | `listChatsByUser` (with `executionMode` to separate interactive from autonomous), `getChatById`, `deleteChat` |
|
|
40
|
+
| Poll the state of a chat | `getChatById({ id, select: { state: 1 } })` |
|
|
41
|
+
| Manage MCP servers | `createMcpServer`, `listMcpServersByUser`, `getMcpServerById`, `updateMcpServer`, `deleteMcpServer`, `verifyMcpServerConnection` |
|
|
42
|
+
| Approve or block an MCP server from a screen | `decideMcpServerTrust` |
|
|
43
|
+
| Usage of a chat or an answer | `getLLMUsage` |
|
|
44
|
+
| Usage statistics of a user | `getLLMUsageStats` |
|
|
45
|
+
| Create the schedule of an agent | `createAgenticJob`, `updateAgenticJob`, `listAgenticJobsByUser`, `getAgenticJobById`, `deleteAgenticJob`, `previewAgenticJobRuns` |
|
|
46
|
+
| History of scheduled runs | `listAgenticJobRunsByJob`, `listAgenticJobRunsByUser` |
|
|
47
|
+
| "Run now" | `runner.runNow({ jobId })` |
|
|
48
|
+
| Sign in, users | `login`, `createUser`, `getUserById`, `updateUser`, `deleteUser` |
|
|
49
|
+
|
|
50
|
+
## Running the agent for a message
|
|
51
|
+
|
|
52
|
+
```typescript
|
|
53
|
+
import { Agent } from 'openloops/core'
|
|
54
|
+
import { AgiLoop } from 'openloops/loops'
|
|
55
|
+
import { CurrentUser } from 'openloops/base'
|
|
56
|
+
import { inputGuards } from 'openloops/guardrails'
|
|
57
|
+
|
|
58
|
+
const currentUser = await CurrentUser.asyncFromDB(userIdFromYourAuth)
|
|
59
|
+
|
|
60
|
+
const agent = new Agent({
|
|
61
|
+
loop: new AgiLoop(),
|
|
62
|
+
tools: yourTools,
|
|
63
|
+
guards: [inputGuards.blockCard],
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
const { data, error } = await agent.run({
|
|
67
|
+
input: { message, chatId },
|
|
68
|
+
currentUser,
|
|
69
|
+
})
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
- **Create an `Agent` for every run.** An instance keeps the state of the run it executes, so do not share one between requests that can overlap. Tools and guards are stateless and can be shared.
|
|
73
|
+
- **Choose the loop yourself.** There is no default. Use `AgiLoop`.
|
|
74
|
+
- **A run does not stream.** `agent.run` returns when the turn ends, which can take from seconds to minutes. Either give the request a long timeout, or start the run without waiting for it and let the client poll `getChatById` with `select: { state: 1 }` until the status stops being `processing`.
|
|
75
|
+
- **It does not throw for failures inside the run.** Read `error` and `data.state.status`. See [agent](agent.md#the-result).
|
|
76
|
+
- **Send the chat to your client with `data.toJSON()`**, after you have removed what the client must not see.
|
|
77
|
+
|
|
78
|
+
## What the client has to handle
|
|
79
|
+
|
|
80
|
+
Show `data.messages`, and look at `data.state.status`:
|
|
81
|
+
|
|
82
|
+
| Status | What your screen does |
|
|
83
|
+
| --- | --- |
|
|
84
|
+
| `idle` | Ready for a new message. |
|
|
85
|
+
| `processing` | A run is in progress. Show a loading state and offer to stop it. |
|
|
86
|
+
| `awaiting_user_input` | The agent asked a question. The answer is sent as a normal message in the same chat. |
|
|
87
|
+
| `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. |
|
|
88
|
+
| `failed` | Show `data.state.lastError.message` and let the user try again. |
|
|
89
|
+
|
|
90
|
+
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).
|
|
91
|
+
|
|
92
|
+
## MCP servers
|
|
93
|
+
|
|
94
|
+
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).
|
|
95
|
+
|
|
96
|
+
**Do not send `headers` or `history` of an MCP server to the client as they are.** `headers` hold credentials and `history` holds the full manifests. Mask the header values and send only what the screen needs, such as the name, `trust`, `status`, `lastError` and the hash of the latest manifest.
|
|
97
|
+
|
|
98
|
+
## Scheduled agents (agentic jobs)
|
|
99
|
+
|
|
100
|
+
Two parts, as described in [agentic jobs](agentic-jobs.md):
|
|
101
|
+
|
|
102
|
+
1. Endpoints that create and manage jobs and read their runs, from the table above.
|
|
103
|
+
2. A worker process that you start once, with the options of the agent your jobs should use:
|
|
104
|
+
|
|
105
|
+
```typescript
|
|
106
|
+
import { createAgenticJobRunner } from 'openloops/agentic-jobs'
|
|
107
|
+
|
|
108
|
+
const runner = createAgenticJobRunner({
|
|
109
|
+
agent: { tools: yourTools, guards: yourGuards },
|
|
110
|
+
onRunFinished: async ({ job, run }) => { /* notify the owner */ },
|
|
111
|
+
})
|
|
112
|
+
|
|
113
|
+
runner.start()
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
- It is safe to start the worker in several instances: a due job is taken by one of them.
|
|
117
|
+
- Stop it with `runner.stop()` when the process receives `SIGTERM`.
|
|
118
|
+
- Create the indexes listed in [agentic jobs](agentic-jobs.md#indexes).
|
|
119
|
+
- A job that needs the user (a destructive tool, an unapproved server, a question) pauses its run, and the user answers in the chat of that run. Show those runs, which you can list with `listAgenticJobRunsByUser({ status: 'paused' })`.
|
|
120
|
+
|
|
121
|
+
## Security checklist
|
|
122
|
+
|
|
123
|
+
- Check ownership before every call that takes an id.
|
|
124
|
+
- Mask `headers` and `history` of MCP servers.
|
|
125
|
+
- Add input guards for what your product must never receive, such as card numbers.
|
|
126
|
+
- 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.
|
|
127
|
+
- Use `strictMode: true` in the agent when the tools of an MCP server must be reviewed by the user on every change.
|
|
128
|
+
- Keep API keys and the secrets of [configuration](configuration.md) out of the code. Values can be stored encrypted with the `ENC:` prefix.
|
|
129
|
+
- Set limits on how many scheduled jobs and how often they run in your own layer, since `openloops` does not limit them yet.
|
|
130
|
+
|
|
131
|
+
## Common mistakes
|
|
132
|
+
|
|
133
|
+
- Sharing one `Agent` between concurrent requests.
|
|
134
|
+
- Waiting for `agent.run` inside a request with a short timeout.
|
|
135
|
+
- Passing `chatId`, `jobId` or `mcpServerId` from the client without checking the owner.
|
|
136
|
+
- Treating `error` as an exception that is thrown. It is returned.
|
|
137
|
+
- Forgetting that a run in `awaiting_user_confirmation` is waiting for the next message of the user, and that the next message is the answer.
|
|
138
|
+
- Starting the scheduled jobs worker without creating its indexes.
|
package/docs/llms.txt
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# openloops
|
|
2
|
+
|
|
3
|
+
> A TypeScript runtime for AI agents that run in your backend. It persists chats, tasks and tool calls in MongoDB, scopes everything to a user, asks for approval before destructive tools and unapproved MCP servers, and can run agents on a schedule. It has no HTTP layer: your backend calls its use cases and owns authorization.
|
|
4
|
+
|
|
5
|
+
Start with the integration guide, then read only the pages you need. All pages are in this folder.
|
|
6
|
+
|
|
7
|
+
## Start here
|
|
8
|
+
|
|
9
|
+
- [Integration guide](integration-guide.md): what to write in a backend, which endpoint calls what, and the security checklist
|
|
10
|
+
- [README](../README.md): what it is, the quick start and the package structure
|
|
11
|
+
|
|
12
|
+
## Run an agent
|
|
13
|
+
|
|
14
|
+
- [The Agent runtime](agent.md): options, running a turn, statuses, confirmations, autonomous mode
|
|
15
|
+
- [Configuration](configuration.md): environment variables, default model, timeouts
|
|
16
|
+
- [Chats](chats.md): the chat record, message source, listing and deleting
|
|
17
|
+
|
|
18
|
+
## Shape the agent
|
|
19
|
+
|
|
20
|
+
- [Building an AgentLoop](loops.md): AgiLoop, the loop contract, RunContext and Harness
|
|
21
|
+
- [Skills](skills.md): LLM-backed reasoning steps and call modes
|
|
22
|
+
- [Tools](tools.md): writing tools, validation, built-in tools
|
|
23
|
+
- [Guardrails](guardrails.md): input guards before the model
|
|
24
|
+
- [The harness](harness.md): tool pipeline, workspace, approvals, language, limits
|
|
25
|
+
|
|
26
|
+
## Connect and schedule
|
|
27
|
+
|
|
28
|
+
- [MCP servers](mcp-servers.md): registering servers, the trust flow, approving and blocking
|
|
29
|
+
- [Agentic jobs](agentic-jobs.md): scheduled agent runs, recurrence, history and the runner
|
|
30
|
+
|
|
31
|
+
## Users and cost
|
|
32
|
+
|
|
33
|
+
- [User management](users.md)
|
|
34
|
+
- [Authentication](authentication.md)
|
|
35
|
+
- [LLM usage tracking](usage.md)
|
package/docs/loops.md
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
# Building an `AgentLoop`
|
|
2
|
+
|
|
3
|
+
An `AgentLoop` decides how your agent thinks and acts: when it answers directly, when it uses tools, when it asks the user. `openloops` ships `AgiLoop`, which is the one to use for general agents, and you can write your own for anything else.
|
|
4
|
+
|
|
5
|
+
```typescript
|
|
6
|
+
import { AgentLoop, AgentStatus, Harness, RunContext } from 'openloops/core'
|
|
7
|
+
import { AgiLoop } from 'openloops/loops'
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
## `AgiLoop`
|
|
11
|
+
|
|
12
|
+
`AgiLoop` is a reasoning loop. It answers directly when it can, and otherwise works in cycles of think, act and observe until it can give a final answer.
|
|
13
|
+
|
|
14
|
+
| Node | What it does |
|
|
15
|
+
| --- | --- |
|
|
16
|
+
| `fast_responder` | Initial node. Answers directly when no tools or memory are needed. It is skipped in an autonomous run. |
|
|
17
|
+
| `reasoning_engine` | Reads the situation or the results of the last actions, rewrites its working state, decides the next actions and reports the language of the conversation. |
|
|
18
|
+
| `action_engine` | Runs the actions the reasoning engine asked for, asks the user for confirmation when needed, delivers the final answer. |
|
|
19
|
+
|
|
20
|
+
The loop works within a budget per task (20 reasoning turns and 40 tool calls) and stops with an honest answer when the model repeats itself, keeps only reading notes, or runs out of budget. It also rejects final answers that cite identifiers that never appeared in a result or in the user's words.
|
|
21
|
+
|
|
22
|
+
```typescript
|
|
23
|
+
const agent = new Agent({ loop: new AgiLoop() })
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
`PlanExecuteLoop` (`openloops/loops`) is deprecated and will be removed. Do not build on it.
|
|
27
|
+
|
|
28
|
+
## The contract
|
|
29
|
+
|
|
30
|
+
```typescript
|
|
31
|
+
export class MyLoop extends AgentLoop {
|
|
32
|
+
get initialNode () {
|
|
33
|
+
return this.nodes.respond
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
get nodes () {
|
|
37
|
+
return {
|
|
38
|
+
respond: this.respond,
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
get name () {
|
|
43
|
+
return 'my-loop'
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
get version () {
|
|
47
|
+
return '1.0.0'
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
get author () {
|
|
51
|
+
return { name: 'You', email: 'you@example.com', website: 'https://example.com' }
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
private async respond (ctx: RunContext, harness: Harness): Promise<void> {
|
|
55
|
+
harness.reply('Hello!')
|
|
56
|
+
ctx.chat.state?.setStatus(AgentStatus.IDLE)
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
- `nodes` is a getter, so it can reference node methods declared later in the class.
|
|
62
|
+
- `initialNode` is where a new task starts. Return a direct reference such as `this.nodes.respond` so a typo is caught by TypeScript.
|
|
63
|
+
- `name` is stored on the chat so `openloops` knows which loop drives it. Bump `version` every time the loop or its prompts change; it is recorded in the metadata of every turn.
|
|
64
|
+
- A node decides what runs next with `harness.setNextNode(...)`. The loop stops when a node leaves the status as `idle`, `failed`, `awaiting_user_input` or `awaiting_user_confirmation`.
|
|
65
|
+
|
|
66
|
+
Nodes are called with the loop as `this`, so private helpers work as usual.
|
|
67
|
+
|
|
68
|
+
## `RunContext`
|
|
69
|
+
|
|
70
|
+
The first argument of a node is everything it needs to read:
|
|
71
|
+
|
|
72
|
+
```typescript
|
|
73
|
+
ctx.currentMessage
|
|
74
|
+
ctx.currentUser
|
|
75
|
+
ctx.currentSession
|
|
76
|
+
ctx.identity
|
|
77
|
+
ctx.chat
|
|
78
|
+
ctx.task
|
|
79
|
+
ctx.chatOptions
|
|
80
|
+
ctx.tools
|
|
81
|
+
ctx.skillParams
|
|
82
|
+
ctx.baseParams
|
|
83
|
+
ctx.logger
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
- `ctx.chat` is the `Chat` entity: state, settings, messages and tasks.
|
|
87
|
+
- `ctx.task` is the active `AgentTask`, if one exists.
|
|
88
|
+
- `ctx.skillParams` is what you spread into `Skill.run`. See [skills](skills.md).
|
|
89
|
+
- `ctx.baseParams` is what a `Tool` receives next to its arguments.
|
|
90
|
+
|
|
91
|
+
## `Harness`
|
|
92
|
+
|
|
93
|
+
The second argument is the runtime services the loop can call:
|
|
94
|
+
|
|
95
|
+
| Member | What it does |
|
|
96
|
+
| --- | --- |
|
|
97
|
+
| `reply(content)` | Sends a message to the user. |
|
|
98
|
+
| `executionMode` | `interactive` or `autonomous`, taken from the run. |
|
|
99
|
+
| `answerQuestion(question)` | In an autonomous run, answers a question of the model on behalf of the absent user and returns the answer, or `undefined` when it cannot. In an interactive run it always returns `undefined`, and the loop pauses and asks the user. |
|
|
100
|
+
| `setNextNode(node)` | Chooses the next node. `undefined` falls back to the initial node on the next message. |
|
|
101
|
+
| `toolCatalog()` | Returns the tools to declare to the model and the context that describes the rest. Handles discovery and MCP servers. |
|
|
102
|
+
| `searchTools(keywords, opts)` | Ranks the available tools by keyword. `opts.server` limits it to one MCP server. |
|
|
103
|
+
| `ensureTools()` | Loads the list of MCP servers and returns the tools known so far. |
|
|
104
|
+
| `runAction(action, opts)` | Runs an action through the tool pipeline. |
|
|
105
|
+
| `requiresApproval(action)` | Tells whether an action needs the user's confirmation before it can run. |
|
|
106
|
+
| `requestApproval(actions, { resumeAt })` | Asks the user to confirm and resumes at `resumeAt` once they answer. |
|
|
107
|
+
| `reopenTask(taskId)` | Continues a task that was already completed. |
|
|
108
|
+
| `workspace` | Saves and reads notes and large outputs of the task. |
|
|
109
|
+
|
|
110
|
+
Always go through `runAction`. It applies validation, grounding checks, duplicate protection, output offloading and the approval rules described in [harness](harness.md#tool-pipeline). Do not call `tool.run` yourself.
|
|
111
|
+
|
|
112
|
+
## Tasks
|
|
113
|
+
|
|
114
|
+
A task is one thing the user asked the agent to do. A later "actually, search the web instead" starts a different task in the same chat.
|
|
115
|
+
|
|
116
|
+
```typescript
|
|
117
|
+
const task = ctx.task ?? ctx.chat.createTask(ctx.currentMessage)
|
|
118
|
+
|
|
119
|
+
ctx.chat.completeTask(task.id, 'summary')
|
|
120
|
+
ctx.chat.failTask(task.id)
|
|
121
|
+
ctx.chat.cancelTask(task.id)
|
|
122
|
+
ctx.chat.lastCompletedTask()
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
A task carries its ledger of actions (tool calls), the turns of the model, what the model learned and the schemas of the tools it has seen.
|
|
126
|
+
|
|
127
|
+
## Running skills
|
|
128
|
+
|
|
129
|
+
```typescript
|
|
130
|
+
const result = await mySkill.run({ ...ctx.skillParams, contextInjection: { goal: ctx.task?.goal } })
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
Every skill run is traced automatically. See [skills](skills.md).
|
|
134
|
+
|
|
135
|
+
## Shared skills
|
|
136
|
+
|
|
137
|
+
`openloops/loops` also exports the skills that every loop can reuse:
|
|
138
|
+
|
|
139
|
+
| Skill | Purpose |
|
|
140
|
+
| --- | --- |
|
|
141
|
+
| `fastResponder` | Answers general conversation directly or says it cannot. |
|
|
142
|
+
| `approvalRequest` | Writes the question that asks the user to confirm pending actions. |
|
|
143
|
+
| `confirmationGate` | Reads the user's answer and decides confirmed, declined or unclear. |
|
|
144
|
+
| `mcpChangeReview` | Reviews what changed in an MCP server and writes the approval question. |
|
|
145
|
+
| `autonomousAnswer` | Answers a question of the model on behalf of an absent user, only from the context of the task. |
|
|
146
|
+
|
|
147
|
+
`Agent` already runs `approvalRequest`, `confirmationGate` and `mcpChangeReview` for you when you use `harness.requestApproval`, and `autonomousAnswer` through `harness.answerQuestion`.
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
# MCP servers
|
|
2
|
+
|
|
3
|
+
Each user can register their own [MCP](https://modelcontextprotocol.io) servers. `openloops` connects to them when the agent needs them and converts their tools into regular `Tool` instances. Your loop never needs to know MCP exists.
|
|
4
|
+
|
|
5
|
+
MCP servers are third parties and can change their tools at any time, so every server goes through a trust flow before the agent is allowed to run any of its tools. Trust is per user: a server that one user approves is not approved for anyone else.
|
|
6
|
+
|
|
7
|
+
```typescript
|
|
8
|
+
import {
|
|
9
|
+
createMcpServer,
|
|
10
|
+
getMcpServerById,
|
|
11
|
+
listMcpServersByUser,
|
|
12
|
+
listAllMcpServersByUser,
|
|
13
|
+
updateMcpServer,
|
|
14
|
+
deleteMcpServer,
|
|
15
|
+
verifyMcpServerConnection,
|
|
16
|
+
decideMcpServerTrust,
|
|
17
|
+
} from 'openloops/mcps'
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Registering a server
|
|
21
|
+
|
|
22
|
+
```typescript
|
|
23
|
+
const result = await createMcpServer({
|
|
24
|
+
payload: {
|
|
25
|
+
name: 'my-mcp',
|
|
26
|
+
type: 'http',
|
|
27
|
+
url: 'https://example.dev/api/mcp',
|
|
28
|
+
headers: {
|
|
29
|
+
Authorization: 'Bearer ...',
|
|
30
|
+
},
|
|
31
|
+
},
|
|
32
|
+
currentUser,
|
|
33
|
+
})
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
Only `type: 'http'` is supported. `stdio` (which would mean running an arbitrary local process on your server) is intentionally not allowed.
|
|
37
|
+
|
|
38
|
+
`headers` is a plain key-value map, so you're not limited to `Authorization`.
|
|
39
|
+
|
|
40
|
+
Creating a server connects to it once to read its manifest (tools and instructions) and returns the result of that check. The new server starts as `locked`: nothing runs until the user approves it.
|
|
41
|
+
|
|
42
|
+
## The server record
|
|
43
|
+
|
|
44
|
+
| Field | Meaning |
|
|
45
|
+
| --- | --- |
|
|
46
|
+
| `name`, `url`, `type`, `headers` | What you registered. |
|
|
47
|
+
| `trust` | `locked` (never approved, also the value of old records without one), `approved` or `blocked`. |
|
|
48
|
+
| `approvedHash` | Hash of the manifest the user approved. |
|
|
49
|
+
| `history` | The last 5 manifests the server has shown, newest first. The approved one is never dropped. |
|
|
50
|
+
| `status` | `ok`, `error` or `unknown`, from the last connection check. Use it for a green or red dot. |
|
|
51
|
+
| `lastError` | Sanitized message of the last failed check, without header values or the url. |
|
|
52
|
+
|
|
53
|
+
The latest manifest is `history[0]`. The approved manifest is the entry of `history` whose hash equals `approvedHash`. What changed between them is computed when it is needed and is never stored.
|
|
54
|
+
|
|
55
|
+
`headers` and `history` are returned as they are stored. If your API exposes these records to a client, mask the header values and leave out `history` in your controller.
|
|
56
|
+
|
|
57
|
+
## Listing a user's servers
|
|
58
|
+
|
|
59
|
+
```typescript
|
|
60
|
+
const page = await listMcpServersByUser({
|
|
61
|
+
options: { page: 1, limit: 10 },
|
|
62
|
+
currentUser,
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
const all = await listAllMcpServersByUser({ currentUser })
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
`listMcpServersByUser` is paginated (page 1, 10 per page by default) and sorted by last update, newest first. Use it for a management screen.
|
|
69
|
+
|
|
70
|
+
`listAllMcpServersByUser` returns every server of the user, oldest first. This is what the agent uses, so a user can have any number of servers.
|
|
71
|
+
|
|
72
|
+
## Getting one by id
|
|
73
|
+
|
|
74
|
+
```typescript
|
|
75
|
+
const result = await getMcpServerById({ id: mcpServerId, currentUser })
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
## Updating a server
|
|
79
|
+
|
|
80
|
+
```typescript
|
|
81
|
+
const result = await updateMcpServer({
|
|
82
|
+
id: mcpServerId,
|
|
83
|
+
payload: {
|
|
84
|
+
name: 'my-mcp-updated',
|
|
85
|
+
headers: { Authorization: 'Bearer <new-token>' },
|
|
86
|
+
__v: current.__v,
|
|
87
|
+
},
|
|
88
|
+
currentUser,
|
|
89
|
+
})
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
- `trust`, `approvedHash`, `status`, `lastError` and `history` in the payload are ignored. They only change through the trust decision and the connection check, so you can send a whole record back without overwriting an approval.
|
|
93
|
+
- Changing `url` or `type` locks the server again (`trust` is `locked` and `approvedHash` is cleared) and checks the connection. The history is kept for traceability.
|
|
94
|
+
- Changing `headers` checks the connection again and keeps the approval. If the server answers with a different manifest, the agent treats it as a change and asks.
|
|
95
|
+
- If you send `__v` and the record was modified in the meantime, the update fails with a `VersionConflictError` (code `VERSION_CONFLICT`, status 409). Reload and try again.
|
|
96
|
+
|
|
97
|
+
## Deleting a server
|
|
98
|
+
|
|
99
|
+
```typescript
|
|
100
|
+
await deleteMcpServer({ id: mcpServerId, currentUser })
|
|
101
|
+
|
|
102
|
+
await deleteMcpServer({ id: mcpServerId, hardDelete: true, currentUser })
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
The first one is a soft delete. A hard delete is for admins only.
|
|
106
|
+
|
|
107
|
+
## Checking the connection
|
|
108
|
+
|
|
109
|
+
```typescript
|
|
110
|
+
const result = await verifyMcpServerConnection({ id: mcpServerId, currentUser })
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
Connects, reads the manifest and stores it in `history` if it is new, or stores a sanitized error and `status: 'error'`. The agent calls it by itself when it sees that a server changed. You can call it from a "test connection" button.
|
|
114
|
+
|
|
115
|
+
## Approving or blocking a server
|
|
116
|
+
|
|
117
|
+
```typescript
|
|
118
|
+
await decideMcpServerTrust({
|
|
119
|
+
id: mcpServerId,
|
|
120
|
+
decision: 'approve',
|
|
121
|
+
manifestHash: result.data.history[0].hash,
|
|
122
|
+
currentUser,
|
|
123
|
+
})
|
|
124
|
+
|
|
125
|
+
await decideMcpServerTrust({ id: mcpServerId, decision: 'block', currentUser })
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
- Approving requires the hash of the manifest the user reviewed, and it must be the latest one in `history`. If the server changed since the user looked at it, the call fails and the user has to review again.
|
|
129
|
+
- A blocked server can be approved again the same way, which unblocks it.
|
|
130
|
+
- The agent makes the same call when the user answers yes or no in the chat, so a screen in your app and the chat are two ways of reaching the same decision.
|
|
131
|
+
|
|
132
|
+
## How the agent uses the servers
|
|
133
|
+
|
|
134
|
+
1. At the start of a turn the model sees an index of the user's servers: `name`, an optional `description` (the server's instructions, truncated) and a `state`: `approved`, `not_approved` or `blocked`. Nothing is connected yet.
|
|
135
|
+
2. When the model needs a capability, it lists the tools of a server with `search_tools` (with the `server` argument) or searches across all of them. Listing needs no approval. A blocked server cannot be listed.
|
|
136
|
+
3. The agent connects to a server the first time it is needed in a run. It compares the manifest the server shows now with the approved one and stores it if it is new.
|
|
137
|
+
4. Approval is asked when a tool is about to run, never before:
|
|
138
|
+
- First use of a server: always asks.
|
|
139
|
+
- The server changed after being approved: asks if a tool was added, a parameter or annotation changed, or the instructions changed. A review skill decides the harmless cases (a description rewritten, a tool removed) and approves them on behalf of the user. With `strictMode: true` it always asks.
|
|
140
|
+
5. The question is written from what changed, not from the whole server. A yes approves the manifest the user was shown, once per server and version, not once per tool. A no blocks the server.
|
|
141
|
+
|
|
142
|
+
A server that cannot be reached after a few retries is reported as unavailable for that run and the others keep working. A tool that does not answer within `MCP_TOOL_TIMEOUT_MS` (60 seconds by default, restarted by progress notifications) fails without being repeated, and the model is told that the result is unknown. See [configuration](configuration.md#timeouts).
|
|
143
|
+
|
|
144
|
+
Connections are closed at the end of every turn, including when the turn fails.
|
|
145
|
+
|
|
146
|
+
## Tools of MCP servers
|
|
147
|
+
|
|
148
|
+
Their names are prefixed with the server name, and they are marked as external so the model treats their descriptions as data. A tool is destructive when the server marks it with `destructiveHint`. A tool without that annotation is not treated as destructive, so rely on the server's approval and on your own review of what the server exposes.
|
package/docs/skills.md
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
# Skills
|
|
2
|
+
|
|
3
|
+
A `Skill` is a typed, structured call to an LLM: the way a node inside your `AgentLoop` reasons about something. Where a `Tool` *does* something in the world, a `Skill` *decides* something.
|
|
4
|
+
|
|
5
|
+
```typescript
|
|
6
|
+
import { Skill } from 'openloops/skills'
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
## Defining a skill
|
|
10
|
+
|
|
11
|
+
```typescript
|
|
12
|
+
export type ResponseSchema = {
|
|
13
|
+
can_answer: boolean
|
|
14
|
+
answer: string
|
|
15
|
+
reasoning: string
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export const myResponder = new Skill<ResponseSchema>({
|
|
19
|
+
name: 'my_responder',
|
|
20
|
+
version: '1.0.0',
|
|
21
|
+
description: 'Decides whether the request can be answered directly.',
|
|
22
|
+
temperature: 0.3,
|
|
23
|
+
systemInstructions: {
|
|
24
|
+
instruction_set: {
|
|
25
|
+
goal: 'Decide if the user message can be answered without any tools.',
|
|
26
|
+
role: 'You are the first filter of an assistant.',
|
|
27
|
+
logic_rules: [
|
|
28
|
+
'OUTPUT_FORMAT: Every response is a single JSON object matching response_schema. Nothing outside that JSON.',
|
|
29
|
+
],
|
|
30
|
+
response_schema: {
|
|
31
|
+
can_answer: 'boolean',
|
|
32
|
+
answer: 'string',
|
|
33
|
+
reasoning: 'string',
|
|
34
|
+
},
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
})
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
| Prop | Required | What it does |
|
|
41
|
+
| --- | --- | --- |
|
|
42
|
+
| `name` | yes | Identifies the skill in traces and usage records. |
|
|
43
|
+
| `systemInstructions` | yes | A plain object the model follows. The bundled skills use `instruction_set` with `goal`, `role`, `logic_rules` and `response_schema`, which is a good shape to copy. |
|
|
44
|
+
| `version` | no | Recorded in the metadata of every call. Bump it whenever the prompt changes. |
|
|
45
|
+
| `description` | no | What the skill is for. |
|
|
46
|
+
| `temperature` | no | Sampling temperature. Defaults to `0.7`. |
|
|
47
|
+
| `systemRole` | no | Default `true`: the instructions travel as a system message. |
|
|
48
|
+
| `mode` | no | How the model is called when tools are given. Defaults to `plain_chat`. |
|
|
49
|
+
|
|
50
|
+
## Running a skill
|
|
51
|
+
|
|
52
|
+
Inside a loop node:
|
|
53
|
+
|
|
54
|
+
```typescript
|
|
55
|
+
const result = await myResponder.run({
|
|
56
|
+
...ctx.skillParams,
|
|
57
|
+
contextInjection: {
|
|
58
|
+
goal: ctx.task?.goal,
|
|
59
|
+
},
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
if (result.can_answer) {
|
|
63
|
+
harness.reply(result.answer)
|
|
64
|
+
}
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
`ctx.skillParams` already carries the provider and model chosen for the chat, `sessionId`, `answerId`, the user's message, `currentUser`, `currentSession` and the current chat. You normally only add `contextInjection`.
|
|
68
|
+
|
|
69
|
+
| Run option | What it does |
|
|
70
|
+
| --- | --- |
|
|
71
|
+
| `contextInjection` | Extra keys placed under `CONTEXT`. |
|
|
72
|
+
| `input` | Extra keys placed under `INPUT`, next to `user_message`. |
|
|
73
|
+
| `history` | The conversation of a reasoning cycle, as messages. Used by loops that keep a state between cycles. |
|
|
74
|
+
| `chatHistory` | Previous user and assistant turns. Cannot be combined with `history`. |
|
|
75
|
+
| `tools` | Tool definitions to show the model. |
|
|
76
|
+
| `mode` | Overrides the mode of the skill for this call. |
|
|
77
|
+
|
|
78
|
+
`tools`, `mode`, `history` and `chatHistory` are run options. Putting them inside `contextInjection` throws.
|
|
79
|
+
|
|
80
|
+
## What the model always receives
|
|
81
|
+
|
|
82
|
+
Besides your `contextInjection`, every call gets the current UTC and local time, time zone, day of the week, the user's name and language, the session's device and location, and the conversation language (`detected_language`) when an earlier node has already detected it. Skills do not need to detect it again.
|
|
83
|
+
|
|
84
|
+
## The result
|
|
85
|
+
|
|
86
|
+
The parsed JSON object comes back with a few extras:
|
|
87
|
+
|
|
88
|
+
| Field | Meaning |
|
|
89
|
+
| --- | --- |
|
|
90
|
+
| `costInUsd` | Cost of the call. |
|
|
91
|
+
| `responseId` | Provider response id. |
|
|
92
|
+
| `opening` | The first message of the call, which loops store to keep the opening stable between cycles. |
|
|
93
|
+
| `callInfo` | `skill`, `skillVersion`, `mode`, `model`, `provider`, number of `blocks` and, when the model did not return JSON, `rawText`. |
|
|
94
|
+
|
|
95
|
+
Every call is traced, including failures. When a model sends several text blocks in one answer, the first complete one is used and the rest are discarded, which is noted in the trace.
|
|
96
|
+
|
|
97
|
+
## Call modes
|
|
98
|
+
|
|
99
|
+
| Mode | When to use it |
|
|
100
|
+
| --- | --- |
|
|
101
|
+
| `plain_chat` | Default. The tool list travels as text and the model answers with the JSON as text. It was the best or tied for best in the evals and works with any provider. |
|
|
102
|
+
| `function_calling` | One forced call per cycle with the whole response as arguments. Needs a provider with forced tool choice. |
|
|
103
|
+
| `function_calling_split` | Native function calling for each action. Experimental. |
|
|
104
|
+
| `legacy_tools_off` | Do not use. It scored 0 to 30% in the evals. |
|
package/docs/tools.md
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
# Tools
|
|
2
|
+
|
|
3
|
+
A `Tool` is a single capability your agent can call, such as searching the web, creating a record, or sending a message. Loops never care whether a tool is native to your code or came from a connected MCP server; both are the same `Tool` class by the time a loop sees them.
|
|
4
|
+
|
|
5
|
+
```typescript
|
|
6
|
+
import { Tool, BaseParams } from 'openloops/tools'
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
## Writing a custom tool
|
|
10
|
+
|
|
11
|
+
```typescript
|
|
12
|
+
interface Params extends BaseParams {
|
|
13
|
+
firstName: string
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
interface Output {
|
|
17
|
+
data: { greeting: string }
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export const greetUserTool = new Tool({
|
|
21
|
+
name: 'greet_user',
|
|
22
|
+
description: 'Greet the user',
|
|
23
|
+
destructive: false,
|
|
24
|
+
parameters: {
|
|
25
|
+
type: 'object',
|
|
26
|
+
properties: {
|
|
27
|
+
firstName: { type: 'string', description: 'First name of the user' },
|
|
28
|
+
},
|
|
29
|
+
required: ['firstName'],
|
|
30
|
+
},
|
|
31
|
+
handler: async (params: Params): Promise<Output> => {
|
|
32
|
+
const { currentUser, sessionId, answerId, firstName } = params
|
|
33
|
+
return { data: { greeting: `Hi ${firstName}` } }
|
|
34
|
+
},
|
|
35
|
+
})
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
| Prop | What it does |
|
|
39
|
+
| --- | --- |
|
|
40
|
+
| `name` | Letters, numbers, `_` and `-`, up to 64 characters. This is the name the model writes in an action. |
|
|
41
|
+
| `description` | What the tool does. Write it for the model. |
|
|
42
|
+
| `parameters` | JSON schema of the arguments. |
|
|
43
|
+
| `destructive` | `true` makes the agent ask the user to confirm before it runs. Defaults to `false`, so set it explicitly for anything that changes or deletes data. |
|
|
44
|
+
| `handler` | Receives the arguments plus `BaseParams` and returns `{ data }`. |
|
|
45
|
+
|
|
46
|
+
`BaseParams` (`currentUser`, `sessionId`, `answerId`) is always merged into the arguments your handler receives. Destructure and ignore the ones you don't need.
|
|
47
|
+
|
|
48
|
+
A `Tool` instance is shared. Never write to `this` inside a handler. Everything that varies between calls comes in through `params`.
|
|
49
|
+
|
|
50
|
+
## Argument validation
|
|
51
|
+
|
|
52
|
+
Arguments are validated against `parameters` before the handler runs: required arguments, types and `enum` values. An invalid call comes back to the model as a failed action with a message it can use to correct itself. Tools from MCP servers are validated strictly, so an argument that is not in the schema is rejected.
|
|
53
|
+
|
|
54
|
+
## Registering a tool
|
|
55
|
+
|
|
56
|
+
```typescript
|
|
57
|
+
import { Agent } from 'openloops/core'
|
|
58
|
+
import { AgiLoop } from 'openloops/loops'
|
|
59
|
+
|
|
60
|
+
const agent = new Agent({ loop: new AgiLoop() })
|
|
61
|
+
agent.addTool(greetUserTool)
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
When your agent has more than 15 local tools, the model no longer receives the whole catalog. It sees a summary per source and finds the tools it needs with the native `search_tools`. See [harness](harness.md#tool-discovery).
|
|
65
|
+
|
|
66
|
+
## Built-in tools
|
|
67
|
+
|
|
68
|
+
### `webSearchTool`
|
|
69
|
+
|
|
70
|
+
```typescript
|
|
71
|
+
import { webSearchTool } from 'openloops/tools'
|
|
72
|
+
|
|
73
|
+
agent.addTool(webSearchTool)
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
Requires a [Serper](https://serper.dev) API key. Set `SERPER_API_KEY` in your `.env`.
|
|
77
|
+
|
|
78
|
+
### `callAiTool`
|
|
79
|
+
|
|
80
|
+
An LLM-backed tool for ad-hoc analysis, summarization, extraction or transformation. It is useful as a fallback when no other tool fits a step.
|
|
81
|
+
|
|
82
|
+
## MCP tools
|
|
83
|
+
|
|
84
|
+
Tools exposed by a connected MCP server are converted to `Tool` instances by `openloops`. See [MCP servers](mcp-servers.md). Their names are prefixed with the server's name (for example `my-server__search`) so two servers never collide.
|
|
85
|
+
|
|
86
|
+
An MCP tool differs from a local one in three ways:
|
|
87
|
+
|
|
88
|
+
- `origin` is `mcp`, `server` is the server's name and `notice` is set. The notice tells the model that the description was written by a third party and is not an instruction from the user.
|
|
89
|
+
- `destructive` comes from the server's `destructiveHint` annotation and is `false` when the annotation is missing.
|
|
90
|
+
- It cannot run until the user has approved its server.
|