@posthog/ai 8.24.0 → 8.24.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/dist/adk/index.cjs +1 -1
- package/dist/adk/index.mjs +1 -1
- package/dist/anthropic/index.cjs +1 -1
- package/dist/anthropic/index.mjs +1 -1
- package/dist/claude-agent-sdk/index.cjs +1 -1
- package/dist/claude-agent-sdk/index.mjs +1 -1
- package/dist/gemini/index.cjs +162 -13
- package/dist/gemini/index.cjs.map +1 -1
- package/dist/gemini/index.d.ts +2 -1
- package/dist/gemini/index.mjs +140 -13
- package/dist/gemini/index.mjs.map +1 -1
- package/dist/genkit/index.cjs +1 -1
- package/dist/genkit/index.mjs +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.mjs +1 -1
- package/dist/langchain/index.cjs +1 -1
- package/dist/langchain/index.mjs +1 -1
- package/dist/langchain/middleware/index.cjs +1 -1
- package/dist/langchain/middleware/index.mjs +1 -1
- package/dist/openai/index.cjs +1 -1
- package/dist/openai/index.mjs +1 -1
- package/dist/openai-agents/index.cjs +1 -1
- package/dist/openai-agents/index.mjs +1 -1
- package/dist/vercel/index.cjs +1 -1
- package/dist/vercel/index.mjs +1 -1
- package/package.json +5 -5
package/dist/gemini/index.d.ts
CHANGED
|
@@ -45,7 +45,8 @@ type InteractionEvent = InteractionStream extends AsyncIterable<infer Event> ? E
|
|
|
45
45
|
declare class WrappedInteractions {
|
|
46
46
|
private readonly client;
|
|
47
47
|
private readonly phClient;
|
|
48
|
-
|
|
48
|
+
private readonly clientTimeout?;
|
|
49
|
+
constructor(client: GoogleGenAI, phClient: PostHog, clientTimeout?: number | undefined);
|
|
49
50
|
create(params: ForegroundParams & MonitoringParams & {
|
|
50
51
|
stream: true;
|
|
51
52
|
}, options?: CreateOptions): Promise<ReadableStream<InteractionEvent> & AsyncIterable<InteractionEvent>>;
|
package/dist/gemini/index.mjs
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { acceptsEventOptions, eventOptionsAsLegacyProperties, toJsonSafeValue, uuidv7 } from "@posthog/core";
|
|
2
|
+
import * as genai from "@google/genai";
|
|
2
3
|
import { GoogleGenAI } from "@google/genai";
|
|
3
4
|
import { v4 } from "uuid";
|
|
4
5
|
//#region src/captureAiEvent.ts
|
|
@@ -647,7 +648,7 @@ function addDefaults(params) {
|
|
|
647
648
|
}
|
|
648
649
|
//#endregion
|
|
649
650
|
//#region package.json
|
|
650
|
-
var version = "8.24.
|
|
651
|
+
var version = "8.24.1";
|
|
651
652
|
//#endregion
|
|
652
653
|
//#region src/serializeError.ts
|
|
653
654
|
const DEFAULT_MAX_DEPTH = 3;
|
|
@@ -1153,13 +1154,98 @@ function safeTools(tools) {
|
|
|
1153
1154
|
name: string(record(tool)?.name)
|
|
1154
1155
|
}));
|
|
1155
1156
|
}
|
|
1157
|
+
const USAGE_LIST_KEYS = {
|
|
1158
|
+
cached_tokens_by_modality: ["modality", "tokens"],
|
|
1159
|
+
input_tokens_by_modality: ["modality", "tokens"],
|
|
1160
|
+
output_tokens_by_modality: ["modality", "tokens"],
|
|
1161
|
+
tool_use_tokens_by_modality: ["modality", "tokens"],
|
|
1162
|
+
grounding_tool_count: ["type", "count"]
|
|
1163
|
+
};
|
|
1164
|
+
function mergeHopUsage(usages) {
|
|
1165
|
+
if (usages.length <= 1) return usages[0];
|
|
1166
|
+
const merged = { ...usages[usages.length - 1] };
|
|
1167
|
+
for (const name of new Set(usages.flatMap((usage) => Object.keys(usage)))) {
|
|
1168
|
+
const values = usages.map((usage) => usage[name]).filter((value) => value !== void 0 && value !== null);
|
|
1169
|
+
if (values.every((value) => number(value) !== void 0)) merged[name] = values.reduce((total, value) => total + value, 0);
|
|
1170
|
+
else if (USAGE_LIST_KEYS[name] && values.every(Array.isArray)) {
|
|
1171
|
+
const [keyName, valueName] = USAGE_LIST_KEYS[name];
|
|
1172
|
+
const entries = /* @__PURE__ */ new Map();
|
|
1173
|
+
for (const entry of values.flat()) {
|
|
1174
|
+
const item = record(entry);
|
|
1175
|
+
if (!item) continue;
|
|
1176
|
+
const existing = entries.get(item[keyName]);
|
|
1177
|
+
const total = number(existing?.[valueName]);
|
|
1178
|
+
const value = number(item[valueName]);
|
|
1179
|
+
entries.set(item[keyName], existing && total !== void 0 && value !== void 0 ? {
|
|
1180
|
+
...existing,
|
|
1181
|
+
[valueName]: total + value
|
|
1182
|
+
} : existing ?? item);
|
|
1183
|
+
}
|
|
1184
|
+
merged[name] = [...entries.values()];
|
|
1185
|
+
}
|
|
1186
|
+
}
|
|
1187
|
+
const initialInputTokens = usages[0].initial_input_tokens ?? usages[0].total_input_tokens;
|
|
1188
|
+
if (initialInputTokens !== void 0) merged.initial_input_tokens = initialInputTokens;
|
|
1189
|
+
return merged;
|
|
1190
|
+
}
|
|
1191
|
+
function positive(value) {
|
|
1192
|
+
const result = number(value);
|
|
1193
|
+
return result !== void 0 && result > 0 ? result : void 0;
|
|
1194
|
+
}
|
|
1195
|
+
function createStreamAbort(options, clientTimeout) {
|
|
1196
|
+
const requestOptions = record(options) ?? {};
|
|
1197
|
+
const fetchOptions = record(requestOptions.fetch_options) ?? record(requestOptions.fetchOptions);
|
|
1198
|
+
const callerSignal = requestOptions.signal ?? fetchOptions?.signal;
|
|
1199
|
+
if (callerSignal !== void 0 && callerSignal !== null && !(callerSignal instanceof AbortSignal)) return void 0;
|
|
1200
|
+
const cancellation = new AbortController();
|
|
1201
|
+
const signals = [cancellation.signal];
|
|
1202
|
+
if (callerSignal) signals.push(callerSignal);
|
|
1203
|
+
const requestTimeout = callerSignal ? void 0 : positive(requestOptions.timeout_ms) ?? positive(requestOptions.timeout);
|
|
1204
|
+
if (requestTimeout !== void 0) signals.push(AbortSignal.timeout(requestTimeout));
|
|
1205
|
+
const hopTimeout = callerSignal || requestTimeout !== void 0 ? void 0 : positive(clientTimeout);
|
|
1206
|
+
const hopTimer = hopTimeout === void 0 ? void 0 : new AbortController();
|
|
1207
|
+
if (hopTimer) signals.push(hopTimer.signal);
|
|
1208
|
+
let timer;
|
|
1209
|
+
let remaining = hopTimeout ?? 0;
|
|
1210
|
+
let resumedAt = 0;
|
|
1211
|
+
const pauseHopTimer = () => {
|
|
1212
|
+
if (timer === void 0) return;
|
|
1213
|
+
clearTimeout(timer);
|
|
1214
|
+
timer = void 0;
|
|
1215
|
+
remaining = Math.max(0, remaining - (Date.now() - resumedAt));
|
|
1216
|
+
};
|
|
1217
|
+
const resumeHopTimer = () => {
|
|
1218
|
+
if (!hopTimer || hopTimer.signal.aborted || timer !== void 0) return;
|
|
1219
|
+
resumedAt = Date.now();
|
|
1220
|
+
timer = setTimeout(() => hopTimer.abort(new DOMException("The operation was aborted due to timeout", "TimeoutError")), remaining);
|
|
1221
|
+
if (typeof timer === "object") timer.unref?.();
|
|
1222
|
+
};
|
|
1223
|
+
const resetHopTimer = () => {
|
|
1224
|
+
pauseHopTimer();
|
|
1225
|
+
remaining = hopTimeout ?? 0;
|
|
1226
|
+
};
|
|
1227
|
+
resumeHopTimer();
|
|
1228
|
+
const signal = signals.length === 1 ? cancellation.signal : AbortSignal.any(signals);
|
|
1229
|
+
return {
|
|
1230
|
+
options: {
|
|
1231
|
+
...requestOptions,
|
|
1232
|
+
signal
|
|
1233
|
+
},
|
|
1234
|
+
abort: () => cancellation.abort(),
|
|
1235
|
+
resumeHopTimer,
|
|
1236
|
+
pauseHopTimer,
|
|
1237
|
+
resetHopTimer
|
|
1238
|
+
};
|
|
1239
|
+
}
|
|
1240
|
+
const needsRequestAbort = "ContinuationInterruptedError" in genai;
|
|
1156
1241
|
function isFailure(status) {
|
|
1157
1242
|
return status === "failed" || status === "cancelled" || status === "incomplete" || status === "budget_exceeded";
|
|
1158
1243
|
}
|
|
1159
1244
|
var WrappedInteractions = class {
|
|
1160
|
-
constructor(client, phClient) {
|
|
1245
|
+
constructor(client, phClient, clientTimeout) {
|
|
1161
1246
|
this.client = client;
|
|
1162
1247
|
this.phClient = phClient;
|
|
1248
|
+
this.clientTimeout = clientTimeout;
|
|
1163
1249
|
}
|
|
1164
1250
|
async create(params, options) {
|
|
1165
1251
|
const { providerParams, posthogParams } = extractPosthogParams(params);
|
|
@@ -1195,12 +1281,16 @@ var WrappedInteractions = class {
|
|
|
1195
1281
|
});
|
|
1196
1282
|
} catch {}
|
|
1197
1283
|
};
|
|
1284
|
+
let streamAbort;
|
|
1198
1285
|
try {
|
|
1199
1286
|
if (!this.client.interactions?.create) throw new Error("Gemini Interactions requires @google/genai 2.18.0 or newer");
|
|
1200
|
-
|
|
1287
|
+
streamAbort = needsRequestAbort && requestData.stream === true && typeof AbortSignal.any === "function" ? createStreamAbort(options, this.clientTimeout) : void 0;
|
|
1288
|
+
const providerOptions = streamAbort ? streamAbort.options : options;
|
|
1289
|
+
const result = await (providerOptions === void 0 ? this.client.interactions.create(request) : this.client.interactions.create(request, providerOptions));
|
|
1201
1290
|
if (requestData.stream === true) {
|
|
1202
1291
|
if (!isStream(result)) throw new Error("Gemini Interactions did not return a stream");
|
|
1203
|
-
|
|
1292
|
+
streamAbort?.pauseHopTimer();
|
|
1293
|
+
return this.wrapStream(result, capture, streamAbort);
|
|
1204
1294
|
}
|
|
1205
1295
|
if (isStream(result)) throw new Error("Gemini Interactions returned an unexpected stream");
|
|
1206
1296
|
const interaction = record(result);
|
|
@@ -1217,11 +1307,16 @@ var WrappedInteractions = class {
|
|
|
1217
1307
|
} catch {}
|
|
1218
1308
|
return result;
|
|
1219
1309
|
} catch (error) {
|
|
1220
|
-
|
|
1310
|
+
streamAbort?.pauseHopTimer();
|
|
1311
|
+
let partialInteraction;
|
|
1312
|
+
try {
|
|
1313
|
+
partialInteraction = record(record(error)?.partialInteraction);
|
|
1314
|
+
} catch {}
|
|
1315
|
+
await capture(partialInteraction, error);
|
|
1221
1316
|
throw error;
|
|
1222
1317
|
}
|
|
1223
1318
|
}
|
|
1224
|
-
wrapStream(stream, capture) {
|
|
1319
|
+
wrapStream(stream, capture, streamAbort) {
|
|
1225
1320
|
const reader = "getReader" in stream && typeof stream.getReader === "function" ? stream.getReader() : void 0;
|
|
1226
1321
|
const iterator = reader ? void 0 : stream[Symbol.asyncIterator]();
|
|
1227
1322
|
let readerReleased = false;
|
|
@@ -1269,6 +1364,10 @@ var WrappedInteractions = class {
|
|
|
1269
1364
|
let interaction;
|
|
1270
1365
|
let latestUsage;
|
|
1271
1366
|
let finalUsage;
|
|
1367
|
+
const previousHopUsages = [];
|
|
1368
|
+
let stepOffset = 0;
|
|
1369
|
+
let hops = 1;
|
|
1370
|
+
let hopBoundary = false;
|
|
1272
1371
|
let error;
|
|
1273
1372
|
let firstTokenTime;
|
|
1274
1373
|
const steps = /* @__PURE__ */ new Map();
|
|
@@ -1276,6 +1375,10 @@ var WrappedInteractions = class {
|
|
|
1276
1375
|
let closed = false;
|
|
1277
1376
|
let cancelled = false;
|
|
1278
1377
|
let captured = false;
|
|
1378
|
+
const stepIndex = (value) => {
|
|
1379
|
+
const index = number(value);
|
|
1380
|
+
return index === void 0 ? void 0 : index + stepOffset;
|
|
1381
|
+
};
|
|
1279
1382
|
const observe = (event) => {
|
|
1280
1383
|
let schemaError;
|
|
1281
1384
|
try {
|
|
@@ -1286,18 +1389,27 @@ var WrappedInteractions = class {
|
|
|
1286
1389
|
const usage = record(record(item?.metadata)?.total_usage) ?? (eventType === "step.stop" ? record(item?.usage) : void 0);
|
|
1287
1390
|
if (usage) latestUsage = { ...usage };
|
|
1288
1391
|
if (eventType === "interaction.created") {
|
|
1392
|
+
if (interaction?.status === "requires_continuation") {
|
|
1393
|
+
hops++;
|
|
1394
|
+
if (finalUsage) previousHopUsages.push(finalUsage);
|
|
1395
|
+
finalUsage = void 0;
|
|
1396
|
+
latestUsage = void 0;
|
|
1397
|
+
stepOffset = steps.size === 0 ? 0 : Math.max(...steps.keys()) + 1;
|
|
1398
|
+
completed = false;
|
|
1399
|
+
error = void 0;
|
|
1400
|
+
}
|
|
1289
1401
|
interaction = { ...record(item?.interaction) };
|
|
1290
1402
|
const initialUsage = record(interaction.usage);
|
|
1291
1403
|
if (initialUsage) latestUsage = { ...initialUsage };
|
|
1292
1404
|
} else if (eventType === "step.start") {
|
|
1293
|
-
const index =
|
|
1405
|
+
const index = stepIndex(item?.index);
|
|
1294
1406
|
const step = record(item?.step);
|
|
1295
1407
|
if (index !== void 0) steps.set(index, {
|
|
1296
1408
|
...step,
|
|
1297
1409
|
content: Array.isArray(step?.content) ? step.content.map((item) => ({ ...record(item) })) : []
|
|
1298
1410
|
});
|
|
1299
1411
|
} else if (eventType === "step.delta") {
|
|
1300
|
-
const index =
|
|
1412
|
+
const index = stepIndex(item?.index);
|
|
1301
1413
|
const delta = record(item?.delta);
|
|
1302
1414
|
const step = index === void 0 ? void 0 : steps.get(index);
|
|
1303
1415
|
if (step && delta?.type === "text" && typeof delta.text === "string") {
|
|
@@ -1330,6 +1442,7 @@ var WrappedInteractions = class {
|
|
|
1330
1442
|
}
|
|
1331
1443
|
if (isFailure(interaction.status)) error = /* @__PURE__ */ new Error(`Gemini interaction ${interaction.status}`);
|
|
1332
1444
|
if (!completed) error = /* @__PURE__ */ new Error("Gemini interaction stream ended without a terminal result");
|
|
1445
|
+
hopBoundary = interaction.status === "requires_continuation";
|
|
1333
1446
|
} else if (eventType === "error") error = /* @__PURE__ */ new Error("Gemini interaction stream failed");
|
|
1334
1447
|
}
|
|
1335
1448
|
} catch {}
|
|
@@ -1338,15 +1451,17 @@ var WrappedInteractions = class {
|
|
|
1338
1451
|
const finish = async (status, providerError) => {
|
|
1339
1452
|
if (captured) return;
|
|
1340
1453
|
captured = true;
|
|
1454
|
+
streamAbort?.pauseHopTimer();
|
|
1341
1455
|
try {
|
|
1342
1456
|
const streamedSteps = [...steps.entries()].sort(([left], [right]) => left - right).map(([, step]) => ({
|
|
1343
1457
|
...step,
|
|
1344
1458
|
arguments: step.argumentChunks === void 0 ? step.arguments : functionArguments(step.argumentChunks)
|
|
1345
1459
|
}));
|
|
1346
|
-
const output = outputFromSteps(Array.isArray(interaction?.steps) && interaction.steps.length > 0 ? interaction.steps : streamedSteps);
|
|
1460
|
+
const output = outputFromSteps(hops === 1 && Array.isArray(interaction?.steps) && interaction.steps.length > 0 ? interaction.steps : streamedSteps);
|
|
1461
|
+
const hopUsage = finalUsage ?? latestUsage;
|
|
1347
1462
|
await capture({
|
|
1348
1463
|
...interaction,
|
|
1349
|
-
usage:
|
|
1464
|
+
usage: mergeHopUsage(hopUsage ? [...previousHopUsages, hopUsage] : previousHopUsages),
|
|
1350
1465
|
...completed ? {} : { status }
|
|
1351
1466
|
}, providerError ?? error ?? (!completed && status === "incomplete" ? /* @__PURE__ */ new Error("Gemini interaction stream ended without completion") : void 0), output, firstTokenTime);
|
|
1352
1467
|
} catch {}
|
|
@@ -1354,7 +1469,17 @@ var WrappedInteractions = class {
|
|
|
1354
1469
|
return new ReadableStream({
|
|
1355
1470
|
async pull(controller) {
|
|
1356
1471
|
try {
|
|
1357
|
-
|
|
1472
|
+
if (hopBoundary) {
|
|
1473
|
+
hopBoundary = false;
|
|
1474
|
+
streamAbort?.resetHopTimer();
|
|
1475
|
+
}
|
|
1476
|
+
streamAbort?.resumeHopTimer();
|
|
1477
|
+
let result;
|
|
1478
|
+
try {
|
|
1479
|
+
result = await next();
|
|
1480
|
+
} finally {
|
|
1481
|
+
streamAbort?.pauseHopTimer();
|
|
1482
|
+
}
|
|
1358
1483
|
if (closed) return;
|
|
1359
1484
|
if (result.done) {
|
|
1360
1485
|
closed = true;
|
|
@@ -1382,7 +1507,9 @@ var WrappedInteractions = class {
|
|
|
1382
1507
|
closed = true;
|
|
1383
1508
|
let cancellationError;
|
|
1384
1509
|
try {
|
|
1385
|
-
|
|
1510
|
+
const cancelling = cancelSource(reason);
|
|
1511
|
+
streamAbort?.abort();
|
|
1512
|
+
await cancelling;
|
|
1386
1513
|
} catch (providerError) {
|
|
1387
1514
|
cancellationError = providerError;
|
|
1388
1515
|
throw providerError;
|
|
@@ -1423,7 +1550,7 @@ var PostHogGoogleGenAI = class {
|
|
|
1423
1550
|
this.phClient = posthog;
|
|
1424
1551
|
this.client = new GoogleGenAI(geminiConfig);
|
|
1425
1552
|
this.models = new WrappedModels(this.client, this.phClient);
|
|
1426
|
-
this.interactions = new WrappedInteractions(this.client, this.phClient);
|
|
1553
|
+
this.interactions = new WrappedInteractions(this.client, this.phClient, geminiConfig.httpOptions?.timeout);
|
|
1427
1554
|
}
|
|
1428
1555
|
};
|
|
1429
1556
|
var WrappedModels = class {
|