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