@emabuild/ai 0.1.1 → 0.9.0
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/LICENSE +21 -0
- package/README.md +139 -13
- package/dist/cli.js +45 -7
- package/dist/cli.js.map +1 -1
- package/dist/handler.d.ts +3 -2
- package/dist/handler.d.ts.map +1 -1
- package/dist/handler.js +4 -1
- package/dist/handler.js.map +1 -1
- package/dist/index.d.ts +9 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +9 -2
- package/dist/index.js.map +1 -1
- package/dist/loop.d.ts +26 -0
- package/dist/loop.d.ts.map +1 -0
- package/dist/loop.js +215 -0
- package/dist/loop.js.map +1 -0
- package/dist/providers/anthropic.d.ts +58 -0
- package/dist/providers/anthropic.d.ts.map +1 -0
- package/dist/providers/anthropic.js +217 -0
- package/dist/providers/anthropic.js.map +1 -0
- package/dist/providers/errors.d.ts +12 -0
- package/dist/providers/errors.d.ts.map +1 -0
- package/dist/providers/errors.js +39 -0
- package/dist/providers/errors.js.map +1 -0
- package/dist/providers/model-call.d.ts +17 -0
- package/dist/providers/model-call.d.ts.map +1 -0
- package/dist/providers/model-call.js +117 -0
- package/dist/providers/model-call.js.map +1 -0
- package/dist/providers/openai-format.d.ts +125 -0
- package/dist/providers/openai-format.d.ts.map +1 -0
- package/dist/providers/openai-format.js +214 -0
- package/dist/providers/openai-format.js.map +1 -0
- package/dist/providers/types.d.ts +193 -0
- package/dist/providers/types.d.ts.map +1 -0
- package/dist/providers/types.js +22 -0
- package/dist/providers/types.js.map +1 -0
- package/dist/run.d.ts +40 -38
- package/dist/run.d.ts.map +1 -1
- package/dist/run.js +44 -323
- package/dist/run.js.map +1 -1
- package/dist/usage.d.ts +7 -1
- package/dist/usage.d.ts.map +1 -1
- package/dist/usage.js +35 -6
- package/dist/usage.js.map +1 -1
- package/package.json +24 -8
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Fabio Varrenti
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
# @emabuild/ai
|
|
2
2
|
|
|
3
|
-
Server side of the Emabuild editor's AI connector. It takes an `AIRequest` from the editor (prompt, business context, current design, tool schema),
|
|
3
|
+
Server side of the Emabuild editor's AI connector. It takes an `AIRequest` from the editor (prompt, business context, current design, tool schema), runs the editing flow with a model, and streams `AIStreamEvent`s back as NDJSON. The editor applies each `operations` event live on the canvas, so users watch the email being built.
|
|
4
4
|
|
|
5
|
-
-
|
|
5
|
+
- **The library runs the flow; you choose the model.** Prompts, tools, validation of every batch, corrections sent back to the model, image search, turn limit and the editor events are handled here. The model answering each turn is Claude by default, or anything you plug in with `callModel` (OpenAI, Azure OpenAI, Cerebras, Groq, a local server…).
|
|
6
|
+
- The built-in Claude adapter uses `@anthropic-ai/sdk`, an **optional** peer dependency: install it only if you use Claude (`npm install @anthropic-ai/sdk`). The default model is `claude-opus-5-5`.
|
|
6
7
|
- Web Fetch handler (`Request → Response`). It runs on Node 18+, Bun, Deno, Cloudflare Workers, Next.js and Hono. A `node:http` / Express adapter is included.
|
|
7
8
|
- Includes a standalone dev server (`emabuild-ai-server`).
|
|
8
9
|
|
|
@@ -13,7 +14,7 @@ pnpm add @emabuild/ai
|
|
|
13
14
|
## Quick start (dev server)
|
|
14
15
|
|
|
15
16
|
```bash
|
|
16
|
-
ANTHROPIC_API_KEY
|
|
17
|
+
ANTHROPIC_API_KEY=<your-key> npx emabuild-ai-server
|
|
17
18
|
# → listening on http://127.0.0.1:8787/ai
|
|
18
19
|
```
|
|
19
20
|
|
|
@@ -25,15 +26,122 @@ editor.init({ ai: { endpoint: 'http://127.0.0.1:8787/ai' } });
|
|
|
25
26
|
|
|
26
27
|
| Env var | Default | Description |
|
|
27
28
|
|---|---|---|
|
|
29
|
+
| `EMABUILD_AI_PROVIDER` | `anthropic` | `anthropic`, or `openai` for any OpenAI-compatible server |
|
|
28
30
|
| `ANTHROPIC_API_KEY` | none | Anthropic API key. If it is unset, the SDK's credential chain is used (`ANTHROPIC_AUTH_TOKEN`, `ant auth login` profiles). |
|
|
29
|
-
| `
|
|
31
|
+
| `OPENAI_API_KEY` / `EMABUILD_AI_API_KEY` | none | Key for `EMABUILD_AI_PROVIDER=openai` |
|
|
32
|
+
| `EMABUILD_AI_BASE_URL` | OpenAI | Base URL of an OpenAI-compatible server, e.g. `https://api.cerebras.ai/v1` |
|
|
33
|
+
| `EMABUILD_AI_MODEL` | `claude-opus-5-5` (`gpt-4o` with openai) | Model id |
|
|
30
34
|
| `EMABUILD_AI_EFFORT` | `medium` | `low` \| `medium` \| `high` \| `xhigh` \| `max` |
|
|
31
|
-
| `EMABUILD_AI_CORS` |
|
|
35
|
+
| `EMABUILD_AI_CORS` | pages on this machine (`localhost`, `127.0.0.1`, `[::1]`, any port) | `*`, allowed origin(s) comma separated, or `false`. The local default stops any website you visit from using your API key through the server. |
|
|
36
|
+
| `EMABUILD_AI_TOKEN` | none | When set, requests need `Authorization: Bearer <token>` (in the editor: `ai.headers`). Set it whenever `HOST` is not loopback. |
|
|
32
37
|
| `PORT` | `8787` | Port |
|
|
33
38
|
| `HOST` | `127.0.0.1` | Bind address (`0.0.0.0` to expose it) |
|
|
34
39
|
|
|
35
40
|
Routes: `POST /ai` (plus `OPTIONS` preflight) and `GET /health`.
|
|
36
41
|
|
|
42
|
+
## Choosing the model
|
|
43
|
+
|
|
44
|
+
Each run is a conversation: the library sends out the prompt, your model answers, the library validates the answer, applies it in the editor and sends the next turn (with the errors to fix, if any), until the model calls `finish`.
|
|
45
|
+
|
|
46
|
+
```
|
|
47
|
+
editor ──AIRequest──▶ flow (this library) ──ModelRequest──▶ your callModel ──▶ any model
|
|
48
|
+
editor ◀──events──── flow ◀──────────────── ModelResponse ◀──────────────────┘
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
### `callModel`: any model, your code
|
|
52
|
+
|
|
53
|
+
```ts
|
|
54
|
+
createAIHandler({
|
|
55
|
+
callModel: async ({ system, messages, tools, maxTokens, effort, signal }) => {
|
|
56
|
+
// call any model here…
|
|
57
|
+
return { text, toolCalls, stop, usage, model };
|
|
58
|
+
},
|
|
59
|
+
});
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
| `ModelRequest` field | Description |
|
|
63
|
+
|---|---|
|
|
64
|
+
| `system` | System prompt (stable parts first: good for providers that cache prefixes) |
|
|
65
|
+
| `messages` | Conversation in a neutral format: `{ role: 'user', content }`, `{ role: 'assistant', content, toolCalls? }`, `{ role: 'tool', toolCallId, content, isError? }` |
|
|
66
|
+
| `tools` | `{ name, description, inputSchema }` (JSON Schema; send it non-strict) |
|
|
67
|
+
| `maxTokens`, `effort` | What the caller asked for; map them where your model supports them |
|
|
68
|
+
| `signal` | Aborted when the user stops the run: pass it to your HTTP call |
|
|
69
|
+
|
|
70
|
+
| `ModelResponse` field | Description |
|
|
71
|
+
|---|---|
|
|
72
|
+
| `text` | Assistant text (shown in the AI panel) |
|
|
73
|
+
| `toolCalls` | `{ id, name, arguments }` with `arguments` as a JSON string |
|
|
74
|
+
| `stop` | `'end' \| 'tool_calls' \| 'max_tokens' \| 'refusal'` (inferred from `toolCalls` when omitted) |
|
|
75
|
+
| `usage` | `{ input, output, cacheRead? }` tokens, for the `usage` event and the cost estimate |
|
|
76
|
+
| `model` | Model that answered (for pricing) |
|
|
77
|
+
|
|
78
|
+
Errors thrown by `callModel` end the run with an `error` event (HTTP `status` 401 / 403 / 404 / 429 / 400 become the usual messages). They are not retried.
|
|
79
|
+
|
|
80
|
+
#### OpenAI (official SDK)
|
|
81
|
+
|
|
82
|
+
`toOpenAIChat` / `fromOpenAIChat` convert between this contract and Chat Completions (plain data, no SDK inside the library):
|
|
83
|
+
|
|
84
|
+
```ts
|
|
85
|
+
import OpenAI from 'openai';
|
|
86
|
+
import { createAIHandler, toOpenAIChat, fromOpenAIChat } from '@emabuild/ai';
|
|
87
|
+
|
|
88
|
+
const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
|
|
89
|
+
|
|
90
|
+
createAIHandler({
|
|
91
|
+
model: 'gpt-4o',
|
|
92
|
+
callModel: async (req) => fromOpenAIChat(
|
|
93
|
+
await openai.chat.completions.create({ model: 'gpt-4o', ...toOpenAIChat(req) }, { signal: req.signal }),
|
|
94
|
+
),
|
|
95
|
+
});
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
#### OpenAI-compatible servers (Cerebras, Groq, Azure, local), no SDK
|
|
99
|
+
|
|
100
|
+
```ts
|
|
101
|
+
import { createAIHandler, openAICompatibleModel } from '@emabuild/ai';
|
|
102
|
+
|
|
103
|
+
createAIHandler({
|
|
104
|
+
callModel: openAICompatibleModel({
|
|
105
|
+
baseURL: 'https://api.cerebras.ai/v1',
|
|
106
|
+
apiKey: process.env.CEREBRAS_API_KEY,
|
|
107
|
+
model: 'gpt-oss-120b',
|
|
108
|
+
}),
|
|
109
|
+
});
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
`openAICompatibleModel({ model, apiKey?, baseURL?, headers?, body?, maxTokensParam?, stream?, fetch? })` posts to `{baseURL}/chat/completions` with `fetch`. Default base URL: OpenAI. Errors keep the provider's message, never your key. With `stream: true` it reads the answer as Server-Sent Events, so each batch of changes reaches the editor while the model is still writing.
|
|
113
|
+
|
|
114
|
+
#### Streaming from your own code
|
|
115
|
+
|
|
116
|
+
`callModel` can also return a stream of events instead of a promise: `{ type: 'text', text }`, `{ type: 'toolCall', call }` (send it when its arguments are complete; the editor applies it right away), `{ type: 'usage', usage, model? }`, `{ type: 'stop', reason }`. For the OpenAI SDK, `fromOpenAIChatStream` does the conversion:
|
|
117
|
+
|
|
118
|
+
```ts
|
|
119
|
+
callModel: async function* (req) {
|
|
120
|
+
const stream = await openai.chat.completions.create(
|
|
121
|
+
{ model: 'gpt-4o', ...toOpenAIChat(req), stream: true, stream_options: { include_usage: true } },
|
|
122
|
+
{ signal: req.signal },
|
|
123
|
+
);
|
|
124
|
+
yield* fromOpenAIChatStream(stream);
|
|
125
|
+
},
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
#### Claude (built in, default)
|
|
129
|
+
|
|
130
|
+
No `callModel`: the built-in adapter streams from the Messages API with prompt caching, progress notes, server-side refusal fallbacks and `effort`. Pass `client` for Bedrock / Vertex / Foundry.
|
|
131
|
+
|
|
132
|
+
#### Capabilities
|
|
133
|
+
|
|
134
|
+
| | Claude (built in) | `callModel` |
|
|
135
|
+
|---|---|---|
|
|
136
|
+
| Live operations while the model writes | per tool call, streamed | per tool call when `callModel` streams (`stream: true`, `fromOpenAIChatStream`), else per turn |
|
|
137
|
+
| Progress notes in the AI panel | yes | no |
|
|
138
|
+
| Prompt caching | explicit (system + conversation) | whatever your provider does (OpenAI caches long prefixes automatically) |
|
|
139
|
+
| Refusal handling | `refusal` stop + server fallbacks | `stop: 'refusal'` if you report it |
|
|
140
|
+
| Token usage / cost | yes | if you return `usage` (prices for common OpenAI models are built in) |
|
|
141
|
+
| Extra dependency | `@anthropic-ai/sdk` | none (your own SDK, if any) |
|
|
142
|
+
|
|
143
|
+
For full control (streaming, your own conversation format) implement a `ModelAdapter` and pass it as `adapter`.
|
|
144
|
+
|
|
37
145
|
## Protocol
|
|
38
146
|
|
|
39
147
|
**Request:** `POST` with a JSON `AIRequest` (from `@emabuild/types`):
|
|
@@ -99,7 +207,23 @@ app.listen(8787);
|
|
|
99
207
|
|
|
100
208
|
Plain `node:http`: `http.createServer(toNodeHandler(createAIHandler())).listen(8787)`.
|
|
101
209
|
|
|
102
|
-
>
|
|
210
|
+
> The stream is sent with `Cache-Control: no-transform`, which the `compression` middleware respects, so it is not buffered. If your compression ignores that header, disable it on this route.
|
|
211
|
+
|
|
212
|
+
#### CommonJS apps (dynamic import) with your own session auth
|
|
213
|
+
|
|
214
|
+
```js
|
|
215
|
+
// server.js (CommonJS), after app.use(express.json())
|
|
216
|
+
(async () => {
|
|
217
|
+
const { createAIHandler, toNodeHandler, openAICompatibleModel } = await import('@emabuild/ai');
|
|
218
|
+
const ai = toNodeHandler(createAIHandler({
|
|
219
|
+
callModel: openAICompatibleModel({ model: 'gpt-4o', apiKey: config.openai.apiKey }),
|
|
220
|
+
transformRequest: (request) => ({ ...request, context: { ...request.context, brand: currentBrand } }),
|
|
221
|
+
}));
|
|
222
|
+
app.post('/api/newsletters/ai', requireSession, ai); // your auth middleware first
|
|
223
|
+
})();
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
The editor only needs `ai: { endpoint: '/api/newsletters/ai' }`.
|
|
103
227
|
|
|
104
228
|
### Next.js (App Router)
|
|
105
229
|
|
|
@@ -152,18 +276,20 @@ for await (const event of runAI(aiRequest, { signal })) {
|
|
|
152
276
|
|
|
153
277
|
| Option | Description |
|
|
154
278
|
|---|---|
|
|
155
|
-
| `
|
|
156
|
-
| `
|
|
157
|
-
| `
|
|
158
|
-
| `
|
|
279
|
+
| `callModel` | Your function that answers each model turn (any provider). See *Choosing the model*. |
|
|
280
|
+
| `adapter` | A full `ModelAdapter` (takes precedence over `callModel`) |
|
|
281
|
+
| `client` | Claude: pre-configured `Anthropic` client (Bedrock/Vertex/Foundry, custom `baseURL`, retries) |
|
|
282
|
+
| `apiKey` | Claude: API key. If omitted, the SDK resolves credentials itself. |
|
|
283
|
+
| `model` | Claude model (default `claude-opus-5-5`); with `callModel`, the name used for pricing when the response has none |
|
|
284
|
+
| `effort` | Default `medium`. Claude: `output_config.effort`; with `callModel` it is passed on. `null` omits it. |
|
|
159
285
|
| `maxTokens` | Output cap per model response. Default `64000` (responses are streamed). |
|
|
160
286
|
| `maxTurns` | Maximum number of model round-trips. Default `30`. |
|
|
161
287
|
| `systemPrompt` | Extra instructions appended to the built-in system prompt |
|
|
162
|
-
| `fallbacks` |
|
|
288
|
+
| `fallbacks` | Claude only: server-side refusal fallbacks (`fallbacks: "default"`). On by default for supported models. Set to `false` on Bedrock/Vertex/Foundry or behind proxies. |
|
|
163
289
|
| `reportUsage` | Send a `usage` event (tokens and estimated cost) right before `done` / `error`. Default `true`; set `false` to keep usage on the server. |
|
|
164
290
|
| `pricing` | Prices in USD per million tokens, merged over the built-in Claude API list prices and keyed by model id: `{ 'my-model': { input, output, cacheRead?, cacheWrite?, cacheWrite1h? } }` |
|
|
165
|
-
| `progressUpdates` |
|
|
166
|
-
| `cors` | `"*"`, an origin, a list of origins, or `false`. The default is `false`, which allows same-origin requests only. |
|
|
291
|
+
| `progressUpdates` | Claude only: forward the model's progress notes as `status` events (`thinking.display: "updates"`). On by default for supported models. |
|
|
292
|
+
| `cors` | `"*"`, an origin, a list of origins, a predicate on the origin (e.g. `isLocalOrigin` from `@emabuild/shared`), or `false`. The default is `false`, which allows same-origin requests only. Requests must be `Content-Type: application/json` (`415` otherwise), so pages on other origins cannot reach the handler without passing CORS. |
|
|
167
293
|
| `authorize(req)` | Return `false` to reject the request with `401` |
|
|
168
294
|
| `transformRequest(aiRequest, req)` | Inject server-side context, such as tenant brand, products or a forced language |
|
|
169
295
|
| `maxBodyBytes` | Default 5 MB |
|
package/dist/cli.js
CHANGED
|
@@ -3,28 +3,62 @@
|
|
|
3
3
|
* Standalone development server for the Emabuild AI connector.
|
|
4
4
|
*
|
|
5
5
|
* ANTHROPIC_API_KEY=... npx emabuild-ai-server
|
|
6
|
+
* EMABUILD_AI_PROVIDER=openai OPENAI_API_KEY=... EMABUILD_AI_MODEL=gpt-4o npx emabuild-ai-server
|
|
6
7
|
*
|
|
7
8
|
* Env: PORT (default 8787), HOST (default 127.0.0.1; use 0.0.0.0 to expose it),
|
|
8
|
-
*
|
|
9
|
+
* EMABUILD_AI_PROVIDER ("anthropic", default, or "openai" for any OpenAI-compatible server),
|
|
10
|
+
* EMABUILD_AI_MODEL, EMABUILD_AI_BASE_URL (OpenAI-compatible base URL, e.g. Cerebras),
|
|
11
|
+
* ANTHROPIC_API_KEY / OPENAI_API_KEY (or EMABUILD_AI_API_KEY), EMABUILD_AI_EFFORT,
|
|
12
|
+
* EMABUILD_AI_CORS (default: pages served from this machine only — localhost, 127.0.0.1, [::1];
|
|
13
|
+
* "*" or a comma-separated list of origins to change it), EMABUILD_AI_TOKEN (when set, requests need
|
|
14
|
+
* `Authorization: Bearer <token>`, e.g. `ai.headers` in the editor options),
|
|
9
15
|
* EMABUILD_AI_IMAGES ("off" disables the free-image search tool).
|
|
16
|
+
* Keys are read from the environment only and never logged.
|
|
10
17
|
*/
|
|
11
18
|
import { createServer } from 'node:http';
|
|
19
|
+
import { timingSafeEqual } from 'node:crypto';
|
|
20
|
+
import { isLocalOrigin } from '@emabuild/shared';
|
|
12
21
|
import { createAIHandler } from './handler.js';
|
|
13
22
|
import { toNodeHandler } from '@emabuild/shared/node';
|
|
14
23
|
import { DEFAULT_MODEL } from './run.js';
|
|
24
|
+
import { openAICompatibleModel } from './providers/openai-format.js';
|
|
15
25
|
const port = Number(process.env.PORT ?? 8787);
|
|
16
26
|
const host = process.env.HOST ?? '127.0.0.1';
|
|
17
|
-
const
|
|
27
|
+
const provider = (process.env.EMABUILD_AI_PROVIDER || 'anthropic').toLowerCase();
|
|
28
|
+
const openai = provider === 'openai' || provider === 'openai-compatible';
|
|
29
|
+
const model = process.env.EMABUILD_AI_MODEL || (openai ? 'gpt-4o' : DEFAULT_MODEL);
|
|
18
30
|
const effort = (process.env.EMABUILD_AI_EFFORT || undefined);
|
|
19
|
-
const corsEnv = process.env.EMABUILD_AI_CORS
|
|
20
|
-
|
|
21
|
-
|
|
31
|
+
const corsEnv = process.env.EMABUILD_AI_CORS?.trim();
|
|
32
|
+
// Default: only pages on this machine may call the server (any site you visit could otherwise spend your API key)
|
|
33
|
+
const cors = !corsEnv ? isLocalOrigin
|
|
34
|
+
: corsEnv === 'false' ? false
|
|
35
|
+
: corsEnv.includes(',') ? corsEnv.split(',').map((s) => s.trim()) : corsEnv;
|
|
36
|
+
const token = process.env.EMABUILD_AI_TOKEN?.trim();
|
|
37
|
+
/** Constant-time `Authorization: Bearer <token>` check (only when EMABUILD_AI_TOKEN is set) */
|
|
38
|
+
function hasToken(req) {
|
|
39
|
+
const sent = Buffer.from(req.headers.get('authorization')?.replace(/^Bearer\s+/i, '') ?? '');
|
|
40
|
+
const expected = Buffer.from(token ?? '');
|
|
41
|
+
return sent.length === expected.length && timingSafeEqual(sent, expected);
|
|
42
|
+
}
|
|
43
|
+
if (openai) {
|
|
44
|
+
if (!process.env.EMABUILD_AI_API_KEY && !process.env.OPENAI_API_KEY)
|
|
45
|
+
console.warn('[emabuild-ai] No API key: set OPENAI_API_KEY or EMABUILD_AI_API_KEY.');
|
|
46
|
+
}
|
|
47
|
+
else if (!process.env.ANTHROPIC_API_KEY && !process.env.ANTHROPIC_AUTH_TOKEN) {
|
|
22
48
|
console.warn('[emabuild-ai] ANTHROPIC_API_KEY is not set; falling back to the SDK credential chain (e.g. `ant auth login`).');
|
|
23
49
|
}
|
|
24
50
|
const ai = toNodeHandler(createAIHandler({
|
|
25
51
|
model,
|
|
26
52
|
effort,
|
|
27
53
|
cors,
|
|
54
|
+
...(token ? { authorize: hasToken } : {}),
|
|
55
|
+
...(openai ? {
|
|
56
|
+
callModel: openAICompatibleModel({
|
|
57
|
+
model,
|
|
58
|
+
apiKey: process.env.EMABUILD_AI_API_KEY || process.env.OPENAI_API_KEY,
|
|
59
|
+
...(process.env.EMABUILD_AI_BASE_URL ? { baseURL: process.env.EMABUILD_AI_BASE_URL } : {}),
|
|
60
|
+
}),
|
|
61
|
+
} : {}),
|
|
28
62
|
...(process.env.EMABUILD_AI_IMAGES === 'off' ? { imageSearch: false } : {}),
|
|
29
63
|
onError: (err) => console.error('[emabuild-ai]', err),
|
|
30
64
|
}));
|
|
@@ -36,14 +70,18 @@ const server = createServer((req, res) => {
|
|
|
36
70
|
}
|
|
37
71
|
if (path === '/health' && req.method === 'GET') {
|
|
38
72
|
res.writeHead(200, { 'content-type': 'application/json' });
|
|
39
|
-
res.end(JSON.stringify({ ok: true, model }));
|
|
73
|
+
res.end(JSON.stringify({ ok: true, provider: openai ? 'openai' : 'anthropic', model }));
|
|
40
74
|
return;
|
|
41
75
|
}
|
|
42
76
|
res.writeHead(404, { 'content-type': 'application/json' });
|
|
43
77
|
res.end(JSON.stringify({ error: 'Not found' }));
|
|
44
78
|
});
|
|
79
|
+
if (!token && !['127.0.0.1', 'localhost', '::1'].includes(host)) {
|
|
80
|
+
console.warn(`[emabuild-ai] Listening on ${host} without EMABUILD_AI_TOKEN: anyone who can reach this port can use your API key.`);
|
|
81
|
+
}
|
|
45
82
|
server.listen(port, host, () => {
|
|
46
|
-
|
|
83
|
+
// eslint-disable-next-line no-console -- the CLI's startup line
|
|
84
|
+
console.log(`[emabuild-ai] listening on http://${host}:${port}/ai (${openai ? 'openai-compatible' : 'anthropic'}, model: ${model})`);
|
|
47
85
|
});
|
|
48
86
|
const shutdown = () => server.close(() => process.exit(0));
|
|
49
87
|
process.on('SIGINT', shutdown);
|
package/dist/cli.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cli.js","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":";AACA
|
|
1
|
+
{"version":3,"file":"cli.js","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":";AACA;;;;;;;;;;;;;;;GAeG;AACH,OAAO,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;AACzC,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAC9C,OAAO,EAAE,aAAa,EAAmB,MAAM,kBAAkB,CAAC;AAClE,OAAO,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAC/C,OAAO,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AACtD,OAAO,EAAE,aAAa,EAAiB,MAAM,UAAU,CAAC;AACxD,OAAO,EAAE,qBAAqB,EAAE,MAAM,8BAA8B,CAAC;AAErE,MAAM,IAAI,GAAG,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,IAAI,IAAI,IAAI,CAAC,CAAC;AAC9C,MAAM,IAAI,GAAG,OAAO,CAAC,GAAG,CAAC,IAAI,IAAI,WAAW,CAAC;AAC7C,MAAM,QAAQ,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,oBAAoB,IAAI,WAAW,CAAC,CAAC,WAAW,EAAE,CAAC;AACjF,MAAM,MAAM,GAAG,QAAQ,KAAK,QAAQ,IAAI,QAAQ,KAAK,mBAAmB,CAAC;AACzE,MAAM,KAAK,GAAG,OAAO,CAAC,GAAG,CAAC,iBAAiB,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,aAAa,CAAC,CAAC;AACnF,MAAM,MAAM,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,kBAAkB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AAC,EAAE,CAAC,SAAS,EAAE,QAAQ,CAAC,CAAC"}
|
package/dist/handler.d.ts
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
import type { AIRequest, AIStreamEvent } from '@emabuild/types';
|
|
2
2
|
import { type RunAIOptions } from './run.js';
|
|
3
|
+
import { type CorsOption } from '@emabuild/shared';
|
|
3
4
|
/** NDJSON content type used by the streaming protocol */
|
|
4
5
|
export declare const NDJSON_CONTENT_TYPE = "application/x-ndjson; charset=utf-8";
|
|
5
6
|
/** Options for {@link createAIHandler} */
|
|
6
7
|
export interface AIHandlerOptions extends Omit<RunAIOptions, 'signal'> {
|
|
7
8
|
/**
|
|
8
9
|
* Allowed CORS origins: "*" for any, a single origin, a list of origins, or
|
|
9
|
-
* false (default) to send no CORS headers (same-origin only).
|
|
10
|
+
* a predicate on the request origin (e.g. `isLocalOrigin`), or false (default) to send no CORS headers (same-origin only).
|
|
10
11
|
*/
|
|
11
|
-
cors?:
|
|
12
|
+
cors?: CorsOption;
|
|
12
13
|
/** Return false to reject the request with 401 (e.g. check a session or bearer token) */
|
|
13
14
|
authorize?: (req: Request) => boolean | Promise<boolean>;
|
|
14
15
|
/** Adjust the request before running it, e.g. inject tenant brand data server-side */
|
package/dist/handler.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"handler.d.ts","sourceRoot":"","sources":["../src/handler.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAChE,OAAO,EAAS,KAAK,YAAY,EAAE,MAAM,UAAU,CAAC;
|
|
1
|
+
{"version":3,"file":"handler.d.ts","sourceRoot":"","sources":["../src/handler.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAChE,OAAO,EAAS,KAAK,YAAY,EAAE,MAAM,UAAU,CAAC;AACpD,OAAO,EAA8B,KAAK,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAE/E,yDAAyD;AACzD,eAAO,MAAM,mBAAmB,wCAAwC,CAAC;AAEzE,0CAA0C;AAC1C,MAAM,WAAW,gBAAiB,SAAQ,IAAI,CAAC,YAAY,EAAE,QAAQ,CAAC;IACpE;;;OAGG;IACH,IAAI,CAAC,EAAE,UAAU,CAAC;IAClB,yFAAyF;IACzF,SAAS,CAAC,EAAE,CAAC,GAAG,EAAE,OAAO,KAAK,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;IACzD,sFAAsF;IACtF,gBAAgB,CAAC,EAAE,CAAC,OAAO,EAAE,SAAS,EAAE,GAAG,EAAE,OAAO,KAAK,SAAS,GAAG,OAAO,CAAC,SAAS,CAAC,CAAC;IACxF,yDAAyD;IACzD,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,2FAA2F;IAC3F,GAAG,CAAC,EAAE,CAAC,OAAO,EAAE,SAAS,EAAE,OAAO,EAAE,YAAY,KAAK,aAAa,CAAC,aAAa,CAAC,CAAC;IAClF,mDAAmD;IACnD,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,OAAO,KAAK,IAAI,CAAC;CACpC;AAYD;;;GAGG;AACH,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,OAAO,GAAG,MAAM,GAAG,IAAI,CAsB9D;AAED;;;;;GAKG;AACH,wBAAgB,eAAe,CAAC,OAAO,GAAE,gBAAqB,GAAG,CAAC,GAAG,EAAE,OAAO,KAAK,OAAO,CAAC,QAAQ,CAAC,CAkGnG"}
|
package/dist/handler.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { runAI } from './run.js';
|
|
2
|
-
import { corsHeaders } from '@emabuild/shared';
|
|
2
|
+
import { corsHeaders, isJsonRequest } from '@emabuild/shared';
|
|
3
3
|
/** NDJSON content type used by the streaming protocol */
|
|
4
4
|
export const NDJSON_CONTENT_TYPE = 'application/x-ndjson; charset=utf-8';
|
|
5
5
|
const DEFAULT_MAX_BODY = 5 * 1024 * 1024;
|
|
@@ -66,6 +66,9 @@ export function createAIHandler(options = {}) {
|
|
|
66
66
|
onError?.(err);
|
|
67
67
|
return jsonResponse(401, { error: 'Unauthorized' }, ch);
|
|
68
68
|
}
|
|
69
|
+
// JSON only: cross-origin pages cannot send it without passing CORS (see isJsonRequest)
|
|
70
|
+
if (!isJsonRequest(req))
|
|
71
|
+
return jsonResponse(415, { error: 'Content-Type must be application/json' }, ch);
|
|
69
72
|
const declared = Number(req.headers.get('content-length') ?? '0');
|
|
70
73
|
if (declared > maxBodyBytes)
|
|
71
74
|
return jsonResponse(413, { error: 'Request body too large' }, ch);
|
package/dist/handler.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"handler.js","sourceRoot":"","sources":["../src/handler.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,EAAqB,MAAM,UAAU,CAAC;AACpD,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAE/
|
|
1
|
+
{"version":3,"file":"handler.js","sourceRoot":"","sources":["../src/handler.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,EAAqB,MAAM,UAAU,CAAC;AACpD,OAAO,EAAE,WAAW,EAAE,aAAa,EAAmB,MAAM,kBAAkB,CAAC;AAE/E,yDAAyD;AACzD,MAAM,CAAC,MAAM,mBAAmB,GAAG,qCAAqC,CAAC;AAqBzE,MAAM,gBAAgB,GAAG,CAAC,GAAG,IAAI,GAAG,IAAI,CAAC;AAGzC,SAAS,YAAY,CAAC,MAAc,EAAE,IAAa,EAAE,OAA+B;IAClF,OAAO,IAAI,QAAQ,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,EAAE;QACxC,MAAM;QACN,OAAO,EAAE,EAAE,GAAG,OAAO,EAAE,cAAc,EAAE,iCAAiC,EAAE;KAC3E,CAAC,CAAC;AACL,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,iBAAiB,CAAC,IA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AAmB,EAAE,IAAI;aAC1B;SACF,CAAC,CAAC;IACL,CAAC,CAAC;AACJ,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,11 +1,18 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @emabuild/ai — server side of the Emabuild editor's AI connector.
|
|
3
3
|
*
|
|
4
|
-
* Turns an `AIRequest` into a stream of `AIStreamEvent`s
|
|
5
|
-
*
|
|
4
|
+
* Turns an `AIRequest` into a stream of `AIStreamEvent`s: the library runs the
|
|
5
|
+
* flow, the model is Claude (default) or anything you plug in with `callModel`.
|
|
6
|
+
* Exposed over HTTP as NDJSON.
|
|
6
7
|
*/
|
|
7
8
|
export { runAI, buildRequestParams, DEFAULT_MODEL, DEFAULT_MAX_TOKENS, DEFAULT_MAX_TURNS, type RunAIOptions, type RequestParams, type AIEffort, } from './run.js';
|
|
8
9
|
export { MODEL_PRICING, pricingFor, UsageMeter, type ModelPricing, type MessageUsage } from './usage.js';
|
|
10
|
+
export { runLoop, MAX_STREAM_RETRIES, type LoopOptions } from './loop.js';
|
|
11
|
+
export { createAnthropicAdapter, systemParts, toolSpecs, type AnthropicOptions } from './providers/anthropic.js';
|
|
12
|
+
export { adapterFromModelCall } from './providers/model-call.js';
|
|
13
|
+
export { toOpenAIChat, fromOpenAIChat, fromOpenAIChatStream, openAICompatibleModel, OpenAICompatibleError, type OpenAIChatParams, type OpenAIChatCompletion, type OpenAIChatChunk, type OpenAICompatibleOptions, } from './providers/openai-format.js';
|
|
14
|
+
export { providerErrorMessage, describeAnyError, type ProviderErrorKind } from './providers/errors.js';
|
|
15
|
+
export { ProviderSetupError, type ModelCall, type ModelRequest, type ModelResponse, type ModelStreamEvent, type ConversationMessage, type ToolCall, type ToolSpec, type ModelAdapter, type AdapterEvent, type NormalizedUsage, type StopReason, type SystemPart, type ToolResult, type TurnSettings, } from './providers/types.js';
|
|
9
16
|
export { createAIHandler, validateAIRequest, NDJSON_CONTENT_TYPE, type AIHandlerOptions } from './handler.js';
|
|
10
17
|
export { toNodeHandler, type FetchHandler, type NodeHandler } from '@emabuild/shared/node';
|
|
11
18
|
export { BASE_SYSTEM_PROMPT, buildSchemaPrompt, buildUserPrompt, flattenMergeTags, compactSourceHtml, MAX_SOURCE_HTML } from './prompt.js';
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,OAAO,EACL,KAAK,EACL,kBAAkB,EAClB,aAAa,EACb,kBAAkB,EAClB,iBAAiB,EACjB,KAAK,YAAY,EACjB,KAAK,aAAa,EAClB,KAAK,QAAQ,GACd,MAAM,UAAU,CAAC;AAClB,OAAO,EAAE,aAAa,EAAE,UAAU,EAAE,UAAU,EAAE,KAAK,YAAY,EAAE,KAAK,YAAY,EAAE,MAAM,YAAY,CAAC;AACzG,OAAO,EAAE,OAAO,EAAE,kBAAkB,EAAE,KAAK,WAAW,EAAE,MAAM,WAAW,CAAC;AAC1E,OAAO,EAAE,sBAAsB,EAAE,WAAW,EAAE,SAAS,EAAE,KAAK,gBAAgB,EAAE,MAAM,0BAA0B,CAAC;AACjH,OAAO,EAAE,oBAAoB,EAAE,MAAM,2BAA2B,CAAC;AACjE,OAAO,EACL,YAAY,EAAE,cAAc,EAAE,oBAAoB,EAAE,qBAAqB,EAAE,qBAAqB,EAChG,KAAK,gBAAgB,EAAE,KAAK,oBAAoB,EAAE,KAAK,eAAe,EAAE,KAAK,uBAAuB,GACrG,MAAM,8BAA8B,CAAC;AACtC,OAAO,EAAE,oBAAoB,EAAE,gBAAgB,EAAE,KAAK,iBAAiB,EAAE,MAAM,uBAAuB,CAAC;AACvG,OAAO,EACL,kBAAkB,EAClB,KAAK,SAAS,EAAE,KAAK,YAAY,EAAE,KAAK,aAAa,EAAE,KAAK,gBAAgB,EAAE,KAAK,mBAAmB,EAAE,KAAK,QAAQ,EAAE,KAAK,QAAQ,EACpI,KAAK,YAAY,EAAE,KAAK,YAAY,EAAE,KAAK,eAAe,EAAE,KAAK,UAAU,EAAE,KAAK,UAAU,EAAE,KAAK,UAAU,EAAE,KAAK,YAAY,GACjI,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAAE,eAAe,EAAE,iBAAiB,EAAE,mBAAmB,EAAE,KAAK,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAC9G,OAAO,EAAE,aAAa,EAAE,KAAK,YAAY,EAAE,KAAK,WAAW,EAAE,MAAM,uBAAuB,CAAC;AAC3F,OAAO,EAAE,kBAAkB,EAAE,iBAAiB,EAAE,eAAe,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAC3I,OAAO,EAAE,aAAa,EAAE,cAAc,EAAE,cAAc,EAAE,WAAW,EAAE,aAAa,EAAE,KAAK,aAAa,EAAE,MAAM,cAAc,CAAC;AAC7H,OAAO,EAAE,kBAAkB,EAAE,WAAW,EAAE,KAAK,gBAAgB,EAAE,MAAM,eAAe,CAAC;AACvF,OAAO,EAAE,QAAQ,EAAE,gBAAgB,EAAE,qBAAqB,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAC5F,YAAY,EAAE,SAAS,EAAE,aAAa,EAAE,OAAO,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,11 +1,18 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @emabuild/ai — server side of the Emabuild editor's AI connector.
|
|
3
3
|
*
|
|
4
|
-
* Turns an `AIRequest` into a stream of `AIStreamEvent`s
|
|
5
|
-
*
|
|
4
|
+
* Turns an `AIRequest` into a stream of `AIStreamEvent`s: the library runs the
|
|
5
|
+
* flow, the model is Claude (default) or anything you plug in with `callModel`.
|
|
6
|
+
* Exposed over HTTP as NDJSON.
|
|
6
7
|
*/
|
|
7
8
|
export { runAI, buildRequestParams, DEFAULT_MODEL, DEFAULT_MAX_TOKENS, DEFAULT_MAX_TURNS, } from './run.js';
|
|
8
9
|
export { MODEL_PRICING, pricingFor, UsageMeter } from './usage.js';
|
|
10
|
+
export { runLoop, MAX_STREAM_RETRIES } from './loop.js';
|
|
11
|
+
export { createAnthropicAdapter, systemParts, toolSpecs } from './providers/anthropic.js';
|
|
12
|
+
export { adapterFromModelCall } from './providers/model-call.js';
|
|
13
|
+
export { toOpenAIChat, fromOpenAIChat, fromOpenAIChatStream, openAICompatibleModel, OpenAICompatibleError, } from './providers/openai-format.js';
|
|
14
|
+
export { providerErrorMessage, describeAnyError } from './providers/errors.js';
|
|
15
|
+
export { ProviderSetupError, } from './providers/types.js';
|
|
9
16
|
export { createAIHandler, validateAIRequest, NDJSON_CONTENT_TYPE } from './handler.js';
|
|
10
17
|
export { toNodeHandler } from '@emabuild/shared/node';
|
|
11
18
|
export { BASE_SYSTEM_PROMPT, buildSchemaPrompt, buildUserPrompt, flattenMergeTags, compactSourceHtml, MAX_SOURCE_HTML } from './prompt.js';
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,OAAO,EACL,KAAK,EACL,kBAAkB,EAClB,aAAa,EACb,kBAAkB,EAClB,iBAAiB,GAIlB,MAAM,UAAU,CAAC;AAClB,OAAO,EAAE,aAAa,EAAE,UAAU,EAAE,UAAU,EAAwC,MAAM,YAAY,CAAC;AACzG,OAAO,EAAE,OAAO,EAAE,kBAAkB,EAAoB,MAAM,WAAW,CAAC;AAC1E,OAAO,EAAE,sBAAsB,EAAE,WAAW,EAAE,SAAS,EAAyB,MAAM,0BAA0B,CAAC;AACjH,OAAO,EAAE,oBAAoB,EAAE,MAAM,2BAA2B,CAAC;AACjE,OAAO,EACL,YAAY,EAAE,cAAc,EAAE,oBAAoB,EAAE,qBAAqB,EAAE,qBAAqB,GAEjG,MAAM,8BAA8B,CAAC;AACtC,OAAO,EAAE,oBAAoB,EAAE,gBAAgB,EAA0B,MAAM,uBAAuB,CAAC;AACvG,OAAO,EACL,kBAAkB,GAGnB,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAAE,eAAe,EAAE,iBAAiB,EAAE,mBAAmB,EAAyB,MAAM,cAAc,CAAC;AAC9G,OAAO,EAAE,aAAa,EAAuC,MAAM,uBAAuB,CAAC;AAC3F,OAAO,EAAE,kBAAkB,EAAE,iBAAiB,EAAE,eAAe,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAC3I,OAAO,EAAE,aAAa,EAAE,cAAc,EAAE,cAAc,EAAE,WAAW,EAAE,aAAa,EAAsB,MAAM,cAAc,CAAC;AAC7H,OAAO,EAAE,kBAAkB,EAAE,WAAW,EAAyB,MAAM,eAAe,CAAC;AACvF,OAAO,EAAE,QAAQ,EAAE,gBAAgB,EAAE,qBAAqB,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC"}
|
package/dist/loop.d.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module loop
|
|
3
|
+
*
|
|
4
|
+
* The provider-agnostic agent loop: it owns the flow (prompts, tools,
|
|
5
|
+
* validation, retries, turn limit, editor events) and asks a
|
|
6
|
+
* {@link ModelAdapter} for each model turn. The `AIStreamEvent`s it yields are
|
|
7
|
+
* the editor's NDJSON protocol, identical whatever model answers.
|
|
8
|
+
*/
|
|
9
|
+
import type { AIRequest, AIStreamEvent, FreeImage, FreeImageSearchOptions } from '@emabuild/types';
|
|
10
|
+
import type { ModelAdapter, TurnSettings } from './providers/types.js';
|
|
11
|
+
import type { UsageMeter } from './usage.js';
|
|
12
|
+
/** Retries of a turn whose stream failed for a non-API reason (only if it changed nothing yet) */
|
|
13
|
+
export declare const MAX_STREAM_RETRIES = 2;
|
|
14
|
+
export interface LoopOptions {
|
|
15
|
+
/** Max model round-trips */
|
|
16
|
+
maxTurns: number;
|
|
17
|
+
settings: TurnSettings;
|
|
18
|
+
/** Free-image search behind `search_images` (false: the tool is not offered) */
|
|
19
|
+
imageSearch?: ((query: string, options: FreeImageSearchOptions) => Promise<FreeImage[]>) | false;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Run the agent loop with `adapter`. Never throws: problems end the run with
|
|
23
|
+
* an `error` event. Usage of every completed turn is recorded in `meter`.
|
|
24
|
+
*/
|
|
25
|
+
export declare function runLoop(adapter: ModelAdapter, request: AIRequest, options: LoopOptions, meter: UsageMeter): AsyncGenerator<AIStreamEvent>;
|
|
26
|
+
//# sourceMappingURL=loop.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"loop.d.ts","sourceRoot":"","sources":["../src/loop.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAE,aAAa,EAAmB,SAAS,EAAE,sBAAsB,EAAE,MAAM,iBAAiB,CAAC;AAMpH,OAAO,KAAK,EAAE,YAAY,EAAoC,YAAY,EAAE,MAAM,sBAAsB,CAAC;AACzG,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAE7C,kGAAkG;AAClG,eAAO,MAAM,kBAAkB,IAAI,CAAC;AAEpC,MAAM,WAAW,WAAW;IAC1B,4BAA4B;IAC5B,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,EAAE,YAAY,CAAC;IACvB,gFAAgF;IAChF,WAAW,CAAC,EAAE,CAAC,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,sBAAsB,KAAK,OAAO,CAAC,SAAS,EAAE,CAAC,CAAC,GAAG,KAAK,CAAC;CAClG;AA+ED;;;GAGG;AACH,wBAAuB,OAAO,CAC5B,OAAO,EAAE,YAAY,EACrB,OAAO,EAAE,SAAS,EAClB,OAAO,EAAE,WAAW,EACpB,KAAK,EAAE,UAAU,GAChB,cAAc,CAAC,aAAa,CAAC,CA+H/B"}
|