Skip to content

Commit

Permalink
Merge pull request #35 from believethehype/036
Browse files Browse the repository at this point in the history
036
  • Loading branch information
believethehype authored Nov 6, 2024
2 parents ff327e9 + e96b736 commit 93c9571
Show file tree
Hide file tree
Showing 85 changed files with 517 additions and 692 deletions.
5 changes: 2 additions & 3 deletions examples/ollama_dvm/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@ async def nostr_client_test_llm(prompt):
"wss://nostr-pub.wellorder.net"])
alttag = Tag.parse(["alt", "This is a NIP90 DVM AI task to generate TTSt"])
event = EventBuilder(EventDefinitions.KIND_NIP90_GENERATE_TEXT, str("Generate an Audio File."),
[iTag, relaysTag, alttag]).to_event(keys)
[iTag, relaysTag, alttag]).sign_with_keys(keys)

relay_list = ["wss://relay.damus.io", "wss://blastr.f7z.xyz", "wss://relayable.org",
"wss://nostr-pub.wellorder.net"]

signer = NostrSigner.keys(keys)
client = Client(signer)
client = Client(keys)

for relay in relay_list:
await client.add_relay(relay)
Expand Down
8 changes: 3 additions & 5 deletions examples/tts_dvm/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,13 @@ async def nostr_client_test_tts(prompt):
"wss://nostr-pub.wellorder.net"])
alttag = Tag.parse(["alt", "This is a NIP90 DVM AI task to generate TTS"])
event = EventBuilder(EventDefinitions.KIND_NIP90_TEXT_TO_SPEECH, str("Generate an Audio File."),
[iTag, paramTag1, bidTag, relaysTag, alttag]).to_event(keys)
[iTag, paramTag1, bidTag, relaysTag, alttag]).sign_with_keys(keys)

relay_list = ["wss://relay.damus.io", "wss://blastr.f7z.xyz", "wss://relayable.org",
"wss://nostr-pub.wellorder.net"]


signer = NostrSigner.keys(keys)
client = Client(signer)
client = Client(keys)
for relay in relay_list:
await client.add_relay(relay)
await client.connect()
Expand All @@ -45,8 +44,7 @@ async def nostr_client():
sk = keys.secret_key()
pk = keys.public_key()
print(f"Nostr Test Client public key: {pk.to_bech32()}, Hex: {pk.to_hex()} ")
signer = NostrSigner.keys(keys)
client = Client(signer)
client = Client(keys)

dvmconfig = DVMConfig()
for relay in dvmconfig.RELAY_LIST:
Expand Down
9 changes: 3 additions & 6 deletions examples/unleashed_dvm/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,12 @@ async def nostr_client_test(prompt):
"wss://nostr-pub.wellorder.net"])
alttag = Tag.parse(["alt", "This is a NIP90 DVM AI task to generate TTS"])
event = EventBuilder(EventDefinitions.KIND_NIP90_GENERATE_TEXT, str("Answer to prompt"),
[iTag, relaysTag, alttag]).to_event(keys)
[iTag, relaysTag, alttag]).sign_with_keys(keys)

relay_list = ["wss://relay.damus.io", "wss://blastr.f7z.xyz", "wss://relayable.org",
"wss://nostr-pub.wellorder.net"]

opts = (Options().wait_for_send(False).send_timeout(timedelta(seconds=5)))
signer = NostrSigner.keys(keys)
client = Client.with_opts(signer,opts)
client = Client(keys)
for relay in relay_list:
await client.add_relay(relay)
await client.connect()
Expand All @@ -44,8 +42,7 @@ async def nostr_client():
sk = keys.secret_key()
pk = keys.public_key()
print(f"Nostr Test Client public key: {pk.to_bech32()}, Hex: {pk.to_hex()} ")
signer = NostrSigner.keys(keys)
client = Client(signer)
client = Client(keys)

