@scoheart/x-search-mcp 0.3.0 → 0.4.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 +3 -3
- package/TOOL.md +8 -6
- package/dist/package.json +2 -1
- package/dist/src/auth.d.ts.map +1 -1
- package/dist/src/auth.js +61 -46
- package/dist/src/auth.js.map +1 -1
- package/dist/src/call-log.d.ts.map +1 -1
- package/dist/src/call-log.js +52 -33
- package/dist/src/call-log.js.map +1 -1
- package/dist/src/device-login.d.ts +46 -0
- package/dist/src/device-login.d.ts.map +1 -0
- package/dist/src/device-login.js +72 -0
- package/dist/src/device-login.js.map +1 -0
- package/dist/src/multi-user/service.js +2 -2
- package/dist/src/multi-user/service.js.map +1 -1
- package/dist/src/search-input.d.ts +43 -0
- package/dist/src/search-input.d.ts.map +1 -0
- package/dist/src/search-input.js +105 -0
- package/dist/src/search-input.js.map +1 -0
- package/dist/src/search-response.d.ts +6 -0
- package/dist/src/search-response.d.ts.map +1 -0
- package/dist/src/search-response.js +125 -0
- package/dist/src/search-response.js.map +1 -0
- package/dist/src/search.d.ts +3 -19
- package/dist/src/search.d.ts.map +1 -1
- package/dist/src/search.js +54 -312
- package/dist/src/search.js.map +1 -1
- package/dist/src/server.d.ts +2 -0
- package/dist/src/server.d.ts.map +1 -1
- package/dist/src/server.js +61 -158
- package/dist/src/server.js.map +1 -1
- package/dist/src/tool-contract.d.ts +150 -0
- package/dist/src/tool-contract.d.ts.map +1 -0
- package/dist/src/tool-contract.js +83 -0
- package/dist/src/tool-contract.js.map +1 -0
- package/examples/codex.toml +1 -1
- package/examples/mcp.json +1 -1
- package/package.json +2 -1
- package/src/auth.ts +59 -46
- package/src/call-log.ts +52 -31
- package/src/device-login.ts +84 -0
- package/src/multi-user/service.ts +2 -2
- package/src/search-input.ts +113 -0
- package/src/search-response.ts +127 -0
- package/src/search.ts +127 -418
- package/src/server.ts +66 -170
- package/src/tool-contract.ts +90 -0
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import type { InlineCitation, ServerSideCall, XSearchResult } from './types.js';
|
|
2
|
+
|
|
3
|
+
const xSearchToolNames = new Set([
|
|
4
|
+
'x_search',
|
|
5
|
+
'x_keyword_search',
|
|
6
|
+
'x_semantic_search',
|
|
7
|
+
'x_user_search',
|
|
8
|
+
'x_thread_fetch',
|
|
9
|
+
]);
|
|
10
|
+
|
|
11
|
+
type SearchResponse = Pick<XSearchResult, 'answer' | 'citations' | 'inline_citations' | 'search_execution'
|
|
12
|
+
| 'server_side_calls' | 'response_status' | 'incomplete_details' | 'usage'>;
|
|
13
|
+
|
|
14
|
+
function record(value: unknown): Record<string, unknown> | undefined {
|
|
15
|
+
return value !== null && typeof value === 'object' && !Array.isArray(value)
|
|
16
|
+
? value as Record<string, unknown> : undefined;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function text(value: unknown): string {
|
|
20
|
+
return typeof value === 'string' ? value.trim() : '';
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function citationUrl(value: unknown): string | undefined {
|
|
24
|
+
const candidate = text(value);
|
|
25
|
+
if (!/^https?:\/\//i.test(candidate) || /[\x00-\x1f\x7f]/.test(candidate)) return undefined;
|
|
26
|
+
try {
|
|
27
|
+
const url = new URL(candidate);
|
|
28
|
+
return url.username || url.password ? undefined : candidate;
|
|
29
|
+
} catch {
|
|
30
|
+
return undefined;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function citationOffset(value: unknown): number | null {
|
|
35
|
+
return typeof value === 'number' && Number.isSafeInteger(value) && value >= 0 ? value : null;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function inlineCitation(value: unknown): InlineCitation | undefined {
|
|
39
|
+
const annotation = record(value);
|
|
40
|
+
if (!annotation || (annotation.type !== 'url_citation' && !annotation.url)) return undefined;
|
|
41
|
+
const nested = record(annotation.citation);
|
|
42
|
+
const url = citationUrl(annotation.url) ?? citationUrl(nested?.url);
|
|
43
|
+
if (!url) return undefined;
|
|
44
|
+
return {
|
|
45
|
+
url,
|
|
46
|
+
title: text(annotation.title) || text(nested?.title) || null,
|
|
47
|
+
start_index: citationOffset(annotation.start_index),
|
|
48
|
+
end_index: citationOffset(annotation.end_index),
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function hasXSearchUsage(usage: unknown): boolean {
|
|
53
|
+
const details = record(record(usage)?.server_side_tool_usage_details);
|
|
54
|
+
return [details?.x_posts_fetched, details?.x_users_fetched].some(
|
|
55
|
+
count => typeof count === 'number' && Number.isSafeInteger(count) && count > 0,
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** Normalize xAI's visible answer, citations and evidence independently of transport and credentials. */
|
|
60
|
+
export function normalizeSearchResponse(raw: Record<string, unknown>): SearchResponse {
|
|
61
|
+
const canonicalAnswer = text(raw.output_text);
|
|
62
|
+
const answerPieces: string[] = [];
|
|
63
|
+
const citations = new Set<string>();
|
|
64
|
+
const inlineCitations: InlineCitation[] = [];
|
|
65
|
+
const serverSideCalls: ServerSideCall[] = [];
|
|
66
|
+
let xSearchObserved = hasXSearchUsage(raw.usage);
|
|
67
|
+
|
|
68
|
+
for (const sources of [raw.citations, raw.sources]) {
|
|
69
|
+
if (!Array.isArray(sources)) continue;
|
|
70
|
+
for (const source of sources) {
|
|
71
|
+
const url = citationUrl(source);
|
|
72
|
+
if (url) citations.add(url);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
for (const value of Array.isArray(raw.output) ? raw.output : []) {
|
|
77
|
+
const item = record(value);
|
|
78
|
+
if (!item) continue;
|
|
79
|
+
const name = text(item.name) || text(item.tool_name);
|
|
80
|
+
const callId = text(item.call_id) || text(item.id);
|
|
81
|
+
const isXSearch = xSearchToolNames.has(name) || callId.startsWith('xs_');
|
|
82
|
+
if (isXSearch) xSearchObserved = true;
|
|
83
|
+
if ((isXSearch || item.type === 'custom_tool_call') && (name || callId)) {
|
|
84
|
+
serverSideCalls.push({
|
|
85
|
+
call_id: callId || null,
|
|
86
|
+
name: name || null,
|
|
87
|
+
arguments: item.arguments ?? item.input ?? null,
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if (item.type !== 'message' && item.type !== undefined) continue;
|
|
92
|
+
const message = record(item.message) ?? item;
|
|
93
|
+
const role = message.role ?? item.role;
|
|
94
|
+
if (role !== undefined && role !== 'assistant') continue;
|
|
95
|
+
if (typeof message.content === 'string') {
|
|
96
|
+
const piece = text(message.content);
|
|
97
|
+
if (piece && !canonicalAnswer) answerPieces.push(piece);
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
if (!Array.isArray(message.content)) continue;
|
|
101
|
+
for (const blockValue of message.content) {
|
|
102
|
+
const block = record(blockValue);
|
|
103
|
+
if (!block || (block.type !== undefined && block.type !== 'output_text' && block.type !== 'text')) continue;
|
|
104
|
+
const piece = text(block.output_text) || text(block.text);
|
|
105
|
+
if (piece && !canonicalAnswer) answerPieces.push(piece);
|
|
106
|
+
if (!Array.isArray(block.annotations)) continue;
|
|
107
|
+
for (const annotation of block.annotations) {
|
|
108
|
+
const citation = inlineCitation(annotation);
|
|
109
|
+
if (citation) {
|
|
110
|
+
inlineCitations.push(citation);
|
|
111
|
+
citations.add(citation.url);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
return {
|
|
118
|
+
answer: canonicalAnswer || answerPieces.join('\n\n'),
|
|
119
|
+
citations: [...citations],
|
|
120
|
+
inline_citations: inlineCitations,
|
|
121
|
+
search_execution: xSearchObserved ? 'observed' : 'unreported',
|
|
122
|
+
server_side_calls: serverSideCalls,
|
|
123
|
+
response_status: text(raw.status) || 'completed',
|
|
124
|
+
incomplete_details: raw.incomplete_details ?? null,
|
|
125
|
+
usage: raw.usage ?? null,
|
|
126
|
+
};
|
|
127
|
+
}
|