@briefcase-ai/auto 0.0.0 → 5.0.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 +103 -0
- package/dist/activate.d.ts +35 -0
- package/dist/activate.js +108 -0
- package/dist/auto.d.ts +92 -0
- package/dist/auto.js +350 -0
- package/dist/context.d.ts +49 -0
- package/dist/context.js +82 -0
- package/dist/detect.d.ts +26 -0
- package/dist/detect.js +127 -0
- package/dist/esbuild.d.ts +40 -0
- package/dist/esbuild.js +55 -0
- package/dist/exporters.d.ts +196 -0
- package/dist/exporters.js +405 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.js +14 -0
- package/dist/instrument/anthropic.d.ts +40 -0
- package/dist/instrument/anthropic.js +178 -0
- package/dist/instrument/bedrock.d.ts +53 -0
- package/dist/instrument/bedrock.js +256 -0
- package/dist/instrument/core.d.ts +257 -0
- package/dist/instrument/core.js +772 -0
- package/dist/instrument/google-genai.d.ts +71 -0
- package/dist/instrument/google-genai.js +289 -0
- package/dist/instrument/openai.d.ts +68 -0
- package/dist/instrument/openai.js +293 -0
- package/dist/instrument/stainless.d.ts +41 -0
- package/dist/instrument/stainless.js +151 -0
- package/dist/loader.d.ts +79 -0
- package/dist/loader.js +202 -0
- package/dist/observe.d.ts +60 -0
- package/dist/observe.js +246 -0
- package/dist/pipeline.d.ts +48 -0
- package/dist/pipeline.js +155 -0
- package/dist/record.d.ts +54 -0
- package/dist/record.js +55 -0
- package/dist/register.d.ts +9 -0
- package/dist/register.js +12 -0
- package/dist/state.d.ts +25 -0
- package/dist/state.js +29 -0
- package/dist/util.d.ts +51 -0
- package/dist/util.js +198 -0
- package/package.json +84 -1
package/dist/pipeline.js
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The bounded export queue between instrumented calls and the exporter.
|
|
3
|
+
*
|
|
4
|
+
* Records are queued in memory (default 10 000; when full, new records are
|
|
5
|
+
* dropped and counted) and sent in batches. Console, file and memory
|
|
6
|
+
* exporters drain on the next turn of the event loop; HTTP and OTLP drain
|
|
7
|
+
* when a batch fills or every `flushIntervalMs`. Every timer is `unref()`ed,
|
|
8
|
+
* so the queue never keeps a process alive. Records still queued when the
|
|
9
|
+
* event loop empties are sent from a `beforeExit` hook; SIGTERM / SIGINT
|
|
10
|
+
* flush (for up to 2 s) before the default signal action runs; `exit` writes
|
|
11
|
+
* what it still can synchronously.
|
|
12
|
+
*/
|
|
13
|
+
import { debug, describeError, warn } from "./util.js";
|
|
14
|
+
export class Pipeline {
|
|
15
|
+
exporter;
|
|
16
|
+
maxQueue;
|
|
17
|
+
queue = [];
|
|
18
|
+
exported = 0;
|
|
19
|
+
dropped = 0;
|
|
20
|
+
failedBatches = 0;
|
|
21
|
+
chain = Promise.resolve();
|
|
22
|
+
immediate = null;
|
|
23
|
+
interval = null;
|
|
24
|
+
warnedFull = false;
|
|
25
|
+
closed = false;
|
|
26
|
+
constructor(exporter, maxQueue = 10_000) {
|
|
27
|
+
this.exporter = exporter;
|
|
28
|
+
this.maxQueue = maxQueue;
|
|
29
|
+
}
|
|
30
|
+
enqueue(record) {
|
|
31
|
+
if (this.closed) {
|
|
32
|
+
this.dropped++;
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
if (this.queue.length >= this.maxQueue) {
|
|
36
|
+
this.dropped++;
|
|
37
|
+
if (!this.warnedFull) {
|
|
38
|
+
this.warnedFull = true;
|
|
39
|
+
warn(`export queue full (${this.maxQueue} records); dropping new records. See exportStats().dropped.`);
|
|
40
|
+
}
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
this.warnedFull = false;
|
|
44
|
+
if (this.exporter.immediate && this.exporter.exportSync) {
|
|
45
|
+
try {
|
|
46
|
+
this.exporter.exportSync([record]);
|
|
47
|
+
this.exported++;
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
catch (error) {
|
|
51
|
+
debug(`immediate export failed, queued instead: ${describeError(error)}`);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
this.queue.push(record);
|
|
55
|
+
this.schedule();
|
|
56
|
+
}
|
|
57
|
+
schedule() {
|
|
58
|
+
const { batchSize, flushIntervalMs } = this.exporter;
|
|
59
|
+
if (flushIntervalMs === 0 || this.queue.length >= batchSize) {
|
|
60
|
+
if (this.immediate === null) {
|
|
61
|
+
this.immediate = setImmediate(() => {
|
|
62
|
+
this.immediate = null;
|
|
63
|
+
void this.flush(false);
|
|
64
|
+
});
|
|
65
|
+
this.immediate.unref();
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
if (flushIntervalMs > 0 && this.interval === null) {
|
|
69
|
+
this.interval = setInterval(() => {
|
|
70
|
+
if (this.queue.length > 0)
|
|
71
|
+
void this.flush(false);
|
|
72
|
+
}, flushIntervalMs);
|
|
73
|
+
this.interval.unref();
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
/** Sends everything queued. `final` skips backoff waits (shutdown / exit). */
|
|
77
|
+
flush(final = false) {
|
|
78
|
+
this.chain = this.chain.then(() => this.drain(final)).catch((error) => {
|
|
79
|
+
debug(`drain failed: ${describeError(error)}`);
|
|
80
|
+
});
|
|
81
|
+
return this.chain;
|
|
82
|
+
}
|
|
83
|
+
async drain(final) {
|
|
84
|
+
const size = Math.max(1, this.exporter.batchSize === 1 ? Number.MAX_SAFE_INTEGER : this.exporter.batchSize);
|
|
85
|
+
while (this.queue.length > 0) {
|
|
86
|
+
const batch = this.queue.splice(0, size);
|
|
87
|
+
let result;
|
|
88
|
+
try {
|
|
89
|
+
result = await this.exporter.exportBatch(batch, final);
|
|
90
|
+
}
|
|
91
|
+
catch (error) {
|
|
92
|
+
warn(`${this.exporter.kind} exporter failed: ${describeError(error)}`);
|
|
93
|
+
result = "retry";
|
|
94
|
+
}
|
|
95
|
+
if (result === "sent") {
|
|
96
|
+
this.exported += batch.length;
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
this.failedBatches++;
|
|
100
|
+
if (result === "rejected") {
|
|
101
|
+
this.dropped += batch.length;
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
// Transient failure: keep the batch (oldest dropped past the bound) for the next flush.
|
|
105
|
+
this.queue.unshift(...batch);
|
|
106
|
+
const overflow = this.queue.length - this.maxQueue;
|
|
107
|
+
if (overflow > 0) {
|
|
108
|
+
this.queue.splice(0, overflow);
|
|
109
|
+
this.dropped += overflow;
|
|
110
|
+
}
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
/** Synchronous last-chance export from the `exit` hook. */
|
|
115
|
+
drainSync() {
|
|
116
|
+
if (this.queue.length === 0)
|
|
117
|
+
return;
|
|
118
|
+
if (this.exporter.exportSync) {
|
|
119
|
+
const batch = this.queue.splice(0, this.queue.length);
|
|
120
|
+
try {
|
|
121
|
+
this.exporter.exportSync(batch);
|
|
122
|
+
this.exported += batch.length;
|
|
123
|
+
}
|
|
124
|
+
catch (error) {
|
|
125
|
+
this.dropped += batch.length;
|
|
126
|
+
debug(`exit export failed: ${describeError(error)}`);
|
|
127
|
+
}
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
const lost = this.queue.length;
|
|
131
|
+
this.queue.length = 0;
|
|
132
|
+
this.dropped += lost;
|
|
133
|
+
warn(`${lost} record(s) for ${this.exporter.describe()} were not sent: the process exited ` +
|
|
134
|
+
"(process.exit) before they could be. Call `await shutdown()` before exiting.");
|
|
135
|
+
}
|
|
136
|
+
stopTimers() {
|
|
137
|
+
if (this.immediate !== null)
|
|
138
|
+
clearImmediate(this.immediate);
|
|
139
|
+
if (this.interval !== null)
|
|
140
|
+
clearInterval(this.interval);
|
|
141
|
+
this.immediate = null;
|
|
142
|
+
this.interval = null;
|
|
143
|
+
}
|
|
144
|
+
async close() {
|
|
145
|
+
await this.flush(true);
|
|
146
|
+
this.closed = true;
|
|
147
|
+
this.stopTimers();
|
|
148
|
+
try {
|
|
149
|
+
await this.exporter.close?.();
|
|
150
|
+
}
|
|
151
|
+
catch (error) {
|
|
152
|
+
debug(`exporter close failed: ${describeError(error)}`);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
package/dist/record.d.ts
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The decision record layout. Field names are the ones Python's `@capture`
|
|
3
|
+
* and provider instrumentation write (see `tests/fixtures/decision_record.json`
|
|
4
|
+
* and `briefcase/integrations/providers/_base.py`), so records from both
|
|
5
|
+
* languages look the same to every consumer.
|
|
6
|
+
*/
|
|
7
|
+
export declare const SCHEMA_VERSION = "1.2";
|
|
8
|
+
export interface TokenUsage {
|
|
9
|
+
prompt_tokens: number | null;
|
|
10
|
+
completion_tokens: number | null;
|
|
11
|
+
total_tokens: number | null;
|
|
12
|
+
[counter: string]: number | null;
|
|
13
|
+
}
|
|
14
|
+
export interface DecisionRecord {
|
|
15
|
+
schema_version: string;
|
|
16
|
+
decision_id: string;
|
|
17
|
+
decision_type: string;
|
|
18
|
+
function_name: string;
|
|
19
|
+
inputs: Record<string, unknown>;
|
|
20
|
+
outputs: Record<string, unknown>;
|
|
21
|
+
started_at: string;
|
|
22
|
+
ended_at?: string;
|
|
23
|
+
execution_time_ms?: number;
|
|
24
|
+
status?: "ok" | "error" | "cancelled";
|
|
25
|
+
error?: string;
|
|
26
|
+
error_type?: string;
|
|
27
|
+
http_status?: number;
|
|
28
|
+
model_parameters?: Record<string, unknown>;
|
|
29
|
+
token_usage?: TokenUsage | null;
|
|
30
|
+
finish_reason?: string;
|
|
31
|
+
response_id?: string;
|
|
32
|
+
request_id?: string;
|
|
33
|
+
stream?: boolean;
|
|
34
|
+
time_to_first_chunk_ms?: number;
|
|
35
|
+
context_version?: string;
|
|
36
|
+
workflow_id?: string;
|
|
37
|
+
trace_id?: string;
|
|
38
|
+
parent_span_id?: string;
|
|
39
|
+
trace_flags?: number;
|
|
40
|
+
[field: string]: unknown;
|
|
41
|
+
}
|
|
42
|
+
/** Starts a record: ids, timestamps and correlation from the caller's context. */
|
|
43
|
+
export declare function startRecord(decisionType: string, functionName: string, contextVersion: string | undefined): {
|
|
44
|
+
record: DecisionRecord;
|
|
45
|
+
startedEpochMs: number;
|
|
46
|
+
t0: number;
|
|
47
|
+
};
|
|
48
|
+
/**
|
|
49
|
+
* Normalizes provider token counters onto prompt/completion/total, keeping
|
|
50
|
+
* provider-specific counters when they are numbers. Returns `null` when the
|
|
51
|
+
* provider reported nothing; an individual counter that was not reported is
|
|
52
|
+
* `null`, never 0.
|
|
53
|
+
*/
|
|
54
|
+
export declare function tokenUsage(prompt: unknown, completion: unknown, total?: unknown, extras?: Record<string, unknown>): TokenUsage | null;
|
package/dist/record.js
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The decision record layout. Field names are the ones Python's `@capture`
|
|
3
|
+
* and provider instrumentation write (see `tests/fixtures/decision_record.json`
|
|
4
|
+
* and `briefcase/integrations/providers/_base.py`), so records from both
|
|
5
|
+
* languages look the same to every consumer.
|
|
6
|
+
*/
|
|
7
|
+
import { randomUUID } from "node:crypto";
|
|
8
|
+
import { correlationFields } from "./context.js";
|
|
9
|
+
import { isoTimestamp } from "./util.js";
|
|
10
|
+
export const SCHEMA_VERSION = "1.2";
|
|
11
|
+
/** Starts a record: ids, timestamps and correlation from the caller's context. */
|
|
12
|
+
export function startRecord(decisionType, functionName, contextVersion) {
|
|
13
|
+
const t0 = performance.now();
|
|
14
|
+
const startedEpochMs = performance.timeOrigin + t0;
|
|
15
|
+
const record = {
|
|
16
|
+
schema_version: SCHEMA_VERSION,
|
|
17
|
+
decision_id: randomUUID(),
|
|
18
|
+
decision_type: decisionType,
|
|
19
|
+
function_name: functionName,
|
|
20
|
+
inputs: {},
|
|
21
|
+
outputs: {},
|
|
22
|
+
started_at: isoTimestamp(startedEpochMs),
|
|
23
|
+
};
|
|
24
|
+
if (contextVersion !== undefined)
|
|
25
|
+
record.context_version = contextVersion;
|
|
26
|
+
Object.assign(record, correlationFields());
|
|
27
|
+
return { record, startedEpochMs, t0 };
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Normalizes provider token counters onto prompt/completion/total, keeping
|
|
31
|
+
* provider-specific counters when they are numbers. Returns `null` when the
|
|
32
|
+
* provider reported nothing; an individual counter that was not reported is
|
|
33
|
+
* `null`, never 0.
|
|
34
|
+
*/
|
|
35
|
+
export function tokenUsage(prompt, completion, total, extras = {}) {
|
|
36
|
+
const p = toInt(prompt);
|
|
37
|
+
const c = toInt(completion);
|
|
38
|
+
const t = toInt(total);
|
|
39
|
+
if (p === null && c === null && t === null)
|
|
40
|
+
return null;
|
|
41
|
+
const usage = {
|
|
42
|
+
prompt_tokens: p,
|
|
43
|
+
completion_tokens: c,
|
|
44
|
+
total_tokens: t ?? (p !== null && c !== null ? p + c : null),
|
|
45
|
+
};
|
|
46
|
+
for (const [key, value] of Object.entries(extras)) {
|
|
47
|
+
const n = toInt(value);
|
|
48
|
+
if (n !== null)
|
|
49
|
+
usage[key] = n;
|
|
50
|
+
}
|
|
51
|
+
return usage;
|
|
52
|
+
}
|
|
53
|
+
function toInt(value) {
|
|
54
|
+
return typeof value === "number" && Number.isFinite(value) ? Math.trunc(value) : null;
|
|
55
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `node --import @briefcase-ai/auto/register app.js`
|
|
3
|
+
*
|
|
4
|
+
* Zero-code activation: `observe()` (destination from the environment) and
|
|
5
|
+
* `auto()` before the application's own modules load. See `activate.ts`
|
|
6
|
+
* for when it activates. No top-level await, so it also loads with
|
|
7
|
+
* `--require` in CommonJS programs (require(esm), Node >= 26).
|
|
8
|
+
*/
|
|
9
|
+
export declare const activation: import("./activate.js").Activation;
|
package/dist/register.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `node --import @briefcase-ai/auto/register app.js`
|
|
3
|
+
*
|
|
4
|
+
* Zero-code activation: `observe()` (destination from the environment) and
|
|
5
|
+
* `auto()` before the application's own modules load. See `activate.ts`
|
|
6
|
+
* for when it activates. No top-level await, so it also loads with
|
|
7
|
+
* `--require` in CommonJS programs (require(esm), Node >= 26).
|
|
8
|
+
*/
|
|
9
|
+
import { fileURLToPath } from "node:url";
|
|
10
|
+
import { activate, preloadedOnCommandLine } from "./activate.js";
|
|
11
|
+
const here = fileURLToPath(import.meta.url);
|
|
12
|
+
export const activation = activate(process.env, preloadedOnCommandLine(process.execArgv, [here]));
|
package/dist/state.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Per-thread state, kept on `globalThis` under a registered symbol so two
|
|
3
|
+
* copies of this package (an ESM import and a bundled copy, say) share one
|
|
4
|
+
* queue and never patch the same SDK twice. Each worker thread has its own
|
|
5
|
+
* `globalThis`, so its own state.
|
|
6
|
+
*/
|
|
7
|
+
import type { PendingCall, Pipeline } from "./pipeline.js";
|
|
8
|
+
import type { ProviderReport } from "./instrument/core.js";
|
|
9
|
+
import type { LoadedCopy } from "./loader.js";
|
|
10
|
+
export interface GlobalState {
|
|
11
|
+
pipeline: Pipeline | null;
|
|
12
|
+
pending: Set<PendingCall>;
|
|
13
|
+
providers: Map<string, ProviderReport>;
|
|
14
|
+
hooksInstalled: boolean;
|
|
15
|
+
/** SDK module copies that have reported themselves (see loader.ts). */
|
|
16
|
+
copies: Map<string, LoadedCopy>;
|
|
17
|
+
loadHooks: "none" | "installed" | "failed";
|
|
18
|
+
/** True when the load hook was installed by the preload, before any application module loaded. */
|
|
19
|
+
hooksAtStartup: boolean;
|
|
20
|
+
/** Request detection (detect.ts) is subscribed. */
|
|
21
|
+
detecting: boolean;
|
|
22
|
+
/** Main thread: the channel worker threads forward records on (observe.ts). */
|
|
23
|
+
workerChannel?: object;
|
|
24
|
+
}
|
|
25
|
+
export declare function state(): GlobalState;
|
package/dist/state.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Per-thread state, kept on `globalThis` under a registered symbol so two
|
|
3
|
+
* copies of this package (an ESM import and a bundled copy, say) share one
|
|
4
|
+
* queue and never patch the same SDK twice. Each worker thread has its own
|
|
5
|
+
* `globalThis`, so its own state.
|
|
6
|
+
*/
|
|
7
|
+
const KEY = Symbol.for("briefcase-ai.auto.state.v1");
|
|
8
|
+
export function state() {
|
|
9
|
+
const holder = globalThis;
|
|
10
|
+
let current = holder[KEY];
|
|
11
|
+
if (current && !current.copies) {
|
|
12
|
+
// Left by an older copy of this package in the same thread: add what it lacks.
|
|
13
|
+
Object.assign(current, { copies: new Map(), loadHooks: "none", hooksAtStartup: false, detecting: false });
|
|
14
|
+
}
|
|
15
|
+
if (!current) {
|
|
16
|
+
current = {
|
|
17
|
+
pipeline: null,
|
|
18
|
+
pending: new Set(),
|
|
19
|
+
providers: new Map(),
|
|
20
|
+
hooksInstalled: false,
|
|
21
|
+
copies: new Map(),
|
|
22
|
+
loadHooks: "none",
|
|
23
|
+
hooksAtStartup: false,
|
|
24
|
+
detecting: false,
|
|
25
|
+
};
|
|
26
|
+
holder[KEY] = current;
|
|
27
|
+
}
|
|
28
|
+
return current;
|
|
29
|
+
}
|
package/dist/util.d.ts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Small helpers shared by every module: logging that never throws, bounded
|
|
3
|
+
* JSON-safe copies of content, hashing and Python-compatible timestamps.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* This module's URL, or (when this package is bundled into a CommonJS
|
|
7
|
+
* bundle, where esbuild leaves `import.meta.url` undefined) a file in the
|
|
8
|
+
* working directory, so `createRequire` still works.
|
|
9
|
+
*/
|
|
10
|
+
export declare function moduleUrl(url: string | undefined): string;
|
|
11
|
+
type Level = "debug" | "warning" | "error";
|
|
12
|
+
/** Writes one `briefcase.auto: ...` line to stderr. Never throws. */
|
|
13
|
+
export declare function log(level: Level, message: string): void;
|
|
14
|
+
export declare const warn: (message: string) => void;
|
|
15
|
+
export declare const debug: (message: string) => void;
|
|
16
|
+
/** Runs `fn`, swallowing (and debug-logging) anything it throws. */
|
|
17
|
+
export declare function safe<T>(fn: () => T, what?: string): T | undefined;
|
|
18
|
+
export declare function describeError(error: unknown): string;
|
|
19
|
+
export declare function sha256Hex(text: string): string;
|
|
20
|
+
/** JSON text with sorted object keys (the TS analog of Python's json.dumps(sort_keys=True)). */
|
|
21
|
+
export declare function stableText(value: unknown): string;
|
|
22
|
+
export declare const DEFAULT_MAX_CONTENT_CHARS = 8000;
|
|
23
|
+
/**
|
|
24
|
+
* Bounded, JSON-safe copy of request or response content. Strings are cut at
|
|
25
|
+
* `limit` characters, binary data is replaced by its length, functions and
|
|
26
|
+
* iterators are named rather than consumed. Never throws.
|
|
27
|
+
*/
|
|
28
|
+
export declare function jsonable(value: unknown, limit?: number, depth?: number): unknown;
|
|
29
|
+
export declare function typeName(value: unknown): string;
|
|
30
|
+
/** Follows keys along `path`; `undefined` when any hop is missing. Never throws. */
|
|
31
|
+
export declare function read(obj: unknown, ...path: string[]): unknown;
|
|
32
|
+
export declare function asInt(value: unknown): number | null;
|
|
33
|
+
export declare function asString(value: unknown): string | null;
|
|
34
|
+
/** Picks the scalar request parameters named in `names`; no content can ride along. */
|
|
35
|
+
export declare function scalarParams(source: unknown, names: readonly string[]): Record<string, unknown>;
|
|
36
|
+
/**
|
|
37
|
+
* Python's `datetime.isoformat()` layout for an aware UTC datetime
|
|
38
|
+
* (`2026-08-15T12:00:00.250000+00:00`), so timestamps from both languages
|
|
39
|
+
* sort and parse the same way.
|
|
40
|
+
*/
|
|
41
|
+
export declare function isoTimestamp(epochMs: number): string;
|
|
42
|
+
/** Bounded string accumulator for streamed text. */
|
|
43
|
+
export declare class TextAccumulator {
|
|
44
|
+
private readonly limit;
|
|
45
|
+
private parts;
|
|
46
|
+
private size;
|
|
47
|
+
constructor(limit: number);
|
|
48
|
+
add(piece: unknown): void;
|
|
49
|
+
get text(): string;
|
|
50
|
+
}
|
|
51
|
+
export {};
|
package/dist/util.js
ADDED
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Small helpers shared by every module: logging that never throws, bounded
|
|
3
|
+
* JSON-safe copies of content, hashing and Python-compatible timestamps.
|
|
4
|
+
*/
|
|
5
|
+
import { createHash } from "node:crypto";
|
|
6
|
+
import { join } from "node:path";
|
|
7
|
+
import { pathToFileURL } from "node:url";
|
|
8
|
+
/**
|
|
9
|
+
* This module's URL, or (when this package is bundled into a CommonJS
|
|
10
|
+
* bundle, where esbuild leaves `import.meta.url` undefined) a file in the
|
|
11
|
+
* working directory, so `createRequire` still works.
|
|
12
|
+
*/
|
|
13
|
+
export function moduleUrl(url) {
|
|
14
|
+
return typeof url === "string" && url ? url : pathToFileURL(join(process.cwd(), "noop.js")).href;
|
|
15
|
+
}
|
|
16
|
+
const LEVELS = { debug: 10, info: 20, warning: 30, warn: 30, error: 40, critical: 50, off: 100 };
|
|
17
|
+
function threshold() {
|
|
18
|
+
const raw = (process.env["BRIEFCASE_LOG_LEVEL"] ?? "").trim().toLowerCase();
|
|
19
|
+
return LEVELS[raw] ?? LEVELS["warning"];
|
|
20
|
+
}
|
|
21
|
+
/** Writes one `briefcase.auto: ...` line to stderr. Never throws. */
|
|
22
|
+
export function log(level, message) {
|
|
23
|
+
try {
|
|
24
|
+
if ((LEVELS[level] ?? 30) < threshold())
|
|
25
|
+
return;
|
|
26
|
+
process.stderr.write(`briefcase.auto: ${message}\n`);
|
|
27
|
+
}
|
|
28
|
+
catch {
|
|
29
|
+
// logging must never break the application
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
export const warn = (message) => log("warning", message);
|
|
33
|
+
export const debug = (message) => log("debug", message);
|
|
34
|
+
/** Runs `fn`, swallowing (and debug-logging) anything it throws. */
|
|
35
|
+
export function safe(fn, what = "callback") {
|
|
36
|
+
try {
|
|
37
|
+
return fn();
|
|
38
|
+
}
|
|
39
|
+
catch (error) {
|
|
40
|
+
debug(`${what} failed: ${describeError(error)}`);
|
|
41
|
+
return undefined;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
export function describeError(error) {
|
|
45
|
+
if (error instanceof Error)
|
|
46
|
+
return `${error.name}: ${error.message}`;
|
|
47
|
+
return String(error);
|
|
48
|
+
}
|
|
49
|
+
export function sha256Hex(text) {
|
|
50
|
+
return createHash("sha256").update(text, "utf8").digest("hex");
|
|
51
|
+
}
|
|
52
|
+
/** JSON text with sorted object keys (the TS analog of Python's json.dumps(sort_keys=True)). */
|
|
53
|
+
export function stableText(value) {
|
|
54
|
+
try {
|
|
55
|
+
return JSON.stringify(value, (_key, v) => {
|
|
56
|
+
if (v && typeof v === "object" && !Array.isArray(v)) {
|
|
57
|
+
const sorted = {};
|
|
58
|
+
for (const k of Object.keys(v).sort())
|
|
59
|
+
sorted[k] = v[k];
|
|
60
|
+
return sorted;
|
|
61
|
+
}
|
|
62
|
+
return v;
|
|
63
|
+
}) ?? String(value);
|
|
64
|
+
}
|
|
65
|
+
catch {
|
|
66
|
+
return String(value);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
export const DEFAULT_MAX_CONTENT_CHARS = 8000;
|
|
70
|
+
const MAX_DEPTH = 8;
|
|
71
|
+
const MAX_ITEMS = 200;
|
|
72
|
+
/**
|
|
73
|
+
* Bounded, JSON-safe copy of request or response content. Strings are cut at
|
|
74
|
+
* `limit` characters, binary data is replaced by its length, functions and
|
|
75
|
+
* iterators are named rather than consumed. Never throws.
|
|
76
|
+
*/
|
|
77
|
+
export function jsonable(value, limit = DEFAULT_MAX_CONTENT_CHARS, depth = 0) {
|
|
78
|
+
try {
|
|
79
|
+
if (value === null || value === undefined)
|
|
80
|
+
return null;
|
|
81
|
+
if (typeof value === "string")
|
|
82
|
+
return value.length <= limit ? value : value.slice(0, limit) + "...";
|
|
83
|
+
if (typeof value === "number")
|
|
84
|
+
return Number.isFinite(value) ? value : String(value);
|
|
85
|
+
if (typeof value === "boolean")
|
|
86
|
+
return value;
|
|
87
|
+
if (typeof value === "bigint")
|
|
88
|
+
return value.toString();
|
|
89
|
+
if (typeof value === "function")
|
|
90
|
+
return "<function>";
|
|
91
|
+
if (typeof value === "symbol")
|
|
92
|
+
return "<symbol>";
|
|
93
|
+
if (value instanceof Uint8Array || value instanceof ArrayBuffer)
|
|
94
|
+
return `<${value.byteLength} bytes>`;
|
|
95
|
+
if (depth >= MAX_DEPTH)
|
|
96
|
+
return `<${typeName(value)}>`;
|
|
97
|
+
if (Array.isArray(value))
|
|
98
|
+
return value.slice(0, MAX_ITEMS).map((v) => jsonable(v, limit, depth + 1));
|
|
99
|
+
if (value instanceof Date)
|
|
100
|
+
return value.toISOString();
|
|
101
|
+
if (typeof value[Symbol.asyncIterator] === "function") {
|
|
102
|
+
return `<${typeName(value)}>`;
|
|
103
|
+
}
|
|
104
|
+
const out = {};
|
|
105
|
+
for (const key of Object.keys(value).slice(0, MAX_ITEMS)) {
|
|
106
|
+
out[key] = jsonable(value[key], limit, depth + 1);
|
|
107
|
+
}
|
|
108
|
+
return out;
|
|
109
|
+
}
|
|
110
|
+
catch {
|
|
111
|
+
return `<${typeName(value)}>`;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
export function typeName(value) {
|
|
115
|
+
if (value === null)
|
|
116
|
+
return "null";
|
|
117
|
+
if (value === undefined)
|
|
118
|
+
return "undefined";
|
|
119
|
+
if (typeof value !== "object" && typeof value !== "function")
|
|
120
|
+
return typeof value;
|
|
121
|
+
try {
|
|
122
|
+
const ctor = value.constructor;
|
|
123
|
+
if (ctor && typeof ctor.name === "string" && ctor.name)
|
|
124
|
+
return ctor.name;
|
|
125
|
+
}
|
|
126
|
+
catch {
|
|
127
|
+
// fall through
|
|
128
|
+
}
|
|
129
|
+
return typeof value;
|
|
130
|
+
}
|
|
131
|
+
/** Follows keys along `path`; `undefined` when any hop is missing. Never throws. */
|
|
132
|
+
export function read(obj, ...path) {
|
|
133
|
+
let current = obj;
|
|
134
|
+
for (const key of path) {
|
|
135
|
+
if (current === null || current === undefined)
|
|
136
|
+
return undefined;
|
|
137
|
+
if (typeof current !== "object" && typeof current !== "function")
|
|
138
|
+
return undefined;
|
|
139
|
+
try {
|
|
140
|
+
current = current[key];
|
|
141
|
+
}
|
|
142
|
+
catch {
|
|
143
|
+
return undefined;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
return current;
|
|
147
|
+
}
|
|
148
|
+
export function asInt(value) {
|
|
149
|
+
if (typeof value !== "number" || !Number.isFinite(value))
|
|
150
|
+
return null;
|
|
151
|
+
return Math.trunc(value);
|
|
152
|
+
}
|
|
153
|
+
export function asString(value) {
|
|
154
|
+
return typeof value === "string" && value ? value : null;
|
|
155
|
+
}
|
|
156
|
+
/** Picks the scalar request parameters named in `names`; no content can ride along. */
|
|
157
|
+
export function scalarParams(source, names) {
|
|
158
|
+
const out = {};
|
|
159
|
+
if (!source || typeof source !== "object")
|
|
160
|
+
return out;
|
|
161
|
+
for (const name of names) {
|
|
162
|
+
const value = read(source, name);
|
|
163
|
+
if (typeof value === "boolean" || (typeof value === "number" && Number.isFinite(value)))
|
|
164
|
+
out[name] = value;
|
|
165
|
+
else if (typeof value === "string" && value.length <= 200)
|
|
166
|
+
out[name] = value;
|
|
167
|
+
}
|
|
168
|
+
return out;
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Python's `datetime.isoformat()` layout for an aware UTC datetime
|
|
172
|
+
* (`2026-08-15T12:00:00.250000+00:00`), so timestamps from both languages
|
|
173
|
+
* sort and parse the same way.
|
|
174
|
+
*/
|
|
175
|
+
export function isoTimestamp(epochMs) {
|
|
176
|
+
const whole = Math.floor(epochMs);
|
|
177
|
+
const micros = Math.round((epochMs - whole) * 1000);
|
|
178
|
+
const iso = new Date(whole).toISOString(); // 2026-08-15T12:00:00.250Z
|
|
179
|
+
return `${iso.slice(0, 23)}${String(micros).padStart(3, "0").slice(0, 3)}+00:00`;
|
|
180
|
+
}
|
|
181
|
+
/** Bounded string accumulator for streamed text. */
|
|
182
|
+
export class TextAccumulator {
|
|
183
|
+
limit;
|
|
184
|
+
parts = [];
|
|
185
|
+
size = 0;
|
|
186
|
+
constructor(limit) {
|
|
187
|
+
this.limit = limit;
|
|
188
|
+
}
|
|
189
|
+
add(piece) {
|
|
190
|
+
if (typeof piece !== "string" || !piece || this.size >= this.limit)
|
|
191
|
+
return;
|
|
192
|
+
this.parts.push(piece.slice(0, this.limit - this.size));
|
|
193
|
+
this.size += piece.length;
|
|
194
|
+
}
|
|
195
|
+
get text() {
|
|
196
|
+
return this.parts.join("");
|
|
197
|
+
}
|
|
198
|
+
}
|
package/package.json
CHANGED
|
@@ -1 +1,84 @@
|
|
|
1
|
-
{
|
|
1
|
+
{
|
|
2
|
+
"name": "@briefcase-ai/auto",
|
|
3
|
+
"version": "5.0.0",
|
|
4
|
+
"description": "Compliance monitoring, auditing, and evaluating third-party artificial intelligence systems and models.",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/briefcasebrain/briefcase-ai-sdk.git",
|
|
9
|
+
"directory": "js/auto"
|
|
10
|
+
},
|
|
11
|
+
"type": "module",
|
|
12
|
+
"sideEffects": [
|
|
13
|
+
"./dist/register.js"
|
|
14
|
+
],
|
|
15
|
+
"engines": {
|
|
16
|
+
"node": ">=26"
|
|
17
|
+
},
|
|
18
|
+
"files": [
|
|
19
|
+
"dist"
|
|
20
|
+
],
|
|
21
|
+
"main": "./dist/index.js",
|
|
22
|
+
"types": "./dist/index.d.ts",
|
|
23
|
+
"exports": {
|
|
24
|
+
".": {
|
|
25
|
+
"types": "./dist/index.d.ts",
|
|
26
|
+
"default": "./dist/index.js"
|
|
27
|
+
},
|
|
28
|
+
"./register": {
|
|
29
|
+
"types": "./dist/register.d.ts",
|
|
30
|
+
"default": "./dist/register.js"
|
|
31
|
+
},
|
|
32
|
+
"./esbuild": {
|
|
33
|
+
"types": "./dist/esbuild.d.ts",
|
|
34
|
+
"default": "./dist/esbuild.js"
|
|
35
|
+
},
|
|
36
|
+
"./package.json": "./package.json"
|
|
37
|
+
},
|
|
38
|
+
"publishConfig": {
|
|
39
|
+
"access": "public",
|
|
40
|
+
"provenance": true
|
|
41
|
+
},
|
|
42
|
+
"scripts": {
|
|
43
|
+
"build": "tsc -p tsconfig.build.json",
|
|
44
|
+
"test": "vitest run",
|
|
45
|
+
"typecheck": "tsc --noEmit",
|
|
46
|
+
"bench:startup": "npm run build && node scripts/startup-bench.mjs 20"
|
|
47
|
+
},
|
|
48
|
+
"peerDependencies": {
|
|
49
|
+
"@anthropic-ai/sdk": "*",
|
|
50
|
+
"@aws-sdk/client-bedrock-runtime": "*",
|
|
51
|
+
"@google/genai": "*",
|
|
52
|
+
"@opentelemetry/api": "*",
|
|
53
|
+
"openai": "*"
|
|
54
|
+
},
|
|
55
|
+
"peerDependenciesMeta": {
|
|
56
|
+
"@anthropic-ai/sdk": {
|
|
57
|
+
"optional": true
|
|
58
|
+
},
|
|
59
|
+
"@aws-sdk/client-bedrock-runtime": {
|
|
60
|
+
"optional": true
|
|
61
|
+
},
|
|
62
|
+
"@google/genai": {
|
|
63
|
+
"optional": true
|
|
64
|
+
},
|
|
65
|
+
"@opentelemetry/api": {
|
|
66
|
+
"optional": true
|
|
67
|
+
},
|
|
68
|
+
"openai": {
|
|
69
|
+
"optional": true
|
|
70
|
+
}
|
|
71
|
+
},
|
|
72
|
+
"devDependencies": {
|
|
73
|
+
"@anthropic-ai/sdk": "1.12.0",
|
|
74
|
+
"@aws-sdk/client-bedrock-runtime": "3.1175.0",
|
|
75
|
+
"@google/genai": "3.6.1",
|
|
76
|
+
"@opentelemetry/api": "^1.9.0",
|
|
77
|
+
"@opentelemetry/context-async-hooks": "^1.30.1",
|
|
78
|
+
"@types/node": "^24.3.0",
|
|
79
|
+
"esbuild": "0.28.2",
|
|
80
|
+
"openai": "7.62.0",
|
|
81
|
+
"typescript": "^5.9.2",
|
|
82
|
+
"vitest": "^3.2.4"
|
|
83
|
+
}
|
|
84
|
+
}
|