Skip to content

Commit

Permalink
Merge pull request #58 from gentlementlegen/fix/start-message
Browse files Browse the repository at this point in the history
fix: a start message saying the bot is "thinking" is displayed
  • Loading branch information
gentlementlegen authored Feb 5, 2025
2 parents 0f0dabf + a28af77 commit 0bd7615
Show file tree
Hide file tree
Showing 15 changed files with 2,553 additions and 43 deletions.
2 changes: 1 addition & 1 deletion .husky/commit-msg
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

yarn commitlint --edit "$1"
bun commitlint --edit "$1"
2 changes: 1 addition & 1 deletion .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

yarn lint-staged
bun lint-staged
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,5 @@ UBIQUITY_OS_APP_NAME="UbiquityOS"
## Testing

```sh
yarn test
bun run test
```
2,445 changes: 2,445 additions & 0 deletions bun.lock

Large diffs are not rendered by default.

Binary file removed bun.lockb
Binary file not shown.
12 changes: 6 additions & 6 deletions dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default tsEslint.config({
},
],
"func-style": [
"warn",
"error",
"declaration",
{
allowArrowFunctions: false,
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"dependencies": {
"@sinclair/typebox": "0.34.3",
"@supabase/supabase-js": "^2.45.4",
"@ubiquity-os/plugin-sdk": "^1.1.0",
"@ubiquity-os/plugin-sdk": "^2.0.6",
"@ubiquity-os/ubiquity-os-logger": "^1.3.2",
"dotenv": "^16.4.5",
"gpt-tokenizer": "^2.5.1",
Expand Down Expand Up @@ -75,7 +75,7 @@
},
"lint-staged": {
"*.ts": [
"yarn prettier --write",
"prettier --write",
"eslint --fix"
],
"src/**.{ts,json}": [
Expand Down
54 changes: 39 additions & 15 deletions src/handlers/add-comment.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Context } from "../types/context";
import { Context } from "../types";

interface CommentOptions {
inReplyTo?: {
Expand All @@ -12,7 +12,7 @@ interface CommentOptions {
* @param message - The message to add as a comment
* @param options - Optional parameters for pull request review comments
*/
export async function addCommentToIssue(context: Context, message: string, options?: CommentOptions) {
export async function addCommentToIssue(context: Context, message: string, options?: CommentOptions): Promise<void> {
const { payload } = context;
const owner = payload.repository.owner.login;
const repo = payload.repository.name;
Expand All @@ -36,13 +36,24 @@ export async function addCommentToIssue(context: Context, message: string, optio

if (options.inReplyTo.commentId) {
// Reply to an existing review comment
await context.octokit.rest.pulls.createReplyForReviewComment({
owner,
repo,
pull_number: pullNumber,
body: message,
comment_id: options.inReplyTo.commentId,
});
if (addCommentToIssue.lastCommentId) {
await context.octokit.rest.pulls.updateReviewComment({
owner,
repo,
pull_number: pullNumber,
body: message,
comment_id: addCommentToIssue.lastCommentId,
});
} else {
const { data } = await context.octokit.rest.pulls.createReplyForReviewComment({
owner,
repo,
pull_number: pullNumber,
body: message,
comment_id: options.inReplyTo.commentId,
});
addCommentToIssue.lastCommentId = data.id;
}
}
} else {
// Regular issue comment
Expand All @@ -59,12 +70,23 @@ export async function addCommentToIssue(context: Context, message: string, optio
throw new Error("Cannot determine issue/PR number");
}

await context.octokit.rest.issues.createComment({
owner,
repo,
issue_number: issueNumber,
body: message,
});
if (addCommentToIssue.lastCommentId) {
await context.octokit.rest.issues.updateComment({
owner,
repo,
issue_number: issueNumber,
body: message,
comment_id: addCommentToIssue.lastCommentId,
});
} else {
const { data } = await context.octokit.rest.issues.createComment({
owner,
repo,
issue_number: issueNumber,
body: message,
});
addCommentToIssue.lastCommentId = data.id;
}
}
} catch (e: unknown) {
const error = e instanceof Error ? e : new Error(String(e));
Expand All @@ -81,3 +103,5 @@ export async function addCommentToIssue(context: Context, message: string, optio
throw error;
}
}

addCommentToIssue.lastCommentId = null as number | null;
6 changes: 3 additions & 3 deletions src/handlers/ask-llm.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Context } from "../types";
import { CompletionsType } from "../adapters/openai/helpers/completions";
import { bubbleUpErrorComment, logger } from "../helpers/errors";
import { formatChatHistory } from "../helpers/format-chat-history";
import { fetchSimilarContent } from "../helpers/issue-fetching";
import { Context } from "../types";
import { fetchRepoDependencies, fetchRepoLanguageStats } from "./ground-truths/chat-bot";
import { findGroundTruths } from "./ground-truths/find-ground-truths";
import { bubbleUpErrorComment, logger } from "../helpers/errors";
import { fetchSimilarContent } from "../helpers/issue-fetching";

export async function askQuestion(context: Context, question: string): Promise<CompletionsType> {
if (!question) {
Expand Down
9 changes: 5 additions & 4 deletions src/handlers/comment-created-callback.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { LogReturn } from "@ubiquity-os/ubiquity-os-logger";
import { bubbleUpErrorComment, sanitizeMetadata } from "../helpers/errors";
import { Context } from "../types";
import { CallbackResult } from "../types/proxy";
import { addCommentToIssue } from "./add-comment";
import { askQuestion } from "./ask-llm";
import { CallbackResult } from "../types/proxy";
import { bubbleUpErrorComment, sanitizeMetadata } from "../helpers/errors";
import { LogReturn } from "@ubiquity-os/ubiquity-os-logger";

export async function processCommentCallback(context: Context<"issue_comment.created" | "pull_request_review_comment.created">): Promise<CallbackResult> {
const { logger, command, payload } = context;
const { logger, command, payload, env } = context;
let question = "";

if (payload.comment.user?.type === "Bot") {
Expand All @@ -22,6 +22,7 @@ export async function processCommentCallback(context: Context<"issue_comment.cre
}

try {
await addCommentToIssue(context, `${env.UBIQUITY_OS_APP_NAME} is thinking...`);
const response = await askQuestion(context, question);
const { answer, tokenUsage, groundTruths } = response;
if (!answer) {
Expand Down
6 changes: 3 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { createActionsPlugin } from "@ubiquity-os/plugin-sdk";
import { LogLevel } from "@ubiquity-os/ubiquity-os-logger";
import { createAdapters } from "./adapters";
import { plugin } from "./plugin";
import { Command } from "./types/command";
import { SupportedEvents } from "./types/context";
import { Env, envSchema } from "./types/env";
import { PluginSettings, pluginSettingsSchema } from "./types/plugin-input";
import { Command } from "./types/command";
import { plugin } from "./plugin";
import { LogLevel } from "@ubiquity-os/ubiquity-os-logger";

createActionsPlugin<PluginSettings, Env, Command, SupportedEvents>(
(context) => {
Expand Down
11 changes: 6 additions & 5 deletions src/worker.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { createPlugin } from "@ubiquity-os/plugin-sdk";
import { Manifest } from "@ubiquity-os/plugin-sdk/manifest";
import { LogLevel } from "@ubiquity-os/ubiquity-os-logger";
import type { ExecutionContext } from "hono";
import manifest from "../manifest.json";
import { createAdapters } from "./adapters";
import { plugin } from "./plugin";
import { Command } from "./types/command";
import { SupportedEvents } from "./types/context";
import { Env, envSchema } from "./types/env";
import { PluginSettings, pluginSettingsSchema } from "./types/plugin-input";
import manifest from "../manifest.json";
import { Command } from "./types/command";
import { plugin } from "./plugin";
import { Manifest } from "@ubiquity-os/plugin-sdk/manifest";
import { LogLevel } from "@ubiquity-os/ubiquity-os-logger";

export default {
async fetch(request: Request, env: Env, executionCtx?: ExecutionContext) {
Expand All @@ -26,6 +26,7 @@ export default {
settingsSchema: pluginSettingsSchema,
logLevel: env.LOG_LEVEL as LogLevel,
kernelPublicKey: env.KERNEL_PUBLIC_KEY,
bypassSignatureVerification: process.env.NODE_ENV === "local",
}
).fetch(request, env, executionCtx);
},
Expand Down
36 changes: 36 additions & 0 deletions tests/log.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { jest } from "@jest/globals";
import { Logs } from "@ubiquity-os/ubiquity-os-logger";

describe("Log post message test", () => {
it("Should post a waiting message on start", async () => {
const addCommentToIssue = jest.fn();
jest.unstable_mockModule("../src/handlers/add-comment", () => ({
addCommentToIssue,
}));
jest.unstable_mockModule("../src/handlers/ask-llm", () => ({
askQuestion: jest.fn(() => ({
answer: "hello",
tokenUsage: 1,
groundThreshold: [],
})),
}));
const { processCommentCallback } = await import("../src/handlers/comment-created-callback");
const context = {
payload: {
comment: {
user: {
type: "User",
},
body: "/ask hello",
},
},
logger: new Logs("debug"),
env: {
UBIQUITY_OS_APP_NAME: "UbiquityOS",
},
} as never;

await processCommentCallback(context);
expect(addCommentToIssue).toHaveBeenCalledWith(expect.anything(), "UbiquityOS is thinking...");
});
});
5 changes: 4 additions & 1 deletion wrangler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ compatibility_flags = [ "nodejs_compat" ]
[env.prod]

[observability]
enabled = true
enabled = true

[version_metadata]
binding = "CLOUDFLARE_VERSION_METADATA"

0 comments on commit 0bd7615

Please sign in to comment.