Skip to content

Commit

Permalink
feat: include other cells as context with ai autocomplete
Browse files Browse the repository at this point in the history
  • Loading branch information
1egoman committed Oct 10, 2024
1 parent 9859780 commit b3cd5d8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 2 additions & 0 deletions packages/web/src/components/cells/code.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ export default function ControlledCodeCell(props: Props) {
);

const {
cells,
updateCell: updateCellOnClient,
clearOutput,
getTsServerDiagnostics,
Expand Down Expand Up @@ -390,6 +391,7 @@ export default function ControlledCodeCell(props: Props) {
prefix + suffix,
cell.language,
prefix.length,
cells.filter((c): c is CodeCellType => c.type === 'code' && c.id !== cell.id),
);
} catch (err) {
console.error('Error fetching ai autocomplete suggestion:', err);
Expand Down
14 changes: 13 additions & 1 deletion packages/web/src/lib/ai-autocomplete/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import protobuf from 'protobufjs';
import Long from 'long';

import { CodeCellType } from '@srcbook/shared';
import languageServerProto from './language-server-proto';

// NOTE: this EDITOR_API_KEY value was just included as a raw string in
Expand All @@ -15,6 +16,7 @@ export async function runCodeiumAiAutocomplete(
source: string,
sourceLanguage: 'javascript' | 'typescript',
cursorOffset: number,
otherCodeCells: Array<CodeCellType> = [],
): Promise<CodiumCompletionResult> {
const protos = protobuf.Root.fromJSON(languageServerProto as protobuf.INamespace);
const GetCompletionsRequest = protos.lookupType('exa.language_server_pb.GetCompletionsRequest');
Expand All @@ -28,7 +30,17 @@ export async function runCodeiumAiAutocomplete(
const apiKey = optionalApiKey ?? EDITOR_API_KEY;

const payload = {
otherDocuments: [],
otherDocuments: otherCodeCells.map((otherCodeCell) =>
DocumentInfo.create({
absolutePath: otherCodeCell.filename,
relativePath: otherCodeCell.filename,
text: otherCodeCell.source,
editorLanguage: sourceLanguage,
language: Language.getOption(sourceLanguage === 'javascript' ? 'JAVASCRIPT' : 'TYPESCRIPT'),
cursorOffset: Long.fromValue(0), // NOTE: how do I represent the cursor not being in here?
lineEnding: '\n',
}),
),
metadata: Metadata.create({
ideName: 'web',
extensionVersion: '1.0.12',
Expand Down

0 comments on commit b3cd5d8

Please sign in to comment.