Files
dingtalk-workspace-cli/internal/helpers/connect_command_test.go
修雨 107f3eaf6e feat(connect): add /new /clear slash commands and persist codex session context
Two connector improvements for the linked-bot chat experience, modelled on
codex / OpenClaw connect:

1. Built-in slash commands. A user can type /new (aliases /start, /reset) to
   open a fresh session or /clear to wipe the current one, instead of the
   message being forwarded to the agent. The command set is fixed (never
   dynamically extended) so behaviour stays predictable. Matching requires the
   whole message to be the bare command, so a normal question that merely starts
   with a slash is forwarded untouched. Costs no agent turn / tokens.

   - connect_command.go: channel-agnostic parser + action table (+ tests).
   - sessionResetter optional interface; execForwarder (Claude family) and the
     codex app-server forwarder both implement it. The main loop intercepts a
     command right after the owner-decision interceptor and acks via webhook.

2. Persist codex per-conversation context. codexThreadSessions previously kept
   the convID->threadID map in memory only, so a codex connector restart lost
   every conversation's context — unlike the Claude-family convSessions store
   which already persisted. The thread identity is now the authoritative,
   mutex-guarded map persisted to <config>/connect/<clientId>/codex-threads.json
   (best-effort, atomic write), restored on startup. An empty clientId keeps it
   in memory only, preserving the original behaviour. This also removes a data
   race on threadID by moving it off the per-conversation state into the
   sessions store.
2026-06-24 10:30:04 +08:00

68 lines
2.0 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 helpers
import "testing"
func TestParseConnectControlCommand(t *testing.T) {
cases := []struct {
in string
wantName string // "" means no match
}{
// Recognised commands and their aliases.
{"/new", "new"},
{"/start", "new"},
{"/reset", "new"},
{"/clear", "clear"},
// Case-insensitive and trimmed.
{"/NEW", "new"},
{" /Clear ", "clear"},
{"/New\n", "new"},
// A normal question that merely starts with a slash is NOT a command —
// it must be forwarded to the agent, not swallowed.
{"/new 这个功能怎么实现?", ""},
{"/clear the cache please", ""},
{"/new x", ""},
// Non-commands.
{"", ""},
{" ", ""},
{"你好", ""},
{"new", ""}, // missing slash
{"/unknown", ""}, // unknown token
{"/", ""},
}
for _, c := range cases {
got, ok := parseConnectControlCommand(c.in)
if c.wantName == "" {
if ok {
t.Errorf("parseConnectControlCommand(%q) = (%+v, true), want no match", c.in, got)
}
continue
}
if !ok {
t.Errorf("parseConnectControlCommand(%q) = no match, want name %q", c.in, c.wantName)
continue
}
if got.name != c.wantName {
t.Errorf("parseConnectControlCommand(%q).name = %q, want %q", c.in, got.name, c.wantName)
}
if got.ack == "" {
t.Errorf("parseConnectControlCommand(%q).ack is empty, want a user-facing confirmation", c.in)
}
if !got.resetsSession() {
t.Errorf("parseConnectControlCommand(%q).resetsSession() = false, want true", c.in)
}
}
}