@statelyai/agent 2.0.0-next.3 → 2.0.0-next.5
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/.changeset/calm-beans-talk.md +5 -0
- package/.changeset/long-guests-explode.md +5 -0
- package/.changeset/nice-pants-rule.md +10 -0
- package/.changeset/odd-kiwis-compare.md +5 -0
- package/.changeset/pre.json +4 -0
- package/CHANGELOG.md +23 -0
- package/architecture.tldr +797 -0
- package/dist/index.d.mts +198 -140
- package/dist/index.d.ts +198 -140
- package/dist/index.js +4397 -148
- package/dist/index.mjs +4397 -149
- package/examples/chatbot.ts +9 -5
- package/examples/cot.ts +2 -4
- package/examples/jugs.ts +2 -2
- package/examples/learn-from-feedback.ts +7 -7
- package/examples/newspaper.ts +1 -1
- package/examples/rewoo.ts +62 -0
- package/examples/river-crossing.ts +2 -2
- package/examples/serverless.ts +71 -0
- package/examples/simple.ts +1 -1
- package/examples/ticTacToe.ts +6 -2
- package/examples/wiki.ts +2 -2
- package/package.json +14 -12
- package/readme.md +57 -0
- package/src/agent.test.ts +387 -30
- package/src/agent.ts +177 -64
- package/src/decide.test.ts +24 -2
- package/src/decide.ts +34 -78
- package/src/index.ts +1 -0
- package/src/{strategies/chainOfThought.ts → policies/chainOfThoughtPolicy.ts} +7 -9
- package/src/policies/index.ts +3 -0
- package/src/{strategies/shortestPath.test.ts → policies/shortestPathPolicy.test.ts} +2 -2
- package/src/{strategies/shortestPath.ts → policies/shortestPathPolicy.ts} +8 -8
- package/src/{strategies/simple.ts → policies/toolPolicy.ts} +27 -26
- package/src/text.ts +17 -22
- package/src/types.ts +162 -166
- package/src/agent-experimental.ts +0 -221
|
@@ -1,221 +0,0 @@
|
|
|
1
|
-
// // type AgentExperiences<TState, TReward> = Record<
|
|
2
|
-
// // string, // serialized state
|
|
3
|
-
// // Record<
|
|
4
|
-
// // string, // serialized event
|
|
5
|
-
// // {
|
|
6
|
-
// // state: TState;
|
|
7
|
-
// // reward: TReward;
|
|
8
|
-
// // }
|
|
9
|
-
// // >
|
|
10
|
-
|
|
11
|
-
// import OpenAI from 'openai';
|
|
12
|
-
// import {
|
|
13
|
-
// AnyEventObject,
|
|
14
|
-
// EventObject,
|
|
15
|
-
// AnyActorLogic,
|
|
16
|
-
// AnyMachineSnapshot,
|
|
17
|
-
// ActorRef,
|
|
18
|
-
// AnyActorRef,
|
|
19
|
-
// SnapshotFrom,
|
|
20
|
-
// EventFrom,
|
|
21
|
-
// } from 'xstate';
|
|
22
|
-
// import { getToolCalls } from './adapters/openai';
|
|
23
|
-
// import { ZodEventMapping } from './schemas';
|
|
24
|
-
|
|
25
|
-
// // >;
|
|
26
|
-
// interface AgentExperience<TState, TEvent extends AnyEventObject> {
|
|
27
|
-
// prevState: TState | undefined;
|
|
28
|
-
// event: TEvent;
|
|
29
|
-
// nextState: TState;
|
|
30
|
-
// }
|
|
31
|
-
|
|
32
|
-
// type AgentPlan<TState, TEvent extends EventObject> = Array<{
|
|
33
|
-
// /**
|
|
34
|
-
// * The current state
|
|
35
|
-
// */
|
|
36
|
-
// state: TState;
|
|
37
|
-
// /**
|
|
38
|
-
// * The event to execute
|
|
39
|
-
// */
|
|
40
|
-
// event: TEvent;
|
|
41
|
-
// /**
|
|
42
|
-
// * The expected next state
|
|
43
|
-
// */
|
|
44
|
-
// nextState?: TState;
|
|
45
|
-
// }>;
|
|
46
|
-
|
|
47
|
-
// interface AgentModel<
|
|
48
|
-
// // TLogic extends AnyActorLogic,
|
|
49
|
-
// TReward,
|
|
50
|
-
// TState,
|
|
51
|
-
// TEvent extends EventObject
|
|
52
|
-
// > {
|
|
53
|
-
// // policy: ({
|
|
54
|
-
// // logic,
|
|
55
|
-
// // state,
|
|
56
|
-
// // goal,
|
|
57
|
-
// // }: {
|
|
58
|
-
// // logic: TLogic;
|
|
59
|
-
// // state: TState;
|
|
60
|
-
// // goal: string;
|
|
61
|
-
// // }) => Promise<AgentPlan<TState>>;
|
|
62
|
-
// getExperiences: () => Promise<Array<AgentExperience<TState, TEvent>>>; // TODO: TLogic instead?
|
|
63
|
-
// addExperience: (experience: AgentExperience<TState, TEvent>) => void;
|
|
64
|
-
// getLogic: ({
|
|
65
|
-
// experiences,
|
|
66
|
-
// }: {
|
|
67
|
-
// experiences: Array<AgentExperience<TState, TEvent>>; // TODO: TLogic instead?
|
|
68
|
-
// }) => Promise<AnyActorLogic>;
|
|
69
|
-
// getNextEvents: ({
|
|
70
|
-
// logic,
|
|
71
|
-
// state,
|
|
72
|
-
// }: {
|
|
73
|
-
// logic: AnyActorLogic;
|
|
74
|
-
// state: TState;
|
|
75
|
-
// }) => Promise<AnyEventObject[]>;
|
|
76
|
-
// getPlans: ({
|
|
77
|
-
// logic,
|
|
78
|
-
// state,
|
|
79
|
-
// goals,
|
|
80
|
-
// }: {
|
|
81
|
-
// logic: AnyActorLogic;
|
|
82
|
-
// state: TState;
|
|
83
|
-
// goals: string[];
|
|
84
|
-
// }) => Promise<Array<AgentPlan<TState, TEvent>>>;
|
|
85
|
-
// getReward: ({
|
|
86
|
-
// logic,
|
|
87
|
-
// state,
|
|
88
|
-
// goals,
|
|
89
|
-
// action,
|
|
90
|
-
// }: {
|
|
91
|
-
// logic: AnyActorLogic;
|
|
92
|
-
// state: TState;
|
|
93
|
-
// goals: string[];
|
|
94
|
-
// action: EventObject;
|
|
95
|
-
// }) => Promise<TReward>;
|
|
96
|
-
// }
|
|
97
|
-
|
|
98
|
-
// interface AgentLogic<T> {
|
|
99
|
-
// /**
|
|
100
|
-
// * The next possible actions (represented by events) that the agent can take
|
|
101
|
-
// * based on the current state of the environment
|
|
102
|
-
// */
|
|
103
|
-
// getActions(state: T): Promise<AnyEventObject[]>;
|
|
104
|
-
// getPlan(state: T, goal: any): Promise<Array<[T, EventObject]>>;
|
|
105
|
-
// }
|
|
106
|
-
|
|
107
|
-
// interface Agent<TState extends AnyMachineSnapshot, TEvent extends EventObject> {
|
|
108
|
-
// act: (env: ActorRef<TState, TEvent>) => Promise<void>;
|
|
109
|
-
// }
|
|
110
|
-
|
|
111
|
-
// function createAgent2<TEnvironment extends AnyActorRef>(
|
|
112
|
-
// openai: OpenAI,
|
|
113
|
-
// // logic: AnyActorLogic,
|
|
114
|
-
// // input: InputFrom<TLogic>,
|
|
115
|
-
// getGoals: (state: SnapshotFrom<TEnvironment>) => string | string[],
|
|
116
|
-
// schemas: ZodEventMapping
|
|
117
|
-
// ): Agent<SnapshotFrom<TEnvironment>, EventFrom<TEnvironment>> {
|
|
118
|
-
// const experiences: Array<AgentExperience<any, any>> = [];
|
|
119
|
-
|
|
120
|
-
// const agentModel: AgentModel<
|
|
121
|
-
// any,
|
|
122
|
-
// SnapshotFrom<TEnvironment>,
|
|
123
|
-
// EventFrom<TEnvironment>
|
|
124
|
-
// > = {
|
|
125
|
-
// // policy: async ({ logic, state, goal }) => {
|
|
126
|
-
// // const toolEvents = await getToolCalls(
|
|
127
|
-
// // openai,
|
|
128
|
-
// // goal,
|
|
129
|
-
// // state,
|
|
130
|
-
// // 'gpt-4o-mini',
|
|
131
|
-
// // (eventType) => eventType.startsWith('agent.'),
|
|
132
|
-
// // schemas
|
|
133
|
-
// // );
|
|
134
|
-
// // console.log(toolEvents);
|
|
135
|
-
// // return toolEvents.map((te) => ({
|
|
136
|
-
// // state,
|
|
137
|
-
// // event: te as EventFromLogic<TLogic>,
|
|
138
|
-
// // }));
|
|
139
|
-
// // },
|
|
140
|
-
// addExperience: (experience) => {
|
|
141
|
-
// experiences.push(experience);
|
|
142
|
-
// },
|
|
143
|
-
// getExperiences: async () => experiences,
|
|
144
|
-
// getLogic: async ({ experiences }) => {
|
|
145
|
-
// return null as any; // TODO
|
|
146
|
-
// },
|
|
147
|
-
// getNextEvents: async ({ logic, state }) => {
|
|
148
|
-
// return [];
|
|
149
|
-
// },
|
|
150
|
-
// getReward: async ({ logic, state, goals, action }) => {
|
|
151
|
-
// return 0;
|
|
152
|
-
// },
|
|
153
|
-
// getPlans: async ({ logic, state, goals }) => {
|
|
154
|
-
// if (!goals[0]) {
|
|
155
|
-
// return [];
|
|
156
|
-
// }
|
|
157
|
-
|
|
158
|
-
// const toolEvents = await getToolCalls(
|
|
159
|
-
// openai,
|
|
160
|
-
// goals[0] + '\nOnly make a single tool call.',
|
|
161
|
-
// state as any,
|
|
162
|
-
// 'gpt-3.5-turbo-16k-0613',
|
|
163
|
-
// (eventType) => eventType.startsWith('agent.'),
|
|
164
|
-
// schemas
|
|
165
|
-
// );
|
|
166
|
-
|
|
167
|
-
// console.log(toolEvents);
|
|
168
|
-
|
|
169
|
-
// return [
|
|
170
|
-
// toolEvents.map((toolEvent) => ({
|
|
171
|
-
// state,
|
|
172
|
-
// event: toolEvent as EventFrom<TEnvironment>,
|
|
173
|
-
// })),
|
|
174
|
-
// ];
|
|
175
|
-
// },
|
|
176
|
-
// };
|
|
177
|
-
|
|
178
|
-
// // const actor = createActor(logic, {
|
|
179
|
-
// // input,
|
|
180
|
-
// // inspect: (inspEv) => {
|
|
181
|
-
// // if (inspEv.type === '@xstate.snapshot') {
|
|
182
|
-
// // agentModel.addExperience({
|
|
183
|
-
// // prevState: experiences[experiences.length - 1]?.nextState,
|
|
184
|
-
// // nextState: (inspEv.snapshot as AnyMachineSnapshot).value,
|
|
185
|
-
// // event: inspEv.event as EventFromLogic<TLogic>,
|
|
186
|
-
// // });
|
|
187
|
-
// // }
|
|
188
|
-
// // },
|
|
189
|
-
// // });
|
|
190
|
-
|
|
191
|
-
// return {
|
|
192
|
-
// act: async (actorRef) => {
|
|
193
|
-
// const state = actorRef.getSnapshot();
|
|
194
|
-
// // @ts-ignore
|
|
195
|
-
// console.log(state.value, state.context);
|
|
196
|
-
// const experiences = await agentModel.getExperiences();
|
|
197
|
-
// const goals = toArray(getGoals(state));
|
|
198
|
-
// console.log('Goal:', goals);
|
|
199
|
-
|
|
200
|
-
// const nextPlans = await agentModel.getPlans({
|
|
201
|
-
// logic: await agentModel.getLogic({ experiences }),
|
|
202
|
-
// goals,
|
|
203
|
-
// state,
|
|
204
|
-
// });
|
|
205
|
-
|
|
206
|
-
// const nextStep = nextPlans?.[0]?.[0];
|
|
207
|
-
|
|
208
|
-
// // TODO: race conditions!
|
|
209
|
-
// if (nextStep) {
|
|
210
|
-
// console.log('nextStep', nextStep?.event);
|
|
211
|
-
// actorRef.send(nextStep.event);
|
|
212
|
-
// } else {
|
|
213
|
-
// console.log('No next step');
|
|
214
|
-
// }
|
|
215
|
-
// },
|
|
216
|
-
// } satisfies Agent<SnapshotFrom<TEnvironment>, EventFrom<TEnvironment>>; // TODO: fix types
|
|
217
|
-
// }
|
|
218
|
-
|
|
219
|
-
// function toArray<T>(value: T | T[]): T[] {
|
|
220
|
-
// return Array.isArray(value) ? value : [value];
|
|
221
|
-
// }
|