@kenkaiiii/gg-core 5.88.1 → 5.90.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -99,6 +99,7 @@ __export(index_exports, {
99
99
  kimiCodeBaseUrl: () => kimiCodeBaseUrl,
100
100
  kimiCodingHeaders: () => kimiCodingHeaders,
101
101
  localAuthStorageKey: () => localAuthStorageKey,
102
+ localModelShortName: () => localModelShortName,
102
103
  log: () => log,
103
104
  loginAnthropic: () => loginAnthropic,
104
105
  loginGemini: () => loginGemini,
@@ -106,6 +107,7 @@ __export(index_exports, {
106
107
  loginOpenAI: () => loginOpenAI,
107
108
  loginXai: () => loginXai,
108
109
  oauthStorageKey: () => oauthStorageKey,
110
+ ollamaServedContext: () => ollamaServedContext,
109
111
  openLog: () => openLog,
110
112
  parseLocalModelId: () => parseLocalModelId,
111
113
  pendingClaudeCodeVersionRefresh: () => import_gg_ai.pendingClaudeCodeVersionRefresh,
@@ -1525,6 +1527,7 @@ function isHiddenProvider(provider) {
1525
1527
  }
1526
1528
 
1527
1529
  // src/auth-storage.ts
1530
+ var import_gg_ai3 = require("@kenkaiiii/gg-ai");
1528
1531
  var MOONSHOT_OAUTH_KEY = "moonshot-oauth";
1529
1532
  var XAI_OAUTH_KEY = "xai-oauth";
1530
1533
  var DUAL_AUTH_PROVIDERS = [
@@ -1683,7 +1686,7 @@ var AuthStorage = class {
1683
1686
  */
1684
1687
  async setLocalEndpoint(endpointId, baseUrl, apiKey) {
1685
1688
  await this.setCredentials(`${LOCAL_AUTH_KEY_PREFIX}${endpointId}`, {
1686
- accessToken: apiKey && apiKey.length > 0 ? apiKey : "local",
1689
+ accessToken: apiKey && apiKey.length > 0 ? apiKey : import_gg_ai3.LOCAL_NO_API_KEY,
1687
1690
  refreshToken: "",
1688
1691
  expiresAt: Date.now() + LOCAL_CREDENTIAL_LIFETIME_MS,
1689
1692
  baseUrl
@@ -1825,6 +1828,12 @@ var AuthStorage = class {
1825
1828
  latest[provider] = creds;
1826
1829
  });
1827
1830
  }
1831
+ // Keeps local-endpoint keys: they're the user's own config, and a 401 there is often a busy gateway.
1832
+ async clearRejectedApiKey(storageKey) {
1833
+ if (storageKey.startsWith(LOCAL_AUTH_KEY_PREFIX)) return false;
1834
+ await this.clearCredentials(storageKey);
1835
+ return true;
1836
+ }
1828
1837
  async clearCredentials(provider) {
1829
1838
  await this.mutateLatest((latest) => {
1830
1839
  delete latest[provider];
@@ -3028,11 +3037,12 @@ function clampThinkingForPlanMode(level) {
3028
3037
  }
3029
3038
 
3030
3039
  // src/local-models.ts
3040
+ var import_gg_ai4 = require("@kenkaiiii/gg-ai");
3031
3041
  var DEFAULT_LOCAL_ENDPOINTS = [
3032
3042
  { id: "ollama", label: "Ollama", baseUrl: "http://127.0.0.1:11434/v1", kind: "ollama" }
3033
3043
  ];
3034
3044
  var FALLBACK_CONTEXT_WINDOW = 4096;
3035
- var LOCAL_API_KEY_PLACEHOLDER = "local";
3045
+ var LOCAL_API_KEY_PLACEHOLDER = import_gg_ai4.LOCAL_NO_API_KEY;
3036
3046
  var DEFAULT_PROBE_TIMEOUT_MS = 1200;
3037
3047
  var ENRICH_CONCURRENCY = 6;
3038
3048
  var CACHE_TTL_MS = 3e4;
@@ -3048,6 +3058,9 @@ function parseLocalModelId(id) {
3048
3058
  if (slash <= 0 || slash === rest.length - 1) return void 0;
3049
3059
  return { endpointId: rest.slice(0, slash), rawId: rest.slice(slash + 1) };
3050
3060
  }
3061
+ function localModelShortName(rawId) {
3062
+ return rawId.slice(rawId.lastIndexOf("/") + 1) || rawId;
3063
+ }
3051
3064
  function isLocalModelId(id) {
3052
3065
  return parseLocalModelId(id) !== void 0;
3053
3066
  }
@@ -3058,8 +3071,9 @@ function endpointRoot(baseUrl) {
3058
3071
  return baseUrl.replace(/\/+$/, "").replace(/\/v1$/, "");
3059
3072
  }
3060
3073
  function authHeaders(endpoint) {
3074
+ const key = (0, import_gg_ai4.localBearerKey)(endpoint.apiKey);
3061
3075
  return {
3062
- Authorization: `Bearer ${endpoint.apiKey ?? LOCAL_API_KEY_PLACEHOLDER}`,
3076
+ ...key ? { Authorization: `Bearer ${key}` } : {},
3063
3077
  Accept: "application/json"
3064
3078
  };
3065
3079
  }
@@ -3121,10 +3135,22 @@ async function enrich(endpoint, entries, options) {
3121
3135
  if (endpoint.kind === "lmstudio") return enrichLmStudio(endpoint, entries, options);
3122
3136
  if (endpoint.kind === "ollama") return enrichOllama(endpoint, entries, options);
3123
3137
  if (endpoint.kind === "llamacpp") return enrichLlamaCpp(endpoint, entries, options);
3138
+ if (endpoint.kind === "custom") return enrichCustom(endpoint, entries, options);
3124
3139
  return entries.map((entry) => genericModel(endpoint, entry));
3125
3140
  }
3141
+ async function enrichCustom(endpoint, entries, options) {
3142
+ const generic = entries.map((entry) => genericModel(endpoint, entry));
3143
+ if (generic.every((model) => model.contextWindowKnown)) return generic;
3144
+ const root = endpointRoot(endpoint.baseUrl);
3145
+ const props = await fetchJson(`${root}/props`, endpoint, options);
3146
+ const nCtx = contextLength(props?.default_generation_settings?.n_ctx);
3147
+ if (nCtx !== void 0) return llamaCppModels(endpoint, entries, nCtx);
3148
+ const lmStudio = await fetchJson(`${root}/api/v1/models`, endpoint, options);
3149
+ if (Array.isArray(lmStudio?.models)) return enrichLmStudio(endpoint, entries, options, lmStudio);
3150
+ return generic;
3151
+ }
3126
3152
  function genericModel(endpoint, entry) {
3127
- const declared = contextLength(entry.max_model_len);
3153
+ const declared = contextLength(entry.max_model_len) ?? contextLength(entry.context_length);
3128
3154
  return {
3129
3155
  rawId: entry.id,
3130
3156
  endpointId: endpoint.id,
@@ -3151,9 +3177,7 @@ async function enrichOllama(endpoint, entries, options) {
3151
3177
  });
3152
3178
  const caps = Array.isArray(show?.capabilities) ? show.capabilities : [];
3153
3179
  if (caps.includes("embedding") && !caps.includes("completion")) return void 0;
3154
- const ctx = contextLength(
3155
- runningModels.find((model) => model?.name === entry.id || model?.model === entry.id)?.context_length
3156
- );
3180
+ const ctx = runningContext(runningModels, entry.id) ?? modelfileNumCtx(show?.parameters);
3157
3181
  return {
3158
3182
  rawId: entry.id,
3159
3183
  endpointId: endpoint.id,
@@ -3168,12 +3192,41 @@ async function enrichOllama(endpoint, entries, options) {
3168
3192
  });
3169
3193
  return enriched.filter((model) => model !== void 0);
3170
3194
  }
3195
+ function runningContext(models, rawId) {
3196
+ return contextLength(
3197
+ models.find((model) => model?.name === rawId || model?.model === rawId)?.context_length
3198
+ );
3199
+ }
3200
+ function modelfileNumCtx(parameters) {
3201
+ const match = /^num_ctx\s+(\d+)\s*$/m.exec(parameters ?? "");
3202
+ return match ? contextLength(Number(match[1])) : void 0;
3203
+ }
3204
+ async function ollamaServedContext(endpoint, rawId, {
3205
+ load = false,
3206
+ timeoutMs = 12e4,
3207
+ signal
3208
+ } = {}) {
3209
+ const root = endpointRoot(endpoint.baseUrl);
3210
+ if (load) {
3211
+ await fetchJson(`${root}/api/generate`, endpoint, {
3212
+ timeoutMs,
3213
+ signal,
3214
+ method: "POST",
3215
+ body: { model: rawId }
3216
+ });
3217
+ }
3218
+ const running = await fetchJson(`${root}/api/ps`, endpoint, {
3219
+ timeoutMs: DEFAULT_PROBE_TIMEOUT_MS,
3220
+ signal
3221
+ });
3222
+ return runningContext(Array.isArray(running?.models) ? running.models : [], rawId);
3223
+ }
3171
3224
  function contextLength(value) {
3172
3225
  return typeof value === "number" && Number.isSafeInteger(value) && value > 0 ? value : void 0;
3173
3226
  }
3174
- async function enrichLmStudio(endpoint, entries, options) {
3227
+ async function enrichLmStudio(endpoint, entries, options, fetched) {
3175
3228
  const root = endpointRoot(endpoint.baseUrl);
3176
- const detail = await fetchJson(`${root}/api/v1/models`, endpoint, options);
3229
+ const detail = fetched ?? await fetchJson(`${root}/api/v1/models`, endpoint, options);
3177
3230
  const byId = /* @__PURE__ */ new Map();
3178
3231
  if (Array.isArray(detail?.models)) {
3179
3232
  for (const model of detail.models) {
@@ -3231,12 +3284,17 @@ async function enrichLlamaCpp(endpoint, entries, options) {
3231
3284
  endpoint,
3232
3285
  options
3233
3286
  );
3234
- const nCtx = contextLength(props?.default_generation_settings?.n_ctx);
3235
- const known = nCtx !== void 0;
3287
+ return llamaCppModels(
3288
+ endpoint,
3289
+ entries,
3290
+ contextLength(props?.default_generation_settings?.n_ctx)
3291
+ );
3292
+ }
3293
+ function llamaCppModels(endpoint, entries, nCtx) {
3236
3294
  return entries.map((entry) => ({
3237
3295
  ...genericModel(endpoint, entry),
3238
- contextWindow: known ? nCtx : FALLBACK_CONTEXT_WINDOW,
3239
- contextWindowKnown: known
3296
+ contextWindow: nCtx ?? FALLBACK_CONTEXT_WINDOW,
3297
+ contextWindowKnown: nCtx !== void 0
3240
3298
  }));
3241
3299
  }
3242
3300
  async function mapLimited(items, limit, fn) {
@@ -4285,6 +4343,7 @@ function createAutoUpdater(config) {
4285
4343
  kimiCodeBaseUrl,
4286
4344
  kimiCodingHeaders,
4287
4345
  localAuthStorageKey,
4346
+ localModelShortName,
4288
4347
  log,
4289
4348
  loginAnthropic,
4290
4349
  loginGemini,
@@ -4292,6 +4351,7 @@ function createAutoUpdater(config) {
4292
4351
  loginOpenAI,
4293
4352
  loginXai,
4294
4353
  oauthStorageKey,
4354
+ ollamaServedContext,
4295
4355
  openLog,
4296
4356
  parseLocalModelId,
4297
4357
  pendingClaudeCodeVersionRefresh,