noguard 1.0.0 → 1.1.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/bin/noguard.mjs CHANGED
@@ -1,154 +1,216 @@
1
- #!/usr/bin/env node
2
-
3
- const VERSION = '1.0.0'
4
- const HELP = `
5
- \x1b[1mNoGuard AI CLI v${VERSION}\x1b[0m
6
- The AI that doesn't say no — from your terminal.
7
-
8
- \x1b[1mUsage:\x1b[0m
9
- noguard Start chatting (same as noguard chat)
10
- noguard chat [--model X] Start an interactive chat session
11
- noguard login Sign in with your NoGuard account
12
- noguard logout Sign out and revoke the token
13
- noguard me Show your account and credits
14
- noguard models List available models with tiers and pricing
15
- noguard usage [--days N] Show credit usage history
16
- noguard config Show config file location
17
- noguard endpoint [URL] Show or set the API endpoint
18
-
19
- \x1b[1mOptions:\x1b[0m
20
- --help, -h Show this help
21
- --version, -v Show version
22
- --model, -m Choose a model (alias or name, e.g. noguard-ultra)
23
-
24
- \x1b[1mIn-chat commands:\x1b[0m
25
- /models List models and switch
26
- /model <name> Switch to a model by alias or name
27
- /libraries Show and toggle research libraries
28
- /clear Start a new conversation
29
- /credits Show remaining credits
30
- /help Show all commands
31
- /exit Leave the chat
32
- `
33
-
34
- async function main() {
35
- const args = process.argv.slice(2)
36
- const command = args[0]?.startsWith('-') ? null : args[0]
37
-
38
- if (args.includes('--help') || args.includes('-h')) {
39
- console.log(HELP)
40
- return
41
- }
42
- if (args.includes('--version') || args.includes('-v')) {
43
- console.log(VERSION)
44
- return
45
- }
46
-
47
- switch (command) {
48
- case 'login': {
49
- const { login } = await import('../lib/auth.mjs')
50
- await login()
51
- break
52
- }
53
- case 'logout': {
54
- const { logout } = await import('../lib/auth.mjs')
55
- await logout()
56
- break
57
- }
58
- case 'me': {
59
- const { getToken } = await import('../lib/config.mjs')
60
- const { get } = await import('../lib/api.mjs')
61
- if (!getToken()) { console.log('\x1b[31m✗\x1b[0m Not signed in. Run \x1b[1mnoguard login\x1b[0m first.'); return }
62
- try {
63
- const me = await get('/api/v1/me')
64
- console.log()
65
- console.log(` \x1b[1mAccount\x1b[0m`)
66
- console.log(` Email: ${me.email}`)
67
- console.log(` Tier: ${me.tier}`)
68
- console.log(` Verified: ${me.verified ? '\x1b[32myes\x1b[0m' : '\x1b[31mno\x1b[0m'}`)
69
- console.log(` Credits: \x1b[33m${me.credits}\x1b[0m (${me.available} available)`)
70
- console.log(` Token: ${me.token.name} (${me.token.uid})`)
71
- console.log()
72
- } catch (err) {
73
- if (err.status === 401) console.log('\x1b[31m✗\x1b[0m Session expired. Run \x1b[1mnoguard login\x1b[0m again.')
74
- else console.error(`\x1b[31m✗\x1b[0m ${err.message}`)
75
- }
76
- break
77
- }
78
- case 'models': {
79
- const { get } = await import('../lib/api.mjs')
80
- const { printModels } = await import('../lib/chat.mjs')
81
- try {
82
- const res = await get('/api/v1/models')
83
- printModels(res.data)
84
- } catch (err) {
85
- console.error(`\x1b[31m✗\x1b[0m ${err.message}`)
86
- }
87
- break
88
- }
89
- case 'usage': {
90
- const { getToken } = await import('../lib/config.mjs')
91
- const { get } = await import('../lib/api.mjs')
92
- if (!getToken()) { console.log('\x1b[31m✗\x1b[0m Not signed in. Run \x1b[1mnoguard login\x1b[0m first.'); return }
93
- const daysIdx = args.indexOf('--days')
94
- const days = daysIdx !== -1 ? parseInt(args[daysIdx + 1], 10) || 30 : 30
95
- try {
96
- const res = await get(`/api/v1/usage?days=${days}`)
97
- console.log()
98
- console.log(` \x1b[1mUsage (last ${res.days_covered} days)\x1b[0m`)
99
- if (res.by_day?.length) {
100
- for (const day of res.by_day) {
101
- console.log(` ${day.date} \x1b[33m${day.credits_spent}\x1b[0m credits ${day.requests} requests`)
102
- }
103
- } else {
104
- console.log(' \x1b[2mNo usage yet.\x1b[0m')
105
- }
106
- console.log()
107
- } catch (err) {
108
- if (err.status === 401) console.log('\x1b[31m✗\x1b[0m Session expired. Run \x1b[1mnoguard login\x1b[0m again.')
109
- else console.error(`\x1b[31m✗\x1b[0m ${err.message}`)
110
- }
111
- break
112
- }
113
- case 'config': {
114
- const { configPath, load } = await import('../lib/config.mjs')
115
- const cfg = load()
116
- console.log()
117
- console.log(` \x1b[1mConfig\x1b[0m`)
118
- console.log(` File: ${configPath()}`)
119
- console.log(` Endpoint: ${cfg.endpoint ?? 'https://noguard.ai'}`)
120
- console.log(` Signed: ${cfg.token ? '\x1b[32myes\x1b[0m' : '\x1b[31mno\x1b[0m'}`)
121
- if (cfg.account) console.log(` Account: ${cfg.account.email}`)
122
- console.log()
123
- break
124
- }
125
- case 'endpoint': {
126
- const sub = args[1]
127
- if (sub && !sub.startsWith('-')) {
128
- const { setEndpoint } = await import('../lib/config.mjs')
129
- setEndpoint(sub)
130
- console.log(`\x1b[32m✓\x1b[0m Endpoint set to \x1b[1m${sub}\x1b[0m`)
131
- } else {
132
- const { getEndpoint } = await import('../lib/config.mjs')
133
- console.log(getEndpoint())
134
- }
135
- break
136
- }
137
- case 'chat':
138
- case undefined: {
139
- const modelIdx = args.indexOf('--model') !== -1 ? args.indexOf('--model') : args.indexOf('-m')
140
- const model = modelIdx !== -1 ? args[modelIdx + 1] : undefined
141
- const { chat } = await import('../lib/chat.mjs')
142
- await chat({ model })
143
- break
144
- }
145
- default:
146
- console.log(`\x1b[31m✗\x1b[0m Unknown command: ${command}`)
147
- console.log(' Run \x1b[1mnoguard --help\x1b[0m for usage.')
148
- }
149
- }
150
-
151
- main().catch(err => {
152
- console.error(`\x1b[31m✗\x1b[0m ${err.message}`)
153
- process.exit(1)
154
- })
1
+ #!/usr/bin/env node
2
+
3
+ const VERSION = '1.1.0'
4
+ const HELP = `
5
+ \x1b[1mNoGuard AI CLI v${VERSION}\x1b[0m
6
+ The AI that doesn't say no — in your terminal, working on your project.
7
+
8
+ Run \x1b[1mnoguard\x1b[0m inside a project folder. NoGuard can read, search and edit its files
9
+ and run commands there, asking you before it changes anything.
10
+
11
+ \x1b[1mUsage:\x1b[0m
12
+ noguard Start a session in the current folder
13
+ noguard "task" Start a session with a first message
14
+ noguard -p "task" Do one task and print the result (no prompts; pipe input works too)
15
+ noguard login Sign in with your NoGuard account
16
+ noguard logout Sign out and revoke the token
17
+ noguard me Show your account and credits
18
+ noguard models List available models with tiers and pricing
19
+ noguard usage [--days N] Show credit usage history
20
+ noguard config Show config file location
21
+ noguard endpoint [URL] Show or set the API endpoint
22
+
23
+ \x1b[1mOptions:\x1b[0m
24
+ --model, -m <name> Choose a model (alias or name, e.g. noguard-ultra)
25
+ --print, -p Print mode: run one task and exit
26
+ --accept-edits Let NoGuard edit files without asking (still asks before commands)
27
+ --yolo Let NoGuard edit files and run commands without asking
28
+ --no-tools Chat only: NoGuard can't see or change files
29
+ --help, -h Show this help
30
+ --version, -v Show version
31
+
32
+ \x1b[1mIn-session commands:\x1b[0m
33
+ /models, /model <name> List models, switch model
34
+ /mode [ask|accept-edits|yolo] Show or change what needs your approval
35
+ /init Write a NOGUARD.md describing this project
36
+ /compact Free context by dropping old tool output
37
+ /libraries, /library <id> Research libraries
38
+ /clear Start a new conversation
39
+ /credits Show remaining credits
40
+ /help, /exit
41
+
42
+ \x1b[1mProject instructions:\x1b[0m
43
+ NoGuard reads NOGUARD.md (and CLAUDE.md or AGENTS.md) from the project folder and
44
+ its parents, and ~/.noguard/NOGUARD.md, at the start of each session.
45
+ `
46
+
47
+ const COMMANDS = new Set(['login', 'logout', 'me', 'models', 'usage', 'config', 'endpoint', 'chat'])
48
+ const VALUE_FLAGS = new Set(['--model', '-m', '--days'])
49
+
50
+ function parseArgs(argv) {
51
+ const flags = new Set()
52
+ const values = {}
53
+ const positional = []
54
+ for (let i = 0; i < argv.length; i++) {
55
+ const arg = argv[i]
56
+ if (VALUE_FLAGS.has(arg)) values[arg] = argv[++i]
57
+ else if (arg.startsWith('-') && arg.length > 1) flags.add(arg)
58
+ else positional.push(arg)
59
+ }
60
+ return { flags, values, positional }
61
+ }
62
+
63
+ /** Piped input. With a typed task too, an open stdin that sends nothing (scripts, CI) is not waited on. */
64
+ function readStdin(optional) {
65
+ if (process.stdin.isTTY) return Promise.resolve('')
66
+ return new Promise(resolve => {
67
+ const chunks = []
68
+ const done = () => { clearTimeout(idle); process.stdin.destroy(); resolve(Buffer.concat(chunks).toString('utf8')) }
69
+ const idle = optional ? setTimeout(done, 500) : null
70
+ process.stdin.on('data', chunk => { clearTimeout(idle); chunks.push(chunk) })
71
+ process.stdin.on('end', done)
72
+ process.stdin.on('error', done)
73
+ })
74
+ }
75
+
76
+ async function main() {
77
+ const args = process.argv.slice(2)
78
+ const { flags, values, positional } = parseArgs(args)
79
+ const command = COMMANDS.has(positional[0]) ? positional[0] : positional.length ? 'prompt' : undefined
80
+
81
+ if (flags.has('--help') || flags.has('-h')) {
82
+ console.log(HELP)
83
+ return
84
+ }
85
+ if (flags.has('--version') || flags.has('-v')) {
86
+ console.log(VERSION)
87
+ return
88
+ }
89
+ const session = {
90
+ model: values['--model'] ?? values['-m'],
91
+ tools: !flags.has('--no-tools'),
92
+ mode: flags.has('--yolo') || flags.has('--dangerously-skip-permissions') ? 'yolo' : flags.has('--accept-edits') ? 'accept-edits' : 'ask',
93
+ }
94
+ if (flags.has('-p') || flags.has('--print')) {
95
+ const typed = (command === 'chat' ? positional.slice(1) : positional).join(' ')
96
+ const piped = await readStdin(Boolean(typed))
97
+ const prompt = [typed, piped.trim()].filter(Boolean).join('\n\n')
98
+ if (!prompt) {
99
+ console.error('\x1b[31m✗\x1b[0m Give a task: noguard -p "what to do" (or pipe text in).')
100
+ process.exit(1)
101
+ }
102
+ const { runOnce } = await import('../lib/chat.mjs')
103
+ await runOnce({ ...session, prompt })
104
+ return
105
+ }
106
+
107
+ switch (command) {
108
+ case 'login': {
109
+ const { login } = await import('../lib/auth.mjs')
110
+ await login()
111
+ break
112
+ }
113
+ case 'logout': {
114
+ const { logout } = await import('../lib/auth.mjs')
115
+ await logout()
116
+ break
117
+ }
118
+ case 'me': {
119
+ const { getToken } = await import('../lib/config.mjs')
120
+ const { get } = await import('../lib/api.mjs')
121
+ if (!getToken()) { console.log('\x1b[31m✗\x1b[0m Not signed in. Run \x1b[1mnoguard login\x1b[0m first.'); return }
122
+ try {
123
+ const me = await get('/api/v1/me')
124
+ console.log()
125
+ console.log(` \x1b[1mAccount\x1b[0m`)
126
+ console.log(` Email: ${me.email}`)
127
+ console.log(` Tier: ${me.tier}`)
128
+ console.log(` Verified: ${me.verified ? '\x1b[32myes\x1b[0m' : '\x1b[31mno\x1b[0m'}`)
129
+ console.log(` Credits: \x1b[33m${me.credits}\x1b[0m (${me.available} available)`)
130
+ console.log(` Token: ${me.token.name} (${me.token.uid})`)
131
+ console.log()
132
+ } catch (err) {
133
+ if (err.status === 401) console.log('\x1b[31m✗\x1b[0m Session expired. Run \x1b[1mnoguard login\x1b[0m again.')
134
+ else console.error(`\x1b[31m✗\x1b[0m ${err.message}`)
135
+ }
136
+ break
137
+ }
138
+ case 'models': {
139
+ const { get } = await import('../lib/api.mjs')
140
+ const { printModels } = await import('../lib/chat.mjs')
141
+ try {
142
+ const res = await get('/api/v1/models')
143
+ printModels(res.data)
144
+ } catch (err) {
145
+ console.error(`\x1b[31m✗\x1b[0m ${err.message}`)
146
+ }
147
+ break
148
+ }
149
+ case 'usage': {
150
+ const { getToken } = await import('../lib/config.mjs')
151
+ const { get } = await import('../lib/api.mjs')
152
+ if (!getToken()) { console.log('\x1b[31m✗\x1b[0m Not signed in. Run \x1b[1mnoguard login\x1b[0m first.'); return }
153
+ const days = parseInt(values['--days'], 10) || 30
154
+ try {
155
+ const res = await get(`/api/v1/usage?days=${days}`)
156
+ console.log()
157
+ console.log(` \x1b[1mUsage (last ${res.days_covered} days)\x1b[0m`)
158
+ if (res.by_day?.length) {
159
+ for (const day of res.by_day) {
160
+ console.log(` ${day.date} \x1b[33m${day.credits_spent}\x1b[0m credits ${day.requests} requests`)
161
+ }
162
+ } else {
163
+ console.log(' \x1b[2mNo usage yet.\x1b[0m')
164
+ }
165
+ console.log()
166
+ } catch (err) {
167
+ if (err.status === 401) console.log('\x1b[31m✗\x1b[0m Session expired. Run \x1b[1mnoguard login\x1b[0m again.')
168
+ else console.error(`\x1b[31m✗\x1b[0m ${err.message}`)
169
+ }
170
+ break
171
+ }
172
+ case 'config': {
173
+ const { configPath, load } = await import('../lib/config.mjs')
174
+ const cfg = load()
175
+ console.log()
176
+ console.log(` \x1b[1mConfig\x1b[0m`)
177
+ console.log(` File: ${configPath()}`)
178
+ console.log(` Endpoint: ${cfg.endpoint ?? 'https://noguard.ai'}`)
179
+ console.log(` Signed: ${cfg.token ? '\x1b[32myes\x1b[0m' : '\x1b[31mno\x1b[0m'}`)
180
+ if (cfg.account) console.log(` Account: ${cfg.account.email}`)
181
+ console.log()
182
+ break
183
+ }
184
+ case 'endpoint': {
185
+ const sub = positional[1]
186
+ if (sub) {
187
+ const { setEndpoint } = await import('../lib/config.mjs')
188
+ setEndpoint(sub)
189
+ console.log(`\x1b[32m✓\x1b[0m Endpoint set to \x1b[1m${sub}\x1b[0m`)
190
+ } else {
191
+ const { getEndpoint } = await import('../lib/config.mjs')
192
+ console.log(getEndpoint())
193
+ }
194
+ break
195
+ }
196
+ case 'prompt':
197
+ case 'chat':
198
+ case undefined: {
199
+ const words = command === 'chat' ? positional.slice(1) : positional
200
+ // A lone word is more likely a mistyped command than a task.
201
+ if (command === 'prompt' && words.length === 1 && !/\s/.test(words[0])) {
202
+ console.log(`\x1b[31m✗\x1b[0m Unknown command: ${words[0]}`)
203
+ console.log(' Run \x1b[1mnoguard --help\x1b[0m for usage, or quote a task: noguard "fix the failing test"')
204
+ process.exit(1)
205
+ }
206
+ const { chat } = await import('../lib/chat.mjs')
207
+ await chat({ ...session, prompt: words.join(' ') || undefined })
208
+ break
209
+ }
210
+ }
211
+ }
212
+
213
+ main().catch(err => {
214
+ console.error(`\x1b[31m✗\x1b[0m ${err.message}`)
215
+ process.exit(1)
216
+ })