-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat(ai): defer marimo-pair references and improve code mode prompts #9985
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,7 +7,7 @@ import { | |
| startCompletion, | ||
| } from "@codemirror/autocomplete"; | ||
| import type { ReactCodeMirrorRef } from "@uiw/react-codemirror"; | ||
| import type { FileUIPart } from "ai"; | ||
| import type { DataUIPart, FileUIPart, UIMessage } from "ai"; | ||
| import { getAIContextRegistry } from "@/core/ai/context/context"; | ||
| import { getCodes } from "@/core/codemirror/copilot/getCodes"; | ||
| import type { LanguageAdapterType } from "@/core/codemirror/language/types"; | ||
|
|
@@ -50,6 +50,91 @@ export function getAICompletionBody({ | |
| }; | ||
| } | ||
|
|
||
| export interface MarimoContextData { | ||
| plainText: string; | ||
| contextIds: string[]; | ||
| } | ||
|
|
||
| export type MarimoContextUIPart = DataUIPart<{ | ||
| "marimo-context": MarimoContextData; | ||
| }>; | ||
|
|
||
| /** | ||
| * Wire `type` of the @-context data part. Must match | ||
| * `MARIMO_CONTEXT_PART_TYPE` on the backend. | ||
| */ | ||
| export const MARIMO_CONTEXT_PART_TYPE = | ||
| "data-marimo-context" as const satisfies MarimoContextUIPart["type"]; | ||
|
|
||
| export interface ResolvedChatContext { | ||
| contextPart: MarimoContextUIPart | null; | ||
| attachments: FileUIPart[]; | ||
| } | ||
|
|
||
| /** | ||
| * Marker stamped onto attachments derived from @-context (as opposed to files | ||
| * the user uploaded directly). | ||
| */ | ||
| const CONTEXT_ATTACHMENT_METADATA = { | ||
| marimo: { source: "context" }, | ||
| } as const; | ||
|
|
||
| /** Whether a part is an attachment that was derived from @-context. */ | ||
| export function isContextAttachment(part: UIMessage["parts"][number]): boolean { | ||
| return ( | ||
| part.type === "file" && | ||
| part.providerMetadata?.marimo?.source === | ||
| CONTEXT_ATTACHMENT_METADATA.marimo.source | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Resolve @-context for messages. They represent referenced | ||
| * datasets, variables, or other context from the user's prompt. | ||
| */ | ||
| export async function resolveChatContext( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2: New Prompt for AI agents |
||
| input: string, | ||
| ): Promise<ResolvedChatContext> { | ||
| if (!input.includes(CONTEXT_TRIGGER)) { | ||
| return { contextPart: null, attachments: [] }; | ||
| } | ||
|
|
||
| const registry = getAIContextRegistry(store); | ||
| const contextIds = registry.parseAllContextIds(input); | ||
| if (contextIds.length === 0) { | ||
| return { contextPart: null, attachments: [] }; | ||
| } | ||
|
|
||
| const plainText = registry.formatContextForAI(contextIds); | ||
|
|
||
| let attachments: FileUIPart[] = []; | ||
| try { | ||
| const resolved = await registry.getAttachmentsForContext(contextIds); | ||
| attachments = resolved.map((attachment) => ({ | ||
| ...attachment, | ||
| providerMetadata: { | ||
| ...attachment.providerMetadata, | ||
| marimo: { | ||
| ...attachment.providerMetadata?.marimo, | ||
| ...CONTEXT_ATTACHMENT_METADATA.marimo, | ||
| }, | ||
| }, | ||
| })); | ||
| } catch (error) { | ||
| Logger.error("Error getting attachments:", error); | ||
| } | ||
|
|
||
| let contextPart: MarimoContextUIPart | null = null; | ||
| if (plainText.trim()) { | ||
| contextPart = { | ||
| type: MARIMO_CONTEXT_PART_TYPE, | ||
| data: { plainText, contextIds: contextIds.map(String) }, | ||
| }; | ||
| } | ||
|
|
||
| return { contextPart, attachments }; | ||
| } | ||
|
|
||
| /** | ||
| * Gets the request body and attachments for the AI completion API. | ||
| */ | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P2: Potential loss of legacy context resolution for old persisted chats after removing
buildCompletionRequestBody. The old helper re-scanned all historical message text for@references on every completion request (regenerate/retry). The new code only preserves context for messages that already contain the newdata-marimo-contextpart. Old persisted chats without that part will silently lose@context on regeneration.Prompt for AI agents