From 33ddfabd1fad85b5dbc21ae85afd77b4747fc9fd Mon Sep 17 00:00:00 2001 From: Michael Bolin Date: Mon, 12 May 2025 18:01:44 -0700 Subject: [PATCH] fix: always load version from package.json at runtime --- codex-cli/build.mjs | 3 + codex-cli/src/app.tsx | 2 +- .../components/chat/terminal-chat-input.tsx | 4 +- .../chat/terminal-chat-past-rollout.tsx | 2 +- .../src/components/chat/terminal-chat.tsx | 2 +- codex-cli/src/session.ts | 60 +++++++++++++++++++ codex-cli/src/utils/agent/agent-loop.ts | 14 ++--- codex-cli/src/utils/check-updates.ts | 2 +- codex-cli/tests/check-updates.test.ts | 2 +- 9 files changed, 77 insertions(+), 14 deletions(-) create mode 100644 codex-cli/src/session.ts diff --git a/codex-cli/build.mjs b/codex-cli/build.mjs index 465e8b9244..16664d76fc 100644 --- a/codex-cli/build.mjs +++ b/codex-cli/build.mjs @@ -72,6 +72,9 @@ if (isDevBuild) { esbuild .build({ entryPoints: ["src/cli.tsx"], + // Do not bundle the contents of package.json at build time: always read it + // at runtime. + external: ["../package.json"], bundle: true, format: "esm", platform: "node", diff --git a/codex-cli/src/app.tsx b/codex-cli/src/app.tsx index 5d859db576..8c634a1243 100644 --- a/codex-cli/src/app.tsx +++ b/codex-cli/src/app.tsx @@ -5,7 +5,7 @@ import type { ResponseItem } from "openai/resources/responses/responses"; import TerminalChat from "./components/chat/terminal-chat"; import TerminalChatPastRollout from "./components/chat/terminal-chat-past-rollout"; import { checkInGit } from "./utils/check-in-git"; -import { CLI_VERSION, type TerminalChatSession } from "./utils/session.js"; +import { CLI_VERSION, type TerminalChatSession } from "./session.js"; import { onExit } from "./utils/terminal"; import { ConfirmInput } from "@inkjs/ui"; import { Box, Text, useApp, useStdin } from "ink"; diff --git a/codex-cli/src/components/chat/terminal-chat-input.tsx b/codex-cli/src/components/chat/terminal-chat-input.tsx index 819b8ea3eb..dbbb38a60f 100644 --- a/codex-cli/src/components/chat/terminal-chat-input.tsx +++ b/codex-cli/src/components/chat/terminal-chat-input.tsx @@ -10,12 +10,12 @@ import type { import MultilineTextEditor from "./multiline-editor"; import { TerminalChatCommandReview } from "./terminal-chat-command-review.js"; import TextCompletions from "./terminal-chat-completions.js"; +import { setSessionId } from "../../session.js"; import { loadConfig } from "../../utils/config.js"; import { getFileSystemSuggestions } from "../../utils/file-system-suggestions.js"; import { expandFileTags } from "../../utils/file-tag-utils"; import { createInputItem } from "../../utils/input-utils.js"; import { log } from "../../utils/logger/log.js"; -import { setSessionId } from "../../utils/session.js"; import { SLASH_COMMANDS, type SlashCommand } from "../../utils/slash-commands"; import { loadCommandHistory, @@ -584,7 +584,7 @@ export default function TerminalChatInput({ try { const os = await import("node:os"); - const { CLI_VERSION } = await import("../../utils/session.js"); + const { CLI_VERSION } = await import("../../session.js"); const { buildBugReportUrl } = await import( "../../utils/bug-report.js" ); diff --git a/codex-cli/src/components/chat/terminal-chat-past-rollout.tsx b/codex-cli/src/components/chat/terminal-chat-past-rollout.tsx index f041f36f76..d822c0e49e 100644 --- a/codex-cli/src/components/chat/terminal-chat-past-rollout.tsx +++ b/codex-cli/src/components/chat/terminal-chat-past-rollout.tsx @@ -1,4 +1,4 @@ -import type { TerminalChatSession } from "../../utils/session.js"; +import type { TerminalChatSession } from "../../session.js"; import type { ResponseItem } from "openai/resources/responses/responses"; import TerminalChatResponseItem from "./terminal-chat-response-item"; diff --git a/codex-cli/src/components/chat/terminal-chat.tsx b/codex-cli/src/components/chat/terminal-chat.tsx index 998a190cf1..ce200be14d 100644 --- a/codex-cli/src/components/chat/terminal-chat.tsx +++ b/codex-cli/src/components/chat/terminal-chat.tsx @@ -10,6 +10,7 @@ import TerminalMessageHistory from "./terminal-message-history.js"; import { formatCommandForDisplay } from "../../format-command.js"; import { useConfirmation } from "../../hooks/use-confirmation.js"; import { useTerminalSize } from "../../hooks/use-terminal-size.js"; +import { CLI_VERSION } from "../../session.js"; import { AgentLoop } from "../../utils/agent/agent-loop.js"; import { ReviewDecision } from "../../utils/agent/review.js"; import { generateCompactSummary } from "../../utils/compact-summary.js"; @@ -24,7 +25,6 @@ import { uniqueById, } from "../../utils/model-utils.js"; import { createOpenAIClient } from "../../utils/openai-client.js"; -import { CLI_VERSION } from "../../utils/session.js"; import { shortCwd } from "../../utils/short-path.js"; import { saveRollout } from "../../utils/storage/save-rollout.js"; import ApprovalModeOverlay from "../approval-mode-overlay.js"; diff --git a/codex-cli/src/session.ts b/codex-cli/src/session.ts new file mode 100644 index 0000000000..6139c2d717 --- /dev/null +++ b/codex-cli/src/session.ts @@ -0,0 +1,60 @@ +// Note that "../package.json" is marked external in build.mjs. This ensures +// that the contents of package.json will always be read at runtime, which is +// preferable so we do not have to make a temporary change to package.json in +// the source tree to update the version number in the code. +import pkg from "../package.json" with { type: "json" }; + +// Read the version directly from package.json. +export const CLI_VERSION: string = (pkg as { version: string }).version; +export const ORIGIN = "codex_cli_ts"; + +export type TerminalChatSession = { + /** Globally unique session identifier */ + id: string; + /** The OpenAI username associated with this session */ + user: string; + /** Version identifier of the Codex CLI that produced the session */ + version: string; + /** The model used for the conversation */ + model: string; + /** ISO timestamp noting when the session was persisted */ + timestamp: string; + /** Optional custom instructions that were active for the run */ + instructions: string; +}; + +let sessionId = ""; + +/** + * Update the globally tracked session identifier. + * Passing an empty string clears the current session. + */ +export function setSessionId(id: string): void { + sessionId = id; +} + +/** + * Retrieve the currently active session identifier, or an empty string when + * no session is active. + */ +export function getSessionId(): string { + return sessionId; +} + +let currentModel = ""; + +/** + * Record the model that is currently being used for the conversation. + * Setting an empty string clears the record so the next agent run can update it. + */ +export function setCurrentModel(model: string): void { + currentModel = model; +} + +/** + * Return the model that was last supplied to {@link setCurrentModel}. + * If no model has been recorded yet, an empty string is returned. + */ +export function getCurrentModel(): string { + return currentModel; +} diff --git a/codex-cli/src/utils/agent/agent-loop.ts b/codex-cli/src/utils/agent/agent-loop.ts index 60749a2389..16e0a3428a 100644 --- a/codex-cli/src/utils/agent/agent-loop.ts +++ b/codex-cli/src/utils/agent/agent-loop.ts @@ -11,6 +11,13 @@ import type { } from "openai/resources/responses/responses.mjs"; import type { Reasoning } from "openai/resources.mjs"; +import { + ORIGIN, + CLI_VERSION, + getSessionId, + setCurrentModel, + setSessionId, +} from "../../session.js"; import { OPENAI_TIMEOUT_MS, OPENAI_ORGANIZATION, @@ -22,13 +29,6 @@ import { import { log } from "../logger/log.js"; import { parseToolCallArguments } from "../parsers.js"; import { responsesCreateViaChatCompletions } from "../responses.js"; -import { - ORIGIN, - CLI_VERSION, - getSessionId, - setCurrentModel, - setSessionId, -} from "../session.js"; import { handleExecCommand } from "./handle-exec-command.js"; import { HttpsProxyAgent } from "https-proxy-agent"; import { randomUUID } from "node:crypto"; diff --git a/codex-cli/src/utils/check-updates.ts b/codex-cli/src/utils/check-updates.ts index 5e326c1c93..c6ce703a0d 100644 --- a/codex-cli/src/utils/check-updates.ts +++ b/codex-cli/src/utils/check-updates.ts @@ -1,7 +1,7 @@ import type { AgentName } from "package-manager-detector"; import { detectInstallerByPath } from "./package-manager-detector"; -import { CLI_VERSION } from "./session"; +import { CLI_VERSION } from "../session"; import boxen from "boxen"; import chalk from "chalk"; import { getLatestVersion } from "fast-npm-meta"; diff --git a/codex-cli/tests/check-updates.test.ts b/codex-cli/tests/check-updates.test.ts index 75ec8aaf4e..e9f62c60c6 100644 --- a/codex-cli/tests/check-updates.test.ts +++ b/codex-cli/tests/check-updates.test.ts @@ -9,7 +9,7 @@ import { renderUpdateCommand, } from "../src/utils/check-updates"; import { detectInstallerByPath } from "../src/utils/package-manager-detector"; -import { CLI_VERSION } from "../src/utils/session"; +import { CLI_VERSION } from "../src/session"; // In-memory FS mock let memfs: Record = {};