@briefcase-ai/auto 0.0.0-stage → 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.
Files changed (42) hide show
  1. package/README.md +102 -2
  2. package/dist/activate.d.ts +35 -0
  3. package/dist/activate.js +108 -0
  4. package/dist/auto.d.ts +92 -0
  5. package/dist/auto.js +350 -0
  6. package/dist/context.d.ts +49 -0
  7. package/dist/context.js +82 -0
  8. package/dist/detect.d.ts +26 -0
  9. package/dist/detect.js +127 -0
  10. package/dist/esbuild.d.ts +40 -0
  11. package/dist/esbuild.js +55 -0
  12. package/dist/exporters.d.ts +196 -0
  13. package/dist/exporters.js +405 -0
  14. package/dist/index.d.ts +16 -0
  15. package/dist/index.js +14 -0
  16. package/dist/instrument/anthropic.d.ts +40 -0
  17. package/dist/instrument/anthropic.js +178 -0
  18. package/dist/instrument/bedrock.d.ts +53 -0
  19. package/dist/instrument/bedrock.js +256 -0
  20. package/dist/instrument/core.d.ts +257 -0
  21. package/dist/instrument/core.js +772 -0
  22. package/dist/instrument/google-genai.d.ts +71 -0
  23. package/dist/instrument/google-genai.js +289 -0
  24. package/dist/instrument/openai.d.ts +68 -0
  25. package/dist/instrument/openai.js +293 -0
  26. package/dist/instrument/stainless.d.ts +41 -0
  27. package/dist/instrument/stainless.js +151 -0
  28. package/dist/loader.d.ts +79 -0
  29. package/dist/loader.js +202 -0
  30. package/dist/observe.d.ts +60 -0
  31. package/dist/observe.js +246 -0
  32. package/dist/pipeline.d.ts +48 -0
  33. package/dist/pipeline.js +155 -0
  34. package/dist/record.d.ts +54 -0
  35. package/dist/record.js +55 -0
  36. package/dist/register.d.ts +9 -0
  37. package/dist/register.js +12 -0
  38. package/dist/state.d.ts +25 -0
  39. package/dist/state.js +29 -0
  40. package/dist/util.d.ts +51 -0
  41. package/dist/util.js +198 -0
  42. package/package.json +82 -4
