@@ -18,6 +18,7 @@ import type {
1818import { addOpencodeCacheHint , isTauriRuntime , safeStringify } from "./utils" ;
1919import { filterProviderList , mapConfigProvidersToList } from "./utils/providers" ;
2020import { createUpdaterState , type UpdateStatus } from "./context/updater" ;
21+ import { createDenClient , readDenSettings } from "./lib/den" ;
2122import {
2223 resetOpenworkState ,
2324 resetOpencodeCache ,
@@ -65,6 +66,84 @@ function forcedDevUpdateStatus(): UpdateStatus | null {
6566 } ;
6667}
6768
69+ function parseComparableVersion ( value : string ) : { release : number [ ] ; prerelease : string [ ] } | null {
70+ const normalized = value . trim ( ) . replace ( / ^ v / i, "" ) ;
71+ if ( ! normalized ) return null ;
72+
73+ const [ versionCore ] = normalized . split ( "+" , 1 ) ;
74+ if ( ! versionCore ) return null ;
75+
76+ const [ releasePart , prereleasePart = "" ] = versionCore . split ( "-" , 2 ) ;
77+ const release = releasePart . split ( "." ) . map ( ( segment ) => Number ( segment ) ) ;
78+ if ( ! release . length || release . some ( ( segment ) => ! Number . isInteger ( segment ) || segment < 0 ) ) {
79+ return null ;
80+ }
81+
82+ const prerelease = prereleasePart
83+ . split ( "." )
84+ . map ( ( segment ) => segment . trim ( ) )
85+ . filter ( Boolean ) ;
86+
87+ return { release, prerelease } ;
88+ }
89+
90+ function comparePrereleaseIdentifiers ( left : string [ ] , right : string [ ] ) : number {
91+ if ( ! left . length && ! right . length ) return 0 ;
92+ if ( ! left . length ) return 1 ;
93+ if ( ! right . length ) return - 1 ;
94+
95+ const count = Math . max ( left . length , right . length ) ;
96+ for ( let index = 0 ; index < count ; index += 1 ) {
97+ const leftPart = left [ index ] ;
98+ const rightPart = right [ index ] ;
99+ if ( leftPart === undefined ) return - 1 ;
100+ if ( rightPart === undefined ) return 1 ;
101+
102+ const leftNumeric = / ^ \d + $ / . test ( leftPart ) ? Number ( leftPart ) : null ;
103+ const rightNumeric = / ^ \d + $ / . test ( rightPart ) ? Number ( rightPart ) : null ;
104+
105+ if ( leftNumeric !== null && rightNumeric !== null ) {
106+ if ( leftNumeric !== rightNumeric ) return leftNumeric < rightNumeric ? - 1 : 1 ;
107+ continue ;
108+ }
109+
110+ if ( leftNumeric !== null ) return - 1 ;
111+ if ( rightNumeric !== null ) return 1 ;
112+
113+ const comparison = leftPart . localeCompare ( rightPart ) ;
114+ if ( comparison !== 0 ) return comparison < 0 ? - 1 : 1 ;
115+ }
116+
117+ return 0 ;
118+ }
119+
120+ function compareVersions ( left : string , right : string ) : number | null {
121+ const parsedLeft = parseComparableVersion ( left ) ;
122+ const parsedRight = parseComparableVersion ( right ) ;
123+ if ( ! parsedLeft || ! parsedRight ) return null ;
124+
125+ const count = Math . max ( parsedLeft . release . length , parsedRight . release . length ) ;
126+ for ( let index = 0 ; index < count ; index += 1 ) {
127+ const leftPart = parsedLeft . release [ index ] ?? 0 ;
128+ const rightPart = parsedRight . release [ index ] ?? 0 ;
129+ if ( leftPart !== rightPart ) return leftPart < rightPart ? - 1 : 1 ;
130+ }
131+
132+ return comparePrereleaseIdentifiers ( parsedLeft . prerelease , parsedRight . prerelease ) ;
133+ }
134+
135+ async function isUpdateSupportedByDen ( updateVersion : string ) {
136+ try {
137+ const settings = readDenSettings ( ) ;
138+ const client = createDenClient ( { baseUrl : settings . baseUrl } ) ;
139+ const metadata = await client . getAppVersionMetadata ( ) ;
140+ const comparison = compareVersions ( updateVersion , metadata . latestAppVersion ) ;
141+ return comparison !== null && comparison <= 0 ;
142+ } catch {
143+ return false ;
144+ }
145+ }
146+
68147export function createSystemState ( options : {
69148 client : Accessor < Client | null > ;
70149 sessions : Accessor < Session [ ] > ;
@@ -444,6 +523,13 @@ export function createSystemState(options: {
444523 }
445524
446525 const notes = typeof update . body === "string" ? update . body : undefined ;
526+
527+ if ( ! ( await isUpdateSupportedByDen ( update . version ) ) ) {
528+ setPendingUpdate ( null ) ;
529+ setUpdateStatus ( { state : "idle" , lastCheckedAt : checkedAt } ) ;
530+ return ;
531+ }
532+
447533 setPendingUpdate ( { update, version : update . version , notes } ) ;
448534 setUpdateStatus ( {
449535 state : "available" ,
0 commit comments