heartcode 0.1.63 → 0.1.95
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/.githooks/bump-version.mjs +29 -0
- package/.githooks/pre-commit +5 -0
- package/CHANGELOG.md +367 -0
- package/README.md +94 -61
- package/SKILL.md +50 -17
- package/bin/heartcode.js +169 -87
- package/package.json +1 -1
- package/prompts/README.md +1 -0
- package/prompts/agent.md +98 -59
- package/prompts/guided.md +20 -0
- package/prompts/guides.md +168 -0
- package/src/acp.js +1 -1
- package/src/agent.js +276 -77
- package/src/alert.js +93 -0
- package/src/background.js +4 -2
- package/src/capabilities.js +2 -3
- package/src/client.js +30 -4
- package/src/clis.js +601 -0
- package/src/config.js +35 -24
- package/src/edit.js +4 -1
- package/src/guard.js +88 -0
- package/src/guided.js +49 -10
- package/src/guides.js +73 -0
- package/src/headless.js +7 -3
- package/src/hooks.js +3 -2
- package/src/http.js +4 -2
- package/src/importer.js +12 -1
- package/src/log.js +27 -2
- package/src/mcp.js +12 -2
- package/src/probe.js +5 -1
- package/src/prompts.js +2 -1
- package/src/rankings.js +23 -5
- package/src/record.js +80 -0
- package/src/settings.html +520 -307
- package/src/settings.js +199 -82
- package/src/skills.js +2 -0
- package/src/tabs.js +1 -0
- package/src/tools.js +90 -33
- package/src/trust.js +49 -0
- package/src/ui.js +219 -126
- package/src/usage.js +13 -5
- package/src/voice.js +61 -13
- package/src/web.js +26 -4
package/src/usage.js
CHANGED
|
@@ -19,6 +19,11 @@ export async function loadPrices({ url = 'https://models.dev/api.json', timeout
|
|
|
19
19
|
table ??= readJson(FILE, null)
|
|
20
20
|
const asked = arguments[0]?.url || force // a list from somewhere else, or asked for again: always fetched
|
|
21
21
|
if (!asked && ((table?.v === VERSION && Date.now() - table.at < DAY) || process.env.HEART_NO_PRICES)) return table // tests: no network
|
|
22
|
+
if (asked) return fetchPrices(url, timeout, force)
|
|
23
|
+
return (pending ??= fetchPrices(url, timeout, false).finally(() => (pending = null))) // asked for at once by several: fetched once
|
|
24
|
+
}
|
|
25
|
+
let pending = null
|
|
26
|
+
async function fetchPrices(url, timeout, force) {
|
|
22
27
|
try {
|
|
23
28
|
const res = await fetch(url, { signal: AbortSignal.timeout(timeout) })
|
|
24
29
|
if (!res.ok) throw new Error(`HTTP ${res.status}`)
|
|
@@ -43,6 +48,7 @@ export async function loadPrices({ url = 'https://models.dev/api.json', timeout
|
|
|
43
48
|
return table
|
|
44
49
|
}
|
|
45
50
|
|
|
51
|
+
const LOCAL = /^(localhost|127\.|0\.0\.0\.0|\[::1\]|10\.|192\.168\.|172\.(1[6-9]|2\d|3[01])\.|[^.:]+(:\d+)?$)/ // this computer, the local network, a bare host name
|
|
46
52
|
const norm = (s) => String(s ?? '').toLowerCase().replace(/[^a-z0-9]/g, '')
|
|
47
53
|
const hostOf = (url) => {
|
|
48
54
|
try {
|
|
@@ -52,18 +58,20 @@ const hostOf = (url) => {
|
|
|
52
58
|
}
|
|
53
59
|
}
|
|
54
60
|
|
|
55
|
-
// The price of a model at a provider (HeartCode's name for it and its URL), or null
|
|
56
|
-
//
|
|
61
|
+
// The price of a model at a provider (HeartCode's name for it and its URL), or null: what models.dev
|
|
62
|
+
// says for this provider (by its name or URL); else, for a name with its maker in front
|
|
63
|
+
// ("anthropic/claude-sonnet-4"), the maker's own. Never just any provider's: a proxy, a company
|
|
64
|
+
// server or a local one isn't priced like whichever lists the model first.
|
|
57
65
|
export function priceOf(provider, baseUrl, model) {
|
|
58
66
|
if (!table || !model) return null
|
|
59
67
|
const all = Object.entries(table.providers)
|
|
60
|
-
const ids = [model, model.split('/').pop()]
|
|
68
|
+
const ids = [...new Set([model, model.replace(/^models\//, ''), model.split('/').pop()])]
|
|
61
69
|
const find = (list) => {
|
|
62
70
|
for (const [, p] of list) for (const id of ids) if (p.models[id]?.input !== undefined) return (({ limit, can, ...price }) => price)(p.models[id])
|
|
63
71
|
return null
|
|
64
72
|
}
|
|
65
|
-
const
|
|
66
|
-
return find(
|
|
73
|
+
const maker = model.includes('/') ? norm(model.split('/').at(-2)) : ''
|
|
74
|
+
return find(all.filter(ownBy(provider, baseUrl))) ?? (maker && !LOCAL.test(hostOf(baseUrl)) ? find(all.filter(([id]) => norm(id) === maker)) : null)
|
|
67
75
|
}
|
|
68
76
|
// models.dev's providers that are this one: by its name in HeartCode, or its URL.
|
|
69
77
|
const ownBy = (provider, baseUrl) => {
|
package/src/voice.js
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
// Voice input: /voice, then Space to talk (and again to pause), Enter to finish, Esc to
|
|
2
2
|
// cancel. What's said shows in the input box as it's said, and stays there to read over and send.
|
|
3
|
-
// Recording: ffmpeg (macOS: the default microphone; Linux: PulseAudio, else ALSA). Speech to text
|
|
4
|
-
//
|
|
5
|
-
//
|
|
6
|
-
//
|
|
7
|
-
// -
|
|
8
|
-
// "model": "
|
|
9
|
-
//
|
|
3
|
+
// Recording: ffmpeg (macOS: the default microphone; Linux: PulseAudio, else ALSA). Speech to text,
|
|
4
|
+
// one of three ("voice": { "engine": … } in config.json; none set: Hex when it's ready, else whisper-cli):
|
|
5
|
+
// - hex: the Hex app's `hex transcribe` (its Labs › Transcribe files on), with your Hex dictation
|
|
6
|
+
// model ("model": an id from `hex model list`); on this computer, fast;
|
|
7
|
+
// - whisper: whisper.cpp's whisper-cli and a model already on disk (~/.whisper-models,
|
|
8
|
+
// ~/.heart/whisper, or "model": "/path/ggml-….bin"); nothing is downloaded;
|
|
9
|
+
// - openai: OpenAI's /audio/transcriptions, with your OpenAI provider's key ("model": whisper-1 or
|
|
10
|
+
// gpt-4o-transcribe); only when chosen (it's paid).
|
|
11
|
+
// "language": "bn", "auto"… for languages other than English.
|
|
10
12
|
import fs from 'node:fs'
|
|
11
13
|
import os from 'node:os'
|
|
12
14
|
import path from 'node:path'
|
|
@@ -47,11 +49,41 @@ function micInput() {
|
|
|
47
49
|
return ['-f', 'dshow', '-i', `audio=${winMic || 'default'}`]
|
|
48
50
|
}
|
|
49
51
|
|
|
52
|
+
// Hex's command: on the PATH, else inside the app (macOS)
|
|
53
|
+
const HEX_APP = '/Applications/Hex.app/Contents/MacOS/hex'
|
|
54
|
+
export const hexPath = () => onPath('hex') || (process.platform === 'darwin' && fs.existsSync(HEX_APP) ? HEX_APP : '')
|
|
55
|
+
// Hex can transcribe a file: its Labs › Transcribe files is on (asked again after a while when it was off)
|
|
56
|
+
let hexOn = { ok: false, at: 0 }
|
|
57
|
+
export function hexReady() {
|
|
58
|
+
const hex = hexPath()
|
|
59
|
+
if (!hex) return false
|
|
60
|
+
if (!hexOn.ok && Date.now() - hexOn.at > 30_000) hexOn = { ok: /^transcribe-files\s+on\b/m.test(spawnSync(hex, ['labs', 'list'], { encoding: 'utf8', timeout: 5000 }).stdout ?? ''), at: Date.now() }
|
|
61
|
+
return hexOn.ok
|
|
62
|
+
}
|
|
63
|
+
// your provider that is OpenAI's own API (its key), the one named first
|
|
64
|
+
const isOpenAI = (p) => {
|
|
65
|
+
try {
|
|
66
|
+
return /(^|\.)openai\.com$/.test(new URL(p?.baseUrl).hostname)
|
|
67
|
+
} catch {
|
|
68
|
+
return false
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
const openaiOf = (cfg, providers) => (isOpenAI(providers[cfg.provider]) ? cfg.provider : Object.keys(providers).find((n) => isOpenAI(providers[n])))
|
|
72
|
+
export const ENGINES = ['hex', 'whisper', 'openai']
|
|
73
|
+
// the engine in use: the one chosen; else Hex when it can, else whisper-cli
|
|
74
|
+
export function engineOf(cfg = {}, providers = {}) {
|
|
75
|
+
if (ENGINES.includes(cfg.engine)) return cfg.engine
|
|
76
|
+
if (cfg.provider && openaiOf(cfg, providers)) return 'openai' // set before engines: an OpenAI provider
|
|
77
|
+
return hexReady() ? 'hex' : 'whisper'
|
|
78
|
+
}
|
|
79
|
+
|
|
50
80
|
// Why voice can't work here, or null when it can.
|
|
51
81
|
export function voiceProblem(cfg = {}, providers = {}) {
|
|
52
82
|
if (!onPath('ffmpeg')) return `voice input needs ffmpeg to record (${install('ffmpeg', 'ffmpeg', 'install ffmpeg with your package manager')})`
|
|
53
|
-
|
|
54
|
-
if (
|
|
83
|
+
const engine = engineOf(cfg, providers)
|
|
84
|
+
if (engine === 'hex') return !hexPath() ? 'voice with Hex: Hex isn\'t installed here' : hexReady() ? null : 'voice with Hex needs its Labs › Transcribe files: run hex labs on transcribe-files'
|
|
85
|
+
if (engine === 'openai') return openaiOf(cfg, providers) ? null : 'voice with OpenAI needs an OpenAI provider (api.openai.com, with its key): add it on /settings → Providers'
|
|
86
|
+
if (!onPath('whisper-cli')) return `voice input needs whisper-cli (${process.platform === 'darwin' ? 'brew install whisper-cpp' : 'whisper.cpp, from github.com/ggml-org/whisper.cpp'}) and a model, or Hex (its Labs › Transcribe files on)`
|
|
55
87
|
return localModel(cfg) ? null : 'voice input needs a whisper model: put a ggml-*.bin in ~/.whisper-models, or set "voice": { "model": "/path/to/ggml-….bin" }'
|
|
56
88
|
}
|
|
57
89
|
|
|
@@ -114,7 +146,8 @@ export function wavOf(pcm) {
|
|
|
114
146
|
export async function transcribePcm(pcm, cfg = {}, providers = {}, { fast = false, signal } = {}) {
|
|
115
147
|
const file = path.join(os.tmpdir(), `heartcode-voice-${process.pid}-${Date.now()}-${Math.random().toString(36).slice(2, 7)}.wav`)
|
|
116
148
|
fs.writeFileSync(file, wavOf(pcm))
|
|
117
|
-
|
|
149
|
+
const quick = fast && engineOf(cfg, providers) === 'whisper' // whisper: the quickest model while you speak
|
|
150
|
+
return transcribe(file, quick ? { ...cfg, model: localModel(cfg, { fast: true }) } : cfg, providers, signal)
|
|
118
151
|
}
|
|
119
152
|
|
|
120
153
|
// Starts recording to a 16 kHz mono WAV. Returns { stop() → the file, cancel() }.
|
|
@@ -150,14 +183,29 @@ export function record() {
|
|
|
150
183
|
// The words in the recording (and the file is deleted).
|
|
151
184
|
export async function transcribe(file, cfg = {}, providers = {}, signal) {
|
|
152
185
|
try {
|
|
153
|
-
|
|
154
|
-
|
|
186
|
+
const engine = engineOf(cfg, providers)
|
|
187
|
+
if (engine === 'hex') {
|
|
188
|
+
const args = ['transcribe', '--json', ...(cfg.model ? ['--model', String(cfg.model)] : []), ...(cfg.language && cfg.language !== 'auto' ? ['--language', String(cfg.language)] : []), file]
|
|
189
|
+
const q = (a) => `'${String(a).replace(/'/g, "'\\''")}'`
|
|
190
|
+
const { code, output, stderr } = await runCommand(os.tmpdir(), [hexPath(), ...args].map(q).join(' '), { signal, timeout: 120_000, split: true })
|
|
191
|
+
log('voice', `hex transcribe → exit ${code}`, output.slice(0, 2000))
|
|
192
|
+
let said
|
|
193
|
+
try {
|
|
194
|
+
said = JSON.parse(output)
|
|
195
|
+
} catch {}
|
|
196
|
+
if (code !== 0 || !said || said.status === 'error') throw new Error(`Hex couldn't transcribe it: ${(said?.message ?? stderr ?? output).trim().split('\n').pop()}`)
|
|
197
|
+
return clean(said.text)
|
|
198
|
+
}
|
|
199
|
+
if (engine === 'openai') {
|
|
200
|
+
const name = openaiOf(cfg, providers)
|
|
201
|
+
if (!name) throw new Error(voiceProblem(cfg, providers) ?? 'no OpenAI provider')
|
|
202
|
+
const p = providers[name]
|
|
155
203
|
const form = new FormData()
|
|
156
204
|
form.append('file', new Blob([fs.readFileSync(file)], { type: 'audio/wav' }), 'speech.wav')
|
|
157
205
|
form.append('model', cfg.model ?? 'whisper-1')
|
|
158
206
|
if (cfg.language && cfg.language !== 'auto') form.append('language', cfg.language)
|
|
159
207
|
const res = await fetch(`${p.baseUrl.replace(/\/+$/, '')}/audio/transcriptions`, { method: 'POST', body: form, headers: p.apiKey ? { authorization: `Bearer ${p.apiKey}` } : {}, signal })
|
|
160
|
-
if (!res.ok) throw new Error(`${
|
|
208
|
+
if (!res.ok) throw new Error(`${name} said ${res.status}: ${(await res.text()).slice(0, 200)}`)
|
|
161
209
|
return clean((await res.json()).text)
|
|
162
210
|
}
|
|
163
211
|
const model = localModel(cfg)
|
package/src/web.js
CHANGED
|
@@ -5,6 +5,8 @@
|
|
|
5
5
|
// (Brave refuses Node's connections), or junk results for scripts. When none can be reached, the
|
|
6
6
|
// model is told "search is unreachable", so it stops searching instead of trying other words.
|
|
7
7
|
import path from 'node:path'
|
|
8
|
+
import dns from 'node:dns/promises'
|
|
9
|
+
import net from 'node:net'
|
|
8
10
|
import { HOME, CONFIG_FILE, readJson, writeJson } from './config.js'
|
|
9
11
|
import { keywords } from './memory.js'
|
|
10
12
|
import { fetch } from './http.js' // not the built-in one: see http.js
|
|
@@ -74,8 +76,22 @@ function tableToMarkdown(inner) {
|
|
|
74
76
|
|
|
75
77
|
const MAX = 10_000 // what one web_fetch result shows (agent.js keeps about that much of one)
|
|
76
78
|
|
|
79
|
+
// The local network and a cloud machine's metadata service: not fetched for the model unless you
|
|
80
|
+
// named that host (allowed: the hosts in your message). A page or file telling it to fetch
|
|
81
|
+
// http://169.254.169.254/… would hand over what's there. This computer (localhost: the project's
|
|
82
|
+
// own dev server) is fine.
|
|
83
|
+
const PRIVATE = (ip) => /^(?:10\.|192\.168\.|169\.254\.|0\.|172\.(?:1[6-9]|2\d|3[01])\.|100\.(?:6[4-9]|[7-9]\d|1[01]\d|12[0-7])\.)/.test(ip) || /^(?:::$|f[cd]|fe[89ab]|::ffff:(?:10|0|192\.168|169\.254)\.)/i.test(ip)
|
|
84
|
+
export const hostsIn = (text) => new Set([...String(text ?? '').matchAll(/\b(?:https?:\/\/)?((?:[a-z0-9-]+\.)*[a-z0-9-]+|\[[0-9a-f:]+\])(?::\d+)?(?=[/\s'"),]|$)/gi)].map((m) => m[1].toLowerCase()))
|
|
85
|
+
const notPrivate = (allowed) => async (u) => {
|
|
86
|
+
const host = u.hostname.replace(/^\[|\]$/g, '').toLowerCase()
|
|
87
|
+
if (allowed?.has(host)) return
|
|
88
|
+
const ips = net.isIP(host) ? [host] : (await dns.lookup(host, { all: true }).catch(() => [])).map((a) => a.address)
|
|
89
|
+
if (ips.some(PRIVATE)) throw Object.assign(new Error(`${host} is on a private network: not fetched (name it in your message to allow it)`), { refused: true })
|
|
90
|
+
}
|
|
91
|
+
|
|
77
92
|
// A page, fetched and turned into Markdown: { url (after redirects), title, text, status, ok } or { error }.
|
|
78
|
-
|
|
93
|
+
// allowed: hosts the user named (a local or private one among them may be fetched)
|
|
94
|
+
export async function fetchPage(url, { signal, allowed } = {}) {
|
|
79
95
|
let u
|
|
80
96
|
try {
|
|
81
97
|
u = new URL(/^\w+:\/\//.test(url) ? url : `https://${url}`)
|
|
@@ -83,7 +99,13 @@ export async function fetchPage(url, { signal } = {}) {
|
|
|
83
99
|
return { error: `"${url}" is not a URL` }
|
|
84
100
|
}
|
|
85
101
|
if (!/^https?:$/.test(u.protocol)) return { error: 'only http and https URLs can be fetched' }
|
|
86
|
-
|
|
102
|
+
let res
|
|
103
|
+
try {
|
|
104
|
+
res = await fetch(u, { headers: HEADERS, redirect: 'follow', guard: notPrivate(allowed), signal: signal ? AbortSignal.any([signal, AbortSignal.timeout(30_000)]) : AbortSignal.timeout(30_000) })
|
|
105
|
+
} catch (e) {
|
|
106
|
+
if (e.refused) return { error: e.message }
|
|
107
|
+
throw e
|
|
108
|
+
}
|
|
87
109
|
const type = res.headers.get('content-type') ?? ''
|
|
88
110
|
if (!/text|json|xml|javascript|markdown/.test(type) && type) return { error: `${u.href} is ${type.split(';')[0]}, not text (${res.status})` }
|
|
89
111
|
const body = await res.text()
|
|
@@ -94,8 +116,8 @@ export async function fetchPage(url, { signal } = {}) {
|
|
|
94
116
|
}
|
|
95
117
|
|
|
96
118
|
// A page's text, from `start` (characters), for the model (no file to point at).
|
|
97
|
-
export async function webFetch(url, { start = 0, signal } = {}) {
|
|
98
|
-
const p = await fetchPage(url, { signal })
|
|
119
|
+
export async function webFetch(url, { start = 0, signal, allowed } = {}) {
|
|
120
|
+
const p = await fetchPage(url, { signal, allowed })
|
|
99
121
|
if (p.error) return `error: ${p.error}`
|
|
100
122
|
const from = Math.max(0, Number(start) || 0)
|
|
101
123
|
const part = p.text.slice(from, from + MAX)
|