@cultured_computer/sight 0.8.2 → 0.8.4
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 +17 -0
- package/README.md +10 -1
- package/dist/cli.js +15 -8
- package/dist/index.cjs +52 -17
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +52 -17
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/schema/sight-record-v1.schema.json +8 -0
package/dist/index.js
CHANGED
|
@@ -10,6 +10,34 @@ var MAX_PENDING_RETRIES = 50;
|
|
|
10
10
|
var SEND_TIMEOUT_MS = 15e3;
|
|
11
11
|
var MAX_INFLIGHT = 64;
|
|
12
12
|
var MAX_TURN_BODY_BYTES = 9e5;
|
|
13
|
+
var MAX_ACK_BODY_BYTES = 65536;
|
|
14
|
+
async function readAcknowledgement(response) {
|
|
15
|
+
if (!response.body) return {};
|
|
16
|
+
const reader = response.body.getReader();
|
|
17
|
+
const chunks = [];
|
|
18
|
+
let size = 0;
|
|
19
|
+
try {
|
|
20
|
+
while (true) {
|
|
21
|
+
const { done, value } = await reader.read();
|
|
22
|
+
if (done) break;
|
|
23
|
+
size += value.byteLength;
|
|
24
|
+
if (size > MAX_ACK_BODY_BYTES) throw new Error("acknowledgement exceeds 65536 bytes");
|
|
25
|
+
chunks.push(value);
|
|
26
|
+
}
|
|
27
|
+
const bytes = new Uint8Array(size);
|
|
28
|
+
let offset = 0;
|
|
29
|
+
for (const chunk of chunks) {
|
|
30
|
+
bytes.set(chunk, offset);
|
|
31
|
+
offset += chunk.byteLength;
|
|
32
|
+
}
|
|
33
|
+
const body = JSON.parse(new TextDecoder().decode(bytes));
|
|
34
|
+
return body !== null && typeof body === "object" && "pendingId" in body && typeof body.pendingId === "string" ? { pendingId: body.pendingId } : {};
|
|
35
|
+
} finally {
|
|
36
|
+
await reader.cancel().catch(() => {
|
|
37
|
+
});
|
|
38
|
+
reader.releaseLock();
|
|
39
|
+
}
|
|
40
|
+
}
|
|
13
41
|
var IngestClient = class {
|
|
14
42
|
apiKey;
|
|
15
43
|
endpoint;
|
|
@@ -45,7 +73,7 @@ var IngestClient = class {
|
|
|
45
73
|
* first drop prints one stderr warning, later ones log only with
|
|
46
74
|
* `debug: true`.
|
|
47
75
|
*/
|
|
48
|
-
async guardedFetch(url, init) {
|
|
76
|
+
async guardedFetch(url, init, consume) {
|
|
49
77
|
if (this.inFlight >= MAX_INFLIGHT) {
|
|
50
78
|
if (!this.shedAnnounced) {
|
|
51
79
|
this.shedAnnounced = true;
|
|
@@ -56,9 +84,14 @@ var IngestClient = class {
|
|
|
56
84
|
throw new Error(`${MAX_INFLIGHT} sends already in flight; this send was dropped`);
|
|
57
85
|
}
|
|
58
86
|
this.inFlight++;
|
|
87
|
+
let response;
|
|
59
88
|
try {
|
|
60
|
-
|
|
89
|
+
response = await fetch(url, { ...init, signal: AbortSignal.timeout(SEND_TIMEOUT_MS) });
|
|
90
|
+
await consume?.(response);
|
|
91
|
+
return response;
|
|
61
92
|
} finally {
|
|
93
|
+
if (response?.body && !response.body.locked) await response.body.cancel().catch(() => {
|
|
94
|
+
});
|
|
62
95
|
this.inFlight--;
|
|
63
96
|
}
|
|
64
97
|
}
|
|
@@ -69,8 +102,8 @@ var IngestClient = class {
|
|
|
69
102
|
* the time is up is not guaranteed to be sent.
|
|
70
103
|
*/
|
|
71
104
|
async drainInFlight(timeoutMs = SEND_TIMEOUT_MS) {
|
|
72
|
-
const deadline =
|
|
73
|
-
while ((this.inFlight > 0 || this.pendingRetries > 0) &&
|
|
105
|
+
const deadline = performance.now() + timeoutMs;
|
|
106
|
+
while ((this.inFlight > 0 || this.pendingRetries > 0) && performance.now() < deadline) {
|
|
74
107
|
await new Promise((r) => setTimeout(r, 50));
|
|
75
108
|
}
|
|
76
109
|
}
|
|
@@ -100,6 +133,18 @@ var IngestClient = class {
|
|
|
100
133
|
"content-type": "application/json"
|
|
101
134
|
},
|
|
102
135
|
body
|
|
136
|
+
}, async (response2) => {
|
|
137
|
+
if (!response2.ok) return;
|
|
138
|
+
if (!this.firstCaptureAnnounced) {
|
|
139
|
+
this.firstCaptureAnnounced = true;
|
|
140
|
+
console.error(
|
|
141
|
+
`Sight: first session captured -> ${this.dashboardUrl}/sessions/${encodeURIComponent(turn.sessionExternalId)}`
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
if (this.debug) {
|
|
145
|
+
const ack = await readAcknowledgement(response2);
|
|
146
|
+
this.log(`[sight] ingest ok id=${ack.pendingId ?? "?"}`);
|
|
147
|
+
}
|
|
103
148
|
});
|
|
104
149
|
const response = await this.retryIfRateLimited(await send(), send);
|
|
105
150
|
if (response === null) return;
|
|
@@ -108,16 +153,6 @@ var IngestClient = class {
|
|
|
108
153
|
this.log(`[sight] ingest failed status=${response.status}`);
|
|
109
154
|
return;
|
|
110
155
|
}
|
|
111
|
-
if (!this.firstCaptureAnnounced) {
|
|
112
|
-
this.firstCaptureAnnounced = true;
|
|
113
|
-
console.error(
|
|
114
|
-
`Sight: first session captured -> ${this.dashboardUrl}/sessions/${encodeURIComponent(turn.sessionExternalId)}`
|
|
115
|
-
);
|
|
116
|
-
}
|
|
117
|
-
if (this.debug) {
|
|
118
|
-
const body2 = await response.json();
|
|
119
|
-
this.log(`[sight] ingest ok id=${body2.pendingId ?? "?"}`);
|
|
120
|
-
}
|
|
121
156
|
} catch (err) {
|
|
122
157
|
const msg = err instanceof Error ? err.message : String(err);
|
|
123
158
|
this.log(`[sight] ingest error: ${msg}`);
|
|
@@ -1898,7 +1933,7 @@ function createIntegration(options) {
|
|
|
1898
1933
|
call.contextSession = sessionFromContext(e["runtimeContext"]) ?? call.contextSession;
|
|
1899
1934
|
const model = rec(e["model"]);
|
|
1900
1935
|
const response = rec(e["response"]);
|
|
1901
|
-
const
|
|
1936
|
+
const performance2 = rec(e["performance"]);
|
|
1902
1937
|
const content = Array.isArray(e["content"]) ? e["content"] : [];
|
|
1903
1938
|
const toolNames = [];
|
|
1904
1939
|
for (const part of content) {
|
|
@@ -1924,8 +1959,8 @@ function createIntegration(options) {
|
|
|
1924
1959
|
finishReason,
|
|
1925
1960
|
rawFinishReason: str(e["rawFinishReason"]),
|
|
1926
1961
|
toolNames,
|
|
1927
|
-
latencyMs: ms(
|
|
1928
|
-
firstTokenMs: ms(
|
|
1962
|
+
latencyMs: ms(performance2?.["responseTimeMs"]) ?? (open ? Math.max(0, Date.now() - open.startedAt) : void 0),
|
|
1963
|
+
firstTokenMs: ms(performance2?.["timeToFirstOutputMs"]),
|
|
1929
1964
|
// A stream that failed part way ends its step with this finish
|
|
1930
1965
|
// reason; the events carry no error message for it.
|
|
1931
1966
|
error: finishReason === "error" ? 'model call ended with finish reason "error"' : void 0,
|