Skip to content

Commit

Permalink
fix: circular dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
whilefoo committed Nov 4, 2023
1 parent 85a3bf4 commit 3e939d5
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 181 deletions.
4 changes: 3 additions & 1 deletion src/adapters/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { User } from "./supabase/helpers/tables/user";
import { Wallet } from "./supabase/helpers/tables/wallet";
import { Database } from "./supabase/types";
import { env } from "../bindings/env";
import OpenAI from "openai";

const supabaseClient = createClient<Database>(env.SUPABASE_URL, env.SUPABASE_KEY, { auth: { persistSession: false } });

Expand All @@ -22,9 +23,10 @@ export function createAdapters(context: Context) {
debit: new Settlement(supabaseClient, context),
settlement: new Settlement(supabaseClient, context),
label: new Label(supabaseClient, context),
logs: new Logs(supabaseClient, context),
logs: new Logs(supabaseClient, context, env.LOG_ENVIRONMENT, env.LOG_RETRY_LIMIT, env.LOG_LEVEL),
locations: new Locations(supabaseClient, context),
super: new Super(supabaseClient, context),
},
openAi: context.config.keys.openAi ? new OpenAI({ apiKey: context.config.keys.openAi }) : null,
};
}
12 changes: 6 additions & 6 deletions src/adapters/supabase/helpers/tables/logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import { Database } from "../../types";
import { prettyLogs } from "../pretty-logs";
import { Super } from "./super";
import { execSync } from "child_process";
import { Context, LogLevel } from "../../../../types";
import { LogLevel } from "../../../../types/logs";
import { Context } from "../../../../types/context";
import Runtime from "../../../../bindings/bot-runtime";
import { env } from "../../../../bindings/env";

type LogFunction = (message: string, metadata?: any) => void;
type LogInsert = Database["public"]["Tables"]["logs"]["Insert"];
Expand Down Expand Up @@ -223,12 +223,12 @@ export class Logs extends Super {
});
}

