Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go
26.1 KB
Raw Blame
package wecom

// strings.go — every string a WeCom user reads, in one place, selected per
// READER.
//
// The copy used to sit inline wherever it was sent: the offline and archived
// notices in replier.go, the binding prompt built by hand a few lines below
// them, the inbox card's type labels in inbox_message.go. All of it Chinese,
// which is right for our own tenant and wrong the moment anyone else installs
// the bot — and impossible to find, because "what does this bot say" was
// spread across three files.
//
// Which pack a given message uses is decided by the DESTINATION, not by the
// installation (language.go): a 1:1 gets that person's Multica profile
// language, and a room — where there is no shared profile and no member list —
// gets the deployment's own language.
//
// Slack's adapter hardcodes English and Lark's hardcodes Chinese, so there is
// no house i18n mechanism to join. This is deliberately not one either: a
// struct of strings per locale. No catalogue files, no message ids, no plural
// rules. If wecom ever needs a third language with real formatting rules,
// that is the moment to reach for a framework — not now.
//
// The zh-Hans pack is the text this adapter was already sending, character for
// character. Nothing a Chinese tenant reads changes here; the English pack is
// new.

import (
	"strings"
	"sync/atomic"

	"github.com/multica-ai/multica/server/internal/integrations/channel/engine"
)

// Locale names the language an installation's users are answered in.
type Locale string

const (
	LocaleZhHans Locale = "zh-Hans"
	LocaleEn     Locale = "en"

	// DefaultLocale is the compile-time fallback: Chinese, because WeCom
	// is a Chinese platform. It is what a deployment that says nothing gets.
	// Read deploymentLocale() rather than this — a deployment can say
	// otherwise, and a room's language is a property of the deployment, not of
	// whichever person happened to speak.
	DefaultLocale = LocaleZhHans
)

// deploymentLocaleValue is the language this server answers in when the reader
// is a room, or a person whose profile says nothing. Set once at boot from
// MULTICA_WECOM_DEFAULT_LOCALE (cmd/server/router.go) and read on every
// message, so it is an atomic rather than a plain var: -race would otherwise
// flag the boot write against the first inbound frame.
//
// It exists because the alternative answers are all worse. A hardcoded
// constant makes an English-speaking tenant's rooms Chinese with no way out
// but a rebuild. An installation-level column was tried and removed: nothing
// could write it. Borrowing the installer's personal profile language repeats,
// with a different person, the exact bug that motivated this — one member's
// setting deciding what a whole room reads. A deployment-level knob is the
// smallest thing that is actually about the deployment.
var deploymentLocaleValue atomic.Value

// SetDeploymentLocale fixes the deployment's language from a raw config string
// and returns what it resolved to, so the caller can log it. Anything
// unrecognised — including empty — leaves the current value in place: a typo in
// an env var must not silently switch a tenant's language.
//
// Deliberately NOT resolveLocale. That one reads a user's profile field, which
// the API has already validated to en / zh-Hans / ko / ja, so it can treat
// "anything that isn't Chinese" as a deliberate choice of the English pack. An
// env var has been validated by nobody: under that rule
// MULTICA_WECOM_DEFAULT_LOCALE=zh_Hant, or a stray quote, would quietly put a
// Chinese tenant's rooms into English. So this one matches exactly, and an
// operator who mistypes gets the old language and a log line, not a surprise.
func SetDeploymentLocale(raw string) Locale {
	switch strings.ToLower(strings.TrimSpace(raw)) {
	case "zh-hans", "zh":
		deploymentLocaleValue.Store(LocaleZhHans)
		return LocaleZhHans
	case "en":
		deploymentLocaleValue.Store(LocaleEn)
		return LocaleEn
	default:
		return deploymentLocale()
	}
}

// deploymentLocale is the configured language, or DefaultLocale before
// anything has configured one — which is also what every test sees.
func deploymentLocale() Locale {
	if v, ok := deploymentLocaleValue.Load().(Locale); ok {
		return v
	}
	return DefaultLocale
}

// DeploymentLocale exposes the room/default notice language to bridges that
// append their own system copy to a reply before it reaches this adapter.
func DeploymentLocale() Locale { return deploymentLocale() }

