-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
842 additions
and
223 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { | ||
CodeMirror, | ||
type CodeMirrorRef, | ||
type CompletionContext, | ||
EditorView, | ||
External, | ||
defaultKeymap, | ||
javascript, | ||
javascriptLanguage, | ||
keymap, | ||
} from "@rivet-gg/components/code-mirror"; | ||
import { forwardRef } from "react"; | ||
|
||
const deleteBgTheme = EditorView.theme({ | ||
".cm-content": { padding: 0 }, | ||
}); | ||
|
||
interface ReplInputProps { | ||
rpcs: string[]; | ||
onRun: (code: string) => void; | ||
} | ||
|
||
export const ReplInput = forwardRef<CodeMirrorRef, ReplInputProps>( | ||
({ rpcs, onRun }, ref) => { | ||
const rivetKeymap = keymap.of([ | ||
{ | ||
key: "Shift-Enter", | ||
run: (editor) => { | ||
onRun(editor?.state.doc.toString()); | ||
editor.dispatch({ | ||
changes: { | ||
from: 0, | ||
to: editor.state.doc.length, | ||
insert: "", | ||
}, | ||
annotations: [External.of(true)], | ||
}); | ||
return true; | ||
}, | ||
}, | ||
...defaultKeymap, | ||
]); | ||
|
||
const replAutocomplete = javascriptLanguage.data.of({ | ||
autocomplete: (context: CompletionContext) => { | ||
const word = context.matchBefore(/^\w*/); | ||
if (!word || (word?.from === word?.to && !context.explicit)) | ||
return null; | ||
return { | ||
from: word.from, | ||
to: word.to, | ||
boost: 99, | ||
options: [ | ||
{ | ||
label: "wait", | ||
apply: "wait", | ||
validFor: /^(@\w*)?$/, | ||
info: "Helper function to wait for a number of milliseconds", | ||
}, | ||
...rpcs.map((rpc) => ({ | ||
label: rpc, | ||
apply: rpc, | ||
validFor: /^(@\w*)?$/, | ||
info: `Call "${rpc}" RPC on Actor`, | ||
})), | ||
], | ||
}; | ||
}, | ||
}); | ||
|
||
return ( | ||
<CodeMirror | ||
autoFocus | ||
basicSetup={{ | ||
lineNumbers: false, | ||
lintKeymap: false, | ||
foldKeymap: false, | ||
searchKeymap: false, | ||
defaultKeymap: false, | ||
foldGutter: false, | ||
}} | ||
extensions={[ | ||
deleteBgTheme, | ||
rivetKeymap, | ||
javascript(), | ||
replAutocomplete, | ||
]} | ||
/> | ||
); | ||
}, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
import { cn } from "@rivet-gg/components"; | ||
import { | ||
Icon, | ||
faArrowLeft, | ||
faArrowRight, | ||
faCircleX, | ||
faSpinner, | ||
} from "@rivet-gg/icons"; | ||
import type { ReplCommand } from "./repl-state"; | ||
|
||
interface ReplLogProps { | ||
commands: ReplCommand[]; | ||
} | ||
|
||
export function ReplLog({ commands }: ReplLogProps) { | ||
return ( | ||
<div className="flex flex-col gap-3"> | ||
{commands.map((command) => ( | ||
<div key={command.key} className="flex flex-col gap-1"> | ||
<div className="text-xs font-mono text-muted-foreground flex gap-1"> | ||
<div className="min-w-4 text-center"> | ||
<Icon icon={faArrowRight} /> | ||
</div> | ||
|
||
{command.status === "pending" ? ( | ||
<div className="text-muted-foreground text-xs font-mono"> | ||
... | ||
</div> | ||
) : null} | ||
|
||
{command.status !== "pending" && command.formatted ? ( | ||
<div | ||
className="text-xs font-mono text-muted-foreground" | ||
style={{ color: command.formatted.fg }} | ||
> | ||
{command.formatted?.tokens.map( | ||
(tokensLine, index) => ( | ||
<span | ||
// biome-ignore lint/suspicious/noArrayIndexKey: we're using the index as a key here because the array is static | ||
key={index} | ||
className="block" | ||
> | ||
{tokensLine.map((token, index) => ( | ||
<span | ||
// biome-ignore lint/suspicious/noArrayIndexKey: we're using the index as a key here because the array is static | ||
key={index} | ||
style={{ | ||
color: token.color, | ||
}} | ||
className="whitespace-pre" | ||
> | ||
{token.content} | ||
</span> | ||
))} | ||
</span> | ||
), | ||
)} | ||
</div> | ||
) : null} | ||
</div> | ||
<div className="flex flex-col gap-1 pl-4 max-w-full min-w-0"> | ||
{command.logs?.map((log, index) => ( | ||
<div | ||
// biome-ignore lint/suspicious/noArrayIndexKey: we're using the index as a key here because the array is static | ||
key={index} | ||
className={cn( | ||
"text-xs font-mono break-words min-w-0 max-w-100", | ||
{ | ||
"text-muted-destructive": | ||
log.level === "error", | ||
}, | ||
)} | ||
> | ||
{log.message} | ||
</div> | ||
))} | ||
</div> | ||
{command.status !== "success" && | ||
command.status !== "error" ? ( | ||
<div className="text-xs font-mono text-muted-foreground flex py-0.5 gap-1"> | ||
<div className="min-w-4 text-center"> | ||
<Icon | ||
icon={faSpinner} | ||
className="animate-spin" | ||
/> | ||
</div> | ||
Waiting for response... | ||
</div> | ||
) : null} | ||
{command.status === "success" ? ( | ||
<div className="text-xs font-mono text-muted-foreground flex py-0.5 gap-1 w-full"> | ||
<div className="min-w-4 text-center"> | ||
<Icon icon={faArrowLeft} /> | ||
</div> | ||
|
||
<div className="break-words min-w-0 max-w-full"> | ||
{(command.result as string) || "undefined"} | ||
</div> | ||
</div> | ||
) : null} | ||
{command.status === "error" ? ( | ||
<div className="text-xs font-mono bg-muted-destructive/50 flex py-0.5 gap-1"> | ||
<div className="min-w-4 text-center"> | ||
<Icon icon={faCircleX} /> | ||
</div> | ||
|
||
<div className="break-words"> | ||
{typeof command.error === "object" && | ||
command.error && | ||
"message" in command.error | ||
? (command.error.message as string) || | ||
"undefined" | ||
: "undefined"} | ||
</div> | ||
</div> | ||
) : null} | ||
</div> | ||
))} | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { z } from "zod"; | ||
|
||
export const MessageSchema = z.object({ | ||
type: z.literal("code"), | ||
data: z.string(), | ||
managerUrl: z.string(), | ||
actorId: z.string(), | ||
rpcs: z.array(z.string()), | ||
id: z.string(), | ||
}); | ||
|
||
export const FormattedCodeSchema = z | ||
.object({ | ||
fg: z.string(), | ||
tokens: z.array( | ||
z.array( | ||
z.object({ | ||
content: z.string(), | ||
color: z.string(), | ||
}), | ||
), | ||
), | ||
}) | ||
.catch((ctx) => ctx.input); | ||
|
||
export const LogSchema = z.object({ | ||
level: z.string(), | ||
message: z.any(), | ||
}); | ||
|
||
export const ResponseSchema = z.discriminatedUnion("type", [ | ||
z.object({ | ||
type: z.literal("error"), | ||
id: z.string(), | ||
data: z.any(), | ||
}), | ||
z.object({ | ||
type: z.literal("formatted"), | ||
id: z.string(), | ||
data: FormattedCodeSchema, | ||
}), | ||
z.object({ | ||
type: z.literal("result"), | ||
id: z.string(), | ||
data: z.any(), | ||
}), | ||
z.object({ | ||
type: z.literal("log"), | ||
id: z.string(), | ||
data: LogSchema, | ||
}), | ||
]); | ||
|
||
export type Response = z.infer<typeof ResponseSchema>; | ||
export type Message = z.infer<typeof MessageSchema>; | ||
export type FormattedCode = z.infer<typeof FormattedCodeSchema>; | ||
export type Log = z.infer<typeof LogSchema>; |
Oops, something went wrong.