@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,137 @@
|
|
|
1
|
+
import { createErrorStatus, createOkStatus } from "../../../core/utils/build-events.mjs";
|
|
2
|
+
import { OpenAIEventFactory } from "../event-factory.mjs";
|
|
3
|
+
function createWrappedResponseCreate(args) {
|
|
4
|
+
const createResponse = args.client.responses.create.bind(args.client.responses);
|
|
5
|
+
return ((body, requestOptions) => {
|
|
6
|
+
const { carbon: carbonObject, ...openAiBody } = body;
|
|
7
|
+
const factory = new OpenAIEventFactory({
|
|
8
|
+
carbonObject
|
|
9
|
+
});
|
|
10
|
+
const startTimeMs = Date.now();
|
|
11
|
+
let responsePromise;
|
|
12
|
+
try {
|
|
13
|
+
responsePromise = createResponse(openAiBody, requestOptions);
|
|
14
|
+
} catch (error) {
|
|
15
|
+
args.carbon.captureEvents({
|
|
16
|
+
events: factory.createResponseEvents({
|
|
17
|
+
body: openAiBody,
|
|
18
|
+
endTimeMs: Date.now(),
|
|
19
|
+
mode: openAiBody.stream ? "stream" : void 0,
|
|
20
|
+
startTimeMs,
|
|
21
|
+
sourceFunction: "responses.create",
|
|
22
|
+
status: createErrorStatus({ error })
|
|
23
|
+
})
|
|
24
|
+
});
|
|
25
|
+
throw error;
|
|
26
|
+
}
|
|
27
|
+
if (openAiBody.stream) {
|
|
28
|
+
attachResponseCreateStreamCapture({
|
|
29
|
+
body: openAiBody,
|
|
30
|
+
carbon: args.carbon,
|
|
31
|
+
factory,
|
|
32
|
+
responsePromise,
|
|
33
|
+
startTimeMs
|
|
34
|
+
});
|
|
35
|
+
return responsePromise;
|
|
36
|
+
}
|
|
37
|
+
void Promise.resolve(responsePromise).then(
|
|
38
|
+
(response) => {
|
|
39
|
+
args.carbon.captureEvents({
|
|
40
|
+
events: factory.createResponseEvents({
|
|
41
|
+
body: openAiBody,
|
|
42
|
+
endTimeMs: Date.now(),
|
|
43
|
+
response,
|
|
44
|
+
startTimeMs,
|
|
45
|
+
sourceFunction: "responses.create",
|
|
46
|
+
status: createOkStatus()
|
|
47
|
+
})
|
|
48
|
+
});
|
|
49
|
+
},
|
|
50
|
+
(error) => {
|
|
51
|
+
args.carbon.captureEvents({
|
|
52
|
+
events: factory.createResponseEvents({
|
|
53
|
+
body: openAiBody,
|
|
54
|
+
endTimeMs: Date.now(),
|
|
55
|
+
startTimeMs,
|
|
56
|
+
sourceFunction: "responses.create",
|
|
57
|
+
status: createErrorStatus({ error })
|
|
58
|
+
})
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
);
|
|
62
|
+
return responsePromise;
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
function attachResponseCreateStreamCapture(args) {
|
|
66
|
+
void Promise.resolve(args.responsePromise).then(
|
|
67
|
+
(stream) => {
|
|
68
|
+
captureResponseCreateStream({
|
|
69
|
+
...args,
|
|
70
|
+
stream
|
|
71
|
+
});
|
|
72
|
+
},
|
|
73
|
+
(error) => {
|
|
74
|
+
args.carbon.captureEvents({
|
|
75
|
+
events: args.factory.createResponseEvents({
|
|
76
|
+
body: args.body,
|
|
77
|
+
endTimeMs: Date.now(),
|
|
78
|
+
mode: "stream",
|
|
79
|
+
startTimeMs: args.startTimeMs,
|
|
80
|
+
sourceFunction: "responses.create",
|
|
81
|
+
status: createErrorStatus({ error })
|
|
82
|
+
})
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
function captureResponseCreateStream(args) {
|
|
88
|
+
const streamWithIterator = args.stream;
|
|
89
|
+
const originalIterator = (streamWithIterator.iterator ?? args.stream[Symbol.asyncIterator]).bind(args.stream);
|
|
90
|
+
let captured = false;
|
|
91
|
+
const wrappedIterator = async function* () {
|
|
92
|
+
try {
|
|
93
|
+
for await (const event of {
|
|
94
|
+
[Symbol.asyncIterator]: originalIterator
|
|
95
|
+
}) {
|
|
96
|
+
if (!captured && event.type === "response.completed") {
|
|
97
|
+
captured = true;
|
|
98
|
+
args.carbon.captureEvents({
|
|
99
|
+
events: args.factory.createResponseEvents({
|
|
100
|
+
body: args.body,
|
|
101
|
+
endTimeMs: Date.now(),
|
|
102
|
+
mode: "stream",
|
|
103
|
+
response: event.response,
|
|
104
|
+
startTimeMs: args.startTimeMs,
|
|
105
|
+
sourceFunction: "responses.create",
|
|
106
|
+
status: createOkStatus()
|
|
107
|
+
})
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
yield event;
|
|
111
|
+
}
|
|
112
|
+
} catch (error) {
|
|
113
|
+
if (!captured) {
|
|
114
|
+
captured = true;
|
|
115
|
+
args.carbon.captureEvents({
|
|
116
|
+
events: args.factory.createResponseEvents({
|
|
117
|
+
body: args.body,
|
|
118
|
+
endTimeMs: Date.now(),
|
|
119
|
+
mode: "stream",
|
|
120
|
+
startTimeMs: args.startTimeMs,
|
|
121
|
+
sourceFunction: "responses.create",
|
|
122
|
+
status: createErrorStatus({ error })
|
|
123
|
+
})
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
throw error;
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
if (streamWithIterator.iterator) {
|
|
130
|
+
streamWithIterator.iterator = wrappedIterator;
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
args.stream[Symbol.asyncIterator] = wrappedIterator;
|
|
134
|
+
}
|
|
135
|
+
export {
|
|
136
|
+
createWrappedResponseCreate
|
|
137
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Carbon } from '../../../core/carbon.mjs';
|
|
2
|
+
import OpenAI from 'openai';
|
|
3
|
+
import '../../../core/schema/carbon-object.mjs';
|
|
4
|
+
import '../../../internal/schema/events.mjs';
|
|
5
|
+
import 'zod';
|
|
6
|
+
import '../../../core/runtime.mjs';
|
|
7
|
+
import '../../../core/transport/types.mjs';
|
|
8
|
+
import '../../../core/tools/wrap-tool.mjs';
|
|
9
|
+
|
|
10
|
+
declare function createWrappedResponseStream(args: {
|
|
11
|
+
client: OpenAI;
|
|
12
|
+
carbon: Carbon;
|
|
13
|
+
}): typeof args.client.responses.stream;
|
|
14
|
+
|
|
15
|
+
export { createWrappedResponseStream };
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { createErrorStatus, createOkStatus } from "../../../core/utils/build-events.mjs";
|
|
2
|
+
import { OpenAIEventFactory } from "../event-factory.mjs";
|
|
3
|
+
function createWrappedResponseStream(args) {
|
|
4
|
+
const streamResponse = args.client.responses.stream.bind(args.client.responses);
|
|
5
|
+
return ((body, requestOptions) => {
|
|
6
|
+
const { carbon: carbonObject, ...openAiBody } = body;
|
|
7
|
+
const factory = new OpenAIEventFactory({
|
|
8
|
+
carbonObject
|
|
9
|
+
});
|
|
10
|
+
const startTimeMs = Date.now();
|
|
11
|
+
let responseStream;
|
|
12
|
+
try {
|
|
13
|
+
responseStream = streamResponse(openAiBody, requestOptions);
|
|
14
|
+
} catch (error) {
|
|
15
|
+
args.carbon.captureEvents({
|
|
16
|
+
events: factory.createResponseEvents({
|
|
17
|
+
body: openAiBody,
|
|
18
|
+
endTimeMs: Date.now(),
|
|
19
|
+
mode: "stream",
|
|
20
|
+
sourceFunction: "responses.stream",
|
|
21
|
+
startTimeMs,
|
|
22
|
+
status: createErrorStatus({ error })
|
|
23
|
+
})
|
|
24
|
+
});
|
|
25
|
+
throw error;
|
|
26
|
+
}
|
|
27
|
+
void responseStream.finalResponse().then(
|
|
28
|
+
(response) => {
|
|
29
|
+
args.carbon.captureEvents({
|
|
30
|
+
events: factory.createResponseEvents({
|
|
31
|
+
body: openAiBody,
|
|
32
|
+
endTimeMs: Date.now(),
|
|
33
|
+
mode: "stream",
|
|
34
|
+
response,
|
|
35
|
+
sourceFunction: "responses.stream",
|
|
36
|
+
startTimeMs,
|
|
37
|
+
status: createOkStatus()
|
|
38
|
+
})
|
|
39
|
+
});
|
|
40
|
+
},
|
|
41
|
+
(error) => {
|
|
42
|
+
args.carbon.captureEvents({
|
|
43
|
+
events: factory.createResponseEvents({
|
|
44
|
+
body: openAiBody,
|
|
45
|
+
endTimeMs: Date.now(),
|
|
46
|
+
mode: "stream",
|
|
47
|
+
sourceFunction: "responses.stream",
|
|
48
|
+
startTimeMs,
|
|
49
|
+
status: createErrorStatus({ error })
|
|
50
|
+
})
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
);
|
|
54
|
+
return responseStream;
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
export {
|
|
58
|
+
createWrappedResponseStream
|
|
59
|
+
};
|
|
File without changes
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Carbon } from '../../core/carbon.mjs';
|
|
2
|
+
import { T_WrappedOpenAISdk } from './types.mjs';
|
|
3
|
+
import OpenAI from 'openai';
|
|
4
|
+
import '../../core/schema/carbon-object.mjs';
|
|
5
|
+
import '../../internal/schema/events.mjs';
|
|
6
|
+
import 'zod';
|
|
7
|
+
import '../../core/runtime.mjs';
|
|
8
|
+
import '../../core/transport/types.mjs';
|
|
9
|
+
import '../../core/tools/wrap-tool.mjs';
|
|
10
|
+
|
|
11
|
+
declare const wrapOpenAISdk: (client: OpenAI, carbon: Carbon) => T_WrappedOpenAISdk;
|
|
12
|
+
|
|
13
|
+
export { wrapOpenAISdk };
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { createWrappedChatCompletionCreate } from "./fns/chat-completions-create.mjs";
|
|
2
|
+
import { createWrappedChatCompletionRunTools } from "./fns/chat-completions-run-tools.mjs";
|
|
3
|
+
import { createWrappedChatCompletionStream } from "./fns/chat-completions-stream.mjs";
|
|
4
|
+
import { createWrappedResponseCreate } from "./fns/response-create.mjs";
|
|
5
|
+
import { createWrappedResponseStream } from "./fns/response-stream.mjs";
|
|
6
|
+
const wrapOpenAISdk = (client, carbon) => {
|
|
7
|
+
const wrappedClient = client;
|
|
8
|
+
wrappedClient.responses.create = createWrappedResponseCreate({
|
|
9
|
+
client,
|
|
10
|
+
carbon
|
|
11
|
+
});
|
|
12
|
+
wrappedClient.responses.stream = createWrappedResponseStream({
|
|
13
|
+
client,
|
|
14
|
+
carbon
|
|
15
|
+
});
|
|
16
|
+
wrappedClient.chat.completions.create = createWrappedChatCompletionCreate({
|
|
17
|
+
client,
|
|
18
|
+
carbon
|
|
19
|
+
});
|
|
20
|
+
wrappedClient.chat.completions.stream = createWrappedChatCompletionStream({
|
|
21
|
+
client,
|
|
22
|
+
carbon
|
|
23
|
+
});
|
|
24
|
+
wrappedClient.chat.completions.runTools = createWrappedChatCompletionRunTools({
|
|
25
|
+
client,
|
|
26
|
+
carbon
|
|
27
|
+
});
|
|
28
|
+
return wrappedClient;
|
|
29
|
+
};
|
|
30
|
+
export {
|
|
31
|
+
wrapOpenAISdk
|
|
32
|
+
};
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { T_CarbonObject } from '../../core/schema/carbon-object.mjs';
|
|
2
|
+
import { T as T_LLMEvent, b as T_ToolEvent } from '../../internal/schema/events.mjs';
|
|
3
|
+
import { OnStartEvent, OnFinishEvent, OnStepStartEvent, OnStepFinishEvent, OnToolCallStartEvent, OnToolCallFinishEvent } from 'ai';
|
|
4
|
+
import 'zod';
|
|
5
|
+
|
|
6
|
+
type T_VercelRecordingMode = "generate" | "stream";
|
|
7
|
+
type T_LlmInputEvent = OnStartEvent | OnFinishEvent | OnStepStartEvent | OnStepFinishEvent | OnToolCallStartEvent | OnToolCallFinishEvent;
|
|
8
|
+
type T_PendingLlmEvent = Pick<T_LLMEvent, "id" | "startTimeMs"> & {
|
|
9
|
+
gateway: NonNullable<T_LLMEvent["properties"]["llm"]["gateway"]>;
|
|
10
|
+
input: T_LLMEvent["properties"]["llm"]["input"];
|
|
11
|
+
model: T_LLMEvent["properties"]["llm"]["model"];
|
|
12
|
+
};
|
|
13
|
+
type T_PendingToolEvent = Pick<T_ToolEvent, "id" | "startTimeMs"> & {
|
|
14
|
+
args: T_ToolEvent["properties"]["tool"]["args"];
|
|
15
|
+
toolName: T_ToolEvent["properties"]["tool"]["name"];
|
|
16
|
+
};
|
|
17
|
+
declare class VercelEventFactory {
|
|
18
|
+
private readonly carbonObject?;
|
|
19
|
+
private readonly mode;
|
|
20
|
+
private readonly sourceFunction;
|
|
21
|
+
private readonly traceId;
|
|
22
|
+
constructor(args: {
|
|
23
|
+
carbonObject?: T_CarbonObject;
|
|
24
|
+
mode: T_VercelRecordingMode;
|
|
25
|
+
sourceFunction: string;
|
|
26
|
+
});
|
|
27
|
+
createFallbackPendingLlmEvent(args: {
|
|
28
|
+
startTimeMs?: number;
|
|
29
|
+
}): T_PendingLlmEvent;
|
|
30
|
+
createPendingLlmEvent(args: {
|
|
31
|
+
event: T_LlmInputEvent;
|
|
32
|
+
startTimeMs?: number;
|
|
33
|
+
}): T_PendingLlmEvent;
|
|
34
|
+
createPendingToolEvent(args: {
|
|
35
|
+
event: OnToolCallStartEvent | OnToolCallFinishEvent;
|
|
36
|
+
startTimeMs?: number;
|
|
37
|
+
}): T_PendingToolEvent;
|
|
38
|
+
createCompletedLlmEvent(args: {
|
|
39
|
+
endTimeMs: number;
|
|
40
|
+
event?: OnFinishEvent | OnStepFinishEvent;
|
|
41
|
+
pendingLlm: T_PendingLlmEvent;
|
|
42
|
+
status: T_LLMEvent["status"];
|
|
43
|
+
}): T_LLMEvent;
|
|
44
|
+
createCompletedToolEvent(args: {
|
|
45
|
+
endTimeMs: number;
|
|
46
|
+
output?: unknown;
|
|
47
|
+
pendingTool: T_PendingToolEvent;
|
|
48
|
+
status: T_ToolEvent["status"];
|
|
49
|
+
}): T_ToolEvent;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export { type T_PendingLlmEvent, type T_PendingToolEvent, type T_VercelRecordingMode, VercelEventFactory };
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { buildLlmEvent, buildToolEvent } from "../../core/utils/build-events.mjs";
|
|
2
|
+
import { EMPTY_AI_USAGE } from "../../core/utils/build-events.mjs";
|
|
3
|
+
import { createSdkInstrumentation } from "../../core/utils/instrumentation.mjs";
|
|
4
|
+
import { generateId } from "../../utils/ids.mjs";
|
|
5
|
+
import { stringify } from "../../utils/stringify.mjs";
|
|
6
|
+
class VercelEventFactory {
|
|
7
|
+
carbonObject;
|
|
8
|
+
mode;
|
|
9
|
+
sourceFunction;
|
|
10
|
+
traceId;
|
|
11
|
+
constructor(args) {
|
|
12
|
+
this.carbonObject = args.carbonObject;
|
|
13
|
+
this.mode = args.mode;
|
|
14
|
+
this.sourceFunction = args.sourceFunction;
|
|
15
|
+
this.traceId = args.carbonObject?.traceId ?? generateId();
|
|
16
|
+
}
|
|
17
|
+
createFallbackPendingLlmEvent(args) {
|
|
18
|
+
return {
|
|
19
|
+
id: generateId(),
|
|
20
|
+
input: {
|
|
21
|
+
prompt: "",
|
|
22
|
+
system: "",
|
|
23
|
+
tools: []
|
|
24
|
+
},
|
|
25
|
+
gateway: "unknown",
|
|
26
|
+
model: "unknown",
|
|
27
|
+
startTimeMs: args.startTimeMs ?? Date.now()
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
createPendingLlmEvent(args) {
|
|
31
|
+
return {
|
|
32
|
+
id: generateId(),
|
|
33
|
+
input: {
|
|
34
|
+
prompt: stringify({
|
|
35
|
+
value: "messages" in args.event && args.event.messages ? args.event.messages : "prompt" in args.event ? args.event.prompt : void 0
|
|
36
|
+
}),
|
|
37
|
+
system: stringify({ value: "system" in args.event ? args.event.system : void 0 }),
|
|
38
|
+
tools: getToolDescriptions({
|
|
39
|
+
tools: "tools" in args.event ? args.event.tools : void 0
|
|
40
|
+
})
|
|
41
|
+
},
|
|
42
|
+
gateway: args.event.model?.provider ?? "unknown",
|
|
43
|
+
model: args.event.model?.modelId ?? "unknown",
|
|
44
|
+
startTimeMs: args.startTimeMs ?? Date.now()
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
createPendingToolEvent(args) {
|
|
48
|
+
return {
|
|
49
|
+
args: stringify({ value: args.event.toolCall.input }),
|
|
50
|
+
id: generateId(),
|
|
51
|
+
startTimeMs: args.startTimeMs ?? Date.now(),
|
|
52
|
+
toolName: args.event.toolCall.toolName
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
createCompletedLlmEvent(args) {
|
|
56
|
+
return buildLlmEvent({
|
|
57
|
+
context: this.carbonObject?.context,
|
|
58
|
+
additionalProperties: this.carbonObject?.additionalProperties,
|
|
59
|
+
endTimeMs: args.endTimeMs,
|
|
60
|
+
id: args.pendingLlm.id,
|
|
61
|
+
instrumentation: createSdkInstrumentation({
|
|
62
|
+
sourceFunction: this.sourceFunction,
|
|
63
|
+
sourcePackage: "ai",
|
|
64
|
+
sourceProvider: "vercel"
|
|
65
|
+
}),
|
|
66
|
+
llm: {
|
|
67
|
+
gateway: args.pendingLlm.gateway,
|
|
68
|
+
model: args.pendingLlm.model,
|
|
69
|
+
input: args.pendingLlm.input,
|
|
70
|
+
output: {
|
|
71
|
+
mode: this.mode,
|
|
72
|
+
reasoning: stringify({ value: args.event?.reasoningText }),
|
|
73
|
+
response: stringify({ value: args.event?.text }),
|
|
74
|
+
toolCalls: args.event?.toolCalls.map((toolCall) => ({
|
|
75
|
+
args: stringify({ value: toolCall.input }),
|
|
76
|
+
name: toolCall.toolName
|
|
77
|
+
})) ?? []
|
|
78
|
+
},
|
|
79
|
+
usage: toAiUsage({ usage: args.event?.usage })
|
|
80
|
+
},
|
|
81
|
+
startTimeMs: args.pendingLlm.startTimeMs,
|
|
82
|
+
status: args.status,
|
|
83
|
+
traceId: this.traceId
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
createCompletedToolEvent(args) {
|
|
87
|
+
return buildToolEvent({
|
|
88
|
+
context: this.carbonObject?.context,
|
|
89
|
+
additionalProperties: this.carbonObject?.additionalProperties,
|
|
90
|
+
endTimeMs: args.endTimeMs,
|
|
91
|
+
id: args.pendingTool.id,
|
|
92
|
+
instrumentation: createSdkInstrumentation({
|
|
93
|
+
sourceFunction: this.sourceFunction,
|
|
94
|
+
sourcePackage: "ai",
|
|
95
|
+
sourceProvider: "vercel"
|
|
96
|
+
}),
|
|
97
|
+
startTimeMs: args.pendingTool.startTimeMs,
|
|
98
|
+
status: args.status,
|
|
99
|
+
tool: {
|
|
100
|
+
args: args.pendingTool.args,
|
|
101
|
+
name: args.pendingTool.toolName,
|
|
102
|
+
result: stringify({ value: args.output })
|
|
103
|
+
},
|
|
104
|
+
traceId: this.traceId
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
function getToolDescriptions(args) {
|
|
109
|
+
if (!args.tools) {
|
|
110
|
+
return [];
|
|
111
|
+
}
|
|
112
|
+
return Object.entries(args.tools).map(([name, tool]) => ({
|
|
113
|
+
description: typeof tool.description === "string" ? tool.description : void 0,
|
|
114
|
+
name
|
|
115
|
+
}));
|
|
116
|
+
}
|
|
117
|
+
const toAiUsage = (args) => {
|
|
118
|
+
if (!args.usage) {
|
|
119
|
+
return EMPTY_AI_USAGE;
|
|
120
|
+
}
|
|
121
|
+
const inputTokenDetails = args.usage.inputTokenDetails ?? {};
|
|
122
|
+
const outputTokenDetails = args.usage.outputTokenDetails ?? {};
|
|
123
|
+
return {
|
|
124
|
+
inputTokenDetails: {
|
|
125
|
+
cacheReadTokens: inputTokenDetails.cacheReadTokens ?? 0,
|
|
126
|
+
cacheWriteTokens: inputTokenDetails.cacheWriteTokens ?? 0,
|
|
127
|
+
uncachedTokens: inputTokenDetails.noCacheTokens ?? 0
|
|
128
|
+
},
|
|
129
|
+
inputTokens: args.usage.inputTokens ?? 0,
|
|
130
|
+
outputTokenDetails: {
|
|
131
|
+
reasoningTokens: outputTokenDetails.reasoningTokens ?? 0,
|
|
132
|
+
responseTokens: outputTokenDetails.textTokens ?? 0
|
|
133
|
+
},
|
|
134
|
+
outputTokens: args.usage.outputTokens ?? 0,
|
|
135
|
+
totalTokens: args.usage.totalTokens ?? 0
|
|
136
|
+
};
|
|
137
|
+
};
|
|
138
|
+
export {
|
|
139
|
+
VercelEventFactory
|
|
140
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Carbon } from '../../../core/carbon.mjs';
|
|
2
|
+
import { T_WrappedToolLoopAgent } from '../types.mjs';
|
|
3
|
+
import * as vercelSdk from 'ai';
|
|
4
|
+
import '../../../core/schema/carbon-object.mjs';
|
|
5
|
+
import '../../../internal/schema/events.mjs';
|
|
6
|
+
import 'zod';
|
|
7
|
+
import '../../../core/runtime.mjs';
|
|
8
|
+
import '../../../core/transport/types.mjs';
|
|
9
|
+
import '../../../core/tools/wrap-tool.mjs';
|
|
10
|
+
|
|
11
|
+
type T_CreateWrappedToolLoopAgent = {
|
|
12
|
+
ToolLoopAgent: typeof vercelSdk.ToolLoopAgent;
|
|
13
|
+
carbon: Carbon;
|
|
14
|
+
};
|
|
15
|
+
declare const createWrappedToolLoopAgent: (args: T_CreateWrappedToolLoopAgent) => T_WrappedToolLoopAgent;
|
|
16
|
+
|
|
17
|
+
export { createWrappedToolLoopAgent };
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { VercelRecorder } from "../recorder.mjs";
|
|
2
|
+
import { addCarbonTelemetry } from "../utils/telemetry.mjs";
|
|
3
|
+
import * as vercelSdk from "ai";
|
|
4
|
+
const createWrappedToolLoopAgent = (args) => {
|
|
5
|
+
class WrappedToolLoopAgent {
|
|
6
|
+
version = "agent-v1";
|
|
7
|
+
carbonObject;
|
|
8
|
+
settings;
|
|
9
|
+
constructor(settings) {
|
|
10
|
+
const { carbon: carbonObject, ...agentSettings } = settings;
|
|
11
|
+
this.carbonObject = carbonObject;
|
|
12
|
+
this.settings = agentSettings;
|
|
13
|
+
}
|
|
14
|
+
get id() {
|
|
15
|
+
return this.settings.id;
|
|
16
|
+
}
|
|
17
|
+
get tools() {
|
|
18
|
+
return this.settings.tools;
|
|
19
|
+
}
|
|
20
|
+
generate(callArgs) {
|
|
21
|
+
return runToolLoopAgentCall({
|
|
22
|
+
ToolLoopAgent: args.ToolLoopAgent,
|
|
23
|
+
callArgs,
|
|
24
|
+
carbon: args.carbon,
|
|
25
|
+
carbonObject: this.carbonObject,
|
|
26
|
+
mode: "generate",
|
|
27
|
+
run: ({ agent, aiArgs }) => agent.generate(aiArgs),
|
|
28
|
+
settings: this.settings,
|
|
29
|
+
sourceFunction: "ToolLoopAgent.generate"
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
stream(callArgs) {
|
|
33
|
+
return runToolLoopAgentCall({
|
|
34
|
+
ToolLoopAgent: args.ToolLoopAgent,
|
|
35
|
+
callArgs,
|
|
36
|
+
carbon: args.carbon,
|
|
37
|
+
carbonObject: this.carbonObject,
|
|
38
|
+
mode: "stream",
|
|
39
|
+
run: ({ agent, aiArgs }) => agent.stream(aiArgs),
|
|
40
|
+
settings: this.settings,
|
|
41
|
+
sourceFunction: "ToolLoopAgent.stream"
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return WrappedToolLoopAgent;
|
|
46
|
+
};
|
|
47
|
+
function runToolLoopAgentCall(args) {
|
|
48
|
+
const { carbon: callCarbonObject, ...aiArgs } = args.callArgs;
|
|
49
|
+
const carbonObject = mergeCarbonObjects({
|
|
50
|
+
call: callCarbonObject,
|
|
51
|
+
defaults: args.carbonObject
|
|
52
|
+
});
|
|
53
|
+
const recorder = new VercelRecorder({
|
|
54
|
+
carbon: args.carbon,
|
|
55
|
+
carbonObject,
|
|
56
|
+
mode: args.mode,
|
|
57
|
+
sourceFunction: args.sourceFunction
|
|
58
|
+
});
|
|
59
|
+
try {
|
|
60
|
+
const agent = new args.ToolLoopAgent(
|
|
61
|
+
withCarbonTelemetry({
|
|
62
|
+
recorder,
|
|
63
|
+
settings: args.settings
|
|
64
|
+
})
|
|
65
|
+
);
|
|
66
|
+
return Promise.resolve(
|
|
67
|
+
args.run({
|
|
68
|
+
agent,
|
|
69
|
+
aiArgs
|
|
70
|
+
})
|
|
71
|
+
).catch((error) => {
|
|
72
|
+
recorder.recordError({ error });
|
|
73
|
+
throw error;
|
|
74
|
+
});
|
|
75
|
+
} catch (error) {
|
|
76
|
+
recorder.recordError({ error });
|
|
77
|
+
throw error;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
function mergeCarbonObjects(args) {
|
|
81
|
+
if (!args.call && !args.defaults) {
|
|
82
|
+
return void 0;
|
|
83
|
+
}
|
|
84
|
+
return {
|
|
85
|
+
context: {
|
|
86
|
+
...args.defaults?.context,
|
|
87
|
+
...args.call?.context
|
|
88
|
+
},
|
|
89
|
+
additionalProperties: {
|
|
90
|
+
...args.defaults?.additionalProperties,
|
|
91
|
+
...args.call?.additionalProperties
|
|
92
|
+
},
|
|
93
|
+
traceId: args.call?.traceId ?? args.defaults?.traceId
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
function withCarbonTelemetry(args) {
|
|
97
|
+
const settingsWithCarbonTelemetry = addCarbonTelemetry({
|
|
98
|
+
args: args.settings,
|
|
99
|
+
recorder: args.recorder
|
|
100
|
+
});
|
|
101
|
+
if (!args.settings.prepareCall) {
|
|
102
|
+
return settingsWithCarbonTelemetry;
|
|
103
|
+
}
|
|
104
|
+
return {
|
|
105
|
+
...settingsWithCarbonTelemetry,
|
|
106
|
+
prepareCall: async (options) => {
|
|
107
|
+
const preparedCallArgs = await args.settings.prepareCall?.(options);
|
|
108
|
+
return addCarbonTelemetry({
|
|
109
|
+
args: preparedCallArgs ?? options,
|
|
110
|
+
recorder: args.recorder
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
export {
|
|
116
|
+
createWrappedToolLoopAgent
|
|
117
|
+
};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { Carbon } from '../../core/carbon.mjs';
|
|
2
|
+
import { T_CarbonObject } from '../../core/schema/carbon-object.mjs';
|
|
3
|
+
import { T_VercelRecordingMode } from './event-factory.mjs';
|
|
4
|
+
import { TelemetryIntegration } from 'ai';
|
|
5
|
+
import '../../core/runtime.mjs';
|
|
6
|
+
import '../../internal/schema/events.mjs';
|
|
7
|
+
import 'zod';
|
|
8
|
+
import '../../core/transport/types.mjs';
|
|
9
|
+
import '../../core/tools/wrap-tool.mjs';
|
|
10
|
+
|
|
11
|
+
declare class VercelRecorder {
|
|
12
|
+
private readonly completedEvents;
|
|
13
|
+
private readonly factory;
|
|
14
|
+
private readonly llmEventsByStep;
|
|
15
|
+
private readonly carbon;
|
|
16
|
+
private readonly toolEventsById;
|
|
17
|
+
private isRecorded;
|
|
18
|
+
private startTimeMs;
|
|
19
|
+
constructor(args: {
|
|
20
|
+
carbon: Carbon;
|
|
21
|
+
carbonObject?: T_CarbonObject;
|
|
22
|
+
mode: T_VercelRecordingMode;
|
|
23
|
+
sourceFunction: string;
|
|
24
|
+
});
|
|
25
|
+
createIntegration(): TelemetryIntegration;
|
|
26
|
+
recordError(args: {
|
|
27
|
+
error: unknown;
|
|
28
|
+
}): void;
|
|
29
|
+
private onToolCallFinish;
|
|
30
|
+
private onFinish;
|
|
31
|
+
private recordEvents;
|
|
32
|
+
private getPendingLlmEvent;
|
|
33
|
+
private getPendingToolEvent;
|
|
34
|
+
private closePendingLlmEvent;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export { VercelRecorder };
|