# Shared State

> Read and write agent state with Angular signals, and choose between shared state and agent context.

Shared state and agent context solve two different data flows.

| Data flow | Use |
| --- | --- |
| Agent and application both read and write the value | `injectAgentStore` and `agent.setState` |
| The application owns the value and the agent only reads it | `connectAgentContext` or `CopilotKitAgentContext` |

This page covers shared state. For values the agent only reads, see
[Agent Read-Only Context](/angular/shared-state/agent-readonly).

## Read agent state [#read-agent-state]

`injectAgentStore` returns a signal that resolves one agent. The store exposes
messages, state, and run status as nested signals.

```ts title="src/app/workspace.component.ts"
import { Component, computed } from "@angular/core";
import { injectAgentStore } from "@copilotkit/angular";

type WorkspaceState = {
  notes: string[];
  priority: "low" | "normal" | "high";
};

const EMPTY_STATE: WorkspaceState = {
  notes: [],
  priority: "normal",
};

@Component({
  selector: "app-workspace",
  template: `
    <p>Priority: {{ state().priority }}</p>
    <ul>
      @for (note of state().notes; track note) {
        <li>{{ note }}</li>
      }
    </ul>
    <button type="button" (click)="setPriority('high')">
      Mark high priority
    </button>
  `,
})
export class WorkspaceComponent {
  readonly store = injectAgentStore("default");
  readonly state = computed(
    () => (this.store().state() as WorkspaceState | undefined) ?? EMPTY_STATE,
  );

  protected setPriority(priority: WorkspaceState["priority"]): void {
    const agent = this.store().agent;
    const current = (agent.state as WorkspaceState | undefined) ?? EMPTY_STATE;
    agent.setState({ ...current, priority });
  }
}
```

Read through `store().state()` so Angular tracks changes. Write through the
plain AG-UI agent at `store().agent`. Replace the object instead of mutating it
in place.

Click **Mark high priority**, then ask the agent for the current priority. It
answers "high".

The same store gives you:

- `store().messages()` for the current conversation
- `store().isRunning()` for loading controls
- `store().agent` for `setState`, `addMessage`, and `abortRun`

To start a run, call `inject(CopilotKit).core.runAgent({ agent })`, as in the
[headless chat](/angular/custom-look-and-feel/headless-ui#build-a-headless-chat).
That call sends your registered frontend tools and agent context with the run.

## Keep ownership clear [#keep-ownership-clear]

- Use shared state for data that the agent may change.
- Use context for application-owned facts.
- Keep durable user preferences in your own data store; publish only the part
  the current agent needs.
- Scope `injectAgentStore` to the same agent id as the chat or workflow that
  reads the state.

## Next steps [#next-steps]

- [injectAgentStore API](/reference/angular/functions/injectAgentStore)
- [Agent Read-Only Context](/angular/shared-state/agent-readonly)
