@gammatech/aijsx 0.5.0-beta.1 → 0.5.0-dev.2024-03-11

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.
@@ -1,83 +1,32 @@
1
1
  import { ZodTypeAny, ZodObject, ZodRawShape } from 'zod';
2
2
 
3
- interface ChatCompletionRequestPayloads {
4
- }
5
- interface LogChatCompletionRequest<R extends Record<string, any> = ChatCompletionRequestPayloads[keyof ChatCompletionRequestPayloads]> {
6
- requestId: string;
7
- startTime: string;
8
- model: string;
9
- providerRegion?: string;
10
- provider?: string;
11
- inputMessages: RenderedConversationMessage[];
12
- request: R;
13
- }
14
- interface LogChatCompletionResponse<R extends Record<string, any> = ChatCompletionRequestPayloads[keyof ChatCompletionRequestPayloads]> extends LogChatCompletionRequest<R> {
15
- endTime: string;
16
- latency: number;
17
- outputMessage: RenderedConversationMessage;
18
- finishReason: string;
19
- tokensUsed: {
20
- prompt: number;
21
- completion: number;
22
- total: number;
23
- };
24
- }
25
-
26
- /**
27
- * This can be extended using declare module to add additional providers.
28
- */
29
- type LogLevel = 'error' | 'warn' | 'info' | 'debug';
30
- type Loggable = string | number | boolean | undefined | null | object;
31
- type Logger = {
32
- error: (...msg: Loggable[]) => void;
33
- warn: (...msg: Loggable[]) => void;
34
- info: (...msg: Loggable[]) => void;
35
- debug: (...msg: Loggable[]) => void;
36
- logException(exception: string | Error): void;
37
- chatCompletionRequest<K extends keyof ChatCompletionRequestPayloads>(provider: K, payload: LogChatCompletionRequest<ChatCompletionRequestPayloads[K]>): void;
38
- chatCompletionResponse<K extends keyof ChatCompletionRequestPayloads>(provider: K, payload: LogChatCompletionResponse<ChatCompletionRequestPayloads[K]>): void;
39
- };
40
- declare abstract class LogImplementation {
41
- private readonly loggedExceptions;
42
- /**
43
- * @param ctx The current RenderContext
44
- * @param level The log level, e.g. 'error', 'warn', 'info', 'debug'
45
- * @param message
46
- */
47
- abstract log(level: LogLevel, message: string): void;
48
- chatCompletionRequest<K extends keyof ChatCompletionRequestPayloads>(provider: K, payload: LogChatCompletionRequest<ChatCompletionRequestPayloads[K]>): void;
49
- chatCompletionResponse<K extends keyof ChatCompletionRequestPayloads>(provider: K, payload: LogChatCompletionResponse<ChatCompletionRequestPayloads[K]>): void;
50
- /**
51
- * Logs exceptions thrown during an element's render.
52
- */
53
- logException(exception: unknown): void;
54
- }
55
- declare class BoundLogger implements Logger {
56
- private readonly impl;
57
- constructor(impl: LogImplementation);
58
- private formatMessage;
59
- error: (...msgs: Loggable[]) => void;
60
- warn: (...msgs: Loggable[]) => void;
61
- info: (...msgs: Loggable[]) => void;
62
- debug: (...msgs: Loggable[]) => void;
63
- logException: (exception: string | Error) => void;
64
- chatCompletionRequest: <K extends keyof ChatCompletionRequestPayloads>(provider: K, payload: LogChatCompletionRequest<ChatCompletionRequestPayloads[K]>) => void;
65
- chatCompletionResponse: <K extends keyof ChatCompletionRequestPayloads>(provider: K, payload: LogChatCompletionResponse<ChatCompletionRequestPayloads[K]>) => void;
66
- }
67
- declare class NoopLogImplementation extends LogImplementation {
68
- log(level: LogLevel, message: string): void;
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>;
69
6
  }
