diff --git a/agent/assistant/agent.go b/agent/assistant/agent.go index 2cdc2970..2f515fc0 100644 --- a/agent/assistant/agent.go +++ b/agent/assistant/agent.go @@ -84,9 +84,9 @@ func (ast *Assistant) Stream(ctx *context.Context, inputMessages []context.Messa // ================================================ // Request Create hook ( Optional ) var createResponse *context.HookCreateResponse - if ast.Script != nil { + if ast.HookScript != nil { var err error - createResponse, opts, err = ast.Script.Create(ctx, fullMessages, opts) + createResponse, opts, err = ast.HookScript.Create(ctx, fullMessages, opts) if err != nil { ast.traceAgentFail(agentNode, err) // Send error stream_end for root stack @@ -234,9 +234,9 @@ func (ast *Assistant) Stream(ctx *context.Context, inputMessages []context.Messa var finalResponse interface{} var nextResponse *context.NextHookResponse = nil - if ast.Script != nil { + if ast.HookScript != nil { var err error - nextResponse, opts, err = ast.Script.Next(ctx, &context.NextHookPayload{ + nextResponse, opts, err = ast.HookScript.Next(ctx, &context.NextHookPayload{ Messages: fullMessages, Completion: completionResponse, Tools: toolCallResponses, diff --git a/agent/assistant/assistant.go b/agent/assistant/assistant.go index 5238bd8b..3e385256 100644 --- a/agent/assistant/assistant.go +++ b/agent/assistant/assistant.go @@ -183,9 +183,9 @@ func (ast *Assistant) Clone() *Assistant { CreatedAt: ast.CreatedAt, UpdatedAt: ast.UpdatedAt, }, - Search: ast.Search, - Script: ast.Script, - openai: ast.openai, + Search: ast.Search, + HookScript: ast.HookScript, + openai: ast.openai, } // Deep copy tags diff --git a/agent/assistant/build_mcp_test.go b/agent/assistant/build_mcp_test.go index 805df44e..b226cd5e 100644 --- a/agent/assistant/build_mcp_test.go +++ b/agent/assistant/build_mcp_test.go @@ -214,8 +214,8 @@ func TestBuildRequest_MCP(t *testing.T) { // Call create hook to get createResponse var createResponse *context.HookCreateResponse - if hookAgent.Script != nil { - createResponse, _, err = hookAgent.Script.Create(hookCtx, inputMessages, &context.Options{}) + if hookAgent.HookScript != nil { + createResponse, _, err = hookAgent.HookScript.Create(hookCtx, inputMessages, &context.Options{}) if err != nil { t.Fatalf("Failed to call create hook: %s", err.Error()) } diff --git a/agent/assistant/build_prompts_test.go b/agent/assistant/build_prompts_test.go index 529a015a..962989d3 100644 --- a/agent/assistant/build_prompts_test.go +++ b/agent/assistant/build_prompts_test.go @@ -636,7 +636,7 @@ func TestPromptPresetAssistant(t *testing.T) { assert.Contains(t, ast.PromptPresets, "mode.professional") // Should have script - assert.NotNil(t, ast.Script) + assert.NotNil(t, ast.HookScript) }) t.Run("CreateHookSelectsFriendlyPreset", func(t *testing.T) { @@ -650,7 +650,7 @@ func TestPromptPresetAssistant(t *testing.T) { } // Call Create hook - createResponse, _, err := ast.Script.Create(ctx, messages, &context.Options{}) + createResponse, _, err := ast.HookScript.Create(ctx, messages, &context.Options{}) require.NoError(t, err) require.NotNil(t, createResponse) assert.Equal(t, "mode.friendly", createResponse.PromptPreset) @@ -681,7 +681,7 @@ func TestPromptPresetAssistant(t *testing.T) { } // Call Create hook - createResponse, _, err := ast.Script.Create(ctx, messages, &context.Options{}) + createResponse, _, err := ast.HookScript.Create(ctx, messages, &context.Options{}) require.NoError(t, err) require.NotNil(t, createResponse) assert.Equal(t, "mode.professional", createResponse.PromptPreset) @@ -718,7 +718,7 @@ func TestPromptPresetAssistant(t *testing.T) { } // Call Create hook - createResponse, _, err := ast.Script.Create(ctx, messages, &context.Options{}) + createResponse, _, err := ast.HookScript.Create(ctx, messages, &context.Options{}) require.NoError(t, err) require.NotNil(t, createResponse) require.NotNil(t, createResponse.DisableGlobalPrompts) @@ -753,7 +753,7 @@ func TestPromptPresetAssistant(t *testing.T) { } // Call Create hook - createResponse, _, err := ast.Script.Create(ctx, messages, &context.Options{}) + createResponse, _, err := ast.HookScript.Create(ctx, messages, &context.Options{}) require.NoError(t, err) require.NotNil(t, createResponse) assert.Equal(t, "mode.friendly", createResponse.PromptPreset) @@ -788,7 +788,7 @@ func TestPromptPresetAssistant(t *testing.T) { } // Call Create hook - createResponse, _, err := ast.Script.Create(ctx, messages, &context.Options{}) + createResponse, _, err := ast.HookScript.Create(ctx, messages, &context.Options{}) require.NoError(t, err) require.NotNil(t, createResponse) assert.Equal(t, "non.existent.preset", createResponse.PromptPreset) @@ -819,7 +819,7 @@ func TestPromptPresetAssistant(t *testing.T) { } // Call Create hook - should return nil - createResponse, _, err := ast.Script.Create(ctx, messages, &context.Options{}) + createResponse, _, err := ast.HookScript.Create(ctx, messages, &context.Options{}) require.NoError(t, err) assert.Nil(t, createResponse) diff --git a/agent/assistant/build_test.go b/agent/assistant/build_test.go index 6cceddb3..c34100fe 100644 --- a/agent/assistant/build_test.go +++ b/agent/assistant/build_test.go @@ -52,7 +52,7 @@ func TestBuildRequest(t *testing.T) { t.Fatalf("Failed to get tests.buildrequest assistant: %s", err.Error()) } - if agent.Script == nil { + if agent.HookScript == nil { t.Fatalf("The tests.buildrequest assistant has no script") } @@ -63,7 +63,7 @@ func TestBuildRequest(t *testing.T) { inputMessages := []context.Message{{Role: "user", Content: "no_override"}} // Call Create hook - createResponse, _, err := agent.Script.Create(ctx, inputMessages, &context.Options{}) + createResponse, _, err := agent.HookScript.Create(ctx, inputMessages, &context.Options{}) if err != nil { t.Fatalf("Failed to call Create hook: %s", err.Error()) } @@ -111,7 +111,7 @@ func TestBuildRequest(t *testing.T) { t.Run("OverrideTemperature", func(t *testing.T) { inputMessages := []context.Message{{Role: "user", Content: "override_temperature"}} - createResponse, _, err := agent.Script.Create(ctx, inputMessages, &context.Options{}) + createResponse, _, err := agent.HookScript.Create(ctx, inputMessages, &context.Options{}) if err != nil { t.Fatalf("Failed to call Create hook: %s", err.Error()) } @@ -142,7 +142,7 @@ func TestBuildRequest(t *testing.T) { t.Run("OverrideAll", func(t *testing.T) { inputMessages := []context.Message{{Role: "user", Content: "override_all"}} - createResponse, _, err := agent.Script.Create(ctx, inputMessages, &context.Options{}) + createResponse, _, err := agent.HookScript.Create(ctx, inputMessages, &context.Options{}) if err != nil { t.Fatalf("Failed to call Create hook: %s", err.Error()) } @@ -195,7 +195,7 @@ func TestBuildRequest(t *testing.T) { t.Run("OverrideRouteMetadata", func(t *testing.T) { inputMessages := []context.Message{{Role: "user", Content: "override_route_metadata"}} - createResponse, _, err := agent.Script.Create(ctx, inputMessages, &context.Options{}) + createResponse, _, err := agent.HookScript.Create(ctx, inputMessages, &context.Options{}) if err != nil { t.Fatalf("Failed to call Create hook: %s", err.Error()) } diff --git a/agent/assistant/hook/create_bench_test.go b/agent/assistant/hook/create_bench_test.go index 2d05375a..618a8efa 100644 --- a/agent/assistant/hook/create_bench_test.go +++ b/agent/assistant/hook/create_bench_test.go @@ -27,14 +27,14 @@ func BenchmarkSimpleStandardMode(b *testing.B) { b.Fatalf("Failed to get assistant: %s", err.Error()) } - if agent.Script == nil { + if agent.HookScript == nil { b.Fatalf("Assistant has no script") } b.ResetTimer() for i := 0; i < b.N; i++ { ctx := newBenchContext("bench-simple-standard", "tests.create") - _, _, err := agent.Script.Create(ctx, []context.Message{ + _, _, err := agent.HookScript.Create(ctx, []context.Message{ {Role: "user", Content: "Hello"}, }) if err != nil { @@ -54,14 +54,14 @@ func BenchmarkSimplePerformanceMode(b *testing.B) { b.Fatalf("Failed to get assistant: %s", err.Error()) } - if agent.Script == nil { + if agent.HookScript == nil { b.Fatalf("Assistant has no script") } b.ResetTimer() for i := 0; i < b.N; i++ { ctx := newBenchContext("bench-simple-performance", "tests.create") - _, _, err := agent.Script.Create(ctx, []context.Message{ + _, _, err := agent.HookScript.Create(ctx, []context.Message{ {Role: "user", Content: "Hello"}, }) if err != nil { @@ -85,7 +85,7 @@ func BenchmarkBusinessStandardMode(b *testing.B) { b.Fatalf("Failed to get assistant: %s", err.Error()) } - if agent.Script == nil { + if agent.HookScript == nil { b.Fatalf("Assistant has no script") } @@ -95,7 +95,7 @@ func BenchmarkBusinessStandardMode(b *testing.B) { for i := 0; i < b.N; i++ { scenario := scenarios[i%len(scenarios)] ctx := newBenchContext("bench-business-standard", "tests.create") - _, _, err := agent.Script.Create(ctx, []context.Message{ + _, _, err := agent.HookScript.Create(ctx, []context.Message{ {Role: "user", Content: scenario.content}, }) if err != nil { @@ -115,7 +115,7 @@ func BenchmarkBusinessPerformanceMode(b *testing.B) { b.Fatalf("Failed to get assistant: %s", err.Error()) } - if agent.Script == nil { + if agent.HookScript == nil { b.Fatalf("Assistant has no script") } @@ -125,7 +125,7 @@ func BenchmarkBusinessPerformanceMode(b *testing.B) { for i := 0; i < b.N; i++ { scenario := scenarios[i%len(scenarios)] ctx := newBenchContext("bench-business-performance", "tests.create") - _, _, err := agent.Script.Create(ctx, []context.Message{ + _, _, err := agent.HookScript.Create(ctx, []context.Message{ {Role: "user", Content: scenario.content}, }) if err != nil { @@ -150,7 +150,7 @@ func BenchmarkConcurrentSimpleStandardMode(b *testing.B) { b.Fatalf("Failed to get assistant: %s", err.Error()) } - if agent.Script == nil { + if agent.HookScript == nil { b.Fatalf("Assistant has no script") } @@ -159,7 +159,7 @@ func BenchmarkConcurrentSimpleStandardMode(b *testing.B) { i := 0 for pb.Next() { ctx := newBenchContext("bench-concurrent-simple-standard", "tests.create") - _, _, err := agent.Script.Create(ctx, []context.Message{ + _, _, err := agent.HookScript.Create(ctx, []context.Message{ {Role: "user", Content: "Hello"}, }) if err != nil { @@ -182,7 +182,7 @@ func BenchmarkConcurrentSimplePerformanceMode(b *testing.B) { b.Fatalf("Failed to get assistant: %s", err.Error()) } - if agent.Script == nil { + if agent.HookScript == nil { b.Fatalf("Assistant has no script") } @@ -191,7 +191,7 @@ func BenchmarkConcurrentSimplePerformanceMode(b *testing.B) { i := 0 for pb.Next() { ctx := newBenchContext("bench-concurrent-simple", "tests.create") - _, _, err := agent.Script.Create(ctx, []context.Message{ + _, _, err := agent.HookScript.Create(ctx, []context.Message{ {Role: "user", Content: "Hello"}, }) if err != nil { @@ -214,7 +214,7 @@ func BenchmarkConcurrentBusinessStandardMode(b *testing.B) { b.Fatalf("Failed to get assistant: %s", err.Error()) } - if agent.Script == nil { + if agent.HookScript == nil { b.Fatalf("Assistant has no script") } @@ -226,7 +226,7 @@ func BenchmarkConcurrentBusinessStandardMode(b *testing.B) { for pb.Next() { scenario := scenarios[i%len(scenarios)] ctx := newBenchContext("bench-concurrent-business-standard", "tests.create") - _, _, err := agent.Script.Create(ctx, []context.Message{ + _, _, err := agent.HookScript.Create(ctx, []context.Message{ {Role: "user", Content: scenario.content}, }) if err != nil { @@ -249,7 +249,7 @@ func BenchmarkConcurrentBusinessPerformanceMode(b *testing.B) { b.Fatalf("Failed to get assistant: %s", err.Error()) } - if agent.Script == nil { + if agent.HookScript == nil { b.Fatalf("Assistant has no script") } @@ -261,7 +261,7 @@ func BenchmarkConcurrentBusinessPerformanceMode(b *testing.B) { for pb.Next() { scenario := scenarios[i%len(scenarios)] ctx := newBenchContext("bench-concurrent-business", "tests.create") - _, _, err := agent.Script.Create(ctx, []context.Message{ + _, _, err := agent.HookScript.Create(ctx, []context.Message{ {Role: "user", Content: scenario.content}, }) if err != nil { diff --git a/agent/assistant/hook/create_mem_test.go b/agent/assistant/hook/create_mem_test.go index 0c8e62ee..3c6f1bd8 100644 --- a/agent/assistant/hook/create_mem_test.go +++ b/agent/assistant/hook/create_mem_test.go @@ -29,14 +29,14 @@ func TestMemoryLeakStandardMode(t *testing.T) { t.Fatalf("Failed to get assistant: %s", err.Error()) } - if agent.Script == nil { + if agent.HookScript == nil { t.Fatalf("Assistant has no script") } // Warm up - execute a few times to stabilize memory for i := 0; i < 10; i++ { ctx := newMemTestContext("warmup", "tests.create") - _, _, _ = agent.Script.Create(ctx, []context.Message{ + _, _, _ = agent.HookScript.Create(ctx, []context.Message{ {Role: "user", Content: "Hello"}, }) ctx.Release() @@ -52,7 +52,7 @@ func TestMemoryLeakStandardMode(t *testing.T) { iterations := 1000 for i := 0; i < iterations; i++ { ctx := newMemTestContext("mem-test-standard", "tests.create") - _, _, err := agent.Script.Create(ctx, []context.Message{ + _, _, err := agent.HookScript.Create(ctx, []context.Message{ {Role: "user", Content: "Hello"}, }) if err != nil { @@ -117,14 +117,14 @@ func TestMemoryLeakPerformanceMode(t *testing.T) { t.Fatalf("Failed to get assistant: %s", err.Error()) } - if agent.Script == nil { + if agent.HookScript == nil { t.Fatalf("Assistant has no script") } // Warm up - execute a few times to stabilize memory and fill isolate pool for i := 0; i < 20; i++ { ctx := newMemTestContext("warmup", "tests.create") - _, _, _ = agent.Script.Create(ctx, []context.Message{ + _, _, _ = agent.HookScript.Create(ctx, []context.Message{ {Role: "user", Content: "Hello"}, }) ctx.Release() @@ -140,7 +140,7 @@ func TestMemoryLeakPerformanceMode(t *testing.T) { iterations := 1000 for i := 0; i < iterations; i++ { ctx := newMemTestContext("mem-test-performance", "tests.create") - _, _, err := agent.Script.Create(ctx, []context.Message{ + _, _, err := agent.HookScript.Create(ctx, []context.Message{ {Role: "user", Content: "Hello"}, }) if err != nil { @@ -202,7 +202,7 @@ func TestMemoryLeakBusinessScenarios(t *testing.T) { t.Fatalf("Failed to get assistant: %s", err.Error()) } - if agent.Script == nil { + if agent.HookScript == nil { t.Fatalf("Assistant has no script") } @@ -221,7 +221,7 @@ func TestMemoryLeakBusinessScenarios(t *testing.T) { // Warm up for i := 0; i < 10; i++ { ctx := newMemTestContext("warmup", "tests.create") - _, _, _ = agent.Script.Create(ctx, []context.Message{ + _, _, _ = agent.HookScript.Create(ctx, []context.Message{ {Role: "user", Content: "return_full"}, }) ctx.Release() @@ -240,7 +240,7 @@ func TestMemoryLeakBusinessScenarios(t *testing.T) { iterations := 200 for i := 0; i < iterations; i++ { ctx := newMemTestContext("mem-test-business", "tests.create") - _, _, err := agent.Script.Create(ctx, []context.Message{ + _, _, err := agent.HookScript.Create(ctx, []context.Message{ {Role: "user", Content: scenario.content}, }) if err != nil { @@ -291,14 +291,14 @@ func TestMemoryLeakConcurrent(t *testing.T) { t.Fatalf("Failed to get assistant: %s", err.Error()) } - if agent.Script == nil { + if agent.HookScript == nil { t.Fatalf("Assistant has no script") } // Warm up for i := 0; i < 20; i++ { ctx := newMemTestContext("warmup", "tests.create") - _, _, _ = agent.Script.Create(ctx, []context.Message{ + _, _, _ = agent.HookScript.Create(ctx, []context.Message{ {Role: "user", Content: "Hello"}, }) ctx.Release() @@ -321,7 +321,7 @@ func TestMemoryLeakConcurrent(t *testing.T) { defer func() { done <- true }() for i := 0; i < iterPerGoroutine; i++ { ctx := newMemTestContext("mem-test-concurrent", "tests.create") - _, _, err := agent.Script.Create(ctx, []context.Message{ + _, _, err := agent.HookScript.Create(ctx, []context.Message{ {Role: "user", Content: "Hello"}, }) if err != nil { @@ -376,14 +376,14 @@ func TestMemoryLeakNestedCalls(t *testing.T) { t.Fatalf("Failed to get assistant: %s", err.Error()) } - if agent.Script == nil { + if agent.HookScript == nil { t.Fatalf("Assistant has no script") } // Warm up for i := 0; i < 10; i++ { ctx := newMemTestContext("warmup", "tests.create") - _, _, _ = agent.Script.Create(ctx, []context.Message{ + _, _, _ = agent.HookScript.Create(ctx, []context.Message{ {Role: "user", Content: "nested_script_call"}, }) ctx.Release() @@ -400,7 +400,7 @@ func TestMemoryLeakNestedCalls(t *testing.T) { iterations := 200 for i := 0; i < iterations; i++ { ctx := newMemTestContext("mem-test-nested", "tests.create") - _, _, err := agent.Script.Create(ctx, []context.Message{ + _, _, err := agent.HookScript.Create(ctx, []context.Message{ {Role: "user", Content: "deep_nested_call"}, }) if err != nil { @@ -452,14 +452,14 @@ func TestMemoryLeakNestedConcurrent(t *testing.T) { t.Fatalf("Failed to get assistant: %s", err.Error()) } - if agent.Script == nil { + if agent.HookScript == nil { t.Fatalf("Assistant has no script") } // Warm up for i := 0; i < 20; i++ { ctx := newMemTestContext("warmup", "tests.create") - _, _, _ = agent.Script.Create(ctx, []context.Message{ + _, _, _ = agent.HookScript.Create(ctx, []context.Message{ {Role: "user", Content: "nested_script_call"}, }) ctx.Release() @@ -482,7 +482,7 @@ func TestMemoryLeakNestedConcurrent(t *testing.T) { defer func() { done <- true }() for i := 0; i < iterPerGoroutine; i++ { ctx := newMemTestContext("mem-test-nested-concurrent", "tests.create") - _, _, err := agent.Script.Create(ctx, []context.Message{ + _, _, err := agent.HookScript.Create(ctx, []context.Message{ {Role: "user", Content: "deep_nested_call"}, }) if err != nil { @@ -538,7 +538,7 @@ func TestIsolateDisposal(t *testing.T) { t.Fatalf("Failed to get assistant: %s", err.Error()) } - if agent.Script == nil { + if agent.HookScript == nil { t.Fatalf("Assistant has no script") } @@ -549,7 +549,7 @@ func TestIsolateDisposal(t *testing.T) { iterations := 100 for i := 0; i < iterations; i++ { ctx := newMemTestContext("disposal-test", "tests.create") - _, _, err := agent.Script.Create(ctx, []context.Message{ + _, _, err := agent.HookScript.Create(ctx, []context.Message{ {Role: "user", Content: "Hello"}, }) if err != nil { diff --git a/agent/assistant/hook/create_nested_test.go b/agent/assistant/hook/create_nested_test.go index c12cc9e1..eae506f8 100644 --- a/agent/assistant/hook/create_nested_test.go +++ b/agent/assistant/hook/create_nested_test.go @@ -20,7 +20,7 @@ func TestNestedScriptCall(t *testing.T) { t.Fatalf("Failed to get assistant: %s", err.Error()) } - if agent.Script == nil { + if agent.HookScript == nil { t.Fatalf("Assistant has no script") } @@ -29,7 +29,7 @@ func TestNestedScriptCall(t *testing.T) { // Call with deep_nested_call scenario // This will: hook -> scripts.tests.create.NestedCall -> GetRoles -> model - res, _, err := agent.Script.Create(ctx, []context.Message{ + res, _, err := agent.HookScript.Create(ctx, []context.Message{ {Role: "user", Content: "deep_nested_call"}, }) @@ -64,7 +64,7 @@ func TestNestedScriptCallConcurrent(t *testing.T) { t.Fatalf("Failed to get assistant: %s", err.Error()) } - if agent.Script == nil { + if agent.HookScript == nil { t.Fatalf("Assistant has no script") } @@ -86,7 +86,7 @@ func TestNestedScriptCallConcurrent(t *testing.T) { for j := 0; j < iterations; j++ { ctx := newTestContext("test-concurrent", "tests.create") - _, _, err := agent.Script.Create(ctx, []context.Message{ + _, _, err := agent.HookScript.Create(ctx, []context.Message{ {Role: "user", Content: "deep_nested_call"}, }) diff --git a/agent/assistant/hook/create_test.go b/agent/assistant/hook/create_test.go index 7e5bad60..d9b2c4bd 100644 --- a/agent/assistant/hook/create_test.go +++ b/agent/assistant/hook/create_test.go @@ -64,7 +64,7 @@ func TestCreate(t *testing.T) { t.Fatalf("Failed to get the tests.create assistant: %s", err.Error()) } - if agent.Script == nil { + if agent.HookScript == nil { t.Fatalf("The tests.create assistant has no script") } @@ -73,7 +73,7 @@ func TestCreate(t *testing.T) { // Test scenario 1: Return null (should get nil response) t.Run("ReturnNull", func(t *testing.T) { - res, _, err := agent.Script.Create(ctx, []context.Message{{Role: "user", Content: "return_null"}}) + res, _, err := agent.HookScript.Create(ctx, []context.Message{{Role: "user", Content: "return_null"}}) if err != nil { t.Fatalf("Failed to create with null return: %s", err.Error()) } @@ -84,7 +84,7 @@ func TestCreate(t *testing.T) { // Test scenario 2: Return undefined (should get nil response) t.Run("ReturnUndefined", func(t *testing.T) { - res, _, err := agent.Script.Create(ctx, []context.Message{{Role: "user", Content: "return_undefined"}}) + res, _, err := agent.HookScript.Create(ctx, []context.Message{{Role: "user", Content: "return_undefined"}}) if err != nil { t.Fatalf("Failed to create with undefined return: %s", err.Error()) } @@ -95,7 +95,7 @@ func TestCreate(t *testing.T) { // Test scenario 3: Return empty object (should get empty HookCreateResponse) t.Run("ReturnEmpty", func(t *testing.T) { - res, _, err := agent.Script.Create(ctx, []context.Message{{Role: "user", Content: "return_empty"}}) + res, _, err := agent.HookScript.Create(ctx, []context.Message{{Role: "user", Content: "return_empty"}}) if err != nil { t.Fatalf("Failed to create with empty return: %s", err.Error()) } @@ -109,7 +109,7 @@ func TestCreate(t *testing.T) { // Test scenario 4: Return full response with all fields t.Run("ReturnFull", func(t *testing.T) { - res, _, err := agent.Script.Create(ctx, []context.Message{{Role: "user", Content: "return_full"}}) + res, _, err := agent.HookScript.Create(ctx, []context.Message{{Role: "user", Content: "return_full"}}) if err != nil { t.Fatalf("Failed to create with full return: %s", err.Error()) } @@ -165,7 +165,7 @@ func TestCreate(t *testing.T) { // Test scenario 5: Return partial response t.Run("ReturnPartial", func(t *testing.T) { - res, _, err := agent.Script.Create(ctx, []context.Message{{Role: "user", Content: "return_partial"}}) + res, _, err := agent.HookScript.Create(ctx, []context.Message{{Role: "user", Content: "return_partial"}}) if err != nil { t.Fatalf("Failed to create with partial return: %s", err.Error()) } @@ -196,7 +196,7 @@ func TestCreate(t *testing.T) { // Test scenario 6: Process call - calls models.__yao.role.Get and adds to messages t.Run("ReturnProcess", func(t *testing.T) { - res, _, err := agent.Script.Create(ctx, []context.Message{{Role: "user", Content: "return_process"}}) + res, _, err := agent.HookScript.Create(ctx, []context.Message{{Role: "user", Content: "return_process"}}) if err != nil { t.Fatalf("Failed to create with process return: %s", err.Error()) } @@ -224,7 +224,7 @@ func TestCreate(t *testing.T) { // Test scenario 7: Default response t.Run("ReturnDefault", func(t *testing.T) { testContent := "Hello, how are you?" - res, _, err := agent.Script.Create(ctx, []context.Message{{Role: "user", Content: testContent}}) + res, _, err := agent.HookScript.Create(ctx, []context.Message{{Role: "user", Content: testContent}}) if err != nil { t.Fatalf("Failed to create with default return: %s", err.Error()) } @@ -251,7 +251,7 @@ func TestCreate(t *testing.T) { // Test scenario 8: Verify context fields - validates all context fields in JavaScript t.Run("VerifyContext", func(t *testing.T) { - res, _, err := agent.Script.Create(ctx, []context.Message{{Role: "user", Content: "verify_context"}}) + res, _, err := agent.HookScript.Create(ctx, []context.Message{{Role: "user", Content: "verify_context"}}) if err != nil { t.Fatalf("Failed to create with verify_context: %s", err.Error()) } @@ -303,7 +303,7 @@ func TestCreate(t *testing.T) { adjustCtx := newTestContext("chat-test-adjust", "tests.create") // Call the hook which should adjust context fields - res, _, err := agent.Script.Create(adjustCtx, []context.Message{{Role: "user", Content: "adjust_context"}}) + res, _, err := agent.HookScript.Create(adjustCtx, []context.Message{{Role: "user", Content: "adjust_context"}}) if err != nil { t.Fatalf("Failed to create with adjust_context: %s", err.Error()) } diff --git a/agent/assistant/hook/goroutine_leak_test.go b/agent/assistant/hook/goroutine_leak_test.go index 2745e709..9673001e 100644 --- a/agent/assistant/hook/goroutine_leak_test.go +++ b/agent/assistant/hook/goroutine_leak_test.go @@ -27,7 +27,7 @@ func TestGoroutineLeakDetailed(t *testing.T) { t.Fatalf("Failed to get assistant: %s", err.Error()) } - if agent.Script == nil { + if agent.HookScript == nil { t.Fatalf("Assistant has no script") } @@ -48,7 +48,7 @@ func TestGoroutineLeakDetailed(t *testing.T) { for i := 0; i < iterations; i++ { ctx := newLeakTestContext(fmt.Sprintf("leak-test-%d", i), "tests.create") - _, _, err := agent.Script.Create(ctx, []context.Message{ + _, _, err := agent.HookScript.Create(ctx, []context.Message{ {Role: "user", Content: "Hello"}, }) if err != nil { @@ -126,7 +126,7 @@ func TestGoroutineLeakByComponent(t *testing.T) { for i := 0; i < 10; i++ { ctx := newLeakTestContext(fmt.Sprintf("test-%d", i), "tests.create") - _, _, _ = agent.Script.Create(ctx, []context.Message{ + _, _, _ = agent.HookScript.Create(ctx, []context.Message{ {Role: "user", Content: "Hello"}, }) ctx.Release() @@ -184,7 +184,7 @@ func TestGoroutineLeakWithoutRelease(t *testing.T) { for i := 0; i < 10; i++ { ctx := newLeakTestContext(fmt.Sprintf("no-release-%d", i), "tests.create") - _, _, _ = agent.Script.Create(ctx, []context.Message{ + _, _, _ = agent.HookScript.Create(ctx, []context.Message{ {Role: "user", Content: "Hello"}, }) // Intentionally NOT calling ctx.Release() @@ -205,7 +205,7 @@ func TestGoroutineLeakWithoutRelease(t *testing.T) { for i := 0; i < 10; i++ { ctx := newLeakTestContext(fmt.Sprintf("with-release-%d", i), "tests.create") - _, _, _ = agent.Script.Create(ctx, []context.Message{ + _, _, _ = agent.HookScript.Create(ctx, []context.Message{ {Role: "user", Content: "Hello"}, }) ctx.Release() // WITH Release diff --git a/agent/assistant/hook/next_test.go b/agent/assistant/hook/next_test.go index e3f69ed3..becaedb2 100644 --- a/agent/assistant/hook/next_test.go +++ b/agent/assistant/hook/next_test.go @@ -65,7 +65,7 @@ func TestNext(t *testing.T) { t.Fatalf("Failed to get the tests.next assistant: %s", err.Error()) } - if agent.Script == nil { + if agent.HookScript == nil { t.Fatalf("The tests.next assistant has no script") } @@ -85,7 +85,7 @@ func TestNext(t *testing.T) { Error: "", } - res, _, err := agent.Script.Next(ctx, payload) + res, _, err := agent.HookScript.Next(ctx, payload) if err != nil { t.Fatalf("Failed to execute Next hook with null return: %s", err.Error()) } @@ -105,7 +105,7 @@ func TestNext(t *testing.T) { }, } - res, _, err := agent.Script.Next(ctx, payload) + res, _, err := agent.HookScript.Next(ctx, payload) if err != nil { t.Fatalf("Failed to execute Next hook with undefined return: %s", err.Error()) } @@ -125,7 +125,7 @@ func TestNext(t *testing.T) { }, } - res, _, err := agent.Script.Next(ctx, payload) + res, _, err := agent.HookScript.Next(ctx, payload) if err != nil { t.Fatalf("Failed to execute Next hook with empty return: %s", err.Error()) } @@ -151,7 +151,7 @@ func TestNext(t *testing.T) { }, } - res, _, err := agent.Script.Next(ctx, payload) + res, _, err := agent.HookScript.Next(ctx, payload) if err != nil { t.Fatalf("Failed to execute Next hook with custom data: %s", err.Error()) } @@ -198,7 +198,7 @@ func TestNext(t *testing.T) { }, } - res, _, err := agent.Script.Next(ctx, payload) + res, _, err := agent.HookScript.Next(ctx, payload) if err != nil { t.Fatalf("Failed to execute Next hook: %s", err.Error()) } @@ -244,7 +244,7 @@ func TestNext(t *testing.T) { }, } - res, _, err := agent.Script.Next(ctx, payload) + res, _, err := agent.HookScript.Next(ctx, payload) if err != nil { t.Fatalf("Failed to execute Next hook with delegate: %s", err.Error()) } @@ -306,7 +306,7 @@ func TestNext(t *testing.T) { Error: "", } - res, _, err := agent.Script.Next(ctx, payload) + res, _, err := agent.HookScript.Next(ctx, payload) if err != nil { t.Fatalf("Failed to execute Next hook: %s", err.Error()) } @@ -365,7 +365,7 @@ func TestNext(t *testing.T) { }, } - res, _, err := agent.Script.Next(ctx, payload) + res, _, err := agent.HookScript.Next(ctx, payload) if err != nil { t.Fatalf("Failed to execute Next hook: %s", err.Error()) } @@ -409,7 +409,7 @@ func TestNext(t *testing.T) { Error: "Tool execution failed: timeout", } - res, _, err := agent.Script.Next(ctx, payload) + res, _, err := agent.HookScript.Next(ctx, payload) if err != nil { t.Fatalf("Failed to execute Next hook: %s", err.Error()) } diff --git a/agent/assistant/hook/realworld_next_test.go b/agent/assistant/hook/realworld_next_test.go index 85a9ab84..1142b749 100644 --- a/agent/assistant/hook/realworld_next_test.go +++ b/agent/assistant/hook/realworld_next_test.go @@ -75,7 +75,7 @@ func TestRealWorldNextStandard(t *testing.T) { Error: "", } - response, _, err := agent.Script.Next(ctx, payload) + response, _, err := agent.HookScript.Next(ctx, payload) if err != nil { t.Fatalf("Next hook failed: %v", err) } @@ -117,7 +117,7 @@ func TestRealWorldNextCustomData(t *testing.T) { Error: "", } - response, _, err := agent.Script.Next(ctx, payload) + response, _, err := agent.HookScript.Next(ctx, payload) if err != nil { t.Fatalf("Next hook failed: %v", err) } @@ -164,7 +164,7 @@ func TestRealWorldNextDelegate(t *testing.T) { Error: "", } - response, _, err := agent.Script.Next(ctx, payload) + response, _, err := agent.HookScript.Next(ctx, payload) if err != nil { t.Fatalf("Next hook failed: %v", err) } @@ -226,7 +226,7 @@ func TestRealWorldNextProcessTools(t *testing.T) { Error: "", } - response, _, err := agent.Script.Next(ctx, payload) + response, _, err := agent.HookScript.Next(ctx, payload) if err != nil { t.Fatalf("Next hook failed: %v", err) } @@ -279,7 +279,7 @@ func TestRealWorldNextErrorRecovery(t *testing.T) { Error: "System error: Database connection timeout", } - response, _, err := agent.Script.Next(ctx, payload) + response, _, err := agent.HookScript.Next(ctx, payload) if err != nil { t.Fatalf("Next hook failed: %v", err) } @@ -328,7 +328,7 @@ func TestRealWorldNextConditional(t *testing.T) { Error: "", } - response, _, err := agent.Script.Next(ctx, payload) + response, _, err := agent.HookScript.Next(ctx, payload) if err != nil { t.Fatalf("Next hook failed: %v", err) } @@ -361,7 +361,7 @@ func TestRealWorldNextConditional(t *testing.T) { Error: "", } - response, _, err := agent.Script.Next(ctx, payload) + response, _, err := agent.HookScript.Next(ctx, payload) if err != nil { t.Fatalf("Next hook failed: %v", err) } @@ -405,7 +405,7 @@ func TestRealWorldNextDefault(t *testing.T) { Error: "", } - response, _, err := agent.Script.Next(ctx, payload) + response, _, err := agent.HookScript.Next(ctx, payload) if err != nil { t.Fatalf("Next hook failed: %v", err) } diff --git a/agent/assistant/hook/realworld_stress_test.go b/agent/assistant/hook/realworld_stress_test.go index 2690ed8a..fa170a2a 100644 --- a/agent/assistant/hook/realworld_stress_test.go +++ b/agent/assistant/hook/realworld_stress_test.go @@ -42,7 +42,7 @@ func TestRealWorldSimpleScenario(t *testing.T) { {Role: "user", Content: "simple"}, } - response, _, err := agent.Script.Create(ctx, messages) + response, _, err := agent.HookScript.Create(ctx, messages) if err != nil { t.Fatalf("Create failed: %v", err) } @@ -73,7 +73,7 @@ func TestRealWorldMCPScenarios(t *testing.T) { {Role: "user", Content: "mcp_health"}, } - response, _, err := agent.Script.Create(ctx, messages) + response, _, err := agent.HookScript.Create(ctx, messages) if err != nil { t.Fatalf("Create failed: %v", err) } @@ -117,7 +117,7 @@ func TestRealWorldMCPScenarios(t *testing.T) { {Role: "user", Content: "mcp_tools"}, } - response, _, err := agent.Script.Create(ctx, messages) + response, _, err := agent.HookScript.Create(ctx, messages) if err != nil { t.Fatalf("Create failed: %v", err) } @@ -170,7 +170,7 @@ func TestRealWorldMCPScenarios(t *testing.T) { {Role: "user", Content: "full_workflow"}, } - response, _, err := agent.Script.Create(ctx, messages) + response, _, err := agent.HookScript.Create(ctx, messages) if err != nil { t.Fatalf("Create failed: %v", err) } @@ -245,7 +245,7 @@ func TestRealWorldTraceIntensive(t *testing.T) { {Role: "user", Content: "trace_intensive"}, } - response, _, err := agent.Script.Create(ctx, messages) + response, _, err := agent.HookScript.Create(ctx, messages) if err != nil { t.Fatalf("Create failed: %v", err) } @@ -279,7 +279,7 @@ func TestRealWorldStressSimple(t *testing.T) { {Role: "user", Content: "simple"}, } - response, _, err := agent.Script.Create(ctx, messages) + response, _, err := agent.HookScript.Create(ctx, messages) if err != nil { t.Fatalf("Iteration %d failed: %v", i, err) } @@ -345,7 +345,7 @@ func TestRealWorldStressMCP(t *testing.T) { {Role: "user", Content: scenario}, } - response, _, err := agent.Script.Create(ctx, messages) + response, _, err := agent.HookScript.Create(ctx, messages) if err != nil { t.Fatalf("Iteration %d (%s) failed: %v", i, scenario, err) } @@ -434,7 +434,7 @@ func TestRealWorldStressFullWorkflow(t *testing.T) { {Role: "user", Content: "full_workflow"}, } - response, _, err := agent.Script.Create(ctx, messages) + response, _, err := agent.HookScript.Create(ctx, messages) if err != nil { t.Fatalf("Iteration %d failed: %v", i, err) } @@ -538,7 +538,7 @@ func TestRealWorldStressConcurrent(t *testing.T) { {Role: "user", Content: scenario}, } - response, _, err := agent.Script.Create(ctx, messages) + response, _, err := agent.HookScript.Create(ctx, messages) if err != nil { errors <- fmt.Errorf("goroutine %d iteration %d (%s): %v", goroutineID, i, scenario, err) done() @@ -665,7 +665,7 @@ func TestRealWorldStressResourceHeavy(t *testing.T) { {Role: "user", Content: "resource_heavy"}, } - response, _, err := agent.Script.Create(ctx, messages) + response, _, err := agent.HookScript.Create(ctx, messages) if err != nil { t.Fatalf("Iteration %d failed: %v", i, err) } diff --git a/agent/assistant/load.go b/agent/assistant/load.go index 9d087f04..b6c93cc0 100644 --- a/agent/assistant/load.go +++ b/agent/assistant/load.go @@ -225,7 +225,7 @@ func LoadStore(id string) (*Assistant, error) { if err != nil { return nil, err } - assistant.Script = script + assistant.HookScript = script } // Initialize the assistant @@ -696,11 +696,11 @@ func loadMap(data map[string]interface{}) (*Assistant, error) { if err != nil { return nil, err } - assistant.Script = &hook.Script{Script: script} + assistant.HookScript = &hook.Script{Script: script} case *hook.Script: - assistant.Script = v + assistant.HookScript = v case *v8.Script: - assistant.Script = &hook.Script{Script: v} + assistant.HookScript = &hook.Script{Script: v} } } else if assistant.Source != "" { // Load from source field if script is not provided @@ -708,7 +708,7 @@ func loadMap(data map[string]interface{}) (*Assistant, error) { if err != nil { return nil, err } - assistant.Script = script + assistant.HookScript = script } // created_at @@ -783,8 +783,8 @@ func (ast *Assistant) initialize() error { ast.openai = api // Check if the assistant has an init hook - if ast.Script != nil { - scriptCtx, err := ast.Script.NewContext("", nil) + if ast.HookScript != nil { + scriptCtx, err := ast.HookScript.NewContext("", nil) if err != nil { return err } diff --git a/agent/assistant/load_store_test.go b/agent/assistant/load_store_test.go index 291d75e2..e8dfb173 100644 --- a/agent/assistant/load_store_test.go +++ b/agent/assistant/load_store_test.go @@ -92,7 +92,7 @@ function Create(ctx, messages) { assert.Contains(t, loaded.Tags, "Source") // Verify script was compiled from source - assert.NotNil(t, loaded.Script, "Script should be compiled from Source field") + assert.NotNil(t, loaded.HookScript, "HookScript should be compiled from Source field") // Verify source is stored assert.NotEmpty(t, loaded.Source) @@ -170,7 +170,7 @@ func TestLoadStoreWithoutSource(t *testing.T) { assert.Contains(t, loaded.Tags, "NoSource") // Verify script is nil (no source) - assert.Nil(t, loaded.Script, "Script should be nil when no Source field") + assert.Nil(t, loaded.HookScript, "HookScript should be nil when no Source field") assert.Empty(t, loaded.Source) } @@ -259,16 +259,16 @@ function Create(ctx: any, messages: any[]): any { loaded, err := assistant.Get(assistantID) require.NoError(t, err) require.NotNil(t, loaded) - require.NotNil(t, loaded.Script, "Script should be compiled from Source") + require.NotNil(t, loaded.HookScript, "HookScript should be compiled from Source") // Verify the script object exists and is usable - assert.NotNil(t, loaded.Script.Script) + assert.NotNil(t, loaded.HookScript.Script) // Execute the Create hook ctx := newStoreTestContext("test-chat-id", assistantID) messages := []context.Message{{Role: "user", Content: "Hello"}} - res, _, err := loaded.Script.Create(ctx, messages, &context.Options{}) + res, _, err := loaded.HookScript.Create(ctx, messages, &context.Options{}) require.NoError(t, err, "Create hook should execute without error") require.NotNil(t, res, "Create hook should return a response") @@ -568,14 +568,14 @@ function Create(ctx: any, messages: any[]): any { assert.Len(t, loaded.Placeholder.Prompts, 2) // Script from source - assert.NotNil(t, loaded.Script) + assert.NotNil(t, loaded.HookScript) assert.NotEmpty(t, loaded.Source) // Execute the Create hook to verify it works ctx := newStoreTestContext("test-chat-all-fields", assistantID) messages := []context.Message{{Role: "user", Content: "Test message"}} - res, _, err := loaded.Script.Create(ctx, messages, &context.Options{}) + res, _, err := loaded.HookScript.Create(ctx, messages, &context.Options{}) require.NoError(t, err, "Create hook should execute without error") require.NotNil(t, res, "Create hook should return a response") @@ -680,7 +680,7 @@ function Create(ctx: CreateContext, messages: Message[]): CreateResponse | null loaded, err := assistant.Get(assistantID) require.NoError(t, err) require.NotNil(t, loaded) - require.NotNil(t, loaded.Script, "Script should be compiled from TypeScript Source") + require.NotNil(t, loaded.HookScript, "HookScript should be compiled from TypeScript Source") // Execute the Create hook ctx := newStoreTestContext("ts-test-chat", assistantID) @@ -690,7 +690,7 @@ function Create(ctx: CreateContext, messages: Message[]): CreateResponse | null {Role: "user", Content: "How are you?"}, } - res, _, err := loaded.Script.Create(ctx, messages, &context.Options{}) + res, _, err := loaded.HookScript.Create(ctx, messages, &context.Options{}) require.NoError(t, err, "TypeScript Create hook should execute without error") require.NotNil(t, res, "Create hook should return a response") @@ -753,12 +753,12 @@ function Create(ctx: any, messages: any[]): any { loaded, err := assistant.Get(assistantID) require.NoError(t, err) require.NotNil(t, loaded) - require.NotNil(t, loaded.Script) + require.NotNil(t, loaded.HookScript) ctx := newStoreTestContext("null-test-chat", assistantID) messages := []context.Message{{Role: "user", Content: "Hello"}} - res, _, err := loaded.Script.Create(ctx, messages, &context.Options{}) + res, _, err := loaded.HookScript.Create(ctx, messages, &context.Options{}) require.NoError(t, err, "Hook returning null should not error") assert.Nil(t, res, "Hook returning null should return nil response") } @@ -824,14 +824,14 @@ function Create(ctx: any, messages: any[]): any { loaded, err := assistant.Get(assistantID) require.NoError(t, err) require.NotNil(t, loaded) - require.NotNil(t, loaded.Script) + require.NotNil(t, loaded.HookScript) // Test friendly preset selection t.Run("SelectFriendlyPreset", func(t *testing.T) { ctx := newStoreTestContext("preset-test-1", assistantID) messages := []context.Message{{Role: "user", Content: "Be friendly please"}} - res, _, err := loaded.Script.Create(ctx, messages, &context.Options{}) + res, _, err := loaded.HookScript.Create(ctx, messages, &context.Options{}) require.NoError(t, err) require.NotNil(t, res) assert.Equal(t, "friendly", res.PromptPreset) @@ -842,7 +842,7 @@ function Create(ctx: any, messages: any[]): any { ctx := newStoreTestContext("preset-test-2", assistantID) messages := []context.Message{{Role: "user", Content: "Be professional"}} - res, _, err := loaded.Script.Create(ctx, messages, &context.Options{}) + res, _, err := loaded.HookScript.Create(ctx, messages, &context.Options{}) require.NoError(t, err) require.NotNil(t, res) assert.Equal(t, "professional", res.PromptPreset) @@ -853,7 +853,7 @@ function Create(ctx: any, messages: any[]): any { ctx := newStoreTestContext("preset-test-3", assistantID) messages := []context.Message{{Role: "user", Content: "Hello"}} - res, _, err := loaded.Script.Create(ctx, messages, &context.Options{}) + res, _, err := loaded.HookScript.Create(ctx, messages, &context.Options{}) require.NoError(t, err) assert.Nil(t, res) }) @@ -908,14 +908,14 @@ function Create(ctx: any, messages: any[]): any { loaded, err := assistant.Get(assistantID) require.NoError(t, err) require.NotNil(t, loaded) - require.NotNil(t, loaded.Script) + require.NotNil(t, loaded.HookScript) // Test disable global prompts t.Run("DisableGlobalPrompts", func(t *testing.T) { ctx := newStoreTestContext("disable-test-1", assistantID) messages := []context.Message{{Role: "user", Content: "disable_global prompts"}} - res, _, err := loaded.Script.Create(ctx, messages, &context.Options{}) + res, _, err := loaded.HookScript.Create(ctx, messages, &context.Options{}) require.NoError(t, err) require.NotNil(t, res) require.NotNil(t, res.DisableGlobalPrompts) @@ -927,7 +927,7 @@ function Create(ctx: any, messages: any[]): any { ctx := newStoreTestContext("disable-test-2", assistantID) messages := []context.Message{{Role: "user", Content: "enable_global prompts"}} - res, _, err := loaded.Script.Create(ctx, messages, &context.Options{}) + res, _, err := loaded.HookScript.Create(ctx, messages, &context.Options{}) require.NoError(t, err) require.NotNil(t, res) require.NotNil(t, res.DisableGlobalPrompts) diff --git a/agent/assistant/load_test.go b/agent/assistant/load_test.go index 08ec735e..49333815 100644 --- a/agent/assistant/load_test.go +++ b/agent/assistant/load_test.go @@ -63,7 +63,7 @@ func TestLoadPath(t *testing.T) { assert.Equal(t, "system", assistant.Prompts[0].Role) // Script (from src/index.ts) - assert.NotNil(t, assistant.Script) + assert.NotNil(t, assistant.HookScript) }) t.Run("LoadConnectorOptions", func(t *testing.T) { @@ -227,8 +227,8 @@ func TestLoadPathBuildRequest(t *testing.T) { assert.Equal(t, "tests.buildrequest", assistant.ID) assert.Equal(t, "Build Request Test", assistant.Name) - // Script should be loaded - assert.NotNil(t, assistant.Script) + // HookScript should be loaded + assert.NotNil(t, assistant.HookScript) // Options assert.NotNil(t, assistant.Options) diff --git a/agent/assistant/types.go b/agent/assistant/types.go index 66b079b6..2df2e994 100644 --- a/agent/assistant/types.go +++ b/agent/assistant/types.go @@ -29,8 +29,8 @@ type SearchOption struct { // Assistant the assistant type Assistant struct { store.AssistantModel - Search *SearchOption `json:"search,omitempty" yaml:"search,omitempty"` // Whether this assistant supports search - Script *hook.Script `json:"-" yaml:"-"` // Assistant Script + Search *SearchOption `json:"search,omitempty" yaml:"search,omitempty"` // Whether this assistant supports search + HookScript *hook.Script `json:"-" yaml:"-"` // Hook Script // Internal // ===============================