feat(ai): add shared contentText utility (#6840)
Co-authored-by: Armin Ronacher <armin.ronacher@active-4.com>
This commit is contained in:
@@ -24,11 +24,11 @@ import type {
|
||||
PrepareNextTurnContext,
|
||||
ThinkingLevel,
|
||||
} from "@earendil-works/pi-agent-core";
|
||||
import { contentText } from "@earendil-works/pi-ai";
|
||||
import type {
|
||||
AssistantMessage,
|
||||
AuthResult,
|
||||
ImageContent,
|
||||
Message,
|
||||
Model,
|
||||
ProviderHeaders,
|
||||
TextContent,
|
||||
@@ -576,7 +576,7 @@ export class AgentSession {
|
||||
// This ensures the UI sees the updated queue state
|
||||
if (event.type === "message_start" && event.message.role === "user") {
|
||||
this._overflowRecoveryAttempted = false;
|
||||
const messageText = this._getUserMessageText(event.message);
|
||||
const messageText = contentText(event.message.content, "");
|
||||
if (messageText) {
|
||||
// Check steering queue first
|
||||
const steeringIndex = this._steeringMessages.indexOf(messageText);
|
||||
@@ -659,15 +659,6 @@ export class AgentSession {
|
||||
return false;
|
||||
}
|
||||
|
||||
/** Extract text content from a message */
|
||||
private _getUserMessageText(message: Message): string {
|
||||
if (message.role !== "user") return "";
|
||||
const content = message.content;
|
||||
if (typeof content === "string") return content;
|
||||
const textBlocks = content.filter((c) => c.type === "text");
|
||||
return textBlocks.map((c) => (c as TextContent).text).join("");
|
||||
}
|
||||
|
||||
/** Find the last assistant message in agent state (including aborted ones) */
|
||||
private _findLastAssistantMessage(): AssistantMessage | undefined {
|
||||
const messages = this.agent.state.messages;
|
||||
@@ -2954,17 +2945,11 @@ export class AgentSession {
|
||||
if (targetEntry.type === "message" && targetEntry.message.role === "user") {
|
||||
// User message: leaf = parent (null if root), text goes to editor
|
||||
newLeafId = targetEntry.parentId;
|
||||
editorText = this._extractUserMessageText(targetEntry.message.content);
|
||||
editorText = contentText(targetEntry.message.content, "");
|
||||
} else if (targetEntry.type === "custom_message") {
|
||||
// Custom message: leaf = parent (null if root), text goes to editor
|
||||
newLeafId = targetEntry.parentId;
|
||||
editorText =
|
||||
typeof targetEntry.content === "string"
|
||||
? targetEntry.content
|
||||
: targetEntry.content
|
||||
.filter((c): c is { type: "text"; text: string } => c.type === "text")
|
||||
.map((c) => c.text)
|
||||
.join("");
|
||||
editorText = contentText(targetEntry.content, "");
|
||||
} else {
|
||||
// Non-user message: leaf = selected node
|
||||
newLeafId = targetId;
|
||||
@@ -3032,7 +3017,7 @@ export class AgentSession {
|
||||
if (entry.type !== "message") continue;
|
||||
if (entry.message.role !== "user") continue;
|
||||
|
||||
const text = this._extractUserMessageText(entry.message.content);
|
||||
const text = contentText(entry.message.content, "");
|
||||
if (text) {
|
||||
result.push({ entryId: entry.id, text });
|
||||
}
|
||||
@@ -3041,17 +3026,6 @@ export class AgentSession {
|
||||
return result;
|
||||
}
|
||||
|
||||
private _extractUserMessageText(content: string | Array<{ type: string; text?: string }>): string {
|
||||
if (typeof content === "string") return content;
|
||||
if (Array.isArray(content)) {
|
||||
return content
|
||||
.filter((c): c is { type: "text"; text: string } => c.type === "text")
|
||||
.map((c) => c.text)
|
||||
.join("");
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
/**
|
||||
* Get session statistics. Aggregates over ALL session entries (including
|
||||
* history that was compacted away), so token/cost totals reflect what was
|
||||
|
||||
Reference in New Issue
Block a user