1package tools
2
3import (
4 "context"
5 "strings"
6)
7
8type (
9 sessionIDContextKey string
10 messageIDContextKey string
11 supportsImagesKey string
12 modelNameKey string
13)
14
15const (
16 // SessionIDContextKey is the key for the session ID in the context.
17 SessionIDContextKey sessionIDContextKey = "session_id"
18 // MessageIDContextKey is the key for the message ID in the context.
19 MessageIDContextKey messageIDContextKey = "message_id"
20 // SupportsImagesContextKey is the key for the model's image support capability.
21 SupportsImagesContextKey supportsImagesKey = "supports_images"
22 // ModelNameContextKey is the key for the model name in the context.
23 ModelNameContextKey modelNameKey = "model_name"
24)
25
26// getContextValue is a generic helper that retrieves a typed value from context.
27// If the value is not found or has the wrong type, it returns the default value.
28func getContextValue[T any](ctx context.Context, key any, defaultValue T) T {
29 value := ctx.Value(key)
30 if value == nil {
31 return defaultValue
32 }
33 if typedValue, ok := value.(T); ok {
34 return typedValue
35 }
36 return defaultValue
37}
38
39// GetSessionFromContext retrieves the session ID from the context.
40func GetSessionFromContext(ctx context.Context) string {
41 return getContextValue(ctx, SessionIDContextKey, "")
42}
43
44// GetMessageFromContext retrieves the message ID from the context.
45func GetMessageFromContext(ctx context.Context) string {
46 return getContextValue(ctx, MessageIDContextKey, "")
47}
48
49// GetSupportsImagesFromContext retrieves whether the model supports images from the context.
50func GetSupportsImagesFromContext(ctx context.Context) bool {
51 return getContextValue(ctx, SupportsImagesContextKey, false)
52}
53
54// GetModelNameFromContext retrieves the model name from the context.
55func GetModelNameFromContext(ctx context.Context) string {
56 return getContextValue(ctx, ModelNameContextKey, "")
57}
58
59// FirstLineDescription returns the first non-empty line from the embedded
60// markdown description. This extracts just the concise summary line,
61// significantly reducing token usage while preserving essential tool context.
62func FirstLineDescription(content []byte) string {
63 lines := strings.SplitSeq(string(content), "\n")
64 for line := range lines {
65 line = strings.TrimSpace(line)
66 if line != "" {
67 return line
68 }
69 }
70 return ""
71}