-
Notifications
You must be signed in to change notification settings - Fork 1
feat(extension): add tool runtime hooks #48
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
+535
−78
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,15 @@ | ||
| package assistant | ||
|
|
||
| import ( | ||
| "context" | ||
| ) | ||
|
|
||
| // DispatchToolCallLifecycleForTest exposes tool call lifecycle dispatch for external package tests. | ||
| func (runtime *Runtime) DispatchToolCallLifecycleForTest(ctx context.Context, call *ToolCallEvent) error { | ||
| return runtime.dispatchToolCallLifecycle(ctx, call) | ||
| } | ||
|
|
||
| // DispatchToolResultLifecycleForTest exposes tool result lifecycle dispatch for external package tests. | ||
| func (runtime *Runtime) DispatchToolResultLifecycleForTest(ctx context.Context, event *ToolEvent) error { | ||
| return runtime.dispatchToolResultLifecycle(ctx, event) | ||
| } |
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
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
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,154 @@ | ||
| package assistant_test | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/omarluq/librecode/internal/assistant" | ||
| ) | ||
|
|
||
| const testToolLifecycleError = "boom" | ||
|
|
||
| func TestRuntime_ToolCallLifecycleAppliesArgumentMutation(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| tests := []struct { | ||
| expectedArguments map[string]any | ||
| initialArguments map[string]any | ||
| name string | ||
| lua string | ||
| expectedArgumentsJSON string | ||
| }{ | ||
| { | ||
| name: "rewrites path and adds limit", | ||
| initialArguments: map[string]any{testToolPathKey: "README.md"}, | ||
| lua: ` | ||
| local lc = require("librecode") | ||
| lc.on("tool_call", function(event) | ||
| return { | ||
| tool_call = { | ||
| arguments = { | ||
| path = "changed.txt", | ||
| limit = 3, | ||
| }, | ||
| }, | ||
| } | ||
| end) | ||
| `, | ||
| expectedArguments: map[string]any{ | ||
| testToolPathKey: "changed.txt", | ||
| "limit": float64(3), | ||
| }, | ||
| expectedArgumentsJSON: `{"limit":3,"path":"changed.txt"}`, | ||
| }, | ||
| } | ||
|
|
||
| for _, testCase := range tests { | ||
| t.Run(testCase.name, func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| runtime, _, manager := newTestRuntimeWithManager(t, testCompletionClient{}) | ||
| loadRuntimeExtension(t, manager, testCase.lua) | ||
| call := assistant.ToolCallEvent{ | ||
| Arguments: testCase.initialArguments, | ||
| ID: "call-1", | ||
| Name: testToolName, | ||
| ArgumentsJSON: testToolArgsJSON, | ||
| } | ||
|
|
||
| err := runtime.DispatchToolCallLifecycleForTest(context.Background(), &call) | ||
|
|
||
| require.NoError(t, err) | ||
| assert.Equal(t, testCase.expectedArguments, call.Arguments) | ||
| assert.JSONEq(t, testCase.expectedArgumentsJSON, call.ArgumentsJSON) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestRuntime_ToolResultLifecycleAppliesResultMutation(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| tests := []struct { | ||
| initialEvent *assistant.ToolEvent | ||
| name string | ||
| lua string | ||
| expectedResult string | ||
| expectedDetailsJSON string | ||
| expectedError string | ||
| }{ | ||
| { | ||
| name: "redacts result and clears error", | ||
| initialEvent: &assistant.ToolEvent{ | ||
| Name: testToolName, | ||
| ArgumentsJSON: testToolArgsJSON, | ||
| DetailsJSON: "{}", | ||
| Result: "secret", | ||
| Error: testToolLifecycleError, | ||
| }, | ||
| lua: ` | ||
| local lc = require("librecode") | ||
| lc.on("tool_result", function(event) | ||
| return { | ||
| tool_result = { | ||
| result = "redacted", | ||
| details_json = "{\"redacted\":true}", | ||
| error = "", | ||
| }, | ||
| } | ||
| end) | ||
| `, | ||
| expectedResult: "redacted", | ||
| expectedDetailsJSON: `{"redacted":true}`, | ||
| expectedError: "", | ||
| }, | ||
| } | ||
|
|
||
| for _, testCase := range tests { | ||
| t.Run(testCase.name, func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| runtime, _, manager := newTestRuntimeWithManager(t, testCompletionClient{}) | ||
| loadRuntimeExtension(t, manager, testCase.lua) | ||
|
|
||
| err := runtime.DispatchToolResultLifecycleForTest(context.Background(), testCase.initialEvent) | ||
|
|
||
| require.NoError(t, err) | ||
| assert.Equal(t, testCase.expectedResult, testCase.initialEvent.Result) | ||
| assert.JSONEq(t, testCase.expectedDetailsJSON, testCase.initialEvent.DetailsJSON) | ||
| assert.Equal(t, testCase.expectedError, testCase.initialEvent.Error) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestRuntime_ToolResultLifecycleDispatchesToolErrorHandlers(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| runtime, _, manager := newTestRuntimeWithManager(t, testCompletionClient{}) | ||
| loadRuntimeExtension(t, manager, ` | ||
| local lc = require("librecode") | ||
| local seen = "" | ||
| lc.on("tool_error", function(event) | ||
| seen = event.payload.name .. ":" .. event.payload.error | ||
| end) | ||
| lc.register_command("seen_tool_error", "seen_tool_error", function() | ||
| return seen | ||
| end) | ||
| `) | ||
| event := &assistant.ToolEvent{ | ||
| Name: testToolName, | ||
| ArgumentsJSON: testToolArgsJSON, | ||
| DetailsJSON: "", | ||
| Result: testToolLifecycleError, | ||
| Error: testToolLifecycleError, | ||
| } | ||
|
|
||
| err := runtime.DispatchToolResultLifecycleForTest(context.Background(), event) | ||
|
|
||
| require.NoError(t, err) | ||
| output, err := manager.ExecuteCommand(context.Background(), "seen_tool_error", "") | ||
| require.NoError(t, err) | ||
| assert.Equal(t, "read:boom", output) | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.