mirror of
https://github.com/aljazceru/opencode.git
synced 2025-12-28 21:24:19 +01:00
sync
This commit is contained in:
@@ -41,9 +41,7 @@ cli
|
||||
? await Session.get(options.session)
|
||||
: await Session.create();
|
||||
console.log("Session:", session.id);
|
||||
console.log(
|
||||
`Share: ${Share.URL.replace("api.", "")}/share?id=${session.id}`,
|
||||
);
|
||||
console.log(`Share: ${Share.URL.replace("api.", "")}/s?id=${session.id}`);
|
||||
|
||||
Bus.subscribe(Message.Event.Updated, async (message) => {
|
||||
console.log("Thinking...");
|
||||
|
||||
@@ -30,8 +30,17 @@ export namespace Session {
|
||||
export const Info = z
|
||||
.object({
|
||||
id: Identifier.schema("session"),
|
||||
shareID: z.string().optional(),
|
||||
share: z
|
||||
.object({
|
||||
secret: z.string(),
|
||||
url: z.string(),
|
||||
})
|
||||
.optional(),
|
||||
title: z.string(),
|
||||
time: z.object({
|
||||
created: z.number(),
|
||||
updated: z.number(),
|
||||
}),
|
||||
})
|
||||
.openapi({
|
||||
ref: "session.info",
|
||||
@@ -61,13 +70,17 @@ export namespace Session {
|
||||
const result: Info = {
|
||||
id: Identifier.descending("session"),
|
||||
title: "New Session - " + new Date().toISOString(),
|
||||
time: {
|
||||
created: Date.now(),
|
||||
updated: Date.now(),
|
||||
},
|
||||
};
|
||||
log.info("created", result);
|
||||
state().sessions.set(result.id, result);
|
||||
await Storage.writeJSON("session/info/" + result.id, result);
|
||||
share(result.id).then((shareID) => {
|
||||
share(result.id).then((share) => {
|
||||
update(result.id, (draft) => {
|
||||
draft.shareID = shareID;
|
||||
draft.share = share;
|
||||
});
|
||||
});
|
||||
Bus.publish(Event.Updated, {
|
||||
@@ -88,13 +101,13 @@ export namespace Session {
|
||||
|
||||
export async function share(id: string) {
|
||||
const session = await get(id);
|
||||
if (session.shareID) return session.shareID;
|
||||
const shareID = await Share.create(id);
|
||||
if (!shareID) return;
|
||||
if (session.share) return session.share;
|
||||
const share = await Share.create(id);
|
||||
console.log("share", share);
|
||||
await update(id, (draft) => {
|
||||
draft.shareID = shareID;
|
||||
draft.share = share;
|
||||
});
|
||||
return shareID as string;
|
||||
return share;
|
||||
}
|
||||
|
||||
export async function update(id: string, editor: (session: Info) => void) {
|
||||
@@ -102,6 +115,7 @@ export namespace Session {
|
||||
const session = await get(id);
|
||||
if (!session) return;
|
||||
editor(session);
|
||||
session.time.updated = Date.now();
|
||||
sessions.set(id, session);
|
||||
await Storage.writeJSON("session/info/" + id, session);
|
||||
Bus.publish(Event.Updated, {
|
||||
|
||||
@@ -16,7 +16,8 @@ export namespace Share {
|
||||
if (root !== "session") return;
|
||||
const [, sessionID] = splits;
|
||||
const session = await Session.get(sessionID);
|
||||
if (!session.shareID) return;
|
||||
if (!session.share) return;
|
||||
const { secret } = session.share;
|
||||
|
||||
const key = payload.properties.key;
|
||||
pending.set(key, payload.properties.content);
|
||||
@@ -31,7 +32,7 @@ export namespace Share {
|
||||
method: "POST",
|
||||
body: JSON.stringify({
|
||||
sessionID: sessionID,
|
||||
shareID: session.shareID,
|
||||
secret,
|
||||
key: key,
|
||||
content,
|
||||
}),
|
||||
@@ -61,6 +62,6 @@ export namespace Share {
|
||||
body: JSON.stringify({ sessionID: sessionID }),
|
||||
})
|
||||
.then((x) => x.json())
|
||||
.then((x) => x.shareID);
|
||||
.then((x) => x as { url: string; secret: string });
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user