@statelyai/agent 1.0.0-beta.0 → 1.0.0
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/heavy-moons-bake.md +12 -0
- package/.changeset/silly-berries-drop.md +5 -0
- package/.changeset/wild-bobcats-care.md +26 -0
- package/.env.template +0 -3
- package/.github/workflows/release.yml +3 -3
- package/dist/index.d.mts +152 -47
- package/dist/index.d.ts +152 -47
- package/dist/index.js +165 -110
- package/dist/index.mjs +155 -108
- package/examples/chatbot.ts +9 -17
- package/examples/cot.ts +5 -7
- package/examples/email.ts +2 -2
- package/examples/example.ts +7 -7
- package/examples/goal.ts +1 -1
- package/examples/joke.ts +8 -10
- package/examples/number.ts +1 -1
- package/examples/raffle.ts +1 -1
- package/examples/sandbox.ts +28 -0
- package/examples/support.ts +1 -1
- package/examples/ticTacToe.ts +18 -22
- package/examples/todo.ts +3 -1
- package/examples/tutor.ts +1 -1
- package/examples/verify.ts +1 -1
- package/examples/weather.ts +1 -1
- package/examples/wiki.ts +1 -1
- package/examples/word.ts +6 -3
- package/package.json +18 -10
- package/src/agent.test.ts +176 -4
- package/src/agent.ts +53 -18
- package/src/decision.ts +6 -5
- package/src/index.ts +2 -2
- package/src/planners/shortestPathPlanner.ts +2 -2
- package/src/planners/simplePlanner.ts +23 -12
- package/src/schemas.ts +4 -8
- package/src/strategies/chain-of-note.ts +3 -3
- package/src/text.ts +19 -31
- package/src/types.ts +134 -35
- package/src/utils.ts +59 -9
- package/src/templates/defaultToolCall.ts +0 -10
package/src/utils.ts
CHANGED
|
@@ -1,18 +1,50 @@
|
|
|
1
|
-
import { AnyMachineSnapshot, AnyStateNode } from 'xstate';
|
|
1
|
+
import { AnyMachineSnapshot, AnyStateMachine, AnyStateNode } from 'xstate';
|
|
2
|
+
import hash from 'object-hash';
|
|
2
3
|
import { TransitionData } from './types';
|
|
3
4
|
|
|
4
5
|
export function getAllTransitions(state: AnyMachineSnapshot): TransitionData[] {
|
|
5
6
|
const nodes = state._nodes;
|
|
6
7
|
const transitions = (nodes as AnyStateNode[])
|
|
7
8
|
.map((node) => [...(node as AnyStateNode).transitions.values()])
|
|
8
|
-
.
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
9
|
+
.map((nodeTransitions) => {
|
|
10
|
+
return nodeTransitions.map((nodeEventTransitions) => {
|
|
11
|
+
return nodeEventTransitions.map((transition) => {
|
|
12
|
+
return {
|
|
13
|
+
...transition,
|
|
14
|
+
guard:
|
|
15
|
+
typeof transition.guard === 'string'
|
|
16
|
+
? { type: transition.guard }
|
|
17
|
+
: (transition.guard as any), // TODO: fix
|
|
18
|
+
};
|
|
19
|
+
});
|
|
20
|
+
});
|
|
21
|
+
})
|
|
22
|
+
.flat(2);
|
|
23
|
+
|
|
24
|
+
return transitions;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function getAllMachineTransitions(
|
|
28
|
+
stateNode: AnyStateNode
|
|
29
|
+
): TransitionData[] {
|
|
30
|
+
const transitions: TransitionData[] = [...stateNode.transitions.values()]
|
|
31
|
+
.map((nodeTransitions) => {
|
|
32
|
+
return nodeTransitions.map((transition) => {
|
|
33
|
+
return {
|
|
34
|
+
...transition,
|
|
35
|
+
guard:
|
|
36
|
+
typeof transition.guard === 'string'
|
|
37
|
+
? { type: transition.guard }
|
|
38
|
+
: (transition.guard as any), // TODO: fix
|
|
39
|
+
};
|
|
40
|
+
});
|
|
41
|
+
})
|
|
42
|
+
.flat(2);
|
|
43
|
+
|
|
44
|
+
for (const s of Object.values(stateNode.states)) {
|
|
45
|
+
const stateTransitions = getAllMachineTransitions(s);
|
|
46
|
+
transitions.push(...stateTransitions);
|
|
47
|
+
}
|
|
16
48
|
|
|
17
49
|
return transitions;
|
|
18
50
|
}
|
|
@@ -20,3 +52,21 @@ export function getAllTransitions(state: AnyMachineSnapshot): TransitionData[] {
|
|
|
20
52
|
export function wrapInXml(tagName: string, content: string): string {
|
|
21
53
|
return `<${tagName}>${content}</${tagName}>`;
|
|
22
54
|
}
|
|
55
|
+
|
|
56
|
+
export function randomId() {
|
|
57
|
+
const timestamp = Date.now().toString(36);
|
|
58
|
+
const random = Math.random().toString(36).substring(2, 9);
|
|
59
|
+
return timestamp + random;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const machineHashes: WeakMap<AnyStateMachine, string> = new WeakMap();
|
|
63
|
+
/**
|
|
64
|
+
* Returns a string hash representing only the transitions in the state machine.
|
|
65
|
+
*/
|
|
66
|
+
export function getMachineHash(machine: AnyStateMachine): string {
|
|
67
|
+
if (machineHashes.has(machine)) return machineHashes.get(machine)!;
|
|
68
|
+
const transitions = getAllMachineTransitions(machine.root);
|
|
69
|
+
const machineHash = hash(transitions);
|
|
70
|
+
machineHashes.set(machine, machineHash);
|
|
71
|
+
return machineHash;
|
|
72
|
+
}
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { PromptTemplate } from '../types';
|
|
2
|
-
import { defaultTextTemplate } from './defaultText';
|
|
3
|
-
|
|
4
|
-
export const defaultToolCallTemplate: PromptTemplate<any> = (data) => {
|
|
5
|
-
return `
|
|
6
|
-
${defaultTextTemplate(data)}
|
|
7
|
-
|
|
8
|
-
Only make a single tool call to achieve the above goal.
|
|
9
|
-
`.trim();
|
|
10
|
-
};
|