@gammatech/aijsx 0.3.0-beta.7 → 0.3.0-beta.9
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/dist/{createElement-t8vKixcE.d.mts → createElement-eXTBmWMH.d.mts} +36 -10
- package/dist/{createElement-t8vKixcE.d.ts → createElement-eXTBmWMH.d.ts} +36 -10
- package/dist/index.d.mts +13 -5
- package/dist/index.d.ts +13 -5
- package/dist/index.js +52 -12
- package/dist/index.mjs +50 -11
- package/dist/jsx-dev-runtime.d.mts +1 -1
- package/dist/jsx-dev-runtime.d.ts +1 -1
- package/dist/jsx-runtime.d.mts +1 -1
- package/dist/jsx-runtime.d.ts +1 -1
- package/package.json +1 -1
|
@@ -31,6 +31,14 @@ type AINode = Literal | AIElement<any> | AINode[];
|
|
|
31
31
|
type Renderable = AINode | PromiseLike<Renderable> | RenderableStream;
|
|
32
32
|
type PropsOfAIComponent<T extends AIComponent<any>> = T extends AIComponent<infer P> ? P : never;
|
|
33
33
|
|
|
34
|
+
type JSONPrimitive = string | number | boolean | null;
|
|
35
|
+
type JSONArray = JSONSerializable[];
|
|
36
|
+
type JSONObject = {
|
|
37
|
+
[key: string]: JSONSerializable;
|
|
38
|
+
} & {
|
|
39
|
+
then?: never;
|
|
40
|
+
};
|
|
41
|
+
type JSONSerializable = JSONPrimitive | JSONArray | JSONObject;
|
|
34
42
|
type EvaluatorPrimitive = string | number | boolean;
|
|
35
43
|
type EvaluatorResult = {
|
|
36
44
|
key: string;
|
|
@@ -51,17 +59,32 @@ interface Prompt<V extends Record<string, any> = Record<string, any>> {
|
|
|
51
59
|
key: string;
|
|
52
60
|
component: AIComponent<V>;
|
|
53
61
|
schema: ZodObject<ZodRawShape>;
|
|
54
|
-
evaluators: EvaluatorFn[];
|
|
55
|
-
|
|
62
|
+
evaluators: EvaluatorFn<V>[];
|
|
63
|
+
messageSchema: ZodTypeAny | null;
|
|
64
|
+
outputSchema: ZodTypeAny;
|
|
65
|
+
metadata: Record<string, any>;
|
|
66
|
+
}
|
|
67
|
+
interface PromptParsed<V extends Record<string, any> = Record<string, any>, O extends JSONSerializable = any> extends Prompt<V> {
|
|
68
|
+
evaluators: EvaluatorFn<V, O>[];
|
|
69
|
+
messageSchema: null;
|
|
70
|
+
parse: (result: string) => O;
|
|
56
71
|
}
|
|
57
|
-
interface
|
|
58
|
-
|
|
72
|
+
interface FunctionChain<V extends Record<string, any> = Record<string, any>, R extends JSONSerializable = any> {
|
|
73
|
+
key: string;
|
|
74
|
+
type: 'function';
|
|
75
|
+
run: (vars: V, context: RenderContext) => Promise<R>;
|
|
76
|
+
schema: ZodObject<ZodRawShape>;
|
|
77
|
+
messageSchema: null;
|
|
78
|
+
outputSchema: ZodTypeAny;
|
|
59
79
|
}
|
|
60
|
-
|
|
80
|
+
interface StreamChain<V extends Record<string, any> = Record<string, any>, R extends JSONSerializable = any> {
|
|
61
81
|
key: string;
|
|
62
|
-
|
|
82
|
+
type: 'stream';
|
|
83
|
+
run: (vars: V, context: RenderContext) => AsyncGenerator<R, void>;
|
|
63
84
|
schema: ZodObject<ZodRawShape>;
|
|
64
|
-
|
|
85
|
+
messageSchema: ZodTypeAny;
|
|
86
|
+
outputSchema: null;
|
|
87
|
+
}
|
|
65
88
|
|
|
66
89
|
declare const LoggerContext: Context<LogImplementation>;
|
|
67
90
|
type ContextValues = Record<symbol, any>;
|
|
@@ -80,9 +103,12 @@ interface RenderContext {
|
|
|
80
103
|
logger: Logger;
|
|
81
104
|
getContext<T>(context: Context<T>): T;
|
|
82
105
|
render(renderable: Renderable, opts?: RenderOptions): RenderResult;
|
|
83
|
-
render<V extends Record<string, any>, O>(prompt: PromptParsed<V, O>, variables: V, options?: RenderPromptOptions): Promise<O>;
|
|
106
|
+
render<V extends Record<string, any>, O extends JSONSerializable>(prompt: PromptParsed<V, O>, variables: V, options?: RenderPromptOptions): Promise<O>;
|
|
84
107
|
render<V extends Record<string, any>>(prompt: Prompt<V>, variables: V): RenderResult;
|
|
85
|
-
render<V extends Record<string, any>, O>(chain:
|
|
108
|
+
render<V extends Record<string, any>, O extends JSONSerializable>(chain: StreamChain<V, O>, variables: V): AsyncGenerator<O, void>;
|
|
109
|
+
render<V extends Record<string, any>, O extends JSONSerializable>(chain: FunctionChain<V, O>, variables: V): Promise<O>;
|
|
110
|
+
runChain<V extends Record<string, any>, R extends JSONSerializable>(chain: FunctionChain<V, R>, variables: V): Promise<R>;
|
|
111
|
+
runChain<V extends Record<string, any>, R extends JSONSerializable>(chain: StreamChain<V, R>, variables: V): AsyncGenerator<R, void>;
|
|
86
112
|
}
|
|
87
113
|
type RenderPromptOptions = {
|
|
88
114
|
validateOutput?: boolean;
|
|
@@ -200,4 +226,4 @@ declare function AIFragment({ children }: {
|
|
|
200
226
|
children: AINode;
|
|
201
227
|
}): Renderable;
|
|
202
228
|
|
|
203
|
-
export { type AIComponent as A, BoundLogger as B, type ContextValues as C, type PropsOfAIComponent as D, type EvaluatorFn as E, type
|
|
229
|
+
export { type AIComponent as A, BoundLogger as B, type ContextValues as C, type PropsOfAIComponent as D, type EvaluatorFn as E, type FunctionChain as F, type EvaluatorResult as G, type PromptRenderResult as H, type JSONSerializable as J, LogImplementation as L, NoopLogImplementation 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 Context as b, type AINode as c, createAIElement as d, AIFragment as e, LoggerContext 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 ChatCompletionRequestPayloads as n, type LogChatCompletionRequest as o, type LogChatCompletionResponse as p, type LogLevel as q, type Logger as r, ConsoleLogger as s, CombinedLogger as t, type Literal as u, type RenderableStream as v, type RenderResult as w, attachedContextSymbol as x, type AIElement as y, type Renderable as z };
|
|
@@ -31,6 +31,14 @@ type AINode = Literal | AIElement<any> | AINode[];
|
|
|
31
31
|
type Renderable = AINode | PromiseLike<Renderable> | RenderableStream;
|
|
32
32
|
type PropsOfAIComponent<T extends AIComponent<any>> = T extends AIComponent<infer P> ? P : never;
|
|
33
33
|
|
|
34
|
+
type JSONPrimitive = string | number | boolean | null;
|
|
35
|
+
type JSONArray = JSONSerializable[];
|
|
36
|
+
type JSONObject = {
|
|
37
|
+
[key: string]: JSONSerializable;
|
|
38
|
+
} & {
|
|
39
|
+
then?: never;
|
|
40
|
+
};
|
|
41
|
+
type JSONSerializable = JSONPrimitive | JSONArray | JSONObject;
|
|
34
42
|
type EvaluatorPrimitive = string | number | boolean;
|
|
35
43
|
type EvaluatorResult = {
|
|
36
44
|
key: string;
|
|
@@ -51,17 +59,32 @@ interface Prompt<V extends Record<string, any> = Record<string, any>> {
|
|
|
51
59
|
key: string;
|
|
52
60
|
component: AIComponent<V>;
|
|
53
61
|
schema: ZodObject<ZodRawShape>;
|
|
54
|
-
evaluators: EvaluatorFn[];
|
|
55
|
-
|
|
62
|
+
evaluators: EvaluatorFn<V>[];
|
|
63
|
+
messageSchema: ZodTypeAny | null;
|
|
64
|
+
outputSchema: ZodTypeAny;
|
|
65
|
+
metadata: Record<string, any>;
|
|
66
|
+
}
|
|
67
|
+
interface PromptParsed<V extends Record<string, any> = Record<string, any>, O extends JSONSerializable = any> extends Prompt<V> {
|
|
68
|
+
evaluators: EvaluatorFn<V, O>[];
|
|
69
|
+
messageSchema: null;
|
|
70
|
+
parse: (result: string) => O;
|
|
56
71
|
}
|
|
57
|
-
interface
|
|
58
|
-
|
|
72
|
+
interface FunctionChain<V extends Record<string, any> = Record<string, any>, R extends JSONSerializable = any> {
|
|
73
|
+
key: string;
|
|
74
|
+
type: 'function';
|
|
75
|
+
run: (vars: V, context: RenderContext) => Promise<R>;
|
|
76
|
+
schema: ZodObject<ZodRawShape>;
|
|
77
|
+
messageSchema: null;
|
|
78
|
+
outputSchema: ZodTypeAny;
|
|
59
79
|
}
|
|
60
|
-
|
|
80
|
+
interface StreamChain<V extends Record<string, any> = Record<string, any>, R extends JSONSerializable = any> {
|
|
61
81
|
key: string;
|
|
62
|
-
|
|
82
|
+
type: 'stream';
|
|
83
|
+
run: (vars: V, context: RenderContext) => AsyncGenerator<R, void>;
|
|
63
84
|
schema: ZodObject<ZodRawShape>;
|
|
64
|
-
|
|
85
|
+
messageSchema: ZodTypeAny;
|
|
86
|
+
outputSchema: null;
|
|
87
|
+
}
|
|
65
88
|
|
|
66
89
|
declare const LoggerContext: Context<LogImplementation>;
|
|
67
90
|
type ContextValues = Record<symbol, any>;
|
|
@@ -80,9 +103,12 @@ interface RenderContext {
|
|
|
80
103
|
logger: Logger;
|
|
81
104
|
getContext<T>(context: Context<T>): T;
|
|
82
105
|
render(renderable: Renderable, opts?: RenderOptions): RenderResult;
|
|
83
|
-
render<V extends Record<string, any>, O>(prompt: PromptParsed<V, O>, variables: V, options?: RenderPromptOptions): Promise<O>;
|
|
106
|
+
render<V extends Record<string, any>, O extends JSONSerializable>(prompt: PromptParsed<V, O>, variables: V, options?: RenderPromptOptions): Promise<O>;
|
|
84
107
|
render<V extends Record<string, any>>(prompt: Prompt<V>, variables: V): RenderResult;
|
|
85
|
-
render<V extends Record<string, any>, O>(chain:
|
|
108
|
+
render<V extends Record<string, any>, O extends JSONSerializable>(chain: StreamChain<V, O>, variables: V): AsyncGenerator<O, void>;
|
|
109
|
+
render<V extends Record<string, any>, O extends JSONSerializable>(chain: FunctionChain<V, O>, variables: V): Promise<O>;
|
|
110
|
+
runChain<V extends Record<string, any>, R extends JSONSerializable>(chain: FunctionChain<V, R>, variables: V): Promise<R>;
|
|
111
|
+
runChain<V extends Record<string, any>, R extends JSONSerializable>(chain: StreamChain<V, R>, variables: V): AsyncGenerator<R, void>;
|
|
86
112
|
}
|
|
87
113
|
type RenderPromptOptions = {
|
|
88
114
|
validateOutput?: boolean;
|
|
@@ -200,4 +226,4 @@ declare function AIFragment({ children }: {
|
|
|
200
226
|
children: AINode;
|
|
201
227
|
}): Renderable;
|
|
202
228
|
|
|
203
|
-
export { type AIComponent as A, BoundLogger as B, type ContextValues as C, type PropsOfAIComponent as D, type EvaluatorFn as E, type
|
|
229
|
+
export { type AIComponent as A, BoundLogger as B, type ContextValues as C, type PropsOfAIComponent as D, type EvaluatorFn as E, type FunctionChain as F, type EvaluatorResult as G, type PromptRenderResult as H, type JSONSerializable as J, LogImplementation as L, NoopLogImplementation 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 Context as b, type AINode as c, createAIElement as d, AIFragment as e, LoggerContext 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 ChatCompletionRequestPayloads as n, type LogChatCompletionRequest as o, type LogChatCompletionResponse as p, type LogLevel as q, type Logger as r, ConsoleLogger as s, CombinedLogger as t, type Literal as u, type RenderableStream as v, type RenderResult as w, attachedContextSymbol as x, type AIElement as y, type Renderable as z };
|
package/dist/index.d.mts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { R as RenderContext, L as LogImplementation, C as ContextValues, A as AIComponent, E as EvaluatorFn, P as PromptParsed, a as Prompt,
|
|
2
|
-
export { y as AIElement,
|
|
1
|
+
import { R as RenderContext, L as LogImplementation, C as ContextValues, A as AIComponent, E as EvaluatorFn, P as PromptParsed, a as Prompt, J as JSONSerializable, F as FunctionChain, S as StreamChain, b as Context, c as AINode } from './createElement-eXTBmWMH.mjs';
|
|
2
|
+
export { y as AIElement, e as AIFragment, j as AssistantMessage, B as BoundLogger, m as ChatCompletionError, n as ChatCompletionRequestPayloads, h as ChatCompletionRole, t as CombinedLogger, s as ConsoleLogger, G as EvaluatorResult, u as Literal, o as LogChatCompletionRequest, p as LogChatCompletionResponse, q as LogLevel, r as Logger, f as LoggerContext, N as NoopLogImplementation, O as OutputParser, H as PromptRenderResult, D as PropsOfAIComponent, w as RenderResult, z as Renderable, v as RenderableStream, k as RenderedConversationMessage, i as SystemMessage, U as UserMessage, x as attachedContextSymbol, l as computeUsage, d as createAIElement, g as createContext } from './createElement-eXTBmWMH.mjs';
|
|
3
3
|
import { ZodObject, ZodRawShape, ZodTypeAny, ZodString, z } from 'zod';
|
|
4
4
|
import { OpenAI } from 'openai';
|
|
5
5
|
export { OpenAI as OpenAIClient } from 'openai';
|
|
@@ -37,11 +37,19 @@ declare function createPrompt<K extends string, I extends ZodObject<ZodRawShape>
|
|
|
37
37
|
metadata?: Record<string, any>;
|
|
38
38
|
}): Prompt<z.infer<I>>;
|
|
39
39
|
|
|
40
|
-
|
|
40
|
+
type Awaited<T> = T extends PromiseLike<infer U> ? U : T;
|
|
41
|
+
declare function createFunctionChain<I extends ZodObject<ZodRawShape>, R extends JSONSerializable | PromiseLike<JSONSerializable>>(chain: {
|
|
41
42
|
key: string;
|
|
42
43
|
run: (variables: z.infer<I>, context: RenderContext) => R;
|
|
44
|
+
outputSchema?: ZodTypeAny;
|
|
43
45
|
schema: I;
|
|
44
|
-
}):
|
|
46
|
+
}): FunctionChain<z.infer<I>, Awaited<R>>;
|
|
47
|
+
declare function createStreamChain<I extends ZodObject<ZodRawShape>, M extends ZodTypeAny, R extends JSONSerializable = z.infer<M>>(chain: {
|
|
48
|
+
key: string;
|
|
49
|
+
run: (variables: z.infer<I>, context: RenderContext) => AsyncGenerator<R>;
|
|
50
|
+
schema: I;
|
|
51
|
+
messageSchema?: M;
|
|
52
|
+
}): StreamChain<z.infer<I>, R>;
|
|
45
53
|
|
|
46
54
|
declare class PromptParseVariablesError extends Error {
|
|
47
55
|
}
|
|
@@ -130,4 +138,4 @@ type AnthropicChatCompletionProps = {
|
|
|
130
138
|
*/
|
|
131
139
|
declare function AnthropicChatCompletion(props: AnthropicChatCompletionProps, { render, logger, getContext }: RenderContext): AsyncGenerator<string, void, unknown>;
|
|
132
140
|
|
|
133
|
-
export { AIComponent, AINode, AnthropicChatCompletion, type AnthropicChatCompletionRequest, AnthropicClientContext,
|
|
141
|
+
export { AIComponent, AINode, AnthropicChatCompletion, type AnthropicChatCompletionRequest, AnthropicClientContext, ChainParseVariablesError, ContentTypeImage, Context, EvaluatorFn, FunctionChain, JSONSerializable, LogImplementation, OpenAIChatCompletion, type OpenAIChatCompletionRequest, type OpenAIChatMessage, OpenAIClientContext, OpenAIVisionChatCompletion, Prompt, PromptInvalidOutputError, PromptParseVariablesError, PromptParsed, RenderContext, StreamChain, type ValidAnthropicChatModel, type ValidOpenAIChatModel, type ValidOpenAIVisionModel, createFunctionChain, createPrompt, createRenderContext, createStreamChain, defaultMaxTokens, tokenCountForOpenAIMessage, tokenCountForOpenAIVisionMessage, tokenLimitForChatModel, tokenizer };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { R as RenderContext, L as LogImplementation, C as ContextValues, A as AIComponent, E as EvaluatorFn, P as PromptParsed, a as Prompt,
|
|
2
|
-
export { y as AIElement,
|
|
1
|
+
import { R as RenderContext, L as LogImplementation, C as ContextValues, A as AIComponent, E as EvaluatorFn, P as PromptParsed, a as Prompt, J as JSONSerializable, F as FunctionChain, S as StreamChain, b as Context, c as AINode } from './createElement-eXTBmWMH.js';
|
|
2
|
+
export { y as AIElement, e as AIFragment, j as AssistantMessage, B as BoundLogger, m as ChatCompletionError, n as ChatCompletionRequestPayloads, h as ChatCompletionRole, t as CombinedLogger, s as ConsoleLogger, G as EvaluatorResult, u as Literal, o as LogChatCompletionRequest, p as LogChatCompletionResponse, q as LogLevel, r as Logger, f as LoggerContext, N as NoopLogImplementation, O as OutputParser, H as PromptRenderResult, D as PropsOfAIComponent, w as RenderResult, z as Renderable, v as RenderableStream, k as RenderedConversationMessage, i as SystemMessage, U as UserMessage, x as attachedContextSymbol, l as computeUsage, d as createAIElement, g as createContext } from './createElement-eXTBmWMH.js';
|
|
3
3
|
import { ZodObject, ZodRawShape, ZodTypeAny, ZodString, z } from 'zod';
|
|
4
4
|
import { OpenAI } from 'openai';
|
|
5
5
|
export { OpenAI as OpenAIClient } from 'openai';
|
|
@@ -37,11 +37,19 @@ declare function createPrompt<K extends string, I extends ZodObject<ZodRawShape>
|
|
|
37
37
|
metadata?: Record<string, any>;
|
|
38
38
|
}): Prompt<z.infer<I>>;
|
|
39
39
|
|
|
40
|
-
|
|
40
|
+
type Awaited<T> = T extends PromiseLike<infer U> ? U : T;
|
|
41
|
+
declare function createFunctionChain<I extends ZodObject<ZodRawShape>, R extends JSONSerializable | PromiseLike<JSONSerializable>>(chain: {
|
|
41
42
|
key: string;
|
|
42
43
|
run: (variables: z.infer<I>, context: RenderContext) => R;
|
|
44
|
+
outputSchema?: ZodTypeAny;
|
|
43
45
|
schema: I;
|
|
44
|
-
}):
|
|
46
|
+
}): FunctionChain<z.infer<I>, Awaited<R>>;
|
|
47
|
+
declare function createStreamChain<I extends ZodObject<ZodRawShape>, M extends ZodTypeAny, R extends JSONSerializable = z.infer<M>>(chain: {
|
|
48
|
+
key: string;
|
|
49
|
+
run: (variables: z.infer<I>, context: RenderContext) => AsyncGenerator<R>;
|
|
50
|
+
schema: I;
|
|
51
|
+
messageSchema?: M;
|
|
52
|
+
}): StreamChain<z.infer<I>, R>;
|
|
45
53
|
|
|
46
54
|
declare class PromptParseVariablesError extends Error {
|
|
47
55
|
}
|
|
@@ -130,4 +138,4 @@ type AnthropicChatCompletionProps = {
|
|
|
130
138
|
*/
|
|
131
139
|
declare function AnthropicChatCompletion(props: AnthropicChatCompletionProps, { render, logger, getContext }: RenderContext): AsyncGenerator<string, void, unknown>;
|
|
132
140
|
|
|
133
|
-
export { AIComponent, AINode, AnthropicChatCompletion, type AnthropicChatCompletionRequest, AnthropicClientContext,
|
|
141
|
+
export { AIComponent, AINode, AnthropicChatCompletion, type AnthropicChatCompletionRequest, AnthropicClientContext, ChainParseVariablesError, ContentTypeImage, Context, EvaluatorFn, FunctionChain, JSONSerializable, LogImplementation, OpenAIChatCompletion, type OpenAIChatCompletionRequest, type OpenAIChatMessage, OpenAIClientContext, OpenAIVisionChatCompletion, Prompt, PromptInvalidOutputError, PromptParseVariablesError, PromptParsed, RenderContext, StreamChain, type ValidAnthropicChatModel, type ValidOpenAIChatModel, type ValidOpenAIVisionModel, createFunctionChain, createPrompt, createRenderContext, createStreamChain, defaultMaxTokens, tokenCountForOpenAIMessage, tokenCountForOpenAIVisionMessage, tokenLimitForChatModel, tokenizer };
|
package/dist/index.js
CHANGED
|
@@ -55,10 +55,11 @@ __export(src_exports, {
|
|
|
55
55
|
computeUsage: () => computeUsage,
|
|
56
56
|
countAnthropicTokens: () => import_tokenizer4.countTokens,
|
|
57
57
|
createAIElement: () => createAIElement,
|
|
58
|
-
createChain: () => createChain,
|
|
59
58
|
createContext: () => createContext,
|
|
59
|
+
createFunctionChain: () => createFunctionChain,
|
|
60
60
|
createPrompt: () => createPrompt,
|
|
61
61
|
createRenderContext: () => createRenderContext,
|
|
62
|
+
createStreamChain: () => createStreamChain,
|
|
62
63
|
defaultMaxTokens: () => defaultMaxTokens,
|
|
63
64
|
tokenCountForOpenAIMessage: () => tokenCountForOpenAIMessage,
|
|
64
65
|
tokenCountForOpenAIVisionMessage: () => tokenCountForOpenAIVisionMessage,
|
|
@@ -652,13 +653,17 @@ var StreamRenderContext = class _StreamRenderContext {
|
|
|
652
653
|
// Implementation of the overloaded function
|
|
653
654
|
render(arg1, arg2, arg3) {
|
|
654
655
|
if (arg1 && typeof arg1 === "object" && "component" in arg1 && arg2) {
|
|
655
|
-
if ("
|
|
656
|
+
if ("parse" in arg1) {
|
|
656
657
|
const result = this.renderPromptParsed(arg1, arg2, arg3 || {});
|
|
657
658
|
return result;
|
|
658
659
|
}
|
|
659
660
|
return this.renderPrompt(arg1, arg2);
|
|
660
661
|
} else if (arg1 && typeof arg1 === "object" && "run" in arg1 && arg2) {
|
|
661
|
-
|
|
662
|
+
if (arg1.type === "function") {
|
|
663
|
+
return this.runChain(arg1, arg2);
|
|
664
|
+
} else {
|
|
665
|
+
return this.runChain(arg1, arg2);
|
|
666
|
+
}
|
|
662
667
|
} else {
|
|
663
668
|
const generator = this.renderStream(
|
|
664
669
|
arg1,
|
|
@@ -695,11 +700,10 @@ var StreamRenderContext = class _StreamRenderContext {
|
|
|
695
700
|
throw new PromptParseVariablesError("Invalid input variables: " + errors);
|
|
696
701
|
}
|
|
697
702
|
const output = await this.render(/* @__PURE__ */ jsx(Component, { ...parsedVariables }));
|
|
698
|
-
const
|
|
699
|
-
const parsedOutput = outputParser.parse(output);
|
|
703
|
+
const parsedOutput = prompt.parse(output);
|
|
700
704
|
if (validateOutput) {
|
|
701
705
|
try {
|
|
702
|
-
|
|
706
|
+
prompt.outputSchema.parse(parsedOutput);
|
|
703
707
|
} catch (e) {
|
|
704
708
|
const err = e;
|
|
705
709
|
const errors = JSON.stringify(err.flatten());
|
|
@@ -718,7 +722,13 @@ var StreamRenderContext = class _StreamRenderContext {
|
|
|
718
722
|
const errors = JSON.stringify(err.flatten());
|
|
719
723
|
throw new ChainParseVariablesError("Invalid input variables: " + errors);
|
|
720
724
|
}
|
|
721
|
-
|
|
725
|
+
if (chain.type === "function") {
|
|
726
|
+
return chain.run(parsedVariables, this);
|
|
727
|
+
}
|
|
728
|
+
if (chain.type === "stream") {
|
|
729
|
+
return chain.run(parsedVariables, this);
|
|
730
|
+
}
|
|
731
|
+
throw new Error("Invalid chain type");
|
|
722
732
|
}
|
|
723
733
|
getContext = (context) => {
|
|
724
734
|
return this.contextValues[context.key] ?? context.defaultValue;
|
|
@@ -782,6 +792,7 @@ function attachedContextValues(element) {
|
|
|
782
792
|
}
|
|
783
793
|
|
|
784
794
|
// src/prompt/createPrompt.tsx
|
|
795
|
+
var import_zod = require("zod");
|
|
785
796
|
function createPrompt(config) {
|
|
786
797
|
if (config.outputParser) {
|
|
787
798
|
const {
|
|
@@ -795,7 +806,9 @@ function createPrompt(config) {
|
|
|
795
806
|
return {
|
|
796
807
|
key: key2,
|
|
797
808
|
component: component2,
|
|
798
|
-
outputParser,
|
|
809
|
+
parse: outputParser.parse,
|
|
810
|
+
outputSchema: outputParser.schema,
|
|
811
|
+
messageSchema: null,
|
|
799
812
|
schema: schema2,
|
|
800
813
|
metadata: metadata2,
|
|
801
814
|
evaluators: evaluators2
|
|
@@ -807,13 +820,39 @@ function createPrompt(config) {
|
|
|
807
820
|
component,
|
|
808
821
|
schema,
|
|
809
822
|
metadata,
|
|
810
|
-
evaluators
|
|
823
|
+
evaluators,
|
|
824
|
+
messageSchema: import_zod.z.string(),
|
|
825
|
+
outputSchema: import_zod.z.string()
|
|
811
826
|
};
|
|
812
827
|
}
|
|
813
828
|
|
|
814
829
|
// src/prompt/createChain.ts
|
|
815
|
-
|
|
816
|
-
|
|
830
|
+
var import_zod2 = require("zod");
|
|
831
|
+
function createFunctionChain(chain) {
|
|
832
|
+
const { key, run, schema, outputSchema = import_zod2.z.any() } = chain;
|
|
833
|
+
const asyncRun = async (vars, context) => {
|
|
834
|
+
return run(vars, context);
|
|
835
|
+
};
|
|
836
|
+
return {
|
|
837
|
+
type: "function",
|
|
838
|
+
key,
|
|
839
|
+
schema,
|
|
840
|
+
messageSchema: null,
|
|
841
|
+
outputSchema,
|
|
842
|
+
// always make it async so RenderContext doesnt have a to guess
|
|
843
|
+
run: asyncRun
|
|
844
|
+
};
|
|
845
|
+
}
|
|
846
|
+
function createStreamChain(chain) {
|
|
847
|
+
const { key, run, messageSchema = import_zod2.z.any(), schema } = chain;
|
|
848
|
+
return {
|
|
849
|
+
type: "stream",
|
|
850
|
+
key,
|
|
851
|
+
schema,
|
|
852
|
+
run,
|
|
853
|
+
outputSchema: null,
|
|
854
|
+
messageSchema
|
|
855
|
+
};
|
|
817
856
|
}
|
|
818
857
|
|
|
819
858
|
// src/lib/openai/OpenAI.tsx
|
|
@@ -1392,10 +1431,11 @@ var import_tokenizer4 = require("@anthropic-ai/tokenizer");
|
|
|
1392
1431
|
computeUsage,
|
|
1393
1432
|
countAnthropicTokens,
|
|
1394
1433
|
createAIElement,
|
|
1395
|
-
createChain,
|
|
1396
1434
|
createContext,
|
|
1435
|
+
createFunctionChain,
|
|
1397
1436
|
createPrompt,
|
|
1398
1437
|
createRenderContext,
|
|
1438
|
+
createStreamChain,
|
|
1399
1439
|
defaultMaxTokens,
|
|
1400
1440
|
tokenCountForOpenAIMessage,
|
|
1401
1441
|
tokenCountForOpenAIVisionMessage,
|
package/dist/index.mjs
CHANGED
|
@@ -558,13 +558,17 @@ var StreamRenderContext = class _StreamRenderContext {
|
|
|
558
558
|
// Implementation of the overloaded function
|
|
559
559
|
render(arg1, arg2, arg3) {
|
|
560
560
|
if (arg1 && typeof arg1 === "object" && "component" in arg1 && arg2) {
|
|
561
|
-
if ("
|
|
561
|
+
if ("parse" in arg1) {
|
|
562
562
|
const result = this.renderPromptParsed(arg1, arg2, arg3 || {});
|
|
563
563
|
return result;
|
|
564
564
|
}
|
|
565
565
|
return this.renderPrompt(arg1, arg2);
|
|
566
566
|
} else if (arg1 && typeof arg1 === "object" && "run" in arg1 && arg2) {
|
|
567
|
-
|
|
567
|
+
if (arg1.type === "function") {
|
|
568
|
+
return this.runChain(arg1, arg2);
|
|
569
|
+
} else {
|
|
570
|
+
return this.runChain(arg1, arg2);
|
|
571
|
+
}
|
|
568
572
|
} else {
|
|
569
573
|
const generator = this.renderStream(
|
|
570
574
|
arg1,
|
|
@@ -601,11 +605,10 @@ var StreamRenderContext = class _StreamRenderContext {
|
|
|
601
605
|
throw new PromptParseVariablesError("Invalid input variables: " + errors);
|
|
602
606
|
}
|
|
603
607
|
const output = await this.render(/* @__PURE__ */ jsx(Component, { ...parsedVariables }));
|
|
604
|
-
const
|
|
605
|
-
const parsedOutput = outputParser.parse(output);
|
|
608
|
+
const parsedOutput = prompt.parse(output);
|
|
606
609
|
if (validateOutput) {
|
|
607
610
|
try {
|
|
608
|
-
|
|
611
|
+
prompt.outputSchema.parse(parsedOutput);
|
|
609
612
|
} catch (e) {
|
|
610
613
|
const err = e;
|
|
611
614
|
const errors = JSON.stringify(err.flatten());
|
|
@@ -624,7 +627,13 @@ var StreamRenderContext = class _StreamRenderContext {
|
|
|
624
627
|
const errors = JSON.stringify(err.flatten());
|
|
625
628
|
throw new ChainParseVariablesError("Invalid input variables: " + errors);
|
|
626
629
|
}
|
|
627
|
-
|
|
630
|
+
if (chain.type === "function") {
|
|
631
|
+
return chain.run(parsedVariables, this);
|
|
632
|
+
}
|
|
633
|
+
if (chain.type === "stream") {
|
|
634
|
+
return chain.run(parsedVariables, this);
|
|
635
|
+
}
|
|
636
|
+
throw new Error("Invalid chain type");
|
|
628
637
|
}
|
|
629
638
|
getContext = (context) => {
|
|
630
639
|
return this.contextValues[context.key] ?? context.defaultValue;
|
|
@@ -688,6 +697,7 @@ function attachedContextValues(element) {
|
|
|
688
697
|
}
|
|
689
698
|
|
|
690
699
|
// src/prompt/createPrompt.tsx
|
|
700
|
+
import { z } from "zod";
|
|
691
701
|
function createPrompt(config) {
|
|
692
702
|
if (config.outputParser) {
|
|
693
703
|
const {
|
|
@@ -701,7 +711,9 @@ function createPrompt(config) {
|
|
|
701
711
|
return {
|
|
702
712
|
key: key2,
|
|
703
713
|
component: component2,
|
|
704
|
-
outputParser,
|
|
714
|
+
parse: outputParser.parse,
|
|
715
|
+
outputSchema: outputParser.schema,
|
|
716
|
+
messageSchema: null,
|
|
705
717
|
schema: schema2,
|
|
706
718
|
metadata: metadata2,
|
|
707
719
|
evaluators: evaluators2
|
|
@@ -713,13 +725,39 @@ function createPrompt(config) {
|
|
|
713
725
|
component,
|
|
714
726
|
schema,
|
|
715
727
|
metadata,
|
|
716
|
-
evaluators
|
|
728
|
+
evaluators,
|
|
729
|
+
messageSchema: z.string(),
|
|
730
|
+
outputSchema: z.string()
|
|
717
731
|
};
|
|
718
732
|
}
|
|
719
733
|
|
|
720
734
|
// src/prompt/createChain.ts
|
|
721
|
-
|
|
722
|
-
|
|
735
|
+
import { z as z2 } from "zod";
|
|
736
|
+
function createFunctionChain(chain) {
|
|
737
|
+
const { key, run, schema, outputSchema = z2.any() } = chain;
|
|
738
|
+
const asyncRun = async (vars, context) => {
|
|
739
|
+
return run(vars, context);
|
|
740
|
+
};
|
|
741
|
+
return {
|
|
742
|
+
type: "function",
|
|
743
|
+
key,
|
|
744
|
+
schema,
|
|
745
|
+
messageSchema: null,
|
|
746
|
+
outputSchema,
|
|
747
|
+
// always make it async so RenderContext doesnt have a to guess
|
|
748
|
+
run: asyncRun
|
|
749
|
+
};
|
|
750
|
+
}
|
|
751
|
+
function createStreamChain(chain) {
|
|
752
|
+
const { key, run, messageSchema = z2.any(), schema } = chain;
|
|
753
|
+
return {
|
|
754
|
+
type: "stream",
|
|
755
|
+
key,
|
|
756
|
+
schema,
|
|
757
|
+
run,
|
|
758
|
+
outputSchema: null,
|
|
759
|
+
messageSchema
|
|
760
|
+
};
|
|
723
761
|
}
|
|
724
762
|
|
|
725
763
|
// src/lib/openai/OpenAI.tsx
|
|
@@ -1297,10 +1335,11 @@ export {
|
|
|
1297
1335
|
computeUsage,
|
|
1298
1336
|
countAnthropicTokens,
|
|
1299
1337
|
createAIElement,
|
|
1300
|
-
createChain,
|
|
1301
1338
|
createContext,
|
|
1339
|
+
createFunctionChain,
|
|
1302
1340
|
createPrompt,
|
|
1303
1341
|
createRenderContext,
|
|
1342
|
+
createStreamChain,
|
|
1304
1343
|
defaultMaxTokens,
|
|
1305
1344
|
tokenCountForOpenAIMessage,
|
|
1306
1345
|
tokenCountForOpenAIVisionMessage,
|
package/dist/jsx-runtime.d.mts
CHANGED
package/dist/jsx-runtime.d.ts
CHANGED