Skip to content

Commit

Permalink
- Renamed SmartThreadDataAdapter to ThreadSourceAdapter for clarity.
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
Brian Joseph Petro committed Dec 3, 2024
1 parent f46230b commit 1756140
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 17 deletions.
2 changes: 1 addition & 1 deletion smart-chats/adapters/_adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions smart-chats/adapters/json.js
Original file line number Diff line number Diff line change
@@ -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(){
Expand Down
4 changes: 2 additions & 2 deletions smart-sources/adapters/_adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'); }
Expand Down
10 changes: 10 additions & 0 deletions smart-sources/adapters/file.js
Original file line number Diff line number Diff line change
@@ -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; }
}
13 changes: 6 additions & 7 deletions smart-sources/adapters/markdown.js
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down Expand Up @@ -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);
Expand All @@ -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();
Expand Down
3 changes: 0 additions & 3 deletions smart-sources/adapters/text.js

This file was deleted.

32 changes: 32 additions & 0 deletions smart-templates/adapters/json.js
Original file line number Diff line number Diff line change
@@ -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
// );
// });
}
}
5 changes: 5 additions & 0 deletions smart-templates/templates/lookup.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"opts": {},
"tool": {},
"output": {}
}

0 comments on commit 1756140

Please sign in to comment.