Skip to content

Commit

Permalink
update symmetry-core
Browse files Browse the repository at this point in the history
  • Loading branch information
rjmacarthy committed Nov 29, 2024
1 parent 2ab7093 commit 87621ca
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 39 deletions.
14 changes: 7 additions & 7 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 @@ -512,7 +512,7 @@
"rehype-raw": "^7.0.0",
"remark-gfm": "^4.0.0",
"string_score": "^0.1.22",
"symmetry-core": "^1.0.19",
"symmetry-core": "^1.0.22",
"tippy.js": "^6.3.7",
"tiptap-markdown": "^0.8.10",
"toxe": "^1.1.0",
Expand Down
111 changes: 80 additions & 31 deletions src/extension/symmetry-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import yaml from "js-yaml"
import os from "os"
import path from "path"
import { EventEmitter } from "stream"
import { serverMessageKeys, SymmetryProvider } from "symmetry-core"
import {
ProviderConfig,
serverMessageKeys,
SymmetryClient
} from "symmetry-core"
import { commands, ExtensionContext, Webview, workspace } from "vscode"

import {
Expand Down Expand Up @@ -44,7 +48,7 @@ export class SymmetryService extends EventEmitter {
private _config = workspace.getConfiguration("twinny")
private _completion = ""
private _context: ExtensionContext
private _provider: SymmetryProvider | undefined
private _client: SymmetryClient | undefined
private _providerPeer: undefined | Peer
private _providerSwarm: undefined | typeof Hyperswarm
private _providerTopic: Buffer | undefined
Expand Down Expand Up @@ -246,66 +250,111 @@ export class SymmetryService extends EventEmitter {
return path.join(homeDir, ".config", "symmetry", "provider.yaml")
}

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

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

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

return fs.promises.writeFile(configPath, symmetryConfiguration, "utf8")
}
const symmetryConfiguration = yaml.dump(config)

public startSymmetryProvider = async () => {
const provider = this.getChatProvider()
fs.promises.writeFile(configPath, symmetryConfiguration, "utf8")

if (!provider) return
return config
}

private async readProviderConfig(): Promise<ProviderConfig> {
const configPath = this.getSymmetryConfigPath()
const configStr = await fs.promises.readFile(configPath, "utf8")
return yaml.load(configStr) as ProviderConfig
}

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

if (!fs.existsSync(configPath)) {
await this.createProviderConfig(provider)
if (!fs.existsSync(configDir)) {
fs.mkdirSync(configDir, { recursive: true })
}

this._provider = new SymmetryProvider(configPath)
try {
if (fs.existsSync(configPath)) {
const backupPath = `${configPath}.backup-${Date.now()}`
await fs.promises.copyFile(configPath, backupPath)
}

const sessionKey = EXTENSION_SESSION_NAME.twinnySymmetryConnectionProvider
let config: ProviderConfig
if (fs.existsSync(configPath)) {
config = await this.readProviderConfig()
const updates: Partial<ProviderConfig> = {}
if (!config.apiChatPath) updates.apiChatPath = provider.apiPath
if (!config.dataPath) updates.dataPath = configDir

const updatedConfig = { ...config, ...updates }
await fs.promises.writeFile(configPath, yaml.dump(updatedConfig), "utf8")
} else {
config = this.createProviderConfig(this.getChatProvider() as TwinnyProvider)
await fs.promises.writeFile(configPath, yaml.dump(config), "utf8")
}
} catch (error) {
console.error("Failed to update config:", error)
throw error
}
}

this._sessionManager?.set(sessionKey, "connecting")
public startSymmetryProvider = async () => {
const provider = this.getChatProvider()
if (!provider) return

const sessionTypeName = `${EVENT_NAME.twinnySessionContext}-${sessionKey}`
try {
await this.updateProviderConfig(provider)

this._webView?.postMessage({
type: sessionTypeName,
value: "connecting"
})
this._client = new SymmetryClient(this.getSymmetryConfigPath())

await this._provider.init()
const sessionKey = EXTENSION_SESSION_NAME.twinnySymmetryConnectionProvider
this._sessionManager?.set(sessionKey, "connecting")

this._sessionManager?.set(sessionKey, "connected")
const sessionTypeName = `${EVENT_NAME.twinnySessionContext}-${sessionKey}`
this._webView?.postMessage({
type: sessionTypeName,
value: "connecting"
})

this._webView?.postMessage({
type: sessionTypeName,
value: "connected"
})
await this._client.init()

this._sessionManager?.set(sessionKey, "connected")
this._webView?.postMessage({
type: sessionTypeName,
value: "connected"
})
} catch (error) {
console.error("Failed to start provider:", error)
this._sessionManager?.set(
EXTENSION_SESSION_NAME.twinnySymmetryConnectionProvider,
"error"
)
}
}

private notifyWebView(type: string, value: any = {}) {
Expand All @@ -320,7 +369,7 @@ export class SymmetryService extends EventEmitter {
}

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

0 comments on commit 87621ca

Please sign in to comment.