Skip to content

Commit

Permalink
Feat/expose agent state (CopilotKit#1283)
Browse files Browse the repository at this point in the history
Co-authored-by: Przemyslaw Wlodarczyk <przemyslaw.wlodarczyk@bcf-software.com>
  • Loading branch information
arielweinberger and pwdevbcf authored Jan 28, 2025
1 parent c4d0b09 commit b49d96f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
19 changes: 16 additions & 3 deletions CopilotKit/packages/runtime/src/lib/runtime/copilot-runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,14 @@ export interface CopilotRuntimeConstructorParams<T extends Parameter[] | [] = []
* An array of LangServer URLs.
*/
langserve?: RemoteChainParameters[];

/*
* Delegates agent state processing to the service adapter.
*
* When enabled, individual agent state requests will not be processed by the agent itself.
* Instead, all processing will be handled by the service adapter.
*/
delegateAgentProcessingToServiceAdapter?: boolean;
}

export class CopilotRuntime<const T extends Parameter[] | [] = []> {
Expand All @@ -177,6 +185,7 @@ export class CopilotRuntime<const T extends Parameter[] | [] = []> {
private langserve: Promise<Action<any>>[] = [];
private onBeforeRequest?: OnBeforeRequestHandler;
private onAfterRequest?: OnAfterRequestHandler;
private delegateAgentProcessingToServiceAdapter: boolean;

constructor(params?: CopilotRuntimeConstructorParams<T>) {
this.actions = params?.actions || [];
Expand All @@ -190,6 +199,8 @@ export class CopilotRuntime<const T extends Parameter[] | [] = []> {

this.onBeforeRequest = params?.middleware?.onBeforeRequest;
this.onAfterRequest = params?.middleware?.onAfterRequest;
this.delegateAgentProcessingToServiceAdapter =
params?.delegateAgentProcessingToServiceAdapter || false;
}

async processRuntimeRequest(request: CopilotRuntimeRequest): Promise<CopilotRuntimeResponse> {
Expand All @@ -202,15 +213,16 @@ export class CopilotRuntime<const T extends Parameter[] | [] = []> {
outputMessagesPromise,
graphqlContext,
forwardedParameters,
agentSession,
url,
extensions,
agentSession,
agentStates,
} = request;

const eventSource = new RuntimeEventSource();

try {
if (agentSession) {
if (agentSession && !this.delegateAgentProcessingToServiceAdapter) {
return await this.processAgentRequest(request);
}
if (serviceAdapter instanceof EmptyAdapter) {
Expand All @@ -222,7 +234,6 @@ please use an LLM adapter instead.`,
}

const messages = rawMessages.filter((message) => !message.agentStateMessage);

const inputMessages = convertGqlInputToMessages(messages);
const serverSideActions = await this.getServerSideActions(request);

Expand Down Expand Up @@ -256,6 +267,8 @@ please use an LLM adapter instead.`,
eventSource,
forwardedParameters,
extensions,
agentSession,
agentStates,
});

// for backwards compatibility, we deal with the case that no threadId is provided
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { ActionInput } from "../graphql/inputs/action.input";
import { ForwardedParametersInput } from "../graphql/inputs/forwarded-parameters.input";
import { ExtensionsInput } from "../graphql/inputs/extensions.input";
import { ExtensionsResponse } from "../graphql/types/extensions-response.type";
import { AgentSessionInput } from "../graphql/inputs/agent-session.input";
import { AgentStateInput } from "../graphql/inputs/agent-state.input";

export interface CopilotKitResponse {
stream: ReadableStream;
Expand All @@ -19,6 +21,8 @@ export interface CopilotRuntimeChatCompletionRequest {
runId?: string;
forwardedParameters?: ForwardedParametersInput;
extensions?: ExtensionsInput;
agentSession?: AgentSessionInput;
agentStates?: AgentStateInput[];
}

export interface CopilotRuntimeChatCompletionResponse {
Expand Down
4 changes: 4 additions & 0 deletions docs/content/docs/reference/classes/CopilotRuntime.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ A list of remote actions that can be executed.
An array of LangServer URLs.
</PropertyReference>

<PropertyReference name="delegateAgentProcessingToServiceAdapter" type="boolean" >
Delegates agent state processing to the service adapter - exposing `agentSession` and `agentStates`.
</PropertyReference>

<PropertyReference name="processRuntimeRequest" type="request: CopilotRuntimeRequest">


Expand Down

0 comments on commit b49d96f

Please sign in to comment.