Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Realtime API関連のバグフィックス #188

Merged
merged 4 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/components/messageInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export const MessageInput = ({
iconName="24/Send"
className="bg-secondary hover:bg-secondary-hover active:bg-secondary-press disabled:bg-secondary-disabled"
isProcessing={chatProcessing}
disabled={chatProcessing || !userMessage}
disabled={chatProcessing || !userMessage || realtimeAPIMode}
onClick={onClickSendButton}
/>
</div>
Expand Down
6 changes: 5 additions & 1 deletion src/components/settings/modelProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ const ModelProvider = () => {
isPlaying: false,
})
}

if (newService !== 'openai' && newService !== 'azure') {
settingsStore.setState({ realtimeAPIMode: false })
}
},
[]
)
Expand Down Expand Up @@ -319,7 +323,7 @@ const ModelProvider = () => {
https://RESOURCE_NAME.openai.azure.com/openai/deployments/DEPLOYMENT_NAME/chat/completions?api-version=API_VERSION
<br />
Realtime API ex.
https://RESOURCE_NAME.openai.azure.com/openai/realtime?api-version=API_VERSION&deployment=DEPLOYMENT_NAME
wss://RESOURCE_NAME.openai.azure.com/openai/realtime?api-version=API_VERSION&deployment=DEPLOYMENT_NAME
</div>
<input
className="text-ellipsis px-16 py-8 w-col-span-2 bg-surface1 hover:bg-surface1-hover rounded-8"
Expand Down
5 changes: 1 addition & 4 deletions src/components/useRealtimeAPI.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -276,10 +276,7 @@ const useRealtimeAPI = ({ handleReceiveTextFromRt }: Params) => {
'openai-beta.realtime-v1',
])
} else if (ss.selectAIService === 'azure') {
const url =
`${ss.azureEndpoint}&api-key=${ss.azureKey}` ||
`${process.env.AZURE_ENDPOINT}&api-key=${ss.azureKey}` ||
''
const url = `${ss.azureEndpoint}&api-key=${ss.azureKey}`
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Azure WebSocket URLの構築を簡素化

URLの構築が簡素化されており、コードの可読性が向上しています。しかし、以下の点について検討が必要です:

  1. ss.azureEndpointss.azureKeyの値が常に設定されていることを前提としています。これらの値が未設定の場合、エラーが発生する可能性があります。

  2. セキュリティの観点から、APIキーをURLに直接含めることは推奨されません。

以下の改善を提案します:

  1. ss.azureEndpointss.azureKeyの存在と有効性を確認するバリデーションを追加してください。

  2. これらの値が設定されていない場合のエラーハンドリングを実装してください。

  3. APIキーをURLパラメータではなく、WebSocketの接続後にメッセージとして送信することを検討してください。

例:

if (!ss.azureEndpoint || !ss.azureKey) {
  console.error('Azure endpoint or key is not set');
  return null;
}

const url = `${ss.azureEndpoint}`;
ws = new WebSocket(url, []);

// WebSocket接続後にAPIキーを送信
ws.addEventListener('open', () => {
  ws.send(JSON.stringify({ type: 'authentication', key: ss.azureKey }));
});

この方法により、コードの堅牢性とセキュリティが向上します。

ws = new WebSocket(url, [])
} else {
return null
Expand Down
6 changes: 0 additions & 6 deletions src/features/chat/vercelAIChat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@ const getAIConfig = () => {
const apiKeyName = `${aiService}Key` as const
const apiKey = ss[apiKeyName]

if (!apiKey) {
throw new Error(
`API key for ${aiService} is missing. Unable to proceed with the AI service.`
)
}

return {
aiApiKey: apiKey,
selectAIService: aiService,
Expand Down
6 changes: 0 additions & 6 deletions src/features/slide/slideAIHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,6 @@ export const judgeSlide = async (
const apiKeyName = `${aiService}Key` as const
const apiKey = ss[apiKeyName]

if (!apiKey) {
throw new Error(
`API key for ${aiService} is missing. Unable to proceed with the AI service.`
)
}

const systemMessage = `
You are an AI tasked with determining whether a user's comment is a question about a given script document and supplementary text, and if so, which page of the document is most relevant to the question. Follow these instructions carefully:

Expand Down
7 changes: 6 additions & 1 deletion src/features/stores/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,12 @@ const settingsStore = create<SettingsState>()(
process.env.NEXT_PUBLIC_CHANGE_ENGLISH_TO_JAPANESE === 'true',
showControlPanel: process.env.NEXT_PUBLIC_SHOW_CONTROL_PANEL !== 'false',
webSocketMode: process.env.NEXT_PUBLIC_WEB_SOCKET_MODE === 'true',
realtimeAPIMode: process.env.NEXT_PUBLIC_REALTIME_API_MODE === 'true',
realtimeAPIMode:
(process.env.NEXT_PUBLIC_REALTIME_API_MODE === 'true' &&
['openai', 'azure'].includes(
process.env.NEXT_PUBLIC_SELECT_AI_SERVICE as AIService
)) ||
false,
slideMode: process.env.NEXT_PUBLIC_SLIDE_MODE === 'true',
messageReceiverEnabled: false,
clientId: '',
Expand Down
Loading