@zodic/shared 0.0.188 → 0.0.190

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 (
@@ -85,10 +85,22 @@ export class ConceptNameDO {
85
85
  ratePerMinute: number;
86
86
  estimatedTimeMinutes: number | 'N/A';
87
87
  }> {
88
+ console.log(`📡 Fetching progress...`);
89
+
90
+ // 🔥 Fetch latest names directly from storage
91
+ const storedNames = await this.state.storage.get<{ 'en-us': string[]; 'pt-br': string[] }>('names');
92
+ if (storedNames) {
93
+ this.names = storedNames;
94
+ } else {
95
+ this.names = { 'en-us': [], 'pt-br': [] };
96
+ }
97
+
88
98
  await this.loadCount();
89
99
  await this.loadTimestamps();
90
100
  await this.loadLastIndex(); // ✅ Ensure latest lastIndex is fetched
91
101
 
102
+ console.log(`🔍 Current stored names (DO): ${JSON.stringify(this.names)}`);
103
+
92
104
  const now = Date.now();
93
105
  const oneMinuteAgo = now - 60 * 1000;
94
106
 
@@ -96,7 +108,11 @@ export class ConceptNameDO {
96
108
  const ratePerMinute = recentTimestamps.length;
97
109
 
98
110
  // 🔥 Only count the `en-us` names to reflect progress properly
99
- const enUsCount = this.names['en-us'].length;
111
+ const enUsCount = Array.isArray(this.names['en-us']) ? this.names['en-us'].length : 0;
112
+
113
+ console.log(
114
+ `🛠️ Debugging Progress: count=${this.count}, en-us=${enUsCount}, lastIndex=${this.lastIndex}`
115
+ );
100
116
 
101
117
  const remainingNames = ConceptNameDO.TOTAL_NAMES - enUsCount;
102
118
  const estimatedTimeMinutes =
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zodic/shared",
3
- "version": "0.0.188",
3
+ "version": "0.0.190",
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<