diff --git a/packages/agents-a365-runtime/.gitignore b/packages/agents-a365-runtime/.gitignore new file mode 100644 index 00000000..4fdcf427 --- /dev/null +++ b/packages/agents-a365-runtime/.gitignore @@ -0,0 +1,2 @@ +# This file is generated by a prebuild script. +src/version.ts \ No newline at end of file diff --git a/packages/agents-a365-runtime/package.json b/packages/agents-a365-runtime/package.json index 1fb4e0c7..903cf2bf 100644 --- a/packages/agents-a365-runtime/package.json +++ b/packages/agents-a365-runtime/package.json @@ -6,6 +6,7 @@ "module": "dist/esm/index.js", "types": "dist/esm/index.d.ts", "scripts": { + "prebuild": "node -p \"'export const LIB_VERSION = ' + JSON.stringify(require('./package.json').version) + ';'\" > src/version.ts", "build:cjs": "npx tsc --project tsconfig.cjs.json", "build:esm": "npx tsc --project tsconfig.esm.json", "build": "npm run build:cjs && npm run build:esm", diff --git a/packages/agents-a365-runtime/src/utility.ts b/packages/agents-a365-runtime/src/utility.ts index 0e7308cb..e0c3ef6f 100644 --- a/packages/agents-a365-runtime/src/utility.ts +++ b/packages/agents-a365-runtime/src/utility.ts @@ -3,6 +3,9 @@ import { TurnContext } from '@microsoft/agents-hosting'; import * as jwt from 'jsonwebtoken'; +import os from 'os'; + +import { LIB_VERSION } from './version'; /** * Utility class providing helper methods for agent runtime operations. @@ -47,4 +50,15 @@ export class Utility { return agenticAppId; } + + /** + * Generates a User-Agent header string containing SDK version, OS type, Node.js version, and orchestrator. + * @param orchestrator Optional orchestrator identifier to include in the User-Agent string. + * @returns Formatted User-Agent header string. + */ + public static GetUserAgentHeader(orchestrator: string = ''): string { + const osType = os.type(); + const orchestratorPart = orchestrator ? `; ${orchestrator}` : ''; + return `Agent365SDK/${LIB_VERSION} (${osType}; Node.js ${process.version}${orchestratorPart})`; + } } \ No newline at end of file diff --git a/tests/runtime/utility.test.ts b/tests/runtime/utility.test.ts index d5cf3bb5..db046b96 100644 --- a/tests/runtime/utility.test.ts +++ b/tests/runtime/utility.test.ts @@ -102,4 +102,31 @@ describe('Utility', () => { expect(Utility.ResolveAgentIdentity(mockContext, '')).toEqual('00000000-0000-0000-0000-000000000000'); }); }); + + describe('GetUserAgentHeader', () => { + it('returns string containing version, OS, and orchestrator', () => { + // Arrange + const orchestrator = 'orch'; + + // Act + const header = Utility.GetUserAgentHeader(orchestrator); + + // Assert + expect(header).toMatch( + /^Agent365SDK\/.+ \(.+; Node\.js v\d+(\.\d+)*; orch\)$/ + ); + }); + + it('works without orchestrator passed', () => { + // Arrange + + // Act + const header = Utility.GetUserAgentHeader(); + + // Assert + expect(header).toMatch( + /^Agent365SDK\/.+ \(.+; Node\.js v\d+(\.\d+)*\)$/ + ); + }); + }); });