| 453 | }, |
| 454 | websocket: { |
| 455 | message(ws, raw) { |
| 456 | const message = JSON.parse(String(raw)) as { |
| 457 | id?: string |
| 458 | method?: string |
| 459 | params?: unknown |
| 460 | } |
| 461 | if (message.method === 'initialize') { |
| 462 | ws.send(JSON.stringify({ id: message.id, result: { ok: true } })) |
| 463 | if (options?.closeAfterInitialize) { |
| 464 | ws.close() |
| 465 | } |
| 466 | return |
| 467 | } |
| 468 | if (message.method === 'chat.completions.start') { |
| 469 | requestIds.push(String(message.id)) |
| 470 | ws.send(JSON.stringify({ id: message.id, result: { accepted: true } })) |
| 471 | if (options?.oversizedPayloadBytes) { |
| 472 | ws.send( |
| 473 | JSON.stringify({ |
| 474 | method: 'chat.completions.delta', |
| 475 | params: { |
| 476 | id: message.id, |
| 477 | data: 'x'.repeat(options.oversizedPayloadBytes), |
| 478 | }, |
| 479 | }), |
| 480 | ) |
| 481 | } |
| 482 | if (options?.delayedDeltaMs !== undefined) { |
| 483 | setTimeout(() => { |
| 484 | ws.send( |
| 485 | JSON.stringify({ |
| 486 | method: 'chat.completions.delta', |
| 487 | params: { |
| 488 | id: message.id, |
| 489 | data: JSON.stringify({ choices: [{ delta: { content: 'delayed' } }] }), |
| 490 | }, |
| 491 | }), |
| 492 | ) |
| 493 | }, options.delayedDeltaMs) |
| 494 | } |
| 495 | if (!options?.keepRequestsOpen) { |
| 496 | ws.send( |
| 497 | JSON.stringify({ |
| 498 | method: 'chat.completions.completed', |
| 499 | params: { id: message.id }, |
| 500 | }), |
| 501 | ) |
| 502 | } |
| 503 | return |
| 504 | } |
| 505 | if (message.method === 'chat.completions.cancel') { |
| 506 | ws.send( |
| 507 | JSON.stringify({ |
| 508 | method: 'chat.completions.completed', |
| 509 | params: { id: (message.params as { id?: string })?.id }, |
| 510 | }), |
| 511 | ) |
| 512 | } |