@lazarusrolando/crayoncli 0.1.0 → 0.3.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,6 @@
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
6
+ CRAYON_TEMPERATURE=0.7
package/README.md CHANGED
@@ -1,21 +1,40 @@
1
- # CRAYON — Open-source Crayon-style CLI
1
+ # CRAYON v0.3.0 — Local Gemma Streaming
2
2
 
3
- Node.js/TypeScript coding agent for local projects.
3
+ Real local token streaming for the CRAYON CLI.
4
4
 
5
- ## Setup
5
+ ## Flow
6
+
7
+ You → Node.js CLI → local FastAPI → Transformers TextIteratorStreamer → local Gemma → tokens → CLI
8
+
9
+ Hugging Face is only the model repository. Inference is local.
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
- npm link
13
- crayon
14
18
  ```
15
19
 
16
- Direct task:
20
+ ## Run
21
+
22
+ ```powershell
23
+ node dist/index.js
24
+ ```
25
+
26
+ or:
27
+
17
28
  ```powershell
18
- crayon "inspect this project and fix the failing tests"
29
+ node dist/index.js "Explain recursion in JavaScript"
19
30
  ```
20
31
 
21
- The agent can read/write files, search source code, and run development commands. Destructive shell commands require approval.
32
+ ## What v0.3 adds
33
+
34
+ - `TextIteratorStreamer` for local token streaming
35
+ - background generation thread
36
+ - FastAPI `StreamingResponse`
37
+ - Node.js `ReadableStream` consumption
38
+ - keeps `/generate` for non-streaming compatibility
39
+
40
+ Streaming changes delivery timing, not the inference provider. The model still runs locally.
package/dist/index.js CHANGED
@@ -1,15 +1,54 @@
1
- #!/usr/bin/env node
2
1
  import "dotenv/config";
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);
2
+ import readline from "node:readline";
3
+ import { LocalGemma } from "./providers/local-gemma.js";
4
+ const gemma = new LocalGemma({
5
+ python: process.env.CRAYON_PYTHON || "python",
6
+ model: process.env.CRAYON_MODEL || "google/gemma-4-E2B-it",
7
+ host: process.env.CRAYON_HOST || "127.0.0.1",
8
+ port: Number(process.env.CRAYON_PORT || 8765),
9
+ maxNewTokens: Number(process.env.CRAYON_MAX_NEW_TOKENS || 1024),
10
+ temperature: Number(process.env.CRAYON_TEMPERATURE || 0.7)
11
+ });
12
+ async function main() {
13
+ const oneShot = process.argv.slice(2).join(" ").trim();
14
+ await gemma.start();
15
+ if (oneShot) {
16
+ await gemma.generateStream(oneShot);
17
+ gemma.stop();
18
+ return;
19
+ }
20
+ console.log("\n==========================================");
21
+ console.log(" CRAYON v0.3.0 - LOCAL STREAMING");
22
+ console.log("==========================================");
23
+ console.log("Gemma inference: LOCAL");
24
+ console.log("Hugging Face inference API: NOT USED");
25
+ console.log("Streaming: ENABLED\n");
26
+ console.log("Type /exit to quit.\n");
27
+ const rl = readline.createInterface({ input: process.stdin, output: process.stdout, prompt: "You: " });
28
+ const shutdown = () => { rl.close(); gemma.stop(); process.exit(0); };
29
+ process.on("SIGINT", shutdown);
30
+ rl.prompt();
31
+ rl.on("line", async (line) => {
32
+ const prompt = line.trim();
33
+ if (!prompt) {
34
+ rl.prompt();
35
+ return;
36
+ }
37
+ if (prompt === "/exit") {
38
+ shutdown();
39
+ return;
40
+ }
41
+ rl.pause();
42
+ try {
43
+ await gemma.generateStream(prompt);
44
+ }
45
+ catch (error) {
46
+ console.error("\n[CRAYON ERROR]", error instanceof Error ? error.message : error);
47
+ }
48
+ finally {
49
+ rl.resume();
50
+ rl.prompt();
51
+ }
52
+ });
12
53
  }
13
- else
14
- await interactive(); });
15
- p.parseAsync();
54
+ main().catch(error => { console.error("[CRAYON FATAL]", error); gemma.stop(); process.exit(1); });
@@ -0,0 +1,79 @@
1
+ import { spawn } from "node:child_process";
2
+ export class LocalGemma {
3
+ process;
4
+ options;
5
+ baseUrl;
6
+ constructor(options) {
7
+ this.options = options;
8
+ this.baseUrl = `http://${options.host}:${options.port}`;
9
+ }
10
+ async start() {
11
+ if (await this.isReady())
12
+ return;
13
+ console.log("[CRAYON] Starting local Gemma server...");
14
+ console.log(`[CRAYON] Model: ${this.options.model}`);
15
+ this.process = spawn(this.options.python, ["model/server.py"], {
16
+ stdio: ["ignore", "pipe", "pipe"],
17
+ env: { ...process.env, CRAYON_MODEL: this.options.model, CRAYON_HOST: this.options.host,
18
+ CRAYON_PORT: String(this.options.port) }
19
+ });
20
+ this.process.stdout?.on("data", (d) => process.stdout.write(`[Gemma] ${d.toString()}`));
21
+ this.process.stderr?.on("data", (d) => process.stderr.write(`[Gemma] ${d.toString()}`));
22
+ const deadline = Date.now() + 180000;
23
+ while (Date.now() < deadline) {
24
+ if (await this.isReady()) {
25
+ console.log("[CRAYON] Local Gemma server is ready.");
26
+ return;
27
+ }
28
+ await new Promise(r => setTimeout(r, 1000));
29
+ }
30
+ throw new Error("Gemma server did not become ready within 180 seconds.");
31
+ }
32
+ async isReady() {
33
+ try {
34
+ return (await fetch(`${this.baseUrl}/health`)).ok;
35
+ }
36
+ catch {
37
+ return false;
38
+ }
39
+ }
40
+ async generateStream(prompt) {
41
+ const response = await fetch(`${this.baseUrl}/generate/stream`, {
42
+ method: "POST", headers: { "Content-Type": "application/json" },
43
+ body: JSON.stringify({ prompt, max_new_tokens: this.options.maxNewTokens, temperature: this.options.temperature })
44
+ });
45
+ if (!response.ok)
46
+ throw new Error(`Gemma streaming error (${response.status}): ${await response.text()}`);
47
+ if (!response.body)
48
+ throw new Error("Gemma returned no streaming body.");
49
+ const reader = response.body.getReader();
50
+ const decoder = new TextDecoder();
51
+ process.stdout.write("\nCRAYON: ");
52
+ try {
53
+ while (true) {
54
+ const { done, value } = await reader.read();
55
+ if (done)
56
+ break;
57
+ process.stdout.write(decoder.decode(value, { stream: true }));
58
+ }
59
+ process.stdout.write(decoder.decode());
60
+ process.stdout.write("\n");
61
+ }
62
+ finally {
63
+ reader.releaseLock();
64
+ }
65
+ }
66
+ async generate(prompt) {
67
+ const response = await fetch(`${this.baseUrl}/generate`, {
68
+ method: "POST", headers: { "Content-Type": "application/json" },
69
+ body: JSON.stringify({ prompt, max_new_tokens: this.options.maxNewTokens, temperature: this.options.temperature })
70
+ });
71
+ if (!response.ok)
72
+ throw new Error(`Gemma error (${response.status}): ${await response.text()}`);
73
+ return (await response.json()).text;
74
+ }
75
+ stop() {
76
+ if (this.process && !this.process.killed)
77
+ this.process.kill();
78
+ }
79
+ }
@@ -0,0 +1,6 @@
1
+ fastapi
2
+ uvicorn[standard]
3
+ transformers
4
+ torch
5
+ accelerate
6
+ sentencepiece
@@ -0,0 +1,113 @@
1
+ import os
2
+ from threading import Thread
3
+ import torch
4
+ from fastapi import FastAPI
5
+ from fastapi.responses import StreamingResponse
6
+ from pydantic import BaseModel
7
+ from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
8
+
9
+ MODEL_ID=os.getenv("CRAYON_MODEL","google/gemma-4-E2B-it")
10
+ HOST=os.getenv("CRAYON_HOST","127.0.0.1")
11
+ PORT=int(os.getenv("CRAYON_PORT","8765"))
12
+
13
+ print(f"[CRAYON MODEL] Loading: {MODEL_ID}",flush=True)
14
+ print(f"[CRAYON MODEL] CUDA available: {torch.cuda.is_available()}",flush=True)
15
+ tokenizer=AutoTokenizer.from_pretrained(MODEL_ID)
16
+ model=AutoModelForCausalLM.from_pretrained(MODEL_ID,device_map="auto",torch_dtype="auto")
17
+ model.eval()
18
+ print("[CRAYON MODEL] Model loaded.",flush=True)
19
+ print(f"[CRAYON MODEL] Device: {model.device}",flush=True)
20
+
21
+ app=FastAPI(title="CRAYON Local Gemma Server",version="0.3.0")
22
+
23
+ class GenerateRequest(BaseModel):
24
+ prompt:str
25
+ max_new_tokens:int=1024
26
+ temperature:float=0.7
27
+
28
+ @app.get("/health")
29
+ def health():
30
+ return {"status":"ok","model":MODEL_ID,"device":str(model.device),
31
+ "cuda":torch.cuda.is_available(),"streaming":True,"inference":"local"}
32
+
33
+ def build_inputs(prompt):
34
+ messages=[{"role":"user","content":prompt}]
35
+ inputs=tokenizer.apply_chat_template(messages,tokenize=True,add_generation_prompt=True,
36
+ return_tensors="pt",return_dict=True)
37
+ input_ids=inputs["input_ids"].to(model.device)
38
+ attention_mask=inputs.get("attention_mask")
39
+ if attention_mask is not None:
40
+ attention_mask=attention_mask.to(model.device)
41
+ return input_ids,attention_mask
42
+
43
+ @app.post("/generate/stream")
44
+ def generate_stream(request:GenerateRequest):
45
+ input_ids,attention_mask=build_inputs(request.prompt)
46
+ streamer=TextIteratorStreamer(tokenizer,skip_prompt=True,skip_special_tokens=True)
47
+ kwargs={"input_ids":input_ids,"streamer":streamer,
48
+ "max_new_tokens":request.max_new_tokens,
49
+ "do_sample":request.temperature>0}
50
+ if attention_mask is not None: kwargs["attention_mask"]=attention_mask
51
+ if request.temperature>0: kwargs["temperature"]=request.temperature
52
+
53
+ worker=Thread(target=model.generate,kwargs=kwargs,daemon=True)
54
+ worker.start()
55
+
56
+ def token_iterator():
57
+ generated_text = ""
58
+ chunk_count = 0
59
+
60
+ try:
61
+ for text in streamer:
62
+ chunk_count += 1
63
+ generated_text += text
64
+
65
+ # TextIteratorStreamer emits decoded text chunks. Re-tokenize
66
+ # accumulated output to report the generated-token count.
67
+ token_count = len(
68
+ tokenizer.encode(
69
+ generated_text,
70
+ add_special_tokens=False,
71
+ )
72
+ )
73
+
74
+ print(
75
+ f"[CRAYON TOKEN] #{token_count}: {text!r}",
76
+ flush=True,
77
+ )
78
+
79
+ yield text
80
+
81
+ final_token_count = len(
82
+ tokenizer.encode(
83
+ generated_text,
84
+ add_special_tokens=False,
85
+ )
86
+ )
87
+
88
+ print(
89
+ f"[CRAYON STREAM] Generation complete | "
90
+ f"chunks={chunk_count} | "
91
+ f"generated_tokens={final_token_count}",
92
+ flush=True,
93
+ )
94
+ finally:
95
+ worker.join()
96
+
97
+ return StreamingResponse(token_iterator(),media_type="text/plain; charset=utf-8",
98
+ headers={"Cache-Control":"no-cache","X-CRAYON-Streaming":"true"})
99
+
100
+ @app.post("/generate")
101
+ def generate(request:GenerateRequest):
102
+ input_ids,attention_mask=build_inputs(request.prompt)
103
+ kwargs={"input_ids":input_ids,"max_new_tokens":request.max_new_tokens,
104
+ "do_sample":request.temperature>0}
105
+ if attention_mask is not None: kwargs["attention_mask"]=attention_mask
106
+ if request.temperature>0: kwargs["temperature"]=request.temperature
107
+ output=model.generate(**kwargs)
108
+ new_tokens=output[0][input_ids.shape[-1]:]
109
+ return {"text":tokenizer.decode(new_tokens,skip_special_tokens=True)}
110
+
111
+ if __name__=="__main__":
112
+ import uvicorn
113
+ uvicorn.run(app,host=HOST,port=PORT)
package/package.json CHANGED
@@ -1,29 +1,18 @@
1
1
  {
2
2
  "name": "@lazarusrolando/crayoncli",
3
- "version": "0.1.0",
3
+ "version": "0.3.0",
4
4
  "type": "module",
5
- "bin": {
6
- "crayon": "./dist/index.js"
7
- },
8
5
  "scripts": {
9
- "dev": "tsx src/index.ts",
10
6
  "build": "tsc",
11
- "start": "node dist/index.js"
7
+ "start": "node dist/index.js",
8
+ "dev": "tsx src/index.ts"
12
9
  },
13
10
  "dependencies": {
14
- "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"
11
+ "dotenv": "^16.4.7"
23
12
  },
24
13
  "devDependencies": {
25
- "@types/node": "^24.5.2",
14
+ "@types/node": "^22.10.5",
26
15
  "tsx": "^4.19.2",
27
- "typescript": "^5.9.2"
16
+ "typescript": "^5.7.2"
28
17
  }
29
18
  }
