showcase/shell-docs/src/content/reference/vue/hooks/useAgentContext.mdx
useAgentContext registers reactive contextual data that is sent with agent runs. The context entry is added while the composable is active and removed automatically on scope cleanup, so the agent always sees an up-to-date snapshot of your application state without manual teardown.
Because the parameters accept MaybeRefOrGetter, you can pass plain values, refs, computeds, or getter functions. When any reactive source changes, the registered context is updated automatically. This lets you surface any serializable application state (user preferences, selected items, computed values, etc.) as context that agents can reference when generating responses or making decisions.
import { useAgentContext } from "@copilotkit/vue/v2";
function useAgentContext(context: AgentContextInput): void;
Provide simple, static application state as context for the agent.
<script setup lang="ts">
import { useAgentContext } from "@copilotkit/vue/v2";
useAgentContext({
description: "Current workspace",
value: "copilotkit-vue",
});
</script>
<template>
<div>Workspace ready</div>
</template>
Pass refs or getters so the context updates automatically whenever the underlying state changes. There is no need to re-register.
<script setup lang="ts">
import { ref } from "vue";
import { useAgentContext } from "@copilotkit/vue/v2";
const selectedCategory = ref("electronics");
const priceRange = ref({ min: 0, max: 500 });
useAgentContext({
description: "The user's current product filter settings",
value: () => ({
category: selectedCategory.value,
priceRange: priceRange.value,
}),
});
</script>
<template>
<select v-model="selectedCategory">
<option value="electronics">Electronics</option>
<option value="books">Books</option>
<option value="clothing">Clothing</option>
</select>
<!-- ... price range controls ... -->
</template>
A computed ref works anywhere a MaybeRefOrGetter is accepted, including the description.
<script setup lang="ts">
import { computed, ref } from "vue";
import { useAgentContext } from "@copilotkit/vue/v2";
const props = defineProps<{
user: { name: string; role: string };
}>();
const userContext = computed(() => ({
name: props.user.name,
role: props.user.role,
}));
useAgentContext({
description: computed(() => `Profile for ${props.user.name}`),
value: userContext,
});
</script>
<template>
<div>Welcome, {{ user.name }}</div>
</template>
Each component can register its own context. All registered contexts are visible to the agent simultaneously.
<script setup lang="ts">
import { ref } from "vue";
import { useAgentContext } from "@copilotkit/vue/v2";
const collapsed = ref(false);
useAgentContext({
description: "Current dashboard view and layout",
value: { view: "analytics", columns: 3 },
});
useAgentContext({
description: "Sidebar navigation state",
value: () => ({ collapsed: collapsed.value, activeSection: "reports" }),
});
</script>
<template>
<div>
<!-- dashboard layout -->
</div>
</template>
description and value are resolved with toValue, so refs, computeds, and getters are tracked. When a resolved value changes, the previous context entry is removed and the new value is re-registered, keeping the agent in sync.JSON.stringify, so it must conform to JsonSerializable (string | number | boolean | null | JsonSerializable[] | { [key: string]: JsonSerializable }). Non-serializable values such as functions, class instances, or symbols will not serialize correctly.useAgentContext calls across your component tree are all visible to the agent concurrently. Each is identified internally by an ID returned from addContext and removed on cleanup.void.useCopilotKit - access the underlying CopilotKit instanceCopilotKitProvider - provides the CopilotKit instance to descendant composables