@rozek/nanoclaw 0.0.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 (306) hide show
  1. package/.claude/settings.json +1 -0
  2. package/.claude/skills/add-compact/SKILL.md +135 -0
  3. package/.claude/skills/add-discord/SKILL.md +203 -0
  4. package/.claude/skills/add-gmail/SKILL.md +220 -0
  5. package/.claude/skills/add-image-vision/SKILL.md +94 -0
  6. package/.claude/skills/add-ollama-tool/SKILL.md +153 -0
  7. package/.claude/skills/add-parallel/SKILL.md +290 -0
  8. package/.claude/skills/add-pdf-reader/SKILL.md +104 -0
  9. package/.claude/skills/add-reactions/SKILL.md +117 -0
  10. package/.claude/skills/add-slack/SKILL.md +207 -0
  11. package/.claude/skills/add-telegram/SKILL.md +222 -0
  12. package/.claude/skills/add-telegram-swarm/SKILL.md +384 -0
  13. package/.claude/skills/add-voice-transcription/SKILL.md +148 -0
  14. package/.claude/skills/add-whatsapp/SKILL.md +372 -0
  15. package/.claude/skills/convert-to-apple-container/SKILL.md +175 -0
  16. package/.claude/skills/customize/SKILL.md +110 -0
  17. package/.claude/skills/debug/SKILL.md +349 -0
  18. package/.claude/skills/get-qodo-rules/SKILL.md +122 -0
  19. package/.claude/skills/get-qodo-rules/references/output-format.md +41 -0
  20. package/.claude/skills/get-qodo-rules/references/pagination.md +33 -0
  21. package/.claude/skills/get-qodo-rules/references/repository-scope.md +26 -0
  22. package/.claude/skills/qodo-pr-resolver/SKILL.md +326 -0
  23. package/.claude/skills/qodo-pr-resolver/resources/providers.md +329 -0
  24. package/.claude/skills/setup/SKILL.md +218 -0
  25. package/.claude/skills/update-nanoclaw/SKILL.md +235 -0
  26. package/.claude/skills/update-skills/SKILL.md +130 -0
  27. package/.claude/skills/use-local-whisper/SKILL.md +152 -0
  28. package/.claude/skills/x-integration/SKILL.md +417 -0
  29. package/.claude/skills/x-integration/agent.ts +243 -0
  30. package/.claude/skills/x-integration/host.ts +159 -0
  31. package/.claude/skills/x-integration/lib/browser.ts +148 -0
  32. package/.claude/skills/x-integration/lib/config.ts +62 -0
  33. package/.claude/skills/x-integration/scripts/like.ts +56 -0
  34. package/.claude/skills/x-integration/scripts/post.ts +66 -0
  35. package/.claude/skills/x-integration/scripts/quote.ts +80 -0
  36. package/.claude/skills/x-integration/scripts/reply.ts +74 -0
  37. package/.claude/skills/x-integration/scripts/retweet.ts +62 -0
  38. package/.claude/skills/x-integration/scripts/setup.ts +87 -0
  39. package/.env.example +1 -0
  40. package/.github/CODEOWNERS +10 -0
  41. package/.github/PULL_REQUEST_TEMPLATE.md +14 -0
  42. package/.github/workflows/bump-version.yml +32 -0
  43. package/.github/workflows/ci.yml +25 -0
  44. package/.github/workflows/merge-forward-skills.yml +160 -0
  45. package/.github/workflows/update-tokens.yml +42 -0
  46. package/.husky/pre-commit +1 -0
  47. package/.mcp.json +3 -0
  48. package/.nvmrc +1 -0
  49. package/.prettierrc +3 -0
  50. package/CHANGELOG.md +8 -0
  51. package/CLAUDE.md +64 -0
  52. package/CONTRIBUTING.md +23 -0
  53. package/CONTRIBUTORS.md +15 -0
  54. package/LICENSE +21 -0
  55. package/NanoClaw_with_Web-Support.md +325 -0
  56. package/README.md +261 -0
  57. package/README_zh.md +200 -0
  58. package/assets/nanoclaw-favicon.png +0 -0
  59. package/assets/nanoclaw-icon.png +0 -0
  60. package/assets/nanoclaw-logo-dark.png +0 -0
  61. package/assets/nanoclaw-logo.png +0 -0
  62. package/assets/nanoclaw-profile.jpeg +0 -0
  63. package/assets/nanoclaw-sales.png +0 -0
  64. package/assets/social-preview.jpg +0 -0
  65. package/config-examples/mount-allowlist.json +25 -0
  66. package/container/Dockerfile +70 -0
  67. package/container/agent-runner/package.json +21 -0
  68. package/container/agent-runner/src/index.ts +774 -0
  69. package/container/agent-runner/src/ipc-mcp-stdio.ts +338 -0
  70. package/container/agent-runner/tsconfig.json +15 -0
  71. package/container/build.sh +23 -0
  72. package/container/skills/agent-browser/SKILL.md +159 -0
  73. package/container/skills/capabilities/SKILL.md +100 -0
  74. package/container/skills/cwd/SKILL.md +32 -0
  75. package/container/skills/pwd/SKILL.md +19 -0
  76. package/container/skills/status/SKILL.md +104 -0
  77. package/dist/channels/index.d.ts +2 -0
  78. package/dist/channels/index.d.ts.map +1 -0
  79. package/dist/channels/index.js +10 -0
  80. package/dist/channels/index.js.map +1 -0
  81. package/dist/channels/registry.d.ts +13 -0
  82. package/dist/channels/registry.d.ts.map +1 -0
  83. package/dist/channels/registry.js +11 -0
  84. package/dist/channels/registry.js.map +1 -0
  85. package/dist/channels/registry.test.d.ts +2 -0
  86. package/dist/channels/registry.test.d.ts.map +1 -0
  87. package/dist/channels/registry.test.js +32 -0
  88. package/dist/channels/registry.test.js.map +1 -0
  89. package/dist/channels/web.d.ts +2 -0
  90. package/dist/channels/web.d.ts.map +1 -0
  91. package/dist/channels/web.js +1843 -0
  92. package/dist/channels/web.js.map +1 -0
  93. package/dist/cli.d.ts +11 -0
  94. package/dist/cli.d.ts.map +1 -0
  95. package/dist/cli.js +182 -0
  96. package/dist/cli.js.map +1 -0
  97. package/dist/config.d.ts +19 -0
  98. package/dist/config.d.ts.map +1 -0
  99. package/dist/config.js +36 -0
  100. package/dist/config.js.map +1 -0
  101. package/dist/container-runner.d.ts +44 -0
  102. package/dist/container-runner.d.ts.map +1 -0
  103. package/dist/container-runner.js +511 -0
  104. package/dist/container-runner.js.map +1 -0
  105. package/dist/container-runner.test.d.ts +2 -0
  106. package/dist/container-runner.test.d.ts.map +1 -0
  107. package/dist/container-runner.test.js +150 -0
  108. package/dist/container-runner.test.js.map +1 -0
  109. package/dist/container-runtime.d.ts +22 -0
  110. package/dist/container-runtime.d.ts.map +1 -0
  111. package/dist/container-runtime.js +96 -0
  112. package/dist/container-runtime.js.map +1 -0
  113. package/dist/container-runtime.test.d.ts +2 -0
  114. package/dist/container-runtime.test.d.ts.map +1 -0
  115. package/dist/container-runtime.test.js +93 -0
  116. package/dist/container-runtime.test.js.map +1 -0
  117. package/dist/credential-proxy.d.ts +21 -0
  118. package/dist/credential-proxy.d.ts.map +1 -0
  119. package/dist/credential-proxy.js +95 -0
  120. package/dist/credential-proxy.js.map +1 -0
  121. package/dist/credential-proxy.test.d.ts +2 -0
  122. package/dist/credential-proxy.test.d.ts.map +1 -0
  123. package/dist/credential-proxy.test.js +134 -0
  124. package/dist/credential-proxy.test.js.map +1 -0
  125. package/dist/db.d.ts +115 -0
  126. package/dist/db.d.ts.map +1 -0
  127. package/dist/db.js +549 -0
  128. package/dist/db.js.map +1 -0
  129. package/dist/db.test.d.ts +2 -0
  130. package/dist/db.test.d.ts.map +1 -0
  131. package/dist/db.test.js +360 -0
  132. package/dist/db.test.js.map +1 -0
  133. package/dist/env.d.ts +8 -0
  134. package/dist/env.d.ts.map +1 -0
  135. package/dist/env.js +42 -0
  136. package/dist/env.js.map +1 -0
  137. package/dist/formatting.test.d.ts +2 -0
  138. package/dist/formatting.test.d.ts.map +1 -0
  139. package/dist/formatting.test.js +183 -0
  140. package/dist/formatting.test.js.map +1 -0
  141. package/dist/group-folder.d.ts +5 -0
  142. package/dist/group-folder.d.ts.map +1 -0
  143. package/dist/group-folder.js +44 -0
  144. package/dist/group-folder.js.map +1 -0
  145. package/dist/group-folder.test.d.ts +2 -0
  146. package/dist/group-folder.test.d.ts.map +1 -0
  147. package/dist/group-folder.test.js +29 -0
  148. package/dist/group-folder.test.js.map +1 -0
  149. package/dist/group-queue.d.ts +40 -0
  150. package/dist/group-queue.d.ts.map +1 -0
  151. package/dist/group-queue.js +276 -0
  152. package/dist/group-queue.js.map +1 -0
  153. package/dist/group-queue.test.d.ts +2 -0
  154. package/dist/group-queue.test.d.ts.map +1 -0
  155. package/dist/group-queue.test.js +341 -0
  156. package/dist/group-queue.test.js.map +1 -0
  157. package/dist/index.d.ts +13 -0
  158. package/dist/index.d.ts.map +1 -0
  159. package/dist/index.js +592 -0
  160. package/dist/index.js.map +1 -0
  161. package/dist/ipc-auth.test.d.ts +2 -0
  162. package/dist/ipc-auth.test.d.ts.map +1 -0
  163. package/dist/ipc-auth.test.js +434 -0
  164. package/dist/ipc-auth.test.js.map +1 -0
  165. package/dist/ipc.d.ts +32 -0
  166. package/dist/ipc.d.ts.map +1 -0
  167. package/dist/ipc.js +311 -0
  168. package/dist/ipc.js.map +1 -0
  169. package/dist/logger.d.ts +3 -0
  170. package/dist/logger.d.ts.map +1 -0
  171. package/dist/logger.js +14 -0
  172. package/dist/logger.js.map +1 -0
  173. package/dist/mount-security.d.ts +34 -0
  174. package/dist/mount-security.d.ts.map +1 -0
  175. package/dist/mount-security.js +325 -0
  176. package/dist/mount-security.js.map +1 -0
  177. package/dist/remote-control.d.ts +32 -0
  178. package/dist/remote-control.d.ts.map +1 -0
  179. package/dist/remote-control.js +185 -0
  180. package/dist/remote-control.js.map +1 -0
  181. package/dist/remote-control.test.d.ts +2 -0
  182. package/dist/remote-control.test.d.ts.map +1 -0
  183. package/dist/remote-control.test.js +321 -0
  184. package/dist/remote-control.test.js.map +1 -0
  185. package/dist/router.d.ts +8 -0
  186. package/dist/router.d.ts.map +1 -0
  187. package/dist/router.js +37 -0
  188. package/dist/router.js.map +1 -0
  189. package/dist/routing.test.d.ts +2 -0
  190. package/dist/routing.test.d.ts.map +1 -0
  191. package/dist/routing.test.js +81 -0
  192. package/dist/routing.test.js.map +1 -0
  193. package/dist/sender-allowlist.d.ts +14 -0
  194. package/dist/sender-allowlist.d.ts.map +1 -0
  195. package/dist/sender-allowlist.js +79 -0
  196. package/dist/sender-allowlist.js.map +1 -0
  197. package/dist/sender-allowlist.test.d.ts +2 -0
  198. package/dist/sender-allowlist.test.d.ts.map +1 -0
  199. package/dist/sender-allowlist.test.js +186 -0
  200. package/dist/sender-allowlist.test.js.map +1 -0
  201. package/dist/session-commands.d.ts +47 -0
  202. package/dist/session-commands.d.ts.map +1 -0
  203. package/dist/session-commands.js +104 -0
  204. package/dist/session-commands.js.map +1 -0
  205. package/dist/session-commands.test.d.ts +2 -0
  206. package/dist/session-commands.test.d.ts.map +1 -0
  207. package/dist/session-commands.test.js +194 -0
  208. package/dist/session-commands.test.js.map +1 -0
  209. package/dist/task-scheduler.d.ts +22 -0
  210. package/dist/task-scheduler.d.ts.map +1 -0
  211. package/dist/task-scheduler.js +241 -0
  212. package/dist/task-scheduler.js.map +1 -0
  213. package/dist/task-scheduler.test.d.ts +2 -0
  214. package/dist/task-scheduler.test.d.ts.map +1 -0
  215. package/dist/task-scheduler.test.js +107 -0
  216. package/dist/task-scheduler.test.js.map +1 -0
  217. package/dist/timezone.d.ts +6 -0
  218. package/dist/timezone.d.ts.map +1 -0
  219. package/dist/timezone.js +17 -0
  220. package/dist/timezone.js.map +1 -0
  221. package/dist/timezone.test.d.ts +2 -0
  222. package/dist/timezone.test.d.ts.map +1 -0
  223. package/dist/timezone.test.js +23 -0
  224. package/dist/timezone.test.js.map +1 -0
  225. package/dist/types.d.ts +79 -0
  226. package/dist/types.d.ts.map +1 -0
  227. package/dist/types.js +2 -0
  228. package/dist/types.js.map +1 -0
  229. package/docs/APPLE-CONTAINER-NETWORKING.md +90 -0
  230. package/docs/DEBUG_CHECKLIST.md +143 -0
  231. package/docs/REQUIREMENTS.md +196 -0
  232. package/docs/SDK_DEEP_DIVE.md +643 -0
  233. package/docs/SECURITY.md +122 -0
  234. package/docs/SPEC.md +785 -0
  235. package/docs/docker-sandboxes.md +359 -0
  236. package/docs/nanoclaw-architecture-final.md +1063 -0
  237. package/docs/nanorepo-architecture.md +168 -0
  238. package/docs/skills-as-branches.md +662 -0
  239. package/groups/global/CLAUDE.md +58 -0
  240. package/groups/main/CLAUDE.md +246 -0
  241. package/launchd/com.nanoclaw.plist +32 -0
  242. package/package.json +45 -0
  243. package/repo-tokens/README.md +113 -0
  244. package/repo-tokens/action.yml +186 -0
  245. package/repo-tokens/badge.svg +23 -0
  246. package/repo-tokens/examples/green.svg +14 -0
  247. package/repo-tokens/examples/red.svg +14 -0
  248. package/repo-tokens/examples/yellow-green.svg +14 -0
  249. package/repo-tokens/examples/yellow.svg +14 -0
  250. package/scripts/run-migrations.ts +105 -0
  251. package/setup/container.ts +144 -0
  252. package/setup/environment.test.ts +121 -0
  253. package/setup/environment.ts +94 -0
  254. package/setup/groups.ts +229 -0
  255. package/setup/index.ts +58 -0
  256. package/setup/mounts.ts +115 -0
  257. package/setup/platform.test.ts +120 -0
  258. package/setup/platform.ts +132 -0
  259. package/setup/register.test.ts +257 -0
  260. package/setup/register.ts +177 -0
  261. package/setup/service.test.ts +187 -0
  262. package/setup/service.ts +362 -0
  263. package/setup/status.ts +16 -0
  264. package/setup/verify.ts +192 -0
  265. package/setup.sh +161 -0
  266. package/src/channels/index.ts +15 -0
  267. package/src/channels/registry.test.ts +42 -0
  268. package/src/channels/registry.ts +32 -0
  269. package/src/channels/web.ts +1931 -0
  270. package/src/cli.ts +209 -0
  271. package/src/config.ts +73 -0
  272. package/src/container-runner.test.ts +210 -0
  273. package/src/container-runner.ts +768 -0
  274. package/src/container-runtime.test.ts +149 -0
  275. package/src/container-runtime.ts +127 -0
  276. package/src/credential-proxy.test.ts +192 -0
  277. package/src/credential-proxy.ts +125 -0
  278. package/src/db.test.ts +484 -0
  279. package/src/db.ts +803 -0
  280. package/src/env.ts +42 -0
  281. package/src/formatting.test.ts +256 -0
  282. package/src/group-folder.test.ts +43 -0
  283. package/src/group-folder.ts +44 -0
  284. package/src/group-queue.test.ts +484 -0
  285. package/src/group-queue.ts +379 -0
  286. package/src/index.ts +832 -0
  287. package/src/ipc-auth.test.ts +679 -0
  288. package/src/ipc.ts +461 -0
  289. package/src/logger.ts +16 -0
  290. package/src/mount-security.ts +419 -0
  291. package/src/remote-control.test.ts +397 -0
  292. package/src/remote-control.ts +224 -0
  293. package/src/router.ts +52 -0
  294. package/src/routing.test.ts +170 -0
  295. package/src/sender-allowlist.test.ts +216 -0
  296. package/src/sender-allowlist.ts +128 -0
  297. package/src/session-commands.test.ts +247 -0
  298. package/src/session-commands.ts +163 -0
  299. package/src/task-scheduler.test.ts +129 -0
  300. package/src/task-scheduler.ts +328 -0
  301. package/src/timezone.test.ts +29 -0
  302. package/src/timezone.ts +16 -0
  303. package/src/types.ts +109 -0
  304. package/tsconfig.json +20 -0
  305. package/vitest.config.ts +7 -0
  306. package/vitest.skills.config.ts +7 -0
