mirror of
https://gitea.toothfairyai.com/ToothFairyAI/tf_code.git
synced 2026-03-31 14:22:27 +00:00
This release has a bunch of minor breaking changes if you are using opencode plugins or sdk 1. storage events have been removed (we might bring this back but had some issues) 2. concept of `app` is gone - there is a new concept called `project` and endpoints to list projects and get the current project 3. plugin receives `directory` which is cwd and `worktree` which is where the root of the project is if it's a git repo 4. the session.chat function has been renamed to session.prompt in sdk. it no longer requires model to be passed in (model is now an object) 5. every endpoint takes an optional `directory` parameter to operate as though opencode is running in that directory
88 lines
2.5 KiB
TypeScript
88 lines
2.5 KiB
TypeScript
import { Bus } from "../bus"
|
|
import { Installation } from "../installation"
|
|
import { Session } from "../session"
|
|
import { MessageV2 } from "../session/message-v2"
|
|
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>()
|
|
|
|
export async function sync(key: string, content: any) {
|
|
const [root, ...splits] = key.split("/")
|
|
if (root !== "session") return
|
|
const [sub, sessionID] = splits
|
|
if (sub === "share") return
|
|
const share = await Session.getShare(sessionID).catch(() => {})
|
|
if (!share) return
|
|
const { secret } = share
|
|
pending.set(key, 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,
|
|
secret,
|
|
key: key,
|
|
content,
|
|
}),
|
|
})
|
|
})
|
|
.then((x) => {
|
|
if (x) {
|
|
log.info("synced", {
|
|
key: key,
|
|
status: x.status,
|
|
})
|
|
}
|
|
})
|
|
}
|
|
|
|
export function init() {
|
|
Bus.subscribe(Session.Event.Updated, async (evt) => {
|
|
await sync("session/info/" + evt.properties.info.id, evt.properties.info)
|
|
})
|
|
Bus.subscribe(MessageV2.Event.Updated, async (evt) => {
|
|
await sync("session/message/" + evt.properties.info.sessionID + "/" + evt.properties.info.id, evt.properties.info)
|
|
})
|
|
Bus.subscribe(MessageV2.Event.PartUpdated, async (evt) => {
|
|
await sync(
|
|
"session/part/" +
|
|
evt.properties.part.sessionID +
|
|
"/" +
|
|
evt.properties.part.messageID +
|
|
"/" +
|
|
evt.properties.part.id,
|
|
evt.properties.part,
|
|
)
|
|
})
|
|
}
|
|
|
|
export const URL =
|
|
process.env["OPENCODE_API"] ??
|
|
(Installation.isSnapshot() || Installation.isDev() ? "https://api.dev.opencode.ai" : "https://api.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 as { url: string; secret: string })
|
|
}
|
|
|
|
export async function remove(sessionID: string, secret: string) {
|
|
return fetch(`${URL}/share_delete`, {
|
|
method: "POST",
|
|
body: JSON.stringify({ sessionID, secret }),
|
|
}).then((x) => x.json())
|
|
}
|
|
}
|