graphcore-apps 0.5.0__py3-none-any.whl

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.
Files changed (118) hide show
  1. agents/__init__.py +0 -0
  2. agents/apps.py +15 -0
  3. agents/authorization.py +62 -0
  4. agents/conf.py +78 -0
  5. agents/delegation.py +218 -0
  6. agents/entities/__init__.py +0 -0
  7. agents/entities/agent.py +16 -0
  8. agents/entities/conversation.py +23 -0
  9. agents/entities/instruction.py +7 -0
  10. agents/entities/message.py +30 -0
  11. agents/entities/profile.py +14 -0
  12. agents/entities/skill.py +8 -0
  13. agents/migrations/0001_initial.py +88 -0
  14. agents/migrations/0002_message_context_versions.py +16 -0
  15. agents/migrations/0003_compaction_threshold.py +21 -0
  16. agents/migrations/0004_agent_debug_context.py +16 -0
  17. agents/migrations/0005_conversation_last_active_at.py +34 -0
  18. agents/migrations/0006_tasks.py +21 -0
  19. agents/migrations/0007_agent_start_address.py +16 -0
  20. agents/migrations/__init__.py +0 -0
  21. agents/models.py +64 -0
  22. agents/persistence.py +18 -0
  23. agents/providers/__init__.py +0 -0
  24. agents/providers/anthropic.py +142 -0
  25. agents/providers/fake.py +51 -0
  26. agents/providers/google.py +186 -0
  27. agents/providers/openai.py +177 -0
  28. agents/providers/port.py +101 -0
  29. agents/providers/registry.py +40 -0
  30. agents/routes.py +5 -0
  31. agents/runtime/__init__.py +0 -0
  32. agents/runtime/compaction.py +29 -0
  33. agents/runtime/context.py +78 -0
  34. agents/runtime/dispatch.py +76 -0
  35. agents/runtime/effective_agent.py +70 -0
  36. agents/runtime/loop.py +454 -0
  37. agents/runtime/prompt.py +183 -0
  38. agents/runtime/resume.py +30 -0
  39. agents/runtime/tools.py +35 -0
  40. agents/specs/__init__.py +3 -0
  41. agents/specs/dsl.py +310 -0
  42. agents/specs/test_r01_agents.py +74 -0
  43. agents/specs/test_r02_conversations.py +146 -0
  44. agents/specs/test_r03_acts_as_the_sender.py +60 -0
  45. agents/specs/test_r04_context.py +63 -0
  46. agents/specs/test_r05_personal_configuration.py +86 -0
  47. agents/specs/test_r06_delegation.py +70 -0
  48. agents/specs/test_r07_sharing.py +103 -0
  49. agents/specs/test_r08_embedding.py +40 -0
  50. agents/specs/test_r09_finding.py +41 -0
  51. agents/specs/test_r10_clients.py +41 -0
  52. agents/strategies/__init__.py +0 -0
  53. agents/strategies/_schema.py +16 -0
  54. agents/strategies/agents.py +93 -0
  55. agents/strategies/context.py +45 -0
  56. agents/strategies/conversations.py +65 -0
  57. agents/strategies/instructions.py +62 -0
  58. agents/strategies/messages.py +56 -0
  59. agents/strategies/models.py +27 -0
  60. agents/strategies/profiles.py +43 -0
  61. agents/strategies/skills.py +47 -0
  62. agents/strategies/turn.py +47 -0
  63. agents/test_settings.py +6 -0
  64. agents/tests/__init__.py +0 -0
  65. agents/tests/support.py +94 -0
  66. agents/tests/test_agent_views.py +181 -0
  67. agents/tests/test_agents.py +166 -0
  68. agents/tests/test_anthropic_adapter.py +221 -0
  69. agents/tests/test_context.py +231 -0
  70. agents/tests/test_control.py +162 -0
  71. agents/tests/test_conversations.py +158 -0
  72. agents/tests/test_delegation.py +392 -0
  73. agents/tests/test_google_adapter.py +208 -0
  74. agents/tests/test_inspect.py +59 -0
  75. agents/tests/test_live_providers.py +69 -0
  76. agents/tests/test_openai_adapter.py +259 -0
  77. agents/tests/test_profiles.py +92 -0
  78. agents/tests/test_resumption.py +150 -0
  79. agents/tests/test_turns.py +230 -0
  80. agents/validation.py +48 -0
  81. agents_agent_frontend/__init__.py +3 -0
  82. agents_agent_frontend/agent_views.py +286 -0
  83. audio/__init__.py +0 -0
  84. audio/apps.py +14 -0
  85. audio/authorization.py +13 -0
  86. audio/conf.py +27 -0
  87. audio/entities/__init__.py +11 -0
  88. audio/entities/recording.py +10 -0
  89. audio/migrations/0001_initial.py +26 -0
  90. audio/migrations/__init__.py +0 -0
  91. audio/models.py +9 -0
  92. audio/persistence.py +6 -0
  93. audio/providers/__init__.py +0 -0
  94. audio/providers/fake.py +35 -0
  95. audio/providers/openai.py +50 -0
  96. audio/providers/port.py +31 -0
  97. audio/providers/registry.py +31 -0
  98. audio/specs/__init__.py +3 -0
  99. audio/specs/dsl.py +120 -0
  100. audio/specs/test_clients.py +20 -0
  101. audio/specs/test_dictation.py +87 -0
  102. audio/specs/test_privacy.py +45 -0
  103. audio/specs/test_vendor.py +59 -0
  104. audio/strategies/__init__.py +11 -0
  105. audio/strategies/recordings.py +88 -0
  106. audio/test_settings.py +6 -0
  107. audio/tests/__init__.py +0 -0
  108. audio/tests/test_openai_adapter.py +65 -0
  109. audio/tests/test_recordings.py +139 -0
  110. audio_agent_frontend/__init__.py +3 -0
  111. audio_agent_frontend/agent_views.py +11 -0
  112. graphcore_apps-0.5.0.dist-info/METADATA +14 -0
  113. graphcore_apps-0.5.0.dist-info/RECORD +118 -0
  114. graphcore_apps-0.5.0.dist-info/WHEEL +5 -0
  115. graphcore_apps-0.5.0.dist-info/entry_points.txt +7 -0
  116. graphcore_apps-0.5.0.dist-info/licenses/LICENSE +202 -0
  117. graphcore_apps-0.5.0.dist-info/licenses/NOTICE +2 -0
  118. graphcore_apps-0.5.0.dist-info/top_level.txt +4 -0
@@ -0,0 +1,162 @@
1
+ import asyncio
2
+ from unittest.mock import patch
3
+
4
+ from agents.entities.conversation import ConversationStatus
5
+ from agents.entities.message import MessageRole
6
+ from agents.models import ConversationModel
7
+ from agents.providers.fake import FakeProvider, calls, says
8
+ from agents.providers.port import Done, TextDelta, ToolCall, Usage
9
+ from agents.runtime.dispatch import Dispatcher
10
+ from agents.strategies.conversations import UpdateConversation
11
+ from agents.tests.support import AgentsTestCase
12
+
13
+ DONE = Done('end', Usage(10, 5))
14
+
15
+
16
+ class ControlTests(AgentsTestCase):
17
+ async def conversation(self):
18
+ alice = await self.create_user('alice')
19
+ agent_id = await self.create_agent(alice)
20
+ return alice, agent_id, await self.create_conversation(alice, agent_id)
21
+
22
+ def stop(self, principal, conversation_id):
23
+ return self.run_strategy(principal, UpdateConversation.request_constructor(conversation_id, stop=True))
24
+
25
+ def all_text(self, request):
26
+ return ' '.join(turn.text for turn in request.turns)
27
+
28
+ async def test_stop_ends_the_turn_at_its_next_save_and_keeps_the_text_so_far(self):
29
+ alice, _, conversation_id = await self.conversation()
30
+ FakeProvider.play([
31
+ TextDelta('Reading the first'), lambda: self.stop(alice, conversation_id), TextDelta(' document'),
32
+ TextDelta(' and the second'), DONE,
33
+ ])
34
+
35
+ await self.send(alice, conversation_id)
36
+
37
+ self.assertEqual(await self.transcript(alice, conversation_id), [
38
+ ('user', 'Hello'), ('assistant', 'Reading the first document'), ('system', 'Stopped.'),
39
+ ])
40
+ conversation = await self.get_entity(alice, conversation_id)
41
+ self.assertEqual((conversation.status, conversation.stop_requested), (ConversationStatus.IDLE, False))
42
+ (result,) = await self.turn_results(alice, conversation_id)
43
+ self.assertEqual(result['status'], 'stopped')
44
+
45
+ async def test_stop_interrupts_a_tool_call_in_flight(self):
46
+ alice, _, conversation_id = await self.conversation()
47
+ started = asyncio.Event()
48
+
49
+ async def never_returns(dispatcher, call, agent):
50
+ started.set()
51
+ await asyncio.Event().wait()
52
+
53
+ FakeProvider.play(calls(ToolCall('c1', 'open', {'at': '/'})))
54
+ with patch.object(Dispatcher, 'run', never_returns):
55
+ await self.send(alice, conversation_id, settle=False)
56
+ await started.wait()
57
+ await self.stop(alice, conversation_id)
58
+ await self.settle()
59
+
60
+ step = (await self.messages(alice, conversation_id))[1]
61
+ self.assertEqual(step.tool_calls[0]['state'], 'interrupted')
62
+ self.assertEqual((await self.transcript(alice, conversation_id))[-1], ('system', 'Stopped.'))
63
+
64
+ async def test_the_model_sees_the_interrupted_call_on_the_next_turn(self):
65
+ alice, _, conversation_id = await self.conversation()
66
+ FakeProvider.play([ToolCall('c1', 'open', {'at': '/'}), lambda: self.stop(alice, conversation_id),
67
+ TextDelta('never saved'), DONE])
68
+ await self.send(alice, conversation_id)
69
+
70
+ await self.send(alice, conversation_id, 'Carry on')
71
+
72
+ request = FakeProvider.requests[-1]
73
+ (result,) = [r for turn in request.turns for r in turn.results]
74
+ self.assertEqual((result.call_id, result.is_error), ('c1', True))
75
+ self.assertIn('Carry on', self.all_text(request))
76
+
77
+ async def test_a_stop_left_on_an_idle_conversation_stops_nothing(self):
78
+ alice, _, conversation_id = await self.conversation()
79
+ await ConversationModel.objects.filter(entity_id=conversation_id).aupdate(stop_requested=True)
80
+
81
+ await self.send(alice, conversation_id)
82
+
83
+ self.assertEqual((await self.transcript(alice, conversation_id))[-1], ('assistant', 'ok'))
84
+ self.assertFalse((await self.get_entity(alice, conversation_id)).stop_requested)
85
+
86
+ async def test_a_stop_sent_before_a_running_conversations_turn_starts_stops_that_turn(self):
87
+ # A task is marked running before its turn starts, so `stop_task` can come first.
88
+ alice, _, conversation_id = await self.conversation()
89
+ await ConversationModel.objects.filter(entity_id=conversation_id).aupdate(status='running')
90
+ await self.stop(alice, conversation_id)
91
+
92
+ await self.send(alice, conversation_id)
93
+
94
+ self.assertEqual(await self.transcript(alice, conversation_id), [('user', 'Hello'), ('system', 'Stopped.')])
95
+ self.assertEqual(FakeProvider.requests, [])
96
+ conversation = await self.get_entity(alice, conversation_id)
97
+ self.assertEqual((conversation.status, conversation.stop_requested), (ConversationStatus.IDLE, False))
98
+ (result,) = await self.turn_results(alice, conversation_id)
99
+ self.assertEqual((result['status'], result['text']), ('stopped', 'Stopped before doing anything.'))
100
+
101
+ async def test_a_message_its_sender_sends_mid_turn_is_read_by_that_turn(self):
102
+ alice, _, conversation_id = await self.conversation()
103
+ second = []
104
+
105
+ async def send_again():
106
+ second.append(await self.send(alice, conversation_id, 'Add a slot for budget', settle=False))
107
+
108
+ FakeProvider.play([TextDelta('Agenda: intro.'), send_again, DONE], says('Agenda: intro, budget.'))
109
+
110
+ await self.send(alice, conversation_id, 'Draft the agenda')
111
+
112
+ self.assertEqual(len(FakeProvider.requests), 2)
113
+ self.assertIn('Add a slot for budget', self.all_text(FakeProvider.requests[1]))
114
+ final = (await self.messages(alice, conversation_id))[-1]
115
+ self.assertEqual((final.content, final.read_through), ('Agenda: intro, budget.', second[0]))
116
+ self.assertEqual([r['status'] for r in await self.turn_results(alice, conversation_id)], ['completed'] * 2)
117
+
118
+ async def test_a_message_sent_before_the_reply_streams_is_sent_after_that_reply(self):
119
+ alice, _, conversation_id = await self.conversation()
120
+
121
+ async def send_again():
122
+ await self.send(alice, conversation_id, 'Add a slot for budget', settle=False)
123
+
124
+ FakeProvider.play([send_again, TextDelta('Agenda: intro.'), DONE], says('Agenda: intro, budget.'))
125
+
126
+ await self.send(alice, conversation_id, 'Draft the agenda')
127
+
128
+ self.assertEqual(await self.transcript(alice, conversation_id), [
129
+ ('user', 'Draft the agenda'), ('user', 'Add a slot for budget'),
130
+ ('assistant', 'Agenda: intro.'), ('assistant', 'Agenda: intro, budget.'),
131
+ ])
132
+ turns = FakeProvider.requests[1].turns
133
+ self.assertEqual([turn.role for turn in turns], ['user', 'assistant', 'user'])
134
+ self.assertEqual((turns[1].text, turns[2].text), ('Agenda: intro.', 'Add a slot for budget'))
135
+
136
+ async def test_another_persons_message_waits_for_a_turn_of_its_own(self):
137
+ alice, agent_id, conversation_id = await self.conversation()
138
+ bob = await self.create_user('bob')
139
+ await self.grant(alice, agent_id, bob, 'viewer')
140
+ await self.grant(alice, conversation_id, bob, 'editor')
141
+
142
+ async def bob_joins():
143
+ await self.send(bob, conversation_id, 'Use the Q3 numbers', settle=False)
144
+
145
+ FakeProvider.play(
146
+ [bob_joins, ToolCall('c1', 'open', {'at': '/'}), Done('tool_calls', Usage(10, 5))],
147
+ says('Here is the agenda.'),
148
+ says('Updated with Q3.'),
149
+ )
150
+
151
+ await self.send(alice, conversation_id, 'Draft the agenda')
152
+
153
+ alices, bobs = FakeProvider.requests[1], FakeProvider.requests[2]
154
+ self.assertNotIn('Q3', self.all_text(alices))
155
+ self.assertIn('Q3', self.all_text(bobs))
156
+ self.assertIn('Here is the agenda.', self.all_text(bobs))
157
+ replies = [m for m in await self.messages(alice, conversation_id) if m.role is MessageRole.ASSISTANT]
158
+ self.assertEqual(
159
+ [m.owner for m in replies],
160
+ [alice.principal_entity_id, alice.principal_entity_id, bob.principal_entity_id],
161
+ )
162
+ self.assertEqual(replies[-1].content, 'Updated with Q3.')
@@ -0,0 +1,158 @@
1
+ from graphcore.public.errors import Forbidden, InvalidRequest
2
+ from graphcore.public.persistence import AllOf, ContainsValue, Equal
3
+ from graphcore.public.strategies import CreateEntity, DeleteEntity
4
+
5
+ from agents.entities.agent import Agent
6
+ from agents.entities.conversation import Conversation, ConversationStatus
7
+ from agents.entities.instruction import Instruction
8
+ from agents.entities.message import Message
9
+ from agents.entities.skill import Skill
10
+ from agents.models import ConversationModel
11
+ from agents.strategies.agents import UpdateAgent
12
+ from agents.strategies.conversations import UpdateConversation
13
+ from agents.tests.support import AgentsTestCase
14
+
15
+
16
+ class ConversationTests(AgentsTestCase):
17
+ async def test_a_viewer_of_the_agent_starts_a_conversation_of_their_own(self):
18
+ carl = await self.create_user('carl')
19
+ riley = await self.create_user('riley')
20
+ agent_id = await self.create_agent(carl)
21
+ await self.grant(carl, agent_id, riley, 'viewer')
22
+ wedding_id = await self.create_instruction(riley, name='Stands in for any entity')
23
+
24
+ conversation_id = await self.create_conversation(riley, agent_id, title='RSVPs', context_ids=[wedding_id])
25
+
26
+ conversation = await self.get_entity(riley, conversation_id)
27
+ self.assertIsInstance(conversation, Conversation)
28
+ self.assertEqual((conversation.agent_id, conversation.title), (agent_id, 'RSVPs'))
29
+ self.assertEqual((conversation.owner, conversation.parent_id), (riley.principal_entity_id, None))
30
+ self.assertIs(conversation.status, ConversationStatus.IDLE)
31
+ self.assertEqual(conversation.context_ids, [wedding_id])
32
+ self.assertTrue((await self.get_entity(carl, conversation_id)).to_client().get('forbidden'))
33
+
34
+ async def test_a_conversation_needs_read_on_the_agent_and_on_its_context(self):
35
+ carl = await self.create_user('carl')
36
+ stranger = await self.create_user('stranger')
37
+ agent_id = await self.create_agent(carl)
38
+ private = await self.create_instruction(stranger)
39
+
40
+ with self.assertRaises(Forbidden):
41
+ await self.create_conversation(stranger, agent_id)
42
+ with self.assertRaises(InvalidRequest):
43
+ await self.create_conversation(carl, agent_id, context_ids=[private])
44
+
45
+ async def test_update_sets_the_given_fields(self):
46
+ carl = await self.create_user('carl')
47
+ conversation_id = await self.create_conversation(carl, await self.create_agent(carl), title='Old')
48
+
49
+ await self.run_strategy(carl, UpdateConversation.request_constructor(conversation_id, title='New'))
50
+
51
+ self.assertEqual((await self.get_entity(carl, conversation_id)).title, 'New')
52
+
53
+ async def test_creating_one_and_each_turn_mark_it_active_and_a_rename_does_not(self):
54
+ carl = await self.create_user('carl')
55
+ conversation_id = await self.create_conversation(carl, await self.create_agent(carl))
56
+ created = (await self.get_entity(carl, conversation_id)).last_active_at
57
+ self.assertIsNotNone(created)
58
+
59
+ await self.run_strategy(carl, UpdateConversation.request_constructor(conversation_id, title='Renamed'))
60
+ self.assertEqual((await self.get_entity(carl, conversation_id)).last_active_at, created)
61
+
62
+ await self.send(carl, conversation_id)
63
+ self.assertGreater((await self.get_entity(carl, conversation_id)).last_active_at, created)
64
+
65
+ async def test_stop_does_nothing_to_an_idle_conversation_and_flags_a_running_one(self):
66
+ carl = await self.create_user('carl')
67
+ conversation_id = await self.create_conversation(carl, await self.create_agent(carl))
68
+
69
+ await self.run_strategy(carl, UpdateConversation.request_constructor(conversation_id, stop=True))
70
+ self.assertFalse((await self.get_entity(carl, conversation_id)).stop_requested)
71
+
72
+ await ConversationModel.objects.filter(entity_id=conversation_id).aupdate(status='running')
73
+ await self.run_strategy(carl, UpdateConversation.request_constructor(conversation_id, stop=True))
74
+ self.assertTrue((await self.get_entity(carl, conversation_id)).stop_requested)
75
+
76
+ async def test_a_viewer_watches_and_an_editor_writes_a_shared_conversation(self):
77
+ alice = await self.create_user('alice')
78
+ bob = await self.create_user('bob')
79
+ conversation_id = await self.create_conversation(alice, await self.create_agent(alice))
80
+ await self.grant(alice, conversation_id, bob, 'viewer')
81
+
82
+ self.assertIsInstance(await self.get_entity(bob, conversation_id), Conversation)
83
+ with self.assertRaises(Forbidden):
84
+ await self.run_strategy(bob, UpdateConversation.request_constructor(conversation_id, title='Mine'))
85
+
86
+ async def test_a_message_cannot_be_created_by_a_generic_strategy(self):
87
+ carl = await self.create_user('carl')
88
+ conversation_id = await self.create_conversation(carl, await self.create_agent(carl))
89
+
90
+ with self.assertRaises(Forbidden):
91
+ await self.run_strategy(carl, CreateEntity.request_constructor(
92
+ conversation_id, Message.get_entity_type_id(),
93
+ ))
94
+ with self.assertRaises(InvalidRequest):
95
+ await self.run_strategy(carl, CreateEntity.request_constructor(None, Message.get_entity_type_id()))
96
+
97
+ async def test_a_conversation_survives_its_agent(self):
98
+ carl = await self.create_user('carl')
99
+ agent_id = await self.create_agent(carl)
100
+ conversation_id = await self.create_conversation(carl, agent_id)
101
+
102
+ await self.run_strategy(carl, DeleteEntity.request_constructor(agent_id))
103
+
104
+ self.assertIsInstance(await self.get_entity(carl, conversation_id), Conversation)
105
+
106
+
107
+ class RoleTests(AgentsTestCase):
108
+ async def test_an_editor_edits_the_agent_and_what_is_inside_it_and_a_viewer_does_not(self):
109
+ carl = await self.create_user('carl')
110
+ viewer = await self.create_user('viewer')
111
+ editor = await self.create_user('editor')
112
+ agent_id = await self.create_agent(carl)
113
+ instruction_id = await self.create_instruction(carl, agent_id, attach=True)
114
+ await self.grant(carl, agent_id, viewer, 'viewer')
115
+ await self.grant(carl, agent_id, editor, 'editor')
116
+
117
+ self.assertIsInstance(await self.get_entity(viewer, instruction_id), Instruction)
118
+ with self.assertRaises(Forbidden):
119
+ await self.run_strategy(viewer, UpdateAgent.request_constructor(agent_id, name='Mine'))
120
+ with self.assertRaises(Forbidden):
121
+ await self.create_skill(viewer, agent_id)
122
+
123
+ await self.run_strategy(editor, UpdateAgent.request_constructor(agent_id, name='Ours'))
124
+ await self.create_skill(editor, agent_id, attach=True)
125
+ with self.assertRaises(Forbidden):
126
+ await self.grant(editor, agent_id, viewer, 'editor')
127
+
128
+ self.assertEqual((await self.get_entity(carl, agent_id)).name, 'Ours')
129
+
130
+
131
+ class QueryTests(AgentsTestCase):
132
+ async def test_the_lists_a_client_shows(self):
133
+ carl = await self.create_user('carl')
134
+ riley = await self.create_user('riley')
135
+ me = Equal('owner', riley.principal_entity_id)
136
+ troy = await self.create_agent(carl)
137
+ mine = await self.create_agent(riley, name='Mine')
138
+ await self.grant(carl, troy, riley, 'viewer')
139
+ instruction_id = await self.create_instruction(carl, troy, attach=True)
140
+ skill_id = await self.create_skill(carl, troy, attach=True)
141
+ about = await self.create_conversation(riley, troy, context_ids=[mine])
142
+ other = await self.create_conversation(riley, troy)
143
+ await self.create_conversation(riley, mine)
144
+ await self.create_conversation(carl, troy)
145
+ await ConversationModel.objects.filter(entity_id=other).aupdate(status='running')
146
+
147
+ self.assertEqual(await self.listed(riley, Agent, where=me), [mine])
148
+ self.assertEqual(await self.listed(riley, Agent, Conversation, Skill, Instruction, granted_to_me=True), [troy])
149
+ self.assertEqual(await self.listed(riley, Agent), sorted([troy, mine]))
150
+ self.assertEqual(
151
+ await self.listed(riley, Conversation, where=AllOf((me, Equal('agent_id', troy), Equal('parent_id', None)))),
152
+ sorted([about, other]),
153
+ )
154
+ self.assertEqual(
155
+ await self.listed(riley, Conversation, where=AllOf((me, ContainsValue('context_ids', mine)))), [about],
156
+ )
157
+ self.assertEqual(await self.listed(riley, Conversation, where=AllOf((me, Equal('status', 'running')))), [other])
158
+ self.assertEqual(await self.listed(riley, Instruction, Skill, parent_id=troy), sorted([instruction_id, skill_id]))