@carbon-js/sdk 0.0.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.
Files changed (83) hide show
  1. package/README.md +26 -0
  2. package/dist/ai/anthropic/event-factory.d.mts +22 -0
  3. package/dist/ai/anthropic/event-factory.mjs +97 -0
  4. package/dist/ai/anthropic/fns/message-create.d.mts +15 -0
  5. package/dist/ai/anthropic/fns/message-create.mjs +248 -0
  6. package/dist/ai/anthropic/fns/message-stream.d.mts +15 -0
  7. package/dist/ai/anthropic/fns/message-stream.mjs +59 -0
  8. package/dist/ai/anthropic/types.d.mts +5 -0
  9. package/dist/ai/anthropic/types.mjs +0 -0
  10. package/dist/ai/anthropic/wrap.d.mts +13 -0
  11. package/dist/ai/anthropic/wrap.mjs +17 -0
  12. package/dist/ai/openai/event-factory.d.mts +34 -0
  13. package/dist/ai/openai/event-factory.mjs +189 -0
  14. package/dist/ai/openai/fns/chat-completions-create.d.mts +15 -0
  15. package/dist/ai/openai/fns/chat-completions-create.mjs +219 -0
  16. package/dist/ai/openai/fns/chat-completions-run-tools.d.mts +15 -0
  17. package/dist/ai/openai/fns/chat-completions-run-tools.mjs +112 -0
  18. package/dist/ai/openai/fns/chat-completions-stream.d.mts +15 -0
  19. package/dist/ai/openai/fns/chat-completions-stream.mjs +69 -0
  20. package/dist/ai/openai/fns/response-create.d.mts +15 -0
  21. package/dist/ai/openai/fns/response-create.mjs +137 -0
  22. package/dist/ai/openai/fns/response-stream.d.mts +15 -0
  23. package/dist/ai/openai/fns/response-stream.mjs +59 -0
  24. package/dist/ai/openai/types.d.mts +5 -0
  25. package/dist/ai/openai/types.mjs +0 -0
  26. package/dist/ai/openai/utils/capture-options.d.mts +6 -0
  27. package/dist/ai/openai/utils/capture-options.mjs +4 -0
  28. package/dist/ai/openai/wrap.d.mts +13 -0
  29. package/dist/ai/openai/wrap.mjs +32 -0
  30. package/dist/ai/vercel/event-factory.d.mts +52 -0
  31. package/dist/ai/vercel/event-factory.mjs +140 -0
  32. package/dist/ai/vercel/fns/tool-loop-agent.d.mts +17 -0
  33. package/dist/ai/vercel/fns/tool-loop-agent.mjs +117 -0
  34. package/dist/ai/vercel/recorder.d.mts +37 -0
  35. package/dist/ai/vercel/recorder.mjs +194 -0
  36. package/dist/ai/vercel/types.d.mts +40 -0
  37. package/dist/ai/vercel/types.mjs +1 -0
  38. package/dist/ai/vercel/utils/telemetry.d.mts +31 -0
  39. package/dist/ai/vercel/utils/telemetry.mjs +46 -0
  40. package/dist/ai/vercel/wrap.d.mts +13 -0
  41. package/dist/ai/vercel/wrap.mjs +29 -0
  42. package/dist/ai.d.mts +16 -0
  43. package/dist/ai.mjs +8 -0
  44. package/dist/core/carbon.d.mts +27 -0
  45. package/dist/core/carbon.mjs +35 -0
  46. package/dist/core/events/event-buffer.d.mts +19 -0
  47. package/dist/core/events/event-buffer.mjs +26 -0
  48. package/dist/core/runtime.d.mts +34 -0
  49. package/dist/core/runtime.mjs +119 -0
  50. package/dist/core/schema/carbon-object.d.mts +11 -0
  51. package/dist/core/schema/carbon-object.mjs +0 -0
  52. package/dist/core/tools/wrap-tool.d.mts +16 -0
  53. package/dist/core/tools/wrap-tool.mjs +120 -0
  54. package/dist/core/transport/file-transport.d.mts +16 -0
  55. package/dist/core/transport/file-transport.mjs +17 -0
  56. package/dist/core/transport/http-transport.d.mts +31 -0
  57. package/dist/core/transport/http-transport.mjs +79 -0
  58. package/dist/core/transport/memory-transport.d.mts +12 -0
  59. package/dist/core/transport/memory-transport.mjs +11 -0
  60. package/dist/core/transport/types.d.mts +13 -0
  61. package/dist/core/transport/types.mjs +0 -0
  62. package/dist/core/utils/build-events.d.mts +33 -0
  63. package/dist/core/utils/build-events.mjs +132 -0
  64. package/dist/core/utils/instrumentation.d.mts +12 -0
  65. package/dist/core/utils/instrumentation.mjs +12 -0
  66. package/dist/index.d.mts +10 -0
  67. package/dist/index.mjs +11 -0
  68. package/dist/internal/schema/events.d.mts +315 -0
  69. package/dist/internal/schema/events.mjs +111 -0
  70. package/dist/internal/schema/index.mjs +1 -0
  71. package/dist/lib/constants.d.mts +16 -0
  72. package/dist/lib/constants.mjs +16 -0
  73. package/dist/utils/ids.d.mts +3 -0
  74. package/dist/utils/ids.mjs +4 -0
  75. package/dist/utils/promise.d.mts +8 -0
  76. package/dist/utils/promise.mjs +6 -0
  77. package/dist/utils/retry.d.mts +16 -0
  78. package/dist/utils/retry.mjs +47 -0
  79. package/dist/utils/stringify.d.mts +6 -0
  80. package/dist/utils/stringify.mjs +16 -0
  81. package/dist/utils/timeout.d.mts +9 -0
  82. package/dist/utils/timeout.mjs +27 -0
  83. package/package.json +28 -0
