UNPKG

@plastichub/osr-log

Version:

Package basics

465 lines 16.4 kB
{ "model": "anthropic/claude-3.5-sonnet", "messages": [ { "role": "system", "content": "You are software engineer :)" }, { "path": "src/index.ts", "content": "import * as path from 'path'\r\nimport { createStream } from \"rotating-file-stream\"\r\nimport { BaseLogger, ILogObjMeta, ISettingsParam, Logger } from \"tslog\"\r\nimport { LogLevelEx } from './zod_schema'\r\nimport { sync as read } from '@plastichub/fs/read'\r\nimport { sync as write } from '@plastichub/fs/write'\r\nimport { sync as exists } from '@plastichub/fs/exists'\r\nexport * from './zod_schema'\r\n\r\nimport * as TransportStream from 'winston-transport'\r\nconst { SeqTransport } = require('@datalust/winston-seq')\r\nimport * as winston from 'winston'\r\n\r\nexport function createLogger(name: string, options?: ISettingsParam<any>) {\r\n return new Logger<unknown>({\r\n name,\r\n type: 'pretty',\r\n ...options,\r\n })\r\n}\r\nexport const defaultLogger = createLogger('DefaultLogger', {\r\n minLevel: LogLevelEx.info\r\n})\r\nexport class CustomLogger<LogObj> extends BaseLogger<LogObj> {\r\n constructor(settings?: ISettingsParam<LogObj>, logObj?: LogObj) {\r\n super(settings, logObj, 5)\r\n }\r\n public custom(loggerName?: string, ...args: unknown[]): LogObj & ILogObjMeta {\r\n return super.log(8, loggerName || 'Custom', ...args)\r\n }\r\n}\r\n\r\nclass JsonArrayFileTransport extends TransportStream {\r\n filename: string;\r\n constructor(opts) {\r\n super(opts);\r\n opts.filename = opts.filename\r\n this.filename = opts.filename || 'logs.json';\r\n setImmediate(() => this.emit('opened'))\r\n }\r\n log(info: any, next: () => void): void {\r\n setImmediate(() => this.emit('logged', info))\r\n const { level, message, exception, stack, ...props } = info;\r\n setImmediate(() => this.emit('logged', info))\r\n const fileExists = exists(this.filename)\r\n const existingLogs = fileExists\r\n ? read(this.filename, 'json') as []\r\n : [];\r\n\r\n const entry = {\r\n level: info.level,\r\n message: info.message,\r\n timestamp: new Date().toISOString(),\r\n ...info\r\n };\r\n existingLogs.push(entry)\r\n write(this.filename, existingLogs)\r\n next()\r\n }\r\n close(): void {\r\n setImmediate(() => this.emit('closed'))\r\n }\r\n flush(): Promise<any> {\r\n return new Promise((resolve, reject) => {\r\n resolve(true)\r\n })\r\n }\r\n}\r\nexport type WinstonLoggerType = 'JSON' | 'Markdown'\r\nexport const winstonLogger = (name: string, file: string, type:WinstonLoggerType = 'JSON' ) => {\r\n /*\r\n const fileLogger = new winston.transports.File(\r\n {\r\n format: winston.format.combine(\r\n winston.format.timestamp(),\r\n winston.format.timestamp({ format: 'MM/DD/YYYY hh:mm:ss.SSS' }),\r\n winston.format.json(),\r\n winston.format.printf(info => {\r\n return JSON.stringify(info, null, 2);\r\n })),\r\n dirname: path.parse(file).dir,\r\n filename: path.parse(file).base\r\n })\r\n */\r\n const logger = winston.createLogger({\r\n defaultMeta: { service: name },\r\n level: 'debug',\r\n transports: [ \r\n new winston.transports.Console(\r\n {\r\n format: winston.format.combine(\r\n winston.format.timestamp(),\r\n winston.format.timestamp({ format: 'MM/DD/YYYY hh:mm:ss.SSS' }),\r\n winston.format.json(),\r\n winston.format.colorize(),\r\n winston.format.printf(info => {\r\n return `[${info.level}] [${name}] : ${info.message}`;\r\n }))\r\n\r\n })\r\n ]\r\n })\r\n if(type === 'JSON'){\r\n logger.add(new JsonArrayFileTransport({ filename: file }))\r\n }\r\n /*\r\n logger.add(new SeqTransport({\r\n serverUrl: \"https://localhost:5341\",\r\n apiKey: \"Qu6XPThyfnXo7ugXoRZS\",\r\n onError: (e => { }),\r\n }))\r\n */\r\n return logger\r\n}\r\nexport const createFileLogger = (logger: Logger<unknown>, level: number, file: string): Logger<unknown> => {\r\n const rfs = createStream(file,\r\n {\r\n size: \"10M\", // rotate every 10 MegaBytes written\r\n interval: \"1d\", // rotate daily\r\n compress: \"gzip\", // compress rotated files\r\n });\r\n\r\n const log = new Logger({\r\n type: \"json\",\r\n attachedTransports: [\r\n (logObj) => {\r\n rfs.write(JSON.stringify(logObj) + \"\\n\");\r\n },\r\n ],\r\n });\r\n return log\r\n\r\n /*\r\nconst transport = {\r\n minLevel: level,\r\n transportLogger:(logObject: any) => {\r\n stream.write(JSON.stringify(logObject, null, 2) + \"\\n\");\r\n }\r\n}\r\nlogger.attachTransport(transport.transportLogger)\r\n*/\r\n return logger\r\n}", "role": "user" }, { "role": "user", "content": "Be silent, show me the results only" }, { "role": "user", "content": "introduce logging flags (enum) to enable targets : file (text, json (JsonArrayFileTransport), console and SeqTransport, in src/index.ts::winstonLogger" } ], "tools": [ { "type": "function", "function": { "name": "list_files", "description": "List all files in a directory", "parameters": { "type": "object", "properties": { "directory": { "type": "string" }, "pattern": { "type": "string", "optional": true } }, "required": [ "directory" ] } } }, { "type": "function", "function": { "name": "read_files", "description": "Reads files in a directory with a given pattern", "parameters": { "type": "object", "properties": { "directory": { "type": "string" }, "pattern": { "type": "string", "optional": true } }, "required": [ "directory" ] } } }, { "type": "function", "function": { "name": "remove_file", "description": "Remove a file at given path", "parameters": { "type": "object", "properties": { "path": { "type": "string" } }, "required": [ "path" ] } } }, { "type": "function", "function": { "name": "rename_file", "description": "Rename or move a file or directory", "parameters": { "type": "object", "properties": { "src": { "type": "string" }, "dst": { "type": "string" } }, "required": [ "path" ] } } }, { "type": "function", "function": { "name": "modify_project_files", "description": "Modify existing project files", "parameters": { "type": "object", "properties": { "files": { "type": "array", "items": { "type": "object", "properties": { "path": { "type": "string" }, "content": { "type": "string" } }, "required": [ "path", "content" ] } } }, "required": [ "files" ] } } }, { "type": "function", "function": { "name": "create_file", "description": "Creates a file, given a path and content", "parameters": { "type": "object", "properties": { "file": { "type": "object", "properties": { "path": { "type": "string" }, "content": { "type": "string" } } } }, "required": [ "file" ] } } }, { "type": "function", "function": { "name": "file_exists", "description": "check if a file or folder exists", "parameters": { "type": "object", "properties": { "file": { "type": "object", "properties": { "path": { "type": "string" } } } }, "required": [ "file" ] } } }, { "type": "function", "function": { "name": "read_file", "description": "read a file, at given a path", "parameters": { "type": "object", "properties": { "file": { "type": "object", "properties": { "path": { "type": "string" } } } }, "required": [ "file" ] } } }, { "type": "function", "function": { "name": "init_repository", "description": "Initialize a new git repository if not exists", "parameters": { "type": "object", "properties": {}, "required": [] } } }, { "type": "function", "function": { "name": "commit_files_git", "description": "Commit files using git", "parameters": { "type": "object", "properties": { "files": { "type": "array", "items": { "type": "string" } }, "message": { "type": "string" } }, "required": [ "files" ] } } }, { "type": "function", "function": { "name": "ask_question", "description": "Ask user a simple question and get response", "parameters": { "type": "object", "properties": { "question": { "type": "string", "description": "Question to ask the user" }, "default": { "type": "string", "description": "Default answer", "optional": true } }, "required": [ "question" ] } } }, { "type": "function", "function": { "name": "choose_option", "description": "Ask user to choose from multiple options", "parameters": { "type": "object", "properties": { "message": { "type": "string", "description": "Message to show the user" }, "choices": { "type": "array", "items": { "type": "string" }, "description": "List of choices" }, "multiple": { "type": "boolean", "description": "Allow multiple selections", "optional": true } }, "required": [ "message", "choices" ] } } }, { "type": "function", "function": { "name": "execute_command", "description": "Execute a terminal command and capture output", "parameters": { "type": "object", "properties": { "command": { "type": "string", "description": "Command to execute" }, "args": { "type": "array", "items": { "type": "string" }, "description": "Command arguments", "optional": true }, "cwd": { "type": "string", "description": "Working directory for command execution", "optional": true }, "background": { "type": "boolean", "description": "Run command in background (non-blocking)", "optional": true }, "window": { "type": "boolean", "description": "Open command in new terminal window", "optional": true }, "detached": { "type": "boolean", "description": "Run process detached from parent", "optional": true } }, "required": [ "command" ] } } }, { "type": "function", "function": { "name": "google", "description": "Searches Google for the given query", "parameters": { "type": "object", "properties": { "query": { "type": "string" } }, "required": [ "query" ] } } }, { "type": "function", "function": { "name": "serpapi", "description": "Searches Serpapi (finds locations (engine:google_local), places on the map (engine:google_maps) ) for the given query", "parameters": { "type": "object", "properties": { "query": { "type": "string" }, "engine": { "type": "string", "default": "google" } }, "required": [ "query" ] } } }, { "type": "function", "function": { "name": "browse_page", "description": "Browse a webpage and return its content as markdown, all links, images and pages main image", "parameters": { "type": "object", "properties": { "url": { "type": "string", "description": "URL of the webpage to browse" } }, "required": [ "url" ] } } }, { "type": "function", "function": { "name": "send_email", "description": "Sends an email", "parameters": { "type": "object", "properties": { "recipient": { "type": [ "string", "array" ], "items": { "type": "string" }, "description": "The email address of the recipient(s). Can be a single email or an array of emails. For \"me\", use the default email address" }, "subject": { "type": "string", "description": "the subject", "optional": true }, "body": { "type": "string", "description": "Markdown formatted body of the email", "optional": true } }, "required": [ "url" ] } } }, { "type": "function", "function": { "name": "capture_screen", "description": "Capture a screenshot and store it as file (jpg). Returns the path to the file", "parameters": { "type": "object", "properties": { "file": { "type": "string" } }, "required": [ "file" ] } } } ], "tool_choice": "auto", "parallel_tool_calls": false }