SSE 改走 /api/stream,并保留旧路径

This commit is contained in:
桃树夭 committed 2026-10-08 17:25:26 +08:00
1 parent 3fc0a058cf
commit 5e2531191b
9 files changed
+64 -33

No files matched your search

@@ -68,7 +68,6 @@ const BlackMarketErrors = apiErrorFilter((error) => {
export class BlackMarketController {
constructor(
@Inject(BlackMarketService) private readonly market: BlackMarketService,
@Inject(SseResponseService) private readonly sse: SseResponseService,
) {}
@Get(':nodeId')
@@ -90,6 +89,39 @@ export class BlackMarketController {
return this.market.open(actor, nodeId, input);
}
@Post(':nodeId/sessions/:sessionId/commit')
@HttpCode(200)
commit(
@CurrentCultivator() actor: ActiveCultivatorRef,
@Param('nodeId') nodeId: string,
@Param('sessionId') sessionId: string,
@JsonBody(new ZodPipe(CommitSchema)) input: z.infer<typeof CommitSchema>,
) {
return this.market.commit(actor, nodeId, sessionId, input);
}
@Post(':nodeId/sessions/:sessionId/leave')
@HttpCode(200)
leave(
@CurrentCultivator() actor: ActiveCultivatorRef,
@Param('nodeId') nodeId: string,
@Param('sessionId') sessionId: string,
@JsonBody(new ZodPipe(LeaveSchema)) input: z.infer<typeof LeaveSchema>,
) {
return this.market.leave(actor, nodeId, sessionId, input);
}
}
// The unprefixed interact route stays until the Pages build that calls `/api/stream` is deployed.
@Controller(['api/black-market', 'api/stream/black-market'])
@Access('active')
@UseFilters(BlackMarketErrors)
export class BlackMarketStreamController {
constructor(
@Inject(BlackMarketService) private readonly market: BlackMarketService,
@Inject(SseResponseService) private readonly sse: SseResponseService,
) {}
@Sse(':nodeId/sessions/:sessionId/interact', { method: RequestMethod.POST })
@HttpCode(200)
interact(
@@ -114,26 +146,4 @@ export class BlackMarketController {
this.market.reply(prepared, replySignal, emit);
});
}
@Post(':nodeId/sessions/:sessionId/commit')
@HttpCode(200)
commit(
@CurrentCultivator() actor: ActiveCultivatorRef,
@Param('nodeId') nodeId: string,
@Param('sessionId') sessionId: string,
@JsonBody(new ZodPipe(CommitSchema)) input: z.infer<typeof CommitSchema>,
) {
return this.market.commit(actor, nodeId, sessionId, input);
}
@Post(':nodeId/sessions/:sessionId/leave')
@HttpCode(200)
leave(
@CurrentCultivator() actor: ActiveCultivatorRef,
@Param('nodeId') nodeId: string,
@Param('sessionId') sessionId: string,
@JsonBody(new ZodPipe(LeaveSchema)) input: z.infer<typeof LeaveSchema>,
) {
return this.market.leave(actor, nodeId, sessionId, input);
}
}
@@ -3,11 +3,14 @@ import {
BlackMarketConversationService,
blackMarketConversationService,
} from '@server/black-market/application/BlackMarketConversationService.js';
import { BlackMarketController } from './black-market.controller.js';
import {
BlackMarketController,
BlackMarketStreamController,
} from './black-market.controller.js';
import { BlackMarketService } from './black-market.service.js';
@Module({
controllers: [BlackMarketController],
controllers: [BlackMarketController, BlackMarketStreamController],
providers: [
{
provide: BlackMarketConversationService,
@@ -78,7 +78,8 @@ const YieldErrors = apiErrorFilter((error) => {
);
});
@Controller('api/cultivator')
// `/api/cultivator/*` stays until the Pages build that calls `/api/stream` is deployed.
@Controller(['api/cultivator', 'api/stream/cultivator'])
@Access('active')
export class CultivationController {
constructor(
@@ -32,7 +32,6 @@ import { DivinationService } from './divination.service.js';
export class DivinationController {
constructor(
@Inject(DivinationService) private readonly divination: DivinationService,
@Inject(SseResponseService) private readonly sse: SseResponseService,
) {}
@Get()
@@ -49,6 +48,17 @@ export class DivinationController {
) {
return this.divination.draw(actor, body.direction);
}
}
// `/api/divination/interpret` stays until the Pages build that calls `/api/stream` is deployed.
@Controller(['api/divination', 'api/stream/divination'])
@Access('active')
@UseFilters(DivinationExceptionFilter)
export class DivinationStreamController {
constructor(
@Inject(DivinationService) private readonly divination: DivinationService,
@Inject(SseResponseService) private readonly sse: SseResponseService,
) {}
@Sse('interpret', { method: RequestMethod.POST })
@HttpCode(200)
+9 -2
View File
@@ -1,11 +1,18 @@
import { Module } from '@nestjs/common';
import { DivinationController } from './divination.controller.js';
import {
DivinationController,
DivinationStreamController,
} from './divination.controller.js';
import { DivinationService } from './divination.service.js';
import { DivineFortuneController } from './divine-fortune.controller.js';
import { DivineFortuneService } from './divine-fortune.service.js';
@Module({
controllers: [DivinationController, DivineFortuneController],
controllers: [
DivinationController,
DivinationStreamController,
DivineFortuneController,
],
providers: [DivinationService, DivineFortuneService],
})
export class DivinationModule {}
@@ -61,7 +61,7 @@ export function YieldCard({
text?: string;
state?: Parameters<typeof consumeResourceChanges>[0];
error?: string;
}>('/api/cultivator/yield', { requestId: pending.requestId })) {
}>('/api/stream/cultivator/yield', { requestId: pending.requestId })) {
if (data.type === 'result' && data.data) {
pending.complete();
setYieldResult(() => ({
@@ -290,7 +290,7 @@ export function useRetreatViewModel(): UseRetreatViewModelReturn {
window.sessionStorage.setItem(storageKey, requestId);
try {
await consumeRetreatEvents(
postEvents<RetreatStreamEvent>('/api/cultivator/retreat', {
postEvents<RetreatStreamEvent>('/api/stream/cultivator/retreat', {
...body,
requestId,
}),
+1 -1
View File
@@ -38,7 +38,7 @@ export async function interpretDivination(
) {
let completed = false;
for await (const event of postEvents<DivinationStreamEvent>(
'/api/divination/interpret',
'/api/stream/divination/interpret',
{ drawId },
signal,
)) {
@@ -54,7 +54,7 @@ export async function interactWithBlackMarket(
): Promise<BlackMarketInteractionResult> {
let result: BlackMarketInteractionResult | undefined;
for await (const event of postEvents<BlackMarketInteractStreamEvent>(
`/api/black-market/${encodeURIComponent(nodeId)}/sessions/${sessionId}/interact`,
`/api/stream/black-market/${encodeURIComponent(nodeId)}/sessions/${sessionId}/interact`,
input,
signal,
)) {