wangzhibo
2026-07-16 bac4362a0b7726d38a23d38f0d7913f2c2bab262
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import { CoolController, BaseController } from '@cool-midway/core';
import { Get, Inject } from '@midwayjs/core';
import { PluginService } from '../../../plugin/service/info';
import { PassThrough } from 'stream';
import { IMidwayKoaContext } from '@midwayjs/koa';
 
/**
 * 事件流 服务端主动推送
 */
@CoolController()
export class OpenDemoSSEController extends BaseController {
  @Inject()
  ctx: IMidwayKoaContext;
 
  @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: any = 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;
  }
}