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
|
? removed
|
||||||
: `Setup replaces ${replaced.join(" and ")}. ${removed}`;
|
: `Setup replaces ${replaced.join(" and ")}. ${removed}`;
|
||||||
const legacy =
|
const legacy =
|
||||||
clientName === "opencode"
|
dialect === "opencode"
|
||||||
? " A legacy OpenCode entry moved into the native server table keeps only its environment."
|
? " 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}`;
|
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";
|
import { z } from "zod";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Optional fields `registration status` parses on every client. User fields
|
* Registration-status fields doctor parses on every client. User fields
|
||||||
* outside this set, such as OMP `timeout`, are not registration-status fields.
|
* outside this schema, such as OMP `timeout`, are not registration-status
|
||||||
* Setup and doctor share these schemas so a retained value cannot be one
|
* fields. Setup accepts a retained value only when this schema does.
|
||||||
* doctor will reject.
|
|
||||||
*/
|
*/
|
||||||
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
|
export const registrationSchema = z
|
||||||
.object({
|
.object({
|
||||||
command: z.string().min(1),
|
command: z.string().min(1),
|
||||||
args: z.array(z.string()).default([]),
|
args: z.array(z.string()).default([]),
|
||||||
startup_timeout_sec: registrationFields.startup_timeout_sec.optional(),
|
startup_timeout_sec: z.number().positive().optional(),
|
||||||
type: registrationFields.type.optional(),
|
type: z.string().optional(),
|
||||||
transport: registrationFields.transport.optional(),
|
transport: z.string().optional(),
|
||||||
serverName: registrationFields.serverName.optional(),
|
serverName: z.string().optional(),
|
||||||
tools: registrationFields.tools.optional(),
|
tools: z.array(z.string()).optional(),
|
||||||
disabled: registrationFields.disabled.optional(),
|
disabled: z.boolean().optional(),
|
||||||
enabled: registrationFields.enabled.optional(),
|
enabled: z.boolean().optional(),
|
||||||
})
|
})
|
||||||
.passthrough();
|
.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. */
|
/** Whether a retained entry value is one registration status can parse. */
|
||||||
export const registrationFieldAccepted = (
|
export const registrationFieldAccepted = (
|
||||||
key: string,
|
key: string,
|
||||||
value: unknown,
|
value: unknown,
|
||||||
): boolean => {
|
): boolean =>
|
||||||
switch (key) {
|
isRegistrationField(key)
|
||||||
case "startup_timeout_sec":
|
? registrationShape[key].safeParse(value).success
|
||||||
return registrationFields.startup_timeout_sec.safeParse(value).success;
|
: true;
|
||||||
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;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|||||||
@@ -251,6 +251,7 @@ describe("setup approval disclosure", () => {
|
|||||||
name: "opencode",
|
name: "opencode",
|
||||||
displayName: "OpenCode",
|
displayName: "OpenCode",
|
||||||
configPath: "/opencode.json",
|
configPath: "/opencode.json",
|
||||||
|
format: "opencode",
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user