Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
996b5e7
use Agent365ExporterOptions for exporter and create AgenticTokenCache…
jsl517 Nov 10, 2025
9c50c72
tests
jsl517 Nov 11, 2025
51b4d86
copilot comment
jsl517 Nov 11, 2025
a0de073
configure batch span processor
jsl517 Nov 11, 2025
88605eb
copilot comment
jsl517 Nov 11, 2025
ca17b96
checkpoint to use cached context and authorization to exchange token.
jsl517 Nov 12, 2025
1b89510
only do refresh token in message handler, unit tests
jsl517 Nov 13, 2025
fa95349
sample update
jsl517 Nov 14, 2025
1c3940e
comments
jsl517 Nov 14, 2025
36c5966
comments
jsl517 Nov 14, 2025
3d174b3
observability always use prod
jsl517 Nov 14, 2025
9ca9f6a
Merge branch 'main' into users/pefan/exportoption
jsl517 Nov 14, 2025
7271de5
should be lowercase
jsl517 Nov 14, 2025
ea0cfc6
Revert "observability always use prod"
jsl517 Nov 15, 2025
5221582
Merge branch 'main' into users/pefan/exportoption
jsl517 Nov 15, 2025
64728ca
default to prod
jsl517 Nov 15, 2025
f1d9e61
expose Agent365ExporterOptions
jsl517 Nov 18, 2025
e07318b
expose Agent365ExporterOptions
jsl517 Nov 18, 2025
057aae7
move azure token cache to its own package
jsl517 Nov 19, 2025
20133be
readme update
jsl517 Nov 19, 2025
d322096
Merge branch 'main' into users/pefan/exportoption
jsl517 Nov 19, 2025
6058c84
fix package.json error
jsl517 Nov 19, 2025
1149e09
fix logging
jsl517 Nov 19, 2025
5e3d93b
lint,jest config
jsl517 Nov 19, 2025
55b5536
cleanup
jsl517 Nov 19, 2025
cf31254
comment
jsl517 Nov 19, 2025
142ec28
rename
jsl517 Nov 19, 2025
54ddd3e
lint
jsl517 Nov 19, 2025
5303929
comment
jsl517 Nov 19, 2025
0b11ffa
Merge branch 'main' into users/pefan/exportoption
fpfp100 Nov 19, 2025
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
100 changes: 100 additions & 0 deletions packages/agents-a365-observability-tokencache/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# @microsoft/agents-a365-observability-tokencache

Observability token cache utilities for the Agent365 SDK. This package provides:

- In‑memory storage for observability (telemetry/export) bearer tokens
Comment thread
fpfp100 marked this conversation as resolved.
- Early refresh using an expiration skew (default 60s before real expiry)
- Automatic fallback TTL if the token lacks an `exp` claim
- Linear retry on transient failures (timeouts, 5xx, 408, 429) during token exchange
- Per key (agent + tenant) serialization to avoid thundering herds

## Installation

```bash
pnpm add @microsoft/agents-a365-observability-tokencache
```

## Core API

```ts
import { AgenticTokenCacheInstance } from '@microsoft/agents-a365-observability-tokencache';
```

## Using With Observability Builder (Telemetry Exporter)

When configuring the observability manager, supply a token resolver. Do **not** pass the method reference directly (it would lose `this`); wrap it to preserve context or use `bind`:

```ts
import { Builder, ObservabilityManager, Agent365ExporterOptions } from '@microsoft/agents-a365-observability';
import { AgenticTokenCacheInstance } from '@microsoft/agents-a365-observability-tokencache';

export const a365Observability = ObservabilityManager.configure((builder: Builder) => {
const exporterOptions = new Agent365ExporterOptions();
exporterOptions.maxQueueSize = 10;

builder
.withService('TypeScript Sample Agent', '1.0.0')
.withClusterCategory('prod')
.withExporterOptions(exporterOptions)
// Wrap to ensure `this` binding (so internal map & methods work).
.withTokenResolver((agentId, tenantId) => AgenticTokenCacheInstance.getObservabilityToken(agentId, tenantId));
});
```

Alternatively:

```ts
builder.withTokenResolver(AgenticTokenCacheInstance.getObservabilityToken.bind(AgenticTokenCacheInstance));
```

## Example: Preloading in an Agent Turn

```ts
import { AgenticTokenCacheInstance } from '@microsoft/agents-a365-observability-tokencache';
import { getObservabilityAuthenticationScope } from '@microsoft/agents-a365-runtime';

// Inside activity handler:
await AgenticTokenCacheInstance.RefreshObservabilityToken(
agentInfo.agentId,
tenantInfo.tenantId,
context,
agentApplication.authorization,
getObservabilityAuthenticationScope()
);
// Token is now cached (non-blocking if acquisition fails; subsequent resolver will return null until success).
```

