@posthog/ai 8.18.0 → 8.18.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.
Files changed (45) hide show
  1. package/dist/adk/index.cjs +8 -9
  2. package/dist/adk/index.cjs.map +1 -1
  3. package/dist/adk/index.mjs +8 -9
  4. package/dist/adk/index.mjs.map +1 -1
  5. package/dist/anthropic/index.cjs +1 -1
  6. package/dist/anthropic/index.cjs.map +1 -1
  7. package/dist/anthropic/index.mjs +1 -1
  8. package/dist/anthropic/index.mjs.map +1 -1
  9. package/dist/claude-agent-sdk/index.cjs +1 -1
  10. package/dist/claude-agent-sdk/index.cjs.map +1 -1
  11. package/dist/claude-agent-sdk/index.mjs +1 -1
  12. package/dist/claude-agent-sdk/index.mjs.map +1 -1
  13. package/dist/gemini/index.cjs +8 -9
  14. package/dist/gemini/index.cjs.map +1 -1
  15. package/dist/gemini/index.mjs +8 -9
  16. package/dist/gemini/index.mjs.map +1 -1
  17. package/dist/genkit/index.cjs +8 -9
  18. package/dist/genkit/index.cjs.map +1 -1
  19. package/dist/genkit/index.mjs +8 -9
  20. package/dist/genkit/index.mjs.map +1 -1
  21. package/dist/index.cjs +54 -20
  22. package/dist/index.cjs.map +1 -1
  23. package/dist/index.mjs +54 -20
  24. package/dist/index.mjs.map +1 -1
  25. package/dist/langchain/index.cjs +1 -1
  26. package/dist/langchain/index.cjs.map +1 -1
  27. package/dist/langchain/index.mjs +1 -1
  28. package/dist/langchain/index.mjs.map +1 -1
  29. package/dist/langchain/middleware/index.cjs +1 -1
  30. package/dist/langchain/middleware/index.cjs.map +1 -1
  31. package/dist/langchain/middleware/index.mjs +1 -1
  32. package/dist/langchain/middleware/index.mjs.map +1 -1
  33. package/dist/openai/index.cjs +1 -1
  34. package/dist/openai/index.cjs.map +1 -1
  35. package/dist/openai/index.mjs +1 -1
  36. package/dist/openai/index.mjs.map +1 -1
  37. package/dist/openai-agents/index.cjs +1 -1
  38. package/dist/openai-agents/index.cjs.map +1 -1
  39. package/dist/openai-agents/index.mjs +1 -1
  40. package/dist/openai-agents/index.mjs.map +1 -1
  41. package/dist/vercel/index.cjs +54 -20
  42. package/dist/vercel/index.cjs.map +1 -1
  43. package/dist/vercel/index.mjs +54 -20
  44. package/dist/vercel/index.mjs.map +1 -1
  45. package/package.json +2 -2
package/dist/index.cjs CHANGED
@@ -302,6 +302,12 @@ const getModelParams = (params, responseServiceTier) => {
302
302
  if (responseServiceTier != null) modelParams.service_tier = responseServiceTier;
303
303
  return modelParams;
304
304
  };
