forked from stackwiseai/stackwise
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
126 lines (110 loc) · 3.94 KB
/
index.ts
File metadata and controls
126 lines (110 loc) · 3.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import * as vscode from 'vscode';
import buildStack from '../stack/buildStack';
import createStackFile from '../stack/createStackFile';
import replaceStackSnippetWithInjectFunction from '../collapseStack';
import addImportStatement from '../addImportStatement';
import chooseBoilerplate from '../stack/integrations/generic/chooseBoilerplate';
import generateFunction from '../stack/integrations/generic/generateFunction';
import createBoilerplateEmbedding from '../stack/createEmbedding';
import { BoilerplateMetadata } from '../stack/integrations/lib/types';
import { incrementEmbeddingCount } from '../stack/integrations/lib/incrementEmbeddingCount';
import stackRegistry from '../stack/registry';
import { combineSkeleton } from '../stack/createSkeleton';
export default async function buildOrUpdateStack(
stackSnippet,
stackPosition,
inputJSON,
outputJSON,
document: vscode.TextDocument
) {
const message = vscode.window.showInformationMessage(
`Building your stack...`
);
const {
brief,
functionId,
functionExists,
methodName,
inputString,
briefSkeleton,
functionAndOutputSkeleton,
inputValues,
} = await buildStack({
inputJSON: inputJSON,
outputJSON: outputJSON,
stackSnippet: stackSnippet,
});
let integrationType = 'generic';
console.log(`functionId in buildOrUpdateStack:`, functionId);
if (!functionExists) {
console.log(`function does not exist yet`);
const { nearestBoilerplate, integration, exactMatch } =
await chooseBoilerplate(
briefSkeleton,
functionAndOutputSkeleton,
functionId
);
integrationType = integration;
console.log(
`nearestBoilerplate in buildOrUpdateStack:`,
nearestBoilerplate
);
console.log(`integration in buildOrUpdateStack:`, integration);
console.log(`exactMatch in buildOrUpdateStack:`, exactMatch);
if (exactMatch) {
console.log(`exactMatch of functionId ${functionId}`);
// if it's an exact match it means that's it's a single BoilerplateMetadata (hash directly matched or >0.98 similarity)
const boilerplate = nearestBoilerplate as BoilerplateMetadata;
// this replaces the previous function name with the new one
const existingFunction = boilerplate.functionString.replace(
new RegExp(methodName, 'g'),
boilerplate.methodName
);
createStackFile(existingFunction, methodName, integration);
// increment count of times it's been used
await incrementEmbeddingCount(boilerplate.functionId);
} else {
console.log(`NO exact match for the functionId ${functionId}`);
// generate a function from the default values
let generatedFunction = await generateFunction(
briefSkeleton,
functionAndOutputSkeleton,
brief,
nearestBoilerplate,
integration
);
generatedFunction = combineSkeleton(briefSkeleton, generatedFunction);
console.log(
`generatedFunction in buildOrUpdateStack:`,
generatedFunction
);
// save it as an embedding
await createBoilerplateEmbedding(
brief,
inputJSON,
outputJSON,
integration,
functionId,
generatedFunction,
methodName,
briefSkeleton,
functionAndOutputSkeleton
);
createStackFile(generatedFunction, methodName, integration);
}
stackRegistry.register(methodName, functionId);
} else {
console.log(`function already exists`);
}
console.log(`inputString in buildOrUpdateStack:`, inputString);
const injectedFunction = `await ${methodName}(${inputValues})`;
console.log(`stackSnippet in buildOrUpdateStack:`, stackSnippet);
// console.log(`document in buildOrUpdateStack:`, document);
console.log(`injectedFunction in buildOrUpdateStack:`, injectedFunction);
replaceStackSnippetWithInjectFunction(
stackSnippet,
document,
injectedFunction
);
addImportStatement(methodName, document, integrationType);
}