@statelyai/agent 0.0.6 → 0.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +6 -0
- package/dist/index.d.ts +2 -2
- package/examples/wordGuesser.ts +156 -0
- package/package.json +1 -1
- package/src/schemas.ts +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
# @statelyai/agent
|
|
2
2
|
|
|
3
|
+
## 0.0.7
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#18](https://github.com/statelyai/agent/pull/18) [`dcaabab`](https://github.com/statelyai/agent/commit/dcaababe69255b7eaff3347d0cf09469d3e6cc78) Thanks [@davidkpiano](https://github.com/davidkpiano)! - `context` is now optional for `createSchemas(…)`
|
|
8
|
+
|
|
3
9
|
## 0.0.6
|
|
4
10
|
|
|
5
11
|
### Patch Changes
|
package/dist/index.d.ts
CHANGED
|
@@ -36,13 +36,13 @@ declare function createSchemas<const TContextSchema extends ContextSchema, const
|
|
|
36
36
|
*
|
|
37
37
|
* Must be of `{ type: 'object' }`.
|
|
38
38
|
*/
|
|
39
|
-
context
|
|
39
|
+
context?: TContextSchema;
|
|
40
40
|
/**
|
|
41
41
|
* An object mapping event types to each event object's JSON Schema.
|
|
42
42
|
*/
|
|
43
43
|
events: TEventSchemas;
|
|
44
44
|
}): {
|
|
45
|
-
context: TContextSchema;
|
|
45
|
+
context: TContextSchema | undefined;
|
|
46
46
|
events: ConvertToJSONSchemas<TEventSchemas>;
|
|
47
47
|
types: {
|
|
48
48
|
context: FromSchema<TContextSchema>;
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import { assign, log, setup } from 'xstate';
|
|
2
|
+
import { getFromTerminal } from './helpers/helpers';
|
|
3
|
+
import { createAgent, createOpenAIAdapter, createSchemas } from '../src';
|
|
4
|
+
import OpenAI from 'openai';
|
|
5
|
+
|
|
6
|
+
const openAI = new OpenAI({
|
|
7
|
+
apiKey: process.env.OPENAI_API_KEY,
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
const adapter = createOpenAIAdapter(openAI, {
|
|
11
|
+
model: 'gpt-4-1106-preview',
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
const schemas = createSchemas({
|
|
15
|
+
events: {
|
|
16
|
+
guessLetter: {
|
|
17
|
+
description: 'Player guesses a letter',
|
|
18
|
+
properties: {
|
|
19
|
+
letter: {
|
|
20
|
+
type: 'string',
|
|
21
|
+
description: 'The letter guessed',
|
|
22
|
+
maxLength: 1,
|
|
23
|
+
minLength: 1,
|
|
24
|
+
},
|
|
25
|
+
},
|
|
26
|
+
},
|
|
27
|
+
guessWord: {
|
|
28
|
+
description: 'Player guesses the full word',
|
|
29
|
+
properties: {
|
|
30
|
+
word: {
|
|
31
|
+
type: 'string',
|
|
32
|
+
description: 'The word guessed',
|
|
33
|
+
},
|
|
34
|
+
},
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
const context = {
|
|
40
|
+
word: null as string | null,
|
|
41
|
+
guessedWord: null as string | null,
|
|
42
|
+
letters: [] as string[],
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
const wordGuesserMachine = setup({
|
|
46
|
+
types: {
|
|
47
|
+
context: {} as typeof context,
|
|
48
|
+
events: schemas.types.events,
|
|
49
|
+
},
|
|
50
|
+
actors: {
|
|
51
|
+
getFromTerminal,
|
|
52
|
+
guesser: adapter.fromEvent(
|
|
53
|
+
(input: typeof context) => `
|
|
54
|
+
You are trying to guess the word. The word has ${
|
|
55
|
+
input.word!.length
|
|
56
|
+
} letters. You have guessed the following letters so far: ${input.letters.join(
|
|
57
|
+
', '
|
|
58
|
+
)}. These letters matched: ${input
|
|
59
|
+
.word!.split('')
|
|
60
|
+
.map((letter) =>
|
|
61
|
+
input.letters.includes(letter.toUpperCase())
|
|
62
|
+
? letter.toUpperCase()
|
|
63
|
+
: '_'
|
|
64
|
+
)
|
|
65
|
+
.join('')}
|
|
66
|
+
Please make your next guess - type a letter or the full word. You can only make 10 total guesses.
|
|
67
|
+
`
|
|
68
|
+
),
|
|
69
|
+
},
|
|
70
|
+
schemas,
|
|
71
|
+
}).createMachine({
|
|
72
|
+
initial: 'providingWord',
|
|
73
|
+
context,
|
|
74
|
+
states: {
|
|
75
|
+
providingWord: {
|
|
76
|
+
invoke: {
|
|
77
|
+
src: 'getFromTerminal',
|
|
78
|
+
input: 'Enter a word',
|
|
79
|
+
onDone: {
|
|
80
|
+
actions: assign({
|
|
81
|
+
word: ({ event }) => event.output,
|
|
82
|
+
}),
|
|
83
|
+
target: 'guessing',
|
|
84
|
+
},
|
|
85
|
+
},
|
|
86
|
+
},
|
|
87
|
+
guessing: {
|
|
88
|
+
always: {
|
|
89
|
+
guard: ({ context }) => context.letters.length > 10,
|
|
90
|
+
target: 'finalGuess',
|
|
91
|
+
},
|
|
92
|
+
invoke: {
|
|
93
|
+
src: 'guesser',
|
|
94
|
+
input: ({ context }) => context,
|
|
95
|
+
},
|
|
96
|
+
on: {
|
|
97
|
+
guessLetter: {
|
|
98
|
+
actions: assign({
|
|
99
|
+
letters: ({ context, event }) => {
|
|
100
|
+
return [...context.letters, event.letter.toUpperCase()];
|
|
101
|
+
},
|
|
102
|
+
}),
|
|
103
|
+
target: 'guessing',
|
|
104
|
+
reenter: true,
|
|
105
|
+
},
|
|
106
|
+
guessWord: {
|
|
107
|
+
actions: assign({
|
|
108
|
+
guessedWord: ({ event }) => event.word,
|
|
109
|
+
}),
|
|
110
|
+
target: 'gameOver',
|
|
111
|
+
},
|
|
112
|
+
},
|
|
113
|
+
},
|
|
114
|
+
finalGuess: {
|
|
115
|
+
invoke: {
|
|
116
|
+
src: 'guesser',
|
|
117
|
+
input: ({ context }) => context,
|
|
118
|
+
},
|
|
119
|
+
on: {
|
|
120
|
+
guessWord: {
|
|
121
|
+
actions: assign({
|
|
122
|
+
guessedWord: ({ event }) => event.word,
|
|
123
|
+
}),
|
|
124
|
+
target: 'gameOver',
|
|
125
|
+
},
|
|
126
|
+
},
|
|
127
|
+
},
|
|
128
|
+
gameOver: {
|
|
129
|
+
entry: log(({ context }) => {
|
|
130
|
+
if (
|
|
131
|
+
context.guessedWord?.toUpperCase() === context.word?.toUpperCase()
|
|
132
|
+
) {
|
|
133
|
+
return 'You won!';
|
|
134
|
+
} else {
|
|
135
|
+
return 'You lost! The word was ' + context.word;
|
|
136
|
+
}
|
|
137
|
+
}),
|
|
138
|
+
},
|
|
139
|
+
},
|
|
140
|
+
exit: () => process.exit(),
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
const actor = createAgent(wordGuesserMachine, {
|
|
144
|
+
inspect: (ev) => {
|
|
145
|
+
if (ev.type === '@xstate.event') {
|
|
146
|
+
console.log(ev.event);
|
|
147
|
+
}
|
|
148
|
+
},
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
actor.subscribe((s) => {
|
|
152
|
+
console.log(s.value);
|
|
153
|
+
console.log(s.context);
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
actor.start();
|
package/package.json
CHANGED
package/src/schemas.ts
CHANGED
|
@@ -19,13 +19,13 @@ export function createSchemas<
|
|
|
19
19
|
*
|
|
20
20
|
* Must be of `{ type: 'object' }`.
|
|
21
21
|
*/
|
|
22
|
-
context
|
|
22
|
+
context?: TContextSchema;
|
|
23
23
|
/**
|
|
24
24
|
* An object mapping event types to each event object's JSON Schema.
|
|
25
25
|
*/
|
|
26
26
|
events: TEventSchemas;
|
|
27
27
|
}): {
|
|
28
|
-
context: TContextSchema;
|
|
28
|
+
context: TContextSchema | undefined;
|
|
29
29
|
events: ConvertToJSONSchemas<TEventSchemas>;
|
|
30
30
|
types: {
|
|
31
31
|
context: FromSchema<TContextSchema>;
|