@boundaryml/baml-bridge 0.0.0 → 0.13.1-nightly.20260707.e

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 (46) hide show
  1. package/dist/ctx_manager.d.ts +22 -0
  2. package/dist/ctx_manager.d.ts.map +1 -0
  3. package/dist/ctx_manager.js +91 -0
  4. package/dist/ctx_manager.js.map +1 -0
  5. package/dist/define_function.d.ts +38 -0
  6. package/dist/define_function.d.ts.map +1 -0
  7. package/dist/define_function.js +281 -0
  8. package/dist/define_function.js.map +1 -0
  9. package/dist/errors.d.ts +51 -0
  10. package/dist/errors.d.ts.map +1 -0
  11. package/dist/errors.js +67 -0
  12. package/dist/errors.js.map +1 -0
  13. package/dist/exit_hook.d.ts +9 -0
  14. package/dist/exit_hook.d.ts.map +1 -0
  15. package/dist/exit_hook.js +27 -0
  16. package/dist/exit_hook.js.map +1 -0
  17. package/dist/host_value_registry.d.ts +57 -0
  18. package/dist/host_value_registry.d.ts.map +1 -0
  19. package/dist/host_value_registry.js +141 -0
  20. package/dist/host_value_registry.js.map +1 -0
  21. package/dist/index.d.ts +67 -0
  22. package/dist/index.d.ts.map +1 -0
  23. package/dist/index.js +162 -0
  24. package/dist/index.js.map +1 -0
  25. package/dist/native.d.ts +312 -0
  26. package/dist/native.js +625 -0
  27. package/dist/proto/baml_cffi.d.ts +6385 -0
  28. package/dist/proto/baml_cffi.js +17738 -0
  29. package/dist/proto.d.ts +62 -0
  30. package/dist/proto.d.ts.map +1 -0
  31. package/dist/proto.js +963 -0
  32. package/dist/proto.js.map +1 -0
  33. package/dist/stream.d.ts +23 -0
  34. package/dist/stream.d.ts.map +1 -0
  35. package/dist/stream.js +68 -0
  36. package/dist/stream.js.map +1 -0
  37. package/dist/typemap.d.ts +37 -0
  38. package/dist/typemap.d.ts.map +1 -0
  39. package/dist/typemap.js +110 -0
  40. package/dist/typemap.js.map +1 -0
  41. package/dist/wire_ty.d.ts +58 -0
  42. package/dist/wire_ty.d.ts.map +1 -0
  43. package/dist/wire_ty.js +147 -0
  44. package/dist/wire_ty.js.map +1 -0
  45. package/package.json +64 -5
  46. package/README.md +0 -3
