From 1756140834f277c096cf6405675c7ba354f3a887 Mon Sep 17 00:00:00 2001 From: Brian Joseph Petro Date: Mon, 2 Dec 2024 21:28:58 -0500 Subject: [PATCH] - Renamed `SmartThreadDataAdapter` to `ThreadSourceAdapter` for clarity. - Updated `SmartThreadJsonDataAdapter` to extend from the new `ThreadSourceAdapter`. - Introduced `FileSourceAdapter` for file handling, replacing the deprecated `TextSourceAdapter`. - Modified `MarkdownSourceAdapter` to extend from `FileSourceAdapter`, enhancing file operations. - Added new `JsonTemplateSourceAdapter` for JSON file persistence in templates. - Created a new lookup JSON template for structured data handling. - Commented out unimplemented methods in `SourceAdapter` to clarify intended functionality. --- smart-chats/adapters/_adapter.js | 2 +- smart-chats/adapters/json.js | 8 +++---- smart-sources/adapters/_adapter.js | 4 ++-- smart-sources/adapters/file.js | 10 +++++++++ smart-sources/adapters/markdown.js | 13 +++++------ smart-sources/adapters/text.js | 3 --- smart-templates/adapters/json.js | 32 +++++++++++++++++++++++++++ smart-templates/templates/lookup.json | 5 +++++ 8 files changed, 60 insertions(+), 17 deletions(-) create mode 100644 smart-sources/adapters/file.js delete mode 100644 smart-sources/adapters/text.js create mode 100644 smart-templates/adapters/json.js create mode 100644 smart-templates/templates/lookup.json diff --git a/smart-chats/adapters/_adapter.js b/smart-chats/adapters/_adapter.js index cff2668a..9d5c8af3 100644 --- a/smart-chats/adapters/_adapter.js +++ b/smart-chats/adapters/_adapter.js @@ -3,7 +3,7 @@ * @description Base adapter class for chat data format conversions. * Provides interface for converting between internal format and various chat formats. */ -export class SmartThreadDataAdapter { +export class ThreadSourceAdapter { /** * @constructor * @param {SmartThread} item - The SmartThread instance this adapter is attached to diff --git a/smart-chats/adapters/json.js b/smart-chats/adapters/json.js index c7f57513..c2ce26f9 100644 --- a/smart-chats/adapters/json.js +++ b/smart-chats/adapters/json.js @@ -1,11 +1,11 @@ -import { SmartThreadDataAdapter } from "./_adapter.js"; +import { ThreadSourceAdapter } from "./_adapter.js"; /** - * @class SmartThreadJsonDataAdapter - * @extends SmartThreadDataAdapter + * @class EnvJsonThreadSourceAdapter + * @extends ThreadSourceAdapter * @description for persisting OpenAI chat completion responses to JSON files */ -export class SmartThreadJsonDataAdapter extends SmartThreadDataAdapter { +export class EnvJsonThreadSourceAdapter extends ThreadSourceAdapter { static extension = 'json'; extension = 'json'; to_source_data(){ diff --git a/smart-sources/adapters/_adapter.js b/smart-sources/adapters/_adapter.js index 5423d802..1fdc2128 100644 --- a/smart-sources/adapters/_adapter.js +++ b/smart-sources/adapters/_adapter.js @@ -18,9 +18,9 @@ export class SourceAdapter { async import() { this.throw_not_implemented('import'); } async append(content) { this.throw_not_implemented('append'); } async update(full_content, opts = {}) { this.throw_not_implemented('update'); } - async _update(content) { this.throw_not_implemented('_update'); } + // async _update(content) { this.throw_not_implemented('_update'); } async read(opts = {}) { this.throw_not_implemented('read'); } - async _read() { this.throw_not_implemented('_read'); } + // async _read() { this.throw_not_implemented('_read'); } async remove() { this.throw_not_implemented('remove'); } async move_to(entity_ref) { this.throw_not_implemented('move_to'); } async merge(content, opts = {}) { this.throw_not_implemented('merge'); } diff --git a/smart-sources/adapters/file.js b/smart-sources/adapters/file.js new file mode 100644 index 00000000..400c9046 --- /dev/null +++ b/smart-sources/adapters/file.js @@ -0,0 +1,10 @@ +import { SourceAdapter } from "./_adapter.js"; +export class FileSourceAdapter extends SourceAdapter { + async update(content) { + await this.fs.write(this.file_path, content); + } + async read() { + return await this.fs.read(this.file_path); + } + get file_path() { return this.item.file_path; } +} \ No newline at end of file diff --git a/smart-sources/adapters/markdown.js b/smart-sources/adapters/markdown.js index 0fcf8823..d1bf31b5 100644 --- a/smart-sources/adapters/markdown.js +++ b/smart-sources/adapters/markdown.js @@ -1,14 +1,13 @@ import { increase_heading_depth } from "../utils/increase_heading_depth.js"; -import { TextSourceAdapter } from "./text.js"; +import { FileSourceAdapter } from "./file.js"; import { markdown_to_blocks } from "../blocks/markdown_to_blocks.js"; import { get_markdown_links } from "../utils/get_markdown_links.js"; import { get_line_range } from "../utils/get_line_range.js"; import { block_read, block_update, block_destroy } from "../blocks/markdown_crud.js"; -export class MarkdownSourceAdapter extends TextSourceAdapter { +export class MarkdownSourceAdapter extends FileSourceAdapter { get fs() { return this.source_collection.fs; } get data() { return this.item.data; } - get file_path() { return this.item.file_path; } get source() { return this.item.source ? this.item.source : this.item; } async import() { @@ -75,11 +74,11 @@ export class MarkdownSourceAdapter extends TextSourceAdapter { await this.merge(full_content, { mode: "append_blocks" }); } } - async _update(content) { - await this.fs.write(this.file_path, content); + await super.update(content); } + async read(opts = {}) { let content = await this._read(); this.source.data.last_read_hash = await this.create_hash(content); @@ -97,11 +96,11 @@ export class MarkdownSourceAdapter extends TextSourceAdapter { return content; } - async _read() { - return await this.fs.read(this.file_path); + return await super.read(); } + async remove() { await this.fs.remove(this.file_path); this.item.delete(); diff --git a/smart-sources/adapters/text.js b/smart-sources/adapters/text.js deleted file mode 100644 index ccb22796..00000000 --- a/smart-sources/adapters/text.js +++ /dev/null @@ -1,3 +0,0 @@ -import { SourceAdapter } from "./_adapter.js"; -export class TextSourceAdapter extends SourceAdapter { -} \ No newline at end of file diff --git a/smart-templates/adapters/json.js b/smart-templates/adapters/json.js new file mode 100644 index 00000000..b62e6195 --- /dev/null +++ b/smart-templates/adapters/json.js @@ -0,0 +1,32 @@ +import { FileSourceAdapter } from "smart-sources/adapters/file.js"; + +/** + * @class SmartTemplateJsonSourceAdapter + * @extends SmartTemplateDataAdapter + * @description for persisting OpenAI chat completion responses to JSON files + */ +export class JsonTemplateSourceAdapter extends FileSourceAdapter { + static extension = 'json'; + extension = 'json'; + async to_source_file(){ + const file_content = { + ...this.item.data, + }; + await this.update(JSON.stringify(file_content, null, 2)); + } + async from_source_file() { + const source_file = await this.read(); + const parsed_data = JSON.parse(source_file); + this.item.data = { + ...this.item.data, + ...parsed_data, + blocks: undefined + }; + // parsed_data.blocks.forEach(block => { + // this.item.env.template_outputs.items[block.key] = new this.item.env.template_outputs.item_type( + // this.item.env, + // block + // ); + // }); + } +} \ No newline at end of file diff --git a/smart-templates/templates/lookup.json b/smart-templates/templates/lookup.json new file mode 100644 index 00000000..2df434b8 --- /dev/null +++ b/smart-templates/templates/lookup.json @@ -0,0 +1,5 @@ +{ + "opts": {}, + "tool": {}, + "output": {} +} \ No newline at end of file