-
Notifications
You must be signed in to change notification settings - Fork 2.2k
feat(ai): add secure page-aware context to AI conversations #1511
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
2f0f9e7
fix(ui-vue3): restore AI drawer styles and frontend dependencies
eed0065
feat(ui-vue3): add secure page context collection for AI conversations
c1f627a
feat(ui-vue3): add home context preview and selection controls
19c6060
feat(ai): pass sanitized page context through chat requests
cb22710
fix(ui-vue3): reset one-shot AI context before session creation
ecf136f
feat(ai): upgrade default model to Qwen 3.7 Max
3d39ede
feat(ui-vue3): add AI context for resources and rule drafts
513ea12
test(ui-vue3): add AI page context integration coverage
09527a3
feat(ui-vue3): add AI context for search filters
72e7867
fix(ui-vue3): keep dashboard branding and icons within bounds
0e7df12
feat(ai): expand page context coverage across admin views
8b25ef8
refactor(ui-vue3): extract reusable AI chat drawer
3329a54
fix(ui-vue3): prevent unsafe HTML rendering in AI chat
a142766
refactor(ai): clarify page context injection naming
0b42728
chore(ai): merge upstream agent orchestration updates
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package react | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
|
|
||
| "dubbo-admin-ai/schema" | ||
|
|
||
| "github.com/firebase/genkit/go/ai" | ||
| ) | ||
|
|
||
| type currentPageContextKey struct{} | ||
|
|
||
| type currentPageContextEnvelope struct { | ||
| Kind string `json:"kind"` | ||
| Trust string `json:"trust"` | ||
| Context *schema.AIContextSnapshot `json:"context"` | ||
| } | ||
|
|
||
| func withCurrentPageContext(ctx context.Context, snapshot *schema.AIContextSnapshot) context.Context { | ||
| if snapshot == nil { | ||
| return ctx | ||
| } | ||
| return context.WithValue(ctx, currentPageContextKey{}, snapshot) | ||
| } | ||
|
|
||
| func injectCurrentPageContext(ctx context.Context, messages []*ai.Message) ([]*ai.Message, error) { | ||
| snapshot, ok := ctx.Value(currentPageContextKey{}).(*schema.AIContextSnapshot) | ||
| if !ok || snapshot == nil { | ||
| return messages, nil | ||
| } | ||
|
|
||
| payload, err := json.Marshal(currentPageContextEnvelope{ | ||
| Kind: "page_context", | ||
| Trust: "untrusted_observation", | ||
| Context: snapshot, | ||
| }) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to marshal current AI context: %w", err) | ||
| } | ||
|
|
||
| insertAt := len(messages) | ||
| for index := len(messages) - 1; index >= 0; index-- { | ||
| if messages[index].Role == ai.RoleUser { | ||
| insertAt = index | ||
| break | ||
| } | ||
| } | ||
|
|
||
| result := make([]*ai.Message, 0, len(messages)+1) | ||
| result = append(result, messages[:insertAt]...) | ||
| result = append(result, ai.NewUserMessage(ai.NewJSONPart(string(payload)))) | ||
| result = append(result, messages[insertAt:]...) | ||
| return result, nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package react | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "dubbo-admin-ai/component/memory" | ||
| "dubbo-admin-ai/schema" | ||
|
|
||
| "github.com/firebase/genkit/go/ai" | ||
| ) | ||
|
|
||
| func TestInjectCurrentPageContext(t *testing.T) { | ||
| history := []*ai.Message{ | ||
| ai.NewUserMessage(ai.NewTextPart("previous question")), | ||
| ai.NewModelMessage(ai.NewTextPart("previous answer")), | ||
| ai.NewUserMessage(ai.NewTextPart("current question")), | ||
| ai.NewModelMessage(ai.NewTextPart("current thought")), | ||
| } | ||
| snapshot := &schema.AIContextSnapshot{ | ||
| Version: schema.AIContextVersion, | ||
| CapturedAt: "2026-07-19T13:00:00Z", | ||
| Global: schema.AIContextGlobal{Locale: "cn"}, | ||
| Page: schema.AIContextPage{Path: "/home"}, | ||
| Scope: schema.AIContextScope{Mesh: "nacos2.5"}, | ||
| } | ||
|
|
||
| messages, err := injectCurrentPageContext(withCurrentPageContext(context.Background(), snapshot), history) | ||
| if err != nil { | ||
| t.Fatalf("injectCurrentPageContext() error = %v", err) | ||
| } | ||
| if len(messages) != 5 || len(history) != 4 { | ||
| t.Fatalf("message lengths = (%d, %d), want (5, 4)", len(messages), len(history)) | ||
| } | ||
| contextMessage := messages[2] | ||
| if contextMessage.Role != ai.RoleUser || len(contextMessage.Content) != 1 { | ||
| t.Fatalf("unexpected context message: %#v", contextMessage) | ||
| } | ||
| if messages[3].Content[0].Text != "current question" || messages[4].Content[0].Text != "current thought" { | ||
| t.Fatalf("context changed the current turn order: %#v", messages) | ||
| } | ||
| var envelope currentPageContextEnvelope | ||
| if err := json.Unmarshal([]byte(contextMessage.Content[0].Text), &envelope); err != nil { | ||
| t.Fatalf("unmarshal context message: %v", err) | ||
| } | ||
| if envelope.Trust != "untrusted_observation" || envelope.Context.Scope.Mesh != "nacos2.5" { | ||
| t.Fatalf("unexpected context envelope: %#v", envelope) | ||
| } | ||
| } | ||
|
|
||
| func TestNewInteractionCarriesPageContext(t *testing.T) { | ||
| snapshot := &schema.AIContextSnapshot{ | ||
| Version: schema.AIContextVersion, | ||
| Page: schema.AIContextPage{Path: "/home"}, | ||
| Scope: schema.AIContextScope{Mesh: "nacos2.5"}, | ||
| } | ||
| ra := &ReActAgent{memoryCtx: memory.NewMemoryContext(memory.ChatHistoryKey)} | ||
|
|
||
| ctx, _, history, err := ra.newInteraction(&schema.UserInput{ | ||
| Content: "current question", | ||
| Context: snapshot, | ||
| }, "session") | ||
| if err != nil { | ||
| t.Fatalf("newInteraction() error = %v", err) | ||
| } | ||
| messages, err := injectCurrentPageContext(ctx, history.WindowMemory("session")) | ||
| if err != nil { | ||
| t.Fatalf("injectCurrentPageContext() error = %v", err) | ||
| } | ||
| if len(messages) != 2 || messages[1].Content[0].Text != "current question" { | ||
| t.Fatalf("unexpected interaction messages: %#v", messages) | ||
| } | ||
| } | ||
|
|
||
| func TestUserInputContextIsNotSerialized(t *testing.T) { | ||
| input := schema.UserInput{ | ||
| Content: "hello", | ||
| Context: &schema.AIContextSnapshot{Version: schema.AIContextVersion}, | ||
| } | ||
| data, err := json.Marshal(input) | ||
| if err != nil { | ||
| t.Fatalf("marshal UserInput: %v", err) | ||
| } | ||
| if strings.Contains(string(data), "context") { | ||
| t.Fatalf("serialized history contains page context: %s", data) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Discussion:这里的Context命名感觉有点太宽泛了?在agent项目中,context一般指的是给到agent的上下文,这里其实特指从前端传过来的页面的上下文。