metergraph 0.5.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 +46 -16
- package/dist/batch-first.js +13 -7
- 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 +8 -5
- package/dist/index.js +86 -47
- package/dist/index.mjs +43 -0
- package/dist/package.json +1 -0
- package/dist/provider-batch.js +11 -4
- 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/index.js
CHANGED
|
@@ -1,20 +1,42 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
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");
|
|
12
32
|
let initialized = false;
|
|
13
33
|
let warnedNoToken = false;
|
|
34
|
+
let warnedNoRepository = false;
|
|
35
|
+
let warnedRepeatedInit = false;
|
|
14
36
|
let transport;
|
|
15
37
|
let config;
|
|
16
38
|
let sessionManager;
|
|
17
|
-
|
|
39
|
+
exports.DEFAULT_INGEST_URL = "https://d2xus7mp8zdv6t.cloudfront.net";
|
|
18
40
|
function env(name) {
|
|
19
41
|
return typeof process === "undefined" ? undefined : process.env[name];
|
|
20
42
|
}
|
|
@@ -24,15 +46,35 @@ function envBool(name, fallback) {
|
|
|
24
46
|
? fallback
|
|
25
47
|
: !["0", "false", "no", "off"].includes(value.toLowerCase());
|
|
26
48
|
}
|
|
27
|
-
|
|
28
|
-
|
|
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)
|
|
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();
|
|
29
70
|
return;
|
|
71
|
+
}
|
|
30
72
|
if (env("METERGRAPH_DISABLED") === "1" || options.disabled) {
|
|
31
73
|
initialized = true;
|
|
32
74
|
return;
|
|
33
75
|
}
|
|
34
76
|
const token = options.token ?? env("METERGRAPH_APP_TOKEN");
|
|
35
|
-
const ingestUrl = options.ingestUrl ?? env("METERGRAPH_INGEST_URL") ?? DEFAULT_INGEST_URL;
|
|
77
|
+
const ingestUrl = options.ingestUrl ?? env("METERGRAPH_INGEST_URL") ?? exports.DEFAULT_INGEST_URL;
|
|
36
78
|
if (!token || !ingestUrl) {
|
|
37
79
|
// Stay uninitialized so a later init() that supplies a token succeeds.
|
|
38
80
|
if (!warnedNoToken) {
|
|
@@ -41,21 +83,25 @@ export function init(options = {}) {
|
|
|
41
83
|
}
|
|
42
84
|
return;
|
|
43
85
|
}
|
|
44
|
-
initialized = true;
|
|
45
86
|
try {
|
|
46
87
|
const appRoot = options.appRoot ?? (typeof process === "undefined" ? "" : process.cwd());
|
|
47
|
-
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
|
+
}
|
|
48
94
|
sessionManager = repoConfig
|
|
49
|
-
? new SessionManager(token, ingestUrl, repoConfig.repository, SDK_VERSION)
|
|
95
|
+
? new session_js_1.SessionManager(token, ingestUrl, repoConfig.repository, version_js_1.SDK_VERSION)
|
|
50
96
|
: undefined;
|
|
51
|
-
transport = new Transport(token, ingestUrl, {
|
|
97
|
+
transport = new transport_js_1.Transport(token, ingestUrl, {
|
|
52
98
|
queueSize: options.queueSize ?? Number(env("METERGRAPH_QUEUE_SIZE") ?? 2_000),
|
|
53
99
|
batchSize: options.batchSize ?? Number(env("METERGRAPH_BATCH_SIZE") ?? 100),
|
|
54
100
|
flushMs: options.flushMs ?? Number(env("METERGRAPH_FLUSH_MS") ?? 5_000),
|
|
55
101
|
mode: options.transport ?? "auto",
|
|
56
102
|
session: sessionManager,
|
|
57
103
|
});
|
|
58
|
-
setCaptureRuntime(new CaptureRuntime(transport, {
|
|
104
|
+
(0, wrap_js_1.setCaptureRuntime)(new capture_js_1.CaptureRuntime(transport, {
|
|
59
105
|
captureText: options.captureText ?? envBool("METERGRAPH_CAPTURE_TEXT", true),
|
|
60
106
|
redact: options.redact,
|
|
61
107
|
appRoot,
|
|
@@ -64,26 +110,26 @@ export function init(options = {}) {
|
|
|
64
110
|
environment: options.environment ?? env("METERGRAPH_ENV"),
|
|
65
111
|
textMaxBytes: Math.min(100 * 1024, Math.max(1, Number(env("METERGRAPH_TEXT_MAX_BYTES") ?? 100 * 1024))),
|
|
66
112
|
}));
|
|
67
|
-
config = new ConfigPoller(token, ingestUrl, options.configPollMs, options.configHardTtlMs);
|
|
113
|
+
config = new config_js_1.ConfigPoller(token, ingestUrl, options.configPollMs, options.configHardTtlMs);
|
|
68
114
|
}
|
|
69
115
|
catch {
|
|
70
116
|
transport = undefined;
|
|
71
117
|
config = undefined;
|
|
72
118
|
sessionManager = undefined;
|
|
73
|
-
setCaptureRuntime();
|
|
119
|
+
(0, wrap_js_1.setCaptureRuntime)();
|
|
74
120
|
console.warn("Metergraph initialization failed; application is running uninstrumented");
|
|
75
121
|
}
|
|
76
122
|
}
|
|
77
|
-
|
|
123
|
+
function modelFor(routeName, options) {
|
|
78
124
|
if (typeof options?.default !== "string" || !options.default) {
|
|
79
125
|
throw new TypeError("modelFor requires options.default to be a non-empty string");
|
|
80
126
|
}
|
|
81
|
-
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;
|
|
82
128
|
}
|
|
83
|
-
|
|
129
|
+
function recordOutcome(routeName, options) {
|
|
84
130
|
const routeNameClean = String(routeName).trim().slice(0, 512);
|
|
85
131
|
const model = String(options.model).trim().slice(0, 512);
|
|
86
|
-
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);
|
|
87
133
|
const randomId = globalThis.crypto?.randomUUID?.()
|
|
88
134
|
?? `${Date.now().toString(36)}-${Math.random().toString(36).slice(2)}`;
|
|
89
135
|
const eventId = String(options.eventId ?? randomId).trim().slice(0, 128);
|
|
@@ -118,22 +164,22 @@ export function recordOutcome(routeName, options) {
|
|
|
118
164
|
regeneration_count: options.regenerationCount,
|
|
119
165
|
});
|
|
120
166
|
}
|
|
121
|
-
|
|
167
|
+
function bindWaitUntil(value) {
|
|
122
168
|
transport?.bindWaitUntil(value);
|
|
123
169
|
}
|
|
124
|
-
|
|
170
|
+
async function flush(timeoutMs = 3_000) {
|
|
125
171
|
return transport?.flush(timeoutMs) ?? true;
|
|
126
172
|
}
|
|
127
|
-
|
|
173
|
+
async function shutdown() {
|
|
128
174
|
config?.stop();
|
|
129
175
|
config = undefined;
|
|
130
176
|
await transport?.shutdown();
|
|
131
177
|
transport = undefined;
|
|
132
178
|
sessionManager?.stop();
|
|
133
179
|
sessionManager = undefined;
|
|
134
|
-
setCaptureRuntime();
|
|
180
|
+
(0, wrap_js_1.setCaptureRuntime)();
|
|
135
181
|
}
|
|
136
|
-
|
|
182
|
+
function wrapHandler(handler) {
|
|
137
183
|
return async (...args) => {
|
|
138
184
|
try {
|
|
139
185
|
return await handler(...args);
|
|
@@ -149,21 +195,14 @@ export function wrapHandler(handler) {
|
|
|
149
195
|
* Calls init() automatically, so with env-var configuration this is the
|
|
150
196
|
* only setup line needed. Call init(...) first to pass options in code.
|
|
151
197
|
*/
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
198
|
+
function wrap(client, provider) {
|
|
199
|
+
if (!initialized)
|
|
200
|
+
init();
|
|
201
|
+
return (0, wrap_js_1.wrap)(client, provider);
|
|
155
202
|
}
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
203
|
+
exports.wrapClient = wrap;
|
|
204
|
+
function vercelAISDKMiddleware(options = {}) {
|
|
205
|
+
if (!initialized)
|
|
206
|
+
init(options);
|
|
207
|
+
return (0, vercel_ai_js_1.createVercelAISDKMiddleware)(options);
|
|
160
208
|
}
|
|
161
|
-
export { route, setSession, setTags, trace, track };
|
|
162
|
-
/**
|
|
163
|
-
* Explicit, opt-in batch-first execution over a provider's Batch API —
|
|
164
|
-
* never enabled by wrap()/capture defaults or by an environment variable.
|
|
165
|
-
* See BatchFirstPolicy for the required, explicit acknowledgements
|
|
166
|
-
* (acceptDuplicateProviderExecution, and allowDuplicateToolCallPlans for
|
|
167
|
-
* requests that include tools).
|
|
168
|
-
*/
|
|
169
|
-
export { batchFirst, BatchFirstIneligibleError };
|
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"}
|
package/dist/provider-batch.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
"use strict";
|
|
1
2
|
/**
|
|
2
3
|
* The narrow adapter boundary batchFirst() drives, plus a worker-only OpenAI
|
|
3
4
|
* Batch API implementation. Deliberately duck-typed against the openai
|
|
@@ -12,6 +13,11 @@
|
|
|
12
13
|
* method inspects or logs a provider error body, a credential, or a raw
|
|
13
14
|
* prompt.
|
|
14
15
|
*/
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.ProviderBatchError = void 0;
|
|
18
|
+
exports.createOpenAIBatchAdapter = createOpenAIBatchAdapter;
|
|
19
|
+
exports.createAnthropicBatchAdapter = createAnthropicBatchAdapter;
|
|
20
|
+
exports.createGoogleBatchAdapter = createGoogleBatchAdapter;
|
|
15
21
|
const OPENAI_BATCH_ENDPOINT = "/v1/responses";
|
|
16
22
|
const OPENAI_COMPLETION_WINDOW = "24h";
|
|
17
23
|
// "validating" | "in_progress" | "finalizing" | "cancelling" all fall
|
|
@@ -32,9 +38,10 @@ function randomCustomId() {
|
|
|
32
38
|
return globalThis.crypto.randomUUID();
|
|
33
39
|
return `batch-first-${Date.now().toString(36)}-${Math.random().toString(36).slice(2)}`;
|
|
34
40
|
}
|
|
35
|
-
|
|
41
|
+
class ProviderBatchError extends Error {
|
|
36
42
|
}
|
|
37
|
-
|
|
43
|
+
exports.ProviderBatchError = ProviderBatchError;
|
|
44
|
+
function createOpenAIBatchAdapter(client) {
|
|
38
45
|
return {
|
|
39
46
|
eligibility(request) {
|
|
40
47
|
if (request.stream === true) {
|
|
@@ -118,7 +125,7 @@ function hasToolUse(message) {
|
|
|
118
125
|
return false;
|
|
119
126
|
return content.some((block) => get(block, "type") === "tool_use");
|
|
120
127
|
}
|
|
121
|
-
|
|
128
|
+
function createAnthropicBatchAdapter(client) {
|
|
122
129
|
return {
|
|
123
130
|
eligibility(request) {
|
|
124
131
|
if (request.stream === true) {
|
|
@@ -187,7 +194,7 @@ function hasFunctionCallPart(response) {
|
|
|
187
194
|
return parts.some((part) => get(part, "functionCall") != null || get(part, "function_call") != null);
|
|
188
195
|
});
|
|
189
196
|
}
|
|
190
|
-
|
|
197
|
+
function createGoogleBatchAdapter(client) {
|
|
191
198
|
return {
|
|
192
199
|
eligibility(request) {
|
|
193
200
|
if (request.stream === true) {
|
package/dist/repo-config.d.ts
CHANGED
|
@@ -18,5 +18,5 @@ export declare function discoverRepoConfig(appRoot: string): RepoConfig | undefi
|
|
|
18
18
|
export declare function writeConfigAtomically(repoRoot: string, repository: string): RepoConfig | undefined;
|
|
19
19
|
/** Discover an existing repo config, or detect+write one once at the git top
|
|
20
20
|
* level. Fail-open: any detection or write failure returns undefined
|
|
21
|
-
* (
|
|
21
|
+
* (app-token ingestion), never throws. */
|
|
22
22
|
export declare function ensureRepoConfig(appRoot: string): RepoConfig | undefined;
|
package/dist/repo-config.js
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SUPPORTED_CONFIG_VERSION = exports.CONFIG_FILENAME = exports.CONFIG_DIRNAME = void 0;
|
|
4
|
+
exports.normalizeGithubRemote = normalizeGithubRemote;
|
|
5
|
+
exports.discoverRepoConfig = discoverRepoConfig;
|
|
6
|
+
exports.writeConfigAtomically = writeConfigAtomically;
|
|
7
|
+
exports.ensureRepoConfig = ensureRepoConfig;
|
|
1
8
|
/**
|
|
2
|
-
*
|
|
9
|
+
* SDK 0.4+ repository-aware ingestion: discover, detect, and write
|
|
3
10
|
* .metergraph/config.json.
|
|
4
11
|
*
|
|
5
12
|
* Discovery is purely file-based (never shells out to git), so a committed
|
|
@@ -8,13 +15,13 @@
|
|
|
8
15
|
* out to git to find the repo's top level and GitHub origin, then writes the
|
|
9
16
|
* config there exactly once. An existing file is always authoritative and is
|
|
10
17
|
* never overwritten. Every failure mode here is fail-open -- callers get
|
|
11
|
-
* undefined and fall back to
|
|
18
|
+
* undefined and fall back to app-token ingestion, never a thrown error.
|
|
12
19
|
*/
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
20
|
+
const node_fs_1 = require("node:fs");
|
|
21
|
+
const node_path_1 = require("node:path");
|
|
22
|
+
exports.CONFIG_DIRNAME = ".metergraph";
|
|
23
|
+
exports.CONFIG_FILENAME = "config.json";
|
|
24
|
+
exports.SUPPORTED_CONFIG_VERSION = 2;
|
|
18
25
|
const MAX_WALK_UP = 64;
|
|
19
26
|
const REMOTE_PATTERNS = [
|
|
20
27
|
/^git@github\.com:([^/]+\/[^/]+?)(\.git)?\/?$/,
|
|
@@ -23,7 +30,7 @@ const REMOTE_PATTERNS = [
|
|
|
23
30
|
];
|
|
24
31
|
/** Return 'owner/repo' from a GitHub SSH or HTTPS remote URL, or undefined
|
|
25
32
|
* if the URL isn't a recognized GitHub origin. */
|
|
26
|
-
|
|
33
|
+
function normalizeGithubRemote(url) {
|
|
27
34
|
const trimmed = url.trim();
|
|
28
35
|
for (const pattern of REMOTE_PATTERNS) {
|
|
29
36
|
const match = pattern.exec(trimmed);
|
|
@@ -35,14 +42,14 @@ export function normalizeGithubRemote(url) {
|
|
|
35
42
|
function load(path, repoRoot) {
|
|
36
43
|
let doc;
|
|
37
44
|
try {
|
|
38
|
-
doc = JSON.parse(readFileSync(path, "utf8"));
|
|
45
|
+
doc = JSON.parse((0, node_fs_1.readFileSync)(path, "utf8"));
|
|
39
46
|
}
|
|
40
47
|
catch (error) {
|
|
41
48
|
console.warn(`metergraph: found ${path} but could not read it: ${error instanceof Error ? error.message : String(error)}`);
|
|
42
49
|
return undefined;
|
|
43
50
|
}
|
|
44
|
-
if (typeof doc !== "object" || doc === null || doc.version !== SUPPORTED_CONFIG_VERSION) {
|
|
45
|
-
console.warn(`metergraph: ${path} has an unsupported schema version; ignoring (expected version ${SUPPORTED_CONFIG_VERSION})`);
|
|
51
|
+
if (typeof doc !== "object" || doc === null || (doc.version ?? exports.SUPPORTED_CONFIG_VERSION) !== exports.SUPPORTED_CONFIG_VERSION) {
|
|
52
|
+
console.warn(`metergraph: ${path} has an unsupported schema version; ignoring (expected version ${exports.SUPPORTED_CONFIG_VERSION})`);
|
|
46
53
|
return undefined;
|
|
47
54
|
}
|
|
48
55
|
const repository = doc.repository;
|
|
@@ -54,24 +61,24 @@ function load(path, repoRoot) {
|
|
|
54
61
|
}
|
|
55
62
|
/** Walk upward from appRoot looking for .metergraph/config.json. Returns
|
|
56
63
|
* undefined -- silently, the normal v1 state -- when nothing is found. */
|
|
57
|
-
|
|
64
|
+
function discoverRepoConfig(appRoot) {
|
|
58
65
|
let current;
|
|
59
66
|
try {
|
|
60
|
-
current = realpathSync(appRoot);
|
|
67
|
+
current = (0, node_fs_1.realpathSync)(appRoot);
|
|
61
68
|
}
|
|
62
69
|
catch {
|
|
63
70
|
return undefined;
|
|
64
71
|
}
|
|
65
72
|
for (let i = 0; i < MAX_WALK_UP; i += 1) {
|
|
66
|
-
const candidate = join(current, CONFIG_DIRNAME, CONFIG_FILENAME);
|
|
73
|
+
const candidate = (0, node_path_1.join)(current, exports.CONFIG_DIRNAME, exports.CONFIG_FILENAME);
|
|
67
74
|
try {
|
|
68
|
-
readFileSync(candidate);
|
|
75
|
+
(0, node_fs_1.readFileSync)(candidate);
|
|
69
76
|
return load(candidate, current);
|
|
70
77
|
}
|
|
71
78
|
catch {
|
|
72
79
|
// no config at this level; keep walking up
|
|
73
80
|
}
|
|
74
|
-
const parent = dirname(current);
|
|
81
|
+
const parent = (0, node_path_1.dirname)(current);
|
|
75
82
|
if (parent === current)
|
|
76
83
|
break;
|
|
77
84
|
current = parent;
|
|
@@ -79,15 +86,15 @@ export function discoverRepoConfig(appRoot) {
|
|
|
79
86
|
return undefined;
|
|
80
87
|
}
|
|
81
88
|
function gitDirectory(repoRoot) {
|
|
82
|
-
const marker = join(repoRoot, ".git");
|
|
89
|
+
const marker = (0, node_path_1.join)(repoRoot, ".git");
|
|
83
90
|
try {
|
|
84
|
-
if (statSync(marker).isDirectory())
|
|
91
|
+
if ((0, node_fs_1.statSync)(marker).isDirectory())
|
|
85
92
|
return marker;
|
|
86
|
-
const match = /^gitdir:\s*(.+)$/m.exec(readFileSync(marker, "utf8"));
|
|
93
|
+
const match = /^gitdir:\s*(.+)$/m.exec((0, node_fs_1.readFileSync)(marker, "utf8"));
|
|
87
94
|
const path = match?.[1];
|
|
88
95
|
if (!path)
|
|
89
96
|
return undefined;
|
|
90
|
-
return isAbsolute(path) ? path : resolve(repoRoot, path);
|
|
97
|
+
return (0, node_path_1.isAbsolute)(path) ? path : (0, node_path_1.resolve)(repoRoot, path);
|
|
91
98
|
}
|
|
92
99
|
catch {
|
|
93
100
|
return undefined;
|
|
@@ -97,7 +104,7 @@ function ownedByCurrentUser(path) {
|
|
|
97
104
|
if (typeof process.getuid !== "function")
|
|
98
105
|
return true;
|
|
99
106
|
try {
|
|
100
|
-
return statSync(path).uid === process.getuid();
|
|
107
|
+
return (0, node_fs_1.statSync)(path).uid === process.getuid();
|
|
101
108
|
}
|
|
102
109
|
catch {
|
|
103
110
|
return false;
|
|
@@ -105,7 +112,7 @@ function ownedByCurrentUser(path) {
|
|
|
105
112
|
}
|
|
106
113
|
function gitTopLevel(appRoot) {
|
|
107
114
|
try {
|
|
108
|
-
let current = realpathSync(appRoot);
|
|
115
|
+
let current = (0, node_fs_1.realpathSync)(appRoot);
|
|
109
116
|
for (let i = 0; i < MAX_WALK_UP; i += 1) {
|
|
110
117
|
const gitDir = gitDirectory(current);
|
|
111
118
|
if (gitDir) {
|
|
@@ -116,7 +123,7 @@ function gitTopLevel(appRoot) {
|
|
|
116
123
|
? current
|
|
117
124
|
: undefined;
|
|
118
125
|
}
|
|
119
|
-
const parent = dirname(current);
|
|
126
|
+
const parent = (0, node_path_1.dirname)(current);
|
|
120
127
|
if (parent === current)
|
|
121
128
|
break;
|
|
122
129
|
current = parent;
|
|
@@ -134,14 +141,14 @@ function gitOriginUrl(repoRoot) {
|
|
|
134
141
|
return undefined;
|
|
135
142
|
let commonDir = gitDir;
|
|
136
143
|
try {
|
|
137
|
-
const relative = readFileSync(join(gitDir, "commondir"), "utf8").trim();
|
|
144
|
+
const relative = (0, node_fs_1.readFileSync)((0, node_path_1.join)(gitDir, "commondir"), "utf8").trim();
|
|
138
145
|
if (relative)
|
|
139
|
-
commonDir = resolve(gitDir, relative);
|
|
146
|
+
commonDir = (0, node_path_1.resolve)(gitDir, relative);
|
|
140
147
|
}
|
|
141
148
|
catch {
|
|
142
149
|
// Normal repositories keep config directly in .git.
|
|
143
150
|
}
|
|
144
|
-
const config = readFileSync(join(commonDir, "config"), "utf8");
|
|
151
|
+
const config = (0, node_fs_1.readFileSync)((0, node_path_1.join)(commonDir, "config"), "utf8");
|
|
145
152
|
let inOrigin = false;
|
|
146
153
|
for (const line of config.split(/\r?\n/)) {
|
|
147
154
|
const section = /^\s*\[remote\s+"([^"]+)"\]\s*$/.exec(line);
|
|
@@ -167,18 +174,18 @@ function gitOriginUrl(repoRoot) {
|
|
|
167
174
|
* exist. Uses O_CREAT|O_EXCL ("wx") for an atomic create-only-if-absent; a
|
|
168
175
|
* concurrent writer (or a file that appeared between discovery and this
|
|
169
176
|
* call) always wins over us, and we simply read back whatever is there. */
|
|
170
|
-
|
|
171
|
-
const configDir = join(repoRoot, CONFIG_DIRNAME);
|
|
172
|
-
const configPath = join(configDir, CONFIG_FILENAME);
|
|
173
|
-
const payload = `${JSON.stringify({ version: SUPPORTED_CONFIG_VERSION, repository })}\n`;
|
|
177
|
+
function writeConfigAtomically(repoRoot, repository) {
|
|
178
|
+
const configDir = (0, node_path_1.join)(repoRoot, exports.CONFIG_DIRNAME);
|
|
179
|
+
const configPath = (0, node_path_1.join)(configDir, exports.CONFIG_FILENAME);
|
|
180
|
+
const payload = `${JSON.stringify({ version: exports.SUPPORTED_CONFIG_VERSION, repository })}\n`;
|
|
174
181
|
try {
|
|
175
|
-
mkdirSync(configDir, { recursive: true });
|
|
176
|
-
const fd = openSync(configPath, "wx", 0o644);
|
|
182
|
+
(0, node_fs_1.mkdirSync)(configDir, { recursive: true });
|
|
183
|
+
const fd = (0, node_fs_1.openSync)(configPath, "wx", 0o644);
|
|
177
184
|
try {
|
|
178
|
-
writeSync(fd, payload);
|
|
185
|
+
(0, node_fs_1.writeSync)(fd, payload);
|
|
179
186
|
}
|
|
180
187
|
finally {
|
|
181
|
-
closeSync(fd);
|
|
188
|
+
(0, node_fs_1.closeSync)(fd);
|
|
182
189
|
}
|
|
183
190
|
}
|
|
184
191
|
catch (error) {
|
|
@@ -191,8 +198,8 @@ export function writeConfigAtomically(repoRoot, repository) {
|
|
|
191
198
|
}
|
|
192
199
|
/** Discover an existing repo config, or detect+write one once at the git top
|
|
193
200
|
* level. Fail-open: any detection or write failure returns undefined
|
|
194
|
-
* (
|
|
195
|
-
|
|
201
|
+
* (app-token ingestion), never throws. */
|
|
202
|
+
function ensureRepoConfig(appRoot) {
|
|
196
203
|
const existing = discoverRepoConfig(appRoot);
|
|
197
204
|
if (existing)
|
|
198
205
|
return existing;
|
package/dist/session.js
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SessionManager = exports.MAX_BACKOFF_MS = exports.REFRESH_MARGIN_MS = exports.DEFAULT_TTL_MS = exports.DEFAULT_TIMEOUT_MS = void 0;
|
|
1
4
|
/**
|
|
2
|
-
*
|
|
5
|
+
* SDK 0.4+ repository-aware ingestion: app-token -> session-token exchange.
|
|
3
6
|
*
|
|
4
7
|
* The app token is sent only on POST /v1/ingest/sessions. The resulting
|
|
5
8
|
* session token is cached in memory and reused until it's within
|
|
@@ -10,30 +13,30 @@
|
|
|
10
13
|
* mode is fail-open: getToken() resolves to undefined and callers drop that
|
|
11
14
|
* batch rather than ever sending the app token to /v1/ingest.
|
|
12
15
|
*/
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
16
|
+
const failure_log_js_1 = require("./failure-log.js");
|
|
17
|
+
exports.DEFAULT_TIMEOUT_MS = 3_000;
|
|
18
|
+
exports.DEFAULT_TTL_MS = 300_000;
|
|
19
|
+
exports.REFRESH_MARGIN_MS = 30_000;
|
|
20
|
+
exports.MAX_BACKOFF_MS = 60_000;
|
|
18
21
|
function parseExpiresAt(value) {
|
|
19
22
|
if (typeof value !== "string")
|
|
20
23
|
return undefined;
|
|
21
24
|
const parsed = Date.parse(value);
|
|
22
25
|
return Number.isNaN(parsed) ? undefined : parsed;
|
|
23
26
|
}
|
|
24
|
-
|
|
27
|
+
class SessionManager {
|
|
25
28
|
appToken;
|
|
26
29
|
repository;
|
|
27
30
|
sdkVersion;
|
|
28
31
|
timeoutMs;
|
|
29
32
|
url;
|
|
30
|
-
failureLog = new FailureLogger();
|
|
33
|
+
failureLog = new failure_log_js_1.FailureLogger();
|
|
31
34
|
token;
|
|
32
35
|
expiresAt = 0;
|
|
33
36
|
stopped = false;
|
|
34
37
|
retryAt = 0;
|
|
35
38
|
backoffMs = 1_000;
|
|
36
|
-
constructor(appToken, baseUrl, repository, sdkVersion, timeoutMs = DEFAULT_TIMEOUT_MS) {
|
|
39
|
+
constructor(appToken, baseUrl, repository, sdkVersion, timeoutMs = exports.DEFAULT_TIMEOUT_MS) {
|
|
37
40
|
this.appToken = appToken;
|
|
38
41
|
this.repository = repository;
|
|
39
42
|
this.sdkVersion = sdkVersion;
|
|
@@ -43,7 +46,7 @@ export class SessionManager {
|
|
|
43
46
|
async getToken() {
|
|
44
47
|
if (this.stopped)
|
|
45
48
|
return undefined;
|
|
46
|
-
if (this.token !== undefined && Date.now() < this.expiresAt - REFRESH_MARGIN_MS) {
|
|
49
|
+
if (this.token !== undefined && Date.now() < this.expiresAt - exports.REFRESH_MARGIN_MS) {
|
|
47
50
|
return this.token;
|
|
48
51
|
}
|
|
49
52
|
if (Date.now() < this.retryAt)
|
|
@@ -89,7 +92,7 @@ export class SessionManager {
|
|
|
89
92
|
this.markFailed();
|
|
90
93
|
return;
|
|
91
94
|
}
|
|
92
|
-
const expiresAt = parseExpiresAt(doc?.expires_at) ?? Date.now() + DEFAULT_TTL_MS;
|
|
95
|
+
const expiresAt = parseExpiresAt(doc?.expires_at) ?? Date.now() + exports.DEFAULT_TTL_MS;
|
|
93
96
|
if (!this.stopped) {
|
|
94
97
|
this.token = token;
|
|
95
98
|
this.expiresAt = expiresAt;
|
|
@@ -104,6 +107,7 @@ export class SessionManager {
|
|
|
104
107
|
}
|
|
105
108
|
markFailed() {
|
|
106
109
|
this.retryAt = Date.now() + this.backoffMs;
|
|
107
|
-
this.backoffMs = Math.min(this.backoffMs * 2, MAX_BACKOFF_MS);
|
|
110
|
+
this.backoffMs = Math.min(this.backoffMs * 2, exports.MAX_BACKOFF_MS);
|
|
108
111
|
}
|
|
109
112
|
}
|
|
113
|
+
exports.SessionManager = SessionManager;
|
package/dist/template.js
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.scrub = scrub;
|
|
4
|
+
exports.templateHash = templateHash;
|
|
1
5
|
const sensitive = new Set([
|
|
2
6
|
"api-key",
|
|
3
7
|
"api_key",
|
|
@@ -15,7 +19,7 @@ const sensitive = new Set([
|
|
|
15
19
|
"token",
|
|
16
20
|
"x-api-key",
|
|
17
21
|
]);
|
|
18
|
-
|
|
22
|
+
function scrub(value) {
|
|
19
23
|
if (Array.isArray(value))
|
|
20
24
|
return value.map(scrub);
|
|
21
25
|
if (value && typeof value === "object") {
|
|
@@ -52,6 +56,6 @@ function fnv1a(value) {
|
|
|
52
56
|
}
|
|
53
57
|
return hash.toString(16).padStart(16, "0");
|
|
54
58
|
}
|
|
55
|
-
|
|
59
|
+
function templateHash(request) {
|
|
56
60
|
return fnv1a(JSON.stringify(normalized(scrub(request))));
|
|
57
61
|
}
|
package/dist/track.js
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.track = track;
|
|
4
|
+
const context_js_1 = require("./context.js");
|
|
5
|
+
function track(nameOrFn, maybeFn) {
|
|
3
6
|
const fn = typeof nameOrFn === "string" ? maybeFn : nameOrFn;
|
|
4
7
|
if (typeof fn !== "function")
|
|
5
8
|
throw new TypeError("track requires a function");
|
|
6
9
|
const funcName = typeof nameOrFn === "string" ? nameOrFn : fn.name || "<anonymous>";
|
|
7
10
|
const wrapped = function (...args) {
|
|
8
|
-
return runWithContext({ ...contextSnapshot(), funcName }, () => fn.apply(this, args));
|
|
11
|
+
return (0, context_js_1.runWithContext)({ ...(0, context_js_1.contextSnapshot)(), funcName }, () => fn.apply(this, args));
|
|
9
12
|
};
|
|
10
13
|
return wrapped;
|
|
11
14
|
}
|