Skip to content

Commit

Permalink
feat: add functionality to download and resolve messages from manifes…
Browse files Browse the repository at this point in the history
…t document

Added `download` and `downloadFromURL` functions in `manifest-document.ts` to download content from URLs or local files.
Updated `getManifestMessages` function in `manifest-document.ts` to resolve messages by downloading their content if necessary.
Updated `messageObjectToMessage` function in `chat-with-manifest.ts` to correctly convert message objects to message objects.
  • Loading branch information
JonDotsoy committed Aug 16, 2024
1 parent bd869ff commit 222443a
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 7 deletions.
52 changes: 52 additions & 0 deletions src/chat-manifest/manifest-document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,24 @@ import { vpath } from "./vpath";
import { $ } from "bun";
import * as Handlebars from "handlebars";

const downloadFromURL = async (source: string) => {
const res = await fetch(source);
return res.text();
};
const download = async (source: string) => {
const src = new URL(source, new URL(`${process.cwd()}/`, "file://"));

if (src.protocol === "http:" || src.protocol === "https:") {
return await downloadFromURL(src.href);
}

if (src.protocol === "file:") {
return await fs.readFile(src.pathname, "utf-8");
}

throw new Error(`Unsupported protocol: ${src.protocol}`);
};

const logWarn = (message: string) => {
if (logWarn.logged) return;
logWarn.logged = true;
Expand Down Expand Up @@ -57,6 +75,8 @@ export class ManifestDocument {
return await this.#manifest;
}

async getManifestMessages() {}

async downloadFileContent(path: string) {
return await fs.readFile(new URL(path, this.path), "utf-8");
}
Expand All @@ -79,6 +99,38 @@ export class ManifestDocument {
manifest.messages = [...nextManifest.messages, ...manifest.messages];
}

// Resolve Messages
for (const messageIndex in manifest.messages) {
const message = manifest.messages[messageIndex];
const getContent = () => {
if ("user" in message)
return {
update: (newContent: string) => (message.user = newContent),
content: message.user,
};
if ("system" in message)
return {
update: (newContent: string) => (message.system = newContent),
content: message.system,
};
if ("assistant" in message)
return {
update: (newContent: string) => (message.assistant = newContent),
content: message.assistant,
};
return null;
};

const content = getContent();
if (content === null)
throw new Error("Cannot resolve message", { cause: message });

if (typeof content.content === "string") continue;
if ("from" in content.content) {
content.update(await download(content.content.from));
}
}

const handlebars = Handlebars.create();

handlebars.registerHelper("include", function (pathInclude) {
Expand Down
27 changes: 20 additions & 7 deletions src/ollama/chat-with-manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,27 @@ export const ollama = new Ollama();
const messageObjectToMessage = (
messageObject: MessageObjectDto,
): Message | null => {
if ("system" in messageObject)
return { role: "system", content: messageObject.system };
if ("user" in messageObject)
return { role: "user", content: messageObject.user };
if ("assistant" in messageObject)
return { role: "assistant", content: messageObject.assistant };
const getContent = () => {
if ("system" in messageObject)
return { role: "system", content: messageObject.system };
if ("user" in messageObject)
return { role: "user", content: messageObject.user };
if ("assistant" in messageObject)
return { role: "assistant", content: messageObject.assistant };
return null;
};

return null;
const content = getContent();

if (content === null) return null;

if (typeof content.content !== "string")
throw new Error("Content must be a string");

return {
role: content.role,
content: content.content,
};
};

export const getMessagesFromManifest = async function* (
Expand Down

0 comments on commit 222443a

Please sign in to comment.