@digipair/skill-logger 0.89.0 → 0.91.0-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/.swcrc ADDED
@@ -0,0 +1,28 @@
1
+ {
2
+ "jsc": {
3
+ "target": "es2017",
4
+ "parser": {
5
+ "syntax": "typescript",
6
+ "decorators": true,
7
+ "dynamicImport": true
8
+ },
9
+ "transform": {
10
+ "decoratorMetadata": true,
11
+ "legacyDecorator": true
12
+ },
13
+ "keepClassNames": true,
14
+ "externalHelpers": true,
15
+ "loose": true
16
+ },
17
+ "module": {
18
+ "type": "es6"
19
+ },
20
+ "sourceMaps": true,
21
+ "exclude": [
22
+ "jest.config.ts",
23
+ ".*\\.spec.tsx?$",
24
+ ".*\\.test.tsx?$",
25
+ "./src/jest-setup.ts$",
26
+ "./**/jest-setup.ts$"
27
+ ]
28
+ }
package/README.md ADDED
@@ -0,0 +1,7 @@
1
+ # mylib
2
+
3
+ This library was generated with [Nx](https://nx.dev).
4
+
5
+ ## Building
6
+
7
+ Run `nx build mylib` to build the library.
@@ -0,0 +1,22 @@
1
+ import baseConfig from '../../eslint.config.mjs';
2
+
3
+ export default [
4
+ ...baseConfig,
5
+ {
6
+ files: ['**/*.json'],
7
+ rules: {
8
+ '@nx/dependency-checks': [
9
+ 'error',
10
+ {
11
+ ignoredFiles: [
12
+ '{projectRoot}/eslint.config.{js,cjs,mjs}',
13
+ '{projectRoot}/rollup.config.{js,ts,mjs,mts,cjs,cts}',
14
+ ],
15
+ },
16
+ ],
17
+ },
18
+ languageOptions: {
19
+ parser: await import('jsonc-eslint-parser'),
20
+ },
21
+ },
22
+ ];
package/package.json CHANGED
@@ -1,12 +1,28 @@
1
1
  {
2
2
  "name": "@digipair/skill-logger",
3
- "version": "0.89.0",
3
+ "version": "0.91.0-0",
4
+ "type": "module",
5
+ "main": "dist/libs/skill-logger/index.cjs.js",
6
+ "module": "dist/libs/skill-logger/index.esm.js",
7
+ "types": "dist/libs/skill-logger/index.esm.d.ts",
8
+ "exports": {
9
+ "./package.json": "./libs/skill-logger/package.json",
10
+ ".": {
11
+ "development": "./dist/libs/skill-logger/src/index.ts",
12
+ "types": "./dist/libs/skill-logger/index.esm.d.ts",
13
+ "import": "./dist/libs/skill-logger/index.esm.js",
14
+ "default": "./dist/libs/skill-logger/index.cjs.js"
15
+ }
16
+ },
4
17
  "keywords": [
5
18
  "digipair",
6
- "service",
7
- "util"
19
+ "util",
20
+ "service"
8
21
  ],
9
- "dependencies": {},
10
- "main": "./index.cjs.js",
11
- "module": "./index.esm.js"
12
- }
22
+ "nx": {
23
+ "name": "skill-logger"
24
+ },
25
+ "dependencies": {
26
+ "@digipair/engine": "0.91.0-0"
27
+ }
28
+ }
@@ -0,0 +1,28 @@
1
+ const { withNx } = require('@nx/rollup/with-nx');
2
+
3
+ module.exports = withNx(
4
+ {
5
+ main: 'libs/skill-logger/src/index.ts',
6
+ outputPath: 'dist/libs/skill-logger',
7
+ tsConfig: 'libs/skill-logger/tsconfig.lib.json',
8
+ compiler: 'swc',
9
+ format: ['esm', "cjs"],
10
+ assets: [
11
+ {
12
+ input: 'libs/skill-logger/',
13
+ glob: 'package.json',
14
+ output: '.'
15
+ },
16
+ {
17
+ input: 'libs/skill-logger/src/',
18
+ glob: '*.json',
19
+ output: '.'
20
+ }
21
+ ]
22
+ },
23
+ {
24
+ // Provide additional rollup configuration here. See: https://rollupjs.org/configuration-options
25
+ // e.g.
26
+ // output: { sourcemap: true },
27
+ }
28
+ );
@@ -0,0 +1 @@
1
+ declare module 'handlebars/dist/handlebars.min.js';
@@ -0,0 +1,7 @@
1
+ import { skillLogger } from './skill-logger';
2
+
3
+ describe('skillLogger', () => {
4
+ it('should work', () => {
5
+ expect(skillLogger()).toEqual('skill-logger');
6
+ });
7
+ });
@@ -0,0 +1,201 @@
1
+ import { PinsSettings, config } from '@digipair/engine';
2
+ import { promises } from 'fs';
3
+
4
+ class LoggerService {
5
+ async initialize(path = process.env['DIGIPAIR_LOGS_PATH'] ?? './factory/logs') {
6
+ // create logs directory if it doesn't exist
7
+ await promises.mkdir(`${path}/factory`, { recursive: true });
8
+
9
+ // create consumption-daily directory if it doesn't exist
10
+ await promises.mkdir(`${path}/consumption-daily`, { recursive: true });
11
+
12
+ // create consumption-monthly directory if it doesn't exist
13
+ await promises.mkdir(`${path}/consumption-monthly`, { recursive: true });
14
+ }
15
+
16
+ async addLog(context: any, type: string, message: string, data?: any) {
17
+ const DIGIPAIR_LOGS_PATH =
18
+ context.privates.DIGIPAIR_LOGS_PATH ?? process.env['DIGIPAIR_LOGS_PATH'] ?? './factory/logs';
19
+ const current = new Date();
20
+ const line = {
21
+ date: current.getTime(),
22
+ digipair: context.request.digipair,
23
+ reasoning: context.request.reasoning,
24
+ type,
25
+ message,
26
+ };
27
+
28
+ config.log(type, context.__PATH__, message, context, data);
29
+
30
+ await promises.appendFile(
31
+ `${DIGIPAIR_LOGS_PATH}/factory/${current.toISOString().split('T')[0]}.jsonl`,
32
+ '\n' + JSON.stringify(line),
33
+ 'utf8',
34
+ );
35
+ }
36
+
37
+ async addConsumption(
38
+ context: any,
39
+ service: string,
40
+ model: string,
41
+ promptTokens: number,
42
+ completionTokens: number,
43
+ ) {
44
+ const DIGIPAIR_LOGS_PATH =
45
+ context.privates.DIGIPAIR_LOGS_PATH ?? process.env['DIGIPAIR_LOGS_PATH'] ?? './factory/logs';
46
+ const current = new Date();
47
+ const line = {
48
+ date: current.getTime(),
49
+ digipair: context.request.digipair,
50
+ reasoning: context.request.reasoning,
51
+ service,
52
+ model,
53
+ promptTokens,
54
+ completionTokens,
55
+ };
56
+
57
+ await promises.appendFile(
58
+ `${DIGIPAIR_LOGS_PATH}/consumption-daily/${current.toISOString().split('T')[0]}.jsonl`,
59
+ '\n' + JSON.stringify(line),
60
+ 'utf8',
61
+ );
62
+ }
63
+
64
+ async computeDailyConsumption(params: any, _pinsSettingsList: PinsSettings[], context: any) {
65
+ const {
66
+ date,
67
+ path = context.privates.DIGIPAIR_LOGS_PATH ??
68
+ process.env['DIGIPAIR_LOGS_PATH'] ??
69
+ './factory/logs',
70
+ } = params;
71
+ let result: any;
72
+
73
+ try {
74
+ const text = await promises.readFile(`${path}/consumption-daily/${date}.jsonl`, 'utf8');
75
+ const lines = text.split('\n').filter(line => line !== '');
76
+ result = lines.map((line: string) => JSON.parse(line));
77
+ } catch (error) {
78
+ result = [];
79
+ }
80
+
81
+ const dayLines = result.reduce((acc: any, curr: any) => {
82
+ let line = acc.find(
83
+ (line: any) =>
84
+ line.digipair === curr.digipair &&
85
+ line.reasoning === curr.reasoning &&
86
+ line.service === curr.service &&
87
+ line.model === curr.model,
88
+ );
89
+
90
+ if (!line) {
91
+ line = {
92
+ date,
93
+ digipair: curr.digipair,
94
+ reasoning: curr.reasoning,
95
+ service: curr.service,
96
+ model: curr.model,
97
+ promptTokens: 0,
98
+ completionTokens: 0,
99
+ };
100
+ acc.push(line);
101
+ }
102
+
103
+ line.promptTokens += curr.promptTokens;
104
+ line.completionTokens += curr.completionTokens;
105
+
106
+ return acc;
107
+ }, []);
108
+
109
+ await promises.appendFile(
110
+ `${path}/consumption-monthly/${date.substring(0, 7)}.jsonl`,
111
+ '\n' + dayLines.map((line: any) => JSON.stringify(line)).join('\n'),
112
+ 'utf8',
113
+ );
114
+ }
115
+
116
+ async read(params: any, _pinsSettingsList: PinsSettings[], context: any) {
117
+ const {
118
+ date,
119
+ type = 'factory', // consumption-daily, consumption-monthly, factory
120
+ path = context.privates.DIGIPAIR_LOGS_PATH ??
121
+ process.env['DIGIPAIR_LOGS_PATH'] ??
122
+ './factory/logs',
123
+ } = params;
124
+ let result: any[];
125
+
126
+ try {
127
+ const text = await promises.readFile(`${path}/${type}/${date}.jsonl`, 'utf8');
128
+ const lines = text.split('\n').filter(line => line !== '');
129
+ result = lines.map((line: string) => JSON.parse(line));
130
+ } catch (error) {
131
+ result = [];
132
+ }
133
+
134
+ return result;
135
+ }
136
+
137
+ async list(params: any, _pinsSettingsList: PinsSettings[], context: any) {
138
+ const {
139
+ type = 'factory', // consumption-daily, consumption-monthly, factory
140
+ path = context.privates.DIGIPAIR_LOGS_PATH ??
141
+ process.env['DIGIPAIR_LOGS_PATH'] ??
142
+ './factory/logs',
143
+ } = params;
144
+
145
+ const files = (await promises.readdir(`${path}/${type}`))
146
+ .filter(file => /\.jsonl$/g.test(file))
147
+ .map(file => file.split('.')[0]);
148
+
149
+ return files;
150
+ }
151
+
152
+ async cleaning(params: any, _pinsSettingsList: PinsSettings[], context: any) {
153
+ const {
154
+ type = 'factory', // consumption-daily, consumption-monthly, factory
155
+ to,
156
+ path = context.privates.DIGIPAIR_LOGS_PATH ??
157
+ process.env['DIGIPAIR_LOGS_PATH'] ??
158
+ './factory/logs',
159
+ } = params;
160
+ const files = await promises.readdir(`${path}/${type}`);
161
+
162
+ for (const file of files) {
163
+ const stats = await promises.stat(`${path}/${type}/${file}`);
164
+ if (stats.mtime.getTime() < to) {
165
+ promises.unlink(`${path}/${type}/${file}`);
166
+ }
167
+ }
168
+ }
169
+ }
170
+
171
+ export const initialize = (path?: string) => new LoggerService().initialize(path);
172
+
173
+ export const addLog = (context: any, type: string, message: string) =>
174
+ new LoggerService().addLog(context, type, message);
175
+
176
+ export const addConsumption = (
177
+ context: any,
178
+ service: string,
179
+ model: string,
180
+ promptTokens: number,
181
+ completionTokens: number,
182
+ ) => new LoggerService().addConsumption(context, service, model, promptTokens, completionTokens);
183
+
184
+ // ----------------------------
185
+ // Methods for the skill
186
+ // ----------------------------
187
+
188
+ export const computeDailyConsumption = (
189
+ params: any,
190
+ pinsSettingsList: PinsSettings[],
191
+ context: any,
192
+ ) => new LoggerService().computeDailyConsumption(params, pinsSettingsList, context);
193
+
194
+ export const read = (params: any, pinsSettingsList: PinsSettings[], context: any) =>
195
+ new LoggerService().read(params, pinsSettingsList, context);
196
+
197
+ export const list = (params: any, pinsSettingsList: PinsSettings[], context: any) =>
198
+ new LoggerService().list(params, pinsSettingsList, context);
199
+
200
+ export const cleaning = (params: any, pinsSettingsList: PinsSettings[], context: any) =>
201
+ new LoggerService().cleaning(params, pinsSettingsList, context);
package/tsconfig.json ADDED
@@ -0,0 +1,13 @@
1
+ {
2
+ "extends": "../../tsconfig.base.json",
3
+ "files": [],
4
+ "include": [],
5
+ "references": [
6
+ {
7
+ "path": "../engine"
8
+ },
9
+ {
10
+ "path": "./tsconfig.lib.json"
11
+ }
12
+ ]
13
+ }
@@ -0,0 +1,19 @@
1
+ {
2
+ "extends": "../../tsconfig.base.json",
3
+ "compilerOptions": {
4
+ "rootDir": "src",
5
+ "outDir": "dist",
6
+ "tsBuildInfoFile": "dist/tsconfig.lib.tsbuildinfo",
7
+ "emitDeclarationOnly": true,
8
+ "module": "esnext",
9
+ "moduleResolution": "node",
10
+ "forceConsistentCasingInFileNames": true,
11
+ "types": ["node"]
12
+ },
13
+ "include": ["src/**/*.ts"],
14
+ "references": [
15
+ {
16
+ "path": "../engine/tsconfig.lib.json"
17
+ }
18
+ ]
19
+ }
package/index.cjs.d.ts DELETED
@@ -1 +0,0 @@
1
- export * from "./src/index";