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
|
import { App } from "../app/app";
import { Bus } from "../bus";
import { Session } from "../session/session";
import { Storage } from "../storage/storage";
import { Log } from "../util/log";
export namespace Share {
const log = Log.create({ service: "share" });
let queue: Promise<void> = Promise.resolve();
const pending = new Map<string, any>();
const state = App.state("share", async () => {
Bus.subscribe(Storage.Event.Write, async (payload) => {
const [root, ...splits] = payload.properties.key.split("/");
if (root !== "session") return;
const [, sessionID] = splits;
const session = await Session.get(sessionID);
if (!session.shareID) return;
const key = payload.properties.key;
pending.set(key, payload.properties.content);
queue = queue
.then(async () => {
const content = pending.get(key);
if (content === undefined) return;
pending.delete(key);
return fetch(`${URL}/share_sync`, {
method: "POST",
body: JSON.stringify({
sessionID: sessionID,
shareID: session.shareID,
key: key,
content,
}),
});
})
.then((x) => {
if (x) {
log.info("synced", {
key: key,
status: x.status,
});
}
});
});
});
export async function init() {
await state();
}
export const URL =
process.env["OPENCODE_API"] ?? "https://api.dev.opencode.ai";
export async function create(sessionID: string) {
return fetch(`${URL}/share_create`, {
method: "POST",
body: JSON.stringify({ sessionID: sessionID }),
})
.then((x) => x.json())
.then((x) => x.shareID);
}
}
|