-
Notifications
You must be signed in to change notification settings - Fork 263
feat(nuxi): use eve framework #2280
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
HugoRCD
wants to merge
23
commits into
main
Choose a base branch
from
feat/nuxi-on-eve
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
e70f2e9
feat(nuxi): use eve framework
HugoRCD dad9002
refacto architecture
HugoRCD 0509ebe
add slack adapter
HugoRCD 2ac18c7
improve architecture
HugoRCD 993ee46
simplify eve setup
HugoRCD cce02ec
use eve convention
HugoRCD 8c6defc
fix(prerender): disable link crawler to avoid flaky build failures
HugoRCD 53fa458
add admin-mcp to nuxi
HugoRCD 1a5a26d
fix some slack issues and remove some useless data
HugoRCD 11ca6fe
try connecting vercel o11y as tool
HugoRCD 72f835b
revert commit
HugoRCD eb33950
Merge remote-tracking branch 'origin/main' into feat/nuxi-on-eve
HugoRCD 8efcfeb
fix(slack): mute session failed message on hook conflict
HugoRCD c46ada5
perf(ci): skip eve dev spawn during nuxt test setup
HugoRCD d7cafe0
improve arch
HugoRCD cf75743
fix command palette
HugoRCD f5b17b2
Merge remote-tracking branch 'origin/main' into feat/nuxi-on-eve
HugoRCD 625c74d
fix feedback from code rabbit
HugoRCD 47bfc13
update deps
HugoRCD 76b0b39
fix(nuxi): stable anonymous principal and message part schema
HugoRCD fc6aaa2
refactor(nuxi): scope anonymous id to rate limits only
HugoRCD 053dc99
overall fixes
HugoRCD 8ed3f01
fix(nuxi): refresh usage on auth change and validate source URLs
HugoRCD File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,8 @@ dist | |
| .cache | ||
| .data | ||
| .eslintcache | ||
| .eve | ||
| .workflow-data | ||
|
|
||
| # Local Netlify folder | ||
| .netlify | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| # Nuxi layer | ||
|
|
||
| Everything for the Nuxi assistant on nuxt.com lives here. | ||
|
|
||
| ```text | ||
| layers/nuxi/ | ||
| ├── agent/ # Eve runtime (channels, tools, hooks) — deployed as the `eve` Vercel service | ||
| ├── app/ # Chat UI (panel, dashboard, composables) | ||
| ├── server/ # Nuxt APIs + internal routes the agent calls over HTTP | ||
| └── shared/ # Types and utils shared by app + server | ||
| ``` | ||
|
|
||
| ## Boundaries | ||
|
|
||
| | Layer | Runs in | Talks to | | ||
| |-------|---------|----------| | ||
| | `agent/` | Eve (`eve dev` / Vercel `eve` service) | Nuxt via `/api/internal/*` (`INTERNAL_API_SECRET`) | | ||
| | `server/api/internal/*` | Nuxt Nitro | DB, GitHub, content — never exposed publicly | | ||
| | `server/api/chats/*`, `agent/*` | Nuxt Nitro | Browser (session auth) | | ||
| | `app/` | Browser | Nuxt APIs + Eve transport (`/_eve_internal/eve`) | | ||
|
|
||
| The agent never imports Nuxt server code directly. Shared logic that both sides need either lives in `shared/` (app + server) or is exposed through an internal API route. | ||
|
|
||
| ## Config | ||
|
|
||
| `eve.eveRoot` points Eve at this layer so `agent/` is discovered at `layers/nuxi/agent/`. The layer includes a minimal `package.json` (Eve project marker for nested layout discovery). Root `vercel.json` declares the dual `web` + `eve` services with `entrypoint: "layers/nuxi"` for Eve. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| import { defineAgent } from 'eve' | ||
|
|
||
| export default defineAgent({ | ||
| model: 'anthropic/claude-sonnet-4.6', | ||
| modelOptions: { | ||
| providerOptions: { | ||
| gateway: { | ||
| caching: 'auto' | ||
| }, | ||
| anthropic: { | ||
| thinking: { | ||
| type: 'enabled', | ||
| budgetTokens: 2048 | ||
| } | ||
| } | ||
| } | ||
| } | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| import type { AuthFn } from 'eve/channels/auth' | ||
| import { localDev, vercelOidc } from 'eve/channels/auth' | ||
| import { defaultEveAuth, eveChannel } from 'eve/channels/eve' | ||
| import { appOrigin, internalHeaders } from '../lib/internal-api.js' | ||
|
|
||
| const PAGE_PATH_PATTERN = /^\/[\w./-]*$/ | ||
|
|
||
| interface SessionPrincipal { | ||
| principalId: string | ||
| principalType: 'user' | 'anonymous' | ||
| authenticated: boolean | ||
| attributes?: { | ||
| login?: string | ||
| name?: string | ||
| avatar?: string | ||
| role?: string | ||
| } | ||
| } | ||
|
|
||
| function nuxtSessionAuth(): AuthFn<Request> { | ||
| return async (request) => { | ||
| const cookie = request.headers.get('cookie') ?? '' | ||
| if (!cookie) return null | ||
|
|
||
| try { | ||
| const response = await fetch(`${appOrigin()}/api/internal/session`, { | ||
| headers: internalHeaders({ cookie }) | ||
| }) | ||
|
|
||
| if (!response.ok) return null | ||
|
|
||
| const data = await response.json() as SessionPrincipal | ||
|
|
||
| return { | ||
| attributes: data.attributes ?? {}, | ||
| authenticator: data.authenticated ? 'github' : 'anonymous', | ||
| issuer: 'nuxt.com', | ||
| principalId: data.principalId, | ||
| principalType: data.principalType === 'user' ? 'user' : 'anonymous' | ||
| } | ||
| } catch { | ||
| return null | ||
| } | ||
| } | ||
| } | ||
|
|
||
| function parsePagePath(request: Request): string | null { | ||
| const raw = request.headers.get('x-page-path')?.trim() ?? null | ||
| if (!raw || !PAGE_PATH_PATTERN.test(raw) || raw.length > 256) return null | ||
| return raw | ||
| } | ||
|
|
||
| export default eveChannel({ | ||
| auth: [ | ||
| nuxtSessionAuth(), | ||
| localDev(), | ||
| vercelOidc() | ||
| ], | ||
| async onMessage(ctx, message) { | ||
| const context: string[] = [] | ||
| const pagePath = parsePagePath(ctx.eve.request) | ||
| const chatId = ctx.eve.request.headers.get('x-nuxi-chat-id')?.trim() | ||
| const isNewSession = !ctx.eve.sessionId | ||
|
|
||
| if (pagePath) { | ||
| context.push(`Current page: ${pagePath}`) | ||
| } | ||
|
|
||
| if (isNewSession && chatId) { | ||
| try { | ||
| const cookie = ctx.eve.request.headers.get('cookie') ?? '' | ||
| const response = await fetch(`${appOrigin()}/api/internal/chats/${encodeURIComponent(chatId)}/context`, { | ||
| headers: internalHeaders(cookie ? { cookie } : undefined) | ||
| }) | ||
|
|
||
| if (response.ok) { | ||
| const data = await response.json() as { summary?: string } | ||
| if (data.summary) { | ||
| context.push(`Prior conversation (for context):\n${data.summary}`) | ||
| } | ||
| } | ||
| } catch { | ||
| // Non-fatal — continue without prior context | ||
| } | ||
| } | ||
|
|
||
| if (typeof message === 'string' && message.trim()) { | ||
| context.push(`User message: ${message}`) | ||
| } | ||
|
|
||
| return { | ||
| auth: defaultEveAuth(ctx), | ||
| context: context.length ? context : undefined | ||
| } | ||
| } | ||
| }) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| import { connectSlackCredentials } from '@vercel/connect/eve' | ||
| import { | ||
| defaultSlackAuth, | ||
| loadThreadContextMessages, | ||
| slackChannel, | ||
| type SlackContext, | ||
| type SlackMessage | ||
| } from 'eve/channels/slack' | ||
|
|
||
| function isHookConflictFailure(event: { code?: string, message?: string }) { | ||
| const message = event.message ?? '' | ||
| return event.code === 'HookConflictError' | ||
| || message.includes('HookConflict') | ||
| || message.includes('already in use by another workflow') | ||
| } | ||
|
|
||
| async function dispatchSlackMessage(ctx: SlackContext, message: SlackMessage) { | ||
| await ctx.thread.startTyping('Thinking...') | ||
|
|
||
| const auth = defaultSlackAuth(message, ctx) | ||
| if (!auth) return null | ||
|
|
||
| const context = ['The user is talking to Nuxi on Slack.'] | ||
|
|
||
| const prior = await loadThreadContextMessages(ctx.thread, message, { | ||
| since: 'last-agent-reply' | ||
| }) | ||
|
|
||
| if (prior.length > 0) { | ||
| const transcript = prior | ||
| .map(m => `${m.isMe ? 'you' : (m.user ?? 'user')}: ${m.markdown}`) | ||
| .join('\n') | ||
| context.push(`Recent thread messages since your last reply:\n\n${transcript}`) | ||
| } | ||
|
|
||
| return { auth, context } | ||
| } | ||
|
|
||
| export default slackChannel({ | ||
| credentials: connectSlackCredentials( | ||
| process.env.SLACK_CONNECTOR ?? 'slack/nuxi' | ||
| ), | ||
| botName: 'Nuxi', | ||
| onAppMention: dispatchSlackMessage, | ||
| onDirectMessage: dispatchSlackMessage, | ||
| events: { | ||
| async 'session.failed'(event, _channel) { | ||
| // DM + @mention (or any double dispatch on the same thread) races on one | ||
| // continuation token — the winning run already handles the user message. | ||
| if (isHookConflictFailure(event)) return | ||
|
|
||
| await _channel.thread.post( | ||
| 'Something went wrong and I cannot continue in this thread. Start a new thread to try again.' | ||
| ) | ||
| } | ||
| } | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| import { defineMcpClientConnection } from 'eve/connections' | ||
| import { appOrigin } from '../lib/internal-api.js' | ||
|
|
||
| export default defineMcpClientConnection({ | ||
| url: `${appOrigin()}/mcp`, | ||
| description: 'Nuxt.com documentation, blog, modules catalog, deploy providers, and changelog.' | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| import { defineHook } from 'eve/hooks' | ||
| import { appOrigin, chatIdFromContinuationToken, internalHeaders } from '../lib/internal-api.js' | ||
|
|
||
| export default defineHook({ | ||
| events: { | ||
| async 'message.received'(event, ctx) { | ||
| const chatId = chatIdFromContinuationToken(ctx.channel.continuationToken) | ||
| const auth = ctx.session.auth.current | ||
| if (!chatId || !auth || auth.principalType !== 'user') return | ||
|
|
||
| const message = event.data.message | ||
| if (!message?.trim()) return | ||
|
|
||
| try { | ||
| const response = await fetch(`${appOrigin()}/api/internal/chats/${encodeURIComponent(chatId)}/title`, { | ||
| method: 'POST', | ||
| headers: internalHeaders(), | ||
| body: JSON.stringify({ | ||
| userId: auth.principalId, | ||
| message: { | ||
| role: 'user', | ||
| parts: [{ type: 'text', text: message }] | ||
| } | ||
| }) | ||
| }) | ||
|
|
||
| if (!response.ok) { | ||
| const text = await response.text().catch(() => '') | ||
| console.warn(`[chat-title] title generation failed (${response.status}): ${text}`) | ||
| } | ||
| } catch (error) { | ||
| console.warn('[chat-title] title generation request errored', error) | ||
| } | ||
| } | ||
| } | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| import { defineHook } from 'eve/hooks' | ||
| import { appOrigin, internalHeaders } from '../lib/internal-api.js' | ||
|
|
||
| type TurnStartedContext = { | ||
| session: { | ||
| auth: { | ||
| current?: { | ||
| principalId?: string | ||
| } | null | ||
| } | ||
| } | ||
| eve?: { | ||
| request?: Request | ||
| } | ||
| } | ||
|
|
||
| export default defineHook({ | ||
| events: { | ||
| async 'turn.started'(_event, ctx) { | ||
| const hookCtx = ctx as TurnStartedContext | ||
| const principalId = hookCtx.session.auth.current?.principalId | ||
| if (!principalId) return | ||
|
|
||
| const cookie = hookCtx.eve?.request?.headers.get('cookie') ?? '' | ||
| const response = await fetch(`${appOrigin()}/api/internal/agent/rate-limit/consume`, { | ||
| method: 'POST', | ||
| headers: internalHeaders(cookie ? { cookie } : undefined), | ||
| body: JSON.stringify({ userId: principalId }) | ||
| }) | ||
|
|
||
| if (response.status === 429) { | ||
| const data = await response.json().catch(() => ({})) as { message?: string } | ||
| throw new Error(data.message ?? 'Daily message limit reached.') | ||
| } | ||
|
|
||
| if (!response.ok) { | ||
| const text = await response.text() | ||
| throw new Error(`Rate limit check failed: ${text}`) | ||
| } | ||
| } | ||
| } | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import { defineDynamic, defineInstructions } from 'eve/instructions' | ||
| import { ADMIN_MCP_INSTRUCTIONS, canAccessAdminMcp } from './tools/admin-mcp.js' | ||
| import { buildInstructionsWithDate } from './lib/base-instructions.js' | ||
|
|
||
| export default defineDynamic({ | ||
| events: { | ||
| 'session.started': async (_event, ctx) => { | ||
| const auth = ctx.session.auth.current | ||
| const markdown = canAccessAdminMcp(auth) | ||
| ? `${buildInstructionsWithDate()}\n\n${ADMIN_MCP_INSTRUCTIONS}` | ||
| : buildInstructionsWithDate() | ||
|
|
||
| return defineInstructions({ markdown }) | ||
| } | ||
| } | ||
| }) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.