Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions src/utils/RestAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,19 @@ interface IOptions {
export class RestAdapter implements IHttpAdapter {
private config: IRestAPIClientConfig;
private options: IOptions;

constructor(config?: IRestAPIClientConfig, options?: IOptions) {
this.config = config || {};
this.options = options || {};
private cliVersion: string;
private agentId: string;

constructor(
config: IRestAPIClientConfig = {},
options: IOptions = {},
cliVersion: string = 'unknown',
agentId: string = 'unknown'
) {
this.config = config;
this.options = options;
this.cliVersion = cliVersion;
this.agentId = agentId;
}

public setConfig(config: IRestAPIClientConfig): void {
Expand All @@ -36,6 +45,15 @@ export class RestAdapter implements IHttpAdapter {
let axiosResponse: AxiosResponse;

try {
// Construct the custom header
const cliInfoHeader = `cognigy-cli/${this.cliVersion} project/${this.agentId}`;

// Ensure headers object exists and add the custom header
httpRequest.headers = {
...httpRequest.headers,
'X-Cognigy-CLI-Info': cliInfoHeader,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gamer496 why didn't you use User-Agent header?

};

if (httpRequest.withAuthentication) {
const authenticationHeaders =
await client.authenticationHandler?.getAuthenticationHeaders();
Expand Down
19 changes: 18 additions & 1 deletion src/utils/cognigyClient.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
import Cognigy from '@cognigy/rest-api-client';
import * as fs from 'fs';
import * as path from 'path';
import CONFIG from './config';
import { RestAdapter } from './RestAdapter';

// Read package.json to get the CLI version
let cliVersion = 'unknown';
try {
const packageJsonPath = path.join(__dirname, '..', '..', 'package.json');
const packageJsonContent = fs.readFileSync(packageJsonPath, 'utf8');
cliVersion = JSON.parse(packageJsonContent).version;
} catch (error) {
console.error('Error reading package.json for CLI version:', error);
}

const CognigyClient = new Cognigy({
httpAdapter: new RestAdapter({}, { retries: 10 }),
httpAdapter: new RestAdapter(
{},
{ retries: 10 },
cliVersion,
CONFIG.agent // Assuming CONFIG.agent is the project/agent ID
),
baseUrl: CONFIG.baseUrl,
numberOfRetries: 10,
versions: {
Expand Down