@gammatech/aijsx 0.4.0-beta.8 → 0.4.1-beta.1

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 CHANGED
@@ -208,6 +208,8 @@ export interface RenderContext {
208
208
 
209
209
  element: AIElement<any>
210
210
 
211
+ renderId: string
212
+
211
213
  logger: Logger
212
214
 
213
215
  getContext<T>(context: Context<T>): T
@@ -1,11 +1,8 @@
1
1
  import { ZodTypeAny, ZodObject, ZodRawShape } from 'zod';
2
2
 
3
3
  type Literal = string | number | null | undefined | boolean;
4
- interface RenderableStream extends AsyncGenerator<string, void, unknown> {
5
- [Symbol.asyncIterator]: () => AsyncGenerator<string, void, unknown>;
6
- }
7
- interface RenderResult extends RenderableStream {
8
- then: (onResolved: (value: string) => void, onRejected?: (reason?: any) => void) => void;
4
+ interface RenderResult extends AsyncGenerator<string, void> {
5
+ then(onFulfilled: (value: string) => string | PromiseLike<string>, onRejected?: (reason: any) => string | PromiseLike<string>): PromiseLike<string>;
9
6
  }
10
7
  interface Context<T> {
11
8
  Provider: AIComponent<{
@@ -28,7 +25,7 @@ interface AIElement<P> {
28
25
  [attachedContextSymbol]?: Record<symbol, any>;
29
26
  }
30
27
  type AINode = Literal | AIElement<any> | AINode[];
31
- type Renderable = AINode | PromiseLike<Renderable> | RenderableStream;
28
+ type Renderable = AINode | PromiseLike<Renderable> | AsyncGenerator<string, void>;
32
29
  type PropsOfAIComponent<T extends AIComponent<any>> = T extends AIComponent<infer P> ? P : never;
33
30
 
34
31
  type EvaluatorPrimitive = string | number | boolean;
@@ -44,9 +41,6 @@ type OutputParser<O> = {
44
41
  schema: ZodTypeAny;
45
42
  parse: (result: string) => O;
46
43
  };
47
- interface PromptRenderResult<O> extends RenderableStream {
48
- then: (onResolved: (value: O) => void, onRejected?: (reason?: any) => void) => void;
49
- }
50
44
  interface Prompt<V extends Record<string, any> = Record<string, any>> {
51
45
  key: string;
52
46
  component: AIComponent<V>;
@@ -80,7 +74,115 @@ interface StreamChain<V extends Record<string, any> = Record<string, any>, R ext
80
74
  type IsAsyncGenerator<T> = T extends AsyncGenerator<any, any, any> ? true : false;
81
75
  type NotAsyncGenerator<T> = IsAsyncGenerator<T> extends true ? never : T;
82
76
 
83
- declare const LoggerContext: Context<LogImplementation>;
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 {
84
+ name: string;
85
+ status: SpanStatus;
86
+ spanContext: SpanContext;
87
+ parentSpanId: string | null;
88
+ attributes: SpanAttributes;
89
+ events: SpanEvent[];
90
+ startTime: string;
91
+ endTime: string | null;
92
+ ended: boolean;
93
+ duration: number;
94
+ }
95
+ interface Span extends ReadableSpan {
96
+ setAttributes(attributes: SpanAttributes): 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
+
84
186
  type ContextValues = Record<symbol, any>;
85
187
  type RenderOptions = {
86
188
  preserveTags?: boolean;
@@ -90,16 +192,8 @@ type RenderOptions = {
90
192
  };
91
193
  };
92
194
  };
93
- type RenderTraceInfo = {
94
- traceId: string;
95
- spanId: string;
96
- parentSpanId: string | null;
97
- renderType: 'element' | 'chain' | 'prompt' | 'root';
98
- key: string;
99
- parent: RenderTraceInfo | null;
100
- };
101
195
  interface RenderContext {
102
- traceInfo: RenderTraceInfo;
196
+ tracer: Tracer;
103
197
  logger: Logger;
104
198
  getContext<T>(context: Context<T>): T;
105
199
  render(renderable: Renderable, opts?: RenderOptions): RenderResult;
@@ -107,67 +201,19 @@ interface RenderContext {
107
201
  render<V extends Record<string, any>>(prompt: Prompt<V>, variables: V): RenderResult;
108
202
  render<V extends Record<string, any>, O extends AsyncGenerator<any>>(chain: StreamChain<V, O>, variables: V): AsyncGenerator<O, void>;
109
203
  render<V extends Record<string, any>, O>(chain: FunctionChain<V, O>, variables: V): O;
110
- runChain<V extends Record<string, any>, R>(chain: FunctionChain<V, R>, variables: V): R;
111
- runChain<V extends Record<string, any>, R extends AsyncGenerator<any>>(chain: StreamChain<V, R>, variables: V): R;
112
204
  }
113
205
  type RenderPromptOptions = {
114
206
  validateOutput?: boolean;
115
207
  };
116
208
  declare function createContext<T>(defaultValue: T): Context<T>;
117
209
 
118
- interface LogChainStart {
119
- startTime: string;
120
- chainKey: string;
121
- variables: Record<string, any>;
122
- chainType: 'function' | 'stream';
123
- }
124
- interface LogChainComplete {
125
- startTime: string;
126
- endTime: string;
127
- latency: number;
128
- chainKey: string;
129
- variables: Record<string, any>;
130
- chainType: 'function' | 'stream';
131
- output: any;
132
- }
133
- interface LogChainError {
134
- startTime: string;
135
- endTime: string;
136
- latency: number;
137
- chainKey: string;
138
- variables: Record<string, any>;
139
- chainType: 'function' | 'stream';
140
- error: Error;
141
- }
142
- interface LogPromptStart {
143
- startTime: string;
144
- promptKey: string;
145
- variables: Record<string, any>;
146
- }
147
- interface LogPromptComplete {
148
- startTime: string;
149
- endTime: string;
150
- latency: number;
151
- promptKey: string;
152
- variables: Record<string, any>;
153
- output: any;
154
- }
155
- interface LogPromptError {
156
- startTime: string;
157
- endTime: string;
158
- latency: number;
159
- promptKey: string;
160
- variables: Record<string, any>;
161
- error: Error;
162
- }
163
210
  /**
164
211
  * This can be extended using declare module to add additional providers.
165
212
  */
166
213
  interface ChatCompletionRequestPayloads {
167
214
  }
168
215
  interface LogChatCompletionRequest<R extends Record<string, any> = ChatCompletionRequestPayloads[keyof ChatCompletionRequestPayloads]> {
169
- requestId: string;
170
- startTime: string;
216
+ startTime: number;
171
217
  model: string;
172
218
  providerRegion?: string;
173
219
  provider?: string;
@@ -175,9 +221,6 @@ interface LogChatCompletionRequest<R extends Record<string, any> = ChatCompletio
175
221
  request: R;
176
222
  }
177
223
  interface LogChatCompletionResponse<R extends Record<string, any> = ChatCompletionRequestPayloads[keyof ChatCompletionRequestPayloads]> extends LogChatCompletionRequest<R> {
178
- requestId: string;
179
- startTime: string;
180
- endTime: string;
181
224
  latency: number;
182
225
  outputMessage: RenderedConversationMessage;
183
226
  finishReason: string;
@@ -189,25 +232,12 @@ interface LogChatCompletionResponse<R extends Record<string, any> = ChatCompleti
189
232
  }
190
233
  type LogLevel = 'error' | 'warn' | 'info' | 'debug';
191
234
  type Loggable = string | number | boolean | undefined | null | object;
192
- type LogContext = {
193
- renderType: 'element' | 'chain' | 'prompt';
194
- key: string;
195
- spanId: string;
196
- traceId: string;
197
- parentSpanId: string;
198
- };
199
235
  type Logger = {
200
236
  error: (...msg: Loggable[]) => void;
201
237
  warn: (...msg: Loggable[]) => void;
202
238
  info: (...msg: Loggable[]) => void;
203
239
  debug: (...msg: Loggable[]) => void;
204
240
  logException: (exception: unknown) => void;
205
- chainStart(chain: FunctionChain | StreamChain, payload: LogChainStart): void;
206
- chainComplete(chain: FunctionChain | StreamChain, payload: LogChainComplete): void;
207
- chainError(chain: FunctionChain | StreamChain, payload: LogChainError): void;
208
- promptStart(prompt: Prompt, payload: LogPromptStart): void;
209
- promptComplete(prompt: Prompt, payload: LogPromptComplete): void;
210
- promptError(prompt: Prompt, payload: LogPromptError): void;
211
241
  chatCompletionRequest: <K extends keyof ChatCompletionRequestPayloads>(provider: K, payload: LogChatCompletionRequest<ChatCompletionRequestPayloads[K]>) => void;
212
242
  chatCompletionResponse: <K extends keyof ChatCompletionRequestPayloads>(provider: K, payload: LogChatCompletionResponse<ChatCompletionRequestPayloads[K]>) => void;
213
243
  };
@@ -218,30 +248,18 @@ declare abstract class LogImplementation {
218
248
  * @param level The log level, e.g. 'error', 'warn', 'info', 'debug'
219
249
  * @param message
220
250
  */
221
- abstract log(ctx: RenderTraceInfo, level: LogLevel, message: string): void;
251
+ abstract log(ctx: RenderContext, level: LogLevel, message: string): void;
222
252
  /**
223
253
  * Logs exceptions thrown during an element's render.
224
254
  */
225
- logException(ctx: RenderTraceInfo, exception: unknown): void;
226
- chainStart(_ctx: RenderTraceInfo, _chain: FunctionChain | StreamChain, _payload: LogChainStart): void;
227
- chainComplete(_ctx: RenderTraceInfo, _chain: FunctionChain | StreamChain, _payload: LogChainComplete): void;
228
- chainError(_ctx: RenderTraceInfo, _chain: FunctionChain | StreamChain, _payload: LogChainError): void;
229
- promptStart(_ctx: RenderTraceInfo, _prompt: Prompt, _payload: LogPromptStart): void;
230
- promptComplete(_ctx: RenderTraceInfo, _prompt: Prompt, _payload: LogPromptComplete): void;
231
- promptError(_ctx: RenderTraceInfo, _prompt: Prompt, _payload: LogPromptError): void;
232
- chatCompletionRequest<K extends keyof ChatCompletionRequestPayloads>(_ctx: RenderTraceInfo, _provider: K, _payload: LogChatCompletionRequest<ChatCompletionRequestPayloads[K]>): void;
233
- chatCompletionResponse<K extends keyof ChatCompletionRequestPayloads>(_ctx: RenderTraceInfo, _provider: K, _payload: LogChatCompletionResponse<ChatCompletionRequestPayloads[K]>): void;
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;
234
258
  }
235
259
  declare class BoundLogger implements Logger {
236
260
  private readonly impl;
237
261
  private readonly ctx;
238
- constructor(impl: LogImplementation, ctx: RenderTraceInfo);
239
- chainStart(chain: FunctionChain | StreamChain, payload: LogChainStart): void;
240
- chainComplete(chain: FunctionChain | StreamChain, payload: LogChainComplete): void;
241
- chainError(chain: FunctionChain | StreamChain, payload: LogChainError): void;
242
- promptStart(prompt: Prompt, payload: LogPromptStart): void;
243
- promptComplete(prompt: Prompt, payload: LogPromptComplete): void;
244
- promptError(prompt: Prompt, payload: LogPromptError): void;
262
+ constructor(impl: LogImplementation, ctx: RenderContext);
245
263
  private formatMessage;
246
264
  error: (...msgs: Loggable[]) => void;
247
265
  warn: (...msgs: Loggable[]) => void;
@@ -252,21 +270,15 @@ declare class BoundLogger implements Logger {
252
270
  chatCompletionResponse: <K extends keyof ChatCompletionRequestPayloads>(provider: K, payload: LogChatCompletionResponse<ChatCompletionRequestPayloads[K]>) => void;
253
271
  }
254
272
  declare class NoopLogImplementation extends LogImplementation {
255
- log(_ctx: RenderTraceInfo, _level: LogLevel, _message: string): void;
273
+ log(_ctx: RenderContext, _level: LogLevel, _message: string): void;
256
274
  }
257
275
  declare class ConsoleLogger extends LogImplementation {
258
- log(ctx: RenderTraceInfo, level: LogLevel, message: string): void;
276
+ log(ctx: RenderContext, level: LogLevel, message: string): void;
259
277
  }
260
278
  declare class CombinedLogger extends LogImplementation {
261
279
  private readonly loggers;
262
280
  constructor(loggers: LogImplementation[]);
263
281
  log(...args: Parameters<LogImplementation['log']>): void;
264
- chainStart(...args: Parameters<LogImplementation['chainStart']>): void;
265
- chainComplete(...args: Parameters<LogImplementation['chainComplete']>): void;
266
- chainError(...args: Parameters<LogImplementation['chainError']>): void;
267
- promptStart(...args: Parameters<LogImplementation['promptStart']>): void;
268
- promptComplete(...args: Parameters<LogImplementation['promptComplete']>): void;
269
- promptError(...args: Parameters<LogImplementation['promptError']>): void;
270
282
  chatCompletionRequest<_K extends keyof ChatCompletionRequestPayloads>(...args: Parameters<LogImplementation['chatCompletionRequest']>): void;
271
283
  chatCompletionResponse<_K extends keyof ChatCompletionRequestPayloads>(...args: Parameters<LogImplementation['chatCompletionResponse']>): void;
272
284
  }
@@ -306,4 +318,4 @@ declare function AIFragment({ children }: {
306
318
  children: AINode;
307
319
  }): Renderable;
308
320
 
309
- export { type AIComponent as A, type Logger as B, type ContextValues as C, BoundLogger as D, type EvaluatorFn as E, type FunctionChain as F, NoopLogImplementation as G, ConsoleLogger as H, CombinedLogger as I, type Literal as J, type RenderableStream as K, LogImplementation as L, type RenderResult as M, type NotAsyncGenerator as N, attachedContextSymbol as O, type PromptParsed as P, type AIElement as Q, type RenderContext as R, type StreamChain as S, type Renderable as T, UserMessage as U, type PropsOfAIComponent as V, type OutputParser as W, type PromptRenderResult as X, type Prompt as a, type EvaluatorResult as b, type Context as c, type AINode as d, createAIElement as e, AIFragment as f, LoggerContext as g, createContext as h, type RenderTraceInfo as i, type ChatCompletionRole as j, SystemMessage as k, AssistantMessage as l, type RenderedConversationMessage as m, computeUsage as n, ChatCompletionError as o, type LogChainStart as p, type LogChainComplete as q, type LogChainError as r, type LogPromptStart as s, type LogPromptComplete as t, type LogPromptError as u, type ChatCompletionRequestPayloads as v, type LogChatCompletionRequest as w, type LogChatCompletionResponse as x, type LogLevel as y, type LogContext as z };
321
+ export { type AIComponent as A, BoundLogger as B, type ContextValues as C, attachedContextSymbol as D, type EvaluatorFn as E, type FunctionChain as F, type AIElement as G, type Renderable as H, type PropsOfAIComponent as I, type SpanContext as J, type SpanAttributes as K, LogImplementation as L, type SpanStatus as M, type NotAsyncGenerator as N, type Span as O, type PromptParsed as P, type SpanEvent as Q, type RenderContext as R, type SpanProcessor as S, type Tracer as T, UserMessage as U, type TracingContextKey as V, type TracingContext as W, type TracingContextManager as X, type OutputParser as Y, type SpanExporter as a, type ReadableSpan as b, type Prompt as c, type StreamChain as d, type EvaluatorResult as e, type Context as f, type AINode as g, createAIElement as h, AIFragment as i, createContext as j, type ChatCompletionRole as k, SystemMessage as l, AssistantMessage as m, type RenderedConversationMessage as n, computeUsage as o, ChatCompletionError as p, type ChatCompletionRequestPayloads as q, type LogChatCompletionRequest as r, type LogChatCompletionResponse as s, type LogLevel as t, type Logger as u, NoopLogImplementation as v, ConsoleLogger as w, CombinedLogger as x, type Literal as y, type RenderResult as z };