Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .env/.env.development
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
PUBLIC_WEBSITE_URL="http://localhost:3000"
ALLOWED_ORIGIN="http://localhost:3000,moz-extension://*,chrome-extension://*"
4 changes: 4 additions & 0 deletions .env/.env.example
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Website (Required)
PUBLIC_WEBSITE_URL=

# Piston (Required)
PISTON_API_KEY=
ALLOWED_ORIGIN=

# Analytics (Optional)
PUBLIC_POSTHOG_KEY=

Expand Down
3 changes: 1 addition & 2 deletions apps/browser-extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@
},
"dependencies": {
"@evaluate/components": "workspace:^",
"@evaluate/execute": "workspace:^",
"@evaluate/logger": "workspace:^",
"@evaluate/runtimes": "workspace:^",
"@evaluate/style": "workspace:^",
"@t3-oss/env-core": "^0.13.8",
"lucide-react": "^0.546.0",
"piston.ts": "workspace:^",
"posthog-js": "^1.276.0",
"react": "^19.2.1",
"react-dom": "^19.2.1",
Expand Down
15 changes: 10 additions & 5 deletions apps/browser-extension/src/background/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { type ExecuteResult, executeCode } from '@evaluate/execute';
import { type Runtime, searchForRuntimes } from '@evaluate/runtimes';
import type { ExecuteResult, Runtime } from 'piston.ts';
import type { ProtocolWithReturn } from 'webext-bridge';
import { onMessage, sendMessage } from 'webext-bridge/background';
import browser from 'webextension-polyfill';
import env from '~/env';
import piston from '~/services/piston.js';
import posthog, { sessionLog } from '~/services/posthog';

declare module 'webext-bridge' {
Expand All @@ -19,7 +19,11 @@ declare module 'webext-bridge' {
>;
executionFailed: ProtocolWithReturn<{ errorMessage: string }, void>;
executionFinished: ProtocolWithReturn<
{ code: string; runtimes: Runtime[]; results: ExecuteResult[] },
{
code: string;
runtimes: (typeof Runtime._output)[];
results: (typeof ExecuteResult._output)[];
},
void
>;
getBackgroundSessionId: ProtocolWithReturn<void, string | undefined>;
Expand Down Expand Up @@ -55,7 +59,7 @@ browser.contextMenus.onClicked.addListener(async (info, tab) => {

const selection = await sendMessage('getSelectionInfo', void 0, endpoint);
const { code, resolvables } = selection;
const runtimes = (await searchForRuntimes(resolvables)).slice(0, 5);
const runtimes = (await piston.runtimes.search(resolvables)).slice(0, 5);
if (!runtimes.length)
return sendMessage('unknownRuntime', { code }, endpoint);

Expand All @@ -65,7 +69,8 @@ browser.contextMenus.onClicked.addListener(async (info, tab) => {

const promises = [];
for (const runtime of runtimes) {
const initialPromise = executeCode(runtime, { code })
const initialPromise = piston
.execute(runtime, { code })
.then(([result, options]) => {
posthog?.capture('executed_code', {
runtime_id: runtime.id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ import {
TabsList,
TabsTrigger,
} from '@evaluate/components/tabs';
import type { ExecuteResult } from '@evaluate/execute';
import type { Runtime } from '@evaluate/runtimes';
import type { ExecuteResult, Runtime } from 'piston.ts';
import browser from 'webextension-polyfill';
import env from '~/env';
import { ResultDialog } from './result';
Expand All @@ -26,9 +25,9 @@ export function ExecutionDialog({
}: {
portal: HTMLElement;
code: string;
runtimes: Runtime[];
results: ExecuteResult[];
setResults: (results: ExecuteResult[]) => void;
runtimes: (typeof Runtime._output)[];
results: (typeof ExecuteResult._output)[];
setResults: (results: (typeof ExecuteResult._output)[]) => void;
}) {
const open = results.length > 0;
const onClose = () => setResults([]);
Expand Down
8 changes: 4 additions & 4 deletions apps/browser-extension/src/content-script/execution/index.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { toast } from '@evaluate/components/toast';
import { type ExecuteResult, makePickRuntimePathname } from '@evaluate/execute';
import type { Runtime } from '@evaluate/runtimes';
import type { ExecuteResult, Runtime } from 'piston.ts';
import { makePickRuntimePathname } from 'piston.ts/evaluate';
import { useEffect, useState } from 'react';
import { onMessage } from 'webext-bridge/content-script';
import env from '~/env.js';
import { ExecutionDialog } from './dialog';

export function Execution({ dialogPortal }: { dialogPortal: HTMLElement }) {
const [code, setCode] = useState('');
const [runtimes, setRuntimes] = useState<Runtime[]>([]);
const [results, setResults] = useState<ExecuteResult[]>([]);
const [runtimes, setRuntimes] = useState<(typeof Runtime._output)[]>([]);
const [results, setResults] = useState<(typeof ExecuteResult._output)[]>([]);

useEffect(() => {
let lastToastId: string | number;
Expand Down
11 changes: 5 additions & 6 deletions apps/browser-extension/src/content-script/execution/result.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { Button } from '@evaluate/components/button';
import { Label } from '@evaluate/components/label';
import { Textarea } from '@evaluate/components/textarea';
import { ExternalLinkIcon } from 'lucide-react';
import type { ExecuteResult, Runtime } from 'piston.ts';
import {
type ExecuteResult,
makeEditCodePathname,
makePickRuntimePathname,
} from '@evaluate/execute';
import type { Runtime } from '@evaluate/runtimes';
import { ExternalLinkIcon } from 'lucide-react';
} from 'piston.ts/evaluate';
import { twMerge as cn } from 'tailwind-merge';
import env from '~/env.js';

Expand All @@ -16,9 +15,9 @@ export function ResultDialog({
options,
result,
}: {
runtime: Runtime;
runtime: typeof Runtime._output;
options: { code: string };
result: ExecuteResult;
result: typeof ExecuteResult._output;
}) {
let output = result.output;
if (result.compile?.expired)
Expand Down
6 changes: 6 additions & 0 deletions apps/browser-extension/src/services/piston.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Piston } from 'piston.ts';
import env from '~/env.js';

export default new Piston({
baseUrl: `${env.VITE_PUBLIC_WEBSITE_URL}api/piston`,
});
3 changes: 1 addition & 2 deletions apps/discord-bot/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,13 @@
},
"dependencies": {
"@buape/carbon": "^0.13.0",
"@evaluate/execute": "workspace:^",
"@evaluate/logger": "workspace:^",
"@evaluate/runtimes": "workspace:^",
"@sayable/carbon": "0.0.0-alpha.4",
"@sayable/react": "0.0.0-alpha.6",
"@t3-oss/env-core": "^0.13.8",
"date-fns": "^4.1.0",
"es-toolkit": "^1.40.0",
"piston.ts": "workspace:^",
"posthog-node": "^5.5.1",
"sayable": "0.0.0-alpha.6",
"zod": "^4.1.12"
Expand Down
6 changes: 3 additions & 3 deletions apps/discord-bot/src/commands/evaluate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import {
Command,
type CommandInteraction,
} from '@buape/carbon';
import { fetchAllRuntimes, searchForRuntimes } from '@evaluate/runtimes';
import { sayable } from '@sayable/carbon';
import type { Sayable } from 'sayable';
import { EvaluateModal } from '~/components/evaluate-modal';
import { handleEvaluating } from '~/handlers/evaluate';
import { getInteractionContext } from '~/helpers/session-context';
import piston from '~/services/piston.js';
import { captureEvent } from '~/services/posthog';

export class EvaluateCommand extends sayable(Command) {
Expand Down Expand Up @@ -51,12 +51,12 @@ export class EvaluateCommand extends sayable(Command) {
const runtime = interaction.options.getString('runtime');

if (runtime) {
const runtimes = await searchForRuntimes(runtime);
const runtimes = await piston.runtimes.search(runtime);
return interaction.respond(
runtimes.slice(0, 25).map((r) => ({ name: r.name, value: r.id })),
);
} else {
const runtimes = await fetchAllRuntimes();
const runtimes = await piston.runtimes();
return interaction.respond(
runtimes.slice(0, 25).map((r) => ({ name: r.name, value: r.id })),
);
Expand Down
1 change: 1 addition & 0 deletions apps/discord-bot/src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export default createEnv({
server: {
WEBSITE_URL: z.url().transform((v) => new URL(v)),
POSTHOG_KEY: z.string().optional(),
PISTON_API_KEY: z.string().min(1),
Comment thread
k0d13 marked this conversation as resolved.
DISCORD_TOKEN: z.string().min(1).optional(),
DISCORD_PUBLIC_KEY: z.string().min(1).optional(),
DISCORD_CLIENT_ID: z.string().min(1).optional(),
Expand Down
27 changes: 13 additions & 14 deletions apps/discord-bot/src/handlers/evaluate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,14 @@ import {
ModalInteraction,
Row,
} from '@buape/carbon';
import {
type CodeOptions,
type ExecuteOptions,
type ExecuteResult,
executeCode,
makeEditCodePathname,
} from '@evaluate/execute';
import { type Runtime, searchForRuntimes } from '@evaluate/runtimes';
import { makeEditCodePathname } from 'piston.ts/evaluate';
import { EditEvaluationButton } from '~/components/edit-evaluation-button.js';
import { OpenEvaluationButton } from '~/components/open-evaluation-button.js';
import env from '~/env.js';
import { codeBlock } from '~/helpers/discord-formatting.js';
import { resolveEmoji } from '~/helpers/resolve-emoji.js';
import { getInteractionContext } from '~/helpers/session-context.js';
import piston from '~/services/piston.js';
import { captureEvent } from '~/services/posthog.js';

//
Expand Down Expand Up @@ -53,7 +47,7 @@ function isEdit(
export async function handleEvaluating(
interaction: CommandInteraction | ModalInteraction,
runtimeResolvable: string,
options: CodeOptions,
options: Parameters<typeof piston.execute>[1],
) {
// HACK: If trying to edit an existing evaluation owned by another user, create new instead
if (
Expand All @@ -66,7 +60,9 @@ export async function handleEvaluating(
if (isNew(interaction)) await interaction.defer();
if (isEdit(interaction)) await interaction.acknowledge();

const runtime = await searchForRuntimes(runtimeResolvable).then((r) => r[0]);
const runtime = await piston.runtimes
.search(runtimeResolvable)
.then((r) => r[0]);
if (!runtime) {
const message = interaction.say`I was unable to find the runtime you were looking for, try again with another name.`;
if (isNew(interaction))
Expand All @@ -82,7 +78,10 @@ export async function handleEvaluating(
throw new Error(message);
}

const [executeResult, executeOptions] = await executeCode(runtime, options);
const [executeResult, executeOptions] = await piston.execute(
runtime,
options,
);

captureEvent(getInteractionContext(interaction.rawData), 'executed_code', {
runtime_id: runtime.id,
Expand Down Expand Up @@ -120,9 +119,9 @@ export async function handleEvaluating(

export function createEvaluationPayload(
interaction: BaseInteraction<APIInteraction>,
runtime: Runtime,
options: ExecuteOptions,
result: ExecuteResult,
runtime: Parameters<typeof piston.execute>[0],
options: Awaited<ReturnType<typeof piston.execute>>[1],
result: Awaited<ReturnType<typeof piston.execute>>[0],
output: string,
): MessagePayload {
const embed = new Embed({
Expand Down
18 changes: 9 additions & 9 deletions apps/discord-bot/src/locales/en/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,41 @@ msgstr ""
"Language: en\n"
"X-Generator: sayable\n"

#: src/handlers/evaluate.ts:71
#: src/handlers/evaluate.ts:67
msgid "I was unable to find the runtime you were looking for, try again with another name."
msgstr "I was unable to find the runtime you were looking for, try again with another name."

#: src/handlers/evaluate.ts:97
#: src/handlers/evaluate.ts:96
msgid "Your code compilation exceeded the allotted time and was terminated. Consider optimising your code for faster compilation."
msgstr "Your code compilation exceeded the allotted time and was terminated. Consider optimising your code for faster compilation."

#: src/handlers/evaluate.ts:99
#: src/handlers/evaluate.ts:98
msgid "Your code execution exceeded the allotted time and was terminated. Consider optimising it for better performance."
msgstr "Your code execution exceeded the allotted time and was terminated. Consider optimising it for better performance."

#: src/handlers/evaluate.ts:101
#: src/handlers/evaluate.ts:100
msgid "Your code executed successfully; however, it did not generate any output for the console."
msgstr "Your code executed successfully; however, it did not generate any output for the console."

#: src/handlers/evaluate.ts:107
#: src/handlers/evaluate.ts:106
msgid "Output was too large to display, [click here to view the full output]({url})."
msgstr "Output was too large to display, [click here to view the full output]({url})."

#: src/handlers/evaluate.ts:129
#: src/handlers/evaluate.ts:128
msgid "Code Evaluation"
msgstr "Code Evaluation"

#: src/handlers/evaluate.ts:144
#: src/handlers/evaluate.ts:143
#: src/components/evaluate-modal.ts:102
msgid "CLI Arguments"
msgstr "CLI Arguments"

#: src/handlers/evaluate.ts:150
#: src/handlers/evaluate.ts:149
#: src/components/evaluate-modal.ts:120
msgid "STDIN"
msgstr "STDIN"

#: src/handlers/evaluate.ts:154
#: src/handlers/evaluate.ts:153
msgid "Output"
msgstr "Output"

Expand Down
6 changes: 6 additions & 0 deletions apps/discord-bot/src/services/piston.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Piston } from 'piston.ts';
import env from '../env.js';

export default new Piston({
apiKey: env.PISTON_API_KEY,
});
3 changes: 1 addition & 2 deletions apps/website/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@
"@codemirror/commands": "^6.9.0",
"@codemirror/view": "^6.38.6",
"@evaluate/components": "workspace:^",
"@evaluate/execute": "workspace:^",
"@evaluate/hooks": "workspace:^",
"@evaluate/logger": "workspace:^",
"@evaluate/runtimes": "workspace:^",
"@evaluate/style": "workspace:^",
"@hookform/resolvers": "^5.2.2",
"@sayable/react": "0.0.0-alpha.8",
Expand All @@ -42,6 +40,7 @@
"material-icon-theme": "^5.27.0",
"next": "15.5.7",
"next-themes": "npm:@wits/next-themes@^0.2.16",
"piston.ts": "workspace:^",
"posthog-js": "^1.276.0",
"react": "^19.2.1",
"react-dnd": "^16.0.1",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
import { fetchAllRuntimes, fetchRuntimeById } from '@evaluate/runtimes';
import { notFound } from 'next/navigation';
import { generateBaseMetadata } from '~/app/[locale]/metadata';

import { Editor } from '~/components/editor';
import { Explorer } from '~/components/explorer';
import { ExplorerProvider } from '~/components/explorer/use';
import { Terminal } from '~/components/terminal';
import { TerminalProvider } from '~/components/terminal/use';
import say from '~/i18n';
import piston from '~/services/piston';
import { EditorWrapper } from './wrapper';

export async function generateStaticParams() {
const runtimes = await fetchAllRuntimes();
const runtimes = await piston.runtimes();
return runtimes.map((r) => ({ playground: r.id }));
}

export async function generateMetadata({
params,
}: PageProps<'/[locale]/playgrounds/[playground]'>) {
const { locale, playground } = await params;
const runtime = await fetchRuntimeById(decodeURIComponent(playground));
const runtime = await piston.runtimes.get(decodeURIComponent(playground));
if (!runtime) notFound();

return generateBaseMetadata(
Expand All @@ -39,7 +38,7 @@ export default async function EditorPage({
params,
}: PageProps<'/[locale]/playgrounds/[playground]'>) {
const { playground } = await params;
const runtime = await fetchRuntimeById(decodeURIComponent(playground));
const runtime = await piston.runtimes.get(decodeURIComponent(playground));
if (!runtime) notFound();

return (
Expand Down
Loading