Linger is An Open-Source Personal Agent That Runs Fully on Your PC. We aim to break Big Tech’s grip on AI by bringing powerful local AI to everyday computers through efficient model–harness co-design, so everyone can run their own.
feat(model-source): show the Computer's message when a BYOK endpoint refuses
LeoXingfd3695bf3613
diff --git a/apps/teamhub/internal/app/handlers_model_source.go b/apps/teamhub/internal/app/handlers_model_source.goindex ed8439e0..17adba7d 100644--- a/apps/teamhub/internal/app/handlers_model_source.go+++ b/apps/teamhub/internal/app/handlers_model_source.go@@ -59,6 +59,18 @@ var modelSourceErrorMessages = map[httpapi.ModelSourceErrorCode]string{httpapi.ModelSourceErrorCodeModelSourceUnconfirmed: "The Computer did not answer in time",}+// For these refusals the Computer's own message names the address it sent to+// and the endpoint's HTTP status. It never holds the key, and only the+// Computer's owner can send a BYOK request.+var modelSourceComputerMessageCodes = map[httpapi.ModelSourceErrorCode]bool{+ httpapi.ModelSourceErrorCodeByokAuthFailed: true,+ httpapi.ModelSourceErrorCodeByokNotFound: true,+ httpapi.ModelSourceErrorCodeByokUnreachable: true,+ httpapi.ModelSourceErrorCodeByokUpstreamRejected: true,+}++const maxComputerMessageLength = 1024+var modelSourceErrorStatuses = map[httpapi.ModelSourceErrorCode]int{httpapi.ModelSourceErrorCodeLocalComputerOffline: http.StatusServiceUnavailable,httpapi.ModelSourceErrorCodeModelSourceForbidden: http.StatusForbidden,@@ -354,7 +366,11 @@ func (a *App) writeModelSourceFailure(c *gin.Context, computerID, action string,a.logger.Warn("model_source.refused", "computer_id", computerID, "action", action, "refusal_code", refusal.Code)code = httpapi.ModelSourceErrorCodeModelSourceFailed}- a.writeModelSourceError(c, code)+ message := modelSourceErrorMessages[code]+ if modelSourceComputerMessageCodes[code] && refusal.Message != "" && len(refusal.Message) <= maxComputerMessageLength {+ message = refusal.Message+ }+ a.writeModelSourceErrorMessage(c, code, message)default:a.logger.Error("model_source.failed", "computer_id", computerID, "action", action, "error", err)a.writeModelSourceError(c, httpapi.ModelSourceErrorCodeModelSourceServiceUnavailable)@@ -362,11 +378,15 @@ func (a *App) writeModelSourceFailure(c *gin.Context, computerID, action string,}func (a *App) writeModelSourceError(c *gin.Context, code httpapi.ModelSourceErrorCode) {+ a.writeModelSourceErrorMessage(c, code, modelSourceErrorMessages[code])+}++func (a *App) writeModelSourceErrorMessage(c *gin.Context, code httpapi.ModelSourceErrorCode, message string) {status, found := modelSourceErrorStatuses[code]if !found {status = http.StatusConflict}- a.writeError(c, status, string(code), modelSourceErrorMessages[code])+ a.writeError(c, status, string(code), message)}func validModelSourceSwitch(request httpapi.ModelSourceSwitchRequest) bool {diff --git a/apps/teamhub/internal/roomagent/model_source.go b/apps/teamhub/internal/roomagent/model_source.goindex 1735468b..e8c12f8e 100644--- a/apps/teamhub/internal/roomagent/model_source.go+++ b/apps/teamhub/internal/roomagent/model_source.go@@ -21,7 +21,11 @@ type ModelSourceTarget struct {// ModelSourceError is a refusal with the stable code the Computer or Agent// Service reported, such as BYOK_AUTH_FAILED or RUNTIME_NOT_ENABLED.-type ModelSourceError struct{ Code string }+type ModelSourceError struct {+ Code string+ // Message is the Computer's own display text for the refusal.+ Message string+}func (e *ModelSourceError) Error() string { return "model source refused: " + e.Code }@@ -115,7 +119,7 @@ func modelSourceGatewayError(status int, body *nodeservicesinternal.ErrorResponscase body != nil && body.Error.Code == "LOCAL_COMPUTER_OFFLINE":return ErrLocalComputerOfflinecase body != nil && body.Error.Code != "":- return &ModelSourceError{Code: body.Error.Code}+ return &ModelSourceError{Code: body.Error.Code, Message: body.Error.Message}}return fmt.Errorf("AgentManagement returned status %d for a model source command", status)}diff --git a/apps/teamhub/internal/roomagent/model_source_test.go b/apps/teamhub/internal/roomagent/model_source_test.goindex 0bc68c1e..122c4957 100644--- a/apps/teamhub/internal/roomagent/model_source_test.go+++ b/apps/teamhub/internal/roomagent/model_source_test.go@@ -65,7 +65,7 @@ func TestHTTPGatewayAppliesModelSourceAndMapsRefusals(t *testing.T) {var refusal *ModelSourceErrorswitch {case test.want != nil && !errors.Is(err, test.want),- test.code != "" && (!errors.As(err, &refusal) || refusal.Code != test.code),+ test.code != "" && (!errors.As(err, &refusal) || refusal.Code != test.code || refusal.Message != "no"),test.want == nil && test.code == "" && (err == nil || errors.As(err, &refusal)):t.Fatalf("status %d: err=%v", test.status, err)}diff --git a/apps/teamhub/tests/integration/local_computer_model_source_test.go b/apps/teamhub/tests/integration/local_computer_model_source_test.goindex f74c3969..161176f1 100644--- a/apps/teamhub/tests/integration/local_computer_model_source_test.go+++ b/apps/teamhub/tests/integration/local_computer_model_source_test.go@@ -51,7 +51,8 @@ func (c *modelSourceComputers) TestLocalComputerByok(_ context.Context, _ roomagc.tested = append(c.tested, request)input := request.Byokif input.ApiKey == nil || *input.ApiKey != modelSourceTestAPIKey {- return nodeservicesinternal.ByokTestResult{}, &roomagent.ModelSourceError{Code: "BYOK_AUTH_FAILED"}+ return nodeservicesinternal.ByokTestResult{}, &roomagent.ModelSourceError{Code: "BYOK_AUTH_FAILED",+ Message: input.BaseUrl + input.Path + input.Endpoint + " rejected the API key (HTTP 401)."}}return nodeservicesinternal.ByokTestResult{Ok: true, Protocol: input.Protocol, Url: input.BaseUrl + input.Path + input.Endpoint, ModelId: input.ModelId, ContextWindow: 200000}, nil}@@ -175,7 +176,8 @@ func TestLocalComputerModelSourceSwitchMovesAgentsAndEndsChosenWork(t *testing.Tt.Fatalf("BYOK test with a path that is not a path: %d %s", status, body)}status, _, body = env.request(alice, http.MethodPost, base+"/test", map[string]any{"byok": map[string]any{"base_url": "https://api.example.com", "path": "/v1", "endpoint": "/messages", "protocol": "anthropic", "api_key": "wrong", "model_id": "vendor/model"}}, "")- if status != http.StatusConflict || !containsJSONText(body, "BYOK_AUTH_FAILED") {+ if status != http.StatusConflict || !containsJSONText(body, "BYOK_AUTH_FAILED") ||+ !containsJSONText(body, "https://api.example.com/v1/messages rejected the API key (HTTP 401).") {t.Fatalf("BYOK test with a wrong key: %d %s", status, body)}status, _, body = env.request(alice, http.MethodPost, base+"/models", map[string]any{"base_url": "https://api.example.com", "path": "/v1", "endpoint": "/chat/completions", "protocol": "openai-chat", "api_key": modelSourceTestAPIKey}, "")diff --git a/contracts/generated/go/teamhubpublic/openapi.gen.go b/contracts/generated/go/teamhubpublic/openapi.gen.goindex 72a55324..1fa92851 100644--- a/contracts/generated/go/teamhubpublic/openapi.gen.go+++ b/contracts/generated/go/teamhubpublic/openapi.gen.go@@ -7348,7 +7348,7 @@ type MessageThreadSummaryUpdatedPayload struct {// ModelSourceError The common error body with error.code narrowed to ModelSourceErrorCode.type ModelSourceError struct {- // Code error.code of a refused model-source request (403, 409, 502, 503, 504); 400, 401, and 404 use the common codes. LOCAL_COMPUTER_OFFLINE: the Computer is not connected. MODEL_SOURCE_FORBIDDEN: the viewer is not the Computer owner. MODEL_SOURCE_UNAVAILABLE: the Computer has no localmodels tool or cannot switch model sources. AGENT_HAS_ACTIVE_RUNS: Agents on the Computer have running work and force_terminate was false; details carries one entry per such Agent, with field "agent" and the Agent's display name as reason. RUNTIME_NOT_ENABLED, LOCAL_MODEL_NO_ROOM, LOCAL_MODEL_UNKNOWN, and the BYOK_ codes are the Computer's refusals (409). MODEL_SOURCE_FAILED: the Computer failed with an error that has no code of its own here (502). MODEL_SOURCE_SERVICE_UNAVAILABLE: Agent Service could not be reached (503). MODEL_SOURCE_UNCONFIRMED: the Computer had not answered by the deadline; it may still switch (504).+ // Code error.code of a refused model-source request (403, 409, 502, 503, 504); 400, 401, and 404 use the common codes. LOCAL_COMPUTER_OFFLINE: the Computer is not connected. MODEL_SOURCE_FORBIDDEN: the viewer is not the Computer owner. MODEL_SOURCE_UNAVAILABLE: the Computer has no localmodels tool or cannot switch model sources. AGENT_HAS_ACTIVE_RUNS: Agents on the Computer have running work and force_terminate was false; details carries one entry per such Agent, with field "agent" and the Agent's display name as reason. RUNTIME_NOT_ENABLED, LOCAL_MODEL_NO_ROOM, LOCAL_MODEL_UNKNOWN, and the BYOK_ codes are the Computer's refusals (409); for BYOK_AUTH_FAILED, BYOK_NOT_FOUND, BYOK_UNREACHABLE and BYOK_UPSTREAM_REJECTED, message is the Computer's own and names the address it sent to and the endpoint's HTTP status. MODEL_SOURCE_FAILED: the Computer failed with an error that has no code of its own here (502). MODEL_SOURCE_SERVICE_UNAVAILABLE: Agent Service could not be reached (503). MODEL_SOURCE_UNCONFIRMED: the Computer had not answered by the deadline; it may still switch (504).Code ModelSourceErrorCode `json:"code"`Details *[]ErrorDetail `json:"details,omitempty"`@@ -7357,7 +7357,7 @@ type ModelSourceError struct {RequestId string `json:"request_id"`}-// ModelSourceErrorCode error.code of a refused model-source request (403, 409, 502, 503, 504); 400, 401, and 404 use the common codes. LOCAL_COMPUTER_OFFLINE: the Computer is not connected. MODEL_SOURCE_FORBIDDEN: the viewer is not the Computer owner. MODEL_SOURCE_UNAVAILABLE: the Computer has no localmodels tool or cannot switch model sources. AGENT_HAS_ACTIVE_RUNS: Agents on the Computer have running work and force_terminate was false; details carries one entry per such Agent, with field "agent" and the Agent's display name as reason. RUNTIME_NOT_ENABLED, LOCAL_MODEL_NO_ROOM, LOCAL_MODEL_UNKNOWN, and the BYOK_ codes are the Computer's refusals (409). MODEL_SOURCE_FAILED: the Computer failed with an error that has no code of its own here (502). MODEL_SOURCE_SERVICE_UNAVAILABLE: Agent Service could not be reached (503). MODEL_SOURCE_UNCONFIRMED: the Computer had not answered by the deadline; it may still switch (504).+// ModelSourceErrorCode error.code of a refused model-source request (403, 409, 502, 503, 504); 400, 401, and 404 use the common codes. LOCAL_COMPUTER_OFFLINE: the Computer is not connected. MODEL_SOURCE_FORBIDDEN: the viewer is not the Computer owner. MODEL_SOURCE_UNAVAILABLE: the Computer has no localmodels tool or cannot switch model sources. AGENT_HAS_ACTIVE_RUNS: Agents on the Computer have running work and force_terminate was false; details carries one entry per such Agent, with field "agent" and the Agent's display name as reason. RUNTIME_NOT_ENABLED, LOCAL_MODEL_NO_ROOM, LOCAL_MODEL_UNKNOWN, and the BYOK_ codes are the Computer's refusals (409); for BYOK_AUTH_FAILED, BYOK_NOT_FOUND, BYOK_UNREACHABLE and BYOK_UPSTREAM_REJECTED, message is the Computer's own and names the address it sent to and the endpoint's HTTP status. MODEL_SOURCE_FAILED: the Computer failed with an error that has no code of its own here (502). MODEL_SOURCE_SERVICE_UNAVAILABLE: Agent Service could not be reached (503). MODEL_SOURCE_UNCONFIRMED: the Computer had not answered by the deadline; it may still switch (504).type ModelSourceErrorCode string// ModelSourceErrorResponse defines model for ModelSourceErrorResponse.diff --git a/contracts/generated/typescript/src/generated/teamhub-public/types.ts b/contracts/generated/typescript/src/generated/teamhub-public/types.tsindex 7328f60e..f0fbb7ce 100644--- a/contracts/generated/typescript/src/generated/teamhub-public/types.ts+++ b/contracts/generated/typescript/src/generated/teamhub-public/types.ts@@ -4906,7 +4906,7 @@ export interface components {terminated_run_count?: number;};/**- * @description error.code of a refused model-source request (403, 409, 502, 503, 504); 400, 401, and 404 use the common codes. LOCAL_COMPUTER_OFFLINE: the Computer is not connected. MODEL_SOURCE_FORBIDDEN: the viewer is not the Computer owner. MODEL_SOURCE_UNAVAILABLE: the Computer has no localmodels tool or cannot switch model sources. AGENT_HAS_ACTIVE_RUNS: Agents on the Computer have running work and force_terminate was false; details carries one entry per such Agent, with field "agent" and the Agent's display name as reason. RUNTIME_NOT_ENABLED, LOCAL_MODEL_NO_ROOM, LOCAL_MODEL_UNKNOWN, and the BYOK_ codes are the Computer's refusals (409). MODEL_SOURCE_FAILED: the Computer failed with an error that has no code of its own here (502). MODEL_SOURCE_SERVICE_UNAVAILABLE: Agent Service could not be reached (503). MODEL_SOURCE_UNCONFIRMED: the Computer had not answered by the deadline; it may still switch (504).+ * @description error.code of a refused model-source request (403, 409, 502, 503, 504); 400, 401, and 404 use the common codes. LOCAL_COMPUTER_OFFLINE: the Computer is not connected. MODEL_SOURCE_FORBIDDEN: the viewer is not the Computer owner. MODEL_SOURCE_UNAVAILABLE: the Computer has no localmodels tool or cannot switch model sources. AGENT_HAS_ACTIVE_RUNS: Agents on the Computer have running work and force_terminate was false; details carries one entry per such Agent, with field "agent" and the Agent's display name as reason. RUNTIME_NOT_ENABLED, LOCAL_MODEL_NO_ROOM, LOCAL_MODEL_UNKNOWN, and the BYOK_ codes are the Computer's refusals (409); for BYOK_AUTH_FAILED, BYOK_NOT_FOUND, BYOK_UNREACHABLE and BYOK_UPSTREAM_REJECTED, message is the Computer's own and names the address it sent to and the endpoint's HTTP status. MODEL_SOURCE_FAILED: the Computer failed with an error that has no code of its own here (502). MODEL_SOURCE_SERVICE_UNAVAILABLE: Agent Service could not be reached (503). MODEL_SOURCE_UNCONFIRMED: the Computer had not answered by the deadline; it may still switch (504).* @enum {string}*/ModelSourceErrorCode:diff --git a/contracts/openapi/components/model-source.yaml b/contracts/openapi/components/model-source.yamlindex 0afcc1ce..de8e843c 100644--- a/contracts/openapi/components/model-source.yaml+++ b/contracts/openapi/components/model-source.yaml@@ -290,7 +290,10 @@ components:false; details carries one entry per such Agent, with field "agent"and the Agent's display name as reason. RUNTIME_NOT_ENABLED,LOCAL_MODEL_NO_ROOM, LOCAL_MODEL_UNKNOWN, and the BYOK_ codes are the- Computer's refusals (409). MODEL_SOURCE_FAILED: the Computer failed+ Computer's refusals (409); for BYOK_AUTH_FAILED, BYOK_NOT_FOUND,+ BYOK_UNREACHABLE and BYOK_UPSTREAM_REJECTED, message is the Computer's+ own and names the address it sent to and the endpoint's HTTP status.+ MODEL_SOURCE_FAILED: the Computer failedwith an error that has no code of its own here (502).MODEL_SOURCE_SERVICE_UNAVAILABLE: Agent Service could not be reached(503). MODEL_SOURCE_UNCONFIRMED: the Computer had not answered by the