Skip to content

Commit 0bb6a32

Browse files
JesuTerrazJesus Terrazas
andauthored
Add Create User-Agent Header Utility (#98)
* Add User-Agent header to util class * Use pkginfo to get pkg version * update format * update test cases to work with github * cache version * update nodejs version format * copilot comments --------- Co-authored-by: Jesus Terrazas <[email protected]>
1 parent 7b596ae commit 0bb6a32

File tree

4 files changed

+44
-0
lines changed

4 files changed

+44
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# This file is generated by a prebuild script.
2+
src/version.ts

packages/agents-a365-runtime/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"module": "dist/esm/index.js",
77
"types": "dist/esm/index.d.ts",
88
"scripts": {
9+
"prebuild": "node -p \"'export const LIB_VERSION = ' + JSON.stringify(require('./package.json').version) + ';'\" > src/version.ts",
910
"build:cjs": "npx tsc --project tsconfig.cjs.json",
1011
"build:esm": "npx tsc --project tsconfig.esm.json",
1112
"build": "npm run build:cjs && npm run build:esm",

packages/agents-a365-runtime/src/utility.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33

44
import { TurnContext } from '@microsoft/agents-hosting';
55
import * as jwt from 'jsonwebtoken';
6+
import os from 'os';
7+
8+
import { LIB_VERSION } from './version';
69

710
/**
811
* Utility class providing helper methods for agent runtime operations.
@@ -47,4 +50,15 @@ export class Utility {
4750

4851
return agenticAppId;
4952
}
53+
54+
/**
55+
* Generates a User-Agent header string containing SDK version, OS type, Node.js version, and orchestrator.
56+
* @param orchestrator Optional orchestrator identifier to include in the User-Agent string.
57+
* @returns Formatted User-Agent header string.
58+
*/
59+
public static GetUserAgentHeader(orchestrator: string = ''): string {
60+
const osType = os.type();
61+
const orchestratorPart = orchestrator ? `; ${orchestrator}` : '';
62+
return `Agent365SDK/${LIB_VERSION} (${osType}; Node.js ${process.version}${orchestratorPart})`;
63+
}
5064
}

tests/runtime/utility.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,31 @@ describe('Utility', () => {
102102
expect(Utility.ResolveAgentIdentity(mockContext, '')).toEqual('00000000-0000-0000-0000-000000000000');
103103
});
104104
});
105+
106+
describe('GetUserAgentHeader', () => {
107+
it('returns string containing version, OS, and orchestrator', () => {
108+
// Arrange
109+
const orchestrator = 'orch';
110+
111+
// Act
112+
const header = Utility.GetUserAgentHeader(orchestrator);
113+
114+
// Assert
115+
expect(header).toMatch(
116+
/^Agent365SDK\/.+ \(.+; Node\.js v\d+(\.\d+)*; orch\)$/
117+
);
118+
});
119+
120+
it('works without orchestrator passed', () => {
121+
// Arrange
122+
123+
// Act
124+
const header = Utility.GetUserAgentHeader();
125+
126+
// Assert
127+
expect(header).toMatch(
128+
/^Agent365SDK\/.+ \(.+; Node\.js v\d+(\.\d+)*\)$/
129+
);
130+
});
131+
});
105132
});

0 commit comments

Comments
 (0)