Skip to content

Commit cccc520

Browse files
extending the api with more functions
1 parent 5f22cab commit cccc520

File tree

2 files changed

+73
-2
lines changed

2 files changed

+73
-2
lines changed

code/logic/fossil/ai/jellyfish.h

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,11 @@ typedef struct {
4141
uint8_t hash[FOSSIL_JELLYFISH_HASH_SIZE];
4242
uint64_t timestamp;
4343
int valid;
44+
float confidence; // New: how trusted this block is (0.0 - 1.0)
45+
uint32_t usage_count; // New: how often it's used
4446
} fossil_jellyfish_block;
4547

48+
4649
/**
4750
* Represents a chain of jellyfish blocks.
4851
* This structure holds the memory for the jellyfish AI and tracks the number of blocks.
@@ -141,6 +144,26 @@ int fossil_jellyfish_load(fossil_jellyfish_chain *chain, const char *filepath);
141144
*/
142145
const char* fossil_jellyfish_reason_fuzzy(fossil_jellyfish_chain *chain, const char *input);
143146

147+
/**
148+
* Get the reason chain for a given input.
149+
* This function provides a detailed explanation of how the AI arrived at its reasoning.
150+
*
151+
* @param chain Pointer to the jellyfish chain.
152+
* @param input The input string to reason about.
153+
* @param depth The depth of reasoning to explore (0 for no depth).
154+
* @return A string explaining the reasoning process, or "Unknown" if not found.
155+
*/
156+
const char* fossil_jellyfish_reason_chain(fossil_jellyfish_chain *chain, const char *input, int depth);
157+
158+
/**
159+
* Decay the confidence of the jellyfish chain.
160+
* This reduces the confidence of all blocks in the chain by a specified decay rate.
161+
*
162+
* @param chain Pointer to the jellyfish chain.
163+
* @param decay_rate The rate at which to decay confidence (0.0 - 1.0).
164+
*/
165+
void fossil_jellyfish_decay_confidence(fossil_jellyfish_chain *chain, float decay_rate);
166+
144167

145168
#ifdef __cplusplus
146169
}
@@ -276,6 +299,29 @@ namespace ai {
276299
return fossil_jellyfish_load(&chain, filepath.c_str());
277300
}
278301

302+
/**
303+
* Get the reason chain for a given input.
304+
* This function provides a detailed explanation of how the AI arrived at its reasoning.
305+
*
306+
* @param input The input string to reason about.
307+
* @param depth The depth of reasoning to explore (0 for no depth).
308+
* @return A string explaining the reasoning process, or "Unknown" if not found.
309+
*/
310+
std::string reason_chain(const std::string &input, int depth = 0) {
311+
const char *result = fossil_jellyfish_reason_chain(&chain, input.c_str(), depth);
312+
return std::string(result);
313+
}
314+
315+
/**
316+
* Decay the confidence of the jellyfish chain.
317+
* This reduces the confidence of all blocks in the chain by a specified decay rate.
318+
*
319+
* @param decay_rate The rate at which to decay confidence (0.0 - 1.0).
320+
*/
321+
void decay_confidence(float decay_rate) {
322+
fossil_jellyfish_decay_confidence(&chain, decay_rate);
323+
}
324+
279325
private:
280326
fossil_jellyfish_chain chain; // The jellyfish chain instance
281327
};

code/logic/jellyfish.c

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,21 +50,26 @@ void fossil_jellyfish_init(fossil_jellyfish_chain *chain) {
5050

5151
void fossil_jellyfish_learn(fossil_jellyfish_chain *chain, const char *input, const char *output) {
5252
if (chain->count >= FOSSIL_JELLYFISH_MAX_MEM) {
53-
fossil_jellyfish_cleanup(chain); // Reclaim space
53+
fossil_jellyfish_cleanup(chain);
5454
}
5555

5656
fossil_jellyfish_block *block = &chain->memory[chain->count++];
5757
strncpy(block->input, input, FOSSIL_JELLYFISH_INPUT_SIZE - 1);
5858
strncpy(block->output, output, FOSSIL_JELLYFISH_OUTPUT_SIZE - 1);
5959
block->timestamp = (uint64_t)time(NULL);
6060
block->valid = 1;
61+
block->confidence = 1.0f;
62+
block->usage_count = 0;
6163

6264
fossil_jellyfish_hash(input, output, block->hash);
6365
}
6466

6567
const char* fossil_jellyfish_reason(fossil_jellyfish_chain *chain, const char *input) {
6668
for (size_t i = 0; i < chain->count; ++i) {
6769
if (chain->memory[i].valid && strncmp(chain->memory[i].input, input, FOSSIL_JELLYFISH_INPUT_SIZE) == 0) {
70+
chain->memory[i].usage_count++;
71+
if (chain->memory[i].confidence < 1.0f)
72+
chain->memory[i].confidence += 0.05f;
6873
return chain->memory[i].output;
6974
}
7075
}
@@ -75,7 +80,7 @@ const char* fossil_jellyfish_reason(fossil_jellyfish_chain *chain, const char *i
7580
void fossil_jellyfish_cleanup(fossil_jellyfish_chain *chain) {
7681
size_t new_count = 0;
7782
for (size_t i = 0; i < chain->count; ++i) {
78-
if (chain->memory[i].valid && (time(NULL) - chain->memory[i].timestamp < 3600)) {
83+
if (chain->memory[i].valid && chain->memory[i].confidence >= 0.05f) {
7984
chain->memory[new_count++] = chain->memory[i];
8085
}
8186
}
@@ -143,3 +148,23 @@ const char* fossil_jellyfish_reason_fuzzy(fossil_jellyfish_chain *chain, const c
143148

144149
return best_output;
145150
}
151+
152+
void fossil_jellyfish_decay_confidence(fossil_jellyfish_chain *chain, float decay_rate) {
153+
for (size_t i = 0; i < chain->count; ++i) {
154+
if (!chain->memory[i].valid) continue;
155+
156+
chain->memory[i].confidence -= decay_rate;
157+
if (chain->memory[i].confidence < 0.05f) {
158+
chain->memory[i].valid = 0; // mark for cleanup
159+
}
160+
}
161+
}
162+
163+
const char* fossil_jellyfish_reason_chain(fossil_jellyfish_chain *chain, const char *input, int depth) {
164+
if (depth <= 0) return input;
165+
166+
const char *first = fossil_jellyfish_reason_fuzzy(chain, input);
167+
if (strcmp(first, "Unknown") == 0) return first;
168+
169+
return fossil_jellyfish_reason_chain(chain, first, depth - 1);
170+
}

0 commit comments

Comments
 (0)