Skip to content
Merged
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 51 additions & 6 deletions apps/api/openapi.gen.json
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,49 @@
}
}
},
"/llm/chat/completions": {
"post": {
"tags": [
"llm"
],
"operationId": "llm_chat_completions",
"parameters": [
{
"name": "x-char-task",
"in": "header",
"description": "Task type for model selection",
"required": false,
"schema": {
"oneOf": [
{
"type": "null"
},
{
"$ref": "#/components/schemas/CharTask"
}
]
}
}
],
"responses": {
"200": {
"description": "Chat completion response (streaming or non-streaming)"
},
"401": {
"description": "Unauthorized"
},
"429": {
"description": "Rate limit exceeded"
},
"502": {
"description": "Upstream provider failed"
},
"504": {
"description": "Request timeout"
}
}
}
},
"/nango/connect-session": {
"post": {
"tags": [
Expand Down Expand Up @@ -881,6 +924,14 @@
}
}
},
"CharTask": {
"type": "string",
"enum": [
"chat",
"enhance",
"title"
]
},
"ConnectSessionResponse": {
"type": "object",
"required": [
Expand Down Expand Up @@ -1759,12 +1810,6 @@
"scheme": "bearer",
"bearerFormat": "JWT",
"description": "Supabase JWT token"
},
"device_fingerprint": {
"type": "apiKey",
"in": "header",
"name": "x-device-fingerprint",
"description": "Optional device fingerprint for analytics"
}
}
},
Expand Down
11 changes: 1 addition & 10 deletions apps/api/src/openapi.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use std::collections::BTreeMap;

use utoipa::openapi::path::{Operation, PathItem};
use utoipa::openapi::security::{
ApiKey, ApiKeyValue, Http, HttpAuthScheme, SecurityRequirement, SecurityScheme,
};
use utoipa::openapi::security::{Http, HttpAuthScheme, SecurityRequirement, SecurityScheme};
use utoipa::{Modify, OpenApi};

