@posthog/ai 7.16.15 → 7.17.0

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.d.ts CHANGED
@@ -6,7 +6,7 @@ import { ResponseCreateParamsWithTools, ExtractParsedContentFromParams } from 'o
6
6
  import { LanguageModelV2, LanguageModelV3 } from '@ai-sdk/provider';
7
7
  import AnthropicOriginal, { APIPromise as APIPromise$1 } from '@anthropic-ai/sdk';
8
8
  import { Stream as Stream$1 } from '@anthropic-ai/sdk/streaming';
9
- import { GoogleGenAI, GenerateContentParameters, GenerateContentResponse, EmbedContentParameters, EmbedContentResponse, GoogleGenAIOptions } from '@google/genai';
9
+ import { GoogleGenAI, GenerateContentParameters, GenerateContentResponse, EmbedContentParameters, EmbedContentResponse, GoogleGenAIOptions, Tool } from '@google/genai';
10
10
  import { BaseCallbackHandler } from '@langchain/core/callbacks/base';
11
11
  import { Serialized } from '@langchain/core/load/serializable';
12
12
  import { ChainValues } from '@langchain/core/utils/types';
@@ -14,7 +14,20 @@ import { LLMResult } from '@langchain/core/outputs';
14
14
  import { AgentAction, AgentFinish } from '@langchain/core/agents';
15
15
  import { DocumentInterface } from '@langchain/core/documents';
16
16
  import { BaseMessage } from '@langchain/core/messages';
17
+ import { ChatCompletionTool } from 'openai/resources/chat/completions';
17
18
 
19
+ /**
20
+ * Token usage information for AI model responses
21
+ */
22
+ interface TokenUsage {
23
+ inputTokens?: number;
24
+ outputTokens?: number;
25
+ reasoningTokens?: unknown;
26
+ cacheReadInputTokens?: unknown;
27
+ cacheCreationInputTokens?: unknown;
28
+ webSearchCount?: number;
29
+ rawUsage?: unknown;
30
+ }
18
31
  /**
19
32
  * Options for fetching a prompt
20
33
  */
@@ -91,6 +104,10 @@ interface CostOverride {
91
104
  inputCost: number;
92
105
  outputCost: number;
93
106
  }
107
+ declare enum AIEvent {
108
+ Generation = "$ai_generation",
109
+ Embedding = "$ai_embedding"
110
+ }
94
111
 
95
112
  declare const Chat: typeof OpenAI.Chat;
96
113
  declare const Completions: typeof OpenAI.Chat.Completions;
@@ -418,4 +435,68 @@ declare class Prompts {
418
435
  private fetchPromptFromApi;
419
436
  }
420
437
 
421
- export { PostHogAnthropic as Anthropic, PostHogAzureOpenAI as AzureOpenAI, PostHogGoogleGenAI as GoogleGenAI, LangChainCallbackHandler, PostHogOpenAI as OpenAI, type PromptCodeFallbackResult, type PromptRemoteResult, type PromptResult, Prompts, wrapVercelLanguageModel as withTracing };
438
+ type AnthropicTool = AnthropicOriginal.Tool;
439
+ /**
440
+ * Options for `captureAiGeneration`. Mirrors the `$ai_generation` event shape
441
+ * directly so that any caller — first-party SDK wrappers and external code
442
+ * alike — produces an identical event.
443
+ */
444
+ interface CaptureAiGenerationOptions {
445
+ distinctId?: string;
446
+ /** Auto-generated when omitted. */
447
+ traceId?: string;
448
+ /** Defaults to `$ai_generation`. */
449
+ eventType?: AIEvent;
450
+ /** Required for the event to be useful, but accepted as optional so SDK wrappers can pass through whatever they detect. */
451
+ model?: string;
452
+ provider: string;
453
+ input: unknown;
454
+ output: unknown;
455
+ /** Maps to `$ai_model_parameters` (temperature, max_tokens, top_p, …). */
456
+ modelParameters?: Record<string, unknown>;
457
+ baseURL?: string;
458
+ httpStatus?: number;
459
+ /** Wall-clock latency in seconds. */
460
+ latency?: number;
461
+ /** Time from request start to the first streamed token, in seconds. */
462
+ timeToFirstToken?: number;
463
+ usage?: TokenUsage;
464
+ /** Extra event properties merged into the captured event. */
465
+ properties?: Record<string, unknown>;
466
+ /** Mapping of group type to group id, matching `EventMessage.groups`. */
467
+ groups?: Record<string, string | number>;
468
+ privacyMode?: boolean;
469
+ /**
470
+ * For SDK wrappers: overrides the auto-detected model. External callers
471
+ * should pass `model` directly instead.
472
+ */
473
+ modelOverride?: string;
474
+ /**
475
+ * For SDK wrappers: overrides the auto-detected provider. External callers
476
+ * should pass `provider` directly instead.
477
+ */
478
+ providerOverride?: string;
479
+ costOverride?: CostOverride;
480
+ tools?: ChatCompletionTool[] | AnthropicTool[] | Tool[] | null;
481
+ stopReason?: string;
482
+ /** When set, the event is captured as an error. */
483
+ error?: unknown;
484
+ /** Awaits delivery instead of batching. Useful in serverless environments. */
485
+ captureImmediate?: boolean;
486
+ }
487
+ /**
488
+ * Capture an `$ai_generation` (or `$ai_embedding`) event to PostHog.
489
+ *
490
+ * This is the canonical primitive that every `@posthog/ai` wrapper
491
+ * (`withTracing`, `OpenAI`, `Anthropic`, `GoogleGenAI`, …) funnels through, so
492
+ * external code can use it directly to instrument LLM calls made through
493
+ * arbitrary clients (Cloudflare Workers AI, custom HTTP, etc.) and get the
494
+ * same events the SDK wrappers produce.
495
+ *
496
+ * When `error` is set, the event is captured as an error. If the error is an
497
+ * object, it is mutated in place to set `__posthog_previously_captured_error`
498
+ * so callers can re-throw the original error reference safely.
499
+ */
500
+ declare const captureAiGeneration: (client: PostHog, options: CaptureAiGenerationOptions) => Promise<void>;
501
+
502
+ export { AIEvent, PostHogAnthropic as Anthropic, PostHogAzureOpenAI as AzureOpenAI, type CaptureAiGenerationOptions, PostHogGoogleGenAI as GoogleGenAI, LangChainCallbackHandler, PostHogOpenAI as OpenAI, type PromptCodeFallbackResult, type PromptRemoteResult, type PromptResult, Prompts, captureAiGeneration, wrapVercelLanguageModel as withTracing };