Skip to content

Commit

Permalink
SeedReference.id --> seed
Browse files Browse the repository at this point in the history
This makes seed references more obviously seed references.

This was noted as an improvement in #36. It will also make #42 easier to do, because then the only
disallowed key names will be "type" and "seed".
  • Loading branch information
jkomoros committed Jul 7, 2023
1 parent 97efc94 commit 56578ac
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 73 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ But seeds can also reference other seeds:
"": {
"type": "log",
"value": {
"id": "sub-seed"
"seed": "sub-seed"
}
},
"sub-seed": {
Expand All @@ -150,7 +150,7 @@ But seeds can also reference other seeds:
}
```
An object shaped like `{"id": "seed"}` is a Seed Reference.
An object shaped like `{"seed": "${id}"}` is a Seed Reference.
When the seed "" is grown, it will first see if any of its properties are a seed reference. If so, it will first grow that sub-seed, and then pass its return value in to the property of the calling seed.
Expand All @@ -166,7 +166,7 @@ You can also fetch seeds from an adjacent file:
"type": "log",
"value": {
"packet": "./other.json",
"id": "sub-seed"
"seed": "sub-seed"
}
}
}
Expand All @@ -185,7 +185,7 @@ You can also fetch a remote packet:
"type": "log",
"value": {
"packet": "https://komoroske.com/seeds/other.json",
"id": "sub-seed"
"seed": "sub-seed"
}
}
}
Expand Down Expand Up @@ -224,7 +224,7 @@ Will unroll to this:
"foo" : {
"type": "log",
"value": {
"id": "foo-value"
"seed": "foo-value"
}
},
"foo-value": {
Expand All @@ -248,7 +248,7 @@ nested seedData:
"type": "log",
"value": {
"type": "log",
"id": "bar",
"seed": "bar",
"value": true
}
}
Expand All @@ -265,12 +265,12 @@ Yields
"foo" : {
"type": "log",
"value": {
"id": "bar"
"seed": "bar"
}
},
"bar": {
"type": "log",
"id": "bar",
"seed": "bar",
"value": true
}
}
Expand Down
4 changes: 2 additions & 2 deletions seed-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,12 @@
}
]
},
"id": {
"seed": {
"$ref": "#/definitions/seedData/anyOf/0/properties/id"
}
},
"required": [
"id"
"seed"
],
"additionalProperties": false
},
Expand Down
4 changes: 2 additions & 2 deletions seeds/example-basic.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"type": "prompt",
"description": "This seed demonstrates a seed that references sub seeds",
"prompt": {
"id": "hello-world"
"seed": "hello-world"
}
},
"input-prompt": {
Expand Down Expand Up @@ -52,7 +52,7 @@
"type": "log",
"value": {
"packet": "https://raw.githubusercontent.com/jkomoros/prompt-garden/main/seeds/example.json",
"id": "hello-world"
"seed": "hello-world"
}
},
"nested-example": {
Expand Down
8 changes: 4 additions & 4 deletions seeds/example-complex.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"b": 0
},
"then": {
"id": "write-favorite-things-limerick"
"seed": "write-favorite-things-limerick"
},
"else": {
"type": "array",
Expand All @@ -40,10 +40,10 @@
"value": "You haven't stored memories yet, so let's store a few."
},
{
"id": "remember-favorite-things"
"seed": "remember-favorite-things"
},
{
"id": "write-favorite-things-limerick"
"seed": "write-favorite-things-limerick"
}
]
}
Expand Down Expand Up @@ -105,7 +105,7 @@
}
},
{
"id": "remember-favorite-things"
"seed": "remember-favorite-things"
}
]
}
Expand Down
10 changes: 5 additions & 5 deletions src/garden.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export class Garden {
await this.ensureSeedPacket(absoluteRef.packet);
const collection = this._seeds[absoluteRef.packet];
if (!collection) throw new Error('Unexpectedly no packet');
const seed = collection[ref.id];
const seed = collection[ref.seed];
if (!seed) throw new Error(`No seed with ID ${packSeedReference(ref)}`);
return seed;
}
Expand Down Expand Up @@ -158,9 +158,9 @@ export class Garden {
if (this._seeds[ref.packet] == undefined) {
this._seeds[ref.packet] = {};
}
this._seeds[ref.packet][ref.id] = new Seed(this, ref, data, environmentOverlay);
if (!this._seedsByID[ref.id]) this._seedsByID[ref.id] = [];
this._seedsByID[ref.id].push(ref);
this._seeds[ref.packet][ref.seed] = new Seed(this, ref, data, environmentOverlay);
if (!this._seedsByID[ref.seed]) this._seedsByID[ref.seed] = [];
this._seedsByID[ref.seed].push(ref);
}

plantSeedPacket(location: SeedPacketAbsoluteLocation, packet: SeedPacket) {
Expand All @@ -173,7 +173,7 @@ export class Garden {
for (const [id, seed] of Object.entries(expandedPacket.seeds)) {
const ref : AbsoluteSeedReference = {
packet: location,
id
seed: id
};
this.plantSeed(ref, seed, expandedPacket.environment);
}
Expand Down
12 changes: 6 additions & 6 deletions src/reference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const isLocalLocation = (location : SeedPacketAbsoluteLocation) : boolean
export const packSeedReference = (ref : SeedReference) : PackedSeedReference => {
let result = ref.packet || '';
if (result) result += PACKED_SEED_REFERENCE_DELIMITER;
result += ref.id;
result += ref.seed;
return result;
};

Expand All @@ -31,12 +31,12 @@ export const unpackSeedReference = (ref : PackedSeedReference, base : SeedPacket
if (parts.length == 1) {
return {
packet: base,
id: parts[0]
seed: parts[0]
};
}
return {
packet: parts[0],
id: parts[1]
seed: parts[1]
};
};

Expand All @@ -49,20 +49,20 @@ export const makeAbsolute = (ref : SeedReference, base : SeedPacketAbsoluteLocat
if (!isRelativeSeedPacketLocation(location)) {
return {
packet: location || base,
id: ref.id
seed: ref.seed
};
}
if (isLocalLocation(base)) {
const url = new URL(location, 'file://localhost/' + base);
return {
//TODO: this slices off the '/' assuming the base is a relative path from the current working directory.
packet: url.pathname.slice(1),
id: ref.id
seed: ref.seed
};
}
const url = new URL(location, base);
return {
packet: url.toString(),
id: ref.id
seed: ref.seed
};
};
8 changes: 4 additions & 4 deletions src/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ const expandSeedData = (idFromParent : SeedID, data : SeedData, result : Expande
const actualSubID = expandSeedData(subID, subSeedData, result);

const subReference : SeedReference = {
id: actualSubID
seed: actualSubID
};

//Do a type cast to allow us to set the key, which we know is a legal key/value combination.
Expand Down Expand Up @@ -129,7 +129,7 @@ export const verifySeedPacket = (packet : ExpandedSeedPacket) : void => {
for (const ref of refs) {
//We don't check external references
if (ref.packet) continue;
if (!packet.seeds[ref.id]) throw new Error(`Seed ${id} referenced a non-existent local seed: ${ref.id}`);
if (!packet.seeds[ref.seed]) throw new Error(`Seed ${id} referenced a non-existent local seed: ${ref.seed}`);
}
}
};
Expand All @@ -146,11 +146,11 @@ export class Seed<D extends ExpandedSeedData = ExpandedSeedData> {
this._ref = ref;
this._data = data;
this._environmentOverlay = environmentOverlay || {};
if (data.id !== undefined && data.id != ref.id) throw new Error('ID provided in seed data did not match ID');
if (data.id !== undefined && data.id != ref.seed) throw new Error('ID provided in seed data did not match ID');
}

get id() : SeedID {
return this._ref.id;
return this._ref.seed;
}

get ref() : AbsoluteSeedReference {
Expand Down
4 changes: 2 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,14 +214,14 @@ export type SeedPacketLocation = z.infer<typeof seedPacketLocation>;

export const seedReference = z.object({
packet: z.optional(seedPacketLocation),
id: seedID
seed: seedID
});

export type SeedReference = z.infer<typeof seedReference>;

export const requiredSeedReference = z.object({
packet: seedPacketAbsoluteLocation,
id: seedID
seed: seedID
});

export type AbsoluteSeedReference = z.infer<typeof requiredSeedReference>;
Expand Down
14 changes: 7 additions & 7 deletions test/base/a_test.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"type": "prompt",
"description": "Tests that a reference with a # works",
"prompt": {
"id": "hello-world"
"seed": "hello-world"
}
},
"true": {
Expand All @@ -27,25 +27,25 @@
"if-true": {
"type": "if",
"test": {
"id": "true"
"seed": "true"
},
"then": {
"id": "true"
"seed": "true"
},
"else": {
"id": "false"
"seed": "false"
}
},
"if-false": {
"type": "if",
"test": {
"id": "false"
"seed": "false"
},
"then": {
"id": "true"
"seed": "true"
},
"else": {
"id": "false"
"seed": "false"
}
},
"render-test": {
Expand Down
2 changes: 1 addition & 1 deletion test/base/b_test.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"type": "log",
"value": {
"packet": "./a_test.json",
"id": "true"
"seed": "true"
}
}
}
Expand Down
Loading

0 comments on commit 56578ac

Please sign in to comment.