Skip to content

Commit

Permalink
keep track of symmetry peers using secret (#398)
Browse files Browse the repository at this point in the history
* add note about use secret

* 3.19.4
  • Loading branch information
rjmacarthy authored Nov 15, 2024
1 parent 543008c commit 0a66431
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 18 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "twinny",
"displayName": "twinny - AI Code Completion and Chat",
"description": "Locally hosted AI code completion plugin for vscode",
"version": "3.19.3",
"version": "3.19.4",
"icon": "assets/icon.png",
"keywords": [
"code-inference",
Expand Down
55 changes: 40 additions & 15 deletions src/extension/symmetry-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import os from "os"
import path from "path"
import { EventEmitter } from "stream"
import { serverMessageKeys, SymmetryProvider } from "symmetry-core"
import { commands, ExtensionContext, Webview, workspace } from "vscode"
import * as vscode from "vscode"
import { commands, ExtensionContext, Webview, window, workspace } from "vscode"

import {
ACTIVE_CHAT_PROVIDER_STORAGE_KEY,
Expand Down Expand Up @@ -262,43 +263,67 @@ export class SymmetryService extends EventEmitter {
return path.join(homeDir, ".config", "symmetry", "provider.yaml")
}

private createOrUpdateProviderConfig(providerConfig: TwinnyProvider): void {
private createProviderConfig(provider: TwinnyProvider): Promise<void> {
const configPath = this.getSymmetryConfigPath()
const configDir = path.dirname(configPath)

if (!fs.existsSync(configDir)) {
fs.mkdirSync(configDir, { recursive: true })
}

const existingConfig = yaml.load(fs.readFileSync(configPath, "utf8")) as any

const userSecret = existingConfig?.userSecret
? existingConfig.userSecret
: crypto.randomBytes(32).toString("hex")

const symmetryConfiguration = yaml.dump({
apiHostname: providerConfig.apiHostname,
apiKey: providerConfig.apiKey,
apiPath: providerConfig.apiPath,
apiPort: providerConfig.apiPort,
apiProtocol: providerConfig.apiProtocol,
apiProvider: providerConfig.provider,
apiHostname: provider.apiHostname,
apiKey: provider.apiKey,
apiPath: provider.apiPath,
apiPort: provider.apiPort,
apiProtocol: provider.apiProtocol,
apiProvider: provider.provider,
dataCollectionEnabled: false,
maxConnections: 10,
modelName: providerConfig.modelName,
modelName: provider.modelName,
name: os.hostname(),
path: configPath,
public: true,
serverKey: this._config.symmetryServerKey,
userSecret,
systemMessage: ""
})

fs.writeFileSync(configPath, symmetryConfiguration, "utf8")
return fs.promises.writeFile(configPath, symmetryConfiguration, "utf8");
}

public async startSymmetryProvider() {
const providerConfig = this.getChatProvider()
public startSymmetryProvider = async () => {
const provider = this.getChatProvider()

if (!providerConfig) return
if (!provider) return

const configPath = this.getSymmetryConfigPath()

if (!fs.existsSync(configPath)) {
this.createOrUpdateProviderConfig(providerConfig)
await this.createProviderConfig(provider)
}

const existingConfig = yaml.load(fs.readFileSync(configPath, "utf8")) as any
const { userSecret } = existingConfig

if (!userSecret) {
vscode.window.showInformationMessage(
`
Twinny did not detect a userSecret for your provider.
Generating a \`userSecret\` for you at at ${configPath}
This only needs to be done once.
`
)
await fs.promises.writeFile(configPath, yaml.dump({
...existingConfig,
userSecret: crypto.randomBytes(32).toString("hex")
}), "utf8")
}

this._provider = new SymmetryProvider(configPath)
Expand All @@ -324,7 +349,7 @@ export class SymmetryService extends EventEmitter {
})
}

public async stopSymmetryProvider() {
public stopSymmetryProvider = async () => {
await this._provider?.destroySwarms()
updateSymmetryStatus(this._webView, "disconnected")
const sessionKey = EXTENSION_SESSION_NAME.twinnySymmetryConnectionProvider
Expand Down

0 comments on commit 0a66431

Please sign in to comment.