Skip to content

Commit

Permalink
refactor: use objects for better intellisense for GWT #5
Browse files Browse the repository at this point in the history
Closes: #5
  • Loading branch information
boan-anbo committed Nov 14, 2023
1 parent e16f6d5 commit 1fe1a6a
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 103 deletions.
27 changes: 18 additions & 9 deletions packages/cantos/src/stories/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,26 @@ export type CastProfiles = Record<string, StoryActor>;
* Bare Act is the base structure for any entity or behavior without methods or nested entities.
*/
export interface IStoryScript<Cast extends CastProfiles = typeof EmptyCast> {
/**
* The protagonists of the story.
*
* @remark
* It defaults to "it".
*/
who?: KeysOfConst<Cast>[];
/**
* Text of the story
*
* @remarks
* What "who" do(es).
*/
story: string;
scenes?: IStoryScripts<Cast>;
/**
* The order of the story among its sibling stories.
*/
order?: number;
cast?: Cast;
/**
* The short version of the story.
*/
story: string;
/**
* Story-by-Story options that can override the global story options.
*/
Expand All @@ -69,15 +79,14 @@ export interface IStoryScript<Cast extends CastProfiles = typeof EmptyCast> {

priority?: number;

who?: KeysOfConst<Cast>[];

context?: IStoryScript<Cast>[];
context?: IStoryScripts<Cast>;

when?: IStoryScript<Cast>[];
when?: IStoryScripts<Cast>;

then?: IStoryScript<Cast>[];
then?: IStoryScripts<Cast>;

so?: IStoryScript<Cast>[];
so?: IStoryScripts<Cast>;

tellAs?: (fn: (entity: Story<Cast>) => string) => string;
}
Expand Down
6 changes: 3 additions & 3 deletions packages/cantos/src/stories/stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ class StoryScript implements IStoryScript {
export class Story<CAST extends CastProfiles = typeof EmptyCast> extends StoryScript implements IStory<CAST> {
options?: StoryOptions;
scenes: Scenes<CAST> = {};
context?: StoryScript[];
when?: StoryScript[];
then?: StoryScript[];
context?: Scenes<CAST>
when?: Scenes<CAST>;
then?: Scenes<CAST>;
status?: StoryStatus | string;
priority?: number;

Expand Down
13 changes: 7 additions & 6 deletions packages/cantos/src/stories/storyteller.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {StoryScript} from "@src/index.ts";
import {Scenes} from "@src/index.ts";
import {ACT_DEFAULT_DESCRIPTIONS, GWT_DESCRIPTIONS, STORY_TELLER} from "@src/consts.ts";
import {Story} from "@src/stories/stories.ts";
import {StoryTag, StoryVersion} from "@src/stories/story-options.ts";
import {Test} from "@src/stories/interfaces.ts";
import {EmptyCast, Test} from "@src/stories/interfaces.ts";

enum STATEMENT_TYPE {
GIVEN,
Expand Down Expand Up @@ -72,12 +72,13 @@ export function tellStory(story: Story<any>, version: StoryVersion): string {

}

export function getActStatements(acts: StoryScript[] | undefined, prefixType: STATEMENT_TYPE, actName?: string): string | undefined {
export function getActStatements(acts: Scenes<typeof EmptyCast> | undefined, prefixType: STATEMENT_TYPE, actName?: string): string | undefined {

const actsLength = acts ? Object.keys(acts).length : 0;
if (!acts) {
return undefined;
}

const actsLength = acts.length;
if (actsLength === 0) {
return undefined;
}
Expand All @@ -97,8 +98,8 @@ export function getActStatements(acts: StoryScript[] | undefined, prefixType: ST
break;
}

const statement = acts.map((given, index) => {
let statement = given.story.trim();
const statement = Object.entries(acts).map(([_, story], index) => {
let statement = story.story.trim();
// there are more than one given
if (actsLength > 1) {
// if it's not the first given, add a comma
Expand Down
18 changes: 10 additions & 8 deletions packages/cantos/tests/fixtures/story-test-fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,23 @@ export const StoryTestFixture = loadScript({
},
describeFully: {
story: "can be described fully",
context: [
context: {

givenProvided:
{
story: "a given is provided"
}
],
when: [
{
},
when: {
askedToDescribeItselfFully: {
story: "asked to describe itself fully"
}
],
then: [
{
},
then: {
shouldDescribeItself: {
story: "should describe itself fully"
}
]
}
}
}
})
Expand Down
108 changes: 63 additions & 45 deletions packages/cantos/tests/fixtures/wizard-of-oz-story.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import {CastProfiles} from "@src/stories/interfaces.ts";
const wizardOfOzCastProfiles = {
Dorothy: {
role: "Dorothy Gale",
roleBio:"A young girl from Kansas who is carried away to the magical Land of Oz in a tornado and embarks on a quest with her new friends to see the Wizard who can help her return home.",
roleBio: "A young girl from Kansas who is carried away to the magical Land of Oz in a tornado and embarks on a quest with her new friends to see the Wizard who can help her return home.",
actor: "Judy Garland",

},
Scarecrow: {
role: "Scarecrow",
actor: "Ray Bolger",
},
TinMan : {
TinMan: {
role: "Tin Woodman",
actor: "Jack Haley",
},
Expand All @@ -32,46 +32,58 @@ const dorothyMeetsTheScarecrow = {
scenes: {
DOROTHY_LOST_HER_WAY: {
story: "Dorothy lost her way",
context: [
{
context: {
DorothyOnHerWayToTheCity: {
who: ["Dorothy"],
story: "tries to get to Emerald City",
story: "is on her way to the City of Emeralds",
}
],
when: [
{
who: ["Dorothy"],
story: "arrives at a crossroad",
}
],
then: [
{
who: ["Dorothy"],
story: "is confused",
}
],
}
,
when: {

arrivesAtACrossRoad:
{
who: ["Dorothy"],
story: "arrives at a crossroad",
}
},
then: {

wasConfused:
{
who: ["Dorothy"],
story: "is confused",
}
}
,
order: 1,
},
SCARECROW_GIVES_DIRECTION: {
story: "The Scarecrow told Dorothy how to get to Emerald City",
context: [
{
who: ["Scarecrow"],
story: "standing at the crossroad, and saw Dorothy",
}
],
when: [
{
who: ["Scarecrow"],
story: "told Dorothy which way to go",
}
],
then: [
{
who: ["Dorothy"],
story: "was happy",
}
],
context: {
ScarecrowAtTHeCrossRoad:
{
who: ["Scarecrow"],
story: "standing at the crossroad, and saw Dorothy",
}
}
,
when: {
scareCrowToldDorothy:
{
who: ["Scarecrow"],
story: "told Dorothy which way to go",
}
},
then: {

dorothyWasHappy:
{
who: ["Dorothy"],
story: "was happy",
}
}
,
order: 2,
},
}
Expand All @@ -83,24 +95,27 @@ const dorothyMeetsTheLion = {
scenes: {
THREE_IN_THE_FOREST: {
story: "The three were walking in the forest",
context: [
{
context: {
DorothyAndOthersInForest: {
who: ["Dorothy", "Scarecrow", "TinMan"],
story: "are walking in the forest",
}
],
when: [
{
}
,
when: {
theyHearsARoar: {
who: ["Dorothy", "Scarecrow", "TinMan"],
story: "hear a roar",
}
],
then: [
{
}
,
then: {
theyAreAfraid: {
who: ["Dorothy", "Scarecrow", "TinMan"],
story: "are afraid",
}
],
}
,
order: 1,
},
}
Expand Down Expand Up @@ -130,3 +145,6 @@ const dorothyMeetsHerCompanions = {


export const wizardOfOzStory = loadScript(dorothyMeetsHerCompanions)



73 changes: 41 additions & 32 deletions packages/cantos/tests/tutorials/cantos-basics-mychat.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,41 +122,50 @@ const myChatStory = {
// Then, we will use the `context`, `when`, and `who` fields to fully describe the story in more details.
//
// The context of the story:
context: [
{
who: ["User"],
story: "has been chatting with the AI for a while", // <-- This is the context of this scenario.
},
{
who: ["InternetConnection"], // <-- This is another context of this scenario. This is too trivial to be really useful, but it's here to show that you can have multiple contexts.
story: "is working",
}
],
context: {

userHasChatted:
{
who: ["User"],
story: "has been chatting with the AI for a while", // <-- This is the context of this scenario.
},
hasInternet:
{
who: ["InternetConnection"], // <-- This is another context of this scenario. This is too trivial to be really useful, but it's here to show that you can have multiple contexts.
story: "is working",
}
},
// When something happens:
when: [
{
who: ["User"],
story: "sent a new message", // <-- notice how you can ignore the "I" in the sentence because we provided the "who".
},
],
when: {
userSendsAMessage:
{
who: ["User"],
story: "sent a new message", // <-- notice how you can ignore the "I" in the sentence because we provided the "who".
},
},
// Then something else should/can/will happen:
then: [
{
who: ["App", "AI"],
story: "include the previous messages in the new message before sending the bundle to AI", // <-- notice that a step can involve two roles, the App and the AI.
},
],
then: {
appWrapsMessage:
{
who: ["App", "AI"],
story: "include the previous messages in the new message before sending the bundle to AI", // <-- notice that a step can involve two roles, the App and the AI.
},
},
// So that...(the implications, consequences, or benefits etc.)
so: [
{
who: ["AI"],
story: "can understand the context of the conversation",
},
{
who: ["User"],
story: "can feel like the AI is smart",
},
],
so: {
aiCanUnderstandTheContext:
{
who: ["AI"],
story: "can understand the context of the conversation",
},
userCanChatInContext:
{
who: ["User"],
story:
"can feel like the AI is smart",
}
,
},

// So on and so forth for the rest of the other 3 scenes.
//
Expand Down

0 comments on commit 1fe1a6a

Please sign in to comment.