@namzu/sdk 71.0.1 → 72.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 +126 -0
- package/dist/agents/QueryAgent.d.ts.map +1 -1
- package/dist/agents/QueryAgent.js +1 -0
- package/dist/agents/QueryAgent.js.map +1 -1
- package/dist/authorization/readonly-commands.d.ts +9 -1
- package/dist/authorization/readonly-commands.d.ts.map +1 -1
- package/dist/authorization/readonly-commands.js +70 -4
- package/dist/authorization/readonly-commands.js.map +1 -1
- package/dist/authorization/shell-paths.d.ts.map +1 -1
- package/dist/authorization/shell-paths.js +68 -16
- package/dist/authorization/shell-paths.js.map +1 -1
- package/dist/compaction/index.d.ts +1 -1
- package/dist/compaction/index.d.ts.map +1 -1
- package/dist/compaction/index.js +1 -1
- package/dist/compaction/index.js.map +1 -1
- package/dist/compaction/manual.d.ts +8 -0
- package/dist/compaction/manual.d.ts.map +1 -1
- package/dist/compaction/manual.js +18 -14
- package/dist/compaction/manual.js.map +1 -1
- package/dist/compaction/model-summary.d.ts +24 -6
- package/dist/compaction/model-summary.d.ts.map +1 -1
- package/dist/compaction/model-summary.js +152 -28
- package/dist/compaction/model-summary.js.map +1 -1
- package/dist/compaction/restore.d.ts +23 -0
- package/dist/compaction/restore.d.ts.map +1 -1
- package/dist/compaction/restore.js +59 -1
- package/dist/compaction/restore.js.map +1 -1
- package/dist/compaction/summary.d.ts +37 -2
- package/dist/compaction/summary.d.ts.map +1 -1
- package/dist/compaction/summary.js +46 -6
- package/dist/compaction/summary.js.map +1 -1
- package/dist/config/registry.d.ts +2 -0
- package/dist/config/registry.d.ts.map +1 -1
- package/dist/config/registry.js +76 -5
- package/dist/config/registry.js.map +1 -1
- package/dist/config/runtime.js +3 -3
- package/dist/config/runtime.js.map +1 -1
- package/dist/connector/index.d.ts +2 -2
- package/dist/connector/index.d.ts.map +1 -1
- package/dist/connector/index.js +1 -1
- package/dist/connector/index.js.map +1 -1
- package/dist/connector/mcp/adapter.d.ts +8 -0
- package/dist/connector/mcp/adapter.d.ts.map +1 -1
- package/dist/connector/mcp/adapter.js +131 -1
- package/dist/connector/mcp/adapter.js.map +1 -1
- package/dist/connector/mcp/client.d.ts +26 -6
- package/dist/connector/mcp/client.d.ts.map +1 -1
- package/dist/connector/mcp/client.js +49 -7
- package/dist/connector/mcp/client.js.map +1 -1
- package/dist/connector/mcp/index.d.ts +2 -0
- package/dist/connector/mcp/index.d.ts.map +1 -1
- package/dist/connector/mcp/index.js +1 -0
- package/dist/connector/mcp/index.js.map +1 -1
- package/dist/connector/mcp/sandbox-stdio.d.ts +67 -0
- package/dist/connector/mcp/sandbox-stdio.d.ts.map +1 -0
- package/dist/connector/mcp/sandbox-stdio.js +178 -0
- package/dist/connector/mcp/sandbox-stdio.js.map +1 -0
- package/dist/connector/mcp/stdio-program.d.ts +34 -0
- package/dist/connector/mcp/stdio-program.d.ts.map +1 -0
- package/dist/connector/mcp/stdio-program.js +47 -0
- package/dist/connector/mcp/stdio-program.js.map +1 -0
- package/dist/connector/mcp/stdio.d.ts +12 -0
- package/dist/connector/mcp/stdio.d.ts.map +1 -1
- package/dist/connector/mcp/stdio.js +54 -7
- package/dist/connector/mcp/stdio.js.map +1 -1
- package/dist/connector/web/guarded-fetch.d.ts +51 -2
- package/dist/connector/web/guarded-fetch.d.ts.map +1 -1
- package/dist/connector/web/guarded-fetch.js +140 -19
- package/dist/connector/web/guarded-fetch.js.map +1 -1
- package/dist/connector/web/index.d.ts +2 -2
- package/dist/connector/web/index.d.ts.map +1 -1
- package/dist/connector/web/index.js +1 -1
- package/dist/connector/web/index.js.map +1 -1
- package/dist/constants/mcp/index.d.ts +29 -0
- package/dist/constants/mcp/index.d.ts.map +1 -1
- package/dist/constants/mcp/index.js +29 -0
- package/dist/constants/mcp/index.js.map +1 -1
- package/dist/manager/agent/lifecycle.d.ts +18 -1
- package/dist/manager/agent/lifecycle.d.ts.map +1 -1
- package/dist/manager/agent/lifecycle.js +53 -8
- package/dist/manager/agent/lifecycle.js.map +1 -1
- package/dist/plugin/hook-module/capabilities.d.ts.map +1 -1
- package/dist/plugin/hook-module/capabilities.js +24 -13
- package/dist/plugin/hook-module/capabilities.js.map +1 -1
- package/dist/plugin/hook-module/host.d.ts +31 -0
- package/dist/plugin/hook-module/host.d.ts.map +1 -1
- package/dist/plugin/hook-module/host.js +56 -13
- package/dist/plugin/hook-module/host.js.map +1 -1
- package/dist/plugin/hook-module/index.d.ts +3 -2
- package/dist/plugin/hook-module/index.d.ts.map +1 -1
- package/dist/plugin/hook-module/index.js +2 -1
- package/dist/plugin/hook-module/index.js.map +1 -1
- package/dist/plugin/index.d.ts +2 -1
- package/dist/plugin/index.d.ts.map +1 -1
- package/dist/plugin/index.js +1 -0
- package/dist/plugin/index.js.map +1 -1
- package/dist/plugin/shell-hook-execute.d.ts +55 -0
- package/dist/plugin/shell-hook-execute.d.ts.map +1 -0
- package/dist/plugin/shell-hook-execute.js +73 -0
- package/dist/plugin/shell-hook-execute.js.map +1 -0
- package/dist/plugin/shell-hook-restrict.d.ts +31 -0
- package/dist/plugin/shell-hook-restrict.d.ts.map +1 -0
- package/dist/plugin/shell-hook-restrict.js +69 -0
- package/dist/plugin/shell-hook-restrict.js.map +1 -0
- package/dist/plugin/shell-hook.d.ts +73 -4
- package/dist/plugin/shell-hook.d.ts.map +1 -1
- package/dist/plugin/shell-hook.js +118 -17
- package/dist/plugin/shell-hook.js.map +1 -1
- package/dist/public-runtime.d.ts +9 -7
- package/dist/public-runtime.d.ts.map +1 -1
- package/dist/public-runtime.js +6 -5
- package/dist/public-runtime.js.map +1 -1
- package/dist/public-types.d.ts +4 -2
- package/dist/public-types.d.ts.map +1 -1
- package/dist/runtime/jobs/websocket-source.d.ts +2 -22
- package/dist/runtime/jobs/websocket-source.d.ts.map +1 -1
- package/dist/runtime/jobs/websocket-source.js +2 -33
- package/dist/runtime/jobs/websocket-source.js.map +1 -1
- package/dist/runtime/query/compact-session.d.ts.map +1 -1
- package/dist/runtime/query/compact-session.js +5 -0
- package/dist/runtime/query/compact-session.js.map +1 -1
- package/dist/runtime/query/events.d.ts +7 -0
- package/dist/runtime/query/events.d.ts.map +1 -1
- package/dist/runtime/query/events.js +29 -11
- package/dist/runtime/query/events.js.map +1 -1
- package/dist/runtime/query/executor/tool-call-admission.d.ts.map +1 -1
- package/dist/runtime/query/executor/tool-call-admission.js +51 -10
- package/dist/runtime/query/executor/tool-call-admission.js.map +1 -1
- package/dist/runtime/query/index.d.ts +6 -0
- package/dist/runtime/query/index.d.ts.map +1 -1
- package/dist/runtime/query/index.js +3 -0
- package/dist/runtime/query/index.js.map +1 -1
- package/dist/runtime/query/iteration/phases/compaction.d.ts.map +1 -1
- package/dist/runtime/query/iteration/phases/compaction.js +21 -3
- package/dist/runtime/query/iteration/phases/compaction.js.map +1 -1
- package/dist/runtime/query/iteration/phases/context.d.ts +4 -0
- package/dist/runtime/query/iteration/phases/context.d.ts.map +1 -1
- package/dist/runtime/query/iteration/phases/context.js.map +1 -1
- package/dist/runtime/query/path-escalation.d.ts.map +1 -1
- package/dist/runtime/query/path-escalation.js +90 -17
- package/dist/runtime/query/path-escalation.js.map +1 -1
- package/dist/runtime/query/steering.d.ts +15 -0
- package/dist/runtime/query/steering.d.ts.map +1 -1
- package/dist/runtime/query/steering.js +27 -3
- package/dist/runtime/query/steering.js.map +1 -1
- package/dist/scheduler/completion-inbox.d.ts.map +1 -1
- package/dist/scheduler/completion-inbox.js +15 -0
- package/dist/scheduler/completion-inbox.js.map +1 -1
- package/dist/session/workspace/driver.d.ts +12 -0
- package/dist/session/workspace/driver.d.ts.map +1 -1
- package/dist/session/workspace/git-worktree.d.ts +16 -0
- package/dist/session/workspace/git-worktree.d.ts.map +1 -1
- package/dist/session/workspace/git-worktree.js +35 -0
- package/dist/session/workspace/git-worktree.js.map +1 -1
- package/dist/session/workspace/holds.d.ts +78 -0
- package/dist/session/workspace/holds.d.ts.map +1 -0
- package/dist/session/workspace/holds.js +150 -0
- package/dist/session/workspace/holds.js.map +1 -0
- package/dist/session/workspace/index.d.ts +3 -1
- package/dist/session/workspace/index.d.ts.map +1 -1
- package/dist/session/workspace/index.js +2 -1
- package/dist/session/workspace/index.js.map +1 -1
- package/dist/store/config/index.d.ts +3 -0
- package/dist/store/config/index.d.ts.map +1 -1
- package/dist/store/config/index.js +3 -0
- package/dist/store/config/index.js.map +1 -1
- package/dist/store/session-index/index.d.ts +13 -0
- package/dist/store/session-index/index.d.ts.map +1 -1
- package/dist/store/session-index/index.js +1 -0
- package/dist/store/session-index/index.js.map +1 -1
- package/dist/store/session-index/rebuild.d.ts +12 -0
- package/dist/store/session-index/rebuild.d.ts.map +1 -1
- package/dist/store/session-index/rebuild.js +20 -4
- package/dist/store/session-index/rebuild.js.map +1 -1
- package/dist/store/session-index/sqlite.d.ts +20 -0
- package/dist/store/session-index/sqlite.d.ts.map +1 -1
- package/dist/store/session-index/sqlite.js +50 -12
- package/dist/store/session-index/sqlite.js.map +1 -1
- package/dist/tools/admit-facts.d.ts.map +1 -1
- package/dist/tools/admit-facts.js +59 -0
- package/dist/tools/admit-facts.js.map +1 -1
- package/dist/tools/builtins/grep.d.ts.map +1 -1
- package/dist/tools/builtins/grep.js +57 -0
- package/dist/tools/builtins/grep.js.map +1 -1
- package/dist/tools/builtins/lsp.d.ts.map +1 -1
- package/dist/tools/builtins/lsp.js +41 -4
- package/dist/tools/builtins/lsp.js.map +1 -1
- package/dist/tools/coordinator/ask-user-question.d.ts.map +1 -1
- package/dist/tools/coordinator/ask-user-question.js +67 -3
- package/dist/tools/coordinator/ask-user-question.js.map +1 -1
- package/dist/turn/memory-promoter.d.ts.map +1 -1
- package/dist/turn/memory-promoter.js +20 -4
- package/dist/turn/memory-promoter.js.map +1 -1
- package/dist/types/agent/base.d.ts +6 -0
- package/dist/types/agent/base.d.ts.map +1 -1
- package/dist/types/agent/task.d.ts +5 -0
- package/dist/types/agent/task.d.ts.map +1 -1
- package/dist/types/agent/task.js.map +1 -1
- package/dist/types/code-navigation/index.d.ts +34 -4
- package/dist/types/code-navigation/index.d.ts.map +1 -1
- package/dist/types/hitl/index.d.ts +8 -0
- package/dist/types/hitl/index.d.ts.map +1 -1
- package/dist/types/hitl/index.js.map +1 -1
- package/dist/types/message/index.d.ts +7 -0
- package/dist/types/message/index.d.ts.map +1 -1
- package/dist/types/message/index.js.map +1 -1
- package/dist/types/tool/presentation.d.ts +21 -0
- package/dist/types/tool/presentation.d.ts.map +1 -1
- package/package.json +6 -6
- package/src/agents/QueryAgent.ts +1 -0
- package/src/authorization/readonly-commands.ts +72 -4
- package/src/authorization/shell-paths.ts +75 -18
- package/src/compaction/index.ts +1 -0
- package/src/compaction/manual.ts +48 -12
- package/src/compaction/model-summary.ts +163 -28
- package/src/compaction/restore.ts +74 -1
- package/src/compaction/summary.ts +69 -3
- package/src/config/registry.ts +87 -5
- package/src/config/runtime.ts +3 -3
- package/src/connector/index.ts +3 -0
- package/src/connector/mcp/adapter.ts +137 -1
- package/src/connector/mcp/client.ts +49 -7
- package/src/connector/mcp/index.ts +5 -0
- package/src/connector/mcp/sandbox-stdio.ts +214 -0
- package/src/connector/mcp/stdio-program.ts +68 -0
- package/src/connector/mcp/stdio.ts +61 -7
- package/src/connector/web/guarded-fetch.ts +197 -30
- package/src/connector/web/index.ts +4 -1
- package/src/constants/mcp/index.ts +31 -0
- package/src/manager/agent/lifecycle.ts +58 -13
- package/src/plugin/hook-module/capabilities.ts +36 -14
- package/src/plugin/hook-module/host.ts +86 -13
- package/src/plugin/hook-module/index.ts +3 -0
- package/src/plugin/index.ts +4 -0
- package/src/plugin/shell-hook-execute.ts +126 -0
- package/src/plugin/shell-hook-restrict.ts +66 -0
- package/src/plugin/shell-hook.ts +209 -16
- package/src/public-runtime.ts +19 -0
- package/src/public-types.ts +20 -0
- package/src/runtime/jobs/websocket-source.ts +2 -46
- package/src/runtime/query/compact-session.ts +5 -0
- package/src/runtime/query/events.ts +29 -11
- package/src/runtime/query/executor/tool-call-admission.ts +62 -17
- package/src/runtime/query/index.ts +10 -0
- package/src/runtime/query/iteration/phases/compaction.ts +26 -2
- package/src/runtime/query/iteration/phases/context.ts +6 -0
- package/src/runtime/query/path-escalation.ts +102 -16
- package/src/runtime/query/steering.ts +38 -3
- package/src/scheduler/completion-inbox.ts +14 -0
- package/src/session/workspace/driver.ts +12 -0
- package/src/session/workspace/git-worktree.ts +49 -0
- package/src/session/workspace/holds.ts +201 -0
- package/src/session/workspace/index.ts +13 -1
- package/src/store/config/index.ts +6 -0
- package/src/store/session-index/index.ts +14 -0
- package/src/store/session-index/rebuild.ts +22 -4
- package/src/store/session-index/sqlite.ts +63 -14
- package/src/tools/admit-facts.ts +64 -0
- package/src/tools/builtins/grep.ts +52 -0
- package/src/tools/builtins/lsp.ts +47 -4
- package/src/tools/coordinator/ask-user-question.ts +88 -4
- package/src/turn/memory-promoter.ts +24 -3
- package/src/types/agent/base.ts +7 -0
- package/src/types/agent/task.ts +5 -0
- package/src/types/code-navigation/index.ts +48 -4
- package/src/types/hitl/index.ts +8 -0
- package/src/types/message/index.ts +7 -0
- package/src/types/tool/presentation.ts +22 -0
package/src/connector/index.ts
CHANGED
|
@@ -97,6 +97,7 @@ export {
|
|
|
97
97
|
mcpToolToToolDefinition,
|
|
98
98
|
renderPromptMessages,
|
|
99
99
|
resolveMcpEra,
|
|
100
|
+
SandboxStdioTransport,
|
|
100
101
|
ServerStdioTransport,
|
|
101
102
|
StdioTransport,
|
|
102
103
|
StreamableHttpTransport,
|
|
@@ -119,6 +120,8 @@ export type {
|
|
|
119
120
|
McpSkillsClient,
|
|
120
121
|
McpSkillsRefresh,
|
|
121
122
|
McpSkillsRefreshStatus,
|
|
123
|
+
SandboxStdioReplyVerdict,
|
|
124
|
+
SandboxStdioTransportOptions,
|
|
122
125
|
McpEnvelope,
|
|
123
126
|
McpEnvelopeInput,
|
|
124
127
|
McpEraProbe,
|
|
@@ -84,7 +84,24 @@ function objectToZod(rawSchema: Record<string, unknown>, depth: number): z.ZodTy
|
|
|
84
84
|
const schema = Array.isArray(allOf) && allOf.length > 0 ? mergeAllOf(allOf, rawSchema) : rawSchema
|
|
85
85
|
|
|
86
86
|
const properties = schema.properties as Record<string, unknown> | undefined
|
|
87
|
-
|
|
87
|
+
const hasProperties = properties !== undefined && Object.keys(properties).length > 0
|
|
88
|
+
|
|
89
|
+
// A root union says "one of these argument shapes", and it used to
|
|
90
|
+
// vanish here for the same reason a root `allOf` did: `properties` is
|
|
91
|
+
// undefined on `{ anyOf: [...] }`, so the schema fell to the empty-object
|
|
92
|
+
// branch below and the tool was bridged to the model as taking no
|
|
93
|
+
// arguments at all. A server that spelled out two alternative payloads
|
|
94
|
+
// had every field of both dropped from the description, and its calls
|
|
95
|
+
// came back rejected for fields the model was never shown. Only taken
|
|
96
|
+
// when the root declares no properties of its own: `properties` beside a
|
|
97
|
+
// union is an intersection of the two, which the object reading below
|
|
98
|
+
// already covers.
|
|
99
|
+
const composite = (schema.anyOf ?? schema.oneOf) as unknown[] | undefined
|
|
100
|
+
if (!hasProperties && Array.isArray(composite) && composite.length > 0) {
|
|
101
|
+
return rootUnionToZod(composite, depth)
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if (!hasProperties) {
|
|
88
105
|
return closeOrOpen(z.object({}), schema, depth)
|
|
89
106
|
}
|
|
90
107
|
if (depth >= MAX_CONVERSION_DEPTH) return z.record(z.string(), z.unknown())
|
|
@@ -100,6 +117,47 @@ function objectToZod(rawSchema: Record<string, unknown>, depth: number): z.ZodTy
|
|
|
100
117
|
return closeOrOpen(z.object(shape), schema, depth)
|
|
101
118
|
}
|
|
102
119
|
|
|
120
|
+
/**
|
|
121
|
+
* Convert a root `anyOf`/`oneOf` into a union of the argument shapes it
|
|
122
|
+
* declares.
|
|
123
|
+
*
|
|
124
|
+
* The wire reads `anyOf` in both dialects namzu emits — `registry/tool/
|
|
125
|
+
* portable.ts` recommends it as the replacement for the `type: [...]` list
|
|
126
|
+
* some wires refuse — so the union survives the round trip and the members
|
|
127
|
+
* and their properties reach the model, instead of the empty object a root
|
|
128
|
+
* union used to collapse to.
|
|
129
|
+
*
|
|
130
|
+
* A single-member union is returned unwrapped, matching the property-level
|
|
131
|
+
* conversion in `baseTypeToZod`: an `anyOf` of one would only cost prompt
|
|
132
|
+
* bytes.
|
|
133
|
+
*/
|
|
134
|
+
function rootUnionToZod(members: readonly unknown[], depth: number): z.ZodType {
|
|
135
|
+
if (depth >= MAX_CONVERSION_DEPTH) return z.record(z.string(), z.unknown())
|
|
136
|
+
const converted = members.map((member) => unionMemberToZod(member, depth + 1))
|
|
137
|
+
return converted.length === 1
|
|
138
|
+
? (converted[0] as z.ZodType)
|
|
139
|
+
: z.union(converted as [z.ZodType, z.ZodType, ...z.ZodType[]])
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Convert one member of a root union.
|
|
144
|
+
*
|
|
145
|
+
* The branches of a tool's root union are argument OBJECTS, and a server
|
|
146
|
+
* commonly writes each one without repeating `type: 'object'` — the
|
|
147
|
+
* `properties` and `required` keys alone carry the shape. Routing those
|
|
148
|
+
* through `jsonSchemaPropertyToZod` would read the missing type as
|
|
149
|
+
* "anything", so an object-shaped member is converted as an object; every
|
|
150
|
+
* other member (a bare `{ type: 'string' }`, say) takes the ordinary
|
|
151
|
+
* property path.
|
|
152
|
+
*/
|
|
153
|
+
function unionMemberToZod(member: unknown, depth: number): z.ZodType {
|
|
154
|
+
if (typeof member !== 'object' || member === null) return z.unknown()
|
|
155
|
+
const schema = member as Record<string, unknown>
|
|
156
|
+
const objectShaped =
|
|
157
|
+
schema.type === 'object' || schema.properties !== undefined || Array.isArray(schema.allOf)
|
|
158
|
+
return objectShaped ? objectToZod(schema, depth) : jsonSchemaPropertyToZod(schema, depth)
|
|
159
|
+
}
|
|
160
|
+
|
|
103
161
|
/**
|
|
104
162
|
* Honor the server's `additionalProperties`, defaulting to closed.
|
|
105
163
|
*
|
|
@@ -457,6 +515,79 @@ function mergeAllOf(members: readonly unknown[], parent: Record<string, unknown>
|
|
|
457
515
|
|
|
458
516
|
type JsonRecord = Record<string, unknown>
|
|
459
517
|
|
|
518
|
+
/**
|
|
519
|
+
* The model-facing form of a root `anyOf`/`oneOf`: one object holding every
|
|
520
|
+
* member's properties, requiring only what every member requires. The union
|
|
521
|
+
* itself stays the validator. Returns undefined when the root is no union.
|
|
522
|
+
*/
|
|
523
|
+
export function flattenRootUnion(schema: MCPJsonSchema): JsonRecord | undefined {
|
|
524
|
+
const inlined = inlineSchemaRefs(schema as unknown as Record<string, unknown>)
|
|
525
|
+
const own = inlined.properties as JsonRecord | undefined
|
|
526
|
+
if (own !== undefined && Object.keys(own).length > 0) return undefined
|
|
527
|
+
const members = (inlined.anyOf ?? inlined.oneOf) as unknown[] | undefined
|
|
528
|
+
if (!Array.isArray(members) || members.length === 0) return undefined
|
|
529
|
+
|
|
530
|
+
const seen = new Map<string, unknown[]>()
|
|
531
|
+
let required: string[] | undefined
|
|
532
|
+
for (const raw of members) {
|
|
533
|
+
if (typeof raw !== 'object' || raw === null) continue
|
|
534
|
+
const member = Array.isArray((raw as JsonRecord).allOf)
|
|
535
|
+
? mergeAllOf((raw as JsonRecord).allOf as unknown[], raw as JsonRecord)
|
|
536
|
+
: (raw as JsonRecord)
|
|
537
|
+
for (const [key, value] of Object.entries(
|
|
538
|
+
(member.properties as JsonRecord | undefined) ?? {},
|
|
539
|
+
)) {
|
|
540
|
+
const definitions = seen.get(key) ?? []
|
|
541
|
+
if (!definitions.some((d) => JSON.stringify(d) === JSON.stringify(value)))
|
|
542
|
+
definitions.push(value)
|
|
543
|
+
seen.set(key, definitions)
|
|
544
|
+
}
|
|
545
|
+
const req = (member.required as string[] | undefined) ?? []
|
|
546
|
+
required = required === undefined ? [...req] : required.filter((key) => req.includes(key))
|
|
547
|
+
}
|
|
548
|
+
const properties: JsonRecord = {}
|
|
549
|
+
for (const [key, definitions] of seen) properties[key] = mergePropertyDefinitions(definitions)
|
|
550
|
+
return {
|
|
551
|
+
type: 'object',
|
|
552
|
+
properties,
|
|
553
|
+
...(required && required.length > 0 ? { required } : {}),
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
/**
|
|
558
|
+
* One definition for a property that several members of a root union declare.
|
|
559
|
+
* The model must be able to pick any member, so a discriminator spelled as a
|
|
560
|
+
* `const` or an `enum` in each member becomes one `enum` of every value, and
|
|
561
|
+
* definitions that differ in any other way become an `anyOf` of them. The
|
|
562
|
+
* arguments are still checked against the original union.
|
|
563
|
+
*/
|
|
564
|
+
function mergePropertyDefinitions(definitions: readonly unknown[]): unknown {
|
|
565
|
+
if (definitions.length === 1) return definitions[0]
|
|
566
|
+
const records = definitions.filter(
|
|
567
|
+
(d): d is JsonRecord => typeof d === 'object' && d !== null && !Array.isArray(d),
|
|
568
|
+
)
|
|
569
|
+
if (records.length === definitions.length) {
|
|
570
|
+
const valuesOf = (d: JsonRecord): unknown[] | undefined =>
|
|
571
|
+
'const' in d ? [d.const] : Array.isArray(d.enum) ? (d.enum as unknown[]) : undefined
|
|
572
|
+
const sets = records.map(valuesOf)
|
|
573
|
+
const plain = (d: JsonRecord) =>
|
|
574
|
+
Object.keys(d).every((k) => ['const', 'enum', 'type', 'description', 'title'].includes(k))
|
|
575
|
+
const types = new Set(records.map((d) => JSON.stringify(d.type)))
|
|
576
|
+
if (sets.every((v) => v !== undefined) && records.every(plain) && types.size === 1) {
|
|
577
|
+
const values: unknown[] = []
|
|
578
|
+
for (const v of sets.flat())
|
|
579
|
+
if (!values.some((x) => JSON.stringify(x) === JSON.stringify(v))) values.push(v)
|
|
580
|
+
const first = records[0] as JsonRecord
|
|
581
|
+
return {
|
|
582
|
+
...(first.type !== undefined ? { type: first.type } : {}),
|
|
583
|
+
...(first.description !== undefined ? { description: first.description } : {}),
|
|
584
|
+
enum: values,
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
return { anyOf: [...definitions] }
|
|
589
|
+
}
|
|
590
|
+
|
|
460
591
|
/** What `z.literal` takes. */
|
|
461
592
|
type LiteralValue = string | number | bigint | boolean | null | undefined
|
|
462
593
|
|
|
@@ -505,6 +636,7 @@ export function mcpToolToToolDefinition(
|
|
|
505
636
|
maxRetries?: number,
|
|
506
637
|
): ToolDefinition {
|
|
507
638
|
const inputSchema = mcpJsonSchemaToZod(tool.inputSchema)
|
|
639
|
+
const flatModelSchema = flattenRootUnion(tool.inputSchema)
|
|
508
640
|
// A call the client keeps alive on progress must not be cut by the
|
|
509
641
|
// executor's own per-tool deadline (120 s unless the host says otherwise)
|
|
510
642
|
// first. Only with `resetTimeoutOnProgress`; otherwise no `timeoutMs`, so
|
|
@@ -542,6 +674,10 @@ export function mcpToolToToolDefinition(
|
|
|
542
674
|
? `[MCP:${serverName}] ${tool.description}`
|
|
543
675
|
: `[MCP:${serverName}] ${tool.name}`,
|
|
544
676
|
inputSchema,
|
|
677
|
+
// A root union is validated through `inputSchema` (the union), but the
|
|
678
|
+
// model is shown one flat object: providers refuse a root without
|
|
679
|
+
// `type: "object"`, and one such tool would fail every turn.
|
|
680
|
+
...(flatModelSchema ? { modelInputSchema: flatModelSchema } : {}),
|
|
545
681
|
// Carried as the server wrote it. It is shown to the model, never
|
|
546
682
|
// validated here, so rebuilding it would only lose fidelity.
|
|
547
683
|
...(tool.outputSchema
|
|
@@ -243,6 +243,13 @@ function sortedByName(tools: MCPToolDefinition[]): MCPToolDefinition[] {
|
|
|
243
243
|
.map(({ tool }) => tool)
|
|
244
244
|
}
|
|
245
245
|
|
|
246
|
+
/** What a malformed tool-list entry was, for the warning that names it. */
|
|
247
|
+
function describeToolEntry(entry: unknown): string {
|
|
248
|
+
if (entry === null) return 'null'
|
|
249
|
+
if (Array.isArray(entry)) return 'array'
|
|
250
|
+
return typeof entry
|
|
251
|
+
}
|
|
252
|
+
|
|
246
253
|
const NAMZU_CLIENT_INFO = { name: 'namzu-sdk', version: VERSION }
|
|
247
254
|
|
|
248
255
|
/** Keep server-authored status text safe and small before it reaches a host UI. */
|
|
@@ -1001,12 +1008,16 @@ export class MCPClient {
|
|
|
1001
1008
|
/**
|
|
1002
1009
|
* Every tool this server publishes that this client is willing to expose.
|
|
1003
1010
|
*
|
|
1004
|
-
*
|
|
1005
|
-
*
|
|
1006
|
-
*
|
|
1007
|
-
*
|
|
1008
|
-
*
|
|
1009
|
-
*
|
|
1011
|
+
* Two clauses refuse, and both refuse ONE entry rather than the listing.
|
|
1012
|
+
* An entry that is not an object at all is dropped by
|
|
1013
|
+
* {@link admitToolEntries}, with its position logged — `tools/list` is
|
|
1014
|
+
* the server's own JSON, and a single `null` used to reach the schema
|
|
1015
|
+
* adapter and throw there, costing every other tool the server offered.
|
|
1016
|
+
* A tool whose `inputSchema` carries an invalid `x-mcp-header` annotation
|
|
1017
|
+
* is dropped by {@link admitToolHeaderAnnotations}, with the tool name
|
|
1018
|
+
* and the reason logged — the spec's requirement, and the reason it is a
|
|
1019
|
+
* requirement is that the annotation names a header this client would
|
|
1020
|
+
* otherwise write from a value it cannot vouch for.
|
|
1010
1021
|
*
|
|
1011
1022
|
* It lives HERE rather than in `MCPToolDiscovery` or the tool adapter
|
|
1012
1023
|
* because the shipping CLI calls `listTools()` directly and never
|
|
@@ -1016,11 +1027,42 @@ export class MCPClient {
|
|
|
1016
1027
|
async listTools(options?: MCPRequestOptions): Promise<MCPToolDefinition[]> {
|
|
1017
1028
|
this.requireConnected()
|
|
1018
1029
|
const listed = await this.listAllPages<MCPToolDefinition>('tools/list', 'tools', options)
|
|
1019
|
-
const admitted = this.admitToolHeaderAnnotations(listed)
|
|
1030
|
+
const admitted = this.admitToolHeaderAnnotations(this.admitToolEntries(listed))
|
|
1020
1031
|
// Only a 2026-07-28 connection is sorted: an older server's callers keep the order it sent.
|
|
1021
1032
|
return this.era?.kind === 'modern' ? sortedByName(admitted) : admitted
|
|
1022
1033
|
}
|
|
1023
1034
|
|
|
1035
|
+
/**
|
|
1036
|
+
* Drop entries that are not tool definitions at all, and keep the rest.
|
|
1037
|
+
*
|
|
1038
|
+
* `tools/list` is parsed JSON from a server this client does not trust,
|
|
1039
|
+
* and the array was taken exactly as sent: one `null`, string, number or
|
|
1040
|
+
* array among fifty well-formed entries travelled as far as the schema
|
|
1041
|
+
* adapter, where reading `.inputSchema` off it threw and took the whole
|
|
1042
|
+
* listing down — every tool that server offered, and in a headless run
|
|
1043
|
+
* the session with it. One bad entry costs one tool.
|
|
1044
|
+
*
|
|
1045
|
+
* The warning names the position rather than the tool, because an entry
|
|
1046
|
+
* this malformed has no name to be named by. The position is its index
|
|
1047
|
+
* in the flattened listing, across pages, so it points at what the server
|
|
1048
|
+
* sent.
|
|
1049
|
+
*/
|
|
1050
|
+
private admitToolEntries(tools: readonly MCPToolDefinition[]): MCPToolDefinition[] {
|
|
1051
|
+
const admitted: MCPToolDefinition[] = []
|
|
1052
|
+
for (const [index, entry] of tools.entries()) {
|
|
1053
|
+
if (typeof entry === 'object' && entry !== null && !Array.isArray(entry)) {
|
|
1054
|
+
admitted.push(entry)
|
|
1055
|
+
continue
|
|
1056
|
+
}
|
|
1057
|
+
this.log.warn('Skipped a malformed entry in the MCP tool list', {
|
|
1058
|
+
'namzu.connector.server': this.config.serverName,
|
|
1059
|
+
'namzu.connector.index': index,
|
|
1060
|
+
'namzu.connector.entry_type': describeToolEntry(entry),
|
|
1061
|
+
})
|
|
1062
|
+
}
|
|
1063
|
+
return admitted
|
|
1064
|
+
}
|
|
1065
|
+
|
|
1024
1066
|
/**
|
|
1025
1067
|
* Call one tool, and recover once from a server that says our mirrored
|
|
1026
1068
|
* headers disagree with its current schema.
|
|
@@ -1,4 +1,9 @@
|
|
|
1
1
|
export { StdioTransport } from './stdio.js'
|
|
2
|
+
export { SandboxStdioTransport } from './sandbox-stdio.js'
|
|
3
|
+
export type {
|
|
4
|
+
SandboxStdioReplyVerdict,
|
|
5
|
+
SandboxStdioTransportOptions,
|
|
6
|
+
} from './sandbox-stdio.js'
|
|
2
7
|
export { HttpSseTransport } from './http-sse.js'
|
|
3
8
|
export { StreamableHttpTransport } from './streamable-http.js'
|
|
4
9
|
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
import { StringDecoder } from 'node:string_decoder'
|
|
2
|
+
import type {
|
|
3
|
+
MCPJsonRpcMessage,
|
|
4
|
+
MCPTransport,
|
|
5
|
+
MCPTransportSendOptions,
|
|
6
|
+
} from '../../types/connector/index.js'
|
|
7
|
+
import type { SandboxStdioChannel, SandboxStdioOperation } from '../../types/sandbox/index.js'
|
|
8
|
+
|
|
9
|
+
type Channel = SandboxStdioChannel
|
|
10
|
+
type Operation = SandboxStdioOperation
|
|
11
|
+
const MAX_MESSAGE_BYTES = 16 * 1024 * 1024
|
|
12
|
+
// The allocation-owned channel admits each stdin write up to 1 MiB. Refuse a
|
|
13
|
+
// larger serialized request before reserving an effect barrier or delivering bytes.
|
|
14
|
+
const MAX_WRITE_BYTES = 1024 * 1024
|
|
15
|
+
|
|
16
|
+
/** What a host decided about one reply to an issued request. */
|
|
17
|
+
export interface SandboxStdioReplyVerdict {
|
|
18
|
+
/**
|
|
19
|
+
* True when the reply proves the request finished, so its effect barrier
|
|
20
|
+
* is released. False leaves the sandbox fenced: the outcome is unknown and
|
|
21
|
+
* the request must never be replayed.
|
|
22
|
+
*/
|
|
23
|
+
readonly confirmed: boolean
|
|
24
|
+
/** Deliver this in place of the reply, e.g. to mark an unconfirmed outcome. */
|
|
25
|
+
readonly reply?: MCPJsonRpcMessage
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface SandboxStdioTransportOptions {
|
|
29
|
+
/** Opens the guest process: typically `() => sandbox.openStdio(command, args, options)`. */
|
|
30
|
+
readonly open: () => Promise<SandboxStdioChannel>
|
|
31
|
+
/**
|
|
32
|
+
* Judge a reply that answers an issued request (`request` is undefined for
|
|
33
|
+
* an id this transport did not issue). The default treats every well-formed
|
|
34
|
+
* reply as exact, which is right for a server that reports no effects the
|
|
35
|
+
* channel cannot see.
|
|
36
|
+
*/
|
|
37
|
+
readonly settleReply?: (
|
|
38
|
+
reply: MCPJsonRpcMessage,
|
|
39
|
+
request: { readonly method: string } | undefined,
|
|
40
|
+
) => SandboxStdioReplyVerdict
|
|
41
|
+
/** Largest accepted JSON-RPC line from the server, in bytes. Defaults to 16 MiB. */
|
|
42
|
+
readonly maxMessageBytes?: number
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* MCP over a guest process's stdin and stdout, for a sandbox that implements
|
|
47
|
+
* `openStdio`. The server runs in the sandbox and never on the host: pass it
|
|
48
|
+
* to an `MCPClient` as `transportFactory: () => new SandboxStdioTransport({
|
|
49
|
+
* open })`. Closing the transport, or destroying the sandbox, stops the
|
|
50
|
+
* server.
|
|
51
|
+
*
|
|
52
|
+
* An idle server owns no sandbox input; each issued request holds the
|
|
53
|
+
* channel's effect barrier until its reply is seen, and a request whose
|
|
54
|
+
* outcome cannot be known (a failed write, a lost stream, a reply the
|
|
55
|
+
* `settleReply` hook refuses to confirm) is reported unknown and fences the
|
|
56
|
+
* sandbox rather than being replayed.
|
|
57
|
+
*/
|
|
58
|
+
export class SandboxStdioTransport implements MCPTransport {
|
|
59
|
+
private channel?: Channel
|
|
60
|
+
private closing?: Promise<void>
|
|
61
|
+
private ended = false
|
|
62
|
+
private messageHandler?: (message: MCPJsonRpcMessage) => void
|
|
63
|
+
private closeHandler?: () => void
|
|
64
|
+
private errorHandler?: (error: Error) => void
|
|
65
|
+
private readonly pending = new Map<
|
|
66
|
+
string | number,
|
|
67
|
+
{ operation: Operation; removeAbort(): void; method: string }
|
|
68
|
+
>()
|
|
69
|
+
|
|
70
|
+
private readonly open: () => Promise<Channel>
|
|
71
|
+
private readonly settleReply: NonNullable<SandboxStdioTransportOptions['settleReply']>
|
|
72
|
+
private readonly maxMessageBytes: number
|
|
73
|
+
|
|
74
|
+
constructor(options: SandboxStdioTransportOptions | (() => Promise<SandboxStdioChannel>)) {
|
|
75
|
+
const resolved = typeof options === 'function' ? { open: options } : options
|
|
76
|
+
this.open = resolved.open
|
|
77
|
+
this.settleReply = resolved.settleReply ?? (() => ({ confirmed: true }))
|
|
78
|
+
this.maxMessageBytes = resolved.maxMessageBytes ?? MAX_MESSAGE_BYTES
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
async connect(): Promise<void> {
|
|
82
|
+
if (this.channel || this.ended) throw new Error('This sandbox MCP transport cannot reconnect.')
|
|
83
|
+
this.channel = await this.open()
|
|
84
|
+
void this.read(this.channel).catch((error: unknown) => {
|
|
85
|
+
this.fail(error instanceof Error ? error : new Error(String(error)))
|
|
86
|
+
})
|
|
87
|
+
// EOF follows buffered stdout. Group exit can be observed before the reader
|
|
88
|
+
// drains its last positive reply, so only a failed close skips that drain.
|
|
89
|
+
void this.channel.closed.catch((error: unknown) =>
|
|
90
|
+
this.fail(error instanceof Error ? error : new Error(String(error))),
|
|
91
|
+
)
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
isConnected(): boolean {
|
|
95
|
+
return this.channel !== undefined && !this.ended && !this.closing
|
|
96
|
+
}
|
|
97
|
+
onMessage(handler: (message: MCPJsonRpcMessage) => void): void {
|
|
98
|
+
this.messageHandler = handler
|
|
99
|
+
}
|
|
100
|
+
onClose(handler: () => void): void {
|
|
101
|
+
this.closeHandler = handler
|
|
102
|
+
}
|
|
103
|
+
onError(handler: (error: Error) => void): void {
|
|
104
|
+
this.errorHandler = handler
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
async send(message: MCPJsonRpcMessage, options?: MCPTransportSendOptions): Promise<void> {
|
|
108
|
+
options?.signal?.throwIfAborted()
|
|
109
|
+
const channel = this.channel
|
|
110
|
+
if (!channel || !this.isConnected()) throw new Error('This sandbox MCP channel is closed.')
|
|
111
|
+
const data = `${JSON.stringify(message)}\n`
|
|
112
|
+
if (Buffer.byteLength(data) > MAX_WRITE_BYTES)
|
|
113
|
+
throw new Error('This sandbox MCP request exceeds the 1 MiB write limit; nothing was sent.')
|
|
114
|
+
const request = message.method !== undefined && message.id !== undefined
|
|
115
|
+
if (!request) {
|
|
116
|
+
await channel.write(data, options?.signal)
|
|
117
|
+
return
|
|
118
|
+
}
|
|
119
|
+
const id = message.id as string | number
|
|
120
|
+
if (this.pending.has(id)) throw new Error('This sandbox MCP request ID is already active.')
|
|
121
|
+
const operation = await channel.beginOperation(options?.signal)
|
|
122
|
+
// No bytes were handed to the channel on either of these paths.
|
|
123
|
+
if (options?.signal?.aborted || !this.isConnected()) {
|
|
124
|
+
operation.complete()
|
|
125
|
+
options?.signal?.throwIfAborted()
|
|
126
|
+
throw new Error('This sandbox MCP channel closed before dispatch.')
|
|
127
|
+
}
|
|
128
|
+
const onAbort = () => this.settle(id, false)
|
|
129
|
+
const entry = {
|
|
130
|
+
operation,
|
|
131
|
+
method: message.method as string,
|
|
132
|
+
removeAbort: () => options?.signal?.removeEventListener('abort', onAbort),
|
|
133
|
+
}
|
|
134
|
+
this.pending.set(id, entry)
|
|
135
|
+
options?.signal?.addEventListener('abort', onAbort, { once: true })
|
|
136
|
+
try {
|
|
137
|
+
await channel.write(data, options?.signal)
|
|
138
|
+
} catch (error) {
|
|
139
|
+
// A failed stdin acknowledgement cannot prove the editor did not receive it.
|
|
140
|
+
this.settle(id, false)
|
|
141
|
+
throw error
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
private settle(id: string | number, confirmed: boolean): void {
|
|
146
|
+
const entry = this.pending.get(id)
|
|
147
|
+
if (!entry) return
|
|
148
|
+
this.pending.delete(id)
|
|
149
|
+
entry.removeAbort()
|
|
150
|
+
if (confirmed) entry.operation.complete()
|
|
151
|
+
else entry.operation.outcomeUnknown()
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
private accept(line: string): void {
|
|
155
|
+
if (Buffer.byteLength(line) > this.maxMessageBytes)
|
|
156
|
+
throw new Error('This sandbox MCP reply exceeds the message limit.')
|
|
157
|
+
const message: unknown = JSON.parse(line)
|
|
158
|
+
if (!message || typeof message !== 'object' || Array.isArray(message))
|
|
159
|
+
throw new Error('This sandbox MCP server returned an invalid JSON-RPC message.')
|
|
160
|
+
let rpc = message as MCPJsonRpcMessage
|
|
161
|
+
if (rpc.jsonrpc !== '2.0') throw new Error('This sandbox MCP server returned invalid JSON-RPC.')
|
|
162
|
+
if (rpc.method === undefined && rpc.id !== undefined) {
|
|
163
|
+
if (Object.hasOwn(rpc, 'result') === Object.hasOwn(rpc, 'error'))
|
|
164
|
+
throw new Error('This sandbox MCP server returned an invalid reply.')
|
|
165
|
+
const entry = this.pending.get(rpc.id)
|
|
166
|
+
const verdict = this.settleReply(rpc, entry ? { method: entry.method } : undefined)
|
|
167
|
+
this.settle(rpc.id, verdict.confirmed)
|
|
168
|
+
if (verdict.reply) rpc = verdict.reply
|
|
169
|
+
}
|
|
170
|
+
this.messageHandler?.(rpc)
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
private async read(channel: Channel): Promise<void> {
|
|
174
|
+
const decoder = new StringDecoder('utf8')
|
|
175
|
+
let buffered = ''
|
|
176
|
+
for await (const event of channel.events) {
|
|
177
|
+
if (event.stream !== 'stdout') continue
|
|
178
|
+
buffered += decoder.write(Buffer.from(event.data))
|
|
179
|
+
for (;;) {
|
|
180
|
+
const end = buffered.indexOf('\n')
|
|
181
|
+
if (end < 0) break
|
|
182
|
+
const line = buffered.slice(0, end).replace(/\r$/, '')
|
|
183
|
+
buffered = buffered.slice(end + 1)
|
|
184
|
+
if (line.trim()) this.accept(line)
|
|
185
|
+
}
|
|
186
|
+
if (Buffer.byteLength(buffered) > this.maxMessageBytes)
|
|
187
|
+
throw new Error('This sandbox MCP reply exceeds the message limit.')
|
|
188
|
+
}
|
|
189
|
+
if (!this.closing) {
|
|
190
|
+
if (buffered.length || decoder.end())
|
|
191
|
+
throw new Error('This sandbox MCP server closed with an incomplete reply.')
|
|
192
|
+
this.end()
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
private end(): void {
|
|
197
|
+
if (this.ended) return
|
|
198
|
+
this.ended = true
|
|
199
|
+
for (const id of this.pending.keys()) this.settle(id, false)
|
|
200
|
+
this.closeHandler?.()
|
|
201
|
+
}
|
|
202
|
+
private fail(error: Error): void {
|
|
203
|
+
if (this.ended) return
|
|
204
|
+
this.errorHandler?.(error)
|
|
205
|
+
this.end()
|
|
206
|
+
void this.close().catch(() => undefined)
|
|
207
|
+
}
|
|
208
|
+
close(): Promise<void> {
|
|
209
|
+
if (this.closing) return this.closing
|
|
210
|
+
this.end()
|
|
211
|
+
this.closing = this.channel?.close() ?? Promise.resolve()
|
|
212
|
+
return this.closing
|
|
213
|
+
}
|
|
214
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { join } from 'node:path'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The folder of an installed Desktop's own tools (`<resources>/toolchain`), named by the app when it
|
|
5
|
+
* starts the engine. Absent means the engine runs on whatever the machine has.
|
|
6
|
+
*/
|
|
7
|
+
export const TOOLCHAIN_DIR_ENV = 'NAMZU_TOOLCHAIN_DIR'
|
|
8
|
+
/** `bundled` when tool servers must run on the app's own Node, `system` when the machine's Node is used. */
|
|
9
|
+
export const TOOLCHAIN_NODE_ENV = 'NAMZU_TOOLCHAIN_NODE'
|
|
10
|
+
|
|
11
|
+
export interface StdioProgram {
|
|
12
|
+
command: string
|
|
13
|
+
args: string[]
|
|
14
|
+
/** Variables the program needs beyond the server's own environment. */
|
|
15
|
+
env: Record<string, string>
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface StdioProgramContext {
|
|
19
|
+
platform?: NodeJS.Platform
|
|
20
|
+
/** Where the toolchain variables are read from; the engine's own environment by default. */
|
|
21
|
+
source?: NodeJS.ProcessEnv
|
|
22
|
+
/** The program running the engine, which is the app's own executable under Desktop. */
|
|
23
|
+
execPath?: string
|
|
24
|
+
/** True when `execPath` is an Electron executable that needs ELECTRON_RUN_AS_NODE to behave as Node. */
|
|
25
|
+
electron?: boolean
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const WINDOWS_SPELLING = /\.(exe|cmd|bat)$/i
|
|
29
|
+
|
|
30
|
+
/** `node`, `npx` or `npm` out of a command as written (`npx`, `NPX.CMD`), or undefined for any other program. */
|
|
31
|
+
function toolName(command: string): 'node' | 'npx' | 'npm' | undefined {
|
|
32
|
+
if (/[\\/]/.test(command)) return undefined
|
|
33
|
+
const name = command.replace(WINDOWS_SPELLING, '').toLowerCase()
|
|
34
|
+
return name === 'node' || name === 'npx' || name === 'npm' ? name : undefined
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* What to start for a tool server's command.
|
|
39
|
+
*
|
|
40
|
+
* A server is usually written as `npx -y some-server` or `node server.js`. On a machine with no Node
|
|
41
|
+
* that fails to start, and on Windows `npx` and `npm` are `.cmd` files that `spawn` cannot start
|
|
42
|
+
* without a shell even when Node is installed. When the app declares a toolchain, those three names
|
|
43
|
+
* are started as the app's own Node running its pinned npm; every other command is returned as given.
|
|
44
|
+
* The machine's own Node is kept for `node` when the app says it chose it, and on Windows `npx` and
|
|
45
|
+
* `npm` still run the pinned npm, on that Node.
|
|
46
|
+
*/
|
|
47
|
+
export function resolveStdioProgram(
|
|
48
|
+
command: string,
|
|
49
|
+
args: readonly string[],
|
|
50
|
+
context: StdioProgramContext = {},
|
|
51
|
+
): StdioProgram {
|
|
52
|
+
const source = context.source ?? process.env
|
|
53
|
+
const platform = context.platform ?? process.platform
|
|
54
|
+
const dir = source[TOOLCHAIN_DIR_ENV]
|
|
55
|
+
const unchanged: StdioProgram = { command, args: [...args], env: {} }
|
|
56
|
+
const name = toolName(command)
|
|
57
|
+
if (!dir || !name) return unchanged
|
|
58
|
+
const bundled = source[TOOLCHAIN_NODE_ENV] === 'bundled'
|
|
59
|
+
const cli = (file: string) => join(dir, 'npm', 'bin', file)
|
|
60
|
+
if (!bundled && !(platform === 'win32' && name !== 'node')) return unchanged
|
|
61
|
+
const node = bundled ? (context.execPath ?? process.execPath) : 'node'
|
|
62
|
+
const env: Record<string, string> =
|
|
63
|
+
bundled && (context.electron ?? Boolean(process.versions.electron))
|
|
64
|
+
? { ELECTRON_RUN_AS_NODE: '1' }
|
|
65
|
+
: {}
|
|
66
|
+
if (name === 'node') return { command: node, args: [...args], env }
|
|
67
|
+
return { command: node, args: [cli(name === 'npx' ? 'npx-cli.js' : 'npm-cli.js'), ...args], env }
|
|
68
|
+
}
|