use model context for widget actions

This commit is contained in:
Prasanna A P 2026-07-21 01:58:55 -07:00
parent c98a3b59c5
commit 2e8244f78d
5 changed files with 23 additions and 24 deletions

View file

@ -124,5 +124,5 @@ export type Props = {
containerTag?: string
}
// MCP resource URI for the widget bundle.
export const SUPERMEMORY_RESOURCE_URI = "ui://supermemory/app.html"
// Hosts cache MCP UI resources by URI, so bump this when shipping a new widget bundle.
export const SUPERMEMORY_RESOURCE_URI = "ui://supermemory/app-v2.html"

View file

@ -47,16 +47,12 @@ export function useApp() {
}
},
/** Inject a user message into the conversation. Triggers a new agent turn. */
async sendMessage(content: string): Promise<ToolCallResult> {
/** Make widget state available to the model on a future turn. */
async updateModelContext(content: string): Promise<ToolCallResult> {
try {
const result = await app.sendMessage({
role: "user",
await app.updateModelContext({
content: [{ type: "text", text: content }],
})
if (result.isError) {
return { ok: false, error: "Host rejected the message" }
}
return { ok: true }
} catch (err) {
return { ok: false, error: String(err) }

View file

@ -28,7 +28,7 @@ export function Picker({
onAdvance,
onError,
}: Props) {
const { callTool, sendMessage } = useApp()
const { callTool, updateModelContext } = useApp()
const log = useLog()
const [pending, setPending] = useState<string | null>(null)
const [query, setQuery] = useState("")
@ -56,13 +56,13 @@ export function Picker({
return
}
onAdvance(result.data)
const notification = await sendMessage(
`I selected "${containerTag}" as my active Supermemory workspace. Use this workspace for future Supermemory actions until I select another one.`,
const contextUpdate = await updateModelContext(
`Supermemory workspace selection changed. Active workspace: "${containerTag}". Use it for future Supermemory actions until another workspace is selected.`,
)
if (!notification.ok) {
if (!contextUpdate.ok) {
log(
"warning",
`[picker] agent notification failed: ${notification.error}`,
`[picker] model context update failed: ${contextUpdate.error}`,
)
}
}

View file

@ -27,7 +27,7 @@ export function Save({
onAdvance,
onError,
}: Props) {
const { callTool, sendMessage } = useApp()
const { callTool, updateModelContext } = useApp()
const log = useLog()
const [content, setContent] = useState(prefill ?? "")
const [selectedTag, setSelectedTag] = useState<string | null>(
@ -66,11 +66,14 @@ export function Save({
const memoryId =
result.data.view === "save-success" ? result.data.id : undefined
onAdvance(result.data)
const notification = await sendMessage(
`I used the Supermemory widget to save a memory to workspace "${selectedTag}"${memoryId ? ` (memory ID: ${memoryId})` : ""}. The memory is already saved; do not save it again.`,
const contextUpdate = await updateModelContext(
`Supermemory widget action completed. A memory was saved to workspace "${selectedTag}"${memoryId ? ` with memory ID "${memoryId}"` : ""}. It is already saved; do not save it again.`,
)
if (!notification.ok) {
log("warning", `[save] agent notification failed: ${notification.error}`)
if (!contextUpdate.ok) {
log(
"warning",
`[save] model context update failed: ${contextUpdate.error}`,
)
}
}

View file

@ -30,7 +30,7 @@ function formatFileSize(bytes: number): string {
const ACCEPT = ".txt,.pdf,.png,.jpg,.jpeg,.mp4"
export function Upload({ activeTag, writableTags, onAdvance, onError }: Props) {
const { callTool, sendMessage } = useApp()
const { callTool, updateModelContext } = useApp()
const log = useLog()
const [file, setFile] = useState<File | null>(null)
const [selectedTag, setSelectedTag] = useState<string | null>(
@ -65,13 +65,13 @@ export function Upload({ activeTag, writableTags, onAdvance, onError }: Props) {
const documentId =
result.data.view === "upload-success" ? result.data.id : undefined
onAdvance(result.data)
const notification = await sendMessage(
`I used the Supermemory widget to upload "${file.name}" to workspace "${selectedTag}"${documentId ? ` (document ID: ${documentId})` : ""}. The file is already uploaded; do not upload it again.`,
const contextUpdate = await updateModelContext(
`Supermemory widget action completed. "${file.name}" was uploaded to workspace "${selectedTag}"${documentId ? ` with document ID "${documentId}"` : ""}. It is already uploaded; do not upload it again.`,
)
if (!notification.ok) {
if (!contextUpdate.ok) {
log(
"warning",
`[upload] agent notification failed: ${notification.error}`,
`[upload] model context update failed: ${contextUpdate.error}`,
)
}
} catch (err) {