From 3718b2636e58af06bdb8abb331267a731b616adc Mon Sep 17 00:00:00 2001 From: QQ Date: Sat, 14 Nov 2026 14:53:07 +0800 Subject: [PATCH] Import packages/plugin/src/client-workspace-queries.ts --- .../plugin/src/client-workspace-queries.ts | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 packages/plugin/src/client-workspace-queries.ts diff --git a/packages/plugin/src/client-workspace-queries.ts b/packages/plugin/src/client-workspace-queries.ts new file mode 100644 index 0000000..f9728bd --- /dev/null +++ b/packages/plugin/src/client-workspace-queries.ts @@ -0,0 +1,45 @@ +import type { RemoteWorkspaceView } from './client-runtime-contract.js' +import { ClientModeError, isRecord } from './client-runtime-contract.js' +import { RemoteTypertGateway } from './remote-typert-gateway.js' + +export async function readRemoteWorkspaceBaseline( + gateway: RemoteTypertGateway, + signal?: AbortSignal, +): Promise { + const lifetime = new AbortController() + const activeSignal = signal === undefined + ? lifetime.signal + : AbortSignal.any([signal, lifetime.signal]) + const source = await gateway.open('workspace/follow', { args: {} }, activeSignal) + const iterator = source[Symbol.asyncIterator]() + try { + const first = await iterator.next() + if (first.done || !isRecord(first.value) || first.value.type !== 'baseline' + || !isRecord(first.value.value) || !Array.isArray(first.value.value.items)) { + throw new ClientModeError('INVALID_MESSAGE', 'The remote Host returned an invalid Workspace baseline.') + } + return first.value.value.items.map((item: unknown) => { + if (!isRecord(item) || typeof item.workspaceId !== 'string' + || typeof item.path !== 'string' || typeof item.title !== 'string') { + throw new ClientModeError('INVALID_MESSAGE', 'The remote Host returned an invalid Workspace row.') + } + return { workspaceId: item.workspaceId, path: item.path, title: item.title } + }) + } finally { + lifetime.abort('workspace-baseline-read') + await iterator.return?.() + } +} + +export function workspaceRecordId(value: unknown): string { + if (typeof value !== 'object' || value === null || !('workspaceId' in value) + || typeof value.workspaceId !== 'string' || value.workspaceId.length === 0) { + throw new ClientModeError('INVALID_MESSAGE', 'The remote Host returned an invalid Workspace.') + } + return value.workspaceId +} + +export function remoteWorkspaceTitle(path: string): string { + const normalized = path.replace(/[\\/]+$/u, '') + return normalized.split(/[\\/]+/u).filter(Boolean).at(-1) ?? path +}