@mynitorai/sdk 0.1.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.
@@ -0,0 +1,23 @@
1
+ /**
2
+ * MyNitor AI TypeScript SDK
3
+ * The "One-Line Magic" for AI Production Safety.
4
+ */
5
+ export interface MyNitorConfig {
6
+ apiKey: string;
7
+ environment?: string;
8
+ endpoint?: string;
9
+ }
10
+ export declare class MyNitor {
11
+ private static instance;
12
+ private config;
13
+ private isInstrumented;
14
+ private constructor();
15
+ static init(config: MyNitorConfig): MyNitor;
16
+ /**
17
+ * Automatically detect and wrap AI libraries like OpenAI
18
+ */
19
+ instrument(): void;
20
+ private sendEvent;
21
+ private wrapOpenAI;
22
+ }
23
+ export declare const init: typeof MyNitor.init;
package/dist/index.js ADDED
@@ -0,0 +1,104 @@
1
+ "use strict";
2
+ /**
3
+ * MyNitor AI TypeScript SDK
4
+ * The "One-Line Magic" for AI Production Safety.
5
+ */
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.init = exports.MyNitor = void 0;
8
+ class MyNitor {
9
+ constructor(config) {
10
+ this.isInstrumented = false;
11
+ this.config = {
12
+ environment: 'production',
13
+ endpoint: 'https://app.mynitor.ai/api/v1/events',
14
+ ...config
15
+ };
16
+ }
17
+ static init(config) {
18
+ if (!MyNitor.instance) {
19
+ MyNitor.instance = new MyNitor(config);
20
+ }
21
+ return MyNitor.instance;
22
+ }
23
+ /**
24
+ * Automatically detect and wrap AI libraries like OpenAI
25
+ */
26
+ instrument() {
27
+ if (this.isInstrumented)
28
+ return;
29
+ this.wrapOpenAI();
30
+ this.isInstrumented = true;
31
+ console.log('🚀 MyNitor: Auto-instrumentation active.');
32
+ }
33
+ async sendEvent(payload) {
34
+ try {
35
+ // Fire and forget - we don't await this to keep the user's app fast
36
+ fetch(this.config.endpoint, {
37
+ method: 'POST',
38
+ headers: {
39
+ 'Content-Type': 'application/json',
40
+ 'Authorization': `Bearer ${this.config.apiKey}`
41
+ },
42
+ body: JSON.stringify({
43
+ ...payload,
44
+ environment: this.config.environment,
45
+ eventVersion: '1.0'
46
+ })
47
+ }).catch(() => {
48
+ /* Silently fail to protect the user's production app */
49
+ });
50
+ }
51
+ catch (e) {
52
+ /* Silently fail */
53
+ }
54
+ }
55
+ wrapOpenAI() {
56
+ try {
57
+ // Detect if OpenAI is installed
58
+ const OpenAI = require('openai');
59
+ if (!OpenAI || !OpenAI.OpenAI)
60
+ return;
61
+ const self = this;
62
+ const originalChatCreate = OpenAI.OpenAI.Chat.Completions.prototype.create;
63
+ OpenAI.OpenAI.Chat.Completions.prototype.create = async function (...args) {
64
+ const start = Date.now();
65
+ const body = args[0];
66
+ try {
67
+ const result = await originalChatCreate.apply(this, args);
68
+ const end = Date.now();
69
+ // Background capture
70
+ self.sendEvent({
71
+ requestId: result.id || `req_${Date.now()}`,
72
+ model: result.model || body.model,
73
+ provider: 'openai',
74
+ inputTokens: result.usage?.prompt_tokens || 0,
75
+ outputTokens: result.usage?.completion_tokens || 0,
76
+ latencyMs: end - start,
77
+ status: 'success'
78
+ });
79
+ return result;
80
+ }
81
+ catch (error) {
82
+ const end = Date.now();
83
+ self.sendEvent({
84
+ requestId: `err_${Date.now()}`,
85
+ model: body?.model || 'unknown',
86
+ provider: 'openai',
87
+ inputTokens: 0,
88
+ outputTokens: 0,
89
+ latencyMs: end - start,
90
+ status: 'error',
91
+ errorType: error?.constructor?.name || 'Error'
92
+ });
93
+ throw error;
94
+ }
95
+ };
96
+ }
97
+ catch (e) {
98
+ // Library not found or version mismatch - skip silently
99
+ }
100
+ }
101
+ }
102
+ exports.MyNitor = MyNitor;
103
+ // Global accessor for snippet simplicity
104
+ exports.init = MyNitor.init;
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "@mynitorai/sdk",
3
+ "version": "0.1.0",
4
+ "description": "Production safety and observability for AI systems.",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "files": [
8
+ "dist"
9
+ ],
10
+ "scripts": {
11
+ "build": "tsc",
12
+ "prepublishOnly": "npm run build"
13
+ },
14
+ "keywords": [
15
+ "ai",
16
+ "observability",
17
+ "monitoring",
18
+ "openai",
19
+ "anthropic",
20
+ "llm",
21
+ "safety"
22
+ ],
23
+ "author": "MyNitor AI",
24
+ "license": "MIT",
25
+ "peerDependencies": {
26
+ "openai": "^4.0.0"
27
+ },
28
+ "devDependencies": {
29
+ "typescript": "^5.0.0",
30
+ "openai": "^4.0.0",
31
+ "@types/node": "^20.0.0"
32
+ }
33
+ }