agent-sh 0.2.0 → 0.3.1

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.
Files changed (42) hide show
  1. package/README.md +21 -0
  2. package/dist/acp-client.d.ts +24 -0
  3. package/dist/acp-client.js +155 -33
  4. package/dist/context-manager.d.ts +5 -3
  5. package/dist/context-manager.js +62 -31
  6. package/dist/core.js +10 -0
  7. package/dist/event-bus.d.ts +26 -0
  8. package/dist/event-bus.js +10 -0
  9. package/dist/extension-loader.js +3 -14
  10. package/dist/extensions/shell-exec.js +27 -22
  11. package/dist/extensions/tui-renderer.d.ts +1 -1
  12. package/dist/extensions/tui-renderer.js +369 -126
  13. package/dist/index.js +184 -37
  14. package/dist/input-handler.d.ts +10 -0
  15. package/dist/input-handler.js +169 -10
  16. package/dist/mcp-server.js +37 -8
  17. package/dist/settings.d.ts +44 -0
  18. package/dist/settings.js +61 -0
  19. package/dist/shell.d.ts +1 -0
  20. package/dist/shell.js +44 -4
  21. package/dist/types.d.ts +17 -0
  22. package/dist/utils/ansi.d.ts +4 -1
  23. package/dist/utils/ansi.js +60 -2
  24. package/dist/utils/box-frame.js +2 -1
  25. package/dist/utils/diff-renderer.js +1 -1
  26. package/dist/utils/frame-renderer.d.ts +26 -0
  27. package/dist/utils/frame-renderer.js +76 -0
  28. package/dist/utils/handler-registry.d.ts +41 -0
  29. package/dist/utils/handler-registry.js +52 -0
  30. package/dist/utils/line-editor.d.ts +21 -1
  31. package/dist/utils/line-editor.js +193 -99
  32. package/dist/utils/markdown.d.ts +15 -6
  33. package/dist/utils/markdown.js +106 -67
  34. package/dist/utils/output-writer.d.ts +22 -0
  35. package/dist/utils/output-writer.js +29 -0
  36. package/dist/utils/stream-transform.d.ts +70 -0
  37. package/dist/utils/stream-transform.js +229 -0
  38. package/dist/utils/tool-display.d.ts +11 -8
  39. package/dist/utils/tool-display.js +69 -46
  40. package/examples/extensions/latex-images.ts +142 -0
  41. package/examples/pi-agent-sh.ts +166 -0
  42. package/package.json +10 -2
