From 56578ac20523f3fb5c60e1c0a57b90ee17f2eba8 Mon Sep 17 00:00:00 2001 From: Alex Komoroske Date: Thu, 6 Jul 2023 19:45:50 -0700 Subject: [PATCH] SeedReference.id --> seed 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". --- README.md | 16 +++++----- seed-schema.json | 4 +-- seeds/example-basic.json | 4 +-- seeds/example-complex.json | 8 ++--- src/garden.ts | 10 +++--- src/reference.ts | 12 +++---- src/seed.ts | 8 ++--- src/types.ts | 4 +-- test/base/a_test.json | 14 ++++----- test/base/b_test.json | 2 +- test/base/test.ts | 64 +++++++++++++++++++------------------- 11 files changed, 73 insertions(+), 73 deletions(-) diff --git a/README.md b/README.md index c5bd4f9..a7ee5d1 100644 --- a/README.md +++ b/README.md @@ -135,7 +135,7 @@ But seeds can also reference other seeds: "": { "type": "log", "value": { - "id": "sub-seed" + "seed": "sub-seed" } }, "sub-seed": { @@ -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. @@ -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" } } } @@ -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" } } } @@ -224,7 +224,7 @@ Will unroll to this: "foo" : { "type": "log", "value": { - "id": "foo-value" + "seed": "foo-value" } }, "foo-value": { @@ -248,7 +248,7 @@ nested seedData: "type": "log", "value": { "type": "log", - "id": "bar", + "seed": "bar", "value": true } } @@ -265,12 +265,12 @@ Yields "foo" : { "type": "log", "value": { - "id": "bar" + "seed": "bar" } }, "bar": { "type": "log", - "id": "bar", + "seed": "bar", "value": true } } diff --git a/seed-schema.json b/seed-schema.json index b531b8a..3147e2d 100644 --- a/seed-schema.json +++ b/seed-schema.json @@ -125,12 +125,12 @@ } ] }, - "id": { + "seed": { "$ref": "#/definitions/seedData/anyOf/0/properties/id" } }, "required": [ - "id" + "seed" ], "additionalProperties": false }, diff --git a/seeds/example-basic.json b/seeds/example-basic.json index ad90a8d..ce7195f 100644 --- a/seeds/example-basic.json +++ b/seeds/example-basic.json @@ -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": { @@ -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": { diff --git a/seeds/example-complex.json b/seeds/example-complex.json index 73864a4..939de18 100644 --- a/seeds/example-complex.json +++ b/seeds/example-complex.json @@ -29,7 +29,7 @@ "b": 0 }, "then": { - "id": "write-favorite-things-limerick" + "seed": "write-favorite-things-limerick" }, "else": { "type": "array", @@ -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" } ] } @@ -105,7 +105,7 @@ } }, { - "id": "remember-favorite-things" + "seed": "remember-favorite-things" } ] } diff --git a/src/garden.ts b/src/garden.ts index 4e27f9b..c3ca1a0 100644 --- a/src/garden.ts +++ b/src/garden.ts @@ -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; } @@ -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) { @@ -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); } diff --git a/src/reference.ts b/src/reference.ts index 6a7e125..d1239ed 100644 --- a/src/reference.ts +++ b/src/reference.ts @@ -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; }; @@ -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] }; }; @@ -49,7 +49,7 @@ export const makeAbsolute = (ref : SeedReference, base : SeedPacketAbsoluteLocat if (!isRelativeSeedPacketLocation(location)) { return { packet: location || base, - id: ref.id + seed: ref.seed }; } if (isLocalLocation(base)) { @@ -57,12 +57,12 @@ export const makeAbsolute = (ref : SeedReference, base : SeedPacketAbsoluteLocat 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 }; }; \ No newline at end of file diff --git a/src/seed.ts b/src/seed.ts index 65757e5..5d09a10 100644 --- a/src/seed.ts +++ b/src/seed.ts @@ -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. @@ -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}`); } } }; @@ -146,11 +146,11 @@ export class Seed { 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 { diff --git a/src/types.ts b/src/types.ts index 60e1581..8cae23a 100644 --- a/src/types.ts +++ b/src/types.ts @@ -214,14 +214,14 @@ export type SeedPacketLocation = z.infer; export const seedReference = z.object({ packet: z.optional(seedPacketLocation), - id: seedID + seed: seedID }); export type SeedReference = z.infer; export const requiredSeedReference = z.object({ packet: seedPacketAbsoluteLocation, - id: seedID + seed: seedID }); export type AbsoluteSeedReference = z.infer; diff --git a/test/base/a_test.json b/test/base/a_test.json index 4bc1cf3..2cee795 100644 --- a/test/base/a_test.json +++ b/test/base/a_test.json @@ -13,7 +13,7 @@ "type": "prompt", "description": "Tests that a reference with a # works", "prompt": { - "id": "hello-world" + "seed": "hello-world" } }, "true": { @@ -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": { diff --git a/test/base/b_test.json b/test/base/b_test.json index 266abef..b80528d 100644 --- a/test/base/b_test.json +++ b/test/base/b_test.json @@ -9,7 +9,7 @@ "type": "log", "value": { "packet": "./a_test.json", - "id": "true" + "seed": "true" } } } diff --git a/test/base/test.ts b/test/base/test.ts index e0465a6..145d180 100644 --- a/test/base/test.ts +++ b/test/base/test.ts @@ -157,7 +157,7 @@ describe('Garden smoke test', () => { it('handles a seed from another file', async() => { const garden = loadTestGarden(); - const seed = await garden.seed({packet: 'test/base/b_test.json', id: ''}); + const seed = await garden.seed({packet: 'test/base/b_test.json', seed: ''}); const result = await seed.grow(); const golden = 'test-other hello world'; assert.deepStrictEqual(result, golden); @@ -174,7 +174,7 @@ describe('Garden smoke test', () => { it('handles growing a seed that references a seed in another file', async () => { //Garden will have both files loaded up, so it won't need to be fetched. const garden = loadTestGarden(); - const seed = await garden.seed({packet: 'test/base/b_test.json', id: 'remote-ref'}); + const seed = await garden.seed({packet: 'test/base/b_test.json', seed: 'remote-ref'}); const result = await seed.grow(); const golden = true; assert.deepStrictEqual(result, golden); @@ -183,7 +183,7 @@ describe('Garden smoke test', () => { it('handles growing a seed that references a seed in another file', async () => { //Garden will have both files loaded up, so it won't need to be fetched. const garden = loadTestGarden(); - const seed = await garden.seed({packet: 'test/base/b_test.json', id: 'remote-ref'}); + const seed = await garden.seed({packet: 'test/base/b_test.json', seed: 'remote-ref'}); const result = await seed.grow(); const golden = true; assert.deepStrictEqual(result, golden); @@ -192,7 +192,7 @@ describe('Garden smoke test', () => { it('handles growing a seed that references a seed in another file that isn\'t loaded yet', async () => { //Force garden to have only the first file loaded. const garden = loadTestGarden(['test/base/b_test.json']); - const seed = await garden.seed({packet: 'test/base/b_test.json', id: 'remote-ref'}); + const seed = await garden.seed({packet: 'test/base/b_test.json', seed: 'remote-ref'}); const result = await seed.grow(); const golden = true; assert.deepStrictEqual(result, golden); @@ -201,7 +201,7 @@ describe('Garden smoke test', () => { it('handles growing a seed that references a seed in another file that isn\'t loaded yet with no files loaded yet', async () => { //Force garden to have no files loaded to start const garden = loadTestGarden([]); - const seed = await garden.seed({packet: 'test/base/b_test.json', id: 'remote-ref'}); + const seed = await garden.seed({packet: 'test/base/b_test.json', seed: 'remote-ref'}); const result = await seed.grow(); const golden = true; assert.deepStrictEqual(result, golden); @@ -211,7 +211,7 @@ describe('Garden smoke test', () => { //Create an empty garden with no fetch const garden = loadTestGarden([], true); try { - await garden.seed({packet: 'test/base/b_test.json', id: 'remote-ref'}); + await garden.seed({packet: 'test/base/b_test.json', seed: 'remote-ref'}); } catch(err) { //Err expected return; @@ -222,7 +222,7 @@ describe('Garden smoke test', () => { it ('a seed with an explict id that matches is legal', async () => { const garden = loadTestGarden(); assert.doesNotThrow(() => { - garden.plantSeed({packet: '', id: 'blammo'}, { + garden.plantSeed({packet: '', seed: 'blammo'}, { 'type': 'log', 'value': true, 'id': 'blammo' @@ -233,7 +233,7 @@ describe('Garden smoke test', () => { it ('a seed with an explicit id that doesnt match is illegal', async () => { const garden = loadTestGarden(); assert.throws(() => { - garden.plantSeed({packet: '', id: 'slammo'}, { + garden.plantSeed({packet: '', seed: 'slammo'}, { 'type': 'log', 'value': true, 'id': 'blammo' @@ -538,7 +538,7 @@ describe('Garden smoke test', () => { 'Three' ] }; - garden.plantSeed({id: 'compose-seed', packet: garden.location || ''}, seedData); + garden.plantSeed({seed: 'compose-seed', packet: garden.location || ''}, seedData); const seed = await garden.seed('compose-seed'); const result = await seed.grow(); const golden = `Prefix @@ -562,7 +562,7 @@ Suffix`; ], max_tokens: 10 }; - garden.plantSeed({id: 'compose-seed', packet: garden.location || ''}, seedData); + garden.plantSeed({seed: 'compose-seed', packet: garden.location || ''}, seedData); const seed = await garden.seed('compose-seed'); const result = await seed.grow(); const golden = `Prefix @@ -586,7 +586,7 @@ Suffix`; //4096 is the maxTokens for default model, so this should be effecively same as test above max_tokens: 10 - 4096 }; - garden.plantSeed({id: 'compose-seed', packet: garden.location || ''}, seedData); + garden.plantSeed({seed: 'compose-seed', packet: garden.location || ''}, seedData); const seed = await garden.seed('compose-seed'); const result = await seed.grow(); const golden = `Prefix @@ -604,7 +604,7 @@ Suffix`; 'foo': { 'type': 'var', 'name': { - 'id': 'bar-typo' + 'seed': 'bar-typo' } }, 'bar': { @@ -656,7 +656,7 @@ Suffix`; name: 'komoroske.com:test', value: 5, block: { - id: 'env-test' + seed: 'env-test' } } } @@ -695,7 +695,7 @@ Suffix`; name: 'komoroske.com:other', value: 5, block: { - id: 'env-test' + seed: 'env-test' } } } @@ -735,7 +735,7 @@ describe('expandSeedPacket tests', () => { '': { 'type': 'log', 'value': { - 'id': 'other' + 'seed': 'other' } }, 'other': { @@ -752,7 +752,7 @@ describe('expandSeedPacket tests', () => { '': { 'type': 'log', 'value': { - 'id': 'other' + 'seed': 'other' } }, 'other': { @@ -785,7 +785,7 @@ describe('expandSeedPacket tests', () => { '': { 'type': 'log', 'value': { - 'id': '-value' + 'seed': '-value' } }, '-value': { @@ -820,7 +820,7 @@ describe('expandSeedPacket tests', () => { '': { 'type': 'log', 'value': { - 'id': 'foo' + 'seed': 'foo' } }, 'foo': { @@ -859,7 +859,7 @@ describe('expandSeedPacket tests', () => { 'type': 'object', 'properties': { 'a': { - 'id': '-a' + 'seed': '-a' }, 'b': true, } @@ -899,7 +899,7 @@ describe('expandSeedPacket tests', () => { 'type': 'array', 'items': [ { - 'id': '-0' + 'seed': '-0' }, true, ] @@ -976,7 +976,7 @@ describe('reference regexp tests', () => { const result = unpackSeedReference(input); const golden : AbsoluteSeedReference = { packet: 'https://foo.com/blammo', - id: 'foo' + seed: 'foo' }; assert.deepStrictEqual(result, golden); }); @@ -986,7 +986,7 @@ describe('reference regexp tests', () => { const result = unpackSeedReference(input); const golden : AbsoluteSeedReference = { packet: '', - id: 'foo' + seed: 'foo' }; assert.deepStrictEqual(result, golden); }); @@ -996,7 +996,7 @@ describe('reference regexp tests', () => { const result = unpackSeedReference(input); const golden : AbsoluteSeedReference = { packet: 'https://foo.com/blammo', - id: '' + seed: '' }; assert.deepStrictEqual(result, golden); }); @@ -1008,7 +1008,7 @@ describe('makeAbsolute', () => { const base = 'd/e.json'; const input : AbsoluteSeedReference = { packet: 'a/b/c.json', - id: 'foo' + seed: 'foo' }; const result = makeAbsolute(input, base); const golden : AbsoluteSeedReference = input; @@ -1019,12 +1019,12 @@ describe('makeAbsolute', () => { const base = 'a/b/c.json'; const input : SeedReference = { packet: '../c/e.json', - id: 'foo' + seed: 'foo' }; const result = makeAbsolute(input, base); const golden : AbsoluteSeedReference = { packet: 'a/c/e.json', - id: 'foo' + seed: 'foo' }; assert.deepStrictEqual(result, golden); }); @@ -1033,12 +1033,12 @@ describe('makeAbsolute', () => { const base = 'https://localhost/a/b/c.json'; const input : SeedReference = { packet: '../c/e.json', - id: 'foo' + seed: 'foo' }; const result = makeAbsolute(input, base); const golden : AbsoluteSeedReference = { packet: 'https://localhost/a/c/e.json', - id: 'foo' + seed: 'foo' }; assert.deepStrictEqual(result, golden); }); @@ -1047,12 +1047,12 @@ describe('makeAbsolute', () => { const base = 'a/b/c.json'; const input : SeedReference = { packet: './f/e.json', - id: 'foo' + seed: 'foo' }; const result = makeAbsolute(input, base); const golden : AbsoluteSeedReference = { packet: 'a/b/f/e.json', - id: 'foo' + seed: 'foo' }; assert.deepStrictEqual(result, golden); }); @@ -1061,12 +1061,12 @@ describe('makeAbsolute', () => { const base = 'https://localhost/a/b/c.json'; const input : SeedReference = { packet: './f/e.json', - id: 'foo' + seed: 'foo' }; const result = makeAbsolute(input, base); const golden : AbsoluteSeedReference = { packet: 'https://localhost/a/b/f/e.json', - id: 'foo' + seed: 'foo' }; assert.deepStrictEqual(result, golden); });