diff --git a/hives/openai-server/src/conversation.ts b/hives/openai-server/src/conversation.ts
index a460503a..95fb3d61 100644
--- a/hives/openai-server/src/conversation.ts
+++ b/hives/openai-server/src/conversation.ts
@@ -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
@@ -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: `\n${text}\n` };
+}
+
+export function noteOf(petal: Petal): string | undefined {
+ const text = petal.kind === PetalKind.text ? petal.text ?? "" : "";
+ return text.startsWith("\n") && text.endsWith("\n") ? 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];
@@ -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 {
@@ -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));
@@ -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;
diff --git a/hives/openai-server/src/nurse.ts b/hives/openai-server/src/nurse.ts
index a897de3b..45360069 100644
--- a/hives/openai-server/src/nurse.ts
+++ b/hives/openai-server/src/nurse.ts
@@ -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";
@@ -62,9 +63,24 @@ function parts(petals: Petal[]): string | Array> {
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 {
@@ -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;
@@ -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);
diff --git a/hives/openai-server/test/conversation.test.ts b/hives/openai-server/test/conversation.test.ts
index 6a172082..fce5c026 100644
--- a/hives/openai-server/test/conversation.test.ts
+++ b/hives/openai-server/test/conversation.test.ts
@@ -40,6 +40,36 @@ describe("fromChat", () => {
});
});
+describe("system words said after the first turn", () => {
+ const note = (text: string) => ({ kind: "text", text: `\n${text}\n` });
+
+ 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");
diff --git a/hives/openai-server/test/nurse.test.ts b/hives/openai-server/test/nurse.test.ts
index 274c8ebc..a413ffce 100644
--- a/hives/openai-server/test/nurse.test.ts
+++ b/hives/openai-server/test/nurse.test.ts
@@ -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", () => {
@@ -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));
+ });
+});