fix(cli): ensure /clear resets context and exclude system messages from approximateTokenUsed count (#443)
https://github.com/user-attachments/assets/58b8f261-a359-4cd8-8c84-90d8d91a5592
This commit is contained in:
committed by
GitHub
parent
6d6ca454cd
commit
425430debb
@@ -345,15 +345,27 @@ export default function TerminalChatInput({
|
|||||||
|
|
||||||
// Emit a system message to confirm the clear action. We *append*
|
// Emit a system message to confirm the clear action. We *append*
|
||||||
// it so Ink's <Static> treats it as new output and actually renders it.
|
// it so Ink's <Static> treats it as new output and actually renders it.
|
||||||
setItems((prev) => [
|
setItems((prev) => {
|
||||||
...prev,
|
const filteredOldItems = prev.filter((item) => {
|
||||||
{
|
if (
|
||||||
id: `clear-${Date.now()}`,
|
item.type === "message" &&
|
||||||
type: "message",
|
(item.role === "user" || item.role === "assistant")
|
||||||
role: "system",
|
) {
|
||||||
content: [{ type: "input_text", text: "Context cleared" }],
|
return false;
|
||||||
},
|
}
|
||||||
]);
|
return true;
|
||||||
|
});
|
||||||
|
|
||||||
|
return [
|
||||||
|
...filteredOldItems,
|
||||||
|
{
|
||||||
|
id: `clear-${Date.now()}`,
|
||||||
|
type: "message",
|
||||||
|
role: "system",
|
||||||
|
content: [{ type: "input_text", text: "Terminal cleared" }],
|
||||||
|
},
|
||||||
|
];
|
||||||
|
});
|
||||||
|
|
||||||
return;
|
return;
|
||||||
} else if (inputValue === "/clearhistory") {
|
} else if (inputValue === "/clearhistory") {
|
||||||
|
|||||||
@@ -19,6 +19,10 @@ export function approximateTokensUsed(items: Array<ResponseItem>): number {
|
|||||||
for (const item of items) {
|
for (const item of items) {
|
||||||
switch (item.type) {
|
switch (item.type) {
|
||||||
case "message": {
|
case "message": {
|
||||||
|
if (item.role !== "user" && item.role !== "assistant") {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
for (const c of item.content) {
|
for (const c of item.content) {
|
||||||
if (c.type === "input_text" || c.type === "output_text") {
|
if (c.type === "input_text" || c.type === "output_text") {
|
||||||
charCount += c.text.length;
|
charCount += c.text.length;
|
||||||
|
|||||||
Reference in New Issue
Block a user