-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathx-search-tool.ts
More file actions
224 lines (202 loc) · 8.08 KB
/
Copy pathx-search-tool.ts
File metadata and controls
224 lines (202 loc) · 8.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/**
* x_search tool: proxy xAI's built-in X Search via a separate API call.
*
* When the model calls this tool, we make an independent request to xAI's
* Responses API with the server-side x_search tool. This means:
*
* - Any model can search X
* - The search call uses a dedicated model with full x_search support
* - Results come back as structured tool output visible in pi's UI
* - Per-query parameters (handles, date ranges) are supported
*/
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
import { Type } from "typebox";
import { CLI_PROXY_BASE_URL, buildProxyHeaders } from "./models.js";
import { readBoundedJson, readBoundedText, safeFetch } from "./safe-fetch.js";
// ─── Config ──────────────────────────────────────────────────────────────────
const SEARCH_MODEL = process.env.PI_XAI_X_SEARCH_MODEL ?? "grok-4.5";
/** Reject any x_search response body larger than this before parsing. */
const SEARCH_MAX_RESPONSE_BYTES = 256 * 1024;
/** Deadline for the x_search model call (search X + synthesize). Measured
* latency is 30-60s for simple queries under normal load; complex queries or
* periods of heavy load on the proxy take longer, so this leaves headroom. */
const SEARCH_TIMEOUT_MS = 120_000;
// ─── Types ───────────────────────────────────────────────────────────────────
interface XSearchResult {
answer: string;
citations?: Array<{ url: string; title?: string }>;
}
/** Map a thrown x_search error to the user-facing text and status shown in
* the tool result. 401 gets a re-login hint; anything else surfaces the
* message. Extracted so the mapping is testable without a tool ctx. */
export function formatXSearchError(err: unknown): { text: string; status?: number } {
if (err instanceof XSearchHttpError) {
if (err.status === 401) {
return { text: "xAI authentication failed. Run /login to re-authenticate.", status: 401 };
}
return { text: `x_search failed: ${err.message}`, status: err.status };
}
return { text: `x_search failed: ${err instanceof Error ? err.message : String(err)}` };
}
/** HTTP failure from the x_search Responses call, carrying the status so the
* tool handler can map 401 to a re-login message. The body is read only to
* drain the response; the upstream text never lands in the message, which
* classifies by status instead so a hostile error page can't push wording
* into the tool result. */
export class XSearchHttpError extends Error {
constructor(public readonly status: number) {
super(`x_search failed: HTTP ${status}`);
this.name = "XSearchHttpError";
}
}
// ─── API call ────────────────────────────────────────────────────────────────
export async function callXSearch(
apiKey: string,
baseUrl: string,
query: string,
options?: {
allowedXHandles?: string[];
excludedXHandles?: string[];
fromDate?: string;
toDate?: string;
},
signal?: AbortSignal,
): Promise<XSearchResult> {
const xSearchTool: Record<string, unknown> = { type: "x_search" };
if (options?.allowedXHandles?.length) xSearchTool.allowed_x_handles = options.allowedXHandles;
if (options?.excludedXHandles?.length) xSearchTool.excluded_x_handles = options.excludedXHandles;
if (options?.fromDate) xSearchTool.from_date = options.fromDate;
if (options?.toDate) xSearchTool.to_date = options.toDate;
const payload = {
model: SEARCH_MODEL,
input: [{ role: "user", content: query }],
tools: [xSearchTool],
store: false,
};
const response = await safeFetch(`${baseUrl}/responses`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${apiKey}`,
...buildProxyHeaders(SEARCH_MODEL),
},
body: JSON.stringify(payload),
// x_search is a model inference call (search X, synthesize an answer),
// not a quick API read. Measured latency is 30-60s for simple queries
// and longer for complex ones, so the timeout must accommodate that.
// Combine the caller's cancel signal with a 90s deadline so either fires.
signal: AbortSignal.any([AbortSignal.timeout(SEARCH_TIMEOUT_MS), ...(signal ? [signal] : [])]),
});
if (!response.ok) {
// Drain the body under the byte cap; only the status lands in the error.
await readBoundedText(response, SEARCH_MAX_RESPONSE_BYTES).catch(() => undefined);
throw new XSearchHttpError(response.status);
}
const data = (await readBoundedJson(response, SEARCH_MAX_RESPONSE_BYTES)) as {
output?: Array<{ type: string; content?: Array<{ type: string; text?: string }> }>;
citations?: Array<{ url: string; title?: string }>;
};
// Extract text from the Responses API output
const textParts: string[] = [];
for (const item of data.output ?? []) {
if (item.type === "message" && Array.isArray(item.content)) {
for (const part of item.content) {
if (part.type === "output_text" && part.text) {
textParts.push(part.text);
}
}
}
}
const citations: XSearchResult["citations"] = [];
for (const c of data.citations ?? []) {
if (c.url) citations.push({ url: c.url, title: c.title });
}
return {
answer: textParts.join("\n") || "(no results)",
citations: citations.length > 0 ? citations : undefined,
};
}
// ─── Tool registration ───────────────────────────────────────────────────────
export function registerXSearchTool(pi: ExtensionAPI) {
pi.registerTool({
name: "x_search",
label: "X Search",
description:
"Search X (formerly Twitter) for posts, users, and threads. "
+ "Returns relevant posts and their content. Use this when you need "
+ "real-time social media information, public sentiment, or to find "
+ "specific posts by keyword, topic, or user.",
promptSnippet: "Search X (Twitter) for posts and users",
parameters: Type.Object({
query: Type.String({
description: "Search query: keywords, hashtags, or natural language description of what to find",
}),
allowed_x_handles: Type.Optional(
Type.Array(Type.String(), {
description: "Only include posts from these X handles (max 10)",
}),
),
excluded_x_handles: Type.Optional(
Type.Array(Type.String(), {
description: "Exclude posts from these X handles (max 10)",
}),
),
from_date: Type.Optional(
Type.String({
description: 'Start date for search range (ISO 8601, e.g. "2025-01-01")',
}),
),
to_date: Type.Optional(
Type.String({
description: 'End date for search range (ISO 8601, e.g. "2025-12-31")',
}),
),
}),
async execute(toolCallId, params, signal, _onUpdate, ctx) {
const apiKey = await ctx.modelRegistry.getApiKeyForProvider("xai-oauth");
if (!apiKey) {
return {
content: [{ type: "text", text: "Error: no xAI credentials. Run /login to authenticate." }],
isError: true,
details: {},
};
}
// X search rides the cli-chat-proxy so the call uses the same
// subscription path as inference.
const baseUrl = CLI_PROXY_BASE_URL;
let result: XSearchResult;
try {
result = await callXSearch(
apiKey,
baseUrl,
params.query,
{
allowedXHandles: params.allowed_x_handles,
excludedXHandles: params.excluded_x_handles,
fromDate: params.from_date,
toDate: params.to_date,
},
signal,
);
} catch (err) {
const mapped = formatXSearchError(err);
return {
content: [{ type: "text", text: mapped.text }],
isError: true,
details: mapped.status !== undefined ? { status: mapped.status } : {},
};
}
let text = result.answer;
if (result.citations?.length) {
text += "\n\nSources:\n";
for (const c of result.citations) {
text += `- ${c.title ? c.title + " " : ""}${c.url}\n`;
}
}
return {
content: [{ type: "text", text }],
details: {},
};
},
});
}