detect-input.ts 621 B

12345678910111213141516171819202122232425
  1. import { VideoInputSchema } from "../types/scene.js";
  2. import type { VideoInput } from "../types/scene.js";
  3. export type InputFormat = "valid-schema" | "invalid-json" | "non-json";
  4. export interface DetectResult {
  5. format: InputFormat;
  6. parsed?: VideoInput;
  7. }
  8. export function detectInputFormat(text: string): DetectResult {
  9. let parsed: unknown;
  10. try {
  11. parsed = JSON.parse(text);
  12. } catch {
  13. return { format: "non-json" };
  14. }
  15. const result = VideoInputSchema.safeParse(parsed);
  16. if (result.success) {
  17. return { format: "valid-schema", parsed: result.data };
  18. }
  19. return { format: "invalid-json" };
  20. }