@sixb/azure-ai-foundry 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/transport.ts CHANGED
@@ -20,9 +20,9 @@ export class FoundryTransport {
20
20
  readonly projectUrl: string
21
21
  private readonly options: TransportOptions
22
22
 
23
- constructor(options: TransportOptions) {
23
+ constructor(options: TransportOptions, target: "project" | "resource" = "project") {
24
24
  if (!URL.canParse(options.endpoint))
25
- throw new TypeError(`${PREFIX} endpoint must be a valid project URL.`)
25
+ throw new TypeError(`${PREFIX} endpoint must be a valid ${target} URL.`)
26
26
  const url = new URL(options.endpoint)
27
27
  if (
28
28
  !["https:", "http:"].includes(url.protocol) ||
@@ -32,16 +32,20 @@ export class FoundryTransport {
32
32
  url.hash
33
33
  ) {
34
34
  throw new TypeError(
35
- `${PREFIX} endpoint must be an HTTP(S) project URL without credentials, query, or fragment.`
35
+ `${PREFIX} endpoint must be an HTTP(S) ${target} URL without credentials, query, or fragment.`
36
36
  )
37
37
  }
38
38
  const path = url.pathname.replace(/\/+$/, "")
39
- if (!/^\/api\/projects\/[^/]+$/.test(path))
39
+ if (target === "project" && !/^\/api\/projects\/[^/]+$/.test(path))
40
40
  throw new TypeError(
41
41
  `${PREFIX} endpoint must be a Foundry project URL: https://<resource>.services.ai.azure.com/api/projects/<project>.`
42
42
  )
43
+ if (target === "resource" && path !== "" && path !== "/openai/v1")
44
+ throw new TypeError(
45
+ `${PREFIX} embeddings.endpoint must be a resource origin or /openai/v1 URL.`
46
+ )
43
47
  this.projectUrl = `${url.origin}${path}`
44
- this.baseUrl = `${this.projectUrl}/openai/v1`
48
+ this.baseUrl = target === "project" ? `${this.projectUrl}/openai/v1` : `${url.origin}/openai/v1`
45
49
  if (
46
50
  typeof options.apiKey !== "function" &&
47
51
  (typeof options.apiKey !== "string" || !options.apiKey.trim())
@@ -65,7 +69,7 @@ export class FoundryTransport {
65
69
  signal: AbortSignal,
66
70
  providerId: string,
67
71
  modelId: string,
68
- protocol: FoundryProtocol = "responses"
72
+ protocol: FoundryProtocol | "embeddings" = "responses"
69
73
  ): Promise<Response> {
70
74
  const url = this.url(protocol)
71
75
  const diagnostics = new RequestDiagnostics()
@@ -77,7 +81,7 @@ export class FoundryTransport {
77
81
  protocol === "messages" ? "x-api-key" : "api-key"
78
82
  )
79
83
  headers.set("content-type", "application/json")
80
- headers.set("accept", "text/event-stream")
84
+ headers.set("accept", protocol === "embeddings" ? "application/json" : "text/event-stream")
81
85
  if (protocol === "messages") headers.set("anthropic-version", "2023-06-01")
82
86
  signal.throwIfAborted()
83
87
  // Ambiguous network failures and accepted streams are never automatically replayed.
@@ -107,7 +111,12 @@ export class FoundryTransport {
107
111
  response,
108
112
  await httpError(response, providerId, modelId, signal)
109
113
  )
110
- if (!error.retryable || attempt >= (this.options.maxRetries ?? 2)) throw error
114
+ if (
115
+ protocol === "embeddings" ||
116
+ !error.retryable ||
117
+ attempt >= (this.options.maxRetries ?? 2)
118
+ )
119
+ throw error
111
120
  await wait(
112
121
  Math.min(error.retryAfterMs ?? 250 * 2 ** attempt, this.options.maxRetryDelayMs ?? 60_000),
113
122
  signal
@@ -149,7 +158,8 @@ export class FoundryTransport {
149
158
  return (this.options.fetch ?? fetch)(url, init)
150
159
  }
151
160
 
152
- url(protocol: FoundryProtocol): string {
161
+ url(protocol: FoundryProtocol | "embeddings"): string {
162
+ if (protocol === "embeddings") return `${this.baseUrl}/embeddings`
153
163
  if (protocol === "responses") return `${this.baseUrl}/responses`
154
164
  if (protocol === "chat") return `${this.baseUrl}/chat/completions`
155
165
  return `${new URL(this.baseUrl).origin}/anthropic/v1/messages`