research-powerpack-mcp 3.4.7 → 3.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +63 -655
- package/dist/clients/reddit.d.ts.map +1 -1
- package/dist/clients/reddit.js +23 -35
- package/dist/clients/reddit.js.map +1 -1
- package/dist/clients/scraper.d.ts +1 -6
- package/dist/clients/scraper.d.ts.map +1 -1
- package/dist/clients/scraper.js +33 -69
- package/dist/clients/scraper.js.map +1 -1
- package/dist/config/env.d.ts +75 -0
- package/dist/config/env.d.ts.map +1 -0
- package/dist/config/env.js +87 -0
- package/dist/config/env.js.map +1 -0
- package/dist/config/yaml/tools-enhanced.yaml +0 -0
- package/dist/index.js +85 -25
- package/dist/index.js.map +1 -1
- package/dist/services/llm-processor.js +0 -25
- package/dist/services/llm-processor.js.map +1 -1
- package/dist/tools/reddit.d.ts.map +1 -1
- package/dist/tools/reddit.js +77 -29
- package/dist/tools/reddit.js.map +1 -1
- package/dist/tools/research.d.ts +2 -6
- package/dist/tools/research.d.ts.map +1 -1
- package/dist/tools/research.js +60 -47
- package/dist/tools/research.js.map +1 -1
- package/dist/tools/scrape.d.ts +1 -5
- package/dist/tools/scrape.d.ts.map +1 -1
- package/dist/tools/scrape.js +107 -64
- package/dist/tools/scrape.js.map +1 -1
- package/dist/tools/search.d.ts +1 -5
- package/dist/tools/search.d.ts.map +1 -1
- package/dist/tools/search.js +26 -16
- package/dist/tools/search.js.map +1 -1
- package/dist/tools/utils.d.ts +20 -59
- package/dist/tools/utils.d.ts.map +1 -1
- package/dist/tools/utils.js +25 -61
- package/dist/tools/utils.js.map +1 -1
- package/dist/utils/logger.d.ts +36 -0
- package/dist/utils/logger.d.ts.map +1 -0
- package/dist/utils/logger.js +40 -0
- package/dist/utils/logger.js.map +1 -0
- package/dist/utils/response.d.ts +101 -0
- package/dist/utils/response.d.ts.map +1 -0
- package/dist/utils/response.js +183 -0
- package/dist/utils/response.js.map +1 -0
- package/package.json +8 -2
package/dist/tools/search.js
CHANGED
|
@@ -6,6 +6,7 @@ import { SearchClient } from '../clients/search.js';
|
|
|
6
6
|
import { aggregateAndRank, buildUrlLookup, lookupUrl, generateEnhancedOutput, markConsensus, } from '../utils/url-aggregator.js';
|
|
7
7
|
import { CTR_WEIGHTS } from '../config/index.js';
|
|
8
8
|
import { classifyError } from '../utils/errors.js';
|
|
9
|
+
import { safeLog, formatSuccess, formatError, formatDuration, } from './utils.js';
|
|
9
10
|
function getPositionScore(position) {
|
|
10
11
|
if (position >= 1 && position <= 10) {
|
|
11
12
|
return CTR_WEIGHTS[position] ?? 0;
|
|
@@ -16,9 +17,7 @@ export async function handleWebSearch(params, options = {}) {
|
|
|
16
17
|
const { sessionId, logger } = options;
|
|
17
18
|
const startTime = Date.now();
|
|
18
19
|
try {
|
|
19
|
-
|
|
20
|
-
await logger('info', `Searching for ${params.keywords.length} keyword(s)`, sessionId);
|
|
21
|
-
}
|
|
20
|
+
await safeLog(logger, sessionId, 'info', `Searching for ${params.keywords.length} keyword(s)`, 'WebSearch');
|
|
22
21
|
const client = new SearchClient();
|
|
23
22
|
const response = await client.searchMultiple(params.keywords);
|
|
24
23
|
const aggregation = aggregateAndRank(response.searches, 5);
|
|
@@ -87,9 +86,19 @@ export async function handleWebSearch(params, options = {}) {
|
|
|
87
86
|
markdown += `\n---\n\n> *${queriesOmitted} additional queries not shown. Consensus URLs above include all ${response.searches.length} queries.*\n`;
|
|
88
87
|
}
|
|
89
88
|
const executionTime = Date.now() - startTime;
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
89
|
+
await safeLog(logger, sessionId, 'info', `Search completed: ${totalResults} results, ${aggregation.totalUniqueUrls} unique URLs, ${consensusUrls.length} consensus URLs in ${executionTime}ms`, 'WebSearch');
|
|
90
|
+
// Add next steps section
|
|
91
|
+
const nextSteps = [
|
|
92
|
+
`Scrape top URLs: \`scrape_links({urls: [${consensusUrls.slice(0, 3).map(u => `"${u.url}"`).join(', ')}], use_llm: true})\``,
|
|
93
|
+
'Use `deep_research()` for in-depth analysis of specific topics',
|
|
94
|
+
'Refine search with more specific keywords based on results',
|
|
95
|
+
];
|
|
96
|
+
const formattedContent = formatSuccess({
|
|
97
|
+
title: `Web Search Results (${params.keywords.length} keywords)`,
|
|
98
|
+
summary: `**Found ${totalResults} results** across ${response.totalKeywords} queries\n• ${aggregation.totalUniqueUrls} unique URLs\n• ${consensusUrls.length} consensus URLs (appeared in ${aggregation.frequencyThreshold}+ searches)\n• Execution time: ${formatDuration(executionTime)}`,
|
|
99
|
+
data: markdown,
|
|
100
|
+
nextSteps,
|
|
101
|
+
});
|
|
93
102
|
const metadata = {
|
|
94
103
|
total_keywords: response.totalKeywords,
|
|
95
104
|
total_results: totalResults,
|
|
@@ -98,20 +107,21 @@ export async function handleWebSearch(params, options = {}) {
|
|
|
98
107
|
consensus_url_count: consensusUrls.length,
|
|
99
108
|
frequency_threshold: aggregation.frequencyThreshold,
|
|
100
109
|
};
|
|
101
|
-
return { content:
|
|
110
|
+
return { content: formattedContent, structuredContent: { content: formattedContent, metadata } };
|
|
102
111
|
}
|
|
103
112
|
catch (error) {
|
|
104
113
|
// Classify error for better reporting
|
|
105
114
|
const structuredError = classifyError(error);
|
|
106
|
-
|
|
107
|
-
if (sessionId && logger) {
|
|
108
|
-
await logger('error', `web_search: ${structuredError.message}`, sessionId);
|
|
109
|
-
}
|
|
115
|
+
await safeLog(logger, sessionId, 'error', `web_search: ${structuredError.message}`, 'WebSearch');
|
|
110
116
|
const executionTime = Date.now() - startTime;
|
|
111
|
-
const
|
|
112
|
-
|
|
113
|
-
:
|
|
114
|
-
|
|
117
|
+
const errorContent = formatError({
|
|
118
|
+
code: structuredError.code,
|
|
119
|
+
message: structuredError.message,
|
|
120
|
+
retryable: structuredError.retryable,
|
|
121
|
+
toolName: 'web_search',
|
|
122
|
+
howToFix: ['Make sure SERPER_API_KEY is set in your environment variables'],
|
|
123
|
+
alternatives: ['Try search_reddit() for community discussions', 'Use deep_research() for AI-powered research'],
|
|
124
|
+
});
|
|
115
125
|
return {
|
|
116
126
|
content: errorContent,
|
|
117
127
|
structuredContent: {
|
|
@@ -120,7 +130,7 @@ export async function handleWebSearch(params, options = {}) {
|
|
|
120
130
|
total_keywords: params.keywords.length,
|
|
121
131
|
total_results: 0,
|
|
122
132
|
execution_time_ms: executionTime,
|
|
123
|
-
errorCode
|
|
133
|
+
errorCode: structuredError.code,
|
|
124
134
|
},
|
|
125
135
|
},
|
|
126
136
|
};
|
package/dist/tools/search.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"search.js","sourceRoot":"","sources":["../../src/tools/search.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAGH,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AACpD,OAAO,EACL,gBAAgB,EAChB,cAAc,EACd,SAAS,EACT,sBAAsB,EACtB,aAAa,GACd,MAAM,4BAA4B,CAAC;AACpC,OAAO,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AACjD,OAAO,EAAE,aAAa,
|
|
1
|
+
{"version":3,"file":"search.js","sourceRoot":"","sources":["../../src/tools/search.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAGH,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AACpD,OAAO,EACL,gBAAgB,EAChB,cAAc,EACd,SAAS,EACT,sBAAsB,EACtB,aAAa,GACd,MAAM,4BAA4B,CAAC;AACpC,OAAO,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AACjD,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACnD,OAAO,EACL,OAAO,EAEP,aAAa,EACb,WAAW,EACX,cAAc,GACf,MAAM,YAAY,CAAC;AAEpB,SAAS,gBAAgB,CAAC,QAAgB;IACxC,IAAI,QAAQ,IAAI,CAAC,IAAI,QAAQ,IAAI,EAAE,EAAE,CAAC;QACpC,OAAO,WAAW,CAAC,QAAQ,CAAC,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
* Central module for common tool functionality
|
|
4
|
+
* Re-exports from specialized utility modules for backwards compatibility
|
|
4
5
|
*/
|
|
6
|
+
export { safeLog, createToolLogger, type ToolLogger, type ToolOptions } from '../utils/logger.js';
|
|
7
|
+
export { formatSuccess, formatError, formatBatchHeader, formatList, formatKeyValues, formatDuration, truncateText, type SuccessResponseOptions, type ErrorResponseOptions, type BatchResultOptions, } from '../utils/response.js';
|
|
5
8
|
/**
|
|
6
|
-
*
|
|
9
|
+
* Token budgets for different tools
|
|
10
|
+
* Centralized here to ensure consistency
|
|
7
11
|
*/
|
|
8
|
-
export
|
|
9
|
-
/**
|
|
10
|
-
|
|
11
|
-
*/
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
}
|
|
16
|
-
/**
|
|
17
|
-
* Safe logger wrapper - NEVER throws
|
|
18
|
-
* Logs to provided logger or falls back to console.error
|
|
19
|
-
*
|
|
20
|
-
* @param logger - Optional logger function
|
|
21
|
-
* @param sessionId - Session ID for logging context
|
|
22
|
-
* @param level - Log level
|
|
23
|
-
* @param message - Message to log
|
|
24
|
-
* @param toolName - Name of the tool for prefixing
|
|
25
|
-
*/
|
|
26
|
-
export declare function safeLog(logger: ToolLogger | undefined, sessionId: string | undefined, level: 'info' | 'error' | 'debug', message: string, toolName: string): Promise<void>;
|
|
12
|
+
export declare const TOKEN_BUDGETS: {
|
|
13
|
+
/** Total token budget for deep research */
|
|
14
|
+
readonly RESEARCH: 32000;
|
|
15
|
+
/** Total token budget for web scraping */
|
|
16
|
+
readonly SCRAPER: 32000;
|
|
17
|
+
/** Total token budget for Reddit post fetching */
|
|
18
|
+
readonly REDDIT: 1000;
|
|
19
|
+
};
|
|
27
20
|
/**
|
|
28
21
|
* Calculate token allocation for batch operations
|
|
29
22
|
* Distributes a fixed budget across multiple items
|
|
30
23
|
*
|
|
31
24
|
* @param count - Number of items to distribute budget across
|
|
32
|
-
* @param budget - Total token budget
|
|
25
|
+
* @param budget - Total token budget (defaults to RESEARCH budget)
|
|
33
26
|
* @returns Tokens per item
|
|
34
27
|
*/
|
|
35
|
-
export declare function calculateTokenAllocation(count: number, budget
|
|
28
|
+
export declare function calculateTokenAllocation(count: number, budget?: number): number;
|
|
36
29
|
/**
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
* @param retryable - Whether the error is retryable
|
|
40
|
-
* @returns Hint string or empty string
|
|
30
|
+
* @deprecated Use formatError from response.ts instead
|
|
41
31
|
*/
|
|
42
32
|
export declare function formatRetryHint(retryable: boolean): string;
|
|
43
33
|
/**
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
* @param toolName - Name of the tool that errored
|
|
47
|
-
* @param errorCode - Error code
|
|
48
|
-
* @param message - Error message
|
|
49
|
-
* @param retryable - Whether error is retryable
|
|
50
|
-
* @param tip - Optional tip for resolution
|
|
51
|
-
* @returns Formatted markdown error string
|
|
34
|
+
* @deprecated Use formatError from response.ts instead
|
|
52
35
|
*/
|
|
53
36
|
export declare function formatToolError(toolName: string, errorCode: string, message: string, retryable: boolean, tip?: string): string;
|
|
54
37
|
/**
|
|
55
38
|
* Validate that a value is a non-empty array
|
|
56
|
-
*
|
|
57
|
-
* @param value - Value to check
|
|
58
|
-
* @param fieldName - Field name for error message
|
|
59
|
-
* @returns Error message or undefined if valid
|
|
60
39
|
*/
|
|
61
40
|
export declare function validateNonEmptyArray(value: unknown, fieldName: string): string | undefined;
|
|
62
41
|
/**
|
|
63
42
|
* Validate array length is within bounds
|
|
64
|
-
*
|
|
65
|
-
* @param arr - Array to check
|
|
66
|
-
* @param min - Minimum length
|
|
67
|
-
* @param max - Maximum length
|
|
68
|
-
* @param fieldName - Field name for error message
|
|
69
|
-
* @returns Error message or undefined if valid
|
|
70
43
|
*/
|
|
71
44
|
export declare function validateArrayBounds(arr: unknown[], min: number, max: number, fieldName: string): string | undefined;
|
|
72
45
|
/**
|
|
73
|
-
*
|
|
74
|
-
*
|
|
75
|
-
* @param title - Title of the results section
|
|
76
|
-
* @param count - Number of items processed
|
|
77
|
-
* @param tokensPerItem - Tokens allocated per item
|
|
78
|
-
* @param totalBudget - Total token budget
|
|
79
|
-
* @returns Formatted header string
|
|
46
|
+
* @deprecated Use formatBatchHeader from response.ts instead
|
|
80
47
|
*/
|
|
81
48
|
export declare function buildBatchHeader(title: string, count: number, tokensPerItem: number, totalBudget: number): string;
|
|
82
49
|
/**
|
|
83
|
-
*
|
|
84
|
-
*
|
|
85
|
-
* @param successful - Number of successful items
|
|
86
|
-
* @param failed - Number of failed items
|
|
87
|
-
* @param batches - Number of batches processed
|
|
88
|
-
* @param extras - Optional extra status items
|
|
89
|
-
* @returns Formatted status line
|
|
50
|
+
* @deprecated Use formatBatchHeader from response.ts instead
|
|
90
51
|
*/
|
|
91
52
|
export declare function buildStatusLine(successful: number, failed: number, batches: number, extras?: string[]): string;
|
|
92
53
|
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/tools/utils.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/tools/utils.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAGH,OAAO,EAAE,OAAO,EAAE,gBAAgB,EAAE,KAAK,UAAU,EAAE,KAAK,WAAW,EAAE,MAAM,oBAAoB,CAAC;AAGlG,OAAO,EACL,aAAa,EACb,WAAW,EACX,iBAAiB,EACjB,UAAU,EACV,eAAe,EACf,cAAc,EACd,YAAY,EACZ,KAAK,sBAAsB,EAC3B,KAAK,oBAAoB,EACzB,KAAK,kBAAkB,GACxB,MAAM,sBAAsB,CAAC;AAM9B;;;GAGG;AACH,eAAO,MAAM,aAAa;IACxB,2CAA2C;;IAE3C,0CAA0C;;IAE1C,kDAAkD;;CAE1C,CAAC;AAMX;;;;;;;GAOG;AACH,wBAAgB,wBAAwB,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,GAAE,MAA+B,GAAG,MAAM,CAGvG;AAMD;;GAEG;AACH,wBAAgB,eAAe,CAAC,SAAS,EAAE,OAAO,GAAG,MAAM,CAI1D;AAED;;GAEG;AACH,wBAAgB,eAAe,CAC7B,QAAQ,EAAE,MAAM,EAChB,SAAS,EAAE,MAAM,EACjB,OAAO,EAAE,MAAM,EACf,SAAS,EAAE,OAAO,EAClB,GAAG,CAAC,EAAE,MAAM,GACX,MAAM,CAIR;AAMD;;GAEG;AACH,wBAAgB,qBAAqB,CACnC,KAAK,EAAE,OAAO,EACd,SAAS,EAAE,MAAM,GAChB,MAAM,GAAG,SAAS,CAQpB;AAED;;GAEG;AACH,wBAAgB,mBAAmB,CACjC,GAAG,EAAE,OAAO,EAAE,EACd,GAAG,EAAE,MAAM,EACX,GAAG,EAAE,MAAM,EACX,SAAS,EAAE,MAAM,GAChB,MAAM,GAAG,SAAS,CAQpB;AAMD;;GAEG;AACH,wBAAgB,gBAAgB,CAC9B,KAAK,EAAE,MAAM,EACb,KAAK,EAAE,MAAM,EACb,aAAa,EAAE,MAAM,EACrB,WAAW,EAAE,MAAM,GAClB,MAAM,CAER;AAED;;GAEG;AACH,wBAAgB,eAAe,CAC7B,UAAU,EAAE,MAAM,EAClB,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,MAAM,EACf,MAAM,CAAC,EAAE,MAAM,EAAE,GAChB,MAAM,CAMR"}
|
package/dist/tools/utils.js
CHANGED
|
@@ -1,31 +1,27 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Shared Tool Utilities
|
|
3
|
-
*
|
|
3
|
+
* Central module for common tool functionality
|
|
4
|
+
* Re-exports from specialized utility modules for backwards compatibility
|
|
4
5
|
*/
|
|
6
|
+
// Re-export logging utilities from centralized module
|
|
7
|
+
export { safeLog, createToolLogger } from '../utils/logger.js';
|
|
8
|
+
// Re-export response formatting utilities
|
|
9
|
+
export { formatSuccess, formatError, formatBatchHeader, formatList, formatKeyValues, formatDuration, truncateText, } from '../utils/response.js';
|
|
5
10
|
// ============================================================================
|
|
6
|
-
//
|
|
11
|
+
// Token Budget Constants
|
|
7
12
|
// ============================================================================
|
|
8
13
|
/**
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
* @param logger - Optional logger function
|
|
13
|
-
* @param sessionId - Session ID for logging context
|
|
14
|
-
* @param level - Log level
|
|
15
|
-
* @param message - Message to log
|
|
16
|
-
* @param toolName - Name of the tool for prefixing
|
|
14
|
+
* Token budgets for different tools
|
|
15
|
+
* Centralized here to ensure consistency
|
|
17
16
|
*/
|
|
18
|
-
export
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
console.error(`[${toolName}] Logger failed: ${message}`);
|
|
27
|
-
}
|
|
28
|
-
}
|
|
17
|
+
export const TOKEN_BUDGETS = {
|
|
18
|
+
/** Total token budget for deep research */
|
|
19
|
+
RESEARCH: 32000,
|
|
20
|
+
/** Total token budget for web scraping */
|
|
21
|
+
SCRAPER: 32000,
|
|
22
|
+
/** Total token budget for Reddit post fetching */
|
|
23
|
+
REDDIT: 1000, // Comments budget
|
|
24
|
+
};
|
|
29
25
|
// ============================================================================
|
|
30
26
|
// Token Allocation
|
|
31
27
|
// ============================================================================
|
|
@@ -34,22 +30,19 @@ export async function safeLog(logger, sessionId, level, message, toolName) {
|
|
|
34
30
|
* Distributes a fixed budget across multiple items
|
|
35
31
|
*
|
|
36
32
|
* @param count - Number of items to distribute budget across
|
|
37
|
-
* @param budget - Total token budget
|
|
33
|
+
* @param budget - Total token budget (defaults to RESEARCH budget)
|
|
38
34
|
* @returns Tokens per item
|
|
39
35
|
*/
|
|
40
|
-
export function calculateTokenAllocation(count, budget) {
|
|
36
|
+
export function calculateTokenAllocation(count, budget = TOKEN_BUDGETS.RESEARCH) {
|
|
41
37
|
if (count <= 0)
|
|
42
38
|
return budget;
|
|
43
39
|
return Math.floor(budget / count);
|
|
44
40
|
}
|
|
45
41
|
// ============================================================================
|
|
46
|
-
// Error Formatting
|
|
42
|
+
// Legacy Error Formatting (deprecated - use response.ts instead)
|
|
47
43
|
// ============================================================================
|
|
48
44
|
/**
|
|
49
|
-
*
|
|
50
|
-
*
|
|
51
|
-
* @param retryable - Whether the error is retryable
|
|
52
|
-
* @returns Hint string or empty string
|
|
45
|
+
* @deprecated Use formatError from response.ts instead
|
|
53
46
|
*/
|
|
54
47
|
export function formatRetryHint(retryable) {
|
|
55
48
|
return retryable
|
|
@@ -57,14 +50,7 @@ export function formatRetryHint(retryable) {
|
|
|
57
50
|
: '';
|
|
58
51
|
}
|
|
59
52
|
/**
|
|
60
|
-
*
|
|
61
|
-
*
|
|
62
|
-
* @param toolName - Name of the tool that errored
|
|
63
|
-
* @param errorCode - Error code
|
|
64
|
-
* @param message - Error message
|
|
65
|
-
* @param retryable - Whether error is retryable
|
|
66
|
-
* @param tip - Optional tip for resolution
|
|
67
|
-
* @returns Formatted markdown error string
|
|
53
|
+
* @deprecated Use formatError from response.ts instead
|
|
68
54
|
*/
|
|
69
55
|
export function formatToolError(toolName, errorCode, message, retryable, tip) {
|
|
70
56
|
const retryHint = formatRetryHint(retryable);
|
|
@@ -76,10 +62,6 @@ export function formatToolError(toolName, errorCode, message, retryable, tip) {
|
|
|
76
62
|
// ============================================================================
|
|
77
63
|
/**
|
|
78
64
|
* Validate that a value is a non-empty array
|
|
79
|
-
*
|
|
80
|
-
* @param value - Value to check
|
|
81
|
-
* @param fieldName - Field name for error message
|
|
82
|
-
* @returns Error message or undefined if valid
|
|
83
65
|
*/
|
|
84
66
|
export function validateNonEmptyArray(value, fieldName) {
|
|
85
67
|
if (!Array.isArray(value)) {
|
|
@@ -92,12 +74,6 @@ export function validateNonEmptyArray(value, fieldName) {
|
|
|
92
74
|
}
|
|
93
75
|
/**
|
|
94
76
|
* Validate array length is within bounds
|
|
95
|
-
*
|
|
96
|
-
* @param arr - Array to check
|
|
97
|
-
* @param min - Minimum length
|
|
98
|
-
* @param max - Maximum length
|
|
99
|
-
* @param fieldName - Field name for error message
|
|
100
|
-
* @returns Error message or undefined if valid
|
|
101
77
|
*/
|
|
102
78
|
export function validateArrayBounds(arr, min, max, fieldName) {
|
|
103
79
|
if (arr.length < min) {
|
|
@@ -109,28 +85,16 @@ export function validateArrayBounds(arr, min, max, fieldName) {
|
|
|
109
85
|
return undefined;
|
|
110
86
|
}
|
|
111
87
|
// ============================================================================
|
|
112
|
-
// Response Builders
|
|
88
|
+
// Legacy Response Builders (deprecated - use response.ts instead)
|
|
113
89
|
// ============================================================================
|
|
114
90
|
/**
|
|
115
|
-
*
|
|
116
|
-
*
|
|
117
|
-
* @param title - Title of the results section
|
|
118
|
-
* @param count - Number of items processed
|
|
119
|
-
* @param tokensPerItem - Tokens allocated per item
|
|
120
|
-
* @param totalBudget - Total token budget
|
|
121
|
-
* @returns Formatted header string
|
|
91
|
+
* @deprecated Use formatBatchHeader from response.ts instead
|
|
122
92
|
*/
|
|
123
93
|
export function buildBatchHeader(title, count, tokensPerItem, totalBudget) {
|
|
124
94
|
return `# ${title} (${count} items)\n\n**Token Allocation:** ${tokensPerItem.toLocaleString()} tokens/item (${count} items, ${totalBudget.toLocaleString()} total budget)`;
|
|
125
95
|
}
|
|
126
96
|
/**
|
|
127
|
-
*
|
|
128
|
-
*
|
|
129
|
-
* @param successful - Number of successful items
|
|
130
|
-
* @param failed - Number of failed items
|
|
131
|
-
* @param batches - Number of batches processed
|
|
132
|
-
* @param extras - Optional extra status items
|
|
133
|
-
* @returns Formatted status line
|
|
97
|
+
* @deprecated Use formatBatchHeader from response.ts instead
|
|
134
98
|
*/
|
|
135
99
|
export function buildStatusLine(successful, failed, batches, extras) {
|
|
136
100
|
let status = `**Status:** ✅ ${successful} successful | ❌ ${failed} failed | 📦 ${batches} batch(es)`;
|
package/dist/tools/utils.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/tools/utils.ts"],"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H,MAAM,UAAU,eAAe,CAC7B,UAAkB,EAClB,MAAc,EACd,OAAe,EACf,MAAiB;IAEjB,IAAI,MAAM,GAAG,iBAAiB,UAAU,mBAAmB,MAAM,gBAAgB,OAAO,YAAY,CAAC;IACrG,IAAI,MAAM,IAAI,MAAM,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QAChC,MAAM,IAAI,MAAM,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC;IACvC,CAAC;IACD,OAAO,MAAM,CAAC;AAChB,CAAC"}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Centralized Logger Utilities
|
|
3
|
+
* Provides safe logging that NEVER throws - extracted from tool handlers
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Logger function type used across all tools
|
|
7
|
+
*/
|
|
8
|
+
export type ToolLogger = (level: 'info' | 'error' | 'debug', message: string, sessionId: string) => Promise<void>;
|
|
9
|
+
/**
|
|
10
|
+
* Standard options passed to tool handlers
|
|
11
|
+
*/
|
|
12
|
+
export interface ToolOptions {
|
|
13
|
+
sessionId?: string;
|
|
14
|
+
logger?: ToolLogger;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Safe logger wrapper - NEVER throws
|
|
18
|
+
* Logs to provided logger or falls back to console.error
|
|
19
|
+
*
|
|
20
|
+
* @param logger - Optional logger function
|
|
21
|
+
* @param sessionId - Session ID for logging context
|
|
22
|
+
* @param level - Log level ('info' | 'error' | 'debug')
|
|
23
|
+
* @param message - Message to log
|
|
24
|
+
* @param toolName - Name of the tool for prefixing (optional)
|
|
25
|
+
*/
|
|
26
|
+
export declare function safeLog(logger: ToolLogger | undefined, sessionId: string | undefined, level: 'info' | 'error' | 'debug', message: string, toolName?: string): Promise<void>;
|
|
27
|
+
/**
|
|
28
|
+
* Create a bound logger for a specific tool
|
|
29
|
+
* Returns a simpler function that only needs level and message
|
|
30
|
+
*
|
|
31
|
+
* @param options - Tool options containing logger and sessionId
|
|
32
|
+
* @param toolName - Name of the tool for log prefixing
|
|
33
|
+
* @returns Bound log function
|
|
34
|
+
*/
|
|
35
|
+
export declare function createToolLogger(options: ToolOptions, toolName: string): (level: 'info' | 'error' | 'debug', message: string) => Promise<void>;
|
|
36
|
+
//# sourceMappingURL=logger.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../../src/utils/logger.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;GAEG;AACH,MAAM,MAAM,UAAU,GAAG,CACvB,KAAK,EAAE,MAAM,GAAG,OAAO,GAAG,OAAO,EACjC,OAAO,EAAE,MAAM,EACf,SAAS,EAAE,MAAM,KACd,OAAO,CAAC,IAAI,CAAC,CAAC;AAEnB;;GAEG;AACH,MAAM,WAAW,WAAW;IAC1B,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,MAAM,CAAC,EAAE,UAAU,CAAC;CACrB;AAED;;;;;;;;;GASG;AACH,wBAAsB,OAAO,CAC3B,MAAM,EAAE,UAAU,GAAG,SAAS,EAC9B,SAAS,EAAE,MAAM,GAAG,SAAS,EAC7B,KAAK,EAAE,MAAM,GAAG,OAAO,GAAG,OAAO,EACjC,OAAO,EAAE,MAAM,EACf,QAAQ,CAAC,EAAE,MAAM,GAChB,OAAO,CAAC,IAAI,CAAC,CAWf;AAED;;;;;;;GAOG;AACH,wBAAgB,gBAAgB,CAC9B,OAAO,EAAE,WAAW,EACpB,QAAQ,EAAE,MAAM,GACf,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,GAAG,OAAO,EAAE,OAAO,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,CAIvE"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Centralized Logger Utilities
|
|
3
|
+
* Provides safe logging that NEVER throws - extracted from tool handlers
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Safe logger wrapper - NEVER throws
|
|
7
|
+
* Logs to provided logger or falls back to console.error
|
|
8
|
+
*
|
|
9
|
+
* @param logger - Optional logger function
|
|
10
|
+
* @param sessionId - Session ID for logging context
|
|
11
|
+
* @param level - Log level ('info' | 'error' | 'debug')
|
|
12
|
+
* @param message - Message to log
|
|
13
|
+
* @param toolName - Name of the tool for prefixing (optional)
|
|
14
|
+
*/
|
|
15
|
+
export async function safeLog(logger, sessionId, level, message, toolName) {
|
|
16
|
+
if (!logger || !sessionId)
|
|
17
|
+
return;
|
|
18
|
+
const prefix = toolName ? `[${toolName}] ` : '';
|
|
19
|
+
try {
|
|
20
|
+
await logger(level, `${prefix}${message}`, sessionId);
|
|
21
|
+
}
|
|
22
|
+
catch {
|
|
23
|
+
// Silently ignore logger errors - they should never crash the tool
|
|
24
|
+
console.error(`${prefix}Logger failed: ${message}`);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Create a bound logger for a specific tool
|
|
29
|
+
* Returns a simpler function that only needs level and message
|
|
30
|
+
*
|
|
31
|
+
* @param options - Tool options containing logger and sessionId
|
|
32
|
+
* @param toolName - Name of the tool for log prefixing
|
|
33
|
+
* @returns Bound log function
|
|
34
|
+
*/
|
|
35
|
+
export function createToolLogger(options, toolName) {
|
|
36
|
+
return async (level, message) => {
|
|
37
|
+
await safeLog(options.logger, options.sessionId, level, message, toolName);
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
//# sourceMappingURL=logger.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"logger.js","sourceRoot":"","sources":["../../src/utils/logger.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAmBH;;;;;;;;;GASG;AACH,MAAM,CAAC,KAAK,UAAU,OAAO,CAC3B,MAA8B,EAC9B,SAA6B,EAC7B,KAAiC,EACjC,OAAe,EACf,QAAiB;IAEjB,IAAI,CAAC,MAAM,IAAI,CAAC,SAAS;QAAE,OAAO;IAElC,MAAM,MAAM,GAAG,QAAQ,CAAC,CAAC,CAAC,IAAI,QAAQ,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC;IAEhD,IAAI,CAAC;QACH,MAAM,MAAM,CAAC,KAAK,EAAE,GAAG,MAAM,GAAG,OAAO,EAAE,EAAE,SAAS,CAAC,CAAC;IACxD,CAAC;IAAC,MAAM,CAAC;QACP,mEAAmE;QACnE,OAAO,CAAC,KAAK,CAAC,GAAG,MAAM,kBAAkB,OAAO,EAAE,CAAC,CAAC;IACtD,CAAC;AACH,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,gBAAgB,CAC9B,OAAoB,EACpB,QAAgB;IAEhB,OAAO,KAAK,EAAE,KAAK,EAAE,OAAO,EAAE,EAAE;QAC9B,MAAM,OAAO,CAAC,OAAO,CAAC,MAAM,EAAE,OAAO,CAAC,SAAS,EAAE,KAAK,EAAE,OAAO,EAAE,QAAQ,CAAC,CAAC;IAC7E,CAAC,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Standardized Response Formatting for MCP Tools
|
|
3
|
+
* Implements the 70/20/10 pattern for agent-optimized responses
|
|
4
|
+
*
|
|
5
|
+
* 70% Summary - Key insights, status, metrics
|
|
6
|
+
* 20% Data - Structured results
|
|
7
|
+
* 10% Next Steps - Actionable follow-ups
|
|
8
|
+
*/
|
|
9
|
+
export interface SuccessResponseOptions {
|
|
10
|
+
/** Main title for the response */
|
|
11
|
+
title: string;
|
|
12
|
+
/** Summary section (70% of response) */
|
|
13
|
+
summary: string;
|
|
14
|
+
/** Optional data section (20% of response) */
|
|
15
|
+
data?: string;
|
|
16
|
+
/** Optional next steps (10% of response) */
|
|
17
|
+
nextSteps?: string[];
|
|
18
|
+
/** Optional metadata to append */
|
|
19
|
+
metadata?: Record<string, string | number>;
|
|
20
|
+
}
|
|
21
|
+
export interface ErrorResponseOptions {
|
|
22
|
+
/** Error code (e.g., RATE_LIMITED, TIMEOUT) */
|
|
23
|
+
code: string;
|
|
24
|
+
/** Human-readable error message */
|
|
25
|
+
message: string;
|
|
26
|
+
/** Whether the error is retryable */
|
|
27
|
+
retryable?: boolean;
|
|
28
|
+
/** Steps to fix the error */
|
|
29
|
+
howToFix?: string[];
|
|
30
|
+
/** Alternative actions to try */
|
|
31
|
+
alternatives?: string[];
|
|
32
|
+
/** Tool name for context */
|
|
33
|
+
toolName?: string;
|
|
34
|
+
}
|
|
35
|
+
export interface BatchResultOptions {
|
|
36
|
+
/** Title for the batch results */
|
|
37
|
+
title: string;
|
|
38
|
+
/** Number of items processed */
|
|
39
|
+
totalItems: number;
|
|
40
|
+
/** Number of successful items */
|
|
41
|
+
successful: number;
|
|
42
|
+
/** Number of failed items */
|
|
43
|
+
failed: number;
|
|
44
|
+
/** Tokens allocated per item */
|
|
45
|
+
tokensPerItem?: number;
|
|
46
|
+
/** Total token budget */
|
|
47
|
+
totalBudget?: number;
|
|
48
|
+
/** Number of batches processed */
|
|
49
|
+
batches?: number;
|
|
50
|
+
/** Additional status items */
|
|
51
|
+
extras?: string[];
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Format a successful tool response using the 70/20/10 pattern
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* formatSuccess({
|
|
58
|
+
* title: 'Web Search Results',
|
|
59
|
+
* summary: '**Found 45 unique URLs** across 5 queries\n• 12 consensus results (appeared 3+ times)\n• Top domains: github.com, stackoverflow.com',
|
|
60
|
+
* data: '## Top Results\n1. [Title](url) - snippet\n2. ...',
|
|
61
|
+
* nextSteps: ['Scrape top URLs: scrape_links({urls: [...]})', 'Deep dive: deep_research({...})']
|
|
62
|
+
* })
|
|
63
|
+
*/
|
|
64
|
+
export declare function formatSuccess(options: SuccessResponseOptions): string;
|
|
65
|
+
/**
|
|
66
|
+
* Format an error response with recovery guidance
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* formatError({
|
|
70
|
+
* code: 'RATE_LIMITED',
|
|
71
|
+
* message: 'API rate limit exceeded',
|
|
72
|
+
* retryable: true,
|
|
73
|
+
* howToFix: ['Wait 60 seconds', 'Reduce batch size'],
|
|
74
|
+
* alternatives: ['Use cached results', 'Try different API']
|
|
75
|
+
* })
|
|
76
|
+
*/
|
|
77
|
+
export declare function formatError(options: ErrorResponseOptions): string;
|
|
78
|
+
/**
|
|
79
|
+
* Format batch operation header with status
|
|
80
|
+
*/
|
|
81
|
+
export declare function formatBatchHeader(options: BatchResultOptions): string;
|
|
82
|
+
/**
|
|
83
|
+
* Format a list of items with optional truncation
|
|
84
|
+
*/
|
|
85
|
+
export declare function formatList(items: string[], options?: {
|
|
86
|
+
maxItems?: number;
|
|
87
|
+
ordered?: boolean;
|
|
88
|
+
}): string;
|
|
89
|
+
/**
|
|
90
|
+
* Format key-value pairs as a compact list
|
|
91
|
+
*/
|
|
92
|
+
export declare function formatKeyValues(data: Record<string, unknown>): string;
|
|
93
|
+
/**
|
|
94
|
+
* Format execution time
|
|
95
|
+
*/
|
|
96
|
+
export declare function formatDuration(ms: number): string;
|
|
97
|
+
/**
|
|
98
|
+
* Truncate text with ellipsis
|
|
99
|
+
*/
|
|
100
|
+
export declare function truncateText(text: string, maxLength: number): string;
|
|
101
|
+
//# sourceMappingURL=response.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"response.d.ts","sourceRoot":"","sources":["../../src/utils/response.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAMH,MAAM,WAAW,sBAAsB;IACrC,kCAAkC;IAClC,KAAK,EAAE,MAAM,CAAC;IACd,wCAAwC;IACxC,OAAO,EAAE,MAAM,CAAC;IAChB,8CAA8C;IAC9C,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,4CAA4C;IAC5C,SAAS,CAAC,EAAE,MAAM,EAAE,CAAC;IACrB,kCAAkC;IAClC,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM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