dvmconfig = DVMConfig()
for relay in dvmconfig.RELAY_LIST:
Expand Down
28 changes: 12 additions & 16 deletions nostr_dvm/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,8 @@ async def run_bot(self, dvm_config, admin_config):
self.keys = Keys.parse(dvm_config.PRIVATE_KEY)
self.CHATBOT = False

wait_for_send = True
skip_disconnected_relays = True
opts = (Options().wait_for_send(wait_for_send).send_timeout(timedelta(seconds=self.dvm_config.RELAY_TIMEOUT))
.skip_disconnected_relays(skip_disconnected_relays).gossip(True))
signer = NostrSigner.keys(self.keys)
self.client = Client.with_opts(signer, opts)
opts = (Options().gossip(True))
self.client = Client.with_opts(self.keys, opts)
self.invoice_list = []

pk = self.keys.public_key()
Expand Down Expand Up @@ -127,11 +123,11 @@ async def handle_dm(nostr_event, giftwrap):
if giftwrap:
try:
# Extract rumor
unwrapped_gift = UnwrappedGift.from_gift_wrap(self.keys, nostr_event)

unwrapped_gift = await UnwrappedGift.from_gift_wrap(self.keys, nostr_event)
sender = unwrapped_gift.sender().to_hex()
rumor: UnsignedEvent = unwrapped_gift.rumor()

# client.send_sealed_msg(sender, f"Echo: {msg}", None)
if rumor.created_at().as_secs() >= Timestamp.now().as_secs():
if rumor.kind().as_enum() == KindEnum.PRIVATE_DIRECT_MESSAGE():
print(f"Received new msg [sealed]: {decrypted_text}")
Expand Down Expand Up @@ -198,13 +194,13 @@ async def handle_dm(nostr_event, giftwrap):
# add the encrypted params to the content
nip90request = (EventBuilder(self.dvm_config.SUPPORTED_DVMS[index].KIND,
encrypted_params, [p_tag, encrypted_tag]).
to_event(self.keys))
sign_with_keys(self.keys))
else:
tags.append(p_tag)

nip90request = (EventBuilder(self.dvm_config.SUPPORTED_DVMS[index].KIND,
"", tags).
to_event(self.keys))
sign_with_keys(self.keys))

# remember in the job_list that we have made an event, if anybody asks for payment,
# we know we actually sent the request
Expand Down Expand Up @@ -317,7 +313,7 @@ async def handle_dm(nostr_event, giftwrap):

nip90request = (EventBuilder(Kind(kind),
"", tags).
to_event(self.keys))
sign_with_keys(self.keys))

entry = {"npub": user.npub, "event_id": nip90request.id().to_hex(),
"dvm_key": self.DVM_KEY, "is_paid": False,
Expand All @@ -338,7 +334,7 @@ async def handle_nip90_feedback(nostr_event):
etag = ""
ptag = ""
content = nostr_event.content()
for tag in nostr_event.tags():
for tag in nostr_event.tags().to_vec():
if tag.as_vec()[0] == "status":
status = tag.as_vec()[1]
if len(tag.as_vec()) > 2:
Expand All @@ -362,7 +358,7 @@ async def handle_nip90_feedback(nostr_event):
event_as_json['content'] = ""
nostr_event = Event.from_json(json.dumps(event_as_json))

for tag in nostr_event.tags():
for tag in nostr_event.tags().to_vec():
if tag.as_vec()[0] == "status":
status = tag.as_vec()[1]
if len(tag.as_vec()) > 2:
Expand Down Expand Up @@ -390,7 +386,7 @@ async def handle_nip90_feedback(nostr_event):
"[" + self.NAME + "] Received reaction from " + nostr_event.author().to_hex() + " message to orignal sender " + user.name)

