@gammatech/aijsx 0.5.0-beta.1 → 0.5.0-dev.2024-03-12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -0
- package/dist/index.d.mts +81 -398
- package/dist/index.d.ts +81 -398
- package/dist/index.js +1127 -1049
- package/dist/index.mjs +1121 -1044
- package/dist/jsx-dev-runtime-LS-DZfkv.d.mts +356 -0
- package/dist/jsx-dev-runtime-LS-DZfkv.d.ts +356 -0
- package/dist/jsx-dev-runtime.d.mts +1 -2
- package/dist/jsx-dev-runtime.d.ts +1 -2
- package/dist/jsx-runtime.d.mts +1 -29
- package/dist/jsx-runtime.d.ts +1 -29
- package/package.json +4 -3
- package/dist/createElement--C_Hwi_c.d.mts +0 -222
- package/dist/createElement--C_Hwi_c.d.ts +0 -222
|
@@ -0,0 +1,356 @@
|
|
|
1
|
+
import { ZodTypeAny, ZodObject, ZodRawShape } from 'zod';
|
|
2
|
+
|
|
3
|
+
type Literal = string | number | null | undefined | boolean;
|
|
4
|
+
interface RenderResult extends AsyncGenerator<string, void> {
|
|
5
|
+
then(onFulfilled: (value: string) => string | PromiseLike<string>, onRejected?: (reason: any) => string | PromiseLike<string>): PromiseLike<string>;
|
|
6
|
+
}
|
|
7
|
+
interface Context<T> {
|
|
8
|
+
Provider: AIComponent<{
|
|
9
|
+
children: AINode;
|
|
10
|
+
value: T;
|
|
11
|
+
}>;
|
|
12
|
+
defaultValue: T;
|
|
13
|
+
key: symbol;
|
|
14
|
+
}
|
|
15
|
+
type AIComponent<P> = (props: P, context: RenderContext) => Renderable;
|
|
16
|
+
declare const attachedContextSymbol: unique symbol;
|
|
17
|
+
interface AIElement<P> {
|
|
18
|
+
/** The tag associated with this {@link AIElement}. */
|
|
19
|
+
tag: AIComponent<P>;
|
|
20
|
+
/** The component properties. */
|
|
21
|
+
props: P;
|
|
22
|
+
/** A function that renders this {@link AIElement} to a {@link Renderable}. */
|
|
23
|
+
render: (ctx: RenderContext) => Renderable;
|
|
24
|
+
/** The {@link RenderContext} associated with this {@link Element}. */
|
|
25
|
+
[attachedContextSymbol]?: Record<symbol, any>;
|
|
26
|
+
}
|
|
27
|
+
type AINode = Literal | AIElement<any> | AINode[];
|
|
28
|
+
type Renderable = AINode | PromiseLike<Renderable> | AsyncGenerator<string, void>;
|
|
29
|
+
type PropsOfAIComponent<T extends AIComponent<any>> = T extends AIComponent<infer P> ? P : never;
|
|
30
|
+
|
|
31
|
+
type EvaluatorPrimitive = string | number | boolean;
|
|
32
|
+
type EvaluatorResult = {
|
|
33
|
+
key: string;
|
|
34
|
+
result: EvaluatorPrimitive | EvaluatorPrimitive[];
|
|
35
|
+
passes: boolean;
|
|
36
|
+
description?: string;
|
|
37
|
+
debug?: string;
|
|
38
|
+
};
|
|
39
|
+
type EvaluatorFn<V extends Record<string, any> = Record<string, any>, O = any> = (variables: V, result: O) => Promise<EvaluatorResult | EvaluatorResult[]>;
|
|
40
|
+
type OutputParser<O> = {
|
|
41
|
+
schema: ZodTypeAny;
|
|
42
|
+
parse: (result: string) => O;
|
|
43
|
+
};
|
|
44
|
+
interface Prompt<V extends Record<string, any> = Record<string, any>> {
|
|
45
|
+
key: string;
|
|
46
|
+
component: AIComponent<V>;
|
|
47
|
+
schema: ZodObject<ZodRawShape>;
|
|
48
|
+
evaluators: EvaluatorFn<V>[];
|
|
49
|
+
messageSchema: ZodTypeAny | null;
|
|
50
|
+
outputSchema: ZodTypeAny;
|
|
51
|
+
metadata: Record<string, any>;
|
|
52
|
+
}
|
|
53
|
+
interface PromptParsed<V extends Record<string, any> = Record<string, any>, O = any> extends Prompt<V> {
|
|
54
|
+
evaluators: EvaluatorFn<V, O>[];
|
|
55
|
+
messageSchema: null;
|
|
56
|
+
parse: (result: string) => O;
|
|
57
|
+
}
|
|
58
|
+
interface FunctionChain<V extends Record<string, any> = Record<string, any>, R extends NotAsyncGenerator<any> = any> {
|
|
59
|
+
key: string;
|
|
60
|
+
chainType: 'function';
|
|
61
|
+
run: (vars: V, context: RenderContext) => R;
|
|
62
|
+
schema: ZodObject<ZodRawShape>;
|
|
63
|
+
messageSchema: null;
|
|
64
|
+
outputSchema: ZodTypeAny;
|
|
65
|
+
}
|
|
66
|
+
interface StreamChain<V extends Record<string, any> = Record<string, any>, R extends AsyncGenerator<any, any, any> = AsyncGenerator<any, any, any>> {
|
|
67
|
+
key: string;
|
|
68
|
+
chainType: 'stream';
|
|
69
|
+
run: (vars: V, context: RenderContext) => R;
|
|
70
|
+
schema: ZodObject<ZodRawShape>;
|
|
71
|
+
messageSchema: ZodTypeAny;
|
|
72
|
+
outputSchema: null;
|
|
73
|
+
}
|
|
74
|
+
type IsAsyncGenerator<T> = T extends AsyncGenerator<any, any, any> ? true : false;
|
|
75
|
+
type NotAsyncGenerator<T> = IsAsyncGenerator<T> extends true ? never : T;
|
|
76
|
+
|
|
77
|
+
type SpanContext = {
|
|
78
|
+
traceId: string;
|
|
79
|
+
spanId: string;
|
|
80
|
+
};
|
|
81
|
+
type SpanAttributes = Record<string, any>;
|
|
82
|
+
type SpanStatus = 'unset' | 'ok' | 'error';
|
|
83
|
+
interface ReadableSpan<Attrs extends SpanAttributes = SpanAttributes> {
|
|
84
|
+
name: string;
|
|
85
|
+
status: SpanStatus;
|
|
86
|
+
spanContext: SpanContext;
|
|
87
|
+
parentSpanId: string | null;
|
|
88
|
+
attributes: Attrs;
|
|
89
|
+
events: SpanEvent[];
|
|
90
|
+
startTime: string;
|
|
91
|
+
endTime: string | null;
|
|
92
|
+
ended: boolean;
|
|
93
|
+
duration: number;
|
|
94
|
+
}
|
|
95
|
+
interface Span<Attrs extends SpanAttributes = SpanAttributes> extends ReadableSpan<Attrs> {
|
|
96
|
+
setAttributes(attributes: Partial<Attrs>): this;
|
|
97
|
+
end(endTime?: [number, number]): void;
|
|
98
|
+
addEvent(name: string, attributes: SpanAttributes, startTime?: [number, number]): this;
|
|
99
|
+
recordException(exception: Error | string, time?: [number, number]): this;
|
|
100
|
+
}
|
|
101
|
+
interface SpanEvent {
|
|
102
|
+
name: string;
|
|
103
|
+
timestamp: string;
|
|
104
|
+
time: [number, number];
|
|
105
|
+
attributes: SpanAttributes;
|
|
106
|
+
}
|
|
107
|
+
interface TracingContextKey<_T> {
|
|
108
|
+
key: symbol;
|
|
109
|
+
}
|
|
110
|
+
interface TracingContext {
|
|
111
|
+
/**
|
|
112
|
+
* Get a value from the context.
|
|
113
|
+
*
|
|
114
|
+
* @param key key which identifies a context value
|
|
115
|
+
*/
|
|
116
|
+
getValue<T>(key: TracingContextKey<T>): T | undefined;
|
|
117
|
+
/**
|
|
118
|
+
* Create a new context which inherits from this context and has
|
|
119
|
+
* the given key set to the given value.
|
|
120
|
+
*
|
|
121
|
+
* @param key context key for which to set the value
|
|
122
|
+
* @param value value to set for the given key
|
|
123
|
+
*/
|
|
124
|
+
setValue<T>(key: TracingContextKey<T>, value: T): TracingContext;
|
|
125
|
+
/**
|
|
126
|
+
* Return a new context which inherits from this context but does
|
|
127
|
+
* not contain a value for the given key.
|
|
128
|
+
*
|
|
129
|
+
* @param key context key for which to clear a value
|
|
130
|
+
*/
|
|
131
|
+
deleteValue<T>(key: TracingContextKey<T>): TracingContext;
|
|
132
|
+
}
|
|
133
|
+
interface TracingContextManager {
|
|
134
|
+
/**
|
|
135
|
+
* Get the current active context
|
|
136
|
+
*/
|
|
137
|
+
active(): TracingContext;
|
|
138
|
+
/**
|
|
139
|
+
* Run the fn callback with object set as the current active context
|
|
140
|
+
* @param context Any object to set as the current active context
|
|
141
|
+
* @param fn A callback to be immediately run within a specific context
|
|
142
|
+
* @param thisArg optional receiver to be used for calling fn
|
|
143
|
+
* @param args optional arguments forwarded to fn
|
|
144
|
+
*/
|
|
145
|
+
with<A extends unknown[], F extends (...args: A) => ReturnType<F>>(context: TracingContext, fn: F, ...args: A): ReturnType<F>;
|
|
146
|
+
/**
|
|
147
|
+
* Bind an object as the current context (or a specific one)
|
|
148
|
+
* @param [context] Optionally specify the context which you want to assign
|
|
149
|
+
* @param target Any object to which a context need to be set
|
|
150
|
+
*/
|
|
151
|
+
bind<T>(context: TracingContext, target: T): T;
|
|
152
|
+
/**
|
|
153
|
+
* Enable context management
|
|
154
|
+
*/
|
|
155
|
+
enable(): this;
|
|
156
|
+
/**
|
|
157
|
+
* Disable context management
|
|
158
|
+
*/
|
|
159
|
+
disable(): this;
|
|
160
|
+
}
|
|
161
|
+
interface SpanProcessor {
|
|
162
|
+
/**
|
|
163
|
+
* Called when a {@link Span} is started, if the `span.isRecording()`
|
|
164
|
+
* returns true.
|
|
165
|
+
* @param span the Span that just started.
|
|
166
|
+
*/
|
|
167
|
+
onStart?(span: ReadableSpan): void;
|
|
168
|
+
onSpanEvent?(event: SpanEvent, span: ReadableSpan): void;
|
|
169
|
+
onSpanException?(exceptionEvent: SpanEvent, span: ReadableSpan): void;
|
|
170
|
+
/**
|
|
171
|
+
* Called when a {@link ReadableSpan} is ended, if the `span.isRecording()`
|
|
172
|
+
* returns true.
|
|
173
|
+
* @param span the Span that just ended.
|
|
174
|
+
*/
|
|
175
|
+
onEnd(span: ReadableSpan): void;
|
|
176
|
+
}
|
|
177
|
+
interface Tracer {
|
|
178
|
+
getActiveSpan(): Span | null;
|
|
179
|
+
trace<F extends (span: Span) => ReturnType<F>>(name: string, attributes: SpanAttributes, fn: F): ReturnType<F>;
|
|
180
|
+
}
|
|
181
|
+
interface SpanExporter {
|
|
182
|
+
export(spans: ReadableSpan[]): Promise<void>;
|
|
183
|
+
shutdown?(): Promise<void>;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
type ContextValues = Record<symbol, any>;
|
|
187
|
+
type RenderOptions = {
|
|
188
|
+
preserveTags?: boolean;
|
|
189
|
+
renderedProps?: {
|
|
190
|
+
[tagName: string]: {
|
|
191
|
+
[propName: string]: boolean;
|
|
192
|
+
};
|
|
193
|
+
};
|
|
194
|
+
};
|
|
195
|
+
interface RenderContext {
|
|
196
|
+
tracer: Tracer;
|
|
197
|
+
logger: Logger;
|
|
198
|
+
getContext<T>(context: Context<T>): T;
|
|
199
|
+
render(renderable: Renderable, opts?: RenderOptions): RenderResult;
|
|
200
|
+
render<V extends Record<string, any>, O>(prompt: PromptParsed<V, O>, variables: V, options?: RenderPromptOptions): Promise<O>;
|
|
201
|
+
render<V extends Record<string, any>>(prompt: Prompt<V>, variables: V): RenderResult;
|
|
202
|
+
render<V extends Record<string, any>, O extends AsyncGenerator<any>>(chain: StreamChain<V, O>, variables: V): AsyncGenerator<O, void>;
|
|
203
|
+
render<V extends Record<string, any>, O>(chain: FunctionChain<V, O>, variables: V): O;
|
|
204
|
+
}
|
|
205
|
+
type RenderPromptOptions = {
|
|
206
|
+
validateOutput?: boolean;
|
|
207
|
+
};
|
|
208
|
+
declare function createContext<T>(defaultValue: T): Context<T>;
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* This can be extended using declare module to add additional providers.
|
|
212
|
+
*/
|
|
213
|
+
interface ChatCompletionRequestPayloads {
|
|
214
|
+
}
|
|
215
|
+
interface LogChatCompletionRequest<R extends Record<string, any> = ChatCompletionRequestPayloads[keyof ChatCompletionRequestPayloads]> {
|
|
216
|
+
startTime: number;
|
|
217
|
+
model: string;
|
|
218
|
+
providerRegion?: string;
|
|
219
|
+
provider?: string;
|
|
220
|
+
inputMessages: RenderedConversationMessage[];
|
|
221
|
+
request: R;
|
|
222
|
+
}
|
|
223
|
+
interface LogChatCompletionResponse<R extends Record<string, any> = ChatCompletionRequestPayloads[keyof ChatCompletionRequestPayloads]> extends LogChatCompletionRequest<R> {
|
|
224
|
+
latency: number;
|
|
225
|
+
outputMessage: RenderedConversationMessage;
|
|
226
|
+
finishReason: string | null;
|
|
227
|
+
tokensUsed: {
|
|
228
|
+
prompt: number;
|
|
229
|
+
completion: number;
|
|
230
|
+
total: number;
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
type LogLevel = 'error' | 'warn' | 'info' | 'debug';
|
|
234
|
+
type Loggable = string | number | boolean | undefined | null | object;
|
|
235
|
+
type Logger = {
|
|
236
|
+
error: (...msg: Loggable[]) => void;
|
|
237
|
+
warn: (...msg: Loggable[]) => void;
|
|
238
|
+
info: (...msg: Loggable[]) => void;
|
|
239
|
+
debug: (...msg: Loggable[]) => void;
|
|
240
|
+
logException: (exception: unknown) => void;
|
|
241
|
+
chatCompletionRequest: <K extends keyof ChatCompletionRequestPayloads>(provider: K, payload: LogChatCompletionRequest<ChatCompletionRequestPayloads[K]>) => void;
|
|
242
|
+
chatCompletionResponse: <K extends keyof ChatCompletionRequestPayloads>(provider: K, payload: LogChatCompletionResponse<ChatCompletionRequestPayloads[K]>) => void;
|
|
243
|
+
};
|
|
244
|
+
declare abstract class LogImplementation {
|
|
245
|
+
protected readonly loggedExceptions: WeakMap<object, boolean>;
|
|
246
|
+
/**
|
|
247
|
+
* @param ctx The current RenderContext
|
|
248
|
+
* @param level The log level, e.g. 'error', 'warn', 'info', 'debug'
|
|
249
|
+
* @param message
|
|
250
|
+
*/
|
|
251
|
+
abstract log(ctx: RenderContext, level: LogLevel, message: string): void;
|
|
252
|
+
/**
|
|
253
|
+
* Logs exceptions thrown during an element's render.
|
|
254
|
+
*/
|
|
255
|
+
logException(ctx: RenderContext, exception: unknown): void;
|
|
256
|
+
chatCompletionRequest<K extends keyof ChatCompletionRequestPayloads>(_ctx: RenderContext, _provider: K, _payload: LogChatCompletionRequest<ChatCompletionRequestPayloads[K]>): void;
|
|
257
|
+
chatCompletionResponse<K extends keyof ChatCompletionRequestPayloads>(_ctx: RenderContext, _provider: K, _payload: LogChatCompletionResponse<ChatCompletionRequestPayloads[K]>): void;
|
|
258
|
+
}
|
|
259
|
+
declare class BoundLogger implements Logger {
|
|
260
|
+
private readonly impl;
|
|
261
|
+
private readonly ctx;
|
|
262
|
+
constructor(impl: LogImplementation, ctx: RenderContext);
|
|
263
|
+
private formatMessage;
|
|
264
|
+
error: (...msgs: Loggable[]) => void;
|
|
265
|
+
warn: (...msgs: Loggable[]) => void;
|
|
266
|
+
info: (...msgs: Loggable[]) => void;
|
|
267
|
+
debug: (...msgs: Loggable[]) => void;
|
|
268
|
+
logException: (exception: unknown) => void;
|
|
269
|
+
chatCompletionRequest: <K extends keyof ChatCompletionRequestPayloads>(provider: K, payload: LogChatCompletionRequest<ChatCompletionRequestPayloads[K]>) => void;
|
|
270
|
+
chatCompletionResponse: <K extends keyof ChatCompletionRequestPayloads>(provider: K, payload: LogChatCompletionResponse<ChatCompletionRequestPayloads[K]>) => void;
|
|
271
|
+
}
|
|
272
|
+
declare class NoopLogImplementation extends LogImplementation {
|
|
273
|
+
log(_ctx: RenderContext, _level: LogLevel, _message: string): void;
|
|
274
|
+
}
|
|
275
|
+
declare class ConsoleLogger extends LogImplementation {
|
|
276
|
+
log(ctx: RenderContext, level: LogLevel, message: string): void;
|
|
277
|
+
}
|
|
278
|
+
declare class CombinedLogger extends LogImplementation {
|
|
279
|
+
private readonly loggers;
|
|
280
|
+
constructor(loggers: LogImplementation[]);
|
|
281
|
+
log(...args: Parameters<LogImplementation['log']>): void;
|
|
282
|
+
chatCompletionRequest<_K extends keyof ChatCompletionRequestPayloads>(...args: Parameters<LogImplementation['chatCompletionRequest']>): void;
|
|
283
|
+
chatCompletionResponse<_K extends keyof ChatCompletionRequestPayloads>(...args: Parameters<LogImplementation['chatCompletionResponse']>): void;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
type ChatCompletionClientAndProvider<K> = {
|
|
287
|
+
client: K;
|
|
288
|
+
provider?: string;
|
|
289
|
+
providerRegion?: string;
|
|
290
|
+
costFn?: (usage: {
|
|
291
|
+
prompt: number;
|
|
292
|
+
completion: number;
|
|
293
|
+
}) => number;
|
|
294
|
+
};
|
|
295
|
+
type ChatCompletionRole = 'user' | 'system' | 'assistant';
|
|
296
|
+
declare const SystemMessage: (props: {
|
|
297
|
+
children: AINode;
|
|
298
|
+
}) => AINode;
|
|
299
|
+
declare const UserMessage: (props: {
|
|
300
|
+
children: AINode;
|
|
301
|
+
}) => AINode;
|
|
302
|
+
declare const AssistantMessage: (props: {
|
|
303
|
+
children: AINode;
|
|
304
|
+
}) => AINode;
|
|
305
|
+
type RenderedConversationMessage = {
|
|
306
|
+
role: ChatCompletionRole;
|
|
307
|
+
content: string;
|
|
308
|
+
tokens: number;
|
|
309
|
+
};
|
|
310
|
+
declare const computeUsage: (messages: RenderedConversationMessage[]) => {
|
|
311
|
+
prompt: number;
|
|
312
|
+
completion: number;
|
|
313
|
+
total: number;
|
|
314
|
+
};
|
|
315
|
+
declare class ChatCompletionError extends Error {
|
|
316
|
+
readonly chatCompletionRequest: LogChatCompletionRequest;
|
|
317
|
+
constructor(message: string, chatCompletionRequest: LogChatCompletionRequest);
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
declare function createAIElement<P extends {
|
|
321
|
+
children: C;
|
|
322
|
+
}, C>(tag: AIComponent<P>, props: Omit<P, 'children'> | null, ...children: [C]): AIElement<P>;
|
|
323
|
+
declare function createAIElement<P extends {
|
|
324
|
+
children: C[];
|
|
325
|
+
}, C>(tag: AIComponent<P>, props: Omit<P, 'children'> | null, ...children: C[]): AIElement<P>;
|
|
326
|
+
declare function AIFragment({ children }: {
|
|
327
|
+
children: AINode;
|
|
328
|
+
}): Renderable;
|
|
329
|
+
|
|
330
|
+
/**
|
|
331
|
+
* The is used as an import source for ts/js files as the JSX transpile functinos
|
|
332
|
+
*/
|
|
333
|
+
|
|
334
|
+
/** @hidden */
|
|
335
|
+
declare namespace JSX {
|
|
336
|
+
type ElementType = AIComponent<any>;
|
|
337
|
+
interface Element extends AIElement<any> {
|
|
338
|
+
}
|
|
339
|
+
interface IntrinsicElements {
|
|
340
|
+
}
|
|
341
|
+
interface ElementChildrenAttribute {
|
|
342
|
+
children: {};
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
/** @hidden */
|
|
346
|
+
declare function jsx(type: any, config: any, maybeKey?: any): AIElement<{
|
|
347
|
+
children: any[];
|
|
348
|
+
}>;
|
|
349
|
+
/** @hidden */
|
|
350
|
+
declare const jsxDEV: typeof jsx;
|
|
351
|
+
/** @hidden */
|
|
352
|
+
declare const jsxs: typeof jsx;
|
|
353
|
+
/** @hidden */
|
|
354
|
+
declare const Fragment: typeof AIFragment;
|
|
355
|
+
|
|
356
|
+
export { jsx as $, type AIComponent as A, BoundLogger as B, type ContextValues as C, type Literal as D, type EvaluatorFn as E, type FunctionChain as F, type RenderResult as G, attachedContextSymbol as H, type AIElement as I, JSX as J, type Renderable as K, LogImplementation as L, type PropsOfAIComponent as M, type NotAsyncGenerator as N, type SpanContext as O, type PromptParsed as P, type SpanStatus as Q, type RenderContext as R, type SpanProcessor as S, type Tracer as T, UserMessage as U, type Span as V, type SpanEvent as W, type TracingContextKey as X, type TracingContext as Y, type TracingContextManager as Z, type OutputParser as _, type ReadableSpan as a, jsxDEV as a0, jsxs as a1, Fragment as a2, type SpanExporter as b, type AINode as c, type SpanAttributes as d, type Prompt as e, type StreamChain as f, type EvaluatorResult as g, type Context as h, type ChatCompletionClientAndProvider as i, createAIElement as j, AIFragment as k, createContext as l, type ChatCompletionRole as m, SystemMessage as n, AssistantMessage as o, type RenderedConversationMessage as p, computeUsage as q, ChatCompletionError as r, type ChatCompletionRequestPayloads as s, type LogChatCompletionRequest as t, type LogChatCompletionResponse as u, type LogLevel as v, type Logger as w, NoopLogImplementation as x, ConsoleLogger as y, CombinedLogger as z };
|