Skip to content

Commit b3cd5d8

Browse files
committed
feat: include other cells as context with ai autocomplete
1 parent 9859780 commit b3cd5d8

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

packages/web/src/components/cells/code.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ export default function ControlledCodeCell(props: Props) {
167167
);
168168

169169
const {
170+
cells,
170171
updateCell: updateCellOnClient,
171172
clearOutput,
172173
getTsServerDiagnostics,
@@ -390,6 +391,7 @@ export default function ControlledCodeCell(props: Props) {
390391
prefix + suffix,
391392
cell.language,
392393
prefix.length,
394+
cells.filter((c): c is CodeCellType => c.type === 'code' && c.id !== cell.id),
393395
);
394396
} catch (err) {
395397
console.error('Error fetching ai autocomplete suggestion:', err);

packages/web/src/lib/ai-autocomplete/index.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import protobuf from 'protobufjs';
22
import Long from 'long';
33

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

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

3032
const payload = {
31-
otherDocuments: [],
33+
otherDocuments: otherCodeCells.map((otherCodeCell) =>
34+
DocumentInfo.create({
35+
absolutePath: otherCodeCell.filename,
36+
relativePath: otherCodeCell.filename,
37+
text: otherCodeCell.source,
38+
editorLanguage: sourceLanguage,
39+
language: Language.getOption(sourceLanguage === 'javascript' ? 'JAVASCRIPT' : 'TYPESCRIPT'),
40+
cursorOffset: Long.fromValue(0), // NOTE: how do I represent the cursor not being in here?
41+
lineEnding: '\n',
42+
}),
43+
),
3244
metadata: Metadata.create({
3345
ideName: 'web',
3446
extensionVersion: '1.0.12',

0 commit comments

Comments
 (0)