Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit871c4fc

Browse files
committed
feat: add endpoint to list interceptions
1 parenta8805c0 commit871c4fc

File tree

13 files changed

+946
-0
lines changed

13 files changed

+946
-0
lines changed

‎coderd/aibridge.go‎

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package coderd
2+
3+
import (
4+
"net/http"
5+
6+
"github.com/coder/coder/v2/coderd/database"
7+
"github.com/coder/coder/v2/coderd/httpapi"
8+
"github.com/coder/coder/v2/coderd/rbac"
9+
"github.com/coder/coder/v2/coderd/rbac/policy"
10+
"github.com/coder/coder/v2/codersdk"
11+
)
12+
13+
const (
14+
maxListInterceptionsLimit=1000
15+
defaultListInterceptionsLimit=100
16+
)
17+
18+
func (api*API)aiBridgeListInterceptions(rw http.ResponseWriter,r*http.Request) {
19+
if!api.Authorize(r,policy.ActionRead,rbac.ResourceAibridgeInterception) {
20+
httpapi.Forbidden(rw)
21+
return
22+
}
23+
24+
ctx:=r.Context()
25+
varreq codersdk.AIBridgeListInterceptionsRequest
26+
if!httpapi.Read(ctx,rw,r,&req) {
27+
return
28+
}
29+
30+
if!req.PeriodStart.IsZero()&&!req.PeriodEnd.IsZero()&&req.PeriodEnd.Before(req.PeriodStart) {
31+
httpapi.Write(ctx,rw,http.StatusBadRequest, codersdk.Response{
32+
Message:"Invalid time frame.",
33+
Detail:"End of the search period must be before start.",
34+
})
35+
return
36+
}
37+
38+
ifreq.Limit==0 {
39+
req.Limit=defaultListInterceptionsLimit
40+
}
41+
42+
ifreq.Limit>maxListInterceptionsLimit||req.Limit<1 {
43+
httpapi.Write(ctx,rw,http.StatusBadRequest, codersdk.Response{
44+
Message:"Invalid limit value.",
45+
Detail:"Limit value must be in range <1, 1000>",
46+
})
47+
return
48+
}
49+
50+
// Database returns one row for each tuple (interception, tool, prompt).
51+
// Right now there is a single promp per interception although model allows multiple so this could change in the future.
52+
// There can be multiple tools used in single interception.
53+
// Results are ordered by Interception.StartedAt, Interception.ID, Tool.CreatedAt
54+
rows,err:=api.Database.ListAIBridgeInterceptions(ctx, database.ListAIBridgeInterceptionsParams{
55+
PeriodStart:req.PeriodStart,
56+
PeriodEnd:req.PeriodEnd,
57+
CursorTime:req.Cursor.Time,
58+
CursorID:req.Cursor.ID,
59+
InitiatorID:req.InitiatorID,
60+
LimitOpt:req.Limit,
61+
})
62+
63+
iferr!=nil {
64+
httpapi.Write(ctx,rw,http.StatusInternalServerError, codersdk.Response{
65+
Message:"Internal error getting AIBridge interceptions.",
66+
Detail:err.Error(),
67+
})
68+
return
69+
}
70+
71+
resp:=prepareResponse(rows)
72+
httpapi.Write(ctx,rw,http.StatusOK,resp)
73+
}
74+
75+
funcprepareResponse(rows []database.ListAIBridgeInterceptionsRow) codersdk.AIBridgeListInterceptionsResponse {
76+
resp:= codersdk.AIBridgeListInterceptionsResponse{
77+
Results: []codersdk.AIBridgeListInterceptionsResult{},
78+
}
79+
80+
iflen(rows)>0 {
81+
resp.Cursor.ID=rows[len(rows)-1].ID
82+
resp.Cursor.Time=rows[len(rows)-1].StartedAt.UTC()
83+
}
84+
85+
fori:=0;i<len(rows); {
86+
row:=rows[i]
87+
row.StartedAt=row.StartedAt.UTC()
88+
89+
result:= codersdk.AIBridgeListInterceptionsResult{
90+
InterceptionID:row.ID,
91+
UserID:row.InitiatorID,
92+
Provider:row.Provider,
93+
Model:row.Model,
94+
Prompt:row.Prompt.String,
95+
StartedAt:row.StartedAt,
96+
Tokens: codersdk.AIBridgeListInterceptionsTokens{
97+
Input:row.InputTokens,
98+
Output:row.OutputTokens,
99+
},
100+
Tools: []codersdk.AIBridgeListInterceptionsTool{},
101+
}
102+
103+
interceptionID:=row.ID
104+
for ;i<len(rows)&&interceptionID==rows[i].ID;i++ {
105+
ifrows[i].ServerUrl.Valid||rows[i].Tool.Valid||rows[i].Input.Valid {
106+
result.Tools=append(result.Tools, codersdk.AIBridgeListInterceptionsTool{
107+
Server:rows[i].ServerUrl.String,
108+
Tool:rows[i].Tool.String,
109+
Input:rows[i].Input.String,
110+
})
111+
}
112+
}
113+
114+
resp.Results=append(resp.Results,result)
115+
}
116+
returnresp
117+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp