Skip to content

OpenAI's Codex Security CLI and TypeScript SDK for finding, validating, and fixing security vulnerabilities. npm: https://www.npmjs.com/package/@openai/codex-security

refactor(sdk): simplify scan usage and cost accounting (#1377)

mldangelo-oaice013bc49718
diff --git a/sdk/typescript/src/api.ts b/sdk/typescript/src/api.ts
index 7fa812e1c..bffb4e049 100644
--- a/sdk/typescript/src/api.ts
+++ b/sdk/typescript/src/api.ts
@@ -4154,7 +4154,7 @@ function addScanCosts(
): ScanCost {
if (previous === null) return { ...current };
const { estimatedUsdRange: currentRange, ...currentCost } = current;
- const previousRange = previous.estimatedUsdRange;
+ const previousRange = previous.estimatedUsdRange!;
return {
...currentCost,
inputTokens: previous.inputTokens + current.inputTokens,
@@ -4167,18 +4167,14 @@ function addScanCosts(
current.cacheWriteInputTokensReported === false
? { cacheWriteInputTokensReported: false }
: {}),
- ...(previousRange === undefined || currentRange === undefined
- ? {}
- : {
- estimatedUsdRange: {
- context: "unknown" as const,
- min: previousRange.min + currentRange.min,
- max:
- previousRange.max === null || currentRange.max === null
- ? null
- : previousRange.max + currentRange.max,
- },
- }),
+ estimatedUsdRange: {
+ context: "unknown" as const,
+ min: previousRange.min + currentRange!.min,
+ max:
+ previousRange.max === null || currentRange!.max === null
+ ? null
+ : previousRange.max + currentRange!.max,
+ },
};
}
diff --git a/sdk/typescript/src/cost.ts b/sdk/typescript/src/cost.ts
index 68a914bf8..6d0895b0e 100644
--- a/sdk/typescript/src/cost.ts
+++ b/sdk/typescript/src/cost.ts
@@ -47,7 +47,6 @@ interface SessionReasoning {
interface SessionUsage {
offset: number;
pendingLine: Buffer[];
- pendingLineBytes: number;
unreadable: boolean;
threadId: string | null;
parentThreadId: string | null;
@@ -94,7 +93,6 @@ function createSessionUsage(): SessionUsage {
return {
offset: 0,
pendingLine: [],
- pendingLineBytes: 0,
unreadable: false,
threadId: null,
parentThreadId: null,
@@ -436,7 +434,6 @@ async function readSessionUsage(
} catch (error) {
session.unreadable = true;
session.pendingLine = [];
- session.pendingLineBytes = 0;
throw error;
}
}
@@ -455,27 +452,24 @@ function readSessionChunk(
const newline = contents.indexOf(0x0a, lineStart);
const lineEnd = newline === -1 ? contents.length : newline;
const fragment = contents.subarray(lineStart, lineEnd);
- const lineBytes = session.pendingLineBytes + fragment.length;
if (newline === -1) {
if (fragment.length > 0) {
session.pendingLine.push(Buffer.from(fragment));
- session.pendingLineBytes = lineBytes;
}
return;
}
- if (session.pendingLineBytes === 0) {
+ if (session.pendingLine.length === 0) {
readSessionEvent(fragment.toString("utf8"), session, repository);
} else {
if (fragment.length > 0) session.pendingLine.push(Buffer.from(fragment));
readSessionEvent(
- Buffer.concat(session.pendingLine, lineBytes).toString("utf8"),
+ Buffer.concat(session.pendingLine).toString("utf8"),
session,
repository,
);
session.pendingLine = [];
- session.pendingLineBytes = 0;
}
lineStart = newline + 1;
}