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

Commitcada69e

Browse files
committed
WIP: replace dRPC API with HTTP API
Signed-off-by: Danny Kopping <dannykopping@gmail.com>
1 parent0589ddc commitcada69e

File tree

10 files changed

+330
-610
lines changed

10 files changed

+330
-610
lines changed

‎aibridged/aibridged.go

Lines changed: 73 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -47,21 +47,36 @@ type Server struct {
4747
shuttingDownBbool
4848
// shuttingDownCh will receive when we start graceful shutdown
4949
shuttingDownChchanstruct{}
50+
51+
bridge*Bridge
5052
}
5153

52-
funcNew(clientDialerDialer,logger slog.Logger) (*Server,error) {
53-
ctx,ctxCancel:=context.WithCancel(context.Background())
54+
funcNew(rpcDialerDialer,httpAddrstring,logger slog.Logger) (*Server,error) {
55+
ifrpcDialer==nil {
56+
returnnil,xerrors.Errorf("nil rpcDialer given")
57+
}
58+
59+
ctx,cancel:=context.WithCancel(context.Background())
60+
bridge:=NewBridge(httpAddr)
5461
daemon:=&Server{
5562
logger:logger,
56-
clientDialer:clientDialer,
63+
clientDialer:rpcDialer,
5764
clientCh:make(chan proto.DRPCAIBridgeDaemonClient),
5865
closeContext:ctx,
59-
closeCancel:ctxCancel,
66+
closeCancel:cancel,
6067
closedCh:make(chanstruct{}),
6168
shuttingDownCh:make(chanstruct{}),
6269
initConnectionCh:make(chanstruct{}),
70+
71+
bridge:bridge,
6372
}
6473
godaemon.connect()
74+
gofunc() {
75+
err:=bridge.Serve()
76+
// TODO: better error handling.
77+
// TODO: close on shutdown.
78+
logger.Error(ctx,"bridge server stopped",slog.Error(err))
79+
}()
6580

6681
returndaemon,nil
6782
}// Connect establishes a connection to coderd.
@@ -148,57 +163,61 @@ func (s *Server) AuditPrompt(ctx context.Context, in *proto.AuditPromptRequest)
148163
returnout,nil
149164
}
150165

151-
func (s*Server)ChatCompletions(payload*proto.JSONPayload,stream proto.DRPCOpenAIService_ChatCompletionsStream)error {
152-
// TODO: call OpenAI API.
166+
//func (s *Server) ChatCompletions(payload *proto.JSONPayload, stream proto.DRPCOpenAIService_ChatCompletionsStream) error {
167+
//// TODO: call OpenAI API.
168+
//
169+
//select {
170+
//case <-stream.Context().Done():
171+
//return nil
172+
//default:
173+
//}
174+
//
175+
//err := stream.Send(&proto.JSONPayload{
176+
//Content: `
177+
//{
178+
// "id": "chatcmpl-B9MBs8CjcvOU2jLn4n570S5qMJKcT",
179+
// "object": "chat.completion",
180+
// "created": 1741569952,
181+
// "model": "gpt-4.1-2025-04-14",
182+
// "choices": [
183+
// {
184+
// "index": 0,
185+
// "message": {
186+
// "role": "assistant",
187+
// "content": "Hello! How can I assist you today?",
188+
// "refusal": null,
189+
// "annotations": []
190+
// },
191+
// "logprobs": null,
192+
// "finish_reason": "stop"
193+
// }
194+
// ],
195+
// "usage": {
196+
// "prompt_tokens": 19,
197+
// "completion_tokens": 10,
198+
// "total_tokens": 29,
199+
// "prompt_tokens_details": {
200+
// "cached_tokens": 0,
201+
// "audio_tokens": 0
202+
// },
203+
// "completion_tokens_details": {
204+
// "reasoning_tokens": 0,
205+
// "audio_tokens": 0,
206+
// "accepted_prediction_tokens": 0,
207+
// "rejected_prediction_tokens": 0
208+
// }
209+
// },
210+
// "service_tier": "default"
211+
//}
212+
//`})
213+
//if err != nil {
214+
//return xerrors.Errorf("stream chat completion response: %w", err)
215+
//}
216+
//return nil
217+
//}
153218

154-
select {
155-
case<-stream.Context().Done():
156-
returnnil
157-
default:
158-
}
159-
160-
err:=stream.Send(&proto.JSONPayload{
161-
Content:`
162-
{
163-
"id": "chatcmpl-B9MBs8CjcvOU2jLn4n570S5qMJKcT",
164-
"object": "chat.completion",
165-
"created": 1741569952,
166-
"model": "gpt-4.1-2025-04-14",
167-
"choices": [
168-
{
169-
"index": 0,
170-
"message": {
171-
"role": "assistant",
172-
"content": "Hello! How can I assist you today?",
173-
"refusal": null,
174-
"annotations": []
175-
},
176-
"logprobs": null,
177-
"finish_reason": "stop"
178-
}
179-
],
180-
"usage": {
181-
"prompt_tokens": 19,
182-
"completion_tokens": 10,
183-
"total_tokens": 29,
184-
"prompt_tokens_details": {
185-
"cached_tokens": 0,
186-
"audio_tokens": 0
187-
},
188-
"completion_tokens_details": {
189-
"reasoning_tokens": 0,
190-
"audio_tokens": 0,
191-
"accepted_prediction_tokens": 0,
192-
"rejected_prediction_tokens": 0
193-
}
194-
},
195-
"service_tier": "default"
196-
}
197-
`})
198-
iferr!=nil {
199-
returnxerrors.Errorf("stream chat completion response: %w",err)
200-
}
201-
returnnil
219+
func (s*Server)BridgeAddr()string {
220+
returns.bridge.Addr()
202221
}
203222

204223
// TODO: direct copy/paste from provisionerd, abstract into common util.

‎aibridged/bridge.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package aibridged
2+
3+
import (
4+
"fmt"
5+
"net"
6+
"net/http"
7+
8+
"golang.org/x/xerrors"
9+
)
10+
11+
typeBridgestruct {
12+
srv*http.Server
13+
addrstring
14+
}
15+
16+
funcNewBridge(addrstring)*Bridge {
17+
mux:=&http.ServeMux{}
18+
mux.HandleFunc("/v1/chat/completions",bridgeOpenAIRequest)
19+
mux.HandleFunc("/v1/messages",bridgeAnthropicRequest)
20+
21+
srv:=&http.Server{
22+
Addr:addr,
23+
Handler:mux,
24+
// TODO: other settings.
25+
}
26+
27+
return&Bridge{srv:srv}
28+
}
29+
30+
func (b*Bridge)Serve()error {
31+
list,err:=net.Listen("tcp",b.srv.Addr)
32+
iferr!=nil {
33+
returnxerrors.Errorf("listen: %w",err)
34+
}
35+
36+
b.addr=list.Addr().String()
37+
38+
returnb.srv.Serve(list)// TODO: TLS.
39+
}
40+
41+
func (b*Bridge)Addr()string {
42+
returnb.addr
43+
}
44+
45+
funcbridgeOpenAIRequest(w http.ResponseWriter,r*http.Request) {
46+
fmt.Println("OpenAI")
47+
}
48+
49+
funcbridgeAnthropicRequest(w http.ResponseWriter,r*http.Request) {
50+
fmt.Println("Anthropic")
51+
}
52+
53+
funcbridgeRequest(w http.ResponseWriter,r*http.Request) {
54+
55+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp