1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
|
import path from "path";
import { z } from "zod";
import { App } from "../app/";
import { Identifier } from "../id/id";
import { LLM } from "../llm/llm";
import { Storage } from "../storage/storage";
import { Log } from "../util/log";
import {
convertToModelMessages,
streamText,
tool,
type TextUIPart,
type ToolInvocationUIPart,
type UIMessage,
} from "ai";
export namespace Session {
const log = Log.create({ service: "session" });
export interface Info {
id: string;
title: string;
}
const state = App.state("session", () => {
const sessions = new Map<string, Info>();
const messages = new Map<string, UIMessage[]>();
return {
sessions,
messages,
};
});
export async function create() {
const result: Info = {
id: Identifier.create("session"),
title: "New Session - " + new Date().toISOString(),
};
log.info("created", result);
await Storage.write(
"session/info/" + result.id + ".json",
JSON.stringify(result),
);
state().sessions.set(result.id, result);
return result;
}
export async function get(id: string) {
const result = state().sessions.get(id);
if (result) {
return result;
}
const read = JSON.parse(await Storage.readToString("session/info/" + id));
state().sessions.set(id, read);
return read;
}
export async function messages(sessionID: string) {
const result = state().messages.get(sessionID);
if (result) {
return result;
}
const read = JSON.parse(
await Storage.readToString(
"session/message/" + sessionID + ".json",
).catch(() => "[]"),
);
state().messages.set(sessionID, read);
return read;
}
export async function* list() {
try {
const result = await Storage.list("session/info");
for await (const item of result) {
yield path.basename(item.path, ".json");
}
} catch {
return;
}
}
export async function chat(sessionID: string, msg: UIMessage) {
const l = log.clone().tag("session", sessionID);
l.info("chatting");
const msgs = (await messages(sessionID)) ?? [
{
id: Identifier.create("message"),
role: "system",
parts: [
{
type: "text",
text: "You are a helpful assistant called opencode",
},
],
} as UIMessage,
];
msgs.push(msg);
state().messages.set(sessionID, msgs);
async function write() {
return Storage.write(
"session/message/" + sessionID + ".json",
JSON.stringify(msgs),
);
}
await write();
const model = await LLM.findModel("claude-3-7-sonnet-20250219");
const result = streamText({
messages: convertToModelMessages(msgs),
temperature: 0,
tools: {
test: tool({
id: "opencode.test" as const,
parameters: z.object({
feeling: z.string(),
}),
execute: async () => {
return `Hello`;
},
description: "call this tool to get a greeting",
}),
},
model,
});
const next: UIMessage = {
id: Identifier.create("message"),
role: "assistant",
parts: [],
};
msgs.push(next);
let text: TextUIPart | undefined;
const reader = result.toUIMessageStream().getReader();
while (true) {
const { done, value } = await reader.read();
if (done) break;
l.info("part", value);
switch (value.type) {
case "start":
break;
case "start-step":
next.parts.push({
type: "step-start",
});
break;
case "text":
if (!text) {
text = value;
next.parts.push(value);
break;
}
text.text += value.text;
break;
case "tool-call":
next.parts.push({
type: "tool-invocation",
toolInvocation: {
state: "call",
...value,
},
});
break;
case "tool-result":
const match = next.parts.find(
(p) =>
p.type === "tool-invocation" &&
p.toolInvocation.toolCallId === value.toolCallId,
) as ToolInvocationUIPart | undefined;
if (match) {
match.toolInvocation = {
...match.toolInvocation,
state: "result",
result: value.result,
};
await write();
}
break;
case "finish":
await write();
break;
case "finish-step":
await write();
break;
default:
l.info("unhandled", {
type: value.type,
});
}
}
}
}
|