305
+ const uint8ArrayToBase64 = (data) => {
306
+ if (typeof Buffer !== "undefined") return Buffer.from(data).toString("base64");
307
+ let binary = "";
308
+ for (let i = 0; i < data.length; i++) binary += String.fromCharCode(data[i]);
309
+ return btoa(binary);
310
+ };
305
311
  function toSafeString(input) {
306
312
  if (input === void 0 || input === null) return "";
307
313
  if (typeof input === "string") return input;
@@ -413,7 +419,7 @@ function sanitizeValues(obj) {
413
419
  }
414
420
  //#endregion
415
421
  //#region package.json
416
- var version = "8.18.0";
422
+ var version = "8.18.1";
417
423
  //#endregion
418
424
  //#region src/serializeError.ts
419
425
  const DEFAULT_MAX_DEPTH = 3;
@@ -719,9 +725,38 @@ function isV3Model(model) {
719
725
  function getSpecificationVersion(model) {
720
726
  if (typeof model === "object" && model !== null && "specificationVersion" in model) return model.specificationVersion;
721
727
  }
722
- const redactFileData = (data, mediaType, client) => {
723
- if (data instanceof URL) return isFullAiCaptureEnabled(client) ? data.toString() : redactBase64DataUrl(data.toString(), data.protocol === "data:" ? mediaType : void 0);
724
- if (isString(data)) return isFullAiCaptureEnabled(client) ? data : redactBase64DataUrl(data, mediaType);
728
+ const dataUrlMediaType = (mediaType) => mediaType && /^[\w.+-]+\/[\w.+-]+$/.test(mediaType) ? mediaType : "application/octet-stream";
729
+ const MAX_EVENT_FILE_BYTES = 7864320;
730
+ const newFileBudget = () => ({ remaining: MAX_EVENT_FILE_BYTES });
731
+ const fitsBudget = (length, budget) => {
732
+ if (length > budget.remaining) return false;
733
+ budget.remaining -= length;
734
+ return true;
735
+ };
736
+ const fullCaptureDataUrl = (dataUrl, budget, mediaType) => fitsBudget(dataUrl.length, budget) ? dataUrl : String(redactBase64DataUrl(dataUrl, mediaType));
737
+ const redactFileData = (data, mediaType, client, budget) => {
738
+ if (data instanceof Uint8Array) {
739
+ if (!isFullAiCaptureEnabled(client)) return String(redactBase64DataUrl(data, mediaType));
740
+ const prefix = `data:${dataUrlMediaType(mediaType)};base64,`;
741
+ if (!fitsBudget(prefix.length + Math.ceil(data.length / 3) * 4, budget)) return `[base64 ${mediaType} redacted]`;
742
+ return prefix + uint8ArrayToBase64(data);
743
+ }
744
+ if (data instanceof URL) return isFullAiCaptureEnabled(client) ? fullCaptureDataUrl(data.toString(), budget, mediaType) : redactBase64DataUrl(data.toString(), data.protocol === "data:" ? mediaType : void 0);
745
+ if (isString(data)) {
746
+ if (!isFullAiCaptureEnabled(client)) return redactBase64DataUrl(data, mediaType);
747
+ if (/^(data:|https?:\/\/)/i.test(data)) return fullCaptureDataUrl(data, budget, mediaType);
748
+ return fullCaptureDataUrl(`data:${dataUrlMediaType(mediaType)};base64,${data}`, budget, mediaType);
749
+ }
750
+ };
751
+ const mapOutputFile = (item, client, budget) => {
752
+ let fileData = redactFileData(item.data, item.mediaType, client, budget) ?? `[binary ${item.mediaType} file]`;
753
+ if (!isFullAiCaptureEnabled(client) && typeof item.data === "string" && fileData === item.data && item.data.length > 1e3) fileData = `[${item.mediaType} file - ${item.data.length} bytes]`;
754
+ return {
755
+ type: "file",
756
+ name: "generated_file",
757
+ mediaType: item.mediaType,
758
+ data: fileData
759
+ };
725
760
  };
726
761
  const mapVercelParams = (params) => {
727
762
  return {
@@ -734,7 +769,7 @@ const mapVercelParams = (params) => {
734
769
  stream: params.stream
735
770
  };
736
771
  };
737
- const mapVercelPrompt = (messages, client) => {
772
+ const mapVercelPrompt = (messages, client, budget = newFileBudget()) => {
738
773
  const inputs = messages.map((message) => {
739
774
  let content;
740
775
  if (message.role === "system") content = [{
@@ -748,7 +783,7 @@ const mapVercelPrompt = (messages, client) => {
748
783
  };
749
784
  else if (c.type === "file") return {
750
785
  type: "file",
751
- file: redactFileData(c.data, c.mediaType, client) ?? "raw files not supported",
786
+ file: redactFileData(c.data, c.mediaType, client, budget) ?? "raw files not supported",
752
787
  mediaType: c.mediaType
753
788
  };
754
789
  else if (c.type === "reasoning") return {
@@ -809,7 +844,7 @@ const mapVercelPrompt = (messages, client) => {
809
844
  }
810
845
  return inputs;
811
846
  };
812
- const mapVercelOutput = (result, client) => {
847
+ const mapVercelOutput = (result, client, budget) => {
813
848
  const content = result.map((item) => {
814
849
  if (item.type === "text") return {
815
850
  type: "text",
@@ -831,16 +866,7 @@ const mapVercelOutput = (result, client) => {
831
866
  type: "reasoning",
832
867
  text: truncate(item.text, client)
833
868
  };
834
- if (item.type === "file") {
835
- let fileData = redactFileData(item.data, item.mediaType, client) ?? `[binary ${item.mediaType} file]`;
836
- if (!isFullAiCaptureEnabled(client) && typeof item.data === "string" && fileData === item.data && item.data.length > 1e3) fileData = `[${item.mediaType} file - ${item.data.length} bytes]`;
837
- return {
838
- type: "file",
839
- name: "generated_file",
840
- mediaType: item.mediaType,
841
- data: fileData
842
- };
843
- }
869
+ if (item.type === "file") return mapOutputFile(item, client, budget);
844
870
  if (item.type === "source") return {
845
871
  type: "source",
846
872
  sourceType: item.sourceType,
@@ -980,7 +1006,8 @@ const wrapVercelLanguageModel = (model, phClient, options) => {
980
1006
  const result = await model.doGenerate(params);
981
1007
  const modelId = mergedOptions.posthogModelOverride ?? (result.response?.modelId ? result.response.modelId : model.modelId);
982
1008
  const provider = mergedOptions.posthogProviderOverride ?? extractProvider(model);
983
- const content = mapVercelOutput(result.content ?? [], phClient);
1009
+ const fileBudget = newFileBudget();
1010
+ const content = mapVercelOutput(result.content ?? [], phClient, fileBudget);
984
1011
  const latency = (Date.now() - startTime) / 1e3;
985
1012
  const providerMetadata = result.providerMetadata;
986
1013
  const additionalTokenValues = extractAdditionalTokenValues(providerMetadata, result.usage);
@@ -1010,7 +1037,7 @@ const wrapVercelLanguageModel = (model, phClient, options) => {
1010
1037
  ...baseOptions,
1011
1038
  model: modelId,
1012
1039
  provider,
1013
- input: privacyMode ? "" : mapVercelPrompt(params.prompt, phClient),
1040
+ input: privacyMode ? "" : mapVercelPrompt(params.prompt, phClient, fileBudget),
1014
1041
  output: content,
1015
1042
  latency,
1016
1043
  baseURL,
@@ -1060,6 +1087,8 @@ const wrapVercelLanguageModel = (model, phClient, options) => {
1060
1087
  const provider = mergedOptions.posthogProviderOverride ?? extractProvider(model);
1061
1088
  const availableTools = extractAvailableToolCalls("vercel", params);
1062
1089
  const baseURL = extractBaseURL(model);
1090
+ const fileBudget = newFileBudget();
1091
+ const generatedFiles = [];
1063
1092
  const toolCallsInProgress = /* @__PURE__ */ new Map();
1064
1093
  const captureStreamGeneration = async (captureOptions) => {
1065
1094
  try {
@@ -1103,6 +1132,10 @@ const wrapVercelLanguageModel = (model, phClient, options) => {
1103
1132
  input: chunk.input
1104
1133
  });
1105
1134
  }
1135
+ if (chunk.type === "file") {
1136
+ if (firstTokenTime === void 0) firstTokenTime = Date.now();
1137
+ if (!privacyMode) generatedFiles.push(mapOutputFile(chunk, phClient, fileBudget));
1138
+ }
1106
1139
  if (chunk.type === "error") {
1107
1140
  hasInBandError = true;
1108
1141
  inBandError = chunk.error;
@@ -1145,6 +1178,7 @@ const wrapVercelLanguageModel = (model, phClient, options) => {
1145
1178
  arguments: toolCall.input
1146
1179
  }
1147
1180
  });
1181
+ content.push(...generatedFiles);
1148
1182
  const output = content.length > 0 ? [{
1149
1183
  role: "assistant",
1150
1184
  content: content.length === 1 && content[0].type === "text" ? content[0].text : content
@@ -1164,7 +1198,7 @@ const wrapVercelLanguageModel = (model, phClient, options) => {
1164
1198
  ...baseOptions,
1165
1199
  model: modelId,
1166
1200
  provider,
1167
- input: privacyMode ? "" : mapVercelPrompt(params.prompt, phClient),
1201
+ input: privacyMode ? "" : mapVercelPrompt(params.prompt, phClient, fileBudget),
1168
1202
  output,
1169
1203
  latency,
1170
1204
  timeToFirstToken,