@statelyai/agent 2.0.0-next.2 → 2.0.0-next.4
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/nice-pants-rule.md +10 -0
- package/.changeset/pink-eagles-deliver.md +13 -0
- package/.changeset/pre.json +7 -1
- package/.changeset/quiet-turtles-do.md +7 -0
- package/.changeset/sweet-clouds-mix.md +16 -0
- package/.changeset/swift-mangos-rush.md +5 -0
- package/.changeset/tough-ways-rhyme.md +5 -0
- package/CHANGELOG.md +50 -0
- package/architecture.tldr +175 -0
- package/dist/index.d.mts +116 -102
- package/dist/index.d.ts +116 -102
- package/dist/index.js +51 -74
- package/dist/index.mjs +51 -74
- package/examples/cot.ts +19 -53
- package/examples/customer-service-sim.ts +3 -3
- package/examples/email.ts +2 -10
- package/examples/example.ts +2 -2
- package/examples/goal.ts +2 -2
- package/examples/joke.ts +10 -10
- package/examples/learn-from-feedback.ts +47 -24
- package/examples/number.ts +2 -2
- package/examples/raffle.ts +2 -2
- package/examples/support.ts +7 -11
- package/examples/ticTacToe.ts +2 -2
- package/examples/todo.ts +3 -3
- package/examples/tutor.ts +2 -2
- package/examples/verify.ts +2 -2
- package/examples/weather-agent.ts +3 -5
- package/examples/weather.ts +10 -7
- package/package.json +1 -1
- package/src/agent.test.ts +124 -43
- package/src/agent.ts +61 -42
- package/src/decide.test.ts +22 -0
- package/src/decide.ts +27 -29
- package/src/strategies/chainOfThought.ts +5 -3
- package/src/strategies/shortestPath.ts +7 -2
- package/src/strategies/{simple.ts → simpleStrategy.ts} +5 -16
- package/src/templates/defaultText.ts +3 -0
- package/src/text.ts +13 -14
- package/src/types.ts +103 -104
- package/src/utils.ts +1 -1
|
@@ -6,15 +6,16 @@ import {
|
|
|
6
6
|
PromptTemplate,
|
|
7
7
|
} from '../types';
|
|
8
8
|
import { getMessages } from '../text';
|
|
9
|
-
import { simpleStrategy } from './
|
|
9
|
+
import { simpleStrategy } from './simpleStrategy';
|
|
10
10
|
import { convertToXml } from '../utils';
|
|
11
11
|
|
|
12
12
|
const chainOfThoughtPromptTemplate: PromptTemplate<any> = ({
|
|
13
|
+
stateValue,
|
|
13
14
|
context,
|
|
14
15
|
goal,
|
|
15
16
|
}) => {
|
|
16
17
|
return `
|
|
17
|
-
${convertToXml({ context, goal })}
|
|
18
|
+
${convertToXml({ stateValue, context, goal })}
|
|
18
19
|
|
|
19
20
|
How would you achieve the goal? Think step-by-step.
|
|
20
21
|
`.trim();
|
|
@@ -25,7 +26,8 @@ export async function chainOfThoughtStrategy<T extends AnyAgent>(
|
|
|
25
26
|
input: AgentDecideInput<any>
|
|
26
27
|
): Promise<AgentDecision<any> | undefined> {
|
|
27
28
|
const prompt = chainOfThoughtPromptTemplate({
|
|
28
|
-
|
|
29
|
+
stateValue: input.state.value,
|
|
30
|
+
context: input.context ?? input.state.context,
|
|
29
31
|
goal: input.goal,
|
|
30
32
|
});
|
|
31
33
|
|
|
@@ -13,10 +13,14 @@ import { z } from 'zod';
|
|
|
13
13
|
import { zodToJsonSchema } from 'zod-to-json-schema';
|
|
14
14
|
import Ajv from 'ajv';
|
|
15
15
|
import { AnyMachineSnapshot } from 'xstate';
|
|
16
|
+
import { randomId } from '../utils';
|
|
16
17
|
|
|
17
18
|
const ajv = new Ajv();
|
|
18
19
|
|
|
19
|
-
function observedStatesEqual(
|
|
20
|
+
function observedStatesEqual(
|
|
21
|
+
state1: ObservedState<any>,
|
|
22
|
+
state2: ObservedState<any>
|
|
23
|
+
) {
|
|
20
24
|
// check state value && state context
|
|
21
25
|
return (
|
|
22
26
|
JSON.stringify(state1.value) === JSON.stringify(state2.value) &&
|
|
@@ -24,7 +28,7 @@ function observedStatesEqual(state1: ObservedState, state2: ObservedState) {
|
|
|
24
28
|
);
|
|
25
29
|
}
|
|
26
30
|
|
|
27
|
-
function trimSteps(steps: AgentStep<any>[], currentState: ObservedState) {
|
|
31
|
+
function trimSteps(steps: AgentStep<any>[], currentState: ObservedState<any>) {
|
|
28
32
|
const index = steps.findIndex(
|
|
29
33
|
(step) => step.state && observedStatesEqual(step.state, currentState)
|
|
30
34
|
);
|
|
@@ -162,6 +166,7 @@ Examples:
|
|
|
162
166
|
const nextStep = leastWeightPath?.steps[0];
|
|
163
167
|
|
|
164
168
|
return {
|
|
169
|
+
id: randomId(),
|
|
165
170
|
strategy: 'shortestPath',
|
|
166
171
|
episodeId: agent.episodeId,
|
|
167
172
|
goal: input.goal,
|
|
@@ -32,7 +32,8 @@ export async function simpleStrategy<T extends AnyAgent>(
|
|
|
32
32
|
// Create a prompt with the given context and goal.
|
|
33
33
|
// The template is used to ensure that a single tool call at most is made.
|
|
34
34
|
const prompt = simpleStrategyPromptTemplate({
|
|
35
|
-
|
|
35
|
+
stateValue: input.state.value,
|
|
36
|
+
context: input.context ?? input.state.context,
|
|
36
37
|
goal: input.goal,
|
|
37
38
|
});
|
|
38
39
|
|
|
@@ -40,16 +41,7 @@ export async function simpleStrategy<T extends AnyAgent>(
|
|
|
40
41
|
|
|
41
42
|
const model = input.model ? agent.wrap(input.model) : agent.model;
|
|
42
43
|
|
|
43
|
-
const {
|
|
44
|
-
state,
|
|
45
|
-
context,
|
|
46
|
-
machine,
|
|
47
|
-
prevDecision,
|
|
48
|
-
events,
|
|
49
|
-
goal,
|
|
50
|
-
model: _,
|
|
51
|
-
...rest
|
|
52
|
-
} = input;
|
|
44
|
+
const { state, machine, events, goal, model: _, ...rest } = input;
|
|
53
45
|
|
|
54
46
|
const machineState =
|
|
55
47
|
input.machine && input.state
|
|
@@ -87,11 +79,12 @@ export async function simpleStrategy<T extends AnyAgent>(
|
|
|
87
79
|
}
|
|
88
80
|
|
|
89
81
|
return {
|
|
82
|
+
id: randomId(),
|
|
90
83
|
strategy: 'simple',
|
|
91
84
|
goal: input.goal,
|
|
92
85
|
goalState: input.state,
|
|
93
86
|
nextEvent: singleResult.result,
|
|
94
|
-
episodeId: agent.episodeId,
|
|
87
|
+
episodeId: input.episodeId ?? agent.episodeId,
|
|
95
88
|
timestamp: Date.now(),
|
|
96
89
|
paths: [
|
|
97
90
|
{
|
|
@@ -109,7 +102,3 @@ export async function simpleStrategy<T extends AnyAgent>(
|
|
|
109
102
|
],
|
|
110
103
|
};
|
|
111
104
|
}
|
|
112
|
-
|
|
113
|
-
export function createSimpleStrategy<T extends AnyAgent>() {
|
|
114
|
-
return simpleStrategy;
|
|
115
|
-
}
|
|
@@ -3,6 +3,9 @@ import { wrapInXml } from '../utils';
|
|
|
3
3
|
|
|
4
4
|
export const defaultTextTemplate: PromptTemplate<any> = (data) => {
|
|
5
5
|
const preamble = [
|
|
6
|
+
data.stateValue
|
|
7
|
+
? wrapInXml('stateValue', JSON.stringify(data.stateValue))
|
|
8
|
+
: undefined,
|
|
6
9
|
data.context
|
|
7
10
|
? wrapInXml('context', JSON.stringify(data.context))
|
|
8
11
|
: undefined,
|
package/src/text.ts
CHANGED
|
@@ -28,10 +28,10 @@ import {
|
|
|
28
28
|
* @param options
|
|
29
29
|
* @returns
|
|
30
30
|
*/
|
|
31
|
-
export async function getMessages(
|
|
32
|
-
agent:
|
|
31
|
+
export async function getMessages<TAgent extends AnyAgent>(
|
|
32
|
+
agent: TAgent,
|
|
33
33
|
prompt: string,
|
|
34
|
-
options: Omit<AgentGenerateTextOptions
|
|
34
|
+
options: Omit<AgentGenerateTextOptions<TAgent>, 'prompt'>
|
|
35
35
|
): Promise<CoreMessage[]> {
|
|
36
36
|
let messages: CoreMessage[] = [];
|
|
37
37
|
if (typeof options.messages === 'function') {
|
|
@@ -48,13 +48,13 @@ export async function getMessages(
|
|
|
48
48
|
return messages;
|
|
49
49
|
}
|
|
50
50
|
|
|
51
|
-
export function fromTextStream<
|
|
52
|
-
agent:
|
|
53
|
-
options?: AgentStreamTextOptions
|
|
51
|
+
export function fromTextStream<TAgent extends AnyAgent>(
|
|
52
|
+
agent: TAgent,
|
|
53
|
+
options?: AgentStreamTextOptions<TAgent>
|
|
54
54
|
): ObservableActorLogic<
|
|
55
55
|
{ textDelta: string },
|
|
56
|
-
Omit<AgentStreamTextOptions
|
|
57
|
-
context?:
|
|
56
|
+
Omit<AgentStreamTextOptions<TAgent>, 'context'> & {
|
|
57
|
+
context?: Record<string, any>;
|
|
58
58
|
}
|
|
59
59
|
> {
|
|
60
60
|
const template = options?.template ?? defaultTextTemplate;
|
|
@@ -106,17 +106,16 @@ export function fromTextStream<T extends AnyAgent>(
|
|
|
106
106
|
});
|
|
107
107
|
}
|
|
108
108
|
|
|
109
|
-
export function fromText<
|
|
110
|
-
agent:
|
|
111
|
-
options?: AgentGenerateTextOptions
|
|
109
|
+
export function fromText<TAgent extends AnyAgent>(
|
|
110
|
+
agent: TAgent,
|
|
111
|
+
options?: AgentGenerateTextOptions<TAgent>
|
|
112
112
|
): PromiseActorLogic<
|
|
113
113
|
GenerateTextResult<Record<string, CoreTool<any, any>>>,
|
|
114
|
-
Omit<AgentGenerateTextOptions
|
|
115
|
-
context?:
|
|
114
|
+
Omit<AgentGenerateTextOptions<TAgent>, 'context'> & {
|
|
115
|
+
context?: Record<string, any>;
|
|
116
116
|
}
|
|
117
117
|
> {
|
|
118
118
|
const resolvedOptions = {
|
|
119
|
-
...agent.defaultOptions,
|
|
120
119
|
...options,
|
|
121
120
|
};
|
|
122
121
|
|
package/src/types.ts
CHANGED
|
@@ -27,18 +27,23 @@ export type GenerateTextOptions = Parameters<typeof generateText>[0];
|
|
|
27
27
|
|
|
28
28
|
export type StreamTextOptions = Parameters<typeof streamText>[0];
|
|
29
29
|
|
|
30
|
-
export type CostFunction<
|
|
31
|
-
path: AgentPath<
|
|
30
|
+
export type CostFunction<TAgent extends AnyAgent> = (
|
|
31
|
+
path: AgentPath<TAgent>
|
|
32
32
|
) => number;
|
|
33
33
|
|
|
34
|
-
export type AgentDecideInput<
|
|
35
|
-
AgentGenerateTextOptions
|
|
36
|
-
'prompt' | 'tools'
|
|
34
|
+
export type AgentDecideInput<TAgent extends AnyAgent> = Omit<
|
|
35
|
+
AgentGenerateTextOptions<TAgent>,
|
|
36
|
+
'model' | 'prompt' | 'tools'
|
|
37
37
|
> & {
|
|
38
|
+
episodeId?: string;
|
|
38
39
|
/**
|
|
39
40
|
* The currently observed state.
|
|
40
41
|
*/
|
|
41
|
-
state: ObservedState
|
|
42
|
+
state: ObservedState<TAgent>;
|
|
43
|
+
/**
|
|
44
|
+
* The context to provide in the prompt to the agent. This overrides the `state.context`.
|
|
45
|
+
*/
|
|
46
|
+
context?: Record<string, any>;
|
|
42
47
|
/**
|
|
43
48
|
* The goal for the agent to accomplish.
|
|
44
49
|
* The agent will make a decision based on this goal.
|
|
@@ -48,45 +53,45 @@ export type AgentDecideInput<TEvent extends EventObject> = Omit<
|
|
|
48
53
|
* The events that the agent can trigger. This is a mapping of
|
|
49
54
|
* event types to Zod event schemas.
|
|
50
55
|
*/
|
|
51
|
-
events
|
|
56
|
+
events?: ZodEventMapping;
|
|
57
|
+
allowedEvents?: Array<EventFromAgent<TAgent>['type']>;
|
|
52
58
|
/**
|
|
53
59
|
* The state machine that represents the environment the agent
|
|
54
60
|
* is interacting with.
|
|
55
61
|
*/
|
|
56
62
|
machine?: AnyStateMachine;
|
|
57
|
-
/**
|
|
58
|
-
* The previous decision made by the agent.
|
|
59
|
-
*/
|
|
60
|
-
prevDecision?: AgentDecision<TEvent>;
|
|
61
63
|
|
|
62
64
|
/**
|
|
63
65
|
* The total cost of the path to the goal state.
|
|
64
66
|
*/
|
|
65
|
-
costFunction?: CostFunction<
|
|
67
|
+
costFunction?: CostFunction<TAgent>;
|
|
66
68
|
|
|
67
69
|
/**
|
|
68
70
|
* The maximum number of attempts to make a decision.
|
|
69
71
|
* Defaults to 2.
|
|
70
72
|
*/
|
|
71
73
|
maxAttempts?: number;
|
|
74
|
+
strategy?: AgentStrategy<TAgent>;
|
|
75
|
+
model?: LanguageModel;
|
|
72
76
|
};
|
|
73
77
|
|
|
74
|
-
export type AgentStep<
|
|
78
|
+
export type AgentStep<TAgent extends AnyAgent> = {
|
|
75
79
|
/** The event to take */
|
|
76
|
-
event:
|
|
80
|
+
event: EventFromAgent<TAgent>;
|
|
77
81
|
/** The next expected state after taking the event */
|
|
78
|
-
state: ObservedState | undefined;
|
|
82
|
+
state: ObservedState<TAgent> | undefined;
|
|
79
83
|
};
|
|
80
84
|
|
|
81
|
-
export type AgentPath<
|
|
85
|
+
export type AgentPath<TAgent extends AnyAgent> = {
|
|
82
86
|
/** The expected ending state of the path */
|
|
83
|
-
state: ObservedState | undefined;
|
|
87
|
+
state: ObservedState<TAgent> | undefined;
|
|
84
88
|
/** The steps to reach the ending state */
|
|
85
|
-
steps: Array<AgentStep<
|
|
89
|
+
steps: Array<AgentStep<TAgent>>;
|
|
86
90
|
weight?: number;
|
|
87
91
|
};
|
|
88
92
|
|
|
89
|
-
export type AgentDecision<
|
|
93
|
+
export type AgentDecision<TAgent extends AnyAgent> = {
|
|
94
|
+
id: string;
|
|
90
95
|
/**
|
|
91
96
|
* The strategy used to generate the decision
|
|
92
97
|
*/
|
|
@@ -95,17 +100,17 @@ export type AgentDecision<TEvent extends EventObject> = {
|
|
|
95
100
|
/**
|
|
96
101
|
* The ending state of the decision.
|
|
97
102
|
*/
|
|
98
|
-
goalState: ObservedState | undefined;
|
|
103
|
+
goalState: ObservedState<TAgent> | undefined;
|
|
99
104
|
/**
|
|
100
105
|
* The next event that the agent decided needs to occur to achieve the `goal`.
|
|
101
106
|
*
|
|
102
107
|
* This next event is chosen from the
|
|
103
108
|
*/
|
|
104
|
-
nextEvent:
|
|
109
|
+
nextEvent: EventFromAgent<TAgent> | undefined;
|
|
105
110
|
/**
|
|
106
111
|
* The paths that the agent can take to achieve the goal.
|
|
107
112
|
*/
|
|
108
|
-
paths: AgentPath<
|
|
113
|
+
paths: AgentPath<TAgent>[];
|
|
109
114
|
episodeId: string;
|
|
110
115
|
timestamp: number;
|
|
111
116
|
// result: GenerateObjectResult<any>;
|
|
@@ -118,17 +123,13 @@ export interface TransitionData {
|
|
|
118
123
|
target?: any;
|
|
119
124
|
}
|
|
120
125
|
|
|
121
|
-
export type PromptTemplate<
|
|
126
|
+
export type PromptTemplate<TAgent extends AnyAgent> = (data: {
|
|
122
127
|
goal: string;
|
|
123
128
|
/**
|
|
124
129
|
* The observed state
|
|
125
130
|
*/
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
* The context to provide.
|
|
129
|
-
* This overrides the observed state.context, if provided.
|
|
130
|
-
*/
|
|
131
|
-
context?: any;
|
|
131
|
+
stateValue?: any;
|
|
132
|
+
context?: Record<string, any>;
|
|
132
133
|
/**
|
|
133
134
|
* The state machine model of the observed environment
|
|
134
135
|
*/
|
|
@@ -144,53 +145,42 @@ export type PromptTemplate<TEvents extends EventObject> = (data: {
|
|
|
144
145
|
observations?: AgentObservation<any>[]; // TODO
|
|
145
146
|
feedback?: AgentFeedback[];
|
|
146
147
|
messages?: AgentMessage[];
|
|
147
|
-
decisions?: AgentDecision<
|
|
148
|
+
decisions?: AgentDecision<TAgent>[];
|
|
148
149
|
}) => string;
|
|
149
150
|
|
|
150
|
-
export type AgentStrategy<
|
|
151
|
-
agent:
|
|
152
|
-
input: AgentDecideInput<
|
|
153
|
-
) => Promise<AgentDecision<
|
|
151
|
+
export type AgentStrategy<TAgent extends AnyAgent> = (
|
|
152
|
+
agent: TAgent,
|
|
153
|
+
input: AgentDecideInput<EventFromAgent<TAgent>>
|
|
154
|
+
) => Promise<AgentDecision<TAgent> | undefined>;
|
|
154
155
|
|
|
155
156
|
export type AgentInteractInput<T extends AnyAgent> = Omit<
|
|
156
|
-
|
|
157
|
+
AgentDecideInput<T>,
|
|
157
158
|
'state'
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
goal: string;
|
|
162
|
-
state: ObservedState;
|
|
163
|
-
context?: Record<string, any>;
|
|
164
|
-
machine?: AnyStateMachine;
|
|
165
|
-
model?: LanguageModel;
|
|
166
|
-
execute?: (event: AnyEventObject) => Promise<void>;
|
|
167
|
-
strategy?: AgentStrategy<T>;
|
|
168
|
-
events?: ZodEventMapping;
|
|
169
|
-
allowedEvents?: Array<EventsFromAgent<T>['type']>;
|
|
170
|
-
/**
|
|
171
|
-
* The maximum number of times the agent will attempt to make a decision.
|
|
172
|
-
* Defaults to 2.
|
|
173
|
-
*/
|
|
174
|
-
maxAttempts?: number;
|
|
175
|
-
} & Omit<Parameters<typeof generateText>[0], 'model' | 'tools' | 'prompt'>;
|
|
159
|
+
> & {
|
|
160
|
+
state?: never;
|
|
161
|
+
};
|
|
176
162
|
|
|
177
|
-
export
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
/**
|
|
181
|
-
* The message correlation that the feedback is relevant for
|
|
182
|
-
*/
|
|
163
|
+
export type AgentFeedback = {
|
|
164
|
+
score: number;
|
|
165
|
+
comment: string | undefined;
|
|
183
166
|
attributes: Record<string, any>;
|
|
184
167
|
timestamp: number;
|
|
185
168
|
episodeId: string;
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
169
|
+
} & (
|
|
170
|
+
| { observationId: string; decisionId?: never }
|
|
171
|
+
| { decisionId: string; observationId?: never }
|
|
172
|
+
);
|
|
173
|
+
|
|
174
|
+
export type AgentFeedbackInput = {
|
|
175
|
+
episodeId?: string;
|
|
176
|
+
score: number;
|
|
177
|
+
comment?: string;
|
|
178
|
+
attributes?: Record<string, any>;
|
|
192
179
|
timestamp?: number;
|
|
193
|
-
}
|
|
180
|
+
} & (
|
|
181
|
+
| { observationId: string; decisionId?: never }
|
|
182
|
+
| { decisionId: string; observationId?: never }
|
|
183
|
+
);
|
|
194
184
|
|
|
195
185
|
export type AgentMessage = CoreMessage & {
|
|
196
186
|
timestamp: number;
|
|
@@ -332,22 +322,29 @@ export type AgentMessageInput = CoreMessage & {
|
|
|
332
322
|
|
|
333
323
|
export interface AgentObservation<TActor extends ActorRefLike> {
|
|
334
324
|
id: string;
|
|
335
|
-
|
|
325
|
+
decisionId?: string | undefined;
|
|
326
|
+
goal?: string;
|
|
336
327
|
prevState: SnapshotFrom<TActor> | undefined;
|
|
337
328
|
event: EventFrom<TActor> | undefined;
|
|
338
329
|
state: SnapshotFrom<TActor>;
|
|
339
|
-
machineHash: string | undefined;
|
|
330
|
+
// machineHash: string | undefined;
|
|
340
331
|
episodeId: string;
|
|
341
332
|
timestamp: number;
|
|
342
333
|
}
|
|
343
334
|
|
|
344
|
-
export interface AgentObservationInput {
|
|
335
|
+
export interface AgentObservationInput<TAgent extends AnyAgent> {
|
|
345
336
|
id?: string;
|
|
346
|
-
|
|
337
|
+
episodeId?: string;
|
|
338
|
+
/**
|
|
339
|
+
* The agent decision that the observation is relevant for
|
|
340
|
+
*/
|
|
341
|
+
decisionId?: string | undefined;
|
|
342
|
+
prevState?: ObservedState<TAgent>;
|
|
347
343
|
event?: AnyEventObject;
|
|
348
|
-
state: ObservedState
|
|
349
|
-
machine?: AnyStateMachine;
|
|
344
|
+
state: ObservedState<TAgent>;
|
|
345
|
+
// machine?: AnyStateMachine;
|
|
350
346
|
timestamp?: number;
|
|
347
|
+
goal?: string | undefined;
|
|
351
348
|
}
|
|
352
349
|
|
|
353
350
|
export type AgentDecisionInput = {
|
|
@@ -356,12 +353,12 @@ export type AgentDecisionInput = {
|
|
|
356
353
|
context?: Record<string, any>;
|
|
357
354
|
} & Omit<Parameters<typeof generateText>[0], 'model' | 'tools' | 'prompt'>;
|
|
358
355
|
|
|
359
|
-
export type AgentDecisionLogic<
|
|
360
|
-
AgentDecision<
|
|
356
|
+
export type AgentDecisionLogic<TAgent extends AnyAgent> = PromiseActorLogic<
|
|
357
|
+
AgentDecision<TAgent> | undefined,
|
|
361
358
|
AgentDecisionInput | string
|
|
362
359
|
>;
|
|
363
360
|
|
|
364
|
-
export type AgentEmitted<
|
|
361
|
+
export type AgentEmitted<TAgent extends AnyAgent> =
|
|
365
362
|
| {
|
|
366
363
|
type: 'feedback';
|
|
367
364
|
feedback: AgentFeedback;
|
|
@@ -376,11 +373,11 @@ export type AgentEmitted<TEvents extends EventObject> =
|
|
|
376
373
|
}
|
|
377
374
|
| {
|
|
378
375
|
type: 'decision';
|
|
379
|
-
decision: AgentDecision<
|
|
376
|
+
decision: AgentDecision<TAgent>;
|
|
380
377
|
};
|
|
381
378
|
|
|
382
|
-
export type AgentLogic<
|
|
383
|
-
TransitionSnapshot<AgentMemoryContext
|
|
379
|
+
export type AgentLogic<TAgent extends AnyAgent> = ActorLogic<
|
|
380
|
+
TransitionSnapshot<AgentMemoryContext<TAgent>>,
|
|
384
381
|
| {
|
|
385
382
|
type: 'agent.feedback';
|
|
386
383
|
feedback: AgentFeedback;
|
|
@@ -395,19 +392,21 @@ export type AgentLogic<TEvents extends EventObject> = ActorLogic<
|
|
|
395
392
|
}
|
|
396
393
|
| {
|
|
397
394
|
type: 'agent.decision';
|
|
398
|
-
decision: AgentDecision<
|
|
395
|
+
decision: AgentDecision<TAgent>;
|
|
399
396
|
},
|
|
400
397
|
any, // TODO: input
|
|
401
398
|
any,
|
|
402
|
-
AgentEmitted<
|
|
399
|
+
AgentEmitted<TAgent>
|
|
403
400
|
>;
|
|
404
401
|
|
|
405
402
|
export type EventsFromZodEventMapping<TEventSchemas extends ZodEventMapping> =
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
403
|
+
Compute<
|
|
404
|
+
Values<{
|
|
405
|
+
[K in keyof TEventSchemas & string]: {
|
|
406
|
+
type: K;
|
|
407
|
+
} & TypeOf<TEventSchemas[K]>;
|
|
408
|
+
}>
|
|
409
|
+
>;
|
|
411
410
|
|
|
412
411
|
export type ContextFromZodContextMapping<
|
|
413
412
|
TContextSchema extends ZodContextMapping
|
|
@@ -419,27 +418,27 @@ export type AnyAgent = Agent<any, any, any, any>;
|
|
|
419
418
|
|
|
420
419
|
export type FromAgent<T> = T | ((agent: AnyAgent) => T | Promise<T>);
|
|
421
420
|
|
|
422
|
-
export type CommonTextOptions = {
|
|
421
|
+
export type CommonTextOptions<TAgent extends AnyAgent> = {
|
|
423
422
|
prompt: FromAgent<string>;
|
|
424
423
|
model?: LanguageModel;
|
|
425
|
-
context?: Record<string, any>;
|
|
426
424
|
messages?: FromAgent<CoreMessage[]>;
|
|
427
425
|
template?: PromptTemplate<any>;
|
|
426
|
+
context?: Record<string, any>;
|
|
428
427
|
};
|
|
429
428
|
|
|
430
|
-
export type AgentGenerateTextOptions = Omit<
|
|
429
|
+
export type AgentGenerateTextOptions<TAgent extends AnyAgent> = Omit<
|
|
431
430
|
GenerateTextOptions,
|
|
432
431
|
'model' | 'prompt' | 'messages'
|
|
433
432
|
> &
|
|
434
|
-
CommonTextOptions
|
|
433
|
+
CommonTextOptions<TAgent>;
|
|
435
434
|
|
|
436
|
-
export type AgentStreamTextOptions = Omit<
|
|
435
|
+
export type AgentStreamTextOptions<TAgent extends AnyAgent> = Omit<
|
|
437
436
|
StreamTextOptions,
|
|
438
437
|
'model' | 'prompt' | 'messages'
|
|
439
438
|
> &
|
|
440
|
-
CommonTextOptions
|
|
439
|
+
CommonTextOptions<TAgent>;
|
|
441
440
|
|
|
442
|
-
export interface ObservedState {
|
|
441
|
+
export interface ObservedState<TAgent extends AnyAgent> {
|
|
443
442
|
/**
|
|
444
443
|
* The current state value of the state machine, e.g.
|
|
445
444
|
* `"loading"` or `"processing"` or `"ready"`
|
|
@@ -448,7 +447,7 @@ export interface ObservedState {
|
|
|
448
447
|
/**
|
|
449
448
|
* Additional contextual data related to the current state
|
|
450
449
|
*/
|
|
451
|
-
context?:
|
|
450
|
+
context?: ContextFromAgent<TAgent>;
|
|
452
451
|
}
|
|
453
452
|
|
|
454
453
|
export type ObservedStateFrom<TActor extends ActorRefLike> = Pick<
|
|
@@ -456,24 +455,24 @@ export type ObservedStateFrom<TActor extends ActorRefLike> = Pick<
|
|
|
456
455
|
'value' | 'context'
|
|
457
456
|
>;
|
|
458
457
|
|
|
459
|
-
export type AgentMemoryContext = {
|
|
460
|
-
observations: AgentObservation<
|
|
458
|
+
export type AgentMemoryContext<TAgent extends AnyAgent> = {
|
|
459
|
+
observations: AgentObservation<TAgent>[]; // TODO
|
|
461
460
|
messages: AgentMessage[];
|
|
462
|
-
decisions: AgentDecision<
|
|
461
|
+
decisions: AgentDecision<TAgent>[];
|
|
463
462
|
feedback: AgentFeedback[];
|
|
464
463
|
};
|
|
465
464
|
|
|
466
|
-
export interface AgentLongTermMemory {
|
|
467
|
-
get<K extends keyof AgentMemoryContext
|
|
465
|
+
export interface AgentLongTermMemory<TAgent extends AnyAgent> {
|
|
466
|
+
get<K extends keyof AgentMemoryContext<TAgent>>(
|
|
468
467
|
key: K
|
|
469
|
-
): Promise<AgentMemoryContext[K]>;
|
|
470
|
-
append<K extends keyof AgentMemoryContext
|
|
468
|
+
): Promise<AgentMemoryContext<TAgent>[K]>;
|
|
469
|
+
append<K extends keyof AgentMemoryContext<TAgent>>(
|
|
471
470
|
key: K,
|
|
472
|
-
item: AgentMemoryContext[K][0]
|
|
471
|
+
item: AgentMemoryContext<TAgent>[K][0]
|
|
473
472
|
): Promise<void>;
|
|
474
|
-
set<K extends keyof AgentMemoryContext
|
|
473
|
+
set<K extends keyof AgentMemoryContext<TAgent>>(
|
|
475
474
|
key: K,
|
|
476
|
-
items: AgentMemoryContext[K]
|
|
475
|
+
items: AgentMemoryContext<TAgent>[K]
|
|
477
476
|
): Promise<void>;
|
|
478
477
|
}
|
|
479
478
|
|
|
@@ -481,7 +480,7 @@ export type Compute<A extends any> = { [K in keyof A]: A[K] } & unknown;
|
|
|
481
480
|
|
|
482
481
|
export type MaybePromise<T> = T | Promise<T>;
|
|
483
482
|
|
|
484
|
-
export type
|
|
483
|
+
export type EventFromAgent<T extends AnyAgent> = T extends Agent<
|
|
485
484
|
infer _,
|
|
486
485
|
infer __,
|
|
487
486
|
infer TEvents,
|