mirror of
https://gitea.toothfairyai.com/ToothFairyAI/tf_code.git
synced 2026-04-05 00:23:10 +00:00
25 lines
587 B
TypeScript
25 lines
587 B
TypeScript
import { ulid } from "ulid";
|
|
import { z } from "zod";
|
|
|
|
export namespace Identifier {
|
|
const prefixes = {
|
|
session: "ses",
|
|
message: "msg",
|
|
} as const;
|
|
|
|
export function create(
|
|
prefix: keyof typeof prefixes,
|
|
given?: string,
|
|
): string {
|
|
if (given) {
|
|
if (given.startsWith(prefixes[prefix])) return given;
|
|
throw new Error(`ID ${given} does not start with ${prefixes[prefix]}`);
|
|
}
|
|
return [prefixes[prefix], ulid()].join("_");
|
|
}
|
|
|
|
export function schema(prefix: keyof typeof prefixes) {
|
|
return z.string().startsWith(prefixes[prefix]);
|
|
}
|
|
}
|