Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions agent/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ import (

// RunRequest is the input for the synchronous agent endpoint.
type RunRequest struct {
Prompt string `json:"prompt"`
Model string `json:"model"`
Prompt string `json:"prompt"`
Model string `json:"model"`
ContextID string `json:"context_id"` // prior flow ID for follow-ups
}

// RunResponse is the output of the synchronous agent endpoint.
type RunResponse struct {
Answer string `json:"answer"`
FlowID string `json:"flow_id,omitempty"`
Tools []ToolUsed `json:"tools,omitempty"`
Error string `json:"error,omitempty"`
}
Expand Down Expand Up @@ -146,5 +148,23 @@ func RunHandler(w http.ResponseWriter, r *http.Request) {
}

answer = app.StripLatexDollars(answer)
app.RespondJSON(w, RunResponse{Answer: answer, Tools: toolsUsed})

// Save as a flow so it appears in the agent history at /agent.
var steps []FlowStep
for _, tu := range toolsUsed {
steps = append(steps, FlowStep{Tool: tu.Name})
}
flow := &Flow{
ID: newFlowID(),
AccountID: acc.ID,
Prompt: req.Prompt,
Steps: steps,
Answer: answer,
Status: "done",
ParentID: req.ContextID,
CreatedAt: time.Now().UTC(),
}
saveFlow(flow)

app.RespondJSON(w, RunResponse{Answer: answer, FlowID: flow.ID, Tools: toolsUsed})
}
4 changes: 3 additions & 1 deletion home/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,7 @@ const consoleScript = `<script>
(function(){
var form = document.getElementById('console-form');
var resp = document.getElementById('console-response');
var currentFlowId = '';
if (!form || !resp) return;

function csrfToken() {
Expand Down Expand Up @@ -577,12 +578,13 @@ const consoleScript = `<script>
method: 'POST',
credentials: 'same-origin',
headers: headers,
body: JSON.stringify({ prompt: q })
body: JSON.stringify({ prompt: q, context_id: currentFlowId })
}).then(function(r){
if (!r.ok) return r.text().then(function(t){ throw new Error(t) });
return r.json();
}).then(function(data){
var answer = (data && data.answer) ? data.answer : (typeof data === 'string' ? data : JSON.stringify(data));
if (data && data.flow_id) currentFlowId = data.flow_id;
var ae = document.getElementById(qid+'-a'); if(ae) ae.outerHTML = '<div style="color:#555;line-height:1.6;word-wrap:break-word">' + renderMd(answer) + '</div>';
}).catch(function(err){
var ee = document.getElementById(qid+'-a'); if(ee) ee.outerHTML = '<p style="color:#c00;margin:0">' + escHtml(err.message || 'Something went wrong') + '</p>';
Expand Down
Loading