fix(server): isolate event schema types
This commit is contained in:
parent
1b91b5df34
commit
b13a2d712a
2 changed files with 20 additions and 23 deletions
|
|
@ -19,11 +19,6 @@ export const ID = Schema.String.check(Schema.isStartsWith("evt_")).pipe(
|
||||||
)
|
)
|
||||||
export type ID = typeof ID.Type
|
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> = {
|
export type Definition<Type extends string = string, DataSchema extends Schema.Top = Schema.Top> = {
|
||||||
readonly type: Type
|
readonly type: Type
|
||||||
readonly durable?: {
|
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 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> = {
|
export type Payload<D extends Definition = Definition> = {
|
||||||
readonly id: ID
|
readonly id: ID
|
||||||
readonly type: D["type"]
|
readonly type: D["type"]
|
||||||
|
|
@ -80,10 +66,10 @@ export function versionedType(type: string, version: number) {
|
||||||
return `${type}.${version}`
|
return `${type}.${version}`
|
||||||
}
|
}
|
||||||
|
|
||||||
export const registry = new Map<string, Definition<string, ServiceFreeSchema>>()
|
export const registry = new Map<string, Definition>()
|
||||||
const durableRegistry = new Map<string, Definition<string, ServiceFreeSchema>>()
|
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 type: Type
|
||||||
readonly durable?: {
|
readonly durable?: {
|
||||||
readonly version: number
|
readonly version: number
|
||||||
|
|
@ -92,13 +78,16 @@ export function define<const Type extends string, Fields extends Record<Property
|
||||||
readonly schema: Fields
|
readonly schema: Fields
|
||||||
}): Schema.Schema<Payload<Definition<Type, Schema.Struct<Fields>>>> & Definition<Type, Schema.Struct<Fields>> {
|
}): Schema.Schema<Payload<Definition<Type, Schema.Struct<Fields>>>> & Definition<Type, Schema.Struct<Fields>> {
|
||||||
const Data = Schema.Struct(input.schema)
|
const Data = Schema.Struct(input.schema)
|
||||||
const Event = Schema.Struct({
|
const Payload = Schema.Struct({
|
||||||
...Payload.fields,
|
id: ID,
|
||||||
|
metadata: Schema.optional(Schema.Record(Schema.String, Schema.Unknown)),
|
||||||
type: Schema.Literal(input.type),
|
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,
|
data: Data,
|
||||||
}).annotate({ identifier: input.type })
|
}).annotate({ identifier: input.type })
|
||||||
|
|
||||||
const definition = Object.assign(Event, {
|
const definition = Object.assign(Payload, {
|
||||||
type: input.type,
|
type: input.type,
|
||||||
...(input.durable === undefined ? {} : { durable: input.durable }),
|
...(input.durable === undefined ? {} : { durable: input.durable }),
|
||||||
data: Data,
|
data: Data,
|
||||||
|
|
|
||||||
|
|
@ -1,17 +1,25 @@
|
||||||
import { EventV2 } from "@opencode-ai/core/event"
|
import { EventV2 } from "@opencode-ai/core/event"
|
||||||
|
import { Location } from "@opencode-ai/core/location"
|
||||||
import { Schema } from "effect"
|
import { Schema } from "effect"
|
||||||
import { HttpApiEndpoint, HttpApiGroup, OpenApi } from "effect/unstable/httpapi"
|
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([
|
const Event = Schema.Union([
|
||||||
...EventV2.definitions().map((definition) =>
|
...EventV2.definitions().map((definition) =>
|
||||||
Schema.Struct({
|
Schema.Struct({
|
||||||
...EventV2.Payload.fields,
|
...fields,
|
||||||
type: Schema.Literal(definition.type),
|
type: Schema.Literal(definition.type),
|
||||||
data: definition.data,
|
data: definition.data as Schema.Struct<{}>,
|
||||||
}).annotate({ identifier: `V2Event.${definition.type}` }),
|
}).annotate({ identifier: `V2Event.${definition.type}` }),
|
||||||
),
|
),
|
||||||
Schema.Struct({
|
Schema.Struct({
|
||||||
...EventV2.Payload.fields,
|
...fields,
|
||||||
type: Schema.Literal("server.connected"),
|
type: Schema.Literal("server.connected"),
|
||||||
data: Schema.Struct({}),
|
data: Schema.Struct({}),
|
||||||
}).annotate({ identifier: "V2Event.server.connected" }),
|
}).annotate({ identifier: "V2Event.server.connected" }),
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue