summaryrefslogtreecommitdiffhomepage
path: root/js/example
diff options
context:
space:
mode:
authorDax Raad <[email protected]>2025-05-18 14:13:04 -0400
committerDax Raad <[email protected]>2025-05-26 12:40:17 -0400
commit0e303e6508edb4374213d1f98ec383b266339774 (patch)
treef7dc146eb58126f55f470ef135b66c678bf16898 /js/example
parentbcd2fd68b7fa00af055f558049994c2975d9515d (diff)
downloadopencode-0e303e6508edb4374213d1f98ec383b266339774.tar.gz
opencode-0e303e6508edb4374213d1f98ec383b266339774.zip
sync
Diffstat (limited to 'js/example')
-rw-r--r--js/example/client.tsx116
1 files changed, 116 insertions, 0 deletions
diff --git a/js/example/client.tsx b/js/example/client.tsx
new file mode 100644
index 000000000..65f0ebe15
--- /dev/null
+++ b/js/example/client.tsx
@@ -0,0 +1,116 @@
+import React, { useEffect, useState } from "react";
+import type { Server } from "../src/server/server";
+import type { Session } from "../src/session/session";
+import { hc } from "hono/client";
+import { createInterface, Interface } from "readline";
+
+const client = hc<Server.App>(`http://localhost:16713`);
+
+
+const session = await client.session_create.$post().then((res) => res.json());
+
+const initial: {
+ session: {
+ info: {
+ [sessionID: string]: Session.Info;
+ };
+ message: {
+ [sessionID: string]: {
+ [messageID: string]: Session.Message;
+ };
+ };
+ };
+} = {
+ session: {
+ info: {
+ [session.id]: session
+ },
+ message: {
+ [session.id]: {}
+ },
+ },
+};
+
+import { render, Text, Newline, useStdout, Box } from "ink";
+import TextInput from "ink-text-input"
+
+function App() {
+ const [state, setState] = useState(initial)
+ const [input, setInput] = useState("")
+
+ useEffect(() => {
+ fetch("http://localhost:16713/event")
+ .then(stream => {
+ const decoder = new TextDecoder();
+ stream.body!.pipeTo(
+ new WritableStream({
+ write(chunk) {
+ const data = decoder.decode(chunk);
+ if (data.startsWith("data: ")) {
+ try {
+ const event = JSON.parse(data.substring(6));
+ switch (event.type) {
+ case "storage.write":
+ const splits: string[] = event.properties.key.split("/");
+ let item = state as any;
+ for (let i = 0; i < splits.length; i++) {
+ const part = splits[i];
+ if (i === splits.length - 1) {
+ item[part] = event.properties.body;
+ continue;
+ }
+ if (!item[part]) item[part] = {};
+ item = item[part];
+ }
+ }
+ setState({ ...state })
+ } catch {
+ }
+ }
+ },
+ }),
+ )
+ });
+ }, [])
+
+
+ return (
+ <>
+ <Text>{session.title}</Text>
+ {
+ Object.values(state.session.message[session.id]).map(message => {
+ return Object.values(message.parts).map((part, index) => {
+ if (part.type === "text") {
+ return <Text key={`${message.id}-${index}`}>{message.role}: {part.text}</Text>
+ }
+ })
+ })
+ }
+ <Box gap={1} >
+ <Text>Input:</Text>
+ <TextInput
+ value={input}
+ onChange={setInput}
+ onSubmit={() => {
+ setInput("")
+ client.session_chat.$post({
+ json: {
+ sessionID: session.id,
+ parts: [
+ {
+ type: "text",
+ text: input,
+ },
+ ],
+ }
+ })
+ }}
+ />
+ </Box>
+ </>
+ );
+};
+
+console.clear();
+render(<App />);
+