agentic-server 0.30.1 → 0.30.3
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/esm/server.js +3 -1
- package/esm/transforms.js +25 -3
- package/package.json +2 -2
- package/server.d.ts +2 -0
- package/server.js +4 -2
- package/transforms.js +25 -3
- package/types.d.ts +5 -0
package/esm/server.js
CHANGED
|
@@ -13,13 +13,15 @@ const IDENTITY_HEADERS = [
|
|
|
13
13
|
'x-attempt',
|
|
14
14
|
'x-run-id'
|
|
15
15
|
];
|
|
16
|
+
/** An image arrives inside the JSON body as base64, a third larger than its bytes. */
|
|
17
|
+
export const DEFAULT_BODY_LIMIT = '32mb';
|
|
16
18
|
/**
|
|
17
19
|
* Create a standalone Express app for the agentic server.
|
|
18
20
|
* Used by both standalone mode and integration tests.
|
|
19
21
|
*/
|
|
20
22
|
export const createAgenticServer = (options) => {
|
|
21
23
|
const app = express();
|
|
22
|
-
app.use(express.json());
|
|
24
|
+
app.use(express.json({ limit: options.bodyLimit ?? DEFAULT_BODY_LIMIT }));
|
|
23
25
|
// When isPublic === true, strip identity headers from all incoming requests.
|
|
24
26
|
// Public deployments cannot trust client-supplied tenant context.
|
|
25
27
|
// When isPublic === false (default), the server is on a private network
|
package/esm/transforms.js
CHANGED
|
@@ -145,12 +145,29 @@ export function transformChatRequest(provider, body) {
|
|
|
145
145
|
// OpenAI-compatible (default)
|
|
146
146
|
return { ...body, model: model || 'gpt-4o' };
|
|
147
147
|
}
|
|
148
|
+
// Anchored, so text that merely mentions a data url stays text. A media type
|
|
149
|
+
// may carry parameters before the encoding (`image/svg+xml;charset=utf-8;base64,`)
|
|
150
|
+
// and both are case-insensitive (RFC 2397).
|
|
151
|
+
const IMAGE_DATA_URL = /^data:image\/[^;,]+(?:;[^;,]+)*;base64,/i;
|
|
152
|
+
/**
|
|
153
|
+
* One embed input in Ollama's shape. An image arrives as a base64 `data:` url,
|
|
154
|
+
* the way OpenAI-style clients carry bytes; Ollama's multimodal embed takes it
|
|
155
|
+
* as `{ image: <base64> }`, and embeds the same url left as a string as text —
|
|
156
|
+
* a vector of the right length for the wrong thing, with no error.
|
|
157
|
+
*/
|
|
158
|
+
function ollamaEmbedInput(input) {
|
|
159
|
+
if (typeof input !== 'string')
|
|
160
|
+
return input;
|
|
161
|
+
const prefix = IMAGE_DATA_URL.exec(input)?.[0];
|
|
162
|
+
return prefix ? { image: input.slice(prefix.length) } : input;
|
|
163
|
+
}
|
|
148
164
|
export function transformEmbedRequest(provider, body) {
|
|
149
165
|
const model = upstreamModel(provider, body.model);
|
|
150
166
|
if (provider.type === 'ollama') {
|
|
151
167
|
return {
|
|
152
168
|
model: model || 'nomic-embed-text',
|
|
153
|
-
input: body.input
|
|
169
|
+
input: Array.isArray(body.input) ? body.input.map(ollamaEmbedInput) : ollamaEmbedInput(body.input),
|
|
170
|
+
...(body.dimensions !== undefined && { dimensions: body.dimensions })
|
|
154
171
|
};
|
|
155
172
|
}
|
|
156
173
|
return { ...body, model: model || 'text-embedding-3-small' };
|
|
@@ -243,8 +260,13 @@ function finishReason(doneReason) {
|
|
|
243
260
|
}
|
|
244
261
|
export function transformEmbedResponse(data, provider) {
|
|
245
262
|
if (provider.type === 'ollama') {
|
|
246
|
-
const raw = (data.embeddings
|
|
247
|
-
const embeddings = Array.isArray(raw[0]) ? raw : [raw];
|
|
263
|
+
const raw = (data.embeddings ?? data.embedding);
|
|
264
|
+
const embeddings = (Array.isArray(raw) && Array.isArray(raw[0]) ? raw : [raw]);
|
|
265
|
+
// An answer with no vector in it is not an embedding of nothing: passing it
|
|
266
|
+
// on would hand the caller one empty vector to store.
|
|
267
|
+
if (!Array.isArray(raw) || raw.length === 0 || embeddings.some((e) => !Array.isArray(e) || e.length === 0)) {
|
|
268
|
+
throw new Error(`ollama: the embed response carries no embedding${typeof data.error === 'string' ? ` (${data.error})` : ''}`);
|
|
269
|
+
}
|
|
248
270
|
// Ollama reports the prompt tokens of an embed as `prompt_eval_count`.
|
|
249
271
|
const promptTokens = data.prompt_eval_count || 0;
|
|
250
272
|
const usage = { prompt_tokens: promptTokens, completion_tokens: 0, total_tokens: promptTokens };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "agentic-server",
|
|
3
|
-
"version": "0.30.
|
|
3
|
+
"version": "0.30.3",
|
|
4
4
|
"description": "Standalone OpenAI-compatible LLM gateway — multi-provider proxy with pluggable inference metering",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"module": "esm/index.js",
|
|
@@ -51,5 +51,5 @@
|
|
|
51
51
|
"metering",
|
|
52
52
|
"constructive"
|
|
53
53
|
],
|
|
54
|
-
"gitHead": "
|
|
54
|
+
"gitHead": "ec9e10035b145bbdedb8b61e83252dff41a5baf0"
|
|
55
55
|
}
|
package/server.d.ts
CHANGED
|
@@ -3,6 +3,8 @@ import type { AgenticServerOptions } from './types';
|
|
|
3
3
|
export interface AgenticServerStartOptions extends AgenticServerOptions {
|
|
4
4
|
port?: number;
|
|
5
5
|
}
|
|
6
|
+
/** An image arrives inside the JSON body as base64, a third larger than its bytes. */
|
|
7
|
+
export declare const DEFAULT_BODY_LIMIT = "32mb";
|
|
6
8
|
/**
|
|
7
9
|
* Create a standalone Express app for the agentic server.
|
|
8
10
|
* Used by both standalone mode and integration tests.
|
package/server.js
CHANGED
|
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.createAgenticServer = void 0;
|
|
6
|
+
exports.createAgenticServer = exports.DEFAULT_BODY_LIMIT = void 0;
|
|
7
7
|
const express_1 = __importDefault(require("express"));
|
|
8
8
|
const router_1 = require("./router");
|
|
9
9
|
/** Identity + task-correlation headers trusted only when isPublic === false
|
|
@@ -19,13 +19,15 @@ const IDENTITY_HEADERS = [
|
|
|
19
19
|
'x-attempt',
|
|
20
20
|
'x-run-id'
|
|
21
21
|
];
|
|
22
|
+
/** An image arrives inside the JSON body as base64, a third larger than its bytes. */
|
|
23
|
+
exports.DEFAULT_BODY_LIMIT = '32mb';
|
|
22
24
|
/**
|
|
23
25
|
* Create a standalone Express app for the agentic server.
|
|
24
26
|
* Used by both standalone mode and integration tests.
|
|
25
27
|
*/
|
|
26
28
|
const createAgenticServer = (options) => {
|
|
27
29
|
const app = (0, express_1.default)();
|
|
28
|
-
app.use(express_1.default.json());
|
|
30
|
+
app.use(express_1.default.json({ limit: options.bodyLimit ?? exports.DEFAULT_BODY_LIMIT }));
|
|
29
31
|
// When isPublic === true, strip identity headers from all incoming requests.
|
|
30
32
|
// Public deployments cannot trust client-supplied tenant context.
|
|
31
33
|
// When isPublic === false (default), the server is on a private network
|
package/transforms.js
CHANGED
|
@@ -154,12 +154,29 @@ function transformChatRequest(provider, body) {
|
|
|
154
154
|
// OpenAI-compatible (default)
|
|
155
155
|
return { ...body, model: model || 'gpt-4o' };
|
|
156
156
|
}
|
|
157
|
+
// Anchored, so text that merely mentions a data url stays text. A media type
|
|
158
|
+
// may carry parameters before the encoding (`image/svg+xml;charset=utf-8;base64,`)
|
|
159
|
+
// and both are case-insensitive (RFC 2397).
|
|
160
|
+
const IMAGE_DATA_URL = /^data:image\/[^;,]+(?:;[^;,]+)*;base64,/i;
|
|
161
|
+
/**
|
|
162
|
+
* One embed input in Ollama's shape. An image arrives as a base64 `data:` url,
|
|
163
|
+
* the way OpenAI-style clients carry bytes; Ollama's multimodal embed takes it
|
|
164
|
+
* as `{ image: <base64> }`, and embeds the same url left as a string as text —
|
|
165
|
+
* a vector of the right length for the wrong thing, with no error.
|
|
166
|
+
*/
|
|
167
|
+
function ollamaEmbedInput(input) {
|
|
168
|
+
if (typeof input !== 'string')
|
|
169
|
+
return input;
|
|
170
|
+
const prefix = IMAGE_DATA_URL.exec(input)?.[0];
|
|
171
|
+
return prefix ? { image: input.slice(prefix.length) } : input;
|
|
172
|
+
}
|
|
157
173
|
function transformEmbedRequest(provider, body) {
|
|
158
174
|
const model = upstreamModel(provider, body.model);
|
|
159
175
|
if (provider.type === 'ollama') {
|
|
160
176
|
return {
|
|
161
177
|
model: model || 'nomic-embed-text',
|
|
162
|
-
input: body.input
|
|
178
|
+
input: Array.isArray(body.input) ? body.input.map(ollamaEmbedInput) : ollamaEmbedInput(body.input),
|
|
179
|
+
...(body.dimensions !== undefined && { dimensions: body.dimensions })
|
|
163
180
|
};
|
|
164
181
|
}
|
|
165
182
|
return { ...body, model: model || 'text-embedding-3-small' };
|
|
@@ -252,8 +269,13 @@ function finishReason(doneReason) {
|
|
|
252
269
|
}
|
|
253
270
|
function transformEmbedResponse(data, provider) {
|
|
254
271
|
if (provider.type === 'ollama') {
|
|
255
|
-
const raw = (data.embeddings
|
|
256
|
-
const embeddings = Array.isArray(raw[0]) ? raw : [raw];
|
|
272
|
+
const raw = (data.embeddings ?? data.embedding);
|
|
273
|
+
const embeddings = (Array.isArray(raw) && Array.isArray(raw[0]) ? raw : [raw]);
|
|
274
|
+
// An answer with no vector in it is not an embedding of nothing: passing it
|
|
275
|
+
// on would hand the caller one empty vector to store.
|
|
276
|
+
if (!Array.isArray(raw) || raw.length === 0 || embeddings.some((e) => !Array.isArray(e) || e.length === 0)) {
|
|
277
|
+
throw new Error(`ollama: the embed response carries no embedding${typeof data.error === 'string' ? ` (${data.error})` : ''}`);
|
|
278
|
+
}
|
|
257
279
|
// Ollama reports the prompt tokens of an embed as `prompt_eval_count`.
|
|
258
280
|
const promptTokens = data.prompt_eval_count || 0;
|
|
259
281
|
const usage = { prompt_tokens: promptTokens, completion_tokens: 0, total_tokens: promptTokens };
|
package/types.d.ts
CHANGED
|
@@ -33,6 +33,11 @@ export interface AgenticServerOptions {
|
|
|
33
33
|
* When true, identity headers are stripped from incoming requests
|
|
34
34
|
* (external clients cannot set tenant context). */
|
|
35
35
|
isPublic?: boolean;
|
|
36
|
+
/** Largest JSON request body accepted, as express.json's `limit` (bytes, or
|
|
37
|
+
* a string such as '32mb'). A request carries an image as base64 — an embed
|
|
38
|
+
* input, a chat image part — so express's 100kb default refuses any real
|
|
39
|
+
* one with a 413. Default: '32mb'. */
|
|
40
|
+
bodyLimit?: number | string;
|
|
36
41
|
}
|
|
37
42
|
export interface ResolvedProvider {
|
|
38
43
|
name?: string;
|