feat: send analytics event for token usage (#37)
This commit is contained in:
@@ -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 SwitchableStream from '~/lib/.server/llm/switchable-stream';
|
||||
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) {
|
||||
return handleWithAuth(args, chatAction);
|
||||
@@ -17,8 +19,21 @@ async function chatAction({ context, request }: ActionFunctionArgs) {
|
||||
try {
|
||||
const options: StreamingOptions = {
|
||||
toolChoice: 'none',
|
||||
onFinish: async ({ text: content, finishReason }) => {
|
||||
onFinish: async ({ text: content, finishReason, usage }) => {
|
||||
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();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user