calmlens 0.29.2 → 0.31.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.
- checksums.yaml +4 -4
- data/lib/calmlens/assets.rb +3 -14
- data/lib/calmlens/error_codes.rb +2 -0
- data/lib/calmlens/events.rb +9 -6
- data/lib/calmlens/models.rb +10 -5
- data/lib/calmlens/testing/fake_api.rb +57 -14
- data/lib/calmlens/testing.rb +6 -4
- data/lib/calmlens/version.rb +1 -1
- data/lib/calmlens/webhooks.rb +32 -25
- data/lib/calmlens.rb +0 -1
- data/sig/calmlens.rbs +3 -13
- data/sig/error_codes.rbs +1 -0
- data/sig/models.rbs +8 -5
- data/sig/types.rbs +6 -12
- metadata +2 -3
- data/lib/calmlens/moderation_context.rb +0 -55
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 45077d27cc23f4fbfbae7a056b96b6ce575a5d0ba2e306ff0d96daacb9406dd9
|
|
4
|
+
data.tar.gz: 321e112bc8d3efab4dd12e9c32c7118335c5759442a2a20883677fbc77ee779a
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 3f9cb8b271474880af0d0e54853a935faac758ac167d38109cc16fb2910c3fc7a376217d3764b832991eab19d0c86a0c80a199baa48911ef5dc8791ba97bdc8a
|
|
7
|
+
data.tar.gz: b7e71f082a6bbe59529f2c49bf46b0a02aae43316b2bf390801e4a791e72272dc31627d0289c4ad6f3550c396f6703aa22043a4b09a1d9e1e7178c80f50d647a
|
data/lib/calmlens/assets.rb
CHANGED
|
@@ -285,9 +285,9 @@ module CalmLens
|
|
|
285
285
|
# @option options [Array<String>] :tags tags for the asset, such as `['catalogue', 'backpacks']`
|
|
286
286
|
# @option options [String] :author_id your identifier for who wrote the content; CalmLens only
|
|
287
287
|
# compares it with the author IDs in `context`, to tell which posts share a speaker.
|
|
288
|
-
# @option options [Hash] :context `{items: [{
|
|
289
|
-
#
|
|
290
|
-
#
|
|
288
|
+
# @option options [Hash] :context `{items: [{asset_id:}]}`, the earlier posts in the conversation,
|
|
289
|
+
# oldest first, as references to their assets in this project. CalmLens reads each post's text
|
|
290
|
+
# and author from its asset and uses the most recent ones it needs.
|
|
291
291
|
# @return [Asset]
|
|
292
292
|
def create(**options)
|
|
293
293
|
call(options, __method__) { |api, request| submit(api, request, false) }
|
|
@@ -387,7 +387,6 @@ module CalmLens
|
|
|
387
387
|
raise Internal.invalid('Supply exactly one of content, part, or url; isFinal belongs in part',
|
|
388
388
|
ErrorCodes::INVALID_UPLOAD_PAYLOAD)
|
|
389
389
|
end
|
|
390
|
-
options = with_used_context(options)
|
|
391
390
|
if options.key?('url')
|
|
392
391
|
return expect_asset(request('POST', project('assets'), query: { 'wait' => wait }, body: options,
|
|
393
392
|
idempotent: true, request_options: request_options))
|
|
@@ -503,16 +502,6 @@ module CalmLens
|
|
|
503
502
|
headers: headers))
|
|
504
503
|
end
|
|
505
504
|
|
|
506
|
-
# Keeps only the earlier posts CalmLens uses, which keeps an upload's body short.
|
|
507
|
-
def with_used_context(options)
|
|
508
|
-
return options unless options.key?('context')
|
|
509
|
-
|
|
510
|
-
context = options['context']
|
|
511
|
-
raise Internal.invalid('context must be a Hash with items') unless context.is_a?(Hash) && context.key?('items')
|
|
512
|
-
|
|
513
|
-
options.merge('context' => { 'items' => ModerationContext.select_items(context['items']) })
|
|
514
|
-
end
|
|
515
|
-
|
|
516
505
|
# The transport values of a first multipart part: its number, whether it is the last and the
|
|
517
506
|
# declared total.
|
|
518
507
|
def first_part_query(options, content, mime, body)
|
data/lib/calmlens/error_codes.rb
CHANGED
|
@@ -119,6 +119,7 @@ module CalmLens
|
|
|
119
119
|
REMOTE_URL_UNAVAILABLE = 'remote-url/unavailable'
|
|
120
120
|
APPEAL_NOT_FOUND = 'appeal/not-found'
|
|
121
121
|
APPEAL_JUDGMENT_NOT_FOUND = 'appeal/judgment-not-found'
|
|
122
|
+
CONTEXT_ASSET_NOT_FOUND = 'context/asset-not-found'
|
|
122
123
|
APPEAL_ASSET_NOT_FOUND = 'appeal/asset-not-found'
|
|
123
124
|
APPEAL_PERMISSION_DENIED = 'appeal/permission-denied'
|
|
124
125
|
APPEAL_ALREADY_REVIEWED = 'appeal/already-reviewed'
|
|
@@ -242,6 +243,7 @@ module CalmLens
|
|
|
242
243
|
'remote-url/unavailable' => 422,
|
|
243
244
|
'appeal/not-found' => 404,
|
|
244
245
|
'appeal/judgment-not-found' => 404,
|
|
246
|
+
'context/asset-not-found' => 400,
|
|
245
247
|
'appeal/asset-not-found' => 404,
|
|
246
248
|
'appeal/permission-denied' => 403,
|
|
247
249
|
'appeal/already-reviewed' => 409,
|
data/lib/calmlens/events.rb
CHANGED
|
@@ -36,9 +36,10 @@ module CalmLens
|
|
|
36
36
|
end
|
|
37
37
|
|
|
38
38
|
# Retrieves the asset an event is about, as it is now, such as one from {#list} or a parsed
|
|
39
|
-
# webhook delivery; events carry only the asset's ID
|
|
40
|
-
# sends. The asset's `
|
|
41
|
-
# `
|
|
39
|
+
# webhook delivery; events carry only the asset's ID, in `resource`. It sends the request
|
|
40
|
+
# {Assets#retrieve} sends. The asset's `sequence` orders it against events: an event with a
|
|
41
|
+
# higher `resource.sequence` happened after this state, one with an equal or lower one is
|
|
42
|
+
# reflected in it.
|
|
42
43
|
# @param event [Event, Hash] an event, or an event or delivery body with string keys
|
|
43
44
|
# @param request_options [Hash, nil]
|
|
44
45
|
# @return [Asset, nil] nil for an event that isn't about an asset, without sending a request
|
|
@@ -46,9 +47,11 @@ module CalmLens
|
|
|
46
47
|
def retrieve_asset(event, request_options: nil)
|
|
47
48
|
raise Internal.invalid('event must be an Event or a Hash') unless event.is_a?(Event) || event.is_a?(Hash)
|
|
48
49
|
|
|
49
|
-
|
|
50
|
-
return nil
|
|
51
|
-
|
|
50
|
+
resource = event['resource']
|
|
51
|
+
return nil unless (resource.is_a?(Hash) || resource.is_a?(Model)) && resource['kind'] == 'asset'
|
|
52
|
+
|
|
53
|
+
asset_id = resource['id']
|
|
54
|
+
raise Internal.invalid('event resource id must be a String') unless asset_id.is_a?(String)
|
|
52
55
|
|
|
53
56
|
@assets.retrieve(asset_id, request_options: request_options)
|
|
54
57
|
end
|
data/lib/calmlens/models.rb
CHANGED
|
@@ -50,6 +50,7 @@ module CalmLens
|
|
|
50
50
|
class Classification::Progress < Model; end
|
|
51
51
|
class Event::Actor < Model; end
|
|
52
52
|
class Event::Fact < Model; end
|
|
53
|
+
class Event::Resource < Model; end
|
|
53
54
|
class MetadataFieldsResource::Field < Model; end
|
|
54
55
|
class Moderation::ManualReview < Model; end
|
|
55
56
|
class Moderation::PendingReview < Model; end
|
|
@@ -108,7 +109,7 @@ module CalmLens
|
|
|
108
109
|
upload: ["upload", [:model, "CalmLens::Asset::Upload"]],
|
|
109
110
|
classification: ["classification", [:model, "CalmLens::Classification"]],
|
|
110
111
|
content: ["content", [:model, "CalmLens::Asset::Content"]],
|
|
111
|
-
|
|
112
|
+
sequence: ["sequence", nil]
|
|
112
113
|
)
|
|
113
114
|
|
|
114
115
|
AssetBatch.declare(
|
|
@@ -209,8 +210,7 @@ module CalmLens
|
|
|
209
210
|
severity: ["severity", nil],
|
|
210
211
|
title: ["title", nil],
|
|
211
212
|
description: ["description", nil],
|
|
212
|
-
|
|
213
|
-
asset_sequence: ["assetSequence", nil],
|
|
213
|
+
resource: ["resource", [:model, "CalmLens::Event::Resource"]],
|
|
214
214
|
facts: ["facts", [:array, [:model, "CalmLens::Event::Fact"]]],
|
|
215
215
|
created_at: ["createdAt", nil]
|
|
216
216
|
)
|
|
@@ -489,6 +489,12 @@ module CalmLens
|
|
|
489
489
|
url: ["url", nil]
|
|
490
490
|
)
|
|
491
491
|
|
|
492
|
+
Event::Resource.declare(
|
|
493
|
+
kind: ["kind", nil],
|
|
494
|
+
id: ["id", nil],
|
|
495
|
+
sequence: ["sequence", nil]
|
|
496
|
+
)
|
|
497
|
+
|
|
492
498
|
MetadataFieldsResource::Field.declare(
|
|
493
499
|
id: ["id", nil],
|
|
494
500
|
title: ["title", nil],
|
|
@@ -615,8 +621,7 @@ module CalmLens
|
|
|
615
621
|
)
|
|
616
622
|
|
|
617
623
|
Asset::Context::Item.declare(
|
|
618
|
-
|
|
619
|
-
author_id: ["authorId", nil]
|
|
624
|
+
asset_id: ["assetId", nil]
|
|
620
625
|
)
|
|
621
626
|
|
|
622
627
|
Classification::FaceDetection::Face.declare(
|
|
@@ -51,6 +51,11 @@ module CalmLens
|
|
|
51
51
|
'content.format' => %w[content format], 'content.extension' => %w[content extension],
|
|
52
52
|
'classification.moderation.decision' => %w[classification moderation decision] }.freeze
|
|
53
53
|
MAX_SEARCH_TOKENS = 8
|
|
54
|
+
# Like the API, a reply's context uses the latest earlier posts within these limits, reading at
|
|
55
|
+
# most CONTEXT_MAX_ITEM_CHARACTERS (Unicode code points) of each.
|
|
56
|
+
CONTEXT_USED_ITEMS = 5
|
|
57
|
+
CONTEXT_USED_CHARACTERS = 2_000
|
|
58
|
+
CONTEXT_MAX_ITEM_CHARACTERS = 2_000
|
|
54
59
|
MAX_SEARCH_PAGE_SIZE = 20
|
|
55
60
|
MAX_SEARCH_QUERY_LENGTH = 200
|
|
56
61
|
# Every API cursor list returns this many items unless `pageSize` says otherwise.
|
|
@@ -174,8 +179,8 @@ module CalmLens
|
|
|
174
179
|
@metadata_fields = []
|
|
175
180
|
@settings_revision = 1
|
|
176
181
|
@request_count = 0
|
|
177
|
-
# Each asset's latest `
|
|
178
|
-
# `
|
|
182
|
+
# Each asset's latest `resource.sequence`, which the API keeps on the asset row and returns as
|
|
183
|
+
# its `sequence`. Kept here as well as on stored assets, which a deleted asset's events outlive.
|
|
179
184
|
@asset_event_sequences = Hash.new(0)
|
|
180
185
|
# The processing options each asset was uploaded with, which the API keeps but doesn't return.
|
|
181
186
|
@processing_options = {}
|
|
@@ -204,20 +209,20 @@ module CalmLens
|
|
|
204
209
|
end
|
|
205
210
|
|
|
206
211
|
# Adds an event to the feed, filling the fields the API always sets. Its `actor` is the mock's
|
|
207
|
-
# API key unless the event names another.
|
|
212
|
+
# API key unless the event names another. With an `assetId` it is about that asset; without,
|
|
213
|
+
# about no resource.
|
|
208
214
|
def add_event(event)
|
|
209
215
|
time = @now.call
|
|
216
|
+
asset_id = event['assetId']
|
|
210
217
|
record = { 'id' => @create_id.call('evt'), 'projectId' => @project_id, 'actor' => deep_copy(API_KEY_ACTOR),
|
|
211
|
-
'severity' => 'low', 'description' => nil,
|
|
212
|
-
'
|
|
213
|
-
'createdAt' => time }.merge(event)
|
|
218
|
+
'severity' => 'low', 'description' => nil, 'facts' => [],
|
|
219
|
+
'createdAt' => time }.merge(event.except('assetId', 'resource'))
|
|
214
220
|
record['title'] ||= record['type']
|
|
215
221
|
# Like the API: each event about an asset takes the asset's next number; others have none.
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
# The asset's `eventSequence` follows, so every copy of it returned from now on has it.
|
|
222
|
+
record['resource'] = asset_id ? { 'kind' => 'asset', 'id' => asset_id, 'sequence' => (@asset_event_sequences[asset_id] += 1) } : nil
|
|
223
|
+
# The asset's `sequence` follows, so every copy of it returned from now on has it.
|
|
219
224
|
stored = asset_id && @state.assets[asset_id]
|
|
220
|
-
stored['
|
|
225
|
+
stored['sequence'] = record['resource']['sequence'] if stored
|
|
221
226
|
@state.events << record
|
|
222
227
|
record
|
|
223
228
|
end
|
|
@@ -673,7 +678,9 @@ module CalmLens
|
|
|
673
678
|
asset_id = query['assetId']
|
|
674
679
|
invalid!('validation/invalid-format', 'assetId') if asset_id && !asset_id.match?(ASSET_ID_PATTERN)
|
|
675
680
|
filter = [JSON.generate([types.uniq.sort, asset_id])].pack('m0')
|
|
676
|
-
matches =
|
|
681
|
+
matches = lambda { |event|
|
|
682
|
+
(types.empty? || types.include?(event['type'])) && (asset_id.nil? || event.dig('resource', 'id') == asset_id)
|
|
683
|
+
}
|
|
677
684
|
feed = @state.events
|
|
678
685
|
unless query['cursor']
|
|
679
686
|
return { 'items' => [], 'nextCursor' => "mock_#{feed.size}~#{filter}", 'hasMore' => false, 'excludedTypes' => [] }
|
|
@@ -958,7 +965,7 @@ module CalmLens
|
|
|
958
965
|
processing = (options['processingOptions'] || {}).dup
|
|
959
966
|
processing['retainContent'] = show_project_settings['settings']['retainContent'] if processing['retainContent'].nil?
|
|
960
967
|
keep = processing.is_a?(Hash) && processing['retainContent'] == true
|
|
961
|
-
context = options['context'].is_a?(Hash) ?
|
|
968
|
+
context = options['context'].is_a?(Hash) ? select_context(options['context'].fetch('items', [])) : []
|
|
962
969
|
# Fields the API returns as null when they are not set.
|
|
963
970
|
asset = { 'id' => @create_id.call('asset'), 'projectId' => @project_id, 'parentId' => nil, 'parentPages' => [],
|
|
964
971
|
'folderId' => options['folderId'], 'uploadPresetId' => options['uploadPresetId'],
|
|
@@ -969,7 +976,7 @@ module CalmLens
|
|
|
969
976
|
'context' => context.empty? ? nil : { 'items' => context }, 'tags' => options['tags'] || [],
|
|
970
977
|
'visibility' => options.fetch('visibility', 'private'),
|
|
971
978
|
# Set as the asset's events are added (add_event).
|
|
972
|
-
'
|
|
979
|
+
'sequence' => 0,
|
|
973
980
|
**sections(now: now, open: open, keep: keep, format: mime, size: data&.bytesize) }
|
|
974
981
|
@state.assets[asset['id']] = asset
|
|
975
982
|
@processing_options[asset['id']] = processing
|
|
@@ -1246,6 +1253,42 @@ module CalmLens
|
|
|
1246
1253
|
asset
|
|
1247
1254
|
end
|
|
1248
1255
|
|
|
1256
|
+
# Like the API, a reply's context references the project's own assets, each read for its text:
|
|
1257
|
+
# a text asset's own text or the text found in it. Only the references CalmLens uses are kept:
|
|
1258
|
+
# the most recent ones, newest first, while CONTEXT_USED_ITEMS posts and
|
|
1259
|
+
# CONTEXT_USED_CHARACTERS characters allow, returned oldest first. The newest is always kept.
|
|
1260
|
+
def select_context(items)
|
|
1261
|
+
posts = Array(items).map do |item|
|
|
1262
|
+
asset_id = item.is_a?(Hash) ? item['assetId'] : nil
|
|
1263
|
+
asset = asset_id.is_a?(String) ? @state.assets[asset_id] : nil
|
|
1264
|
+
fail!(ErrorCodes::CONTEXT_ASSET_NOT_FOUND, details: { 'assetId' => asset_id }) unless asset && asset['projectId'] == @project_id
|
|
1265
|
+
[{ 'assetId' => asset_id }, context_text(asset).length]
|
|
1266
|
+
end
|
|
1267
|
+
selected = []
|
|
1268
|
+
characters = 0
|
|
1269
|
+
posts.reverse_each do |item, length|
|
|
1270
|
+
break if selected.size == CONTEXT_USED_ITEMS || (!selected.empty? && characters + length > CONTEXT_USED_CHARACTERS)
|
|
1271
|
+
|
|
1272
|
+
selected.unshift(item)
|
|
1273
|
+
characters += length
|
|
1274
|
+
end
|
|
1275
|
+
selected
|
|
1276
|
+
end
|
|
1277
|
+
|
|
1278
|
+
# An earlier post's text, to the most CalmLens reads of one post; empty once its content is gone.
|
|
1279
|
+
def context_text(asset)
|
|
1280
|
+
bytes = asset['type'] == 'plaintext-document' ? stored_bytes(asset) : nil
|
|
1281
|
+
extracted = asset['content']['extractedText']
|
|
1282
|
+
text = if bytes && !bytes.empty?
|
|
1283
|
+
bytes.dup.force_encoding(Encoding::UTF_8).scrub
|
|
1284
|
+
elsif extracted.is_a?(Hash) && extracted['type'] == 'string'
|
|
1285
|
+
extracted['content'].to_s
|
|
1286
|
+
else
|
|
1287
|
+
''
|
|
1288
|
+
end
|
|
1289
|
+
text.strip[0, CONTEXT_MAX_ITEM_CHARACTERS]
|
|
1290
|
+
end
|
|
1291
|
+
|
|
1249
1292
|
# An asset's stored content, its parts in order, or nil once it is removed.
|
|
1250
1293
|
def stored_bytes(asset)
|
|
1251
1294
|
stored = asset['content']['status'] == 'removed' ? nil : @state.content[asset['id']]
|
|
@@ -1465,7 +1508,7 @@ module CalmLens
|
|
|
1465
1508
|
holder = project_assets.find { |other| other['id'] != item['id'] && other['externalId'] == json['externalId'] }
|
|
1466
1509
|
duplicate_external_id!(holder) if holder
|
|
1467
1510
|
end
|
|
1468
|
-
touch(item.merge!(json.except('id', 'projectId', 'upload', 'classification', 'content', '
|
|
1511
|
+
touch(item.merge!(json.except('id', 'projectId', 'upload', 'classification', 'content', 'sequence')))
|
|
1469
1512
|
end
|
|
1470
1513
|
|
|
1471
1514
|
# Like the API: the record and report stay; stored content, extracted text, metadata, the
|
data/lib/calmlens/testing.rb
CHANGED
|
@@ -335,10 +335,12 @@ module CalmLens
|
|
|
335
335
|
end
|
|
336
336
|
|
|
337
337
|
# Adds an event to the mock's events feed. Fields you omit (`id`, `projectId`, `createdAt`,
|
|
338
|
-
# `severity`, `title`, `facts`, and nil `description`
|
|
339
|
-
#
|
|
340
|
-
# `actor: { type: 'user', id: nil }`. An event with an `asset_id`
|
|
341
|
-
# `
|
|
338
|
+
# `severity`, `title`, `facts`, and nil `description`) are filled in, and `actor` is the mock
|
|
339
|
+
# API key (`{ 'type' => 'api-key', 'id' => API_KEY_ID }`) unless you pass one, such as
|
|
340
|
+
# `actor: { type: 'user', id: nil }`. An event with an `asset_id` is about that asset: its
|
|
341
|
+
# `resource` is `{ 'kind' => 'asset', 'id' => asset_id, 'sequence' => n }`, with the asset's next
|
|
342
|
+
# number, which becomes the stored asset's `sequence`; it changes nothing else. Without one,
|
|
343
|
+
# its `resource` is nil.
|
|
342
344
|
# @param event [Hash] at least `type:`; snake_case keys are converted.
|
|
343
345
|
# @return [Event] the stored event
|
|
344
346
|
def enqueue_event(event)
|
data/lib/calmlens/version.rb
CHANGED
data/lib/calmlens/webhooks.rb
CHANGED
|
@@ -15,17 +15,18 @@ module CalmLens
|
|
|
15
15
|
WEBHOOK_SIGNATURE_TOLERANCE_SECONDS = 300
|
|
16
16
|
|
|
17
17
|
# An event from {Events#list} or a verified webhook delivery ({Webhooks.parse_event}). `type` is
|
|
18
|
-
# such as `asset.allowed`; new types may appear, so ignore unknown ones. `
|
|
19
|
-
#
|
|
18
|
+
# such as `asset.allowed`; new types may appear, so ignore unknown ones. `resource` is the asset,
|
|
19
|
+
# membership or API key the event is about (`kind`, `id`, `sequence`), or nil for events about no
|
|
20
|
+
# resource; new kinds may appear. Its `sequence` increases with each event about the same resource;
|
|
20
21
|
# deliveries can arrive out of order, see {#newer_than?}. {#to_h} is the body as received.
|
|
21
22
|
class Event
|
|
22
23
|
# @return [Boolean] true for a test send from the portal or CLI.
|
|
23
24
|
def test? = Webhooks.test_event?(self)
|
|
24
25
|
|
|
25
|
-
# @param last_sequence [Integer, nil] the highest `
|
|
26
|
+
# @param last_sequence [Integer, nil] the highest `resource.sequence` applied for this event's resource
|
|
26
27
|
# @return [Boolean] whether to apply this event after it.
|
|
27
|
-
# @see Webhooks.
|
|
28
|
-
def newer_than?(last_sequence) = Webhooks.
|
|
28
|
+
# @see Webhooks.newer_event?
|
|
29
|
+
def newer_than?(last_sequence) = Webhooks.newer_event?(self, last_sequence)
|
|
29
30
|
end
|
|
30
31
|
|
|
31
32
|
# Webhook signing and verification. Always verify the exact bytes received: read the raw body
|
|
@@ -69,25 +70,25 @@ module CalmLens
|
|
|
69
70
|
facts.is_a?(Array) && facts.any? { |fact| (fact.is_a?(Hash) || fact.is_a?(Model)) && fact['key'] == 'test' && fact['value'] == true }
|
|
70
71
|
end
|
|
71
72
|
|
|
72
|
-
# Whether to apply `event` after `last_sequence`, the highest `
|
|
73
|
-
# for its
|
|
74
|
-
# newer only with a higher
|
|
75
|
-
# returns false. Events
|
|
76
|
-
#
|
|
73
|
+
# Whether to apply `event` after `last_sequence`, the highest `resource.sequence` you have applied
|
|
74
|
+
# for its resource (nil when none). Deliveries can arrive out of order: an event about a resource
|
|
75
|
+
# is newer only with a higher sequence, so a retried or repeated delivery of an earlier one
|
|
76
|
+
# returns false. Events about no resource have no sequence and are always new. Store the highest
|
|
77
|
+
# `resource.sequence` applied per `resource.id` and ignore lower ones.
|
|
77
78
|
# @param event [Event, Hash] a parsed delivery, or a delivery body with string keys
|
|
78
79
|
# @param last_sequence [Integer, nil]
|
|
79
80
|
# @return [Boolean]
|
|
80
|
-
def
|
|
81
|
-
|
|
82
|
-
|
|
81
|
+
def newer_event?(event, last_sequence)
|
|
82
|
+
resource = event['resource']
|
|
83
|
+
resource.nil? || last_sequence.nil? || resource['sequence'] > last_sequence
|
|
83
84
|
end
|
|
84
85
|
|
|
85
86
|
# Converts an event from {Events#list} to the JSON body of its webhook delivery, which has the
|
|
86
|
-
# same fields: `description
|
|
87
|
-
#
|
|
87
|
+
# same fields: `description` and `resource` are nil when absent, and `actor` is left out when
|
|
88
|
+
# the event has none.
|
|
88
89
|
# @param event [Event, Hash] an {Events#list} item, or an event with string keys
|
|
89
|
-
# @return [Hash] `{ 'id', 'projectId', 'actor', 'type', 'severity', 'title', 'description', '
|
|
90
|
-
# '
|
|
90
|
+
# @return [Hash] `{ 'id', 'projectId', 'actor', 'type', 'severity', 'title', 'description', 'resource',
|
|
91
|
+
# 'facts', 'createdAt' }`
|
|
91
92
|
def to_payload(event)
|
|
92
93
|
event = event.to_h if event.is_a?(Model)
|
|
93
94
|
raise Internal.invalid('event must be an Event or a Hash') unless event.is_a?(Hash)
|
|
@@ -95,9 +96,8 @@ module CalmLens
|
|
|
95
96
|
payload = { 'id' => event['id'], 'projectId' => event['projectId'] }
|
|
96
97
|
payload['actor'] = event['actor'] if event.key?('actor')
|
|
97
98
|
payload.merge('type' => event['type'], 'severity' => event['severity'], 'title' => event['title'],
|
|
98
|
-
'description' => event['description'], '
|
|
99
|
-
'
|
|
100
|
-
'createdAt' => event['createdAt'])
|
|
99
|
+
'description' => event['description'], 'resource' => event['resource'],
|
|
100
|
+
'facts' => event['facts'], 'createdAt' => event['createdAt'])
|
|
101
101
|
end
|
|
102
102
|
|
|
103
103
|
# Checks a delivery's signature and freshness in constant time. Pass the `signature` and
|
|
@@ -171,17 +171,24 @@ module CalmLens
|
|
|
171
171
|
|
|
172
172
|
optional = ->(key, *types) { !body.key?(key) || types.any? { |type| type.nil? ? body[key].nil? : body[key].is_a?(type) } }
|
|
173
173
|
optional.call('severity', String) && optional.call('title', String) && optional.call('description', String, nil) &&
|
|
174
|
-
|
|
174
|
+
nested?(body)
|
|
175
175
|
end
|
|
176
176
|
|
|
177
177
|
# The nested fields, when present: `actor` is `{type, id}` with a String type and a String or null
|
|
178
|
-
# ID
|
|
178
|
+
# ID, `resource` is null or `{kind, id, sequence}` with any String kind, and `facts` lists
|
|
179
|
+
# `{key, value, url?}` entries.
|
|
179
180
|
def nested?(body)
|
|
180
181
|
actor = body.fetch('actor', { 'type' => '' })
|
|
181
182
|
actor.is_a?(Hash) && actor['type'].is_a?(String) && (actor['id'].nil? || actor['id'].is_a?(String)) &&
|
|
183
|
+
resource?(body['resource']) &&
|
|
182
184
|
(!body.key?('facts') || (body['facts'].is_a?(Array) && body['facts'].all? { |fact| fact?(fact) }))
|
|
183
185
|
end
|
|
184
186
|
|
|
187
|
+
def resource?(resource)
|
|
188
|
+
resource.nil? ||
|
|
189
|
+
(resource.is_a?(Hash) && resource['kind'].is_a?(String) && resource['id'].is_a?(String) && resource['sequence'].is_a?(Numeric))
|
|
190
|
+
end
|
|
191
|
+
|
|
185
192
|
def fact?(fact)
|
|
186
193
|
fact.is_a?(Hash) && fact['key'].is_a?(String) && fact.key?('value') && (!fact.key?('url') || fact['url'].is_a?(String))
|
|
187
194
|
end
|
|
@@ -204,7 +211,7 @@ module CalmLens
|
|
|
204
211
|
raise Internal.invalid("Webhook payload must be the raw body String, not a #{payload.class}. " \
|
|
205
212
|
'Read request.raw_post (Rails) or request.body.read (Rack, Sinatra) before parsing.')
|
|
206
213
|
end
|
|
207
|
-
private_class_method :decode_event, :event?, :nested?, :fact?, :header, :raw_body
|
|
214
|
+
private_class_method :decode_event, :event?, :nested?, :resource?, :fact?, :header, :raw_body
|
|
208
215
|
end
|
|
209
216
|
|
|
210
217
|
# @see Webhooks.signature_header
|
|
@@ -228,8 +235,8 @@ module CalmLens
|
|
|
228
235
|
# @see Webhooks.test_event?
|
|
229
236
|
def self.test_webhook_event?(...) = Webhooks.test_event?(...)
|
|
230
237
|
|
|
231
|
-
# @see Webhooks.
|
|
232
|
-
def self.
|
|
238
|
+
# @see Webhooks.newer_event?
|
|
239
|
+
def self.newer_event?(...) = Webhooks.newer_event?(...)
|
|
233
240
|
|
|
234
241
|
# @see EventFacts.value
|
|
235
242
|
def self.get_event_fact(...) = EventFacts.value(...)
|
data/lib/calmlens.rb
CHANGED
|
@@ -22,7 +22,6 @@ require_relative 'calmlens/transport'
|
|
|
22
22
|
require_relative 'calmlens/upload_body'
|
|
23
23
|
require_relative 'calmlens/session'
|
|
24
24
|
require_relative 'calmlens/resource'
|
|
25
|
-
require_relative 'calmlens/moderation_context'
|
|
26
25
|
require_relative 'calmlens/assets'
|
|
27
26
|
require_relative 'calmlens/folders'
|
|
28
27
|
require_relative 'calmlens/hash_lists'
|
data/sig/calmlens.rbs
CHANGED
|
@@ -224,16 +224,6 @@ module CalmLens
|
|
|
224
224
|
private def initialize: (untyped session) -> void
|
|
225
225
|
end
|
|
226
226
|
|
|
227
|
-
# The earlier posts CalmLens uses from an upload's `context`.
|
|
228
|
-
module ModerationContext
|
|
229
|
-
# The most recent earlier posts CalmLens uses.
|
|
230
|
-
USED_ITEMS: Integer
|
|
231
|
-
# The most characters (Unicode code points) of earlier posts CalmLens uses.
|
|
232
|
-
USED_CHARACTERS: Integer
|
|
233
|
-
# The most recent posts within USED_ITEMS and USED_CHARACTERS, oldest first; the newest is always kept.
|
|
234
|
-
def self.select_items: (Array[moderation_context_item_input | Hash[String | Symbol, untyped]] items) -> Array[moderation_context_item]
|
|
235
|
-
end
|
|
236
|
-
|
|
237
227
|
class Assets < Resource
|
|
238
228
|
# The classification statuses of a settled asset; wait_for_decision returns at the first.
|
|
239
229
|
SETTLED_STATUSES: Array[String]
|
|
@@ -374,7 +364,7 @@ module CalmLens
|
|
|
374
364
|
class Event
|
|
375
365
|
# Whether the event is a test send from the portal or CLI.
|
|
376
366
|
def test?: () -> bool
|
|
377
|
-
# Whether to apply this event after `last_sequence`, the highest `
|
|
367
|
+
# Whether to apply this event after `last_sequence`, the highest `resource.sequence` applied for its resource.
|
|
378
368
|
def newer_than?: (Integer? last_sequence) -> bool
|
|
379
369
|
end
|
|
380
370
|
|
|
@@ -425,7 +415,7 @@ module CalmLens
|
|
|
425
415
|
def self.parse_event: (payload: String, secret: String, ?signature: String?, ?timestamp: String?, ?headers: webhook_headers?,
|
|
426
416
|
?tolerance_seconds: Integer, ?now: number) -> Event
|
|
427
417
|
def self.test_event?: (Event | Hash[String, untyped] event) -> bool
|
|
428
|
-
def self.
|
|
418
|
+
def self.newer_event?: (Event | Hash[String, untyped] event, Integer? last_sequence) -> bool
|
|
429
419
|
# The value of an event's fact by key (the first, when the event repeats it), or nil without one.
|
|
430
420
|
def self.get_event_fact: (Event | Hash[String, untyped] event, String key) -> event_fact_value
|
|
431
421
|
# An event's facts as values by key; a repeated key keeps its first value.
|
|
@@ -447,7 +437,7 @@ module CalmLens
|
|
|
447
437
|
def self.parse_webhook_event: (payload: String, secret: String, ?signature: String?, ?timestamp: String?,
|
|
448
438
|
?headers: webhook_headers?, ?tolerance_seconds: Integer, ?now: number) -> Event
|
|
449
439
|
def self.test_webhook_event?: (Event | Hash[String, untyped] event) -> bool
|
|
450
|
-
def self.
|
|
440
|
+
def self.newer_event?: (Event | Hash[String, untyped] event, Integer? last_sequence) -> bool
|
|
451
441
|
# The value of an event's fact by key (the first, when the event repeats it), or nil without one.
|
|
452
442
|
def self.get_event_fact: (Event | Hash[String, untyped] event, String key) -> event_fact_value
|
|
453
443
|
# An event's facts as values by key; a repeated key keeps its first value.
|
data/sig/error_codes.rbs
CHANGED
|
@@ -116,6 +116,7 @@ module CalmLens
|
|
|
116
116
|
REMOTE_URL_UNAVAILABLE: String
|
|
117
117
|
APPEAL_NOT_FOUND: String
|
|
118
118
|
APPEAL_JUDGMENT_NOT_FOUND: String
|
|
119
|
+
CONTEXT_ASSET_NOT_FOUND: String
|
|
119
120
|
APPEAL_ASSET_NOT_FOUND: String
|
|
120
121
|
APPEAL_PERMISSION_DENIED: String
|
|
121
122
|
APPEAL_ALREADY_REVIEWED: String
|
data/sig/models.rbs
CHANGED
|
@@ -25,7 +25,7 @@ module CalmLens
|
|
|
25
25
|
def upload: () -> Asset::Upload
|
|
26
26
|
def classification: () -> Classification
|
|
27
27
|
def content: () -> Asset::Content
|
|
28
|
-
def
|
|
28
|
+
def sequence: () -> Integer
|
|
29
29
|
end
|
|
30
30
|
class AssetBatch < Model
|
|
31
31
|
def batch_id: () -> String
|
|
@@ -114,8 +114,7 @@ module CalmLens
|
|
|
114
114
|
def severity: () -> event_severity
|
|
115
115
|
def title: () -> String
|
|
116
116
|
def description: () -> String?
|
|
117
|
-
def
|
|
118
|
-
def asset_sequence: () -> Integer?
|
|
117
|
+
def resource: () -> Event::Resource?
|
|
119
118
|
def facts: () -> Array[Event::Fact]
|
|
120
119
|
def created_at: () -> Integer
|
|
121
120
|
end
|
|
@@ -357,6 +356,11 @@ module CalmLens
|
|
|
357
356
|
def value: () -> (Array[classification_category] | String | Integer | bool)?
|
|
358
357
|
def url: () -> String?
|
|
359
358
|
end
|
|
359
|
+
class Event::Resource < Model
|
|
360
|
+
def kind: () -> ("asset" | "member" | "api-key")
|
|
361
|
+
def id: () -> String
|
|
362
|
+
def sequence: () -> Integer
|
|
363
|
+
end
|
|
360
364
|
class MetadataFieldsResource::Field < Model
|
|
361
365
|
def id: () -> String
|
|
362
366
|
def title: () -> String
|
|
@@ -466,8 +470,7 @@ module CalmLens
|
|
|
466
470
|
def disposed_at: () -> Integer?
|
|
467
471
|
end
|
|
468
472
|
class Asset::Context::Item < Model
|
|
469
|
-
def
|
|
470
|
-
def author_id: () -> String?
|
|
473
|
+
def asset_id: () -> String
|
|
471
474
|
end
|
|
472
475
|
class Classification::FaceDetection::Face < Model
|
|
473
476
|
def box: () -> Classification::Ocr::TextRegion::Box
|
data/sig/types.rbs
CHANGED
|
@@ -53,11 +53,11 @@ module CalmLens
|
|
|
53
53
|
|
|
54
54
|
# A metadata item to send. Keys inside `metadata` are sent unchanged, so use `id:` and `value:`.
|
|
55
55
|
type metadata_item_input = { id: String, value: json_input }
|
|
56
|
-
# An earlier post in the conversation, as the API returns it.
|
|
57
|
-
type moderation_context_item = { "
|
|
58
|
-
# An earlier post to send
|
|
59
|
-
type moderation_context_item_input = {
|
|
60
|
-
# Earlier posts to send, oldest first;
|
|
56
|
+
# An earlier post in the conversation, as the API returns it: a reference to its asset.
|
|
57
|
+
type moderation_context_item = { "assetId" => String }
|
|
58
|
+
# An earlier post to send: a reference to its asset in the same project.
|
|
59
|
+
type moderation_context_item_input = { asset_id: String }
|
|
60
|
+
# Earlier posts to send, oldest first; CalmLens reads each from its asset and uses the latest it needs.
|
|
61
61
|
type moderation_context_input = { items: Array[moderation_context_item_input] }
|
|
62
62
|
|
|
63
63
|
# ProcessingFeatures.ts: optional analyses to run, keyed by feature. Link detection runs unless
|
|
@@ -398,14 +398,8 @@ module CalmLens
|
|
|
398
398
|
?severity: event_severity,
|
|
399
399
|
?title: String,
|
|
400
400
|
?description: String?,
|
|
401
|
+
# The asset the event is about; it takes the asset's next `resource.sequence`. Without one, the event is about no resource.
|
|
401
402
|
?asset_id: String?,
|
|
402
|
-
?asset: {
|
|
403
|
-
id: String,
|
|
404
|
-
?external_id: String?,
|
|
405
|
-
upload: { status: "uploading" | "completed" },
|
|
406
|
-
classification: { status: asset_classification_status, moderation: { decision: asset_moderation_decision? } },
|
|
407
|
-
content: { status: asset_content_status }
|
|
408
|
-
}?,
|
|
409
403
|
?facts: Array[event_fact_input],
|
|
410
404
|
?created_at: Integer
|
|
411
405
|
}
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: calmlens
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.31.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- CalmLens
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2026-
|
|
11
|
+
date: 2026-12-01 00:00:00.000000000 Z
|
|
12
12
|
dependencies: []
|
|
13
13
|
description: 'Server-side client for the CalmLens API: moderation, uploads, events,
|
|
14
14
|
webhooks and CDN delivery.'
|
|
@@ -31,7 +31,6 @@ files:
|
|
|
31
31
|
- lib/calmlens/lenses.rb
|
|
32
32
|
- lib/calmlens/model.rb
|
|
33
33
|
- lib/calmlens/models.rb
|
|
34
|
-
- lib/calmlens/moderation_context.rb
|
|
35
34
|
- lib/calmlens/option_keys.rb
|
|
36
35
|
- lib/calmlens/policies.rb
|
|
37
36
|
- lib/calmlens/project_configuration.rb
|
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module CalmLens
|
|
4
|
-
# Earlier posts in a conversation, sent with an upload as `context: { items: [{ text:, author_id: }] }`
|
|
5
|
-
# (oldest first) so CalmLens can tell who the content addresses and what it refers to. CalmLens
|
|
6
|
-
# moderates only the uploaded content; it uses the most recent {USED_ITEMS} posts that fit in
|
|
7
|
-
# {USED_CHARACTERS} characters, and the SDK sends only those.
|
|
8
|
-
module ModerationContext
|
|
9
|
-
# The most recent earlier posts CalmLens uses.
|
|
10
|
-
USED_ITEMS = 5
|
|
11
|
-
# The most characters of earlier posts CalmLens uses, counted as Unicode code points.
|
|
12
|
-
USED_CHARACTERS = 2_000
|
|
13
|
-
|
|
14
|
-
module_function
|
|
15
|
-
|
|
16
|
-
# The earlier posts CalmLens uses: the most recent ones, newest first, while at most
|
|
17
|
-
# {USED_ITEMS} posts and {USED_CHARACTERS} characters allow, returned oldest first. The newest
|
|
18
|
-
# post is always kept, so the API can reject one that is too long.
|
|
19
|
-
# @param items [Array<Hash>] posts oldest first, each with `text` and optionally `author_id`
|
|
20
|
-
# (Symbol or String keys, snake_case or camelCase).
|
|
21
|
-
# @return [Array<Hash>] the kept posts as `{'text' => String, 'authorId' => String}`, without
|
|
22
|
-
# `authorId` when a post has none.
|
|
23
|
-
# @raise [InvalidArgumentError] when `items` is not an Array of posts with String text.
|
|
24
|
-
def select_items(items)
|
|
25
|
-
raise Internal.invalid('context.items must be an array of earlier posts') unless items.is_a?(Array)
|
|
26
|
-
|
|
27
|
-
selected = []
|
|
28
|
-
characters = 0
|
|
29
|
-
items.reverse_each do |item|
|
|
30
|
-
break if selected.size == USED_ITEMS
|
|
31
|
-
|
|
32
|
-
post = post(item)
|
|
33
|
-
length = post['text'].length
|
|
34
|
-
break if !selected.empty? && characters + length > USED_CHARACTERS
|
|
35
|
-
|
|
36
|
-
selected.unshift(post)
|
|
37
|
-
characters += length
|
|
38
|
-
end
|
|
39
|
-
selected
|
|
40
|
-
end
|
|
41
|
-
|
|
42
|
-
# One post as the API takes it.
|
|
43
|
-
def post(item)
|
|
44
|
-
fields = item.is_a?(Hash) ? Internal.normalize(item) : nil
|
|
45
|
-
text = fields&.fetch('text', nil)
|
|
46
|
-
raise Internal.invalid('Each earlier post needs its text as a String') unless text.is_a?(String)
|
|
47
|
-
|
|
48
|
-
author = fields['authorId']
|
|
49
|
-
raise Internal.invalid("An earlier post's author_id must be a String") unless author.nil? || author.is_a?(String)
|
|
50
|
-
|
|
51
|
-
author.nil? ? { 'text' => text } : { 'text' => text, 'authorId' => author }
|
|
52
|
-
end
|
|
53
|
-
private_class_method :post
|
|
54
|
-
end
|
|
55
|
-
end
|