Skip to content

Commit

Permalink
Merge pull request #87 from aws-samples/chat-title
Browse files Browse the repository at this point in the history
会話履歴のページは header にタイトルを出すようにする
  • Loading branch information
wadabee authored Oct 4, 2023
2 parents b8f4dbc + 65170fc commit d0c2327
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 15 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

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

8 changes: 4 additions & 4 deletions packages/cdk/lambda/utils/sagemakerApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ async function* invokeStream(
// b'data:{"token": {"text": " problem"
// b'}}'
//
// While usually each PayloadPart event from the event stream will contain a byte array
// While usually each PayloadPart event from the event stream will contain a byte array
// with a full json, this is not guaranteed and some of the json objects may be split across
// PayloadPart events. For example:
// {'PayloadPart': {'Bytes': b'{"outputs": '}}
// {'PayloadPart': {'Bytes': b'[" problem"]}\n'}}
//
// This logic accounts for this by concatenating bytes and
// return lines (ending with a '\n' character) within the buffer.
// It will also save any pending lines that doe not end with a '\n'
// This logic accounts for this by concatenating bytes and
// return lines (ending with a '\n' character) within the buffer.
// It will also save any pending lines that doe not end with a '\n'
// to make sure truncations are concatinated.

let buffer = '';
Expand Down
6 changes: 5 additions & 1 deletion packages/cdk/lib/construct/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,11 @@ export class Api extends Construct {
const sagemakerPolicy = new PolicyStatement({
effect: Effect.ALLOW,
actions: ['sagemaker:DescribeEndpoint', 'sagemaker:InvokeEndpoint'],
resources: [`arn:aws:sagemaker:${modelRegion}:${Stack.of(this).account}:endpoint/${modelName}`],
resources: [
`arn:aws:sagemaker:${modelRegion}:${
Stack.of(this).account
}:endpoint/${modelName}`,
],
});
predictFunction.role?.addToPrincipalPolicy(sagemakerPolicy);
predictStreamFunction.role?.addToPrincipalPolicy(sagemakerPolicy);
Expand Down
28 changes: 19 additions & 9 deletions packages/web/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState, useEffect } from 'react';
import React, { useMemo } from 'react';
import { useLocation } from 'react-router-dom';
import {
PiDotsThreeVertical,
Expand All @@ -20,6 +20,7 @@ import '@aws-amplify/ui-react/styles.css';
import MenuDropdown from './components/MenuDropdown';
import MenuItem from './components/MenuItem';
import useDrawer from './hooks/useDrawer';
import useConversation from './hooks/useConversation';

const items = [
{
Expand Down Expand Up @@ -59,10 +60,13 @@ const items = [
},
];

const getLabelByPath = (_path: string) => {
// MEMO: /chat/:chatId の path の場合に件名が表示されないため、以下の実装としている
const path = '/' + _path.split('/')[1];
return items.find((i) => i.to === path)?.label || '';
// /chat/:chatId の形式から :chatId を返す
// path が別の形式の場合は null を返す
const extractChatId = (path: string): string | null => {
const pattern = /\/chat\/(.+)/;
const match = path.match(pattern);

return match ? match[1] : null;
};

const App: React.FC = () => {
Expand All @@ -79,12 +83,18 @@ const App: React.FC = () => {
I18n.setLanguage('ja');

const { switchOpen: switchDrawer } = useDrawer();
const [label, setLabel] = useState('');
const { pathname } = useLocation();
const { getConversationTitle } = useConversation();

const label = useMemo(() => {
const chatId = extractChatId(pathname);

useEffect(() => {
setLabel(getLabelByPath(pathname));
}, [pathname]);
if (chatId) {
return getConversationTitle(chatId) || '';
} else {
return items.find((i) => i.to === pathname)?.label || '';
}
}, [pathname, getConversationTitle]);

return (
<Authenticator
Expand Down
11 changes: 11 additions & 0 deletions packages/web/src/hooks/useConversation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,24 @@ const useConversation = () => {
mutate();
});
};
const getConversationTitle = (chatId: string) => {
const idx =
data?.chats.findIndex((d) => d.chatId === `chat#${chatId}`) ?? -1;

if (idx > -1) {
return data!.chats[idx].title;
} else {
return null;
}
};

return {
loading: isLoading,
conversations: data ? data.chats : [],
mutate,
updateConversationTitle,
deleteConversation,
getConversationTitle,
};
};

Expand Down

0 comments on commit d0c2327

Please sign in to comment.