#[derive(OpenApi)]
Expand Down Expand Up @@ -71,13 +69,6 @@ impl Modify for SecurityAddon {
.build(),
),
);
components.add_security_scheme(
"device_fingerprint",
SecurityScheme::ApiKey(ApiKey::Header(ApiKeyValue::with_description(
"x-device-fingerprint",
"Optional device fingerprint for analytics",
))),
);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion apps/desktop/src/components/chat/session.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ function useTransport(
systemPromptOverride?: string,
) {
const registry = useToolRegistry();
const configuredModel = useLanguageModel();
const configuredModel = useLanguageModel("chat");
const model = modelOverride ?? configuredModel;
const language = main.UI.useValue("ai_language", main.STORE_ID) ?? "en";
const [systemPrompt, setSystemPrompt] = useState<string | undefined>();
Expand Down
2 changes: 1 addition & 1 deletion apps/desktop/src/components/chat/view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export function ChatView() {
currentTab?.type === "sessions" ? currentTab.id : undefined;

const stableSessionId = useStableSessionId(groupId);
const model = useLanguageModel();
const model = useLanguageModel("chat");

const { handleSendMessage } = useChatActions({
groupId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export function OptionsMenu({
const { user_id } = main.UI.useValues(main.STORE_ID);
const updateSessionTabState = useTabs((state) => state.updateSessionTabState);
const createEnhancedNote = useCreateEnhancedNote();
const model = useLanguageModel();
const model = useLanguageModel("enhance");
const generate = useAITask((state) => state.generate);
const selectedTemplateId = settings.UI.useValue(
"selected_template_id",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export function EnhanceError({
enhancedNoteId: string;
error: Error | undefined;
}) {
const model = useLanguageModel();
const model = useLanguageModel("enhance");
const generate = useAITask((state) => state.generate);
const templateId =
(main.UI.useCell(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ function CreateOtherFormatButton({
main.STORE_ID,
);
const createEnhancedNote = useCreateEnhancedNote();
const model = useLanguageModel();
const model = useLanguageModel("enhance");
const openNew = useTabs((state) => state.openNew);

const store = main.UI.useStore(main.STORE_ID);
Expand Down Expand Up @@ -611,7 +611,7 @@ function labelForEditorView(view: EditorView): string {
}

function useEnhanceLogic(sessionId: string, enhancedNoteId: string) {
const model = useLanguageModel();
const model = useLanguageModel("enhance");
const llmStatus = useLLMConnectionStatus();
const taskId = createTaskId(enhancedNoteId, "enhance");
const [missingModelError, setMissingModelError] = useState<Error | null>(
Expand Down
2 changes: 1 addition & 1 deletion apps/desktop/src/hooks/autoEnhance/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export function useAutoEnhanceRunner(
isEnhancing: boolean;
} {
const sessionId = tab.id;
const model = useLanguageModel();
const model = useLanguageModel("enhance");
const { conn: llmConn } = useLLMConnection();
const { updateSessionTabState } = useTabs();
const createEnhancedNote = useCreateEnhancedNote();
Expand Down
17 changes: 12 additions & 5 deletions apps/desktop/src/hooks/useLLMConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { fetch as tauriFetch } from "@tauri-apps/plugin-http";
import { extractReasoningMiddleware, wrapLanguageModel } from "ai";
import { useMemo } from "react";

import type { CharTask } from "@hypr/api-client";
import type { AIProviderStorage } from "@hypr/store";

import { useAuth } from "../auth";
Expand All @@ -22,7 +23,7 @@ import {
} from "../components/settings/ai/shared/eligibility";
import { env } from "../env";
import * as settings from "../store/tinybase/store/settings";
import { tracedFetch } from "../utils/traced-fetch";
import { createTracedFetch, tracedFetch } from "../utils/traced-fetch";

type LanguageModelV3 = Parameters<typeof wrapLanguageModel>[0]["model"];

Expand Down Expand Up @@ -52,9 +53,12 @@ type LLMConnectionResult = {
status: LLMConnectionStatus;
};

export const useLanguageModel = (): LanguageModelV3 | null => {
export const useLanguageModel = (task?: CharTask): LanguageModelV3 | null => {
const { conn } = useLLMConnection();
return useMemo(() => (conn ? createLanguageModel(conn) : null), [conn]);
return useMemo(
() => (conn ? createLanguageModel(conn, task) : null),
[conn, task],
);
};

export const useLLMConnection = (): LLMConnectionResult => {
Expand Down Expand Up @@ -227,11 +231,14 @@ const wrapWithThinkingMiddleware = (
});
};

const createLanguageModel = (conn: LLMConnectionInfo): LanguageModelV3 => {
const createLanguageModel = (
conn: LLMConnectionInfo,
task?: CharTask,
): LanguageModelV3 => {
switch (conn.providerId) {
case "hyprnote": {
const provider = createOpenRouter({
fetch: tracedFetch,
fetch: task ? createTracedFetch(task) : tracedFetch,
baseURL: conn.baseUrl,
apiKey: conn.apiKey,
});
Expand Down
2 changes: 1 addition & 1 deletion apps/desktop/src/hooks/useTitleGeneration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { useLanguageModel } from "./useLLMConnection";

export function useTitleGeneration(tab: Extract<Tab, { type: "sessions" }>) {
const sessionId = tab.id;
const model = useLanguageModel();
const model = useLanguageModel("title");

const titleTaskId = createTaskId(sessionId, "title");

Expand Down
1 change: 1 addition & 0 deletions apps/desktop/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,4 @@ export const buildWebAppUrl = async (
export const DEFAULT_USER_ID = "00000000-0000-0000-0000-000000000000";

export const DEVICE_FINGERPRINT_HEADER = "x-device-fingerprint";
export const CHAR_TASK_HEADER = "x-char-task";
11 changes: 10 additions & 1 deletion apps/desktop/src/utils/traced-fetch.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import * as Sentry from "@sentry/react";
import { fetch as tauriFetch } from "@tauri-apps/plugin-http";

import type { CharTask } from "@hypr/api-client";
import { commands as miscCommands } from "@hypr/plugin-misc";

import { DEVICE_FINGERPRINT_HEADER } from ".";
import { CHAR_TASK_HEADER, DEVICE_FINGERPRINT_HEADER } from ".";

let cachedFingerprint: string | null = null;

Expand Down Expand Up @@ -57,3 +58,11 @@ export const tracedFetch: typeof fetch = async (input, init) => {
},
);
};

export function createTracedFetch(task: CharTask): typeof fetch {
return async (input, init) => {
const headers = new Headers(init?.headers);
headers.set(CHAR_TASK_HEADER, task);
return tracedFetch(input, { ...init, headers });
};
}
24 changes: 16 additions & 8 deletions crates/api-support/src/routes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,23 @@ use crate::state::AppState;
pub use feedback::{FeedbackRequest, FeedbackResponse};

pub async fn router(config: SupportConfig) -> Router {
let resolver = hypr_llm_proxy::StaticModelResolver::default()
.with_models(
hypr_llm_proxy::MODEL_KEY_DEFAULT,
vec![
"openai/gpt-oss-120b".into(),
"moonshotai/kimi-k2-0905".into(),
],
)
.with_models(
hypr_llm_proxy::MODEL_KEY_TOOL_CALLING,
vec![
"anthropic/claude-haiku-4.5".into(),
"moonshotai/kimi-k2-0905:exacto".into(),
],
);
let llm_config = hypr_llm_proxy::LlmProxyConfig::new(&config.openrouter)
.with_models_default(vec![
"openai/gpt-oss-120b".into(),
"moonshotai/kimi-k2-0905".into(),
])
.with_models_tool_calling(vec![
"anthropic/claude-haiku-4.5".into(),
"moonshotai/kimi-k2-0905:exacto".into(),
]);
.with_model_resolver(std::sync::Arc::new(resolver));
let llm_router = hypr_llm_proxy::router(llm_config);

let state = AppState::new(config).await;
Expand Down
1 change: 1 addition & 0 deletions crates/llm-proxy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ backon = { workspace = true }
bytes = { workspace = true }
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
strum = { workspace = true, features = ["derive"] }
thiserror = { workspace = true }
utoipa = { workspace = true }

Expand Down
28 changes: 9 additions & 19 deletions crates/llm-proxy/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::time::Duration;

use crate::analytics::AnalyticsReporter;
use crate::env::ApiKey;
use crate::model::{ModelContext, ModelResolver, StaticModelResolver};
use crate::provider::{OpenRouterProvider, Provider};

const DEFAULT_TIMEOUT_MS: u64 = 120_000;
Expand All @@ -28,8 +29,7 @@ impl Default for RetryConfig {
pub struct LlmProxyConfig {
pub api_key: String,
pub timeout: Duration,
pub models_tool_calling: Vec<String>,
pub models_default: Vec<String>,
resolver: Arc<dyn ModelResolver>,
pub analytics: Option<Arc<dyn AnalyticsReporter>>,
pub provider: Arc<dyn Provider>,
pub retry_config: RetryConfig,
Expand All @@ -40,34 +40,24 @@ impl LlmProxyConfig {
Self {
api_key: api_key.into().0,
timeout: Duration::from_millis(DEFAULT_TIMEOUT_MS),
models_tool_calling: vec![
"anthropic/claude-haiku-4.5".into(),
"openai/gpt-oss-120b:exacto".into(),
"moonshotai/kimi-k2-0905:exacto".into(),
],
models_default: vec![
"anthropic/claude-sonnet-4.5".into(),
"openai/gpt-5.2-chat".into(),
"moonshotai/kimi-k2-0905".into(),
],
resolver: Arc::new(StaticModelResolver::default()),
analytics: None,
provider: Arc::new(OpenRouterProvider::default()),
retry_config: RetryConfig::default(),
}
}

pub fn with_timeout(mut self, timeout: Duration) -> Self {
self.timeout = timeout;
self
pub fn resolve(&self, ctx: &ModelContext) -> Vec<String> {
self.resolver.resolve(ctx)
}

pub fn with_models_tool_calling(mut self, models: Vec<String>) -> Self {
self.models_tool_calling = models;
pub fn with_timeout(mut self, timeout: Duration) -> Self {
self.timeout = timeout;
self
}

pub fn with_models_default(mut self, models: Vec<String>) -> Self {
self.models_default = models;
pub fn with_model_resolver(mut self, resolver: Arc<dyn ModelResolver>) -> Self {
self.resolver = resolver;
self
}

Expand Down
Loading
Loading