elif status == "payment-required" or status == "partial":
for tag in nostr_event.tags():
for tag in nostr_event.tags().to_vec():
if tag.as_vec()[0] == "amount":
amount_msats = int(tag.as_vec()[1])
amount = int(amount_msats / 1000)
Expand Down Expand Up @@ -469,7 +465,7 @@ async def handle_nip90_response_event(nostr_event: Event):
ptag = ""
etag = ""
is_encrypted = False
for tag in nostr_event.tags():
for tag in nostr_event.tags().to_vec():
if tag.as_vec()[0] == "e":
etag = tag.as_vec()[1]
elif tag.as_vec()[0] == "p":
Expand Down Expand Up @@ -522,7 +518,7 @@ async def handle_zap(zap_event):

etag = ""
if zapped_event is not None:
for tag in zapped_event.tags():
for tag in zapped_event.tags().to_vec():
if tag.as_vec()[0] == "e":
etag = tag.as_vec()[1]

Expand Down
37 changes: 16 additions & 21 deletions nostr_dvm/dvm.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import asyncio
import json
import os
from datetime import timedelta
from sys import platform

from nostr_sdk import PublicKey, Keys, Client, Tag, Event, EventBuilder, Filter, HandleNotification, Timestamp, \
LogLevel, Options, nip04_encrypt, NostrSigner, Kind, RelayLimits
LogLevel, Options, nip04_encrypt, Kind, RelayLimits

from nostr_dvm.utils.admin_utils import admin_make_database_updates, AdminConfig
from nostr_dvm.utils.backend_utils import get_amount_per_task, check_task_is_supported, get_task
Expand Down Expand Up @@ -42,15 +41,11 @@ async def run_dvm(self, dvm_config, admin_config):
self.dvm_config = dvm_config
self.admin_config = admin_config
self.keys = Keys.parse(dvm_config.PRIVATE_KEY)
wait_for_send = False
skip_disconnected_relays = True
relaylimits = RelayLimits.disable()
opts = (
Options().wait_for_send(wait_for_send).send_timeout(timedelta(seconds=self.dvm_config.RELAY_TIMEOUT))
.skip_disconnected_relays(skip_disconnected_relays).relay_limits(relaylimits))
Options().relay_limits(relaylimits))

signer = NostrSigner.keys(self.keys)
self.client = Client.with_opts(signer, opts)
self.client = Client.with_opts(self.keys, opts)
self.job_list = []
self.jobs_on_hold_list = []
pk = self.keys.public_key()
Expand Down Expand Up @@ -119,7 +114,7 @@ async def handle_nip90_job_event(nip90_event):
cashu = ""
p_tag_str = ""

for tag in nip90_event.tags():
for tag in nip90_event.tags().to_vec():
if tag.as_vec()[0] == "cashu":
cashu = tag.as_vec()[1]
elif tag.as_vec()[0] == "p":
Expand Down Expand Up @@ -281,7 +276,7 @@ async def handle_nip90_job_event(nip90_event):
# dvm_config=self.dvm_config)
else:
bid = 0
for tag in nip90_event.tags():
for tag in nip90_event.tags().to_vec():
if tag.as_vec()[0] == 'bid':
bid = int(tag.as_vec()[1])

