@lazarusrolando/crayoncli 0.1.0 → 0.2.0

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.
package/.env.example CHANGED
@@ -1,3 +1,5 @@
1
- OPENAI_API_KEY=
2
- OPENAI_BASE_URL=https://api.openai.com/v1
3
- CRAYON_MODEL=gpt-5.6
1
+ CRAYON_MODEL=google/gemma-4-E2B-it
2
+ CRAYON_HOST=127.0.0.1
3
+ CRAYON_PORT=8765
4
+ CRAYON_PYTHON=python
5
+ CRAYON_MAX_NEW_TOKENS=1024
@@ -0,0 +1,16 @@
1
+ #!/bin/sh
2
+ basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')")
3
+
4
+ case `uname` in
5
+ *CYGWIN*|*MINGW*|*MSYS*)
6
+ if command -v cygpath > /dev/null 2>&1; then
7
+ basedir=`cygpath -w "$basedir"`
8
+ fi
9
+ ;;
10
+ esac
11
+
12
+ if [ -x "$basedir/node" ]; then
13
+ exec "$basedir/node" "$basedir/node_modules/Crayon-cli-local-gemma/dist/index.js" "$@"
14
+ else
15
+ exec node "$basedir/node_modules/Crayon-cli-local-gemma/dist/index.js" "$@"
16
+ fi
@@ -0,0 +1,17 @@
1
+ @ECHO off
2
+ GOTO start
3
+ :find_dp0
4
+ SET dp0=%~dp0
5
+ EXIT /b
6
+ :start
7
+ SETLOCAL
8
+ CALL :find_dp0
9
+
10
+ IF EXIST "%dp0%\node.exe" (
11
+ SET "_prog=%dp0%\node.exe"
12
+ ) ELSE (
13
+ SET "_prog=node"
14
+ SET PATHEXT=%PATHEXT:;.JS;=;%
15
+ )
16
+
17
+ endLocal & goto #_undefined_# 2>NUL || title %COMSPEC% & "%_prog%" "%dp0%\node_modules\Crayon-cli-local-gemma\dist\index.js" %*
@@ -0,0 +1,28 @@
1
+ #!/usr/bin/env pwsh
2
+ $basedir=Split-Path $MyInvocation.MyCommand.Definition -Parent
3
+
4
+ $exe=""
5
+ if ($PSVersionTable.PSVersion -lt "6.0" -or $IsWindows) {
6
+ # Fix case when both the Windows and Linux builds of Node
7
+ # are installed in the same directory
8
+ $exe=".exe"
9
+ }
10
+ $ret=0
11
+ if (Test-Path "$basedir/node$exe") {
12
+ # Support pipeline input
13
+ if ($MyInvocation.ExpectingInput) {
14
+ $input | & "$basedir/node$exe" "$basedir/node_modules/Crayon-cli-local-gemma/dist/index.js" $args
15
+ } else {
16
+ & "$basedir/node$exe" "$basedir/node_modules/Crayon-cli-local-gemma/dist/index.js" $args
17
+ }
18
+ $ret=$LASTEXITCODE
19
+ } else {
20
+ # Support pipeline input
21
+ if ($MyInvocation.ExpectingInput) {
22
+ $input | & "node$exe" "$basedir/node_modules/Crayon-cli-local-gemma/dist/index.js" $args
23
+ } else {
24
+ & "node$exe" "$basedir/node_modules/Crayon-cli-local-gemma/dist/index.js" $args
25
+ }
26
+ $ret=$LASTEXITCODE
27
+ }
28
+ exit $ret
package/README.md CHANGED
@@ -1,21 +1,38 @@
1
- # CRAYON — Open-source Crayon-style CLI
1
+ # CRAYON v0.2.0 — Local Gemma CLI
2
2
 
3
- Node.js/TypeScript coding agent for local projects.
3
+ Open-source Crayon-style CLI using Node.js/TypeScript for the agent interface and Google's Gemma model from Hugging Face for local inference.
4
4
 
5
- ## Setup
5
+ ## Requirements
6
+ - Node.js 20+
7
+ - Python 3.11+
8
+ - Sufficient RAM/VRAM for the selected Gemma model
9
+ - Hugging Face access accepted for Gemma if required
10
+
11
+ ## Install
6
12
 
7
13
  ```powershell
8
14
  npm install
15
+ python -m pip install -r model/requirements.txt
9
16
  Copy-Item .env.example .env
10
- # edit .env and add OPENAI_API_KEY
11
17
  npm run build
12
18
  npm link
13
- crayon
14
19
  ```
15
20
 
16
- Direct task:
21
+ ## Run
22
+
17
23
  ```powershell
18
- crayon "inspect this project and fix the failing tests"
24
+ crayon
25
+ crayon "explain this project"
19
26
  ```
20
27
 
21
- The agent can read/write files, search source code, and run development commands. Destructive shell commands require approval.
28
+ The first run downloads the model through Transformers into the normal Hugging Face cache. Inference runs locally; CRAYON does not use the Hugging Face inference API.
29
+
30
+ ## Configuration
31
+
32
+ ```env
33
+ CRAYON_MODEL=google/gemma-4-E2B-it
34
+ CRAYON_HOST=127.0.0.1
35
+ CRAYON_PORT=8765
36
+ CRAYON_PYTHON=python
37
+ CRAYON_MAX_NEW_TOKENS=1024
38
+ ```
package/dist/index.js CHANGED
@@ -1,15 +1,45 @@
1
1
  #!/usr/bin/env node