package/src/index.ts CHANGED
@@ -1,3 +1,43 @@
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();
1
+ import "dotenv/config";
2
+ import readline from "node:readline";
3
+ import { LocalGemma } from "./providers/local-gemma.js";
4
+
5
+ const gemma=new LocalGemma({
6
+ python:process.env.CRAYON_PYTHON||"python",
7
+ model:process.env.CRAYON_MODEL||"google/gemma-4-E2B-it",
8
+ host:process.env.CRAYON_HOST||"127.0.0.1",
9
+ port:Number(process.env.CRAYON_PORT||8765),
10
+ maxNewTokens:Number(process.env.CRAYON_MAX_NEW_TOKENS||1024),
11
+ temperature:Number(process.env.CRAYON_TEMPERATURE||0.7)
12
+ });
13
+
14
+ async function main(){
15
+ const oneShot=process.argv.slice(2).join(" ").trim();
16
+ await gemma.start();
17
+
18
+ if(oneShot){ await gemma.generateStream(oneShot); gemma.stop(); return; }
19
+
20
+ console.log("\n==========================================");
21
+ console.log(" CRAYON v0.3.0 - LOCAL STREAMING");
22
+ console.log("==========================================");
23
+ console.log("Gemma inference: LOCAL");
24
+ console.log("Hugging Face inference API: NOT USED");
25
+ console.log("Streaming: ENABLED\n");
26
+ console.log("Type /exit to quit.\n");
27
+
28
+ const rl=readline.createInterface({input:process.stdin,output:process.stdout,prompt:"You: "});
29
+ const shutdown=()=>{rl.close();gemma.stop();process.exit(0);};
30
+ process.on("SIGINT",shutdown);
31
+ rl.prompt();
32
+
33
+ rl.on("line",async line=>{
34
+ const prompt=line.trim();
35
+ if(!prompt){rl.prompt();return;}
36
+ if(prompt==="/exit"){shutdown();return;}
37
+ rl.pause();
38
+ try{await gemma.generateStream(prompt);}
39
+ catch(error){console.error("\n[CRAYON ERROR]",error instanceof Error?error.message:error);}
40
+ finally{rl.resume();rl.prompt();}
41
+ });
42
+ }
43
+ main().catch(error=>{console.error("[CRAYON FATAL]",error);gemma.stop();process.exit(1);});
@@ -0,0 +1,78 @@
1
+ import { spawn, ChildProcess } from "node:child_process";
2
+
3
+ export interface GemmaOptions {
4
+ python:string; model:string; host:string; port:number;
5
+ maxNewTokens:number; temperature:number;
6
+ }
7
+
8
+ export class LocalGemma {
9
+ private process?:ChildProcess;
10
+ private readonly options:GemmaOptions;
11
+ private readonly baseUrl:string;
12
+
13
+ constructor(options:GemmaOptions){
14
+ this.options=options;
15
+ this.baseUrl=`http://${options.host}:${options.port}`;
16
+ }
17
+
18
+ async start():Promise<void>{
19
+ if(await this.isReady()) return;
20
+ console.log("[CRAYON] Starting local Gemma server...");
21
+ console.log(`[CRAYON] Model: ${this.options.model}`);
22
+
23
+ this.process=spawn(this.options.python,["model/server.py"],{
24
+ stdio:["ignore","pipe","pipe"],
25
+ env:{...process.env,CRAYON_MODEL:this.options.model,CRAYON_HOST:this.options.host,
26
+ CRAYON_PORT:String(this.options.port)}
27
+ });
28
+
29
+ this.process.stdout?.on("data",(d:Buffer)=>process.stdout.write(`[Gemma] ${d.toString()}`));
30
+ this.process.stderr?.on("data",(d:Buffer)=>process.stderr.write(`[Gemma] ${d.toString()}`));
31
+
32
+ const deadline=Date.now()+180000;
33
+ while(Date.now()<deadline){
34
+ if(await this.isReady()){ console.log("[CRAYON] Local Gemma server is ready."); return; }
35
+ await new Promise(r=>setTimeout(r,1000));
36
+ }
37
+ throw new Error("Gemma server did not become ready within 180 seconds.");
38
+ }
39
+
40
+ async isReady():Promise<boolean>{
41
+ try{return (await fetch(`${this.baseUrl}/health`)).ok;}catch{return false;}
42
+ }
43
+
44
+ async generateStream(prompt:string):Promise<void>{
45
+ const response=await fetch(`${this.baseUrl}/generate/stream`,{
46
+ method:"POST",headers:{"Content-Type":"application/json"},
47
+ body:JSON.stringify({prompt,max_new_tokens:this.options.maxNewTokens,temperature:this.options.temperature})
48
+ });
49
+ if(!response.ok) throw new Error(`Gemma streaming error (${response.status}): ${await response.text()}`);
50
+ if(!response.body) throw new Error("Gemma returned no streaming body.");
51
+
52
+ const reader=response.body.getReader();
53
+ const decoder=new TextDecoder();
54
+ process.stdout.write("\nCRAYON: ");
55
+ try{
56
+ while(true){
57
+ const {done,value}=await reader.read();
58
+ if(done) break;
59
+ process.stdout.write(decoder.decode(value,{stream:true}));
60
+ }
61
+ process.stdout.write(decoder.decode());
62
+ process.stdout.write("\n");
63
+ }finally{reader.releaseLock();}
64
+ }
65
+
66
+ async generate(prompt:string):Promise<string>{
67
+ const response=await fetch(`${this.baseUrl}/generate`,{
68
+ method:"POST",headers:{"Content-Type":"application/json"},
69
+ body:JSON.stringify({prompt,max_new_tokens:this.options.maxNewTokens,temperature:this.options.temperature})
70
+ });
71
+ if(!response.ok) throw new Error(`Gemma error (${response.status}): ${await response.text()}`);
72
+ return ((await response.json()) as {text:string}).text;
73
+ }
74
+
75
+ stop():void{
76
+ if(this.process && !this.process.killed) this.process.kill();
77
+ }
78
+ }
package/tsconfig.json CHANGED
@@ -1 +1 @@
1
- {"compilerOptions":{"target":"ES2022","module":"NodeNext","moduleResolution":"NodeNext","strict":true,"esModuleInterop":true,"skipLibCheck":true,"outDir":"dist","rootDir":"src"},"include":["src/**/*.ts"]}
1
+ {"compilerOptions":{"target":"ES2022","module":"NodeNext","moduleResolution":"NodeNext","outDir":"dist","rootDir":"src","strict":true,"esModuleInterop":true,"skipLibCheck":true},"include":["src/**/*.ts"]}
@@ -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();}