feedbackSubmitModelContext.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import { decodeCustomModelValue } from "@zcode/shared";
  2. import type { ZCodeConfigOption } from "@zcode/shared";
  3. const FEEDBACK_MODEL_OPTIONS_PREVIEW_LIMIT = 12;
  4. export interface FeedbackAgentModelContext {
  5. model?: string;
  6. display?: string;
  7. optionCount?: number;
  8. optionsPreview?: string[];
  9. }
  10. export function readCurrentAgentModelContext(
  11. configOptions?: readonly ZCodeConfigOption[] | null,
  12. ): FeedbackAgentModelContext {
  13. const modelOption = configOptions?.find(
  14. (option) => option.category === "model" && option.type === "select",
  15. );
  16. if (!modelOption) {
  17. return {};
  18. }
  19. const options = (modelOption.options ?? [])
  20. .map((option) => ({
  21. value: option.value.trim(),
  22. name: option.name.trim(),
  23. }))
  24. .filter((option) => option.value.length > 0 || option.name.length > 0);
  25. const rawModel =
  26. typeof modelOption.currentValue === "string"
  27. ? modelOption.currentValue.trim()
  28. : String(modelOption.currentValue ?? "").trim();
  29. const matchedOption = rawModel ? options.find((option) => option.value === rawModel) : undefined;
  30. const customModel = rawModel ? decodeCustomModelValue(rawModel) : null;
  31. const display = matchedOption?.name || customModel?.modelName?.trim() || rawModel;
  32. return {
  33. ...(rawModel ? { model: rawModel } : {}),
  34. ...(display ? { display } : {}),
  35. optionCount: options.length,
  36. optionsPreview: options
  37. .slice(0, FEEDBACK_MODEL_OPTIONS_PREVIEW_LIMIT)
  38. .map((option) => option.name || option.value),
  39. };
  40. }