metergraph 0.3.2 → 0.4.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 CHANGED
@@ -86,7 +86,12 @@ required and throws if missing so a caller error surfaces immediately
86
86
  instead of an `undefined` model reaching a provider call.
87
87
 
88
88
  Set `METERGRAPH_APP_TOKEN`; `METERGRAPH_INGEST_URL` is only needed to override
89
- the hosted HTTPS endpoint. SDK 0.3 captures the scrubbed provider request and a
89
+ the hosted HTTPS endpoint. SDK 0.4 automatically reads the GitHub `origin` on
90
+ first initialization and creates `.metergraph/config.json` at the repository
91
+ root if absent. Commit this non-secret file so production can use
92
+ repository-aware ingest without Git metadata. Existing config is authoritative
93
+ and never overwritten; when discovery or creation is unavailable, the SDK
94
+ continues with protocol v1. SDK 0.4 captures the scrubbed provider request and a
90
95
  normalized response envelope, including assistant content and tool calls, by
91
96
  default. Provider credentials and transport headers are removed. Request and
92
97
  response are each limited to 100 KiB of UTF-8 with an explicit truncation
@@ -134,7 +139,7 @@ import * as mg from "metergraph". wrap() returns the same client and
134
139
  initializes itself from the environment: METERGRAPH_APP_TOKEN is required
135
140
  (capture is silently off without it) and METERGRAPH_INGEST_URL is only for
136
141
  self-hosted servers. Add both to .env.example, and never commit a real token.
137
- SDK 0.3 captures scrubbed provider requests and normalized responses by
142
+ SDK 0.4 captures scrubbed provider requests and normalized responses by
138
143
  default for the hosted dashboard; use METERGRAPH_CAPTURE_TEXT=0 or
139
144
  captureText: false around sensitive operations. Provider credentials and
140
145
  transport headers must never be captured. Capture is fail-open, so do not