## Custom Token Resolver Example (Using Application-Level Cache)

If you prefer to manage the token yourself and only use this cache for retrieval:

```ts
const tokenResolver = (agentId: string, tenantId: string): string | null => {
const t = AgenticTokenCacheInstance.getObservabilityToken(agentId, tenantId);
return t ?? null;
};

builder.withTokenResolver(tokenResolver);
```

## When to Refresh vs. When to Read

- Use `RefreshObservabilityToken` when you have access to `TurnContext` and `Authorization` and want to ensure a fresh token is available.
- Use `getObservabilityToken` inside exporters / resolvers where only agent & tenant IDs are available, and you can tolerate `null` (meaning skip authenticated export or wait until later).

## Handling Expiration

The cache considers a token expired if:
1. It has an `exp` and current time >= `exp * 1000 - skewMs` (default skew 60s)
2. Or it has no `exp` and current time >= `acquiredOn + maxTokenAgeMs` (default 1h)

Expired tokens are not returned; they force a refresh on next `RefreshObservabilityToken` call.

## Error & Retry Behavior

- Transient errors (timeouts, network issues, 408, 429, 5xx) trigger up to 2 linear backoff retries (200ms, then 400ms).
- Non-retriable errors clear the entry’s token & expiry; subsequent reads return `null` until a successful refresh.
- All events are logged via lightweight console wrappers (info/warn/error).

## License
MIT
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
testPathIgnorePatterns: ['/integration/'],
clearMocks: true,
};
63 changes: 63 additions & 0 deletions packages/agents-a365-observability-tokencache/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
{
"name": "@microsoft/agents-a365-observability-tokencache",
"version": "0.0.0-placeholder",
"description": "Microsoft Agent 365 SDK observability token cache utilities",
"keywords": [
"agent365",
"observability",
"telemetry",
"token",
"cache"
],
"homepage": "https://github.com/microsoft/Agent365-nodejs",
"bugs": {
"url": "https://github.com/microsoft/Agent365-nodejs/issues"
},
"repository": {
"type": "git",
"url": "https://github.com/microsoft/Agent365-nodejs.git",
"directory": "packages/agents-a365-observability-tokencache"
},
"license": "MIT",
"author": "Microsoft",
"main": "./dist/cjs/index.js",
"module": "./dist/esm/index.js",
"types": "./dist/cjs/index.d.ts",
"files": [
"dist",
"README.md",
"CHANGELOG.md"
],
"scripts": {
"build": "npm run build:cjs && npm run build:esm",
"build:cjs": "npx tsc --project tsconfig.cjs.json",
"build:esm": "npx tsc --project tsconfig.esm.json",
"build:watch": "npx tsc --watch",
"clean": "npx rimraf dist",
"lint": "eslint src/**/*.ts",
"lint:fix": "eslint src/**/*.ts --fix",
"test": "jest --config ./jest.config.cjs --passWithNoTests",
"test:watch": "jest --watch",
"test:coverage": "jest --coverage",
"pack": "npm pack --pack-destination=../"
},
"dependencies": {
"@microsoft/agents-hosting": "^1.1.0-alpha.85",
"@microsoft/agents-a365-runtime": "workspace:*",
"@microsoft/agents-a365-observability": "workspace:*"
},
"devDependencies": {
"@types/jest": "^29.5.12",
"@types/node": "^20.0.0",
"@typescript-eslint/eslint-plugin": "^6.0.0",
"@typescript-eslint/parser": "^6.0.0",
"eslint": "^8.0.0",
"jest": "^29.7.0",
"rimraf": "^6.0.0",
"ts-jest": "^29.2.0",
"typescript": "^5.0.0"
},
"engines": {
"node": ">=18.0.0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"root": true,
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 2020,
"sourceType": "module"
},
"plugins": ["@typescript-eslint"],
"extends": ["eslint:recommended"],
"rules": {
"no-unused-vars": "off",
"@typescript-eslint/no-unused-vars": ["error", {
"argsIgnorePattern": "^_",
"varsIgnorePattern": "^_",
"caughtErrorsIgnorePattern": "^_"
}],
"@typescript-eslint/no-explicit-any": "error",
"prefer-const": "error",
"no-var": "error",
"no-console": "error",
"semi": ["error", "always"],
"quotes": ["error", "single"],
"indent": ["error", 4],
"no-trailing-spaces": "error"
},
"env": {
"node": true,
"es6": true,
"jest": true
},
"ignorePatterns": ["dist/**/*", "node_modules/**/*", "*.js"]
}
Loading