From 2437ce3f8b79a7f9d987862b633f3340bfa2c1c4 Mon Sep 17 00:00:00 2001 From: Dax Raad Date: Mon, 19 May 2025 19:29:38 -0400 Subject: toolz --- js/example/cli.ts | 27 +++++++++++ js/example/client.tsx | 116 ----------------------------------------------- js/example/ink.tsx | 122 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 149 insertions(+), 116 deletions(-) create mode 100644 js/example/cli.ts delete mode 100644 js/example/client.tsx create mode 100644 js/example/ink.tsx (limited to 'js/example') diff --git a/js/example/cli.ts b/js/example/cli.ts new file mode 100644 index 000000000..bb7522cbb --- /dev/null +++ b/js/example/cli.ts @@ -0,0 +1,27 @@ +import { hc } from "hono/client"; +import type { Server } from "../src/server/server"; + +const message = process.argv.slice(2).join(" "); +console.log(message); + +const client = hc(`http://localhost:16713`); +const session = await client.session_create.$post().then((res) => res.json()); +const result = await client.session_chat + .$post({ + json: { + sessionID: session.id, + parts: [ + { + type: "text", + text: message, + }, + ], + }, + }) + .then((res) => res.json()); + +for (const part of result.parts) { + if (part.type === "text") { + console.log(part.text); + } +} diff --git a/js/example/client.tsx b/js/example/client.tsx deleted file mode 100644 index 65f0ebe15..000000000 --- a/js/example/client.tsx +++ /dev/null @@ -1,116 +0,0 @@ -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(`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 ( - <> - {session.title} - { - Object.values(state.session.message[session.id]).map(message => { - return Object.values(message.parts).map((part, index) => { - if (part.type === "text") { - return {message.role}: {part.text} - } - }) - }) - } - - Input: - { - setInput("") - client.session_chat.$post({ - json: { - sessionID: session.id, - parts: [ - { - type: "text", - text: input, - }, - ], - } - }) - }} - /> - - - ); -}; - -console.clear(); -render(); - diff --git a/js/example/ink.tsx b/js/example/ink.tsx new file mode 100644 index 000000000..5eaab4d3b --- /dev/null +++ b/js/example/ink.tsx @@ -0,0 +1,122 @@ +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(`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 ( + <> + {session.title} + { + Object.values(state.session.message[session.id]) + .filter(message => message.role !== "system") + .map(message => { + return Object.values(message.parts) + .map((part, index) => { + if (part.type === "text") { + return {message.role}: {part.text} + } + if (part.type === "tool-invocation") { + return {message.role}: {part.toolInvocation.toolName} {JSON.stringify(part.toolInvocation.args)} + } + }) + }) + } + + Input: + { + setInput("") + client.session_chat.$post({ + json: { + sessionID: session.id, + parts: [ + { + type: "text", + text: input, + }, + ], + } + }) + }} + /> + + + ); +}; + +console.clear(); +render(); + -- cgit v1.2.3