feat: use vercel ai gateway (#38)

This commit is contained in:
2025-10-11 16:37:36 +02:00
committed by GitHub
parent 8ed813dd53
commit 2afe8e39b9
10 changed files with 1271 additions and 948 deletions

52
utils/ai/client.ts Normal file
View File

@@ -0,0 +1,52 @@
import { generateText, tool, jsonSchema } from 'ai';
import { BaseTool } from './tool';
import type { JSONSchema7 } from 'json-schema';
export async function getMessage<T>(text: string, baseTool: BaseTool) {
console.info('Vercel AI Gateway request with text: ', text);
try {
const { steps } = await generateText({
model: 'anthropic/claude-sonnet-4.5',
temperature: 1,
tools: {
[baseTool.name]: tool({
description: baseTool.input_schema.description || '',
inputSchema: jsonSchema(baseTool.input_schema as JSONSchema7),
execute: async args => args
})
},
toolChoice: {
type: 'tool',
toolName: baseTool.name
},
prompt: text
});
console.info('Vercel AI Gateway response steps: ', steps);
const toolCalls = steps.flatMap(step => step.toolCalls);
if (!toolCalls || toolCalls.length === 0) {
throw new Error('No tool calls found in response');
}
const typedCall = toolCalls[0] as unknown as {
toolName: string;
input: Record<string, unknown>;
};
console.info('Tool call input: ', typedCall.input);
if (typedCall.toolName !== baseTool.name) {
throw new Error(
`Expected tool ${baseTool.name} but got ${typedCall.toolName}`
);
}
return typedCall.input as T;
} catch (error) {
console.error('Vercel AI Gateway error: ', error);
throw new Error('Failed to get message from AI Gateway');
}
}

28
utils/ai/summarize.ts Normal file
View File

@@ -0,0 +1,28 @@
import { getMessage } from './client';
import { NewsType } from '../validationSchemas';
import { NewsletterTool, newsletterTool } from './tool';
export async function summirize(news: NewsType[]) {
const newsInput = news
.map(n => `TITLE: ${n.title}, CONTENT: ${n.text}, LINK: ${n.url}`)
.join('\n\n');
const promptSetup =
"You are a tech journalist with a technology degree and background. Create a tech newsletter from the following list of posts from an online forum. Use the NewsletterTool to structure your response with the following requirements:\n\n1. TITLE:\n- Create a title that is 40-50 characters long\n- Capture the most significant themes or developments\n- Use engaging but professional language\n\n2. CONTENT:\nSummarize the following list of posts from an online forum as a TL;DR (Too Long; Didn't Read) summary. Your summary should:\n1. Be 300-400 words long (not counting the urls)\n2. Structure the content in 2-3 short paragraphs, with each paragraph focusing on a specific theme or technology area\n3. Start with the 2-3 most significant or impactful news items in the first paragraph\n4. Use HTML paragraph tags (<p>) to separate paragraphs for better readability\n5. Use a tone that is informative and slightly enthusiastic, aimed at tech-savvy general readers\n6. Incorporate links as follows, including at most 3-4 words: <a href='[LINK]' target='_blank' rel='noopener noreferrer'>[linked text]</a>\n7. Each mentioned news item must include its own url link\n\n3. FOCUS:\nEnd with a forward-looking assessment of the key developments and trends emerging from these news items. Consider their potential impact on the tech landscape and any implications for future developments. Keep this section concise while maintaining an analytical perspective that helps readers understand what to watch for in the coming weeks and months.\n\nUse the NewsletterTool to provide your response with these three components.\n\nThe news items are structured as follows:\n\nTITLE: <title>\nCONTENT: <content>\nLINK: <link>\n\nPlease analyze and summarize the following news:";
try {
const { title, content, focus } = await getMessage<NewsletterTool>(
promptSetup + newsInput,
newsletterTool
);
return {
title,
content,
focus
};
} catch (error) {
console.error('Error in summarize:', error);
throw Error(JSON.stringify(error));
}
}

44
utils/ai/tool.ts Normal file
View File

@@ -0,0 +1,44 @@
export interface BaseTool {
readonly name: string;
readonly input_schema: {
readonly type: 'object';
readonly properties: Record<string, unknown>;
readonly required?: readonly string[];
readonly description?: string;
};
}
export interface ToolUseBlock {
type: 'tool_use';
id: string;
name: string;
input: Record<string, unknown>;
}
export interface NewsletterTool {
title: string;
content: string;
focus: string;
}
export const newsletterTool: BaseTool = {
name: 'NewsletterTool' as const,
input_schema: {
type: 'object' as const,
description: 'Newsletter',
properties: {
title: {
type: 'string' as const,
description: 'The title of the newsletter'
},
content: {
type: 'string' as const,
description: 'The main content of the newsletter'
},
focus: {
type: 'string' as const,
description: 'The text of the focus segment'
}
}
}
} as const;