Files
insights-js/packages/ai/rollup.config.mjs
Andrew MaguireandGitHub 28ea1890c1 feat(ai): add OpenAI Agents SDK tracing support (#3377)
* feat(ai): add OpenAI Agents SDK tracing support

Add PostHogTracingProcessor implementing the TracingProcessor interface
from @openai/agents-core to capture agent traces, spans, and LLM
generations as PostHog LLM analytics events.

Supports all span types: generation, response, function/tool, agent,
handoff, guardrail, custom, transcription, speech, speech_group, and
MCP tools. Includes privacy mode, personless mode, error categorization,
latency tracking, and an `instrument()` one-liner for easy setup.

Available as `@posthog/ai/openai-agents`.

* fix(ai): normalize input roles for OpenAI Agents SDK response spans

Add role fields to function_call (role=assistant) and function_call_result
(role=tool) items in $ai_input. The OpenAI Responses API format omits
roles on these items, causing PostHog's trace viewer to default them to
"user" messages.

* fix: lint errors in openai-agents processor and tests

* fix: address PR review feedback

- Reverse property spread order so built-in $ai_* properties can't be
  overridden by user properties
- Remove eval() from examples, replace with safe stubs
- Switch instrument() from require() to dynamic import() for ESM compat
- Forward speech_group input to event properties
- Collapse error categorization tests into it.each

* fix: address P1/P2 review findings

- Peer dep is now @openai/agents (what users install), runtime import
  stays on @openai/agents-core (available transitively, lighter init)
- Examples now await instrument() to prevent race with first run()
- Updated JSDoc to show await pattern

* refactor(ai): align openai-agents processor with SDK patterns

- Add $ai_lib and $ai_lib_version to all events (matches other integrations)
- Use shared withPrivacyMode from utils.ts instead of private duplicate

* fix(ai): address openai agents review feedback
2026-04-14 11:49:00 +01:00

78 lines
1.9 KiB
JavaScript

import { external, babel, resolve, commonjs, json, dts, plugins } from '@hanzo/insights-tooling-rollup-utils'
import packageJson from './package.json' with { type: 'json' }
const configs = []
const extensions = ['.js', '.jsx', '.ts', '.tsx']
const externalDeps = external(packageJson)
configs.push({
input: `./src/index.ts`,
output: [
{
file: packageJson.main,
sourcemap: true,
exports: 'named',
format: `cjs`,
interop: 'auto',
},
{
file: packageJson.module,
sourcemap: true,
format: `es`,
},
],
external: externalDeps,
plugins: plugins(extensions),
})
configs.push({
input: `./src/index.ts`,
output: [{ file: `./dist/index.d.ts`, format: 'es' }],
external: externalDeps,
plugins: [resolve({ extensions }), dts()],
})
// Add submodule builds for insights-ai
const providers = ['anthropic', 'openai', 'vercel', 'langchain', 'gemini', 'otel']
providers.forEach((provider) => {
configs.push({
input: `./src/${provider}/index.ts`,
output: [
{
file: `./dist/${provider}/index.cjs`,
sourcemap: true,
exports: 'named',
format: 'cjs',
interop: 'auto',
},
{
file: `./dist/${provider}/index.mjs`,
sourcemap: true,
format: 'es',
},
],
external: externalDeps,
plugins: [
resolve({ extensions }),
commonjs(),
json(),
babel({
extensions,
babelHelpers: 'bundled',
include: ['./src/**/*.{js,jsx,ts,tsx}'],
presets: [['@babel/preset-env', { targets: { node: '20.0' } }], '@babel/preset-typescript'],
}),
],
})
configs.push({
input: `./src/${provider}/index.ts`,
output: [{ file: `./dist/${provider}/index.d.ts`, format: 'es' }],
external: externalDeps,
plugins: [resolve({ extensions }), dts()],
})
})
export default configs