mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-10 03:52:42 +00:00
refactor(gateway): share agent prompt builder
This commit is contained in:
43
src/gateway/agent-prompt.ts
Normal file
43
src/gateway/agent-prompt.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import { buildHistoryContextFromEntries, type HistoryEntry } from "../auto-reply/reply/history.js";
|
||||
|
||||
export type ConversationEntry = {
|
||||
role: "user" | "assistant" | "tool";
|
||||
entry: HistoryEntry;
|
||||
};
|
||||
|
||||
export function buildAgentMessageFromConversationEntries(entries: ConversationEntry[]): string {
|
||||
if (entries.length === 0) {
|
||||
return "";
|
||||
}
|
||||
|
||||
// Prefer the last user/tool entry as "current message" so the agent responds to
|
||||
// the latest user input or tool output, not the assistant's previous message.
|
||||
let currentIndex = -1;
|
||||
for (let i = entries.length - 1; i >= 0; i -= 1) {
|
||||
const role = entries[i]?.role;
|
||||
if (role === "user" || role === "tool") {
|
||||
currentIndex = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (currentIndex < 0) {
|
||||
currentIndex = entries.length - 1;
|
||||
}
|
||||
|
||||
const currentEntry = entries[currentIndex]?.entry;
|
||||
if (!currentEntry) {
|
||||
return "";
|
||||
}
|
||||
|
||||
const historyEntries = entries.slice(0, currentIndex).map((e) => e.entry);
|
||||
if (historyEntries.length === 0) {
|
||||
return currentEntry.body;
|
||||
}
|
||||
|
||||
const formatEntry = (entry: HistoryEntry) => `${entry.sender}: ${entry.body}`;
|
||||
return buildHistoryContextFromEntries({
|
||||
entries: [...historyEntries, currentEntry],
|
||||
currentMessage: formatEntry(currentEntry),
|
||||
formatEntry,
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user