tool-presentation-failures.fixture.tsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import { useEffect, useState } from "react";
  2. import { createRoot } from "react-dom/client";
  3. import type { FileMediaPreview, IPlatformService } from "@zcode/shared";
  4. import { ToolCallBody } from "@/ToolCallBlocks/ToolCallBody.js";
  5. import { ToolPresentationCapabilitiesProvider } from "@/ToolCallBlocks/presentationCapabilities.js";
  6. import { OpenSplitButton } from "@/OpenSplitButton.js";
  7. import { PlatformProvider } from "@/hooks/usePlatform.js";
  8. import { TabStoreProvider, useTabStoreApi } from "@/store/TabStoreProvider.js";
  9. import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
  10. import { Button } from "@/components/ui/button.js";
  11. import "@/styles.css";
  12. const params = new URLSearchParams(location.search);
  13. const icon =
  14. "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+a9K0AAAAASUVORK5CYII=";
  15. document.documentElement.className = `${params.get("theme")} theme-zai-${params.get("theme")}`;
  16. let releaseOld: ((value: FileMediaPreview) => void) | undefined;
  17. const readMediaPreview = async (path: string): Promise<FileMediaPreview> => {
  18. if (path === "failure") throw new Error("fixture image read failure");
  19. if (path === "pending")
  20. return new Promise((resolve) => {
  21. releaseOld = resolve;
  22. });
  23. return { path, dataBase64: path === "empty" ? "" : icon, totalBytes: 1, mediaType: "image/png" };
  24. };
  25. const platform = {
  26. canOpenLocalFiles: true,
  27. getInstalledEditors: async () => [
  28. { id: "vscode", name: "VS Code", iconDataUrl: icon },
  29. { id: "word", name: "Word", iconDataUrl: icon },
  30. ],
  31. openInEditor: async (_id: string, _path: string, options: unknown) => {
  32. document.querySelector("#opened")!.textContent = JSON.stringify(options);
  33. if (params.get("failure") === "reject") throw new Error("fixture open rejection");
  34. return { success: false, error: "fixture open failure" };
  35. },
  36. } as unknown as IPlatformService;
  37. function Fixture() {
  38. const tabs = useTabStoreApi();
  39. const [path, setPath] = useState("pending");
  40. useEffect(() => {
  41. tabs.getState().addTab("/workspace", {
  42. workspaceIdentity: "remote-fixture",
  43. remoteTarget: { kind: "ssh", host: "example.com", username: "dev" },
  44. });
  45. }, [tabs]);
  46. return (
  47. <main className="min-h-screen bg-background text-foreground p-4 space-y-4">
  48. <div className="flex flex-wrap gap-2">
  49. {["pending", "failure", "empty", "success"].map((value) => (
  50. <Button key={value} onClick={() => setPath(value)}>
  51. {value}
  52. </Button>
  53. ))}
  54. <Button
  55. onClick={() =>
  56. releaseOld?.({
  57. path: "old",
  58. dataBase64: "old-image",
  59. totalBytes: 1,
  60. mediaType: "image/png",
  61. })
  62. }
  63. >
  64. release old
  65. </Button>
  66. </div>
  67. <section data-testid="image-preview">
  68. <ToolPresentationCapabilitiesProvider value={{ readMediaPreview }}>
  69. <ToolCallBody
  70. childToolList={null}
  71. workspacePath="/workspace"
  72. toolCall={{ toolId: "image", kind: "read", status: "completed" }}
  73. displayModel={{
  74. inlinePreview: {
  75. type: "image",
  76. source: { type: "image", title: "Tool image", path },
  77. },
  78. planResult: null,
  79. viewerSource: null,
  80. viewerLabelId: "codeViewer.viewCode",
  81. showSummaryFileLink: false,
  82. showInput: false,
  83. showOutput: false,
  84. showKind: false,
  85. }}
  86. />
  87. </ToolPresentationCapabilitiesProvider>
  88. </section>
  89. <OpenSplitButton
  90. target={{
  91. type: "file",
  92. title: "Report",
  93. label: "Report",
  94. path: "/workspace/report.docx",
  95. previewSource: {
  96. type: "file",
  97. title: "Report",
  98. path: "/workspace/report.docx",
  99. workspacePath: "/workspace",
  100. workspaceIdentity: "remote-fixture",
  101. },
  102. }}
  103. onOpenCodeViewer={() => {}}
  104. renderCard={(button) => <div data-testid="file-card">Report{button}</div>}
  105. />
  106. <output id="opened" className="break-all" />
  107. </main>
  108. );
  109. }
  110. createRoot(document.getElementById("root")!).render(
  111. <ZCodeIntlProvider initialLocale={params.get("locale") === "en-US" ? "en-US" : "zh-CN"}>
  112. <PlatformProvider platform={platform}>
  113. <TabStoreProvider>
  114. <Fixture />
  115. </TabStoreProvider>
  116. </PlatformProvider>
  117. </ZCodeIntlProvider>,
  118. );