mirror of
https://github.com/aljazceru/opencode.git
synced 2025-12-21 17:54:23 +01:00
45 lines
1.3 KiB
TypeScript
45 lines
1.3 KiB
TypeScript
import { App } from "../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" });
|
|
|
|
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 [type, sessionID] = splits;
|
|
const session = await Session.get(sessionID);
|
|
if (!session.shareID) return;
|
|
await fetch(`${URL}/share_sync`, {
|
|
method: "POST",
|
|
body: JSON.stringify({
|
|
sessionID: sessionID,
|
|
shareID: session.shareID,
|
|
key: payload.properties.key,
|
|
content: payload.properties.content,
|
|
}),
|
|
})
|
|
.then((x) => x.text())
|
|
.then(console.log);
|
|
});
|
|
});
|
|
|
|
export async function init() {
|
|
await state();
|
|
}
|
|
|
|
const URL = "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);
|
|
}
|
|
}
|