diff --git a/internal/server/ai.go b/internal/server/ai.go index bc28745..ce5d31b 100644 --- a/internal/server/ai.go +++ b/internal/server/ai.go @@ -363,6 +363,10 @@ const aiAgent = ` Режим агента: ты сам ведёшь задачу шаг за шагом. В каждом ответе - коротко, что делаешь, и ровно одно действие. Получив результат, решай следующий шаг. Когда задача решена (или решить нельзя) - ответь итогом без блоков действий.` +const aiSearch = ` +Поиск в интернете включён: блок ` + "```search\nзапрос\n```" + ` - панель найдёт его в DuckDuckGo и пришлёт результаты (заголовок, адрес, отрывок) следующим сообщением. +Ищи активно: свежие данные, версии, документация, тексты ошибок, всё, в чём не уверен, - сначала поиск, потом ответ со ссылками на найденное. Одно действие за ответ. Ссылки не выдумывай.` + const aiThink = ` Режим размышления: начни ответ строкой ::думаю:: и подумай - разбери задачу, варианты и риски; затем строка ::ответ:: и сам ответ.` @@ -524,6 +528,7 @@ func (s *Server) handleAIChat(w http.ResponseWriter, r *http.Request) { Context string `json:"context"` Agent bool `json:"agent"` Think bool `json:"think"` + Search bool `json:"search"` Chat int64 `json:"chat"` } if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, 1<<20)).Decode(&in); err != nil || (in.Message == "" && in.Result == "") { @@ -546,6 +551,9 @@ func (s *Server) handleAIChat(w http.ResponseWriter, r *http.Request) { if in.Agent { system += aiAgent } + if in.Search { + system += aiSearch + } if in.Think { system += aiThink // Models heed the last words most: the reminder goes there too (not diff --git a/internal/server/search.go b/internal/server/search.go new file mode 100644 index 0000000..c661e15 --- /dev/null +++ b/internal/server/search.go @@ -0,0 +1,128 @@ +package server + +import ( + "context" + "encoding/json" + "errors" + "html" + "io" + "net/http" + "net/url" + "regexp" + "strings" + "time" +) + +// Web search for the assistant, when the user turned «Поиск» on: DuckDuckGo's +// plain pages, no key and nothing to install. The light page first, the +// HTML one if that fails; DuckDuckGo turns away too many requests from one +// address, and then the assistant is told so. + +type searchResult struct { + Title string `json:"title"` + URL string `json:"url"` + Snippet string `json:"snippet"` +} + +var ( + reLiteLink = regexp.MustCompile(`(?s)]+href="([^"]+)"[^>]*class='result-link'[^>]*>(.*?)`) + reLiteSnippet = regexp.MustCompile(`(?s)
+
+ + + + +
+
| + + | ++ + + + + | +
+ +
| + + 1. + + | ++ Canceling in-progress operations - The Go Programming Language + | + +
| + | + Learn how to use context.Context to manage and cancel database operations in Go. See examples of how to set a timeout, deadline, and propagate cancellation requests with context. + | +
| + | + go.dev/doc/database/cancel-operations + + | +
| + | + |
| + + 2. + + | ++ Go Context Tutorial: Cancellation and Deadlines + | + +
| + | + Learn Go context with practical cancellation, timeout, HTTP request, and goroutine examples. + | +
| + | + golangtutorial.com/golang-context/ + + + + + | +
| + | + |
| + + 3. + + | ++ go - Context confusion regarding cancellation - Stack Overflow + | + +
| + | + Additional info #1: calling cancel () right after context initialized As per context documentation regarding the cancel () function: Canceling this context releases resources associated with it, so code should call cancel as soon as the operations running in this Context complete. It's good to always call cancel () function right after the context declaration. doesn't matter whether it's also ... + | +
| + | + stackoverflow.com/questions/52799280/context-confusion-regarding-cancellation + + | +
| + | + |
| + + 4. + + | ++ context package - context - Go Packages + | + +
| + | + Package context defines the Context type, which carries deadlines, cancellation signals, and other request-scoped values across API boundaries and between processes. + | +
| + | + pkg.go.dev/context + + + + + | +
| + | + |
| + + 5. + + | ++ Best practices on go context cancelation functions + | + +
| + | + So, is passing this cancellation function actually an issue? Are there any best practices related to the use of the context package and their cancel function? + | +
| + | + stackoverflow.com/questions/70042646/best-practices-on-go-context-cancelation-functions + + | +
| + | + |
| + + 6. + + | ++ Golang Context - Cancellation, Timeout and Propagation | golangbot.com + | + +
| + | + Learn how to cancel long running tasks in Go using context cancellation and timeout. This tutorial also teaches how to terminate outstanding web requests which were cancelled by the client. + | +
| + | + golangbot.com/context-timeout-cancellation/ + + + + + | +
| + | + |
| + + 7. + + | ++ Go Concurrency Patterns: Context - The Go Programming Language + | + +
| + | + Learn how to use the context package to pass request-scoped values, cancellation signals, and deadlines across API boundaries in Go servers. See a complete example of an HTTP server that handles Google Web Search requests with context. + | +
| + | + go.dev/blog/context + + | +
| + | + |
| + + 8. + + | ++ How to Use Context in Golang (Deadlines, Cancellation, and Passing Values) + | + +
| + | + In this post, we'll learn about Go's context package, and more specifically, how we can use it to improve our applications performance. The main purpose of using Context is to manage long-running processes (like an HTTP request, a database call, or a network call) in a way that doesn't waste resources. If used correctly, the context package can help you: Cancel long running processes ... + | +
| + | + www.sohamkamani.com/golang/context/ + + | +
| + | + |
| + + 9. + + | ++ context - The Go Programming Language + | + +
| + | + The returned context's Done channel is closed when the deadline expires, when the returned cancel function is called, or when the parent context's Done channel is closed, whichever happens first. + | +
| + | + golangdoc.github.io/pkg/1.12/context/index.html + + | +
| + | + |
| + + 10. + + | ++ Golang context — WithCancel, WithTimeout, WithDeadline, Background + | + +
| + | + The golang context package ties cancellation, timeouts, and optional request values to a Context value you pass through your call stack. Use Background (or TODO temporarily) as the root, WithTimeout / WithDeadline for time bounds, WithCancel for explicit shutdown, and WithValue only for small, request-scoped metadata—always defer cancel ... + | +
| + | + www.golinuxcloud.com/golang-context/ + + | +
| + | + |
| + + + + + + + | +|
+ + + +
+ + +
**a**