|
11 | 11 | let gToolsWorker = /** @type{Worker} */(/** @type {unknown} */(null)); |
12 | 12 |
|
13 | 13 |
|
| 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 | + |
14 | 84 | let js_meta = { |
15 | 85 | "type": "function", |
16 | 86 | "function": { |
@@ -79,6 +149,11 @@ function calc_run(chatid, toolcallid, toolname, obj) { |
79 | 149 | * @type {Object<string, Object<string, any>>} |
80 | 150 | */ |
81 | 151 | export let tc_switch = { |
| 152 | + "sys_date_time": { |
| 153 | + "handler": sysdatetime_run, |
| 154 | + "meta": sysdatetime_meta, |
| 155 | + "result": "" |
| 156 | + }, |
82 | 157 | "run_javascript_function_code": { |
83 | 158 | "handler": js_run, |
84 | 159 | "meta": js_meta, |
|
0 commit comments