Skip to content

Commit 0771b99

Browse files
Fix: Web Services Improvement
Fix: Web Services Improvement
1 parent 5969301 commit 0771b99

File tree

6 files changed

+29
-33
lines changed

6 files changed

+29
-33
lines changed

extensions-web/src/jan-provider-web/api.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Handles API requests to Jan backend for models and chat completions
44
*/
55

6-
import { JanAuthService } from '../shared/auth'
6+
import { getSharedAuthService, JanAuthService } from '../shared'
77
import { JanModel, janProviderStore } from './store'
88

99
// JAN_API_BASE is defined in vite.config.ts
@@ -77,7 +77,7 @@ export class JanApiClient {
7777
private authService: JanAuthService
7878

7979
private constructor() {
80-
this.authService = JanAuthService.getInstance()
80+
this.authService = getSharedAuthService()
8181
}
8282

8383
static getInstance(): JanApiClient {
@@ -216,12 +216,9 @@ export class JanApiClient {
216216

217217
async initialize(): Promise<void> {
218218
try {
219-
await this.authService.initialize()
220219
janProviderStore.setAuthenticated(true)
221-
222220
// Fetch initial models
223221
await this.getModels()
224-
225222
console.log('Jan API client initialized successfully')
226223
} catch (error) {
227224
const errorMessage = error instanceof Error ? error.message : 'Failed to initialize API client'

extensions-web/src/mcp-web/index.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66

77
import { MCPExtension, MCPTool, MCPToolCallResult } from '@janhq/core'
8-
import { JanAuthService } from '../shared/auth'
8+
import { getSharedAuthService, JanAuthService } from '../shared'
99
import { Client } from '@modelcontextprotocol/sdk/client/index.js'
1010
import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js'
1111
import { JanMCPOAuthProvider } from './oauth-provider'
@@ -30,14 +30,12 @@ export default class MCPExtensionWeb extends MCPExtension {
3030
version?: string
3131
) {
3232
super(url, name, productName, active, description, version)
33-
this.authService = JanAuthService.getInstance()
33+
this.authService = getSharedAuthService()
3434
this.oauthProvider = new JanMCPOAuthProvider(this.authService)
3535
}
3636

3737
async onLoad(): Promise<void> {
3838
try {
39-
// Initialize authentication first
40-
await this.authService.initialize()
4139
// Initialize MCP client with OAuth
4240
await this.initializeMCPClient()
4341
// Then fetch tools

extensions-web/src/shared/auth.ts

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,13 @@ const AUTH_STORAGE_KEY = 'jan_auth_tokens'
2020
const TOKEN_EXPIRY_BUFFER = 60 * 1000 // 1 minute buffer before actual expiry
2121

2222
export class JanAuthService {
23-
private static instance: JanAuthService
2423
private tokens: AuthTokens | null = null
2524
private tokenExpiryTime: number = 0
2625

27-
private constructor() {
26+
constructor() {
2827
this.loadTokensFromStorage()
2928
}
3029

31-
static getInstance(): JanAuthService {
32-
if (!JanAuthService.instance) {
33-
JanAuthService.instance = new JanAuthService()
34-
}
35-
return JanAuthService.instance
36-
}
37-
3830
private loadTokensFromStorage(): void {
3931
try {
4032
const storedTokens = localStorage.getItem(AUTH_STORAGE_KEY)
@@ -169,16 +161,6 @@ export class JanAuthService {
169161
return this.tokens.access_token
170162
}
171163

172-
async initialize(): Promise<void> {
173-
try {
174-
await this.getValidAccessToken()
175-
console.log('Jan auth service initialized successfully')
176-
} catch (error) {
177-
console.error('Failed to initialize Jan auth service:', error)
178-
throw error
179-
}
180-
}
181-
182164
async getAuthHeader(): Promise<{ Authorization: string }> {
183165
const token = await this.getValidAccessToken()
184166
return {
@@ -217,4 +199,21 @@ export class JanAuthService {
217199
logout(): void {
218200
this.clearTokens()
219201
}
202+
}
203+
204+
declare global {
205+
interface Window {
206+
janAuthService?: JanAuthService
207+
}
208+
}
209+
210+
/**
211+
* Gets or creates the shared JanAuthService instance on the window object
212+
* This ensures all extensions use the same auth service instance
213+
*/
214+
export function getSharedAuthService(): JanAuthService {
215+
if (!window.janAuthService) {
216+
window.janAuthService = new JanAuthService()
217+
}
218+
return window.janAuthService
220219
}

extensions-web/src/shared/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
export { getSharedDB } from './db'
2-
export { JanAuthService } from './auth'
2+
export { JanAuthService, getSharedAuthService } from './auth'
33
export type { AuthTokens, AuthResponse } from './auth'

web-app/src/lib/completion.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ export const sendCompletion = async (
193193

194194
if (
195195
thread.model.id &&
196+
models[providerName]?.models !== true && // Skip if provider accepts any model (models: true)
196197
!Object.values(models[providerName]).flat().includes(thread.model.id) &&
197198
!tokenJS.extendedModelExist(providerName as any, thread.model.id) &&
198199
provider.provider !== 'llamacpp'
@@ -396,9 +397,12 @@ export const postMessageProcessing = async (
396397
let toolParameters = {}
397398
if (toolCall.function.arguments.length) {
398399
try {
400+
console.log('Raw tool arguments:', toolCall.function.arguments)
399401
toolParameters = JSON.parse(toolCall.function.arguments)
402+
console.log('Parsed tool parameters:', toolParameters)
400403
} catch (error) {
401404
console.error('Failed to parse tool arguments:', error)
405+
console.error('Raw arguments that failed:', toolCall.function.arguments)
402406
}
403407
}
404408
const approved =
@@ -414,9 +418,7 @@ export const postMessageProcessing = async (
414418

415419
const { promise, cancel } = getServiceHub().mcp().callToolWithCancellation({
416420
toolName: toolCall.function.name,
417-
arguments: toolCall.function.arguments.length
418-
? JSON.parse(toolCall.function.arguments)
419-
: {},
421+
arguments: toolCall.function.arguments.length ? toolParameters : {},
420422
})
421423

422424
useAppState.getState().setCancelToolCall(cancel)

web-app/src/routes/__root.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ const AppLayout = () => {
114114
{/* Fake absolute panel top to enable window drag */}
115115
<div className="absolute w-full h-10 z-10" data-tauri-drag-region />
116116
<DialogAppUpdater />
117-
<BackendUpdater />
117+
{PlatformFeatures[PlatformFeature.LOCAL_INFERENCE] && <BackendUpdater />}
118118

119119
{/* Use ResizablePanelGroup only on larger screens */}
120120
{!isSmallScreen && isLeftPanelOpen ? (

0 commit comments

Comments
 (0)