2
2
  import "dotenv/config";
3
3
  import { Command } from "commander";
4
- import { interactive } from "./cli/interactive.js";
5
- import { Agent } from "./agent/agent.js";
6
- const p = new Command();
7
- p.name("crayon").description("Open-source AI coding agent CLI").argument("[prompt]", "task for the coding agent").option("-v, --version", "show version").action(async (prompt) => { if (!process.env.OPENAI_API_KEY) {
8
- console.error("Missing OPENAI_API_KEY. Copy .env.example to .env and add your key.");
9
- process.exit(1);
10
- } if (prompt) {
11
- await new Agent().run(prompt);
12
- }
13
- else
14
- await interactive(); });
15
- p.parseAsync();
4
+ import chalk from "chalk";
5
+ import { LocalGemma } from "./providers/local-gemma.js";
6
+ import { createInterface } from "node:readline/promises";
7
+ import { stdin as input, stdout as output } from "node:process";
8
+ const program = new Command();
9
+ program
10
+ .name("crayon")
11
+ .description("CRAYON — local Crayon-style coding agent")
12
+ .argument("[task]");
13
+ program.action(async (task) => {
14
+ const gemma = new LocalGemma();
15
+ process.on("SIGINT", () => {
16
+ gemma.stop();
17
+ process.exit(130);
18
+ });
19
+ console.log(chalk.cyan("\n╭──────────────────────────────────────────────╮"));
20
+ console.log(chalk.cyan("│ CRAYON LOCAL CODER │"));
21
+ console.log(chalk.cyan("│ Google Gemma · Local Inference │"));
22
+ console.log(chalk.cyan("╰──────────────────────────────────────────────╯\n"));
23
+ if (!task) {
24
+ const rl = createInterface({ input, output });
25
+ try {
26
+ while (true) {
27
+ const q = (await rl.question(chalk.green("> "))).trim();
28
+ if (!q || q === "exit" || q === "quit")
29
+ break;
30
+ console.log(chalk.dim("Thinking...\n"));
31
+ console.log(await gemma.generate(q));
32
+ console.log();
33
+ }
34
+ }
35
+ finally {
36
+ rl.close();
37
+ gemma.stop();
38
+ }
39
+ }
40
+ else {
41
+ console.log(await gemma.generate(task));
42
+ gemma.stop();
43
+ }
44
+ });
45
+ await program.parseAsync();
@@ -0,0 +1,109 @@
1
+ import { spawn } from "node:child_process";
2
+ import { setTimeout as delay } from "node:timers/promises";
3
+ export class LocalGemma {
4
+ process;
5
+ python;
6
+ host;
7
+ port;
8
+ model;
9
+ constructor(options = {}) {
10
+ this.python = options.python ?? process.env.CRAYON_PYTHON ?? "python";
11
+ this.host = options.host ?? process.env.CRAYON_HOST ?? "127.0.0.1";
12
+ this.port = options.port ?? Number(process.env.CRAYON_PORT ?? 8765);
13
+ this.model =
14
+ options.model ??
15
+ process.env.CRAYON_MODEL ??
16
+ "google/gemma-4-E2B-it";
17
+ }
18
+ url(path) {
19
+ return `http://${this.host}:${this.port}${path}`;
20
+ }
21
+ async start() {
22
+ // Check whether a server is already running.
23
+ try {
24
+ const response = await fetch(this.url("/health"));
25
+ if (response.ok) {
26
+ return;
27
+ }
28
+ }
29
+ catch {
30
+ // Server isn't running; start it below.
31
+ }
32
+ console.log(`[CRAYON] Starting local Gemma server...`);
33
+ console.log(`[CRAYON] Model: ${this.model}`);
34
+ this.process = spawn(this.python, ["model/server.py"], {
35
+ stdio: ["ignore", "pipe", "pipe"],
36
+ env: {
37
+ ...process.env,
38
+ CRAYON_MODEL: this.model,
39
+ CRAYON_HOST: this.host,
40
+ CRAYON_PORT: String(this.port),
41
+ },
42
+ });
43
+ // stdout is available because we configured it as "pipe".
44
+ this.process.stdout?.on("data", (data) => {
45
+ process.stdout.write(`\x1b[90m[Gemma] ${data.toString()}\x1b[0m`);
46
+ });
47
+ // stderr is available because we configured it as "pipe".
48
+ this.process.stderr?.on("data", (data) => {
49
+ process.stderr.write(`\x1b[90m[Gemma] ${data.toString()}\x1b[0m`);
50
+ });
51
+ this.process.on("error", (error) => {
52
+ console.error("[CRAYON] Failed to start Gemma:", error);
53
+ });
54
+ this.process.on("exit", (code, signal) => {
55
+ if (code !== 0 && code !== null) {
56
+ console.error(`[CRAYON] Gemma server exited with code ${code}`);
57
+ }
58
+ if (signal) {
59
+ console.error(`[CRAYON] Gemma server stopped by signal ${signal}`);
60
+ }
61
+ });
62
+ // Wait for FastAPI to become available.
63
+ for (let attempt = 0; attempt < 180; attempt++) {
64
+ try {
65
+ const response = await fetch(this.url("/health"));
66
+ if (response.ok) {
67
+ console.log("[CRAYON] Gemma server is ready.");
68
+ return;
69
+ }
70
+ }
71
+ catch {
72
+ // Still starting.
73
+ }
74
+ await delay(1000);
75
+ }
76
+ this.stop();
77
+ throw new Error("Gemma server did not become ready within 180 seconds.");
78
+ }
79
+ async generate(prompt, maxNewTokens = Number(process.env.CRAYON_MAX_NEW_TOKENS ?? 1024)) {
80
+ await this.start();
81
+ const response = await fetch(this.url("/generate"), {
82
+ method: "POST",
83
+ headers: {
84
+ "content-type": "application/json",
85
+ },
86
+ body: JSON.stringify({
87
+ prompt,
88
+ max_new_tokens: maxNewTokens,
89
+ temperature: 0.2,
90
+ top_p: 0.9,
91
+ }),
92
+ });
93
+ if (!response.ok) {
94
+ const errorText = await response.text();
95
+ throw new Error(`Gemma server returned HTTP ${response.status}: ${errorText}`);
96
+ }
97
+ const result = (await response.json());
98
+ return result.text;
99
+ }
100
+ stop() {
101
+ if (!this.process) {
102
+ return;
103
+ }
104
+ if (!this.process.killed) {
105
+ this.process.kill();
106
+ }
107
+ this.process = undefined;
108
+ }
109
+ }
@@ -0,0 +1,6 @@
1
+ torch>=2.6
2
+ transformers>=4.57
3
+ accelerate>=1.10
4
+ safetensors>=0.5
5
+ fastapi>=0.115
6
+ uvicorn[standard]>=0.34
@@ -0,0 +1,159 @@
1
+ import os
2
+ import torch
3
+ from fastapi import FastAPI, HTTPException
4
+ from pydantic import BaseModel
5
+ from transformers import AutoTokenizer, AutoModelForCausalLM
6
+
7
+ MODEL_ID = os.getenv("CRAYON_MODEL", "google/gemma-4-E2B-it")
8
+ HOST = os.getenv("CRAYON_HOST", "127.0.0.1")
9
+ PORT = int(os.getenv("CRAYON_PORT", "8765"))
10
+
11
+ print("=" * 60)
12
+ print("[CRAYON] Local Gemma Server")
13
+ print("=" * 60)
14
+ print(f"[CRAYON] Model: {MODEL_ID}")
15
+ print(f"[CRAYON] PyTorch: {torch.__version__}")
16
+ print(f"[CRAYON] CUDA available: {torch.cuda.is_available()}")
17
+
18
+ if torch.cuda.is_available():
19
+ print(f"[CRAYON] GPU: {torch.cuda.get_device_name(0)}")
20
+ print(f"[CRAYON] CUDA: {torch.version.cuda}")
21
+
22
+ print("[CRAYON] Loading tokenizer...")
23
+
24
+ tokenizer = AutoTokenizer.from_pretrained(
25
+ MODEL_ID
26
+ )
27
+
28
+ print("[CRAYON] Tokenizer loaded.")
29
+ print("[CRAYON] Loading model...")
30
+
31
+ model = AutoModelForCausalLM.from_pretrained(
32
+ MODEL_ID,
33
+ device_map="auto",
34
+ torch_dtype="auto",
35
+ )
36
+
37
+ model.eval()
38
+
39
+ print("[CRAYON] Model loaded successfully.")
40
+
41
+ app = FastAPI(
42
+ title="CRAYON Local Gemma Server",
43
+ version="0.2.1",
44
+ )
45
+
46
+
47
+ class GenerateRequest(BaseModel):
48
+ prompt: str
49
+ max_new_tokens: int = 1024
50
+ temperature: float = 0.2
51
+ top_p: float = 0.9
52
+
53
+
54
+ @app.get("/health")
55
+ def health():
56
+ return {
57
+ "ok": True,
58
+ "model": MODEL_ID,
59
+ "cuda": torch.cuda.is_available(),
60
+ "device": str(model.device),
61
+ }
62
+
63
+
64
+ @app.post("/generate")
65
+ def generate(req: GenerateRequest):
66
+
67
+ try:
68
+ print(f"[CRAYON] Request: {req.prompt[:100]}")
69
+
70
+ messages = [
71
+ {
72
+ "role": "user",
73
+ "content": req.prompt,
74
+ }
75
+ ]
76
+
77
+ # Create the model input using Gemma's chat template.
78
+ inputs = tokenizer.apply_chat_template(
79
+ messages,
80
+ add_generation_prompt=True,
81
+ tokenize=True,
82
+ return_tensors="pt",
83
+ return_dict=True,
84
+ )
85
+
86
+ # BatchEncoding -> tensors
87
+ input_ids = inputs["input_ids"]
88
+ attention_mask = inputs.get("attention_mask")
89
+
90
+ # Move tensors to the model's device.
91
+ input_ids = input_ids.to(model.device)
92
+
93
+ if attention_mask is not None:
94
+ attention_mask = attention_mask.to(model.device)
95
+
96
+ print(
97
+ f"[CRAYON] Input tokens: {input_ids.shape[-1]}"
98
+ )
99
+
100
+ generation_kwargs = {
101
+ "input_ids": input_ids,
102
+ "max_new_tokens": req.max_new_tokens,
103
+ "do_sample": req.temperature > 0,
104
+ "temperature": max(req.temperature, 1e-5),
105
+ "top_p": req.top_p,
106
+ "pad_token_id": tokenizer.eos_token_id,
107
+ }
108
+
109
+ if attention_mask is not None:
110
+ generation_kwargs["attention_mask"] = attention_mask
111
+
112
+ print("[CRAYON] Generating...")
113
+
114
+ with torch.inference_mode():
115
+
116
+ output = model.generate(
117
+ **generation_kwargs
118
+ )
119
+
120
+ # Remove the original prompt tokens.
121
+ generated_tokens = output[
122
+ 0,
123
+ input_ids.shape[-1]:
124
+ ]
125
+
126
+ text = tokenizer.decode(
127
+ generated_tokens,
128
+ skip_special_tokens=True,
129
+ )
130
+
131
+ print(
132
+ f"[CRAYON] Generated {len(generated_tokens)} tokens."
133
+ )
134
+
135
+ return {
136
+ "text": text,
137
+ "model": MODEL_ID,
138
+ }
139
+
140
+ except Exception as exc:
141
+
142
+ print("[CRAYON] Generation error:")
143
+ print(repr(exc))
144
+
145
+ raise HTTPException(
146
+ status_code=500,
147
+ detail=str(exc),
148
+ )
149
+
150
+
151
+ if __name__ == "__main__":
152
+
153
+ import uvicorn
154
+
155
+ uvicorn.run(
156
+ app,
157
+ host=HOST,
158
+ port=PORT,
159
+ )
package/package.json CHANGED
@@ -1,29 +1,21 @@
1
1
  {
2
2
  "name": "@lazarusrolando/crayoncli",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "type": "module",
5
5
  "bin": {
6
- "crayon": "./dist/index.js"
6
+ "crayon": "dist/index.js"
7
7
  },
8
8
  "scripts": {
9
- "dev": "tsx src/index.ts",
10
9
  "build": "tsc",
11
10
  "start": "node dist/index.js"
12
11
  },
13
12
  "dependencies": {
14
13
  "chalk": "^5.4.1",
15
- "commander": "^14.0.0",
16
- "dotenv": "^17.2.2",
17
- "fast-glob": "^3.3.3",
18
- "inquirer": "^12.9.4",
19
- "openai": "^6.0.0",
20
- "ora": "^8.2.0",
21
- "pino": "^9.9.0",
22
- "uuid": "^11.1.0"
14
+ "commander": "^14.0.1",
15
+ "dotenv": "^17.2.1"
23
16
  },
24
17
  "devDependencies": {
25
- "@types/node": "^24.5.2",
26
- "tsx": "^4.19.2",
18
+ "@types/node": "^24.0.0",
27
19
  "typescript": "^5.9.2"
28
20
  }
29
21
  }
