app-submit-controller.ts 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import React from "react";
  2. import type { ModelSelection } from "@zcode/shared";
  3. import type { SessionEvent, TurnId } from "@zcode/contracts";
  4. import { submitDuringActiveTurn, submitIdleTurn } from "./app-submit.js";
  5. import type {
  6. DraftAttachment,
  7. Message,
  8. QueuedInput,
  9. SelectionState,
  10. SubmitValueOptions,
  11. SlashSelectionState,
  12. } from "./app-model.js";
  13. import type { TuiOptions, TuiSubmitPromptResult } from "./types.js";
  14. export function useSubmitValue(input: {
  15. activeTurnId?: TurnId;
  16. applyResult: (result: TuiSubmitPromptResult, preserveTurnState?: boolean) => void;
  17. applySessionEvent: (event: SessionEvent) => void;
  18. busy: boolean;
  19. draftAttachmentsRef: React.MutableRefObject<DraftAttachment[]>;
  20. emptyPromptStatus: string;
  21. messageInsertIndex: number;
  22. options: TuiOptions;
  23. resolveSubmittedText: (submittedValue: string) => string;
  24. resolveSubmittedModel?: (submittedValue: string) => ModelSelection | undefined;
  25. setBusy: (value: boolean) => void;
  26. setDraftAttachments: React.Dispatch<React.SetStateAction<DraftAttachment[]>>;
  27. setDraftValue: (value: string) => void;
  28. setLastError: (message: string | undefined) => void;
  29. setLiveModelText: (value: string) => void;
  30. setMessages: React.Dispatch<React.SetStateAction<Message[]>>;
  31. setQueuedInputs: React.Dispatch<React.SetStateAction<QueuedInput[]>>;
  32. setSelection: React.Dispatch<React.SetStateAction<SelectionState | undefined>>;
  33. setSlashSelection: React.Dispatch<React.SetStateAction<SlashSelectionState | undefined>>;
  34. setStatus: (status: string) => void;
  35. setStatusDetails: React.Dispatch<React.SetStateAction<string[]>>;
  36. turnRef: React.MutableRefObject<AbortController | undefined>;
  37. }): (submittedValue: string, options?: SubmitValueOptions) => Promise<void> {
  38. return React.useCallback(
  39. async (submittedValue: string, options: SubmitValueOptions = {}) => {
  40. const text = input.resolveSubmittedText(submittedValue).trim();
  41. const modelSelection = input.resolveSubmittedModel?.(submittedValue);
  42. if (!text) {
  43. input.setStatus(input.emptyPromptStatus);
  44. return;
  45. }
  46. if (input.busy) {
  47. await submitDuringActiveTurn({
  48. activeTurnId: input.activeTurnId,
  49. applyResult: input.applyResult,
  50. applySessionEvent: input.applySessionEvent,
  51. draftAttachments: input.draftAttachmentsRef.current,
  52. messageInsertIndex: input.messageInsertIndex,
  53. options: input.options,
  54. setDraftValue: input.setDraftValue,
  55. setLastError: input.setLastError,
  56. setMessages: input.setMessages,
  57. setQueuedInputs: input.setQueuedInputs,
  58. setStatus: input.setStatus,
  59. signal: input.turnRef.current?.signal,
  60. text,
  61. modelSelection,
  62. });
  63. return;
  64. }
  65. await submitIdleTurn({
  66. applyResult: input.applyResult,
  67. applySessionEvent: input.applySessionEvent,
  68. draftAttachments: input.draftAttachmentsRef.current,
  69. options: input.options,
  70. setBusy: input.setBusy,
  71. setDraftAttachments: input.setDraftAttachments,
  72. setDraftValue: input.setDraftValue,
  73. setLastError: input.setLastError,
  74. setLiveModelText: input.setLiveModelText,
  75. setMessages: input.setMessages,
  76. setSelection: input.setSelection,
  77. setSlashSelection: input.setSlashSelection,
  78. setStatus: input.setStatus,
  79. setStatusDetails: input.setStatusDetails,
  80. submitOptions: options,
  81. text,
  82. modelSelection,
  83. turnRef: input.turnRef,
  84. });
  85. },
  86. [input],
  87. );
  88. }