MCPcopy Index your code
hub / github.com/coder/coder / updateAgentChatLastInjectedContextFromMessages

Function updateAgentChatLastInjectedContextFromMessages

coderd/workspaceagents.go:2914–2947  ·  view source on GitHub ↗

updateAgentChatLastInjectedContextFromMessages rebuilds the injected-context cache from all persisted context-file and skill parts.

(
	ctx context.Context,
	logger slog.Logger,
	db database.Store,
	chatID uuid.UUID,
)

Source from the content-addressed store, hash-verified

2912// updateAgentChatLastInjectedContextFromMessages rebuilds the
2913// injected-context cache from all persisted context-file and skill parts.
2914func updateAgentChatLastInjectedContextFromMessages(
2915 ctx context.Context,
2916 logger slog.Logger,
2917 db database.Store,
2918 chatID uuid.UUID,
2919) error {
2920 messages, err := db.GetChatMessagesByChatID(ctx, database.GetChatMessagesByChatIDParams{
2921 ChatID: chatID,
2922 AfterID: 0,
2923 })
2924 if err != nil {
2925 return xerrors.Errorf("load context messages for injected context: %w", err)
2926 }
2927
2928 sortChatMessagesByCreatedAtAndID(messages)
2929
2930 parts, err := chatd.CollectContextPartsFromMessages(ctx, logger, messages, true)
2931 if err != nil {
2932 return xerrors.Errorf("collect injected context parts: %w", err)
2933 }
2934 parts = chatd.FilterContextPartsToLatestAgent(parts)
2935
2936 param, err := chatd.BuildLastInjectedContext(parts)
2937 if err != nil {
2938 return xerrors.Errorf("update injected context: %w", err)
2939 }
2940 if _, err := db.UpdateChatLastInjectedContext(ctx, database.UpdateChatLastInjectedContextParams{
2941 ID: chatID,
2942 LastInjectedContext: param,
2943 }); err != nil {
2944 return xerrors.Errorf("update injected context: %w", err)
2945 }
2946 return nil
2947}
2948
2949func messageHasPartTypes(raw []byte, types ...codersdk.ChatMessagePartType) bool {
2950 var parts []codersdk.ChatMessagePart