@zodic/shared 0.0.189 → 0.0.191

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/app/api/index.ts CHANGED
@@ -46,51 +46,51 @@ export const Api = (env: BackendBindings) => ({
46
46
  messages,
47
47
  ...options,
48
48
  });
49
-
50
- if (response.choices[0].message.content) {
51
- return response.choices[0].message.content.trim();
52
- } else {
49
+
50
+ if (!response.choices[0].message.content) {
53
51
  throw new Error('Content is null');
54
52
  }
53
+
54
+ // ✅ Extract API usage data
55
+ const { usage } = response;
56
+ if (usage) {
57
+ console.log(`📊 API Usage: Tokens - ${usage.total_tokens}`);
58
+
59
+ if (env.KV_API_USAGE) {
60
+ // ✅ Check how many usage logs exist
61
+ const { keys } = await env.KV_API_USAGE.list({ prefix: 'usage:' });
62
+
63
+ if (keys.length < 10) {
64
+ // ✅ Store new entry ONLY if there are less than 10
65
+ const timestamp = new Date().toISOString();
66
+ const logEntry = {
67
+ model,
68
+ total_tokens: usage.total_tokens,
69
+ prompt_tokens: usage.prompt_tokens,
70
+ completion_tokens: usage.completion_tokens,
71
+ timestamp,
72
+ };
73
+
74
+ await env.KV_API_USAGE.put(`usage:${timestamp}`, JSON.stringify(logEntry), {
75
+ expirationTtl: 60 * 60 * 24 * 7, // ✅ Store for 7 days
76
+ });
77
+
78
+ console.log(`✅ Logged API usage (Entry ${keys.length + 1}/10)`);
79
+ } else {
80
+ console.log(`⏳ API usage logging skipped: 10 records already exist.`);
81
+ }
82
+ }
83
+ } else {
84
+ console.warn('⚠️ API Usage Data Missing');
85
+ }
86
+
87
+ return response.choices[0].message.content.trim();
88
+
55
89
  } catch (err: any) {
56
- console.error('Error calling DeepSeek API:', err.message);
90
+ console.error('Error calling Together API:', err.message);
57
91
  throw err;
58
92
  }
59
93
  },
60
-
61
- // batch: async (
62
- // batchItems: DeepSeekBatchInputItem[]
63
- // ): Promise<DeepSeekBatchResponse> => {
64
- // console.log('Preparing to send DeepSeek batch request.');
65
-
66
- // try {
67
- // const responses: DeepSeekBatchResponse = [];
68
-
69
- // for (const item of batchItems) {
70
- // const { messages, options } = item;
71
- // const response = await deepseek(env).chat.completions.create({
72
- // model: options?.model || 'deepseek-v3',
73
- // messages,
74
- // ...options,
75
- // });
76
-
77
- // if (response.choices[0].message.content) {
78
- // responses.push({
79
- // input: item,
80
- // output: response.choices[0].message.content.trim(),
81
- // });
82
- // } else {
83
- // console.error('Content is null');
84
- // }
85
- // }
86
-
87
- // console.log('Batch processing completed successfully.');
88
- // return responses;
89
- // } catch (error) {
90
- // console.error('Error in callDeepSeekBatch:', error);
91
- // throw error;
92
- // }
93
- // },
94
94
  },
95
95
  callDeepSeek: {
96
96
  single: async (
@@ -160,7 +160,7 @@ export class ConceptService {
160
160
  console.log(`✅ Stored basic info for ${conceptSlug}, combination: ${combinationString}.`);
161
161
 
162
162
  // ✅ **Throttling for next request**
163
- const delayMs = Math.floor(Math.random() * (500 - 200 + 1)) + 200; // 🔥 Random delay between 200-500ms
163
+ const delayMs = Math.floor(Math.random() * (500 - 200 + 1)) + 2000; // 🔥 Random delay between 200-500ms
164
164
  console.log(`⏳ Waiting ${delayMs}ms before processing next missing entry...`);
165
165
  await new Promise((resolve) => setTimeout(resolve, delayMs));
166
166
 
@@ -186,7 +186,7 @@ export class ConceptService {
186
186
  throw new Error(`Failed to generate basic info after ${maxAttempts} attempts.`);
187
187
  }
188
188
  console.log('🔁 Retrying...');
189
- await new Promise((resolve) => setTimeout(resolve, 500));
189
+ await new Promise((resolve) => setTimeout(resolve, 3000));
190
190
  }
191
191
  }
192
192
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zodic/shared",
3
- "version": "0.0.189",
3
+ "version": "0.0.191",
4
4
  "module": "index.ts",
5
5
  "type": "module",
6
6
  "publishConfig": {
@@ -24,6 +24,7 @@ export type CentralBindings = {
24
24
  KV_ASTRO: KVNamespace;
25
25
  KV_CONCEPT_FAILURES: KVNamespace;
26
26
  KV_CONCEPT_NAMES: KVNamespace;
27
+ KV_API_USAGE: KVNamespace;
27
28
  CONCEPT_GENERATION_QUEUE: Queue;
28
29
  CONCEPT_NAMES_DO: DurableObjectNamespace;
29
30
  CROWN_QUEUE: Queue;
@@ -128,6 +129,7 @@ export type BackendBindings = Env &
128
129
  | 'RING_QUEUE'
129
130
  | 'LANTERN_QUEUE'
130
131
  | 'ORB_QUEUE'
132
+ | 'KV_API_USAGE'
131
133
  >;
132
134
 
133
135
  export type BackendCtx = Context<
@@ -147,6 +149,7 @@ export type FrontendBindings = Pick<
147
149
  | 'PUBLIC_APP_NAME'
148
150
  | 'PUBLIC_APP_VERSION'
149
151
  | 'PUBLIC_ANALYTICS_ID'
152
+ | 'KV_API_USAGE'
150
153
  >;
151
154
 
152
155
  export type FrontCtx = Context<