@morsehq-dev/sdk 0.4.0-rc.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/LICENSE +21 -0
- package/README.md +294 -0
- package/dist/anthropic/index.cjs +39 -0
- package/dist/anthropic/index.cjs.map +1 -0
- package/dist/anthropic/index.d.cts +213 -0
- package/dist/anthropic/index.d.ts +213 -0
- package/dist/anthropic/index.js +6 -0
- package/dist/anthropic/index.js.map +1 -0
- package/dist/anthropic-agent-sdk/index.cjs +744 -0
- package/dist/anthropic-agent-sdk/index.cjs.map +1 -0
- package/dist/anthropic-agent-sdk/index.d.cts +371 -0
- package/dist/anthropic-agent-sdk/index.d.ts +371 -0
- package/dist/anthropic-agent-sdk/index.js +735 -0
- package/dist/anthropic-agent-sdk/index.js.map +1 -0
- package/dist/browser/anthropic/index.cjs +39 -0
- package/dist/browser/anthropic/index.cjs.map +1 -0
- package/dist/browser/anthropic/index.js +6 -0
- package/dist/browser/anthropic/index.js.map +1 -0
- package/dist/browser/anthropic-agent-sdk/index.cjs +744 -0
- package/dist/browser/anthropic-agent-sdk/index.cjs.map +1 -0
- package/dist/browser/anthropic-agent-sdk/index.js +735 -0
- package/dist/browser/anthropic-agent-sdk/index.js.map +1 -0
- package/dist/browser/chunk-3643DC7K.cjs +365 -0
- package/dist/browser/chunk-3643DC7K.cjs.map +1 -0
- package/dist/browser/chunk-4FALQMOZ.js +232 -0
- package/dist/browser/chunk-4FALQMOZ.js.map +1 -0
- package/dist/browser/chunk-5J2QBK75.js +884 -0
- package/dist/browser/chunk-5J2QBK75.js.map +1 -0
- package/dist/browser/chunk-7MSXWGAH.js +53 -0
- package/dist/browser/chunk-7MSXWGAH.js.map +1 -0
- package/dist/browser/chunk-A24L5N5N.js +596 -0
- package/dist/browser/chunk-A24L5N5N.js.map +1 -0
- package/dist/browser/chunk-B7DEUDP6.cjs +177 -0
- package/dist/browser/chunk-B7DEUDP6.cjs.map +1 -0
- package/dist/browser/chunk-F6CJACNH.cjs +604 -0
- package/dist/browser/chunk-F6CJACNH.cjs.map +1 -0
- package/dist/browser/chunk-JBOYFQSB.js +412 -0
- package/dist/browser/chunk-JBOYFQSB.js.map +1 -0
- package/dist/browser/chunk-KRBADE6R.cjs +183 -0
- package/dist/browser/chunk-KRBADE6R.cjs.map +1 -0
- package/dist/browser/chunk-MCJYZH6W.cjs +415 -0
- package/dist/browser/chunk-MCJYZH6W.cjs.map +1 -0
- package/dist/browser/chunk-NQG2IAQS.js +178 -0
- package/dist/browser/chunk-NQG2IAQS.js.map +1 -0
- package/dist/browser/chunk-O4HG3OSK.cjs +929 -0
- package/dist/browser/chunk-O4HG3OSK.cjs.map +1 -0
- package/dist/browser/chunk-QWRQJO57.js +363 -0
- package/dist/browser/chunk-QWRQJO57.js.map +1 -0
- package/dist/browser/chunk-SWQOPFE4.cjs +234 -0
- package/dist/browser/chunk-SWQOPFE4.cjs.map +1 -0
- package/dist/browser/chunk-TYDG747E.js +171 -0
- package/dist/browser/chunk-TYDG747E.js.map +1 -0
- package/dist/browser/chunk-TZRSDDFP.cjs +56 -0
- package/dist/browser/chunk-TZRSDDFP.cjs.map +1 -0
- package/dist/browser/index.cjs +587 -0
- package/dist/browser/index.cjs.map +1 -0
- package/dist/browser/index.js +522 -0
- package/dist/browser/index.js.map +1 -0
- package/dist/browser/integrations/pino.cjs +89 -0
- package/dist/browser/integrations/pino.cjs.map +1 -0
- package/dist/browser/integrations/pino.js +86 -0
- package/dist/browser/integrations/pino.js.map +1 -0
- package/dist/browser/langchain/index.cjs +535 -0
- package/dist/browser/langchain/index.cjs.map +1 -0
- package/dist/browser/langchain/index.js +528 -0
- package/dist/browser/langchain/index.js.map +1 -0
- package/dist/browser/langgraph/index.cjs +377 -0
- package/dist/browser/langgraph/index.cjs.map +1 -0
- package/dist/browser/langgraph/index.js +371 -0
- package/dist/browser/langgraph/index.js.map +1 -0
- package/dist/browser/openai/index.cjs +125 -0
- package/dist/browser/openai/index.cjs.map +1 -0
- package/dist/browser/openai/index.js +122 -0
- package/dist/browser/openai/index.js.map +1 -0
- package/dist/browser/openai-agents/index.cjs +689 -0
- package/dist/browser/openai-agents/index.cjs.map +1 -0
- package/dist/browser/openai-agents/index.js +678 -0
- package/dist/browser/openai-agents/index.js.map +1 -0
- package/dist/browser/vercel-ai/index.cjs +233 -0
- package/dist/browser/vercel-ai/index.cjs.map +1 -0
- package/dist/browser/vercel-ai/index.js +231 -0
- package/dist/browser/vercel-ai/index.js.map +1 -0
- package/dist/chunk-4R4SHGOK.js +363 -0
- package/dist/chunk-4R4SHGOK.js.map +1 -0
- package/dist/chunk-7EO7MQBA.cjs +183 -0
- package/dist/chunk-7EO7MQBA.cjs.map +1 -0
- package/dist/chunk-7YCENA54.cjs +604 -0
- package/dist/chunk-7YCENA54.cjs.map +1 -0
- package/dist/chunk-CKFOGDUF.js +596 -0
- package/dist/chunk-CKFOGDUF.js.map +1 -0
- package/dist/chunk-FJUNILZT.cjs +1324 -0
- package/dist/chunk-FJUNILZT.cjs.map +1 -0
- package/dist/chunk-HDAFUKQ3.js +171 -0
- package/dist/chunk-HDAFUKQ3.js.map +1 -0
- package/dist/chunk-KBWPNIH4.cjs +234 -0
- package/dist/chunk-KBWPNIH4.cjs.map +1 -0
- package/dist/chunk-KJEO52QS.cjs +365 -0
- package/dist/chunk-KJEO52QS.cjs.map +1 -0
- package/dist/chunk-KZBCOZIQ.cjs +177 -0
- package/dist/chunk-KZBCOZIQ.cjs.map +1 -0
- package/dist/chunk-ME5JALGT.js +53 -0
- package/dist/chunk-ME5JALGT.js.map +1 -0
- package/dist/chunk-PVHDEPRE.cjs +56 -0
- package/dist/chunk-PVHDEPRE.cjs.map +1 -0
- package/dist/chunk-RTL23YOQ.js +178 -0
- package/dist/chunk-RTL23YOQ.js.map +1 -0
- package/dist/chunk-TQWI4UYO.js +1277 -0
- package/dist/chunk-TQWI4UYO.js.map +1 -0
- package/dist/chunk-VXDBDPDR.cjs +415 -0
- package/dist/chunk-VXDBDPDR.cjs.map +1 -0
- package/dist/chunk-XTKMUJWI.js +232 -0
- package/dist/chunk-XTKMUJWI.js.map +1 -0
- package/dist/chunk-ZKUGOWER.js +412 -0
- package/dist/chunk-ZKUGOWER.js.map +1 -0
- package/dist/index.cjs +843 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +464 -0
- package/dist/index.d.ts +464 -0
- package/dist/index.js +778 -0
- package/dist/index.js.map +1 -0
- package/dist/integrations/pino.cjs +89 -0
- package/dist/integrations/pino.cjs.map +1 -0
- package/dist/integrations/pino.d.cts +65 -0
- package/dist/integrations/pino.d.ts +65 -0
- package/dist/integrations/pino.js +86 -0
- package/dist/integrations/pino.js.map +1 -0
- package/dist/langchain/index.cjs +535 -0
- package/dist/langchain/index.cjs.map +1 -0
- package/dist/langchain/index.d.cts +265 -0
- package/dist/langchain/index.d.ts +265 -0
- package/dist/langchain/index.js +528 -0
- package/dist/langchain/index.js.map +1 -0
- package/dist/langgraph/index.cjs +377 -0
- package/dist/langgraph/index.cjs.map +1 -0
- package/dist/langgraph/index.d.cts +324 -0
- package/dist/langgraph/index.d.ts +324 -0
- package/dist/langgraph/index.js +371 -0
- package/dist/langgraph/index.js.map +1 -0
- package/dist/openai/index.cjs +125 -0
- package/dist/openai/index.cjs.map +1 -0
- package/dist/openai/index.d.cts +136 -0
- package/dist/openai/index.d.ts +136 -0
- package/dist/openai/index.js +122 -0
- package/dist/openai/index.js.map +1 -0
- package/dist/openai-agents/index.cjs +689 -0
- package/dist/openai-agents/index.cjs.map +1 -0
- package/dist/openai-agents/index.d.cts +502 -0
- package/dist/openai-agents/index.d.ts +502 -0
- package/dist/openai-agents/index.js +678 -0
- package/dist/openai-agents/index.js.map +1 -0
- package/dist/spans-DZtMuBvc.d.cts +73 -0
- package/dist/spans-DZtMuBvc.d.ts +73 -0
- package/dist/tracing-BYAqjT5Q.d.cts +114 -0
- package/dist/tracing-rz9cWQ8d.d.ts +114 -0
- package/dist/vercel-ai/index.cjs +233 -0
- package/dist/vercel-ai/index.cjs.map +1 -0
- package/dist/vercel-ai/index.d.cts +93 -0
- package/dist/vercel-ai/index.d.ts +93 -0
- package/dist/vercel-ai/index.js +231 -0
- package/dist/vercel-ai/index.js.map +1 -0
- package/package.json +182 -0
|
@@ -0,0 +1,596 @@
|
|
|
1
|
+
import { calculateCost } from './chunk-4FALQMOZ.js';
|
|
2
|
+
import { absorbErrorsAsync, absorbErrorsSync } from './chunk-NQG2IAQS.js';
|
|
3
|
+
import { init_spans, init_tracing, getActiveTraceState, runAsync, startSpan, nowIso, generateSpanId } from './chunk-5J2QBK75.js';
|
|
4
|
+
|
|
5
|
+
// src/anthropic/index.ts
|
|
6
|
+
init_spans();
|
|
7
|
+
init_tracing();
|
|
8
|
+
var WRAPPED_CLIENT = /* @__PURE__ */ Symbol.for("morse.anthropic.wrapped");
|
|
9
|
+
var INSTALL_SENTINEL = /* @__PURE__ */ Symbol.for("morse.anthropic.installed");
|
|
10
|
+
var ORIGINALS_SENTINEL = /* @__PURE__ */ Symbol.for("morse.anthropic.originals");
|
|
11
|
+
var globalInstallActive = false;
|
|
12
|
+
function wrapAnthropic(client, options = {}) {
|
|
13
|
+
if (!client || typeof client !== "object") return client;
|
|
14
|
+
if (client[WRAPPED_CLIENT]) return client;
|
|
15
|
+
if (globalInstallActive) return client;
|
|
16
|
+
const originalCreate = client.messages?.create?.bind(client.messages);
|
|
17
|
+
if (typeof originalCreate !== "function") return client;
|
|
18
|
+
const patchedCreate = buildPatchedCreate(originalCreate, options);
|
|
19
|
+
const patchedMessages = new Proxy(client.messages, {
|
|
20
|
+
get(target, prop, receiver) {
|
|
21
|
+
if (prop === "create") return patchedCreate;
|
|
22
|
+
const value = Reflect.get(target, prop, receiver);
|
|
23
|
+
if (typeof value === "function") return value.bind(target);
|
|
24
|
+
return value;
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
const wrapper = new Proxy(client, {
|
|
28
|
+
get(target, prop, receiver) {
|
|
29
|
+
if (prop === WRAPPED_CLIENT) return true;
|
|
30
|
+
if (prop === "messages") return patchedMessages;
|
|
31
|
+
const value = Reflect.get(target, prop, receiver);
|
|
32
|
+
if (typeof value === "function") return value.bind(target);
|
|
33
|
+
return value;
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
return wrapper;
|
|
37
|
+
}
|
|
38
|
+
var instrumentAnthropic = absorbErrorsAsync(
|
|
39
|
+
async (clientOrOptions, maybeOptions) => {
|
|
40
|
+
if (clientOrOptions && isClient(clientOrOptions)) {
|
|
41
|
+
const opts2 = maybeOptions ?? {};
|
|
42
|
+
return wrapAnthropic(clientOrOptions, opts2);
|
|
43
|
+
}
|
|
44
|
+
const opts = clientOrOptions ?? {};
|
|
45
|
+
const mod = await loadAnthropicModule(opts.anthropicModule);
|
|
46
|
+
if (!mod) return false;
|
|
47
|
+
const installed = mod[INSTALL_SENTINEL] === true;
|
|
48
|
+
if (installed) {
|
|
49
|
+
globalInstallActive = true;
|
|
50
|
+
return true;
|
|
51
|
+
}
|
|
52
|
+
const carrier = findMessagesPrototypeCarrier(mod);
|
|
53
|
+
if (!carrier) return false;
|
|
54
|
+
const originalCreate = carrier.prototype.create;
|
|
55
|
+
if (typeof originalCreate !== "function") return false;
|
|
56
|
+
const patchedCreate = buildPatchedCreate(function(params, callOptions) {
|
|
57
|
+
return originalCreate.call(
|
|
58
|
+
this,
|
|
59
|
+
params,
|
|
60
|
+
callOptions
|
|
61
|
+
);
|
|
62
|
+
}, opts);
|
|
63
|
+
carrier.prototype.create = patchedCreate;
|
|
64
|
+
mod[ORIGINALS_SENTINEL] = {
|
|
65
|
+
protoCreate: originalCreate,
|
|
66
|
+
protoCarrier: carrier
|
|
67
|
+
};
|
|
68
|
+
mod[INSTALL_SENTINEL] = true;
|
|
69
|
+
globalInstallActive = true;
|
|
70
|
+
return true;
|
|
71
|
+
},
|
|
72
|
+
{ fingerprint: "morse.adapters.anthropic.install" }
|
|
73
|
+
);
|
|
74
|
+
var uninstallAnthropic = absorbErrorsSync(
|
|
75
|
+
(anthropicModule) => {
|
|
76
|
+
if (!anthropicModule || typeof anthropicModule !== "object") return;
|
|
77
|
+
const mod = anthropicModule;
|
|
78
|
+
if (!mod[INSTALL_SENTINEL]) return;
|
|
79
|
+
const originals = mod[ORIGINALS_SENTINEL];
|
|
80
|
+
if (originals?.protoCarrier && originals.protoCreate !== void 0) {
|
|
81
|
+
originals.protoCarrier.prototype.create = originals.protoCreate;
|
|
82
|
+
}
|
|
83
|
+
mod[ORIGINALS_SENTINEL] = void 0;
|
|
84
|
+
mod[INSTALL_SENTINEL] = false;
|
|
85
|
+
globalInstallActive = false;
|
|
86
|
+
},
|
|
87
|
+
{ fingerprint: "morse.adapters.anthropic.uninstall" }
|
|
88
|
+
);
|
|
89
|
+
function _resetAnthropicInstallStateForTesting() {
|
|
90
|
+
globalInstallActive = false;
|
|
91
|
+
}
|
|
92
|
+
function isAnthropicWrapped(client) {
|
|
93
|
+
if (!client || typeof client !== "object") return false;
|
|
94
|
+
return client[WRAPPED_CLIENT] === true;
|
|
95
|
+
}
|
|
96
|
+
function isClient(x) {
|
|
97
|
+
if (!x || typeof x !== "object") return false;
|
|
98
|
+
const messages = x.messages;
|
|
99
|
+
return !!messages && typeof messages.create === "function";
|
|
100
|
+
}
|
|
101
|
+
async function loadAnthropicModule(explicit) {
|
|
102
|
+
if (explicit && typeof explicit === "object") return explicit;
|
|
103
|
+
try {
|
|
104
|
+
return await Function("m", "return import(m)")("@anthropic-ai/sdk");
|
|
105
|
+
} catch {
|
|
106
|
+
return null;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
function findMessagesPrototypeCarrier(mod) {
|
|
110
|
+
if (!mod || typeof mod !== "object") return null;
|
|
111
|
+
const m = mod;
|
|
112
|
+
const Default = m.default ?? m.Anthropic ?? m;
|
|
113
|
+
const candidates = [
|
|
114
|
+
Default?.Messages,
|
|
115
|
+
m.Messages,
|
|
116
|
+
Default?.resources?.Messages
|
|
117
|
+
];
|
|
118
|
+
for (const c of candidates) {
|
|
119
|
+
if (c && typeof c === "function" && c.prototype && typeof c.prototype.create === "function") {
|
|
120
|
+
return c;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
return null;
|
|
124
|
+
}
|
|
125
|
+
function buildPatchedCreate(original, options) {
|
|
126
|
+
const agentName = options.defaultAgentName ?? "anthropic.messages.create";
|
|
127
|
+
const summarizeMode = options.summarize === true;
|
|
128
|
+
return async function patchedCreate(params, callOptions) {
|
|
129
|
+
const inTrace = getActiveTraceState() !== null;
|
|
130
|
+
const work = async () => {
|
|
131
|
+
const handle = startSpan({
|
|
132
|
+
name: "anthropic.messages.create",
|
|
133
|
+
type: "llm",
|
|
134
|
+
metadata: { model: params?.model, max_tokens: params?.max_tokens }
|
|
135
|
+
});
|
|
136
|
+
let response;
|
|
137
|
+
try {
|
|
138
|
+
response = await original.call(this, params, callOptions);
|
|
139
|
+
} catch (err) {
|
|
140
|
+
handle?.end(err);
|
|
141
|
+
throw err;
|
|
142
|
+
}
|
|
143
|
+
try {
|
|
144
|
+
if (handle) {
|
|
145
|
+
const segments = safeExtractSegments(params, response);
|
|
146
|
+
const usage = response?.usage ?? {};
|
|
147
|
+
const inputTokens = (usage.input_tokens ?? 0) + (usage.cache_creation_input_tokens ?? 0) + (usage.cache_read_input_tokens ?? 0);
|
|
148
|
+
const outputTokens = usage.output_tokens ?? 0;
|
|
149
|
+
handle.span.model = response?.model ?? params?.model;
|
|
150
|
+
if (inputTokens > 0) handle.span.input_tokens = inputTokens;
|
|
151
|
+
if (outputTokens > 0) handle.span.output_tokens = outputTokens;
|
|
152
|
+
const cacheWrite = usage.cache_creation_input_tokens ?? 0;
|
|
153
|
+
const cacheRead = usage.cache_read_input_tokens ?? 0;
|
|
154
|
+
if (cacheWrite > 0) handle.span.cache_write_tokens = cacheWrite;
|
|
155
|
+
if (cacheRead > 0) handle.span.cached_input_tokens = cacheRead;
|
|
156
|
+
if (inputTokens > 0 || outputTokens > 0) {
|
|
157
|
+
handle.span.total_tokens = inputTokens + outputTokens;
|
|
158
|
+
}
|
|
159
|
+
if (handle.span.cost_usd === void 0 && handle.span.model && (inputTokens > 0 || outputTokens > 0)) {
|
|
160
|
+
const cost = calculateCost({
|
|
161
|
+
inputTokens,
|
|
162
|
+
outputTokens,
|
|
163
|
+
model: handle.span.model,
|
|
164
|
+
cachedInputTokens: cacheRead,
|
|
165
|
+
cacheWriteTokens: cacheWrite
|
|
166
|
+
});
|
|
167
|
+
if (cost !== void 0) handle.span.cost_usd = cost;
|
|
168
|
+
}
|
|
169
|
+
handle.span.context_segments = segments;
|
|
170
|
+
}
|
|
171
|
+
} catch {
|
|
172
|
+
}
|
|
173
|
+
handle?.end();
|
|
174
|
+
if (summarizeMode && handle) {
|
|
175
|
+
try {
|
|
176
|
+
emitSummarizeMemorySpan(handle.span, response);
|
|
177
|
+
} catch {
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
return response;
|
|
181
|
+
};
|
|
182
|
+
if (inTrace) {
|
|
183
|
+
return work();
|
|
184
|
+
}
|
|
185
|
+
let result;
|
|
186
|
+
let thrown;
|
|
187
|
+
await runAsync({ agentName }, async (runHandle) => {
|
|
188
|
+
runHandle.setOutcome(true);
|
|
189
|
+
try {
|
|
190
|
+
result = await work();
|
|
191
|
+
} catch (err) {
|
|
192
|
+
thrown = err;
|
|
193
|
+
throw err;
|
|
194
|
+
}
|
|
195
|
+
return null;
|
|
196
|
+
});
|
|
197
|
+
if (thrown !== void 0) throw thrown;
|
|
198
|
+
return result;
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
var MAX_SEGMENT_CONTENT_BYTES = 4096;
|
|
202
|
+
function extractAnthropicSegments(params, response) {
|
|
203
|
+
const segments = [];
|
|
204
|
+
let position = 0;
|
|
205
|
+
if (Array.isArray(params.tools) && params.tools.length > 0) {
|
|
206
|
+
const content = canonicalJsonStringify(params.tools);
|
|
207
|
+
segments.push(makeSegment(position++, "tool_descriptions", content));
|
|
208
|
+
}
|
|
209
|
+
let systemHadEphemeral = false;
|
|
210
|
+
if (params.system !== void 0 && params.system !== null) {
|
|
211
|
+
let text = "";
|
|
212
|
+
if (typeof params.system === "string") {
|
|
213
|
+
text = params.system;
|
|
214
|
+
} else if (Array.isArray(params.system)) {
|
|
215
|
+
const parts = [];
|
|
216
|
+
for (const block of params.system) {
|
|
217
|
+
if (block && typeof block === "object" && block.type === "text") {
|
|
218
|
+
parts.push(typeof block.text === "string" ? block.text : "");
|
|
219
|
+
if (block.cache_control && typeof block.cache_control === "object" && block.cache_control.type === "ephemeral") {
|
|
220
|
+
systemHadEphemeral = true;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
text = parts.join("\n");
|
|
225
|
+
}
|
|
226
|
+
const seg = makeSegment(position++, "system_prompt", text);
|
|
227
|
+
if (systemHadEphemeral) seg.cache_state = "cache_write";
|
|
228
|
+
segments.push(seg);
|
|
229
|
+
}
|
|
230
|
+
const messages = Array.isArray(params.messages) ? params.messages : [];
|
|
231
|
+
let lastUserIdx = -1;
|
|
232
|
+
for (let i = messages.length - 1; i >= 0; i--) {
|
|
233
|
+
if (messages[i]?.role === "user") {
|
|
234
|
+
lastUserIdx = i;
|
|
235
|
+
break;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
for (let i = 0; i < messages.length; i++) {
|
|
239
|
+
const msg = messages[i];
|
|
240
|
+
if (!msg) continue;
|
|
241
|
+
const blocks = normalizeContent(msg.content);
|
|
242
|
+
const toolResults = blocks.filter(
|
|
243
|
+
(b) => b.type === "tool_result"
|
|
244
|
+
);
|
|
245
|
+
const nonToolResults = blocks.filter((b) => b.type !== "tool_result");
|
|
246
|
+
if (nonToolResults.length > 0) {
|
|
247
|
+
const text = renderBlocksAsText(nonToolResults, msg.role);
|
|
248
|
+
let source;
|
|
249
|
+
if (msg.role === "user" && i === lastUserIdx) {
|
|
250
|
+
source = "current_input";
|
|
251
|
+
} else {
|
|
252
|
+
source = "chat_history";
|
|
253
|
+
}
|
|
254
|
+
segments.push(makeSegment(position++, source, text));
|
|
255
|
+
} else if (typeof msg.content === "string" && msg.role === "user" && i === lastUserIdx && msg.content.length > 0) {
|
|
256
|
+
segments.push(makeSegment(position++, "current_input", msg.content));
|
|
257
|
+
}
|
|
258
|
+
for (const tr of toolResults) {
|
|
259
|
+
const text = renderToolResult(tr);
|
|
260
|
+
const seg = makeSegment(position++, "tool_observation", text);
|
|
261
|
+
seg.attrs = { tool_use_id: tr.tool_use_id };
|
|
262
|
+
if (tr.is_error) {
|
|
263
|
+
seg.attrs.is_error = true;
|
|
264
|
+
}
|
|
265
|
+
segments.push(seg);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
const cacheRead = response?.usage?.cache_read_input_tokens ?? 0;
|
|
269
|
+
if (cacheRead > 0) {
|
|
270
|
+
for (const seg of segments) {
|
|
271
|
+
if (seg.source === "system_prompt" || seg.source === "tool_descriptions") {
|
|
272
|
+
seg.cache_state = "cache_hit";
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
return segments;
|
|
277
|
+
}
|
|
278
|
+
function safeExtractSegments(params, response) {
|
|
279
|
+
try {
|
|
280
|
+
return extractAnthropicSegments(params, response);
|
|
281
|
+
} catch {
|
|
282
|
+
return [];
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
function emitSummarizeMemorySpan(llmSpan, response) {
|
|
286
|
+
const state = getActiveTraceState();
|
|
287
|
+
if (state === null) return;
|
|
288
|
+
const summaryText = extractFirstTextBlock(response);
|
|
289
|
+
const summaryHash = sha256Hex(summaryText);
|
|
290
|
+
const summaryTokens = response?.usage?.output_tokens ?? 0;
|
|
291
|
+
const startedAt = llmSpan.ended_at ?? llmSpan.started_at ?? nowIso();
|
|
292
|
+
const memorySpan = {
|
|
293
|
+
trace_id: state.traceId,
|
|
294
|
+
span_id: generateSpanId(),
|
|
295
|
+
type: "memory",
|
|
296
|
+
name: "anthropic.summarize",
|
|
297
|
+
parent_span_id: llmSpan.span_id,
|
|
298
|
+
started_at: startedAt,
|
|
299
|
+
ended_at: startedAt,
|
|
300
|
+
duration_ms: 0,
|
|
301
|
+
status: "completed",
|
|
302
|
+
metadata: {
|
|
303
|
+
memory_op_type: "consolidate",
|
|
304
|
+
summary_token_count: summaryTokens,
|
|
305
|
+
summary_content_hash: summaryHash
|
|
306
|
+
}
|
|
307
|
+
};
|
|
308
|
+
state.completedSpans.push(memorySpan);
|
|
309
|
+
}
|
|
310
|
+
function extractFirstTextBlock(response) {
|
|
311
|
+
try {
|
|
312
|
+
const content = response?.content;
|
|
313
|
+
if (!Array.isArray(content)) return "";
|
|
314
|
+
for (const block of content) {
|
|
315
|
+
if (block && typeof block === "object" && block.type === "text") {
|
|
316
|
+
const t = block.text;
|
|
317
|
+
if (typeof t === "string" && t.length > 0) return t;
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
return "";
|
|
321
|
+
} catch {
|
|
322
|
+
return "";
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
function makeSegment(position, source, rawContent) {
|
|
326
|
+
const fullHash = sha256Hex(rawContent);
|
|
327
|
+
const truncated = truncateToBytes(rawContent, MAX_SEGMENT_CONTENT_BYTES);
|
|
328
|
+
const seg = {
|
|
329
|
+
position,
|
|
330
|
+
source,
|
|
331
|
+
content_hash: fullHash,
|
|
332
|
+
content: truncated.text,
|
|
333
|
+
cache_state: "cold"
|
|
334
|
+
};
|
|
335
|
+
if (truncated.truncated) seg.content_truncated = true;
|
|
336
|
+
return seg;
|
|
337
|
+
}
|
|
338
|
+
function normalizeContent(content) {
|
|
339
|
+
if (typeof content === "string") {
|
|
340
|
+
return [{ type: "text", text: content }];
|
|
341
|
+
}
|
|
342
|
+
if (Array.isArray(content)) {
|
|
343
|
+
return content.filter((b) => !!b && typeof b === "object");
|
|
344
|
+
}
|
|
345
|
+
return [];
|
|
346
|
+
}
|
|
347
|
+
function renderBlocksAsText(blocks, role) {
|
|
348
|
+
const out = [];
|
|
349
|
+
for (const b of blocks) {
|
|
350
|
+
if (!b || typeof b !== "object") continue;
|
|
351
|
+
if (b.type === "text") {
|
|
352
|
+
const text = b.text;
|
|
353
|
+
if (typeof text === "string") out.push(text);
|
|
354
|
+
} else if (b.type === "tool_use") {
|
|
355
|
+
const tu = b;
|
|
356
|
+
out.push(`[tool_use:${tu.name}:${tu.id}] ${canonicalJsonStringify(tu.input ?? null)}`);
|
|
357
|
+
} else {
|
|
358
|
+
out.push(canonicalJsonStringify(b));
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
if (role === "assistant") {
|
|
362
|
+
return out.length === 1 && out[0] !== void 0 ? out[0] : out.join("\n");
|
|
363
|
+
}
|
|
364
|
+
return out.length === 1 && out[0] !== void 0 ? out[0] : out.join("\n");
|
|
365
|
+
}
|
|
366
|
+
function renderToolResult(tr) {
|
|
367
|
+
if (typeof tr.content === "string") return tr.content;
|
|
368
|
+
if (Array.isArray(tr.content)) {
|
|
369
|
+
const parts = [];
|
|
370
|
+
for (const b of tr.content) {
|
|
371
|
+
if (b && typeof b === "object") {
|
|
372
|
+
if (b.type === "text" && typeof b.text === "string") parts.push(b.text);
|
|
373
|
+
else parts.push(canonicalJsonStringify(b));
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
return parts.join("\n");
|
|
377
|
+
}
|
|
378
|
+
return "";
|
|
379
|
+
}
|
|
380
|
+
function canonicalJsonStringify(value) {
|
|
381
|
+
return JSON.stringify(value, function replacer(_key, val) {
|
|
382
|
+
if (val && typeof val === "object" && !Array.isArray(val)) {
|
|
383
|
+
const obj = val;
|
|
384
|
+
const sorted = {};
|
|
385
|
+
for (const k of Object.keys(obj).sort()) {
|
|
386
|
+
sorted[k] = obj[k];
|
|
387
|
+
}
|
|
388
|
+
return sorted;
|
|
389
|
+
}
|
|
390
|
+
return val;
|
|
391
|
+
});
|
|
392
|
+
}
|
|
393
|
+
function sha256Hex(text) {
|
|
394
|
+
try {
|
|
395
|
+
if (typeof process !== "undefined" && process.versions?.node) {
|
|
396
|
+
const dynamicRequire = Function("m", "return require(m)");
|
|
397
|
+
const cryptoMod = dynamicRequire("node:crypto");
|
|
398
|
+
if (cryptoMod?.createHash) {
|
|
399
|
+
return cryptoMod.createHash("sha256").update(text).digest("hex");
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
} catch {
|
|
403
|
+
}
|
|
404
|
+
return purejsSha256(text);
|
|
405
|
+
}
|
|
406
|
+
function truncateToBytes(text, maxBytes) {
|
|
407
|
+
if (text.length <= maxBytes / 4) return { text, truncated: false };
|
|
408
|
+
const bytes = utf8Encode(text);
|
|
409
|
+
if (bytes.length <= maxBytes) return { text, truncated: false };
|
|
410
|
+
let cut = maxBytes;
|
|
411
|
+
while (cut > 0 && (bytes[cut] & 192) === 128) {
|
|
412
|
+
cut--;
|
|
413
|
+
}
|
|
414
|
+
return { text: utf8Decode(bytes.subarray(0, cut)), truncated: true };
|
|
415
|
+
}
|
|
416
|
+
function utf8Encode(text) {
|
|
417
|
+
if (typeof TextEncoder !== "undefined") {
|
|
418
|
+
return new TextEncoder().encode(text);
|
|
419
|
+
}
|
|
420
|
+
const out = [];
|
|
421
|
+
for (let i = 0; i < text.length; i++) {
|
|
422
|
+
const code = text.charCodeAt(i);
|
|
423
|
+
if (code < 128) {
|
|
424
|
+
out.push(code);
|
|
425
|
+
} else if (code < 2048) {
|
|
426
|
+
out.push(192 | code >> 6, 128 | code & 63);
|
|
427
|
+
} else {
|
|
428
|
+
out.push(224 | code >> 12, 128 | code >> 6 & 63, 128 | code & 63);
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
return new Uint8Array(out);
|
|
432
|
+
}
|
|
433
|
+
function utf8Decode(bytes) {
|
|
434
|
+
if (typeof TextDecoder !== "undefined") {
|
|
435
|
+
return new TextDecoder("utf-8").decode(bytes);
|
|
436
|
+
}
|
|
437
|
+
let out = "";
|
|
438
|
+
for (let i = 0; i < bytes.length; ) {
|
|
439
|
+
const b = bytes[i];
|
|
440
|
+
if (b < 128) {
|
|
441
|
+
out += String.fromCharCode(b);
|
|
442
|
+
i++;
|
|
443
|
+
} else if (b < 224) {
|
|
444
|
+
out += String.fromCharCode((b & 31) << 6 | bytes[i + 1] & 63);
|
|
445
|
+
i += 2;
|
|
446
|
+
} else {
|
|
447
|
+
out += String.fromCharCode(
|
|
448
|
+
(b & 15) << 12 | (bytes[i + 1] & 63) << 6 | bytes[i + 2] & 63
|
|
449
|
+
);
|
|
450
|
+
i += 3;
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
return out;
|
|
454
|
+
}
|
|
455
|
+
function purejsSha256(text) {
|
|
456
|
+
const K = [
|
|
457
|
+
1116352408,
|
|
458
|
+
1899447441,
|
|
459
|
+
3049323471,
|
|
460
|
+
3921009573,
|
|
461
|
+
961987163,
|
|
462
|
+
1508970993,
|
|
463
|
+
2453635748,
|
|
464
|
+
2870763221,
|
|
465
|
+
3624381080,
|
|
466
|
+
310598401,
|
|
467
|
+
607225278,
|
|
468
|
+
1426881987,
|
|
469
|
+
1925078388,
|
|
470
|
+
2162078206,
|
|
471
|
+
2614888103,
|
|
472
|
+
3248222580,
|
|
473
|
+
3835390401,
|
|
474
|
+
4022224774,
|
|
475
|
+
264347078,
|
|
476
|
+
604807628,
|
|
477
|
+
770255983,
|
|
478
|
+
1249150122,
|
|
479
|
+
1555081692,
|
|
480
|
+
1996064986,
|
|
481
|
+
2554220882,
|
|
482
|
+
2821834349,
|
|
483
|
+
2952996808,
|
|
484
|
+
3210313671,
|
|
485
|
+
3336571891,
|
|
486
|
+
3584528711,
|
|
487
|
+
113926993,
|
|
488
|
+
338241895,
|
|
489
|
+
666307205,
|
|
490
|
+
773529912,
|
|
491
|
+
1294757372,
|
|
492
|
+
1396182291,
|
|
493
|
+
1695183700,
|
|
494
|
+
1986661051,
|
|
495
|
+
2177026350,
|
|
496
|
+
2456956037,
|
|
497
|
+
2730485921,
|
|
498
|
+
2820302411,
|
|
499
|
+
3259730800,
|
|
500
|
+
3345764771,
|
|
501
|
+
3516065817,
|
|
502
|
+
3600352804,
|
|
503
|
+
4094571909,
|
|
504
|
+
275423344,
|
|
505
|
+
430227734,
|
|
506
|
+
506948616,
|
|
507
|
+
659060556,
|
|
508
|
+
883997877,
|
|
509
|
+
958139571,
|
|
510
|
+
1322822218,
|
|
511
|
+
1537002063,
|
|
512
|
+
1747873779,
|
|
513
|
+
1955562222,
|
|
514
|
+
2024104815,
|
|
515
|
+
2227730452,
|
|
516
|
+
2361852424,
|
|
517
|
+
2428436474,
|
|
518
|
+
2756734187,
|
|
519
|
+
3204031479,
|
|
520
|
+
3329325298
|
|
521
|
+
];
|
|
522
|
+
const H = [
|
|
523
|
+
1779033703,
|
|
524
|
+
3144134277,
|
|
525
|
+
1013904242,
|
|
526
|
+
2773480762,
|
|
527
|
+
1359893119,
|
|
528
|
+
2600822924,
|
|
529
|
+
528734635,
|
|
530
|
+
1541459225
|
|
531
|
+
];
|
|
532
|
+
const msg = utf8Encode(text);
|
|
533
|
+
const msgLen = msg.length;
|
|
534
|
+
const bitLen = msgLen * 8;
|
|
535
|
+
const padded = new Uint8Array(msgLen + 9 + 63 >> 6 << 6);
|
|
536
|
+
padded.set(msg);
|
|
537
|
+
padded[msgLen] = 128;
|
|
538
|
+
const lo = (bitLen & 4294967295) >>> 0;
|
|
539
|
+
const hi = Math.floor(bitLen / 4294967296) >>> 0;
|
|
540
|
+
padded[padded.length - 8] = hi >>> 24 & 255;
|
|
541
|
+
padded[padded.length - 7] = hi >>> 16 & 255;
|
|
542
|
+
padded[padded.length - 6] = hi >>> 8 & 255;
|
|
543
|
+
padded[padded.length - 5] = hi & 255;
|
|
544
|
+
padded[padded.length - 4] = lo >>> 24 & 255;
|
|
545
|
+
padded[padded.length - 3] = lo >>> 16 & 255;
|
|
546
|
+
padded[padded.length - 2] = lo >>> 8 & 255;
|
|
547
|
+
padded[padded.length - 1] = lo & 255;
|
|
548
|
+
const W = new Uint32Array(64);
|
|
549
|
+
for (let chunk = 0; chunk < padded.length; chunk += 64) {
|
|
550
|
+
for (let i = 0; i < 16; i++) {
|
|
551
|
+
W[i] = (padded[chunk + i * 4] << 24 | padded[chunk + i * 4 + 1] << 16 | padded[chunk + i * 4 + 2] << 8 | padded[chunk + i * 4 + 3]) >>> 0;
|
|
552
|
+
}
|
|
553
|
+
for (let i = 16; i < 64; i++) {
|
|
554
|
+
const s0 = rotr(W[i - 15], 7) ^ rotr(W[i - 15], 18) ^ W[i - 15] >>> 3;
|
|
555
|
+
const s1 = rotr(W[i - 2], 17) ^ rotr(W[i - 2], 19) ^ W[i - 2] >>> 10;
|
|
556
|
+
W[i] = W[i - 16] + s0 + W[i - 7] + s1 >>> 0;
|
|
557
|
+
}
|
|
558
|
+
let [a, b, c, d, e, f, g, h] = H;
|
|
559
|
+
for (let i = 0; i < 64; i++) {
|
|
560
|
+
const S1 = rotr(e, 6) ^ rotr(e, 11) ^ rotr(e, 25);
|
|
561
|
+
const ch = e & f ^ ~e & g;
|
|
562
|
+
const temp1 = h + S1 + ch + K[i] + W[i] >>> 0;
|
|
563
|
+
const S0 = rotr(a, 2) ^ rotr(a, 13) ^ rotr(a, 22);
|
|
564
|
+
const maj = a & b ^ a & c ^ b & c;
|
|
565
|
+
const temp2 = S0 + maj >>> 0;
|
|
566
|
+
h = g;
|
|
567
|
+
g = f;
|
|
568
|
+
f = e;
|
|
569
|
+
e = d + temp1 >>> 0;
|
|
570
|
+
d = c;
|
|
571
|
+
c = b;
|
|
572
|
+
b = a;
|
|
573
|
+
a = temp1 + temp2 >>> 0;
|
|
574
|
+
}
|
|
575
|
+
H[0] = H[0] + a >>> 0;
|
|
576
|
+
H[1] = H[1] + b >>> 0;
|
|
577
|
+
H[2] = H[2] + c >>> 0;
|
|
578
|
+
H[3] = H[3] + d >>> 0;
|
|
579
|
+
H[4] = H[4] + e >>> 0;
|
|
580
|
+
H[5] = H[5] + f >>> 0;
|
|
581
|
+
H[6] = H[6] + g >>> 0;
|
|
582
|
+
H[7] = H[7] + h >>> 0;
|
|
583
|
+
}
|
|
584
|
+
let hex = "";
|
|
585
|
+
for (const v of H) {
|
|
586
|
+
hex += v.toString(16).padStart(8, "0");
|
|
587
|
+
}
|
|
588
|
+
return hex;
|
|
589
|
+
}
|
|
590
|
+
function rotr(x, n) {
|
|
591
|
+
return (x >>> n | x << 32 - n) >>> 0;
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
export { MAX_SEGMENT_CONTENT_BYTES, _resetAnthropicInstallStateForTesting, extractAnthropicSegments, instrumentAnthropic, isAnthropicWrapped, uninstallAnthropic, wrapAnthropic };
|
|
595
|
+
//# sourceMappingURL=chunk-A24L5N5N.js.map
|
|
596
|
+
//# sourceMappingURL=chunk-A24L5N5N.js.map
|