-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
884 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { BamlSpan, RuntimeContextManager, BamlRuntime, BamlLogEvent } from './native'; | ||
export declare class BamlCtxManager { | ||
private rt; | ||
private ctx; | ||
constructor(rt: BamlRuntime); | ||
upsertTags(tags: Record<string, string>): void; | ||
cloneContext(): RuntimeContextManager; | ||
startTrace(name: string, args: Record<string, any>): [RuntimeContextManager, BamlSpan]; | ||
endTrace(span: BamlSpan, response: any): void; | ||
flush(): void; | ||
onLogEvent(callback: (event: BamlLogEvent) => void): void; | ||
traceFnSync<ReturnType, F extends (...args: any[]) => ReturnType>(name: string, func: F): F; | ||
traceFnAsync<ReturnType, F extends (...args: any[]) => Promise<ReturnType>>(name: string, func: F): F; | ||
} | ||
//# sourceMappingURL=async_context_vars.d.ts.map |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.BamlCtxManager = void 0; | ||
const native_1 = require("./native"); | ||
const async_hooks_1 = require("async_hooks"); | ||
class BamlCtxManager { | ||
rt; | ||
ctx; | ||
constructor(rt) { | ||
this.rt = rt; | ||
this.ctx = new async_hooks_1.AsyncLocalStorage(); | ||
this.ctx.enterWith(rt.createContextManager()); | ||
process.on('exit', () => { | ||
this.rt.flush(); | ||
}); | ||
} | ||
upsertTags(tags) { | ||
const manager = this.ctx.getStore(); | ||
manager.upsertTags(tags); | ||
} | ||
cloneContext() { | ||
let store = this.ctx.getStore(); | ||
if (store === undefined) { | ||
store = this.rt.createContextManager(); | ||
this.ctx.enterWith(store); | ||
} | ||
return store.deepClone(); | ||
} | ||
startTrace(name, args) { | ||
const mng = this.cloneContext(); | ||
return [mng, native_1.BamlSpan.new(this.rt, name, args, mng)]; | ||
} | ||
endTrace(span, response) { | ||
const manager = this.ctx.getStore(); | ||
if (!manager) { | ||
console.error('Context lost before span could be finished\n'); | ||
return; | ||
} | ||
try { | ||
span.finish(response, manager); | ||
} | ||
catch (e) { | ||
console.error('BAML internal error', e); | ||
} | ||
} | ||
flush() { | ||
this.rt.flush(); | ||
} | ||
onLogEvent(callback) { | ||
this.rt.setLogEventCallback((error, param) => { | ||
if (!error) { | ||
callback(param); | ||
} | ||
}); | ||
} | ||
traceFnSync(name, func) { | ||
return ((...args) => { | ||
const params = args.reduce((acc, arg, i) => ({ | ||
...acc, | ||
[`arg${i}`]: arg, // generic way to label args | ||
}), {}); | ||
const [mng, span] = this.startTrace(name, params); | ||
this.ctx.run(mng, () => { | ||
try { | ||
const response = func(...args); | ||
this.endTrace(span, response); | ||
return response; | ||
} | ||
catch (e) { | ||
this.endTrace(span, e); | ||
throw e; | ||
} | ||
}); | ||
}); | ||
} | ||
traceFnAsync(name, func) { | ||
const funcName = name; | ||
return (async (...args) => { | ||
const params = args.reduce((acc, arg, i) => ({ | ||
...acc, | ||
[`arg${i}`]: arg, // generic way to label args | ||
}), {}); | ||
const [mng, span] = this.startTrace(name, params); | ||
await this.ctx.run(mng, async () => { | ||
try { | ||
const response = await func(...args); | ||
this.endTrace(span, response); | ||
return response; | ||
} | ||
catch (e) { | ||
this.endTrace(span, e); | ||
throw e; | ||
} | ||
}); | ||
}); | ||
} | ||
} | ||
exports.BamlCtxManager = BamlCtxManager; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export { BamlRuntime, FunctionResult, FunctionResultStream, BamlImage as Image, BamlAudio as Audio, invoke_runtime_cli, } from './native'; | ||
export { BamlStream } from './stream'; | ||
export { BamlCtxManager } from './async_context_vars'; | ||
//# sourceMappingURL=index.d.ts.map |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.BamlCtxManager = exports.BamlStream = exports.invoke_runtime_cli = exports.Audio = exports.Image = exports.FunctionResultStream = exports.FunctionResult = exports.BamlRuntime = void 0; | ||
var native_1 = require("./native"); | ||
Object.defineProperty(exports, "BamlRuntime", { enumerable: true, get: function () { return native_1.BamlRuntime; } }); | ||
Object.defineProperty(exports, "FunctionResult", { enumerable: true, get: function () { return native_1.FunctionResult; } }); | ||
Object.defineProperty(exports, "FunctionResultStream", { enumerable: true, get: function () { return native_1.FunctionResultStream; } }); | ||
Object.defineProperty(exports, "Image", { enumerable: true, get: function () { return native_1.BamlImage; } }); | ||
Object.defineProperty(exports, "Audio", { enumerable: true, get: function () { return native_1.BamlAudio; } }); | ||
Object.defineProperty(exports, "invoke_runtime_cli", { enumerable: true, get: function () { return native_1.invoke_runtime_cli; } }); | ||
var stream_1 = require("./stream"); | ||
Object.defineProperty(exports, "BamlStream", { enumerable: true, get: function () { return stream_1.BamlStream; } }); | ||
var async_context_vars_1 = require("./async_context_vars"); | ||
Object.defineProperty(exports, "BamlCtxManager", { enumerable: true, get: function () { return async_context_vars_1.BamlCtxManager; } }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
/* auto-generated by NAPI-RS */ | ||
/* eslint-disable */ | ||
export class BamlAudio { | ||
static fromUrl(url: string): BamlAudio | ||
static fromBase64(mediaType: string, base64: string): BamlAudio | ||
isUrl(): boolean | ||
asUrl(): string | ||
asBase64(): [string, string] | ||
toJSON(): any | ||
} | ||
|
||
export class BamlImage { | ||
static fromUrl(url: string): BamlImage | ||
static fromBase64(mediaType: string, base64: string): BamlImage | ||
isUrl(): boolean | ||
asUrl(): string | ||
asBase64(): [string, string] | ||
toJSON(): any | ||
} | ||
|
||
export class BamlRuntime { | ||
static fromDirectory(directory: string, envVars: Record<string, string>): BamlRuntime | ||
static fromFiles(rootPath: string, files: Record<string, string>, envVars: Record<string, string>): BamlRuntime | ||
createContextManager(): RuntimeContextManager | ||
callFunction(functionName: string, args: { [string]: any }, ctx: RuntimeContextManager, tb?: TypeBuilder | undefined | null): Promise<FunctionResult> | ||
streamFunction(functionName: string, args: { [string]: any }, cb: (err: any, param: FunctionResult) => void, ctx: RuntimeContextManager, tb?: TypeBuilder | undefined | null): FunctionResultStream | ||
setLogEventCallback(func: (err: any, param: BamlLogEvent) => void): void | ||
flush(): void | ||
drainStats(): TraceStats | ||
} | ||
|
||
export class BamlSpan { | ||
static new(runtime: BamlRuntime, functionName: string, args: any, ctx: RuntimeContextManager): BamlSpan | ||
finish(result: any, ctx: RuntimeContextManager): any | ||
} | ||
|
||
export class ClassBuilder { | ||
field(): FieldType | ||
property(name: string): ClassPropertyBuilder | ||
} | ||
|
||
export class ClassPropertyBuilder { | ||
setType(fieldType: FieldType): ClassPropertyBuilder | ||
alias(alias?: string | undefined | null): ClassPropertyBuilder | ||
description(description?: string | undefined | null): ClassPropertyBuilder | ||
} | ||
|
||
export class EnumBuilder { | ||
value(name: string): EnumValueBuilder | ||
alias(alias?: string | undefined | null): EnumBuilder | ||
field(): FieldType | ||
} | ||
|
||
export class EnumValueBuilder { | ||
alias(alias?: string | undefined | null): EnumValueBuilder | ||
skip(skip?: boolean | undefined | null): EnumValueBuilder | ||
description(description?: string | undefined | null): EnumValueBuilder | ||
} | ||
|
||
export class FieldType { | ||
list(): FieldType | ||
optional(): FieldType | ||
} | ||
|
||
export class FunctionResult { | ||
parsed(): any | ||
} | ||
|
||
export class FunctionResultStream { | ||
onEvent(func: (err: any, param: FunctionResult) => void): void | ||
done(rctx: RuntimeContextManager): Promise<FunctionResult> | ||
} | ||
|
||
export class RuntimeContextManager { | ||
upsertTags(tags: any): void | ||
deepClone(): RuntimeContextManager | ||
} | ||
|
||
export class TraceStats { | ||
get failed(): number | ||
get started(): number | ||
get finalized(): number | ||
get submitted(): number | ||
get sent(): number | ||
get done(): number | ||
toJson(): string | ||
} | ||
|
||
export class TypeBuilder { | ||
constructor() | ||
getEnum(name: string): EnumBuilder | ||
getClass(name: string): ClassBuilder | ||
list(inner: FieldType): FieldType | ||
optional(inner: FieldType): FieldType | ||
string(): FieldType | ||
int(): FieldType | ||
float(): FieldType | ||
bool(): FieldType | ||
null(): FieldType | ||
} | ||
|
||
export interface BamlLogEvent { | ||
metadata: LogEventMetadata | ||
prompt?: string | ||
rawOutput?: string | ||
parsedOutput?: string | ||
startTime: string | ||
} | ||
|
||
export function invoke_runtime_cli(params: Array<string>): void | ||
|
||
export interface LogEventMetadata { | ||
eventId: string | ||
parentId?: string | ||
rootEventId: string | ||
} | ||
|
Oops, something went wrong.