@chainfuse/ai-tools 0.7.1 → 0.8.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.
@@ -9,15 +9,20 @@ export class AiRawProviders extends AiBase {
9
9
  const updateMetadata = NetHelpers.cfApi(this.config.gateway.apiToken).then((cf) => cf.aiGateway.logs.edit(this.config.environment, response.headers.get('cf-aig-log-id'), {
10
10
  account_id: this.config.gateway.accountId,
11
11
  metadata: {
12
- ...Object.entries(metadataHeader).reduce((acc, [key, value]) => {
12
+ ...Object.entries({
13
+ ...metadataHeader,
14
+ serverInfo: {
15
+ ...JSON.parse(metadataHeader.serverInfo),
16
+ timing: {
17
+ fromCache: response.headers.get('cf-aig-cache-status')?.toLowerCase() === 'hit',
18
+ totalRoundtripTime: performance.now() - startRoundTrip,
19
+ modelTime,
20
+ },
21
+ },
22
+ }).reduce((acc, [key, value]) => {
13
23
  acc[key] = typeof value === 'string' ? value : JSON.stringify(value);
14
24
  return acc;
15
25
  }, {}),
16
- timing: JSON.stringify({
17
- fromCache: response.headers.get('cf-aig-cache-status')?.toLowerCase() === 'hit',
18
- totalRoundtripTime: performance.now() - startRoundTrip,
19
- modelTime,
20
- }),
21
26
  },
22
27
  }));
23
28
  if (this.config.backgroundContext) {
@@ -35,21 +40,14 @@ export class AiRawProviders extends AiBase {
35
40
  headers: {
36
41
  'cf-aig-authorization': `Bearer ${this.config.gateway.apiToken}`,
37
42
  'cf-aig-metadata': JSON.stringify({
38
- dbInfo: JSON.stringify({
39
- messageId: (await BufferHelpers.uuidConvert(args.messageId)).utf8,
40
- dataspaceId: (await BufferHelpers.uuidConvert(args.dataspaceId)).utf8,
41
- }),
42
- executor: JSON.stringify(args.executor),
43
- // Generate incomplete id because we don't have the body to hash yet. Fill it in in the `fetch()`
44
- idempotencyId: args.idempotencyId ?? (await BufferHelpers.generateUuid).utf8.slice(0, 23),
43
+ dataspaceId: (await BufferHelpers.uuidConvert(args.dataspaceId)).utf8,
44
+ messageId: (await BufferHelpers.uuidConvert(args.messageId)).utf8,
45
45
  serverInfo: JSON.stringify({
46
46
  name: 'openai',
47
47
  }),
48
- /**
49
- * Blank at first, add after request finishes
50
- * CF AI Gateway allows only editing existing metadata not creating new ones after the request is made
51
- */
52
- timing: JSON.stringify({}),
48
+ // Generate incomplete id because we don't have the body to hash yet. Fill it in in the `fetch()`
49
+ idempotencyId: args.idempotencyId ?? (await BufferHelpers.generateUuid).utf8.slice(0, 23),
50
+ executor: JSON.stringify(args.executor),
53
51
  }),
54
52
  ...(args.cache && { 'cf-aig-cache-ttl': (typeof args.cache === 'boolean' ? (args.cache ? this.cacheTtl : 0) : args.cache).toString() }),
55
53
  ...(args.skipCache && { 'cf-aig-skip-cache': 'true' }),
@@ -96,13 +94,8 @@ export class AiRawProviders extends AiBase {
96
94
  headers: {
97
95
  'cf-aig-authorization': `Bearer ${this.config.gateway.apiToken}`,
98
96
  'cf-aig-metadata': JSON.stringify({
99
- dbInfo: JSON.stringify({
100
- messageId: (await BufferHelpers.uuidConvert(args.messageId)).utf8,
101
- dataspaceId: (await BufferHelpers.uuidConvert(args.dataspaceId)).utf8,
102
- }),
103
- executor: JSON.stringify(args.executor),
104
- // Generate incomplete id because we don't have the body to hash yet. Fill it in in the `fetch()`
105
- idempotencyId: args.idempotencyId ?? (await BufferHelpers.generateUuid).utf8.slice(0, 23),
97
+ dataspaceId: (await BufferHelpers.uuidConvert(args.dataspaceId)).utf8,
98
+ messageId: (await BufferHelpers.uuidConvert(args.messageId)).utf8,
106
99
  serverInfo: JSON.stringify({
107
100
  name: `azure-${server.id}`,
108
101
  distance: haversine({
@@ -110,11 +103,9 @@ export class AiRawProviders extends AiBase {
110
103
  lon: Helpers.precisionFloat(this.config.geoRouting?.userCoordinate?.lon ?? '0'),
111
104
  }, server.coordinate),
112
105
  }),
113
- /**
114
- * Blank at first, add after request finishes
115
- * CF AI Gateway allows only editing existing metadata not creating new ones after the request is made
116
- */
117
- timing: JSON.stringify({}),
106
+ // Generate incomplete id because we don't have the body to hash yet. Fill it in in the `fetch()`
107
+ idempotencyId: args.idempotencyId ?? (await BufferHelpers.generateUuid).utf8.slice(0, 23),
108
+ executor: JSON.stringify(args.executor),
118
109
  }),
119
110
  ...(args.cache && { 'cf-aig-cache-ttl': (typeof args.cache === 'boolean' ? (args.cache ? this.cacheTtl : 0) : args.cache).toString() }),
120
111
  ...(args.skipCache && { 'cf-aig-skip-cache': 'true' }),
@@ -155,21 +146,14 @@ export class AiRawProviders extends AiBase {
155
146
  headers: {
156
147
  'cf-aig-authorization': `Bearer ${this.config.gateway.apiToken}`,
157
148
  'cf-aig-metadata': JSON.stringify({
158
- dbInfo: JSON.stringify({
159
- messageId: (await BufferHelpers.uuidConvert(args.messageId)).utf8,
160
- dataspaceId: (await BufferHelpers.uuidConvert(args.dataspaceId)).utf8,
161
- }),
162
- executor: JSON.stringify(args.executor),
163
- // Generate incomplete id because we don't have the body to hash yet. Fill it in in the `fetch()`
164
- idempotencyId: args.idempotencyId ?? (await BufferHelpers.generateUuid).utf8.slice(0, 23),
149
+ dataspaceId: (await BufferHelpers.uuidConvert(args.dataspaceId)).utf8,
150
+ messageId: (await BufferHelpers.uuidConvert(args.messageId)).utf8,
165
151
  serverInfo: JSON.stringify({
166
152
  name: 'anthropic',
167
153
  }),
168
- /**
169
- * Blank at first, add after request finishes
170
- * CF AI Gateway allows only editing existing metadata not creating new ones after the request is made
171
- */
172
- timing: JSON.stringify({}),
154
+ // Generate incomplete id because we don't have the body to hash yet. Fill it in in the `fetch()`
155
+ idempotencyId: args.idempotencyId ?? (await BufferHelpers.generateUuid).utf8.slice(0, 23),
156
+ executor: JSON.stringify(args.executor),
173
157
  }),
174
158
  ...(args.cache && { 'cf-aig-cache-ttl': (typeof args.cache === 'boolean' ? (args.cache ? this.cacheTtl : 0) : args.cache).toString() }),
175
159
  ...(args.skipCache && { 'cf-aig-skip-cache': 'true' }),
@@ -319,21 +303,14 @@ export class AiRawProviders extends AiBase {
319
303
  headers: {
320
304
  'cf-aig-authorization': `Bearer ${this.config.gateway.apiToken}`,
321
305
  'cf-aig-metadata': JSON.stringify({
322
- dbInfo: JSON.stringify({
323
- messageId: (await BufferHelpers.uuidConvert(args.messageId)).utf8,
324
- dataspaceId: (await BufferHelpers.uuidConvert(args.dataspaceId)).utf8,
325
- }),
326
- executor: JSON.stringify(args.executor),
327
- // Generate incomplete id because we don't have the body to hash yet. Fill it in in the `fetch()`
328
- idempotencyId: args.idempotencyId ?? (await BufferHelpers.generateUuid).utf8.slice(0, 23),
306
+ dataspaceId: (await BufferHelpers.uuidConvert(args.dataspaceId)).utf8,
307
+ messageId: (await BufferHelpers.uuidConvert(args.messageId)).utf8,
329
308
  serverInfo: JSON.stringify({
330
309
  name: 'googleai',
331
310
  }),
332
- /**
333
- * Blank at first, add after request finishes
334
- * CF AI Gateway allows only editing existing metadata not creating new ones after the request is made
335
- */
336
- timing: JSON.stringify({}),
311
+ // Generate incomplete id because we don't have the body to hash yet. Fill it in in the `fetch()`
312
+ idempotencyId: args.idempotencyId ?? (await BufferHelpers.generateUuid).utf8.slice(0, 23),
313
+ executor: JSON.stringify(args.executor),
337
314
  }),
338
315
  ...(args.cache && { 'cf-aig-cache-ttl': (typeof args.cache === 'boolean' ? (args.cache ? this.cacheTtl : 0) : args.cache).toString() }),
339
316
  ...(args.skipCache && { 'cf-aig-skip-cache': 'true' }),
@@ -374,21 +351,14 @@ export class AiRawProviders extends AiBase {
374
351
  headers: {
375
352
  'cf-aig-authorization': `Bearer ${this.config.gateway.apiToken}`,
376
353
  'cf-aig-metadata': JSON.stringify({
377
- dbInfo: JSON.stringify({
378
- messageId: (await BufferHelpers.uuidConvert(args.messageId)).utf8,
379
- dataspaceId: (await BufferHelpers.uuidConvert(args.dataspaceId)).utf8,
380
- }),
381
- executor: JSON.stringify(args.executor),
382
- // Generate incomplete id because we don't have the body to hash yet. Fill it in in the `fetch()`
383
- idempotencyId: args.idempotencyId ?? (await BufferHelpers.generateUuid).utf8.slice(0, 23),
354
+ dataspaceId: (await BufferHelpers.uuidConvert(args.dataspaceId)).utf8,
355
+ messageId: (await BufferHelpers.uuidConvert(args.messageId)).utf8,
384
356
  serverInfo: JSON.stringify({
385
357
  name: 'cloudflare',
386
358
  }),
387
- /**
388
- * Blank at first, add after request finishes
389
- * CF AI Gateway allows only editing existing metadata not creating new ones after the request is made
390
- */
391
- timing: JSON.stringify({}),
359
+ // Generate incomplete id because we don't have the body to hash yet. Fill it in in the `fetch()`
360
+ idempotencyId: args.idempotencyId ?? (await BufferHelpers.generateUuid).utf8.slice(0, 23),
361
+ executor: JSON.stringify(args.executor),
392
362
  }),
393
363
  ...(args.cache && { 'cf-aig-cache-ttl': (typeof args.cache === 'boolean' ? (args.cache ? this.cacheTtl : 0) : args.cache).toString() }),
394
364
  ...(args.skipCache && { 'cf-aig-skip-cache': 'true' }),
@@ -431,21 +401,14 @@ export class AiRawProviders extends AiBase {
431
401
  ...(args.cache && { cacheTtl: typeof args.cache === 'boolean' ? (args.cache ? this.cacheTtl : 0) : args.cache }),
432
402
  ...(args.skipCache && { skipCache: true }),
433
403
  metadata: {
434
- dbInfo: JSON.stringify({
435
- messageId: (await BufferHelpers.uuidConvert(args.messageId)).utf8,
436
- dataspaceId: (await BufferHelpers.uuidConvert(args.dataspaceId)).utf8,
437
- }),
438
- executor: JSON.stringify(args.executor),
439
- // Generate incomplete id because we don't have the body to hash yet. Fill it in in the `fetch()`
440
- idempotencyId: args.idempotencyId ?? (await BufferHelpers.generateUuid).utf8.slice(0, 23),
404
+ dataspaceId: (await BufferHelpers.uuidConvert(args.dataspaceId)).utf8,
405
+ messageId: (await BufferHelpers.uuidConvert(args.messageId)).utf8,
441
406
  serverInfo: JSON.stringify({
442
407
  name: 'cloudflare',
443
408
  }),
444
- /**
445
- * Blank at first, add after request finishes
446
- * CF AI Gateway allows only editing existing metadata not creating new ones after the request is made
447
- */
448
- timing: JSON.stringify({}),
409
+ // Generate incomplete id because we don't have the body to hash yet. Fill it in in the `fetch()`
410
+ idempotencyId: args.idempotencyId ?? (await BufferHelpers.generateUuid).utf8.slice(0, 23),
411
+ executor: JSON.stringify(args.executor),
449
412
  },
450
413
  },
451
414
  }));
package/dist/types.d.mts CHANGED
@@ -90,9 +90,10 @@ export interface AiRequestConfig {
90
90
  */
91
91
  skipCache?: boolean;
92
92
  }
93
- export interface AiRequestMetadataDbInfo {
94
- dataspaceId: AiRequestConfig['dataspaceId'];
95
- messageId: AiRequestConfig['dataspaceId'];
93
+ export interface AiRequestMetadataTiming {
94
+ modelTime?: number;
95
+ fromCache: boolean;
96
+ totalRoundtripTime: number;
96
97
  }
97
98
  export interface AiRequestMetadataServerInfo {
98
99
  name: 'anthropic' | 'cloudflare' | 'googleai' | 'openai';
@@ -104,17 +105,14 @@ export interface AiRequestMetadataServerInfoWithLocation {
104
105
  */
105
106
  distance: ReturnType<typeof haversine>;
106
107
  }
107
- export interface AiRequestMetadataTiming {
108
- modelTime?: number;
109
- fromCache: boolean;
110
- totalRoundtripTime: number;
111
- }
112
108
  export interface AiRequestMetadata {
113
- dbInfo: AiRequestMetadataDbInfo;
114
- serverInfo: AiRequestMetadataServerInfo | AiRequestMetadataServerInfoWithLocation;
109
+ dataspaceId: AiRequestConfig['dataspaceId'];
110
+ messageId: AiRequestConfig['messageId'];
111
+ serverInfo: (AiRequestMetadataServerInfo | AiRequestMetadataServerInfoWithLocation) & {
112
+ timing?: AiRequestMetadataTiming;
113
+ };
115
114
  idempotencyId: AiRequestIdempotencyId;
116
115
  executor: AiRequestExecutor;
117
- timing: AiRequestMetadataTiming;
118
116
  }
119
117
  export type AiRequestMetadataStringified = Record<keyof AiRequestMetadata, string>;
120
118
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chainfuse/ai-tools",
3
- "version": "0.7.1",
3
+ "version": "0.8.0",
4
4
  "description": "",
5
5
  "author": "ChainFuse",
6
6
  "homepage": "https://github.com/ChainFuse/packages/tree/main/packages/ai-tools#readme",
@@ -64,5 +64,5 @@
64
64
  "@cloudflare/workers-types": "^4.20250319.0",
65
65
  "openai": "^4.89.0"
66
66
  },
67
- "gitHead": "afa550b265b1815397b3c8f81939c601ff09ae7e"
67
+ "gitHead": "a48db66b6313a868ab0affa50a934ad13f6bf391"
68
68
  }