@mcpmesh/sdk 0.8.0-beta.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/dist/__tests__/claude-handler.test.d.ts +7 -0
- package/dist/__tests__/claude-handler.test.d.ts.map +1 -0
- package/dist/__tests__/claude-handler.test.js +455 -0
- package/dist/__tests__/claude-handler.test.js.map +1 -0
- package/dist/__tests__/config.test.d.ts +7 -0
- package/dist/__tests__/config.test.d.ts.map +1 -0
- package/dist/__tests__/config.test.js +156 -0
- package/dist/__tests__/config.test.js.map +1 -0
- package/dist/__tests__/errors.test.d.ts +7 -0
- package/dist/__tests__/errors.test.d.ts.map +1 -0
- package/dist/__tests__/errors.test.js +170 -0
- package/dist/__tests__/errors.test.js.map +1 -0
- package/dist/__tests__/generic-handler.test.d.ts +7 -0
- package/dist/__tests__/generic-handler.test.d.ts.map +1 -0
- package/dist/__tests__/generic-handler.test.js +243 -0
- package/dist/__tests__/generic-handler.test.js.map +1 -0
- package/dist/__tests__/llm-provider.test.d.ts +7 -0
- package/dist/__tests__/llm-provider.test.d.ts.map +1 -0
- package/dist/__tests__/llm-provider.test.js +217 -0
- package/dist/__tests__/llm-provider.test.js.map +1 -0
- package/dist/__tests__/openai-handler.test.d.ts +7 -0
- package/dist/__tests__/openai-handler.test.d.ts.map +1 -0
- package/dist/__tests__/openai-handler.test.js +359 -0
- package/dist/__tests__/openai-handler.test.js.map +1 -0
- package/dist/__tests__/provider-handler-registry.test.d.ts +9 -0
- package/dist/__tests__/provider-handler-registry.test.d.ts.map +1 -0
- package/dist/__tests__/provider-handler-registry.test.js +187 -0
- package/dist/__tests__/provider-handler-registry.test.js.map +1 -0
- package/dist/__tests__/response-parser.test.d.ts +7 -0
- package/dist/__tests__/response-parser.test.d.ts.map +1 -0
- package/dist/__tests__/response-parser.test.js +360 -0
- package/dist/__tests__/response-parser.test.js.map +1 -0
- package/dist/__tests__/route.test.d.ts +7 -0
- package/dist/__tests__/route.test.d.ts.map +1 -0
- package/dist/__tests__/route.test.js +281 -0
- package/dist/__tests__/route.test.js.map +1 -0
- package/dist/__tests__/sse.test.d.ts +7 -0
- package/dist/__tests__/sse.test.d.ts.map +1 -0
- package/dist/__tests__/sse.test.js +172 -0
- package/dist/__tests__/sse.test.js.map +1 -0
- package/dist/__tests__/template.test.d.ts +7 -0
- package/dist/__tests__/template.test.d.ts.map +1 -0
- package/dist/__tests__/template.test.js +176 -0
- package/dist/__tests__/template.test.js.map +1 -0
- package/dist/__tests__/tracing.test.d.ts +7 -0
- package/dist/__tests__/tracing.test.d.ts.map +1 -0
- package/dist/__tests__/tracing.test.js +264 -0
- package/dist/__tests__/tracing.test.js.map +1 -0
- package/dist/agent.d.ts +165 -0
- package/dist/agent.d.ts.map +1 -0
- package/dist/agent.js +626 -0
- package/dist/agent.js.map +1 -0
- package/dist/api-runtime.d.ts +166 -0
- package/dist/api-runtime.d.ts.map +1 -0
- package/dist/api-runtime.js +459 -0
- package/dist/api-runtime.js.map +1 -0
- package/dist/config.d.ts +31 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +60 -0
- package/dist/config.js.map +1 -0
- package/dist/debug.d.ts +47 -0
- package/dist/debug.d.ts.map +1 -0
- package/dist/debug.js +86 -0
- package/dist/debug.js.map +1 -0
- package/dist/errors.d.ts +99 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +110 -0
- package/dist/errors.js.map +1 -0
- package/dist/express.d.ts +146 -0
- package/dist/express.d.ts.map +1 -0
- package/dist/express.js +371 -0
- package/dist/express.js.map +1 -0
- package/dist/index.d.ts +96 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +107 -0
- package/dist/index.js.map +1 -0
- package/dist/llm-agent.d.ts +193 -0
- package/dist/llm-agent.d.ts.map +1 -0
- package/dist/llm-agent.js +634 -0
- package/dist/llm-agent.js.map +1 -0
- package/dist/llm-provider.d.ts +323 -0
- package/dist/llm-provider.d.ts.map +1 -0
- package/dist/llm-provider.js +446 -0
- package/dist/llm-provider.js.map +1 -0
- package/dist/llm.d.ts +194 -0
- package/dist/llm.d.ts.map +1 -0
- package/dist/llm.js +304 -0
- package/dist/llm.js.map +1 -0
- package/dist/provider-handlers/claude-handler.d.ts +98 -0
- package/dist/provider-handlers/claude-handler.d.ts.map +1 -0
- package/dist/provider-handlers/claude-handler.js +268 -0
- package/dist/provider-handlers/claude-handler.js.map +1 -0
- package/dist/provider-handlers/gemini-handler.d.ts +82 -0
- package/dist/provider-handlers/gemini-handler.d.ts.map +1 -0
- package/dist/provider-handlers/gemini-handler.js +152 -0
- package/dist/provider-handlers/gemini-handler.js.map +1 -0
- package/dist/provider-handlers/generic-handler.d.ts +78 -0
- package/dist/provider-handlers/generic-handler.d.ts.map +1 -0
- package/dist/provider-handlers/generic-handler.js +152 -0
- package/dist/provider-handlers/generic-handler.js.map +1 -0
- package/dist/provider-handlers/index.d.ts +29 -0
- package/dist/provider-handlers/index.d.ts.map +1 -0
- package/dist/provider-handlers/index.js +32 -0
- package/dist/provider-handlers/index.js.map +1 -0
- package/dist/provider-handlers/openai-handler.d.ts +86 -0
- package/dist/provider-handlers/openai-handler.d.ts.map +1 -0
- package/dist/provider-handlers/openai-handler.js +160 -0
- package/dist/provider-handlers/openai-handler.js.map +1 -0
- package/dist/provider-handlers/provider-handler-registry.d.ts +124 -0
- package/dist/provider-handlers/provider-handler-registry.d.ts.map +1 -0
- package/dist/provider-handlers/provider-handler-registry.js +180 -0
- package/dist/provider-handlers/provider-handler-registry.js.map +1 -0
- package/dist/provider-handlers/provider-handler.d.ts +245 -0
- package/dist/provider-handlers/provider-handler.d.ts.map +1 -0
- package/dist/provider-handlers/provider-handler.js +238 -0
- package/dist/provider-handlers/provider-handler.js.map +1 -0
- package/dist/proxy.d.ts +44 -0
- package/dist/proxy.d.ts.map +1 -0
- package/dist/proxy.js +324 -0
- package/dist/proxy.js.map +1 -0
- package/dist/response-parser.d.ts +103 -0
- package/dist/response-parser.d.ts.map +1 -0
- package/dist/response-parser.js +232 -0
- package/dist/response-parser.js.map +1 -0
- package/dist/route.d.ts +185 -0
- package/dist/route.d.ts.map +1 -0
- package/dist/route.js +310 -0
- package/dist/route.js.map +1 -0
- package/dist/sse.d.ts +45 -0
- package/dist/sse.d.ts.map +1 -0
- package/dist/sse.js +77 -0
- package/dist/sse.js.map +1 -0
- package/dist/template.d.ts +86 -0
- package/dist/template.d.ts.map +1 -0
- package/dist/template.js +206 -0
- package/dist/template.js.map +1 -0
- package/dist/tracing.d.ts +88 -0
- package/dist/tracing.d.ts.map +1 -0
- package/dist/tracing.js +193 -0
- package/dist/tracing.js.map +1 -0
- package/dist/types.d.ts +618 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +5 -0
- package/dist/types.js.map +1 -0
- package/package.json +68 -0
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Template rendering for LLM system prompts using Handlebars.
|
|
3
|
+
*
|
|
4
|
+
* Supports both inline templates and file-based templates using the `file://` prefix.
|
|
5
|
+
* Handlebars was chosen for its popularity in the TypeScript/JavaScript ecosystem.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```typescript
|
|
9
|
+
* // Inline template
|
|
10
|
+
* const rendered = await renderTemplate(
|
|
11
|
+
* "Hello, {{name}}! You have {{count}} items.",
|
|
12
|
+
* { name: "User", count: 5 }
|
|
13
|
+
* );
|
|
14
|
+
*
|
|
15
|
+
* // File-based template
|
|
16
|
+
* const rendered = await renderTemplate(
|
|
17
|
+
* "file://prompts/assistant.hbs",
|
|
18
|
+
* { user: "John", context: { topic: "math" } }
|
|
19
|
+
* );
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
import Handlebars from "handlebars";
|
|
23
|
+
/**
|
|
24
|
+
* Check if a template string is a file reference.
|
|
25
|
+
*/
|
|
26
|
+
export declare function isFileTemplate(template: string): boolean;
|
|
27
|
+
/**
|
|
28
|
+
* Extract the file path from a file:// template reference.
|
|
29
|
+
*/
|
|
30
|
+
export declare function extractFilePath(template: string): string;
|
|
31
|
+
/**
|
|
32
|
+
* Render a template with the given context.
|
|
33
|
+
*
|
|
34
|
+
* @param template - Template string (inline) or file reference ("file://path/to/template.hbs")
|
|
35
|
+
* @param context - Context object for template variables
|
|
36
|
+
* @returns Rendered string
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* ```typescript
|
|
40
|
+
* // Inline template
|
|
41
|
+
* const result = await renderTemplate("Hello {{name}}!", { name: "World" });
|
|
42
|
+
* // => "Hello World!"
|
|
43
|
+
*
|
|
44
|
+
* // File template (prompts/assistant.hbs)
|
|
45
|
+
* const result = await renderTemplate("file://prompts/assistant.hbs", {
|
|
46
|
+
* user: { name: "John" },
|
|
47
|
+
* preferences: ["math", "science"],
|
|
48
|
+
* });
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
export declare function renderTemplate(template: string, context?: Record<string, unknown>): Promise<string>;
|
|
52
|
+
/**
|
|
53
|
+
* Clear the template cache.
|
|
54
|
+
* Useful for development/testing when templates are being modified.
|
|
55
|
+
*/
|
|
56
|
+
export declare function clearTemplateCache(): void;
|
|
57
|
+
/**
|
|
58
|
+
* Register a custom Handlebars helper.
|
|
59
|
+
*
|
|
60
|
+
* @param name - Helper name
|
|
61
|
+
* @param fn - Helper function
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
64
|
+
* ```typescript
|
|
65
|
+
* registerHelper("uppercase", (str) => str.toUpperCase());
|
|
66
|
+
* // Template: "Hello {{uppercase name}}!"
|
|
67
|
+
* // Context: { name: "world" }
|
|
68
|
+
* // Result: "Hello WORLD!"
|
|
69
|
+
* ```
|
|
70
|
+
*/
|
|
71
|
+
export declare function registerHelper(name: string, fn: Handlebars.HelperDelegate): void;
|
|
72
|
+
/**
|
|
73
|
+
* Register a partial template.
|
|
74
|
+
*
|
|
75
|
+
* @param name - Partial name
|
|
76
|
+
* @param template - Template string
|
|
77
|
+
*
|
|
78
|
+
* @example
|
|
79
|
+
* ```typescript
|
|
80
|
+
* registerPartial("header", "<h1>{{title}}</h1>");
|
|
81
|
+
* // Template: "{{> header title='Welcome'}}"
|
|
82
|
+
* // Result: "<h1>Welcome</h1>"
|
|
83
|
+
* ```
|
|
84
|
+
*/
|
|
85
|
+
export declare function registerPartial(name: string, template: string): void;
|
|
86
|
+
//# sourceMappingURL=template.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"template.d.ts","sourceRoot":"","sources":["../src/template.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH,OAAO,UAAU,MAAM,YAAY,CAAC;AAUpC;;GAEG;AACH,wBAAgB,cAAc,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAExD;AAED;;GAEG;AACH,wBAAgB,eAAe,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,CAKxD;AAuED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAsB,cAAc,CAClC,QAAQ,EAAE,MAAM,EAChB,OAAO,GAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAM,GACpC,OAAO,CAAC,MAAM,CAAC,CAGjB;AAED;;;GAGG;AACH,wBAAgB,kBAAkB,IAAI,IAAI,CAEzC;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,cAAc,CAC5B,IAAI,EAAE,MAAM,EACZ,EAAE,EAAE,UAAU,CAAC,cAAc,GAC5B,IAAI,CAEN;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,eAAe,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,IAAI,CAEpE"}
|
package/dist/template.js
ADDED
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Template rendering for LLM system prompts using Handlebars.
|
|
3
|
+
*
|
|
4
|
+
* Supports both inline templates and file-based templates using the `file://` prefix.
|
|
5
|
+
* Handlebars was chosen for its popularity in the TypeScript/JavaScript ecosystem.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```typescript
|
|
9
|
+
* // Inline template
|
|
10
|
+
* const rendered = await renderTemplate(
|
|
11
|
+
* "Hello, {{name}}! You have {{count}} items.",
|
|
12
|
+
* { name: "User", count: 5 }
|
|
13
|
+
* );
|
|
14
|
+
*
|
|
15
|
+
* // File-based template
|
|
16
|
+
* const rendered = await renderTemplate(
|
|
17
|
+
* "file://prompts/assistant.hbs",
|
|
18
|
+
* { user: "John", context: { topic: "math" } }
|
|
19
|
+
* );
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
import Handlebars from "handlebars";
|
|
23
|
+
import * as fs from "fs/promises";
|
|
24
|
+
import * as path from "path";
|
|
25
|
+
/**
|
|
26
|
+
* Cache for compiled Handlebars templates.
|
|
27
|
+
* Key is the template string (for inline) or resolved file path (for file://).
|
|
28
|
+
*/
|
|
29
|
+
const templateCache = new Map();
|
|
30
|
+
/**
|
|
31
|
+
* Check if a template string is a file reference.
|
|
32
|
+
*/
|
|
33
|
+
export function isFileTemplate(template) {
|
|
34
|
+
return template.startsWith("file://");
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Extract the file path from a file:// template reference.
|
|
38
|
+
*/
|
|
39
|
+
export function extractFilePath(template) {
|
|
40
|
+
if (!isFileTemplate(template)) {
|
|
41
|
+
throw new Error(`Not a file template: ${template}`);
|
|
42
|
+
}
|
|
43
|
+
return template.slice(7); // Remove "file://" prefix
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Resolve a file path relative to the caller's location.
|
|
47
|
+
* If the path is absolute, it's returned as-is.
|
|
48
|
+
* If relative, it's resolved from the current working directory.
|
|
49
|
+
*/
|
|
50
|
+
function resolveTemplatePath(filePath) {
|
|
51
|
+
if (path.isAbsolute(filePath)) {
|
|
52
|
+
return filePath;
|
|
53
|
+
}
|
|
54
|
+
return path.resolve(process.cwd(), filePath);
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Load a template file and return its contents.
|
|
58
|
+
*/
|
|
59
|
+
async function loadTemplateFile(filePath) {
|
|
60
|
+
const resolvedPath = resolveTemplatePath(filePath);
|
|
61
|
+
try {
|
|
62
|
+
return await fs.readFile(resolvedPath, "utf-8");
|
|
63
|
+
}
|
|
64
|
+
catch (err) {
|
|
65
|
+
if (err.code === "ENOENT") {
|
|
66
|
+
throw new Error(`Template file not found: ${resolvedPath}`);
|
|
67
|
+
}
|
|
68
|
+
throw err;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Get or compile a Handlebars template.
|
|
73
|
+
* Templates are cached by their source (inline string or resolved absolute file path).
|
|
74
|
+
*/
|
|
75
|
+
async function getCompiledTemplate(template) {
|
|
76
|
+
// Determine cache key - use absolute path for file templates
|
|
77
|
+
let cacheKey;
|
|
78
|
+
let templateContent;
|
|
79
|
+
if (isFileTemplate(template)) {
|
|
80
|
+
const filePath = extractFilePath(template);
|
|
81
|
+
const absolutePath = resolveTemplatePath(filePath);
|
|
82
|
+
cacheKey = absolutePath; // Use absolute path as cache key
|
|
83
|
+
// Check cache first
|
|
84
|
+
const cached = templateCache.get(cacheKey);
|
|
85
|
+
if (cached) {
|
|
86
|
+
return cached;
|
|
87
|
+
}
|
|
88
|
+
templateContent = await loadTemplateFile(filePath);
|
|
89
|
+
}
|
|
90
|
+
else {
|
|
91
|
+
cacheKey = template; // Use inline template as cache key
|
|
92
|
+
// Check cache first
|
|
93
|
+
const cached = templateCache.get(cacheKey);
|
|
94
|
+
if (cached) {
|
|
95
|
+
return cached;
|
|
96
|
+
}
|
|
97
|
+
templateContent = template;
|
|
98
|
+
}
|
|
99
|
+
// Compile and cache
|
|
100
|
+
const compiled = Handlebars.compile(templateContent);
|
|
101
|
+
templateCache.set(cacheKey, compiled);
|
|
102
|
+
return compiled;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Render a template with the given context.
|
|
106
|
+
*
|
|
107
|
+
* @param template - Template string (inline) or file reference ("file://path/to/template.hbs")
|
|
108
|
+
* @param context - Context object for template variables
|
|
109
|
+
* @returns Rendered string
|
|
110
|
+
*
|
|
111
|
+
* @example
|
|
112
|
+
* ```typescript
|
|
113
|
+
* // Inline template
|
|
114
|
+
* const result = await renderTemplate("Hello {{name}}!", { name: "World" });
|
|
115
|
+
* // => "Hello World!"
|
|
116
|
+
*
|
|
117
|
+
* // File template (prompts/assistant.hbs)
|
|
118
|
+
* const result = await renderTemplate("file://prompts/assistant.hbs", {
|
|
119
|
+
* user: { name: "John" },
|
|
120
|
+
* preferences: ["math", "science"],
|
|
121
|
+
* });
|
|
122
|
+
* ```
|
|
123
|
+
*/
|
|
124
|
+
export async function renderTemplate(template, context = {}) {
|
|
125
|
+
const compiled = await getCompiledTemplate(template);
|
|
126
|
+
return compiled(context);
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Clear the template cache.
|
|
130
|
+
* Useful for development/testing when templates are being modified.
|
|
131
|
+
*/
|
|
132
|
+
export function clearTemplateCache() {
|
|
133
|
+
templateCache.clear();
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Register a custom Handlebars helper.
|
|
137
|
+
*
|
|
138
|
+
* @param name - Helper name
|
|
139
|
+
* @param fn - Helper function
|
|
140
|
+
*
|
|
141
|
+
* @example
|
|
142
|
+
* ```typescript
|
|
143
|
+
* registerHelper("uppercase", (str) => str.toUpperCase());
|
|
144
|
+
* // Template: "Hello {{uppercase name}}!"
|
|
145
|
+
* // Context: { name: "world" }
|
|
146
|
+
* // Result: "Hello WORLD!"
|
|
147
|
+
* ```
|
|
148
|
+
*/
|
|
149
|
+
export function registerHelper(name, fn) {
|
|
150
|
+
Handlebars.registerHelper(name, fn);
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Register a partial template.
|
|
154
|
+
*
|
|
155
|
+
* @param name - Partial name
|
|
156
|
+
* @param template - Template string
|
|
157
|
+
*
|
|
158
|
+
* @example
|
|
159
|
+
* ```typescript
|
|
160
|
+
* registerPartial("header", "<h1>{{title}}</h1>");
|
|
161
|
+
* // Template: "{{> header title='Welcome'}}"
|
|
162
|
+
* // Result: "<h1>Welcome</h1>"
|
|
163
|
+
* ```
|
|
164
|
+
*/
|
|
165
|
+
export function registerPartial(name, template) {
|
|
166
|
+
Handlebars.registerPartial(name, template);
|
|
167
|
+
}
|
|
168
|
+
// Register common helpers
|
|
169
|
+
/**
|
|
170
|
+
* JSON helper - stringify an object
|
|
171
|
+
* Usage: {{json data}}
|
|
172
|
+
*/
|
|
173
|
+
registerHelper("json", (context) => {
|
|
174
|
+
return JSON.stringify(context, null, 2);
|
|
175
|
+
});
|
|
176
|
+
/**
|
|
177
|
+
* Join helper - join array elements
|
|
178
|
+
* Usage: {{join items ", "}}
|
|
179
|
+
*/
|
|
180
|
+
registerHelper("join", (items, separator) => {
|
|
181
|
+
if (!Array.isArray(items))
|
|
182
|
+
return "";
|
|
183
|
+
return items.join(typeof separator === "string" ? separator : ", ");
|
|
184
|
+
});
|
|
185
|
+
/**
|
|
186
|
+
* Eq helper - equality check
|
|
187
|
+
* Usage: {{#if (eq a b)}}...{{/if}}
|
|
188
|
+
*/
|
|
189
|
+
registerHelper("eq", (a, b) => {
|
|
190
|
+
return a === b;
|
|
191
|
+
});
|
|
192
|
+
/**
|
|
193
|
+
* Ne helper - not equal check
|
|
194
|
+
* Usage: {{#if (ne a b)}}...{{/if}}
|
|
195
|
+
*/
|
|
196
|
+
registerHelper("ne", (a, b) => {
|
|
197
|
+
return a !== b;
|
|
198
|
+
});
|
|
199
|
+
/**
|
|
200
|
+
* Default helper - provide default value
|
|
201
|
+
* Usage: {{default value "fallback"}}
|
|
202
|
+
*/
|
|
203
|
+
registerHelper("default", (value, defaultValue) => {
|
|
204
|
+
return value ?? defaultValue;
|
|
205
|
+
});
|
|
206
|
+
//# sourceMappingURL=template.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":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c,CAAC,SAAS,EAAE,CAAC,KAAc,EAAE,YAAqB,EAAE,EAAE;IAClE,OAAO,KAAK,IAAI,YAAY,CAAC;AAC/B,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Distributed tracing for MCP Mesh TypeScript SDK.
|
|
3
|
+
*
|
|
4
|
+
* Publishes trace spans to Redis via Rust core for distributed tracing.
|
|
5
|
+
* Uses the same trace format as Python SDK for interoperability.
|
|
6
|
+
*/
|
|
7
|
+
export interface TraceContext {
|
|
8
|
+
traceId: string;
|
|
9
|
+
parentSpanId: string | null;
|
|
10
|
+
}
|
|
11
|
+
export interface AgentMetadata {
|
|
12
|
+
agentId: string;
|
|
13
|
+
agentName: string;
|
|
14
|
+
agentNamespace: string;
|
|
15
|
+
agentHostname: string;
|
|
16
|
+
agentIp: string;
|
|
17
|
+
agentPort: number;
|
|
18
|
+
agentEndpoint: string;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Initialize the tracing system.
|
|
22
|
+
*
|
|
23
|
+
* Must be called before publishing spans. Checks if tracing is enabled
|
|
24
|
+
* and initializes the Redis connection via Rust core.
|
|
25
|
+
*
|
|
26
|
+
* @returns true if tracing is enabled and ready, false otherwise.
|
|
27
|
+
*/
|
|
28
|
+
export declare function initTracing(agentMetadata: AgentMetadata): Promise<boolean>;
|
|
29
|
+
/**
|
|
30
|
+
* Check if tracing is currently available.
|
|
31
|
+
*/
|
|
32
|
+
export declare function isTracingAvailable(): Promise<boolean>;
|
|
33
|
+
/**
|
|
34
|
+
* Generate a new trace ID (OpenTelemetry compliant).
|
|
35
|
+
*
|
|
36
|
+
* @returns 32-character hex string (128-bit trace ID per OTel spec)
|
|
37
|
+
*/
|
|
38
|
+
export declare function generateTraceId(): string;
|
|
39
|
+
/**
|
|
40
|
+
* Generate a new span ID (OpenTelemetry compliant).
|
|
41
|
+
*
|
|
42
|
+
* @returns 16-character hex string (64-bit span ID per OTel spec)
|
|
43
|
+
*/
|
|
44
|
+
export declare function generateSpanId(): string;
|
|
45
|
+
/**
|
|
46
|
+
* Parse trace context from HTTP headers.
|
|
47
|
+
*
|
|
48
|
+
* Looks for X-Trace-ID and X-Parent-Span headers (matching Python SDK).
|
|
49
|
+
*/
|
|
50
|
+
export declare function parseTraceContext(headers: Record<string, string | undefined>): TraceContext | null;
|
|
51
|
+
/**
|
|
52
|
+
* Create trace headers to propagate context to downstream calls.
|
|
53
|
+
* Uses Python SDK convention: X-Trace-ID, X-Parent-Span
|
|
54
|
+
*/
|
|
55
|
+
export declare function createTraceHeaders(traceId: string, spanId: string): Record<string, string>;
|
|
56
|
+
/**
|
|
57
|
+
* Span data for publishing.
|
|
58
|
+
*/
|
|
59
|
+
export interface SpanData {
|
|
60
|
+
traceId: string;
|
|
61
|
+
spanId: string;
|
|
62
|
+
parentSpan: string | null;
|
|
63
|
+
functionName: string;
|
|
64
|
+
startTime: number;
|
|
65
|
+
endTime: number;
|
|
66
|
+
durationMs: number;
|
|
67
|
+
success: boolean;
|
|
68
|
+
error: string | null;
|
|
69
|
+
resultType: string;
|
|
70
|
+
argsCount: number;
|
|
71
|
+
kwargsCount: number;
|
|
72
|
+
dependencies: string[];
|
|
73
|
+
injectedDependencies: number;
|
|
74
|
+
meshPositions: number[];
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Publish a trace span to Redis via Rust core.
|
|
78
|
+
*
|
|
79
|
+
* Non-blocking - silently handles failures to never break agent operations.
|
|
80
|
+
*/
|
|
81
|
+
export declare function publishTraceSpan(span: SpanData): Promise<boolean>;
|
|
82
|
+
/**
|
|
83
|
+
* Create a traced wrapper for a tool execution function.
|
|
84
|
+
*
|
|
85
|
+
* Automatically captures timing, success/error status, and publishes spans.
|
|
86
|
+
*/
|
|
87
|
+
export declare function createTracedExecutor<TArgs, TResult>(functionName: string, dependencies: string[], injectedDependencies: number, executor: (args: TArgs, traceContext: TraceContext | null) => Promise<TResult>): (args: TArgs, traceContext: TraceContext | null) => Promise<TResult>;
|
|
88
|
+
//# sourceMappingURL=tracing.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tracing.d.ts","sourceRoot":"","sources":["../src/tracing.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAWH,MAAM,WAAW,YAAY;IAC3B,OAAO,EAAE,MAAM,CAAC;IAChB,YAAY,EAAE,MAAM,GAAG,IAAI,CAAC;CAC7B;AAGD,MAAM,WAAW,aAAa;IAC5B,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,EAAE,MAAM,CAAC;IAClB,cAAc,EAAE,MAAM,CAAC;IACvB,aAAa,EAAE,MAAM,CAAC;IACtB,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,EAAE,MAAM,CAAC;IAClB,aAAa,EAAE,MAAM,CAAC;CACvB;AAOD;;;;;;;GAOG;AACH,wBAAsB,WAAW,CAAC,aAAa,EAAE,aAAa,GAAG,OAAO,CAAC,OAAO,CAAC,CA0BhF;AAED;;GAEG;AACH,wBAAsB,kBAAkB,IAAI,OAAO,CAAC,OAAO,CAAC,CAG3D;AAED;;;;GAIG;AACH,wBAAgB,eAAe,IAAI,MAAM,CAExC;AAED;;;;GAIG;AACH,wBAAgB,cAAc,IAAI,MAAM,CAEvC;AAED;;;;GAIG;AACH,wBAAgB,iBAAiB,CAC/B,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC,GAC1C,YAAY,GAAG,IAAI,CAWrB;AAED;;;GAGG;AACH,wBAAgB,kBAAkB,CAChC,OAAO,EAAE,MAAM,EACf,MAAM,EAAE,MAAM,GACb,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAKxB;AAED;;GAEG;AACH,MAAM,WAAW,QAAQ;IACvB,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,EAAE,MAAM,CAAC;IACf,UAAU,EAAE,MAAM,GAAG,IAAI,CAAC;IAC1B,YAAY,EAAE,MAAM,CAAC;IACrB,SAAS,EAAE,MAAM,CAAC;IAClB,OAAO,EAAE,MAAM,CAAC;IAChB,UAAU,EAAE,MAAM,CAAC;IACnB,OAAO,EAAE,OAAO,CAAC;IACjB,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IACrB,UAAU,EAAE,MAAM,CAAC;IACnB,SAAS,EAAE,MAAM,CAAC;IAClB,WAAW,EAAE,MAAM,CAAC;IACpB,YAAY,EAAE,MAAM,EAAE,CAAC;IACvB,oBAAoB,EAAE,MAAM,CAAC;IAC7B,aAAa,EAAE,MAAM,EAAE,CAAC;CACzB;AAED;;;;GAIG;AACH,wBAAsB,gBAAgB,CAAC,IAAI,EAAE,QAAQ,GAAG,OAAO,CAAC,OAAO,CAAC,CAsCvE;AAED;;;;GAIG;AACH,wBAAgB,oBAAoB,CAAC,KAAK,EAAE,OAAO,EACjD,YAAY,EAAE,MAAM,EACpB,YAAY,EAAE,MAAM,EAAE,EACtB,oBAAoB,EAAE,MAAM,EAC5B,QAAQ,EAAE,CAAC,IAAI,EAAE,KAAK,EAAE,YAAY,EAAE,YAAY,GAAG,IAAI,KAAK,OAAO,CAAC,OAAO,CAAC,GAC7E,CAAC,IAAI,EAAE,KAAK,EAAE,YAAY,EAAE,YAAY,GAAG,IAAI,KAAK,OAAO,CAAC,OAAO,CAAC,CAoDtE"}
|
package/dist/tracing.js
ADDED
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Distributed tracing for MCP Mesh TypeScript SDK.
|
|
3
|
+
*
|
|
4
|
+
* Publishes trace spans to Redis via Rust core for distributed tracing.
|
|
5
|
+
* Uses the same trace format as Python SDK for interoperability.
|
|
6
|
+
*/
|
|
7
|
+
import { randomUUID } from "crypto";
|
|
8
|
+
import { isTracingEnabled, initTracePublisher, publishSpan, isTracePublisherAvailable, } from "@mcpmesh/core";
|
|
9
|
+
// Global state
|
|
10
|
+
let tracingEnabled = false;
|
|
11
|
+
let tracingInitialized = false;
|
|
12
|
+
let currentAgentMetadata = null;
|
|
13
|
+
/**
|
|
14
|
+
* Initialize the tracing system.
|
|
15
|
+
*
|
|
16
|
+
* Must be called before publishing spans. Checks if tracing is enabled
|
|
17
|
+
* and initializes the Redis connection via Rust core.
|
|
18
|
+
*
|
|
19
|
+
* @returns true if tracing is enabled and ready, false otherwise.
|
|
20
|
+
*/
|
|
21
|
+
export async function initTracing(agentMetadata) {
|
|
22
|
+
if (tracingInitialized) {
|
|
23
|
+
return tracingEnabled;
|
|
24
|
+
}
|
|
25
|
+
tracingInitialized = true;
|
|
26
|
+
currentAgentMetadata = agentMetadata;
|
|
27
|
+
// Check if tracing is enabled via env var
|
|
28
|
+
tracingEnabled = isTracingEnabled();
|
|
29
|
+
if (!tracingEnabled) {
|
|
30
|
+
console.log("Distributed tracing: disabled");
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
console.log("Distributed tracing: enabled");
|
|
34
|
+
// Initialize Rust core trace publisher
|
|
35
|
+
const available = await initTracePublisher();
|
|
36
|
+
if (!available) {
|
|
37
|
+
console.warn("Rust core trace publisher initialization failed");
|
|
38
|
+
tracingEnabled = false;
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
return true;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Check if tracing is currently available.
|
|
45
|
+
*/
|
|
46
|
+
export async function isTracingAvailable() {
|
|
47
|
+
if (!tracingEnabled)
|
|
48
|
+
return false;
|
|
49
|
+
return await isTracePublisherAvailable();
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Generate a new trace ID (OpenTelemetry compliant).
|
|
53
|
+
*
|
|
54
|
+
* @returns 32-character hex string (128-bit trace ID per OTel spec)
|
|
55
|
+
*/
|
|
56
|
+
export function generateTraceId() {
|
|
57
|
+
return randomUUID().replace(/-/g, "");
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Generate a new span ID (OpenTelemetry compliant).
|
|
61
|
+
*
|
|
62
|
+
* @returns 16-character hex string (64-bit span ID per OTel spec)
|
|
63
|
+
*/
|
|
64
|
+
export function generateSpanId() {
|
|
65
|
+
return randomUUID().replace(/-/g, "").slice(0, 16);
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Parse trace context from HTTP headers.
|
|
69
|
+
*
|
|
70
|
+
* Looks for X-Trace-ID and X-Parent-Span headers (matching Python SDK).
|
|
71
|
+
*/
|
|
72
|
+
export function parseTraceContext(headers) {
|
|
73
|
+
// Check both cases: X-Trace-ID (Python) and X-Trace-Id (legacy)
|
|
74
|
+
const traceId = headers["x-trace-id"] || headers["X-Trace-ID"] || headers["X-Trace-Id"];
|
|
75
|
+
if (!traceId)
|
|
76
|
+
return null;
|
|
77
|
+
return {
|
|
78
|
+
traceId,
|
|
79
|
+
// Check both: X-Parent-Span (Python) and X-Parent-Span-Id (legacy)
|
|
80
|
+
parentSpanId: headers["x-parent-span"] || headers["X-Parent-Span"] ||
|
|
81
|
+
headers["x-parent-span-id"] || headers["X-Parent-Span-Id"] || null,
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Create trace headers to propagate context to downstream calls.
|
|
86
|
+
* Uses Python SDK convention: X-Trace-ID, X-Parent-Span
|
|
87
|
+
*/
|
|
88
|
+
export function createTraceHeaders(traceId, spanId) {
|
|
89
|
+
return {
|
|
90
|
+
"X-Trace-ID": traceId,
|
|
91
|
+
"X-Parent-Span": spanId,
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Publish a trace span to Redis via Rust core.
|
|
96
|
+
*
|
|
97
|
+
* Non-blocking - silently handles failures to never break agent operations.
|
|
98
|
+
*/
|
|
99
|
+
export async function publishTraceSpan(span) {
|
|
100
|
+
if (!tracingEnabled || !currentAgentMetadata) {
|
|
101
|
+
return false;
|
|
102
|
+
}
|
|
103
|
+
try {
|
|
104
|
+
// Convert span to string map for Redis storage
|
|
105
|
+
const spanMap = {
|
|
106
|
+
trace_id: span.traceId,
|
|
107
|
+
span_id: span.spanId,
|
|
108
|
+
parent_span: span.parentSpan ?? "null",
|
|
109
|
+
function_name: span.functionName,
|
|
110
|
+
start_time: String(span.startTime),
|
|
111
|
+
end_time: String(span.endTime),
|
|
112
|
+
duration_ms: span.durationMs.toFixed(2),
|
|
113
|
+
success: String(span.success),
|
|
114
|
+
error: span.error ?? "null",
|
|
115
|
+
result_type: span.resultType,
|
|
116
|
+
args_count: String(span.argsCount),
|
|
117
|
+
kwargs_count: String(span.kwargsCount),
|
|
118
|
+
dependencies: JSON.stringify(span.dependencies),
|
|
119
|
+
injected_dependencies: String(span.injectedDependencies),
|
|
120
|
+
mesh_positions: JSON.stringify(span.meshPositions),
|
|
121
|
+
// Agent metadata
|
|
122
|
+
agent_id: currentAgentMetadata.agentId,
|
|
123
|
+
agent_name: currentAgentMetadata.agentName,
|
|
124
|
+
agent_namespace: currentAgentMetadata.agentNamespace,
|
|
125
|
+
agent_hostname: currentAgentMetadata.agentHostname,
|
|
126
|
+
agent_ip: currentAgentMetadata.agentIp,
|
|
127
|
+
agent_port: String(currentAgentMetadata.agentPort),
|
|
128
|
+
agent_endpoint: currentAgentMetadata.agentEndpoint,
|
|
129
|
+
};
|
|
130
|
+
return await publishSpan(spanMap);
|
|
131
|
+
}
|
|
132
|
+
catch (err) {
|
|
133
|
+
// Non-blocking - never fail agent operations due to trace publishing
|
|
134
|
+
return false;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Create a traced wrapper for a tool execution function.
|
|
139
|
+
*
|
|
140
|
+
* Automatically captures timing, success/error status, and publishes spans.
|
|
141
|
+
*/
|
|
142
|
+
export function createTracedExecutor(functionName, dependencies, injectedDependencies, executor) {
|
|
143
|
+
return async (args, traceContext) => {
|
|
144
|
+
if (!tracingEnabled) {
|
|
145
|
+
return executor(args, traceContext);
|
|
146
|
+
}
|
|
147
|
+
// Generate span context
|
|
148
|
+
const traceId = traceContext?.traceId ?? generateTraceId();
|
|
149
|
+
const spanId = generateSpanId();
|
|
150
|
+
const parentSpan = traceContext?.parentSpanId ?? null;
|
|
151
|
+
const startTime = Date.now() / 1000;
|
|
152
|
+
let success = true;
|
|
153
|
+
let error = null;
|
|
154
|
+
let resultType = "unknown";
|
|
155
|
+
try {
|
|
156
|
+
// Execute with trace context for propagation
|
|
157
|
+
const newContext = { traceId, parentSpanId: spanId };
|
|
158
|
+
const result = await executor(args, newContext);
|
|
159
|
+
resultType = typeof result;
|
|
160
|
+
return result;
|
|
161
|
+
}
|
|
162
|
+
catch (err) {
|
|
163
|
+
success = false;
|
|
164
|
+
error = err instanceof Error ? err.message : String(err);
|
|
165
|
+
throw err;
|
|
166
|
+
}
|
|
167
|
+
finally {
|
|
168
|
+
const endTime = Date.now() / 1000;
|
|
169
|
+
const durationMs = (endTime - startTime) * 1000;
|
|
170
|
+
// Publish span asynchronously (fire and forget)
|
|
171
|
+
publishTraceSpan({
|
|
172
|
+
traceId,
|
|
173
|
+
spanId,
|
|
174
|
+
parentSpan,
|
|
175
|
+
functionName,
|
|
176
|
+
startTime,
|
|
177
|
+
endTime,
|
|
178
|
+
durationMs,
|
|
179
|
+
success,
|
|
180
|
+
error,
|
|
181
|
+
resultType,
|
|
182
|
+
argsCount: 0,
|
|
183
|
+
kwargsCount: typeof args === "object" && args !== null ? Object.keys(args).length : 0,
|
|
184
|
+
dependencies,
|
|
185
|
+
injectedDependencies,
|
|
186
|
+
meshPositions: [],
|
|
187
|
+
}).catch(() => {
|
|
188
|
+
// Silently ignore publish errors
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
//# sourceMappingURL=tracing.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tracing.js","sourceRoot":"","sources":["../src/tracing.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,QAAQ,CAAC;AACpC,OAA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