mirror of
https://github.com/stackblitz/bolt.new
synced 2024-11-27 22:42:21 +00:00
feat: send analytics event for token usage (#37)
This commit is contained in:
parent
fb2d957351
commit
cf265515cb
@ -30,9 +30,6 @@ export function streamText(messages: Messages, env: Env, options?: StreamingOpti
|
|||||||
'anthropic-beta': 'max-tokens-3-5-sonnet-2024-07-15',
|
'anthropic-beta': 'max-tokens-3-5-sonnet-2024-07-15',
|
||||||
},
|
},
|
||||||
messages: convertToCoreMessages(messages),
|
messages: convertToCoreMessages(messages),
|
||||||
onFinish: ({ finishReason, usage, warnings }) => {
|
|
||||||
console.log({ finishReason, usage, warnings });
|
|
||||||
},
|
|
||||||
...options,
|
...options,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -24,8 +24,6 @@ export default class SwitchableStream extends TransformStream {
|
|||||||
await this._currentReader.cancel();
|
await this._currentReader.cancel();
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log('Switching stream');
|
|
||||||
|
|
||||||
this._currentReader = newStream.getReader();
|
this._currentReader = newStream.getReader();
|
||||||
|
|
||||||
this._pumpStream();
|
this._pumpStream();
|
||||||
|
@ -13,6 +13,7 @@ const MESSAGE_PREFIX = 'Bolt';
|
|||||||
|
|
||||||
export enum AnalyticsTrackEvent {
|
export enum AnalyticsTrackEvent {
|
||||||
MessageSent = `${MESSAGE_PREFIX} Message Sent`,
|
MessageSent = `${MESSAGE_PREFIX} Message Sent`,
|
||||||
|
MessageComplete = `${MESSAGE_PREFIX} Message Complete`,
|
||||||
ChatCreated = `${MESSAGE_PREFIX} Chat Created`,
|
ChatCreated = `${MESSAGE_PREFIX} Chat Created`,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,6 +4,8 @@ import { CONTINUE_PROMPT } from '~/lib/.server/llm/prompts';
|
|||||||
import { streamText, type Messages, type StreamingOptions } from '~/lib/.server/llm/stream-text';
|
import { streamText, type Messages, type StreamingOptions } from '~/lib/.server/llm/stream-text';
|
||||||
import SwitchableStream from '~/lib/.server/llm/switchable-stream';
|
import SwitchableStream from '~/lib/.server/llm/switchable-stream';
|
||||||
import { handleWithAuth } from '~/lib/.server/login';
|
import { handleWithAuth } from '~/lib/.server/login';
|
||||||
|
import { getSession } from '~/lib/.server/sessions';
|
||||||
|
import { AnalyticsAction, AnalyticsTrackEvent, sendEventInternal } from '~/lib/analytics';
|
||||||
|
|
||||||
export async function action(args: ActionFunctionArgs) {
|
export async function action(args: ActionFunctionArgs) {
|
||||||
return handleWithAuth(args, chatAction);
|
return handleWithAuth(args, chatAction);
|
||||||
@ -17,8 +19,21 @@ async function chatAction({ context, request }: ActionFunctionArgs) {
|
|||||||
try {
|
try {
|
||||||
const options: StreamingOptions = {
|
const options: StreamingOptions = {
|
||||||
toolChoice: 'none',
|
toolChoice: 'none',
|
||||||
onFinish: async ({ text: content, finishReason }) => {
|
onFinish: async ({ text: content, finishReason, usage }) => {
|
||||||
if (finishReason !== 'length') {
|
if (finishReason !== 'length') {
|
||||||
|
const { session } = await getSession(request, context.cloudflare.env);
|
||||||
|
|
||||||
|
await sendEventInternal(session.data, {
|
||||||
|
action: AnalyticsAction.Track,
|
||||||
|
payload: {
|
||||||
|
event: AnalyticsTrackEvent.MessageComplete,
|
||||||
|
properties: {
|
||||||
|
usage,
|
||||||
|
finishReason,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
return stream.close();
|
return stream.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user