// resolveLocale maps a user's profile language onto a supported Locale. The
// profile validates to en / zh-Hans / ko / ja (handler/auth.go), and there are
// two packs: Chinese for zh*, English for everyone who chose anything else —
// a ko or ja user deliberately picked "not Chinese", and English is the
// lingua-franca pack we have for them. Only an EMPTY value falls back to
// deploymentLocale: absence is not a choice.
func resolveLocale(s string) Locale {
	switch v := strings.ToLower(strings.TrimSpace(s)); {
	case v == "":
		return deploymentLocale()
	case strings.HasPrefix(v, "zh"):
		return LocaleZhHans
	default:
		return LocaleEn
	}
}

// copyPack is the full set of user-visible strings for one locale. Everything
// the adapter can say is a field here; nothing is built by concatenating
// fragments elsewhere.
type copyPack struct {
	// AgentOffline / AgentArchived answer an engine outcome.
	AgentOffline  string
	AgentArchived string

	// FreshPending confirms /clear: the next chat message stays in the
	// conversation it is already in and runs without the context before it.
	// ChatStarted confirms /new, which is the other half of that split — a
	// new conversation the next message enters. Two commands, two answers:
	// telling a /clear user their conversation was replaced sends them
	// looking for a thread that never moved. IssueUsage answers a bare
	// /issue with the shape it wanted. Help lists the available commands.
	// Command replies live here rather than as package constants — the lint
	// asserts it, and a Chinese literal in replier.go cannot answer other locales.
	FreshPending string
	ChatStarted  string
	Help         string
	IssueUsage   string

	// InvokeDenied answers a member who may not run this agent (a private
	// agent belongs to its owner alone). It always goes to that member alone —
	// in place in a 1:1, in their own 1:1 for a group trigger (replier.go) — so
	// their profile picks the language even when the trigger was a room.
	InvokeDenied string

	// BindingPromptPrefix / BindingPromptSuffix wrap the bind URL.
	// BindingPending replaces the whole thing when the mint was throttled and
	// there is no URL to print. Both go to the sender alone, never to a room
	// (replier.go) — the URL carries a bearer token.
	BindingPromptPrefix string
	BindingPromptSuffix string
	BindingPending      string

	// BindingSentPrivately is what the ROOM sees when an unbound member
	// triggers the bot in a group: the prompt itself went to that member's
	// 1:1 chat, and without this line the group would read the bot as broken.
	// It names no one and carries no token, so it is safe in front of an
	// audience of unknown size.
	BindingSentPrivately string

	// MessageMeFirst is what the ROOM sees when WeCom refused a private reply
	// to a member because they have never written to the bot in their 1:1
	// (replier.go, errcodeUserMustMessageFirst). Both the binding link and an
	// invoke refusal end there, so it asks them to open that chat and promises
	// the reply will be waiting: the next message they send there is handled
	// like any other. Like BindingSentPrivately it names no one and carries no
	// token, and it does not say what the reply is about.
	MessageMeFirst string

	// IssueCreatedPrefix leads the /issue confirmation; IssueTitleSeparator
	// joins the identifier to the title when there is one.
	IssueCreatedPrefix  string
	IssueTitleSeparator string

	// IssueDuplicatePrefix leads the answer for an /issue the engine refused
	// because an active issue already matched. The identifier and title that
	// follow belong to THAT issue, which is why the copy has to say so rather
	// than reading like a confirmation: this case used to be answered with
	// the created copy, so the person was shown a number somebody else opened,
	// under a title they never wrote, for a report that was never filed.
	IssueDuplicatePrefix string

	// The ways a streaming reply ends in something other than an answer. Each
	// one closes the loading bubble the question opened, so each one has to
	// carry visible text — WeCom discards a closing frame it considers empty
	// and the bubble spins on forever (see hasVisibleChar in ws_frame.go).
	//
	// StreamNoReply — the agent finished with nothing to say.
	// StreamNoReplyWithFiles — the agent finished with no words but produced
	//   files, which arrive as separate messages right after this one.
	//   Distinct from StreamNoReply because that copy says nothing is coming,
	//   and then something arrives: a bubble that contradicts the next message
	//   reads as a bug even though both halves are working.
	// StreamNotStarted — no run was triggered at all (agent offline or
	//   archived, or the enqueue failed); the replier's own notice follows as
	//   a separate message with the detail.
	// StreamFailed — the run failed, and the platform published no reason of
	//   its own. A failure that DID carry one says that instead (failureText
	//   in typing_indicator.go).
	// StreamCancelled — the user stopped the run, so no answer is coming.
	//   Separate copy from StreamFailed on purpose: inviting a retry of
	//   something somebody just stopped on purpose reads as the bot not having
	//   noticed.
	StreamNoReply          string
	StreamNoReplyWithFiles string
	StreamNotStarted       string
	StreamFailed           string
	StreamCancelled        string

	// StreamContinued — the run outlived one stream's window, so the bubble is
	// sealed with this and the run carries on in a fresh bubble right
	// underneath it. A statement of fact, not a promise: nothing is owed on the
	// strength of it, the next bubble is already on screen.
	StreamContinued string

	// StreamStuck is the odd one out among the Stream* lines: it does not close
	// a bubble, it explains one that can no longer be closed. The server has
	// disowned the stream mid-run — another connection owns this conversation
	// now — so the spinner on the user's screen will turn for good and the rest
	// of the round has to arrive as new messages. Said once per bubble.
	StreamStuck string

	// StreamProgressPrefix heads the list of steps inside an open bubble. It
	// has to be there: without a heading, a list of actions sitting in a chat
	// reads as the answer arriving rather than as a status.
	StreamProgressPrefix string

	// Progress words each step the run takes. Kept in its own struct because
	// there are twenty-odd of them and they are only ever read together.
	Progress progressCopy

	// InboxDetailLink is the anchor text on the inbox card's deep link;
	// InboxTypeLabels names each notification kind, with InboxTypeFallback
	// covering a kind this adapter has not been taught yet.
	InboxDetailLink   string
	InboxTypeLabels   map[string]string
	InboxTypeFallback string
	// MediaTooLarge / MediaUnreadable tell the sender that an attachment did
	// not make it. Two reasons rather than one because the fix differs: a
	// file over the limit needs splitting or a link, whereas an expired or
	// undecryptable download just needs sending again.
	MediaTooLarge   string
	MediaUnreadable string
	// MediaSendFailed / MediaSendUnknown / MediaLookupFailed go the other way:
	// the agent produced a file and it did not plainly reach the chat. The
	// answer is already on screen and may well point at the file, so silence
	// would leave the user waiting for something that is not coming. One line
	// covers the whole turn however many files were affected — the reason (too
	// big, storage down, WeCom refused it) is a log line, not something the
	// reader can act on.
	//
	// Three of them because there are three things that can be true, and
	// telling them apart is the point (see deliveryState in outbound_media.go).
	// MediaSendFailed is the definite one: nothing arrived and nothing can
	// have. MediaSendUnknown is for a send whose verdict never came back — its
	// wording has to survive both endings, so it must not say "failed" to
	// someone looking at the file, and it says why nothing is resent, since a
	// duplicate cannot be taken back. MediaLookupFailed is earlier still: we
	// could not read what was attached to this reply, so we do not know whether
	// there was a file at all.
	MediaSendFailed   string
	MediaSendUnknown  string
	MediaLookupFailed string

	// UnsupportedMsgType answers a message kind the adapter cannot read at
	// all — sent from the read loop, which never reaches the Replier. It does
	// not name text, because photos, files, videos and 图文混排 route: a person
	// who has just watched the bot answer a screenshot and is then told it
	// only handles text reads that as the bot being broken.
	UnsupportedMsgType string
}

