Files
llmx/codex-cli/src/utils/session.ts
Michael Bolin 05bb5d7d46 fix: always load version from package.json at runtime (#909)
Note the high-level motivation behind this change is to avoid the need
to make temporary changes in the source tree in order to cut a release
build since that runs the risk of leaving things in an inconsistent
state in the event of a failure. The existing code:

```
import pkg from "../../package.json" assert { type: "json" };
```

did not work as intended because, as written, ESBuild would bake the
contents of the local `package.json` into the release build at build
time whereas we want it to read the contents at runtime so we can use
the `package.json` in the tree to build the code and later inject a
modified version into the release package with a timestamped build
version.

Changes:

* move `CLI_VERSION` out of `src/utils/session.ts` and into
`src/version.ts` so `../package.json` is a correct relative path both
from `src/version.ts` in the source tree and also in the final
`dist/cli.js` build output
* change `assert` to `with` in `import pkg` as apparently `with` became
standard in Node 22
* mark `"../package.json"` as external in `build.mjs` so the version is
not baked into the `.js` at build time

After using `pnpm stage-release` to build a release version, if I use
Node 22.0 to run Codex, I see the following printed to stderr at
startup:

```
(node:71308) ExperimentalWarning: Importing JSON modules is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
```

Note it is a warning and does not prevent Codex from running.

In Node 22.12, the warning goes away, but the warning still appears in
Node 22.11. For Node 22, 22.15.0 is the current LTS version, so LTS
users will not see this.

Also, something about moving the definition of `CLI_VERSION` caused a
problem with the mocks in `check-updates.test.ts`. I asked Codex to fix
it, and it came up with the change to the test configs. I don't know
enough about vitest to understand what it did, but the tests seem
healthy again, so I'm going with it.
2025-05-12 21:27:15 -07:00

53 lines
1.4 KiB
TypeScript

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;
}