@chainfuse/ai-tools 1.1.0 → 1.1.1

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/base.mjs CHANGED
@@ -8,7 +8,10 @@ export class AiBase {
8
8
  get config() {
9
9
  return {
10
10
  ...Object.freeze(this._config),
11
- gateway: this._config.gateway,
11
+ gateway: {
12
+ ...Object.freeze(this._config.gateway),
13
+ ...('binding' in this._config.gateway && { binding: this._config.gateway.binding }),
14
+ },
12
15
  providers: {
13
16
  ...Object.freeze(this._config.providers),
14
17
  workersAi: this._config.providers.workersAi,
@@ -24,7 +24,7 @@ export class AiRawProviders extends AiBase {
24
24
  };
25
25
  if (logId) {
26
26
  const updateMetadata = (() => {
27
- if ('binding' in this.config.gateway && typeof this.config.gateway.binding?.gateway === 'function') {
27
+ if ('binding' in this.config.gateway) {
28
28
  return this.config.gateway.binding
29
29
  .gateway(this.gatewayName)
30
30
  .patchLog(logId, {
@@ -39,7 +39,7 @@ export class AiRawProviders extends AiBase {
39
39
  }
40
40
  else {
41
41
  return import('@chainfuse/helpers')
42
- .then(({ NetHelpers }) => NetHelpers.cfApi(this.config.gateway.apiToken, { logging: { level: Number(logging) } }))
42
+ .then(({ NetHelpers }) => NetHelpers.cfApi('apiToken' in this.config.gateway ? this.config.gateway.apiToken : '', { logging: { level: Number(logging) } }))
43
43
  .then((cf) => cf.aiGateway.logs.edit(this.gatewayName, logId, {
44
44
  account_id: this.config.gateway.accountId,
45
45
  metadata: {
@@ -72,7 +72,7 @@ export class AiRawProviders extends AiBase {
72
72
  apiKey: this.config.providers.openAi.apiToken,
73
73
  organization: this.config.providers.openAi.organization,
74
74
  headers: {
75
- 'cf-aig-authorization': `Bearer ${this.config.gateway.apiToken}`,
75
+ 'cf-aig-authorization': `Bearer ${'apiToken' in this.config.gateway ? this.config.gateway.apiToken : ''}`,
76
76
  'cf-aig-metadata': JSON.stringify({
77
77
  dataspaceId: (await BufferHelpers.uuidConvert(args.dataspaceId)).utf8,
78
78
  ...(args.groupBillingId && { groupBillingId: (await BufferHelpers.uuidConvert(args.groupBillingId)).utf8 }),
@@ -96,7 +96,54 @@ export class AiRawProviders extends AiBase {
96
96
  }
97
97
  if (args.logging ?? this.gatewayLog)
98
98
  console.info(new Date().toISOString(), this.chalk.rgb(...Helpers.uniqueIdColor(metadataHeader.idempotencyId))(`[${metadataHeader.idempotencyId}]`), this.chalk.magenta(rawInit?.method), this.chalk.magenta(new URL(new Request(input).url).pathname));
99
- return fetch(input, { ...rawInit, headers }).then(async (response) => {
99
+ return (() => {
100
+ if ('binding' in this.config.gateway) {
101
+ const fallbackedHeaders = {
102
+ ...Object.fromEntries(Array.from(headers.entries()).filter(([key]) => !key.toLowerCase().startsWith('cf-aig-'))),
103
+ 'cf-aig-metadata': JSON.stringify({
104
+ ...metadataHeader,
105
+ serverInfo: JSON.stringify({
106
+ name: 'openai',
107
+ }),
108
+ }),
109
+ };
110
+ // Prevent double stringification
111
+ let fallbackedQuery;
112
+ try {
113
+ fallbackedQuery = JSON.parse(rawInit?.body);
114
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
115
+ }
116
+ catch (error) {
117
+ fallbackedQuery = rawInit?.body;
118
+ }
119
+ return this.config.gateway.binding.gateway(this.gatewayName).run({
120
+ provider: 'openai',
121
+ endpoint: new URL(new Request(input).url).pathname.split('/').slice(5).join('/'),
122
+ headers: fallbackedHeaders,
123
+ query: fallbackedQuery,
124
+ }, {
125
+ extraHeaders: (() => {
126
+ // Prevent duplicates
127
+ headers.delete('cf-aig-authorization');
128
+ headers.delete('cf-aig-metadata');
129
+ headers.delete('cf-aig-cache-ttl');
130
+ headers.delete('cf-aig-skip-cache');
131
+ return headers;
132
+ })(),
133
+ gateway: {
134
+ id: this.gatewayName,
135
+ eventId: metadataHeader.idempotencyId,
136
+ metadata: metadataHeader,
137
+ ...(args.cache && { cacheTtl: typeof args.cache === 'boolean' ? (args.cache ? this.cacheTtl : 0) : args.cache }),
138
+ ...(args.skipCache && { skipCache: true }),
139
+ },
140
+ });
141
+ }
142
+ else {
143
+ return fetch(input, { ...rawInit, headers });
144
+ }
145
+ })().then(async (_response) => {
146
+ const response = _response;
100
147
  if (args.logging ?? this.gatewayLog)
101
148
  console.info(new Date().toISOString(), 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));
102
149
  // Inject it to have it available for retries
@@ -136,7 +183,7 @@ export class AiRawProviders extends AiBase {
136
183
  useDeploymentBasedUrls: true,
137
184
  baseURL: new URL(['v1', this.config.gateway.accountId, this.gatewayName, 'azure-openai', 'server-placeholder'].join('/'), 'https://gateway.ai.cloudflare.com').toString(),
138
185
  headers: {
139
- 'cf-aig-authorization': `Bearer ${this.config.gateway.apiToken}`,
186
+ 'cf-aig-authorization': `Bearer ${'apiToken' in this.config.gateway ? this.config.gateway.apiToken : ''}`,
140
187
  'cf-aig-metadata': JSON.stringify(metadataHeader),
141
188
  ...(args.cache && { 'cf-aig-cache-ttl': (typeof args.cache === 'boolean' ? (args.cache ? this.cacheTtl : 0) : args.cache).toString() }),
142
189
  ...(args.skipCache && { 'cf-aig-skip-cache': 'true' }),
@@ -288,7 +335,7 @@ export class AiRawProviders extends AiBase {
288
335
  baseURL: new URL(['v1', this.config.gateway.accountId, this.gatewayName, 'anthropic'].join('/'), 'https://gateway.ai.cloudflare.com').toString(),
289
336
  apiKey: this.config.providers.anthropic.apiToken,
290
337
  headers: {
291
- 'cf-aig-authorization': `Bearer ${this.config.gateway.apiToken}`,
338
+ 'cf-aig-authorization': `Bearer ${'apiToken' in this.config.gateway ? this.config.gateway.apiToken : ''}`,
292
339
  'cf-aig-metadata': JSON.stringify({
293
340
  dataspaceId: (await BufferHelpers.uuidConvert(args.dataspaceId)).utf8,
294
341
  ...(args.groupBillingId && { groupBillingId: (await BufferHelpers.uuidConvert(args.groupBillingId)).utf8 }),
@@ -312,7 +359,54 @@ export class AiRawProviders extends AiBase {
312
359
  }
313
360
  if (args.logging ?? this.gatewayLog)
314
361
  console.info(new Date().toISOString(), this.chalk.rgb(...Helpers.uniqueIdColor(metadataHeader.idempotencyId))(`[${metadataHeader.idempotencyId}]`), this.chalk.magenta(rawInit?.method), this.chalk.magenta(new URL(new Request(input).url).pathname));
315
- return fetch(input, { ...rawInit, headers }).then(async (response) => {
362
+ return (() => {
363
+ if ('binding' in this.config.gateway) {
364
+ const fallbackedHeaders = {
365
+ ...Object.fromEntries(Array.from(headers.entries()).filter(([key]) => !key.toLowerCase().startsWith('cf-aig-'))),
366
+ 'cf-aig-metadata': JSON.stringify({
367
+ ...metadataHeader,
368
+ serverInfo: JSON.stringify({
369
+ name: 'anthropic',
370
+ }),
371
+ }),
372
+ };
373
+ // Prevent double stringification
374
+ let fallbackedQuery;
375
+ try {
376
+ fallbackedQuery = JSON.parse(rawInit?.body);
377
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
378
+ }
379
+ catch (error) {
380
+ fallbackedQuery = rawInit?.body;
381
+ }
382
+ return this.config.gateway.binding.gateway(this.gatewayName).run({
383
+ provider: 'anthropic',
384
+ endpoint: new URL(new Request(input).url).pathname.split('/').slice(5).join('/'),
385
+ headers: fallbackedHeaders,
386
+ query: fallbackedQuery,
387
+ }, {
388
+ extraHeaders: (() => {
389
+ // Prevent duplicates
390
+ headers.delete('cf-aig-authorization');
391
+ headers.delete('cf-aig-metadata');
392
+ headers.delete('cf-aig-cache-ttl');
393
+ headers.delete('cf-aig-skip-cache');
394
+ return headers;
395
+ })(),
396
+ gateway: {
397
+ id: this.gatewayName,
398
+ eventId: metadataHeader.idempotencyId,
399
+ metadata: metadataHeader,
400
+ ...(args.cache && { cacheTtl: typeof args.cache === 'boolean' ? (args.cache ? this.cacheTtl : 0) : args.cache }),
401
+ ...(args.skipCache && { skipCache: true }),
402
+ },
403
+ });
404
+ }
405
+ else {
406
+ return fetch(input, { ...rawInit, headers });
407
+ }
408
+ })().then(async (_response) => {
409
+ const response = _response;
316
410
  if (args.logging ?? this.gatewayLog)
317
411
  console.info(new Date().toISOString(), 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));
318
412
  // Inject it to have it available for retries
@@ -442,7 +536,7 @@ export class AiRawProviders extends AiBase {
442
536
  baseURL: new URL(['v1', this.config.gateway.accountId, this.gatewayName, 'google-ai-studio', 'v1beta'].join('/'), 'https://gateway.ai.cloudflare.com').toString(),
443
537
  apiKey: this.config.providers.googleAi.apiToken,
444
538
  headers: {
445
- 'cf-aig-authorization': `Bearer ${this.config.gateway.apiToken}`,
539
+ 'cf-aig-authorization': `Bearer ${'apiToken' in this.config.gateway ? this.config.gateway.apiToken : ''}`,
446
540
  'cf-aig-metadata': JSON.stringify({
447
541
  dataspaceId: (await BufferHelpers.uuidConvert(args.dataspaceId)).utf8,
448
542
  ...(args.groupBillingId && { groupBillingId: (await BufferHelpers.uuidConvert(args.groupBillingId)).utf8 }),
@@ -466,7 +560,54 @@ export class AiRawProviders extends AiBase {
466
560
  }
467
561
  if (args.logging ?? this.gatewayLog)
468
562
  console.info(new Date().toISOString(), this.chalk.rgb(...Helpers.uniqueIdColor(metadataHeader.idempotencyId))(`[${metadataHeader.idempotencyId}]`), this.chalk.magenta(rawInit?.method), this.chalk.magenta(new URL(new Request(input).url).pathname));
469
- return fetch(input, { ...rawInit, headers }).then(async (response) => {
563
+ return (() => {
564
+ if ('binding' in this.config.gateway) {
565
+ const fallbackedHeaders = {
566
+ ...Object.fromEntries(Array.from(headers.entries()).filter(([key]) => !key.toLowerCase().startsWith('cf-aig-'))),
567
+ 'cf-aig-metadata': JSON.stringify({
568
+ ...metadataHeader,
569
+ serverInfo: JSON.stringify({
570
+ name: 'googleai',
571
+ }),
572
+ }),
573
+ };
574
+ // Prevent double stringification
575
+ let fallbackedQuery;
576
+ try {
577
+ fallbackedQuery = JSON.parse(rawInit?.body);
578
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
579
+ }
580
+ catch (error) {
581
+ fallbackedQuery = rawInit?.body;
582
+ }
583
+ return this.config.gateway.binding.gateway(this.gatewayName).run({
584
+ provider: 'google-ai-studio',
585
+ endpoint: new URL(new Request(input).url).pathname.split('/').slice(5).join('/'),
586
+ headers: fallbackedHeaders,
587
+ query: fallbackedQuery,
588
+ }, {
589
+ extraHeaders: (() => {
590
+ // Prevent duplicates
591
+ headers.delete('cf-aig-authorization');
592
+ headers.delete('cf-aig-metadata');
593
+ headers.delete('cf-aig-cache-ttl');
594
+ headers.delete('cf-aig-skip-cache');
595
+ return headers;
596
+ })(),
597
+ gateway: {
598
+ id: this.gatewayName,
599
+ eventId: metadataHeader.idempotencyId,
600
+ metadata: metadataHeader,
601
+ ...(args.cache && { cacheTtl: typeof args.cache === 'boolean' ? (args.cache ? this.cacheTtl : 0) : args.cache }),
602
+ ...(args.skipCache && { skipCache: true }),
603
+ },
604
+ });
605
+ }
606
+ else {
607
+ return fetch(input, { ...rawInit, headers });
608
+ }
609
+ })().then(async (_response) => {
610
+ const response = _response;
470
611
  if (args.logging ?? this.gatewayLog)
471
612
  console.info(new Date().toISOString(), 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));
472
613
  // Inject it to have it available for retries
@@ -491,7 +632,7 @@ export class AiRawProviders extends AiBase {
491
632
  baseURL: new URL(['v1', this.config.gateway.accountId, this.gatewayName, 'workers-ai', 'v1'].join('/'), 'https://gateway.ai.cloudflare.com').toString(),
492
633
  apiKey: this.config.providers.workersAi.apiToken,
493
634
  headers: {
494
- 'cf-aig-authorization': `Bearer ${this.config.gateway.apiToken}`,
635
+ 'cf-aig-authorization': `Bearer ${'apiToken' in this.config.gateway ? this.config.gateway.apiToken : ''}`,
495
636
  'cf-aig-metadata': JSON.stringify({
496
637
  dataspaceId: (await BufferHelpers.uuidConvert(args.dataspaceId)).utf8,
497
638
  ...(args.groupBillingId && { groupBillingId: (await BufferHelpers.uuidConvert(args.groupBillingId)).utf8 }),
@@ -517,7 +658,54 @@ export class AiRawProviders extends AiBase {
517
658
  }
518
659
  if (args.logging ?? this.gatewayLog)
519
660
  console.info(new Date().toISOString(), this.chalk.rgb(...Helpers.uniqueIdColor(metadataHeader.idempotencyId))(`[${metadataHeader.idempotencyId}]`), this.chalk.magenta(rawInit?.method), this.chalk.magenta(new URL(new Request(input).url).pathname));
520
- return fetch(input, { ...rawInit, headers }).then(async (response) => {
661
+ return (() => {
662
+ if ('binding' in this.config.gateway) {
663
+ const fallbackedHeaders = {
664
+ ...Object.fromEntries(Array.from(headers.entries()).filter(([key]) => !key.toLowerCase().startsWith('cf-aig-'))),
665
+ 'cf-aig-metadata': JSON.stringify({
666
+ ...metadataHeader,
667
+ serverInfo: JSON.stringify({
668
+ name: 'cloudflare',
669
+ }),
670
+ }),
671
+ };
672
+ // Prevent double stringification
673
+ let fallbackedQuery;
674
+ try {
675
+ fallbackedQuery = JSON.parse(rawInit?.body);
676
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
677
+ }
678
+ catch (error) {
679
+ fallbackedQuery = rawInit?.body;
680
+ }
681
+ return this.config.gateway.binding.gateway(this.gatewayName).run({
682
+ provider: 'workers-ai',
683
+ endpoint: new URL(new Request(input).url).pathname.split('/').slice(5).join('/'),
684
+ headers: fallbackedHeaders,
685
+ query: fallbackedQuery,
686
+ }, {
687
+ extraHeaders: (() => {
688
+ // Prevent duplicates
689
+ headers.delete('cf-aig-authorization');
690
+ headers.delete('cf-aig-metadata');
691
+ headers.delete('cf-aig-cache-ttl');
692
+ headers.delete('cf-aig-skip-cache');
693
+ return headers;
694
+ })(),
695
+ gateway: {
696
+ id: this.gatewayName,
697
+ eventId: metadataHeader.idempotencyId,
698
+ metadata: metadataHeader,
699
+ ...(args.cache && { cacheTtl: typeof args.cache === 'boolean' ? (args.cache ? this.cacheTtl : 0) : args.cache }),
700
+ ...(args.skipCache && { skipCache: true }),
701
+ },
702
+ });
703
+ }
704
+ else {
705
+ return fetch(input, { ...rawInit, headers });
706
+ }
707
+ })().then(async (_response) => {
708
+ const response = _response;
521
709
  if (args.logging ?? this.gatewayLog)
522
710
  console.info(new Date().toISOString(), 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));
523
711
  // Inject it to have it available for retries
package/dist/types.d.mts CHANGED
@@ -7,13 +7,11 @@ import type haversine from 'haversine-distance';
7
7
  export interface AiConfig<C extends Pick<ExecutionContext | DurableObjectState, 'waitUntil'> = Pick<ExecutionContext | DurableObjectState, 'waitUntil'>> {
8
8
  gateway: {
9
9
  accountId: string;
10
+ } & ({
10
11
  apiToken: string;
11
- binding: never;
12
12
  } | {
13
- accountId: never;
14
- apiToken: never;
15
13
  binding: AiConfigWorkersAiBinding;
16
- };
14
+ });
17
15
  geoRouting?: {
18
16
  userCoordinate?: Coordinate;
19
17
  country?: IncomingRequestCfProperties['country'];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chainfuse/ai-tools",
3
- "version": "1.1.0",
3
+ "version": "1.1.1",
4
4
  "description": "",
5
5
  "author": "ChainFuse",
6
6
  "homepage": "https://github.com/ChainFuse/packages/tree/main/packages/ai-tools#readme",
@@ -48,23 +48,23 @@
48
48
  },
49
49
  "prettier": "@demosjarco/prettier-config",
50
50
  "dependencies": {
51
- "@ai-sdk/anthropic": "^2.0.53",
52
- "@ai-sdk/azure": "^2.0.82",
53
- "@ai-sdk/google": "^2.0.45",
51
+ "@ai-sdk/anthropic": "^2.0.56",
52
+ "@ai-sdk/azure": "^2.0.89",
53
+ "@ai-sdk/google": "^2.0.47",
54
54
  "@ai-sdk/openai": "^2.0.71",
55
- "@ai-sdk/openai-compatible": "^1.0.28",
55
+ "@ai-sdk/openai-compatible": "^1.0.29",
56
56
  "@ai-sdk/provider": "^2.0.0",
57
- "@chainfuse/helpers": "^4.2.8",
58
- "@chainfuse/types": "^4.2.1",
59
- "ai": "^5.0.108",
57
+ "@chainfuse/helpers": "^4.2.9",
58
+ "@chainfuse/types": "^4.2.2",
59
+ "ai": "^5.0.114",
60
60
  "chalk": "^5.6.2",
61
61
  "haversine-distance": "^1.2.4",
62
62
  "workers-ai-provider": "^2.0.0",
63
- "zod": "^4.1.13"
63
+ "zod": "^4.2.1"
64
64
  },
65
65
  "devDependencies": {
66
- "@cloudflare/workers-types": "^4.20251209.0",
67
- "openai": "^6.10.0"
66
+ "@cloudflare/workers-types": "^4.20251216.0",
67
+ "openai": "^6.14.0"
68
68
  },
69
- "gitHead": "7473e63a02b4d8150b0f4b118e75b87583c36323"
69
+ "gitHead": "90fd8902fc31eff523845e162610ddfbf2c710d8"
70
70
  }