mirror of
https://github.com/supermemoryai/supermemory.git
synced 2026-08-28 05:25:33 +00:00
notify agent after widget actions
This commit is contained in:
parent
bdef38398e
commit
fea6e2bba4
4 changed files with 41 additions and 56 deletions
|
|
@ -48,19 +48,19 @@ export function useApp() {
|
|||
},
|
||||
|
||||
/** Inject a user message into the conversation. Triggers a new agent turn. */
|
||||
sendMessage(content: string) {
|
||||
return app.sendMessage({
|
||||
role: "user",
|
||||
content: [{ type: "text", text: content }],
|
||||
})
|
||||
},
|
||||
|
||||
/** Update ambient model context. Model sees on next user message. */
|
||||
updateContext(text: string, structuredContent?: Record<string, unknown>) {
|
||||
return app.updateModelContext({
|
||||
content: [{ type: "text", text }],
|
||||
structuredContent,
|
||||
})
|
||||
async sendMessage(content: string): Promise<ToolCallResult> {
|
||||
try {
|
||||
const result = await app.sendMessage({
|
||||
role: "user",
|
||||
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) }
|
||||
}
|
||||
},
|
||||
|
||||
/** Send a structured log line to the host. */
|
||||
|
|
|
|||
|
|
@ -28,7 +28,7 @@ export function Picker({
|
|||
onAdvance,
|
||||
onError,
|
||||
}: Props) {
|
||||
const { callTool, updateContext } = useApp()
|
||||
const { callTool, sendMessage } = useApp()
|
||||
const log = useLog()
|
||||
const [pending, setPending] = useState<string | null>(null)
|
||||
const [query, setQuery] = useState("")
|
||||
|
|
@ -55,20 +55,13 @@ export function Picker({
|
|||
onError(result.error ?? "Failed to set active workspace")
|
||||
return
|
||||
}
|
||||
try {
|
||||
await updateContext(
|
||||
`The user selected the Supermemory workspace "${containerTag}". Use this as the active workspace for future Supermemory actions unless the user selects another workspace.`,
|
||||
{
|
||||
supermemory: {
|
||||
activeWorkspace: containerTag,
|
||||
lastAction: "workspace-selected",
|
||||
},
|
||||
},
|
||||
)
|
||||
} catch (error) {
|
||||
log("warning", `[picker] model context update failed: ${error}`)
|
||||
}
|
||||
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.`,
|
||||
)
|
||||
if (!notification.ok) {
|
||||
log("warning", `[picker] agent notification failed: ${notification.error}`)
|
||||
}
|
||||
}
|
||||
|
||||
const count = containerTags.length
|
||||
|
|
|
|||
|
|
@ -27,7 +27,7 @@ export function Save({
|
|||
onAdvance,
|
||||
onError,
|
||||
}: Props) {
|
||||
const { callTool, updateContext } = useApp()
|
||||
const { callTool, sendMessage } = useApp()
|
||||
const log = useLog()
|
||||
const [content, setContent] = useState(prefill ?? "")
|
||||
const [selectedTag, setSelectedTag] = useState<string | null>(
|
||||
|
|
@ -63,20 +63,15 @@ export function Save({
|
|||
onError(result.error ?? "Failed to save memory")
|
||||
return
|
||||
}
|
||||
try {
|
||||
await updateContext(
|
||||
`The user saved a memory to the Supermemory workspace "${selectedTag}" from the interactive widget.`,
|
||||
{
|
||||
supermemory: {
|
||||
activeWorkspace: selectedTag,
|
||||
lastAction: "memory-saved",
|
||||
},
|
||||
},
|
||||
)
|
||||
} catch (error) {
|
||||
log("warning", `[save] model context update failed: ${error}`)
|
||||
}
|
||||
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.`,
|
||||
)
|
||||
if (!notification.ok) {
|
||||
log("warning", `[save] agent notification failed: ${notification.error}`)
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
|
|
|
|||
|
|
@ -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, updateContext } = useApp()
|
||||
const { callTool, sendMessage } = useApp()
|
||||
const log = useLog()
|
||||
const [file, setFile] = useState<File | null>(null)
|
||||
const [selectedTag, setSelectedTag] = useState<string | null>(
|
||||
|
|
@ -62,21 +62,18 @@ export function Upload({ activeTag, writableTags, onAdvance, onError }: Props) {
|
|||
onError(result.error ?? "Upload failed")
|
||||
return
|
||||
}
|
||||
try {
|
||||
await updateContext(
|
||||
`The user uploaded "${file.name}" to the Supermemory workspace "${selectedTag}" from the interactive widget.`,
|
||||
{
|
||||
supermemory: {
|
||||
activeWorkspace: selectedTag,
|
||||
lastAction: "file-uploaded",
|
||||
fileName: file.name,
|
||||
},
|
||||
},
|
||||
)
|
||||
} catch (error) {
|
||||
log("warning", `[upload] model context update failed: ${error}`)
|
||||
}
|
||||
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.`,
|
||||
)
|
||||
if (!notification.ok) {
|
||||
log(
|
||||
"warning",
|
||||
`[upload] agent notification failed: ${notification.error}`,
|
||||
)
|
||||
}
|
||||
} catch (err) {
|
||||
log("error", `[upload] threw: ${err}`)
|
||||
onError(String(err))
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue