Skip to content

Commit

Permalink
Fix lint issues, split CI tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
stwiname committed Oct 6, 2024
1 parent ba08ed0 commit 53578c4
Show file tree
Hide file tree
Showing 22 changed files with 62 additions and 41 deletions.
16 changes: 13 additions & 3 deletions .github/workflows/pr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,18 @@ jobs:
with:
deno-version: v1.x

- name: Check typescript
run: deno task check-ts

- name: Lint
run: deno lint

typescript:
name: typescript
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- uses: denoland/setup-deno@v1
with:
deno-version: v1.x

- name: Check typescript
run: deno task check-ts
2 changes: 1 addition & 1 deletion src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export async function runApp(config: {
),
);

const runnerHost = new RunnerHost(async () => {
const runnerHost = new RunnerHost(() => {
const chatStorage = new MemoryChatStorage();

chatStorage.append([{ role: "system", content: sandbox.systemPrompt }]);
Expand Down
2 changes: 1 addition & 1 deletion src/bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ async function fsExists(path: string): Promise<boolean> {
try {
await Deno.lstat(path);
return true;
} catch (e) {
} catch (_e) {
return false;
}
}
2 changes: 2 additions & 0 deletions src/chatStorage/memoryChatStoarge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import { IChatStorage } from "./chatStorage.ts";
export class MemoryChatStorage implements IChatStorage {
private messages: Message[] = [];

// deno-lint-ignore require-await
async getHistory(): Promise<Message[]> {
return this.messages;
}

// deno-lint-ignore require-await
async append(messages: Message[]): Promise<void> {
this.messages.push(...messages);
}
Expand Down
2 changes: 1 addition & 1 deletion src/context/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export class Context implements IContext {
this.#vectorStorage = vectorStorage;
}

async vectorSearch(tableName: string, vector: number[]): Promise<any[]> {
async vectorSearch(tableName: string, vector: number[]): Promise<unknown[]> {
if (!this.#vectorStorage) {
throw new Error(
"Project did not provide vector storage. Unable to perform search",
Expand Down
6 changes: 2 additions & 4 deletions src/embeddings/generator/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,12 @@ export async function generate(

for (const source of embeddingSources) {
try {
const { checksum, meta, sections } = await source.load();
const { sections } = await source.load();

for (const { slug, heading, content } of sections) {
for (const { content } of sections) {
// OpenAI recommends replacing newlines with spaces for best results (specific to embeddings)
const input = content.replace(/\n/g, " ");

// console.log('CONTENT', content);

lanceWriter.write(input);
}
} catch (e) {
Expand Down
2 changes: 1 addition & 1 deletion src/embeddings/generator/mdSource.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// This file is based on https://github.com/supabase-community/nextjs-openai-doc-search/blob/main/lib/generate-embeddings.ts

// @ts-types="npm:@types/estree"
import { ObjectExpression } from 'estree';
import type { ObjectExpression } from 'estree';
// @ts-types="npm:@types/mdast"
import { Content, Root } from "mdast";
import { fromMarkdown } from "mdast-util-from-markdown";
Expand Down
1 change: 1 addition & 0 deletions src/embeddings/lance/writer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export class LanceWriter implements IEmbeddingWriter {
await this.#table.add(data);
}

// deno-lint-ignore require-await
async close(): Promise<void> {
return this.#table.close();
}
Expand Down
1 change: 1 addition & 0 deletions src/fromSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ type TFromObject<T extends SObject> = TFromProperties<
T["properties"],
Exclude<T["required"], undefined>[number]
> extends infer Properties extends Type.TProperties ? Type.TObject<Properties>
// deno-lint-ignore ban-types
: Type.TObject<{}>;
function FromObject<T extends SObject>(T: T): TFromObject<T> {
const properties = globalThis.Object.getOwnPropertyNames(T.properties).reduce(
Expand Down
2 changes: 2 additions & 0 deletions src/ipfs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ export class IPFSClient {
throw new Error(await res.text());
}

// This could use typebox for runtime validation
// deno-lint-ignore no-explicit-any
const mapResponse = (raw: any): AddResult => ({
path: raw.Name,
cid: raw.Hash,
Expand Down
1 change: 0 additions & 1 deletion src/ipfs_test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { expect } from "@std/expect";
import { IPFSClient } from "./ipfs.ts";
import { Buffer } from "@std/io/buffer";

const ipfs = new IPFSClient(
Deno.env.get("IPFS_ENDPOINT") ??
Expand Down
2 changes: 1 addition & 1 deletion src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export async function loadVectorStoragePath(
if (uri.protocol) {
return vectorStoragePath;
}
} catch (e) {
} catch (_e) {
// DO nothing
}

Expand Down
28 changes: 16 additions & 12 deletions src/project/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export const Project = Type.Object({
export type IFunctionTool = Static<typeof FunctionToolType>;
export type IVectorConfig = Static<typeof VectorConfig>;

type IProjectEntry<Config extends TObject> = TUnion<[
type IProjectEntry<Config extends TObject = TObject> = TUnion<[
TObject<
{
configType: TSchema;
Expand All @@ -71,14 +71,16 @@ export type IProjectEntrypoint<T extends TObject = TObject> = Static<
IProjectEntry<T>
>;

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

function validateProjectEntry(entry: any): void {
const projectType = ProjectEntrypointGen(entry?.configType);
function validateProjectEntry(entry: unknown): entry is IProjectEntry {
// deno-lint-ignore no-explicit-any
const projectType = ProjectEntrypointGen((entry as any)?.configType);

Value.Assert(projectType, entry);
return true;
}

const ProjectEntrypointGen = <T extends TObject>(t: T) =>
Expand All @@ -94,20 +96,22 @@ const ProjectEntrypointGen = <T extends TObject>(t: T) =>
]);

export async function getProjectFromEntrypoint(
entrypoint: any,
entrypoint: unknown,
): Promise<IProject> {
if (!entrypoint) {
throw new Error("Project entry is invalid");
}
// Validate the entrypoint
validateProjectEntry(entrypoint);
if (validateProjectEntry(entrypoint)) {
const config = loadConfigFromEnv(entrypoint.configType);

const config = loadConfigFromEnv(entrypoint.configType);
// Check that the constructed project is valid
const project = await entrypoint.projectFactory(config);

// Check that the constructed project is valid
const project = await entrypoint.projectFactory(config);
validateProject(project);

validateProject(project);

return project;
return project;
} else {
throw new Error('Unable to validate project');
}
}
4 changes: 2 additions & 2 deletions src/runnerHost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Runner } from "./runner.ts";
export class RunnerHost {
#runners: Record<string, Runner> = {};

constructor(private initRunner: () => Promise<Runner>) {}
constructor(private initRunner: () => Runner | Promise<Runner>) {}

async getRunner(id: string): Promise<Runner> {
this.#runners[id] ??= await this.initRunner();
Expand All @@ -12,6 +12,6 @@ export class RunnerHost {
}

async getAnonymousRunner(): Promise<Runner> {
return this.initRunner();
return await this.initRunner();
}
}
6 changes: 4 additions & 2 deletions src/sandbox/mockSandbox.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { TSchema } from "@sinclair/typebox";
import { IContext } from "../context/context.ts";
import { ITool } from "../tool.ts";
import { ISandbox } from "./sandbox.ts";
Expand All @@ -8,14 +9,15 @@ export class MockSandbox implements ISandbox {
readonly systemPrompt: string,
private tools: ITool[],
readonly userMessage?: string,
readonly config?: any,
readonly config?: TSchema,
) {}

// deno-lint-ignore require-await
async getTools() {
return this.tools.map((t) => t.toTool());
}

async runTool(toolName: string, args: any, ctx: IContext): Promise<any> {
runTool(toolName: string, args: unknown, ctx: IContext): Promise<string> {
const tool = this.tools.find((t) => t.name === toolName);

if (!tool) {
Expand Down
2 changes: 1 addition & 1 deletion src/sandbox/sandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export interface ISandbox {

getTools(): Promise<Tool[]>;

runTool(toolName: string, args: any, ctx: IContext): Promise<any>;
runTool(toolName: string, args: unknown, ctx: IContext): Promise<string>;

// TODO expand this interface with more untrusted data/functions. e.g RAG
}
4 changes: 2 additions & 2 deletions src/sandbox/unsafeSandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
validateProject,
} from "../project/project.ts";
import { ISandbox } from "./sandbox.ts";
import { TSchema } from "@sinclair/typebox";
import { IContext } from "../context/context.ts";

/**
Expand Down Expand Up @@ -49,11 +48,12 @@ export class UnsafeSandbox implements ISandbox {
return this.#project.vectorStorage;
}

// deno-lint-ignore require-await
async getTools(): Promise<Tool[]> {
return this.#project.tools.map((t) => t.toTool());
}

runTool(toolName: string, args: any, ctx: IContext): Promise<any> {
runTool(toolName: string, args: unknown, ctx: IContext): Promise<string> {
const tool = this.#project.tools.find((t) => t.name === toolName);

if (!tool) {
Expand Down
4 changes: 2 additions & 2 deletions src/sandbox/webWorker/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ export const Init = new rpc.RequestType<
export const GetConfig = new rpc.RequestType0<TSchema | undefined, void>(
"get_config",
);
export const CallTool = new rpc.RequestType2<string, any, any, void>(
export const CallTool = new rpc.RequestType2<string, unknown, string, void>(
"call_tool",
);

// Sandbox -> Framework
export const CtxVectorSearch = new rpc.RequestType2<
string,
number[],
any,
unknown[],
void
>("ctx_vector_search");
export const CtxComputeQueryEmbedding = new rpc.RequestType<
Expand Down
5 changes: 3 additions & 2 deletions src/sandbox/webWorker/webWorkerSandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export class WebWorkerSandbox implements ISandbox {

// Need to restore the config and make it compatible as it uses symbols internally
const configType = rawConfigType
// @ts-ignore
// @ts-ignore functionally works but types are too complex
? FromSchema(JSON.parse(JSON.stringify(rawConfigType)))
: undefined;
const config = loadConfigFromEnv(configType);
Expand Down Expand Up @@ -91,6 +91,7 @@ export class WebWorkerSandbox implements ISandbox {
return this.#config;
}

// deno-lint-ignore require-await
async getTools(): Promise<Tool[]> {
return this.#tools;
}
Expand All @@ -112,7 +113,7 @@ export class WebWorkerSandbox implements ISandbox {
this.#hasSetupCxt = true;
}

runTool(toolName: string, args: any, ctx: IContext): Promise<any> {
runTool(toolName: string, args: unknown, ctx: IContext): Promise<string> {
// Connect up context so sandbox can call application
this.#connection.onRequest(CtxVectorSearch, async (tableName, vector) => {
const res = await ctx.vectorSearch(tableName, vector);
Expand Down
4 changes: 2 additions & 2 deletions src/tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ export abstract class FunctionTool<P extends Parameters = Parameters>
abstract parameters: P;
abstract description: string;
abstract call(
args: Record<any, any>, /*RequiredParams<P> & OptionalParams<P>*/
args: unknown, /*RequiredParams<P> & OptionalParams<P>*/
ctx: IContext,
): Promise<any>;
): Promise<string>;

toTool(): Tool {
return {
Expand Down
3 changes: 2 additions & 1 deletion subquery-delegator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ If the question seems to be unrelated to the API, just return "I don't know" as

export const entrypoint: IProjectEntrypoint<typeof ConfigType> = {
configType: ConfigType,
// deno-lint-ignore require-await
projectFactory: async (config: Config) => {
const tools = [
new TotalDelegation(config.GRAPHQL_ENDPOINT),
Expand Down Expand Up @@ -67,7 +68,7 @@ export const entrypoint: IProjectEntrypoint<typeof ConfigType> = {
};

// Some example messages to ask with this set of tools
const messages = [
const _messages = [
// Delegation
"My address is 0x108A496cDC32DA84e4D5905bb02ED695BC1024cd, use this for any further prompts. What is my delegation?",
"Who am i delegating to?",
Expand Down
4 changes: 2 additions & 2 deletions subquery-delegator/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export async function grahqlRequest<T = any>(
export async function grahqlRequest<T = unknown>(
endpoint: string,
query: string,
variables?: unknown,
Expand All @@ -19,7 +19,7 @@ export async function grahqlRequest<T = any>(
if (res.errors) {
console.log(`Request failed\n${query}`);

throw new Error(res.errors.map((e: any) => e.message).join("\n"));
throw new Error(res.errors.map((e: { message: string }) => e.message).join("\n"));
}

return res.data;
Expand Down

0 comments on commit 53578c4

Please sign in to comment.