Skip to content

Commit

Permalink
bot wip
Browse files Browse the repository at this point in the history
  • Loading branch information
borisbat committed Apr 2, 2023
1 parent f52da00 commit 2b1b9e1
Show file tree
Hide file tree
Showing 7 changed files with 534 additions and 350 deletions.
8 changes: 5 additions & 3 deletions daslib/json.das
Original file line number Diff line number Diff line change
Expand Up @@ -257,12 +257,14 @@ def private parse_value ( var itv : iterator<Token>; var error : string & ) : Js
def read_json ( text : string implicit; var error : string& ) : JsonValue?
//! reads JSON from the `text` string.
//! if `error` is not empty, it contains the parsing error message.
error = ""
var lex <- lexer(text)
let res = parse_value(lex,error)
delete lex
return res

def read_json ( text : array<uint8>; var error : string& ) : JsonValue?
error = ""
var lex <- lexer(text)
let res = parse_value(lex,error)
delete lex
Expand Down Expand Up @@ -423,16 +425,16 @@ def try_fixing_broken_json ( var bad:string )
i = nested + 1
continue
// . we assume its nested quotes and we replace with \"
writer |> write("\\\"")
writer |> write("`")
i = nested + 1
while i < lstr && str[i]!='"'u8
writer |> write_char(int(str[i]))
i ++
if i >= lstr // if eof we done, we fix nested quote and and closing quote and we are done
writer |> write("\\\"")
writer |> write("`")
writer |> write_char('"')
break
// write the second replaced quote
writer |> write("\\\"")
writer |> write("`")
i ++
goto label 0
2 changes: 1 addition & 1 deletion modules/dasOpenAI/openai/openai.das
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ struct public Completion
best_of:int = 1 // Number of completions to consider when averaging logprobs or returning samples
user:string = "" // User ID for logged metrics

def private write_request_json ( JV:JsonValue? )
def public write_request_json ( JV:JsonValue? )
var ontz = set_no_trailing_zeros(true)
var nea = set_no_empty_arrays(true)
var text = write_json(JV)
Expand Down
112 changes: 109 additions & 3 deletions modules/dasTelegram/examples/chat_log.das
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ def private create_chat_table
if rc != SQLITE_OK
to_log(LOG_ERROR, "Error creating table users: {sqlite3_errmsg(g_db)}\n")
return false
sql = "CREATE TABLE IF NOT EXISTS summary_log (
chat_id INTEGER,
date INTEGER,
text TEXT);"
rc = sqlite3_exec(g_db, sql, null)
if rc != SQLITE_OK
to_log(LOG_ERROR, "Error creating table summary_log: {sqlite3_errmsg(g_db)}\n")
return false
return true

def public create_chat_log ( dbname:string )
Expand Down Expand Up @@ -74,6 +82,11 @@ struct public ChatLogEntry
text : string
transcription : string

struct public SummaryEntry
chat_id : int64
date : int64
text : string

def public write_to_log ( entry:ChatLogEntry )
var sql = "INSERT INTO chat_log (chat_id, message_id, date, from_id, reply_to_message, text, transcription) VALUES (?, ?, ?, ?, ?, ?, ?)"
var stmt : sqlite3_stmt?
Expand Down Expand Up @@ -116,6 +129,24 @@ def public write_user ( user:UserEntry )
finally
sqlite3_finalize(stmt)

def public write_summary ( summary:SummaryEntry )
var sql = "INSERT OR IGNORE INTO summary_log (chat_id, date, text) VALUES (?, ?, ?)"
var stmt : sqlite3_stmt?
var rc = sqlite3_prepare_v2(g_db, sql, -1, unsafe(addr(stmt)), null)
if rc != SQLITE_OK
to_log(LOG_ERROR, "Error preparing statement: {sqlite3_errmsg(g_db)}\n")
return false
sqlite3_bind_int64(stmt, 1, summary.chat_id)
sqlite3_bind_int64(stmt, 2, summary.date)
sqlite3_bind_text(stmt, 3, summary.text)
rc = sqlite3_step(stmt)
if rc != SQLITE_DONE
to_log(LOG_ERROR, "Error inserting data: {sqlite3_errmsg(g_db)}\n")
return false
return true
finally
sqlite3_finalize(stmt)

def public list_unique_users ( chat_id:int64 ) : array<int64>
var sql = "SELECT DISTINCT from_id FROM chat_log WHERE chat_id = ?"
var stmt : sqlite3_stmt?
Expand Down Expand Up @@ -172,14 +203,63 @@ finally
sqlite3_finalize(stmt)