@@ -0,0 +1,315 @@
1
+ import { z } from 'zod';
2
+
3
+ declare const Z_EventAiUsage: z.ZodObject<{
4
+ inputTokens: z.ZodNumber;
5
+ inputTokenDetails: z.ZodObject<{
6
+ uncachedTokens: z.ZodNumber;
7
+ cacheReadTokens: z.ZodNumber;
8
+ cacheWriteTokens: z.ZodNumber;
9
+ }, z.core.$strip>;
10
+ outputTokens: z.ZodNumber;
11
+ outputTokenDetails: z.ZodObject<{
12
+ reasoningTokens: z.ZodNumber;
13
+ responseTokens: z.ZodNumber;
14
+ }, z.core.$strip>;
15
+ totalTokens: z.ZodNumber;
16
+ }, z.core.$strip>;
17
+ declare const Z_EventContext: z.ZodObject<{
18
+ threadId: z.ZodOptional<z.ZodString>;
19
+ agentId: z.ZodOptional<z.ZodString>;
20
+ agentGroupId: z.ZodOptional<z.ZodString>;
21
+ userId: z.ZodOptional<z.ZodString>;
22
+ }, z.core.$strip>;
23
+ declare const Z_EventAdditionalProperties: z.ZodRecord<z.ZodString, z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>>;
24
+ declare const Z_EventBase: z.ZodObject<{
25
+ id: z.ZodUUID;
26
+ spaceId: z.ZodOptional<z.ZodString>;
27
+ type: z.ZodEnum<{
28
+ llm: "llm";
29
+ tool: "tool";
30
+ }>;
31
+ traceId: z.ZodOptional<z.ZodUUID>;
32
+ startTimeMs: z.ZodNumber;
33
+ endTimeMs: z.ZodNumber;
34
+ durationMs: z.ZodNumber;
35
+ status: z.ZodObject<{
36
+ state: z.ZodEnum<{
37
+ ok: "ok";
38
+ error: "error";
39
+ }>;
40
+ error: z.ZodOptional<z.ZodObject<{
41
+ code: z.ZodOptional<z.ZodString>;
42
+ httpStatus: z.ZodOptional<z.ZodNumber>;
43
+ message: z.ZodOptional<z.ZodString>;
44
+ }, z.core.$strip>>;
45
+ }, z.core.$strip>;
46
+ instrumentation: z.ZodObject<{
47
+ provider: z.ZodUnion<readonly [z.ZodLiteral<"carbon">]>;
48
+ providerPackage: z.ZodOptional<z.ZodUnion<readonly [z.ZodLiteral<"sdk">]>>;
49
+ sourceProvider: z.ZodUnion<readonly [z.ZodLiteral<"openai">, z.ZodLiteral<"anthropic">, z.ZodLiteral<"vercel">]>;
50
+ sourcePackage: z.ZodUnion<readonly [z.ZodLiteral<"openai">, z.ZodLiteral<"@anthropic-ai/sdk">, z.ZodLiteral<"ai">]>;
51
+ sourceFunction: z.ZodOptional<z.ZodString>;
52
+ }, z.core.$strip>;
53
+ context: z.ZodObject<{
54
+ threadId: z.ZodOptional<z.ZodString>;
55
+ agentId: z.ZodOptional<z.ZodString>;
56
+ agentGroupId: z.ZodOptional<z.ZodString>;
57
+ userId: z.ZodOptional<z.ZodString>;
58
+ }, z.core.$strip>;
59
+ additionalProperties: z.ZodRecord<z.ZodString, z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>>;
60
+ }, z.core.$strip>;
61
+ declare const Z_LLMEvent: z.ZodObject<{
62
+ id: z.ZodUUID;
63
+ spaceId: z.ZodOptional<z.ZodString>;
64
+ traceId: z.ZodOptional<z.ZodUUID>;
65
+ startTimeMs: z.ZodNumber;
66
+ endTimeMs: z.ZodNumber;
67
+ durationMs: z.ZodNumber;
68
+ status: z.ZodObject<{
69
+ state: z.ZodEnum<{
70
+ ok: "ok";
71
+ error: "error";
72
+ }>;
73
+ error: z.ZodOptional<z.ZodObject<{
74
+ code: z.ZodOptional<z.ZodString>;
75
+ httpStatus: z.ZodOptional<z.ZodNumber>;
76
+ message: z.ZodOptional<z.ZodString>;
77
+ }, z.core.$strip>>;
78
+ }, z.core.$strip>;
79
+ instrumentation: z.ZodObject<{
80
+ provider: z.ZodUnion<readonly [z.ZodLiteral<"carbon">]>;
81
+ providerPackage: z.ZodOptional<z.ZodUnion<readonly [z.ZodLiteral<"sdk">]>>;
82
+ sourceProvider: z.ZodUnion<readonly [z.ZodLiteral<"openai">, z.ZodLiteral<"anthropic">, z.ZodLiteral<"vercel">]>;
83
+ sourcePackage: z.ZodUnion<readonly [z.ZodLiteral<"openai">, z.ZodLiteral<"@anthropic-ai/sdk">, z.ZodLiteral<"ai">]>;
84
+ sourceFunction: z.ZodOptional<z.ZodString>;
85
+ }, z.core.$strip>;
86
+ context: z.ZodObject<{
87
+ threadId: z.ZodOptional<z.ZodString>;
88
+ agentId: z.ZodOptional<z.ZodString>;
89
+ agentGroupId: z.ZodOptional<z.ZodString>;
90
+ userId: z.ZodOptional<z.ZodString>;
91
+ }, z.core.$strip>;
92
+ additionalProperties: z.ZodRecord<z.ZodString, z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>>;
93
+ type: z.ZodLiteral<"llm">;
94
+ properties: z.ZodObject<{
95
+ llm: z.ZodObject<{
96
+ gateway: z.ZodOptional<z.ZodString>;
97
+ provider: z.ZodOptional<z.ZodString>;
98
+ model: z.ZodString;
99
+ input: z.ZodObject<{
100
+ system: z.ZodString;
101
+ prompt: z.ZodString;
102
+ tools: z.ZodArray<z.ZodObject<{
103
+ name: z.ZodString;
104
+ description: z.ZodOptional<z.ZodString>;
105
+ }, z.core.$strip>>;
106
+ }, z.core.$strip>;
107
+ output: z.ZodObject<{
108
+ mode: z.ZodEnum<{
109
+ generate: "generate";
110
+ stream: "stream";
111
+ }>;
112
+ reasoning: z.ZodOptional<z.ZodString>;
113
+ response: z.ZodOptional<z.ZodString>;
114
+ toolCalls: z.ZodArray<z.ZodObject<{
115
+ name: z.ZodString;
116
+ args: z.ZodOptional<z.ZodString>;
117
+ }, z.core.$strip>>;
118
+ }, z.core.$strip>;
119
+ usage: z.ZodObject<{
120
+ inputTokens: z.ZodNumber;
121
+ inputTokenDetails: z.ZodObject<{
122
+ uncachedTokens: z.ZodNumber;
123
+ cacheReadTokens: z.ZodNumber;
124
+ cacheWriteTokens: z.ZodNumber;
125
+ }, z.core.$strip>;
126
+ outputTokens: z.ZodNumber;
127
+ outputTokenDetails: z.ZodObject<{
128
+ reasoningTokens: z.ZodNumber;
129
+ responseTokens: z.ZodNumber;
130
+ }, z.core.$strip>;
131
+ totalTokens: z.ZodNumber;
132
+ }, z.core.$strip>;
133
+ cost: z.ZodOptional<z.ZodObject<{
134
+ inputUncachedUsd: z.ZodNumber;
135
+ inputCacheReadUsd: z.ZodNumber;
136
+ inputCacheWriteUsd: z.ZodNumber;
137
+ outputUsd: z.ZodNumber;
138
+ totalUsd: z.ZodNumber;
139
+ }, z.core.$strip>>;
140
+ }, z.core.$strip>;
141
+ }, z.core.$strip>;
142
+ }, z.core.$strip>;
143
+ declare const Z_ToolEvent: z.ZodObject<{
144
+ id: z.ZodUUID;
145
+ spaceId: z.ZodOptional<z.ZodString>;
146
+ traceId: z.ZodOptional<z.ZodUUID>;
147
+ startTimeMs: z.ZodNumber;
148
+ endTimeMs: z.ZodNumber;
149
+ durationMs: z.ZodNumber;
150
+ status: z.ZodObject<{
151
+ state: z.ZodEnum<{
152
+ ok: "ok";
153
+ error: "error";
154
+ }>;
155
+ error: z.ZodOptional<z.ZodObject<{
156
+ code: z.ZodOptional<z.ZodString>;
157
+ httpStatus: z.ZodOptional<z.ZodNumber>;
158
+ message: z.ZodOptional<z.ZodString>;
159
+ }, z.core.$strip>>;
160
+ }, z.core.$strip>;
161
+ instrumentation: z.ZodObject<{
162
+ provider: z.ZodUnion<readonly [z.ZodLiteral<"carbon">]>;
163
+ providerPackage: z.ZodOptional<z.ZodUnion<readonly [z.ZodLiteral<"sdk">]>>;
164
+ sourceProvider: z.ZodUnion<readonly [z.ZodLiteral<"openai">, z.ZodLiteral<"anthropic">, z.ZodLiteral<"vercel">]>;
165
+ sourcePackage: z.ZodUnion<readonly [z.ZodLiteral<"openai">, z.ZodLiteral<"@anthropic-ai/sdk">, z.ZodLiteral<"ai">]>;
166
+ sourceFunction: z.ZodOptional<z.ZodString>;
167
+ }, z.core.$strip>;
168
+ context: z.ZodObject<{
169
+ threadId: z.ZodOptional<z.ZodString>;
170
+ agentId: z.ZodOptional<z.ZodString>;
171
+ agentGroupId: z.ZodOptional<z.ZodString>;
172
+ userId: z.ZodOptional<z.ZodString>;
173
+ }, z.core.$strip>;
174
+ additionalProperties: z.ZodRecord<z.ZodString, z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>>;
175
+ type: z.ZodLiteral<"tool">;
176
+ properties: z.ZodObject<{
177
+ tool: z.ZodObject<{
178
+ name: z.ZodString;
179
+ args: z.ZodOptional<z.ZodString>;
180
+ result: z.ZodOptional<z.ZodString>;
181
+ }, z.core.$strip>;
182
+ }, z.core.$strip>;
183
+ }, z.core.$strip>;
184
+ declare const Z_Event: z.ZodDiscriminatedUnion<[z.ZodObject<{
185
+ id: z.ZodUUID;
186
+ spaceId: z.ZodOptional<z.ZodString>;
187
+ traceId: z.ZodOptional<z.ZodUUID>;
188
+ startTimeMs: z.ZodNumber;
189
+ endTimeMs: z.ZodNumber;
190
+ durationMs: z.ZodNumber;
191
+ status: z.ZodObject<{
192
+ state: z.ZodEnum<{
193
+ ok: "ok";
194
+ error: "error";
195
+ }>;
196
+ error: z.ZodOptional<z.ZodObject<{
197
+ code: z.ZodOptional<z.ZodString>;
198
+ httpStatus: z.ZodOptional<z.ZodNumber>;
199
+ message: z.ZodOptional<z.ZodString>;
200
+ }, z.core.$strip>>;
201
+ }, z.core.$strip>;
202
+ instrumentation: z.ZodObject<{
203
+ provider: z.ZodUnion<readonly [z.ZodLiteral<"carbon">]>;
204
+ providerPackage: z.ZodOptional<z.ZodUnion<readonly [z.ZodLiteral<"sdk">]>>;
205
+ sourceProvider: z.ZodUnion<readonly [z.ZodLiteral<"openai">, z.ZodLiteral<"anthropic">, z.ZodLiteral<"vercel">]>;
206
+ sourcePackage: z.ZodUnion<readonly [z.ZodLiteral<"openai">, z.ZodLiteral<"@anthropic-ai/sdk">, z.ZodLiteral<"ai">]>;
207
+ sourceFunction: z.ZodOptional<z.ZodString>;
208
+ }, z.core.$strip>;
209
+ context: z.ZodObject<{
210
+ threadId: z.ZodOptional<z.ZodString>;
211
+ agentId: z.ZodOptional<z.ZodString>;
212
+ agentGroupId: z.ZodOptional<z.ZodString>;
213
+ userId: z.ZodOptional<z.ZodString>;
214
+ }, z.core.$strip>;
215
+ additionalProperties: z.ZodRecord<z.ZodString, z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>>;
216
+ type: z.ZodLiteral<"llm">;
217
+ properties: z.ZodObject<{
218
+ llm: z.ZodObject<{
219
+ gateway: z.ZodOptional<z.ZodString>;
220
+ provider: z.ZodOptional<z.ZodString>;
221
+ model: z.ZodString;
222
+ input: z.ZodObject<{
223
+ system: z.ZodString;
224
+ prompt: z.ZodString;
225
+ tools: z.ZodArray<z.ZodObject<{
226
+ name: z.ZodString;
227
+ description: z.ZodOptional<z.ZodString>;
228
+ }, z.core.$strip>>;
229
+ }, z.core.$strip>;
230
+ output: z.ZodObject<{
231
+ mode: z.ZodEnum<{
232
+ generate: "generate";
233
+ stream: "stream";
234
+ }>;
235
+ reasoning: z.ZodOptional<z.ZodString>;
236
+ response: z.ZodOptional<z.ZodString>;
237
+ toolCalls: z.ZodArray<z.ZodObject<{
238
+ name: z.ZodString;
239
+ args: z.ZodOptional<z.ZodString>;
240
+ }, z.core.$strip>>;
241
+ }, z.core.$strip>;
242
+ usage: z.ZodObject<{
243
+ inputTokens: z.ZodNumber;
244
+ inputTokenDetails: z.ZodObject<{
245
+ uncachedTokens: z.ZodNumber;
246
+ cacheReadTokens: z.ZodNumber;
247
+ cacheWriteTokens: z.ZodNumber;
248
+ }, z.core.$strip>;
249
+ outputTokens: z.ZodNumber;
250
+ outputTokenDetails: z.ZodObject<{
251
+ reasoningTokens: z.ZodNumber;
252
+ responseTokens: z.ZodNumber;
253
+ }, z.core.$strip>;
254
+ totalTokens: z.ZodNumber;
255
+ }, z.core.$strip>;
256
+ cost: z.ZodOptional<z.ZodObject<{
257
+ inputUncachedUsd: z.ZodNumber;
258
+ inputCacheReadUsd: z.ZodNumber;
259
+ inputCacheWriteUsd: z.ZodNumber;
260
+ outputUsd: z.ZodNumber;
261
+ totalUsd: z.ZodNumber;
262
+ }, z.core.$strip>>;
263
+ }, z.core.$strip>;
264
+ }, z.core.$strip>;
265
+ }, z.core.$strip>, z.ZodObject<{
266
+ id: z.ZodUUID;
267
+ spaceId: z.ZodOptional<z.ZodString>;
268
+ traceId: z.ZodOptional<z.ZodUUID>;
269
+ startTimeMs: z.ZodNumber;
270
+ endTimeMs: z.ZodNumber;
271
+ durationMs: z.ZodNumber;
272
+ status: z.ZodObject<{
273
+ state: z.ZodEnum<{
274
+ ok: "ok";
275
+ error: "error";
276
+ }>;
277
+ error: z.ZodOptional<z.ZodObject<{
278
+ code: z.ZodOptional<z.ZodString>;
279
+ httpStatus: z.ZodOptional<z.ZodNumber>;
280
+ message: z.ZodOptional<z.ZodString>;
281
+ }, z.core.$strip>>;
282
+ }, z.core.$strip>;
283
+ instrumentation: z.ZodObject<{
284
+ provider: z.ZodUnion<readonly [z.ZodLiteral<"carbon">]>;
285
+ providerPackage: z.ZodOptional<z.ZodUnion<readonly [z.ZodLiteral<"sdk">]>>;
286
+ sourceProvider: z.ZodUnion<readonly [z.ZodLiteral<"openai">, z.ZodLiteral<"anthropic">, z.ZodLiteral<"vercel">]>;
287
+ sourcePackage: z.ZodUnion<readonly [z.ZodLiteral<"openai">, z.ZodLiteral<"@anthropic-ai/sdk">, z.ZodLiteral<"ai">]>;
288
+ sourceFunction: z.ZodOptional<z.ZodString>;
289
+ }, z.core.$strip>;
290
+ context: z.ZodObject<{
291
+ threadId: z.ZodOptional<z.ZodString>;
292
+ agentId: z.ZodOptional<z.ZodString>;
293
+ agentGroupId: z.ZodOptional<z.ZodString>;
294
+ userId: z.ZodOptional<z.ZodString>;
295
+ }, z.core.$strip>;
296
+ additionalProperties: z.ZodRecord<z.ZodString, z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>>;
297
+ type: z.ZodLiteral<"tool">;
298
+ properties: z.ZodObject<{
299
+ tool: z.ZodObject<{
300
+ name: z.ZodString;
301
+ args: z.ZodOptional<z.ZodString>;
302
+ result: z.ZodOptional<z.ZodString>;
303
+ }, z.core.$strip>;
304
+ }, z.core.$strip>;
305
+ }, z.core.$strip>], "type">;
306
+ type T_EventAiUsage = z.infer<typeof Z_EventAiUsage>;
307
+ type T_EventContext = z.infer<typeof Z_EventContext>;
308
+ type T_EventAdditionalProperties = z.infer<typeof Z_EventAdditionalProperties>;
309
+ type T_EventBase = z.infer<typeof Z_EventBase>;
310
+ type T_EventStatus = T_EventBase["status"];
311
+ type T_LLMEvent = z.infer<typeof Z_LLMEvent>;
312
+ type T_ToolEvent = z.infer<typeof Z_ToolEvent>;
313
+ type T_Event = z.infer<typeof Z_Event>;
314
+
315
+ export type { T_LLMEvent as T, T_Event as a, T_ToolEvent as b, T_EventAiUsage as c, T_EventAdditionalProperties as d, T_EventContext as e, T_EventBase as f, T_EventStatus as g };
@@ -0,0 +1,111 @@
1
+ import { z } from "zod";
2
+ const Z_EventAiUsage = z.object({
3
+ inputTokens: z.number(),
4
+ inputTokenDetails: z.object({
5
+ uncachedTokens: z.number(),
6
+ cacheReadTokens: z.number(),
7
+ cacheWriteTokens: z.number()
8
+ }),
9
+ outputTokens: z.number(),
10
+ outputTokenDetails: z.object({
11
+ reasoningTokens: z.number(),
12
+ responseTokens: z.number()
13
+ }),
14
+ totalTokens: z.number()
15
+ });
16
+ const Z_EventLlmCost = z.object({
17
+ inputUncachedUsd: z.number(),
18
+ inputCacheReadUsd: z.number(),
19
+ inputCacheWriteUsd: z.number(),
20
+ outputUsd: z.number(),
21
+ totalUsd: z.number()
22
+ });
23
+ const Z_EventContext = z.object({
24
+ threadId: z.string().optional(),
25
+ agentId: z.string().optional(),
26
+ agentGroupId: z.string().optional(),
27
+ userId: z.string().optional()
28
+ });
29
+ const Z_EventInstrumentation = z.object({
30
+ provider: z.union([z.literal("carbon")]),
31
+ providerPackage: z.union([z.literal("sdk")]).optional(),
32
+ sourceProvider: z.union([z.literal("openai"), z.literal("anthropic"), z.literal("vercel")]),
33
+ sourcePackage: z.union([z.literal("openai"), z.literal("@anthropic-ai/sdk"), z.literal("ai")]),
34
+ sourceFunction: z.string().optional()
35
+ });
36
+ const Z_EventAdditionalProperties = z.record(z.string(), z.union([z.string(), z.number()]));
37
+ const Z_EventBase = z.object({
38
+ id: z.uuidv4(),
39
+ spaceId: z.string().optional(),
40
+ type: z.enum(["llm", "tool"]),
41
+ traceId: z.uuidv4().optional(),
42
+ startTimeMs: z.number(),
43
+ endTimeMs: z.number(),
44
+ durationMs: z.number(),
45
+ status: z.object({
46
+ state: z.enum(["ok", "error"]),
47
+ error: z.object({
48
+ code: z.string().optional(),
49
+ httpStatus: z.number().optional(),
50
+ message: z.string().optional()
51
+ }).optional()
52
+ }),
53
+ instrumentation: Z_EventInstrumentation,
54
+ context: Z_EventContext,
55
+ additionalProperties: Z_EventAdditionalProperties
56
+ });
57
+ const Z_LLMEvent = Z_EventBase.extend({
58
+ type: z.literal("llm"),
59
+ properties: z.object({
60
+ llm: z.object({
61
+ gateway: z.string().optional(),
62
+ provider: z.string().optional(),
63
+ model: z.string(),
64
+ input: z.object({
65
+ system: z.string(),
66
+ prompt: z.string(),
67
+ tools: z.array(
68
+ z.object({
69
+ name: z.string(),
70
+ description: z.string().optional()
71
+ })
72
+ )
73
+ }),
74
+ output: z.object({
75
+ mode: z.enum(["generate", "stream"]),
76
+ reasoning: z.string().optional(),
77
+ response: z.string().optional(),
78
+ toolCalls: z.array(
79
+ z.object({
80
+ name: z.string(),
81
+ args: z.string().optional()
82
+ })
83
+ )
84
+ }),
85
+ usage: Z_EventAiUsage,
86
+ cost: Z_EventLlmCost.optional()
87
+ })
88
+ })
89
+ });
90
+ const Z_ToolEvent = Z_EventBase.extend({
91
+ type: z.literal("tool"),
92
+ properties: z.object({
93
+ tool: z.object({
94
+ name: z.string(),
95
+ args: z.string().optional(),
96
+ result: z.string().optional()
97
+ })
98
+ })
99
+ });
100
+ const Z_Event = z.discriminatedUnion("type", [Z_LLMEvent, Z_ToolEvent]);
101
+ export {
102
+ Z_Event,
103
+ Z_EventAdditionalProperties,
104
+ Z_EventAiUsage,
105
+ Z_EventBase,
106
+ Z_EventContext,
107
+ Z_EventInstrumentation,
108
+ Z_EventLlmCost,
109
+ Z_LLMEvent,
110
+ Z_ToolEvent
111
+ };
@@ -0,0 +1 @@
1
+ export * from "./events.mjs";
@@ -0,0 +1,16 @@
1
+ type T_Constants = {
2
+ api: {
3
+ baseUrl: string;
4
+ ingestPath: string;
5
+ retryMaxAttempts: number;
6
+ retryMaxElapsedMs: number;
7
+ timeoutMs: number;
8
+ };
9
+ buffer: {
10
+ batchSize: number;
11
+ maxTimeMs: number;
12
+ };
13
+ };
14
+ declare const CONSTANTS: T_Constants;
15
+
16
+ export { CONSTANTS };
@@ -0,0 +1,16 @@
1
+ const CONSTANTS = {
2
+ api: {
3
+ baseUrl: "https://api.carbonrow.com",
4
+ ingestPath: "/v1/ingest-events",
5
+ retryMaxAttempts: 2,
6
+ retryMaxElapsedMs: 3e4,
7
+ timeoutMs: 15e3
8
+ },
9
+ buffer: {
10
+ batchSize: 50,
11
+ maxTimeMs: 5e3
12
+ }
13
+ };
14
+ export {
15
+ CONSTANTS
16
+ };
@@ -0,0 +1,3 @@
1
+ declare const generateId: () => `${string}-${string}-${string}-${string}-${string}`;
2
+
3
+ export { generateId };
@@ -0,0 +1,4 @@
1
+ const generateId = () => crypto.randomUUID();
2
+ export {
3
+ generateId
4
+ };
@@ -0,0 +1,8 @@
1
+ type T_IsPromiseLike = {
2
+ value: unknown;
3
+ };
4
+ declare const isPromiseLike: (args: T_IsPromiseLike) => args is T_IsPromiseLike & {
5
+ value: PromiseLike<unknown>;
6
+ };
7
+
8
+ export { isPromiseLike };
@@ -0,0 +1,6 @@
1
+ const isPromiseLike = (args) => {
2
+ return typeof args.value === "object" && args.value !== null && "then" in args.value && typeof args.value.then === "function";
3
+ };
4
+ export {
5
+ isPromiseLike
6
+ };
@@ -0,0 +1,16 @@
1
+ type T_Retry<T_Result> = {
2
+ initialDelayMs?: number;
3
+ jitterRatio?: number;
4
+ maxAttempts?: number;
5
+ maxDelayMs?: number;
6
+ maxElapsedMs?: number;
7
+ run: () => Promise<T_Result>;
8
+ shouldRetry?: (args: {
9
+ attempt: number;
10
+ error: unknown;
11
+ }) => boolean;
12
+ unrefDelay?: boolean;
13
+ };
14
+ declare const retry: <T_Result>(args: T_Retry<T_Result>) => Promise<T_Result>;
15
+
16
+ export { retry };
@@ -0,0 +1,47 @@
1
+ const RETRY_DEFAULTS = {
2
+ initialDelayMs: 500,
3
+ jitterRatio: 0.2,
4
+ maxAttempts: 3,
5
+ maxDelayMs: 5e3,
6
+ maxElapsedMs: 15e3
7
+ };
8
+ const retry = async (args) => {
9
+ let attempt = 1;
10
+ const startedAtMs = Date.now();
11
+ while (true) {
12
+ try {
13
+ return await args.run();
14
+ } catch (error) {
15
+ const canRetry = args.shouldRetry?.({ attempt, error }) ?? true;
16
+ if (attempt >= (args.maxAttempts ?? RETRY_DEFAULTS.maxAttempts) || !canRetry) {
17
+ throw error;
18
+ }
19
+ const remainingMs = getRemainingElapsedBudgetMs({
20
+ maxElapsedMs: args.maxElapsedMs ?? RETRY_DEFAULTS.maxElapsedMs,
21
+ startedAtMs
22
+ });
23
+ if (remainingMs <= 0) {
24
+ throw error;
25
+ }
26
+ const initialDelayMs = args.initialDelayMs ?? RETRY_DEFAULTS.initialDelayMs;
27
+ const jitterRatio = args.jitterRatio ?? RETRY_DEFAULTS.jitterRatio;
28
+ const maxDelayMs = args.maxDelayMs ?? RETRY_DEFAULTS.maxDelayMs;
29
+ const baseDelayMs = Math.min(initialDelayMs * 2 ** (attempt - 1), maxDelayMs);
30
+ const jitterMs = baseDelayMs * jitterRatio;
31
+ const delayMs = jitterRatio <= 0 ? baseDelayMs : Math.round(baseDelayMs - jitterMs + Math.random() * jitterMs * 2);
32
+ await new Promise((resolve) => {
33
+ const timeout = setTimeout(resolve, Math.min(delayMs, remainingMs));
34
+ if (args.unrefDelay) {
35
+ timeout.unref?.();
36
+ }
37
+ });
38
+ attempt += 1;
39
+ }
40
+ }
41
+ };
42
+ function getRemainingElapsedBudgetMs(args) {
43
+ return args.maxElapsedMs - (Date.now() - args.startedAtMs);
44
+ }
45
+ export {
46
+ retry
47
+ };
@@ -0,0 +1,6 @@
1
+ type T_Stringify = {
2
+ value: unknown;
3
+ };
4
+ declare const stringify: (args: T_Stringify) => string;
5
+
6
+ export { stringify };
@@ -0,0 +1,16 @@
1
+ const stringify = (args) => {
2
+ if (args.value === null || args.value === void 0) {
3
+ return "";
4
+ }
5
+ if (typeof args.value === "string") {
6
+ return args.value;
7
+ }
8
+ try {
9
+ return JSON.stringify(args.value);
10
+ } catch {
11
+ return String(args.value);
12
+ }
13
+ };
14
+ export {
15
+ stringify
16
+ };
@@ -0,0 +1,9 @@
1
+ type T_RunWithTimeout<T_Result> = {
2
+ run: (args: {
3
+ signal: AbortSignal;
4
+ }) => Promise<T_Result>;
5
+ timeoutMs: number;
6
+ };
7
+ declare const runWithTimeout: <T_Result>(args: T_RunWithTimeout<T_Result>) => Promise<T_Result>;
8
+
9
+ export { runWithTimeout };
@@ -0,0 +1,27 @@
1
+ const runWithTimeout = async (args) => {
2
+ if (!Number.isInteger(args.timeoutMs) || args.timeoutMs <= 0) {
3
+ throw new Error("timeoutMs must be a positive integer.");
4
+ }
5
+ const controller = new AbortController();
6
+ let timeout;
7
+ const timeoutPromise = new Promise((_, reject) => {
8
+ timeout = setTimeout(() => {
9
+ controller.abort();
10
+ reject(new Error(`Operation timed out after ${args.timeoutMs}ms.`));
11
+ }, args.timeoutMs);
12
+ timeout.unref?.();
13
+ });
14
+ try {
15
+ return await Promise.race([
16
+ args.run({
17
+ signal: controller.signal
18
+ }),
19
+ timeoutPromise
20
+ ]);
21
+ } finally {
22
+ clearTimeout(timeout);
23
+ }
24
+ };
25
+ export {
26
+ runWithTimeout
27
+ };
package/package.json ADDED
@@ -0,0 +1,28 @@
1
+ {
2
+ "name": "@carbon-js/sdk",
3
+ "version": "0.0.1",
4
+ "type": "module",
5
+ "exports": {
6
+ ".": {
7
+ "types": "./dist/index.d.mts",
8
+ "default": "./dist/index.mjs"
9
+ },
10
+ "./ai": {
11
+ "types": "./dist/ai.d.mts",
12
+ "default": "./dist/ai.mjs"
13
+ }
14
+ },
15
+ "types": "./dist/index.d.mts",
16
+ "files": [
17
+ "README.md",
18
+ "dist"
19
+ ],
20
+ "dependencies": {
21
+ "@ai-sdk/anthropic": "^3.0.79",
22
+ "@ai-sdk/openai": "^3.0.65",
23
+ "@anthropic-ai/sdk": "^0.96.0",
24
+ "ai": "^6.0.177",
25
+ "openai": "^6.37.0",
26
+ "zod": "^4.4.3"
27
+ }
28
+ }