Skip to content

Commit

Permalink
boop
Browse files Browse the repository at this point in the history
  • Loading branch information
actualwitch committed Dec 6, 2024
1 parent 1f4adb0 commit 7efad02
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 88 deletions.
Binary file modified bun.lockb
Binary file not shown.
9 changes: 8 additions & 1 deletion src/components/ModalTrigger.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@ import { Button } from "../style";
import { Modal } from "./Modal";
import { cloneElement } from "react";

export function ModalTrigger({ label, children, ...props }: { label: string; children: (close: () => void) => JSX.Element }) {
export function ModalTrigger({
label,
children,
...props
}: {
label: string;
children: (close: () => void) => JSX.Element;
}) {
const state = useOverlayTriggerState(props);
const { triggerProps, overlayProps } = useOverlayTrigger({ type: "dialog" }, state);

Expand Down
2 changes: 1 addition & 1 deletion src/components/Slider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const Track = styled.div<{ orientation: "horizontal" | "vertical"; disabled?: bo
display: block;
position: absolute;
background: gray;
height: 3px;
width: 100%;
top: 50%;
Expand Down
1 change: 0 additions & 1 deletion src/components/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { useSetAtom } from "jotai";
import { useNavigate } from "react-router";
import { experimentAtom, parentAtom, type Message } from "../state/common";


export const ForkButton = ({ experiment, parent }: { experiment?: Message[]; parent?: string }) => {
const setExoeriment = useSetAtom(experimentAtom);
const setParent = useSetAtom(parentAtom);
Expand Down
28 changes: 6 additions & 22 deletions src/entry/_deflector.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@


// inversion of control container to store parked requests
export type ParkingLot = Map<
string,
[{}, (value: Response) => void, (reason: unknown) => void]
>;
export type ParkingLot = Map<string, [{}, (value: Response) => void, (reason: unknown) => void]>;

export const parkingLot: ParkingLot = new Map();

Expand All @@ -17,14 +12,11 @@ export const setDeflectorStatus = (status: boolean) => {
export const doDeflection = async (request: Request) => {
const deflectTo = c.req.header("x-fpx-deflect-to");
if (!isDeflectorEnabled || !deflectTo) {
return ;
return;
}

const traceId = crypto.randomUUID();
const [requestUrl, deflectionType] = getTargetUrlAndDeflectionType(
deflectTo,
request.url,
);
const [requestUrl, deflectionType] = getTargetUrlAndDeflectionType(deflectTo, request.url);
console.info(`Deflecting request to ${requestUrl}`);
const newHeaders = new Headers(c.req.raw.headers);
newHeaders.append("x-fpx-trace-id", traceId);
Expand Down Expand Up @@ -83,13 +75,7 @@ export const doDeflection = async (request: Request) => {
throw new Error();
}
const duration = Date.now() - startTime;
await handleSuccessfulRequest(
db,
requestId,
duration,
response.clone(),
traceId,
);
await handleSuccessfulRequest(db, requestId, duration, response.clone(), traceId);

return response;
} catch (error) {
Expand All @@ -108,9 +94,7 @@ function getTargetUrlAndDeflectionType(
requestString: string,
): [finalUrl: URL, deflectionType: DeflectionType] {
try {
const [targetUrl, requestUrl] = [targetString, requestString].map(
(url) => new URL(url),
);
const [targetUrl, requestUrl] = [targetString, requestString].map((url) => new URL(url));
for (const prop of ["hostname", "port", "protocol"] as const) {
requestUrl[prop] = targetUrl[prop];
}
Expand All @@ -120,4 +104,4 @@ function getTargetUrlAndDeflectionType(
url.hostname = targetString;
return [url, "serverSimulator"];
}
}
}
4 changes: 2 additions & 2 deletions src/pages/NewExperiment.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,10 @@ export function withIds<T extends string>(items: T[]) {
return items.map((name) => ({
id: name,
name,
}))
}));
}
export const providerTypes = ["anthropic", "mistral", "openai"] as const;
export type ProviderType = typeof providerTypes[number];
export type ProviderType = (typeof providerTypes)[number];
export const providers = withIds(providerTypes);

const ModalContainer = styled.div`
Expand Down
51 changes: 30 additions & 21 deletions src/pages/Parameters.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import { css } from "@emotion/react";
import styled from "@emotion/styled";
import { useAtom } from "jotai";
import { experimentLayoutAtom, isDarkModeAtom, tokensAtom } from "../state/common";
import { bs, Button } from "../style";
import { withFormStyling, type FormProps } from "../style/form";
import { hasResolvedTokenAtom } from "../state/inference";
import { Switch } from "../components/switch";
import { useState, type PropsWithChildren } from "react";
import { type PropsWithChildren, useState } from "react";
import { Item } from "react-stately";
import { ModalTrigger } from "../components/ModalTrigger";
import { Block, providers, providerTypes, type ProviderType } from "./NewExperiment";
import { Select } from "../components/Select";
import { Item } from "react-stately";
import { withDarkMode, type WithDarkMode } from "../style/darkMode";
import { css } from "@emotion/react";
import { Switch } from "../components/switch";
import { experimentLayoutAtom, isDarkModeAtom, tokensAtom } from "../state/common";
import { Button, bs } from "../style";
import { type WithDarkMode, withDarkMode } from "../style/darkMode";
import { type FormProps, withFormStyling } from "../style/form";
import { Palette } from "../style/palette";
import { type ProviderType, providers } from "./NewExperiment";
import { hasBackend } from "../utils/realm";
import { View } from "../components/view";

const Input = styled.input<FormProps>(withFormStyling);

Expand Down Expand Up @@ -53,17 +54,21 @@ const Container = styled.div<WithDarkMode>`
input {
width: 100%;
}
${p => withDarkMode(p.isDarkMode, css`
background: ${Palette.black};
`)}
${(p) =>
withDarkMode(
p.isDarkMode,
css`
background: ${Palette.black};
`,
)}
`;

const Actions = styled.p`
display: flex;
justify-content: flex-end;
`;

const ModalContent = ({ children, close }: PropsWithChildren<{close: () => void}>) => {
const ModalContent = ({ children, close }: PropsWithChildren<{ close: () => void }>) => {
const [isDarkMode] = useAtom(isDarkModeAtom);
const [selectedProvider, setSelectedProvider] = useState<ProviderType | null>(null);
const [token, setToken] = useState("");
Expand All @@ -79,7 +84,7 @@ const ModalContent = ({ children, close }: PropsWithChildren<{close: () => void}
}
setTokens({ ...tokens, [selectedProvider]: value });
close();
}
};

return (
<Container isDarkMode={isDarkMode}>
Expand All @@ -96,17 +101,22 @@ const ModalContent = ({ children, close }: PropsWithChildren<{close: () => void}
<p>
<Input
type="password"
placeholder="Token or 1password reference"
placeholder={hasBackend() ? "Token or 1password reference" : "Token"}
value={token}
onChange={(e) => setToken(e.target.value)}
/>
</p>
<Actions>
{children}
<Button onClick={(e) => {
e.preventDefault();
submit();
}} disabled={!selectedProvider || !token}>Add</Button>
<Button
onClick={(e) => {
e.preventDefault();
submit();
}}
disabled={!selectedProvider || !token}
>
Add
</Button>
</Actions>
</Container>
);
Expand All @@ -115,7 +125,6 @@ const ModalContent = ({ children, close }: PropsWithChildren<{close: () => void}
export default function Configure() {
const [isDarkMode, setIsDarkMode] = useAtom(isDarkModeAtom);
const [experimentLayout, setExperimentLayout] = useAtom(experimentLayoutAtom);
const [hasResolvedToken] = useAtom(hasResolvedTokenAtom);
const [tokens, setTokens] = useAtom(tokensAtom);
return (
<>
Expand Down
73 changes: 37 additions & 36 deletions src/state/inference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,43 +26,41 @@ export const MistralModel = Union(

export const tempAtom = atom(0.0);

export const resolvedTokensAtom = divergentAtom(
() => {
return atom<Store["tokens"] | Promise<Store["tokens"]>>(async (get) => {
const references = get(tokensAtom);
const result: Store["tokens"] = {};
if (!references) return result;
const promises = Object.entries(references).map(async ([key, ref]) => {
if (!ref) return [key, null];
if (ref.startsWith("op:")) {
const { spawn } = await maybeImport("child_process");
if (!spawn) return [key, null];
const handle = spawn("op", ["read", ref]);
return await new Promise<string | null>((ok, ko) => {
handle.stdout.on("data", (data: unknown) => {
ok(String(data).trim());
});

handle.stderr.on("data", (data: unknown) => {
console.error(String(data));
});

handle.on("close", () => {
ok(null);
});
export const resolvedTokensAtom = divergentAtom(() => {
return atom<Store["tokens"] | Promise<Store["tokens"]>>(async (get) => {
const references = get(tokensAtom);
const result: Store["tokens"] = {};
if (!references) return result;
const promises = Object.entries(references).map(async ([key, ref]) => {
if (!ref) return [key, null];
if (ref.startsWith("op:")) {
const { spawn } = await maybeImport("child_process");
if (!spawn) return [key, null];
const handle = spawn("op", ["read", ref]);
const token = await new Promise<string | null>((ok, ko) => {
handle.stdout.on("data", (data: unknown) => {
ok(String(data).trim());
});
}
return [key, ref];
});
const resolved = await Promise.all(promises);
for (const [key, value] of resolved) {
result[key] = value;

handle.stderr.on("data", (data: unknown) => {
console.error(String(data));
});

handle.on("close", () => {
ok(null);
});
});
return [key, token];
}
return result;
return [key, null];
});
},
() => tokensAtom,
);
const resolved = await Promise.all(promises);
for (const [key, value] of resolved) {
result[key] = value;
}
return result;
});
});

export const hasResolvedTokenAtom = entangledAtom(
"has resolved tokens",
Expand Down Expand Up @@ -122,7 +120,10 @@ export const runExperimentAsAnthropic = entangledAtom(

const { stream, ...experimentAsAnthropic } = experimentToAnthropic(experiment);

const anthropic = new Anthropic({ apiKey: resolvedTokens.anthropic, dangerouslyAllowBrowser: hasBackend() ? undefined : true });
const anthropic = new Anthropic({
apiKey: resolvedTokens.anthropic,
dangerouslyAllowBrowser: hasBackend() ? undefined : true,
});
if (stream) {
const stream = await anthropic.messages.create({
...experimentAsAnthropic,
Expand Down Expand Up @@ -168,7 +169,7 @@ export const runExperimentAsAnthropic = entangledAtom(
export const runExperimentAsOpenAi = entangledAtom(
{ name: "run-experiment-openai" },
atom(null, async (get, set) => {
const resolvedTokens = await store.get(resolvedTokensAtom);
const resolvedTokens = await get(resolvedTokensAtom);
const experiment = get(experimentAtom);

if (!resolvedTokens.openai || !experiment) {
Expand Down
6 changes: 2 additions & 4 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { SyncStringStorage } from "jotai/vanilla/utils/atomWithStorage";
import { existsSync, readFileSync, writeFileSync, mkdirSync } from "node:fs";
import {platform} from "node:os";
import { platform } from "node:os";
import { getRealm } from "./realm";
import { DEBUG } from "../const";

Expand Down Expand Up @@ -38,9 +38,7 @@ const getStoragePath = () => {
mkdirSync(`${home}/.experiment`, { recursive: true });
}
return `${home}/.experiment`;
}


};

export function createFileStorage(...keys: string[]): SyncStringStorage {
const store = new Map<string, string>();
Expand Down

0 comments on commit 7efad02

Please sign in to comment.