@nethru/kit 1.1.7 → 1.1.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,18 +1,21 @@
1
1
  import React, { createContext, useContext, useEffect, useRef, useState } from 'react';
2
2
  import { getConfig } from "../../../js/config";
3
+ import { isPageAnalysisIntent } from "../../../js/promptIntent";
3
4
  import { jsx as _jsx } from "react/jsx-runtime";
4
5
  const ChatContext = /*#__PURE__*/createContext();
5
6
  export function ChatProvider({
6
- children,
7
- defaultMessages = []
7
+ defaultProvider = 'OPENAI',
8
+ defaultModel = 'gpt-5-chat-latest',
9
+ defaultMessages = [],
10
+ children
8
11
  }) {
9
12
  const [messages, setMessages] = useState(defaultMessages);
10
13
  const [inputValue, setInputValue] = useState('');
11
14
  const [isLoading, setIsLoading] = useState(false);
12
15
  const [conversationId, setConversationId] = useState(null);
13
- const [provider, setProvider] = useState('OPENAI');
16
+ const [provider, setProvider] = useState(defaultProvider);
14
17
  const [providers, setProviders] = useState([]);
15
- const [model, setModel] = useState('gpt-4.1-mini');
18
+ const [model, setModel] = useState(defaultModel);
16
19
  const [tools, setTools] = useState([]);
17
20
  const chatContainerRef = useRef(null);
18
21
  const mainPageRef = useRef(null);
@@ -52,10 +55,48 @@ export function ChatProvider({
52
55
  console.error('Error loading tools:', error);
53
56
  }
54
57
  };
55
- const sendMessage = async () => {
58
+ async function answer(message) {
59
+ const {
60
+ apiUrl
61
+ } = getConfig();
62
+ const response = await fetch(`${apiUrl}/api/chat`, {
63
+ method: 'POST',
64
+ headers: {
65
+ 'Content-Type': 'application/json'
66
+ },
67
+ body: JSON.stringify({
68
+ conversationId: conversationId,
69
+ message: message,
70
+ provider: provider,
71
+ model: model
72
+ })
73
+ });
74
+ return await response.json();
75
+ }
76
+ async function analyzePage(message) {
77
+ if (!mainPageRef.current) throw new Error('No main page');
56
78
  const {
57
79
  apiUrl
58
80
  } = getConfig();
81
+ const html = mainPageRef.current?.innerHTML;
82
+ const response = await fetch(`${apiUrl}/api/pages/analyze`, {
83
+ method: 'POST',
84
+ headers: {
85
+ 'Content-Type': 'application/json'
86
+ },
87
+ body: JSON.stringify({
88
+ conversationId: conversationId,
89
+ message: message,
90
+ htmlContent: html,
91
+ pageUrl: window.location.href,
92
+ provider: provider,
93
+ model: model,
94
+ forceRefresh: true
95
+ })
96
+ });
97
+ return await response.json();
98
+ }
99
+ const sendMessage = async () => {
59
100
  const message = inputValue.trim();
60
101
  if (!message) return;
61
102
  const userMessage = {
@@ -69,21 +110,8 @@ export function ChatProvider({
69
110
  setInputValue('');
70
111
  setIsLoading(true);
71
112
  try {
72
- const response = await fetch(`${apiUrl}/api/chat`, {
73
- method: 'POST',
74
- headers: {
75
- 'Content-Type': 'application/json'
76
- },
77
- body: JSON.stringify({
78
- message: message,
79
- conversationId: conversationId,
80
- provider: provider,
81
- model: model
82
- })
83
- });
84
- let data = await response.json();
113
+ let data = isPageAnalysisIntent(message) ? await analyzePage(message) : await answer(message);
85
114
  setConversationId(data.conversationId);
86
- if (isPageAnalysisIntent(data)) data = await sendPageAnalysisRequest(data);
87
115
  setMessages(prev => [...prev, {
88
116
  role: data.role,
89
117
  kinds: data.kinds,
@@ -102,40 +130,6 @@ export function ChatProvider({
102
130
  setIsLoading(false);
103
131
  }
104
132
  };
105
- const isPageAnalysisIntent = data => {
106
- return data.content.filter(c => c.type === 'tool').some(c => c.name === 'detect-page-analysis-intent' && c.result === 'true');
107
- };
108
- const sendPageAnalysisRequest = async data => {
109
- if (!mainPageRef.current) return data;
110
- const {
111
- apiUrl
112
- } = getConfig();
113
- const html = mainPageRef.current?.innerHTML;
114
- try {
115
- const response = await fetch(`${apiUrl}/api/pages/analyze`, {
116
- method: 'POST',
117
- headers: {
118
- 'Content-Type': 'application/json'
119
- },
120
- body: JSON.stringify({
121
- htmlContent: html,
122
- pageUrl: window.location.href,
123
- provider: provider,
124
- model: model
125
- })
126
- });
127
- const json = await response.json();
128
- if (json.summary) {
129
- data.content = [{
130
- type: 'text',
131
- value: json.summary
132
- }];
133
- }
134
- } catch (error) {
135
- console.error('Error:', error);
136
- }
137
- return data;
138
- };
139
133
  const clearChat = () => {
140
134
  setMessages([]);
141
135
  setConversationId(null);
@@ -0,0 +1,22 @@
1
+ export function isPageAnalysisIntent(userPrompt) {
2
+ if (!userPrompt || userPrompt.trim() === "") return false;
3
+ const analysisKeywords = ["요약", "정리", "분석", "설명", "페이지", "화면", "글", "기사", "문서", "내용", "포스트", "블로그", "뭐야", "뭔가", "무엇", "어떤", "핵심", "주요", "포인트", "요점", "이", "현재", "지금", "여기", "summarize", "summary", "analyze", "analysis", "explain", "this page", "current page", "this article", "this post"];
4
+ const excludeKeywords = ["http://", "https://", "www.", "날씨", "weather", "뉴스", "news", "코드", "code", "프로그램", "program"];
5
+ const prompt = userPrompt.toLowerCase();
6
+
7
+ // 제외 키워드 포함 시, "이 페이지" 패턴만 예외 허용
8
+ if (excludeKeywords.some(k => prompt.includes(k))) {
9
+ if (!(prompt.includes("이") && prompt.includes("페이지"))) {
10
+ return false;
11
+ }
12
+ }
13
+ const matchedKeywords = analysisKeywords.filter(k => prompt.includes(k));
14
+
15
+ // 최소 2개 이상의 키워드 매칭
16
+ if (matchedKeywords.length >= 2) return true;
17
+
18
+ // 단일 키워드지만 강한 패턴
19
+ const strongPatterns = ["요약해", "정리해", "분석해", "설명해", "요약 해", "정리 해", "분석 해", "설명 해", "페이지 요약", "페이지 정리", "페이지 분석", "summarize this", "analyze this", "explain this"];
20
+ if (strongPatterns.some(p => prompt.includes(p))) return true;
21
+ return false;
22
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nethru/kit",
3
- "version": "1.1.7",
3
+ "version": "1.1.9",
4
4
  "description": "A React component library by Nethru",
5
5
  "main": "dist/index.js",
6
6
  "homepage": ".",