@matimo/core 0.1.0-alpha.4
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/LICENSE +21 -0
- package/dist/src/auth/oauth2-config.d.ts +104 -0
- package/dist/src/auth/oauth2-config.d.ts.map +1 -0
- package/dist/src/auth/oauth2-config.js +38 -0
- package/dist/src/auth/oauth2-config.js.map +1 -0
- package/dist/src/auth/oauth2-handler.d.ts +130 -0
- package/dist/src/auth/oauth2-handler.d.ts.map +1 -0
- package/dist/src/auth/oauth2-handler.js +265 -0
- package/dist/src/auth/oauth2-handler.js.map +1 -0
- package/dist/src/auth/oauth2-provider-loader.d.ts +68 -0
- package/dist/src/auth/oauth2-provider-loader.d.ts.map +1 -0
- package/dist/src/auth/oauth2-provider-loader.js +120 -0
- package/dist/src/auth/oauth2-provider-loader.js.map +1 -0
- package/dist/src/core/schema.d.ts +248 -0
- package/dist/src/core/schema.d.ts.map +1 -0
- package/dist/src/core/schema.js +182 -0
- package/dist/src/core/schema.js.map +1 -0
- package/dist/src/core/tool-loader.d.ts +45 -0
- package/dist/src/core/tool-loader.d.ts.map +1 -0
- package/dist/src/core/tool-loader.js +205 -0
- package/dist/src/core/tool-loader.js.map +1 -0
- package/dist/src/core/tool-registry.d.ts +48 -0
- package/dist/src/core/tool-registry.d.ts.map +1 -0
- package/dist/src/core/tool-registry.js +93 -0
- package/dist/src/core/tool-registry.js.map +1 -0
- package/dist/src/core/types.d.ts +157 -0
- package/dist/src/core/types.d.ts.map +1 -0
- package/dist/src/core/types.js +5 -0
- package/dist/src/core/types.js.map +1 -0
- package/dist/src/decorators/index.d.ts +2 -0
- package/dist/src/decorators/index.d.ts.map +1 -0
- package/dist/src/decorators/index.js +2 -0
- package/dist/src/decorators/index.js.map +1 -0
- package/dist/src/decorators/tool-decorator.d.ts +97 -0
- package/dist/src/decorators/tool-decorator.d.ts.map +1 -0
- package/dist/src/decorators/tool-decorator.js +157 -0
- package/dist/src/decorators/tool-decorator.js.map +1 -0
- package/dist/src/encodings/parameter-encoding.d.ts +51 -0
- package/dist/src/encodings/parameter-encoding.d.ts.map +1 -0
- package/dist/src/encodings/parameter-encoding.js +123 -0
- package/dist/src/encodings/parameter-encoding.js.map +1 -0
- package/dist/src/errors/matimo-error.d.ts +34 -0
- package/dist/src/errors/matimo-error.d.ts.map +1 -0
- package/dist/src/errors/matimo-error.js +49 -0
- package/dist/src/errors/matimo-error.js.map +1 -0
- package/dist/src/executors/command-executor.d.ts +19 -0
- package/dist/src/executors/command-executor.d.ts.map +1 -0
- package/dist/src/executors/command-executor.js +98 -0
- package/dist/src/executors/command-executor.js.map +1 -0
- package/dist/src/executors/function-executor.d.ts +23 -0
- package/dist/src/executors/function-executor.d.ts.map +1 -0
- package/dist/src/executors/function-executor.js +164 -0
- package/dist/src/executors/function-executor.js.map +1 -0
- package/dist/src/executors/http-executor.d.ts +26 -0
- package/dist/src/executors/http-executor.d.ts.map +1 -0
- package/dist/src/executors/http-executor.js +137 -0
- package/dist/src/executors/http-executor.js.map +1 -0
- package/dist/src/index.d.ts +26 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +26 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/integrations/langchain.d.ts +46 -0
- package/dist/src/integrations/langchain.d.ts.map +1 -0
- package/dist/src/integrations/langchain.js +197 -0
- package/dist/src/integrations/langchain.js.map +1 -0
- package/dist/src/matimo-instance.d.ts +124 -0
- package/dist/src/matimo-instance.d.ts.map +1 -0
- package/dist/src/matimo-instance.js +313 -0
- package/dist/src/matimo-instance.js.map +1 -0
- package/dist/tools/calculator/calculator.d.ts +26 -0
- package/dist/tools/calculator/calculator.d.ts.map +1 -0
- package/dist/tools/calculator/calculator.js +104 -0
- package/dist/tools/calculator/calculator.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/package.json +94 -0
- package/tools/calculator/calculator.ts +125 -0
- package/tools/calculator/definition.yaml +71 -0
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import { pathToFileURL } from 'node:url';
|
|
4
|
+
import axios from 'axios';
|
|
5
|
+
import { MatimoError, ErrorCode } from '../errors/matimo-error';
|
|
6
|
+
/**
|
|
7
|
+
* FunctionExecutor - Executes async functions
|
|
8
|
+
* Supports functions defined in:
|
|
9
|
+
* 1. Embedded code in tool YAML (legacy)
|
|
10
|
+
* 2. Colocated .ts files (recommended)
|
|
11
|
+
*
|
|
12
|
+
* For .ts files, the tool directory structure should be:
|
|
13
|
+
* tools/provider/tool-name/
|
|
14
|
+
* ├── definition.yaml
|
|
15
|
+
* └── tool-name.ts (exports default async function)
|
|
16
|
+
*/
|
|
17
|
+
export class FunctionExecutor {
|
|
18
|
+
constructor(toolsPath) {
|
|
19
|
+
this.toolsPath = toolsPath || process.cwd();
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Execute a tool that runs an async function
|
|
23
|
+
* Supports both embedded code and external .ts/.js files
|
|
24
|
+
*/
|
|
25
|
+
async execute(tool, params) {
|
|
26
|
+
if (tool.execution.type !== 'function') {
|
|
27
|
+
throw new MatimoError('Tool execution type is not function', ErrorCode.EXECUTION_FAILED, {
|
|
28
|
+
expectedType: 'function',
|
|
29
|
+
actualType: tool.execution.type,
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
const { code, timeout = 30000 } = tool.execution;
|
|
33
|
+
const startTime = Date.now();
|
|
34
|
+
if (!code || code.trim().length === 0) {
|
|
35
|
+
throw new MatimoError('Function code is empty', ErrorCode.EXECUTION_FAILED, {
|
|
36
|
+
toolName: tool.name,
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
return new Promise((resolve) => {
|
|
40
|
+
let timedOut = false;
|
|
41
|
+
let settled = false;
|
|
42
|
+
// Set up timeout that properly rejects
|
|
43
|
+
const timer = setTimeout(() => {
|
|
44
|
+
timedOut = true;
|
|
45
|
+
if (!settled) {
|
|
46
|
+
settled = true;
|
|
47
|
+
resolve({
|
|
48
|
+
success: false,
|
|
49
|
+
error: 'Function execution timeout',
|
|
50
|
+
duration: Date.now() - startTime,
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
}, timeout);
|
|
54
|
+
const cleanup = () => {
|
|
55
|
+
clearTimeout(timer);
|
|
56
|
+
};
|
|
57
|
+
const handleError = (error) => {
|
|
58
|
+
cleanup();
|
|
59
|
+
if (!settled) {
|
|
60
|
+
settled = true;
|
|
61
|
+
resolve({
|
|
62
|
+
success: false,
|
|
63
|
+
error: error instanceof Error ? error.message : String(error),
|
|
64
|
+
duration: Date.now() - startTime,
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
const handleSuccess = (data) => {
|
|
69
|
+
cleanup();
|
|
70
|
+
if (!settled) {
|
|
71
|
+
settled = true;
|
|
72
|
+
if (timedOut) {
|
|
73
|
+
resolve({
|
|
74
|
+
success: false,
|
|
75
|
+
error: 'Function execution timeout',
|
|
76
|
+
duration: Date.now() - startTime,
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
else {
|
|
80
|
+
resolve(data);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
try {
|
|
85
|
+
// Check if code is a file path (starts with ./ or contains .ts or .js)
|
|
86
|
+
if (code.includes('.ts') || code.includes('.js') || code.startsWith('./')) {
|
|
87
|
+
// Load from external file using dynamic import()
|
|
88
|
+
// This works with TypeScript via ESM import
|
|
89
|
+
// Resolve relative to the tool definition file location
|
|
90
|
+
let absolutePath;
|
|
91
|
+
if (tool._definitionPath) {
|
|
92
|
+
// Use the definition file directory as the base for relative paths
|
|
93
|
+
const definitionDir = path.dirname(tool._definitionPath);
|
|
94
|
+
absolutePath = path.resolve(definitionDir, code);
|
|
95
|
+
}
|
|
96
|
+
else {
|
|
97
|
+
// Fallback: use the old logic (for backward compatibility)
|
|
98
|
+
// Compute tool directory: tools/{provider}/{tool-name}/
|
|
99
|
+
const toolName = tool.name;
|
|
100
|
+
let toolDir;
|
|
101
|
+
if (toolName.includes('-')) {
|
|
102
|
+
const parts = toolName.split('-');
|
|
103
|
+
const provider = parts[0];
|
|
104
|
+
toolDir = path.join(this.toolsPath, provider, toolName);
|
|
105
|
+
}
|
|
106
|
+
else {
|
|
107
|
+
toolDir = path.join(this.toolsPath, toolName);
|
|
108
|
+
}
|
|
109
|
+
absolutePath = path.resolve(toolDir, code);
|
|
110
|
+
}
|
|
111
|
+
const fileUrl = pathToFileURL(absolutePath).href;
|
|
112
|
+
// Use dynamic import() for ESM/TypeScript compatibility with robust URL handling
|
|
113
|
+
import(fileUrl)
|
|
114
|
+
.then((module) => {
|
|
115
|
+
const fn = (module.default || module);
|
|
116
|
+
const result = fn(params);
|
|
117
|
+
// Handle both Promise and non-Promise returns
|
|
118
|
+
if (result instanceof Promise) {
|
|
119
|
+
result.then(handleSuccess).catch(handleError);
|
|
120
|
+
}
|
|
121
|
+
else {
|
|
122
|
+
handleSuccess(result);
|
|
123
|
+
}
|
|
124
|
+
})
|
|
125
|
+
.catch(handleError);
|
|
126
|
+
}
|
|
127
|
+
else {
|
|
128
|
+
// Execute embedded code (legacy) - create function from string
|
|
129
|
+
// SECURITY WARNING: Embedded code execution runs arbitrary JS with fs/path/axios access.
|
|
130
|
+
// This is a potential RCE vector if tool YAML files come from untrusted sources.
|
|
131
|
+
// Embedded code is DISABLED by default. Must explicitly opt-in via MATIMO_ALLOW_EMBEDDED_CODE=true
|
|
132
|
+
const embeddedCodeDisabled = process.env.MATIMO_ALLOW_EMBEDDED_CODE !== 'true';
|
|
133
|
+
if (embeddedCodeDisabled) {
|
|
134
|
+
throw new MatimoError('Embedded code execution is disabled by default for security. Use external .ts/.js files instead.', ErrorCode.EXECUTION_FAILED, {
|
|
135
|
+
toolName: tool.name,
|
|
136
|
+
recommendation: 'Create a separate .ts file in the tool directory instead of using embedded code',
|
|
137
|
+
enableFeatureFlag: 'Set MATIMO_ALLOW_EMBEDDED_CODE=true to enable (not recommended)',
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
// Log warning when embedded code is executed
|
|
141
|
+
console.info(`⚠️ Warning: Executing embedded code from tool '${tool.name}'. This carries security risks if tool YAML is from untrusted sources.`);
|
|
142
|
+
// In ESM modules, require is not available by default
|
|
143
|
+
// We pass a safe require function that embedded code can use
|
|
144
|
+
const functionBody = `return (${code})`;
|
|
145
|
+
const fn = new Function(functionBody)();
|
|
146
|
+
// Pass undefined for require in ESM - embedded code should use import syntax
|
|
147
|
+
const result = fn(params, {}, fs, path, axios, undefined);
|
|
148
|
+
// Handle both Promise and non-Promise returns
|
|
149
|
+
if (result instanceof Promise) {
|
|
150
|
+
result.then(handleSuccess).catch(handleError);
|
|
151
|
+
}
|
|
152
|
+
else {
|
|
153
|
+
handleSuccess(result);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
catch (error) {
|
|
158
|
+
handleError(error);
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
export default FunctionExecutor;
|
|
164
|
+
//# sourceMappingURL=function-executor.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"function-executor.js","sourceRoot":"","sources":["../../../src/executors/function-executor.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,IAAI,CAAC;AACpB,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AACzC,OAAO,KAAK,MAAM,OAAO,CAAC;AAE1B,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAC;AAEhE;;;;;;;;;;GAUG;AACH,MAAM,OAAO,gBAAgB;IAG3B,YAAY,SAAkB;QAC5B,IAAI,CAAC,SAAS,GAAG,SAAS,IAAI,OAAO,CAAC,GAAG,EAAE,CAAC;IAC9C,CAAC;IAED;;;OAGG;IACH,K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'../core/schema';
|
|
2
|
+
/**
|
|
3
|
+
* HttpExecutor - Executes HTTP requests
|
|
4
|
+
* Handles authentication, retries, and response validation
|
|
5
|
+
*/
|
|
6
|
+
export declare class HttpExecutor {
|
|
7
|
+
/**
|
|
8
|
+
* Execute a tool that makes an HTTP request
|
|
9
|
+
*/
|
|
10
|
+
execute(tool: ToolDefinition, params: Record<string, unknown>): Promise<unknown>;
|
|
11
|
+
/**
|
|
12
|
+
* Replace parameter placeholders in a string
|
|
13
|
+
*/
|
|
14
|
+
private templateString;
|
|
15
|
+
/**
|
|
16
|
+
* Build query string from query_params, only including provided values
|
|
17
|
+
*/
|
|
18
|
+
private buildQueryString;
|
|
19
|
+
/**
|
|
20
|
+
* Replace parameter placeholders in an object (headers, body)
|
|
21
|
+
* Recursively handles nested objects
|
|
22
|
+
*/
|
|
23
|
+
private templateObject;
|
|
24
|
+
}
|
|
25
|
+
export default HttpExecutor;
|
|
26
|
+
//# sourceMappingURL=http-executor.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"http-executor.d.ts","sourceRoot":"","sources":["../../../src/executors/http-executor.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAIhD;;;GAGG;AAEH,qBAAa,YAAY;IACvB;;OAEG;IACG,OAAO,CAAC,IAAI,EAAE,cAAc,EAAE,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,OAAO,CAAC,OAAO,CAAC;IAoFtF;;OAEG;IACH,OAAO,CAAC,cAAc;IAWtB;;OAEG;IACH,OAAO,CAAC,gBAAgB;IAexB;;;OAGG;IACH,OAAO,CAAC,cAAc;CA2BvB;AAED,eAAe,YAAY,CAAC"}
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import axios from 'axios';
|
|
2
|
+
import { applyParameterEncodings } from '../encodings/parameter-encoding';
|
|
3
|
+
import { MatimoError, ErrorCode } from '../errors/matimo-error';
|
|
4
|
+
/**
|
|
5
|
+
* HttpExecutor - Executes HTTP requests
|
|
6
|
+
* Handles authentication, retries, and response validation
|
|
7
|
+
*/
|
|
8
|
+
export class HttpExecutor {
|
|
9
|
+
/**
|
|
10
|
+
* Execute a tool that makes an HTTP request
|
|
11
|
+
*/
|
|
12
|
+
async execute(tool, params) {
|
|
13
|
+
if (tool.execution.type !== 'http') {
|
|
14
|
+
throw new MatimoError('Tool execution type is not http', ErrorCode.EXECUTION_FAILED, {
|
|
15
|
+
expectedType: 'http',
|
|
16
|
+
actualType: tool.execution.type,
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
const { method, url, headers = {}, body, timeout, query_params, parameter_encoding, } = tool.execution;
|
|
20
|
+
const queryParams = query_params;
|
|
21
|
+
const parameterEncodings = parameter_encoding;
|
|
22
|
+
// Apply parameter encodings (e.g., MIME encoding for email parameters)
|
|
23
|
+
let finalParams = params;
|
|
24
|
+
if (parameterEncodings && parameterEncodings.length > 0) {
|
|
25
|
+
finalParams = applyParameterEncodings(params, parameterEncodings);
|
|
26
|
+
}
|
|
27
|
+
// Implement parameter templating
|
|
28
|
+
let finalUrl = this.templateString(url, finalParams);
|
|
29
|
+
// Handle query parameters - only include non-empty ones
|
|
30
|
+
if (queryParams) {
|
|
31
|
+
const queryString = this.buildQueryString(queryParams, finalParams);
|
|
32
|
+
if (queryString) {
|
|
33
|
+
finalUrl += '?' + queryString;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
const templatedHeaders = this.templateObject(headers, finalParams);
|
|
37
|
+
const templatedBody = body && typeof body === 'object'
|
|
38
|
+
? this.templateObject(body, finalParams)
|
|
39
|
+
: body;
|
|
40
|
+
// Build request config
|
|
41
|
+
const requestConfig = {
|
|
42
|
+
method,
|
|
43
|
+
url: finalUrl,
|
|
44
|
+
};
|
|
45
|
+
if (Object.keys(templatedHeaders).length > 0) {
|
|
46
|
+
requestConfig.headers = templatedHeaders;
|
|
47
|
+
}
|
|
48
|
+
if (templatedBody !== undefined) {
|
|
49
|
+
requestConfig.data = templatedBody;
|
|
50
|
+
}
|
|
51
|
+
if (timeout !== undefined) {
|
|
52
|
+
requestConfig.timeout = timeout;
|
|
53
|
+
}
|
|
54
|
+
try {
|
|
55
|
+
const response = await axios.request(requestConfig);
|
|
56
|
+
const success = response.status >= 200 && response.status < 300;
|
|
57
|
+
return {
|
|
58
|
+
success,
|
|
59
|
+
data: response.data,
|
|
60
|
+
statusCode: response.status,
|
|
61
|
+
headers: response.headers,
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
catch (error) {
|
|
65
|
+
const axiosError = error;
|
|
66
|
+
const status = axiosError.response?.status || 500;
|
|
67
|
+
const details = axiosError.response?.data || {};
|
|
68
|
+
return {
|
|
69
|
+
success: false,
|
|
70
|
+
error: axiosError.message || String(error),
|
|
71
|
+
statusCode: status,
|
|
72
|
+
details,
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Replace parameter placeholders in a string
|
|
78
|
+
*/
|
|
79
|
+
templateString(str, params) {
|
|
80
|
+
let result = str;
|
|
81
|
+
for (const [key, value] of Object.entries(params)) {
|
|
82
|
+
const placeholder = `{${key}}`;
|
|
83
|
+
if (value !== undefined && value !== null && value !== '') {
|
|
84
|
+
result = result.replace(new RegExp(placeholder, 'g'), String(value));
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return result;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Build query string from query_params, only including provided values
|
|
91
|
+
*/
|
|
92
|
+
buildQueryString(queryParams, params) {
|
|
93
|
+
const parts = [];
|
|
94
|
+
for (const [key, template] of Object.entries(queryParams)) {
|
|
95
|
+
const value = this.templateString(template, params);
|
|
96
|
+
// Only include if not empty and didn't result in "{param}" (unfilled placeholder)
|
|
97
|
+
if (value && !value.startsWith('{')) {
|
|
98
|
+
parts.push(`${encodeURIComponent(key)}=${encodeURIComponent(value)}`);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return parts.join('&');
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Replace parameter placeholders in an object (headers, body)
|
|
105
|
+
* Recursively handles nested objects
|
|
106
|
+
*/
|
|
107
|
+
templateObject(obj, params) {
|
|
108
|
+
const result = {};
|
|
109
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
110
|
+
if (typeof value === 'string') {
|
|
111
|
+
result[key] = this.templateString(value, params);
|
|
112
|
+
}
|
|
113
|
+
else if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
|
|
114
|
+
// Recursively template nested objects
|
|
115
|
+
result[key] = this.templateObject(value, params);
|
|
116
|
+
}
|
|
117
|
+
else if (Array.isArray(value)) {
|
|
118
|
+
// Handle arrays of objects
|
|
119
|
+
result[key] = value.map((item) => {
|
|
120
|
+
if (typeof item === 'string') {
|
|
121
|
+
return this.templateString(item, params);
|
|
122
|
+
}
|
|
123
|
+
else if (typeof item === 'object' && item !== null && !Array.isArray(item)) {
|
|
124
|
+
return this.templateObject(item, params);
|
|
125
|
+
}
|
|
126
|
+
return item;
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
else {
|
|
130
|
+
result[key] = value;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
return result;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
export default HttpExecutor;
|
|
137
|
+
//# sourceMappingURL=http-executor.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"http-executor.js","sourceRoot":"","sources":["../../../src/executors/http-executor.ts"],"names":[],"mappings":"AAAA,OAAO,KAAyC,MAAM,OAAO,CAAC;AAE9D,OAAO,EAAE,uBAAuB,EAAE,MAAM,iCAAiC,CAAC;AAC1E,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAC;AAEhE;;;GAGG;AAEH,MAAM,OAAO,YAAY;IACvB;;OAEG;IACH,KAAK,CAAC,OA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integrate 1000+ tools
|
|
5
|
+
* across any AI framework (LangChain, CrewAI, Anthropic SDK, etc.).
|
|
6
|
+
*/
|
|
7
|
+
export type { Parameter, AuthConfig, HttpExecution, CommandExecution, FunctionExecution, OutputSchema, RateLimitConfig, ErrorHandlingConfig, ToolDefinition, } from './core/types';
|
|
8
|
+
export { ParameterSchema, AuthConfigSchema, ExecutionConfigSchema } from './core/schema';
|
|
9
|
+
export { ToolLoader } from './core/tool-loader';
|
|
10
|
+
export { ToolRegistry } from './core/tool-registry';
|
|
11
|
+
export { CommandExecutor } from './executors/command-executor';
|
|
12
|
+
export { HttpExecutor } from './executors/http-executor';
|
|
13
|
+
export { FunctionExecutor } from './executors/function-executor';
|
|
14
|
+
export { applyParameterEncodings } from './encodings/parameter-encoding';
|
|
15
|
+
export type { ParameterEncodingConfig } from './encodings/parameter-encoding';
|
|
16
|
+
export { tool, setGlobalMatimoInstance, getGlobalMatimoInstance, } from './decorators/tool-decorator';
|
|
17
|
+
export { MatimoError, ErrorCode, createValidationError, createExecutionError, } from './errors/matimo-error';
|
|
18
|
+
export { MatimoInstance, matimo } from './matimo-instance';
|
|
19
|
+
export type { InitOptions } from './matimo-instance';
|
|
20
|
+
export type { OAuth2Provider, OAuth2Token, OAuth2Config, AuthorizationOptions, TokenResponse, OAuth2Endpoints, } from './auth/oauth2-config';
|
|
21
|
+
export type { ProviderDefinition } from './core/schema';
|
|
22
|
+
export { OAuth2ProviderLoader } from './auth/oauth2-provider-loader';
|
|
23
|
+
export { OAuth2Handler } from './auth/oauth2-handler';
|
|
24
|
+
export { convertToolsToLangChain } from './integrations/langchain';
|
|
25
|
+
export type { LangChainTool } from './integrations/langchain';
|
|
26
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,YAAY,EACV,SAAS,EACT,UAAU,EACV,aAAa,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
+
/**
|
|
2
|
+
* Matimo - Universal AI Agent Tools Ecosystem
|
|
3
|
+
*
|
|
4
|
+
* Framework-agnostic SDK that enables any developer to integrate 1000+ tools
|
|
5
|
+
* across any AI framework (LangChain, CrewAI, Anthropic SDK, etc.).
|
|
6
|
+
*/
|
|
7
|
+
export { ParameterSchema, AuthConfigSchema, ExecutionConfigSchema } from './core/schema';
|
|
8
|
+
export { ToolLoader } from './core/tool-loader';
|
|
9
|
+
export { ToolRegistry } from './core/tool-registry';
|
|
10
|
+
// Executors
|
|
11
|
+
export { CommandExecutor } from './executors/command-executor';
|
|
12
|
+
export { HttpExecutor } from './executors/http-executor';
|
|
13
|
+
export { FunctionExecutor } from './executors/function-executor';
|
|
14
|
+
// Parameter Encoding
|
|
15
|
+
export { applyParameterEncodings } from './encodings/parameter-encoding';
|
|
16
|
+
// Decorators
|
|
17
|
+
export { tool, setGlobalMatimoInstance, getGlobalMatimoInstance, } from './decorators/tool-decorator';
|
|
18
|
+
// Error handling
|
|
19
|
+
export { MatimoError, ErrorCode, createValidationError, createExecutionError, } from './errors/matimo-error';
|
|
20
|
+
// Matimo instance and namespace
|
|
21
|
+
export { MatimoInstance, matimo } from './matimo-instance';
|
|
22
|
+
export { OAuth2ProviderLoader } from './auth/oauth2-provider-loader';
|
|
23
|
+
export { OAuth2Handler } from './auth/oauth2-handler';
|
|
24
|
+
// LangChain integration
|
|
25
|
+
export { convertToolsToLangChain } from './integrations/langchain';
|
|
26
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAcH,OAAO,EAAE,eAAe,EAAE,gBAAgB,EAAE,qBAAqB,EAAE,MAAM,eAAe,CAAC;AACzF,OAAO,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAChD,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AAEpD,YAAY;AACZ,OAAO,EAAE,eAAe,EAAE,MAAM,8BAA8B,CAAC;AAC/D,OAAO,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC;AACzD,OAAO,EAAE,gBAAgB,EAAE,MAAM,+BAA+B,CAAC;AAEjE,qBAAqB;AACrB,OAAO,EAAE,uBAAuB,EAAE,MAAM,gCAAgC,CAAC;AAGzE,aAAa;AACb,OAAO,EACL,IAAI,EACJ,uBAAuB,EACvB,uBAAuB,GACxB,MAAM,6BAA6B,CAAC;AAErC,iBAAiB;AACjB,OAAO,EACL,WAAW,EACX,SAAS,EACT,qBAAqB,EACrB,oBAAoB,GACrB,MAAM,uBAAuB,CAAC;AAE/B,gCAAgC;AAChC,OAAO,EAAE,cAAc,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAa3D,OAAO,EAAE,oBAAoB,EAAE,MAAM,+BAA+B,CAAC;AACrE,OAAO,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AAEtD,wBAAwB;AACxB,OAAO,EAAE,uBAAuB,EAAE,MAAM,0BAA0B,CAAC"}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LangChain Integration for Matimo
|
|
3
|
+
*
|
|
4
|
+
* Converts Matimo tools to LangChain-compatible format.
|
|
5
|
+
* Simple, lightweight, scales to 2000+ tools.
|
|
6
|
+
*
|
|
7
|
+
* NOTE: Requires @langchain/core as peer dependency.
|
|
8
|
+
* Install with: npm install @langchain/core langchain
|
|
9
|
+
*
|
|
10
|
+
* Usage:
|
|
11
|
+
* const matimo = await MatimoInstance.init('./tools');
|
|
12
|
+
* const langchainTools = await convertToolsToLangChain(
|
|
13
|
+
* matimo.listTools(),
|
|
14
|
+
* matimo,
|
|
15
|
+
* { SLACK_BOT_TOKEN: process.env.SLACK_BOT_TOKEN }
|
|
16
|
+
* );
|
|
17
|
+
*/
|
|
18
|
+
import { z } from 'zod';
|
|
19
|
+
import type { ToolDefinition } from '../core/types';
|
|
20
|
+
import type { MatimoInstance } from '../matimo-instance';
|
|
21
|
+
export interface LangChainTool {
|
|
22
|
+
name: string;
|
|
23
|
+
description: string;
|
|
24
|
+
schema: z.ZodSchema;
|
|
25
|
+
invoke: (input: Record<string, unknown>) => Promise<unknown>;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Convert Matimo tools to LangChain format
|
|
29
|
+
*
|
|
30
|
+
* @param tools - Matimo tools
|
|
31
|
+
* @param matimo - MatimoInstance
|
|
32
|
+
* @param secrets - Map of parameter names to secret values
|
|
33
|
+
* @param secretParamNames - Explicitly declared secret parameters (optional)
|
|
34
|
+
* @returns LangChain tools
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```ts
|
|
38
|
+
* const tools = await convertToolsToLangChain(
|
|
39
|
+
* matimo.listTools().filter(t => t.name.startsWith('slack')),
|
|
40
|
+
* matimo,
|
|
41
|
+
* { SLACK_BOT_TOKEN: process.env.SLACK_BOT_TOKEN }
|
|
42
|
+
* );
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
export declare function convertToolsToLangChain(tools: ToolDefinition[], matimo: MatimoInstance, secrets?: Record<string, string>, secretParamNames?: Set<string>): Promise<LangChainTool[]>;
|
|
46
|
+
//# sourceMappingURL=langchain.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"langchain.d.ts","sourceRoot":"","sources":["../../../src/integrations/langchain.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAEH,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,KAAK,EAAE,cAAc,EAAa,MAAM,eAAe,CAAC;AAC/D,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AAGzD,MAAM,WAAW,aAAa;IAC5B,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,MAAM,CAAC;IACpB,MAAM,EAAE,CAAC,CAAC,SAAS,CAAC;IACpB,MAAM,EAAE,CAAC,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAK,OAAO,CAAC,OAAO,CAAC,CAAC;CAC9D;AAgMD;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAsB,uBAAuB,CAC3C,KAAK,EAAE,cAAc,EAAE,EACvB,MAAM,EAAE,cAAc,EACtB,OAAO,GAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAM,EACpC,gBAAgB,CAAC,EAAE,GAAG,CAAC,MAAM,CAAC,GAC7B,OAAO,CAAC,aAAa,EAAE,CAAC,CAiB1B"}
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LangChain Integration for Matimo
|
|
3
|
+
*
|
|
4
|
+
* Converts Matimo tools to LangChain-compatible format.
|
|
5
|
+
* Simple, lightweight, scales to 2000+ tools.
|
|
6
|
+
*
|
|
7
|
+
* NOTE: Requires @langchain/core as peer dependency.
|
|
8
|
+
* Install with: npm install @langchain/core langchain
|
|
9
|
+
*
|
|
10
|
+
* Usage:
|
|
11
|
+
* const matimo = await MatimoInstance.init('./tools');
|
|
12
|
+
* const langchainTools = await convertToolsToLangChain(
|
|
13
|
+
* matimo.listTools(),
|
|
14
|
+
* matimo,
|
|
15
|
+
* { SLACK_BOT_TOKEN: process.env.SLACK_BOT_TOKEN }
|
|
16
|
+
* );
|
|
17
|
+
*/
|
|
18
|
+
import { z } from 'zod';
|
|
19
|
+
// Lazy load LangChain to avoid hard dependency
|
|
20
|
+
let langChainToolFn = null;
|
|
21
|
+
async function getLangChainTool() {
|
|
22
|
+
if (!langChainToolFn) {
|
|
23
|
+
try {
|
|
24
|
+
const langChainModule = await import('@langchain/core/tools');
|
|
25
|
+
langChainToolFn = langChainModule.tool;
|
|
26
|
+
}
|
|
27
|
+
catch {
|
|
28
|
+
throw new Error('LangChain not installed. Install: npm install @langchain/core langchain');
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return langChainToolFn;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Convert parameter to Zod schema
|
|
35
|
+
*
|
|
36
|
+
* Supports:
|
|
37
|
+
* - enum constraints (if present, validates against allowed values)
|
|
38
|
+
* - default values (sets default in schema)
|
|
39
|
+
* - type validation (string, number, boolean, array, object)
|
|
40
|
+
* - description and required metadata
|
|
41
|
+
*/
|
|
42
|
+
function parameterToZod(param) {
|
|
43
|
+
let schema;
|
|
44
|
+
// If enum is present, validate against allowed values
|
|
45
|
+
if (param.enum && param.enum.length > 0) {
|
|
46
|
+
// Create enum schema from allowed values using z.union for mixed types
|
|
47
|
+
const enumSchemas = param.enum.map((value) => z.literal(value));
|
|
48
|
+
// Build union from array of literal schemas (type-safe via unknown cast)
|
|
49
|
+
schema = z.union(enumSchemas);
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
switch (param.type) {
|
|
53
|
+
case 'string':
|
|
54
|
+
schema = z.string();
|
|
55
|
+
break;
|
|
56
|
+
case 'number':
|
|
57
|
+
schema = z.number();
|
|
58
|
+
break;
|
|
59
|
+
case 'boolean':
|
|
60
|
+
schema = z.boolean();
|
|
61
|
+
break;
|
|
62
|
+
case 'array': {
|
|
63
|
+
const itemSchema = param.items ? parameterToZod(param.items) : z.unknown();
|
|
64
|
+
schema = z.array(itemSchema);
|
|
65
|
+
break;
|
|
66
|
+
}
|
|
67
|
+
case 'object': {
|
|
68
|
+
if (param.properties) {
|
|
69
|
+
const props = {};
|
|
70
|
+
for (const [key, prop] of Object.entries(param.properties)) {
|
|
71
|
+
props[key] = parameterToZod(prop);
|
|
72
|
+
}
|
|
73
|
+
schema = z.object(props);
|
|
74
|
+
}
|
|
75
|
+
else {
|
|
76
|
+
schema = z.record(z.string(), z.unknown());
|
|
77
|
+
}
|
|
78
|
+
break;
|
|
79
|
+
}
|
|
80
|
+
default:
|
|
81
|
+
schema = z.unknown();
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
if (param.description) {
|
|
85
|
+
schema = schema.describe(param.description);
|
|
86
|
+
}
|
|
87
|
+
// Apply default value if present
|
|
88
|
+
if (param.default !== undefined) {
|
|
89
|
+
schema = schema.default(param.default);
|
|
90
|
+
}
|
|
91
|
+
if (!param.required) {
|
|
92
|
+
schema = schema.optional();
|
|
93
|
+
}
|
|
94
|
+
return schema;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Auto-detect if a parameter name looks like a secret
|
|
98
|
+
* based on common patterns (TOKEN, KEY, SECRET, PASSWORD)
|
|
99
|
+
*
|
|
100
|
+
* Uses word-boundary matching and camelCase detection to avoid false positives:
|
|
101
|
+
* - ✅ Matches: "api_token", "API_KEY", "getToken", "secret", "password_hash"
|
|
102
|
+
* - ❌ Rejects: "monkey", "turkey_id", "donkey" (substrings only)
|
|
103
|
+
*/
|
|
104
|
+
function isSecretParameter(paramName) {
|
|
105
|
+
const upperName = paramName.toUpperCase();
|
|
106
|
+
// Pattern 1: Word boundaries (works for snake_case: api_token, API_KEY)
|
|
107
|
+
if (/\b(TOKEN|KEY|SECRET|PASSWORD)\b/.test(upperName)) {
|
|
108
|
+
return true;
|
|
109
|
+
}
|
|
110
|
+
// Pattern 2: Underscore prefix/suffix (works for snake_case: _token_, prefix_token_)
|
|
111
|
+
if (/(^|_)(TOKEN|KEY|SECRET|PASSWORD)(_|$)/.test(upperName)) {
|
|
112
|
+
return true;
|
|
113
|
+
}
|
|
114
|
+
// Pattern 3: CamelCase detection (works for camelCase: getToken, apiKey)
|
|
115
|
+
// Check if secret word appears after a lowercase letter (camelCase boundary)
|
|
116
|
+
if (/[a-z](TOKEN|KEY|SECRET|PASSWORD)/.test(paramName)) {
|
|
117
|
+
return true;
|
|
118
|
+
}
|
|
119
|
+
return false;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Build Zod schema for tool input, excluding secret parameters
|
|
123
|
+
*/
|
|
124
|
+
function buildInputSchema(tool, secretParams) {
|
|
125
|
+
if (!tool.parameters) {
|
|
126
|
+
return z.object({});
|
|
127
|
+
}
|
|
128
|
+
const shape = {};
|
|
129
|
+
for (const [name, param] of Object.entries(tool.parameters)) {
|
|
130
|
+
if (secretParams.has(name)) {
|
|
131
|
+
continue; // Skip secrets - they're injected
|
|
132
|
+
}
|
|
133
|
+
shape[name] = parameterToZod(param);
|
|
134
|
+
}
|
|
135
|
+
return z.object(shape);
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Convert Matimo tool to LangChain format
|
|
139
|
+
*/
|
|
140
|
+
async function convertTool(matimo, tool, secretParams, secrets) {
|
|
141
|
+
const toolFn = await getLangChainTool();
|
|
142
|
+
const schema = buildInputSchema(tool, secretParams);
|
|
143
|
+
return toolFn(async (input) => {
|
|
144
|
+
const params = { ...input };
|
|
145
|
+
// Inject secrets
|
|
146
|
+
for (const param of secretParams) {
|
|
147
|
+
if (param in secrets) {
|
|
148
|
+
params[param] = secrets[param];
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
try {
|
|
152
|
+
return await matimo.execute(tool.name, params);
|
|
153
|
+
}
|
|
154
|
+
catch (error) {
|
|
155
|
+
return `Error: ${error instanceof Error ? error.message : String(error)}`;
|
|
156
|
+
}
|
|
157
|
+
}, {
|
|
158
|
+
name: tool.name,
|
|
159
|
+
description: tool.description || tool.name,
|
|
160
|
+
schema,
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Convert Matimo tools to LangChain format
|
|
165
|
+
*
|
|
166
|
+
* @param tools - Matimo tools
|
|
167
|
+
* @param matimo - MatimoInstance
|
|
168
|
+
* @param secrets - Map of parameter names to secret values
|
|
169
|
+
* @param secretParamNames - Explicitly declared secret parameters (optional)
|
|
170
|
+
* @returns LangChain tools
|
|
171
|
+
*
|
|
172
|
+
* @example
|
|
173
|
+
* ```ts
|
|
174
|
+
* const tools = await convertToolsToLangChain(
|
|
175
|
+
* matimo.listTools().filter(t => t.name.startsWith('slack')),
|
|
176
|
+
* matimo,
|
|
177
|
+
* { SLACK_BOT_TOKEN: process.env.SLACK_BOT_TOKEN }
|
|
178
|
+
* );
|
|
179
|
+
* ```
|
|
180
|
+
*/
|
|
181
|
+
export async function convertToolsToLangChain(tools, matimo, secrets = {}, secretParamNames) {
|
|
182
|
+
// Start with explicitly declared secret param names or auto-detect from secrets keys
|
|
183
|
+
const detectedSecrets = secretParamNames || new Set(Object.keys(secrets));
|
|
184
|
+
// Auto-detect additional secret parameters by scanning all tool parameters
|
|
185
|
+
for (const tool of tools) {
|
|
186
|
+
if (tool.parameters) {
|
|
187
|
+
for (const paramName of Object.keys(tool.parameters)) {
|
|
188
|
+
// Auto-detect if parameter looks like a secret
|
|
189
|
+
if (isSecretParameter(paramName)) {
|
|
190
|
+
detectedSecrets.add(paramName);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
return Promise.all(tools.map((tool) => convertTool(matimo, tool, detectedSecrets, secrets)));
|
|
196
|
+
}
|
|
197
|
+
//# sourceMappingURL=langchain.js.map
|