mirror of
https://github.com/FranP-code/create-better-t-stack.git
synced 2025-10-12 23:52:15 +00:00
feat(cli): add elysia + aisdk support and fix fastify ai example
This commit is contained in:
@@ -4,6 +4,10 @@ import { node } from "@elysiajs/node";
|
||||
{{/if}}
|
||||
import { Elysia } from "elysia";
|
||||
import { cors } from "@elysiajs/cors";
|
||||
{{#if (includes examples "ai")}}
|
||||
import { google } from "@ai-sdk/google";
|
||||
import { convertToModelMessages, streamText } from "ai";
|
||||
{{/if}}
|
||||
{{#if (eq api "trpc")}}
|
||||
import { createContext } from "./lib/context";
|
||||
import { appRouter } from "./routers/index";
|
||||
@@ -94,6 +98,18 @@ const app = new Elysia()
|
||||
});
|
||||
return res;
|
||||
})
|
||||
{{/if}}
|
||||
{{#if (includes examples "ai")}}
|
||||
.post("/ai", async (context) => {
|
||||
const body = await context.request.json();
|
||||
const uiMessages = body.messages || [];
|
||||
const result = streamText({
|
||||
model: google("gemini-2.0-flash"),
|
||||
messages: convertToModelMessages(uiMessages)
|
||||
});
|
||||
|
||||
return result.toUIMessageStreamResponse();
|
||||
})
|
||||
{{/if}}
|
||||
.get("/", () => "OK")
|
||||
.listen(3000, () => {
|
||||
|
||||
@@ -158,15 +158,14 @@ interface AiRequestBody {
|
||||
messages: UIMessage[];
|
||||
}
|
||||
|
||||
fastify.post('/ai', async function (request, reply) {
|
||||
// there are some issues with the ai sdk and fastify, docs: https://ai-sdk.dev/cookbook/api-servers/fastify
|
||||
fastify.post('/ai', async function (request) {
|
||||
const { messages } = request.body as AiRequestBody;
|
||||
const result = streamText({
|
||||
model: google('gemini-1.5-flash'),
|
||||
messages: convertToModelMessages(messages),
|
||||
});
|
||||
|
||||
return result.pipeUIMessageStreamToResponse(reply.raw);
|
||||
return result.toUIMessageStreamResponse();
|
||||
});
|
||||
{{/if}}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user