Update plugin documentation and types to support ReadableStream in provider results
This commit is contained in:
12
plugins.md
12
plugins.md
@@ -107,9 +107,9 @@ Adds a provider to the plugin.
|
|||||||
- `top_p?: number` - The top p value.
|
- `top_p?: number` - The top p value.
|
||||||
- `temperature?: number` - The temperature value.
|
- `temperature?: number` - The temperature value.
|
||||||
- `mode: string` - The mode. one of `model`, `submodel`, `memory`, `emotion`, `otherAx`, `translate`
|
- `mode: string` - The mode. one of `model`, `submodel`, `memory`, `emotion`, `otherAx`, `translate`
|
||||||
- `Promise<{success:boolean,content:string}>` - The provider result.
|
- `Promise<{success:boolean,content:string|ReadableStream<string>}>` - The provider result.
|
||||||
- `success: boolean` - If the provider was successful.
|
- `success: boolean` - If the provider was successful.
|
||||||
- `content: string` - The provider content.
|
- `content: string|ReadableStream<string>` - The provider content. if it's a ReadableStream, it will be streamed to the chat.
|
||||||
|
|
||||||
### `addRisuScriptHandler(type: string, func: (content:string) => string|null|undefined|Promise<string|null|undefined>): void`
|
### `addRisuScriptHandler(type: string, func: (content:string) => string|null|undefined|Promise<string|null|undefined>): void`
|
||||||
|
|
||||||
@@ -118,6 +118,10 @@ Adds a risu script handler to the plugin.
|
|||||||
#### Arguments
|
#### Arguments
|
||||||
|
|
||||||
- `type: string` - The handler type. one of `display`, `output`, `input`, `process`
|
- `type: string` - The handler type. one of `display`, `output`, `input`, `process`
|
||||||
|
- `display` - The handler will be called when the data is displayed.
|
||||||
|
- `output` - The handler will be called when the data is outputted by the AI model.
|
||||||
|
- `input` - The handler will be called when the data is inputted to the user.
|
||||||
|
- `process` - The handler will be called when creating actual request data.
|
||||||
- `func: (content:string) => string|null|undefined|Promise<string|null|undefined>` - The handler function.
|
- `func: (content:string) => string|null|undefined|Promise<string|null|undefined>` - The handler function.
|
||||||
- `content: string` - The content to handle.
|
- `content: string` - The content to handle.
|
||||||
- `string|null|undefined|Promise<string|null|undefined>` - The handler result. if it is a string or string promise, the data will be replaced with the result.
|
- `string|null|undefined|Promise<string|null|undefined>` - The handler result. if it is a string or string promise, the data will be replaced with the result.
|
||||||
@@ -133,6 +137,8 @@ Adds a risu replacer to the plugin.
|
|||||||
#### Arguments
|
#### Arguments
|
||||||
|
|
||||||
- `type: string` - The replacer type. one of `beforeRequest`, `afterRequest`.
|
- `type: string` - The replacer type. one of `beforeRequest`, `afterRequest`.
|
||||||
|
- `beforeRequest` - The replacer will be called right before the request is sent.
|
||||||
|
- `afterRequest` - The replacer will be called right after the response is received.
|
||||||
- `func: ReplacerFunction` - The replacer function. vary depending on the type.
|
- `func: ReplacerFunction` - The replacer function. vary depending on the type.
|
||||||
- If the type is `afterRequest`, the function should be `(content: string, mode:string) => string`.
|
- If the type is `afterRequest`, the function should be `(content: string, mode:string) => string`.
|
||||||
- If the type is `beforeRequest`, the function should be `(content: Chat[], mode:string) => Chat[]`.
|
- If the type is `beforeRequest`, the function should be `(content: Chat[], mode:string) => Chat[]`.
|
||||||
@@ -147,7 +153,7 @@ Removes a risu replacer from the plugin.
|
|||||||
Adds an unload handler to the plugin.
|
Adds an unload handler to the plugin.
|
||||||
|
|
||||||
|
|
||||||
## Migration from Plugin V1
|
## Changes from Plugin V1
|
||||||
|
|
||||||
The plugin system has been updated to V2. The following changes have been made:
|
The plugin system has been updated to V2. The following changes have been made:
|
||||||
- Now runs in same context as the main script rather than in a sandbox, making it accessible to the main script and DOM.
|
- Now runs in same context as the main script rather than in a sandbox, making it accessible to the main script and DOM.
|
||||||
|
|||||||
@@ -305,7 +305,7 @@ type EditFunction = (content:string) => string|null|undefined|Promise<string|nul
|
|||||||
type ReplacerFunction = (content:OpenAIChat[], type:string) => OpenAIChat[]|Promise<OpenAIChat[]>
|
type ReplacerFunction = (content:OpenAIChat[], type:string) => OpenAIChat[]|Promise<OpenAIChat[]>
|
||||||
|
|
||||||
export const pluginV2 = {
|
export const pluginV2 = {
|
||||||
providers: new Map<string, (arg:PluginV2ProviderArgument) => Promise<{success:boolean,content:string}> >(),
|
providers: new Map<string, (arg:PluginV2ProviderArgument) => Promise<{success:boolean,content:string|ReadableStream<string>}> >(),
|
||||||
editdisplay: new Set<EditFunction>(),
|
editdisplay: new Set<EditFunction>(),
|
||||||
editoutput: new Set<EditFunction>(),
|
editoutput: new Set<EditFunction>(),
|
||||||
editprocess: new Set<EditFunction>(),
|
editprocess: new Set<EditFunction>(),
|
||||||
|
|||||||
@@ -1420,7 +1420,24 @@ async function requestPlugin(arg:RequestDataArgumentExtended):Promise<requestDat
|
|||||||
else if(!d.success){
|
else if(!d.success){
|
||||||
return {
|
return {
|
||||||
type: 'fail',
|
type: 'fail',
|
||||||
result: d.content
|
result: d.content instanceof ReadableStream ? await (new Response(d.content)).text() : d.content
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if(d.content instanceof ReadableStream){
|
||||||
|
|
||||||
|
let fullText = ''
|
||||||
|
const piper = new TransformStream<string, StreamResponseChunk>( {
|
||||||
|
transform(chunk, control) {
|
||||||
|
fullText += chunk
|
||||||
|
control.enqueue({
|
||||||
|
"0": fullText
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
return {
|
||||||
|
type: 'streaming',
|
||||||
|
result: d.content.pipeThrough(piper)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
|
|||||||
Reference in New Issue
Block a user