@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,210 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Grok-MCP TypeScript Type Definitions
|
|
3
|
+
*
|
|
4
|
+
* Defines all types for xAI API, MCP tools, and project configuration
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* xAI Responses API request type
|
|
8
|
+
*/
|
|
9
|
+
export interface XAIResponsesRequest {
|
|
10
|
+
/** Model name, e.g. "grok-4.20-beta" */
|
|
11
|
+
model: string;
|
|
12
|
+
/** Conversation messages array */
|
|
13
|
+
messages: Array<{
|
|
14
|
+
role: 'system' | 'user' | 'assistant';
|
|
15
|
+
content: string;
|
|
16
|
+
}>;
|
|
17
|
+
/** Streaming response toggle */
|
|
18
|
+
stream: boolean;
|
|
19
|
+
/** Temperature (0-2), controls randomness */
|
|
20
|
+
temperature?: number;
|
|
21
|
+
/** Server-side tool configuration */
|
|
22
|
+
server_side_tools?: Array<XAIWebSearchTool | XAIXSearchTool>;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Web Search tool configuration
|
|
26
|
+
*/
|
|
27
|
+
export interface XAIWebSearchTool {
|
|
28
|
+
type: 'web_search';
|
|
29
|
+
/** Enable image understanding */
|
|
30
|
+
enable_image_understanding?: boolean;
|
|
31
|
+
/** Filter configuration */
|
|
32
|
+
filters?: {
|
|
33
|
+
/** Only search specified domains (max 5) */
|
|
34
|
+
allowed_domains?: string[];
|
|
35
|
+
/** Exclude specified domains (max 5) */
|
|
36
|
+
excluded_domains?: string[];
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* X Search tool configuration
|
|
41
|
+
*/
|
|
42
|
+
export interface XAIXSearchTool {
|
|
43
|
+
type: 'x_search';
|
|
44
|
+
/** Start date (ISO8601 format) */
|
|
45
|
+
from_date?: string;
|
|
46
|
+
/** End date (ISO8601 format) */
|
|
47
|
+
to_date?: string;
|
|
48
|
+
/** Enable image understanding */
|
|
49
|
+
enable_image_understanding?: boolean;
|
|
50
|
+
/** Enable video understanding */
|
|
51
|
+
enable_video_understanding?: boolean;
|
|
52
|
+
/** Only search specified users (max 10) */
|
|
53
|
+
allowed_x_handles?: string[];
|
|
54
|
+
/** Exclude specified users (max 10) */
|
|
55
|
+
excluded_x_handles?: string[];
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* xAI Responses API response type
|
|
59
|
+
*/
|
|
60
|
+
export interface XAIResponsesResponse {
|
|
61
|
+
/** Output array, may contain search call records and messages */
|
|
62
|
+
output: Array<{
|
|
63
|
+
type: string;
|
|
64
|
+
role?: 'assistant';
|
|
65
|
+
content?: Array<{
|
|
66
|
+
type: 'output_text';
|
|
67
|
+
text: string;
|
|
68
|
+
/** Text annotations (contains citation info) */
|
|
69
|
+
annotations?: Array<{
|
|
70
|
+
type: string;
|
|
71
|
+
url?: string;
|
|
72
|
+
title?: string;
|
|
73
|
+
start_index: number;
|
|
74
|
+
end_index: number;
|
|
75
|
+
}>;
|
|
76
|
+
}>;
|
|
77
|
+
/** Search call action info */
|
|
78
|
+
action?: {
|
|
79
|
+
type: string;
|
|
80
|
+
query?: string;
|
|
81
|
+
};
|
|
82
|
+
status?: string;
|
|
83
|
+
}>;
|
|
84
|
+
/** Token usage statistics */
|
|
85
|
+
usage: XAIUsage;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Token usage statistics type
|
|
89
|
+
*/
|
|
90
|
+
export interface XAIUsage {
|
|
91
|
+
/** Input tokens */
|
|
92
|
+
input_tokens: number;
|
|
93
|
+
/** Input tokens details */
|
|
94
|
+
input_tokens_details?: {
|
|
95
|
+
cached_tokens?: number;
|
|
96
|
+
};
|
|
97
|
+
/** Output tokens */
|
|
98
|
+
output_tokens: number;
|
|
99
|
+
/** Output tokens details */
|
|
100
|
+
output_tokens_details?: {
|
|
101
|
+
reasoning_tokens?: number;
|
|
102
|
+
};
|
|
103
|
+
/** Total tokens */
|
|
104
|
+
total_tokens: number;
|
|
105
|
+
/** Cost (unit: ticks, 1 tick = 0.0000000001 USD) */
|
|
106
|
+
cost_in_usd_ticks: number;
|
|
107
|
+
/** Server-side tool usage details (may be absent when no search tools used) */
|
|
108
|
+
server_side_tool_usage_details?: {
|
|
109
|
+
web_search_calls?: number;
|
|
110
|
+
x_search_calls?: number;
|
|
111
|
+
code_interpreter_calls?: number;
|
|
112
|
+
file_search_calls?: number;
|
|
113
|
+
mcp_calls?: number;
|
|
114
|
+
document_search_calls?: number;
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* grok_agent_search tool input parameters
|
|
119
|
+
*/
|
|
120
|
+
export interface GrokAgentSearchInput {
|
|
121
|
+
/** Search query */
|
|
122
|
+
query: string;
|
|
123
|
+
/** Search type */
|
|
124
|
+
search_type?: 'web' | 'x' | 'mixed';
|
|
125
|
+
/** Model to use */
|
|
126
|
+
model?: string;
|
|
127
|
+
/** Output format */
|
|
128
|
+
output_format?: 'text' | 'json';
|
|
129
|
+
/** Web Search config (when search_type is 'web' or 'mixed') */
|
|
130
|
+
web_search_config?: {
|
|
131
|
+
enable_image_understanding?: boolean;
|
|
132
|
+
allowed_domains?: string[];
|
|
133
|
+
excluded_domains?: string[];
|
|
134
|
+
};
|
|
135
|
+
/** X Search config (when search_type is 'x' or 'mixed') */
|
|
136
|
+
x_search_config?: {
|
|
137
|
+
from_date?: string;
|
|
138
|
+
to_date?: string;
|
|
139
|
+
enable_image_understanding?: boolean;
|
|
140
|
+
enable_video_understanding?: boolean;
|
|
141
|
+
allowed_x_handles?: string[];
|
|
142
|
+
excluded_x_handles?: string[];
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* grok_agent_search tool output result
|
|
147
|
+
*/
|
|
148
|
+
export interface GrokAgentSearchOutput {
|
|
149
|
+
/** Search result content */
|
|
150
|
+
content: string;
|
|
151
|
+
/** Cited URL list */
|
|
152
|
+
citations: string[];
|
|
153
|
+
/** Token usage statistics */
|
|
154
|
+
usage: {
|
|
155
|
+
input_tokens: number;
|
|
156
|
+
output_tokens: number;
|
|
157
|
+
reasoning_tokens: number;
|
|
158
|
+
cached_tokens: number;
|
|
159
|
+
total_tokens: number;
|
|
160
|
+
web_search_calls: number;
|
|
161
|
+
x_search_calls: number;
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* grok_brainstorm tool input parameters
|
|
166
|
+
*/
|
|
167
|
+
export interface GrokBrainstormInput {
|
|
168
|
+
/** Brainstorm topic */
|
|
169
|
+
topic: string;
|
|
170
|
+
/** Context information (optional) */
|
|
171
|
+
context?: string;
|
|
172
|
+
/** Project file paths, read as context */
|
|
173
|
+
context_files?: string[];
|
|
174
|
+
/** Number of ideas to generate, 1-10 */
|
|
175
|
+
count?: number;
|
|
176
|
+
/** Brainstorm style */
|
|
177
|
+
style?: 'innovative' | 'practical' | 'radical' | 'balanced';
|
|
178
|
+
/** Model to use */
|
|
179
|
+
model?: string;
|
|
180
|
+
/** Output format */
|
|
181
|
+
output_format?: 'text' | 'json';
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* grok_brainstorm tool output result
|
|
185
|
+
*/
|
|
186
|
+
export interface GrokBrainstormOutput {
|
|
187
|
+
/** Generated content */
|
|
188
|
+
content: string;
|
|
189
|
+
/** Token usage statistics */
|
|
190
|
+
usage: {
|
|
191
|
+
input_tokens: number;
|
|
192
|
+
output_tokens: number;
|
|
193
|
+
reasoning_tokens: number;
|
|
194
|
+
total_tokens: number;
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Grok-MCP project configuration type
|
|
199
|
+
*/
|
|
200
|
+
export interface GrokMCPConfig {
|
|
201
|
+
/** xAI API key */
|
|
202
|
+
apiKey: string;
|
|
203
|
+
/** API base URL */
|
|
204
|
+
baseURL: string;
|
|
205
|
+
/** Default model */
|
|
206
|
+
model: string;
|
|
207
|
+
/** Debug mode */
|
|
208
|
+
debug?: boolean;
|
|
209
|
+
}
|
|
210
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../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ACH;AAED;;GAEG;AACH,MAAM,WAAW,mBAAmB;IAClC,uBAAuB;IACvB,KAAK,EAAE,MAAM,CAAC;IACd,qCAAqC;IACrC,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,0CAA0C;IAC1C,aAAa,CAAC,EAAE,MAAM,EAAE,CAAC;IACzB,wCAAwC;IACxC,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,uBAAuB;IACvB,KAAK,CAAC,EAAE,YAAY,GAAG,WAAW,GAAG,SAAS,GAAG,UAAU,CAAC;IAC5D,mBAAmB;IACnB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,oBAAoB;IACpB,aAAa,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;CACjC;AAED;;GAEG;AACH,MAAM,WAAW,oBAAoB;IACnC,wBAAwB;IACxB,OAAO,EAAE,MAAM,CAAC;IAChB,6BAA6B;IAC7B,KAAK,EAAE;QACL,YAAY,EAAE,MAAM,CAAC;QACrB,aAAa,EAAE,MAAM,CAAC;QACtB,gBAAgB,EAAE,MAAM,CAAC;QACzB,YAAY,EAAE,MAAM,CAAC;KACtB,CAAC;CACH;AAMD;;GAEG;AACH,MAAM,WAAW,aAAa;IAC5B,kBAAkB;IAClB,MAAM,EAAE,MAAM,CAAC;IACf,mBAAmB;IACnB,OAAO,EAAE,MAAM,CAAC;IAChB,oBAAoB;IACpB,KAAK,EAAE,MAAM,CAAC;IACd,iBAAiB;IACjB,KAAK,CAAC,EAAE,OAAO,CAAC;CACjB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG"}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Grok API Client
|
|
3
|
+
*
|
|
4
|
+
* Uses native fetch to call xAI Responses API
|
|
5
|
+
* Native fetch (undici) automatically reads HTTPS_PROXY env var, no extra proxy config needed
|
|
6
|
+
*/
|
|
7
|
+
import type { XAIResponsesRequest, XAIResponsesResponse } from '../types/index.js';
|
|
8
|
+
/**
|
|
9
|
+
* Call xAI Responses API
|
|
10
|
+
*
|
|
11
|
+
* @param request - API request parameters
|
|
12
|
+
* @returns API response
|
|
13
|
+
*/
|
|
14
|
+
export declare function createResponse(request: Omit<XAIResponsesRequest, 'stream'>): Promise<XAIResponsesResponse>;
|
|
15
|
+
/**
|
|
16
|
+
* Extract text content from response
|
|
17
|
+
*
|
|
18
|
+
* @param response - API response
|
|
19
|
+
* @returns Text content
|
|
20
|
+
*/
|
|
21
|
+
export declare function extractContent(response: XAIResponsesResponse): string;
|
|
22
|
+
/**
|
|
23
|
+
* Extract citation URLs from text
|
|
24
|
+
*
|
|
25
|
+
* xAI citation format: [[1]](https://example.com)
|
|
26
|
+
*
|
|
27
|
+
* @param text - Text containing citations
|
|
28
|
+
* @returns Deduplicated citation URL array
|
|
29
|
+
*/
|
|
30
|
+
export declare function extractCitations(text: string): string[];
|
|
31
|
+
/**
|
|
32
|
+
* Extract token usage statistics
|
|
33
|
+
*
|
|
34
|
+
* @param response - API response
|
|
35
|
+
* @returns Token usage statistics object
|
|
36
|
+
*/
|
|
37
|
+
export declare function extractUsage(response: XAIResponsesResponse): {
|
|
38
|
+
input_tokens: number;
|
|
39
|
+
output_tokens: number;
|
|
40
|
+
reasoning_tokens: number;
|
|
41
|
+
cached_tokens: number;
|
|
42
|
+
total_tokens: number;
|
|
43
|
+
web_search_calls: number;
|
|
44
|
+
x_search_calls: number;
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* Calculate API call cost
|
|
48
|
+
*
|
|
49
|
+
* @param response - API response
|
|
50
|
+
* @returns Cost in USD
|
|
51
|
+
*/
|
|
52
|
+
export declare function calculateCost(response: XAIResponsesResponse): number;
|
|
53
|
+
//# sourceMappingURL=grok-client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"grok-client.d.ts","sourceRoot":"","sources":["../../src/utils/grok-client.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EACV,mBAAmB,EACnB,oBAAoB,EAGrB,MAAM,mBAAmB,CAAC;AAO3B;;;;;GAKG;AACH,wBAAsB,cAAc,CAClC,OAAO,EAAE,IAAI,CAAC,mBAAmB,EAAE,QAAQ,CAAC,GAC3C,OAAO,CAAC,oBAAoB,CAAC,CA2E/B;AAMD;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,QAAQ,EAAE,oBAAoB,GAAG,MAAM,CAarE;AAED;;;;;;;GAOG;AACH,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,EAAE,CAWvD;AAED;;;;;GAKG;AACH,wBAAgB,YAAY,CAAC,QAAQ,EAAE,oBAAoB;;;;;;;;EAY1D;AAED;;;;;GAKG;AACH,wBAAgB,aAAa,CAAC,QAAQ,EAAE,oBAAoB,GAAG,MAAM,CAKpE"}
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Grok API Client
|
|
3
|
+
*
|
|
4
|
+
* Uses native fetch to call xAI Responses API
|
|
5
|
+
* Native fetch (undici) automatically reads HTTPS_PROXY env var, no extra proxy config needed
|
|
6
|
+
*/
|
|
7
|
+
import { xaiConfig, debugMode } from '../config/index.js';
|
|
8
|
+
// ============================================================================
|
|
9
|
+
// Core API Methods
|
|
10
|
+
// ============================================================================
|
|
11
|
+
/**
|
|
12
|
+
* Call xAI Responses API
|
|
13
|
+
*
|
|
14
|
+
* @param request - API request parameters
|
|
15
|
+
* @returns API response
|
|
16
|
+
*/
|
|
17
|
+
export async function createResponse(request) {
|
|
18
|
+
const url = `${xaiConfig.baseURL}/responses`;
|
|
19
|
+
const body = {
|
|
20
|
+
model: request.model,
|
|
21
|
+
input: request.messages,
|
|
22
|
+
stream: false,
|
|
23
|
+
};
|
|
24
|
+
if (request.temperature !== undefined) {
|
|
25
|
+
body.temperature = request.temperature;
|
|
26
|
+
}
|
|
27
|
+
if (request.server_side_tools) {
|
|
28
|
+
body.tools = request.server_side_tools;
|
|
29
|
+
}
|
|
30
|
+
if (debugMode) {
|
|
31
|
+
console.error('[Grok Client] Sending request:', JSON.stringify(body, null, 2));
|
|
32
|
+
}
|
|
33
|
+
// Retry logic: up to 3 attempts
|
|
34
|
+
let lastError = null;
|
|
35
|
+
for (let attempt = 0; attempt < 3; attempt++) {
|
|
36
|
+
try {
|
|
37
|
+
const controller = new AbortController();
|
|
38
|
+
const timeoutId = setTimeout(() => controller.abort(), xaiConfig.timeout);
|
|
39
|
+
const res = await fetch(url, {
|
|
40
|
+
method: 'POST',
|
|
41
|
+
headers: {
|
|
42
|
+
'Content-Type': 'application/json',
|
|
43
|
+
'Authorization': `Bearer ${xaiConfig.apiKey}`,
|
|
44
|
+
},
|
|
45
|
+
body: JSON.stringify(body),
|
|
46
|
+
signal: controller.signal,
|
|
47
|
+
});
|
|
48
|
+
clearTimeout(timeoutId);
|
|
49
|
+
if (!res.ok) {
|
|
50
|
+
const errorText = await res.text();
|
|
51
|
+
throw new Error(`API request failed (${res.status}): ${errorText}`);
|
|
52
|
+
}
|
|
53
|
+
const response = await res.json();
|
|
54
|
+
if (debugMode) {
|
|
55
|
+
console.error('[Grok Client] Received response:', JSON.stringify(response, null, 2));
|
|
56
|
+
}
|
|
57
|
+
return response;
|
|
58
|
+
}
|
|
59
|
+
catch (error) {
|
|
60
|
+
lastError = error instanceof Error ? error : new Error(String(error));
|
|
61
|
+
// If timeout or network error, wait and retry
|
|
62
|
+
if (attempt < 2 && (lastError.name === 'AbortError' ||
|
|
63
|
+
lastError.message.includes('fetch failed') ||
|
|
64
|
+
lastError.message.includes('ECONNRESET'))) {
|
|
65
|
+
const delay = Math.pow(2, attempt) * 1000; // Exponential backoff: 1s, 2s
|
|
66
|
+
if (debugMode) {
|
|
67
|
+
console.error(`[Grok Client] Retry attempt ${attempt + 1}, waiting ${delay}ms...`);
|
|
68
|
+
}
|
|
69
|
+
await new Promise(resolve => setTimeout(resolve, delay));
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
break;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
console.error('[Grok Client] API call failed:', lastError);
|
|
76
|
+
throw lastError;
|
|
77
|
+
}
|
|
78
|
+
// ============================================================================
|
|
79
|
+
// Helper Methods
|
|
80
|
+
// ============================================================================
|
|
81
|
+
/**
|
|
82
|
+
* Extract text content from response
|
|
83
|
+
*
|
|
84
|
+
* @param response - API response
|
|
85
|
+
* @returns Text content
|
|
86
|
+
*/
|
|
87
|
+
export function extractContent(response) {
|
|
88
|
+
// Output array may contain web_search_call, x_search_call, etc. — find the message type
|
|
89
|
+
const messageOutput = response.output?.find(item => item.type === 'message');
|
|
90
|
+
if (!messageOutput) {
|
|
91
|
+
throw new Error('Invalid response structure: missing message type output');
|
|
92
|
+
}
|
|
93
|
+
const content = messageOutput.content?.[0];
|
|
94
|
+
if (!content || content.type !== 'output_text') {
|
|
95
|
+
throw new Error('Invalid response structure: missing output_text type content');
|
|
96
|
+
}
|
|
97
|
+
return content.text;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Extract citation URLs from text
|
|
101
|
+
*
|
|
102
|
+
* xAI citation format: [[1]](https://example.com)
|
|
103
|
+
*
|
|
104
|
+
* @param text - Text containing citations
|
|
105
|
+
* @returns Deduplicated citation URL array
|
|
106
|
+
*/
|
|
107
|
+
export function extractCitations(text) {
|
|
108
|
+
const citationRegex = /\[\[(\d+)\]\]\((https?:\/\/[^\)]+)\)/g;
|
|
109
|
+
const citations = [];
|
|
110
|
+
let match;
|
|
111
|
+
while ((match = citationRegex.exec(text)) !== null) {
|
|
112
|
+
citations.push(match[2]); // Extract URL
|
|
113
|
+
}
|
|
114
|
+
// Deduplicate and return
|
|
115
|
+
return [...new Set(citations)];
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Extract token usage statistics
|
|
119
|
+
*
|
|
120
|
+
* @param response - API response
|
|
121
|
+
* @returns Token usage statistics object
|
|
122
|
+
*/
|
|
123
|
+
export function extractUsage(response) {
|
|
124
|
+
const usage = response.usage;
|
|
125
|
+
return {
|
|
126
|
+
input_tokens: usage.input_tokens,
|
|
127
|
+
output_tokens: usage.output_tokens,
|
|
128
|
+
reasoning_tokens: usage.output_tokens_details?.reasoning_tokens || 0,
|
|
129
|
+
cached_tokens: usage.input_tokens_details?.cached_tokens || 0,
|
|
130
|
+
total_tokens: usage.total_tokens,
|
|
131
|
+
web_search_calls: usage.server_side_tool_usage_details?.web_search_calls || 0,
|
|
132
|
+
x_search_calls: usage.server_side_tool_usage_details?.x_search_calls || 0,
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Calculate API call cost
|
|
137
|
+
*
|
|
138
|
+
* @param response - API response
|
|
139
|
+
* @returns Cost in USD
|
|
140
|
+
*/
|
|
141
|
+
export function calculateCost(response) {
|
|
142
|
+
const costInTicks = response.usage.cost_in_usd_ticks;
|
|
143
|
+
// 1 tick = 0.0000000001 USD
|
|
144
|
+
// i.e. 10,000,000,000 ticks = 1 USD
|
|
145
|
+
return costInTicks / 10_000_000_000;
|
|
146
|
+
}
|
|
147
|
+
//# sourceMappingURL=grok-client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"grok-client.js","sourceRoot":"","sources":["../../src/utils/grok-client.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAQH,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAE1D,+EAA+E;AAC/E,mBAAmB;AACnB,+EAA+E;AAE/E;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,cAAc,CAClC,OAA4C;IAE5C,MAAM,GAAG,GAAG,GAAG,SAAS,CAAC,OAAO,YAAY,CAAC;IAE7C,MAAM,IAAI,GAA4B;QACpC,KAAK,EAAE,OAAO,CAAC,KAAK;QACpB,KAAK,EAAE,OAAO,CAAC,QAAQ;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,IAAI,CAAC;QACpE,aAAa,EAAE,KAAK,CAAC,oBAAoB,EAAE,aAAa,IAAI,CAAC;QAC7D,YAAY,EAAE,KAAK,CAAC,YAAY;QAChC,gBAAgB,EAAE,KAAK,CAAC,8BAA8B,EAAE,gBAAgB,IAAI,CAAC;QAC7E,cAAc,EAAE,KAAK,CAAC,8BAA8B,EAAE,cAAc,IAAI,CAAC;KAC1E,CAAC;AACJ,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,aAAa,CAAC,QAA8B;IAC1D,MAAM,WAAW,GAAG,QAAQ,CAAC,KAAK,CAAC,iBAAiB,CAAC;IACrD,4BAA4B;IAC5B,oCAAoC;IACpC,OAAO,WAAW,GAAG,cAAc,CAAC;AACtC,CAAC"}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Logger Module
|
|
3
|
+
*
|
|
4
|
+
* Provides a unified logging interface with different log levels
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Log levels
|
|
8
|
+
*/
|
|
9
|
+
export declare enum LogLevel {
|
|
10
|
+
DEBUG = "DEBUG",
|
|
11
|
+
INFO = "INFO",
|
|
12
|
+
WARN = "WARN",
|
|
13
|
+
ERROR = "ERROR"
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Logger
|
|
17
|
+
*/
|
|
18
|
+
export declare const logger: {
|
|
19
|
+
/**
|
|
20
|
+
* Debug log (only outputs in DEBUG mode)
|
|
21
|
+
*/
|
|
22
|
+
debug: (message: string, meta?: any) => void;
|
|
23
|
+
/**
|
|
24
|
+
* Info log
|
|
25
|
+
*/
|
|
26
|
+
info: (message: string, meta?: any) => void;
|
|
27
|
+
/**
|
|
28
|
+
* Warning log
|
|
29
|
+
*/
|
|
30
|
+
warn: (message: string, meta?: any) => void;
|
|
31
|
+
/**
|
|
32
|
+
* Error log
|
|
33
|
+
*/
|
|
34
|
+
error: (message: string, error?: Error | any) => void;
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* Performance monitor
|
|
38
|
+
*/
|
|
39
|
+
export declare class PerformanceMonitor {
|
|
40
|
+
private startTime;
|
|
41
|
+
private label;
|
|
42
|
+
constructor(label: string);
|
|
43
|
+
/**
|
|
44
|
+
* End monitoring and log duration
|
|
45
|
+
*/
|
|
46
|
+
end(meta?: any): number;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Create a performance monitor instance
|
|
50
|
+
*/
|
|
51
|
+
export declare function startPerformanceMonitor(label: string): PerformanceMonitor;
|
|
52
|
+
//# sourceMappingURL=logger.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../../src/utils/logger.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAIH;;GAEG;AACH,oBAAY,QAAQ;IAClB,KAAK,UAAU;IACf,IAAI,SAAS;IACb,IAAI,SAAS;IACb,KAAK,UAAU;CAChB;AASD;;GAEG;AACH,eAAO,MAAM,MAAM;IACjB;;OAEG;qBACc,MAAM,SAAS,GAAG;IASnC;;OAEG;oBACa,MAAM,SAAS,GAAG;IAOlC;;OAEG;oBACa,MAAM,SAAS,GAAG;IAOlC;;OAEG;qBACc,MAAM,UAAU,KAAK,GAAG,GAAG;CAU7C,CAAC;AAEF;;GAEG;AACH,qBAAa,kBAAkB;IAC7B,OAAO,CAAC,SAAS,CAAS;IAC1B,OAAO,CAAC,KAAK,CAAS;gBAEV,KAAK,EAAE,MAAM;IAMzB;;OAEG;IACH,GAAG,CAAC,IAAI,CAAC,EAAE,GAAG,GAAG,MAAM;CAKxB;AAED;;GAEG;AACH,wBAAgB,uBAAuB,CAAC,KAAK,EAAE,MAAM,GAAG,kBAAkB,CAEzE"}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Logger Module
|
|
3
|
+
*
|
|
4
|
+
* Provides a unified logging interface with different log levels
|
|
5
|
+
*/
|
|
6
|
+
import { debugMode } from '../config/index.js';
|
|
7
|
+
/**
|
|
8
|
+
* Log levels
|
|
9
|
+
*/
|
|
10
|
+
export var LogLevel;
|
|
11
|
+
(function (LogLevel) {
|
|
12
|
+
LogLevel["DEBUG"] = "DEBUG";
|
|
13
|
+
LogLevel["INFO"] = "INFO";
|
|
14
|
+
LogLevel["WARN"] = "WARN";
|
|
15
|
+
LogLevel["ERROR"] = "ERROR";
|
|
16
|
+
})(LogLevel || (LogLevel = {}));
|
|
17
|
+
/**
|
|
18
|
+
* Format timestamp
|
|
19
|
+
*/
|
|
20
|
+
function getTimestamp() {
|
|
21
|
+
return new Date().toISOString();
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Logger
|
|
25
|
+
*/
|
|
26
|
+
export const logger = {
|
|
27
|
+
/**
|
|
28
|
+
* Debug log (only outputs in DEBUG mode)
|
|
29
|
+
*/
|
|
30
|
+
debug: (message, meta) => {
|
|
31
|
+
if (debugMode) {
|
|
32
|
+
console.error(`[${LogLevel.DEBUG}] ${getTimestamp()} - ${message}`);
|
|
33
|
+
if (meta) {
|
|
34
|
+
console.error(JSON.stringify(meta, null, 2));
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
/**
|
|
39
|
+
* Info log
|
|
40
|
+
*/
|
|
41
|
+
info: (message, meta) => {
|
|
42
|
+
console.error(`[${LogLevel.INFO}] ${getTimestamp()} - ${message}`);
|
|
43
|
+
if (meta) {
|
|
44
|
+
console.error(JSON.stringify(meta, null, 2));
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
/**
|
|
48
|
+
* Warning log
|
|
49
|
+
*/
|
|
50
|
+
warn: (message, meta) => {
|
|
51
|
+
console.error(`[${LogLevel.WARN}] ${getTimestamp()} - ${message}`);
|
|
52
|
+
if (meta) {
|
|
53
|
+
console.error(JSON.stringify(meta, null, 2));
|
|
54
|
+
}
|
|
55
|
+
},
|
|
56
|
+
/**
|
|
57
|
+
* Error log
|
|
58
|
+
*/
|
|
59
|
+
error: (message, error) => {
|
|
60
|
+
console.error(`[${LogLevel.ERROR}] ${getTimestamp()} - ${message}`);
|
|
61
|
+
if (error) {
|
|
62
|
+
if (error instanceof Error) {
|
|
63
|
+
console.error(error.stack || error.message);
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
console.error(JSON.stringify(error, null, 2));
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
},
|
|
70
|
+
};
|
|
71
|
+
/**
|
|
72
|
+
* Performance monitor
|
|
73
|
+
*/
|
|
74
|
+
export class PerformanceMonitor {
|
|
75
|
+
startTime;
|
|
76
|
+
label;
|
|
77
|
+
constructor(label) {
|
|
78
|
+
this.label = label;
|
|
79
|
+
this.startTime = Date.now();
|
|
80
|
+
logger.debug(`[Perf] ${label} started`);
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* End monitoring and log duration
|
|
84
|
+
*/
|
|
85
|
+
end(meta) {
|
|
86
|
+
const duration = Date.now() - this.startTime;
|
|
87
|
+
logger.debug(`[Perf] ${this.label} completed, duration: ${duration}ms`, meta);
|
|
88
|
+
return duration;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Create a performance monitor instance
|
|
93
|
+
*/
|
|
94
|
+
export function startPerformanceMonitor(label) {
|
|
95
|
+
return new PerformanceMonitor(label);
|
|
96
|
+
}
|
|
97
|
+
//# sourceMappingURL=logger.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"logger.js","sourceRoot":"","sources":["../../src/utils/logger.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAE/C;;GAEG;AACH,MAAM,CAAN,IAAY,QAKX;AALD,WAAY,QAAQ;IAClB,2BAAe,CAAA;IACf,yBAAa,CAAA;IACb,yBAAa,CAAA;IACb,2BAAe,CAAA;AACjB,CAAC,EALW,QAAQ,KAAR,Q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import type { XAIWebSearchTool, XAIXSearchTool } from '../types/index.js';
|
|
7
|
+
/**
|
|
8
|
+
* Build Web Search tool configuration
|
|
9
|
+
*
|
|
10
|
+
* @param options - Web Search configuration options
|
|
11
|
+
* @returns Web Search tool object
|
|
12
|
+
* @throws If parameter validation fails
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* const tool = buildWebSearchTool({
|
|
17
|
+
* allowedDomains: ['https://example.com'],
|
|
18
|
+
* enableImageUnderstanding: true
|
|
19
|
+
* });
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
export declare function buildWebSearchTool(options?: {
|
|
23
|
+
allowedDomains?: string[];
|
|
24
|
+
excludedDomains?: string[];
|
|
25
|
+
enableImageUnderstanding?: boolean;
|
|
26
|
+
}): XAIWebSearchTool;
|
|
27
|
+
/**
|
|
28
|
+
* Build X Search tool configuration
|
|
29
|
+
*
|
|
30
|
+
* @param options - X Search configuration options
|
|
31
|
+
* @returns X Search tool object
|
|
32
|
+
* @throws If parameter validation fails
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```typescript
|
|
36
|
+
* const tool = buildXSearchTool({
|
|
37
|
+
* fromDate: '2024-01-01T00:00:00Z',
|
|
38
|
+
* toDate: '2024-12-31T23:59:59Z',
|
|
39
|
+
* allowedXHandles: ['elonmusk', 'OpenAI'],
|
|
40
|
+
* enableVideoUnderstanding: true
|
|
41
|
+
* });
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
export declare function buildXSearchTool(options?: {
|
|
45
|
+
fromDate?: string;
|
|
46
|
+
toDate?: string;
|
|
47
|
+
allowedXHandles?: string[];
|
|
48
|
+
excludedXHandles?: string[];
|
|
49
|
+
enableImageUnderstanding?: boolean;
|
|
50
|
+
enableVideoUnderstanding?: boolean;
|
|
51
|
+
}): XAIXSearchTool;
|
|
52
|
+
/**
|
|
53
|
+
* Validate ISO8601 date format
|
|
54
|
+
*
|
|
55
|
+
* @param dateString - Date string
|
|
56
|
+
* @returns Whether the string is valid ISO8601 format
|
|
57
|
+
*/
|
|
58
|
+
export declare function isValidISO8601(dateString: string): boolean;
|
|
59
|
+
/**
|
|
60
|
+
* Convert date to ISO8601 format
|
|
61
|
+
*
|
|
62
|
+
* @param date - Date object
|
|
63
|
+
* @returns ISO8601 formatted date string
|
|
64
|
+
*/
|
|
65
|
+
export declare function toISO8601(date: Date): string;
|
|
66
|
+
//# sourceMappingURL=tool-builder.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tool-builder.d.ts","sourceRoot":"","sources":["../../src/utils/tool-builder.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAGH,OAAO,KAAK,EAAE,gBAAgB,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AA6D1E;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,kBAAkB,CAAC,OAAO,CAAC,EAAE;IAC3C,cAAc,CAAC,EAAE,MAAM,EAAE,CAAC;IAC1B,eAAe,CAAC,EAAE,MAAM,EAAE,CAAC;IAC3B,wBAAwB,CAAC,EAAE,OAAO,CAAC;CACpC,GAAG,gBAAgB,CAwCnB;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,gBAAgB,CAAC,OAAO,CAAC,EAAE;IACzC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,eAAe,CAAC,EAAE,MAAM,EAAE,CAAC;IAC3B,gBAAgB,CAAC,EAAE,MAAM,EAAE,CAAC;IAC5B,wBAAwB,CAAC,EAAE,OAAO,CAAC;IACnC,wBAAwB,CAAC,EAAE,OAAO,CAAC;CACpC,GAAG,cAAc,CA+CjB;AAMD;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,UAAU,EAAE,MAAM,GAAG,OAAO,CAQ1D;AAED;;;;;GAKG;AACH,wBAAgB,SAAS,CAAC,IAAI,EAAE,IAAI,GAAG,MAAM,CAE5C"}
|