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

noodle: add ContextItemHistory type #4036

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 14 additions & 1 deletion lib/shared/src/codebase-context/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export enum ContextItemSource {
/**
* An item (such as a file or symbol) that is included as context in a chat message.
*/
export type ContextItem = ContextItemFile | ContextItemSymbol | ContextItemPackage
export type ContextItem = ContextItemFile | ContextItemSymbol | ContextItemPackage | ContextItemHistory

/**
* A file (or a subset of a file given by a range) that is included as context in a chat message.
Expand Down Expand Up @@ -147,6 +147,19 @@ export interface ContextItemSymbol extends ContextItemCommon {
/** The valid kinds of a symbol. */
export type SymbolKind = 'class' | 'function' | 'method'

/**
* The output of source control history for a file.
*
* Note: Currently this is only used for the "Explain History" action which runs
* on a symbol.
*/
export interface ContextItemHistory extends ContextItemCommon {
type: 'history'

/** The symbol name we asked history for, used for presentation only (not semantically meaningful). */
symbolName: string
}

/** {@link ContextItem} with the `content` field set to the content. */
export type ContextItemWithContent = ContextItem & Required<Pick<ContextItem, 'content'>>

Expand Down
1 change: 1 addition & 0 deletions lib/shared/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export type {
export {
type ContextItem,
type ContextItemFile,
type ContextItemHistory,
ContextItemSource,
type ContextItemWithContent,
type ContextItemSymbol,
Expand Down
20 changes: 10 additions & 10 deletions vscode/src/commands/context/git-log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { type SpawnOptionsWithoutStdio, spawn } from 'node:child_process'
import path from 'node:path'
import {
type ContextItem,
type ContextItemHistory,
ContextItemSource,
type FileURI,
wrapInActiveSpan,
} from '@sourcegraph/cody-shared'
import * as vscode from 'vscode'

export async function getContextFileFromGitLog(
file: FileURI,
Expand Down Expand Up @@ -45,15 +45,15 @@ export async function getContextFileFromGitLog(
throw new Error(`git log failed with exit code ${result.code}: ${result.stderr}`)
}

return [
{
type: 'file',
content: result.stdout,
title: 'Terminal Output',
uri: vscode.Uri.file('terminal-output'),
source: ContextItemSource.History,
},
]
const contextItem: ContextItemHistory = {
type: 'history',
content: result.stdout,
title: `history for ${options.funcname}`,
uri: file,
symbolName: options.funcname,
source: ContextItemSource.History,
}
return [contextItem]
})
}

Expand Down
27 changes: 14 additions & 13 deletions vscode/webviews/promptEditor/nodes/ContextItemMentionNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import styles from './ContextItemMentionNode.module.css'
import {
type ContextItem,
type ContextItemFile,
type ContextItemHistory,
type ContextItemPackage,
type ContextItemSymbol,
displayLineRange,
Expand Down Expand Up @@ -33,6 +34,7 @@ export type SerializedContextItem = { uri: string; title?: string; content?: und
| Omit<ContextItemFile, 'uri' | 'content'>
| Omit<ContextItemSymbol, 'uri' | 'content'>
| Omit<ContextItemPackage, 'uri' | 'content'>
| Omit<ContextItemHistory, 'uri' | 'content'>
)

export function serializeContextItem(
Expand Down Expand Up @@ -180,20 +182,19 @@ export function contextItemMentionNodeDisplayText(contextItem: SerializedContext
// range needs to go to the start (0th character) of line 5. Also, `RangeData` is 0-indexed but
// display ranges are 1-indexed.
const rangeText = contextItem.range?.start ? `:${displayLineRange(contextItem.range)}` : ''
if (contextItem.type === 'file') {
if (contextItem.provider && contextItem.title) {
return `@${contextItem.title}`
}
return `@${decodeURIComponent(displayPath(URI.parse(contextItem.uri)))}${rangeText}`
}
if (contextItem.type === 'symbol') {
return `@${displayPath(URI.parse(contextItem.uri))}${rangeText}#${contextItem.symbolName}`
}
if (contextItem.type === 'package') {
return `@${contextItem.ecosystem}:${contextItem.name}`
switch (contextItem.type) {
case 'file':
if (contextItem.provider && contextItem.title) {
return `@${contextItem.title}`
}
return `@${decodeURIComponent(displayPath(URI.parse(contextItem.uri)))}${rangeText}`
case 'symbol':
return `@${displayPath(URI.parse(contextItem.uri))}${rangeText}#${contextItem.symbolName}`
case 'package':
return `@${contextItem.ecosystem}:${contextItem.name}`
case 'history':
return `@history:${contextItem.symbolName}:${displayPath(URI.parse(contextItem.uri))}`
}
// @ts-ignore
throw new Error(`unrecognized context item type ${contextItem.type}`)
}

export function $createContextItemMentionNode(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ const Item: FunctionComponent<{
const isFileType = item.type === 'file'
const isPackageType = item.type === 'package'
const icon =
isFileType || isPackageType ? null : item.kind === 'class' ? 'symbol-structure' : 'symbol-method'
item.type === 'symbol' ? (item.kind === 'class' ? 'symbol-structure' : 'symbol-method') : null
const title =
item.title ?? (isFileType || isPackageType ? displayPathBasename(item.uri) : item.symbolName)

Expand Down