Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 35 additions & 19 deletions src/plugin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@ function serverAuthHeaders(): HeadersInit | undefined {
return { Authorization: `Basic ${btoa(`${username}:${password}`)}` }
}

async function listGlobalSessions(serverUrl: URL): Promise<unknown[]> {
export async function listGlobalSessions(serverUrl: URL): Promise<unknown[]> {
const sessions: unknown[] = []
let cursor: string | undefined

do {
const url = new URL("/experimental/session", serverUrl)
url.searchParams.set("archived", "true")
url.searchParams.set("limit", "100")
if (cursor) url.searchParams.set("cursor", cursor)
const response = await fetch(url, { headers: serverAuthHeaders() })
Expand All @@ -50,17 +51,39 @@ async function listGlobalSessions(serverUrl: URL): Promise<unknown[]> {
return sessions
}

export default (async ({ client, project, directory, serverUrl }) => {
const transport = (
client as unknown as {
_client: {
get(options: {
url: string
query: { limit: number; cursor?: number }
}): Promise<{ data?: unknown; error?: unknown; response: Response }>
}
type GlobalSessionTransport = {
get(options: {
url: string
query: { archived: true; limit: number; cursor?: number }
}): Promise<{ data?: unknown; error?: unknown; response: Response }>
}

export async function listGlobalSessionsWithTransport(
transport: GlobalSessionTransport,
): Promise<unknown[]> {
const sessions: unknown[] = []
let cursor: number | undefined

do {
const result = await transport.get({
url: "/experimental/session",
query: { archived: true, limit: 100, cursor },
})
if (result.error || !Array.isArray(result.data)) {
throw new Error("Global session request failed")
}
)._client
sessions.push(...result.data)

const nextCursor = result.response.headers.get("x-next-cursor")
cursor = nextCursor ? Number(nextCursor) : undefined
} while (cursor !== undefined && Number.isFinite(cursor))

return sessions
}

export default (async ({ client, project, directory, serverUrl }) => {
const transport = (client as unknown as { _client: GlobalSessionTransport })
._client
const source: SourceIdentity = {
processInstanceId,
pluginInstanceId: crypto.randomUUID(),
Expand Down Expand Up @@ -133,14 +156,7 @@ export default (async ({ client, project, directory, serverUrl }) => {
try {
globalResult = await listGlobalSessions(serverUrl)
} catch {
const result = await transport.get({
url: "/experimental/session",
query: { limit: 100 },
})
if (result.error || !Array.isArray(result.data)) {
throw new Error("Global session request failed")
}
globalResult = result.data
globalResult = await listGlobalSessionsWithTransport(transport)
}

for (const info of globalResult) {
Expand Down
45 changes: 39 additions & 6 deletions src/tui/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import type { TuiPluginApi, TuiPluginModule } from "@opencode-ai/plugin/tui";
import {
createOpencodeClient,
type OpencodeClient,
type OpencodeClientConfig,
} from "@opencode-ai/sdk/v2/client";
import path from "node:path";
import { createServer } from "../collector/server";
import { sanitizeSession } from "../plugin/normalize";
Expand Down Expand Up @@ -42,12 +47,31 @@ async function ensureCollector() {
}
}

async function sendGlobalSnapshot(api: TuiPluginApi, source: SourceIdentity) {
export function createGlobalSessionClient(client: TuiPluginApi["client"]) {
const config = (
client as unknown as {
client: { getConfig(): OpencodeClientConfig };
}
).client.getConfig();
const headers = new Headers(config.headers as HeadersInit | undefined);
headers.delete("x-opencode-directory");
headers.delete("x-opencode-workspace");

return createOpencodeClient({
...config,
directory: undefined,
experimental_workspaceID: undefined,
headers,
});
}

export async function listGlobalSessions(client: OpencodeClient) {
const sessions: SnapshotSession[] = [];
let cursor: number | undefined;

do {
const result = await api.client.experimental.session.list({
const result = await client.experimental.session.list({
archived: true,
limit: 100,
cursor,
});
Expand All @@ -62,16 +86,25 @@ async function sendGlobalSnapshot(api: TuiPluginApi, source: SourceIdentity) {
cursor = nextCursor ? Number(nextCursor) : undefined;
} while (cursor !== undefined && Number.isFinite(cursor));

await sender.sendSnapshot({ source, scope: "global", sessions });
return sessions;
}

async function sendGlobalSnapshot(api: TuiPluginApi, source: SourceIdentity) {
await sender.sendSnapshot({
source,
scope: "global",
sessions: await listGlobalSessions(createGlobalSessionClient(api.client)),
});
}

function openDashboard() {
const url = `${dashboardUrl}/?v=${Date.now()}`;
const command =
process.platform === "darwin"
? ["open", dashboardUrl]
? ["open", url]
: process.platform === "win32"
? ["cmd", "/c", "start", "", dashboardUrl]
: ["xdg-open", dashboardUrl];
? ["cmd", "/c", "start", "", url]
: ["xdg-open", url];
const subprocess = Bun.spawn(command, {
stdin: "ignore",
stdout: "ignore",
Expand Down
Loading
Loading