Skip to content

Commit

Permalink
chore: use open schemas on deno land
Browse files Browse the repository at this point in the history
  • Loading branch information
jack0pan committed Apr 12, 2024
1 parent 7f0b61c commit 0f0a020
Show file tree
Hide file tree
Showing 12 changed files with 58 additions and 15 deletions.
2 changes: 2 additions & 0 deletions consts/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ export const FILE_INFO_KEY = "file_info";
// 10 minutes, unit: second
export const RUN_EXPIRED_DURATION = 10 * 60;

export const DEFAULT_ORGANIZATION = "org";

// files api
export const DEFAULT_FILE_DIR = "/tmp/assistant";
export const DEFAULT_ORG_FILES_SIZE_MAX = 100_000_000_000; // 100GB
5 changes: 5 additions & 0 deletions consts/api_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
RUN_EXPIRED_DURATION,
DEFAULT_FILE_DIR,
DEFAULT_ORG_FILES_SIZE_MAX,
DEFAULT_ORGANIZATION,
} from "$/consts/api.ts";

describe("HTTP Headers", () => {
Expand Down Expand Up @@ -122,4 +123,8 @@ describe("API consts", () => {
it("has DEFAULT_ORG_FILES_SIZE_MAX const", () => {
assertEquals(DEFAULT_ORG_FILES_SIZE_MAX, 100_000_000_000);
});

it("has DEFAULT_ORGANIZATION const", () => {
assertEquals(DEFAULT_ORGANIZATION, "org");
});
});
3 changes: 3 additions & 0 deletions consts/envs.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// deno
export const DENO_KV_PATH = "DENO_KV_PATH";

// for single tenant
export const NO_TENANT = "NO_TENANT";

// the evn names for log
export const LOG_LEVEL = "LOG_LEVEL";

Expand Down
7 changes: 6 additions & 1 deletion consts/envs_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
ANTHROPIC_VERSION,
FILE_DIR,
ORG_FILES_SIZE_MAX,
NO_TENANT,
} from "$/consts/envs.ts";

describe("Log variables", () => {
Expand Down Expand Up @@ -51,12 +52,16 @@ describe("Anthropic variables", () => {
});
});

describe("Files API variables", () => {
describe("API variables", () => {
it("The FILE_DIR const exists", () => {
assertExists(FILE_DIR);
});

it("The ORG_FILES_SIZE_MAX const exists", () => {
assertExists(ORG_FILES_SIZE_MAX);
});

it("The NO_TENANT const exists", () => {
assertExists(NO_TENANT);
});
});
1 change: 1 addition & 0 deletions deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"imports": {
"$/": "./",
"$fresh/": "https://deno.land/x/[email protected]/",
"$open-schemas/": "https://deno.land/x/[email protected]/",
"$std/": "https://deno.land/[email protected]/",
"@open-schemas/zod": "jsr:@open-schemas/zod@^0.10.2",
"@std/assert": "jsr:@std/assert@^0.221.0",
Expand Down
13 changes: 13 additions & 0 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion providers/llm/anthropic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ import {
CreateMessageResponseToAssistantResponse,
MessageObjectToMessage,
StepObjectToMessages,
ConvertRetrievalToolToTools,
} from "$/schemas/anthropic/messages.ts";
import { MessageToChunkStream } from "$/schemas/anthropic/streaming_messages.ts";
import { USER_PROMPT, FILES_PROMPT } from "$/utils/prompts.ts";
import { XML } from "$/utils/xml.ts";
import { ConvertRetrievalToolToTools } from "$/schemas/anthropic/messages.ts";
import { getEnv } from "$/utils/env.ts";
import { APPLICATION_JSON_HEADER } from "$/consts/api.ts";

Expand Down
4 changes: 2 additions & 2 deletions providers/llm/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import {
MessageObject,
StepObject,
Tool,
AssistantResponse,
FileObject,
} from "@open-schemas/zod/openai";
import { AssistantResponse } from "$open-schemas/zod/openai/mod.ts";

/**
* Base class for language model providers.
Expand Down Expand Up @@ -52,7 +52,7 @@ export class Base {
throw new NotImplemented("Base.createChatCompletion");
}

static async runStep(
static runStep(
_model: string,
_messages: MessageObject[],
_steps: StepObject[],
Expand Down
11 changes: 6 additions & 5 deletions routes/internal/_middleware.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import { FreshContext } from "$fresh/server.ts";
import { DEFAULT_ORGANIZATION } from "$/consts/api.ts";
import { BadRequest, Unauthorized } from "$/utils/errors.ts";
import { State } from "$/routes/_middleware.ts";

export function handler(req: Request, ctx: FreshContext<State>) {
const token = req.headers.get("X-Assist-Token");
const token = req.headers.get("X-Assistant-Token");
if (token && token !== Deno.env.get("INTERNAL_TOKEN")) {
throw new Unauthorized(undefined, { cause: "Invalid internal token." });
throw new Unauthorized(undefined, { cause: "Invalid X-Assistant-Token header." });
}

let organization = req.headers.get("X-Assist-Org-Id");
let organization = req.headers.get("X-Assistant-Organization");
if (Deno.env.get("NO_TENANT") === "true") {
organization = "#org";
organization = DEFAULT_ORGANIZATION;
}
if (!organization) {
throw new BadRequest(undefined, {
cause: "Missing X-Assist-Org-Id header.",
cause: "Invalid X-Assistant-Organization header.",
});
}
ctx.state.organization = organization;
Expand Down
13 changes: 8 additions & 5 deletions routes/v1/_middleware.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
import { FreshContext } from "$fresh/server.ts";
import { DEFAULT_ORGANIZATION } from "$/consts/api.ts";
import { NO_TENANT } from "$/consts/envs.ts";
import { Unauthorized } from "$/utils/errors.ts";
import { State } from "$/routes/_middleware.ts";
import { TokenRepository } from "$/repositories/token.ts";

export async function handler(req: Request, ctx: FreshContext<State>) {
if (Deno.env.get("NO_TENANT") === "true") {
ctx.state.organization = "#org";
if (Deno.env.get(NO_TENANT) === "true") {
ctx.state.organization = DEFAULT_ORGANIZATION;
} else {
const unauthorized = new Unauthorized(undefined, { cause: "Bad credentials." });
const authorization = req.headers.get("Authorization");
if (!authorization) {
throw new Unauthorized("Missing authorization header.");
throw unauthorized;
}
const [type, token] = authorization.split(" ");
if (!type || !token || type.toLowerCase() !== "bearer") {
throw new Unauthorized("Invalid authorization format.");
throw unauthorized;
}

const result = await TokenRepository.findOrgByToken(token);
if (!result.value) throw new Unauthorized("Invalid token.");
if (!result.value) throw unauthorized;
ctx.state.organization = result.value;
}

Expand Down
2 changes: 1 addition & 1 deletion schemas/anthropic/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ import {
FunctionToolCall,
RetrievalToolCall,
ToolCall,
AssistantResponse,
RetrievalTool,
} from "@open-schemas/zod/openai";
import { AssistantResponse } from "$/schemas/assistant.ts";
import { CALLS_STOP, CHAT_COMPLETION_PREFIX } from "$/consts/llm.ts";
import { TOOLS_PROMPT, FUNCTION_TOOLS_PROMPT } from "$/utils/prompts.ts";
import { XML } from "$/utils/xml.ts";
Expand Down
10 changes: 10 additions & 0 deletions schemas/assistant.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type { MessageTextContent, ToolCall, Usage } from "$open-schemas/types/openai/mod.ts";

/**
* Unified assistant response for Open Assistant.
*/
export type AssistantResponse = {
content?: MessageTextContent;
tool_calls?: ToolCall[];
usage: Usage;
};

0 comments on commit 0f0a020

Please sign in to comment.