Skip to content

Commit

Permalink
Fix update checker
Browse files Browse the repository at this point in the history
Add basic anon usage tracking
  • Loading branch information
ColinMcNeil committed Sep 25, 2024
1 parent cdf7ae2 commit 66fdcc3
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 12 deletions.
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": "labs-ai-tools-vscode",
"displayName": "Labs: AI Tools for VSCode",
"description": "Run & Debug AI Prompts with Dockerized tools",
"version": "0.1.2",
"version": "0.1.3",
"publisher": "docker",
"repository": {
"type": "git",
Expand Down
15 changes: 10 additions & 5 deletions src/commands/runPrompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { spawnPromptImage, writeKeyToVolume } from "../utils/promptRunner";
import { verifyHasOpenAIKey } from "./setOpenAIKey";
import { getCredential } from "../utils/credential";
import { setProjectDir } from "./setProjectDir";
import { postToBackendSocket } from "../utils/ddSocket";
import { ctx } from "../extension";

type PromptOption = 'local-dir' | 'local-file' | 'remote';

Expand All @@ -18,6 +20,7 @@ const START_DOCKER_COMMAND = {
};

const checkDockerDesktop = () => {

// Coerce the error to have an exit code
type DockerSpawnError = Error & { code: number };

Expand Down Expand Up @@ -82,9 +85,7 @@ const getWorkspaceFolder = async () => {


export const runPrompt: (secrets: vscode.SecretStorage, mode: PromptOption) => void = (secrets: vscode.SecretStorage, mode: PromptOption) => vscode.window.withProgress({ location: vscode.ProgressLocation.Window, cancellable: true }, async (progress, token) => {



postToBackendSocket({ event: 'eventLabsPromptRunPrepare', properties: { mode } });
const result = await checkDockerDesktop();
if (result === 'RETRY') {
return runPrompt(secrets, mode);
Expand Down Expand Up @@ -113,6 +114,8 @@ export const runPrompt: (secrets: vscode.SecretStorage, mode: PromptOption) => v
}
const runningLocal = promptOption.id.startsWith('local://');

postToBackendSocket({ event: 'eventLabsPromptRunStart', properties: { mode, ref: runningLocal ? 'local' : promptOption.id } });

if (!runningLocal && !workspaceFolder) {
return vscode.window.showErrorMessage("No workspace selected. Either open a workspace or run a local prompt.", "Open workspace", "Run local prompt").then((value) => {
if (!value) {
Expand Down Expand Up @@ -140,6 +143,7 @@ export const runPrompt: (secrets: vscode.SecretStorage, mode: PromptOption) => v
const { editor, doc } = await createOutputBuffer();

if (!editor || !doc) {
postToBackendSocket({ event: 'eventLabsPromptError', properties: { error: 'No editor or document found' } });
return;
}

Expand Down Expand Up @@ -204,11 +208,12 @@ export const runPrompt: (secrets: vscode.SecretStorage, mode: PromptOption) => v
else {
await writeToEditor(JSON.stringify(json, null, 2));
}
},token);
}, token);
} catch (e: unknown) {
e = e as Error;
void vscode.window.showErrorMessage("Error running prompt");
await writeToEditor('```json\n' + (e as Error).toString() + '\n```');
postToBackendSocket({ event: 'eventLabsPromptError', properties: { error: (e as Error).toString() } });
return;
}
postToBackendSocket({ event: 'eventLabsPromptFinished', properties: { mode } });
});
8 changes: 2 additions & 6 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import * as vscode from 'vscode';
import { runPrompt } from './commands/runPrompt';

import { runHotCommand } from './commands/runHotCommand';
import { setOpenAIKey } from './commands/setOpenAIKey';
import { nativeClient } from './utils/lsp';
import { deletePrompt, savePrompt } from './commands/manageSavedPrompts';
import { spawnSync } from 'child_process';
import semver from 'semver';
import commands from './commands';
Expand All @@ -26,12 +22,12 @@ export const packageJSON = vscode.extensions.getExtension(extensionId)?.packageJ

const getLatestVersion = async () => {
const resp = (await fetch(
"https://api.github.com/repos/docker/labs-make-runbook/releases/latest"
"https://api.github.com/repos/docker/labs-ai-tools-vscode/releases/latest"
)
.then((r) => r.json())
.catch(() => null)) as { name: string } | null;

const version = resp?.name?.split("v")[1]?.split(" ")[0];
const version = resp?.name
return version;
};

Expand Down
93 changes: 93 additions & 0 deletions src/utils/ddSocket.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import os from 'os';
import path from 'path';
import http from 'http';
import { ctx } from '../extension';
const getDevhomePrefix = () => {
return process.env['DEVHOME'] ? path.basename(process.env['DEVHOME']) : '';
}

const getDevhome = (): string => {
return process.env['DEVHOME'] ?? os.homedir();
}
const getUserDataDirectory = (
/** This distinction is only currently meaningful on Windows */
type: 'local' | 'roaming' = 'local',
): string => {
const devhome = getDevhome();
if (os.platform() === 'win32') {
return path.join(
devhome,
'AppData',
type === 'local' ? 'Local' : 'Roaming',
'Docker',
);
}
if (os.platform() === 'linux') {
return path.join(devhome, '.docker', 'desktop');
}
if (os.platform() === 'darwin') {
return path.join(
devhome,
'Library',
'Containers',
'com.docker.docker',
'Data',
);
}

throw new Error('Unrecognized platform');
}

export function getBackendSocketByPlatform(): string {
switch (os.platform()) {
case 'darwin':
return `${getDevhome()}/Library/Containers/com.docker.docker/Data/backend.sock`;
case 'win32':
return '\\\\.\\pipe\\dockerBackendApiServer';
default:
return `${getDevhome()}/.docker/desktop/backend.sock`;
}
}

type TrackEvent = {
event: string;
properties?: Record<string, string>
}

const defaultProperties = {
version: ctx.extension.packageJSON.version,
platform: os.platform(),
arch: os.arch(),
nodeVersion: process.version,
vscodeVersion: ctx.extension.packageJSON.version,
}

export const postToBackendSocket = (event: TrackEvent) => {
event.properties = { ...event.properties, ...defaultProperties }
const postData = JSON.stringify(event);
const options = {
path: '/analytics/track',
method: 'POST',
socketPath: getBackendSocketByPlatform(),
headers: {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(postData)
}
};

const req = http.request(options, (res) => {
res.setEncoding('utf8');
res.on('error', (e) => {
throw new Error(`problem with response: ${e.message}`);
});

});

req.on('error', (e) => {
throw new Error(`problem with request: ${e.message}`);
});

// Write data to request body
req.write(postData);
req.end();
}
3 changes: 3 additions & 0 deletions src/utils/promptPicker.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { QuickPickItem, QuickPickItemKind, ThemeIcon, commands, window, workspace } from "vscode";
import { ctx } from "../extension";
import { postToBackendSocket } from "./ddSocket";

// https://github.com/owner/repo/tree/ref/path
// https://github.com/docker/labs-ai-tools-vscode/tree/main/prompts/docker
Expand Down Expand Up @@ -163,10 +164,12 @@ export const showPromptPicker = () =>
if (item.id) {
if (item.saved) {
await commands.executeCommand("docker.labs-ai-tools-vscode.delete-prompt", item.id);
postToBackendSocket({ event: 'eventLabsPromptDelete', properties: { ref: item.id } });
quickPick.items = getDefaultItems();
}
else {
await commands.executeCommand("docker.labs-ai-tools-vscode.save-prompt", item.id);
postToBackendSocket({ event: 'eventLabsPromptSave', properties: { ref: item.id } });
quickPick.items = getDefaultItems();
}

Expand Down

0 comments on commit 66fdcc3

Please sign in to comment.