Expand Down Expand Up @@ -321,7 +316,7 @@ async def handle_nutzap(nut_zap_event):
user = await get_or_add_user(db=self.dvm_config.DB, npub=sender, client=self.client,
config=self.dvm_config)
zapped_event = None
for tag in nut_zap_event.tags():
for tag in nut_zap_event.tags().to_vec():
if tag.as_vec()[0] == 'e':
zapped_event = await get_event_by_id(tag.as_vec()[1], client=self.client,
config=self.dvm_config)
Expand All @@ -332,7 +327,7 @@ async def handle_nutzap(nut_zap_event):
job_event = None
p_tag_str = ""
status = ""
for tag in zapped_event.tags():
for tag in zapped_event.tags().to_vec():
if tag.as_vec()[0] == 'amount':
amount = int(float(tag.as_vec()[1]) / 1000)
elif tag.as_vec()[0] == 'e':
Expand Down Expand Up @@ -414,7 +409,7 @@ async def handle_zap(zap_event):
job_event = None
p_tag_str = ""
status = ""
for tag in zapped_event.tags():
for tag in zapped_event.tags().to_vec():
if tag.as_vec()[0] == 'amount':
amount = int(float(tag.as_vec()[1]) / 1000)
elif tag.as_vec()[0] == 'e':
Expand Down Expand Up @@ -499,7 +494,7 @@ async def check_event_has_not_unfinished_job_input(nevent, append, client, dvmco
if not task_supported:
return False

for tag in nevent.tags():
for tag in nevent.tags().to_vec():
if tag.as_vec()[0] == 'i':
if len(tag.as_vec()) < 3:
print("Job Event missing/malformed i tag, skipping..")
Expand Down Expand Up @@ -587,7 +582,7 @@ async def send_nostr_reply_event(content, original_event_as_str):
reply_tags = [request_tag, e_tag, p_tag, alt_tag, status_tag]

relay_tag = None
for tag in original_event.tags():
for tag in original_event.tags().to_vec():
if tag.as_vec()[0] == "relays":
relay_tag = tag
if tag.as_vec()[0] == "client":
Expand All @@ -597,13 +592,13 @@ async def send_nostr_reply_event(content, original_event_as_str):
reply_tags.append(relay_tag)

encrypted = False
for tag in original_event.tags():
for tag in original_event.tags().to_vec():
if tag.as_vec()[0] == "encrypted":
encrypted = True
encrypted_tag = Tag.parse(["encrypted"])
reply_tags.append(encrypted_tag)

for tag in original_event.tags():
for tag in original_event.tags().to_vec():
if tag.as_vec()[0] == "i":
i_tag = tag
if not encrypted:
Expand All @@ -614,7 +609,7 @@ async def send_nostr_reply_event(content, original_event_as_str):
content = nip04_encrypt(self.keys.secret_key(), PublicKey.from_hex(original_event.author().to_hex()),
content)

reply_event = EventBuilder(Kind(original_event.kind().as_u16() + 1000), str(content), reply_tags).to_event(
reply_event = EventBuilder(Kind(original_event.kind().as_u16() + 1000), str(content), reply_tags).sign_with_keys(
self.keys)

# send_event(reply_event, client=self.client, dvm_config=self.dvm_config)
Expand All @@ -641,7 +636,7 @@ async def send_job_status_reaction(original_event, status, is_paid=True, amount=
reply_tags = [e_tag, alt_tag, status_tag]

relay_tag = None
for tag in original_event.tags():
for tag in original_event.tags().to_vec():
if tag.as_vec()[0] == "relays":
relay_tag = tag
break
Expand All @@ -651,7 +646,7 @@ async def send_job_status_reaction(original_event, status, is_paid=True, amount=
encryption_tags = []

encrypted = False
for tag in original_event.tags():
for tag in original_event.tags().to_vec():
if tag.as_vec()[0] == "encrypted":
encrypted = True
encrypted_tag = Tag.parse(["encrypted"])
Expand Down Expand Up @@ -730,7 +725,7 @@ async def send_job_status_reaction(original_event, status, is_paid=True, amount=
content = reaction

keys = Keys.parse(dvm_config.PRIVATE_KEY)
reaction_event = EventBuilder(EventDefinitions.KIND_FEEDBACK, str(content), reply_tags).to_event(keys)
reaction_event = EventBuilder(EventDefinitions.KIND_FEEDBACK, str(content), reply_tags).sign_with_keys(keys)
# send_event(reaction_event, client=self.client, dvm_config=self.dvm_config)
await send_event_outbox(reaction_event, client=self.client, dvm_config=self.dvm_config)

Expand Down
Loading

0 comments on commit 93c9571

Please sign in to comment.