refactor(setup): accept retained fields from the registration schema
Setup keeps a registration field only when the schema doctor parses accepts it, and the OpenCode legacy disclosure follows that client's dialect.
This commit is contained in:
committed by
xeropresence
parent
ecd92970a4
commit
fb3fa1dc5f
@@ -1618,7 +1618,7 @@ export const describeClientRegistrationRetention = (
|
||||
? removed
|
||||
: `Setup replaces ${replaced.join(" and ")}. ${removed}`;
|
||||
const legacy =
|
||||
clientName === "opencode"
|
||||
dialect === "opencode"
|
||||
? " A legacy OpenCode entry moved into the native server table keeps only its environment."
|
||||
: "";
|
||||
return `On an existing local command entry, other user fields are kept. ${replacement} When that entry is remote, its other fields are replaced.${legacy}`;
|
||||
|
||||
@@ -1,56 +1,36 @@
|
||||
import { z } from "zod";
|
||||
|
||||
/**
|
||||
* Optional fields `registration status` parses on every client. User fields
|
||||
* outside this set, such as OMP `timeout`, are not registration-status fields.
|
||||
* Setup and doctor share these schemas so a retained value cannot be one
|
||||
* doctor will reject.
|
||||
* Registration-status fields doctor parses on every client. User fields
|
||||
* outside this schema, such as OMP `timeout`, are not registration-status
|
||||
* fields. Setup accepts a retained value only when this schema does.
|
||||
*/
|
||||
const registrationFields = {
|
||||
startup_timeout_sec: z.number().positive(),
|
||||
type: z.string(),
|
||||
transport: z.string(),
|
||||
serverName: z.string(),
|
||||
tools: z.array(z.string()),
|
||||
disabled: z.boolean(),
|
||||
enabled: z.boolean(),
|
||||
} as const;
|
||||
|
||||
export const registrationSchema = z
|
||||
.object({
|
||||
command: z.string().min(1),
|
||||
args: z.array(z.string()).default([]),
|
||||
startup_timeout_sec: registrationFields.startup_timeout_sec.optional(),
|
||||
type: registrationFields.type.optional(),
|
||||
transport: registrationFields.transport.optional(),
|
||||
serverName: registrationFields.serverName.optional(),
|
||||
tools: registrationFields.tools.optional(),
|
||||
disabled: registrationFields.disabled.optional(),
|
||||
enabled: registrationFields.enabled.optional(),
|
||||
startup_timeout_sec: z.number().positive().optional(),
|
||||
type: z.string().optional(),
|
||||
transport: z.string().optional(),
|
||||
serverName: z.string().optional(),
|
||||
tools: z.array(z.string()).optional(),
|
||||
disabled: z.boolean().optional(),
|
||||
enabled: z.boolean().optional(),
|
||||
})
|
||||
.passthrough();
|
||||
|
||||
const registrationShape = registrationSchema.shape;
|
||||
|
||||
const isRegistrationField = (
|
||||
key: string,
|
||||
): key is keyof typeof registrationShape =>
|
||||
Object.hasOwn(registrationShape, key);
|
||||
|
||||
/** Whether a retained entry value is one registration status can parse. */
|
||||
export const registrationFieldAccepted = (
|
||||
key: string,
|
||||
value: unknown,
|
||||
): boolean => {
|
||||
switch (key) {
|
||||
case "startup_timeout_sec":
|
||||
return registrationFields.startup_timeout_sec.safeParse(value).success;
|
||||
case "type":
|
||||
return registrationFields.type.safeParse(value).success;
|
||||
case "transport":
|
||||
return registrationFields.transport.safeParse(value).success;
|
||||
case "serverName":
|
||||
return registrationFields.serverName.safeParse(value).success;
|
||||
case "tools":
|
||||
return registrationFields.tools.safeParse(value).success;
|
||||
case "disabled":
|
||||
return registrationFields.disabled.safeParse(value).success;
|
||||
case "enabled":
|
||||
return registrationFields.enabled.safeParse(value).success;
|
||||
default:
|
||||
return true;
|
||||
}
|
||||
};
|
||||
): boolean =>
|
||||
isRegistrationField(key)
|
||||
? registrationShape[key].safeParse(value).success
|
||||
: true;
|
||||
|
||||
@@ -251,6 +251,7 @@ describe("setup approval disclosure", () => {
|
||||
name: "opencode",
|
||||
displayName: "OpenCode",
|
||||
configPath: "/opencode.json",
|
||||
format: "opencode",
|
||||
},
|
||||
];
|
||||
|
||||
|
||||
Reference in New Issue
Block a user