noguard 1.1.0 → 1.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/bin/noguard.mjs +245 -216
- package/lib/agent.mjs +24 -17
- package/lib/chat.mjs +515 -377
- package/lib/persona-eval.mjs +305 -0
- package/lib/render.mjs +186 -41
- package/package.json +1 -1
package/bin/noguard.mjs
CHANGED
|
@@ -1,216 +1,245 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
noguard
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
const
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
if (
|
|
95
|
-
const
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
return
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
const {
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
console.log(
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
const
|
|
175
|
-
|
|
176
|
-
console.log(
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
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.0'
|
|
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
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
import { randomBytes, randomUUID } from 'node:crypto'
|
|
5
5
|
import { ApiError, streamMessages } from './api.mjs'
|
|
6
6
|
import { isInsideReal, resolvePath } from './files.mjs'
|
|
7
|
-
import { clean, MarkdownStream, Spinner, toolHeader, toolResultLine, BOLD,
|
|
7
|
+
import { clean, MarkdownStream, Spinner, toolHeader, toolResultLine, BOLD, DIM, RST, UNDERLINE, NEON_CYAN, HOT_PINK, LIME, ORANGE, BRIGHT_WHITE, SOFT_GRAY } from './render.mjs'
|
|
8
8
|
import { systemPrompt } from './context.mjs'
|
|
9
9
|
import { TOOLS_BY_NAME, toolDefinitions } from './tools.mjs'
|
|
10
10
|
|
|
@@ -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') {
|
|
@@ -183,10 +190,10 @@ export class Agent {
|
|
|
183
190
|
}
|
|
184
191
|
this.messages.push({ role: 'user', content: results })
|
|
185
192
|
if (halt || this.controller.signal.aborted) {
|
|
186
|
-
if (this.controller.signal.aborted) console.log(` ${
|
|
193
|
+
if (this.controller.signal.aborted) console.log(` ${SOFT_GRAY}⎿ Interrupted. Tell NoGuard what to do instead.${RST}`)
|
|
187
194
|
break
|
|
188
195
|
}
|
|
189
|
-
if (step === MAX_STEPS - 1) console.log(`${
|
|
196
|
+
if (step === MAX_STEPS - 1) console.log(`${ORANGE}⚠${RST} Stopped after ${MAX_STEPS} steps. Send a message to continue.`)
|
|
190
197
|
}
|
|
191
198
|
} finally {
|
|
192
199
|
this.spinner.stop()
|
|
@@ -270,7 +277,7 @@ export class Agent {
|
|
|
270
277
|
await new Promise(r => setTimeout(r, 2000))
|
|
271
278
|
continue
|
|
272
279
|
}
|
|
273
|
-
console.log(`${
|
|
280
|
+
console.log(`${HOT_PINK}✗${RST} Connection error: ${err.message}`)
|
|
274
281
|
return { blocks: [], spent: 0, balance: null, stop: true, failed: true }
|
|
275
282
|
}
|
|
276
283
|
}
|
|
@@ -282,10 +289,10 @@ export class Agent {
|
|
|
282
289
|
}
|
|
283
290
|
|
|
284
291
|
#explain(err) {
|
|
285
|
-
if (err.status === 401 || err.code === 'invalid_token') console.log(`${
|
|
286
|
-
else if (err.code === 'out_of_credits' || err.status === 402) console.log(`${
|
|
287
|
-
else if (err.code === 'model_unavailable') console.log(`${
|
|
288
|
-
else console.log(`${
|
|
292
|
+
if (err.status === 401 || err.code === 'invalid_token') console.log(`${HOT_PINK}✗${RST} Session expired. Run ${BOLD}noguard login${RST} again.`)
|
|
293
|
+
else if (err.code === 'out_of_credits' || err.status === 402) console.log(`${ORANGE}⚠${RST} ${err.message}\n Top up at ${NEON_CYAN}${UNDERLINE}https://noguard.ai/credits${RST}`)
|
|
294
|
+
else if (err.code === 'model_unavailable') console.log(`${ORANGE}⚠${RST} ${err.message} Run /models to pick another.`)
|
|
295
|
+
else console.log(`${HOT_PINK}✗${RST} ${err.message}`)
|
|
289
296
|
return err.message
|
|
290
297
|
}
|
|
291
298
|
|
|
@@ -367,7 +374,7 @@ export class Agent {
|
|
|
367
374
|
finished.push(call)
|
|
368
375
|
}
|
|
369
376
|
if (streamError) {
|
|
370
|
-
console.log(`${
|
|
377
|
+
console.log(`${HOT_PINK}✗${RST} ${streamError}`)
|
|
371
378
|
return { blocks: finished.filter(b => b.type === 'text'), spent, balance, stop: true, failed: true }
|
|
372
379
|
}
|
|
373
380
|
if (stopReason === 'max_tokens' && !finished.some(b => b.type === 'tool_use')) {
|
|
@@ -382,19 +389,19 @@ export class Agent {
|
|
|
382
389
|
const fail = (message, extra = {}) => ({ result: { type: 'tool_result', tool_use_id: call.id, content: message, is_error: true }, ...extra })
|
|
383
390
|
if (!tool) {
|
|
384
391
|
console.log(toolHeader(call.name))
|
|
385
|
-
console.log(toolResultLine(`Unknown tool`,
|
|
392
|
+
console.log(toolResultLine(`Unknown tool`, HOT_PINK))
|
|
386
393
|
return fail(`There is no tool named ${call.name}.`)
|
|
387
394
|
}
|
|
388
395
|
let label = ''
|
|
389
396
|
try { label = tool.label(call.input, this.ctx) } catch { /* bad input shows no label */ }
|
|
390
397
|
console.log(toolHeader(tool.name, label))
|
|
391
398
|
if (call.parseError) {
|
|
392
|
-
console.log(toolResultLine('Invalid arguments',
|
|
399
|
+
console.log(toolResultLine('Invalid arguments', HOT_PINK))
|
|
393
400
|
return fail(`The tool arguments were not valid JSON (${call.parseError}). The reply may have been cut off; try again with smaller input.`)
|
|
394
401
|
}
|
|
395
402
|
const missing = (tool.input_schema.required ?? []).filter(key => call.input[key] === undefined)
|
|
396
403
|
if (missing.length) {
|
|
397
|
-
console.log(toolResultLine(`Missing ${missing.join(', ')}`,
|
|
404
|
+
console.log(toolResultLine(`Missing ${missing.join(', ')}`, HOT_PINK))
|
|
398
405
|
return fail(`Missing required argument${missing.length > 1 ? 's' : ''}: ${missing.join(', ')}.`)
|
|
399
406
|
}
|
|
400
407
|
const reason = this.permissions.check(tool, call.input, this.cwd)
|
|
@@ -405,16 +412,16 @@ export class Agent {
|
|
|
405
412
|
if (tool.kind === 'exec') {
|
|
406
413
|
// The header may be shortened: show exactly what will run before asking.
|
|
407
414
|
const command = clean(String(call.input.command ?? ''))
|
|
408
|
-
if (command !== label) console.log(command.split('\n').map(line => ` ${
|
|
415
|
+
if (command !== label) console.log(command.split('\n').map(line => ` ${NEON_CYAN}${line}${RST}`).join('\n'))
|
|
409
416
|
if (call.input.description) console.log(` ${DIM}${clean(String(call.input.description))}${RST}`)
|
|
410
417
|
}
|
|
411
418
|
const always = this.permissions.alwaysLabel(tool, call.input)
|
|
412
|
-
const answer = await this.ui.ask(` ${
|
|
419
|
+
const answer = await this.ui.ask(` ${ORANGE}?${RST} Allow NoGuard to ${reason}? ${LIME}y${RST}es${always ? ` · ${NEON_CYAN}a${RST}lways (${always})` : ''} · ${HOT_PINK}n${RST}o, or type what to do instead ${NEON_CYAN}›${RST} `)
|
|
413
420
|
if (answer === null) {
|
|
414
421
|
const why = this.ui.interactive
|
|
415
422
|
? 'Not run: the user interrupted.'
|
|
416
423
|
: `Not run: this needs approval, and nobody can approve it in print mode. Rerun with --accept-edits or --yolo to allow it.`
|
|
417
|
-
console.log(toolResultLine(this.ui.interactive ? 'Interrupted' : 'Needs approval (print mode)',
|
|
424
|
+
console.log(toolResultLine(this.ui.interactive ? 'Interrupted' : 'Needs approval (print mode)', ORANGE))
|
|
418
425
|
return fail(why, { halt: true })
|
|
419
426
|
}
|
|
420
427
|
const reply = answer.trim()
|
|
@@ -423,7 +430,7 @@ export class Agent {
|
|
|
423
430
|
if (always) this.permissions.alwaysAllow(tool, call.input)
|
|
424
431
|
} else if (!(word === '' || word === 'y' || word === 'yes')) {
|
|
425
432
|
const feedback = ['n', 'no'].includes(word) ? '' : reply
|
|
426
|
-
console.log(toolResultLine('Denied',
|
|
433
|
+
console.log(toolResultLine('Denied', ORANGE))
|
|
427
434
|
if (feedback) {
|
|
428
435
|
return fail('The user denied this action.', { feedback: `The user denied that and said: ${feedback}` })
|
|
429
436
|
}
|
|
@@ -439,7 +446,7 @@ export class Agent {
|
|
|
439
446
|
}
|
|
440
447
|
this.spinner.stop()
|
|
441
448
|
if (outcome.error) {
|
|
442
|
-
console.log(toolResultLine(clean(outcome.error),
|
|
449
|
+
console.log(toolResultLine(clean(outcome.error), HOT_PINK))
|
|
443
450
|
return fail(outcome.error)
|
|
444
451
|
}
|
|
445
452
|
if (outcome.display) console.log(toolResultLine(outcome.display))
|