Skip to content

Commit 670fe1a

Browse files
refactor(constants): add role
1 parent 1011dbb commit 670fe1a

4 files changed

Lines changed: 23 additions & 7 deletions

File tree

src/components/Chat.tsx

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ import { Spinner, TextInput } from '@inkjs/ui';
22
import { Box, Text } from 'ink';
33
import { useCallback, useState } from 'react';
44

5+
import { ROLE } from '../constants';
56
import { ollama } from '../utils';
67

8+
const PROMPT_PREFIX = '> ';
9+
710
export function Chat() {
811
const [messages, setMessages] = useState<ollama.Message[]>([]);
912
const [submitKey, setSubmitKey] = useState(0);
@@ -18,14 +21,14 @@ export function Chat() {
1821
setIsLoading(true);
1922

2023
const userMessage: ollama.Message = {
21-
role: 'user',
24+
role: ROLE.USER,
2225
content: userContent,
2326
};
2427
setMessages((prev) => [...prev, userMessage]);
2528

2629
const updatedMessages = [...messages, userMessage];
2730
const assistantMessage: ollama.Message = {
28-
role: 'assistant',
31+
role: ROLE.ASSISTANT,
2932
content: '',
3033
};
3134
setMessages((prev) => [...prev, assistantMessage]);
@@ -57,8 +60,11 @@ export function Chat() {
5760
<Box flexDirection="column">
5861
<Box flexDirection="column">
5962
{messages.map((message, index) => (
60-
<Text key={index} color={message.role === 'user' ? 'green' : 'blue'}>
61-
{message.role === 'user' ? '> ' : ''}
63+
<Text
64+
key={index}
65+
color={message.role === ROLE.USER ? 'green' : 'blue'}
66+
>
67+
{message.role === ROLE.USER ? PROMPT_PREFIX : ''}
6268
{message.content}
6369
</Text>
6470
))}
@@ -69,7 +75,7 @@ export function Chat() {
6975
</Box>
7076

7177
<Box>
72-
<Text>&gt; </Text>
78+
<Text>{PROMPT_PREFIX}</Text>
7379
<TextInput
7480
key={submitKey}
7581
defaultValue=""

src/constants/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './role';

src/constants/role.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export const ROLE = {
2+
USER: 'user',
3+
ASSISTANT: 'assistant',
4+
SYSTEM: 'system',
5+
} as const;
6+
7+
export type Role = (typeof ROLE)[keyof typeof ROLE];

src/utils/ollama.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import { Ollama } from 'ollama';
22

3+
import type { Role } from '../constants';
4+
35
const OLLAMA_HOST = process.env.OLLAMA_HOST ?? 'http://localhost:11434';
46
const DEFAULT_MODEL = process.env.OLLAMA_MODEL ?? 'gemma4';
57

68
const client = new Ollama({ host: OLLAMA_HOST });
79

810
export interface Message {
9-
role: 'user' | 'assistant' | 'system';
11+
role: Role;
1012
content: string;
1113
}
1214

@@ -29,5 +31,5 @@ export async function* streamChat(
2931

3032
export async function listModels(): Promise<string[]> {
3133
const response = await client.list();
32-
return response.models.map((m) => m.name);
34+
return response.models.map(({ name }) => name);
3335
}

0 commit comments

Comments
 (0)