Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions app/api/chat/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ import { getAuthorizedUserInfo } from "@/lib/auth.rsc";
import { createPostHogClient } from "@/lib/posthog.rsc";
import { anthropic, type AnthropicProviderOptions } from "@ai-sdk/anthropic";
import { withTracing } from "@posthog/ai";
import { convertToModelMessages, stepCountIs, streamText, tool, type UIMessage } from "ai";
import { convertToModelMessages, pruneMessages, stepCountIs, streamText, tool, type UIMessage } from "ai";
import { NextResponse } from "next/server";
import { z } from "zod";

export const maxDuration = 30;

const QUESTION_INFO = graphql(`
query QuestionInfo($id: ID!) {
question(id: $id) {
Expand Down Expand Up @@ -101,18 +99,25 @@ export async function POST(req: Request) {
);
}

const model = anthropic("claude-sonnet-4-5");
const model = anthropic("claude-haiku-4-5");
const posthogClient = await createPostHogClient();

const tracedModel = withTracing(model, posthogClient, {
posthogDistinctId: userInfo.sub,
});

const prunedMessages = pruneMessages({
messages: convertToModelMessages(messages),
reasoning: "none",
toolCalls: "before-last-message",
emptyMessages: "remove",
});

const result = streamText({
model: tracedModel,
providerOptions: {
anthropic: {
thinking: { type: "enabled", budgetTokens: 12000 },
thinking: { type: "enabled", budgetTokens: 2000 },
} satisfies AnthropicProviderOptions,
},
messages: [
Expand All @@ -136,7 +141,7 @@ export async function POST(req: Request) {
} satisfies AnthropicProviderOptions,
},
},
...convertToModelMessages(messages),
...prunedMessages,
],
stopWhen: stepCountIs(10),
tools: {
Expand Down
26 changes: 22 additions & 4 deletions gql/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,17 @@ export type CreateGroupInput = {
scopeSetIDs?: InputMaybe<Array<Scalars['ID']['input']>>;
};

/**
* CreatePointInput is used for create Point object.
* Input was generated by ent.
*/
export type CreatePointInput = {
description?: InputMaybe<Scalars['String']['input']>;
grantedAt?: InputMaybe<Scalars['Time']['input']>;
points?: InputMaybe<Scalars['Int']['input']>;
userID: Scalars['ID']['input'];
};

/**
* CreateQuestionInput is used for create Question object.
* Input was generated by ent.
Expand Down Expand Up @@ -388,6 +399,8 @@ export type Mutation = {
createDatabase: Database;
/** Create a new group. */
createGroup?: Maybe<Group>;
/** Create a new point record for a user. */
createPoint?: Maybe<Point>;
/** Create a question. */
createQuestion: Question;
/** Create a new scope set. */
Expand Down Expand Up @@ -440,6 +453,11 @@ export type MutationCreateGroupArgs = {
};


export type MutationCreatePointArgs = {
input: CreatePointInput;
};


export type MutationCreateQuestionArgs = {
input: CreateQuestionInput;
};
Expand Down Expand Up @@ -897,13 +915,13 @@ export enum QuestionOrderField {

export type QuestionStatistics = {
__typename?: 'QuestionStatistics';
/** 嘗試人數 */
/** Number of users who attempted */
attemptedUsers: Scalars['Int']['output'];
/** 答案正確的提交數 */
/** Number of correct submissions */
correctSubmissionCount: Scalars['Int']['output'];
/** 通過的學生數 */
/** Number of users who passed */
passedUsers: Scalars['Int']['output'];
/** 所有提交數 */
/** Total number of submissions */
submissionCount: Scalars['Int']['output'];
};

Expand Down
21 changes: 17 additions & 4 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,17 @@ input CreateGroupInput {
scopeSetIDs: [ID!]
}

"""
CreatePointInput is used for create Point object.
Input was generated by ent.
"""
input CreatePointInput {
description: String
grantedAt: Time
points: Int
userID: ID!
}

"""
CreateQuestionInput is used for create Question object.
Input was generated by ent.
Expand Down Expand Up @@ -383,6 +394,8 @@ type Mutation {
createDatabase(input: CreateDatabaseInput!): Database!
"""Create a new group."""
createGroup(input: CreateGroupInput!): Group
"""Create a new point record for a user."""
createPoint(input: CreatePointInput!): Point
"""Create a question."""
createQuestion(input: CreateQuestionInput!): Question!
"""Create a new scope set."""
Expand Down Expand Up @@ -770,13 +783,13 @@ enum QuestionOrderField {
}

type QuestionStatistics {
"""嘗試人數"""
"""Number of users who attempted"""
attemptedUsers: Int!
"""答案正確的提交數"""
"""Number of correct submissions"""
correctSubmissionCount: Int!
"""通過的學生數"""
"""Number of users who passed"""
passedUsers: Int!
"""所有提交數"""
"""Total number of submissions"""
submissionCount: Int!
}

Expand Down