@@ -0,0 +1,166 @@
1
+ /**
2
+ * Pi extension: agent-sh tools (user_shell + shell_recall).
3
+ *
4
+ * When running inside agent-sh, registers tools that communicate with
5
+ * the user's live terminal via a Unix domain socket (JSON-RPC 2.0).
6
+ *
7
+ * - user_shell: execute commands in the user's live PTY
8
+ * - shell_recall: search/expand/browse session exchange history
9
+ *
10
+ * Socket path comes from the AGENT_SH_SOCKET env var.
11
+ * When not running inside agent-sh, the extension silently does nothing.
12
+ */
13
+
14
+ import type { ExtensionAPI } from "@mariozechner/pi-coding-agent";
15
+ import { Type } from "@sinclair/typebox";
16
+ import { createConnection } from "node:net";
17
+
18
+ const SOCKET_PATH = process.env.AGENT_SH_SOCKET;
19
+
20
+ export default function (pi: ExtensionAPI): void {
21
+ if (!SOCKET_PATH) return; // Not running inside agent-sh
22
+
23
+ pi.registerTool({
24
+ name: "shell_cwd",
25
+ label: "Shell CWD",
26
+ description:
27
+ "Get the user's current working directory in their live shell. " +
28
+ "IMPORTANT: Your internal working directory may differ from the user's actual shell cwd — " +
29
+ "the user may have cd'd after your session started. Call this tool to get the real cwd " +
30
+ "before file operations if you're unsure.",
31
+ promptSnippet:
32
+ "Get the user's real shell cwd (may differ from your internal cwd).",
33
+ parameters: Type.Object({}),
34
+
35
+ async execute() {
36
+ const result = (await callSocket("shell/cwd", {})) as { cwd: string };
37
+ return {
38
+ content: [{ type: "text", text: `User's current working directory: ${result.cwd}` }],
39
+ };
40
+ },
41
+ });
42
+
43
+ pi.registerTool({
44
+ name: "user_shell",
45
+ label: "User Shell",
46
+ description:
47
+ "Execute a command in the user's live terminal session. " +
48
+ "Use this for commands that should affect the user's shell state: " +
49
+ "cd, export, source, pushd/popd, alias, etc. " +
50
+ "The command runs in the user's actual shell with their full environment " +
51
+ "(aliases, functions, PATH), not an isolated subprocess. " +
52
+ "NOTE: Your internal cwd may be stale — the user may have cd'd. " +
53
+ "Check the shell context for [shell cwd:...] labels or call shell_cwd " +
54
+ "to determine the real working directory. Use absolute paths when possible.",
55
+ promptSnippet:
56
+ "Run commands in the user's live shell (cd, export, source — affects their session). " +
57
+ "Your internal cwd may be stale — check shell context or use shell_cwd for the real cwd.",
58
+ parameters: Type.Object({
59
+ command: Type.String({ description: "Shell command to execute in the user's live terminal" }),
60
+ }),
61
+
62
+ async execute(_toolCallId, params) {
63
+ const result = (await callSocket("shell/exec", { command: params.command })) as {
64
+ output: string;
65
+ cwd: string;
66
+ };
67
+ return {
68
+ content: [{ type: "text", text: result.output || "(no output)" }],
69
+ };
70
+ },
71
+ });
72
+
73
+ pi.registerTool({
74
+ name: "shell_recall",
75
+ label: "Shell Recall",
76
+ description:
77
+ "Retrieve past shell commands, agent responses, and tool executions from the session history. " +
78
+ "Use this to look up truncated output, search for previous commands or errors, " +
79
+ "or browse recent exchanges. Each entry shows [shell cwd:...] so you can see " +
80
+ "which directory commands were run in. Operations: " +
81
+ '"browse" lists recent exchange summaries with line counts, ' +
82
+ '"search" finds exchanges matching a regex query, ' +
83
+ '"expand" retrieves content by exchange ID (use start/end for specific line ranges).',
84
+ promptSnippet:
85
+ "Look up session history — search past commands/output, expand truncated exchanges, or browse recent activity.",
86
+ parameters: Type.Object({
87
+ operation: Type.Optional(
88
+ Type.Union([Type.Literal("search"), Type.Literal("expand"), Type.Literal("browse")], {
89
+ description: 'Operation to perform (default: "browse")',
90
+ }),
91
+ ),
92
+ query: Type.Optional(
93
+ Type.String({ description: 'Search query — supports regex (required for "search")' }),
94
+ ),
95
+ ids: Type.Optional(
96
+ Type.Array(Type.Number(), {
97
+ description: 'Exchange IDs to expand (required for "expand")',
98
+ }),
99
+ ),
100
+ start: Type.Optional(
101
+ Type.Number({ description: "Start line number, 1-indexed (optional, for expand)" }),
102
+ ),
103
+ end: Type.Optional(
104
+ Type.Number({ description: "End line number, inclusive (optional, for expand)" }),
105
+ ),
106
+ }),
107
+
108
+ async execute(_toolCallId, params) {
109
+ const result = (await callSocket("shell/recall", {
110
+ operation: params.operation || "browse",
111
+ query: params.query,
112
+ ids: params.ids,
113
+ start: params.start,
114
+ end: params.end,
115
+ })) as { result: string };
116
+ return {
117
+ content: [{ type: "text", text: result.result || "(no results)" }],
118
+ };
119
+ },
120
+ });
121
+ }
122
+
123
+ // -- agent-sh socket client (JSON-RPC 2.0) --
124
+
125
+ let rpcId = 0;
126
+
127
+ function callSocket(method: string, params?: Record<string, unknown>): Promise<unknown> {
128
+ return new Promise((resolve, reject) => {
129
+ const conn = createConnection(SOCKET_PATH!);
130
+ let buffer = "";
131
+
132
+ conn.on("connect", () => {
133
+ const msg = { jsonrpc: "2.0", id: ++rpcId, method, params: params ?? {} };
134
+ conn.write(JSON.stringify(msg) + "\n");
135
+ });
136
+
137
+ conn.on("data", (chunk) => {
138
+ buffer += chunk.toString();
139
+ const newlineIdx = buffer.indexOf("\n");
140
+ if (newlineIdx === -1) return;
141
+
142
+ const line = buffer.slice(0, newlineIdx).trim();
143
+ conn.destroy();
144
+
145
+ try {
146
+ const response = JSON.parse(line);
147
+ if (response.error) {
148
+ reject(new Error(response.error.message || "RPC error"));
149
+ } else {
150
+ resolve(response.result);
151
+ }
152
+ } catch {
153
+ reject(new Error(`Invalid response from agent-sh: ${line}`));
154
+ }
155
+ });
156
+
157
+ conn.on("error", (err) => {
158
+ reject(new Error(`Failed to connect to agent-sh: ${err.message}`));
159
+ });
160
+
161
+ conn.setTimeout(35_000, () => {
162
+ conn.destroy();
163
+ reject(new Error("Connection to agent-sh timed out"));
164
+ });
165
+ });
166
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agent-sh",
3
- "version": "0.2.0",
3
+ "version": "0.3.1",
4
4
  "description": "A shell-first terminal where any ACP-compatible AI agent is one keystroke away",
5
5
  "type": "module",
6
6
  "main": "dist/core.js",
@@ -17,10 +17,18 @@
17
17
  "types": "./dist/core.d.ts",
18
18
  "default": "./dist/core.js"
19
19
  },
20
- "./utils/*": "./dist/utils/*",
20
+ "./utils/*": "./dist/utils/*.js",
21
21
  "./types": {
22
22
  "types": "./dist/types.d.ts",
23
23
  "default": "./dist/types.js"
24
+ },
25
+ "./settings": {
26
+ "types": "./dist/settings.d.ts",
27
+ "default": "./dist/settings.js"
28
+ },
29
+ "./utils/stream-transform": {
30
+ "types": "./dist/utils/stream-transform.d.ts",
31
+ "default": "./dist/utils/stream-transform.js"
24
32
  }
25
33
  },
26
34
  "files": [