|
| 1 | +import 'dart:convert'; |
| 2 | +import 'dart:io' show Platform; |
| 3 | + |
| 4 | +import 'package:http/http.dart' as http; |
| 5 | + |
| 6 | +import 'alphahuman_error.dart'; |
| 7 | +import 'types.dart'; |
| 8 | + |
| 9 | +class AlphahumanMemoryClient { |
| 10 | + static const _defaultBaseUrl = 'https://staging-api.alphahuman.xyz'; |
| 11 | + |
| 12 | + final String _token; |
| 13 | + final String _baseUrl; |
| 14 | + final http.Client _httpClient; |
| 15 | + final bool _ownsClient; |
| 16 | + |
| 17 | + AlphahumanMemoryClient( |
| 18 | + String token, { |
| 19 | + String? baseUrl, |
| 20 | + http.Client? httpClient, |
| 21 | + }) : _token = token, |
| 22 | + _baseUrl = (baseUrl ?? |
| 23 | + Platform.environment['ALPHAHUMAN_BASE_URL'] ?? |
| 24 | + _defaultBaseUrl) |
| 25 | + .replaceAll(RegExp(r'/+$'), ''), |
| 26 | + _httpClient = httpClient ?? http.Client(), |
| 27 | + _ownsClient = httpClient == null { |
| 28 | + if (token.trim().isEmpty) { |
| 29 | + throw ArgumentError('token is required'); |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + Future<InsertMemoryResponse> insertMemory(InsertMemoryParams params) async { |
| 34 | + params.validate(); |
| 35 | + final result = await _post('/v1/memory/insert', params.toJson()); |
| 36 | + return InsertMemoryResponse.fromJson(result); |
| 37 | + } |
| 38 | + |
| 39 | + Future<RecallMemoryResponse> recallMemory([RecallMemoryParams? params]) async { |
| 40 | + params ??= RecallMemoryParams(); |
| 41 | + params.validate(); |
| 42 | + final result = await _post('/v1/memory/recall', params.toJson()); |
| 43 | + return RecallMemoryResponse.fromJson(result); |
| 44 | + } |
| 45 | + |
| 46 | + Future<DeleteMemoryResponse> deleteMemory([DeleteMemoryParams? params]) async { |
| 47 | + params ??= DeleteMemoryParams(); |
| 48 | + params.validate(); |
| 49 | + final result = await _post('/v1/memory/admin/delete', params.toJson()); |
| 50 | + return DeleteMemoryResponse.fromJson(result); |
| 51 | + } |
| 52 | + |
| 53 | + Future<QueryMemoryResponse> queryMemory(QueryMemoryParams params) async { |
| 54 | + params.validate(); |
| 55 | + final result = await _post('/v1/memory/query', params.toJson()); |
| 56 | + return QueryMemoryResponse.fromJson(result); |
| 57 | + } |
| 58 | + |
| 59 | + Future<RecallMemoriesResponse> recallMemories( |
| 60 | + [RecallMemoriesParams? params]) async { |
| 61 | + params ??= RecallMemoriesParams(); |
| 62 | + params.validate(); |
| 63 | + final result = await _post('/v1/memory/memories/recall', params.toJson()); |
| 64 | + return RecallMemoriesResponse.fromJson(result); |
| 65 | + } |
| 66 | + |
| 67 | + Future<Map<String, dynamic>> _post( |
| 68 | + String path, Map<String, dynamic> body) async { |
| 69 | + final url = Uri.parse('$_baseUrl$path'); |
| 70 | + final response = await _httpClient.post( |
| 71 | + url, |
| 72 | + headers: { |
| 73 | + 'Content-Type': 'application/json', |
| 74 | + 'Authorization': 'Bearer $_token', |
| 75 | + }, |
| 76 | + body: jsonEncode(body), |
| 77 | + ); |
| 78 | + return _handleResponse(response); |
| 79 | + } |
| 80 | + |
| 81 | + Map<String, dynamic> _handleResponse(http.Response response) { |
| 82 | + Map<String, dynamic> json; |
| 83 | + try { |
| 84 | + json = response.body.isNotEmpty |
| 85 | + ? jsonDecode(response.body) as Map<String, dynamic> |
| 86 | + : <String, dynamic>{}; |
| 87 | + } catch (_) { |
| 88 | + throw AlphahumanError( |
| 89 | + 'HTTP ${response.statusCode}: non-JSON response', |
| 90 | + response.statusCode, |
| 91 | + response.body, |
| 92 | + ); |
| 93 | + } |
| 94 | + |
| 95 | + if (response.statusCode < 200 || response.statusCode >= 300) { |
| 96 | + final message = |
| 97 | + json['error'] as String? ?? 'HTTP ${response.statusCode}'; |
| 98 | + throw AlphahumanError(message, response.statusCode, response.body); |
| 99 | + } |
| 100 | + |
| 101 | + return json; |
| 102 | + } |
| 103 | + |
| 104 | + void close() { |
| 105 | + if (_ownsClient) { |
| 106 | + _httpClient.close(); |
| 107 | + } |
| 108 | + } |
| 109 | +} |
0 commit comments