@lkbaba/grok-mcp 1.0.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/CHANGELOG.md +40 -0
- package/LICENSE +21 -0
- package/README.md +179 -0
- package/dist/config/index.d.ts +48 -0
- package/dist/config/index.d.ts.map +1 -0
- package/dist/config/index.js +101 -0
- package/dist/config/index.js.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +182 -0
- package/dist/index.js.map +1 -0
- package/dist/tools/agent-search.d.ts +14 -0
- package/dist/tools/agent-search.d.ts.map +1 -0
- package/dist/tools/agent-search.js +135 -0
- package/dist/tools/agent-search.js.map +1 -0
- package/dist/tools/brainstorm.d.ts +14 -0
- package/dist/tools/brainstorm.d.ts.map +1 -0
- package/dist/tools/brainstorm.js +208 -0
- package/dist/tools/brainstorm.js.map +1 -0
- package/dist/tools/definitions.d.ts +310 -0
- package/dist/tools/definitions.d.ts.map +1 -0
- package/dist/tools/definitions.js +187 -0
- package/dist/tools/definitions.js.map +1 -0
- package/dist/types/index.d.ts +210 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +7 -0
- package/dist/types/index.js.map +1 -0
- package/dist/utils/grok-client.d.ts +53 -0
- package/dist/utils/grok-client.d.ts.map +1 -0
- package/dist/utils/grok-client.js +147 -0
- package/dist/utils/grok-client.js.map +1 -0
- package/dist/utils/logger.d.ts +52 -0
- package/dist/utils/logger.d.ts.map +1 -0
- package/dist/utils/logger.js +97 -0
- package/dist/utils/logger.js.map +1 -0
- package/dist/utils/tool-builder.d.ts +66 -0
- package/dist/utils/tool-builder.d.ts.map +1 -0
- package/dist/utils/tool-builder.js +187 -0
- package/dist/utils/tool-builder.js.map +1 -0
- package/package.json +67 -0
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* grok_agent_search Tool Implementation
|
|
3
|
+
*
|
|
4
|
+
* Intelligent search using Grok AI, supports Web search, X search, or mixed search
|
|
5
|
+
*/
|
|
6
|
+
import { createResponse, extractContent, extractCitations, extractUsage, calculateCost, } from '../utils/grok-client.js';
|
|
7
|
+
import { buildWebSearchTool, buildXSearchTool, } from '../utils/tool-builder.js';
|
|
8
|
+
import { xaiConfig, debugMode, SUPPORTED_MODELS } from '../config/index.js';
|
|
9
|
+
/**
|
|
10
|
+
* Validate model name is supported
|
|
11
|
+
*/
|
|
12
|
+
function resolveModel(model) {
|
|
13
|
+
if (!model)
|
|
14
|
+
return xaiConfig.defaultModel;
|
|
15
|
+
if (SUPPORTED_MODELS.includes(model))
|
|
16
|
+
return model;
|
|
17
|
+
// Unsupported model, fall back to default with warning
|
|
18
|
+
console.error(`[Agent Search] Unsupported model "${model}", falling back to default ${xaiConfig.defaultModel}`);
|
|
19
|
+
return xaiConfig.defaultModel;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* grok_agent_search main function
|
|
23
|
+
*
|
|
24
|
+
* @param input - Search input parameters
|
|
25
|
+
* @returns Search result
|
|
26
|
+
*/
|
|
27
|
+
export async function grokAgentSearch(input) {
|
|
28
|
+
try {
|
|
29
|
+
// search_type defaults to mixed (search both Web and X)
|
|
30
|
+
const searchType = input.search_type || 'mixed';
|
|
31
|
+
const model = resolveModel(input.model);
|
|
32
|
+
const outputFormat = input.output_format || 'text';
|
|
33
|
+
if (debugMode) {
|
|
34
|
+
console.error('[Agent Search] Starting search:', input.query);
|
|
35
|
+
console.error('[Agent Search] Search type:', searchType);
|
|
36
|
+
console.error('[Agent Search] Model:', model);
|
|
37
|
+
console.error('[Agent Search] Output format:', outputFormat);
|
|
38
|
+
}
|
|
39
|
+
// 1. Build tool list based on search_type
|
|
40
|
+
const tools = [];
|
|
41
|
+
if (searchType === 'web' || searchType === 'mixed') {
|
|
42
|
+
// Build Web Search tool
|
|
43
|
+
const webTool = buildWebSearchTool({
|
|
44
|
+
allowedDomains: input.web_search_config?.allowed_domains,
|
|
45
|
+
excludedDomains: input.web_search_config?.excluded_domains,
|
|
46
|
+
enableImageUnderstanding: input.web_search_config?.enable_image_understanding,
|
|
47
|
+
});
|
|
48
|
+
tools.push(webTool);
|
|
49
|
+
if (debugMode) {
|
|
50
|
+
console.error('[Agent Search] Added Web Search tool');
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
if (searchType === 'x' || searchType === 'mixed') {
|
|
54
|
+
// Build X Search tool
|
|
55
|
+
const xTool = buildXSearchTool({
|
|
56
|
+
fromDate: input.x_search_config?.from_date,
|
|
57
|
+
toDate: input.x_search_config?.to_date,
|
|
58
|
+
allowedXHandles: input.x_search_config?.allowed_x_handles,
|
|
59
|
+
excludedXHandles: input.x_search_config?.excluded_x_handles,
|
|
60
|
+
enableImageUnderstanding: input.x_search_config?.enable_image_understanding,
|
|
61
|
+
enableVideoUnderstanding: input.x_search_config?.enable_video_understanding,
|
|
62
|
+
});
|
|
63
|
+
tools.push(xTool);
|
|
64
|
+
if (debugMode) {
|
|
65
|
+
console.error('[Agent Search] Added X Search tool');
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
if (tools.length === 0) {
|
|
69
|
+
throw new Error('At least one search type must be specified (web, x, or mixed)');
|
|
70
|
+
}
|
|
71
|
+
// 2. Build query content (if JSON output needed, request it in the prompt)
|
|
72
|
+
let queryContent = input.query;
|
|
73
|
+
if (outputFormat === 'json') {
|
|
74
|
+
queryContent += '\n\nPlease return search results in JSON format with the following structure:\n' +
|
|
75
|
+
'{"summary": "Search summary", "results": [{"title": "Title", "content": "Content", "source": "Source URL"}], "key_findings": ["Finding 1", "Finding 2"]}';
|
|
76
|
+
}
|
|
77
|
+
// 3. Call Grok API
|
|
78
|
+
if (debugMode) {
|
|
79
|
+
console.error('[Agent Search] Calling Grok API...');
|
|
80
|
+
}
|
|
81
|
+
const response = await createResponse({
|
|
82
|
+
model,
|
|
83
|
+
messages: [{ role: 'user', content: queryContent }],
|
|
84
|
+
server_side_tools: tools,
|
|
85
|
+
});
|
|
86
|
+
// 4. Extract results
|
|
87
|
+
const content = extractContent(response);
|
|
88
|
+
const citations = extractCitations(content);
|
|
89
|
+
const usage = extractUsage(response);
|
|
90
|
+
const cost = calculateCost(response);
|
|
91
|
+
if (debugMode) {
|
|
92
|
+
console.error('[Agent Search] Search completed');
|
|
93
|
+
console.error('[Agent Search] Model:', model);
|
|
94
|
+
console.error('[Agent Search] Citations:', citations.length);
|
|
95
|
+
console.error('[Agent Search] Token usage:', usage.total_tokens);
|
|
96
|
+
console.error('[Agent Search] Cost:', `$${cost.toFixed(6)}`);
|
|
97
|
+
}
|
|
98
|
+
// 5. Return result
|
|
99
|
+
return {
|
|
100
|
+
content,
|
|
101
|
+
citations,
|
|
102
|
+
usage,
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
catch (error) {
|
|
106
|
+
// Error handling
|
|
107
|
+
if (error instanceof Error) {
|
|
108
|
+
console.error('[Agent Search] Search failed:', error.message);
|
|
109
|
+
// Timeout error
|
|
110
|
+
if (error.message.includes('timeout') || error.message.includes('ETIMEDOUT')) {
|
|
111
|
+
throw new Error(`Search timed out. Suggestions:\n` +
|
|
112
|
+
`1. Simplify the query\n` +
|
|
113
|
+
`2. Narrow the search scope\n` +
|
|
114
|
+
`3. Try again later\n` +
|
|
115
|
+
`Original error: ${error.message}`);
|
|
116
|
+
}
|
|
117
|
+
// API error
|
|
118
|
+
if (error.message.includes('API') || error.message.includes('401')) {
|
|
119
|
+
throw new Error(`API call failed. Please check:\n` +
|
|
120
|
+
`1. Is XAI_API_KEY configured correctly?\n` +
|
|
121
|
+
`2. Is the API key valid?\n` +
|
|
122
|
+
`3. Is the network connection working?\n` +
|
|
123
|
+
`Original error: ${error.message}`);
|
|
124
|
+
}
|
|
125
|
+
// Parameter validation error
|
|
126
|
+
if (error.message.includes('validation failed')) {
|
|
127
|
+
throw new Error(`Parameter validation failed: ${error.message}`);
|
|
128
|
+
}
|
|
129
|
+
// Other errors
|
|
130
|
+
throw new Error(`Search failed: ${error.message}`);
|
|
131
|
+
}
|
|
132
|
+
throw error;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
//# sourceMappingURL=agent-search.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"agent-search.js","sourceRoot":"","sources":["../../src/tools/agent-search.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAMH,OAAO,EACL,cAAc,EACd,cAAc,EACd,gBAAgB,EAChB,YAAY,EACZ,aAAa,GACd,MAAM,yBAAyB,CAAC;AACjC,OAAO,EACL,kBAAkB,EAClB,gBAAgB,GACjB,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AAE5E;;GAEG;AACH,SAAS,YAAY,CAAC,KAAc;IAClC,IAAI,CAAC,KAAK;QAAE,OAAO,SAAS,CAAC,YAAY,CAAC;IAC1C,IAAK,gBAAsC,CAAC,QAAQ,CAAC,KAAK,CAAC;QAAE,OAAO,KAAK,CAAC;IAC1E,uDAAuD;IACvD,OAAO,CAAC,KAAK,CAAC,qCAAqC,KAAK,8BAA8B,SAAS,CAAC,YAAY,EAAE,CAAC,CAAC;IAChH,OAAO,SAAS,CAAC,YAAY,CAAC;AAChC,CAAC;AAED;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,eAAe,CACnC,KAA2B;IAE3B,IAAI,CAAC;QACH,wDAAwD;QACxD,MAAM,UAAU,GAAG,KAAK,CAAC,WAAW,IAAI,OAAO,CAAC;QAChD,MAAM,KAAK,GAAG,YAAY,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;QACxC,MAAM,YAAY,GAAG,KAAK,CAAC,aAAa,IAAI,MAAM,CAAC;QAEnD,IAAI,SAAS,EAAE,CAAC;YACd,OAAO,CAAC,KAAK,CAAC,iCAAiC,EAAE,KAAK,CAAC,KAAK,CAAC,CAAC;YAC9D,OAAO,CAAC,KAAK,CAAC,6BAA6B,EA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* grok_brainstorm Tool Implementation
|
|
3
|
+
*
|
|
4
|
+
* Creative brainstorming using Grok AI
|
|
5
|
+
*/
|
|
6
|
+
import type { GrokBrainstormInput, GrokBrainstormOutput } from '../types/index.js';
|
|
7
|
+
/**
|
|
8
|
+
* grok_brainstorm main function
|
|
9
|
+
*
|
|
10
|
+
* @param input - Brainstorm input parameters
|
|
11
|
+
* @returns Brainstorm result
|
|
12
|
+
*/
|
|
13
|
+
export declare function grokBrainstorm(input: GrokBrainstormInput): Promise<GrokBrainstormOutput>;
|
|
14
|
+
//# sourceMappingURL=brainstorm.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"brainstorm.d.ts","sourceRoot":"","sources":["../../src/tools/brainstorm.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAIH,OAAO,KAAK,EACV,mBAAmB,EACnB,oBAAoB,EACrB,MAAM,mBAAmB,CAAC;AA+G3B;;;;;GAKG;AACH,wBAAsB,cAAc,CAClC,KAAK,EAAE,mBAAmB,GACzB,OAAO,CAAC,oBAAoB,CAAC,CAiH/B"}
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* grok_brainstorm Tool Implementation
|
|
3
|
+
*
|
|
4
|
+
* Creative brainstorming using Grok AI
|
|
5
|
+
*/
|
|
6
|
+
import { readFile } from 'fs/promises';
|
|
7
|
+
import { resolve } from 'path';
|
|
8
|
+
import { createResponse, extractContent, extractUsage, calculateCost, } from '../utils/grok-client.js';
|
|
9
|
+
import { xaiConfig, debugMode, SUPPORTED_MODELS } from '../config/index.js';
|
|
10
|
+
/**
|
|
11
|
+
* Style description mapping
|
|
12
|
+
*/
|
|
13
|
+
const STYLE_DESCRIPTIONS = {
|
|
14
|
+
innovative: 'Innovative: Pursue novel and unique ideas, encourage thinking outside the box',
|
|
15
|
+
practical: 'Practical: Focus on feasibility and ROI, prioritize actionable solutions',
|
|
16
|
+
radical: 'Radical: Break conventional thinking, boldly challenge existing assumptions, explore disruptive solutions',
|
|
17
|
+
balanced: 'Balanced: Balance innovation with feasibility, analyze from multiple dimensions',
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* Read project file contents
|
|
21
|
+
*/
|
|
22
|
+
async function readContextFiles(files) {
|
|
23
|
+
const contents = [];
|
|
24
|
+
for (const filePath of files) {
|
|
25
|
+
try {
|
|
26
|
+
const absolutePath = resolve(filePath);
|
|
27
|
+
const content = await readFile(absolutePath, 'utf-8');
|
|
28
|
+
// Limit each file to 5000 chars to avoid token explosion
|
|
29
|
+
const truncated = content.length > 5000
|
|
30
|
+
? content.slice(0, 5000) + '\n... (file truncated, original length: ' + content.length + ' chars)'
|
|
31
|
+
: content;
|
|
32
|
+
contents.push(`--- File: ${filePath} ---\n${truncated}`);
|
|
33
|
+
}
|
|
34
|
+
catch (error) {
|
|
35
|
+
console.error(`[Brainstorm] Failed to read file ${filePath}:`, error instanceof Error ? error.message : error);
|
|
36
|
+
contents.push(`--- File: ${filePath} ---\n(read failed)`);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return contents.join('\n\n');
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Validate model name is supported
|
|
43
|
+
*/
|
|
44
|
+
function resolveModel(model) {
|
|
45
|
+
if (!model)
|
|
46
|
+
return xaiConfig.defaultModel;
|
|
47
|
+
if (SUPPORTED_MODELS.includes(model))
|
|
48
|
+
return model;
|
|
49
|
+
console.error(`[Brainstorm] Unsupported model "${model}", falling back to default ${xaiConfig.defaultModel}`);
|
|
50
|
+
return xaiConfig.defaultModel;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Build brainstorm prompt
|
|
54
|
+
*
|
|
55
|
+
* @param input - Brainstorm input parameters
|
|
56
|
+
* @param fileContents - Project file contents (optional)
|
|
57
|
+
* @returns Prompt string
|
|
58
|
+
*/
|
|
59
|
+
function buildBrainstormPrompt(input, fileContents) {
|
|
60
|
+
const count = input.count || 5;
|
|
61
|
+
const style = input.style || 'balanced';
|
|
62
|
+
const styleDesc = STYLE_DESCRIPTIONS[style] || STYLE_DESCRIPTIONS.balanced;
|
|
63
|
+
const outputFormat = input.output_format || 'text';
|
|
64
|
+
let prompt = `You are a creative thinking expert, skilled at analyzing problems from multiple angles and generating innovative ideas.
|
|
65
|
+
|
|
66
|
+
**Thinking Style**: ${styleDesc}
|
|
67
|
+
|
|
68
|
+
Please brainstorm on the following topic:
|
|
69
|
+
|
|
70
|
+
**Topic**: ${input.topic}
|
|
71
|
+
`;
|
|
72
|
+
if (input.context) {
|
|
73
|
+
prompt += `\n**Background**: ${input.context}\n`;
|
|
74
|
+
}
|
|
75
|
+
if (fileContents) {
|
|
76
|
+
prompt += `\n**Project File Reference**:\n${fileContents}\n`;
|
|
77
|
+
}
|
|
78
|
+
prompt += `\nPlease generate **${count}** creative ideas`;
|
|
79
|
+
if (outputFormat === 'json') {
|
|
80
|
+
// JSON format output requirements
|
|
81
|
+
prompt += `, and strictly return in the following JSON format (do not include markdown code block markers):
|
|
82
|
+
{"ideas": [{"title": "Idea title", "description": "Detailed description", "pros": ["Pro 1", "Pro 2"], "cons": ["Challenge 1"], "feasibility": "high or medium or low", "implementation": "Implementation suggestions"}]}`;
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
// Markdown format output requirements
|
|
86
|
+
prompt += `. Each idea should include:
|
|
87
|
+
1. **Title**: A concise and impactful title
|
|
88
|
+
2. **Description**: Detailed explanation of the core idea
|
|
89
|
+
3. **Pros**: List at least 2-3 advantages
|
|
90
|
+
4. **Potential Challenges**: List 1-2 possible challenges
|
|
91
|
+
5. **Feasibility Assessment**: High / Medium / Low
|
|
92
|
+
6. **Implementation Suggestions**: Specific steps or recommendations
|
|
93
|
+
|
|
94
|
+
Please consider the following perspectives:
|
|
95
|
+
- **Innovation**: Any unique angles or approaches?
|
|
96
|
+
- **Feasibility**: Implementation difficulty and required resources
|
|
97
|
+
- **Impact**: Potential impact on target users or business
|
|
98
|
+
- **Scalability**: Room for future growth
|
|
99
|
+
|
|
100
|
+
Please present your ideas in a clear, structured manner.`;
|
|
101
|
+
}
|
|
102
|
+
return prompt;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* grok_brainstorm main function
|
|
106
|
+
*
|
|
107
|
+
* @param input - Brainstorm input parameters
|
|
108
|
+
* @returns Brainstorm result
|
|
109
|
+
*/
|
|
110
|
+
export async function grokBrainstorm(input) {
|
|
111
|
+
try {
|
|
112
|
+
const model = resolveModel(input.model);
|
|
113
|
+
if (debugMode) {
|
|
114
|
+
console.error('[Brainstorm] Starting brainstorm:', input.topic);
|
|
115
|
+
console.error('[Brainstorm] Model:', model);
|
|
116
|
+
console.error('[Brainstorm] Count:', input.count || 5);
|
|
117
|
+
console.error('[Brainstorm] Style:', input.style || 'balanced');
|
|
118
|
+
console.error('[Brainstorm] Output format:', input.output_format || 'text');
|
|
119
|
+
if (input.context) {
|
|
120
|
+
console.error('[Brainstorm] Context:', input.context);
|
|
121
|
+
}
|
|
122
|
+
if (input.context_files) {
|
|
123
|
+
console.error('[Brainstorm] Context files:', input.context_files);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
// 1. Read project files (if specified)
|
|
127
|
+
let fileContents;
|
|
128
|
+
if (input.context_files && input.context_files.length > 0) {
|
|
129
|
+
if (debugMode) {
|
|
130
|
+
console.error('[Brainstorm] Reading project files...');
|
|
131
|
+
}
|
|
132
|
+
fileContents = await readContextFiles(input.context_files);
|
|
133
|
+
}
|
|
134
|
+
// 2. Build prompt
|
|
135
|
+
const prompt = buildBrainstormPrompt(input, fileContents);
|
|
136
|
+
// 3. Call Grok API (without search tools)
|
|
137
|
+
if (debugMode) {
|
|
138
|
+
console.error('[Brainstorm] Calling Grok API...');
|
|
139
|
+
}
|
|
140
|
+
// Adjust temperature based on style
|
|
141
|
+
let temperature;
|
|
142
|
+
switch (input.style) {
|
|
143
|
+
case 'radical':
|
|
144
|
+
temperature = 1.2; // Higher randomness, encourage bold ideas
|
|
145
|
+
break;
|
|
146
|
+
case 'innovative':
|
|
147
|
+
temperature = 0.9; // Moderate creativity
|
|
148
|
+
break;
|
|
149
|
+
case 'practical':
|
|
150
|
+
temperature = 0.5; // More conservative, focus on feasibility
|
|
151
|
+
break;
|
|
152
|
+
// balanced and default: no temperature set, use model default
|
|
153
|
+
}
|
|
154
|
+
const response = await createResponse({
|
|
155
|
+
model,
|
|
156
|
+
messages: [{ role: 'user', content: prompt }],
|
|
157
|
+
temperature,
|
|
158
|
+
// No search tools — purely relying on Grok's creative thinking
|
|
159
|
+
});
|
|
160
|
+
// 4. Extract results
|
|
161
|
+
const content = extractContent(response);
|
|
162
|
+
const usage = extractUsage(response);
|
|
163
|
+
const cost = calculateCost(response);
|
|
164
|
+
if (debugMode) {
|
|
165
|
+
console.error('[Brainstorm] Brainstorm completed');
|
|
166
|
+
console.error('[Brainstorm] Model:', model);
|
|
167
|
+
console.error('[Brainstorm] Token usage:', usage.total_tokens);
|
|
168
|
+
console.error('[Brainstorm] Reasoning tokens:', usage.reasoning_tokens);
|
|
169
|
+
console.error('[Brainstorm] Cost:', `$${cost.toFixed(6)}`);
|
|
170
|
+
}
|
|
171
|
+
// 5. Return result
|
|
172
|
+
return {
|
|
173
|
+
content,
|
|
174
|
+
usage: {
|
|
175
|
+
input_tokens: usage.input_tokens,
|
|
176
|
+
output_tokens: usage.output_tokens,
|
|
177
|
+
reasoning_tokens: usage.reasoning_tokens,
|
|
178
|
+
total_tokens: usage.total_tokens,
|
|
179
|
+
},
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
catch (error) {
|
|
183
|
+
// Error handling
|
|
184
|
+
if (error instanceof Error) {
|
|
185
|
+
console.error('[Brainstorm] Brainstorm failed:', error.message);
|
|
186
|
+
// Timeout error
|
|
187
|
+
if (error.message.includes('timeout') || error.message.includes('ETIMEDOUT')) {
|
|
188
|
+
throw new Error(`Brainstorm timed out. Suggestions:\n` +
|
|
189
|
+
`1. Simplify the topic description\n` +
|
|
190
|
+
`2. Reduce context or file count\n` +
|
|
191
|
+
`3. Try again later\n` +
|
|
192
|
+
`Original error: ${error.message}`);
|
|
193
|
+
}
|
|
194
|
+
// API error
|
|
195
|
+
if (error.message.includes('API') || error.message.includes('401')) {
|
|
196
|
+
throw new Error(`API call failed. Please check:\n` +
|
|
197
|
+
`1. Is XAI_API_KEY configured correctly?\n` +
|
|
198
|
+
`2. Is the API key valid?\n` +
|
|
199
|
+
`3. Is the network connection working?\n` +
|
|
200
|
+
`Original error: ${error.message}`);
|
|
201
|
+
}
|
|
202
|
+
// Other errors
|
|
203
|
+
throw new Error(`Brainstorm failed: ${error.message}`);
|
|
204
|
+
}
|
|
205
|
+
throw error;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
//# sourceMappingURL=brainstorm.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"brainstorm.js","sourceRoot":"","sources":["../../src/tools/brainstorm.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AACvC,OAAO,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AAK/B,OAAO,EACL,cAAc,EACd,cAAc,EACd,YAAY,EACZ,aAAa,GACd,MAAM,yBAAyB,CAAC;AACjC,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AAE5E;;GAEG;AACH,MAAM,kBAAkB,GAA2B;IACjD,UAAU,EAAE,+EAA+E;IAC3F,SAAS,EAAE,0EAA0E;IACrF,OAAO,EAAE,2GAA2G;IACpH,QAAQ,EAAE,iFAAiF;CAC5F,CAAC;AAEF;;GAEG;AACH,KAAK,UAAU,gBAAgB,CAAC,KAAe;IAC7C,MAAM,QAAQ,GAAa,EAAE,CAAC;IAE9B,KAAK,MAAM,QAAQ,IAAI,KAAK,EAAE,CAAC;QAC7B,IAAI,CAAC;YACH,MAAM,YAAY,GAAG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