Add colorization.

This commit is contained in:
JOLIMAITRE Matthieu 2025-05-28 19:18:12 +02:00
parent 0172b20207
commit 12de0a7c7c
2 changed files with 19 additions and 8 deletions

7
deno.lock generated
View file

@ -49,6 +49,11 @@
"https://deno.land/std@0.224.0/streams/to_transform_stream.ts": "4c4836455ef89bab9ece55975ee3a819f07d3d8b0e43101ec7f4ed033c8a2b61", "https://deno.land/std@0.224.0/streams/to_transform_stream.ts": "4c4836455ef89bab9ece55975ee3a819f07d3d8b0e43101ec7f4ed033c8a2b61",
"https://deno.land/std@0.224.0/streams/writable_stream_from_writer.ts": "527fc1b136fc53a9f0b32641f04a4522c72617fa7ca3778d27ed064f9cd98932", "https://deno.land/std@0.224.0/streams/writable_stream_from_writer.ts": "527fc1b136fc53a9f0b32641f04a4522c72617fa7ca3778d27ed064f9cd98932",
"https://deno.land/std@0.224.0/streams/writer_from_stream_writer.ts": "22cba4e5162fc443c7e5ef62f2054674cd6a20f5d7519a62db8d201496463931", "https://deno.land/std@0.224.0/streams/writer_from_stream_writer.ts": "22cba4e5162fc443c7e5ef62f2054674cd6a20f5d7519a62db8d201496463931",
"https://deno.land/std@0.224.0/streams/zip_readable_streams.ts": "53eb10d7557539b489bd858907aab6dd28247f074b3446573801de3150cb932e" "https://deno.land/std@0.224.0/streams/zip_readable_streams.ts": "53eb10d7557539b489bd858907aab6dd28247f074b3446573801de3150cb932e",
"https://deno.land/x/crayon@3.3.3/mod.ts": "82ad225583a483c4837577971629cddaa22614093af8353da6426b9366de9780",
"https://deno.land/x/crayon@3.3.3/src/conversions.ts": "9bfd3b1fbe412bcba092890ac558b6beaad4c3aa399cd99d45fadb324d28afd6",
"https://deno.land/x/crayon@3.3.3/src/crayon.ts": "6b237baa08a31c903436e040afd2228a7fffaa5d11dddc58e3c402f79b3c1d04",
"https://deno.land/x/crayon@3.3.3/src/styles.ts": "aa588b57b2c0482dc5c6f53109b4287831a9827c0aeef9a88129beae1172c1ee",
"https://deno.land/x/crayon@3.3.3/src/util.ts": "af8884a917488de76ac0c2b92482093ade74514ece77a4c64e5eb5b0f6ed68e6"
} }
} }

View file

@ -1,15 +1,16 @@
import { TextLineStream } from "https://deno.land/std@0.224.0/streams/mod.ts" import { TextLineStream } from "https://deno.land/std@0.224.0/streams/mod.ts"
import { writeAll } from "https://deno.land/std@0.224.0/io/write_all.ts" import { writeAll } from "https://deno.land/std@0.224.0/io/write_all.ts"
import ollama, { Message } from "npm:ollama@0.5.15" import ollama, { Message } from "npm:ollama@0.5.15"
import { crayon } from "https://deno.land/x/crayon@3.3.3/mod.ts"
import { History } from "./history.ts" import { History } from "./history.ts"
const header = (name: string) => ` const header = (name: string) => `
KUB ${crayon.yellow("KUB")}
Kub is a LLM running on TCP which includes all messages in the same context. Kub is a LLM running on TCP which includes all messages in the same context.
As Kub is having several discussions at once, he will know you as : ${name} As Kub is having several discussions at once, he will know you as ${crayon.blue.bold(name)}
` `
@ -17,7 +18,7 @@ export async function session(conn: Deno.TcpConn, name: string, history: History
console.log("Opening session for", name, "at", conn.remoteAddr.hostname, conn.remoteAddr.port) console.log("Opening session for", name, "at", conn.remoteAddr.hostname, conn.remoteAddr.port)
conn.setNoDelay(true) conn.setNoDelay(true)
await write_encoded(conn, header(name)) await write_encoded(conn, header(name))
await write_encoded(conn, "> ") await prompt(conn, name)
const lines = conn.readable const lines = conn.readable
.pipeThrough(new TextDecoderStream()) .pipeThrough(new TextDecoderStream())
.pipeThrough(new TextLineStream()) .pipeThrough(new TextLineStream())
@ -28,10 +29,10 @@ export async function session(conn: Deno.TcpConn, name: string, history: History
if (line.startsWith("/nick ")) { if (line.startsWith("/nick ")) {
name = line.slice("/nick ".length) name = line.slice("/nick ".length)
console.log("Changed name of", conn.remoteAddr.hostname, "to", name) console.log("Changed name of", conn.remoteAddr.hostname, "to", name)
await write_encoded(conn, "\n> ") await prompt(conn, name)
continue continue
} }
await write_encoded(conn, "\n[kub]\n") await write_encoded(conn, `\n ${crayon.yellow("kub")}\n\n`)
const user_message = make_message(name, line) const user_message = make_message(name, line)
const response_parts = await get_response_parts(history, user_message) const response_parts = await get_response_parts(history, user_message)
let content = "", role = "assistant" let content = "", role = "assistant"
@ -40,12 +41,17 @@ export async function session(conn: Deno.TcpConn, name: string, history: History
content += part.message.content content += part.message.content
role = part.message.role role = part.message.role
} }
await write_encoded(conn, "\n\n> ") await write_encoded(conn, "\n\n")
await prompt(conn, name)
await history.push(user_message) await history.push(user_message)
await history.push({ role, content }) await history.push({ role, content })
} }
} }
async function prompt(connection: Deno.TcpConn, name: string) {
await write_encoded(connection, ` ${crayon.blue.bold(name)}\n\n> `)
}
async function write_encoded(connection: Deno.TcpConn, text: string) { async function write_encoded(connection: Deno.TcpConn, text: string) {
const encoded = new TextEncoder().encode(text) const encoded = new TextEncoder().encode(text)
await writeAll(connection, encoded) await writeAll(connection, encoded)