Skip to content

Commit 1bbea4b

Browse files
models and cookies invalidation (#6613)
1 parent 39aa1c4 commit 1bbea4b

File tree

2 files changed

+68
-2
lines changed

2 files changed

+68
-2
lines changed

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

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ import {
1616
import { janApiClient, JanChatMessage } from './api'
1717
import { janProviderStore } from './store'
1818

19+
// Jan models support tools via MCP
20+
const JAN_MODEL_CAPABILITIES = ['tools'] as const
21+
1922
export default class JanProviderWeb extends AIEngine {
2023
readonly provider = 'jan'
2124
private activeSessions: Map<string, SessionInfo> = new Map()
@@ -24,6 +27,9 @@ export default class JanProviderWeb extends AIEngine {
2427
console.log('Loading Jan Provider Extension...')
2528

2629
try {
30+
// Check and clear invalid Jan models (capabilities mismatch)
31+
this.validateJanModelsLocalStorage()
32+
2733
// Initialize authentication and fetch models
2834
await janApiClient.initialize()
2935
console.log('Jan Provider Extension loaded successfully')
@@ -35,6 +41,54 @@ export default class JanProviderWeb extends AIEngine {
3541
super.onLoad()
3642
}
3743

44+
// Verify Jan models capabilities in localStorage
45+
private validateJanModelsLocalStorage() {
46+
try {
47+
console.log("Validating Jan models in localStorage...")
48+
const storageKey = 'model-provider'
49+
const data = localStorage.getItem(storageKey)
50+
if (!data) return
51+
52+
const parsed = JSON.parse(data)
53+
if (!parsed?.state?.providers) return
54+
55+
// Check if any Jan model has incorrect capabilities
56+
let hasInvalidModel = false
57+
58+
for (const provider of parsed.state.providers) {
59+
if (provider.provider === 'jan' && provider.models) {
60+
for (const model of provider.models) {
61+
console.log(`Checking Jan model: ${model.id}`, model.capabilities)
62+
if (JSON.stringify(model.capabilities) !== JSON.stringify(JAN_MODEL_CAPABILITIES)) {
63+
hasInvalidModel = true
64+
console.log(`Found invalid Jan model: ${model.id}, clearing localStorage`)
65+
break
66+
}
67+
}
68+
}
69+
if (hasInvalidModel) break
70+
}
71+
72+
// If any invalid model found, just clear the storage
73+
if (hasInvalidModel) {
74+
// Force clear the storage
75+
localStorage.removeItem(storageKey)
76+
// Verify it's actually removed
77+
const afterRemoval = localStorage.getItem(storageKey)
78+
// If still present, try setting to empty state
79+
if (afterRemoval) {
80+
// Try alternative clearing method
81+
localStorage.setItem(storageKey, JSON.stringify({ state: { providers: [] }, version: parsed.version || 3 }))
82+
}
83+
console.log('Cleared model-provider from localStorage due to invalid Jan capabilities')
84+
// Force a page reload to ensure clean state
85+
window.location.reload()
86+
}
87+
} catch (error) {
88+
console.error('Failed to check Jan models:', error)
89+
}
90+
}
91+
3892
override async onUnload() {
3993
console.log('Unloading Jan Provider Extension...')
4094

@@ -64,7 +118,7 @@ export default class JanProviderWeb extends AIEngine {
64118
path: undefined, // Remote model, no local path
65119
owned_by: model.owned_by,
66120
object: model.object,
67-
capabilities: ['tools'], // Jan models support both tools via MCP
121+
capabilities: [...JAN_MODEL_CAPABILITIES],
68122
}
69123
: undefined
70124
)
@@ -85,7 +139,7 @@ export default class JanProviderWeb extends AIEngine {
85139
path: undefined, // Remote model, no local path
86140
owned_by: model.owned_by,
87141
object: model.object,
88-
capabilities: ['tools'], // Jan models support both tools via MCP
142+
capabilities: [...JAN_MODEL_CAPABILITIES],
89143
}))
90144
} catch (error) {
91145
console.error('Failed to list Jan models:', error)

extensions-web/src/shared/auth/service.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,18 @@ export class JanAuthService {
4848
* Called on app load to check existing session
4949
*/
5050
async initialize(): Promise<void> {
51+
// Ensure refreshtoken is valid (in case of expired session or secret change)
52+
try {
53+
await refreshToken()
54+
} catch (error) {
55+
console.log('Failed to refresh token on init:', error)
56+
// If refresh fails, logout to clear any invalid state
57+
console.log('Logging out and clearing auth state to clear invalid session...')
58+
await logoutUser()
59+
this.clearAuthState()
60+
this.authBroadcast.broadcastLogout()
61+
}
62+
// Authentication state check
5163
try {
5264
if (!this.isAuthenticated()) {
5365
// Not authenticated - ensure guest access

0 commit comments

Comments
 (0)