@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
package/README.md
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# SDK
|
|
2
|
+
|
|
3
|
+
Native Carbon ingestion SDK for capturing canonical AI events and sending them to the Carbon ingest API.
|
|
4
|
+
|
|
5
|
+
## Usage
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
import { Carbon } from "@carbon-js/sdk";
|
|
9
|
+
import { wrapOpenAISdk } from "@carbon-js/sdk/ai";
|
|
10
|
+
|
|
11
|
+
const carbon = new Carbon({ apiKey: process.env.CARBON_API_KEY! });
|
|
12
|
+
const openai = wrapOpenAISdk(client, carbon);
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Development
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
bun install
|
|
19
|
+
bunx tsc --noEmit -p packages/sdk/tsconfig.json
|
|
20
|
+
bun test packages/sdk/tests
|
|
21
|
+
```
|
|
22
|
+
## Smoke Test
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
bun run --cwd packages/sdk example:vercel
|
|
26
|
+
```
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { T_CarbonObject } from '../../core/schema/carbon-object.mjs';
|
|
2
|
+
import { T as T_LLMEvent, a as T_Event } from '../../internal/schema/events.mjs';
|
|
3
|
+
import { MessageCreateParamsBase, Message } from '@anthropic-ai/sdk/resources/messages/messages';
|
|
4
|
+
import 'zod';
|
|
5
|
+
|
|
6
|
+
declare class AnthropicEventFactory {
|
|
7
|
+
private readonly carbonObject?;
|
|
8
|
+
constructor(args: {
|
|
9
|
+
carbonObject?: T_CarbonObject;
|
|
10
|
+
});
|
|
11
|
+
createMessageEvents(args: {
|
|
12
|
+
body: MessageCreateParamsBase;
|
|
13
|
+
endTimeMs: number;
|
|
14
|
+
message?: Message;
|
|
15
|
+
mode?: T_LLMEvent["properties"]["llm"]["output"]["mode"];
|
|
16
|
+
sourceFunction?: string;
|
|
17
|
+
startTimeMs: number;
|
|
18
|
+
status: T_LLMEvent["status"];
|
|
19
|
+
}): T_Event[];
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export { AnthropicEventFactory };
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { buildLlmEvent } 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 { stringify } from "../../utils/stringify.mjs";
|
|
5
|
+
class AnthropicEventFactory {
|
|
6
|
+
carbonObject;
|
|
7
|
+
constructor(args) {
|
|
8
|
+
this.carbonObject = args.carbonObject;
|
|
9
|
+
}
|
|
10
|
+
createMessageEvents(args) {
|
|
11
|
+
const usage = toAnthropicAiUsage({ usage: args.message?.usage });
|
|
12
|
+
return [
|
|
13
|
+
buildLlmEvent({
|
|
14
|
+
context: this.carbonObject?.context,
|
|
15
|
+
additionalProperties: this.carbonObject?.additionalProperties,
|
|
16
|
+
endTimeMs: args.endTimeMs,
|
|
17
|
+
instrumentation: createSdkInstrumentation({
|
|
18
|
+
sourceFunction: args.sourceFunction,
|
|
19
|
+
sourcePackage: "@anthropic-ai/sdk",
|
|
20
|
+
sourceProvider: "anthropic"
|
|
21
|
+
}),
|
|
22
|
+
llm: {
|
|
23
|
+
gateway: "anthropic",
|
|
24
|
+
model: String(args.message?.model ?? args.body.model ?? "unknown"),
|
|
25
|
+
input: {
|
|
26
|
+
prompt: stringify({ value: args.body.messages }),
|
|
27
|
+
system: stringify({ value: args.body.system }),
|
|
28
|
+
tools: getToolDescriptions({ tools: args.body.tools })
|
|
29
|
+
},
|
|
30
|
+
output: {
|
|
31
|
+
mode: args.mode ?? "generate",
|
|
32
|
+
reasoning: getReasoning({ content: args.message?.content }),
|
|
33
|
+
response: getResponseText({ content: args.message?.content }),
|
|
34
|
+
toolCalls: getToolCalls({ content: args.message?.content })
|
|
35
|
+
},
|
|
36
|
+
usage
|
|
37
|
+
},
|
|
38
|
+
startTimeMs: args.startTimeMs,
|
|
39
|
+
status: args.status,
|
|
40
|
+
traceId: this.carbonObject?.traceId
|
|
41
|
+
})
|
|
42
|
+
];
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
function toAnthropicAiUsage(args) {
|
|
46
|
+
if (!args.usage) {
|
|
47
|
+
return EMPTY_AI_USAGE;
|
|
48
|
+
}
|
|
49
|
+
const cacheReadTokens = args.usage.cache_read_input_tokens ?? 0;
|
|
50
|
+
const cacheWriteTokens = args.usage.cache_creation_input_tokens ?? 0;
|
|
51
|
+
const inputTokens = args.usage.input_tokens + cacheReadTokens + cacheWriteTokens;
|
|
52
|
+
return {
|
|
53
|
+
inputTokenDetails: {
|
|
54
|
+
cacheReadTokens,
|
|
55
|
+
cacheWriteTokens,
|
|
56
|
+
uncachedTokens: args.usage.input_tokens
|
|
57
|
+
},
|
|
58
|
+
inputTokens,
|
|
59
|
+
outputTokenDetails: {
|
|
60
|
+
reasoningTokens: 0,
|
|
61
|
+
responseTokens: args.usage.output_tokens
|
|
62
|
+
},
|
|
63
|
+
outputTokens: args.usage.output_tokens,
|
|
64
|
+
totalTokens: inputTokens + args.usage.output_tokens
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
function getToolDescriptions(args) {
|
|
68
|
+
if (!args.tools) {
|
|
69
|
+
return [];
|
|
70
|
+
}
|
|
71
|
+
return args.tools.map((tool) => ({
|
|
72
|
+
description: "description" in tool && typeof tool.description === "string" ? tool.description : void 0,
|
|
73
|
+
name: tool.name
|
|
74
|
+
}));
|
|
75
|
+
}
|
|
76
|
+
function getReasoning(args) {
|
|
77
|
+
const thinking = args.content?.filter((block) => block.type === "thinking").map((block) => block.thinking).filter((text) => text.length > 0);
|
|
78
|
+
return stringify({ value: thinking && thinking.length > 0 ? thinking : void 0 });
|
|
79
|
+
}
|
|
80
|
+
function getResponseText(args) {
|
|
81
|
+
if (!args.content) {
|
|
82
|
+
return "";
|
|
83
|
+
}
|
|
84
|
+
return args.content.filter((block) => block.type === "text").map((block) => block.text).join("");
|
|
85
|
+
}
|
|
86
|
+
function getToolCalls(args) {
|
|
87
|
+
if (!args.content) {
|
|
88
|
+
return [];
|
|
89
|
+
}
|
|
90
|
+
return args.content.filter((block) => block.type === "tool_use" || block.type === "server_tool_use").map((block) => ({
|
|
91
|
+
args: stringify({ value: block.input }),
|
|
92
|
+
name: block.name
|
|
93
|
+
}));
|
|
94
|
+
}
|
|
95
|
+
export {
|
|
96
|
+
AnthropicEventFactory
|
|
97
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Carbon } from '../../../core/carbon.mjs';
|
|
2
|
+
import Anthropic from '@anthropic-ai/sdk';
|
|
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 createWrappedMessageCreate(args: {
|
|
11
|
+
client: Anthropic;
|
|
12
|
+
carbon: Carbon;
|
|
13
|
+
}): typeof args.client.messages.create;
|
|
14
|
+
|
|
15
|
+
export { createWrappedMessageCreate };
|
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
import { createErrorStatus, createOkStatus } from "../../../core/utils/build-events.mjs";
|
|
2
|
+
import { AnthropicEventFactory } from "../event-factory.mjs";
|
|
3
|
+
function createWrappedMessageCreate(args) {
|
|
4
|
+
const createMessage = args.client.messages.create.bind(args.client.messages);
|
|
5
|
+
return ((body, requestOptions) => {
|
|
6
|
+
const { carbon: carbonObject, ...anthropicBody } = body;
|
|
7
|
+
const factory = new AnthropicEventFactory({
|
|
8
|
+
carbonObject
|
|
9
|
+
});
|
|
10
|
+
const startTimeMs = Date.now();
|
|
11
|
+
let messagePromise;
|
|
12
|
+
try {
|
|
13
|
+
messagePromise = createMessage(anthropicBody, requestOptions);
|
|
14
|
+
} catch (error) {
|
|
15
|
+
args.carbon.captureEvents({
|
|
16
|
+
events: factory.createMessageEvents({
|
|
17
|
+
body: anthropicBody,
|
|
18
|
+
endTimeMs: Date.now(),
|
|
19
|
+
mode: anthropicBody.stream ? "stream" : void 0,
|
|
20
|
+
sourceFunction: "messages.create",
|
|
21
|
+
startTimeMs,
|
|
22
|
+
status: createErrorStatus({ error })
|
|
23
|
+
})
|
|
24
|
+
});
|
|
25
|
+
throw error;
|
|
26
|
+
}
|
|
27
|
+
if (anthropicBody.stream) {
|
|
28
|
+
attachMessageCreateStreamCapture({
|
|
29
|
+
body: anthropicBody,
|
|
30
|
+
carbon: args.carbon,
|
|
31
|
+
factory,
|
|
32
|
+
messagePromise,
|
|
33
|
+
startTimeMs
|
|
34
|
+
});
|
|
35
|
+
return messagePromise;
|
|
36
|
+
}
|
|
37
|
+
void Promise.resolve(messagePromise).then(
|
|
38
|
+
(message) => {
|
|
39
|
+
args.carbon.captureEvents({
|
|
40
|
+
events: factory.createMessageEvents({
|
|
41
|
+
body: anthropicBody,
|
|
42
|
+
endTimeMs: Date.now(),
|
|
43
|
+
message,
|
|
44
|
+
sourceFunction: "messages.create",
|
|
45
|
+
startTimeMs,
|
|
46
|
+
status: createOkStatus()
|
|
47
|
+
})
|
|
48
|
+
});
|
|
49
|
+
},
|
|
50
|
+
(error) => {
|
|
51
|
+
args.carbon.captureEvents({
|
|
52
|
+
events: factory.createMessageEvents({
|
|
53
|
+
body: anthropicBody,
|
|
54
|
+
endTimeMs: Date.now(),
|
|
55
|
+
sourceFunction: "messages.create",
|
|
56
|
+
startTimeMs,
|
|
57
|
+
status: createErrorStatus({ error })
|
|
58
|
+
})
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
);
|
|
62
|
+
return messagePromise;
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
function attachMessageCreateStreamCapture(args) {
|
|
66
|
+
void Promise.resolve(args.messagePromise).then(
|
|
67
|
+
(stream) => {
|
|
68
|
+
captureMessageCreateStream({
|
|
69
|
+
...args,
|
|
70
|
+
stream
|
|
71
|
+
});
|
|
72
|
+
},
|
|
73
|
+
(error) => {
|
|
74
|
+
args.carbon.captureEvents({
|
|
75
|
+
events: args.factory.createMessageEvents({
|
|
76
|
+
body: args.body,
|
|
77
|
+
endTimeMs: Date.now(),
|
|
78
|
+
mode: "stream",
|
|
79
|
+
sourceFunction: "messages.create",
|
|
80
|
+
startTimeMs: args.startTimeMs,
|
|
81
|
+
status: createErrorStatus({ error })
|
|
82
|
+
})
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
function captureMessageCreateStream(args) {
|
|
88
|
+
const streamWithIterator = args.stream;
|
|
89
|
+
const originalIterator = (streamWithIterator.iterator ?? args.stream[Symbol.asyncIterator]).bind(args.stream);
|
|
90
|
+
const accumulator = createMessageAccumulator({ body: args.body });
|
|
91
|
+
let captured = false;
|
|
92
|
+
const wrappedIterator = async function* () {
|
|
93
|
+
try {
|
|
94
|
+
for await (const event of {
|
|
95
|
+
[Symbol.asyncIterator]: originalIterator
|
|
96
|
+
}) {
|
|
97
|
+
accumulator.addEvent({ event });
|
|
98
|
+
yield event;
|
|
99
|
+
}
|
|
100
|
+
captured = true;
|
|
101
|
+
args.carbon.captureEvents({
|
|
102
|
+
events: args.factory.createMessageEvents({
|
|
103
|
+
body: args.body,
|
|
104
|
+
endTimeMs: Date.now(),
|
|
105
|
+
message: accumulator.toMessage(),
|
|
106
|
+
mode: "stream",
|
|
107
|
+
sourceFunction: "messages.create",
|
|
108
|
+
startTimeMs: args.startTimeMs,
|
|
109
|
+
status: createOkStatus()
|
|
110
|
+
})
|
|
111
|
+
});
|
|
112
|
+
} catch (error) {
|
|
113
|
+
if (!captured) {
|
|
114
|
+
captured = true;
|
|
115
|
+
args.carbon.captureEvents({
|
|
116
|
+
events: args.factory.createMessageEvents({
|
|
117
|
+
body: args.body,
|
|
118
|
+
endTimeMs: Date.now(),
|
|
119
|
+
mode: "stream",
|
|
120
|
+
sourceFunction: "messages.create",
|
|
121
|
+
startTimeMs: args.startTimeMs,
|
|
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
|
+
function createMessageAccumulator(args) {
|
|
136
|
+
let message;
|
|
137
|
+
const toolInputByIndex = /* @__PURE__ */ new Map();
|
|
138
|
+
return {
|
|
139
|
+
addEvent(addArgs) {
|
|
140
|
+
switch (addArgs.event.type) {
|
|
141
|
+
case "message_start":
|
|
142
|
+
message = {
|
|
143
|
+
...addArgs.event.message,
|
|
144
|
+
content: [...addArgs.event.message.content],
|
|
145
|
+
usage: {
|
|
146
|
+
...addArgs.event.message.usage
|
|
147
|
+
}
|
|
148
|
+
};
|
|
149
|
+
return;
|
|
150
|
+
case "content_block_start":
|
|
151
|
+
message?.content.push({ ...addArgs.event.content_block });
|
|
152
|
+
return;
|
|
153
|
+
case "content_block_delta":
|
|
154
|
+
applyContentDelta({
|
|
155
|
+
content: message?.content,
|
|
156
|
+
event: addArgs.event,
|
|
157
|
+
toolInputByIndex
|
|
158
|
+
});
|
|
159
|
+
return;
|
|
160
|
+
case "message_delta":
|
|
161
|
+
if (message) {
|
|
162
|
+
message = {
|
|
163
|
+
...message,
|
|
164
|
+
stop_reason: addArgs.event.delta.stop_reason,
|
|
165
|
+
stop_sequence: addArgs.event.delta.stop_sequence,
|
|
166
|
+
usage: {
|
|
167
|
+
...message.usage,
|
|
168
|
+
cache_creation_input_tokens: addArgs.event.usage.cache_creation_input_tokens ?? message.usage.cache_creation_input_tokens,
|
|
169
|
+
cache_read_input_tokens: addArgs.event.usage.cache_read_input_tokens ?? message.usage.cache_read_input_tokens,
|
|
170
|
+
input_tokens: addArgs.event.usage.input_tokens ?? message.usage.input_tokens,
|
|
171
|
+
output_tokens: addArgs.event.usage.output_tokens,
|
|
172
|
+
server_tool_use: addArgs.event.usage.server_tool_use ?? message.usage.server_tool_use
|
|
173
|
+
}
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
return;
|
|
177
|
+
case "content_block_stop":
|
|
178
|
+
case "message_stop":
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
},
|
|
182
|
+
toMessage() {
|
|
183
|
+
return message ?? {
|
|
184
|
+
container: null,
|
|
185
|
+
content: [],
|
|
186
|
+
id: "",
|
|
187
|
+
model: String(args.body.model ?? "unknown"),
|
|
188
|
+
role: "assistant",
|
|
189
|
+
stop_details: null,
|
|
190
|
+
stop_reason: null,
|
|
191
|
+
stop_sequence: null,
|
|
192
|
+
type: "message",
|
|
193
|
+
usage: {
|
|
194
|
+
cache_creation: null,
|
|
195
|
+
cache_creation_input_tokens: 0,
|
|
196
|
+
cache_read_input_tokens: 0,
|
|
197
|
+
inference_geo: null,
|
|
198
|
+
input_tokens: 0,
|
|
199
|
+
output_tokens: 0,
|
|
200
|
+
server_tool_use: null,
|
|
201
|
+
service_tier: null
|
|
202
|
+
}
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
function applyContentDelta(args) {
|
|
208
|
+
const block = args.content?.[args.event.index];
|
|
209
|
+
if (!block) {
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
switch (args.event.delta.type) {
|
|
213
|
+
case "text_delta":
|
|
214
|
+
if (block.type === "text") {
|
|
215
|
+
block.text = (block.text ?? "") + args.event.delta.text;
|
|
216
|
+
}
|
|
217
|
+
return;
|
|
218
|
+
case "thinking_delta":
|
|
219
|
+
if (block.type === "thinking") {
|
|
220
|
+
block.thinking = (block.thinking ?? "") + args.event.delta.thinking;
|
|
221
|
+
}
|
|
222
|
+
return;
|
|
223
|
+
case "signature_delta":
|
|
224
|
+
if (block.type === "thinking") {
|
|
225
|
+
block.signature = args.event.delta.signature;
|
|
226
|
+
}
|
|
227
|
+
return;
|
|
228
|
+
case "input_json_delta":
|
|
229
|
+
if (block.type === "tool_use" || block.type === "server_tool_use") {
|
|
230
|
+
const inputJson = (args.toolInputByIndex.get(args.event.index) ?? "") + args.event.delta.partial_json;
|
|
231
|
+
args.toolInputByIndex.set(args.event.index, inputJson);
|
|
232
|
+
try {
|
|
233
|
+
block.input = JSON.parse(inputJson);
|
|
234
|
+
} catch {
|
|
235
|
+
block.input = inputJson;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
return;
|
|
239
|
+
case "citations_delta":
|
|
240
|
+
if (block.type === "text") {
|
|
241
|
+
block.citations = [...block.citations ?? [], args.event.delta.citation];
|
|
242
|
+
}
|
|
243
|
+
return;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
export {
|
|
247
|
+
createWrappedMessageCreate
|
|
248
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Carbon } from '../../../core/carbon.mjs';
|
|
2
|
+
import Anthropic from '@anthropic-ai/sdk';
|
|
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 createWrappedMessageStream(args: {
|
|
11
|
+
client: Anthropic;
|
|
12
|
+
carbon: Carbon;
|
|
13
|
+
}): typeof args.client.messages.stream;
|
|
14
|
+
|
|
15
|
+
export { createWrappedMessageStream };
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { createErrorStatus, createOkStatus } from "../../../core/utils/build-events.mjs";
|
|
2
|
+
import { AnthropicEventFactory } from "../event-factory.mjs";
|
|
3
|
+
function createWrappedMessageStream(args) {
|
|
4
|
+
const streamMessage = args.client.messages.stream.bind(args.client.messages);
|
|
5
|
+
return ((body, requestOptions) => {
|
|
6
|
+
const { carbon: carbonObject, ...anthropicBody } = body;
|
|
7
|
+
const factory = new AnthropicEventFactory({
|
|
8
|
+
carbonObject
|
|
9
|
+
});
|
|
10
|
+
const startTimeMs = Date.now();
|
|
11
|
+
let messageStream;
|
|
12
|
+
try {
|
|
13
|
+
messageStream = streamMessage(anthropicBody, requestOptions);
|
|
14
|
+
} catch (error) {
|
|
15
|
+
args.carbon.captureEvents({
|
|
16
|
+
events: factory.createMessageEvents({
|
|
17
|
+
body: anthropicBody,
|
|
18
|
+
endTimeMs: Date.now(),
|
|
19
|
+
mode: "stream",
|
|
20
|
+
sourceFunction: "messages.stream",
|
|
21
|
+
startTimeMs,
|
|
22
|
+
status: createErrorStatus({ error })
|
|
23
|
+
})
|
|
24
|
+
});
|
|
25
|
+
throw error;
|
|
26
|
+
}
|
|
27
|
+
void messageStream.finalMessage().then(
|
|
28
|
+
(message) => {
|
|
29
|
+
args.carbon.captureEvents({
|
|
30
|
+
events: factory.createMessageEvents({
|
|
31
|
+
body: anthropicBody,
|
|
32
|
+
endTimeMs: Date.now(),
|
|
33
|
+
message,
|
|
34
|
+
mode: "stream",
|
|
35
|
+
sourceFunction: "messages.stream",
|
|
36
|
+
startTimeMs,
|
|
37
|
+
status: createOkStatus()
|
|
38
|
+
})
|
|
39
|
+
});
|
|
40
|
+
},
|
|
41
|
+
(error) => {
|
|
42
|
+
args.carbon.captureEvents({
|
|
43
|
+
events: factory.createMessageEvents({
|
|
44
|
+
body: anthropicBody,
|
|
45
|
+
endTimeMs: Date.now(),
|
|
46
|
+
mode: "stream",
|
|
47
|
+
sourceFunction: "messages.stream",
|
|
48
|
+
startTimeMs,
|
|
49
|
+
status: createErrorStatus({ error })
|
|
50
|
+
})
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
);
|
|
54
|
+
return messageStream;
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
export {
|
|
58
|
+
createWrappedMessageStream
|
|
59
|
+
};
|
|
File without changes
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Carbon } from '../../core/carbon.mjs';
|
|
2
|
+
import { T_WrappedAnthropicSdk } from './types.mjs';
|
|
3
|
+
import Anthropic from '@anthropic-ai/sdk';
|
|
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 wrapAnthropicSdk: (client: Anthropic, carbon: Carbon) => T_WrappedAnthropicSdk;
|
|
12
|
+
|
|
13
|
+
export { wrapAnthropicSdk };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { createWrappedMessageCreate } from "./fns/message-create.mjs";
|
|
2
|
+
import { createWrappedMessageStream } from "./fns/message-stream.mjs";
|
|
3
|
+
const wrapAnthropicSdk = (client, carbon) => {
|
|
4
|
+
const wrappedClient = client;
|
|
5
|
+
wrappedClient.messages.create = createWrappedMessageCreate({
|
|
6
|
+
client,
|
|
7
|
+
carbon
|
|
8
|
+
});
|
|
9
|
+
wrappedClient.messages.stream = createWrappedMessageStream({
|
|
10
|
+
client,
|
|
11
|
+
carbon
|
|
12
|
+
});
|
|
13
|
+
return wrappedClient;
|
|
14
|
+
};
|
|
15
|
+
export {
|
|
16
|
+
wrapAnthropicSdk
|
|
17
|
+
};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { T_CarbonObject } from '../../core/schema/carbon-object.mjs';
|
|
2
|
+
import { T as T_LLMEvent, a as T_Event } from '../../internal/schema/events.mjs';
|
|
3
|
+
import { ChatCompletionCreateParamsBase, ChatCompletion } from 'openai/resources/chat/completions/completions';
|
|
4
|
+
import { ResponseCreateParamsBase, Response } from 'openai/resources/responses/responses';
|
|
5
|
+
import 'zod';
|
|
6
|
+
|
|
7
|
+
declare class OpenAIEventFactory {
|
|
8
|
+
private readonly carbonObject?;
|
|
9
|
+
constructor(args: {
|
|
10
|
+
carbonObject?: T_CarbonObject;
|
|
11
|
+
});
|
|
12
|
+
createResponseEvents(args: {
|
|
13
|
+
body: Partial<ResponseCreateParamsBase>;
|
|
14
|
+
endTimeMs: number;
|
|
15
|
+
mode?: T_LLMEvent["properties"]["llm"]["output"]["mode"];
|
|
16
|
+
response?: Response;
|
|
17
|
+
sourceFunction?: string;
|
|
18
|
+
startTimeMs: number;
|
|
19
|
+
status: T_LLMEvent["status"];
|
|
20
|
+
}): T_Event[];
|
|
21
|
+
createChatCompletionEvents(args: {
|
|
22
|
+
body: ChatCompletionCreateParamsBase;
|
|
23
|
+
completion?: ChatCompletion;
|
|
24
|
+
endTimeMs: number;
|
|
25
|
+
mode?: T_LLMEvent["properties"]["llm"]["output"]["mode"];
|
|
26
|
+
sourceFunction?: string;
|
|
27
|
+
startTimeMs: number;
|
|
28
|
+
status: T_LLMEvent["status"];
|
|
29
|
+
}): T_Event[];
|
|
30
|
+
private createResponseLlmInput;
|
|
31
|
+
private createChatCompletionLlmInput;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export { OpenAIEventFactory };
|