(input: RunAgentInput)
| 948 | } |
| 949 | |
| 950 | run(input: RunAgentInput): Observable<BaseEvent> { |
| 951 | if (isFactoryConfig(this.config)) { |
| 952 | return this.runFactory(input, this.config); |
| 953 | } |
| 954 | |
| 955 | // Capture the narrowed classic config — the narrowing of `this.config` |
| 956 | // above does not survive into the Observable/async closures below. |
| 957 | const config = this.config; |
| 958 | |
| 959 | if (this.abortController) { |
| 960 | throw new Error( |
| 961 | "Agent is already running. Call abortRun() first or create a new instance.", |
| 962 | ); |
| 963 | } |
| 964 | |
| 965 | // Set synchronously before Observable creation to close TOCTOU window |
| 966 | this.abortController = new AbortController(); |
| 967 | const abortController = this.abortController; |
| 968 | |
| 969 | return new Observable<BaseEvent>((subscriber) => { |
| 970 | // Emit RUN_STARTED event |
| 971 | const startEvent: RunStartedEvent = { |
| 972 | type: EventType.RUN_STARTED, |
| 973 | threadId: input.threadId, |
| 974 | runId: input.runId, |
| 975 | }; |
| 976 | subscriber.next(startEvent); |
| 977 | |
| 978 | // Resolve the model, passing API key if provided |
| 979 | const model = resolveModel(config.model, config.apiKey); |
| 980 | |
| 981 | // Build prompt based on conditions |
| 982 | let systemPrompt: string | undefined = undefined; |
| 983 | |
| 984 | // Check if we should build a prompt: |
| 985 | // - config.prompt is set, OR |
| 986 | // - input.context is non-empty, OR |
| 987 | // - input.state is non-empty and not an empty object |
| 988 | const hasPrompt = !!config.prompt; |
| 989 | const hasContext = input.context && input.context.length > 0; |
| 990 | const hasState = |
| 991 | input.state !== undefined && |
| 992 | input.state !== null && |
| 993 | !( |
| 994 | typeof input.state === "object" && |
| 995 | Object.keys(input.state).length === 0 |
| 996 | ); |
| 997 | |
| 998 | if (hasPrompt || hasContext || hasState) { |
| 999 | const parts: string[] = []; |
| 1000 | |
| 1001 | // First: the prompt if any |
| 1002 | if (hasPrompt) { |
| 1003 | parts.push(config.prompt!); |
| 1004 | } |
| 1005 | |
| 1006 | // Second: context from the application |
| 1007 | if (hasContext) { |
nothing calls this directly
no test coverage detected