@aexol/spectral 0.2.5 → 0.2.6
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/dist/cli.js +10 -47
- package/dist/mcp/agent-dir.js +18 -0
- package/dist/mcp/app-bridge.bundle.js +67 -0
- package/dist/mcp/commands.js +263 -0
- package/dist/mcp/config.js +532 -0
- package/dist/mcp/consent-manager.js +59 -0
- package/dist/mcp/direct-tools.js +354 -0
- package/dist/mcp/errors.js +165 -0
- package/dist/mcp/glimpse-ui.js +67 -0
- package/dist/mcp/host-html-template.js +412 -0
- package/dist/mcp/index.js +291 -0
- package/dist/mcp/init.js +280 -0
- package/dist/mcp/lifecycle.js +79 -0
- package/dist/mcp/logger.js +130 -0
- package/dist/mcp/mcp-auth-flow.js +283 -0
- package/dist/mcp/mcp-auth.js +226 -0
- package/dist/mcp/mcp-callback-server.js +225 -0
- package/dist/mcp/mcp-oauth-provider.js +243 -0
- package/dist/mcp/mcp-panel.js +646 -0
- package/dist/mcp/mcp-setup-panel.js +485 -0
- package/dist/mcp/metadata-cache.js +158 -0
- package/dist/mcp/npx-resolver.js +385 -0
- package/dist/mcp/oauth-handler.js +54 -0
- package/dist/mcp/onboarding-state.js +56 -0
- package/dist/mcp/proxy-modes.js +714 -0
- package/dist/mcp/resource-tools.js +14 -0
- package/dist/mcp/sampling-handler.js +206 -0
- package/dist/mcp/server-manager.js +301 -0
- package/dist/mcp/state.js +1 -0
- package/dist/mcp/tool-metadata.js +128 -0
- package/dist/mcp/tool-registrar.js +43 -0
- package/dist/mcp/types.js +93 -0
- package/dist/mcp/ui-resource-handler.js +113 -0
- package/dist/mcp/ui-server.js +522 -0
- package/dist/mcp/ui-session.js +306 -0
- package/dist/mcp/ui-stream-types.js +58 -0
- package/dist/mcp/utils.js +104 -0
- package/dist/mcp/vitest.config.js +13 -0
- package/package.json +6 -3
|
@@ -0,0 +1,306 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { extractUiPromptText, UI_STREAM_HOST_CONTEXT_KEY, UI_STREAM_REQUEST_META_KEY, UI_STREAM_STRUCTURED_CONTENT_KEY, } from "./types.js";
|
|
3
|
+
import { logger } from "./logger.js";
|
|
4
|
+
import { startUiServer } from "./ui-server.js";
|
|
5
|
+
import { isGlimpseAvailable, openGlimpseWindow } from "./glimpse-ui.js";
|
|
6
|
+
let activeGlimpseWindow = null;
|
|
7
|
+
const MAX_COMPLETED_SESSIONS = 10;
|
|
8
|
+
function withStreamEnvelope(result, streamId, sequence) {
|
|
9
|
+
if (!streamId) {
|
|
10
|
+
return result;
|
|
11
|
+
}
|
|
12
|
+
const structuredContent = result.structuredContent && typeof result.structuredContent === "object" && !Array.isArray(result.structuredContent)
|
|
13
|
+
? { ...result.structuredContent }
|
|
14
|
+
: {};
|
|
15
|
+
const rawEnvelope = structuredContent[UI_STREAM_STRUCTURED_CONTENT_KEY];
|
|
16
|
+
const envelope = rawEnvelope && typeof rawEnvelope === "object" && !Array.isArray(rawEnvelope)
|
|
17
|
+
? { ...rawEnvelope }
|
|
18
|
+
: {
|
|
19
|
+
frameType: "final",
|
|
20
|
+
phase: "settled",
|
|
21
|
+
status: result.isError ? "error" : "ok",
|
|
22
|
+
};
|
|
23
|
+
structuredContent[UI_STREAM_STRUCTURED_CONTENT_KEY] = {
|
|
24
|
+
...envelope,
|
|
25
|
+
streamId,
|
|
26
|
+
sequence,
|
|
27
|
+
};
|
|
28
|
+
return {
|
|
29
|
+
...result,
|
|
30
|
+
structuredContent,
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
async function openInBrowser(state, url) {
|
|
34
|
+
try {
|
|
35
|
+
await state.openBrowser(url);
|
|
36
|
+
}
|
|
37
|
+
catch (error) {
|
|
38
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
39
|
+
state.ui?.notify(`MCP UI browser open failed: ${message}`, "warning");
|
|
40
|
+
state.ui?.notify(`Open manually: ${url}`, "info");
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
export async function maybeStartUiSession(state, request) {
|
|
44
|
+
const log = logger.child({
|
|
45
|
+
component: "UiSession",
|
|
46
|
+
server: request.serverName,
|
|
47
|
+
tool: request.toolName,
|
|
48
|
+
});
|
|
49
|
+
try {
|
|
50
|
+
if (state.uiServer &&
|
|
51
|
+
state.uiServer.serverName === request.serverName &&
|
|
52
|
+
state.uiServer.toolName === request.toolName) {
|
|
53
|
+
const existingHandle = state.uiServer;
|
|
54
|
+
const streamMode = request.streamMode;
|
|
55
|
+
const streamId = streamMode ? randomUUID() : undefined;
|
|
56
|
+
const streamToken = streamMode ? randomUUID() : undefined;
|
|
57
|
+
let active = true;
|
|
58
|
+
let nextStreamSequence = 0;
|
|
59
|
+
const cleanupStreamListener = () => {
|
|
60
|
+
if (streamToken) {
|
|
61
|
+
state.manager.removeUiStreamListener(streamToken);
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
existingHandle.sendToolInput(request.toolArgs);
|
|
65
|
+
if (streamToken) {
|
|
66
|
+
state.manager.registerUiStreamListener(streamToken, (serverName, notification) => {
|
|
67
|
+
if (!active || state.uiServer !== existingHandle)
|
|
68
|
+
return;
|
|
69
|
+
if (serverName !== request.serverName)
|
|
70
|
+
return;
|
|
71
|
+
nextStreamSequence += 1;
|
|
72
|
+
existingHandle.sendResultPatch(withStreamEnvelope(notification.result, streamId, nextStreamSequence));
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
return {
|
|
76
|
+
serverName: request.serverName,
|
|
77
|
+
toolName: request.toolName,
|
|
78
|
+
reused: true,
|
|
79
|
+
streamId,
|
|
80
|
+
streamToken,
|
|
81
|
+
streamMode,
|
|
82
|
+
requestMeta: streamToken ? { [UI_STREAM_REQUEST_META_KEY]: streamToken } : undefined,
|
|
83
|
+
url: existingHandle.url,
|
|
84
|
+
isActive: () => active && state.uiServer === existingHandle,
|
|
85
|
+
sendToolResult: (result) => {
|
|
86
|
+
if (!active || state.uiServer !== existingHandle)
|
|
87
|
+
return;
|
|
88
|
+
nextStreamSequence += 1;
|
|
89
|
+
existingHandle.sendToolResult(withStreamEnvelope(result, streamId, nextStreamSequence));
|
|
90
|
+
},
|
|
91
|
+
sendResultPatch: (result) => {
|
|
92
|
+
if (!active || state.uiServer !== existingHandle)
|
|
93
|
+
return;
|
|
94
|
+
nextStreamSequence += 1;
|
|
95
|
+
existingHandle.sendResultPatch(withStreamEnvelope(result, streamId, nextStreamSequence));
|
|
96
|
+
},
|
|
97
|
+
sendToolCancelled: (reason) => {
|
|
98
|
+
if (!active || state.uiServer !== existingHandle)
|
|
99
|
+
return;
|
|
100
|
+
nextStreamSequence += 1;
|
|
101
|
+
existingHandle.sendToolResult(withStreamEnvelope({
|
|
102
|
+
isError: true,
|
|
103
|
+
content: [{ type: "text", text: reason }],
|
|
104
|
+
}, streamId, nextStreamSequence));
|
|
105
|
+
},
|
|
106
|
+
close: () => {
|
|
107
|
+
active = false;
|
|
108
|
+
cleanupStreamListener();
|
|
109
|
+
},
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
const resource = await state.uiResourceHandler.readUiResource(request.serverName, request.uiResourceUri);
|
|
113
|
+
if (state.uiServer) {
|
|
114
|
+
state.uiServer.close("replaced");
|
|
115
|
+
state.uiServer = null;
|
|
116
|
+
}
|
|
117
|
+
if (activeGlimpseWindow) {
|
|
118
|
+
activeGlimpseWindow.close();
|
|
119
|
+
activeGlimpseWindow = null;
|
|
120
|
+
}
|
|
121
|
+
const streamMode = request.streamMode;
|
|
122
|
+
const streamId = streamMode ? randomUUID() : undefined;
|
|
123
|
+
const streamToken = streamMode ? randomUUID() : undefined;
|
|
124
|
+
const hostContext = streamMode && streamId
|
|
125
|
+
? {
|
|
126
|
+
[UI_STREAM_HOST_CONTEXT_KEY]: {
|
|
127
|
+
mode: streamMode,
|
|
128
|
+
streamId,
|
|
129
|
+
intermediateResultPatches: streamMode === "stream-first",
|
|
130
|
+
partialInput: false,
|
|
131
|
+
},
|
|
132
|
+
}
|
|
133
|
+
: undefined;
|
|
134
|
+
let active = true;
|
|
135
|
+
let nextStreamSequence = 0;
|
|
136
|
+
let handle = null;
|
|
137
|
+
const cleanupStreamListener = () => {
|
|
138
|
+
if (streamToken) {
|
|
139
|
+
state.manager.removeUiStreamListener(streamToken);
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
handle = await startUiServer({
|
|
143
|
+
serverName: request.serverName,
|
|
144
|
+
toolName: request.toolName,
|
|
145
|
+
toolArgs: streamMode === "stream-first" ? {} : request.toolArgs,
|
|
146
|
+
resource,
|
|
147
|
+
manager: state.manager,
|
|
148
|
+
consentManager: state.consentManager,
|
|
149
|
+
hostContext,
|
|
150
|
+
onMessage: (params) => {
|
|
151
|
+
const prompt = extractUiPromptText(params);
|
|
152
|
+
if (prompt) {
|
|
153
|
+
if (state.sendMessage) {
|
|
154
|
+
state.sendMessage({
|
|
155
|
+
customType: "mcp-ui-prompt",
|
|
156
|
+
content: [{ type: "text", text: `User sent prompt from ${request.serverName} UI: "${prompt}"` }],
|
|
157
|
+
display: `💬 UI Prompt: ${prompt}`,
|
|
158
|
+
details: { server: request.serverName, tool: request.toolName, prompt },
|
|
159
|
+
}, { triggerTurn: true });
|
|
160
|
+
log.debug("Triggered agent turn for UI prompt", { prompt: prompt.slice(0, 50) });
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
else if (params.type === "intent" || params.intent) {
|
|
164
|
+
const intent = params.intent ?? "";
|
|
165
|
+
const intentParams = params.params;
|
|
166
|
+
if (intent && state.sendMessage) {
|
|
167
|
+
const paramsStr = intentParams ? ` ${JSON.stringify(intentParams)}` : "";
|
|
168
|
+
state.sendMessage({
|
|
169
|
+
customType: "mcp-ui-intent",
|
|
170
|
+
content: [{ type: "text", text: `User triggered intent from ${request.serverName} UI: ${intent}${paramsStr}` }],
|
|
171
|
+
display: `🎯 UI Intent: ${intent}`,
|
|
172
|
+
details: { server: request.serverName, tool: request.toolName, intent, params: intentParams },
|
|
173
|
+
}, { triggerTurn: true });
|
|
174
|
+
log.debug("Triggered agent turn for UI intent", { intent });
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
else if (params.type === "notify" || params.message) {
|
|
178
|
+
const text = params.message ?? "";
|
|
179
|
+
if (text && state.ui) {
|
|
180
|
+
state.ui.notify(`[${request.serverName}] ${text}`, "info");
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
},
|
|
184
|
+
onContextUpdate: (params) => {
|
|
185
|
+
log.debug("Model context update from UI", {
|
|
186
|
+
hasContent: !!params.content,
|
|
187
|
+
hasStructured: !!params.structuredContent,
|
|
188
|
+
});
|
|
189
|
+
},
|
|
190
|
+
onComplete: (reason) => {
|
|
191
|
+
active = false;
|
|
192
|
+
cleanupStreamListener();
|
|
193
|
+
if (state.uiServer === handle) {
|
|
194
|
+
const messages = handle.getSessionMessages();
|
|
195
|
+
const stream = handle.getStreamSummary();
|
|
196
|
+
const hasContent = messages.prompts.length > 0 ||
|
|
197
|
+
messages.intents.length > 0 ||
|
|
198
|
+
messages.notifications.length > 0 ||
|
|
199
|
+
!!stream;
|
|
200
|
+
if (hasContent) {
|
|
201
|
+
state.completedUiSessions.push({
|
|
202
|
+
serverName: handle.serverName,
|
|
203
|
+
toolName: handle.toolName,
|
|
204
|
+
completedAt: new Date(),
|
|
205
|
+
reason,
|
|
206
|
+
messages,
|
|
207
|
+
stream,
|
|
208
|
+
});
|
|
209
|
+
while (state.completedUiSessions.length > MAX_COMPLETED_SESSIONS) {
|
|
210
|
+
state.completedUiSessions.shift();
|
|
211
|
+
}
|
|
212
|
+
log.debug("Session completed", {
|
|
213
|
+
reason,
|
|
214
|
+
prompts: messages.prompts.length,
|
|
215
|
+
intents: messages.intents.length,
|
|
216
|
+
notifications: messages.notifications.length,
|
|
217
|
+
streamFrames: stream?.frames ?? 0,
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
state.uiServer = null;
|
|
221
|
+
if (activeGlimpseWindow) {
|
|
222
|
+
activeGlimpseWindow.close();
|
|
223
|
+
activeGlimpseWindow = null;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
},
|
|
227
|
+
});
|
|
228
|
+
if (streamToken) {
|
|
229
|
+
state.manager.registerUiStreamListener(streamToken, (serverName, notification) => {
|
|
230
|
+
if (!active || state.uiServer !== handle)
|
|
231
|
+
return;
|
|
232
|
+
if (serverName !== request.serverName)
|
|
233
|
+
return;
|
|
234
|
+
nextStreamSequence += 1;
|
|
235
|
+
handle.sendResultPatch(withStreamEnvelope(notification.result, streamId, nextStreamSequence));
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
state.uiServer = handle;
|
|
239
|
+
const glimpseDetected = isGlimpseAvailable();
|
|
240
|
+
const viewerPref = process.env.MCP_UI_VIEWER?.toLowerCase();
|
|
241
|
+
const useGlimpse = viewerPref === "glimpse" ||
|
|
242
|
+
(viewerPref !== "browser" && glimpseDetected);
|
|
243
|
+
if (useGlimpse) {
|
|
244
|
+
try {
|
|
245
|
+
const glimpseHtml = `<!DOCTYPE html><html><head><meta charset="utf-8"><style>body{margin:0;padding:0;width:100vw;height:100vh;overflow:hidden}iframe{width:100%;height:100%;border:none}</style></head><body><iframe src="${handle.url}"></iframe></body></html>`;
|
|
246
|
+
activeGlimpseWindow = await openGlimpseWindow(glimpseHtml, {
|
|
247
|
+
title: `MCP · ${request.serverName} · ${request.toolName}`,
|
|
248
|
+
width: 1000,
|
|
249
|
+
height: 800,
|
|
250
|
+
onClosed: () => {
|
|
251
|
+
if (active)
|
|
252
|
+
handle.close("glimpse-closed");
|
|
253
|
+
},
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
catch (error) {
|
|
257
|
+
log.debug("Glimpse unavailable, using browser", {
|
|
258
|
+
error: error instanceof Error ? error.message : String(error),
|
|
259
|
+
});
|
|
260
|
+
await openInBrowser(state, handle.url);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
else {
|
|
264
|
+
await openInBrowser(state, handle.url);
|
|
265
|
+
}
|
|
266
|
+
return {
|
|
267
|
+
serverName: request.serverName,
|
|
268
|
+
toolName: request.toolName,
|
|
269
|
+
reused: false,
|
|
270
|
+
streamId,
|
|
271
|
+
streamToken,
|
|
272
|
+
streamMode,
|
|
273
|
+
requestMeta: streamToken ? { [UI_STREAM_REQUEST_META_KEY]: streamToken } : undefined,
|
|
274
|
+
url: handle.url,
|
|
275
|
+
isActive: () => active && state.uiServer === handle,
|
|
276
|
+
sendToolResult: (result) => {
|
|
277
|
+
if (!active || state.uiServer !== handle)
|
|
278
|
+
return;
|
|
279
|
+
nextStreamSequence += 1;
|
|
280
|
+
handle.sendToolResult(withStreamEnvelope(result, streamId, nextStreamSequence));
|
|
281
|
+
},
|
|
282
|
+
sendResultPatch: (result) => {
|
|
283
|
+
if (!active || state.uiServer !== handle)
|
|
284
|
+
return;
|
|
285
|
+
nextStreamSequence += 1;
|
|
286
|
+
handle.sendResultPatch(withStreamEnvelope(result, streamId, nextStreamSequence));
|
|
287
|
+
},
|
|
288
|
+
sendToolCancelled: (reason) => {
|
|
289
|
+
if (!active || state.uiServer !== handle)
|
|
290
|
+
return;
|
|
291
|
+
handle.sendToolCancelled(reason);
|
|
292
|
+
},
|
|
293
|
+
close: (reason) => {
|
|
294
|
+
active = false;
|
|
295
|
+
cleanupStreamListener();
|
|
296
|
+
handle.close(reason);
|
|
297
|
+
},
|
|
298
|
+
};
|
|
299
|
+
}
|
|
300
|
+
catch (error) {
|
|
301
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
302
|
+
log.error("Failed to start UI session", error instanceof Error ? error : undefined);
|
|
303
|
+
state.ui?.notify(`MCP UI unavailable for ${request.toolName} (${request.serverName}): ${message}`, "warning");
|
|
304
|
+
return null;
|
|
305
|
+
}
|
|
306
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
export const UI_STREAM_HOST_CONTEXT_KEY = "pi-mcp-adapter/stream";
|
|
3
|
+
export const UI_STREAM_REQUEST_META_KEY = "pi-mcp-adapter/stream-token";
|
|
4
|
+
export const UI_STREAM_RESULT_PATCH_METHOD = "notifications/pi-mcp-adapter/ui-result-patch";
|
|
5
|
+
export const SERVER_STREAM_RESULT_PATCH_METHOD = "notifications/pi-mcp-adapter/result-patch";
|
|
6
|
+
export const UI_STREAM_STRUCTURED_CONTENT_KEY = "pi-mcp-adapter/stream";
|
|
7
|
+
export const uiStreamModeSchema = z.enum(["eager", "stream-first"]);
|
|
8
|
+
export const visualizationStreamPhaseSchema = z.enum(["shell", "narrative", "structure", "detail", "settled"]);
|
|
9
|
+
export const visualizationStreamFrameTypeSchema = z.enum(["patch", "checkpoint", "final"]);
|
|
10
|
+
export const visualizationStreamStatusSchema = z.enum(["ok", "error"]);
|
|
11
|
+
const looseRecordSchema = z.record(z.string(), z.unknown());
|
|
12
|
+
const looseArraySchema = z.array(z.unknown());
|
|
13
|
+
export const uiStreamHostContextSchema = z.object({
|
|
14
|
+
mode: uiStreamModeSchema,
|
|
15
|
+
streamId: z.string().min(1),
|
|
16
|
+
intermediateResultPatches: z.boolean(),
|
|
17
|
+
partialInput: z.boolean(),
|
|
18
|
+
});
|
|
19
|
+
export const visualizationStreamEnvelopeSchema = z.object({
|
|
20
|
+
streamId: z.string().min(1),
|
|
21
|
+
sequence: z.number().int().nonnegative(),
|
|
22
|
+
frameType: visualizationStreamFrameTypeSchema,
|
|
23
|
+
phase: visualizationStreamPhaseSchema,
|
|
24
|
+
status: visualizationStreamStatusSchema,
|
|
25
|
+
message: z.string().optional(),
|
|
26
|
+
spec: looseRecordSchema.optional(),
|
|
27
|
+
checkpoint: looseRecordSchema.optional(),
|
|
28
|
+
});
|
|
29
|
+
export const uiStreamCallToolResultSchema = z.object({
|
|
30
|
+
content: looseArraySchema.optional(),
|
|
31
|
+
structuredContent: looseRecordSchema.optional(),
|
|
32
|
+
isError: z.boolean().optional(),
|
|
33
|
+
_meta: looseRecordSchema.optional(),
|
|
34
|
+
}).passthrough();
|
|
35
|
+
export const uiStreamResultPatchNotificationSchema = z.object({
|
|
36
|
+
method: z.literal(UI_STREAM_RESULT_PATCH_METHOD),
|
|
37
|
+
params: uiStreamCallToolResultSchema,
|
|
38
|
+
});
|
|
39
|
+
export const serverStreamResultPatchNotificationSchema = z.object({
|
|
40
|
+
method: z.literal(SERVER_STREAM_RESULT_PATCH_METHOD),
|
|
41
|
+
params: z.object({
|
|
42
|
+
streamToken: z.string().min(1),
|
|
43
|
+
result: uiStreamCallToolResultSchema,
|
|
44
|
+
}),
|
|
45
|
+
});
|
|
46
|
+
export function getUiStreamHostContext(hostContext) {
|
|
47
|
+
const candidate = hostContext?.[UI_STREAM_HOST_CONTEXT_KEY];
|
|
48
|
+
const parsed = uiStreamHostContextSchema.safeParse(candidate);
|
|
49
|
+
return parsed.success ? parsed.data : undefined;
|
|
50
|
+
}
|
|
51
|
+
export function getVisualizationStreamEnvelope(structuredContent) {
|
|
52
|
+
if (!structuredContent || typeof structuredContent !== "object" || Array.isArray(structuredContent)) {
|
|
53
|
+
return undefined;
|
|
54
|
+
}
|
|
55
|
+
const candidate = structuredContent[UI_STREAM_STRUCTURED_CONTENT_KEY];
|
|
56
|
+
const parsed = visualizationStreamEnvelopeSchema.safeParse(candidate);
|
|
57
|
+
return parsed.success ? parsed.data : undefined;
|
|
58
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { homedir, platform } from "node:os";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
async function execOpen(pi, target, browser) {
|
|
4
|
+
const os = platform();
|
|
5
|
+
if (os === "darwin") {
|
|
6
|
+
return browser ? pi.exec("open", ["-a", browser, target]) : pi.exec("open", [target]);
|
|
7
|
+
}
|
|
8
|
+
if (os === "win32") {
|
|
9
|
+
return browser
|
|
10
|
+
? pi.exec("cmd", ["/c", "start", "", browser, target])
|
|
11
|
+
: pi.exec("cmd", ["/c", "start", "", target]);
|
|
12
|
+
}
|
|
13
|
+
return browser ? pi.exec(browser, [target]) : pi.exec("xdg-open", [target]);
|
|
14
|
+
}
|
|
15
|
+
export async function openUrl(pi, url, browser) {
|
|
16
|
+
const result = await execOpen(pi, url, browser);
|
|
17
|
+
if (result.code !== 0) {
|
|
18
|
+
throw new Error(result.stderr || `Failed to open browser (exit code ${result.code})`);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
export async function openPath(pi, targetPath) {
|
|
22
|
+
const result = await execOpen(pi, targetPath);
|
|
23
|
+
if (result.code !== 0) {
|
|
24
|
+
throw new Error(result.stderr || `Failed to open path (exit code ${result.code})`);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
export async function parallelLimit(items, limit, fn) {
|
|
28
|
+
const results = [];
|
|
29
|
+
let index = 0;
|
|
30
|
+
async function worker() {
|
|
31
|
+
while (index < items.length) {
|
|
32
|
+
const i = index++;
|
|
33
|
+
results[i] = await fn(items[i]);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
const workers = Array(Math.min(limit, items.length)).fill(null).map(() => worker());
|
|
37
|
+
await Promise.all(workers);
|
|
38
|
+
return results;
|
|
39
|
+
}
|
|
40
|
+
export function getConfigPathFromArgv() {
|
|
41
|
+
const idx = process.argv.indexOf("--mcp-config");
|
|
42
|
+
if (idx >= 0 && idx + 1 < process.argv.length) {
|
|
43
|
+
return process.argv[idx + 1];
|
|
44
|
+
}
|
|
45
|
+
return undefined;
|
|
46
|
+
}
|
|
47
|
+
export function interpolateEnvVars(value) {
|
|
48
|
+
return value
|
|
49
|
+
.replace(/\$\{(\w+)\}/g, (_, name) => process.env[name] ?? "")
|
|
50
|
+
.replace(/\$env:(\w+)/g, (_, name) => process.env[name] ?? "");
|
|
51
|
+
}
|
|
52
|
+
export function interpolateEnvRecord(values) {
|
|
53
|
+
if (!values)
|
|
54
|
+
return undefined;
|
|
55
|
+
const resolved = {};
|
|
56
|
+
for (const [key, value] of Object.entries(values)) {
|
|
57
|
+
resolved[key] = interpolateEnvVars(value);
|
|
58
|
+
}
|
|
59
|
+
return resolved;
|
|
60
|
+
}
|
|
61
|
+
export function resolveConfigPath(value) {
|
|
62
|
+
if (value === undefined)
|
|
63
|
+
return undefined;
|
|
64
|
+
const resolved = interpolateEnvVars(value);
|
|
65
|
+
if (resolved === "~")
|
|
66
|
+
return homedir();
|
|
67
|
+
if (resolved.startsWith("~/") || resolved.startsWith("~\\")) {
|
|
68
|
+
return join(homedir(), resolved.slice(2));
|
|
69
|
+
}
|
|
70
|
+
return resolved;
|
|
71
|
+
}
|
|
72
|
+
export function resolveBearerToken(definition) {
|
|
73
|
+
if (definition.bearerToken !== undefined) {
|
|
74
|
+
return interpolateEnvVars(definition.bearerToken);
|
|
75
|
+
}
|
|
76
|
+
return definition.bearerTokenEnv ? process.env[definition.bearerTokenEnv] : undefined;
|
|
77
|
+
}
|
|
78
|
+
export function truncateAtWord(text, target) {
|
|
79
|
+
if (!text || text.length <= target)
|
|
80
|
+
return text;
|
|
81
|
+
const truncated = text.slice(0, target);
|
|
82
|
+
const lastSpace = truncated.lastIndexOf(" ");
|
|
83
|
+
if (lastSpace > target * 0.6) {
|
|
84
|
+
return truncated.slice(0, lastSpace) + "...";
|
|
85
|
+
}
|
|
86
|
+
return truncated + "...";
|
|
87
|
+
}
|
|
88
|
+
export function formatAuthRequiredMessage(config, serverName, defaultMessage) {
|
|
89
|
+
const template = config.settings?.authRequiredMessage;
|
|
90
|
+
return template ? template.replaceAll("${server}", serverName) : defaultMessage;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Extract the adapter-owned UI stream mode from tool metadata.
|
|
94
|
+
*/
|
|
95
|
+
export function extractToolUiStreamMode(toolMeta) {
|
|
96
|
+
const uiMeta = toolMeta?.ui;
|
|
97
|
+
if (!uiMeta || typeof uiMeta !== "object")
|
|
98
|
+
return undefined;
|
|
99
|
+
const streamMode = uiMeta["pi-mcp-adapter.streamMode"];
|
|
100
|
+
if (streamMode === "eager" || streamMode === "stream-first") {
|
|
101
|
+
return streamMode;
|
|
102
|
+
}
|
|
103
|
+
return undefined;
|
|
104
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { defineConfig } from "vitest/config";
|
|
2
|
+
export default defineConfig({
|
|
3
|
+
test: {
|
|
4
|
+
globals: true,
|
|
5
|
+
environment: "node",
|
|
6
|
+
include: ["__tests__/**/*.test.ts"],
|
|
7
|
+
coverage: {
|
|
8
|
+
provider: "v8",
|
|
9
|
+
include: ["*.ts"],
|
|
10
|
+
exclude: ["__tests__/**", "vitest.config.ts", "cli.js"],
|
|
11
|
+
},
|
|
12
|
+
},
|
|
13
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aexol/spectral",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.6",
|
|
4
4
|
"description": "Always-on coding agent for Aexol — branded pi wrapper with relay-based browser access.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"private": false,
|
|
@@ -54,12 +54,15 @@
|
|
|
54
54
|
"@mariozechner/jiti": "^2.6.5",
|
|
55
55
|
"@mariozechner/pi-coding-agent": "^0.70.2",
|
|
56
56
|
"better-sqlite3": "^12.9.0",
|
|
57
|
-
"pi-
|
|
57
|
+
"@mariozechner/pi-ai": "^0.70.2",
|
|
58
|
+
"@mariozechner/pi-tui": "^0.70.2",
|
|
58
59
|
"@modelcontextprotocol/sdk": "^1.25.1",
|
|
59
60
|
"@modelcontextprotocol/ext-apps": "^1.2.2",
|
|
60
61
|
"open": "^10.2.0",
|
|
61
62
|
"picocolors": "^1.1.1",
|
|
62
|
-
"
|
|
63
|
+
"typebox": "^1.1.24",
|
|
64
|
+
"ws": "^8.20.0",
|
|
65
|
+
"zod": "^3.25.0 || ^4.0.0"
|
|
63
66
|
},
|
|
64
67
|
"devDependencies": {
|
|
65
68
|
"@aexol/relay-protocol": "file:../packages/relay-protocol",
|