Connor Fogarty commited on
Commit
cf26551
·
unverified ·
1 Parent(s): fb2d957

feat: send analytics event for token usage (#37)

Browse files
packages/bolt/app/lib/.server/llm/stream-text.ts CHANGED
@@ -30,9 +30,6 @@ export function streamText(messages: Messages, env: Env, options?: StreamingOpti
30
  'anthropic-beta': 'max-tokens-3-5-sonnet-2024-07-15',
31
  },
32
  messages: convertToCoreMessages(messages),
33
- onFinish: ({ finishReason, usage, warnings }) => {
34
- console.log({ finishReason, usage, warnings });
35
- },
36
  ...options,
37
  });
38
  }
 
30
  'anthropic-beta': 'max-tokens-3-5-sonnet-2024-07-15',
31
  },
32
  messages: convertToCoreMessages(messages),
 
 
 
33
  ...options,
34
  });
35
  }
packages/bolt/app/lib/.server/llm/switchable-stream.ts CHANGED
@@ -24,8 +24,6 @@ export default class SwitchableStream extends TransformStream {
24
  await this._currentReader.cancel();
25
  }
26
 
27
- console.log('Switching stream');
28
-
29
  this._currentReader = newStream.getReader();
30
 
31
  this._pumpStream();
 
24
  await this._currentReader.cancel();
25
  }
26
 
 
 
27
  this._currentReader = newStream.getReader();
28
 
29
  this._pumpStream();
packages/bolt/app/lib/analytics.ts CHANGED
@@ -13,6 +13,7 @@ const MESSAGE_PREFIX = 'Bolt';
13
 
14
  export enum AnalyticsTrackEvent {
15
  MessageSent = `${MESSAGE_PREFIX} Message Sent`,
 
16
  ChatCreated = `${MESSAGE_PREFIX} Chat Created`,
17
  }
18
 
 
13
 
14
  export enum AnalyticsTrackEvent {
15
  MessageSent = `${MESSAGE_PREFIX} Message Sent`,
16
+ MessageComplete = `${MESSAGE_PREFIX} Message Complete`,
17
  ChatCreated = `${MESSAGE_PREFIX} Chat Created`,
18
  }
19
 
packages/bolt/app/routes/api.chat.ts CHANGED
@@ -4,6 +4,8 @@ import { CONTINUE_PROMPT } from '~/lib/.server/llm/prompts';
4
  import { streamText, type Messages, type StreamingOptions } from '~/lib/.server/llm/stream-text';
5
  import SwitchableStream from '~/lib/.server/llm/switchable-stream';
6
  import { handleWithAuth } from '~/lib/.server/login';
 
 
7
 
8
  export async function action(args: ActionFunctionArgs) {
9
  return handleWithAuth(args, chatAction);
@@ -17,8 +19,21 @@ async function chatAction({ context, request }: ActionFunctionArgs) {
17
  try {
18
  const options: StreamingOptions = {
19
  toolChoice: 'none',
20
- onFinish: async ({ text: content, finishReason }) => {
21
  if (finishReason !== 'length') {
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  return stream.close();
23
  }
24
 
 
4
  import { streamText, type Messages, type StreamingOptions } from '~/lib/.server/llm/stream-text';
5
  import SwitchableStream from '~/lib/.server/llm/switchable-stream';
6
  import { handleWithAuth } from '~/lib/.server/login';
7
+ import { getSession } from '~/lib/.server/sessions';
8
+ import { AnalyticsAction, AnalyticsTrackEvent, sendEventInternal } from '~/lib/analytics';
9
 
10
  export async function action(args: ActionFunctionArgs) {
11
  return handleWithAuth(args, chatAction);
 
19
  try {
20
  const options: StreamingOptions = {
21
  toolChoice: 'none',
22
+ onFinish: async ({ text: content, finishReason, usage }) => {
23
  if (finishReason !== 'length') {
24
+ const { session } = await getSession(request, context.cloudflare.env);
25
+
26
+ await sendEventInternal(session.data, {
27
+ action: AnalyticsAction.Track,
28
+ payload: {
29
+ event: AnalyticsTrackEvent.MessageComplete,
30
+ properties: {
31
+ usage,
32
+ finishReason,
33
+ },
34
+ },
35
+ });
36
+
37
  return stream.close();
38
  }
39