Skip to content

Commit 63aacce

Browse files
committed
adding auto-generated comments
1 parent 766acc9 commit 63aacce

90 files changed

Lines changed: 2383 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

packages/core/src/agent.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,21 @@ import { prettifyMarkdown } from "./markdown"
1111
import { MarkdownTrace, TraceOptions } from "./trace"
1212
import { logVerbose } from "./util"
1313

14+
/**
15+
* Queries the memory for contextual information relevant to the given query.
16+
*
17+
* The function retrieves memories associated with the user state and processes them
18+
* to return concise answers. If no relevant information is found, it returns
19+
* a predefined token indicating no answer is available. The querying process is done
20+
* using a language model, ensuring that only memory content is utilized to formulate the response.
21+
*
22+
* @param ctx - The context object for the chat generation process.
23+
* @param query - The user's query to find relevant information in memory.
24+
* @param options - Options including user state and tracing details for the memory query.
25+
*
26+
* @returns A string containing the memory answer or undefined if the query is empty
27+
* or no memories are found.
28+
*/
1429
export async function agentQueryMemory(
1530
ctx: ChatGenerationContext,
1631
query: string,
@@ -48,6 +63,18 @@ export async function agentQueryMemory(
4863
return memoryAnswer
4964
}
5065

66+
/**
67+
* Adds a memory entry to the agent's memory cache.
68+
*
69+
* This function stores a specified answer related to a query made by the agent.
70+
* The memory is cached using a unique key combining the agent and query.
71+
* Additionally, it provides tracing details for the stored memory.
72+
*
73+
* @param agent - The identifier for the agent.
74+
* @param query - The query for which the memory is being added.
75+
* @param text - The response text to be stored in memory.
76+
* @param options - Additional options, including user state and trace options.
77+
*/
5178
export async function agentAddMemory(
5279
agent: string,
5380
query: string,
@@ -92,6 +119,16 @@ async function loadMemories(options: Pick<GenerationOptions, "userState">) {
92119
return memories
93120
}
94121

122+
/**
123+
* Traces and logs the agent's memory details.
124+
*
125+
* This function retrieves stored memories for the agent based on the provided options.
126+
* It formats and displays each memory entry in reverse order, including the agent's name,
127+
* the query, and the corresponding answer. The trace context is used to encapsulate
128+
* these details and provide structured logging of the memory.
129+
*
130+
* @param options - The context options for the current generation, which include user state and tracing options.
131+
*/
95132
export async function traceAgentMemory(
96133
options: Pick<GenerationOptions, "userState"> & Required<TraceOptions>
97134
) {

packages/core/src/annotations.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,30 @@ export function parseAnnotations(text: string): Diagnostic[] {
8686
return Array.from(annotations.values()) // Convert the set to an array
8787
}
8888

89+
/**
90+
* Removes annotations from the input text.
91+
*
92+
* This function utilizes regular expressions defined for TypeScript, GitHub Actions, and Azure DevOps annotations
93+
* to replace occurrences of annotations with an empty string, effectively erasing them from the provided text.
94+
*
95+
* @param text - The input text containing annotations to be erased.
96+
* @returns The input text with all annotations removed.
97+
*/
8998
export function eraseAnnotations(text: string) {
9099
return ANNOTATIONS_RX.reduce((t, rx) => t.replace(rx, ""), text)
91100
}
92101

102+
/**
103+
* Transforms text annotations into diagnostic items.
104+
*
105+
* This function utilizes annotated regex patterns to identify and extract
106+
* diagnostic information from the input text. It constructs `Diagnostic`
107+
* objects from matched groups, which are then converted into a string
108+
* representation.
109+
*
110+
* @param text - The input text containing annotations to be converted.
111+
* @returns A string of formatted diagnostic items derived from the annotations.
112+
*/
93113
export function convertAnnotationsToItems(text: string) {
94114
return ANNOTATIONS_RX.reduce(
95115
(t, rx) =>
@@ -112,6 +132,15 @@ export function convertAnnotationsToItems(text: string) {
112132
)
113133
}
114134

135+
/**
136+
* Converts a `Diagnostic` object into a formatted string representation.
137+
*
138+
* The formatted string includes an emoji corresponding to the severity level,
139+
* the diagnostic message, and the filename with an optional line number link.
140+
*
141+
* @param d - The `Diagnostic` object to convert.
142+
* @returns A formatted string representing the annotation.
143+
*/
115144
export function convertAnnotationToItem(d: Diagnostic) {
116145
const { severity, message, filename, code, range } = d
117146
const line = range?.[0]?.[0]

packages/core/src/assert.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
/**
2+
* """
3+
* Asserts that a given condition is true. If the condition is false, logs an error message and throws an exception. Optionally supports additional debug data for logging.
4+
*
5+
* Parameters:
6+
* - cond: A boolean condition to evaluate.
7+
* - msg: An optional message to display if the assertion fails.
8+
* - debugData: Optional extra data for debugging output when the assertion fails.
9+
*
10+
* Throws:
11+
* - Error with the provided message if the condition is false.
12+
* """
13+
*/
114
export function assert(
215
cond: boolean,
316
msg = "Assertion failed",

packages/core/src/ast.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,17 @@ export interface ScriptFilterOptions {
9494
unlisted?: boolean
9595
}
9696

97+
/**
98+
* Filters an array of scripts based on specified options.
99+
*
100+
* This function allows the application of multiple filters to the given scripts
101+
* such as filtering by IDs, groups, and flags for test, redteam, and unlisted statuses.
102+
*
103+
* @param scripts - The array of scripts to be filtered.
104+
* @param options - The filtering options that determine which scripts to include in the result.
105+
*
106+
* @returns An array of scripts that match the specified filtering criteria.
107+
*/
97108
export function filterScripts(
98109
scripts: PromptScript[],
99110
options: ScriptFilterOptions

packages/core/src/astgrep.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,19 @@ import { host } from "./host"
88
import { uniq } from "es-toolkit"
99
import { readText, writeText } from "./fs"
1010

11+
/**
12+
* Searches for files matching a specified glob pattern and applies a matcher to find nodes within those files.
13+
*
14+
* @param lang - The programming language to use for parsing files.
15+
* @param glob - A pattern or array of patterns to match files.
16+
* @param matcher - A string or matcher object used to identify nodes in the files.
17+
* @param options - Options to configure the search process, including cancellation options.
18+
*
19+
* @returns An object containing the number of files scanned, matched nodes, a replace function to modify nodes,
20+
* and a commitEdits function to apply changes to the files.
21+
*
22+
* @throws Error if glob or matcher is not provided.
23+
*/
1124
export async function astGrepFindFiles(
1225
lang: SgLang,
1326
glob: ElementOrArray<string>,
@@ -105,6 +118,14 @@ export async function astGrepFindFiles(
105118
return { files: scanned, matches, replace, commitEdits }
106119
}
107120

121+
/**
122+
* Writes edits to the roots of the provided nodes. For each unique root,
123+
* it checks if the current content differs from the updated content. If they
124+
* are different, it writes the updated content to the corresponding file.
125+
*
126+
* @param nodes - An array of nodes whose edits need to be written.
127+
* @param options - Optional settings that may include a cancellation token.
128+
*/
108129
export async function astGrepWriteRootEdits(
109130
nodes: SgNode[],
110131
options?: CancellationOptions
@@ -127,6 +148,20 @@ export async function astGrepWriteRootEdits(
127148
}
128149
}
129150

151+
/**
152+
* Parses the content of a given workspace file and resolves its language.
153+
*
154+
* This function checks for binary encoding, retrieves the file content,
155+
* and utilizes the AST-Grep library to parse the content based on the
156+
* specified or inferred language. It supports cancellation tokens to
157+
* allow for interruption of long-running processes.
158+
*
159+
* @param file The workspace file to be parsed.
160+
* @param options Optional parameters including language specification
161+
* and cancellation options.
162+
* @returns The root AST node of the parsed file content, or undefined
163+
* if the file is binary or if the language cannot be resolved.
164+
*/
130165
export async function astGrepParse(
131166
file: WorkspaceFile,
132167
options?: { lang?: SgLang } & CancellationOptions

packages/core/src/azurecontentsafety.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,12 @@ class AzureContentSafetyClient implements ContentSafety {
239239
}
240240
}
241241

242+
/**
243+
* Checks if the Azure Content Safety client is configured correctly.
244+
* This is determined by the presence of the necessary endpoint environment variable.
245+
*
246+
* @returns A boolean indicating whether the Azure Content Safety client is configured.
247+
*/
242248
export function isAzureContentSafetyClientConfigured() {
243249
const endpoint = trimTrailingSlash(
244250
process.env.AZURE_CONTENT_SAFETY_ENDPOINT ||
@@ -247,6 +253,12 @@ export function isAzureContentSafetyClientConfigured() {
247253
return !!endpoint
248254
}
249255

256+
/**
257+
* Creates an instance of the Azure Content Safety client.
258+
*
259+
* @param options Configuration options including cancellation options and tracing options.
260+
* @returns An object implementing the ContentSafety interface with methods to detect harmful content and prompt injection.
261+
*/
250262
export function createAzureContentSafetyClient(
251263
options: CancellationOptions &
252264
TraceOptions & {

packages/core/src/azuredevops.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,22 @@ async function findPullRequest(info: AzureDevOpsEnv) {
7878
return pr
7979
}
8080

81+
/**
82+
* Updates the description of an existing pull request in Azure DevOps.
83+
*
84+
* This function retrieves the pull request associated with the provided
85+
* Azure DevOps environment information, formats the given text, and appends
86+
* it to the existing description. A footer generated by the script is also added.
87+
*
88+
* If the pull request is found, the function sends a PATCH request to update
89+
* the description. Error logging is performed if the update fails.
90+
*
91+
* @param script The script object used to generate the footer for the description.
92+
* @param info The Azure DevOps environment information containing access and
93+
* repository details.
94+
* @param text The new text to be added to the pull request description.
95+
* @param commentTag A tag used for merging the description.
96+
*/
8197
export async function azureDevOpsUpdatePullRequestDescription(
8298
script: PromptScript,
8399
info: AzureDevOpsEnv,
@@ -116,6 +132,18 @@ export async function azureDevOpsUpdatePullRequestDescription(
116132
else logVerbose(`pull request updated`)
117133
}
118134

135+
/**
136+
* Creates a comment on an Azure DevOps pull request.
137+
*
138+
* This function retrieves the pull request ID based on the provided environment information,
139+
* assembles the comment body, and submits it as a new comment thread. If a commentTag is provided,
140+
* it checks for existing active threads with the same tag and closes them before creating a new comment.
141+
*
142+
* @param script - The script that initiated the call.
143+
* @param info - Environment information needed for Azure DevOps API calls.
144+
* @param body - The content of the comment to be added.
145+
* @param commentTag - An optional tag to identify and close old comment threads.
146+
*/
119147
export async function azureDevOpsCreateIssueComment(
120148
script: PromptScript,
121149
info: AzureDevOpsEnv,

packages/core/src/azuretoken.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,14 @@ class AzureTokenResolverImpl implements AzureTokenResolver {
169169
}
170170
}
171171

172+
/**
173+
* Creates an instance of AzureTokenResolver.
174+
*
175+
* @param name - The name identifier for the token resolver.
176+
* @param envName - The environment name from which to read the secret.
177+
* @param scopes - An array of scopes required for the Azure authentication token.
178+
* @returns An instance of AzureTokenResolver, which is responsible for obtaining and managing Azure authentication tokens.
179+
*/
172180
export function createAzureTokenResolver(
173181
name: string,
174182
envName: string,

packages/core/src/bufferlike.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,18 @@ import { resolveFileBytes } from "./file"
22
import { TraceOptions } from "./trace"
33
import { fileTypeFromBuffer } from "./filetype"
44

5+
/**
6+
* Resolves a buffer-like object into a Buffer.
7+
* Supports various input types including string URLs, Blob, ReadableStream,
8+
* ArrayBuffer, Uint8Array, and objects containing a filename.
9+
*
10+
* @param bufferLike - The buffer-like object to resolve.
11+
* @param options - Optional tracing options for resolution.
12+
*
13+
* @returns A Promise that resolves to a Buffer.
14+
*
15+
* @throws Error if the provided buffer-like object is unsupported.
16+
*/
517
export async function resolveBufferLike(
618
bufferLike: BufferLike,
719
options?: TraceOptions
@@ -28,6 +40,14 @@ export async function resolveBufferLike(
2840
throw new Error("Unsupported buffer-like object")
2941
}
3042

43+
/**
44+
* Converts a Buffer or Uint8Array to a Blob, optionally specifying the MIME type.
45+
* The MIME type is inferred from the buffer if not provided.
46+
*
47+
* @param buffer - The data to be converted.
48+
* @param mime - Optional MIME type for the Blob. If not provided, detected from the buffer.
49+
* @returns A Blob containing the data from the buffer.
50+
*/
3151
export async function BufferToBlob(buffer: Buffer | Uint8Array, mime?: string) {
3252
const type = await fileTypeFromBuffer(buffer)
3353
return new Blob([buffer], {

0 commit comments

Comments
 (0)