@uniqbit/mate-core 1.0.0-canary.9 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/claude-capability-plugins/companion-mentions/.claude-plugin/plugin.json +1 -1
- package/package.json +5 -4
- package/src/cli/commands/artifact/delivery/link.ts +1 -1
- package/src/cli/commands/artifact/delivery/output.ts +10 -1
- package/src/cli/commands/artifact/delivery/resolve.ts +3 -2
- package/src/cli/commands/artifact/delivery/submit.ts +9 -4
- package/src/cli/commands/artifact/delivery/take.ts +12 -9
- package/src/cli/commands/artifact/finish/command.ts +16 -14
- package/src/cli/commands/artifact/finish/engine.ts +8 -8
- package/src/cli/commands/artifact/project/command.ts +2 -3
- package/src/cli/commands/companion/hub.ts +11 -6
- package/src/cli/commands/companion/link.ts +173 -46
- package/src/cli/commands/report/contract.ts +0 -17
- package/src/cli/commands/studio/access.ts +4 -0
- package/src/cli/commands/studio/actions.ts +35 -2
- package/src/cli/commands/studio/mcp-manager.ts +162 -0
- package/src/cli/commands/studio/routes.ts +12 -0
- package/src/cli/commands/studio/selection.ts +44 -11
- package/src/cli/commands/studio/server.ts +246 -2
- package/src/cli/commands/studio/services.ts +26 -0
- package/src/cli/commands/studio/terminal-launch.ts +21 -4
- package/src/cli/commands/studio/terminal.ts +3 -14
- package/src/cli/commands/studio/work-requests.ts +9 -0
- package/src/cli/commands/studio/work.ts +473 -0
- package/src/cli/commands/vscode/release-vsix.json +2 -2
- package/src/cli/commands/working/cleanup.ts +1 -18
- package/src/cli/companion-link-wizard.tsx +158 -91
- package/src/cli/usage.ts +1 -1
- package/src/lib/agent-credential.ts +131 -0
- package/src/lib/credential-kinds.ts +7 -0
- package/src/lib/delivery/merge-git.ts +16 -0
- package/src/lib/delivery/record-delivery.ts +12 -6
- package/src/lib/delivery/record.ts +5 -0
- package/src/lib/delivery/refresh.ts +34 -6
- package/src/lib/delivery/staging/refs.ts +9 -4
- package/src/lib/delivery/staging/submit.ts +81 -22
- package/src/lib/mcp-manager/resolve.ts +27 -9
- package/src/lib/mcp-manager/store.ts +35 -11
- package/src/lib/mcp-manager/types.ts +14 -0
- package/src/lib/mcp-manager/validate.ts +6 -10
- package/src/lib/opencode-server.ts +2 -17
- package/src/lib/orchestrator/repo-local-store.ts +14 -0
- package/src/lib/orchestrator/repository-link.ts +80 -52
- package/src/lib/orchestrator/root-context.ts +17 -1
- package/src/lib/studio-chat/agent.ts +139 -0
- package/src/lib/studio-chat/companion-context.ts +136 -0
- package/src/lib/studio-chat/credential.ts +79 -0
- package/src/lib/studio-chat/harness-chat-agent.ts +334 -0
- package/src/lib/studio-chat/host-sandbox.ts +578 -0
- package/src/lib/studio-chat/prepare-bridges.ts +95 -0
- package/src/lib/studio-chat/session.ts +15 -0
- package/src/lib/studio-chat/turns.ts +112 -0
- package/src/lib/workset/claims.ts +85 -0
- package/src/lib/workset/execution.ts +71 -0
- package/src/lib/workset/git-transaction.ts +74 -0
- package/src/lib/workset/groups.ts +176 -0
- package/src/lib/workset/inventory.ts +196 -0
- package/src/lib/workset/projection.ts +237 -0
- package/src/lib/workset/record.ts +101 -36
- package/src/lib/workset/repositories.ts +24 -0
- package/src/lib/workset/sharing.ts +951 -0
- package/src/lib/workset/stages.ts +50 -14
- package/src/lib/workset/transactions.ts +158 -0
- package/src/runtime/companion-git-state.ts +1 -0
- package/src/runtime/companion-guidance.ts +2 -2
- package/src/runtime/forge-remote.ts +6 -0
- package/src/runtime/host-process.ts +28 -0
- package/src/runtime/secret-guard.ts +18 -1
- package/src/runtime/secret-patterns.ts +4 -3
- package/src/runtime/submitted-arrivals.ts +28 -2
- package/src/runtime/write-atomic.ts +2 -2
- package/src/studio.ts +52 -8
- package/src/templates/capabilities/openspec-cap/mate-full/schema.yaml +1 -1
- package/src/templates/capabilities/openspec-cap/mate-full/templates/proposal.md +1 -1
- package/src/templates/mate-skills/agents/mate-publish-artifact/SKILL.md +3 -2
- package/src/templates/mate-skills/agents/mate-submit-change/SKILL.md +5 -3
- package/src/templates/mate-skills/claude/mate-publish-artifact/SKILL.md +3 -2
- package/src/templates/mate-skills/claude/mate-submit-change/SKILL.md +5 -3
- package/src/tools/setup/__snapshots__/runtime-surface-golden.test.ts.snap +12 -12
- package/src/tools/setup/capabilities/mcp-manager.ts +1 -2
- package/src/tools/setup/dynamic-plugins/declarations.ts +0 -26
- package/src/tools/setup/plugins/gitignore.ts +1 -0
- package/src/tools/setup/providers/claude-format.ts +1 -5
- package/src/tools/setup/providers/claude.ts +28 -3
- package/src/tools/setup/providers/opencode.ts +7 -18
- package/src/tools/setup/providers/relative-path-warnings.ts +26 -0
- package/studio-dist/THIRD-PARTY-NOTICES.md +297 -0
- package/studio-dist/client/THIRD-PARTY-LICENSES.md +4294 -0
- package/studio-dist/client/assets/DialogTitle-DOhjDa7H.js +1 -0
- package/studio-dist/client/assets/app-CG3vCgyA.css +2 -0
- package/studio-dist/client/assets/bundle-mjs-CdK3cBzO.js +1 -0
- package/studio-dist/client/assets/button-upSzkd-q.js +1 -0
- package/studio-dist/client/assets/chat-DbaMXb8H.js +157 -0
- package/studio-dist/client/assets/cn-BCnYnarL.js +1 -0
- package/studio-dist/client/assets/combobox-cP9OsRf2.js +1 -0
- package/studio-dist/client/assets/{companion-view-C1F0_jbt.js → companion-view-B54_Lj0S.js} +1 -1
- package/studio-dist/client/assets/fields-DNZ4a89M.js +1 -0
- package/studio-dist/client/assets/friction-DgEimeDB.js +1 -0
- package/studio-dist/client/assets/getting-started-BTfYCt5s.js +1 -0
- package/studio-dist/client/assets/{getting-started-BN4aTpsz.js → getting-started-CAszmdDJ.js} +1 -1
- package/studio-dist/client/assets/getting-started._topic-CHJMf0pa.js +1 -0
- package/studio-dist/client/assets/getting-started.index-MR6HftWs.js +1 -0
- package/studio-dist/client/assets/{graph-canvas-Be5qgdDt.js → graph-canvas-BdNk2F9O.js} +1 -1
- package/studio-dist/client/assets/graph-hRijsC89.js +2 -0
- package/studio-dist/client/assets/guides-CwFnh08E.js +1 -0
- package/studio-dist/client/assets/highlighted-body-JLMTFACH-BcroQey-.js +1 -0
- package/studio-dist/client/assets/index-D5oGyvG8.js +41 -0
- package/studio-dist/client/assets/{markdown-view-dku7MGPn.js → markdown-view-CJ8JsLb7.js} +1 -1
- package/studio-dist/client/assets/marked.esm-vFuq1cCd.js +65 -0
- package/studio-dist/client/assets/mcp-BE9M-bkO.js +4 -0
- package/studio-dist/client/assets/mermaid-WFAXDIZX-BjSsakrn.js +1 -0
- package/studio-dist/client/assets/panel-CDQ6Lryw.js +1 -0
- package/studio-dist/client/assets/plugins._service._view-DRVPUAWN.js +1 -0
- package/studio-dist/client/assets/{prepaint-BkoDSw5X.js → prepaint-sD_LlrDN.js} +1 -1
- package/studio-dist/client/assets/{queries-CMvC2OkU.js → queries-COL925ML.js} +1 -1
- package/studio-dist/client/assets/react-dom-K3Hp2Cdj.js +1 -0
- package/studio-dist/client/assets/{react-BYUSgtBs.js → react-h2xQ_NSp.js} +1 -1
- package/studio-dist/client/assets/reports-BClsojkb.js +1 -0
- package/studio-dist/client/assets/routes-CJQ2MAej.js +1 -0
- package/studio-dist/client/assets/search-DhWdEiKT.js +1 -0
- package/studio-dist/client/assets/select-B2FsN9FY.js +1 -0
- package/studio-dist/client/assets/skills-SoUHk77W.js +1 -0
- package/studio-dist/client/assets/{specs-Bqd1ajyD.js → specs-C_tZnAvF.js} +1 -1
- package/studio-dist/client/assets/specs-view-C6t3Y5IT.js +1 -0
- package/studio-dist/client/assets/steps-CF3-Zpmh.js +1 -0
- package/studio-dist/client/assets/{store-BYnEU33C.js → store-pY9gU4uS.js} +1 -1
- package/studio-dist/client/assets/studio-X7bnFrRe.js +36 -0
- package/studio-dist/client/assets/studio-queries-CKtAnIKP.js +2 -0
- package/studio-dist/client/assets/usage-BaaqUpTG.js +55 -0
- package/studio-dist/client/assets/useAnchoredPopupScrollLock-BspHR7Wt.js +1 -0
- package/studio-dist/client/assets/useBlocker-BQOfcu_f.js +1 -0
- package/studio-dist/client/assets/useMutation-DKeEoP5S.js +1 -0
- package/studio-dist/client/assets/useOpenInteractionType-CtaZCaFT.js +1 -0
- package/studio-dist/client/assets/usePopupHandleStore-Bc7Q4bnF.js +1 -0
- package/studio-dist/client/assets/vault-sAPFUnPN.js +1 -0
- package/studio-dist/client/assets/{warnings-Cnw99ugO.js → warnings-D7YY1HSG.js} +1 -1
- package/studio-dist/client/assets/work-BZek_LGB.js +5 -0
- package/studio-dist/client/assets/workflow-N5boHXFz.js +1 -0
- package/studio-dist/server/THIRD-PARTY-LICENSES.md +4371 -0
- package/studio-dist/server/assets/DialogTitle-DnKSl2V0.js +421 -0
- package/studio-dist/server/assets/{__23tanstack-start-server-fn-resolver-CS3QRY4Q.js → __23tanstack-start-server-fn-resolver-Bli9bpSB.js} +38 -18
- package/studio-dist/server/assets/_tanstack-start-manifest_v.js +91 -59
- package/studio-dist/server/assets/{actions-D8erOphH.js → actions-CHEFA1i9.js} +33 -1
- package/studio-dist/server/assets/{cn-DWFJaP_9.js → bundle-mjs-Ot6dQ8Pq.js} +1 -2056
- package/studio-dist/server/assets/{button-Ct_WeYLc.js → button-BqA1urbl.js} +14 -7
- package/studio-dist/server/assets/chat-zl332Tvu.js +49486 -0
- package/studio-dist/server/assets/cn-B0fl6NWK.js +2057 -0
- package/studio-dist/server/assets/{combobox-17vO5Uz8.js → combobox-iTjvPCtS.js} +7 -6
- package/studio-dist/server/assets/{companion-view-JkxnGLlp.js → companion-view-DIQ6AFIY.js} +2 -2
- package/studio-dist/server/assets/{companions-YNLsbnch.js → companions-aVwjRL8j.js} +1 -1
- package/studio-dist/server/assets/{fields-VytPpqBq.js → fields-BMap2_Ju.js} +10 -3
- package/studio-dist/server/assets/{friction-BW86PCPM.js → friction-BER50hoD.js} +2 -2
- package/studio-dist/server/assets/{friction-B6osqFSm.js → friction-BPs9KWdq.js} +8 -8
- package/studio-dist/server/assets/{getting-started-CFHSeX5h.js → getting-started-DGfRVmZ_.js} +2 -2
- package/studio-dist/server/assets/{getting-started._topic-BK2eU6AS.js → getting-started._topic-BgrBA4Vj.js} +2 -2
- package/studio-dist/server/assets/{getting-started.index-DZLuXnNO.js → getting-started.index-BaFfQ9Mn.js} +2 -2
- package/studio-dist/server/assets/{graph-BJVZBevL.js → graph-Cgzlrv3P.js} +8 -8
- package/studio-dist/server/assets/{graph-DGZHV-Ek.js → graph-DODpuOy_.js} +1 -1
- package/studio-dist/server/assets/{graph-canvas-lrNT6jbl.js → graph-canvas-DW_zU1nA.js} +2 -2
- package/studio-dist/server/assets/{guides-_G8Vucsk.js → guides-CA_EMG7E.js} +1 -1
- package/studio-dist/server/assets/{guides-CEwLOZb9.js → guides-D0SpoD32.js} +1 -1
- package/studio-dist/server/assets/{guides.server-ZWFKX2XJ.js → guides.server-DJjQuy03.js} +1 -1
- package/studio-dist/server/assets/highlighted-body-JLMTFACH-BeHllu-r.js +41 -0
- package/studio-dist/server/assets/{markdown-view-Bmjghg_Z.js → markdown-view-CvVBGMZV.js} +1 -1
- package/studio-dist/server/assets/mcp-BU0fLYyJ.js +1103 -0
- package/studio-dist/server/assets/mcp-manager-D9YctTQe.js +50 -0
- package/studio-dist/server/assets/mermaid-WFAXDIZX-B1yDN6vG.js +1 -0
- package/studio-dist/server/assets/{panel-DvRjyil3.js → panel-D1K6Bsux.js} +4 -32
- package/studio-dist/server/assets/{plugins._service._view-B32LzraG.js → plugins._service._view-DdO1nELF.js} +4 -4
- package/studio-dist/server/assets/{prepaint-BEHwKl51.js → prepaint-BB3dwSmz.js} +1 -1
- package/studio-dist/server/assets/{queries-D6Vq0AuZ.js → queries-BwiDxQy5.js} +1 -1
- package/studio-dist/server/assets/{react-CiBZPDs8.js → react-B2cLiz-i.js} +1 -1
- package/studio-dist/server/assets/{reports-Dn92pLap.js → reports-9Pr7Bz6o.js} +7 -7
- package/studio-dist/server/assets/{reports-DDaH5k0H.js → reports-Cc9M3phM.js} +1 -1
- package/studio-dist/server/assets/{router-Bjf4S9tO.js → router-DcWe0J7Y.js} +827 -548
- package/studio-dist/server/assets/{routes-B4sNiE0X.js → routes-BEXxl0py.js} +8 -8
- package/studio-dist/server/assets/{search-DKeJRvg8.js → search-bltT7gwy.js} +39 -7
- package/studio-dist/server/assets/{select-CVHYL7oR.js → select-CZvHgGCi.js} +7 -6
- package/studio-dist/server/assets/{shell-BxbYYaAk.js → shell-BQ0vMKXN.js} +13 -4
- package/studio-dist/server/assets/{skills-Bj8eOgzR.js → skills-Be_52h0i.js} +1 -1
- package/studio-dist/server/assets/{skills-B4-0qpqN.js → skills-BpGGUl6L.js} +8 -8
- package/studio-dist/server/assets/{specs-rfMug6qW.js → specs-CCGgXE-V.js} +5 -5
- package/studio-dist/server/assets/{specs-view-qi1SE1Sl.js → specs-view-DtL5JxYM.js} +12 -11
- package/studio-dist/server/assets/{steps-rger6HJc.js → steps-BWFSO6dM.js} +1 -1
- package/studio-dist/server/assets/{store-Dg9T7oWs.js → store-X7yLPp7v.js} +1 -1
- package/studio-dist/server/assets/{studio-queries-CeFfmIuM.js → studio-queries-ur1ygXi3.js} +117 -13
- package/studio-dist/server/assets/{studio-D79JzDLc.js → studio-qzH-UETC.js} +209 -180
- package/studio-dist/server/assets/{usage-Dw_UQFX-.js → usage-C1o0DLzS.js} +1 -1
- package/studio-dist/server/assets/{usage-BJgYWRCX.js → usage-DWbSPrPD.js} +10 -9
- package/studio-dist/server/assets/{useAnchoredPopupScrollLock-ciS_sEq3.js → useAnchoredPopupScrollLock-DLDmv6kM.js} +4 -135
- package/studio-dist/server/assets/useBlocker-CQtANHK8.js +111 -0
- package/studio-dist/server/assets/{useMutation-DnWK0PhO.js → useMutation-NjlA8oFY.js} +2 -2
- package/studio-dist/server/assets/useOpenInteractionType-CZYscfOb.js +136 -0
- package/studio-dist/server/assets/usePopupHandleStore-DEEXJYv0.js +26 -0
- package/studio-dist/server/assets/{vault-BzarssiL.js → vault-B_kfi1h-.js} +1 -1
- package/studio-dist/server/assets/{vault-CvlrLd2H.js → vault-dL79JlKH.js} +8 -115
- package/studio-dist/server/assets/{views-CAxwKHMK.js → views-BSGH56lb.js} +3 -3
- package/studio-dist/server/assets/{warnings-BpVCugHm.js → warnings-DtUVPV-k.js} +2 -2
- package/studio-dist/server/assets/work-D789VXGH.js +110 -0
- package/studio-dist/server/assets/work-DsCEfhmK.js +3817 -0
- package/studio-dist/server/assets/work-requests-BLhOmQFO.js +11 -0
- package/studio-dist/server/assets/{workflow-BdrzcCt8.js → workflow-BOZ2sRYg.js} +9 -9
- package/studio-dist/server/server.js +3 -3
- package/wrappers/bin/openspec +87 -2
- package/studio-dist/client/assets/app-ggXJ_QVh.css +0 -2
- package/studio-dist/client/assets/button-C9SQW3kI.js +0 -1
- package/studio-dist/client/assets/cn-DC80MJV0.js +0 -1
- package/studio-dist/client/assets/combobox-D7mKYCBb.js +0 -1
- package/studio-dist/client/assets/fields-CEep8IFT.js +0 -1
- package/studio-dist/client/assets/friction-D9dBU7WN.js +0 -1
- package/studio-dist/client/assets/getting-started-BDyGvHhc.js +0 -1
- package/studio-dist/client/assets/getting-started._topic-B9ajbF4o.js +0 -1
- package/studio-dist/client/assets/getting-started.index-CYiBPtb7.js +0 -1
- package/studio-dist/client/assets/graph-ah7ZXfTa.js +0 -2
- package/studio-dist/client/assets/guides-CsL0660B.js +0 -1
- package/studio-dist/client/assets/index-zmRH87D4.js +0 -40
- package/studio-dist/client/assets/panel-COgk0vP5.js +0 -1
- package/studio-dist/client/assets/plugins._service._view-Cn3WBHab.js +0 -1
- package/studio-dist/client/assets/reports-C_AqLBYH.js +0 -1
- package/studio-dist/client/assets/routes-BokQmHtC.js +0 -1
- package/studio-dist/client/assets/search-DxidNK2M.js +0 -1
- package/studio-dist/client/assets/select-CYFCbHUd.js +0 -1
- package/studio-dist/client/assets/skills-R3vttWHM.js +0 -1
- package/studio-dist/client/assets/specs-view-znqFPx9p.js +0 -1
- package/studio-dist/client/assets/steps-tVDWn7j6.js +0 -1
- package/studio-dist/client/assets/studio-ndtFcS2Y.js +0 -100
- package/studio-dist/client/assets/studio-queries-CkUSByy7.js +0 -2
- package/studio-dist/client/assets/usage-Kuhr8dSl.js +0 -55
- package/studio-dist/client/assets/useAnchoredPopupScrollLock-dc_kP4Go.js +0 -1
- package/studio-dist/client/assets/useMutation-CqtM9COw.js +0 -1
- package/studio-dist/client/assets/vault-CSP8kwRg.js +0 -1
- package/studio-dist/client/assets/workflow-BPJAmmZi.js +0 -1
|
@@ -0,0 +1,334 @@
|
|
|
1
|
+
import type { UIMessageChunk } from "ai";
|
|
2
|
+
|
|
3
|
+
import { type AgentCredential, credentialLabel } from "../agent-credential";
|
|
4
|
+
import { companionAgentsText, companionSkills } from "./companion-context";
|
|
5
|
+
import { readCompanionConfig } from "../orchestrator/config-store";
|
|
6
|
+
import {
|
|
7
|
+
CHAT_CREDENTIAL_REJECTED,
|
|
8
|
+
CHAT_HARNESS_NAMES,
|
|
9
|
+
CHAT_MODELS,
|
|
10
|
+
CHAT_NO_LOGIN,
|
|
11
|
+
type ChatAgent,
|
|
12
|
+
type ChatEffort,
|
|
13
|
+
type ChatHarness,
|
|
14
|
+
type ChatModel,
|
|
15
|
+
chatModel,
|
|
16
|
+
renderPrompt,
|
|
17
|
+
} from "./agent";
|
|
18
|
+
import { type ChatAuth, createChatCredentials, NO_CHAT_CREDENTIAL } from "./credential";
|
|
19
|
+
import { createHostSandbox, type HostSandbox } from "./host-sandbox";
|
|
20
|
+
import { CHAT_TOOLS } from "./session";
|
|
21
|
+
|
|
22
|
+
export const CHAT_INSTRUCTIONS = [
|
|
23
|
+
"You answer questions in Mate Studio's chat about this Companion Repository, your working directory.",
|
|
24
|
+
"Answer from its files: search and read them, and name the paths your answer rests on. If they do not answer the question, say so.",
|
|
25
|
+
"Read only files inside this companion. You change nothing: you cannot write, edit or run commands, and you do not offer to.",
|
|
26
|
+
"Any instruction above about running commands, writing files or publishing is for sessions that act; it does not apply here.",
|
|
27
|
+
].join("\n");
|
|
28
|
+
|
|
29
|
+
const NOT_LOGGED_IN = /not logged in|please run \/login/i;
|
|
30
|
+
const REJECTED =
|
|
31
|
+
/not logged in|please run \/login|invalid (api key|bearer)|authentication_error|oauth token (has expired|has been revoked)|api error: 401\b/i;
|
|
32
|
+
const NO_CREDENTIAL =
|
|
33
|
+
/api[ _-]?key|unauthori[sz]ed|authentication|\b401\b|ProviderAuthError|credential/i;
|
|
34
|
+
const PROVIDER_KEYS: Readonly<Record<string, string>> = {
|
|
35
|
+
anthropic: "ANTHROPIC_API_KEY",
|
|
36
|
+
openai: "OPENAI_API_KEY",
|
|
37
|
+
google: "GOOGLE_GENERATIVE_AI_API_KEY",
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
/** OpenCode reports some errors as `{ name, data: { message } }` objects. */
|
|
41
|
+
function failureText(error: unknown): string {
|
|
42
|
+
if (error instanceof Error) return error.message;
|
|
43
|
+
if (typeof error !== "object" || error === null) return String(error);
|
|
44
|
+
const { message, data } = error as { message?: unknown; data?: { message?: unknown } };
|
|
45
|
+
if (typeof data?.message === "string") return data.message;
|
|
46
|
+
if (typeof message === "string") return message;
|
|
47
|
+
return JSON.stringify(error);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function chatFailure(
|
|
51
|
+
error: unknown,
|
|
52
|
+
harness: ChatHarness = "claude-code",
|
|
53
|
+
model?: ChatModel,
|
|
54
|
+
credential?: AgentCredential,
|
|
55
|
+
): string {
|
|
56
|
+
const text = failureText(error);
|
|
57
|
+
if (text === NO_CHAT_CREDENTIAL) return text;
|
|
58
|
+
if (harness === "claude-code" && credential && REJECTED.test(text)) {
|
|
59
|
+
return `${CHAT_CREDENTIAL_REJECTED} (${credentialLabel(credential.name)}). Set another under Credential in the session panel, then ask again.`;
|
|
60
|
+
}
|
|
61
|
+
if (harness === "claude-code" && NOT_LOGGED_IN.test(text)) {
|
|
62
|
+
return `${CHAT_NO_LOGIN}. Run \`claude\` in a terminal and log in, or set a credential under Credential in the session panel, then ask again.`;
|
|
63
|
+
}
|
|
64
|
+
if (harness === "claude-code" && REJECTED.test(text)) {
|
|
65
|
+
return `${CHAT_CREDENTIAL_REJECTED} (your local Claude login). Run \`claude\` in a terminal and log in again, or set a credential under Credential in the session panel, then ask again.`;
|
|
66
|
+
}
|
|
67
|
+
if (harness === "opencode" && NO_CREDENTIAL.test(text)) {
|
|
68
|
+
const provider = model?.split("/")[0];
|
|
69
|
+
const key = (provider && PROVIDER_KEYS[provider]) ?? "the provider's API key";
|
|
70
|
+
const login = provider === "anthropic" ? "" : ", or log in with `opencode auth login`";
|
|
71
|
+
return `OpenCode has no credential for ${provider ?? "this model"}. Set ${key} in Studio's environment${login}, then ask again.`;
|
|
72
|
+
}
|
|
73
|
+
return `${CHAT_HARNESS_NAMES[harness]}: ${text || "the chat agent failed"}`;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
interface HarnessSession {
|
|
77
|
+
stop(): Promise<unknown>;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
interface HarnessResult {
|
|
81
|
+
toUIMessageStream(options: {
|
|
82
|
+
onError: (error: unknown) => string;
|
|
83
|
+
}): AsyncIterable<UIMessageChunk>;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
interface HarnessAgentLike {
|
|
87
|
+
createSession(options: {
|
|
88
|
+
sandboxSession: HostSandbox;
|
|
89
|
+
abortSignal: AbortSignal;
|
|
90
|
+
}): Promise<HarnessSession>;
|
|
91
|
+
stream(options: {
|
|
92
|
+
session: HarnessSession;
|
|
93
|
+
prompt: string;
|
|
94
|
+
abortSignal: AbortSignal;
|
|
95
|
+
}): Promise<HarnessResult>;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export interface HarnessModules {
|
|
99
|
+
createAgent(settings: {
|
|
100
|
+
port: number;
|
|
101
|
+
instructions: string;
|
|
102
|
+
skills: Awaited<ReturnType<typeof companionSkills>>;
|
|
103
|
+
model?: ChatModel;
|
|
104
|
+
effort?: ChatEffort;
|
|
105
|
+
credential?: AgentCredential;
|
|
106
|
+
}): HarnessAgentLike;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
type AdapterFactory = (settings: {
|
|
110
|
+
port: number;
|
|
111
|
+
model: ChatModel;
|
|
112
|
+
effort?: ChatEffort;
|
|
113
|
+
credential?: AgentCredential;
|
|
114
|
+
}) => unknown;
|
|
115
|
+
|
|
116
|
+
const ADAPTERS: Record<ChatHarness, () => Promise<AdapterFactory>> = {
|
|
117
|
+
"claude-code": async () => {
|
|
118
|
+
const { createClaudeCode } = await import("@ai-sdk/harness-claude-code");
|
|
119
|
+
return ({ port, effort, credential }) =>
|
|
120
|
+
createClaudeCode({
|
|
121
|
+
port,
|
|
122
|
+
strictMcpConfig: true,
|
|
123
|
+
...(effort ? { effort } : {}),
|
|
124
|
+
...(credential ? { auth: { [credential.name]: credential.value } } : {}),
|
|
125
|
+
});
|
|
126
|
+
},
|
|
127
|
+
opencode: async () => {
|
|
128
|
+
const { createOpenCode } = await import("@ai-sdk/harness-opencode");
|
|
129
|
+
return ({ port, model, effort }) =>
|
|
130
|
+
createOpenCode({
|
|
131
|
+
port,
|
|
132
|
+
provider: model.split("/")[0],
|
|
133
|
+
openCodeConfig: { share: "disabled", autoupdate: false },
|
|
134
|
+
...(effort ? { reasoningVariant: effort } : {}),
|
|
135
|
+
});
|
|
136
|
+
},
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
export function loadAdapter(harness: ChatHarness): Promise<AdapterFactory> {
|
|
140
|
+
return ADAPTERS[harness]();
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
export async function loadHarness(harness: ChatHarness): Promise<HarnessModules> {
|
|
144
|
+
const [{ HarnessAgent }, adapter] = await Promise.all([
|
|
145
|
+
import("@ai-sdk/harness/agent"),
|
|
146
|
+
loadAdapter(harness),
|
|
147
|
+
]);
|
|
148
|
+
const defaultModel = CHAT_MODELS[harness][0]!.id;
|
|
149
|
+
return {
|
|
150
|
+
createAgent: ({ port, instructions, skills, model = defaultModel, effort, credential }) =>
|
|
151
|
+
new HarnessAgent({
|
|
152
|
+
harness: adapter({
|
|
153
|
+
port,
|
|
154
|
+
model,
|
|
155
|
+
...(effort ? { effort } : {}),
|
|
156
|
+
...(credential ? { credential } : {}),
|
|
157
|
+
}) as never,
|
|
158
|
+
model,
|
|
159
|
+
activeTools: [...CHAT_TOOLS[harness]] as never,
|
|
160
|
+
permissionMode: "allow-reads",
|
|
161
|
+
skills,
|
|
162
|
+
instructions,
|
|
163
|
+
// The companion itself, not a per-session subdirectory of it.
|
|
164
|
+
sandboxConfig: { workDir: "." },
|
|
165
|
+
}) as unknown as HarnessAgentLike,
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
export interface HarnessChatAgentOptions {
|
|
170
|
+
bootstrapCache?: string;
|
|
171
|
+
prebuiltBridges?: string;
|
|
172
|
+
credential?: () => ChatAuth;
|
|
173
|
+
loadHarness?: (harness: ChatHarness) => Promise<HarnessModules>;
|
|
174
|
+
createSandbox?: typeof createHostSandbox;
|
|
175
|
+
companionContext?: (
|
|
176
|
+
companionPath: string,
|
|
177
|
+
) => Promise<{ instructions: string; skills: Awaited<ReturnType<typeof companionSkills>> }>;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
async function readCompanionContext(companionPath: string) {
|
|
181
|
+
const config = await readCompanionConfig(companionPath);
|
|
182
|
+
const [skills, instructions] = await Promise.all([
|
|
183
|
+
companionSkills(companionPath, config),
|
|
184
|
+
companionAgentsText(companionPath),
|
|
185
|
+
]);
|
|
186
|
+
return { instructions, skills };
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
const takesNoEffort = (harness: ChatHarness, model: ChatModel | undefined) =>
|
|
190
|
+
model !== undefined && chatModel(harness, model)?.effortless === true;
|
|
191
|
+
|
|
192
|
+
type InstallClaim = (signal: AbortSignal) => Promise<((ok: boolean) => void) | null>;
|
|
193
|
+
|
|
194
|
+
function installClaim(): InstallClaim {
|
|
195
|
+
let installed = false;
|
|
196
|
+
let installing: Promise<void> | null = null;
|
|
197
|
+
return async (signal) => {
|
|
198
|
+
const aborted = new Promise<void>((resolve) => {
|
|
199
|
+
if (signal.aborted) resolve();
|
|
200
|
+
signal.addEventListener("abort", () => resolve(), { once: true });
|
|
201
|
+
});
|
|
202
|
+
// oxlint-disable-next-line no-unmodified-loop-condition -- the running install changes both.
|
|
203
|
+
while (!installed && installing) {
|
|
204
|
+
// oxlint-disable-next-line no-await-in-loop -- waits for the install, not in parallel with it.
|
|
205
|
+
await Promise.race([installing, aborted]);
|
|
206
|
+
signal.throwIfAborted();
|
|
207
|
+
}
|
|
208
|
+
if (installed) return null;
|
|
209
|
+
let release!: () => void;
|
|
210
|
+
installing = new Promise<void>((resolve) => (release = resolve));
|
|
211
|
+
return (ok) => {
|
|
212
|
+
installed ||= ok;
|
|
213
|
+
installing = null;
|
|
214
|
+
release();
|
|
215
|
+
};
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
function defaultCredential(): () => ChatAuth {
|
|
220
|
+
let credentials: ReturnType<typeof createChatCredentials> | null = null;
|
|
221
|
+
return () => (credentials ??= createChatCredentials()).auth();
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
export function createHarnessChatAgent(options: HarnessChatAgentOptions = {}): ChatAgent {
|
|
225
|
+
const load = options.loadHarness ?? loadHarness;
|
|
226
|
+
const sandboxFor = options.createSandbox ?? createHostSandbox;
|
|
227
|
+
const contextFor = options.companionContext ?? readCompanionContext;
|
|
228
|
+
const readCredential = options.credential ?? defaultCredential();
|
|
229
|
+
const prebuiltBridges =
|
|
230
|
+
options.prebuiltBridges ?? (process.env.MATE_STUDIO_CHAT_BRIDGES || undefined);
|
|
231
|
+
const loaded = new Map<ChatHarness, Promise<HarnessModules>>();
|
|
232
|
+
const claims = new Map<ChatHarness, InstallClaim>();
|
|
233
|
+
const modulesFor = (harness: ChatHarness): Promise<HarnessModules> => {
|
|
234
|
+
let modules = loaded.get(harness);
|
|
235
|
+
if (!modules) {
|
|
236
|
+
modules = load(harness).catch((error: unknown) => {
|
|
237
|
+
loaded.delete(harness);
|
|
238
|
+
throw error;
|
|
239
|
+
});
|
|
240
|
+
loaded.set(harness, modules);
|
|
241
|
+
}
|
|
242
|
+
return modules;
|
|
243
|
+
};
|
|
244
|
+
const claimFor = (harness: ChatHarness): InstallClaim => {
|
|
245
|
+
let claim = claims.get(harness);
|
|
246
|
+
if (!claim) claims.set(harness, (claim = installClaim()));
|
|
247
|
+
return claim;
|
|
248
|
+
};
|
|
249
|
+
|
|
250
|
+
return {
|
|
251
|
+
turn({ companionPath, messages, signal, harness = "claude-code", model, effort }) {
|
|
252
|
+
let sandbox: HostSandbox | null = null;
|
|
253
|
+
let session: HarnessSession | null = null;
|
|
254
|
+
const cleanup = async () => {
|
|
255
|
+
const [startedSession, startedSandbox] = [session, sandbox];
|
|
256
|
+
session = null;
|
|
257
|
+
sandbox = null;
|
|
258
|
+
await startedSession?.stop().catch(() => {});
|
|
259
|
+
await Promise.resolve(startedSandbox?.stop()).catch(() => {});
|
|
260
|
+
};
|
|
261
|
+
let running: Promise<void> = Promise.resolve();
|
|
262
|
+
return new ReadableStream<UIMessageChunk>({
|
|
263
|
+
start: (controller) => {
|
|
264
|
+
running = answer(controller);
|
|
265
|
+
},
|
|
266
|
+
cancel: async () => {
|
|
267
|
+
await cleanup();
|
|
268
|
+
await running;
|
|
269
|
+
},
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
async function answer(controller: ReadableStreamDefaultController<UIMessageChunk>) {
|
|
273
|
+
const push = (chunk: UIMessageChunk) => {
|
|
274
|
+
try {
|
|
275
|
+
controller.enqueue(chunk);
|
|
276
|
+
} catch {}
|
|
277
|
+
};
|
|
278
|
+
let credential: AgentCredential | undefined;
|
|
279
|
+
const failure = (error: unknown) => chatFailure(error, harness, model, credential);
|
|
280
|
+
try {
|
|
281
|
+
const prompt = renderPrompt(messages);
|
|
282
|
+
if (prompt === null) throw new Error("the conversation must end with a question");
|
|
283
|
+
if (harness === "claude-code") {
|
|
284
|
+
const auth = readCredential();
|
|
285
|
+
if (auth === null) throw new Error(NO_CHAT_CREDENTIAL);
|
|
286
|
+
if (auth !== "login") credential = auth;
|
|
287
|
+
}
|
|
288
|
+
const [modules, context] = await Promise.all([
|
|
289
|
+
modulesFor(harness),
|
|
290
|
+
contextFor(companionPath),
|
|
291
|
+
]);
|
|
292
|
+
const release = await claimFor(harness)(signal);
|
|
293
|
+
let started = false;
|
|
294
|
+
let agent: HarnessAgentLike;
|
|
295
|
+
let created: HarnessSession;
|
|
296
|
+
try {
|
|
297
|
+
signal.throwIfAborted();
|
|
298
|
+
sandbox = await sandboxFor({
|
|
299
|
+
workDir: companionPath,
|
|
300
|
+
...(options.bootstrapCache ? { bootstrapCache: options.bootstrapCache } : {}),
|
|
301
|
+
...(prebuiltBridges ? { prebuiltBridges } : {}),
|
|
302
|
+
});
|
|
303
|
+
signal.throwIfAborted();
|
|
304
|
+
agent = modules.createAgent({
|
|
305
|
+
port: sandbox.port,
|
|
306
|
+
instructions: [context.instructions, CHAT_INSTRUCTIONS].filter(Boolean).join("\n\n"),
|
|
307
|
+
skills: context.skills,
|
|
308
|
+
...(model ? { model } : {}),
|
|
309
|
+
...(effort && !takesNoEffort(harness, model) ? { effort } : {}),
|
|
310
|
+
...(credential ? { credential } : {}),
|
|
311
|
+
});
|
|
312
|
+
created = await agent.createSession({ sandboxSession: sandbox, abortSignal: signal });
|
|
313
|
+
session = created;
|
|
314
|
+
started = true;
|
|
315
|
+
} finally {
|
|
316
|
+
release?.(started);
|
|
317
|
+
}
|
|
318
|
+
signal.throwIfAborted();
|
|
319
|
+
const result = await agent.stream({ session: created, prompt, abortSignal: signal });
|
|
320
|
+
for await (const chunk of result.toUIMessageStream({ onError: failure })) {
|
|
321
|
+
push(chunk);
|
|
322
|
+
}
|
|
323
|
+
} catch (error) {
|
|
324
|
+
if (!signal.aborted) push({ type: "error", errorText: failure(error) });
|
|
325
|
+
} finally {
|
|
326
|
+
await cleanup();
|
|
327
|
+
try {
|
|
328
|
+
controller.close();
|
|
329
|
+
} catch {}
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
},
|
|
333
|
+
};
|
|
334
|
+
}
|