Skip to content

Commit 49a31d7

Browse files
committed
fix(session): include session id on v2 events
1 parent 5c3d206 commit 49a31d7

3 files changed

Lines changed: 109 additions & 61 deletions

File tree

packages/opencode/src/v2/event.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { Identifier } from "@/id/id"
2+
import { SyncEvent } from "@/sync"
3+
import { withStatics } from "@/util/schema"
4+
import * as Schema from "effect/Schema"
5+
6+
export const ID = Schema.String.pipe(
7+
Schema.brand("Event.ID"),
8+
withStatics((s) => ({
9+
create: () => s.make(Identifier.create("evt", "ascending")),
10+
})),
11+
)
12+
export type ID = Schema.Schema.Type<typeof ID>
13+
14+
export function define<const Type extends string, Fields extends Schema.Struct.Fields>(input: {
15+
type: Type
16+
schema: Fields
17+
aggregate: string
18+
version?: number
19+
}) {
20+
const Event = Schema.Struct({
21+
id: ID,
22+
metadata: Schema.Record(Schema.String, Schema.Unknown).pipe(Schema.optional),
23+
timestamp: Schema.DateTimeUtc,
24+
type: Schema.Literal(input.type),
25+
version: Schema.Number.pipe(Schema.optional),
26+
...input.schema,
27+
}).annotate({
28+
identifier: input.type,
29+
})
30+
31+
const Sync = SyncEvent.define({
32+
type: input.type,
33+
version: input.version ?? 1,
34+
aggregate: input.aggregate,
35+
schema: Event,
36+
})
37+
38+
return Object.assign(Event, { Sync })
39+
}
40+
41+
export * as Event from "./event"

packages/opencode/src/v2/session-entry.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { Schema } from "effect"
22
import { Prompt } from "./session-prompt"
33
import { SessionEvent } from "./session-event"
4+
import { Event } from "./event"
45

5-
export const ID = SessionEvent.ID
6+
export const ID = Event.ID
67
export type ID = Schema.Schema.Type<typeof ID>
78

