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
4 changes: 2 additions & 2 deletions kernel/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion kernel/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "acp-kernel",
"version": "0.0.147",
"version": "0.0.148",
"description": "Framework-agnostic context-compression engine (model-driven, 3-tier LSM). Pure core: no host dependency.",
"license": "MIT",
"author": "ranxianglei",
Expand Down
30 changes: 30 additions & 0 deletions kernel/src/protected.ts
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@ interface MediaSidecar {
rawOpenaiContent?: unknown;
rawOpenaiContentParts?: unknown[];
rawAnthropicBlock?: unknown;
rawAnthropicMessage?: unknown;
rawResponsesItem?: unknown;
rawGoogleParts?: unknown[];
}
Expand Down Expand Up @@ -437,6 +438,13 @@ export function hasMediaPayload(msg: CoreMessage): boolean {
export function hasUnrecoverableMediaPayload(msg: CoreMessage): boolean {
const m = msg as CoreMessage & MediaSidecar;

// A verbatim anthropic system message carrying control-plane state (Pi's
// tool_addition deltas, message-level output_config): folding it would
// silently un-declare the tools it adds — never fold. Plain-text ones
// (Claude Code's environment note) fold like any text message.
if (m.rawAnthropicMessage != null)
return !isPlainTextSystemMessage(m.rawAnthropicMessage);

const roc = m.rawOpenaiContent;
if (roc != null) {
const part = asObj(roc);
Expand Down Expand Up @@ -530,6 +538,28 @@ function anthropicSourceArchivable(
return false;
}

/** A mid-conversation anthropic `role:"system"` message whose whole payload
* is text (blocks may carry cache_control) — nothing a fold would lose. */
export function isPlainTextSystemMessage(raw: unknown): boolean {
const m = asObj(raw);
if (!m || Object.keys(m).some((k) => k !== "role" && k !== "content"))
return false;
if (typeof m.content === "string") return true;
return (
Array.isArray(m.content) &&
m.content.length > 0 &&
m.content.every((b) => {
const o = asObj(b);
return (
o?.type === "text" &&
Object.keys(o).every(
(k) => k === "type" || k === "text" || k === "cache_control",
)
);
})
);
}

function asObj(v: unknown): Record<string, unknown> | undefined {
return typeof v === "object" && v !== null
? (v as Record<string, unknown>)
Expand Down
94 changes: 91 additions & 3 deletions kernel/src/wire/anthropic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from "./identity.js";
import { hashId } from "./util.js";
import { ClusterCounter } from "./message-id.js";
import { isPlainTextSystemMessage } from "../protected.js";

export type AnthropicTextBlock = {
type: "text";
Expand Down Expand Up @@ -52,11 +53,19 @@ export type AnthropicMessage = {
content: string | AnthropicBlock[];
};

export type AnthropicSystemMessage = {
role: "system";
content: string | AnthropicBlock[];
[key: string]: unknown;
};

export type AnthropicWireMessage = AnthropicMessage | AnthropicSystemMessage;

export type AnthropicRequestBody = {
model?: string;
max_tokens?: number;
system?: string | AnthropicTextBlock[];
messages: AnthropicMessage[];
messages: AnthropicWireMessage[];
tools?: unknown[];
stream?: boolean;
temperature?: number;
Expand Down Expand Up @@ -102,6 +111,17 @@ export function anthropicToCore(body: AnthropicRequestBody): Flat {
// map fold coverage (core-message space) back to wire positions without
// re-deriving the converter's block-split behavior.
const before = msgs.length;
if (m.role === "system") {
const core = systemMessageCore(m, clusters);
core.wireIndex = wireIndex;
msgs.push(core);
// Harvest the client's breakpoint like any text block's: the host's
// client-managed-caching detection (#1637) and WC-010's 4-mark budget
// both read this map, not the verbatim sidecar.
const mark = systemMessageMark(m);
if (mark) cacheControls.set(core.id, mark);
continue;
}
const blocks =
typeof m.content === "string"
? [{ type: "text" as const, text: m.content }]
Expand Down Expand Up @@ -280,11 +300,74 @@ export function anthropicToCore(body: AnthropicRequestBody): Flat {
return { msgs, cacheControls };
}

// Mid-conversation system messages (Pi's tool_addition / tool_removal
// deltas, message-level output_config) are host control-plane state: one
// core per wire message, re-emitted verbatim, never split into the
// user/assistant block stream.
function systemMessageCore(
m: AnthropicSystemMessage,
clusters: ClusterCounter,
): BiliMessage {
const label = systemMessageText(m);
// A single-text-block message keeps the legacy per-block id (zero churn
// for Claude Code's trailing environment message).
const base = deriveCanonicalMessageId("system", "text", label);
return {
id: clusters.next(base),
role: "system",
contentType: "text",
text: label,
rawAnthropicMessage: m,
};
}

function systemMessageText(m: AnthropicSystemMessage): string {
const text =
typeof m.content === "string"
? m.content
: m.content
.map((b) => (b.type === "text" ? b.text : `[${b.type}]`))
.join("\n");
return text.length > 0 ? text : "[system]";
}

function systemMessageMark(m: AnthropicSystemMessage): unknown {
if (typeof m.content === "string") return undefined;
let mark: unknown;
for (const b of m.content as { cache_control?: unknown }[])
if (b.cache_control) mark = b.cache_control;
return mark;
}

// Verbatim unless the kernel rewrote the text (e.g. emergency truncation of
// a plain-text message) — re-emitting the original would undo it. A mark the
// original lacks (host-stamped, #1637) lands on the last block.
function rebuildSystemMessage(
m: BiliMessage,
mark: unknown,
): AnthropicSystemMessage {
const raw = m.rawAnthropicMessage as AnthropicSystemMessage;
const cc = mark ? { cache_control: mark } : {};
const text = m.text ?? "";
if (text !== systemMessageText(raw) && isPlainTextSystemMessage(raw)) {
return { role: "system", content: [{ type: "text", text, ...cc }] };
}
if (!mark || systemMessageMark(raw) || typeof raw.content === "string") {
return raw;
}
const last = raw.content.length - 1;
if (last < 0) return raw;
return {
...raw,
content: raw.content.map((b, i) => (i === last ? { ...b, ...cc } : b)),
};
}

export function coreToAnthropic(
messages: BiliMessage[],
cacheControls?: Map<string, unknown>,
): AnthropicMessage[] {
const out: AnthropicMessage[] = [];
): AnthropicWireMessage[] {
const out: AnthropicWireMessage[] = [];
let current: { role: "user" | "assistant"; blocks: AnthropicBlock[] } | null =
null;
const flush = () => {
Expand All @@ -298,6 +381,11 @@ export function coreToAnthropic(
return v ? { cache_control: v } : {};
};
for (const m of messages) {
if (m.rawAnthropicMessage) {
flush();
out.push(rebuildSystemMessage(m, cacheControls?.get(m.id)));
continue;
}
const target: "user" | "assistant" =
m.role === "assistant" ? "assistant" : "user";
if (!current || current.role !== target) {
Expand Down
6 changes: 6 additions & 0 deletions kernel/src/wire/bili-message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ export interface BiliMessage extends CoreMessage {
* coreToAnthropic (redacted_thinking must reach the wire byte-exact —
* Anthropic rejects modified thinking-family blocks). */
rawAnthropicBlock?: unknown;
/** Anthropic: a whole mid-conversation `role:"system"` wire message
* (Pi's `tool_addition` / `tool_removal` deltas, message-level
* `output_config`). Re-emitted verbatim by coreToAnthropic as its own
* system-role message; kernel-synthesized system cores (rendered
* summaries) never carry it and keep their user-role mapping. */
rawAnthropicMessage?: unknown;
/** OpenAI chat: the original content part for an image_url (data: or
* remote URL) or the original message object for a developer-role
* message. */
Expand Down
Loading
Loading