@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/src/schemas.ts CHANGED
@@ -1,40 +1,26 @@
1
1
  import { Values } from 'xstate';
2
- import {
3
- ContextSchema,
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 function createSchemas<
11
- const TContextSchema extends ContextSchema,
12
- const TEventSchemas extends EventSchemas
13
- >({
14
- context,
15
- events,
16
- }: {
17
- /**
18
- * The JSON schema for the context object.
19
- *
20
- * Must be of `{ type: 'object' }`.
21
- */
22
- context?: TContextSchema;
23
- /**
24
- * An object mapping event types to each event object's JSON Schema.
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]: JSONSchema7;
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>>;