Skip to content

Commit

Permalink
Merge pull request #9 from susom/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
Jordan-M-Schultz authored Sep 23, 2024
2 parents 1c0bcb6 + 5c4b6f5 commit d22f50d
Show file tree
Hide file tree
Showing 22 changed files with 663 additions and 519 deletions.
417 changes: 192 additions & 225 deletions MICA.php

Large diffs are not rendered by default.

25 changes: 22 additions & 3 deletions assets/jsmo.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@
callback(parsed)
}
},
verifyPhone: async (payload, callback, errorCallback) => {
const res = await module.ajax('verifyPhone', payload);

verifyEmail: async (payload, callback, errorCallback) => {
const res = await module.ajax('verifyEmail', payload);
let parsed = JSON.parse(res)

if('error' in parsed) {
Expand All @@ -54,6 +55,24 @@
} else {
callback(parsed)
}
}
},

fetchSavedQueries: async (payload, callback, errorCallback) => {
const res = await module.ajax('fetchSavedQueries', payload);
let parsed = JSON.parse(res)

if('error' in parsed) {
console.error(parsed['error'])
errorCallback(parsed['error'])
} else {
callback(parsed)
}
},

completeSession: async (payload, callback, errorCallback) => {
const res = await module.ajax('completeSession', payload);
let parsed = JSON.parse(res)
callback(parsed);
},
});
}
4 changes: 2 additions & 2 deletions classes/ASEMLO.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/
class ASEMLO
{
const OBJECT_NAME = null; // Object name (defaults to class name) - can be overwritten by parent object
const OBJECT_NAME = "MICAQuery"; // Object name (defaults to class name) - can be overwritten by parent object
const NAME_COLUMN = 'record'; // typically 'message' or 'record' - recommend using record unless you need
// the built-in record logging functionality
const LOG_OBJECT_NAME = null; // Will default to class name+'_LOG' unless set in parent class
Expand Down Expand Up @@ -453,7 +453,7 @@ public static function queryIds($module, $filter_clause = "", $parameters = [])

$sql = "select log_id where " . static::NAME_COLUMN . "= ?" . (empty($filter_clause) ? "" : " and " . $filter_clause);
$params = array_merge([self::getObjectName()], $parameters);
$module->emDebug($sql, $params);
// $module->emDebug($sql, $params);

$result = $framework->queryLogs($sql,$params);
$ids = [];
Expand Down
99 changes: 0 additions & 99 deletions classes/Action.php

This file was deleted.

124 changes: 124 additions & 0 deletions classes/MICAQuery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php

namespace Stanford\MICA;
require_once "ASEMLO.php";


/**
* The Conversation State extends the Simple EM Log Object to provide a data store for all conversations
*
*/
class MICAQuery extends ASEMLO
{
/** @var MICA $this ->module */

/**
* @param $module
* @param $type
* @param $log_id
* @param $limit_params //used if you want to obtain a specific log_id and then only pull certain parameters
* @throws \Exception
*/
public function __construct($module, $log_id = null, $limit_params = [])
{
parent::__construct($module, $log_id, $limit_params);
}

// public function payloadCheck() {
// if (! isset($this->payload)) {
// $this->payload = $this->getPayload();
// }
// }

/** GETTERS */
public function getPayload()
{
// The payload is in the settings table and has the json object
// TODO:? Should we json_decode here?
return $this->getValue('payload');

}

public function getMICAQuery()
{
$message = $this->getValue('message');
$decoded = json_decode($message, true);
$decoded['timestamp'] = $this->getValue('timestamp');
$decoded['id'] = $this->getId();
return $decoded;
}


/** SETTERS */


/** STATIC METHODS */

/**
* Load the active conversation after action_id
* @param MICA $module
* @param int $project_id
* @param int $action_id
* @return array Action
* @throws \Exception
*/
public static function getLogsFor($module, $project_id, $mica_id)
{

$filter_clause = "project_id = ? and mica_id = ? order by log_id asc";
$objs = self::queryObjects(
$module, $filter_clause, [$project_id, $mica_id]
);

$chatSession = [];

foreach ($objs as $action) {
$messageJson = $action->getValue('message');
$timestamp = $action->getValue('timestamp');

// Decode the JSON message
$messageData = json_decode($messageJson, true);

// Handle potential double encoding
if (is_string($messageData)) {
$messageData = json_decode($messageData, true);
}

// Check for JSON decoding errors
if (json_last_error() !== JSON_ERROR_NONE) {
$module->emDebug('JSON decoding error: ' . json_last_error_msg(), $messageJson);
continue; // Skip this iteration if decoding failed
}

// Extract data
$assistantContent = $messageData['response']['content'] ?? null;
$userContent = $messageData['query']['content'] ?? null;

if (empty($assistantContent)) {
// $module->emDebug("only return for completed Q + A");
continue; // Skip this iteration
}

$id = $messageData['id'] ?? null;
$model = $messageData['model'] ?? null;
$usage = $messageData['usage'] ?? [];
$inputTokens = $usage['prompt_tokens'] ?? null;
$outputTokens = $usage['completion_tokens'] ?? null;

// Build the chat session entry
$chatSession[] = [
'assistant_content' => $assistantContent,
'user_content' => $userContent,
'id' => $id,
'model' => $model,
'input_tokens' => $inputTokens,
'output_tokens' => $outputTokens,
'input_cost' => null, // Or calculate if available
'output_cost' => null, // Or calculate if available
'timestamp' => strtotime($timestamp) * 1000, // Convert timestamp to milliseconds
];
}

return $chatSession;
}
}
4 changes: 3 additions & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@
"auth-ajax-actions": [
"callAI",
"login",
"verifyPhone"
"verifyEmail",
"completeSession",
"fetchSavedQueries"
],

"no-auth-ajax-actions": [
Expand Down
Loading

0 comments on commit d22f50d

Please sign in to comment.