// label returns the display name for an inbox notification type.
func (c copyPack) label(t string) string {
	if l, ok := c.InboxTypeLabels[t]; ok {
		return l
	}
	return c.InboxTypeFallback
}

// issueCreated renders the /issue confirmation.
//
// The title is whatever the reporter typed, and this goes out as a markdown
// body — so it goes through breakMemberLinks here, at the render boundary,
// exactly as buildInboxMarkdown treats a title before putting it in a card.
// Without it, "/issue 安全升级:请点击 [重置密码](https://evil.example) 完成验证"
// comes back from the Multica bot, in the group, as a working link inside a
// confirmation everyone has reason to trust. Our own copy fragments stay raw;
// only the member-authored field is broken.
func (c copyPack) issueCreated(identifier, title string) string {
	title = breakMemberLinks(strings.TrimSpace(title))
	if title == "" {
		return c.IssueCreatedPrefix + identifier
	}
	return c.IssueCreatedPrefix + identifier + c.IssueTitleSeparator + title
}

// issueDuplicate renders the answer for an /issue the engine refused because
// an active issue already matched. The identifier and title belong to THAT
// issue, not to the message being answered.
//
// The title here belongs to somebody ELSE's issue, which makes breaking its
// links more important rather than less: the reporter is being shown text they
// did not write.
func (c copyPack) issueDuplicate(identifier, title string) string {
	out := c.IssueDuplicatePrefix + identifier
	if t := breakMemberLinks(strings.TrimSpace(title)); t != "" {
		out += c.IssueTitleSeparator + t
	}
	return out
}

