@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.cjs CHANGED
@@ -1235,77 +1235,75 @@ const mapVercelPrompt = messages => {
1235
1235
  return inputs;
1236
1236
  };
1237
1237
  const mapVercelOutput = result => {
1238
- const content = [];
1239
- if (result.text) {
1240
- content.push({
1241
- type: 'text',
1242
- text: truncate(result.text)
1243
- });
1244
- }
1245
- if (result.toolCalls && Array.isArray(result.toolCalls)) {
1246
- for (const toolCall of result.toolCalls) {
1247
- content.push({
1248
- type: 'function',
1249
- id: toolCall.toolCallId,
1238
+ const content = result.map(item => {
1239
+ if (item.type === 'text') {
1240
+ return {
1241
+ type: 'text',
1242
+ text: truncate(item.text)
1243
+ };
1244
+ }
1245
+ if (item.type === 'tool-call') {
1246
+ return {
1247
+ type: 'tool-call',
1248
+ id: item.toolCallId,
1250
1249
  function: {
1251
- name: toolCall.toolName,
1252
- arguments: typeof toolCall.args === 'string' ? toolCall.args : JSON.stringify(toolCall.args)
1250
+ name: item.toolName,
1251
+ arguments: item.args || JSON.stringify(item.arguments || {})
1253
1252
  }
1254
- });
1253
+ };
1255
1254
  }
1256
- }
1255
+ if (item.type === 'reasoning') {
1256
+ return {
1257
+ type: 'reasoning',
1258
+ text: truncate(item.text)
1259
+ };
1260
+ }
1261
+ if (item.type === 'file') {
1262
+ // Handle files similar to input mapping - avoid large base64 data
1263
+ let fileData;
1264
+ if (item.data instanceof URL) {
1265
+ fileData = item.data.toString();
1266
+ } else if (typeof item.data === 'string') {
1267
+ // Check if it's base64 data and potentially large
1268
+ if (item.data.startsWith('data:') || item.data.length > 1000) {
1269
+ fileData = `[${item.mediaType} file - ${item.data.length} bytes]`;
1270
+ } else {
1271
+ fileData = item.data;
1272
+ }
1273
+ } else {
1274
+ fileData = `[binary ${item.mediaType} file]`;
1275
+ }
1276
+ return {
1277
+ type: 'file',
1278
+ name: 'generated_file',
1279
+ mediaType: item.mediaType,
1280
+ data: fileData
1281
+ };
1282
+ }
1283
+ if (item.type === 'source') {
1284
+ return {
1285
+ type: 'source',
1286
+ sourceType: item.sourceType,
1287
+ id: item.id,
1288
+ url: item.url || '',
1289
+ title: item.title || ''
1290
+ };
1291
+ }
1292
+ // Fallback for unknown types - try to extract text if possible
1293
+ return {
1294
+ type: 'text',
1295
+ text: truncate(JSON.stringify(item))
1296
+ };
1297
+ });
1257
1298
  if (content.length > 0) {
1258
1299
  return [{
1259
1300
  role: 'assistant',
1260
1301
  content: content.length === 1 && content[0].type === 'text' ? content[0].text : content
1261
1302
  }];
1262
1303
  }
1263
- // Fallback to original behavior for other result types TODO: check if we can remove this
1264
- const normalizedResult = typeof result === 'string' ? {
1265
- text: result
1266
- } : result;
1267
- const output = {
1268
- ...(normalizedResult.text ? {
1269
- text: normalizedResult.text
1270
- } : {}),
1271
- ...(normalizedResult.object ? {
1272
- object: normalizedResult.object
1273
- } : {}),
1274
- ...(normalizedResult.reasoningText ? {
1275
- reasoning: normalizedResult.reasoningText
1276
- } : {}),
1277
- ...(normalizedResult.response ? {
1278
- response: normalizedResult.response
1279
- } : {}),
1280
- ...(normalizedResult.finishReason ? {
1281
- finishReason: normalizedResult.finishReason
1282
- } : {}),
1283
- ...(normalizedResult.usage ? {
1284
- usage: normalizedResult.usage
1285
- } : {}),
1286
- ...(normalizedResult.warnings ? {
1287
- warnings: normalizedResult.warnings
1288
- } : {}),
1289
- ...(normalizedResult.providerMetadata ? {
1290
- toolCalls: normalizedResult.providerMetadata
1291
- } : {}),
1292
- ...(normalizedResult.files ? {
1293
- files: normalizedResult.files.map(file => ({
1294
- name: file.name,
1295
- size: file.size,
1296
- type: file.type
1297
- }))
1298
- } : {})
1299
- };
1300
- if (output.text && !output.object && !output.reasoning) {
1301
- return [{
1302
- content: truncate(output.text),
1303
- role: 'assistant'
1304
- }];
1305
- }
1306
1304
  // otherwise stringify and truncate
1307
1305
  try {
1308
- const jsonOutput = JSON.stringify(output);
1306
+ const jsonOutput = JSON.stringify(result);
1309
1307
  return [{
1310
1308
  content: truncate(jsonOutput),
1311
1309
  role: 'assistant'
@@ -1337,7 +1335,7 @@ const createInstrumentationMiddleware = (phClient, model, options) => {
1337
1335
  const modelId = options.posthogModelOverride ?? (result.response?.modelId ? result.response.modelId : model.modelId);
1338
1336
  const provider = options.posthogProviderOverride ?? extractProvider(model);
1339
1337
  const baseURL = ''; // cannot currently get baseURL from vercel
1340
- const content = mapVercelOutput(result);
1338
+ const content = mapVercelOutput(result.content);
1341
1339
  const latency = (Date.now() - startTime) / 1000;
1342
1340
  const providerMetadata = result.providerMetadata;
1343
1341
  const additionalTokenValues = {
@@ -1451,7 +1449,25 @@ const createInstrumentationMiddleware = (phClient, model, options) => {
1451
1449
  },
1452
1450
  flush: async () => {
1453
1451
  const latency = (Date.now() - startTime) / 1000;
1454
- const outputContent = reasoningText ? `${reasoningText}\n\n${generatedText}` : generatedText;
1452
+ // Build content array similar to mapVercelOutput structure
1453
+ const content = [];
1454
+ if (reasoningText) {
1455
+ content.push({
1456
+ type: 'reasoning',
1457
+ text: truncate(reasoningText)
1458
+ });
1459
+ }
1460
+ if (generatedText) {
1461
+ content.push({
1462
+ type: 'text',
1463
+ text: truncate(generatedText)
1464
+ });
1465
+ }
1466
+ // Structure output like mapVercelOutput does
1467
+ const output = content.length > 0 ? [{
1468
+ role: 'assistant',
1469
+ content: content.length === 1 && content[0].type === 'text' ? content[0].text : content
1470
+ }] : [];
1455
1471
  await sendEventToPosthog({
1456
1472
  client: phClient,
1457
1473
  distinctId: options.posthogDistinctId,
@@ -1459,10 +1475,7 @@ const createInstrumentationMiddleware = (phClient, model, options) => {
1459
1475
  model: modelId,
1460
1476
  provider: provider,
1461
1477
  input: options.posthogPrivacyMode ? '' : mapVercelPrompt(params.prompt),
1462
- output: [{
1463
- content: outputContent,
1464
- role: 'assistant'
1465
- }],
1478
+ output: output,
1466
1479
  latency,
1467
1480
  baseURL,
1468
1481
  params: mergedParams,