@cetra-ai/sdk 0.0.0-stage → 0.1.0-rc.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/LICENSE +21 -0
- package/README.md +46 -2
- package/dist/anthropic.d.ts +15 -0
- package/dist/anthropic.js +15 -0
- package/dist/browser.d.ts +42 -0
- package/dist/browser.js +75 -0
- package/dist/index.d.ts +585 -0
- package/dist/index.js +4056 -0
- package/dist/instrument-YhFFywwZ.d.ts +52 -0
- package/dist/langchain.d.ts +15 -0
- package/dist/langchain.js +25 -0
- package/dist/openai.d.ts +15 -0
- package/dist/openai.js +25 -0
- package/dist/web-C0F5sf2n.js +44 -0
- package/package.json +108 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 sebastiencs
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,47 @@
|
|
|
1
|
-
#
|
|
1
|
+
# cetra for TypeScript
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The `@cetra-ai/sdk` package (MIT, ESM only) is the TypeScript SDK of
|
|
4
|
+
[cetra](https://cetra.sh), an open-source LLM observability platform.
|
|
5
|
+
It is a thin layer over OpenTelemetry: tracing goes out as OTLP/HTTP protobuf,
|
|
6
|
+
scores and feedback over a small REST surface. Runs on Node ≥ 22.12, Bun, Deno
|
|
7
|
+
and edge runtimes (D77) — one `fetch`-based transport everywhere.
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
```sh
|
|
12
|
+
pnpm add @cetra-ai/sdk
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
The instrumentation helpers need their OpenInference packages (optional peers,
|
|
16
|
+
installed next to the SDK):
|
|
17
|
+
|
|
18
|
+
```sh
|
|
19
|
+
pnpm add @arizeai/openinference-instrumentation-openai openai
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Quick start
|
|
23
|
+
|
|
24
|
+
```ts
|
|
25
|
+
import { flush, init, observe } from "@cetra-ai/sdk";
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
<!-- example:quick-start -->
|
|
29
|
+
```ts
|
|
30
|
+
init({ apiKey: process.env.CETRA_API_KEY });
|
|
31
|
+
const searchDocs = observe(({ query }: { query: string }) => `results for ${query}`, {
|
|
32
|
+
kind: "tool",
|
|
33
|
+
name: "search_docs",
|
|
34
|
+
});
|
|
35
|
+
searchDocs({ query: "pricing" });
|
|
36
|
+
await flush();
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
`init()` is once per process: a second call logs one warning and changes
|
|
40
|
+
nothing (D-F-15). Without an API key (argument and `CETRA_API_KEY` both
|
|
41
|
+
unset) it warns once and runs disabled; `CETRA_ENABLED=false` silences the
|
|
42
|
+
warning.
|
|
43
|
+
|
|
44
|
+
## Documentation
|
|
45
|
+
|
|
46
|
+
The full guide and reference are at https://cetra.sh/docs/reference/typescript/ (configuration,
|
|
47
|
+
tracing, context, scores, datasets and experiments, OpenTelemetry, runtimes, the API).
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Anthropic helper (D-F-7): builds the OpenInference instrumentation for
|
|
3
|
+
* the Anthropic SDK and patches the module by hand, so ESM works without
|
|
4
|
+
* loader flags. Pass the result to `init({ instrument: [...] })`; the patch is
|
|
5
|
+
* active immediately, but spans reach cetra only once `init` has bound our
|
|
6
|
+
* tracer provider.
|
|
7
|
+
*
|
|
8
|
+
* `captureContent` defaults to `true` and is not re-read by `init`: pass the
|
|
9
|
+
* same value as `init`'s `captureContent` option.
|
|
10
|
+
*/
|
|
11
|
+
import { t as Instrumentation } from "./instrument-YhFFywwZ.js";
|
|
12
|
+
import Anthropic from "@anthropic-ai/sdk";
|
|
13
|
+
export declare function anthropic(module: typeof Anthropic, options?: {
|
|
14
|
+
captureContent?: boolean;
|
|
15
|
+
}): Instrumentation;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { AnthropicInstrumentation } from "@arizeai/openinference-instrumentation-anthropic";
|
|
2
|
+
// src/anthropic.ts
|
|
3
|
+
function anthropic(module, options) {
|
|
4
|
+
const capture = options?.captureContent ?? true;
|
|
5
|
+
const instrumentation = new AnthropicInstrumentation({
|
|
6
|
+
instrumentationConfig: { enabled: false },
|
|
7
|
+
traceConfig: {
|
|
8
|
+
hideInputs: !capture,
|
|
9
|
+
hideOutputs: !capture
|
|
10
|
+
}
|
|
11
|
+
});
|
|
12
|
+
instrumentation.manuallyInstrument(module);
|
|
13
|
+
return instrumentation;
|
|
14
|
+
}
|
|
15
|
+
export { anthropic };
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Browser feedback entry point (D-F-16, spec 06 § End-user feedback).
|
|
3
|
+
*
|
|
4
|
+
* The only export is `feedback()`, a single POST of spec 06's end-user
|
|
5
|
+
* feedback body. This module never grows a runtime, a queue or OTel: it
|
|
6
|
+
* imports nothing beyond `./web.js` so the subpath stays loadable in a
|
|
7
|
+
* browser bundle on its own.
|
|
8
|
+
*/
|
|
9
|
+
type FetchLike = (url: string, init: {
|
|
10
|
+
method: string;
|
|
11
|
+
headers: Record<string, string>;
|
|
12
|
+
body: Uint8Array | string;
|
|
13
|
+
signal?: unknown;
|
|
14
|
+
keepalive?: boolean;
|
|
15
|
+
}) => Promise<ResponseLike>;
|
|
16
|
+
interface ResponseLike {
|
|
17
|
+
status: number;
|
|
18
|
+
ok: boolean;
|
|
19
|
+
headers: {
|
|
20
|
+
get(name: string): string | null;
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
export interface FeedbackOptions {
|
|
24
|
+
baseUrl: string;
|
|
25
|
+
publicKey: string;
|
|
26
|
+
traceId: string;
|
|
27
|
+
spanId?: string;
|
|
28
|
+
name?: string;
|
|
29
|
+
value: number | boolean | string;
|
|
30
|
+
comment?: string;
|
|
31
|
+
/** Injection point for tests. */
|
|
32
|
+
fetch?: FetchLike;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Post one end-user feedback score to `{baseUrl}/api/public/feedback`.
|
|
36
|
+
* Resolves `{ ok: boolean }`: false for invalid input, a missing `fetch`,
|
|
37
|
+
* a rejected request or a non-2xx response. Never throws, never retries.
|
|
38
|
+
*/
|
|
39
|
+
export declare function feedback(options: FeedbackOptions): Promise<{
|
|
40
|
+
ok: boolean;
|
|
41
|
+
}>;
|
|
42
|
+
export {};
|
package/dist/browser.js
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { r as hasHttpAuthority, s as utf8Encode, t as getFetch } from "./web-C0F5sf2n.js";
|
|
2
|
+
// src/browser.ts
|
|
3
|
+
/**
|
|
4
|
+
* Browser feedback entry point (D-F-16, spec 06 § End-user feedback).
|
|
5
|
+
*
|
|
6
|
+
* The only export is `feedback()`, a single POST of spec 06's end-user
|
|
7
|
+
* feedback body. This module never grows a runtime, a queue or OTel: it
|
|
8
|
+
* imports nothing beyond `./web.js` so the subpath stays loadable in a
|
|
9
|
+
* browser bundle on its own.
|
|
10
|
+
*/
|
|
11
|
+
const PUBLIC_KEY_PREFIX = "pk-cetra-";
|
|
12
|
+
const TRACE_ID = /^[0-9a-f]{32}$/;
|
|
13
|
+
const SPAN_ID = /^[0-9a-f]{16}$/;
|
|
14
|
+
/** Spec 06: the feedback payload is at most 4 KiB. */
|
|
15
|
+
const MAX_BODY_BYTES = 4096;
|
|
16
|
+
const FEEDBACK_PATH = "/api/public/feedback";
|
|
17
|
+
/**
|
|
18
|
+
* Validate `options` and build the request; `undefined` when anything is
|
|
19
|
+
* invalid, in which case `feedback()` resolves `{ ok: false }` without a
|
|
20
|
+
* request. Never throws.
|
|
21
|
+
*/
|
|
22
|
+
function prepare(options) {
|
|
23
|
+
const { baseUrl, publicKey, traceId, value } = options;
|
|
24
|
+
if (typeof baseUrl !== "string" || !hasHttpAuthority(baseUrl) || baseUrl.includes("?") || baseUrl.includes("#")) return;
|
|
25
|
+
if (typeof publicKey !== "string" || !publicKey.startsWith(PUBLIC_KEY_PREFIX)) return;
|
|
26
|
+
if (typeof traceId !== "string" || !TRACE_ID.test(traceId)) return;
|
|
27
|
+
const spanId = options.spanId;
|
|
28
|
+
if (spanId !== void 0 && (typeof spanId !== "string" || !SPAN_ID.test(spanId))) return;
|
|
29
|
+
if (typeof value === "number" ? !Number.isFinite(value) : typeof value !== "boolean" && typeof value !== "string") return;
|
|
30
|
+
const body = {
|
|
31
|
+
trace_id: traceId,
|
|
32
|
+
...spanId !== void 0 ? { span_id: spanId } : {},
|
|
33
|
+
...options.name !== void 0 ? { name: options.name } : {},
|
|
34
|
+
value,
|
|
35
|
+
...options.comment !== void 0 ? { comment: options.comment } : {}
|
|
36
|
+
};
|
|
37
|
+
let json;
|
|
38
|
+
let bytes;
|
|
39
|
+
try {
|
|
40
|
+
json = JSON.stringify(body);
|
|
41
|
+
bytes = utf8Encode(json).length;
|
|
42
|
+
} catch {
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
if (bytes > MAX_BODY_BYTES) return;
|
|
46
|
+
return {
|
|
47
|
+
url: `${baseUrl.replace(/\/+$/, "")}${FEEDBACK_PATH}`,
|
|
48
|
+
body: json
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Post one end-user feedback score to `{baseUrl}/api/public/feedback`.
|
|
53
|
+
* Resolves `{ ok: boolean }`: false for invalid input, a missing `fetch`,
|
|
54
|
+
* a rejected request or a non-2xx response. Never throws, never retries.
|
|
55
|
+
*/
|
|
56
|
+
async function feedback(options) {
|
|
57
|
+
const request = prepare(options);
|
|
58
|
+
if (request === void 0) return { ok: false };
|
|
59
|
+
const fetch = options.fetch ?? getFetch();
|
|
60
|
+
if (fetch === void 0) return { ok: false };
|
|
61
|
+
try {
|
|
62
|
+
return { ok: (await fetch(request.url, {
|
|
63
|
+
method: "POST",
|
|
64
|
+
headers: {
|
|
65
|
+
"Content-Type": "application/json",
|
|
66
|
+
Authorization: `Bearer ${options.publicKey}`
|
|
67
|
+
},
|
|
68
|
+
body: request.body,
|
|
69
|
+
keepalive: true
|
|
70
|
+
})).ok };
|
|
71
|
+
} catch {
|
|
72
|
+
return { ok: false };
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
export { feedback };
|