feat: remove logging.ts and uuid

old-agentic-v1^2
Travis Fischer 2023-06-28 20:01:45 -07:00
rodzic 311db19f02
commit d1838c734e
3 zmienionych plików z 1 dodań i 160 usunięć

Wyświetl plik

@ -65,7 +65,6 @@
"quick-lru": "^6.1.1",
"replicate": "^0.12.3",
"ts-dedent": "^2.2.0",
"uuid": "^9.0.0",
"zod": "^3.21.4",
"zod-to-json-schema": "^3.21.2",
"zod-validation-error": "^1.3.1"
@ -76,7 +75,6 @@
"@types/debug": "^4.1.8",
"@types/node": "^20.3.2",
"@types/sinon": "^10.0.15",
"@types/uuid": "^9.0.2",
"@typescript-eslint/eslint-plugin": "^5.60.1",
"@typescript-eslint/parser": "^5.60.1",
"ava": "^5.3.1",

Wyświetl plik

@ -1,4 +1,4 @@
lockfileVersion: '6.1'
lockfileVersion: '6.0'
settings:
autoInstallPeers: true
@ -83,9 +83,6 @@ dependencies:
ts-dedent:
specifier: ^2.2.0
version: 2.2.0
uuid:
specifier: ^9.0.0
version: 9.0.0
zod:
specifier: ^3.21.4
version: 3.21.4
@ -112,9 +109,6 @@ devDependencies:
'@types/sinon':
specifier: ^10.0.15
version: 10.0.15
'@types/uuid':
specifier: ^9.0.2
version: 9.0.2
'@typescript-eslint/eslint-plugin':
specifier: ^5.60.1
version: 5.60.1(@typescript-eslint/parser@5.60.1)(eslint@8.43.0)(typescript@5.1.3)
@ -1058,10 +1052,6 @@ packages:
resolution: {integrity: sha512-9GcLXF0/v3t80caGs5p2rRfkB+a8VBGLJZVih6CNFkx8IZ994wiKKLSRs9nuFwk1HevWs/1mnUmkApGrSGsShA==}
dev: true
/@types/uuid@9.0.2:
resolution: {integrity: sha512-kNnC1GFBLuhImSnV7w4njQkUiJi0ZXUycu1rUaouPqiKlXkh77JKgdRnTAp1x5eBwcIwbtI+3otwzuIDEuDoxQ==}
dev: true
/@typescript-eslint/eslint-plugin@5.60.1(@typescript-eslint/parser@5.60.1)(eslint@8.43.0)(typescript@5.1.3):
resolution: {integrity: sha512-KSWsVvsJsLJv3c4e73y/Bzt7OpqMCADUO846bHcuWYSYM19bldbAeDv7dYyV0jwkbMfJ2XdlzwjhXtuD7OY6bw==}
engines: {node: ^12.22.0 || ^14.17.0 || >=16.0.0}
@ -4457,11 +4447,6 @@ packages:
punycode: 2.3.0
dev: true
/uuid@9.0.0:
resolution: {integrity: sha512-MXcSTerfPa4uqyzStbRoTgt5XIe3x5+42+q1sDuy3R5MDk66URdLMOZe5aPX/SQd+kuYAh0FdP/pO28IkQyTeg==}
hasBin: true
dev: false
/v8-to-istanbul@9.1.0:
resolution: {integrity: sha512-6z3GW9x8G1gd+JIIgQQQxXuiJtCXeAjp6RaPEPLv62mH3iPHPxV6W3robxtCzNErRo6ZwTmzWhsbNvjyEBKzKA==}
engines: {node: '>=10.12.0'}

Wyświetl plik

@ -1,142 +0,0 @@
import logger from 'debug'
import { v4 as uuidv4 } from 'uuid'
import { getEnv } from './env'
/**
* List of events that can occur within the library.
*/
export const Events = {
LLM_CALL: 'LLM_CALL',
LLM_COMPLETION: 'LLM_COMPLETION'
} as const
export type EventType = (typeof Events)[keyof typeof Events]
/**
* Severity levels of an event.
*/
export const Severity = {
DEBUG: 0,
INFO: 1,
WARNING: 2,
ERROR: 3,
CRITICAL: 4
} as const
type SeverityType = (typeof Severity)[keyof typeof Severity]
/*
* Define minimum LOG_LEVEL, defaulting to Severity.INFO if not provided or if an invalid value is provided. Any events below that level won't be logged to the console.
*/
let LOG_LEVEL: SeverityType = Severity.INFO
const logLevelEnv = getEnv('DEBUG_LOG_LEVEL')
if (logLevelEnv && Severity[logLevelEnv.toUpperCase()] !== undefined) {
LOG_LEVEL = Severity[logLevelEnv.toUpperCase()]
} else if (logLevelEnv) {
throw new Error(`Invalid value for LOG_LEVEL: ${logLevelEnv}`)
}
/**
* Define loggers for each severity level such that logs can be filtered by severity.
*/
const LOGGERS: Record<SeverityType, ReturnType<typeof logger>> = {
[Severity.CRITICAL]: logger('agentic:events:critical'),
[Severity.ERROR]: logger('agentic:events:error'),
[Severity.WARNING]: logger('agentic:events:warning'),
[Severity.INFO]: logger('agentic:events:info'),
[Severity.DEBUG]: logger('agentic:events:debug')
}
/**
* Payload of an event.
*/
interface EventPayload {
[key: string]: unknown
}
/**
* Data required to create a new Event object.
*/
interface EventData {
parentId?: string
id?: string
timestamp?: Date
payload?: EventPayload
severity?: SeverityType
version?: number
}
/**
* Events that occur within the library (should be treated as immutable).
*/
export class Event {
public readonly type: EventType
public readonly parentId?: string
public readonly id: string
public readonly timestamp: Date
public readonly payload?: EventPayload
public readonly severity: SeverityType
public readonly version: number
constructor(type: EventType, data: EventData = {}) {
this.type = type
this.parentId = data.parentId
this.id = data.id ?? uuidv4()
this.timestamp = data.timestamp ?? new Date()
this.payload = data.payload ? { ...data.payload } : {} // Only doing a shallow instead of a deep copy for performance reasons..
this.severity = data.severity ?? Severity.INFO
this.version = data.version ?? 1 // Default to version 1 if not provided...
}
/**
* Converts a JSON string representation of an event back into an Event object.
*/
static fromJSON(json: string): Event {
const { type, ...data } = JSON.parse(json)
// Convert the timestamp back into a Date object, since `JSON.parse()` will have turned it into a string:
data.timestamp = new Date(data.timestamp)
const event = new Event(type, data)
return event
}
/**
* Converts the event to a JSON string representation.
*
* @returns JSON representation
*/
toJSON(): string {
return JSON.stringify({
type: this.type,
parentId: this.parentId,
id: this.id,
timestamp: this.timestamp.toISOString(),
payload: this.payload,
severity: this.severity,
version: this.version
})
}
/**
* Converts the event to a human-readable string representation suitable for logging.
*
* @returns string representation
*/
toString(): string {
return `Event { type: ${this.type}, parentId: ${this.parentId}, id: ${
this.id
}, timestamp: ${this.timestamp.toISOString()}, payload: ${JSON.stringify(
this.payload
)}, severity: ${this.severity} }`
}
/**
* Logs the event to the console.
*/
log(): void {
if (this.severity >= LOG_LEVEL) {
const logger = LOGGERS[this.severity]
logger(this.toString())
}
}
}