5
|
1 local Luan = require "luan:Luan.luan"
|
|
2 local error = Luan.error
|
6
|
3 local ipairs = Luan.ipairs or error()
|
|
4 local type = Luan.type or error()
|
|
5 local String = require "luan:String.luan"
|
|
6 local starts_with = String.starts_with or error()
|
|
7 local Html = require "luan:Html.luan"
|
|
8 local html_encode = Html.encode or error()
|
|
9 local Parsers = require "luan:Parsers.luan"
|
|
10 local json_parse = Parsers.json_parse or error()
|
|
11 local json_string = Parsers.json_string or error()
|
|
12 local Claude = require "site:/lib/ai/claude/Claude.luan"
|
|
13 local claude_chat = Claude.chat or error()
|
|
14 local Logging = require "luan:logging/Logging.luan"
|
|
15 local logger = Logging.logger "claude/Chat"
|
|
16 local Thread = require "luan:Thread.luan"
|
5
|
17
|
|
18
|
|
19 local Chat = {}
|
|
20
|
9
|
21 function Chat.output_system_prompt(thread)
|
|
22 if thread == nil then
|
|
23 return
|
|
24 end
|
|
25 thread = json_parse(thread)
|
|
26 local system_prompt = thread.system or error
|
|
27 system_prompt = html_encode(system_prompt)
|
|
28 %><%=system_prompt%><%
|
|
29 end
|
|
30
|
5
|
31 function Chat.output_messages_html(thread)
|
9
|
32 if thread == nil then
|
|
33 return
|
|
34 end
|
|
35 thread = json_parse(thread)
|
|
36 local messages = thread.messages or error
|
6
|
37 for _, message in ipairs(messages) do
|
|
38 local role = message.role or error()
|
|
39 local who
|
|
40 if role=="assistant" then
|
|
41 who = "Claude"
|
|
42 elseif role=="user" then
|
|
43 who = "You"
|
|
44 else
|
|
45 error(role)
|
|
46 end
|
|
47 local function output(text)
|
9
|
48 text = html_encode(text)
|
6
|
49 %>
|
7
|
50 <h3><%=who%></h3>
|
9
|
51 <div markdown role="<%=role%>"><%=text%></div>
|
6
|
52 <%
|
|
53 end
|
|
54 local content = message.content or error()
|
|
55 if type(content) == "string" then
|
|
56 output(content)
|
|
57 else
|
|
58 for _, part in ipairs(content) do
|
|
59 if part.type=="text" then
|
|
60 local text = part.text or error()
|
|
61 output(text)
|
|
62 end
|
|
63 end
|
|
64 end
|
|
65 end_for
|
5
|
66 end
|
|
67
|
9
|
68
|
|
69 local system_prompt = [[
|
|
70 CRITICAL REQUIREMENT: When writing Japanese, use ruby markdown syntax {japanese|romaji} for pronunciation guidance.
|
|
71
|
|
72 Apply ruby tags to meaningful pronunciation units:
|
|
73 - Individual kanji or kanji compounds: {私|watashi}, {学生|gakusei}
|
|
74 - Hiragana/katakana words and particles: {は|wa}, {です|desu}, {ありがとう|arigatō}
|
|
75 - Grammatical elements: {ました|mashita}, {ません|masen}
|
|
76
|
|
77 The romaji must reflect ACTUAL PRONUNCIATION, not character-by-character readings.
|
|
78 Use macrons for long vowels: ā, ī, ū, ē, ō
|
|
79
|
|
80 APPLIES TO ALL JAPANESE TEXT: Example sentences, grammar explanations, vocabulary lists, casual mentions - ANY Japanese characters in your response need ruby tags.
|
|
81
|
|
82 VERIFICATION STEP: Before sending, scan your ENTIRE response for any Japanese characters (hiragana, katakana, kanji) and ensure they all have ruby tags.
|
|
83 ]]
|
|
84
|
|
85
|
5
|
86 function Chat.ask(thread,input)
|
9
|
87 thread = thread and json_parse(thread) or {
|
|
88 system = system_prompt
|
|
89 messages = {nil}
|
|
90 }
|
|
91 local messages = thread.messages or error
|
6
|
92 messages[#messages+1] = {
|
|
93 role = "user"
|
|
94 content = input
|
|
95 }
|
|
96 --[=[
|
|
97 messages[#messages+1] = {
|
|
98 role = "assistant"
|
|
99 content = [[
|
|
100 - 1
|
|
101 - 2
|
|
102 - 3
|
|
103 - 4
|
|
104 - 5
|
|
105 - 6
|
|
106 - 7
|
|
107 - 8
|
|
108 - 9
|
|
109 - 10
|
|
110 ]]
|
|
111 Thread.sleep(5000)
|
|
112 }
|
|
113 if true then
|
9
|
114 return json_string(thread)
|
6
|
115 end
|
|
116 --]=]
|
9
|
117 local resultJson = claude_chat(thread)
|
6
|
118 local result = json_parse(resultJson)
|
|
119 -- logger.info(json_string(result))
|
|
120 result.type == "message" or error()
|
|
121 result.role == "assistant" or error()
|
|
122 result.stop_reason == "end_turn" or result.stop_reason == "tool_use" or error()
|
|
123 local content = result.content or error()
|
|
124 messages[#messages+1] = {
|
|
125 role = "assistant"
|
|
126 content = content
|
|
127 }
|
9
|
128 return json_string(thread)
|
5
|
129 end
|
|
130
|
|
131 return Chat
|