Переглянути джерело

fix(server): isolate event schema types

Dax Raad 2 місяців тому
батько
коміт
b13a2d712a
2 змінених файлів з 20 додано та 23 видалено
  1. 9 20
      packages/core/src/event.ts
  2. 11 3
      packages/server/src/groups/event.ts

+ 9 - 20
packages/core/src/event.ts

@@ -19,11 +19,6 @@ export const ID = Schema.String.check(Schema.isStartsWith("evt_")).pipe(
 )
 export type ID = typeof ID.Type
 
-type ServiceFreeSchema = Schema.Top & {
-  readonly DecodingServices: never
-  readonly EncodingServices: never
-}
-
 export type Definition<Type extends string = string, DataSchema extends Schema.Top = Schema.Top> = {
   readonly type: Type
   readonly durable?: {
@@ -35,15 +30,6 @@ export type Definition<Type extends string = string, DataSchema extends Schema.T
 
 export type Data<D extends Definition> = Schema.Schema.Type<D["data"]>
 
-export const Payload = Schema.Struct({
-  id: ID,
-  metadata: Schema.optional(Schema.Record(Schema.String, Schema.Unknown)),
-  type: Schema.String,
-  durable: Schema.optional(Schema.Struct({ aggregateID: Schema.String, seq: Schema.Int, version: Schema.Int })),
-  location: Schema.optional(Location.Ref),
-  data: Schema.Unknown,
-})
-
 export type Payload<D extends Definition = Definition> = {
   readonly id: ID
   readonly type: D["type"]
@@ -80,10 +66,10 @@ export function versionedType(type: string, version: number) {
   return `${type}.${version}`
 }
 
-export const registry = new Map<string, Definition<string, ServiceFreeSchema>>()
-const durableRegistry = new Map<string, Definition<string, ServiceFreeSchema>>()
+export const registry = new Map<string, Definition>()
+const durableRegistry = new Map<string, Definition>()
 
-export function define<const Type extends string, Fields extends Record<PropertyKey, ServiceFreeSchema>>(input: {
+export function define<const Type extends string, Fields extends Schema.Struct.Fields>(input: {
   readonly type: Type
   readonly durable?: {
     readonly version: number
@@ -92,13 +78,16 @@ export function define<const Type extends string, Fields extends Record<Property
   readonly schema: Fields
 }): Schema.Schema<Payload<Definition<Type, Schema.Struct<Fields>>>> & Definition<Type, Schema.Struct<Fields>> {
   const Data = Schema.Struct(input.schema)
-  const Event = Schema.Struct({
-    ...Payload.fields,
+  const Payload = Schema.Struct({
+    id: ID,
+    metadata: Schema.optional(Schema.Record(Schema.String, Schema.Unknown)),
     type: Schema.Literal(input.type),
+    durable: Schema.optional(Schema.Struct({ aggregateID: Schema.String, seq: Schema.Number, version: Schema.Number })),
+    location: Schema.optional(Location.Ref),
     data: Data,
   }).annotate({ identifier: input.type })
 
-  const definition = Object.assign(Event, {
+  const definition = Object.assign(Payload, {
     type: input.type,
     ...(input.durable === undefined ? {} : { durable: input.durable }),
     data: Data,

+ 11 - 3
packages/server/src/groups/event.ts

@@ -1,17 +1,25 @@
 import { EventV2 } from "@opencode-ai/core/event"
+import { Location } from "@opencode-ai/core/location"
 import { Schema } from "effect"
 import { HttpApiEndpoint, HttpApiGroup, OpenApi } from "effect/unstable/httpapi"
 
+const fields = {
+  id: EventV2.ID,
+  metadata: Schema.optional(Schema.Record(Schema.String, Schema.Unknown)),
+  durable: Schema.optional(Schema.Struct({ aggregateID: Schema.String, seq: Schema.Int, version: Schema.Int })),
+  location: Schema.optional(Location.Ref),
+}
+
 const Event = Schema.Union([
   ...EventV2.definitions().map((definition) =>
     Schema.Struct({
-      ...EventV2.Payload.fields,
+      ...fields,
       type: Schema.Literal(definition.type),
-      data: definition.data,
+      data: definition.data as Schema.Struct<{}>,
     }).annotate({ identifier: `V2Event.${definition.type}` }),
   ),
   Schema.Struct({
-    ...EventV2.Payload.fields,
+    ...fields,
     type: Schema.Literal("server.connected"),
     data: Schema.Struct({}),
   }).annotate({ identifier: "V2Event.server.connected" }),