@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.
@@ -1,222 +0,0 @@
1
- import { ZodTypeAny, ZodObject, ZodRawShape } from 'zod';
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;
69
- }
70
- declare class ConsoleLogger extends LogImplementation {
71
- log(level: LogLevel, message: string): void;
72
- }
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;
80
- }
81
-
82
- type EvaluatorPrimitive = string | number | boolean;
83
- type EvaluatorResult = {
84
- key: string;
85
- result: EvaluatorPrimitive | EvaluatorPrimitive[];
86
- passes: boolean;
87
- description?: string;
88
- debug?: string;
89
- };
90
- type EvaluatorFn<V extends Record<string, any> = Record<string, any>, O = any> = (variables: V, result: O) => Promise<EvaluatorResult | EvaluatorResult[]>;
91
- type OutputParser<O> = {
92
- schema: ZodTypeAny;
93
- parse: (result: string) => O;
94
- };
95
- interface PromptRenderResult<O> extends RenderableStream {
96
- then: (onResolved: (value: O) => void, onRejected?: (reason?: any) => void) => void;
97
- }
98
- interface Prompt<V extends Record<string, any> = Record<string, any>> {
99
- key: string;
100
- component: AIComponent<V>;
101
- schema: ZodObject<ZodRawShape>;
102
- evaluators: EvaluatorFn<V>[];
103
- messageSchema: ZodTypeAny | null;
104
- outputSchema: ZodTypeAny;
105
- metadata: Record<string, any>;
106
- }
107
- interface PromptParsed<V extends Record<string, any> = Record<string, any>, O = any> extends Prompt<V> {
108
- evaluators: EvaluatorFn<V, O>[];
109
- messageSchema: null;
110
- parse: (result: string) => O;
111
- }
112
- interface FunctionChain<V extends Record<string, any> = Record<string, any>, R extends NotAsyncGenerator<any> = any> {
113
- key: string;
114
- chainType: 'function';
115
- run: (vars: V, context: RenderContext) => R;
116
- schema: ZodObject<ZodRawShape>;
117
- messageSchema: null;
118
- outputSchema: ZodTypeAny;
119
- }
120
- interface StreamChain<V extends Record<string, any> = Record<string, any>, R extends AsyncGenerator<any, any, any> = AsyncGenerator<any, any, any>> {
121
- key: string;
122
- chainType: 'stream';
123
- run: (vars: V, context: RenderContext) => R;
124
- schema: ZodObject<ZodRawShape>;
125
- messageSchema: ZodTypeAny;
126
- outputSchema: null;
127
- }
128
- type IsAsyncGenerator<T> = T extends AsyncGenerator<any, any, any> ? true : false;
129
- type NotAsyncGenerator<T> = IsAsyncGenerator<T> extends true ? never : T;
130
-
131
- type ContextValues = Record<symbol, any>;
132
- type RenderOptions = {
133
- preserveTags?: boolean;
134
- renderedProps?: {
135
- [tagName: string]: {
136
- [propName: string]: boolean;
137
- };
138
- };
139
- };
140
- interface RenderContext {
141
- logger: Logger;
142
- getContext<T>(context: Context<T>): T;
143
- render(renderable: Renderable, opts?: RenderOptions): RenderResult;
144
- render<V extends Record<string, any>, O>(prompt: PromptParsed<V, O>, variables: V, options?: RenderPromptOptions): Promise<O>;
145
- render<V extends Record<string, any>>(prompt: Prompt<V>, variables: V): RenderResult;
146
- render<V extends Record<string, any>, O extends AsyncGenerator<any>>(chain: StreamChain<V, O>, variables: V): AsyncGenerator<O, void>;
147
- 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
- }
151
- type RenderPromptOptions = {
152
- validateOutput?: boolean;
153
- };
154
- declare function createContext<T>(defaultValue: T): Context<T>;
155
-
156
- type Literal = string | number | null | undefined | boolean;
157
- interface RenderableStream extends AsyncGenerator<string, void, unknown> {
158
- [Symbol.asyncIterator]: () => AsyncGenerator<string, void, unknown>;
159
- }
160
- interface RenderResult extends RenderableStream {
161
- then: (onResolved: (value: string) => void, onRejected?: (reason?: any) => void) => void;
162
- }
163
- interface Context<T> {
164
- Provider: AIComponent<{
165
- children: AINode;
166
- value: T;
167
- }>;
168
- defaultValue: T;
169
- key: symbol;
170
- }
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>;
182
- }
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
-
187
- type ChatCompletionRole = 'user' | 'system' | 'assistant';
188
- declare const SystemMessage: (props: {
189
- children: AINode;
190
- }) => AINode;
191
- declare const UserMessage: (props: {
192
- children: AINode;
193
- }) => AINode;
194
- declare const AssistantMessage: (props: {
195
- children: AINode;
196
- }) => AINode;
197
- type RenderedConversationMessage = {
198
- role: ChatCompletionRole;
199
- content: string;
200
- tokens: number;
201
- };
202
- declare const computeUsage: (messages: RenderedConversationMessage[]) => {
203
- prompt: number;
204
- completion: number;
205
- total: number;
206
- };
207
- declare class ChatCompletionError extends Error {
208
- readonly chatCompletionRequest: Record<string, any>;
209
- constructor(message: string, chatCompletionRequest: Record<string, any>);
210
- }
211
-
212
- declare function createAIElement<P extends {
213
- children: C;
214
- }, C>(tag: AIComponent<P>, props: Omit<P, 'children'> | null, ...children: [C]): AIElement<P>;
215
- declare function createAIElement<P extends {
216
- children: C[];
217
- }, C>(tag: AIComponent<P>, props: Omit<P, 'children'> | null, ...children: C[]): AIElement<P>;
218
- declare function AIFragment({ children }: {
219
- children: AINode;
220
- }): Renderable;
221
-
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 };
@@ -1,222 +0,0 @@
1
- import { ZodTypeAny, ZodObject, ZodRawShape } from 'zod';
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;
69
- }
70
- declare class ConsoleLogger extends LogImplementation {
71
- log(level: LogLevel, message: string): void;
72
- }
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;
80
- }
81
-
82
- type EvaluatorPrimitive = string | number | boolean;
83
- type EvaluatorResult = {
84
- key: string;
85
- result: EvaluatorPrimitive | EvaluatorPrimitive[];
86
- passes: boolean;
87
- description?: string;
88
- debug?: string;
89
- };
90
- type EvaluatorFn<V extends Record<string, any> = Record<string, any>, O = any> = (variables: V, result: O) => Promise<EvaluatorResult | EvaluatorResult[]>;
91
- type OutputParser<O> = {
92
- schema: ZodTypeAny;
93
- parse: (result: string) => O;
94
- };
95
- interface PromptRenderResult<O> extends RenderableStream {
96
- then: (onResolved: (value: O) => void, onRejected?: (reason?: any) => void) => void;
97
- }
98
- interface Prompt<V extends Record<string, any> = Record<string, any>> {
99
- key: string;
100
- component: AIComponent<V>;
101
- schema: ZodObject<ZodRawShape>;
102
- evaluators: EvaluatorFn<V>[];
103
- messageSchema: ZodTypeAny | null;
104
- outputSchema: ZodTypeAny;
105
- metadata: Record<string, any>;
106
- }
107
- interface PromptParsed<V extends Record<string, any> = Record<string, any>, O = any> extends Prompt<V> {
108
- evaluators: EvaluatorFn<V, O>[];
109
- messageSchema: null;
110
- parse: (result: string) => O;
111
- }
112
- interface FunctionChain<V extends Record<string, any> = Record<string, any>, R extends NotAsyncGenerator<any> = any> {
113
- key: string;
114
- chainType: 'function';
115
- run: (vars: V, context: RenderContext) => R;
116
- schema: ZodObject<ZodRawShape>;
117
- messageSchema: null;
118
- outputSchema: ZodTypeAny;
119
- }
120
- interface StreamChain<V extends Record<string, any> = Record<string, any>, R extends AsyncGenerator<any, any, any> = AsyncGenerator<any, any, any>> {
121
- key: string;
122
- chainType: 'stream';
123
- run: (vars: V, context: RenderContext) => R;
124
- schema: ZodObject<ZodRawShape>;
125
- messageSchema: ZodTypeAny;
126
- outputSchema: null;
127
- }
128
- type IsAsyncGenerator<T> = T extends AsyncGenerator<any, any, any> ? true : false;
129
- type NotAsyncGenerator<T> = IsAsyncGenerator<T> extends true ? never : T;
130
-
131
- type ContextValues = Record<symbol, any>;
132
- type RenderOptions = {
133
- preserveTags?: boolean;
134
- renderedProps?: {
135
- [tagName: string]: {
136
- [propName: string]: boolean;
137
- };
138
- };
139
- };
140
- interface RenderContext {
141
- logger: Logger;
142
- getContext<T>(context: Context<T>): T;
143
- render(renderable: Renderable, opts?: RenderOptions): RenderResult;
144
- render<V extends Record<string, any>, O>(prompt: PromptParsed<V, O>, variables: V, options?: RenderPromptOptions): Promise<O>;
145
- render<V extends Record<string, any>>(prompt: Prompt<V>, variables: V): RenderResult;
146
- render<V extends Record<string, any>, O extends AsyncGenerator<any>>(chain: StreamChain<V, O>, variables: V): AsyncGenerator<O, void>;
147
- 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
- }
151
- type RenderPromptOptions = {
152
- validateOutput?: boolean;
153
- };
154
- declare function createContext<T>(defaultValue: T): Context<T>;
155
-
156
- type Literal = string | number | null | undefined | boolean;
157
- interface RenderableStream extends AsyncGenerator<string, void, unknown> {
158
- [Symbol.asyncIterator]: () => AsyncGenerator<string, void, unknown>;
159
- }
160
- interface RenderResult extends RenderableStream {
161
- then: (onResolved: (value: string) => void, onRejected?: (reason?: any) => void) => void;
162
- }
163
- interface Context<T> {
164
- Provider: AIComponent<{
165
- children: AINode;
166
- value: T;
167
- }>;
168
- defaultValue: T;
169
- key: symbol;
170
- }
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>;
182
- }
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
-
187
- type ChatCompletionRole = 'user' | 'system' | 'assistant';
188
- declare const SystemMessage: (props: {
189
- children: AINode;
190
- }) => AINode;
191
- declare const UserMessage: (props: {
192
- children: AINode;
193
- }) => AINode;
194
- declare const AssistantMessage: (props: {
195
- children: AINode;
196
- }) => AINode;
197
- type RenderedConversationMessage = {
198
- role: ChatCompletionRole;
199
- content: string;
200
- tokens: number;
201
- };
202
- declare const computeUsage: (messages: RenderedConversationMessage[]) => {
203
- prompt: number;
204
- completion: number;
205
- total: number;
206
- };
207
- declare class ChatCompletionError extends Error {
208
- readonly chatCompletionRequest: Record<string, any>;
209
- constructor(message: string, chatCompletionRequest: Record<string, any>);
210
- }
211
-
212
- declare function createAIElement<P extends {
213
- children: C;
214
- }, C>(tag: AIComponent<P>, props: Omit<P, 'children'> | null, ...children: [C]): AIElement<P>;
215
- declare function createAIElement<P extends {
216
- children: C[];
217
- }, C>(tag: AIComponent<P>, props: Omit<P, 'children'> | null, ...children: C[]): AIElement<P>;
218
- declare function AIFragment({ children }: {
219
- children: AINode;
220
- }): Renderable;
221
-
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 };