// copyFor returns the pack for a locale, falling back to the deployment's.
func copyFor(l Locale) copyPack {
	if pack, ok := copyPacks[l]; ok {
		return pack
	}
	return copyPacks[deploymentLocale()]
}

// progressCopy is what the bubble says while the run is still going: one whole
// line per kind of work the agent can be doing.
//
// Every line comes in two forms, and which one is used depends on whether the
// call named anything. The %s is the argument that identifies the work, and it
// has been through progress_render.go's own cleaning first — never a content
// block, never a control character, never longer than a few lines on a phone.
// See the two rules at the top of that file.
type progressCopy struct {
	// Starting is the run getting under way, before its first tool call — on a
	// slow start the longest silence there is. The daemon's own milestone text
	// is English for operators and is never shown.
	Starting string

	// Read / Edit name the file; the Plain variants cover a call that names
	// no file this adapter recognises.
	Read      string
	ReadPlain string
	Edit      string
	EditPlain string

	// Command is a shell call; CommandNamed carries the command line.
	Command      string
	CommandNamed string

	// Four kinds whose plain form says only what sort of work it was. Each
	// Named variant carries the one thing that separates this call from the
	// next one: the search term, the URL, the subagent's brief, the plan.
	Search       string
	SearchNamed  string
	Web          string
	WebNamed     string
	Subtask      string
	SubtaskNamed string
	Plan         string
	PlanNamed    string

	// Service words an MCP call as "<server> · <tool>"; ServiceArgs adds the
	// call's parameters, which for an MCP tool are the only description of
	// what it is doing that this adapter can produce.
	Service     string
	ServiceArgs string

	// Skill / SkillPlain name the packaged procedure a Skill call ran. The
	// tool is always called Skill, so the skill's own name is the line.
	Skill      string
	SkillPlain string

	// Tool / ToolArgs / Fallback cover a tool this adapter has not been
	// taught. Saying something vague beats saying nothing: a step the user
	// never sees happen is indistinguishable from a run that has stalled.
	Tool     string
	ToolArgs string
	Fallback string

	// Failed marks a step that errored; FailedNamed carries the message. The
	// run may still recover, which is why the line says so either way.
	Failed      string
	FailedNamed string

	// Thinking heads the agent's own reasoning, which sits under the step
	// list. It needs a heading because without one a paragraph of prose in
	// the middle of a status block reads as the answer arriving early.
	Thinking string

	// Elapsed closes the list with how long the user has been waiting. A
	// spinner with no clock on it reads as stuck.
	Elapsed string
}

