This repository has been archived on 2026-02-01. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
siri-shortcuts/utils/commands/anthropic.ts
Riccardo Senica 30c67a308b Data storage (#3)
* feat: add prisma

* fix: generate prisma
2025-06-23 19:04:01 +02:00

67 lines
1.7 KiB
TypeScript

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.'
};
}
question = parameters['question'];
const prompt =
'I want to know ' +
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,
data: {
tokensUsed
}
};
} catch (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: response
};
} finally {
if (question) {
try {
await dbOperations.saveAnthropicQuery({
question,
response,
success,
errorMessage,
tokensUsed
});
} catch (error) {
console.error('Failed to log query to database:', error);
}
}
}
}