ExecuteWithStream mirrors Execute — this plugin produces no incremental output, so the stream callback is ignored.
(_ context.Context, args []string, _ func(string))
| 155 | // ExecuteWithStream mirrors Execute — this plugin produces no incremental |
| 156 | // output, so the stream callback is ignored. |
| 157 | func (p *BuiltinKnowledgePlugin) ExecuteWithStream(_ context.Context, args []string, _ func(string)) (string, error) { |
| 158 | adapter := currentKnowledgeAdapter() |
| 159 | if adapter == nil { |
| 160 | return "", errors.New("@knowledge: no knowledge adapter wired in this session") |
| 161 | } |
| 162 | if len(args) == 0 { |
| 163 | return "", errors.New(`@knowledge: empty args. Example: <tool_call name="@knowledge" args='{"cmd":"search","args":{"query":"..."}}' />`) |
| 164 | } |
| 165 | |
| 166 | cmd, inner, err := parseKnowledgeInvocation(args) |
| 167 | if err != nil { |
| 168 | return "", fmt.Errorf("@knowledge: %w", err) |
| 169 | } |
| 170 | |
| 171 | switch cmd { |
| 172 | case "search": |
| 173 | var in struct { |
| 174 | Query string `json:"query"` |
| 175 | TopK int `json:"top_k"` |
| 176 | KB string `json:"kb"` |
| 177 | } |
| 178 | _ = json.Unmarshal([]byte(inner), &in) |
| 179 | if strings.TrimSpace(in.Query) == "" { |
| 180 | return "", errors.New(`@knowledge search: "query" is required`) |
| 181 | } |
| 182 | return adapter.Search(in.Query, in.KB, in.TopK) |
| 183 | case "get": |
| 184 | var in struct { |
| 185 | Source string `json:"source"` |
| 186 | Offset int `json:"offset"` |
| 187 | KB string `json:"kb"` |
| 188 | } |
| 189 | _ = json.Unmarshal([]byte(inner), &in) |
| 190 | if strings.TrimSpace(in.Source) == "" { |
| 191 | return "", errors.New(`@knowledge get: "source" is required (a document path from search/toc)`) |
| 192 | } |
| 193 | return adapter.Get(in.Source, in.KB, in.Offset) |
| 194 | case "toc": |
| 195 | var in struct { |
| 196 | Prefix string `json:"prefix"` |
| 197 | KB string `json:"kb"` |
| 198 | } |
| 199 | _ = json.Unmarshal([]byte(inner), &in) |
| 200 | return adapter.TOC(in.KB, in.Prefix) |
| 201 | case "list": |
| 202 | return adapter.List() |
| 203 | default: |
| 204 | return "", fmt.Errorf("@knowledge: unknown cmd %q (valid: search|get|toc|list)", cmd) |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | // parseKnowledgeInvocation accepts the JSON envelope {"cmd":..,"args":{..}}, |
| 209 | // flat JSON, and the flattened argv form. Returns the canonical (cmd, innerJSON). |
no test coverage detected