@vite-hub/shell 0.0.3 → 0.1.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/LICENSE +201 -0
- package/README.md +85 -35
- package/dist/analyze-BXwz20IH.js +247 -0
- package/dist/index.d.ts +4 -5
- package/dist/index.js +2 -2
- package/dist/{just-bash-D84udCFy.d.ts → just-bash-DAyZQWLK.d.ts} +2 -2
- package/dist/providers/just-bash.d.ts +1 -1
- package/dist/providers/just-bash.js +132 -59
- package/dist/session-CIy2VLp9.js +245 -0
- package/dist/{types-CNRdehmi.d.ts → types-2uHfTVHi.d.ts} +23 -3
- package/dist/workspace.d.ts +14 -11
- package/dist/workspace.js +237 -209
- package/package.json +29 -14
- package/dist/analyze-Bu5Ueqph.js +0 -126
- package/dist/parse-D9VT4P6N.js +0 -41
- package/dist/providers/cloudflare.d.ts +0 -28
- package/dist/providers/cloudflare.js +0 -44
- package/dist/session-0OrrlmE9.js +0 -178
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
# @vite-hub/shell
|
|
2
2
|
|
|
3
3
|
<p>
|
|
4
|
-
<a href="https://vitehub.dev"><img alt="ViteHub" src="https://
|
|
5
|
-
<img alt="TypeScript" src="https://
|
|
6
|
-
<img alt="Shell" src="https://
|
|
4
|
+
<a href="https://vitehub.dev"><img alt="ViteHub" src="https://comark-ui.onmax.me/badge.svg?label=ViteHub&value=vitehub.dev"></a>
|
|
5
|
+
<img alt="TypeScript" src="https://comark-ui.onmax.me/badge.svg?label=TypeScript&value=ready">
|
|
6
|
+
<img alt="Shell" src="https://comark-ui.onmax.me/badge.svg?label=Shell&value=controlled+runtime">
|
|
7
7
|
</p>
|
|
8
8
|
|
|
9
|
-
`@vite-hub/shell` gives
|
|
9
|
+
`@vite-hub/shell` gives server code structured command analysis and execution with explicit filesystem, network, process, and timeout boundaries.
|
|
10
|
+
|
|
11
|
+
Applications that only need an Agent to inspect or edit its Workspace usually use the `workspaceShell()` Capability from `@vite-hub/agent/capabilities`. Install this owner package directly when you are building a Capability, Workspace adapter, or host integration.
|
|
10
12
|
|
|
11
13
|
## Install
|
|
12
14
|
|
|
@@ -14,43 +16,91 @@
|
|
|
14
16
|
pnpm add @vite-hub/shell
|
|
15
17
|
```
|
|
16
18
|
|
|
17
|
-
##
|
|
19
|
+
## Inspect a command
|
|
18
20
|
|
|
19
21
|
```ts
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
async exec(command) {
|
|
35
|
-
return {
|
|
36
|
-
command,
|
|
37
|
-
event: "command_finished",
|
|
38
|
-
exitCode: 0,
|
|
39
|
-
stderr: "",
|
|
40
|
-
stdout: "ok\n",
|
|
41
|
-
}
|
|
42
|
-
},
|
|
43
|
-
}
|
|
22
|
+
import { analyzeShellCommand } from "@vite-hub/shell";
|
|
23
|
+
|
|
24
|
+
const analysis = await analyzeShellCommand("rg TODO src");
|
|
25
|
+
|
|
26
|
+
console.log(analysis.commands); // ["rg"]
|
|
27
|
+
console.log(analysis.hasPipelines); // false
|
|
28
|
+
console.log(analysis.ok); // true
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
Analysis reports command names, pipelines, redirects, heredocs, and command substitution. It does not make a command safe to run. The caller's policy and the execution provider remain the enforcement boundaries.
|
|
32
|
+
|
|
33
|
+
## Run against a Workspace
|
|
34
|
+
|
|
35
|
+
The built-in Just Bash provider executes Bash-compatible commands against the filesystem adapter you give it. This example mounts a ViteHub Workspace read-only, permits four inspection commands, and returns a structured observation.
|
|
44
36
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
37
|
+
```sh
|
|
38
|
+
pnpm add @vite-hub/shell @vite-hub/workspace
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
```ts
|
|
42
|
+
// server/tasks/search-docs.ts
|
|
43
|
+
import { createShellRuntime } from "@vite-hub/shell";
|
|
44
|
+
import { createJustBashProvider } from "@vite-hub/shell/providers/just-bash";
|
|
45
|
+
import { createReadonlyWorkspaceFs, workspaceMountPoint } from "@vite-hub/shell/workspace";
|
|
46
|
+
import { useWorkspace } from "@vite-hub/workspace";
|
|
47
|
+
|
|
48
|
+
export async function searchDocs() {
|
|
49
|
+
const workspace = useWorkspace("docs");
|
|
50
|
+
const shell = createShellRuntime({
|
|
51
|
+
policy: {
|
|
52
|
+
maxOutputLength: 10_000,
|
|
53
|
+
maxShellCalls: 1,
|
|
54
|
+
timeout: 30_000,
|
|
55
|
+
},
|
|
56
|
+
provider: createJustBashProvider({
|
|
57
|
+
commands: ["cat", "ls", "pwd", "rg"],
|
|
58
|
+
cwd: workspaceMountPoint,
|
|
59
|
+
fs: createReadonlyWorkspaceFs(workspace.fs),
|
|
60
|
+
}),
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
return shell.exec("rg auth .", { cwd: workspaceMountPoint });
|
|
64
|
+
}
|
|
48
65
|
```
|
|
49
66
|
|
|
50
|
-
|
|
67
|
+
`shell.exec()` creates a short-lived Shell Session and returns a Shell Observation with `event`, `exitCode`, `stdout`, and `stderr`. The provider above has no network access, background processes, or interactive processes, and its Workspace filesystem cannot write.
|
|
68
|
+
|
|
69
|
+
For writable access, pass `useWorkspace(name, { mode: "write" }).fs` directly to `createWritableWorkspaceFs()`. The adapter accepts Workspace writes that return revision receipts. Its `writeFile()` and `appendFile()` methods wait for the write and return `void`.
|
|
70
|
+
|
|
71
|
+
## Providers and boundaries
|
|
72
|
+
|
|
73
|
+
- `@vite-hub/shell/providers/just-bash` runs selected commands in `just-bash` against a supplied filesystem adapter.
|
|
74
|
+
- A custom `ShellExecutionProvider` declares its boundary and implements execution for another host.
|
|
75
|
+
|
|
76
|
+
Custom providers can return Shell Processes as class instances. Sessions read the process ID, command, and working directory from the provider handle when inspected. These public metadata fields are readonly. A failed metadata read does not prevent process cleanup.
|
|
77
|
+
|
|
78
|
+
Shell policy can bound calls, processes, output size, and timeouts. A declared boundary describes the provider contract; it is not proof of operating-system isolation. Use [Sandbox](https://vitehub.dev/docs/sandbox) when work needs provider-managed isolation.
|
|
79
|
+
|
|
80
|
+
`runtime.boundary.execution` and `runtime.boundary.resources` expose the provider's execution model and resource controls. The Just Bash provider runs in a virtual in-process filesystem and truncates completed output before it creates a Shell Observation. It buffers the full result first, so this is not an execution memory limit. Pass an `AbortSignal` to `exec()` to cancel the command and any controlled Source request it starts.
|
|
81
|
+
|
|
82
|
+
Custom providers can return Shell Observations as class instances. Sessions preserve every declared observation field, including fields exposed through getters.
|
|
83
|
+
|
|
84
|
+
The Workspace filesystem has a virtual root at `/workspace`. Root existence checks return true. On a writable filesystem, recursive directory creation at the root succeeds without changing the Workspace. Creating the root without `recursive` fails because it already exists.
|
|
85
|
+
|
|
86
|
+
Directory paths may end in `/`. Listing or copying `models/` preserves each child's full name, just as `models` does.
|
|
87
|
+
|
|
88
|
+
The Just Bash `commands` list also applies to controlled `curl` requests. A Source network grant permits access to its declared target, but `curl` must still be included when you configure a command list.
|
|
89
|
+
|
|
90
|
+
The provider copies the command list at creation. Later changes to the supplied list do not change its permissions or network boundary.
|
|
91
|
+
|
|
92
|
+
The writable Workspace filesystem leaves content unchanged when a move resolves to the same source and destination. Missing sources fail. It rejects moves into descendants before changing content.
|
|
93
|
+
|
|
94
|
+
Workspace filesystem adapters preserve binary output from redirection and `tee`. Their read, write, and append methods honor Just Bash encoding options.
|
|
95
|
+
|
|
96
|
+
Just Bash defaults to `/workspace`. A provider `cwd` changes that default, and an `exec` `cwd` takes precedence. The provider reports the selected directory in its observations. Set `cwd` for a custom filesystem with another root.
|
|
97
|
+
|
|
98
|
+
Appending creates an absent Workspace file. A read failure for an existing file fails the append before changing its content.
|
|
99
|
+
|
|
100
|
+
## Use with Agents
|
|
51
101
|
|
|
52
|
-
`workspaceShell()` in [`@vite-hub/agent`](../agent/README.md)
|
|
102
|
+
`workspaceShell()` in [`@vite-hub/agent`](../agent/README.md) exposes scoped shell work through an Agent Capability. It attaches Workspace Scope, Shell policy, metadata, and tools to the Agent Definition; do not expose an unrestricted raw runtime to a model.
|
|
53
103
|
|
|
54
104
|
Built on [just-bash](https://www.npmjs.com/package/just-bash) for the built-in shell provider and [sh-syntax](https://www.npmjs.com/package/sh-syntax) for command analysis.
|
|
55
105
|
|
|
56
|
-
|
|
106
|
+
Read the complete [Shell guide](https://vitehub.dev/docs/shell), the [Workspace shell Capability](https://vitehub.dev/docs/workspace/agent-capability), and the [official Capabilities guide](https://vitehub.dev/docs/agents/capabilities/official).
|
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
import { defineDiagnostics } from "nostics";
|
|
2
|
+
//#region src/error-diagnostics.ts
|
|
3
|
+
const dynamicError = { why: ({ message }) => message === void 0 ? "" : String(message) };
|
|
4
|
+
const shellErrorDiagnostics = /*#__PURE__*/ defineDiagnostics({
|
|
5
|
+
docsBase: () => "https://vitehub.dev/docs/reference/diagnostics",
|
|
6
|
+
codes: {
|
|
7
|
+
SHELL_R0001: dynamicError,
|
|
8
|
+
SHELL_R0002: dynamicError,
|
|
9
|
+
SHELL_R0003: dynamicError,
|
|
10
|
+
SHELL_R0004: dynamicError,
|
|
11
|
+
SHELL_R0005: dynamicError,
|
|
12
|
+
SHELL_R0006: dynamicError,
|
|
13
|
+
SHELL_R0007: dynamicError,
|
|
14
|
+
SHELL_R0008: dynamicError,
|
|
15
|
+
SHELL_R0009: dynamicError,
|
|
16
|
+
SHELL_R0010: dynamicError,
|
|
17
|
+
SHELL_R0011: dynamicError,
|
|
18
|
+
SHELL_R0012: dynamicError,
|
|
19
|
+
SHELL_R0013: dynamicError,
|
|
20
|
+
SHELL_R0014: dynamicError,
|
|
21
|
+
SHELL_R0015: dynamicError,
|
|
22
|
+
SHELL_R0016: dynamicError,
|
|
23
|
+
SHELL_R0017: dynamicError,
|
|
24
|
+
SHELL_R0018: dynamicError,
|
|
25
|
+
SHELL_R0019: dynamicError,
|
|
26
|
+
SHELL_R0020: dynamicError,
|
|
27
|
+
SHELL_R0021: dynamicError,
|
|
28
|
+
SHELL_R0022: dynamicError,
|
|
29
|
+
SHELL_R0023: dynamicError
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
//#endregion
|
|
33
|
+
//#region src/command/parse.ts
|
|
34
|
+
function parseShellCommand(command, mode = "execution") {
|
|
35
|
+
const words = [];
|
|
36
|
+
let current = "";
|
|
37
|
+
let wordStarted = false;
|
|
38
|
+
let quote;
|
|
39
|
+
let escaped = false;
|
|
40
|
+
for (let index = 0; index < command.length; index += 1) {
|
|
41
|
+
const char = command[index];
|
|
42
|
+
if (escaped) {
|
|
43
|
+
if (char !== "\n") {
|
|
44
|
+
current += char;
|
|
45
|
+
wordStarted = true;
|
|
46
|
+
}
|
|
47
|
+
escaped = false;
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
if (quote) {
|
|
51
|
+
if (char === quote) quote = void 0;
|
|
52
|
+
else if (char === "\\" && quote === "\"" && /[\\$`"\n]/.test(command[index + 1] ?? "")) escaped = true;
|
|
53
|
+
else current += char;
|
|
54
|
+
wordStarted = true;
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
if (char === "\\") {
|
|
58
|
+
escaped = true;
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
if (char === "'" || char === "\"") {
|
|
62
|
+
quote = char;
|
|
63
|
+
wordStarted = true;
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
if (/\s/.test(char)) {
|
|
67
|
+
if (wordStarted) {
|
|
68
|
+
words.push(current);
|
|
69
|
+
current = "";
|
|
70
|
+
wordStarted = false;
|
|
71
|
+
}
|
|
72
|
+
continue;
|
|
73
|
+
}
|
|
74
|
+
current += char;
|
|
75
|
+
wordStarted = true;
|
|
76
|
+
}
|
|
77
|
+
if (mode === "execution") {
|
|
78
|
+
if (escaped) {
|
|
79
|
+
current += "\\";
|
|
80
|
+
wordStarted = true;
|
|
81
|
+
}
|
|
82
|
+
if (quote) throw shellErrorDiagnostics.SHELL_R0003({ message: "unterminated quote" });
|
|
83
|
+
}
|
|
84
|
+
if (wordStarted) words.push(current);
|
|
85
|
+
return words;
|
|
86
|
+
}
|
|
87
|
+
function splitShellCommandSegments(command) {
|
|
88
|
+
const segments = [];
|
|
89
|
+
let current = "";
|
|
90
|
+
let quote;
|
|
91
|
+
let ansiCQuoted = false;
|
|
92
|
+
let escaped = false;
|
|
93
|
+
let comment = false;
|
|
94
|
+
let wordBoundary = true;
|
|
95
|
+
let followsPipe = false;
|
|
96
|
+
for (let index = 0; index < command.length; index++) {
|
|
97
|
+
const char = command[index];
|
|
98
|
+
if (comment) {
|
|
99
|
+
if (char !== "\n") continue;
|
|
100
|
+
comment = false;
|
|
101
|
+
}
|
|
102
|
+
if (escaped) {
|
|
103
|
+
if (char === "\n" && !ansiCQuoted) {
|
|
104
|
+
current = current.slice(0, -1);
|
|
105
|
+
escaped = false;
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
current += char;
|
|
109
|
+
wordBoundary = false;
|
|
110
|
+
escaped = false;
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
if (char === "\\" && (quote !== "'" || ansiCQuoted)) {
|
|
114
|
+
current += char;
|
|
115
|
+
escaped = true;
|
|
116
|
+
continue;
|
|
117
|
+
}
|
|
118
|
+
if (quote) {
|
|
119
|
+
if (char === quote) {
|
|
120
|
+
quote = void 0;
|
|
121
|
+
ansiCQuoted = false;
|
|
122
|
+
}
|
|
123
|
+
current += char;
|
|
124
|
+
wordBoundary = false;
|
|
125
|
+
continue;
|
|
126
|
+
}
|
|
127
|
+
if (char === "$" && command[index + 1] === "'") {
|
|
128
|
+
quote = "'";
|
|
129
|
+
ansiCQuoted = true;
|
|
130
|
+
current += "$'";
|
|
131
|
+
wordBoundary = false;
|
|
132
|
+
index += 1;
|
|
133
|
+
continue;
|
|
134
|
+
}
|
|
135
|
+
if (char === "'" || char === "\"") {
|
|
136
|
+
quote = char;
|
|
137
|
+
current += char;
|
|
138
|
+
wordBoundary = false;
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
141
|
+
if (char === "#" && wordBoundary) {
|
|
142
|
+
comment = true;
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
const next = command[index + 1];
|
|
146
|
+
if (char === "&" && next === "&") {
|
|
147
|
+
segments.push({
|
|
148
|
+
command: current,
|
|
149
|
+
followsPipe,
|
|
150
|
+
separatorAfter: "&&"
|
|
151
|
+
});
|
|
152
|
+
current = "";
|
|
153
|
+
followsPipe = false;
|
|
154
|
+
wordBoundary = true;
|
|
155
|
+
index += 1;
|
|
156
|
+
continue;
|
|
157
|
+
}
|
|
158
|
+
if (char === "|" && next === "|") {
|
|
159
|
+
segments.push({
|
|
160
|
+
command: current,
|
|
161
|
+
followsPipe,
|
|
162
|
+
separatorAfter: "||"
|
|
163
|
+
});
|
|
164
|
+
current = "";
|
|
165
|
+
followsPipe = false;
|
|
166
|
+
wordBoundary = true;
|
|
167
|
+
index += 1;
|
|
168
|
+
continue;
|
|
169
|
+
}
|
|
170
|
+
if (char === "|" || char === ";" || char === "\n") {
|
|
171
|
+
segments.push({
|
|
172
|
+
command: current,
|
|
173
|
+
followsPipe,
|
|
174
|
+
separatorAfter: char
|
|
175
|
+
});
|
|
176
|
+
current = "";
|
|
177
|
+
followsPipe = char === "|";
|
|
178
|
+
wordBoundary = true;
|
|
179
|
+
continue;
|
|
180
|
+
}
|
|
181
|
+
current += char;
|
|
182
|
+
wordBoundary = /[\s()<>]/.test(char) || char === "&";
|
|
183
|
+
}
|
|
184
|
+
segments.push({
|
|
185
|
+
command: current,
|
|
186
|
+
followsPipe
|
|
187
|
+
});
|
|
188
|
+
return segments;
|
|
189
|
+
}
|
|
190
|
+
//#endregion
|
|
191
|
+
//#region src/command/analyze.ts
|
|
192
|
+
const defaultMaxInputBytes = 65536;
|
|
193
|
+
const defaultTimeoutMs = 100;
|
|
194
|
+
async function analyzeShellCommand(command, options = {}) {
|
|
195
|
+
const maxInputBytes = options.maxInputBytes ?? defaultMaxInputBytes;
|
|
196
|
+
if (new TextEncoder().encode(command).byteLength > maxInputBytes) return {
|
|
197
|
+
error: `Shell command exceeds ${maxInputBytes} bytes.`,
|
|
198
|
+
ok: false,
|
|
199
|
+
parser: "sh-syntax"
|
|
200
|
+
};
|
|
201
|
+
const segments = splitShellCommandSegments(command);
|
|
202
|
+
const commands = [...new Set(segments.map((segment) => firstCommandWord(segment.command)).filter((name) => name !== void 0))];
|
|
203
|
+
try {
|
|
204
|
+
await withTimeout(parseWithShSyntax(command), options.timeoutMs ?? defaultTimeoutMs);
|
|
205
|
+
} catch (error) {
|
|
206
|
+
return {
|
|
207
|
+
commands,
|
|
208
|
+
error: error instanceof Error ? error.message : String(error),
|
|
209
|
+
ok: false,
|
|
210
|
+
parser: "sh-syntax"
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
return {
|
|
214
|
+
commands,
|
|
215
|
+
hasCommandSubstitution: /(?:\$\(|`)/.test(command),
|
|
216
|
+
hasHeredocs: /<<-?/.test(command),
|
|
217
|
+
hasPipelines: segments.some((segment) => segment.separatorAfter === "|" || segment.separatorAfter === "||"),
|
|
218
|
+
hasRedirects: /(?:^|[^<])(?:>>?|<)/.test(command),
|
|
219
|
+
ok: true,
|
|
220
|
+
parser: "sh-syntax"
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
async function parseWithShSyntax(command) {
|
|
224
|
+
const { parse, LangVariant } = await import("sh-syntax");
|
|
225
|
+
await parse(command, { variant: LangVariant.LangBash });
|
|
226
|
+
}
|
|
227
|
+
async function withTimeout(promise, timeoutMs) {
|
|
228
|
+
if (timeoutMs <= 0) throw shellErrorDiagnostics.SHELL_R0001({ message: `Shell analysis timed out after ${timeoutMs}ms.` });
|
|
229
|
+
let timeout;
|
|
230
|
+
try {
|
|
231
|
+
return await Promise.race([promise, new Promise((_, reject) => {
|
|
232
|
+
timeout = setTimeout(() => reject(shellErrorDiagnostics.SHELL_R0002({ message: `Shell analysis timed out after ${timeoutMs}ms.` })), timeoutMs);
|
|
233
|
+
})]);
|
|
234
|
+
} finally {
|
|
235
|
+
if (timeout) clearTimeout(timeout);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
function firstCommandWord(segment) {
|
|
239
|
+
const words = segment.trim().match(/[^\s]+/g) || [];
|
|
240
|
+
for (const word of words) {
|
|
241
|
+
if (/^[A-Za-z_][A-Za-z0-9_]*=/.test(word)) continue;
|
|
242
|
+
if (/^\d*(?:>>?|<<?-?|<&|>&)/.test(word)) continue;
|
|
243
|
+
return word.replace(/^command$/, "") || void 0;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
//#endregion
|
|
247
|
+
export { shellErrorDiagnostics as i, parseShellCommand as n, splitShellCommandSegments as r, analyzeShellCommand as t };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import { a as ShellBoundary, c as ShellObservationEvent, d as ShellRuntimeExecOptions, f as ShellSession, i as ShellAnalyzeResult, l as ShellProcess, n as CreateShellSessionOptions, o as ShellExecutionProvider, p as ShellSessionPolicy, r as ShellAnalyzeOptions, s as ShellObservation, t as CreateShellRuntimeOptions, u as ShellRuntime } from "./types-
|
|
2
|
-
|
|
1
|
+
import { a as ShellBoundary, c as ShellObservationEvent, d as ShellRuntimeExecOptions, f as ShellSession, i as ShellAnalyzeResult, l as ShellProcess, n as CreateShellSessionOptions, o as ShellExecutionProvider, p as ShellSessionPolicy, r as ShellAnalyzeOptions, s as ShellObservation, t as CreateShellRuntimeOptions, u as ShellRuntime } from "./types-2uHfTVHi.js";
|
|
3
2
|
//#region src/command/analyze.d.ts
|
|
4
|
-
declare function analyzeShellCommand(command: string, options?: ShellAnalyzeOptions): Promise<ShellAnalyzeResult>;
|
|
3
|
+
export declare function analyzeShellCommand(command: string, options?: ShellAnalyzeOptions): Promise<ShellAnalyzeResult>;
|
|
5
4
|
//#endregion
|
|
6
5
|
//#region src/runtime/session.d.ts
|
|
7
|
-
declare function createShellRuntime(options: CreateShellRuntimeOptions): ShellRuntime;
|
|
6
|
+
export declare function createShellRuntime(options: CreateShellRuntimeOptions): ShellRuntime;
|
|
8
7
|
//#endregion
|
|
9
|
-
export {
|
|
8
|
+
export type { CreateShellRuntimeOptions, CreateShellSessionOptions, ShellAnalyzeOptions, ShellAnalyzeResult, ShellBoundary, ShellExecutionProvider, ShellObservation, ShellObservationEvent, ShellProcess, ShellRuntime, ShellRuntimeExecOptions, ShellSession, ShellSessionPolicy };
|
package/dist/index.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { t as analyzeShellCommand } from "./analyze-
|
|
2
|
-
import { t as createShellRuntime } from "./session-
|
|
1
|
+
import { t as analyzeShellCommand } from "./analyze-BXwz20IH.js";
|
|
2
|
+
import { t as createShellRuntime } from "./session-CIy2VLp9.js";
|
|
3
3
|
export { analyzeShellCommand, createShellRuntime };
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import { o as ShellExecutionProvider } from "./types-
|
|
1
|
+
import { o as ShellExecutionProvider } from "./types-2uHfTVHi.js";
|
|
2
2
|
import { IFileSystem } from "just-bash";
|
|
3
|
-
|
|
4
3
|
//#region src/providers/just-bash.d.ts
|
|
5
4
|
interface JustBashFileSystem extends IFileSystem {
|
|
6
5
|
readonly writeFs: boolean;
|
|
@@ -17,6 +16,7 @@ interface ShellNetworkGrantExecutor {
|
|
|
17
16
|
interface ShellNetworkRequest {
|
|
18
17
|
body?: unknown;
|
|
19
18
|
method: "GET" | "HEAD" | "POST";
|
|
19
|
+
signal?: AbortSignal;
|
|
20
20
|
url: string;
|
|
21
21
|
}
|
|
22
22
|
interface ShellNetworkRequestResult {
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { a as ShellNetworkRequestResult, i as ShellNetworkRequest, n as JustBashProviderOptions, o as createJustBashProvider, r as ShellNetworkGrantExecutor, t as JustBashFileSystem } from "../just-bash-
|
|
1
|
+
import { a as ShellNetworkRequestResult, i as ShellNetworkRequest, n as JustBashProviderOptions, o as createJustBashProvider, r as ShellNetworkGrantExecutor, t as JustBashFileSystem } from "../just-bash-DAyZQWLK.js";
|
|
2
2
|
export { JustBashFileSystem, JustBashProviderOptions, ShellNetworkGrantExecutor, ShellNetworkRequest, ShellNetworkRequestResult, createJustBashProvider };
|