Skip to content

Commit

Permalink
Merge pull request #1 from lightning-digital-entertainment/processing…
Browse files Browse the repository at this point in the history
…-handler

v0.2
  • Loading branch information
Egge21M authored Dec 14, 2023
2 parents 0ae4f7e + 0b1c34d commit 0e4ec90
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 28 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "plebai-sdk",
"version": "0.1.1",
"version": "0.2.0",
"description": "A wrapper for communicating with plebai agents",
"module": "./lib/esm/index.js",
"main": "./lib/cjs/index.js",
Expand Down
65 changes: 38 additions & 27 deletions src/Conversation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ import {
signFromLocalstorage,
} from './localstorage.js';

type Listeners = {
onMessage: (e: Event<4>, decryptedMessage: string) => void;
onInvoice?: (lightningInvoice: string) => void;
onProcessing?: () => void;
};

class Conversation {
private agentKey: string;

Expand All @@ -27,7 +33,8 @@ class Conversation {

private relayPool: SimplePool;

private secretKeyMethod?: 'nip07' | 'throwaway' | 'localstorage' = 'throwaway';
private secretKeyMethod?: 'nip07' | 'throwaway' | 'localstorage' =

Check failure on line 36 in src/Conversation.ts

View workflow job for this annotation

GitHub Actions / build

There should be no line break before or after '='
'throwaway';

private secretKey?: string;

Expand Down Expand Up @@ -123,11 +130,8 @@ class Conversation {
return this.singEvent(event);
}

async sub(
eventCallback: (e: Event<4>, decryptedMessage: string) => void,
invoiceCallback?: (lightningInvoice: string) => void,
) {
if (!invoiceCallback && !this.useWebLn) {
async sub(listeners: Listeners) {
if (!listeners.onInvoice && !this.useWebLn) {
throw new Error('InvoiceCallback is required when useWebLn is false.');
}
let userPublicKey: string | undefined;
Expand All @@ -146,32 +150,39 @@ class Conversation {
}
this.relayPool
.sub(this.relays, [
{ kinds: [4], authors: [userPublicKey], '#p': [this.agentKey] },
{ kinds: [4], authors: [this.agentKey], '#p': [userPublicKey] },
{ kinds: [4, 7000], authors: [userPublicKey], '#p': [this.agentKey] },
{ kinds: [4, 7000], authors: [this.agentKey], '#p': [userPublicKey] },
])
.on('event', async (e) => {
const invoice = getTagValue(e, 'invoice', 1);
if (invoice) {
if (this.useWebLn) {
try {
handleWebLnPayment(invoice);
} catch (paymentErr) {
if (!invoiceCallback) {
throw new Error(
'WebLN failed and there was no invoice callback to fallback to.',
);
if (e.kind === 7000 && listeners.onProcessing) {
listeners.onProcessing();
} else if (e.kind === 4) {
const invoice = getTagValue(e, 'invoice', 1);
if (invoice) {
if (this.useWebLn) {
try {
handleWebLnPayment(invoice);
} catch (paymentErr) {
if (!listeners.onInvoice) {
throw new Error(
'WebLN failed and there was no invoice callback to fallback to.',
);
}
listeners.onInvoice(invoice);
}
} else {
// eslint-disable-next-line no-lonely-if
if (listeners.onInvoice) {
listeners.onInvoice(invoice);
}
invoiceCallback(invoice);
}
} else {
invoiceCallback!(invoice);
const decryptedMessage = await this.decryptMessage(
this.agentKey,
e.content,
);
listeners.onMessage(e as Event<4>, decryptedMessage);
}
} else {
const decryptedMessage = await this.decryptMessage(
this.agentKey,
e.content,
);
eventCallback(e, decryptedMessage);
}
});
}
Expand Down Expand Up @@ -205,7 +216,7 @@ class Conversation {
async sendPrompt(prompt: string) {
const event = await this.createKind4(prompt);
const pubs = this.relayPool.publish(this.relays, event);
return Promise.all(pubs);
return pubs;
}
}

Expand Down

0 comments on commit 0e4ec90

Please sign in to comment.