Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add property from on messages to include prompts from remote sources #14

Merged
merged 3 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,23 @@
"type": "object",
"properties": {
"system": {
"type": "string"
"anyOf": [
{
"type": "string"
},
{
"type": "object",
"properties": {
"from": {
"type": "string"
}
},
"required": [
"from"
],
"additionalProperties": false
}
]
}
},
"required": [
Expand All @@ -43,7 +59,7 @@
"type": "object",
"properties": {
"user": {
"type": "string"
"$ref": "#/properties/messages/items/anyOf/0/properties/system"
}
},
"required": [
Expand All @@ -55,7 +71,7 @@
"type": "object",
"properties": {
"assistant": {
"type": "string"
"$ref": "#/properties/messages/items/anyOf/0/properties/system"
}
},
"required": [
Expand Down
19 changes: 14 additions & 5 deletions src/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ import { pkg } from "../pkg";
import * as fs from "fs/promises";
import * as os from "os";

const VISUAL_EDITOR = process.env.Q_EDITOR ?? process.env.GIT_EDITOR ?? process.env.EDITOR ?? 'code';
const VISUAL_EDITOR =
process.env.Q_EDITOR ??
process.env.GIT_EDITOR ??
process.env.EDITOR ??
"code";

function createProgressSpin() {
const symbols = [". ", ".. ", "..."];
Expand Down Expand Up @@ -128,17 +132,22 @@ const main = async (args: string[]) => {
let tmpFileFullPath: null | URL = null;

if (options.new) {
const tmpId = crypto.randomUUID()
tmpFileFullPath = new URL(`${tmpId}.yaml`, new URL(`${os.tmpdir()}/`, "file:"));
const tmpId = crypto.randomUUID();
tmpFileFullPath = new URL(
`${tmpId}.yaml`,
new URL(`${os.tmpdir()}/`, "file:"),
);
await fs.copyFile(fileFullPath, tmpFileFullPath);
try {
await Bun.$`${VISUAL_EDITOR} ${tmpFileFullPath.pathname}`;
} catch (ex) {
console.info(`Open the temporal file ${tmpFileFullPath.pathname}`);
}
};
}

const manifest = await ManifestDocument.fromPath(tmpFileFullPath ?? fileFullPath);
const manifest = await ManifestDocument.fromPath(
tmpFileFullPath ?? fileFullPath,
);

manifest.setSchemaIfNotExists(schemaDocument);

Expand Down
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
13 changes: 10 additions & 3 deletions src/chat-manifest/schemas/manifest.schema.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import { object, union, string, array, optional } from "zod";

export const MessageContent = union([
string(),
object({
from: string(),
}),
]);

export const MessageObject = union([
object({ system: string() }),
object({ user: string() }),
object({ assistant: string() }),
object({ system: MessageContent }),
object({ user: MessageContent }),
object({ assistant: MessageContent }),
]);

export const ManifestSchema = object({
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