| 1345 | } |
| 1346 | |
| 1347 | func decodeHomeModels(raw []byte) ([]homeModelEntry, error) { |
| 1348 | if len(raw) == 0 { |
| 1349 | return nil, fmt.Errorf("home models payload is empty") |
| 1350 | } |
| 1351 | |
| 1352 | var bySection map[string][]map[string]any |
| 1353 | if err := json.Unmarshal(raw, &bySection); err != nil { |
| 1354 | return nil, fmt.Errorf("parse home models payload: %w", err) |
| 1355 | } |
| 1356 | if len(bySection) == 0 { |
| 1357 | return nil, fmt.Errorf("home models payload has no sections") |
| 1358 | } |
| 1359 | |
| 1360 | seen := make(map[string]struct{}) |
| 1361 | out := make([]homeModelEntry, 0, 256) |
| 1362 | for _, models := range bySection { |
| 1363 | for _, model := range models { |
| 1364 | id, _ := model["id"].(string) |
| 1365 | id = strings.TrimSpace(id) |
| 1366 | if id == "" { |
| 1367 | name, _ := model["name"].(string) |
| 1368 | name = strings.TrimSpace(name) |
| 1369 | id = strings.TrimPrefix(name, "models/") |
| 1370 | } |
| 1371 | if id == "" { |
| 1372 | continue |
| 1373 | } |
| 1374 | if _, ok := seen[id]; ok { |
| 1375 | continue |
| 1376 | } |
| 1377 | seen[id] = struct{}{} |
| 1378 | |
| 1379 | ownedBy, _ := model["owned_by"].(string) |
| 1380 | ownedBy = strings.TrimSpace(ownedBy) |
| 1381 | displayName, _ := model["display_name"].(string) |
| 1382 | displayName = strings.TrimSpace(displayName) |
| 1383 | if displayName == "" { |
| 1384 | displayName, _ = model["displayName"].(string) |
| 1385 | displayName = strings.TrimSpace(displayName) |
| 1386 | } |
| 1387 | |
| 1388 | out = append(out, homeModelEntry{ |
| 1389 | id: id, |
| 1390 | created: homeModelInt64Value(model, "created"), |
| 1391 | ownedBy: ownedBy, |
| 1392 | displayName: displayName, |
| 1393 | contextLength: int(homeModelInt64Value(model, "context_length", "contextLength", "inputTokenLimit", "max_input_tokens")), |
| 1394 | maxCompletionTokens: int(homeModelInt64Value(model, "max_completion_tokens", "maxCompletionTokens", "outputTokenLimit", "max_tokens")), |
| 1395 | }) |
| 1396 | } |
| 1397 | } |
| 1398 | |
| 1399 | sort.Slice(out, func(i, j int) bool { return out[i].id < out[j].id }) |
| 1400 | if len(out) == 0 { |
| 1401 | return nil, fmt.Errorf("home models payload contains no models") |
| 1402 | } |
| 1403 | return out, nil |
| 1404 | } |