Skip to content

Commit

Permalink
Add seed_type dynamic.
Browse files Browse the repository at this point in the history
It allows executing a seed reference that isn't bound until run-time.

This will be very useful for #20.

Part of #21.
  • Loading branch information
jkomoros committed Jul 9, 2023
1 parent 6341699 commit 734b260
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 5 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ You can also fetch a remote packet:
}
```
All seed references so far have been constant references. If you want to execute a seed that varies at run-time, see the `dynamic` seed type.
When you're building complex seeds, you'll likely have many many sub-seeds nested deeply, since each seed is a very basic operation.
It can get annoying to create a lot of different seeds at the top-level, name them, and then keep track of when their names change.
Expand Down Expand Up @@ -479,6 +481,15 @@ Parameters:
- `seed_id` - The ID of the seed to select. (defaults to '')
- `packet` - (optional) The location of the packet. Can be relative. If omitted defaults to the packet it's being called from.
#### dynamic
Executes a reference to another seed. Like SeedReference, but doesn't have to be set at authoring time. Useful for creating 'meta-nodes'.
It will fail if the packet location is a remote seed packet.
Parameters:
- `reference` - The packed reference to the seed to execute. You can retrieve one with `reference`
#### property
Selects a named property from an object
Expand Down
43 changes: 40 additions & 3 deletions seed-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1620,6 +1620,43 @@
],
"additionalProperties": false
},
{
"type": "object",
"properties": {
"id": {
"$ref": "#/definitions/seedData/anyOf/0/properties/id"
},
"description": {
"$ref": "#/definitions/seedData/anyOf/0/properties/description"
},
"private": {
"$ref": "#/definitions/seedData/anyOf/0/properties/private"
},
"type": {
"type": "string",
"const": "dynamic"
},
"reference": {
"anyOf": [
{
"$ref": "#/definitions/seedData"
},
{
"$ref": "#/definitions/seedData/anyOf/0/properties/prompt/anyOf/1"
},
{
"type": "string",
"description": "The packed ID of the seed to reference"
}
]
}
},
"required": [
"type",
"reference"
],
"additionalProperties": false
},
{
"type": "object",
"properties": {
Expand Down Expand Up @@ -1741,7 +1778,7 @@
"items": {
"anyOf": [
{
"$ref": "#/definitions/seedData/anyOf/24/properties/properties/additionalProperties/anyOf/0"
"$ref": "#/definitions/seedData/anyOf/25/properties/properties/additionalProperties/anyOf/0"
},
{
"$ref": "#/definitions/seedData/anyOf/0/properties/prompt/anyOf/1"
Expand All @@ -1755,7 +1792,7 @@
"return": {
"anyOf": [
{
"$ref": "#/definitions/seedData/anyOf/24/properties/properties/additionalProperties/anyOf/0"
"$ref": "#/definitions/seedData/anyOf/25/properties/properties/additionalProperties/anyOf/0"
},
{
"$ref": "#/definitions/seedData/anyOf/0/properties/prompt/anyOf/1"
Expand Down Expand Up @@ -1867,7 +1904,7 @@
{
"anyOf": [
{
"$ref": "#/definitions/seedData/anyOf/26/properties/name/anyOf/2/anyOf/0"
"$ref": "#/definitions/seedData/anyOf/27/properties/name/anyOf/2/anyOf/0"
},
{
"$ref": "#/definitions/seedData/anyOf/2/properties/memory/anyOf/2/anyOf/1"
Expand Down
19 changes: 17 additions & 2 deletions src/grow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ import {
SeedDataAdd,
SeedDataMultiply,
SeedDataDivide,
SeedDataReference
SeedDataReference,
SeedDataDynamic
} from './types.js';

import {
Expand All @@ -58,8 +59,10 @@ import {
} from 'openai';

import {
isLocalLocation,
makeAbsolute,
packSeedReference
packSeedReference,
unpackSeedReference
} from './reference.js';

import {
Expand Down Expand Up @@ -487,6 +490,15 @@ const growReference = async (seed : Seed<SeedDataReference>, env : Environment)
return packSeedReference(absReference);
};

const growDynamic = async (seed : Seed<SeedDataDynamic>, env : Environment) : Promise<Value> => {
const data = seed.data;
const ref = extractString(await getProperty(seed, env, data.reference, ''));
if (!ref) throw new Error('empty reference');
const unpackedRef = unpackSeedReference(ref);
if (!isLocalLocation(unpackedRef.packet)) throw new Error(`Cannot load a dynamic remote seed packet: ${unpackedRef.packet}`);
return await growSubSeed(seed,env,unpackedRef);
};

const growProperty = async (seed : Seed<SeedDataProperty>, env : Environment) : Promise<Value> => {
const data = seed.data;
const obj = await getProperty(seed, env, data.object);
Expand Down Expand Up @@ -676,6 +688,9 @@ export const grow = async (seed : Seed, env : Environment) : Promise<Value> => {
case 'reference':
result = await growReference(seed as Seed<SeedDataReference>, env);
break;
case 'dynamic':
result = await growDynamic(seed as Seed<SeedDataDynamic>, env);
break;
case 'property':
result = await growProperty(seed as Seed<SeedDataProperty>, env);
break;
Expand Down
1 change: 1 addition & 0 deletions src/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ export class Seed<D extends ExpandedSeedData = ExpandedSeedData> {
return this._data;
}

//Note: does not include dynamic references
references(excludeRemote = false) : {[prop : string] : AbsoluteSeedReference} {
const result : {[prop : string] : AbsoluteSeedReference} = {};
for (const [key, value] of Object.entries(this.data)) {
Expand Down
14 changes: 14 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,18 @@ const seedDataReference = makeSeedData(seedDataConfigReference);

export type SeedDataReference = z.infer<typeof seedDataReference>;

const seedDataConfigDynamic = {
type: z.literal('dynamic'),
properties: {
reference: z.string().describe('The packed ID of the seed to reference')
}
};

const nestedSeedDataDynamic = makeNestedSeedData(seedDataConfigDynamic);
const seedDataDynamic = makeSeedData(seedDataConfigDynamic);

export type SeedDataDynamic = z.infer<typeof seedDataDynamic>;

const seedDataConfigProperty = {
type: z.literal('property'),
properties: {
Expand Down Expand Up @@ -818,6 +830,7 @@ export const expandedSeedData = z.discriminatedUnion('type', [
seedDataExtract,
seedDataInput,
seedDataReference,
seedDataDynamic,
seedDataProperty,
seedDataObject,
seedDataArray,
Expand Down Expand Up @@ -855,6 +868,7 @@ export const seedData = z.discriminatedUnion('type', [
nestedSeedDataExtract,
nestedSeedDataInput,
nestedSeedDataReference,
nestedSeedDataDynamic,
nestedSeedDataProperty,
nestedSeedDataObject,
nestedSeedDataArray,
Expand Down
15 changes: 15 additions & 0 deletions test/base/a_test.json
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,21 @@
"reference-test": {
"type": "reference",
"seed_id": "private"
},
"dynamic-test": {
"type": "dynamic",
"reference": {
"type": "reference",
"seed_id": "private"
}
},
"dynamic-remote-test": {
"type": "dynamic",
"reference": {
"type": "reference",
"packet": "https://raw.githubusercontent.com/jkomoros/prompt-garden/main/seeds/example-basic.json",
"seed_id": "private"
}
}
}
}
16 changes: 16 additions & 0 deletions test/base/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,22 @@ Suffix`;
assert.deepStrictEqual(actual, golden);
});

it('dynamic seed test', async () => {
const garden = loadTestGarden();
const seed = await garden.seed('dynamic-test');
const actual = await seed.grow();
const golden = true;
assert.deepStrictEqual(actual, golden);
});

it('dynamic remote seed fails', async () => {
const garden = loadTestGarden();
const seed = await garden.seed('dynamic-remote-test');
assert.rejects(async () => {
await seed.grow();
});
});

});

describe('expandSeedPacket tests', () => {
Expand Down

0 comments on commit 734b260

Please sign in to comment.