在 CoStrict provider 的所有 API 请求中注入 agent-type header,
反映当前会话所使用的 agent 或 skill,默认值为 "build"。
- fetch.ts: createCoStrictFetch 接受可选 agentType 参数,PascalCase
自动转 kebab-case(StrictSpec → strict-spec),注入 agent-type header
- index.ts: queryModelCoStrict 读取 getMainThreadAgentType()(--agent 启动)
或 getActiveSkillName()(slash skill 触发)作为 agentType
- models.ts: /v1/models 请求添加 User-Agent: csc/{VERSION} header
- state.ts: 新增 activeSkillName 状态及 getter/setter
- processSlashCommand.tsx: inline skill 和 fork skill(context:fork)
触发时均调用 setActiveSkillName 记录当前 skill/agent 名
- caches.ts: /clear 时调用 setActiveSkillName(undefined) 重置状态,
避免新会话继承上一会话的 agent-type
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
85 lines
1.9 KiB
TypeScript
85 lines
1.9 KiB
TypeScript
/**
|
||
* CoStrict 动态模型列表模块
|
||
* 从 /ai-gateway/api/v1/models 获取可用模型,1小时缓存
|
||
*/
|
||
|
||
export interface CoStrictModel {
|
||
id: string
|
||
name?: string
|
||
object?: string
|
||
created?: number
|
||
owned_by?: string
|
||
supportsImages?: boolean
|
||
contextWindow?: number
|
||
maxTokens?: number
|
||
creditConsumption?: number
|
||
creditDiscount?: number
|
||
[key: string]: any
|
||
}
|
||
|
||
interface ModelCache {
|
||
models: CoStrictModel[]
|
||
timestamp: number
|
||
}
|
||
|
||
let modelCache: ModelCache | null = null
|
||
const CACHE_TTL_MS = 60 * 60 * 1000 // 1 小时
|
||
|
||
/**
|
||
* 获取 CoStrict 可用模型列表
|
||
*/
|
||
export async function fetchCoStrictModels(
|
||
baseUrl: string,
|
||
accessToken: string,
|
||
): Promise<CoStrictModel[]> {
|
||
if (modelCache && Date.now() - modelCache.timestamp < CACHE_TTL_MS) {
|
||
return modelCache.models
|
||
}
|
||
|
||
try {
|
||
const response = await fetch(`${baseUrl}/ai-gateway/api/v1/models`, {
|
||
method: 'GET',
|
||
headers: {
|
||
Authorization: `Bearer ${accessToken}`,
|
||
Accept: 'application/json',
|
||
'User-Agent': `csc/${MACRO.VERSION}`,
|
||
},
|
||
})
|
||
|
||
if (!response.ok) {
|
||
throw new Error(`Failed to fetch models: HTTP ${response.status}`)
|
||
}
|
||
|
||
const data = (await response.json()) as { data?: CoStrictModel[] }
|
||
const models = data.data || []
|
||
|
||
if (models.length === 0) return getDefaultModels()
|
||
|
||
modelCache = { models, timestamp: Date.now() }
|
||
return models
|
||
} catch {
|
||
// 有旧缓存则使用旧缓存
|
||
if (modelCache) return modelCache.models
|
||
return getDefaultModels()
|
||
}
|
||
}
|
||
|
||
function getDefaultModels(): CoStrictModel[] {
|
||
return [
|
||
{ id: 'gpt-4', name: 'GPT-4' },
|
||
{ id: 'gpt-3.5-turbo', name: 'GPT-3.5 Turbo' },
|
||
]
|
||
}
|
||
|
||
export function clearModelCache(): void {
|
||
modelCache = null
|
||
}
|
||
|
||
/**
|
||
* 同步读取已缓存的模型列表(不发起网络请求)
|
||
* 供 modelOptions.ts 等同步上下文使用
|
||
*/
|
||
export function getCachedCoStrictModels(): CoStrictModel[] {
|
||
return modelCache?.models ?? []
|
||
}
|