@tencent-adp/portal-cli 0.0.0-stage → 0.0.2

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.
Files changed (48) hide show
  1. package/dist/acp/agent.d.ts +25 -0
  2. package/dist/acp/agent.js +195 -0
  3. package/dist/acp/history.d.ts +15 -0
  4. package/dist/acp/history.js +41 -0
  5. package/dist/acp/options.d.ts +9 -0
  6. package/dist/acp/options.js +45 -0
  7. package/dist/acp/prompt.d.ts +3 -0
  8. package/dist/acp/prompt.js +10 -0
  9. package/dist/acp/server.d.ts +2 -0
  10. package/dist/acp/server.js +96 -0
  11. package/dist/acp/session.d.ts +12 -0
  12. package/dist/acp/session.js +2 -0
  13. package/dist/acp/turn.d.ts +15 -0
  14. package/dist/acp/turn.js +158 -0
  15. package/dist/auth/loadAuth.d.ts +22 -0
  16. package/dist/auth/loadAuth.js +54 -0
  17. package/dist/auth/login.d.ts +28 -0
  18. package/dist/auth/login.js +339 -0
  19. package/dist/bin.d.ts +3 -0
  20. package/dist/bin.js +24 -0
  21. package/dist/cli/ask.d.ts +3 -0
  22. package/dist/cli/ask.js +30 -0
  23. package/dist/cli/chat.d.ts +5 -0
  24. package/dist/cli/chat.js +48 -0
  25. package/dist/cli/session.d.ts +2 -0
  26. package/dist/cli/session.js +6 -0
  27. package/dist/cli/sessions.d.ts +20 -0
  28. package/dist/cli/sessions.js +56 -0
  29. package/dist/portal/callerTools.d.ts +68 -0
  30. package/dist/portal/callerTools.js +50 -0
  31. package/dist/portal/chat.d.ts +23 -0
  32. package/dist/portal/chat.js +73 -0
  33. package/dist/portal/client.d.ts +29 -0
  34. package/dist/portal/client.js +98 -0
  35. package/dist/portal/headers.d.ts +3 -0
  36. package/dist/portal/headers.js +5 -0
  37. package/dist/portal/sse.d.ts +9 -0
  38. package/dist/portal/sse.js +219 -0
  39. package/dist/portal/sseLog.d.ts +4 -0
  40. package/dist/portal/sseLog.js +47 -0
  41. package/dist/portal/target.d.ts +40 -0
  42. package/dist/portal/target.js +72 -0
  43. package/dist/portal/types.d.ts +53 -0
  44. package/dist/portal/types.js +2 -0
  45. package/dist/tools/local.d.ts +3 -0
  46. package/dist/tools/local.js +122 -0
  47. package/package.json +36 -3
  48. package/README.md +0 -3
