@chainfuse/ai-tools 0.12.3 → 0.13.0
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/dist/providers/customProviders.d.mts +3 -4
- package/dist/providers/customProviders.mjs +1 -41
- package/dist/providers/rawProviders.d.mts +2 -2
- package/dist/providers/rawProviders.mjs +36 -16
- package/dist/providers/types.d.mts +1 -17
- package/dist/registry.d.mts +2 -2
- package/dist/types.d.mts +1 -1
- package/package.json +11 -11
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
import type { GoogleGenerativeAIProvider } from '@ai-sdk/google';
|
|
2
|
-
import type { OpenAICompatibleProvider } from '@ai-sdk/openai-compatible';
|
|
3
2
|
import { AiBase } from '../base.mjs';
|
|
4
3
|
import type { AiRequestConfig, AzureServers } from '../types.mjs';
|
|
5
|
-
import type { AzureOpenAIProvider
|
|
4
|
+
import type { AzureOpenAIProvider } from './types.mjs';
|
|
6
5
|
export declare class AiCustomProviders extends AiBase {
|
|
7
6
|
oaiOpenai(args: AiRequestConfig): Promise<import("@ai-sdk/openai").OpenAIProvider>;
|
|
8
7
|
azOpenai(args: AiRequestConfig, filteredServers?: AzureServers): Promise<AzureOpenAIProvider>;
|
|
9
8
|
anthropic(args: AiRequestConfig): Promise<import("@ai-sdk/anthropic").AnthropicProvider>;
|
|
10
9
|
private static workersAiIsRest;
|
|
11
|
-
cfWorkersAi(args: AiRequestConfig): Promise<
|
|
12
|
-
custom(args: AiRequestConfig): Promise<OpenAICompatibleProvider<string, string, string, string>>;
|
|
10
|
+
cfWorkersAi(args: AiRequestConfig): Promise<import("@ai-sdk/openai-compatible").OpenAICompatibleProvider<"@cf/qwen/qwen1.5-0.5b-chat" | "@cf/google/gemma-2b-it-lora" | "@hf/nexusflow/starling-lm-7b-beta" | "@cf/meta/llama-3-8b-instruct" | "@cf/meta/llama-3.2-3b-instruct" | "@hf/thebloke/llamaguard-7b-awq" | "@hf/thebloke/neural-chat-7b-v3-1-awq" | "@cf/meta/llama-guard-3-8b" | "@cf/meta/llama-2-7b-chat-fp16" | "@cf/mistral/mistral-7b-instruct-v0.1" | "@cf/mistral/mistral-7b-instruct-v0.2-lora" | "@cf/tinyllama/tinyllama-1.1b-chat-v1.0" | "@hf/mistral/mistral-7b-instruct-v0.2" | "@cf/fblgit/una-cybertron-7b-v2-bf16" | "@cf/deepseek-ai/deepseek-r1-distill-qwen-32b" | "@cf/thebloke/discolm-german-7b-v1-awq" | "@cf/meta/llama-2-7b-chat-int8" | "@cf/meta/llama-3.1-8b-instruct-fp8" | "@hf/thebloke/mistral-7b-instruct-v0.1-awq" | "@cf/qwen/qwen1.5-7b-chat-awq" | "@cf/meta/llama-3.2-1b-instruct" | "@hf/thebloke/llama-2-13b-chat-awq" | "@hf/thebloke/deepseek-coder-6.7b-base-awq" | "@cf/meta-llama/llama-2-7b-chat-hf-lora" | "@cf/meta/llama-3.3-70b-instruct-fp8-fast" | "@hf/thebloke/openhermes-2.5-mistral-7b-awq" | "@hf/thebloke/deepseek-coder-6.7b-instruct-awq" | "@cf/deepseek-ai/deepseek-math-7b-instruct" | "@cf/tiiuae/falcon-7b-instruct" | "@hf/nousresearch/hermes-2-pro-mistral-7b" | "@cf/meta/llama-3.1-8b-instruct" | "@cf/meta/llama-3.1-8b-instruct-awq" | "@hf/thebloke/zephyr-7b-beta-awq" | "@cf/google/gemma-7b-it-lora" | "@cf/qwen/qwen1.5-1.8b-chat" | "@cf/meta/llama-3-8b-instruct-awq" | "@cf/meta/llama-3.2-11b-vision-instruct" | "@cf/defog/sqlcoder-7b-2" | "@cf/microsoft/phi-2" | "@hf/meta-llama/meta-llama-3-8b-instruct" | "@hf/google/gemma-7b-it" | "@cf/qwen/qwen1.5-14b-chat-awq" | "@cf/openchat/openchat-3.5-0106" | "@cf/meta/llama-4-scout-17b-16e-instruct", "@cf/qwen/qwen1.5-0.5b-chat" | "@cf/google/gemma-2b-it-lora" | "@hf/nexusflow/starling-lm-7b-beta" | "@cf/meta/llama-3-8b-instruct" | "@cf/meta/llama-3.2-3b-instruct" | "@hf/thebloke/llamaguard-7b-awq" | "@hf/thebloke/neural-chat-7b-v3-1-awq" | "@cf/meta/llama-guard-3-8b" | "@cf/meta/llama-2-7b-chat-fp16" | "@cf/mistral/mistral-7b-instruct-v0.1" | "@cf/mistral/mistral-7b-instruct-v0.2-lora" | "@cf/tinyllama/tinyllama-1.1b-chat-v1.0" | "@hf/mistral/mistral-7b-instruct-v0.2" | "@cf/fblgit/una-cybertron-7b-v2-bf16" | "@cf/deepseek-ai/deepseek-r1-distill-qwen-32b" | "@cf/thebloke/discolm-german-7b-v1-awq" | "@cf/meta/llama-2-7b-chat-int8" | "@cf/meta/llama-3.1-8b-instruct-fp8" | "@hf/thebloke/mistral-7b-instruct-v0.1-awq" | "@cf/qwen/qwen1.5-7b-chat-awq" | "@cf/meta/llama-3.2-1b-instruct" | "@hf/thebloke/llama-2-13b-chat-awq" | "@hf/thebloke/deepseek-coder-6.7b-base-awq" | "@cf/meta-llama/llama-2-7b-chat-hf-lora" | "@cf/meta/llama-3.3-70b-instruct-fp8-fast" | "@hf/thebloke/openhermes-2.5-mistral-7b-awq" | "@hf/thebloke/deepseek-coder-6.7b-instruct-awq" | "@cf/deepseek-ai/deepseek-math-7b-instruct" | "@cf/tiiuae/falcon-7b-instruct" | "@hf/nousresearch/hermes-2-pro-mistral-7b" | "@cf/meta/llama-3.1-8b-instruct" | "@cf/meta/llama-3.1-8b-instruct-awq" | "@hf/thebloke/zephyr-7b-beta-awq" | "@cf/google/gemma-7b-it-lora" | "@cf/qwen/qwen1.5-1.8b-chat" | "@cf/meta/llama-3-8b-instruct-awq" | "@cf/meta/llama-3.2-11b-vision-instruct" | "@cf/defog/sqlcoder-7b-2" | "@cf/microsoft/phi-2" | "@hf/meta-llama/meta-llama-3-8b-instruct" | "@hf/google/gemma-7b-it" | "@cf/qwen/qwen1.5-14b-chat-awq" | "@cf/openchat/openchat-3.5-0106" | "@cf/meta/llama-4-scout-17b-16e-instruct", "@cf/baai/bge-m3" | "@cf/baai/bge-small-en-v1.5" | "@cf/baai/bge-base-en-v1.5" | "@cf/baai/bge-large-en-v1.5", string>>;
|
|
11
|
+
custom(args: AiRequestConfig): Promise<import("@ai-sdk/openai-compatible").OpenAICompatibleProvider<string, string, string, string>>;
|
|
13
12
|
googleAi(args: AiRequestConfig): Promise<GoogleGenerativeAIProvider>;
|
|
14
13
|
}
|
|
@@ -196,47 +196,7 @@ export class AiCustomProviders extends AiBase {
|
|
|
196
196
|
});
|
|
197
197
|
}
|
|
198
198
|
else {
|
|
199
|
-
return
|
|
200
|
-
// @ts-expect-error override for types
|
|
201
|
-
languageModels: await enabledCloudflareLlmProviders.reduce(async (accPromise, model) => {
|
|
202
|
-
const acc = await accPromise;
|
|
203
|
-
/**
|
|
204
|
-
* Intercept and add in missing index property to be OpenAI compatible
|
|
205
|
-
*/
|
|
206
|
-
// @ts-expect-error override for types
|
|
207
|
-
acc[model] = wrapLanguageModel({
|
|
208
|
-
model: (await raw.bindingWorkersAi(args))(model),
|
|
209
|
-
middleware: [
|
|
210
|
-
// Fix output generation where it's correct, but encapsulated in a code fence
|
|
211
|
-
{
|
|
212
|
-
wrapGenerate: async ({ doGenerate, model }) => {
|
|
213
|
-
const result = await doGenerate();
|
|
214
|
-
/**
|
|
215
|
-
* `chunkSchema` is undocumented but always present in `model` regardless of model
|
|
216
|
-
* Can't use `responseFormat` (in `params`) because it isn't always present because some models don't support that part of openai api spec.
|
|
217
|
-
*/
|
|
218
|
-
if ('chunkSchema' in model) {
|
|
219
|
-
const codeFenceStart = new RegExp(/^`{1,3}\w*\s*(?=[\[{])/i);
|
|
220
|
-
const codefenceEnd = new RegExp(/(?![\]}])\s*`{1,3}$/i);
|
|
221
|
-
return {
|
|
222
|
-
...result,
|
|
223
|
-
/**
|
|
224
|
-
* 1. trim initially to remove any leading/trailing whitespace
|
|
225
|
-
* 2. Remove start and end
|
|
226
|
-
* 3. Trim again to remove any leading/trailing whitespace
|
|
227
|
-
*/
|
|
228
|
-
text: result.text?.trim().replace(codeFenceStart, '').replace(codefenceEnd, '').trim(),
|
|
229
|
-
};
|
|
230
|
-
}
|
|
231
|
-
return result;
|
|
232
|
-
},
|
|
233
|
-
},
|
|
234
|
-
],
|
|
235
|
-
});
|
|
236
|
-
return acc;
|
|
237
|
-
}, Promise.resolve({})),
|
|
238
|
-
fallbackProvider: await new AiRawProviders(this.config).bindingWorkersAi(args),
|
|
239
|
-
});
|
|
199
|
+
return new AiRawProviders(this.config).bindingWorkersAi(args);
|
|
240
200
|
}
|
|
241
201
|
}
|
|
242
202
|
custom(args) {
|
|
@@ -2,9 +2,9 @@ import type { OpenAICompatibleProvider } from '@ai-sdk/openai-compatible';
|
|
|
2
2
|
import type { cloudflareModelPossibilities } from '@chainfuse/types';
|
|
3
3
|
import { AiBase } from '../base.mjs';
|
|
4
4
|
import type { AiRequestConfig, Servers } from '../types.mjs';
|
|
5
|
-
import type { WorkersAIProvider } from './types.mts';
|
|
6
5
|
export declare class AiRawProviders extends AiBase {
|
|
7
6
|
private readonly cacheTtl;
|
|
7
|
+
private static serverTimingHeader;
|
|
8
8
|
private updateGatewayLog;
|
|
9
9
|
oaiOpenai(args: AiRequestConfig): Promise<import("@ai-sdk/openai").OpenAIProvider>;
|
|
10
10
|
azOpenai(args: AiRequestConfig, server: Servers[number], cost?: {
|
|
@@ -15,5 +15,5 @@ export declare class AiRawProviders extends AiBase {
|
|
|
15
15
|
custom(args: AiRequestConfig): Promise<OpenAICompatibleProvider<string, string, string, string>>;
|
|
16
16
|
googleAi(args: AiRequestConfig): Promise<import("@ai-sdk/google").GoogleGenerativeAIProvider>;
|
|
17
17
|
restWorkersAi(args: AiRequestConfig): Promise<OpenAICompatibleProvider<cloudflareModelPossibilities<'Text Generation'>, cloudflareModelPossibilities<'Text Generation'>, cloudflareModelPossibilities<'Text Embeddings'>>>;
|
|
18
|
-
bindingWorkersAi(args: AiRequestConfig): Promise<
|
|
18
|
+
bindingWorkersAi(args: AiRequestConfig): Promise<OpenAICompatibleProvider<"@cf/qwen/qwen1.5-0.5b-chat" | "@cf/google/gemma-2b-it-lora" | "@hf/nexusflow/starling-lm-7b-beta" | "@cf/meta/llama-3-8b-instruct" | "@cf/meta/llama-3.2-3b-instruct" | "@hf/thebloke/llamaguard-7b-awq" | "@hf/thebloke/neural-chat-7b-v3-1-awq" | "@cf/meta/llama-guard-3-8b" | "@cf/meta/llama-2-7b-chat-fp16" | "@cf/mistral/mistral-7b-instruct-v0.1" | "@cf/mistral/mistral-7b-instruct-v0.2-lora" | "@cf/tinyllama/tinyllama-1.1b-chat-v1.0" | "@hf/mistral/mistral-7b-instruct-v0.2" | "@cf/fblgit/una-cybertron-7b-v2-bf16" | "@cf/deepseek-ai/deepseek-r1-distill-qwen-32b" | "@cf/thebloke/discolm-german-7b-v1-awq" | "@cf/meta/llama-2-7b-chat-int8" | "@cf/meta/llama-3.1-8b-instruct-fp8" | "@hf/thebloke/mistral-7b-instruct-v0.1-awq" | "@cf/qwen/qwen1.5-7b-chat-awq" | "@cf/meta/llama-3.2-1b-instruct" | "@hf/thebloke/llama-2-13b-chat-awq" | "@hf/thebloke/deepseek-coder-6.7b-base-awq" | "@cf/meta-llama/llama-2-7b-chat-hf-lora" | "@cf/meta/llama-3.3-70b-instruct-fp8-fast" | "@hf/thebloke/openhermes-2.5-mistral-7b-awq" | "@hf/thebloke/deepseek-coder-6.7b-instruct-awq" | "@cf/deepseek-ai/deepseek-math-7b-instruct" | "@cf/tiiuae/falcon-7b-instruct" | "@hf/nousresearch/hermes-2-pro-mistral-7b" | "@cf/meta/llama-3.1-8b-instruct" | "@cf/meta/llama-3.1-8b-instruct-awq" | "@hf/thebloke/zephyr-7b-beta-awq" | "@cf/google/gemma-7b-it-lora" | "@cf/qwen/qwen1.5-1.8b-chat" | "@cf/meta/llama-3-8b-instruct-awq" | "@cf/meta/llama-3.2-11b-vision-instruct" | "@cf/defog/sqlcoder-7b-2" | "@cf/microsoft/phi-2" | "@hf/meta-llama/meta-llama-3-8b-instruct" | "@hf/google/gemma-7b-it" | "@cf/qwen/qwen1.5-14b-chat-awq" | "@cf/openchat/openchat-3.5-0106" | "@cf/meta/llama-4-scout-17b-16e-instruct", "@cf/qwen/qwen1.5-0.5b-chat" | "@cf/google/gemma-2b-it-lora" | "@hf/nexusflow/starling-lm-7b-beta" | "@cf/meta/llama-3-8b-instruct" | "@cf/meta/llama-3.2-3b-instruct" | "@hf/thebloke/llamaguard-7b-awq" | "@hf/thebloke/neural-chat-7b-v3-1-awq" | "@cf/meta/llama-guard-3-8b" | "@cf/meta/llama-2-7b-chat-fp16" | "@cf/mistral/mistral-7b-instruct-v0.1" | "@cf/mistral/mistral-7b-instruct-v0.2-lora" | "@cf/tinyllama/tinyllama-1.1b-chat-v1.0" | "@hf/mistral/mistral-7b-instruct-v0.2" | "@cf/fblgit/una-cybertron-7b-v2-bf16" | "@cf/deepseek-ai/deepseek-r1-distill-qwen-32b" | "@cf/thebloke/discolm-german-7b-v1-awq" | "@cf/meta/llama-2-7b-chat-int8" | "@cf/meta/llama-3.1-8b-instruct-fp8" | "@hf/thebloke/mistral-7b-instruct-v0.1-awq" | "@cf/qwen/qwen1.5-7b-chat-awq" | "@cf/meta/llama-3.2-1b-instruct" | "@hf/thebloke/llama-2-13b-chat-awq" | "@hf/thebloke/deepseek-coder-6.7b-base-awq" | "@cf/meta-llama/llama-2-7b-chat-hf-lora" | "@cf/meta/llama-3.3-70b-instruct-fp8-fast" | "@hf/thebloke/openhermes-2.5-mistral-7b-awq" | "@hf/thebloke/deepseek-coder-6.7b-instruct-awq" | "@cf/deepseek-ai/deepseek-math-7b-instruct" | "@cf/tiiuae/falcon-7b-instruct" | "@hf/nousresearch/hermes-2-pro-mistral-7b" | "@cf/meta/llama-3.1-8b-instruct" | "@cf/meta/llama-3.1-8b-instruct-awq" | "@hf/thebloke/zephyr-7b-beta-awq" | "@cf/google/gemma-7b-it-lora" | "@cf/qwen/qwen1.5-1.8b-chat" | "@cf/meta/llama-3-8b-instruct-awq" | "@cf/meta/llama-3.2-11b-vision-instruct" | "@cf/defog/sqlcoder-7b-2" | "@cf/microsoft/phi-2" | "@hf/meta-llama/meta-llama-3-8b-instruct" | "@hf/google/gemma-7b-it" | "@cf/qwen/qwen1.5-14b-chat-awq" | "@cf/openchat/openchat-3.5-0106" | "@cf/meta/llama-4-scout-17b-16e-instruct", string, string>>;
|
|
19
19
|
}
|
|
@@ -3,25 +3,31 @@ import { AiBase } from '../base.mjs';
|
|
|
3
3
|
export class AiRawProviders extends AiBase {
|
|
4
4
|
// 2628288 seconds is what cf defines as 1 month in their cache rules
|
|
5
5
|
cacheTtl = 2628288;
|
|
6
|
+
static serverTimingHeader(metrics) {
|
|
7
|
+
return Object.entries(metrics)
|
|
8
|
+
.map(([name, duration]) => `${name};dur=${duration}`)
|
|
9
|
+
.join(', ');
|
|
10
|
+
}
|
|
6
11
|
async updateGatewayLog(response, metadataHeader, startRoundTrip, modelTime) {
|
|
7
12
|
const logId = response.headers.get('cf-aig-log-id');
|
|
13
|
+
const rawMetadata = {
|
|
14
|
+
...metadataHeader,
|
|
15
|
+
serverInfo: {
|
|
16
|
+
...JSON.parse(metadataHeader.serverInfo),
|
|
17
|
+
timing: {
|
|
18
|
+
fromCache: response.headers.get('cf-aig-cache-status')?.toLowerCase() === 'hit',
|
|
19
|
+
totalRoundtripTime: performance.now() - startRoundTrip,
|
|
20
|
+
modelTime,
|
|
21
|
+
},
|
|
22
|
+
},
|
|
23
|
+
};
|
|
8
24
|
if (logId) {
|
|
9
25
|
const updateMetadata = import('@chainfuse/helpers')
|
|
10
26
|
.then(({ NetHelpers }) => NetHelpers.cfApi(this.config.gateway.apiToken))
|
|
11
27
|
.then((cf) => cf.aiGateway.logs.edit(this.gatewayName, logId, {
|
|
12
28
|
account_id: this.config.gateway.accountId,
|
|
13
29
|
metadata: {
|
|
14
|
-
...Object.entries({
|
|
15
|
-
...metadataHeader,
|
|
16
|
-
serverInfo: {
|
|
17
|
-
...JSON.parse(metadataHeader.serverInfo),
|
|
18
|
-
timing: {
|
|
19
|
-
fromCache: response.headers.get('cf-aig-cache-status')?.toLowerCase() === 'hit',
|
|
20
|
-
totalRoundtripTime: performance.now() - startRoundTrip,
|
|
21
|
-
modelTime,
|
|
22
|
-
},
|
|
23
|
-
},
|
|
24
|
-
}).reduce((acc, [key, value]) => {
|
|
30
|
+
...Object.entries(rawMetadata).reduce((acc, [key, value]) => {
|
|
25
31
|
acc[key] = typeof value === 'string' ? value : JSON.stringify(value);
|
|
26
32
|
return acc;
|
|
27
33
|
}, {}),
|
|
@@ -37,6 +43,10 @@ export class AiRawProviders extends AiBase {
|
|
|
37
43
|
else {
|
|
38
44
|
console.warn('Not updating gateway log, no cf-aig-log-id header');
|
|
39
45
|
}
|
|
46
|
+
return AiRawProviders.serverTimingHeader({
|
|
47
|
+
total: rawMetadata.serverInfo.timing.totalRoundtripTime,
|
|
48
|
+
...(rawMetadata.serverInfo.timing?.modelTime && { model: rawMetadata.serverInfo.timing.modelTime }),
|
|
49
|
+
});
|
|
40
50
|
}
|
|
41
51
|
oaiOpenai(args) {
|
|
42
52
|
return import('@ai-sdk/openai').then(async ({ createOpenAI }) => createOpenAI({
|
|
@@ -72,10 +82,11 @@ export class AiRawProviders extends AiBase {
|
|
|
72
82
|
return fetch(input, { ...rawInit, headers }).then(async (response) => {
|
|
73
83
|
if (args.logging ?? this.gatewayLog)
|
|
74
84
|
console.info('ai', 'raw provider', this.chalk.rgb(...Helpers.uniqueIdColor(metadataHeader.idempotencyId))(`[${metadataHeader.idempotencyId}]`), response.ok ? this.chalk.green(response.status) : this.chalk.red(response.status), response.ok ? this.chalk.green(new URL(response.url).pathname) : this.chalk.red(new URL(response.url).pathname));
|
|
75
|
-
await this.updateGatewayLog(response, metadataHeader, startRoundTrip, response.headers.has('openai-processing-ms') ? parseInt(response.headers.get('openai-processing-ms')) : undefined);
|
|
76
85
|
// Inject it to have it available for retries
|
|
77
86
|
const mutableHeaders = new Headers(response.headers);
|
|
87
|
+
const serverTiming = await this.updateGatewayLog(response, metadataHeader, startRoundTrip, response.headers.has('openai-processing-ms') ? parseInt(response.headers.get('openai-processing-ms')) : undefined);
|
|
78
88
|
mutableHeaders.set('X-Idempotency-Id', metadataHeader.idempotencyId);
|
|
89
|
+
mutableHeaders.set('Server-Timing', serverTiming);
|
|
79
90
|
if (response.ok) {
|
|
80
91
|
return new Response(response.body, { ...response, headers: mutableHeaders });
|
|
81
92
|
}
|
|
@@ -133,10 +144,11 @@ export class AiRawProviders extends AiBase {
|
|
|
133
144
|
return fetch(input, { ...rawInit, headers }).then(async (response) => {
|
|
134
145
|
if (args.logging ?? this.gatewayLog)
|
|
135
146
|
console.info('ai', 'raw provider', this.chalk.rgb(...Helpers.uniqueIdColor(metadataHeader.idempotencyId))(`[${metadataHeader.idempotencyId}]`), response.ok ? this.chalk.green(response.status) : this.chalk.red(response.status), response.ok ? this.chalk.green(new URL(response.url).pathname) : this.chalk.red(new URL(response.url).pathname));
|
|
136
|
-
await this.updateGatewayLog(response, metadataHeader, startRoundTrip, response.headers.has('x-envoy-upstream-service-time') ? parseInt(response.headers.get('x-envoy-upstream-service-time')) : undefined);
|
|
137
147
|
// Inject it to have it available for retries
|
|
138
148
|
const mutableHeaders = new Headers(response.headers);
|
|
149
|
+
const serverTiming = await this.updateGatewayLog(response, metadataHeader, startRoundTrip, response.headers.has('x-envoy-upstream-service-time') ? parseInt(response.headers.get('x-envoy-upstream-service-time')) : undefined);
|
|
139
150
|
mutableHeaders.set('X-Idempotency-Id', metadataHeader.idempotencyId);
|
|
151
|
+
mutableHeaders.set('Server-Timing', serverTiming);
|
|
140
152
|
if (response.ok) {
|
|
141
153
|
return new Response(response.body, { ...response, headers: mutableHeaders });
|
|
142
154
|
}
|
|
@@ -181,10 +193,11 @@ export class AiRawProviders extends AiBase {
|
|
|
181
193
|
return fetch(input, { ...rawInit, headers }).then(async (response) => {
|
|
182
194
|
if (args.logging ?? this.gatewayLog)
|
|
183
195
|
console.info('ai', 'raw provider', this.chalk.rgb(...Helpers.uniqueIdColor(metadataHeader.idempotencyId))(`[${metadataHeader.idempotencyId}]`), response.ok ? this.chalk.green(response.status) : this.chalk.red(response.status), response.ok ? this.chalk.green(new URL(response.url).pathname) : this.chalk.red(new URL(response.url).pathname));
|
|
184
|
-
await this.updateGatewayLog(response, metadataHeader, startRoundTrip);
|
|
185
196
|
// Inject it to have it available for retries
|
|
186
197
|
const mutableHeaders = new Headers(response.headers);
|
|
198
|
+
const serverTiming = await this.updateGatewayLog(response, metadataHeader, startRoundTrip);
|
|
187
199
|
mutableHeaders.set('X-Idempotency-Id', metadataHeader.idempotencyId);
|
|
200
|
+
mutableHeaders.set('Server-Timing', serverTiming);
|
|
188
201
|
if (response.ok) {
|
|
189
202
|
return new Response(response.body, { ...response, headers: mutableHeaders });
|
|
190
203
|
}
|
|
@@ -341,10 +354,11 @@ export class AiRawProviders extends AiBase {
|
|
|
341
354
|
return fetch(input, { ...rawInit, headers }).then(async (response) => {
|
|
342
355
|
if (args.logging ?? this.gatewayLog)
|
|
343
356
|
console.info('ai', 'raw provider', this.chalk.rgb(...Helpers.uniqueIdColor(metadataHeader.idempotencyId))(`[${metadataHeader.idempotencyId}]`), response.ok ? this.chalk.green(response.status) : this.chalk.red(response.status), response.ok ? this.chalk.green(new URL(response.url).pathname) : this.chalk.red(new URL(response.url).pathname));
|
|
344
|
-
await this.updateGatewayLog(response, metadataHeader, startRoundTrip);
|
|
345
357
|
// Inject it to have it available for retries
|
|
346
358
|
const mutableHeaders = new Headers(response.headers);
|
|
359
|
+
const serverTiming = await this.updateGatewayLog(response, metadataHeader, startRoundTrip);
|
|
347
360
|
mutableHeaders.set('X-Idempotency-Id', metadataHeader.idempotencyId);
|
|
361
|
+
mutableHeaders.set('Server-Timing', serverTiming);
|
|
348
362
|
if (response.ok) {
|
|
349
363
|
return new Response(response.body, { ...response, headers: mutableHeaders });
|
|
350
364
|
}
|
|
@@ -390,10 +404,16 @@ export class AiRawProviders extends AiBase {
|
|
|
390
404
|
return fetch(input, { ...rawInit, headers }).then(async (response) => {
|
|
391
405
|
if (args.logging ?? this.gatewayLog)
|
|
392
406
|
console.info('ai', 'raw provider', this.chalk.rgb(...Helpers.uniqueIdColor(metadataHeader.idempotencyId))(`[${metadataHeader.idempotencyId}]`), response.ok ? this.chalk.green(response.status) : this.chalk.red(response.status), response.ok ? this.chalk.green(new URL(response.url).pathname) : this.chalk.red(new URL(response.url).pathname));
|
|
393
|
-
await this.updateGatewayLog(response, metadataHeader, startRoundTrip);
|
|
394
407
|
// Inject it to have it available for retries
|
|
395
408
|
const mutableHeaders = new Headers(response.headers);
|
|
409
|
+
if (headers.get('cf-ray')) {
|
|
410
|
+
metadataHeader.serverInfo = JSON.stringify({
|
|
411
|
+
name: `cloudflare-${mutableHeaders.get('cf-ray')?.split('-')[1]}`,
|
|
412
|
+
});
|
|
413
|
+
}
|
|
414
|
+
const serverTiming = await this.updateGatewayLog(response, metadataHeader, startRoundTrip);
|
|
396
415
|
mutableHeaders.set('X-Idempotency-Id', metadataHeader.idempotencyId);
|
|
416
|
+
mutableHeaders.set('Server-Timing', serverTiming);
|
|
397
417
|
if (response.ok) {
|
|
398
418
|
return new Response(response.body, { ...response, headers: mutableHeaders });
|
|
399
419
|
}
|
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import type { OpenAIChatSettings, OpenAIEmbeddingSettings } from '@ai-sdk/openai/internal';
|
|
2
2
|
import type { EmbeddingModelV1, LanguageModelV1 } from '@ai-sdk/provider';
|
|
3
|
-
import type { AzureChatModels, AzureEmbeddingModels
|
|
3
|
+
import type { AzureChatModels, AzureEmbeddingModels } from '@chainfuse/types';
|
|
4
4
|
import type { Provider } from 'ai';
|
|
5
|
-
import type { createWorkersAI, WorkersAI } from 'workers-ai-provider';
|
|
6
5
|
export interface AzureOpenAIProvider extends Provider {
|
|
7
6
|
(deploymentId: AzureChatModels, settings?: OpenAIChatSettings): LanguageModelV1;
|
|
8
7
|
/**
|
|
@@ -14,18 +13,3 @@ export interface AzureOpenAIProvider extends Provider {
|
|
|
14
13
|
*/
|
|
15
14
|
textEmbeddingModel(deploymentId: AzureEmbeddingModels, settings?: OpenAIEmbeddingSettings): EmbeddingModelV1<string>;
|
|
16
15
|
}
|
|
17
|
-
export interface WorkersAIProvider extends WorkersAI {
|
|
18
|
-
(modelId: cloudflareModelPossibilities<'Text Generation'>, settings?: Parameters<typeof createWorkersAI>[0]): ReturnType<ReturnType<typeof createWorkersAI>>;
|
|
19
|
-
/**
|
|
20
|
-
* Creates a model for text generation.
|
|
21
|
-
**/
|
|
22
|
-
chat(modelId: cloudflareModelPossibilities<'Text Generation'>, settings?: Parameters<typeof createWorkersAI>[0]): ReturnType<ReturnType<typeof createWorkersAI>['chat']>;
|
|
23
|
-
/**
|
|
24
|
-
* Creates an Azure OpenAI chat model for text generation.
|
|
25
|
-
*/
|
|
26
|
-
languageModel(modelId: cloudflareModelPossibilities<'Text Generation'>, settings?: Parameters<typeof createWorkersAI>[0]): ReturnType<ReturnType<typeof createWorkersAI>>;
|
|
27
|
-
/**
|
|
28
|
-
* Creates an Azure OpenAI model for text embeddings.
|
|
29
|
-
*/
|
|
30
|
-
textEmbeddingModel(modelId: cloudflareModelPossibilities<'Text Embeddings'>, settings?: Parameters<typeof createWorkersAI>[0]): EmbeddingModelV1<string>;
|
|
31
|
-
}
|
package/dist/registry.d.mts
CHANGED
|
@@ -7,7 +7,7 @@ export declare class AiRegistry extends AiBase {
|
|
|
7
7
|
anthropic: import("@ai-sdk/anthropic").AnthropicProvider;
|
|
8
8
|
custom: import("@ai-sdk/openai-compatible").OpenAICompatibleProvider<string, string, string, string>;
|
|
9
9
|
'google.generative-ai': import("@ai-sdk/google").GoogleGenerativeAIProvider;
|
|
10
|
-
workersai: import("
|
|
10
|
+
workersai: import("@ai-sdk/openai-compatible").OpenAICompatibleProvider<"@cf/qwen/qwen1.5-0.5b-chat" | "@cf/google/gemma-2b-it-lora" | "@hf/nexusflow/starling-lm-7b-beta" | "@cf/meta/llama-3-8b-instruct" | "@cf/meta/llama-3.2-3b-instruct" | "@hf/thebloke/llamaguard-7b-awq" | "@hf/thebloke/neural-chat-7b-v3-1-awq" | "@cf/meta/llama-guard-3-8b" | "@cf/meta/llama-2-7b-chat-fp16" | "@cf/mistral/mistral-7b-instruct-v0.1" | "@cf/mistral/mistral-7b-instruct-v0.2-lora" | "@cf/tinyllama/tinyllama-1.1b-chat-v1.0" | "@hf/mistral/mistral-7b-instruct-v0.2" | "@cf/fblgit/una-cybertron-7b-v2-bf16" | "@cf/deepseek-ai/deepseek-r1-distill-qwen-32b" | "@cf/thebloke/discolm-german-7b-v1-awq" | "@cf/meta/llama-2-7b-chat-int8" | "@cf/meta/llama-3.1-8b-instruct-fp8" | "@hf/thebloke/mistral-7b-instruct-v0.1-awq" | "@cf/qwen/qwen1.5-7b-chat-awq" | "@cf/meta/llama-3.2-1b-instruct" | "@hf/thebloke/llama-2-13b-chat-awq" | "@hf/thebloke/deepseek-coder-6.7b-base-awq" | "@cf/meta-llama/llama-2-7b-chat-hf-lora" | "@cf/meta/llama-3.3-70b-instruct-fp8-fast" | "@hf/thebloke/openhermes-2.5-mistral-7b-awq" | "@hf/thebloke/deepseek-coder-6.7b-instruct-awq" | "@cf/deepseek-ai/deepseek-math-7b-instruct" | "@cf/tiiuae/falcon-7b-instruct" | "@hf/nousresearch/hermes-2-pro-mistral-7b" | "@cf/meta/llama-3.1-8b-instruct" | "@cf/meta/llama-3.1-8b-instruct-awq" | "@hf/thebloke/zephyr-7b-beta-awq" | "@cf/google/gemma-7b-it-lora" | "@cf/qwen/qwen1.5-1.8b-chat" | "@cf/meta/llama-3-8b-instruct-awq" | "@cf/meta/llama-3.2-11b-vision-instruct" | "@cf/defog/sqlcoder-7b-2" | "@cf/microsoft/phi-2" | "@hf/meta-llama/meta-llama-3-8b-instruct" | "@hf/google/gemma-7b-it" | "@cf/qwen/qwen1.5-14b-chat-awq" | "@cf/openchat/openchat-3.5-0106" | "@cf/meta/llama-4-scout-17b-16e-instruct", "@cf/qwen/qwen1.5-0.5b-chat" | "@cf/google/gemma-2b-it-lora" | "@hf/nexusflow/starling-lm-7b-beta" | "@cf/meta/llama-3-8b-instruct" | "@cf/meta/llama-3.2-3b-instruct" | "@hf/thebloke/llamaguard-7b-awq" | "@hf/thebloke/neural-chat-7b-v3-1-awq" | "@cf/meta/llama-guard-3-8b" | "@cf/meta/llama-2-7b-chat-fp16" | "@cf/mistral/mistral-7b-instruct-v0.1" | "@cf/mistral/mistral-7b-instruct-v0.2-lora" | "@cf/tinyllama/tinyllama-1.1b-chat-v1.0" | "@hf/mistral/mistral-7b-instruct-v0.2" | "@cf/fblgit/una-cybertron-7b-v2-bf16" | "@cf/deepseek-ai/deepseek-r1-distill-qwen-32b" | "@cf/thebloke/discolm-german-7b-v1-awq" | "@cf/meta/llama-2-7b-chat-int8" | "@cf/meta/llama-3.1-8b-instruct-fp8" | "@hf/thebloke/mistral-7b-instruct-v0.1-awq" | "@cf/qwen/qwen1.5-7b-chat-awq" | "@cf/meta/llama-3.2-1b-instruct" | "@hf/thebloke/llama-2-13b-chat-awq" | "@hf/thebloke/deepseek-coder-6.7b-base-awq" | "@cf/meta-llama/llama-2-7b-chat-hf-lora" | "@cf/meta/llama-3.3-70b-instruct-fp8-fast" | "@hf/thebloke/openhermes-2.5-mistral-7b-awq" | "@hf/thebloke/deepseek-coder-6.7b-instruct-awq" | "@cf/deepseek-ai/deepseek-math-7b-instruct" | "@cf/tiiuae/falcon-7b-instruct" | "@hf/nousresearch/hermes-2-pro-mistral-7b" | "@cf/meta/llama-3.1-8b-instruct" | "@cf/meta/llama-3.1-8b-instruct-awq" | "@hf/thebloke/zephyr-7b-beta-awq" | "@cf/google/gemma-7b-it-lora" | "@cf/qwen/qwen1.5-1.8b-chat" | "@cf/meta/llama-3-8b-instruct-awq" | "@cf/meta/llama-3.2-11b-vision-instruct" | "@cf/defog/sqlcoder-7b-2" | "@cf/microsoft/phi-2" | "@hf/meta-llama/meta-llama-3-8b-instruct" | "@hf/google/gemma-7b-it" | "@cf/qwen/qwen1.5-14b-chat-awq" | "@cf/openchat/openchat-3.5-0106" | "@cf/meta/llama-4-scout-17b-16e-instruct", "@cf/baai/bge-m3" | "@cf/baai/bge-small-en-v1.5" | "@cf/baai/bge-base-en-v1.5" | "@cf/baai/bge-large-en-v1.5", string>;
|
|
11
11
|
}>>;
|
|
12
12
|
registry(args: AiRequestConfig): Promise<import("ai").ProviderRegistryProvider<Readonly<{
|
|
13
13
|
openai: import("@ai-sdk/openai").OpenAIProvider;
|
|
@@ -15,6 +15,6 @@ export declare class AiRegistry extends AiBase {
|
|
|
15
15
|
anthropic: import("@ai-sdk/anthropic").AnthropicProvider;
|
|
16
16
|
custom: import("@ai-sdk/openai-compatible").OpenAICompatibleProvider<string, string, string, string>;
|
|
17
17
|
'google.generative-ai': import("@ai-sdk/google").GoogleGenerativeAIProvider;
|
|
18
|
-
workersai: import("
|
|
18
|
+
workersai: import("@ai-sdk/openai-compatible").OpenAICompatibleProvider<"@cf/qwen/qwen1.5-0.5b-chat" | "@cf/google/gemma-2b-it-lora" | "@hf/nexusflow/starling-lm-7b-beta" | "@cf/meta/llama-3-8b-instruct" | "@cf/meta/llama-3.2-3b-instruct" | "@hf/thebloke/llamaguard-7b-awq" | "@hf/thebloke/neural-chat-7b-v3-1-awq" | "@cf/meta/llama-guard-3-8b" | "@cf/meta/llama-2-7b-chat-fp16" | "@cf/mistral/mistral-7b-instruct-v0.1" | "@cf/mistral/mistral-7b-instruct-v0.2-lora" | "@cf/tinyllama/tinyllama-1.1b-chat-v1.0" | "@hf/mistral/mistral-7b-instruct-v0.2" | "@cf/fblgit/una-cybertron-7b-v2-bf16" | "@cf/deepseek-ai/deepseek-r1-distill-qwen-32b" | "@cf/thebloke/discolm-german-7b-v1-awq" | "@cf/meta/llama-2-7b-chat-int8" | "@cf/meta/llama-3.1-8b-instruct-fp8" | "@hf/thebloke/mistral-7b-instruct-v0.1-awq" | "@cf/qwen/qwen1.5-7b-chat-awq" | "@cf/meta/llama-3.2-1b-instruct" | "@hf/thebloke/llama-2-13b-chat-awq" | "@hf/thebloke/deepseek-coder-6.7b-base-awq" | "@cf/meta-llama/llama-2-7b-chat-hf-lora" | "@cf/meta/llama-3.3-70b-instruct-fp8-fast" | "@hf/thebloke/openhermes-2.5-mistral-7b-awq" | "@hf/thebloke/deepseek-coder-6.7b-instruct-awq" | "@cf/deepseek-ai/deepseek-math-7b-instruct" | "@cf/tiiuae/falcon-7b-instruct" | "@hf/nousresearch/hermes-2-pro-mistral-7b" | "@cf/meta/llama-3.1-8b-instruct" | "@cf/meta/llama-3.1-8b-instruct-awq" | "@hf/thebloke/zephyr-7b-beta-awq" | "@cf/google/gemma-7b-it-lora" | "@cf/qwen/qwen1.5-1.8b-chat" | "@cf/meta/llama-3-8b-instruct-awq" | "@cf/meta/llama-3.2-11b-vision-instruct" | "@cf/defog/sqlcoder-7b-2" | "@cf/microsoft/phi-2" | "@hf/meta-llama/meta-llama-3-8b-instruct" | "@hf/google/gemma-7b-it" | "@cf/qwen/qwen1.5-14b-chat-awq" | "@cf/openchat/openchat-3.5-0106" | "@cf/meta/llama-4-scout-17b-16e-instruct", "@cf/qwen/qwen1.5-0.5b-chat" | "@cf/google/gemma-2b-it-lora" | "@hf/nexusflow/starling-lm-7b-beta" | "@cf/meta/llama-3-8b-instruct" | "@cf/meta/llama-3.2-3b-instruct" | "@hf/thebloke/llamaguard-7b-awq" | "@hf/thebloke/neural-chat-7b-v3-1-awq" | "@cf/meta/llama-guard-3-8b" | "@cf/meta/llama-2-7b-chat-fp16" | "@cf/mistral/mistral-7b-instruct-v0.1" | "@cf/mistral/mistral-7b-instruct-v0.2-lora" | "@cf/tinyllama/tinyllama-1.1b-chat-v1.0" | "@hf/mistral/mistral-7b-instruct-v0.2" | "@cf/fblgit/una-cybertron-7b-v2-bf16" | "@cf/deepseek-ai/deepseek-r1-distill-qwen-32b" | "@cf/thebloke/discolm-german-7b-v1-awq" | "@cf/meta/llama-2-7b-chat-int8" | "@cf/meta/llama-3.1-8b-instruct-fp8" | "@hf/thebloke/mistral-7b-instruct-v0.1-awq" | "@cf/qwen/qwen1.5-7b-chat-awq" | "@cf/meta/llama-3.2-1b-instruct" | "@hf/thebloke/llama-2-13b-chat-awq" | "@hf/thebloke/deepseek-coder-6.7b-base-awq" | "@cf/meta-llama/llama-2-7b-chat-hf-lora" | "@cf/meta/llama-3.3-70b-instruct-fp8-fast" | "@hf/thebloke/openhermes-2.5-mistral-7b-awq" | "@hf/thebloke/deepseek-coder-6.7b-instruct-awq" | "@cf/deepseek-ai/deepseek-math-7b-instruct" | "@cf/tiiuae/falcon-7b-instruct" | "@hf/nousresearch/hermes-2-pro-mistral-7b" | "@cf/meta/llama-3.1-8b-instruct" | "@cf/meta/llama-3.1-8b-instruct-awq" | "@hf/thebloke/zephyr-7b-beta-awq" | "@cf/google/gemma-7b-it-lora" | "@cf/qwen/qwen1.5-1.8b-chat" | "@cf/meta/llama-3-8b-instruct-awq" | "@cf/meta/llama-3.2-11b-vision-instruct" | "@cf/defog/sqlcoder-7b-2" | "@cf/microsoft/phi-2" | "@hf/meta-llama/meta-llama-3-8b-instruct" | "@hf/google/gemma-7b-it" | "@cf/qwen/qwen1.5-14b-chat-awq" | "@cf/openchat/openchat-3.5-0106" | "@cf/meta/llama-4-scout-17b-16e-instruct", "@cf/baai/bge-m3" | "@cf/baai/bge-small-en-v1.5" | "@cf/baai/bge-base-en-v1.5" | "@cf/baai/bge-large-en-v1.5", string>;
|
|
19
19
|
}>, ":">>;
|
|
20
20
|
}
|
package/dist/types.d.mts
CHANGED
|
@@ -113,7 +113,7 @@ export interface AiRequestMetadataTiming {
|
|
|
113
113
|
totalRoundtripTime: number;
|
|
114
114
|
}
|
|
115
115
|
export interface AiRequestMetadataServerInfo {
|
|
116
|
-
name: 'anthropic' | 'cloudflare' | 'googleai' | 'openai';
|
|
116
|
+
name: 'anthropic' | 'cloudflare' | `cloudflare-${string}` | 'googleai' | 'openai';
|
|
117
117
|
}
|
|
118
118
|
export interface AiRequestMetadataServerInfoWithLocation {
|
|
119
119
|
name: `${'azure' | 'google'}-${string}`;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@chainfuse/ai-tools",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.13.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"author": "ChainFuse",
|
|
6
6
|
"homepage": "https://github.com/ChainFuse/packages/tree/main/packages/ai-tools#readme",
|
|
@@ -48,21 +48,21 @@
|
|
|
48
48
|
},
|
|
49
49
|
"prettier": "@demosjarco/prettier-config",
|
|
50
50
|
"dependencies": {
|
|
51
|
-
"@ai-sdk/anthropic": "^1.2.
|
|
52
|
-
"@ai-sdk/azure": "^1.3.
|
|
53
|
-
"@ai-sdk/google": "^1.2.
|
|
51
|
+
"@ai-sdk/anthropic": "^1.2.6",
|
|
52
|
+
"@ai-sdk/azure": "^1.3.8",
|
|
53
|
+
"@ai-sdk/google": "^1.2.8",
|
|
54
54
|
"@ai-sdk/openai": "^1.0.5",
|
|
55
|
-
"@ai-sdk/openai-compatible": "^0.2.
|
|
56
|
-
"@chainfuse/helpers": "^2.
|
|
57
|
-
"@chainfuse/types": "^2.
|
|
58
|
-
"ai": "^4.2
|
|
55
|
+
"@ai-sdk/openai-compatible": "^0.2.6",
|
|
56
|
+
"@chainfuse/helpers": "^2.3.0",
|
|
57
|
+
"@chainfuse/types": "^2.5.0",
|
|
58
|
+
"ai": "^4.3.2",
|
|
59
59
|
"chalk": "^5.4.1",
|
|
60
60
|
"haversine-distance": "^1.2.3",
|
|
61
61
|
"workers-ai-provider": "^0.3.0"
|
|
62
62
|
},
|
|
63
63
|
"devDependencies": {
|
|
64
|
-
"@cloudflare/workers-types": "^4.
|
|
65
|
-
"openai": "^4.
|
|
64
|
+
"@cloudflare/workers-types": "^4.20250407.0",
|
|
65
|
+
"openai": "^4.92.0"
|
|
66
66
|
},
|
|
67
|
-
"gitHead": "
|
|
67
|
+
"gitHead": "bbe7acd905d1ac371a01cbdbcba1fb0023ee185c"
|
|
68
68
|
}
|