calmlens 0.30.0 → 0.31.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 34cc7decf11dccd3df4fc06875ea3652b32d6eacf4321f64953791eacaebfd80
4
- data.tar.gz: 0ecffa743bc5f7a0df8efd3084c7fe94046dac06c763962b7ce998e360c78e71
3
+ metadata.gz: cb92c3f2338dd93756f4b08b425f6234d31a1f77c0000c938814d671474170ff
4
+ data.tar.gz: 4c57d18c2fa05e27b81171760e3878c0cd3476e0756d7aee5efcd29462aa6f65
5
5
  SHA512:
6
- metadata.gz: 5ed41fed093ded33d5c6e87e64c76b2a5584a82dfcc4129ee9cdc6b43c3f7bb33709996212e3fc73d51aba7ad107814cd0742146d0ee63dde3941666083ed6ea
7
- data.tar.gz: be5700de1ea19f67e65f933ee74016d33a61363434b2c5a37d02dbb4279949a845a58ede6b1cd5af63429012e6ad8254bbe030258440e1a49271fc7341262686
6
+ metadata.gz: c0864ec596bc1f6bef85fe947a7235db81bb728561975de69b662f163e7cffb22a101381bc04a7a268934203b2ff53e5a4d5958521a02c2e12cb0a6ceaaef023
7
+ data.tar.gz: be5a3d9d4237cd912d2adbae2210b8a2f2e394da7eefeabeca786d516f7e48d8487ec3763da79c7da0038fb61f9cf62351c655f65f71a81eb4d7bfca80fe8373
@@ -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: [{text:, author_id:}]}`, earlier posts in the
289
- # conversation, oldest first. Only the posts CalmLens uses are sent: the most recent
290
- # {ModerationContext::USED_ITEMS} that fit in {ModerationContext::USED_CHARACTERS} characters.
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)
@@ -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,
@@ -621,8 +621,7 @@ module CalmLens
621
621
  )
622
622
 
623
623
  Asset::Context::Item.declare(
624
- text: ["text", nil],
625
- author_id: ["authorId", nil]
624
+ asset_id: ["assetId", nil]
626
625
  )
627
626
 
628
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.
@@ -291,9 +296,9 @@ module CalmLens
291
296
  # the API key as its events' actor; the test hook's is a reviewer's (a `user`).
292
297
  def record_decision(item, decision, note, by_api_key)
293
298
  apply_outcome(item, decision, 'requested')
294
- # Like the API: the human review queue's decision settles its review, one through the API
295
- # leaves it waiting.
296
- item['classification']['moderation']['pendingReview'] = nil unless by_api_key
299
+ # Like the API: the human review queue's decision settles its review, and so does the app's own
300
+ # decision through the API.
301
+ item['classification']['moderation']['pendingReview'] = nil
297
302
  review = { 'id' => @create_id.call('rev'), 'decision' => decision, 'note' => note,
298
303
  'decidedBy' => deep_copy(API_KEY_ACTOR), 'reviewedAt' => @now.call, 'applied' => true }
299
304
  actor = by_api_key ? API_KEY_ACTOR : REVIEWER_ACTOR
@@ -323,7 +328,13 @@ module CalmLens
323
328
  fail!(ErrorCodes::ASSET_PROCESSING) if processing
324
329
  status = item['classification']['moderation']['decision']
325
330
  fail!(ErrorCodes::ASSET_DECISION_NOT_ALLOWED) if status.nil? && !%w[blocked purged].include?(decision)
326
- fail!(ErrorCodes::ASSET_DECISION_UNCHANGED) if status == decision
331
+ if status == decision
332
+ # Like the API: agreeing with the decision a queued review waits on settles the review,
333
+ # leaving the decision as it is.
334
+ fail!(ErrorCodes::ASSET_DECISION_UNCHANGED) if item['classification']['moderation']['pendingReview'].nil?
335
+ item['classification']['moderation']['pendingReview'] = nil
336
+ return item
337
+ end
327
338
  fail!(ErrorCodes::ASSET_CONTENT_PURGED) if status == 'purged' && decision != 'blocked'
328
339
 
329
340
  record_decision(item, decision, note, true)
@@ -960,7 +971,7 @@ module CalmLens
960
971
  processing = (options['processingOptions'] || {}).dup
961
972
  processing['retainContent'] = show_project_settings['settings']['retainContent'] if processing['retainContent'].nil?
962
973
  keep = processing.is_a?(Hash) && processing['retainContent'] == true
963
- context = options['context'].is_a?(Hash) ? ModerationContext.select_items(options['context'].fetch('items', [])) : []
974
+ context = options['context'].is_a?(Hash) ? select_context(options['context'].fetch('items', [])) : []
964
975
  # Fields the API returns as null when they are not set.
965
976
  asset = { 'id' => @create_id.call('asset'), 'projectId' => @project_id, 'parentId' => nil, 'parentPages' => [],
966
977
  'folderId' => options['folderId'], 'uploadPresetId' => options['uploadPresetId'],
@@ -1248,6 +1259,49 @@ module CalmLens
1248
1259
  asset
1249
1260
  end
1250
1261
 
1262
+ # Like the API, a reply's context references the project's own assets, each read for its text:
1263
+ # a text asset's own text or the text found in it. Only the references CalmLens uses are kept:
1264
+ # the most recent ones, newest first, while CONTEXT_USED_ITEMS posts and
1265
+ # CONTEXT_USED_CHARACTERS characters allow, returned oldest first. The newest is always kept.
1266
+ def select_context(items)
1267
+ # Like the API's schema, a reference that isn't an asset ID is refused before any lookup.
1268
+ Array(items).each_with_index do |item, index|
1269
+ asset_id = item.is_a?(Hash) ? item['assetId'] : nil
1270
+ next if asset_id.is_a?(String) && asset_id.match?(ASSET_ID_PATTERN)
1271
+
1272
+ invalid!('validation/invalid-format', "context.items.#{index}.assetId")
1273
+ end
1274
+ posts = Array(items).map do |item|
1275
+ asset_id = item.is_a?(Hash) ? item['assetId'] : nil
1276
+ asset = asset_id.is_a?(String) ? @state.assets[asset_id] : nil
1277
+ fail!(ErrorCodes::CONTEXT_ASSET_NOT_FOUND, details: { 'assetId' => asset_id }) unless asset && asset['projectId'] == @project_id
1278
+ [{ 'assetId' => asset_id }, context_text(asset).length]
1279
+ end
1280
+ selected = []
1281
+ characters = 0
1282
+ posts.reverse_each do |item, length|
1283
+ break if selected.size == CONTEXT_USED_ITEMS || (!selected.empty? && characters + length > CONTEXT_USED_CHARACTERS)
1284
+
1285
+ selected.unshift(item)
1286
+ characters += length
1287
+ end
1288
+ selected
1289
+ end
1290
+
1291
+ # An earlier post's text, to the most CalmLens reads of one post; empty once its content is gone.
1292
+ def context_text(asset)
1293
+ bytes = asset['type'] == 'plaintext-document' ? stored_bytes(asset) : nil
1294
+ extracted = asset['content']['extractedText']
1295
+ text = if bytes && !bytes.empty?
1296
+ bytes.dup.force_encoding(Encoding::UTF_8).scrub
1297
+ elsif extracted.is_a?(Hash) && extracted['type'] == 'string'
1298
+ extracted['content'].to_s
1299
+ else
1300
+ ''
1301
+ end
1302
+ text.strip[0, CONTEXT_MAX_ITEM_CHARACTERS]
1303
+ end
1304
+
1251
1305
  # An asset's stored content, its parts in order, or nil once it is removed.
1252
1306
  def stored_bytes(asset)
1253
1307
  stored = asset['content']['status'] == 'removed' ? nil : @state.content[asset['id']]
@@ -2,5 +2,5 @@
2
2
 
3
3
  module CalmLens
4
4
  # The gem version. The gemspec and the User-Agent header read it from here.
5
- VERSION = '0.30.0'
5
+ VERSION = '0.31.1'
6
6
  end
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]
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
@@ -470,8 +470,7 @@ module CalmLens
470
470
  def disposed_at: () -> Integer?
471
471
  end
472
472
  class Asset::Context::Item < Model
473
- def text: () -> String
474
- def author_id: () -> String?
473
+ def asset_id: () -> String
475
474
  end
476
475
  class Classification::FaceDetection::Face < Model
477
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 = { "text" => String, ?"authorId" => String }
58
- # An earlier post to send. `author_id` is your identifier for who wrote it.
59
- type moderation_context_item_input = { text: String, ?author_id: String }
60
- # Earlier posts to send, oldest first; only the posts CalmLens uses are sent.
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
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.30.0
4
+ version: 0.31.1
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-30 00:00:00.000000000 Z
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