@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.
- package/README.md +26 -0
- package/dist/ai/anthropic/event-factory.d.mts +22 -0
- package/dist/ai/anthropic/event-factory.mjs +97 -0
- package/dist/ai/anthropic/fns/message-create.d.mts +15 -0
- package/dist/ai/anthropic/fns/message-create.mjs +248 -0
- package/dist/ai/anthropic/fns/message-stream.d.mts +15 -0
- package/dist/ai/anthropic/fns/message-stream.mjs +59 -0
- package/dist/ai/anthropic/types.d.mts +5 -0
- package/dist/ai/anthropic/types.mjs +0 -0
- package/dist/ai/anthropic/wrap.d.mts +13 -0
- package/dist/ai/anthropic/wrap.mjs +17 -0
- package/dist/ai/openai/event-factory.d.mts +34 -0
- package/dist/ai/openai/event-factory.mjs +189 -0
- package/dist/ai/openai/fns/chat-completions-create.d.mts +15 -0
- package/dist/ai/openai/fns/chat-completions-create.mjs +219 -0
- package/dist/ai/openai/fns/chat-completions-run-tools.d.mts +15 -0
- package/dist/ai/openai/fns/chat-completions-run-tools.mjs +112 -0
- package/dist/ai/openai/fns/chat-completions-stream.d.mts +15 -0
- package/dist/ai/openai/fns/chat-completions-stream.mjs +69 -0
- package/dist/ai/openai/fns/response-create.d.mts +15 -0
- package/dist/ai/openai/fns/response-create.mjs +137 -0
- package/dist/ai/openai/fns/response-stream.d.mts +15 -0
- package/dist/ai/openai/fns/response-stream.mjs +59 -0
- package/dist/ai/openai/types.d.mts +5 -0
- package/dist/ai/openai/types.mjs +0 -0
- package/dist/ai/openai/utils/capture-options.d.mts +6 -0
- package/dist/ai/openai/utils/capture-options.mjs +4 -0
- package/dist/ai/openai/wrap.d.mts +13 -0
- package/dist/ai/openai/wrap.mjs +32 -0
- package/dist/ai/vercel/event-factory.d.mts +52 -0
- package/dist/ai/vercel/event-factory.mjs +140 -0
- package/dist/ai/vercel/fns/tool-loop-agent.d.mts +17 -0
- package/dist/ai/vercel/fns/tool-loop-agent.mjs +117 -0
- package/dist/ai/vercel/recorder.d.mts +37 -0
- package/dist/ai/vercel/recorder.mjs +194 -0
- package/dist/ai/vercel/types.d.mts +40 -0
- package/dist/ai/vercel/types.mjs +1 -0
- package/dist/ai/vercel/utils/telemetry.d.mts +31 -0
- package/dist/ai/vercel/utils/telemetry.mjs +46 -0
- package/dist/ai/vercel/wrap.d.mts +13 -0
- package/dist/ai/vercel/wrap.mjs +29 -0
- package/dist/ai.d.mts +16 -0
- package/dist/ai.mjs +8 -0
- package/dist/core/carbon.d.mts +27 -0
- package/dist/core/carbon.mjs +35 -0
- package/dist/core/events/event-buffer.d.mts +19 -0
- package/dist/core/events/event-buffer.mjs +26 -0
- package/dist/core/runtime.d.mts +34 -0
- package/dist/core/runtime.mjs +119 -0
- package/dist/core/schema/carbon-object.d.mts +11 -0
- package/dist/core/schema/carbon-object.mjs +0 -0
- package/dist/core/tools/wrap-tool.d.mts +16 -0
- package/dist/core/tools/wrap-tool.mjs +120 -0
- package/dist/core/transport/file-transport.d.mts +16 -0
- package/dist/core/transport/file-transport.mjs +17 -0
- package/dist/core/transport/http-transport.d.mts +31 -0
- package/dist/core/transport/http-transport.mjs +79 -0
- package/dist/core/transport/memory-transport.d.mts +12 -0
- package/dist/core/transport/memory-transport.mjs +11 -0
- package/dist/core/transport/types.d.mts +13 -0
- package/dist/core/transport/types.mjs +0 -0
- package/dist/core/utils/build-events.d.mts +33 -0
- package/dist/core/utils/build-events.mjs +132 -0
- package/dist/core/utils/instrumentation.d.mts +12 -0
- package/dist/core/utils/instrumentation.mjs +12 -0
- package/dist/index.d.mts +10 -0
- package/dist/index.mjs +11 -0
- package/dist/internal/schema/events.d.mts +315 -0
- package/dist/internal/schema/events.mjs +111 -0
- package/dist/internal/schema/index.mjs +1 -0
- package/dist/lib/constants.d.mts +16 -0
- package/dist/lib/constants.mjs +16 -0
- package/dist/utils/ids.d.mts +3 -0
- package/dist/utils/ids.mjs +4 -0
- package/dist/utils/promise.d.mts +8 -0
- package/dist/utils/promise.mjs +6 -0
- package/dist/utils/retry.d.mts +16 -0
- package/dist/utils/retry.mjs +47 -0
- package/dist/utils/stringify.d.mts +6 -0
- package/dist/utils/stringify.mjs +16 -0
- package/dist/utils/timeout.d.mts +9 -0
- package/dist/utils/timeout.mjs +27 -0
- package/package.json +28 -0
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { buildToolEvent, createErrorStatus, createOkStatus } from "../utils/build-events.mjs";
|
|
2
|
+
import { createSdkInstrumentation } from "../utils/instrumentation.mjs";
|
|
3
|
+
import { isPromiseLike } from "../../utils/promise.mjs";
|
|
4
|
+
import { stringify } from "../../utils/stringify.mjs";
|
|
5
|
+
function createWrappedTool(args) {
|
|
6
|
+
return (...callArgs) => {
|
|
7
|
+
const parsedArgs = parseToolCallArgs({ args: callArgs });
|
|
8
|
+
const startTimeMs = Date.now();
|
|
9
|
+
try {
|
|
10
|
+
const result = args.run(...parsedArgs.toolArgs);
|
|
11
|
+
if (isPromiseLike({ value: result })) {
|
|
12
|
+
return Promise.resolve(result).then(
|
|
13
|
+
(resolvedResult) => {
|
|
14
|
+
captureToolResult({
|
|
15
|
+
args: parsedArgs.toolArgs,
|
|
16
|
+
carbon: parsedArgs.carbon,
|
|
17
|
+
name: args.name,
|
|
18
|
+
result: resolvedResult,
|
|
19
|
+
startTimeMs,
|
|
20
|
+
captureToolCall: args.captureToolCall
|
|
21
|
+
});
|
|
22
|
+
return resolvedResult;
|
|
23
|
+
},
|
|
24
|
+
(error) => {
|
|
25
|
+
captureToolResult({
|
|
26
|
+
args: parsedArgs.toolArgs,
|
|
27
|
+
carbon: parsedArgs.carbon,
|
|
28
|
+
name: args.name,
|
|
29
|
+
result: getToolErrorResult({ error }),
|
|
30
|
+
startTimeMs,
|
|
31
|
+
status: createErrorStatus({ error }),
|
|
32
|
+
captureToolCall: args.captureToolCall
|
|
33
|
+
});
|
|
34
|
+
throw error;
|
|
35
|
+
}
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
captureToolResult({
|
|
39
|
+
args: parsedArgs.toolArgs,
|
|
40
|
+
carbon: parsedArgs.carbon,
|
|
41
|
+
name: args.name,
|
|
42
|
+
result,
|
|
43
|
+
startTimeMs,
|
|
44
|
+
captureToolCall: args.captureToolCall
|
|
45
|
+
});
|
|
46
|
+
return result;
|
|
47
|
+
} catch (error) {
|
|
48
|
+
captureToolResult({
|
|
49
|
+
args: parsedArgs.toolArgs,
|
|
50
|
+
carbon: parsedArgs.carbon,
|
|
51
|
+
name: args.name,
|
|
52
|
+
result: getToolErrorResult({ error }),
|
|
53
|
+
startTimeMs,
|
|
54
|
+
status: createErrorStatus({ error }),
|
|
55
|
+
captureToolCall: args.captureToolCall
|
|
56
|
+
});
|
|
57
|
+
throw error;
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
function parseToolCallArgs(args) {
|
|
62
|
+
const lastArg = args.args.at(-1);
|
|
63
|
+
const options = getToolCallOptions({ value: lastArg });
|
|
64
|
+
if (options) {
|
|
65
|
+
return {
|
|
66
|
+
carbon: options,
|
|
67
|
+
toolArgs: args.args.slice(0, -1)
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
return {
|
|
71
|
+
toolArgs: args.args
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
function getToolCallOptions(args) {
|
|
75
|
+
if (!args.value || typeof args.value !== "object") {
|
|
76
|
+
return null;
|
|
77
|
+
}
|
|
78
|
+
const keys = Object.keys(args.value);
|
|
79
|
+
if (!keys.some((key) => key === "context" || key === "additionalProperties" || key === "traceId")) {
|
|
80
|
+
return null;
|
|
81
|
+
}
|
|
82
|
+
return args.value;
|
|
83
|
+
}
|
|
84
|
+
function captureToolResult(args) {
|
|
85
|
+
args.captureToolCall({
|
|
86
|
+
event: buildToolEvent({
|
|
87
|
+
context: args.carbon?.context,
|
|
88
|
+
additionalProperties: args.carbon?.additionalProperties,
|
|
89
|
+
endTimeMs: Date.now(),
|
|
90
|
+
instrumentation: createSdkInstrumentation({
|
|
91
|
+
sourceFunction: "wrapTool",
|
|
92
|
+
sourcePackage: "ai",
|
|
93
|
+
sourceProvider: "vercel"
|
|
94
|
+
}),
|
|
95
|
+
startTimeMs: args.startTimeMs,
|
|
96
|
+
status: args.status ?? createOkStatus(),
|
|
97
|
+
traceId: args.carbon?.traceId,
|
|
98
|
+
tool: {
|
|
99
|
+
args: stringify({ value: serializeToolArgs({ args: args.args }) }),
|
|
100
|
+
name: args.name,
|
|
101
|
+
result: stringify({ value: args.result })
|
|
102
|
+
}
|
|
103
|
+
})
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
function serializeToolArgs(args) {
|
|
107
|
+
if (args.args.length === 1) {
|
|
108
|
+
return args.args[0];
|
|
109
|
+
}
|
|
110
|
+
return args.args;
|
|
111
|
+
}
|
|
112
|
+
function getToolErrorResult(args) {
|
|
113
|
+
if (args.error instanceof Error) {
|
|
114
|
+
return args.error.message;
|
|
115
|
+
}
|
|
116
|
+
return args.error;
|
|
117
|
+
}
|
|
118
|
+
export {
|
|
119
|
+
createWrappedTool
|
|
120
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { T_EventTransport, T_EventBatch } from './types.mjs';
|
|
2
|
+
import '../../internal/schema/events.mjs';
|
|
3
|
+
import 'zod';
|
|
4
|
+
|
|
5
|
+
type T_FileTransport = {
|
|
6
|
+
dirPath?: string;
|
|
7
|
+
};
|
|
8
|
+
declare class FileTransport implements T_EventTransport {
|
|
9
|
+
private readonly dirPath;
|
|
10
|
+
constructor(args?: T_FileTransport);
|
|
11
|
+
sendBatch(args: {
|
|
12
|
+
batch: T_EventBatch;
|
|
13
|
+
}): Promise<void>;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export { FileTransport };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { mkdirSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { dirname, join } from "node:path";
|
|
3
|
+
import { generateId } from "../../utils/ids.mjs";
|
|
4
|
+
class FileTransport {
|
|
5
|
+
dirPath;
|
|
6
|
+
constructor(args = {}) {
|
|
7
|
+
this.dirPath = args.dirPath ?? join(process.cwd(), ".tmp");
|
|
8
|
+
}
|
|
9
|
+
async sendBatch(args) {
|
|
10
|
+
const outputPath = join(this.dirPath, Date.now().toString() + "-" + generateId() + ".json");
|
|
11
|
+
mkdirSync(dirname(outputPath), { recursive: true });
|
|
12
|
+
writeFileSync(outputPath, JSON.stringify(args.batch, null, 2));
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
export {
|
|
16
|
+
FileTransport
|
|
17
|
+
};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { T_EventTransport, T_EventBatch } from './types.mjs';
|
|
2
|
+
import '../../internal/schema/events.mjs';
|
|
3
|
+
import 'zod';
|
|
4
|
+
|
|
5
|
+
type T_HttpTransport = {
|
|
6
|
+
apiKey?: string;
|
|
7
|
+
baseUrl?: string;
|
|
8
|
+
retryMaxAttempts?: number;
|
|
9
|
+
retryMaxElapsedMs?: number;
|
|
10
|
+
timeoutMs?: number;
|
|
11
|
+
};
|
|
12
|
+
declare class CarbonIngestError extends Error {
|
|
13
|
+
readonly retryable: boolean;
|
|
14
|
+
readonly status: number;
|
|
15
|
+
constructor(args: {
|
|
16
|
+
status: number;
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
declare class HttpTransport implements T_EventTransport {
|
|
20
|
+
private readonly apiKey;
|
|
21
|
+
private readonly retryMaxAttempts;
|
|
22
|
+
private readonly retryMaxElapsedMs;
|
|
23
|
+
private readonly timeoutMs;
|
|
24
|
+
private readonly url;
|
|
25
|
+
constructor(args: T_HttpTransport);
|
|
26
|
+
sendBatch(args: {
|
|
27
|
+
batch: T_EventBatch;
|
|
28
|
+
}): Promise<void>;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export { CarbonIngestError, HttpTransport };
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { CONSTANTS } from "../../lib/constants.mjs";
|
|
2
|
+
import { retry } from "../../utils/retry.mjs";
|
|
3
|
+
import { runWithTimeout } from "../../utils/timeout.mjs";
|
|
4
|
+
class CarbonIngestError extends Error {
|
|
5
|
+
retryable;
|
|
6
|
+
status;
|
|
7
|
+
constructor(args) {
|
|
8
|
+
super(`Carbon ingest failed with status ${args.status}`);
|
|
9
|
+
this.name = "CarbonIngestError";
|
|
10
|
+
this.status = args.status;
|
|
11
|
+
this.retryable = [408, 409, 425, 429].includes(args.status) || args.status >= 500;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
class HttpTransport {
|
|
15
|
+
apiKey;
|
|
16
|
+
retryMaxAttempts;
|
|
17
|
+
retryMaxElapsedMs;
|
|
18
|
+
timeoutMs;
|
|
19
|
+
url;
|
|
20
|
+
constructor(args) {
|
|
21
|
+
this.apiKey = getApiKey({ apiKey: args.apiKey });
|
|
22
|
+
this.retryMaxAttempts = validatePositiveInteger({
|
|
23
|
+
name: "retryMaxAttempts",
|
|
24
|
+
value: args.retryMaxAttempts ?? CONSTANTS.api.retryMaxAttempts
|
|
25
|
+
});
|
|
26
|
+
this.retryMaxElapsedMs = validatePositiveInteger({
|
|
27
|
+
name: "retryMaxElapsedMs",
|
|
28
|
+
value: args.retryMaxElapsedMs ?? CONSTANTS.api.retryMaxElapsedMs
|
|
29
|
+
});
|
|
30
|
+
this.timeoutMs = validatePositiveInteger({
|
|
31
|
+
name: "timeoutMs",
|
|
32
|
+
value: args.timeoutMs ?? CONSTANTS.api.timeoutMs
|
|
33
|
+
});
|
|
34
|
+
this.url = (args.baseUrl ?? CONSTANTS.api.baseUrl).trim().replace(/\/+$/, "") + CONSTANTS.api.ingestPath;
|
|
35
|
+
}
|
|
36
|
+
async sendBatch(args) {
|
|
37
|
+
await retry({
|
|
38
|
+
maxAttempts: this.retryMaxAttempts,
|
|
39
|
+
maxElapsedMs: this.retryMaxElapsedMs,
|
|
40
|
+
run: async () => {
|
|
41
|
+
await runWithTimeout({
|
|
42
|
+
timeoutMs: this.timeoutMs,
|
|
43
|
+
run: async ({ signal }) => {
|
|
44
|
+
const response = await fetch(this.url, {
|
|
45
|
+
method: "POST",
|
|
46
|
+
headers: {
|
|
47
|
+
authorization: `Bearer ${this.apiKey}`,
|
|
48
|
+
"content-type": "application/json"
|
|
49
|
+
},
|
|
50
|
+
body: JSON.stringify(args.batch),
|
|
51
|
+
signal
|
|
52
|
+
});
|
|
53
|
+
if (!response.ok) {
|
|
54
|
+
throw new CarbonIngestError({ status: response.status });
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
},
|
|
59
|
+
shouldRetry: ({ error }) => !(error instanceof CarbonIngestError) || error.retryable
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
function getApiKey(args) {
|
|
64
|
+
const apiKey = args.apiKey ?? process.env.CARBON_API_KEY;
|
|
65
|
+
if (!apiKey) {
|
|
66
|
+
throw new Error("CARBON_API_KEY is required when using the HTTP transport.");
|
|
67
|
+
}
|
|
68
|
+
return apiKey;
|
|
69
|
+
}
|
|
70
|
+
function validatePositiveInteger(args) {
|
|
71
|
+
if (!Number.isInteger(args.value) || args.value <= 0) {
|
|
72
|
+
throw new Error(`${args.name} must be a positive integer.`);
|
|
73
|
+
}
|
|
74
|
+
return args.value;
|
|
75
|
+
}
|
|
76
|
+
export {
|
|
77
|
+
CarbonIngestError,
|
|
78
|
+
HttpTransport
|
|
79
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { T_EventTransport, T_EventBatch } from './types.mjs';
|
|
2
|
+
import '../../internal/schema/events.mjs';
|
|
3
|
+
import 'zod';
|
|
4
|
+
|
|
5
|
+
declare class MemoryTransport implements T_EventTransport {
|
|
6
|
+
readonly batches: T_EventBatch[];
|
|
7
|
+
sendBatch(args: {
|
|
8
|
+
batch: T_EventBatch;
|
|
9
|
+
}): Promise<void>;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export { MemoryTransport };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { a as T_Event } from '../../internal/schema/events.mjs';
|
|
2
|
+
import 'zod';
|
|
3
|
+
|
|
4
|
+
type T_EventTransport = {
|
|
5
|
+
sendBatch: (args: {
|
|
6
|
+
batch: T_EventBatch;
|
|
7
|
+
}) => Promise<void>;
|
|
8
|
+
};
|
|
9
|
+
type T_EventBatch = {
|
|
10
|
+
events: T_Event[];
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export type { T_EventBatch, T_EventTransport };
|
|
File without changes
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { c as T_EventAiUsage, d as T_EventAdditionalProperties, e as T_EventContext, f as T_EventBase, T as T_LLMEvent, g as T_EventStatus, b as T_ToolEvent } from '../../internal/schema/events.mjs';
|
|
2
|
+
import 'zod';
|
|
3
|
+
|
|
4
|
+
type T_EventContextInput = Partial<Record<keyof T_EventContext, string>>;
|
|
5
|
+
type T_EventPropertiesInput = {
|
|
6
|
+
additionalProperties?: T_EventAdditionalProperties;
|
|
7
|
+
context?: T_EventContextInput;
|
|
8
|
+
};
|
|
9
|
+
declare const EMPTY_AI_USAGE: T_EventAiUsage;
|
|
10
|
+
declare const createOkStatus: () => T_EventStatus;
|
|
11
|
+
declare const createErrorStatus: (args: {
|
|
12
|
+
error: unknown;
|
|
13
|
+
}) => T_EventStatus;
|
|
14
|
+
declare function buildLlmEvent(args: T_EventPropertiesInput & {
|
|
15
|
+
endTimeMs: number;
|
|
16
|
+
id?: string;
|
|
17
|
+
instrumentation?: T_EventBase["instrumentation"];
|
|
18
|
+
llm: T_LLMEvent["properties"]["llm"];
|
|
19
|
+
startTimeMs: number;
|
|
20
|
+
status: T_EventStatus;
|
|
21
|
+
traceId?: string;
|
|
22
|
+
}): T_LLMEvent;
|
|
23
|
+
declare function buildToolEvent(args: T_EventPropertiesInput & {
|
|
24
|
+
endTimeMs: number;
|
|
25
|
+
id?: string;
|
|
26
|
+
instrumentation?: T_EventBase["instrumentation"];
|
|
27
|
+
startTimeMs: number;
|
|
28
|
+
status: T_EventStatus;
|
|
29
|
+
tool: T_ToolEvent["properties"]["tool"];
|
|
30
|
+
traceId?: string;
|
|
31
|
+
}): T_ToolEvent;
|
|
32
|
+
|
|
33
|
+
export { EMPTY_AI_USAGE, buildLlmEvent, buildToolEvent, createErrorStatus, createOkStatus };
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import { generateId } from "../../utils/ids.mjs";
|
|
2
|
+
import { createSdkInstrumentation } from "./instrumentation.mjs";
|
|
3
|
+
import { Z_Event } from "../../internal/schema/index.mjs";
|
|
4
|
+
const EMPTY_AI_USAGE = {
|
|
5
|
+
inputTokenDetails: {
|
|
6
|
+
cacheReadTokens: 0,
|
|
7
|
+
cacheWriteTokens: 0,
|
|
8
|
+
uncachedTokens: 0
|
|
9
|
+
},
|
|
10
|
+
inputTokens: 0,
|
|
11
|
+
outputTokenDetails: {
|
|
12
|
+
reasoningTokens: 0,
|
|
13
|
+
responseTokens: 0
|
|
14
|
+
},
|
|
15
|
+
outputTokens: 0,
|
|
16
|
+
totalTokens: 0
|
|
17
|
+
};
|
|
18
|
+
const createOkStatus = () => {
|
|
19
|
+
return {
|
|
20
|
+
state: "ok"
|
|
21
|
+
};
|
|
22
|
+
};
|
|
23
|
+
const createErrorStatus = (args) => {
|
|
24
|
+
const message = args.error instanceof Error ? args.error.message : String(args.error);
|
|
25
|
+
return {
|
|
26
|
+
error: {
|
|
27
|
+
...getErrorCode({ error: args.error }) ? { code: getErrorCode({ error: args.error }) } : {},
|
|
28
|
+
...getHttpStatus({ error: args.error }) ? { httpStatus: getHttpStatus({ error: args.error }) } : {},
|
|
29
|
+
message
|
|
30
|
+
},
|
|
31
|
+
state: "error"
|
|
32
|
+
};
|
|
33
|
+
};
|
|
34
|
+
function buildLlmEvent(args) {
|
|
35
|
+
const event = {
|
|
36
|
+
...buildBaseEvent({
|
|
37
|
+
additionalProperties: args.additionalProperties,
|
|
38
|
+
context: args.context,
|
|
39
|
+
endTimeMs: args.endTimeMs,
|
|
40
|
+
id: args.id,
|
|
41
|
+
instrumentation: args.instrumentation,
|
|
42
|
+
startTimeMs: args.startTimeMs,
|
|
43
|
+
status: args.status,
|
|
44
|
+
traceId: args.traceId,
|
|
45
|
+
type: "llm"
|
|
46
|
+
}),
|
|
47
|
+
properties: {
|
|
48
|
+
llm: args.llm
|
|
49
|
+
},
|
|
50
|
+
type: "llm"
|
|
51
|
+
};
|
|
52
|
+
return Z_Event.parse(event);
|
|
53
|
+
}
|
|
54
|
+
function buildToolEvent(args) {
|
|
55
|
+
const event = {
|
|
56
|
+
...buildBaseEvent({
|
|
57
|
+
additionalProperties: args.additionalProperties,
|
|
58
|
+
context: args.context,
|
|
59
|
+
endTimeMs: args.endTimeMs,
|
|
60
|
+
id: args.id,
|
|
61
|
+
instrumentation: args.instrumentation,
|
|
62
|
+
startTimeMs: args.startTimeMs,
|
|
63
|
+
status: args.status,
|
|
64
|
+
traceId: args.traceId,
|
|
65
|
+
type: "tool"
|
|
66
|
+
}),
|
|
67
|
+
properties: {
|
|
68
|
+
tool: args.tool
|
|
69
|
+
},
|
|
70
|
+
type: "tool"
|
|
71
|
+
};
|
|
72
|
+
return Z_Event.parse(event);
|
|
73
|
+
}
|
|
74
|
+
function buildBaseEvent(args) {
|
|
75
|
+
return {
|
|
76
|
+
additionalProperties: args.additionalProperties ?? {},
|
|
77
|
+
context: buildEventContext({ context: args.context }),
|
|
78
|
+
durationMs: Math.max(args.endTimeMs - args.startTimeMs, 0),
|
|
79
|
+
endTimeMs: args.endTimeMs,
|
|
80
|
+
id: args.id ?? generateId(),
|
|
81
|
+
instrumentation: args.instrumentation ?? createSdkInstrumentation({
|
|
82
|
+
sourceFunction: "manual",
|
|
83
|
+
sourcePackage: "ai",
|
|
84
|
+
sourceProvider: "vercel"
|
|
85
|
+
}),
|
|
86
|
+
startTimeMs: args.startTimeMs,
|
|
87
|
+
status: args.status,
|
|
88
|
+
...args.traceId ? { traceId: args.traceId } : {},
|
|
89
|
+
type: args.type
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
function buildEventContext(args) {
|
|
93
|
+
return {
|
|
94
|
+
...args.context?.agentId ? { agentId: args.context.agentId } : {},
|
|
95
|
+
...args.context?.agentGroupId ? { agentGroupId: args.context.agentGroupId } : {},
|
|
96
|
+
...args.context?.threadId ? { threadId: args.context.threadId } : {},
|
|
97
|
+
...args.context?.userId ? { userId: args.context.userId } : {}
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
function getErrorCode(args) {
|
|
101
|
+
const error = asRecord({ value: args.error });
|
|
102
|
+
if (typeof error?.code === "string") {
|
|
103
|
+
return error.code;
|
|
104
|
+
}
|
|
105
|
+
if (typeof error?.type === "string") {
|
|
106
|
+
return error.type;
|
|
107
|
+
}
|
|
108
|
+
if (args.error instanceof Error) {
|
|
109
|
+
return args.error.name;
|
|
110
|
+
}
|
|
111
|
+
return void 0;
|
|
112
|
+
}
|
|
113
|
+
function getHttpStatus(args) {
|
|
114
|
+
const error = asRecord({ value: args.error });
|
|
115
|
+
if (typeof error?.status === "number") {
|
|
116
|
+
return error.status;
|
|
117
|
+
}
|
|
118
|
+
if (typeof error?.statusCode === "number") {
|
|
119
|
+
return error.statusCode;
|
|
120
|
+
}
|
|
121
|
+
return void 0;
|
|
122
|
+
}
|
|
123
|
+
function asRecord(args) {
|
|
124
|
+
return typeof args.value === "object" && args.value !== null ? args.value : null;
|
|
125
|
+
}
|
|
126
|
+
export {
|
|
127
|
+
EMPTY_AI_USAGE,
|
|
128
|
+
buildLlmEvent,
|
|
129
|
+
buildToolEvent,
|
|
130
|
+
createErrorStatus,
|
|
131
|
+
createOkStatus
|
|
132
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { f as T_EventBase } from '../../internal/schema/events.mjs';
|
|
2
|
+
import 'zod';
|
|
3
|
+
|
|
4
|
+
type T_SourceProvider = T_EventBase["instrumentation"]["sourceProvider"];
|
|
5
|
+
type T_SourcePackage = T_EventBase["instrumentation"]["sourcePackage"];
|
|
6
|
+
declare function createSdkInstrumentation(args: {
|
|
7
|
+
sourceFunction?: string;
|
|
8
|
+
sourcePackage: T_SourcePackage;
|
|
9
|
+
sourceProvider: T_SourceProvider;
|
|
10
|
+
}): T_EventBase["instrumentation"];
|
|
11
|
+
|
|
12
|
+
export { createSdkInstrumentation };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
function createSdkInstrumentation(args) {
|
|
2
|
+
return {
|
|
3
|
+
provider: "carbon",
|
|
4
|
+
providerPackage: "sdk",
|
|
5
|
+
sourceFunction: args.sourceFunction,
|
|
6
|
+
sourcePackage: args.sourcePackage,
|
|
7
|
+
sourceProvider: args.sourceProvider
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
export {
|
|
11
|
+
createSdkInstrumentation
|
|
12
|
+
};
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export { Carbon, T_CarbonOptions } from './core/carbon.mjs';
|
|
2
|
+
export { T_CarbonContext, T_CarbonObject } from './core/schema/carbon-object.mjs';
|
|
3
|
+
export { FileTransport } from './core/transport/file-transport.mjs';
|
|
4
|
+
export { CarbonIngestError, HttpTransport } from './core/transport/http-transport.mjs';
|
|
5
|
+
export { MemoryTransport } from './core/transport/memory-transport.mjs';
|
|
6
|
+
export { T_EventBatch, T_EventTransport } from './core/transport/types.mjs';
|
|
7
|
+
export { T_WrapToolArgs } from './core/tools/wrap-tool.mjs';
|
|
8
|
+
import './core/runtime.mjs';
|
|
9
|
+
import './internal/schema/events.mjs';
|
|
10
|
+
import 'zod';
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Carbon } from "./core/carbon.mjs";
|
|
2
|
+
import { FileTransport } from "./core/transport/file-transport.mjs";
|
|
3
|
+
import { HttpTransport, CarbonIngestError } from "./core/transport/http-transport.mjs";
|
|
4
|
+
import { MemoryTransport } from "./core/transport/memory-transport.mjs";
|
|
5
|
+
export {
|
|
6
|
+
Carbon,
|
|
7
|
+
CarbonIngestError,
|
|
8
|
+
FileTransport,
|
|
9
|
+
HttpTransport,
|
|
10
|
+
MemoryTransport
|
|
11
|
+
};
|