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,63 @@
1
+ """R4 — Context: entities attached to a conversation or a message are
2
+ rendered for the agent as an agent would see them."""
3
+ from __future__ import annotations
4
+
5
+ from graphcore.public.live_testing import scenario
6
+
7
+ from agents.specs.dsl import AgentsDsl, says
8
+
9
+
10
+ @scenario('R4', 'Standing context is rendered for the agent and kept as a context message')
11
+ async def _(a: AgentsDsl):
12
+ ana = a.as_('Ana')
13
+ venue = await ana.writes_an_instruction('Charleston, 80 guests, in June.', name='Wedding')
14
+ conversation = await ana.starts_a_conversation(await ana.creates_an_agent('Troy'), context_ids=[venue])
15
+ model = a.the_model_replies(says('80 guests in June.'))
16
+
17
+ await ana.asks(conversation, 'How many guests?')
18
+
19
+ assert model.saw('Charleston, 80 guests, in June.')
20
+ [shown] = [m for m in await ana.messages_in(conversation) if m['context_versions']]
21
+ assert shown['role'] == 'system' and list(shown['context_versions']) == [venue], shown
22
+
23
+
24
+ @scenario('R4', 'Context on one message is rendered for that turn')
25
+ async def _(a: AgentsDsl):
26
+ ana = a.as_('Ana')
27
+ venue = await ana.writes_an_instruction('The Cedar Room seats 90.', name='Venue')
28
+ conversation = await ana.starts_a_conversation(await ana.creates_an_agent('Troy'))
29
+ model = a.the_model_replies(says('No context.'), says('It seats 90.'))
30
+
31
+ await ana.asks(conversation, 'Hello')
32
+ await ana.asks(conversation, 'How many does this seat?', context_ids=[venue])
33
+
34
+ assert not model.saw('The Cedar Room seats 90.', call=0)
35
+ assert model.saw('The Cedar Room seats 90.', call=1)
36
+
37
+
38
+ @scenario('R4', 'A context entity that changed is shown again at the next turn')
39
+ async def _(a: AgentsDsl):
40
+ ana = a.as_('Ana')
41
+ venue = await ana.writes_an_instruction('80 guests.', name='Wedding')
42
+ conversation = await ana.starts_a_conversation(await ana.creates_an_agent('Troy'), context_ids=[venue])
43
+ model = a.the_model_replies(says('80.'), says('Now 95.'))
44
+ await ana.asks(conversation, 'How many guests?')
45
+
46
+ await ana.edits_the_instruction(venue, '95 guests.')
47
+ await ana.asks(conversation, 'And now?')
48
+
49
+ assert model.saw('95 guests.', call=1)
50
+ assert len([m for m in await ana.messages_in(conversation) if m['context_versions']]) == 2
51
+
52
+
53
+ @scenario('R4', 'An entity the person cannot read cannot be attached')
54
+ async def _(a: AgentsDsl):
55
+ ana, ben = a.as_('Ana'), a.as_('Ben')
56
+ troy = await ana.creates_an_agent('Troy')
57
+ await ana.shares(troy, with_=ben)
58
+ private = await ana.writes_an_instruction('The budget is 40k.', name='Budget')
59
+
60
+ refused = await ben.tries_to_start_a_conversation(troy, context_ids=[private])
61
+
62
+ assert refused['status'] == 'failed' and private in refused['error'], refused
63
+ assert await ben.my_conversations_with(troy) == []
@@ -0,0 +1,86 @@
1
+ """R5 — Personal configuration: a person's own instructions, skills and model
2
+ on an agent, for them alone."""
3
+ from __future__ import annotations
4
+
5
+ from graphcore.public.live_testing import scenario
6
+
7
+ from agents.specs.dsl import OTHER_MODEL, AgentsDsl, says
8
+
9
+
10
+ async def a_shared_agent(a: AgentsDsl):
11
+ ana, ben = a.as_('Ana'), a.as_('Ben')
12
+ troy = await ana.creates_an_agent('Troy')
13
+ await ana.writes_an_instruction('Be brief.', on=troy, attach=True)
14
+ await ana.shares(troy, with_=ben, role='viewer')
15
+ return ana, ben, troy
16
+
17
+
18
+ @scenario('R5', "A person's own instruction on a shared agent applies to their turns alone")
19
+ async def _(a: AgentsDsl):
20
+ ana, ben, troy = await a_shared_agent(a)
21
+ spanish = await ben.writes_an_instruction('Answer in Spanish.', name='Spanish')
22
+ model = a.the_model_replies(says('Hola.'), says('Hello.'))
23
+
24
+ customized = await ben.customizes(troy, instruction_ids=[spanish])
25
+ await ben.asks(await ben.starts_a_conversation(troy), 'Hi')
26
+ await ana.asks(await ana.starts_a_conversation(troy), 'Hi')
27
+
28
+ assert customized['status'] == 'succeeded', customized
29
+ assert model.prefix(call=0)[-2:] == ('Be brief.', 'Answer in Spanish.')
30
+ assert 'Answer in Spanish.' not in model.prefix(call=1)
31
+ assert len((await ana.get(troy))['instruction_ids']) == 1
32
+
33
+
34
+ @scenario('R5', 'A person picks their own model for an agent without changing anyone else\'s')
35
+ async def _(a: AgentsDsl):
36
+ ana, ben, troy = await a_shared_agent(a)
37
+ model = a.the_model_replies(says('Other.'), says('Default.'))
38
+
39
+ await ben.customizes(troy, model=OTHER_MODEL)
40
+ await ben.asks(await ben.starts_a_conversation(troy), 'Which model?')
41
+ await ana.asks(await ana.starts_a_conversation(troy), 'Which model?')
42
+
43
+ assert [request.model for request in model.requests] == ['other', 'scripted']
44
+ assert (await ana.get(troy))['model'] is None
45
+
46
+
47
+ @scenario('R5', 'A person has one configuration per agent, however often they change it')
48
+ async def _(a: AgentsDsl):
49
+ _, ben, troy = await a_shared_agent(a)
50
+ a.the_deployment_offers_the_scripted_models()
51
+
52
+ first = await ben.customizes(troy, model=OTHER_MODEL)
53
+ second = await ben.customizes(troy, compaction_threshold=0.5)
54
+
55
+ assert first['ret_val']['profile_id'] == second['ret_val']['profile_id'], (first, second)
56
+ [profile] = await ben.my_profiles_on(troy)
57
+ assert (profile['model'], profile['compaction_threshold']) == (OTHER_MODEL, 0.5), profile
58
+
59
+
60
+ @scenario('R5', 'Deleting a configuration loses only it')
61
+ async def _(a: AgentsDsl):
62
+ _, ben, troy = await a_shared_agent(a)
63
+ spanish = await ben.writes_an_instruction('Answer in Spanish.', name='Spanish')
64
+ profile = (await ben.customizes(troy, instruction_ids=[spanish]))['ret_val']['profile_id']
65
+ conversation = await ben.starts_a_conversation(troy)
66
+ model = a.the_model_replies(says('Hola.'), says('Hello.'))
67
+ await ben.asks(conversation, 'Hi')
68
+
69
+ assert (await ben.deletes(profile))['status'] == 'succeeded'
70
+ await ben.asks(conversation, 'Hi again')
71
+
72
+ assert 'Answer in Spanish.' in model.prefix(call=0) and 'Answer in Spanish.' not in model.prefix(call=1)
73
+ assert await ben.can_read(spanish) and await ben.can_read(troy)
74
+ assert len(await ben.transcript(conversation, roles=('user',))) == 2
75
+
76
+
77
+ @scenario('R5', 'Nobody configures an agent they cannot read')
78
+ async def _(a: AgentsDsl):
79
+ ana, ben = a.as_('Ana'), a.as_('Ben')
80
+ troy = await ana.creates_an_agent('Troy')
81
+ a.the_deployment_offers_the_scripted_models()
82
+
83
+ refused = await ben.customizes(troy, model=OTHER_MODEL)
84
+
85
+ assert refused['status'] == 'failed' and 'model' not in refused['error'], refused
86
+ assert await ben.my_profiles_on(troy) == []
@@ -0,0 +1,70 @@
1
+ """R6 — Delegation: an agent hands a goal to another agent in a
2
+ sub-conversation, waiting for it or told when it is done."""
3
+ from __future__ import annotations
4
+
5
+ from graphcore.public.live_testing import eventually, scenario
6
+
7
+ from agents.specs.dsl import AgentsDsl, delegates, result_of, says
8
+
9
+
10
+ @scenario('R6', 'A waiting agent gets the sub-conversation\'s answer as its tool result')
11
+ async def _(a: AgentsDsl):
12
+ ana = a.as_('Ana')
13
+ troy, planner = await ana.creates_an_agent('Troy'), await ana.creates_an_agent('Planner')
14
+ conversation = await ana.starts_a_conversation(troy)
15
+ a.the_model_replies(delegates(planner, 'Find flights to Charleston'), says('Two flights found.'),
16
+ says('The planner found two flights.'))
17
+
18
+ reply = await ana.asks(conversation, 'Plan my trip')
19
+
20
+ [child] = await ana.sub_conversations_of(conversation)
21
+ assert (child['agent_id'], child['owner']) == (planner, await ana.entity_id())
22
+ assert await ana.transcript(child['entity_id'], roles=('user', 'assistant')) == [
23
+ ('user', 'Find flights to Charleston'), ('assistant', 'Two flights found.')]
24
+ [call] = await ana.tool_calls_in(conversation)
25
+ assert result_of(call) == {'conversation_id': child['entity_id'], 'status': 'completed',
26
+ 'text': 'Two flights found.'}
27
+ assert reply == 'The planner found two flights.'
28
+ assert child['entity_id'] not in await ana.my_conversations_with(planner)
29
+
30
+
31
+ @scenario('R6', 'A background sub-conversation tells its parent when it is done, and the parent carries on')
32
+ async def _(a: AgentsDsl):
33
+ ana = a.as_('Ana')
34
+ troy, planner = await ana.creates_an_agent('Troy'), await ana.creates_an_agent('Planner')
35
+ conversation = await ana.starts_a_conversation(troy)
36
+ a.the_model_replies(delegates(planner, 'Find flights', background=True), says('Done.'), says('Done.'),
37
+ says('Done.'))
38
+
39
+ await ana.asks(conversation, 'Plan my trip')
40
+
41
+ [child] = await ana.sub_conversations_of(conversation)
42
+ [call] = await ana.tool_calls_in(conversation)
43
+ assert result_of(call) == {'conversation_id': child['entity_id']}
44
+
45
+ async def delivered():
46
+ return [m for m in await ana.messages_in(conversation) if m['source_conversation_id']]
47
+ [delivery] = await eventually(delivered, message='the planner never reported back')
48
+ assert delivery['role'] == 'system' and 'Done.' in delivery['content'], delivery
49
+ await ana.waits_for_the_turns(conversation, at_least=2)
50
+ assert (await ana.transcript(conversation))[-1][0] == 'assistant'
51
+
52
+
53
+ @scenario('R6', 'Stopping a conversation stops the sub-conversation it is waiting on')
54
+ async def _(a: AgentsDsl):
55
+ ana = a.as_('Ana')
56
+ troy, planner = await ana.creates_an_agent('Troy'), await ana.creates_an_agent('Planner')
57
+ conversation = await ana.starts_a_conversation(troy)
58
+ model = a.the_model_replies(delegates(planner, 'Search everything'), says('Searching'), hold=True)
59
+ await ana.sends(conversation, 'Plan my trip')
60
+ model.answers(1)
61
+ await model.is_asked(2)
62
+
63
+ await ana.stops(conversation)
64
+ model.answers(1)
65
+ await ana.waits_for_the_turns(conversation)
66
+
67
+ [child] = await ana.sub_conversations_of(conversation)
68
+ await ana.waits_for_the_turns(child['entity_id'])
69
+ assert (await ana.transcript(conversation))[-1] == ('system', 'Stopped.')
70
+ assert (await ana.transcript(child['entity_id']))[-1] == ('system', 'Stopped.')
@@ -0,0 +1,103 @@
1
+ """R7 — Sharing: grants on agents, conversations, instructions and skills;
2
+ never on profiles or single messages."""
3
+ from __future__ import annotations
4
+
5
+ from graphcore.public.live_testing import scenario
6
+
7
+ from agents.specs.dsl import AgentsDsl, Person, delegates, says
8
+
9
+
10
+ @scenario('R7', 'A viewer of a conversation watches the reply arrive, and may not send')
11
+ async def _(a: AgentsDsl):
12
+ ana, ben = a.as_('Ana'), a.as_('Ben')
13
+ conversation = await ana.starts_a_conversation(await ana.creates_an_agent('Troy'))
14
+ await ana.shares(conversation, with_=ben, role='viewer')
15
+ watching = await ben.connects()
16
+ await watching.subscribe(conversation)
17
+ a.the_model_replies(says('Drafting ', 'the agenda.', pause=1.5))
18
+
19
+ await ana.asks(conversation, 'Draft the agenda')
20
+ refused = await ben.sends(conversation, 'Add a budget slot')
21
+
22
+ contents = [state['content'] for state in Person.message_states(watching, conversation)]
23
+ assert contents and contents[-1] == 'Drafting the agenda.', contents
24
+ assert refused['status'] == 'failed', refused
25
+ assert await ben.transcript(conversation, roles=('user', 'assistant')) == [
26
+ ('user', 'Draft the agenda'), ('assistant', 'Drafting the agenda.')]
27
+
28
+
29
+ @scenario('R7', 'An editor of a conversation sends in it, and their turn runs as them')
30
+ async def _(a: AgentsDsl):
31
+ ana, ben = a.as_('Ana'), a.as_('Ben')
32
+ troy = await ana.creates_an_agent('Troy')
33
+ await ana.shares(troy, with_=ben)
34
+ conversation = await ana.starts_a_conversation(troy)
35
+ await ana.shares(conversation, with_=ben, role='editor')
36
+ a.the_model_replies(says('Noted, Ben.'))
37
+
38
+ reply = await ben.asks(conversation, 'Use the Q3 numbers')
39
+
40
+ assert reply == 'Noted, Ben.'
41
+ [turn] = await ben.turns_on(conversation)
42
+ assert turn['owner'] == await ben.entity_id()
43
+
44
+
45
+ @scenario('R7', 'A grant on an agent reaches the instructions and skills inside it, and only an editor edits')
46
+ async def _(a: AgentsDsl):
47
+ ana, ben, cy = a.as_('Ana'), a.as_('Ben'), a.as_('Cy')
48
+ troy = await ana.creates_an_agent('Troy')
49
+ brief = await ana.writes_an_instruction('Be brief.', on=troy, attach=True)
50
+ skill = await ana.writes_a_skill('research', 'How to research', 'Search first.', on=troy, attach=True)
51
+ assert not await ben.can_read(brief)
52
+
53
+ await ana.shares(troy, with_=ben, role='viewer')
54
+ await ana.shares(troy, with_=cy, role='editor')
55
+
56
+ assert await ben.can_read(brief) and await ben.can_read(skill)
57
+ assert (await ben.updates_the_agent(troy, name='Ben\'s'))['status'] == 'failed'
58
+ assert (await ben.edits_the_instruction(brief, 'Be long.'))['status'] == 'failed'
59
+ assert (await cy.updates_the_agent(troy, name='Troy II'))['status'] == 'succeeded'
60
+ assert (await ana.get(troy))['name'] == 'Troy II'
61
+
62
+
63
+ @scenario('R7', 'A grant on a conversation reaches its sub-conversations')
64
+ async def _(a: AgentsDsl):
65
+ ana, ben = a.as_('Ana'), a.as_('Ben')
66
+ troy, planner = await ana.creates_an_agent('Troy'), await ana.creates_an_agent('Planner')
67
+ conversation = await ana.starts_a_conversation(troy)
68
+ a.the_model_replies(delegates(planner, 'Find flights'), says('Two flights.'), says('Done.'))
69
+ await ana.asks(conversation, 'Plan my trip')
70
+
71
+ await ana.shares(conversation, with_=ben, role='viewer')
72
+
73
+ [child] = await ben.sub_conversations_of(conversation)
74
+ assert ('assistant', 'Two flights.') in await ben.transcript(child['entity_id'])
75
+
76
+
77
+ @scenario('R7', 'A configuration is never shared')
78
+ async def _(a: AgentsDsl):
79
+ ana, ben = a.as_('Ana'), a.as_('Ben')
80
+ troy = await ana.creates_an_agent('Troy')
81
+ await ana.shares(troy, with_=ben)
82
+ profile = (await ben.customizes(troy, compaction_threshold=0.5))['ret_val']['profile_id']
83
+
84
+ refused = await ben.shares(profile, with_=ana)
85
+
86
+ assert refused['status'] == 'failed', refused
87
+ assert not await ana.can_read(profile)
88
+
89
+
90
+ @scenario('R7', 'Only the runtime writes the record: no message is created or changed directly')
91
+ async def _(a: AgentsDsl):
92
+ ana = a.as_('Ana')
93
+ conversation = await ana.starts_a_conversation(await ana.creates_an_agent('Troy'))
94
+ a.the_model_replies(says('Hello.'))
95
+ await ana.asks(conversation, 'Hi')
96
+ [_, reply] = await ana.messages_in(conversation)
97
+
98
+ created = await ana.writes_a_message_directly(conversation, 'I never said this')
99
+ changed = await ana.execute_one({'strategy_name': 'SetAttributes', 'target_entity_id': reply['entity_id'],
100
+ 'param_config': {'attributes': {'content': 'Rewritten'}}})
101
+
102
+ assert created['status'] == 'failed' and changed['status'] == 'failed', (created, changed)
103
+ assert await ana.transcript(conversation) == [('user', 'Hi'), ('assistant', 'Hello.')]
@@ -0,0 +1,40 @@
1
+ """R8 — Embedding: a view places the agent with the entity it shows as
2
+ context; the person's conversations about that entity are found and continued.
3
+ Placing by key in a page is the web client's part, covered by the devshell's
4
+ browser tests."""
5
+ from __future__ import annotations
6
+
7
+ from graphcore.public.live_testing import scenario
8
+
9
+ from agents.specs.dsl import AgentsDsl, says
10
+
11
+
12
+ @scenario('R8', 'Conversations started about an entity are found by it, and only the person\'s own')
13
+ async def _(a: AgentsDsl):
14
+ ana, ben = a.as_('Ana'), a.as_('Ben')
15
+ troy = await ana.creates_an_agent('Troy')
16
+ await ana.shares(troy, with_=ben)
17
+ wedding = await ana.writes_an_instruction('Riley and Sam, June 14.', name='Wedding')
18
+ await ana.shares(wedding, with_=ben)
19
+
20
+ about = await ana.starts_a_conversation(troy, context_ids=[wedding])
21
+ await ana.starts_a_conversation(troy)
22
+ bens = await ben.starts_a_conversation(troy, context_ids=[wedding])
23
+
24
+ assert await ana.my_conversations_about(wedding) == [about]
25
+ assert await ben.my_conversations_about(wedding) == [bens]
26
+
27
+
28
+ @scenario('R8', 'A conversation about an entity is continued with the entity still in view')
29
+ async def _(a: AgentsDsl):
30
+ ana = a.as_('Ana')
31
+ wedding = await ana.writes_an_instruction('Riley and Sam, June 14.', name='Wedding')
32
+ conversation = await ana.starts_a_conversation(await ana.creates_an_agent('Troy'), context_ids=[wedding])
33
+ model = a.the_model_replies(says('June 14.'), says('Riley and Sam.'))
34
+ await ana.asks(conversation, 'When is it?', context_ids=[wedding])
35
+
36
+ [continued] = await ana.my_conversations_about(wedding)
37
+ await ana.asks(continued, 'Whose is it?', context_ids=[wedding])
38
+
39
+ assert continued == conversation
40
+ assert model.saw('Riley and Sam, June 14.', call=1)
@@ -0,0 +1,41 @@
1
+ """R9 — Finding: a person's agents, what is shared with them, their
2
+ conversations with an agent, and what is running."""
3
+ from __future__ import annotations
4
+
5
+ from graphcore.public.live_testing import scenario
6
+
7
+ from agents.specs.dsl import AgentsDsl, says
8
+
9
+
10
+ @scenario('R9', 'A person finds their agents, the ones shared with them, and every one they can use')
11
+ async def _(a: AgentsDsl):
12
+ ana, ben = a.as_('Ana'), a.as_('Ben')
13
+ await ana.creates_an_agent('Troy')
14
+ planner = await ben.creates_an_agent('Planner')
15
+ await ben.creates_an_agent('Private')
16
+
17
+ await ben.shares(planner, with_=ana)
18
+
19
+ assert await ana.my_agents() == ['Troy']
20
+ assert await ana.shared_with_me() == ['Planner']
21
+ assert sorted(await ana.agents_i_can_use()) == ['Planner', 'Troy']
22
+
23
+
24
+ @scenario('R9', 'A person finds their conversations with an agent, and which are running')
25
+ async def _(a: AgentsDsl):
26
+ ana, ben = a.as_('Ana'), a.as_('Ben')
27
+ troy = await ana.creates_an_agent('Troy')
28
+ await ana.shares(troy, with_=ben)
29
+ first, second = await ana.starts_a_conversation(troy), await ana.starts_a_conversation(troy)
30
+ await ben.starts_a_conversation(troy)
31
+ model = a.the_model_replies(says('Working.'), hold=True)
32
+
33
+ await ana.sends(first, 'Go')
34
+ await model.is_asked(1)
35
+ running = await ana.my_running_conversations()
36
+ model.answers(1)
37
+ await ana.waits_for_the_turns(first)
38
+
39
+ assert sorted(await ana.my_conversations_with(troy)) == sorted([first, second])
40
+ assert running == [first]
41
+ assert await ana.my_running_conversations() == []
@@ -0,0 +1,41 @@
1
+ """R10 — Clients: an agent connected over MCP does what the web client does,
2
+ through the same strategies and with the same authorization."""
3
+ from __future__ import annotations
4
+
5
+ import re
6
+
7
+ from graphcore.public.live_testing import eventually, scenario
8
+
9
+ from agents.specs.dsl import AgentsDsl, says
10
+
11
+
12
+ @scenario('R10', 'An agent over MCP finds an agent, starts a conversation, sends, and reads the answer')
13
+ async def _(a: AgentsDsl):
14
+ ana = a.as_('Ana')
15
+ troy = await ana.creates_an_agent('Troy')
16
+ a.the_model_replies(says('Two review threads are open.'))
17
+
18
+ home = await ana.opens_over_mcp('/agents')
19
+ started = await ana.calls_over_mcp(troy, 'create_conversation', title='Reviews')
20
+ [conversation] = await ana.my_conversations_with(troy)
21
+ sent = await ana.calls_over_mcp(conversation, 'send', content='Summarize the open review threads')
22
+ await ana.waits_for_the_turns(conversation)
23
+
24
+ assert 'Troy' in home, home
25
+ assert not started['error'] and not sent['error'], (started, sent)
26
+ place = await eventually(lambda: ana.opens_over_mcp(conversation))
27
+ assert 'Two review threads are open.' in place and re.search(r'\bidle\b', place), place
28
+
29
+
30
+ @scenario('R10', 'Over MCP a viewer is refused what they are refused over HTTP')
31
+ async def _(a: AgentsDsl):
32
+ ana, ben = a.as_('Ana'), a.as_('Ben')
33
+ conversation = await ana.starts_a_conversation(await ana.creates_an_agent('Troy'))
34
+ await ana.shares(conversation, with_=ben, role='viewer')
35
+
36
+ over_mcp = await ben.calls_over_mcp(conversation, 'send', content='Hello')
37
+ over_http = await ben.sends(conversation, 'Hello')
38
+
39
+ assert over_mcp['error'], over_mcp
40
+ assert over_http['status'] == 'failed', over_http
41
+ assert await ana.transcript(conversation) == []
File without changes
@@ -0,0 +1,16 @@
1
+ STRING = {"type": "string"}
2
+ MODEL = {"type": ["string", "null"], "description": "A model id from ListModels; null leaves it to the layer below"}
3
+ THRESHOLD = {
4
+ "type": ["number", "null"], "exclusiveMinimum": 0, "maximum": 1,
5
+ "description": "Fraction of the model's context size past which a turn summarizes; null leaves it to the layer below",
6
+ }
7
+ IDS = {"type": "array", "items": {"type": "string"}}
8
+
9
+
10
+ def params(properties: dict, required: tuple = ()) -> dict:
11
+ return {
12
+ "type": "object",
13
+ "properties": properties,
14
+ "required": list(required),
15
+ "additionalProperties": False,
16
+ }
@@ -0,0 +1,93 @@
1
+ from typing import Optional
2
+
3
+ from graphcore.public.entities import Entity
4
+ from graphcore.public.strategies import Strategy, StrategyRequestPayload, register_strategy
5
+
6
+ from agents.entities.agent import Agent
7
+ from agents.entities.instruction import Instruction
8
+ from agents.entities.skill import Skill
9
+ from agents.strategies._schema import IDS, MODEL, STRING, THRESHOLD, params
10
+ from agents.validation import listed_model, openable_address, readable_ids
11
+
12
+ _FIELDS = {
13
+ "name": STRING,
14
+ "description": STRING,
15
+ "model": MODEL,
16
+ "compaction_threshold": THRESHOLD,
17
+ "instruction_ids": {**IDS, "description": "Instructions placed in the prompt, in order"},
18
+ "skill_ids": {**IDS, "description": "Skills the agent may load"},
19
+ "debug_context": {
20
+ "type": "boolean",
21
+ "description": "true lets anyone who can read a conversation with the agent inspect the request its model is sent",
22
+ },
23
+ "max_tasks": {
24
+ "type": ["integer", "null"], "minimum": 1, "maximum": 100,
25
+ "description": "How many background tasks one of its conversations may have running at once; null leaves it to the deployment",
26
+ },
27
+ "start_address": {
28
+ "type": ["string", "null"], "maxLength": 1024,
29
+ "description": "An address each of its conversations starts out looking at, such as /notes; null for none",
30
+ },
31
+ }
32
+
33
+
34
+ async def set_agent_fields(session, agent, given: dict) -> None:
35
+ """Sets the given instruction, skill, model and threshold fields on an agent or a profile."""
36
+ if 'instruction_ids' in given:
37
+ agent.instruction_ids = await readable_ids(session, given['instruction_ids'], Instruction)
38
+ if 'skill_ids' in given:
39
+ agent.skill_ids = await readable_ids(session, given['skill_ids'], Skill)
40
+ if 'model' in given:
41
+ agent.model = listed_model(given['model'])
42
+ if 'compaction_threshold' in given:
43
+ agent.compaction_threshold = given['compaction_threshold']
44
+
45
+
46
+ @register_strategy(Entity)
47
+ class CreateAgent(Strategy):
48
+ strategy_description = "Creates an agent inside the target, or a top-level one owned by the caller"
49
+ param_schema = params(_FIELDS, required=('name',))
50
+
51
+ async def apply(self) -> tuple:
52
+ given = self.strategy_request.param_config
53
+ agent = self.session.create(
54
+ Agent, name=given['name'], description=given.get('description', ''),
55
+ parent_id=self.entity.entity_id if self.entity is not None else None,
56
+ )
57
+ agent.debug_context = given.get('debug_context', False)
58
+ agent.max_tasks = given.get('max_tasks')
59
+ agent.start_address = await openable_address(self.principal, given.get('start_address'))
60
+ await set_agent_fields(self.session, agent, given)
61
+ agent = await self.save_entity(agent)
62
+ self.strategy_request.ret_val['agent_id'] = agent.entity_id
63
+ return self.strategy_request, self.entity
64
+
65
+ @classmethod
66
+ def request_constructor(cls, target_entity_id: Optional[str] = None, **fields) -> StrategyRequestPayload:
67
+ return cls._build_request(target_entity_id=target_entity_id, param_config=fields)
68
+
69
+
70
+ @register_strategy(Agent)
71
+ class UpdateAgent(Strategy):
72
+ strategy_description = "Sets the given fields of an agent"
73
+ param_schema = params(_FIELDS)
74
+
75
+ async def apply(self) -> tuple:
76
+ given = self.strategy_request.param_config
77
+ if 'name' in given:
78
+ self.entity.name = given['name']
79
+ if 'description' in given:
80
+ self.entity.description = given['description']
81
+ if 'debug_context' in given:
82
+ self.entity.debug_context = given['debug_context']
83
+ if 'max_tasks' in given:
84
+ self.entity.max_tasks = given['max_tasks']
85
+ if 'start_address' in given:
86
+ self.entity.start_address = await openable_address(self.principal, given['start_address'])
87
+ await set_agent_fields(self.session, self.entity, given)
88
+ self.entity = await self.save_entity(self.entity)
89
+ return self.strategy_request, self.entity
90
+
91
+ @classmethod
92
+ def request_constructor(cls, target_entity_id: str, **fields) -> StrategyRequestPayload:
93
+ return cls._build_request(target_entity_id=target_entity_id, param_config=fields)
@@ -0,0 +1,45 @@
1
+ from graphcore.public.agent_frontend import connect
2
+ from graphcore.public.errors import Forbidden, InvalidRequest
3
+ from graphcore.public.strategies import Strategy, StrategyRequestPayload, register_strategy
4
+
5
+ from agents.entities.conversation import Conversation
6
+ from agents.runtime import prompt
7
+ from agents.runtime.effective_agent import TurnFailure, resolve
8
+ from agents.runtime.loop import dispatcher_for, messages_of
9
+
10
+
11
+ @register_strategy(Conversation)
12
+ class InspectContext(Strategy):
13
+ """For seeing what an agent is shown. Built by the code a turn's own calls
14
+ are built by, as the caller, from what the conversation holds now."""
15
+
16
+ strategy_description = "Returns the request the conversation's next model call would send, if its agent has debug_context on"
17
+ read_only_strategy = True
18
+ param_schema = {"type": "object", "properties": {}, "additionalProperties": False}
19
+
20
+ async def apply(self) -> tuple:
21
+ try:
22
+ agent = await resolve(self.session, self.entity)
23
+ except TurnFailure as exc:
24
+ raise InvalidRequest(str(exc)) from None
25
+ if not agent.debug_context:
26
+ raise Forbidden("This conversation's agent does not have debug_context on")
27
+ conversation_id = self.entity.entity_id
28
+ async with connect(self.principal) as client:
29
+ tools = await dispatcher_for(self.session, client, conversation_id).tool_defs()
30
+ server_instructions = client.instructions
31
+ provider_name, _, model = agent.model.partition('/')
32
+ asked, summary, rest = prompt.next_request(
33
+ model, agent, server_instructions, tools, await messages_of(self.session, conversation_id),
34
+ provider_name, conversation_id,
35
+ )
36
+ self.strategy_request.ret_val['context'] = {
37
+ 'model': agent.model,
38
+ **prompt.described(asked),
39
+ 'sizes': prompt.sizes(asked.prefix, asked.tools, summary, rest),
40
+ }
41
+ return self.strategy_request, self.entity
42
+
43
+ @classmethod
44
+ def request_constructor(cls, target_entity_id: str) -> StrategyRequestPayload:
45
+ return cls._build_request(target_entity_id=target_entity_id)