@skydiveai/pi-extensions 0.1.258-beta.12 → 0.1.258-beta.120
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/index.mjs +158 -45
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -265,7 +265,7 @@ function createHealthHandler({ metadata }) {
|
|
|
265
265
|
const CAPABILITY_SOUL_NUDGE = "New capability gained — once the current task is done, if this changes what you can do for the user, record it in `soul.md` so it carries into future conversations rather than being rediscovered from scratch (then commit and push).";
|
|
266
266
|
//#endregion
|
|
267
267
|
//#region src/extensions/tool-call-summary.ts
|
|
268
|
-
const log$
|
|
268
|
+
const log$20 = logger.child({ module: "tool-call-summary-extension" });
|
|
269
269
|
/**
|
|
270
270
|
* The injected parameter name. Deliberately markdown-safe: the previous wire
|
|
271
271
|
* name (`__skydive_summary__`, now `TOOL_CALL_SUMMARY_FIELD_LEGACY`) carried
|
|
@@ -499,7 +499,7 @@ function withSummaryStrippedBeforeValidation(tool) {
|
|
|
499
499
|
try {
|
|
500
500
|
stripped = withoutInjectedSummary(args);
|
|
501
501
|
} catch (err) {
|
|
502
|
-
log$
|
|
502
|
+
log$20.error({
|
|
503
503
|
err,
|
|
504
504
|
event: "tool_call_summary_prepare_strip_failed",
|
|
505
505
|
toolName: tool.name
|
|
@@ -530,7 +530,7 @@ function stripInjectedSummary(pi, toolName, input) {
|
|
|
530
530
|
if (toolDeclaresSummaryParam(pi, toolName)) return;
|
|
531
531
|
for (const field of TOOL_CALL_SUMMARY_FIELDS) delete input[field];
|
|
532
532
|
} catch (err) {
|
|
533
|
-
log$
|
|
533
|
+
log$20.error({
|
|
534
534
|
err,
|
|
535
535
|
event: "tool_call_summary_strip_failed",
|
|
536
536
|
toolName
|
|
@@ -546,7 +546,7 @@ function buildInjectedPayload(pi, payload) {
|
|
|
546
546
|
try {
|
|
547
547
|
return injectToolCallSummary(payload, getStrictToolNames(pi));
|
|
548
548
|
} catch (err) {
|
|
549
|
-
log$
|
|
549
|
+
log$20.error({
|
|
550
550
|
err,
|
|
551
551
|
event: "tool_call_summary_injection_failed"
|
|
552
552
|
}, "tool_call_summary injection failed; passing payload through unchanged");
|
|
@@ -565,7 +565,7 @@ const toolCallSummaryExtension = (pi) => {
|
|
|
565
565
|
pi.registerTool(withSummaryStrippedBeforeValidation(createEditToolDefinition(ctx.cwd)));
|
|
566
566
|
editOverrideRegistered = true;
|
|
567
567
|
} catch (err) {
|
|
568
|
-
log$
|
|
568
|
+
log$20.error({
|
|
569
569
|
err,
|
|
570
570
|
event: "tool_call_summary_edit_override_failed"
|
|
571
571
|
}, "failed to register summary-tolerant edit tool; built-in remains active");
|
|
@@ -596,7 +596,7 @@ const toolCallSummaryExtension = (pi) => {
|
|
|
596
596
|
* or `ToolDefinition[]`. Files starting with `_` or `.` are skipped, so
|
|
597
597
|
* `tools/_example.ts` documents the shape without registering.
|
|
598
598
|
*/
|
|
599
|
-
const log$
|
|
599
|
+
const log$19 = logger.child({ module: "local-tools-extension" });
|
|
600
600
|
const TOOLS_DIRNAME = "tools";
|
|
601
601
|
const fileState = /* @__PURE__ */ new Map();
|
|
602
602
|
let pendingLocalToolsUpdate = null;
|
|
@@ -740,7 +740,7 @@ async function reconcileAndQueue({ pi, dir, reason }) {
|
|
|
740
740
|
dir
|
|
741
741
|
});
|
|
742
742
|
if (reason !== "session_start" && summaryHasChanges$1(summary)) pendingLocalToolsUpdate = summary;
|
|
743
|
-
log$
|
|
743
|
+
log$19.info({
|
|
744
744
|
event: "local_tools_reconcile",
|
|
745
745
|
reason,
|
|
746
746
|
total_tools: summary.totalTools,
|
|
@@ -762,7 +762,7 @@ const localToolsExtension = (pi) => {
|
|
|
762
762
|
reason: "session_start"
|
|
763
763
|
});
|
|
764
764
|
} catch (err) {
|
|
765
|
-
log$
|
|
765
|
+
log$19.error({
|
|
766
766
|
err,
|
|
767
767
|
event: "local_tools_reconcile_failed"
|
|
768
768
|
}, "local tools reconcile failed");
|
|
@@ -774,7 +774,7 @@ const localToolsExtension = (pi) => {
|
|
|
774
774
|
try {
|
|
775
775
|
current = await listToolFiles(dir);
|
|
776
776
|
} catch (err) {
|
|
777
|
-
log$
|
|
777
|
+
log$19.warn({
|
|
778
778
|
err,
|
|
779
779
|
event: "local_tools_listing_failed"
|
|
780
780
|
}, "tools/ listing failed");
|
|
@@ -796,7 +796,7 @@ const localToolsExtension = (pi) => {
|
|
|
796
796
|
reason: "auto_reload"
|
|
797
797
|
});
|
|
798
798
|
} catch (err) {
|
|
799
|
-
log$
|
|
799
|
+
log$19.error({
|
|
800
800
|
err,
|
|
801
801
|
event: "local_tools_auto_reload_failed"
|
|
802
802
|
}, "auto-reload after tools/ change failed");
|
|
@@ -1288,17 +1288,23 @@ function legacyMcpToolName({ serverName, toolName }) {
|
|
|
1288
1288
|
* a small, focused unit-test surface (typebox validation, error formatting).
|
|
1289
1289
|
*/
|
|
1290
1290
|
const MCP_CONFIG_FILENAME = "mcp.config.json";
|
|
1291
|
+
const ToolSelectionSchema = {
|
|
1292
|
+
tools: Type.Optional(Type.Union([Type.Array(Type.String()), Type.Null()])),
|
|
1293
|
+
excludeTools: Type.Optional(Type.Union([Type.Array(Type.String()), Type.Null()]))
|
|
1294
|
+
};
|
|
1291
1295
|
const StdioServerSchema = Type.Object({
|
|
1292
1296
|
transport: Type.Literal("stdio"),
|
|
1293
1297
|
command: Type.String(),
|
|
1294
1298
|
args: Type.Array(Type.String()),
|
|
1295
1299
|
env: Type.Optional(Type.Union([Type.Record(Type.String(), Type.String()), Type.Null()])),
|
|
1296
|
-
cwd: Type.Optional(Type.Union([Type.String(), Type.Null()]))
|
|
1300
|
+
cwd: Type.Optional(Type.Union([Type.String(), Type.Null()])),
|
|
1301
|
+
...ToolSelectionSchema
|
|
1297
1302
|
});
|
|
1298
1303
|
const HttpServerSchema = Type.Object({
|
|
1299
1304
|
transport: Type.Literal("http"),
|
|
1300
1305
|
url: Type.String(),
|
|
1301
|
-
headers: Type.Optional(Type.Union([Type.Record(Type.String(), Type.String()), Type.Null()]))
|
|
1306
|
+
headers: Type.Optional(Type.Union([Type.Record(Type.String(), Type.String()), Type.Null()])),
|
|
1307
|
+
...ToolSelectionSchema
|
|
1302
1308
|
});
|
|
1303
1309
|
const ServerSchema = Type.Union([StdioServerSchema, HttpServerSchema]);
|
|
1304
1310
|
const McpConfigSchema = Type.Object({ servers: Type.Record(Type.String(), ServerSchema) });
|
|
@@ -1325,11 +1331,15 @@ async function loadMcpConfig(path) {
|
|
|
1325
1331
|
for (const [id, server] of Object.entries(json.servers)) if (server.transport === "stdio") servers[id] = {
|
|
1326
1332
|
...server,
|
|
1327
1333
|
env: server.env ?? null,
|
|
1328
|
-
cwd: server.cwd ?? null
|
|
1334
|
+
cwd: server.cwd ?? null,
|
|
1335
|
+
tools: server.tools ?? null,
|
|
1336
|
+
excludeTools: server.excludeTools ?? null
|
|
1329
1337
|
};
|
|
1330
1338
|
else servers[id] = {
|
|
1331
1339
|
...server,
|
|
1332
|
-
headers: server.headers ?? null
|
|
1340
|
+
headers: server.headers ?? null,
|
|
1341
|
+
tools: server.tools ?? null,
|
|
1342
|
+
excludeTools: server.excludeTools ?? null
|
|
1333
1343
|
};
|
|
1334
1344
|
return { servers };
|
|
1335
1345
|
}
|
|
@@ -1490,7 +1500,55 @@ function toProviderSafeSchema(inputSchema) {
|
|
|
1490
1500
|
* Clients are keyed by JSON-stringified config and reused across
|
|
1491
1501
|
* reloads — only changed configs reconnect.
|
|
1492
1502
|
*/
|
|
1493
|
-
const log$
|
|
1503
|
+
const log$18 = logger.child({ module: "mcp-extension" });
|
|
1504
|
+
function resolveToolSelection(server) {
|
|
1505
|
+
return {
|
|
1506
|
+
allow: server?.tools?.length ? new Set(server.tools) : null,
|
|
1507
|
+
deny: new Set(server?.excludeTools ?? [])
|
|
1508
|
+
};
|
|
1509
|
+
}
|
|
1510
|
+
/**
|
|
1511
|
+
* Connection identity of a server config: everything except the per-server
|
|
1512
|
+
* tool selection. `tools`/`excludeTools` only filter the advertised list at
|
|
1513
|
+
* registration time, so an edit to them must re-register tools, not tear
|
|
1514
|
+
* down and re-handshake the client (kills the stdio process, re-runs the
|
|
1515
|
+
* http OAuth/handshake for an identical connection).
|
|
1516
|
+
*/
|
|
1517
|
+
function connectionKey(serverConfig) {
|
|
1518
|
+
const { tools: _tools, excludeTools: _excludeTools, ...connection } = serverConfig;
|
|
1519
|
+
return JSON.stringify(connection);
|
|
1520
|
+
}
|
|
1521
|
+
/**
|
|
1522
|
+
* Apply a server's configured tool selection to its advertised list.
|
|
1523
|
+
* Runs BEFORE the budget allocator: a filtered set is what competes for
|
|
1524
|
+
* the per-server / total / token caps, so an allowlisted tool wins the
|
|
1525
|
+
* budget contest instead of being truncated behind tools the config
|
|
1526
|
+
* never asked for. Intentional exclusions are NOT budget drops — they
|
|
1527
|
+
* don't count toward `droppedTools` and produce no truncation warning.
|
|
1528
|
+
*/
|
|
1529
|
+
function applyToolSelection(tools, selection) {
|
|
1530
|
+
if (selection.allow === null && selection.deny.size === 0) return {
|
|
1531
|
+
list: tools,
|
|
1532
|
+
filteredOut: 0,
|
|
1533
|
+
unknownTools: [],
|
|
1534
|
+
unknownExcludeTools: []
|
|
1535
|
+
};
|
|
1536
|
+
const kept = [];
|
|
1537
|
+
for (const tool of tools) {
|
|
1538
|
+
if (selection.allow !== null && !selection.allow.has(tool.name)) continue;
|
|
1539
|
+
if (selection.deny.has(tool.name)) continue;
|
|
1540
|
+
kept.push(tool);
|
|
1541
|
+
}
|
|
1542
|
+
const notAdvertised = (names) => [...names].filter((name) => !tools.some((tool) => tool.name === name));
|
|
1543
|
+
const unknownTools = selection.allow === null ? [] : notAdvertised(selection.allow);
|
|
1544
|
+
const unknownExcludeTools = selection.deny.size === 0 ? [] : notAdvertised(selection.deny);
|
|
1545
|
+
return {
|
|
1546
|
+
list: kept,
|
|
1547
|
+
filteredOut: tools.length - kept.length,
|
|
1548
|
+
unknownTools,
|
|
1549
|
+
unknownExcludeTools
|
|
1550
|
+
};
|
|
1551
|
+
}
|
|
1494
1552
|
async function closeConnected(connected) {
|
|
1495
1553
|
try {
|
|
1496
1554
|
await connected.client.close();
|
|
@@ -1640,7 +1698,7 @@ function providerSafeTools({ serverId, tools }) {
|
|
|
1640
1698
|
for (const tool of tools) {
|
|
1641
1699
|
const result = toProviderSafeSchema(tool.inputSchema);
|
|
1642
1700
|
if (!result.ok) {
|
|
1643
|
-
log$
|
|
1701
|
+
log$18.warn({
|
|
1644
1702
|
event: "mcp_tool_schema_unsupported",
|
|
1645
1703
|
server_id: serverId,
|
|
1646
1704
|
tool_name: tool.name,
|
|
@@ -1797,9 +1855,11 @@ var McpExtension = class {
|
|
|
1797
1855
|
else if (outcome.change === "refreshed") summary.refreshed.push(id);
|
|
1798
1856
|
}
|
|
1799
1857
|
const alloc = outcomes.filter((o) => Boolean(o.tools)).map((o) => {
|
|
1858
|
+
const selection = resolveToolSelection(config.servers[o.id]);
|
|
1859
|
+
const picked = applyToolSelection(o.tools.list, selection);
|
|
1800
1860
|
const { tools: list, skipped } = providerSafeTools({
|
|
1801
1861
|
serverId: o.id,
|
|
1802
|
-
tools:
|
|
1862
|
+
tools: picked.list
|
|
1803
1863
|
});
|
|
1804
1864
|
return {
|
|
1805
1865
|
id: o.id,
|
|
@@ -1807,6 +1867,9 @@ var McpExtension = class {
|
|
|
1807
1867
|
list,
|
|
1808
1868
|
advertised: o.tools.list.length,
|
|
1809
1869
|
skipped,
|
|
1870
|
+
filtered: picked.filteredOut,
|
|
1871
|
+
unknownTools: picked.unknownTools,
|
|
1872
|
+
unknownExcludeTools: picked.unknownExcludeTools,
|
|
1810
1873
|
room: Math.min(list.length, limits.maxToolsPerServer),
|
|
1811
1874
|
registered: 0,
|
|
1812
1875
|
serverTokens: 0,
|
|
@@ -1850,7 +1913,10 @@ var McpExtension = class {
|
|
|
1850
1913
|
registered: s.registered,
|
|
1851
1914
|
tokens: s.serverTokens,
|
|
1852
1915
|
droppedReason,
|
|
1853
|
-
skipped: s.skipped
|
|
1916
|
+
skipped: s.skipped,
|
|
1917
|
+
filtered: s.filtered,
|
|
1918
|
+
unknownTools: s.unknownTools,
|
|
1919
|
+
unknownExcludeTools: s.unknownExcludeTools
|
|
1854
1920
|
};
|
|
1855
1921
|
}
|
|
1856
1922
|
return summary;
|
|
@@ -1862,10 +1928,10 @@ var McpExtension = class {
|
|
|
1862
1928
|
* map/summary, so callers can run these concurrently.
|
|
1863
1929
|
*/
|
|
1864
1930
|
async reconcileServer({ id, serverConfig, existing, connectTimeoutMs }) {
|
|
1865
|
-
const configKey =
|
|
1931
|
+
const configKey = connectionKey(serverConfig);
|
|
1866
1932
|
let action;
|
|
1867
1933
|
let connected;
|
|
1868
|
-
if (existing && existing.
|
|
1934
|
+
if (existing && existing.connectionKey === configKey) {
|
|
1869
1935
|
connected = existing;
|
|
1870
1936
|
action = "reused";
|
|
1871
1937
|
} else {
|
|
@@ -1891,7 +1957,7 @@ var McpExtension = class {
|
|
|
1891
1957
|
id,
|
|
1892
1958
|
store: {
|
|
1893
1959
|
client: result.client,
|
|
1894
|
-
configKey,
|
|
1960
|
+
connectionKey: configKey,
|
|
1895
1961
|
status: "pending_auth",
|
|
1896
1962
|
stderrBuffer: result.stderrBuffer,
|
|
1897
1963
|
cliHint: result.cliHint
|
|
@@ -1909,7 +1975,7 @@ var McpExtension = class {
|
|
|
1909
1975
|
id,
|
|
1910
1976
|
store: {
|
|
1911
1977
|
client: result.client,
|
|
1912
|
-
configKey,
|
|
1978
|
+
connectionKey: configKey,
|
|
1913
1979
|
status: "timeout",
|
|
1914
1980
|
stderrBuffer: result.stderrBuffer,
|
|
1915
1981
|
cliHint: null
|
|
@@ -1925,7 +1991,7 @@ var McpExtension = class {
|
|
|
1925
1991
|
};
|
|
1926
1992
|
connected = {
|
|
1927
1993
|
client: result.client,
|
|
1928
|
-
configKey,
|
|
1994
|
+
connectionKey: configKey,
|
|
1929
1995
|
status: "connected",
|
|
1930
1996
|
stderrBuffer: result.stderr,
|
|
1931
1997
|
cliHint: null
|
|
@@ -1939,7 +2005,7 @@ var McpExtension = class {
|
|
|
1939
2005
|
id,
|
|
1940
2006
|
store: {
|
|
1941
2007
|
client: retry.client,
|
|
1942
|
-
configKey,
|
|
2008
|
+
connectionKey: configKey,
|
|
1943
2009
|
status: "pending_auth",
|
|
1944
2010
|
stderrBuffer: null,
|
|
1945
2011
|
cliHint: retry.cliHint
|
|
@@ -1957,7 +2023,7 @@ var McpExtension = class {
|
|
|
1957
2023
|
id,
|
|
1958
2024
|
store: {
|
|
1959
2025
|
client: retry.client,
|
|
1960
|
-
configKey,
|
|
2026
|
+
connectionKey: configKey,
|
|
1961
2027
|
status: "timeout",
|
|
1962
2028
|
stderrBuffer: retry.stderrBuffer,
|
|
1963
2029
|
cliHint: null
|
|
@@ -1985,7 +2051,7 @@ var McpExtension = class {
|
|
|
1985
2051
|
};
|
|
1986
2052
|
connected = {
|
|
1987
2053
|
client: retry.client,
|
|
1988
|
-
configKey,
|
|
2054
|
+
connectionKey: configKey,
|
|
1989
2055
|
status: "connected",
|
|
1990
2056
|
stderrBuffer: retry.stderr,
|
|
1991
2057
|
cliHint: null
|
|
@@ -2017,7 +2083,7 @@ var McpExtension = class {
|
|
|
2017
2083
|
id,
|
|
2018
2084
|
store: {
|
|
2019
2085
|
client: retry.client,
|
|
2020
|
-
configKey,
|
|
2086
|
+
connectionKey: configKey,
|
|
2021
2087
|
status: "pending_auth",
|
|
2022
2088
|
stderrBuffer: null,
|
|
2023
2089
|
cliHint: retry.cliHint
|
|
@@ -2049,7 +2115,7 @@ var McpExtension = class {
|
|
|
2049
2115
|
if (retry.status === "connected") {
|
|
2050
2116
|
connected = {
|
|
2051
2117
|
client: retry.client,
|
|
2052
|
-
configKey,
|
|
2118
|
+
connectionKey: configKey,
|
|
2053
2119
|
status: "connected",
|
|
2054
2120
|
stderrBuffer: retry.stderr,
|
|
2055
2121
|
cliHint: null
|
|
@@ -2106,7 +2172,7 @@ var McpExtension = class {
|
|
|
2106
2172
|
});
|
|
2107
2173
|
this.lastConfigMtimeMs = await readConfigMtimeMs(configPath);
|
|
2108
2174
|
if (reason !== "session_start" && summaryHasChanges(summary)) this.pendingMcpUpdate = summary;
|
|
2109
|
-
log$
|
|
2175
|
+
log$18.info({
|
|
2110
2176
|
event: "mcp_reconcile",
|
|
2111
2177
|
reason,
|
|
2112
2178
|
total_tools: summary.totalTools,
|
|
@@ -2133,7 +2199,7 @@ var McpExtension = class {
|
|
|
2133
2199
|
contextWindow: ctx.model?.contextWindow
|
|
2134
2200
|
});
|
|
2135
2201
|
} catch (err) {
|
|
2136
|
-
log$
|
|
2202
|
+
log$18.error({
|
|
2137
2203
|
err,
|
|
2138
2204
|
event: "mcp_reconcile_failed"
|
|
2139
2205
|
}, "MCP reconcile failed");
|
|
@@ -2147,7 +2213,7 @@ var McpExtension = class {
|
|
|
2147
2213
|
for (const legacy of renamed) {
|
|
2148
2214
|
if (this.loggedLegacyToolNames.has(legacy)) continue;
|
|
2149
2215
|
this.loggedLegacyToolNames.add(legacy);
|
|
2150
|
-
log$
|
|
2216
|
+
log$18.info({
|
|
2151
2217
|
event: "mcp_tool_legacy_name",
|
|
2152
2218
|
legacy_name: legacy,
|
|
2153
2219
|
tool_name: this.legacyToolNames.get(legacy)
|
|
@@ -2161,7 +2227,7 @@ var McpExtension = class {
|
|
|
2161
2227
|
try {
|
|
2162
2228
|
mtime = await readConfigMtimeMs(configPath);
|
|
2163
2229
|
} catch (err) {
|
|
2164
|
-
log$
|
|
2230
|
+
log$18.warn({
|
|
2165
2231
|
err,
|
|
2166
2232
|
event: "mcp_mtime_check_failed"
|
|
2167
2233
|
}, "mtime check on mcp.config.json failed");
|
|
@@ -2176,7 +2242,7 @@ var McpExtension = class {
|
|
|
2176
2242
|
contextWindow: ctx.model?.contextWindow
|
|
2177
2243
|
});
|
|
2178
2244
|
} catch (err) {
|
|
2179
|
-
log$
|
|
2245
|
+
log$18.error({
|
|
2180
2246
|
err,
|
|
2181
2247
|
event: "mcp_auto_reload_failed"
|
|
2182
2248
|
}, "auto-reload after mcp.config.json change failed");
|
|
@@ -2304,11 +2370,27 @@ function skippedSchemaLines(summary) {
|
|
|
2304
2370
|
for (const [id, count] of entries) for (const { name, reason } of count.skipped) lines.push(` - ${id}/${name}: ${reason}`);
|
|
2305
2371
|
return lines;
|
|
2306
2372
|
}
|
|
2373
|
+
/**
|
|
2374
|
+
* Lines naming allowlist entries in mcp.config.json that matched no tool the
|
|
2375
|
+
* server advertises — almost always a typo, and silent otherwise: the agent
|
|
2376
|
+
* just sees a narrower tool set and can't tell the config is wrong.
|
|
2377
|
+
*/
|
|
2378
|
+
function unknownToolLines(summary) {
|
|
2379
|
+
const entries = Object.entries(summary.toolCounts).filter(([, count]) => count.unknownTools.length > 0 || count.unknownExcludeTools.length > 0);
|
|
2380
|
+
if (entries.length === 0) return [];
|
|
2381
|
+
const lines = [];
|
|
2382
|
+
for (const [id, count] of entries) {
|
|
2383
|
+
if (count.unknownTools.length > 0) lines.push(` NOTE: ${id}.tools in mcp.config.json names tool(s) the server does not advertise: ${count.unknownTools.join(", ")}.`);
|
|
2384
|
+
if (count.unknownExcludeTools.length > 0) lines.push(` NOTE: ${id}.excludeTools in mcp.config.json names tool(s) the server does not advertise: ${count.unknownExcludeTools.join(", ")}.`);
|
|
2385
|
+
}
|
|
2386
|
+
return lines;
|
|
2387
|
+
}
|
|
2307
2388
|
function summaryText(summary) {
|
|
2308
2389
|
const lines = [];
|
|
2309
2390
|
lines.push(`MCP reconcile complete: ${summary.totalTools} tool(s) live.`);
|
|
2310
2391
|
lines.push(...truncationLines(summary));
|
|
2311
2392
|
lines.push(...skippedSchemaLines(summary));
|
|
2393
|
+
lines.push(...unknownToolLines(summary));
|
|
2312
2394
|
if (summary.added.length > 0) lines.push(` Added: ${summary.added.join(", ")}`);
|
|
2313
2395
|
if (summary.refreshed.length > 0) lines.push(` Refreshed: ${summary.refreshed.join(", ")}`);
|
|
2314
2396
|
if (summary.removed.length > 0) lines.push(` Removed: ${summary.removed.join(", ")}`);
|
|
@@ -2526,11 +2608,21 @@ const assistantSchema = z.object({
|
|
|
2526
2608
|
id: z.string(),
|
|
2527
2609
|
model: z.string()
|
|
2528
2610
|
});
|
|
2611
|
+
const sdkToolResultSchema = z.object({ content: z.array(z.object({ type: z.literal("tool_result") })).min(1) });
|
|
2529
2612
|
function assistantMessage(entry) {
|
|
2530
2613
|
if (entry.type !== "assistant") return null;
|
|
2531
2614
|
const parsed = assistantSchema.safeParse(entry.message);
|
|
2532
2615
|
return parsed.success ? parsed.data : null;
|
|
2533
2616
|
}
|
|
2617
|
+
/**
|
|
2618
|
+
* Claude Code answers some tool_use blocks itself mid-stream (e.g. "No such
|
|
2619
|
+
* tool available") and persists that result between the response's blocks.
|
|
2620
|
+
* Pi owns tool execution and replays its own results, so these never belong
|
|
2621
|
+
* in the accepted prefix.
|
|
2622
|
+
*/
|
|
2623
|
+
function isSdkToolResult(entry) {
|
|
2624
|
+
return entry.type === "user" && sdkToolResultSchema.safeParse(entry.message).success;
|
|
2625
|
+
}
|
|
2534
2626
|
function contentHash(message) {
|
|
2535
2627
|
return createHash("sha256").update(JSON.stringify(message.content)).digest("hex");
|
|
2536
2628
|
}
|
|
@@ -2588,7 +2680,7 @@ async function captureSeatTranscript({ configDir, sessionId, boundaryId, lastAss
|
|
|
2588
2680
|
const entry = byId.get(id);
|
|
2589
2681
|
if (!entry) break;
|
|
2590
2682
|
const assistant = assistantMessage(entry);
|
|
2591
|
-
if (!assistant || assistant.model === "<synthetic>" || acceptedIds.has(assistant.id)) entries.push(entry);
|
|
2683
|
+
if (!isSdkToolResult(entry) && (!assistant || assistant.model === "<synthetic>" || acceptedIds.has(assistant.id))) entries.push(entry);
|
|
2592
2684
|
id = entry.parentUuid;
|
|
2593
2685
|
}
|
|
2594
2686
|
if (id === boundaryId && entries.length) return entries.reverse();
|
|
@@ -2866,7 +2958,7 @@ const SEAT_QUERY_TIMEOUTS = {
|
|
|
2866
2958
|
preparation: 3e4
|
|
2867
2959
|
};
|
|
2868
2960
|
const PI_TOOLS_SERVER = "custom-tools";
|
|
2869
|
-
const log$
|
|
2961
|
+
const log$17 = logger.child({ module: "claude-seat-sdk" });
|
|
2870
2962
|
/** Preserve existing MCP names; only ordinary Pi tools need a server prefix. */
|
|
2871
2963
|
function toolBindings(context) {
|
|
2872
2964
|
const parsed = (context.tools ?? []).map((definition) => {
|
|
@@ -2924,7 +3016,7 @@ var SeatQuery = class {
|
|
|
2924
3016
|
this.deadline = setTimeout(() => {
|
|
2925
3017
|
const err = /* @__PURE__ */ new Error(`Claude seat SDK timed out waiting for ${phase}`);
|
|
2926
3018
|
err.name = "TimeoutError";
|
|
2927
|
-
log$
|
|
3019
|
+
log$17.error({
|
|
2928
3020
|
err,
|
|
2929
3021
|
phase
|
|
2930
3022
|
}, "Claude seat SDK wait deadline exceeded");
|
|
@@ -3105,7 +3197,7 @@ var SeatQuery = class {
|
|
|
3105
3197
|
}
|
|
3106
3198
|
} catch (err) {
|
|
3107
3199
|
this.failure = err;
|
|
3108
|
-
if (!this.controller.signal.aborted) log$
|
|
3200
|
+
if (!this.controller.signal.aborted) log$17.error({
|
|
3109
3201
|
err,
|
|
3110
3202
|
sessionId
|
|
3111
3203
|
}, "Claude seat SDK query failed");
|
|
@@ -3120,7 +3212,7 @@ var SeatQuery = class {
|
|
|
3120
3212
|
force: true
|
|
3121
3213
|
});
|
|
3122
3214
|
} catch (err) {
|
|
3123
|
-
log$
|
|
3215
|
+
log$17.error({
|
|
3124
3216
|
err,
|
|
3125
3217
|
sessionId
|
|
3126
3218
|
}, "Could not remove Claude seat SDK config");
|
|
@@ -3153,10 +3245,26 @@ function mapUsage(usage) {
|
|
|
3153
3245
|
//#endregion
|
|
3154
3246
|
//#region src/extensions/claude-seat-response.ts
|
|
3155
3247
|
const toolInput = z.record(z.string(), z.unknown());
|
|
3156
|
-
|
|
3157
|
-
|
|
3158
|
-
|
|
3159
|
-
|
|
3248
|
+
const log$16 = logger.child({ module: "claude-seat-sdk" });
|
|
3249
|
+
/**
|
|
3250
|
+
* Claude sometimes calls a tool by the name it had outside the bridge (Pi's
|
|
3251
|
+
* own name from non-seat history, or a bare MCP tool name). Resolve those when
|
|
3252
|
+
* unambiguous; anything else passes through unbound so Pi's executor returns
|
|
3253
|
+
* its normal not-found error result and the model can recover.
|
|
3254
|
+
*/
|
|
3255
|
+
function piToolName({ name, context, sessionId }) {
|
|
3256
|
+
const bindings = toolBindings(context);
|
|
3257
|
+
const exact = bindings.find((binding) => binding.sdkName === name) ?? bindings.find((binding) => binding.definition.name === name);
|
|
3258
|
+
if (exact) return exact.definition.name;
|
|
3259
|
+
const bare = bindings.filter((binding) => binding.toolName === name);
|
|
3260
|
+
const [only] = bare;
|
|
3261
|
+
if (only && bare.length === 1) return only.definition.name;
|
|
3262
|
+
log$16.warn({
|
|
3263
|
+
sessionId,
|
|
3264
|
+
toolName: name,
|
|
3265
|
+
candidates: bare.length
|
|
3266
|
+
}, "Claude requested a tool outside the Pi bridge");
|
|
3267
|
+
return name;
|
|
3160
3268
|
}
|
|
3161
3269
|
/** Commit only the accepted SDK attempt: Pi deltas cannot retract retried output. */
|
|
3162
3270
|
var ModelResponse = class {
|
|
@@ -3164,9 +3272,10 @@ var ModelResponse = class {
|
|
|
3164
3272
|
pending = [];
|
|
3165
3273
|
messageId = null;
|
|
3166
3274
|
stopReason = null;
|
|
3167
|
-
constructor(model, context, events) {
|
|
3275
|
+
constructor(model, context, events, sessionId) {
|
|
3168
3276
|
this.context = context;
|
|
3169
3277
|
this.events = events;
|
|
3278
|
+
this.sessionId = sessionId;
|
|
3170
3279
|
this.message = {
|
|
3171
3280
|
role: "assistant",
|
|
3172
3281
|
content: [],
|
|
@@ -3261,7 +3370,11 @@ var ModelResponse = class {
|
|
|
3261
3370
|
block = {
|
|
3262
3371
|
type: "toolCall",
|
|
3263
3372
|
id: sdkBlock.id,
|
|
3264
|
-
name: piToolName(
|
|
3373
|
+
name: piToolName({
|
|
3374
|
+
name: sdkBlock.name,
|
|
3375
|
+
context: this.context,
|
|
3376
|
+
sessionId: this.sessionId
|
|
3377
|
+
}),
|
|
3265
3378
|
arguments: toolInput.parse(sdkBlock.input)
|
|
3266
3379
|
};
|
|
3267
3380
|
break;
|
|
@@ -3355,7 +3468,7 @@ var SeatSdkBridge = class {
|
|
|
3355
3468
|
}
|
|
3356
3469
|
stream(model, context, options = {}) {
|
|
3357
3470
|
const events = createAssistantMessageEventStream();
|
|
3358
|
-
const response = new ModelResponse(model, context, events);
|
|
3471
|
+
const response = new ModelResponse(model, context, events, this.deps.sessionId);
|
|
3359
3472
|
const signal = AbortSignal.any([this.controller.signal, ...options.signal ? [options.signal] : []]);
|
|
3360
3473
|
(async () => {
|
|
3361
3474
|
let seatQuery = null;
|