@avowa/runtime 0.0.0-stage → 0.12.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/LICENSE +202 -0
- package/NOTICE +4 -0
- package/README.md +104 -2
- package/avowa-runtime.js +834 -0
- package/cli.js +255 -0
- package/env.js +42 -0
- package/package.json +48 -4
- package/register.js +130 -0
package/cli.js
ADDED
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
// Copyright 2026 Avowa, Inc.
|
|
4
|
+
/**
|
|
5
|
+
* `npx @avowa/runtime <command>` - set up and check the Runtime SDK. A setup tool, not a launcher:
|
|
6
|
+
* after `init` your app starts the way it always has.
|
|
7
|
+
*
|
|
8
|
+
* init <setup-token> connect this project: writes avowa.env (one line: AVOWA_TOKEN)
|
|
9
|
+
* init --dry-run show what init would do; changes nothing, uses no token
|
|
10
|
+
* status what is wired, what is covered, does the server know this key
|
|
11
|
+
* doctor the server check only (sends no signal)
|
|
12
|
+
*/
|
|
13
|
+
import fs from 'node:fs'
|
|
14
|
+
import path from 'node:path'
|
|
15
|
+
import crypto from 'node:crypto'
|
|
16
|
+
import readline from 'node:readline'
|
|
17
|
+
import { checkedBase, makeToken, readToken, SDK_VERSION as VERSION } from './avowa-runtime.js'
|
|
18
|
+
import { loadEnv } from './env.js'
|
|
19
|
+
|
|
20
|
+
const REGISTER = '@avowa/runtime/register'
|
|
21
|
+
const USAGE = `usage: npx @avowa/runtime <command>
|
|
22
|
+
|
|
23
|
+
init <setup-token> [--api https://api.avowa.ai] [--dry-run] [--yes]
|
|
24
|
+
status
|
|
25
|
+
doctor
|
|
26
|
+
|
|
27
|
+
Generate the setup token in the Avowa console: Connections, Runtime SDK.
|
|
28
|
+
In production set two variables and change nothing else:
|
|
29
|
+
AVOWA_TOKEN=<the value in avowa.env>
|
|
30
|
+
NODE_OPTIONS=--import ${REGISTER}
|
|
31
|
+
AVOWA_DISABLE=1 switches everything off.`
|
|
32
|
+
|
|
33
|
+
// How each library is covered: "auto" needs nothing; the rest say what they need.
|
|
34
|
+
const LIBS = [
|
|
35
|
+
['openai', 'covered automatically (OpenAI, Azure OpenAI, OpenAI-compatible hosts)'],
|
|
36
|
+
['@anthropic-ai/sdk', 'covered automatically'],
|
|
37
|
+
['@aws-sdk/client-bedrock-runtime', 'no wrapper yet: record calls with avowa.capture(...)'],
|
|
38
|
+
['@google/genai', 'no wrapper yet: record calls with avowa.capture(...)'],
|
|
39
|
+
['@google/generative-ai', 'no wrapper yet: record calls with avowa.capture(...)'],
|
|
40
|
+
['ai', 'Vercel AI SDK calls providers over its own HTTP client: record calls with avowa.track(...)'],
|
|
41
|
+
]
|
|
42
|
+
const BUNDLED = ['next', 'nuxt', '@remix-run/dev', '@sveltejs/kit']
|
|
43
|
+
|
|
44
|
+
const readPkg = () => { try { return JSON.parse(fs.readFileSync('package.json', 'utf8')) } catch { return null } }
|
|
45
|
+
const deps = (pkg) => ({ ...(pkg && pkg.dependencies), ...(pkg && pkg.devDependencies) })
|
|
46
|
+
|
|
47
|
+
function coverage(pkg) {
|
|
48
|
+
const d = deps(pkg)
|
|
49
|
+
const out = LIBS.filter(([n]) => n in d).map(([n, note]) => ` ${n.padEnd(18)}: ${note}`)
|
|
50
|
+
if (!out.length) out.push(' no AI provider library in package.json. Calls made over fetch need avowa.capture(...).')
|
|
51
|
+
const b = BUNDLED.filter((n) => n in d)
|
|
52
|
+
if (b.length) out.push(` ${b.join(', ')} bundles server code, so the preload may not see the provider SDK.\n` +
|
|
53
|
+
' Keep it external to the bundle (Next.js: serverExternalPackages), or wrap the\n' +
|
|
54
|
+
' client once with avowa.instrumentOpenAI(client).')
|
|
55
|
+
return out
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function nodeOk() {
|
|
59
|
+
const [maj, min] = process.versions.node.split('.').map(Number)
|
|
60
|
+
return maj > 20 || (maj === 20 && min >= 6) || (maj === 18 && min >= 18) || maj === 19
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function gitignore(dry) {
|
|
64
|
+
if (!fs.existsSync('.gitignore') && !fs.existsSync('.git')) return null
|
|
65
|
+
let lines = []
|
|
66
|
+
try { lines = fs.readFileSync('.gitignore', 'utf8').split(/\r?\n/) } catch { /* no file yet */ }
|
|
67
|
+
if (lines.some((l) => ['avowa.env', '/avowa.env', '*.env'].includes(l.trim()))) return '.gitignore : already ignores avowa.env'
|
|
68
|
+
if (dry) return '.gitignore : would add avowa.env'
|
|
69
|
+
try {
|
|
70
|
+
const sep = lines.length && lines[lines.length - 1] !== '' ? '\n' : ''
|
|
71
|
+
fs.appendFileSync('.gitignore', sep + 'avowa.env\n')
|
|
72
|
+
} catch { return '.gitignore : could not be updated. Add the line avowa.env yourself; the file holds a secret.' }
|
|
73
|
+
return '.gitignore : added avowa.env (the file holds a secret)'
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// The scripts that start the app with plain `node`, and what each would become.
|
|
77
|
+
function startScripts(pkg) {
|
|
78
|
+
const out = []
|
|
79
|
+
for (const name of ['start', 'dev', 'serve']) {
|
|
80
|
+
const cmd = pkg && pkg.scripts && pkg.scripts[name]
|
|
81
|
+
if (typeof cmd !== 'string' || cmd.includes(REGISTER)) continue
|
|
82
|
+
const m = cmd.match(/^(\s*(?:[A-Z_]+=\S+\s+)*node\s)(.*)$/)
|
|
83
|
+
if (m) out.push({ name, from: cmd, to: `${m[1]}--import ${REGISTER} ${m[2]}` })
|
|
84
|
+
}
|
|
85
|
+
return out
|
|
86
|
+
}
|
|
87
|
+
const wired = (pkg) => (process.env.NODE_OPTIONS || '').includes(REGISTER) ||
|
|
88
|
+
Object.values((pkg && pkg.scripts) || {}).some((c) => typeof c === 'string' && c.includes(REGISTER))
|
|
89
|
+
|
|
90
|
+
function ask(question) {
|
|
91
|
+
if (!process.stdin.isTTY) return Promise.resolve(false)
|
|
92
|
+
const rl = readline.createInterface({ input: process.stdin, output: process.stdout })
|
|
93
|
+
return new Promise((ok) => rl.question(question + ' [y/N] ', (a) => { rl.close(); ok(/^y(es)?$/i.test(a.trim())) }))
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
async function redeem(token, api) {
|
|
97
|
+
const [base, problem] = checkedBase(api)
|
|
98
|
+
if (problem) return [null, problem.replace('AVOWA_BASE_URL', '--api') + '.']
|
|
99
|
+
let r
|
|
100
|
+
try {
|
|
101
|
+
r = await fetch(base + '/public/sdk/setup', { method: 'POST', body: JSON.stringify({ token }),
|
|
102
|
+
headers: { 'Content-Type': 'application/json', 'User-Agent': 'avowa-runtime-js (+https://avowa.ai)' },
|
|
103
|
+
signal: AbortSignal.timeout(15000) })
|
|
104
|
+
} catch (e) { return [null, `Could not reach ${base} (${e.name}). Pass --api with your Avowa API URL.`] }
|
|
105
|
+
if (r.status === 404) return [null, 'That setup token is invalid, expired (24h) or already used. Generate a new one in the console.']
|
|
106
|
+
if (!r.ok) return [null, `${base} answered HTTP ${r.status}.`]
|
|
107
|
+
const creds = await r.json()
|
|
108
|
+
if (creds.connector_id !== 'runtime_sdk') return [null, 'That token belongs to a Collector connection, not the Runtime SDK.']
|
|
109
|
+
const [b, p] = checkedBase(creds.base_url || base)
|
|
110
|
+
if (p) return [null, `The server returned an unsafe API URL (${p}); nothing was written.`]
|
|
111
|
+
return [{ ...creds, base_url: b }, null]
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
async function init(args) {
|
|
115
|
+
const dry = args.includes('--dry-run')
|
|
116
|
+
const yes = args.includes('--yes')
|
|
117
|
+
let api = (process.env.AVOWA_BASE_URL || 'https://api.avowa.ai').replace(/\/+$/, '')
|
|
118
|
+
const i = args.indexOf('--api')
|
|
119
|
+
if (i >= 0) {
|
|
120
|
+
if (!args[i + 1]) { console.error('--api needs a URL'); return 2 }
|
|
121
|
+
api = args[i + 1].replace(/\/+$/, ''); args = [...args.slice(0, i), ...args.slice(i + 2)]
|
|
122
|
+
}
|
|
123
|
+
const words = args.filter((a) => !a.startsWith('-'))
|
|
124
|
+
const file = path.join(process.cwd(), 'avowa.env')
|
|
125
|
+
const pkg = readPkg()
|
|
126
|
+
const scripts = startScripts(pkg)
|
|
127
|
+
const dep = pkg && !('@avowa/runtime' in deps(pkg)) ? 'dependency : run `npm install @avowa/runtime` so your build installs it too' : null
|
|
128
|
+
const node = nodeOk() ? null : `node : ${process.versions.node} has no --import flag (needs 18.18+ or 20.6+). Wrap the client with avowa.instrumentOpenAI(client) instead.`
|
|
129
|
+
|
|
130
|
+
if (dry) {
|
|
131
|
+
console.log('npx @avowa/runtime init --dry-run: nothing is changed and no setup token is used.\n')
|
|
132
|
+
console.log(`credentials : would write ${file} (mode 0600, one line: AVOWA_TOKEN)`)
|
|
133
|
+
for (const l of [gitignore(true), dep, node]) if (l) console.log(l)
|
|
134
|
+
for (const s of scripts) console.log(`package.json : would offer to change "${s.name}"\n from: ${s.from}\n to: ${s.to}`)
|
|
135
|
+
console.log('coverage from package.json:'); console.log(coverage(pkg).join('\n'))
|
|
136
|
+
return 0
|
|
137
|
+
}
|
|
138
|
+
if (!words.length) { console.error(USAGE); return 2 }
|
|
139
|
+
const [, unsafe] = checkedBase(api)
|
|
140
|
+
if (unsafe) { console.error(unsafe.replace('AVOWA_BASE_URL', '--api') + '.'); return 2 }
|
|
141
|
+
const [creds, problem] = await redeem(words[0], api)
|
|
142
|
+
if (problem) { console.error(problem); return 1 }
|
|
143
|
+
const token = makeToken(creds.ingest_token, creds.ingest_secret || '', creds.base_url)
|
|
144
|
+
fs.writeFileSync(file,
|
|
145
|
+
'# Avowa Runtime SDK: the credential for this connection. Keep private, server-side only.\n' +
|
|
146
|
+
'# Written by `npx @avowa/runtime init`. Read from the directory you start your app in.\n' +
|
|
147
|
+
'# In production, put this one value in your secret store as AVOWA_TOKEN instead.\n' +
|
|
148
|
+
`AVOWA_TOKEN=${token}\n`, { mode: 0o600 })
|
|
149
|
+
try { fs.chmodSync(file, 0o600) } catch { /* Windows */ }
|
|
150
|
+
console.log(`connected : ${creds.label || 'Avowa Runtime SDK'} (${creds.environment || 'prod'})`)
|
|
151
|
+
console.log(`credentials : ${file} (mode 0600, one line: AVOWA_TOKEN)`)
|
|
152
|
+
for (const l of [gitignore(false), dep, node]) if (l) console.log(l)
|
|
153
|
+
|
|
154
|
+
let changed = false
|
|
155
|
+
for (const s of scripts) {
|
|
156
|
+
console.log(`package.json : "${s.name}" can load Avowa before your app:\n from: ${s.from}\n to: ${s.to}`)
|
|
157
|
+
if (yes || await ask(' Make this change?')) {
|
|
158
|
+
const raw = fs.readFileSync('package.json', 'utf8')
|
|
159
|
+
const next = raw.replace(JSON.stringify(s.from), JSON.stringify(s.to))
|
|
160
|
+
if (next !== raw) { fs.writeFileSync('package.json', next); changed = true; console.log(' changed') }
|
|
161
|
+
} else console.log(' left as it is')
|
|
162
|
+
}
|
|
163
|
+
console.log('coverage from package.json:'); console.log(coverage(pkg).join('\n'))
|
|
164
|
+
console.log(await reportSetup(creds.ingest_token, creds.ingest_secret || '', creds.base_url, readPkg(), wired(readPkg())))
|
|
165
|
+
console.log('\nDone.' + (changed ? ' Start your app the way you always do' : ` Start your app with NODE_OPTIONS="--import ${REGISTER}" set`) +
|
|
166
|
+
', then send one real request that calls the model.\n' +
|
|
167
|
+
' check : npx @avowa/runtime status\n' +
|
|
168
|
+
` production : set AVOWA_TOKEN (the value in avowa.env) and NODE_OPTIONS=--import ${REGISTER}. Nothing else changes.\n` +
|
|
169
|
+
' off switch : AVOWA_DISABLE=1')
|
|
170
|
+
return 0
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// Tell the server how the install stands, so the console's checklist can tick: the key and secret
|
|
174
|
+
// verify, which covered libraries package.json names, whether the preload is wired. The same
|
|
175
|
+
// signed whoami the doctor makes, with one extra header. No signal is sent.
|
|
176
|
+
async function reportSetup(key, secret, base, pkg, isWired) {
|
|
177
|
+
const d = deps(pkg)
|
|
178
|
+
const covered = ['openai', '@anthropic-ai/sdk'].filter((n) => n in d)
|
|
179
|
+
const ts = String(Math.floor(Date.now() / 1000))
|
|
180
|
+
const headers = { 'X-Ingest-Key': key, 'X-Avowa-Timestamp': ts,
|
|
181
|
+
'X-Avowa-Signature': crypto.createHmac('sha256', secret || '').update(ts + '.').digest('hex'),
|
|
182
|
+
'X-Avowa-Setup': `wired=${isWired ? 1 : 0}; covered=${covered.join(',')}`,
|
|
183
|
+
'User-Agent': `avowa-runtime-js/${VERSION} (+https://avowa.ai)` }
|
|
184
|
+
try {
|
|
185
|
+
const r = await fetch(base + '/ingest/whoami', { headers, signal: AbortSignal.timeout(8000) })
|
|
186
|
+
if (!r.ok) return `server check : could not confirm (HTTP ${r.status}). Run \`npx @avowa/runtime status\` to see why.`
|
|
187
|
+
const who = await r.json()
|
|
188
|
+
if (!who.signature_ok) return 'server check : the secret did NOT verify. Run `npx @avowa/runtime status`.'
|
|
189
|
+
if (!who.capture_allowed) return "server check : key accepted, but this workspace's plan does not allow capture yet"
|
|
190
|
+
return 'server check : key and signature accepted (your console card now shows this step done)'
|
|
191
|
+
} catch (e) { return `server check : could not confirm (${e.name}). Run \`npx @avowa/runtime status\` to see why.` }
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// Asks the server who this key is. Sends no signal: a synthetic one would show on the trust
|
|
195
|
+
// profile as inference that never happened.
|
|
196
|
+
async function doctor() {
|
|
197
|
+
const found = loadEnv()
|
|
198
|
+
const tok = process.env.AVOWA_TOKEN ? readToken(process.env.AVOWA_TOKEN) : null
|
|
199
|
+
const key = process.env.AVOWA_RUNTIME_KEY || (tok && tok[0])
|
|
200
|
+
const secret = process.env.AVOWA_RUNTIME_SECRET || (tok && tok[1])
|
|
201
|
+
const [base, problem] = checkedBase(process.env.AVOWA_BASE_URL || (tok && tok[2]) || undefined)
|
|
202
|
+
console.log(`env file : ${found || 'not found'}`)
|
|
203
|
+
if (process.env.AVOWA_TOKEN && !tok) { console.log('token : AVOWA_TOKEN is not a valid Avowa token (one starts with avw1_)'); return 1 }
|
|
204
|
+
if (problem) { console.log('api : ' + problem); return 1 }
|
|
205
|
+
console.log('api : ' + base)
|
|
206
|
+
if (!key) { console.log('credential : MISSING. Run `npx @avowa/runtime init <setup-token>` or set AVOWA_TOKEN'); return 1 }
|
|
207
|
+
const ts = String(Math.floor(Date.now() / 1000))
|
|
208
|
+
const headers = { 'X-Ingest-Key': key, 'X-Avowa-Timestamp': ts, 'User-Agent': `avowa-runtime-js/${VERSION} (+https://avowa.ai)` }
|
|
209
|
+
if (secret) headers['X-Avowa-Signature'] = crypto.createHmac('sha256', secret).update(ts + '.').digest('hex')
|
|
210
|
+
let who
|
|
211
|
+
try {
|
|
212
|
+
const r = await fetch(base + '/ingest/whoami', { headers, signal: AbortSignal.timeout(8000) })
|
|
213
|
+
if (r.status === 401) { console.log(`credential : REJECTED by ${base}. Not a key this server knows (rotated or revoked?)`); return 1 }
|
|
214
|
+
if (!r.ok) { console.log(`api : ${base} answered HTTP ${r.status}`); return 1 }
|
|
215
|
+
who = await r.json()
|
|
216
|
+
} catch (e) { console.log(`api : NOT reachable from this host (${e.name})`); return 1 }
|
|
217
|
+
let ok = true
|
|
218
|
+
console.log(`connection : ${who.label || who.connection_id} · ${who.environment}`)
|
|
219
|
+
if (who.capture_allowed) console.log('plan : capture allowed')
|
|
220
|
+
else { console.log('plan : capture NOT allowed. The workspace is on Declare; signals will get 402'); ok = false }
|
|
221
|
+
if (!secret) { console.log('secret : not set. Signals would be sent unsigned and refused'); ok = false }
|
|
222
|
+
else if (who.signature_ok) console.log('secret : signs correctly')
|
|
223
|
+
else { console.log('secret : does NOT verify. Every signal would get 401'); ok = false }
|
|
224
|
+
if (typeof who.clock_skew_s === 'number') {
|
|
225
|
+
const s = who.clock_skew_s
|
|
226
|
+
console.log(`clock : ${s >= 0 ? '+' : ''}${Math.round(s)}s vs server${Math.abs(s) < 120 ? '' : '. Over 5 minutes is rejected; fix NTP'}`)
|
|
227
|
+
ok = ok && Math.abs(s) < 300
|
|
228
|
+
}
|
|
229
|
+
console.log(`last event : ${who.last_event_at || 'never'} (${who.events_total || 0} total)`)
|
|
230
|
+
console.log('\nNo signal was sent.' + (ok ? '' : ' Fix the lines marked above, then run again.'))
|
|
231
|
+
return ok ? 0 : 1
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
async function status() {
|
|
235
|
+
const pkg = readPkg()
|
|
236
|
+
console.log(`node : ${process.versions.node}${nodeOk() ? '' : ' (no --import flag: needs 18.18+ or 20.6+)'}`)
|
|
237
|
+
console.log(`preload : ${wired(pkg) ? 'wired (' + REGISTER + ')' : 'NOT wired. Set NODE_OPTIONS="--import ' + REGISTER + '" or run init'}`)
|
|
238
|
+
console.log('coverage from package.json:'); console.log(coverage(pkg).join('\n'))
|
|
239
|
+
loadEnv()
|
|
240
|
+
const tok = process.env.AVOWA_TOKEN ? readToken(process.env.AVOWA_TOKEN) : null
|
|
241
|
+
const key = process.env.AVOWA_RUNTIME_KEY || (tok && tok[0])
|
|
242
|
+
const secret = process.env.AVOWA_RUNTIME_SECRET || (tok && tok[1])
|
|
243
|
+
const [base] = checkedBase(process.env.AVOWA_BASE_URL || (tok && tok[2]) || undefined)
|
|
244
|
+
if (key && secret && base) await reportSetup(key, secret, base, pkg, wired(pkg)) // refreshes the console checklist
|
|
245
|
+
console.log('server check:')
|
|
246
|
+
return doctor()
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
const [cmd, ...rest] = process.argv.slice(2)
|
|
250
|
+
let code
|
|
251
|
+
if (cmd === 'init') code = await init(rest)
|
|
252
|
+
else if (cmd === 'status') code = await status()
|
|
253
|
+
else if (cmd === 'doctor') code = await doctor()
|
|
254
|
+
else { console.error(USAGE); code = cmd && !['-h', '--help', 'help'].includes(cmd) ? 2 : (cmd ? 0 : 2) }
|
|
255
|
+
process.exitCode = code
|
package/env.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// Copyright 2026 Avowa, Inc.
|
|
3
|
+
import fs from 'node:fs';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
const on = v => ['1', 'true', 'yes', 'on'].includes(String(v || '').trim().toLowerCase());
|
|
6
|
+
export function loadEnv(file) {
|
|
7
|
+
if (on(process.env.AVOWA_NO_ENV_FILE)) return null;
|
|
8
|
+
const candidates = [];
|
|
9
|
+
const explicit = file || process.env.AVOWA_ENV_FILE;
|
|
10
|
+
if (explicit) candidates.push(explicit);else {
|
|
11
|
+
let d = process.cwd();
|
|
12
|
+
for (let i = 0; i < 3; i++) {
|
|
13
|
+
candidates.push(path.join(d, 'avowa.env'), path.join(d, '.env'));
|
|
14
|
+
const parent = path.dirname(d);
|
|
15
|
+
if (parent === d) break;
|
|
16
|
+
d = parent;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
for (const cand of candidates) {
|
|
20
|
+
let text;
|
|
21
|
+
try {
|
|
22
|
+
text = fs.readFileSync(cand, 'utf8');
|
|
23
|
+
} catch {
|
|
24
|
+
continue;
|
|
25
|
+
}
|
|
26
|
+
let found = false;
|
|
27
|
+
for (let line of text.split(/\r?\n/)) {
|
|
28
|
+
line = line.trim();
|
|
29
|
+
if (!line || line.startsWith('#')) continue;
|
|
30
|
+
if (line.startsWith('export ')) line = line.slice(7).trimStart();
|
|
31
|
+
const i = line.indexOf('=');
|
|
32
|
+
if (i < 1) continue;
|
|
33
|
+
const key = line.slice(0, i).trim();
|
|
34
|
+
if (!key.startsWith('AVOWA_')) continue;
|
|
35
|
+
const val = line.slice(i + 1).trim().replace(/^["']|["']$/g, '');
|
|
36
|
+
found = true;
|
|
37
|
+
if (process.env[key] === undefined) process.env[key] = val;
|
|
38
|
+
}
|
|
39
|
+
if (found) return cand;
|
|
40
|
+
}
|
|
41
|
+
return null;
|
|
42
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,50 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@avowa/runtime",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.12.0",
|
|
4
|
+
"description": "Avowa Runtime SDK \u2014 emit live AI-inference signals as observed evidence. Metadata only; never prompts or completions.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "avowa-runtime.js",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": "./avowa-runtime.js",
|
|
9
|
+
"./register": "./register.js",
|
|
10
|
+
"./env": "./env.js"
|
|
11
|
+
},
|
|
12
|
+
"files": [
|
|
13
|
+
"avowa-runtime.js",
|
|
14
|
+
"register.js",
|
|
15
|
+
"env.js",
|
|
16
|
+
"cli.js",
|
|
17
|
+
"README.md",
|
|
18
|
+
"LICENSE",
|
|
19
|
+
"NOTICE"
|
|
20
|
+
],
|
|
21
|
+
"keywords": [
|
|
22
|
+
"avowa",
|
|
23
|
+
"ai-trust",
|
|
24
|
+
"evidence",
|
|
25
|
+
"compliance",
|
|
26
|
+
"observability",
|
|
27
|
+
"llm"
|
|
28
|
+
],
|
|
29
|
+
"license": "Apache-2.0",
|
|
30
|
+
"author": "Avowa, Inc.",
|
|
31
|
+
"homepage": "https://avowa.ai",
|
|
32
|
+
"repository": {
|
|
33
|
+
"type": "git",
|
|
34
|
+
"url": "git+https://github.com/Avowa-AI/avowa-runtime-sdk.git"
|
|
35
|
+
},
|
|
36
|
+
"engines": {
|
|
37
|
+
"node": ">=18"
|
|
38
|
+
},
|
|
39
|
+
"dependencies": {},
|
|
40
|
+
"publishConfig": {
|
|
41
|
+
"access": "public",
|
|
42
|
+
"provenance": true
|
|
43
|
+
},
|
|
44
|
+
"bugs": {
|
|
45
|
+
"url": "https://github.com/Avowa-AI/avowa-runtime-sdk/issues"
|
|
46
|
+
},
|
|
47
|
+
"bin": {
|
|
48
|
+
"avowa": "cli.js"
|
|
49
|
+
}
|
|
50
|
+
}
|
package/register.js
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// Copyright 2026 Avowa, Inc.
|
|
3
|
+
import fs from 'node:fs';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import { createRequire } from 'node:module';
|
|
6
|
+
import { pathToFileURL } from 'node:url';
|
|
7
|
+
import { createAvowa } from './avowa-runtime.js';
|
|
8
|
+
import { loadEnv } from './env.js';
|
|
9
|
+
const on = v => ['1', 'true', 'yes', 'on'].includes(String(v || '').trim().toLowerCase());
|
|
10
|
+
const debug = msg => {
|
|
11
|
+
if (on(process.env.AVOWA_DEBUG)) console.error('[avowa:debug] ' + msg);
|
|
12
|
+
};
|
|
13
|
+
const TARGETS = {
|
|
14
|
+
'openai': {
|
|
15
|
+
kind: 'openai',
|
|
16
|
+
root: 'OpenAI',
|
|
17
|
+
methods: [['Chat', 'Completions', 'create'], ['Responses', 'create']]
|
|
18
|
+
},
|
|
19
|
+
'@anthropic-ai/sdk': {
|
|
20
|
+
kind: 'anthropic',
|
|
21
|
+
root: 'Anthropic',
|
|
22
|
+
methods: [['Messages', 'create']]
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
export const instrumented = [];
|
|
26
|
+
let avowa = null;
|
|
27
|
+
function patchModule(name, mod, flavour) {
|
|
28
|
+
const t = TARGETS[name];
|
|
29
|
+
if (!t || !mod || !avowa) return;
|
|
30
|
+
const root = mod[t.root] || mod.default && (mod.default[t.root] || mod.default);
|
|
31
|
+
if (typeof root !== 'function') return;
|
|
32
|
+
for (const p of t.methods) {
|
|
33
|
+
const method = p[p.length - 1];
|
|
34
|
+
let cls = root;
|
|
35
|
+
for (const seg of p.slice(0, -1)) cls = cls && cls[seg];
|
|
36
|
+
const proto = cls && cls.prototype;
|
|
37
|
+
if (!proto || typeof proto[method] !== 'function' || proto[method]._avowa_class_patch) continue;
|
|
38
|
+
const original = proto[method];
|
|
39
|
+
const shim = function (...args) {
|
|
40
|
+
try {
|
|
41
|
+
const own = Object.prototype.hasOwnProperty.call(this, method) ? this[method] : null;
|
|
42
|
+
if (own && own._avowa_wrapped) return original.apply(this, args);
|
|
43
|
+
avowa._wrapResource(this, method, t.kind, original);
|
|
44
|
+
const wrapped = Object.prototype.hasOwnProperty.call(this, method) ? this[method] : null;
|
|
45
|
+
if (wrapped && wrapped._avowa_wrapped) return wrapped.apply(this, args);
|
|
46
|
+
} catch {}
|
|
47
|
+
return original.apply(this, args);
|
|
48
|
+
};
|
|
49
|
+
shim._avowa_class_patch = true;
|
|
50
|
+
try {
|
|
51
|
+
proto[method] = shim;
|
|
52
|
+
const label = `${name} ${p.join('.')} (${flavour})`;
|
|
53
|
+
instrumented.push(label);
|
|
54
|
+
debug('patched ' + label);
|
|
55
|
+
} catch {}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
function esmEntry(cjsEntry, name) {
|
|
59
|
+
let dir = path.dirname(cjsEntry);
|
|
60
|
+
for (let i = 0; i < 6; i++) {
|
|
61
|
+
const pj = path.join(dir, 'package.json');
|
|
62
|
+
if (fs.existsSync(pj)) {
|
|
63
|
+
let pkg;
|
|
64
|
+
try {
|
|
65
|
+
pkg = JSON.parse(fs.readFileSync(pj, 'utf8'));
|
|
66
|
+
} catch {
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
69
|
+
if (pkg.name !== name) {
|
|
70
|
+
dir = path.dirname(dir);
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
const pick = e => {
|
|
74
|
+
if (typeof e === 'string') return e;
|
|
75
|
+
if (!e || typeof e !== 'object') return null;
|
|
76
|
+
return pick(e.import) || pick(e.default);
|
|
77
|
+
};
|
|
78
|
+
const dot = pkg.exports && (pkg.exports['.'] !== undefined ? pkg.exports['.'] : pkg.exports);
|
|
79
|
+
const rel = pick(dot) || pkg.module;
|
|
80
|
+
return rel ? path.join(dir, rel) : null;
|
|
81
|
+
}
|
|
82
|
+
dir = path.dirname(dir);
|
|
83
|
+
}
|
|
84
|
+
return null;
|
|
85
|
+
}
|
|
86
|
+
async function install() {
|
|
87
|
+
if (on(process.env.AVOWA_DISABLE)) {
|
|
88
|
+
debug('AVOWA_DISABLE is set: nothing is patched');
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
const found = loadEnv();
|
|
92
|
+
avowa = createAvowa({
|
|
93
|
+
region: process.env.AVOWA_REGION
|
|
94
|
+
});
|
|
95
|
+
debug(`@avowa/runtime/register active · env file ${found || 'none'}`);
|
|
96
|
+
const nodeModule = createRequire(import.meta.url)('node:module');
|
|
97
|
+
const load = nodeModule._load;
|
|
98
|
+
nodeModule._load = function (request) {
|
|
99
|
+
const exp = load.apply(this, arguments);
|
|
100
|
+
if (Object.prototype.hasOwnProperty.call(TARGETS, request)) {
|
|
101
|
+
try {
|
|
102
|
+
patchModule(request, exp, 'require');
|
|
103
|
+
} catch {}
|
|
104
|
+
}
|
|
105
|
+
return exp;
|
|
106
|
+
};
|
|
107
|
+
const roots = [];
|
|
108
|
+
if (process.argv[1]) roots.push(path.dirname(path.resolve(process.argv[1])));
|
|
109
|
+
roots.push(process.cwd());
|
|
110
|
+
for (const name of Object.keys(TARGETS)) {
|
|
111
|
+
for (const root of roots) {
|
|
112
|
+
let cjs;
|
|
113
|
+
try {
|
|
114
|
+
cjs = createRequire(path.join(root, 'noop.js')).resolve(name);
|
|
115
|
+
} catch {
|
|
116
|
+
continue;
|
|
117
|
+
}
|
|
118
|
+
const esm = esmEntry(cjs, name);
|
|
119
|
+
if (esm && esm !== cjs && fs.existsSync(esm)) {
|
|
120
|
+
try {
|
|
121
|
+
patchModule(name, await import(pathToFileURL(esm).href), 'import');
|
|
122
|
+
} catch {}
|
|
123
|
+
}
|
|
124
|
+
break;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
try {
|
|
129
|
+
await install();
|
|
130
|
+
} catch {}
|