mirror of
https://github.com/openai/codex.git
synced 2026-04-24 14:45:27 +00:00
lint, formatting
This commit is contained in:
@@ -9,13 +9,13 @@ import { TerminalChatCommandReview } from "./terminal-chat-command-review.js";
|
||||
import { log, isLoggingEnabled } from "../../utils/agent/log.js";
|
||||
import { loadConfig } from "../../utils/config.js";
|
||||
import { createInputItem } from "../../utils/input-utils.js";
|
||||
import { printAndResetSessionSummary } from "../../utils/session-cost.js";
|
||||
import { setSessionId } from "../../utils/session.js";
|
||||
import {
|
||||
loadCommandHistory,
|
||||
addToHistory,
|
||||
} from "../../utils/storage/command-history.js";
|
||||
import { clearTerminal, onExit } from "../../utils/terminal.js";
|
||||
import { printAndResetSessionSummary } from "../../utils/session-cost.js";
|
||||
import Spinner from "../vendor/ink-spinner.js";
|
||||
import TextInput from "../vendor/ink-text-input.js";
|
||||
import { Box, Text, useApp, useInput, useStdin } from "ink";
|
||||
|
||||
@@ -11,13 +11,13 @@ import { TerminalChatCommandReview } from "./terminal-chat-command-review.js";
|
||||
import { log, isLoggingEnabled } from "../../utils/agent/log.js";
|
||||
import { loadConfig } from "../../utils/config.js";
|
||||
import { createInputItem } from "../../utils/input-utils.js";
|
||||
import { printAndResetSessionSummary } from "../../utils/session-cost.js";
|
||||
import { setSessionId } from "../../utils/session.js";
|
||||
import {
|
||||
loadCommandHistory,
|
||||
addToHistory,
|
||||
} from "../../utils/storage/command-history.js";
|
||||
import { clearTerminal, onExit } from "../../utils/terminal.js";
|
||||
import { printAndResetSessionSummary } from "../../utils/session-cost.js";
|
||||
import Spinner from "../vendor/ink-spinner.js";
|
||||
import { Box, Text, useApp, useInput, useStdin } from "ink";
|
||||
import { fileURLToPath } from "node:url";
|
||||
|
||||
@@ -57,6 +57,13 @@ type AgentLoopParams = {
|
||||
onLastResponseId: (lastResponseId: string) => void;
|
||||
};
|
||||
|
||||
type Usage = {
|
||||
total_tokens?: number;
|
||||
input_tokens?: number;
|
||||
output_tokens?: number;
|
||||
};
|
||||
type MaybeUsageEvent = { response?: { usage?: Usage } };
|
||||
|
||||
export class AgentLoop {
|
||||
private model: string;
|
||||
private instructions?: string;
|
||||
@@ -796,12 +803,17 @@ export class AgentLoop {
|
||||
// with {input_tokens, output_tokens, total_tokens}. We record
|
||||
// the total (or fallback to summing the parts if needed).
|
||||
try {
|
||||
const usage: unknown = (event as any).response?.usage;
|
||||
const usage = (event as MaybeUsageEvent).response?.usage;
|
||||
if (usage && typeof usage === "object") {
|
||||
const u = usage as { total_tokens?: number; input_tokens?: number; output_tokens?: number };
|
||||
const u = usage as {
|
||||
total_tokens?: number;
|
||||
input_tokens?: number;
|
||||
output_tokens?: number;
|
||||
};
|
||||
const tokens =
|
||||
u.total_tokens ??
|
||||
(typeof u.input_tokens === "number" && typeof u.output_tokens === "number"
|
||||
(typeof u.input_tokens === "number" &&
|
||||
typeof u.output_tokens === "number"
|
||||
? u.input_tokens + u.output_tokens
|
||||
: undefined);
|
||||
if (typeof tokens === "number" && tokens > 0) {
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
/* eslint-disable no-irregular-whitespace */
|
||||
|
||||
import type { ResponseItem } from "openai/resources/responses/responses.mjs";
|
||||
|
||||
import { approximateTokensUsed } from "./approximate-tokens-used.js";
|
||||
|
||||
@@ -55,7 +55,9 @@ export class SessionCostTracker {
|
||||
if (cost == null) {
|
||||
return `Session complete – approx. ${tokens} tokens used.`;
|
||||
}
|
||||
return `Session complete – approx. ${tokens} tokens, $${cost.toFixed(4)} USD.`;
|
||||
return `Session complete – approx. ${tokens} tokens, $${cost.toFixed(
|
||||
4,
|
||||
)} USD.`;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -8,7 +8,11 @@ import {
|
||||
printAndResetSessionSummary,
|
||||
} from "../src/utils/session-cost.js";
|
||||
|
||||
function makeMessage(id: string, role: "user" | "assistant", text: string): ResponseItem {
|
||||
function makeMessage(
|
||||
id: string,
|
||||
role: "user" | "assistant",
|
||||
text: string,
|
||||
): ResponseItem {
|
||||
return {
|
||||
id,
|
||||
type: "message",
|
||||
@@ -71,7 +75,9 @@ describe("printAndResetSessionSummary", () => {
|
||||
|
||||
// Now inject an exact low token count and ensure it overrides
|
||||
tracker.addTokens(10);
|
||||
expect(tracker.getTokensUsed()).toBe(heuristicTokens + (10 - heuristicTokens));
|
||||
expect(tracker.getTokensUsed()).toBe(
|
||||
heuristicTokens + (10 - heuristicTokens),
|
||||
);
|
||||
|
||||
const cost = tracker.getCostUSD();
|
||||
expect(cost).not.toBeNull();
|
||||
|
||||
Reference in New Issue
Block a user