-
Notifications
You must be signed in to change notification settings - Fork 10.6k
Expand file tree
/
Copy pathaimlapi.ts
More file actions
83 lines (76 loc) · 3.44 KB
/
Copy pathaimlapi.ts
File metadata and controls
83 lines (76 loc) · 3.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// aimlapi.com BYOK provider — shared identity + outbound header helper.
//
// aimlapi.com (https://aimlapi.com) is an OpenAI-wire-compatible aggregator:
// a single API key fronts ~900 models from many creators, routed by model name
// on the upstream side. Because the wire shape is identical to OpenAI's, the
// chat proxy, connection test and model discovery all reuse the OpenAI call
// shape — the ONLY thing that differs is the outbound headers, which is why
// every outbound call point funnels through `aimlapiHeaders()` rather than
// hand-building `Authorization` inline.
//
// The distinctive aimlapi.com detail is the attribution pair: aimlapi.com
// expects `X-AIMLAPI-Source` and `X-AIMLAPI-Partner-ID` on EVERY request it
// serves — inference, catalog and key checks alike, not just sign-up. Injecting
// them in one helper keeps the invariant "every aimlapi.com request carries our
// attribution" enforceable in one place instead of being re-derived at each
// call site; the aggregator's rebate accounting keys off exactly this, and a
// missing header means the request serves fine but is silently untagged.
/**
* Provisioned partner for this integration. Valid on both aimlapi.com's staging
* and production backends, so it ships compiled in and one build works against
* either — only the base URL differs. Overridable via `AIMLAPI_PARTNER_ID` for
* a staging-only test id.
*/
export const AIMLAPI_PARTNER_ID = 'part_9TWZWFsyMyNrBDEENq5JaU0r';
/**
* `<channel>/<client>` — the channel is a small closed set (agent|mcp|web) and
* the client is this integration's registry slug.
*/
export const AIMLAPI_SOURCE = 'agent/open-design';
/**
* Default base URL the daemon assumes when the BYOK form leaves the field
* blank. Kept here as the single source of truth so the chat proxy, model
* discovery and connection test all default to the same origin.
*
* Note the `/v1`: aimlapi.com's OpenAI-compatible surface lives there, while
* `/v2` on the same host is billing/usage only and 404s for chat completions.
*/
export const AIMLAPI_DEFAULT_BASE_URL = 'https://api.aimlapi.com/v1';
function partnerId(): string {
return (process.env.AIMLAPI_PARTNER_ID || '').trim() || AIMLAPI_PARTNER_ID;
}
/**
* The attribution pair on its own (no auth). For any route that carries its own
* auth header — spread this alongside it so every aimlapi.com request, whatever
* the wire protocol, still carries attribution.
*/
export function aimlapiAttributionHeaders(): Record<string, string> {
return {
'X-AIMLAPI-Source': AIMLAPI_SOURCE,
'X-AIMLAPI-Partner-ID': partnerId(),
};
}
/**
* Build the outbound header set for an aimlapi.com request: Bearer auth plus
* the attribution pair. Callers spread the result into their `fetch` headers
* and may add `content-type` etc. on top.
*/
export function aimlapiHeaders(apiKey: string): Record<string, string> {
return {
authorization: `Bearer ${apiKey}`,
...aimlapiAttributionHeaders(),
};
}
/**
* Origin of a configured base URL, for callers that need to reach a sibling
* path on the same host. Falls back to the default origin when the configured
* value is unusable, so a malformed setting degrades instead of throwing.
*/
export function aimlapiOriginFromBase(baseUrl: string | undefined | null): string {
const candidate = (baseUrl || '').trim() || AIMLAPI_DEFAULT_BASE_URL;
try {
return new URL(candidate).origin;
} catch {
return new URL(AIMLAPI_DEFAULT_BASE_URL).origin;
}
}