neosigma-sdk 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 +7 -7
- package/dist/context.d.ts +5 -5
- package/dist/context.js +5 -5
- package/dist/events.d.ts +2 -2
- package/dist/events.js +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +3 -3
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# neosigma-sdk
|
|
2
2
|
|
|
3
3
|
NeoSigma TypeScript SDK. Emit product events that join your agent traces on
|
|
4
|
-
`
|
|
4
|
+
`turn_id`, in NeoSigma's own telemetry store.
|
|
5
5
|
|
|
6
6
|
## Quick start
|
|
7
7
|
|
|
@@ -11,7 +11,7 @@ import { init, trace, capture, identify, installShutdownHandlers } from "neosigm
|
|
|
11
11
|
init(); // reads NEOSIGMA_API_KEY / NEOSIGMA_EVENTS_ENDPOINT; dark without a key
|
|
12
12
|
installShutdownHandlers(); // flush queued events on SIGTERM/SIGINT
|
|
13
13
|
|
|
14
|
-
await trace({
|
|
14
|
+
await trace({ turnId: turn.id, distinctId: userId }, async () => {
|
|
15
15
|
identify(userId, { plan: "pro" });
|
|
16
16
|
capture("message sent", { length: text.length });
|
|
17
17
|
});
|
|
@@ -19,9 +19,9 @@ await trace({ runId: run.id, distinctId: userId }, async () => {
|
|
|
19
19
|
|
|
20
20
|
## How it joins
|
|
21
21
|
|
|
22
|
-
`
|
|
23
|
-
`trace({
|
|
24
|
-
events sit next to the model's spans for the same
|
|
22
|
+
`turn_id` is the durable join key back to the agent's spans. Bind it once with
|
|
23
|
+
`trace({ turnId })`; every `capture()` inside that scope carries it, so product
|
|
24
|
+
events sit next to the model's spans for the same turn in ClickHouse. The ingest
|
|
25
25
|
resolves your API key to an `org_id` server-side (the client never sends it).
|
|
26
26
|
|
|
27
27
|
## Configuration
|
|
@@ -49,5 +49,5 @@ the environment.
|
|
|
49
49
|
## Correlation across processes
|
|
50
50
|
|
|
51
51
|
`AsyncLocalStorage` does not cross a queue or process hop. When you hand work to
|
|
52
|
-
another service or a background job, pass
|
|
53
|
-
on the far side with `trace({
|
|
52
|
+
another service or a background job, pass the turn id in the payload and re-bind it
|
|
53
|
+
on the far side with `trace({ turnId })` (the Python SDK follows the same rule).
|
package/dist/context.d.ts
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
|
-
/** The ambient
|
|
1
|
+
/** The ambient turn-correlation ids: the join spine for events + spans. */
|
|
2
2
|
export interface Correlation {
|
|
3
|
-
|
|
3
|
+
turnId: string;
|
|
4
4
|
distinctId: string;
|
|
5
5
|
sessionId: string;
|
|
6
6
|
}
|
|
7
|
-
export declare function
|
|
7
|
+
export declare function currentTurnId(): string;
|
|
8
8
|
export declare function currentDistinctId(): string;
|
|
9
9
|
export declare function currentSessionId(): string;
|
|
10
10
|
/**
|
|
11
11
|
* Run `fn` with the given correlation ids bound (only non-empty ones overlay the
|
|
12
12
|
* parent scope), returning `fn`'s value. AsyncLocalStorage propagates the store
|
|
13
13
|
* across awaits within this async context, but NOT across a process/queue hop:
|
|
14
|
-
* thread
|
|
14
|
+
* thread turnId into the job payload and re-bind it on the far side, exactly as
|
|
15
15
|
* the Python SDK requires.
|
|
16
16
|
*
|
|
17
17
|
* Each distinct actor should own its own `trace()` frame. `setDistinctId` mutates
|
|
@@ -20,7 +20,7 @@ export declare function currentSessionId(): string;
|
|
|
20
20
|
* wins); give each its own `trace({ distinctId })` frame instead.
|
|
21
21
|
*/
|
|
22
22
|
export declare function trace<T>(opts: {
|
|
23
|
-
|
|
23
|
+
turnId?: string;
|
|
24
24
|
distinctId?: string;
|
|
25
25
|
sessionId?: string;
|
|
26
26
|
}, fn: () => T): T;
|
package/dist/context.js
CHANGED
|
@@ -8,8 +8,8 @@ let outsideScopeDrops = 0;
|
|
|
8
8
|
function store() {
|
|
9
9
|
return als.getStore();
|
|
10
10
|
}
|
|
11
|
-
export function
|
|
12
|
-
return store()?.
|
|
11
|
+
export function currentTurnId() {
|
|
12
|
+
return store()?.turnId ?? "";
|
|
13
13
|
}
|
|
14
14
|
export function currentDistinctId() {
|
|
15
15
|
return store()?.distinctId ?? "";
|
|
@@ -21,7 +21,7 @@ export function currentSessionId() {
|
|
|
21
21
|
* Run `fn` with the given correlation ids bound (only non-empty ones overlay the
|
|
22
22
|
* parent scope), returning `fn`'s value. AsyncLocalStorage propagates the store
|
|
23
23
|
* across awaits within this async context, but NOT across a process/queue hop:
|
|
24
|
-
* thread
|
|
24
|
+
* thread turnId into the job payload and re-bind it on the far side, exactly as
|
|
25
25
|
* the Python SDK requires.
|
|
26
26
|
*
|
|
27
27
|
* Each distinct actor should own its own `trace()` frame. `setDistinctId` mutates
|
|
@@ -32,7 +32,7 @@ export function currentSessionId() {
|
|
|
32
32
|
export function trace(opts, fn) {
|
|
33
33
|
const parent = store();
|
|
34
34
|
const merged = {
|
|
35
|
-
|
|
35
|
+
turnId: opts.turnId || parent?.turnId || "",
|
|
36
36
|
distinctId: opts.distinctId || parent?.distinctId || "",
|
|
37
37
|
sessionId: opts.sessionId || parent?.sessionId || "",
|
|
38
38
|
};
|
|
@@ -58,6 +58,6 @@ export function setDistinctId(distinctId) {
|
|
|
58
58
|
if (crossedWarnBoundary(prev, 1)) {
|
|
59
59
|
console.warn(`[neosigma] setDistinctId/identify called outside a trace() scope ` +
|
|
60
60
|
`(${outsideScopeDrops} dropped); ignored. Wrap request handling in ` +
|
|
61
|
-
`trace({
|
|
61
|
+
`trace({ turnId }, ...) so identity stays per-request.`);
|
|
62
62
|
}
|
|
63
63
|
}
|
package/dist/events.d.ts
CHANGED
|
@@ -21,8 +21,8 @@ export interface ProductEvent {
|
|
|
21
21
|
event_name: string;
|
|
22
22
|
/** Analytics actor; "" when anonymous. */
|
|
23
23
|
distinct_id: string;
|
|
24
|
-
/** Durable join key back to
|
|
25
|
-
|
|
24
|
+
/** Durable join key back to the turn's spans; "" outside a turn scope. */
|
|
25
|
+
turn_id: string;
|
|
26
26
|
/** Optional framework/session id. */
|
|
27
27
|
session_id: string;
|
|
28
28
|
/** Best-effort OTel trace id; "" when no active span. */
|
package/dist/events.js
CHANGED
package/dist/index.d.ts
CHANGED
|
@@ -14,7 +14,7 @@ export declare function init(overrides?: {
|
|
|
14
14
|
enabled?: boolean;
|
|
15
15
|
}): void;
|
|
16
16
|
/**
|
|
17
|
-
* Emit a product event, stamped with the ambient
|
|
17
|
+
* Emit a product event, stamped with the ambient turn-correlation ids (bound by
|
|
18
18
|
* trace()). Mints an idempotency event_uuid and a call-site timestamp, and
|
|
19
19
|
* sanitizes properties to wire-safe scalars. Fail-open: never throws.
|
|
20
20
|
*/
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { randomUUID } from "node:crypto";
|
|
2
2
|
import { isEnabled, resolveConfig } from "./config.js";
|
|
3
|
-
import { currentDistinctId,
|
|
3
|
+
import { currentDistinctId, currentTurnId, currentSessionId, setDistinctId, } from "./context.js";
|
|
4
4
|
import { coerceProperties } from "./properties.js";
|
|
5
5
|
import { HttpEventSink, getEventSink, resetSink, setEventSink } from "./sink.js";
|
|
6
6
|
import { nowNs } from "./time.js";
|
|
@@ -28,7 +28,7 @@ export function init(overrides) {
|
|
|
28
28
|
}
|
|
29
29
|
}
|
|
30
30
|
/**
|
|
31
|
-
* Emit a product event, stamped with the ambient
|
|
31
|
+
* Emit a product event, stamped with the ambient turn-correlation ids (bound by
|
|
32
32
|
* trace()). Mints an idempotency event_uuid and a call-site timestamp, and
|
|
33
33
|
* sanitizes properties to wire-safe scalars. Fail-open: never throws.
|
|
34
34
|
*/
|
|
@@ -38,7 +38,7 @@ export function capture(eventName, properties) {
|
|
|
38
38
|
event_uuid: randomUUID(),
|
|
39
39
|
event_name: eventName,
|
|
40
40
|
distinct_id: currentDistinctId(),
|
|
41
|
-
|
|
41
|
+
turn_id: currentTurnId(),
|
|
42
42
|
session_id: currentSessionId(),
|
|
43
43
|
trace_id: "", // populated only if a TS OTel span integration is added later
|
|
44
44
|
timestamp_ns: nowNs(),
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "neosigma-sdk",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "NeoSigma TypeScript SDK: emit product events that join your agent traces on
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "NeoSigma TypeScript SDK: emit product events that join your agent traces on turn_id.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "https://www.neosigma.ai",
|
|
7
7
|
"repository": {
|