Skip to content

Commit 09d9154

Browse files
Merge pull request #32 from dreamer-coding/fish_ai_file
2 parents 35d056c + 1490769 commit 09d9154

File tree

3 files changed

+471
-22
lines changed

3 files changed

+471
-22
lines changed

code/logic/fossil/ai/jellyfish.h

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
#define FOSSIL_JELLYFISH_HASH_SIZE 32
2222
#define FOSSIL_JELLYFISH_INPUT_SIZE 64
2323
#define FOSSIL_JELLYFISH_OUTPUT_SIZE 64
24+
#define FOSSIL_JELLYFISH_MAX_MODELS 32
25+
#define FOSSIL_JELLYFISH_MAX_TOKENS 16
26+
#define FOSSIL_JELLYFISH_TOKEN_SIZE 16
2427

2528
#ifdef __cplusplus
2629
extern "C"
@@ -55,6 +58,12 @@ typedef struct {
5558
size_t count;
5659
} fossil_jellyfish_chain;
5760

61+
typedef struct {
62+
fossil_jellyfish_chain models[FOSSIL_JELLYFISH_MAX_MODELS];
63+
char model_names[FOSSIL_JELLYFISH_MAX_MODELS][64];
64+
size_t model_count;
65+
} fossil_jellyfish_mind;
66+
5867
// *****************************************************************************
5968
// Function prototypes
6069
// *****************************************************************************
@@ -164,6 +173,68 @@ const char* fossil_jellyfish_reason_chain(fossil_jellyfish_chain *chain, const c
164173
*/
165174
void fossil_jellyfish_decay_confidence(fossil_jellyfish_chain *chain, float decay_rate);
166175

176+
/**
177+
* Loads a named memory model from a file into the specified mind.
178+
*
179+
* @param mind Pointer to the target mind structure.
180+
* @param filepath Path to the .fish or binary model file.
181+
* @param name Name to associate with the loaded model.
182+
* @return 1 if successful, 0 on failure.
183+
*/
184+
int fossil_jellyfish_mind_load_model(fossil_jellyfish_mind *mind, const char *filepath, const char *name);
185+
186+
/**
187+
* Performs reasoning using the mind's active memory chain.
188+
*
189+
* @param mind Pointer to the mind structure.
190+
* @param input Input question or statement to reason about.
191+
* @return Output string representing the closest known answer or reasoning result.
192+
*/
193+
const char* fossil_jellyfish_mind_reason(fossil_jellyfish_mind *mind, const char *input);
194+
195+
/**
196+
* Tokenizes a given input string into lowercase word tokens.
197+
*
198+
* @param input Null-terminated string to tokenize.
199+
* @param tokens 2D array to store output tokens (each max FOSSIL_JELLYFISH_TOKEN_SIZE).
200+
* @param max_tokens Maximum number of tokens to extract.
201+
* @return The number of tokens actually written to the tokens array.
202+
*/
203+
size_t fossil_jellyfish_tokenize(const char *input, char tokens[][FOSSIL_JELLYFISH_TOKEN_SIZE], size_t max_tokens);
204+
205+
/**
206+
* Returns a pointer to the memory block in the chain with the highest confidence score.
207+
*
208+
* @param chain Pointer to the memory chain.
209+
* @return Pointer to the best fossil_jellyfish_block, or NULL if no valid memory exists.
210+
*/
211+
const fossil_jellyfish_block *fossil_jellyfish_best_memory(const fossil_jellyfish_chain *chain);
212+
213+
/**
214+
* Calculates a normalized score representing how "full" or utilized the knowledge base is.
215+
*
216+
* @param chain Pointer to the memory chain.
217+
* @return Float between 0.0 and 1.0 indicating knowledge coverage.
218+
*/
219+
float fossil_jellyfish_knowledge_coverage(const fossil_jellyfish_chain *chain);
220+
221+
/**
222+
* Checks if adding a given input-output pair would contradict existing memory.
223+
*
224+
* @param chain Pointer to the memory chain.
225+
* @param input Input to check.
226+
* @param output Output to check.
227+
* @return 1 if a conflict is found, 0 otherwise.
228+
*/
229+
int fossil_jellyfish_detect_conflict(const fossil_jellyfish_chain *chain, const char *input, const char *output);
230+
231+
/**
232+
* Prints a self-reflection report of the current memory chain to stdout.
233+
* Includes memory size, confidence distribution, usage patterns, and top entries.
234+
*
235+
* @param chain Pointer to the memory chain to reflect on.
236+
*/
237+
void fossil_jellyfish_reflect(const fossil_jellyfish_chain *chain);
167238

168239
#ifdef __cplusplus
169240
}
@@ -321,8 +392,95 @@ namespace ai {
321392
void decay_confidence(float decay_rate) {
322393
fossil_jellyfish_decay_confidence(&chain, decay_rate);
323394
}
395+
396+
/**
397+
* @brief Load a named model into the mind from a .fish or binary file.
398+
*
399+
* @param filepath Path to the model file.
400+
* @param name Logical name for the model within the mind.
401+
* @return True if loaded successfully, false on failure.
402+
*/
403+
bool load_model(const std::string &filepath, const std::string &name) {
404+
return fossil_jellyfish_mind_load_model(&mind, filepath.c_str(), name.c_str()) != 0;
405+
}
406+
407+
/**
408+
* @brief Perform reasoning across all loaded models.
409+
*
410+
* @param input Input question or query.
411+
* @return The best known answer string, or "Unknown".
412+
*/
413+
std::string mind_reason(const std::string &input) {
414+
return std::string(fossil_jellyfish_mind_reason(&mind, input.c_str()));
415+
}
416+
417+
/**
418+
* @brief Tokenize the input string into lowercase word tokens.
419+
*
420+
* @param input The input string to tokenize.
421+
* @return A vector of token strings (max size determined by macro).
422+
*/
423+
std::vector<std::string> tokenize(const std::string &input) {
424+
char tokens[FOSSIL_JELLYFISH_MAX_TOKENS][FOSSIL_JELLYFISH_TOKEN_SIZE] = {};
425+
size_t count = fossil_jellyfish_tokenize(input.c_str(), tokens, FOSSIL_JELLYFISH_MAX_TOKENS);
426+
427+
std::vector<std::string> result;
428+
for (size_t i = 0; i < count; ++i) {
429+
result.emplace_back(tokens[i]);
430+
}
431+
return result;
432+
}
433+
434+
/**
435+
* @brief Get the memory block with the highest confidence.
436+
*
437+
* @param chain Reference to a memory chain.
438+
* @return Pointer to the best memory block, or nullptr if none.
439+
*/
440+
const fossil_jellyfish_block* best_memory(const fossil_jellyfish_chain &chain) const {
441+
return fossil_jellyfish_best_memory(&chain);
442+
}
443+
444+
/**
445+
* @brief Get a knowledge coverage score from the given chain.
446+
*
447+
* @param chain Reference to the chain.
448+
* @return A float from 0.0 to 1.0 indicating how full the chain is.
449+
*/
450+
float knowledge_coverage(const fossil_jellyfish_chain &chain) const {
451+
return fossil_jellyfish_knowledge_coverage(&chain);
452+
}
453+
454+
/**
455+
* @brief Check if the input-output pair conflicts with existing memory.
456+
*
457+
* @param chain The chain to check against.
458+
* @param input Input string.
459+
* @param output Output string.
460+
* @return True if a contradiction exists, false otherwise.
461+
*/
462+
bool detect_conflict(const fossil_jellyfish_chain &chain, const std::string &input, const std::string &output) const {
463+
return fossil_jellyfish_detect_conflict(&chain, input.c_str(), output.c_str()) != 0;
464+
}
465+
466+
/**
467+
* @brief Print a self-reflection report about a chain.
468+
*
469+
* @param chain Reference to the memory chain to reflect on.
470+
*/
471+
void reflect(const fossil_jellyfish_chain &chain) const {
472+
fossil_jellyfish_reflect(&chain);
473+
}
474+
475+
/**
476+
* @brief Access the underlying C mind object.
477+
*
478+
* @return Reference to the mind struct.
479+
*/
480+
fossil_jellyfish_mind& raw() { return mind; }
324481

325482
private:
483+
fossil_jellyfish_mind mind; ///< Internal mind state holding multiple chains/models.
326484
fossil_jellyfish_chain chain; // The jellyfish chain instance
327485
};
328486

0 commit comments

Comments
 (0)