@ -0,0 +1,50 @@
|
||||
import { createAction } from '@typebot.io/forge'
|
||||
import { auth } from '../auth'
|
||||
import { parseChatCompletionOptions } from '@typebot.io/openai-block/shared/parseChatCompletionOptions'
|
||||
import { getChatCompletionSetVarIds } from '@typebot.io/openai-block/shared/getChatCompletionSetVarIds'
|
||||
import { getChatCompletionStreamVarId } from '@typebot.io/openai-block/shared/getChatCompletionStreamVarId'
|
||||
import { runChatCompletion } from '@typebot.io/openai-block/shared/runChatCompletion'
|
||||
import { runChatCompletionStream } from '@typebot.io/openai-block/shared/runChatCompletionStream'
|
||||
import { defaultOpenRouterOptions } from '../constants'
|
||||
import { got } from 'got'
|
||||
import { ModelsResponse } from '../types'
|
||||
|
||||
export const createChatCompletion = createAction({
|
||||
name: 'Create chat completion',
|
||||
auth,
|
||||
options: parseChatCompletionOptions({
|
||||
modelFetchId: 'fetchModels',
|
||||
}),
|
||||
getSetVariableIds: getChatCompletionSetVarIds,
|
||||
fetchers: [
|
||||
{
|
||||
id: 'fetchModels',
|
||||
dependencies: [],
|
||||
fetch: async () => {
|
||||
const response = await got
|
||||
.get(defaultOpenRouterOptions.baseUrl + '/models')
|
||||
.json<ModelsResponse>()
|
||||
|
||||
return response.data.map((model) => ({
|
||||
value: model.id,
|
||||
label: model.name,
|
||||
}))
|
||||
},
|
||||
},
|
||||
],
|
||||
run: {
|
||||
server: (params) =>
|
||||
runChatCompletion({
|
||||
...params,
|
||||
config: { baseUrl: defaultOpenRouterOptions.baseUrl },
|
||||
}),
|
||||
stream: {
|
||||
getStreamVariableId: getChatCompletionStreamVarId,
|
||||
run: (params) =>
|
||||
runChatCompletionStream({
|
||||
...params,
|
||||
config: { baseUrl: defaultOpenRouterOptions.baseUrl },
|
||||
}),
|
||||
},
|
||||
},
|
||||
})
|
15
packages/forge/blocks/openRouter/auth.ts
Normal file
15
packages/forge/blocks/openRouter/auth.ts
Normal file
@ -0,0 +1,15 @@
|
||||
import { option, AuthDefinition } from '@typebot.io/forge'
|
||||
|
||||
export const auth = {
|
||||
type: 'encryptedCredentials',
|
||||
name: 'OpenRouter account',
|
||||
schema: option.object({
|
||||
apiKey: option.string.layout({
|
||||
label: 'API key',
|
||||
isRequired: true,
|
||||
inputType: 'password',
|
||||
helperText:
|
||||
'You can generate an API key [here](https://openrouter.ai/keys).',
|
||||
}),
|
||||
}),
|
||||
} satisfies AuthDefinition
|
3
packages/forge/blocks/openRouter/constants.ts
Normal file
3
packages/forge/blocks/openRouter/constants.ts
Normal file
@ -0,0 +1,3 @@
|
||||
export const defaultOpenRouterOptions = {
|
||||
baseUrl: 'https://openrouter.ai/api/v1',
|
||||
} as const
|
13
packages/forge/blocks/openRouter/index.ts
Normal file
13
packages/forge/blocks/openRouter/index.ts
Normal file
@ -0,0 +1,13 @@
|
||||
import { createBlock } from '@typebot.io/forge'
|
||||
import { OpenRouterLogo } from './logo'
|
||||
import { auth } from './auth'
|
||||
import { createChatCompletion } from './actions/createChatCompletion'
|
||||
|
||||
export const openRouter = createBlock({
|
||||
id: 'open-router',
|
||||
name: 'OpenRouter',
|
||||
tags: ['ai', 'openai', 'chat', 'completion'],
|
||||
LightLogo: OpenRouterLogo,
|
||||
auth,
|
||||
actions: [createChatCompletion],
|
||||
})
|
23
packages/forge/blocks/openRouter/logo.tsx
Normal file
23
packages/forge/blocks/openRouter/logo.tsx
Normal file
@ -0,0 +1,23 @@
|
||||
import React from 'react'
|
||||
|
||||
export const OpenRouterLogo = (props: React.SVGProps<SVGSVGElement>) => (
|
||||
<svg viewBox="0 0 512 512" fill="#71717A" stroke="#71717A" {...props}>
|
||||
<g clip-path="url(#clip0_205_3)">
|
||||
<path
|
||||
d="M3 248.945C18 248.945 76 236 106 219C136 202 136 202 198 158C276.497 102.293 332 120.945 423 120.945"
|
||||
strokeWidth="90"
|
||||
></path>
|
||||
<path d="M511 121.5L357.25 210.268L357.25 32.7324L511 121.5Z"></path>
|
||||
<path
|
||||
d="M0 249C15 249 73 261.945 103 278.945C133 295.945 133 295.945 195 339.945C273.497 395.652 329 377 420 377"
|
||||
strokeWidth="90"
|
||||
></path>
|
||||
<path d="M508 376.445L354.25 287.678L354.25 465.213L508 376.445Z"></path>
|
||||
</g>
|
||||
<defs>
|
||||
<clipPath id="clip0_205_3">
|
||||
<rect width="512" height="512"></rect>
|
||||
</clipPath>
|
||||
</defs>
|
||||
</svg>
|
||||
)
|
17
packages/forge/blocks/openRouter/package.json
Normal file
17
packages/forge/blocks/openRouter/package.json
Normal file
@ -0,0 +1,17 @@
|
||||
{
|
||||
"name": "@typebot.io/open-router-block",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.ts",
|
||||
"keywords": [],
|
||||
"license": "ISC",
|
||||
"devDependencies": {
|
||||
"@typebot.io/forge": "workspace:*",
|
||||
"@typebot.io/tsconfig": "workspace:*",
|
||||
"@types/react": "18.2.15",
|
||||
"typescript": "5.3.2",
|
||||
"@typebot.io/lib": "workspace:*",
|
||||
"@typebot.io/openai-block": "workspace:*",
|
||||
"got": "12.6.0"
|
||||
}
|
||||
}
|
10
packages/forge/blocks/openRouter/tsconfig.json
Normal file
10
packages/forge/blocks/openRouter/tsconfig.json
Normal file
@ -0,0 +1,10 @@
|
||||
{
|
||||
"extends": "@typebot.io/tsconfig/base.json",
|
||||
"include": ["**/*.ts", "**/*.tsx"],
|
||||
"exclude": ["node_modules"],
|
||||
"compilerOptions": {
|
||||
"lib": ["ESNext", "DOM"],
|
||||
"noEmit": true,
|
||||
"jsx": "react"
|
||||
}
|
||||
}
|
6
packages/forge/blocks/openRouter/types.ts
Normal file
6
packages/forge/blocks/openRouter/types.ts
Normal file
@ -0,0 +1,6 @@
|
||||
export type ModelsResponse = {
|
||||
data: {
|
||||
id: string
|
||||
name: string
|
||||
}[]
|
||||
}
|
Reference in New Issue
Block a user