Skip to content

Commit a65f4a0

Browse files
committed
SimpleChatTC:System Date and Time
1 parent 3b61b03 commit a65f4a0

File tree

3 files changed

+84
-2
lines changed

3 files changed

+84
-2
lines changed

tools/server/public_simplechat/datautils.mjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@ export function trim_hist_garbage_at_end(sIn, maxType, maxUniq, maxMatchLenThres
125125
let iNum = 0;
126126
let iOth = 0;
127127
// Learn
128+
/**
129+
* @type {Object<string, number>}
130+
*/
128131
let hist = {};
129132
let iUniq = 0;
130133
for(let i=0; i<maxMatchLenThreshold; i++) {

tools/server/public_simplechat/readme.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ remember to
107107
* review and update this to match your needs.
108108
* the shared bearer token between server and client ui
109109

110-
* other builtin tool / function calls like calculator, javascript runner, DataStore dont require the
111-
simpleproxy.py helper.
110+
* other builtin tool / function calls like datetime, calculator, javascript runner, DataStore
111+
dont require the simpleproxy.py helper.
112112

113113

114114

@@ -435,6 +435,8 @@ The following tools/functions are currently provided by default
435435

436436
##### directly in browser
437437

438+
* sys_date_time - provides the current date and time
439+
438440
* simple_calculator - which can solve simple arithmatic expressions
439441

440442
* run_javascript_function_code - which can be used to run ai generated or otherwise javascript code
@@ -628,6 +630,8 @@ sliding window based drop off or even before they kick in, this can help in many
628630

629631
* renamed pdf_to_text to fetch_pdf_as_text so that ai model can understand the semantic better.
630632

633+
* sys_date_time tool call has been added.
634+
631635

632636
#### ToDo
633637

tools/server/public_simplechat/tooljs.mjs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,76 @@
1111
let gToolsWorker = /** @type{Worker} */(/** @type {unknown} */(null));
1212

1313

14+
let sysdatetime_meta = {
15+
"type": "function",
16+
"function": {
17+
"name": "sys_date_time",
18+
"description": "Returns the current system date and time. One can optionally pass template to control what and all parts of date and time are returned",
19+
"parameters": {
20+
"type": "object",
21+
"properties": {
22+
"template": {
23+
"type": "string",
24+
"description": `Optional template can be any combination of Y,M,D,h,m,s.
25+
Y - FullYear 4 digits, M - Month 2 digits, D - Day 2 digits,
26+
h - hour 2 digits, m - minutes 2 digits, s - seconds 2 digits,
27+
any other char will be returned as is
28+
29+
If no template is given, it defaults to YMDThm
30+
`
31+
}
32+
},
33+
"required": ["template"]
34+
}
35+
}
36+
}
37+
38+
39+
/**
40+
* Implementation of the system date and time.
41+
* @param {string} chatid
42+
* @param {string} toolcallid
43+
* @param {string} toolname
44+
* @param {any} obj
45+
*/
46+
function sysdatetime_run(chatid, toolcallid, toolname, obj) {
47+
let dt = new Date()
48+
let tmpl = obj['template'];
49+
if ((tmpl == undefined) || (tmpl == "")) {
50+
tmpl = 'YMDThm';
51+
}
52+
let sDT = ""
53+
for (const c of tmpl) {
54+
switch (c) {
55+
case 'Y':
56+
sDT += dt.getFullYear().toString().padStart(4, '0')
57+
break;
58+
case 'M':
59+
sDT += (dt.getMonth()+1).toString().padStart(2, '0')
60+
break;
61+
case 'D':
62+
sDT += dt.getDate().toString().padStart(2, '0')
63+
break;
64+
case 'h':
65+
sDT += dt.getHours().toString().padStart(2, '0')
66+
break;
67+
case 'm':
68+
sDT += dt.getMinutes().toString().padStart(2, '0')
69+
break;
70+
case 's':
71+
sDT += dt.getSeconds().toString().padStart(2, '0')
72+
break;
73+
default:
74+
sDT += c;
75+
break;
76+
}
77+
}
78+
if (gToolsWorker.onmessage != null) {
79+
gToolsWorker.onmessage(new MessageEvent('message', {data: {cid: chatid, tcid: toolcallid, name: toolname, data: sDT}}))
80+
}
81+
}
82+
83+
1484
let js_meta = {
1585
"type": "function",
1686
"function": {
@@ -79,6 +149,11 @@ function calc_run(chatid, toolcallid, toolname, obj) {
79149
* @type {Object<string, Object<string, any>>}
80150
*/
81151
export let tc_switch = {
152+
"sys_date_time": {
153+
"handler": sysdatetime_run,
154+
"meta": sysdatetime_meta,
155+
"result": ""
156+
},
82157
"run_javascript_function_code": {
83158
"handler": js_run,
84159
"meta": js_meta,

0 commit comments

Comments
 (0)