package/dist/capture.d.ts CHANGED
@@ -4,6 +4,7 @@ export interface RuntimeOptions {
4
4
  captureText: boolean;
5
5
  redact?: (text: string, kind: "request" | "response") => string;
6
6
  appRoot: string;
7
+ repoRoot?: string;
7
8
  skipFrames: string[];
8
9
  environment?: string;
9
10
  textMaxBytes: number;
@@ -12,6 +13,7 @@ interface Frame {
12
13
  m: string;
13
14
  f: string;
14
15
  l: number;
16
+ p?: string;
15
17
  }
16
18
  export interface CallState {
17
19
  provider: string;
package/dist/capture.js CHANGED
@@ -376,7 +376,7 @@ const SDK_DIR = (() => {
376
376
  return "metergraph-sdk-dir-unavailable";
377
377
  }
378
378
  })();
379
- function frames(stack, appRoot, skip) {
379
+ function frames(stack, appRoot, skip, repoRoot) {
380
380
  if (!stack)
381
381
  return [];
382
382
  const result = [];
@@ -389,11 +389,17 @@ function frames(stack, appRoot, skip) {
389
389
  continue;
390
390
  if (["node_modules", "node:internal", SDK_DIR, ...skip].some((value) => file.includes(value)))
391
391
  continue;
392
- result.push({
392
+ const entry = {
393
393
  m: file.slice(file.indexOf(appRoot) + appRoot.length).replace(/^\//, ""),
394
394
  f: fn,
395
395
  l: Number(lineNo),
396
- });
396
+ };
397
+ const normalizedFile = file.replaceAll("\\", "/").replace(/^file:\/\//, "");
398
+ const normalizedRepoRoot = repoRoot?.replaceAll("\\", "/").replace(/\/$/, "");
399
+ if (normalizedRepoRoot && normalizedFile.startsWith(`${normalizedRepoRoot}/`)) {
400
+ entry.p = normalizedFile.slice(normalizedRepoRoot.length + 1);
401
+ }
402
+ result.push(entry);
397
403
  if (result.length === 5)
398
404
  break;
399
405
  }
@@ -414,7 +420,7 @@ export class CaptureRuntime {
414
420
  context,
415
421
  started: performance.now(),
416
422
  ts: new Date().toISOString(),
417
- frames: frames(stack, this.options.appRoot, this.options.skipFrames),
423
+ frames: frames(stack, this.options.appRoot, this.options.skipFrames, this.options.repoRoot),
418
424
  traceId: context.traceId ?? randomBytes(16).toString("hex"),
419
425
  spanId: randomBytes(8).toString("hex"),
420
426
  parentSpanId: context.parentSpanId,
package/dist/index.js CHANGED
@@ -1,14 +1,18 @@
1
1
  import { CaptureRuntime } from "./capture.js";
2
2
  import { ConfigPoller } from "./config.js";
3
3
  import { contextSnapshot, route, setSession, setTags, trace, } from "./context.js";
4
+ import { ensureRepoConfig } from "./repo-config.js";
5
+ import { SessionManager } from "./session.js";
4
6
  import { track } from "./track.js";
5
7
  import { Transport } from "./transport.js";
8
+ import { SDK_VERSION } from "./version.js";
6
9
  import { setCaptureRuntime, wrap as wrapProvider } from "./wrap.js";
7
10
  import { createVercelAISDKMiddleware, } from "./vercel-ai.js";
8
11
  let initialized = false;
9
12
  let warnedNoToken = false;
10
13
  let transport;
11
14
  let config;
15
+ let sessionManager;
12
16
  export const DEFAULT_INGEST_URL = "https://d2xus7mp8zdv6t.cloudfront.net";
13
17
  function env(name) {
14
18
  return typeof process === "undefined" ? undefined : process.env[name];
@@ -38,16 +42,23 @@ export function init(options = {}) {
38
42
  }
39
43
  initialized = true;
40
44
  try {
45
+ const appRoot = options.appRoot ?? (typeof process === "undefined" ? "" : process.cwd());
46
+ const repoConfig = ensureRepoConfig(appRoot);
47
+ sessionManager = repoConfig
48
+ ? new SessionManager(token, ingestUrl, repoConfig.repository, SDK_VERSION)
49
+ : undefined;
41
50
  transport = new Transport(token, ingestUrl, {
42
51
  queueSize: options.queueSize ?? Number(env("METERGRAPH_QUEUE_SIZE") ?? 2_000),
43
52
  batchSize: options.batchSize ?? Number(env("METERGRAPH_BATCH_SIZE") ?? 100),
44
53
  flushMs: options.flushMs ?? Number(env("METERGRAPH_FLUSH_MS") ?? 5_000),
45
54
  mode: options.transport ?? "auto",
55
+ session: sessionManager,
46
56
  });
47
57
  setCaptureRuntime(new CaptureRuntime(transport, {
48
58
  captureText: options.captureText ?? envBool("METERGRAPH_CAPTURE_TEXT", true),
49
59
  redact: options.redact,
50
- appRoot: options.appRoot ?? (typeof process === "undefined" ? "" : process.cwd()),
60
+ appRoot,
61
+ repoRoot: repoConfig?.repoRoot,
51
62
  skipFrames: options.skipFrames ?? [],
52
63
  environment: options.environment ?? env("METERGRAPH_ENV"),
53
64
  textMaxBytes: Math.min(100 * 1024, Math.max(1, Number(env("METERGRAPH_TEXT_MAX_BYTES") ?? 100 * 1024))),
@@ -57,6 +68,7 @@ export function init(options = {}) {
57
68
  catch {
58
69
  transport = undefined;
59
70
  config = undefined;
71
+ sessionManager = undefined;
60
72
  setCaptureRuntime();
61
73
  console.warn("Metergraph initialization failed; application is running uninstrumented");
62
74
  }
@@ -116,6 +128,8 @@ export async function shutdown() {
116
128
  config = undefined;
117
129
  await transport?.shutdown();
118
130
  transport = undefined;
131
+ sessionManager?.stop();
132
+ sessionManager = undefined;
119
133
  setCaptureRuntime();
120
134
  }
121
135
  export function wrapHandler(handler) {
@@ -0,0 +1,22 @@
1
+ export declare const CONFIG_DIRNAME = ".metergraph";
2
+ export declare const CONFIG_FILENAME = "config.json";
3
+ export declare const SUPPORTED_CONFIG_VERSION = 2;
4
+ export interface RepoConfig {
5
+ repository: string;
6
+ repoRoot: string;
7
+ }
8
+ /** Return 'owner/repo' from a GitHub SSH or HTTPS remote URL, or undefined
9
+ * if the URL isn't a recognized GitHub origin. */
10
+ export declare function normalizeGithubRemote(url: string): string | undefined;
11
+ /** Walk upward from appRoot looking for .metergraph/config.json. Returns
12
+ * undefined -- silently, the normal v1 state -- when nothing is found. */
13
+ export declare function discoverRepoConfig(appRoot: string): RepoConfig | undefined;
14
+ /** Create .metergraph/config.json if -- and only if -- it doesn't already
15
+ * exist. Uses O_CREAT|O_EXCL ("wx") for an atomic create-only-if-absent; a
16
+ * concurrent writer (or a file that appeared between discovery and this
17
+ * call) always wins over us, and we simply read back whatever is there. */
18
+ export declare function writeConfigAtomically(repoRoot: string, repository: string): RepoConfig | undefined;
19
+ /** Discover an existing repo config, or detect+write one once at the git top
20
+ * level. Fail-open: any detection or write failure returns undefined
21
+ * (legacy protocol v1), never throws. */
22
+ export declare function ensureRepoConfig(appRoot: string): RepoConfig | undefined;
@@ -0,0 +1,214 @@
1
+ /**
2
+ * Repository-aware ingest protocol v2: discover, detect, and write
3
+ * .metergraph/config.json.
4
+ *
5
+ * Discovery is purely file-based (never shells out to git), so a committed
6
+ * config is honored in production without needing a .git directory at all.
7
+ * Detection + write only ever runs when discovery finds nothing: it shells
8
+ * out to git to find the repo's top level and GitHub origin, then writes the
9
+ * config there exactly once. An existing file is always authoritative and is
10
+ * never overwritten. Every failure mode here is fail-open -- callers get
11
+ * undefined and fall back to legacy protocol v1, never a thrown error.
12
+ */
13
+ import { closeSync, mkdirSync, openSync, readFileSync, realpathSync, statSync, writeSync } from "node:fs";
14
+ import { dirname, isAbsolute, join, resolve } from "node:path";
15
+ export const CONFIG_DIRNAME = ".metergraph";
16
+ export const CONFIG_FILENAME = "config.json";
17
+ export const SUPPORTED_CONFIG_VERSION = 2;
18
+ const MAX_WALK_UP = 64;
19
+ const REMOTE_PATTERNS = [
20
+ /^git@github\.com:([^/]+\/[^/]+?)(\.git)?\/?$/,
21
+ /^https:\/\/github\.com\/([^/]+\/[^/]+?)(\.git)?\/?$/,
22
+ /^ssh:\/\/git@github\.com\/([^/]+\/[^/]+?)(\.git)?\/?$/,
23
+ ];
24
+ /** Return 'owner/repo' from a GitHub SSH or HTTPS remote URL, or undefined
25
+ * if the URL isn't a recognized GitHub origin. */
26
+ export function normalizeGithubRemote(url) {
27
+ const trimmed = url.trim();
28
+ for (const pattern of REMOTE_PATTERNS) {
29
+ const match = pattern.exec(trimmed);
30
+ if (match)
31
+ return match[1];
32
+ }
33
+ return undefined;
34
+ }
35
+ function load(path, repoRoot) {
36
+ let doc;
37
+ try {
38
+ doc = JSON.parse(readFileSync(path, "utf8"));
39
+ }
40
+ catch (error) {
41
+ console.warn(`metergraph: found ${path} but could not read it: ${error instanceof Error ? error.message : String(error)}`);
42
+ return undefined;
43
+ }
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})`);
46
+ return undefined;
47
+ }
48
+ const repository = doc.repository;
49
+ if (typeof repository !== "string" || !repository.includes("/")) {
50
+ console.warn(`metergraph: ${path} is missing a valid 'repository' field; ignoring`);
51
+ return undefined;
52
+ }
53
+ return { repository, repoRoot };
54
+ }
55
+ /** Walk upward from appRoot looking for .metergraph/config.json. Returns
56
+ * undefined -- silently, the normal v1 state -- when nothing is found. */
57
+ export function discoverRepoConfig(appRoot) {
58
+ let current;
59
+ try {
60
+ current = realpathSync(appRoot);
61
+ }
62
+ catch {
63
+ return undefined;
64
+ }
65
+ for (let i = 0; i < MAX_WALK_UP; i += 1) {
66
+ const candidate = join(current, CONFIG_DIRNAME, CONFIG_FILENAME);
67
+ try {
68
+ readFileSync(candidate);
69
+ return load(candidate, current);
70
+ }
71
+ catch {
72
+ // no config at this level; keep walking up
73
+ }
74
+ const parent = dirname(current);
75
+ if (parent === current)
76
+ break;
77
+ current = parent;
78
+ }
79
+ return undefined;
80
+ }
81
+ function gitDirectory(repoRoot) {
82
+ const marker = join(repoRoot, ".git");
83
+ try {
84
+ if (statSync(marker).isDirectory())
85
+ return marker;
86
+ const match = /^gitdir:\s*(.+)$/m.exec(readFileSync(marker, "utf8"));
87
+ const path = match?.[1];
88
+ if (!path)
89
+ return undefined;
90
+ return isAbsolute(path) ? path : resolve(repoRoot, path);
91
+ }
92
+ catch {
93
+ return undefined;
94
+ }
95
+ }
96
+ function ownedByCurrentUser(path) {
97
+ if (typeof process.getuid !== "function")
98
+ return true;
99
+ try {
100
+ return statSync(path).uid === process.getuid();
101
+ }
102
+ catch {
103
+ return false;
104
+ }
105
+ }
106
+ function gitTopLevel(appRoot) {
107
+ try {
108
+ let current = realpathSync(appRoot);
109
+ for (let i = 0; i < MAX_WALK_UP; i += 1) {
110
+ const gitDir = gitDirectory(current);
111
+ if (gitDir) {
112
+ // Match Git's safe-directory default: never trust repository metadata
113
+ // owned by another Unix user. Unsupported platforms retain v1's
114
+ // existing fail-open behavior.
115
+ return ownedByCurrentUser(current) && ownedByCurrentUser(gitDir)
116
+ ? current
117
+ : undefined;
118
+ }
119
+ const parent = dirname(current);
120
+ if (parent === current)
121
+ break;
122
+ current = parent;
123
+ }
124
+ }
125
+ catch {
126
+ // Fall through to the normal v1 path.
127
+ }
128
+ return undefined;
129
+ }
130
+ function gitOriginUrl(repoRoot) {
131
+ try {
132
+ const gitDir = gitDirectory(repoRoot);
133
+ if (!gitDir)
134
+ return undefined;
135
+ let commonDir = gitDir;
136
+ try {
137
+ const relative = readFileSync(join(gitDir, "commondir"), "utf8").trim();
138
+ if (relative)
139
+ commonDir = resolve(gitDir, relative);
140
+ }
141
+ catch {
142
+ // Normal repositories keep config directly in .git.
143
+ }
144
+ const config = readFileSync(join(commonDir, "config"), "utf8");
145
+ let inOrigin = false;
146
+ for (const line of config.split(/\r?\n/)) {
147
+ const section = /^\s*\[remote\s+"([^"]+)"\]\s*$/.exec(line);
148
+ if (section) {
149
+ inOrigin = section[1] === "origin";
150
+ continue;
151
+ }
152
+ if (/^\s*\[/.test(line))
153
+ inOrigin = false;
154
+ if (inOrigin) {
155
+ const url = /^\s*url\s*=\s*(.+?)\s*$/.exec(line);
156
+ if (url)
157
+ return url[1];
158
+ }
159
+ }
160
+ }
161
+ catch {
162
+ // Missing or unreadable Git metadata is the normal production fallback.
163
+ }
164
+ return undefined;
165
+ }
166
+ /** Create .metergraph/config.json if -- and only if -- it doesn't already
167
+ * exist. Uses O_CREAT|O_EXCL ("wx") for an atomic create-only-if-absent; a
168
+ * concurrent writer (or a file that appeared between discovery and this
169
+ * call) always wins over us, and we simply read back whatever is there. */
170
+ export function writeConfigAtomically(repoRoot, repository) {
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`;
174
+ try {
175
+ mkdirSync(configDir, { recursive: true });
176
+ const fd = openSync(configPath, "wx", 0o644);
177
+ try {
178
+ writeSync(fd, payload);
179
+ }
180
+ finally {
181
+ closeSync(fd);
182
+ }
183
+ }
184
+ catch (error) {
185
+ if (error.code !== "EEXIST") {
186
+ console.warn(`metergraph: could not write ${configPath}: ${error instanceof Error ? error.message : String(error)}`);
187
+ return undefined;
188
+ }
189
+ }
190
+ return load(configPath, repoRoot);
191
+ }
192
+ /** Discover an existing repo config, or detect+write one once at the git top
193
+ * level. Fail-open: any detection or write failure returns undefined
194
+ * (legacy protocol v1), never throws. */
195
+ export function ensureRepoConfig(appRoot) {
196
+ const existing = discoverRepoConfig(appRoot);
197
+ if (existing)
198
+ return existing;
199
+ try {
200
+ const repoRoot = gitTopLevel(appRoot);
201
+ if (!repoRoot)
202
+ return undefined;
203
+ const origin = gitOriginUrl(repoRoot);
204
+ if (!origin)
205
+ return undefined;
206
+ const repository = normalizeGithubRemote(origin);
207
+ if (!repository)
208
+ return undefined;
209
+ return writeConfigAtomically(repoRoot, repository);
210
+ }
211
+ catch {
212
+ return undefined;
213
+ }
214
+ }
@@ -0,0 +1,23 @@
1
+ export declare const DEFAULT_TIMEOUT_MS = 3000;
2
+ export declare const DEFAULT_TTL_MS = 300000;
3
+ export declare const REFRESH_MARGIN_MS = 30000;
4
+ export declare const MAX_BACKOFF_MS = 60000;
5
+ export declare class SessionManager {
6
+ private readonly appToken;
7
+ private readonly repository;
8
+ private readonly sdkVersion;
9
+ private readonly timeoutMs;
10
+ private readonly url;
11
+ private readonly failureLog;
12
+ private token;
13
+ private expiresAt;
14
+ private stopped;
15
+ private retryAt;
16
+ private backoffMs;
17
+ constructor(appToken: string, baseUrl: string, repository: string, sdkVersion: string, timeoutMs?: number);
18
+ getToken(): Promise<string | undefined>;
19
+ invalidate(): void;
20
+ stop(): void;
21
+ private exchange;
22
+ private markFailed;
23
+ }
@@ -0,0 +1,109 @@
1
+ /**
2
+ * Repository-aware ingest protocol v2: app-token -> session-token exchange.
3
+ *
4
+ * The app token is sent only on POST /v1/ingest/sessions. The resulting
5
+ * session token is cached in memory and reused until it's within
6
+ * REFRESH_MARGIN_MS of expiry, at which point the next getToken() call
7
+ * transparently re-exchanges it. Exchanges happen lazily, off the caller's
8
+ * request path (Transport.deliver already runs asynchronously), so a slow
9
+ * round trip here adds no latency to the customer's LLM call. Every failure
10
+ * mode is fail-open: getToken() resolves to undefined and callers drop that
11
+ * batch rather than ever sending the app token to /v1/ingest.
12
+ */
13
+ import { FailureLogger } from "./failure-log.js";
14
+ export const DEFAULT_TIMEOUT_MS = 3_000;
15
+ export const DEFAULT_TTL_MS = 300_000;
16
+ export const REFRESH_MARGIN_MS = 30_000;
17
+ export const MAX_BACKOFF_MS = 60_000;
18
+ function parseExpiresAt(value) {
19
+ if (typeof value !== "string")
20
+ return undefined;
21
+ const parsed = Date.parse(value);
22
+ return Number.isNaN(parsed) ? undefined : parsed;
23
+ }
24
+ export class SessionManager {
25
+ appToken;
26
+ repository;
27
+ sdkVersion;
28
+ timeoutMs;
29
+ url;
30
+ failureLog = new FailureLogger();
31
+ token;
32
+ expiresAt = 0;
33
+ stopped = false;
34
+ retryAt = 0;
35
+ backoffMs = 1_000;
36
+ constructor(appToken, baseUrl, repository, sdkVersion, timeoutMs = DEFAULT_TIMEOUT_MS) {
37
+ this.appToken = appToken;
38
+ this.repository = repository;
39
+ this.sdkVersion = sdkVersion;
40
+ this.timeoutMs = timeoutMs;
41
+ this.url = `${baseUrl.replace(/\/$/, "")}/v1/ingest/sessions`;
42
+ }
43
+ async getToken() {
44
+ if (this.stopped)
45
+ return undefined;
46
+ if (this.token !== undefined && Date.now() < this.expiresAt - REFRESH_MARGIN_MS) {
47
+ return this.token;
48
+ }
49
+ if (Date.now() < this.retryAt)
50
+ return undefined;
51
+ await this.exchange();
52
+ return this.stopped ? undefined : this.token;
53
+ }
54
+ invalidate() {
55
+ this.token = undefined;
56
+ this.expiresAt = 0;
57
+ this.retryAt = 0;
58
+ this.backoffMs = 1_000;
59
+ }
60
+ stop() {
61
+ this.stopped = true;
62
+ this.token = undefined;
63
+ this.expiresAt = 0;
64
+ }
65
+ async exchange() {
66
+ try {
67
+ const response = await fetch(this.url, {
68
+ method: "POST",
69
+ headers: {
70
+ Authorization: `Bearer ${this.appToken}`,
71
+ "Content-Type": "application/json",
72
+ },
73
+ body: JSON.stringify({
74
+ protocol_version: 2,
75
+ repository: this.repository,
76
+ sdk_version: this.sdkVersion,
77
+ }),
78
+ signal: AbortSignal.timeout(this.timeoutMs),
79
+ });
80
+ if (!response.ok) {
81
+ this.failureLog.report("session_exchange_error", `session exchange to ${this.url} failed with HTTP ${response.status}`);
82
+ this.markFailed();
83
+ return;
84
+ }
85
+ const doc = (await response.json());
86
+ const token = doc?.session_token;
87
+ if (typeof token !== "string" || !token) {
88
+ this.failureLog.report("session_exchange_error", `session exchange to ${this.url} returned no session_token`);
89
+ this.markFailed();
90
+ return;
91
+ }
92
+ const expiresAt = parseExpiresAt(doc?.expires_at) ?? Date.now() + DEFAULT_TTL_MS;
93
+ if (!this.stopped) {
94
+ this.token = token;
95
+ this.expiresAt = expiresAt;
96
+ this.retryAt = 0;
97
+ this.backoffMs = 1_000;
98
+ }
99
+ }
100
+ catch (error) {
101
+ this.failureLog.report("session_exchange_error", `session exchange to ${this.url} failed: ${error instanceof Error ? error.message : String(error)}`);
102
+ this.markFailed();
103
+ }
104
+ }
105
+ markFailed() {
106
+ this.retryAt = Date.now() + this.backoffMs;
107
+ this.backoffMs = Math.min(this.backoffMs * 2, MAX_BACKOFF_MS);
108
+ }
109
+ }
@@ -1,3 +1,4 @@
1
+ import type { SessionManager } from "./session.js";
1
2
  export type TransportMode = "auto" | "background" | "buffered";
2
3
  export type WaitUntil = (promise: Promise<unknown>) => void;
3
4
  export declare const MAX_BATCH_BYTES: number;
@@ -6,6 +7,7 @@ export interface TransportOptions {
6
7
  batchSize?: number;
7
8
  flushMs?: number;
8
9
  mode?: TransportMode;
10
+ session?: SessionManager;
9
11
  }
10
12
  export declare class Transport {
11
13
  private readonly token;
@@ -24,6 +26,7 @@ export declare class Transport {
24
26
  private retryAt;
25
27
  private backoffMs;
26
28
  private readonly failureLog;
29
+ private readonly session?;
27
30
  constructor(token: string, baseUrl: string, options?: TransportOptions);
28
31
  bindWaitUntil(value: {
29
32
  waitUntil(promise: Promise<unknown>): void;
package/dist/transport.js CHANGED
@@ -30,9 +30,11 @@ export class Transport {
30
30
  retryAt = 0;
31
31
  backoffMs = 1_000;
32
32
  failureLog = new FailureLogger();
33
+ session;
33
34
  constructor(token, baseUrl, options = {}) {
34
35
  this.token = token;
35
36
  this.baseUrl = baseUrl;
37
+ this.session = options.session;
36
38
  this.queueSize = Math.max(1, options.queueSize ?? 2_000);
37
39
  this.batchSize = Math.max(1, Math.min(1_000, options.batchSize ?? 100));
38
40
  this.mode = options.mode === "auto" || !options.mode ? detectedMode() : options.mode;
@@ -99,6 +101,11 @@ export class Transport {
99
101
  this.dropped += rows.length;
100
102
  return;
101
103
  }
104
+ const token = this.session ? await this.session.getToken() : this.token;
105
+ if (token === undefined) {
106
+ this.dropped += rows.length;
107
+ return;
108
+ }
102
109
  const encoded = new TextEncoder().encode(JSON.stringify({
103
110
  schema_version: 1,
104
111
  rows,
@@ -121,7 +128,7 @@ export class Transport {
121
128
  const response = await fetch(`${this.baseUrl.replace(/\/$/, "")}/v1/ingest`, {
122
129
  method: "POST",
123
130
  headers: {
124
- Authorization: `Bearer ${this.token}`,
131
+ Authorization: `Bearer ${token}`,
125
132
  "Content-Type": "application/json",
126
133
  ...(compressed && body !== raw ? { "Content-Encoding": "gzip" } : {}),
127
134
  },
@@ -129,6 +136,11 @@ export class Transport {
129
136
  keepalive: this.mode === "buffered" && body.byteLength <= 64 * 1024,
130
137
  });
131
138
  if (response.status === 401 || response.status === 403) {
139
+ if (this.session) {
140
+ this.session.invalidate();
141
+ this.dropped += rows.length;
142
+ return;
143
+ }
132
144
  this.fatal = true;
133
145
  console.warn("Metergraph authentication failed; capture disabled for this process");
134
146
  return;
package/dist/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const SDK_VERSION = "0.3.2";
1
+ export declare const SDK_VERSION = "0.4.0";
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const SDK_VERSION = "0.3.2";
1
+ export const SDK_VERSION = "0.4.0";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "metergraph",
3
- "version": "0.3.2",
3
+ "version": "0.4.0",
4
4
  "description": "Fire-and-forget LLM spend capture for Metergraph",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",