tools.go

 1package tools
 2
 3import (
 4	"context"
 5)
 6
 7type (
 8	sessionIDContextKey string
 9	messageIDContextKey string
10	supportsImagesKey   string
11	modelNameKey        string
12)
13
14const (
15	// SessionIDContextKey is the key for the session ID in the context.
16	SessionIDContextKey sessionIDContextKey = "session_id"
17	// MessageIDContextKey is the key for the message ID in the context.
18	MessageIDContextKey messageIDContextKey = "message_id"
19	// SupportsImagesContextKey is the key for the model's image support capability.
20	SupportsImagesContextKey supportsImagesKey = "supports_images"
21	// ModelNameContextKey is the key for the model name in the context.
22	ModelNameContextKey modelNameKey = "model_name"
23)
24
25// getContextValue is a generic helper that retrieves a typed value from context.
26// If the value is not found or has the wrong type, it returns the default value.
27func getContextValue[T any](ctx context.Context, key any, defaultValue T) T {
28	value := ctx.Value(key)
29	if value == nil {
30		return defaultValue
31	}
32	if typedValue, ok := value.(T); ok {
33		return typedValue
34	}
35	return defaultValue
36}
37
38// GetSessionFromContext retrieves the session ID from the context.
39func GetSessionFromContext(ctx context.Context) string {
40	return getContextValue(ctx, SessionIDContextKey, "")
41}
42
43// GetMessageFromContext retrieves the message ID from the context.
44func GetMessageFromContext(ctx context.Context) string {
45	return getContextValue(ctx, MessageIDContextKey, "")
46}
47
48// GetSupportsImagesFromContext retrieves whether the model supports images from the context.
49func GetSupportsImagesFromContext(ctx context.Context) bool {
50	return getContextValue(ctx, SupportsImagesContextKey, false)
51}
52
53// GetModelNameFromContext retrieves the model name from the context.
54func GetModelNameFromContext(ctx context.Context) string {
55	return getContextValue(ctx, ModelNameContextKey, "")
56}