@@ -0,0 +1,243 @@
1
+ /**
2
+ * X Integration - MCP Tool Definitions (Agent/Container Side)
3
+ *
4
+ * These tools run inside the container and communicate with the host via IPC.
5
+ * The host-side implementation is in host.ts.
6
+ *
7
+ * Note: This file is compiled in the container, not on the host.
8
+ * The @ts-ignore is needed because the SDK is only available in the container.
9
+ */
10
+
11
+ // @ts-ignore - SDK available in container environment only
12
+ import { tool } from '@anthropic-ai/claude-agent-sdk';
13
+ import { z } from 'zod';
14
+ import fs from 'fs';
15
+ import path from 'path';
16
+
17
+ // IPC directories (inside container)
18
+ const IPC_DIR = '/workspace/ipc';
19
+ const TASKS_DIR = path.join(IPC_DIR, 'tasks');
20
+ const RESULTS_DIR = path.join(IPC_DIR, 'x_results');
21
+
22
+ function writeIpcFile(dir: string, data: object): string {
23
+ fs.mkdirSync(dir, { recursive: true });
24
+ const filename = `${Date.now()}-${Math.random().toString(36).slice(2, 8)}.json`;
25
+ const filepath = path.join(dir, filename);
26
+ const tempPath = `${filepath}.tmp`;
27
+ fs.writeFileSync(tempPath, JSON.stringify(data, null, 2));
28
+ fs.renameSync(tempPath, filepath);
29
+ return filename;
30
+ }
31
+
32
+ async function waitForResult(requestId: string, maxWait = 60000): Promise<{ success: boolean; message: string }> {
33
+ const resultFile = path.join(RESULTS_DIR, `${requestId}.json`);
34
+ const pollInterval = 1000;
35
+ let elapsed = 0;
36
+
37
+ while (elapsed < maxWait) {
38
+ if (fs.existsSync(resultFile)) {
39
+ try {
40
+ const result = JSON.parse(fs.readFileSync(resultFile, 'utf-8'));
41
+ fs.unlinkSync(resultFile);
42
+ return result;
43
+ } catch (err) {
44
+ return { success: false, message: `Failed to read result: ${err}` };
45
+ }
46
+ }
47
+ await new Promise(resolve => setTimeout(resolve, pollInterval));
48
+ elapsed += pollInterval;
49
+ }
50
+
51
+ return { success: false, message: 'Request timed out' };
52
+ }
53
+
54
+ export interface SkillToolsContext {
55
+ groupFolder: string;
56
+ isMain: boolean;
57
+ }
58
+
59
+ /**
60
+ * Create X integration MCP tools
61
+ */
62
+ export function createXTools(ctx: SkillToolsContext) {
63
+ const { groupFolder, isMain } = ctx;
64
+
65
+ return [
66
+ tool(
67
+ 'x_post',
68
+ `Post a tweet to X (Twitter). Main group only.
69
+
70
+ The host machine will execute the browser automation to post the tweet.
71
+ Make sure the content is appropriate and within X's character limit (280 chars for text).`,
72
+ {
73
+ content: z.string().max(280).describe('The tweet content to post (max 280 characters)')
74
+ },
75
+ async (args: { content: string }) => {
76
+ if (!isMain) {
77
+ return {
78
+ content: [{ type: 'text', text: 'Only the main group can post tweets.' }],
79
+ isError: true
80
+ };
81
+ }
82
+
83
+ if (args.content.length > 280) {
84
+ return {
85
+ content: [{ type: 'text', text: `Tweet exceeds 280 character limit (current: ${args.content.length})` }],
86
+ isError: true
87
+ };
88
+ }
89
+
90
+ const requestId = `xpost-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`;
91
+ writeIpcFile(TASKS_DIR, {
92
+ type: 'x_post',
93
+ requestId,
94
+ content: args.content,
95
+ groupFolder,
96
+ timestamp: new Date().toISOString()
97
+ });
98
+
99
+ const result = await waitForResult(requestId);
100
+ return {
101
+ content: [{ type: 'text', text: result.message }],
102
+ isError: !result.success
103
+ };
104
+ }
105
+ ),
106
+
107
+ tool(
108
+ 'x_like',
109
+ `Like a tweet on X (Twitter). Main group only.
110
+
111
+ Provide the tweet URL or tweet ID to like.`,
112
+ {
113
+ tweet_url: z.string().describe('The tweet URL (e.g., https://x.com/user/status/123) or tweet ID')
114
+ },
115
+ async (args: { tweet_url: string }) => {
116
+ if (!isMain) {
117
+ return {
118
+ content: [{ type: 'text', text: 'Only the main group can interact with X.' }],
119
+ isError: true
120
+ };
121
+ }
122
+
123
+ const requestId = `xlike-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`;
124
+ writeIpcFile(TASKS_DIR, {
125
+ type: 'x_like',
126
+ requestId,
127
+ tweetUrl: args.tweet_url,
128
+ groupFolder,
129
+ timestamp: new Date().toISOString()
130
+ });
131
+
132
+ const result = await waitForResult(requestId);
133
+ return {
134
+ content: [{ type: 'text', text: result.message }],
135
+ isError: !result.success
136
+ };
137
+ }
138
+ ),
139
+
140
+ tool(
141
+ 'x_reply',
142
+ `Reply to a tweet on X (Twitter). Main group only.
143
+
144
+ Provide the tweet URL and your reply content.`,
145
+ {
146
+ tweet_url: z.string().describe('The tweet URL (e.g., https://x.com/user/status/123) or tweet ID'),
147
+ content: z.string().max(280).describe('The reply content (max 280 characters)')
148
+ },
149
+ async (args: { tweet_url: string; content: string }) => {
150
+ if (!isMain) {
151
+ return {
152
+ content: [{ type: 'text', text: 'Only the main group can interact with X.' }],
153
+ isError: true
154
+ };
155
+ }
156
+
157
+ const requestId = `xreply-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`;
158
+ writeIpcFile(TASKS_DIR, {
159
+ type: 'x_reply',
160
+ requestId,
161
+ tweetUrl: args.tweet_url,
162
+ content: args.content,
163
+ groupFolder,
164
+ timestamp: new Date().toISOString()
165
+ });
166
+
167
+ const result = await waitForResult(requestId);
168
+ return {
169
+ content: [{ type: 'text', text: result.message }],
170
+ isError: !result.success
171
+ };
172
+ }
173
+ ),
174
+
175
+ tool(
176
+ 'x_retweet',
177
+ `Retweet a tweet on X (Twitter). Main group only.
178
+
179
+ Provide the tweet URL to retweet.`,
180
+ {
181
+ tweet_url: z.string().describe('The tweet URL (e.g., https://x.com/user/status/123) or tweet ID')
182
+ },
183
+ async (args: { tweet_url: string }) => {
184
+ if (!isMain) {
185
+ return {
186
+ content: [{ type: 'text', text: 'Only the main group can interact with X.' }],
187
+ isError: true
188
+ };
189
+ }
190
+
191
+ const requestId = `xretweet-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`;
192
+ writeIpcFile(TASKS_DIR, {
193
+ type: 'x_retweet',
194
+ requestId,
195
+ tweetUrl: args.tweet_url,
196
+ groupFolder,
197
+ timestamp: new Date().toISOString()
198
+ });
199
+
200
+ const result = await waitForResult(requestId);
201
+ return {
202
+ content: [{ type: 'text', text: result.message }],
203
+ isError: !result.success
204
+ };
205
+ }
206
+ ),
207
+
208
+ tool(
209
+ 'x_quote',
210
+ `Quote tweet on X (Twitter). Main group only.
211
+
212
+ Retweet with your own comment added.`,
213
+ {
214
+ tweet_url: z.string().describe('The tweet URL (e.g., https://x.com/user/status/123) or tweet ID'),
215
+ comment: z.string().max(280).describe('Your comment for the quote tweet (max 280 characters)')
216
+ },
217
+ async (args: { tweet_url: string; comment: string }) => {
218
+ if (!isMain) {
219
+ return {
220
+ content: [{ type: 'text', text: 'Only the main group can interact with X.' }],
221
+ isError: true
222
+ };
223
+ }
224
+
225
+ const requestId = `xquote-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`;
226
+ writeIpcFile(TASKS_DIR, {
227
+ type: 'x_quote',
228
+ requestId,
229
+ tweetUrl: args.tweet_url,
230
+ comment: args.comment,
231
+ groupFolder,
232
+ timestamp: new Date().toISOString()
233
+ });
234
+
235
+ const result = await waitForResult(requestId);
236
+ return {
237
+ content: [{ type: 'text', text: result.message }],
238
+ isError: !result.success
239
+ };
240
+ }
241
+ )
242
+ ];
243
+ }
@@ -0,0 +1,159 @@
1
+ /**
2
+ * X Integration IPC Handler
3
+ *
4
+ * Handles all x_* IPC messages from container agents.
5
+ * This is the entry point for X integration in the host process.
6
+ */
7
+
8
+ import { spawn } from 'child_process';
9
+ import fs from 'fs';
10
+ import path from 'path';
11
+ import pino from 'pino';
12
+
13
+ const logger = pino({
14
+ level: process.env.LOG_LEVEL || 'info',
15
+ transport: { target: 'pino-pretty', options: { colorize: true } }
16
+ });
17
+
18
+ interface SkillResult {
19
+ success: boolean;
20
+ message: string;
21
+ data?: unknown;
22
+ }
23
+
24
+ // Run a skill script as subprocess
25
+ async function runScript(script: string, args: object): Promise<SkillResult> {
26
+ const scriptPath = path.join(process.cwd(), '.claude', 'skills', 'x-integration', 'scripts', `${script}.ts`);
27
+
28
+ return new Promise((resolve) => {
29
+ const proc = spawn('npx', ['tsx', scriptPath], {
30
+ cwd: process.cwd(),
31
+ env: { ...process.env, NANOCLAW_ROOT: process.cwd() },
32
+ stdio: ['pipe', 'pipe', 'pipe']
33
+ });
34
+
35
+ let stdout = '';
36
+ proc.stdout.on('data', (data) => { stdout += data.toString(); });
37
+ proc.stdin.write(JSON.stringify(args));
38
+ proc.stdin.end();
39
+
40
+ const timer = setTimeout(() => {
41
+ proc.kill('SIGTERM');
42
+ resolve({ success: false, message: 'Script timed out (120s)' });
43
+ }, 120000);
44
+
45
+ proc.on('close', (code) => {
46
+ clearTimeout(timer);
47
+ if (code !== 0) {
48
+ resolve({ success: false, message: `Script exited with code: ${code}` });
49
+ return;
50
+ }
51
+ try {
52
+ const lines = stdout.trim().split('\n');
53
+ resolve(JSON.parse(lines[lines.length - 1]));
54
+ } catch {
55
+ resolve({ success: false, message: `Failed to parse output: ${stdout.slice(0, 200)}` });
56
+ }
57
+ });
58
+
59
+ proc.on('error', (err) => {
60
+ clearTimeout(timer);
61
+ resolve({ success: false, message: `Failed to spawn: ${err.message}` });
62
+ });
63
+ });
64
+ }
65
+
66
+ // Write result to IPC results directory
67
+ function writeResult(dataDir: string, sourceGroup: string, requestId: string, result: SkillResult): void {
68
+ const resultsDir = path.join(dataDir, 'ipc', sourceGroup, 'x_results');
69
+ fs.mkdirSync(resultsDir, { recursive: true });
70
+ fs.writeFileSync(path.join(resultsDir, `${requestId}.json`), JSON.stringify(result));
71
+ }
72
+
73
+ /**
74
+ * Handle X integration IPC messages
75
+ *
76
+ * @returns true if message was handled, false if not an X message
77
+ */
78
+ export async function handleXIpc(
79
+ data: Record<string, unknown>,
80
+ sourceGroup: string,
81
+ isMain: boolean,
82
+ dataDir: string
83
+ ): Promise<boolean> {
84
+ const type = data.type as string;
85
+
86
+ // Only handle x_* types
87
+ if (!type?.startsWith('x_')) {
88
+ return false;
89
+ }
90
+
91
+ // Only main group can use X integration
92
+ if (!isMain) {
93
+ logger.warn({ sourceGroup, type }, 'X integration blocked: not main group');
94
+ return true;
95
+ }
96
+
97
+ const requestId = data.requestId as string;
98
+ if (!requestId) {
99
+ logger.warn({ type }, 'X integration blocked: missing requestId');
100
+ return true;
101
+ }
102
+
103
+ logger.info({ type, requestId }, 'Processing X request');
104
+
105
+ let result: SkillResult;
106
+
107
+ switch (type) {
108
+ case 'x_post':
109
+ if (!data.content) {
110
+ result = { success: false, message: 'Missing content' };
111
+ break;
112
+ }
113
+ result = await runScript('post', { content: data.content });
114
+ break;
115
+
116
+ case 'x_like':
117
+ if (!data.tweetUrl) {
118
+ result = { success: false, message: 'Missing tweetUrl' };
119
+ break;
120
+ }
121
+ result = await runScript('like', { tweetUrl: data.tweetUrl });
122
+ break;
123
+
124
+ case 'x_reply':
125
+ if (!data.tweetUrl || !data.content) {
126
+ result = { success: false, message: 'Missing tweetUrl or content' };
127
+ break;
128
+ }
129
+ result = await runScript('reply', { tweetUrl: data.tweetUrl, content: data.content });
130
+ break;
131
+
132
+ case 'x_retweet':
133
+ if (!data.tweetUrl) {
134
+ result = { success: false, message: 'Missing tweetUrl' };
135
+ break;
136
+ }
137
+ result = await runScript('retweet', { tweetUrl: data.tweetUrl });
138
+ break;
139
+
140
+ case 'x_quote':
141
+ if (!data.tweetUrl || !data.comment) {
142
+ result = { success: false, message: 'Missing tweetUrl or comment' };
143
+ break;
144
+ }
145
+ result = await runScript('quote', { tweetUrl: data.tweetUrl, comment: data.comment });
146
+ break;
147
+
148
+ default:
149
+ return false;
150
+ }
151
+
152
+ writeResult(dataDir, sourceGroup, requestId, result);
153
+ if (result.success) {
154
+ logger.info({ type, requestId }, 'X request completed');
155
+ } else {
156
+ logger.error({ type, requestId, message: result.message }, 'X request failed');
157
+ }
158
+ return true;
159
+ }
@@ -0,0 +1,148 @@
1
+ /**
2
+ * X Integration - Shared utilities
3
+ * Used by all X scripts
4
+ */
5
+
6
+ import { chromium, BrowserContext, Page } from 'playwright';
7
+ import fs from 'fs';
8
+ import path from 'path';
9
+ import { config } from './config.js';
10
+
11
+ export { config };
12
+
13
+ export interface ScriptResult {
14
+ success: boolean;
15
+ message: string;
16
+ data?: unknown;
17
+ }
18
+
19
+ /**
20
+ * Read input from stdin
21
+ */
22
+ export async function readInput<T>(): Promise<T> {
23
+ return new Promise((resolve, reject) => {
24
+ let data = '';
25
+ process.stdin.setEncoding('utf8');
26
+ process.stdin.on('data', chunk => { data += chunk; });
27
+ process.stdin.on('end', () => {
28
+ try {
29
+ resolve(JSON.parse(data));
30
+ } catch (err) {
31
+ reject(new Error(`Invalid JSON input: ${err}`));
32
+ }
33
+ });
34
+ process.stdin.on('error', reject);
35
+ });
36
+ }
37
+
38
+ /**
39
+ * Write result to stdout
40
+ */
41
+ export function writeResult(result: ScriptResult): void {
42
+ console.log(JSON.stringify(result));
43
+ }
44
+
45
+ /**
46
+ * Clean up browser lock files
47
+ */
48
+ export function cleanupLockFiles(): void {
49
+ for (const lockFile of ['SingletonLock', 'SingletonSocket', 'SingletonCookie']) {
50
+ const lockPath = path.join(config.browserDataDir, lockFile);
51
+ if (fs.existsSync(lockPath)) {
52
+ try { fs.unlinkSync(lockPath); } catch {}
53
+ }
54
+ }
55
+ }
56
+
57
+ /**
58
+ * Validate tweet/reply content
59
+ */
60
+ export function validateContent(content: string | undefined, type = 'Tweet'): ScriptResult | null {
61
+ if (!content || content.length === 0) {
62
+ return { success: false, message: `${type} content cannot be empty` };
63
+ }
64
+ if (content.length > config.limits.tweetMaxLength) {
65
+ return { success: false, message: `${type} exceeds ${config.limits.tweetMaxLength} character limit (current: ${content.length})` };
66
+ }
67
+ return null; // Valid
68
+ }
69
+
70
+ /**
71
+ * Get browser context with persistent profile
72
+ */
73
+ export async function getBrowserContext(): Promise<BrowserContext> {
74
+ if (!fs.existsSync(config.authPath)) {
75
+ throw new Error('X authentication not configured. Run /x-integration to complete login.');
76
+ }
77
+
78
+ cleanupLockFiles();
79
+
80
+ const context = await chromium.launchPersistentContext(config.browserDataDir, {
81
+ executablePath: config.chromePath,
82
+ headless: false,
83
+ viewport: config.viewport,
84
+ args: config.chromeArgs,
85
+ ignoreDefaultArgs: config.chromeIgnoreDefaultArgs,
86
+ });
87
+
88
+ return context;
89
+ }
90
+
91
+ /**
92
+ * Extract tweet ID from URL or raw ID
93
+ */
94
+ export function extractTweetId(input: string): string | null {
95
+ const urlMatch = input.match(/(?:x\.com|twitter\.com)\/\w+\/status\/(\d+)/);
96
+ if (urlMatch) return urlMatch[1];
97
+ if (/^\d+$/.test(input.trim())) return input.trim();
98
+ return null;
99
+ }
100
+
101
+ /**
102
+ * Navigate to a tweet page
103
+ */
104
+ export async function navigateToTweet(
105
+ context: BrowserContext,
106
+ tweetUrl: string
107
+ ): Promise<{ page: Page; success: boolean; error?: string }> {
108
+ const page = context.pages()[0] || await context.newPage();
109
+
110
+ let url = tweetUrl;
111
+ const tweetId = extractTweetId(tweetUrl);
112
+ if (tweetId && !tweetUrl.startsWith('http')) {
113
+ url = `https://x.com/i/status/${tweetId}`;
114
+ }
115
+
116
+ try {
117
+ await page.goto(url, { timeout: config.timeouts.navigation, waitUntil: 'domcontentloaded' });
118
+ await page.waitForTimeout(config.timeouts.pageLoad);
119
+
120
+ const exists = await page.locator('article[data-testid="tweet"]').first().isVisible().catch(() => false);
121
+ if (!exists) {
122
+ return { page, success: false, error: 'Tweet not found. It may have been deleted or the URL is invalid.' };
123
+ }
124
+
125
+ return { page, success: true };
126
+ } catch (err) {
127
+ return { page, success: false, error: `Navigation failed: ${err instanceof Error ? err.message : String(err)}` };
128
+ }
129
+ }
130
+
131
+ /**
132
+ * Run script with error handling
133
+ */
134
+ export async function runScript<T>(
135
+ handler: (input: T) => Promise<ScriptResult>
136
+ ): Promise<void> {
137
+ try {
138
+ const input = await readInput<T>();
139
+ const result = await handler(input);
140
+ writeResult(result);
141
+ } catch (err) {
142
+ writeResult({
143
+ success: false,
144
+ message: `Script execution failed: ${err instanceof Error ? err.message : String(err)}`
145
+ });
146
+ process.exit(1);
147
+ }
148
+ }
@@ -0,0 +1,62 @@
1
+ /**
2
+ * X Integration - Configuration
3
+ *
4
+ * All environment-specific settings in one place.
5
+ * Override via environment variables or modify defaults here.
6
+ */
7
+
8
+ import path from 'path';
9
+
10
+ // Project root - can be overridden for different deployments
11
+ const PROJECT_ROOT = process.env.NANOCLAW_ROOT || process.cwd();
12
+
13
+ /**
14
+ * Configuration object with all settings
15
+ */
16
+ export const config = {
17
+ // Chrome executable path
18
+ // Default: standard macOS Chrome location
19
+ // Override: CHROME_PATH environment variable
20
+ chromePath: process.env.CHROME_PATH || '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',
21
+
22
+ // Browser profile directory for persistent login sessions
23
+ browserDataDir: path.join(PROJECT_ROOT, 'data', 'x-browser-profile'),
24
+
25
+ // Auth state marker file
26
+ authPath: path.join(PROJECT_ROOT, 'data', 'x-auth.json'),
27
+
28
+ // Browser viewport settings
29
+ viewport: {
30
+ width: 1280,
31
+ height: 800,
32
+ },
33
+
34
+ // Timeouts (in milliseconds)
35
+ timeouts: {
36
+ navigation: 30000,
37
+ elementWait: 5000,
38
+ afterClick: 1000,
39
+ afterFill: 1000,
40
+ afterSubmit: 3000,
41
+ pageLoad: 3000,
42
+ },
43
+
44
+ // X character limits
45
+ limits: {
46
+ tweetMaxLength: 280,
47
+ },
48
+
49
+ // Chrome launch arguments
50
+ chromeArgs: [
51
+ '--disable-blink-features=AutomationControlled',
52
+ '--no-sandbox',
53
+ '--disable-setuid-sandbox',
54
+ '--no-first-run',
55
+ '--no-default-browser-check',
56
+ '--disable-sync',
57
+ ],
58
+
59
+ // Args to ignore when launching Chrome
60
+ chromeIgnoreDefaultArgs: ['--enable-automation'],
61
+ };
62
+
@@ -0,0 +1,56 @@
1
+ #!/usr/bin/env npx tsx
2
+ /**
3
+ * X Integration - Like Tweet
4
+ * Usage: echo '{"tweetUrl":"https://x.com/user/status/123"}' | npx tsx like.ts
5
+ */
6
+
7
+ import { getBrowserContext, navigateToTweet, runScript, config, ScriptResult } from '../lib/browser.js';
8
+
9
+ interface LikeInput {
10
+ tweetUrl: string;
11
+ }
12
+
13
+ async function likeTweet(input: LikeInput): Promise<ScriptResult> {
14
+ const { tweetUrl } = input;
15
+
16
+ if (!tweetUrl) {
17
+ return { success: false, message: 'Please provide a tweet URL' };
18
+ }
19
+
20
+ let context = null;
21
+ try {
22
+ context = await getBrowserContext();
23
+ const { page, success, error } = await navigateToTweet(context, tweetUrl);
24
+
25
+ if (!success) {
26
+ return { success: false, message: error || 'Navigation failed' };
27
+ }
28
+
29
+ const tweet = page.locator('article[data-testid="tweet"]').first();
30
+ const unlikeButton = tweet.locator('[data-testid="unlike"]');
31
+ const likeButton = tweet.locator('[data-testid="like"]');
32
+
33
+ // Check if already liked
34
+ const alreadyLiked = await unlikeButton.isVisible().catch(() => false);
35
+ if (alreadyLiked) {
36
+ return { success: true, message: 'Tweet already liked' };
37
+ }
38
+
39
+ await likeButton.waitFor({ timeout: config.timeouts.elementWait });
40
+ await likeButton.click();
41
+ await page.waitForTimeout(config.timeouts.afterClick);
42
+
43
+ // Verify
44
+ const nowLiked = await unlikeButton.isVisible().catch(() => false);
45
+ if (nowLiked) {
46
+ return { success: true, message: 'Like successful' };
47
+ }
48
+
49
+ return { success: false, message: 'Like action completed but could not verify success' };
50
+
51
+ } finally {
52
+ if (context) await context.close();
53
+ }
54
+ }
55
+
56
+ runScript<LikeInput>(likeTweet);