-
Notifications
You must be signed in to change notification settings - Fork 77
/
message-history-resolver.ts
87 lines (76 loc) · 3.22 KB
/
message-history-resolver.ts
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
import { MappingTemplate } from '@aws-amplify/graphql-transformer-core';
import { MappingTemplateProvider } from '@aws-amplify/graphql-transformer-interfaces';
import { dedent } from 'ts-dedent';
import { JSResolverFunctionProvider } from './js-resolver-function-provider';
/**
* Creates a mapping template for reading message history in a conversation.
*
* @returns {JSResolverFunctionProvider} An object containing request and response mapping functions.
*/
export const readHistoryMappingTemplate = (): JSResolverFunctionProvider => {
// TODO: filter to only retrieve messages that have an assistant response.
const req = createMessageHistoryRequestFunction();
const res = createMessageHistoryResponseFunction();
return { req, res };
};
/**
* Creates a request mapping template for reading message history in a conversation.
*
* @returns {MappingTemplateProvider} A mapping template provider for the request function.
*/
const createMessageHistoryRequestFunction = (): MappingTemplateProvider => {
const requestFunctionString = `
export function request(ctx) {
const { conversationId } = ctx.args;
const { authFilter } = ctx.stash;
const limit = 100;
const query = {
expression: 'conversationId = :conversationId',
expressionValues: util.dynamodb.toMapValues({
':conversationId': ctx.args.conversationId
})
};
const filter = JSON.parse(util.transform.toDynamoDBFilterExpression(authFilter));
const index = 'gsi-ConversationMessage.conversationId.createdAt';
return {
operation: 'Query',
query,
filter,
index,
scanIndexForward: false,
}
}`;
return MappingTemplate.inlineTemplateFromString(dedent(requestFunctionString));
};
/**
* Creates a response mapping template for reading message history in a conversation.
*
* @returns {MappingTemplateProvider} A mapping template provider for the response function.
*/
const createMessageHistoryResponseFunction = (): MappingTemplateProvider => {
const responseFunctionString = `
export function response(ctx) {
if (ctx.error) {
util.error(ctx.error.message, ctx.error.type);
}
const messagesWithAssistantResponse = ctx.result.items
.filter((message) => message.assistantContent !== undefined)
.reduce((acc, current) => {
const { content, assistantContent, aiContext } = current;
const userContent = aiContext
? [...content, { text: JSON.stringify(aiContext) }]
: content;
acc.push({ role: 'user', content: userContent });
acc.push({ role: 'assistant', content: assistantContent });
return acc;
}, [])
const { content, aiContext } = ctx.prev.result;
const currentUserMessageContent = aiContext
? [...content, { text: JSON.stringify(aiContext) }]
: content;
const currentMessage = { role: 'user', content: currentUserMessageContent };
const items = [...messagesWithAssistantResponse, currentMessage];
return { items };
}`;
return MappingTemplate.inlineTemplateFromString(dedent(responseFunctionString));
};