Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev Data Destinations #3853

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions core/autocomplete/util/AutocompleteLoggingService.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { logDevData } from "../../util/devdata";
import { DataLogger } from "../../data/log";
import { COUNT_COMPLETION_REJECTED_AFTER } from "../../util/parameters";
import { Telemetry } from "../../util/posthog";
import { getUriFileExtension } from "../../util/uri";
Expand Down Expand Up @@ -96,7 +96,11 @@ export class AutocompleteLoggingService {
}

private logAutocompleteOutcome(outcome: AutocompleteOutcome) {
logDevData("autocomplete", outcome);
void DataLogger.getInstance().logDevData({
name: "autocomplete",
data: outcome,
});

const { prompt, completion, prefix, suffix, ...restOfOutcome } = outcome;
void Telemetry.capture(
"autocomplete",
Expand Down
2 changes: 2 additions & 0 deletions core/config/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,13 @@ export const defaultConfig: SerializedContinueConfig = {
tabAutocompleteModel: DEFAULT_AUTOCOMPLETE_MODEL_CONFIG,
contextProviders: defaultContextProvidersVsCode,
slashCommands: defaultSlashCommandsVscode,
data: [],
};

export const defaultConfigJetBrains: SerializedContinueConfig = {
models: [DEFAULT_CHAT_MODEL_CONFIG],
tabAutocompleteModel: DEFAULT_AUTOCOMPLETE_MODEL_CONFIG,
contextProviders: defaultContextProvidersJetBrains,
slashCommands: defaultSlashCommandsJetBrains,
data: [],
};
1 change: 1 addition & 0 deletions core/config/load.ts
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,7 @@ async function finalToBrowserConfig(
docs: final.docs,
tools: final.tools,
usePlatform: await useHub(ide.getIdeSettings()),
// data not included here
};
}

Expand Down
3 changes: 1 addition & 2 deletions core/config/profile/PlatformProfileLoader.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { AssistantUnrolled } from "@continuedev/config-yaml/dist/schemas/index.js";
import { AssistantUnrolled, ConfigResult } from "@continuedev/config-yaml";

import { ControlPlaneClient } from "../../control-plane/client.js";
import { ContinueConfig, IDE, IdeSettings } from "../../index.js";

import { ConfigResult } from "@continuedev/config-yaml";
import { ProfileDescription } from "../ProfileLifecycleManager.js";
import doLoadConfig from "./doLoadConfig.js";
import { IProfileLoader } from "./IProfileLoader.js";
Expand Down
3 changes: 1 addition & 2 deletions core/config/yaml/convertFromJson.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { ModelConfig } from "@continuedev/config-yaml";
import { AssistantUnrolled } from "@continuedev/config-yaml/dist/schemas";
import { ModelConfig, AssistantUnrolled } from "@continuedev/config-yaml";

import { SerializedContinueConfig } from "../..";

Expand Down
2 changes: 1 addition & 1 deletion core/config/yaml/default.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AssistantUnrolled } from "@continuedev/config-yaml/dist/schemas";
import { AssistantUnrolled } from "@continuedev/config-yaml";

// TODO
export const defaultConfigYaml: AssistantUnrolled = {
Expand Down
1 change: 1 addition & 0 deletions core/config/yaml/loadYaml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ async function configYamlToContinueConfig(
faviconUrl: doc.faviconUrl,
})),
contextProviders: [],
data: config.data,
};

// Models
Expand Down
16 changes: 10 additions & 6 deletions core/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ import { createNewPromptFileV2 } from "./promptFiles/v2/createNewPromptFile";
import { callTool } from "./tools/callTool";
import { ChatDescriber } from "./util/chatDescriber";
import { clipboardCache } from "./util/clipboardCache";
import { logDevData } from "./util/devdata";
import { DevDataSqliteDb } from "./util/devdataSqlite";
import { DataLogger } from "./data/log";
import { DevDataSqliteDb } from "./data/devdataSqlite";
import { GlobalContext } from "./util/GlobalContext";
import historyManager from "./util/history";
import {
editConfigJson,
getConfigJsonPath,
setupInitialDotContinueDirectory,
migrateV1DevDataFiles,
} from "./util/paths";
import { localPathToUri } from "./util/pathToUri";
import { Telemetry } from "./util/posthog";
Expand Down Expand Up @@ -100,7 +100,7 @@ export class Core {
private readonly onWrite: (text: string) => Promise<void> = async () => {},
) {
// Ensure .continue directory is created
setupInitialDotContinueDirectory();
migrateV1DevDataFiles();

this.codebaseIndexingState = {
status: "loading",
Expand Down Expand Up @@ -144,6 +144,10 @@ export class Core {
this.messenger.send("didChangeAvailableProfiles", { profiles }),
);

// Dev Data Logger
const dataLogger = DataLogger.getInstance();
dataLogger.core = this;

// Codebase Indexer and ContinueServerClient depend on IdeSettings
let codebaseIndexerResolve: (_: any) => void | undefined;
this.codebaseIndexerPromise = new Promise(
Expand Down Expand Up @@ -253,8 +257,8 @@ export class Core {
});

// Dev data
on("devdata/log", (msg) => {
logDevData(msg.data.tableName, msg.data.data);
on("devdata/log", async (msg) => {
void DataLogger.getInstance().logDevData(msg.data);
});

// Edit config
Expand Down
3 changes: 2 additions & 1 deletion core/util/devdataSqlite.ts → core/data/devdataSqlite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import sqlite3 from "sqlite3";

import { DatabaseConnection } from "../indexing/refreshIndex.js";

import { getDevDataSqlitePath } from "./paths.js";
import { getDevDataSqlitePath } from "../util/paths.js";

/* The Dev Data SQLITE table is only used for local tokens generated */
export class DevDataSqliteDb {
static db: DatabaseConnection | null = null;

Expand Down
144 changes: 144 additions & 0 deletions core/data/log.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
import fs from "fs";
import path from "path";

import { getDevDataFilePath } from "../util/paths.js";
import { Core } from "../core.js";
import {
allDevEventNames,
DataLogLevel,
DevDataLogEvent,
devDataVersionedSchemas,
} from "@continuedev/config-yaml";
import { fetchwithRequestOptions } from "@continuedev/fetch";
import { joinPathsToUri } from "../util/uri.js";
import * as URI from "uri-js";
import { fileURLToPath } from "url";

const DEFAULT_DEV_DATA_LEVEL: DataLogLevel = "all";
export const LOCAL_DEV_DATA_VERSION = "0.2.0";
export class DataLogger {
private static instance: DataLogger | null = null;
core?: Core;

private constructor() {}

public static getInstance(): DataLogger {
if (DataLogger.instance === null) {
DataLogger.instance = new DataLogger();
}
return DataLogger.instance;
}

async logDevData(event: DevDataLogEvent) {
// Local logs (always on for all levels)
const filepath: string = getDevDataFilePath(
event.name,
LOCAL_DEV_DATA_VERSION,
);
const jsonLine = JSON.stringify(event.data);
fs.writeFileSync(filepath, `${jsonLine}\n`, { flag: "a" });

// Remote logs
const config = (await this.core?.configHandler.loadConfig())?.config;
if (config?.data?.length) {
await Promise.allSettled(
config.data.map(async (dataConfig) => {
try {
// First extract the data schema based on the version and level
const { schemaVersion } = dataConfig;

const level = dataConfig.level ?? DEFAULT_DEV_DATA_LEVEL;

// Skip event if `events` is specified and does not include the event
const events = dataConfig.events ?? allDevEventNames;
if (!events.includes(event.name)) {
return;
}

const versionSchemas = devDataVersionedSchemas[schemaVersion];
if (!versionSchemas) {
throw new Error(
`Attempting to log dev data to non-existent version ${schemaVersion}`,
);
}

const levelSchemas = versionSchemas[level];
if (!levelSchemas) {
throw new Error(
`Attempting to log dev data at level ${level} for version ${schemaVersion} which does not exist`,
);
}

const schema = levelSchemas[event.name];
if (!schema) {
throw new Error(
`Attempting to log dev data for event ${event.name} at level ${level} for version ${schemaVersion}: no schema found`,
);
}

const parsed = schema.safeParse(event.data);
if (!parsed.success) {
throw new Error(
`Failed to parse event data ${parsed.error.toString()}`,
);
}

const uriComponents = URI.parse(dataConfig.destination);

// Send to remote server
if (
uriComponents.scheme === "https" ||
uriComponents.scheme === "http"
) {
const headers: Record<string, string> = {
"Content-Type": "application/json",
};
if (dataConfig.apiKey) {
headers["Authorization"] = `Bearer ${dataConfig.apiKey}`;
}
const response = await fetchwithRequestOptions(
dataConfig.destination,
{
method: "POST",
headers,
body: JSON.stringify({
name: event.name,
data: parsed.data,
schemaVersion,
level,
}),
},
dataConfig.requestOptions,
);
if (!response.ok) {
throw new Error(
`Post request failed. ${response.status}: ${response.statusText}`,
);
}
} else if (uriComponents.scheme === "file") {
// Write to jsonc file for local file URIs
const dirUri = joinPathsToUri(
dataConfig.destination,
schemaVersion,
);
const dirPath = fileURLToPath(dirUri);

if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath, { recursive: true });
}
const filepath = path.join(dirPath, `${event.name}.jsonl`);
const jsonLine = JSON.stringify(event.data);
fs.writeFileSync(filepath, `${jsonLine}\n`, { flag: "a" });
} else {
throw new Error(`Unsupported scheme ${uriComponents.scheme}`);
}
} catch (error) {
console.error(
`Error logging data to ${dataConfig.destination}: ${error instanceof Error ? error.message : error}`,
);
}
}),
);
}
}
}
5 changes: 4 additions & 1 deletion core/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Parser from "web-tree-sitter";
import { GetGhTokenArgs } from "./protocol/ide";

import { DataDestination } from "@continuedev/config-yaml";
declare global {
interface Window {
ide?: "vscode";
Expand Down Expand Up @@ -1134,6 +1134,7 @@ export interface SerializedContinueConfig {
experimental?: ExperimentalConfig;
analytics?: AnalyticsConfig;
docs?: SiteIndexingConfig[];
data?: DataDestination[];
}

export type ConfigMergeType = "merge" | "overwrite";
Expand Down Expand Up @@ -1186,6 +1187,7 @@ export interface Config {
experimental?: ExperimentalConfig;
/** Analytics configuration */
analytics?: AnalyticsConfig;
data?: DataDestination[];
}

// in the actual Continue source code
Expand All @@ -1209,6 +1211,7 @@ export interface ContinueConfig {
analytics?: AnalyticsConfig;
docs?: SiteIndexingConfig[];
tools: Tool[];
data?: DataDestination[];
}

export interface BrowserSerializedContinueConfig {
Expand Down
17 changes: 10 additions & 7 deletions core/llm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import {
RequestOptions,
TemplateType,
} from "../index.js";
import { logDevData } from "../util/devdata.js";
import { DevDataSqliteDb } from "../util/devdataSqlite.js";
import { DataLogger } from "../data/log.js";
import { DevDataSqliteDb } from "../data/devdataSqlite.js";
import mergeJson from "../util/merge.js";
import { renderChatMessage } from "../util/messageContent.js";
import { isOllamaInstalled } from "../util/ollamaHelper.js";
Expand Down Expand Up @@ -339,11 +339,14 @@ export abstract class BaseLLM implements ILLM {
generatedTokens,
);

logDevData("tokens_generated", {
model: model,
provider: this.providerName,
promptTokens: promptTokens,
generatedTokens: generatedTokens,
void DataLogger.getInstance().logDevData({
name: "tokensGenerated",
data: {
model: model,
provider: this.providerName,
promptTokens: promptTokens,
generatedTokens: generatedTokens,
},
});
}

Expand Down
Loading
Loading