package/src/index.ts CHANGED
@@ -1,3 +1,52 @@
1
1
  #!/usr/bin/env node
2
- import "dotenv/config"; import {Command} from "commander"; import {interactive} from "./cli/interactive.js"; import {Agent} from "./agent/agent.js";
3
- const p=new Command();p.name("crayon").description("Open-source AI coding agent CLI").argument("[prompt]","task for the coding agent").option("-v, --version","show version").action(async(prompt:string|undefined)=>{if(!process.env.OPENAI_API_KEY){console.error("Missing OPENAI_API_KEY. Copy .env.example to .env and add your key.");process.exit(1);}if(prompt){await new Agent().run(prompt);}else await interactive();});p.parseAsync();
2
+ import "dotenv/config";
3
+ import { Command } from "commander";
4
+ import chalk from "chalk";
5
+ import { LocalGemma } from "./providers/local-gemma.js";
6
+ import { createInterface } from "node:readline/promises";
7
+ import { stdin as input, stdout as output } from "node:process";
8
+
9
+ const program = new Command();
10
+
11
+ program
12
+ .name("crayon")
13
+ .description("CRAYON — local Crayon-style coding agent")
14
+ .argument("[task]");
15
+
16
+ program.action(async (task?: string) => {
17
+ const gemma = new LocalGemma();
18
+
19
+ process.on("SIGINT", () => {
20
+ gemma.stop();
21
+ process.exit(130);
22
+ });
23
+
24
+ console.log(chalk.cyan("\n╭──────────────────────────────────────────────╮"));
25
+ console.log(chalk.cyan("│ CRAYON LOCAL CODER │"));
26
+ console.log(chalk.cyan("│ Google Gemma · Local Inference │"));
27
+ console.log(chalk.cyan("╰──────────────────────────────────────────────╯\n"));
28
+
29
+ if (!task) {
30
+ const rl = createInterface({input, output});
31
+
32
+ try {
33
+ while (true) {
34
+ const q = (await rl.question(chalk.green("> "))).trim();
35
+
36
+ if (!q || q === "exit" || q === "quit") break;
37
+
38
+ console.log(chalk.dim("Thinking...\n"));
39
+ console.log(await gemma.generate(q));
40
+ console.log();
41
+ }
42
+ } finally {
43
+ rl.close();
44
+ gemma.stop();
45
+ }
46
+ } else {
47
+ console.log(await gemma.generate(task));
48
+ gemma.stop();
49
+ }
50
+ });
51
+
52
+ await program.parseAsync();
@@ -0,0 +1,165 @@
1
+ import { spawn, type ChildProcess } from "node:child_process";
2
+ import { setTimeout as delay } from "node:timers/promises";
3
+
4
+ export interface GemmaOptions {
5
+ python?: string;
6
+ host?: string;
7
+ port?: number;
8
+ model?: string;
9
+ }
10
+
11
+ export class LocalGemma {
12
+ private process?: ChildProcess;
13
+
14
+ private readonly python: string;
15
+ private readonly host: string;
16
+ private readonly port: number;
17
+ private readonly model: string;
18
+
19
+ constructor(options: GemmaOptions = {}) {
20
+ this.python = options.python ?? process.env.CRAYON_PYTHON ?? "python";
21
+ this.host = options.host ?? process.env.CRAYON_HOST ?? "127.0.0.1";
22
+ this.port = options.port ?? Number(process.env.CRAYON_PORT ?? 8765);
23
+ this.model =
24
+ options.model ??
25
+ process.env.CRAYON_MODEL ??
26
+ "google/gemma-4-E2B-it";
27
+ }
28
+
29
+ private url(path: string): string {
30
+ return `http://${this.host}:${this.port}${path}`;
31
+ }
32
+
33
+ async start(): Promise<void> {
34
+ // Check whether a server is already running.
35
+ try {
36
+ const response = await fetch(this.url("/health"));
37
+
38
+ if (response.ok) {
39
+ return;
40
+ }
41
+ } catch {
42
+ // Server isn't running; start it below.
43
+ }
44
+
45
+ console.log(`[CRAYON] Starting local Gemma server...`);
46
+ console.log(`[CRAYON] Model: ${this.model}`);
47
+
48
+ this.process = spawn(
49
+ this.python,
50
+ ["model/server.py"],
51
+ {
52
+ stdio: ["ignore", "pipe", "pipe"],
53
+ env: {
54
+ ...process.env,
55
+ CRAYON_MODEL: this.model,
56
+ CRAYON_HOST: this.host,
57
+ CRAYON_PORT: String(this.port),
58
+ },
59
+ },
60
+ );
61
+
62
+ // stdout is available because we configured it as "pipe".
63
+ this.process.stdout?.on("data", (data: Buffer) => {
64
+ process.stdout.write(
65
+ `\x1b[90m[Gemma] ${data.toString()}\x1b[0m`,
66
+ );
67
+ });
68
+
69
+ // stderr is available because we configured it as "pipe".
70
+ this.process.stderr?.on("data", (data: Buffer) => {
71
+ process.stderr.write(
72
+ `\x1b[90m[Gemma] ${data.toString()}\x1b[0m`,
73
+ );
74
+ });
75
+
76
+ this.process.on("error", (error) => {
77
+ console.error("[CRAYON] Failed to start Gemma:", error);
78
+ });
79
+
80
+ this.process.on("exit", (code, signal) => {
81
+ if (code !== 0 && code !== null) {
82
+ console.error(
83
+ `[CRAYON] Gemma server exited with code ${code}`,
84
+ );
85
+ }
86
+
87
+ if (signal) {
88
+ console.error(
89
+ `[CRAYON] Gemma server stopped by signal ${signal}`,
90
+ );
91
+ }
92
+ });
93
+
94
+ // Wait for FastAPI to become available.
95
+ for (let attempt = 0; attempt < 180; attempt++) {
96
+ try {
97
+ const response = await fetch(this.url("/health"));
98
+
99
+ if (response.ok) {
100
+ console.log("[CRAYON] Gemma server is ready.");
101
+ return;
102
+ }
103
+ } catch {
104
+ // Still starting.
105
+ }
106
+
107
+ await delay(1000);
108
+ }
109
+
110
+ this.stop();
111
+
112
+ throw new Error(
113
+ "Gemma server did not become ready within 180 seconds.",
114
+ );
115
+ }
116
+
117
+ async generate(
118
+ prompt: string,
119
+ maxNewTokens = Number(
120
+ process.env.CRAYON_MAX_NEW_TOKENS ?? 1024,
121
+ ),
122
+ ): Promise<string> {
123
+ await this.start();
124
+
125
+ const response = await fetch(this.url("/generate"), {
126
+ method: "POST",
127
+ headers: {
128
+ "content-type": "application/json",
129
+ },
130
+ body: JSON.stringify({
131
+ prompt,
132
+ max_new_tokens: maxNewTokens,
133
+ temperature: 0.2,
134
+ top_p: 0.9,
135
+ }),
136
+ });
137
+
138
+ if (!response.ok) {
139
+ const errorText = await response.text();
140
+
141
+ throw new Error(
142
+ `Gemma server returned HTTP ${response.status}: ${errorText}`,
143
+ );
144
+ }
145
+
146
+ const result = (await response.json()) as {
147
+ text: string;
148
+ model?: string;
149
+ };
150
+
151
+ return result.text;
152
+ }
153
+
154
+ stop(): void {
155
+ if (!this.process) {
156
+ return;
157
+ }
158
+
159
+ if (!this.process.killed) {
160
+ this.process.kill();
161
+ }
162
+
163
+ this.process = undefined;
164
+ }
165
+ }
package/tsconfig.json CHANGED
@@ -1 +1,15 @@
1
- {"compilerOptions":{"target":"ES2022","module":"NodeNext","moduleResolution":"NodeNext","strict":true,"esModuleInterop":true,"skipLibCheck":true,"outDir":"dist","rootDir":"src"},"include":["src/**/*.ts"]}
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2022",
4
+ "module": "NodeNext",
5
+ "moduleResolution": "NodeNext",
6
+ "outDir": "dist",
7
+ "rootDir": "src",
8
+ "strict": true,
9
+ "esModuleInterop": true,
10
+ "skipLibCheck": true
11
+ },
12
+ "include": [
13
+ "src"
14
+ ]
15
+ }
@@ -1,27 +0,0 @@
1
- import { OpenAIProvider } from "../providers/openai.js";
2
- import { definitions, execute } from "../tools/registry.js";
3
- const SYSTEM = `You are CRAYON, an autonomous coding agent. Work inside the current project. Inspect before editing. Prefer small, safe changes. Explain what you are doing. Use tools to read/search/edit/run tests. Never claim a change was made unless the tool succeeded. Do not access files outside the project root.`;
4
- export class Agent {
5
- provider = new OpenAIProvider();
6
- messages = [{ role: "system", content: SYSTEM }];
7
- async run(input) { this.messages.push({ role: "user", content: input }); for (let i = 0; i < 30; i++) {
8
- const r = await this.provider.chat(this.messages, definitions);
9
- if (r.content)
10
- console.log(`\n${r.content}`);
11
- if (!r.toolCalls.length)
12
- return;
13
- if (r.content)
14
- this.messages.push({ role: "assistant", content: r.content });
15
- for (const c of r.toolCalls) {
16
- let result = "";
17
- try {
18
- result = await execute(c.name, JSON.parse(c.arguments));
19
- }
20
- catch (e) {
21
- result = `ERROR: ${e instanceof Error ? e.message : String(e)}`;
22
- }
23
- this.messages.push({ role: "tool", tool_call_id: c.id, content: String(result) });
24
- console.log(`\n● ${c.name}`);
25
- }
26
- } throw new Error("Agent reached maximum tool iterations."); }
27
- }
@@ -1,16 +0,0 @@
1
- import readline from "node:readline/promises";
2
- import { stdin as input, stdout as output } from "node:process";
3
- import { Agent } from "../agent/agent.js";
4
- export async function interactive() { console.log("\n╭──────────────────────────────────────────────╮\n│ CRAYON CODING AGENT │\n│ Open-source Crayon-style CLI │\n╰──────────────────────────────────────────────╯\n"); const rl = readline.createInterface({ input, output }); const agent = new Agent(); while (true) {
5
- const q = (await rl.question("\n> ")).trim();
6
- if (!q)
7
- continue;
8
- if (["exit", "quit"].includes(q.toLowerCase()))
9
- break;
10
- try {
11
- await agent.run(q);
12
- }
13
- catch (e) {
14
- console.error(`\n✖ ${e instanceof Error ? e.message : e}`);
15
- }
16
- } rl.close(); }
@@ -1,11 +0,0 @@
1
- import OpenAI from "openai";
2
- export class OpenAIProvider {
3
- client;
4
- model;
5
- constructor() { this.client = new OpenAI({ apiKey: process.env.OPENAI_API_KEY, baseURL: process.env.OPENAI_BASE_URL }); this.model = process.env.CRAYON_MODEL || "gpt-5.6"; }
6
- async chat(messages, tools) {
7
- const r = await this.client.chat.completions.create({ model: this.model, messages: messages, tools: tools, tool_choice: "auto" });
8
- const m = r.choices[0]?.message;
9
- return { content: m?.content ?? null, toolCalls: (m?.tool_calls ?? []).map((x) => ({ id: x.id, name: x.function.name, arguments: x.function.arguments })) };
10
- }
11
- }
@@ -1 +0,0 @@
1
- export {};
@@ -1,29 +0,0 @@
1
- import fs from "node:fs/promises";
2
- import path from "node:path";
3
- const root = process.cwd();
4
- function safe(p) { const r = path.resolve(root, p); if (r !== root && !r.startsWith(root + path.sep))
5
- throw new Error("Path escapes project root"); return r; }
6
- export const fsTools = [
7
- { name: "read_file", description: "Read a UTF-8 text file in the project.", parameters: { type: "object", properties: { path: { type: "string" } }, required: ["path"] } },
8
- { name: "write_file", description: "Write or replace a UTF-8 text file in the project.", parameters: { type: "object", properties: { path: { type: "string" }, content: { type: "string" } }, required: ["path", "content"] } },
9
- { name: "list_files", description: "List project files recursively, excluding common dependency/build folders.", parameters: { type: "object", properties: { path: { type: "string" } }, required: [] } },
10
- ];
11
- export async function runFs(name, args) { if (name === "read_file")
12
- return await fs.readFile(safe(args.path), "utf8"); if (name === "write_file") {
13
- await fs.mkdir(path.dirname(safe(args.path)), { recursive: true });
14
- await fs.writeFile(safe(args.path), args.content, "utf8");
15
- return `Wrote ${args.path}`;
16
- } if (name === "list_files") {
17
- const out = [];
18
- async function walk(d) { for (const e of await fs.readdir(d, { withFileTypes: true })) {
19
- if (["node_modules", ".git", "dist", "build", ".next"].includes(e.name))
20
- continue;
21
- const p = path.join(d, e.name);
22
- if (e.isDirectory())
23
- await walk(p);
24
- else
25
- out.push(path.relative(root, p));
26
- } }
27
- await walk(safe(args.path || "."));
28
- return out.slice(0, 500).join("\n");
29
- } throw new Error("Unknown filesystem tool"); }
@@ -1,8 +0,0 @@
1
- import { fsTools, runFs } from "./filesystem.js";
2
- import { searchTool, searchCode } from "./search.js";
3
- import { terminalTool, runCommand } from "./terminal.js";
4
- export const definitions = [...fsTools.map(x => ({ type: "function", function: x })), { type: "function", function: searchTool }, { type: "function", function: terminalTool }];
5
- export async function execute(name, args) { if (["read_file", "write_file", "list_files"].includes(name))
6
- return runFs(name, args); if (name === "search_code")
7
- return searchCode(args.query); if (name === "run_command")
8
- return runCommand(args.command); throw new Error(`Unknown tool: ${name}`); }
@@ -1,18 +0,0 @@
1
- import fs from "node:fs/promises";
2
- import path from "node:path";
3
- export const searchTool = { name: "search_code", description: "Search text across project source files.", parameters: { type: "object", properties: { query: { type: "string" } }, required: ["query"] } };
4
- export async function searchCode(query) { const root = process.cwd(), hits = []; async function walk(d) { for (const e of await fs.readdir(d, { withFileTypes: true })) {
5
- if (["node_modules", ".git", "dist", "build", ".next"].includes(e.name))
6
- continue;
7
- const p = path.join(d, e.name);
8
- if (e.isDirectory())
9
- await walk(p);
10
- else {
11
- try {
12
- const s = await fs.readFile(p, "utf8");
13
- if (s.includes(query))
14
- hits.push(path.relative(root, p));
15
- }
16
- catch { }
17
- }
18
- } } await walk(root); return hits.slice(0, 100).join("\n") || "No matches."; }
@@ -1,13 +0,0 @@
1
- import { exec } from "node:child_process";
2
- import { promisify } from "node:util";
3
- import readline from "node:readline/promises";
4
- const ex = promisify(exec);
5
- export const terminalTool = { name: "run_command", description: "Run a shell command in the project. Use only for safe development commands.", parameters: { type: "object", properties: { command: { type: "string" } }, required: ["command"] } };
6
- const dangerous = /\b(del|erase|rmdir|rm|format|shutdown|reboot|diskpart|git\s+reset\s+--hard)\b/i;
7
- export async function runCommand(command) { if (dangerous.test(command)) {
8
- const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
9
- const a = await rl.question(`Approve potentially destructive command? ${command} [y/N] `);
10
- rl.close();
11
- if (a.toLowerCase() !== "y")
12
- return "Command denied by user.";
13
- } const r = await ex(command, { cwd: process.cwd(), timeout: 120000, maxBuffer: 5 * 1024 * 1024, shell: process.platform === "win32" ? "powershell.exe" : true }); return (r.stdout + r.stderr).trim(); }
@@ -1,3 +0,0 @@
1
- import type {Message} from "../providers/provider.js"; import {OpenAIProvider} from "../providers/openai.js"; import {definitions,execute} from "../tools/registry.js";
2
- const SYSTEM=`You are CRAYON, an autonomous coding agent. Work inside the current project. Inspect before editing. Prefer small, safe changes. Explain what you are doing. Use tools to read/search/edit/run tests. Never claim a change was made unless the tool succeeded. Do not access files outside the project root.`;
3
- export class Agent { private provider=new OpenAIProvider(); private messages:Message[]=[{role:"system",content:SYSTEM}]; async run(input:string){this.messages.push({role:"user",content:input}); for(let i=0;i<30;i++){const r=await this.provider.chat(this.messages,definitions); if(r.content) console.log(`\n${r.content}`); if(!r.toolCalls.length) return; if(r.content)this.messages.push({role:"assistant",content:r.content}); for(const c of r.toolCalls){let result="";try{result=await execute(c.name,JSON.parse(c.arguments));}catch(e){result=`ERROR: ${e instanceof Error?e.message:String(e)}`;} this.messages.push({role:"tool",tool_call_id:c.id,content:String(result)}); console.log(`\n● ${c.name}`);}} throw new Error("Agent reached maximum tool iterations.");}}
@@ -1,2 +0,0 @@
1
- import readline from "node:readline/promises"; import {stdin as input,stdout as output} from "node:process"; import {Agent} from "../agent/agent.js";
2
- export async function interactive(){console.log("\n╭──────────────────────────────────────────────╮\n│ CRAYON CODING AGENT │\n│ Open-source Crayon-style CLI │\n╰──────────────────────────────────────────────╯\n"); const rl=readline.createInterface({input,output}); const agent=new Agent(); while(true){const q=(await rl.question("\n> ")).trim();if(!q)continue;if(["exit","quit"].includes(q.toLowerCase()))break;try{await agent.run(q);}catch(e){console.error(`\n✖ ${e instanceof Error?e.message:e}`);}}rl.close();}
@@ -1,10 +0,0 @@
1
- import OpenAI from "openai";
2
- import type { ModelProvider, Message, ToolDef, ChatResult } from "./provider.js";
3
- export class OpenAIProvider implements ModelProvider {
4
- private client: OpenAI; private model: string;
5
- constructor() { this.client = new OpenAI({ apiKey: process.env.OPENAI_API_KEY, baseURL: process.env.OPENAI_BASE_URL }); this.model = process.env.CRAYON_MODEL || "gpt-5.6"; }
6
- async chat(messages: Message[], tools: ToolDef[]): Promise<ChatResult> {
7
- const r = await this.client.chat.completions.create({ model: this.model, messages: messages as any, tools: tools as any, tool_choice: "auto" });
8
- const m = r.choices[0]?.message; return { content: m?.content ?? null, toolCalls: (m?.tool_calls ?? []).map((x: any) => ({ id:x.id, name:x.function.name, arguments:x.function.arguments })) };
9
- }
10
- }
@@ -1,4 +0,0 @@
1
- export type Message = { role: "system" | "user" | "assistant" | "tool"; content: string; tool_call_id?: string };
2
- export type ToolDef = { type: "function"; function: { name: string; description: string; parameters: Record<string, unknown> } };
3
- export type ChatResult = { content: string | null; toolCalls: { id: string; name: string; arguments: string }[] };
4
- export interface ModelProvider { chat(messages: Message[], tools: ToolDef[]): Promise<ChatResult>; }
@@ -1,9 +0,0 @@
1
- import fs from "node:fs/promises"; import path from "node:path";
2
- const root = process.cwd();
3
- function safe(p:string){ const r=path.resolve(root,p); if(r!==root && !r.startsWith(root+path.sep)) throw new Error("Path escapes project root"); return r; }
4
- export const fsTools = [
5
- {name:"read_file",description:"Read a UTF-8 text file in the project.",parameters:{type:"object",properties:{path:{type:"string"}},required:["path"]}},
6
- {name:"write_file",description:"Write or replace a UTF-8 text file in the project.",parameters:{type:"object",properties:{path:{type:"string"},content:{type:"string"}},required:["path","content"]}},
7
- {name:"list_files",description:"List project files recursively, excluding common dependency/build folders.",parameters:{type:"object",properties:{path:{type:"string"}},required:[]}},
8
- ];
9
- export async function runFs(name:string,args:any){ if(name==="read_file") return await fs.readFile(safe(args.path),"utf8"); if(name==="write_file"){await fs.mkdir(path.dirname(safe(args.path)),{recursive:true});await fs.writeFile(safe(args.path),args.content,"utf8");return `Wrote ${args.path}`;} if(name==="list_files"){const out:string[]=[]; async function walk(d:string){for(const e of await fs.readdir(d,{withFileTypes:true})){if(["node_modules",".git","dist","build",".next"].includes(e.name))continue;const p=path.join(d,e.name); if(e.isDirectory()) await walk(p); else out.push(path.relative(root,p));}} await walk(safe(args.path||".")); return out.slice(0,500).join("\n");} throw new Error("Unknown filesystem tool"); }
@@ -1,3 +0,0 @@
1
- import {fsTools,runFs} from "./filesystem.js"; import {searchTool,searchCode} from "./search.js"; import {terminalTool,runCommand} from "./terminal.js"; import type {ToolDef} from "../providers/provider.js";
2
- export const definitions:ToolDef[]=[...fsTools.map(x=>({type:"function" as const,function:x})),{type:"function",function:searchTool},{type:"function",function:terminalTool}];
3
- export async function execute(name:string,args:any){if(["read_file","write_file","list_files"].includes(name))return runFs(name,args);if(name==="search_code")return searchCode(args.query);if(name==="run_command")return runCommand(args.command);throw new Error(`Unknown tool: ${name}`);}
@@ -1,3 +0,0 @@
1
- import fs from "node:fs/promises"; import path from "node:path";
2
- export const searchTool={name:"search_code",description:"Search text across project source files.",parameters:{type:"object",properties:{query:{type:"string"}},required:["query"]}};
3
- export async function searchCode(query:string){const root=process.cwd(), hits:string[]=[]; async function walk(d:string){for(const e of await fs.readdir(d,{withFileTypes:true})){if(["node_modules",".git","dist","build",".next"].includes(e.name))continue;const p=path.join(d,e.name);if(e.isDirectory()) await walk(p); else {try{const s=await fs.readFile(p,"utf8");if(s.includes(query)) hits.push(path.relative(root,p));}catch{}}}} await walk(root); return hits.slice(0,100).join("\n")||"No matches.";}
@@ -1,4 +0,0 @@
1
- import {exec} from "node:child_process"; import {promisify} from "node:util"; import readline from "node:readline/promises";
2
- const ex=promisify(exec); export const terminalTool={name:"run_command",description:"Run a shell command in the project. Use only for safe development commands.",parameters:{type:"object",properties:{command:{type:"string"}},required:["command"]}};
3
- const dangerous=/\b(del|erase|rmdir|rm|format|shutdown|reboot|diskpart|git\s+reset\s+--hard)\b/i;
4
- export async function runCommand(command:string){if(dangerous.test(command)){const rl=readline.createInterface({input:process.stdin,output:process.stdout});const a=await rl.question(`Approve potentially destructive command? ${command} [y/N] `);rl.close();if(a.toLowerCase()!=="y")return "Command denied by user.";} const r=await ex(command,{cwd:process.cwd(),timeout:120000,maxBuffer:5*1024*1024,shell:process.platform==="win32"?"powershell.exe":true});return (r.stdout+r.stderr).trim();}