sigit-issue-30 #1

Merged
y merged 3 commits from sigit-issue-30 into master 2024-05-24 13:15:15 +00:00
6 changed files with 106 additions and 21 deletions
Showing only changes of commit d67d53b172 - Show all commits

View File

@ -64,8 +64,8 @@ importers:
specifier: ~29.5
version: 29.5.0(@types/node@18.16.19)
prettier:
specifier: ~2.8
version: 2.8.8
specifier: 3.2.5
version: 3.2.5
rimraf:
specifier: ~5.0
version: 5.0.1
@ -2171,9 +2171,9 @@ packages:
resolution: {integrity: sha512-vkcDPrRZo1QZLbn5RLGPpg/WmIQ65qoWWhcGKf/b5eplkkarX0m9z8ppCat4mlOqUsWpyNuYgO3VRyrYHSzX5g==}
engines: {node: '>= 0.8.0'}
prettier@2.8.8:
resolution: {integrity: sha512-tdN8qQGvNjw4CHbY+XXk0JgCXn9QiF21a55rBe5LJAU+kDyC4WQn4+awm2Xfk2lQMk5fKup9XgzTZtGkjBdP9Q==}
engines: {node: '>=10.13.0'}
prettier@3.2.5:
resolution: {integrity: sha512-3/GWa9aOC0YeD7LUfvOG2NiDyhOWRvt1k+rcKhOuYnMY24iiCphgneUfJDyFXd6rZCAnuLBv6UeAULtrhT/F4A==}
engines: {node: '>=14'}
hasBin: true
pretty-format@29.6.1:
@ -5246,7 +5246,7 @@ snapshots:
prelude-ls@1.2.1: {}
prettier@2.8.8: {}
prettier@3.2.5: {}
pretty-format@29.6.1:
dependencies:

View File

@ -0,0 +1,49 @@
import { NDKEvent } from '@nostr-dev-kit/ndk'
import { log } from '../main.js'
import axios from 'axios'
import { RelayInfo } from '../types/index.js'
export const relayInfoJob = async (event: NDKEvent): Promise<string> => {
log('New relay-info job', event.rawEvent())
const input: string = event.tagValue('i')
let relays: string[] = []
try {
const relaysArr = JSON.parse(input)
// input is a string containing an array of strings representing multiple relay URIs
relays = relaysArr
} catch (err) {
// input is a string containing a string representing single relay URI
relays.push(input)
}
const wssPrefix = 'wss://'
const headers = {
headers: { Accept: 'application/nostr+json' }
}
const requests = relays.map((relay) => {
if (relay.startsWith(wssPrefix)) {
relay = relay.replace(wssPrefix, '')
}
return axios
.get<RelayInfo>((relay = `https://${relay}`), headers)
.catch(() => {
return axios
.get<RelayInfo>(`http://${relay}`, headers)
.catch(() => undefined)
})
})
let responses = await Promise.all(requests)
responses = responses.filter((response) => response !== undefined)
const data = responses.map((response) => response.data)
return Promise.resolve(JSON.stringify(data))
}

View File

@ -11,6 +11,8 @@ import { getConfig } from './config/index.js'
import { decode } from 'light-bolt11-decoder'
import { checkInvoiceStatus } from './utils/lnbits.js'
import { blockChainBlockNumberJob } from './job-types/blockChain-block-number.js'
import { relayInfoJob } from './job-types/relay-info.js'
import { JobType, JobTypes } from './types/index.js'
export const log = debug('fool-me-once-dvm')
@ -34,7 +36,7 @@ const subs = ndk.subscribe(
{
kinds: [68001 as number],
since: Math.floor(Date.now() / 1000),
'#j': ['summarize', 'explain']
'#j': [JobTypes.Summarize, JobTypes.Explain]
},
{ closeOnEose: false }
)
@ -43,7 +45,7 @@ const speechToTextSub = ndk.subscribe(
{
kinds: [68001 as number],
since: Math.floor(Date.now() / 1000),
'#j': ['speech-to-text']
'#j': [JobTypes.SpeechToText]
},
{ closeOnEose: false }
)
@ -52,26 +54,31 @@ const blockChainBlockNumberSub = ndk.subscribe(
{
kinds: [68001 as number],
since: Math.floor(Date.now() / 1000),
'#j': ['blockChain-block-number']
'#j': [JobTypes.BlockChainBlockNumber]
},
{ closeOnEose: false }
)
const relayInfoSub = ndk.subscribe(
{
kinds: [68001 as number],
since: Math.floor(Date.now() / 1000),
'#j': [JobTypes.RelayInfo]
},
{ closeOnEose: false }
)
subs.on('event', (e) => processJobEvent(e, 'summarize'))
speechToTextSub.on('event', (e) => processJobEvent(e, 'speech-to-text'))
subs.on('event', (e) => processJobEvent(e, JobTypes.Summarize))
speechToTextSub.on('event', (e) => processJobEvent(e, JobTypes.SpeechToText))
blockChainBlockNumberSub.on('event', (e) =>
processJobEvent(e, 'blockChain-block-number')
processJobEvent(e, JobTypes.BlockChainBlockNumber)
)
relayInfoSub.on('event', (e) => processJobEvent(e, JobTypes.RelayInfo))
type JobType =
| 'summarize'
| 'explain'
| 'speech-to-text'
| 'blockChain-block-number'
const freeJobs: string[] = [JobTypes.BlockChainBlockNumber, JobTypes.RelayInfo]
async function processJobEvent(event: NDKEvent, type: JobType): Promise<void> {
const config = getConfig()
let jobAmount = type === 'blockChain-block-number' ? 0 : await priceJob(event)
let jobAmount = freeJobs.includes(type) ? 0 : await priceJob(event)
let output: any
let payReqEvent: NDKEvent
let paidAmount = 0
@ -195,18 +202,22 @@ async function processJobEvent(event: NDKEvent, type: JobType): Promise<void> {
await inProgress(event)
switch (type) {
case 'summarize': {
case JobTypes.Summarize: {
output = await onNewSummarizationJob(event)
break
}
case 'speech-to-text': {
case JobTypes.SpeechToText: {
output = await speechToTextJob(event)
break
}
case 'blockChain-block-number': {
case JobTypes.BlockChainBlockNumber: {
output = await blockChainBlockNumberJob(event)
break
}
case JobTypes.RelayInfo: {
output = await relayInfoJob(event)
break
}
}
}

2
src/types/index.ts Normal file
View File

@ -0,0 +1,2 @@
export * from './relay.js'
export * from './job.js'

14
src/types/job.ts Normal file
View File

@ -0,0 +1,14 @@
export type JobType =
| 'summarize'
| 'explain'
| 'speech-to-text'
| 'blockChain-block-number'
| 'relay-info'
export enum JobTypes {
Summarize = 'summarize',
Explain = 'explain',
SpeechToText = 'speech-to-text',
BlockChainBlockNumber = 'blockChain-block-number',
RelayInfo = 'relay-info'
}

9
src/types/relay.ts Normal file
View File

@ -0,0 +1,9 @@
export interface RelayInfo {
name: string
description: string
pubkey: string
contact: string
supported_nips: number[]
software: string
version: string
}