mirror of
https://github.com/cool-team-official/cool-admin-midway.git
synced 2024-11-01 14:10:29 +08:00
新增SSE返回示例
This commit is contained in:
parent
00c27c8548
commit
da533ae1ed
49
src/modules/demo/controller/open/sse.ts
Normal file
49
src/modules/demo/controller/open/sse.ts
Normal file
@ -0,0 +1,49 @@
|
||||
import { CoolController, BaseController } from '@cool-midway/core';
|
||||
import { Get, Inject } from '@midwayjs/core';
|
||||
import { Context } from 'koa';
|
||||
import { PluginService } from '../../../plugin/service/info';
|
||||
import { PassThrough } from 'stream';
|
||||
|
||||
/**
|
||||
* 事件流 服务端主动推送
|
||||
*/
|
||||
@CoolController()
|
||||
export class OpenDemoSSEController extends BaseController {
|
||||
@Inject()
|
||||
ctx: Context;
|
||||
|
||||
@Inject()
|
||||
pluginService: PluginService;
|
||||
|
||||
@Get('/call', { summary: '事件流 服务端主动推送' })
|
||||
async call() {
|
||||
// 设置响应头
|
||||
this.ctx.set('Content-Type', 'text/event-stream');
|
||||
this.ctx.set('Cache-Control', 'no-cache');
|
||||
this.ctx.set('Connection', 'keep-alive');
|
||||
|
||||
const stream = new PassThrough();
|
||||
|
||||
// 发送数据
|
||||
const send = (data: any) => {
|
||||
stream.write(`data: ${JSON.stringify(data)}\n\n`);
|
||||
};
|
||||
|
||||
// 获取插件实例
|
||||
const instance = await this.pluginService.getInstance('ollama');
|
||||
// 调用chat
|
||||
const messages = [
|
||||
{ role: 'system', content: '你叫小酷,是个编程助手' },
|
||||
{ role: 'user', content: '用js写个Hello World' },
|
||||
];
|
||||
instance.chat(messages, { stream: true }, res => {
|
||||
send(res);
|
||||
if (res.isEnd) {
|
||||
this.ctx.res.end();
|
||||
}
|
||||
});
|
||||
|
||||
this.ctx.status = 200;
|
||||
this.ctx.body = stream;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user