@cultured_computer/sight 0.6.0 → 0.8.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/CHANGELOG.md +257 -0
- package/README.md +482 -88
- package/dist/cli.js +242 -27
- package/dist/index.cjs +1470 -224
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1034 -112
- package/dist/index.d.ts +1034 -112
- package/dist/index.js +1459 -226
- package/dist/index.js.map +1 -1
- package/package.json +17 -14
package/README.md
CHANGED
|
@@ -1,26 +1,11 @@
|
|
|
1
1
|
# Sight
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
The record also carries an environment channel: tool outcomes, world
|
|
11
|
-
signals, stimulus events with their timing. Behavior reads against what
|
|
12
|
-
the world actually did, not just against text.
|
|
13
|
-
|
|
14
|
-
The SDK captures out of band; responses are never modified or delayed.
|
|
15
|
-
From the record, the dashboard shows the worst conversations first (opened
|
|
16
|
-
on the transcript), joins outcomes back to the conversation that caused
|
|
17
|
-
them even when they arrive days later, and computes behavioural
|
|
18
|
-
measurements on top: the situations your deployment handles, voice
|
|
19
|
-
consistency, uptake, breaks, return behaviour. Every interpreted number
|
|
20
|
-
stays preliminary until it agrees with your own labels on your own traffic.
|
|
21
|
-
|
|
22
|
-
The SDK is in beta. The record format is versioned and stable; the surfaces
|
|
23
|
-
around it are still moving.
|
|
3
|
+
The capture SDK and CLI for Sight. It records one turn per model call made
|
|
4
|
+
inside a session scope: the person's message, the reply it prompted, the
|
|
5
|
+
plumbing (model, tokens, latency, tool names, finish reason, cost when
|
|
6
|
+
reported). Capture is out of band; responses are never modified or
|
|
7
|
+
delayed. The record is one published, versioned shape. Docs:
|
|
8
|
+
https://sight.cultured.computer/docs
|
|
24
9
|
|
|
25
10
|
## Install
|
|
26
11
|
|
|
@@ -28,36 +13,118 @@ around it are still moving.
|
|
|
28
13
|
npm install @cultured_computer/sight
|
|
29
14
|
```
|
|
30
15
|
|
|
31
|
-
|
|
16
|
+
Requirements:
|
|
17
|
+
|
|
18
|
+
- Node 20.6 or later.
|
|
19
|
+
- Server-side Node only; never ship the key to a browser.
|
|
20
|
+
- TypeScript types ship with the package. ESM and CommonJS builds are both
|
|
21
|
+
included.
|
|
32
22
|
|
|
33
23
|
## Quickstart
|
|
34
24
|
|
|
25
|
+
A complete example that captures one turn per model call:
|
|
26
|
+
|
|
35
27
|
```ts
|
|
36
|
-
import
|
|
28
|
+
import OpenAI from "openai";
|
|
29
|
+
import { Sight, wrapOpenAI } from "@cultured_computer/sight";
|
|
37
30
|
|
|
38
31
|
Sight.init({
|
|
39
32
|
apiKey: process.env.SIGHT_API_KEY!,
|
|
40
|
-
deployment:
|
|
41
|
-
})
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
33
|
+
deployment: "my-assistant",
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
const openai = wrapOpenAI(new OpenAI());
|
|
37
|
+
|
|
38
|
+
export async function reply(
|
|
39
|
+
conversationId: string,
|
|
40
|
+
userId: string,
|
|
41
|
+
userMessage: string,
|
|
42
|
+
): Promise<string> {
|
|
43
|
+
Sight.startSession({ externalId: conversationId, externalUserId: userId });
|
|
44
|
+
// The person's message. It is sent with the turn captured next.
|
|
45
|
+
Sight.userInputSubmit(conversationId, userMessage);
|
|
46
|
+
|
|
47
|
+
// A model call is captured only inside a session scope.
|
|
48
|
+
return Sight.withSession(conversationId, async () => {
|
|
49
|
+
const res = await openai.chat.completions.create({
|
|
50
|
+
model: "gpt-4.1",
|
|
51
|
+
messages: [{ role: "user", content: userMessage }],
|
|
52
|
+
});
|
|
53
|
+
return res.choices[0]?.message.content ?? "";
|
|
54
|
+
});
|
|
55
|
+
}
|
|
47
56
|
```
|
|
48
57
|
|
|
58
|
+
On the first accepted turn the SDK prints one line on stderr with a link to
|
|
59
|
+
the session in the dashboard.
|
|
60
|
+
|
|
49
61
|
Notes:
|
|
50
62
|
|
|
51
|
-
- `apiKey` is a
|
|
52
|
-
|
|
53
|
-
(shown once)
|
|
54
|
-
|
|
55
|
-
|
|
63
|
+
- `apiKey` is a key with the `ingest` capability (access word `ingest`,
|
|
64
|
+
`both` or `app`), minted by an owner on the dashboard's API Keys page
|
|
65
|
+
(shown once) and set as `SIGHT_API_KEY`. Sign-in is allowlisted; without
|
|
66
|
+
a dashboard, redeem a provisioning token with `sight account create`.
|
|
67
|
+
The endpoint defaults to `https://fieldwork-ingest.fly.dev`; override it
|
|
68
|
+
with `endpoint` only for self-hosting or staging.
|
|
56
69
|
- `init` runs once at startup. `deployment` identifies the configuration this
|
|
57
70
|
traffic belongs to; all measurement is per deployment.
|
|
58
|
-
- `
|
|
59
|
-
|
|
71
|
+
- `Sight.withSession(id, fn)` is required for autocapture. `id` is the
|
|
72
|
+
session's external id (one conversation). A model call made outside any
|
|
73
|
+
session scope is not captured. The first such call in a process prints one
|
|
74
|
+
warning naming `Sight.withSession(id, fn)`; later ones log only with
|
|
75
|
+
`debug: true`.
|
|
76
|
+
- `startSession` is optional. Use it to attach `externalUserId`, a stable,
|
|
77
|
+
pseudonymous user id you supply. Return metrics key on the user id;
|
|
60
78
|
sessions without it still capture, but return behaviour is not computable.
|
|
79
|
+
- `wrapOpenAI` does not read the person's message from the request. Record
|
|
80
|
+
it with `Sight.userInputSubmit(sessionId, text)` before the model call.
|
|
81
|
+
|
|
82
|
+
## Headless project setup
|
|
83
|
+
|
|
84
|
+
Provider setup requires an explicitly issued **management** provisioning token
|
|
85
|
+
(`--access manage`), not an ordinary ingest, Studio, or combined token. It grants
|
|
86
|
+
only `providers:write`, allowing provider-key replacement and budget changes.
|
|
87
|
+
With that administrative token, connect your own OpenRouter key without a browser:
|
|
88
|
+
|
|
89
|
+
```bash
|
|
90
|
+
export SIGHT_PROVISION_TOKEN='<one-time management token from Sight>'
|
|
91
|
+
export OPENROUTER_API_KEY='<your OpenRouter API key>'
|
|
92
|
+
|
|
93
|
+
npx @cultured_computer/sight account create --openrouter --budget 25
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
The provisioning token and OpenRouter key are read only from the environment,
|
|
97
|
+
not command-line arguments. The Sight key is returned once and saved to
|
|
98
|
+
`~/.config/sight/machine.json` with mode `0600`; it is never printed. Sight
|
|
99
|
+
checks the OpenRouter key against OpenRouter, seals it before database storage,
|
|
100
|
+
and records the monthly budget. Check the safe, redacted state later with:
|
|
101
|
+
|
|
102
|
+
```bash
|
|
103
|
+
npx @cultured_computer/sight openrouter status
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
If OpenRouter setup fails after account creation, the Sight credential remains
|
|
107
|
+
saved. Fix the key or budget and retry only that step:
|
|
108
|
+
|
|
109
|
+
```bash
|
|
110
|
+
export OPENROUTER_API_KEY='<your OpenRouter API key>'
|
|
111
|
+
npx @cultured_computer/sight openrouter connect --budget 25
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
For an existing project, ask its owner for a separate **Provider administration
|
|
115
|
+
only** key, or have the operator mint one with `scripts/mint-key.mjs --access manage`.
|
|
116
|
+
Set it as `SIGHT_API_KEY` for `openrouter connect`. Use a separate `studio` or
|
|
117
|
+
`both` key for execution; management keys do not authorize runs or ingest.
|
|
118
|
+
Existing tokens and keys are never upgraded. An ordinary provisioning token
|
|
119
|
+
can still be redeemed with `account create` without `--openrouter`.
|
|
120
|
+
Redemption is one-time for the project, so a second provisioning token cannot
|
|
121
|
+
add an execution key after admin provisioning. The owner must mint the separate
|
|
122
|
+
ingest/Studio key in API Keys after claiming the project, or the operator must
|
|
123
|
+
mint it with `scripts/mint-key.mjs --access ingest`, `studio`, or `both`.
|
|
124
|
+
|
|
125
|
+
Use `SIGHT_API_KEY` from your secret manager in the application process. The
|
|
126
|
+
local `machine.json` is a CLI credential store, not a project `.env` file and
|
|
127
|
+
is never copied into your repository automatically.
|
|
61
128
|
|
|
62
129
|
## Pull your own data out: the CLI, from your terminal
|
|
63
130
|
|
|
@@ -97,43 +164,304 @@ npx @cultured_computer/sight auth logout
|
|
|
97
164
|
|
|
98
165
|
One more CLI door, for a different shape: `sight connect` attaches Sight to a
|
|
99
166
|
buzz community you don't serve as a read-only observer (invite-code
|
|
100
|
-
authorization, no account needed up front). A side lane, not the install
|
|
167
|
+
authorization, no account needed up front). A side lane, not the install:
|
|
101
168
|
the walkthrough lives at
|
|
102
169
|
[Connect your buzz community](https://sight.cultured.computer/docs/connect-your-room).
|
|
103
170
|
|
|
104
|
-
## What
|
|
171
|
+
## What autocapture records
|
|
172
|
+
|
|
173
|
+
Autocapture turns each model call made inside `Sight.withSession(id, fn)`
|
|
174
|
+
into one turn. There are three paths. A model call seen by more than one
|
|
175
|
+
path is one turn.
|
|
176
|
+
|
|
177
|
+
### OpenAI: wrap the client
|
|
178
|
+
|
|
179
|
+
The path to use for OpenAI. It does not depend on module format, load
|
|
180
|
+
order, or the `openai` version.
|
|
181
|
+
|
|
182
|
+
```ts
|
|
183
|
+
const openai = wrapOpenAI(new OpenAI());
|
|
184
|
+
// Wrap before or after Sight.init(); order does not matter.
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
Covers `chat.completions.create` and `responses.create`, streaming included.
|
|
188
|
+
|
|
189
|
+
Provider is read from the client's `baseURL`:
|
|
190
|
+
|
|
191
|
+
| `baseURL` host | Provider |
|
|
192
|
+
|---|---|
|
|
193
|
+
| none, or `api.openai.com` | `openai` |
|
|
194
|
+
| `openrouter.ai` | `openrouter` |
|
|
195
|
+
| `*.openai.azure.com` | `azure` |
|
|
196
|
+
| `*.inceptionlabs.ai`, or a model name starting with `mercury` | `inception` |
|
|
197
|
+
| any other host | not sent |
|
|
198
|
+
|
|
199
|
+
A streamed call through a wrapped client also records:
|
|
200
|
+
|
|
201
|
+
- First-token time: milliseconds from the start of the request to the first
|
|
202
|
+
chunk that carries output. Chat Completions: a non-empty
|
|
203
|
+
`delta.content`, a non-empty `delta.refusal`, or a `delta.tool_calls`
|
|
204
|
+
entry. The opening chunk that carries only the role is not a token.
|
|
205
|
+
Responses API: the first `response.output_text.delta`,
|
|
206
|
+
`response.refusal.delta` or `response.function_call_arguments.delta`, or
|
|
207
|
+
a `response.output_item.added` whose item is a `function_call`.
|
|
208
|
+
- Tool names and count of streamed tool calls.
|
|
105
209
|
|
|
106
|
-
|
|
107
|
-
|
|
210
|
+
A call that is not streamed has no first-token time; the field is not sent.
|
|
211
|
+
A stream that ends before any output has none either.
|
|
212
|
+
|
|
213
|
+
### Vercel AI SDK (`ai`)
|
|
214
|
+
|
|
215
|
+
Verified with `ai` 7.0.17 (telemetry integration) and `ai` 6.0.300
|
|
216
|
+
(OpenTelemetry spans). `ai` 3 to 5 are not verified.
|
|
217
|
+
|
|
218
|
+
`ai` 7: nothing to add. `init` registers a telemetry integration with `ai`,
|
|
219
|
+
and `ai` 7 sends every call to a registered integration; no per-call flag.
|
|
220
|
+
|
|
221
|
+
```ts
|
|
222
|
+
import { generateText } from "ai";
|
|
223
|
+
|
|
224
|
+
Sight.init({ apiKey: process.env.SIGHT_API_KEY!, deployment: "my-assistant" });
|
|
225
|
+
|
|
226
|
+
await Sight.withSession(conversationId, () =>
|
|
227
|
+
generateText({ model, system, messages }),
|
|
228
|
+
);
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
Covers `generateText`, `streamText`, `generateObject`, `streamObject` and
|
|
232
|
+
`ToolLoopAgent`.
|
|
233
|
+
|
|
234
|
+
- Turn unit: one model call. A tool loop of N model calls is N turns. Each
|
|
235
|
+
carries the text of that reply (none when the model only requested tools)
|
|
236
|
+
and the names of the tools it requested. The outer `generateText` call is
|
|
237
|
+
not a turn.
|
|
238
|
+
- Streaming: one turn when the model call's stream ends, with the full text
|
|
239
|
+
and the time to the first output. A stream nobody reads captures nothing.
|
|
240
|
+
- A model call that throws is one turn with the error message and no reply.
|
|
241
|
+
An aborted stream is one turn with error `aborted` and no reply. A stream
|
|
242
|
+
that fails part way is one turn with the partial text, finish reason
|
|
243
|
+
`error` and an error field.
|
|
244
|
+
- `generateObject` and `streamObject`: the reply is the JSON text the model
|
|
245
|
+
produced.
|
|
246
|
+
- Session: the `Sight.withSession` scope the call started in. Outside any
|
|
247
|
+
scope, name the session on the call, either way:
|
|
108
248
|
|
|
109
249
|
```ts
|
|
110
|
-
import
|
|
111
|
-
|
|
250
|
+
import { sightTelemetry } from "@cultured_computer/sight";
|
|
251
|
+
|
|
252
|
+
// 1. On the integration, for this call.
|
|
253
|
+
generateText({
|
|
254
|
+
model,
|
|
255
|
+
prompt,
|
|
256
|
+
telemetry: { integrations: sightTelemetry({ sessionId: conversationId }) },
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
// 2. In runtimeContext. `ai` passes a runtimeContext key to telemetry only
|
|
260
|
+
// when includeRuntimeContext names it. Keys read: sightSessionId, then
|
|
261
|
+
// sessionId.
|
|
262
|
+
generateText({
|
|
263
|
+
model,
|
|
264
|
+
prompt,
|
|
265
|
+
runtimeContext: { sightSessionId: conversationId },
|
|
266
|
+
telemetry: { includeRuntimeContext: { sightSessionId: true } },
|
|
267
|
+
});
|
|
268
|
+
```
|
|
112
269
|
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
270
|
+
An explicit scope wins over both. `telemetry.integrations` on a call
|
|
271
|
+
replaces the registered integrations for that call (an `ai` rule): to keep
|
|
272
|
+
another integration on that call, list it next to `sightTelemetry(...)`.
|
|
273
|
+
With no scope and no session id the call is skipped and the one
|
|
274
|
+
no-session warning prints.
|
|
275
|
+
- `telemetry: { isEnabled: false }` on a call turns capture off for it.
|
|
276
|
+
`recordOutputs: false` drops the reply text. `recordInputs: false` drops
|
|
277
|
+
the person's message and the system prompt hash.
|
|
278
|
+
- Registration: `init` loads `ai` and calls its `registerTelemetry()`.
|
|
279
|
+
On Node 22.13 or later (`ai` 7 itself needs Node 22) this is done before
|
|
280
|
+
`init` returns. On older Node it completes a moment later and a call made
|
|
281
|
+
in the same tick as `init` is missed. If the SDK cannot resolve your `ai` (some bundles), register it
|
|
282
|
+
yourself, once:
|
|
283
|
+
|
|
284
|
+
```ts
|
|
285
|
+
import { registerTelemetry } from "ai";
|
|
286
|
+
import { sightTelemetry } from "@cultured_computer/sight";
|
|
287
|
+
|
|
288
|
+
registerTelemetry(sightTelemetry());
|
|
117
289
|
```
|
|
118
290
|
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
and
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
291
|
+
Registering both ways is safe: it is registered once.
|
|
292
|
+
- With `@ai-sdk/otel` also registered, the span it emits for a model call
|
|
293
|
+
and the Sight integration's turn are one turn (matched on the provider
|
|
294
|
+
response id).
|
|
295
|
+
|
|
296
|
+
What an `ai` 7 turn carries: reply text (text parts only; reasoning, tool
|
|
297
|
+
input and tool output are never part of it), the model id the provider
|
|
298
|
+
returned, the provider id as `ai` names it (for example `openai.responses`),
|
|
299
|
+
input and output tokens, cache read and cache write tokens, tool names and
|
|
300
|
+
count, finish reason, the model call's response time, time to first output
|
|
301
|
+
for streams, the person's message (same rule as below), a `turnId`.
|
|
302
|
+
Not carried: cost; the error message of a stream that fails part way (the
|
|
303
|
+
events do not include it); the partial text of an aborted stream.
|
|
304
|
+
|
|
305
|
+
`ai` 6: it sends no events to the integration's model-call hooks. It emits
|
|
306
|
+
OpenTelemetry spans, and only for calls that set the flag:
|
|
307
|
+
|
|
308
|
+
```ts
|
|
309
|
+
await Sight.withSession(conversationId, () =>
|
|
310
|
+
generateText({ model, prompt, experimental_telemetry: { isEnabled: true } }),
|
|
311
|
+
);
|
|
312
|
+
```
|
|
313
|
+
|
|
314
|
+
The span processor (next section) captures its model-call spans:
|
|
315
|
+
`ai.generateText.doGenerate`, `ai.streamText.doStream`,
|
|
316
|
+
`ai.generateObject.doGenerate`, `ai.streamObject.doStream`. Same turn unit.
|
|
317
|
+
The outer `ai.generateText` span and `ai.toolCall` spans are not turns.
|
|
318
|
+
Outside a scope, the session id is read from
|
|
319
|
+
`experimental_telemetry.metadata.sightSessionId`, then `.sessionId`.
|
|
320
|
+
|
|
321
|
+
### Instrumented libraries: OpenTelemetry spans
|
|
322
|
+
|
|
323
|
+
From `init`, the SDK enables module instrumentation for the OpenAI SDK, the
|
|
324
|
+
Anthropic SDK and LangChain (LangGraph runs through the LangChain
|
|
325
|
+
instrumentation), and captures the model-call spans they emit. Each adapter
|
|
326
|
+
does nothing if its library is not installed.
|
|
327
|
+
|
|
328
|
+
Limits:
|
|
329
|
+
|
|
330
|
+
- OpenAI module patching attaches only under CommonJS, only when `openai`
|
|
331
|
+
loads after `init`, and only for openai v4. If it did not attach and no
|
|
332
|
+
client has been wrapped, the SDK prints one warning at startup. Under ESM
|
|
333
|
+
or openai v5 and later, wrap the client.
|
|
334
|
+
- Anthropic module patching has the same CommonJS and load-order limits and
|
|
335
|
+
no startup check. Confirm capture with `debug: true`.
|
|
336
|
+
- LiteLLM (Python) is not captured.
|
|
337
|
+
|
|
338
|
+
Any OpenTelemetry instrumentation that emits GenAI spans through the global
|
|
339
|
+
tracer provider is captured the same way. A span becomes a turn when
|
|
340
|
+
`gen_ai.operation.name` is `chat`, `generate_content` or `text_completion`,
|
|
341
|
+
or when it is one of the `ai` 6 model-call spans named above (identified by
|
|
342
|
+
`ai.operationId`). Spans for `embeddings`, `invoke_agent`, `create_agent`,
|
|
343
|
+
workflows and `execute_tool` are not turns.
|
|
344
|
+
|
|
345
|
+
Attributes read from a span (the current name first, then the fallbacks):
|
|
346
|
+
|
|
347
|
+
| Field | Attributes |
|
|
348
|
+
|---|---|
|
|
349
|
+
| Provider | `gen_ai.provider.name`, `gen_ai.system`, `gen_ai.provider`, `ai.model.provider` |
|
|
350
|
+
| Model | `gen_ai.response.model`, `ai.response.model`, `gen_ai.request.model`, `ai.model.id` |
|
|
351
|
+
| Requested model | `gen_ai.request.model`, `ai.model.id` |
|
|
352
|
+
| Cost | `gen_ai.usage.cost` |
|
|
353
|
+
| Reply text | `ai.response.text` when present; otherwise the text parts of `gen_ai.output.messages`; otherwise `ai.response.object` |
|
|
354
|
+
| The person's message | last user message of `gen_ai.input.messages`, or of `ai.prompt.messages` |
|
|
355
|
+
| Input tokens | `gen_ai.usage.input_tokens`, `gen_ai.usage.prompt_tokens`, `ai.usage.inputTokens`, `ai.usage.promptTokens` |
|
|
356
|
+
| Output tokens | `gen_ai.usage.output_tokens`, `gen_ai.usage.completion_tokens`, `ai.usage.outputTokens`, `ai.usage.completionTokens` |
|
|
357
|
+
| Cache read tokens | `gen_ai.usage.cache_read.input_tokens`, `gen_ai.usage.cache_read_input_tokens`, `ai.usage.inputTokenDetails.cacheReadTokens`, `ai.usage.cachedInputTokens` |
|
|
358
|
+
| Cache creation tokens | `gen_ai.usage.cache_creation.input_tokens`, `gen_ai.usage.cache_creation_input_tokens`, `ai.usage.inputTokenDetails.cacheWriteTokens` |
|
|
359
|
+
| Tool names and count | `tool_call` parts of `gen_ai.output.messages`; `toolName` of each entry in `ai.response.toolCalls`; child `execute_tool` spans (`gen_ai.tool.name`); `gen_ai.response.tool_names`, `gen_ai.response.tool_calls_count` |
|
|
360
|
+
| Finish reason | first of `gen_ai.response.finish_reasons`, `ai.response.finishReason` |
|
|
361
|
+
| Time to first token | `ai.response.msToFirstChunk`, `ai.stream.msToFirstChunk` |
|
|
362
|
+
| Session (fallback) | `gen_ai.conversation.id`, `ai.telemetry.metadata.sightSessionId`, `ai.telemetry.metadata.sessionId` |
|
|
363
|
+
|
|
364
|
+
Rules:
|
|
365
|
+
|
|
366
|
+
- Session: the `Sight.withSession` scope first. With no scope, the first
|
|
367
|
+
session attribute in the table that the span carries is the session id.
|
|
368
|
+
- A span is not a second turn for a model call another path captures: a
|
|
369
|
+
span started inside an `ai` 7 model call that the Sight integration
|
|
370
|
+
handles is skipped, and so is a span whose `gen_ai.response.id` (or
|
|
371
|
+
`ai.response.id`) the integration reported for the same call. A wrapped
|
|
372
|
+
OpenAI client called inside such a model call passes through uncaptured.
|
|
373
|
+
Not covered: two instrumentations that both emit a span for one call
|
|
374
|
+
outside `ai` (for example a patched and a wrapped OpenAI client used
|
|
375
|
+
together); those are two turns.
|
|
376
|
+
- The person's message is sent as `userInput.text` only when the request's
|
|
377
|
+
last message is the person's and carries text, and only when no text
|
|
378
|
+
recorded with `userInputSubmit` is pending for that turn. Explicit input
|
|
379
|
+
wins. A turn's person text is the message that prompted its reply.
|
|
380
|
+
- The reply is stored as plain text. A structured message array is parsed;
|
|
381
|
+
reasoning and tool parts are not part of the reply text.
|
|
382
|
+
|
|
383
|
+
### Fields on every autocaptured turn
|
|
384
|
+
|
|
385
|
+
Model, provider, input and output tokens, latency, tool names and count,
|
|
386
|
+
finish reason, error message, cache read tokens when the provider reports
|
|
387
|
+
them, the reply text, the requested model, a `turnId`.
|
|
388
|
+
|
|
389
|
+
First-token time: sent for a streamed call through a wrapped OpenAI client
|
|
390
|
+
and for an `ai` stream. Not sent for a call that is not streamed, and not
|
|
391
|
+
sent by the bundled OpenAI, Anthropic and LangChain instrumentations, which
|
|
392
|
+
do not report it on the span.
|
|
393
|
+
|
|
394
|
+
Cost, in this order:
|
|
395
|
+
|
|
396
|
+
1. Reported by the provider. When the response usage carries a cost
|
|
397
|
+
(OpenRouter's `usage.cost`; `gen_ai.usage.cost` on a span), the SDK sends
|
|
398
|
+
it and the turn stores it as reported. A reported cost of 0 is stored
|
|
399
|
+
as 0. On an OpenRouter bring-your-own-key call the cost is sent only
|
|
400
|
+
when `cost_details.upstream_inference_cost` is reported too, as the sum
|
|
401
|
+
of the two. The `ai` 7 integration sends no cost.
|
|
402
|
+
2. Estimated from the price catalogue. With no reported cost, the server
|
|
403
|
+
computes the cost from the turn's token counts when the turn's provider
|
|
404
|
+
and model name exactly one catalogue price in effect at the turn's time.
|
|
405
|
+
The match is exact string equality on provider and model: the response
|
|
406
|
+
model first, then the requested model (OpenRouter turns: the response
|
|
407
|
+
model only). Cached tokens are priced at the catalogue's cached rate.
|
|
408
|
+
The estimate is a list price, not a billed amount.
|
|
409
|
+
3. Unavailable. In every other case the turn has no cost: no token counts,
|
|
410
|
+
an unknown provider or model, more than one matching catalogue entry,
|
|
411
|
+
no price in effect at the turn's time, or cached tokens with no cached
|
|
412
|
+
rate in the catalogue or no known counting convention. An unavailable
|
|
413
|
+
cost is empty, never 0.
|
|
414
|
+
|
|
415
|
+
Each turn records which of the three applied on its `probe_meta`:
|
|
416
|
+
`costSource` is `provider` or `catalogue:<price version id>`, or absent
|
|
417
|
+
with `costUnavailable` naming the reason.
|
|
418
|
+
|
|
419
|
+
Not sent by autocapture: reasoning tokens.
|
|
420
|
+
|
|
421
|
+
### Delivery
|
|
422
|
+
|
|
423
|
+
- Capture never blocks your LLM path. Send failures log in debug mode and
|
|
424
|
+
are otherwise swallowed.
|
|
425
|
+
- Invalid input to any capture method logs one warning line naming the
|
|
426
|
+
first invalid field and is dropped.
|
|
427
|
+
- A rate-limited send (429) is retried once after `Retry-After`.
|
|
428
|
+
- Sessions are created lazily on the first captured turn.
|
|
429
|
+
|
|
430
|
+
## Capture a turn manually
|
|
431
|
+
|
|
432
|
+
For calls autocapture does not see:
|
|
433
|
+
|
|
434
|
+
```ts
|
|
435
|
+
await Sight.captureTurn({
|
|
436
|
+
sessionExternalId: conversationId,
|
|
437
|
+
turnIndex: 0,
|
|
438
|
+
userInput: { text: userMessage },
|
|
439
|
+
payload: {
|
|
440
|
+
response: { text: replyText },
|
|
441
|
+
probe_model: "my-model",
|
|
442
|
+
probe_provider: "my-provider",
|
|
443
|
+
probe_tokens_input: 412,
|
|
444
|
+
probe_tokens_output: 96,
|
|
445
|
+
probe_latency_ms: 840,
|
|
446
|
+
},
|
|
447
|
+
});
|
|
448
|
+
```
|
|
449
|
+
|
|
450
|
+
- `deploymentSlug` is optional. It defaults to the `deployment` given to
|
|
451
|
+
`init`; an explicit value wins.
|
|
452
|
+
- `turnIndex` is the turn's position in the session, starting at 0. You
|
|
453
|
+
supply it. (Autocapture counts per session inside the process.)
|
|
454
|
+
- The turn is checked against the wire schema before it is sent. An invalid turn logs one warning
|
|
455
|
+
line and is not sent.
|
|
456
|
+
|
|
457
|
+
## Turn identity
|
|
458
|
+
|
|
459
|
+
Every captured turn is sent with a `turnId`: a fresh UUID per logical turn.
|
|
460
|
+
The SDK's own retry of a send re-sends the same `turnId`. Autocapture's
|
|
461
|
+
`turnIndex` is a counter held in the process, so two processes capturing
|
|
462
|
+
one session, or one process restarted, send the same `turnIndex` with
|
|
463
|
+
different `turnId`s. To supply your own identity on a manual capture, pass
|
|
464
|
+
`turnId` (a UUID) to `captureTurn`.
|
|
137
465
|
|
|
138
466
|
## Attach outcomes
|
|
139
467
|
|
|
@@ -144,15 +472,15 @@ even hours later, from a webhook or CRM callback.
|
|
|
144
472
|
// A user reporting a persona break.
|
|
145
473
|
await Sight.flagPersonaBreak({
|
|
146
474
|
sessionExternalId: sessionId,
|
|
147
|
-
note:
|
|
148
|
-
})
|
|
475
|
+
note: "sounded robotic",
|
|
476
|
+
});
|
|
149
477
|
|
|
150
478
|
// Your own downstream outcome: a resolved ticket, a rating, a conversion.
|
|
151
479
|
await Sight.emitOutcomeEvent({
|
|
152
480
|
sessionExternalId: sessionId,
|
|
153
|
-
kind:
|
|
154
|
-
detail: { ticketId:
|
|
155
|
-
})
|
|
481
|
+
kind: "ticket_resolved",
|
|
482
|
+
detail: { ticketId: "ZD-88213" },
|
|
483
|
+
});
|
|
156
484
|
```
|
|
157
485
|
|
|
158
486
|
## Capture environment events
|
|
@@ -163,10 +491,10 @@ against the conversation:
|
|
|
163
491
|
|
|
164
492
|
```ts
|
|
165
493
|
await Sight.captureEnvironmentEvents(events, {
|
|
166
|
-
clock_source:
|
|
494
|
+
clock_source: "my-event-clock",
|
|
167
495
|
skew_bound_ms: 25,
|
|
168
|
-
rule_version:
|
|
169
|
-
})
|
|
496
|
+
rule_version: "v1",
|
|
497
|
+
});
|
|
170
498
|
```
|
|
171
499
|
|
|
172
500
|
The envelope is generalized: `source_type` and `event` are free strings whose
|
|
@@ -184,26 +512,83 @@ steps, per-call model usage inside a single turn) can ship it as spans:
|
|
|
184
512
|
await Sight.captureSpans(
|
|
185
513
|
[
|
|
186
514
|
{
|
|
187
|
-
|
|
515
|
+
contractVersion: 2,
|
|
516
|
+
deploymentSlug: "my-agent",
|
|
188
517
|
sessionExternalId: sessionId,
|
|
189
518
|
turnIndex: 3,
|
|
190
|
-
spanType:
|
|
191
|
-
|
|
192
|
-
|
|
519
|
+
spanType: "tool_call",
|
|
520
|
+
spanId: "lookup-3",
|
|
521
|
+
parentSpanId: null,
|
|
522
|
+
name: "lookup_order",
|
|
523
|
+
status: "ok",
|
|
524
|
+
startedAt: "2026-07-22T00:00:01.200Z",
|
|
525
|
+
endedAt: "2026-07-22T00:00:01.380Z",
|
|
193
526
|
durationMs: 180,
|
|
194
|
-
|
|
527
|
+
model: null,
|
|
528
|
+
provider: null,
|
|
529
|
+
tokensInput: null,
|
|
530
|
+
tokensOutput: null,
|
|
531
|
+
costUsd: null,
|
|
532
|
+
error: null,
|
|
533
|
+
input: { state: "captured", value: { orderId: "ord_123" } },
|
|
534
|
+
output: { state: "captured", value: { status: "shipped" } },
|
|
535
|
+
intervals: [
|
|
536
|
+
{
|
|
537
|
+
kind: "active",
|
|
538
|
+
startedAt: "2026-07-22T00:00:01.200Z",
|
|
539
|
+
endedAt: "2026-07-22T00:00:01.280Z",
|
|
540
|
+
},
|
|
541
|
+
{
|
|
542
|
+
kind: "waiting",
|
|
543
|
+
startedAt: "2026-07-22T00:00:01.280Z",
|
|
544
|
+
endedAt: "2026-07-22T00:00:01.380Z",
|
|
545
|
+
},
|
|
546
|
+
],
|
|
547
|
+
attributes: { toolName: "lookup_order", argKeys: ["orderId"] },
|
|
195
548
|
},
|
|
196
549
|
],
|
|
197
|
-
{ clock_source:
|
|
198
|
-
)
|
|
550
|
+
{ clock_source: "my-agent-clock", skew_bound_ms: 25, rule_version: "v1" },
|
|
551
|
+
);
|
|
199
552
|
```
|
|
200
553
|
|
|
201
554
|
A span lands on the turn you name, or on the session's latest turn at/under
|
|
202
555
|
its `startedAt` when `turnIndex` is omitted. Parent structure rides the
|
|
203
556
|
batch: give a span a caller-local `spanId` and reference it from a sibling's
|
|
204
|
-
`parentSpanId`.
|
|
205
|
-
|
|
206
|
-
|
|
557
|
+
`parentSpanId`. In contract v2 every operational key is present; use `null`
|
|
558
|
+
for an unknown model, token count, cost, end time, or parent. Input and output
|
|
559
|
+
use explicit `captured`, `empty`, `not_captured`, `redacted`, or `unavailable`
|
|
560
|
+
states, so absence is never guessed from attributes. `attributes` are metadata
|
|
561
|
+
only. Optional `intervals` record internal `active`, `waiting`, `retry`, and
|
|
562
|
+
`error` windows. Every interval requires its own `startedAt` and `endedAt` and
|
|
563
|
+
must stay within the span; when intervals are omitted, the waterfall does not
|
|
564
|
+
invent them from status, names, or retry counters. Status is always explicit
|
|
565
|
+
`ok` or `error`; the historical `unset` state
|
|
566
|
+
is reserved for contract-v1 rows. The unversioned v1 shape remains accepted for older SDK callers and is
|
|
567
|
+
read through a legacy adapter. `Sight.captureSpan(span)` is the single-span
|
|
568
|
+
form.
|
|
569
|
+
|
|
570
|
+
Run and Session trace views render recorded `tool_call` and `llm_call` spans
|
|
571
|
+
with their explicit status and payload states. To display a provider-exposed
|
|
572
|
+
reasoning **summary** and a caller-measured context budget, attach these
|
|
573
|
+
optional attributes to the relevant span:
|
|
574
|
+
|
|
575
|
+
```ts
|
|
576
|
+
attributes: {
|
|
577
|
+
"sight.reasoning.summary": "The recorded provider summary",
|
|
578
|
+
"sight.reasoning.duration_ms": 7000,
|
|
579
|
+
"gen_ai.usage.cache_read_input_tokens": 31500,
|
|
580
|
+
"sight.context.used_tokens": 40372,
|
|
581
|
+
"sight.context.limit_tokens": 100000,
|
|
582
|
+
}
|
|
583
|
+
```
|
|
584
|
+
|
|
585
|
+
Do not supply hidden reasoning or manufacture a summary from the reply.
|
|
586
|
+
Summary strings pass through the same span PII scrub; they are dropped when
|
|
587
|
+
redaction is unavailable. Budget values are recorded per call, not accumulated
|
|
588
|
+
Run spend, and both values are required for the gauge. Missing cached-token
|
|
589
|
+
usage is not zero usage. Tool arguments/results belong in the versioned
|
|
590
|
+
`input`/`output` payloads, not attributes; completion of a model call never
|
|
591
|
+
establishes that a requested tool ran successfully.
|
|
207
592
|
|
|
208
593
|
## Scope
|
|
209
594
|
|
|
@@ -228,13 +613,22 @@ consequences. See [Data format](https://sight.cultured.computer/docs/data-format
|
|
|
228
613
|
## Docs
|
|
229
614
|
|
|
230
615
|
- [Get started](https://sight.cultured.computer/docs/get-started):
|
|
231
|
-
install, initialise, verify capture.
|
|
232
|
-
- [
|
|
233
|
-
|
|
616
|
+
install, initialise, verify capture, the turn identity contract.
|
|
617
|
+
- [Keys and access](https://sight.cultured.computer/docs/keys-and-access):
|
|
618
|
+
the seven access words and which host takes which key.
|
|
619
|
+
- [Integration paths](https://sight.cultured.computer/docs/integration-paths):
|
|
620
|
+
what each capture path sends.
|
|
234
621
|
- [SDK reference](https://sight.cultured.computer/docs/sdk-reference):
|
|
235
622
|
the full method surface.
|
|
236
|
-
- [
|
|
237
|
-
|
|
623
|
+
- [CLI](https://sight.cultured.computer/docs/cli): every verb.
|
|
624
|
+
- [HTTP API](https://sight.cultured.computer/docs/http-api): every route
|
|
625
|
+
on the capture host and the Field API.
|
|
626
|
+
- [Privacy & retention](https://sight.cultured.computer/docs/data-and-privacy):
|
|
627
|
+
what is scrubbed, by what, and what is kept.
|
|
628
|
+
- [Serve a SOUL.md](https://sight.cultured.computer/docs/serve-a-soul-md):
|
|
629
|
+
the Field API, which this package has no client or CLI verbs for.
|
|
630
|
+
- [Status](https://sight.cultured.computer/docs/where-it-stands): built,
|
|
631
|
+
operator-run, not built.
|
|
238
632
|
|
|
239
633
|
## Legal
|
|
240
634
|
|