Skip to content

Commit

Permalink
remove assistantContent, add associatedUserMessageId for item level m…
Browse files Browse the repository at this point in the history
…essages
  • Loading branch information
atierian committed Sep 25, 2024
1 parent 9102419 commit 9c89563
Show file tree
Hide file tree
Showing 12 changed files with 95 additions and 173 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ exports[`ConversationTransformer valid schemas should transform conversation rou
"import { util } from '@aws-appsync/utils';
export function request(ctx) {
const { args, request, prev } = ctx;
const { args, request } = ctx;
const selectionSet = 'id conversationId content { image { format source { bytes }} text toolUse { toolUseId name input } toolResult { status toolUseId content { json text image { format source { bytes }} document { format name source { bytes }} }}} role owner createdAt updatedAt';
const graphqlApiEndpoint = '",
Expand All @@ -62,7 +62,6 @@ export function request(ctx) {
},
"';
const messages = prev.result.items;
const responseMutation = {
name: 'createAssistantResponsePirateChat',
inputTypeName: 'CreateConversationMessagePirateChatAssistantInput',
Expand Down Expand Up @@ -182,7 +181,7 @@ exports[`ConversationTransformer valid schemas should transform conversation rou
"import { util } from '@aws-appsync/utils';
export function request(ctx) {
const { args, request, prev } = ctx;
const { args, request } = ctx;
const toolDefinitions = {"tools":[{"name":"listTodos","description":"lists todos","inputSchema":{"json":{"type":"object","properties":{},"required":[]}},"graphqlRequestInputDescriptor":{"selectionSet":"items { content isDone id createdAt updatedAt owner } nextToken","propertyTypes":{},"queryName":"listTodos"}}]};
const selectionSet = 'id conversationId content { image { format source { bytes }} text toolUse { toolUseId name input } toolResult { status toolUseId content { json text image { format source { bytes }} document { format name source { bytes }} }}} role owner createdAt updatedAt';
const graphqlApiEndpoint = '",
Expand All @@ -194,7 +193,6 @@ export function request(ctx) {
},
"';
const messages = prev.result.items;
const responseMutation = {
name: 'createAssistantResponsePirateChat',
inputTypeName: 'CreateConversationMessagePirateChatAssistantInput',
Expand Down Expand Up @@ -316,7 +314,7 @@ exports[`ConversationTransformer valid schemas should transform conversation rou
"import { util } from '@aws-appsync/utils';
export function request(ctx) {
const { args, request, prev } = ctx;
const { args, request } = ctx;
const toolDefinitions = {"tools":[{"name":"listCustomers","description":"Provides data about the customer sending a message","inputSchema":{"json":{"type":"object","properties":{},"required":[]}},"graphqlRequestInputDescriptor":{"selectionSet":"items { name email activeCart { products { name price } customerId id createdAt updatedAt owner } orderHistory { items { products { name price } customerId id createdAt updatedAt owner } nextToken } id createdAt updatedAt owner } nextToken","propertyTypes":{},"queryName":"listCustomers"}}]};
const selectionSet = 'id conversationId content { image { format source { bytes }} text toolUse { toolUseId name input } toolResult { status toolUseId content { json text image { format source { bytes }} document { format name source { bytes }} }}} role owner createdAt updatedAt';
const graphqlApiEndpoint = '",
Expand All @@ -328,7 +326,6 @@ export function request(ctx) {
},
"';
const messages = prev.result.items;
const responseMutation = {
name: 'createAssistantResponsePirateChat',
inputTypeName: 'CreateConversationMessagePirateChatAssistantInput',
Expand Down Expand Up @@ -450,7 +447,7 @@ exports[`ConversationTransformer valid schemas should transform conversation rou
"import { util } from '@aws-appsync/utils';
export function request(ctx) {
const { args, request, prev } = ctx;
const { args, request } = ctx;
const toolDefinitions = {"tools":[{"name":"getTemperature","description":"does a thing","inputSchema":{"json":{"type":"object","properties":{"city":{"type":"string","description":"A UTF-8 character sequence."}},"required":["city"]}},"graphqlRequestInputDescriptor":{"selectionSet":"value unit","propertyTypes":{"city":"String!"},"queryName":"getTemperature"}},{"name":"plus","description":"does a different thing","inputSchema":{"json":{"type":"object","properties":{"a":{"type":"number","description":"A signed 32-bit integer value."},"b":{"type":"number","description":"A signed 32-bit integer value."}},"required":[]}},"graphqlRequestInputDescriptor":{"selectionSet":"","propertyTypes":{"a":"Int","b":"Int"},"queryName":"plus"}}]};
const selectionSet = 'id conversationId content { image { format source { bytes }} text toolUse { toolUseId name input } toolResult { status toolUseId content { json text image { format source { bytes }} document { format name source { bytes }} }}} role owner createdAt updatedAt';
const graphqlApiEndpoint = '",
Expand All @@ -462,7 +459,6 @@ export function request(ctx) {
},
"';
const messages = prev.result.items;
const responseMutation = {
name: 'createAssistantResponsePirateChat',
inputTypeName: 'CreateConversationMessagePirateChatAssistantInput',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ describe('ConversationTransformer', () => {
expect(
out.stacks.ConversationMessagePirateChat.Resources![`ListConversationMessage${toUpper(routeName)}Resolver`].Properties
.PipelineConfig.Functions,
).toHaveLength(5);
).toHaveLength(4);
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ interface ConversationMessage {
content: [ContentBlock]
context: AWSJSON
toolConfiguration: ToolConfiguration
associatedUserMessageId: ID
}

input DocumentBlockSourceInput {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,12 +227,12 @@ const constructConversationMessageModel = (
const content = makeField('content', [], makeListType(makeNamedType('ContentBlock')));
const context = makeField('aiContext', [], makeNamedType('AWSJSON'));
const uiComponents = makeField('toolConfiguration', [], makeNamedType('ToolConfiguration'));
const assistantContent = makeField('assistantContent', [], makeListType(makeNamedType('ContentBlock')));
const associatedUserMessageId = makeField('associatedUserMessageId', [], makeNamedType('ID'));

const object = {
...blankObject(modelName),
interfaces: [conversationMessageInterface],
fields: [id, conversationId, sessionField, role, content, context, uiComponents, assistantContent],
fields: [id, conversationId, sessionField, role, content, context, uiComponents, associatedUserMessageId],
directives: typeDirectives,
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,48 @@
import { util } from '@aws-appsync/utils';
import * as ddb from '@aws-appsync/utils/dynamodb';

/**
* Sends a request to the attached data source
* @param {import('@aws-appsync/utils').Context} ctx the context
* @returns {*} the request
*/
export function request(ctx) {
const owner = ctx.identity['claims']['sub'];
ctx.stash.owner = owner;
const { conversationId, content, associatedUserMessageId } = ctx.args.input;
const updatedAt = util.time.nowISO8601();
const { owner } = ctx.args;
const defaultValues = ctx.stash.defaultValues ?? {};

const expression = 'SET #assistantContent = :assistantContent, #updatedAt = :updatedAt';
const expressionNames = { '#assistantContent': 'assistantContent', '#updatedAt': 'updatedAt' };
const expressionValues = { ':assistantContent': content, ':updatedAt': updatedAt };
const condition = JSON.parse(
util.transform.toDynamoDBConditionExpression({
owner: { eq: owner },
conversationId: { eq: conversationId },
}),
);
return {
operation: 'UpdateItem',
key: util.dynamodb.toMapValues({ id: associatedUserMessageId }),
condition,
update: {
expression,
expressionNames,
expressionValues: util.dynamodb.toMapValues(expressionValues),
},
const message = {
__typename: '[[CONVERSATION_MESSAGE_TYPE_NAME]]',
id,
role: 'assistant',
content,
conversationId,
associatedUserMessageId,
owner,
...defaultValues,
};

return ddb.put({ key: { id }, item: message });

// const expression = 'SET #assistantContent = :assistantContent, #updatedAt = :updatedAt';
// const expressionNames = { '#assistantContent': 'assistantContent', '#updatedAt': 'updatedAt' };
// const expressionValues = { ':assistantContent': content, ':updatedAt': updatedAt };
// const condition = JSON.parse(
// util.transform.toDynamoDBConditionExpression({
// owner: { eq: owner },
// conversationId: { eq: conversationId },
// }),
// );
// return {
// operation: 'UpdateItem',
// key: util.dynamodb.toMapValues({ id: associatedUserMessageId }),
// condition,
// update: {
// expression,
// expressionNames,
// expressionValues: util.dynamodb.toMapValues(expressionValues),
// },
// };
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { MappingTemplateProvider } from '@aws-amplify/graphql-transformer-interf
import fs from 'fs';
import path from 'path';
import { ConversationDirectiveConfiguration } from '../grapqhl-conversation-transformer';
import { toUpper } from 'graphql-transformer-common';

/**
* Creates and returns the mapping template for the assistant mutation resolver.
Expand All @@ -11,7 +12,15 @@ import { ConversationDirectiveConfiguration } from '../grapqhl-conversation-tran
* @returns {MappingTemplateProvider} An object containing request and response MappingTemplateProviders.
*/
export const assistantMutationResolver = (config: ConversationDirectiveConfiguration): MappingTemplateProvider => {
const resolver = fs.readFileSync(path.join(__dirname, 'assistant-mutation-resolver-fn.js'), 'utf8');
const fieldName = toUpper(config.field.name.value);
const substitutions = {
CONVERSATION_MESSAGE_TYPE_NAME: `ConversationMessage${fieldName}`,
};
let resolver = fs.readFileSync(path.join(__dirname, 'assistant-mutation-resolver-fn.js'), 'utf8');
Object.entries(substitutions).forEach(([key, value]) => {
const replaced = resolver.replace(new RegExp(`\\[\\[${key}\\]\\]`, 'g'), value);
resolver = replaced;
});
const templateName = `Mutation.${config.field.name.value}.assistant-response.js`;
return MappingTemplate.s3MappingFunctionCodeFromString(resolver, templateName);
};
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { util } from '@aws-appsync/utils';

export function request(ctx) {
const { args, request, prev } = ctx;
const { args, request } = ctx;
[[TOOL_DEFINITIONS_LINE]]
const selectionSet = '[[SELECTION_SET]]';
const graphqlApiEndpoint = '[[GRAPHQL_API_ENDPOINT]]';

const messages = prev.result.items;
const responseMutation = {
name: '[[RESPONSE_MUTATION_NAME]]',
inputTypeName: '[[RESPONSE_MUTATION_INPUT_TYPE_NAME]]',
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit 9c89563

Please sign in to comment.