Skip to content

Commit

Permalink
Add a slice seed_type.
Browse files Browse the repository at this point in the history
Part of #46.
  • Loading branch information
jkomoros committed Jul 30, 2023
1 parent fa4edf1 commit 10cb766
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 3 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,15 @@ Required parameters
- `container` - The string, object, or array to search within.
- `search` - The thing to find within the container.
#### slice
Returns a new subset of the input.
Required parameters
- `input` - The string or array to create a slice of.
- `start` - (optional) The start index. If omitted, defaults to 0. May be negative to count from end of item.
- `end` - (optional) The first index to not include. May be negative to count from end of item. If omitted, defaults to `input.length`.
#### split
Splits a string at a delimiter to give an array of strings.
Expand Down
93 changes: 91 additions & 2 deletions seed-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -2454,6 +2454,95 @@
],
"additionalProperties": false
},
{
"type": "object",
"properties": {
"id": {
"$ref": "#/definitions/seedData/anyOf/0/properties/id"
},
"description": {
"$ref": "#/definitions/seedData/anyOf/0/properties/description"
},
"comment": {
"$ref": "#/definitions/seedData/anyOf/0/properties/comment"
},
"private": {
"$ref": "#/definitions/seedData/anyOf/0/properties/private"
},
"type": {
"type": "string",
"const": "slice"
},
"input": {
"anyOf": [
{
"$ref": "#/definitions/seedData"
},
{
"$ref": "#/definitions/seedData/anyOf/0/properties/prompt/anyOf/1"
},
{
"anyOf": [
{
"type": "string"
},
{
"$ref": "#/definitions/seedData/anyOf/5/properties/value/anyOf/2/anyOf/1/additionalProperties/anyOf/2/anyOf/2"
}
],
"description": "The thing to slice"
}
]
},
"start": {
"anyOf": [
{
"$ref": "#/definitions/seedData"
},
{
"$ref": "#/definitions/seedData/anyOf/0/properties/prompt/anyOf/1"
},
{
"anyOf": [
{
"not": {}
},
{
"type": "integer"
}
],
"description": "The index to start from"
}
]
},
"end": {
"anyOf": [
{
"$ref": "#/definitions/seedData"
},
{
"$ref": "#/definitions/seedData/anyOf/0/properties/prompt/anyOf/1"
},
{
"anyOf": [
{
"not": {}
},
{
"type": "integer"
}
],
"description": "The index to end before (exclusive)"
}
]
}
},
"required": [
"type",
"input"
],
"additionalProperties": false
},
{
"type": "object",
"properties": {
Expand Down Expand Up @@ -2917,7 +3006,7 @@
{
"anyOf": [
{
"$ref": "#/definitions/seedData/anyOf/36/properties/name/anyOf/2/anyOf/0"
"$ref": "#/definitions/seedData/anyOf/37/properties/name/anyOf/2/anyOf/0"
},
{
"$ref": "#/definitions/seedData/anyOf/2/properties/memory/anyOf/2/anyOf/1"
Expand Down Expand Up @@ -3186,7 +3275,7 @@
{
"type": "object",
"additionalProperties": {
"$ref": "#/definitions/seedData/anyOf/41/properties/defaults/anyOf/2/anyOf/1/additionalProperties"
"$ref": "#/definitions/seedData/anyOf/42/properties/defaults/anyOf/2/anyOf/1/additionalProperties"
},
"propertyNames": {
"pattern": "^arg:[a-zA-Z0-9-_.]*$"
Expand Down
21 changes: 20 additions & 1 deletion src/grow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ import {
SeedDataEnumerate,
enumerateResourceType,
SeedDataSpread,
SeedDataIndex
SeedDataIndex,
SeedDataSlice
} from './types.js';

import {
Expand Down Expand Up @@ -647,6 +648,21 @@ const growIndex = async (seed : Seed<SeedDataIndex>, env : Environment) : Promis
return result < 0 ? null : result;
};

const growSlice = async (seed : Seed<SeedDataSlice>, env : Environment) : Promise<ValueArray | string> => {
const data = seed.data;
let input = await getProperty(seed, env, data.input, null);
if (input === null) throw new Error('input is a required property');
if (input instanceof Embedding) input = input.text;
if (!Array.isArray(input) && typeof input != 'string') throw new Error('input must be a string or array');

const start = await getProperty(seed, env, data.start, 0);
if (typeof start != 'number') throw new Error('start must be a number');
const end = await getProperty(seed, env, data.end, Number.MAX_SAFE_INTEGER);
if (typeof end != 'number') throw new Error('end must be a number');

return input.slice(start, end);
};

const growSplit = async (seed : Seed<SeedDataSplit>, env : Environment) : Promise<ValueArray> => {
const data = seed.data;
const input = extractString(await getProperty(seed, env, data.input, null));
Expand Down Expand Up @@ -952,6 +968,9 @@ export const grow = async (seed : Seed, env : Environment) : Promise<Value> => {
case 'index':
result = await growIndex(seed as Seed<SeedDataIndex>, env);
break;
case 'slice':
result = await growSlice(seed as Seed<SeedDataSlice>, env);
break;
case 'split':
result = await growSplit(seed as Seed<SeedDataSplit>, env);
break;
Expand Down
16 changes: 16 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -855,6 +855,20 @@ const seedDataIndex = makeSeedData(seedDataConfigIndex);

export type SeedDataIndex = z.infer<typeof seedDataIndex>;

const seedDataConfigSlice = {
type: z.literal('slice'),
properties: {
input: z.union([z.string(), inputValueArray]).describe('The thing to slice'),
start: z.number().int().optional().describe('The index to start from'),
end: z.number().int().optional().describe('The index to end before (exclusive)')
}
};

const nestedSeedDataSlice = makeNestedSeedData(seedDataConfigSlice);
const seedDataSlice = makeSeedData(seedDataConfigSlice);

export type SeedDataSlice = z.infer<typeof seedDataSlice>;

const seedDataConfigSplit = {
type: z.literal('split'),
properties: {
Expand Down Expand Up @@ -1112,6 +1126,7 @@ export const expandedSeedData = z.discriminatedUnion('type', [
seedDataFilter,
seedDataSpread,
seedDataIndex,
seedDataSlice,
seedDataSplit,
seedDataJoin,
seedDataThrow,
Expand Down Expand Up @@ -1164,6 +1179,7 @@ export const seedData = z.discriminatedUnion('type', [
nestedSeedDataFilter,
nestedSeedDataSpread,
nestedSeedDataIndex,
nestedSeedDataSlice,
nestedSeedDataSplit,
nestedSeedDataJoin,
nestedSeedDataThrow,
Expand Down
14 changes: 14 additions & 0 deletions test/base/a_test.json
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,20 @@
2
],
"search": 1
},
"slice-string-test": {
"type": "slice",
"input": "012345"
},
"slice-array-test": {
"type": "slice",
"input": [
0,
1,
2,
3
],
"end": -1
}
}
}
16 changes: 16 additions & 0 deletions test/base/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1028,6 +1028,22 @@ Suffix`;
assert.deepStrictEqual(actual, golden);
});

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

it('slice array', async () => {
const garden = loadTestGarden();
const seed = await garden.seed('slice-array-test');
const actual = await seed.grow();
const golden = [0, 1, 2];
assert.deepStrictEqual(actual, golden);
});

});

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

0 comments on commit 10cb766

Please sign in to comment.