Add src/hk_schema.erl
Browse files- src/hk_schema.erl +125 -0
src/hk_schema.erl
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
%%%-------------------------------------------------------------------
|
| 2 |
+
%%% @doc Wire-format conversions for every entity in the data model.
|
| 3 |
+
%%%
|
| 4 |
+
%%% This is the single place a record from `include/hyperkitty.hrl'
|
| 5 |
+
%%% is turned into a JSON-able map. Keeping it centralized means the
|
| 6 |
+
%%% API layer, the event payloads, and the audit view all render a
|
| 7 |
+
%%% given entity identically -- there is exactly one `#browser_tab{}'
|
| 8 |
+
%%% -> map projection, not one per call site.
|
| 9 |
+
%%% @end
|
| 10 |
+
%%%-------------------------------------------------------------------
|
| 11 |
+
-module(hk_schema).
|
| 12 |
+
-include("hyperkitty.hrl").
|
| 13 |
+
|
| 14 |
+
-export([to_map/1]).
|
| 15 |
+
|
| 16 |
+
to_map(#agent{} = A) ->
|
| 17 |
+
#{
|
| 18 |
+
agent_id => A#agent.agent_id,
|
| 19 |
+
objective => opt_map(A#agent.objective),
|
| 20 |
+
state => A#agent.state,
|
| 21 |
+
capabilities => A#agent.capabilities,
|
| 22 |
+
action_count => A#agent.action_count,
|
| 23 |
+
started_at_ms => A#agent.started_at_ms,
|
| 24 |
+
updated_at_ms => A#agent.updated_at_ms,
|
| 25 |
+
limits => A#agent.limits,
|
| 26 |
+
result => term_or_null(A#agent.result),
|
| 27 |
+
failure_reason => term_or_null(A#agent.failure_reason)
|
| 28 |
+
};
|
| 29 |
+
to_map(#agent_objective{} = O) ->
|
| 30 |
+
#{
|
| 31 |
+
objective_id => O#agent_objective.objective_id,
|
| 32 |
+
agent_id => O#agent_objective.agent_id,
|
| 33 |
+
description => O#agent_objective.description,
|
| 34 |
+
success_criteria => bin_or_null(O#agent_objective.success_criteria),
|
| 35 |
+
created_at_ms => O#agent_objective.created_at_ms
|
| 36 |
+
};
|
| 37 |
+
to_map(#browser_session{} = S) ->
|
| 38 |
+
#{
|
| 39 |
+
session_id => S#browser_session.session_id,
|
| 40 |
+
owner_agent_id => bin_or_null(S#browser_session.owner_agent_id),
|
| 41 |
+
profile => S#browser_session.profile,
|
| 42 |
+
status => S#browser_session.status,
|
| 43 |
+
tabs => S#browser_session.tabs,
|
| 44 |
+
created_at_ms => S#browser_session.created_at_ms,
|
| 45 |
+
updated_at_ms => S#browser_session.updated_at_ms
|
| 46 |
+
};
|
| 47 |
+
to_map(#browser_tab{} = T) ->
|
| 48 |
+
#{
|
| 49 |
+
tab_id => T#browser_tab.tab_id,
|
| 50 |
+
session_id => T#browser_tab.session_id,
|
| 51 |
+
url => bin_or_null(T#browser_tab.url),
|
| 52 |
+
title => bin_or_null(T#browser_tab.title),
|
| 53 |
+
status => T#browser_tab.status,
|
| 54 |
+
created_at_ms => T#browser_tab.created_at_ms,
|
| 55 |
+
updated_at_ms => T#browser_tab.updated_at_ms
|
| 56 |
+
};
|
| 57 |
+
to_map(#search_job{} = J) ->
|
| 58 |
+
#{
|
| 59 |
+
job_id => J#search_job.job_id,
|
| 60 |
+
owner_agent_id => bin_or_null(J#search_job.owner_agent_id),
|
| 61 |
+
query => J#search_job.query,
|
| 62 |
+
stage => J#search_job.stage,
|
| 63 |
+
max_iterations => J#search_job.max_iterations,
|
| 64 |
+
iteration => J#search_job.iteration,
|
| 65 |
+
results => J#search_job.results,
|
| 66 |
+
created_at_ms => J#search_job.created_at_ms,
|
| 67 |
+
updated_at_ms => J#search_job.updated_at_ms
|
| 68 |
+
};
|
| 69 |
+
to_map(#search_result{} = R) ->
|
| 70 |
+
#{
|
| 71 |
+
result_id => R#search_result.result_id,
|
| 72 |
+
job_id => R#search_result.job_id,
|
| 73 |
+
url => R#search_result.url,
|
| 74 |
+
normalized_url => R#search_result.normalized_url,
|
| 75 |
+
title => bin_or_null(R#search_result.title),
|
| 76 |
+
snippet => bin_or_null(R#search_result.snippet),
|
| 77 |
+
rank => term_or_null(R#search_result.rank),
|
| 78 |
+
source_document_id => bin_or_null(R#search_result.source_document_id),
|
| 79 |
+
created_at_ms => R#search_result.created_at_ms
|
| 80 |
+
};
|
| 81 |
+
to_map(#source_document{} = D) ->
|
| 82 |
+
#{
|
| 83 |
+
document_id => D#source_document.document_id,
|
| 84 |
+
url => D#source_document.url,
|
| 85 |
+
fetched_at_ms => D#source_document.fetched_at_ms,
|
| 86 |
+
content_type => bin_or_null(D#source_document.content_type),
|
| 87 |
+
extracted_text => bin_or_null(D#source_document.extracted_text),
|
| 88 |
+
citation_map => D#source_document.citation_map
|
| 89 |
+
};
|
| 90 |
+
to_map(#message{} = M) ->
|
| 91 |
+
#{
|
| 92 |
+
message_id => M#message.message_id,
|
| 93 |
+
sender => M#message.sender,
|
| 94 |
+
recipient => M#message.recipient,
|
| 95 |
+
timestamp => M#message.timestamp,
|
| 96 |
+
conversation_id => M#message.conversation_id,
|
| 97 |
+
message_type => M#message.message_type,
|
| 98 |
+
payload => M#message.payload,
|
| 99 |
+
status => M#message.status
|
| 100 |
+
};
|
| 101 |
+
to_map(#operation{} = Op) ->
|
| 102 |
+
#{
|
| 103 |
+
operation_id => Op#operation.operation_id,
|
| 104 |
+
request_id => Op#operation.request_id,
|
| 105 |
+
kind => Op#operation.kind,
|
| 106 |
+
status => Op#operation.status,
|
| 107 |
+
subject => case Op#operation.subject of
|
| 108 |
+
{Type, Id} -> #{type => Type, id => Id};
|
| 109 |
+
undefined -> null
|
| 110 |
+
end,
|
| 111 |
+
error => term_or_null(Op#operation.error),
|
| 112 |
+
created_at_ms => Op#operation.created_at_ms,
|
| 113 |
+
updated_at_ms => Op#operation.updated_at_ms
|
| 114 |
+
}.
|
| 115 |
+
|
| 116 |
+
%%% internal
|
| 117 |
+
|
| 118 |
+
opt_map(undefined) -> null;
|
| 119 |
+
opt_map(Record) -> to_map(Record).
|
| 120 |
+
|
| 121 |
+
bin_or_null(undefined) -> null;
|
| 122 |
+
bin_or_null(B) -> B.
|
| 123 |
+
|
| 124 |
+
term_or_null(undefined) -> null;
|
| 125 |
+
term_or_null(T) -> T.
|