var copyPacks = map[Locale]copyPack{
	LocaleZhHans: {
		AgentOffline:         "⚠️ 智能体当前不在线,你的消息已收到,等它上线后会处理。",
		AgentArchived:        "⚠️ 该智能体已归档,无法回复。请联系工作区管理员。",
		FreshPending:         "✅ 已准备从空上下文运行。你的下一条聊天消息仍会进入当前对话,但不会带上之前的上下文。",
		ChatStarted:          "✅ 已新建 Multica 对话。你的下一条消息会进入该对话。",
		Help:                 engine.HelpTextChinese,
		IssueUsage:           "请填写任务标题,格式如下:\n\n`/issue <标题>`\n`[描述]`(可选)",
		InvokeDenied:         "⚠️ 你没有权限运行该智能体。如需使用,请联系它的所有者。",
		BindingPromptPrefix:  "👋 请先绑定你的 Multica 账号,才能与我对话:\n",
		BindingPromptSuffix:  "\n(链接 15 分钟内有效)",
		BindingPending:       "👋 绑定链接刚才已经发给你了,就在上方,请直接点击完成绑定。",
		BindingSentPrivately: "👋 已把绑定链接私发给你,请在与我的单聊里点击完成绑定。",
		MessageMeFirst:       "👋 请先私信我一句,我在那里回复你。",

		IssueCreatedPrefix:   "✅ 已创建 ",
		IssueTitleSeparator:  " — ",
		IssueDuplicatePrefix: "⚠️ 未创建 —— 已存在进行中的 ",

		StreamNoReply:          "(这轮没有需要回复的内容)",
		StreamNoReplyWithFiles: "(这轮没有文字回复,附件在下面)",
		StreamNotStarted:       "已收到,但这条暂时没能开始处理。",
		StreamFailed:           "⚠️ 这次没跑通,请稍后再试一次。",
		MediaTooLarge:          "抱歉,附件太大了,我这边收不下。",
		MediaUnreadable:        "抱歉,有附件没能收到,麻烦重新发一次。",
		MediaSendFailed:        "⚠️ 有文件没能发出来,我这边保留着,需要的话我再试一次。",
		MediaSendUnknown:       "⚠️ 有文件我没收到企业微信的送达回执,可能已经发到了、也可能没有。我不会自动重发,免得发重了;你那边没看到的话说一声,我再发一次。",
		MediaLookupFailed:      "⚠️ 我这边没查到这条回答带没带文件,所以要是有,这次没发出来。需要的话我再试一次。",
		UnsupportedMsgType:     "抱歉,我暂时无法处理这类消息。",
		StreamCancelled:        "⏹️ 这次处理已取消。",
		StreamContinued:        "处理时间较长,接下一条",
		StreamStuck:            "⚠️ 上面那条进度不会再更新了,这轮的结果我用新消息发你。",

		StreamProgressPrefix: "正在处理:",
		Progress: progressCopy{
			Starting:     "正在启动智能体",
			Read:         "正在读取 %s",
			ReadPlain:    "正在读取文件",
			Edit:         "正在修改 %s",
			EditPlain:    "正在修改文件",
			Command:      "正在执行命令",
			CommandNamed: "正在执行 %s",
			Search:       "正在检索代码",
			SearchNamed:  "正在检索 %s",
			Web:          "正在查资料",
			WebNamed:     "正在查 %s",
			Subtask:      "正在派子任务",
			SubtaskNamed: "正在派子任务:%s",
			Plan:         "正在梳理计划",
			PlanNamed:    "正在梳理计划:%s",
			Service:      "正在调用 %s · %s",
			ServiceArgs:  "正在调用 %s · %s:%s",
			Skill:        "正在启用技能 %s",
			SkillPlain:   "正在启用技能",
			Tool:         "正在使用 %s",
			ToolArgs:     "正在使用 %s:%s",
			Fallback:     "正在处理",
			Failed:       "上一步出错了,正在继续",
			FailedNamed:  "上一步出错了:%s,正在继续",
			Thinking:     "思考:",
			Elapsed:      "已用时 %s",
		},

		InboxDetailLink: "查看详情",
		InboxTypeLabels: map[string]string{
			"issue_assigned":       "任务指派",
			"mentioned":            "提及你",
			"status_changed":       "状态变更",
			"comment_added":        "新评论",
			"new_comment":          "新评论",
			"reaction_added":       "表情反应",
			"task_failed":          "task 失败",
			"unassigned":           "取消指派",
			"assignee_changed":     "指派人变更",
			"priority_changed":     "优先级变更",
			"due_date_changed":     "截止日期变更",
			"start_date_changed":   "开始日期变更",
			"issue_created":        "新任务",
			"project_owner_joined": "所有者加入了项目",
		},
		InboxTypeFallback: "新消息",
	},
	LocaleEn: {
		AgentOffline:         "⚠️ The agent is offline right now. Your message was received and will be handled once it's back.",
		AgentArchived:        "⚠️ This agent has been archived and can't reply. Please contact your workspace admin.",
		FreshPending:         "✅ Fresh start ready. Your next message stays in this chat but runs without the earlier context.",
		ChatStarted:          "✅ Started a new Multica chat. Your next message will enter it.",
		Help:                 engine.HelpTextEnglish,
		IssueUsage:           "Please include an issue title. Use:\n\n`/issue <title>`\n`[description]` (optional)",
		InvokeDenied:         "⚠️ You don't have permission to run this agent. Ask its owner if you need to use it.",
		BindingPromptPrefix:  "👋 Link your Multica account before we can talk:\n",
		BindingPromptSuffix:  "\n(the link is good for 15 minutes)",
		BindingPending:       "👋 I already sent you a link — it is just above, tap it to finish linking.",
		BindingSentPrivately: "👋 I've sent the link to your direct chat with me — tap it there to finish linking.",
		MessageMeFirst:       "👋 Send me a direct message first, and I'll reply there.",

		IssueCreatedPrefix:   "✅ Created ",
		IssueTitleSeparator:  " — ",
		IssueDuplicatePrefix: "⚠️ Not created — an active issue already covers this: ",

		StreamNoReply:          "(nothing to reply with this round)",
		StreamNoReplyWithFiles: "(no text this round — the files follow)",
		StreamNotStarted:       "Got it, but this one couldn't start processing.",
		StreamFailed:           "⚠️ That run didn't go through. Please try again.",
		MediaTooLarge:          "Sorry, that attachment is too big for me to take.",
		MediaUnreadable:        "Sorry, an attachment didn't come through — please send it again.",
		MediaSendFailed:        "⚠️ I couldn't send one of the files. It is still here — say the word and I'll try again.",
		MediaSendUnknown:       "⚠️ WeCom never confirmed one of the files, so it may or may not have arrived. I won't resend it automatically in case that shows it twice — tell me if it isn't there and I'll send it again.",
		MediaLookupFailed:      "⚠️ I couldn't check whether this answer had a file with it, so if it did, it didn't go out. Say the word and I'll try again.",
		UnsupportedMsgType:     "Sorry, I can't read that kind of message.",
		StreamCancelled:        "⏹️ That run was cancelled.",
		StreamContinued:        "Still working, continued below",
		StreamStuck:            "⚠️ The status above won't update any further. I'll send this round's result as a new message.",

		StreamProgressPrefix: "Working on it: ",
		Progress: progressCopy{
			Starting:     "Starting the agent",
			Read:         "Reading %s",
			ReadPlain:    "Reading a file",
			Edit:         "Editing %s",
			EditPlain:    "Editing a file",
			Command:      "Running a command",
			CommandNamed: "Running %s",
			Search:       "Searching the code",
			SearchNamed:  "Searching for %s",
			Web:          "Looking things up",
			WebNamed:     "Looking up %s",
			Subtask:      "Handing off a subtask",
			SubtaskNamed: "Handing off a subtask: %s",
			Plan:         "Working out a plan",
			PlanNamed:    "Working out a plan: %s",
			Service:      "Calling %s · %s",
			ServiceArgs:  "Calling %s · %s: %s",
			Skill:        "Using the %s skill",
			SkillPlain:   "Using a skill",
			Tool:         "Using %s",
			ToolArgs:     "Using %s: %s",
			Fallback:     "Working",
			Failed:       "That step errored — carrying on",
			FailedNamed:  "That step errored: %s — carrying on",
			Thinking:     "Thinking:",
			Elapsed:      "%s elapsed",
		},

		InboxDetailLink: "View details",
		InboxTypeLabels: map[string]string{
			"issue_assigned":       "Assigned",
			"mentioned":            "Mentioned",
			"status_changed":       "Status changed",
			"comment_added":        "New comment",
			"new_comment":          "New comment",
			"reaction_added":       "Reaction",
			"task_failed":          "Run failed",
			"unassigned":           "Unassigned",
			"assignee_changed":     "Assignee changed",
			"priority_changed":     "Priority changed",
			"due_date_changed":     "Due date changed",
			"start_date_changed":   "Start date changed",
			"issue_created":        "New issue",
			"project_owner_joined": "Owner joined project",
		},
		InboxTypeFallback: "Notification",
	},
}