step 5 (partial): strip dropped packages from production code
Removed all references to dropped packages in non-test code.
Zero dropped imports, store refs, or model types remaining.
Major removals:
- scheduler/ package (entire dir) — tasks moved to extension track
- taskutil/ package (entire dir)
- health/ package (entire dir) — provider/tool health
- sandbox/provider_module.go — provider.complete module
- handlers: workflow_entry, workflow_instances, workflow_forms,
workflow_assignments, workflow_monitor, health_admin (6 files)
- auth/session.go, middleware/session_auth.go
- store/{postgres,sqlite}/sessions.go, tasks.go
Rewrites:
- store/{postgres,sqlite}/health.go — kernel-only Prune
(ws_tickets, rate_limit_counters, stale presence)
- handlers/admin.go: 847→467 lines (stripped provider CRUD)
- handlers/teams.go: 520→411 lines (stripped model listing)
- sandbox/runner.go: removed ProviderResolver
- sandbox/workflow_module.go: 216→83 lines (definition-only)
- main.go: 1579→1325 lines (stripped dropped package init)
- pages/pages.go: stripped channel/session lookups
- events/types.go: stripped chat/channel/workspace event routes
- models: stripped stale types, constants, notification types
Store interface cleanup:
- Removed SessionStore, TaskStore from Stores struct
- Stripped workflow assignment methods from WorkflowStore iface
- Stripped assignment methods from PG+SQLite workflow stores
- Updated testhelper.go table list to kernel-only
Migrations: removed 008_tasks.sql (both dialects)
-9665/+69 lines across 51 files.
This commit is contained in:
@@ -1,241 +0,0 @@
|
||||
// Package sandbox — provider_module.go
|
||||
//
|
||||
// v0.29.1 CS2: Provider module for Starlark extensions.
|
||||
// Requires permission: provider.complete
|
||||
//
|
||||
// Starlark API:
|
||||
//
|
||||
// resp = provider.complete(
|
||||
// messages=[{"role": "user", "content": "Hello"}],
|
||||
// model="claude-3-haiku", # optional — uses default from BYOK chain
|
||||
// max_tokens=1024, # optional — default 4096
|
||||
// temperature=0.7, # optional — provider default
|
||||
// )
|
||||
//
|
||||
// # resp = {
|
||||
// # "content": "Hi there!",
|
||||
// # "model": "claude-3-haiku-20240307",
|
||||
// # "finish_reason": "stop",
|
||||
// # "input_tokens": 10,
|
||||
// # "output_tokens": 15,
|
||||
// # }
|
||||
//
|
||||
// Provider resolution uses the existing BYOK chain via the
|
||||
// ProviderResolver interface (implemented by handlers package).
|
||||
// This avoids a circular dependency (sandbox → handlers → sandbox).
|
||||
package sandbox
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"go.starlark.net/starlark"
|
||||
"go.starlark.net/starlarkstruct"
|
||||
|
||||
"switchboard-core/providers"
|
||||
)
|
||||
|
||||
// ─── Provider Resolution Interface ──────────
|
||||
|
||||
// ProviderResolution holds the resolved provider and config.
|
||||
// Mirrors handlers.ProviderResolution without importing it.
|
||||
type ProviderResolution struct {
|
||||
Provider providers.Provider
|
||||
Config providers.ProviderConfig
|
||||
ProviderID string
|
||||
Model string
|
||||
ConfigID string
|
||||
}
|
||||
|
||||
// ProviderResolver resolves a provider configuration from the BYOK chain.
|
||||
// Implemented by handlers via a thin adapter (set on Runner at startup).
|
||||
type ProviderResolver interface {
|
||||
Resolve(ctx context.Context, userID, channelID, providerConfigID, model string) (*ProviderResolution, error)
|
||||
}
|
||||
|
||||
// ─── Configuration ──────────────────────────
|
||||
|
||||
const providerDefaultMaxTokens = 4096
|
||||
|
||||
// ProviderModuleConfig holds per-package provider settings parsed from
|
||||
// the manifest's requires_provider field.
|
||||
type ProviderModuleConfig struct {
|
||||
// ProviderConfigID pins the extension to a specific provider config.
|
||||
// Empty string means use the BYOK resolution chain.
|
||||
ProviderConfigID string
|
||||
|
||||
// DefaultModel is the model to use when the script doesn't specify one.
|
||||
DefaultModel string
|
||||
}
|
||||
|
||||
// ParseRequiresProvider extracts ProviderModuleConfig from a manifest.
|
||||
//
|
||||
// Accepted formats:
|
||||
//
|
||||
// true → empty config (BYOK default)
|
||||
// {"model": "claude-3-haiku"} → default model
|
||||
// {"provider_config_id": "uuid", ...} → pinned provider
|
||||
func ParseRequiresProvider(manifest map[string]any) (ProviderModuleConfig, bool) {
|
||||
raw, ok := manifest["requires_provider"]
|
||||
if !ok {
|
||||
return ProviderModuleConfig{}, false
|
||||
}
|
||||
|
||||
// Boolean shorthand
|
||||
if b, ok := raw.(bool); ok {
|
||||
return ProviderModuleConfig{}, b
|
||||
}
|
||||
|
||||
// Object form
|
||||
m, ok := raw.(map[string]any)
|
||||
if !ok {
|
||||
return ProviderModuleConfig{}, false
|
||||
}
|
||||
|
||||
cfg := ProviderModuleConfig{}
|
||||
if v, ok := m["provider_config_id"].(string); ok {
|
||||
cfg.ProviderConfigID = v
|
||||
}
|
||||
if v, ok := m["model"].(string); ok {
|
||||
cfg.DefaultModel = v
|
||||
}
|
||||
|
||||
return cfg, true
|
||||
}
|
||||
|
||||
// ─── Module Builder ─────────────────────────
|
||||
|
||||
// BuildProviderModule creates the "provider" Starlark module. Each call
|
||||
// to provider.complete() resolves a provider via the BYOK chain and
|
||||
// makes a synchronous (non-streaming) LLM completion call.
|
||||
func BuildProviderModule(
|
||||
ctx context.Context,
|
||||
resolver ProviderResolver,
|
||||
userID string,
|
||||
manifestCfg ProviderModuleConfig,
|
||||
) *starlarkstruct.Module {
|
||||
return MakeModule("provider", starlark.StringDict{
|
||||
"complete": starlark.NewBuiltin("provider.complete", func(
|
||||
thread *starlark.Thread, b *starlark.Builtin,
|
||||
args starlark.Tuple, kwargs []starlark.Tuple,
|
||||
) (starlark.Value, error) {
|
||||
var messagesList *starlark.List
|
||||
var model string
|
||||
var maxTokens int = providerDefaultMaxTokens
|
||||
var temperature starlark.Value = starlark.None
|
||||
|
||||
if err := starlark.UnpackArgs(b.Name(), args, kwargs,
|
||||
"messages", &messagesList,
|
||||
"model?", &model,
|
||||
"max_tokens?", &maxTokens,
|
||||
"temperature?", &temperature,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Apply default model from manifest
|
||||
if model == "" {
|
||||
model = manifestCfg.DefaultModel
|
||||
}
|
||||
|
||||
// Convert Starlark messages to provider messages
|
||||
msgs, err := starlarkToMessages(messagesList)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("provider.complete: %w", err)
|
||||
}
|
||||
if len(msgs) == 0 {
|
||||
return nil, fmt.Errorf("provider.complete: messages list is empty")
|
||||
}
|
||||
|
||||
// Resolve provider via BYOK chain
|
||||
res, err := resolver.Resolve(ctx, userID, "",
|
||||
manifestCfg.ProviderConfigID, model)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("provider.complete: %w", err)
|
||||
}
|
||||
|
||||
// Build request
|
||||
req := providers.CompletionRequest{
|
||||
Model: res.Model,
|
||||
Messages: msgs,
|
||||
MaxTokens: maxTokens,
|
||||
Stream: false,
|
||||
}
|
||||
|
||||
// Set temperature if provided
|
||||
if temperature != starlark.None {
|
||||
if f, ok := starlark.AsFloat(temperature); ok {
|
||||
req.Temperature = &f
|
||||
}
|
||||
}
|
||||
|
||||
// Make synchronous completion call
|
||||
resp, err := res.Provider.ChatCompletion(ctx, res.Config, req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("provider.complete: LLM call failed: %w", err)
|
||||
}
|
||||
|
||||
return completionToStarlark(resp)
|
||||
}),
|
||||
})
|
||||
}
|
||||
|
||||
// ─── Message Conversion ─────────────────────
|
||||
|
||||
// starlarkToMessages converts a Starlark list of dicts to provider Messages.
|
||||
// Each dict must have "role" (string) and "content" (string).
|
||||
func starlarkToMessages(list *starlark.List) ([]providers.Message, error) {
|
||||
if list == nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
msgs := make([]providers.Message, 0, list.Len())
|
||||
iter := list.Iterate()
|
||||
defer iter.Done()
|
||||
|
||||
var item starlark.Value
|
||||
for iter.Next(&item) {
|
||||
dict, ok := item.(*starlark.Dict)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("expected dict in messages list, got %s", item.Type())
|
||||
}
|
||||
|
||||
roleVal, found, _ := dict.Get(starlark.String("role"))
|
||||
if !found {
|
||||
return nil, fmt.Errorf("message dict missing 'role' key")
|
||||
}
|
||||
role, ok := starlark.AsString(roleVal)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("message 'role' must be a string")
|
||||
}
|
||||
|
||||
contentVal, found, _ := dict.Get(starlark.String("content"))
|
||||
if !found {
|
||||
return nil, fmt.Errorf("message dict missing 'content' key")
|
||||
}
|
||||
content, ok := starlark.AsString(contentVal)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("message 'content' must be a string")
|
||||
}
|
||||
|
||||
msgs = append(msgs, providers.Message{
|
||||
Role: role,
|
||||
Content: content,
|
||||
})
|
||||
}
|
||||
|
||||
return msgs, nil
|
||||
}
|
||||
|
||||
// ─── Response Conversion ────────────────────
|
||||
|
||||
// completionToStarlark converts a provider CompletionResponse to a Starlark dict.
|
||||
func completionToStarlark(resp *providers.CompletionResponse) (starlark.Value, error) {
|
||||
d := starlark.NewDict(5)
|
||||
_ = d.SetKey(starlark.String("content"), starlark.String(resp.Content))
|
||||
_ = d.SetKey(starlark.String("model"), starlark.String(resp.Model))
|
||||
_ = d.SetKey(starlark.String("finish_reason"), starlark.String(resp.FinishReason))
|
||||
_ = d.SetKey(starlark.String("input_tokens"), starlark.MakeInt(resp.InputTokens))
|
||||
_ = d.SetKey(starlark.String("output_tokens"), starlark.MakeInt(resp.OutputTokens))
|
||||
return d, nil
|
||||
}
|
||||
@@ -44,8 +44,7 @@ type RunContext struct {
|
||||
// UserID is the acting user for provider resolution (BYOK chain).
|
||||
UserID string
|
||||
|
||||
// ChannelID is the channel context, if any. Used for provider
|
||||
// resolution when a channel has a pinned provider config.
|
||||
// ChannelID is the context identifier, if any.
|
||||
ChannelID string
|
||||
}
|
||||
|
||||
@@ -55,7 +54,6 @@ type Runner struct {
|
||||
stores store.Stores
|
||||
packagesDir string // v0.38.0: disk path for load() support
|
||||
notifier NotificationSender // nil = notifications module unavailable
|
||||
resolver ProviderResolver // nil = provider module unavailable
|
||||
connResolver ConnectionResolver // nil = connections module unavailable (v0.38.1)
|
||||
db *sql.DB // nil = db module unavailable
|
||||
dbPostgres bool // true = use $N placeholders; false = use ?
|
||||
@@ -75,11 +73,6 @@ func (r *Runner) SetNotifier(n NotificationSender) {
|
||||
r.notifier = n
|
||||
}
|
||||
|
||||
// SetProviderResolver attaches the provider resolver for the provider.complete module.
|
||||
func (r *Runner) SetProviderResolver(pr ProviderResolver) {
|
||||
r.resolver = pr
|
||||
}
|
||||
|
||||
// SetConnectionResolver attaches the connection resolver for the connections module (v0.38.1).
|
||||
func (r *Runner) SetConnectionResolver(cr ConnectionResolver) {
|
||||
r.connResolver = cr
|
||||
@@ -315,11 +308,6 @@ func (r *Runner) buildModulesWithLibCtx(ctx context.Context, packageID string, m
|
||||
httpCfg.AllowPrivateIPs = r.allowPrivateIPs
|
||||
modules["http"] = BuildHTTPModule(ctx, httpCfg)
|
||||
|
||||
case models.ExtPermProviderComplete:
|
||||
if r.resolver != nil && rc != nil && rc.UserID != "" {
|
||||
provCfg, ok := ParseRequiresProvider(manifest)
|
||||
if ok {
|
||||
modules["provider"] = BuildProviderModule(ctx, r.resolver, rc.UserID, provCfg)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -28,10 +28,6 @@ import (
|
||||
func BuildWorkflowModule(ctx context.Context, stores store.Stores) *starlarkstruct.Module {
|
||||
return MakeModule("workflow", starlark.StringDict{
|
||||
"get_definition": starlark.NewBuiltin("workflow.get_definition", workflowGetDef(ctx, stores)),
|
||||
"get_stage_data": starlark.NewBuiltin("workflow.get_stage_data", workflowGetStageData(ctx, stores)),
|
||||
"advance": starlark.NewBuiltin("workflow.advance", workflowAdvance(ctx, stores)),
|
||||
"reject": starlark.NewBuiltin("workflow.reject", workflowReject(ctx, stores)),
|
||||
"route": starlark.NewBuiltin("workflow.route", workflowRoute(ctx, stores)),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -82,135 +78,5 @@ func workflowGetDef(ctx context.Context, stores store.Stores) func(*starlark.Thr
|
||||
}
|
||||
}
|
||||
|
||||
func workflowGetStageData(ctx context.Context, stores store.Stores) func(*starlark.Thread, *starlark.Builtin, starlark.Tuple, []starlark.Tuple) (starlark.Value, error) {
|
||||
return func(_ *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
|
||||
var channelID string
|
||||
if err := starlark.UnpackPositionalArgs("workflow.get_stage_data", args, kwargs, 1, &channelID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ws, err := stores.Channels.GetWorkflowStatus(ctx, channelID)
|
||||
if err != nil || ws == nil {
|
||||
return starlark.None, fmt.Errorf("workflow.get_stage_data: channel not found or not a workflow")
|
||||
}
|
||||
|
||||
result := starlark.NewDict(8)
|
||||
result.SetKey(starlark.String("current_stage"), starlark.MakeInt(ws.CurrentStage))
|
||||
result.SetKey(starlark.String("status"), starlark.String(ws.Status))
|
||||
|
||||
if ws.StageData != nil {
|
||||
var data map[string]any
|
||||
if json.Unmarshal(ws.StageData, &data) == nil {
|
||||
for k, v := range data {
|
||||
sv, err := goToStarlark(v)
|
||||
if err == nil {
|
||||
result.SetKey(starlark.String(k), sv)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
}
|
||||
|
||||
func workflowAdvance(ctx context.Context, stores store.Stores) func(*starlark.Thread, *starlark.Builtin, starlark.Tuple, []starlark.Tuple) (starlark.Value, error) {
|
||||
return func(_ *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
|
||||
var channelID string
|
||||
if err := starlark.UnpackPositionalArgs("workflow.advance", args, kwargs, 1, &channelID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ws, err := stores.Channels.GetWorkflowStatus(ctx, channelID)
|
||||
if err != nil || ws == nil || ws.WorkflowID == nil {
|
||||
return starlark.None, fmt.Errorf("workflow.advance: channel not found or not a workflow")
|
||||
}
|
||||
|
||||
stages, err := stores.Workflows.ListStages(ctx, *ws.WorkflowID)
|
||||
if err != nil {
|
||||
return starlark.None, fmt.Errorf("workflow.advance: %w", err)
|
||||
}
|
||||
|
||||
nextStage, err := workflow.ResolveNextStage(stages, ws.CurrentStage, ws.StageData)
|
||||
if err != nil {
|
||||
return starlark.None, fmt.Errorf("workflow.advance: routing error: %w", err)
|
||||
}
|
||||
if nextStage >= len(stages) {
|
||||
// Complete the workflow
|
||||
if err := stores.Channels.CompleteWorkflow(ctx, channelID, ws.CurrentStage, ws.StageData); err != nil {
|
||||
return starlark.None, fmt.Errorf("workflow.advance: %w", err)
|
||||
}
|
||||
return starlark.String("completed"), nil
|
||||
}
|
||||
|
||||
if err := stores.Channels.AdvanceWorkflowStage(ctx, channelID, nextStage, ws.StageData); err != nil {
|
||||
return starlark.None, fmt.Errorf("workflow.advance: %w", err)
|
||||
}
|
||||
return starlark.String("advanced"), nil
|
||||
}
|
||||
}
|
||||
|
||||
func workflowReject(ctx context.Context, stores store.Stores) func(*starlark.Thread, *starlark.Builtin, starlark.Tuple, []starlark.Tuple) (starlark.Value, error) {
|
||||
return func(_ *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
|
||||
var channelID, reason string
|
||||
if err := starlark.UnpackPositionalArgs("workflow.reject", args, kwargs, 2, &channelID, &reason); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ws, err := stores.Channels.GetWorkflowStatus(ctx, channelID)
|
||||
if err != nil || ws == nil {
|
||||
return starlark.None, fmt.Errorf("workflow.reject: channel not found or not a workflow")
|
||||
}
|
||||
|
||||
if ws.CurrentStage <= 0 {
|
||||
return starlark.None, fmt.Errorf("workflow.reject: already at stage 0")
|
||||
}
|
||||
|
||||
prevStage := ws.CurrentStage - 1
|
||||
if err := stores.Channels.RejectWorkflowToStage(ctx, channelID, prevStage); err != nil {
|
||||
return starlark.None, fmt.Errorf("workflow.reject: %w", err)
|
||||
}
|
||||
return starlark.String("rejected"), nil
|
||||
}
|
||||
}
|
||||
|
||||
// workflowRoute routes the workflow to a named stage (v0.35.0).
|
||||
// Starlark: workflow.route(channel_id, target_stage, reason)
|
||||
func workflowRoute(ctx context.Context, stores store.Stores) func(*starlark.Thread, *starlark.Builtin, starlark.Tuple, []starlark.Tuple) (starlark.Value, error) {
|
||||
return func(_ *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
|
||||
var channelID, targetStage, reason string
|
||||
if err := starlark.UnpackPositionalArgs("workflow.route", args, kwargs, 3, &channelID, &targetStage, &reason); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ws, err := stores.Channels.GetWorkflowStatus(ctx, channelID)
|
||||
if err != nil || ws == nil || ws.WorkflowID == nil {
|
||||
return starlark.None, fmt.Errorf("workflow.route: channel not found or not a workflow")
|
||||
}
|
||||
if ws.Status != "active" {
|
||||
return starlark.None, fmt.Errorf("workflow.route: workflow is %s", ws.Status)
|
||||
}
|
||||
|
||||
stages, err := stores.Workflows.ListStages(ctx, *ws.WorkflowID)
|
||||
if err != nil {
|
||||
return starlark.None, fmt.Errorf("workflow.route: %w", err)
|
||||
}
|
||||
|
||||
targetOrdinal, err := workflow.ResolveStageByName(stages, targetStage)
|
||||
if err != nil {
|
||||
return starlark.None, fmt.Errorf("workflow.route: %w", err)
|
||||
}
|
||||
|
||||
if targetOrdinal >= len(stages) {
|
||||
if err := stores.Channels.CompleteWorkflow(ctx, channelID, targetOrdinal, ws.StageData); err != nil {
|
||||
return starlark.None, fmt.Errorf("workflow.route: %w", err)
|
||||
}
|
||||
return starlark.String("completed"), nil
|
||||
}
|
||||
|
||||
if err := stores.Channels.AdvanceWorkflowStage(ctx, channelID, targetOrdinal, ws.StageData); err != nil {
|
||||
return starlark.None, fmt.Errorf("workflow.route: %w", err)
|
||||
}
|
||||
return starlark.String("routed"), nil
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user