@fitchmultz/pi-mcp-adapter 5.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 +750 -0
- package/LICENSE +21 -0
- package/OAUTH.md +462 -0
- package/README.md +820 -0
- package/abort.ts +34 -0
- package/agent-dir.ts +24 -0
- package/app-bridge.bundle.js +67 -0
- package/banner.png +0 -0
- package/checkpoint.ts +55 -0
- package/cli.js +222 -0
- package/commands.ts +619 -0
- package/config.ts +1204 -0
- package/consent-manager.ts +25 -0
- package/direct-tools.ts +408 -0
- package/dist/abort.d.ts +2 -0
- package/dist/abort.js +35 -0
- package/dist/agent-dir.d.ts +3 -0
- package/dist/agent-dir.js +21 -0
- package/dist/app-bridge.bundle.js +67 -0
- package/dist/checkpoint.d.ts +18 -0
- package/dist/checkpoint.js +53 -0
- package/dist/commands.d.ts +20 -0
- package/dist/commands.js +521 -0
- package/dist/config.d.ts +109 -0
- package/dist/config.js +1013 -0
- package/dist/consent-manager.d.ts +8 -0
- package/dist/consent-manager.js +21 -0
- package/dist/direct-tools.d.ts +10 -0
- package/dist/direct-tools.js +350 -0
- package/dist/docs/client-metadata.json +14 -0
- package/dist/elicitation-handler.d.ts +17 -0
- package/dist/elicitation-handler.js +308 -0
- package/dist/error-signal.d.ts +15 -0
- package/dist/error-signal.js +21 -0
- package/dist/glimpse-ui.d.ts +9 -0
- package/dist/glimpse-ui.js +75 -0
- package/dist/host-html-template.d.ts +15 -0
- package/dist/host-html-template.js +429 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +840 -0
- package/dist/init.d.ts +25 -0
- package/dist/init.js +562 -0
- package/dist/legacy-migration.d.ts +18 -0
- package/dist/legacy-migration.js +45 -0
- package/dist/lifecycle.d.ts +40 -0
- package/dist/lifecycle.js +167 -0
- package/dist/logger.d.ts +38 -0
- package/dist/logger.js +102 -0
- package/dist/mcp-auth-flow.d.ts +113 -0
- package/dist/mcp-auth-flow.js +834 -0
- package/dist/mcp-auth.d.ts +127 -0
- package/dist/mcp-auth.js +658 -0
- package/dist/mcp-callback-server.d.ts +56 -0
- package/dist/mcp-callback-server.js +371 -0
- package/dist/mcp-code.d.ts +51 -0
- package/dist/mcp-code.js +366 -0
- package/dist/mcp-keyring-helper.cjs +89 -0
- package/dist/mcp-oauth-provider.d.ts +154 -0
- package/dist/mcp-oauth-provider.js +672 -0
- package/dist/mcp-output-guard.d.ts +59 -0
- package/dist/mcp-output-guard.js +332 -0
- package/dist/mcp-panel.d.ts +66 -0
- package/dist/mcp-panel.js +889 -0
- package/dist/mcp-script-worker.mjs +128 -0
- package/dist/mcp-setup-panel.d.ts +84 -0
- package/dist/mcp-setup-panel.js +561 -0
- package/dist/mcp-status.d.ts +11 -0
- package/dist/mcp-status.js +96 -0
- package/dist/mcp-trace.d.ts +95 -0
- package/dist/mcp-trace.js +233 -0
- package/dist/metadata-cache.d.ts +18 -0
- package/dist/metadata-cache.js +277 -0
- package/dist/npx-resolver.d.ts +6 -0
- package/dist/npx-resolver.js +448 -0
- package/dist/onboarding-state.d.ts +11 -0
- package/dist/onboarding-state.js +47 -0
- package/dist/panel-keys.d.ts +16 -0
- package/dist/panel-keys.js +15 -0
- package/dist/prompts.d.ts +56 -0
- package/dist/prompts.js +284 -0
- package/dist/proxy-modes.d.ts +38 -0
- package/dist/proxy-modes.js +1235 -0
- package/dist/resource-tools.d.ts +1 -0
- package/dist/resource-tools.js +14 -0
- package/dist/runtime-owner.d.ts +18 -0
- package/dist/runtime-owner.js +117 -0
- package/dist/sampling-handler.d.ts +15 -0
- package/dist/sampling-handler.js +211 -0
- package/dist/search-ranking.d.ts +20 -0
- package/dist/search-ranking.js +180 -0
- package/dist/server-manager.d.ts +114 -0
- package/dist/server-manager.js +1175 -0
- package/dist/session-recovery.d.ts +31 -0
- package/dist/session-recovery.js +310 -0
- package/dist/state.d.ts +63 -0
- package/dist/state.js +1 -0
- package/dist/tool-approval.d.ts +10 -0
- package/dist/tool-approval.js +42 -0
- package/dist/tool-metadata.d.ts +9 -0
- package/dist/tool-metadata.js +193 -0
- package/dist/tool-registrar.d.ts +10 -0
- package/dist/tool-registrar.js +64 -0
- package/dist/tool-result-renderer.d.ts +45 -0
- package/dist/tool-result-renderer.js +224 -0
- package/dist/ts-shape.d.ts +2 -0
- package/dist/ts-shape.js +147 -0
- package/dist/types.d.ts +516 -0
- package/dist/types.js +168 -0
- package/dist/ui-resource-handler.d.ts +17 -0
- package/dist/ui-resource-handler.js +209 -0
- package/dist/ui-server.d.ts +51 -0
- package/dist/ui-server.js +669 -0
- package/dist/ui-session.d.ts +39 -0
- package/dist/ui-session.js +463 -0
- package/dist/ui-stream-types.d.ts +104 -0
- package/dist/ui-stream-types.js +58 -0
- package/dist/ui-tool-visibility.d.ts +4 -0
- package/dist/ui-tool-visibility.js +26 -0
- package/dist/unix-socket-transport.d.ts +15 -0
- package/dist/unix-socket-transport.js +84 -0
- package/dist/utils.d.ts +30 -0
- package/dist/utils.js +276 -0
- package/docs/client-metadata.json +14 -0
- package/elicitation-handler.ts +349 -0
- package/error-signal.ts +21 -0
- package/glimpse-ui.ts +80 -0
- package/host-html-template.ts +456 -0
- package/index.ts +900 -0
- package/init.ts +627 -0
- package/legacy-migration.ts +47 -0
- package/lifecycle.ts +165 -0
- package/logger.ts +127 -0
- package/mcp-auth-flow.ts +975 -0
- package/mcp-auth.ts +793 -0
- package/mcp-callback-server.ts +450 -0
- package/mcp-code.ts +386 -0
- package/mcp-keyring-helper.cjs +89 -0
- package/mcp-oauth-provider.ts +774 -0
- package/mcp-output-guard.ts +412 -0
- package/mcp-panel.ts +978 -0
- package/mcp-script-worker.mjs +128 -0
- package/mcp-setup-panel.ts +666 -0
- package/mcp-status.ts +109 -0
- package/mcp-trace.ts +295 -0
- package/metadata-cache.ts +333 -0
- package/npx-resolver.ts +488 -0
- package/onboarding-state.ts +58 -0
- package/package.json +182 -0
- package/panel-keys.ts +37 -0
- package/prompts.ts +337 -0
- package/proxy-modes.ts +1361 -0
- package/resource-tools.ts +17 -0
- package/runtime-owner.ts +123 -0
- package/sampling-handler.ts +273 -0
- package/scripts/build.mjs +131 -0
- package/scripts/prepare.mjs +102 -0
- package/search-ranking.ts +196 -0
- package/server-manager.ts +1320 -0
- package/session-recovery.ts +316 -0
- package/skills/mcp-scripting/SKILL.md +38 -0
- package/state.ts +64 -0
- package/tool-approval.ts +78 -0
- package/tool-metadata.ts +235 -0
- package/tool-registrar.ts +69 -0
- package/tool-result-renderer.ts +286 -0
- package/ts-shape.ts +145 -0
- package/tsconfig.build.json +13 -0
- package/tsconfig.json +16 -0
- package/types.ts +810 -0
- package/ui-resource-handler.ts +249 -0
- package/ui-server.ts +808 -0
- package/ui-session.ts +568 -0
- package/ui-stream-types.ts +89 -0
- package/ui-tool-visibility.ts +25 -0
- package/unix-socket-transport.ts +85 -0
- package/utils.ts +309 -0
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
// tool-registrar.ts - MCP content transformation
|
|
2
|
+
// NOTE: Tools are NOT registered with Pi - only the unified `mcp` proxy tool is registered.
|
|
3
|
+
// This keeps the LLM context small (1 tool instead of 100s).
|
|
4
|
+
/**
|
|
5
|
+
* Transform MCP content types to Pi content blocks.
|
|
6
|
+
*/
|
|
7
|
+
export function transformMcpContent(content) {
|
|
8
|
+
return content.map(c => {
|
|
9
|
+
if (c.type === "text") {
|
|
10
|
+
return { type: "text", text: c.text ?? "" };
|
|
11
|
+
}
|
|
12
|
+
if (c.type === "image") {
|
|
13
|
+
return {
|
|
14
|
+
type: "image",
|
|
15
|
+
data: c.data ?? "",
|
|
16
|
+
mimeType: c.mimeType ?? "image/png",
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
if (c.type === "resource") {
|
|
20
|
+
const resourceUri = c.resource?.uri ?? "(no URI)";
|
|
21
|
+
const resourceContent = c.resource?.text ?? (c.resource ? JSON.stringify(c.resource) : "(no content)");
|
|
22
|
+
return {
|
|
23
|
+
type: "text",
|
|
24
|
+
text: `[Resource: ${resourceUri}]\n${resourceContent}`,
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
if (c.type === "resource_link") {
|
|
28
|
+
const linkName = c.name ?? c.uri ?? "unknown";
|
|
29
|
+
const linkUri = c.uri ?? "(no URI)";
|
|
30
|
+
return {
|
|
31
|
+
type: "text",
|
|
32
|
+
text: `[Resource Link: ${linkName}]\nURI: ${linkUri}`,
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
if (c.type === "audio") {
|
|
36
|
+
return {
|
|
37
|
+
type: "text",
|
|
38
|
+
text: `[Audio content: ${c.mimeType ?? "audio/*"}]`,
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
return { type: "text", text: JSON.stringify(c) };
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Resolve a tool result's content blocks, falling back to structuredContent
|
|
46
|
+
* when content is empty.
|
|
47
|
+
*/
|
|
48
|
+
export function resolveMcpResultContent(result) {
|
|
49
|
+
const blocks = transformMcpContent((Array.isArray(result.content) ? result.content : []));
|
|
50
|
+
if (blocks.length > 0)
|
|
51
|
+
return blocks;
|
|
52
|
+
if (result.structuredContent !== undefined) {
|
|
53
|
+
return [{ type: "text", text: stringifyStructuredContent(result.structuredContent) }];
|
|
54
|
+
}
|
|
55
|
+
return [];
|
|
56
|
+
}
|
|
57
|
+
function stringifyStructuredContent(value) {
|
|
58
|
+
try {
|
|
59
|
+
return JSON.stringify(value, null, 2) ?? String(value);
|
|
60
|
+
}
|
|
61
|
+
catch {
|
|
62
|
+
return String(value);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { type AgentToolResult, type Theme, type ToolRenderResultOptions } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { type Component, Text } from "@earendil-works/pi-tui";
|
|
3
|
+
type McpToolResultDetails = Record<string, unknown> & {
|
|
4
|
+
error?: unknown;
|
|
5
|
+
};
|
|
6
|
+
type RenderTheme = Pick<Theme, "fg" | "bold">;
|
|
7
|
+
export interface McpProxyToolCallInput {
|
|
8
|
+
tool?: string;
|
|
9
|
+
args?: string | object;
|
|
10
|
+
connect?: string;
|
|
11
|
+
describe?: string;
|
|
12
|
+
search?: string;
|
|
13
|
+
includeSchemas?: boolean;
|
|
14
|
+
server?: string;
|
|
15
|
+
action?: string;
|
|
16
|
+
}
|
|
17
|
+
interface McpToolRenderContext {
|
|
18
|
+
isError: boolean;
|
|
19
|
+
}
|
|
20
|
+
export interface McpToolResultDisplay {
|
|
21
|
+
lines: string[];
|
|
22
|
+
truncated: boolean;
|
|
23
|
+
}
|
|
24
|
+
declare class CollapsibleText implements Component {
|
|
25
|
+
private readonly text;
|
|
26
|
+
private readonly expanded;
|
|
27
|
+
private readonly maxCollapsedLines;
|
|
28
|
+
private readonly ellipsis;
|
|
29
|
+
private readonly expandHint;
|
|
30
|
+
private readonly preTruncated;
|
|
31
|
+
private readonly fullText;
|
|
32
|
+
private readonly footerText;
|
|
33
|
+
private collapsedText;
|
|
34
|
+
constructor(text: string, expanded: boolean, maxCollapsedLines: number, ellipsis: string, expandHint: string, preTruncated?: boolean);
|
|
35
|
+
render(width: number): string[];
|
|
36
|
+
invalidate(): void;
|
|
37
|
+
}
|
|
38
|
+
export declare function formatMcpProxyToolCallLines(args: McpProxyToolCallInput, maxInputChars?: number): string[];
|
|
39
|
+
export declare function formatMcpDirectToolCallLines(displayName: string, args: Record<string, unknown>, maxInputChars?: number): string[];
|
|
40
|
+
export declare function renderMcpProxyToolCall(args: McpProxyToolCallInput, theme?: RenderTheme): Text;
|
|
41
|
+
export declare function createMcpDirectToolCallRenderer(displayName: string): (args: Record<string, unknown>, theme?: RenderTheme) => Text;
|
|
42
|
+
export declare function formatMcpToolResultIdentity(details: McpToolResultDetails | undefined): string | null;
|
|
43
|
+
export declare function formatMcpToolResultLines(result: Pick<AgentToolResult<McpToolResultDetails>, "content">, expanded: boolean, maxCollapsedLines?: number, maxCollapsedChars?: number): McpToolResultDisplay;
|
|
44
|
+
export declare function renderMcpToolResult(result: AgentToolResult<unknown>, options: ToolRenderResultOptions, theme?: RenderTheme, context?: McpToolRenderContext): CollapsibleText | Text;
|
|
45
|
+
export {};
|
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
import { keyHint } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { Text } from "@earendil-works/pi-tui";
|
|
3
|
+
const plainTheme = {
|
|
4
|
+
fg: (_name, text) => text,
|
|
5
|
+
bold: (text) => text,
|
|
6
|
+
};
|
|
7
|
+
const DEFAULT_MAX_CALL_INPUT_CHARS = 1500;
|
|
8
|
+
const DEFAULT_MAX_COLLAPSED_LINES = 3;
|
|
9
|
+
const DEFAULT_MAX_COLLAPSED_CHARS = 8000;
|
|
10
|
+
const COLLAPSED_RENDER_CHAR_SLACK = 8;
|
|
11
|
+
class CollapsibleText {
|
|
12
|
+
text;
|
|
13
|
+
expanded;
|
|
14
|
+
maxCollapsedLines;
|
|
15
|
+
ellipsis;
|
|
16
|
+
expandHint;
|
|
17
|
+
preTruncated;
|
|
18
|
+
fullText;
|
|
19
|
+
footerText;
|
|
20
|
+
collapsedText = null;
|
|
21
|
+
constructor(text, expanded, maxCollapsedLines, ellipsis, expandHint, preTruncated = false) {
|
|
22
|
+
this.text = text;
|
|
23
|
+
this.expanded = expanded;
|
|
24
|
+
this.maxCollapsedLines = maxCollapsedLines;
|
|
25
|
+
this.ellipsis = ellipsis;
|
|
26
|
+
this.expandHint = expandHint;
|
|
27
|
+
this.preTruncated = preTruncated;
|
|
28
|
+
this.fullText = new Text(text, 0, 0);
|
|
29
|
+
this.footerText = new Text(`${ellipsis}\n${expandHint}`, 0, 0);
|
|
30
|
+
}
|
|
31
|
+
render(width) {
|
|
32
|
+
if (this.expanded) {
|
|
33
|
+
return this.fullText.render(width);
|
|
34
|
+
}
|
|
35
|
+
const safeWidth = Math.max(1, Math.floor(width));
|
|
36
|
+
const charBudget = safeWidth * (this.maxCollapsedLines + 1) * COLLAPSED_RENDER_CHAR_SLACK;
|
|
37
|
+
if (!this.collapsedText || this.collapsedText.charBudget !== charBudget) {
|
|
38
|
+
const prefix = this.text.length > charBudget
|
|
39
|
+
? this.text.slice(0, charBudget)
|
|
40
|
+
: this.text;
|
|
41
|
+
this.collapsedText = {
|
|
42
|
+
charBudget,
|
|
43
|
+
fullyIncluded: prefix === this.text,
|
|
44
|
+
text: new Text(prefix, 0, 0),
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
const lines = this.collapsedText.text.render(width);
|
|
48
|
+
if (!this.preTruncated && this.collapsedText.fullyIncluded && lines.length <= this.maxCollapsedLines)
|
|
49
|
+
return lines;
|
|
50
|
+
return [
|
|
51
|
+
...lines.slice(0, this.maxCollapsedLines),
|
|
52
|
+
...this.footerText.render(width),
|
|
53
|
+
];
|
|
54
|
+
}
|
|
55
|
+
invalidate() { }
|
|
56
|
+
}
|
|
57
|
+
function truncateText(value, maxChars) {
|
|
58
|
+
if (value.length <= maxChars)
|
|
59
|
+
return value;
|
|
60
|
+
return `${value.slice(0, Math.max(0, maxChars - 1))}…`;
|
|
61
|
+
}
|
|
62
|
+
function formatJsonish(value, maxChars) {
|
|
63
|
+
if (typeof value === "string") {
|
|
64
|
+
try {
|
|
65
|
+
return truncateText(JSON.stringify(JSON.parse(value), null, 2), maxChars);
|
|
66
|
+
}
|
|
67
|
+
catch {
|
|
68
|
+
return truncateText(value, maxChars);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
try {
|
|
72
|
+
return truncateText(JSON.stringify(value, null, 2), maxChars);
|
|
73
|
+
}
|
|
74
|
+
catch {
|
|
75
|
+
return truncateText(String(value), maxChars);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
function hasUsefulObjectContent(value) {
|
|
79
|
+
return typeof value === "object" && value !== null && !Array.isArray(value) && Object.keys(value).length > 0;
|
|
80
|
+
}
|
|
81
|
+
export function formatMcpProxyToolCallLines(args, maxInputChars = DEFAULT_MAX_CALL_INPUT_CHARS) {
|
|
82
|
+
if (args.action === "ui-messages")
|
|
83
|
+
return [`mcp ${args.action}`];
|
|
84
|
+
if (args.tool) {
|
|
85
|
+
const target = args.server ? `${args.tool} @ ${args.server}` : args.tool;
|
|
86
|
+
const lines = [`mcp call ${target}`];
|
|
87
|
+
if (args.args)
|
|
88
|
+
lines.push(formatJsonish(args.args, maxInputChars));
|
|
89
|
+
return lines;
|
|
90
|
+
}
|
|
91
|
+
if (args.connect)
|
|
92
|
+
return [`mcp connect ${args.connect}`];
|
|
93
|
+
if (args.describe)
|
|
94
|
+
return [`mcp describe ${args.describe}`];
|
|
95
|
+
if (args.search) {
|
|
96
|
+
let line = `mcp search ${args.search}`;
|
|
97
|
+
if (args.server)
|
|
98
|
+
line += ` @ ${args.server}`;
|
|
99
|
+
if (args.includeSchemas === false)
|
|
100
|
+
line += " (schemas hidden)";
|
|
101
|
+
return [line];
|
|
102
|
+
}
|
|
103
|
+
if (args.server)
|
|
104
|
+
return [`mcp list ${args.server}`];
|
|
105
|
+
if (args.action)
|
|
106
|
+
return [`mcp ${args.action}`];
|
|
107
|
+
return ["mcp status"];
|
|
108
|
+
}
|
|
109
|
+
export function formatMcpDirectToolCallLines(displayName, args, maxInputChars = DEFAULT_MAX_CALL_INPUT_CHARS) {
|
|
110
|
+
if (!hasUsefulObjectContent(args))
|
|
111
|
+
return [displayName];
|
|
112
|
+
return [displayName, formatJsonish(args, maxInputChars)];
|
|
113
|
+
}
|
|
114
|
+
function renderToolCallLines(lines, theme) {
|
|
115
|
+
const activeTheme = theme ?? plainTheme;
|
|
116
|
+
const [title = "mcp", ...rest] = lines;
|
|
117
|
+
const styledTitle = activeTheme.fg("toolTitle", activeTheme.bold(title));
|
|
118
|
+
const styledRest = rest.map(line => activeTheme.fg("muted", line));
|
|
119
|
+
return new Text([styledTitle, ...styledRest].join("\n"), 0, 0);
|
|
120
|
+
}
|
|
121
|
+
export function renderMcpProxyToolCall(args, theme) {
|
|
122
|
+
return renderToolCallLines(formatMcpProxyToolCallLines(args), theme);
|
|
123
|
+
}
|
|
124
|
+
export function createMcpDirectToolCallRenderer(displayName) {
|
|
125
|
+
return (args, theme) => {
|
|
126
|
+
return renderToolCallLines(formatMcpDirectToolCallLines(displayName, args), theme);
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
function blockToLines(block) {
|
|
130
|
+
if (block.type === "text") {
|
|
131
|
+
return block.text.split("\n");
|
|
132
|
+
}
|
|
133
|
+
return [`[image: ${block.mimeType}]`];
|
|
134
|
+
}
|
|
135
|
+
function collectCollapsedResultLines(content, maxLines, maxChars) {
|
|
136
|
+
if (content.length === 0)
|
|
137
|
+
return { lines: ["(empty result)"], truncated: false };
|
|
138
|
+
const lines = [];
|
|
139
|
+
let remainingChars = maxChars;
|
|
140
|
+
let truncated = false;
|
|
141
|
+
const appendLine = (line) => {
|
|
142
|
+
if (lines.length >= maxLines || remainingChars <= 0) {
|
|
143
|
+
truncated = true;
|
|
144
|
+
return false;
|
|
145
|
+
}
|
|
146
|
+
if (line.length > remainingChars) {
|
|
147
|
+
lines.push(line.slice(0, remainingChars));
|
|
148
|
+
truncated = true;
|
|
149
|
+
remainingChars = 0;
|
|
150
|
+
return false;
|
|
151
|
+
}
|
|
152
|
+
lines.push(line);
|
|
153
|
+
remainingChars -= line.length + 1;
|
|
154
|
+
return true;
|
|
155
|
+
};
|
|
156
|
+
for (const block of content) {
|
|
157
|
+
if (block.type !== "text") {
|
|
158
|
+
if (!appendLine(`[image: ${block.mimeType}]`))
|
|
159
|
+
break;
|
|
160
|
+
continue;
|
|
161
|
+
}
|
|
162
|
+
let start = 0;
|
|
163
|
+
while (start <= block.text.length) {
|
|
164
|
+
const newline = block.text.indexOf("\n", start);
|
|
165
|
+
const line = newline === -1 ? block.text.slice(start) : block.text.slice(start, newline);
|
|
166
|
+
if (!appendLine(line))
|
|
167
|
+
break;
|
|
168
|
+
if (newline === -1)
|
|
169
|
+
break;
|
|
170
|
+
start = newline + 1;
|
|
171
|
+
}
|
|
172
|
+
if (truncated)
|
|
173
|
+
break;
|
|
174
|
+
}
|
|
175
|
+
if (lines.length === 0)
|
|
176
|
+
lines.push("");
|
|
177
|
+
if (truncated && lines.length >= maxLines)
|
|
178
|
+
lines.push("…");
|
|
179
|
+
return { lines, truncated };
|
|
180
|
+
}
|
|
181
|
+
export function formatMcpToolResultIdentity(details) {
|
|
182
|
+
if (details?.mode !== "call")
|
|
183
|
+
return null;
|
|
184
|
+
const server = typeof details.server === "string"
|
|
185
|
+
? details.server
|
|
186
|
+
: typeof details.hintServer === "string"
|
|
187
|
+
? details.hintServer
|
|
188
|
+
: null;
|
|
189
|
+
if (!server)
|
|
190
|
+
return null;
|
|
191
|
+
if (typeof details.tool === "string")
|
|
192
|
+
return `MCP ${server}/${details.tool}`;
|
|
193
|
+
if (typeof details.resourceUri === "string")
|
|
194
|
+
return `MCP ${server} resource ${details.resourceUri}`;
|
|
195
|
+
if (typeof details.requestedTool === "string")
|
|
196
|
+
return `MCP ${server}/${details.requestedTool}`;
|
|
197
|
+
return null;
|
|
198
|
+
}
|
|
199
|
+
export function formatMcpToolResultLines(result, expanded, maxCollapsedLines = 3, maxCollapsedChars = DEFAULT_MAX_COLLAPSED_CHARS) {
|
|
200
|
+
if (!expanded) {
|
|
201
|
+
return collectCollapsedResultLines(result.content, maxCollapsedLines, maxCollapsedChars);
|
|
202
|
+
}
|
|
203
|
+
const allLines = result.content.flatMap(blockToLines);
|
|
204
|
+
const lines = allLines.length > 0 ? allLines : ["(empty result)"];
|
|
205
|
+
return { lines, truncated: false };
|
|
206
|
+
}
|
|
207
|
+
export function renderMcpToolResult(result, options, theme, context) {
|
|
208
|
+
const activeTheme = theme ?? plainTheme;
|
|
209
|
+
if (options.isPartial) {
|
|
210
|
+
return new Text(activeTheme.fg("warning", "Running MCP tool..."), 0, 0);
|
|
211
|
+
}
|
|
212
|
+
const details = typeof result.details === "object" && result.details !== null
|
|
213
|
+
? result.details
|
|
214
|
+
: {};
|
|
215
|
+
const hasErrorDetails = Boolean(details.error);
|
|
216
|
+
const expanded = options.expanded || context?.isError === true || hasErrorDetails;
|
|
217
|
+
const display = formatMcpToolResultLines(result, expanded);
|
|
218
|
+
const identity = formatMcpToolResultIdentity(details);
|
|
219
|
+
const output = [
|
|
220
|
+
...(identity ? [activeTheme.fg("muted", identity)] : []),
|
|
221
|
+
...display.lines.map((line) => activeTheme.fg("toolOutput", line)),
|
|
222
|
+
].join("\n");
|
|
223
|
+
return new CollapsibleText(output, expanded, DEFAULT_MAX_COLLAPSED_LINES + (identity ? 1 : 0), activeTheme.fg("muted", "…"), activeTheme.fg("muted", `(${keyHint("app.tools.expand", "to expand")})`), display.truncated);
|
|
224
|
+
}
|
package/dist/ts-shape.js
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
const UNSUPPORTED_KEYWORDS = ["if", "then", "else", "allOf", "not", "patternProperties", "additionalProperties"];
|
|
2
|
+
/** Renders the useful JSON Schema subset as TypeScript, or null for unsupported schemas. */
|
|
3
|
+
export function renderTsShape(inputSchema) {
|
|
4
|
+
try {
|
|
5
|
+
if (!isSchema(inputSchema))
|
|
6
|
+
return null;
|
|
7
|
+
const definitions = new Map();
|
|
8
|
+
for (const key of ["$defs", "definitions"]) {
|
|
9
|
+
const rawDefinitions = inputSchema[key];
|
|
10
|
+
if (rawDefinitions === undefined)
|
|
11
|
+
continue;
|
|
12
|
+
if (!isSchema(rawDefinitions))
|
|
13
|
+
return null;
|
|
14
|
+
for (const [name, definition] of Object.entries(rawDefinitions)) {
|
|
15
|
+
if (!isSchema(definition))
|
|
16
|
+
return null;
|
|
17
|
+
definitions.set(`${key}/${decodePointerToken(name)}`, definition);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
const aliases = new Map();
|
|
21
|
+
const usedAliases = new Set();
|
|
22
|
+
let aliasIndex = 0;
|
|
23
|
+
const aliasFor = (definitionKey) => {
|
|
24
|
+
let alias = aliases.get(definitionKey);
|
|
25
|
+
if (alias)
|
|
26
|
+
return alias;
|
|
27
|
+
const name = definitionKey.slice(definitionKey.indexOf("/") + 1);
|
|
28
|
+
alias = /^[A-Za-z_$][\w$]*$/.test(name) && !usedAliases.has(name) ? name : `Definition${++aliasIndex}`;
|
|
29
|
+
while (usedAliases.has(alias))
|
|
30
|
+
alias = `Definition${++aliasIndex}`;
|
|
31
|
+
aliases.set(definitionKey, alias);
|
|
32
|
+
usedAliases.add(alias);
|
|
33
|
+
return alias;
|
|
34
|
+
};
|
|
35
|
+
const render = (schema) => {
|
|
36
|
+
if (!isSchema(schema) || hasUnsupportedKeyword(schema))
|
|
37
|
+
return null;
|
|
38
|
+
if ("$ref" in schema) {
|
|
39
|
+
if (typeof schema.$ref !== "string")
|
|
40
|
+
return null;
|
|
41
|
+
const match = schema.$ref.match(/^#\/(\$defs|definitions)\/([^/]+)$/);
|
|
42
|
+
if (!match)
|
|
43
|
+
return null;
|
|
44
|
+
const definitionGroup = match[1];
|
|
45
|
+
const definitionName = match[2];
|
|
46
|
+
if (definitionGroup === undefined || definitionName === undefined)
|
|
47
|
+
return null;
|
|
48
|
+
const definitionKey = `${definitionGroup}/${decodePointerToken(definitionName)}`;
|
|
49
|
+
if (!definitions.has(definitionKey))
|
|
50
|
+
return null;
|
|
51
|
+
return aliasFor(definitionKey);
|
|
52
|
+
}
|
|
53
|
+
if (Array.isArray(schema.enum)) {
|
|
54
|
+
const values = schema.enum.map(renderLiteral);
|
|
55
|
+
return values.every((value) => value !== null) ? values.join(" | ") : null;
|
|
56
|
+
}
|
|
57
|
+
if (Object.hasOwn(schema, "const"))
|
|
58
|
+
return renderLiteral(schema.const);
|
|
59
|
+
if (Array.isArray(schema.anyOf) || Array.isArray(schema.oneOf)) {
|
|
60
|
+
const variants = (schema.anyOf ?? schema.oneOf);
|
|
61
|
+
if (variants.length === 0)
|
|
62
|
+
return null;
|
|
63
|
+
const rendered = variants.map(render);
|
|
64
|
+
return rendered.every((value) => value !== null) ? rendered.join(" | ") : null;
|
|
65
|
+
}
|
|
66
|
+
if (schema.type === "object" || schema.properties !== undefined) {
|
|
67
|
+
if (schema.properties === undefined)
|
|
68
|
+
return "{}";
|
|
69
|
+
if (!isSchema(schema.properties))
|
|
70
|
+
return null;
|
|
71
|
+
const required = new Set(Array.isArray(schema.required)
|
|
72
|
+
? schema.required.filter((name) => typeof name === "string")
|
|
73
|
+
: []);
|
|
74
|
+
const properties = [];
|
|
75
|
+
for (const [name, property] of Object.entries(schema.properties)) {
|
|
76
|
+
const rendered = render(property);
|
|
77
|
+
if (rendered === null)
|
|
78
|
+
return null;
|
|
79
|
+
properties.push(`${formatPropertyName(name)}${required.has(name) ? "" : "?"}: ${rendered};`);
|
|
80
|
+
}
|
|
81
|
+
return properties.length === 0 ? "{}" : `{ ${properties.join(" ")} }`;
|
|
82
|
+
}
|
|
83
|
+
if (schema.type === "array") {
|
|
84
|
+
if (schema.items === undefined)
|
|
85
|
+
return "unknown[]";
|
|
86
|
+
const item = render(schema.items);
|
|
87
|
+
return item === null ? null : `${needsParentheses(item) ? `(${item})` : item}[]`;
|
|
88
|
+
}
|
|
89
|
+
if (Array.isArray(schema.type)) {
|
|
90
|
+
const types = schema.type.map(renderType);
|
|
91
|
+
return types.every((type) => type !== null) ? types.join(" | ") : null;
|
|
92
|
+
}
|
|
93
|
+
if (typeof schema.type === "string")
|
|
94
|
+
return renderType(schema.type);
|
|
95
|
+
return "unknown";
|
|
96
|
+
};
|
|
97
|
+
const root = render(inputSchema);
|
|
98
|
+
if (root === null)
|
|
99
|
+
return null;
|
|
100
|
+
const definitionsText = [];
|
|
101
|
+
for (const [key, alias] of aliases) {
|
|
102
|
+
const definition = definitions.get(key);
|
|
103
|
+
if (!definition)
|
|
104
|
+
return null;
|
|
105
|
+
const rendered = render(definition);
|
|
106
|
+
if (rendered === null)
|
|
107
|
+
return null;
|
|
108
|
+
definitionsText.push(`type ${alias} = ${rendered};`);
|
|
109
|
+
}
|
|
110
|
+
return definitionsText.length > 0 ? `${definitionsText.join("\n")}\n\n${root}` : root;
|
|
111
|
+
}
|
|
112
|
+
catch {
|
|
113
|
+
return null;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
function isSchema(value) {
|
|
117
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
118
|
+
}
|
|
119
|
+
function hasUnsupportedKeyword(schema) {
|
|
120
|
+
return UNSUPPORTED_KEYWORDS.some(keyword => Object.hasOwn(schema, keyword));
|
|
121
|
+
}
|
|
122
|
+
function decodePointerToken(token) {
|
|
123
|
+
return token.replace(/~1/g, "/").replace(/~0/g, "~");
|
|
124
|
+
}
|
|
125
|
+
function renderType(type) {
|
|
126
|
+
switch (type) {
|
|
127
|
+
case "string": return "string";
|
|
128
|
+
case "number":
|
|
129
|
+
case "integer": return "number";
|
|
130
|
+
case "boolean": return "boolean";
|
|
131
|
+
case "null": return "null";
|
|
132
|
+
case "object": return "{}";
|
|
133
|
+
case "array": return "unknown[]";
|
|
134
|
+
default: return null;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
function renderLiteral(value) {
|
|
138
|
+
if (value === null || typeof value === "string" || typeof value === "boolean")
|
|
139
|
+
return JSON.stringify(value);
|
|
140
|
+
return typeof value === "number" && Number.isFinite(value) ? String(value) : null;
|
|
141
|
+
}
|
|
142
|
+
function formatPropertyName(name) {
|
|
143
|
+
return /^[A-Za-z_$][\w$]*$/.test(name) ? name : JSON.stringify(name);
|
|
144
|
+
}
|
|
145
|
+
function needsParentheses(type) {
|
|
146
|
+
return type.includes(" | ");
|
|
147
|
+
}
|