@boundaryml/baml-bridge-web 0.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/dist/index.d.ts +30 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +57 -0
- package/dist/index.js.map +1 -0
- package/dist/native.d.ts +79 -0
- package/dist/native.d.ts.map +1 -0
- package/dist/native.js +148 -0
- package/dist/native.js.map +1 -0
- package/dist/shared/define_function.d.ts +31 -0
- package/dist/shared/define_function.d.ts.map +1 -0
- package/dist/shared/define_function.js +274 -0
- package/dist/shared/define_function.js.map +1 -0
- package/dist/shared/errors.d.ts +51 -0
- package/dist/shared/errors.d.ts.map +1 -0
- package/dist/shared/errors.js +60 -0
- package/dist/shared/errors.js.map +1 -0
- package/dist/shared/host_value_registry.d.ts +50 -0
- package/dist/shared/host_value_registry.d.ts.map +1 -0
- package/dist/shared/host_value_registry.js +134 -0
- package/dist/shared/host_value_registry.js.map +1 -0
- package/dist/shared/proto/baml_cffi.d.ts +6282 -0
- package/dist/shared/proto/baml_cffi.js +12482 -0
- package/dist/shared/proto.d.ts +55 -0
- package/dist/shared/proto.d.ts.map +1 -0
- package/dist/shared/proto.js +956 -0
- package/dist/shared/proto.js.map +1 -0
- package/dist/shared/stream.d.ts +16 -0
- package/dist/shared/stream.d.ts.map +1 -0
- package/dist/shared/stream.js +61 -0
- package/dist/shared/stream.js.map +1 -0
- package/dist/shared/typemap.d.ts +30 -0
- package/dist/shared/typemap.d.ts.map +1 -0
- package/dist/shared/typemap.js +103 -0
- package/dist/shared/typemap.js.map +1 -0
- package/dist/shared/wire_ty.d.ts +51 -0
- package/dist/shared/wire_ty.d.ts.map +1 -0
- package/dist/shared/wire_ty.js +140 -0
- package/dist/shared/wire_ty.js.map +1 -0
- package/dist/wasm/bridge_web_core.d.ts +174 -0
- package/dist/wasm/bridge_web_core.js +1277 -0
- package/dist/wasm/bridge_web_core_bg.wasm +0 -0
- package/dist/wasm/bridge_web_core_bg.wasm.d.ts +41 -0
- package/dist/wasm/package.json +19 -0
- package/dist/workerd/index.d.ts +30 -0
- package/dist/workerd/index.d.ts.map +1 -0
- package/dist/workerd/index.js +57 -0
- package/dist/workerd/index.js.map +1 -0
- package/dist/workerd/native.d.ts +79 -0
- package/dist/workerd/native.d.ts.map +1 -0
- package/dist/workerd/native.js +147 -0
- package/dist/workerd/shared/define_function.d.ts +31 -0
- package/dist/workerd/shared/define_function.d.ts.map +1 -0
- package/dist/workerd/shared/define_function.js +274 -0
- package/dist/workerd/shared/define_function.js.map +1 -0
- package/dist/workerd/shared/errors.d.ts +51 -0
- package/dist/workerd/shared/errors.d.ts.map +1 -0
- package/dist/workerd/shared/errors.js +60 -0
- package/dist/workerd/shared/errors.js.map +1 -0
- package/dist/workerd/shared/host_value_registry.d.ts +50 -0
- package/dist/workerd/shared/host_value_registry.d.ts.map +1 -0
- package/dist/workerd/shared/host_value_registry.js +134 -0
- package/dist/workerd/shared/host_value_registry.js.map +1 -0
- package/dist/workerd/shared/proto/baml_cffi.d.ts +6282 -0
- package/dist/workerd/shared/proto/baml_cffi.js +12482 -0
- package/dist/workerd/shared/proto.d.ts +55 -0
- package/dist/workerd/shared/proto.d.ts.map +1 -0
- package/dist/workerd/shared/proto.js +956 -0
- package/dist/workerd/shared/proto.js.map +1 -0
- package/dist/workerd/shared/stream.d.ts +16 -0
- package/dist/workerd/shared/stream.d.ts.map +1 -0
- package/dist/workerd/shared/stream.js +61 -0
- package/dist/workerd/shared/stream.js.map +1 -0
- package/dist/workerd/shared/typemap.d.ts +30 -0
- package/dist/workerd/shared/typemap.d.ts.map +1 -0
- package/dist/workerd/shared/typemap.js +103 -0
- package/dist/workerd/shared/typemap.js.map +1 -0
- package/dist/workerd/shared/wire_ty.d.ts +51 -0
- package/dist/workerd/shared/wire_ty.d.ts.map +1 -0
- package/dist/workerd/shared/wire_ty.js +140 -0
- package/dist/workerd/shared/wire_ty.js.map +1 -0
- package/dist/workerd-wasm/bridge_web_core.d.ts +108 -0
- package/dist/workerd-wasm/bridge_web_core.js +14 -0
- package/dist/workerd-wasm/bridge_web_core_bg.js +1178 -0
- package/dist/workerd-wasm/bridge_web_core_bg.wasm +0 -0
- package/dist/workerd-wasm/bridge_web_core_bg.wasm.d.ts +41 -0
- package/dist/workerd-wasm/package.json +21 -0
- package/package.json +31 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { BamlCallContext, BamlRuntime, Collector, HostSpanManager } from "./native.js";
|
|
2
|
+
export { BamlAudio, BamlCallContext, BamlHandle, BamlImage, BamlPdf, BamlRuntime, BamlVideo, Collector, FunctionLog, HostSpanManager, LLMCall, Timing, Usage, flushEvents, getRuntime, getVersion, newFunctionCall } from "./native.js";
|
|
3
|
+
export { BamlStream } from "./shared/stream.js";
|
|
4
|
+
export { BamlTypeMap, getTypeMap, setTypeMap } from "./shared/typemap.js";
|
|
5
|
+
export { defineFunction, defineInstanceFunction, UNSET } from "./shared/define_function.js";
|
|
6
|
+
export type { GenericParams } from "./shared/define_function.js";
|
|
7
|
+
export { Never, lowerTypeToWireTy } from "./shared/wire_ty.js";
|
|
8
|
+
export type { BamlClassCtor, BamlPrimitiveToken, BamlType } from "./shared/wire_ty.js";
|
|
9
|
+
export { BamlAbortError, BamlCancelledError, BamlClientError, BamlError, BamlInvalidArgumentError, BamlPanic } from "./shared/errors.js";
|
|
10
|
+
export { decodeCallResult, encodeCallArgs } from "./shared/proto.js";
|
|
11
|
+
export declare function initializeRuntimeFromBytecode(bytecode: Uint8Array): void;
|
|
12
|
+
export declare function initializeRuntime(srcDir: string, files: Record<string, string>): void;
|
|
13
|
+
export declare class FunctionResult {
|
|
14
|
+
private readonly value;
|
|
15
|
+
constructor(value: unknown);
|
|
16
|
+
result(): unknown;
|
|
17
|
+
toString(): string;
|
|
18
|
+
}
|
|
19
|
+
export declare function callFunctionSync(rt: BamlRuntime, functionName: string, kwargs: Record<string, unknown>, ctx?: HostSpanManager, collectors?: Collector[], callCtx?: BamlCallContext): FunctionResult;
|
|
20
|
+
export declare function callFunction(rt: BamlRuntime, functionName: string, kwargs: Record<string, unknown>, ctx?: HostSpanManager, collectors?: Collector[], callCtx?: BamlCallContext): Promise<FunctionResult>;
|
|
21
|
+
export declare class CtxManager {
|
|
22
|
+
private readonly context;
|
|
23
|
+
get(): HostSpanManager;
|
|
24
|
+
allowReset(): boolean;
|
|
25
|
+
reset(): void;
|
|
26
|
+
cloneContext(): HostSpanManager;
|
|
27
|
+
upsertTags(tags: Record<string, string>): void;
|
|
28
|
+
}
|
|
29
|
+
export declare function cancelFunctionCall(callId: bigint): boolean;
|
|
30
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../typescript_src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,WAAW,EAAE,SAAS,EAAE,eAAe,EAAwG,MAAM,aAAa,CAAC;AAG7L,OAAO,EAAE,SAAS,EAAE,eAAe,EAAE,UAAU,EAAE,SAAS,EAAE,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,SAAS,EAAE,WAAW,EAAE,eAAe,EAAE,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,WAAW,EAAE,UAAU,EAAE,UAAU,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AACxO,OAAO,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAChD,OAAO,EAAE,WAAW,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AAC1E,OAAO,EAAE,cAAc,EAAE,sBAAsB,EAAE,KAAK,EAAE,MAAM,6BAA6B,CAAC;AAC5F,YAAY,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAC;AACjE,OAAO,EAAE,KAAK,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AAC/D,YAAY,EAAE,aAAa,EAAE,kBAAkB,EAAE,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M,EAAE,MAAM,CAAC,GAAG,IAAI;CAC/C;AAED,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAE1D"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { BamlRuntime, HostSpanManager, cancelFunctionCall as nativeCancelFunctionCall, newFunctionCall as nativeNewFunctionCall } from "./native.js";
|
|
2
|
+
import { decodeCallResult, encodeCallArgs } from "./shared/proto.js";
|
|
3
|
+
export { BamlAudio, BamlCallContext, BamlHandle, BamlImage, BamlPdf, BamlRuntime, BamlVideo, Collector, FunctionLog, HostSpanManager, LLMCall, Timing, Usage, flushEvents, getRuntime, getVersion, newFunctionCall } from "./native.js";
|
|
4
|
+
export { BamlStream } from "./shared/stream.js";
|
|
5
|
+
export { BamlTypeMap, getTypeMap, setTypeMap } from "./shared/typemap.js";
|
|
6
|
+
export { defineFunction, defineInstanceFunction, UNSET } from "./shared/define_function.js";
|
|
7
|
+
export { Never, lowerTypeToWireTy } from "./shared/wire_ty.js";
|
|
8
|
+
export { BamlAbortError, BamlCancelledError, BamlClientError, BamlError, BamlInvalidArgumentError, BamlPanic } from "./shared/errors.js";
|
|
9
|
+
export { decodeCallResult, encodeCallArgs } from "./shared/proto.js";
|
|
10
|
+
export function initializeRuntimeFromBytecode(bytecode) {
|
|
11
|
+
BamlRuntime.initializeRuntimeFromBytecode(bytecode);
|
|
12
|
+
}
|
|
13
|
+
export function initializeRuntime(srcDir, files) {
|
|
14
|
+
BamlRuntime.initializeRuntime(srcDir, files);
|
|
15
|
+
}
|
|
16
|
+
export class FunctionResult {
|
|
17
|
+
value;
|
|
18
|
+
constructor(value) {
|
|
19
|
+
this.value = value;
|
|
20
|
+
}
|
|
21
|
+
result() { return this.value; }
|
|
22
|
+
toString() { return `FunctionResult(${JSON.stringify(this.value)})`; }
|
|
23
|
+
}
|
|
24
|
+
export function callFunctionSync(rt, functionName, kwargs, ctx, collectors, callCtx) {
|
|
25
|
+
const callId = nativeNewFunctionCall();
|
|
26
|
+
const args = encodeCallArgs(kwargs, { syncMode: true, callId });
|
|
27
|
+
callCtx?._attachCallId(callId.toString());
|
|
28
|
+
try {
|
|
29
|
+
return new FunctionResult(decodeCallResult(rt.callFunctionSync(functionName, args, ctx ?? null, collectors ?? null)));
|
|
30
|
+
}
|
|
31
|
+
finally {
|
|
32
|
+
callCtx?._detachCallId(callId.toString());
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
export async function callFunction(rt, functionName, kwargs, ctx, collectors, callCtx) {
|
|
36
|
+
const callId = nativeNewFunctionCall();
|
|
37
|
+
const args = encodeCallArgs(kwargs, { callId });
|
|
38
|
+
callCtx?._attachCallId(callId.toString());
|
|
39
|
+
try {
|
|
40
|
+
return new FunctionResult(decodeCallResult(await rt.callFunction(functionName, args, ctx ?? null, collectors ?? null)));
|
|
41
|
+
}
|
|
42
|
+
finally {
|
|
43
|
+
callCtx?._detachCallId(callId.toString());
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
export class CtxManager {
|
|
47
|
+
context = new HostSpanManager();
|
|
48
|
+
get() { return this.context; }
|
|
49
|
+
allowReset() { return true; }
|
|
50
|
+
reset() { }
|
|
51
|
+
cloneContext() { return this.context.deepClone(); }
|
|
52
|
+
upsertTags(tags) { this.context.upsertTags(tags); }
|
|
53
|
+
}
|
|
54
|
+
export function cancelFunctionCall(callId) {
|
|
55
|
+
return nativeCancelFunctionCall(callId);
|
|
56
|
+
}
|
|
57
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../typescript_src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAmB,WAAW,EAAa,eAAe,EAAE,kBAAkB,IAAI,wBAAwB,EAAc,eAAe,IAAI,qBAAqB,EAAE,MAAM,aAAa,CAAC;AAC7L,OAAO,EAAE,gBAAgB,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC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AC;IACjD,GAAG,KAAsB,OAAO,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC;IAC/C,UAAU,KAAc,OAAO,IAAI,CAAC,CAAC,CAAC;IACtC,KAAK,KAAU,CAAC;IAChB,YAAY,KAAsB,OAAO,IAAI,CAAC,OAAO,CAAC,SAAS,EAAE,CAAC,CAAC,CAAC;IACpE,UAAU,CAAC,IAA4B,IAAU,IAAI,CAAC,OAAO,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;CAClF;AAED,MAAM,UAAU,kBAAkB,CAAC,MAAc;IAC/C,OAAO,wBAAwB,CAAC,MAAM,CAAC,CAAC;AAC1C,CAAC"}
|
package/dist/native.d.ts
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
export interface HandleKey {
|
|
2
|
+
low: number;
|
|
3
|
+
high: number;
|
|
4
|
+
}
|
|
5
|
+
export declare class BamlHandle {
|
|
6
|
+
readonly key: HandleKey;
|
|
7
|
+
readonly handleType: number;
|
|
8
|
+
constructor(key: HandleKey, handleType: number);
|
|
9
|
+
clone(): BamlHandle;
|
|
10
|
+
_cloneKeyForWire(): HandleKey;
|
|
11
|
+
}
|
|
12
|
+
declare class BamlMedia {
|
|
13
|
+
private readonly handle;
|
|
14
|
+
protected constructor(handle: BamlHandle);
|
|
15
|
+
static fromUrl(_url: string, _mimeType?: string | null): BamlMedia;
|
|
16
|
+
static fromFile(_file: string, _mimeType?: string | null): BamlMedia;
|
|
17
|
+
static fromBase64(_base64: string, _mimeType?: string | null): BamlMedia;
|
|
18
|
+
static _fromHandle(handle: BamlHandle): BamlMedia;
|
|
19
|
+
_toHandle(): BamlHandle;
|
|
20
|
+
url(): string | null;
|
|
21
|
+
file(): string | null;
|
|
22
|
+
base64(): string;
|
|
23
|
+
mimeType(): string | null;
|
|
24
|
+
}
|
|
25
|
+
export declare class BamlImage extends BamlMedia {
|
|
26
|
+
}
|
|
27
|
+
export declare class BamlAudio extends BamlMedia {
|
|
28
|
+
}
|
|
29
|
+
export declare class BamlVideo extends BamlMedia {
|
|
30
|
+
}
|
|
31
|
+
export declare class BamlPdf extends BamlMedia {
|
|
32
|
+
}
|
|
33
|
+
export declare class BamlCallContext {
|
|
34
|
+
private callIds;
|
|
35
|
+
private isAborted;
|
|
36
|
+
abort(): void;
|
|
37
|
+
get aborted(): boolean;
|
|
38
|
+
_attachCallId(callId: string): void;
|
|
39
|
+
_detachCallId(callId: string): void;
|
|
40
|
+
}
|
|
41
|
+
export declare class HostSpanManager {
|
|
42
|
+
enter(_name: string, _args: unknown): void;
|
|
43
|
+
exitOk(): void;
|
|
44
|
+
exitError(_message: string): void;
|
|
45
|
+
upsertTags(_tags: Record<string, string>): void;
|
|
46
|
+
deepClone(): HostSpanManager;
|
|
47
|
+
contextDepth(): number;
|
|
48
|
+
}
|
|
49
|
+
export declare class Timing {
|
|
50
|
+
}
|
|
51
|
+
export declare class Usage {
|
|
52
|
+
}
|
|
53
|
+
export declare class LlmCall {
|
|
54
|
+
}
|
|
55
|
+
export { LlmCall as LLMCall };
|
|
56
|
+
export declare class FunctionLog {
|
|
57
|
+
}
|
|
58
|
+
export declare class Collector {
|
|
59
|
+
constructor(_name?: string | null);
|
|
60
|
+
}
|
|
61
|
+
export declare class BamlRuntime {
|
|
62
|
+
static initializeRuntimeFromBytecode(bytecode: Uint8Array): BamlRuntime;
|
|
63
|
+
static initializeRuntime(_rootPath: string, _files: Record<string, string>): BamlRuntime;
|
|
64
|
+
callFunctionSync(functionName: string, encodedArgs: Uint8Array, _ctx?: HostSpanManager | null, _collectors?: Collector[] | null): Uint8Array;
|
|
65
|
+
callFunction(functionName: string, encodedArgs: Uint8Array, _ctx?: HostSpanManager | null, _collectors?: Collector[] | null): Promise<Uint8Array>;
|
|
66
|
+
}
|
|
67
|
+
export declare function getRuntime(): BamlRuntime;
|
|
68
|
+
export declare function newFunctionCall(): bigint;
|
|
69
|
+
export declare function cancelFunctionCall(callId: bigint | string): boolean;
|
|
70
|
+
export declare function flushEvents(): void;
|
|
71
|
+
export declare function getVersion(): string;
|
|
72
|
+
export declare function mintHostValueKey(): HandleKey;
|
|
73
|
+
export declare function registerHostValueReleaseCallback(callback: (key: HandleKey) => void): void;
|
|
74
|
+
export declare function registerHostCallable(callback: (callId: number, args: Uint8Array) => void): HandleKey;
|
|
75
|
+
export declare function releaseHostCallable(key: HandleKey): void;
|
|
76
|
+
export declare function completeHostCall(callId: number, isError: number, content: Uint8Array): void;
|
|
77
|
+
export declare function _seedFunctionRefHandle(_globalIndex: number): [HandleKey, number];
|
|
78
|
+
export declare function _seedGenericMediaHandle(): [HandleKey, number];
|
|
79
|
+
//# sourceMappingURL=native.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"native.d.ts","sourceRoot":"","sources":["../typescript_src/native.ts"],"names":[],"mappings":"AAuDA,MAAM,WAAW,SAAS;IAAG,GAAG,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAC;CAAE;AAMzD,qBAAa,UAAU;aACO,GAAG,EAAE,SAAS;aAAkB,UAAU,EAAE,MAAM;gBAAlD,GAAG,EAAE,SAAS,EAAkB,UAAU,EAAE,MAAM;IAC9E,KAAK,IAAI,UAAU;IACnB,gBAAgB,IAAI,SAAS;CAC9B;AAED,cAAM,SAAS;IACS,OAAO,CAAC,QAAQ,CAAC,MAAM;IAA7C,SAAS,aAA8B,MAAM,EAAE,UAAU;IACzD,MAAM,CAAC,OAAO,CAAC,IAAI,EAAE,MAAM,EAAE,SAAS,CAAC,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS;IAClE,MAAM,CAAC,QAAQ,CAAC,KAAK,EAAE,MAAM,EAAE,SAAS,CAAC,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS;IACpE,MAAM,CAAC,UAAU,CAAC,OAAO,EAAE,MAAM,EAAE,SAAS,CAAC,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS;IACxE,MAAM,CAAC,WAAW,CAAC,MAAM,EAAE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AAC,SAAS,EAAE,MAAM,CAAC,CAAmE"}
|
package/dist/native.js
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import initWasm, { callFunction as callWasmFunction, callFunctionSync as callWasmFunctionSync, cancelFunctionCall as cancelWasmFunctionCall, completeWebHostCall, mintWebHostValueKey, newFunctionCall as newWasmFunctionCall, registerWebHostCallable, registerWebHostValueReleaseCallback, releaseWebHostCallable, stageRuntimeBytecode, } from "./wasm/bridge_web_core.js";
|
|
2
|
+
await initWasm();
|
|
3
|
+
async function browserFetch(_callId, method, url, headersJson, body) {
|
|
4
|
+
const headers = JSON.parse(headersJson);
|
|
5
|
+
const response = await fetch(url, {
|
|
6
|
+
method,
|
|
7
|
+
headers,
|
|
8
|
+
body: body.length === 0 || method === "GET" || method === "HEAD" ? undefined : body,
|
|
9
|
+
});
|
|
10
|
+
const responseHeaders = {};
|
|
11
|
+
response.headers.forEach((value, key) => { responseHeaders[key] = value; });
|
|
12
|
+
return {
|
|
13
|
+
status: response.status,
|
|
14
|
+
headersJson: JSON.stringify(responseHeaders),
|
|
15
|
+
url: response.url,
|
|
16
|
+
bodyPromise: response.text(),
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
const unsupportedProcess = async () => {
|
|
20
|
+
throw new Error("process execution is not available in a browser");
|
|
21
|
+
};
|
|
22
|
+
const browserCallbacks = {
|
|
23
|
+
fetch: browserFetch,
|
|
24
|
+
env: (key) => globalThis.BAML_ENV?.[key],
|
|
25
|
+
input: async (_requestId, promptText) => globalThis.prompt?.(promptText ?? "") ?? "",
|
|
26
|
+
exec: unsupportedProcess,
|
|
27
|
+
shell: unsupportedProcess,
|
|
28
|
+
lsp_send_notification: () => { },
|
|
29
|
+
lsp_send_response: () => { },
|
|
30
|
+
lsp_make_request: () => { },
|
|
31
|
+
playground_send_notification: () => { },
|
|
32
|
+
host_dispatch: (key) => { throw new Error(`unknown browser host callable ${key}`); },
|
|
33
|
+
};
|
|
34
|
+
function keyFromBigint(value) {
|
|
35
|
+
return { low: Number(BigInt.asIntN(32, value)), high: Number(BigInt.asIntN(32, value >> 32n)) };
|
|
36
|
+
}
|
|
37
|
+
export class BamlHandle {
|
|
38
|
+
key;
|
|
39
|
+
handleType;
|
|
40
|
+
constructor(key, handleType) {
|
|
41
|
+
this.key = key;
|
|
42
|
+
this.handleType = handleType;
|
|
43
|
+
}
|
|
44
|
+
clone() { return new BamlHandle(this.key, this.handleType); }
|
|
45
|
+
_cloneKeyForWire() { return this.key; }
|
|
46
|
+
}
|
|
47
|
+
class BamlMedia {
|
|
48
|
+
handle;
|
|
49
|
+
constructor(handle) {
|
|
50
|
+
this.handle = handle;
|
|
51
|
+
}
|
|
52
|
+
static fromUrl(_url, _mimeType) { throw new Error("browser media construction is not implemented yet"); }
|
|
53
|
+
static fromFile(_file, _mimeType) { throw new Error("browser media construction is not implemented yet"); }
|
|
54
|
+
static fromBase64(_base64, _mimeType) { throw new Error("browser media construction is not implemented yet"); }
|
|
55
|
+
static _fromHandle(handle) { return new this(handle); }
|
|
56
|
+
_toHandle() { return this.handle.clone(); }
|
|
57
|
+
url() { return null; }
|
|
58
|
+
file() { return null; }
|
|
59
|
+
base64() { throw new Error("browser media access is not implemented yet"); }
|
|
60
|
+
mimeType() { return null; }
|
|
61
|
+
}
|
|
62
|
+
export class BamlImage extends BamlMedia {
|
|
63
|
+
}
|
|
64
|
+
export class BamlAudio extends BamlMedia {
|
|
65
|
+
}
|
|
66
|
+
export class BamlVideo extends BamlMedia {
|
|
67
|
+
}
|
|
68
|
+
export class BamlPdf extends BamlMedia {
|
|
69
|
+
}
|
|
70
|
+
export class BamlCallContext {
|
|
71
|
+
callIds = new Set();
|
|
72
|
+
isAborted = false;
|
|
73
|
+
abort() {
|
|
74
|
+
this.isAborted = true;
|
|
75
|
+
for (const callId of this.callIds)
|
|
76
|
+
cancelWasmFunctionCall(callId);
|
|
77
|
+
}
|
|
78
|
+
get aborted() { return this.isAborted; }
|
|
79
|
+
_attachCallId(callId) {
|
|
80
|
+
const id = BigInt(callId);
|
|
81
|
+
this.callIds.add(id);
|
|
82
|
+
if (this.isAborted)
|
|
83
|
+
cancelWasmFunctionCall(id);
|
|
84
|
+
}
|
|
85
|
+
_detachCallId(callId) { this.callIds.delete(BigInt(callId)); }
|
|
86
|
+
}
|
|
87
|
+
export class HostSpanManager {
|
|
88
|
+
enter(_name, _args) { }
|
|
89
|
+
exitOk() { }
|
|
90
|
+
exitError(_message) { }
|
|
91
|
+
upsertTags(_tags) { }
|
|
92
|
+
deepClone() { return new HostSpanManager(); }
|
|
93
|
+
contextDepth() { return 0; }
|
|
94
|
+
}
|
|
95
|
+
export class Timing {
|
|
96
|
+
}
|
|
97
|
+
export class Usage {
|
|
98
|
+
}
|
|
99
|
+
export class LlmCall {
|
|
100
|
+
}
|
|
101
|
+
export { LlmCall as LLMCall };
|
|
102
|
+
export class FunctionLog {
|
|
103
|
+
}
|
|
104
|
+
export class Collector {
|
|
105
|
+
constructor(_name) { }
|
|
106
|
+
}
|
|
107
|
+
export class BamlRuntime {
|
|
108
|
+
static initializeRuntimeFromBytecode(bytecode) {
|
|
109
|
+
stageRuntimeBytecode(bytecode, browserCallbacks);
|
|
110
|
+
runtime = new BamlRuntime();
|
|
111
|
+
return runtime;
|
|
112
|
+
}
|
|
113
|
+
static initializeRuntime(_rootPath, _files) {
|
|
114
|
+
throw new Error("browser source initialization is not implemented; use bytecode");
|
|
115
|
+
}
|
|
116
|
+
callFunctionSync(functionName, encodedArgs, _ctx, _collectors) {
|
|
117
|
+
return callWasmFunctionSync(functionName, encodedArgs);
|
|
118
|
+
}
|
|
119
|
+
callFunction(functionName, encodedArgs, _ctx, _collectors) {
|
|
120
|
+
return callWasmFunction(functionName, encodedArgs);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
let runtime;
|
|
124
|
+
let hostRelease;
|
|
125
|
+
export function getRuntime() {
|
|
126
|
+
if (!runtime)
|
|
127
|
+
throw new Error("BAML runtime has not been initialized");
|
|
128
|
+
return runtime;
|
|
129
|
+
}
|
|
130
|
+
export function newFunctionCall() { return newWasmFunctionCall(); }
|
|
131
|
+
export function cancelFunctionCall(callId) { return cancelWasmFunctionCall(BigInt(callId)); }
|
|
132
|
+
export function flushEvents() { }
|
|
133
|
+
export function getVersion() { return "0.0.0-web"; }
|
|
134
|
+
export function mintHostValueKey() { return keyFromBigint(mintWebHostValueKey()); }
|
|
135
|
+
export function registerHostValueReleaseCallback(callback) {
|
|
136
|
+
hostRelease = callback;
|
|
137
|
+
registerWebHostValueReleaseCallback((key) => hostRelease?.(keyFromBigint(key)));
|
|
138
|
+
}
|
|
139
|
+
export function registerHostCallable(callback) { return keyFromBigint(registerWebHostCallable(callback)); }
|
|
140
|
+
export function releaseHostCallable(key) {
|
|
141
|
+
const value = BigInt.asUintN(64, BigInt(key.high) << 32n | BigInt.asUintN(32, BigInt(key.low)));
|
|
142
|
+
releaseWebHostCallable(value);
|
|
143
|
+
hostRelease?.(key);
|
|
144
|
+
}
|
|
145
|
+
export function completeHostCall(callId, isError, content) { completeWebHostCall(callId, isError, content); }
|
|
146
|
+
export function _seedFunctionRefHandle(_globalIndex) { throw new Error("test handle seeding is not implemented yet"); }
|
|
147
|
+
export function _seedGenericMediaHandle() { throw new Error("test handle seeding is not implemented yet"); }
|
|
148
|
+
//# sourceMappingURL=native.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"native.js","sourceRoot":"","sources":["../typescript_src/native.ts"],"names":[],"mappings":"AAAA,OAAO,QAAQ,EAAE,EACf,YAAY,IAAI,gBAAgB,EAChC,gBAAgB,IAAI,oBAAoB,EACxC,kBAAkB,IAAI,sBAAsB,EAC5C,mBAAmB,EACnB,mBAAmB,EACnB,eAAe,IAAI,mBAAmB,EACtC,uBAAuB,EACvB,mCAAmC,EACnC,sBAAsB,EACtB,oBAAoB,GACrB,MAAM,2BAA2B,CAAC;AAEnC,MAAM,QAAQ,EAAE,CAAC;AAEjB,KAAK,UAAU,YAA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AM,EAAE,OAAO,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC;AAC/I,MAAM,UAAU,sBAAsB,CAAC,YAAoB,IAAyB,MAAM,IAAI,KAAK,CAAC,4CAA4C,CAAC,CAAC,CAAC,CAAC;AACpJ,MAAM,UAAU,uBAAuB,KAA0B,MAAM,IAAI,KAAK,CAAC,4CAA4C,CAAC,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export type Mode = 'sync' | 'async';
|
|
2
|
+
/** Sentinel for "argument not supplied" so optional kwargs can be skipped. */
|
|
3
|
+
export declare const UNSET: unique symbol;
|
|
4
|
+
/**
|
|
5
|
+
* Generic-binding contract for a callable. `typeParams` are the callee's OWN
|
|
6
|
+
* `<...>` param names (bound by the caller's `$types` option); `classTypeParams`
|
|
7
|
+
* are the enclosing generic class's params (bound from the `self` receiver's
|
|
8
|
+
* `$types` field). Mirrors `define_function`'s `type_params` /
|
|
9
|
+
* `class_type_params` kwargs in the Python SDK. Omitted/empty ⇒ non-generic.
|
|
10
|
+
*/
|
|
11
|
+
export interface GenericParams {
|
|
12
|
+
typeParams?: readonly string[];
|
|
13
|
+
classTypeParams?: readonly string[];
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Factory for a free function or static method binding. Returns a callable
|
|
17
|
+
* that maps positional args to kwargs, encodes, calls the runtime, and decodes.
|
|
18
|
+
* `sync` returns the decoded value; `async` returns a `Promise` of it.
|
|
19
|
+
*/
|
|
20
|
+
export declare function defineFunction(bamlFqn: string, mode: Mode, requiredParamNames: readonly string[], optionalParamNames?: readonly string[] | undefined, generics?: GenericParams | undefined): (...args: unknown[]) => unknown;
|
|
21
|
+
/**
|
|
22
|
+
* Receiver-binding factory for instance methods. `paramNames[0]` is always
|
|
23
|
+
* `"self"`. Codegen emits the binding as a class-field initializer
|
|
24
|
+
* `m = defineInstanceFunction(...).bind(this) as () => R;`, so `.bind(self)`
|
|
25
|
+
* captures the instance at construction time; the synthetic `self` param never
|
|
26
|
+
* appears in the surface type.
|
|
27
|
+
*/
|
|
28
|
+
export declare function defineInstanceFunction(bamlFqn: string, mode: Mode, requiredParamNames: readonly string[], optionalParamNames?: readonly string[] | undefined, generics?: GenericParams | undefined): {
|
|
29
|
+
bind(self: unknown): (...args: unknown[]) => unknown;
|
|
30
|
+
};
|
|
31
|
+
//# sourceMappingURL=define_function.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"define_function.d.ts","sourceRoot":"","sources":["../../typescript_src/shared/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,274 @@
|
|
|
1
|
+
// define_function.ts — runtime factories for BAML callables, the Node analog
|
|
2
|
+
// of `define_function` in sdks/python/src/baml_bridge/__init__.py.
|
|
3
|
+
//
|
|
4
|
+
// Generated SDK code emits, per BAML function:
|
|
5
|
+
// export const f = defineFunction("user.ns.f", "sync", ["a"]) as (a: A) => R;
|
|
6
|
+
// export const f_async = defineFunction("user.ns.f", "async", ["a"]) as (a: A) => Promise<R>;
|
|
7
|
+
// and per instance method (inside the class body):
|
|
8
|
+
// m = defineInstanceFunction("user.ns.C.m", "sync", ["self"]).bind(this) as () => R;
|
|
9
|
+
//
|
|
10
|
+
// The factory captures (fqn, mode, requiredNames, optionalNames) by closure;
|
|
11
|
+
// the returned callable zips positional args against requiredNames into kwargs,
|
|
12
|
+
// encodes it, calls the runtime, and decodes the result.
|
|
13
|
+
import { getRuntime, newFunctionCall as nativeNewFunctionCall, } from '../native.js';
|
|
14
|
+
import { encodeCallArgs, decodeCallResult } from './proto.js';
|
|
15
|
+
import { lowerTypeToWireTy } from './wire_ty.js';
|
|
16
|
+
/** Sentinel for "argument not supplied" so optional kwargs can be skipped. */
|
|
17
|
+
export const UNSET = Symbol('baml.UNSET');
|
|
18
|
+
/**
|
|
19
|
+
* Resolve the caller's `$types` option onto the callee's own generic params, in
|
|
20
|
+
* declaration order. Mirrors Python's `_resolve_types_kwarg`: `$types` is an
|
|
21
|
+
* object keyed by param name and is required iff the callee declares its own
|
|
22
|
+
* generic params; it must bind exactly those params (no missing, no extras).
|
|
23
|
+
*/
|
|
24
|
+
function resolveTypesOption(typesOpt, typeParams) {
|
|
25
|
+
if (typeParams.length === 0) {
|
|
26
|
+
if (typesOpt !== undefined && typesOpt !== null) {
|
|
27
|
+
throw new TypeError('$types is not accepted here: this function/method declares no generic type ' +
|
|
28
|
+
'parameters of its own');
|
|
29
|
+
}
|
|
30
|
+
return [];
|
|
31
|
+
}
|
|
32
|
+
const example = `{ ${JSON.stringify(typeParams[0])}: 'int' }`;
|
|
33
|
+
if (typesOpt === undefined || typesOpt === null) {
|
|
34
|
+
throw new TypeError(`$types is required for this generic call: bind every type parameter in ` +
|
|
35
|
+
`${JSON.stringify(typeParams)} with an object, e.g. $types: ${example}`);
|
|
36
|
+
}
|
|
37
|
+
if (typeof typesOpt !== 'object' || Array.isArray(typesOpt)) {
|
|
38
|
+
throw new TypeError(`$types must be an object mapping type-parameter names to types (e.g. ` +
|
|
39
|
+
`$types: ${example}); got ${typeof typesOpt}`);
|
|
40
|
+
}
|
|
41
|
+
const obj = typesOpt;
|
|
42
|
+
const missing = typeParams.filter((n) => !(n in obj));
|
|
43
|
+
if (missing.length) {
|
|
44
|
+
throw new TypeError(`$types is missing binding(s) for ${JSON.stringify(missing)}: every type parameter ` +
|
|
45
|
+
`in ${JSON.stringify(typeParams)} must be bound.`);
|
|
46
|
+
}
|
|
47
|
+
const extra = Object.keys(obj).filter((k) => !typeParams.includes(k));
|
|
48
|
+
if (extra.length) {
|
|
49
|
+
throw new TypeError(`$types has unknown type parameter(s) ${JSON.stringify(extra)}; expected exactly ` +
|
|
50
|
+
`${JSON.stringify(typeParams)}.`);
|
|
51
|
+
}
|
|
52
|
+
return typeParams.map((n) => obj[n]);
|
|
53
|
+
}
|
|
54
|
+
/** The `$types` field of a generic receiver instance (its class TypeVar
|
|
55
|
+
* bindings), or `undefined` when the receiver carries none. The TS analog of
|
|
56
|
+
* Python's `pydantic_instance_type_args(self)`. */
|
|
57
|
+
function receiverTypes(self) {
|
|
58
|
+
if (self != null && typeof self === 'object') {
|
|
59
|
+
const t = self.$types;
|
|
60
|
+
if (t != null && typeof t === 'object') {
|
|
61
|
+
return t;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
return undefined;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Build the named, order-preserving wire `type_args` for a generic call. Mirrors
|
|
68
|
+
* Python's `_build_type_args`: enclosing class params (recovered from the `self`
|
|
69
|
+
* receiver's `$types`) come first, then the callee's own params (`$types`
|
|
70
|
+
* option) — De Bruijn order. Class params are seeded only when the receiver
|
|
71
|
+
* actually carries them, so a non-generic receiver keeps the engine's
|
|
72
|
+
* recover-from-receiver behavior. Returns `[]` when the call binds nothing.
|
|
73
|
+
*/
|
|
74
|
+
function buildTypeArgs(self, typesOpt, typeParams, classTypeParams) {
|
|
75
|
+
const wire = [];
|
|
76
|
+
const classTypes = classTypeParams.length ? receiverTypes(self) : undefined;
|
|
77
|
+
if (classTypes) {
|
|
78
|
+
for (const name of classTypeParams) {
|
|
79
|
+
wire.push([name, lowerTypeToWireTy(classTypes[name])]);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
const resolved = resolveTypesOption(typesOpt, typeParams);
|
|
83
|
+
if (typeParams.length > 0) {
|
|
84
|
+
if (classTypeParams.length && !classTypes) {
|
|
85
|
+
// The method's own params sit after the class prefix in De Bruijn
|
|
86
|
+
// order; without recovered class args we can't position them.
|
|
87
|
+
throw new TypeError('$types on a generic method requires a generic receiver carrying its class type ' +
|
|
88
|
+
'args (a `$types` field on the instance)');
|
|
89
|
+
}
|
|
90
|
+
typeParams.forEach((name, i) => {
|
|
91
|
+
wire.push([name, lowerTypeToWireTy(resolved[i])]);
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
return wire;
|
|
95
|
+
}
|
|
96
|
+
function newFunctionCall() {
|
|
97
|
+
return BigInt(nativeNewFunctionCall());
|
|
98
|
+
}
|
|
99
|
+
function attachCallContext(ctx, callId) {
|
|
100
|
+
ctx?._attachCallId(callId.toString());
|
|
101
|
+
return {
|
|
102
|
+
detach() {
|
|
103
|
+
ctx?._detachCallId(callId.toString());
|
|
104
|
+
},
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
function buildArgs(args, requiredParamNames, optionalParamNames) {
|
|
108
|
+
const positionalLimit = requiredParamNames.length;
|
|
109
|
+
if (args.length > positionalLimit + 1) {
|
|
110
|
+
throw new TypeError(`got ${args.length} positional arguments but only ${positionalLimit} positional ` +
|
|
111
|
+
`parameter names (${JSON.stringify(requiredParamNames)})`);
|
|
112
|
+
}
|
|
113
|
+
const built = {};
|
|
114
|
+
for (let i = 0; i < args.length && i < positionalLimit; i++) {
|
|
115
|
+
if (args[i] === UNSET)
|
|
116
|
+
continue;
|
|
117
|
+
built[requiredParamNames[i]] = args[i];
|
|
118
|
+
}
|
|
119
|
+
let ctx;
|
|
120
|
+
let types;
|
|
121
|
+
if (args.length > positionalLimit) {
|
|
122
|
+
const opts = args[positionalLimit];
|
|
123
|
+
if (opts === undefined || opts === UNSET) {
|
|
124
|
+
return { kwargs: built };
|
|
125
|
+
}
|
|
126
|
+
if (opts === null || Array.isArray(opts) || typeof opts !== 'object') {
|
|
127
|
+
throw new TypeError('optional arguments must be passed as an object');
|
|
128
|
+
}
|
|
129
|
+
const optionNames = new Set(optionalParamNames);
|
|
130
|
+
for (const [key, value] of Object.entries(opts)) {
|
|
131
|
+
if (key === '$ctx') {
|
|
132
|
+
if (value !== undefined && value !== UNSET) {
|
|
133
|
+
ctx = value;
|
|
134
|
+
}
|
|
135
|
+
continue;
|
|
136
|
+
}
|
|
137
|
+
if (key === '$types') {
|
|
138
|
+
// Generic-call TypeVar bindings — captured here and lowered to
|
|
139
|
+
// wire `type_args` by the factory, never sent as a kwarg value.
|
|
140
|
+
if (value !== undefined && value !== UNSET) {
|
|
141
|
+
types = value;
|
|
142
|
+
}
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
if (!optionNames.has(key)) {
|
|
146
|
+
throw new TypeError(`unknown optional argument ${JSON.stringify(key)}`);
|
|
147
|
+
}
|
|
148
|
+
if (value === undefined || value === UNSET)
|
|
149
|
+
continue;
|
|
150
|
+
built[key] = value;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
return { kwargs: built, ctx, types };
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Factory for a free function or static method binding. Returns a callable
|
|
157
|
+
* that maps positional args to kwargs, encodes, calls the runtime, and decodes.
|
|
158
|
+
* `sync` returns the decoded value; `async` returns a `Promise` of it.
|
|
159
|
+
*/
|
|
160
|
+
export function defineFunction(bamlFqn, mode, requiredParamNames, optionalParamNames, generics) {
|
|
161
|
+
const requiredNames = [...requiredParamNames];
|
|
162
|
+
const optionNames = [...(optionalParamNames ?? [])];
|
|
163
|
+
// A free function / static method binds only its OWN generic params (a
|
|
164
|
+
// generic receiver is never in play here), so `classTypeParams` is unused.
|
|
165
|
+
const typeParams = generics?.typeParams ?? [];
|
|
166
|
+
const isGeneric = typeParams.length > 0;
|
|
167
|
+
// Eagerly reject `$types` on a non-generic call, matching the generic path's
|
|
168
|
+
// strict binding contract (mirrors Python's `is_generic` gate).
|
|
169
|
+
const typeArgsFor = (built) => isGeneric ? buildTypeArgs(undefined, built.types, typeParams, []) : [];
|
|
170
|
+
if (mode === 'sync') {
|
|
171
|
+
return (...args) => {
|
|
172
|
+
const built = buildArgs(args, requiredNames, optionNames);
|
|
173
|
+
const typeArgs = typeArgsFor(built);
|
|
174
|
+
const rt = getRuntime();
|
|
175
|
+
const callId = newFunctionCall();
|
|
176
|
+
const argsProto = encodeCallArgs(built.kwargs, { syncMode: true, callId, typeArgs });
|
|
177
|
+
const callCtxBinding = attachCallContext(built.ctx, callId);
|
|
178
|
+
let resultBytes;
|
|
179
|
+
try {
|
|
180
|
+
resultBytes = rt.callFunctionSync(bamlFqn, argsProto, null, null);
|
|
181
|
+
}
|
|
182
|
+
finally {
|
|
183
|
+
callCtxBinding.detach();
|
|
184
|
+
}
|
|
185
|
+
return decodeCallResult(resultBytes);
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
if (mode === 'async') {
|
|
189
|
+
return async (...args) => {
|
|
190
|
+
const built = buildArgs(args, requiredNames, optionNames);
|
|
191
|
+
const typeArgs = typeArgsFor(built);
|
|
192
|
+
const rt = getRuntime();
|
|
193
|
+
const callId = newFunctionCall();
|
|
194
|
+
const argsProto = encodeCallArgs(built.kwargs, { callId, typeArgs });
|
|
195
|
+
const callCtxBinding = attachCallContext(built.ctx, callId);
|
|
196
|
+
let resultBytes;
|
|
197
|
+
try {
|
|
198
|
+
resultBytes = await rt.callFunction(bamlFqn, argsProto, null, null);
|
|
199
|
+
}
|
|
200
|
+
finally {
|
|
201
|
+
callCtxBinding.detach();
|
|
202
|
+
}
|
|
203
|
+
return decodeCallResult(resultBytes);
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
throw new Error(`mode must be 'sync' or 'async', got ${JSON.stringify(mode)}`);
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Receiver-binding factory for instance methods. `paramNames[0]` is always
|
|
210
|
+
* `"self"`. Codegen emits the binding as a class-field initializer
|
|
211
|
+
* `m = defineInstanceFunction(...).bind(this) as () => R;`, so `.bind(self)`
|
|
212
|
+
* captures the instance at construction time; the synthetic `self` param never
|
|
213
|
+
* appears in the surface type.
|
|
214
|
+
*/
|
|
215
|
+
export function defineInstanceFunction(bamlFqn, mode, requiredParamNames, optionalParamNames, generics) {
|
|
216
|
+
const requiredNames = [...requiredParamNames];
|
|
217
|
+
const optionNames = [...(optionalParamNames ?? [])];
|
|
218
|
+
const selfName = requiredNames[0] ?? 'self';
|
|
219
|
+
const rest = requiredNames.slice(1);
|
|
220
|
+
// An instance method binds its own `<...>` params (caller's `$types`) AND
|
|
221
|
+
// the enclosing class's params, recovered from the `self` receiver's
|
|
222
|
+
// `$types` field. Mirrors Python's `class_type_params` for instance methods.
|
|
223
|
+
const typeParams = generics?.typeParams ?? [];
|
|
224
|
+
const classTypeParams = generics?.classTypeParams ?? [];
|
|
225
|
+
const isGeneric = typeParams.length > 0 || classTypeParams.length > 0;
|
|
226
|
+
const makeArgs = (self, args) => {
|
|
227
|
+
const built = buildArgs(args, rest, optionNames);
|
|
228
|
+
built.kwargs[selfName] = self;
|
|
229
|
+
return built;
|
|
230
|
+
};
|
|
231
|
+
return {
|
|
232
|
+
bind(self) {
|
|
233
|
+
const typeArgsFor = (built) => isGeneric ? buildTypeArgs(self, built.types, typeParams, classTypeParams) : [];
|
|
234
|
+
if (mode === 'sync') {
|
|
235
|
+
return (...args) => {
|
|
236
|
+
const built = makeArgs(self, args);
|
|
237
|
+
const typeArgs = typeArgsFor(built);
|
|
238
|
+
const rt = getRuntime();
|
|
239
|
+
const callId = newFunctionCall();
|
|
240
|
+
const argsProto = encodeCallArgs(built.kwargs, { syncMode: true, callId, typeArgs });
|
|
241
|
+
const callCtxBinding = attachCallContext(built.ctx, callId);
|
|
242
|
+
let resultBytes;
|
|
243
|
+
try {
|
|
244
|
+
resultBytes = rt.callFunctionSync(bamlFqn, argsProto, null, null);
|
|
245
|
+
}
|
|
246
|
+
finally {
|
|
247
|
+
callCtxBinding.detach();
|
|
248
|
+
}
|
|
249
|
+
return decodeCallResult(resultBytes);
|
|
250
|
+
};
|
|
251
|
+
}
|
|
252
|
+
if (mode === 'async') {
|
|
253
|
+
return async (...args) => {
|
|
254
|
+
const built = makeArgs(self, args);
|
|
255
|
+
const typeArgs = typeArgsFor(built);
|
|
256
|
+
const rt = getRuntime();
|
|
257
|
+
const callId = newFunctionCall();
|
|
258
|
+
const argsProto = encodeCallArgs(built.kwargs, { callId, typeArgs });
|
|
259
|
+
const callCtxBinding = attachCallContext(built.ctx, callId);
|
|
260
|
+
let resultBytes;
|
|
261
|
+
try {
|
|
262
|
+
resultBytes = await rt.callFunction(bamlFqn, argsProto, null, null);
|
|
263
|
+
}
|
|
264
|
+
finally {
|
|
265
|
+
callCtxBinding.detach();
|
|
266
|
+
}
|
|
267
|
+
return decodeCallResult(resultBytes);
|
|
268
|
+
};
|
|
269
|
+
}
|
|
270
|
+
throw new Error(`mode must be 'sync' or 'async', got ${JSON.stringify(mode)}`);
|
|
271
|
+
},
|
|
272
|
+
};
|
|
273
|
+
}
|
|
274
|
+
//# sourceMappingURL=define_function.js.map
|