Skip to content

Commit 2aa6110

Browse files
committed
ignore: exploration
1 parent 8b9b9ad commit 2aa6110

File tree

2 files changed

+144
-26
lines changed

2 files changed

+144
-26
lines changed

packages/opencode/src/v2/message.ts

Lines changed: 73 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,59 +10,106 @@ export namespace Message {
1010
})),
1111
)
1212

13-
export class File extends Schema.Class<File>("Message.File")({
14-
url: Schema.String,
13+
export class Source extends Schema.Class<Source>("Message.Source")({
14+
start: Schema.Number,
15+
end: Schema.Number,
16+
text: Schema.String,
17+
}) {}
18+
19+
export class FileAttachment extends Schema.Class<FileAttachment>("Message.File.Attachment")({
20+
uri: Schema.String,
1521
mime: Schema.String,
22+
name: Schema.String.pipe(Schema.optional),
23+
description: Schema.String.pipe(Schema.optional),
24+
source: Source.pipe(Schema.optional),
1625
}) {
1726
static create(url: string) {
18-
return new File({
19-
url,
27+
return new FileAttachment({
28+
uri: url,
2029
mime: "text/plain",
2130
})
2231
}
2332
}
2433

25-
export class UserContent extends Schema.Class<UserContent>("Message.User.Content")({
26-
text: Schema.String,
27-
synthetic: Schema.Boolean.pipe(Schema.optional),
28-
agent: Schema.String.pipe(Schema.optional),
29-
files: Schema.Array(File).pipe(Schema.optional),
34+
export class AgentAttachment extends Schema.Class<AgentAttachment>("Message.Agent.Attachment")({
35+
name: Schema.String,
36+
source: Source.pipe(Schema.optional),
3037
}) {}
3138

3239
export class User extends Schema.Class<User>("Message.User")({
3340
id: ID,
3441
type: Schema.Literal("user"),
42+
text: Schema.String,
43+
files: Schema.Array(FileAttachment).pipe(Schema.optional),
44+
agents: Schema.Array(AgentAttachment).pipe(Schema.optional),
3545
time: Schema.Struct({
3646
created: Schema.DateTimeUtc,
3747
}),
38-
content: UserContent,
3948
}) {
40-
static create(content: Schema.Schema.Type<typeof UserContent>) {
49+
static create(input: { text: User["text"]; files?: User["files"]; agents?: User["agents"] }) {
4150
const msg = new User({
4251
id: ID.create(),
4352
type: "user",
53+
...input,
4454
time: {
4555
created: Effect.runSync(DateTime.now),
4656
},
47-
content,
4857
})
4958
return msg
5059
}
51-
52-
static file(url: string) {
53-
return new File({
54-
url,
55-
mime: "text/plain",
56-
})
57-
}
5860
}
5961

60-
export namespace User {}
61-
}
62+
export class Synthetic extends Schema.Class<Synthetic>("Message.Synthetic")({
63+
id: ID,
64+
type: Schema.Literal("synthetic"),
65+
text: Schema.String,
66+
time: Schema.Struct({
67+
created: Schema.DateTimeUtc,
68+
}),
69+
}) {}
6270

63-
const msg = Message.User.create({
64-
text: "Hello world",
65-
files: [Message.File.create("file://example.com/file.txt")],
66-
})
71+
export class Request extends Schema.Class<Request>("Message.Request")({
72+
id: ID,
73+
type: Schema.Literal("start"),
74+
model: Schema.Struct({
75+
id: Schema.String,
76+
providerID: Schema.String,
77+
variant: Schema.String.pipe(Schema.optional),
78+
}),
79+
time: Schema.Struct({
80+
created: Schema.DateTimeUtc,
81+
}),
82+
}) {}
83+
84+
export class Text extends Schema.Class<Text>("Message.Text")({
85+
id: ID,
86+
type: Schema.Literal("text"),
87+
text: Schema.String,
88+
time: Schema.Struct({
89+
created: Schema.DateTimeUtc,
90+
completed: Schema.DateTimeUtc.pipe(Schema.optional),
91+
}),
92+
}) {}
6793

68-
console.log(JSON.stringify(msg, null, 2))
94+
export class Complete extends Schema.Class<Complete>("Message.Complete")({
95+
id: ID,
96+
type: Schema.Literal("complete"),
97+
time: Schema.Struct({
98+
created: Schema.DateTimeUtc,
99+
}),
100+
cost: Schema.Number,
101+
tokens: Schema.Struct({
102+
total: Schema.Number,
103+
input: Schema.Number,
104+
output: Schema.Number,
105+
reasoning: Schema.Number,
106+
cache: Schema.Struct({
107+
read: Schema.Number,
108+
write: Schema.Number,
109+
}),
110+
}),
111+
}) {}
112+
113+
export const Info = Schema.Union([User, Text])
114+
export type Info = Schema.Schema.Type<typeof Info>
115+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import { Context, Layer, Schema, Effect } from "effect"
2+
import { Message } from "./message"
3+
import { Struct } from "effect"
4+
import { Identifier } from "@/id/id"
5+
import { withStatics } from "@/util/schema"
6+
import { Session } from "@/session"
7+
import { SessionID } from "@/session/schema"
8+
9+
export namespace SessionV2 {
10+
export const ID = SessionID
11+
12+
export type ID = Schema.Schema.Type<typeof ID>
13+
14+
export class PromptInput extends Schema.Class<PromptInput>("Session.PromptInput")({
15+
...Struct.omit(Message.User.fields, ["time", "type"]),
16+
id: Schema.optionalKey(Message.ID),
17+
sessionID: SessionV2.ID,
18+
}) {}
19+
20+
export class CreateInput extends Schema.Class<CreateInput>("Session.CreateInput")({
21+
id: Schema.optionalKey(SessionV2.ID),
22+
}) {}
23+
24+
export class Info extends Schema.Class<Info>("Session.Info")({
25+
id: SessionV2.ID,
26+
model: Schema.Struct({
27+
id: Schema.String,
28+
providerID: Schema.String,
29+
modelID: Schema.String,
30+
}).pipe(Schema.optional),
31+
}) {}
32+
33+
export interface Interface {
34+
fromID: (id: SessionV2.ID) => Effect.Effect<Info>
35+
create: (input: CreateInput) => Effect.Effect<Info>
36+
prompt: (input: PromptInput) => Effect.Effect<Message.User>
37+
}
38+
39+
export class Service extends Context.Service<Service, Interface>()("Session.Service") {}
40+
41+
export const layer = Layer.effect(Service)(
42+
Effect.gen(function* () {
43+
const session = yield* Session.Service
44+
45+
const create: Interface["create"] = Effect.fn("Session.create")(function* (input) {
46+
throw new Error("Not implemented")
47+
})
48+
49+
const prompt: Interface["prompt"] = Effect.fn("Session.prompt")(function* (input) {
50+
throw new Error("Not implemented")
51+
})
52+
53+
const fromID: Interface["fromID"] = Effect.fn("Session.fromID")(function* (id) {
54+
const match = yield* session.get(id)
55+
return fromV1(match)
56+
})
57+
58+
return Service.of({
59+
create,
60+
prompt,
61+
fromID,
62+
})
63+
}),
64+
)
65+
66+
function fromV1(input: Session.Info): Info {
67+
return new Info({
68+
id: SessionV2.ID.make(input.id),
69+
})
70+
}
71+
}

0 commit comments

Comments
 (0)