@niadra/sdk 0.1.0 → 0.7.0
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/CHANGELOG.md +161 -20
- package/README.md +484 -38
- package/dist/ai-sdk.cjs +1037 -0
- package/dist/ai-sdk.cjs.map +1 -0
- package/dist/ai-sdk.d.cts +118 -0
- package/dist/ai-sdk.d.ts +118 -0
- package/dist/ai-sdk.js +1030 -0
- package/dist/ai-sdk.js.map +1 -0
- package/dist/anthropic.cjs +528 -0
- package/dist/anthropic.cjs.map +1 -0
- package/dist/anthropic.d.cts +35 -0
- package/dist/anthropic.d.ts +35 -0
- package/dist/anthropic.js +523 -0
- package/dist/anthropic.js.map +1 -0
- package/dist/bedrock.cjs +392 -0
- package/dist/bedrock.cjs.map +1 -0
- package/dist/bedrock.d.cts +25 -0
- package/dist/bedrock.d.ts +25 -0
- package/dist/bedrock.js +388 -0
- package/dist/bedrock.js.map +1 -0
- package/dist/cli.js +11045 -0
- package/dist/cli.js.map +1 -0
- package/dist/client-DsIxZxZk.d.cts +6905 -0
- package/dist/client-DsIxZxZk.d.ts +6905 -0
- package/dist/cloudflare-agents.cjs +626 -0
- package/dist/cloudflare-agents.cjs.map +1 -0
- package/dist/cloudflare-agents.d.cts +113 -0
- package/dist/cloudflare-agents.d.ts +113 -0
- package/dist/cloudflare-agents.js +622 -0
- package/dist/cloudflare-agents.js.map +1 -0
- package/dist/elevenlabs.cjs +738 -0
- package/dist/elevenlabs.cjs.map +1 -0
- package/dist/elevenlabs.d.cts +86 -0
- package/dist/elevenlabs.d.ts +86 -0
- package/dist/elevenlabs.js +733 -0
- package/dist/elevenlabs.js.map +1 -0
- package/dist/genkit.cjs +323 -0
- package/dist/genkit.cjs.map +1 -0
- package/dist/genkit.d.cts +67 -0
- package/dist/genkit.d.ts +67 -0
- package/dist/genkit.js +318 -0
- package/dist/genkit.js.map +1 -0
- package/dist/google-adk.cjs +705 -0
- package/dist/google-adk.cjs.map +1 -0
- package/dist/google-adk.d.cts +91 -0
- package/dist/google-adk.d.ts +91 -0
- package/dist/google-adk.js +701 -0
- package/dist/google-adk.js.map +1 -0
- package/dist/google-genai.cjs +422 -0
- package/dist/google-genai.cjs.map +1 -0
- package/dist/google-genai.d.cts +26 -0
- package/dist/google-genai.d.ts +26 -0
- package/dist/google-genai.js +418 -0
- package/dist/google-genai.js.map +1 -0
- package/dist/index.cjs +11449 -1796
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1006 -1247
- package/dist/index.d.ts +1006 -1247
- package/dist/index.js +11393 -1797
- package/dist/index.js.map +1 -1
- package/dist/intercept-0_lJE6k1.d.cts +20 -0
- package/dist/intercept-D7qFCaRb.d.ts +20 -0
- package/dist/langchain.cjs +1077 -0
- package/dist/langchain.cjs.map +1 -0
- package/dist/langchain.d.cts +102 -0
- package/dist/langchain.d.ts +102 -0
- package/dist/langchain.js +1068 -0
- package/dist/langchain.js.map +1 -0
- package/dist/livekit.cjs +404 -0
- package/dist/livekit.cjs.map +1 -0
- package/dist/livekit.d.cts +113 -0
- package/dist/livekit.d.ts +113 -0
- package/dist/livekit.js +398 -0
- package/dist/livekit.js.map +1 -0
- package/dist/llamaindex.cjs +349 -0
- package/dist/llamaindex.cjs.map +1 -0
- package/dist/llamaindex.d.cts +92 -0
- package/dist/llamaindex.d.ts +92 -0
- package/dist/llamaindex.js +344 -0
- package/dist/llamaindex.js.map +1 -0
- package/dist/mastra.cjs +1082 -0
- package/dist/mastra.cjs.map +1 -0
- package/dist/mastra.d.cts +86 -0
- package/dist/mastra.d.ts +86 -0
- package/dist/mastra.js +1075 -0
- package/dist/mastra.js.map +1 -0
- package/dist/openai-agents.cjs +465 -0
- package/dist/openai-agents.cjs.map +1 -0
- package/dist/openai-agents.d.cts +75 -0
- package/dist/openai-agents.d.ts +75 -0
- package/dist/openai-agents.js +459 -0
- package/dist/openai-agents.js.map +1 -0
- package/dist/retell.cjs +846 -0
- package/dist/retell.cjs.map +1 -0
- package/dist/retell.d.cts +161 -0
- package/dist/retell.d.ts +161 -0
- package/dist/retell.js +841 -0
- package/dist/retell.js.map +1 -0
- package/dist/shared-Bb5B59Bu.d.ts +39 -0
- package/dist/shared-DTPWVlWm.d.cts +39 -0
- package/dist/strands.cjs +352 -0
- package/dist/strands.cjs.map +1 -0
- package/dist/strands.d.cts +72 -0
- package/dist/strands.d.ts +72 -0
- package/dist/strands.js +347 -0
- package/dist/strands.js.map +1 -0
- package/dist/twilio.cjs +174 -0
- package/dist/twilio.cjs.map +1 -0
- package/dist/twilio.d.cts +64 -0
- package/dist/twilio.d.ts +64 -0
- package/dist/twilio.js +167 -0
- package/dist/twilio.js.map +1 -0
- package/dist/vapi.cjs +661 -0
- package/dist/vapi.cjs.map +1 -0
- package/dist/vapi.d.cts +78 -0
- package/dist/vapi.d.ts +78 -0
- package/dist/vapi.js +656 -0
- package/dist/vapi.js.map +1 -0
- package/dist/voltagent.cjs +509 -0
- package/dist/voltagent.cjs.map +1 -0
- package/dist/voltagent.d.cts +70 -0
- package/dist/voltagent.d.ts +70 -0
- package/dist/voltagent.js +503 -0
- package/dist/voltagent.js.map +1 -0
- package/dist/webhook-1ozGyksG.d.ts +37 -0
- package/dist/webhook-CG4om_DK.d.cts +37 -0
- package/dist/whatsapp.cjs +214 -0
- package/dist/whatsapp.cjs.map +1 -0
- package/dist/whatsapp.d.cts +81 -0
- package/dist/whatsapp.d.ts +81 -0
- package/dist/whatsapp.js +207 -0
- package/dist/whatsapp.js.map +1 -0
- package/package.json +355 -7
|
@@ -0,0 +1,523 @@
|
|
|
1
|
+
// src/usage.ts
|
|
2
|
+
var PROVIDER = /^[a-z0-9][a-z0-9_.-]{0,63}$/;
|
|
3
|
+
var MODEL = /^[A-Za-z0-9][A-Za-z0-9_.:/@-]{0,127}$/;
|
|
4
|
+
function get(value, name) {
|
|
5
|
+
return (typeof value === "object" || typeof value === "function") && value !== null ? value[name] : void 0;
|
|
6
|
+
}
|
|
7
|
+
function count(value) {
|
|
8
|
+
return typeof value === "number" && Number.isInteger(value) && value >= 0 ? value : null;
|
|
9
|
+
}
|
|
10
|
+
function tokenCounts(usage) {
|
|
11
|
+
const read = count(get(usage, "cache_read_input_tokens")) ?? 0;
|
|
12
|
+
const written = count(get(usage, "cache_creation_input_tokens")) ?? 0;
|
|
13
|
+
let prompt = count(get(usage, "prompt_tokens"));
|
|
14
|
+
let cached;
|
|
15
|
+
if (prompt !== null) {
|
|
16
|
+
const reported = count(get(get(usage, "prompt_tokens_details"), "cached_tokens"));
|
|
17
|
+
cached = reported !== null && reported > 0 ? reported : read;
|
|
18
|
+
} else {
|
|
19
|
+
const inputs = count(get(usage, "input_tokens"));
|
|
20
|
+
if (inputs === null) return null;
|
|
21
|
+
const details = get(usage, "input_tokens_details");
|
|
22
|
+
if (details !== void 0 && details !== null) {
|
|
23
|
+
prompt = inputs;
|
|
24
|
+
cached = count(get(details, "cached_tokens")) ?? 0;
|
|
25
|
+
} else {
|
|
26
|
+
prompt = inputs + read + written;
|
|
27
|
+
cached = read;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return { prompt_tokens: Math.max(prompt, cached + written), cached_tokens: cached, cache_write_tokens: written };
|
|
31
|
+
}
|
|
32
|
+
function providerOf(model, usage) {
|
|
33
|
+
const slash = model.indexOf("/");
|
|
34
|
+
if (slash > 0) return model.slice(0, slash).trim().toLowerCase();
|
|
35
|
+
const name = model.trim().toLowerCase();
|
|
36
|
+
if (name.startsWith("claude") || `.${name}`.includes(".anthropic.")) return "anthropic";
|
|
37
|
+
if (name.startsWith("gemini")) return "google";
|
|
38
|
+
if (get(usage, "prompt_tokens") === void 0 && get(usage, "cache_read_input_tokens") !== void 0) {
|
|
39
|
+
return "anthropic";
|
|
40
|
+
}
|
|
41
|
+
return "openai";
|
|
42
|
+
}
|
|
43
|
+
function modelUsage(response, options = {}) {
|
|
44
|
+
try {
|
|
45
|
+
let usage = get(response, "usage");
|
|
46
|
+
if (usage === void 0 || usage === null || typeof usage !== "object") usage = response;
|
|
47
|
+
const counts = tokenCounts(usage);
|
|
48
|
+
const name = options.model ?? get(response, "model");
|
|
49
|
+
if (!counts || typeof name !== "string" || !MODEL.test(name)) return null;
|
|
50
|
+
const provider = (options.provider ?? providerOf(name, usage)).toLowerCase();
|
|
51
|
+
if (!PROVIDER.test(provider)) return null;
|
|
52
|
+
return { provider, model: name, ...counts };
|
|
53
|
+
} catch {
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// src/context.ts
|
|
59
|
+
function emptyResult(error) {
|
|
60
|
+
return { text: "", suffix: "", variables: {}, pack: null, source: "none", response: null, error, constraints: null, state: null, coordination: null };
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// src/integrations/shared.ts
|
|
64
|
+
var ATTESTED = { A: "V2", B: "V1", C: "V1" };
|
|
65
|
+
function attestationProof(attestation) {
|
|
66
|
+
if (!attestation) return null;
|
|
67
|
+
const value = attestation.trim().toUpperCase();
|
|
68
|
+
if (value.includes("FAILED") || value.startsWith("NO-")) return null;
|
|
69
|
+
const letter = value.length === 1 ? value : /PASSED-([ABC])$/.exec(value)?.[1];
|
|
70
|
+
const level = letter ? ATTESTED[letter] : void 0;
|
|
71
|
+
return level ? { method: "network_attestation", level } : null;
|
|
72
|
+
}
|
|
73
|
+
function resolveSession(source) {
|
|
74
|
+
if (typeof source !== "function") return source;
|
|
75
|
+
try {
|
|
76
|
+
return source() ?? null;
|
|
77
|
+
} catch {
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
function isConversation(session) {
|
|
82
|
+
return "customer" in session && typeof session.customer === "function";
|
|
83
|
+
}
|
|
84
|
+
function errorName(error) {
|
|
85
|
+
return error instanceof Error ? error.name : typeof error;
|
|
86
|
+
}
|
|
87
|
+
var Bridge = class {
|
|
88
|
+
constructor(session, proof, memory = false) {
|
|
89
|
+
this.session = session;
|
|
90
|
+
this.proof = proof;
|
|
91
|
+
this.memory = memory;
|
|
92
|
+
}
|
|
93
|
+
session;
|
|
94
|
+
proof;
|
|
95
|
+
memory;
|
|
96
|
+
proving = null;
|
|
97
|
+
/** `begin()` started the first read of a call: the first `context()` waits for it with `ready()`. */
|
|
98
|
+
begun = false;
|
|
99
|
+
opened = false;
|
|
100
|
+
/**
|
|
101
|
+
* The context and, when enabled, the agent's notes, read side by side within the same budget.
|
|
102
|
+
* Never rejects.
|
|
103
|
+
*/
|
|
104
|
+
async read(options = {}) {
|
|
105
|
+
const memoryParams = this.memory === false ? null : this.memory === true ? {} : withoutWrite(this.memory);
|
|
106
|
+
const [context, memory] = await Promise.all([
|
|
107
|
+
this.context(options),
|
|
108
|
+
memoryParams ? this.agentMemory(memoryParams, options) : Promise.resolve("")
|
|
109
|
+
]);
|
|
110
|
+
return { context, memory, prefix: [memory, context.text].filter(Boolean).join("\n\n"), suffix: context.suffix };
|
|
111
|
+
}
|
|
112
|
+
get logger() {
|
|
113
|
+
return this.session.logger;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Starts the first read of a call now, in the background, after the proof when one was given (the
|
|
117
|
+
* pack depends on the level it proves): at call start, so the read runs while the call is set up.
|
|
118
|
+
* The first `context()` then waits for it within `timeouts.contextVoiceStart`. Never throws.
|
|
119
|
+
*/
|
|
120
|
+
begin() {
|
|
121
|
+
const session = this.session;
|
|
122
|
+
if (!isConversation(session)) return;
|
|
123
|
+
const start = () => {
|
|
124
|
+
this.safe("start the first read", () => {
|
|
125
|
+
this.begun = session.begin();
|
|
126
|
+
});
|
|
127
|
+
};
|
|
128
|
+
if (this.proof === void 0 || this.proof === null) start();
|
|
129
|
+
else void this.verifyOnce().then(start);
|
|
130
|
+
}
|
|
131
|
+
/** Verifies once (when a proof was given), then reads the context. Never rejects. */
|
|
132
|
+
async context(options = {}) {
|
|
133
|
+
await this.verifyOnce();
|
|
134
|
+
try {
|
|
135
|
+
const session = this.session;
|
|
136
|
+
if (this.begun && !this.opened && isConversation(session)) {
|
|
137
|
+
this.opened = true;
|
|
138
|
+
await session.ready(options.timeout === void 0 ? {} : { timeout: options.timeout });
|
|
139
|
+
}
|
|
140
|
+
return await this.session.context(options);
|
|
141
|
+
} catch (error) {
|
|
142
|
+
this.logger.warn(`could not read context (${errorName(error)})`);
|
|
143
|
+
return emptyResult(null);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
/** Records that `context` went into the prompt, unless it was empty for a reason other than the holdout. */
|
|
147
|
+
injected(context) {
|
|
148
|
+
if (!context.text && !context.suffix && context.response?.path !== "holdout") return;
|
|
149
|
+
this.safe("mark the injection", () => {
|
|
150
|
+
this.session.markInjected(context);
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
customer(text, options = {}) {
|
|
154
|
+
const session = this.session;
|
|
155
|
+
if (!text.trim() || !isConversation(session)) return;
|
|
156
|
+
this.safe("record the customer's turn", () => session.customer(text, options));
|
|
157
|
+
}
|
|
158
|
+
/** Sends the customer's turn so far, while they speak. In the background; never throws. */
|
|
159
|
+
prefetch(text) {
|
|
160
|
+
const session = this.session;
|
|
161
|
+
if (!text.trim() || !isConversation(session)) return;
|
|
162
|
+
this.safe("prefetch the turn", () => session.prefetch(text));
|
|
163
|
+
}
|
|
164
|
+
agent(text, options = {}) {
|
|
165
|
+
if (!text.trim()) return;
|
|
166
|
+
this.safe("record the agent's turn", () => this.session.agent(text, options));
|
|
167
|
+
}
|
|
168
|
+
/** Records a transfer. Tasks have no transfer; the call is ignored for them. */
|
|
169
|
+
async handoff(target, reason) {
|
|
170
|
+
const session = this.session;
|
|
171
|
+
if (!isConversation(session)) return;
|
|
172
|
+
try {
|
|
173
|
+
await session.handoff(reason ? { target, reason } : { target });
|
|
174
|
+
} catch (error) {
|
|
175
|
+
this.logger.warn(`could not record the handoff (${errorName(error)})`);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
async end() {
|
|
179
|
+
try {
|
|
180
|
+
await this.session.end();
|
|
181
|
+
} catch (error) {
|
|
182
|
+
this.logger.warn(`could not end the session (${errorName(error)})`);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
/** The navigation kit as plain specs (with the agent's memory tools when enabled), or none for a task about an object only. */
|
|
186
|
+
tools() {
|
|
187
|
+
let bound;
|
|
188
|
+
try {
|
|
189
|
+
const memory = this.memory;
|
|
190
|
+
bound = this.session.tools(memory === false ? {} : { agentMemory: true, writeAgentMemory: memory !== true && memory.write === true });
|
|
191
|
+
} catch (error) {
|
|
192
|
+
this.logger.warn(`could not build the tools (${errorName(error)})`);
|
|
193
|
+
return [];
|
|
194
|
+
}
|
|
195
|
+
return bound ? toolSpecs(bound) : [];
|
|
196
|
+
}
|
|
197
|
+
async agentMemory(params, options) {
|
|
198
|
+
try {
|
|
199
|
+
const result = await this.session.agentMemory(params, options.timeout === void 0 ? {} : { timeout: options.timeout });
|
|
200
|
+
return result.text;
|
|
201
|
+
} catch (error) {
|
|
202
|
+
this.logger.warn(`could not read the agent's memory (${errorName(error)})`);
|
|
203
|
+
return "";
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
async verifyOnce() {
|
|
207
|
+
if (this.proof === void 0 || this.proof === null) return;
|
|
208
|
+
this.proving ??= this.runVerify();
|
|
209
|
+
await this.proving;
|
|
210
|
+
}
|
|
211
|
+
async runVerify() {
|
|
212
|
+
try {
|
|
213
|
+
const source = this.proof;
|
|
214
|
+
const proof = typeof source === "function" ? await source() : source;
|
|
215
|
+
if (!proof) return;
|
|
216
|
+
await this.session.verify(proof);
|
|
217
|
+
} catch (error) {
|
|
218
|
+
this.logger.warn(`could not record the verification (${errorName(error)})`);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
safe(what, run) {
|
|
222
|
+
try {
|
|
223
|
+
run();
|
|
224
|
+
} catch (error) {
|
|
225
|
+
this.logger.warn(`could not ${what} (${errorName(error)})`);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
};
|
|
229
|
+
function withoutWrite(option) {
|
|
230
|
+
const { write: _, ...params } = option;
|
|
231
|
+
return params;
|
|
232
|
+
}
|
|
233
|
+
function toolSpecs(bound) {
|
|
234
|
+
return bound.definitions.map(({ function: definition }) => ({
|
|
235
|
+
name: definition.name,
|
|
236
|
+
description: definition.description,
|
|
237
|
+
parameters: definition.parameters,
|
|
238
|
+
execute: (args) => bound.call(definition.name, args)
|
|
239
|
+
}));
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
// src/integrations/intercept.ts
|
|
243
|
+
var States = class {
|
|
244
|
+
constructor(options) {
|
|
245
|
+
this.options = options;
|
|
246
|
+
}
|
|
247
|
+
options;
|
|
248
|
+
states = /* @__PURE__ */ new WeakMap();
|
|
249
|
+
/** The session's state; a new one takes `options` (by default the ones this object was built with). */
|
|
250
|
+
resolve(source, options = this.options) {
|
|
251
|
+
const session = resolveSession(source);
|
|
252
|
+
if (!session) return null;
|
|
253
|
+
let state = this.states.get(session);
|
|
254
|
+
if (!state) {
|
|
255
|
+
state = { bridge: new Bridge(session, options.verify, options.agentMemory), seen: /* @__PURE__ */ new Set() };
|
|
256
|
+
this.states.set(session, state);
|
|
257
|
+
}
|
|
258
|
+
return state;
|
|
259
|
+
}
|
|
260
|
+
/** Records the user message at `index` once, however many tool steps send it again. */
|
|
261
|
+
customer(state, index, text) {
|
|
262
|
+
if (!(this.options.recordCustomer ?? true) || index < 0 || !text) return;
|
|
263
|
+
const key = `${String(index)}:${text}`;
|
|
264
|
+
if (state.seen.has(key)) return;
|
|
265
|
+
state.seen.add(key);
|
|
266
|
+
state.bridge.customer(text);
|
|
267
|
+
}
|
|
268
|
+
get recordsAgent() {
|
|
269
|
+
return this.options.recordAgent ?? true;
|
|
270
|
+
}
|
|
271
|
+
};
|
|
272
|
+
function overlay(target, overrides) {
|
|
273
|
+
return new Proxy(target, {
|
|
274
|
+
get(obj, prop) {
|
|
275
|
+
if (Object.prototype.hasOwnProperty.call(overrides, prop)) return overrides[prop];
|
|
276
|
+
const value = Reflect.get(obj, prop, obj);
|
|
277
|
+
return typeof value === "function" ? value.bind(obj) : value;
|
|
278
|
+
}
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
var Pending = class {
|
|
282
|
+
constructor(started, finish2) {
|
|
283
|
+
this.started = started;
|
|
284
|
+
this.finish = finish2;
|
|
285
|
+
started.catch(() => void 0);
|
|
286
|
+
}
|
|
287
|
+
started;
|
|
288
|
+
finish;
|
|
289
|
+
result = null;
|
|
290
|
+
then(onfulfilled, onrejected) {
|
|
291
|
+
return this.value().then(onfulfilled, onrejected);
|
|
292
|
+
}
|
|
293
|
+
catch(onrejected) {
|
|
294
|
+
return this.value().catch(onrejected);
|
|
295
|
+
}
|
|
296
|
+
finally(onfinally) {
|
|
297
|
+
return this.value().finally(onfinally);
|
|
298
|
+
}
|
|
299
|
+
async withResponse() {
|
|
300
|
+
const { call } = await this.started;
|
|
301
|
+
const method = property(call, "withResponse");
|
|
302
|
+
if (typeof method !== "function") throw new TypeError("this client's calls have no withResponse()");
|
|
303
|
+
const settled = await method.call(call);
|
|
304
|
+
if (!isBag(settled)) return settled;
|
|
305
|
+
return { ...settled, data: this.finish(settled.data) };
|
|
306
|
+
}
|
|
307
|
+
async asResponse() {
|
|
308
|
+
const { call } = await this.started;
|
|
309
|
+
const method = property(call, "asResponse");
|
|
310
|
+
if (typeof method !== "function") throw new TypeError("this client's calls have no asResponse()");
|
|
311
|
+
return method.call(call);
|
|
312
|
+
}
|
|
313
|
+
value() {
|
|
314
|
+
this.result ??= this.started.then(({ call }) => call).then((value) => this.finish(value));
|
|
315
|
+
return this.result;
|
|
316
|
+
}
|
|
317
|
+
};
|
|
318
|
+
function observeStream(stream, observe, done, logger) {
|
|
319
|
+
let finished = false;
|
|
320
|
+
const finish2 = () => {
|
|
321
|
+
if (finished) return;
|
|
322
|
+
finished = true;
|
|
323
|
+
try {
|
|
324
|
+
done();
|
|
325
|
+
} catch (error) {
|
|
326
|
+
logger.warn(`could not record the model's answer (${errorName(error)})`);
|
|
327
|
+
}
|
|
328
|
+
};
|
|
329
|
+
return new Proxy(stream, {
|
|
330
|
+
get(target, prop) {
|
|
331
|
+
if (prop === Symbol.asyncIterator) {
|
|
332
|
+
return () => {
|
|
333
|
+
const inner = target[Symbol.asyncIterator]();
|
|
334
|
+
return {
|
|
335
|
+
async next() {
|
|
336
|
+
try {
|
|
337
|
+
const step = await inner.next();
|
|
338
|
+
if (step.done) finish2();
|
|
339
|
+
else {
|
|
340
|
+
try {
|
|
341
|
+
observe(step.value);
|
|
342
|
+
} catch {
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
return step;
|
|
346
|
+
} catch (error) {
|
|
347
|
+
finish2();
|
|
348
|
+
throw error;
|
|
349
|
+
}
|
|
350
|
+
},
|
|
351
|
+
async return(value2) {
|
|
352
|
+
finish2();
|
|
353
|
+
return inner.return ? inner.return(value2) : { done: true, value: value2 };
|
|
354
|
+
}
|
|
355
|
+
};
|
|
356
|
+
};
|
|
357
|
+
}
|
|
358
|
+
const value = Reflect.get(target, prop, target);
|
|
359
|
+
return typeof value === "function" ? value.bind(target) : value;
|
|
360
|
+
}
|
|
361
|
+
});
|
|
362
|
+
}
|
|
363
|
+
function property(value, name) {
|
|
364
|
+
return isBag(value) ? value[name] : void 0;
|
|
365
|
+
}
|
|
366
|
+
function isBag(value) {
|
|
367
|
+
return (typeof value === "object" || typeof value === "function") && value !== null;
|
|
368
|
+
}
|
|
369
|
+
function isAsyncIterable(value) {
|
|
370
|
+
return isBag(value) && typeof value[Symbol.asyncIterator] === "function";
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
// src/integrations/anthropic.ts
|
|
374
|
+
var MAX_BREAKPOINTS = 4;
|
|
375
|
+
function wrapAnthropic(client, session, options = {}) {
|
|
376
|
+
const states = new States(options);
|
|
377
|
+
const overrides = {};
|
|
378
|
+
const messages = property(client, "messages");
|
|
379
|
+
if (!isBag(messages) || typeof messages.create !== "function") {
|
|
380
|
+
throw new TypeError("wrapAnthropic() expects an Anthropic client with messages.create");
|
|
381
|
+
}
|
|
382
|
+
overrides.messages = overlay(messages, { create: intercept(messages, states, session) });
|
|
383
|
+
const beta = property(client, "beta");
|
|
384
|
+
const betaMessages = property(beta, "messages");
|
|
385
|
+
if (isBag(beta) && isBag(betaMessages) && typeof betaMessages.create === "function") {
|
|
386
|
+
overrides.beta = overlay(beta, { messages: overlay(betaMessages, { create: intercept(betaMessages, states, session) }) });
|
|
387
|
+
}
|
|
388
|
+
return overlay(client, overrides);
|
|
389
|
+
}
|
|
390
|
+
async function anthropicParams(session, body, options = {}) {
|
|
391
|
+
const state = helperStates.resolve(session, options);
|
|
392
|
+
return state ? await prepare(state, helperStates, body) : body;
|
|
393
|
+
}
|
|
394
|
+
var helperStates = new States({});
|
|
395
|
+
function recordAnthropic(session, message) {
|
|
396
|
+
const current = resolveSession(session);
|
|
397
|
+
if (!current) return;
|
|
398
|
+
try {
|
|
399
|
+
const text = textOfBlocks(property(message, "content"));
|
|
400
|
+
const usage = modelUsage(message);
|
|
401
|
+
if (text) current.agent(text, usage ? { usage } : {});
|
|
402
|
+
} catch (error) {
|
|
403
|
+
current.logger.warn(`could not record the model's answer (${errorName(error)})`);
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
function intercept(messages, states, session) {
|
|
407
|
+
const create = messages.create.bind(messages);
|
|
408
|
+
return (body, ...rest) => {
|
|
409
|
+
const state = states.resolve(session);
|
|
410
|
+
if (!state || !isBag(body)) return create(body, ...rest);
|
|
411
|
+
const started = prepare(state, states, body).then((prepared) => ({ call: create(prepared, ...rest) }));
|
|
412
|
+
return new Pending(started, (value) => finish(state, states, value, body.stream === true));
|
|
413
|
+
};
|
|
414
|
+
}
|
|
415
|
+
async function prepare(state, states, body) {
|
|
416
|
+
try {
|
|
417
|
+
const messages = Array.isArray(body.messages) ? body.messages : null;
|
|
418
|
+
if (!messages) return body;
|
|
419
|
+
const last = lastUserWithText(messages);
|
|
420
|
+
if (last >= 0) states.customer(state, last, textOfContent(property(messages[last], "content")));
|
|
421
|
+
const read = await state.bridge.read();
|
|
422
|
+
state.bridge.injected(read.context);
|
|
423
|
+
return inject(body, messages, read);
|
|
424
|
+
} catch (error) {
|
|
425
|
+
state.bridge.logger.warn(`could not inject context (${errorName(error)})`);
|
|
426
|
+
return body;
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
function inject(body, messages, read) {
|
|
430
|
+
const next = { ...body };
|
|
431
|
+
if (read.prefix) {
|
|
432
|
+
const system = body.system;
|
|
433
|
+
if (system === void 0 || system === null || system === "") next.system = read.prefix;
|
|
434
|
+
else if (typeof system === "string") next.system = `${system}
|
|
435
|
+
|
|
436
|
+
${read.prefix}`;
|
|
437
|
+
else if (Array.isArray(system)) {
|
|
438
|
+
const block = { type: "text", text: read.prefix };
|
|
439
|
+
if (breakpoints(body) > 0 && breakpoints(body) < MAX_BREAKPOINTS) block.cache_control = { type: "ephemeral" };
|
|
440
|
+
next.system = [...system, block];
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
if (read.suffix) {
|
|
444
|
+
const index = lastIndexOfRole(messages, "user");
|
|
445
|
+
const message = messages[index];
|
|
446
|
+
if (isBag(message)) {
|
|
447
|
+
const content = message.content;
|
|
448
|
+
const blocks = typeof content === "string" ? [{ type: "text", text: content }] : Array.isArray(content) ? [...content] : [];
|
|
449
|
+
const copy = [...messages];
|
|
450
|
+
copy[index] = { ...message, content: [...blocks, { type: "text", text: read.suffix }] };
|
|
451
|
+
next.messages = copy;
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
return next;
|
|
455
|
+
}
|
|
456
|
+
function finish(state, states, value, stream) {
|
|
457
|
+
if (!states.recordsAgent) return value;
|
|
458
|
+
try {
|
|
459
|
+
if (!stream) {
|
|
460
|
+
const text = textOfBlocks(property(value, "content"));
|
|
461
|
+
const usage = modelUsage(value);
|
|
462
|
+
state.bridge.agent(text, usage ? { usage } : {});
|
|
463
|
+
return value;
|
|
464
|
+
}
|
|
465
|
+
if (!isAsyncIterable(value)) return value;
|
|
466
|
+
const parts = [];
|
|
467
|
+
let start = null;
|
|
468
|
+
return observeStream(
|
|
469
|
+
value,
|
|
470
|
+
(event) => {
|
|
471
|
+
const type = property(event, "type");
|
|
472
|
+
if (type === "message_start") start = property(event, "message");
|
|
473
|
+
else if (type === "content_block_delta") {
|
|
474
|
+
const delta = property(event, "delta");
|
|
475
|
+
if (property(delta, "type") === "text_delta" && typeof property(delta, "text") === "string") parts.push(property(delta, "text"));
|
|
476
|
+
}
|
|
477
|
+
},
|
|
478
|
+
() => {
|
|
479
|
+
const usage = start ? modelUsage(start) : null;
|
|
480
|
+
state.bridge.agent(parts.join(""), usage ? { usage } : {});
|
|
481
|
+
},
|
|
482
|
+
state.bridge.logger
|
|
483
|
+
);
|
|
484
|
+
} catch (error) {
|
|
485
|
+
state.bridge.logger.warn(`could not capture the model's answer (${errorName(error)})`);
|
|
486
|
+
return value;
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
function breakpoints(body) {
|
|
490
|
+
let total = 0;
|
|
491
|
+
const visit = (value) => {
|
|
492
|
+
if (Array.isArray(value)) for (const item of value) visit(item);
|
|
493
|
+
else if (isBag(value)) {
|
|
494
|
+
if (isBag(value.cache_control)) total++;
|
|
495
|
+
if (Array.isArray(value.content)) visit(value.content);
|
|
496
|
+
}
|
|
497
|
+
};
|
|
498
|
+
visit(body.system);
|
|
499
|
+
visit(body.messages);
|
|
500
|
+
visit(body.tools);
|
|
501
|
+
return total;
|
|
502
|
+
}
|
|
503
|
+
function lastUserWithText(messages) {
|
|
504
|
+
for (let index = messages.length - 1; index >= 0; index--) {
|
|
505
|
+
if (property(messages[index], "role") === "user" && textOfContent(property(messages[index], "content"))) return index;
|
|
506
|
+
}
|
|
507
|
+
return -1;
|
|
508
|
+
}
|
|
509
|
+
function lastIndexOfRole(messages, role) {
|
|
510
|
+
for (let index = messages.length - 1; index >= 0; index--) if (property(messages[index], "role") === role) return index;
|
|
511
|
+
return -1;
|
|
512
|
+
}
|
|
513
|
+
function textOfContent(content) {
|
|
514
|
+
return typeof content === "string" ? content : textOfBlocks(content);
|
|
515
|
+
}
|
|
516
|
+
function textOfBlocks(content) {
|
|
517
|
+
if (!Array.isArray(content)) return "";
|
|
518
|
+
return content.map((block) => property(block, "type") === "text" && typeof property(block, "text") === "string" ? property(block, "text") : "").filter(Boolean).join("\n");
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
export { anthropicParams, attestationProof, recordAnthropic, wrapAnthropic };
|
|
522
|
+
//# sourceMappingURL=anthropic.js.map
|
|
523
|
+
//# sourceMappingURL=anthropic.js.map
|