share-import-selection.fixture.tsx 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. import { useRef, useState } from "react";
  2. import { createRoot } from "react-dom/client";
  3. import type { IServiceAccessor, IModelSelectionService, ModelSelectionView } from "@zcode/services";
  4. import type { IPlatformService } from "@zcode/shared";
  5. import { resolveEffectiveModelSelection, type ProviderRegistryView } from "@zcode/provider";
  6. import { ServiceProvider } from "@/hooks/useServices.js";
  7. import { PlatformProvider } from "@/hooks/usePlatform.js";
  8. import { TabStoreProvider } from "@/store/TabStoreProvider.js";
  9. import { ZCodeIntlProvider, useZCodeIntl } from "@/i18n/IntlProvider.js";
  10. import { TooltipProvider } from "@/components/ui/tooltip.js";
  11. import { V4ComposerModeSwitch } from "@/v4/composer/V4ComposerModeControls.js";
  12. import { ThoughtLevelCycleControl } from "@/chat-input-toolbar/ThoughtLevelCycleControl.js";
  13. import { ModelConfigSelect } from "@/ModelConfigSelect.js";
  14. import { buildRegistryModelSelectGroups } from "@/lib/modelSelectionGroups.js";
  15. import { decodeCustomModelValue, encodeCustomModelValue } from "@/lib/zcodeCustomModelValue.js";
  16. import { captureComposerRecentSubmission } from "@/lib/composerRecent.js";
  17. import { useDraftConfigControl } from "@/v4/composer/useDraftConfigControl.js";
  18. import { persistV4ComposerDraft, readV4ComposerDraft } from "@/v4/composer/composerDraftStore.js";
  19. import { resolveImportedComposerConfig } from "@/v4/composer/newTaskDraft.js";
  20. import { useZCodeSessionStore } from "@/store/zcodeSessionStore.js";
  21. import type { SessionConfigState } from "@zcode/shared/zcode-protocol-v4";
  22. import { createComposerSubmissionConfig } from "@/v4/composer/composerSubmissionConfig.js";
  23. import type { V4ComposerConfigPicker } from "@/v4/composer/configPickerState.js";
  24. import "@/styles.css";
  25. const params = new URLSearchParams(location.search);
  26. const source = params.get("source") ?? "root";
  27. const workspacePath = "/share-selection-fixture";
  28. const selection = {
  29. providerId: "recipient",
  30. modelId: "recipient-model",
  31. options: { reasoningLevel: "high" },
  32. };
  33. const view = {
  34. revision: 1,
  35. preferredSelection: selection,
  36. providers: [
  37. {
  38. providerId: "recipient",
  39. providerName: "Recipient",
  40. config: { api: { type: "anthropic-messages", baseUrl: "https://fixture.invalid" } },
  41. models: [
  42. {
  43. modelId: "recipient-model",
  44. config: {
  45. enabled: true,
  46. optionSpecs: { reasoningLevel: { values: ["low", "high"], map: "{}" } },
  47. },
  48. },
  49. ],
  50. },
  51. ],
  52. } as unknown as ModelSelectionView;
  53. const modelSelectionService: IModelSelectionService = {
  54. getView: async (input) => ({
  55. ...view,
  56. ...resolveEffectiveModelSelection({
  57. selection: input?.selection ?? null,
  58. registry: view as unknown as ProviderRegistryView,
  59. classifyProvider: () => "ordinary",
  60. }),
  61. }),
  62. onDidChange: () => ({ dispose() {} }),
  63. };
  64. const services = {
  65. zcodeSessionService: {},
  66. settingService: { get: async () => ({}) },
  67. } as unknown as IServiceAccessor;
  68. const platform = {} as IPlatformService;
  69. if (!sessionStorage.getItem("prepared")) {
  70. sessionStorage.setItem("prepared", "true");
  71. if (source === "session-text")
  72. persistV4ComposerDraft(workspacePath, undefined, "session", {
  73. text: "UNSENT TEXT",
  74. mode: "yolo",
  75. planEnabled: true,
  76. modelSelection: selection,
  77. });
  78. if (source === "recent")
  79. captureComposerRecentSubmission(workspacePath, { modelSelection: selection, mode: "yolo" })();
  80. if (source === "root" || source === "empty")
  81. persistV4ComposerDraft(workspacePath, undefined, "__draft__", {
  82. text: "ROOT MUST STAY",
  83. mode: "plan",
  84. ...(source === "root" ? { modelSelection: selection } : {}),
  85. });
  86. }
  87. document.documentElement.className =
  88. params.get("theme") === "light" ? "light theme-zai-light" : "dark theme-zai-dark";
  89. function Fixture() {
  90. const { intl } = useZCodeIntl();
  91. const [sessionId, setSessionId] = useState<string | null>(
  92. source.startsWith("session") ? "session" : "unrelated-history",
  93. );
  94. const [snapshot, setSnapshot] = useState<Partial<SessionConfigState>>({
  95. mode: "build",
  96. planEnabled: false,
  97. modelSelection: selection,
  98. permissionGrant: { interactionId: "historical-grant" },
  99. planTransition: { toolCallId: "historical-plan", planEnabled: true },
  100. });
  101. const navigate = (id: string | null, imported = false) => {
  102. const store = useZCodeSessionStore.getState();
  103. if (id === null) store.startDraft(workspacePath);
  104. else
  105. store.setActiveTaskId(
  106. workspacePath,
  107. id,
  108. undefined,
  109. imported
  110. ? resolveImportedComposerConfig({ workspacePath, sessionId: id, reused: false })
  111. : undefined,
  112. );
  113. setSessionId(id);
  114. };
  115. const [ready, setReady] = useState(source !== "delayed");
  116. const [picker, setPicker] = useState<V4ComposerConfigPicker | null>(null);
  117. const [submitted, setSubmitted] = useState("");
  118. const thoughtRef = useRef<HTMLSpanElement>(null);
  119. const control = useDraftConfigControl({
  120. workspacePath,
  121. sessionId,
  122. sessionConfig: snapshot,
  123. agentStartupAllowed: false,
  124. modelSelectionService: ready ? modelSelectionService : null,
  125. });
  126. const current = control.draftConfig.modelSelection;
  127. const config = createComposerSubmissionConfig(control.draftConfig, ready ? view : null);
  128. const open = (next: V4ComposerConfigPicker, value: boolean) => setPicker(value ? next : null);
  129. return (
  130. <main className="bg-background text-foreground min-h-screen p-4 space-y-4">
  131. <button
  132. onClick={() => {
  133. navigate("imported", true);
  134. }}
  135. >
  136. Import
  137. </button>
  138. <button
  139. onClick={() => {
  140. navigate("imported");
  141. }}
  142. >
  143. Reopen
  144. </button>
  145. <button onClick={() => navigate(null)}>Ordinary new task</button>
  146. <button onClick={() => navigate("session")}>Open session</button>
  147. <button
  148. onClick={() =>
  149. setSnapshot({
  150. ...snapshot,
  151. mode: "yolo",
  152. permissionGrant: { interactionId: "live-grant" },
  153. })
  154. }
  155. >
  156. New grant
  157. </button>
  158. <button
  159. onClick={() =>
  160. setSnapshot({
  161. ...snapshot,
  162. planEnabled: true,
  163. planTransition: { toolCallId: "live-plan", planEnabled: true },
  164. })
  165. }
  166. >
  167. New plan
  168. </button>
  169. <button onClick={() => setSnapshot({ ...snapshot })}>Repeat snapshot</button>
  170. <button onClick={() => setReady(true)}>Registry ready</button>
  171. <p data-testid="content">{sessionId === "imported" ? "IMPORTED CONTENT" : "other"}</p>
  172. <div className="flex flex-wrap gap-2">
  173. <V4ComposerModeSwitch
  174. workspacePath={workspacePath}
  175. draftConfig={control.draftConfig}
  176. disabled={false}
  177. activeConfigPicker={picker}
  178. onConfigPickerOpenChange={open}
  179. onSwitchMode={control.handleDraftSwitchMode}
  180. />
  181. <ModelConfigSelect
  182. modelGroups={buildRegistryModelSelectGroups("glm", view)}
  183. normalizedValue={
  184. current ? encodeCustomModelValue(current.providerId, current.modelId) : ""
  185. }
  186. triggerLabel={current?.modelId ?? "Pick model"}
  187. labelVisibilityClassName="inline-flex"
  188. showManageModelsAction={false}
  189. lockReasonMessage=""
  190. isItemLocked={() => false}
  191. onValueChange={(value) => {
  192. const parsed = decodeCustomModelValue(value);
  193. if (parsed?.modelName)
  194. control.handleDraftSelectModel(parsed.providerId, parsed.modelName);
  195. }}
  196. />
  197. <span ref={thoughtRef}>
  198. <ThoughtLevelCycleControl
  199. intl={intl}
  200. triggerRef={thoughtRef}
  201. labelVisibilityClassName="inline-flex"
  202. option={{
  203. id: "thought",
  204. name: "Reasoning",
  205. type: "select",
  206. category: "thought",
  207. currentValue: current?.options?.reasoningLevel ?? "",
  208. options: [
  209. { value: "low", name: "Low" },
  210. { value: "high", name: "High" },
  211. ],
  212. }}
  213. onValueChange={control.handleDraftSelectThought}
  214. />
  215. </span>
  216. </div>
  217. <textarea
  218. aria-label="Message"
  219. value={control.composerDraft.text}
  220. onChange={(event) => control.updateComposerContent({ text: event.target.value })}
  221. />
  222. <button
  223. disabled={!config}
  224. onClick={() => {
  225. const frozen = createComposerSubmissionConfig(control.draftConfig, view);
  226. setSubmitted(JSON.stringify(frozen));
  227. }}
  228. >
  229. Submit
  230. </button>
  231. <output data-testid="config">{JSON.stringify(control.draftConfig)}</output>
  232. <output data-testid="root-draft">
  233. {JSON.stringify(readV4ComposerDraft(workspacePath, undefined, "__draft__"))}
  234. </output>
  235. <output data-testid="submission">{submitted}</output>
  236. </main>
  237. );
  238. }
  239. createRoot(document.getElementById("root")!).render(
  240. <ServiceProvider services={services}>
  241. <PlatformProvider platform={platform}>
  242. <TabStoreProvider>
  243. <ZCodeIntlProvider locale={params.get("locale") === "zh-CN" ? "zh-CN" : "en-US"}>
  244. <TooltipProvider>
  245. <Fixture />
  246. </TooltipProvider>
  247. </ZCodeIntlProvider>
  248. </TabStoreProvider>
  249. </PlatformProvider>
  250. </ServiceProvider>,
  251. );