feat: AI coach functions

This commit is contained in:
2026-05-20 17:48:23 +02:00
parent 93aad601cf
commit fc57fe2415
+69
View File
@@ -0,0 +1,69 @@
import type { Database } from 'better-sqlite3';
import { createAiClient } from './client';
import { buildContext } from './context';
import { progressPrompt, nextStepPrompt, reschedulePrompt, setupPrompt } from './prompts';
import { parseScheduleJson, type ScheduleProposal } from './parser';
import { getSetting } from '../db/settings';
import { config } from '../config';
function getModel(db: Database): string {
return getSetting(db, 'model') ?? config.defaultModel;
}
async function chat(
db: Database,
system: string,
user: string,
jsonMode = false
): Promise<string> {
const client = createAiClient();
const model = getModel(db);
const response = await client.chat.completions.create({
model,
messages: [
{ role: 'system', content: system },
{ role: 'user', content: user },
],
...(jsonMode ? { response_format: { type: 'json_object' } } : {}),
});
return response.choices[0]?.message.content ?? '';
}
export async function getProgressAnalysis(db: Database, today: string): Promise<string> {
const ctx = buildContext(db, today);
const { system, user } = progressPrompt(ctx);
return chat(db, system, user);
}
export async function getNextStep(db: Database, today: string): Promise<string> {
const ctx = buildContext(db, today);
const { system, user } = nextStepPrompt(ctx);
return chat(db, system, user);
}
export async function generateSchedule(
db: Database, today: string, note: string | null
): Promise<ScheduleProposal[]> {
const ctx = buildContext(db, today);
const { system, user } = reschedulePrompt(ctx, note);
const raw = await chat(db, system, user);
return parseScheduleJson(raw);
}
export async function generateSetupSchedule(
db: Database,
today: string,
currentLevel: string,
availability: string,
goal: string
): Promise<ScheduleProposal[]> {
const { system, user } = setupPrompt(currentLevel, availability, goal);
const raw = await chat(db, system, user);
return parseScheduleJson(raw);
}
export async function freeChat(db: Database, today: string, message: string): Promise<string> {
const ctx = buildContext(db, today);
const { system } = progressPrompt(ctx);
return chat(db, system, message);
}