Skip to content

Commit cd16d72

Browse files
fix(company-resolver): honour /clip connect in multi-company setups (#42)
resolveCompanyId() previously called ctx.companies.list({ limit: 1 }) and cached the result globally, ignoring any company explicitly set via /clip connect. In multi-company instances this caused /clip status, /clip issues, /clip budget, /clip agents, and job-level resolvers to always show data for the wrong company. The fix reads the company_default instance state written by handleConnect before falling back to list-based resolution. The /clip connect result is intentionally NOT cached so that switching companies takes effect immediately without restarting the plugin. Fixes #31 (command-path variant — escalation button path already fixed by #32). Co-authored-by: Paperclip <noreply@paperclip.ing>
1 parent 561dd0b commit cd16d72

1 file changed

Lines changed: 16 additions & 0 deletions

File tree

src/company-resolver.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,26 @@ import type { PluginContext } from "@paperclipai/plugin-sdk";
33
/**
44
* Lazy company-ID resolver — avoids startup-time API calls that can crash
55
* worker activation. The resolved value is cached after the first successful call.
6+
*
7+
* Multi-company fix: check `company_default` instance state (written by
8+
* `/clip connect`) before falling back to list-based resolution. The
9+
* connected company is NOT cached so that `/clip connect` changes take effect
10+
* immediately without restarting the plugin.
611
*/
712
let _cachedCompanyId: string | null = null;
813

914
export async function resolveCompanyId(ctx: PluginContext): Promise<string> {
15+
// Check if a guild-level default was set via /clip connect — always re-read
16+
// so that switching companies works without a plugin restart.
17+
try {
18+
const connected = await ctx.state.get({ scopeKind: "instance", stateKey: "company_default" });
19+
if (connected?.companyId) {
20+
return connected.companyId as string;
21+
}
22+
} catch {
23+
// state API unavailable at this call site — fall through to list-based resolution
24+
}
25+
1026
if (_cachedCompanyId) return _cachedCompanyId;
1127
try {
1228
const companies = await ctx.companies.list({ limit: 1 });

0 commit comments

Comments
 (0)