kopia lustrzana https://github.com/transitive-bullshit/chatgpt-api
feat: add Novu service and tool
rodzic
81eb5f4594
commit
907d27ffa8
|
@ -0,0 +1,30 @@
|
|||
<h1 align="center">Novu Agentic Service</h1>
|
||||
|
||||
## Intro
|
||||
|
||||
[Novu][novu] provides open-source notification infrastructure for all communication channels in one place: Email, SMS, Direct, and Push. It integrates with almost all major email providers (Mailgun, Sendgrid, Postmark, etc.), SMS providers (e.g., Twilio or Plivo), and a large selection of push and chat providers (such as OneSignal or Slack) while providing a unified API for sending notifications.
|
||||
|
||||
## Pre-requisites
|
||||
|
||||
Ensure the following environment variable is set:
|
||||
|
||||
- `NOVU_API_KEY` - Novu API key.
|
||||
|
||||
Otherwise, you can pass it in as an argument to the `Novu` constructor.
|
||||
|
||||
### How to Retrieve API Key on Novu.co
|
||||
|
||||
1. Open https://web.novu.co and sign in with your existing Novu account credentials (create a new account with your email address and a password or sign in with GitHub if you don't have an account yet)
|
||||
|
||||
2. Navigate to "Settings"
|
||||
|
||||

|
||||
|
||||
3. Click "API Keys"
|
||||
|
||||

|
||||
|
||||
4. Click here to copy your API key to your clipboard
|
||||

|
||||
|
||||
[novu]: https://novu.co/
|
|
@ -0,0 +1,58 @@
|
|||
import ky from 'ky'
|
||||
|
||||
export type NovuSubscriber = {
|
||||
subscriberId: string
|
||||
email?: string
|
||||
firstName?: string
|
||||
lastName?: string
|
||||
phone?: string
|
||||
}
|
||||
|
||||
export type NovuTriggerEventResponse = {
|
||||
data: {
|
||||
acknowledged?: boolean
|
||||
status?: string
|
||||
transactionId?: string
|
||||
}
|
||||
}
|
||||
|
||||
export class NovuClient {
|
||||
apiKey: string
|
||||
baseUrl: string
|
||||
|
||||
constructor({
|
||||
apiKey = process.env.NOVU_API_KEY,
|
||||
baseUrl = 'https://api.novu.co/v1'
|
||||
}: {
|
||||
apiKey?: string
|
||||
baseUrl?: string
|
||||
} = {}) {
|
||||
if (!apiKey) {
|
||||
throw new Error(`Error NovuClient missing required "apiKey"`)
|
||||
}
|
||||
this.apiKey = apiKey
|
||||
this.baseUrl = baseUrl
|
||||
}
|
||||
|
||||
async triggerEvent(
|
||||
name: string,
|
||||
payload: Record<string, unknown>,
|
||||
to: NovuSubscriber[]
|
||||
) {
|
||||
const url = `${this.baseUrl}/events/trigger`
|
||||
const headers = {
|
||||
Authorization: `ApiKey ${this.apiKey}`,
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
const body = JSON.stringify({
|
||||
name,
|
||||
payload,
|
||||
to
|
||||
})
|
||||
const response = await ky.post(url, {
|
||||
headers,
|
||||
body
|
||||
})
|
||||
return response.json<NovuTriggerEventResponse>()
|
||||
}
|
||||
}
|
|
@ -0,0 +1,82 @@
|
|||
import { z } from 'zod'
|
||||
|
||||
import * as types from '../types'
|
||||
import { Agentic } from '../agentic'
|
||||
import { NovuClient } from '../services/novu'
|
||||
import { BaseTask } from '../task'
|
||||
|
||||
export const NovuNotificationToolInputSchema = z.object({
|
||||
name: z.string(),
|
||||
payload: z.record(z.unknown()),
|
||||
to: z.array(
|
||||
z.object({
|
||||
subscriberId: z.string(),
|
||||
email: z.string().optional(),
|
||||
firstName: z.string().optional(),
|
||||
lastName: z.string().optional(),
|
||||
phone: z.string().optional()
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
export type NovuNotificationToolInput = z.infer<
|
||||
typeof NovuNotificationToolInputSchema
|
||||
>
|
||||
|
||||
export const NovuNotificationToolOutputSchema = z.object({
|
||||
data: z.object({
|
||||
acknowledged: z.boolean().optional(),
|
||||
status: z.string().optional(),
|
||||
transactionId: z.string().optional()
|
||||
})
|
||||
})
|
||||
|
||||
export type NovuNotificationToolOutput = z.infer<
|
||||
typeof NovuNotificationToolOutputSchema
|
||||
>
|
||||
|
||||
export class NovuNotificationTool extends BaseTask<
|
||||
typeof NovuNotificationToolInputSchema,
|
||||
typeof NovuNotificationToolOutputSchema
|
||||
> {
|
||||
_novuClient: NovuClient
|
||||
|
||||
constructor({
|
||||
agentic,
|
||||
novuClient = new NovuClient()
|
||||
}: {
|
||||
agentic: Agentic
|
||||
novuClient?: NovuClient
|
||||
}) {
|
||||
super({
|
||||
agentic
|
||||
})
|
||||
|
||||
this._novuClient = novuClient
|
||||
}
|
||||
|
||||
public override get inputSchema() {
|
||||
return NovuNotificationToolInputSchema
|
||||
}
|
||||
|
||||
public override get outputSchema() {
|
||||
return NovuNotificationToolOutputSchema
|
||||
}
|
||||
|
||||
protected override async _call(
|
||||
input: NovuNotificationToolInput
|
||||
): Promise<types.TaskResponse<typeof NovuNotificationToolOutputSchema>> {
|
||||
// TODO: handle errors gracefully
|
||||
input = this.inputSchema.parse(input)
|
||||
|
||||
const result = await this._novuClient.triggerEvent(
|
||||
input.name,
|
||||
input.payload,
|
||||
input.to
|
||||
)
|
||||
return {
|
||||
result,
|
||||
metadata: {}
|
||||
}
|
||||
}
|
||||
}
|
Ładowanie…
Reference in New Issue