Skip to content

Commit e126238

Browse files
committed
save progress
1 parent 67e5d01 commit e126238

File tree

3 files changed

+59
-5
lines changed

3 files changed

+59
-5
lines changed

packages/ai/src/config/resolver.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,26 @@
11
import type { AxiomEvalInstrumentationOptions, ResolvedAxiomConfig } from './index';
22

3+
const DEFAULT_EDGE_REGION = 'us-east-1';
4+
const NON_EDGE_HOSTS = new Set(['api.axiom.co', 'api.dev.axiomtestlabs.co']);
5+
const LOCALHOST_HOSTS = new Set(['localhost', '127.0.0.1', '::1', '0.0.0.0']);
6+
7+
export function resolveEdgeRegion(edgeUrl: string): string {
8+
let hostname = '';
9+
10+
try {
11+
hostname = new URL(edgeUrl).hostname.toLowerCase();
12+
} catch {
13+
return DEFAULT_EDGE_REGION;
14+
}
15+
16+
if (NON_EDGE_HOSTS.has(hostname) || LOCALHOST_HOSTS.has(hostname)) {
17+
return DEFAULT_EDGE_REGION;
18+
}
19+
20+
const [region] = hostname.split('.');
21+
return region ? region.toLowerCase() : DEFAULT_EDGE_REGION;
22+
}
23+
324
/**
425
* Builds a resources URL under the assumption that the API URL is in the format of https://api.axiom.co by replacing the subdomain with app.
526
* @param urlString - The API URL
@@ -27,14 +48,16 @@ const buildConsoleUrl = (urlString: string) => {
2748
export function resolveAxiomConnection(
2849
config: ResolvedAxiomConfig,
2950
consoleUrlOverride?: string,
30-
): AxiomEvalInstrumentationOptions & { consoleEndpointUrl: string } {
51+
): AxiomEvalInstrumentationOptions & { edgeRegion: string; consoleEndpointUrl: string } {
3152
const consoleEndpointUrl = consoleUrlOverride ?? buildConsoleUrl(config.eval.url);
3253
// Use edgeUrl for ingest/query operations, falling back to url if not specified
3354
const edgeUrl = config.eval.edgeUrl || config.eval.url;
55+
const edgeRegion = resolveEdgeRegion(edgeUrl);
3456

3557
return {
3658
url: config.eval.url,
3759
edgeUrl,
60+
edgeRegion,
3861
consoleEndpointUrl: consoleEndpointUrl,
3962
token: config.eval.token,
4063
dataset: config.eval.dataset,

packages/ai/src/config/validate-eval-token-permissions.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,18 +67,18 @@ export async function validateTokenPermissions(config: ResolvedAxiomConfig) {
6767

6868
try {
6969
const headers: Record<string, string> = {
70+
'X-Axiom-Org-Id': connection.orgId ?? '',
7071
'X-Axiom-Dataset': connection.dataset,
7172
};
7273

7374
if (connection.token) {
7475
headers.Authorization = `Bearer ${connection.token}`;
7576
}
7677

77-
const edge = new URL(connection.edgeUrl);
78-
const region = edge.hostname || 'us-east-1';
79-
console.log({ region });
78+
console.log({ region: connection.edgeRegion });
79+
8080
const response = await fetch(
81-
`${connection.url}/api/v3/evaluations/validate?dataset=${connection.dataset}&region=${region}`,
81+
`${connection.url}/api/v3/evaluations/validate?dataset=${connection.dataset}&region=${connection.edgeRegion}`,
8282
{
8383
headers,
8484
},
@@ -88,6 +88,7 @@ export async function validateTokenPermissions(config: ResolvedAxiomConfig) {
8888
let serverMessage: string | undefined;
8989
try {
9090
const data = await response.json();
91+
console.debug('validation response', { data })
9192
serverMessage = data?.error || data?.message;
9293
} catch {
9394
serverMessage = undefined;

packages/ai/test/config/load-config.test.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ describe('resolveAxiomConnection', () => {
8585

8686
expect(connection.edgeUrl).toBe('https://api.axiom.co');
8787
expect(connection.url).toBe('https://api.axiom.co');
88+
expect(connection.edgeRegion).toBe('us-east-1');
8889
});
8990

9091
it('uses edgeUrl when explicitly set', () => {
@@ -96,12 +97,41 @@ describe('resolveAxiomConnection', () => {
9697

9798
expect(connection.edgeUrl).toBe('https://eu-central-1.aws.edge.axiom.co');
9899
expect(connection.url).toBe('https://api.axiom.co');
100+
expect(connection.edgeRegion).toBe('eu-central-1');
99101
});
100102

101103
it('falls back to url when edgeUrl is empty string', () => {
102104
const config = createConfig({ url: 'https://api.axiom.co', edgeUrl: '' });
103105
const connection = resolveAxiomConnection(config);
104106

105107
expect(connection.edgeUrl).toBe('https://api.axiom.co');
108+
expect(connection.edgeRegion).toBe('us-east-1');
109+
});
110+
111+
it('defaults edgeRegion to us-east-1 for non-edge hosts', () => {
112+
const config = createConfig({
113+
url: 'https://api.axiom.co',
114+
edgeUrl: 'https://api.dev.axiomtestlabs.co',
115+
});
116+
const connection = resolveAxiomConnection(config);
117+
118+
expect(connection.edgeRegion).toBe('us-east-1');
119+
});
120+
121+
it('defaults edgeRegion to us-east-1 for localhost edgeUrl', () => {
122+
const config = createConfig({ url: 'https://api.axiom.co', edgeUrl: 'http://localhost:3000' });
123+
const connection = resolveAxiomConnection(config);
124+
125+
expect(connection.edgeRegion).toBe('us-east-1');
126+
});
127+
128+
it('resolves edgeRegion when edge host does not include edge segment', () => {
129+
const config = createConfig({
130+
url: 'https://api.axiom.co',
131+
edgeUrl: 'https://us-east-1.aws.axiom.co',
132+
});
133+
const connection = resolveAxiomConnection(config);
134+
135+
expect(connection.edgeRegion).toBe('us-east-1');
106136
});
107137
});

0 commit comments

Comments
 (0)