def public list_chat_log ( chat_id:int64; last_n_entries:int; blk:block<(entry:ChatLogEntry):void>)
var sql = "SELECT message_id, date, from_id, reply_to_message, text, transcription FROM chat_log WHERE chat_id = ? ORDER BY date DESC LIMIT ?"
var sql = "SELECT message_id, date, from_id, reply_to_message, text, transcription
FROM chat_log
WHERE chat_id = @cid
ORDER BY date DESC
LIMIT @limit"
var stmt : sqlite3_stmt?
var rc = sqlite3_prepare_v2(g_db, sql, -1, unsafe(addr(stmt)), null)
if rc != SQLITE_OK
to_log(LOG_ERROR, "Error preparing statement: {sqlite3_errmsg(g_db)}\n")
return false
sqlite3_bind_int64(stmt, 1, chat_id)
sqlite3_bind_int(stmt, 2, last_n_entries)
sqlite3_bind_int64(stmt, sqlite3_bind_parameter_index(stmt, "@cid"), chat_id)
sqlite3_bind_int(stmt, sqlite3_bind_parameter_index(stmt, "@limit"), last_n_entries)
while true
rc = sqlite3_step(stmt)
if rc == SQLITE_DONE
break
if rc != SQLITE_ROW
to_log(LOG_ERROR, "Error reading data: {sqlite3_errmsg(g_db)}\n")
return false
var entry <- [[ChatLogEntry
chat_id = chat_id,
message_id = sqlite3_column_int64(stmt, 0),
date = sqlite3_column_int64(stmt, 1),
from_id = sqlite3_column_int64(stmt, 2),
reply_to_message = sqlite3_column_int64(stmt, 3),
text = sqlite3_column_text_(stmt, 4),
transcription = sqlite3_column_text_(stmt, 5)
]]
invoke(blk,entry)
return true
finally
sqlite3_finalize(stmt)

def public list_conversation_log ( chat_id, bot_id:int64; n_entries, n_noise_entries:int; blk:block<(entry:ChatLogEntry):void>)
var sql = "SELECT message_id, date, from_id, reply_to_message, text, transcription
FROM chat_log
WHERE chat_id = @cid
AND (from_id = @botid
OR reply_to_message = @botid
OR message_id IN (
SELECT message_id
FROM chat_log
ORDER BY date DESC
LIMIT @noise_limit
)
)
ORDER BY date DESC
LIMIT @limit;"
var stmt : sqlite3_stmt?
var rc = sqlite3_prepare_v2(g_db, sql, -1, unsafe(addr(stmt)), null)
if rc != SQLITE_OK
to_log(LOG_ERROR, "Error preparing statement: {sqlite3_errmsg(g_db)}\n")
return false
sqlite3_bind_int64(stmt, sqlite3_bind_parameter_index(stmt, "@cid"), chat_id)
sqlite3_bind_int64(stmt, sqlite3_bind_parameter_index(stmt, "@botid"), bot_id)
sqlite3_bind_int(stmt, sqlite3_bind_parameter_index(stmt, "@noise_limit"), n_noise_entries)
sqlite3_bind_int(stmt, sqlite3_bind_parameter_index(stmt, "@limit"), n_entries)
while true
rc = sqlite3_step(stmt)
if rc == SQLITE_DONE
Expand All @@ -200,3 +280,29 @@ def public list_chat_log ( chat_id:int64; last_n_entries:int; blk:block<(entry:C
return true
finally
sqlite3_finalize(stmt)

def public get_first_summary ( chat_id:int64; dayZ:int64 ) : tuple<date:int64;text:string>
var sql = "SELECT date, text
FROM summary_log
WHERE chat_id=@cid AND date<@dayZ
ORDER BY date DESC LIMIT 1;"
var stmt : sqlite3_stmt?
var rc = sqlite3_prepare_v2(g_db, sql, -1, unsafe(addr(stmt)), null)
if rc != SQLITE_OK
to_log(LOG_ERROR, "Error preparing statement: {sqlite3_errmsg(g_db)}\n")
return [[auto -1l, ""]]
sqlite3_bind_int64(stmt, sqlite3_bind_parameter_index(stmt, "@cid"), chat_id)
sqlite3_bind_int64(stmt, sqlite3_bind_parameter_index(stmt, "@dayZ"), dayZ)
rc = sqlite3_step(stmt)
if rc == SQLITE_DONE
to_log(LOG_ERROR, "Error reading data: {sqlite3_errmsg(g_db)}\n")
return [[auto -1l, ""]]
if rc != SQLITE_ROW
to_log(LOG_ERROR, "Error reading data: {sqlite3_errmsg(g_db)}\n")
return [[auto -1l, ""]]
return [[auto sqlite3_column_int64(stmt,0), sqlite3_column_text_(stmt, 1)]]
finally
sqlite3_finalize(stmt)

options debugger
require daslib/debug
7 changes: 7 additions & 0 deletions modules/dasTelegram/examples/emoji.das
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,11 @@ def public fix_broken_html_hex ( str:string )
let utf32 = to_uint(code, true)
return string(utf8_encode(utf32))

def public fix_broken_escaping ( prompt:string )
return build_string <| $ ( writer )
for Ch in prompt
if Ch<=31
writer |> write("\\u00{(Ch>>4)+'0'}{(Ch&15)+'0'}")
else
writer |> write_char(Ch)

Loading

0 comments on commit 2b1b9e1

Please sign in to comment.