Skip to content

Commit 768352b

Browse files
committed
Dynamic version number Header
1 parent f5d9ecc commit 768352b

6 files changed

Lines changed: 49 additions & 5 deletions

File tree

source/api/anthropic.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {logger} from '../utils/core/logger.js';
1515
import {addProxyToFetchOptions} from '../utils/core/proxyUtils.js';
1616
import {saveUsageToFile} from '../utils/core/usageLogger.js';
1717
import {isDevMode, getDevUserId} from '../utils/core/devMode.js';
18+
import {getVersionHeader} from '../utils/core/version.js';
1819

1920
export interface AnthropicOptions {
2021
model: string;
@@ -561,7 +562,7 @@ export async function* createStreamingAnthropicCompletion(
561562
'x-api-key': config.apiKey,
562563
Authorization: `Bearer ${config.apiKey}`,
563564
'anthropic-version': '2023-06-01',
564-
'x-snow': 'true',
565+
'x-snow': getVersionHeader(),
565566
...customHeaders,
566567
};
567568

source/api/chat.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import type {
1717
} from './types.js';
1818
import {addProxyToFetchOptions} from '../utils/core/proxyUtils.js';
1919
import {saveUsageToFile} from '../utils/core/usageLogger.js';
20+
import {getVersionHeader} from '../utils/core/version.js';
2021

2122
export type {
2223
ChatMessage,
@@ -429,7 +430,7 @@ export async function* createStreamingChatCompletion(
429430
headers: {
430431
'Content-Type': 'application/json',
431432
Authorization: `Bearer ${config.apiKey}`,
432-
'x-snow': 'true',
433+
'x-snow': getVersionHeader(),
433434
...customHeaders,
434435
},
435436
body: JSON.stringify(requestBody),

source/api/embedding.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {loadCodebaseConfig} from '../utils/config/codebaseConfig.js';
22
import {addProxyToFetchOptions} from '../utils/core/proxyUtils.js';
3+
import {getVersionHeader} from '../utils/core/version.js';
34

45
export interface EmbeddingOptions {
56
model?: string;
@@ -101,7 +102,7 @@ export async function createEmbeddings(
101102
// Build headers - only include Authorization if API key is provided
102103
const headers: Record<string, string> = {
103104
'Content-Type': 'application/json',
104-
'x-snow': 'true',
105+
'x-snow': getVersionHeader(),
105106
};
106107
if (apiKey) {
107108
headers['Authorization'] = `Bearer ${apiKey}`;

source/api/gemini.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
import type {ChatMessage, ChatCompletionTool, UsageInfo} from './types.js';
1212
import {addProxyToFetchOptions} from '../utils/core/proxyUtils.js';
1313
import {saveUsageToFile} from '../utils/core/usageLogger.js';
14+
import {getVersionHeader} from '../utils/core/version.js';
1415

1516
export interface GeminiOptions {
1617
model: string;
@@ -459,7 +460,7 @@ export async function* createStreamingGeminiCompletion(
459460
headers: {
460461
'Content-Type': 'application/json',
461462
Authorization: `Bearer ${config.apiKey}`,
462-
'x-snow': 'true',
463+
'x-snow': getVersionHeader(),
463464
...customHeaders,
464465
},
465466
body: JSON.stringify(requestBody),

source/api/responses.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import type {
1616
} from './types.js';
1717
import {addProxyToFetchOptions} from '../utils/core/proxyUtils.js';
1818
import {saveUsageToFile} from '../utils/core/usageLogger.js';
19+
import {getVersionHeader} from '../utils/core/version.js';
1920
export interface ResponseOptions {
2021
model: string;
2122
messages: ChatMessage[];
@@ -488,7 +489,7 @@ export async function* createStreamingResponse(
488489
headers: {
489490
'Content-Type': 'application/json',
490491
Authorization: `Bearer ${config.apiKey}`,
491-
'x-snow': 'true',
492+
'x-snow': getVersionHeader(),
492493
...(options.prompt_cache_key && {
493494
conversation_id: options.prompt_cache_key,
494495
session_id: options.prompt_cache_key,

source/utils/core/version.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import {readFileSync} from 'fs';
2+
import {join, dirname} from 'path';
3+
import {fileURLToPath} from 'url';
4+
5+
let cachedVersion: string = '';
6+
7+
/**
8+
* Get the current package version
9+
* Reads from package.json and caches the result
10+
* After bundling, all code is in bundle/cli.mjs, so we need to go up one level
11+
*/
12+
export function getPackageVersion(): string {
13+
if (cachedVersion) {
14+
return cachedVersion;
15+
}
16+
17+
try {
18+
// In bundled code, __filename points to bundle/cli.mjs
19+
// So we need to go up one level to reach package.json
20+
const currentDir = dirname(fileURLToPath(import.meta.url));
21+
const packageJsonPath = join(currentDir, '../package.json');
22+
const packageJson = JSON.parse(readFileSync(packageJsonPath, 'utf-8'));
23+
cachedVersion = packageJson.version || '1.0.0';
24+
return cachedVersion;
25+
} catch (error) {
26+
// Fallback version if reading fails
27+
console.error('Failed to read version from package.json:', error);
28+
cachedVersion = '1.0.0';
29+
return cachedVersion;
30+
}
31+
}
32+
33+
/**
34+
* Get version header value for API requests
35+
* Returns version in format: v1.0.0
36+
*/
37+
export function getVersionHeader(): string {
38+
return `v${getPackageVersion()}`;
39+
}

0 commit comments

Comments
 (0)