Skip to content

armgit / codex

Public Imported from GitHub
0

Lightweight coding agent that runs in your terminal

Rust

fix(cli): ensure /clear resets context and exclude system messages from approximateTokenUsed count (#443)

Abdelrhman Kamal Mahmoud Ali Slim425430debb56
diff --git a/codex-cli/src/components/chat/terminal-chat-input.tsx b/codex-cli/src/components/chat/terminal-chat-input.tsx
index e783f845f5..af1ebe1fd6 100644
--- a/codex-cli/src/components/chat/terminal-chat-input.tsx
+++ b/codex-cli/src/components/chat/terminal-chat-input.tsx
@@ -345,15 +345,27 @@ export default function TerminalChatInput({
// 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.
- setItems((prev) => [
- ...prev,
- {
- id: `clear-${Date.now()}`,
- type: "message",
- role: "system",
- content: [{ type: "input_text", text: "Context cleared" }],
- },
- ]);
+ setItems((prev) => {
+ const filteredOldItems = prev.filter((item) => {
+ if (
+ item.type === "message" &&
+ (item.role === "user" || item.role === "assistant")
+ ) {
+ return false;
+ }
+ return true;
+ });
+
+ return [
+ ...filteredOldItems,
+ {
+ id: `clear-${Date.now()}`,
+ type: "message",
+ role: "system",
+ content: [{ type: "input_text", text: "Terminal cleared" }],
+ },
+ ];
+ });
return;
} else if (inputValue === "/clearhistory") {
diff --git a/codex-cli/src/utils/approximate-tokens-used.ts b/codex-cli/src/utils/approximate-tokens-used.ts
index a50dd7305c..569ccebd1d 100644
--- a/codex-cli/src/utils/approximate-tokens-used.ts
+++ b/codex-cli/src/utils/approximate-tokens-used.ts
@@ -19,6 +19,10 @@ export function approximateTokensUsed(items: Array<ResponseItem>): number {
for (const item of items) {
switch (item.type) {
case "message": {
+ if (item.role !== "user" && item.role !== "assistant") {
+ continue;
+ }
+
for (const c of item.content) {
if (c.type === "input_text" || c.type === "output_text") {
charCount += c.text.length;