Skip to content

Commit ed6ffa8

Browse files
authored
Use agent slugs in conversation store contract (#120)
1 parent 362d275 commit ed6ffa8

4 files changed

Lines changed: 15 additions & 33 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,8 @@ $scope = new AgentsAPI\Core\FilesRepository\WP_Agent_Memory_Scope(
382382

383383
Transcript sessions are also workspace-stamped. `WP_Agent_Conversation_Store::create_session()` and `::get_recent_pending_session()` both receive an `WP_Agent_Workspace_Scope`, and `WP_Agent_Conversation_Request` can carry a workspace so runtime persisters can stamp the session they materialize.
384384

385+
Transcript sessions use registered agent slugs for runtime agent identity. `WP_Agent_Conversation_Store::create_session()` accepts `string $agent_slug = ''`, matching `wp_register_agent()` / `wp_get_agent()`. Concrete stores that materialize agents as posts can keep post IDs internally, but generic session arrays expose `agent_slug` rather than requiring a WordPress post ID.
386+
385387
Transcript stores preserve provider continuity metadata as part of the complete session state. `WP_Agent_Conversation_Store::update_session()` accepts an optional opaque `provider_response_id`, and `::get_session()` returns the same key alongside `provider` and `model`. Consumers using provider-side state, such as the OpenAI Responses API `previous_response_id` flow, can pass the provider's response ID through this field without encoding per-consumer metadata keys. A `null` value means no provider-side response ID is associated with the current transcript state.
386388

387389
## Retrieved Context Authority

docs/default-stores-companion.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ The class names above are placeholders for the companion repository. They are no
6868

6969
- Implements `AgentsAPI\Core\Database\Chat\WP_Agent_Conversation_Store`.
7070
- Stores transcript sessions in a companion-owned CPT or table chosen by the companion.
71-
- Preserves `WP_Agent_Workspace_Scope`, `user_id`, `agent_id`, title, metadata, provider, model, `provider_response_id`, timestamps, and pending-session dedup fields from the contract.
71+
- Preserves `WP_Agent_Workspace_Scope`, `user_id`, `agent_slug`, title, metadata, provider, model, `provider_response_id`, timestamps, and pending-session dedup fields from the contract.
7272
- Leaves chat UI listing, read state, retention scheduling, and analytics outside the generic store unless a future contract promotes them.
7373

7474
## Interop Tests

src/Transcripts/class-wp-agent-conversation-store.php

Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -19,53 +19,31 @@
1919

2020
interface WP_Agent_Conversation_Store {
2121

22-
/**
23-
* Convention key for storing the agent's registered slug inside session
24-
* `metadata`.
25-
*
26-
* Agents are slug-keyed in this substrate (`wp_register_agent`,
27-
* `wp_get_agent`, `wp_has_agent` all take strings) but the conversation
28-
* store contract carries `int $agent_id`. Until the contract is widened
29-
* to a string identifier, callers that registered an agent via
30-
* `wp_register_agent` should mirror its slug into the session metadata
31-
* under this key so that downstream consumers can resolve the agent the
32-
* same way the registry does. The value is a `WP_Agent::get_slug()`
33-
* string; recommended reading path is
34-
* `$session['metadata'][ WP_Agent_Conversation_Store::META_KEY_AGENT_SLUG ]`.
35-
*
36-
* Tracking issue: https://github.com/Automattic/agents-api/issues/95
37-
*/
38-
public const META_KEY_AGENT_SLUG = 'agent_slug';
39-
4022
/**
4123
* Create a new conversation transcript session and return its ID.
4224
*
43-
* `$agent_id` is an opaque integer; consumers without an integer agent
44-
* identifier should pass `0` and mirror the slug into `$metadata` under
45-
* {@see self::META_KEY_AGENT_SLUG}. See #95 for context on the slug-vs-int gap.
25+
* `$agent_slug` is the registered agent slug used by `wp_register_agent()`,
26+
* `wp_get_agent()`, and related registry functions. Consumers that materialize
27+
* agents as posts may store post IDs in their concrete backend, but the generic
28+
* transcript contract keys runtime agent identity by slug.
4629
*
4730
* @param WP_Agent_Workspace_Scope $workspace Workspace owning the session.
4831
* @param int $user_id WordPress user ID owning the session.
49-
* @param int $agent_id Agent ID (0 = agent-less or slug-only session).
50-
* @param array $metadata Arbitrary session metadata (JSON-serializable). When the
51-
* session belongs to a slug-registered agent, callers should
52-
* include `[ self::META_KEY_AGENT_SLUG => $slug ]`.
32+
* @param string $agent_slug Registered agent slug, or empty string for agent-less sessions.
33+
* @param array $metadata Arbitrary session metadata (JSON-serializable).
5334
* @param string $context Execution mode ('chat', 'pipeline', 'system').
5435
* @return string Session ID (UUIDv4), or empty string on failure.
5536
*/
56-
public function create_session( WP_Agent_Workspace_Scope $workspace, int $user_id, int $agent_id = 0, array $metadata = array(), string $context = 'chat' ): string;
37+
public function create_session( WP_Agent_Workspace_Scope $workspace, int $user_id, string $agent_slug = '', array $metadata = array(), string $context = 'chat' ): string;
5738

5839
/**
5940
* Retrieve a transcript session by ID.
6041
*
6142
* Returns the session as an associative array with keys:
62-
* session_id, workspace_type, workspace_id, user_id, agent_id, title, messages (decoded array),
43+
* session_id, workspace_type, workspace_id, user_id, agent_slug, title, messages (decoded array),
6344
* metadata (decoded array), provider, model, provider_response_id, context/mode, created_at,
6445
* updated_at, last_read_at, expires_at.
6546
*
66-
* The agent slug, when present, lives at
67-
* `$session['metadata'][ self::META_KEY_AGENT_SLUG ]`.
68-
*
6947
* @param string $session_id Session UUID.
7048
* @return array|null Session data or null if not found.
7149
*/
@@ -76,8 +54,7 @@ public function get_session( string $session_id ): ?array;
7654
*
7755
* @param string $session_id Session UUID.
7856
* @param array $messages Complete messages array (not a delta).
79-
* @param array $metadata Updated metadata. Slug-registered agents should keep
80-
* the {@see self::META_KEY_AGENT_SLUG} entry in sync.
57+
* @param array $metadata Updated metadata.
8158
* @param string $provider Optional AI provider identifier.
8259
* @param string $model Optional AI model identifier.
8360
* @param string|null $provider_response_id Opaque provider-side response/state ID, or null when none.

tests/workspace-scope-smoke.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@
8686

8787
agents_api_smoke_assert_equals( 'workspace', $create_params[0]->getName(), 'create_session first parameter is workspace', $failures, $passes );
8888
agents_api_smoke_assert_equals( $workspace_class, $create_params[0]->getType()->getName(), 'create_session workspace parameter is typed', $failures, $passes );
89+
agents_api_smoke_assert_equals( 'agent_slug', $create_params[2]->getName(), 'create_session accepts registered agent slug', $failures, $passes );
90+
agents_api_smoke_assert_equals( 'string', $create_params[2]->getType()->getName(), 'create_session agent slug is string typed', $failures, $passes );
91+
agents_api_smoke_assert_equals( '', $create_params[2]->getDefaultValue(), 'create_session agent slug defaults to agent-less session', $failures, $passes );
8992
agents_api_smoke_assert_equals( 'provider_response_id', $update_params[5]->getName(), 'update_session accepts provider response ID', $failures, $passes );
9093
agents_api_smoke_assert_equals( true, $update_params[5]->allowsNull(), 'provider response ID can be null when no provider state exists', $failures, $passes );
9194
agents_api_smoke_assert_equals( 'workspace', $pending_params[0]->getName(), 'get_recent_pending_session first parameter is workspace', $failures, $passes );

0 commit comments

Comments
 (0)