Skip to content

Commit

Permalink
feat: update to use gpt-conversation-prompt lib types in domain
Browse files Browse the repository at this point in the history
  • Loading branch information
Yengas committed Jan 3, 2023
1 parent 99e9298 commit 28525ca
Show file tree
Hide file tree
Showing 12 changed files with 115 additions and 121 deletions.
38 changes: 19 additions & 19 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions src/application/conversation/conversation-command.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import {
AddUserMessageCommand,
ConversationCommand,
CreateConversationCommand,
Message,
} from "../../domain/conversation/conversation.dto";
} from "../../domain/conversation/conversation.commands";
import { ConversationMessage } from "../../domain/conversation/conversation.dto";
import { TriggerBotService } from "../../domain/conversation/trigger-bot.service";
import { ConversationAggregateDynamodbRepository } from "../../infrastructure/dynamodb/conversation-aggregate-dynamodb.repository";
import { OpenAIService } from "../../infrastructure/openai/openai.service";
Expand Down Expand Up @@ -73,7 +73,7 @@ export class ConversationCommandHandler {

const correlationId = crypto.randomUUID();
const triggerBotService: TriggerBotService = {
trigger: (messages: Message[]) => {
trigger: (messages: ConversationMessage[]) => {
status.isTriggered = true;

status.result = this.openAiService.askForResponse(messages);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
BotResponseAdded,
BotResponseRequested,
ConversationStarted,
} from "../../domain/conversation/conversation.dto";
} from "../../domain/conversation/conversation.events";
import { SlackConversationView } from "../../domain/slack-adapter/slack-adapter.dto";
import { SlackConversationDynamodbRepository } from "../../infrastructure/dynamodb/slack-conversation-dynamodb.repository";
import { SlackMessageHelpers } from "../../infrastructure/slack/slack-message-helpers";
Expand Down
4 changes: 2 additions & 2 deletions src/application/slack-adapter/slack-event-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export class SlackEventHandler {
conversationId: slackConversationView.conversationId,
message: {
id: event.ts,
author: { userId: event.user },
author: { id: event.user },
text: prepareForConversationDomain({
text: event.text,
botUserId: slackConversationView.botUserId,
Expand All @@ -113,7 +113,7 @@ export class SlackEventHandler {
conversationId: crypto.randomUUID(),
initialMessage: {
id: event.ts,
author: { userId: event.user },
author: { id: event.user },
text: prepareForConversationDomain({ text: event.text, botUserId }),
},
metadata: {
Expand Down
34 changes: 20 additions & 14 deletions src/domain/conversation/conversation.aggregate.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { DomainEvent } from "../bus/event-bus";
import {
AddUserMessageCommand,
AIStatus,
BOT_USER_ID,
ConversationEvent,
Message,
} from "./conversation.dto";
import { AddUserMessageCommand } from "./conversation.commands";
import { ConversationMessage } from "./conversation.dto";
import { ConversationEvent } from "./conversation.events";
import { BotResponse, TriggerBotService } from "./trigger-bot.service";

type AIStatus =
| {
status: "IDLE";
}
| { status: "PROCESSING"; correlationId: string };

function assertUnreachable(value: never): never {
throw new Error(`expected value to be unreachable: '${value}'`);
}
Expand All @@ -31,7 +33,7 @@ export class ConversationAggregate {
constructor(
public readonly conversationId: string,
public status: "ONGOING" | "COMPLETED",
public readonly messages: Message[],
public readonly messages: ConversationMessage[],
public aiStatus: AIStatus,
private newEvents: ConversationEvent[] = []
) {}
Expand All @@ -44,12 +46,16 @@ export class ConversationAggregate {
}

addUserMessage({ message }: AddUserMessageCommand) {
this.messages.push(message);
this.messages.push({
id: message.id,
text: message.text,
author: { type: "USER", id: message.author.id },
});

this.apply({
type: "USER_MESSAGE_ADDED",
conversationId: this.conversationId,
authorUserId: message.author.userId,
authorUserId: message.author.id,
messageId: message.id,
});
}
Expand Down Expand Up @@ -89,9 +95,9 @@ export class ConversationAggregate {

switch (botResponse.type) {
case "BOT_RESPONSE_SUCCESS": {
const message: Message = {
const message: ConversationMessage = {
id: messageId,
author: { userId: BOT_USER_ID },
author: { type: "BOT" },
text: botResponse.message,
};

Expand All @@ -107,9 +113,9 @@ export class ConversationAggregate {
return;
}
case "BOT_RESPONSE_ERROR": {
const message: Message = {
const message: ConversationMessage = {
id: messageId,
author: { userId: BOT_USER_ID },
author: { type: "BOT" },
text: `unexpected error occurred: ${botResponse.error.message}`,
};

Expand Down
22 changes: 22 additions & 0 deletions src/domain/conversation/conversation.commands.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
type UserMessage = {
id: string;
text: string;
author: { id: string };
};

export type AddUserMessageCommand = {
type: "ADD_USER_MESSAGE_COMMAND";
conversationId: string;
message: UserMessage;
};

export type CreateConversationCommand = {
type: "CREATE_CONVERSATION_COMMAND";
conversationId: string;
initialMessage: UserMessage;
metadata: Record<string, string>;
};

export type ConversationCommand =
| AddUserMessageCommand
| CreateConversationCommand;
71 changes: 2 additions & 69 deletions src/domain/conversation/conversation.dto.ts
Original file line number Diff line number Diff line change
@@ -1,72 +1,5 @@
export const BOT_USER_ID = "bot-user-id";
import { Message } from "@wisegpt/gpt-conversation-prompt";

export type User = {
userId: string;
};

export type Message = {
author: User;
export type ConversationMessage = Message & {
id: string;
text: string;
};

export type AIStatus =
| {
status: "IDLE";
}
| { status: "PROCESSING"; correlationId: string };

export type AddUserMessageCommand = {
type: "ADD_USER_MESSAGE_COMMAND";
conversationId: string;
message: Message;
};

export type CreateConversationCommand = {
type: "CREATE_CONVERSATION_COMMAND";
conversationId: string;
initialMessage: Message;
metadata: Record<string, string>;
};

export type ConversationCommand =
| AddUserMessageCommand
| CreateConversationCommand;

export type ConversationStarted = {
type: "CONVERSATION_STARTED";
conversationId: string;
metadata: Record<string, string>;
};

export type UserMessageAdded = {
type: "USER_MESSAGE_ADDED";
conversationId: string;
authorUserId: string;
messageId: string;
};

export type ConversationEnded = {
type: "CONVERSATION_ENDED";
conversationId: string;
};

export type BotResponseRequested = {
type: "BOT_RESPONSE_REQUESTED";
conversationId: string;
correlationId: string;
};

export type BotResponseAdded = {
type: "BOT_RESPONSE_ADDED";
conversationId: string;
correlationId: string;
message: Message;
};

export type ConversationEvent =
| ConversationStarted
| UserMessageAdded
| BotResponseRequested
| BotResponseAdded
| ConversationEnded;
39 changes: 39 additions & 0 deletions src/domain/conversation/conversation.events.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { ConversationMessage } from "./conversation.dto";

export type ConversationStarted = {
type: "CONVERSATION_STARTED";
conversationId: string;
metadata: Record<string, string>;
};

export type UserMessageAdded = {
type: "USER_MESSAGE_ADDED";
conversationId: string;
authorUserId: string;
messageId: string;
};

export type ConversationEnded = {
type: "CONVERSATION_ENDED";
conversationId: string;
};

export type BotResponseRequested = {
type: "BOT_RESPONSE_REQUESTED";
conversationId: string;
correlationId: string;
};

export type BotResponseAdded = {
type: "BOT_RESPONSE_ADDED";
conversationId: string;
correlationId: string;
message: ConversationMessage;
};

export type ConversationEvent =
| ConversationStarted
| UserMessageAdded
| BotResponseRequested
| BotResponseAdded
| ConversationEnded;
4 changes: 2 additions & 2 deletions src/domain/conversation/trigger-bot.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Message } from "./conversation.dto";
import { ConversationMessage } from "./conversation.dto";

export type BotResponse =
| {
Expand All @@ -9,5 +9,5 @@ export type BotResponse =
| { type: "BOT_RESPONSE_ERROR"; correlationId: string; error: Error };

export interface TriggerBotService {
trigger(messages: Message[]): void;
trigger(messages: ConversationMessage[]): void;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export const slackSoftwareEngPersona: Persona = {
configName: "slack-software-eng",
name: "WiseGPT",
instructions: `You're a regular Slack user.
When providing code examples, use triple backticks and do not suffix the markdown shortcut for the language.
When providing code examples, use triple backticks.
You always like providing lengthy responses and explaining things.
You are helpful and descriptive.
You make well-informed decisions and cite your sources if you can.
Expand Down
Loading

0 comments on commit 28525ca

Please sign in to comment.