├── .prettierrc ├── src ├── vite-env.d.ts ├── utils │ ├── mod.ts │ ├── nodeId.ts │ ├── rand.ts │ ├── apikey.ts │ ├── qparams.ts │ ├── clipboard.ts │ ├── resize.ts │ ├── debounce.ts │ ├── lstore.ts │ ├── types.ts │ ├── color.ts │ ├── fluxEdge.ts │ ├── constants.ts │ ├── prompt.ts │ ├── fluxNode.ts │ └── chakra.tsx ├── index.css ├── components │ ├── utils │ │ ├── BigButton.tsx │ │ ├── APIKeyInput.tsx │ │ ├── TextAndCodeBlock.tsx │ │ ├── LabeledInputs.tsx │ │ └── NavigationBar.tsx │ ├── modals │ │ ├── APIKeyModal.tsx │ │ └── SettingsModal.tsx │ ├── nodes │ │ └── LabelUpdaterNode.tsx │ ├── Prompt.tsx │ └── App.tsx └── main.tsx ├── public ├── dave.jpg ├── meta.jpg ├── t11s.jpg ├── favicon.ico ├── meta-full.png ├── paradigm-full.svg └── paradigm.svg ├── vite.config.ts ├── tsconfig.node.json ├── .gitignore ├── .github ├── ISSUE_TEMPLATE │ ├── bug_report.md │ └── feature_request.md └── PULL_REQUEST_TEMPLATE.md ├── tsconfig.json ├── index.html ├── LICENSE ├── package.json ├── README.md └── CONTRIBUTING.md /.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "printWidth": 90 3 | } 4 | -------------------------------------------------------------------------------- /src/vite-env.d.ts: -------------------------------------------------------------------------------- 1 | /// 2 | -------------------------------------------------------------------------------- /public/dave.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hey/flux/main/public/dave.jpg -------------------------------------------------------------------------------- /public/meta.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hey/flux/main/public/meta.jpg -------------------------------------------------------------------------------- /public/t11s.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hey/flux/main/public/t11s.jpg -------------------------------------------------------------------------------- /public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hey/flux/main/public/favicon.ico -------------------------------------------------------------------------------- /public/meta-full.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hey/flux/main/public/meta-full.png -------------------------------------------------------------------------------- /src/utils/mod.ts: -------------------------------------------------------------------------------- 1 | export function mod(n: number, m: number) { 2 | return ((n % m) + m) % m; 3 | } 4 | -------------------------------------------------------------------------------- /src/utils/nodeId.ts: -------------------------------------------------------------------------------- 1 | export function generateNodeId() { 2 | return Math.random().toString().replace("0.", ""); 3 | } 4 | -------------------------------------------------------------------------------- /src/utils/rand.ts: -------------------------------------------------------------------------------- 1 | export function randomNumber(min: number, max: number) { 2 | return Math.random() * (max - min) + min; 3 | } 4 | -------------------------------------------------------------------------------- /src/utils/apikey.ts: -------------------------------------------------------------------------------- 1 | export function isValidAPIKey(apiKey: string | null) { 2 | return apiKey?.length == 51 && apiKey?.startsWith("sk-"); 3 | } 4 | -------------------------------------------------------------------------------- /vite.config.ts: -------------------------------------------------------------------------------- 1 | import { defineConfig } from "vite"; 2 | import react from "@vitejs/plugin-react-swc"; 3 | 4 | // https://vitejs.dev/config/ 5 | export default defineConfig({ 6 | plugins: [react()], 7 | }); 8 | -------------------------------------------------------------------------------- /tsconfig.node.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "composite": true, 4 | "module": "ESNext", 5 | "moduleResolution": "Node", 6 | "allowSyntheticDefaultImports": true 7 | }, 8 | "include": ["vite.config.ts"] 9 | } 10 | -------------------------------------------------------------------------------- /src/utils/qparams.ts: -------------------------------------------------------------------------------- 1 | export function getQueryParam(parameterName: string) { 2 | const urlParams = new URLSearchParams(window.location.search); 3 | return urlParams.get(parameterName); 4 | } 5 | 6 | export function resetURL() { 7 | history.replaceState({}, document.title, window.location.pathname); 8 | } 9 | -------------------------------------------------------------------------------- /src/utils/clipboard.ts: -------------------------------------------------------------------------------- 1 | export const copySnippetToClipboard = async (text: string): Promise => { 2 | try { 3 | await navigator.clipboard.writeText(text); 4 | return true; 5 | } catch (err) { 6 | console.error("Failed to copy to clipboard", err); 7 | return false; 8 | } 9 | }; 10 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | pnpm-debug.log* 8 | lerna-debug.log* 9 | 10 | node_modules 11 | dist 12 | dist-ssr 13 | *.local 14 | 15 | # Editor directories and files 16 | .vscode/* 17 | !.vscode/extensions.json 18 | .idea 19 | .DS_Store 20 | *.suo 21 | *.ntvs* 22 | *.njsproj 23 | *.sln 24 | *.sw? 25 | .env -------------------------------------------------------------------------------- /src/index.css: -------------------------------------------------------------------------------- 1 | .selected { 2 | box-shadow: 0px 0px 0px 2px red, 0px 0px 30px 2px red !important; 3 | } 4 | 5 | .react-flow__node { 6 | border-radius: 6px; 7 | border-width: 0px; 8 | } 9 | 10 | .react-flow__node:not(.selected):hover { 11 | box-shadow: 0 0 0 0.5px #1a192b !important; 12 | } 13 | 14 | /* Enable if you 15 | want bigger handles: 16 | .react-flow__handle { 17 | width: 20px; 18 | height: 7px; 19 | border-radius: 5px; 20 | } */ 21 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report 3 | about: Create a report to help us improve 4 | title: "fix: " 5 | labels: bug 6 | assignees: "" 7 | --- 8 | 9 | **Platform** 10 | The OS and browser you're using. 11 | 12 | **Description** 13 | Enter your issue details here. 14 | One way to structure the description: 15 | 16 | [short summary of the bug] 17 | 18 | I tried doing this: 19 | 20 | [screenshot or video that causes the bug] 21 | 22 | I expected to see this happen: [explanation] 23 | 24 | Instead, this happened: [explanation] 25 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | 5 | 6 | ## Motivation 7 | 8 | 13 | 14 | ## Solution 15 | 16 | 20 | -------------------------------------------------------------------------------- /src/components/utils/BigButton.tsx: -------------------------------------------------------------------------------- 1 | import { ButtonProps, Button, Tooltip } from "@chakra-ui/react"; 2 | 3 | import { adjustColor } from "../../utils/color"; 4 | 5 | export function BigButton({ 6 | color, 7 | tooltip, 8 | ...others 9 | }: { color: string; tooltip: string } & ButtonProps) { 10 | return ( 11 | 12 | 20 | 21 | ); 22 | } 23 | -------------------------------------------------------------------------------- /src/utils/resize.ts: -------------------------------------------------------------------------------- 1 | import { useEffect } from "react"; 2 | 3 | export function useDebouncedWindowResize(callback: () => void, timeout: number) { 4 | useEffect(() => { 5 | let resizeTimeout: NodeJS.Timeout; 6 | 7 | const handleResize = () => { 8 | clearTimeout(resizeTimeout); 9 | resizeTimeout = setTimeout(callback, timeout); 10 | }; 11 | 12 | window.addEventListener("resize", handleResize); 13 | 14 | return () => { 15 | window.removeEventListener("resize", handleResize); 16 | clearTimeout(resizeTimeout); 17 | }; 18 | }, [callback, timeout]); 19 | } 20 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "ESNext", 4 | "useDefineForClassFields": true, 5 | "lib": ["DOM", "DOM.Iterable", "ESNext"], 6 | "allowJs": false, 7 | "skipLibCheck": true, 8 | "esModuleInterop": false, 9 | "allowSyntheticDefaultImports": true, 10 | "strict": true, 11 | "forceConsistentCasingInFileNames": true, 12 | "module": "ESNext", 13 | "moduleResolution": "Node", 14 | "resolveJsonModule": true, 15 | "isolatedModules": true, 16 | "noEmit": true, 17 | "jsx": "react-jsx" 18 | }, 19 | "include": ["src"], 20 | "references": [{ "path": "./tsconfig.node.json" }] 21 | } 22 | -------------------------------------------------------------------------------- /src/utils/debounce.ts: -------------------------------------------------------------------------------- 1 | import { useEffect, useState } from "react"; 2 | 3 | export function useDebouncedEffect( 4 | callback: () => void, 5 | timeout: number, 6 | deps: any[] 7 | ): boolean { 8 | const [isWaiting, setIsWaiting] = useState(false); 9 | 10 | useEffect(() => { 11 | setIsWaiting(true); 12 | 13 | const handler = setTimeout(() => { 14 | callback(); 15 | 16 | setIsWaiting(false); 17 | }, timeout); 18 | 19 | // This line is secret sauce: cancel the 20 | // timeout if the effect gets triggered again. 21 | return () => clearTimeout(handler); 22 | }, [timeout, ...deps]); 23 | 24 | return isWaiting; // Useful for showing loading animations, etc. 25 | } 26 | -------------------------------------------------------------------------------- /src/components/utils/APIKeyInput.tsx: -------------------------------------------------------------------------------- 1 | import { BoxProps } from "@chakra-ui/react"; 2 | import { LabeledPasswordInputWithLink } from "./LabeledInputs"; 3 | 4 | export function APIKeyInput({ 5 | apiKey, 6 | setApiKey, 7 | ...others 8 | }: { 9 | apiKey: string | null; 10 | setApiKey: (apiKey: string) => void; 11 | } & BoxProps) { 12 | return ( 13 | 23 | ); 24 | } 25 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feature_request.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Feature request 3 | about: Suggest an idea for this project 4 | title: "feat: " 5 | labels: feature-request 6 | assignees: "" 7 | --- 8 | 9 | **Is your feature request related to a problem? Please describe.** 10 | A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] 11 | 12 | **Describe the solution you'd like** 13 | A clear and concise description of what you want to happen. 14 | 15 | **Describe alternatives you've considered** 16 | A clear and concise description of any alternative solutions or features you've considered. 17 | 18 | **Additional context** 19 | Add any other context or screenshots about the feature request here. 20 | -------------------------------------------------------------------------------- /src/main.tsx: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | 3 | import ReactDOM from "react-dom/client"; 4 | 5 | import { ReactFlowProvider } from "reactflow"; 6 | 7 | import { ChakraProvider } from "@chakra-ui/react"; 8 | 9 | import mixpanel from "mixpanel-browser"; 10 | 11 | import App from "./components/App"; 12 | 13 | import "./index.css"; 14 | 15 | export const MIXPANEL_TOKEN = import.meta.env.VITE_MIXPANEL_TOKEN; 16 | 17 | if (MIXPANEL_TOKEN) mixpanel.init(MIXPANEL_TOKEN); 18 | 19 | ReactDOM.createRoot(document.getElementById("root") as HTMLElement).render( 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | ); 28 | -------------------------------------------------------------------------------- /src/utils/lstore.ts: -------------------------------------------------------------------------------- 1 | import { useState, useEffect } from "react"; 2 | 3 | export function readLocalStorage(key: string): T | null { 4 | const storedValue = localStorage.getItem(key); 5 | return storedValue ? JSON.parse(storedValue) : null; 6 | } 7 | 8 | export function writeLocalStorage(key: string, value: any) { 9 | localStorage.setItem(key, JSON.stringify(value)); 10 | } 11 | 12 | export function useLocalStorage( 13 | key: string 14 | ): [T | null, React.Dispatch>] { 15 | const [value, setValue] = useState(() => readLocalStorage(key)); 16 | 17 | useEffect(() => setValue(readLocalStorage(key)), [key]); 18 | 19 | useEffect(() => writeLocalStorage(key, value), [key, value]); 20 | 21 | return [value, setValue]; 22 | } 23 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | Flux 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /src/utils/types.ts: -------------------------------------------------------------------------------- 1 | import { Node, Edge } from "reactflow"; 2 | 3 | import { ChatCompletionResponseMessage } from "openai-streams"; 4 | 5 | export type FluxNodeData = { 6 | label: string; 7 | fluxNodeType: FluxNodeType; 8 | text: string; 9 | generating: boolean; 10 | }; 11 | 12 | export enum FluxNodeType { 13 | System = "System", 14 | User = "User", 15 | GPT = "GPT", 16 | TweakedGPT = "GPT (tweaked)", 17 | } 18 | 19 | export type Settings = { 20 | defaultPreamble: string; 21 | autoZoom: boolean; 22 | model: string; 23 | temp: number; 24 | n: number; 25 | }; 26 | 27 | export enum ReactFlowNodeTypes { 28 | LabelUpdater = "LabelUpdater", 29 | } 30 | 31 | // The stream response is weird and has a delta instead of message field. 32 | export interface CreateChatCompletionStreamResponseChoicesInner { 33 | index?: number; 34 | delta?: ChatCompletionResponseMessage; 35 | finish_reason?: string; 36 | } 37 | 38 | export type HistoryItem = { 39 | nodes: Node[]; 40 | edges: Edge[]; 41 | selectedNodeId: string | null; 42 | lastSelectedNodeId: string | null; 43 | }; 44 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 t11s 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/utils/color.ts: -------------------------------------------------------------------------------- 1 | import { FluxNodeType } from "./types"; 2 | 3 | export function adjustColor(color: string, amount: number) { 4 | return ( 5 | "#" + 6 | color 7 | .replace(/^#/, "") 8 | .replace(/../g, (color) => 9 | ( 10 | "0" + Math.min(255, Math.max(0, parseInt(color, 16) + amount)).toString(16) 11 | ).substr(-2) 12 | ) 13 | ); 14 | } 15 | 16 | export function getFluxNodeTypeColor(fluxNodeType: FluxNodeType) { 17 | switch (fluxNodeType) { 18 | case FluxNodeType.User: 19 | return "#EEEEEE"; 20 | case FluxNodeType.GPT: 21 | return "#d9f3d6"; 22 | case FluxNodeType.TweakedGPT: 23 | return "#f7d0a1"; 24 | case FluxNodeType.System: 25 | return "#C5E2F6"; 26 | } 27 | } 28 | 29 | export function getFluxNodeTypeDarkColor(fluxNodeType: FluxNodeType) { 30 | switch (fluxNodeType) { 31 | case FluxNodeType.User: 32 | return "#A9ABAE"; 33 | case FluxNodeType.GPT: 34 | return "#619F83"; 35 | case FluxNodeType.TweakedGPT: 36 | return "#CB7937"; 37 | case FluxNodeType.System: 38 | return "#5F8AF7"; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "flux", 3 | "private": true, 4 | "version": "0.0.0", 5 | "type": "module", 6 | "scripts": { 7 | "dev": "vite", 8 | "build": "tsc && vite build", 9 | "preview": "vite preview" 10 | }, 11 | "dependencies": { 12 | "@chakra-ui/icons": "^2.0.17", 13 | "@chakra-ui/react": "^2.5.1", 14 | "@emotion/react": "^11.10.6", 15 | "@emotion/styled": "^11.10.6", 16 | "fast-deep-equal": "^3.1.3", 17 | "framer-motion": "^9.0.4", 18 | "mixpanel-browser": "^2.46.0", 19 | "openai": "^3.1.0", 20 | "openai-streams": "^4.2.0", 21 | "re-resizable": "^6.9.9", 22 | "react": "^18.2.0", 23 | "react-beforeunload": "^2.5.3", 24 | "react-dom": "^18.2.0", 25 | "react-hotkeys-hook": "^4.3.7", 26 | "react-syntax-highlighter": "^15.5.0", 27 | "react-textarea-autosize": "^8.4.0", 28 | "reactflow": "^11.5.6", 29 | "yield-stream": "^2.3.0" 30 | }, 31 | "devDependencies": { 32 | "@types/mixpanel-browser": "^2.38.1", 33 | "@types/node": "^18.14.2", 34 | "@types/react": "^18.0.27", 35 | "@types/react-beforeunload": "^2.1.1", 36 | "@types/react-dom": "^18.0.10", 37 | "@types/react-syntax-highlighter": "^15.5.6", 38 | "@vitejs/plugin-react-swc": "^3.0.0", 39 | "typescript": "^4.9.3", 40 | "vite": "^4.1.0" 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/utils/fluxEdge.ts: -------------------------------------------------------------------------------- 1 | import { Edge } from "reactflow"; 2 | 3 | /*////////////////////////////////////////////////////////////// 4 | CONSTRUCTORS 5 | //////////////////////////////////////////////////////////////*/ 6 | 7 | export function newFluxEdge({ 8 | source, 9 | target, 10 | animated, 11 | }: { 12 | source: string; 13 | target: string; 14 | animated: boolean; 15 | }): Edge { 16 | return { 17 | id: `${source}-${target}`, 18 | source, 19 | target, 20 | animated, 21 | }; 22 | } 23 | 24 | /*////////////////////////////////////////////////////////////// 25 | TRANSFORMERS 26 | //////////////////////////////////////////////////////////////*/ 27 | 28 | export function addFluxEdge( 29 | existingEdges: Edge[], 30 | { source, target, animated }: { source: string; target: string; animated: boolean } 31 | ): Edge[] { 32 | const newEdge = newFluxEdge({ source, target, animated }); 33 | 34 | return [...existingEdges, newEdge]; 35 | } 36 | 37 | export function modifyFluxEdge( 38 | existingEdges: Edge[], 39 | { source, target, animated }: { source: string; target: string; animated: boolean } 40 | ): Edge[] { 41 | return existingEdges.map((edge) => { 42 | if (edge.id !== `${source}-${target}`) return edge; 43 | 44 | const copy = { ...edge }; 45 | 46 | copy.animated = animated; 47 | 48 | return copy; 49 | }); 50 | } 51 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | Flux 3 | 4 | LLM power tool 5 | 6 | 7 | Announcement 8 | · 9 | Try Online 10 | · 11 | Report a Bug 12 | 13 | 14 | 15 | 16 | 17 |  18 | 19 | ## About 20 | 21 | Flux is a power tool for interacting with large language models (LLMs) that **generates multiple completions per prompt in a tree structure and lets you explore the best ones in parallel.** 22 | 23 | Flux's tree structure allows you to: 24 | 25 | - Get a wider variety of creative responses 26 | 27 | - Test out different prompts with the same shared context 28 | 29 | - Use inconsistencies to identify where the model is uncertain 30 | 31 | It also provides a robust set of keyboard shortcuts, allows setting the system message and editing GPT messages, autosaves to local storage, uses the OpenAI API directly, and is 100% open source and MIT licensed. 32 | 33 | ## Usage 34 | 35 | Visit [flux.paradigm.xyz](https://flux.paradigm.xyz) to try Flux online or follow the instructions below to run it locally. 36 | 37 | ## Running Locally 38 | 39 | ```sh 40 | git clone https://github.com/transmissions11/flux.git 41 | npm install 42 | npm run dev 43 | ``` 44 | 45 | ## Contributing 46 | 47 | See the [open issues](https://github.com/transmissions11/flux/issues) for a list of proposed features (and known issues). 48 | -------------------------------------------------------------------------------- /src/components/modals/APIKeyModal.tsx: -------------------------------------------------------------------------------- 1 | import mixpanel from "mixpanel-browser"; 2 | 3 | import { Modal, ModalOverlay, ModalContent, Link, Text } from "@chakra-ui/react"; 4 | 5 | import { MIXPANEL_TOKEN } from "../../main"; 6 | 7 | import { Column } from "../../utils/chakra"; 8 | import { isValidAPIKey } from "../../utils/apikey"; 9 | import { APIKeyInput } from "../utils/APIKeyInput"; 10 | 11 | export function APIKeyModal({ 12 | apiKey, 13 | setApiKey, 14 | }: { 15 | apiKey: string | null; 16 | setApiKey: (apiKey: string) => void; 17 | }) { 18 | const setApiKeyTracked = (apiKey: string) => { 19 | setApiKey(apiKey); 20 | 21 | if (isValidAPIKey(apiKey)) { 22 | if (MIXPANEL_TOKEN) mixpanel.track("Entered API Key"); 23 | 24 | // Hacky way to get the prompt box to focus after the 25 | // modal closes. Long term should probably use a ref. 26 | setTimeout(() => window.document.getElementById("promptBox")?.focus(), 50); 27 | } 28 | }; 29 | 30 | return ( 31 | {}} 34 | size="3xl" 35 | isCentered={true} 36 | motionPreset="none" 37 | > 38 | 39 | 40 | 41 | 42 | 43 | We will never upload, log, or store your API key outside of your 44 | browser's local storage. Verify for yourself{" "} 45 | 46 | here 47 | 48 | . 49 | 50 | 51 | 52 | 53 | ); 54 | } 55 | -------------------------------------------------------------------------------- /src/utils/constants.ts: -------------------------------------------------------------------------------- 1 | import { UseToastOptions } from "@chakra-ui/toast"; 2 | 3 | import { Options } from "react-hotkeys-hook"; 4 | 5 | import { NodeProps } from "reactflow"; 6 | 7 | import { ReactFlowNodeTypes, Settings } from "./types"; 8 | 9 | import { LabelUpdaterNode } from "../components/nodes/LabelUpdaterNode"; 10 | 11 | export const REACT_FLOW_NODE_TYPES: Record< 12 | ReactFlowNodeTypes, 13 | (args: NodeProps) => JSX.Element 14 | > = { 15 | LabelUpdater: LabelUpdaterNode, 16 | }; 17 | 18 | export const SUPPORTED_MODELS = ["gpt-3.5-turbo", "gpt-4"]; 19 | 20 | export const DEFAULT_SETTINGS: Settings = { 21 | temp: 1.2, 22 | n: 3, 23 | autoZoom: true, 24 | model: "gpt-3.5-turbo", 25 | defaultPreamble: `You are ChatGPT, a large language model trained by OpenAI. Answer as concisely as possible. Knowledge cutoff: 2021-09 Current date: ${ 26 | new Date().toISOString().split("T")[0] 27 | }`, 28 | }; 29 | 30 | export const HOTKEY_CONFIG: Options = { 31 | preventDefault: true, 32 | enableOnFormTags: true, 33 | }; 34 | 35 | export const TOAST_CONFIG: UseToastOptions = { 36 | isClosable: true, 37 | variant: "left-accent", 38 | position: "bottom-left", 39 | }; 40 | 41 | export const MAX_HISTORY_SIZE = 256; 42 | 43 | export const OVERLAP_RANDOMNESS_MAX = 20; 44 | 45 | export const API_KEY_LOCAL_STORAGE_KEY = "FLUX_OPENAI_API_KEY"; 46 | export const REACT_FLOW_LOCAL_STORAGE_KEY = "FLUX_REACT_FLOW_DATA"; 47 | export const MODEL_SETTINGS_LOCAL_STORAGE_KEY = "FLUX_MODEL_SETTINGS"; 48 | 49 | export const NEW_TREE_CONTENT_QUERY_PARAM = "newTreeWith"; 50 | 51 | export const UNDEFINED_RESPONSE_STRING = "[UNDEFINED RESPONSE]"; 52 | 53 | export const FIT_VIEW_SETTINGS = { padding: 0.1, duration: 200 }; 54 | 55 | export const NEW_TREE_X_OFFSET = 600; 56 | 57 | export const CODE_BLOCK_DETECT_REGEX = 58 | /\s*(```(?:[a-zA-Z0-9-]*\n|\n?)([\s\S]+?)\n```)\s*/; 59 | export const CODE_BLOCK_LANGUAGE_DETECT_REGEX = /^```[a-zA-Z0-9-]*$/m; 60 | -------------------------------------------------------------------------------- /src/utils/prompt.ts: -------------------------------------------------------------------------------- 1 | import { Node } from "reactflow"; 2 | 3 | import { ChatCompletionRequestMessage } from "openai-streams"; 4 | 5 | import { FluxNodeData, FluxNodeType, Settings } from "./types"; 6 | 7 | export function messagesFromLineage( 8 | lineage: Node[], 9 | settings: Settings 10 | ): ChatCompletionRequestMessage[] { 11 | const messages: ChatCompletionRequestMessage[] = []; 12 | 13 | // Iterate backwards. 14 | for (let i = lineage.length - 1; i >= 0; i--) { 15 | const node = lineage[i]; 16 | 17 | if (node.data.fluxNodeType === FluxNodeType.System) { 18 | messages.push({ 19 | role: "system", 20 | content: node.data.text, 21 | }); 22 | } else if (i === lineage.length - 1) { 23 | // If this is the first node and it's 24 | // not a system node, we'll push the 25 | // default preamble on there. 26 | messages.push({ 27 | role: "system", 28 | content: settings.defaultPreamble, 29 | }); 30 | } 31 | 32 | if (node.data.fluxNodeType === FluxNodeType.User) { 33 | messages.push({ 34 | role: "user", 35 | content: node.data.text, 36 | }); 37 | } else if ( 38 | node.data.fluxNodeType === FluxNodeType.TweakedGPT || 39 | node.data.fluxNodeType === FluxNodeType.GPT 40 | ) { 41 | messages.push({ 42 | role: "assistant", 43 | content: node.data.text, 44 | }); 45 | } 46 | } 47 | 48 | console.table(messages); 49 | 50 | return messages; 51 | } 52 | 53 | export function promptFromLineage( 54 | lineage: Node[], 55 | settings: Settings, 56 | endWithNewlines: boolean = false 57 | ): string { 58 | const messages = messagesFromLineage(lineage, settings); 59 | 60 | let prompt = ""; 61 | 62 | messages.forEach((message, i) => { 63 | prompt += `${message.role}: ${message.content}`; 64 | 65 | if (endWithNewlines ? true : i !== messages.length - 1) { 66 | prompt += "\n\n"; 67 | } 68 | }); 69 | 70 | console.log(prompt); 71 | 72 | return prompt; 73 | } 74 | -------------------------------------------------------------------------------- /public/paradigm-full.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/components/nodes/LabelUpdaterNode.tsx: -------------------------------------------------------------------------------- 1 | import { useEffect, useState } from "react"; 2 | 3 | import { Box, Input } from "@chakra-ui/react"; 4 | 5 | import { Handle, Position, useReactFlow } from "reactflow"; 6 | 7 | import { Row } from "../../utils/chakra"; 8 | import { FluxNodeData } from "../../utils/types"; 9 | import { modifyFluxNodeLabel, modifyReactFlowNodeProperties } from "../../utils/fluxNode"; 10 | 11 | export function LabelUpdaterNode({ 12 | id, 13 | data, 14 | isConnectable, 15 | }: { 16 | id: string; 17 | data: FluxNodeData; 18 | isConnectable: boolean; 19 | }) { 20 | const { setNodes } = useReactFlow(); 21 | 22 | const [renameLabel, setRenameLabel] = useState(data.label); 23 | 24 | // Select the input element on mount. 25 | useEffect(() => { 26 | const input = document.getElementById("renameInput") as HTMLInputElement | null; 27 | 28 | // Have to do this with a bit of a delay to 29 | // ensure it works when triggered via navbar. 30 | setTimeout(() => input?.select(), 50); 31 | }, []); 32 | 33 | const cancel = () => { 34 | setNodes((nodes) => 35 | // Reset the node type to the default 36 | // type and make it draggable again. 37 | modifyReactFlowNodeProperties(nodes, { 38 | id, 39 | type: undefined, 40 | draggable: true, 41 | }) 42 | ); 43 | }; 44 | 45 | const submit = () => { 46 | setNodes((nodes) => 47 | modifyFluxNodeLabel(nodes, { 48 | id, 49 | label: renameLabel, 50 | }) 51 | ); 52 | }; 53 | 54 | return ( 55 | 56 | 57 | 58 | 59 | setRenameLabel(e.target.value)} 64 | onKeyDown={(e) => e.key === "Enter" && submit()} 65 | className="nodrag" // https://reactflow.dev/docs/api/nodes/custom-nodes/#prevent-dragging--selecting 66 | textAlign="center" 67 | size="xs" 68 | // px={6} 69 | /> 70 | 71 | {/* 80 | 87 | */} 88 | 89 | 90 | 91 | 92 | ); 93 | } 94 | -------------------------------------------------------------------------------- /src/components/utils/TextAndCodeBlock.tsx: -------------------------------------------------------------------------------- 1 | import { MouseEvent, useState, useEffect, memo } from "react"; 2 | 3 | import { Prism as SyntaxHighlighter } from "react-syntax-highlighter"; 4 | import { coy } from "react-syntax-highlighter/dist/esm/styles/prism"; 5 | 6 | import { Button, Box, Text } from "@chakra-ui/react"; 7 | 8 | import { CopyIcon } from "@chakra-ui/icons"; 9 | 10 | import { 11 | CODE_BLOCK_DETECT_REGEX, 12 | CODE_BLOCK_LANGUAGE_DETECT_REGEX, 13 | } from "../../utils/constants"; 14 | import { Row } from "../../utils/chakra"; 15 | import { copySnippetToClipboard } from "../../utils/clipboard"; 16 | 17 | const CopyCodeButton = ({ code }: { code: string }) => { 18 | const [copied, setCopied] = useState(false); 19 | 20 | const handleCopyButtonClick = async (e: MouseEvent) => { 21 | e.stopPropagation(); // Prevent this from triggering edit mode in the parent. 22 | 23 | if (await copySnippetToClipboard(code)) setCopied(true); 24 | }; 25 | 26 | useEffect(() => { 27 | if (copied) { 28 | const timer = setTimeout(() => setCopied(false), 2000); 29 | return () => clearTimeout(timer); 30 | } 31 | }, [copied]); 32 | 33 | return ( 34 | 41 | {copied ? "Copied!" : "Copy Code"} 42 | 43 | ); 44 | }; 45 | 46 | const TitleBar = ({ language, code }: { language?: string; code: string }) => { 47 | return ( 48 | 60 | {language || "plaintext"} 61 | 62 | 63 | ); 64 | }; 65 | 66 | export const TextAndCodeBlock = memo(({ text }: { text: string }) => { 67 | let remainingText = text; 68 | 69 | const elements: React.ReactNode[] = []; 70 | 71 | while (remainingText.length > 0) { 72 | const match = CODE_BLOCK_DETECT_REGEX.exec(remainingText); 73 | 74 | if (!match) { 75 | elements.push({remainingText}); 76 | break; 77 | } 78 | 79 | const before = remainingText.substring(0, match.index); 80 | const language = 81 | CODE_BLOCK_LANGUAGE_DETECT_REGEX.exec(match[1])?.[0]?.substring(3) || "plaintext"; 82 | const code = match[2].trim(); 83 | const after = remainingText.substring(match.index + match[0].length); 84 | 85 | if (before.length > 0) 86 | elements.push( 87 | 88 | {before} 89 | 90 | ); 91 | 92 | elements.push( 93 | 94 | 95 | 102 | {code} 103 | 104 | 105 | ); 106 | 107 | remainingText = after; 108 | } 109 | 110 | return {elements}; 111 | }); 112 | -------------------------------------------------------------------------------- /src/components/modals/SettingsModal.tsx: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | 3 | import { 4 | Button, 5 | Text, 6 | Modal, 7 | ModalBody, 8 | ModalCloseButton, 9 | ModalContent, 10 | ModalFooter, 11 | ModalHeader, 12 | ModalOverlay, 13 | Checkbox, 14 | } from "@chakra-ui/react"; 15 | 16 | import { LabeledSelect, LabeledSlider } from "../utils/LabeledInputs"; 17 | 18 | import { APIKeyInput } from "../utils/APIKeyInput"; 19 | import { Settings, FluxNodeType } from "../../utils/types"; 20 | import { getFluxNodeTypeDarkColor } from "../../utils/color"; 21 | import { DEFAULT_SETTINGS, SUPPORTED_MODELS } from "../../utils/constants"; 22 | 23 | export const SettingsModal = React.memo(function SettingsModal({ 24 | isOpen, 25 | onClose, 26 | settings, 27 | setSettings, 28 | apiKey, 29 | setApiKey, 30 | }: { 31 | isOpen: boolean; 32 | onClose: () => void; 33 | settings: Settings; 34 | setSettings: (settings: Settings) => void; 35 | apiKey: string | null; 36 | setApiKey: (apiKey: string) => void; 37 | }) { 38 | const reset = () => 39 | confirm( 40 | "Are you sure you want to reset your settings to default? This cannot be undone!" 41 | ) && setSettings(DEFAULT_SETTINGS); 42 | 43 | const hardReset = () => { 44 | if ( 45 | confirm( 46 | "Are you sure you want to delete ALL data (including your saved API key, conversations, etc?) This cannot be undone!" 47 | ) && 48 | confirm( 49 | "Are you 100% sure? Reminder this cannot be undone and you will lose EVERYTHING!" 50 | ) 51 | ) { 52 | // Clear local storage. 53 | localStorage.clear(); 54 | 55 | // Ensure that the page is reloaded even if there are unsaved changes. 56 | window.onbeforeunload = null; 57 | 58 | // Reload the window. 59 | window.location.reload(); 60 | } 61 | }; 62 | 63 | return ( 64 | 65 | 66 | 67 | Settings 68 | 69 | 70 | setSettings({ ...settings, model: v })} 75 | /> 76 | 77 | 78 | 79 | setSettings({ ...settings, temp: v })} 84 | color={getFluxNodeTypeDarkColor(FluxNodeType.User)} 85 | max={1.25} 86 | min={0} 87 | step={0.01} 88 | /> 89 | 90 | setSettings({ ...settings, n: v })} 95 | color={getFluxNodeTypeDarkColor(FluxNodeType.User)} 96 | max={10} 97 | min={1} 98 | step={1} 99 | /> 100 | 101 | 107 | setSettings({ ...settings, autoZoom: event.target.checked }) 108 | } 109 | > 110 | Auto Zoom 111 | 112 | 113 | 114 | 115 | 116 | Restore Defaults 117 | 118 | 119 | 120 | Hard Reset 121 | 122 | 123 | 124 | 125 | ); 126 | }); 127 | -------------------------------------------------------------------------------- /src/components/utils/LabeledInputs.tsx: -------------------------------------------------------------------------------- 1 | import { useEffect, useState } from "react"; 2 | 3 | import { 4 | Box, 5 | BoxProps, 6 | Button, 7 | Input, 8 | InputGroup, 9 | InputRightElement, 10 | Link, 11 | Select, 12 | Slider, 13 | SliderFilledTrack, 14 | SliderThumb, 15 | SliderTrack, 16 | Textarea, 17 | } from "@chakra-ui/react"; 18 | import { Row } from "../../utils/chakra"; 19 | import { ExternalLinkIcon } from "@chakra-ui/icons"; 20 | 21 | export function LabeledSlider({ 22 | label, 23 | value, 24 | setValue, 25 | color, 26 | max, 27 | min, 28 | step, 29 | ...others 30 | }: { 31 | label: string; 32 | value: number; 33 | setValue: (value: number) => void; 34 | color: string; 35 | max: number; 36 | min: number; 37 | step: number; 38 | } & BoxProps) { 39 | return ( 40 | 41 | {label}: {value} 42 | setValue(v)} 47 | max={max} 48 | min={min} 49 | step={step} 50 | > 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | ); 59 | } 60 | 61 | export function LabeledInput({ 62 | label, 63 | value, 64 | setValue, 65 | ...others 66 | }: { 67 | label: string; 68 | value: string; 69 | setValue: (value: string) => void; 70 | } & BoxProps) { 71 | return ( 72 | 73 | {label}: 74 | setValue(e.target.value)} value={value} /> 75 | 76 | ); 77 | } 78 | 79 | export function LabeledPasswordInputWithLink({ 80 | label, 81 | linkLabel, 82 | placeholder, 83 | link, 84 | value, 85 | setValue, 86 | ...others 87 | }: { 88 | label: string; 89 | linkLabel: string; 90 | placeholder?: string; 91 | link: string; 92 | value: string; 93 | setValue: (value: string) => void; 94 | } & BoxProps) { 95 | const [show, setShow] = useState(false); 96 | 97 | return ( 98 | 99 | 100 | {label}: 101 | 108 | {linkLabel} 109 | 110 | 111 | 112 | 113 | setValue(e.target.value)} 118 | /> 119 | 120 | setShow(!show)} 127 | > 128 | {show ? "Hide" : "Show"} 129 | 130 | 131 | 132 | 133 | ); 134 | } 135 | 136 | export function LabeledTextArea({ 137 | label, 138 | value, 139 | setValue, 140 | textAreaId, 141 | ...others 142 | }: { 143 | label: string; 144 | value: string; 145 | textAreaId: string | undefined; 146 | setValue: (value: string) => void; 147 | } & BoxProps) { 148 | return ( 149 | 150 | {label}: 151 | setValue(e.target.value)} 155 | value={value} 156 | id={textAreaId} 157 | placeholder="Enter text here..." 158 | /> 159 | 160 | ); 161 | } 162 | 163 | export function SelfSelectingLabeledTextArea({ 164 | label, 165 | value, 166 | setValue, 167 | id, 168 | ...others 169 | }: { 170 | label: string; 171 | value: string; 172 | id: string; 173 | setValue: (value: string) => void; 174 | } & BoxProps) { 175 | useEffect( 176 | () => (window.document.getElementById(id) as HTMLTextAreaElement | null)?.select(), 177 | [] 178 | ); 179 | 180 | return ( 181 | 188 | ); 189 | } 190 | 191 | export function LabeledSelect({ 192 | label, 193 | value, 194 | setValue, 195 | options, 196 | ...others 197 | }: { 198 | label: string; 199 | value: string; 200 | options: string[]; 201 | setValue: (value: string) => void; 202 | } & BoxProps) { 203 | return ( 204 | 205 | {label}: 206 | setValue(e.target.value)} value={value}> 207 | {options.map((o) => ( 208 | {o} 209 | ))} 210 | 211 | 212 | ); 213 | } 214 | -------------------------------------------------------------------------------- /public/paradigm.svg: -------------------------------------------------------------------------------- 1 | 2 | 4 | 7 | 8 | Created by potrace 1.11, written by Peter Selinger 2001-2013 9 | 10 | 12 | 84 | 85 | 86 | -------------------------------------------------------------------------------- /src/components/utils/NavigationBar.tsx: -------------------------------------------------------------------------------- 1 | import { 2 | Menu, 3 | MenuButton, 4 | Button, 5 | MenuList, 6 | MenuGroup, 7 | MenuItem, 8 | MenuDivider, 9 | Box, 10 | Text, 11 | Avatar, 12 | AvatarGroup, 13 | Link, 14 | } from "@chakra-ui/react"; 15 | import { ChevronDownIcon } from "@chakra-ui/icons"; 16 | 17 | import { Row } from "../../utils/chakra"; 18 | import { FluxNodeType } from "../../utils/types"; 19 | 20 | import dave from "/dave.jpg"; 21 | import t11s from "/t11s.jpg"; 22 | import paradigm from "/paradigm.svg"; 23 | 24 | export function NavigationBar({ 25 | newUserNodeLinkedToANewSystemNode, 26 | newConnectedToSelectedNode, 27 | submitPrompt, 28 | regenerate, 29 | completeNextWords, 30 | undo, 31 | redo, 32 | onClear, 33 | copyMessagesToClipboard, 34 | showRenameInput, 35 | deleteSelectedNodes, 36 | moveToParent, 37 | moveToChild, 38 | moveToLeftSibling, 39 | moveToRightSibling, 40 | autoZoom, 41 | onOpenSettingsModal, 42 | }: { 43 | newUserNodeLinkedToANewSystemNode: () => void; 44 | newConnectedToSelectedNode: (nodeType: FluxNodeType) => void; 45 | submitPrompt: () => void; 46 | regenerate: () => void; 47 | completeNextWords: () => void; 48 | deleteSelectedNodes: () => void; 49 | undo: () => void; 50 | redo: () => void; 51 | onClear: () => void; 52 | copyMessagesToClipboard: () => void; 53 | showRenameInput: () => void; 54 | moveToParent: () => void; 55 | moveToChild: () => void; 56 | moveToLeftSibling: () => void; 57 | moveToRightSibling: () => void; 58 | autoZoom: () => void; 59 | onOpenSettingsModal: () => void; 60 | }) { 61 | return ( 62 | 69 | 75 | 76 | Flux by 77 | 78 | 79 | 80 | 89 | 98 | 107 | 108 | 109 | 110 | 111 | 112 | } 115 | variant="ghost" 116 | height="80%" 117 | px="5px" 118 | > 119 | File 120 | 121 | 122 | 123 | 124 | New conversation tree 125 | 126 | 127 | 128 | 129 | 130 | 131 | newConnectedToSelectedNode(FluxNodeType.User)} 134 | > 135 | New user node 136 | 137 | 138 | newConnectedToSelectedNode(FluxNodeType.System)} 141 | > 142 | New system node 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | Generate GPT responses 151 | 152 | 153 | 154 | Regenerate GPT responses 155 | 156 | 157 | 158 | Complete next words 159 | 160 | 161 | 162 | 163 | 164 | } 167 | variant="ghost" 168 | height="80%" 169 | px="5px" 170 | ml="11px" 171 | > 172 | Edit 173 | 174 | 175 | 176 | 177 | Undo 178 | 179 | 180 | 181 | Redo 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | Delete selected node(s) 190 | 191 | 192 | 193 | Delete everything 194 | 195 | 196 | 197 | 198 | 199 | 200 | 201 | Rename selected node 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | Copy messages to clipboard 210 | 211 | 212 | 213 | 214 | 215 | } 218 | variant="ghost" 219 | height="80%" 220 | px="5px" 221 | ml="11px" 222 | > 223 | Navigate 224 | 225 | 226 | 227 | 228 | Up to parent node 229 | 230 | 231 | Down child node 232 | 233 | 234 | 235 | 236 | 237 | 238 | 239 | Left to sibling node 240 | 241 | 242 | Right to sibling node 243 | 244 | 245 | 246 | 247 | 248 | 249 | 250 | Zoom out & center 251 | 252 | 253 | 254 | 255 | 262 | Settings 263 | 264 | 272 | About 273 | 274 | 275 | 276 | ); 277 | } 278 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | ## Contributing to Flux 2 | 3 | Thanks for your interest in improving Flux! 4 | 5 | There are multiple opportunities to contribute at any level. It doesn't matter if you are just getting started with Rust or are the most weathered expert, we can use your help. 6 | 7 | **No contribution is too small and all contributions are valued.** 8 | 9 | This document will help you get started. **Do not let the document intimidate you**. 10 | It should be considered as a guide to help you navigate the process. 11 | 12 | ### Code of Conduct 13 | 14 | Flux adheres to the [Rust Code of Conduct](https://www.rust-lang.org/policies/code-of-conduct) (even though it's not a Rust project). This code of conduct describes the _minimum_ behavior expected from all contributors. 15 | 16 | Instances of violations of the Code of Conduct can be reported by contacting the team at [t11s@paradigm.xyz](mailto:t11s@paradigm.xyz). 17 | 18 | ### Ways to contribute 19 | 20 | There are fundamentally four ways an individual can contribute: 21 | 22 | 1. **By opening an issue:** For example, if you believe that you have uncovered a bug 23 | in Flux, creating a new issue in the issue tracker is the way to report it. 24 | 2. **By adding context:** Providing additional context to existing issues, 25 | such as screenshots, code snippets and helps resolve issues. 26 | 3. **By resolving issues:** Typically this is done in the form of either 27 | demonstrating that the issue reported is not a problem after all, or more often, 28 | by opening a pull request that fixes the underlying problem, in a concrete and 29 | reviewable manner. 30 | 31 | **Anybody can participate in any stage of contribution**. We urge you to participate in the discussion 32 | around bugs and participate in reviewing PRs. 33 | 34 | ### Submitting a bug report 35 | 36 | When filing a new bug report in the issue tracker, you will be presented with a basic form to fill out. 37 | 38 | If you believe that you have uncovered a bug, please fill out the form to the best of your ability. Do not worry if you cannot answer every detail, 39 | just fill in what you can. Contributors will ask follow-up questions if something is unclear. 40 | 41 | The most important pieces of information we need in a bug report are: 42 | 43 | - The OS you are on (Windows, macOS, or Linux) 44 | - The Browser you have (Chrome, Safari, etc) 45 | - Concrete steps to reproduce the bug 46 | - What you expect to happen instead 47 | 48 | ### Submitting a feature request 49 | 50 | When adding a feature request in the issue tracker, you will be presented with a basic form to fill out. 51 | 52 | Please include as detailed of an explanation as possible of the feature you would like, adding additional context if necessary. 53 | 54 | If you have examples of other tools that have the feature you are requesting, please include them as well. 55 | 56 | ### Resolving an issue 57 | 58 | Pull requests are the way concrete changes are made to the code and dependencies of Flux. 59 | 60 | Even tiny pull requests, like fixing wording, are greatly appreciated. Before making a large change, it is usually 61 | a good idea to first open an issue describing the change to solicit feedback and guidance. This will increase 62 | the likelihood of the PR getting merged. 63 | 64 | #### Commits 65 | 66 | It is a recommended best practice to keep your changes as logically grouped as possible within individual commits. There is no limit to the number of commits any single pull request may have, and many contributors find it easier to review changes that are split across multiple commits. 67 | 68 | That said, if you have a number of commits that are "checkpoints" and don't represent a single logical change, please squash those together. 69 | 70 | #### Opening the pull request 71 | 72 | From within GitHub, opening a new pull request will present you with a template that should be filled out. Please try your best at filling out the details, but feel free to skip parts if you're not sure what to put. 73 | 74 | #### Discuss and update 75 | 76 | You will probably get feedback or requests for changes to your pull request. 77 | This is a big part of the submission process, so don't be discouraged! Some contributors may sign off on the pull request right away, others may have more detailed comments or feedback. 78 | This is a necessary part of the process in order to evaluate whether the changes are correct and necessary. 79 | 80 | **Any community member can review a PR, so you might get conflicting feedback**. 81 | Keep an eye out for comments from code owners to provide guidance on conflicting feedback. 82 | 83 | #### Reviewing pull requests 84 | 85 | **Any Flux community member is welcome to review any pull request**. 86 | 87 | All contributors who choose to review and provide feedback on pull requests have a responsibility to both the project and individual making the contribution. Reviews and feedback must be helpful, insightful, and geared towards improving the contribution as opposed to simply blocking it. If there are reasons why you feel the PR should not be merged, explain what those are. Do not expect to be able to block a PR from advancing simply because you say "no" without giving an explanation. Be open to having your mind changed. Be open to working _with_ the contributor to make the pull request better. 88 | 89 | Reviews that are dismissive or disrespectful of the contributor or any other reviewers are strictly counter to the Code of Conduct. 90 | 91 | When reviewing a pull request, the primary goals are for the codebase to improve and for the person submitting the request to succeed. **Even if a pull request is not merged, the submitter should come away from the experience feeling like their effort was not unappreciated**. Every PR from a new contributor is an opportunity to grow the community. 92 | 93 | ##### Review a bit at a time 94 | 95 | Do not overwhelm new contributors. 96 | 97 | It is tempting to micro-optimize and make everything about relative performance, perfect grammar, or exact style matches. Do not succumb to that temptation.. 98 | 99 | Focus first on the most significant aspects of the change: 100 | 101 | 1. Does this change make sense for Flux? 102 | 2. Does this change make Flux better, even if only incrementally? 103 | 3. Are there clear bugs or larger scale issues that need attending? 104 | 4. Are the commit messages readable and correct? If it contains a breaking change, is it clear enough? 105 | 106 | Note that only **incremental** improvement is needed to land a PR. This means that the PR does not need to be perfect, only better than the status quo. Follow-up PRs may be opened to continue iterating. 107 | 108 | When changes are necessary, _request_ them, do not _demand_ them, and **do not assume that the submitter already knows how to add a test or run a benchmark**. 109 | 110 | Specific performance optimization techniques, coding styles and conventions change over time. The first impression you give to a new contributor never does. 111 | 112 | Nits (requests for small changes that are not essential) are fine, but try to avoid stalling the pull request. Most nits can typically be fixed by the Flux maintainers merging the pull request, but they can also be an opportunity for the contributor to learn a bit more about the project. 113 | 114 | It is always good to clearly indicate nits when you comment, e.g.: `nit: change foo() to bar(). But this is not blocking`. 115 | 116 | If your comments were addressed but were not folded after new commits, or if they proved to be mistaken, please, hide them with the appropriate reason to keep the conversation flow concise and relevant. 117 | 118 | ##### Be aware of the person behind the code 119 | 120 | Be aware that _how_ you communicate requests and reviews in your feedback can have a significant impact on the success of the pull request. Yes, we may merge a particular change that makes Flux better, but the individual might just not want to have anything to do with Flux ever again. The goal is not just having good code. 121 | 122 | ##### Abandoned or stale pull requests 123 | 124 | If a pull request appears to be abandoned or stalled, it is polite to first check with the contributor to see if they intend to continue the work before checking if they would mind if you took it over (especially if it just has nits left). When doing so, it is courteous to give the original contributor credit for the work they started, either by preserving their name and e-mail address in the commit log, or by using the `Author: ` or `Co-authored-by: ` metadata tag in the commits. 125 | 126 | _Adapted from the [ethers-rs contributing guide](https://github.com/gakonst/ethers-rs/blob/master/CONTRIBUTING.md)_. 127 | -------------------------------------------------------------------------------- /src/components/Prompt.tsx: -------------------------------------------------------------------------------- 1 | import { Node } from "reactflow"; 2 | 3 | import { useState, useEffect, useRef } from "react"; 4 | 5 | import { Spinner, Text, Button } from "@chakra-ui/react"; 6 | 7 | import { EditIcon, ViewIcon } from "@chakra-ui/icons"; 8 | 9 | import TextareaAutosize from "react-textarea-autosize"; 10 | 11 | import { getFluxNodeTypeColor, getFluxNodeTypeDarkColor } from "../utils/color"; 12 | import { TextAndCodeBlock } from "./utils/TextAndCodeBlock"; 13 | import { FluxNodeData, FluxNodeType, Settings } from "../utils/types"; 14 | import { displayNameFromFluxNodeType } from "../utils/fluxNode"; 15 | import { LabeledSlider } from "./utils/LabeledInputs"; 16 | import { Row, Center, Column } from "../utils/chakra"; 17 | import { BigButton } from "./utils/BigButton"; 18 | 19 | export function Prompt({ 20 | lineage, 21 | submitPrompt, 22 | onType, 23 | selectNode, 24 | newConnectedToSelectedNode, 25 | isGPT4, 26 | settings, 27 | setSettings, 28 | }: { 29 | lineage: Node[]; 30 | onType: (text: string) => void; 31 | submitPrompt: () => Promise; 32 | selectNode: (id: string) => void; 33 | newConnectedToSelectedNode: (type: FluxNodeType) => void; 34 | isGPT4: boolean; 35 | settings: Settings; 36 | setSettings: (settings: Settings) => void; 37 | }) { 38 | const promptNode = lineage[0]; 39 | 40 | const promptNodeType = promptNode.data.fluxNodeType; 41 | 42 | const onMainButtonClick = () => { 43 | if (promptNodeType === FluxNodeType.User) { 44 | submitPrompt(); 45 | } else { 46 | newConnectedToSelectedNode(FluxNodeType.User); 47 | } 48 | }; 49 | 50 | /*////////////////////////////////////////////////////////////// 51 | STATE 52 | //////////////////////////////////////////////////////////////*/ 53 | 54 | const [isEditing, setIsEditing] = useState( 55 | promptNodeType === FluxNodeType.User || promptNodeType === FluxNodeType.System 56 | ); 57 | const [hoveredNodeId, setHoveredNodeId] = useState(null); 58 | 59 | /*////////////////////////////////////////////////////////////// 60 | EFFECTS 61 | //////////////////////////////////////////////////////////////*/ 62 | 63 | const textOffsetRef = useRef(-1); 64 | 65 | // Scroll to the prompt buttons 66 | // when the bottom node is swapped. 67 | useEffect(() => { 68 | window.document 69 | .getElementById("promptButtons") 70 | ?.scrollIntoView(/* { behavior: "smooth" } */); 71 | 72 | // If the user clicked on the node, we assume they want to edit it. 73 | // Otherwise, we only put them in edit mode if its a user or system node. 74 | setIsEditing( 75 | textOffsetRef.current !== -1 || 76 | promptNodeType === FluxNodeType.User || 77 | promptNodeType === FluxNodeType.System 78 | ); 79 | }, [promptNode.id]); 80 | 81 | // Focus the textbox when the user changes into edit mode. 82 | useEffect(() => { 83 | if (isEditing) { 84 | const promptBox = window.document.getElementById( 85 | "promptBox" 86 | ) as HTMLTextAreaElement | null; 87 | 88 | // Focus the text box and move the cursor to chosen offset (defaults to end). 89 | promptBox?.setSelectionRange(textOffsetRef.current, textOffsetRef.current); 90 | promptBox?.focus(); 91 | 92 | // Default to moving to the end of the text. 93 | textOffsetRef.current = -1; 94 | } 95 | }, [promptNode.id, isEditing]); 96 | 97 | /*////////////////////////////////////////////////////////////// 98 | APP 99 | //////////////////////////////////////////////////////////////*/ 100 | 101 | return ( 102 | <> 103 | {lineage 104 | .slice() 105 | .reverse() 106 | .map((node, i) => { 107 | const isLast = i === lineage.length - 1; 108 | 109 | const data = node.data; 110 | 111 | return ( 112 | setHoveredNodeId(node.id)} 125 | onMouseLeave={() => setHoveredNodeId(null)} 126 | bg={getFluxNodeTypeColor(data.fluxNodeType)} 127 | key={node.id} 128 | onClick={ 129 | isLast 130 | ? isEditing 131 | ? undefined 132 | : () => setIsEditing(true) 133 | : () => { 134 | const selection = window.getSelection(); 135 | 136 | // We don't want to trigger the selection 137 | // if they're just selecting/copying text. 138 | if (selection?.isCollapsed) { 139 | // TODO: Note this is basically broken because of codeblocks. 140 | textOffsetRef.current = selection.anchorOffset ?? 0; 141 | 142 | selectNode(node.id); 143 | setIsEditing(true); 144 | } 145 | } 146 | } 147 | cursor={isLast && isEditing ? "text" : "pointer"} 148 | > 149 | {data.generating && data.text === "" ? ( 150 | 151 | 152 | 153 | ) : ( 154 | <> 155 | setIsEditing(!isEditing)} 162 | position="absolute" 163 | top={1} 164 | right={1} 165 | zIndex={10} 166 | variant="outline" 167 | border="0px" 168 | _hover={{ background: "none" }} 169 | p={1} 170 | > 171 | {isEditing ? : } 172 | 173 | 174 | {displayNameFromFluxNodeType(data.fluxNodeType)} 175 | : 176 | 177 | 186 | {isLast && isEditing ? ( 187 | onType(e.target.value)} 196 | placeholder={ 197 | data.fluxNodeType === FluxNodeType.User 198 | ? "Write a poem about..." 199 | : data.fluxNodeType === FluxNodeType.System 200 | ? "You are ChatGPT..." 201 | : undefined 202 | } 203 | /> 204 | ) : ( 205 | 206 | )} 207 | 208 | > 209 | )} 210 | 211 | ); 212 | })} 213 | 214 | 221 | 229 | {promptNodeType === FluxNodeType.User ? "Generate" : "Compose"} 230 | 231 | 232 | {promptNodeType === FluxNodeType.User 233 | ? displayNameFromFluxNodeType(FluxNodeType.GPT, isGPT4) 234 | : displayNameFromFluxNodeType(FluxNodeType.User, isGPT4)} 235 | 236 | 237 | response 238 | 239 | 240 | 241 | {promptNodeType === FluxNodeType.User ? ( 242 | <> 243 | setSettings({ ...settings, temp: v })} 248 | color={getFluxNodeTypeDarkColor(FluxNodeType.User)} 249 | max={1.25} 250 | min={0} 251 | step={0.01} 252 | /> 253 | 254 | setSettings({ ...settings, n: v })} 259 | color={getFluxNodeTypeDarkColor(FluxNodeType.User)} 260 | max={10} 261 | min={1} 262 | step={1} 263 | /> 264 | > 265 | ) : null} 266 | > 267 | ); 268 | } 269 | -------------------------------------------------------------------------------- /src/utils/fluxNode.ts: -------------------------------------------------------------------------------- 1 | import { Node, Edge } from "reactflow"; 2 | 3 | import { NEW_TREE_X_OFFSET, OVERLAP_RANDOMNESS_MAX } from "./constants"; 4 | import { FluxNodeType, FluxNodeData, ReactFlowNodeTypes } from "./types"; 5 | import { getFluxNodeTypeColor } from "./color"; 6 | import { generateNodeId } from "./nodeId"; 7 | 8 | /*////////////////////////////////////////////////////////////// 9 | CONSTRUCTORS 10 | //////////////////////////////////////////////////////////////*/ 11 | 12 | export function newFluxNode({ 13 | id, 14 | x, 15 | y, 16 | fluxNodeType, 17 | text, 18 | generating, 19 | }: { 20 | id?: string; 21 | x: number; 22 | y: number; 23 | fluxNodeType: FluxNodeType; 24 | text: string; 25 | generating: boolean; 26 | }): Node { 27 | return { 28 | id: id ?? generateNodeId(), 29 | position: { x, y }, 30 | style: { 31 | background: getFluxNodeTypeColor(fluxNodeType), 32 | }, 33 | data: { 34 | label: displayNameFromFluxNodeType(fluxNodeType), 35 | fluxNodeType, 36 | text, 37 | generating, 38 | }, 39 | }; 40 | } 41 | 42 | /*////////////////////////////////////////////////////////////// 43 | TRANSFORMERS 44 | //////////////////////////////////////////////////////////////*/ 45 | 46 | export function addFluxNode( 47 | existingNodes: Node[], 48 | { 49 | id, 50 | x, 51 | y, 52 | fluxNodeType, 53 | text, 54 | generating, 55 | }: { 56 | id?: string; 57 | x: number; 58 | y: number; 59 | fluxNodeType: FluxNodeType; 60 | text: string; 61 | generating: boolean; 62 | } 63 | ): Node[] { 64 | const newNode = newFluxNode({ x, y, fluxNodeType, text, id, generating }); 65 | 66 | return [...existingNodes, newNode]; 67 | } 68 | 69 | export function addUserNodeLinkedToASystemNode( 70 | nodes: Node[], 71 | systemNodeText: string, 72 | userNodeText: string | null = "", 73 | systemId: string = generateNodeId(), 74 | userId: string = generateNodeId() 75 | ) { 76 | const nodesCopy = [...nodes]; 77 | 78 | const systemNode = newFluxNode({ 79 | id: systemId, 80 | x: 81 | nodesCopy.length > 0 82 | ? nodesCopy.reduce((prev, current) => 83 | prev.position.x > current.position.x ? prev : current 84 | ).position.x + NEW_TREE_X_OFFSET 85 | : window.innerWidth / 2 / 2 - 75, 86 | y: 500, 87 | fluxNodeType: FluxNodeType.System, 88 | text: systemNodeText, 89 | generating: false, 90 | }); 91 | 92 | nodesCopy.push(systemNode); 93 | 94 | nodesCopy.push( 95 | newFluxNode({ 96 | id: userId, 97 | x: systemNode.position.x, 98 | // Add OVERLAP_RANDOMNESS_MAX of randomness to 99 | // the y position so that nodes don't overlap. 100 | y: systemNode.position.y + 100 + Math.random() * OVERLAP_RANDOMNESS_MAX, 101 | fluxNodeType: FluxNodeType.User, 102 | text: userNodeText ?? "", 103 | generating: false, 104 | }) 105 | ); 106 | 107 | return nodesCopy; 108 | } 109 | 110 | export function modifyReactFlowNodeProperties( 111 | existingNodes: Node[], 112 | { 113 | id, 114 | type, 115 | draggable, 116 | }: { id: string; type: ReactFlowNodeTypes | undefined; draggable: boolean } 117 | ): Node[] { 118 | return existingNodes.map((node) => { 119 | if (node.id !== id) return node; 120 | 121 | const copy = { ...node, data: { ...node.data }, type, draggable }; 122 | 123 | return copy; 124 | }); 125 | } 126 | 127 | export function modifyFluxNodeText( 128 | existingNodes: Node[], 129 | { asHuman, id, text }: { asHuman: boolean; id: string; text: string } 130 | ): Node[] { 131 | return existingNodes.map((node) => { 132 | if (node.id !== id) return node; 133 | 134 | const copy = { ...node, data: { ...node.data } }; 135 | 136 | copy.data.text = text; 137 | 138 | // If the node's fluxNodeType is GPT and we're changing 139 | // it as a human then its type becomes GPT + Human. 140 | if (asHuman && copy.data.fluxNodeType === FluxNodeType.GPT) { 141 | copy.style = { 142 | ...copy.style, 143 | background: getFluxNodeTypeColor(FluxNodeType.TweakedGPT), 144 | }; 145 | 146 | copy.data.fluxNodeType = FluxNodeType.TweakedGPT; 147 | copy.data.label = displayNameFromFluxNodeType( 148 | FluxNodeType.TweakedGPT, 149 | undefined, 150 | copy.data.label 151 | ); 152 | } 153 | 154 | return copy; 155 | }); 156 | } 157 | 158 | export function modifyFluxNodeLabel( 159 | existingNodes: Node[], 160 | { id, type, label }: { id: string; type?: FluxNodeType; label: string } 161 | ): Node[] { 162 | return existingNodes.map((node) => { 163 | if (node.id !== id) return node; 164 | 165 | const copy = { ...node, data: { ...node.data, label }, type, draggable: undefined }; 166 | 167 | return copy; 168 | }); 169 | } 170 | 171 | export function appendTextToFluxNodeAsGPT( 172 | existingNodes: Node[], 173 | { id, text }: { id: string; text: string } 174 | ): Node[] { 175 | return existingNodes.map((node) => { 176 | if (node.id !== id) return node; 177 | 178 | const copy = { ...node, data: { ...node.data } }; 179 | 180 | copy.data.text += text; 181 | 182 | return copy; 183 | }); 184 | } 185 | 186 | export function markFluxNodeAsDoneGenerating( 187 | existingNodes: Node[], 188 | id: string 189 | ): Node[] { 190 | return existingNodes.map((node) => { 191 | if (node.id !== id) return node; 192 | 193 | return { ...node, data: { ...node.data, generating: false } }; 194 | }); 195 | } 196 | 197 | export function deleteFluxNode( 198 | existingNodes: Node[], 199 | id: string 200 | ): Node[] { 201 | return existingNodes.filter((node) => node.id !== id); 202 | } 203 | 204 | export function deleteSelectedFluxNodes( 205 | existingNodes: Node[] 206 | ): Node[] { 207 | return existingNodes.filter((node) => !node.selected); 208 | } 209 | 210 | export function markOnlyNodeAsSelected( 211 | existingNodes: Node[], 212 | id: string 213 | ): Node[] { 214 | return existingNodes.map((node) => { 215 | return { ...node, selected: node.id === id }; 216 | }); 217 | } 218 | 219 | /*////////////////////////////////////////////////////////////// 220 | GETTERS 221 | //////////////////////////////////////////////////////////////*/ 222 | 223 | export function getFluxNode( 224 | nodes: Node[], 225 | id: string 226 | ): Node | undefined { 227 | return nodes.find((node) => node.id === id); 228 | } 229 | 230 | export function getFluxNodeGPTChildren( 231 | existingNodes: Node[], 232 | existingEdges: Edge[], 233 | id: string 234 | ): Node[] { 235 | return existingNodes.filter( 236 | (node) => 237 | (node.data.fluxNodeType === FluxNodeType.GPT || 238 | node.data.fluxNodeType === FluxNodeType.TweakedGPT) && 239 | getFluxNodeParent(existingNodes, existingEdges, node.id)?.id === id 240 | ); 241 | } 242 | 243 | export function getFluxNodeChildren( 244 | existingNodes: Node[], 245 | existingEdges: Edge[], 246 | id: string 247 | ) { 248 | return existingNodes.filter( 249 | (node) => getFluxNodeParent(existingNodes, existingEdges, node.id)?.id === id 250 | ); 251 | } 252 | 253 | export function getFluxNodeSiblings( 254 | existingNodes: Node[], 255 | existingEdges: Edge[], 256 | id: string 257 | ): Node[] { 258 | const parent = getFluxNodeParent(existingNodes, existingEdges, id); 259 | 260 | if (!parent) return []; 261 | 262 | return getFluxNodeChildren(existingNodes, existingEdges, parent.id); 263 | } 264 | 265 | export function getFluxNodeParent( 266 | existingNodes: Node[], 267 | existingEdges: Edge[], 268 | id: string 269 | ): Node | undefined { 270 | let edge: Edge | undefined; 271 | 272 | // We iterate in reverse to ensure we don't try to route 273 | // through a stale (now hidden) edge to find the parent. 274 | for (let i = existingEdges.length - 1; i >= 0; i--) { 275 | const e = existingEdges[i]; 276 | 277 | if (e.target === id) { 278 | edge = e; 279 | break; 280 | } 281 | } 282 | 283 | if (!edge) return; 284 | 285 | return existingNodes.find((node) => node.id === edge!.source); 286 | } 287 | 288 | // Get the lineage of the node, 289 | // where index 0 is the node, 290 | // index 1 is the node's parent, 291 | // index 2 is the node's grandparent, etc. 292 | // TODO: Eventually would be nice to have 293 | // support for connecting multiple parents! 294 | export function getFluxNodeLineage( 295 | existingNodes: Node[], 296 | existingEdges: Edge[], 297 | id: string 298 | ): Node[] { 299 | const lineage: Node[] = []; 300 | 301 | let currentNode = getFluxNode(existingNodes, id); 302 | 303 | while (currentNode) { 304 | lineage.push(currentNode); 305 | 306 | currentNode = getFluxNodeParent(existingNodes, existingEdges, currentNode.id); 307 | } 308 | 309 | return lineage; 310 | } 311 | 312 | export function isFluxNodeInLineage( 313 | existingNodes: Node[], 314 | existingEdges: Edge[], 315 | { nodeToCheck, nodeToGetLineageOf }: { nodeToCheck: string; nodeToGetLineageOf: string } 316 | ): boolean { 317 | const lineage = getFluxNodeLineage(existingNodes, existingEdges, nodeToGetLineageOf); 318 | 319 | return lineage.some((node) => node.id === nodeToCheck); 320 | } 321 | 322 | export function getConnectionAllowed( 323 | existingNodes: Node[], 324 | existingEdges: Edge[], 325 | { source, target }: { source: string; target: string } 326 | ): boolean { 327 | return ( 328 | // Check the lineage of the source node to make 329 | // sure we aren't creating a recursive connection. 330 | !isFluxNodeInLineage(existingNodes, existingEdges, { 331 | nodeToCheck: target, 332 | nodeToGetLineageOf: source, 333 | // Check if the target node already has a parent. 334 | }) && getFluxNodeParent(existingNodes, existingEdges, target) === undefined 335 | ); 336 | } 337 | 338 | /*////////////////////////////////////////////////////////////// 339 | RENDERERS 340 | //////////////////////////////////////////////////////////////*/ 341 | 342 | export function displayNameFromFluxNodeType( 343 | fluxNodeType: FluxNodeType, 344 | isGPT4?: boolean, 345 | overrideLabel?: string 346 | ) { 347 | switch (fluxNodeType) { 348 | case FluxNodeType.User: 349 | return "User"; 350 | case FluxNodeType.GPT: 351 | return isGPT4 === undefined ? "GPT" : isGPT4 ? "GPT-4" : "GPT-3.5"; 352 | case FluxNodeType.TweakedGPT: 353 | return `${overrideLabel} (edited)`; 354 | case FluxNodeType.System: 355 | return "System"; 356 | } 357 | } 358 | -------------------------------------------------------------------------------- /src/utils/chakra.tsx: -------------------------------------------------------------------------------- 1 | import { useState, useEffect } from "react"; 2 | import { Flex, FlexProps } from "@chakra-ui/react"; 3 | 4 | /* Typings */ 5 | export type MainAxisAlignmentStrings = 6 | | "space-between" 7 | | "space-around" 8 | | "flex-start" 9 | | "center" 10 | | "flex-end"; 11 | 12 | export type MainAxisAlignment = 13 | | MainAxisAlignmentStrings 14 | | { md: MainAxisAlignmentStrings; base: MainAxisAlignmentStrings }; 15 | 16 | export type CrossAxisAlignmentStrings = "flex-start" | "center" | "flex-end" | "stretch"; 17 | 18 | export type CrossAxisAlignment = 19 | | CrossAxisAlignmentStrings 20 | | { 21 | md: CrossAxisAlignmentStrings; 22 | base: CrossAxisAlignmentStrings; 23 | }; 24 | 25 | export class PixelMeasurement { 26 | size: number; 27 | 28 | constructor(num: number) { 29 | this.size = num; 30 | } 31 | 32 | asPxString(): string { 33 | return this.size + "px"; 34 | } 35 | 36 | toString(): string { 37 | return this.asPxString(); 38 | } 39 | 40 | asNumber(): number { 41 | return this.size; 42 | } 43 | } 44 | 45 | export class PercentageSize { 46 | percent: number; 47 | 48 | constructor(num: number) { 49 | if (num > 1) { 50 | throw new Error("Cannot have a percentage higher than 1!"); 51 | } 52 | 53 | this.percent = num; 54 | } 55 | } 56 | 57 | export class PercentOnDesktopPixelOnMobileSize { 58 | percent: number; 59 | pixel: number; 60 | 61 | constructor({ 62 | percentageSize, 63 | pixelSize, 64 | }: { 65 | percentageSize: number; 66 | pixelSize: number; 67 | }) { 68 | if (percentageSize > 1) { 69 | throw new Error("Cannot have a percentage higher than 1!"); 70 | } 71 | 72 | this.percent = percentageSize; 73 | this.pixel = pixelSize; 74 | } 75 | } 76 | 77 | export class PixelSize { 78 | pixel: number; 79 | 80 | constructor(num: number) { 81 | this.pixel = num; 82 | } 83 | } 84 | 85 | export class ResponsivePixelSize { 86 | desktop: number; 87 | mobile: number; 88 | 89 | constructor({ desktop, mobile }: { desktop: number; mobile: number }) { 90 | this.mobile = mobile; 91 | this.desktop = desktop; 92 | } 93 | } 94 | 95 | /************************************** 96 | * 97 | * 98 | * Components 99 | * - Center.tsx 100 | * - Column.tsx 101 | * - Row.tsx 102 | * - RowOnDesktopColumnOnMobile.tsx 103 | * - RowOrColumn.tsx 104 | * 105 | *************************************** 106 | */ 107 | 108 | /** 109 | * Center.tsx 110 | * 111 | * Creates a Flex where `justifyContent === 'center'` and `alignItems === 'center'` 112 | * If `expand === true` it will set the height and width of the Flex to 100%. 113 | * Passes all extra props to the Flex. 114 | */ 115 | 116 | export type CenterProps = { 117 | children: React.ReactNode; 118 | expand?: boolean; 119 | } & FlexProps; 120 | 121 | export const Center = ({ children, expand, ...others }: CenterProps) => { 122 | if (expand) { 123 | others.height = "100%"; 124 | others.width = "100%"; 125 | } 126 | 127 | return ( 128 | 129 | {children} 130 | 131 | ); 132 | }; 133 | 134 | /** 135 | * Column.tsx 136 | * 137 | * Creates a Flex with a column direction 138 | * and sets the `justifyContent` to the `mainAxisAlignment` 139 | * and the `alignItems` to the `crossAxisAlignment`. 140 | * If `expand === true` it will set the height and width of the Flex to 100%. 141 | * Passes all extra props to the Flex. 142 | */ 143 | 144 | export type ColumnProps = { 145 | mainAxisAlignment: MainAxisAlignment; 146 | crossAxisAlignment: CrossAxisAlignment; 147 | children: React.ReactNode; 148 | expand?: boolean; 149 | } & FlexProps; 150 | 151 | export const Column = ({ 152 | mainAxisAlignment, 153 | crossAxisAlignment, 154 | children, 155 | expand, 156 | ...others 157 | }: ColumnProps) => { 158 | if (expand) { 159 | others.height = "100%"; 160 | others.width = "100%"; 161 | } 162 | 163 | return ( 164 | 170 | {children} 171 | 172 | ); 173 | }; 174 | 175 | /** 176 | * Row.tsx 177 | * 178 | * Creates a Flex with a row direction 179 | * and sets the `justifyContent` to the `mainAxisAlignment` 180 | * and the `alignItems` to the `crossAxisAlignment`. 181 | * If `expand === true` it will set the height and width of the Flex to 100%. 182 | * Passes all extra props to the Flex. 183 | */ 184 | 185 | export type RowProps = { 186 | mainAxisAlignment: MainAxisAlignment; 187 | crossAxisAlignment: CrossAxisAlignment; 188 | children: React.ReactNode; 189 | expand?: boolean; 190 | } & FlexProps; 191 | 192 | export const Row = ({ 193 | mainAxisAlignment, 194 | crossAxisAlignment, 195 | children, 196 | expand, 197 | ...others 198 | }: RowProps) => { 199 | if (expand) { 200 | others.height = "100%"; 201 | others.width = "100%"; 202 | } 203 | 204 | return ( 205 | 211 | {children} 212 | 213 | ); 214 | }; 215 | 216 | /** 217 | * RowOnDesktopColumnOnMobile.tsx 218 | * 219 | * Creates a Flex with a row direction on desktop and a column direction on mobile. 220 | * and sets the `justifyContent` to the `mainAxisAlignment` 221 | * and the `alignItems` to the `crossAxisAlignment`. 222 | * If `expand === true` it will set the height and width of the Flex to 100%. 223 | * Passes all extra props to the Flex. 224 | */ 225 | export const RowOnDesktopColumnOnMobile = ({ 226 | mainAxisAlignment, 227 | crossAxisAlignment, 228 | children, 229 | expand, 230 | ...others 231 | }: RowProps) => { 232 | if (expand) { 233 | others.height = "100%"; 234 | others.width = "100%"; 235 | } 236 | 237 | return ( 238 | 244 | {children} 245 | 246 | ); 247 | }; 248 | 249 | /** 250 | * RowOrColumn.tsx 251 | * 252 | * Creates a Flex which will be a row if `isRow` is true 253 | * and sets the `justifyContent` to the `mainAxisAlignment` 254 | * and the `alignItems` to the `crossAxisAlignment`. 255 | * If `expand === true` it will set the height and width of the Flex to 100%. 256 | * Passes all extra props to the Flex. 257 | */ 258 | export const RowOrColumn = ({ 259 | mainAxisAlignment, 260 | crossAxisAlignment, 261 | children, 262 | expand, 263 | isRow, 264 | ...others 265 | }: RowProps & { isRow: boolean }) => { 266 | if (expand) { 267 | others.height = "100%"; 268 | others.width = "100%"; 269 | } 270 | 271 | return ( 272 | 278 | {children} 279 | 280 | ); 281 | }; 282 | 283 | /************************************** 284 | * 285 | * 286 | * Hooks 287 | * - useWindowSize.ts 288 | * - useLockedViewHeight.ts 289 | * - useIsMobile.ts 290 | * - useSpacedLayout.ts 291 | * 292 | *************************************** 293 | */ 294 | 295 | /** 296 | * useWindowSize.ts 297 | * 298 | * Gets the height and width of the current window. 299 | */ 300 | export const useWindowSize = () => { 301 | const [windowSize, setWindowSize] = useState({ 302 | width: window.innerWidth, 303 | height: window.innerHeight, 304 | }); 305 | 306 | useEffect(() => { 307 | // Handler to call on window resize 308 | function handleResize() { 309 | // Set window width/height to state 310 | setWindowSize({ 311 | width: window.innerWidth, 312 | height: window.innerHeight, 313 | }); 314 | } 315 | 316 | // Add event listener 317 | window.addEventListener("resize", handleResize); 318 | 319 | // Call handler right away so state gets updated with initial window size 320 | handleResize(); 321 | 322 | // Remove event listener on cleanup 323 | return () => window.removeEventListener("resize", handleResize); 324 | }, []); // Empty array ensures that effect is only run on mount 325 | 326 | return windowSize; 327 | }; 328 | 329 | /** 330 | * useLockedViewHeight.ts 331 | * 332 | * Returns the pixel count of the height of the window, 333 | * but will not return a value lower or higher than the minimum/maximum passed. 334 | */ 335 | export function useLockedViewHeight({ 336 | min = -1, 337 | max = Number.MAX_SAFE_INTEGER, 338 | }: { 339 | min?: number; 340 | max?: number; 341 | }) { 342 | const { height } = useWindowSize(); 343 | 344 | if (height <= min) { 345 | return { 346 | windowHeight: new PixelMeasurement(min), 347 | isLocked: true, 348 | }; 349 | } else if (height >= max) { 350 | return { 351 | windowHeight: new PixelMeasurement(max), 352 | isLocked: true, 353 | }; 354 | } else { 355 | return { 356 | windowHeight: new PixelMeasurement(height), 357 | isLocked: false, 358 | }; 359 | } 360 | } 361 | 362 | /** 363 | * useIsMobile.ts 364 | * 365 | * Returns whether the width of the window makes it likely a mobile device. 366 | * */ 367 | export function useIsMobile() { 368 | const { width } = useWindowSize(); 369 | 370 | return width < 768; 371 | } 372 | 373 | /** 374 | * useSpacedLayout.ts 375 | * 376 | * Takes the height of the parent, the desired spacing between children, 377 | * and the desired percentage sizes of the children (relative to their parent minus the spacing desired and the size of fixed sized children) 378 | * or the size of the child in pixels 379 | * and returns the pixel size of each child 380 | * that makes that child conform to the desired percentage. 381 | */ 382 | export function useSpacedLayout({ 383 | parentHeight, 384 | spacing, 385 | childSizes, 386 | }: { 387 | parentHeight: number; 388 | spacing: number; 389 | childSizes: ( 390 | | PercentageSize 391 | | PercentOnDesktopPixelOnMobileSize 392 | | PixelSize 393 | | ResponsivePixelSize 394 | )[]; 395 | }) { 396 | const isMobile = useIsMobile(); 397 | 398 | let parentMinusSpacingAndFixedChildSizes = 399 | parentHeight - 400 | spacing * (childSizes.length - 1) - 401 | childSizes.reduce((past, value) => { 402 | if ( 403 | value instanceof PixelSize || 404 | (value instanceof PercentOnDesktopPixelOnMobileSize && isMobile) 405 | ) { 406 | return past + value.pixel; 407 | } else if (value instanceof ResponsivePixelSize) { 408 | return past + (isMobile ? value.mobile : value.desktop); 409 | } else { 410 | return past; 411 | } 412 | }, 0); 413 | 414 | let spacedChildren: PixelMeasurement[] = []; 415 | 416 | for (const size of childSizes) { 417 | if ( 418 | size instanceof PercentageSize || 419 | (size instanceof PercentOnDesktopPixelOnMobileSize && !isMobile) 420 | ) { 421 | spacedChildren.push( 422 | new PixelMeasurement(size.percent * parentMinusSpacingAndFixedChildSizes) 423 | ); 424 | } else if (size instanceof PercentOnDesktopPixelOnMobileSize && isMobile) { 425 | spacedChildren.push(new PixelMeasurement(size.pixel)); 426 | } else if (size instanceof ResponsivePixelSize) { 427 | spacedChildren.push(new PixelMeasurement(isMobile ? size.mobile : size.desktop)); 428 | } else { 429 | spacedChildren.push(new PixelMeasurement(size.pixel)); 430 | } 431 | } 432 | 433 | return { 434 | parentHeight: new PixelMeasurement(parentHeight), 435 | spacing: new PixelMeasurement(spacing), 436 | childSizes: spacedChildren, 437 | }; 438 | } 439 | -------------------------------------------------------------------------------- /src/components/App.tsx: -------------------------------------------------------------------------------- 1 | import { useEffect, useState, useCallback, useRef } from "react"; 2 | 3 | import ReactFlow, { 4 | addEdge, 5 | Background, 6 | Connection, 7 | Node, 8 | Edge, 9 | useEdgesState, 10 | useNodesState, 11 | SelectionMode, 12 | ReactFlowInstance, 13 | ReactFlowJsonObject, 14 | useReactFlow, 15 | updateEdge, 16 | } from "reactflow"; 17 | 18 | import "reactflow/dist/style.css"; 19 | 20 | import mixpanel from "mixpanel-browser"; 21 | 22 | import { Resizable } from "re-resizable"; 23 | 24 | import { yieldStream } from "yield-stream"; 25 | 26 | import { useHotkeys } from "react-hotkeys-hook"; 27 | 28 | import { useBeforeunload } from "react-beforeunload"; 29 | 30 | import { CheckCircleIcon } from "@chakra-ui/icons"; 31 | import { Box, useDisclosure, Spinner, useToast } from "@chakra-ui/react"; 32 | 33 | import { CreateCompletionResponseChoicesInner, OpenAI } from "openai-streams"; 34 | 35 | import { Prompt } from "./Prompt"; 36 | 37 | import { APIKeyModal } from "./modals/APIKeyModal"; 38 | import { SettingsModal } from "./modals/SettingsModal"; 39 | 40 | import { MIXPANEL_TOKEN } from "../main"; 41 | 42 | import { 43 | getFluxNode, 44 | getFluxNodeGPTChildren, 45 | displayNameFromFluxNodeType, 46 | newFluxNode, 47 | appendTextToFluxNodeAsGPT, 48 | getFluxNodeLineage, 49 | addFluxNode, 50 | modifyFluxNodeText, 51 | modifyReactFlowNodeProperties, 52 | getFluxNodeChildren, 53 | getFluxNodeParent, 54 | getFluxNodeSiblings, 55 | markOnlyNodeAsSelected, 56 | deleteFluxNode, 57 | deleteSelectedFluxNodes, 58 | addUserNodeLinkedToASystemNode, 59 | markFluxNodeAsDoneGenerating, 60 | getConnectionAllowed, 61 | } from "../utils/fluxNode"; 62 | import { 63 | FluxNodeData, 64 | FluxNodeType, 65 | HistoryItem, 66 | Settings, 67 | CreateChatCompletionStreamResponseChoicesInner, 68 | ReactFlowNodeTypes, 69 | } from "../utils/types"; 70 | import { 71 | API_KEY_LOCAL_STORAGE_KEY, 72 | DEFAULT_SETTINGS, 73 | FIT_VIEW_SETTINGS, 74 | HOTKEY_CONFIG, 75 | MAX_HISTORY_SIZE, 76 | MODEL_SETTINGS_LOCAL_STORAGE_KEY, 77 | NEW_TREE_CONTENT_QUERY_PARAM, 78 | OVERLAP_RANDOMNESS_MAX, 79 | REACT_FLOW_NODE_TYPES, 80 | REACT_FLOW_LOCAL_STORAGE_KEY, 81 | TOAST_CONFIG, 82 | UNDEFINED_RESPONSE_STRING, 83 | } from "../utils/constants"; 84 | import { mod } from "../utils/mod"; 85 | import { BigButton } from "./utils/BigButton"; 86 | import { Column, Row } from "../utils/chakra"; 87 | import { isValidAPIKey } from "../utils/apikey"; 88 | import { generateNodeId } from "../utils/nodeId"; 89 | import { useLocalStorage } from "../utils/lstore"; 90 | import { NavigationBar } from "./utils/NavigationBar"; 91 | import { useDebouncedEffect } from "../utils/debounce"; 92 | import { useDebouncedWindowResize } from "../utils/resize"; 93 | import { getQueryParam, resetURL } from "../utils/qparams"; 94 | import { copySnippetToClipboard } from "../utils/clipboard"; 95 | import { messagesFromLineage, promptFromLineage } from "../utils/prompt"; 96 | import { newFluxEdge, modifyFluxEdge, addFluxEdge } from "../utils/fluxEdge"; 97 | import { getFluxNodeTypeColor, getFluxNodeTypeDarkColor } from "../utils/color"; 98 | 99 | function App() { 100 | const toast = useToast(); 101 | 102 | /*////////////////////////////////////////////////////////////// 103 | UNDO REDO LOGIC 104 | //////////////////////////////////////////////////////////////*/ 105 | 106 | const [past, setPast] = useState([]); 107 | const [future, setFuture] = useState([]); 108 | 109 | const takeSnapshot = () => { 110 | // Push the current graph to the past state. 111 | setPast((past) => [ 112 | ...past.slice(past.length - MAX_HISTORY_SIZE + 1, past.length), 113 | { nodes, edges, selectedNodeId, lastSelectedNodeId }, 114 | ]); 115 | 116 | // Whenever we take a new snapshot, the redo operations 117 | // need to be cleared to avoid state mismatches. 118 | setFuture([]); 119 | }; 120 | 121 | const undo = () => { 122 | // get the last state that we want to go back to 123 | const pastState = past[past.length - 1]; 124 | 125 | if (pastState) { 126 | // First we remove the state from the history. 127 | setPast((past) => past.slice(0, past.length - 1)); 128 | // We store the current graph for the redo operation. 129 | setFuture((future) => [ 130 | ...future, 131 | { nodes, edges, selectedNodeId, lastSelectedNodeId }, 132 | ]); 133 | 134 | // Now we can set the graph to the past state. 135 | setNodes(pastState.nodes); 136 | setEdges(pastState.edges); 137 | setLastSelectedNodeId(pastState.lastSelectedNodeId); 138 | setSelectedNodeId(pastState.selectedNodeId); 139 | 140 | autoZoomIfNecessary(); 141 | } 142 | }; 143 | 144 | const redo = () => { 145 | const futureState = future[future.length - 1]; 146 | 147 | if (futureState) { 148 | setFuture((future) => future.slice(0, future.length - 1)); 149 | setPast((past) => [...past, { nodes, edges, selectedNodeId, lastSelectedNodeId }]); 150 | setNodes(futureState.nodes); 151 | setEdges(futureState.edges); 152 | setLastSelectedNodeId(futureState.lastSelectedNodeId); 153 | setSelectedNodeId(futureState.selectedNodeId); 154 | 155 | autoZoomIfNecessary(); 156 | } 157 | }; 158 | 159 | /*////////////////////////////////////////////////////////////// 160 | CORE REACT FLOW LOGIC 161 | //////////////////////////////////////////////////////////////*/ 162 | 163 | const { setViewport, fitView } = useReactFlow(); 164 | 165 | const [reactFlow, setReactFlow] = useState(null); 166 | 167 | const [nodes, setNodes, onNodesChange] = useNodesState([]); 168 | const [edges, setEdges, onEdgesChange] = useEdgesState([]); 169 | 170 | const edgeUpdateSuccessful = useRef(true); 171 | 172 | const onEdgeUpdateStart = useCallback(() => { 173 | edgeUpdateSuccessful.current = false; 174 | }, []); 175 | 176 | const onEdgeUpdate = (oldEdge: Edge, newConnection: Connection) => { 177 | if ( 178 | !getConnectionAllowed(nodes, edges, { 179 | source: newConnection.source!, 180 | target: newConnection.target!, 181 | }) 182 | ) 183 | return; 184 | 185 | edgeUpdateSuccessful.current = true; 186 | setEdges((edges) => updateEdge(oldEdge, newConnection, edges)); 187 | }; 188 | 189 | const onEdgeUpdateEnd = useCallback((_: unknown, edge: Edge) => { 190 | if (!edgeUpdateSuccessful.current) { 191 | setEdges((edges) => edges.filter((e) => e.id !== edge.id)); 192 | } 193 | 194 | edgeUpdateSuccessful.current = true; 195 | }, []); 196 | 197 | const onConnect = (connection: Edge | Connection) => { 198 | if ( 199 | !getConnectionAllowed(nodes, edges, { 200 | source: connection.source!, 201 | target: connection.target!, 202 | }) 203 | ) 204 | return; 205 | 206 | takeSnapshot(); 207 | setEdges((eds) => addEdge({ ...connection }, eds)); 208 | }; 209 | 210 | const autoZoom = () => setTimeout(() => fitView(FIT_VIEW_SETTINGS), 50); 211 | 212 | const autoZoomIfNecessary = () => { 213 | if (settings.autoZoom) autoZoom(); 214 | }; 215 | 216 | const save = () => { 217 | if (reactFlow) { 218 | localStorage.setItem( 219 | REACT_FLOW_LOCAL_STORAGE_KEY, 220 | JSON.stringify(reactFlow.toObject()) 221 | ); 222 | 223 | console.log("Saved React Flow state!"); 224 | } 225 | }; 226 | 227 | // Auto save. 228 | const isSavingReactFlow = useDebouncedEffect( 229 | save, 230 | 1000, // 1 second. 231 | [reactFlow, nodes, edges] 232 | ); 233 | 234 | // Auto restore on load. 235 | useEffect(() => { 236 | if (reactFlow) { 237 | const rawFlow = localStorage.getItem(REACT_FLOW_LOCAL_STORAGE_KEY); 238 | 239 | const flow: ReactFlowJsonObject = rawFlow ? JSON.parse(rawFlow) : null; 240 | 241 | // Get the content of the newTreeWith query param. 242 | const content = getQueryParam(NEW_TREE_CONTENT_QUERY_PARAM); 243 | 244 | if (flow) { 245 | console.log("Restoring react flow from local storage."); 246 | 247 | setEdges(flow.edges || []); 248 | setViewport(flow.viewport); 249 | 250 | const nodes = flow.nodes; // For brevity. 251 | 252 | if (nodes.length > 0) { 253 | // Either the first selected node we find, or the first node in the array. 254 | const toSelect = nodes.find((node) => node.selected)?.id ?? nodes[0].id; 255 | 256 | // Add the nodes to the React Flow array and select the node. 257 | selectNode(toSelect, () => nodes); 258 | 259 | // If there was a newTreeWith query param, create a new tree with that content. 260 | // We pass false for forceAutoZoom because we'll do it 500ms later to avoid lag. 261 | if (content) newUserNodeLinkedToANewSystemNode(content, false); 262 | } else newUserNodeLinkedToANewSystemNode(content, false); // Create a new node if there are none. 263 | } else newUserNodeLinkedToANewSystemNode(content, false); // Create a new node if there are none. 264 | 265 | setTimeout(() => { 266 | // Do this with a more generous timeout to make sure 267 | // the nodes are rendered and the settings have loaded in. 268 | if (settings.autoZoom) fitView(FIT_VIEW_SETTINGS); 269 | }, 500); 270 | 271 | resetURL(); // Get rid of the query params. 272 | } 273 | }, [reactFlow]); 274 | 275 | /*////////////////////////////////////////////////////////////// 276 | AI PROMPT CALLBACKS 277 | //////////////////////////////////////////////////////////////*/ 278 | 279 | // Takes a prompt, submits it to the GPT API with n responses, 280 | // then creates a child node for each response under the selected node. 281 | const submitPrompt = async (overrideExistingIfPossible: boolean) => { 282 | takeSnapshot(); 283 | 284 | if (MIXPANEL_TOKEN) mixpanel.track("Submitted Prompt"); 285 | 286 | const responses = settings.n; 287 | const temp = settings.temp; 288 | const model = settings.model; 289 | 290 | const parentNodeLineage = selectedNodeLineage; 291 | const parentNodeId = selectedNodeLineage[0].id; 292 | 293 | const newNodes = [...nodes]; 294 | 295 | const currentNode = getFluxNode(newNodes, parentNodeId)!; 296 | const currentNodeChildren = getFluxNodeGPTChildren(newNodes, edges, parentNodeId); 297 | 298 | let firstCompletionId: string | undefined; 299 | 300 | // Update newNodes, adding new child nodes as 301 | // needed, re-using existing ones wherever possible if overrideExistingIfPossible is set. 302 | for (let i = 0; i < responses; i++) { 303 | // If we have enough children, and overrideExistingIfPossible is true, we'll just re-use one. 304 | if (overrideExistingIfPossible && i < currentNodeChildren.length) { 305 | const childNode = currentNodeChildren[i]; 306 | 307 | if (i === 0) firstCompletionId = childNode.id; 308 | 309 | const idx = newNodes.findIndex((node) => node.id === childNode.id); 310 | 311 | newNodes[idx] = { 312 | ...childNode, 313 | data: { 314 | ...childNode.data, 315 | text: "", 316 | label: displayNameFromFluxNodeType(FluxNodeType.GPT), 317 | fluxNodeType: FluxNodeType.GPT, 318 | generating: true, 319 | }, 320 | style: { 321 | ...childNode.style, 322 | background: getFluxNodeTypeColor(FluxNodeType.GPT), 323 | }, 324 | }; 325 | } else { 326 | const id = generateNodeId(); 327 | 328 | if (i === 0) firstCompletionId = id; 329 | 330 | // Otherwise, we'll create a new node. 331 | newNodes.push( 332 | newFluxNode({ 333 | id, 334 | // Position it 50px below the current node, offset 335 | // horizontally according to the number of responses 336 | // such that the middle response is right below the current node. 337 | // Note that node x y coords are the top left corner of the node, 338 | // so we need to offset by at the width of the node (150px). 339 | x: 340 | (currentNodeChildren.length > 0 341 | ? // If there are already children we want to put the 342 | // next child to the right of the furthest right one. 343 | currentNodeChildren.reduce((prev, current) => 344 | prev.position.x > current.position.x ? prev : current 345 | ).position.x + 346 | (responses / 2) * 180 + 347 | 90 348 | : currentNode.position.x) + 349 | (i - (responses - 1) / 2) * 180, 350 | // Add OVERLAP_RANDOMNESS_MAX of randomness to the y position so that nodes don't overlap. 351 | y: currentNode.position.y + 100 + Math.random() * OVERLAP_RANDOMNESS_MAX, 352 | fluxNodeType: FluxNodeType.GPT, 353 | text: "", 354 | generating: true, 355 | }) 356 | ); 357 | } 358 | } 359 | 360 | if (firstCompletionId === undefined) throw new Error("No first completion id!"); 361 | 362 | (async () => { 363 | const stream = await OpenAI( 364 | "chat", 365 | { 366 | model, 367 | n: responses, 368 | temperature: temp, 369 | messages: messagesFromLineage(parentNodeLineage, settings), 370 | }, 371 | { apiKey: apiKey!, mode: "raw" } 372 | ); 373 | 374 | const DECODER = new TextDecoder(); 375 | 376 | for await (const chunk of yieldStream(stream)) { 377 | try { 378 | const decoded = JSON.parse(DECODER.decode(chunk)); 379 | 380 | if (decoded.choices === undefined) 381 | throw new Error( 382 | "No choices in response. Decoded response: " + JSON.stringify(decoded) 383 | ); 384 | 385 | const choice: CreateChatCompletionStreamResponseChoicesInner = 386 | decoded.choices[0]; 387 | 388 | if (choice.index === undefined) 389 | throw new Error( 390 | "No index in choice. Decoded choice: " + JSON.stringify(choice) 391 | ); 392 | 393 | const correspondingNodeId = 394 | // If we re-used a node we have to pull it from children array. 395 | overrideExistingIfPossible && choice.index < currentNodeChildren.length 396 | ? currentNodeChildren[choice.index].id 397 | : newNodes[newNodes.length - responses + choice.index].id; 398 | 399 | // The ChatGPT API will start by returning a 400 | // choice with only a role delta and no content. 401 | if (choice.delta?.content) { 402 | setNodes((newerNodes) => { 403 | return appendTextToFluxNodeAsGPT(newerNodes, { 404 | id: correspondingNodeId, 405 | text: choice.delta?.content ?? UNDEFINED_RESPONSE_STRING, 406 | }); 407 | }); 408 | } 409 | 410 | // If the choice has a finish reason, then it's the final 411 | // choice and we can mark it as no longer animated right now. 412 | if (choice.finish_reason !== null) { 413 | setNodes((nodes) => markFluxNodeAsDoneGenerating(nodes, correspondingNodeId)); 414 | 415 | setEdges((edges) => 416 | modifyFluxEdge(edges, { 417 | source: parentNodeId, 418 | target: correspondingNodeId, 419 | animated: false, 420 | }) 421 | ); 422 | } 423 | } catch (err) { 424 | console.error(err); 425 | } 426 | } 427 | 428 | // Mark all the edges as no longer animated. 429 | for (let i = 0; i < responses; i++) { 430 | const correspondingNodeId = 431 | overrideExistingIfPossible && i < currentNodeChildren.length 432 | ? currentNodeChildren[i].id 433 | : newNodes[newNodes.length - responses + i].id; 434 | 435 | setNodes((nodes) => markFluxNodeAsDoneGenerating(nodes, correspondingNodeId)); 436 | 437 | setEdges((edges) => 438 | modifyFluxEdge(edges, { 439 | source: parentNodeId, 440 | target: correspondingNodeId, 441 | animated: false, 442 | }) 443 | ); 444 | } 445 | })().catch((err) => 446 | toast({ 447 | title: err.toString(), 448 | status: "error", 449 | ...TOAST_CONFIG, 450 | }) 451 | ); 452 | 453 | setNodes(markOnlyNodeAsSelected(newNodes, firstCompletionId!)); 454 | 455 | setLastSelectedNodeId(selectedNodeId); 456 | setSelectedNodeId(firstCompletionId); 457 | 458 | setEdges((edges) => { 459 | let newEdges = [...edges]; 460 | 461 | for (let i = 0; i < responses; i++) { 462 | // Update the links between 463 | // re-used nodes if necessary. 464 | if (overrideExistingIfPossible && i < currentNodeChildren.length) { 465 | const childId = currentNodeChildren[i].id; 466 | 467 | const idx = newEdges.findIndex( 468 | (edge) => edge.source === parentNodeId && edge.target === childId 469 | ); 470 | 471 | newEdges[idx] = { 472 | ...newEdges[idx], 473 | animated: true, 474 | }; 475 | } else { 476 | // The new nodes are added to the end of the array, so we need to 477 | // subtract responses from and add i to length of the array to access. 478 | const childId = newNodes[newNodes.length - responses + i].id; 479 | 480 | // Otherwise, add a new edge. 481 | newEdges.push( 482 | newFluxEdge({ 483 | source: parentNodeId, 484 | target: childId, 485 | animated: true, 486 | }) 487 | ); 488 | } 489 | } 490 | 491 | return newEdges; 492 | }); 493 | 494 | autoZoomIfNecessary(); 495 | }; 496 | 497 | const completeNextWords = () => { 498 | takeSnapshot(); 499 | 500 | const temp = settings.temp; 501 | 502 | const parentNodeLineage = selectedNodeLineage; 503 | const parentNodeId = parentNodeLineage[0].id; 504 | 505 | (async () => { 506 | // TODO: Stop sequences for user/assistant/etc? min tokens? 507 | // Select between instruction and auto completer? 508 | const stream = await OpenAI( 509 | "completions", 510 | { 511 | model: "text-davinci-003", // TODO: Allow customizing. 512 | temperature: temp, // TODO: Allow customizing. 513 | prompt: promptFromLineage(parentNodeLineage, settings), 514 | max_tokens: 250, // Allow customizing. 515 | stop: ["\n\n", "assistant:", "user:"], // TODO: Allow customizing. 516 | }, 517 | { apiKey: apiKey!, mode: "raw" } 518 | ); 519 | 520 | const DECODER = new TextDecoder(); 521 | 522 | for await (const chunk of yieldStream(stream)) { 523 | try { 524 | const decoded = JSON.parse(DECODER.decode(chunk)); 525 | 526 | if (decoded.choices === undefined) 527 | throw new Error( 528 | "No choices in response. Decoded response: " + JSON.stringify(decoded) 529 | ); 530 | 531 | const choice: CreateCompletionResponseChoicesInner = decoded.choices[0]; 532 | 533 | setNodes((newNodes) => 534 | appendTextToFluxNodeAsGPT(newNodes, { 535 | id: parentNodeId, 536 | text: choice.text ?? UNDEFINED_RESPONSE_STRING, 537 | }) 538 | ); 539 | } catch (err) { 540 | console.error(err); 541 | } 542 | } 543 | })().catch((err) => console.error(err)); 544 | }; 545 | 546 | /*////////////////////////////////////////////////////////////// 547 | SELECTED NODE LOGIC 548 | //////////////////////////////////////////////////////////////*/ 549 | 550 | const [selectedNodeId, setSelectedNodeId] = useState(null); 551 | const [lastSelectedNodeId, setLastSelectedNodeId] = useState(null); 552 | 553 | const selectedNodeLineage = 554 | selectedNodeId !== null ? getFluxNodeLineage(nodes, edges, selectedNodeId) : []; 555 | 556 | /*////////////////////////////////////////////////////////////// 557 | NODE MUTATION CALLBACKS 558 | //////////////////////////////////////////////////////////////*/ 559 | 560 | const newUserNodeLinkedToANewSystemNode = ( 561 | text: string | null = "", 562 | forceAutoZoom: boolean = true 563 | ) => { 564 | takeSnapshot(); 565 | 566 | const systemId = generateNodeId(); 567 | const userId = generateNodeId(); 568 | 569 | selectNode(userId, (nodes) => 570 | addUserNodeLinkedToASystemNode( 571 | nodes, 572 | settings.defaultPreamble, 573 | text, 574 | systemId, 575 | userId 576 | ) 577 | ); 578 | 579 | setEdges((edges) => 580 | addFluxEdge(edges, { 581 | source: systemId, 582 | target: userId, 583 | animated: false, 584 | }) 585 | ); 586 | 587 | if (forceAutoZoom) autoZoom(); 588 | }; 589 | 590 | const newConnectedToSelectedNode = (type: FluxNodeType) => { 591 | const selectedNode = getFluxNode(nodes, selectedNodeId!); 592 | 593 | if (selectedNode) { 594 | takeSnapshot(); 595 | 596 | const selectedNodeChildren = getFluxNodeChildren(nodes, edges, selectedNodeId!); 597 | 598 | const id = generateNodeId(); 599 | 600 | selectNode(id, (nodes) => 601 | addFluxNode(nodes, { 602 | id, 603 | x: 604 | selectedNodeChildren.length > 0 605 | ? // If there are already children we want to put the 606 | // next child to the right of the furthest right one. 607 | selectedNodeChildren.reduce((prev, current) => 608 | prev.position.x > current.position.x ? prev : current 609 | ).position.x + 180 610 | : selectedNode.position.x, 611 | // Add OVERLAP_RANDOMNESS_MAX of randomness to 612 | // the y position so that nodes don't overlap. 613 | y: selectedNode.position.y + 100 + Math.random() * OVERLAP_RANDOMNESS_MAX, 614 | fluxNodeType: type, 615 | text: "", 616 | generating: false, 617 | }) 618 | ); 619 | 620 | setEdges((edges) => 621 | addFluxEdge(edges, { 622 | source: selectedNodeId!, 623 | target: id, 624 | animated: false, 625 | }) 626 | ); 627 | 628 | autoZoomIfNecessary(); 629 | } 630 | }; 631 | 632 | const deleteSelectedNodes = () => { 633 | takeSnapshot(); 634 | 635 | const selectedNodes = nodes.filter((node) => node.selected); 636 | 637 | if ( 638 | selectedNodeId && // There's a selected node under the hood. 639 | (selectedNodes.length === 0 || // There are no selected nodes. 640 | // There is only one selected node, and it's the selected node. 641 | (selectedNodes.length === 1 && selectedNodes[0].id === selectedNodeId)) 642 | ) { 643 | // Try to move to sibling first. 644 | const hasSibling = moveToRightSibling(); 645 | 646 | // If there's no sibling, move to parent. 647 | if (!hasSibling) moveToParent(); 648 | 649 | setNodes((nodes) => deleteFluxNode(nodes, selectedNodeId)); 650 | } else { 651 | setNodes(deleteSelectedFluxNodes); 652 | 653 | // If any of the selected nodes are the selected node, unselect it. 654 | if (selectedNodeId && selectedNodes.some((node) => node.id === selectedNodeId)) { 655 | setLastSelectedNodeId(null); 656 | setSelectedNodeId(null); 657 | } 658 | } 659 | 660 | autoZoomIfNecessary(); 661 | }; 662 | 663 | const onClear = () => { 664 | if (confirm("Are you sure you want to delete all nodes?")) { 665 | takeSnapshot(); 666 | 667 | setNodes([]); 668 | setEdges([]); 669 | setViewport({ x: 0, y: 0, zoom: 1 }); 670 | } 671 | }; 672 | 673 | /*////////////////////////////////////////////////////////////// 674 | NODE SELECTION CALLBACKS 675 | //////////////////////////////////////////////////////////////*/ 676 | 677 | const selectNode = ( 678 | id: string, 679 | computeNewNodes?: (currNodes: Node[]) => Node[] 680 | ) => { 681 | setLastSelectedNodeId(selectedNodeId); 682 | setSelectedNodeId(id); 683 | setNodes((currNodes) => 684 | // If we were passed a computeNewNodes function, use it, otherwise just use the current nodes. 685 | markOnlyNodeAsSelected(computeNewNodes ? computeNewNodes(currNodes) : currNodes, id) 686 | ); 687 | }; 688 | 689 | const moveToChild = () => { 690 | const children = getFluxNodeChildren(nodes, edges, selectedNodeId!); 691 | 692 | if (children.length > 0) { 693 | selectNode( 694 | lastSelectedNodeId !== null && 695 | children.some((node) => node.id == lastSelectedNodeId) 696 | ? lastSelectedNodeId 697 | : children[0].id 698 | ); 699 | 700 | return true; 701 | } else { 702 | return false; 703 | } 704 | }; 705 | 706 | const moveToParent = () => { 707 | const parent = getFluxNodeParent(nodes, edges, selectedNodeId!); 708 | 709 | if (parent) { 710 | selectNode(parent.id); 711 | 712 | return true; 713 | } else { 714 | return false; 715 | } 716 | }; 717 | 718 | const moveToLeftSibling = () => { 719 | const siblings = getFluxNodeSiblings(nodes, edges, selectedNodeId!); 720 | 721 | if (siblings.length > 1) { 722 | const currentIndex = siblings.findIndex((node) => node.id == selectedNodeId!)!; 723 | 724 | selectNode(siblings[mod(currentIndex - 1, siblings.length)].id); 725 | 726 | return true; 727 | } else { 728 | return false; 729 | } 730 | }; 731 | 732 | const moveToRightSibling = () => { 733 | const siblings = getFluxNodeSiblings(nodes, edges, selectedNodeId!); 734 | 735 | if (siblings.length > 1) { 736 | const currentIndex = siblings.findIndex((node) => node.id == selectedNodeId!)!; 737 | 738 | selectNode(siblings[mod(currentIndex + 1, siblings.length)].id); 739 | 740 | return true; 741 | } else { 742 | return false; 743 | } 744 | }; 745 | 746 | /*////////////////////////////////////////////////////////////// 747 | SETTINGS MODAL LOGIC 748 | //////////////////////////////////////////////////////////////*/ 749 | 750 | const { 751 | isOpen: isSettingsModalOpen, 752 | onOpen: onOpenSettingsModal, 753 | onClose: onCloseSettingsModal, 754 | onToggle: onToggleSettingsModal, 755 | } = useDisclosure(); 756 | 757 | const [settings, setSettings] = useState(() => { 758 | const rawSettings = localStorage.getItem(MODEL_SETTINGS_LOCAL_STORAGE_KEY); 759 | 760 | if (rawSettings !== null) { 761 | console.log("Restoring settings from local storage."); 762 | 763 | return JSON.parse(rawSettings) as Settings; 764 | } else { 765 | return DEFAULT_SETTINGS; 766 | } 767 | }); 768 | 769 | const isGPT4 = settings.model.includes("gpt-4"); 770 | 771 | // Auto save. 772 | const isSavingSettings = useDebouncedEffect( 773 | () => { 774 | console.log("Saved settings!"); 775 | 776 | localStorage.setItem(MODEL_SETTINGS_LOCAL_STORAGE_KEY, JSON.stringify(settings)); 777 | }, 778 | 1000, // 1 second. 779 | [settings] 780 | ); 781 | 782 | /*////////////////////////////////////////////////////////////// 783 | API KEY LOGIC 784 | //////////////////////////////////////////////////////////////*/ 785 | 786 | const [apiKey, setApiKey] = useLocalStorage(API_KEY_LOCAL_STORAGE_KEY); 787 | 788 | const isAnythingLoading = isSavingReactFlow || isSavingSettings; 789 | 790 | useBeforeunload((event: BeforeUnloadEvent) => { 791 | // Prevent leaving the page before saving. 792 | if (isAnythingLoading) event.preventDefault(); 793 | }); 794 | 795 | /*////////////////////////////////////////////////////////////// 796 | COPY MESSAGES LOGIC 797 | //////////////////////////////////////////////////////////////*/ 798 | 799 | const copyMessagesToClipboard = async () => { 800 | const messages = promptFromLineage(selectedNodeLineage, settings); 801 | 802 | if (await copySnippetToClipboard(messages)) { 803 | toast({ 804 | title: "Copied messages to clipboard!", 805 | status: "success", 806 | ...TOAST_CONFIG, 807 | }); 808 | } else { 809 | toast({ 810 | title: "Failed to copy messages to clipboard!", 811 | status: "error", 812 | ...TOAST_CONFIG, 813 | }); 814 | } 815 | }; 816 | 817 | /*////////////////////////////////////////////////////////////// 818 | RENAME NODE LOGIC 819 | //////////////////////////////////////////////////////////////*/ 820 | 821 | const showRenameInput = () => { 822 | takeSnapshot(); 823 | 824 | const selectedNode = nodes.find((node) => node.selected); 825 | const nodeId = selectedNode?.id ?? selectedNodeId; 826 | 827 | if (nodeId) { 828 | setNodes((nodes) => 829 | modifyReactFlowNodeProperties(nodes, { 830 | id: nodeId, 831 | type: ReactFlowNodeTypes.LabelUpdater, 832 | draggable: false, 833 | }) 834 | ); 835 | } 836 | }; 837 | 838 | /*////////////////////////////////////////////////////////////// 839 | WINDOW RESIZE LOGIC 840 | //////////////////////////////////////////////////////////////*/ 841 | 842 | useDebouncedWindowResize(autoZoomIfNecessary, 100); 843 | 844 | /*////////////////////////////////////////////////////////////// 845 | HOTKEYS LOGIC 846 | //////////////////////////////////////////////////////////////*/ 847 | 848 | useHotkeys("meta+s", save, HOTKEY_CONFIG); 849 | 850 | useHotkeys( 851 | "meta+p", 852 | () => newConnectedToSelectedNode(FluxNodeType.User), 853 | HOTKEY_CONFIG 854 | ); 855 | useHotkeys( 856 | "meta+u", 857 | () => newConnectedToSelectedNode(FluxNodeType.System), 858 | HOTKEY_CONFIG 859 | ); 860 | 861 | useHotkeys("meta+shift+p", () => newUserNodeLinkedToANewSystemNode(), HOTKEY_CONFIG); 862 | 863 | useHotkeys("meta+.", () => fitView(FIT_VIEW_SETTINGS), HOTKEY_CONFIG); 864 | useHotkeys("meta+/", onToggleSettingsModal, HOTKEY_CONFIG); 865 | useHotkeys("meta+shift+backspace", onClear, HOTKEY_CONFIG); 866 | 867 | useHotkeys("meta+z", undo, HOTKEY_CONFIG); 868 | useHotkeys("meta+shift+z", redo, HOTKEY_CONFIG); 869 | 870 | useHotkeys("meta+e", showRenameInput, HOTKEY_CONFIG); 871 | 872 | useHotkeys("meta+up", moveToParent, HOTKEY_CONFIG); 873 | useHotkeys("meta+down", moveToChild, HOTKEY_CONFIG); 874 | useHotkeys("meta+left", moveToLeftSibling, HOTKEY_CONFIG); 875 | useHotkeys("meta+right", moveToRightSibling, HOTKEY_CONFIG); 876 | useHotkeys("meta+return", () => submitPrompt(false), HOTKEY_CONFIG); 877 | useHotkeys("meta+shift+return", () => submitPrompt(true), HOTKEY_CONFIG); 878 | useHotkeys("meta+k", completeNextWords, HOTKEY_CONFIG); 879 | useHotkeys("meta+backspace", deleteSelectedNodes, HOTKEY_CONFIG); 880 | useHotkeys("ctrl+c", copyMessagesToClipboard, HOTKEY_CONFIG); 881 | 882 | /*////////////////////////////////////////////////////////////// 883 | APP 884 | //////////////////////////////////////////////////////////////*/ 885 | 886 | return ( 887 | <> 888 | {!isValidAPIKey(apiKey) && } 889 | 890 | 898 | 904 | 905 | 924 | 931 | 940 | 942 | newUserNodeLinkedToANewSystemNode() 943 | } 944 | newConnectedToSelectedNode={newConnectedToSelectedNode} 945 | deleteSelectedNodes={deleteSelectedNodes} 946 | submitPrompt={() => submitPrompt(false)} 947 | regenerate={() => submitPrompt(true)} 948 | completeNextWords={completeNextWords} 949 | undo={undo} 950 | redo={redo} 951 | onClear={onClear} 952 | copyMessagesToClipboard={copyMessagesToClipboard} 953 | showRenameInput={showRenameInput} 954 | moveToParent={moveToParent} 955 | moveToChild={moveToChild} 956 | moveToLeftSibling={moveToLeftSibling} 957 | moveToRightSibling={moveToRightSibling} 958 | autoZoom={autoZoom} 959 | onOpenSettingsModal={() => { 960 | if (MIXPANEL_TOKEN) mixpanel.track("Opened Settings Modal"); 961 | onOpenSettingsModal(); 962 | }} 963 | /> 964 | 965 | 966 | {isAnythingLoading ? ( 967 | 968 | ) : ( 969 | 970 | )} 971 | 972 | 973 | 974 | { 1004 | setLastSelectedNodeId(selectedNodeId); 1005 | setSelectedNodeId(node.id); 1006 | }} 1007 | > 1008 | 1009 | 1010 | 1011 | 1012 | 1013 | 1014 | {selectedNodeLineage.length >= 1 ? ( 1015 | { 1023 | takeSnapshot(); 1024 | setNodes((nodes) => 1025 | modifyFluxNodeText(nodes, { 1026 | asHuman: true, 1027 | id: selectedNodeId!, 1028 | text, 1029 | }) 1030 | ); 1031 | }} 1032 | submitPrompt={() => submitPrompt(false)} 1033 | /> 1034 | ) : ( 1035 | 1041 | newUserNodeLinkedToANewSystemNode()} 1047 | color={getFluxNodeTypeDarkColor(FluxNodeType.GPT)} 1048 | > 1049 | Create a new conversation tree 1050 | 1051 | 1052 | )} 1053 | 1054 | 1055 | 1056 | > 1057 | ); 1058 | } 1059 | 1060 | export default App; 1061 | --------------------------------------------------------------------------------
4 | LLM power tool 5 | 6 | 7 | Announcement 8 | · 9 | Try Online 10 | · 11 | Report a Bug 12 |