Skip to content

add arkType support #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"typescript": "^5.0.4"
},
"dependencies": {
"arktype": "1.0.14-alpha",
"zod": "^3.21.4"
}
}
7 changes: 7 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

85 changes: 69 additions & 16 deletions src/PromptBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { z } from "zod";
import { Type } from "arktype";
import { F } from "ts-toolbelt";
import { Prompt } from "./Prompt";
import { ExtractArgs, ReplaceArgs, TypeToZodShape } from "./types";
Expand All @@ -18,23 +19,13 @@ export class PromptBuilder<
addZodInputValidation<TShape extends TExpectedInput>(
shape: TypeToZodShape<TShape>
) {
const zodValidator = z.object(shape as any);
return new (class extends PromptBuilder<TPromptTemplate, TShape> {
validate(args: Record<string, any>): args is TShape {
return zodValidator.safeParse(args).success;
}

get type() {
return this.template as ReplaceArgs<TPromptTemplate, TShape>;
}
return new ZodPromptBuilder(this.template, shape);
}

build<TSuppliedInputArgs extends TShape>(
args: F.Narrow<TSuppliedInputArgs>
) {
zodValidator.parse(args);
return new Prompt(this.template, args).toString();
}
})(this.template);
addArkTypeInputValidation<TShape extends TExpectedInput>(
shape: Type<TShape>
) {
return new ArkTypePromptBuilder(this.template, shape);
}

validate(args: Record<string, any>): args is TExpectedInput {
Expand All @@ -52,3 +43,65 @@ export class PromptBuilder<
return new Prompt(this.template, args).toString();
}
}

class ZodPromptBuilder<
TPromptTemplate extends string,
TExpectedInput extends ExtractArgs<TPromptTemplate, {}>
> extends PromptBuilder<TPromptTemplate, TExpectedInput> {
constructor(
public template: TPromptTemplate,
public shape: TypeToZodShape<TExpectedInput>
) {
super(template);
}
validate(args: Record<string, any>): args is TExpectedInput {
const zodValidator = z.object(this.shape as any);
return zodValidator.safeParse(args).success;
}

get type() {
return this.template as ReplaceArgs<TPromptTemplate, TExpectedInput>;
}

build<TSuppliedInputArgs extends TExpectedInput>(
args: F.Narrow<TSuppliedInputArgs>
) {
const zodValidator = z.object(this.shape as any);
zodValidator.parse(args);
return new Prompt(this.template, args).toString();
}
}

class ArkTypePromptBuilder<
TPromptTemplate extends string,
TExpectedInput extends ExtractArgs<TPromptTemplate, {}>
> extends PromptBuilder<TPromptTemplate, TExpectedInput> {
constructor(
public template: TPromptTemplate,
public shape: Type<TExpectedInput>
) {
super(template);
}
validate(args: Record<string, any>): args is TExpectedInput {
try {
this.shape(args);
return true;
} catch (e) {
return false;
}
}

get type() {
return this.template as ReplaceArgs<TPromptTemplate, TExpectedInput>;
}

build<TSuppliedInputArgs extends TExpectedInput>(
args: F.Narrow<TSuppliedInputArgs>
) {
const { problems } = this.shape(args);
if (problems?.summary) {
throw new Error(problems.summary);
}
return new Prompt(this.template, args).toString();
}
}
59 changes: 46 additions & 13 deletions src/__tests__/PromptBuilder.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { strict as assert } from "node:assert";
import { z, ZodError } from "zod";
import { type } from 'arktype';
import { PromptBuilder } from "../PromptBuilder";
import { Equal, Expect } from "./types.test";

Expand Down Expand Up @@ -282,7 +283,9 @@ describe("PromptBuilder with input validation using Zod", () => {
);
type BasicType = typeof promptBuilder.type;
// ^?
type BasicTest = Expect<Equal<BasicType, `Tell ${any} ${any} ${any} ${any} joke`>>;
type BasicTest = Expect<
Equal<BasicType, `Tell ${any} ${any} ${any} ${any} joke`>
>;

const tsValidatedPromptBuilder = promptBuilder.addInputValidation<{
jokeType: "funny" | "silly";
Expand All @@ -305,34 +308,37 @@ describe("PromptBuilder with input validation using Zod", () => {
});

test("Can write a function that accepts the type of a PromptBuilder then accepts any output from that builder", () => {
const promptBuilder = new PromptBuilder("Tell me a {{jokeType}} joke.").addInputValidation<{
jokeType: "funny" | "silly"
const promptBuilder = new PromptBuilder(
"Tell me a {{jokeType}} joke."
).addInputValidation<{
jokeType: "funny" | "silly";
}>();
function exampleFunction(input: typeof promptBuilder.type) {}

exampleFunction(promptBuilder.build({ jokeType: "funny" }));
exampleFunction("Tell me a funny joke.")
exampleFunction("Tell me a funny joke.");
exampleFunction(promptBuilder.build({ jokeType: "silly" }));
exampleFunction("Tell me a silly joke.")
exampleFunction("Tell me a silly joke.");
// @ts-expect-error
exampleFunction(promptBuilder.build({ jokeType: "bad" }));
// @ts-expect-error
exampleFunction("Tell me a bad joke.")
})

exampleFunction("Tell me a bad joke.");
});

test("Can write a function that accepts the type of a PromptBuilder then accepts any output from that builder", () => {
const promptBuilder = new PromptBuilder("Tell me a {{jokeType}} joke.").addZodInputValidation({
const promptBuilder = new PromptBuilder(
"Tell me a {{jokeType}} joke."
).addZodInputValidation({
jokeType: z.union([z.literal("funny"), z.literal("silly")]),
});
function exampleFunction(input: typeof promptBuilder.type) {}

exampleFunction(promptBuilder.build({ jokeType: "funny" }));
exampleFunction("Tell me a funny joke.")
exampleFunction("Tell me a funny joke.");
exampleFunction(promptBuilder.build({ jokeType: "silly" }));
exampleFunction("Tell me a silly joke.")
exampleFunction("Tell me a silly joke.");
// @ts-expect-error
exampleFunction("Tell me a bad joke.")
exampleFunction("Tell me a bad joke.");
assert.throws(
() => {
// @ts-expect-error
Expand All @@ -350,5 +356,32 @@ describe("PromptBuilder with input validation using Zod", () => {
return true;
}
);
})
});

test("Can add arktypes validation", () => {
const promptBuilder = new PromptBuilder(
"Tell me a {{jokeType}} joke."
).addArkTypeInputValidation(
type({
jokeType: "'funny' | 'silly'",
})
);
function exampleFunction(input: typeof promptBuilder.type) {}

exampleFunction(promptBuilder.build({ jokeType: "funny" }));
exampleFunction("Tell me a funny joke.");
exampleFunction(promptBuilder.build({ jokeType: "silly" }));
exampleFunction("Tell me a silly joke.");
// @ts-expect-error
exampleFunction("Tell me a bad joke.");

assert.throws(
() => {

// @ts-expect-error
exampleFunction(promptBuilder.build({ jokeType: "bad" }));
}
);

});
});