@@ -1,6 +1,11 @@
|
||||
import Anthropic from '@anthropic-ai/sdk';
|
||||
|
||||
export async function getMessage(text: string) {
|
||||
interface AnthropicResponse {
|
||||
text: string;
|
||||
tokensUsed: number;
|
||||
}
|
||||
|
||||
export async function getMessage(text: string): Promise<AnthropicResponse> {
|
||||
const anthropic = new Anthropic({
|
||||
apiKey: process.env.ANTHROPIC_API_KEY
|
||||
});
|
||||
@@ -18,7 +23,20 @@ export async function getMessage(text: string) {
|
||||
try {
|
||||
const data = response.content as [{ type: string; text: string }];
|
||||
|
||||
return data[0].text;
|
||||
const tokensUsed =
|
||||
(response.usage?.input_tokens || 0) +
|
||||
(response.usage?.output_tokens || 0);
|
||||
|
||||
console.info('Token usage:', {
|
||||
input_tokens: response.usage?.input_tokens,
|
||||
output_tokens: response.usage?.output_tokens,
|
||||
total: tokensUsed
|
||||
});
|
||||
|
||||
return {
|
||||
text: data[0].text,
|
||||
tokensUsed
|
||||
};
|
||||
} catch (error) {
|
||||
throw new Error(`Anthropic client error: ${JSON.stringify(error)}.`);
|
||||
}
|
||||
|
||||
@@ -1,32 +1,66 @@
|
||||
import { getMessage } from '@utils/anthropicClient';
|
||||
import { ShortcutsResponse } from '../types';
|
||||
import { dbOperations } from '@utils/db';
|
||||
|
||||
export async function anthropicCommand(
|
||||
parameters: Record<string, string> | undefined
|
||||
): Promise<ShortcutsResponse> {
|
||||
let question = '';
|
||||
let response = '';
|
||||
let success = false;
|
||||
let errorMessage: string | undefined;
|
||||
let tokensUsed: number | undefined;
|
||||
|
||||
try {
|
||||
if (!parameters || !parameters['question']) {
|
||||
errorMessage = 'Need to provide a question.';
|
||||
return {
|
||||
success: false,
|
||||
message: 'Sorry. Need to provide a question.'
|
||||
};
|
||||
}
|
||||
|
||||
const response = await getMessage(
|
||||
question = parameters['question'];
|
||||
const prompt =
|
||||
'I want to know ' +
|
||||
parameters['question'] +
|
||||
'. Structure the response in a manner suitable for spoken communication.'
|
||||
);
|
||||
question +
|
||||
'. Structure the response in a manner suitable for spoken communication.';
|
||||
|
||||
const anthropicResponse = await getMessage(prompt);
|
||||
response = anthropicResponse.text;
|
||||
tokensUsed = anthropicResponse.tokensUsed;
|
||||
success = true;
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: response
|
||||
message: response,
|
||||
data: {
|
||||
tokensUsed
|
||||
}
|
||||
};
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
console.error('Anthropic command error:', error);
|
||||
success = false;
|
||||
errorMessage = error instanceof Error ? error.message : 'Unknown error';
|
||||
response = 'Sorry. There was a problem with Anthropic.';
|
||||
|
||||
return {
|
||||
success: false,
|
||||
message: 'Sorry. There was a problem with Anthropic.'
|
||||
message: response
|
||||
};
|
||||
} finally {
|
||||
if (question) {
|
||||
try {
|
||||
await dbOperations.saveAnthropicQuery({
|
||||
question,
|
||||
response,
|
||||
success,
|
||||
errorMessage,
|
||||
tokensUsed
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Failed to log query to database:', error);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
44
utils/db.ts
Normal file
44
utils/db.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
|
||||
declare global {
|
||||
var prisma: PrismaClient | undefined;
|
||||
}
|
||||
|
||||
const db = global.prisma || new PrismaClient();
|
||||
|
||||
if (process.env.NODE_ENV === 'development') {
|
||||
global.prisma = db;
|
||||
}
|
||||
|
||||
export const dbOperations = {
|
||||
async saveAnthropicQuery({
|
||||
question,
|
||||
response,
|
||||
success,
|
||||
errorMessage,
|
||||
tokensUsed
|
||||
}: {
|
||||
question: string;
|
||||
response: string;
|
||||
success: boolean;
|
||||
errorMessage?: string;
|
||||
tokensUsed?: number;
|
||||
}) {
|
||||
try {
|
||||
return await db.anthropicQuery.create({
|
||||
data: {
|
||||
question,
|
||||
response,
|
||||
success,
|
||||
errorMessage,
|
||||
tokensUsed
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Failed to save query:', error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export { db };
|
||||
Reference in New Issue
Block a user