@@ -0,0 +1,72 @@
1
+ // Portal's canonical reasoning levels, ordered weakest to strongest. A model
2
+ // only exposes the subset reported by /runtime-models.
3
+ export const ALL_REASONING_EFFORTS = ['none', 'minimal', 'low', 'medium', 'high', 'xhigh'];
4
+ export async function loadWorkbench(client, spaceId) {
5
+ const agentId = process.env.PORTAL_AGENT_ID || await resolveEntryAgent(client, spaceId);
6
+ const body = await client.getJson(`/api/spaces/${encodeURIComponent(spaceId)}/agents/${encodeURIComponent(agentId)}/runtime-models`);
7
+ const models = body.models ?? [];
8
+ const defaultModel = models.find((model) => model.id === body.defaultModelId) ?? models[0];
9
+ if (!defaultModel)
10
+ throw new Error('Portal workbench agent has no runtime model');
11
+ return {
12
+ agentId,
13
+ models,
14
+ defaultModelId: defaultModel.id,
15
+ defaultReasoningEffort: body.defaultRuntimeConfig?.reasoningEffort ?? defaultModel.defaultReasoningEffort ?? 'none',
16
+ };
17
+ }
18
+ export async function resolveTarget(client, spaceId) {
19
+ const workbench = await loadWorkbench(client, spaceId);
20
+ const model = pickModel(workbench.models, workbench.defaultModelId, process.env.PORTAL_RUNTIME_MODEL);
21
+ if (!model)
22
+ throw new Error('Portal workbench agent has no runtime model');
23
+ return {
24
+ agentId: workbench.agentId,
25
+ modelId: model.id,
26
+ // Never send a level the selected model does not expose.
27
+ reasoningEffort: defaultReasoningEffort(model, process.env.PORTAL_REASONING_EFFORT || workbench.defaultReasoningEffort),
28
+ };
29
+ }
30
+ async function resolveEntryAgent(client, spaceId) {
31
+ const body = await client.getJson(`/api/spaces/${encodeURIComponent(spaceId)}/display/entry`);
32
+ return body.superAgentEntry.agentId;
33
+ }
34
+ /**
35
+ * Reasoning levels the given model actually accepts.
36
+ *
37
+ * The server resolves provider-native levels into these Portal levels, so the
38
+ * client must not invent its own list: a model that only supports low/medium/high
39
+ * would otherwise show minimal and xhigh and fail on selection. An older server
40
+ * omits the capabilities, in which case every level is offered as before.
41
+ */
42
+ export function reasoningEfforts(model) {
43
+ const supported = model?.supportedReasoningEfforts;
44
+ if (!supported)
45
+ return [...ALL_REASONING_EFFORTS];
46
+ const canDisable = model?.canDisableReasoning ?? true;
47
+ return ALL_REASONING_EFFORTS.filter((value) => (value === 'none' ? canDisable : supported.includes(value)));
48
+ }
49
+ export function isReasoningEffort(model, value) {
50
+ return reasoningEfforts(model).includes(value);
51
+ }
52
+ /** Session default: the server-configured level when that model still supports it. */
53
+ export function defaultReasoningEffort(model, configured = '') {
54
+ const efforts = reasoningEfforts(model);
55
+ for (const candidate of [configured, model?.defaultReasoningEffort ?? '', 'none']) {
56
+ if (candidate && efforts.includes(candidate))
57
+ return candidate;
58
+ }
59
+ return efforts[0] ?? 'none';
60
+ }
61
+ export function pickModel(models, defaultModelId, requested) {
62
+ const name = requested?.trim().toLowerCase();
63
+ if (!name)
64
+ return models.find((model) => model.id === defaultModelId);
65
+ return models.find((model) => modelNames(model).includes(name));
66
+ }
67
+ function modelNames(model) {
68
+ return [model.id, model.label, model.modelId, model.name, model.modelName]
69
+ .filter((value) => Boolean(value))
70
+ .map((value) => value.toLowerCase());
71
+ }
72
+ //# sourceMappingURL=target.js.map
@@ -0,0 +1,53 @@
1
+ export interface ToolCall {
2
+ callId: string;
3
+ name: string;
4
+ arguments: Record<string, unknown>;
5
+ }
6
+ export interface ThoughtBlock {
7
+ id: string;
8
+ title: string;
9
+ text: string;
10
+ }
11
+ export interface UserQuestionOption {
12
+ label: string;
13
+ description: string;
14
+ }
15
+ export interface UserQuestionItem {
16
+ question: string;
17
+ header: string;
18
+ options: UserQuestionOption[];
19
+ multiple?: boolean;
20
+ }
21
+ export interface UserQuestion {
22
+ interactionId: string;
23
+ version: number;
24
+ questions: UserQuestionItem[];
25
+ }
26
+ export type ChatContent = {
27
+ Type: 'text';
28
+ Text: string;
29
+ } | {
30
+ Type: 'function_call_output';
31
+ CallId: string;
32
+ Output: string;
33
+ };
34
+ export interface ChatTurn {
35
+ contents: ChatContent[];
36
+ previousResponseId: string;
37
+ }
38
+ export interface ContextUsage {
39
+ usedTokens: number;
40
+ limitTokens?: number;
41
+ }
42
+ export interface StreamSegment {
43
+ text: string;
44
+ thoughts: ThoughtBlock[];
45
+ calls: ToolCall[];
46
+ recordId: string;
47
+ completed: boolean;
48
+ errors: string[];
49
+ sessionTitle: string;
50
+ question?: UserQuestion;
51
+ contextUsage?: ContextUsage;
52
+ }
53
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1,3 @@
1
+ import type { ToolCall } from '../portal/types.js';
2
+ export declare function runLocalTool(cwd: string, call: ToolCall, timeoutMs?: number): Promise<string>;
3
+ //# sourceMappingURL=local.d.ts.map
@@ -0,0 +1,122 @@
1
+ import { spawn } from 'node:child_process';
2
+ import { readFileSync, writeFileSync } from 'node:fs';
3
+ import path from 'node:path';
4
+ const READ_LINE_LIMIT = 2000;
5
+ const EXEC_TIMEOUT_MS = 60_000;
6
+ export async function runLocalTool(cwd, call, timeoutMs = EXEC_TIMEOUT_MS) {
7
+ try {
8
+ if (call.name === 'exec')
9
+ return await exec(cwd, String(call.arguments.command ?? ''), timeoutMs);
10
+ if (call.name === 'read')
11
+ return read(cwd, call.arguments);
12
+ if (call.name === 'edit')
13
+ return edit(cwd, call.arguments);
14
+ return `Tool ${call.name} is not available in this environment.`;
15
+ }
16
+ catch (error) {
17
+ return error instanceof Error ? error.message : String(error);
18
+ }
19
+ }
20
+ function exec(cwd, command, timeoutMs) {
21
+ return new Promise((resolve) => {
22
+ const signal = AbortSignal.timeout(timeoutMs);
23
+ // detached so the shell is its own process group and a timeout can stop
24
+ // the command it launched, not only the shell.
25
+ const child = spawn(command, { cwd, shell: true, detached: true, signal });
26
+ let stdout = '';
27
+ let stderr = '';
28
+ let settled = false;
29
+ const finish = (text) => {
30
+ if (settled)
31
+ return;
32
+ settled = true;
33
+ resolve(text);
34
+ };
35
+ child.stdout.setEncoding('utf8');
36
+ child.stderr.setEncoding('utf8');
37
+ child.stdout.on('data', (chunk) => { stdout += chunk; });
38
+ child.stderr.on('data', (chunk) => { stderr += chunk; });
39
+ // A timeout aborts the signal and emits 'error'. Leaving that unhandled
40
+ // kills the ACP process, so the failure never goes back to the remote.
41
+ child.on('error', (error) => {
42
+ if (signal.aborted)
43
+ stopCommand(child);
44
+ finish(signal.aborted ? timeoutResult(stdout, stderr, timeoutMs) : error.message);
45
+ });
46
+ child.on('close', (code) => {
47
+ if (signal.aborted) {
48
+ finish(timeoutResult(stdout, stderr, timeoutMs));
49
+ return;
50
+ }
51
+ finish(code ? `${stdout}\n[exit ${code}]\n${stderr}`.trim() : stdout);
52
+ });
53
+ });
54
+ }
55
+ function timeoutResult(stdout, stderr, timeoutMs) {
56
+ const seconds = timeoutMs / 1000;
57
+ const notice = `command timed out after ${seconds}s`;
58
+ return [stdout.trimEnd(), stderr.trimEnd(), notice].filter((part) => part.length > 0).join('\n');
59
+ }
60
+ function stopCommand(child) {
61
+ if (!child.pid)
62
+ return;
63
+ try {
64
+ process.kill(-child.pid, 'SIGKILL');
65
+ }
66
+ catch (error) {
67
+ if (error.code !== 'ESRCH')
68
+ throw error;
69
+ }
70
+ }
71
+ function read(cwd, args) {
72
+ const filePath = String(args.file_path ?? args.path ?? '');
73
+ const lines = fileLines(resolvePath(cwd, filePath));
74
+ const offset = Number(args.offset ?? 1);
75
+ const limit = Number(args.limit ?? READ_LINE_LIMIT);
76
+ const window = lines.slice(offset - 1, offset - 1 + limit);
77
+ const end = window.length > 0 ? offset + window.length - 1 : offset - 1;
78
+ const numbered = window.map((line, index) => `${offset + index}|${line}`).join('\n');
79
+ return [
80
+ `file_path: ${filePath}`,
81
+ `offset: ${offset}`,
82
+ `shown: ${offset}-${end}`,
83
+ `total_lines: ${lines.length}`,
84
+ numbered,
85
+ ].join('\n').trimEnd();
86
+ }
87
+ function edit(cwd, args) {
88
+ const filePath = String(args.file_path ?? args.path ?? '');
89
+ const oldString = String(args.old_string ?? args.old ?? '');
90
+ const newString = String(args.new_string ?? args.new ?? '');
91
+ if (!oldString)
92
+ return 'old_string must be a non-empty string';
93
+ const target = resolvePath(cwd, filePath);
94
+ const text = readFileSync(target, 'utf8');
95
+ const count = text.split(oldString).length - 1;
96
+ if (count === 0)
97
+ return 'old_string not found';
98
+ if (args.replace_all !== true && count !== 1) {
99
+ return `old_string must appear exactly once, found ${count}`;
100
+ }
101
+ const updated = args.replace_all === true
102
+ ? text.split(oldString).join(newString)
103
+ : replaceOnce(text, oldString, newString);
104
+ writeFileSync(target, updated);
105
+ return args.replace_all === true
106
+ ? `The file ${filePath} has been updated. All occurrences were successfully replaced.`
107
+ : `The file ${filePath} has been updated successfully.`;
108
+ }
109
+ function replaceOnce(text, oldString, newString) {
110
+ const index = text.indexOf(oldString);
111
+ return text.slice(0, index) + newString + text.slice(index + oldString.length);
112
+ }
113
+ function fileLines(target) {
114
+ const lines = readFileSync(target, 'utf8').split(/\r?\n/);
115
+ if (lines.at(-1) === '')
116
+ lines.pop();
117
+ return lines;
118
+ }
119
+ function resolvePath(cwd, filePath) {
120
+ return path.isAbsolute(filePath) ? filePath : path.join(cwd, filePath);
121
+ }
122
+ //# sourceMappingURL=local.js.map
package/package.json CHANGED
@@ -1,6 +1,39 @@
1
1
  {
2
2
  "name": "@tencent-adp/portal-cli",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.0.2",
4
+ "description": "Talk to a Portal agent and let it use this machine",
5
+ "type": "module",
6
+ "bin": {
7
+ "portal": "./dist/bin.js"
8
+ },
9
+ "files": [
10
+ "dist",
11
+ "!dist/**/*.map"
12
+ ],
13
+ "publishConfig": {
14
+ "access": "public",
15
+ "registry": "https://registry.npmjs.org/"
16
+ },
17
+ "engines": {
18
+ "node": ">=24.0.0"
19
+ },
20
+ "devDependencies": {
21
+ "@types/node": "^25.7.0",
22
+ "eslint": "^9.39.4",
23
+ "tsx": "^4.21.0",
24
+ "typescript": "~5.9.3",
25
+ "@agent-portal/eslint-config": "2.0.1"
26
+ },
27
+ "dependencies": {
28
+ "@agentclientprotocol/sdk": "2.6.0"
29
+ },
30
+ "scripts": {
31
+ "build": "tsc",
32
+ "dev": "tsx src/bin.ts",
33
+ "typecheck": "tsc --noEmit",
34
+ "test": "tsx --test --test-concurrency=1 --test-force-exit src/**/*.test.ts",
35
+ "clean": "rm -rf dist",
36
+ "lint": "eslint .",
37
+ "lint:fix": "eslint . --fix"
38
+ }
6
39
  }
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.