Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

genai comments #605

Draft
wants to merge 12 commits into
base: main
Choose a base branch
from
Draft
66 changes: 66 additions & 0 deletions docs/genaisrc/genaiscript.d.ts

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

66 changes: 66 additions & 0 deletions genaisrc/genaiscript.d.ts

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

10 changes: 7 additions & 3 deletions packages/core/src/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@
super()
}

static byName<K, V>(name: string): JSONLineCache<K, V> {
static byName<K, V>(
name: string,
options?: { snapshot?: boolean }
): JSONLineCache<K, V> {
const { snapshot } = options || {}
name = name.replace(/[^a-z0-9_]/gi, "_")
const key = "cacheKV." + name
if (host.userState[key]) return host.userState[key]
if (!snapshot && host.userState[key]) return host.userState[key]
const r = new JSONLineCache<K, V>(name)
host.userState[key] = r
if (!snapshot) host.userState[key] = r
return r

Check failure on line 26 in packages/core/src/cache.ts

View workflow job for this annotation

GitHub Actions / build

The function `byName` does not handle the case when `options` is undefined. This could lead to a TypeError when trying to destructure `snapshot` from `options`. Consider adding a default value for `options` in the function parameters or a check for `options` before destructuring. 🛠️

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function byName does not handle the case when options is undefined. This could lead to a TypeError when trying to destructure snapshot from options. Consider adding a default value for options in the function parameters or a check for options before destructuring. 🛠️

generated by pr-review-commit missing_error_handling

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name parameter in the byName method is not validated for being a non-empty string. This could lead to unexpected behavior if an empty string or a string with only whitespace is passed.

generated by pr-review-commit missing_validation

}

private folder() {
Expand Down
22 changes: 22 additions & 0 deletions packages/core/src/comments.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { JSONLineCache } from "./cache"
import { COMMENTS_CACHE } from "./constants"

export interface CommentKey {
uri: string
line: number
source: string
}

export function commentsCache(options?: { snapshot?: boolean }) {
const cache = JSONLineCache.byName<CommentKey, CommentThread>(
COMMENTS_CACHE,
options
)
return cache
}

export async function commentsForSource(source: string) {
const cache = commentsCache()
const entries = await cache.entries()
return entries.map(({ val }) => val).filter((c) => c.source === source)
}

Check failure on line 22 in packages/core/src/comments.ts

View workflow job for this annotation

GitHub Actions / build

The function `commentsCache` does not handle the case when `options` is undefined. This could lead to a TypeError when passing `options` to `JSONLineCache.byName`. Consider adding a default value for `options` in the function parameters or a check for `options` before passing it to `JSONLineCache.byName`. 🛠️

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function commentsCache does not handle the case when options is undefined. This could lead to a TypeError when passing options to JSONLineCache.byName. Consider adding a default value for options in the function parameters or a check for options before passing it to JSONLineCache.byName. 🛠️

generated by pr-review-commit missing_error_handling

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function commentsCache does not have a return type. It is a good practice to always specify return types for better readability and maintainability.

generated by pr-review-commit missing_return_type

1 change: 1 addition & 0 deletions packages/core/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ export const AI_REQUESTS_CACHE = "airequests"
export const CHAT_CACHE = "chat"
export const GITHUB_PULL_REQUEST_REVIEWS_CACHE = "prr"
export const GITHUB_PULLREQUEST_REVIEW_COMMENT_LINE_DISTANCE = 5
export const COMMENTS_CACHE = "comments"

export const PLACEHOLDER_API_BASE = "<custom api base>"
export const PLACEHOLDER_API_KEY = "<your token>"
Expand Down
66 changes: 66 additions & 0 deletions packages/core/src/genaisrc/genaiscript.d.ts

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

3 changes: 3 additions & 0 deletions packages/core/src/promptrunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { validateJSONWithSchema } from "./schema"
import { YAMLParse } from "./yaml"
import { expandTemplate } from "./expander"
import { resolveLanguageModel } from "./lm"
import { commentsCache, commentsForSource } from "./comments"

async function resolveExpansionVars(
project: Project,
Expand Down Expand Up @@ -57,6 +58,7 @@ async function resolveExpansionVars(
secrets[secret] = value
} else trace.error(`secret \`${secret}\` not found`)
}
const comments = await commentsForSource(template.id)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might have forgotten to use 'await' before calling the async function 'commentsForSource'.

generated by pr-review-commit missing_await

const res: Partial<ExpansionVariables> = {
dir: ".",
files,
Expand All @@ -67,6 +69,7 @@ async function resolveExpansionVars(
},
vars: attrs,
secrets,
comments,
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The variable res does not have a type annotation. It is a good practice to always specify types for better readability and maintainability.

generated by pr-review-commit missing_type_annotation

return res
}
Expand Down
66 changes: 66 additions & 0 deletions packages/core/src/types/prompt_template.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,67 @@ interface ChatParticipant {
options: ChatParticipantOptions
}

/**
* A comment thread
*/
interface CommentThread {
filename: string
source: string
line: number
comments: Comment[]
label?: string
resolved?: boolean
}

/**
* A comment
*/
interface Comment {
/**
* The human-readable comment body
*/
body: string

/**
* The author of the comment
*/
author: { name: string }

/**
* Optional reactions of the comment
*/
reactions?: CommentReaction[]

/**
* Optional label describing the comment
* Label will be rendered next to authorName if exists.
*/
label?: string

/**
* Optional timestamp that will be displayed in comments.
* The date will be formatted according to the user's locale and settings.
*/
timestamp?: string
}

interface CommentReaction {
/**
* The human-readable label for the reaction
*/
label: string

/**
* The number of users who have reacted to this reaction
*/
count: number

/**
* Whether the author of the comment has reacted to this reaction
*/
authorHasReacted: boolean
}

/**
* A set of text extracted from the context of the prompt execution
*/
Expand Down Expand Up @@ -647,6 +708,11 @@ interface ExpansionVariables {
* Root prompt generation context
*/
generator: ChatGenerationContext

/**
* Comment threads generated in the context of this prompt
*/
comments: CommentThread[]
}

type MakeOptional<T, P extends keyof T> = Partial<Pick<T, P>> & Omit<T, P>
Expand Down
Loading