@volter/twin-xai 0.1.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.
Files changed (56) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +246 -0
  3. package/client/xai-device-auth.css +246 -0
  4. package/client/xai-device-auth.tsx +138 -0
  5. package/dist/client/xai-device-auth.bundle.js +18 -0
  6. package/dist/client/xai-device-auth.css +246 -0
  7. package/dist/client/xai-device-auth.d.ts +19 -0
  8. package/dist/client/xai-device-auth.js +50 -0
  9. package/dist/client/xai-device-auth.tsx +138 -0
  10. package/dist/src/cli.d.ts +2 -0
  11. package/dist/src/cli.js +28 -0
  12. package/dist/src/index.d.ts +17 -0
  13. package/dist/src/index.js +70 -0
  14. package/dist/src/xai-budget.d.ts +60 -0
  15. package/dist/src/xai-budget.js +139 -0
  16. package/dist/src/xai-capabilities.d.ts +4 -0
  17. package/dist/src/xai-capabilities.js +1072 -0
  18. package/dist/src/xai-conformance.d.ts +13 -0
  19. package/dist/src/xai-conformance.js +148 -0
  20. package/dist/src/xai-connector.d.ts +82 -0
  21. package/dist/src/xai-connector.js +174 -0
  22. package/dist/src/xai-device-auth-css.gen.d.ts +1 -0
  23. package/dist/src/xai-device-auth-css.gen.js +6 -0
  24. package/dist/src/xai-device-auth-ui.d.ts +13 -0
  25. package/dist/src/xai-device-auth-ui.js +72 -0
  26. package/dist/src/xai-models.d.ts +57 -0
  27. package/dist/src/xai-models.js +102 -0
  28. package/dist/src/xai-oauth.d.ts +30 -0
  29. package/dist/src/xai-oauth.js +279 -0
  30. package/dist/src/xai-scenario.d.ts +33 -0
  31. package/dist/src/xai-scenario.js +139 -0
  32. package/dist/src/xai-server.d.ts +36 -0
  33. package/dist/src/xai-server.js +232 -0
  34. package/dist/src/xai-stub.d.ts +69 -0
  35. package/dist/src/xai-stub.js +210 -0
  36. package/dist/src/xai-twin.d.ts +89 -0
  37. package/dist/src/xai-twin.js +883 -0
  38. package/dist/src/xai-types.d.ts +118 -0
  39. package/dist/src/xai-types.js +6 -0
  40. package/package.json +76 -0
  41. package/src/cli.ts +27 -0
  42. package/src/index.ts +120 -0
  43. package/src/xai-budget.ts +165 -0
  44. package/src/xai-capabilities.ts +1046 -0
  45. package/src/xai-conformance.ts +136 -0
  46. package/src/xai-connector.ts +212 -0
  47. package/src/xai-device-auth-css.gen.ts +6 -0
  48. package/src/xai-device-auth-ui.ts +90 -0
  49. package/src/xai-journey.uitest.ts +155 -0
  50. package/src/xai-models.ts +154 -0
  51. package/src/xai-oauth.ts +301 -0
  52. package/src/xai-scenario.ts +148 -0
  53. package/src/xai-server.ts +258 -0
  54. package/src/xai-stub.ts +213 -0
  55. package/src/xai-twin.ts +960 -0
  56. package/src/xai-types.ts +111 -0
