Skip to content

Commit 606634e

Browse files
chore: make task run create new runs in the UI (#2325)
1 parent ce01746 commit 606634e

File tree

16 files changed

+156
-107
lines changed

16 files changed

+156
-107
lines changed

pkg/api/authz/resources.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,6 @@ var apiResources = []string{
5151
"DELETE /api/assistants/{assistant_id}/projects/{project_id}/tasks/{task_id}",
5252
"GET /api/assistants/{assistant_id}/projects/{project_id}/tasks/{task_id}",
5353
"PUT /api/assistants/{assistant_id}/projects/{project_id}/tasks/{task_id}",
54-
"GET /api/assistants/{assistant_id}/projects/{project_id}/tasks/{task_id}/events",
55-
"POST /api/assistants/{assistant_id}/projects/{project_id}/tasks/{task_id}/events",
5654
"POST /api/assistants/{assistant_id}/projects/{project_id}/tasks/{task_id}/run",
5755
"GET /api/assistants/{assistant_id}/projects/{project_id}/tasks/{task_id}/runs",
5856
"DELETE /api/assistants/{assistant_id}/projects/{project_id}/tasks/{task_id}/runs/{run_id}",
@@ -66,6 +64,7 @@ var apiResources = []string{
6664
"DELETE /api/assistants/{assistant_id}/projects/{project_id}/tasks/{task_id}/runs/{run_id}/files/{file...}",
6765
"GET /api/assistants/{assistant_id}/projects/{project_id}/tasks/{task_id}/runs/{run_id}/files/{file...}",
6866
"POST /api/assistants/{assistant_id}/projects/{project_id}/tasks/{task_id}/runs/{run_id}/files/{file...}",
67+
"POST /api/assistants/{assistant_id}/projects/{project_id}/tasks/{task_id}/runs/{run_id}/steps/{step_id}/run",
6968
"GET /api/assistants/{assistant_id}/projects/{project_id}/threads",
7069
"POST /api/assistants/{assistant_id}/projects/{project_id}/threads",
7170
"DELETE /api/assistants/{assistant_id}/projects/{project_id}/threads/{thread_id}",

pkg/api/handlers/assistants.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ func (a *AssistantHandler) SetTools(req api.Context) error {
468468
}
469469

470470
for _, tool := range tools.Items {
471-
if tool.Enabled && !tool.Builtin {
471+
if tool.Enabled && !tool.Builtin && strings.HasPrefix(tool.Name, system.ToolPrefix) {
472472
toolList = append(toolList, tool.ID)
473473
}
474474
}

pkg/api/handlers/tasks.go

Lines changed: 16 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/obot-platform/obot/pkg/wait"
2020
apierrors "k8s.io/apimachinery/pkg/api/errors"
2121
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
22+
"k8s.io/apimachinery/pkg/runtime/schema"
2223
kclient "sigs.k8s.io/controller-runtime/pkg/client"
2324
)
2425

@@ -48,19 +49,16 @@ func (t *TaskHandler) Abort(req api.Context) error {
4849
}
4950

5051
func (t *TaskHandler) AbortFromScope(req api.Context) error {
51-
workflow, userThread, err := t.getTask(req)
52+
workflow, projectThread, err := t.getTask(req)
5253
if err != nil {
5354
return err
5455
}
5556

56-
return t.abort(req, workflow, userThread.Name)
57+
return t.abort(req, workflow, projectThread.Name)
5758
}
5859

5960
func (t *TaskHandler) abort(req api.Context, workflow *v1.Workflow, threadName string) error {
6061
taskRunID := req.PathValue("run_id")
61-
if taskRunID == "" {
62-
taskRunID = editorWFE(req, workflow.Name)
63-
}
6462

6563
wfe, err := wait.For(req.Context(), req.Storage, &v1.WorkflowExecution{
6664
ObjectMeta: metav1.ObjectMeta{
@@ -109,9 +107,6 @@ func (t *TaskHandler) EventsFromScope(req api.Context) error {
109107

110108
func (t *TaskHandler) streamEvents(req api.Context, workflow *v1.Workflow, threadName string) error {
111109
taskRunID := req.PathValue("run_id")
112-
if taskRunID == "" {
113-
taskRunID = editorWFE(req, workflow.Name)
114-
}
115110

116111
wfe, err := wait.For(req.Context(), req.Storage, &v1.WorkflowExecution{
117112
ObjectMeta: metav1.ObjectMeta{
@@ -149,10 +144,6 @@ func (t *TaskHandler) streamEvents(req api.Context, workflow *v1.Workflow, threa
149144
return req.WriteEvents(events)
150145
}
151146

152-
func editorWFE(req api.Context, workflowName string) string {
153-
return name.SafeHashConcatName(system.ThreadPrefix, workflowName, req.User.GetUID())
154-
}
155-
156147
func (t *TaskHandler) AbortRun(req api.Context) error {
157148
var workflow v1.Workflow
158149
if err := req.Get(&workflow, req.PathValue("id")); err != nil {
@@ -177,10 +168,6 @@ func (t *TaskHandler) abortRun(req api.Context, workflow *v1.Workflow) error {
177168
runID = req.PathValue("run_id")
178169
)
179170

180-
if runID == "editor" {
181-
runID = editorWFE(req, workflow.Name)
182-
}
183-
184171
if err := req.Get(&wfe, runID); err != nil {
185172
return err
186173
}
@@ -197,15 +184,6 @@ func (t *TaskHandler) abortRun(req api.Context, workflow *v1.Workflow) error {
197184
return abortThread(req, &thread)
198185
}
199186

200-
func (t *TaskHandler) GetRun(req api.Context) error {
201-
var workflow v1.Workflow
202-
if err := req.Get(&workflow, req.PathValue("id")); err != nil {
203-
return err
204-
}
205-
206-
return t.getRun(req, &workflow)
207-
}
208-
209187
func (t *TaskHandler) GetRunFromScope(req api.Context) error {
210188
workflow, _, err := t.getTask(req)
211189
if err != nil {
@@ -220,9 +198,6 @@ func (t *TaskHandler) getRun(req api.Context, workflow *v1.Workflow) error {
220198
wfe v1.WorkflowExecution
221199
runID = req.PathValue("run_id")
222200
)
223-
if runID == "editor" {
224-
runID = editorWFE(req, workflow.Name)
225-
}
226201
if err := req.Get(&wfe, runID); err != nil {
227202
return err
228203
}
@@ -298,14 +273,10 @@ func (t *TaskHandler) listRuns(req api.Context, workflow *v1.Workflow, userThrea
298273
}
299274

300275
var (
301-
result types.TaskRunList
302-
editorWFE = editorWFE(req, workflow.Name)
276+
result types.TaskRunList
303277
)
304278

305279
for _, wfe := range wfeList.Items {
306-
if wfe.Name == editorWFE {
307-
continue
308-
}
309280
result.Items = append(result.Items, convertTaskRun(workflow, &wfe))
310281
}
311282

@@ -322,16 +293,17 @@ func (t *TaskHandler) Run(req api.Context) error {
322293
}
323294

324295
func (t *TaskHandler) RunFromScope(req api.Context) error {
325-
workflow, userThread, err := t.getTask(req)
296+
workflow, projectThread, err := t.getTask(req)
326297
if err != nil {
327298
return err
328299
}
329300

330-
return t.run(req, workflow, userThread.Name)
301+
return t.run(req, workflow, projectThread.Name)
331302
}
332303

333304
func (t *TaskHandler) run(req api.Context, workflow *v1.Workflow, threadName string) error {
334-
stepID := req.Request.URL.Query().Get("step")
305+
stepID := req.PathValue("step_id")
306+
runID := req.PathValue("run_id")
335307

336308
input, err := req.Body()
337309
if err != nil {
@@ -355,7 +327,7 @@ func (t *TaskHandler) run(req api.Context, workflow *v1.Workflow, threadName str
355327
return err
356328
}
357329
wfe = resp.WorkflowExecution
358-
} else if stepID == "" {
330+
} else if stepID == "" || runID == "" {
359331
wfe = &v1.WorkflowExecution{
360332
ObjectMeta: metav1.ObjectMeta{
361333
GenerateName: system.WorkflowExecutionPrefix,
@@ -365,6 +337,7 @@ func (t *TaskHandler) run(req api.Context, workflow *v1.Workflow, threadName str
365337
Input: string(input),
366338
ThreadName: threadName,
367339
WorkflowName: workflow.Name,
340+
RunUntilStep: req.URL.Query().Get("stepID"),
368341
RunName: getRunIDFromUser(req),
369342
},
370343
}
@@ -373,7 +346,7 @@ func (t *TaskHandler) run(req api.Context, workflow *v1.Workflow, threadName str
373346
}
374347
} else {
375348
resp, err := t.invoker.Workflow(req.Context(), req.Storage, workflow, string(input), invoke.WorkflowOptions{
376-
WorkflowExecutionName: editorWFE(req, workflow.Name),
349+
WorkflowExecutionName: runID,
377350
StepID: stepID,
378351
})
379352
if err != nil {
@@ -864,9 +837,6 @@ func getThreadForScope(req api.Context) (*v1.Thread, error) {
864837
taskID := req.PathValue("task_id")
865838
runID := req.PathValue("run_id")
866839
if taskID != "" && runID != "" {
867-
if runID == "editor" {
868-
runID = editorWFE(req, taskID)
869-
}
870840
var wfe v1.WorkflowExecution
871841
if err := req.Get(&wfe, runID); err != nil {
872842
return nil, err
@@ -877,6 +847,11 @@ func getThreadForScope(req api.Context) (*v1.Thread, error) {
877847
if wfe.Spec.WorkflowName != taskID {
878848
return nil, types.NewErrNotFound("task run not found")
879849
}
850+
if wfe.Status.ThreadName == "" {
851+
return nil, apierrors.NewNotFound(schema.GroupResource{
852+
Resource: "runs",
853+
}, runID)
854+
}
880855
return thread, req.Get(thread, wfe.Status.ThreadName)
881856
}
882857

pkg/api/router/router.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,8 @@ func Router(services *services.Services) (http.Handler, error) {
153153
mux.HandleFunc("DELETE /api/assistants/{assistant_id}/projects/{project_id}/tasks/{id}", tasks.DeleteFromScope)
154154
mux.HandleFunc("GET /api/assistants/{assistant_id}/projects/{project_id}/tasks/{id}", tasks.GetFromScope)
155155
mux.HandleFunc("PUT /api/assistants/{assistant_id}/projects/{project_id}/tasks/{id}", tasks.UpdateFromScope)
156-
mux.HandleFunc("POST /api/assistants/{assistant_id}/projects/{project_id}/tasks/{id}/events", tasks.AbortFromScope)
157-
mux.HandleFunc("GET /api/assistants/{assistant_id}/projects/{project_id}/tasks/{id}/events", tasks.EventsFromScope)
158156
mux.HandleFunc("POST /api/assistants/{assistant_id}/projects/{project_id}/tasks/{id}/run", tasks.RunFromScope)
157+
mux.HandleFunc("POST /api/assistants/{assistant_id}/projects/{project_id}/tasks/{id}/runs/{run_id}/steps/{step_id}/run", tasks.RunFromScope)
159158
mux.HandleFunc("GET /api/assistants/{assistant_id}/projects/{project_id}/tasks/{id}/runs", tasks.ListRunsFromScope)
160159
mux.HandleFunc("DELETE /api/assistants/{assistant_id}/projects/{project_id}/tasks/{id}/runs/{run_id}", tasks.DeleteRunFromScope)
161160
mux.HandleFunc("GET /api/assistants/{assistant_id}/projects/{project_id}/tasks/{id}/runs/{run_id}", tasks.GetRunFromScope)

pkg/controller/controller.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package controller
33
import (
44
"context"
55
"fmt"
6+
"time"
67

78
"github.com/obot-platform/nah/pkg/router"
89
"github.com/obot-platform/obot/pkg/controller/data"
@@ -45,7 +46,15 @@ func (c *Controller) PreStart(ctx context.Context) error {
4546

4647
func (c *Controller) PostStart(ctx context.Context, client kclient.Client) {
4748
go c.toolRefHandler.PollRegistries(ctx, client)
48-
if err := c.toolRefHandler.EnsureOpenAIEnvCredentialAndDefaults(ctx, client); err != nil {
49+
var err error
50+
for range 3 {
51+
err = c.toolRefHandler.EnsureOpenAIEnvCredentialAndDefaults(ctx, client)
52+
if err == nil {
53+
break
54+
}
55+
time.Sleep(500 * time.Millisecond) // wait a bit before retrying
56+
}
57+
if err != nil {
4958
panic(fmt.Errorf("failed to ensure openai env credential and defaults: %w", err))
5059
}
5160
}

pkg/events/events.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,17 @@ func (e *Emitter) printRun(ctx context.Context, state *printState, run v1.Run, r
354354
}
355355
runState, err := e.gatewayClient.RunState(ctx, run.Namespace, run.Name)
356356
if apierrors.IsNotFound(err) {
357+
var checkRun v1.Run
358+
if err := e.client.Get(ctx, router.Key(run.Namespace, run.Name), &checkRun); err == nil {
359+
if checkRun.Status.Error != "" {
360+
result <- types.Progress{
361+
RunID: run.Name,
362+
Time: types.NewTime(time.Now()),
363+
Error: checkRun.Status.Error,
364+
}
365+
return nil
366+
}
367+
}
357368
continue
358369
} else if err != nil {
359370
return err

pkg/render/render.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ func Agent(ctx context.Context, db kclient.Client, agent *v1.Agent, oauthServerU
123123
}
124124

125125
for _, t := range toolNames {
126+
if strings.HasPrefix(t, system.ToolPrefix) {
127+
continue
128+
}
126129
if !added && t == knowledgeToolName {
127130
continue
128131
}

ui/user/src/lib/components/Obot.svelte

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,6 @@
8282

8383
<div class="relative flex h-[calc(100%-76px)] max-w-full grow">
8484
{#if !responsive.isMobile || (responsive.isMobile && !layout.fileEditorOpen)}
85-
{@const taskRun =
86-
!!currentThreadID && layout.taskRuns?.find((run) => run.id === currentThreadID)}
8785
{#if layout.editTaskID && layout.tasks}
8886
{#each layout.tasks as task, i}
8987
{#if task.id === layout.editTaskID}
@@ -99,11 +97,15 @@
9997
{/key}
10098
{/if}
10199
{/each}
102-
{:else if taskRun}
103-
{@const task = layout.tasks?.find((t) => t.id === taskRun.taskID)}
104-
{#if task}
105-
<Task {project} {task} runId={taskRun.taskRunID} readOnly />
106-
{/if}
100+
{:else if layout.displayTaskRun}
101+
<Task
102+
{project}
103+
task={{
104+
...layout.displayTaskRun.task,
105+
id: layout.displayTaskRun.taskID
106+
}}
107+
runID={layout.displayTaskRun.id}
108+
/>
107109
{:else}
108110
<Thread bind:id={currentThreadID} bind:project />
109111
{/if}

ui/user/src/lib/components/shared/task/TaskItem.svelte

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
<script lang="ts">
22
import { ChevronDown, Trash2 } from 'lucide-svelte/icons';
33
import { overflowToolTip } from '$lib/actions/overflow';
4-
import { closeAll, getLayout, isSomethingSelected, openTask } from '$lib/context/layout.svelte';
4+
import {
5+
getLayout,
6+
isSomethingSelected,
7+
openTask,
8+
openTaskRun
9+
} from '$lib/context/layout.svelte';
510
import { formatTime } from '$lib/time.js';
611
import { type Task, type Thread, type Project } from '$lib/services';
712
import { twMerge } from 'tailwind-merge';
@@ -75,7 +80,12 @@
7580
if (responsive.isMobile) {
7681
layout.sidebarOpen = false;
7782
}
78-
openTask(layout, task.id);
83+
if (layout.editTaskID === task.id && expanded) {
84+
expanded = false;
85+
} else {
86+
expanded = true;
87+
openTask(layout, task.id);
88+
}
7989
}}
8090
>
8191
{task.name ?? ''}
@@ -96,9 +106,18 @@
96106
>
97107
<button
98108
class="w-full p-2 text-left"
99-
onclick={() => {
100-
closeAll(layout);
101-
currentThreadID = taskRun.id;
109+
onclick={async () => {
110+
if (taskRun.taskID && taskRun.taskRunID) {
111+
openTaskRun(
112+
layout,
113+
await ChatService.getTaskRun(
114+
project.assistantID,
115+
project.id,
116+
taskRun.taskID,
117+
taskRun.taskRunID
118+
)
119+
);
120+
}
102121
}}
103122
>
104123
{formatTime(taskRun.created)}

ui/user/src/lib/components/tasks/Step.svelte

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
task: Task;
2323
index: number;
2424
step: TaskStep;
25+
runID?: string;
2526
pending?: boolean;
2627
stepMessages?: Map<string, Messages>;
2728
project: Project;
@@ -35,6 +36,7 @@
3536
task = $bindable(),
3637
index,
3738
step = $bindable(),
39+
runID,
3840
pending,
3941
stepMessages,
4042
project,
@@ -97,10 +99,12 @@
9799
98100
async function doRun() {
99101
if (running || pending) {
100-
await ChatService.abort(project.assistantID, project.id, {
101-
taskID: task.id
102-
// runID: 'editor'
103-
});
102+
if (runID) {
103+
await ChatService.abort(project.assistantID, project.id, {
104+
taskID: task.id,
105+
runID: runID
106+
});
107+
}
104108
return;
105109
}
106110
if (running || pending || !step.step || step.step?.trim() === '') {
@@ -224,6 +228,7 @@
224228
{#key task.steps[index + 1].id}
225229
<Self
226230
{run}
231+
{runID}
227232
{pending}
228233
{task}
229234
index={index + 1}

0 commit comments

Comments
 (0)