-
-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
59 changed files
with
938 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
{ | ||
"name": "@nlux-dev/nlbridge", | ||
"version": "0.0.0-latest", | ||
"license": "MPL-2.0", | ||
"scripts": { | ||
"build": "rollup --config rollup.config.ts --configPlugin 'typescript={moduleResolution: \"bundler\"}' --resolveJsonModule", | ||
"watch": "rollup --config rollup.config.ts --configPlugin 'typescript={moduleResolution: \"bundler\"}' --resolveJsonModule --watch" | ||
}, | ||
"dependencies": { | ||
"@nlux/core": "{versions.nlux}" | ||
}, | ||
"peerDependencies": { | ||
}, | ||
"devDependencies": { | ||
"@rollup/plugin-commonjs": "^25.0.7", | ||
"@rollup/plugin-node-resolve": "^15.2.3", | ||
"@rollup/plugin-replace": "^5.0.5", | ||
"@rollup/plugin-strip": "^3.0.4", | ||
"@rollup/plugin-terser": "^0.4.4", | ||
"@rollup/plugin-typescript": "^11.1.6", | ||
"rollup": "^4.9.6", | ||
"rollup-plugin-dts": "^6.1.0", | ||
"rollup-plugin-esbuild": "^6.1.1", | ||
"tslib": "^2.6.2" | ||
}, | ||
"main": "index.js", | ||
"types": "nlbridge.d.ts", | ||
"module": "esm/nlbridge.js", | ||
"browser": "umd/nlbridge.js" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import commonjs from '@rollup/plugin-commonjs'; | ||
import {nodeResolve} from '@rollup/plugin-node-resolve'; | ||
import replace from '@rollup/plugin-replace'; | ||
import strip from '@rollup/plugin-strip'; | ||
import terser from '@rollup/plugin-terser'; | ||
import {RollupOptions} from 'rollup'; | ||
import esbuild from 'rollup-plugin-esbuild'; | ||
// @ts-ignore | ||
import {generateDts} from '../../../pipeline/utils/rollup/generateDts'; | ||
import {generateOutputConfig} from '../../../pipeline/utils/rollup/generateOutputConfig'; | ||
|
||
const isProduction = process.env.NODE_ENV === 'production'; | ||
const packageName = '@nlux/nlbridge'; | ||
const outputFile = 'nlbridge'; | ||
|
||
const packageConfig: () => Promise<RollupOptions[]> = async () => ([ | ||
{ | ||
input: './src/index.ts', | ||
logLevel: 'silent', | ||
treeshake: 'smallest', | ||
strictDeprecations: true, | ||
plugins: [ | ||
commonjs(), | ||
esbuild(), | ||
isProduction && strip({ | ||
include: '**/*.(mjs|js|ts)', | ||
functions: ['debug', 'console.log', 'console.info'], | ||
}), | ||
!isProduction && nodeResolve(), | ||
replace({ | ||
values: { | ||
'process.env.NLUX_DEBUG_ENABLED': JSON.stringify(isProduction ? 'false' : 'true'), | ||
}, | ||
preventAssignment: true, | ||
}), | ||
isProduction && terser(), | ||
], | ||
external: [ | ||
'@nlux/core', | ||
], | ||
output: generateOutputConfig(packageName, outputFile, isProduction), | ||
}, | ||
generateDts(outputFile, isProduction), | ||
]); | ||
|
||
export default packageConfig; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
export type { | ||
Adapter, | ||
StandardAdapter, | ||
StreamingAdapterObserver, | ||
DataTransferMode, | ||
} from '@nlux/core'; | ||
|
||
export {debug} from '@nlux/core'; | ||
|
||
export type {NlBridgeAdapterOptions} from './nlbridge/types/adapterOptions'; | ||
|
||
export type {NlBridgeAdapterBuilder} from './nlbridge/builder/builder'; | ||
|
||
export {createAdapter} from './nlbridge/builder/createAdapter'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { | ||
AdapterExtras, | ||
DataTransferMode, | ||
StandardAdapter, | ||
StandardAdapterConfig, | ||
StandardAdapterInfo, | ||
StreamingAdapterObserver, | ||
uid, | ||
} from '@nlux/core'; | ||
import {NlBridgeAdapterOptions} from '../types/adapterOptions'; | ||
|
||
export abstract class NlBridgeAbstractAdapter implements StandardAdapter<string, string | undefined> { | ||
static defaultDataTransferMode: DataTransferMode = 'stream'; | ||
|
||
private readonly __instanceId: string; | ||
private readonly __options: NlBridgeAdapterOptions; | ||
|
||
private readonly theDataTransferModeToUse: DataTransferMode; | ||
private readonly theEndpointUrlToUse: string; | ||
|
||
constructor(options: NlBridgeAdapterOptions) { | ||
this.__instanceId = `${this.info.id}-${uid()}`; | ||
this.__options = {...options}; | ||
|
||
this.theDataTransferModeToUse = options.dataTransferMode ?? NlBridgeAbstractAdapter.defaultDataTransferMode; | ||
this.theEndpointUrlToUse = options.url; | ||
} | ||
|
||
get config(): StandardAdapterConfig<any, any> { | ||
return { | ||
encodeMessage: (message: string) => { | ||
return Promise.resolve(message); | ||
}, | ||
decodeMessage: (payload: any) => { | ||
return Promise.resolve(payload); | ||
}, | ||
}; | ||
} | ||
|
||
get dataTransferMode(): DataTransferMode { | ||
return this.theDataTransferModeToUse; | ||
} | ||
|
||
get endpointUrl(): string { | ||
return this.theEndpointUrlToUse; | ||
} | ||
|
||
get id(): string { | ||
return this.__instanceId; | ||
} | ||
|
||
get info(): StandardAdapterInfo { | ||
return { | ||
id: 'nlbridge-adapter', | ||
capabilities: { | ||
textChat: true, | ||
audio: false, | ||
fileUpload: false, | ||
}, | ||
inputFormats: ['text'], | ||
outputFormats: ['text', 'markdown'], | ||
}; | ||
} | ||
|
||
async decode(payload: string): Promise<string | undefined> { | ||
return undefined; | ||
} | ||
|
||
async encode(message: string): Promise<string | undefined> { | ||
return undefined; | ||
} | ||
|
||
abstract fetchText(message: string, extras: AdapterExtras): Promise<string>; | ||
|
||
abstract streamText(message: string, observer: StreamingAdapterObserver, extras: AdapterExtras): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import {AdapterExtras, NluxError, NluxUsageError, StreamingAdapterObserver} from '@nlux/core'; | ||
import {NlBridgeAbstractAdapter} from './adapter'; | ||
|
||
export class NlBridgeFetchAdapter extends NlBridgeAbstractAdapter { | ||
constructor(options: any) { | ||
super(options); | ||
} | ||
|
||
async fetchText(message: string, extras: AdapterExtras): Promise<string> { | ||
const response = await fetch(this.endpointUrl, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({ | ||
action: 'chat', | ||
payload: { | ||
message, | ||
}, | ||
}), | ||
}); | ||
|
||
if (!response.ok) { | ||
throw new NluxError({ | ||
source: this.constructor.name, | ||
message: `NlBridge adapter returned status code: ${response.status}`, | ||
}); | ||
} | ||
|
||
const body = await response.json(); | ||
if ( | ||
typeof body === 'object' && body !== null && body.success === true && | ||
typeof body.result === 'object' && body.result !== null && | ||
typeof body.result.response === 'string' | ||
) { | ||
return body.result.response; | ||
} else { | ||
throw new NluxError({ | ||
source: this.constructor.name, | ||
message: 'Invalid response from NlBridge: String expected.', | ||
}); | ||
} | ||
} | ||
|
||
streamText(message: string, observer: StreamingAdapterObserver, extras: AdapterExtras): void { | ||
throw new NluxUsageError({ | ||
source: this.constructor.name, | ||
message: 'Cannot stream text from the fetch adapter!', | ||
}); | ||
} | ||
} |
Oops, something went wrong.