Spaces:
Build error
Build error
File size: 2,945 Bytes
d9494a5 | 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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | import { Injectable } from '@nestjs/common';
import { RedisClientService } from 'src/engine/core-modules/redis-client/redis-client.service';
import { SubscriptionChannel } from 'src/engine/subscriptions/enums/subscription-channel.enum';
@Injectable()
export class SubscriptionService {
constructor(private readonly redisClient: RedisClientService) {}
private getSubscriptionChannel({
channel,
workspaceId,
}: {
channel: SubscriptionChannel;
workspaceId: string;
}) {
return `${channel}:${workspaceId}`;
}
private getEventStreamChannel({
workspaceId,
eventStreamChannelId,
}: {
workspaceId: string;
eventStreamChannelId: string;
}) {
return `${SubscriptionChannel.EVENT_STREAM_CHANNEL}:${workspaceId}:${eventStreamChannelId}`;
}
async subscribe({
channel,
workspaceId,
}: {
channel: SubscriptionChannel;
workspaceId: string;
}) {
const client = this.redisClient.getPubSubClient();
return client.asyncIterator(
this.getSubscriptionChannel({ channel, workspaceId }),
);
}
async subscribeToEventStream({
workspaceId,
eventStreamChannelId,
}: {
workspaceId: string;
eventStreamChannelId: string;
}) {
const client = this.redisClient.getPubSubClient();
return client.asyncIterator(
this.getEventStreamChannel({ workspaceId, eventStreamChannelId }),
);
}
async publish<T>({
channel,
payload,
workspaceId,
}: {
channel: SubscriptionChannel;
payload: T;
workspaceId: string;
}): Promise<void> {
const client = this.redisClient.getPubSubClient();
await client.publish(
this.getSubscriptionChannel({ channel, workspaceId }),
payload,
);
}
async publishToEventStream<T>({
workspaceId,
eventStreamChannelId,
payload,
}: {
workspaceId: string;
eventStreamChannelId: string;
payload: T;
}): Promise<void> {
const client = this.redisClient.getPubSubClient();
await client.publish(
this.getEventStreamChannel({ workspaceId, eventStreamChannelId }),
payload,
);
}
private getAgentChatChannel({
workspaceId,
threadId,
}: {
workspaceId: string;
threadId: string;
}) {
return `${SubscriptionChannel.AGENT_CHAT_CHANNEL}:${workspaceId}:${threadId}`;
}
async subscribeToAgentChat({
workspaceId,
threadId,
}: {
workspaceId: string;
threadId: string;
}) {
const client = this.redisClient.getPubSubClient();
return client.asyncIterator(
this.getAgentChatChannel({ workspaceId, threadId }),
);
}
async publishToAgentChat<T>({
workspaceId,
threadId,
payload,
}: {
workspaceId: string;
threadId: string;
payload: T;
}): Promise<void> {
const client = this.redisClient.getPubSubClient();
await client.publish(
this.getAgentChatChannel({ workspaceId, threadId }),
payload,
);
}
}
|