diff --git a/internal/adapter/openai/files_route_test.go b/internal/adapter/openai/files_route_test.go index 4db1ea5..6c8eb0b 100644 --- a/internal/adapter/openai/files_route_test.go +++ b/internal/adapter/openai/files_route_test.go @@ -16,6 +16,30 @@ import ( "ds2api/internal/deepseek" ) +type managedFilesAuthStub struct{} + +func (managedFilesAuthStub) Determine(_ *http.Request) (*auth.RequestAuth, error) { + return &auth.RequestAuth{ + UseConfigToken: true, + DeepSeekToken: "managed-token", + CallerID: "caller:test", + AccountID: "acct-123", + TriedAccounts: map[string]bool{}, + }, nil +} + +func (managedFilesAuthStub) DetermineCaller(_ *http.Request) (*auth.RequestAuth, error) { + return &auth.RequestAuth{ + UseConfigToken: true, + DeepSeekToken: "managed-token", + CallerID: "caller:test", + AccountID: "acct-123", + TriedAccounts: map[string]bool{}, + }, nil +} + +func (managedFilesAuthStub) Release(_ *auth.RequestAuth) {} + type filesRouteDSStub struct { lastReq deepseek.UploadFileRequest upload *deepseek.UploadFileResult @@ -115,6 +139,28 @@ func TestFilesRouteUploadSuccess(t *testing.T) { } } +func TestFilesRouteUploadIncludesAccountIDForManagedAccount(t *testing.T) { + ds := &filesRouteDSStub{} + h := &Handler{Store: mockOpenAIConfig{wideInput: true}, Auth: managedFilesAuthStub{}, DS: ds} + r := chi.NewRouter() + RegisterRoutes(r, h) + + req := newMultipartUploadRequest(t, "assistants", "notes.txt", []byte("hello world")) + rec := httptest.NewRecorder() + r.ServeHTTP(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("expected 200, got %d body=%s", rec.Code, rec.Body.String()) + } + var out map[string]any + if err := json.Unmarshal(rec.Body.Bytes(), &out); err != nil { + t.Fatalf("decode response failed: %v body=%s", err, rec.Body.String()) + } + if out["account_id"] != "acct-123" { + t.Fatalf("expected account_id acct-123, got %#v", out["account_id"]) + } +} + func TestFilesRouteRejectsNonMultipart(t *testing.T) { h := &Handler{Store: mockOpenAIConfig{wideInput: true}, Auth: streamStatusAuthStub{}, DS: &filesRouteDSStub{}} r := chi.NewRouter() diff --git a/internal/adapter/openai/handler_files.go b/internal/adapter/openai/handler_files.go index 5a86d24..8253135 100644 --- a/internal/adapter/openai/handler_files.go +++ b/internal/adapter/openai/handler_files.go @@ -61,12 +61,15 @@ func (h *Handler) UploadFile(w http.ResponseWriter, r *http.Request) { writeOpenAIError(w, http.StatusInternalServerError, "Failed to upload file.") return } + if result != nil && result.AccountID == "" { + result.AccountID = a.AccountID + } writeJSON(w, http.StatusOK, buildOpenAIFileObject(result)) } func buildOpenAIFileObject(result *deepseek.UploadFileResult) map[string]any { if result == nil { - return map[string]any{ + obj := map[string]any{ "id": "", "object": "file", "bytes": 0, @@ -76,8 +79,9 @@ func buildOpenAIFileObject(result *deepseek.UploadFileResult) map[string]any { "status": "uploaded", "status_details": nil, } + return obj } - return map[string]any{ + obj := map[string]any{ "id": result.ID, "object": "file", "bytes": result.Bytes, @@ -87,4 +91,8 @@ func buildOpenAIFileObject(result *deepseek.UploadFileResult) map[string]any { "status": result.Status, "status_details": nil, } + if result.AccountID != "" { + obj["account_id"] = result.AccountID + } + return obj } diff --git a/internal/deepseek/client_upload.go b/internal/deepseek/client_upload.go index 573a804..c494b7b 100644 --- a/internal/deepseek/client_upload.go +++ b/internal/deepseek/client_upload.go @@ -31,6 +31,7 @@ type UploadFileResult struct { Bytes int64 Status string Purpose string + AccountID string IsImage bool Raw map[string]any RawHeaders http.Header @@ -117,6 +118,9 @@ func (c *Client) UploadFile(ctx context.Context, a *auth.RequestAuth, req Upload if result.Purpose == "" { result.Purpose = purpose } + if result.AccountID == "" { + result.AccountID = a.AccountID + } if result.ID == "" { return nil, errors.New("upload file succeeded without file id") } @@ -234,6 +238,9 @@ func extractUploadFileResult(resp map[string]any) *UploadFileResult { if result.Purpose == "" { result.Purpose = firstNonEmptyString(m, "purpose") } + if result.AccountID == "" { + result.AccountID = firstNonEmptyString(m, "account_id", "accountId", "owner_account_id", "ownerAccountId") + } if result.Bytes == 0 { result.Bytes = firstPositiveInt64(m, "bytes", "size", "file_size") } diff --git a/webui/src/features/apiTester/ApiTesterContainer.jsx b/webui/src/features/apiTester/ApiTesterContainer.jsx index fe80753..96e824a 100644 --- a/webui/src/features/apiTester/ApiTesterContainer.jsx +++ b/webui/src/features/apiTester/ApiTesterContainer.jsx @@ -109,6 +109,7 @@ export default function ApiTesterContainer({ config, onMessage, authFetch }) { setMessage={setMessage} attachedFiles={attachedFiles} setAttachedFiles={setAttachedFiles} + setSelectedAccount={setSelectedAccount} effectiveKey={effectiveKey} selectedAccount={selectedAccount} onMessage={onMessage} diff --git a/webui/src/features/apiTester/ChatPanel.jsx b/webui/src/features/apiTester/ChatPanel.jsx index 4633e37..86f865f 100644 --- a/webui/src/features/apiTester/ChatPanel.jsx +++ b/webui/src/features/apiTester/ChatPanel.jsx @@ -2,12 +2,15 @@ import { Bot, Loader2, Send, Square, User, Zap, Paperclip, X, FileIcon } from 'l import clsx from 'clsx' import { useRef, useState } from 'react' +import { getAttachedFileAccountIds } from './fileAccountBinding' + export default function ChatPanel({ t, message, setMessage, attachedFiles = [], setAttachedFiles, + setSelectedAccount, effectiveKey, selectedAccount, onMessage, @@ -32,6 +35,8 @@ export default function ChatPanel({ } setUploadingFiles(true) + const initialSelectedAccount = String(selectedAccount || '').trim() + let boundAccount = initialSelectedAccount for (const file of files) { const formData = new FormData() formData.append('file', file) @@ -40,8 +45,8 @@ export default function ChatPanel({ const headers = { 'Authorization': `Bearer ${effectiveKey}`, } - if (selectedAccount) { - headers['X-Ds2-Target-Account'] = selectedAccount + if (boundAccount) { + headers['X-Ds2-Target-Account'] = boundAccount } try { @@ -57,11 +62,18 @@ export default function ChatPanel({ } const data = await res.json() setAttachedFiles(prev => [...prev, data]) + const uploadedAccount = String(data?.account_id || '').trim() + if (!boundAccount && uploadedAccount) { + boundAccount = uploadedAccount + } } catch (error) { onMessage('error', error.message || 'Network error during upload') } } setUploadingFiles(false) + if (!initialSelectedAccount && boundAccount && setSelectedAccount) { + setSelectedAccount(boundAccount) + } if (fileInputRef.current) { fileInputRef.current.value = '' } @@ -70,6 +82,9 @@ export default function ChatPanel({ const removeFile = (id) => { setAttachedFiles(prev => prev.filter(f => f.id !== id)) } + + const attachmentAccountIds = getAttachedFileAccountIds(attachedFiles) + const attachmentAccountId = attachmentAccountIds.length === 1 ? attachmentAccountIds[0] : '' return (