Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions hives/openai-server/src/conversation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// sigil, one bloom a turn. A conversation reads as blooms: each user turn
// opens one, its words, images, audio and files its content, and what the
// assistant said, the tools it called and what came back from them are
// what grew in it. System and developer words are the system prompt.
// what grew in it. Leading system words are the system prompt; later, notes.
//
// A request's last bloom is what it asks for. Its words are the prompt's
// content and the blooms before it the graft. A conversation that ends
Expand Down Expand Up @@ -125,10 +125,20 @@ function servedPetals(id: string, name: string, args: unknown, output: unknown,
];
}

export function noted(text: string): Petal {
return { kind: PetalKind.text, text: `<system>\n${text}\n</system>` };
}

export function noteOf(petal: Petal): string | undefined {
const text = petal.kind === PetalKind.text ? petal.text ?? "" : "";
return text.startsWith("<system>\n") && text.endsWith("\n</system>") ? text.slice(9, -10) : undefined;
}

class Reader {
system: string[] = [];
blooms: Bloom[] = [];
tail: Tail = "none";
leading = true;

#open(): Bloom {
let last = this.blooms[this.blooms.length - 1];
Expand All @@ -139,17 +149,24 @@ class Reader {
return last;
}

told(text: string): void {
if (this.leading) this.system.push(text);
else if (text.length > 0) this.said([noted(text)]);
}

said(petals: Petal[]): void {
const last = this.blooms[this.blooms.length - 1];
if (last && (last.grew ?? []).length === 0) last.content = [...(last.content ?? []), ...petals];
else this.blooms.push({ content: petals, grew: [] });
this.tail = "user";
this.leading = false;
}

grew(petals: Petal[], tail: Tail): void {
const bloom = this.#open();
bloom.grew = [...(bloom.grew ?? []), ...petals];
this.tail = tail;
this.leading = false;
}

done(): Said {
Expand Down Expand Up @@ -184,7 +201,7 @@ export function fromChat(messages: ChatMessage[]): Said {
switch (m.role) {
case "system":
case "developer":
r.system.push(resultOf(m.content));
r.told(resultOf(m.content));
break;
case "user":
r.said(partsOf(m.content));
Expand Down Expand Up @@ -232,7 +249,7 @@ export function fromResponses(input: unknown, instructions?: string, before?: Sa
const type = item?.type ?? (item?.role ? "message" : undefined);
switch (type) {
case "message":
if (item.role === "system" || item.role === "developer") r.system.push(resultOf(item.content));
if (item.role === "system" || item.role === "developer") r.told(resultOf(item.content));
else if (item.role === "assistant") r.grew(partsOf(item.content), "assistant");
else r.said(partsOf(item.content));
break;
Expand Down
29 changes: 22 additions & 7 deletions hives/openai-server/src/nurse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import { randomUUID } from "node:crypto";
import { Bee, Chi, PetalKind, Role, type Bloom, type Petal, type PetalKindT, type Tone } from "../../../thrum-clients/ts/index.ts";
import { HIVE } from "./config.ts";
import { noteOf } from "./conversation.ts";
import { SOURCE, VERSION } from "./requester.ts";
import { events, post } from "./upstream.ts";

Expand Down Expand Up @@ -62,9 +63,24 @@ function parts(petals: Petal[]): string | Array<Record<string, unknown>> {
return out;
}

function said(petals: Petal[]): Message | undefined {
const content = parts(petals);
return (typeof content === "string" ? content.length > 0 : content.length > 0) ? { role: "user", content } : undefined;
function said(petals: Petal[]): Message[] {
const out: Message[] = [];
let held: Petal[] = [];
const flush = () => {
const content = parts(held);
if (content.length > 0) out.push({ role: "user", content });
held = [];
};
for (const p of petals) {
const note = noteOf(p);
if (note === undefined) held.push(p);
else {
flush();
out.push({ role: "system", content: note });
}
}
flush();
return out;
}

function grown(petals: Petal[], into: Message[]): void {
Expand Down Expand Up @@ -93,8 +109,7 @@ function grown(petals: Petal[], into: Message[]): void {
export function messagesOf(graft: Bloom[]): Message[] {
const out: Message[] = [];
for (const b of graft) {
const m = said(b.content ?? []);
if (m) out.push(m);
out.push(...said(b.content ?? []));
grown(b.grew ?? [], out);
}
return out;
Expand Down Expand Up @@ -232,8 +247,8 @@ export class Nurse {
s.waiting.clear();
const content = t.content && t.content.length > 0 ? t.content : t.text ? [{ kind: PetalKind.text, text: t.text }] : [];
const asked = said(content);
if (asked) {
s.messages.push(asked);
if (asked.length > 0) {
s.messages.push(...asked);
return this.#grow(sid, s);
}
const text = plainText(graft);
Expand Down
30 changes: 30 additions & 0 deletions hives/openai-server/test/conversation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,36 @@ describe("fromChat", () => {
});
});

describe("system words said after the first turn", () => {
const note = (text: string) => ({ kind: "text", text: `<system>\n${text}\n</system>` });

test("are a note in the turn they came in, and the system prompt keeps only what led", () => {
const said = fromChat([
{ role: "system", content: "be terse" },
{ role: "user", content: "hi" },
{ role: "assistant", content: "hello" },
{ role: "user", content: "again" },
{ role: "system", content: "[bookkeeping] deem it" },
]);
expect(said.system).toBe("be terse");
expect(said.blooms.length).toBe(2);
expect(said.blooms[1].content).toEqual([{ kind: "text", text: "again" }, note("[bookkeeping] deem it")]);
});

test("are said once each, where they were said, however many pile up", () => {
const said = fromResponses([
{ type: "message", role: "user", content: "one" },
{ type: "message", role: "system", content: "[bookkeeping] first" },
{ type: "message", role: "assistant", content: [{ type: "output_text", text: "ok" }] },
{ type: "message", role: "user", content: "two" },
{ type: "message", role: "system", content: "[bookkeeping] second" },
], "be terse");
expect(said.system).toBe("be terse");
expect(said.blooms[0].content).toEqual([{ kind: "text", text: "one" }, note("[bookkeeping] first")]);
expect(said.blooms[1].content).toEqual([{ kind: "text", text: "two" }, note("[bookkeeping] second")]);
});
});

describe("fromResponses", () => {
test("items, instructions and a predecessor's blooms read alike", () => {
const before = fromResponses("one");
Expand Down
16 changes: 16 additions & 0 deletions hives/openai-server/test/nurse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import { describe, expect, test } from "bun:test";
import { askOf, lateAnswers, messagesOf, plainText } from "../src/nurse.ts";
import { fromChat } from "../src/conversation.ts";

describe("the nurse half", () => {
test("a graft is the conversation, tool calls and answers in their places", () => {
Expand Down Expand Up @@ -86,3 +87,18 @@ describe("the nurse half", () => {
});
});
});

describe("system words said after the first turn", () => {
test("come back to the model as system messages where the client said them", () => {
const chat = [
{ role: "system", content: "be terse" },
{ role: "user", content: "hi" },
{ role: "assistant", content: "hello" },
{ role: "user", content: "again" },
{ role: "system", content: "[bookkeeping] deem it" },
];
const said = fromChat(chat);
expect(said.system).toBe("be terse");
expect(messagesOf(said.blooms)).toEqual(chat.slice(1));
});
});
Loading