char-slop/berry

multipurpose self-modifiable agentic harness

git clone https://git.t4t.associates/char-slop/berry

Charlotte Somwindows: add terminal support via ConPTY + PowerShell0853dde

main
1.4 KiB38 linesraw
1import { BACKGROUND_CONTEXT } from "@earendil-works/chord/context";
2import type { Environment } from "@berry/api";
3
4/** An interactive shell on the target, preferring PowerShell 7 to Windows PowerShell. */
5export async function shell(env: Environment): Promise<string[]> {
6  if (env.platform !== "win32") return [env.variables.SHELL || "/bin/sh", "-l"];
7
8  const variables = Object.fromEntries(
9    Object.entries(env.variables).map(([key, value]) => [key.toUpperCase(), value]),
10  );
11  const onPath = (name: string) =>
12    (variables.PATH ?? "").split(";").filter(Boolean).flatMap((dir) =>
13      (variables.PATHEXT ?? ".EXE").split(";").filter(Boolean).map((ext) => [
14        dir.replace(/^"(.*)"$/, "$1"),
15        name + ext,
16      ])
17    );
18  const candidates = [
19    ...onPath("pwsh"),
20    ...[variables.PROGRAMFILES, variables.PROGRAMW6432].filter(Boolean).map((dir) => [
21      dir,
22      "PowerShell",
23      "7",
24      "pwsh.exe",
25    ]),
26    ...onPath("powershell"),
27    ...(variables.SYSTEMROOT
28      ? [[variables.SYSTEMROOT, "System32", "WindowsPowerShell", "v1.0", "powershell.exe"]]
29      : []),
30  ];
31  for (const parts of candidates) {
32    const path = await env.joinPath(parts, BACKGROUND_CONTEXT);
33    if (!path.ok) continue;
34    const info = await env.fileInfo(path.value, BACKGROUND_CONTEXT);
35    if (info.ok && info.value.kind !== "directory") return [path.value, "-NoLogo"];
36  }
37  throw new Error("PowerShell not found. Install PowerShell 7 or add it to PATH.");
38}