Add persistence and nick changes.
This commit is contained in:
parent
e357fabc30
commit
ba61f6b0cf
5 changed files with 16 additions and 10 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -1,2 +1,2 @@
|
||||||
/target
|
/target
|
||||||
/history
|
/history.json
|
||||||
|
|
|
@ -22,8 +22,8 @@ $ ./src/kub.ts
|
||||||
|
|
||||||
## TODO
|
## TODO
|
||||||
|
|
||||||
|
- [ ] Auto update.
|
||||||
- [ ] Colored chat.
|
- [ ] Colored chat.
|
||||||
- [ ] Persistent chat.
|
|
||||||
- [ ] System messages.
|
- [ ] System messages.
|
||||||
- Change of days.
|
- Change of days.
|
||||||
- [ ] Tools.
|
- [ ] Tools.
|
||||||
|
|
|
@ -1 +0,0 @@
|
||||||
{"messages":[]}
|
|
|
@ -7,9 +7,10 @@ export class History {
|
||||||
private limit: number,
|
private limit: number,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
public push(value: Message) {
|
public async push(value: Message) {
|
||||||
this.messages.push(value)
|
this.messages.push(value)
|
||||||
while (this.messages.length > this.limit) this.messages.splice(0, 1)
|
while (this.messages.length > this.limit) this.messages.splice(0, 1)
|
||||||
|
await this.save()
|
||||||
}
|
}
|
||||||
|
|
||||||
public static async load(path: string, limit: number) {
|
public static async load(path: string, limit: number) {
|
||||||
|
@ -30,6 +31,6 @@ export class History {
|
||||||
}
|
}
|
||||||
|
|
||||||
private async save() {
|
private async save() {
|
||||||
await Deno.writeTextFile(this.path, JSON.stringify({ messages: this.messages }))
|
await Deno.writeTextFile(this.path, JSON.stringify({ messages: this.messages }, null, 4))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,8 +24,14 @@ export async function session(conn: Deno.TcpConn, name: string, history: History
|
||||||
for await (const line of lines) {
|
for await (const line of lines) {
|
||||||
if (line === "") continue
|
if (line === "") continue
|
||||||
if (line.length > 10_000) return
|
if (line.length > 10_000) return
|
||||||
console.log("prompted '", line, "' by ", conn.remoteAddr.hostname)
|
console.log("user", conn.remoteAddr.hostname, "promoted", line)
|
||||||
await write_encoded(conn, "\n< ")
|
if (line.startsWith("/nick ")) {
|
||||||
|
name = line.slice("/nick ".length)
|
||||||
|
console.log("Changed name of", conn.remoteAddr.hostname, "to", name)
|
||||||
|
await write_encoded(conn, "\n> ")
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
await write_encoded(conn, "\n[kub] ")
|
||||||
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"
|
||||||
|
@ -34,9 +40,9 @@ 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> ")
|
||||||
history.push(user_message)
|
await history.push(user_message)
|
||||||
history.push({ role, content })
|
await history.push({ role, content })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue