Skip to content

Commit 2832325

Browse files
committed
Merge branch 'release/0.1.7'
2 parents 4cc8ec4 + 7d6ad23 commit 2832325

File tree

4 files changed

+116
-11
lines changed

4 files changed

+116
-11
lines changed

lib/tama_ex/perception/concept.ex

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,22 @@ defmodule TamaEx.Perception.Concept do
22
use Ecto.Schema
33
import Ecto.Changeset
44

5+
alias TamaEx.Perception.Concept.Generator
6+
57
@primary_key false
68
embedded_schema do
79
field :id, :binary_id
810
field :relation, :string
911
field :content, :map
12+
13+
embeds_one :generator, Generator, on_replace: :update
1014
end
1115

1216
def changeset(concept, attrs) do
1317
concept
1418
|> cast(attrs, [:id, :relation, :content])
1519
|> validate_required([:id, :relation, :content])
20+
|> cast_embed(:generator)
1621
end
1722

1823
@doc """
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
defmodule TamaEx.Perception.Concept.Generator do
2+
use Ecto.Schema
3+
import Ecto.Changeset
4+
5+
@primary_key false
6+
embedded_schema do
7+
field :type, Ecto.Enum, values: [:model, :module]
8+
field :reference, :string
9+
field :parameters, :map
10+
end
11+
12+
def changeset(generator, attrs) do
13+
generator
14+
|> cast(attrs, [:type, :reference, :parameters])
15+
|> validate_required([:type, :reference, :parameters])
16+
end
17+
end

mix.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ defmodule TamaEx.MixProject do
44
def project do
55
[
66
app: :tama_ex,
7-
version: "0.1.6",
7+
version: "0.1.7",
88
elixir: "~> 1.18",
99
start_permanent: Mix.env() == :prod,
1010
deps: deps(),

test/tama_ex/perception_test.exs

Lines changed: 93 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -283,12 +283,22 @@ defmodule TamaEx.PerceptionTest do
283283
%{
284284
"id" => "concept_001",
285285
"relation" => "reply",
286-
"content" => %{"text" => "Hello world"}
286+
"content" => %{"text" => "Hello world"},
287+
"generator" => %{
288+
"type" => "module",
289+
"reference" => "tama/classes/extraction",
290+
"parameters" => %{"depth" => 1, "names" => nil, "types" => ["array"]}
291+
}
287292
},
288293
%{
289294
"id" => "concept_002",
290295
"relation" => "mention",
291-
"content" => %{"text" => "Another concept"}
296+
"content" => %{"text" => "Another concept"},
297+
"generator" => %{
298+
"type" => "module",
299+
"reference" => "tama/classes/extraction",
300+
"parameters" => %{"depth" => 2, "names" => ["test"], "types" => ["string"]}
301+
}
292302
}
293303
]
294304
}
@@ -308,10 +318,28 @@ defmodule TamaEx.PerceptionTest do
308318
assert concept1.relation == "reply"
309319
assert concept1.content == %{"text" => "Hello world"}
310320

321+
assert concept1.generator.type == :module
322+
assert concept1.generator.reference == "tama/classes/extraction"
323+
324+
assert concept1.generator.parameters == %{
325+
"depth" => 1,
326+
"names" => nil,
327+
"types" => ["array"]
328+
}
329+
311330
assert %Concept{} = concept2
312331
assert concept2.id == "concept_002"
313332
assert concept2.relation == "mention"
314333
assert concept2.content == %{"text" => "Another concept"}
334+
335+
assert concept2.generator.type == :module
336+
assert concept2.generator.reference == "tama/classes/extraction"
337+
338+
assert concept2.generator.parameters == %{
339+
"depth" => 2,
340+
"names" => ["test"],
341+
"types" => ["string"]
342+
}
315343
end
316344

317345
test "handles empty list response", %{bypass: bypass, client: client} do
@@ -345,7 +373,12 @@ defmodule TamaEx.PerceptionTest do
345373
%{
346374
"id" => "concept_filtered",
347375
"relation" => "reply",
348-
"content" => %{"filtered" => true}
376+
"content" => %{"filtered" => true},
377+
"generator" => %{
378+
"type" => "module",
379+
"reference" => "tama/classes/extraction",
380+
"parameters" => %{"depth" => 1, "names" => nil, "types" => ["array"]}
381+
}
349382
}
350383
]
351384
}
@@ -435,7 +468,12 @@ defmodule TamaEx.PerceptionTest do
435468
%{
436469
"id" => "valid_concept",
437470
"relation" => "reply",
438-
"content" => %{"text" => "Valid"}
471+
"content" => %{"text" => "Valid"},
472+
"generator" => %{
473+
"type" => "module",
474+
"reference" => "tama/classes/extraction",
475+
"parameters" => %{"depth" => 1, "names" => nil, "types" => ["array"]}
476+
}
439477
},
440478
%{
441479
# Missing required fields - should create empty Concept struct
@@ -456,11 +494,21 @@ defmodule TamaEx.PerceptionTest do
456494
assert valid_concept.id == "valid_concept"
457495
assert valid_concept.relation == "reply"
458496

497+
assert valid_concept.generator.type == :module
498+
assert valid_concept.generator.reference == "tama/classes/extraction"
499+
500+
assert valid_concept.generator.parameters == %{
501+
"depth" => 1,
502+
"names" => nil,
503+
"types" => ["array"]
504+
}
505+
459506
# Invalid concept should have nil values for required fields
460507
assert %Concept{} = invalid_concept
461508
assert invalid_concept.id == nil
462509
assert invalid_concept.relation == nil
463510
assert invalid_concept.content == nil
511+
assert invalid_concept.generator == nil
464512
end
465513
end
466514

@@ -469,27 +517,46 @@ defmodule TamaEx.PerceptionTest do
469517
concept_data = %{
470518
"id" => "concept_123",
471519
"relation" => "reply",
472-
"content" => %{"text" => "Hello world", "metadata" => %{"score" => 0.95}}
520+
"content" => %{"text" => "Hello world", "metadata" => %{"score" => 0.95}},
521+
"generator" => %{
522+
"type" => "module",
523+
"reference" => "tama/classes/extraction",
524+
"parameters" => %{"depth" => 1, "names" => nil, "types" => ["array"]}
525+
}
473526
}
474527

475528
concept = Concept.parse(concept_data)
476529
assert %Concept{} = concept
477530
assert concept.id == "concept_123"
478531
assert concept.relation == "reply"
479532
assert concept.content == %{"text" => "Hello world", "metadata" => %{"score" => 0.95}}
533+
534+
assert concept.generator.type == :module
535+
assert concept.generator.reference == "tama/classes/extraction"
536+
assert concept.generator.parameters == %{"depth" => 1, "names" => nil, "types" => ["array"]}
480537
end
481538

482539
test "Concept.parse handles list of concepts" do
483540
concepts_data = [
484541
%{
485542
"id" => "concept_001",
486543
"relation" => "reply",
487-
"content" => %{"text" => "First concept"}
544+
"content" => %{"text" => "First concept"},
545+
"generator" => %{
546+
"type" => "module",
547+
"reference" => "tama/classes/extraction",
548+
"parameters" => %{"depth" => 1, "names" => nil, "types" => ["array"]}
549+
}
488550
},
489551
%{
490552
"id" => "concept_002",
491553
"relation" => "mention",
492-
"content" => %{"text" => "Second concept"}
554+
"content" => %{"text" => "Second concept"},
555+
"generator" => %{
556+
"type" => "module",
557+
"reference" => "tama/classes/extraction",
558+
"parameters" => %{"depth" => 2, "names" => ["test"], "types" => ["string"]}
559+
}
493560
}
494561
]
495562

@@ -512,18 +579,23 @@ defmodule TamaEx.PerceptionTest do
512579
assert concept.id == nil
513580
assert concept.relation == nil
514581
assert concept.content == nil
582+
assert concept.generator == nil
515583
end
516584

517585
test "Concept.parse handles JSON string input" do
518586
json_string =
519-
~s({"id": "concept_json", "relation": "reply", "content": {"text": "From JSON"}})
587+
~s({"id": "concept_json", "relation": "reply", "content": {"text": "From JSON"}, "generator": {"type": "module", "reference": "tama/classes/extraction", "parameters": {"depth": 1, "names": null, "types": ["array"]}}})
520588

521589
concept = Concept.parse(json_string)
522590

523591
assert %Concept{} = concept
524592
assert concept.id == "concept_json"
525593
assert concept.relation == "reply"
526594
assert concept.content == %{"text" => "From JSON"}
595+
596+
assert concept.generator.type == :module
597+
assert concept.generator.reference == "tama/classes/extraction"
598+
assert concept.generator.parameters == %{"depth" => 1, "names" => nil, "types" => ["array"]}
527599
end
528600

529601
test "Concept.parse handles invalid JSON gracefully" do
@@ -538,7 +610,12 @@ defmodule TamaEx.PerceptionTest do
538610
valid_data = %{
539611
"id" => "concept_parse_bang",
540612
"relation" => "reply",
541-
"content" => %{"test" => true}
613+
"content" => %{"test" => true},
614+
"generator" => %{
615+
"type" => "module",
616+
"reference" => "tama/classes/extraction",
617+
"parameters" => %{"depth" => 1, "names" => nil, "types" => ["array"]}
618+
}
542619
}
543620

544621
concept = Concept.parse!(valid_data)
@@ -561,12 +638,18 @@ defmodule TamaEx.PerceptionTest do
561638
assert changeset.errors[:id]
562639
assert changeset.errors[:relation]
563640
assert changeset.errors[:content]
641+
# Generator is embedded and validated separately, so no direct error here
564642

565643
valid_changeset =
566644
Concept.changeset(%Concept{}, %{
567645
"id" => "valid_id",
568646
"relation" => "reply",
569-
"content" => %{"valid" => true}
647+
"content" => %{"valid" => true},
648+
"generator" => %{
649+
"type" => "module",
650+
"reference" => "tama/classes/extraction",
651+
"parameters" => %{"depth" => 1, "names" => nil, "types" => ["array"]}
652+
}
570653
})
571654

572655
assert valid_changeset.valid?

0 commit comments

Comments
 (0)