Class: BrainzLab::Dendrite::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/brainzlab/dendrite/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Client

Returns a new instance of Client.



11
12
13
14
# File 'lib/brainzlab/dendrite/client.rb', line 11

def initialize(config)
  @config = config
  @base_url = config.dendrite_url || 'https://dendrite.brainzlab.ai'
end

Instance Method Details

#ask(repo_id, question, session_id: nil) ⇒ Object

Ask a question about the codebase



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/brainzlab/dendrite/client.rb', line 113

def ask(repo_id, question, session_id: nil)
  response = request(
    :post,
    '/api/v1/chat',
    body: {
      repo_id: repo_id,
      question: question,
      session_id: session_id
    }
  )

  return nil unless response.is_a?(Net::HTTPSuccess)

  JSON.parse(response.body, symbolize_names: true)
rescue StandardError => e
  log_error('ask', e)
  nil
end

#connect_repository(url:, name: nil, branch: 'main', **options) ⇒ Object

Connect a repository



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/brainzlab/dendrite/client.rb', line 17

def connect_repository(url:, name: nil, branch: 'main', **options)
  response = request(
    :post,
    '/api/v1/repositories',
    body: {
      url: url,
      name: name,
      branch: branch,
      **options
    }
  )

  return nil unless response.is_a?(Net::HTTPSuccess) || response.is_a?(Net::HTTPCreated)

  JSON.parse(response.body, symbolize_names: true)
rescue StandardError => e
  log_error('connect_repository', e)
  nil
end

#explain(repo_id, path, symbol: nil) ⇒ Object

Explain a specific file or function



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/brainzlab/dendrite/client.rb', line 133

def explain(repo_id, path, symbol: nil)
  response = request(
    :post,
    '/api/v1/explain',
    body: {
      repo_id: repo_id,
      path: path,
      symbol: symbol
    }
  )

  return nil unless response.is_a?(Net::HTTPSuccess)

  JSON.parse(response.body, symbolize_names: true)
rescue StandardError => e
  log_error('explain', e)
  nil
end

#generate_diagram(repo_id, type:, scope: nil) ⇒ Object

Generate a diagram



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/brainzlab/dendrite/client.rb', line 153

def generate_diagram(repo_id, type:, scope: nil)
  response = request(
    :post,
    '/api/v1/diagrams',
    body: {
      repo_id: repo_id,
      type: type, # class, er, sequence, architecture
      scope: scope
    }
  )

  return nil unless response.is_a?(Net::HTTPSuccess)

  JSON.parse(response.body, symbolize_names: true)
rescue StandardError => e
  log_error('generate_diagram', e)
  nil
end

#get_repository(repo_id) ⇒ Object

Get repository status



47
48
49
50
51
52
53
54
55
56
# File 'lib/brainzlab/dendrite/client.rb', line 47

def get_repository(repo_id)
  response = request(:get, "/api/v1/repositories/#{repo_id}")

  return nil unless response.is_a?(Net::HTTPSuccess)

  JSON.parse(response.body, symbolize_names: true)
rescue StandardError => e
  log_error('get_repository', e)
  nil
end

#get_wiki(repo_id) ⇒ Object

Get wiki pages for a repository



72
73
74
75
76
77
78
79
80
81
# File 'lib/brainzlab/dendrite/client.rb', line 72

def get_wiki(repo_id)
  response = request(:get, "/api/v1/wiki/#{repo_id}")

  return nil unless response.is_a?(Net::HTTPSuccess)

  JSON.parse(response.body, symbolize_names: true)
rescue StandardError => e
  log_error('get_wiki', e)
  nil
end

#get_wiki_page(repo_id, page_slug) ⇒ Object

Get a specific wiki page



84
85
86
87
88
89
90
91
92
93
# File 'lib/brainzlab/dendrite/client.rb', line 84

def get_wiki_page(repo_id, page_slug)
  response = request(:get, "/api/v1/wiki/#{repo_id}/#{CGI.escape(page_slug)}")

  return nil unless response.is_a?(Net::HTTPSuccess)

  JSON.parse(response.body, symbolize_names: true)
rescue StandardError => e
  log_error('get_wiki_page', e)
  nil
end

#list_repositoriesObject

List repositories



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/brainzlab/dendrite/client.rb', line 59

def list_repositories
  response = request(:get, '/api/v1/repositories')

  return [] unless response.is_a?(Net::HTTPSuccess)

  data = JSON.parse(response.body, symbolize_names: true)
  data[:repositories] || []
rescue StandardError => e
  log_error('list_repositories', e)
  []
end

#provision(project_id:, app_name:) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/brainzlab/dendrite/client.rb', line 172

def provision(project_id:, app_name:)
  response = request(
    :post,
    '/api/v1/projects/provision',
    body: { project_id: project_id, app_name: app_name },
    use_service_key: true
  )

  response.is_a?(Net::HTTPSuccess) || response.is_a?(Net::HTTPCreated)
rescue StandardError => e
  log_error('provision', e)
  false
end

#search(repo_id, query, limit: 10) ⇒ Object

Semantic search across codebase



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/brainzlab/dendrite/client.rb', line 96

def search(repo_id, query, limit: 10)
  response = request(
    :get,
    '/api/v1/search',
    params: { repo_id: repo_id, q: query, limit: limit }
  )

  return [] unless response.is_a?(Net::HTTPSuccess)

  data = JSON.parse(response.body, symbolize_names: true)
  data[:results] || []
rescue StandardError => e
  log_error('search', e)
  []
end

#sync_repository(repo_id) ⇒ Object

Trigger sync for a repository



38
39
40
41
42
43
44
# File 'lib/brainzlab/dendrite/client.rb', line 38

def sync_repository(repo_id)
  response = request(:post, "/api/v1/repositories/#{repo_id}/sync")
  response.is_a?(Net::HTTPSuccess) || response.is_a?(Net::HTTPAccepted)
rescue StandardError => e
  log_error('sync_repository', e)
  false
end