mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-11-19 22:43:38 +00:00
* 📎 feat: Copy Chat Attachments into Attached Workspaces A chat on an attached workspace now copies the user's own attachments into .librechat/attachments/ before the model runs, through the worker's import_file operation, and tells the model where they are. A worker without the operation, a deny file-write policy, or a copy that fails or runs past its bound leaves a plain note saying the file is not on this machine and why. * 🧭 fix: Scope Attachment Records by Tenant and Prime Late Subagents Match the tenant when recording a workspace copy, require write_file so the folder can always ignore itself, key copies by the live Code API route and worker, keep a digest when a copy fails so it is not hashed again, stop waiting on bookkeeping at the deadline, refuse to start on a cancelled turn, prime lazily initialized subagents through one per-turn primer, and make the budget configurable as endpoints.agents.workspaceAttachments.timeoutMs. * 🙈 fix: Copy Attachments Only Where the Folder Ignores Itself Gate every copy on .librechat/attachments/.gitignore ignoring the whole folder: create it with *, and accept an existing one only when it already ignores everything, otherwise report the file as unavailable with how to fix it. Prime a lazily routed subagent against its own tool context with a provisioning callback bound to it, and check for a cancelled turn before arming the deadline timer. * 🪢 fix: Recheck the Attachment Ignore Rule Each Turn and Share One Request Cap Drop the process-wide ignore cache and check the folder once per turn, shared by every agent, rejecting a truncated read. Cap workspace requests per turn across agents (workspaceAttachments.concurrency, default 3), name converted images for their stored type, log only safe error metadata, and keep the bookkeeping write from advancing updatedAt. * 🔀 chore: Take dev's File Methods Ahead of Merging dev dev and this branch both add to the same lines of the file methods module. Take dev's version here so the merge of dev that follows is clean; the next commit adds recordFileWorkspaceImport back beside dev's new methods. * 🧩 fix: Ignore Workspace Operations a Newer Worker Advertises Bring recordFileWorkspaceImport back beside dev's new file methods after the merge. Drop well-formed operation names this build does not know from a worker's capabilities, globally and per workspace, instead of rejecting the whole status, so workers can turn on new operations such as import_file by default. Known names keep their strict checks, malformed lists are still refused, and a worker offering only unknown operations offers no workspace tools rather than failing.
657 lines
23 KiB
TypeScript
657 lines
23 KiB
TypeScript
import { createHash } from 'node:crypto';
|
|
import { logger } from '@librechat/data-schemas';
|
|
import {
|
|
CODE_ENVIRONMENT_COMMAND_TIMEOUT_HARD_MAX_MS,
|
|
CODE_WORKSPACE_ID_PATTERN,
|
|
CODE_WORKSPACE_MAX_COUNT,
|
|
CODE_WORKSPACE_OPERATIONS,
|
|
isCodeWorkspaceEnvironment,
|
|
isRepositoryInstructionDescriptor,
|
|
} from 'librechat-data-provider';
|
|
import type { CodeWorkspaceDescriptor, CodeWorkspaceOperation } from 'librechat-data-provider';
|
|
import type { WorkspaceEditFileFeature } from './edits';
|
|
import { WORKSPACE_EDIT_FILE_FEATURES } from './edits';
|
|
|
|
const CODE_BRIDGE_REQUEST_TIMEOUT_MS = 10_000;
|
|
// Covers 32 roots with 32 bounded action names and escaped metadata per root.
|
|
const CODE_BRIDGE_STATUS_RESPONSE_MAX_BYTES = 256 * 1024;
|
|
|
|
export type CodeBridgePrincipalType = 'deployment' | 'tenant' | 'user' | 'role' | 'group';
|
|
|
|
export type CodeBridgeWorkerBinding = {
|
|
tenantId: string;
|
|
principal: {
|
|
type: CodeBridgePrincipalType;
|
|
id: string;
|
|
};
|
|
};
|
|
|
|
export type CodeBridgePairing = {
|
|
protocolVersion: 1;
|
|
workerId: string;
|
|
code: string;
|
|
expiresAt: string;
|
|
};
|
|
|
|
export type CodeBridgeWorkerStatus = {
|
|
status: 'offline' | 'starting' | 'ready';
|
|
leaseExpiresInMs?: number;
|
|
statefulWorkspace?: boolean;
|
|
sandboxProfile?: string;
|
|
runtimes?: string[];
|
|
operations?: CodeWorkspaceOperation[];
|
|
workspaces?: CodeWorkspaceDescriptor[];
|
|
programmaticLanguages?: ['bash'];
|
|
/** Negotiated edit features; unknown names are dropped. */
|
|
editFileFeatures?: WorkspaceEditFileFeature[];
|
|
maxCommandTimeoutMs?: number;
|
|
};
|
|
|
|
export type CodeBridgeFetch = (
|
|
input: Parameters<typeof fetch>[0],
|
|
init?: Parameters<typeof fetch>[1],
|
|
) => ReturnType<typeof fetch>;
|
|
|
|
export class CodeBridgePairingError extends Error {
|
|
constructor(
|
|
public readonly reason: 'rejected' | 'invalid' | 'timeout' | 'failed',
|
|
public readonly upstreamStatus?: number,
|
|
) {
|
|
super(`Code bridge pairing ${reason}`);
|
|
this.name = 'CodeBridgePairingError';
|
|
}
|
|
}
|
|
|
|
export class CodeBridgeLifecycleError extends Error {
|
|
constructor(
|
|
public readonly reason: 'rejected' | 'invalid' | 'timeout' | 'failed',
|
|
public readonly upstreamStatus?: number,
|
|
) {
|
|
super(`Code bridge lifecycle request ${reason}`);
|
|
this.name = 'CodeBridgeLifecycleError';
|
|
}
|
|
}
|
|
|
|
export class CodeBridgeStatusError extends Error {
|
|
constructor(
|
|
public readonly reason: 'rejected' | 'invalid' | 'timeout' | 'failed' | 'busy',
|
|
public readonly upstreamStatus?: number,
|
|
) {
|
|
super(`Code bridge status request ${reason}`);
|
|
this.name = 'CodeBridgeStatusError';
|
|
}
|
|
}
|
|
|
|
/** Transport failures that say nothing about the worker itself; its own heartbeat lease still does. */
|
|
function isTransientStatusFailure(error: unknown): boolean {
|
|
if (!(error instanceof CodeBridgeStatusError)) return false;
|
|
if (error.reason === 'timeout' || error.reason === 'failed') return true;
|
|
return error.reason === 'rejected' && error.upstreamStatus != null && error.upstreamStatus >= 500;
|
|
}
|
|
|
|
type RememberedReadyStatus = { status: CodeBridgeWorkerStatus; leaseExpiresAt: number };
|
|
|
|
/**
|
|
* Polls worker status with a short shared cache. A transient transport failure (timeout, network
|
|
* error, 5xx from the Code API or the proxy in front of it) is not evidence that the worker left:
|
|
* while the last `ready` observation's heartbeat lease is still running, that observation answers
|
|
* for it, and without one the poll is retried once. Rejections and malformed responses fail
|
|
* immediately and forget the remembered status. `bypassCache` callers validate a selection before
|
|
* it is persisted, so they get exactly one fresh observation: no retry, no remembered status.
|
|
*/
|
|
export function createCodeBridgeStatusPoller({
|
|
fetchImpl,
|
|
maxConcurrent = 32,
|
|
maxEntries = 1_000,
|
|
cacheTtlMs = 2_000,
|
|
}: {
|
|
fetchImpl?: CodeBridgeFetch;
|
|
maxConcurrent?: number;
|
|
maxEntries?: number;
|
|
cacheTtlMs?: number;
|
|
} = {}): (params: {
|
|
baseURL: string;
|
|
token: string;
|
|
workerId: string;
|
|
/** Force a new upstream request, bypassing cached results and older in-flight polls. */
|
|
bypassCache?: boolean;
|
|
}) => Promise<CodeBridgeWorkerStatus> {
|
|
const requests = new Map<
|
|
string,
|
|
{ expiresAt: number; request: Promise<CodeBridgeWorkerStatus> }
|
|
>();
|
|
const lastReady = new Map<string, RememberedReadyStatus>();
|
|
let active = 0;
|
|
|
|
const remember = (key: string, status: CodeBridgeWorkerStatus, startedAt: number): void => {
|
|
lastReady.delete(key);
|
|
if (status.status !== 'ready' || status.leaseExpiresInMs == null) return;
|
|
if (lastReady.size >= maxEntries) {
|
|
const oldest = lastReady.keys().next().value;
|
|
if (oldest != null) lastReady.delete(oldest);
|
|
}
|
|
/** The lease was read after the request started, so this bound never outlives it. */
|
|
lastReady.set(key, { status, leaseExpiresAt: startedAt + status.leaseExpiresInMs });
|
|
};
|
|
|
|
const recall = (key: string): CodeBridgeWorkerStatus | undefined => {
|
|
const entry = lastReady.get(key);
|
|
if (entry == null) return undefined;
|
|
const remainingMs = entry.leaseExpiresAt - Date.now();
|
|
if (remainingMs <= 0) {
|
|
lastReady.delete(key);
|
|
return undefined;
|
|
}
|
|
return { ...entry.status, leaseExpiresInMs: remainingMs };
|
|
};
|
|
|
|
const fetchStatus = async (
|
|
params: { baseURL: string; token: string; workerId: string },
|
|
key: string,
|
|
): Promise<CodeBridgeWorkerStatus> => {
|
|
const startedAt = Date.now();
|
|
try {
|
|
const status = await getCodeBridgeWorkerStatus({ ...params, fetchImpl });
|
|
remember(key, status, startedAt);
|
|
return status;
|
|
} catch (error) {
|
|
if (!isTransientStatusFailure(error)) lastReady.delete(key);
|
|
throw error;
|
|
}
|
|
};
|
|
|
|
const observe = async (
|
|
params: { baseURL: string; token: string; workerId: string },
|
|
key: string,
|
|
): Promise<CodeBridgeWorkerStatus> => {
|
|
try {
|
|
return await fetchStatus(params, key);
|
|
} catch (error) {
|
|
if (!isTransientStatusFailure(error)) throw error;
|
|
const remembered = recall(key);
|
|
if (remembered != null) {
|
|
logger.warn(
|
|
`[codeBridge] Worker status poll failed (${(error as CodeBridgeStatusError).reason}); ` +
|
|
`using the last ready status for ${remembered.leaseExpiresInMs}ms more of its lease`,
|
|
);
|
|
return remembered;
|
|
}
|
|
return await fetchStatus(params, key);
|
|
}
|
|
};
|
|
|
|
return (params) => {
|
|
const credentialId = createHash('sha256').update(params.token).digest('base64url');
|
|
const normalizedBaseURL = params.baseURL.trim().replace(/\/+$/, '');
|
|
const key = `${normalizedBaseURL}\u0000${params.workerId}\u0000${credentialId}`;
|
|
if (params.bypassCache) {
|
|
if (active >= maxConcurrent) return Promise.reject(new CodeBridgeStatusError('busy'));
|
|
active += 1;
|
|
// Mutation validation needs its own observation, but still shares the polling capacity.
|
|
return fetchStatus(params, key).finally(() => {
|
|
active -= 1;
|
|
});
|
|
}
|
|
const now = Date.now();
|
|
const cached = requests.get(key);
|
|
if (cached != null && cached.expiresAt > now) return cached.request;
|
|
if (cached != null) requests.delete(key);
|
|
if (active >= maxConcurrent) return Promise.reject(new CodeBridgeStatusError('busy'));
|
|
if (requests.size >= maxEntries) {
|
|
for (const [cachedKey, entry] of requests) {
|
|
if (entry.expiresAt <= now) requests.delete(cachedKey);
|
|
}
|
|
if (requests.size >= maxEntries) {
|
|
return Promise.reject(new CodeBridgeStatusError('busy'));
|
|
}
|
|
}
|
|
active += 1;
|
|
const startedAt = Date.now();
|
|
const request = observe(params, key)
|
|
.then((status) => {
|
|
const completedAt = Date.now();
|
|
const ttl =
|
|
status.leaseExpiresInMs == null
|
|
? cacheTtlMs
|
|
: Math.max(
|
|
0,
|
|
Math.min(cacheTtlMs, status.leaseExpiresInMs - (completedAt - startedAt)),
|
|
);
|
|
requests.set(key, {
|
|
expiresAt: completedAt + ttl,
|
|
request: Promise.resolve(status),
|
|
});
|
|
return status;
|
|
})
|
|
.catch((error: unknown) => {
|
|
requests.delete(key);
|
|
throw error;
|
|
})
|
|
.finally(() => {
|
|
active -= 1;
|
|
});
|
|
requests.set(key, { expiresAt: Number.POSITIVE_INFINITY, request });
|
|
return request;
|
|
};
|
|
}
|
|
|
|
function validStatusString(value: unknown): value is string {
|
|
return typeof value === 'string' && value.length > 0 && value.length <= 128;
|
|
}
|
|
|
|
function validStatusStringArray(value: unknown): value is string[] {
|
|
return (
|
|
Array.isArray(value) && value.length <= 32 && value.every((item) => validStatusString(item))
|
|
);
|
|
}
|
|
|
|
/** Bound on advertised operation names, known or not, so a status cannot carry an unbounded list. */
|
|
const MAX_ADVERTISED_WORKSPACE_OPERATIONS = 64;
|
|
const WORKSPACE_OPERATION_NAME = /^[a-z][a-z0-9_]{0,63}$/;
|
|
const KNOWN_WORKSPACE_OPERATIONS: ReadonlySet<string> = new Set(CODE_WORKSPACE_OPERATIONS);
|
|
|
|
/**
|
|
* The operations this build knows from a well-formed advertised list, or undefined when the list
|
|
* itself is malformed. Workers gain operations before every LibreChat deployment knows them, so a
|
|
* well-formed name this build does not know is dropped rather than invalidating the worker.
|
|
*/
|
|
function knownWorkspaceOperations(value: unknown): CodeWorkspaceOperation[] | undefined {
|
|
if (
|
|
!Array.isArray(value) ||
|
|
value.length > MAX_ADVERTISED_WORKSPACE_OPERATIONS ||
|
|
new Set(value).size !== value.length ||
|
|
!value.every(
|
|
(operation) => typeof operation === 'string' && WORKSPACE_OPERATION_NAME.test(operation),
|
|
)
|
|
) {
|
|
return undefined;
|
|
}
|
|
return value.filter((operation): operation is CodeWorkspaceOperation =>
|
|
KNOWN_WORKSPACE_OPERATIONS.has(operation),
|
|
);
|
|
}
|
|
|
|
/** A worker-level list must still offer at least one operation this build can use. */
|
|
function validWorkspaceOperations(value: unknown): boolean {
|
|
return (knownWorkspaceOperations(value)?.length ?? 0) > 0;
|
|
}
|
|
|
|
function knownEditFileFeatures(value: unknown): WorkspaceEditFileFeature[] {
|
|
if (!Array.isArray(value)) {
|
|
return [];
|
|
}
|
|
return WORKSPACE_EDIT_FILE_FEATURES.filter((feature) => value.includes(feature));
|
|
}
|
|
|
|
function validWorkspaceCapabilities(value: unknown): value is {
|
|
protocolVersion: 1;
|
|
/** Raw advertised names; read through `knownWorkspaceOperations`. */
|
|
operations: string[];
|
|
workspaces: Array<Omit<CodeWorkspaceDescriptor, 'operations'> & { operations?: string[] }>;
|
|
programmaticLanguages?: unknown;
|
|
editFileFeatures?: unknown;
|
|
} {
|
|
if (value == null || typeof value !== 'object' || Array.isArray(value)) return false;
|
|
const capabilities = value as Record<string, unknown>;
|
|
if (
|
|
capabilities.protocolVersion !== 1 ||
|
|
!validWorkspaceOperations(capabilities.operations) ||
|
|
!Array.isArray(capabilities.workspaces) ||
|
|
capabilities.workspaces.length < 1 ||
|
|
capabilities.workspaces.length > CODE_WORKSPACE_MAX_COUNT
|
|
) {
|
|
return false;
|
|
}
|
|
const operations = knownWorkspaceOperations(capabilities.operations) ?? [];
|
|
const ids = new Set<string>();
|
|
return capabilities.workspaces.every((value) => {
|
|
if (value == null || typeof value !== 'object' || Array.isArray(value)) return false;
|
|
const workspace = value as Record<string, unknown>;
|
|
if (
|
|
Object.keys(workspace).some(
|
|
(key) =>
|
|
key !== 'id' &&
|
|
key !== 'name' &&
|
|
key !== 'operations' &&
|
|
key !== 'workspaceInstances' &&
|
|
key !== 'workspaceScopes' &&
|
|
key !== 'environment' &&
|
|
key !== 'instructions',
|
|
) ||
|
|
typeof workspace.id !== 'string' ||
|
|
!CODE_WORKSPACE_ID_PATTERN.test(workspace.id) ||
|
|
ids.has(workspace.id) ||
|
|
(workspace.instructions !== undefined &&
|
|
(!Array.isArray(workspace.instructions) ||
|
|
workspace.instructions.length > 1 ||
|
|
!workspace.instructions.every(isRepositoryInstructionDescriptor))) ||
|
|
(workspace.environment !== undefined && !isCodeWorkspaceEnvironment(workspace.environment)) ||
|
|
(workspace.workspaceInstances !== undefined &&
|
|
(!Array.isArray(workspace.workspaceInstances) ||
|
|
workspace.workspaceInstances.length !== 1 ||
|
|
workspace.workspaceInstances[0] !== 'git_worktree')) ||
|
|
(workspace.workspaceScopes !== undefined &&
|
|
(!Array.isArray(workspace.workspaceScopes) ||
|
|
workspace.workspaceScopes.length !== 1 ||
|
|
workspace.workspaceScopes[0] !== 'git_linked_worktree')) ||
|
|
(workspace.name !== undefined &&
|
|
(typeof workspace.name !== 'string' ||
|
|
workspace.name.trim().length === 0 ||
|
|
workspace.name.length > 128)) ||
|
|
(workspace.operations !== undefined &&
|
|
!isWorkspaceOperationSubset(workspace.operations, operations))
|
|
) {
|
|
return false;
|
|
}
|
|
ids.add(workspace.id);
|
|
return true;
|
|
});
|
|
}
|
|
|
|
/** A root may narrow the worker's known operations; one only it knows is dropped, not refused. */
|
|
function isWorkspaceOperationSubset(
|
|
value: unknown,
|
|
workerOperations: readonly CodeWorkspaceOperation[],
|
|
): boolean {
|
|
const known = knownWorkspaceOperations(value);
|
|
return known != null && known.every((operation) => workerOperations.includes(operation));
|
|
}
|
|
|
|
/**
|
|
* A worker whose workspace tools are all newer than this build is still a valid worker; it
|
|
* simply offers no workspace tools here, exactly like one that advertises none.
|
|
*/
|
|
function offersOnlyUnknownWorkspaceOperations(value: unknown): boolean {
|
|
if (value == null || typeof value !== 'object' || Array.isArray(value)) return false;
|
|
const { operations } = value as { operations?: unknown };
|
|
return (
|
|
Array.isArray(operations) &&
|
|
operations.length > 0 &&
|
|
knownWorkspaceOperations(operations)?.length === 0
|
|
);
|
|
}
|
|
|
|
/** Keep the status endpoint readable during worker-first rolling upgrades.
|
|
* Legacy capabilities remain non-selectable and are surfaced as an explicit
|
|
* update-required state; they never regain an implicit `primary` binding. */
|
|
function validLegacyWorkspaceCapabilities(value: unknown): value is {
|
|
operations: string[];
|
|
} {
|
|
if (value == null || typeof value !== 'object' || Array.isArray(value)) return false;
|
|
const capabilities = value as Record<string, unknown>;
|
|
return (
|
|
capabilities.protocolVersion === undefined &&
|
|
capabilities.workspaces === undefined &&
|
|
validWorkspaceOperations(capabilities.operations)
|
|
);
|
|
}
|
|
|
|
async function readBoundedStatusJson(response: Response): Promise<unknown> {
|
|
const reader = response.body?.getReader();
|
|
if (reader == null) throw new CodeBridgeStatusError('invalid');
|
|
const decoder = new TextDecoder();
|
|
let bytes = 0;
|
|
let json = '';
|
|
while (true) {
|
|
const { done, value } = await reader.read();
|
|
if (done) break;
|
|
bytes += value.byteLength;
|
|
if (bytes > CODE_BRIDGE_STATUS_RESPONSE_MAX_BYTES) {
|
|
await reader.cancel();
|
|
throw new CodeBridgeStatusError('invalid');
|
|
}
|
|
json += decoder.decode(value, { stream: true });
|
|
}
|
|
json += decoder.decode();
|
|
try {
|
|
return JSON.parse(json) as unknown;
|
|
} catch {
|
|
throw new CodeBridgeStatusError('invalid');
|
|
}
|
|
}
|
|
|
|
export async function getCodeBridgeWorkerStatus({
|
|
baseURL,
|
|
token,
|
|
workerId,
|
|
fetchImpl = fetch,
|
|
}: {
|
|
baseURL: string;
|
|
token: string;
|
|
workerId: string;
|
|
fetchImpl?: CodeBridgeFetch;
|
|
}): Promise<CodeBridgeWorkerStatus> {
|
|
try {
|
|
const response = await fetchImpl(
|
|
`${baseURL.trim().replace(/\/+$/, '')}/bridge/workers/${encodeURIComponent(workerId)}/status`,
|
|
{
|
|
headers: { Authorization: `Bearer ${token}` },
|
|
redirect: 'error',
|
|
signal: AbortSignal.timeout(CODE_BRIDGE_REQUEST_TIMEOUT_MS),
|
|
},
|
|
);
|
|
if (!response.ok) {
|
|
await response.body?.cancel().catch(() => undefined);
|
|
throw new CodeBridgeStatusError('rejected', response.status);
|
|
}
|
|
const payload = await readBoundedStatusJson(response);
|
|
if (typeof payload !== 'object' || payload == null) {
|
|
throw new CodeBridgeStatusError('invalid');
|
|
}
|
|
const status = payload as {
|
|
protocolVersion?: unknown;
|
|
workerId?: unknown;
|
|
online?: unknown;
|
|
ready?: unknown;
|
|
leaseExpiresInMs?: unknown;
|
|
maxCommandTimeoutMs?: unknown;
|
|
capabilities?: {
|
|
statefulWorkspace?: unknown;
|
|
sandboxProfile?: unknown;
|
|
runtimes?: unknown;
|
|
workspaceTools?: unknown;
|
|
};
|
|
};
|
|
const capabilities = status.capabilities;
|
|
const validLease =
|
|
status.leaseExpiresInMs == null ||
|
|
(typeof status.leaseExpiresInMs === 'number' &&
|
|
Number.isSafeInteger(status.leaseExpiresInMs) &&
|
|
status.leaseExpiresInMs > 0 &&
|
|
status.leaseExpiresInMs <= 60_000);
|
|
const validCommandTimeout =
|
|
status.maxCommandTimeoutMs == null ||
|
|
(typeof status.maxCommandTimeoutMs === 'number' &&
|
|
Number.isSafeInteger(status.maxCommandTimeoutMs) &&
|
|
status.maxCommandTimeoutMs >= 1 &&
|
|
status.maxCommandTimeoutMs <= CODE_ENVIRONMENT_COMMAND_TIMEOUT_HARD_MAX_MS);
|
|
const validCapabilities =
|
|
capabilities == null || validStatusString(capabilities.sandboxProfile);
|
|
const validRuntimes = capabilities == null || validStatusStringArray(capabilities.runtimes);
|
|
const validWorkspaceTools =
|
|
capabilities?.workspaceTools == null ||
|
|
validWorkspaceCapabilities(capabilities.workspaceTools) ||
|
|
validLegacyWorkspaceCapabilities(capabilities.workspaceTools) ||
|
|
offersOnlyUnknownWorkspaceOperations(capabilities.workspaceTools);
|
|
if (
|
|
status.protocolVersion !== 1 ||
|
|
status.workerId !== workerId ||
|
|
typeof status.online !== 'boolean' ||
|
|
typeof status.ready !== 'boolean' ||
|
|
(status.ready && !status.online) ||
|
|
(status.online && (status.leaseExpiresInMs == null || capabilities == null)) ||
|
|
(!status.online && (status.leaseExpiresInMs != null || capabilities != null)) ||
|
|
!validLease ||
|
|
!validCommandTimeout ||
|
|
!validCapabilities ||
|
|
!validRuntimes ||
|
|
!validWorkspaceTools ||
|
|
(capabilities?.statefulWorkspace != null &&
|
|
typeof capabilities.statefulWorkspace !== 'boolean')
|
|
) {
|
|
throw new CodeBridgeStatusError('invalid');
|
|
}
|
|
let workerStatus: CodeBridgeWorkerStatus['status'] = 'offline';
|
|
if (status.online) {
|
|
workerStatus = status.ready ? 'ready' : 'starting';
|
|
}
|
|
let workspaceStatus: Pick<
|
|
CodeBridgeWorkerStatus,
|
|
'operations' | 'workspaces' | 'programmaticLanguages' | 'editFileFeatures'
|
|
> = {};
|
|
if (validWorkspaceCapabilities(capabilities?.workspaceTools)) {
|
|
workspaceStatus = {
|
|
operations: knownWorkspaceOperations(capabilities.workspaceTools.operations) ?? [],
|
|
workspaces: capabilities.workspaceTools.workspaces.map(
|
|
({ operations, ...workspace }): CodeWorkspaceDescriptor => ({
|
|
...workspace,
|
|
...(operations ? { operations: knownWorkspaceOperations(operations) ?? [] } : {}),
|
|
}),
|
|
),
|
|
};
|
|
if (
|
|
Array.isArray(capabilities.workspaceTools.programmaticLanguages) &&
|
|
capabilities.workspaceTools.programmaticLanguages.every(
|
|
(language) => typeof language === 'string',
|
|
) &&
|
|
capabilities.workspaceTools.programmaticLanguages.includes('bash')
|
|
) {
|
|
workspaceStatus.programmaticLanguages = ['bash'];
|
|
}
|
|
const editFileFeatures = knownEditFileFeatures(capabilities.workspaceTools.editFileFeatures);
|
|
if (editFileFeatures.length > 0) {
|
|
workspaceStatus.editFileFeatures = editFileFeatures;
|
|
}
|
|
} else if (validLegacyWorkspaceCapabilities(capabilities?.workspaceTools)) {
|
|
workspaceStatus = {
|
|
operations: knownWorkspaceOperations(capabilities.workspaceTools.operations) ?? [],
|
|
};
|
|
}
|
|
return {
|
|
status: workerStatus,
|
|
...(typeof capabilities?.statefulWorkspace === 'boolean'
|
|
? { statefulWorkspace: capabilities.statefulWorkspace }
|
|
: {}),
|
|
...(typeof status.leaseExpiresInMs !== 'number'
|
|
? {}
|
|
: { leaseExpiresInMs: status.leaseExpiresInMs }),
|
|
...(typeof status.maxCommandTimeoutMs !== 'number'
|
|
? {}
|
|
: { maxCommandTimeoutMs: status.maxCommandTimeoutMs }),
|
|
...(typeof capabilities?.sandboxProfile !== 'string'
|
|
? {}
|
|
: { sandboxProfile: capabilities.sandboxProfile }),
|
|
...(validStatusStringArray(capabilities?.runtimes)
|
|
? { runtimes: capabilities.runtimes }
|
|
: {}),
|
|
...workspaceStatus,
|
|
};
|
|
} catch (error) {
|
|
if (error instanceof CodeBridgeStatusError) throw error;
|
|
if (error instanceof Error && error.name === 'TimeoutError') {
|
|
throw new CodeBridgeStatusError('timeout');
|
|
}
|
|
throw new CodeBridgeStatusError('failed');
|
|
}
|
|
}
|
|
|
|
function validPairing(value: unknown, workerId: string): value is CodeBridgePairing {
|
|
if (typeof value !== 'object' || value == null) return false;
|
|
const pairing = value as Partial<CodeBridgePairing>;
|
|
const expiresAt = typeof pairing.expiresAt === 'string' ? Date.parse(pairing.expiresAt) : NaN;
|
|
return (
|
|
pairing.protocolVersion === 1 &&
|
|
pairing.workerId === workerId &&
|
|
typeof pairing.code === 'string' &&
|
|
/^[A-Za-z0-9_-]{32}$/.test(pairing.code) &&
|
|
Number.isFinite(expiresAt) &&
|
|
expiresAt > Date.now()
|
|
);
|
|
}
|
|
|
|
export async function createCodeBridgePairing({
|
|
baseURL,
|
|
token,
|
|
workerId,
|
|
binding,
|
|
fetchImpl = fetch,
|
|
}: {
|
|
baseURL: string;
|
|
token: string;
|
|
workerId: string;
|
|
binding?: CodeBridgeWorkerBinding;
|
|
fetchImpl?: CodeBridgeFetch;
|
|
}): Promise<CodeBridgePairing> {
|
|
try {
|
|
const response = await fetchImpl(`${baseURL.trim().replace(/\/+$/, '')}/bridge/pairings`, {
|
|
method: 'POST',
|
|
headers: {
|
|
Authorization: `Bearer ${token}`,
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify({ workerId, ...(binding != null ? { binding } : {}) }),
|
|
redirect: 'error',
|
|
signal: AbortSignal.timeout(CODE_BRIDGE_REQUEST_TIMEOUT_MS),
|
|
});
|
|
if (!response.ok) {
|
|
throw new CodeBridgePairingError('rejected', response.status);
|
|
}
|
|
const payload = (await response.json()) as unknown;
|
|
if (!validPairing(payload, workerId)) {
|
|
throw new CodeBridgePairingError('invalid');
|
|
}
|
|
return payload;
|
|
} catch (error) {
|
|
if (error instanceof CodeBridgePairingError) throw error;
|
|
if (error instanceof Error && error.name === 'TimeoutError') {
|
|
throw new CodeBridgePairingError('timeout');
|
|
}
|
|
throw new CodeBridgePairingError('failed');
|
|
}
|
|
}
|
|
|
|
export async function revokeCodeBridgeWorker({
|
|
baseURL,
|
|
token,
|
|
workerId,
|
|
fetchImpl = fetch,
|
|
}: {
|
|
baseURL: string;
|
|
token: string;
|
|
workerId: string;
|
|
fetchImpl?: CodeBridgeFetch;
|
|
}): Promise<void> {
|
|
try {
|
|
const response = await fetchImpl(
|
|
`${baseURL.trim().replace(/\/+$/, '')}/bridge/workers/${encodeURIComponent(workerId)}/revoke`,
|
|
{
|
|
method: 'POST',
|
|
headers: { Authorization: `Bearer ${token}` },
|
|
redirect: 'error',
|
|
signal: AbortSignal.timeout(CODE_BRIDGE_REQUEST_TIMEOUT_MS),
|
|
},
|
|
);
|
|
if (!response.ok) {
|
|
throw new CodeBridgeLifecycleError('rejected', response.status);
|
|
}
|
|
const payload = (await response.json()) as unknown;
|
|
if (
|
|
typeof payload !== 'object' ||
|
|
payload == null ||
|
|
(payload as { protocolVersion?: unknown }).protocolVersion !== 1 ||
|
|
(payload as { revoked?: unknown }).revoked !== true
|
|
) {
|
|
throw new CodeBridgeLifecycleError('invalid');
|
|
}
|
|
} catch (error) {
|
|
if (error instanceof CodeBridgeLifecycleError) throw error;
|
|
if (error instanceof Error && error.name === 'TimeoutError') {
|
|
throw new CodeBridgeLifecycleError('timeout');
|
|
}
|
|
throw new CodeBridgeLifecycleError('failed');
|
|
}
|
|
}
|
|
|
|
export function readCodeBridgeSecret(name: string): string | undefined {
|
|
return Object.prototype.hasOwnProperty.call(process.env, name) ? process.env[name] : undefined;
|
|
}
|