@countly/ai-sdk-core 0.0.1 → 0.0.2
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 +85 -0
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
# @countly/ai-sdk-core
|
|
2
|
+
|
|
3
|
+
> Shared transport, data model, cost calculation, and event builders for the [Countly AI SDK](https://github.com/Countly/countly-ai-sdk).
|
|
4
|
+
|
|
5
|
+
This is the foundation package. You usually don't install it directly — every adapter pulls it in automatically:
|
|
6
|
+
|
|
7
|
+
- [`@countly/ai-sdk-openai`](https://www.npmjs.com/package/@countly/ai-sdk-openai)
|
|
8
|
+
- [`@countly/ai-sdk-anthropic`](https://www.npmjs.com/package/@countly/ai-sdk-anthropic)
|
|
9
|
+
- [`@countly/ai-sdk-mastra`](https://www.npmjs.com/package/@countly/ai-sdk-mastra)
|
|
10
|
+
- [`@countly/ai-sdk-vercel`](https://www.npmjs.com/package/@countly/ai-sdk-vercel)
|
|
11
|
+
- [`@countly/ai-sdk-google-genai`](https://www.npmjs.com/package/@countly/ai-sdk-google-genai)
|
|
12
|
+
- [`@countly/ai-sdk-langchain`](https://www.npmjs.com/package/@countly/ai-sdk-langchain)
|
|
13
|
+
- [`@countly/ai-sdk-cohere`](https://www.npmjs.com/package/@countly/ai-sdk-cohere)
|
|
14
|
+
- [`@countly/ai-sdk-llamaindex`](https://www.npmjs.com/package/@countly/ai-sdk-llamaindex)
|
|
15
|
+
|
|
16
|
+
## What it provides
|
|
17
|
+
|
|
18
|
+
- **Unified data model** (`RawExtractionResult`) — OpenTelemetry GenAI-aligned schema produced by every adapter
|
|
19
|
+
- **HTTP transport** — buffered `POST /i` with per-event `device_id`, retry with exponential backoff, dead-letter buffer (max 1000), `429 Retry-After` support
|
|
20
|
+
- **Cost calculation** — built-in pricing for 30+ models (OpenAI, Anthropic, Google, Cohere) with per-model override
|
|
21
|
+
- **Event builders** — `[CLY]_llm_interaction`, `[CLY]_llm_tool_used`, `[CLY]_llm_tool_usage_parameter`, `[CLY]_llm_interaction_feedback`
|
|
22
|
+
- **Finish reason normalization** — maps provider-specific values to canonical `stop | length | tool_calls | content_filter | error | other`
|
|
23
|
+
- **Error categorization** — `rate_limit | context_length | content_filter | timeout | auth_error | api_error`
|
|
24
|
+
|
|
25
|
+
## Direct usage
|
|
26
|
+
|
|
27
|
+
If you're building a custom integration not covered by an existing adapter:
|
|
28
|
+
|
|
29
|
+
```typescript
|
|
30
|
+
import {
|
|
31
|
+
resolveConfig,
|
|
32
|
+
createTransport,
|
|
33
|
+
buildAllEvents,
|
|
34
|
+
generatePromptId,
|
|
35
|
+
} from "@countly/ai-sdk-core";
|
|
36
|
+
|
|
37
|
+
const config = resolveConfig({
|
|
38
|
+
appKey: "YOUR_APP_KEY",
|
|
39
|
+
url: "https://your-countly-server.com",
|
|
40
|
+
});
|
|
41
|
+
const transport = createTransport(config);
|
|
42
|
+
|
|
43
|
+
const raw = {
|
|
44
|
+
provider: "my-llm",
|
|
45
|
+
model: "my-model",
|
|
46
|
+
usage_input: 100,
|
|
47
|
+
usage_output: 50,
|
|
48
|
+
usage_total: 150,
|
|
49
|
+
latency_total: 1200,
|
|
50
|
+
status: "success" as const,
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
const events = buildAllEvents(raw, config, {
|
|
54
|
+
prompt_id: generatePromptId(),
|
|
55
|
+
sdk_adapter: "custom",
|
|
56
|
+
});
|
|
57
|
+
transport.enqueue(events);
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## Feedback events
|
|
61
|
+
|
|
62
|
+
Feedback is not auto-collected — wire it from your UI's thumbs-up/down handler:
|
|
63
|
+
|
|
64
|
+
```typescript
|
|
65
|
+
import { buildFeedbackEvent, createTransport, resolveConfig } from "@countly/ai-sdk-core";
|
|
66
|
+
|
|
67
|
+
const config = resolveConfig({ appKey: "...", url: "..." });
|
|
68
|
+
const transport = createTransport(config);
|
|
69
|
+
|
|
70
|
+
function onFeedback(promptId: string, rating: string, userId: string) {
|
|
71
|
+
const event = buildFeedbackEvent({ prompt_id: promptId, rating });
|
|
72
|
+
event.deviceId = userId;
|
|
73
|
+
transport.enqueue([event]);
|
|
74
|
+
}
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
The `prompt_id` links back to the `[CLY]_llm_interaction` event. Store it in your chat UI and read it back when the user clicks feedback.
|
|
78
|
+
|
|
79
|
+
## Full documentation
|
|
80
|
+
|
|
81
|
+
See the [Countly AI SDK repository](https://github.com/Countly/countly-ai-sdk) for the unified data model, observability levels (0/1/2), per-user attribution via AsyncLocalStorage, cost calculation, privacy controls, and Countly plugin integration (Drill, Funnels, Cohorts, APM, Crash Analytics).
|
|
82
|
+
|
|
83
|
+
## License
|
|
84
|
+
|
|
85
|
+
MIT
|