diff --git a/packages/personal-ai-chat/TECHNICAL.md b/packages/personal-ai-chat/TECHNICAL.md index 8043e29..e114ce2 100644 --- a/packages/personal-ai-chat/TECHNICAL.md +++ b/packages/personal-ai-chat/TECHNICAL.md @@ -10,6 +10,12 @@ Connectors rather than package-local credentials. Save user/assistant turns in conversation Knowledge as `pos/ai-message` blocks; preserve meaningful model and response metadata (`pos_model`, `pos_last_response_id`). +The selected conversation is addressable as +`/ai-chat/?conversation=` and follows browser history. AI Chat owns +native permalinks and shortlinks for Knowledge rows carrying both `conversation` +and `ai-chat`. An editable legacy `?p=` request redirects to that app +URL; unauthorized requests retain WordPress's normal response. + Use the package's PHP class and block metadata for exact REST and block schemas. Do not restore old monolith `/pos/v1/openai/*` transport or root editor assets. diff --git a/packages/personal-ai-chat/includes/class-personal-ai-chat-plugin.php b/packages/personal-ai-chat/includes/class-personal-ai-chat-plugin.php index 0f801b1..5c481ab 100644 --- a/packages/personal-ai-chat/includes/class-personal-ai-chat-plugin.php +++ b/packages/personal-ai-chat/includes/class-personal-ai-chat-plugin.php @@ -53,6 +53,9 @@ public function register() { $this->vocabulary()->register_type_labels(); $this->register_missing_knowledge_notice(); $this->register_wp_app( array( $this, 'render_admin_page' ) ); + add_action( 'template_redirect', array( $this, 'redirect_conversation_link' ), 5 ); + add_filter( 'post_type_link', array( $this, 'filter_conversation_permalink' ), 10, 2 ); + add_filter( 'get_shortlink', array( $this, 'filter_conversation_shortlink' ), 10, 2 ); add_action( 'admin_menu', array( $this, 'add_admin_menu' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_assets' ) ); add_action( 'rest_api_init', array( $this, 'register_rest_routes' ) ); @@ -89,6 +92,47 @@ public function register_blocks() { $this->register_block_from_package( 'build/blocks/tool' ); } + /** + * Return the AI Chat app URL for a conversation. + * + * @param int $post_id Post ID. + * @return string Empty when the post is not an AI Chat conversation. + */ + public function get_conversation_url( $post_id ) { + $post = get_post( $post_id ); + if ( ! $this->is_conversation( $post ) ) { + return ''; + } + + return add_query_arg( 'conversation', $post->ID, home_url( '/ai-chat/' ) ); + } + + /** Filter a conversation permalink to its AI Chat interface URL. */ + public function filter_conversation_permalink( $url, $post ) { + $conversation_url = $post instanceof WP_Post ? $this->get_conversation_url( $post->ID ) : ''; + + return $conversation_url ? $conversation_url : $url; + } + + /** Filter a conversation shortlink to its AI Chat interface URL. */ + public function filter_conversation_shortlink( $shortlink, $post_id ) { + $conversation_url = $this->get_conversation_url( $post_id ); + + return $conversation_url ? $conversation_url : $shortlink; + } + + /** Redirect an editable legacy ?p= link to the AI Chat conversation. */ + public function redirect_conversation_link() { + $post_id = absint( get_query_var( 'p' ) ); + $conversation_url = $this->get_conversation_url( $post_id ); + if ( get_query_var( 'preview' ) || ! $conversation_url || ! current_user_can( 'edit_post', $post_id ) ) { + return; + } + + wp_safe_redirect( $conversation_url ); + exit; + } + /** * Register package REST routes. * @@ -464,7 +508,7 @@ public function rest_generate_message( WP_REST_Request $request ) { * @return WP_REST_Response */ public function rest_list_abilities() { - return rest_ensure_response( array_map( array( $this, 'format_ability' ), $this->discover_abilities() ) ); + return rest_ensure_response( array_values( array_map( array( $this, 'format_ability' ), $this->discover_abilities() ) ) ); } /** @@ -823,6 +867,17 @@ private function extract_conversation_messages( $content ) { * @return array */ private function format_ability( $ability ) { + if ( is_object( $ability ) && is_callable( array( $ability, 'get_name' ) ) ) { + return array( + 'name' => sanitize_text_field( $ability->get_name() ), + 'label' => sanitize_text_field( $ability->get_label() ), + 'description' => sanitize_text_field( $ability->get_description() ), + 'input_schema' => $ability->get_input_schema(), + 'output_schema' => $ability->get_output_schema(), + 'meta' => $ability->get_meta(), + ); + } + $ability = (array) $ability; return array( diff --git a/packages/personal-ai-chat/src/index.js b/packages/personal-ai-chat/src/index.js index 42b3820..5694f99 100644 --- a/packages/personal-ai-chat/src/index.js +++ b/packages/personal-ai-chat/src/index.js @@ -69,10 +69,17 @@ function AiChatAdmin() { path: '/personal-ai-chat/v1/conversations', } ); setConversations( response ); - if ( ! active && response.length ) { - setActive( response[ 0 ] ); - setTitle( response[ 0 ].title ); - } + const conversationId = Number( + new URLSearchParams( window.location.search ).get( + 'conversation' + ) + ); + const selected = + response.find( ( item ) => item.id === conversationId ) || + response[ 0 ] || + null; + setActive( selected ); + setTitle( selected?.title || '' ); } catch ( error ) { setNotice( { status: 'error', @@ -83,7 +90,7 @@ function AiChatAdmin() { } finally { setLoading( false ); } - }, [ active ] ); + }, [] ); useEffect( () => { document.body.classList.add( 'personal-ai-chat-js' ); @@ -93,6 +100,40 @@ function AiChatAdmin() { .catch( () => setAbilities( [] ) ); }, [ fetchConversations ] ); + useEffect( () => { + function syncConversationToRoute() { + const conversationId = Number( + new URLSearchParams( window.location.search ).get( + 'conversation' + ) + ); + const selected = + conversations.find( ( item ) => item.id === conversationId ) || + conversations[ 0 ]; + + if ( selected ) { + setActive( selected ); + setTitle( selected.title ); + } + } + + window.addEventListener( 'popstate', syncConversationToRoute ); + return () => + window.removeEventListener( 'popstate', syncConversationToRoute ); + }, [ conversations ] ); + + function selectConversation( conversation ) { + setActive( conversation ); + setTitle( conversation.title ); + const route = new URLSearchParams( window.location.search ); + route.set( 'conversation', conversation.id ); + window.history.pushState( + {}, + '', + `${ window.location.pathname }?${ route }` + ); + } + async function createConversation() { setSaving( true ); setNotice( null ); @@ -108,8 +149,7 @@ function AiChatAdmin() { content: '', }, } ); - setActive( response ); - setTitle( response.title ); + selectConversation( response ); setConversations( ( current ) => [ response, ...current ] ); setNotice( { status: 'success', @@ -221,10 +261,9 @@ function AiChatAdmin() { key={ conversation.id } type="button" className="personal-ai-chat-admin__conversation" - onClick={ () => { - setActive( conversation ); - setTitle( conversation.title ); - } } + onClick={ () => + selectConversation( conversation ) + } > { conversation.title } { conversation.modified_gmt } diff --git a/packages/personal-notes/TECHNICAL.md b/packages/personal-notes/TECHNICAL.md index 9ac6598..d62b934 100644 --- a/packages/personal-notes/TECHNICAL.md +++ b/packages/personal-notes/TECHNICAL.md @@ -25,6 +25,11 @@ The library lists `note` records. Stuff uses `artifact` + `stuff-item`, without `note`, and should not automatically appear as ordinary Notes rows. Preserve source/provenance terms (`manual`, `readwise`, `evernote`, `synced`) during edits. +Notes owns native permalinks and shortlinks for Knowledge rows carrying the +`note` identity and resolves them to the native WordPress editor. An editable +legacy `?p=` request redirects to that editor; unauthorized requests +retain WordPress's normal response. + ## Gutenberg sidebar The package-local sidebar searches Knowledge, filters by Knowledge Type, previews diff --git a/packages/personal-notes/includes/class-personal-notes-plugin.php b/packages/personal-notes/includes/class-personal-notes-plugin.php index 6566fdf..38b3dd7 100644 --- a/packages/personal-notes/includes/class-personal-notes-plugin.php +++ b/packages/personal-notes/includes/class-personal-notes-plugin.php @@ -46,6 +46,9 @@ public function register() { $this->vocabulary()->register_type_labels(); $this->register_missing_knowledge_notice(); $this->register_wp_app( array( $this, 'render_admin_page' ) ); + add_action( 'template_redirect', array( $this, 'redirect_note_link' ), 5 ); + add_filter( 'post_type_link', array( $this, 'filter_note_permalink' ), 10, 2 ); + add_filter( 'get_shortlink', array( $this, 'filter_note_shortlink' ), 10, 2 ); add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_assets' ) ); add_action( 'rest_api_init', array( $this, 'register_rest_routes' ) ); @@ -155,6 +158,53 @@ public function enqueue_package_assets() { ); } + /** + * Return the native editor URL for a note. + * + * @param int $post_id Post ID. + * @return string Empty when the post is not a note. + */ + public function get_note_url( $post_id ) { + $post = get_post( $post_id ); + if ( ! $this->is_note( $post ) ) { + return ''; + } + + return add_query_arg( + array( + 'post' => $post->ID, + 'action' => 'edit', + ), + admin_url( 'post.php' ) + ); + } + + /** Filter a note permalink to its native editor URL. */ + public function filter_note_permalink( $url, $post ) { + $note_url = $post instanceof WP_Post ? $this->get_note_url( $post->ID ) : ''; + + return $note_url ? $note_url : $url; + } + + /** Filter a note shortlink to its native editor URL. */ + public function filter_note_shortlink( $shortlink, $post_id ) { + $note_url = $this->get_note_url( $post_id ); + + return $note_url ? $note_url : $shortlink; + } + + /** Redirect an editable legacy ?p= link to the native note editor. */ + public function redirect_note_link() { + $post_id = absint( get_query_var( 'p' ) ); + $note_url = $this->get_note_url( $post_id ); + if ( get_query_var( 'preview' ) || ! $note_url || ! current_user_can( 'edit_post', $post_id ) ) { + return; + } + + wp_safe_redirect( $note_url ); + exit; + } + /** * Enqueue the Notes sidebar in native post block editors. * diff --git a/packages/personal-stuff/TECHNICAL.md b/packages/personal-stuff/TECHNICAL.md index fb6a762..2874ef8 100644 --- a/packages/personal-stuff/TECHNICAL.md +++ b/packages/personal-stuff/TECHNICAL.md @@ -34,6 +34,11 @@ The app assigns the leaf identity, physical location and tags, preserving unrela Derived in memory: breadcrumbs, descendants, cover, photo count, search index, filtered collection, previous/next order. Filters and item selection are URL query parameters (`q`, `place`, `tag`, `photo`, `item`); place/tag filter values are slugs. DataViews layout and paging stay in memory. There is no localStorage database, service worker or offline write queue. +Stuff owns links for Knowledge rows carrying the `stuff-item` identity. Native +permalinks and shortlinks resolve to `/stuff/?item=`. An editable legacy +`?p=` request redirects there as well; unauthorized requests retain +WordPress's normal response. + ## Content and editing Core paragraphs, images and galleries are the only native structures needed. Gallery children and document block order define photo order; the first image is the cover. Captions, alt text, image links, gallery layout and any other blocks live in their ordinary block attributes/HTML. diff --git a/packages/personal-stuff/includes/class-personal-stuff-plugin.php b/packages/personal-stuff/includes/class-personal-stuff-plugin.php index 0d93cd5..537b999 100644 --- a/packages/personal-stuff/includes/class-personal-stuff-plugin.php +++ b/packages/personal-stuff/includes/class-personal-stuff-plugin.php @@ -40,6 +40,9 @@ public function register() { $this->vocabulary()->register_rest_descriptions(); $this->register_missing_knowledge_notice(); $this->register_wp_app( array( $this, 'render_app' ) ); + add_action( 'template_redirect', array( $this, 'redirect_item_link' ), 5 ); + add_filter( 'post_type_link', array( $this, 'filter_item_permalink' ), 10, 2 ); + add_filter( 'get_shortlink', array( $this, 'filter_item_shortlink' ), 10, 2 ); add_action( 'admin_menu', array( $this, 'add_admin_menu' ) ); // Recheck actual terms when Knowledge becomes available; no installed flag. $this->ensure_terms(); @@ -125,6 +128,59 @@ public function is_item( $post_id ) { return 'wp_knowledge' === get_post_type( $post_id ) && has_term( 'stuff-item', 'wp_knowledge_type', $post_id ); } + /** + * Return the Stuff app URL for an item. + * + * @param int $post_id Post ID. + * @return string Empty when the post is not a Stuff item. + */ + public function get_item_url( $post_id ) { + $post = get_post( $post_id ); + if ( ! $post instanceof WP_Post || ! $this->is_item( $post->ID ) ) { + return ''; + } + + return add_query_arg( 'item', $post->ID, home_url( '/stuff/' ) ); + } + + /** + * Point native Knowledge permalinks at the owning Stuff interface. + * + * @param string $url Existing permalink. + * @param WP_Post $post Post object. + * @return string + */ + public function filter_item_permalink( $url, $post ) { + $item_url = $post instanceof WP_Post ? $this->get_item_url( $post->ID ) : ''; + + return $item_url ? $item_url : $url; + } + + /** + * Point native shortlinks at the owning Stuff interface. + * + * @param string $shortlink Existing shortlink. + * @param int $post_id Post ID. + * @return string + */ + public function filter_item_shortlink( $shortlink, $post_id ) { + $item_url = $this->get_item_url( $post_id ); + + return $item_url ? $item_url : $shortlink; + } + + /** Redirect an editable legacy ?p= link to the Stuff item interface. */ + public function redirect_item_link() { + $post_id = absint( get_query_var( 'p' ) ); + $item_url = $this->get_item_url( $post_id ); + if ( get_query_var( 'preview' ) || ! $item_url || ! current_user_can( 'edit_post', $post_id ) ) { + return; + } + + wp_safe_redirect( $item_url ); + exit; + } + /** * Keep empty Stuff items in Gutenberg even with Personal Notes active. * diff --git a/packages/personal-todo/TECHNICAL.md b/packages/personal-todo/TECHNICAL.md index 8487813..0a85da1 100644 --- a/packages/personal-todo/TECHNICAL.md +++ b/packages/personal-todo/TECHNICAL.md @@ -11,6 +11,11 @@ Preserve compact quick-add, the DataViews list/filter/actions, and the full edit modal with scheduling, recurrence, dependencies and history. Use shared Knowledge Type terms in assignment controls instead of the old notebook controls. +The selected edit modal is addressable as `/todo/?task=` and follows +browser history. TODO owns native permalinks and shortlinks for Knowledge rows +carrying the `todo` identity. An editable legacy `?p=` request redirects +to that app URL; unauthorized requests retain WordPress's normal response. + ## Runtime Dependencies Personal TODO requires a WordPress Knowledge surface but does not require any diff --git a/packages/personal-todo/includes/class-personal-todo-plugin.php b/packages/personal-todo/includes/class-personal-todo-plugin.php index b0ab829..bb57566 100644 --- a/packages/personal-todo/includes/class-personal-todo-plugin.php +++ b/packages/personal-todo/includes/class-personal-todo-plugin.php @@ -61,6 +61,9 @@ public function register() { $this->register_missing_knowledge_notice(); $this->register_wp_app( array( $this, 'render_admin_page' ) ); + add_action( 'template_redirect', array( $this, 'redirect_task_link' ), 5 ); + add_filter( 'post_type_link', array( $this, 'filter_task_permalink' ), 10, 2 ); + add_filter( 'get_shortlink', array( $this, 'filter_task_shortlink' ), 10, 2 ); add_action( 'admin_menu', array( $this, 'add_admin_menu' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_assets' ) ); add_action( 'rest_api_init', array( $this, 'register_rest_routes' ) ); @@ -140,6 +143,47 @@ public function enqueue_package_assets() { ); } + /** + * Return the TODO app URL for a task. + * + * @param int $post_id Post ID. + * @return string Empty when the post is not a task. + */ + public function get_task_url( $post_id ) { + $post = get_post( $post_id ); + if ( ! $this->is_task( $post ) ) { + return ''; + } + + return add_query_arg( 'task', $post->ID, home_url( '/todo/' ) ); + } + + /** Filter a task permalink to its TODO interface URL. */ + public function filter_task_permalink( $url, $post ) { + $task_url = $post instanceof WP_Post ? $this->get_task_url( $post->ID ) : ''; + + return $task_url ? $task_url : $url; + } + + /** Filter a task shortlink to its TODO interface URL. */ + public function filter_task_shortlink( $shortlink, $post_id ) { + $task_url = $this->get_task_url( $post_id ); + + return $task_url ? $task_url : $shortlink; + } + + /** Redirect an editable legacy ?p= link to the TODO task interface. */ + public function redirect_task_link() { + $post_id = absint( get_query_var( 'p' ) ); + $task_url = $this->get_task_url( $post_id ); + if ( get_query_var( 'preview' ) || ! $task_url || ! current_user_can( 'edit_post', $post_id ) ) { + return; + } + + wp_safe_redirect( $task_url ); + exit; + } + /** * Render a small native admin UI. * diff --git a/packages/personal-todo/src/index.js b/packages/personal-todo/src/index.js index c479a22..417c411 100644 --- a/packages/personal-todo/src/index.js +++ b/packages/personal-todo/src/index.js @@ -421,6 +421,23 @@ function TodoAdmin() { .finally( () => setLoading( false ) ); }, [] ); + useEffect( () => { + function syncEditorToRoute() { + const taskId = Number( + new URLSearchParams( window.location.search ).get( 'task' ) + ); + const task = tasks.find( ( item ) => item.id === taskId ); + + setEditedTask( task || null ); + setEditDraft( task ? draftFromTask( task ) : null ); + } + + syncEditorToRoute(); + window.addEventListener( 'popstate', syncEditorToRoute ); + return () => + window.removeEventListener( 'popstate', syncEditorToRoute ); + }, [ tasks ] ); + const termNames = useCallback( ( task ) => task.terms @@ -596,8 +613,9 @@ function TodoAdmin() { ...current.filter( ( item ) => item.id !== task.id ), ] ); setDraft( EMPTY_DRAFT ); - setEditedTask( null ); - setEditDraft( null ); + if ( taskDraft.id ) { + closeEditor(); + } setNotice( { status: 'success', message: taskDraft.id @@ -691,6 +709,25 @@ function TodoAdmin() { function openEditor( task ) { setEditedTask( task ); setEditDraft( draftFromTask( task ) ); + const route = new URLSearchParams( window.location.search ); + route.set( 'task', task.id ); + window.history.pushState( + {}, + '', + `${ window.location.pathname }?${ route }` + ); + } + + function closeEditor() { + setEditedTask( null ); + setEditDraft( null ); + const route = new URLSearchParams( window.location.search ); + route.delete( 'task' ); + window.history.pushState( + {}, + '', + `${ window.location.pathname }${ route.size ? `?${ route }` : '' }` + ); } const actions = [ @@ -779,14 +816,14 @@ function TodoAdmin() { __( 'Edit %s', 'personal-todo' ), editedTask.title ) } - onRequestClose={ () => setEditedTask( null ) } + onRequestClose={ closeEditor } className="personal-todo-admin__modal" > saveTask( event, editDraft ) } - onCancel={ () => setEditedTask( null ) } + onCancel={ closeEditor } saving={ saving } tasks={ tasks } terms={ terms } diff --git a/tests/unit/PersonalStuffTest.php b/tests/unit/PersonalStuffTest.php index a5c3ffa..adfe5fa 100644 --- a/tests/unit/PersonalStuffTest.php +++ b/tests/unit/PersonalStuffTest.php @@ -92,6 +92,18 @@ public function test_editor_selection_coexists_with_notes_for_empty_and_block_it $this->assertTrue( use_block_editor_for_post( $block ) ); } + public function test_item_links_resolve_to_the_stuff_interface() { + $item_id = $this->item(); + $expected = home_url( '/stuff/?item=' . $item_id ); + + $this->assertSame( $expected, $this->plugin->get_item_url( $item_id ) ); + $this->assertSame( $expected, $this->plugin->filter_item_permalink( 'https://example.com/original', get_post( $item_id ) ) ); + $this->assertSame( $expected, $this->plugin->filter_item_shortlink( 'https://example.com/?p=' . $item_id, $item_id ) ); + + $post_id = self::factory()->post->create( array( 'post_type' => 'wp_knowledge' ) ); + $this->assertSame( '', $this->plugin->get_item_url( $post_id ) ); + } + public function test_upload_names_are_random_only_for_authorized_item_context() { $file = array( 'name' => 'secret-camera.JPG', 'type' => 'image/jpeg', 'tmp_name' => '/tmp/example', 'error' => 0 ); $request = new WP_REST_Request( 'POST', '/wp/v2/media' ); diff --git a/tests/unit/SplitPackageKnowledgeTest.php b/tests/unit/SplitPackageKnowledgeTest.php index f08bc44..d4460d0 100644 --- a/tests/unit/SplitPackageKnowledgeTest.php +++ b/tests/unit/SplitPackageKnowledgeTest.php @@ -112,6 +112,42 @@ public function test_packages_register_wp_app_routes() { $this->assertArrayNotHasKey( 'evernote', $apps ); } + /** + * Each app independently owns links for its canonical Knowledge identity. + */ + public function test_packages_resolve_owned_knowledge_links() { + $this->setExpectedIncorrectUsage( 'WP_Block_Type_Registry::register' ); + $notes = new Personal_Notes_Plugin(); + $todo = new Personal_TODO_Plugin(); + $chat = new Personal_AI_Chat_Plugin(); + $notes->register(); + $todo->register(); + $chat->register(); + + $note_id = $notes->create_note( 'Linked note', 'Body' ); + $task_id = $todo->create_task( array( 'post_title' => 'Linked task' ) ); + $chat_id = $chat->create_conversation( 'Linked conversation', '' ); + + $note_url = add_query_arg( array( 'post' => $note_id, 'action' => 'edit' ), admin_url( 'post.php' ) ); + $this->assertSame( $note_url, $notes->get_note_url( $note_id ) ); + $this->assertSame( home_url( '/todo/?task=' . $task_id ), $todo->get_task_url( $task_id ) ); + $this->assertSame( home_url( '/ai-chat/?conversation=' . $chat_id ), $chat->get_conversation_url( $chat_id ) ); + $this->assertSame( $note_url, get_permalink( $note_id ) ); + $this->assertSame( home_url( '/todo/?task=' . $task_id ), get_permalink( $task_id ) ); + $this->assertSame( home_url( '/ai-chat/?conversation=' . $chat_id ), wp_get_shortlink( $chat_id ) ); + + $original = 'https://example.com/?p=' . $note_id; + $this->assertSame( $original, $todo->filter_task_shortlink( $original, $note_id ) ); + $this->assertSame( $original, $chat->filter_conversation_shortlink( $original, $note_id ) ); + $this->assertSame( $note_url, $notes->filter_note_shortlink( $original, $note_id ) ); + + $abilities = $chat->rest_list_abilities()->get_data(); + $this->assertSame( array_values( $abilities ), $abilities ); + foreach ( $abilities as $ability ) { + $this->assertNotEmpty( $ability['name'] ); + } + } + /** * Manual notes are private Knowledge artifacts with note/manual terms. */