@@ -0,0 +1,111 @@
1
+ // Shared wire-shape types for the xAI (Grok) API surface. These mirror the real vendor JSON
2
+ // shapes (not any SDK's internal types — the twin never imports an SDK at runtime; the real
3
+ // `@ai-sdk/xai` provider is exercised only in *.test.ts). xAI's chat surface is OpenAI-chat-
4
+ // compatible with xAI-specific deltas (Live Search citations, reasoning_content, deferred
5
+ // completions, the {code, error} error envelope), and those deltas are modeled here explicitly.
6
+
7
+ // ── Chat Completions (OpenAI-compatible + xAI deltas) ───────────────────────────────────
8
+ /** A chat message param as the caller sends it (content is a string OR a content-part array). */
9
+ export type ChatMessageParam = {
10
+ role: 'system' | 'user' | 'assistant' | 'tool';
11
+ content?: string | Array<Record<string, unknown>> | null;
12
+ name?: string;
13
+ tool_calls?: ChatToolCall[];
14
+ tool_call_id?: string;
15
+ };
16
+
17
+ /** A function tool_call inside an assistant message (faithful shape). */
18
+ export type ChatToolCall = {
19
+ id: string;
20
+ type: 'function';
21
+ function: { name: string; arguments: string };
22
+ };
23
+
24
+ /** xAI reports token detail objects inside `usage` (prompt_tokens_details with text/audio/
25
+ * image/cached splits; completion_tokens_details with reasoning tokens), plus the Live Search
26
+ * `num_sources_used` counter. Deterministic in the twin. */
27
+ export type ChatUsage = {
28
+ prompt_tokens: number;
29
+ completion_tokens: number;
30
+ total_tokens: number;
31
+ prompt_tokens_details: { text_tokens: number; audio_tokens: number; image_tokens: number; cached_tokens: number };
32
+ completion_tokens_details: {
33
+ reasoning_tokens: number;
34
+ audio_tokens: number;
35
+ accepted_prediction_tokens: number;
36
+ rejected_prediction_tokens: number;
37
+ };
38
+ num_sources_used: number;
39
+ };
40
+
41
+ export type ChatChoice = {
42
+ index: number;
43
+ message: {
44
+ role: 'assistant';
45
+ content: string | null;
46
+ /** xAI delta: grok-3-mini exposes its chain-of-thought here (the twin's is a labeled stub);
47
+ * grok-4-family reasoning models spend reasoning tokens but never expose the content. */
48
+ reasoning_content?: string;
49
+ tool_calls?: ChatToolCall[];
50
+ refusal?: null;
51
+ };
52
+ logprobs: null;
53
+ finish_reason: 'stop' | 'length' | 'tool_calls' | 'content_filter';
54
+ };
55
+
56
+ /** The unary chat.completion response envelope (faithful shape; xAI ids are UUID-formatted). */
57
+ export type ChatCompletion = {
58
+ id: string;
59
+ object: 'chat.completion';
60
+ created: number;
61
+ model: string;
62
+ choices: ChatChoice[];
63
+ usage: ChatUsage;
64
+ system_fingerprint: string;
65
+ /** Live Search: the source URLs consulted (deterministic labeled stubs in the twin). Present
66
+ * only when search ran and return_citations was not disabled. */
67
+ citations?: string[];
68
+ };
69
+
70
+ // ── Legacy completions (POST /v1/completions) ───────────────────────────────────────────
71
+ export type TextCompletion = {
72
+ id: string;
73
+ object: 'text_completion';
74
+ created: number;
75
+ model: string;
76
+ choices: Array<{ text: string; index: number; logprobs: null; finish_reason: 'stop' | 'length' }>;
77
+ usage: ChatUsage;
78
+ system_fingerprint: string;
79
+ };
80
+
81
+ // ── Anthropic-compatible Messages (POST /v1/messages) ───────────────────────────────────
82
+ export type MessagesContentBlock =
83
+ | { type: 'text'; text: string }
84
+ | { type: 'tool_use'; id: string; name: string; input: Record<string, unknown> };
85
+
86
+ export type MessagesResponse = {
87
+ id: string;
88
+ type: 'message';
89
+ role: 'assistant';
90
+ model: string;
91
+ content: MessagesContentBlock[];
92
+ stop_reason: 'end_turn' | 'max_tokens' | 'stop_sequence' | 'tool_use';
93
+ stop_sequence: string | null;
94
+ usage: { input_tokens: number; output_tokens: number };
95
+ };
96
+
97
+ // ── Errors ──────────────────────────────────────────────────────────────────────────────
98
+ /** The xAI error envelope. Unlike OpenAI's nested `{ error: {...} }`, xAI's REST layer is a
99
+ * gRPC transcoding and returns TWO FLAT STRINGS: `code` (a gRPC status description, e.g.
100
+ * "Client specified an invalid argument") and `error` (the human message). The real
101
+ * `@ai-sdk/xai` provider parses exactly this shape. */
102
+ export type XaiError = { code: string; error: string };
103
+
104
+ // ── Streaming ───────────────────────────────────────────────────────────────────────────
105
+ /** A single Server-Sent Event the streaming path emits (collected, never socketed in tests).
106
+ * `data` is the JSON payload; `[DONE]` is signalled with `done: true` (no data object). */
107
+ export type SseEvent = { data?: Record<string, unknown>; done?: boolean };
108
+
109
+ /** A sink the streaming path writes events into (an injected collector in tests / a real
110
+ * HTTP SSE writer in the server). NO real sockets or setTimeout in the handler. */
111
+ export type SseSink = (event: SseEvent) => void;