@posthog/ai 6.0.0 → 6.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/dist/index.mjs CHANGED
@@ -1215,77 +1215,75 @@ const mapVercelPrompt = messages => {
1215
1215
  return inputs;
1216
1216
  };
1217
1217
  const mapVercelOutput = result => {
1218
- const content = [];
1219
- if (result.text) {
1220
- content.push({
1221
- type: 'text',
1222
- text: truncate(result.text)
1223
- });
1224
- }
1225
- if (result.toolCalls && Array.isArray(result.toolCalls)) {
1226
- for (const toolCall of result.toolCalls) {
1227
- content.push({
1228
- type: 'function',
1229
- id: toolCall.toolCallId,
1218
+ const content = result.map(item => {
1219
+ if (item.type === 'text') {
1220
+ return {
1221
+ type: 'text',
1222
+ text: truncate(item.text)
1223
+ };
1224
+ }
1225
+ if (item.type === 'tool-call') {
1226
+ return {
1227
+ type: 'tool-call',
1228
+ id: item.toolCallId,
1230
1229
  function: {
1231
- name: toolCall.toolName,
1232
- arguments: typeof toolCall.args === 'string' ? toolCall.args : JSON.stringify(toolCall.args)
1230
+ name: item.toolName,
1231
+ arguments: item.args || JSON.stringify(item.arguments || {})
1233
1232
  }
1234
- });
1233
+ };
1235
1234
  }
1236
- }
1235
+ if (item.type === 'reasoning') {
1236
+ return {
1237
+ type: 'reasoning',
1238
+ text: truncate(item.text)
1239
+ };
1240
+ }
1241
+ if (item.type === 'file') {
1242
+ // Handle files similar to input mapping - avoid large base64 data
1243
+ let fileData;
1244
+ if (item.data instanceof URL) {
1245
+ fileData = item.data.toString();
1246
+ } else if (typeof item.data === 'string') {
1247
+ // Check if it's base64 data and potentially large
1248
+ if (item.data.startsWith('data:') || item.data.length > 1000) {
1249
+ fileData = `[${item.mediaType} file - ${item.data.length} bytes]`;
1250
+ } else {
1251
+ fileData = item.data;
1252
+ }
1253
+ } else {
1254
+ fileData = `[binary ${item.mediaType} file]`;
1255
+ }
1256
+ return {
1257
+ type: 'file',
1258
+ name: 'generated_file',
1259
+ mediaType: item.mediaType,
1260
+ data: fileData
1261
+ };
1262
+ }
1263
+ if (item.type === 'source') {
1264
+ return {
1265
+ type: 'source',
1266
+ sourceType: item.sourceType,
1267
+ id: item.id,
1268
+ url: item.url || '',
1269
+ title: item.title || ''
1270
+ };
1271
+ }
1272
+ // Fallback for unknown types - try to extract text if possible
1273
+ return {
1274
+ type: 'text',
1275
+ text: truncate(JSON.stringify(item))
1276
+ };
1277
+ });
1237
1278
  if (content.length > 0) {
1238
1279
  return [{
1239
1280
  role: 'assistant',
1240
1281
  content: content.length === 1 && content[0].type === 'text' ? content[0].text : content
1241
1282
  }];
1242
1283
  }
1243
- // Fallback to original behavior for other result types TODO: check if we can remove this
1244
- const normalizedResult = typeof result === 'string' ? {
1245
- text: result
1246
- } : result;
1247
- const output = {
1248
- ...(normalizedResult.text ? {
1249
- text: normalizedResult.text
1250
- } : {}),
1251
- ...(normalizedResult.object ? {
1252
- object: normalizedResult.object
1253
- } : {}),
1254
- ...(normalizedResult.reasoningText ? {
1255
- reasoning: normalizedResult.reasoningText
1256
- } : {}),
1257
- ...(normalizedResult.response ? {
1258
- response: normalizedResult.response
1259
- } : {}),
1260
- ...(normalizedResult.finishReason ? {
1261
- finishReason: normalizedResult.finishReason
1262
- } : {}),
1263
- ...(normalizedResult.usage ? {
1264
- usage: normalizedResult.usage
1265
- } : {}),
1266
- ...(normalizedResult.warnings ? {
1267
- warnings: normalizedResult.warnings
1268
- } : {}),
1269
- ...(normalizedResult.providerMetadata ? {
1270
- toolCalls: normalizedResult.providerMetadata
1271
- } : {}),
1272
- ...(normalizedResult.files ? {
1273
- files: normalizedResult.files.map(file => ({
1274
- name: file.name,
1275
- size: file.size,
1276
- type: file.type
1277
- }))
1278
- } : {})
1279
- };
1280
- if (output.text && !output.object && !output.reasoning) {
1281
- return [{
1282
- content: truncate(output.text),
1283
- role: 'assistant'
1284
- }];
1285
- }
1286
1284
  // otherwise stringify and truncate
1287
1285
  try {
1288
- const jsonOutput = JSON.stringify(output);
1286
+ const jsonOutput = JSON.stringify(result);
1289
1287
  return [{
1290
1288
  content: truncate(jsonOutput),
1291
1289
  role: 'assistant'
@@ -1317,7 +1315,7 @@ const createInstrumentationMiddleware = (phClient, model, options) => {
1317
1315
  const modelId = options.posthogModelOverride ?? (result.response?.modelId ? result.response.modelId : model.modelId);
1318
1316
  const provider = options.posthogProviderOverride ?? extractProvider(model);
1319
1317
  const baseURL = ''; // cannot currently get baseURL from vercel
1320
- const content = mapVercelOutput(result);
1318
+ const content = mapVercelOutput(result.content);
1321
1319
  const latency = (Date.now() - startTime) / 1000;
1322
1320
  const providerMetadata = result.providerMetadata;
1323
1321
  const additionalTokenValues = {
@@ -1431,7 +1429,25 @@ const createInstrumentationMiddleware = (phClient, model, options) => {
1431
1429
  },
1432
1430
  flush: async () => {
1433
1431
  const latency = (Date.now() - startTime) / 1000;
1434
- const outputContent = reasoningText ? `${reasoningText}\n\n${generatedText}` : generatedText;
1432
+ // Build content array similar to mapVercelOutput structure
1433
+ const content = [];
1434
+ if (reasoningText) {
1435
+ content.push({
1436
+ type: 'reasoning',
1437
+ text: truncate(reasoningText)
1438
+ });
1439
+ }
1440
+ if (generatedText) {
1441
+ content.push({
1442
+ type: 'text',
1443
+ text: truncate(generatedText)
1444
+ });
1445
+ }
1446
+ // Structure output like mapVercelOutput does
1447
+ const output = content.length > 0 ? [{
1448
+ role: 'assistant',
1449
+ content: content.length === 1 && content[0].type === 'text' ? content[0].text : content
1450
+ }] : [];
1435
1451
  await sendEventToPosthog({
1436
1452
  client: phClient,
1437
1453
  distinctId: options.posthogDistinctId,
@@ -1439,10 +1455,7 @@ const createInstrumentationMiddleware = (phClient, model, options) => {
1439
1455
  model: modelId,
1440
1456
  provider: provider,
1441
1457
  input: options.posthogPrivacyMode ? '' : mapVercelPrompt(params.prompt),
1442
- output: [{
1443
- content: outputContent,
1444
- role: 'assistant'
1445
- }],
1458
+ output: output,
1446
1459
  latency,
1447
1460
  baseURL,
1448
1461
  params: mergedParams,