-
Notifications
You must be signed in to change notification settings - Fork 17
Learning engagement context and Nina harness #187
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
694a878
Implement learning engagement context and Nina harness
nabilfatih 856fdac
Address learning engagement review gates
nabilfatih f0e6f1b
Prove pinned Nina context reuse
nabilfatih 1e7ac11
Refactor Nina harness around Effect services
nabilfatih 2534043
Fix pinned Nina context and popularity dedupe
nabilfatih ba62bbb
Refine specialist agent contracts
nabilfatih d9bc537
Fix Nina suggestions and review blockers
nabilfatih 5c5adbd
Refine Nina capability harness
nabilfatih 46a7e85
Fix Nina runtime and engagement review blockers
nabilfatih 207a461
Fix Nina rewrite context boundaries
nabilfatih 09d666c
Fix learning view identity ownership
nabilfatih 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
43 changes: 0 additions & 43 deletions
43
...ocale]/(app)/(shared)/(main)/(learn)/materials/[subject]/[topic]/[[...lesson]]/layout.tsx
This file was deleted.
Oops, something went wrong.
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,167 @@ | ||
| import { | ||
| type NinaAgentChat as CoreNinaAgentChat, | ||
| type NinaAgentRuntime as CoreNinaAgentRuntime, | ||
| type NinaAgentUser as CoreNinaAgentUser, | ||
| createNinaAgentContext, | ||
| type NinaAgentPage, | ||
| runNinaAgentTurn, | ||
| } from "@repo/ai/nina/agent"; | ||
| import type { MyUIMessage } from "@repo/ai/types/message"; | ||
| import type { Id } from "@repo/backend/convex/_generated/dataModel"; | ||
| import type { LogContext } from "@repo/utilities/logging/types"; | ||
| import type { UIMessageStreamWriter } from "ai"; | ||
| import { Effect } from "effect"; | ||
| import type { getTranslations } from "next-intl/server"; | ||
| import { recoverChatToolCall } from "@/app/api/chat/recovery"; | ||
| import { prepareChatStep } from "@/app/api/chat/step"; | ||
| import { writeSuggestions } from "@/app/api/chat/suggestions"; | ||
| import { createNinaToolSet } from "@/app/api/chat/tools"; | ||
| import { trackUsage } from "@/app/api/chat/usage"; | ||
| import type { getLearningProfile, getUserInfo } from "@/app/api/chat/utils"; | ||
|
|
||
| type Translator = Awaited<ReturnType<typeof getTranslations>>; | ||
| type LearningProfile = Effect.Effect.Success< | ||
| ReturnType<typeof getLearningProfile> | ||
| >; | ||
| type UserInfo = Effect.Effect.Success<ReturnType<typeof getUserInfo>>; | ||
|
|
||
| /** Prepared chat state needed by Nina's ToolLoopAgent turn. */ | ||
| export interface NinaAgentChat extends CoreNinaAgentChat { | ||
| readonly id: Id<"chats">; | ||
| } | ||
|
|
||
| /** Runtime services and callbacks for Nina's ToolLoopAgent turn. */ | ||
| export interface NinaAgentRuntime extends CoreNinaAgentRuntime { | ||
| readonly logContext: LogContext; | ||
| readonly reportError: (error: unknown, source: string) => void; | ||
| readonly translate: Translator; | ||
| } | ||
|
|
||
| /** User context exposed to Nina's ToolLoopAgent turn. */ | ||
| export interface NinaAgentUser | ||
| extends Omit<CoreNinaAgentUser, "learningProfile" | "role"> { | ||
| readonly info: UserInfo; | ||
| readonly learningProfile: LearningProfile; | ||
| } | ||
|
|
||
| /** Inputs for the app-bound Nina ToolLoopAgent execution seam. */ | ||
| export interface StreamNinaAgentInput { | ||
| readonly chat: NinaAgentChat; | ||
| readonly onStreamError: (error: unknown, source: string) => void; | ||
| readonly page: NinaAgentPage; | ||
| readonly runtime: NinaAgentRuntime; | ||
| readonly user: NinaAgentUser; | ||
| readonly writer: UIMessageStreamWriter<MyUIMessage>; | ||
| } | ||
|
|
||
| /** Converts app auth/profile rows into the package-owned Nina user context. */ | ||
| function createCoreNinaUser(user: NinaAgentUser): CoreNinaAgentUser { | ||
| return { | ||
| learningProfile: user.learningProfile ?? undefined, | ||
| location: user.location, | ||
| role: user.info.role ?? undefined, | ||
| }; | ||
| } | ||
|
|
||
| /** Formats AI SDK stream errors while preserving server-side diagnostics. */ | ||
| function formatNinaStreamError({ | ||
| error, | ||
| runtime, | ||
| }: { | ||
| readonly error: unknown; | ||
| readonly runtime: NinaAgentRuntime; | ||
| }) { | ||
| if (error instanceof Error) { | ||
| if (error.message.includes("Rate limit")) { | ||
| Effect.runFork( | ||
| Effect.logWarning("Rate limit exceeded in message stream").pipe( | ||
| Effect.annotateLogs(runtime.logContext) | ||
| ) | ||
| ); | ||
| return runtime.translate("rate-limit-message"); | ||
| } | ||
|
|
||
| return error.message; | ||
| } | ||
|
|
||
| Effect.runFork( | ||
| Effect.logError("Unknown error in message stream").pipe( | ||
| Effect.annotateLogs(runtime.logContext) | ||
| ) | ||
| ); | ||
| return runtime.translate("error-message"); | ||
| } | ||
|
|
||
| /** Streams one Nina ToolLoopAgent turn into the AI SDK UI writer. */ | ||
| export const streamNinaAgent = Effect.fn("chat.streamNinaAgent")(function* ({ | ||
| chat, | ||
| onStreamError, | ||
| page, | ||
| runtime, | ||
| user, | ||
| writer, | ||
| }: StreamNinaAgentInput) { | ||
| const usage = yield* trackUsage(); | ||
| const coreUser = createCoreNinaUser(user); | ||
| const context = createNinaAgentContext({ | ||
| page, | ||
| runtime, | ||
| user: coreUser, | ||
| }); | ||
|
|
||
| const turn = yield* runNinaAgentTurn({ | ||
| adapter: { | ||
| formatStreamError: (error) => formatNinaStreamError({ error, runtime }), | ||
| onStreamError, | ||
| prepareStep: ({ messages, stepNumber, system }) => | ||
| Effect.runSync( | ||
| prepareChatStep({ | ||
| messages, | ||
| needsPageFetch: page.needsFetch, | ||
| stepNumber, | ||
| system, | ||
| }) | ||
| ), | ||
| readFinishMetadata: (mainUsage) => | ||
| Effect.runSync( | ||
| usage.metadata({ | ||
| mainUsage, | ||
| modelId: runtime.modelId, | ||
| }) | ||
| ), | ||
| repairToolCall: (options) => | ||
| Effect.runPromise( | ||
| recoverChatToolCall({ | ||
| ...options, | ||
| needsPageFetch: context.needsPageFetch, | ||
| sessionLogger: runtime.logContext, | ||
| url: page.url, | ||
| }) | ||
| ), | ||
| tools: createNinaToolSet({ | ||
| context, | ||
| locale: page.locale, | ||
| logContext: runtime.logContext, | ||
| modelId: runtime.modelId, | ||
| reportError: runtime.reportError, | ||
| usage, | ||
| writer, | ||
| }), | ||
| writer, | ||
| }, | ||
| chat, | ||
| page, | ||
| runtime, | ||
| user: coreUser, | ||
| }); | ||
|
|
||
| yield* writeSuggestions({ | ||
| locale: page.locale, | ||
| messages: [...chat.finalMessages, ...turn.messages], | ||
| writer, | ||
| }).pipe( | ||
| Effect.catchAll((error) => | ||
| Effect.sync(() => runtime.reportError(error, "writeSuggestions")) | ||
| ) | ||
| ); | ||
| }); | ||
Oops, something went wrong.
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.