@cultured_computer/sight 0.1.0 → 0.2.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/README.md +4 -4
- package/dist/index.cjs +184 -17
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +56 -7
- package/dist/index.d.ts +56 -7
- package/dist/index.js +184 -17
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.d.cts
CHANGED
|
@@ -135,7 +135,13 @@ type PersonaConfig = z.infer<typeof PersonaConfigSchema>;
|
|
|
135
135
|
declare const OutcomeHookSchema: z.ZodObject<{
|
|
136
136
|
/** Type of outcome the deployment tracks. */
|
|
137
137
|
type: z.ZodEnum<["numeric", "categorical", "boolean", "duration"]>;
|
|
138
|
-
/**
|
|
138
|
+
/**
|
|
139
|
+
* Callback that resolves the outcome for a completed session. Invoked by
|
|
140
|
+
* Probe.endSession() with the session's externalId; a non-null result
|
|
141
|
+
* lands as an outcome event on the session. Not invoked for sessions
|
|
142
|
+
* swept server-side (no explicit endSession) — use emitOutcomeEvent()
|
|
143
|
+
* for outcomes that become known out of band.
|
|
144
|
+
*/
|
|
139
145
|
resolve: z.ZodType<(sessionId: string) => Promise<OutcomeResult | null>, z.ZodTypeDef, (sessionId: string) => Promise<OutcomeResult | null>>;
|
|
140
146
|
}, "strip", z.ZodTypeAny, {
|
|
141
147
|
type: "boolean" | "numeric" | "categorical" | "duration";
|
|
@@ -157,6 +163,13 @@ declare const ProbeConfigSchema: z.ZodObject<{
|
|
|
157
163
|
deployment: z.ZodString;
|
|
158
164
|
/** Ingest endpoint. Defaults to https://ingest.probe.io. */
|
|
159
165
|
endpoint: z.ZodDefault<z.ZodString>;
|
|
166
|
+
/**
|
|
167
|
+
* Dashboard base URL used by the one-time first-capture announcement
|
|
168
|
+
* (the stderr permalink printed after the first successful send).
|
|
169
|
+
* Self-hosted or staging dashboards override it the same way `endpoint`
|
|
170
|
+
* is overridden.
|
|
171
|
+
*/
|
|
172
|
+
dashboardUrl: z.ZodDefault<z.ZodString>;
|
|
160
173
|
/** Persona identification. Optional at init; can be set later via identifyPersona(). */
|
|
161
174
|
persona: z.ZodOptional<z.ZodObject<{
|
|
162
175
|
/** Persona slug matching the deployment's registered persona. */
|
|
@@ -230,7 +243,13 @@ declare const ProbeConfigSchema: z.ZodObject<{
|
|
|
230
243
|
outcomeHook: z.ZodOptional<z.ZodObject<{
|
|
231
244
|
/** Type of outcome the deployment tracks. */
|
|
232
245
|
type: z.ZodEnum<["numeric", "categorical", "boolean", "duration"]>;
|
|
233
|
-
/**
|
|
246
|
+
/**
|
|
247
|
+
* Callback that resolves the outcome for a completed session. Invoked by
|
|
248
|
+
* Probe.endSession() with the session's externalId; a non-null result
|
|
249
|
+
* lands as an outcome event on the session. Not invoked for sessions
|
|
250
|
+
* swept server-side (no explicit endSession) — use emitOutcomeEvent()
|
|
251
|
+
* for outcomes that become known out of band.
|
|
252
|
+
*/
|
|
234
253
|
resolve: z.ZodType<(sessionId: string) => Promise<OutcomeResult | null>, z.ZodTypeDef, (sessionId: string) => Promise<OutcomeResult | null>>;
|
|
235
254
|
}, "strip", z.ZodTypeAny, {
|
|
236
255
|
type: "boolean" | "numeric" | "categorical" | "duration";
|
|
@@ -253,6 +272,7 @@ declare const ProbeConfigSchema: z.ZodObject<{
|
|
|
253
272
|
apiKey: string;
|
|
254
273
|
deployment: string;
|
|
255
274
|
endpoint: string;
|
|
275
|
+
dashboardUrl: string;
|
|
256
276
|
sampleRate: number;
|
|
257
277
|
debug: boolean;
|
|
258
278
|
persona?: {
|
|
@@ -277,6 +297,7 @@ declare const ProbeConfigSchema: z.ZodObject<{
|
|
|
277
297
|
apiKey: string;
|
|
278
298
|
deployment: string;
|
|
279
299
|
endpoint?: string | undefined;
|
|
300
|
+
dashboardUrl?: string | undefined;
|
|
280
301
|
persona?: {
|
|
281
302
|
slug: string;
|
|
282
303
|
rice?: string | Record<string, unknown> | undefined;
|
|
@@ -549,11 +570,24 @@ type IngestClientOptions = {
|
|
|
549
570
|
apiKey: string;
|
|
550
571
|
endpoint: string;
|
|
551
572
|
debug: boolean;
|
|
573
|
+
/** Dashboard base URL for the one-time first-capture announcement. */
|
|
574
|
+
dashboardUrl?: string;
|
|
552
575
|
};
|
|
553
576
|
declare class IngestClient {
|
|
554
577
|
private apiKey;
|
|
555
578
|
private endpoint;
|
|
556
579
|
private debug;
|
|
580
|
+
private dashboardUrl;
|
|
581
|
+
/**
|
|
582
|
+
* First-trace permalink ritual (PLAN-2026-07-14-adoptions A1): the first
|
|
583
|
+
* SUCCESSFUL sendTurn prints one stderr line with a dashboard permalink,
|
|
584
|
+
* then never again for the life of this client (one IngestClient per
|
|
585
|
+
* process — one `init` per process; a restart prints once more, accepted).
|
|
586
|
+
* Failed sends do not consume the announcement. Only sendTurn announces:
|
|
587
|
+
* flags, outcome events, and session-end sends never trigger it (a flag
|
|
588
|
+
* arriving before any turn is not a captured session).
|
|
589
|
+
*/
|
|
590
|
+
private firstCaptureAnnounced;
|
|
557
591
|
constructor(opts: IngestClientOptions);
|
|
558
592
|
/**
|
|
559
593
|
* Send a turn to the ingest API. Fire-and-log: errors are logged but never
|
|
@@ -792,8 +826,11 @@ declare class Probe {
|
|
|
792
826
|
* Chapter 8e′ Card 3 (version behavioral diff) — every customer would
|
|
793
827
|
* hash their own way and version-diff would become noise.
|
|
794
828
|
*
|
|
795
|
-
*
|
|
796
|
-
*
|
|
829
|
+
* The same hashing (src/hash.ts) backs the OTel processor's automatic
|
|
830
|
+
* version stamping: when autocapture sees a system message and the
|
|
831
|
+
* deployer set no environmentConfigHash, the session is stamped with
|
|
832
|
+
* the system prompt's hash and an 'auto:xxxxxxxx' label. Explicit
|
|
833
|
+
* startSession values always win over the auto stamp.
|
|
797
834
|
*/
|
|
798
835
|
static hashEnvironmentConfig(input: string): Promise<string>;
|
|
799
836
|
/**
|
|
@@ -801,6 +838,16 @@ declare class Probe {
|
|
|
801
838
|
* eventually), but recommended for long-running processes with many distinct
|
|
802
839
|
* conversations. If `input.endedReason` is provided the SDK sends an end
|
|
803
840
|
* event to ingest so the worker records the reason server-side.
|
|
841
|
+
*
|
|
842
|
+
* Resolve-on-close: if an outcome hook is registered (init `outcomeHook`
|
|
843
|
+
* or wireOutcomeHook()), endSession invokes `hook.resolve(externalId)`
|
|
844
|
+
* and, when it returns a non-null OutcomeResult, lands it as an outcome
|
|
845
|
+
* event (kind = result.outcome, detail carries type/value). This is the
|
|
846
|
+
* ONLY place the SDK invokes the hook — there are no client-side timers,
|
|
847
|
+
* so sessions that are never ended explicitly are swept server-side as
|
|
848
|
+
* `timeout` and receive no hook-resolved outcome; land those via
|
|
849
|
+
* emitOutcomeEvent() from wherever the outcome becomes known. Error-safe:
|
|
850
|
+
* a throwing or rejecting hook never breaks endSession.
|
|
804
851
|
*/
|
|
805
852
|
static endSession(externalId: string | SessionEndInput): Promise<void>;
|
|
806
853
|
/**
|
|
@@ -833,9 +880,11 @@ declare class Probe {
|
|
|
833
880
|
static startSessionWithContext(input: SessionStart, includes?: ClientContextIncludes): void;
|
|
834
881
|
/**
|
|
835
882
|
* Register the outcome hook: declares the deployment's outcome type and
|
|
836
|
-
* the resolver for it.
|
|
837
|
-
*
|
|
838
|
-
*
|
|
883
|
+
* the resolver for it. The hook's `resolve(sessionExternalId)` is invoked
|
|
884
|
+
* by endSession() — explicit close is the only trigger; the SDK runs no
|
|
885
|
+
* client-side timers. A non-null OutcomeResult lands as an outcome event
|
|
886
|
+
* on the session. Sessions that are never ended explicitly are swept
|
|
887
|
+
* server-side and never resolve through the hook; land those outcomes via
|
|
839
888
|
* emitOutcomeEvent() from the place where the outcome becomes known
|
|
840
889
|
* (webhook, CRM callback); it joins back by sessionExternalId.
|
|
841
890
|
*/
|
package/dist/index.d.ts
CHANGED
|
@@ -135,7 +135,13 @@ type PersonaConfig = z.infer<typeof PersonaConfigSchema>;
|
|
|
135
135
|
declare const OutcomeHookSchema: z.ZodObject<{
|
|
136
136
|
/** Type of outcome the deployment tracks. */
|
|
137
137
|
type: z.ZodEnum<["numeric", "categorical", "boolean", "duration"]>;
|
|
138
|
-
/**
|
|
138
|
+
/**
|
|
139
|
+
* Callback that resolves the outcome for a completed session. Invoked by
|
|
140
|
+
* Probe.endSession() with the session's externalId; a non-null result
|
|
141
|
+
* lands as an outcome event on the session. Not invoked for sessions
|
|
142
|
+
* swept server-side (no explicit endSession) — use emitOutcomeEvent()
|
|
143
|
+
* for outcomes that become known out of band.
|
|
144
|
+
*/
|
|
139
145
|
resolve: z.ZodType<(sessionId: string) => Promise<OutcomeResult | null>, z.ZodTypeDef, (sessionId: string) => Promise<OutcomeResult | null>>;
|
|
140
146
|
}, "strip", z.ZodTypeAny, {
|
|
141
147
|
type: "boolean" | "numeric" | "categorical" | "duration";
|
|
@@ -157,6 +163,13 @@ declare const ProbeConfigSchema: z.ZodObject<{
|
|
|
157
163
|
deployment: z.ZodString;
|
|
158
164
|
/** Ingest endpoint. Defaults to https://ingest.probe.io. */
|
|
159
165
|
endpoint: z.ZodDefault<z.ZodString>;
|
|
166
|
+
/**
|
|
167
|
+
* Dashboard base URL used by the one-time first-capture announcement
|
|
168
|
+
* (the stderr permalink printed after the first successful send).
|
|
169
|
+
* Self-hosted or staging dashboards override it the same way `endpoint`
|
|
170
|
+
* is overridden.
|
|
171
|
+
*/
|
|
172
|
+
dashboardUrl: z.ZodDefault<z.ZodString>;
|
|
160
173
|
/** Persona identification. Optional at init; can be set later via identifyPersona(). */
|
|
161
174
|
persona: z.ZodOptional<z.ZodObject<{
|
|
162
175
|
/** Persona slug matching the deployment's registered persona. */
|
|
@@ -230,7 +243,13 @@ declare const ProbeConfigSchema: z.ZodObject<{
|
|
|
230
243
|
outcomeHook: z.ZodOptional<z.ZodObject<{
|
|
231
244
|
/** Type of outcome the deployment tracks. */
|
|
232
245
|
type: z.ZodEnum<["numeric", "categorical", "boolean", "duration"]>;
|
|
233
|
-
/**
|
|
246
|
+
/**
|
|
247
|
+
* Callback that resolves the outcome for a completed session. Invoked by
|
|
248
|
+
* Probe.endSession() with the session's externalId; a non-null result
|
|
249
|
+
* lands as an outcome event on the session. Not invoked for sessions
|
|
250
|
+
* swept server-side (no explicit endSession) — use emitOutcomeEvent()
|
|
251
|
+
* for outcomes that become known out of band.
|
|
252
|
+
*/
|
|
234
253
|
resolve: z.ZodType<(sessionId: string) => Promise<OutcomeResult | null>, z.ZodTypeDef, (sessionId: string) => Promise<OutcomeResult | null>>;
|
|
235
254
|
}, "strip", z.ZodTypeAny, {
|
|
236
255
|
type: "boolean" | "numeric" | "categorical" | "duration";
|
|
@@ -253,6 +272,7 @@ declare const ProbeConfigSchema: z.ZodObject<{
|
|
|
253
272
|
apiKey: string;
|
|
254
273
|
deployment: string;
|
|
255
274
|
endpoint: string;
|
|
275
|
+
dashboardUrl: string;
|
|
256
276
|
sampleRate: number;
|
|
257
277
|
debug: boolean;
|
|
258
278
|
persona?: {
|
|
@@ -277,6 +297,7 @@ declare const ProbeConfigSchema: z.ZodObject<{
|
|
|
277
297
|
apiKey: string;
|
|
278
298
|
deployment: string;
|
|
279
299
|
endpoint?: string | undefined;
|
|
300
|
+
dashboardUrl?: string | undefined;
|
|
280
301
|
persona?: {
|
|
281
302
|
slug: string;
|
|
282
303
|
rice?: string | Record<string, unknown> | undefined;
|
|
@@ -549,11 +570,24 @@ type IngestClientOptions = {
|
|
|
549
570
|
apiKey: string;
|
|
550
571
|
endpoint: string;
|
|
551
572
|
debug: boolean;
|
|
573
|
+
/** Dashboard base URL for the one-time first-capture announcement. */
|
|
574
|
+
dashboardUrl?: string;
|
|
552
575
|
};
|
|
553
576
|
declare class IngestClient {
|
|
554
577
|
private apiKey;
|
|
555
578
|
private endpoint;
|
|
556
579
|
private debug;
|
|
580
|
+
private dashboardUrl;
|
|
581
|
+
/**
|
|
582
|
+
* First-trace permalink ritual (PLAN-2026-07-14-adoptions A1): the first
|
|
583
|
+
* SUCCESSFUL sendTurn prints one stderr line with a dashboard permalink,
|
|
584
|
+
* then never again for the life of this client (one IngestClient per
|
|
585
|
+
* process — one `init` per process; a restart prints once more, accepted).
|
|
586
|
+
* Failed sends do not consume the announcement. Only sendTurn announces:
|
|
587
|
+
* flags, outcome events, and session-end sends never trigger it (a flag
|
|
588
|
+
* arriving before any turn is not a captured session).
|
|
589
|
+
*/
|
|
590
|
+
private firstCaptureAnnounced;
|
|
557
591
|
constructor(opts: IngestClientOptions);
|
|
558
592
|
/**
|
|
559
593
|
* Send a turn to the ingest API. Fire-and-log: errors are logged but never
|
|
@@ -792,8 +826,11 @@ declare class Probe {
|
|
|
792
826
|
* Chapter 8e′ Card 3 (version behavioral diff) — every customer would
|
|
793
827
|
* hash their own way and version-diff would become noise.
|
|
794
828
|
*
|
|
795
|
-
*
|
|
796
|
-
*
|
|
829
|
+
* The same hashing (src/hash.ts) backs the OTel processor's automatic
|
|
830
|
+
* version stamping: when autocapture sees a system message and the
|
|
831
|
+
* deployer set no environmentConfigHash, the session is stamped with
|
|
832
|
+
* the system prompt's hash and an 'auto:xxxxxxxx' label. Explicit
|
|
833
|
+
* startSession values always win over the auto stamp.
|
|
797
834
|
*/
|
|
798
835
|
static hashEnvironmentConfig(input: string): Promise<string>;
|
|
799
836
|
/**
|
|
@@ -801,6 +838,16 @@ declare class Probe {
|
|
|
801
838
|
* eventually), but recommended for long-running processes with many distinct
|
|
802
839
|
* conversations. If `input.endedReason` is provided the SDK sends an end
|
|
803
840
|
* event to ingest so the worker records the reason server-side.
|
|
841
|
+
*
|
|
842
|
+
* Resolve-on-close: if an outcome hook is registered (init `outcomeHook`
|
|
843
|
+
* or wireOutcomeHook()), endSession invokes `hook.resolve(externalId)`
|
|
844
|
+
* and, when it returns a non-null OutcomeResult, lands it as an outcome
|
|
845
|
+
* event (kind = result.outcome, detail carries type/value). This is the
|
|
846
|
+
* ONLY place the SDK invokes the hook — there are no client-side timers,
|
|
847
|
+
* so sessions that are never ended explicitly are swept server-side as
|
|
848
|
+
* `timeout` and receive no hook-resolved outcome; land those via
|
|
849
|
+
* emitOutcomeEvent() from wherever the outcome becomes known. Error-safe:
|
|
850
|
+
* a throwing or rejecting hook never breaks endSession.
|
|
804
851
|
*/
|
|
805
852
|
static endSession(externalId: string | SessionEndInput): Promise<void>;
|
|
806
853
|
/**
|
|
@@ -833,9 +880,11 @@ declare class Probe {
|
|
|
833
880
|
static startSessionWithContext(input: SessionStart, includes?: ClientContextIncludes): void;
|
|
834
881
|
/**
|
|
835
882
|
* Register the outcome hook: declares the deployment's outcome type and
|
|
836
|
-
* the resolver for it.
|
|
837
|
-
*
|
|
838
|
-
*
|
|
883
|
+
* the resolver for it. The hook's `resolve(sessionExternalId)` is invoked
|
|
884
|
+
* by endSession() — explicit close is the only trigger; the SDK runs no
|
|
885
|
+
* client-side timers. A non-null OutcomeResult lands as an outcome event
|
|
886
|
+
* on the session. Sessions that are never ended explicitly are swept
|
|
887
|
+
* server-side and never resolve through the hook; land those outcomes via
|
|
839
888
|
* emitOutcomeEvent() from the place where the outcome becomes known
|
|
840
889
|
* (webhook, CRM callback); it joins back by sessionExternalId.
|
|
841
890
|
*/
|
package/dist/index.js
CHANGED
|
@@ -2,14 +2,27 @@ import { z } from 'zod';
|
|
|
2
2
|
import { propagation, context, trace } from '@opentelemetry/api';
|
|
3
3
|
|
|
4
4
|
// src/client.ts
|
|
5
|
+
var DEFAULT_DASHBOARD_URL = "https://field.cultured.computer/dashboard";
|
|
5
6
|
var IngestClient = class {
|
|
6
7
|
apiKey;
|
|
7
8
|
endpoint;
|
|
8
9
|
debug;
|
|
10
|
+
dashboardUrl;
|
|
11
|
+
/**
|
|
12
|
+
* First-trace permalink ritual (PLAN-2026-07-14-adoptions A1): the first
|
|
13
|
+
* SUCCESSFUL sendTurn prints one stderr line with a dashboard permalink,
|
|
14
|
+
* then never again for the life of this client (one IngestClient per
|
|
15
|
+
* process — one `init` per process; a restart prints once more, accepted).
|
|
16
|
+
* Failed sends do not consume the announcement. Only sendTurn announces:
|
|
17
|
+
* flags, outcome events, and session-end sends never trigger it (a flag
|
|
18
|
+
* arriving before any turn is not a captured session).
|
|
19
|
+
*/
|
|
20
|
+
firstCaptureAnnounced = false;
|
|
9
21
|
constructor(opts) {
|
|
10
22
|
this.apiKey = opts.apiKey;
|
|
11
23
|
this.endpoint = opts.endpoint.replace(/\/$/, "");
|
|
12
24
|
this.debug = opts.debug;
|
|
25
|
+
this.dashboardUrl = (opts.dashboardUrl ?? DEFAULT_DASHBOARD_URL).replace(/\/$/, "");
|
|
13
26
|
}
|
|
14
27
|
/**
|
|
15
28
|
* Send a turn to the ingest API. Fire-and-log: errors are logged but never
|
|
@@ -29,6 +42,12 @@ var IngestClient = class {
|
|
|
29
42
|
this.log(`[probe] ingest failed status=${response.status}`);
|
|
30
43
|
return;
|
|
31
44
|
}
|
|
45
|
+
if (!this.firstCaptureAnnounced) {
|
|
46
|
+
this.firstCaptureAnnounced = true;
|
|
47
|
+
console.error(
|
|
48
|
+
`Sight: first session captured -> ${this.dashboardUrl}/sessions/${encodeURIComponent(turn.sessionExternalId)}`
|
|
49
|
+
);
|
|
50
|
+
}
|
|
32
51
|
if (this.debug) {
|
|
33
52
|
const body = await response.json();
|
|
34
53
|
this.log(`[probe] ingest ok id=${body.pendingId ?? "?"}`);
|
|
@@ -172,7 +191,13 @@ var PersonaConfigSchema = z.object({
|
|
|
172
191
|
var OutcomeHookSchema = z.object({
|
|
173
192
|
/** Type of outcome the deployment tracks. */
|
|
174
193
|
type: z.enum(["numeric", "categorical", "boolean", "duration"]),
|
|
175
|
-
/**
|
|
194
|
+
/**
|
|
195
|
+
* Callback that resolves the outcome for a completed session. Invoked by
|
|
196
|
+
* Probe.endSession() with the session's externalId; a non-null result
|
|
197
|
+
* lands as an outcome event on the session. Not invoked for sessions
|
|
198
|
+
* swept server-side (no explicit endSession) — use emitOutcomeEvent()
|
|
199
|
+
* for outcomes that become known out of band.
|
|
200
|
+
*/
|
|
176
201
|
resolve: z.custom()
|
|
177
202
|
});
|
|
178
203
|
var ProbeConfigSchema = z.object({
|
|
@@ -182,6 +207,13 @@ var ProbeConfigSchema = z.object({
|
|
|
182
207
|
deployment: z.string().min(1),
|
|
183
208
|
/** Ingest endpoint. Defaults to https://ingest.probe.io. */
|
|
184
209
|
endpoint: z.string().url().default("https://ingest.probe.io"),
|
|
210
|
+
/**
|
|
211
|
+
* Dashboard base URL used by the one-time first-capture announcement
|
|
212
|
+
* (the stderr permalink printed after the first successful send).
|
|
213
|
+
* Self-hosted or staging dashboards override it the same way `endpoint`
|
|
214
|
+
* is overridden.
|
|
215
|
+
*/
|
|
216
|
+
dashboardUrl: z.string().url().default("https://field.cultured.computer/dashboard"),
|
|
185
217
|
/** Persona identification. Optional at init; can be set later via identifyPersona(). */
|
|
186
218
|
persona: PersonaConfigSchema.optional(),
|
|
187
219
|
/** Outcome hook. Optional at init; can be set later via wireOutcomeHook(). */
|
|
@@ -316,6 +348,17 @@ function collectClientContext(includes = DEFAULT_INCLUDES) {
|
|
|
316
348
|
}
|
|
317
349
|
return out;
|
|
318
350
|
}
|
|
351
|
+
|
|
352
|
+
// src/hash.ts
|
|
353
|
+
async function hashEnvironmentConfig(input) {
|
|
354
|
+
if (typeof globalThis.crypto !== "undefined" && globalThis.crypto.subtle) {
|
|
355
|
+
const bytes = new TextEncoder().encode(input);
|
|
356
|
+
const digest = await globalThis.crypto.subtle.digest("SHA-256", bytes);
|
|
357
|
+
return Array.from(new Uint8Array(digest)).map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
358
|
+
}
|
|
359
|
+
const { createHash } = await import('crypto');
|
|
360
|
+
return createHash("sha256").update(input).digest("hex");
|
|
361
|
+
}
|
|
319
362
|
var SESSION_BAGGAGE_KEY = "probe.session_id";
|
|
320
363
|
var contextManagerRegistered = false;
|
|
321
364
|
async function ensureContextManager() {
|
|
@@ -435,6 +478,92 @@ function buildPayload(span) {
|
|
|
435
478
|
}
|
|
436
479
|
};
|
|
437
480
|
}
|
|
481
|
+
function messageText(content) {
|
|
482
|
+
if (typeof content === "string") return content;
|
|
483
|
+
if (Array.isArray(content)) {
|
|
484
|
+
const parts = [];
|
|
485
|
+
for (const part of content) {
|
|
486
|
+
if (typeof part === "string") {
|
|
487
|
+
parts.push(part);
|
|
488
|
+
} else if (part && typeof part === "object") {
|
|
489
|
+
const p = part;
|
|
490
|
+
const text = p["content"] ?? p["text"];
|
|
491
|
+
if (typeof text === "string") parts.push(text);
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
return parts.length > 0 ? parts.join("\n") : void 0;
|
|
495
|
+
}
|
|
496
|
+
return void 0;
|
|
497
|
+
}
|
|
498
|
+
function systemFromMessagesJson(raw) {
|
|
499
|
+
try {
|
|
500
|
+
const parsed = JSON.parse(raw);
|
|
501
|
+
if (!Array.isArray(parsed)) return void 0;
|
|
502
|
+
for (const msg of parsed) {
|
|
503
|
+
if (msg && typeof msg === "object") {
|
|
504
|
+
const m = msg;
|
|
505
|
+
if (m["role"] === "system") {
|
|
506
|
+
const text = messageText(m["content"] ?? m["parts"]);
|
|
507
|
+
if (text) return text;
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
} catch {
|
|
512
|
+
}
|
|
513
|
+
return void 0;
|
|
514
|
+
}
|
|
515
|
+
function extractSystemPromptText(attrs) {
|
|
516
|
+
const instructions = attrs["gen_ai.system_instructions"];
|
|
517
|
+
if (typeof instructions === "string" && instructions.length > 0) {
|
|
518
|
+
try {
|
|
519
|
+
const parsed = JSON.parse(instructions);
|
|
520
|
+
const text = messageText(parsed);
|
|
521
|
+
if (text) return text;
|
|
522
|
+
} catch {
|
|
523
|
+
return instructions;
|
|
524
|
+
}
|
|
525
|
+
return instructions;
|
|
526
|
+
}
|
|
527
|
+
const inputMessages = asString(attrs["gen_ai.input.messages"]);
|
|
528
|
+
if (inputMessages) {
|
|
529
|
+
const text = systemFromMessagesJson(inputMessages);
|
|
530
|
+
if (text) return text;
|
|
531
|
+
}
|
|
532
|
+
for (let i = 0; ; i++) {
|
|
533
|
+
const role = attrs[`gen_ai.prompt.${i}.role`];
|
|
534
|
+
if (role === void 0) break;
|
|
535
|
+
if (role === "system") {
|
|
536
|
+
const content = asString(attrs[`gen_ai.prompt.${i}.content`]);
|
|
537
|
+
if (content) return content;
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
const aiMessages = asString(attrs["ai.prompt.messages"]);
|
|
541
|
+
if (aiMessages) {
|
|
542
|
+
const text = systemFromMessagesJson(aiMessages);
|
|
543
|
+
if (text) return text;
|
|
544
|
+
}
|
|
545
|
+
const aiPrompt = asString(attrs["ai.prompt"]);
|
|
546
|
+
if (aiPrompt) {
|
|
547
|
+
try {
|
|
548
|
+
const parsed = JSON.parse(aiPrompt);
|
|
549
|
+
if (parsed && typeof parsed === "object") {
|
|
550
|
+
const system = parsed["system"];
|
|
551
|
+
if (typeof system === "string" && system.length > 0) return system;
|
|
552
|
+
}
|
|
553
|
+
} catch {
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
return void 0;
|
|
557
|
+
}
|
|
558
|
+
async function maybeAutoStampEnvironmentConfig(session, attrs) {
|
|
559
|
+
if (session.environmentConfigHash != null) return;
|
|
560
|
+
const systemText = extractSystemPromptText(attrs);
|
|
561
|
+
if (!systemText) return;
|
|
562
|
+
const hash = await hashEnvironmentConfig(systemText);
|
|
563
|
+
if (session.environmentConfigHash != null) return;
|
|
564
|
+
session.environmentConfigHash = hash;
|
|
565
|
+
session.environmentConfigLabel = `auto:${hash.slice(0, 8)}`;
|
|
566
|
+
}
|
|
438
567
|
function resolveSessionAndTurn(state2, sessionId) {
|
|
439
568
|
let session = state2.sessions.get(sessionId);
|
|
440
569
|
if (!session) {
|
|
@@ -442,13 +571,16 @@ function resolveSessionAndTurn(state2, sessionId) {
|
|
|
442
571
|
externalId: sessionId,
|
|
443
572
|
turnIndex: 0,
|
|
444
573
|
startedAt: Date.now(),
|
|
445
|
-
personaSlug: state2.persona?.slug ?? null
|
|
574
|
+
personaSlug: state2.persona?.slug ?? null,
|
|
575
|
+
environmentConfigHash: null,
|
|
576
|
+
environmentConfigLabel: null
|
|
446
577
|
};
|
|
447
578
|
state2.sessions.set(sessionId, session);
|
|
448
579
|
}
|
|
449
580
|
const turnIndex = session.turnIndex;
|
|
450
581
|
session.turnIndex += 1;
|
|
451
582
|
return {
|
|
583
|
+
session,
|
|
452
584
|
sessionExternalId: sessionId,
|
|
453
585
|
turnIndex,
|
|
454
586
|
personaSlug: session.personaSlug ?? state2.persona?.slug ?? null
|
|
@@ -479,7 +611,7 @@ var ProbeSpanProcessor = class {
|
|
|
479
611
|
}
|
|
480
612
|
return;
|
|
481
613
|
}
|
|
482
|
-
const { sessionExternalId, turnIndex, personaSlug } = resolveSessionAndTurn(
|
|
614
|
+
const { session, sessionExternalId, turnIndex, personaSlug } = resolveSessionAndTurn(
|
|
483
615
|
this.state,
|
|
484
616
|
sessionId
|
|
485
617
|
);
|
|
@@ -490,7 +622,10 @@ var ProbeSpanProcessor = class {
|
|
|
490
622
|
personaSlug: personaSlug ?? void 0,
|
|
491
623
|
payload: buildPayload(span)
|
|
492
624
|
};
|
|
493
|
-
|
|
625
|
+
(async () => {
|
|
626
|
+
await maybeAutoStampEnvironmentConfig(session, a);
|
|
627
|
+
await this.captureFn(captured);
|
|
628
|
+
})().catch((err) => {
|
|
494
629
|
if (this.state.config.debug) {
|
|
495
630
|
console.error(
|
|
496
631
|
`[probe/otel] capture error: ${err instanceof Error ? err.message : err}`
|
|
@@ -997,7 +1132,8 @@ var Probe = class {
|
|
|
997
1132
|
const client = new IngestClient({
|
|
998
1133
|
apiKey: validated.apiKey,
|
|
999
1134
|
endpoint: validated.endpoint,
|
|
1000
|
-
debug: validated.debug
|
|
1135
|
+
debug: validated.debug,
|
|
1136
|
+
dashboardUrl: validated.dashboardUrl
|
|
1001
1137
|
});
|
|
1002
1138
|
state = {
|
|
1003
1139
|
config: validated,
|
|
@@ -1136,28 +1272,57 @@ var Probe = class {
|
|
|
1136
1272
|
* Chapter 8e′ Card 3 (version behavioral diff) — every customer would
|
|
1137
1273
|
* hash their own way and version-diff would become noise.
|
|
1138
1274
|
*
|
|
1139
|
-
*
|
|
1140
|
-
*
|
|
1275
|
+
* The same hashing (src/hash.ts) backs the OTel processor's automatic
|
|
1276
|
+
* version stamping: when autocapture sees a system message and the
|
|
1277
|
+
* deployer set no environmentConfigHash, the session is stamped with
|
|
1278
|
+
* the system prompt's hash and an 'auto:xxxxxxxx' label. Explicit
|
|
1279
|
+
* startSession values always win over the auto stamp.
|
|
1141
1280
|
*/
|
|
1142
1281
|
static async hashEnvironmentConfig(input) {
|
|
1143
|
-
|
|
1144
|
-
const bytes = new TextEncoder().encode(input);
|
|
1145
|
-
const digest = await globalThis.crypto.subtle.digest("SHA-256", bytes);
|
|
1146
|
-
return Array.from(new Uint8Array(digest)).map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
1147
|
-
}
|
|
1148
|
-
const { createHash } = await import('crypto');
|
|
1149
|
-
return createHash("sha256").update(input).digest("hex");
|
|
1282
|
+
return hashEnvironmentConfig(input);
|
|
1150
1283
|
}
|
|
1151
1284
|
/**
|
|
1152
1285
|
* End a session and release its in-memory entry. Optional (sessions time out
|
|
1153
1286
|
* eventually), but recommended for long-running processes with many distinct
|
|
1154
1287
|
* conversations. If `input.endedReason` is provided the SDK sends an end
|
|
1155
1288
|
* event to ingest so the worker records the reason server-side.
|
|
1289
|
+
*
|
|
1290
|
+
* Resolve-on-close: if an outcome hook is registered (init `outcomeHook`
|
|
1291
|
+
* or wireOutcomeHook()), endSession invokes `hook.resolve(externalId)`
|
|
1292
|
+
* and, when it returns a non-null OutcomeResult, lands it as an outcome
|
|
1293
|
+
* event (kind = result.outcome, detail carries type/value). This is the
|
|
1294
|
+
* ONLY place the SDK invokes the hook — there are no client-side timers,
|
|
1295
|
+
* so sessions that are never ended explicitly are swept server-side as
|
|
1296
|
+
* `timeout` and receive no hook-resolved outcome; land those via
|
|
1297
|
+
* emitOutcomeEvent() from wherever the outcome becomes known. Error-safe:
|
|
1298
|
+
* a throwing or rejecting hook never breaks endSession.
|
|
1156
1299
|
*/
|
|
1157
1300
|
static async endSession(externalId) {
|
|
1158
1301
|
if (state === null) return;
|
|
1159
1302
|
const externalIdStr = typeof externalId === "string" ? externalId : externalId.externalId;
|
|
1160
1303
|
const endedReason = typeof externalId === "string" ? void 0 : externalId.endedReason;
|
|
1304
|
+
if (state.outcomeHook) {
|
|
1305
|
+
try {
|
|
1306
|
+
const result = await state.outcomeHook.resolve(externalIdStr);
|
|
1307
|
+
if (result && state !== null) {
|
|
1308
|
+
await this.emitOutcomeEvent({
|
|
1309
|
+
sessionExternalId: externalIdStr,
|
|
1310
|
+
kind: result.outcome,
|
|
1311
|
+
detail: {
|
|
1312
|
+
type: result.type,
|
|
1313
|
+
value: result.value,
|
|
1314
|
+
resolvedBy: "outcome_hook"
|
|
1315
|
+
}
|
|
1316
|
+
});
|
|
1317
|
+
}
|
|
1318
|
+
} catch (err) {
|
|
1319
|
+
console.warn(
|
|
1320
|
+
"[probe] outcome hook resolve failed:",
|
|
1321
|
+
err instanceof Error ? err.message : err
|
|
1322
|
+
);
|
|
1323
|
+
}
|
|
1324
|
+
}
|
|
1325
|
+
if (state === null) return;
|
|
1161
1326
|
if (endedReason) {
|
|
1162
1327
|
await state.client.sendSessionEnd(state.config.deployment, { externalId: externalIdStr, endedReason }).catch((err) => {
|
|
1163
1328
|
console.warn("[probe] sendSessionEnd failed:", err instanceof Error ? err.message : err);
|
|
@@ -1248,9 +1413,11 @@ var Probe = class {
|
|
|
1248
1413
|
}
|
|
1249
1414
|
/**
|
|
1250
1415
|
* Register the outcome hook: declares the deployment's outcome type and
|
|
1251
|
-
* the resolver for it.
|
|
1252
|
-
*
|
|
1253
|
-
*
|
|
1416
|
+
* the resolver for it. The hook's `resolve(sessionExternalId)` is invoked
|
|
1417
|
+
* by endSession() — explicit close is the only trigger; the SDK runs no
|
|
1418
|
+
* client-side timers. A non-null OutcomeResult lands as an outcome event
|
|
1419
|
+
* on the session. Sessions that are never ended explicitly are swept
|
|
1420
|
+
* server-side and never resolve through the hook; land those outcomes via
|
|
1254
1421
|
* emitOutcomeEvent() from the place where the outcome becomes known
|
|
1255
1422
|
* (webhook, CRM callback); it joins back by sessionExternalId.
|
|
1256
1423
|
*/
|