Files
dingtalk-workspace-cli/pkg/edition/edition.go
aa6abc5ed6 feat(event): add personal event subscriptions (#589)
* dws event

* fix consume fail

* test: add stream ticket injection probe

* feat: add portal ticket stream mode

* user event

* fix: allow portal ticket normal without app secret

* event

* user event

* eventType filter

* refactor(event): 优化IPC端点路径处理和改进相关测试

- 用dwsevent.IPCEndpoint替代原先根据GOOS判断的路径逻辑
- 新增event包实现Unix socket路径长度限制及长路径fallback机制
- 添加endpoint_test.go覆盖路径短长及唯一性的单元测试
- 修改busctl模块使用统一的IPC端点获取方法,避免重复实现
- transport_unix.go新增checkSocketPath函数检查路径长度,防止EINVAL错误
- 在监听和连接Unix socket时加入路径限制检查,提升错误明晰度
- 去除多个文件中无用的runtime导入,简化代码依赖

* opt

* event skill

* default value

* install script event

* fix: remove subscribe id event fanout filter

* fix(personal): 修正指定发送人消息描述错误

* more im event

* filter subId

* fix: align personal event schema with stream payload

* fix: avoid duplicate app helper name

* feat: simplify personal event schemas

* feat: simplify event schema output

* docs: refine dingtalk event skill references

* feat: align personal event consume flags

* fix event stop and status visibility

* hide app event public entrypoints

* hide incomplete personal sender event

* remove external event reference comments

* chore(event): prepare official release

* fix(event): harden personal stream lifecycle

---------

Co-authored-by: 玉澜 <yulan.wqy@alibaba-inc.com>
Co-authored-by: zhengyubai <zhengyubai618@gmail.com>
2026-07-10 17:54:43 +08:00

206 lines
7.9 KiB
Go

// Copyright 2026 Alibaba Group
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package edition provides an extension-point mechanism that allows private
// overlay modules (e.g. an internal distribution) to customise CLI behaviour
// without modifying the open-source core. The open-source build uses the
// zero-value defaults; an overlay calls Override before Execute.
package edition
import (
"context"
"sync"
"github.com/spf13/cobra"
)
// ServerInfo describes a static MCP server endpoint.
type ServerInfo struct {
ID string
Name string
Endpoint string
Prefixes []string
}
// ContentBlock is a single content item in a ToolResult.
type ContentBlock struct {
Type string
Text string
}
// ToolResult holds the response from an MCP tool call.
type ToolResult struct {
Content []ContentBlock
}
// ToolCaller abstracts MCP tool invocation so private overlays can call MCP
// tools without importing internal packages. The open-source core provides a
// concrete adapter wrapping executor.Runner.
type ToolCaller interface {
// CallTool invokes an MCP tool by product ID and tool name.
CallTool(ctx context.Context, productID, toolName string, args map[string]any) (*ToolResult, error)
// Format returns the current output format ("json", "table", "raw").
Format() string
// DryRun returns true when --dry-run is active.
DryRun() bool
// Fields returns the global --fields output projection ("" if unset).
Fields() string
// JQ returns the global --jq output filter expression ("" if unset).
JQ() string
}
// RuntimeDefaultFn resolves a single runtimeDefault placeholder (e.g.
// "$currentUserId") to a concrete string value. Called lazily at RunE time.
// Returning (_, false) is equivalent to "not registered" and falls through
// to the next-lower default source.
type RuntimeDefaultFn func(ctx context.Context) (string, bool)
// Hooks groups all edition-specific behavioural overrides. Zero values
// fall back to open-source defaults so the struct is safe to use as-is.
type Hooks struct {
// --- identity ---
Name string // "open" (default) / overlay identifier
ScenarioCode string // injected into x-dingtalk-scenario-code header
// ClawTypeValue is the claw identity carried in message-send tool
// arguments (parameter clawType) so the IM server can render the
// "Send from AI" indicator on delivered messages. Empty → falls back
// to DefaultOSSClawType; overlays set their own value (e.g. "wukong").
ClawTypeValue string
// PersonalEventSourceID identifies the personal-event source channel
// used by dws event --as user. Empty → "open". Overlays set their own
// value through Override; open-source core never infers custom sources
// from environment/user input.
PersonalEventSourceID string
// --- runtime mode ---
IsEmbedded bool // true when running inside a host application
HideAuthLogin bool // true suppresses the "dws auth login" command
AutoPurgeToken bool // true deletes local token data on expiry
// --- paths ---
ConfigDir func() string // custom config directory; nil → ~/.dws
// --- HTTP headers ---
MergeHeaders func(base map[string]string) map[string]string
// --- EnterpriseCredential HTTP headers ---
EnterpriseCredentialHeaders func(base map[string]string) map[string]string
// --- auth ---
AuthClientID string // OAuth client ID for device-flow authorisation
AuthClientFromMCP bool // true → fetch client ID from MCP at runtime
OnAuthError func(configDir string, err error) error
TokenProvider func(ctx context.Context, fallback func() (string, error)) (string, error)
// --- token persistence (overlay-managed keychain / encrypted storage) ---
SaveToken func(configDir string, data []byte) error // persist token blob
LoadToken func(configDir string) ([]byte, error) // retrieve token blob
DeleteToken func(configDir string) error // remove persisted token
// --- MCP result classification ---
// ClassifyToolResult inspects raw MCP tool-call content and returns a typed
// error (e.g. PATError, CLIError) when the response contains a known
// gateway-auth or PAT-permission failure. nil → no special handling.
// ClassifyToolResult func(content map[string]any) error
// --- product & endpoint ---
StaticServers func() []ServerInfo // non-nil → skip Market discovery
VisibleProducts func() []string // non-nil → override help visibility
RegisterExtraCommands func(root *cobra.Command, caller ToolCaller) // register overlay-only commands
// --- discovery ---
// DiscoveryURL overrides the Market API endpoint for server list.
// Non-empty → loadDynamicCommands uses FetchServersFromURL(DiscoveryURL)
// instead of the default Market base URL. Provides edition-level isolation.
DiscoveryURL string
// DiscoveryHeaders returns HTTP headers injected into discovery requests.
// Used to authenticate edition-specific endpoints.
DiscoveryHeaders func() map[string]string
// SupplementServers returns edition-specific MCP servers NOT registered
// in any Market registry. Always merged into the endpoint map alongside
// Market/cache results, regardless of discovery success or failure.
SupplementServers func() []ServerInfo
// FallbackServers returns the full server list as a safety net when
// Market discovery + cache both fail. Results are NOT cached so the
// next startup still attempts live discovery.
FallbackServers func() []ServerInfo
// AfterPersistentPreRun runs at the end of the root PersistentPreRunE after
// global setup (OAuth flag overrides, log level, output sink). Overlays use
// this for clients that bypass the MCP runner (e.g. A2A gateway).
AfterPersistentPreRun func(cmd *cobra.Command, args []string) error
// ClassifyToolResult is called before the framework's default business-error
// detection on MCP tool results. If it returns a non-nil error, that error
// is used instead of the generic CategoryAPI business error. Editions use
// this to return custom error types with specific exit codes (e.g. PAT
// authorization errors with exit code 4).
ClassifyToolResult func(content map[string]any) error
// --- schema v3: runtime defaults ---
// RuntimeDefaults returns resolvers for runtimeDefault placeholders (e.g.
// "$currentUserId" → fn). Placeholders not in the map fall through to a
// "not registered" warning. Open-source core returns an empty map;
// overlays populate the whitelist ($currentUserId / $unionId / $corpId /
// $now / $today). See schema v3 §2.3.
RuntimeDefaults func() map[string]RuntimeDefaultFn
}
var (
mu sync.RWMutex
current = defaultHooks()
)
// Get returns the active edition hooks (never nil).
func Get() *Hooks {
mu.RLock()
defer mu.RUnlock()
return current
}
// Override replaces the active edition hooks. Must be called before Execute.
func Override(h *Hooks) {
if h == nil {
return
}
mu.Lock()
defer mu.Unlock()
current = h
}
// ClawType returns the claw identity for the active edition, falling back
// to DefaultOSSClawType when the overlay does not set one. Message-send
// helpers attach this value as the clawType tool argument so the IM server
// can label delivered messages as sent via AI.
func ClawType() string {
if v := Get().ClawTypeValue; v != "" {
return v
}
return DefaultOSSClawType
}
// PersonalEventSourceID returns the source channel for user-level events.
func PersonalEventSourceID() string {
if v := Get().PersonalEventSourceID; v != "" {
return v
}
return "open"
}