@@ -0,0 +1,22 @@
1
+ /**
2
+ * THIS FILE IS AUTO-GENERATED — DO NOT EDIT BY HAND.
3
+ *
4
+ * Source: baml_language/crates/bridge_nodejs/typescript_src/
5
+ * Proto: baml_language/crates/bridge_ctypes/types/baml_bridge/cffi/v1/*.proto
6
+ * Build: cd baml_language/crates/bridge_nodejs && pnpm build:debug
7
+ */
8
+ import { HostSpanManager } from './native.js';
9
+ export declare class CtxManager {
10
+ private rt;
11
+ private ctx;
12
+ constructor(rt: unknown);
13
+ get(): HostSpanManager;
14
+ allowReset(): boolean;
15
+ reset(): void;
16
+ cloneContext(): HostSpanManager;
17
+ upsertTags(tags: Record<string, string>): void;
18
+ traceFn<T>(name: string, fn: (...args: unknown[]) => T): (...args: unknown[]) => T;
19
+ traceFnAsync<T>(name: string, fn: (...args: unknown[]) => Promise<T>): (...args: unknown[]) => Promise<T>;
20
+ flush(): void;
21
+ }
22
+ //# sourceMappingURL=ctx_manager.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ctx_manager.d.ts","sourceRoot":"","sources":["../typescript_src/ctx_manager.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,eAAe,EAAe,MAAM,aAAa,CAAC;AAG3D,qBAAa,UAAU;IACnB,OAAO,CAAC,EAAE,CAAU;IACpB,OAAO,CAAC,GAAG,CAAqC;gBAEpC,EAAE,EAAE,OAAO;IAavB,GAAG,IAAI,eAAe;IAStB,UAAU,IAAI,OAAO;IAMrB,KAAK,IAAI,IAAI;IAIb,YAAY,IAAI,eAAe;IAK/B,UAAU,CAAC,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,IAAI;IAI9C,OAAO,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,EAAE,EAAE,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,KAAK,CAAC,GAAG,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,KAAK,CAAC;IAkBlF,YAAY,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,EAAE,EAAE,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,KAAK,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,KAAK,OAAO,CAAC,CAAC,CAAC;IAkBzG,KAAK,IAAI,IAAI;CAGhB"}
@@ -0,0 +1,91 @@
1
+ /**
2
+ * THIS FILE IS AUTO-GENERATED — DO NOT EDIT BY HAND.
3
+ *
4
+ * Source: baml_language/crates/bridge_nodejs/typescript_src/
5
+ * Proto: baml_language/crates/bridge_ctypes/types/baml_bridge/cffi/v1/*.proto
6
+ * Build: cd baml_language/crates/bridge_nodejs && pnpm build:debug
7
+ */
8
+ // ctx_manager.ts — mirrors bridge_python/python_src/baml_py/ctx_manager.py
9
+ // Uses AsyncLocalStorage for async context isolation (Node.js built-in).
10
+ import { AsyncLocalStorage } from 'node:async_hooks';
11
+ import { HostSpanManager, flushEvents } from './native.js';
12
+ import { installFlushOnExit } from './exit_hook.js';
13
+ export class CtxManager {
14
+ rt;
15
+ ctx;
16
+ constructor(rt) {
17
+ this.rt = rt;
18
+ this.ctx = new AsyncLocalStorage();
19
+ // FIXME: Eagerly creates HostSpanManager before the runtime may be fully initialized.
20
+ // Legacy engine/ was also eager (rt.createContextManager() in constructor).
21
+ // bridge_python lazily creates HostSpanManager per-thread on first access.
22
+ // Leaving as-is: get() and reset() already create fresh managers, and the legacy
23
+ // engine/ had the same eager pattern without reported issues.
24
+ this.ctx.enterWith(new HostSpanManager());
25
+ installFlushOnExit();
26
+ }
27
+ get() {
28
+ let mgr = this.ctx.getStore();
29
+ if (!mgr) {
30
+ mgr = new HostSpanManager();
31
+ this.ctx.enterWith(mgr);
32
+ }
33
+ return mgr;
34
+ }
35
+ allowReset() {
36
+ const mgr = this.ctx.getStore();
37
+ if (!mgr)
38
+ return true;
39
+ return mgr.contextDepth() === 0;
40
+ }
41
+ reset() {
42
+ this.ctx.enterWith(new HostSpanManager());
43
+ }
44
+ cloneContext() {
45
+ const mgr = this.get();
46
+ return mgr.deepClone();
47
+ }
48
+ upsertTags(tags) {
49
+ this.get().upsertTags(tags);
50
+ }
51
+ traceFn(name, fn) {
52
+ const self = this;
53
+ return function (...args) {
54
+ const mgr = self.cloneContext();
55
+ const argsObj = {};
56
+ args.forEach((arg, i) => { argsObj[`arg${i}`] = arg; });
57
+ mgr.enter(name, argsObj);
58
+ try {
59
+ const result = self.ctx.run(mgr, () => fn.apply(this, args));
60
+ mgr.exitOk();
61
+ return result;
62
+ }
63
+ catch (e) {
64
+ mgr.exitError(String(e));
65
+ throw e;
66
+ }
67
+ };
68
+ }
69
+ traceFnAsync(name, fn) {
70
+ const self = this;
71
+ return async function (...args) {
72
+ const mgr = self.cloneContext();
73
+ const argsObj = {};
74
+ args.forEach((arg, i) => { argsObj[`arg${i}`] = arg; });
75
+ mgr.enter(name, argsObj);
76
+ try {
77
+ const result = await self.ctx.run(mgr, () => fn.apply(this, args));
78
+ mgr.exitOk();
79
+ return result;
80
+ }
81
+ catch (e) {
82
+ mgr.exitError(String(e));
83
+ throw e;
84
+ }
85
+ };
86
+ }
87
+ flush() {
88
+ flushEvents();
89
+ }
90
+ }
91
+ //# sourceMappingURL=ctx_manager.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ctx_manager.js","sourceRoot":"","sources":["../typescript_src/ctx_manager.ts"],"names":[],"mappings":"AAAA,2EAA2E;AAC3E,yEAAyE;AAEzE,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AACrD,OAAO,EAAE,eAAe,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC3D,OAAO,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAC;AAEpD,MAAM,OAAO,UAAU;IACX,EAAE,CAAU;IACZ,GAA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AI,CAAC,CAAC,CAAC;gBACnE,GAAG,CAAC,MAAM,EAAE,CAAC;gBACb,OAAO,MAAM,CAAC;YAClB,CAAC;YAAC,OAAO,CAAC,EAAE,CAAC;gBACT,GAAG,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC;gBACzB,MAAM,CAAC,CAAC;YACZ,CAAC;QACL,CAAC,CAAC;IACN,CAAC;IAED,KAAK;QACD,WAAW,EAAE,CAAC;IAClB,CAAC;CACJ"}
@@ -0,0 +1,38 @@
1
+ /**
2
+ * THIS FILE IS AUTO-GENERATED — DO NOT EDIT BY HAND.
3
+ *
4
+ * Source: baml_language/crates/bridge_nodejs/typescript_src/
5
+ * Proto: baml_language/crates/bridge_ctypes/types/baml_bridge/cffi/v1/*.proto
6
+ * Build: cd baml_language/crates/bridge_nodejs && pnpm build:debug
7
+ */
8
+ export type Mode = 'sync' | 'async';
9
+ /** Sentinel for "argument not supplied" so optional kwargs can be skipped. */
10
+ export declare const UNSET: unique symbol;
11
+ /**
12
+ * Generic-binding contract for a callable. `typeParams` are the callee's OWN
13
+ * `<...>` param names (bound by the caller's `$types` option); `classTypeParams`
14
+ * are the enclosing generic class's params (bound from the `self` receiver's
15
+ * `$types` field). Mirrors `define_function`'s `type_params` /
16
+ * `class_type_params` kwargs in the Python SDK. Omitted/empty ⇒ non-generic.
17
+ */
18
+ export interface GenericParams {
19
+ typeParams?: readonly string[];
20
+ classTypeParams?: readonly string[];
21
+ }
22
+ /**
23
+ * Factory for a free function or static method binding. Returns a callable
24
+ * that maps positional args to kwargs, encodes, calls the runtime, and decodes.
25
+ * `sync` returns the decoded value; `async` returns a `Promise` of it.
26
+ */
27
+ export declare function defineFunction(bamlFqn: string, mode: Mode, requiredParamNames: readonly string[], optionalParamNames?: readonly string[] | undefined, generics?: GenericParams | undefined): (...args: unknown[]) => unknown;
28
+ /**
29
+ * Receiver-binding factory for instance methods. `paramNames[0]` is always
30
+ * `"self"`. Codegen emits the binding as a class-field initializer
31
+ * `m = defineInstanceFunction(...).bind(this) as () => R;`, so `.bind(self)`
32
+ * captures the instance at construction time; the synthetic `self` param never
33
+ * appears in the surface type.
34
+ */
35
+ export declare function defineInstanceFunction(bamlFqn: string, mode: Mode, requiredParamNames: readonly string[], optionalParamNames?: readonly string[] | undefined, generics?: GenericParams | undefined): {
36
+ bind(self: unknown): (...args: unknown[]) => unknown;
37
+ };
38
+ //# sourceMappingURL=define_function.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"define_function.d.ts","sourceRoot":"","sources":["../typescript_src/define_function.ts"],"names":[],"mappings":"AAsBA,MAAM,MAAM,IAAI,GAAG,MAAM,GAAG,OAAO,CAAC;AAEpC,8EAA8E;AAC9E,eAAO,MAAM,KAAK,EAAE,OAAO,MAA6B,CAAC;AAEzD;;;;;;GAMG;AACH,MAAM,WAAW,aAAa;IAC1B,UAAU,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;IAC/B,eAAe,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;CACvC;AAoLD;;;;GAIG;AACH,wBAAgB,cAAc,CAC1B,OAAO,EAAE,MAAM,EACf,IAAI,EAAE,IAAI,EACV,kBAAkB,EAAE,SAAS,MAAM,EAAE,EACrC,kBAAkB,CAAC,EAAE,SAAS,MAAM,EAAE,GAAG,SAAS,EAClD,QAAQ,CAAC,EAAE,aAAa,GAAG,SAAS,GACrC,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,KAAK,OAAO,CA8CjC;AAED;;;;;;GAMG;AACH,wBAAgB,sBAAsB,CAClC,OAAO,EAAE,MAAM,EACf,IAAI,EAAE,IAAI,EACV,kBAAkB,EAAE,SAAS,MAAM,EAAE,EACrC,kBAAkB,CAAC,EAAE,SAAS,MAAM,EAAE,GAAG,SAAS,EAClD,QAAQ,CAAC,EAAE,aAAa,GAAG,SAAS,GACrC;IAAE,IAAI,CAAC,IAAI,EAAE,OAAO,GAAG,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,KAAK,OAAO,CAAA;CAAE,CA2D1D"}
@@ -0,0 +1,281 @@
1
+ /**
2
+ * THIS FILE IS AUTO-GENERATED — DO NOT EDIT BY HAND.
3
+ *
4
+ * Source: baml_language/crates/bridge_nodejs/typescript_src/
5
+ * Proto: baml_language/crates/bridge_ctypes/types/baml_bridge/cffi/v1/*.proto
6
+ * Build: cd baml_language/crates/bridge_nodejs && pnpm build:debug
7
+ */
8
+ // define_function.ts — runtime factories for BAML callables, the Node analog
9
+ // of `define_function` in sdks/python/src/baml_bridge/__init__.py.
10
+ //
11
+ // Generated SDK code emits, per BAML function:
12
+ // export const f = defineFunction("user.ns.f", "sync", ["a"]) as (a: A) => R;
13
+ // export const f_async = defineFunction("user.ns.f", "async", ["a"]) as (a: A) => Promise<R>;
14
+ // and per instance method (inside the class body):
15
+ // m = defineInstanceFunction("user.ns.C.m", "sync", ["self"]).bind(this) as () => R;
16
+ //
17
+ // The factory captures (fqn, mode, requiredNames, optionalNames) by closure;
18
+ // the returned callable zips positional args against requiredNames into kwargs,
19
+ // encodes it, calls the runtime, and decodes the result.
20
+ import { getRuntime, newFunctionCall as nativeNewFunctionCall, } from './native.js';
21
+ import { encodeCallArgs, decodeCallResult } from './proto.js';
22
+ import { lowerTypeToWireTy } from './wire_ty.js';
23
+ /** Sentinel for "argument not supplied" so optional kwargs can be skipped. */
24
+ export const UNSET = Symbol('baml.UNSET');
25
+ /**
26
+ * Resolve the caller's `$types` option onto the callee's own generic params, in
27
+ * declaration order. Mirrors Python's `_resolve_types_kwarg`: `$types` is an
28
+ * object keyed by param name and is required iff the callee declares its own
29
+ * generic params; it must bind exactly those params (no missing, no extras).
30
+ */
31
+ function resolveTypesOption(typesOpt, typeParams) {
32
+ if (typeParams.length === 0) {
33
+ if (typesOpt !== undefined && typesOpt !== null) {
34
+ throw new TypeError('$types is not accepted here: this function/method declares no generic type ' +
35
+ 'parameters of its own');
36
+ }
37
+ return [];
38
+ }
39
+ const example = `{ ${JSON.stringify(typeParams[0])}: 'int' }`;
40
+ if (typesOpt === undefined || typesOpt === null) {
41
+ throw new TypeError(`$types is required for this generic call: bind every type parameter in ` +
42
+ `${JSON.stringify(typeParams)} with an object, e.g. $types: ${example}`);
43
+ }
44
+ if (typeof typesOpt !== 'object' || Array.isArray(typesOpt)) {
45
+ throw new TypeError(`$types must be an object mapping type-parameter names to types (e.g. ` +
46
+ `$types: ${example}); got ${typeof typesOpt}`);
47
+ }
48
+ const obj = typesOpt;
49
+ const missing = typeParams.filter((n) => !(n in obj));
50
+ if (missing.length) {
51
+ throw new TypeError(`$types is missing binding(s) for ${JSON.stringify(missing)}: every type parameter ` +
52
+ `in ${JSON.stringify(typeParams)} must be bound.`);
53
+ }
54
+ const extra = Object.keys(obj).filter((k) => !typeParams.includes(k));
55
+ if (extra.length) {
56
+ throw new TypeError(`$types has unknown type parameter(s) ${JSON.stringify(extra)}; expected exactly ` +
57
+ `${JSON.stringify(typeParams)}.`);
58
+ }
59
+ return typeParams.map((n) => obj[n]);
60
+ }
61
+ /** The `$types` field of a generic receiver instance (its class TypeVar
62
+ * bindings), or `undefined` when the receiver carries none. The TS analog of
63
+ * Python's `pydantic_instance_type_args(self)`. */
64
+ function receiverTypes(self) {
65
+ if (self != null && typeof self === 'object') {
66
+ const t = self.$types;
67
+ if (t != null && typeof t === 'object') {
68
+ return t;
69
+ }
70
+ }
71
+ return undefined;
72
+ }
73
+ /**
74
+ * Build the named, order-preserving wire `type_args` for a generic call. Mirrors
75
+ * Python's `_build_type_args`: enclosing class params (recovered from the `self`
76
+ * receiver's `$types`) come first, then the callee's own params (`$types`
77
+ * option) — De Bruijn order. Class params are seeded only when the receiver
78
+ * actually carries them, so a non-generic receiver keeps the engine's
79
+ * recover-from-receiver behavior. Returns `[]` when the call binds nothing.
80
+ */
81
+ function buildTypeArgs(self, typesOpt, typeParams, classTypeParams) {
82
+ const wire = [];
83
+ const classTypes = classTypeParams.length ? receiverTypes(self) : undefined;
84
+ if (classTypes) {
85
+ for (const name of classTypeParams) {
86
+ wire.push([name, lowerTypeToWireTy(classTypes[name])]);
87
+ }
88
+ }
89
+ const resolved = resolveTypesOption(typesOpt, typeParams);
90
+ if (typeParams.length > 0) {
91
+ if (classTypeParams.length && !classTypes) {
92
+ // The method's own params sit after the class prefix in De Bruijn
93
+ // order; without recovered class args we can't position them.
94
+ throw new TypeError('$types on a generic method requires a generic receiver carrying its class type ' +
95
+ 'args (a `$types` field on the instance)');
96
+ }
97
+ typeParams.forEach((name, i) => {
98
+ wire.push([name, lowerTypeToWireTy(resolved[i])]);
99
+ });
100
+ }
101
+ return wire;
102
+ }
103
+ function newFunctionCall() {
104
+ return BigInt(nativeNewFunctionCall());
105
+ }
106
+ function attachCallContext(ctx, callId) {
107
+ ctx?._attachCallId(callId.toString());
108
+ return {
109
+ detach() {
110
+ ctx?._detachCallId(callId.toString());
111
+ },
112
+ };
113
+ }
114
+ function buildArgs(args, requiredParamNames, optionalParamNames) {
115
+ const positionalLimit = requiredParamNames.length;
116
+ if (args.length > positionalLimit + 1) {
117
+ throw new TypeError(`got ${args.length} positional arguments but only ${positionalLimit} positional ` +
118
+ `parameter names (${JSON.stringify(requiredParamNames)})`);
119
+ }
120
+ const built = {};
121
+ for (let i = 0; i < args.length && i < positionalLimit; i++) {
122
+ if (args[i] === UNSET)
123
+ continue;
124
+ built[requiredParamNames[i]] = args[i];
125
+ }
126
+ let ctx;
127
+ let types;
128
+ if (args.length > positionalLimit) {
129
+ const opts = args[positionalLimit];
130
+ if (opts === undefined || opts === UNSET) {
131
+ return { kwargs: built };
132
+ }
133
+ if (opts === null || Array.isArray(opts) || typeof opts !== 'object') {
134
+ throw new TypeError('optional arguments must be passed as an object');
135
+ }
136
+ const optionNames = new Set(optionalParamNames);
137
+ for (const [key, value] of Object.entries(opts)) {
138
+ if (key === '$ctx') {
139
+ if (value !== undefined && value !== UNSET) {
140
+ ctx = value;
141
+ }
142
+ continue;
143
+ }
144
+ if (key === '$types') {
145
+ // Generic-call TypeVar bindings — captured here and lowered to
146
+ // wire `type_args` by the factory, never sent as a kwarg value.
147
+ if (value !== undefined && value !== UNSET) {
148
+ types = value;
149
+ }
150
+ continue;
151
+ }
152
+ if (!optionNames.has(key)) {
153
+ throw new TypeError(`unknown optional argument ${JSON.stringify(key)}`);
154
+ }
155
+ if (value === undefined || value === UNSET)
156
+ continue;
157
+ built[key] = value;
158
+ }
159
+ }
160
+ return { kwargs: built, ctx, types };
161
+ }
162
+ /**
163
+ * Factory for a free function or static method binding. Returns a callable
164
+ * that maps positional args to kwargs, encodes, calls the runtime, and decodes.
165
+ * `sync` returns the decoded value; `async` returns a `Promise` of it.
166
+ */
167
+ export function defineFunction(bamlFqn, mode, requiredParamNames, optionalParamNames, generics) {
168
+ const requiredNames = [...requiredParamNames];
169
+ const optionNames = [...(optionalParamNames ?? [])];
170
+ // A free function / static method binds only its OWN generic params (a
171
+ // generic receiver is never in play here), so `classTypeParams` is unused.
172
+ const typeParams = generics?.typeParams ?? [];
173
+ const isGeneric = typeParams.length > 0;
174
+ // Eagerly reject `$types` on a non-generic call, matching the generic path's
175
+ // strict binding contract (mirrors Python's `is_generic` gate).
176
+ const typeArgsFor = (built) => isGeneric ? buildTypeArgs(undefined, built.types, typeParams, []) : [];
177
+ if (mode === 'sync') {
178
+ return (...args) => {
179
+ const built = buildArgs(args, requiredNames, optionNames);
180
+ const typeArgs = typeArgsFor(built);
181
+ const rt = getRuntime();
182
+ const callId = newFunctionCall();
183
+ const argsProto = encodeCallArgs(built.kwargs, { syncMode: true, callId, typeArgs });
184
+ const callCtxBinding = attachCallContext(built.ctx, callId);
185
+ let resultBytes;
186
+ try {
187
+ resultBytes = rt.callFunctionSync(bamlFqn, argsProto, null, null);
188
+ }
189
+ finally {
190
+ callCtxBinding.detach();
191
+ }
192
+ return decodeCallResult(resultBytes);
193
+ };
194
+ }
195
+ if (mode === 'async') {
196
+ return async (...args) => {
197
+ const built = buildArgs(args, requiredNames, optionNames);
198
+ const typeArgs = typeArgsFor(built);
199
+ const rt = getRuntime();
200
+ const callId = newFunctionCall();
201
+ const argsProto = encodeCallArgs(built.kwargs, { callId, typeArgs });
202
+ const callCtxBinding = attachCallContext(built.ctx, callId);
203
+ let resultBytes;
204
+ try {
205
+ resultBytes = await rt.callFunction(bamlFqn, argsProto, null, null);
206
+ }
207
+ finally {
208
+ callCtxBinding.detach();
209
+ }
210
+ return decodeCallResult(resultBytes);
211
+ };
212
+ }
213
+ throw new Error(`mode must be 'sync' or 'async', got ${JSON.stringify(mode)}`);
214
+ }
215
+ /**
216
+ * Receiver-binding factory for instance methods. `paramNames[0]` is always
217
+ * `"self"`. Codegen emits the binding as a class-field initializer
218
+ * `m = defineInstanceFunction(...).bind(this) as () => R;`, so `.bind(self)`
219
+ * captures the instance at construction time; the synthetic `self` param never
220
+ * appears in the surface type.
221
+ */
222
+ export function defineInstanceFunction(bamlFqn, mode, requiredParamNames, optionalParamNames, generics) {
223
+ const requiredNames = [...requiredParamNames];
224
+ const optionNames = [...(optionalParamNames ?? [])];
225
+ const selfName = requiredNames[0] ?? 'self';
226
+ const rest = requiredNames.slice(1);
227
+ // An instance method binds its own `<...>` params (caller's `$types`) AND
228
+ // the enclosing class's params, recovered from the `self` receiver's
229
+ // `$types` field. Mirrors Python's `class_type_params` for instance methods.
230
+ const typeParams = generics?.typeParams ?? [];
231
+ const classTypeParams = generics?.classTypeParams ?? [];
232
+ const isGeneric = typeParams.length > 0 || classTypeParams.length > 0;
233
+ const makeArgs = (self, args) => {
234
+ const built = buildArgs(args, rest, optionNames);
235
+ built.kwargs[selfName] = self;
236
+ return built;
237
+ };
238
+ return {
239
+ bind(self) {
240
+ const typeArgsFor = (built) => isGeneric ? buildTypeArgs(self, built.types, typeParams, classTypeParams) : [];
241
+ if (mode === 'sync') {
242
+ return (...args) => {
243
+ const built = makeArgs(self, args);
244
+ const typeArgs = typeArgsFor(built);
245
+ const rt = getRuntime();
246
+ const callId = newFunctionCall();
247
+ const argsProto = encodeCallArgs(built.kwargs, { syncMode: true, callId, typeArgs });
248
+ const callCtxBinding = attachCallContext(built.ctx, callId);
249
+ let resultBytes;
250
+ try {
251
+ resultBytes = rt.callFunctionSync(bamlFqn, argsProto, null, null);
252
+ }
253
+ finally {
254
+ callCtxBinding.detach();
255
+ }
256
+ return decodeCallResult(resultBytes);
257
+ };
258
+ }
259
+ if (mode === 'async') {
260
+ return async (...args) => {
261
+ const built = makeArgs(self, args);
262
+ const typeArgs = typeArgsFor(built);
263
+ const rt = getRuntime();
264
+ const callId = newFunctionCall();
265
+ const argsProto = encodeCallArgs(built.kwargs, { callId, typeArgs });
266
+ const callCtxBinding = attachCallContext(built.ctx, callId);
267
+ let resultBytes;
268
+ try {
269
+ resultBytes = await rt.callFunction(bamlFqn, argsProto, null, null);
270
+ }
271
+ finally {
272
+ callCtxBinding.detach();
273
+ }
274
+ return decodeCallResult(resultBytes);
275
+ };
276
+ }
277
+ throw new Error(`mode must be 'sync' or 'async', got ${JSON.stringify(mode)}`);
278
+ },
279
+ };
280
+ }
281
+ //# sourceMappingURL=define_function.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"define_function.js","sourceRoot":"","sources":["../typescript_src/define_function.ts"],"names":[],"mappings":"AAAA,6EAA6E;AAC7E,mEAAmE;AACnE,EAAE;AACF,+CAA+C;AAC/C,gFAAgF;AAChF,gGAAgG;AAChG,mDAAmD;AACnD,uFAAuF;AACvF,EAAE;AACF,6EAA6E;AAC7E,gFAAgF;AAChF,yDAAyD;AAEzD,OAAO,EAEH,UAAU,EACV,eAAe,IAAI,qBAAqB,GAC3C,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,cAAc,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAE9D,OAAO,EAAE,iBAAiB,EAAiB,MAAM,cAAc,CAAC;AAIhE,8EAA8E;AAC9E,MAAM,CAAC,MAAM,KAAK,GAAkB,MAAM,CAAC,YAAY,CAAC,CAAC;AAwBzD;;;;;GAKG;AACH,SAAS,kBAAkB,CAAC,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,MAAM,EAAE,EAAE,MAAM,EAAE,QAAQ,EAAE,CAAC,CAAC;oBACrE,MAAM,cAAc,GAAG,iBAAiB,CAAC,KAAK,CAAC,GAAG,EAAE,MAAM,CAAC,CAAC;oBAC5D,IAAI,WAAmB,CAAC;oBACxB,IAAI,CAAC;wBACD,WAAW,GAAG,MAAM,EAAE,CAAC,YAAY,CAAC,OAAO,EAAE,SAAS,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC;oBACxE,CAAC;4BAAS,CAAC;wBACP,cAAc,CAAC,MAAM,EAAE,CAAC;oBAC5B,CAAC;oBACD,OAAO,gBAAgB,CAAC,WAAW,CAAC,CAAC;gBACzC,CAAC,CAAC;YACN,CAAC;YACD,MAAM,IAAI,KAAK,CAAC,uCAAuC,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;QACnF,CAAC;KACJ,CAAC;AACN,CAAC"}
@@ -0,0 +1,51 @@
1
+ /**
2
+ * Structured detail carried by a thrown `BamlError` / `BamlPanic`, mirroring
3
+ * `bridge_python`'s `BamlError(value, baml_trace=..., class_name=...)`.
4
+ *
5
+ * - `value`: the fully decoded thrown BAML value (a generated class instance
6
+ * when the FQN is mapped, else a plain object / primitive).
7
+ * - `bamlTrace`: the pre-rendered `File "...", line N, in fn` frame strings
8
+ * from the BAML stack.
9
+ * - `className`: the thrown value's BAML FQN when known (e.g.
10
+ * `baml.json.JsonParseError`).
11
+ */
12
+ export interface BamlErrorDetail {
13
+ value?: unknown;
14
+ bamlTrace?: string[];
15
+ className?: string;
16
+ }
17
+ export declare class BamlError extends Error {
18
+ /** The decoded thrown BAML value, or `undefined` for SDK-internal errors. */
19
+ readonly value: unknown;
20
+ /** Pre-rendered BAML stack frames; empty for SDK-internal errors. */
21
+ readonly bamlTrace: string[];
22
+ /** The thrown value's BAML FQN, when known. */
23
+ readonly className: string | undefined;
24
+ constructor(message: string, detail?: BamlErrorDetail);
25
+ }
26
+ export declare class BamlInvalidArgumentError extends BamlError {
27
+ constructor(message: string);
28
+ }
29
+ export declare class BamlClientError extends BamlError {
30
+ constructor(message: string);
31
+ }
32
+ export declare class BamlCancelledError extends BamlError {
33
+ constructor(message: string);
34
+ }
35
+ export declare class BamlAbortError extends Error {
36
+ readonly reason: unknown;
37
+ constructor(message: string, options?: {
38
+ reason?: unknown;
39
+ });
40
+ }
41
+ /**
42
+ * Raised for SDK-setup failures and BAML-runtime panics — the Node analog of
43
+ * `bridge_python`'s `BamlPanic`. The in-call panic path (`decodeCallResult`'s
44
+ * `panic` arm) and the `getRuntime` not-initialized path both surface this,
45
+ * except clean process-exit panics, which exit after flushing telemetry.
46
+ */
47
+ export declare class BamlPanic extends BamlError {
48
+ constructor(message: string, detail?: BamlErrorDetail);
49
+ }
50
+ export declare function wrapNativeError(err: unknown): BamlError;
51
+ //# sourceMappingURL=errors.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../typescript_src/errors.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;GAUG;AACH,MAAM,WAAW,eAAe;IAC5B,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,SAAS,CAAC,EAAE,MAAM,EAAE,CAAC;IACrB,SAAS,CAAC,EAAE,MAAM,CAAC;CACtB;AAED,qBAAa,SAAU,SAAQ,KAAK;IAChC,6EAA6E;IAC7E,QAAQ,CAAC,KAAK,EAAE,OAAO,CAAC;IACxB,qEAAqE;IACrE,QAAQ,CAAC,SAAS,EAAE,MAAM,EAAE,CAAC;IAC7B,+CAA+C;IAC/C,QAAQ,CAAC,SAAS,EAAE,MAAM,GAAG,SAAS,CAAC;gBAE3B,OAAO,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,eAAe;CAOxD;AAED,qBAAa,wBAAyB,SAAQ,SAAS;gBACvC,OAAO,EAAE,MAAM;CAI9B;AAED,qBAAa,eAAgB,SAAQ,SAAS;gBAC9B,OAAO,EAAE,MAAM;CAI9B;AAED,qBAAa,kBAAmB,SAAQ,SAAS;gBACjC,OAAO,EAAE,MAAM;CAI9B;AAED,qBAAa,cAAe,SAAQ,KAAK;IACrC,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAC;gBAEb,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE;QAAE,MAAM,CAAC,EAAE,OAAO,CAAA;KAAE;CAK9D;AAED;;;;;GAKG;AACH,qBAAa,SAAU,SAAQ,SAAS;gBACxB,OAAO,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,eAAe;CAIxD;AAED,wBAAgB,eAAe,CAAC,GAAG,EAAE,OAAO,GAAG,SAAS,CAGvD"}
package/dist/errors.js ADDED
@@ -0,0 +1,67 @@
1
+ /**
2
+ * THIS FILE IS AUTO-GENERATED — DO NOT EDIT BY HAND.
3
+ *
4
+ * Source: baml_language/crates/bridge_nodejs/typescript_src/
5
+ * Proto: baml_language/crates/bridge_ctypes/types/baml_bridge/cffi/v1/*.proto
6
+ * Build: cd baml_language/crates/bridge_nodejs && pnpm build:debug
7
+ */
8
+ // errors.ts — mirrors bridge_python/python_src/baml_py/errors.py
9
+ export class BamlError extends Error {
10
+ /** The decoded thrown BAML value, or `undefined` for SDK-internal errors. */
11
+ value;
12
+ /** Pre-rendered BAML stack frames; empty for SDK-internal errors. */
13
+ bamlTrace;
14
+ /** The thrown value's BAML FQN, when known. */
15
+ className;
16
+ constructor(message, detail) {
17
+ super(message);
18
+ this.name = 'BamlError';
19
+ this.value = detail?.value;
20
+ this.bamlTrace = detail?.bamlTrace ? [...detail.bamlTrace] : [];
21
+ this.className = detail?.className;
22
+ }
23
+ }
24
+ export class BamlInvalidArgumentError extends BamlError {
25
+ constructor(message) {
26
+ super(message);
27
+ this.name = 'BamlInvalidArgumentError';
28
+ }
29
+ }
30
+ export class BamlClientError extends BamlError {
31
+ constructor(message) {
32
+ super(message);
33
+ this.name = 'BamlClientError';
34
+ }
35
+ }
36
+ export class BamlCancelledError extends BamlError {
37
+ constructor(message) {
38
+ super(message);
39
+ this.name = 'BamlCancelledError';
40
+ }
41
+ }
42
+ export class BamlAbortError extends Error {
43
+ reason;
44
+ constructor(message, options) {
45
+ super(message);
46
+ this.name = 'AbortError';
47
+ this.reason = options?.reason;
48
+ }
49
+ }
50
+ /**
51
+ * Raised for SDK-setup failures and BAML-runtime panics — the Node analog of
52
+ * `bridge_python`'s `BamlPanic`. The in-call panic path (`decodeCallResult`'s
53
+ * `panic` arm) and the `getRuntime` not-initialized path both surface this,
54
+ * except clean process-exit panics, which exit after flushing telemetry.
55
+ */
56
+ export class BamlPanic extends BamlError {
57
+ constructor(message, detail) {
58
+ super(message, detail);
59
+ this.name = 'BamlPanic';
60
+ }
61
+ }
62
+ export function wrapNativeError(err) {
63
+ if (!(err instanceof Error))
64
+ return new BamlError(String(err));
65
+ return new BamlError(err.message);
66
+ }
67
+ //# sourceMappingURL=errors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.js","sourceRoot":"","sources":["../typescript_src/errors.ts"],"names":[],"mappings":"AAAA,iEAAiE;AAmBjE,MAAM,OAAO,SAAU,SAAQ,KAAK;IAChC,6EAA6E;IACpE,KAAK,CAAU;IACxB,qEAAqE;IAC5D,SAAS,CAAW;IAC7B,+CAA+C;IACtC,SAAS,CAAqB;IAEvC,YAAY,OAAe,EAAE,MAAwB;QACjD,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,WAAW,CAAC;QACxB,IAAI,CAAC,KAAK,GAAG,MAAM,EAAE,KAAK,CAAC;QAC3B,IAAI,CAAC,SAAS,GAAG,MAAM,EAAE,SAAS,CAAC,CAAC,CAAC,CAAC,GAAG,MAAM,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;QAChE,IAAI,CAAC,SAAS,GAAG,MAAM,EAAE,SAAS,CAAC;IACvC,CAAC;CACJ;AAED,MAAM,OAAO,wBAAyB,SAAQ,SAAS;IACnD,YAAY,OAAe;QACvB,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,0BAA0B,CAAC;IAC3C,CAAC;CACJ;AAED,MAAM,OAAO,eAAgB,SAAQ,SAAS;IAC1C,YAAY,OAAe;QACvB,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,iBAAiB,CAAC;IAClC,CAAC;CACJ;AAED,MAAM,OAAO,kBAAmB,SAAQ,SAAS;IAC7C,YAAY,OAAe;QACvB,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,oBAAoB,CAAC;IACrC,CAAC;CACJ;AAED,MAAM,OAAO,cAAe,SAAQ,KAAK;IAC5B,MAAM,CAAU;IAEzB,YAAY,OAAe,EAAE,OAA8B;QACvD,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,YAAY,CAAC;QACzB,IAAI,CAAC,MAAM,GAAG,OAAO,EAAE,MAAM,CAAC;IAClC,CAAC;CACJ;AAED;;;;;GAKG;AACH,MAAM,OAAO,SAAU,SAAQ,SAAS;IACpC,YAAY,OAAe,EAAE,MAAwB;QACjD,KAAK,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;QACvB,IAAI,CAAC,IAAI,GAAG,WAAW,CAAC;IAC5B,CAAC;CACJ;AAED,MAAM,UAAU,eAAe,CAAC,GAAY;IACxC,IAAI,CAAC,CAAC,GAAG,YAAY,KAAK,CAAC;QAAE,OAAO,IAAI,SAAS,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC;IAC/D,OAAO,IAAI,SAAS,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;AACtC,CAAC"}
@@ -0,0 +1,9 @@
1
+ /**
2
+ * THIS FILE IS AUTO-GENERATED — DO NOT EDIT BY HAND.
3
+ *
4
+ * Source: baml_language/crates/bridge_nodejs/typescript_src/
5
+ * Proto: baml_language/crates/bridge_ctypes/types/baml_bridge/cffi/v1/*.proto
6
+ * Build: cd baml_language/crates/bridge_nodejs && pnpm build:debug
7
+ */
8
+ export declare function installFlushOnExit(): void;
9
+ //# sourceMappingURL=exit_hook.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"exit_hook.d.ts","sourceRoot":"","sources":["../typescript_src/exit_hook.ts"],"names":[],"mappings":"AASA,wBAAgB,kBAAkB,IAAI,IAAI,CAUzC"}
@@ -0,0 +1,27 @@
1
+ /**
2
+ * THIS FILE IS AUTO-GENERATED — DO NOT EDIT BY HAND.
3
+ *
4
+ * Source: baml_language/crates/bridge_nodejs/typescript_src/
5
+ * Proto: baml_language/crates/bridge_ctypes/types/baml_bridge/cffi/v1/*.proto
6
+ * Build: cd baml_language/crates/bridge_nodejs && pnpm build:debug
7
+ */
8
+ // exit_hook.ts — single-registration helper for flushEvents on process exit.
9
+ // Both index.ts and the CtxManager constructor used to call
10
+ // process.once('exit', …) independently; consolidate to one registration so
11
+ // `process.listenerCount('exit')` increases by exactly one per process.
12
+ import { flushEvents } from './native.js';
13
+ let installed = false;
14
+ export function installFlushOnExit() {
15
+ if (installed)
16
+ return;
17
+ installed = true;
18
+ process.once('exit', () => {
19
+ try {
20
+ flushEvents();
21
+ }
22
+ catch {
23
+ /* ignore */
24
+ }
25
+ });
26
+ }
27
+ //# sourceMappingURL=exit_hook.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"exit_hook.js","sourceRoot":"","sources":["../typescript_src/exit_hook.ts"],"names":[],"mappings":"AAAA,6EAA6E;AAC7E,4DAA4D;AAC5D,4EAA4E;AAC5E,wEAAwE;AAExE,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAE1C,IAAI,SAAS,GAAG,KAAK,CAAC;AAEtB,MAAM,UAAU,kBAAkB;IAC9B,IAAI,SAAS;QAAE,OAAO;IACtB,SAAS,GAAG,IAAI,CAAC;IACjB,OAAO,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,EAAE;QACtB,IAAI,CAAC;YACD,WAAW,EAAE,CAAC;QAClB,CAAC;QAAC,MAAM,CAAC;YACL,YAAY;QAChB,CAAC;IACL,CAAC,CAAC,CAAC;AACP,CAAC"}