@statelyai/agent 0.0.7 → 0.0.8
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 +29 -0
- package/dist/index.d.ts +13 -44
- package/dist/index.js +4761 -25
- package/examples/joke.ts +55 -62
- package/examples/numberGuesser.ts +10 -37
- package/examples/ticTacToe.ts +37 -72
- package/examples/weather.ts +48 -27
- package/examples/wordGuesser.ts +14 -26
- package/package.json +5 -3
- package/src/adapters/openai.ts +8 -3
- package/src/agent.ts +1 -1
- package/src/index.ts +1 -1
- package/src/schemas.ts +18 -32
- package/src/utils.ts +23 -1
package/src/schemas.ts
CHANGED
|
@@ -1,40 +1,26 @@
|
|
|
1
1
|
import { Values } from 'xstate';
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
EventSchemas,
|
|
5
|
-
ConvertToJSONSchemas,
|
|
6
|
-
createEventSchemas,
|
|
7
|
-
} from './utils';
|
|
8
|
-
import { FromSchema } from 'json-schema-to-ts';
|
|
2
|
+
import { createZodEventSchemas } from './utils';
|
|
3
|
+
import { SomeZodObject, TypeOf } from 'zod';
|
|
9
4
|
|
|
10
|
-
export
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
*/
|
|
26
|
-
events: TEventSchemas;
|
|
27
|
-
}): {
|
|
28
|
-
context: TContextSchema | undefined;
|
|
29
|
-
events: ConvertToJSONSchemas<TEventSchemas>;
|
|
30
|
-
types: {
|
|
31
|
-
context: FromSchema<TContextSchema>;
|
|
32
|
-
events: FromSchema<Values<ConvertToJSONSchemas<TEventSchemas>>>;
|
|
5
|
+
export type ZodEventTypes = {
|
|
6
|
+
// map event types to Zod types
|
|
7
|
+
[eventType: string]: SomeZodObject;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export function defineEvents<const TEventSchemas extends ZodEventTypes>(
|
|
11
|
+
events: TEventSchemas
|
|
12
|
+
): {
|
|
13
|
+
types: Values<{
|
|
14
|
+
[K in keyof TEventSchemas]: {
|
|
15
|
+
type: K;
|
|
16
|
+
} & TypeOf<TEventSchemas[K]>;
|
|
17
|
+
}>;
|
|
18
|
+
schemas: {
|
|
19
|
+
[K in keyof TEventSchemas]: unknown;
|
|
33
20
|
};
|
|
34
21
|
} {
|
|
35
22
|
return {
|
|
36
|
-
context,
|
|
37
|
-
events: createEventSchemas(events),
|
|
38
23
|
types: {} as any,
|
|
24
|
+
schemas: createZodEventSchemas(events),
|
|
39
25
|
};
|
|
40
26
|
}
|
package/src/utils.ts
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import { AnyMachineSnapshot, AnyStateNode, Prop, Values } from 'xstate';
|
|
2
2
|
import { FromSchema } from 'json-schema-to-ts';
|
|
3
3
|
import { JSONSchema7 } from 'json-schema-to-ts/lib/types/definitions';
|
|
4
|
+
import zodToJsonSchema, { JsonSchema7Type } from 'zod-to-json-schema';
|
|
5
|
+
import { ZodEventTypes } from './schemas';
|
|
6
|
+
import { z } from 'zod';
|
|
4
7
|
|
|
5
8
|
export function getAllTransitions(state: AnyMachineSnapshot) {
|
|
6
9
|
const nodes = state._nodes;
|
|
@@ -15,7 +18,7 @@ export type EventSchemas = {
|
|
|
15
18
|
[key: string]: {
|
|
16
19
|
description?: string;
|
|
17
20
|
properties?: {
|
|
18
|
-
[key: string]:
|
|
21
|
+
[key: string]: JsonSchema7Type;
|
|
19
22
|
};
|
|
20
23
|
};
|
|
21
24
|
};
|
|
@@ -55,5 +58,24 @@ export function createEventSchemas<T extends EventSchemas>(
|
|
|
55
58
|
return resolvedEventSchemaMap as ConvertToJSONSchemas<T>;
|
|
56
59
|
}
|
|
57
60
|
|
|
61
|
+
export function createZodEventSchemas<T extends ZodEventTypes>(
|
|
62
|
+
eventSchemaMap: T
|
|
63
|
+
): {
|
|
64
|
+
[K in keyof T]: unknown;
|
|
65
|
+
} {
|
|
66
|
+
const resolvedEventSchemaMap = {};
|
|
67
|
+
|
|
68
|
+
for (const [eventType, zodType] of Object.entries(eventSchemaMap)) {
|
|
69
|
+
// @ts-ignore
|
|
70
|
+
resolvedEventSchemaMap[eventType] = zodToJsonSchema(
|
|
71
|
+
zodType.extend({
|
|
72
|
+
type: z.literal(eventType),
|
|
73
|
+
})
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return resolvedEventSchemaMap as ConvertToJSONSchemas<T>;
|
|
78
|
+
}
|
|
79
|
+
|
|
58
80
|
export type InferEventsFromSchemas<T extends ConvertToJSONSchemas<any>> =
|
|
59
81
|
FromSchema<Values<T>>;
|