Skip to content

Please someone help to make Database adapter like thing #715

@devshahoriar

Description

@devshahoriar

TanStack AI version

"@tanstack/ai": "^0.28.0",

Framework/Library version

"react": "^19.2.7",

Describe the bug and the steps to reproduce it

import {
chat,
ChatMiddleware,
convertMessagesToModelMessages,
toHttpResponse,
toolDefinition,
uiMessageToModelMessages
} from '@tanstack/ai'
import { Hono } from 'hono'
import z from 'zod'
import { gemini25FlashLite, openRouterChat } from '../ai/models'
import needAuth, { WithAuth } from '../middleware/auth'

export const aiChatRouter = new Hono()

aiChatRouter.use('*',needAuth)

const changeThemeDef = toolDefinition({
name: 'changeTheme',
description: 'Change the theme of the application',
inputSchema: z.object({
theme: z.enum(['light', 'dark']),
}),
outputSchema: z.object({
success: z.boolean(),
}),
})

const messageSaveToDB :ChatMiddleware={
name:'saveToDb',
onFinish(ctx, info) {
console.log(ctx.messages)
},

}

aiChatRouter.post('/chat', async (c) => {
const body = await c.req.json()
const messages = Array.isArray(body?.messages) ? body.messages : []
const threadId = body.threadId || thread-${Date.now()}
try {
const stream = chat({
adapter: openRouterChat,
messages,
tools: [changeThemeDef],
middleware: [messageSaveToDB],
threadId,
systemPrompts: [
'You are a helpful assistant that answers in GitHub Flavored Markdown (GFM). When you include math, use proper LaTeX: inline math in $...$ and block math in $$...$$. For matrices and aligned equations, use LaTeX environments . Do not output matrices as plain text. Prefer clear, minimal expressions; do not mix plain text with math inside the same delimiters. If a formula is wide, render it as block math.',
],
})

return toHttpResponse(stream, {
  headers: {
    'Content-Type': 'application/x-ndjson',
    'Cache-Control': 'no-cache',
  },
})

} catch (error) {
console.log(error)
return c.json({ error: 'Failed to get response from AI model' }, 500)
}
})

Look at the code i use onFinish but it do't show last ai replay how to make a database adapter. i use openrouter and reactnative.

Your Minimal, Reproducible Example - (Sandbox Highly Recommended)

code given

Screenshots or Videos (Optional)

Image

Do you intend to try to help solve this bug with your own PR?

No, because I do not know how

Terms & Code of Conduct

  • I agree to follow this project's Code of Conduct
  • I understand that if my bug cannot be reliable reproduced in a debuggable environment, it will probably not be fixed and this issue may even be closed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions