Skip to content

Commit

Permalink
Tauri v1 -> v2 at the point where it compiles and loads
Browse files Browse the repository at this point in the history
- app updater broken
- native menu commented out
  • Loading branch information
mtsgrd committed Aug 29, 2024
1 parent b2cdc37 commit d5fab5e
Show file tree
Hide file tree
Showing 46 changed files with 16,735 additions and 1,469 deletions.
2,259 changes: 1,443 additions & 816 deletions Cargo.lock

Large diffs are not rendered by default.

17 changes: 12 additions & 5 deletions apps/desktop/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,22 @@
"@sveltejs/adapter-static": "catalog:svelte",
"@sveltejs/kit": "catalog:svelte",
"@sveltejs/vite-plugin-svelte": "catalog:svelte",
"@tauri-apps/api": "^1.6.0",
"@tauri-apps/api": "2.0.0-rc.3",
"@tauri-apps/plugin-dialog": "2.0.0-rc.0",
"@tauri-apps/plugin-http": "2.0.0-rc.1",
"@tauri-apps/plugin-log": "2.0.0-rc.0",
"@tauri-apps/plugin-os": "2.0.0-rc.0",
"@tauri-apps/plugin-process": "2.0.0-rc.0",
"@tauri-apps/plugin-shell": "2.0.0-rc.0",
"@tauri-apps/plugin-store": "2.0.0-rc.0",
"@tauri-apps/plugin-updater": "2.0.0-rc.0",
"@testing-library/jest-dom": "^6.4.8",
"@testing-library/svelte": "^5.2.1",
"@types/diff-match-patch": "^1.0.36",
"@types/git-url-parse": "^9.0.3",
"@types/lscache": "^1.3.4",
"@types/marked": "^5.0.2",
"@types/node": "^22.3.0",
"@types/postcss-pxtorem": "^6.0.3",
"@vitest/ui": "^2.0.5",
"@wdio/cli": "^8.39.1",
Expand Down Expand Up @@ -76,15 +85,13 @@
"svelte": "catalog:svelte",
"svelte-check": "catalog:svelte",
"svelte-french-toast": "^1.2.0",
"tauri-plugin-log-api": "github:tauri-apps/tauri-plugin-log#v1",
"tauri-plugin-store-api": "github:tauri-apps/tauri-plugin-store#v1",
"tinykeys": "^2.1.0",
"ts-node": "^10.9.2",
"vite": "catalog:",
"vitest": "^2.0.5",
"@types/node": "^22.3.0"
"vitest": "^2.0.5"
},
"dependencies": {
"@tauri-apps/plugin-fs": "2.0.0-rc.1",
"openai": "^4.47.3"
}
}
73 changes: 37 additions & 36 deletions apps/desktop/src/hooks.client.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,45 @@
import { showError } from '$lib/notifications/toasts';
import { captureException } from '@sentry/sveltekit';
import { error as logErrorToFile } from 'tauri-plugin-log-api';
import type { HandleClientError } from '@sveltejs/kit';
// import { showError } from '$lib/notifications/toasts';
// import { captureException } from '@sentry/sveltekit';
// import { error as logErrorToFile } from 'tauri-plugin-log-api';
// import type { HandleClientError } from '@sveltejs/kit';

// SvelteKit error handler.
export function handleError({
error,
status
}: {
error: unknown;
status: number;
}): ReturnType<HandleClientError> {
if (status !== 404) {
logError(error);
}
return {
message: String(error)
};
}
// // SvelteKit error handler.
// export function handleError({
// error,
// status
// }: {
// error: unknown;
// status: number;
// }): ReturnType<HandleClientError> {
// if (status !== 404) {
// logError(error);
// }
// return {
// message: String(error)
// };
// }

// Handler for unhandled errors inside promises.
window.onunhandledrejection = (e: PromiseRejectionEvent) => {
logError(e.reason);
// logError(e.reason);
console.trace(e);
};

function logError(error: unknown) {
let message = error instanceof Error ? error.message : String(error);
const stack = error instanceof Error ? error.stack : undefined;
// function logError(error: unknown) {
// let message = error instanceof Error ? error.message : String(error);
// const stack = error instanceof Error ? error.stack : undefined;

const id = captureException(message, {
mechanism: {
type: 'sveltekit',
handled: false
}
});
message = `${id}: ${message}\n`;
if (stack) message = `${message}\n${stack}\n`;
// const id = captureException(message, {
// mechanism: {
// type: 'sveltekit',
// handled: false
// }
// });
// message = `${id}: ${message}\n`;
// if (stack) message = `${message}\n${stack}\n`;

logErrorToFile(message);
console.error(message);
showError('Something went wrong', message);
return id;
}
// logErrorToFile(message);
// console.error(message);
// showError('Something went wrong', message);
// return id;
// }
13 changes: 7 additions & 6 deletions apps/desktop/src/lib/ai/anthropicClient.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { SHORT_DEFAULT_COMMIT_TEMPLATE, SHORT_DEFAULT_BRANCH_TEMPLATE } from '$lib/ai/prompts';
import { type AIClient, type AnthropicModelName, type Prompt } from '$lib/ai/types';
import { buildFailureFromAny, ok, type Result } from '$lib/result';
import { fetch, Body } from '@tauri-apps/api/http';
import { fetch } from '@tauri-apps/plugin-http';

type AnthropicAPIResponse = {
content: { text: string }[];
Expand All @@ -18,13 +18,13 @@ export class AnthropicAIClient implements AIClient {
) {}

async evaluate(prompt: Prompt): Promise<Result<string, Error>> {
const body = Body.json({
const body = JSON.stringify({
messages: prompt,
max_tokens: 1024,
model: this.modelName
});

const response = await fetch<AnthropicAPIResponse>('https://api.anthropic.com/v1/messages', {
const response = await fetch('https://api.anthropic.com/v1/messages', {
method: 'POST',
headers: {
'x-api-key': this.apiKey,
Expand All @@ -34,11 +34,12 @@ export class AnthropicAIClient implements AIClient {
body
});

if (response.ok && response.data?.content?.[0]?.text) {
return ok(response.data.content[0].text);
const data = (await response.json()) as AnthropicAPIResponse;
if (response.ok) {
return ok(data.content[0]?.text || '');
} else {
return buildFailureFromAny(
`Anthropic returned error code ${response.status} ${response.data?.error?.message}`
`Anthropic returned error code ${response.status} ${data?.error?.message}`
);
}
}
Expand Down
4 changes: 2 additions & 2 deletions apps/desktop/src/lib/ai/butlerClient.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { SHORT_DEFAULT_BRANCH_TEMPLATE, SHORT_DEFAULT_COMMIT_TEMPLATE } from '$lib/ai/prompts';
import { SHORT_DEFAULT_BRANCH_TEMPLATE, SHORT_DEFAULT_COMMIT_TEMPLATE } from './prompts';
import { map, type Result } from '$lib/result';
import type { AIClient, ModelKind, Prompt } from '$lib/ai/types';
import type { HttpClient } from '$lib/backend/httpClient';
import type { AIClient, ModelKind, Prompt } from './types';

export class ButlerAIClient implements AIClient {
defaultCommitTemplate = SHORT_DEFAULT_COMMIT_TEMPLATE;
Expand Down
10 changes: 5 additions & 5 deletions apps/desktop/src/lib/ai/ollamaClient.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { LONG_DEFAULT_BRANCH_TEMPLATE, LONG_DEFAULT_COMMIT_TEMPLATE } from '$lib/ai/prompts';
import { MessageRole, type PromptMessage, type AIClient, type Prompt } from '$lib/ai/types';
import { LONG_DEFAULT_BRANCH_TEMPLATE, LONG_DEFAULT_COMMIT_TEMPLATE } from './prompts';
import { MessageRole, type PromptMessage, type AIClient, type Prompt } from './types';
import { andThen, buildFailureFromAny, ok, wrap, wrapAsync, type Result } from '$lib/result';
import { isNonEmptyObject } from '@gitbutler/ui/utils/typeguards';
import { fetch, Body, Response } from '@tauri-apps/api/http';
import { fetch } from '@tauri-apps/plugin-http';

export const DEFAULT_OLLAMA_ENDPOINT = 'http://127.0.0.1:11434';
export const DEFAULT_OLLAMA_MODEL_NAME = 'llama3';
Expand Down Expand Up @@ -132,9 +132,9 @@ ${JSON.stringify(OLLAMA_CHAT_MESSAGE_FORMAT_SCHEMA, null, 2)}`
* @param request - The OllamaChatRequest object containing the request details.
* @returns A Promise that resolves to the Response object.
*/
private async fetchChat(request: OllamaChatRequest): Promise<Result<Response<any>, Error>> {
private async fetchChat(request: OllamaChatRequest): Promise<Result<any, Error>> {
const url = new URL(OllamaAPEndpoint.Chat, this.endpoint);
const body = Body.json(request);
const body = JSON.stringify(request);
return await wrapAsync(
async () =>
await fetch(url.toString(), {
Expand Down
4 changes: 2 additions & 2 deletions apps/desktop/src/lib/ai/openAIClient.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { SHORT_DEFAULT_BRANCH_TEMPLATE, SHORT_DEFAULT_COMMIT_TEMPLATE } from '$lib/ai/prompts';
import { SHORT_DEFAULT_BRANCH_TEMPLATE, SHORT_DEFAULT_COMMIT_TEMPLATE } from './prompts';
import { andThen, buildFailureFromAny, ok, wrapAsync, type Result } from '$lib/result';
import type { OpenAIModelName, Prompt, AIClient } from '$lib/ai/types';
import type { OpenAIModelName, Prompt, AIClient } from './types';
import type OpenAI from 'openai';
import type { ChatCompletion } from 'openai/resources/index.mjs';

Expand Down
2 changes: 1 addition & 1 deletion apps/desktop/src/lib/ai/prompts.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type Prompt, MessageRole } from '$lib/ai/types';
import { type Prompt, MessageRole } from './types';

export const SHORT_DEFAULT_COMMIT_TEMPLATE: Prompt = [
{
Expand Down
12 changes: 6 additions & 6 deletions apps/desktop/src/lib/ai/service.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { AnthropicAIClient } from '$lib/ai/anthropicClient';
import { ButlerAIClient } from '$lib/ai/butlerClient';
import { OpenAIClient } from '$lib/ai/openAIClient';
import { SHORT_DEFAULT_BRANCH_TEMPLATE, SHORT_DEFAULT_COMMIT_TEMPLATE } from '$lib/ai/prompts';
import { AISecretHandle, AIService, GitAIConfigKey, KeyOption, buildDiff } from '$lib/ai/service';
import { AnthropicAIClient } from './anthropicClient';
import { ButlerAIClient } from './butlerClient';
import { OpenAIClient } from './openAIClient';
import { SHORT_DEFAULT_BRANCH_TEMPLATE, SHORT_DEFAULT_COMMIT_TEMPLATE } from './prompts';
import { AISecretHandle, AIService, GitAIConfigKey, KeyOption, buildDiff } from './service';
import {
AnthropicModelName,
ModelKind,
OpenAIModelName,
type AIClient,
type Prompt
} from '$lib/ai/types';
} from './types';
import { HttpClient } from '$lib/backend/httpClient';
import { buildFailureFromAny, ok, unwrap, type Result } from '$lib/result';
import { Hunk } from '$lib/vbranches/types';
Expand Down
14 changes: 5 additions & 9 deletions apps/desktop/src/lib/ai/service.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
import { AnthropicAIClient } from '$lib/ai/anthropicClient';
import { ButlerAIClient } from '$lib/ai/butlerClient';
import {
DEFAULT_OLLAMA_ENDPOINT,
DEFAULT_OLLAMA_MODEL_NAME,
OllamaClient
} from '$lib/ai/ollamaClient';
import { OpenAIClient } from '$lib/ai/openAIClient';
import { AnthropicAIClient } from './anthropicClient';
import { ButlerAIClient } from './butlerClient';
import { DEFAULT_OLLAMA_ENDPOINT, DEFAULT_OLLAMA_MODEL_NAME, OllamaClient } from './ollamaClient';
import { OpenAIClient } from './openAIClient';
import {
OpenAIModelName,
type AIClient,
AnthropicModelName,
ModelKind,
MessageRole,
type Prompt
} from '$lib/ai/types';
} from './types';
import { buildFailureFromAny, isFailure, ok, type Result } from '$lib/result';
import { splitMessage } from '$lib/utils/commitMessage';
import OpenAI from 'openai';
Expand Down
2 changes: 1 addition & 1 deletion apps/desktop/src/lib/backend/ipc.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { invoke as invokeTauri } from '@tauri-apps/api/core';
import { listen as listenTauri } from '@tauri-apps/api/event';
import { invoke as invokeTauri } from '@tauri-apps/api/tauri';
import type { EventCallback, EventName } from '@tauri-apps/api/event';

export enum Code {
Expand Down
2 changes: 1 addition & 1 deletion apps/desktop/src/lib/backend/projects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { invoke } from '$lib/backend/ipc';
import { showError } from '$lib/notifications/toasts';
import { persisted } from '$lib/persisted/persisted';
import * as toasts from '$lib/utils/toasts';
import { open } from '@tauri-apps/api/dialog';
import { open } from '@tauri-apps/plugin-dialog';
import { plainToInstance } from 'class-transformer';
import { get, writable } from 'svelte/store';
import type { HttpClient } from './httpClient';
Expand Down
5 changes: 2 additions & 3 deletions apps/desktop/src/lib/backend/tauri.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { invoke as invokeIpc, listen as listenIpc } from './ipc';
import { getVersion } from '@tauri-apps/api/app';
import { checkUpdate, onUpdaterEvent } from '@tauri-apps/api/updater';
import { check } from '@tauri-apps/plugin-updater';

export class Tauri {
invoke = invokeIpc;
listen = listenIpc;
checkUpdate = checkUpdate;
onUpdaterEvent = onUpdaterEvent;
checkUpdate = check;
currentVersion = getVersion;
}
31 changes: 13 additions & 18 deletions apps/desktop/src/lib/backend/updater.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import { Tauri } from './tauri';
import { showToast } from '$lib/notifications/toasts';
import { relaunch } from '@tauri-apps/api/process';
import {
installUpdate,
type UpdateResult,
type UpdateManifest,
type UpdateStatus
} from '@tauri-apps/api/updater';
import { relaunch } from '@tauri-apps/plugin-process';
import { type DownloadEvent, Update } from '@tauri-apps/plugin-updater';
import posthog from 'posthog-js';
import { derived, readable, writable } from 'svelte/store';

Expand All @@ -26,7 +21,7 @@ export const UPDATE_INTERVAL_MS = 3600000; // Hourly
export class UpdaterService {
readonly loading = writable(false);
readonly status = writable<Status | undefined>();
private manifest = writable<UpdateManifest | undefined>(undefined, () => {
private manifest = writable<Update | undefined>(undefined, () => {
this.start();
return () => {
this.stop();
Expand Down Expand Up @@ -63,16 +58,16 @@ export class UpdaterService {
this.checkForUpdate(true);
});

this.unlistenStatus = await this.tauri.onUpdaterEvent((event) => {
const { error, status } = event;
if (status !== 'UPTODATE' || this.lastCheckWasManual) {
this.status.set(status);
}
if (error) {
handleError(error, false);
posthog.capture('App Update Status Error', { error });
}
});
// this.unlistenStatus = await this.tauri.onUpdaterEvent((event) => {
// const { error, status } = event;
// if (status !== 'UPTODATE' || this.lastCheckWasManual) {
// this.status.set(status);
// }
// if (error) {
// handleError(error, false);
// posthog.capture('App Update Status Error', { error });
// }
// });

setInterval(async () => await this.checkForUpdate(), UPDATE_INTERVAL_MS);
this.checkForUpdate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import * as events from '$lib/utils/events';
import { createKeybind } from '$lib/utils/hotkeys';
import { unsubscribe } from '$lib/utils/unsubscribe';
import { open } from '@tauri-apps/api/shell';
import { open } from '@tauri-apps/plugin-shell';
import { onMount } from 'svelte';
import { goto } from '$app/navigation';
Expand Down
2 changes: 1 addition & 1 deletion apps/desktop/src/lib/components/Board.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import { BranchController } from '$lib/vbranches/branchController';
import { VirtualBranchService } from '$lib/vbranches/virtualBranch';
import Icon from '@gitbutler/ui/Icon.svelte';
import { open } from '@tauri-apps/api/shell';
import { open } from '@tauri-apps/plugin-shell';
import { flip } from 'svelte/animate';
const vbranchService = getContext(VirtualBranchService);
Expand Down
6 changes: 3 additions & 3 deletions apps/desktop/src/lib/components/ProjectSetup.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
loading = true;
try {
// TODO: Refactor temporary solution to forcing Windows to use system executable
if ($platformName === 'win32') {
if (platformName === 'windows') {
project.preferred_key = 'systemExecutable';
await projectService.updateProject(project);
await baseBranchService.refresh();
Expand All @@ -41,7 +41,7 @@
</script>

<DecorativeSplitView img={newProjectSvg}>
{#if selectedBranch[0] && selectedBranch[0] !== '' && $platformName !== 'win32'}
{#if selectedBranch[0] && selectedBranch[0] !== '' && platformName !== 'windows'}
{@const [remoteName, branchName] = selectedBranch[0].split(/\/(.*)/s)}
<KeysForm {remoteName} {branchName} disabled={loading} />
<div class="actions">
Expand All @@ -59,7 +59,7 @@
on:branchSelected={async (e) => {
selectedBranch = e.detail;
// TODO: Temporary solution to forcing Windows to use system executable
if ($platformName === 'win32') {
if (platformName === 'windows') {
setTarget();
}
}}
Expand Down
2 changes: 1 addition & 1 deletion apps/desktop/src/lib/components/ProjectSetupTarget.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@
testId="set-base-branch"
id="set-base-branch"
>
{#if $platformName === 'win32'}
{#if platformName === 'windows'}
Let's go
{:else}
Continue
Expand Down
Loading

0 comments on commit d5fab5e

Please sign in to comment.