@herbertgao/pi-extensions 2026.9.17 → 2026.9.18
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 +14 -15
- package/node_modules/@herbertgao/pi-bark/package.json +2 -2
- package/node_modules/@herbertgao/pi-cc-extensions/README.en.md +4 -1
- package/node_modules/@herbertgao/pi-cc-extensions/README.md +4 -1
- package/node_modules/@herbertgao/pi-cc-extensions/package.json +75 -75
- package/node_modules/@herbertgao/pi-subagents/CHANGELOG.md +6 -0
- package/node_modules/@herbertgao/pi-subagents/package.json +7 -7
- package/node_modules/@herbertgao/resume-from/package.json +4 -2
- package/node_modules/@herbertgao/sol-pi/package.json +6 -6
- package/node_modules/@juicesharp/rpiv-ask-user-question/package.json +99 -99
- package/node_modules/@pi-plugins/fast-mode/package.json +1 -1
- package/node_modules/@tifan/pi-handoff/package.json +1 -1
- package/node_modules/@tifan/pi-handoff/src/index.ts +56 -26
- package/node_modules/@tifan/pi-mermaid-open/package.json +1 -1
- package/node_modules/@tifan/pi-preferred-thinking/package.json +1 -1
- package/node_modules/@tifan/pi-recap/package.json +1 -1
- package/node_modules/@tifan/pi-rename/package.json +1 -1
- package/node_modules/pi-antigravity/package.json +2 -2
- package/node_modules/pi-lens/package.json +158 -158
- package/node_modules/pi-multi-account/package.json +2 -2
- package/node_modules/pi-typesafe/README.md +1 -1
- package/node_modules/pi-typesafe/dist/auth.d.ts +9 -5
- package/node_modules/pi-typesafe/dist/auth.js +7 -3
- package/node_modules/pi-typesafe/dist/backends.d.ts +33 -3
- package/node_modules/pi-typesafe/dist/backends.js +95 -1
- package/node_modules/pi-typesafe/dist/client.d.ts +5 -5
- package/node_modules/pi-typesafe/dist/client.js +12 -9
- package/node_modules/pi-typesafe/dist/credentials.d.ts +14 -8
- package/node_modules/pi-typesafe/dist/credentials.js +10 -7
- package/node_modules/pi-typesafe/dist/errors.d.ts +2 -2
- package/node_modules/pi-typesafe/dist/index.d.ts +2 -2
- package/node_modules/pi-typesafe/dist/index.js +1 -1
- package/node_modules/pi-typesafe/dist/login.d.ts +2 -2
- package/node_modules/pi-typesafe/dist/login.js +2 -2
- package/node_modules/pi-typesafe/package.json +5 -5
- package/node_modules/pi-web-access/package.json +2 -2
- package/node_modules/remote-pi/package.json +5 -1
- package/package.json +16 -28
- package/node_modules/pi-mcp-adapter/CHANGELOG.md +0 -1013
- package/node_modules/pi-mcp-adapter/LICENSE +0 -21
- package/node_modules/pi-mcp-adapter/OAUTH.md +0 -369
- package/node_modules/pi-mcp-adapter/README.md +0 -1021
- package/node_modules/pi-mcp-adapter/abort.ts +0 -34
- package/node_modules/pi-mcp-adapter/agent-dir.ts +0 -68
- package/node_modules/pi-mcp-adapter/agent-plugin-loader.ts +0 -416
- package/node_modules/pi-mcp-adapter/agent-plugin-provenance.ts +0 -36
- package/node_modules/pi-mcp-adapter/app-bridge.bundle.js +0 -67
- package/node_modules/pi-mcp-adapter/banner.png +0 -0
- package/node_modules/pi-mcp-adapter/bearer-command-resolver.ts +0 -193
- package/node_modules/pi-mcp-adapter/claude-plugin-loader.ts +0 -365
- package/node_modules/pi-mcp-adapter/cli.js +0 -454
- package/node_modules/pi-mcp-adapter/commands.ts +0 -861
- package/node_modules/pi-mcp-adapter/config.ts +0 -1565
- package/node_modules/pi-mcp-adapter/consent-manager.ts +0 -95
- package/node_modules/pi-mcp-adapter/direct-tool-surface.ts +0 -244
- package/node_modules/pi-mcp-adapter/direct-tools.ts +0 -428
- package/node_modules/pi-mcp-adapter/dist/abort.d.ts +0 -2
- package/node_modules/pi-mcp-adapter/dist/abort.js +0 -36
- package/node_modules/pi-mcp-adapter/dist/abort.js.map +0 -1
- package/node_modules/pi-mcp-adapter/dist/agent-dir.d.ts +0 -10
- package/node_modules/pi-mcp-adapter/dist/agent-dir.js +0 -63
- package/node_modules/pi-mcp-adapter/dist/agent-dir.js.map +0 -1
- package/node_modules/pi-mcp-adapter/dist/agent-plugin-loader.d.ts +0 -8
- package/node_modules/pi-mcp-adapter/dist/agent-plugin-loader.js +0 -398
- package/node_modules/pi-mcp-adapter/dist/agent-plugin-loader.js.map +0 -1
- package/node_modules/pi-mcp-adapter/dist/agent-plugin-provenance.d.ts +0 -1
- package/node_modules/pi-mcp-adapter/dist/agent-plugin-provenance.js +0 -31
- package/node_modules/pi-mcp-adapter/dist/agent-plugin-provenance.js.map +0 -1
- package/node_modules/pi-mcp-adapter/dist/bearer-command-resolver.d.ts +0 -6
- package/node_modules/pi-mcp-adapter/dist/bearer-command-resolver.js +0 -194
- package/node_modules/pi-mcp-adapter/dist/bearer-command-resolver.js.map +0 -1
- package/node_modules/pi-mcp-adapter/dist/claude-plugin-loader.d.ts +0 -9
- package/node_modules/pi-mcp-adapter/dist/claude-plugin-loader.js +0 -362
- package/node_modules/pi-mcp-adapter/dist/claude-plugin-loader.js.map +0 -1
- package/node_modules/pi-mcp-adapter/dist/config.d.ts +0 -134
- package/node_modules/pi-mcp-adapter/dist/config.js +0 -1361
- package/node_modules/pi-mcp-adapter/dist/config.js.map +0 -1
- package/node_modules/pi-mcp-adapter/dist/consent-manager.d.ts +0 -18
- package/node_modules/pi-mcp-adapter/dist/consent-manager.js +0 -88
- package/node_modules/pi-mcp-adapter/dist/consent-manager.js.map +0 -1
- package/node_modules/pi-mcp-adapter/dist/elicitation-handler.d.ts +0 -17
- package/node_modules/pi-mcp-adapter/dist/elicitation-handler.js +0 -316
- package/node_modules/pi-mcp-adapter/dist/elicitation-handler.js.map +0 -1
- package/node_modules/pi-mcp-adapter/dist/errors.d.ts +0 -131
- package/node_modules/pi-mcp-adapter/dist/errors.js +0 -278
- package/node_modules/pi-mcp-adapter/dist/errors.js.map +0 -1
- package/node_modules/pi-mcp-adapter/dist/http-ca.d.ts +0 -8
- package/node_modules/pi-mcp-adapter/dist/http-ca.js +0 -88
- package/node_modules/pi-mcp-adapter/dist/http-ca.js.map +0 -1
- package/node_modules/pi-mcp-adapter/dist/jev-client.d.ts +0 -14
- package/node_modules/pi-mcp-adapter/dist/jev-client.js +0 -367
- package/node_modules/pi-mcp-adapter/dist/jev-client.js.map +0 -1
- package/node_modules/pi-mcp-adapter/dist/jev-contracts.d.ts +0 -77
- package/node_modules/pi-mcp-adapter/dist/jev-contracts.js +0 -2
- package/node_modules/pi-mcp-adapter/dist/jev-contracts.js.map +0 -1
- package/node_modules/pi-mcp-adapter/dist/jev-key-store.d.ts +0 -51
- package/node_modules/pi-mcp-adapter/dist/jev-key-store.js +0 -205
- package/node_modules/pi-mcp-adapter/dist/jev-key-store.js.map +0 -1
- package/node_modules/pi-mcp-adapter/dist/json-schema-validator.d.ts +0 -2
- package/node_modules/pi-mcp-adapter/dist/json-schema-validator.js +0 -68
- package/node_modules/pi-mcp-adapter/dist/json-schema-validator.js.map +0 -1
- package/node_modules/pi-mcp-adapter/dist/lifecycle.d.ts +0 -56
- package/node_modules/pi-mcp-adapter/dist/lifecycle.js +0 -410
- package/node_modules/pi-mcp-adapter/dist/lifecycle.js.map +0 -1
- package/node_modules/pi-mcp-adapter/dist/logger.d.ts +0 -51
- package/node_modules/pi-mcp-adapter/dist/logger.js +0 -131
- package/node_modules/pi-mcp-adapter/dist/logger.js.map +0 -1
- package/node_modules/pi-mcp-adapter/dist/mcp-auth-fetch.d.ts +0 -26
- package/node_modules/pi-mcp-adapter/dist/mcp-auth-fetch.js +0 -103
- package/node_modules/pi-mcp-adapter/dist/mcp-auth-fetch.js.map +0 -1
- package/node_modules/pi-mcp-adapter/dist/mcp-auth-flow.d.ts +0 -118
- package/node_modules/pi-mcp-adapter/dist/mcp-auth-flow.js +0 -1097
- package/node_modules/pi-mcp-adapter/dist/mcp-auth-flow.js.map +0 -1
- package/node_modules/pi-mcp-adapter/dist/mcp-auth.d.ts +0 -170
- package/node_modules/pi-mcp-adapter/dist/mcp-auth.js +0 -1142
- package/node_modules/pi-mcp-adapter/dist/mcp-auth.js.map +0 -1
- package/node_modules/pi-mcp-adapter/dist/mcp-bearer-store.d.ts +0 -24
- package/node_modules/pi-mcp-adapter/dist/mcp-bearer-store.js +0 -203
- package/node_modules/pi-mcp-adapter/dist/mcp-bearer-store.js.map +0 -1
- package/node_modules/pi-mcp-adapter/dist/mcp-callback-server.d.ts +0 -53
- package/node_modules/pi-mcp-adapter/dist/mcp-callback-server.js +0 -441
- package/node_modules/pi-mcp-adapter/dist/mcp-callback-server.js.map +0 -1
- package/node_modules/pi-mcp-adapter/dist/mcp-oauth-provider.d.ts +0 -148
- package/node_modules/pi-mcp-adapter/dist/mcp-oauth-provider.js +0 -689
- package/node_modules/pi-mcp-adapter/dist/mcp-oauth-provider.js.map +0 -1
- package/node_modules/pi-mcp-adapter/dist/mcp-probe.d.ts +0 -6
- package/node_modules/pi-mcp-adapter/dist/mcp-probe.js +0 -166
- package/node_modules/pi-mcp-adapter/dist/mcp-probe.js.map +0 -1
- package/node_modules/pi-mcp-adapter/dist/mcp-tasks.d.ts +0 -102
- package/node_modules/pi-mcp-adapter/dist/mcp-tasks.js +0 -369
- package/node_modules/pi-mcp-adapter/dist/mcp-tasks.js.map +0 -1
- package/node_modules/pi-mcp-adapter/dist/mcp-trace.d.ts +0 -95
- package/node_modules/pi-mcp-adapter/dist/mcp-trace.js +0 -242
- package/node_modules/pi-mcp-adapter/dist/mcp-trace.js.map +0 -1
- package/node_modules/pi-mcp-adapter/dist/metadata-cache.d.ts +0 -19
- package/node_modules/pi-mcp-adapter/dist/metadata-cache.js +0 -301
- package/node_modules/pi-mcp-adapter/dist/metadata-cache.js.map +0 -1
- package/node_modules/pi-mcp-adapter/dist/npx-resolver.d.ts +0 -6
- package/node_modules/pi-mcp-adapter/dist/npx-resolver.js +0 -505
- package/node_modules/pi-mcp-adapter/dist/npx-resolver.js.map +0 -1
- package/node_modules/pi-mcp-adapter/dist/package-mcp-loader.d.ts +0 -2
- package/node_modules/pi-mcp-adapter/dist/package-mcp-loader.js +0 -144
- package/node_modules/pi-mcp-adapter/dist/package-mcp-loader.js.map +0 -1
- package/node_modules/pi-mcp-adapter/dist/request-headers-command.d.ts +0 -10
- package/node_modules/pi-mcp-adapter/dist/request-headers-command.js +0 -312
- package/node_modules/pi-mcp-adapter/dist/request-headers-command.js.map +0 -1
- package/node_modules/pi-mcp-adapter/dist/resource-tools.d.ts +0 -1
- package/node_modules/pi-mcp-adapter/dist/resource-tools.js +0 -15
- package/node_modules/pi-mcp-adapter/dist/resource-tools.js.map +0 -1
- package/node_modules/pi-mcp-adapter/dist/runtime-owner.d.ts +0 -13
- package/node_modules/pi-mcp-adapter/dist/runtime-owner.js +0 -97
- package/node_modules/pi-mcp-adapter/dist/runtime-owner.js.map +0 -1
- package/node_modules/pi-mcp-adapter/dist/sampling-handler.d.ts +0 -17
- package/node_modules/pi-mcp-adapter/dist/sampling-handler.js +0 -203
- package/node_modules/pi-mcp-adapter/dist/sampling-handler.js.map +0 -1
- package/node_modules/pi-mcp-adapter/dist/secure-keyring.d.ts +0 -11
- package/node_modules/pi-mcp-adapter/dist/secure-keyring.js +0 -172
- package/node_modules/pi-mcp-adapter/dist/secure-keyring.js.map +0 -1
- package/node_modules/pi-mcp-adapter/dist/server-manager.d.ts +0 -157
- package/node_modules/pi-mcp-adapter/dist/server-manager.js +0 -1745
- package/node_modules/pi-mcp-adapter/dist/server-manager.js.map +0 -1
- package/node_modules/pi-mcp-adapter/dist/session-approvals.d.ts +0 -34
- package/node_modules/pi-mcp-adapter/dist/session-approvals.js +0 -140
- package/node_modules/pi-mcp-adapter/dist/session-approvals.js.map +0 -1
- package/node_modules/pi-mcp-adapter/dist/session-recovery.d.ts +0 -26
- package/node_modules/pi-mcp-adapter/dist/session-recovery.js +0 -142
- package/node_modules/pi-mcp-adapter/dist/session-recovery.js.map +0 -1
- package/node_modules/pi-mcp-adapter/dist/state.d.ts +0 -73
- package/node_modules/pi-mcp-adapter/dist/state.js +0 -2
- package/node_modules/pi-mcp-adapter/dist/state.js.map +0 -1
- package/node_modules/pi-mcp-adapter/dist/types.d.ts +0 -642
- package/node_modules/pi-mcp-adapter/dist/types.js +0 -331
- package/node_modules/pi-mcp-adapter/dist/types.js.map +0 -1
- package/node_modules/pi-mcp-adapter/dist/ui-app-bridge-helpers.d.ts +0 -6
- package/node_modules/pi-mcp-adapter/dist/ui-app-bridge-helpers.js +0 -37
- package/node_modules/pi-mcp-adapter/dist/ui-app-bridge-helpers.js.map +0 -1
- package/node_modules/pi-mcp-adapter/dist/ui-resource-handler.d.ts +0 -17
- package/node_modules/pi-mcp-adapter/dist/ui-resource-handler.js +0 -219
- package/node_modules/pi-mcp-adapter/dist/ui-resource-handler.js.map +0 -1
- package/node_modules/pi-mcp-adapter/dist/ui-stream-types.d.ts +0 -104
- package/node_modules/pi-mcp-adapter/dist/ui-stream-types.js +0 -59
- package/node_modules/pi-mcp-adapter/dist/ui-stream-types.js.map +0 -1
- package/node_modules/pi-mcp-adapter/dist/ui-tool-visibility.d.ts +0 -4
- package/node_modules/pi-mcp-adapter/dist/ui-tool-visibility.js +0 -27
- package/node_modules/pi-mcp-adapter/dist/ui-tool-visibility.js.map +0 -1
- package/node_modules/pi-mcp-adapter/dist/unix-socket-transport.d.ts +0 -15
- package/node_modules/pi-mcp-adapter/dist/unix-socket-transport.js +0 -85
- package/node_modules/pi-mcp-adapter/dist/unix-socket-transport.js.map +0 -1
- package/node_modules/pi-mcp-adapter/dist/utils.d.ts +0 -41
- package/node_modules/pi-mcp-adapter/dist/utils.js +0 -417
- package/node_modules/pi-mcp-adapter/dist/utils.js.map +0 -1
- package/node_modules/pi-mcp-adapter/elicitation-handler.ts +0 -358
- package/node_modules/pi-mcp-adapter/error-signal.ts +0 -32
- package/node_modules/pi-mcp-adapter/errors.ts +0 -364
- package/node_modules/pi-mcp-adapter/examples/jev-accessibility-loop.mjs +0 -85
- package/node_modules/pi-mcp-adapter/examples/jev-semantic-filter.mjs +0 -34
- package/node_modules/pi-mcp-adapter/failure-backoff.ts +0 -23
- package/node_modules/pi-mcp-adapter/glimpse-ui.ts +0 -80
- package/node_modules/pi-mcp-adapter/host-html-template.ts +0 -515
- package/node_modules/pi-mcp-adapter/http-ca.ts +0 -84
- package/node_modules/pi-mcp-adapter/index.ts +0 -2106
- package/node_modules/pi-mcp-adapter/init.ts +0 -721
- package/node_modules/pi-mcp-adapter/jev-client.ts +0 -300
- package/node_modules/pi-mcp-adapter/jev-contracts.ts +0 -53
- package/node_modules/pi-mcp-adapter/jev-key-store.ts +0 -192
- package/node_modules/pi-mcp-adapter/json-schema-validator.ts +0 -83
- package/node_modules/pi-mcp-adapter/lazy-loader.ts +0 -7
- package/node_modules/pi-mcp-adapter/lifecycle.ts +0 -457
- package/node_modules/pi-mcp-adapter/logger.ts +0 -169
- package/node_modules/pi-mcp-adapter/mcp-auth-fetch.ts +0 -113
- package/node_modules/pi-mcp-adapter/mcp-auth-flow.ts +0 -1296
- package/node_modules/pi-mcp-adapter/mcp-auth.ts +0 -1287
- package/node_modules/pi-mcp-adapter/mcp-bearer-store.ts +0 -250
- package/node_modules/pi-mcp-adapter/mcp-callback-server.ts +0 -534
- package/node_modules/pi-mcp-adapter/mcp-code.ts +0 -495
- package/node_modules/pi-mcp-adapter/mcp-install.ts +0 -60
- package/node_modules/pi-mcp-adapter/mcp-keyring-helper.cjs +0 -89
- package/node_modules/pi-mcp-adapter/mcp-oauth-provider.ts +0 -819
- package/node_modules/pi-mcp-adapter/mcp-output-guard.ts +0 -511
- package/node_modules/pi-mcp-adapter/mcp-panel-theme.ts +0 -104
- package/node_modules/pi-mcp-adapter/mcp-panel.ts +0 -1099
- package/node_modules/pi-mcp-adapter/mcp-probe.ts +0 -201
- package/node_modules/pi-mcp-adapter/mcp-references.ts +0 -373
- package/node_modules/pi-mcp-adapter/mcp-script-worker.mjs +0 -137
- package/node_modules/pi-mcp-adapter/mcp-setup-panel.ts +0 -747
- package/node_modules/pi-mcp-adapter/mcp-status.ts +0 -105
- package/node_modules/pi-mcp-adapter/mcp-tasks.ts +0 -467
- package/node_modules/pi-mcp-adapter/mcp-trace.ts +0 -306
- package/node_modules/pi-mcp-adapter/metadata-cache.ts +0 -362
- package/node_modules/pi-mcp-adapter/namespace-tools.ts +0 -334
- package/node_modules/pi-mcp-adapter/npx-resolver.ts +0 -543
- package/node_modules/pi-mcp-adapter/oauth-handler.ts +0 -30
- package/node_modules/pi-mcp-adapter/oauth.ts +0 -52
- package/node_modules/pi-mcp-adapter/onboarding-state.ts +0 -76
- package/node_modules/pi-mcp-adapter/package-mcp-loader.ts +0 -156
- package/node_modules/pi-mcp-adapter/package.json +0 -219
- package/node_modules/pi-mcp-adapter/panel-keys.ts +0 -53
- package/node_modules/pi-mcp-adapter/prompts.ts +0 -385
- package/node_modules/pi-mcp-adapter/proxy-modes.ts +0 -1735
- package/node_modules/pi-mcp-adapter/request-headers-command.ts +0 -333
- package/node_modules/pi-mcp-adapter/resource-tools.ts +0 -17
- package/node_modules/pi-mcp-adapter/runtime-owner.ts +0 -106
- package/node_modules/pi-mcp-adapter/sampling-handler.ts +0 -261
- package/node_modules/pi-mcp-adapter/sandbox-proxy-template.ts +0 -144
- package/node_modules/pi-mcp-adapter/search-ranking.ts +0 -309
- package/node_modules/pi-mcp-adapter/secure-keyring.ts +0 -170
- package/node_modules/pi-mcp-adapter/semantic-search.ts +0 -186
- package/node_modules/pi-mcp-adapter/server-manager.ts +0 -2088
- package/node_modules/pi-mcp-adapter/session-approvals.ts +0 -201
- package/node_modules/pi-mcp-adapter/session-recovery.ts +0 -158
- package/node_modules/pi-mcp-adapter/skills/mcp-scripting/SKILL.md +0 -49
- package/node_modules/pi-mcp-adapter/state.ts +0 -71
- package/node_modules/pi-mcp-adapter/tool-approval.ts +0 -205
- package/node_modules/pi-mcp-adapter/tool-metadata.ts +0 -302
- package/node_modules/pi-mcp-adapter/tool-registrar.ts +0 -255
- package/node_modules/pi-mcp-adapter/tool-result-renderer.ts +0 -573
- package/node_modules/pi-mcp-adapter/ts-shape.ts +0 -149
- package/node_modules/pi-mcp-adapter/types.ts +0 -1120
- package/node_modules/pi-mcp-adapter/ui-app-bridge-helpers.ts +0 -40
- package/node_modules/pi-mcp-adapter/ui-resource-handler.ts +0 -266
- package/node_modules/pi-mcp-adapter/ui-server.ts +0 -993
- package/node_modules/pi-mcp-adapter/ui-session.ts +0 -624
- package/node_modules/pi-mcp-adapter/ui-stream-types.ts +0 -89
- package/node_modules/pi-mcp-adapter/ui-tool-visibility.ts +0 -25
- package/node_modules/pi-mcp-adapter/unix-socket-transport.ts +0 -85
- package/node_modules/pi-mcp-adapter/utils.ts +0 -464
|
@@ -1,511 +0,0 @@
|
|
|
1
|
-
import { randomBytes } from "node:crypto";
|
|
2
|
-
import { mkdtemp, rm, writeFile } from "node:fs/promises";
|
|
3
|
-
import { tmpdir } from "node:os";
|
|
4
|
-
import { dirname, join } from "node:path";
|
|
5
|
-
import {
|
|
6
|
-
DEFAULT_MAX_BYTES,
|
|
7
|
-
DEFAULT_MAX_LINES,
|
|
8
|
-
formatSize,
|
|
9
|
-
truncateHead,
|
|
10
|
-
type TruncationResult,
|
|
11
|
-
} from "@earendil-works/pi-coding-agent";
|
|
12
|
-
import type { ContentBlock, McpSettings } from "./types.ts";
|
|
13
|
-
|
|
14
|
-
export const DEFAULT_MCP_OUTPUT_MAX_BYTES = DEFAULT_MAX_BYTES;
|
|
15
|
-
export const DEFAULT_MCP_OUTPUT_MAX_LINES = DEFAULT_MAX_LINES;
|
|
16
|
-
export const DEFAULT_MCP_DETAILS_MAX_BYTES = 16 * 1024;
|
|
17
|
-
|
|
18
|
-
const CONTENT_SUMMARY_LIMIT = 20;
|
|
19
|
-
const KEY_PREVIEW_LIMIT = 20;
|
|
20
|
-
const KEY_MAX_BYTES = 120;
|
|
21
|
-
const STRUCTURED_CONTENT_PRESERVE_MAX_BYTES = 4 * 1024;
|
|
22
|
-
const STRUCTURED_CONTENT_FIELD_PRESERVE_MAX_BYTES = 512;
|
|
23
|
-
|
|
24
|
-
type Recordish = Record<string, unknown>;
|
|
25
|
-
|
|
26
|
-
export interface McpOutputGuardDetails {
|
|
27
|
-
truncated: true;
|
|
28
|
-
originalBytes: number;
|
|
29
|
-
returnedBytes: number;
|
|
30
|
-
originalLines: number;
|
|
31
|
-
returnedLines: number;
|
|
32
|
-
/** Host truncation classification and preview counts for the composed text before the MCP notice is added. */
|
|
33
|
-
truncatedBy: TruncationResult["truncatedBy"];
|
|
34
|
-
totalLines: number;
|
|
35
|
-
totalBytes: number;
|
|
36
|
-
outputLines: number;
|
|
37
|
-
outputBytes: number;
|
|
38
|
-
lastLinePartial: boolean;
|
|
39
|
-
firstLineExceedsLimit: boolean;
|
|
40
|
-
maxLines: number;
|
|
41
|
-
maxBytes: number;
|
|
42
|
-
/** Number of image content blocks returned untouched alongside the truncated text. */
|
|
43
|
-
imageBlocksPassedThrough?: number;
|
|
44
|
-
fullOutputPath?: string;
|
|
45
|
-
writeError?: string;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
export interface McpResultSummary {
|
|
49
|
-
omitted: true;
|
|
50
|
-
reason: string;
|
|
51
|
-
isError: boolean;
|
|
52
|
-
contentBlocks: number;
|
|
53
|
-
contentSummary: Array<Record<string, unknown>>;
|
|
54
|
-
structuredContent?: Record<string, unknown>;
|
|
55
|
-
meta?: Record<string, unknown>;
|
|
56
|
-
extraFields?: Array<Record<string, unknown>>;
|
|
57
|
-
rawResultBytes: number;
|
|
58
|
-
fullResultPath?: string;
|
|
59
|
-
resultWriteError?: string;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
export interface McpOutputGuardOptions {
|
|
63
|
-
enabled?: boolean;
|
|
64
|
-
prefix?: string;
|
|
65
|
-
suffix?: string;
|
|
66
|
-
emptyTextFallback?: string;
|
|
67
|
-
maxBytes?: number;
|
|
68
|
-
maxLines?: number;
|
|
69
|
-
detailsMaxBytes?: number;
|
|
70
|
-
/**
|
|
71
|
-
* Raw MCP result to expose as details.mcpResult. Kept raw when its JSON
|
|
72
|
-
* fits detailsMaxBytes (or when the guard is disabled); otherwise replaced
|
|
73
|
-
* with a compact summary and spilled to a temp file. Omit for call sites
|
|
74
|
-
* whose details never carried the raw result (e.g. direct tools).
|
|
75
|
-
*/
|
|
76
|
-
rawMcpResult?: unknown;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
export interface GuardedMcpOutput {
|
|
80
|
-
content: ContentBlock[];
|
|
81
|
-
outputGuard?: McpOutputGuardDetails;
|
|
82
|
-
mcpResult?: unknown;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
export function resolveMcpOutputGuardOptions(settings?: McpSettings): Pick<McpOutputGuardOptions, "enabled" | "maxBytes" | "maxLines" | "detailsMaxBytes"> {
|
|
86
|
-
const configured = settings?.outputGuard;
|
|
87
|
-
const tuning = typeof configured === "object" && configured !== null ? configured : undefined;
|
|
88
|
-
return {
|
|
89
|
-
enabled: envKillSwitch("MCP_OUTPUT_GUARD") ?? configured !== false,
|
|
90
|
-
maxBytes: positiveInt(tuning?.maxBytes) ?? DEFAULT_MCP_OUTPUT_MAX_BYTES,
|
|
91
|
-
maxLines: positiveInt(tuning?.maxLines) ?? DEFAULT_MCP_OUTPUT_MAX_LINES,
|
|
92
|
-
detailsMaxBytes: positiveInt(tuning?.detailsMaxBytes) ?? DEFAULT_MCP_DETAILS_MAX_BYTES,
|
|
93
|
-
};
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
/** Spread helper for tool-result details: includes mcpResult/outputGuard only when present. */
|
|
97
|
-
export function guardedMcpDetails(guarded: GuardedMcpOutput): Record<string, unknown> {
|
|
98
|
-
return {
|
|
99
|
-
...(guarded.mcpResult !== undefined ? { mcpResult: guarded.mcpResult } : {}),
|
|
100
|
-
...(guarded.outputGuard ? { outputGuard: guarded.outputGuard } : {}),
|
|
101
|
-
};
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
/**
|
|
105
|
-
* Bound model-facing MCP output. Text output is capped at maxBytes/maxLines and
|
|
106
|
-
* spilled to a temp file when oversized. Image blocks pass through untouched —
|
|
107
|
-
* they are delivered to the provider as native image content, not text context.
|
|
108
|
-
*/
|
|
109
|
-
export async function guardMcpOutput(
|
|
110
|
-
content: ContentBlock[],
|
|
111
|
-
options: McpOutputGuardOptions = {},
|
|
112
|
-
): Promise<GuardedMcpOutput> {
|
|
113
|
-
const maxBytes = options.maxBytes ?? DEFAULT_MCP_OUTPUT_MAX_BYTES;
|
|
114
|
-
const maxLines = options.maxLines ?? DEFAULT_MCP_OUTPUT_MAX_LINES;
|
|
115
|
-
const detailsMaxBytes = options.detailsMaxBytes ?? DEFAULT_MCP_DETAILS_MAX_BYTES;
|
|
116
|
-
const prefix = options.prefix ?? "";
|
|
117
|
-
const suffix = options.suffix ?? "";
|
|
118
|
-
|
|
119
|
-
const normalizedContent = withEmptyTextFallback(
|
|
120
|
-
content.length > 0
|
|
121
|
-
? sanitizeContent(content)
|
|
122
|
-
: [{ type: "text" as const, text: options.emptyTextFallback ?? "(empty result)" }],
|
|
123
|
-
options.emptyTextFallback,
|
|
124
|
-
);
|
|
125
|
-
|
|
126
|
-
if (options.enabled === false) {
|
|
127
|
-
return {
|
|
128
|
-
content: addAffixes(normalizedContent, prefix, suffix),
|
|
129
|
-
...(options.rawMcpResult !== undefined ? { mcpResult: options.rawMcpResult } : {}),
|
|
130
|
-
};
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
const imageBlocks = normalizedContent.filter((block) => block.type === "image");
|
|
134
|
-
const textOutput = normalizedContent
|
|
135
|
-
.filter((block) => block.type === "text")
|
|
136
|
-
.map((block) => (block as { text: string }).text)
|
|
137
|
-
.join("\n");
|
|
138
|
-
const composedOutput = `${prefix}${textOutput}${suffix}`;
|
|
139
|
-
const truncation = truncateHead(composedOutput, { maxBytes, maxLines });
|
|
140
|
-
|
|
141
|
-
let guardedContent: ContentBlock[] = addAffixes(normalizedContent, prefix, suffix);
|
|
142
|
-
let outputGuard: McpOutputGuardDetails | undefined;
|
|
143
|
-
|
|
144
|
-
if (truncation.truncated) {
|
|
145
|
-
const { path: fullOutputPath, error: writeError } = await saveArtifact("output", composedOutput);
|
|
146
|
-
const initialNotice = formatTruncationNotice(truncation, fullOutputPath, writeError);
|
|
147
|
-
const previewBudget = reserveBudget(maxBytes, maxLines, initialNotice);
|
|
148
|
-
const preview = truncateHead(composedOutput, {
|
|
149
|
-
maxBytes: previewBudget.maxBytes,
|
|
150
|
-
maxLines: previewBudget.maxLines,
|
|
151
|
-
});
|
|
152
|
-
const notice = formatTruncationNotice(
|
|
153
|
-
{ ...truncation, outputLines: preview.outputLines, outputBytes: preview.outputBytes },
|
|
154
|
-
fullOutputPath,
|
|
155
|
-
writeError,
|
|
156
|
-
);
|
|
157
|
-
const finalText = `${preview.content}\n\n${notice}`;
|
|
158
|
-
const finalStats = textStats(finalText);
|
|
159
|
-
|
|
160
|
-
guardedContent = [{ type: "text" as const, text: finalText }, ...imageBlocks];
|
|
161
|
-
outputGuard = {
|
|
162
|
-
truncated: true,
|
|
163
|
-
originalBytes: truncation.totalBytes,
|
|
164
|
-
returnedBytes: finalStats.bytes,
|
|
165
|
-
originalLines: truncation.totalLines,
|
|
166
|
-
returnedLines: finalStats.lines,
|
|
167
|
-
truncatedBy: truncation.truncatedBy,
|
|
168
|
-
totalLines: truncation.totalLines,
|
|
169
|
-
totalBytes: truncation.totalBytes,
|
|
170
|
-
outputLines: preview.outputLines,
|
|
171
|
-
outputBytes: preview.outputBytes,
|
|
172
|
-
lastLinePartial: truncation.lastLinePartial,
|
|
173
|
-
firstLineExceedsLimit: truncation.firstLineExceedsLimit,
|
|
174
|
-
maxLines: truncation.maxLines,
|
|
175
|
-
maxBytes: truncation.maxBytes,
|
|
176
|
-
...(imageBlocks.length > 0 ? { imageBlocksPassedThrough: imageBlocks.length } : {}),
|
|
177
|
-
...(fullOutputPath !== undefined ? { fullOutputPath } : {}),
|
|
178
|
-
...(writeError !== undefined ? { writeError } : {}),
|
|
179
|
-
};
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
const mcpResult = options.rawMcpResult === undefined
|
|
183
|
-
? undefined
|
|
184
|
-
: await boundMcpResult(options.rawMcpResult, detailsMaxBytes);
|
|
185
|
-
|
|
186
|
-
return {
|
|
187
|
-
content: guardedContent,
|
|
188
|
-
...(outputGuard ? { outputGuard } : {}),
|
|
189
|
-
...(mcpResult !== undefined ? { mcpResult } : {}),
|
|
190
|
-
};
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
function sanitizeContent(content: ContentBlock[]): ContentBlock[] {
|
|
194
|
-
return content.map((block) => {
|
|
195
|
-
if (block.type !== "image") return block;
|
|
196
|
-
const mimeType = typeof block.mimeType === "string" && block.mimeType.trim()
|
|
197
|
-
? block.mimeType.trim().slice(0, 100)
|
|
198
|
-
: "image/png";
|
|
199
|
-
return { ...block, mimeType };
|
|
200
|
-
});
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
function withEmptyTextFallback(content: ContentBlock[], fallback: string | undefined): ContentBlock[] {
|
|
204
|
-
if (!fallback) return content;
|
|
205
|
-
const textOutput = content
|
|
206
|
-
.filter((block) => block.type === "text")
|
|
207
|
-
.map((block) => (block as { text: string }).text)
|
|
208
|
-
.join("\n");
|
|
209
|
-
if (textOutput) return content;
|
|
210
|
-
return [{ type: "text", text: fallback }, ...content.filter((block) => block.type === "image")];
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
function addAffixes(content: ContentBlock[], prefix: string, suffix: string): ContentBlock[] {
|
|
214
|
-
if (!prefix && !suffix) return content;
|
|
215
|
-
const next: ContentBlock[] = [...content];
|
|
216
|
-
|
|
217
|
-
if (prefix) {
|
|
218
|
-
const index = next.findIndex((block) => block.type === "text");
|
|
219
|
-
const block = next[index];
|
|
220
|
-
if (block?.type === "text") {
|
|
221
|
-
next[index] = { ...block, text: `${prefix}${block.text}` };
|
|
222
|
-
} else {
|
|
223
|
-
next.unshift({ type: "text", text: prefix });
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
if (suffix) {
|
|
228
|
-
let index = -1;
|
|
229
|
-
for (let i = next.length - 1; i >= 0; i--) {
|
|
230
|
-
if (next[i]?.type === "text") {
|
|
231
|
-
index = i;
|
|
232
|
-
break;
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
|
-
const block = next[index];
|
|
236
|
-
if (block?.type === "text") {
|
|
237
|
-
next[index] = { ...block, text: `${block.text}${suffix}` };
|
|
238
|
-
} else {
|
|
239
|
-
next.push({ type: "text", text: suffix });
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
return next;
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
function reserveBudget(maxBytes: number, maxLines: number, notice: string): { maxBytes: number; maxLines: number } {
|
|
247
|
-
const noticeStats = textStats(`\n\n${notice}`);
|
|
248
|
-
return {
|
|
249
|
-
maxBytes: Math.max(0, maxBytes - noticeStats.bytes),
|
|
250
|
-
maxLines: Math.max(0, maxLines - noticeStats.lines),
|
|
251
|
-
};
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
function truncateStringToBytes(value: string, maxBytes: number): string {
|
|
255
|
-
if (byteLength(value) <= maxBytes) return value;
|
|
256
|
-
const buffer = Buffer.from(value, "utf8");
|
|
257
|
-
let end = Number.isFinite(maxBytes) ? Math.max(0, Math.floor(maxBytes)) : 0;
|
|
258
|
-
while (end > 0 && (buffer.readUInt8(end) & 0xc0) === 0x80) end--;
|
|
259
|
-
return buffer.subarray(0, end).toString("utf8");
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
function formatTruncationNotice(
|
|
263
|
-
truncation: TruncationResult,
|
|
264
|
-
fullOutputPath: string | undefined,
|
|
265
|
-
writeError: string | undefined,
|
|
266
|
-
): string {
|
|
267
|
-
let reason: string;
|
|
268
|
-
if (truncation.firstLineExceedsLimit) {
|
|
269
|
-
reason = `First line exceeds ${formatSize(truncation.maxBytes)} limit`;
|
|
270
|
-
} else if (truncation.truncatedBy === "lines") {
|
|
271
|
-
reason = `Truncated: showing ${truncation.outputLines} of ${truncation.totalLines} lines (${truncation.maxLines} line limit)`;
|
|
272
|
-
} else {
|
|
273
|
-
reason = `Truncated: ${truncation.outputLines} lines shown (${formatSize(truncation.maxBytes)} limit)`;
|
|
274
|
-
}
|
|
275
|
-
const base = `[MCP text output truncated: original ${truncation.totalLines.toLocaleString()} lines / ${formatSize(truncation.totalBytes)}. ${reason}.`;
|
|
276
|
-
if (fullOutputPath) {
|
|
277
|
-
return `${base} Full text saved to: ${fullOutputPath} — use read with offset/limit or grep to inspect.]`;
|
|
278
|
-
}
|
|
279
|
-
return `${base} Full output could not be saved: ${writeError ?? "unknown error"}]`;
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
/**
|
|
283
|
-
* Bound details.mcpResult: keep the raw result when its JSON fits within
|
|
284
|
-
* detailsMaxBytes; otherwise replace it with a compact summary and spill the
|
|
285
|
-
* raw JSON to a temp file.
|
|
286
|
-
*/
|
|
287
|
-
async function boundMcpResult(result: unknown, detailsMaxBytes: number): Promise<unknown> {
|
|
288
|
-
const raw = safeStringify(result);
|
|
289
|
-
const rawBytes = byteLength(raw);
|
|
290
|
-
if (rawBytes <= detailsMaxBytes) return result;
|
|
291
|
-
const marker = { omitted: true } as const;
|
|
292
|
-
if (byteLength(safeStringify(marker)) > detailsMaxBytes) return undefined;
|
|
293
|
-
|
|
294
|
-
const summary = await summarizeMcpResult(result, raw, rawBytes);
|
|
295
|
-
if (byteLength(safeStringify(summary)) <= detailsMaxBytes) return summary;
|
|
296
|
-
return compactMcpResultOmission(summary, marker, detailsMaxBytes);
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
async function compactMcpResultOmission(
|
|
300
|
-
summary: McpResultSummary,
|
|
301
|
-
marker: Readonly<{ omitted: true }>,
|
|
302
|
-
maxBytes: number,
|
|
303
|
-
): Promise<unknown> {
|
|
304
|
-
if (summary.fullResultPath) {
|
|
305
|
-
const withPathAndSize = {
|
|
306
|
-
...marker,
|
|
307
|
-
rawResultBytes: summary.rawResultBytes,
|
|
308
|
-
fullResultPath: summary.fullResultPath,
|
|
309
|
-
};
|
|
310
|
-
if (byteLength(safeStringify(withPathAndSize)) <= maxBytes) return withPathAndSize;
|
|
311
|
-
const withPath = { ...marker, fullResultPath: summary.fullResultPath };
|
|
312
|
-
if (byteLength(safeStringify(withPath)) <= maxBytes) return withPath;
|
|
313
|
-
await discardArtifact(summary.fullResultPath);
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
const withSize = { ...marker, rawResultBytes: summary.rawResultBytes };
|
|
317
|
-
return byteLength(safeStringify(withSize)) <= maxBytes ? withSize : marker;
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
async function summarizeMcpResult(result: unknown, raw: string, rawBytes: number): Promise<McpResultSummary> {
|
|
321
|
-
const { path: fullResultPath, error: resultWriteError } = await saveArtifact("mcp-result", raw);
|
|
322
|
-
|
|
323
|
-
const record = asRecord(result);
|
|
324
|
-
const content = Array.isArray(record?.content) ? record.content : [];
|
|
325
|
-
const summary: McpResultSummary = {
|
|
326
|
-
omitted: true,
|
|
327
|
-
reason: "Raw MCP result exceeded the details size limit and was replaced with this summary to keep session context bounded.",
|
|
328
|
-
isError: record?.isError === true,
|
|
329
|
-
contentBlocks: content.length,
|
|
330
|
-
contentSummary: summarizeContent(content),
|
|
331
|
-
rawResultBytes: rawBytes,
|
|
332
|
-
...(fullResultPath !== undefined ? { fullResultPath } : {}),
|
|
333
|
-
...(resultWriteError !== undefined ? { resultWriteError } : {}),
|
|
334
|
-
};
|
|
335
|
-
|
|
336
|
-
if (record && "structuredContent" in record) {
|
|
337
|
-
summary.structuredContent = summarizeStructuredContent(record.structuredContent);
|
|
338
|
-
}
|
|
339
|
-
if (record && "_meta" in record) {
|
|
340
|
-
summary.meta = summarizeValue(record._meta);
|
|
341
|
-
}
|
|
342
|
-
if (record) {
|
|
343
|
-
const standard = new Set(["content", "isError", "structuredContent", "_meta"]);
|
|
344
|
-
const extraFields = Object.keys(record)
|
|
345
|
-
.filter((key) => !standard.has(key))
|
|
346
|
-
.slice(0, KEY_PREVIEW_LIMIT)
|
|
347
|
-
.map((key) => ({ key: truncateKey(key), type: typeof record[key], estimatedBytes: estimateValueBytes(record[key]), omitted: true }));
|
|
348
|
-
if (extraFields.length > 0) summary.extraFields = extraFields;
|
|
349
|
-
}
|
|
350
|
-
|
|
351
|
-
return summary;
|
|
352
|
-
}
|
|
353
|
-
|
|
354
|
-
function summarizeContent(content: unknown[]): Array<Record<string, unknown>> {
|
|
355
|
-
const summaries: Array<Record<string, unknown>> = content.slice(0, CONTENT_SUMMARY_LIMIT).map((block) => {
|
|
356
|
-
const record = asRecord(block);
|
|
357
|
-
if (!record) return { type: typeof block, omitted: true };
|
|
358
|
-
if (record.type === "text") {
|
|
359
|
-
const text = typeof record.text === "string" ? record.text : "";
|
|
360
|
-
return { type: "text", bytes: byteLength(text), lines: textStats(text).lines, textOmitted: true };
|
|
361
|
-
}
|
|
362
|
-
if (record.type === "image") {
|
|
363
|
-
const data = typeof record.data === "string" ? record.data : "";
|
|
364
|
-
return { type: "image", mimeType: typeof record.mimeType === "string" ? record.mimeType : undefined, dataBytes: byteLength(data), dataOmitted: true };
|
|
365
|
-
}
|
|
366
|
-
return { type: typeof record.type === "string" ? record.type : "unknown", estimatedBytes: estimateValueBytes(record), omitted: true };
|
|
367
|
-
});
|
|
368
|
-
if (content.length > CONTENT_SUMMARY_LIMIT) {
|
|
369
|
-
summaries.push({ type: "omitted", count: content.length - CONTENT_SUMMARY_LIMIT });
|
|
370
|
-
}
|
|
371
|
-
return summaries;
|
|
372
|
-
}
|
|
373
|
-
|
|
374
|
-
function summarizeValue(value: unknown): Record<string, unknown> {
|
|
375
|
-
const record = asRecord(value);
|
|
376
|
-
if (!record) {
|
|
377
|
-
return { type: value === null ? "null" : typeof value, estimatedBytes: estimateValueBytes(value), omitted: true };
|
|
378
|
-
}
|
|
379
|
-
const keys = Object.keys(record);
|
|
380
|
-
return {
|
|
381
|
-
type: Array.isArray(value) ? "array" : "object",
|
|
382
|
-
estimatedBytes: estimateValueBytes(value),
|
|
383
|
-
keyCount: keys.length,
|
|
384
|
-
keysPreview: uniqueBoundedKeys(keys.slice(0, KEY_PREVIEW_LIMIT)),
|
|
385
|
-
omitted: true,
|
|
386
|
-
};
|
|
387
|
-
}
|
|
388
|
-
|
|
389
|
-
function summarizeStructuredContent(value: unknown): Record<string, unknown> {
|
|
390
|
-
const record = asRecord(value);
|
|
391
|
-
if (!record || Array.isArray(value)) return summarizeValue(value);
|
|
392
|
-
const keys = Object.keys(record);
|
|
393
|
-
const entries = Object.entries(record).slice(0, KEY_PREVIEW_LIMIT);
|
|
394
|
-
const previewKeys = uniqueBoundedKeys(entries.map(([key]) => key));
|
|
395
|
-
const fields: Record<string, unknown> = {};
|
|
396
|
-
let preservedBytes = byteLength("{}");
|
|
397
|
-
for (const [key, field] of entries) {
|
|
398
|
-
const fieldBytes = byteLength(safeStringify(field));
|
|
399
|
-
const candidate = fieldBytes <= STRUCTURED_CONTENT_FIELD_PRESERVE_MAX_BYTES
|
|
400
|
-
? field
|
|
401
|
-
: summarizeValue(field);
|
|
402
|
-
const entryBytes = serializedObjectEntryBytes(key, candidate, Object.keys(fields).length > 0);
|
|
403
|
-
if (preservedBytes + entryBytes > STRUCTURED_CONTENT_PRESERVE_MAX_BYTES) continue;
|
|
404
|
-
fields[key] = candidate;
|
|
405
|
-
preservedBytes += entryBytes;
|
|
406
|
-
}
|
|
407
|
-
const preservedCount = Object.keys(fields).length;
|
|
408
|
-
return {
|
|
409
|
-
omitted: true,
|
|
410
|
-
preservedFields: fields,
|
|
411
|
-
summary: {
|
|
412
|
-
type: "object",
|
|
413
|
-
estimatedBytes: estimateValueBytes(value),
|
|
414
|
-
keyCount: keys.length,
|
|
415
|
-
preservedCount,
|
|
416
|
-
droppedCount: keys.length - preservedCount,
|
|
417
|
-
keysPreview: previewKeys,
|
|
418
|
-
omitted: true,
|
|
419
|
-
},
|
|
420
|
-
};
|
|
421
|
-
}
|
|
422
|
-
|
|
423
|
-
function estimateValueBytes(value: unknown, depth = 0): number {
|
|
424
|
-
if (value === null || value === undefined) return 0;
|
|
425
|
-
if (typeof value === "string") return byteLength(value);
|
|
426
|
-
if (typeof value === "number" || typeof value === "boolean" || typeof value === "bigint") return byteLength(String(value));
|
|
427
|
-
const record = asRecord(value);
|
|
428
|
-
if (!record || depth >= 2) return 0;
|
|
429
|
-
const values = Array.isArray(value) ? value.slice(0, KEY_PREVIEW_LIMIT) : Object.values(record).slice(0, KEY_PREVIEW_LIMIT);
|
|
430
|
-
return values.reduce((total, item) => total + estimateValueBytes(item, depth + 1), 0);
|
|
431
|
-
}
|
|
432
|
-
|
|
433
|
-
function truncateKey(key: string): string {
|
|
434
|
-
if (byteLength(key) <= KEY_MAX_BYTES) return key;
|
|
435
|
-
const suffix = "…";
|
|
436
|
-
return `${truncateStringToBytes(key, KEY_MAX_BYTES - byteLength(suffix))}${suffix}`;
|
|
437
|
-
}
|
|
438
|
-
|
|
439
|
-
function uniqueBoundedKeys(keys: string[]): string[] {
|
|
440
|
-
const used = new Set<string>();
|
|
441
|
-
return keys.map((key) => {
|
|
442
|
-
let candidate = truncateKey(key);
|
|
443
|
-
let ordinal = 2;
|
|
444
|
-
while (used.has(candidate)) {
|
|
445
|
-
const suffix = `~${ordinal}`;
|
|
446
|
-
candidate = `${truncateStringToBytes(key, KEY_MAX_BYTES - byteLength(suffix))}${suffix}`;
|
|
447
|
-
ordinal += 1;
|
|
448
|
-
}
|
|
449
|
-
used.add(candidate);
|
|
450
|
-
return candidate;
|
|
451
|
-
});
|
|
452
|
-
}
|
|
453
|
-
|
|
454
|
-
function serializedObjectEntryBytes(key: string, value: unknown, hasPrevious: boolean): number {
|
|
455
|
-
const serialized = safeStringify({ [key]: value });
|
|
456
|
-
return Math.max(0, byteLength(serialized) - byteLength("{}")) + (hasPrevious ? 1 : 0);
|
|
457
|
-
}
|
|
458
|
-
|
|
459
|
-
async function saveArtifact(kind: string, text: string): Promise<{ path?: string; error?: string }> {
|
|
460
|
-
try {
|
|
461
|
-
const dir = await mkdtemp(join(tmpdir(), "pi-mcp-output-"));
|
|
462
|
-
const path = join(dir, `${kind}-${randomBytes(4).toString("hex")}.txt`);
|
|
463
|
-
await writeFile(path, text, { encoding: "utf8", mode: 0o600 });
|
|
464
|
-
return { path };
|
|
465
|
-
} catch (error) {
|
|
466
|
-
return { error: error instanceof Error ? error.message : String(error) };
|
|
467
|
-
}
|
|
468
|
-
}
|
|
469
|
-
|
|
470
|
-
async function discardArtifact(path: string): Promise<void> {
|
|
471
|
-
try {
|
|
472
|
-
await rm(dirname(path), { recursive: true, force: true });
|
|
473
|
-
} catch {
|
|
474
|
-
// Cleanup cannot increase the returned details payload.
|
|
475
|
-
}
|
|
476
|
-
}
|
|
477
|
-
|
|
478
|
-
function asRecord(value: unknown): Recordish | undefined {
|
|
479
|
-
return typeof value === "object" && value !== null ? value as Recordish : undefined;
|
|
480
|
-
}
|
|
481
|
-
|
|
482
|
-
function safeStringify(value: unknown): string {
|
|
483
|
-
try {
|
|
484
|
-
// The output guard measures and spills raw MCP results; it does not render this JSON for the model.
|
|
485
|
-
return JSON.stringify(value);
|
|
486
|
-
} catch {
|
|
487
|
-
return String(value);
|
|
488
|
-
}
|
|
489
|
-
}
|
|
490
|
-
|
|
491
|
-
function textStats(text: string): { bytes: number; lines: number } {
|
|
492
|
-
return { bytes: byteLength(text), lines: text.length === 0 ? 0 : text.split("\n").length };
|
|
493
|
-
}
|
|
494
|
-
|
|
495
|
-
function byteLength(text: string): number {
|
|
496
|
-
return Buffer.byteLength(text, "utf8");
|
|
497
|
-
}
|
|
498
|
-
|
|
499
|
-
function positiveInt(value: unknown): number | undefined {
|
|
500
|
-
if (typeof value !== "number" || !Number.isFinite(value)) return undefined;
|
|
501
|
-
const integer = Math.floor(value);
|
|
502
|
-
return integer > 0 ? integer : undefined;
|
|
503
|
-
}
|
|
504
|
-
|
|
505
|
-
function envKillSwitch(name: string): boolean | undefined {
|
|
506
|
-
const value = process.env[name]?.trim().toLowerCase();
|
|
507
|
-
if (!value) return undefined;
|
|
508
|
-
if (["0", "false", "no", "off"].includes(value)) return false;
|
|
509
|
-
if (["1", "true", "yes", "on"].includes(value)) return true;
|
|
510
|
-
return undefined;
|
|
511
|
-
}
|
|
@@ -1,104 +0,0 @@
|
|
|
1
|
-
import { DynamicBorder, type Theme } from "@earendil-works/pi-coding-agent";
|
|
2
|
-
import { truncateToWidth, visibleWidth, type Component } from "@earendil-works/pi-tui";
|
|
3
|
-
|
|
4
|
-
export class McpPanelFrame implements Component {
|
|
5
|
-
private readonly border: DynamicBorder;
|
|
6
|
-
|
|
7
|
-
constructor(
|
|
8
|
-
private readonly theme: McpPanelTheme,
|
|
9
|
-
private readonly left: string,
|
|
10
|
-
private readonly right: string,
|
|
11
|
-
private readonly title?: string,
|
|
12
|
-
) {
|
|
13
|
-
this.border = new DynamicBorder((text: string) => this.theme.border(text));
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
render(width: number): string[] {
|
|
17
|
-
if (width <= 0) return [];
|
|
18
|
-
if (width === 1) return [truncateToWidth(this.theme.border(this.left), width, "", false)];
|
|
19
|
-
|
|
20
|
-
const innerWidth = width - 2;
|
|
21
|
-
if (this.title !== undefined) {
|
|
22
|
-
const titleText = truncateToWidth(` ${this.title} `, innerWidth, "", false);
|
|
23
|
-
const borderLength = Math.max(0, innerWidth - visibleWidth(titleText));
|
|
24
|
-
const leftLength = Math.floor(borderLength / 2);
|
|
25
|
-
const rightLength = borderLength - leftLength;
|
|
26
|
-
return [
|
|
27
|
-
this.theme.border(this.left) +
|
|
28
|
-
this.renderBorderSegment(leftLength) +
|
|
29
|
-
this.theme.title(titleText) +
|
|
30
|
-
this.renderBorderSegment(rightLength) +
|
|
31
|
-
this.theme.border(this.right),
|
|
32
|
-
];
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
return [
|
|
36
|
-
this.theme.border(this.left) +
|
|
37
|
-
this.renderBorderSegment(innerWidth) +
|
|
38
|
-
this.theme.border(this.right),
|
|
39
|
-
];
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
invalidate(): void {
|
|
43
|
-
this.border.invalidate();
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
private renderBorderSegment(width: number): string {
|
|
47
|
-
if (width <= 0) return "";
|
|
48
|
-
return this.border.render(width)[0] ?? "";
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
export interface McpPanelTheme {
|
|
53
|
-
border(text: string): string;
|
|
54
|
-
title(text: string): string;
|
|
55
|
-
selected(text: string): string;
|
|
56
|
-
direct(text: string): string;
|
|
57
|
-
needsAuth(text: string): string;
|
|
58
|
-
placeholder(text: string): string;
|
|
59
|
-
description(text: string): string;
|
|
60
|
-
hint(text: string): string;
|
|
61
|
-
confirm(text: string): string;
|
|
62
|
-
cancel(text: string): string;
|
|
63
|
-
bold(text: string): string;
|
|
64
|
-
italic(text: string): string;
|
|
65
|
-
inverse(text: string): string;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
const identity = (text: string): string => text;
|
|
69
|
-
|
|
70
|
-
const PLAIN_THEME: McpPanelTheme = {
|
|
71
|
-
border: identity,
|
|
72
|
-
title: identity,
|
|
73
|
-
selected: identity,
|
|
74
|
-
direct: identity,
|
|
75
|
-
needsAuth: identity,
|
|
76
|
-
placeholder: identity,
|
|
77
|
-
description: identity,
|
|
78
|
-
hint: identity,
|
|
79
|
-
confirm: identity,
|
|
80
|
-
cancel: identity,
|
|
81
|
-
bold: identity,
|
|
82
|
-
italic: identity,
|
|
83
|
-
inverse: identity,
|
|
84
|
-
};
|
|
85
|
-
|
|
86
|
-
export function createMcpPanelTheme(theme?: Theme): McpPanelTheme {
|
|
87
|
-
if (!theme) return PLAIN_THEME;
|
|
88
|
-
|
|
89
|
-
return {
|
|
90
|
-
border: (text) => theme.fg("border", text),
|
|
91
|
-
title: (text) => theme.fg("accent", text),
|
|
92
|
-
selected: (text) => theme.fg("accent", text),
|
|
93
|
-
direct: (text) => theme.fg("success", text),
|
|
94
|
-
needsAuth: (text) => theme.fg("warning", text),
|
|
95
|
-
placeholder: (text) => theme.fg("dim", text),
|
|
96
|
-
description: (text) => theme.fg("muted", text),
|
|
97
|
-
hint: (text) => theme.fg("dim", text),
|
|
98
|
-
confirm: (text) => theme.fg("success", text),
|
|
99
|
-
cancel: (text) => theme.fg("error", text),
|
|
100
|
-
bold: (text) => theme.bold(text),
|
|
101
|
-
italic: (text) => theme.italic(text),
|
|
102
|
-
inverse: (text) => theme.inverse(text),
|
|
103
|
-
};
|
|
104
|
-
}
|