Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into support-ollama-api
Browse files Browse the repository at this point in the history
  • Loading branch information
haraldschilly committed Mar 8, 2024
2 parents a1ddbed + 0681765 commit 37b0791
Show file tree
Hide file tree
Showing 143 changed files with 2,344 additions and 1,321 deletions.
10 changes: 5 additions & 5 deletions src/packages/backend/execute-code.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,21 +58,21 @@ describe("tests involving bash mode", () => {

describe("test timeout", () => {
it("kills if timeout reached", async () => {
const t = new Date().valueOf();
const t = Date.now();
try {
await executeCode({ command: "sleep 60", timeout: 0.1 });
expect(false).toBe(true);
} catch (err) {
expect(err).toContain("killed command");
expect(new Date().valueOf() - t).toBeGreaterThan(90);
expect(new Date().valueOf() - t).toBeLessThan(500);
expect(Date.now() - t).toBeGreaterThan(90);
expect(Date.now() - t).toBeLessThan(500);
}
});

it("doesn't kill when timeout not reached", async () => {
const t = new Date().valueOf();
const t = Date.now();
await executeCode({ command: "sleep 0.1", timeout: 0.5 });
expect(new Date().valueOf() - t).toBeGreaterThan(90);
expect(Date.now() - t).toBeGreaterThan(90);
});

it("kills in non-bash mode if timeout reached", async () => {
Expand Down
102 changes: 36 additions & 66 deletions src/packages/backend/watcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,92 +14,62 @@ is an event emitter with events:
and a method .close().
The ctime might be undefined, in case it can't be determined.
If debounce is given, only fires after the file
definitely has not had its ctime changed
for at least debounce ms. Does NOT fire when
the file first has ctime changed.
Only fires after the file definitely has not had its
ctime changed for at least debounce ms (this is the atomic
option to chokidar). Does NOT fire when the file first
has ctime changed.
*/

import { EventEmitter } from "node:events";
import { stat, unwatchFile, watchFile } from "node:fs";
import { watch, FSWatcher } from "chokidar";
import { getLogger } from "./logger";
import { debounce as lodashDebounce } from "lodash";

const L = getLogger("watcher");

export class Watcher extends EventEmitter {
private path: string;
private interval: number;
private debounce: number;
private _waiting_for_stable?: boolean;
private watcher: FSWatcher;

constructor(path, interval, debounce) {
constructor(path: string, interval: number=300, debounce: number=0) {
super();
this.close = this.close.bind(this);
this._listen = this._listen.bind(this);
this._emit_when_stable = this._emit_when_stable.bind(this);
this.path = path;
this.interval = interval;
this.debounce = debounce;

L.debug(`${path}: interval=${interval}, debounce=${debounce}`);
watchFile(
this.path,
{ interval: this.interval, persistent: false },
this._listen
);
}

close() {
this.removeAllListeners();
unwatchFile(this.path, this._listen);
}

_listen(curr, _prev): void {
if (curr.dev === 0) {
this.watcher = watch(this.path, {
interval: this.interval, // only effective if we end up forced to use polling
persistent: false,
alwaysStat: true,
atomic: true,
});
this.watcher.on("unlink", () => {
this.emit("delete");
return;
}
if (this.debounce) {
this._emit_when_stable(true);
} else {
stat(this.path, (err, stats) => {
if (!err) {
this.emit("change", stats.ctime);
}
});
}
}
});
this.watcher.on("unlinkDir", () => {
this.emit("delete");
});

_emit_when_stable(first): void {
/*
@_emit_when_stable gets called
periodically until the last ctime of the file
is at least @debounce ms in the past, or there
is an error.
*/
if (first && this._waiting_for_stable) {
return;
}
this._waiting_for_stable = true;
stat(this.path, (err, stats) => {
if (err) {
// maybe file deleted; give up.
delete this._waiting_for_stable;
const emitChange = lodashDebounce(
(ctime) => this.emit("change", ctime),
debounce,
);
this.watcher.on("error", (err) => {
L.debug("WATCHER error -- ", err);
});

this.watcher.on("change", (_, stats) => {
if (stats == null) {
L.debug("WATCHER change with no stats (shouldn't happen)", { path });
return;
}
const elapsed = Date.now() - stats.ctime.getTime();
if (elapsed < this.debounce) {
// File keeps changing - try again soon
setTimeout(
() => this._emit_when_stable(false),
Math.max(500, this.debounce - elapsed + 100)
);
} else {
delete this._waiting_for_stable;
this.emit("change", stats.ctime);
}
emitChange(stats.ctime);
});
}

close = async () => {
this.removeAllListeners();
await this.watcher.close();
};
}
4 changes: 2 additions & 2 deletions src/packages/database/pool/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ export function timeInSeconds(field: string, asField?: string): string {

// Given number of seconds **in the future**.
export function expireTime(ttl_s: number = 0): Date {
return new Date(new Date().valueOf() + ttl_s * 1000);
}
return new Date(Date.now() + ttl_s * 1000);
}
2 changes: 1 addition & 1 deletion src/packages/frontend/account/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ If that doesn't work after a few minutes, try these ${doc_conn} or email ${this.
[opts.fingerprint]: {
title: opts.title,
value: opts.value,
creation_date: new Date().valueOf(),
creation_date: Date.now(),
},
},
});
Expand Down
11 changes: 10 additions & 1 deletion src/packages/frontend/account/useLanguageModelSetting.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,20 @@ export const SETTINGS_LANGUAGE_MODEL_KEY = "language_model";
// ATTN: it is tempting to use the `useProjectContext` hook here, but it is not possible
// The "AI Formula" dialog is outside the project context (unfortunately)
export function useLanguageModelSetting(
enabledLLMs: EnabledLLMs,
project_id?: string,
): [LanguageModel | string, (llm: LanguageModel | string) => void] {
const other_settings = useTypedRedux("account", "other_settings");
const ollama = useTypedRedux("customize", "ollama");

const haveOpenAI = useTypedRedux("customize", "openai_enabled");
const haveGoogle = useTypedRedux("customize", "google_vertexai_enabled");
const haveOllama = useTypedRedux("customize", "ollama_enabled");

const enabledLLMs: EnabledLLMs = useMemo(() => {
const projectsStore = redux.getStore("projects");
return projectsStore.whichLLMareEnabled(project_id);
}, [haveOpenAI, haveGoogle, haveOllama]);

const llm = useMemo(() => {
return getValidLanguageModelName(
other_settings?.get("language_model"),
Expand Down
2 changes: 1 addition & 1 deletion src/packages/frontend/alerts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export function alert_message(opts: AlertMessageOptions = {}) {
}

function check_for_clock_skew() {
const local_time = new Date().valueOf();
const local_time = Date.now();
const s = Math.ceil(
Math.abs(
webapp_client.time_client.server_time().valueOf() - local_time.valueOf()
Expand Down
2 changes: 1 addition & 1 deletion src/packages/frontend/client/anonymous-setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export async function do_anonymous_setup(client: WebappClient): Promise<void> {
try {
const resp = await client.account_client.create_account({
first_name: "Anonymous",
last_name: `User-${Math.round(new Date().valueOf() / 1000)}`,
last_name: `User-${Math.round(Date.now() / 1000)}`,
});
if (resp?.event == "account_creation_failed") {
throw Error(resp.error);
Expand Down
23 changes: 11 additions & 12 deletions src/packages/frontend/client/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,18 @@ async function callApi(endpoint: string, args?: object) {
...(args != null ? { body: JSON.stringify(args) } : undefined),
});
const respClone = resp.clone();
let json: any = null;
try {
const json = await resp.json();
if (json == null) {
throw Error("timeout -- please try again");
}
if (json.error) {
throw Error(json.error);
}
return json;
json = await resp.json();
} catch (e) {
console.error(`Error parsing response JSON from ${url}. Response text:`, {
text: await respClone.text(),
});
throw e; // rethrow the original error
const e2 = `Error -- invalid JSON: ${await respClone.text()}`;
throw Error(e2);
}
if (json == null) {
throw Error("timeout -- please try again");
}
if (typeof json == "object" && json.error) {
throw Error(json.error);
}
return json;
}
3 changes: 2 additions & 1 deletion src/packages/frontend/client/console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@ export function setup_global_cocalc(client): void {
cocalc.schema = require("@cocalc/util/schema");
cocalc.redux = redux;
cocalc.load_eruda = load_eruda;
cocalc.compute = require("@cocalc/frontend/compute/api");
console.log(
"DEBUG: Enabling extra CoCalc library functionality. Type cocalc or cc.[tab]."
"DEBUG: Enabling extra CoCalc library functionality. Type cocalc or cc.[tab].",
);
window.cocalc = window.cc = cocalc;

Expand Down
2 changes: 1 addition & 1 deletion src/packages/frontend/client/hub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ export class HubClient {
case "signed_in":
this.client.account_id = mesg.account_id;
this.set_signed_in();
this.signed_in_time = new Date().valueOf();
this.signed_in_time = Date.now();
setRememberMe(appBasePath);
this.signed_in_mesg = mesg;
this.client.emit("signed_in", mesg);
Expand Down
6 changes: 3 additions & 3 deletions src/packages/frontend/client/idle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class IdleClient {
// Wait a little before setting this stuff up.
await delay(15 * 1000);

this.idle_time = new Date().valueOf() + this.idle_timeout;
this.idle_time = Date.now() + this.idle_timeout;

/*
The this.init_time is a Date in the future.
Expand Down Expand Up @@ -86,7 +86,7 @@ export class IdleClient {

private idle_check(): void {
if (!this.idle_time) return;
const now = new Date().valueOf();
const now = Date.now();
if (this.idle_time >= now) return;
this.show_notification();
if (!this.delayed_disconnect) {
Expand All @@ -105,7 +105,7 @@ export class IdleClient {
// indicating that user is currently active.
public idle_reset(): void {
this.hide_notification();
this.idle_time = new Date().valueOf() + this.idle_timeout + 1000;
this.idle_time = Date.now() + this.idle_timeout + 1000;
if (this.delayed_disconnect) {
clearTimeout(this.delayed_disconnect);
this.delayed_disconnect = undefined;
Expand Down
6 changes: 3 additions & 3 deletions src/packages/frontend/client/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ export class ProjectClient {
project_id: opts.project_id,
command,
timeout: 60,
aggregate: Math.round(new Date().valueOf() / 5000), // aggregate calls into 5s windows, in case multiple clients ask for same find at once...
aggregate: Math.round(Date.now() / 5000), // aggregate calls into 5s windows, in case multiple clients ask for same find at once...
});
const n = opts.path.length + 1;
let v = result.stdout.split("\n");
Expand Down Expand Up @@ -404,10 +404,10 @@ export class ProjectClient {
// Do not make the timeout long, since that can mess up
// getting the hub-websocket to connect to the project.
const last = this.touch_throttle[project_id];
if (last != null && new Date().valueOf() - last <= 3000) {
if (last != null && Date.now() - last <= 3000) {
return;
}
this.touch_throttle[project_id] = new Date().valueOf();
this.touch_throttle[project_id] = Date.now();
try {
await this.call(message.touch_project({ project_id }));
} catch (err) {
Expand Down
4 changes: 2 additions & 2 deletions src/packages/frontend/client/time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export class TimeClient {
}
}
if (this.clock_skew_ms != null) {
return new Date(new Date().valueOf() - this.clock_skew_ms);
return new Date(Date.now() - this.clock_skew_ms);
} else {
return new Date();
}
Expand Down Expand Up @@ -164,7 +164,7 @@ export class TimeClient {
message: message.ping(),
timeout: opts.timeout,
});
ping_time = new Date().valueOf() - t.valueOf();
ping_time = Date.now() - t.valueOf();
bar = "";
for (let j = 0; j <= Math.floor(ping_time / 10); j++) {
bar += "*";
Expand Down
4 changes: 2 additions & 2 deletions src/packages/frontend/client/tracking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ export class TrackingClient {
error = JSON.stringify(error);
}
const last = this.log_error_cache[error];
if (last != null && new Date().valueOf() - last <= 1000 * 60 * 15) {
if (last != null && Date.now() - last <= 1000 * 60 * 15) {
return;
}
this.log_error_cache[error] = new Date().valueOf();
this.log_error_cache[error] = Date.now();
this.client.call({
message: message.log_client_error({ error }),
});
Expand Down
2 changes: 1 addition & 1 deletion src/packages/frontend/client/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export class UsersClient {
}
const v = await callback2(get_username, {
call: this.call,
aggregate: Math.floor(new Date().valueOf() / 60000),
aggregate: Math.floor(Date.now() / 60000),
account_id,
});
const u = v[account_id];
Expand Down
Loading

0 comments on commit 37b0791

Please sign in to comment.