Skip to content

Commit

Permalink
Make it so makeSeedData runs the makeSeedReferenceProperty on every v…
Browse files Browse the repository at this point in the history
…alue automatically.

No semantic difference, just the type construction for seed data is automated.

There's a cast in there (as Shape) that doesn't feel right, but it still works so 🤷

Part of #2.
  • Loading branch information
jkomoros committed Jun 24, 2023
1 parent ac6b26e commit 10e2c79
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 17 deletions.
21 changes: 21 additions & 0 deletions src/typed-object.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//Versions of Object.entries(), Object.keys(), and Object.values() that preserve
//type for constrained type maps. By default they return [string, any]

//Use: instead of Object.keys(), do TypedObject.keys()

type Entries<T> = { [K in keyof T]: [K, T[K]] }[keyof T];

export class TypedObject {

//Based on https://stackoverflow.com/a/59459000
static keys<T extends object>(t : T): Array<keyof T> {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return Object.keys(t) as any;
}

//Based on https://stackoverflow.com/a/62055863
static entries<T extends object>(t: T): Entries<T>[] {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return Object.entries(t) as any;
}
}
33 changes: 16 additions & 17 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import {
z
} from 'zod';

import {
TypedObject
} from './typed-object.js';

const CHANGE_ME_SENTINEL = 'CHANGE_ME';

const value = z.union([
Expand Down Expand Up @@ -132,9 +136,14 @@ type SeedDataConfiguration<Kind extends z.ZodLiteral<string>, Shape extends z.Zo
};

const makeSeedData = <Kind extends z.ZodLiteral<string>, Shape extends z.ZodRawShape>(config : SeedDataConfiguration<Kind, Shape>) => {
const entries = TypedObject.entries(config.properties).map(entry => [entry[0], makeSeedReferenceProperty(entry[1])]);
//TODO: this cast is actually wrong... but it still works as expected?
const modifiedProperties = Object.fromEntries(entries) as Shape;
return seedDataBase.extend({
type: config.type
}).extend(config.properties);
type: config.type,
}).extend(
modifiedProperties
);
};

/*
Expand All @@ -146,9 +155,7 @@ const makeSeedData = <Kind extends z.ZodLiteral<string>, Shape extends z.ZodRawS
export const seedDataPrompt = makeSeedData({
type: z.literal('prompt'),
properties: {
prompt: makeSeedReferenceProperty(
z.string().describe('The full prompt to be passed to the configured commpletion_model')
)
prompt: z.string().describe('The full prompt to be passed to the configured commpletion_model')
}
});

Expand All @@ -157,9 +164,7 @@ export type SeedDataPrompt = z.infer<typeof seedDataPrompt>;
export const seedDataLog = makeSeedData({
type: z.literal('log'),
properties: {
value: makeSeedReferenceProperty(
value.describe('The message to echo back')
)
value: value.describe('The message to echo back')
}
});

Expand All @@ -168,15 +173,9 @@ export type SeedDataLog = z.infer<typeof seedDataLog>;
export const seedDataIf = makeSeedData({
type: z.literal('if'),
properties: {
test: makeSeedReferenceProperty(
value.describe('The value to examine')
),
then: makeSeedReferenceProperty(
value.describe('The value to return if the value of test is truthy')
),
else: makeSeedReferenceProperty(
value.describe('The value to return if the value of test is falsy')
)
test: value.describe('The value to examine'),
then: value.describe('The value to return if the value of test is truthy'),
else: value.describe('The value to return if the value of test is falsy')
}
});

Expand Down

0 comments on commit 10e2c79

Please sign in to comment.