@cultured_computer/sight 0.5.1 → 0.7.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/README.md +151 -44
- package/dist/cli.js +204 -7
- package/dist/index.cjs +692 -41
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +863 -29
- package/dist/index.d.ts +863 -29
- package/dist/index.js +681 -42
- package/dist/index.js.map +1 -1
- package/package.json +14 -15
package/README.md
CHANGED
|
@@ -28,22 +28,22 @@ around it are still moving.
|
|
|
28
28
|
npm install @cultured_computer/sight
|
|
29
29
|
```
|
|
30
30
|
|
|
31
|
-
Node
|
|
31
|
+
Node 20.6 or later. TypeScript types ship with the package.
|
|
32
32
|
|
|
33
33
|
## Quickstart
|
|
34
34
|
|
|
35
35
|
```ts
|
|
36
|
-
import { Sight } from
|
|
36
|
+
import { Sight } from "@cultured_computer/sight";
|
|
37
37
|
|
|
38
38
|
Sight.init({
|
|
39
39
|
apiKey: process.env.SIGHT_API_KEY!,
|
|
40
|
-
deployment:
|
|
41
|
-
})
|
|
40
|
+
deployment: "my-assistant",
|
|
41
|
+
});
|
|
42
42
|
|
|
43
43
|
Sight.startSession({
|
|
44
44
|
externalId: conversationId,
|
|
45
45
|
externalUserId: userId,
|
|
46
|
-
})
|
|
46
|
+
});
|
|
47
47
|
```
|
|
48
48
|
|
|
49
49
|
Notes:
|
|
@@ -59,28 +59,52 @@ Notes:
|
|
|
59
59
|
stable, pseudonymous user id you supply. Return metrics key on the user id;
|
|
60
60
|
sessions without it still capture, but return behaviour is not computable.
|
|
61
61
|
|
|
62
|
-
##
|
|
62
|
+
## Headless project setup
|
|
63
63
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
64
|
+
Provider setup requires an explicitly issued **management** provisioning token
|
|
65
|
+
(`--access manage`), not an ordinary ingest, Studio, or combined token. It grants
|
|
66
|
+
only `providers:write`, allowing provider-key replacement and budget changes.
|
|
67
|
+
With that administrative token, connect your own OpenRouter key without a browser:
|
|
68
68
|
|
|
69
69
|
```bash
|
|
70
|
-
|
|
70
|
+
export SIGHT_PROVISION_TOKEN='<one-time management token from Sight>'
|
|
71
|
+
export OPENROUTER_API_KEY='<your OpenRouter API key>'
|
|
72
|
+
|
|
73
|
+
npx @cultured_computer/sight account create --openrouter --budget 25
|
|
71
74
|
```
|
|
72
75
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
76
|
+
The provisioning token and OpenRouter key are read only from the environment,
|
|
77
|
+
not command-line arguments. The Sight key is returned once and saved to
|
|
78
|
+
`~/.config/sight/machine.json` with mode `0600`; it is never printed. Sight
|
|
79
|
+
validates the OpenRouter key, seals it before database storage, and records the
|
|
80
|
+
explicit local monthly budget. Check the safe, redacted state later with:
|
|
78
81
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
82
|
+
```bash
|
|
83
|
+
npx @cultured_computer/sight openrouter status
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
If OpenRouter setup fails after account creation, the Sight credential remains
|
|
87
|
+
saved. Fix the key or budget and retry only that step:
|
|
88
|
+
|
|
89
|
+
```bash
|
|
90
|
+
export OPENROUTER_API_KEY='<your OpenRouter API key>'
|
|
91
|
+
npx @cultured_computer/sight openrouter connect --budget 25
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
For an existing project, ask its owner for a separate **Provider administration
|
|
95
|
+
only** key, or have the operator mint one with `scripts/mint-key.mjs --access manage`.
|
|
96
|
+
Set it as `SIGHT_API_KEY` for `openrouter connect`. Use a separate `studio` or
|
|
97
|
+
`both` key for execution; management keys do not authorize runs or ingest.
|
|
98
|
+
Existing tokens and keys are never upgraded. An ordinary provisioning token
|
|
99
|
+
can still be redeemed with `account create` without `--openrouter`.
|
|
100
|
+
Redemption is one-time for the project, so a second provisioning token cannot
|
|
101
|
+
add an execution key after admin provisioning. The owner must mint the separate
|
|
102
|
+
ingest/Studio key in API Keys after claiming the project, or the operator must
|
|
103
|
+
mint it with `scripts/mint-key.mjs --access ingest`, `studio`, or `both`.
|
|
104
|
+
|
|
105
|
+
Use `SIGHT_API_KEY` from your secret manager in the application process. The
|
|
106
|
+
local `machine.json` is a CLI credential store, not a project `.env` file and
|
|
107
|
+
is never copied into your repository automatically.
|
|
84
108
|
|
|
85
109
|
## Pull your own data out: the CLI, from your terminal
|
|
86
110
|
|
|
@@ -96,6 +120,9 @@ npx @cultured_computer/sight auth login
|
|
|
96
120
|
# then pull a deployment's full record
|
|
97
121
|
npx @cultured_computer/sight export --project <slug> --deployment <slug> --out data.jsonl
|
|
98
122
|
|
|
123
|
+
# or pull the readings: the per-turn judge labels (closure, backward-pass, ...)
|
|
124
|
+
npx @cultured_computer/sight export --project <slug> --deployment <slug> --readings --out readings.jsonl
|
|
125
|
+
|
|
99
126
|
# sign out when you're done
|
|
100
127
|
npx @cultured_computer/sight auth logout
|
|
101
128
|
```
|
|
@@ -109,15 +136,56 @@ npx @cultured_computer/sight auth logout
|
|
|
109
136
|
Tier-2 redacted shape the dashboard produces. It is owner-only: the token is
|
|
110
137
|
your session, so the carry-out gate is exactly the dashboard's. Members read
|
|
111
138
|
in-product; owners carry the dataset out.
|
|
139
|
+
- `--readings` pulls the graded reads instead of the record: one line per
|
|
140
|
+
per-turn judge label (closure, backward-pass, and the rest), with the rubric
|
|
141
|
+
version and judge-model pin. These are the re-computable evidence layer over
|
|
142
|
+
the record, not part of the versioned record schema.
|
|
112
143
|
- `--out <file>` writes to a file; omit it to stream to stdout.
|
|
113
144
|
|
|
145
|
+
One more CLI door, for a different shape: `sight connect` attaches Sight to a
|
|
146
|
+
buzz community you don't serve as a read-only observer (invite-code
|
|
147
|
+
authorization, no account needed up front). A side lane, not the install —
|
|
148
|
+
the walkthrough lives at
|
|
149
|
+
[Connect your buzz community](https://sight.cultured.computer/docs/connect-your-room).
|
|
150
|
+
|
|
114
151
|
## What lands automatically
|
|
115
152
|
|
|
116
|
-
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
153
|
+
- **OpenAI: wrap your client.** This is the reliable autocapture path and
|
|
154
|
+
the one to use in every new integration:
|
|
155
|
+
|
|
156
|
+
```ts
|
|
157
|
+
import OpenAI from "openai";
|
|
158
|
+
import { Sight, wrapOpenAI } from "@cultured_computer/sight";
|
|
159
|
+
|
|
160
|
+
const openai = wrapOpenAI(new OpenAI());
|
|
161
|
+
// Wrap before or after Sight.init(); order does not matter.
|
|
162
|
+
|
|
163
|
+
// Calls are captured within a session scope: wrapOpenAI reads the session
|
|
164
|
+
// from withSession's async context. Every chat.completions.create and
|
|
165
|
+
// responses.create made inside the scope is captured, streaming included.
|
|
166
|
+
await Sight.withSession(conversationId, async () => {
|
|
167
|
+
const res = await openai.chat.completions.create({
|
|
168
|
+
model: "gpt-4.1",
|
|
169
|
+
messages: [{ role: "user", content: userMessage }],
|
|
170
|
+
});
|
|
171
|
+
});
|
|
172
|
+
// A call made outside any session scope is skipped — with a one-line
|
|
173
|
+
// warning naming withSession, never a silent drop.
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
Module-level autocapture (no wrap call) also exists but has hard limits:
|
|
177
|
+
it attaches only under CommonJS, only when `openai` loads after `init`,
|
|
178
|
+
and only for openai v4 (the underlying instrumentation does not support
|
|
179
|
+
v5+). When it cannot attach, the SDK now prints a warning naming
|
|
180
|
+
`wrapOpenAI` instead of failing silently. If you run ESM or openai v5+,
|
|
181
|
+
wrap the client.
|
|
182
|
+
|
|
183
|
+
- Module-level autocapture is on from `init` for calls made through the
|
|
184
|
+
Anthropic SDK, Vercel AI SDK, LangChain, and LangGraph in the process,
|
|
185
|
+
with the same CommonJS and load-order caveats. Each adapter no-ops if
|
|
186
|
+
its target library is not installed. Mercury-2 reached through the
|
|
187
|
+
OpenAI SDK with a baseURL override is captured and tagged by provider;
|
|
188
|
+
`wrapOpenAI` applies there too.
|
|
121
189
|
- Plumbing fields populate with no further configuration: model, tokens,
|
|
122
190
|
latency, cost, tool calls, finish reason.
|
|
123
191
|
- Turns are grouped into sessions by the ids you supply; sessions are created
|
|
@@ -134,15 +202,15 @@ even hours later, from a webhook or CRM callback.
|
|
|
134
202
|
// A user reporting a persona break.
|
|
135
203
|
await Sight.flagPersonaBreak({
|
|
136
204
|
sessionExternalId: sessionId,
|
|
137
|
-
note:
|
|
138
|
-
})
|
|
205
|
+
note: "sounded robotic",
|
|
206
|
+
});
|
|
139
207
|
|
|
140
208
|
// Your own downstream outcome: a resolved ticket, a rating, a conversion.
|
|
141
209
|
await Sight.emitOutcomeEvent({
|
|
142
210
|
sessionExternalId: sessionId,
|
|
143
|
-
kind:
|
|
144
|
-
detail: { ticketId:
|
|
145
|
-
})
|
|
211
|
+
kind: "ticket_resolved",
|
|
212
|
+
detail: { ticketId: "ZD-88213" },
|
|
213
|
+
});
|
|
146
214
|
```
|
|
147
215
|
|
|
148
216
|
## Capture environment events
|
|
@@ -153,10 +221,10 @@ against the conversation:
|
|
|
153
221
|
|
|
154
222
|
```ts
|
|
155
223
|
await Sight.captureEnvironmentEvents(events, {
|
|
156
|
-
clock_source:
|
|
224
|
+
clock_source: "my-event-clock",
|
|
157
225
|
skew_bound_ms: 25,
|
|
158
|
-
rule_version:
|
|
159
|
-
})
|
|
226
|
+
rule_version: "v1",
|
|
227
|
+
});
|
|
160
228
|
```
|
|
161
229
|
|
|
162
230
|
The envelope is generalized: `source_type` and `event` are free strings whose
|
|
@@ -174,26 +242,60 @@ steps, per-call model usage inside a single turn) can ship it as spans:
|
|
|
174
242
|
await Sight.captureSpans(
|
|
175
243
|
[
|
|
176
244
|
{
|
|
177
|
-
|
|
245
|
+
contractVersion: 2,
|
|
246
|
+
deploymentSlug: "my-agent",
|
|
178
247
|
sessionExternalId: sessionId,
|
|
179
248
|
turnIndex: 3,
|
|
180
|
-
spanType:
|
|
181
|
-
|
|
182
|
-
|
|
249
|
+
spanType: "tool_call",
|
|
250
|
+
spanId: "lookup-3",
|
|
251
|
+
parentSpanId: null,
|
|
252
|
+
name: "lookup_order",
|
|
253
|
+
status: "ok",
|
|
254
|
+
startedAt: "2026-07-22T00:00:01.200Z",
|
|
255
|
+
endedAt: "2026-07-22T00:00:01.380Z",
|
|
183
256
|
durationMs: 180,
|
|
184
|
-
|
|
257
|
+
model: null,
|
|
258
|
+
provider: null,
|
|
259
|
+
tokensInput: null,
|
|
260
|
+
tokensOutput: null,
|
|
261
|
+
costUsd: null,
|
|
262
|
+
error: null,
|
|
263
|
+
input: { state: "captured", value: { orderId: "ord_123" } },
|
|
264
|
+
output: { state: "captured", value: { status: "shipped" } },
|
|
265
|
+
intervals: [
|
|
266
|
+
{
|
|
267
|
+
kind: "active",
|
|
268
|
+
startedAt: "2026-07-22T00:00:01.200Z",
|
|
269
|
+
endedAt: "2026-07-22T00:00:01.280Z",
|
|
270
|
+
},
|
|
271
|
+
{
|
|
272
|
+
kind: "waiting",
|
|
273
|
+
startedAt: "2026-07-22T00:00:01.280Z",
|
|
274
|
+
endedAt: "2026-07-22T00:00:01.380Z",
|
|
275
|
+
},
|
|
276
|
+
],
|
|
277
|
+
attributes: { toolName: "lookup_order", argKeys: ["orderId"] },
|
|
185
278
|
},
|
|
186
279
|
],
|
|
187
|
-
{ clock_source:
|
|
188
|
-
)
|
|
280
|
+
{ clock_source: "my-agent-clock", skew_bound_ms: 25, rule_version: "v1" },
|
|
281
|
+
);
|
|
189
282
|
```
|
|
190
283
|
|
|
191
284
|
A span lands on the turn you name, or on the session's latest turn at/under
|
|
192
285
|
its `startedAt` when `turnIndex` is omitted. Parent structure rides the
|
|
193
286
|
batch: give a span a caller-local `spanId` and reference it from a sibling's
|
|
194
|
-
`parentSpanId`.
|
|
195
|
-
|
|
196
|
-
|
|
287
|
+
`parentSpanId`. In contract v2 every operational key is present; use `null`
|
|
288
|
+
for an unknown model, token count, cost, end time, or parent. Input and output
|
|
289
|
+
use explicit `captured`, `empty`, `not_captured`, `redacted`, or `unavailable`
|
|
290
|
+
states, so absence is never guessed from attributes. `attributes` are metadata
|
|
291
|
+
only. Optional `intervals` record internal `active`, `waiting`, `retry`, and
|
|
292
|
+
`error` windows. Every interval requires its own `startedAt` and `endedAt` and
|
|
293
|
+
must stay within the span; when intervals are omitted, the waterfall does not
|
|
294
|
+
invent them from status, names, or retry counters. Status is always explicit
|
|
295
|
+
`ok` or `error`; the historical `unset` state
|
|
296
|
+
is reserved for contract-v1 rows. The unversioned v1 shape remains accepted for older SDK callers and is
|
|
297
|
+
read through a legacy adapter. `Sight.captureSpan(span)` is the single-span
|
|
298
|
+
form.
|
|
197
299
|
|
|
198
300
|
## Scope
|
|
199
301
|
|
|
@@ -226,6 +328,11 @@ consequences. See [Data format](https://sight.cultured.computer/docs/data-format
|
|
|
226
328
|
- [Status](https://sight.cultured.computer/docs/where-it-stands): what
|
|
227
329
|
is available now versus in beta.
|
|
228
330
|
|
|
331
|
+
## Legal
|
|
332
|
+
|
|
333
|
+
[Privacy Policy](https://sight.cultured.computer/privacy) and
|
|
334
|
+
[Terms of Service](https://sight.cultured.computer/terms).
|
|
335
|
+
|
|
229
336
|
## License
|
|
230
337
|
|
|
231
338
|
MIT (c) Cultured Computer
|
package/dist/cli.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
// src/cli.ts
|
|
4
4
|
import { exec } from "child_process";
|
|
5
|
-
import { chmodSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "fs";
|
|
5
|
+
import { chmodSync, mkdirSync, mkdtempSync, readFileSync, renameSync, rmSync, writeFileSync } from "fs";
|
|
6
6
|
import { homedir } from "os";
|
|
7
7
|
import { dirname, join } from "path";
|
|
8
8
|
var DEFAULT_API = "https://fieldwork-ingest.fly.dev";
|
|
@@ -19,7 +19,11 @@ function usage() {
|
|
|
19
19
|
" sight connect <relay-url> --invite <code> [--pulse] [--api <base>]",
|
|
20
20
|
" sight auth login [--base <dashboard-url>]",
|
|
21
21
|
" sight auth logout",
|
|
22
|
-
" sight
|
|
22
|
+
" sight account create [--openrouter --budget <usd>] [--api <base>]",
|
|
23
|
+
" sight openrouter connect --budget <usd> [--api <base>]",
|
|
24
|
+
" sight openrouter status [--api <base>]",
|
|
25
|
+
" sight export --project <slug> --deployment <slug> [--readings|--conditions] [--out file.jsonl]",
|
|
26
|
+
" sight conditions --session <id-or-external-id> [--run <id>] [--since <iso>] [--until <iso>] [--base <dashboard-url>]",
|
|
23
27
|
"",
|
|
24
28
|
" connect attach Sight to a buzz community as a read-only observer.",
|
|
25
29
|
" --invite one single-use invite code, minted by you on",
|
|
@@ -33,8 +37,21 @@ function usage() {
|
|
|
33
37
|
" auth login sign this terminal in with your browser session \u2014 a",
|
|
34
38
|
" code prints, you approve on the dashboard, done. The",
|
|
35
39
|
" token lives at ~/.config/sight/credentials.json (0600).",
|
|
40
|
+
" account create redeem SIGHT_PROVISION_TOKEN without a browser and",
|
|
41
|
+
" save the scoped Sight key in machine.json (0600). Add",
|
|
42
|
+
" --openrouter to validate + seal OPENROUTER_API_KEY.",
|
|
43
|
+
" openrouter connect attach/rotate your BYOK key; requires a positive",
|
|
44
|
+
" local monthly --budget and a providers:write Sight key.",
|
|
36
45
|
" export pull a deployment\u2019s full record (RLDS JSONL) with that",
|
|
37
46
|
" token. Owner role on the project required.",
|
|
47
|
+
" --readings pull the per-turn judge labels (the graded",
|
|
48
|
+
" reads: closure, backward-pass, and the rest)",
|
|
49
|
+
" instead of the record.",
|
|
50
|
+
" --conditions pull the detector checks: each checked",
|
|
51
|
+
" sentence, flagged / not flagged / not checked.",
|
|
52
|
+
" conditions pull one session\u2019s (or one run\u2019s) detector checks with",
|
|
53
|
+
" the machine key from ~/.config/sight/machine.json \u2014",
|
|
54
|
+
" NDJSON to stdout, errors to stderr.",
|
|
38
55
|
"",
|
|
39
56
|
"The SDK half of this package (serving-path capture) is documented at",
|
|
40
57
|
"https://sight.cultured.computer/docs (the two lanes share one project)."
|
|
@@ -91,18 +108,24 @@ async function connect(args) {
|
|
|
91
108
|
console.log(` ${body["claimUrl"]}`);
|
|
92
109
|
return 0;
|
|
93
110
|
}
|
|
111
|
+
var DEFAULT_DASHBOARD = "https://sight.cultured.computer";
|
|
94
112
|
var [, , command, ...rest] = process.argv;
|
|
95
113
|
if (command === "connect") {
|
|
96
114
|
connect(rest).then((code) => process.exit(code));
|
|
97
115
|
} else if (command === "auth") {
|
|
98
116
|
auth(rest).then((code) => process.exit(code));
|
|
117
|
+
} else if (command === "account") {
|
|
118
|
+
account(rest).then((code) => process.exit(code));
|
|
119
|
+
} else if (command === "openrouter") {
|
|
120
|
+
openrouter(rest).then((code) => process.exit(code));
|
|
99
121
|
} else if (command === "export") {
|
|
100
122
|
exportRecord(rest).then((code) => process.exit(code));
|
|
123
|
+
} else if (command === "conditions") {
|
|
124
|
+
conditions(rest).then((code) => process.exit(code));
|
|
101
125
|
} else {
|
|
102
126
|
usage();
|
|
103
127
|
process.exit(command && command !== "--help" && command !== "-h" ? 1 : 0);
|
|
104
128
|
}
|
|
105
|
-
var DEFAULT_DASHBOARD = "https://sight.cultured.computer";
|
|
106
129
|
var CLIENT_ID = "sight-cli";
|
|
107
130
|
function credentialsPath() {
|
|
108
131
|
return join(process.env["XDG_CONFIG_HOME"] ?? join(homedir(), ".config"), "sight", "credentials.json");
|
|
@@ -122,6 +145,142 @@ function writeCredentials(token, base) {
|
|
|
122
145
|
});
|
|
123
146
|
chmodSync(p, 384);
|
|
124
147
|
}
|
|
148
|
+
function machineCredentialsPath() {
|
|
149
|
+
return join(process.env["XDG_CONFIG_HOME"] ?? join(homedir(), ".config"), "sight", "machine.json");
|
|
150
|
+
}
|
|
151
|
+
function readMachineCredentials() {
|
|
152
|
+
try {
|
|
153
|
+
const value = JSON.parse(readFileSync(machineCredentialsPath(), "utf8"));
|
|
154
|
+
return value.apiKey && value.api ? value : null;
|
|
155
|
+
} catch {
|
|
156
|
+
return null;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
function writeMachineCredentials(value) {
|
|
160
|
+
const path = machineCredentialsPath();
|
|
161
|
+
mkdirSync(dirname(path), { recursive: true, mode: 448 });
|
|
162
|
+
const temporary = mkdtempSync(join(dirname(path), ".machine-"));
|
|
163
|
+
try {
|
|
164
|
+
const pending = join(temporary, "machine.json");
|
|
165
|
+
writeFileSync(pending, JSON.stringify({ ...value, savedAt: (/* @__PURE__ */ new Date()).toISOString() }, null, 2) + "\n", {
|
|
166
|
+
mode: 384
|
|
167
|
+
});
|
|
168
|
+
renameSync(pending, path);
|
|
169
|
+
} finally {
|
|
170
|
+
rmSync(temporary, { recursive: true, force: true });
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
function positiveBudget(args) {
|
|
174
|
+
const raw = flag(args, "--budget");
|
|
175
|
+
if (!raw) return null;
|
|
176
|
+
const value = Number(raw);
|
|
177
|
+
return Number.isFinite(value) && value > 0 && value <= 1e5 ? value : null;
|
|
178
|
+
}
|
|
179
|
+
async function connectOpenRouter(api, sightKey, args) {
|
|
180
|
+
const openRouterKey = process.env["OPENROUTER_API_KEY"];
|
|
181
|
+
if (!openRouterKey) {
|
|
182
|
+
console.error("OPENROUTER_API_KEY is required; it is read from the environment and never printed.");
|
|
183
|
+
return 1;
|
|
184
|
+
}
|
|
185
|
+
const budget = positiveBudget(args);
|
|
186
|
+
if (budget === null) {
|
|
187
|
+
console.error("--budget must be a positive monthly USD limit (maximum 100000).");
|
|
188
|
+
return 1;
|
|
189
|
+
}
|
|
190
|
+
const response = await fetch(`${api}/v1/provider-connections/openrouter`, {
|
|
191
|
+
method: "POST",
|
|
192
|
+
redirect: "error",
|
|
193
|
+
headers: {
|
|
194
|
+
authorization: `Bearer ${sightKey}`,
|
|
195
|
+
"content-type": "application/json"
|
|
196
|
+
},
|
|
197
|
+
body: JSON.stringify({ apiKey: openRouterKey, monthlyBudgetUsd: budget })
|
|
198
|
+
}).catch((_error) => ({ __error: _error }));
|
|
199
|
+
if (!response.ok) {
|
|
200
|
+
const body2 = response instanceof Response ? await response.json().catch(() => ({})) : {};
|
|
201
|
+
const status = response instanceof Response ? response.status : "unreachable";
|
|
202
|
+
console.error(`OpenRouter connect failed (${status}): ${body2["error"] ?? "network error"}`);
|
|
203
|
+
return 1;
|
|
204
|
+
}
|
|
205
|
+
const body = await response.json();
|
|
206
|
+
console.log(`OpenRouter connected (\u2026${String(body["last4"] ?? "")}); local monthly budget: $${budget}.`);
|
|
207
|
+
return 0;
|
|
208
|
+
}
|
|
209
|
+
async function account(args) {
|
|
210
|
+
if (args[0] !== "create") {
|
|
211
|
+
console.error("usage: sight account create [--openrouter --budget <usd>] [--api <base>]");
|
|
212
|
+
return 1;
|
|
213
|
+
}
|
|
214
|
+
const token = process.env["SIGHT_PROVISION_TOKEN"];
|
|
215
|
+
if (!token) {
|
|
216
|
+
console.error("SIGHT_PROVISION_TOKEN is required; keep it in the environment, not a command-line flag.");
|
|
217
|
+
return 1;
|
|
218
|
+
}
|
|
219
|
+
const api = (flag(args, "--api") ?? process.env["SIGHT_API"] ?? DEFAULT_API).replace(/\/+$/, "");
|
|
220
|
+
const response = await fetch(`${api}/v1/provision/redeem`, {
|
|
221
|
+
method: "POST",
|
|
222
|
+
redirect: "error",
|
|
223
|
+
headers: { "content-type": "application/json" },
|
|
224
|
+
body: JSON.stringify({ token })
|
|
225
|
+
}).catch((_error) => ({ __error: _error }));
|
|
226
|
+
if (!response.ok) {
|
|
227
|
+
const body2 = response instanceof Response ? await response.json().catch(() => ({})) : {};
|
|
228
|
+
const status = response instanceof Response ? response.status : "unreachable";
|
|
229
|
+
console.error(`account create failed (${status}): ${body2["error"] ?? "network error"}`);
|
|
230
|
+
return 1;
|
|
231
|
+
}
|
|
232
|
+
const body = await response.json();
|
|
233
|
+
if (!body.key || !body.project || !Array.isArray(body.capabilities)) {
|
|
234
|
+
console.error("account create failed: the server returned an incomplete credential.");
|
|
235
|
+
return 1;
|
|
236
|
+
}
|
|
237
|
+
writeMachineCredentials({
|
|
238
|
+
apiKey: body.key,
|
|
239
|
+
api,
|
|
240
|
+
project: body.project,
|
|
241
|
+
tier: body.tier ?? "research",
|
|
242
|
+
capabilities: body.capabilities
|
|
243
|
+
});
|
|
244
|
+
console.log(`Sight account ready for project ${body.project}; key saved at ${machineCredentialsPath()} (0600).`);
|
|
245
|
+
console.log(`capabilities: ${body.capabilities.join(", ") || "none"}`);
|
|
246
|
+
if (!args.includes("--openrouter")) return 0;
|
|
247
|
+
if (!body.capabilities.includes("providers:write")) {
|
|
248
|
+
console.error("The Sight key was saved, but provider setup requires providers:write. Ask the owner or operator for a separate management key.");
|
|
249
|
+
return 1;
|
|
250
|
+
}
|
|
251
|
+
const connected = await connectOpenRouter(api, body.key, args);
|
|
252
|
+
if (connected !== 0) {
|
|
253
|
+
console.error("The Sight key was saved. Fix the OpenRouter input and retry: sight openrouter connect --budget <usd>");
|
|
254
|
+
}
|
|
255
|
+
return connected;
|
|
256
|
+
}
|
|
257
|
+
async function openrouter(args) {
|
|
258
|
+
const sub = args[0];
|
|
259
|
+
if (sub !== "connect" && sub !== "status") {
|
|
260
|
+
console.error("usage: sight openrouter connect --budget <usd> [--api <base>] \xB7 sight openrouter status");
|
|
261
|
+
return 1;
|
|
262
|
+
}
|
|
263
|
+
const stored = readMachineCredentials();
|
|
264
|
+
const sightKey = process.env["SIGHT_API_KEY"] ?? stored?.apiKey;
|
|
265
|
+
const api = (flag(args, "--api") ?? process.env["SIGHT_API"] ?? stored?.api ?? DEFAULT_API).replace(/\/+$/, "");
|
|
266
|
+
if (!sightKey) {
|
|
267
|
+
console.error("No machine credential. Run sight account create or set SIGHT_API_KEY.");
|
|
268
|
+
return 1;
|
|
269
|
+
}
|
|
270
|
+
if (sub === "connect") return connectOpenRouter(api, sightKey, args);
|
|
271
|
+
const response = await fetch(`${api}/v1/provider-connections/openrouter`, {
|
|
272
|
+
redirect: "error",
|
|
273
|
+
headers: { authorization: `Bearer ${sightKey}` }
|
|
274
|
+
}).catch(() => null);
|
|
275
|
+
if (!response?.ok) {
|
|
276
|
+
const body2 = response ? await response.json().catch(() => ({})) : {};
|
|
277
|
+
console.error(`OpenRouter status failed (${response?.status ?? "unreachable"}): ${body2["error"] ?? body2["status"] ?? "network error"}`);
|
|
278
|
+
return 1;
|
|
279
|
+
}
|
|
280
|
+
const body = await response.json();
|
|
281
|
+
console.log(`OpenRouter ${body["status"]} (\u2026${String(body["last4"] ?? "")}); monthly budget: $${body["monthlyBudgetUsd"]}.`);
|
|
282
|
+
return 0;
|
|
283
|
+
}
|
|
125
284
|
function openBrowser(url) {
|
|
126
285
|
const cmd = process.platform === "darwin" ? "open" : process.platform === "win32" ? "start" : "xdg-open";
|
|
127
286
|
exec(`${cmd} "${url}"`);
|
|
@@ -204,8 +363,10 @@ async function exportRecord(args) {
|
|
|
204
363
|
const project = flag(args, "--project");
|
|
205
364
|
const deployment = flag(args, "--deployment");
|
|
206
365
|
const out = flag(args, "--out") ?? null;
|
|
207
|
-
|
|
208
|
-
|
|
366
|
+
const readings = args.includes("--readings");
|
|
367
|
+
const conditions2 = args.includes("--conditions");
|
|
368
|
+
if (!project || !deployment || readings && conditions2) {
|
|
369
|
+
console.error("usage: sight export --project <slug> --deployment <slug> [--readings|--conditions] [--out file.jsonl]");
|
|
209
370
|
return 1;
|
|
210
371
|
}
|
|
211
372
|
const creds = readCredentials();
|
|
@@ -213,7 +374,8 @@ async function exportRecord(args) {
|
|
|
213
374
|
console.error("no stored login \u2014 run sight auth login first.");
|
|
214
375
|
return 1;
|
|
215
376
|
}
|
|
216
|
-
const
|
|
377
|
+
const kind = conditions2 ? "conditions" : readings ? "readings" : null;
|
|
378
|
+
const url = `${creds.base}/api/export?project=${encodeURIComponent(project)}&deployment=${encodeURIComponent(deployment)}` + (kind ? `&kind=${kind}` : "");
|
|
217
379
|
const res = await fetch(url, {
|
|
218
380
|
headers: { authorization: `Bearer ${creds.token}` }
|
|
219
381
|
}).catch((err) => ({ __err: err }));
|
|
@@ -227,9 +389,44 @@ async function exportRecord(args) {
|
|
|
227
389
|
const text = await res.text();
|
|
228
390
|
if (out) {
|
|
229
391
|
writeFileSync(out, text);
|
|
230
|
-
console.log(
|
|
392
|
+
console.log(
|
|
393
|
+
`exported to ${out} (${text.split("\n").filter(Boolean).length} ${kind ?? "episodes"}).`
|
|
394
|
+
);
|
|
231
395
|
} else {
|
|
232
396
|
process.stdout.write(text);
|
|
233
397
|
}
|
|
234
398
|
return 0;
|
|
235
399
|
}
|
|
400
|
+
async function conditions(args) {
|
|
401
|
+
const session = flag(args, "--session");
|
|
402
|
+
const run = flag(args, "--run");
|
|
403
|
+
if (!session && !run) {
|
|
404
|
+
console.error("usage: sight conditions --session <id-or-external-id> [--run <id>] [--since <iso>] [--until <iso>] [--base <dashboard-url>]");
|
|
405
|
+
return 1;
|
|
406
|
+
}
|
|
407
|
+
const stored = readMachineCredentials();
|
|
408
|
+
const sightKey = process.env["SIGHT_API_KEY"] ?? stored?.apiKey;
|
|
409
|
+
const base = (flag(args, "--base") ?? process.env["SIGHT_BASE"] ?? DEFAULT_DASHBOARD).replace(/\/+$/, "");
|
|
410
|
+
if (!sightKey) {
|
|
411
|
+
console.error("No machine credential. Run sight account create or set SIGHT_API_KEY.");
|
|
412
|
+
return 1;
|
|
413
|
+
}
|
|
414
|
+
const params = new URLSearchParams();
|
|
415
|
+
if (session) params.set("session", session);
|
|
416
|
+
if (run) params.set("run", run);
|
|
417
|
+
const since = flag(args, "--since");
|
|
418
|
+
if (since) params.set("since", since);
|
|
419
|
+
const until = flag(args, "--until");
|
|
420
|
+
if (until) params.set("until", until);
|
|
421
|
+
const res = await fetch(`${base}/api/studio/conditions?${params.toString()}`, {
|
|
422
|
+
redirect: "error",
|
|
423
|
+
headers: { authorization: `Bearer ${sightKey}` }
|
|
424
|
+
}).catch(() => null);
|
|
425
|
+
if (!res?.ok) {
|
|
426
|
+
const body = res ? await res.json().catch(() => ({})) : {};
|
|
427
|
+
console.error(`conditions failed (${res?.status ?? "unreachable"}): ${body["error"] ?? "network error"}`);
|
|
428
|
+
return 1;
|
|
429
|
+
}
|
|
430
|
+
process.stdout.write(await res.text());
|
|
431
|
+
return 0;
|
|
432
|
+
}
|