@namzu/sdk 69.0.0 → 69.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/CHANGELOG.md +61 -0
- package/dist/agents/QueryAgent.d.ts.map +1 -1
- package/dist/agents/QueryAgent.js +2 -0
- package/dist/agents/QueryAgent.js.map +1 -1
- package/dist/authorization/skill-grant.d.ts.map +1 -1
- package/dist/authorization/skill-grant.js +1 -0
- package/dist/authorization/skill-grant.js.map +1 -1
- package/dist/bridge/acp/permission.d.ts +10 -0
- package/dist/bridge/acp/permission.d.ts.map +1 -1
- package/dist/bridge/acp/permission.js.map +1 -1
- package/dist/bridge/acp/server.d.ts.map +1 -1
- package/dist/bridge/acp/server.js +1 -0
- package/dist/bridge/acp/server.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/client.d.ts +29 -0
- package/dist/connector/mcp/client.d.ts.map +1 -1
- package/dist/connector/mcp/client.js +59 -2
- package/dist/connector/mcp/client.js.map +1 -1
- package/dist/connector/mcp/errors.d.ts +7 -0
- package/dist/connector/mcp/errors.d.ts.map +1 -1
- package/dist/connector/mcp/errors.js +14 -0
- package/dist/connector/mcp/errors.js.map +1 -1
- package/dist/connector/mcp/index.d.ts +5 -1
- package/dist/connector/mcp/index.d.ts.map +1 -1
- package/dist/connector/mcp/index.js +3 -1
- package/dist/connector/mcp/index.js.map +1 -1
- package/dist/connector/mcp/skill-source.d.ts +144 -0
- package/dist/connector/mcp/skill-source.d.ts.map +1 -0
- package/dist/connector/mcp/skill-source.js +375 -0
- package/dist/connector/mcp/skill-source.js.map +1 -0
- package/dist/connector/mcp/skills.d.ts +137 -0
- package/dist/connector/mcp/skills.d.ts.map +1 -0
- package/dist/connector/mcp/skills.js +303 -0
- package/dist/connector/mcp/skills.js.map +1 -0
- package/dist/network/audit.d.ts +1 -1
- package/dist/network/audit.d.ts.map +1 -1
- package/dist/network/audit.js +2 -0
- package/dist/network/audit.js.map +1 -1
- package/dist/network/computer.d.ts +107 -0
- package/dist/network/computer.d.ts.map +1 -0
- package/dist/network/computer.js +250 -0
- package/dist/network/computer.js.map +1 -0
- package/dist/network/index.d.ts +2 -0
- package/dist/network/index.d.ts.map +1 -1
- package/dist/network/index.js +1 -0
- package/dist/network/index.js.map +1 -1
- package/dist/network/join.d.ts +3 -1
- package/dist/network/join.d.ts.map +1 -1
- package/dist/network/join.js +6 -3
- package/dist/network/join.js.map +1 -1
- package/dist/network/roster.d.ts +5 -0
- package/dist/network/roster.d.ts.map +1 -1
- package/dist/network/roster.js.map +1 -1
- package/dist/network/service.d.ts +12 -2
- package/dist/network/service.d.ts.map +1 -1
- package/dist/network/service.js +377 -52
- package/dist/network/service.js.map +1 -1
- package/dist/network/store.d.ts +10 -0
- package/dist/network/store.d.ts.map +1 -1
- package/dist/network/store.js +25 -3
- package/dist/network/store.js.map +1 -1
- package/dist/network/wire.d.ts +2 -0
- package/dist/network/wire.d.ts.map +1 -1
- package/dist/network/wire.js +5 -0
- package/dist/network/wire.js.map +1 -1
- package/dist/plugin/shell-hook-tool-names.d.ts.map +1 -1
- package/dist/plugin/shell-hook-tool-names.js +1 -0
- package/dist/plugin/shell-hook-tool-names.js.map +1 -1
- package/dist/plugin/shell-hook.d.ts +8 -3
- package/dist/plugin/shell-hook.d.ts.map +1 -1
- package/dist/plugin/shell-hook.js +132 -7
- package/dist/plugin/shell-hook.js.map +1 -1
- package/dist/provider/fallback.d.ts.map +1 -1
- package/dist/provider/fallback.js +4 -0
- package/dist/provider/fallback.js.map +1 -1
- package/dist/provider/idle-timeout.d.ts.map +1 -1
- package/dist/provider/idle-timeout.js +3 -0
- package/dist/provider/idle-timeout.js.map +1 -1
- package/dist/provider/model-window-suffix.d.ts.map +1 -1
- package/dist/provider/model-window-suffix.js +1 -0
- package/dist/provider/model-window-suffix.js.map +1 -1
- package/dist/provider/retry.d.ts.map +1 -1
- package/dist/provider/retry.js +3 -0
- package/dist/provider/retry.js.map +1 -1
- package/dist/public-runtime.d.ts +9 -6
- package/dist/public-runtime.d.ts.map +1 -1
- package/dist/public-runtime.js +11 -4
- package/dist/public-runtime.js.map +1 -1
- package/dist/public-tools.d.ts +5 -4
- package/dist/public-tools.d.ts.map +1 -1
- package/dist/public-tools.js +5 -3
- package/dist/public-tools.js.map +1 -1
- package/dist/public-types.d.ts +3 -1
- package/dist/public-types.d.ts.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 +10 -1
- package/dist/runtime/query/executor/tool-call-admission.js.map +1 -1
- package/dist/runtime/query/executor.d.ts +12 -0
- package/dist/runtime/query/executor.d.ts.map +1 -1
- package/dist/runtime/query/executor.js +43 -9
- package/dist/runtime/query/executor.js.map +1 -1
- package/dist/runtime/query/index.d.ts +4 -0
- package/dist/runtime/query/index.d.ts.map +1 -1
- package/dist/runtime/query/index.js +32 -0
- package/dist/runtime/query/index.js.map +1 -1
- package/dist/runtime/query/review-policy.d.ts +2 -2
- package/dist/runtime/query/review-policy.d.ts.map +1 -1
- package/dist/runtime/query/review-policy.js +2 -2
- package/dist/runtime/query/review-policy.js.map +1 -1
- package/dist/runtime/query/shell-line-escalation.d.ts +24 -0
- package/dist/runtime/query/shell-line-escalation.d.ts.map +1 -0
- package/dist/runtime/query/shell-line-escalation.js +38 -0
- package/dist/runtime/query/shell-line-escalation.js.map +1 -0
- package/dist/runtime/query/tooling.d.ts +7 -0
- package/dist/runtime/query/tooling.d.ts.map +1 -1
- package/dist/runtime/query/tooling.js +2 -0
- package/dist/runtime/query/tooling.js.map +1 -1
- package/dist/runtime/query/turn/outcome-ledger.d.ts.map +1 -1
- package/dist/runtime/query/turn/outcome-ledger.js +11 -0
- package/dist/runtime/query/turn/outcome-ledger.js.map +1 -1
- package/dist/scheduler/local.d.ts.map +1 -1
- package/dist/scheduler/local.js +3 -1
- package/dist/scheduler/local.js.map +1 -1
- package/dist/skills/index.d.ts +4 -0
- package/dist/skills/index.d.ts.map +1 -1
- package/dist/skills/index.js +2 -0
- package/dist/skills/index.js.map +1 -1
- package/dist/skills/inline-shell-host.d.ts +103 -0
- package/dist/skills/inline-shell-host.d.ts.map +1 -0
- package/dist/skills/inline-shell-host.js +238 -0
- package/dist/skills/inline-shell-host.js.map +1 -0
- package/dist/skills/inline-shell.d.ts +176 -0
- package/dist/skills/inline-shell.d.ts.map +1 -0
- package/dist/skills/inline-shell.js +646 -0
- package/dist/skills/inline-shell.js.map +1 -0
- package/dist/store/index.d.ts +2 -0
- package/dist/store/index.d.ts.map +1 -1
- package/dist/store/index.js +1 -0
- package/dist/store/index.js.map +1 -1
- package/dist/store/task/file.d.ts +124 -0
- package/dist/store/task/file.d.ts.map +1 -0
- package/dist/store/task/file.js +498 -0
- package/dist/store/task/file.js.map +1 -0
- package/dist/teams/backend.d.ts +28 -0
- package/dist/teams/backend.d.ts.map +1 -0
- package/dist/teams/backend.js +2 -0
- package/dist/teams/backend.js.map +1 -0
- package/dist/teams/fs-sync.d.ts +19 -0
- package/dist/teams/fs-sync.d.ts.map +1 -0
- package/dist/teams/fs-sync.js +81 -0
- package/dist/teams/fs-sync.js.map +1 -0
- package/dist/teams/index.d.ts +14 -0
- package/dist/teams/index.d.ts.map +1 -0
- package/dist/teams/index.js +11 -0
- package/dist/teams/index.js.map +1 -0
- package/dist/teams/names.d.ts +15 -0
- package/dist/teams/names.d.ts.map +1 -0
- package/dist/teams/names.js +43 -0
- package/dist/teams/names.js.map +1 -0
- package/dist/teams/store.d.ts +133 -0
- package/dist/teams/store.d.ts.map +1 -0
- package/dist/teams/store.js +536 -0
- package/dist/teams/store.js.map +1 -0
- package/dist/teams/types.d.ts +97 -0
- package/dist/teams/types.d.ts.map +1 -0
- package/dist/teams/types.js +70 -0
- package/dist/teams/types.js.map +1 -0
- package/dist/tools/admit-facts.d.ts.map +1 -1
- package/dist/tools/admit-facts.js +9 -1
- package/dist/tools/admit-facts.js.map +1 -1
- package/dist/tools/builtins/computer-use.d.ts +56 -2
- package/dist/tools/builtins/computer-use.d.ts.map +1 -1
- package/dist/tools/builtins/computer-use.js +604 -90
- package/dist/tools/builtins/computer-use.js.map +1 -1
- package/dist/tools/builtins/index.d.ts +1 -1
- package/dist/tools/builtins/index.d.ts.map +1 -1
- package/dist/tools/builtins/index.js.map +1 -1
- package/dist/tools/builtins/skill.d.ts +31 -0
- package/dist/tools/builtins/skill.d.ts.map +1 -1
- package/dist/tools/builtins/skill.js +232 -88
- package/dist/tools/builtins/skill.js.map +1 -1
- package/dist/tools/task/create.d.ts.map +1 -1
- package/dist/tools/task/create.js +11 -2
- package/dist/tools/task/create.js.map +1 -1
- package/dist/tools/task/get.d.ts +8 -0
- package/dist/tools/task/get.d.ts.map +1 -0
- package/dist/tools/task/get.js +51 -0
- package/dist/tools/task/get.js.map +1 -0
- package/dist/tools/task/index.d.ts +7 -0
- package/dist/tools/task/index.d.ts.map +1 -1
- package/dist/tools/task/index.js +10 -2
- package/dist/tools/task/index.js.map +1 -1
- package/dist/tools/task/list.d.ts.map +1 -1
- package/dist/tools/task/list.js +10 -2
- package/dist/tools/task/list.js.map +1 -1
- package/dist/tools/task/number.d.ts +13 -0
- package/dist/tools/task/number.d.ts.map +1 -0
- package/dist/tools/task/number.js +30 -0
- package/dist/tools/task/number.js.map +1 -0
- package/dist/tools/task/present.d.ts.map +1 -1
- package/dist/tools/task/present.js +17 -3
- package/dist/tools/task/present.js.map +1 -1
- package/dist/tools/task/update.d.ts +2 -1
- package/dist/tools/task/update.d.ts.map +1 -1
- package/dist/tools/task/update.js +86 -19
- package/dist/tools/task/update.js.map +1 -1
- package/dist/types/acp/index.d.ts +10 -0
- package/dist/types/acp/index.d.ts.map +1 -1
- package/dist/types/agent/base.d.ts +4 -0
- package/dist/types/agent/base.d.ts.map +1 -1
- package/dist/types/agent/scheduler.d.ts +9 -0
- package/dist/types/agent/scheduler.d.ts.map +1 -1
- package/dist/types/computer-use/index.d.ts +7 -0
- package/dist/types/computer-use/index.d.ts.map +1 -1
- package/dist/types/computer-use/index.js.map +1 -1
- package/dist/types/connector/mcp.d.ts +10 -0
- package/dist/types/connector/mcp.d.ts.map +1 -1
- package/dist/types/permission/index.d.ts +20 -0
- package/dist/types/permission/index.d.ts.map +1 -1
- package/dist/types/permission/index.js +25 -0
- package/dist/types/permission/index.js.map +1 -1
- package/dist/types/plugin/index.d.ts +47 -3
- package/dist/types/plugin/index.d.ts.map +1 -1
- package/dist/types/plugin/index.js +4 -0
- package/dist/types/plugin/index.js.map +1 -1
- package/dist/types/provider/interface.d.ts +6 -0
- package/dist/types/provider/interface.d.ts.map +1 -1
- package/dist/types/tool/index.d.ts +28 -2
- package/dist/types/tool/index.d.ts.map +1 -1
- package/dist/types/tool/index.js.map +1 -1
- package/dist/types/tool/presentation.d.ts +4 -0
- package/dist/types/tool/presentation.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/agents/QueryAgent.ts +2 -0
- package/src/authorization/skill-grant.ts +1 -0
- package/src/bridge/acp/permission.ts +11 -1
- package/src/bridge/acp/server.ts +1 -0
- package/src/connector/index.ts +27 -0
- package/src/connector/mcp/client.ts +71 -2
- package/src/connector/mcp/errors.ts +14 -0
- package/src/connector/mcp/index.ts +36 -0
- package/src/connector/mcp/skill-source.ts +499 -0
- package/src/connector/mcp/skills.ts +367 -0
- package/src/network/audit.ts +2 -0
- package/src/network/computer.ts +347 -0
- package/src/network/index.ts +20 -0
- package/src/network/join.ts +4 -2
- package/src/network/roster.ts +5 -0
- package/src/network/service.ts +492 -75
- package/src/network/store.ts +29 -3
- package/src/network/wire.ts +5 -0
- package/src/plugin/shell-hook-tool-names.ts +1 -0
- package/src/plugin/shell-hook.ts +144 -7
- package/src/provider/fallback.ts +4 -0
- package/src/provider/idle-timeout.ts +3 -0
- package/src/provider/model-window-suffix.ts +1 -0
- package/src/provider/retry.ts +3 -0
- package/src/public-runtime.ts +99 -0
- package/src/public-tools.ts +9 -0
- package/src/public-types.ts +28 -0
- package/src/runtime/query/executor/tool-call-admission.ts +10 -1
- package/src/runtime/query/executor.ts +57 -8
- package/src/runtime/query/index.ts +43 -0
- package/src/runtime/query/review-policy.ts +7 -1
- package/src/runtime/query/shell-line-escalation.ts +49 -0
- package/src/runtime/query/tooling.ts +6 -0
- package/src/runtime/query/turn/outcome-ledger.ts +11 -0
- package/src/scheduler/local.ts +3 -1
- package/src/skills/index.ts +43 -0
- package/src/skills/inline-shell-host.ts +303 -0
- package/src/skills/inline-shell.ts +765 -0
- package/src/store/index.ts +8 -0
- package/src/store/task/file.ts +641 -0
- package/src/teams/backend.ts +29 -0
- package/src/teams/fs-sync.ts +91 -0
- package/src/teams/index.ts +47 -0
- package/src/teams/names.ts +49 -0
- package/src/teams/store.ts +607 -0
- package/src/teams/types.ts +102 -0
- package/src/tools/admit-facts.ts +9 -1
- package/src/tools/builtins/computer-use.ts +689 -95
- package/src/tools/builtins/index.ts +1 -0
- package/src/tools/builtins/skill.ts +291 -95
- package/src/tools/task/create.ts +11 -2
- package/src/tools/task/get.ts +53 -0
- package/src/tools/task/index.ts +14 -2
- package/src/tools/task/list.ts +10 -2
- package/src/tools/task/number.ts +32 -0
- package/src/tools/task/present.ts +18 -3
- package/src/tools/task/update.ts +93 -19
- package/src/types/acp/index.ts +11 -1
- package/src/types/agent/base.ts +6 -0
- package/src/types/agent/scheduler.ts +9 -0
- package/src/types/computer-use/index.ts +7 -0
- package/src/types/connector/mcp.ts +10 -0
- package/src/types/permission/index.ts +29 -0
- package/src/types/plugin/index.ts +95 -39
- package/src/types/provider/interface.ts +7 -0
- package/src/types/tool/index.ts +23 -2
- package/src/types/tool/presentation.ts +2 -0
|
@@ -0,0 +1,765 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shell commands written inside a skill or a custom command, run while the
|
|
3
|
+
* text loads.
|
|
4
|
+
*
|
|
5
|
+
* Two forms, both read by {@link findInlineShell}:
|
|
6
|
+
*
|
|
7
|
+
* - A fenced block opened by a line that is three backticks and a bang, closed
|
|
8
|
+
* by a line of three backticks. Everything between is ONE command, so a
|
|
9
|
+
* multi-line script runs as a single shell call.
|
|
10
|
+
* - Inline: a bang and a backtick span, `!` then a backtick, the command, a
|
|
11
|
+
* backtick, where the bang starts the text or follows whitespace.
|
|
12
|
+
*
|
|
13
|
+
* An ordinary backtick span and an ordinary fence are masked first, so the
|
|
14
|
+
* documentation a skill shows (a command in code font, an example block)
|
|
15
|
+
* never runs. An unterminated shell fence is plain text.
|
|
16
|
+
*
|
|
17
|
+
* This file reads no permission and no file; the one thing it asks of the process is which
|
|
18
|
+
* shell reads a command, when a value lands inside one. {@link expandInlineShell}
|
|
19
|
+
* takes the host's way of running one command (permission check, the hook, the
|
|
20
|
+
* shell tool) and decides everything around it: limits, placeholders, how the
|
|
21
|
+
* output replaces the marker, what fails the load.
|
|
22
|
+
*
|
|
23
|
+
* ## A value never becomes a command
|
|
24
|
+
*
|
|
25
|
+
* The order is: substitute `$ARGUMENTS` and the variables, then read the
|
|
26
|
+
* commands. A value a person typed (or a model sent), and any path or name the
|
|
27
|
+
* host wrote into the text, is therefore text that the reader sees, so
|
|
28
|
+
* {@link substituteEscaped} does two things. Outside a command it rewrites the
|
|
29
|
+
* shapes that would begin one. Inside a command it quotes the value for the
|
|
30
|
+
* shell, one word at a time, so it is read as arguments and never as shell
|
|
31
|
+
* syntax. It refuses the load if a value managed to add a command anyway.
|
|
32
|
+
*/
|
|
33
|
+
|
|
34
|
+
import { hostCommandShell } from '../tools/command-shell.js'
|
|
35
|
+
import type { ShellDialect } from '../types/tool/index.js'
|
|
36
|
+
|
|
37
|
+
/** Most commands one expansion may run. */
|
|
38
|
+
export const INLINE_SHELL_MAX_COMMANDS = 256
|
|
39
|
+
/** Longest command text, in characters. */
|
|
40
|
+
export const INLINE_SHELL_MAX_COMMAND_CHARS = 32_768
|
|
41
|
+
/** Most characters of command output one expansion may put into the text. */
|
|
42
|
+
export const INLINE_SHELL_MAX_OUTPUT_CHARS = 4_000_000
|
|
43
|
+
/** The longest reason from a hook that is shown. */
|
|
44
|
+
export const INLINE_SHELL_REASON_CHARS = 500
|
|
45
|
+
/** The longest command shown in a warning. */
|
|
46
|
+
export const INLINE_SHELL_COMMAND_SHOWN_CHARS = 80
|
|
47
|
+
|
|
48
|
+
export type InlineShellKind = 'fence' | 'inline'
|
|
49
|
+
|
|
50
|
+
/** One command found in a text: the marker as written, and where it sits. */
|
|
51
|
+
export interface InlineShellMarker {
|
|
52
|
+
readonly kind: InlineShellKind
|
|
53
|
+
/** Offset of the first character of the marker, in the normalised text. */
|
|
54
|
+
readonly start: number
|
|
55
|
+
/** Offset just past the last character of the marker. */
|
|
56
|
+
readonly end: number
|
|
57
|
+
/** The command, trimmed. */
|
|
58
|
+
readonly command: string
|
|
59
|
+
/** The marker as written. */
|
|
60
|
+
readonly raw: string
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** The text with line endings as `\n`, which is the text every offset refers to. */
|
|
64
|
+
export function normalizeInlineShellText(text: string): string {
|
|
65
|
+
return text.replace(/\r\n?/g, '\n')
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const SHELL_FENCE_OPEN = /^[ \t]*```![ \t]*$/
|
|
69
|
+
const FENCE_LINE = /^[ \t]*(`{3,}|~{3,})(.*)$/
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Every command in the text, in order. Offsets refer to
|
|
73
|
+
* {@link normalizeInlineShellText} of the text; {@link expandInlineShell} does
|
|
74
|
+
* that first, and a caller that splices by hand must do the same.
|
|
75
|
+
*/
|
|
76
|
+
export function findInlineShell(text: string): InlineShellMarker[] {
|
|
77
|
+
const source = normalizeInlineShellText(text)
|
|
78
|
+
const markers: InlineShellMarker[] = []
|
|
79
|
+
const lines = source.split('\n')
|
|
80
|
+
const starts: number[] = []
|
|
81
|
+
let running = 0
|
|
82
|
+
for (const line of lines) {
|
|
83
|
+
starts.push(running)
|
|
84
|
+
running += line.length + 1
|
|
85
|
+
}
|
|
86
|
+
const endOf = (i: number): number => (starts[i] as number) + (lines[i] as string).length
|
|
87
|
+
// Ranges that hold a fence: nothing inside them is read for an inline command.
|
|
88
|
+
const fenced: Array<{ from: number; to: number }> = []
|
|
89
|
+
|
|
90
|
+
let i = 0
|
|
91
|
+
while (i < lines.length) {
|
|
92
|
+
const line = lines[i] as string
|
|
93
|
+
if (SHELL_FENCE_OPEN.test(line)) {
|
|
94
|
+
let close = -1
|
|
95
|
+
for (let j = i + 1; j < lines.length; j++) {
|
|
96
|
+
if (/^[ \t]*```[ \t]*$/.test(lines[j] as string)) {
|
|
97
|
+
close = j
|
|
98
|
+
break
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
if (close === -1) {
|
|
102
|
+
// Never closed: plain text.
|
|
103
|
+
i += 1
|
|
104
|
+
continue
|
|
105
|
+
}
|
|
106
|
+
const command = lines
|
|
107
|
+
.slice(i + 1, close)
|
|
108
|
+
.join('\n')
|
|
109
|
+
.trim()
|
|
110
|
+
if (command.length > 0) {
|
|
111
|
+
const start = starts[i] as number
|
|
112
|
+
const end = endOf(close)
|
|
113
|
+
markers.push({ kind: 'fence', start, end, command, raw: source.slice(start, end) })
|
|
114
|
+
fenced.push({ from: start, to: end })
|
|
115
|
+
}
|
|
116
|
+
i = close + 1
|
|
117
|
+
continue
|
|
118
|
+
}
|
|
119
|
+
const fence = FENCE_LINE.exec(line)
|
|
120
|
+
if (fence !== null) {
|
|
121
|
+
const mark = fence[1] as string
|
|
122
|
+
const char = mark[0] as string
|
|
123
|
+
const info = fence[2] as string
|
|
124
|
+
// A backtick fence cannot have a backtick in its info string; that line is an ordinary span.
|
|
125
|
+
if (!(char === '`' && info.includes('`'))) {
|
|
126
|
+
let close = -1
|
|
127
|
+
for (let j = i + 1; j < lines.length; j++) {
|
|
128
|
+
const candidate = (lines[j] as string).trim()
|
|
129
|
+
if (candidate.length >= mark.length && [...candidate].every((c) => c === char)) {
|
|
130
|
+
close = j
|
|
131
|
+
break
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
// An ordinary fence with no close runs to the end of the text, as it does when read.
|
|
135
|
+
const last = close === -1 ? lines.length - 1 : close
|
|
136
|
+
fenced.push({ from: starts[i] as number, to: endOf(last) })
|
|
137
|
+
i = last + 1
|
|
138
|
+
continue
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
i += 1
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
let at = 0
|
|
145
|
+
for (const gap of fenced) {
|
|
146
|
+
if (gap.from > at) for (const m of inlineMarkers(source, at, gap.from)) markers.push(m)
|
|
147
|
+
at = Math.max(at, gap.to)
|
|
148
|
+
}
|
|
149
|
+
if (at < source.length) for (const m of inlineMarkers(source, at, source.length)) markers.push(m)
|
|
150
|
+
markers.sort((x, y) => x.start - y.start)
|
|
151
|
+
return markers
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/** The inline commands between two offsets, ordinary code spans set aside. */
|
|
155
|
+
function inlineMarkers(source: string, from: number, to: number): InlineShellMarker[] {
|
|
156
|
+
const found: InlineShellMarker[] = []
|
|
157
|
+
let i = from
|
|
158
|
+
while (i < to) {
|
|
159
|
+
if (source[i] !== '`') {
|
|
160
|
+
i += 1
|
|
161
|
+
continue
|
|
162
|
+
}
|
|
163
|
+
let runEnd = i
|
|
164
|
+
while (runEnd < to && source[runEnd] === '`') runEnd += 1
|
|
165
|
+
const length = runEnd - i
|
|
166
|
+
const bang = i > from && source[i - 1] === '!'
|
|
167
|
+
const bangStart = i - 1
|
|
168
|
+
const startsText = bangStart === from || /\s/.test(source[bangStart - 1] ?? '')
|
|
169
|
+
if (bang && length === 1 && startsText) {
|
|
170
|
+
// The body is every character up to the next backtick, and has some.
|
|
171
|
+
const close = source.indexOf('`', runEnd)
|
|
172
|
+
if (close !== -1 && close < to && close > runEnd) {
|
|
173
|
+
const command = source.slice(runEnd, close).trim()
|
|
174
|
+
if (command.length > 0) {
|
|
175
|
+
found.push({
|
|
176
|
+
kind: 'inline',
|
|
177
|
+
start: bangStart,
|
|
178
|
+
end: close + 1,
|
|
179
|
+
command,
|
|
180
|
+
raw: source.slice(bangStart, close + 1),
|
|
181
|
+
})
|
|
182
|
+
}
|
|
183
|
+
i = close + 1
|
|
184
|
+
continue
|
|
185
|
+
}
|
|
186
|
+
i = runEnd
|
|
187
|
+
continue
|
|
188
|
+
}
|
|
189
|
+
// An ordinary span: closed by a run of the same length. Masked whole.
|
|
190
|
+
let search = runEnd
|
|
191
|
+
let closed = -1
|
|
192
|
+
while (search < to) {
|
|
193
|
+
const at = source.indexOf('`', search)
|
|
194
|
+
if (at === -1 || at >= to) break
|
|
195
|
+
let end = at
|
|
196
|
+
while (end < to && source[end] === '`') end += 1
|
|
197
|
+
if (end - at === length) {
|
|
198
|
+
closed = end
|
|
199
|
+
break
|
|
200
|
+
}
|
|
201
|
+
search = end
|
|
202
|
+
}
|
|
203
|
+
i = closed === -1 ? runEnd : closed
|
|
204
|
+
}
|
|
205
|
+
return found
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* A value made safe to put into a text that is read for commands afterwards:
|
|
210
|
+
* the shapes that open a command are broken by a space, and a leading bang is
|
|
211
|
+
* escaped so the template's own backtick after it cannot complete one.
|
|
212
|
+
*/
|
|
213
|
+
export function escapeInlineShell(value: string): string {
|
|
214
|
+
let out = value.replaceAll('!`', '! `').replaceAll('`!', '` !')
|
|
215
|
+
if (out.startsWith('!')) out = `\\${out}`
|
|
216
|
+
return out
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/** A value is put where `before` and `after` are the template's characters around it. */
|
|
220
|
+
function escapeBetween(value: string, before: string, after: string): string {
|
|
221
|
+
let out = escapeInlineShell(value)
|
|
222
|
+
if (before === '!' && out.startsWith('`')) out = ` ${out}`
|
|
223
|
+
if (after === '`' && out.endsWith('!')) out = `${out} `
|
|
224
|
+
if (after === '!' && out.endsWith('`')) out = `${out} `
|
|
225
|
+
return out
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/** A load that cannot go on: the reason is the message, and what to do about it is in it. */
|
|
229
|
+
export class InlineShellError extends Error {
|
|
230
|
+
constructor(
|
|
231
|
+
message: string,
|
|
232
|
+
readonly reason: 'failed' | 'interrupted' | 'permission' | 'limit' | 'stopped' | 'injection',
|
|
233
|
+
) {
|
|
234
|
+
super(message)
|
|
235
|
+
this.name = 'InlineShellError'
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/** Where a value sits in the text of a command, as far as the shell reads it. */
|
|
240
|
+
type ShellContext = 'none' | 'single' | 'double' | 'unsafe'
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* How the shell reads the position at the end of `prefix`, the text of one
|
|
244
|
+
* command up to a value: outside any quote, inside single or double quotes, or
|
|
245
|
+
* somewhere a value cannot be made safe (a comment, a dangling backslash, a here-document, a `${...}`, an
|
|
246
|
+
* arithmetic or backtick expansion, an ANSI-C string that is not closed).
|
|
247
|
+
*
|
|
248
|
+
* A `$( ... )` starts a fresh reading of its own, so a value inside one is
|
|
249
|
+
* quoted for the quotes opened inside it.
|
|
250
|
+
*/
|
|
251
|
+
function shellContextAt(prefix: string): ShellContext {
|
|
252
|
+
const stack: Array<'double' | 'sub'> = []
|
|
253
|
+
let single = false
|
|
254
|
+
let wordStart = true
|
|
255
|
+
const skipTo = (from: number, close: string, nest?: string): number => {
|
|
256
|
+
let depth = 0
|
|
257
|
+
for (let j = from; j < prefix.length; j++) {
|
|
258
|
+
const ch = prefix[j] as string
|
|
259
|
+
if (ch === '\\') {
|
|
260
|
+
j += 1
|
|
261
|
+
continue
|
|
262
|
+
}
|
|
263
|
+
if (nest !== undefined && prefix.startsWith(nest, j)) {
|
|
264
|
+
depth += 1
|
|
265
|
+
j += nest.length - 1
|
|
266
|
+
continue
|
|
267
|
+
}
|
|
268
|
+
if (prefix.startsWith(close, j)) {
|
|
269
|
+
if (depth === 0) return j + close.length - 1
|
|
270
|
+
depth -= 1
|
|
271
|
+
j += close.length - 1
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
return -1
|
|
275
|
+
}
|
|
276
|
+
for (let i = 0; i < prefix.length; i++) {
|
|
277
|
+
const c = prefix[i] as string
|
|
278
|
+
if (single) {
|
|
279
|
+
if (c === "'") single = false
|
|
280
|
+
continue
|
|
281
|
+
}
|
|
282
|
+
if (c === '\\') {
|
|
283
|
+
// A backslash right before the value would escape the first quote the value gets.
|
|
284
|
+
if (i + 1 >= prefix.length) return 'unsafe'
|
|
285
|
+
i += 1
|
|
286
|
+
wordStart = false
|
|
287
|
+
continue
|
|
288
|
+
}
|
|
289
|
+
const top = stack[stack.length - 1]
|
|
290
|
+
if (c === '$') {
|
|
291
|
+
const next = prefix[i + 1]
|
|
292
|
+
if (next === '(') {
|
|
293
|
+
if (prefix[i + 2] === '(') return 'unsafe'
|
|
294
|
+
stack.push('sub')
|
|
295
|
+
i += 1
|
|
296
|
+
wordStart = true
|
|
297
|
+
continue
|
|
298
|
+
}
|
|
299
|
+
if (next === '{') {
|
|
300
|
+
const end = skipTo(i + 2, '}', '{')
|
|
301
|
+
if (end === -1) return 'unsafe'
|
|
302
|
+
i = end
|
|
303
|
+
wordStart = false
|
|
304
|
+
continue
|
|
305
|
+
}
|
|
306
|
+
if (next === "'") {
|
|
307
|
+
const end = skipTo(i + 2, "'")
|
|
308
|
+
if (end === -1) return 'unsafe'
|
|
309
|
+
i = end
|
|
310
|
+
wordStart = false
|
|
311
|
+
continue
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
if (c === '`') {
|
|
315
|
+
const end = skipTo(i + 1, '`')
|
|
316
|
+
if (end === -1) return 'unsafe'
|
|
317
|
+
i = end
|
|
318
|
+
wordStart = false
|
|
319
|
+
continue
|
|
320
|
+
}
|
|
321
|
+
if (top === 'double') {
|
|
322
|
+
if (c === '"') stack.pop()
|
|
323
|
+
continue
|
|
324
|
+
}
|
|
325
|
+
if (c === "'") {
|
|
326
|
+
single = true
|
|
327
|
+
wordStart = false
|
|
328
|
+
continue
|
|
329
|
+
}
|
|
330
|
+
if (c === '"') {
|
|
331
|
+
stack.push('double')
|
|
332
|
+
wordStart = false
|
|
333
|
+
continue
|
|
334
|
+
}
|
|
335
|
+
if (c === '(') {
|
|
336
|
+
stack.push('sub')
|
|
337
|
+
wordStart = true
|
|
338
|
+
continue
|
|
339
|
+
}
|
|
340
|
+
if (c === ')') {
|
|
341
|
+
if (top === 'sub') stack.pop()
|
|
342
|
+
wordStart = true
|
|
343
|
+
continue
|
|
344
|
+
}
|
|
345
|
+
if (c === '<' && prefix[i + 1] === '<' && prefix[i + 2] !== '<') return 'unsafe'
|
|
346
|
+
if (c === '#' && wordStart) {
|
|
347
|
+
const end = prefix.indexOf('\n', i)
|
|
348
|
+
// A value inside a comment would meet a newline that ends the comment.
|
|
349
|
+
if (end === -1) return 'unsafe'
|
|
350
|
+
i = end
|
|
351
|
+
wordStart = true
|
|
352
|
+
continue
|
|
353
|
+
}
|
|
354
|
+
wordStart = /[\s;|&]/.test(c)
|
|
355
|
+
}
|
|
356
|
+
if (single) return 'single'
|
|
357
|
+
return stack[stack.length - 1] === 'double' ? 'double' : 'none'
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
/** One word as the shell reads it literally. */
|
|
361
|
+
function shellQuoteWord(word: string): string {
|
|
362
|
+
return `'${word.replaceAll("'", "'\\''")}'`
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
/**
|
|
366
|
+
* The words a typed value holds, read as a person typed them: spaces separate,
|
|
367
|
+
* a quote groups, a backslash keeps the next character. A quote that is never
|
|
368
|
+
* closed is an ordinary character.
|
|
369
|
+
*/
|
|
370
|
+
function typedWords(value: string): string[] {
|
|
371
|
+
const words: string[] = []
|
|
372
|
+
let current = ''
|
|
373
|
+
let inWord = false
|
|
374
|
+
for (let i = 0; i < value.length; i++) {
|
|
375
|
+
const c = value[i] as string
|
|
376
|
+
if (/\s/.test(c)) {
|
|
377
|
+
if (inWord) words.push(current)
|
|
378
|
+
current = ''
|
|
379
|
+
inWord = false
|
|
380
|
+
continue
|
|
381
|
+
}
|
|
382
|
+
inWord = true
|
|
383
|
+
if (c === '\\' && i + 1 < value.length) {
|
|
384
|
+
current += value[i + 1]
|
|
385
|
+
i += 1
|
|
386
|
+
continue
|
|
387
|
+
}
|
|
388
|
+
if (c === "'" || c === '"') {
|
|
389
|
+
let j = i + 1
|
|
390
|
+
let body = ''
|
|
391
|
+
let closed = false
|
|
392
|
+
for (; j < value.length; j++) {
|
|
393
|
+
const d = value[j] as string
|
|
394
|
+
if (c === '"' && d === '\\' && (value[j + 1] === '"' || value[j + 1] === '\\')) {
|
|
395
|
+
body += value[j + 1]
|
|
396
|
+
j += 1
|
|
397
|
+
continue
|
|
398
|
+
}
|
|
399
|
+
if (d === c) {
|
|
400
|
+
closed = true
|
|
401
|
+
break
|
|
402
|
+
}
|
|
403
|
+
body += d
|
|
404
|
+
}
|
|
405
|
+
if (closed) {
|
|
406
|
+
current += body
|
|
407
|
+
i = j
|
|
408
|
+
continue
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
current += c
|
|
412
|
+
}
|
|
413
|
+
if (inWord) words.push(current)
|
|
414
|
+
return words
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
/**
|
|
418
|
+
* A value as the text of a command holds it, so the shell reads it as data:
|
|
419
|
+
*
|
|
420
|
+
* - outside quotes each word of the value becomes one single-quoted word, so a
|
|
421
|
+
* value of `-n 5` is still two arguments while `;`, `|`, `&`, `$(`, a
|
|
422
|
+
* backtick, a redirection, a newline and `#` are plain characters;
|
|
423
|
+
* - inside single or double quotes the value is escaped for that quote and
|
|
424
|
+
* stays one piece.
|
|
425
|
+
*
|
|
426
|
+
* A value with a backtick cannot go into a command at all (the backtick would
|
|
427
|
+
* end the command where it is read), and neither can one where no quoting
|
|
428
|
+
* holds (a here-document, `${...}`): the load is refused.
|
|
429
|
+
*/
|
|
430
|
+
function quoteForCommand(value: string, prefix: string, dialect: ShellDialect): string {
|
|
431
|
+
if (dialect === 'cmd') {
|
|
432
|
+
// No quoting here is known to hold in the Windows command interpreter, so only words
|
|
433
|
+
// made of characters it reads literally go in.
|
|
434
|
+
const words = typedWords(value)
|
|
435
|
+
if (words.every((word) => /^[\w.\\/:=+,@-]+$/.test(word))) return words.join(' ')
|
|
436
|
+
throw new InlineShellError(
|
|
437
|
+
'A value given to this skill or command has characters that cannot be put safely inside a command for the Windows command interpreter, so it was not loaded.',
|
|
438
|
+
'injection',
|
|
439
|
+
)
|
|
440
|
+
}
|
|
441
|
+
const context = shellContextAt(prefix)
|
|
442
|
+
if (context === 'unsafe' || value.includes('`')) {
|
|
443
|
+
throw new InlineShellError(
|
|
444
|
+
context === 'unsafe'
|
|
445
|
+
? 'A value given to this skill or command would land inside a here-document or a ${...} or arithmetic expansion of a shell command, where it cannot be quoted, so it was not loaded.'
|
|
446
|
+
: 'A value given to this skill or command contains a backtick, which cannot be put inside a shell command, so it was not loaded.',
|
|
447
|
+
'injection',
|
|
448
|
+
)
|
|
449
|
+
}
|
|
450
|
+
if (context === 'single') return value.replaceAll("'", "'\\''")
|
|
451
|
+
if (context === 'double') return value.replace(/[\\"$]/g, '\\$&')
|
|
452
|
+
return typedWords(value).map(shellQuoteWord).join(' ')
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
interface Occurrence {
|
|
456
|
+
readonly token: string
|
|
457
|
+
/** Where the value goes in the neutral text. */
|
|
458
|
+
readonly at: number
|
|
459
|
+
readonly before: string
|
|
460
|
+
readonly after: string
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
/**
|
|
464
|
+
* Put values into a template, each escaped, and prove no value added a
|
|
465
|
+
* command. `values` maps the token written in the text (`$ARGUMENTS`) to its
|
|
466
|
+
* value.
|
|
467
|
+
*
|
|
468
|
+
* A value outside a command is rewritten so that no shape of it begins one
|
|
469
|
+
* ({@link escapeInlineShell}). A value inside a command is quoted so the shell
|
|
470
|
+
* reads it as data ({@link quoteForCommand}), by where it sits: a person's
|
|
471
|
+
* `-n 5` stays two arguments, and `x; touch f #` stays words of an argument
|
|
472
|
+
* list. Throws {@link InlineShellError} when a value cannot be made safe, or the
|
|
473
|
+
* substituted text holds more commands than the template does.
|
|
474
|
+
*/
|
|
475
|
+
export function substituteEscaped(
|
|
476
|
+
template: string,
|
|
477
|
+
values: Readonly<Record<string, string>>,
|
|
478
|
+
options: { readonly dialect?: ShellDialect } = {},
|
|
479
|
+
): string {
|
|
480
|
+
const normalized = normalizeInlineShellText(template)
|
|
481
|
+
const tokens = Object.keys(values).filter((token) => token.length > 0)
|
|
482
|
+
if (tokens.length === 0) return normalized
|
|
483
|
+
// Longest first, so `$ARGUMENTS_X` is never read as `$ARGUMENTS` plus a tail.
|
|
484
|
+
tokens.sort((a, b) => b.length - a.length)
|
|
485
|
+
// The template with a word where each value goes: the commands the author wrote.
|
|
486
|
+
let neutral = ''
|
|
487
|
+
const pieces: Array<string | Occurrence> = []
|
|
488
|
+
let text = ''
|
|
489
|
+
let i = 0
|
|
490
|
+
while (i < normalized.length) {
|
|
491
|
+
const token = tokens.find((candidate) => normalized.startsWith(candidate, i))
|
|
492
|
+
if (token === undefined) {
|
|
493
|
+
text += normalized[i]
|
|
494
|
+
neutral += normalized[i]
|
|
495
|
+
i += 1
|
|
496
|
+
continue
|
|
497
|
+
}
|
|
498
|
+
if (text !== '') pieces.push(text)
|
|
499
|
+
text = ''
|
|
500
|
+
pieces.push({
|
|
501
|
+
token,
|
|
502
|
+
at: neutral.length,
|
|
503
|
+
before: normalized[i - 1] ?? '',
|
|
504
|
+
after: normalized[i + token.length] ?? '',
|
|
505
|
+
})
|
|
506
|
+
neutral += 'x'
|
|
507
|
+
i += token.length
|
|
508
|
+
}
|
|
509
|
+
if (text !== '') pieces.push(text)
|
|
510
|
+
const markers = findInlineShell(neutral)
|
|
511
|
+
const commandAt = (at: number): number | undefined => {
|
|
512
|
+
const marker = markers.find((m) => m.start <= at && at < m.end)
|
|
513
|
+
if (marker === undefined) return undefined
|
|
514
|
+
return marker.kind === 'inline' ? marker.start + 2 : neutral.indexOf('\n', marker.start) + 1
|
|
515
|
+
}
|
|
516
|
+
let out = ''
|
|
517
|
+
for (const piece of pieces) {
|
|
518
|
+
if (typeof piece === 'string') {
|
|
519
|
+
out += piece
|
|
520
|
+
continue
|
|
521
|
+
}
|
|
522
|
+
const value = values[piece.token] as string
|
|
523
|
+
const from = commandAt(piece.at)
|
|
524
|
+
out +=
|
|
525
|
+
from === undefined
|
|
526
|
+
? escapeBetween(value, piece.before, piece.after)
|
|
527
|
+
: quoteForCommand(
|
|
528
|
+
value,
|
|
529
|
+
neutral.slice(from, piece.at),
|
|
530
|
+
options.dialect ?? hostCommandShell().dialect,
|
|
531
|
+
)
|
|
532
|
+
}
|
|
533
|
+
if (findInlineShell(out).length > markers.length) {
|
|
534
|
+
throw new InlineShellError(
|
|
535
|
+
'A value given to this skill or command contains text that would start a shell command, so it was not loaded.',
|
|
536
|
+
'injection',
|
|
537
|
+
)
|
|
538
|
+
}
|
|
539
|
+
return out
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
export const SHELL_OFF_PLACEHOLDER =
|
|
543
|
+
'[shell commands are turned off (disableSkillShellExecution), so this command was not run]'
|
|
544
|
+
export const SHELL_REMOTE_PLACEHOLDER =
|
|
545
|
+
'[shell commands are not run in text that comes from a tool server or another remote source, so this command was not run]'
|
|
546
|
+
export const SHELL_UNTRUSTED_PLACEHOLDER =
|
|
547
|
+
'[shell commands are not run until this folder is trusted, so this command was not run]'
|
|
548
|
+
|
|
549
|
+
export const SHELL_PRELOAD_PLACEHOLDER =
|
|
550
|
+
'[shell commands in a skill preloaded into an agent are not run when the agent is set up, so this command was not run]'
|
|
551
|
+
|
|
552
|
+
/** Why a command in this text may not run at all, before any is tried. */
|
|
553
|
+
export type InlineShellBlock = 'off' | 'remote' | 'untrusted' | 'preload'
|
|
554
|
+
|
|
555
|
+
const PLACEHOLDERS: Readonly<Record<InlineShellBlock, string>> = {
|
|
556
|
+
off: SHELL_OFF_PLACEHOLDER,
|
|
557
|
+
remote: SHELL_REMOTE_PLACEHOLDER,
|
|
558
|
+
untrusted: SHELL_UNTRUSTED_PLACEHOLDER,
|
|
559
|
+
preload: SHELL_PRELOAD_PLACEHOLDER,
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
/** What a hook said about one command. */
|
|
563
|
+
export type InlineShellHookVerdict =
|
|
564
|
+
| { readonly kind: 'run'; readonly warning?: string }
|
|
565
|
+
/** Not this command; the rest of the text loads. */
|
|
566
|
+
| { readonly kind: 'refuse'; readonly reason?: string; readonly warning?: string }
|
|
567
|
+
/** The whole load stops. */
|
|
568
|
+
| { readonly kind: 'stop'; readonly reason: string }
|
|
569
|
+
|
|
570
|
+
/** How the host ended one command. */
|
|
571
|
+
export type InlineShellOutcome =
|
|
572
|
+
/** The permission rules said no, or would have asked a person who is not there. */
|
|
573
|
+
| { readonly kind: 'denied'; readonly reason: string }
|
|
574
|
+
/** The hook, which runs after permission and before the shell, refused or stopped. */
|
|
575
|
+
| { readonly kind: 'hook'; readonly verdict: Exclude<InlineShellHookVerdict, { kind: 'run' }> }
|
|
576
|
+
| {
|
|
577
|
+
readonly kind: 'ran'
|
|
578
|
+
readonly stdout: string
|
|
579
|
+
readonly stderr: string
|
|
580
|
+
/** Zero is success. `null` when the command did not report one. */
|
|
581
|
+
readonly exitCode: number | null
|
|
582
|
+
readonly interrupted?: boolean
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
export interface InlineShellHost {
|
|
586
|
+
/**
|
|
587
|
+
* Run one command: check permission, then call `beforeRun`, then start it.
|
|
588
|
+
* `beforeRun` must be called exactly when the permission check has allowed
|
|
589
|
+
* the command and the command has not started, and a verdict other than
|
|
590
|
+
* `run` must end the call with `{ kind: 'hook', verdict }`.
|
|
591
|
+
*/
|
|
592
|
+
run(
|
|
593
|
+
command: string,
|
|
594
|
+
beforeRun: () => Promise<InlineShellHookVerdict>,
|
|
595
|
+
): Promise<InlineShellOutcome>
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
export interface InlineShellOptions {
|
|
599
|
+
readonly host: InlineShellHost
|
|
600
|
+
/** The skill or command, for the line a warning names. */
|
|
601
|
+
readonly commandName: string
|
|
602
|
+
/** Set when no command may run; each is replaced by the matching placeholder. */
|
|
603
|
+
readonly block?: InlineShellBlock
|
|
604
|
+
/** Absent when no hook is attached for the event: nothing is asked. */
|
|
605
|
+
readonly hook?: (command: string) => Promise<InlineShellHookVerdict>
|
|
606
|
+
/** A line for the person, for a refused command and a warning from a hook. */
|
|
607
|
+
readonly warn?: (line: string) => void
|
|
608
|
+
readonly signal?: AbortSignal
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
/** `/name` for a line a person reads. */
|
|
612
|
+
function shown(name: string): string {
|
|
613
|
+
return `/${name}`
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
function cut(text: string, max: number): string {
|
|
617
|
+
const flat = text.replace(/\s+/g, ' ').trim()
|
|
618
|
+
return flat.length > max ? `${flat.slice(0, max - 1)}…` : flat
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
/** A command as one short line: its first line, and how many more lines a fenced block holds. */
|
|
622
|
+
function commandLine(command: string): string {
|
|
623
|
+
const lines = command
|
|
624
|
+
.split('\n')
|
|
625
|
+
.map((line) => line.trim())
|
|
626
|
+
.filter((line) => line !== '')
|
|
627
|
+
const head = cut(lines[0] ?? '', INLINE_SHELL_COMMAND_SHOWN_CHARS)
|
|
628
|
+
const more = lines.length - 1
|
|
629
|
+
return more > 0 ? `${head} (+${more} more line${more === 1 ? '' : 's'})` : head
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
/** Trimmed standard output, then a `[stderr]` block when there is any standard error. */
|
|
633
|
+
export function shapeShellOutput(stdout: string, stderr: string): string {
|
|
634
|
+
const out = stdout.trim()
|
|
635
|
+
const err = stderr.trim()
|
|
636
|
+
if (err === '') return out
|
|
637
|
+
return out === '' ? `[stderr]\n${err}` : `${out}\n[stderr]\n${err}`
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
/**
|
|
641
|
+
* Run every command in `text` and put each one's output where it was written.
|
|
642
|
+
*
|
|
643
|
+
* Commands run one after another, in order. A load fails (throws
|
|
644
|
+
* {@link InlineShellError}) on: more than 256 commands, a command over 32 KB,
|
|
645
|
+
* more than 4 MB of output in all, a permission refusal, a non-zero exit, an
|
|
646
|
+
* interrupt, or a hook's request to stop. A hook that refuses one command
|
|
647
|
+
* leaves `[not run: blocked by hook: <reason>]` in its place and loads the rest.
|
|
648
|
+
*/
|
|
649
|
+
export async function expandInlineShell(
|
|
650
|
+
text: string,
|
|
651
|
+
options: InlineShellOptions,
|
|
652
|
+
): Promise<string> {
|
|
653
|
+
const source = normalizeInlineShellText(text)
|
|
654
|
+
const markers = findInlineShell(source)
|
|
655
|
+
if (markers.length === 0) return source
|
|
656
|
+
if (options.block !== undefined) {
|
|
657
|
+
const placeholder = PLACEHOLDERS[options.block]
|
|
658
|
+
return replaceMarkers(source, markers, () => placeholder)
|
|
659
|
+
}
|
|
660
|
+
if (markers.length > INLINE_SHELL_MAX_COMMANDS) {
|
|
661
|
+
throw new InlineShellError(
|
|
662
|
+
`${shown(options.commandName)} has ${markers.length} shell commands, and at most ${INLINE_SHELL_MAX_COMMANDS} may run while it loads.`,
|
|
663
|
+
'limit',
|
|
664
|
+
)
|
|
665
|
+
}
|
|
666
|
+
for (const marker of markers) {
|
|
667
|
+
if (marker.command.length > INLINE_SHELL_MAX_COMMAND_CHARS) {
|
|
668
|
+
throw new InlineShellError(
|
|
669
|
+
`A shell command in ${shown(options.commandName)} is ${marker.command.length} characters, and at most ${INLINE_SHELL_MAX_COMMAND_CHARS} may run while it loads.`,
|
|
670
|
+
'limit',
|
|
671
|
+
)
|
|
672
|
+
}
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
const replacements: string[] = []
|
|
676
|
+
let total = 0
|
|
677
|
+
for (const marker of markers) {
|
|
678
|
+
options.signal?.throwIfAborted()
|
|
679
|
+
const pattern = commandLine(marker.command)
|
|
680
|
+
const beforeRun = async (): Promise<InlineShellHookVerdict> => {
|
|
681
|
+
if (options.hook === undefined) return { kind: 'run' }
|
|
682
|
+
return options.hook(marker.command)
|
|
683
|
+
}
|
|
684
|
+
const outcome = await options.host.run(marker.command, beforeRun)
|
|
685
|
+
if (outcome.kind === 'denied') {
|
|
686
|
+
throw new InlineShellError(
|
|
687
|
+
`Shell command permission check failed for pattern "${pattern}": ${outcome.reason}`,
|
|
688
|
+
'permission',
|
|
689
|
+
)
|
|
690
|
+
}
|
|
691
|
+
if (outcome.kind === 'hook') {
|
|
692
|
+
const verdict = outcome.verdict
|
|
693
|
+
if (verdict.kind === 'stop') {
|
|
694
|
+
throw new InlineShellError(
|
|
695
|
+
`${shown(options.commandName)} was stopped by a hook: ${cut(verdict.reason, INLINE_SHELL_REASON_CHARS)}`,
|
|
696
|
+
'stopped',
|
|
697
|
+
)
|
|
698
|
+
}
|
|
699
|
+
const reason =
|
|
700
|
+
verdict.reason === undefined || verdict.reason.trim() === ''
|
|
701
|
+
? undefined
|
|
702
|
+
: cut(verdict.reason, INLINE_SHELL_REASON_CHARS)
|
|
703
|
+
if (verdict.warning) options.warn?.(verdict.warning)
|
|
704
|
+
options.warn?.(
|
|
705
|
+
`Not run in ${shown(options.commandName)}: ${pattern} (blocked by hook${reason ? `: ${reason}` : ''})`,
|
|
706
|
+
)
|
|
707
|
+
replacements.push(`[not run: blocked by hook${reason ? `: ${reason}` : ''}]`)
|
|
708
|
+
continue
|
|
709
|
+
}
|
|
710
|
+
if (outcome.interrupted === true) {
|
|
711
|
+
throw new InlineShellError(
|
|
712
|
+
`Shell command interrupted for pattern "${pattern}": [Command interrupted]`,
|
|
713
|
+
'interrupted',
|
|
714
|
+
)
|
|
715
|
+
}
|
|
716
|
+
if (outcome.exitCode !== 0) {
|
|
717
|
+
const output = shapeShellOutput(outcome.stdout, outcome.stderr)
|
|
718
|
+
throw new InlineShellError(
|
|
719
|
+
`Shell command failed for pattern "${pattern}": ${output === '' ? `exit code ${outcome.exitCode ?? 'unknown'}` : output}`,
|
|
720
|
+
'failed',
|
|
721
|
+
)
|
|
722
|
+
}
|
|
723
|
+
const shaped = shapeShellOutput(outcome.stdout, outcome.stderr)
|
|
724
|
+
total += shaped.length
|
|
725
|
+
if (total > INLINE_SHELL_MAX_OUTPUT_CHARS) {
|
|
726
|
+
throw new InlineShellError(
|
|
727
|
+
`The shell commands in ${shown(options.commandName)} printed more than ${INLINE_SHELL_MAX_OUTPUT_CHARS} characters in all, which is the most a load may take in.`,
|
|
728
|
+
'limit',
|
|
729
|
+
)
|
|
730
|
+
}
|
|
731
|
+
replacements.push(shaped)
|
|
732
|
+
}
|
|
733
|
+
let n = 0
|
|
734
|
+
return replaceMarkers(source, markers, () => replacements[n++] as string)
|
|
735
|
+
}
|
|
736
|
+
|
|
737
|
+
function replaceMarkers(
|
|
738
|
+
source: string,
|
|
739
|
+
markers: readonly InlineShellMarker[],
|
|
740
|
+
replacement: (marker: InlineShellMarker) => string,
|
|
741
|
+
): string {
|
|
742
|
+
let out = ''
|
|
743
|
+
let at = 0
|
|
744
|
+
for (const marker of markers) {
|
|
745
|
+
out += source.slice(at, marker.start) + replacement(marker)
|
|
746
|
+
at = marker.end
|
|
747
|
+
}
|
|
748
|
+
return out + source.slice(at)
|
|
749
|
+
}
|
|
750
|
+
|
|
751
|
+
/**
|
|
752
|
+
* Turn the text of a bash tool result into its two streams. The tool labels
|
|
753
|
+
* them `STDOUT:` and `STDERR:`; a result with neither label is all standard
|
|
754
|
+
* output, and `(no output)` is nothing.
|
|
755
|
+
*/
|
|
756
|
+
export function splitShellToolOutput(output: string): { stdout: string; stderr: string } {
|
|
757
|
+
if (output === '(no output)') return { stdout: '', stderr: '' }
|
|
758
|
+
if (output.startsWith('STDERR:\n'))
|
|
759
|
+
return { stdout: '', stderr: output.slice('STDERR:\n'.length) }
|
|
760
|
+
if (!output.startsWith('STDOUT:\n')) return { stdout: output, stderr: '' }
|
|
761
|
+
const body = output.slice('STDOUT:\n'.length)
|
|
762
|
+
const split = body.indexOf('\n\nSTDERR:\n')
|
|
763
|
+
if (split === -1) return { stdout: body, stderr: '' }
|
|
764
|
+
return { stdout: body.slice(0, split), stderr: body.slice(split + '\n\nSTDERR:\n'.length) }
|
|
765
|
+
}
|