@adaptic/lumic-utils 1.0.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/README.md +637 -0
- package/dist/index-CcXQeJz8.js +1477 -0
- package/dist/index-CcXQeJz8.js.map +1 -0
- package/dist/index-CcgbRft2.js +1479 -0
- package/dist/index-CcgbRft2.js.map +1 -0
- package/dist/index.cjs +11420 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.mjs +11400 -0
- package/dist/index.mjs.map +1 -0
- package/dist/test.cjs +1119 -0
- package/dist/test.cjs.map +1 -0
- package/dist/test.mjs +1117 -0
- package/dist/test.mjs.map +1 -0
- package/dist/types/aws-types.ts +64 -0
- package/dist/types/functions/aws-lambda.d.ts +17 -0
- package/dist/types/functions/aws-s3-utils.d.ts +118 -0
- package/dist/types/functions/get-weather.d.ts +10 -0
- package/dist/types/functions/google-sheets.d.ts +40 -0
- package/dist/types/functions/json-llm-tools.d.ts +12 -0
- package/dist/types/functions/json-tools.d.ts +16 -0
- package/dist/types/functions/llm-call.d.ts +29 -0
- package/dist/types/functions/llm-config.d.ts +34 -0
- package/dist/types/functions/llm-deepseek.d.ts +11 -0
- package/dist/types/functions/llm-images.d.ts +48 -0
- package/dist/types/functions/llm-openai.d.ts +55 -0
- package/dist/types/functions/llm-utils.d.ts +15 -0
- package/dist/types/functions/pdf-create.d.ts +2 -0
- package/dist/types/functions/performance.d.ts +72 -0
- package/dist/types/functions/perplexity-api.d.ts +1 -0
- package/dist/types/functions/slack-utils.d.ts +11 -0
- package/dist/types/functions/utils.d.ts +22 -0
- package/dist/types/functions/zip-utils.d.ts +38 -0
- package/dist/types/google-types.ts +48 -0
- package/dist/types/index.d.ts +72 -0
- package/dist/types/index.ts +57 -0
- package/dist/types/llm-tools.d.ts +2 -0
- package/dist/types/openai-types.ts +92 -0
- package/dist/types/test-data.d.ts +14 -0
- package/dist/types/test-llm-functions-archive.d.ts +1 -0
- package/dist/types/test.d.ts +1 -0
- package/dist/types/types/aws-types.d.ts +53 -0
- package/dist/types/types/google-types.d.ts +41 -0
- package/dist/types/types/index.d.ts +44 -0
- package/dist/types/types/openai-types.d.ts +81 -0
- package/dist/types/utils/aws-initialise.d.ts +21 -0
- package/dist/types/utils/aws-s3-utils.d.ts +32 -0
- package/dist/types/utils/config.d.ts +11 -0
- package/package.json +56 -0
package/README.md
ADDED
|
@@ -0,0 +1,637 @@
|
|
|
1
|
+
# lumic-utility-functions
|
|
2
|
+
|
|
3
|
+
lumic-utility-functions is a versatile npm package that provides a comprehensive collection of utility functions for Node.js projects. It simplifies common tasks including:
|
|
4
|
+
- Making LLM API calls (unified interface for OpenAI models)
|
|
5
|
+
- Managing AWS services (Lambda, S3)
|
|
6
|
+
- Sending Slack messages
|
|
7
|
+
- Handling file operations (ZIP files, S3 storage)
|
|
8
|
+
- Working with JSON data
|
|
9
|
+
- Interacting with Google Sheets
|
|
10
|
+
|
|
11
|
+
## Installation
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
npm install lumic-utility-functions
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Usage
|
|
18
|
+
|
|
19
|
+
Import the lumic namespace:
|
|
20
|
+
```javascript
|
|
21
|
+
import { lumic } from "lumic-utility-functions";
|
|
22
|
+
|
|
23
|
+
// Access functions through their namespaces
|
|
24
|
+
const openaiResult = await lumic.llm.call("Your prompt", "text", { model: "GPT-5-mini" }); // OpenAI model
|
|
25
|
+
const deepseekResult = await lumic.llm.seek("Your prompt", "json"); // Deepseek model
|
|
26
|
+
const s3Result = await lumic.s3.upload("bucket", "file.txt");
|
|
27
|
+
const slackResult = await lumic.slack.sendMessage("#channel", "message");
|
|
28
|
+
const fixedJson = lumic.json.fix(brokenJsonString);
|
|
29
|
+
const gsResult = await lumic.gs.addRow({ spreadsheetId: "id", sheetName: "Sheet1" }, ["Value1", "Value2"]);
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Required Environment Variables
|
|
33
|
+
|
|
34
|
+
The following environment variables are recommended for full functionality:
|
|
35
|
+
|
|
36
|
+
* `MY_AWS_ACCESS_KEY_ID` - For AWS services
|
|
37
|
+
* `MY_AWS_REGION` - For AWS services
|
|
38
|
+
* `MY_AWS_SECRET_ACCESS_KEY` - For AWS services
|
|
39
|
+
* `OPENAI_API_KEY` - For OpenAI models
|
|
40
|
+
* `DEEPSEEK_API_KEY` - For Deepseek models
|
|
41
|
+
* `SLACK_BOT_TOKEN` - For Slack messaging
|
|
42
|
+
* `GOOGLE_SHEETS_CLIENT_EMAIL` - For Google Sheets service account
|
|
43
|
+
* `GOOGLE_SHEETS_PRIVATE_KEY` - For Google Sheets service account
|
|
44
|
+
* `AI_MODEL` - Default model for LLM calls (optional, defaults to 'GPT-5-mini')
|
|
45
|
+
|
|
46
|
+
Most functions also accept explicit authentication parameters if you prefer not to use environment variables.
|
|
47
|
+
|
|
48
|
+
### Table of Contents
|
|
49
|
+
- [Installation](#installation)
|
|
50
|
+
- [Usage](#usage)
|
|
51
|
+
- [LLM Interface](#llm-interface)
|
|
52
|
+
- [sendMessageToSlack](#sendmessagetoslack)
|
|
53
|
+
- [callPerplexityAPI](#callperplexityapi)
|
|
54
|
+
- [invokeLambdaFunction](#invokelambdafunction)
|
|
55
|
+
- [Zip File Utilities](#zip-file-utilities)
|
|
56
|
+
- [AWS S3 Utilities](#aws-s3-utilities)
|
|
57
|
+
- [JSON Utilities](#json-utilities)
|
|
58
|
+
- [Google Sheets Utilities](#google-sheets-utilities)
|
|
59
|
+
- [Performance Timer](#performance-timer)
|
|
60
|
+
|
|
61
|
+
## LLM Interface
|
|
62
|
+
|
|
63
|
+
The package provides a unified interface for interacting with various LLM providers through the `lumic.llm.call` function.
|
|
64
|
+
|
|
65
|
+
### Supported Models
|
|
66
|
+
|
|
67
|
+
#### OpenAI Models
|
|
68
|
+
- `GPT-5-mini`: Optimized GPT-4 model for general use (default)
|
|
69
|
+
- `GPT-5`: Full GPT-4 model with enhanced capabilities
|
|
70
|
+
- `o1-mini`: Compact O1 model
|
|
71
|
+
- `o1`: Full O1 model
|
|
72
|
+
- `o3-mini`: Compact O3 model
|
|
73
|
+
- `gpt-4.1`: GPT-4.1 model
|
|
74
|
+
- `gpt-4.1-mini`: Compact GPT-4.1 model
|
|
75
|
+
- `o4-mini`: Compact O4 model
|
|
76
|
+
|
|
77
|
+
#### Deepseek Models
|
|
78
|
+
- `deepseek-chat`: Versatile chat model with JSON output and function calling support (via `lumic.llm.seek`)
|
|
79
|
+
- `deepseek-reasoner`: Advanced reasoning model for complex tasks (via `lumic.llm.seek`)
|
|
80
|
+
|
|
81
|
+
### Input Parameters
|
|
82
|
+
|
|
83
|
+
- `content` (string | ContentPart[]): The content to send to the LLM. Can be either:
|
|
84
|
+
- A string: Traditional text prompt
|
|
85
|
+
- An array of content parts: For multi-modal inputs, supporting:
|
|
86
|
+
- Text content: `{ type: 'text', text: string }`
|
|
87
|
+
- Image content: `{ type: 'image_url', image_url: { url: string, detail?: 'auto' | 'low' | 'high' } }`
|
|
88
|
+
|
|
89
|
+
- `responseFormat` (string | object): The desired format of the response. Can be:
|
|
90
|
+
- "text": Returns plain text response
|
|
91
|
+
- "json": Returns parsed JSON object with advanced parsing strategies
|
|
92
|
+
- JSON schema object: Returns parsed object conforming to the provided schema
|
|
93
|
+
|
|
94
|
+
- `options` (object, optional): Additional options for the API call
|
|
95
|
+
- `model` (string, optional): The model to use (default: "GPT-5-mini" for OpenAI, "deepseek-chat" for Deepseek)
|
|
96
|
+
- `developerPrompt` (string, optional): Custom developer context prompt
|
|
97
|
+
- `apiKey` (string, optional): Custom API key
|
|
98
|
+
- `reasoning_effort` (string, optional): Specify reasoning depth ('low' | 'medium' | 'high')
|
|
99
|
+
- `temperature` (number, optional): Controls randomness of the output
|
|
100
|
+
- `top_p` (number, optional): Controls diversity of token selection
|
|
101
|
+
- `frequency_penalty` (number, optional): Reduces repetition of tokens
|
|
102
|
+
- `presence_penalty` (number, optional): Encourages discussing new topics
|
|
103
|
+
- `max_completion_tokens` (number, optional): Limits response length
|
|
104
|
+
- `store` (boolean, optional): Option to store conversation
|
|
105
|
+
- `metadata` (object, optional): Additional metadata for stored conversations
|
|
106
|
+
- `tools` (Tool[], optional): Function calling tools
|
|
107
|
+
|
|
108
|
+
### Response Format
|
|
109
|
+
|
|
110
|
+
All responses follow a consistent format:
|
|
111
|
+
```typescript
|
|
112
|
+
interface LLMResponse<T> {
|
|
113
|
+
response: T; // The model's response
|
|
114
|
+
usage: {
|
|
115
|
+
prompt_tokens: number; // Number of tokens in the prompt
|
|
116
|
+
completion_tokens: number; // Number of tokens in the completion
|
|
117
|
+
reasoning_tokens?: number; // Number of tokens for reasoning (Deepseek models)
|
|
118
|
+
provider: string; // 'openai' or 'deepseek'
|
|
119
|
+
model: string; // The model name (e.g., 'GPT-5-mini', 'deepseek-chat')
|
|
120
|
+
cache_hit_tokens?: number; // Cache hit tokens for Deepseek models
|
|
121
|
+
cost: number; // Cost of the API call
|
|
122
|
+
};
|
|
123
|
+
tool_calls?: ToolCall[]; // Optional tool calls if tools were provided
|
|
124
|
+
}
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
### Examples
|
|
128
|
+
|
|
129
|
+
1. Basic text completion with OpenAI:
|
|
130
|
+
```javascript
|
|
131
|
+
const result = await lumic.llm.call(
|
|
132
|
+
"What is the capital of France?",
|
|
133
|
+
"text",
|
|
134
|
+
{ model: "GPT-5-mini" }
|
|
135
|
+
);
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
2. JSON output:
|
|
139
|
+
```javascript
|
|
140
|
+
const result = await lumic.llm.call(
|
|
141
|
+
"List three famous scientists in JSON format",
|
|
142
|
+
"json"
|
|
143
|
+
);
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
3. Using function calling:
|
|
147
|
+
```javascript
|
|
148
|
+
const weatherTool = {
|
|
149
|
+
type: 'function',
|
|
150
|
+
function: {
|
|
151
|
+
name: 'get_weather',
|
|
152
|
+
description: 'Get the current weather for a location',
|
|
153
|
+
parameters: {
|
|
154
|
+
type: 'object',
|
|
155
|
+
properties: {
|
|
156
|
+
location: {
|
|
157
|
+
type: 'string',
|
|
158
|
+
description: 'The city name',
|
|
159
|
+
},
|
|
160
|
+
},
|
|
161
|
+
required: ['location'],
|
|
162
|
+
},
|
|
163
|
+
},
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
const result = await lumic.llm.call(
|
|
167
|
+
"What's the weather like in Tokyo?",
|
|
168
|
+
"text",
|
|
169
|
+
{
|
|
170
|
+
model: "GPT-5-mini",
|
|
171
|
+
tools: [weatherTool],
|
|
172
|
+
}
|
|
173
|
+
);
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
4. Using Deepseek models:
|
|
177
|
+
```javascript
|
|
178
|
+
// Using Deepseek chat model for JSON output
|
|
179
|
+
const result = await lumic.llm.seek(
|
|
180
|
+
"List three famous scientists in JSON format",
|
|
181
|
+
"json"
|
|
182
|
+
);
|
|
183
|
+
|
|
184
|
+
// Using Deepseek reasoner model for complex reasoning tasks
|
|
185
|
+
const result = await lumic.llm.seek(
|
|
186
|
+
"Explain quantum entanglement in simple terms",
|
|
187
|
+
"text",
|
|
188
|
+
{ model: "deepseek-reasoner" }
|
|
189
|
+
);
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
### Model Features and Compatibility
|
|
193
|
+
|
|
194
|
+
| Feature | OpenAI Models | Deepseek Chat | Deepseek Reasoner |
|
|
195
|
+
|---------|---------------|---------------|-------------------|
|
|
196
|
+
| Text output | ✅ | ✅ | ✅ |
|
|
197
|
+
| JSON output | ✅ | ✅ | ❌ |
|
|
198
|
+
| Function calling | ✅ | ✅ | ❌ |
|
|
199
|
+
| Context length | Up to 128K | 64K | 64K |
|
|
200
|
+
| Cost | See [OpenAI pricing](https://openai.com/pricing) | $0.27/1M input tokens, $1.10/1M output tokens | $0.55/1M input tokens, $2.19/1M output tokens |
|
|
201
|
+
|
|
202
|
+
Note: When attempting to use unsupported features with a model (e.g., JSON output with Deepseek Reasoner), the API will return a graceful error response with details about the incompatibility.
|
|
203
|
+
|
|
204
|
+
## sendMessageToSlack
|
|
205
|
+
|
|
206
|
+
The `sendMessageToSlack` function is designed to send a message to a specified Slack channel.
|
|
207
|
+
|
|
208
|
+
### Input Parameters
|
|
209
|
+
|
|
210
|
+
- `channel` (string): The Slack channel to send the message to.
|
|
211
|
+
- `message` (string): The message content to send.
|
|
212
|
+
|
|
213
|
+
### Usage Example
|
|
214
|
+
|
|
215
|
+
```javascript
|
|
216
|
+
import { lumic } from "lumic-utility-functions";
|
|
217
|
+
|
|
218
|
+
const success = await lumic.slack.sendMessage("#general", "Hello, Slack!");
|
|
219
|
+
console.log(success); // true if message was sent successfully
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
## callPerplexityAPI
|
|
223
|
+
|
|
224
|
+
The `callPerplexityAPI` function is designed to send a query to the Perplexity API and return the response.
|
|
225
|
+
|
|
226
|
+
### Input Parameters
|
|
227
|
+
|
|
228
|
+
- `query` (string): The text query to send to the Perplexity API.
|
|
229
|
+
- `maxTokens` (number, optional): The maximum number of tokens for the response (default: 4000).
|
|
230
|
+
- `apiKey` (string, optional): The API key to use for the Perplexity API (defaults to the configured PERPLEXITY_API_KEY).
|
|
231
|
+
|
|
232
|
+
### Usage Example
|
|
233
|
+
|
|
234
|
+
```javascript
|
|
235
|
+
import { lumic } from "lumic-utility-functions";
|
|
236
|
+
|
|
237
|
+
const result = await lumic.perplexity.call("What is the capital of France?");
|
|
238
|
+
console.log(result); // "The capital of France is Paris."
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
## invokeLambdaFunction
|
|
242
|
+
|
|
243
|
+
The `invokeLambdaFunction` function is designed to invoke an AWS Lambda function.
|
|
244
|
+
|
|
245
|
+
### Input Parameters
|
|
246
|
+
|
|
247
|
+
- `functionName` (string): The name of the Lambda function to invoke.
|
|
248
|
+
- `payload` (object, optional): The payload to send to the Lambda function (default: empty object).
|
|
249
|
+
- `invocationType` (string, optional): The type of invocation (default: 'RequestResponse').
|
|
250
|
+
- `auth` (object, optional): AWS credentials for Lambda invocation. If not provided, falls back to environment variables.
|
|
251
|
+
|
|
252
|
+
### Usage Example
|
|
253
|
+
|
|
254
|
+
```javascript
|
|
255
|
+
import { lumic } from "lumic-utility-functions";
|
|
256
|
+
|
|
257
|
+
// Using environment variables
|
|
258
|
+
const result = await lumic.lambda.invoke(
|
|
259
|
+
"myLambdaFunction",
|
|
260
|
+
{ key: "value" }
|
|
261
|
+
);
|
|
262
|
+
|
|
263
|
+
// Using explicit auth credentials
|
|
264
|
+
const resultWithAuth = await lumic.lambda.invoke(
|
|
265
|
+
"myLambdaFunction",
|
|
266
|
+
{ key: "value" },
|
|
267
|
+
'RequestResponse',
|
|
268
|
+
{
|
|
269
|
+
AWS_ACCESS_KEY_ID: 'your-access-key',
|
|
270
|
+
AWS_SECRET_ACCESS_KEY: 'your-secret-key',
|
|
271
|
+
AWS_REGION: 'us-west-2'
|
|
272
|
+
}
|
|
273
|
+
);
|
|
274
|
+
```
|
|
275
|
+
|
|
276
|
+
## Zip File Utilities
|
|
277
|
+
|
|
278
|
+
The package includes a set of file management utilities to work with zip files, along with other general utility functions.
|
|
279
|
+
|
|
280
|
+
### createZipFile
|
|
281
|
+
|
|
282
|
+
The `createZipFile` function compresses a directory into a zip file.
|
|
283
|
+
|
|
284
|
+
### Input Parameters
|
|
285
|
+
|
|
286
|
+
- `sourceDir` (string): The path of the directory to compress. It must start with `/tmp/` for security purposes.
|
|
287
|
+
|
|
288
|
+
### Usage Example
|
|
289
|
+
|
|
290
|
+
```javascript
|
|
291
|
+
import { lumic } from "lumic-utility-functions";
|
|
292
|
+
|
|
293
|
+
const zipFilePath = await lumic.zip.create("/tmp/myDirectory");
|
|
294
|
+
console.log(`Zip file created at: ${zipFilePath}`);
|
|
295
|
+
```
|
|
296
|
+
|
|
297
|
+
### extractZipFile
|
|
298
|
+
|
|
299
|
+
The `extractZipFile` function extracts a zip archive into a specified directory.
|
|
300
|
+
|
|
301
|
+
### Input Parameters
|
|
302
|
+
|
|
303
|
+
- `zipPath` (string): The path to the zip file. It must start with `/tmp/`.
|
|
304
|
+
- `destPath` (string): The directory where the zip content will be extracted. It must start with `/tmp/`.
|
|
305
|
+
|
|
306
|
+
### Usage Example
|
|
307
|
+
|
|
308
|
+
```javascript
|
|
309
|
+
import { lumic } from "lumic-utility-functions";
|
|
310
|
+
|
|
311
|
+
const destPath = await lumic.zip.extract("/tmp/myZipFile.zip", "/tmp/extractedFolder");
|
|
312
|
+
console.log(`Files extracted to: ${destPath}`);
|
|
313
|
+
```
|
|
314
|
+
|
|
315
|
+
### addToZipFile
|
|
316
|
+
|
|
317
|
+
The `addToZipFile` function allows adding new files or directories to an existing zip file.
|
|
318
|
+
|
|
319
|
+
### Input Parameters
|
|
320
|
+
|
|
321
|
+
- `zipFilePath` (string): The path to the existing zip file (must start with `/tmp/`).
|
|
322
|
+
- `itemPath` (string): The file or directory to add to the zip (must start with `/tmp/`).
|
|
323
|
+
- `destPath` (string, optional): The destination folder inside the zip.
|
|
324
|
+
|
|
325
|
+
### Usage Example
|
|
326
|
+
|
|
327
|
+
```javascript
|
|
328
|
+
import { lumic } from "lumic-utility-functions";
|
|
329
|
+
|
|
330
|
+
const updatedZip = await lumic.zip.addTo("/tmp/myZipFile.zip", "/tmp/newFile.txt");
|
|
331
|
+
console.log(`File added to zip: ${updatedZip}`);
|
|
332
|
+
```
|
|
333
|
+
|
|
334
|
+
## AWS S3 Utilities
|
|
335
|
+
|
|
336
|
+
The package includes comprehensive S3 management functions:
|
|
337
|
+
|
|
338
|
+
### Core Functions
|
|
339
|
+
- `create(bucketName, auth?)`: Creates a new S3 bucket
|
|
340
|
+
- `destroy(bucketName, auth?)`: Deletes a bucket and all its contents
|
|
341
|
+
- `generateBucketName(prefix?)`: Generates a valid S3 bucket name
|
|
342
|
+
- `ls(options?)`: Lists buckets or objects in a bucket, sorted by most recent first. Options include:
|
|
343
|
+
- `bucket?`: Optional bucket name. If provided, lists objects in the bucket. If not provided, lists all buckets.
|
|
344
|
+
- `auth?`: Optional AWS credentials
|
|
345
|
+
- `limit?`: Maximum number of items to return (default: 10)
|
|
346
|
+
|
|
347
|
+
### File Operations
|
|
348
|
+
- `upload(bucketName, sourcePath, destinationPath?, auth?, isPublic?)`: Uploads files/directories with optional public access
|
|
349
|
+
- `download(bucketName, s3Path?, localPath?, auth?)`: Downloads from S3
|
|
350
|
+
- `readFile(bucket, key, auth?)`: Reads a single file as a stream
|
|
351
|
+
- `saveTo(bucket, content, filename?, auth?, isPublic?)`: Saves content directly to S3 with optional public access
|
|
352
|
+
- `deleteFile(s3FileUrl, auth?)`: Deletes a file using its S3 URL
|
|
353
|
+
|
|
354
|
+
### Utility Functions
|
|
355
|
+
- `generateBucketName(prefix?)`: Creates a valid bucket name with optional prefix
|
|
356
|
+
- `checkBucketForFileNamePart(bucketName, fileNamePart, auth?)`: Searches for files in a bucket
|
|
357
|
+
|
|
358
|
+
### Usage Examples
|
|
359
|
+
|
|
360
|
+
#### Creating and Managing Buckets
|
|
361
|
+
```javascript
|
|
362
|
+
import { lumic } from "lumic-utility-functions";
|
|
363
|
+
|
|
364
|
+
// Generate a unique bucket name with prefix
|
|
365
|
+
const bucketName = await lumic.s3.generateBucketName("myproject");
|
|
366
|
+
|
|
367
|
+
// Create the bucket
|
|
368
|
+
await lumic.s3.create(bucketName);
|
|
369
|
+
|
|
370
|
+
// Clean up when done
|
|
371
|
+
await lumic.s3.destroy(bucketName);
|
|
372
|
+
|
|
373
|
+
// List all buckets in the account (up to 10, most recent first)
|
|
374
|
+
const buckets = await lumic.s3.ls();
|
|
375
|
+
console.log(buckets.objects); // Array of { key: string, createdAt: number }
|
|
376
|
+
|
|
377
|
+
// List objects in a specific bucket with custom limit
|
|
378
|
+
const objects = await lumic.s3.ls({
|
|
379
|
+
bucket: "my-bucket",
|
|
380
|
+
limit: 5
|
|
381
|
+
});
|
|
382
|
+
console.log(objects.objects); // Array of { key: string, createdAt: number }
|
|
383
|
+
```
|
|
384
|
+
|
|
385
|
+
#### Uploading and Downloading Files
|
|
386
|
+
```javascript
|
|
387
|
+
// Upload a file (private by default)
|
|
388
|
+
const uploadResult = await lumic.s3.upload(
|
|
389
|
+
"my-bucket",
|
|
390
|
+
"/tmp/myfile.txt"
|
|
391
|
+
);
|
|
392
|
+
|
|
393
|
+
// Upload a publicly accessible file
|
|
394
|
+
const publicUploadResult = await lumic.s3.upload(
|
|
395
|
+
"my-bucket",
|
|
396
|
+
"/tmp/myfile.txt",
|
|
397
|
+
{
|
|
398
|
+
isPublic: true,
|
|
399
|
+
}
|
|
400
|
+
);
|
|
401
|
+
|
|
402
|
+
// Download files
|
|
403
|
+
const downloadResult = await lumic.s3.download(
|
|
404
|
+
"my-bucket",
|
|
405
|
+
"uploads/",
|
|
406
|
+
"/tmp/downloads"
|
|
407
|
+
);
|
|
408
|
+
|
|
409
|
+
// Stream a file
|
|
410
|
+
const fileStream = await lumic.s3.readFile(
|
|
411
|
+
"my-bucket",
|
|
412
|
+
"uploads/myfile.txt"
|
|
413
|
+
);
|
|
414
|
+
```
|
|
415
|
+
|
|
416
|
+
#### Working with Content
|
|
417
|
+
```javascript
|
|
418
|
+
// Save JSON directly to S3 (private by default)
|
|
419
|
+
const jsonContent = { name: "John", age: 30 };
|
|
420
|
+
const saveResult = await lumic.s3.saveTo(
|
|
421
|
+
"my-bucket",
|
|
422
|
+
jsonContent,
|
|
423
|
+
"data.json"
|
|
424
|
+
);
|
|
425
|
+
|
|
426
|
+
// Save JSON as a publicly accessible file
|
|
427
|
+
const publicSaveResult = await lumic.s3.saveTo(
|
|
428
|
+
"my-bucket",
|
|
429
|
+
jsonContent,
|
|
430
|
+
"public-data.json",
|
|
431
|
+
null, // using default AWS credentials
|
|
432
|
+
true // make the file public
|
|
433
|
+
);
|
|
434
|
+
|
|
435
|
+
// Check if a file exists
|
|
436
|
+
const searchResult = await lumic.s3.checkBucketForFileNamePart(
|
|
437
|
+
"my-bucket",
|
|
438
|
+
"data-2024"
|
|
439
|
+
);
|
|
440
|
+
|
|
441
|
+
// Delete a file using its URL
|
|
442
|
+
await lumic.s3.deleteFile(
|
|
443
|
+
"https://my-bucket.s3.region.amazonaws.com/uploads/myfile.txt"
|
|
444
|
+
);
|
|
445
|
+
```
|
|
446
|
+
|
|
447
|
+
## JSON Utilities
|
|
448
|
+
|
|
449
|
+
The package includes utility functions for working with JSON data, particularly for handling and fixing malformed JSON.
|
|
450
|
+
|
|
451
|
+
### fixBrokenJson
|
|
452
|
+
|
|
453
|
+
The `fixBrokenJson` function is a robust JSON parser that attempts to repair and parse malformed JSON strings.
|
|
454
|
+
|
|
455
|
+
### Input Parameters
|
|
456
|
+
|
|
457
|
+
- `jsonStr` (string): The potentially malformed JSON string to fix and parse
|
|
458
|
+
|
|
459
|
+
### Usage Example
|
|
460
|
+
|
|
461
|
+
```javascript
|
|
462
|
+
import { lumic } from "lumic-utility-functions";
|
|
463
|
+
|
|
464
|
+
// Fix malformed JSON
|
|
465
|
+
const brokenJson = `{
|
|
466
|
+
name: "John",
|
|
467
|
+
age: 30,
|
|
468
|
+
hobbies: ['reading' "writing", coding],
|
|
469
|
+
address: {
|
|
470
|
+
street: '123 Main St,
|
|
471
|
+
city: "Springfield"
|
|
472
|
+
},
|
|
473
|
+
}`;
|
|
474
|
+
|
|
475
|
+
const fixed = lumic.json.fix(brokenJson);
|
|
476
|
+
console.log(fixed);
|
|
477
|
+
```
|
|
478
|
+
|
|
479
|
+
### parseResponse
|
|
480
|
+
|
|
481
|
+
The `parseResponse` function is a sophisticated JSON parser that attempts to extract and parse JSON content from various formats, including code blocks.
|
|
482
|
+
|
|
483
|
+
### Input Parameters
|
|
484
|
+
|
|
485
|
+
- `content` (string): The content string that may contain JSON
|
|
486
|
+
- `responseFormat` ('text' | 'json'): The expected format of the response. Use 'json' to attempt JSON parsing.
|
|
487
|
+
|
|
488
|
+
### Usage Example
|
|
489
|
+
|
|
490
|
+
```javascript
|
|
491
|
+
import { lumic } from "lumic-utility-functions";
|
|
492
|
+
|
|
493
|
+
// Parse JSON from a string that might contain code blocks or extra text
|
|
494
|
+
const content = `
|
|
495
|
+
\`\`\`json
|
|
496
|
+
{
|
|
497
|
+
"name": "John",
|
|
498
|
+
"age": 30
|
|
499
|
+
}
|
|
500
|
+
\`\`\`
|
|
501
|
+
`;
|
|
502
|
+
|
|
503
|
+
const parsed = await lumic.json.parse(content, 'json');
|
|
504
|
+
console.log(parsed); // { name: "John", age: 30 }
|
|
505
|
+
```
|
|
506
|
+
|
|
507
|
+
The function employs multiple parsing strategies:
|
|
508
|
+
1. Direct JSON parsing
|
|
509
|
+
2. Extracting JSON between first {} or []
|
|
510
|
+
3. Removing leading/trailing text
|
|
511
|
+
4. AI-assisted JSON fixing (using GPT-4)
|
|
512
|
+
|
|
513
|
+
### isValidJson
|
|
514
|
+
|
|
515
|
+
The `isValidJson` function checks if a string contains valid JSON.
|
|
516
|
+
|
|
517
|
+
### Usage Example
|
|
518
|
+
|
|
519
|
+
```javascript
|
|
520
|
+
import { lumic } from "lumic-utility-functions";
|
|
521
|
+
|
|
522
|
+
const validJson = '{"name": "John", "age": 30}';
|
|
523
|
+
const invalidJson = '{name: John, age: 30}';
|
|
524
|
+
|
|
525
|
+
console.log(lumic.json.isValid(validJson)); // true
|
|
526
|
+
console.log(lumic.json.isValid(invalidJson)); // false
|
|
527
|
+
```
|
|
528
|
+
|
|
529
|
+
## Google Sheets Utilities
|
|
530
|
+
|
|
531
|
+
The package provides utilities for working with Google Sheets through the `lumic.gs` namespace:
|
|
532
|
+
|
|
533
|
+
### Functions
|
|
534
|
+
|
|
535
|
+
- `addRow(config, values, startColumn?)`: Add a row of values to a Google Sheet
|
|
536
|
+
- `addSheet(spreadsheetId, sheetTitle)`: Add a new sheet to an existing spreadsheet
|
|
537
|
+
- `writeCell(config, cell, value)`: Write a value to a specific cell
|
|
538
|
+
- `getCell(config, cell)`: Get the value from a specific cell
|
|
539
|
+
- `convertA1ToRowCol(a1Notation)`: Convert A1 notation (e.g., 'A1') to row and column numbers
|
|
540
|
+
- `writeArray(config, data, startCell?)`: Write a 2D array of data to a sheet
|
|
541
|
+
|
|
542
|
+
### Usage Example
|
|
543
|
+
|
|
544
|
+
```javascript
|
|
545
|
+
import { lumic } from "lumic-utility-functions";
|
|
546
|
+
|
|
547
|
+
// Configure the sheet
|
|
548
|
+
const config = {
|
|
549
|
+
spreadsheetId: "your-spreadsheet-id",
|
|
550
|
+
sheetName: "Sheet1"
|
|
551
|
+
};
|
|
552
|
+
|
|
553
|
+
// Add a row
|
|
554
|
+
await lumic.gs.addRow(config, ["Value1", "Value2", "Value3"]);
|
|
555
|
+
|
|
556
|
+
// Write to a specific cell
|
|
557
|
+
await lumic.gs.writeCell(config, { row: 1, column: 1 }, "Hello World");
|
|
558
|
+
|
|
559
|
+
// Write an array of data
|
|
560
|
+
const data = [
|
|
561
|
+
["Header1", "Header2"],
|
|
562
|
+
["Value1", "Value2"],
|
|
563
|
+
["Value3", "Value4"]
|
|
564
|
+
];
|
|
565
|
+
await lumic.gs.writeArray(config, data, "A1");
|
|
566
|
+
```
|
|
567
|
+
|
|
568
|
+
## Performance Timer
|
|
569
|
+
|
|
570
|
+
The `PerformanceTimer` utility helps track execution time and performance metrics across your application. It's accessible through `lumic.utils.Timer`.
|
|
571
|
+
|
|
572
|
+
### Usage
|
|
573
|
+
|
|
574
|
+
```javascript
|
|
575
|
+
import { lumic } from "lumic-utility-functions";
|
|
576
|
+
|
|
577
|
+
// Get the global Timer instance
|
|
578
|
+
const timer = lumic.utils.Timer;
|
|
579
|
+
|
|
580
|
+
// Timer starts automatically upon instantiation
|
|
581
|
+
timer.checkpoint("start-processing"); // Record a checkpoint
|
|
582
|
+
|
|
583
|
+
// Do some work...
|
|
584
|
+
await someAsyncOperation();
|
|
585
|
+
timer.checkpoint("after-async-operation");
|
|
586
|
+
|
|
587
|
+
// Get current elapsed time in seconds
|
|
588
|
+
console.log(`Elapsed time: ${timer.getElapsedSeconds()} seconds`);
|
|
589
|
+
|
|
590
|
+
// Do more work...
|
|
591
|
+
await anotherOperation();
|
|
592
|
+
timer.checkpoint("after-another-operation");
|
|
593
|
+
|
|
594
|
+
// Stop the timer when done
|
|
595
|
+
timer.stop();
|
|
596
|
+
|
|
597
|
+
// Generate a performance report
|
|
598
|
+
const report = timer.generateReport();
|
|
599
|
+
console.log(report);
|
|
600
|
+
// Output:
|
|
601
|
+
// {
|
|
602
|
+
// totalTime: 1234.56, // Total time in milliseconds
|
|
603
|
+
// phases: [
|
|
604
|
+
// { label: "start-processing", duration: 100.5 },
|
|
605
|
+
// { label: "after-async-operation", duration: 534.2 },
|
|
606
|
+
// { label: "after-another-operation", duration: 599.86 }
|
|
607
|
+
// ]
|
|
608
|
+
// }
|
|
609
|
+
```
|
|
610
|
+
|
|
611
|
+
The PerformanceTimer provides:
|
|
612
|
+
- Automatic timing start upon instantiation
|
|
613
|
+
- Checkpoint recording with custom labels
|
|
614
|
+
- Total execution time tracking
|
|
615
|
+
- Quick elapsed time in seconds via `getElapsedSeconds()`
|
|
616
|
+
- Detailed performance report with phase-by-phase timing
|
|
617
|
+
|
|
618
|
+
Note: Use the global Timer instance across your application to track the complete execution flow, rather than creating new instances.
|
|
619
|
+
|
|
620
|
+
## TypeScript Support
|
|
621
|
+
|
|
622
|
+
This package includes TypeScript definitions. When using TypeScript, you'll get full type support and autocompletion for all functions and their parameters.
|
|
623
|
+
|
|
624
|
+
```typescript
|
|
625
|
+
import { lumic } from "lumic-utility-functions";
|
|
626
|
+
|
|
627
|
+
// TypeScript will provide type checking and autocompletion
|
|
628
|
+
const result = await lumic.llm.call(
|
|
629
|
+
"Your prompt",
|
|
630
|
+
"json",
|
|
631
|
+
{ model: "GPT-5-mini" }
|
|
632
|
+
);
|
|
633
|
+
```
|
|
634
|
+
|
|
635
|
+
## Author
|
|
636
|
+
|
|
637
|
+
The utility-functions project is a product of [Lumic.ai](https://lumic.ai).
|