mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 12:54:20 +01:00
refactor(ocm): pair linked servers with native OpenCode links and drop legacy CLI stubs
This commit is contained in:
1 parent
bf2f81f921
commit
3c7bc80988
20 files changed
+483
-1595
No files matched your search
@@ -27,9 +27,8 @@ export const OpenCodeServerSchema = z.object({
|
||||
updatedAt: z.number(),
|
||||
});
|
||||
|
||||
export const OpenCodeServerPairingSchema = z.object({
|
||||
pairUrl: HttpUrlSchema,
|
||||
code: z.string().min(16).max(256),
|
||||
export const OpenCodeConnectPairingSchema = z.object({
|
||||
connectCode: z.string().regex(/^[A-Za-z0-9_-]+$/).min(1).max(256),
|
||||
});
|
||||
|
||||
export const CreateOpenCodeServerRequestSchema = z
|
||||
@@ -38,7 +37,7 @@ export const CreateOpenCodeServerRequestSchema = z
|
||||
baseUrl: HttpUrlSchema,
|
||||
username: z.string().trim().min(1).max(100).default(OPENCODE_SERVER_USERNAME),
|
||||
password: z.string().min(1).max(1024).optional(),
|
||||
pairing: OpenCodeServerPairingSchema.optional(),
|
||||
pairing: OpenCodeConnectPairingSchema.optional(),
|
||||
enabled: z.boolean().default(true),
|
||||
})
|
||||
.refine((value) => !(value.password && value.pairing), "Provide either a password or a pairing code, not both");
|
||||
@@ -49,7 +48,7 @@ export const UpdateOpenCodeServerRequestSchema = z
|
||||
baseUrl: HttpUrlSchema.optional(),
|
||||
username: z.string().trim().min(1).max(100).optional(),
|
||||
password: z.string().min(1).max(1024).nullable().optional(),
|
||||
pairing: OpenCodeServerPairingSchema.optional(),
|
||||
pairing: OpenCodeConnectPairingSchema.optional(),
|
||||
enabled: z.boolean().optional(),
|
||||
})
|
||||
.refine((value) => !(value.password && value.pairing), "Provide either a password or a pairing code, not both");
|
||||
@@ -75,13 +74,6 @@ export const OpenCodeServerStatusSchema = z.object({
|
||||
error: z.string().nullable(),
|
||||
});
|
||||
|
||||
export const OpenCodeServerLinkSchema = z.object({
|
||||
kind: z.literal("opencode"),
|
||||
name: z.string().trim().min(1).max(100),
|
||||
baseUrl: HttpUrlSchema,
|
||||
pairing: OpenCodeServerPairingSchema,
|
||||
});
|
||||
|
||||
export const ManagerAppLinkSchema = z.object({
|
||||
kind: z.literal("manager"),
|
||||
name: z.string().trim().min(1).max(100),
|
||||
@@ -91,26 +83,91 @@ export const ManagerAppLinkSchema = z.object({
|
||||
});
|
||||
|
||||
export type OpenCodeServer = z.infer<typeof OpenCodeServerSchema>;
|
||||
export type OpenCodeServerPairing = z.infer<typeof OpenCodeServerPairingSchema>;
|
||||
export type OpenCodeConnectPairing = z.infer<typeof OpenCodeConnectPairingSchema>;
|
||||
export type CreateOpenCodeServerRequest = z.input<typeof CreateOpenCodeServerRequestSchema>;
|
||||
export type UpdateOpenCodeServerRequest = z.input<typeof UpdateOpenCodeServerRequestSchema>;
|
||||
export type TestOpenCodeServerRequest = z.input<typeof TestOpenCodeServerRequestSchema>;
|
||||
export type OpenCodeServerTestResult = z.infer<typeof OpenCodeServerTestResultSchema>;
|
||||
export type OpenCodeServerStatus = z.infer<typeof OpenCodeServerStatusSchema>;
|
||||
export type OpenCodeServerLink = z.infer<typeof OpenCodeServerLinkSchema>;
|
||||
export type ManagerAppLink = z.infer<typeof ManagerAppLinkSchema>;
|
||||
|
||||
export function buildOpenCodeServerLink(link: OpenCodeServerLink): string {
|
||||
const params = buildLinkParams(link);
|
||||
params.set("pair", link.pairing.pairUrl);
|
||||
params.set("code", link.pairing.code);
|
||||
return buildLinkUrl(params);
|
||||
export interface OpenCodeConnectLink {
|
||||
name: string;
|
||||
baseUrl: string;
|
||||
pairing: { connectCode: string };
|
||||
}
|
||||
|
||||
export function parseOpenCodeServerLink(text: string): OpenCodeServerLink | null {
|
||||
return readLink(OpenCodeServerLinkSchema, text, (url) => ({
|
||||
pairing: { pairUrl: url.searchParams.get("pair"), code: url.searchParams.get("code") },
|
||||
}));
|
||||
const CONNECT_CODE_PATTERN = /^[A-Za-z0-9_-]{1,256}$/;
|
||||
const CONNECT_LINK_PATH_PATTERN = /^\/auth\/connect\/([A-Za-z0-9_-]{1,256})$/;
|
||||
|
||||
function readConnectOrigin(value: string): string | null {
|
||||
let url: URL;
|
||||
try {
|
||||
url = new URL(value);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
if (url.protocol !== "http:" && url.protocol !== "https:") return null;
|
||||
if (url.username || url.password || url.search || url.hash) return null;
|
||||
if (url.pathname !== "/") return null;
|
||||
return url.origin;
|
||||
}
|
||||
|
||||
const ConnectOriginSchema = z.string().transform((value, ctx) => {
|
||||
const origin = readConnectOrigin(value);
|
||||
if (origin === null) {
|
||||
ctx.addIssue({ code: "custom", message: "URL must be an http(s) origin" });
|
||||
return z.NEVER;
|
||||
}
|
||||
return origin;
|
||||
});
|
||||
|
||||
const ConnectPayloadSchema = z.object({
|
||||
code: z.string().regex(CONNECT_CODE_PATTERN),
|
||||
urls: z.array(ConnectOriginSchema).min(1),
|
||||
name: z.string().optional(),
|
||||
});
|
||||
|
||||
function parseConnectPayload(text: string): OpenCodeConnectLink | null {
|
||||
let payload: unknown;
|
||||
try {
|
||||
payload = JSON.parse(text);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
|
||||
const parsed = ConnectPayloadSchema.safeParse(payload);
|
||||
if (!parsed.success) return null;
|
||||
|
||||
const baseUrl = parsed.data.urls[0];
|
||||
if (baseUrl === undefined) return null;
|
||||
|
||||
const name = parsed.data.name?.trim();
|
||||
return {
|
||||
name: name !== undefined && name.length > 0 ? name : new URL(baseUrl).hostname,
|
||||
baseUrl,
|
||||
pairing: { connectCode: parsed.data.code },
|
||||
};
|
||||
}
|
||||
|
||||
function parseConnectUrl(text: string): OpenCodeConnectLink | null {
|
||||
let url: URL;
|
||||
try {
|
||||
url = new URL(text);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
if (url.protocol !== "http:" && url.protocol !== "https:") return null;
|
||||
if (url.username || url.password || url.search || url.hash) return null;
|
||||
const connectCode = CONNECT_LINK_PATH_PATTERN.exec(url.pathname)?.[1];
|
||||
if (!connectCode) return null;
|
||||
return { name: url.hostname, baseUrl: url.origin, pairing: { connectCode } };
|
||||
}
|
||||
|
||||
export function parseOpenCodeConnectLink(text: string): OpenCodeConnectLink | null {
|
||||
const trimmed = text.trim();
|
||||
if (trimmed.startsWith("{")) return parseConnectPayload(trimmed);
|
||||
return parseConnectUrl(trimmed);
|
||||
}
|
||||
|
||||
export function buildManagerAppLink(link: ManagerAppLink): string {
|
||||
@@ -132,10 +189,6 @@ export function parseManagerAppLink(text: string): ManagerAppLink | null {
|
||||
});
|
||||
}
|
||||
|
||||
export function readOpenCodeServerLinkVersion(text: string): string | null {
|
||||
return parseOpenCodeServerLinkUrl(text)?.searchParams.get("v") ?? null;
|
||||
}
|
||||
|
||||
function parseOpenCodeServerLinkUrl(text: string): URL | null {
|
||||
let url: URL;
|
||||
try {
|
||||
|
||||
Reference in new issue
Block a user