@matimo/core 0.1.0-alpha.10
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/README.md +252 -0
- package/destructive-keywords.yaml +70 -0
- package/dist/approval/approval-handler.d.ts +74 -0
- package/dist/approval/approval-handler.d.ts.map +1 -0
- package/dist/approval/approval-handler.js +210 -0
- package/dist/approval/approval-handler.js.map +1 -0
- package/dist/auth/oauth2-config.d.ts +104 -0
- package/dist/auth/oauth2-config.d.ts.map +1 -0
- package/dist/auth/oauth2-config.js +38 -0
- package/dist/auth/oauth2-config.js.map +1 -0
- package/dist/auth/oauth2-handler.d.ts +130 -0
- package/dist/auth/oauth2-handler.d.ts.map +1 -0
- package/dist/auth/oauth2-handler.js +265 -0
- package/dist/auth/oauth2-handler.js.map +1 -0
- package/dist/auth/oauth2-provider-loader.d.ts +68 -0
- package/dist/auth/oauth2-provider-loader.d.ts.map +1 -0
- package/dist/auth/oauth2-provider-loader.js +120 -0
- package/dist/auth/oauth2-provider-loader.js.map +1 -0
- package/dist/core/schema.d.ts +259 -0
- package/dist/core/schema.d.ts.map +1 -0
- package/dist/core/schema.js +187 -0
- package/dist/core/schema.js.map +1 -0
- package/dist/core/tool-loader.d.ts +57 -0
- package/dist/core/tool-loader.d.ts.map +1 -0
- package/dist/core/tool-loader.js +250 -0
- package/dist/core/tool-loader.js.map +1 -0
- package/dist/core/tool-registry.d.ts +48 -0
- package/dist/core/tool-registry.d.ts.map +1 -0
- package/dist/core/tool-registry.js +93 -0
- package/dist/core/tool-registry.js.map +1 -0
- package/dist/core/types.d.ts +162 -0
- package/dist/core/types.d.ts.map +1 -0
- package/dist/core/types.js +5 -0
- package/dist/core/types.js.map +1 -0
- package/dist/decorators/index.d.ts +2 -0
- package/dist/decorators/index.d.ts.map +1 -0
- package/dist/decorators/index.js +2 -0
- package/dist/decorators/index.js.map +1 -0
- package/dist/decorators/tool-decorator.d.ts +97 -0
- package/dist/decorators/tool-decorator.d.ts.map +1 -0
- package/dist/decorators/tool-decorator.js +157 -0
- package/dist/decorators/tool-decorator.js.map +1 -0
- package/dist/encodings/parameter-encoding.d.ts +51 -0
- package/dist/encodings/parameter-encoding.d.ts.map +1 -0
- package/dist/encodings/parameter-encoding.js +124 -0
- package/dist/encodings/parameter-encoding.js.map +1 -0
- package/dist/errors/matimo-error.d.ts +41 -0
- package/dist/errors/matimo-error.d.ts.map +1 -0
- package/dist/errors/matimo-error.js +71 -0
- package/dist/errors/matimo-error.js.map +1 -0
- package/dist/executors/command-executor.d.ts +19 -0
- package/dist/executors/command-executor.d.ts.map +1 -0
- package/dist/executors/command-executor.js +98 -0
- package/dist/executors/command-executor.js.map +1 -0
- package/dist/executors/function-executor.d.ts +23 -0
- package/dist/executors/function-executor.d.ts.map +1 -0
- package/dist/executors/function-executor.js +181 -0
- package/dist/executors/function-executor.js.map +1 -0
- package/dist/executors/http-executor.d.ts +78 -0
- package/dist/executors/http-executor.d.ts.map +1 -0
- package/dist/executors/http-executor.js +279 -0
- package/dist/executors/http-executor.js.map +1 -0
- package/dist/index.d.ts +30 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +33 -0
- package/dist/index.js.map +1 -0
- package/dist/integrations/langchain.d.ts +46 -0
- package/dist/integrations/langchain.d.ts.map +1 -0
- package/dist/integrations/langchain.js +197 -0
- package/dist/integrations/langchain.js.map +1 -0
- package/dist/logging/index.d.ts +3 -0
- package/dist/logging/index.d.ts.map +1 -0
- package/dist/logging/index.js +3 -0
- package/dist/logging/index.js.map +1 -0
- package/dist/logging/logger.d.ts +96 -0
- package/dist/logging/logger.d.ts.map +1 -0
- package/dist/logging/logger.js +53 -0
- package/dist/logging/logger.js.map +1 -0
- package/dist/logging/winston-logger.d.ts +29 -0
- package/dist/logging/winston-logger.d.ts.map +1 -0
- package/dist/logging/winston-logger.js +73 -0
- package/dist/logging/winston-logger.js.map +1 -0
- package/dist/matimo-instance.d.ts +140 -0
- package/dist/matimo-instance.d.ts.map +1 -0
- package/dist/matimo-instance.js +412 -0
- package/dist/matimo-instance.js.map +1 -0
- package/package.json +96 -0
- package/tools/calculator/calculator.ts +145 -0
- package/tools/calculator/definition.yaml +70 -0
- package/tools/edit/definition.yaml +115 -0
- package/tools/edit/edit.ts +187 -0
- package/tools/execute/definition.yaml +90 -0
- package/tools/execute/execute.ts +207 -0
- package/tools/read/definition.yaml +106 -0
- package/tools/read/read.ts +118 -0
- package/tools/search/definition.yaml +148 -0
- package/tools/search/search.ts +192 -0
- package/tools/web/definition.yaml +132 -0
- package/tools/web/web.ts +134 -0
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { spawn } from 'child_process';
|
|
2
|
+
import { MatimoError, ErrorCode } from '../errors/matimo-error';
|
|
3
|
+
/**
|
|
4
|
+
* CommandExecutor - Executes shell commands
|
|
5
|
+
* Handles parameter templating, timeouts, and error capture
|
|
6
|
+
*/
|
|
7
|
+
export class CommandExecutor {
|
|
8
|
+
constructor(cwd) {
|
|
9
|
+
this.cwd = cwd;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Execute a tool that runs a shell command
|
|
13
|
+
*/
|
|
14
|
+
async execute(tool, params) {
|
|
15
|
+
if (tool.execution.type !== 'command') {
|
|
16
|
+
throw new MatimoError('Tool execution type is not command', ErrorCode.EXECUTION_FAILED, {
|
|
17
|
+
expectedType: 'command',
|
|
18
|
+
actualType: tool.execution.type,
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
const { command, args = [], timeout = 30000 } = tool.execution;
|
|
22
|
+
const startTime = Date.now();
|
|
23
|
+
// Implement parameter templating
|
|
24
|
+
const templatedCommand = this.templateString(command, params);
|
|
25
|
+
const templatedArgs = args.map((arg) => this.templateString(arg, params));
|
|
26
|
+
return new Promise((resolve) => {
|
|
27
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
28
|
+
const spawnOptions = {
|
|
29
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
30
|
+
};
|
|
31
|
+
// Set working directory if provided
|
|
32
|
+
if (this.cwd) {
|
|
33
|
+
spawnOptions.cwd = this.cwd;
|
|
34
|
+
}
|
|
35
|
+
const child = spawn(templatedCommand, templatedArgs, spawnOptions);
|
|
36
|
+
let stdout = '';
|
|
37
|
+
let stderr = '';
|
|
38
|
+
let timedOut = false;
|
|
39
|
+
// Set up timeout
|
|
40
|
+
const timer = setTimeout(() => {
|
|
41
|
+
timedOut = true;
|
|
42
|
+
child.kill('SIGTERM');
|
|
43
|
+
}, timeout);
|
|
44
|
+
child.stdout?.on('data', (data) => {
|
|
45
|
+
stdout += data.toString();
|
|
46
|
+
});
|
|
47
|
+
child.stderr?.on('data', (data) => {
|
|
48
|
+
stderr += data.toString();
|
|
49
|
+
});
|
|
50
|
+
child.on('close', (code) => {
|
|
51
|
+
clearTimeout(timer);
|
|
52
|
+
const duration = Date.now() - startTime;
|
|
53
|
+
if (timedOut) {
|
|
54
|
+
resolve({
|
|
55
|
+
success: false,
|
|
56
|
+
error: 'timeout',
|
|
57
|
+
exitCode: -1,
|
|
58
|
+
duration,
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
const exitCode = code || 0;
|
|
63
|
+
const success = exitCode === 0;
|
|
64
|
+
resolve({
|
|
65
|
+
success,
|
|
66
|
+
stdout: stdout.trim(),
|
|
67
|
+
stderr: stderr.trim(),
|
|
68
|
+
exitCode,
|
|
69
|
+
duration,
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
child.on('error', (error) => {
|
|
74
|
+
clearTimeout(timer);
|
|
75
|
+
const duration = Date.now() - startTime;
|
|
76
|
+
resolve({
|
|
77
|
+
success: false,
|
|
78
|
+
error: error.message,
|
|
79
|
+
exitCode: -1,
|
|
80
|
+
duration,
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Replace parameter placeholders in a string
|
|
87
|
+
*/
|
|
88
|
+
templateString(str, params) {
|
|
89
|
+
let result = str;
|
|
90
|
+
for (const [key, value] of Object.entries(params)) {
|
|
91
|
+
const placeholder = `{${key}}`;
|
|
92
|
+
result = result.replace(new RegExp(placeholder, 'g'), String(value));
|
|
93
|
+
}
|
|
94
|
+
return result;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
export default CommandExecutor;
|
|
98
|
+
//# sourceMappingURL=command-executor.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"command-executor.js","sourceRoot":"","sources":["../../src/executors/command-executor.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AAEtC,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAC;AAEhE;;;GAGG;AAEH,MAAM,OAAO,eAAe;IAG1B,YAAY,GAAY;QACtB,IAAI,CAAC,GAAG,GAAG,GAAG,CAAC;IACjB,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,OAAO,CAAC,IAAoB,EAAE,MAA+B;QACjE,IAAI,IAAI,CAAC,SAAS,CAAC,IAAI,KAAK,SAAS,EAAE,CAAC;YACtC,MAAM,IAAI,WAAW,CAAC,oCAAoC,EAAE,SAAS,CAAC,gBAAgB,EAAE;gBACtF,YAAY,EAAE,SAAS;gBACvB,UAAU,EAAE,IAAI,CAAC,SAAS,CAAC,IAAI;aAChC,CAAC,CAAC;QACL,CAAC;QAED,MAAM,EAAE,OAAO,EAAE,IAAI,GAAG,EAAE,EAAE,OAAO,GAAG,KAAK,EAAE,GAAG,IAAI,CAAC,SAAS,CAAC;QAC/D,MAAM,SAAS,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;QAE7B,iCAAiC;QACjC,MAAM,gBAAgB,GAAG,IAAI,CAAC,cAAc,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;QAC9D,MAAM,aAAa,GAAG,IAAI,CAAC,GAAG,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AAC;QACvE,CAAC;QACD,OAAO,MAAM,CAAC;IAChB,CAAC;CACF;AAED,eAAe,eAAe,CAAC"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { ToolDefinition } from '../core/schema';
|
|
2
|
+
/**
|
|
3
|
+
* FunctionExecutor - Executes async functions
|
|
4
|
+
* Supports functions defined in:
|
|
5
|
+
* 1. Embedded code in tool YAML (legacy)
|
|
6
|
+
* 2. Colocated .ts files (recommended)
|
|
7
|
+
*
|
|
8
|
+
* For .ts files, the tool directory structure should be:
|
|
9
|
+
* tools/provider/tool-name/
|
|
10
|
+
* ├── definition.yaml
|
|
11
|
+
* └── tool-name.ts (exports default async function)
|
|
12
|
+
*/
|
|
13
|
+
export declare class FunctionExecutor {
|
|
14
|
+
private toolsPath;
|
|
15
|
+
constructor(toolsPath?: string);
|
|
16
|
+
/**
|
|
17
|
+
* Execute a tool that runs an async function
|
|
18
|
+
* Supports both embedded code and external .ts/.js files
|
|
19
|
+
*/
|
|
20
|
+
execute(tool: ToolDefinition, params: Record<string, unknown>): Promise<unknown>;
|
|
21
|
+
}
|
|
22
|
+
export default FunctionExecutor;
|
|
23
|
+
//# sourceMappingURL=function-executor.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"function-executor.d.ts","sourceRoot":"","sources":["../../src/executors/function-executor.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAIhD;;;;;;;;;;GAUG;AACH,qBAAa,gBAAgB;IAC3B,OAAO,CAAC,SAAS,CAAS;gBAEd,SAAS,CAAC,EAAE,MAAM;IAI9B;;;OAGG;IACG,OAAO,CAAC,IAAI,EAAE,cAAc,EAAE,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,OAAO,CAAC,OAAO,CAAC;CAkLvF;AAED,eAAe,gBAAgB,CAAC"}
|
|
@@ -0,0 +1,181 @@
|
|
|
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
|
+
import { getGlobalMatimoLogger } from '../logging/logger';
|
|
7
|
+
/**
|
|
8
|
+
* FunctionExecutor - Executes async functions
|
|
9
|
+
* Supports functions defined in:
|
|
10
|
+
* 1. Embedded code in tool YAML (legacy)
|
|
11
|
+
* 2. Colocated .ts files (recommended)
|
|
12
|
+
*
|
|
13
|
+
* For .ts files, the tool directory structure should be:
|
|
14
|
+
* tools/provider/tool-name/
|
|
15
|
+
* ├── definition.yaml
|
|
16
|
+
* └── tool-name.ts (exports default async function)
|
|
17
|
+
*/
|
|
18
|
+
export class FunctionExecutor {
|
|
19
|
+
constructor(toolsPath) {
|
|
20
|
+
this.toolsPath = toolsPath || process.cwd();
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Execute a tool that runs an async function
|
|
24
|
+
* Supports both embedded code and external .ts/.js files
|
|
25
|
+
*/
|
|
26
|
+
async execute(tool, params) {
|
|
27
|
+
if (tool.execution.type !== 'function') {
|
|
28
|
+
throw new MatimoError('Tool execution type is not function', ErrorCode.EXECUTION_FAILED, {
|
|
29
|
+
expectedType: 'function',
|
|
30
|
+
actualType: tool.execution.type,
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
const { code, timeout = 30000 } = tool.execution;
|
|
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 resolves with error
|
|
43
|
+
const timer = setTimeout(() => {
|
|
44
|
+
timedOut = true;
|
|
45
|
+
if (!settled) {
|
|
46
|
+
settled = true;
|
|
47
|
+
resolve({
|
|
48
|
+
success: false,
|
|
49
|
+
error: 'Function execution timeout',
|
|
50
|
+
code: ErrorCode.EXECUTION_FAILED,
|
|
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 with error object for tools to handle
|
|
62
|
+
if (error instanceof MatimoError) {
|
|
63
|
+
resolve({
|
|
64
|
+
success: false,
|
|
65
|
+
error: error.message,
|
|
66
|
+
code: error.code,
|
|
67
|
+
details: error.details,
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
else if (error instanceof Error) {
|
|
71
|
+
resolve({
|
|
72
|
+
success: false,
|
|
73
|
+
error: error.message,
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
resolve({
|
|
78
|
+
success: false,
|
|
79
|
+
error: String(error),
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
const handleSuccess = (data) => {
|
|
85
|
+
cleanup();
|
|
86
|
+
if (!settled) {
|
|
87
|
+
settled = true;
|
|
88
|
+
if (timedOut) {
|
|
89
|
+
resolve({
|
|
90
|
+
success: false,
|
|
91
|
+
error: 'Function execution timeout',
|
|
92
|
+
code: ErrorCode.EXECUTION_FAILED,
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
resolve(data);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
try {
|
|
101
|
+
// Check if code is a file path (starts with ./ or contains .ts or .js)
|
|
102
|
+
if (code.includes('.ts') || code.includes('.js') || code.startsWith('./')) {
|
|
103
|
+
// Load from external file using dynamic import()
|
|
104
|
+
// This works with TypeScript via ESM import
|
|
105
|
+
// Resolve relative to the tool definition file location
|
|
106
|
+
let absolutePath;
|
|
107
|
+
if (tool._definitionPath) {
|
|
108
|
+
// Use the definition file directory as the base for relative paths
|
|
109
|
+
const definitionDir = path.dirname(tool._definitionPath);
|
|
110
|
+
absolutePath = path.resolve(definitionDir, code);
|
|
111
|
+
}
|
|
112
|
+
else {
|
|
113
|
+
// Fallback: use the old logic (for backward compatibility)
|
|
114
|
+
// Compute tool directory: tools/{provider}/{tool-name}/
|
|
115
|
+
const toolName = tool.name;
|
|
116
|
+
let toolDir;
|
|
117
|
+
if (toolName.includes('-')) {
|
|
118
|
+
const parts = toolName.split('-');
|
|
119
|
+
const provider = parts[0];
|
|
120
|
+
toolDir = path.join(this.toolsPath, provider, toolName);
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
toolDir = path.join(this.toolsPath, toolName);
|
|
124
|
+
}
|
|
125
|
+
absolutePath = path.resolve(toolDir, code);
|
|
126
|
+
}
|
|
127
|
+
const fileUrl = pathToFileURL(absolutePath).href;
|
|
128
|
+
// Use dynamic import() for ESM/TypeScript compatibility with robust URL handling
|
|
129
|
+
import(fileUrl)
|
|
130
|
+
.then((module) => {
|
|
131
|
+
const fn = (module.default || module);
|
|
132
|
+
const result = fn(params);
|
|
133
|
+
// Handle both Promise and non-Promise returns
|
|
134
|
+
if (result instanceof Promise) {
|
|
135
|
+
result.then(handleSuccess).catch(handleError);
|
|
136
|
+
}
|
|
137
|
+
else {
|
|
138
|
+
handleSuccess(result);
|
|
139
|
+
}
|
|
140
|
+
})
|
|
141
|
+
.catch(handleError);
|
|
142
|
+
}
|
|
143
|
+
else {
|
|
144
|
+
// Execute embedded code (legacy) - create function from string
|
|
145
|
+
// SECURITY WARNING: Embedded code execution runs arbitrary JS with fs/path/axios access.
|
|
146
|
+
// This is a potential RCE vector if tool YAML files come from untrusted sources.
|
|
147
|
+
// Embedded code is DISABLED by default. Must explicitly opt-in via MATIMO_ALLOW_EMBEDDED_CODE=true
|
|
148
|
+
const embeddedCodeDisabled = process.env.MATIMO_ALLOW_EMBEDDED_CODE !== 'true';
|
|
149
|
+
if (embeddedCodeDisabled) {
|
|
150
|
+
throw new MatimoError('Embedded code execution is disabled by default for security. Use external .ts/.js files instead.', ErrorCode.EXECUTION_FAILED, {
|
|
151
|
+
toolName: tool.name,
|
|
152
|
+
recommendation: 'Create a separate .ts file in the tool directory instead of using embedded code',
|
|
153
|
+
enableFeatureFlag: 'Set MATIMO_ALLOW_EMBEDDED_CODE=true to enable (not recommended)',
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
// Log warning when embedded code is executed
|
|
157
|
+
const logger = getGlobalMatimoLogger();
|
|
158
|
+
logger.warn(`⚠️ Warning: Executing embedded code from tool '${tool.name}'. This carries security risks if tool YAML is from untrusted sources.`, { toolName: tool.name });
|
|
159
|
+
// In ESM modules, require is not available by default
|
|
160
|
+
// We pass a safe require function that embedded code can use
|
|
161
|
+
const functionBody = `return (${code})`;
|
|
162
|
+
const fn = new Function(functionBody)();
|
|
163
|
+
// Pass undefined for require in ESM - embedded code should use import syntax
|
|
164
|
+
const result = fn(params, {}, fs, path, axios, undefined);
|
|
165
|
+
// Handle both Promise and non-Promise returns
|
|
166
|
+
if (result instanceof Promise) {
|
|
167
|
+
result.then(handleSuccess).catch(handleError);
|
|
168
|
+
}
|
|
169
|
+
else {
|
|
170
|
+
handleSuccess(result);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
catch (error) {
|
|
175
|
+
handleError(error);
|
|
176
|
+
}
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
export default FunctionExecutor;
|
|
181
|
+
//# 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,MAA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D;oBAC7D,MAAM,YAAY,GAAG,WAAW,IAAI,GAAG,CAAC;oBACxC,MAAM,EAAE,GAAG,IAAI,QAAQ,CAAC,YAAY,CAAC,EAOhB,CAAC;oBACtB,6EAA6E;oBAC7E,MAAM,MAAM,GAAG,EAAE,CAAC,MAAM,EAAE,EAAE,EAAE,EAAE,EAAE,IAAI,EAAE,KAAK,EAAE,SAAS,CAAC,CAAC;oBAE1D,8CAA8C;oBAC9C,IAAI,MAAM,YAAY,OAAO,EAAE,CAAC;wBAC9B,MAAM,CAAC,IAAI,CAAC,aAAa,CAAC,CAAC,KAAK,CAAC,WAAW,CAAC,CAAC;oBAChD,CAAC;yBAAM,CAAC;wBACN,aAAa,CAAC,MAAM,CAAC,CAAC;oBACxB,CAAC;gBACH,CAAC;YACH,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBACf,WAAW,CAAC,KAAK,CAAC,CAAC;YACrB,CAAC;QACH,CAAC,CAAC,CAAC;IACL,CAAC;CACF;AAED,eAAe,gBAAgB,CAAC"}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { ToolDefinition } from '../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
|
+
* Check if a string is an unfilled placeholder
|
|
17
|
+
* Only matches single placeholders like "{param}", not "{...}" or embedded placeholders
|
|
18
|
+
*/
|
|
19
|
+
private isUnfilledPlaceholder;
|
|
20
|
+
/**
|
|
21
|
+
* Validate that all URL parameters are provided
|
|
22
|
+
*/
|
|
23
|
+
private validateUrlParameters;
|
|
24
|
+
/**
|
|
25
|
+
* Build query string from query_params, only including provided values
|
|
26
|
+
*/
|
|
27
|
+
private buildQueryString;
|
|
28
|
+
/**
|
|
29
|
+
* Replace parameter placeholders in an object (headers, body, query params)
|
|
30
|
+
*
|
|
31
|
+
* CORE PRINCIPLE: "Define once in YAML, embed correctly at execution time"
|
|
32
|
+
*
|
|
33
|
+
* This method intelligently handles different parameter types:
|
|
34
|
+
* - STRING placeholders like "{title}": Always templated as strings
|
|
35
|
+
* - OBJECT placeholders like "{parent}": Embedded directly as JSON objects (not stringified) if paramDefinitions specifies type:object
|
|
36
|
+
* - ARRAY placeholders like "{items}": Embedded directly as JSON arrays (not stringified) if paramDefinitions specifies type:array
|
|
37
|
+
*
|
|
38
|
+
* Key behaviors:
|
|
39
|
+
* - Recursively processes nested objects
|
|
40
|
+
* - Skips keys with unfilled placeholders (e.g., "{sort_by}" when sort_by not provided)
|
|
41
|
+
* - Uses parameter schema type from YAML to determine how to embed values
|
|
42
|
+
* - Preserves JSON structure for complex types (objects/arrays) sent to APIs
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* ```
|
|
46
|
+
* // YAML definition:
|
|
47
|
+
* parameters:
|
|
48
|
+
* parent:
|
|
49
|
+
* type: object // <-- Tells executor to embed as-is, not stringify
|
|
50
|
+
* items:
|
|
51
|
+
* type: array // <-- Tells executor to embed as-is, not stringify
|
|
52
|
+
* title:
|
|
53
|
+
* type: string // <-- String templating applies
|
|
54
|
+
*
|
|
55
|
+
* body:
|
|
56
|
+
* parent: "{parent}" // Object embedded as {"id": "123", ...}
|
|
57
|
+
* items: "{items}" // Array embedded as [{"name": "a"}, ...]
|
|
58
|
+
* title: "{title}" // String embedded as "My Title"
|
|
59
|
+
*
|
|
60
|
+
* // JavaScript call:
|
|
61
|
+
* const result = await matimo.execute('notion_create_page', {
|
|
62
|
+
* parent: { database_id: 'abc123' }, // JavaScript object
|
|
63
|
+
* items: [{ type: 'text' }], // JavaScript array
|
|
64
|
+
* title: 'Create This Page' // String
|
|
65
|
+
* });
|
|
66
|
+
*
|
|
67
|
+
* // HTTP body sent to API:
|
|
68
|
+
* {
|
|
69
|
+
* "parent": {"database_id": "abc123"}, // Proper JSON object
|
|
70
|
+
* "items": [{"type": "text"}], // Proper JSON array
|
|
71
|
+
* "title": "Create This Page" // String
|
|
72
|
+
* }
|
|
73
|
+
* ```
|
|
74
|
+
*/
|
|
75
|
+
private templateObject;
|
|
76
|
+
}
|
|
77
|
+
export default HttpExecutor;
|
|
78
|
+
//# 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;IAsFtF;;OAEG;IACH,OAAO,CAAC,cAAc;IAYtB;;;OAGG;IACH,OAAO,CAAC,qBAAqB;IAK7B;;OAEG;IACH,OAAO,CAAC,qBAAqB;IAyB7B;;OAEG;IACH,OAAO,CAAC,gBAAgB;IAexB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA8CG;IACH,OAAO,CAAC,cAAc;CA+GvB;AAED,eAAe,YAAY,CAAC"}
|