@mariozechner/pi-mom 0.9.4

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 (47) hide show
  1. package/CHANGELOG.md +17 -0
  2. package/README.md +183 -0
  3. package/dist/agent.d.ts +9 -0
  4. package/dist/agent.d.ts.map +1 -0
  5. package/dist/agent.js +248 -0
  6. package/dist/agent.js.map +1 -0
  7. package/dist/main.d.ts +3 -0
  8. package/dist/main.d.ts.map +1 -0
  9. package/dist/main.js +125 -0
  10. package/dist/main.js.map +1 -0
  11. package/dist/sandbox.d.ts +34 -0
  12. package/dist/sandbox.d.ts.map +1 -0
  13. package/dist/sandbox.js +183 -0
  14. package/dist/sandbox.js.map +1 -0
  15. package/dist/slack.d.ts +46 -0
  16. package/dist/slack.d.ts.map +1 -0
  17. package/dist/slack.js +208 -0
  18. package/dist/slack.js.map +1 -0
  19. package/dist/store.d.ts +52 -0
  20. package/dist/store.d.ts.map +1 -0
  21. package/dist/store.js +124 -0
  22. package/dist/store.js.map +1 -0
  23. package/dist/tools/attach.d.ts +10 -0
  24. package/dist/tools/attach.d.ts.map +1 -0
  25. package/dist/tools/attach.js +34 -0
  26. package/dist/tools/attach.js.map +1 -0
  27. package/dist/tools/bash.d.ts +10 -0
  28. package/dist/tools/bash.d.ts.map +1 -0
  29. package/dist/tools/bash.js +30 -0
  30. package/dist/tools/bash.js.map +1 -0
  31. package/dist/tools/edit.d.ts +11 -0
  32. package/dist/tools/edit.d.ts.map +1 -0
  33. package/dist/tools/edit.js +131 -0
  34. package/dist/tools/edit.js.map +1 -0
  35. package/dist/tools/index.d.ts +5 -0
  36. package/dist/tools/index.d.ts.map +1 -0
  37. package/dist/tools/index.js +16 -0
  38. package/dist/tools/index.js.map +1 -0
  39. package/dist/tools/read.d.ts +11 -0
  40. package/dist/tools/read.d.ts.map +1 -0
  41. package/dist/tools/read.js +102 -0
  42. package/dist/tools/read.js.map +1 -0
  43. package/dist/tools/write.d.ts +10 -0
  44. package/dist/tools/write.d.ts.map +1 -0
  45. package/dist/tools/write.js +33 -0
  46. package/dist/tools/write.js.map +1 -0
  47. package/package.json +52 -0
