@stackfactor/agent-utils 1.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/dist/cjs/const.d.ts +15 -0
- package/dist/cjs/const.d.ts.map +1 -0
- package/dist/cjs/const.js +15 -0
- package/dist/cjs/errorHandling.d.ts +20 -0
- package/dist/cjs/errorHandling.d.ts.map +1 -0
- package/dist/cjs/errorHandling.js +23 -0
- package/dist/cjs/index.d.ts +9 -0
- package/dist/cjs/index.d.ts.map +1 -0
- package/dist/cjs/index.js +14 -0
- package/dist/cjs/langChain.d.ts +10 -0
- package/dist/cjs/langChain.d.ts.map +1 -0
- package/dist/cjs/langChain.js +1241 -0
- package/dist/cjs/logger.d.ts +29 -0
- package/dist/cjs/logger.d.ts.map +1 -0
- package/dist/cjs/logger.js +77 -0
- package/dist/cjs/package.json +1 -0
- package/dist/esm/const.d.ts +15 -0
- package/dist/esm/const.d.ts.map +1 -0
- package/dist/esm/const.js +13 -0
- package/dist/esm/errorHandling.d.ts +20 -0
- package/dist/esm/errorHandling.d.ts.map +1 -0
- package/dist/esm/errorHandling.js +21 -0
- package/dist/esm/index.d.ts +9 -0
- package/dist/esm/index.d.ts.map +1 -0
- package/dist/esm/index.js +8 -0
- package/dist/esm/langChain.d.ts +10 -0
- package/dist/esm/langChain.d.ts.map +1 -0
- package/dist/esm/langChain.js +1236 -0
- package/dist/esm/logger.d.ts +29 -0
- package/dist/esm/logger.d.ts.map +1 -0
- package/dist/esm/logger.js +72 -0
- package/dist/esm/package.json +1 -0
- package/package.json +57 -0
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Writes a structured log entry via Winston using the GCP-compatible format. The
|
|
3
|
+
* current OpenTelemetry span context is read to enrich the entry with `trace` and
|
|
4
|
+
* `spanId` fields when an active span is present. The user's email (if available on the
|
|
5
|
+
* request) is prepended to the message string along with an icon derived from the log
|
|
6
|
+
* level. Any additional fields supplied in `options` are merged into the log record.
|
|
7
|
+
* @param request - The HTTP request object; used to extract `request.user.email` for
|
|
8
|
+
* the log message prefix; may be `null`
|
|
9
|
+
* @param level - The Winston log level string (e.g. `"info"`, `"error"`, `"warn"`);
|
|
10
|
+
* must be one of the keys in the `levels` constant
|
|
11
|
+
* @param message - The primary log message string
|
|
12
|
+
* @param options - Additional key-value pairs merged into the log record for structured
|
|
13
|
+
* logging (e.g. `{ service: "auth", requestId: "..." }`); defaults to `{}`
|
|
14
|
+
*/
|
|
15
|
+
type LogLevel = "error" | "warn" | "info" | "http" | "verbose" | "debug" | "silly";
|
|
16
|
+
declare const _default: {
|
|
17
|
+
log: (request: any, level: LogLevel, message: string, options?: any) => void;
|
|
18
|
+
levels: {
|
|
19
|
+
readonly error: "error";
|
|
20
|
+
readonly warn: "warn";
|
|
21
|
+
readonly info: "info";
|
|
22
|
+
readonly http: "http";
|
|
23
|
+
readonly verbose: "verbose";
|
|
24
|
+
readonly debug: "debug";
|
|
25
|
+
readonly silly: "silly";
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
export default _default;
|
|
29
|
+
//# sourceMappingURL=logger.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../../src/logger.ts"],"names":[],"mappings":"AAiEA;;;;;;;;;;;;;GAaG;AACH,KAAK,QAAQ,GACT,OAAO,GACP,MAAM,GACN,MAAM,GACN,MAAM,GACN,SAAS,GACT,OAAO,GACP,OAAO,CAAC;;mBAGD,GAAG,SACL,QAAQ,WACN,MAAM,YACN,GAAG,KACX,IAAI;;;;;;;;;;;AA0BP,wBAGE"}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const winston_1 = __importDefault(require("winston"));
|
|
7
|
+
const api_1 = require("@opentelemetry/api");
|
|
8
|
+
const { combine, timestamp, printf } = winston_1.default.format;
|
|
9
|
+
const levels = {
|
|
10
|
+
error: "error",
|
|
11
|
+
warn: "warn",
|
|
12
|
+
info: "info",
|
|
13
|
+
http: "http",
|
|
14
|
+
verbose: "verbose",
|
|
15
|
+
debug: "debug",
|
|
16
|
+
silly: "silly",
|
|
17
|
+
};
|
|
18
|
+
const levelIcons = {
|
|
19
|
+
error: "❌",
|
|
20
|
+
warn: "⚠️",
|
|
21
|
+
info: "📝",
|
|
22
|
+
http: "🌐",
|
|
23
|
+
verbose: "📝",
|
|
24
|
+
debug: "🔍",
|
|
25
|
+
silly: "🎭",
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* Custom Winston printf formatter that serialises log records to a single-line JSON
|
|
29
|
+
* string compatible with Google Cloud Platform (GCP) structured logging. The `level`
|
|
30
|
+
* field is mapped to GCP's `severity` key (uppercased) and all standard fields plus any
|
|
31
|
+
* additional metadata are included in the output.
|
|
32
|
+
*/
|
|
33
|
+
const gcpFormat = printf((info) => {
|
|
34
|
+
const { level, message, timestamp: ts, service, environment, requestId, userId, trace, spanId, sourceLocation, ...rest } = info;
|
|
35
|
+
return JSON.stringify({
|
|
36
|
+
severity: level ? level.toUpperCase() : undefined,
|
|
37
|
+
message,
|
|
38
|
+
timestamp: ts,
|
|
39
|
+
service,
|
|
40
|
+
environment,
|
|
41
|
+
requestId,
|
|
42
|
+
userId,
|
|
43
|
+
trace,
|
|
44
|
+
spanId,
|
|
45
|
+
sourceLocation,
|
|
46
|
+
...rest,
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
const logger = winston_1.default.createLogger({
|
|
50
|
+
format: combine(timestamp(), gcpFormat),
|
|
51
|
+
transports: [new winston_1.default.transports.Console()],
|
|
52
|
+
});
|
|
53
|
+
const log = (request, level, message, options = {}) => {
|
|
54
|
+
// Get current OpenTelemetry span context
|
|
55
|
+
const span = api_1.trace.getSpan(api_1.context.active());
|
|
56
|
+
let traceId, spanId;
|
|
57
|
+
if (span) {
|
|
58
|
+
const spanContext = span.spanContext();
|
|
59
|
+
traceId = spanContext.traceId;
|
|
60
|
+
spanId = spanContext.spanId;
|
|
61
|
+
}
|
|
62
|
+
// eslint-disable-next-line security/detect-object-injection
|
|
63
|
+
const icon = levelIcons[level] || "ℹ️";
|
|
64
|
+
logger.log({
|
|
65
|
+
level: level,
|
|
66
|
+
message: `${icon} ${request && request.user ? request.user.email + " - " : ""}${message}`,
|
|
67
|
+
trace: traceId
|
|
68
|
+
? `projects/${process.env.GOOGLE_CLOUD_PROJECT}/traces/${traceId}`
|
|
69
|
+
: undefined,
|
|
70
|
+
spanId: spanId,
|
|
71
|
+
...options,
|
|
72
|
+
});
|
|
73
|
+
};
|
|
74
|
+
exports.default = {
|
|
75
|
+
log,
|
|
76
|
+
levels,
|
|
77
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type":"commonjs"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
declare const _default: {
|
|
2
|
+
HTTP_CODES: {
|
|
3
|
+
BAD_REQUEST: number;
|
|
4
|
+
UNPROCESSABLE_ENTITY: number;
|
|
5
|
+
INTERNAL_SERVER_ERROR: number;
|
|
6
|
+
BAD_GATEWAY: number;
|
|
7
|
+
};
|
|
8
|
+
ERROR: {
|
|
9
|
+
UNABLE_TO_GENERATE_CONTENT: string;
|
|
10
|
+
UNEXPECTED_ERROR: string;
|
|
11
|
+
UNSUPPORTED_MODEL: string;
|
|
12
|
+
};
|
|
13
|
+
};
|
|
14
|
+
export default _default;
|
|
15
|
+
//# sourceMappingURL=const.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"const.d.ts","sourceRoot":"","sources":["../../src/const.ts"],"names":[],"mappings":";;;;;;;;;;;;;AAAA,wBAaE"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export default {
|
|
2
|
+
HTTP_CODES: {
|
|
3
|
+
BAD_REQUEST: 400,
|
|
4
|
+
UNPROCESSABLE_ENTITY: 422,
|
|
5
|
+
INTERNAL_SERVER_ERROR: 500,
|
|
6
|
+
BAD_GATEWAY: 502,
|
|
7
|
+
},
|
|
8
|
+
ERROR: {
|
|
9
|
+
UNABLE_TO_GENERATE_CONTENT: "Unable to generate content",
|
|
10
|
+
UNEXPECTED_ERROR: "An unexpected error occured. If the issue persists please contact the StackFactor support team at support@stackfactor.ai",
|
|
11
|
+
UNSUPPORTED_MODEL: "The specified model is not supported",
|
|
12
|
+
},
|
|
13
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
declare const _default: {
|
|
2
|
+
/**
|
|
3
|
+
* Creates a structured error object with a code, message, and optional details.
|
|
4
|
+
* This is used as a convenience factory to produce consistently shaped error payloads
|
|
5
|
+
* that can be passed to other error-handling methods in this module.
|
|
6
|
+
*
|
|
7
|
+
* @param errorCode - The numeric HTTP status code or application error code representing the error.
|
|
8
|
+
* @param errorMessage - A human-readable description of the error.
|
|
9
|
+
* @param details - Optional supplementary information about the error, such as validation details.
|
|
10
|
+
* @returns A plain object with `code`, `message`, and `details` properties.
|
|
11
|
+
*/
|
|
12
|
+
create: (errorCode: number, errorMessage: string, details?: any) => {
|
|
13
|
+
code: number;
|
|
14
|
+
message: string;
|
|
15
|
+
details: any;
|
|
16
|
+
stack: string;
|
|
17
|
+
};
|
|
18
|
+
};
|
|
19
|
+
export default _default;
|
|
20
|
+
//# sourceMappingURL=errorHandling.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errorHandling.d.ts","sourceRoot":"","sources":["../../src/errorHandling.ts"],"names":[],"mappings":";IACE;;;;;;;;;OASG;wBACiB,MAAM,gBAAgB,MAAM,YAAW,GAAG;;;;;;;AAXhE,wBAoBE"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export default {
|
|
2
|
+
/**
|
|
3
|
+
* Creates a structured error object with a code, message, and optional details.
|
|
4
|
+
* This is used as a convenience factory to produce consistently shaped error payloads
|
|
5
|
+
* that can be passed to other error-handling methods in this module.
|
|
6
|
+
*
|
|
7
|
+
* @param errorCode - The numeric HTTP status code or application error code representing the error.
|
|
8
|
+
* @param errorMessage - A human-readable description of the error.
|
|
9
|
+
* @param details - Optional supplementary information about the error, such as validation details.
|
|
10
|
+
* @returns A plain object with `code`, `message`, and `details` properties.
|
|
11
|
+
*/
|
|
12
|
+
create: (errorCode, errorMessage, details = null) => {
|
|
13
|
+
const error = new Error(errorMessage);
|
|
14
|
+
return {
|
|
15
|
+
code: errorCode,
|
|
16
|
+
message: errorMessage,
|
|
17
|
+
details: details,
|
|
18
|
+
stack: error.stack,
|
|
19
|
+
};
|
|
20
|
+
},
|
|
21
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import constants from "./const.js";
|
|
2
|
+
import errorHandling from "./errorHandling.js";
|
|
3
|
+
import langChain from "./langChain.js";
|
|
4
|
+
import logger from "./logger.js";
|
|
5
|
+
export { constants };
|
|
6
|
+
export { errorHandling };
|
|
7
|
+
export { langChain };
|
|
8
|
+
export { logger };
|
|
9
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,SAAS,MAAM,YAAY,CAAC;AACnC,OAAO,aAAa,MAAM,oBAAoB,CAAC;AAC/C,OAAO,SAAS,MAAM,gBAAgB,CAAC;AACvC,OAAO,MAAM,MAAM,aAAa,CAAC;AAEjC,OAAO,EAAE,SAAS,EAAE,CAAC;AAErB,OAAO,EAAE,aAAa,EAAE,CAAC;AAEzB,OAAO,EAAE,SAAS,EAAE,CAAC;AAErB,OAAO,EAAE,MAAM,EAAE,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
declare const _default: {
|
|
2
|
+
createAIAgent: (name: string, modelName: string, systemPrompt: string, tools: any[], responseFormat: any, config: any, onReportProgress?: Function | null, minPercent?: number, maxPercent?: number) => any;
|
|
3
|
+
runAIAgent: (agent: any, prompt: string, config: any, onProgress?: Function | null) => Promise<any>;
|
|
4
|
+
runChatPromptWithModel: (modelName: string, config: any, prompt: any, onProgressReport: any) => any;
|
|
5
|
+
runPromptWithModel: (modelName: string, config: any, prompt: any, onProgressReport: any, minPercent?: number, maxPercent?: number, expectsJsonResponse?: boolean, schema?: any, agentName?: string, tools?: any[]) => Promise<any>;
|
|
6
|
+
runPromptWithModelForImageGeneration: (modelName: string, config: any, prompt: string, options?: any) => Promise<any>;
|
|
7
|
+
throwErrorIfNotSuccessful: (response: any) => string;
|
|
8
|
+
};
|
|
9
|
+
export default _default;
|
|
10
|
+
//# sourceMappingURL=langChain.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"langChain.d.ts","sourceRoot":"","sources":["../../src/langChain.ts"],"names":[],"mappings":";0BAoaQ,MAAM,aACD,MAAM,gBACH,MAAM,SACb,GAAG,EAAE,kBACI,GAAG,UACX,GAAG,qBACO,QAAQ,GAAG,IAAI,eACrB,MAAM,eACN,MAAM,KACjB,GAAG;wBAyCG,GAAG,UACF,MAAM,UACN,GAAG,eACC,QAAQ,GAAG,IAAI,KAC1B,OAAO,CAAC,GAAG,CAAC;wCAioBF,MAAM,UACT,GAAG,UACH,GAAG,oBACO,GAAG,KACpB,GAAG;oCApfO,MAAM,UACT,GAAG,UACH,GAAG,oBACO,GAAG,eACT,MAAM,eACN,MAAM,wBACG,OAAO,WACpB,GAAG,cACA,MAAM,UACV,GAAG,EAAE,KACX,OAAO,CAAC,GAAG,CAAC;sDAyyBF,MAAM,UACT,GAAG,UACH,MAAM,YACL,GAAG,KACX,OAAO,CAAC,GAAG,CAAC;0CAz2B8B,GAAG,KAAG,MAAM;;AAo6BzD,wBAOE"}
|