constructor(supabase: SupabaseClient, context: Context) {
constructor(supabase: SupabaseClient, context: Context, environment: string, retryLimit: number, logLevel: LogLevel) {
super(supabase, context);

this.environment = env.LOG_ENVIRONMENT;
this.retryLimit = env.LOG_RETRY_LIMIT;
this.maxLevel = this._getNumericLevel(env.LOG_LEVEL);
this.environment = environment;
this.retryLimit = retryLimit;
this.maxLevel = this._getNumericLevel(logLevel);
}

private async _sendLogsToSupabase(log: LogInsert) {
Expand Down
2 changes: 1 addition & 1 deletion src/bindings/env.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EnvConfig, validateEnvConfig } from "../types";
import { EnvConfig, validateEnvConfig } from "../types/configuration-types";
import dotenv from "dotenv";
dotenv.config();

Expand Down
15 changes: 4 additions & 11 deletions src/bindings/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,12 @@ import { LogMessage } from "../adapters/supabase/helpers/tables/logs";
import { processors, wildcardProcessors } from "../handlers/processors";
import { validateConfigChange } from "../handlers/push";
import { addCommentToIssue, shouldSkip } from "../helpers";
import {
GitHubEvent,
MainActionHandler,
PayloadSchema,
PostActionHandler,
PreActionHandler,
WildCardHandler,
} from "../types";
import { Payload } from "../types/payload";
import { ajv } from "../utils";
import { MainActionHandler, PostActionHandler, PreActionHandler, WildCardHandler } from "../types/handlers";
import { Payload, PayloadSchema, GitHubEvent } from "../types/payload";
import { ajv } from "../utils/ajv";
import Runtime from "./bot-runtime";
import { loadConfiguration } from "./config";
import { Context } from "../types";
import { Context } from "../types/context";

const NO_VALIDATION = [GitHubEvent.INSTALLATION_ADDED_EVENT, GitHubEvent.PUSH_EVENT] as string[];
type PreHandlerWithType = { type: string; actions: PreActionHandler[] };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import { encodingForModel } from "js-tiktoken";
import Decimal from "decimal.js";
import Runtime from "../../../../bindings/bot-runtime";

//const openai = new OpenAI(); // apiKey: // defaults to process.env["OPENAI_API_KEY"]

export async function calculateQualScore(issue: Issue, contributorComments: Comment[]) {
const sumOfConversationTokens = countTokensOfConversation(issue, contributorComments);
const estimatedOptimalModel = estimateOptimalModel(sumOfConversationTokens);
Expand Down Expand Up @@ -56,10 +54,13 @@ export async function gptRelevance(
CONVERSATION_STRINGS: string[],
ARRAY_LENGTH = CONVERSATION_STRINGS.length
) {
const runtime = Runtime.getState();
const openAi = runtime.adapters.openAi;
if (!openAi) throw new Error("OpenAI adapter is not defined");
const PROMPT = `I need to evaluate the relevance of GitHub contributors' comments to a specific issue specification. Specifically, I'm interested in how much each comment helps to further define the issue specification or contributes new information or research relevant to the issue. Please provide a float between 0 and 1 to represent the degree of relevance. A score of 1 indicates that the comment is entirely relevant and adds significant value to the issue, whereas a score of 0 indicates no relevance or added value. Each contributor's comment is on a new line.\n\nIssue Specification:\n\`\`\`\n${ISSUE_SPECIFICATION_BODY}\n\`\`\`\n\nConversation:\n\`\`\`\n${CONVERSATION_STRINGS.join(
"\n"
)}\n\`\`\`\n\n\nTo what degree are each of the comments in the conversation relevant and valuable to further defining the issue specification? Please reply with an array of float numbers between 0 and 1, corresponding to each comment in the order they appear. Each float should represent the degree of relevance and added value of the comment to the issue. The total length of the array in your response should equal exactly ${ARRAY_LENGTH} elements.`;
const response: OpenAI.Chat.ChatCompletion = await openai.chat.completions.create({
const response: OpenAI.Chat.ChatCompletion = await openAi.chat.completions.create({
model: model,
messages: [
{
Expand Down
221 changes: 62 additions & 159 deletions src/types/configuration-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Type as T, Static, TProperties, TObject, ObjectOptions, StringOptions,
import { LogLevel } from "../types";
import { validHTMLElements } from "../handlers/comment/handlers/issue/valid-html-elements";
import { userCommands } from "../handlers";
import { ajv } from "../utils";
import { ajv } from "../utils/ajv";
import ms from "ms";

const promotionComment =
Expand Down Expand Up @@ -35,106 +35,6 @@ const defaultPriorityLabels = [
{ name: "Priority: 5 (Emergency)" },
];

/*
export const EnvConfigSchema = z.object({
WEBHOOK_PROXY_URL: z.string().url(),
LOG_ENVIRONMENT: z.string().default("development"),
LOG_LEVEL: z.nativeEnum(LogLevel).default(LogLevel.SILLY),
LOG_RETRY_LIMIT: z.number().default(8),
SUPABASE_URL: z.string().url(),
SUPABASE_KEY: z.string(),
X25519_PRIVATE_KEY: z.string(),
PRIVATE_KEY: z.string(),
APP_ID: z.number(),
});
export type EnvConfig = z.infer<typeof EnvConfigSchema>;
export const BotConfigSchema = z.strictObject({
keys: z.strictObject({
evmPrivateEncrypted: z.string().optional(),
openAi: z.string().optional(),
}),
features: z.strictObject({
assistivePricing: z.boolean().default(false),
defaultLabels: z.string().array().default([]),
newContributorGreeting: z
.strictObject({
header: z.string().default(
"Thank you for contributing! \
Please be sure to set your wallet address \
before completing your first task so that you can \
collect your reward."
),
displayHelpMenu: z.boolean().default(true),
footer: z.string().default(promotionComment),
})
.default({}),
publicAccessControl: z
.strictObject({
setLabel: z.boolean().default(true),
fundExternalClosedIssue: z.boolean().default(true),
})
.default({}),
}),
timers: z
.strictObject({
reviewDelayTolerance: z.string().default("1 day"),
taskStaleTimeoutDuration: z.string().default("1 month"),
taskFollowUpDuration: z.string().default("0.5 weeks"),
taskDisqualifyDuration: z.string().default("1 week"),
})
.default({}),
payments: z
.strictObject({
maxPermitPrice: z.number().default(Number.MAX_SAFE_INTEGER),
evmNetworkId: z.number().default(1),
basePriceMultiplier: z.number().default(1),
issueCreatorMultiplier: z.number().default(1),
})
.default({}),
commands: z.array(
z.strictObject({
name: z.string(),
enabled: z.boolean(),
})
),
incentives: z
.strictObject({
comment: z
.strictObject({
elements: z.record(z.union(HtmlEntities), z.number()).default(allHtmlElementsSetToZero),
totals: z
.strictObject({
character: z.number().default(0),
word: z.number().default(0),
sentence: z.number().default(0),
paragraph: z.number().default(0),
comment: z.number().default(0),
})
.default({}),
})
.default({}),
})
.default({}),
labels: z
.strictObject({
time: z.array(z.strictObject({ name: z.string() })).default(defaultTimeLabels),
priority: z.array(z.strictObject({ name: z.string() })).default(defaultPriorityLabels),
})
.default({}),
miscellaneous: z
.strictObject({
maxConcurrentTasks: z.number().default(Number.MAX_SAFE_INTEGER),
promotionComment: z.string().default(promotionComment),
registerWalletWithVerification: z.boolean().default(false),
})
.default({}),
});
export type BotConfig = z.infer<typeof BotConfigSchema>;
*/

function StrictObject<T extends TProperties>(obj: T, options?: ObjectOptions): TObject<T> {
return T.Object<T>(obj, { additionalProperties: false, default: {}, ...options });
}
Expand Down Expand Up @@ -164,69 +64,72 @@ export const EnvConfigSchema = T.Object({
export const validateEnvConfig = ajv.compile(EnvConfigSchema);
export type EnvConfig = Static<typeof EnvConfigSchema>;

export const BotConfigSchema = StrictObject({
keys: StrictObject({
evmPrivateEncrypted: T.String(),
openAi: T.Optional(T.String()),
}),
features: StrictObject({
assistivePricing: T.Boolean({ default: false }),
defaultLabels: T.Array(T.String(), { default: [] }),
newContributorGreeting: StrictObject({
enabled: T.Boolean({ default: false }),
header: T.String({ default: defaultGreetingHeader }),
displayHelpMenu: T.Boolean({ default: true }),
footer: T.String({ default: promotionComment }),
export const BotConfigSchema = StrictObject(
{
keys: StrictObject({
evmPrivateEncrypted: T.String(),
openAi: T.Optional(T.String()),
}),
features: StrictObject({
assistivePricing: T.Boolean({ default: false }),
defaultLabels: T.Array(T.String(), { default: [] }),
newContributorGreeting: StrictObject({
enabled: T.Boolean({ default: false }),
header: T.String({ default: defaultGreetingHeader }),
displayHelpMenu: T.Boolean({ default: true }),
footer: T.String({ default: promotionComment }),
}),
publicAccessControl: StrictObject({
setLabel: T.Boolean({ default: true }),
fundExternalClosedIssue: T.Boolean({ default: true }),
}),
}),
publicAccessControl: StrictObject({
setLabel: T.Boolean({ default: true }),
fundExternalClosedIssue: T.Boolean({ default: true }),
openai: StrictObject({
tokenLimit: T.Number({ default: 100000 }),
}),
}),
openai: StrictObject({
tokenLimit: T.Number({ default: 100000 }),
}),
timers: StrictObject({
reviewDelayTolerance: stringDuration({ default: "1 day" }),
taskStaleTimeoutDuration: stringDuration({ default: "1 month" }),
taskFollowUpDuration: stringDuration({ default: "0.5 weeks" }),
taskDisqualifyDuration: stringDuration({ default: "1 week" }),
}),
payments: StrictObject({
maxPermitPrice: T.Number({ default: Number.MAX_SAFE_INTEGER }),
evmNetworkId: T.Number({ default: 1 }),
basePriceMultiplier: T.Number({ default: 1 }),
issueCreatorMultiplier: T.Number({ default: 1 }),
}),
commands: T.Array(
StrictObject({
name: T.String(),
enabled: T.Boolean(),
timers: StrictObject({
reviewDelayTolerance: stringDuration({ default: "1 day" }),
taskStaleTimeoutDuration: stringDuration({ default: "1 month" }),
taskFollowUpDuration: stringDuration({ default: "0.5 weeks" }),
taskDisqualifyDuration: stringDuration({ default: "1 week" }),
}),
{ default: allCommands }
),
incentives: StrictObject({
comment: StrictObject({
elements: T.Record(T.Union(HtmlEntities), T.Number(), { default: allHtmlElementsSetToZero }),
totals: StrictObject({
character: T.Number({ default: 0, minimum: 0 }),
word: T.Number({ default: 0, minimum: 0 }),
sentence: T.Number({ default: 0, minimum: 0 }),
paragraph: T.Number({ default: 0, minimum: 0 }),
comment: T.Number({ default: 0, minimum: 0 }),
payments: StrictObject({
maxPermitPrice: T.Number({ default: Number.MAX_SAFE_INTEGER }),
evmNetworkId: T.Number({ default: 1 }),
basePriceMultiplier: T.Number({ default: 1 }),
issueCreatorMultiplier: T.Number({ default: 1 }),
}),
commands: T.Array(
StrictObject({
name: T.String(),
enabled: T.Boolean(),
}),
{ default: allCommands }
),
incentives: StrictObject({
comment: StrictObject({
elements: T.Record(T.Union(HtmlEntities), T.Number(), { default: allHtmlElementsSetToZero }),
totals: StrictObject({
character: T.Number({ default: 0, minimum: 0 }),
word: T.Number({ default: 0, minimum: 0 }),
sentence: T.Number({ default: 0, minimum: 0 }),
paragraph: T.Number({ default: 0, minimum: 0 }),
comment: T.Number({ default: 0, minimum: 0 }),
}),
}),
}),
}),
labels: StrictObject({
time: T.Array(StrictObject({ name: T.String() }), { default: defaultTimeLabels }),
priority: T.Array(StrictObject({ name: T.String() }), { default: defaultPriorityLabels }),
}),
miscellaneous: StrictObject({
maxConcurrentTasks: T.Number({ default: Number.MAX_SAFE_INTEGER }),
promotionComment: T.String({ default: promotionComment }),
registerWalletWithVerification: T.Boolean({ default: false }),
}),
});
labels: StrictObject({
time: T.Array(StrictObject({ name: T.String() }), { default: defaultTimeLabels }),
priority: T.Array(StrictObject({ name: T.String() }), { default: defaultPriorityLabels }),
}),
miscellaneous: StrictObject({
maxConcurrentTasks: T.Number({ default: Number.MAX_SAFE_INTEGER }),
promotionComment: T.String({ default: promotionComment }),
registerWalletWithVerification: T.Boolean({ default: false }),
}),
},
{ default: undefined }
);
export const validateBotConfig = ajv.compile(BotConfigSchema);

export type BotConfig = StaticDecode<typeof BotConfigSchema>;
1 change: 1 addition & 0 deletions src/utils/ajv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const ajv = addFormats(
removeAdditional: true,
useDefaults: true,
allErrors: true,
coerceTypes: true,
}),
{
formats: [
Expand Down

0 comments on commit 3e939d5

Please sign in to comment.