70
- declare class ConsoleLogger extends LogImplementation {
71
- log(level: LogLevel, message: string): void;
7
+ interface Context<T> {
8
+ Provider: AIComponent<{
9
+ children: AINode;
10
+ value: T;
11
+ }>;
12
+ defaultValue: T;
13
+ key: symbol;
72
14
  }
73
- declare class CombinedLogger extends LogImplementation {
74
- private readonly loggers;
75
- constructor(loggers: LogImplementation[]);
76
- log(...args: Parameters<LogImplementation['log']>): void;
77
- logException(...args: Parameters<LogImplementation['logException']>): void;
78
- chatCompletionRequest<_K extends keyof ChatCompletionRequestPayloads>(...args: Parameters<LogImplementation['chatCompletionRequest']>): void;
79
- chatCompletionResponse<_K extends keyof ChatCompletionRequestPayloads>(...args: Parameters<LogImplementation['chatCompletionResponse']>): void;
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>;
80
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;
81
30
 
82
31
  type EvaluatorPrimitive = string | number | boolean;
83
32
  type EvaluatorResult = {
@@ -92,9 +41,6 @@ type OutputParser<O> = {
92
41
  schema: ZodTypeAny;
93
42
  parse: (result: string) => O;
94
43
  };
95
- interface PromptRenderResult<O> extends RenderableStream {
96
- then: (onResolved: (value: O) => void, onRejected?: (reason?: any) => void) => void;
97
- }
98
44
  interface Prompt<V extends Record<string, any> = Record<string, any>> {
99
45
  key: string;
100
46
  component: AIComponent<V>;
@@ -128,6 +74,115 @@ interface StreamChain<V extends Record<string, any> = Record<string, any>, R ext
128
74
  type IsAsyncGenerator<T> = T extends AsyncGenerator<any, any, any> ? true : false;
129
75
  type NotAsyncGenerator<T> = IsAsyncGenerator<T> extends true ? never : T;
130
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
+
131
186
  type ContextValues = Record<symbol, any>;
132
187
  type RenderOptions = {
133
188
  preserveTags?: boolean;
@@ -138,6 +193,7 @@ type RenderOptions = {
138
193
  };
139
194
  };
140
195
  interface RenderContext {
196
+ tracer: Tracer;
141
197
  logger: Logger;
142
198
  getContext<T>(context: Context<T>): T;
143
199
  render(renderable: Renderable, opts?: RenderOptions): RenderResult;
@@ -145,44 +201,87 @@ interface RenderContext {
145
201
  render<V extends Record<string, any>>(prompt: Prompt<V>, variables: V): RenderResult;
146
202
  render<V extends Record<string, any>, O extends AsyncGenerator<any>>(chain: StreamChain<V, O>, variables: V): AsyncGenerator<O, void>;
147
203
  render<V extends Record<string, any>, O>(chain: FunctionChain<V, O>, variables: V): O;
148
- runChain<V extends Record<string, any>, R>(chain: FunctionChain<V, R>, variables: V): R;
149
- runChain<V extends Record<string, any>, R extends AsyncGenerator<any>>(chain: StreamChain<V, R>, variables: V): R;
150
204
  }
151
205
  type RenderPromptOptions = {
152
206
  validateOutput?: boolean;
153
207
  };
154
208
  declare function createContext<T>(defaultValue: T): Context<T>;
155
209
 
156
- type Literal = string | number | null | undefined | boolean;
157
- interface RenderableStream extends AsyncGenerator<string, void, unknown> {
158
- [Symbol.asyncIterator]: () => AsyncGenerator<string, void, unknown>;
210
+ /**
211
+ * This can be extended using declare module to add additional providers.
212
+ */
213
+ interface ChatCompletionRequestPayloads {
159
214
  }
160
- interface RenderResult extends RenderableStream {
161
- then: (onResolved: (value: string) => void, onRejected?: (reason?: any) => void) => void;
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;
162
222
  }
163
- interface Context<T> {
164
- Provider: AIComponent<{
165
- children: AINode;
166
- value: T;
167
- }>;
168
- defaultValue: T;
169
- key: symbol;
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
+ };
170
232
  }
171
- type AIComponent<P> = (props: P, context: RenderContext) => Renderable;
172
- declare const attachedContextSymbol: unique symbol;
173
- interface AIElement<P> {
174
- /** The tag associated with this {@link AIElement}. */
175
- tag: AIComponent<P>;
176
- /** The component properties. */
177
- props: P;
178
- /** A function that renders this {@link AIElement} to a {@link Renderable}. */
179
- render: (ctx: RenderContext) => Renderable;
180
- /** The {@link RenderContext} associated with this {@link Element}. */
181
- [attachedContextSymbol]?: Record<symbol, any>;
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;
182
284
  }
183
- type AINode = Literal | AIElement<any> | AINode[];
184
- type Renderable = AINode | PromiseLike<Renderable> | RenderableStream;
185
- type PropsOfAIComponent<T extends AIComponent<any>> = T extends AIComponent<infer P> ? P : never;
186
285
 
187
286
  type ChatCompletionRole = 'user' | 'system' | 'assistant';
188
287
  declare const SystemMessage: (props: {
@@ -205,8 +304,8 @@ declare const computeUsage: (messages: RenderedConversationMessage[]) => {
205
304
  total: number;
206
305
  };
207
306
  declare class ChatCompletionError extends Error {
208
- readonly chatCompletionRequest: Record<string, any>;
209
- constructor(message: string, chatCompletionRequest: Record<string, any>);
307
+ readonly chatCompletionRequest: LogChatCompletionRequest;
308
+ constructor(message: string, chatCompletionRequest: LogChatCompletionRequest);
210
309
  }
211
310
 
212
311
  declare function createAIElement<P extends {
@@ -219,4 +318,30 @@ declare function AIFragment({ children }: {
219
318
  children: AINode;
220
319
  }): Renderable;
221
320
 
222
- export { type AIComponent as A, BoundLogger as B, type ContextValues as C, type Renderable as D, type EvaluatorFn as E, type FunctionChain as F, type PropsOfAIComponent as G, type PromptRenderResult as H, LogImplementation as L, type NotAsyncGenerator as N, type OutputParser as O, type PromptParsed as P, type RenderContext as R, type StreamChain as S, UserMessage as U, type Prompt as a, type EvaluatorResult as b, type Context as c, type AINode as d, createAIElement as e, AIFragment as f, createContext as g, type ChatCompletionRole as h, SystemMessage as i, AssistantMessage as j, type RenderedConversationMessage as k, computeUsage as l, ChatCompletionError as m, type LogLevel as n, type Logger as o, NoopLogImplementation as p, ConsoleLogger as q, CombinedLogger as r, type ChatCompletionRequestPayloads as s, type LogChatCompletionRequest as t, type LogChatCompletionResponse as u, type Literal as v, type RenderableStream as w, type RenderResult as x, attachedContextSymbol as y, type AIElement as z };
321
+ /**
322
+ * The is used as an import source for ts/js files as the JSX transpile functinos
323
+ */
324
+
325
+ /** @hidden */
326
+ declare namespace JSX {
327
+ type ElementType = AIComponent<any>;
328
+ interface Element extends AIElement<any> {
329
+ }
330
+ interface IntrinsicElements {
331
+ }
332
+ interface ElementChildrenAttribute {
333
+ children: {};
334
+ }
335
+ }
336
+ /** @hidden */
337
+ declare function jsx(type: any, config: any, maybeKey?: any): AIElement<{
338
+ children: any[];
339
+ }>;
340
+ /** @hidden */
341
+ declare const jsxDEV: typeof jsx;
342
+ /** @hidden */
343
+ declare const jsxs: typeof jsx;
344
+ /** @hidden */
345
+ declare const Fragment: typeof AIFragment;
346
+
347
+ export { jsxDEV as $, type AIComponent as A, BoundLogger as B, type ContextValues as C, type RenderResult as D, type EvaluatorFn as E, type FunctionChain as F, attachedContextSymbol as G, type AIElement as H, type Renderable as I, JSX as J, type PropsOfAIComponent as K, LogImplementation as L, type SpanContext as M, type NotAsyncGenerator as N, type SpanStatus as O, type PromptParsed as P, type Span as Q, type RenderContext as R, type SpanProcessor as S, type Tracer as T, UserMessage as U, type SpanEvent as V, type TracingContextKey as W, type TracingContext as X, type TracingContextManager as Y, type OutputParser as Z, jsx as _, type ReadableSpan as a, jsxs as a0, Fragment as a1, 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, createAIElement as i, AIFragment as j, createContext as k, type ChatCompletionRole as l, SystemMessage as m, AssistantMessage as n, type RenderedConversationMessage as o, computeUsage as p, ChatCompletionError as q, type ChatCompletionRequestPayloads as r, type LogChatCompletionRequest as s, type LogChatCompletionResponse as t, type LogLevel as u, type Logger as v, NoopLogImplementation as w, ConsoleLogger as x, CombinedLogger as y, type Literal as z };