noguard 1.1.1 → 1.2.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.
package/bin/noguard.mjs CHANGED
@@ -1,233 +1,245 @@
1
- #!/usr/bin/env node
2
-
3
- import {
4
- BOLD, RST, UNDERLINE,
5
- NEON_CYAN, HOT_PINK, LIME, ORANGE, GOLD, SKY, BRIGHT_WHITE, SOFT_GRAY, DARK_GRAY,
6
- gradient, NOGUARD_GRADIENT, sectionHeader,
7
- } from '../lib/render.mjs'
8
-
9
- const VERSION = '1.1.1'
10
-
11
- function helpText() {
12
- return `
13
- ${gradient('NoGuard AI CLI', NOGUARD_GRADIENT)} ${SOFT_GRAY}v${VERSION}${RST}
14
- ${SOFT_GRAY}The AI that doesn't say no — in your terminal, working on your project.${RST}
15
-
16
- Run ${BRIGHT_WHITE}${BOLD}noguard${RST} inside a project folder. NoGuard can read, search and edit its files
17
- and run commands there, asking you before it changes anything.
18
-
19
- ${GOLD}${BOLD}Usage:${RST}
20
- ${BRIGHT_WHITE}noguard${RST} ${SOFT_GRAY}Start a session in the current folder${RST}
21
- ${BRIGHT_WHITE}noguard${RST} ${SKY}"task"${RST} ${SOFT_GRAY}Start a session with a first message${RST}
22
- ${BRIGHT_WHITE}noguard${RST} ${NEON_CYAN}-p${RST} ${SKY}"task"${RST} ${SOFT_GRAY}Do one task and print the result (pipe input works too)${RST}
23
- ${BRIGHT_WHITE}noguard login${RST} ${SOFT_GRAY}Sign in with your NoGuard account${RST}
24
- ${BRIGHT_WHITE}noguard logout${RST} ${SOFT_GRAY}Sign out and revoke the token${RST}
25
- ${BRIGHT_WHITE}noguard me${RST} ${SOFT_GRAY}Show your account and credits${RST}
26
- ${BRIGHT_WHITE}noguard models${RST} ${SOFT_GRAY}List available models with tiers and pricing${RST}
27
- ${BRIGHT_WHITE}noguard usage${RST} ${NEON_CYAN}[--days N]${RST} ${SOFT_GRAY}Show credit usage history${RST}
28
- ${BRIGHT_WHITE}noguard config${RST} ${SOFT_GRAY}Show config file location${RST}
29
- ${BRIGHT_WHITE}noguard endpoint${RST} ${SKY}[URL]${RST} ${SOFT_GRAY}Show or set the API endpoint${RST}
30
-
31
- ${GOLD}${BOLD}Options:${RST}
32
- ${NEON_CYAN}--model, -m${RST} ${SKY}<name>${RST} ${SOFT_GRAY}Choose a model (alias or name, e.g. noguard-ultra)${RST}
33
- ${NEON_CYAN}--print, -p${RST} ${SOFT_GRAY}Print mode: run one task and exit${RST}
34
- ${NEON_CYAN}--accept-edits${RST} ${SOFT_GRAY}Let NoGuard edit files without asking (still asks before commands)${RST}
35
- ${NEON_CYAN}--yolo${RST} ${SOFT_GRAY}Let NoGuard edit files and run commands without asking${RST}
36
- ${NEON_CYAN}--no-tools${RST} ${SOFT_GRAY}Chat only: NoGuard can't see or change files${RST}
37
- ${NEON_CYAN}--help, -h${RST} ${SOFT_GRAY}Show this help${RST}
38
- ${NEON_CYAN}--version, -v${RST} ${SOFT_GRAY}Show version${RST}
39
-
40
- ${GOLD}${BOLD}In-session commands:${RST}
41
- ${BRIGHT_WHITE}/models, /model <name>${RST} ${SOFT_GRAY}List models, switch model${RST}
42
- ${BRIGHT_WHITE}/mode${RST} ${SKY}[ask|accept-edits|yolo]${RST} ${SOFT_GRAY}Show or change what needs your approval${RST}
43
- ${BRIGHT_WHITE}/status${RST} ${SOFT_GRAY}Show session info, credits, model, and stats${RST}
44
- ${BRIGHT_WHITE}/init${RST} ${SOFT_GRAY}Write a NOGUARD.md describing this project${RST}
45
- ${BRIGHT_WHITE}/compact${RST} ${SOFT_GRAY}Free context by dropping old tool output${RST}
46
- ${BRIGHT_WHITE}/libraries, /library <id>${RST} ${SOFT_GRAY}Research libraries${RST}
47
- ${BRIGHT_WHITE}/clear${RST} ${SOFT_GRAY}Start a new conversation${RST}
48
- ${BRIGHT_WHITE}/credits${RST} ${SOFT_GRAY}Show remaining credits${RST}
49
- ${BRIGHT_WHITE}/help, /exit${RST}
50
-
51
- ${GOLD}${BOLD}Project instructions:${RST}
52
- NoGuard reads ${SKY}NOGUARD.md${RST} (and ${SKY}CLAUDE.md${RST} or ${SKY}AGENTS.md${RST}) from the project folder and
53
- its parents, and ${SKY}~/.noguard/NOGUARD.md${RST}, at the start of each session.
54
- `
55
- }
56
-
57
- const COMMANDS = new Set(['login', 'logout', 'me', 'models', 'usage', 'config', 'endpoint', 'chat'])
58
- const VALUE_FLAGS = new Set(['--model', '-m', '--days'])
59
-
60
- function parseArgs(argv) {
61
- const flags = new Set()
62
- const values = {}
63
- const positional = []
64
- for (let i = 0; i < argv.length; i++) {
65
- const arg = argv[i]
66
- if (VALUE_FLAGS.has(arg)) values[arg] = argv[++i]
67
- else if (arg.startsWith('-') && arg.length > 1) flags.add(arg)
68
- else positional.push(arg)
69
- }
70
- return { flags, values, positional }
71
- }
72
-
73
- /** Piped input. With a typed task too, an open stdin that sends nothing (scripts, CI) is not waited on. */
74
- function readStdin(optional) {
75
- if (process.stdin.isTTY) return Promise.resolve('')
76
- return new Promise(resolve => {
77
- const chunks = []
78
- const done = () => { clearTimeout(idle); process.stdin.destroy(); resolve(Buffer.concat(chunks).toString('utf8')) }
79
- const idle = optional ? setTimeout(done, 500) : null
80
- process.stdin.on('data', chunk => { clearTimeout(idle); chunks.push(chunk) })
81
- process.stdin.on('end', done)
82
- process.stdin.on('error', done)
83
- })
84
- }
85
-
86
- function fmtNumber(n) {
87
- return Number(n).toLocaleString('en-US')
88
- }
89
-
90
- async function main() {
91
- const args = process.argv.slice(2)
92
- const { flags, values, positional } = parseArgs(args)
93
- const command = COMMANDS.has(positional[0]) ? positional[0] : positional.length ? 'prompt' : undefined
94
-
95
- if (flags.has('--help') || flags.has('-h')) {
96
- console.log(helpText())
97
- return
98
- }
99
- if (flags.has('--version') || flags.has('-v')) {
100
- console.log(`${gradient('NoGuard', NOGUARD_GRADIENT)} ${SOFT_GRAY}v${VERSION}${RST}`)
101
- return
102
- }
103
- const session = {
104
- model: values['--model'] ?? values['-m'],
105
- tools: !flags.has('--no-tools'),
106
- mode: flags.has('--yolo') || flags.has('--dangerously-skip-permissions') ? 'yolo' : flags.has('--accept-edits') ? 'accept-edits' : 'ask',
107
- }
108
- if (flags.has('-p') || flags.has('--print')) {
109
- const typed = (command === 'chat' ? positional.slice(1) : positional).join(' ')
110
- const piped = await readStdin(Boolean(typed))
111
- const prompt = [typed, piped.trim()].filter(Boolean).join('\n\n')
112
- if (!prompt) {
113
- console.error(`${HOT_PINK}✗${RST} Give a task: ${BRIGHT_WHITE}noguard -p "what to do"${RST} (or pipe text in).`)
114
- process.exit(1)
115
- }
116
- const { runOnce } = await import('../lib/chat.mjs')
117
- await runOnce({ ...session, prompt })
118
- return
119
- }
120
-
121
- switch (command) {
122
- case 'login': {
123
- const { login } = await import('../lib/auth.mjs')
124
- await login()
125
- break
126
- }
127
- case 'logout': {
128
- const { logout } = await import('../lib/auth.mjs')
129
- await logout()
130
- break
131
- }
132
- case 'me': {
133
- const { getToken } = await import('../lib/config.mjs')
134
- const { get } = await import('../lib/api.mjs')
135
- if (!getToken()) { console.log(`${HOT_PINK}✗${RST} Not signed in. Run ${BOLD}noguard login${RST} first.`); return }
136
- try {
137
- const me = await get('/api/v1/me')
138
- const tierColor = me.tier === 'ultra' ? GOLD : me.tier === 'pro' ? LIME : NEON_CYAN
139
- console.log()
140
- console.log(sectionHeader('Account'))
141
- console.log()
142
- console.log(` ${SOFT_GRAY}Email:${RST} ${BRIGHT_WHITE}${me.email}${RST}`)
143
- console.log(` ${SOFT_GRAY}Tier:${RST} ${tierColor}${BOLD}${me.tier}${RST}`)
144
- console.log(` ${SOFT_GRAY}Verified:${RST} ${me.verified ? `${LIME}yes${RST}` : `${HOT_PINK}no${RST}`}`)
145
- console.log(` ${SOFT_GRAY}Credits:${RST} ${ORANGE}${BOLD}${fmtNumber(me.credits)}${RST} ${DARK_GRAY}(${fmtNumber(me.available)} available)${RST}`)
146
- console.log(` ${SOFT_GRAY}Token:${RST} ${SKY}${me.token.name}${RST} ${DARK_GRAY}(${me.token.uid})${RST}`)
147
- console.log()
148
- } catch (err) {
149
- if (err.status === 401) console.log(`${HOT_PINK}✗${RST} Session expired. Run ${BOLD}noguard login${RST} again.`)
150
- else console.error(`${HOT_PINK}✗${RST} ${err.message}`)
151
- }
152
- break
153
- }
154
- case 'models': {
155
- const { get } = await import('../lib/api.mjs')
156
- const { printModels } = await import('../lib/chat.mjs')
157
- try {
158
- const res = await get('/api/v1/models')
159
- printModels(res.data)
160
- } catch (err) {
161
- console.error(`${HOT_PINK}✗${RST} ${err.message}`)
162
- }
163
- break
164
- }
165
- case 'usage': {
166
- const { getToken } = await import('../lib/config.mjs')
167
- const { get } = await import('../lib/api.mjs')
168
- if (!getToken()) { console.log(`${HOT_PINK}✗${RST} Not signed in. Run ${BOLD}noguard login${RST} first.`); return }
169
- const days = parseInt(values['--days'], 10) || 30
170
- try {
171
- const res = await get(`/api/v1/usage?days=${days}`)
172
- console.log()
173
- console.log(sectionHeader(`Usage (last ${res.days_covered} days)`))
174
- console.log()
175
- if (res.by_day?.length) {
176
- for (const day of res.by_day) {
177
- console.log(` ${BRIGHT_WHITE}${day.date}${RST} ${ORANGE}${BOLD}${fmtNumber(day.credits_spent)}${RST} ${SOFT_GRAY}credits${RST} ${DARK_GRAY}${day.requests} requests${RST}`)
178
- }
179
- } else {
180
- console.log(` ${SOFT_GRAY}No usage yet.${RST}`)
181
- }
182
- console.log()
183
- } catch (err) {
184
- if (err.status === 401) console.log(`${HOT_PINK}✗${RST} Session expired. Run ${BOLD}noguard login${RST} again.`)
185
- else console.error(`${HOT_PINK}✗${RST} ${err.message}`)
186
- }
187
- break
188
- }
189
- case 'config': {
190
- const { configPath, load } = await import('../lib/config.mjs')
191
- const cfg = load()
192
- console.log()
193
- console.log(sectionHeader('Config'))
194
- console.log()
195
- console.log(` ${SOFT_GRAY}File:${RST} ${SKY}${configPath()}${RST}`)
196
- console.log(` ${SOFT_GRAY}Endpoint:${RST} ${BRIGHT_WHITE}${cfg.endpoint ?? 'https://noguard.ai'}${RST}`)
197
- console.log(` ${SOFT_GRAY}Signed:${RST} ${cfg.token ? `${LIME}yes${RST}` : `${HOT_PINK}no${RST}`}`)
198
- if (cfg.account) console.log(` ${SOFT_GRAY}Account:${RST} ${BRIGHT_WHITE}${cfg.account.email}${RST}`)
199
- console.log()
200
- break
201
- }
202
- case 'endpoint': {
203
- const sub = positional[1]
204
- if (sub) {
205
- const { setEndpoint } = await import('../lib/config.mjs')
206
- setEndpoint(sub)
207
- console.log(`${LIME}✓${RST} Endpoint set to ${BRIGHT_WHITE}${BOLD}${sub}${RST}`)
208
- } else {
209
- const { getEndpoint } = await import('../lib/config.mjs')
210
- console.log(getEndpoint())
211
- }
212
- break
213
- }
214
- case 'prompt':
215
- case 'chat':
216
- case undefined: {
217
- const words = command === 'chat' ? positional.slice(1) : positional
218
- if (command === 'prompt' && words.length === 1 && !/\s/.test(words[0])) {
219
- console.log(`${HOT_PINK}✗${RST} Unknown command: ${BRIGHT_WHITE}${words[0]}${RST}`)
220
- console.log(` Run ${BRIGHT_WHITE}${BOLD}noguard --help${RST} for usage, or quote a task: ${BRIGHT_WHITE}noguard "fix the failing test"${RST}`)
221
- process.exit(1)
222
- }
223
- const { chat } = await import('../lib/chat.mjs')
224
- await chat({ ...session, prompt: words.join(' ') || undefined })
225
- break
226
- }
227
- }
228
- }
229
-
230
- main().catch(err => {
231
- console.error(`${HOT_PINK}✗${RST} ${err.message}`)
232
- process.exit(1)
233
- })
1
+ #!/usr/bin/env node
2
+
3
+ import {
4
+ BOLD, RST, UNDERLINE,
5
+ NEON_CYAN, HOT_PINK, LIME, ORANGE, GOLD, SKY, BRIGHT_WHITE, SOFT_GRAY, DARK_GRAY,
6
+ AMBER, MINT, ROSE, FG_STRONG, MUTED, FAINT,
7
+ gradient, NOGUARD_GRADIENT, sectionHeader, tierPill, creditMeter, sparkline,
8
+ } from '../lib/render.mjs'
9
+
10
+ const VERSION = '1.2.1'
11
+
12
+ function helpText() {
13
+ return `
14
+ ${gradient('NoGuard AI CLI', NOGUARD_GRADIENT)} ${SOFT_GRAY}v${VERSION}${RST}
15
+ ${SOFT_GRAY}The AI that doesn't say no — in your terminal, working on your project.${RST}
16
+
17
+ Run ${BRIGHT_WHITE}${BOLD}noguard${RST} inside a project folder. NoGuard can read, search and edit its files
18
+ and run commands there, asking you before it changes anything.
19
+
20
+ ${GOLD}${BOLD}Usage:${RST}
21
+ ${BRIGHT_WHITE}noguard${RST} ${SOFT_GRAY}Start a session in the current folder${RST}
22
+ ${BRIGHT_WHITE}noguard${RST} ${SKY}"task"${RST} ${SOFT_GRAY}Start a session with a first message${RST}
23
+ ${BRIGHT_WHITE}noguard${RST} ${NEON_CYAN}-p${RST} ${SKY}"task"${RST} ${SOFT_GRAY}Do one task and print the result (pipe input works too)${RST}
24
+ ${BRIGHT_WHITE}noguard login${RST} ${SOFT_GRAY}Sign in with your NoGuard account${RST}
25
+ ${BRIGHT_WHITE}noguard logout${RST} ${SOFT_GRAY}Sign out and revoke the token${RST}
26
+ ${BRIGHT_WHITE}noguard me${RST} ${SOFT_GRAY}Show your account and credits${RST}
27
+ ${BRIGHT_WHITE}noguard models${RST} ${SOFT_GRAY}List available models with tiers and pricing${RST}
28
+ ${BRIGHT_WHITE}noguard usage${RST} ${NEON_CYAN}[--days N]${RST} ${SOFT_GRAY}Show credit usage history${RST}
29
+ ${BRIGHT_WHITE}noguard config${RST} ${SOFT_GRAY}Show config file location${RST}
30
+ ${BRIGHT_WHITE}noguard endpoint${RST} ${SKY}[URL]${RST} ${SOFT_GRAY}Show or set the API endpoint${RST}
31
+ ${BRIGHT_WHITE}noguard eval-personas${RST} ${SOFT_GRAY}Run standardized persona evaluations (--help for options)${RST}
32
+
33
+ ${GOLD}${BOLD}Options:${RST}
34
+ ${NEON_CYAN}--model, -m${RST} ${SKY}<name>${RST} ${SOFT_GRAY}Choose a model (alias or name, e.g. noguard-ultra)${RST}
35
+ ${NEON_CYAN}--print, -p${RST} ${SOFT_GRAY}Print mode: run one task and exit${RST}
36
+ ${NEON_CYAN}--accept-edits${RST} ${SOFT_GRAY}Let NoGuard edit files without asking (still asks before commands)${RST}
37
+ ${NEON_CYAN}--yolo${RST} ${SOFT_GRAY}Let NoGuard edit files and run commands without asking${RST}
38
+ ${NEON_CYAN}--no-tools${RST} ${SOFT_GRAY}Chat only: NoGuard can't see or change files${RST}
39
+ ${NEON_CYAN}--help, -h${RST} ${SOFT_GRAY}Show this help${RST}
40
+ ${NEON_CYAN}--version, -v${RST} ${SOFT_GRAY}Show version${RST}
41
+
42
+ ${GOLD}${BOLD}In-session commands:${RST}
43
+ ${BRIGHT_WHITE}/models, /model <name>${RST} ${SOFT_GRAY}List models, switch model${RST}
44
+ ${BRIGHT_WHITE}/mode${RST} ${SKY}[ask|accept-edits|yolo]${RST} ${SOFT_GRAY}Show or change what needs your approval${RST}
45
+ ${BRIGHT_WHITE}/status${RST} ${SOFT_GRAY}Show session info, credits, model, and stats${RST}
46
+ ${BRIGHT_WHITE}/init${RST} ${SOFT_GRAY}Write a NOGUARD.md describing this project${RST}
47
+ ${BRIGHT_WHITE}/compact${RST} ${SOFT_GRAY}Free context by dropping old tool output${RST}
48
+ ${BRIGHT_WHITE}/libraries, /library <id>${RST} ${SOFT_GRAY}Research libraries${RST}
49
+ ${BRIGHT_WHITE}/clear${RST} ${SOFT_GRAY}Start a new conversation${RST}
50
+ ${BRIGHT_WHITE}/credits${RST} ${SOFT_GRAY}Show remaining credits${RST}
51
+ ${BRIGHT_WHITE}/help, /exit${RST}
52
+
53
+ ${GOLD}${BOLD}Project instructions:${RST}
54
+ NoGuard reads ${SKY}NOGUARD.md${RST} (and ${SKY}CLAUDE.md${RST} or ${SKY}AGENTS.md${RST}) from the project folder and
55
+ its parents, and ${SKY}~/.noguard/NOGUARD.md${RST}, at the start of each session.
56
+ `
57
+ }
58
+
59
+ const COMMANDS = new Set(['login', 'logout', 'me', 'models', 'usage', 'config', 'endpoint', 'chat'])
60
+ const VALUE_FLAGS = new Set(['--model', '-m', '--days'])
61
+
62
+ function parseArgs(argv) {
63
+ const flags = new Set()
64
+ const values = {}
65
+ const positional = []
66
+ for (let i = 0; i < argv.length; i++) {
67
+ const arg = argv[i]
68
+ if (VALUE_FLAGS.has(arg)) values[arg] = argv[++i]
69
+ else if (arg.startsWith('-') && arg.length > 1) flags.add(arg)
70
+ else positional.push(arg)
71
+ }
72
+ return { flags, values, positional }
73
+ }
74
+
75
+ /** Piped input. With a typed task too, an open stdin that sends nothing (scripts, CI) is not waited on. */
76
+ function readStdin(optional) {
77
+ if (process.stdin.isTTY) return Promise.resolve('')
78
+ return new Promise(resolve => {
79
+ const chunks = []
80
+ const done = () => { clearTimeout(idle); process.stdin.destroy(); resolve(Buffer.concat(chunks).toString('utf8')) }
81
+ const idle = optional ? setTimeout(done, 500) : null
82
+ process.stdin.on('data', chunk => { clearTimeout(idle); chunks.push(chunk) })
83
+ process.stdin.on('end', done)
84
+ process.stdin.on('error', done)
85
+ })
86
+ }
87
+
88
+ function fmtNumber(n) {
89
+ return Number(n).toLocaleString('en-US')
90
+ }
91
+
92
+ async function main() {
93
+ const args = process.argv.slice(2)
94
+ if (args[0] === 'eval-personas') {
95
+ const { main: evaluatePersonas } = await import('../lib/persona-eval.mjs')
96
+ await evaluatePersonas(args.slice(1))
97
+ return
98
+ }
99
+ const { flags, values, positional } = parseArgs(args)
100
+ const command = COMMANDS.has(positional[0]) ? positional[0] : positional.length ? 'prompt' : undefined
101
+
102
+ if (flags.has('--help') || flags.has('-h')) {
103
+ console.log(helpText())
104
+ return
105
+ }
106
+ if (flags.has('--version') || flags.has('-v')) {
107
+ console.log(`${gradient('NoGuard', NOGUARD_GRADIENT)} ${SOFT_GRAY}v${VERSION}${RST}`)
108
+ return
109
+ }
110
+ const session = {
111
+ model: values['--model'] ?? values['-m'],
112
+ tools: !flags.has('--no-tools'),
113
+ mode: flags.has('--yolo') || flags.has('--dangerously-skip-permissions') ? 'yolo' : flags.has('--accept-edits') ? 'accept-edits' : 'ask',
114
+ }
115
+ if (flags.has('-p') || flags.has('--print')) {
116
+ const typed = (command === 'chat' ? positional.slice(1) : positional).join(' ')
117
+ const piped = await readStdin(Boolean(typed))
118
+ const prompt = [typed, piped.trim()].filter(Boolean).join('\n\n')
119
+ if (!prompt) {
120
+ console.error(`${HOT_PINK}✗${RST} Give a task: ${BRIGHT_WHITE}noguard -p "what to do"${RST} (or pipe text in).`)
121
+ process.exit(1)
122
+ }
123
+ const { runOnce } = await import('../lib/chat.mjs')
124
+ await runOnce({ ...session, prompt })
125
+ return
126
+ }
127
+
128
+ switch (command) {
129
+ case 'login': {
130
+ const { login } = await import('../lib/auth.mjs')
131
+ await login()
132
+ break
133
+ }
134
+ case 'logout': {
135
+ const { logout } = await import('../lib/auth.mjs')
136
+ await logout()
137
+ break
138
+ }
139
+ case 'me': {
140
+ const { getToken } = await import('../lib/config.mjs')
141
+ const { get } = await import('../lib/api.mjs')
142
+ if (!getToken()) { console.log(`${HOT_PINK}✗${RST} Not signed in. Run ${BOLD}noguard login${RST} first.`); return }
143
+ try {
144
+ const me = await get('/api/v1/me')
145
+ const low = me.low_balance || me.available < 100
146
+ const row = (name, value) => console.log(` ${MUTED}${name.padEnd(9)}${RST} ${value}`)
147
+ console.log()
148
+ console.log(sectionHeader('Account'))
149
+ console.log()
150
+ if (me.display_name) row('Name', `${FG_STRONG}${BOLD}${me.display_name}${RST}`)
151
+ row('Email', `${FG_STRONG}${me.email}${RST} ${tierPill(me.tier)}`)
152
+ row('Verified', me.verified ? `${MINT}yes${RST}` : `${ROSE}no${RST}`)
153
+ row('Credits', `${low ? ROSE : AMBER}${BOLD}${fmtNumber(me.available)}${RST} ${MUTED}available${RST} ${FAINT}of ${fmtNumber(me.credits)}${RST} ${creditMeter(me.available, me.credits, { low })}`)
154
+ row('Token', `${SKY}${me.token.name}${RST} ${FAINT}(${me.token.uid})${RST}`)
155
+ console.log()
156
+ } catch (err) {
157
+ if (err.status === 401) console.log(`${HOT_PINK}✗${RST} Session expired. Run ${BOLD}noguard login${RST} again.`)
158
+ else console.error(`${HOT_PINK}✗${RST} ${err.message}`)
159
+ }
160
+ break
161
+ }
162
+ case 'models': {
163
+ const { get } = await import('../lib/api.mjs')
164
+ const { printModels } = await import('../lib/chat.mjs')
165
+ try {
166
+ const res = await get('/api/v1/models')
167
+ printModels(res.data)
168
+ } catch (err) {
169
+ console.error(`${HOT_PINK}✗${RST} ${err.message}`)
170
+ }
171
+ break
172
+ }
173
+ case 'usage': {
174
+ const { getToken } = await import('../lib/config.mjs')
175
+ const { get } = await import('../lib/api.mjs')
176
+ if (!getToken()) { console.log(`${HOT_PINK}✗${RST} Not signed in. Run ${BOLD}noguard login${RST} first.`); return }
177
+ const days = parseInt(values['--days'], 10) || 30
178
+ try {
179
+ const res = await get(`/api/v1/usage?days=${days}`)
180
+ console.log()
181
+ console.log(sectionHeader(`Usage · last ${res.days_covered} days`))
182
+ console.log()
183
+ const list = res.days ?? []
184
+ if (list.length) {
185
+ for (const day of list) {
186
+ console.log(` ${FG_STRONG}${day.day}${RST} ${AMBER}${BOLD}${fmtNumber(day.credits)}${RST} ${MUTED}cr${RST} ${FAINT}${day.requests} req${RST}`)
187
+ }
188
+ const total = res.total_credits ?? list.reduce((a, b) => a + b.credits, 0)
189
+ console.log()
190
+ console.log(` ${sparkline(list.map(d => d.credits))} ${FAINT}${fmtNumber(total)} credits · ${fmtNumber(res.total_requests ?? list.reduce((a, b) => a + b.requests, 0))} requests${RST}`)
191
+ } else {
192
+ console.log(` ${MUTED}No usage yet.${RST}`)
193
+ }
194
+ console.log()
195
+ } catch (err) {
196
+ if (err.status === 401) console.log(`${HOT_PINK}✗${RST} Session expired. Run ${BOLD}noguard login${RST} again.`)
197
+ else console.error(`${HOT_PINK}✗${RST} ${err.message}`)
198
+ }
199
+ break
200
+ }
201
+ case 'config': {
202
+ const { configPath, load } = await import('../lib/config.mjs')
203
+ const cfg = load()
204
+ console.log()
205
+ console.log(sectionHeader('Config'))
206
+ console.log()
207
+ console.log(` ${SOFT_GRAY}File:${RST} ${SKY}${configPath()}${RST}`)
208
+ console.log(` ${SOFT_GRAY}Endpoint:${RST} ${BRIGHT_WHITE}${cfg.endpoint ?? 'https://noguard.ai'}${RST}`)
209
+ console.log(` ${SOFT_GRAY}Signed:${RST} ${cfg.token ? `${LIME}yes${RST}` : `${HOT_PINK}no${RST}`}`)
210
+ if (cfg.account) console.log(` ${SOFT_GRAY}Account:${RST} ${BRIGHT_WHITE}${cfg.account.email}${RST}`)
211
+ console.log()
212
+ break
213
+ }
214
+ case 'endpoint': {
215
+ const sub = positional[1]
216
+ if (sub) {
217
+ const { setEndpoint } = await import('../lib/config.mjs')
218
+ setEndpoint(sub)
219
+ console.log(`${LIME}✓${RST} Endpoint set to ${BRIGHT_WHITE}${BOLD}${sub}${RST}`)
220
+ } else {
221
+ const { getEndpoint } = await import('../lib/config.mjs')
222
+ console.log(getEndpoint())
223
+ }
224
+ break
225
+ }
226
+ case 'prompt':
227
+ case 'chat':
228
+ case undefined: {
229
+ const words = command === 'chat' ? positional.slice(1) : positional
230
+ if (command === 'prompt' && words.length === 1 && !/\s/.test(words[0])) {
231
+ console.log(`${HOT_PINK}✗${RST} Unknown command: ${BRIGHT_WHITE}${words[0]}${RST}`)
232
+ console.log(` Run ${BRIGHT_WHITE}${BOLD}noguard --help${RST} for usage, or quote a task: ${BRIGHT_WHITE}noguard "fix the failing test"${RST}`)
233
+ process.exit(1)
234
+ }
235
+ const { chat } = await import('../lib/chat.mjs')
236
+ await chat({ ...session, prompt: words.join(' ') || undefined })
237
+ break
238
+ }
239
+ }
240
+ }
241
+
242
+ main().catch(err => {
243
+ console.error(`${HOT_PINK}✗${RST} ${err.message}`)
244
+ process.exit(1)
245
+ })
package/lib/agent.mjs CHANGED
@@ -143,6 +143,13 @@ export class Agent {
143
143
  this.controller?.abort()
144
144
  }
145
145
 
146
+ /** Rough context usage, for the prompt status line: { used, max, left } in characters. */
147
+ contextUse() {
148
+ const { chars } = this.#budget(this.getModel?.())
149
+ const used = this.#size()
150
+ return { used, max: chars, left: Math.max(0, chars - used) }
151
+ }
152
+
146
153
  #pushUserText(text) {
147
154
  const last = this.messages.at(-1)
148
155
  if (last?.role === 'user') {