Skip to content

Commit

Permalink
Pretty typebox errors, add spec version to project
Browse files Browse the repository at this point in the history
  • Loading branch information
stwiname committed Oct 14, 2024
1 parent 45f796a commit 9191ec1
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 11 deletions.
6 changes: 5 additions & 1 deletion src/bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ export async function publishProject(
sandboxFactory = getDefaultSandbox,
): Promise<string> {
projectPath = await Deno.realPath(projectPath);
const projectJson = await getProjectJson(projectPath, sandboxFactory);
const projectJson = await getProjectJson(
projectPath,
"local",
sandboxFactory,
);
let code = await generateBundle(projectPath);
const vectorDbPath = projectJson.vectorStorage?.path;
if (vectorDbPath) {
Expand Down
19 changes: 12 additions & 7 deletions src/project/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ export const VectorConfig = Type.Object({
});

export const Project = Type.Object({
// Note: If a new spec version is introduced Type.TemplateLiteral could be used here
specVersion: Type.Literal(
"0.0.1",
{ description: "The specification version of the project structure." },
),
model: Type.String({
description: "The llm model to use",
}),
Expand Down Expand Up @@ -71,8 +76,9 @@ export type IProjectEntrypoint<T extends TObject = TObject> = Static<
IProjectEntry<T>
>;

export function validateProject(project: unknown): void {
return Value.Assert(Project, project);
export function validateProject(project: unknown): project is IProject {
Value.Assert(Project, project);
return true;
}

function validateProjectEntry(entry: unknown): entry is IProjectEntry {
Expand Down Expand Up @@ -109,10 +115,9 @@ export async function getProjectFromEntrypoint(
// Check that the constructed project is valid
const project = await entrypoint.projectFactory(config);

validateProject(project);

return project;
} else {
throw new Error("Unable to validate project");
if (validateProject(project)) {
return project;
}
}
throw new Error("Unable to validate project");
}
12 changes: 10 additions & 2 deletions src/sandbox/webWorker/webWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
type IProjectEntrypoint,
} from "../../project/project.ts";
import type { IContext } from "../../context/context.ts";
import { PrettyTypeboxError } from "../../util.ts";

const conn = rpc.createMessageConnection(
new BrowserMessageReader(self),
Expand Down Expand Up @@ -52,9 +53,16 @@ conn.onRequest(Init, async (config) => {
throw new Error("Please call `load` first");
}

project ??= await getProjectFromEntrypoint(entrypoint, config);
try {
project ??= await getProjectFromEntrypoint(entrypoint, config);

return toJsonProject();
return toJsonProject();
} catch (e: unknown) {
if (e instanceof Error) {
throw PrettyTypeboxError(e, "Project validation failed");
}
throw e;
}
});

conn.onRequest(GetConfig, () => {
Expand Down
21 changes: 20 additions & 1 deletion src/util.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Static, TSchema } from "@sinclair/typebox";
import { Value } from "@sinclair/typebox/value";
import { AssertError, Value } from "@sinclair/typebox/value";
import ora, { type Ora } from "ora";
import { brightBlue } from "@std/fmt/colors";

Expand Down Expand Up @@ -35,3 +35,22 @@ export function getPrompt(): string | null {

// Possible sources where projects can be loaded from
export type ProjectSource = "local" | "ipfs";

export function PrettyTypeboxError(
error: Error,
prefix = "Type Assertion Failed",
): Error {
if (
error instanceof AssertError || error.constructor.name === "AssertError"
) {
const errs = [...(error as AssertError).Errors()];

let msg = `${prefix}:\n`;
for (const e of errs) {
msg += `\t${e.path}: ${e.message}\n`;
}
return new Error(msg, { cause: error });
}

return error;
}
1 change: 1 addition & 0 deletions subquery-delegator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export const entrypoint: IProjectEntrypoint<typeof ConfigType> = {

return {
tools,
specVersion: "0.0.1",
model: "llama3.1",
vectorStorage: {
type: "lancedb",
Expand Down

0 comments on commit 9191ec1

Please sign in to comment.