fix(app): notifications on child sessions
This commit is contained in:
@@ -69,7 +69,7 @@ export const { use: useNotification, provider: NotificationProvider } = createSi
|
|||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
|
|
||||||
const meta = { pruned: false }
|
const meta = { pruned: false, disposed: false }
|
||||||
|
|
||||||
createEffect(() => {
|
createEffect(() => {
|
||||||
if (!ready()) return
|
if (!ready()) return
|
||||||
@@ -84,6 +84,17 @@ export const { use: useNotification, provider: NotificationProvider } = createSi
|
|||||||
|
|
||||||
const index = createMemo(() => buildNotificationIndex(store.list))
|
const index = createMemo(() => buildNotificationIndex(store.list))
|
||||||
|
|
||||||
|
const lookup = (directory: string, sessionID?: string) => {
|
||||||
|
if (!sessionID) return Promise.resolve(undefined)
|
||||||
|
const [syncStore] = globalSync.child(directory, { bootstrap: false })
|
||||||
|
const match = Binary.search(syncStore.session, sessionID, (s) => s.id)
|
||||||
|
if (match.found) return Promise.resolve(syncStore.session[match.index])
|
||||||
|
return globalSDK.client.session
|
||||||
|
.get({ directory, sessionID })
|
||||||
|
.then((x) => x.data)
|
||||||
|
.catch(() => undefined)
|
||||||
|
}
|
||||||
|
|
||||||
const unsub = globalSDK.event.listen((e) => {
|
const unsub = globalSDK.event.listen((e) => {
|
||||||
const event = e.details
|
const event = e.details
|
||||||
if (event.type !== "session.idle" && event.type !== "session.error") return
|
if (event.type !== "session.idle" && event.type !== "session.error") return
|
||||||
@@ -102,61 +113,65 @@ export const { use: useNotification, provider: NotificationProvider } = createSi
|
|||||||
switch (event.type) {
|
switch (event.type) {
|
||||||
case "session.idle": {
|
case "session.idle": {
|
||||||
const sessionID = event.properties.sessionID
|
const sessionID = event.properties.sessionID
|
||||||
const [syncStore] = globalSync.child(directory, { bootstrap: false })
|
void lookup(directory, sessionID).then((session) => {
|
||||||
const match = Binary.search(syncStore.session, sessionID, (s) => s.id)
|
if (meta.disposed) return
|
||||||
const session = match.found ? syncStore.session[match.index] : undefined
|
if (!session) return
|
||||||
if (session?.parentID) break
|
if (session.parentID) return
|
||||||
|
|
||||||
playSound(soundSrc(settings.sounds.agent()))
|
playSound(soundSrc(settings.sounds.agent()))
|
||||||
|
|
||||||
append({
|
append({
|
||||||
directory,
|
directory,
|
||||||
time,
|
time,
|
||||||
viewed: viewed(sessionID),
|
viewed: viewed(sessionID),
|
||||||
type: "turn-complete",
|
type: "turn-complete",
|
||||||
session: sessionID,
|
session: sessionID,
|
||||||
|
})
|
||||||
|
|
||||||
|
const href = `/${base64Encode(directory)}/session/${sessionID}`
|
||||||
|
if (settings.notifications.agent()) {
|
||||||
|
void platform.notify(
|
||||||
|
language.t("notification.session.responseReady.title"),
|
||||||
|
session.title ?? sessionID,
|
||||||
|
href,
|
||||||
|
)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
const href = `/${base64Encode(directory)}/session/${sessionID}`
|
|
||||||
if (settings.notifications.agent()) {
|
|
||||||
void platform.notify(
|
|
||||||
language.t("notification.session.responseReady.title"),
|
|
||||||
session?.title ?? sessionID,
|
|
||||||
href,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
case "session.error": {
|
case "session.error": {
|
||||||
const sessionID = event.properties.sessionID
|
const sessionID = event.properties.sessionID
|
||||||
const [syncStore] = globalSync.child(directory, { bootstrap: false })
|
void lookup(directory, sessionID).then((session) => {
|
||||||
const match = sessionID ? Binary.search(syncStore.session, sessionID, (s) => s.id) : undefined
|
if (meta.disposed) return
|
||||||
const session = sessionID && match?.found ? syncStore.session[match.index] : undefined
|
if (session?.parentID) return
|
||||||
if (session?.parentID) break
|
|
||||||
|
|
||||||
playSound(soundSrc(settings.sounds.errors()))
|
playSound(soundSrc(settings.sounds.errors()))
|
||||||
|
|
||||||
const error = "error" in event.properties ? event.properties.error : undefined
|
const error = "error" in event.properties ? event.properties.error : undefined
|
||||||
append({
|
append({
|
||||||
directory,
|
directory,
|
||||||
time,
|
time,
|
||||||
viewed: viewed(sessionID),
|
viewed: viewed(sessionID),
|
||||||
type: "error",
|
type: "error",
|
||||||
session: sessionID ?? "global",
|
session: sessionID ?? "global",
|
||||||
error,
|
error,
|
||||||
|
})
|
||||||
|
const description =
|
||||||
|
session?.title ??
|
||||||
|
(typeof error === "string" ? error : language.t("notification.session.error.fallbackDescription"))
|
||||||
|
const href = sessionID ? `/${base64Encode(directory)}/session/${sessionID}` : `/${base64Encode(directory)}`
|
||||||
|
if (settings.notifications.errors()) {
|
||||||
|
void platform.notify(language.t("notification.session.error.title"), description, href)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
const description =
|
|
||||||
session?.title ??
|
|
||||||
(typeof error === "string" ? error : language.t("notification.session.error.fallbackDescription"))
|
|
||||||
const href = sessionID ? `/${base64Encode(directory)}/session/${sessionID}` : `/${base64Encode(directory)}`
|
|
||||||
if (settings.notifications.errors()) {
|
|
||||||
void platform.notify(language.t("notification.session.error.title"), description, href)
|
|
||||||
}
|
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
onCleanup(unsub)
|
onCleanup(() => {
|
||||||
|
meta.disposed = true
|
||||||
|
unsub()
|
||||||
|
})
|
||||||
|
|
||||||
return {
|
return {
|
||||||
ready,
|
ready,
|
||||||
|
|||||||
Reference in New Issue
Block a user