dify/cli/src/api/app-meta.ts
Yunlu Wen a728e0ac69
feat: adding dify cli (#36348)
Co-authored-by: GareArc <garethcxy@dify.ai>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: L1nSn0w <l1nsn0w@qq.com>
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Co-authored-by: gigglewang <gigglewang@dify.ai>
Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
Co-authored-by: Xiyuan Chen <52963600+GareArc@users.noreply.github.com>
2026-05-26 01:12:36 +00:00

46 lines
1.6 KiB
TypeScript

import type { AppInfoCache } from '../cache/app-info.js'
import type { AppMeta, AppMetaFieldKey } from '../types/app-meta.js'
import type { AppsClient } from './apps.js'
import { covers, fromDescribe, mergeMeta } from '../types/app-meta.js'
export type AppMetaClientOptions = {
readonly apps: AppsClient
readonly host: string
readonly cache?: AppInfoCache
readonly now?: () => Date
}
export class AppMetaClient {
private readonly apps: AppsClient
private readonly host: string
private readonly cache: AppInfoCache | undefined
private readonly now: () => Date
constructor(opts: AppMetaClientOptions) {
this.apps = opts.apps
this.host = opts.host
this.cache = opts.cache
this.now = opts.now ?? (() => new Date())
}
async get(appId: string, workspaceId: string, fields: readonly AppMetaFieldKey[] = []): Promise<AppMeta> {
const cached = this.cache?.get(this.host, appId)
if (cached !== undefined && this.cache?.isFresh(cached, this.now()) === true && covers(cached.meta, fields))
return cached.meta
const resp = await this.apps.describe(appId, workspaceId, fields.length === 0 ? undefined : fields)
const fresh = fromDescribe(resp, fields)
const merged = cached !== undefined && this.cache?.isFresh(cached, this.now()) === true
? mergeMeta(cached.meta, fresh)
: fresh
if (this.cache !== undefined)
await this.cache.set(this.host, appId, merged)
return merged
}
async invalidate(appId: string): Promise<void> {
if (this.cache !== undefined)
await this.cache.delete(this.host, appId)
}
}