89
const Base = {
9-
id: SessionEvent.ID,
10+
id: ID,
1011
metadata: Schema.Record(Schema.String, Schema.Unknown).pipe(Schema.optional),
1112
time: Schema.Struct({
1213
created: Schema.DateTimeUtc,

packages/opencode/src/v2/session-event.ts

Lines changed: 65 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,12 @@
1-
import { Identifier } from "@/id/id"
2-
import { FileAttachment, Prompt } from "./session-prompt"
31
import { SessionID } from "@/session/schema"
4-
import { SyncEvent } from "@/sync"
5-
import { withStatics } from "@/util/schema"
2+
import { Event as BaseEvent } from "./event"
3+
import { FileAttachment, Prompt } from "./session-prompt"
64
import { Schema } from "effect"
75
export { FileAttachment }
86

9-
export const ID = Schema.String.pipe(
10-
Schema.brand("Session.Event.ID"),
11-
withStatics((s) => ({
12-
create: () => s.make(Identifier.create("evt", "ascending")),
13-
})),
14-
)
7+
export const ID = BaseEvent.ID
158
export type ID = Schema.Schema.Type<typeof ID>
169

17-
function defineEvent<const Type extends string, Fields extends Schema.Struct.Fields>(input: {
18-
type: Type
19-
schema: Fields
20-
version?: number
21-
}) {
22-
const Event = Schema.Struct({
23-
id: ID,
24-
sessionID: SessionID,
25-
metadata: Schema.Record(Schema.String, Schema.Unknown).pipe(Schema.optional),
26-
timestamp: Schema.DateTimeUtc,
27-
type: Schema.Literal(input.type),
28-
version: Schema.Number.pipe(Schema.optional),
29-
...input.schema,
30-
}).annotate({
31-
identifier: input.type,
32-
})
33-
34-
const Sync = SyncEvent.define({
35-
type: input.type,
36-
version: input.version ?? 1,
37-
aggregate: "sessionID",
38-
schema: Event,
39-
})
40-
41-
return Object.assign(Event, {
42-
Sync,
43-
})
44-
}
45-
4610
export const Source = Schema.Struct({
4711
start: Schema.Number,
4812
end: Schema.Number,
@@ -52,26 +16,36 @@ export const Source = Schema.Struct({
5216
})
5317
export type Source = Schema.Schema.Type<typeof Source>
5418

55-
export const Prompted = defineEvent({
19+
const Base = {
20+
sessionID: SessionID,
21+
}
22+
23+
export const Prompted = BaseEvent.define({
5624
type: "session.prompted",
25+
aggregate: "sessionID",
5726
schema: {
27+
...Base,
5828
prompt: Prompt,
5929
},
6030
})
6131
export type Prompted = Schema.Schema.Type<typeof Prompted>
6232

63-
export const Synthetic = defineEvent({
33+
export const Synthetic = BaseEvent.define({
6434
type: "session.synthetic",
35+
aggregate: "sessionID",
6536
schema: {
37+
...Base,
6638
text: Schema.String,
6739
},
6840
})
6941
export type Synthetic = Schema.Schema.Type<typeof Synthetic>
7042

7143
export namespace Step {
72-
export const Started = defineEvent({
44+
export const Started = BaseEvent.define({
7345
type: "session.step.started",
46+
aggregate: "sessionID",
7447
schema: {
48+
...Base,
7549
model: Schema.Struct({
7650
id: Schema.String,
7751
providerID: Schema.String,
@@ -81,9 +55,11 @@ export namespace Step {
8155
})
8256
export type Started = Schema.Schema.Type<typeof Started>
8357

84-
export const Ended = defineEvent({
58+
export const Ended = BaseEvent.define({
8559
type: "session.step.ended",
60+
aggregate: "sessionID",
8661
schema: {
62+
...Base,
8763
reason: Schema.String,
8864
cost: Schema.Number,
8965
tokens: Schema.Struct({
@@ -101,47 +77,61 @@ export namespace Step {
10177
}
10278

10379
export namespace Text {
104-
export const Started = defineEvent({
80+
export const Started = BaseEvent.define({
10581
type: "session.text.started",
106-
schema: {},
82+
aggregate: "sessionID",
83+
schema: {
84+
...Base,
85+
},
10786
})
10887
export type Started = Schema.Schema.Type<typeof Started>
10988

110-
export const Delta = defineEvent({
89+
export const Delta = BaseEvent.define({
11190
type: "session.text.delta",
91+
aggregate: "sessionID",
11292
schema: {
93+
...Base,
11394
delta: Schema.String,
11495
},
11596
})
11697
export type Delta = Schema.Schema.Type<typeof Delta>
11798

118-
export const Ended = defineEvent({
99+
export const Ended = BaseEvent.define({
119100
type: "session.text.ended",
101+
aggregate: "sessionID",
120102
schema: {
103+
...Base,
121104
text: Schema.String,
122105
},
123106
})
124107
export type Ended = Schema.Schema.Type<typeof Ended>
125108
}
126109

127110
export namespace Reasoning {
128-
export const Started = defineEvent({
111+
export const Started = BaseEvent.define({
129112
type: "session.reasoning.started",
130-
schema: {},
113+
aggregate: "sessionID",
114+
schema: {
115+
...Base,
116+
},
131117
})
132118
export type Started = Schema.Schema.Type<typeof Started>
133119

134-
export const Delta = defineEvent({
120+
export const Delta = BaseEvent.define({
135121
type: "session.reasoning.delta",
122+
aggregate: "sessionID",
136123
schema: {
124+
...Base,
137125
delta: Schema.String,
138126
},
139127
})
140128
export type Delta = Schema.Schema.Type<typeof Delta>
141129

142-
export const Ended = defineEvent({
130+
export const Ended = BaseEvent.define({
143131
type: "session.reasoning.ended",
132+
aggregate: "sessionID",
144133
schema: {
134+
...Base,
145135
text: Schema.String,
146136
},
147137
})
@@ -150,37 +140,45 @@ export namespace Reasoning {
150140

151141
export namespace Tool {
152142
export namespace Input {
153-
export const Started = defineEvent({
143+
export const Started = BaseEvent.define({
154144
type: "session.tool.input.started",
145+
aggregate: "sessionID",
155146
schema: {
147+
...Base,
156148
callID: Schema.String,
157149
name: Schema.String,
158150
},
159151
})
160152
export type Started = Schema.Schema.Type<typeof Started>
161153

162-
export const Delta = defineEvent({
154+
export const Delta = BaseEvent.define({
163155
type: "session.tool.input.delta",
156+
aggregate: "sessionID",
164157
schema: {
158+
...Base,
165159
callID: Schema.String,
166160
delta: Schema.String,
167161
},
168162
})
169163
export type Delta = Schema.Schema.Type<typeof Delta>
170164

171-
export const Ended = defineEvent({
165+
export const Ended = BaseEvent.define({
172166
type: "session.tool.input.ended",
167+
aggregate: "sessionID",
173168
schema: {
169+
...Base,
174170
callID: Schema.String,
175171
text: Schema.String,
176172
},
177173
})
178174
export type Ended = Schema.Schema.Type<typeof Ended>
179175
}
180176

181-
export const Called = defineEvent({
177+
export const Called = BaseEvent.define({
182178
type: "session.tool.called",
179+
aggregate: "sessionID",
183180
schema: {
181+
...Base,
184182
callID: Schema.String,
185183
tool: Schema.String,
186184
input: Schema.Record(Schema.String, Schema.Unknown),
@@ -192,9 +190,11 @@ export namespace Tool {
192190
})
193191
export type Called = Schema.Schema.Type<typeof Called>
194192

195-
export const Success = defineEvent({
193+
export const Success = BaseEvent.define({
196194
type: "session.tool.success",
195+
aggregate: "sessionID",
197196
schema: {
197+
...Base,
198198
callID: Schema.String,
199199
title: Schema.String,
200200
output: Schema.String.pipe(Schema.optional),
@@ -207,9 +207,11 @@ export namespace Tool {
207207
})
208208
export type Success = Schema.Schema.Type<typeof Success>
209209

210-
export const Error = defineEvent({
210+
export const Error = BaseEvent.define({
211211
type: "session.tool.error",
212+
aggregate: "sessionID",
212213
schema: {
214+
...Base,
213215
callID: Schema.String,
214216
error: Schema.String,
215217
provider: Schema.Struct({
@@ -233,18 +235,22 @@ export const RetryError = Schema.Struct({
233235
})
234236
export type RetryError = Schema.Schema.Type<typeof RetryError>
235237

236-
export const Retried = defineEvent({
238+
export const Retried = BaseEvent.define({
237239
type: "session.retried",
240+
aggregate: "sessionID",
238241
schema: {
242+
...Base,
239243
attempt: Schema.Number,
240244
error: RetryError,
241245
},
242246
})
243247
export type Retried = Schema.Schema.Type<typeof Retried>
244248

245-
export const Compacted = defineEvent({
249+
export const Compacted = BaseEvent.define({
246250
type: "session.compacted",
251+
aggregate: "sessionID",
247252
schema: {
253+
...Base,
248254
auto: Schema.Boolean,
249255
overflow: Schema.Boolean.pipe(Schema.optional),
250256
},

0 commit comments

Comments
 (0)