@@ -0,0 +1,52 @@
1
+ export interface Attachment {
2
+ original: string;
3
+ local: string;
4
+ }
5
+ export interface LoggedMessage {
6
+ ts: string;
7
+ user: string;
8
+ userName?: string;
9
+ displayName?: string;
10
+ text: string;
11
+ attachments: Attachment[];
12
+ isBot: boolean;
13
+ }
14
+ export interface ChannelStoreConfig {
15
+ workingDir: string;
16
+ botToken: string;
17
+ }
18
+ export declare class ChannelStore {
19
+ private workingDir;
20
+ private botToken;
21
+ private pendingDownloads;
22
+ private isDownloading;
23
+ constructor(config: ChannelStoreConfig);
24
+ /**
25
+ * Get or create the directory for a channel/DM
26
+ */
27
+ getChannelDir(channelId: string): string;
28
+ /**
29
+ * Generate a unique local filename for an attachment
30
+ */
31
+ generateLocalFilename(originalName: string, timestamp: string): string;
32
+ /**
33
+ * Process attachments from a Slack message event
34
+ * Returns attachment metadata and queues downloads
35
+ */
36
+ processAttachments(channelId: string, files: Array<{
37
+ name: string;
38
+ url_private_download?: string;
39
+ url_private?: string;
40
+ }>, timestamp: string): Attachment[];
41
+ /**
42
+ * Log a message to the channel's log.jsonl
43
+ */
44
+ logMessage(channelId: string, message: LoggedMessage): Promise<void>;
45
+ /**
46
+ * Log a bot response
47
+ */
48
+ logBotResponse(channelId: string, text: string, ts: string): Promise<void>;
49
+ private processDownloadQueue;
50
+ private downloadAttachment;
51
+ }
52
+ //# sourceMappingURL=store.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"store.d.ts","sourceRoot":"","sources":["../src/store.ts"],"names":[],"mappings":"AAIA,MAAM,WAAW,UAAU;IAC1B,QAAQ,EAAE,MAAM,CAAC;IACjB,KAAK,EAAE,MAAM,CAAC;CACd;AAED,MAAM,WAAW,aAAa;IAC7B,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,UAAU,EAAE,CAAC;IAC1B,KAAK,EAAE,OAAO,CAAC;CACf;AAED,MAAM,WAAW,kBAAkB;IAClC,UAAU,EAAE,MAAM,CAAC;IACnB,QAAQ,EAAE,MAAM,CAAC;CACjB;AAQD,qBAAa,YAAY;IACxB,OAAO,CAAC,UAAU,CAAS;IAC3B,OAAO,CAAC,QAAQ,CAAS;IACzB,OAAO,CAAC,gBAAgB,CAAyB;IACjD,OAAO,CAAC,aAAa,CAAS;IAE9B,YAAY,MAAM,EAAE,kBAAkB,EAQrC;IAED;;OAEG;IACH,aAAa,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM,CAMvC;IAED;;OAEG;IACH,qBAAqB,CAAC,YAAY,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,MAAM,CAMrE;IAED;;;OAGG;IACH,kBAAkB,CACjB,SAAS,EAAE,MAAM,EACjB,KAAK,EAAE,KAAK,CAAC;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,oBAAoB,CAAC,EAAE,MAAM,CAAC;QAAC,WAAW,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC,EACnF,SAAS,EAAE,MAAM,GACf,UAAU,EAAE,CAuBd;IAED;;OAEG;IACG,UAAU,CAAC,SAAS,EAAE,MAAM,EAAE,OAAO,EAAE,aAAa,GAAG,OAAO,CAAC,IAAI,CAAC,CAIzE;IAED;;OAEG;IACG,cAAc,CAAC,SAAS,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAQ/E;YAKa,oBAAoB;YAwBpB,kBAAkB;CAsBhC","sourcesContent":["import { existsSync, mkdirSync } from \"fs\";\nimport { appendFile, writeFile } from \"fs/promises\";\nimport { join } from \"path\";\n\nexport interface Attachment {\n\toriginal: string; // original filename from uploader\n\tlocal: string; // path relative to working dir (e.g., \"C12345/attachments/1732531234567_file.png\")\n}\n\nexport interface LoggedMessage {\n\tts: string; // slack timestamp\n\tuser: string; // user ID (or \"bot\" for bot responses)\n\tuserName?: string; // handle (e.g., \"mario\")\n\tdisplayName?: string; // display name (e.g., \"Mario Zechner\")\n\ttext: string;\n\tattachments: Attachment[];\n\tisBot: boolean;\n}\n\nexport interface ChannelStoreConfig {\n\tworkingDir: string;\n\tbotToken: string; // needed for authenticated file downloads\n}\n\ninterface PendingDownload {\n\tchannelId: string;\n\tlocalPath: string; // relative path\n\turl: string;\n}\n\nexport class ChannelStore {\n\tprivate workingDir: string;\n\tprivate botToken: string;\n\tprivate pendingDownloads: PendingDownload[] = [];\n\tprivate isDownloading = false;\n\n\tconstructor(config: ChannelStoreConfig) {\n\t\tthis.workingDir = config.workingDir;\n\t\tthis.botToken = config.botToken;\n\n\t\t// Ensure working directory exists\n\t\tif (!existsSync(this.workingDir)) {\n\t\t\tmkdirSync(this.workingDir, { recursive: true });\n\t\t}\n\t}\n\n\t/**\n\t * Get or create the directory for a channel/DM\n\t */\n\tgetChannelDir(channelId: string): string {\n\t\tconst dir = join(this.workingDir, channelId);\n\t\tif (!existsSync(dir)) {\n\t\t\tmkdirSync(dir, { recursive: true });\n\t\t}\n\t\treturn dir;\n\t}\n\n\t/**\n\t * Generate a unique local filename for an attachment\n\t */\n\tgenerateLocalFilename(originalName: string, timestamp: string): string {\n\t\t// Convert slack timestamp (1234567890.123456) to milliseconds\n\t\tconst ts = Math.floor(parseFloat(timestamp) * 1000);\n\t\t// Sanitize original name (remove problematic characters)\n\t\tconst sanitized = originalName.replace(/[^a-zA-Z0-9._-]/g, \"_\");\n\t\treturn `${ts}_${sanitized}`;\n\t}\n\n\t/**\n\t * Process attachments from a Slack message event\n\t * Returns attachment metadata and queues downloads\n\t */\n\tprocessAttachments(\n\t\tchannelId: string,\n\t\tfiles: Array<{ name: string; url_private_download?: string; url_private?: string }>,\n\t\ttimestamp: string,\n\t): Attachment[] {\n\t\tconst attachments: Attachment[] = [];\n\n\t\tfor (const file of files) {\n\t\t\tconst url = file.url_private_download || file.url_private;\n\t\t\tif (!url) continue;\n\n\t\t\tconst filename = this.generateLocalFilename(file.name, timestamp);\n\t\t\tconst localPath = `${channelId}/attachments/${filename}`;\n\n\t\t\tattachments.push({\n\t\t\t\toriginal: file.name,\n\t\t\t\tlocal: localPath,\n\t\t\t});\n\n\t\t\t// Queue for background download\n\t\t\tthis.pendingDownloads.push({ channelId, localPath, url });\n\t\t}\n\n\t\t// Trigger background download\n\t\tthis.processDownloadQueue();\n\n\t\treturn attachments;\n\t}\n\n\t/**\n\t * Log a message to the channel's log.jsonl\n\t */\n\tasync logMessage(channelId: string, message: LoggedMessage): Promise<void> {\n\t\tconst logPath = join(this.getChannelDir(channelId), \"log.jsonl\");\n\t\tconst line = JSON.stringify(message) + \"\\n\";\n\t\tawait appendFile(logPath, line, \"utf-8\");\n\t}\n\n\t/**\n\t * Log a bot response\n\t */\n\tasync logBotResponse(channelId: string, text: string, ts: string): Promise<void> {\n\t\tawait this.logMessage(channelId, {\n\t\t\tts,\n\t\t\tuser: \"bot\",\n\t\t\ttext,\n\t\t\tattachments: [],\n\t\t\tisBot: true,\n\t\t});\n\t}\n\n\t/**\n\t * Process the download queue in the background\n\t */\n\tprivate async processDownloadQueue(): Promise<void> {\n\t\tif (this.isDownloading || this.pendingDownloads.length === 0) return;\n\n\t\tthis.isDownloading = true;\n\n\t\twhile (this.pendingDownloads.length > 0) {\n\t\t\tconst item = this.pendingDownloads.shift();\n\t\t\tif (!item) break;\n\n\t\t\ttry {\n\t\t\t\tawait this.downloadAttachment(item.localPath, item.url);\n\t\t\t\tconsole.log(`Downloaded: ${item.localPath}`);\n\t\t\t} catch (error) {\n\t\t\t\tconsole.error(`Failed to download ${item.localPath}:`, error);\n\t\t\t\t// Could re-queue for retry here\n\t\t\t}\n\t\t}\n\n\t\tthis.isDownloading = false;\n\t}\n\n\t/**\n\t * Download a single attachment\n\t */\n\tprivate async downloadAttachment(localPath: string, url: string): Promise<void> {\n\t\tconst filePath = join(this.workingDir, localPath);\n\n\t\t// Ensure directory exists\n\t\tconst dir = join(this.workingDir, localPath.substring(0, localPath.lastIndexOf(\"/\")));\n\t\tif (!existsSync(dir)) {\n\t\t\tmkdirSync(dir, { recursive: true });\n\t\t}\n\n\t\tconst response = await fetch(url, {\n\t\t\theaders: {\n\t\t\t\tAuthorization: `Bearer ${this.botToken}`,\n\t\t\t},\n\t\t});\n\n\t\tif (!response.ok) {\n\t\t\tthrow new Error(`HTTP ${response.status}: ${response.statusText}`);\n\t\t}\n\n\t\tconst buffer = await response.arrayBuffer();\n\t\tawait writeFile(filePath, Buffer.from(buffer));\n\t}\n}\n"]}
package/dist/store.js ADDED
@@ -0,0 +1,124 @@
1
+ import { existsSync, mkdirSync } from "fs";
2
+ import { appendFile, writeFile } from "fs/promises";
3
+ import { join } from "path";
4
+ export class ChannelStore {
5
+ workingDir;
6
+ botToken;
7
+ pendingDownloads = [];
8
+ isDownloading = false;
9
+ constructor(config) {
10
+ this.workingDir = config.workingDir;
11
+ this.botToken = config.botToken;
12
+ // Ensure working directory exists
13
+ if (!existsSync(this.workingDir)) {
14
+ mkdirSync(this.workingDir, { recursive: true });
15
+ }
16
+ }
17
+ /**
18
+ * Get or create the directory for a channel/DM
19
+ */
20
+ getChannelDir(channelId) {
21
+ const dir = join(this.workingDir, channelId);
22
+ if (!existsSync(dir)) {
23
+ mkdirSync(dir, { recursive: true });
24
+ }
25
+ return dir;
26
+ }
27
+ /**
28
+ * Generate a unique local filename for an attachment
29
+ */
30
+ generateLocalFilename(originalName, timestamp) {
31
+ // Convert slack timestamp (1234567890.123456) to milliseconds
32
+ const ts = Math.floor(parseFloat(timestamp) * 1000);
33
+ // Sanitize original name (remove problematic characters)
34
+ const sanitized = originalName.replace(/[^a-zA-Z0-9._-]/g, "_");
35
+ return `${ts}_${sanitized}`;
36
+ }
37
+ /**
38
+ * Process attachments from a Slack message event
39
+ * Returns attachment metadata and queues downloads
40
+ */
41
+ processAttachments(channelId, files, timestamp) {
42
+ const attachments = [];
43
+ for (const file of files) {
44
+ const url = file.url_private_download || file.url_private;
45
+ if (!url)
46
+ continue;
47
+ const filename = this.generateLocalFilename(file.name, timestamp);
48
+ const localPath = `${channelId}/attachments/${filename}`;
49
+ attachments.push({
50
+ original: file.name,
51
+ local: localPath,
52
+ });
53
+ // Queue for background download
54
+ this.pendingDownloads.push({ channelId, localPath, url });
55
+ }
56
+ // Trigger background download
57
+ this.processDownloadQueue();
58
+ return attachments;
59
+ }
60
+ /**
61
+ * Log a message to the channel's log.jsonl
62
+ */
63
+ async logMessage(channelId, message) {
64
+ const logPath = join(this.getChannelDir(channelId), "log.jsonl");
65
+ const line = JSON.stringify(message) + "\n";
66
+ await appendFile(logPath, line, "utf-8");
67
+ }
68
+ /**
69
+ * Log a bot response
70
+ */
71
+ async logBotResponse(channelId, text, ts) {
72
+ await this.logMessage(channelId, {
73
+ ts,
74
+ user: "bot",
75
+ text,
76
+ attachments: [],
77
+ isBot: true,
78
+ });
79
+ }
80
+ /**
81
+ * Process the download queue in the background
82
+ */
83
+ async processDownloadQueue() {
84
+ if (this.isDownloading || this.pendingDownloads.length === 0)
85
+ return;
86
+ this.isDownloading = true;
87
+ while (this.pendingDownloads.length > 0) {
88
+ const item = this.pendingDownloads.shift();
89
+ if (!item)
90
+ break;
91
+ try {
92
+ await this.downloadAttachment(item.localPath, item.url);
93
+ console.log(`Downloaded: ${item.localPath}`);
94
+ }
95
+ catch (error) {
96
+ console.error(`Failed to download ${item.localPath}:`, error);
97
+ // Could re-queue for retry here
98
+ }
99
+ }
100
+ this.isDownloading = false;
101
+ }
102
+ /**
103
+ * Download a single attachment
104
+ */
105
+ async downloadAttachment(localPath, url) {
106
+ const filePath = join(this.workingDir, localPath);
107
+ // Ensure directory exists
108
+ const dir = join(this.workingDir, localPath.substring(0, localPath.lastIndexOf("/")));
109
+ if (!existsSync(dir)) {
110
+ mkdirSync(dir, { recursive: true });
111
+ }
112
+ const response = await fetch(url, {
113
+ headers: {
114
+ Authorization: `Bearer ${this.botToken}`,
115
+ },
116
+ });
117
+ if (!response.ok) {
118
+ throw new Error(`HTTP ${response.status}: ${response.statusText}`);
119
+ }
120
+ const buffer = await response.arrayBuffer();
121
+ await writeFile(filePath, Buffer.from(buffer));
122
+ }
123
+ }
124
+ //# sourceMappingURL=store.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"store.js","sourceRoot":"","sources":["../src/store.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,IAAI,CAAC;AAC3C,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACpD,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AA4B5B,MAAM,OAAO,YAAY;IAChB,UAAU,CAAS;IACnB,QAAQ,CAAS;IACjB,gBAAgB,GAAsB,EAAE,CAAC;IACzC,aAAa,GAAG,KAAK,CAAC;IAE9B,YAAY,MAA0B,EAAE;QACvC,IAAI,CAAC,UAAU,GAAG,MAAM,CAAC,UAAU,CAAC;QACpC,IAAI,CAAC,QAAQ,GAAG,MAAM,CAAC,QAAQ,CAAC;QAEhC,kCAAkC;QAClC,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,UAAU,CAAC,EAAE,CAAC;YAClC,SAAS,CAAC,IAAI,CAAC,UAAU,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;QACjD,CAAC;IAAA,CACD;IAED;;OAEG;IACH,aAAa,CAAC,SAAiB,EAAU;QACxC,MAAM,GAAG,GAAG,IAAI,CAAC,IAAI,CAAC,UAAU,EAAE,SAAS,CAAC,CAAC;QAC7C,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,EAAE,CAAC;YACtB,SAAS,CAAC,GAAG,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;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C3B;IAED;;OAEG;IACK,KAAK,CAAC,kBAAkB,CAAC,SAAiB,EAAE,GAAW,EAAiB;QAC/E,MAAM,QAAQ,GAAG,IAAI,CAAC,IAAI,CAAC,UAAU,EAAE,SAAS,CAAC,CAAC;QAElD,0BAA0B;QAC1B,MAAM,GAAG,GAAG,IAAI,CAAC,IAAI,CAAC,UAAU,EAAE,SAAS,CAAC,SAAS,CAAC,CAAC,EAAE,SAAS,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;QACtF,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,EAAE,CAAC;YACtB,SAAS,CAAC,GAAG,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;QACrC,CAAC;QAED,MAAM,QAAQ,GAAG,MAAM,KAAK,CAAC,GAAG,EAAE;YACjC,OAAO,EAAE;gBACR,aAAa,EAAE,UAAU,IAAI,CAAC,QAAQ,EAAE;aACxC;SACD,CAAC,CAAC;QAEH,IAAI,CAAC,QAAQ,CAAC,EAAE,EAAE,CAAC;YAClB,MAAM,IAAI,KAAK,CAAC,QAAQ,QAAQ,CAAC,MAAM,KAAK,QAAQ,CAAC,UAAU,EAAE,CAAC,CAAC;QACpE,CAAC;QAED,MAAM,MAAM,GAAG,MAAM,QAAQ,CAAC,WAAW,EAAE,CAAC;QAC5C,MAAM,SAAS,CAAC,QAAQ,EAAE,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC;IAAA,CAC/C;CACD","sourcesContent":["import { existsSync, mkdirSync } from \"fs\";\nimport { appendFile, writeFile } from \"fs/promises\";\nimport { join } from \"path\";\n\nexport interface Attachment {\n\toriginal: string; // original filename from uploader\n\tlocal: string; // path relative to working dir (e.g., \"C12345/attachments/1732531234567_file.png\")\n}\n\nexport interface LoggedMessage {\n\tts: string; // slack timestamp\n\tuser: string; // user ID (or \"bot\" for bot responses)\n\tuserName?: string; // handle (e.g., \"mario\")\n\tdisplayName?: string; // display name (e.g., \"Mario Zechner\")\n\ttext: string;\n\tattachments: Attachment[];\n\tisBot: boolean;\n}\n\nexport interface ChannelStoreConfig {\n\tworkingDir: string;\n\tbotToken: string; // needed for authenticated file downloads\n}\n\ninterface PendingDownload {\n\tchannelId: string;\n\tlocalPath: string; // relative path\n\turl: string;\n}\n\nexport class ChannelStore {\n\tprivate workingDir: string;\n\tprivate botToken: string;\n\tprivate pendingDownloads: PendingDownload[] = [];\n\tprivate isDownloading = false;\n\n\tconstructor(config: ChannelStoreConfig) {\n\t\tthis.workingDir = config.workingDir;\n\t\tthis.botToken = config.botToken;\n\n\t\t// Ensure working directory exists\n\t\tif (!existsSync(this.workingDir)) {\n\t\t\tmkdirSync(this.workingDir, { recursive: true });\n\t\t}\n\t}\n\n\t/**\n\t * Get or create the directory for a channel/DM\n\t */\n\tgetChannelDir(channelId: string): string {\n\t\tconst dir = join(this.workingDir, channelId);\n\t\tif (!existsSync(dir)) {\n\t\t\tmkdirSync(dir, { recursive: true });\n\t\t}\n\t\treturn dir;\n\t}\n\n\t/**\n\t * Generate a unique local filename for an attachment\n\t */\n\tgenerateLocalFilename(originalName: string, timestamp: string): string {\n\t\t// Convert slack timestamp (1234567890.123456) to milliseconds\n\t\tconst ts = Math.floor(parseFloat(timestamp) * 1000);\n\t\t// Sanitize original name (remove problematic characters)\n\t\tconst sanitized = originalName.replace(/[^a-zA-Z0-9._-]/g, \"_\");\n\t\treturn `${ts}_${sanitized}`;\n\t}\n\n\t/**\n\t * Process attachments from a Slack message event\n\t * Returns attachment metadata and queues downloads\n\t */\n\tprocessAttachments(\n\t\tchannelId: string,\n\t\tfiles: Array<{ name: string; url_private_download?: string; url_private?: string }>,\n\t\ttimestamp: string,\n\t): Attachment[] {\n\t\tconst attachments: Attachment[] = [];\n\n\t\tfor (const file of files) {\n\t\t\tconst url = file.url_private_download || file.url_private;\n\t\t\tif (!url) continue;\n\n\t\t\tconst filename = this.generateLocalFilename(file.name, timestamp);\n\t\t\tconst localPath = `${channelId}/attachments/${filename}`;\n\n\t\t\tattachments.push({\n\t\t\t\toriginal: file.name,\n\t\t\t\tlocal: localPath,\n\t\t\t});\n\n\t\t\t// Queue for background download\n\t\t\tthis.pendingDownloads.push({ channelId, localPath, url });\n\t\t}\n\n\t\t// Trigger background download\n\t\tthis.processDownloadQueue();\n\n\t\treturn attachments;\n\t}\n\n\t/**\n\t * Log a message to the channel's log.jsonl\n\t */\n\tasync logMessage(channelId: string, message: LoggedMessage): Promise<void> {\n\t\tconst logPath = join(this.getChannelDir(channelId), \"log.jsonl\");\n\t\tconst line = JSON.stringify(message) + \"\\n\";\n\t\tawait appendFile(logPath, line, \"utf-8\");\n\t}\n\n\t/**\n\t * Log a bot response\n\t */\n\tasync logBotResponse(channelId: string, text: string, ts: string): Promise<void> {\n\t\tawait this.logMessage(channelId, {\n\t\t\tts,\n\t\t\tuser: \"bot\",\n\t\t\ttext,\n\t\t\tattachments: [],\n\t\t\tisBot: true,\n\t\t});\n\t}\n\n\t/**\n\t * Process the download queue in the background\n\t */\n\tprivate async processDownloadQueue(): Promise<void> {\n\t\tif (this.isDownloading || this.pendingDownloads.length === 0) return;\n\n\t\tthis.isDownloading = true;\n\n\t\twhile (this.pendingDownloads.length > 0) {\n\t\t\tconst item = this.pendingDownloads.shift();\n\t\t\tif (!item) break;\n\n\t\t\ttry {\n\t\t\t\tawait this.downloadAttachment(item.localPath, item.url);\n\t\t\t\tconsole.log(`Downloaded: ${item.localPath}`);\n\t\t\t} catch (error) {\n\t\t\t\tconsole.error(`Failed to download ${item.localPath}:`, error);\n\t\t\t\t// Could re-queue for retry here\n\t\t\t}\n\t\t}\n\n\t\tthis.isDownloading = false;\n\t}\n\n\t/**\n\t * Download a single attachment\n\t */\n\tprivate async downloadAttachment(localPath: string, url: string): Promise<void> {\n\t\tconst filePath = join(this.workingDir, localPath);\n\n\t\t// Ensure directory exists\n\t\tconst dir = join(this.workingDir, localPath.substring(0, localPath.lastIndexOf(\"/\")));\n\t\tif (!existsSync(dir)) {\n\t\t\tmkdirSync(dir, { recursive: true });\n\t\t}\n\n\t\tconst response = await fetch(url, {\n\t\t\theaders: {\n\t\t\t\tAuthorization: `Bearer ${this.botToken}`,\n\t\t\t},\n\t\t});\n\n\t\tif (!response.ok) {\n\t\t\tthrow new Error(`HTTP ${response.status}: ${response.statusText}`);\n\t\t}\n\n\t\tconst buffer = await response.arrayBuffer();\n\t\tawait writeFile(filePath, Buffer.from(buffer));\n\t}\n}\n"]}
@@ -0,0 +1,10 @@
1
+ import type { AgentTool } from "@mariozechner/pi-ai";
2
+ export declare function setUploadFunction(fn: (filePath: string, title?: string) => Promise<void>): void;
3
+ declare const attachSchema: import("@sinclair/typebox").TObject<{
4
+ label: import("@sinclair/typebox").TString;
5
+ path: import("@sinclair/typebox").TString;
6
+ title: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TString>;
7
+ }>;
8
+ export declare const attachTool: AgentTool<typeof attachSchema>;
9
+ export {};
10
+ //# sourceMappingURL=attach.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"attach.d.ts","sourceRoot":"","sources":["../../src/tools/attach.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;AAOrD,wBAAgB,iBAAiB,CAAC,EAAE,EAAE,CAAC,QAAQ,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI,CAE/F;AAED,QAAA,MAAM,YAAY;;;;EAIhB,CAAC;AAEH,eAAO,MAAM,UAAU,EAAE,SAAS,CAAC,OAAO,YAAY,CA4BrD,CAAC","sourcesContent":["import type { AgentTool } from \"@mariozechner/pi-ai\";\nimport { Type } from \"@sinclair/typebox\";\nimport { basename, resolve as resolvePath } from \"path\";\n\n// This will be set by the agent before running\nlet uploadFn: ((filePath: string, title?: string) => Promise<void>) | null = null;\n\nexport function setUploadFunction(fn: (filePath: string, title?: string) => Promise<void>): void {\n\tuploadFn = fn;\n}\n\nconst attachSchema = Type.Object({\n\tlabel: Type.String({ description: \"Brief description of what you're sharing (shown to user)\" }),\n\tpath: Type.String({ description: \"Path to the file to attach\" }),\n\ttitle: Type.Optional(Type.String({ description: \"Title for the file (defaults to filename)\" })),\n});\n\nexport const attachTool: AgentTool<typeof attachSchema> = {\n\tname: \"attach\",\n\tlabel: \"attach\",\n\tdescription: \"Attach a file to your response. Use this to share files, images, or documents with the user.\",\n\tparameters: attachSchema,\n\texecute: async (\n\t\t_toolCallId: string,\n\t\t{ path, title }: { label: string; path: string; title?: string },\n\t\tsignal?: AbortSignal,\n\t) => {\n\t\tif (!uploadFn) {\n\t\t\tthrow new Error(\"Upload function not configured\");\n\t\t}\n\n\t\tif (signal?.aborted) {\n\t\t\tthrow new Error(\"Operation aborted\");\n\t\t}\n\n\t\tconst absolutePath = resolvePath(path);\n\t\tconst fileName = title || basename(absolutePath);\n\n\t\tawait uploadFn(absolutePath, fileName);\n\n\t\treturn {\n\t\t\tcontent: [{ type: \"text\" as const, text: `Attached file: ${fileName}` }],\n\t\t\tdetails: undefined,\n\t\t};\n\t},\n};\n"]}
@@ -0,0 +1,34 @@
1
+ import { Type } from "@sinclair/typebox";
2
+ import { basename, resolve as resolvePath } from "path";
3
+ // This will be set by the agent before running
4
+ let uploadFn = null;
5
+ export function setUploadFunction(fn) {
6
+ uploadFn = fn;
7
+ }
8
+ const attachSchema = Type.Object({
9
+ label: Type.String({ description: "Brief description of what you're sharing (shown to user)" }),
10
+ path: Type.String({ description: "Path to the file to attach" }),
11
+ title: Type.Optional(Type.String({ description: "Title for the file (defaults to filename)" })),
12
+ });
13
+ export const attachTool = {
14
+ name: "attach",
15
+ label: "attach",
16
+ description: "Attach a file to your response. Use this to share files, images, or documents with the user.",
17
+ parameters: attachSchema,
18
+ execute: async (_toolCallId, { path, title }, signal) => {
19
+ if (!uploadFn) {
20
+ throw new Error("Upload function not configured");
21
+ }
22
+ if (signal?.aborted) {
23
+ throw new Error("Operation aborted");
24
+ }
25
+ const absolutePath = resolvePath(path);
26
+ const fileName = title || basename(absolutePath);
27
+ await uploadFn(absolutePath, fileName);
28
+ return {
29
+ content: [{ type: "text", text: `Attached file: ${fileName}` }],
30
+ details: undefined,
31
+ };
32
+ },
33
+ };
34
+ //# sourceMappingURL=attach.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"attach.js","sourceRoot":"","sources":["../../src/tools/attach.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,IAAI,EAAE,MAAM,mBAAmB,CAAC;AACzC,OAAO,EAAE,QAAQ,EAAE,OAAO,IAAI,WAAW,EAAE,MAAM,MAAM,CAAC;AAExD,+CAA+C;AAC/C,IAAI,QAAQ,GAAiE,IAAI,CAAC;AAElF,MAAM,UAAU,iBAAiB,CAAC,EAAuD,EAAQ;IAChG,QAAQ,GAAG,EAAE,CAAC;AAAA,CACd;AAED,MAAM,YAAY,GAAG,IAAI,CAAC,MAAM,CAAC;IAChC,KAAK,EAAE,IAAI,CAAC,MAAM,CAAC,EAAE,WAAW,EAAE,0DAA0D,EAAE,CAAC;IAC/F,IAAI,EAAE,IAAI,CAAC,MAAM,CAAC,EAAE,WAAW,EAAE,4BAA4B,EAAE,CAAC;IAChE,KAAK,EAAE,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,WAAW,EAAE,2CAA2C,EAAE,CAAC,CAAC;CAC/F,CAAC,CAAC;AAEH,MAAM,CAAC,MAAM,UAAU,GAAmC;IACzD,IAAI,EAAE,QAAQ;IACd,KAAK,EAAE,QAAQ;IACf,WAAW,EAAE,8FAA8F;IAC3G,UAAU,EAAE,YAAY;IACxB,OAAO,EAAE,KAAK,EACb,WAAmB,EACnB,EAAE,IAAI,EAAE,KAAK,EAAmD,EAChE,MAAoB,EACnB,EAAE,CAAC;QACJ,IAAI,CAAC,QAAQ,EAAE,CAAC;YACf,MAAM,IAAI,KAAK,CAAC,gCAAgC,CAAC,CAAC;QACnD,CAAC;QAED,IAAI,MAAM,EAAE,OAAO,EAAE,CAAC;YACrB,MAAM,IAAI,KAAK,CAAC,mBAAmB,CAAC,CAAC;QACtC,CAAC;QAED,MAAM,YAAY,GAAG,WAAW,CAAC,IAAI,CAAC,CAAC;QACvC,MAAM,QAAQ,GAAG,KAAK,IAAI,QAAQ,CAAC,YAAY,CAAC,CAAC;QAEjD,MAAM,QAAQ,CAAC,YAAY,EAAE,QAAQ,CAAC,CAAC;QAEvC,OAAO;YACN,OAAO,EAAE,CAAC,EAAE,IAAI,EAAE,MAAe,EAAE,IAAI,EAAE,kBAAkB,QAAQ,EAAE,EAAE,CAAC;YACxE,OAAO,EAAE,SAAS;SAClB,CAAC;IAAA,CACF;CACD,CAAC","sourcesContent":["import type { AgentTool } from \"@mariozechner/pi-ai\";\nimport { Type } from \"@sinclair/typebox\";\nimport { basename, resolve as resolvePath } from \"path\";\n\n// This will be set by the agent before running\nlet uploadFn: ((filePath: string, title?: string) => Promise<void>) | null = null;\n\nexport function setUploadFunction(fn: (filePath: string, title?: string) => Promise<void>): void {\n\tuploadFn = fn;\n}\n\nconst attachSchema = Type.Object({\n\tlabel: Type.String({ description: \"Brief description of what you're sharing (shown to user)\" }),\n\tpath: Type.String({ description: \"Path to the file to attach\" }),\n\ttitle: Type.Optional(Type.String({ description: \"Title for the file (defaults to filename)\" })),\n});\n\nexport const attachTool: AgentTool<typeof attachSchema> = {\n\tname: \"attach\",\n\tlabel: \"attach\",\n\tdescription: \"Attach a file to your response. Use this to share files, images, or documents with the user.\",\n\tparameters: attachSchema,\n\texecute: async (\n\t\t_toolCallId: string,\n\t\t{ path, title }: { label: string; path: string; title?: string },\n\t\tsignal?: AbortSignal,\n\t) => {\n\t\tif (!uploadFn) {\n\t\t\tthrow new Error(\"Upload function not configured\");\n\t\t}\n\n\t\tif (signal?.aborted) {\n\t\t\tthrow new Error(\"Operation aborted\");\n\t\t}\n\n\t\tconst absolutePath = resolvePath(path);\n\t\tconst fileName = title || basename(absolutePath);\n\n\t\tawait uploadFn(absolutePath, fileName);\n\n\t\treturn {\n\t\t\tcontent: [{ type: \"text\" as const, text: `Attached file: ${fileName}` }],\n\t\t\tdetails: undefined,\n\t\t};\n\t},\n};\n"]}
@@ -0,0 +1,10 @@
1
+ import type { AgentTool } from "@mariozechner/pi-ai";
2
+ import type { Executor } from "../sandbox.js";
3
+ declare const bashSchema: import("@sinclair/typebox").TObject<{
4
+ label: import("@sinclair/typebox").TString;
5
+ command: import("@sinclair/typebox").TString;
6
+ timeout: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TNumber>;
7
+ }>;
8
+ export declare function createBashTool(executor: Executor): AgentTool<typeof bashSchema>;
9
+ export {};
10
+ //# sourceMappingURL=bash.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bash.d.ts","sourceRoot":"","sources":["../../src/tools/bash.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;AAErD,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAE9C,QAAA,MAAM,UAAU;;;;EAId,CAAC;AAEH,wBAAgB,cAAc,CAAC,QAAQ,EAAE,QAAQ,GAAG,SAAS,CAAC,OAAO,UAAU,CAAC,CA2B/E","sourcesContent":["import type { AgentTool } from \"@mariozechner/pi-ai\";\nimport { Type } from \"@sinclair/typebox\";\nimport type { Executor } from \"../sandbox.js\";\n\nconst bashSchema = Type.Object({\n\tlabel: Type.String({ description: \"Brief description of what this command does (shown to user)\" }),\n\tcommand: Type.String({ description: \"Bash command to execute\" }),\n\ttimeout: Type.Optional(Type.Number({ description: \"Timeout in seconds (optional, no default timeout)\" })),\n});\n\nexport function createBashTool(executor: Executor): AgentTool<typeof bashSchema> {\n\treturn {\n\t\tname: \"bash\",\n\t\tlabel: \"bash\",\n\t\tdescription:\n\t\t\t\"Execute a bash command in the current working directory. Returns stdout and stderr. Optionally provide a timeout in seconds.\",\n\t\tparameters: bashSchema,\n\t\texecute: async (\n\t\t\t_toolCallId: string,\n\t\t\t{ command, timeout }: { label: string; command: string; timeout?: number },\n\t\t\tsignal?: AbortSignal,\n\t\t) => {\n\t\t\tconst result = await executor.exec(command, { timeout, signal });\n\t\t\tlet output = \"\";\n\t\t\tif (result.stdout) output += result.stdout;\n\t\t\tif (result.stderr) {\n\t\t\t\tif (output) output += \"\\n\";\n\t\t\t\toutput += result.stderr;\n\t\t\t}\n\n\t\t\tif (result.code !== 0) {\n\t\t\t\tthrow new Error(`${output}\\n\\nCommand exited with code ${result.code}`.trim());\n\t\t\t}\n\n\t\t\treturn { content: [{ type: \"text\", text: output || \"(no output)\" }], details: undefined };\n\t\t},\n\t};\n}\n"]}
@@ -0,0 +1,30 @@
1
+ import { Type } from "@sinclair/typebox";
2
+ const bashSchema = Type.Object({
3
+ label: Type.String({ description: "Brief description of what this command does (shown to user)" }),
4
+ command: Type.String({ description: "Bash command to execute" }),
5
+ timeout: Type.Optional(Type.Number({ description: "Timeout in seconds (optional, no default timeout)" })),
6
+ });
7
+ export function createBashTool(executor) {
8
+ return {
9
+ name: "bash",
10
+ label: "bash",
11
+ description: "Execute a bash command in the current working directory. Returns stdout and stderr. Optionally provide a timeout in seconds.",
12
+ parameters: bashSchema,
13
+ execute: async (_toolCallId, { command, timeout }, signal) => {
14
+ const result = await executor.exec(command, { timeout, signal });
15
+ let output = "";
16
+ if (result.stdout)
17
+ output += result.stdout;
18
+ if (result.stderr) {
19
+ if (output)
20
+ output += "\n";
21
+ output += result.stderr;
22
+ }
23
+ if (result.code !== 0) {
24
+ throw new Error(`${output}\n\nCommand exited with code ${result.code}`.trim());
25
+ }
26
+ return { content: [{ type: "text", text: output || "(no output)" }], details: undefined };
27
+ },
28
+ };
29
+ }
30
+ //# sourceMappingURL=bash.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bash.js","sourceRoot":"","sources":["../../src/tools/bash.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,IAAI,EAAE,MAAM,mBAAmB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AAI,aAAa,EAAE,CAAC,EAAE,OAAO,EAAE,SAAS,EAAE,CAAC;QAAA,CAC1F;KACD,CAAC;AAAA,CACF","sourcesContent":["import type { AgentTool } from \"@mariozechner/pi-ai\";\nimport { Type } from \"@sinclair/typebox\";\nimport type { Executor } from \"../sandbox.js\";\n\nconst bashSchema = Type.Object({\n\tlabel: Type.String({ description: \"Brief description of what this command does (shown to user)\" }),\n\tcommand: Type.String({ description: \"Bash command to execute\" }),\n\ttimeout: Type.Optional(Type.Number({ description: \"Timeout in seconds (optional, no default timeout)\" })),\n});\n\nexport function createBashTool(executor: Executor): AgentTool<typeof bashSchema> {\n\treturn {\n\t\tname: \"bash\",\n\t\tlabel: \"bash\",\n\t\tdescription:\n\t\t\t\"Execute a bash command in the current working directory. Returns stdout and stderr. Optionally provide a timeout in seconds.\",\n\t\tparameters: bashSchema,\n\t\texecute: async (\n\t\t\t_toolCallId: string,\n\t\t\t{ command, timeout }: { label: string; command: string; timeout?: number },\n\t\t\tsignal?: AbortSignal,\n\t\t) => {\n\t\t\tconst result = await executor.exec(command, { timeout, signal });\n\t\t\tlet output = \"\";\n\t\t\tif (result.stdout) output += result.stdout;\n\t\t\tif (result.stderr) {\n\t\t\t\tif (output) output += \"\\n\";\n\t\t\t\toutput += result.stderr;\n\t\t\t}\n\n\t\t\tif (result.code !== 0) {\n\t\t\t\tthrow new Error(`${output}\\n\\nCommand exited with code ${result.code}`.trim());\n\t\t\t}\n\n\t\t\treturn { content: [{ type: \"text\", text: output || \"(no output)\" }], details: undefined };\n\t\t},\n\t};\n}\n"]}
@@ -0,0 +1,11 @@
1
+ import type { AgentTool } from "@mariozechner/pi-ai";
2
+ import type { Executor } from "../sandbox.js";
3
+ declare const editSchema: import("@sinclair/typebox").TObject<{
4
+ label: import("@sinclair/typebox").TString;
5
+ path: import("@sinclair/typebox").TString;
6
+ oldText: import("@sinclair/typebox").TString;
7
+ newText: import("@sinclair/typebox").TString;
8
+ }>;
9
+ export declare function createEditTool(executor: Executor): AgentTool<typeof editSchema>;
10
+ export {};
11
+ //# sourceMappingURL=edit.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"edit.d.ts","sourceRoot":"","sources":["../../src/tools/edit.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;AAGrD,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAqF9C,QAAA,MAAM,UAAU;;;;;EAKd,CAAC;AAEH,wBAAgB,cAAc,CAAC,QAAQ,EAAE,QAAQ,GAAG,SAAS,CAAC,OAAO,UAAU,CAAC,CAiE/E","sourcesContent":["import type { AgentTool } from \"@mariozechner/pi-ai\";\nimport { Type } from \"@sinclair/typebox\";\nimport * as Diff from \"diff\";\nimport type { Executor } from \"../sandbox.js\";\n\n/**\n * Generate a unified diff string with line numbers and context\n */\nfunction generateDiffString(oldContent: string, newContent: string, contextLines = 4): string {\n\tconst parts = Diff.diffLines(oldContent, newContent);\n\tconst output: string[] = [];\n\n\tconst oldLines = oldContent.split(\"\\n\");\n\tconst newLines = newContent.split(\"\\n\");\n\tconst maxLineNum = Math.max(oldLines.length, newLines.length);\n\tconst lineNumWidth = String(maxLineNum).length;\n\n\tlet oldLineNum = 1;\n\tlet newLineNum = 1;\n\tlet lastWasChange = false;\n\n\tfor (let i = 0; i < parts.length; i++) {\n\t\tconst part = parts[i];\n\t\tconst raw = part.value.split(\"\\n\");\n\t\tif (raw[raw.length - 1] === \"\") {\n\t\t\traw.pop();\n\t\t}\n\n\t\tif (part.added || part.removed) {\n\t\t\tfor (const line of raw) {\n\t\t\t\tif (part.added) {\n\t\t\t\t\tconst lineNum = String(newLineNum).padStart(lineNumWidth, \" \");\n\t\t\t\t\toutput.push(`+${lineNum} ${line}`);\n\t\t\t\t\tnewLineNum++;\n\t\t\t\t} else {\n\t\t\t\t\tconst lineNum = String(oldLineNum).padStart(lineNumWidth, \" \");\n\t\t\t\t\toutput.push(`-${lineNum} ${line}`);\n\t\t\t\t\toldLineNum++;\n\t\t\t\t}\n\t\t\t}\n\t\t\tlastWasChange = true;\n\t\t} else {\n\t\t\tconst nextPartIsChange = i < parts.length - 1 && (parts[i + 1].added || parts[i + 1].removed);\n\n\t\t\tif (lastWasChange || nextPartIsChange) {\n\t\t\t\tlet linesToShow = raw;\n\t\t\t\tlet skipStart = 0;\n\t\t\t\tlet skipEnd = 0;\n\n\t\t\t\tif (!lastWasChange) {\n\t\t\t\t\tskipStart = Math.max(0, raw.length - contextLines);\n\t\t\t\t\tlinesToShow = raw.slice(skipStart);\n\t\t\t\t}\n\n\t\t\t\tif (!nextPartIsChange && linesToShow.length > contextLines) {\n\t\t\t\t\tskipEnd = linesToShow.length - contextLines;\n\t\t\t\t\tlinesToShow = linesToShow.slice(0, contextLines);\n\t\t\t\t}\n\n\t\t\t\tif (skipStart > 0) {\n\t\t\t\t\toutput.push(` ${\"\".padStart(lineNumWidth, \" \")} ...`);\n\t\t\t\t}\n\n\t\t\t\tfor (const line of linesToShow) {\n\t\t\t\t\tconst lineNum = String(oldLineNum).padStart(lineNumWidth, \" \");\n\t\t\t\t\toutput.push(` ${lineNum} ${line}`);\n\t\t\t\t\toldLineNum++;\n\t\t\t\t\tnewLineNum++;\n\t\t\t\t}\n\n\t\t\t\tif (skipEnd > 0) {\n\t\t\t\t\toutput.push(` ${\"\".padStart(lineNumWidth, \" \")} ...`);\n\t\t\t\t}\n\n\t\t\t\toldLineNum += skipStart + skipEnd;\n\t\t\t\tnewLineNum += skipStart + skipEnd;\n\t\t\t} else {\n\t\t\t\toldLineNum += raw.length;\n\t\t\t\tnewLineNum += raw.length;\n\t\t\t}\n\n\t\t\tlastWasChange = false;\n\t\t}\n\t}\n\n\treturn output.join(\"\\n\");\n}\n\nconst editSchema = Type.Object({\n\tlabel: Type.String({ description: \"Brief description of the edit you're making (shown to user)\" }),\n\tpath: Type.String({ description: \"Path to the file to edit (relative or absolute)\" }),\n\toldText: Type.String({ description: \"Exact text to find and replace (must match exactly)\" }),\n\tnewText: Type.String({ description: \"New text to replace the old text with\" }),\n});\n\nexport function createEditTool(executor: Executor): AgentTool<typeof editSchema> {\n\treturn {\n\t\tname: \"edit\",\n\t\tlabel: \"edit\",\n\t\tdescription:\n\t\t\t\"Edit a file by replacing exact text. The oldText must match exactly (including whitespace). Use this for precise, surgical edits.\",\n\t\tparameters: editSchema,\n\t\texecute: async (\n\t\t\t_toolCallId: string,\n\t\t\t{ path, oldText, newText }: { label: string; path: string; oldText: string; newText: string },\n\t\t\tsignal?: AbortSignal,\n\t\t) => {\n\t\t\t// Read the file\n\t\t\tconst readResult = await executor.exec(`cat ${shellEscape(path)}`, { signal });\n\t\t\tif (readResult.code !== 0) {\n\t\t\t\tthrow new Error(readResult.stderr || `File not found: ${path}`);\n\t\t\t}\n\n\t\t\tconst content = readResult.stdout;\n\n\t\t\t// Check if old text exists\n\t\t\tif (!content.includes(oldText)) {\n\t\t\t\tthrow new Error(\n\t\t\t\t\t`Could not find the exact text in ${path}. The old text must match exactly including all whitespace and newlines.`,\n\t\t\t\t);\n\t\t\t}\n\n\t\t\t// Count occurrences\n\t\t\tconst occurrences = content.split(oldText).length - 1;\n\n\t\t\tif (occurrences > 1) {\n\t\t\t\tthrow new Error(\n\t\t\t\t\t`Found ${occurrences} occurrences of the text in ${path}. The text must be unique. Please provide more context to make it unique.`,\n\t\t\t\t);\n\t\t\t}\n\n\t\t\t// Perform replacement\n\t\t\tconst index = content.indexOf(oldText);\n\t\t\tconst newContent = content.substring(0, index) + newText + content.substring(index + oldText.length);\n\n\t\t\tif (content === newContent) {\n\t\t\t\tthrow new Error(\n\t\t\t\t\t`No changes made to ${path}. The replacement produced identical content. This might indicate an issue with special characters or the text not existing as expected.`,\n\t\t\t\t);\n\t\t\t}\n\n\t\t\t// Write the file back\n\t\t\tconst writeResult = await executor.exec(`printf '%s' ${shellEscape(newContent)} > ${shellEscape(path)}`, {\n\t\t\t\tsignal,\n\t\t\t});\n\t\t\tif (writeResult.code !== 0) {\n\t\t\t\tthrow new Error(writeResult.stderr || `Failed to write file: ${path}`);\n\t\t\t}\n\n\t\t\treturn {\n\t\t\t\tcontent: [\n\t\t\t\t\t{\n\t\t\t\t\t\ttype: \"text\",\n\t\t\t\t\t\ttext: `Successfully replaced text in ${path}. Changed ${oldText.length} characters to ${newText.length} characters.`,\n\t\t\t\t\t},\n\t\t\t\t],\n\t\t\t\tdetails: { diff: generateDiffString(content, newContent) },\n\t\t\t};\n\t\t},\n\t};\n}\n\nfunction shellEscape(s: string): string {\n\treturn `'${s.replace(/'/g, \"'\\\\''\")}'`;\n}\n"]}
@@ -0,0 +1,131 @@
1
+ import { Type } from "@sinclair/typebox";
2
+ import * as Diff from "diff";
3
+ /**
4
+ * Generate a unified diff string with line numbers and context
5
+ */
6
+ function generateDiffString(oldContent, newContent, contextLines = 4) {
7
+ const parts = Diff.diffLines(oldContent, newContent);
8
+ const output = [];
9
+ const oldLines = oldContent.split("\n");
10
+ const newLines = newContent.split("\n");
11
+ const maxLineNum = Math.max(oldLines.length, newLines.length);
12
+ const lineNumWidth = String(maxLineNum).length;
13
+ let oldLineNum = 1;
14
+ let newLineNum = 1;
15
+ let lastWasChange = false;
16
+ for (let i = 0; i < parts.length; i++) {
17
+ const part = parts[i];
18
+ const raw = part.value.split("\n");
19
+ if (raw[raw.length - 1] === "") {
20
+ raw.pop();
21
+ }
22
+ if (part.added || part.removed) {
23
+ for (const line of raw) {
24
+ if (part.added) {
25
+ const lineNum = String(newLineNum).padStart(lineNumWidth, " ");
26
+ output.push(`+${lineNum} ${line}`);
27
+ newLineNum++;
28
+ }
29
+ else {
30
+ const lineNum = String(oldLineNum).padStart(lineNumWidth, " ");
31
+ output.push(`-${lineNum} ${line}`);
32
+ oldLineNum++;
33
+ }
34
+ }
35
+ lastWasChange = true;
36
+ }
37
+ else {
38
+ const nextPartIsChange = i < parts.length - 1 && (parts[i + 1].added || parts[i + 1].removed);
39
+ if (lastWasChange || nextPartIsChange) {
40
+ let linesToShow = raw;
41
+ let skipStart = 0;
42
+ let skipEnd = 0;
43
+ if (!lastWasChange) {
44
+ skipStart = Math.max(0, raw.length - contextLines);
45
+ linesToShow = raw.slice(skipStart);
46
+ }
47
+ if (!nextPartIsChange && linesToShow.length > contextLines) {
48
+ skipEnd = linesToShow.length - contextLines;
49
+ linesToShow = linesToShow.slice(0, contextLines);
50
+ }
51
+ if (skipStart > 0) {
52
+ output.push(` ${"".padStart(lineNumWidth, " ")} ...`);
53
+ }
54
+ for (const line of linesToShow) {
55
+ const lineNum = String(oldLineNum).padStart(lineNumWidth, " ");
56
+ output.push(` ${lineNum} ${line}`);
57
+ oldLineNum++;
58
+ newLineNum++;
59
+ }
60
+ if (skipEnd > 0) {
61
+ output.push(` ${"".padStart(lineNumWidth, " ")} ...`);
62
+ }
63
+ oldLineNum += skipStart + skipEnd;
64
+ newLineNum += skipStart + skipEnd;
65
+ }
66
+ else {
67
+ oldLineNum += raw.length;
68
+ newLineNum += raw.length;
69
+ }
70
+ lastWasChange = false;
71
+ }
72
+ }
73
+ return output.join("\n");
74
+ }
75
+ const editSchema = Type.Object({
76
+ label: Type.String({ description: "Brief description of the edit you're making (shown to user)" }),
77
+ path: Type.String({ description: "Path to the file to edit (relative or absolute)" }),
78
+ oldText: Type.String({ description: "Exact text to find and replace (must match exactly)" }),
79
+ newText: Type.String({ description: "New text to replace the old text with" }),
80
+ });
81
+ export function createEditTool(executor) {
82
+ return {
83
+ name: "edit",
84
+ label: "edit",
85
+ description: "Edit a file by replacing exact text. The oldText must match exactly (including whitespace). Use this for precise, surgical edits.",
86
+ parameters: editSchema,
87
+ execute: async (_toolCallId, { path, oldText, newText }, signal) => {
88
+ // Read the file
89
+ const readResult = await executor.exec(`cat ${shellEscape(path)}`, { signal });
90
+ if (readResult.code !== 0) {
91
+ throw new Error(readResult.stderr || `File not found: ${path}`);
92
+ }
93
+ const content = readResult.stdout;
94
+ // Check if old text exists
95
+ if (!content.includes(oldText)) {
96
+ throw new Error(`Could not find the exact text in ${path}. The old text must match exactly including all whitespace and newlines.`);
97
+ }
98
+ // Count occurrences
99
+ const occurrences = content.split(oldText).length - 1;
100
+ if (occurrences > 1) {
101
+ throw new Error(`Found ${occurrences} occurrences of the text in ${path}. The text must be unique. Please provide more context to make it unique.`);
102
+ }
103
+ // Perform replacement
104
+ const index = content.indexOf(oldText);
105
+ const newContent = content.substring(0, index) + newText + content.substring(index + oldText.length);
106
+ if (content === newContent) {
107
+ throw new Error(`No changes made to ${path}. The replacement produced identical content. This might indicate an issue with special characters or the text not existing as expected.`);
108
+ }
109
+ // Write the file back
110
+ const writeResult = await executor.exec(`printf '%s' ${shellEscape(newContent)} > ${shellEscape(path)}`, {
111
+ signal,
112
+ });
113
+ if (writeResult.code !== 0) {
114
+ throw new Error(writeResult.stderr || `Failed to write file: ${path}`);
115
+ }
116
+ return {
117
+ content: [
118
+ {
119
+ type: "text",
120
+ text: `Successfully replaced text in ${path}. Changed ${oldText.length} characters to ${newText.length} characters.`,
121
+ },
122
+ ],
123
+ details: { diff: generateDiffString(content, newContent) },
124
+ };
125
+ },
126
+ };
127
+ }
128
+ function shellEscape(s) {
129
+ return `'${s.replace(/'/g, "'\\''")}'`;
130
+ }
131
+ //# sourceMappingURL=edit.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"edit.js","sourceRoot":"","sources":["../../src/tools/edit.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,IAAI,EAAE,MAAM,mBAAmB,CAAC;AACzC,OAAO,KAAK,IAAI,MAAM,MAAM,CAAC;AAG7B;;GAEG;AACH,SAAS,kBAAkB,CAAC,UAAkB,EAAE,UAAkB,EAAE,YAAY,GAAG,CAAC,EAAU;IAC7F,MAAM,KAAK,GAAG,IAAI,CAAC,SAAS,CAAC,UAAU,EAAE,UAAU,CAAC,CAAC;IACrD,MAAM,MAAM,GAAa,EAAE,CAAC;IAE5B,MAAM,QAAQ,GAAG,UAAU,CAAC,KAAK,CAAC,IAAI,CAAC,CAA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CAAC,MAAM,WAAW,CAAC,IAAI,CAAC,EAAE,EAAE;gBACxG,MAAM;aACN,CAAC,CAAC;YACH,IAAI,WAAW,CAAC,IAAI,KAAK,CAAC,EAAE,CAAC;gBAC5B,MAAM,IAAI,KAAK,CAAC,WAAW,CAAC,MAAM,IAAI,yBAAyB,IAAI,EAAE,CAAC,CAAC;YACxE,CAAC;YAED,OAAO;gBACN,OAAO,EAAE;oBACR;wBACC,IAAI,EAAE,MAAM;wBACZ,IAAI,EAAE,iCAAiC,IAAI,aAAa,OAAO,CAAC,MAAM,kBAAkB,OAAO,CAAC,MAAM,cAAc;qBACpH;iBACD;gBACD,OAAO,EAAE,EAAE,IAAI,EAAE,kBAAkB,CAAC,OAAO,EAAE,UAAU,CAAC,EAAE;aAC1D,CAAC;QAAA,CACF;KACD,CAAC;AAAA,CACF;AAED,SAAS,WAAW,CAAC,CAAS,EAAU;IACvC,OAAO,IAAI,CAAC,CAAC,OAAO,CAAC,IAAI,EAAE,OAAO,CAAC,GAAG,CAAC;AAAA,CACvC","sourcesContent":["import type { AgentTool } from \"@mariozechner/pi-ai\";\nimport { Type } from \"@sinclair/typebox\";\nimport * as Diff from \"diff\";\nimport type { Executor } from \"../sandbox.js\";\n\n/**\n * Generate a unified diff string with line numbers and context\n */\nfunction generateDiffString(oldContent: string, newContent: string, contextLines = 4): string {\n\tconst parts = Diff.diffLines(oldContent, newContent);\n\tconst output: string[] = [];\n\n\tconst oldLines = oldContent.split(\"\\n\");\n\tconst newLines = newContent.split(\"\\n\");\n\tconst maxLineNum = Math.max(oldLines.length, newLines.length);\n\tconst lineNumWidth = String(maxLineNum).length;\n\n\tlet oldLineNum = 1;\n\tlet newLineNum = 1;\n\tlet lastWasChange = false;\n\n\tfor (let i = 0; i < parts.length; i++) {\n\t\tconst part = parts[i];\n\t\tconst raw = part.value.split(\"\\n\");\n\t\tif (raw[raw.length - 1] === \"\") {\n\t\t\traw.pop();\n\t\t}\n\n\t\tif (part.added || part.removed) {\n\t\t\tfor (const line of raw) {\n\t\t\t\tif (part.added) {\n\t\t\t\t\tconst lineNum = String(newLineNum).padStart(lineNumWidth, \" \");\n\t\t\t\t\toutput.push(`+${lineNum} ${line}`);\n\t\t\t\t\tnewLineNum++;\n\t\t\t\t} else {\n\t\t\t\t\tconst lineNum = String(oldLineNum).padStart(lineNumWidth, \" \");\n\t\t\t\t\toutput.push(`-${lineNum} ${line}`);\n\t\t\t\t\toldLineNum++;\n\t\t\t\t}\n\t\t\t}\n\t\t\tlastWasChange = true;\n\t\t} else {\n\t\t\tconst nextPartIsChange = i < parts.length - 1 && (parts[i + 1].added || parts[i + 1].removed);\n\n\t\t\tif (lastWasChange || nextPartIsChange) {\n\t\t\t\tlet linesToShow = raw;\n\t\t\t\tlet skipStart = 0;\n\t\t\t\tlet skipEnd = 0;\n\n\t\t\t\tif (!lastWasChange) {\n\t\t\t\t\tskipStart = Math.max(0, raw.length - contextLines);\n\t\t\t\t\tlinesToShow = raw.slice(skipStart);\n\t\t\t\t}\n\n\t\t\t\tif (!nextPartIsChange && linesToShow.length > contextLines) {\n\t\t\t\t\tskipEnd = linesToShow.length - contextLines;\n\t\t\t\t\tlinesToShow = linesToShow.slice(0, contextLines);\n\t\t\t\t}\n\n\t\t\t\tif (skipStart > 0) {\n\t\t\t\t\toutput.push(` ${\"\".padStart(lineNumWidth, \" \")} ...`);\n\t\t\t\t}\n\n\t\t\t\tfor (const line of linesToShow) {\n\t\t\t\t\tconst lineNum = String(oldLineNum).padStart(lineNumWidth, \" \");\n\t\t\t\t\toutput.push(` ${lineNum} ${line}`);\n\t\t\t\t\toldLineNum++;\n\t\t\t\t\tnewLineNum++;\n\t\t\t\t}\n\n\t\t\t\tif (skipEnd > 0) {\n\t\t\t\t\toutput.push(` ${\"\".padStart(lineNumWidth, \" \")} ...`);\n\t\t\t\t}\n\n\t\t\t\toldLineNum += skipStart + skipEnd;\n\t\t\t\tnewLineNum += skipStart + skipEnd;\n\t\t\t} else {\n\t\t\t\toldLineNum += raw.length;\n\t\t\t\tnewLineNum += raw.length;\n\t\t\t}\n\n\t\t\tlastWasChange = false;\n\t\t}\n\t}\n\n\treturn output.join(\"\\n\");\n}\n\nconst editSchema = Type.Object({\n\tlabel: Type.String({ description: \"Brief description of the edit you're making (shown to user)\" }),\n\tpath: Type.String({ description: \"Path to the file to edit (relative or absolute)\" }),\n\toldText: Type.String({ description: \"Exact text to find and replace (must match exactly)\" }),\n\tnewText: Type.String({ description: \"New text to replace the old text with\" }),\n});\n\nexport function createEditTool(executor: Executor): AgentTool<typeof editSchema> {\n\treturn {\n\t\tname: \"edit\",\n\t\tlabel: \"edit\",\n\t\tdescription:\n\t\t\t\"Edit a file by replacing exact text. The oldText must match exactly (including whitespace). Use this for precise, surgical edits.\",\n\t\tparameters: editSchema,\n\t\texecute: async (\n\t\t\t_toolCallId: string,\n\t\t\t{ path, oldText, newText }: { label: string; path: string; oldText: string; newText: string },\n\t\t\tsignal?: AbortSignal,\n\t\t) => {\n\t\t\t// Read the file\n\t\t\tconst readResult = await executor.exec(`cat ${shellEscape(path)}`, { signal });\n\t\t\tif (readResult.code !== 0) {\n\t\t\t\tthrow new Error(readResult.stderr || `File not found: ${path}`);\n\t\t\t}\n\n\t\t\tconst content = readResult.stdout;\n\n\t\t\t// Check if old text exists\n\t\t\tif (!content.includes(oldText)) {\n\t\t\t\tthrow new Error(\n\t\t\t\t\t`Could not find the exact text in ${path}. The old text must match exactly including all whitespace and newlines.`,\n\t\t\t\t);\n\t\t\t}\n\n\t\t\t// Count occurrences\n\t\t\tconst occurrences = content.split(oldText).length - 1;\n\n\t\t\tif (occurrences > 1) {\n\t\t\t\tthrow new Error(\n\t\t\t\t\t`Found ${occurrences} occurrences of the text in ${path}. The text must be unique. Please provide more context to make it unique.`,\n\t\t\t\t);\n\t\t\t}\n\n\t\t\t// Perform replacement\n\t\t\tconst index = content.indexOf(oldText);\n\t\t\tconst newContent = content.substring(0, index) + newText + content.substring(index + oldText.length);\n\n\t\t\tif (content === newContent) {\n\t\t\t\tthrow new Error(\n\t\t\t\t\t`No changes made to ${path}. The replacement produced identical content. This might indicate an issue with special characters or the text not existing as expected.`,\n\t\t\t\t);\n\t\t\t}\n\n\t\t\t// Write the file back\n\t\t\tconst writeResult = await executor.exec(`printf '%s' ${shellEscape(newContent)} > ${shellEscape(path)}`, {\n\t\t\t\tsignal,\n\t\t\t});\n\t\t\tif (writeResult.code !== 0) {\n\t\t\t\tthrow new Error(writeResult.stderr || `Failed to write file: ${path}`);\n\t\t\t}\n\n\t\t\treturn {\n\t\t\t\tcontent: [\n\t\t\t\t\t{\n\t\t\t\t\t\ttype: \"text\",\n\t\t\t\t\t\ttext: `Successfully replaced text in ${path}. Changed ${oldText.length} characters to ${newText.length} characters.`,\n\t\t\t\t\t},\n\t\t\t\t],\n\t\t\t\tdetails: { diff: generateDiffString(content, newContent) },\n\t\t\t};\n\t\t},\n\t};\n}\n\nfunction shellEscape(s: string): string {\n\treturn `'${s.replace(/'/g, \"'\\\\''\")}'`;\n}\n"]}
@@ -0,0 +1,5 @@
1
+ import type { AgentTool } from "@mariozechner/pi-ai";
2
+ import type { Executor } from "../sandbox.js";
3
+ export { setUploadFunction } from "./attach.js";
4
+ export declare function createMomTools(executor: Executor): AgentTool<any>[];
5
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/tools/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;AACrD,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAO9C,OAAO,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAEhD,wBAAgB,cAAc,CAAC,QAAQ,EAAE,QAAQ,GAAG,SAAS,CAAC,GAAG,CAAC,EAAE,CAQnE","sourcesContent":["import type { AgentTool } from \"@mariozechner/pi-ai\";\nimport type { Executor } from \"../sandbox.js\";\nimport { attachTool } from \"./attach.js\";\nimport { createBashTool } from \"./bash.js\";\nimport { createEditTool } from \"./edit.js\";\nimport { createReadTool } from \"./read.js\";\nimport { createWriteTool } from \"./write.js\";\n\nexport { setUploadFunction } from \"./attach.js\";\n\nexport function createMomTools(executor: Executor): AgentTool<any>[] {\n\treturn [\n\t\tcreateReadTool(executor),\n\t\tcreateBashTool(executor),\n\t\tcreateEditTool(executor),\n\t\tcreateWriteTool(executor),\n\t\tattachTool,\n\t];\n}\n"]}
@@ -0,0 +1,16 @@
1
+ import { attachTool } from "./attach.js";
2
+ import { createBashTool } from "./bash.js";
3
+ import { createEditTool } from "./edit.js";
4
+ import { createReadTool } from "./read.js";
5
+ import { createWriteTool } from "./write.js";
6
+ export { setUploadFunction } from "./attach.js";
7
+ export function createMomTools(executor) {
8
+ return [
9
+ createReadTool(executor),
10
+ createBashTool(executor),
11
+ createEditTool(executor),
12
+ createWriteTool(executor),
13
+ attachTool,
14
+ ];
15
+ }
16
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/tools/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAC3C,OAAO,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAC3C,OAAO,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAC3C,OAAO,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAE7C,OAAO,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAEhD,MAAM,UAAU,cAAc,CAAC,QAAkB,EAAoB;IACpE,OAAO;QACN,cAAc,CAAC,QAAQ,CAAC;QACxB,cAAc,CAAC,QAAQ,CAAC;QACxB,cAAc,CAAC,QAAQ,CAAC;QACxB,eAAe,CAAC,QAAQ,CAAC;QACzB,UAAU;KACV,CAAC;AAAA,CACF","sourcesContent":["import type { AgentTool } from \"@mariozechner/pi-ai\";\nimport type { Executor } from \"../sandbox.js\";\nimport { attachTool } from \"./attach.js\";\nimport { createBashTool } from \"./bash.js\";\nimport { createEditTool } from \"./edit.js\";\nimport { createReadTool } from \"./read.js\";\nimport { createWriteTool } from \"./write.js\";\n\nexport { setUploadFunction } from \"./attach.js\";\n\nexport function createMomTools(executor: Executor): AgentTool<any>[] {\n\treturn [\n\t\tcreateReadTool(executor),\n\t\tcreateBashTool(executor),\n\t\tcreateEditTool(executor),\n\t\tcreateWriteTool(executor),\n\t\tattachTool,\n\t];\n}\n"]}
@@ -0,0 +1,11 @@
1
+ import type { AgentTool } from "@mariozechner/pi-ai";
2
+ import type { Executor } from "../sandbox.js";
3
+ declare const readSchema: import("@sinclair/typebox").TObject<{
4
+ label: import("@sinclair/typebox").TString;
5
+ path: import("@sinclair/typebox").TString;
6
+ offset: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TNumber>;
7
+ limit: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TNumber>;
8
+ }>;
9
+ export declare function createReadTool(executor: Executor): AgentTool<typeof readSchema>;
10
+ export {};
11
+ //# sourceMappingURL=read.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"read.d.ts","sourceRoot":"","sources":["../../src/tools/read.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAA6B,MAAM,qBAAqB,CAAC;AAGhF,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAqB9C,QAAA,MAAM,UAAU;;;;;EAKd,CAAC;AAKH,wBAAgB,cAAc,CAAC,QAAQ,EAAE,QAAQ,GAAG,SAAS,CAAC,OAAO,UAAU,CAAC,CAwF/E","sourcesContent":["import type { AgentTool, ImageContent, TextContent } from \"@mariozechner/pi-ai\";\nimport { Type } from \"@sinclair/typebox\";\nimport { extname } from \"path\";\nimport type { Executor } from \"../sandbox.js\";\n\n/**\n * Map of file extensions to MIME types for common image formats\n */\nconst IMAGE_MIME_TYPES: Record<string, string> = {\n\t\".jpg\": \"image/jpeg\",\n\t\".jpeg\": \"image/jpeg\",\n\t\".png\": \"image/png\",\n\t\".gif\": \"image/gif\",\n\t\".webp\": \"image/webp\",\n};\n\n/**\n * Check if a file is an image based on its extension\n */\nfunction isImageFile(filePath: string): string | null {\n\tconst ext = extname(filePath).toLowerCase();\n\treturn IMAGE_MIME_TYPES[ext] || null;\n}\n\nconst readSchema = Type.Object({\n\tlabel: Type.String({ description: \"Brief description of what you're reading and why (shown to user)\" }),\n\tpath: Type.String({ description: \"Path to the file to read (relative or absolute)\" }),\n\toffset: Type.Optional(Type.Number({ description: \"Line number to start reading from (1-indexed)\" })),\n\tlimit: Type.Optional(Type.Number({ description: \"Maximum number of lines to read\" })),\n});\n\nconst MAX_LINES = 2000;\nconst MAX_LINE_LENGTH = 2000;\n\nexport function createReadTool(executor: Executor): AgentTool<typeof readSchema> {\n\treturn {\n\t\tname: \"read\",\n\t\tlabel: \"read\",\n\t\tdescription:\n\t\t\t\"Read the contents of a file. Supports text files and images (jpg, png, gif, webp). Images are sent as attachments. For text files, defaults to first 2000 lines. Use offset/limit for large files.\",\n\t\tparameters: readSchema,\n\t\texecute: async (\n\t\t\t_toolCallId: string,\n\t\t\t{ path, offset, limit }: { label: string; path: string; offset?: number; limit?: number },\n\t\t\tsignal?: AbortSignal,\n\t\t) => {\n\t\t\tconst mimeType = isImageFile(path);\n\n\t\t\tif (mimeType) {\n\t\t\t\t// Read as image (binary) - use base64\n\t\t\t\tconst result = await executor.exec(`base64 < ${shellEscape(path)}`, { signal });\n\t\t\t\tif (result.code !== 0) {\n\t\t\t\t\tthrow new Error(result.stderr || `Failed to read file: ${path}`);\n\t\t\t\t}\n\t\t\t\tconst base64 = result.stdout.replace(/\\s/g, \"\"); // Remove whitespace from base64\n\n\t\t\t\treturn {\n\t\t\t\t\tcontent: [\n\t\t\t\t\t\t{ type: \"text\", text: `Read image file [${mimeType}]` },\n\t\t\t\t\t\t{ type: \"image\", data: base64, mimeType },\n\t\t\t\t\t] as (TextContent | ImageContent)[],\n\t\t\t\t\tdetails: undefined,\n\t\t\t\t};\n\t\t\t}\n\n\t\t\t// Read as text using cat with offset/limit via sed/head/tail\n\t\t\tlet cmd: string;\n\t\t\tconst startLine = offset ? Math.max(1, offset) : 1;\n\t\t\tconst maxLines = limit || MAX_LINES;\n\n\t\t\tif (startLine === 1) {\n\t\t\t\tcmd = `head -n ${maxLines} ${shellEscape(path)}`;\n\t\t\t} else {\n\t\t\t\tcmd = `sed -n '${startLine},${startLine + maxLines - 1}p' ${shellEscape(path)}`;\n\t\t\t}\n\n\t\t\t// Also get total line count\n\t\t\tconst countResult = await executor.exec(`wc -l < ${shellEscape(path)}`, { signal });\n\t\t\tconst totalLines = Number.parseInt(countResult.stdout.trim(), 10) || 0;\n\n\t\t\tconst result = await executor.exec(cmd, { signal });\n\t\t\tif (result.code !== 0) {\n\t\t\t\tthrow new Error(result.stderr || `Failed to read file: ${path}`);\n\t\t\t}\n\n\t\t\tconst lines = result.stdout.split(\"\\n\");\n\n\t\t\t// Truncate long lines\n\t\t\tlet hadTruncatedLines = false;\n\t\t\tconst formattedLines = lines.map((line) => {\n\t\t\t\tif (line.length > MAX_LINE_LENGTH) {\n\t\t\t\t\thadTruncatedLines = true;\n\t\t\t\t\treturn line.slice(0, MAX_LINE_LENGTH);\n\t\t\t\t}\n\t\t\t\treturn line;\n\t\t\t});\n\n\t\t\tlet outputText = formattedLines.join(\"\\n\");\n\n\t\t\t// Add notices\n\t\t\tconst notices: string[] = [];\n\t\t\tconst endLine = startLine + lines.length - 1;\n\n\t\t\tif (hadTruncatedLines) {\n\t\t\t\tnotices.push(`Some lines were truncated to ${MAX_LINE_LENGTH} characters for display`);\n\t\t\t}\n\n\t\t\tif (endLine < totalLines) {\n\t\t\t\tconst remaining = totalLines - endLine;\n\t\t\t\tnotices.push(`${remaining} more lines not shown. Use offset=${endLine + 1} to continue reading`);\n\t\t\t}\n\n\t\t\tif (notices.length > 0) {\n\t\t\t\toutputText += `\\n\\n... (${notices.join(\". \")})`;\n\t\t\t}\n\n\t\t\treturn {\n\t\t\t\tcontent: [{ type: \"text\", text: outputText }] as (TextContent | ImageContent)[],\n\t\t\t\tdetails: undefined,\n\t\t\t};\n\t\t},\n\t};\n}\n\nfunction shellEscape(s: string): string {\n\treturn `'${s.replace(/'/g, \"'\\\\''\")}'`;\n}\n"]}