feat(sdk): expose live event stream (#34098)
This commit is contained in:
parent
2c02f8bace
commit
42e6b7db32
19 changed files with 395 additions and 56 deletions
|
|
@ -3,7 +3,7 @@ import { EventManifest } from "@opencode-ai/schema/event-manifest"
|
|||
import { Location } from "@opencode-ai/schema/location"
|
||||
import type { Definition } from "@opencode-ai/schema/event"
|
||||
import { Schema } from "effect"
|
||||
import { HttpApiEndpoint, HttpApiGroup, OpenApi } from "effect/unstable/httpapi"
|
||||
import { HttpApiEndpoint, HttpApiGroup, HttpApiSchema, OpenApi } from "effect/unstable/httpapi"
|
||||
|
||||
const fields = {
|
||||
id: Event.ID,
|
||||
|
|
@ -12,15 +12,9 @@ const fields = {
|
|||
location: Schema.optional(Location.Ref),
|
||||
}
|
||||
|
||||
const schema = (definitions: ReadonlyArray<Definition>) =>
|
||||
const schema = <const Definitions extends ReadonlyArray<Definition>>(definitions: Definitions) =>
|
||||
Schema.Union([
|
||||
...definitions.map((definition) =>
|
||||
Schema.Struct({
|
||||
...fields,
|
||||
type: Schema.Literal(definition.type),
|
||||
data: definition.data,
|
||||
}).annotate({ identifier: `V2Event.${definition.type}` }),
|
||||
),
|
||||
...definitions,
|
||||
...(definitions.some((definition) => definition.type === "server.connected")
|
||||
? []
|
||||
: [
|
||||
|
|
@ -32,14 +26,14 @@ const schema = (definitions: ReadonlyArray<Definition>) =>
|
|||
]),
|
||||
]).annotate({ identifier: "V2Event" })
|
||||
|
||||
const make = (definitions: ReadonlyArray<Definition>) => {
|
||||
const make = <const Definitions extends ReadonlyArray<Definition>>(definitions: Definitions) => {
|
||||
const EventSchema = schema(definitions)
|
||||
return {
|
||||
schema: EventSchema,
|
||||
group: HttpApiGroup.make("server.event")
|
||||
.add(
|
||||
HttpApiEndpoint.get("event.subscribe", "/api/event", {
|
||||
success: EventSchema,
|
||||
success: HttpApiSchema.StreamSse({ data: EventSchema }),
|
||||
}).annotateMerge(
|
||||
OpenApi.annotations({
|
||||
identifier: "v2.event.subscribe",
|
||||
|
|
@ -52,8 +46,11 @@ const make = (definitions: ReadonlyArray<Definition>) => {
|
|||
}
|
||||
}
|
||||
|
||||
export const makeEventGroup = (definitions: ReadonlyArray<Definition>) => make(definitions).group
|
||||
export const makeEventGroup = <const Definitions extends ReadonlyArray<Definition>>(definitions: Definitions) =>
|
||||
make(definitions).group
|
||||
|
||||
const event = make(EventManifest.ServerDefinitions)
|
||||
export const EventGroup = event.group
|
||||
export type Event = typeof event.schema.Type
|
||||
export const OpenCodeEvent = event.schema
|
||||
export type OpenCodeEvent = typeof OpenCodeEvent.Type
|
||||
export type OpenCodeEventEncoded = typeof OpenCodeEvent.Encoded
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue