continue-custom/core/util/devdataSqlite.ts
Nate Sesti ca884b8212
fix ollama onboarding (#1539)
* allow setting apiBase for bedrock provider

* ⚡️ better linking

* ✨ new prompt file action

* ⚡️ strip <COMPLETION> from GPT-4 completion

* continue proxy FIM support

* warn user if potentially dangerous command is generated

* tab autocomplete docs

* docs: update PR template (#1531)

* chore: add docs to install script (#1533)

* fix(extensions): schema for db ctx provider (#1534)

* fix(extensions): schema for db ctx provider

* fix: invalid enum

* prettierignore

* docs: add docs and schema for "OS" provider (#1536)

* tests (#1501)

* 👷 CI for jetbrains

* default working dir

* changelog

* build binaries

* binary testing setup

* idesettings

* core binary testing

* run binary tests in ci

* remove unused targets

* needs build

* console.log bin contents

* fix ci

* fix win32 binary download

* test

* no linux arm64

* macos latest

* macos-12

* binary permissions

* upload logs

* fix

* upload full folder as binary artifact

* test

* test macos only

* set full execute permissions

* copy sqlite binary

* cd

* it worked!

* build again in test job

* debug

* remove timeout

* info

* log

* log2

* more logs

* catch

* fewer logs

* test all platforms

* test downloaded artifact

* needs build

* updates

* build

* false

* release

* add tag and upload binaryes

* change tag name

* proper artifact upload

* jest updates

* ✅ generate a few unit tests with Continue

* fix imports related to IdeSettings

* run tsc on PRs

* remove shareSession command (unused)

* update release process

* update plugin version

* don't show ghost text when jetbrains completion visible

* run jetbrains ci in main

* check ts in dev

* ignore .env

* 🚑 fix constant warnings when onboarding with Ollama

---------

Co-authored-by: Patrick Erichsen <patrick.a.erichsen@gmail.com>
2024-06-21 11:09:03 -07:00

86 lines
2.7 KiB
TypeScript

import fs from "fs";
import { open } from "sqlite";
import sqlite3 from "sqlite3";
import { DatabaseConnection } from "../indexing/refreshIndex.js";
import { getDevDataSqlitePath } from "./paths.js";
export class DevDataSqliteDb {
static db: DatabaseConnection | null = null;
private static async createTables(db: DatabaseConnection) {
await db.exec(
`CREATE TABLE IF NOT EXISTS tokens_generated (
id INTEGER PRIMARY KEY AUTOINCREMENT,
model TEXT NOT NULL,
provider TEXT NOT NULL,
tokens_generated INTEGER NOT NULL,
tokens_prompt INTEGER NOT NULL DEFAULT 0,
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
)`,
);
// Add tokens_prompt column if it doesn't exist
const columnCheckResult = await db.all(
`PRAGMA table_info(tokens_generated);`,
);
const columnExists = columnCheckResult.some(
(col: any) => col.name === "tokens_prompt",
);
if (!columnExists) {
await db.exec(
`ALTER TABLE tokens_generated ADD COLUMN tokens_prompt INTEGER NOT NULL DEFAULT 0;`,
);
}
}
public static async logTokensGenerated(
model: string,
provider: string,
promptTokens: number,
generatedTokens: number,
) {
const db = await DevDataSqliteDb.get();
await db?.run(
`INSERT INTO tokens_generated (model, provider, tokens_prompt, tokens_generated) VALUES (?, ?, ?, ?)`,
[model, provider, promptTokens, generatedTokens],
);
}
public static async getTokensPerDay() {
const db = await DevDataSqliteDb.get();
const result = await db?.all(
// Return a sum of tokens_generated and tokens_prompt columns aggregated by day
`SELECT date(timestamp) as day, sum(tokens_prompt) as promptTokens, sum(tokens_generated) as generatedTokens
FROM tokens_generated
GROUP BY date(timestamp)`,
);
return result ?? [];
}
public static async getTokensPerModel() {
const db = await DevDataSqliteDb.get();
const result = await db?.all(
// Return a sum of tokens_generated and tokens_prompt columns aggregated by model
`SELECT model, sum(tokens_prompt) as promptTokens, sum(tokens_generated) as generatedTokens
FROM tokens_generated
GROUP BY model`,
);
return result ?? [];
}
static async get() {
const devDataSqlitePath = getDevDataSqlitePath();
if (DevDataSqliteDb.db && fs.existsSync(devDataSqlitePath)) {
return DevDataSqliteDb.db;
}
DevDataSqliteDb.db = await open({
filename: devDataSqlitePath,
driver: sqlite3.Database,
});
await DevDataSqliteDb.createTables(DevDataSqliteDb.db!);
return DevDataSqliteDb.db;
}
}