Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ storage.cirru
backups/

.DS_Store
lib
23 changes: 23 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@


.compact-inc.cirru
.calcit-error.cirru

js-out/
dist/

yarn-error.log

storage.cirru

backups/

.DS_Store

*.cirru

.github

tsconfig.json

index.html
12 changes: 12 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"files.exclude": {
"lib/**": true,
"js-out/**": true,
"node_modules/**": true,
"**/.git": true,
"**/.svn": true,
"**/.hg": true,
"**/.DS_Store": true,
"**/Thumbs.db": true
}
}
23 changes: 20 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@

Macrophylla
----
## Macrophylla

> try smarter CLI tools, still experimenting...

Macrophylla 是一个命令行助手,它使用 Gemini API 与用户交互,并能够执行 Bash 命令和 Node.js 代码。

### 使用方式

1. **准备 Gemini API Key:** 确保你已经设置了 `GEMINI_API_KEY` 环境变量。

2. **运行工具:** 直接运行该工具。

3. **与助手交互:** 工具会提示你输入任务描述。 你可以使用自然语言描述你的需求。

4. **执行 Bash 命令和 Node.js 代码:** 工具会根据你的描述,自动判断是否需要执行 Bash 命令或 Node.js 代码来完成任务。 如果需要执行,会先向你确认,然后执行并将结果返回。

5. **示例:**

- 用户: 读取当前目录下的 `README.md` 文件内容。
- 助手: (判断需要执行 Bash 命令) Bash command to execute: `cat README.md` Execute this Bash command? (y/n):
- 用户: y
- 助手: (执行命令,并将结果返回)

### License

MIT
3 changes: 3 additions & 0 deletions bin.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env node

import "./lib/main.mjs";
Copy link

Copilot AI Apr 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The import path in bin.mjs points to './lib/main.mjs' while the main module is defined in 'src/main.mts'. Update the import path to correctly reference the intended module.

Suggested change
import "./lib/main.mjs";
import "../src/main.mts";

Copilot uses AI. Check for mistakes.
183 changes: 99 additions & 84 deletions calcit.cirru

Large diffs are not rendered by default.

30 changes: 17 additions & 13 deletions compact.cirru
Original file line number Diff line number Diff line change
Expand Up @@ -125,20 +125,10 @@
router-data $ :data router
div
{} $ :class-name (str-spaced css/preset css/global css/fullscreen css/column)
comp-navigation (:logged-in? store) (:count store)
memof1-call comp-navigation (:logged-in? store) (:count store)
if (:logged-in? store)
case-default (:name router) (<> router)
:home $ div
{} (:class-name css/expand)
:style $ {} (:padding "\"8px")
input $ {} (:class-name css/input)
:value $ :demo state
=< 8 nil
<> "\"demo page"
pre $ {}
:style $ {} (:line-height 1.4) (:padding 4)
:border $ str "\"1px solid #ddd"
:inner-text $ str "\"backend data" (format-cirru-edn store)
:home $ comp-main-ui (>> states :home)
:profile $ comp-profile (:user store) (:data router)
comp-login $ >> states :login
comp-status-color $ :color store
Expand All @@ -149,6 +139,19 @@
{}
fn (info d!) (d! :session/remove-message info)
when dev? $ comp-reel (:reel-length store) ({})
|comp-main-ui $ %{} :CodeEntry (:doc |)
:code $ quote
defn comp-main-ui (states)
let
cursor $ :cursor states
state $ :data states
div
{}
:class-name $ str-spaced css/expand css/column
:style $ {} (:padding "\"8px")
div $ {} (:class-name css/expand)
textarea $ {} (:class-name css/textarea) (:placeholder "\"prompt for task..")
:value $ :demo state
|comp-offline $ %{} :CodeEntry (:doc |)
:code $ quote
defcomp comp-offline (mark)
Expand Down Expand Up @@ -192,7 +195,7 @@
respo.util.format :refer $ hsl
respo-ui.core :as ui
respo-ui.css :as css
respo.core :refer $ defcomp <> >> div span button input pre
respo.core :refer $ defcomp <> >> div span button input pre textarea
respo.css :refer $ defstyle
respo.comp.inspect :refer $ comp-inspect
respo.comp.space :refer $ =<
Expand All @@ -204,6 +207,7 @@
app.config :refer $ dev?
app.schema :as schema
app.config :as config
memof.once :refer $ memof1-call
|app.comp.login $ %{} :FileEntry
:defs $ {}
|comp-login $ %{} :CodeEntry (:doc |)
Expand Down
20 changes: 17 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,30 @@
{
"name": "macrophylla",
"dependencies": {
"@calcit/procs": "^0.9.9"
"@calcit/procs": "^0.9.9",
"@google/genai": "^0.8.0",
"@google/generative-ai": "^0.24.0",
"chalk": "^5.4.1",
"string-width": "^7.2.0"
},
"scripts": {
"compile-page": "cr --once js",
"release-page": "vite build --base=./",
"watch-page": "cr js"
"watch-page": "cr js",
"tool": "tsc && node lib/main.mjs"
},
"bin": {
"mcpl": "./bin.mjs"
},
"devDependencies": {
"@types/node": "^22.14.0",
"bottom-tip": "^0.1.5",
"typescript": "^5.8.3",
"url-parse": "^1.5.10",
"vite": "^6.2.5"
},
"version": "0.0.1"
"version": "0.0.1",
"cirruInfo": {
"calcitVersion": "0.9.9"
}
}
103 changes: 103 additions & 0 deletions src/exec.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import { exec, execFile } from "child_process";
import path from "path";
import fs from "fs/promises";

