metergraph 0.4.0 → 0.6.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/README.md +83 -16
- package/dist/batch-first.d.ts +112 -0
- package/dist/batch-first.js +213 -0
- package/dist/capture.js +29 -27
- package/dist/config.js +9 -4
- package/dist/context.js +18 -10
- package/dist/failure-log.js +5 -1
- package/dist/index.d.ts +18 -5
- package/dist/index.js +86 -38
- package/dist/index.mjs +43 -0
- package/dist/package.json +1 -0
- package/dist/provider-batch.d.ts +160 -0
- package/dist/provider-batch.js +247 -0
- package/dist/repo-config.d.ts +1 -1
- package/dist/repo-config.js +43 -36
- package/dist/session.js +16 -12
- package/dist/template.js +6 -2
- package/dist/track.js +6 -3
- package/dist/transport.js +9 -5
- package/dist/vercel-ai.js +9 -6
- package/dist/version.d.ts +1 -1
- package/dist/version.js +4 -1
- package/dist/wrap.js +24 -18
- package/package.json +17 -8
package/dist/context.js
CHANGED
|
@@ -1,15 +1,23 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.contextSnapshot = contextSnapshot;
|
|
4
|
+
exports.runWithContext = runWithContext;
|
|
5
|
+
exports.route = route;
|
|
6
|
+
exports.trace = trace;
|
|
7
|
+
exports.setSession = setSession;
|
|
8
|
+
exports.setTags = setTags;
|
|
9
|
+
const node_async_hooks_1 = require("node:async_hooks");
|
|
10
|
+
const node_crypto_1 = require("node:crypto");
|
|
11
|
+
const storage = new node_async_hooks_1.AsyncLocalStorage();
|
|
4
12
|
let ambient = { tags: {} };
|
|
5
|
-
|
|
13
|
+
function contextSnapshot() {
|
|
6
14
|
const value = storage.getStore() ?? ambient;
|
|
7
15
|
return { ...value, tags: { ...value.tags } };
|
|
8
16
|
}
|
|
9
|
-
|
|
17
|
+
function runWithContext(context, fn) {
|
|
10
18
|
return storage.run(context, fn);
|
|
11
19
|
}
|
|
12
|
-
|
|
20
|
+
async function route(name, fn, options = {}) {
|
|
13
21
|
const parent = contextSnapshot();
|
|
14
22
|
const child = {
|
|
15
23
|
...parent,
|
|
@@ -24,28 +32,28 @@ export async function route(name, fn, options = {}) {
|
|
|
24
32
|
};
|
|
25
33
|
return storage.run(child, fn);
|
|
26
34
|
}
|
|
27
|
-
|
|
35
|
+
function trace(name, fn, options = {}) {
|
|
28
36
|
const parent = contextSnapshot();
|
|
29
37
|
const requested = options.traceId?.trim();
|
|
30
38
|
const reuse = parent.traceId !== undefined
|
|
31
39
|
&& (requested === undefined || requested === parent.traceId);
|
|
32
40
|
const child = {
|
|
33
41
|
...parent,
|
|
34
|
-
traceId: reuse ? parent.traceId : requested || randomBytes(16).toString("hex"),
|
|
42
|
+
traceId: reuse ? parent.traceId : requested || (0, node_crypto_1.randomBytes)(16).toString("hex"),
|
|
35
43
|
traceName: reuse ? parent.traceName : String(name),
|
|
36
44
|
parentSpanId: options.parentSpanId ?? (reuse ? parent.parentSpanId : undefined),
|
|
37
45
|
captureText: options.captureText ?? parent.captureText,
|
|
38
46
|
};
|
|
39
47
|
return storage.run(child, fn);
|
|
40
48
|
}
|
|
41
|
-
|
|
49
|
+
function setSession(sessionId) {
|
|
42
50
|
const store = storage.getStore();
|
|
43
51
|
if (store)
|
|
44
52
|
store.sessionId = sessionId;
|
|
45
53
|
else
|
|
46
54
|
ambient = { ...ambient, sessionId };
|
|
47
55
|
}
|
|
48
|
-
|
|
56
|
+
function setTags(tags) {
|
|
49
57
|
const normalized = Object.fromEntries(Object.entries(tags).map(([key, value]) => [key, String(value)]));
|
|
50
58
|
const store = storage.getStore();
|
|
51
59
|
if (store)
|
package/dist/failure-log.js
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.FailureLogger = void 0;
|
|
1
4
|
/**
|
|
2
5
|
* Rate-limited failure logging. Logs the first occurrence of a failure kind
|
|
3
6
|
* immediately, then suppresses repeats within a quiet window and reports how
|
|
4
7
|
* many were suppressed on the next log line for that kind.
|
|
5
8
|
*/
|
|
6
|
-
|
|
9
|
+
class FailureLogger {
|
|
7
10
|
quietMs;
|
|
8
11
|
clock;
|
|
9
12
|
sink;
|
|
@@ -30,3 +33,4 @@ export class FailureLogger {
|
|
|
30
33
|
this.lastLogged.set(kind, now);
|
|
31
34
|
}
|
|
32
35
|
}
|
|
36
|
+
exports.FailureLogger = FailureLogger;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
import { route, setSession, setTags, trace, type RouteOptions, type TraceOptions } from "./context.js";
|
|
2
|
+
import { BatchFirstIneligibleError, batchFirst, type BatchFirstMetadata, type BatchFirstPolicy, type BatchFirstProvider, type BatchFirstRequest, type BatchFirstResult, type BatchFirstSource, type LateBatchInfo } from "./batch-first.js";
|
|
2
3
|
import { track } from "./track.js";
|
|
3
4
|
import { type TransportMode, type WaitUntil } from "./transport.js";
|
|
4
|
-
import { type VercelAISDKMiddleware, type VercelAISDKMiddlewareOptions, type VercelAISDKMiddlewareSpecificationOptions, type VercelAISDKMiddlewareVersionOptions, type VercelAISDKSpecificationVersion } from "./vercel-ai.js";
|
|
5
|
+
import { type VercelAISDKMiddleware, type VercelAISDKMiddlewareOptions as VercelAISDKCompatibilityOptions, type VercelAISDKMiddlewareSpecificationOptions, type VercelAISDKMiddlewareVersionOptions, type VercelAISDKSpecificationVersion } from "./vercel-ai.js";
|
|
5
6
|
export interface MetergraphOptions {
|
|
6
7
|
token?: string;
|
|
7
8
|
ingestUrl?: string;
|
|
8
9
|
captureText?: boolean;
|
|
9
10
|
redact?: (text: string, kind: "request" | "response") => string;
|
|
10
11
|
appRoot?: string;
|
|
12
|
+
repository?: string;
|
|
11
13
|
skipFrames?: string[];
|
|
12
14
|
environment?: string;
|
|
13
15
|
disabled?: boolean;
|
|
@@ -18,6 +20,7 @@ export interface MetergraphOptions {
|
|
|
18
20
|
configPollMs?: number;
|
|
19
21
|
configHardTtlMs?: number;
|
|
20
22
|
}
|
|
23
|
+
export type VercelAISDKMiddlewareOptions<TVersion extends VercelAISDKSpecificationVersion = "v3"> = MetergraphOptions & VercelAISDKCompatibilityOptions<TVersion>;
|
|
21
24
|
export interface ModelForOptions {
|
|
22
25
|
default: string;
|
|
23
26
|
sessionKey?: string;
|
|
@@ -54,9 +57,19 @@ export declare function wrap<T extends Record<PropertyKey, any>>(client: T, prov
|
|
|
54
57
|
export declare const wrapClient: typeof wrap;
|
|
55
58
|
/**
|
|
56
59
|
* Capture Vercel AI SDK language-model calls through its middleware API.
|
|
57
|
-
*
|
|
60
|
+
* Initializes Metergraph from these options when no runtime exists. The
|
|
61
|
+
* returned middleware has no runtime dependency on `ai`.
|
|
58
62
|
*/
|
|
59
|
-
export declare function vercelAISDKMiddleware(options: VercelAISDKMiddlewareVersionOptions): VercelAISDKMiddleware<"v2">;
|
|
60
|
-
export declare function vercelAISDKMiddleware<TVersion extends VercelAISDKSpecificationVersion = "v3">(options?: VercelAISDKMiddlewareSpecificationOptions<TVersion>): VercelAISDKMiddleware<TVersion>;
|
|
63
|
+
export declare function vercelAISDKMiddleware(options: MetergraphOptions & VercelAISDKMiddlewareVersionOptions): VercelAISDKMiddleware<"v2">;
|
|
64
|
+
export declare function vercelAISDKMiddleware<TVersion extends VercelAISDKSpecificationVersion = "v3">(options?: MetergraphOptions & VercelAISDKMiddlewareSpecificationOptions<TVersion>): VercelAISDKMiddleware<TVersion>;
|
|
61
65
|
export { route, setSession, setTags, trace, track };
|
|
62
|
-
export type { RouteOptions, TraceOptions, TransportMode, VercelAISDKMiddleware,
|
|
66
|
+
export type { RouteOptions, TraceOptions, TransportMode, VercelAISDKMiddleware, VercelAISDKSpecificationVersion, };
|
|
67
|
+
/**
|
|
68
|
+
* Explicit, opt-in batch-first execution over a provider's Batch API —
|
|
69
|
+
* never enabled by wrap()/capture defaults or by an environment variable.
|
|
70
|
+
* See BatchFirstPolicy for the required, explicit acknowledgements
|
|
71
|
+
* (acceptDuplicateProviderExecution, and allowDuplicateToolCallPlans for
|
|
72
|
+
* requests that include tools).
|
|
73
|
+
*/
|
|
74
|
+
export { batchFirst, BatchFirstIneligibleError };
|
|
75
|
+
export type { BatchFirstMetadata, BatchFirstPolicy, BatchFirstProvider, BatchFirstRequest, BatchFirstResult, BatchFirstSource, LateBatchInfo, };
|
package/dist/index.js
CHANGED
|
@@ -1,19 +1,42 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BatchFirstIneligibleError = exports.batchFirst = exports.track = exports.trace = exports.setTags = exports.setSession = exports.route = exports.wrapClient = exports.DEFAULT_INGEST_URL = void 0;
|
|
4
|
+
exports.init = init;
|
|
5
|
+
exports.modelFor = modelFor;
|
|
6
|
+
exports.recordOutcome = recordOutcome;
|
|
7
|
+
exports.bindWaitUntil = bindWaitUntil;
|
|
8
|
+
exports.flush = flush;
|
|
9
|
+
exports.shutdown = shutdown;
|
|
10
|
+
exports.wrapHandler = wrapHandler;
|
|
11
|
+
exports.wrap = wrap;
|
|
12
|
+
exports.vercelAISDKMiddleware = vercelAISDKMiddleware;
|
|
13
|
+
const capture_js_1 = require("./capture.js");
|
|
14
|
+
const config_js_1 = require("./config.js");
|
|
15
|
+
const context_js_1 = require("./context.js");
|
|
16
|
+
Object.defineProperty(exports, "route", { enumerable: true, get: function () { return context_js_1.route; } });
|
|
17
|
+
Object.defineProperty(exports, "setSession", { enumerable: true, get: function () { return context_js_1.setSession; } });
|
|
18
|
+
Object.defineProperty(exports, "setTags", { enumerable: true, get: function () { return context_js_1.setTags; } });
|
|
19
|
+
Object.defineProperty(exports, "trace", { enumerable: true, get: function () { return context_js_1.trace; } });
|
|
20
|
+
const batch_first_js_1 = require("./batch-first.js");
|
|
21
|
+
Object.defineProperty(exports, "BatchFirstIneligibleError", { enumerable: true, get: function () { return batch_first_js_1.BatchFirstIneligibleError; } });
|
|
22
|
+
Object.defineProperty(exports, "batchFirst", { enumerable: true, get: function () { return batch_first_js_1.batchFirst; } });
|
|
23
|
+
const node_fs_1 = require("node:fs");
|
|
24
|
+
const repo_config_js_1 = require("./repo-config.js");
|
|
25
|
+
const session_js_1 = require("./session.js");
|
|
26
|
+
const track_js_1 = require("./track.js");
|
|
27
|
+
Object.defineProperty(exports, "track", { enumerable: true, get: function () { return track_js_1.track; } });
|
|
28
|
+
const transport_js_1 = require("./transport.js");
|
|
29
|
+
const version_js_1 = require("./version.js");
|
|
30
|
+
const wrap_js_1 = require("./wrap.js");
|
|
31
|
+
const vercel_ai_js_1 = require("./vercel-ai.js");
|
|
11
32
|
let initialized = false;
|
|
12
33
|
let warnedNoToken = false;
|
|
34
|
+
let warnedNoRepository = false;
|
|
35
|
+
let warnedRepeatedInit = false;
|
|
13
36
|
let transport;
|
|
14
37
|
let config;
|
|
15
38
|
let sessionManager;
|
|
16
|
-
|
|
39
|
+
exports.DEFAULT_INGEST_URL = "https://d2xus7mp8zdv6t.cloudfront.net";
|
|
17
40
|
function env(name) {
|
|
18
41
|
return typeof process === "undefined" ? undefined : process.env[name];
|
|
19
42
|
}
|
|
@@ -23,15 +46,35 @@ function envBool(name, fallback) {
|
|
|
23
46
|
? fallback
|
|
24
47
|
: !["0", "false", "no", "off"].includes(value.toLowerCase());
|
|
25
48
|
}
|
|
26
|
-
|
|
27
|
-
|
|
49
|
+
function repositoryConfig(options, appRoot) {
|
|
50
|
+
const explicit = options.repository ?? env("METERGRAPH_REPOSITORY");
|
|
51
|
+
if (typeof explicit === "string" && explicit.trim().includes("/")) {
|
|
52
|
+
let repoRoot = appRoot;
|
|
53
|
+
try {
|
|
54
|
+
repoRoot = (0, node_fs_1.realpathSync)(appRoot);
|
|
55
|
+
}
|
|
56
|
+
catch { /* attribution remains best-effort */ }
|
|
57
|
+
return { repository: explicit.trim(), repoRoot };
|
|
58
|
+
}
|
|
59
|
+
return (0, repo_config_js_1.discoverRepoConfig)(appRoot);
|
|
60
|
+
}
|
|
61
|
+
function warnOnRepeatedInit() {
|
|
62
|
+
if (warnedRepeatedInit)
|
|
28
63
|
return;
|
|
64
|
+
warnedRepeatedInit = true;
|
|
65
|
+
console.warn("Metergraph init() was called more than once; the first configuration remains active.");
|
|
66
|
+
}
|
|
67
|
+
function init(options = {}) {
|
|
68
|
+
if (initialized) {
|
|
69
|
+
warnOnRepeatedInit();
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
29
72
|
if (env("METERGRAPH_DISABLED") === "1" || options.disabled) {
|
|
30
73
|
initialized = true;
|
|
31
74
|
return;
|
|
32
75
|
}
|
|
33
76
|
const token = options.token ?? env("METERGRAPH_APP_TOKEN");
|
|
34
|
-
const ingestUrl = options.ingestUrl ?? env("METERGRAPH_INGEST_URL") ?? DEFAULT_INGEST_URL;
|
|
77
|
+
const ingestUrl = options.ingestUrl ?? env("METERGRAPH_INGEST_URL") ?? exports.DEFAULT_INGEST_URL;
|
|
35
78
|
if (!token || !ingestUrl) {
|
|
36
79
|
// Stay uninitialized so a later init() that supplies a token succeeds.
|
|
37
80
|
if (!warnedNoToken) {
|
|
@@ -40,21 +83,25 @@ export function init(options = {}) {
|
|
|
40
83
|
}
|
|
41
84
|
return;
|
|
42
85
|
}
|
|
43
|
-
initialized = true;
|
|
44
86
|
try {
|
|
45
87
|
const appRoot = options.appRoot ?? (typeof process === "undefined" ? "" : process.cwd());
|
|
46
|
-
const repoConfig =
|
|
88
|
+
const repoConfig = repositoryConfig(options, appRoot);
|
|
89
|
+
initialized = true;
|
|
90
|
+
if (!repoConfig && !warnedNoRepository) {
|
|
91
|
+
warnedNoRepository = true;
|
|
92
|
+
console.warn("Metergraph repository identity is not configured; set init({ repository: \"owner/repository\" }), METERGRAPH_REPOSITORY, or provide .metergraph/config.json. Continuing with legacy ingestion.");
|
|
93
|
+
}
|
|
47
94
|
sessionManager = repoConfig
|
|
48
|
-
? new SessionManager(token, ingestUrl, repoConfig.repository, SDK_VERSION)
|
|
95
|
+
? new session_js_1.SessionManager(token, ingestUrl, repoConfig.repository, version_js_1.SDK_VERSION)
|
|
49
96
|
: undefined;
|
|
50
|
-
transport = new Transport(token, ingestUrl, {
|
|
97
|
+
transport = new transport_js_1.Transport(token, ingestUrl, {
|
|
51
98
|
queueSize: options.queueSize ?? Number(env("METERGRAPH_QUEUE_SIZE") ?? 2_000),
|
|
52
99
|
batchSize: options.batchSize ?? Number(env("METERGRAPH_BATCH_SIZE") ?? 100),
|
|
53
100
|
flushMs: options.flushMs ?? Number(env("METERGRAPH_FLUSH_MS") ?? 5_000),
|
|
54
101
|
mode: options.transport ?? "auto",
|
|
55
102
|
session: sessionManager,
|
|
56
103
|
});
|
|
57
|
-
setCaptureRuntime(new CaptureRuntime(transport, {
|
|
104
|
+
(0, wrap_js_1.setCaptureRuntime)(new capture_js_1.CaptureRuntime(transport, {
|
|
58
105
|
captureText: options.captureText ?? envBool("METERGRAPH_CAPTURE_TEXT", true),
|
|
59
106
|
redact: options.redact,
|
|
60
107
|
appRoot,
|
|
@@ -63,26 +110,26 @@ export function init(options = {}) {
|
|
|
63
110
|
environment: options.environment ?? env("METERGRAPH_ENV"),
|
|
64
111
|
textMaxBytes: Math.min(100 * 1024, Math.max(1, Number(env("METERGRAPH_TEXT_MAX_BYTES") ?? 100 * 1024))),
|
|
65
112
|
}));
|
|
66
|
-
config = new ConfigPoller(token, ingestUrl, options.configPollMs, options.configHardTtlMs);
|
|
113
|
+
config = new config_js_1.ConfigPoller(token, ingestUrl, options.configPollMs, options.configHardTtlMs);
|
|
67
114
|
}
|
|
68
115
|
catch {
|
|
69
116
|
transport = undefined;
|
|
70
117
|
config = undefined;
|
|
71
118
|
sessionManager = undefined;
|
|
72
|
-
setCaptureRuntime();
|
|
119
|
+
(0, wrap_js_1.setCaptureRuntime)();
|
|
73
120
|
console.warn("Metergraph initialization failed; application is running uninstrumented");
|
|
74
121
|
}
|
|
75
122
|
}
|
|
76
|
-
|
|
123
|
+
function modelFor(routeName, options) {
|
|
77
124
|
if (typeof options?.default !== "string" || !options.default) {
|
|
78
125
|
throw new TypeError("modelFor requires options.default to be a non-empty string");
|
|
79
126
|
}
|
|
80
|
-
return config?.modelFor(routeName, options.default, options.sessionKey ?? contextSnapshot().sessionId) ?? options.default;
|
|
127
|
+
return config?.modelFor(routeName, options.default, options.sessionKey ?? (0, context_js_1.contextSnapshot)().sessionId) ?? options.default;
|
|
81
128
|
}
|
|
82
|
-
|
|
129
|
+
function recordOutcome(routeName, options) {
|
|
83
130
|
const routeNameClean = String(routeName).trim().slice(0, 512);
|
|
84
131
|
const model = String(options.model).trim().slice(0, 512);
|
|
85
|
-
const sessionKey = String(options.sessionKey ?? contextSnapshot().sessionId ?? "").trim().slice(0, 512);
|
|
132
|
+
const sessionKey = String(options.sessionKey ?? (0, context_js_1.contextSnapshot)().sessionId ?? "").trim().slice(0, 512);
|
|
86
133
|
const randomId = globalThis.crypto?.randomUUID?.()
|
|
87
134
|
?? `${Date.now().toString(36)}-${Math.random().toString(36).slice(2)}`;
|
|
88
135
|
const eventId = String(options.eventId ?? randomId).trim().slice(0, 128);
|
|
@@ -117,22 +164,22 @@ export function recordOutcome(routeName, options) {
|
|
|
117
164
|
regeneration_count: options.regenerationCount,
|
|
118
165
|
});
|
|
119
166
|
}
|
|
120
|
-
|
|
167
|
+
function bindWaitUntil(value) {
|
|
121
168
|
transport?.bindWaitUntil(value);
|
|
122
169
|
}
|
|
123
|
-
|
|
170
|
+
async function flush(timeoutMs = 3_000) {
|
|
124
171
|
return transport?.flush(timeoutMs) ?? true;
|
|
125
172
|
}
|
|
126
|
-
|
|
173
|
+
async function shutdown() {
|
|
127
174
|
config?.stop();
|
|
128
175
|
config = undefined;
|
|
129
176
|
await transport?.shutdown();
|
|
130
177
|
transport = undefined;
|
|
131
178
|
sessionManager?.stop();
|
|
132
179
|
sessionManager = undefined;
|
|
133
|
-
setCaptureRuntime();
|
|
180
|
+
(0, wrap_js_1.setCaptureRuntime)();
|
|
134
181
|
}
|
|
135
|
-
|
|
182
|
+
function wrapHandler(handler) {
|
|
136
183
|
return async (...args) => {
|
|
137
184
|
try {
|
|
138
185
|
return await handler(...args);
|
|
@@ -148,13 +195,14 @@ export function wrapHandler(handler) {
|
|
|
148
195
|
* Calls init() automatically, so with env-var configuration this is the
|
|
149
196
|
* only setup line needed. Call init(...) first to pass options in code.
|
|
150
197
|
*/
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
198
|
+
function wrap(client, provider) {
|
|
199
|
+
if (!initialized)
|
|
200
|
+
init();
|
|
201
|
+
return (0, wrap_js_1.wrap)(client, provider);
|
|
154
202
|
}
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
203
|
+
exports.wrapClient = wrap;
|
|
204
|
+
function vercelAISDKMiddleware(options = {}) {
|
|
205
|
+
if (!initialized)
|
|
206
|
+
init(options);
|
|
207
|
+
return (0, vercel_ai_js_1.createVercelAISDKMiddleware)(options);
|
|
159
208
|
}
|
|
160
|
-
export { route, setSession, setTags, trace, track };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import metergraph from "./index.js";
|
|
2
|
+
|
|
3
|
+
const {
|
|
4
|
+
BatchFirstIneligibleError,
|
|
5
|
+
DEFAULT_INGEST_URL,
|
|
6
|
+
batchFirst,
|
|
7
|
+
bindWaitUntil,
|
|
8
|
+
flush,
|
|
9
|
+
init,
|
|
10
|
+
modelFor,
|
|
11
|
+
recordOutcome,
|
|
12
|
+
route,
|
|
13
|
+
setSession,
|
|
14
|
+
setTags,
|
|
15
|
+
shutdown,
|
|
16
|
+
trace,
|
|
17
|
+
track,
|
|
18
|
+
vercelAISDKMiddleware,
|
|
19
|
+
wrap,
|
|
20
|
+
wrapClient,
|
|
21
|
+
wrapHandler,
|
|
22
|
+
} = metergraph;
|
|
23
|
+
|
|
24
|
+
export {
|
|
25
|
+
BatchFirstIneligibleError,
|
|
26
|
+
DEFAULT_INGEST_URL,
|
|
27
|
+
batchFirst,
|
|
28
|
+
bindWaitUntil,
|
|
29
|
+
flush,
|
|
30
|
+
init,
|
|
31
|
+
modelFor,
|
|
32
|
+
recordOutcome,
|
|
33
|
+
route,
|
|
34
|
+
setSession,
|
|
35
|
+
setTags,
|
|
36
|
+
shutdown,
|
|
37
|
+
trace,
|
|
38
|
+
track,
|
|
39
|
+
vercelAISDKMiddleware,
|
|
40
|
+
wrap,
|
|
41
|
+
wrapClient,
|
|
42
|
+
wrapHandler,
|
|
43
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type":"commonjs"}
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The narrow adapter boundary batchFirst() drives, plus a worker-only OpenAI
|
|
3
|
+
* Batch API implementation. Deliberately duck-typed against the openai
|
|
4
|
+
* package's shape (see OpenAIBatchCapableClient) rather than importing it —
|
|
5
|
+
* `openai` is an optional peer dependency of this package, and structural
|
|
6
|
+
* typing already lets a real `OpenAI` client instance satisfy this
|
|
7
|
+
* interface without any import, type-only or otherwise.
|
|
8
|
+
*
|
|
9
|
+
* Every method here returns only bounded, structural information — a
|
|
10
|
+
* status, a result the caller explicitly asked for, and a boolean noting
|
|
11
|
+
* whether that result happened to contain a tool-call plan. No adapter
|
|
12
|
+
* method inspects or logs a provider error body, a credential, or a raw
|
|
13
|
+
* prompt.
|
|
14
|
+
*/
|
|
15
|
+
/** A caller's own provider request body (e.g. an OpenAI Responses payload).
|
|
16
|
+
* Only `stream` and `tools` are inspected by this module; every other
|
|
17
|
+
* field passes through untouched. */
|
|
18
|
+
export type BatchFirstRequest = Record<string, unknown> & {
|
|
19
|
+
stream?: boolean;
|
|
20
|
+
tools?: unknown[];
|
|
21
|
+
};
|
|
22
|
+
export interface BatchHandle {
|
|
23
|
+
/** The provider's own batch identifier — never returned to a caller of
|
|
24
|
+
* batchFirst() or logged; adapters use it only to poll/read their own
|
|
25
|
+
* batch. */
|
|
26
|
+
providerBatchId: string;
|
|
27
|
+
}
|
|
28
|
+
export type BatchPollStatus = "pending" | "completed" | "failed" | "expired";
|
|
29
|
+
export interface BatchPollResult {
|
|
30
|
+
status: BatchPollStatus;
|
|
31
|
+
}
|
|
32
|
+
export interface ProviderBatchResult<TResult = unknown> {
|
|
33
|
+
result: TResult;
|
|
34
|
+
/** Whether this result's own output happens to include a tool/function
|
|
35
|
+
* call — reported as a boolean only, never by exposing the call's
|
|
36
|
+
* arguments or the surrounding response through this flag. */
|
|
37
|
+
containedToolCallPlan: boolean;
|
|
38
|
+
}
|
|
39
|
+
export interface ProviderBatchEligibility {
|
|
40
|
+
eligible: boolean;
|
|
41
|
+
reason?: string;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* The seam batchFirst() is written against. An adapter submits exactly one
|
|
45
|
+
* request per batch and knows how to poll it, read its terminal result,
|
|
46
|
+
* and run the same request directly (bypassing batch entirely) as a
|
|
47
|
+
* fallback.
|
|
48
|
+
*/
|
|
49
|
+
export interface ProviderBatchAdapter<TResult = unknown> {
|
|
50
|
+
eligibility(request: BatchFirstRequest): ProviderBatchEligibility;
|
|
51
|
+
submitOne(request: BatchFirstRequest): Promise<BatchHandle>;
|
|
52
|
+
poll(handle: BatchHandle): Promise<BatchPollResult>;
|
|
53
|
+
readResult(handle: BatchHandle): Promise<ProviderBatchResult<TResult>>;
|
|
54
|
+
direct(request: BatchFirstRequest): Promise<ProviderBatchResult<TResult>>;
|
|
55
|
+
}
|
|
56
|
+
/** The minimal slice of the `openai` package's client shape this adapter
|
|
57
|
+
* calls. A real `OpenAI` client instance satisfies this structurally. */
|
|
58
|
+
export interface OpenAIBatchCapableClient {
|
|
59
|
+
files: {
|
|
60
|
+
create(params: {
|
|
61
|
+
file: Blob;
|
|
62
|
+
purpose: string;
|
|
63
|
+
}): Promise<{
|
|
64
|
+
id: string;
|
|
65
|
+
}>;
|
|
66
|
+
content(fileId: string): Promise<{
|
|
67
|
+
text(): Promise<string>;
|
|
68
|
+
}>;
|
|
69
|
+
};
|
|
70
|
+
batches: {
|
|
71
|
+
create(params: {
|
|
72
|
+
input_file_id: string;
|
|
73
|
+
endpoint: string;
|
|
74
|
+
completion_window: string;
|
|
75
|
+
}): Promise<{
|
|
76
|
+
id: string;
|
|
77
|
+
status?: string;
|
|
78
|
+
}>;
|
|
79
|
+
retrieve(batchId: string): Promise<{
|
|
80
|
+
id: string;
|
|
81
|
+
status?: string;
|
|
82
|
+
output_file_id?: string | null;
|
|
83
|
+
}>;
|
|
84
|
+
};
|
|
85
|
+
responses: {
|
|
86
|
+
create(request: Record<string, unknown>): Promise<Record<string, unknown>>;
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
export declare class ProviderBatchError extends Error {
|
|
90
|
+
}
|
|
91
|
+
export declare function createOpenAIBatchAdapter(client: OpenAIBatchCapableClient): ProviderBatchAdapter;
|
|
92
|
+
/** The minimal slice of the `@anthropic-ai/sdk` package's client shape this
|
|
93
|
+
* adapter calls. A real `Anthropic` client instance satisfies this
|
|
94
|
+
* structurally — see `patchAnthropicBatchResults` in wrap.ts, which
|
|
95
|
+
* confirms `messages.batches.results()` returns an async iterable of
|
|
96
|
+
* `{custom_id, result: {type, message}}` items against the real SDK. */
|
|
97
|
+
export interface AnthropicBatchCapableClient {
|
|
98
|
+
messages: {
|
|
99
|
+
create(request: Record<string, unknown>): Promise<Record<string, unknown>>;
|
|
100
|
+
batches: {
|
|
101
|
+
create(params: {
|
|
102
|
+
requests: Array<{
|
|
103
|
+
custom_id: string;
|
|
104
|
+
params: Record<string, unknown>;
|
|
105
|
+
}>;
|
|
106
|
+
}): Promise<{
|
|
107
|
+
id: string;
|
|
108
|
+
processing_status?: string;
|
|
109
|
+
}>;
|
|
110
|
+
retrieve(batchId: string): Promise<{
|
|
111
|
+
id: string;
|
|
112
|
+
processing_status?: string;
|
|
113
|
+
request_counts?: Record<string, number>;
|
|
114
|
+
}>;
|
|
115
|
+
results(batchId: string): AsyncIterable<unknown>;
|
|
116
|
+
};
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
export declare function createAnthropicBatchAdapter(client: AnthropicBatchCapableClient): ProviderBatchAdapter;
|
|
120
|
+
/** The minimal slice of the `@google/genai` package's client shape this
|
|
121
|
+
* adapter calls. A real Google GenAI client instance satisfies this
|
|
122
|
+
* structurally. The exact batch job/response wire shape below (`state` vs
|
|
123
|
+
* `metadata.state`, `dest.inlinedResponses`) is our best-effort reading of
|
|
124
|
+
* the current Gemini Batch API and needs live/gated verification before
|
|
125
|
+
* production use — see this module's caller-facing report for specifics. */
|
|
126
|
+
export interface GoogleBatchCapableClient {
|
|
127
|
+
models: {
|
|
128
|
+
generateContent(request: Record<string, unknown>): Promise<Record<string, unknown>>;
|
|
129
|
+
};
|
|
130
|
+
batches: {
|
|
131
|
+
create(params: {
|
|
132
|
+
model: string;
|
|
133
|
+
src: {
|
|
134
|
+
inlinedRequests: Array<Record<string, unknown>>;
|
|
135
|
+
};
|
|
136
|
+
}): Promise<{
|
|
137
|
+
name: string;
|
|
138
|
+
state?: string;
|
|
139
|
+
metadata?: {
|
|
140
|
+
state?: string;
|
|
141
|
+
};
|
|
142
|
+
}>;
|
|
143
|
+
get(params: {
|
|
144
|
+
name: string;
|
|
145
|
+
}): Promise<{
|
|
146
|
+
name: string;
|
|
147
|
+
state?: string;
|
|
148
|
+
metadata?: {
|
|
149
|
+
state?: string;
|
|
150
|
+
};
|
|
151
|
+
dest?: {
|
|
152
|
+
inlinedResponses?: Array<{
|
|
153
|
+
response?: Record<string, unknown>;
|
|
154
|
+
error?: Record<string, unknown>;
|
|
155
|
+
}>;
|
|
156
|
+
};
|
|
157
|
+
}>;
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
export declare function createGoogleBatchAdapter(client: GoogleBatchCapableClient): ProviderBatchAdapter;
|