mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-11-17 04:11:35 +00:00
Adapted from https://github.com/NousResearch/hermes-agent/pull/51893 (a70b0991b2).
Co-authored-by: brooklyn! <[email protected]>
29 lines
827 B
TypeScript
29 lines
827 B
TypeScript
function isActionIndex(value: unknown): value is number {
|
|
return typeof value === 'number' && Number.isInteger(value)
|
|
}
|
|
|
|
function notificationActionIndex(actionEvent: unknown, legacyIndex: unknown): number | null {
|
|
if (actionEvent && typeof actionEvent === 'object' && 'actionIndex' in actionEvent) {
|
|
const { actionIndex } = actionEvent as { actionIndex?: unknown }
|
|
|
|
if (isActionIndex(actionIndex)) {
|
|
return actionIndex
|
|
}
|
|
}
|
|
|
|
return isActionIndex(legacyIndex) ? legacyIndex : null
|
|
}
|
|
|
|
export function resolveNotificationAction<T>(
|
|
actions: readonly T[],
|
|
actionEvent: unknown,
|
|
legacyIndex: unknown
|
|
): T | null {
|
|
const index = notificationActionIndex(actionEvent, legacyIndex)
|
|
|
|
if (index === null || index < 0 || index >= actions.length) {
|
|
return null
|
|
}
|
|
|
|
return actions[index] ?? null
|
|
}
|