// Define function to execute Node.js code
export const executeNodeJsCode = async (
code: string,
tempDir: string
): Promise<{
stdout: string;
stderr: string;
}> => {
// Create temp directory if it doesn't exist
try {
await fs.mkdir(tempDir, { recursive: true });
} catch (err) {
// Directory might already exist
}

// Create a temporary file with the code
const tempFilePath = path.join(tempDir, `exec_${Date.now()}.mjs`);
await fs.writeFile(tempFilePath, code);
// Execute the file as an ES module
const child = execFile("node", ["--experimental-modules", tempFilePath], {
encoding: "utf8",
maxBuffer: 10 * 1024 * 1024, // 10MB buffer to handle large outputs
});

let stdout = "";
let stderr = "";

// Stream stdout in real-time while preserving color
child.stdout?.on("data", (data) => {
process.stdout.write(data);
stdout += data;
});

// Stream stderr in real-time while preserving color
child.stderr?.on("data", (data) => {
process.stderr.write(data);
stderr += data;
});

// Wait for process to complete
const result = await new Promise<{ stdout: string; stderr: string }>(
(resolve, reject) => {
child.on("close", (code) => {
// Clean up the temporary file, asynchronously
fs.unlink(tempFilePath).catch((err) =>
console.error("Failed to delete temp file:", err)
);

if (code === 0 || code === null) {
resolve({ stdout, stderr });
} else {
reject(new Error(`Process exited with code ${code}\n${stderr}`));
}
});

child.on("error", reject);
}
);

return result;
};

export let execBash = async (
command: string
): Promise<{ stdout: string; stderr: string }> => {
// Execute the command
const child = exec(command, {
encoding: "utf8",
maxBuffer: 10 * 1024 * 1024, // 10MB buffer to handle large outputs
});

let stdout = "";
let stderr = "";

// Stream stdout in real-time while preserving colors
child.stdout?.on("data", (data) => {
process.stdout.write(data);
stdout += data;
});

// Stream stderr in real-time while preserving colors
child.stderr?.on("data", (data) => {
process.stderr.write(data);
stderr += data;
});

// Wait for process to complete
return new Promise<{ stdout: string; stderr: string }>((resolve, reject) => {
child.on("close", (code) => {
if (code === 0 || code === null) {
resolve({ stdout, stderr });
} else {
reject(new Error(`Process exited with code ${code}\n${stderr}`));
}
});

child.on("error", reject);
});
};
Loading