@@ -0,0 +1,257 @@
1
+ /**
2
+ * Shared machinery for provider instrumentation (the TypeScript counterpart
3
+ * of `briefcase/integrations/providers/_base.py`):
4
+ *
5
+ * - `Instrumentor`: owns the patches of one provider. Every target of a
6
+ * module copy is checked before any is applied, so an SDK whose layout
7
+ * differs from the verified one is skipped whole (one warning), never
8
+ * half-patched. `undo()` restores an original only while our wrapper is
9
+ * still the installed value; a later patch by someone else is left alone.
10
+ * - `ProviderCall`: one call in flight; `finish()` emits exactly one record
11
+ * and never throws.
12
+ * - `observeAsyncIterator`: reports the items and the end of an async
13
+ * iterator without changing what the consumer sees.
14
+ * - `loadCopies`: the ESM and CommonJS copies of an SDK (dual packages load
15
+ * them as separate module instances, with separate classes).
16
+ */
17
+ import { AsyncLocalStorage } from "node:async_hooks";
18
+ import type { PendingCall } from "../pipeline.js";
19
+ import { type DecisionRecord, type TokenUsage } from "../record.js";
20
+ export type ContentMode = "none" | "hash" | "full";
21
+ export interface CaptureOptions {
22
+ captureContent: ContentMode;
23
+ maxContentChars: number;
24
+ redact?: (text: string) => string;
25
+ contextVersion?: string;
26
+ /** openai: ask chat streams for a final usage chunk (`stream_options.include_usage`). */
27
+ streamUsage: boolean;
28
+ }
29
+ export declare function normalizeContentMode(value: unknown): ContentMode;
30
+ /**
31
+ * The call being recorded in the current async context. Wrappers run the
32
+ * SDK's own method inside it, so nested SDK internals can tell they are
33
+ * already covered and the request detector (`detect.ts`) can tell a
34
+ * recorded request from one made through an uninstrumented SDK copy.
35
+ */
36
+ export declare const callStore: AsyncLocalStorage<ProviderCall>;
37
+ /** Runs `fn` inside `call`'s context (no-op wrapper when `call` is null). */
38
+ export declare function within<T>(call: ProviderCall | null, fn: () => T): T;
39
+ /** Marker on every wrapper we install; holds the wrapped original. */
40
+ export declare const WRAPPED: unique symbol;
41
+ export interface ProviderReport {
42
+ name: string;
43
+ /**
44
+ * patched: at least one SDK copy is instrumented. waiting: installed (or
45
+ * listening for a bundled copy) but no copy has been loaded yet; it is
46
+ * patched when the application loads it. skipped: the layout check
47
+ * failed. not-installed: not resolvable from here and nothing reported
48
+ * itself. undone: `undo()` ran. not-checked: `auto()` never looked.
49
+ */
50
+ state: "patched" | "waiting" | "skipped" | "not-installed" | "undone" | "not-checked";
51
+ packageName: string;
52
+ sdkVersion: string | null;
53
+ verifiedVersion: string;
54
+ patched: string[];
55
+ skipped: {
56
+ target: string;
57
+ reason: string;
58
+ }[];
59
+ refusedUndo: string[];
60
+ /** Provider requests seen on the wire that no instrumented copy recorded (see detect.ts). */
61
+ unrecordedRequests: number;
62
+ instrumentor?: Instrumentor;
63
+ }
64
+ export interface MethodTarget {
65
+ kind: "method";
66
+ owner: unknown;
67
+ key: string;
68
+ label: string;
69
+ /** Allow a method the owner inherits: our wrapper becomes an own property that undo deletes. */
70
+ inherited?: boolean;
71
+ wrap: (original: (...args: unknown[]) => unknown) => (...args: unknown[]) => unknown;
72
+ }
73
+ /**
74
+ * An accessor installed on a prototype for a property the SDK assigns per
75
+ * instance in its constructor (`this.generateContent = async (...) => ...`).
76
+ * The setter stores `wrap(value)` as the instance's own data property.
77
+ */
78
+ export interface InstanceAssignTarget {
79
+ kind: "instance-assign";
80
+ owner: unknown;
81
+ key: string;
82
+ label: string;
83
+ wrap: (original: (...args: unknown[]) => unknown) => (...args: unknown[]) => unknown;
84
+ /** Called with the instance whenever the setter wraps a value for it. */
85
+ assigned?: (instance: object) => void;
86
+ }
87
+ export type PatchTarget = MethodTarget | InstanceAssignTarget;
88
+ export declare class Instrumentor {
89
+ readonly provider: string;
90
+ readonly options: CaptureOptions;
91
+ active: boolean;
92
+ private readonly applied;
93
+ /** Module copies (see `loader.ts`) this instrumentor has already handled. */
94
+ readonly handledCopies: Set<string>;
95
+ readonly report: ProviderReport;
96
+ constructor(provider: string, options: CaptureOptions, packageName: string, verifiedVersion: string);
97
+ get capturesContent(): boolean;
98
+ /** Checks one target; returns why it cannot be patched, or null. */
99
+ private problem;
100
+ /**
101
+ * Applies every target, or none: if any target is missing or reshaped the
102
+ * whole set is skipped with one warning. Returns true when applied.
103
+ */
104
+ applyAll(copy: string, targets: PatchTarget[], verify?: () => string | null): boolean;
105
+ private applyOne;
106
+ private rollback;
107
+ /**
108
+ * Restores each original whose slot still holds our wrapper. A slot that
109
+ * now holds something else (another tool patched it after us) is left as
110
+ * it is; our wrapper inside it is switched to pass-through.
111
+ */
112
+ undo(): {
113
+ restored: string[];
114
+ refused: string[];
115
+ };
116
+ /** Starts a record, or returns null when nothing is exporting (the call then runs untouched). */
117
+ begin(decisionType: string, functionName: string, request: {
118
+ model?: unknown;
119
+ params?: Record<string, unknown>;
120
+ inputs?: () => unknown;
121
+ stream?: boolean;
122
+ }): ProviderCall | null;
123
+ /** Applies the content mode to an inputs / outputs value (Python's CaptureOptions.content). */
124
+ content(value: unknown): unknown;
125
+ errorText(error: unknown): string;
126
+ }
127
+ /** Class name of a thrown value (Python's `type(error).__name__`). */
128
+ export declare function errorClass(error: unknown): string;
129
+ export declare function isAbortError(error: unknown): boolean;
130
+ export interface CallInfo {
131
+ model?: unknown;
132
+ response_id?: unknown;
133
+ request_id?: unknown;
134
+ finish_reason?: unknown;
135
+ usage?: TokenUsage | null;
136
+ content?: unknown;
137
+ extra?: Record<string, unknown>;
138
+ cancelled?: boolean;
139
+ }
140
+ export interface StreamOutcome {
141
+ chunk_count: number;
142
+ stream_completed: boolean;
143
+ }
144
+ export declare class ProviderCall implements PendingCall {
145
+ private readonly inst;
146
+ private readonly record;
147
+ private readonly startedEpochMs;
148
+ private readonly t0;
149
+ private readonly model;
150
+ private readonly params;
151
+ private stream;
152
+ done: boolean;
153
+ /** The SDK object whose method started this call (used to recognise nested internal calls). */
154
+ owner: unknown;
155
+ private firstChunkMs;
156
+ /** What the record says if the call is abandoned (process exit) before it finishes. */
157
+ fallback: CallInfo;
158
+ chunks: number;
159
+ constructor(inst: Instrumentor, record: DecisionRecord, startedEpochMs: number, t0: number, model: string | null, params: Record<string, unknown>, stream: boolean);
160
+ get provider(): string;
161
+ markStream(): void;
162
+ firstChunk(): void;
163
+ /** Emits the record now, as an unfinished call (stream never read to the end). */
164
+ abandon(): void;
165
+ /** Completes and exports the record exactly once. Never throws. */
166
+ finish(info?: CallInfo, outcome?: {
167
+ error?: unknown;
168
+ stream?: StreamOutcome;
169
+ }): void;
170
+ private build;
171
+ }
172
+ /** How an observed iterator ended. */
173
+ export type EndKind = "exhausted" | "returned" | "error";
174
+ /** Per-chunk bookkeeping for a streamed call; provider modules subclass it. */
175
+ export declare abstract class StreamState {
176
+ readonly call: ProviderCall;
177
+ constructor(call: ProviderCall);
178
+ abstract observe(chunk: unknown): void;
179
+ abstract info(): CallInfo;
180
+ onItem(chunk: unknown): void;
181
+ /**
182
+ * Finishes the call. `exhausted` with `aborted` set means the request was
183
+ * aborted and the SDK ended the iteration quietly: recorded as cancelled.
184
+ * `returned` (the consumer stopped early) is recorded as an incomplete
185
+ * stream with status ok, as Python records a stream closed early.
186
+ */
187
+ onEnd(kind: EndKind, error: unknown, aborted?: boolean): void;
188
+ }
189
+ export interface IteratorHooks {
190
+ onItem(item: unknown): void;
191
+ onEnd(kind: EndKind, error?: unknown): void;
192
+ }
193
+ /**
194
+ * Wraps an async iterator so each item and the end are reported once. The
195
+ * consumer sees the same items, errors and `return()` behaviour; hook
196
+ * failures are swallowed.
197
+ */
198
+ export declare function observeAsyncIterator<T>(inner: AsyncIterator<T>, hooks: IteratorHooks): AsyncIterableIterator<T>;
199
+ /**
200
+ * Instruments an async iterable in place, keeping the caller's object:
201
+ * an async generator (or any object that is its own iterator) gets own
202
+ * `next` / `return` / `throw` that report to `hooks`; any other async
203
+ * iterable gets an own `[Symbol.asyncIterator]` returning an observed
204
+ * iterator. Returns false when `obj` is not async-iterable.
205
+ */
206
+ export declare function instrumentAsyncIterable(obj: unknown, hooks: IteratorHooks, runInside?: <R>(fn: () => R) => R): boolean;
207
+ export declare function finishWhenCollected(target: object, call: ProviderCall): void;
208
+ export declare const DEFAULT_OPTIONS: CaptureOptions;
209
+ /**
210
+ * One SDK file that holds patch targets, relative to the package root, as
211
+ * laid out in the verified version. `group` separates builds of the same
212
+ * format inside one package (e.g. @google/genai's `dist/node` and `dist`).
213
+ */
214
+ export interface SdkFile {
215
+ specifier: string;
216
+ rel: string;
217
+ format: "esm" | "cjs";
218
+ group: string;
219
+ }
220
+ /** The `.mjs` / `.js` pair of a Stainless resource module (`openai/resources/x` -> `resources/x.mjs`, `resources/x.js`). */
221
+ export declare function stainlessFiles(packageName: string, specifiers: readonly string[]): SdkFile[];
222
+ export interface ModuleCopy {
223
+ format: "esm" | "cjs";
224
+ path: string;
225
+ ns: Record<string, unknown>;
226
+ }
227
+ /**
228
+ * Resolves `specifier` the way an `import` from Node would, by reading the
229
+ * package's `exports` map with the conditions node / import / default.
230
+ * Used when `import.meta.resolve` is unavailable (some test runners and
231
+ * transpilers); `cjsPath` locates the package directory.
232
+ */
233
+ export declare function resolveEsmFromExports(specifier: string, cjsPath: string): string | null;
234
+ export declare function isNotFound(error: unknown): boolean;
235
+ /**
236
+ * The files an application could get for `specifier` from this package's
237
+ * location: what `import` resolves to and what `require` resolves to. For
238
+ * dual packages these differ and load as separate module instances.
239
+ * Resolution only; nothing is loaded. Null when not installed.
240
+ */
241
+ export declare function resolveCopies(specifier: string): {
242
+ esm: string | null;
243
+ cjs: string | null;
244
+ } | null;
245
+ /** Loads one file with `require()` (an ES module through require(esm): the same instance `import` gets). */
246
+ export declare function requireFile(path: string): Record<string, unknown>;
247
+ /**
248
+ * Loads every copy of `specifier` an application could get (see
249
+ * `resolveCopies`). Used by `auto({ eager: true })`. Returns null when the
250
+ * package is not installed.
251
+ */
252
+ export declare function loadCopies(specifier: string): {
253
+ copies: ModuleCopy[];
254
+ problems: string[];
255
+ } | null;
256
+ /** The `version` of the installed package that contains `file`. */
257
+ export declare function packageVersion(file: string, packageName: string): string | null;