@insmind/cli 2.0.0 → 2.1.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/README.md CHANGED
@@ -7,8 +7,13 @@ npm install --global --allow-scripts=@insmind/cli @insmind/cli
7
7
  insmind-cli auth login
8
8
  insmind-cli --help
9
9
  insmind-cli editor apply --schema
10
+ insmind-cli credits --json
11
+ insmind-cli credits usage list --help
12
+ insmind-cli credits usage get --schema
10
13
  ```
11
14
 
12
15
  Requires Node.js 20 or newer. Credentials and local runtime state are stored separately from GaoDing CLI under `~/.insmind`.
13
16
 
17
+ `credits` reads the current balance; `credits usage list` returns one page of consumption, restoration, recycling and expiry records. `credits usage get <record-id>` reads source breakdown. Each supports offline `--schema` discovery without login. Records default to the current UTC month. Continue using the returned range and `nextCursor`; a page is not a complete spending total. Member period limits are shown separately from the overview balance.
18
+
14
19
  Discover command behavior with `--help`, structured input through supported `--schema` modes, and live model parameters through `model list` and `model get`. `editor apply --schema` reads the installed Action contract without login or an Editor connection; it cannot be combined with execution via `--input`.
@@ -0,0 +1,8 @@
1
+ {
2
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
3
+ "$id": "https://schemas.insmind.com/operations/credits.get/input.schema.json",
4
+ "title": "CreditsGetInput",
5
+ "type": "object",
6
+ "additionalProperties": false,
7
+ "properties": {}
8
+ }
@@ -0,0 +1,92 @@
1
+ {
2
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
3
+ "$id": "https://schemas.insmind.com/operations/credits.get/output.schema.json",
4
+ "title": "CreditsResult",
5
+ "type": "object",
6
+ "additionalProperties": false,
7
+ "required": [
8
+ "credits"
9
+ ],
10
+ "properties": {
11
+ "credits": {
12
+ "type": "object",
13
+ "additionalProperties": false,
14
+ "required": [
15
+ "unit",
16
+ "total",
17
+ "used",
18
+ "pending",
19
+ "available"
20
+ ],
21
+ "properties": {
22
+ "unit": {
23
+ "type": "string",
24
+ "const": "credit"
25
+ },
26
+ "total": {
27
+ "type": "number",
28
+ "minimum": 0,
29
+ "maximum": 9007199254740991
30
+ },
31
+ "used": {
32
+ "type": "number",
33
+ "minimum": 0,
34
+ "maximum": 9007199254740991
35
+ },
36
+ "pending": {
37
+ "type": "number",
38
+ "minimum": 0,
39
+ "maximum": 9007199254740991
40
+ },
41
+ "available": {
42
+ "type": "number",
43
+ "minimum": 0,
44
+ "maximum": 9007199254740991
45
+ }
46
+ }
47
+ },
48
+ "balances": {
49
+ "type": "array",
50
+ "items": {
51
+ "type": "object",
52
+ "additionalProperties": false,
53
+ "required": [
54
+ "total",
55
+ "used",
56
+ "pending",
57
+ "available"
58
+ ],
59
+ "properties": {
60
+ "identifier": {
61
+ "type": "string"
62
+ },
63
+ "total": {
64
+ "type": "number",
65
+ "minimum": 0,
66
+ "maximum": 9007199254740991
67
+ },
68
+ "used": {
69
+ "type": "number",
70
+ "minimum": 0,
71
+ "maximum": 9007199254740991
72
+ },
73
+ "pending": {
74
+ "type": "number",
75
+ "minimum": 0,
76
+ "maximum": 9007199254740991
77
+ },
78
+ "available": {
79
+ "type": "number",
80
+ "minimum": 0,
81
+ "maximum": 9007199254740991
82
+ },
83
+ "memberPeriodLimit": {
84
+ "type": "number",
85
+ "minimum": 0,
86
+ "maximum": 9007199254740991
87
+ }
88
+ }
89
+ }
90
+ }
91
+ }
92
+ }
@@ -0,0 +1,17 @@
1
+ {
2
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
3
+ "$id": "https://schemas.insmind.com/operations/credits.usage.get/input.schema.json",
4
+ "title": "CreditsUsageGetInput",
5
+ "type": "object",
6
+ "additionalProperties": false,
7
+ "required": [
8
+ "id"
9
+ ],
10
+ "properties": {
11
+ "id": {
12
+ "type": "string",
13
+ "minLength": 1,
14
+ "pattern": ".*\\S.*"
15
+ }
16
+ }
17
+ }
@@ -0,0 +1,46 @@
1
+ {
2
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
3
+ "$id": "https://schemas.insmind.com/operations/credits.usage.get/output.schema.json",
4
+ "title": "CreditsUsageGetResult",
5
+ "type": "object",
6
+ "additionalProperties": false,
7
+ "required": [
8
+ "id",
9
+ "unit",
10
+ "breakdown"
11
+ ],
12
+ "properties": {
13
+ "id": {
14
+ "type": "string",
15
+ "minLength": 1,
16
+ "pattern": ".*\\S.*"
17
+ },
18
+ "unit": {
19
+ "type": "string",
20
+ "const": "credit"
21
+ },
22
+ "breakdown": {
23
+ "type": "array",
24
+ "items": {
25
+ "type": "object",
26
+ "additionalProperties": false,
27
+ "required": [
28
+ "amount"
29
+ ],
30
+ "properties": {
31
+ "amount": {
32
+ "type": "number",
33
+ "minimum": 0,
34
+ "maximum": 9007199254740991
35
+ },
36
+ "usageType": {
37
+ "type": "number"
38
+ },
39
+ "sourceType": {
40
+ "type": "string"
41
+ }
42
+ }
43
+ }
44
+ }
45
+ }
46
+ }
@@ -0,0 +1,27 @@
1
+ {
2
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
3
+ "$id": "https://schemas.insmind.com/operations/credits.usage.list/input.schema.json",
4
+ "title": "CreditsUsageListInput",
5
+ "type": "object",
6
+ "additionalProperties": false,
7
+ "properties": {
8
+ "from": {
9
+ "type": "string",
10
+ "format": "date-time"
11
+ },
12
+ "to": {
13
+ "type": "string",
14
+ "format": "date-time"
15
+ },
16
+ "limit": {
17
+ "type": "integer",
18
+ "minimum": 1,
19
+ "default": 20
20
+ },
21
+ "cursor": {
22
+ "type": "string",
23
+ "minLength": 1,
24
+ "pattern": ".*\\S.*"
25
+ }
26
+ }
27
+ }
@@ -0,0 +1,135 @@
1
+ {
2
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
3
+ "$id": "https://schemas.insmind.com/operations/credits.usage.list/output.schema.json",
4
+ "title": "CreditsUsageListResult",
5
+ "type": "object",
6
+ "additionalProperties": false,
7
+ "required": [
8
+ "unit",
9
+ "range",
10
+ "items",
11
+ "hasMore",
12
+ "nextCursor"
13
+ ],
14
+ "properties": {
15
+ "unit": {
16
+ "type": "string",
17
+ "const": "credit"
18
+ },
19
+ "range": {
20
+ "type": "object",
21
+ "additionalProperties": false,
22
+ "required": [
23
+ "from",
24
+ "to"
25
+ ],
26
+ "properties": {
27
+ "from": {
28
+ "type": "string",
29
+ "format": "date-time"
30
+ },
31
+ "to": {
32
+ "type": "string",
33
+ "format": "date-time"
34
+ }
35
+ }
36
+ },
37
+ "items": {
38
+ "type": "array",
39
+ "items": {
40
+ "type": "object",
41
+ "additionalProperties": false,
42
+ "required": [
43
+ "id",
44
+ "changeType",
45
+ "amount",
46
+ "recordedAt"
47
+ ],
48
+ "properties": {
49
+ "id": {
50
+ "type": "string",
51
+ "minLength": 1,
52
+ "pattern": ".*\\S.*"
53
+ },
54
+ "changeType": {
55
+ "type": "string",
56
+ "minLength": 1,
57
+ "pattern": ".*\\S.*"
58
+ },
59
+ "amount": {
60
+ "type": "number",
61
+ "minimum": 0,
62
+ "maximum": 9007199254740991
63
+ },
64
+ "recordedAt": {
65
+ "type": "string",
66
+ "format": "date-time"
67
+ },
68
+ "recordMode": {
69
+ "type": "string"
70
+ },
71
+ "description": {
72
+ "type": "string"
73
+ },
74
+ "scene": {
75
+ "type": "number"
76
+ },
77
+ "referenceId": {
78
+ "type": "string"
79
+ },
80
+ "user": {
81
+ "type": "object",
82
+ "additionalProperties": false,
83
+ "properties": {
84
+ "id": {
85
+ "type": "string"
86
+ },
87
+ "name": {
88
+ "type": "string"
89
+ }
90
+ }
91
+ }
92
+ }
93
+ }
94
+ },
95
+ "hasMore": {
96
+ "type": "boolean"
97
+ },
98
+ "nextCursor": {
99
+ "type": [
100
+ "string",
101
+ "null"
102
+ ]
103
+ }
104
+ },
105
+ "allOf": [
106
+ {
107
+ "if": {
108
+ "properties": {
109
+ "hasMore": {
110
+ "const": true
111
+ }
112
+ },
113
+ "required": [
114
+ "hasMore"
115
+ ]
116
+ },
117
+ "then": {
118
+ "properties": {
119
+ "nextCursor": {
120
+ "type": "string",
121
+ "minLength": 1,
122
+ "pattern": ".*\\S.*"
123
+ }
124
+ }
125
+ },
126
+ "else": {
127
+ "properties": {
128
+ "nextCursor": {
129
+ "type": "null"
130
+ }
131
+ }
132
+ }
133
+ }
134
+ ]
135
+ }
@@ -35,22 +35,19 @@ export function createCli(runtime, options) {
35
35
  writeErr: () => undefined
36
36
  });
37
37
  program.addHelpText("after", runtime.product.id === "insmind"
38
- ? "\nstdout contains command results; stderr contains progress, warnings and errors. Fixed JSON commands, --json and --json-errors use {error:{code,message,details?,trace_id?}} for final errors. stderr may contain multiple messages.\nExit codes: 0 success, 1 execution failure, 2 invalid input, 130 local interruption; dispatched work may still be active.\n"
39
- : "\nstdout 输出命令结果;stderr 输出进度、警告和错误。固定 JSON 命令、--json 和 --json-errors 的最终错误使用 {error:{code,message,details?,trace_id?}},stderr 可能包含多条信息。\n退出码:0 成功,1 执行失败,2 输入无效,130 本地中断;已派发的工作可能仍在继续。\n");
38
+ ? "\nstdout contains command results; human-readable views can include an update notice. stderr contains progress, warnings and errors. Fixed JSON commands, --json and --json-errors use {error:{code,message,details?,trace_id?}} for final errors. stderr may contain multiple messages.\nExit codes: 0 success, 1 execution failure, 2 invalid input, 130 local interruption; dispatched work may still be active.\n"
39
+ : "\nstdout 输出命令结果,人类可读视图可以附带升级提醒;stderr 输出进度、警告和错误。固定 JSON 命令、--json 和 --json-errors 的最终错误使用 {error:{code,message,details?,trace_id?}},stderr 可能包含多条信息。\n退出码:0 成功,1 执行失败,2 输入无效,130 本地中断;已派发的工作可能仍在继续。\n");
40
40
  if (runtime.product.id === "insmind") {
41
41
  program.option("--locale <locale>", "locale for service-owned business descriptions");
42
42
  }
43
43
  configureActionEnvironment(program, {
44
44
  policies: runtime.accessPolicies,
45
45
  signal: options.signal,
46
- telemetry: runtime.telemetry,
47
- updateNotification: runtime.updateNotification
46
+ telemetry: runtime.telemetry
48
47
  });
49
48
  registerAuthCommands(program, runtime);
50
49
  if (runtime.product.exposesOrganizations)
51
50
  registerOrgCommands(program, runtime);
52
- else
53
- registerCreditsCommand(program, runtime);
54
51
  registerAgentCommands(program, runtime);
55
52
  registerToolCommands(program, runtime);
56
53
  registerModelCommands(program, runtime);
@@ -58,6 +55,7 @@ export function createCli(runtime, options) {
58
55
  registerDamCommands(program, runtime);
59
56
  registerUpdateCommand(program, runtime);
60
57
  registerTaskCommands(program, runtime);
58
+ registerCreditsCommand(program, runtime);
61
59
  assertAllLeafCommandsBound(program);
62
60
  outputs.set(program, runtime);
63
61
  if (runtime.analytics)
@@ -65,7 +63,11 @@ export function createCli(runtime, options) {
65
63
  return program;
66
64
  }
67
65
  export async function executeCli(options) {
68
- await options.telemetry.issue(invocationDefinition(options.program, options.argv));
66
+ const definition = invocationDefinition(options.program, options.argv);
67
+ await options.telemetry.issue(definition);
68
+ const automaticNotice = definition.operation !== "update" && !definition.operation.startsWith("update.");
69
+ let informational = false;
70
+ let help = false;
69
71
  try {
70
72
  try {
71
73
  await options.program.parseAsync(options.argv, { from: "user" });
@@ -73,12 +75,16 @@ export async function executeCli(options) {
73
75
  catch (error) {
74
76
  if (!isHelpOutcome(error))
75
77
  throw error;
76
- if (options.argv[0] !== "update")
77
- await checkUpdateNotification(options.updateNotification, options.signal);
78
+ informational = true;
79
+ help = error instanceof CommanderError && error.code === "commander.helpDisplayed";
78
80
  options.signal.throwIfAborted();
79
81
  }
80
82
  await options.telemetry.stage("output", async () => { await outputs.get(options.program)?.io.flush?.(); });
81
83
  await outputs.get(options.program)?.tasks.outputCompleted();
84
+ if (automaticNotice)
85
+ await checkUpdateNotification(options.updateNotification, options.signal, help || options.presenter.humanResult ? "text" : "machine");
86
+ if (informational)
87
+ options.signal.throwIfAborted();
82
88
  await sendFinished(options, { outcome: "success" });
83
89
  return 0;
84
90
  }
@@ -129,6 +135,9 @@ export async function executeCli(options) {
129
135
  }
130
136
  if (errorDelivered)
131
137
  await outputs.get(options.program)?.tasks.outputCompleted(error);
138
+ if (errorDelivered && automaticNotice && outcome.exitCode === 1 && definition.operation !== "unknown") {
139
+ await checkUpdateNotification(options.updateNotification, options.signal, "machine");
140
+ }
132
141
  await sendFinished(options, outcome.kind === "interrupted"
133
142
  ? { outcome: "interrupted", error }
134
143
  : {
@@ -149,9 +158,9 @@ export async function executeCli(options) {
149
158
  return outcome.exitCode;
150
159
  }
151
160
  }
152
- async function checkUpdateNotification(updateNotification, signal) {
161
+ async function checkUpdateNotification(updateNotification, signal, output) {
153
162
  try {
154
- await updateNotification.check(signal);
163
+ await updateNotification.check(signal, output);
155
164
  }
156
165
  catch {
157
166
  // Version availability must never change the requested command outcome.
@@ -1,5 +1,6 @@
1
1
  import { createWindAnalytics } from "../analytics/wind.js";
2
2
  import { taskScope } from "../features/task/store.js";
3
+ import { noticeWriter, sameTerminal } from "../features/update/notice-output.js";
3
4
  import { createTextOutput } from "../platform/text-output.js";
4
5
  import { TaskNotFoundError } from "../features/task/use-cases.js";
5
6
  import { homedir } from "node:os";
@@ -213,7 +214,8 @@ export function createProductionRuntime(options = {}) {
213
214
  });
214
215
  const credits = createCreditsUseCases({
215
216
  transport: creditsTransport,
216
- billingUnit: product.billingUnit
217
+ billingUnit: product.billingUnit,
218
+ now
217
219
  });
218
220
  const storageUpload = createDamStorageUploader({
219
221
  transport: damTransport,
@@ -504,7 +506,8 @@ export function createProductionRuntime(options = {}) {
504
506
  installationOwner,
505
507
  ...(options.homeDirectory ? { homeDirectory: options.homeDirectory } : {}),
506
508
  ...(options.env ? { env: options.env } : {}),
507
- notify: (message) => { presenter.notice(message); },
509
+ diagnosticOnly: !io.stderrTerminal && env.CLI_UPDATE_NOTICE_FORMAT !== "offer",
510
+ notify: noticeWriter(stdout, stderr, sameTerminal(rawIo.stdout, rawIo.stderr), env.CLI_UPDATE_NOTICE_FORMAT === "offer", text("提示: ", "Notice: ")),
508
511
  ...(product.id === "insmind" ? {
509
512
  stateDirectoryName: product.stateDirectory,
510
513
  registryUrl: `https://registry.npmjs.org/${encodeURIComponent(product.npmPackage)}`,
@@ -123,6 +123,9 @@ export function createContractValidators(options = {}) {
123
123
  const taskResult = ajv.compile(runOutputSchema);
124
124
  const taskListResult = ajv.compile(contractSchema("task.list", "output"));
125
125
  return {
126
+ creditsOutput: assertion(ajv.compile(contractSchema("credits.get", "output")), OutputContractError),
127
+ creditsUsageListOutput: assertion(ajv.compile(contractSchema("credits.usage.list", "output")), OutputContractError),
128
+ creditsUsageGetOutput: assertion(ajv.compile(contractSchema("credits.usage.get", "output")), OutputContractError),
126
129
  taskResult: assertion(taskResult, OutputContractError),
127
130
  taskListResult: assertion(taskListResult, OutputContractError),
128
131
  agentSendInput: assertion(agentSendInput, InputContractError),
@@ -3,6 +3,7 @@ const environments = new WeakMap();
3
3
  const boundPolicies = new WeakMap();
4
4
  const jsonCommands = new WeakSet();
5
5
  const selectedCommands = new WeakMap();
6
+ const commandDefinitions = new WeakMap();
6
7
  /** Declare the existing fixed JSON contract alongside the command registration. */
7
8
  export function jsonOutput(command) {
8
9
  jsonCommands.add(command);
@@ -15,7 +16,7 @@ export function errorUsesJson(program, argv) {
15
16
  return true;
16
17
  const command = selectedCommands.get(program);
17
18
  if (command)
18
- return jsonCommands.has(command) || command.opts().json === true;
19
+ return jsonCommands.has(command) || command.optsWithGlobals().json === true;
19
20
  return jsonErrorsRequested(argv);
20
21
  }
21
22
  export function configureActionEnvironment(program, environment) {
@@ -30,7 +31,6 @@ export function bindAction(command, policy, handler, telemetry) {
30
31
  const environment = environments.get(rootCommand(command));
31
32
  if (!environment)
32
33
  throw new Error("CLI action environment is missing.");
33
- await checkUpdateNotification(environment, telemetry);
34
34
  const context = await environment.telemetry.stage("access", async () => {
35
35
  const authorized = await environment.policies[policy].authorize(environment.signal);
36
36
  if (authorized.policy !== policy) {
@@ -51,7 +51,6 @@ export function bindPreparedAction(command, policy, prepare, run, telemetry) {
51
51
  const environment = environments.get(rootCommand(command));
52
52
  if (!environment)
53
53
  throw new Error("CLI action environment is missing.");
54
- await checkUpdateNotification(environment, telemetry);
55
54
  const prepared = await environment.telemetry.stage("input", () => prepare(environment.signal));
56
55
  if (prepared.handled)
57
56
  return;
@@ -66,16 +65,6 @@ export function bindPreparedAction(command, policy, prepare, run, telemetry) {
66
65
  await run(context, prepared.input, environment.signal);
67
66
  });
68
67
  }
69
- async function checkUpdateNotification(environment, telemetry) {
70
- if (telemetry?.operation === "update")
71
- return;
72
- try {
73
- await environment.updateNotification.check(environment.signal);
74
- }
75
- catch {
76
- // Version availability must never change the requested command outcome.
77
- }
78
- }
79
68
  export function assertAllLeafCommandsBound(program) {
80
69
  for (const command of leafCommands(program)) {
81
70
  if (!boundPolicies.has(command)) {
@@ -96,6 +85,7 @@ function rootCommand(command) {
96
85
  }
97
86
  function bindTelemetrySelection(command, definition) {
98
87
  definition ??= { operation: commandPath(command).split(" ").slice(1).join(".") };
88
+ commandDefinitions.set(command, definition);
99
89
  if (!command.parent)
100
90
  throw new Error(`Telemetry command has no parent: ${command.name()}`);
101
91
  command.parent.hook("preSubcommand", (parent, selected) => {
@@ -152,7 +142,7 @@ export function invocationDefinition(program, argv) {
152
142
  }
153
143
  }
154
144
  const path = commandPath(command).split(" ").slice(1).join(".");
155
- const operation = mode === "version" ? "version" : unknown ? "unknown" : path || (mode === "help" ? "help" : "unknown");
145
+ const operation = mode === "version" ? "version" : unknown ? "unknown" : commandDefinitions.get(command)?.operation ?? (path || (mode === "help" ? "help" : "unknown"));
156
146
  return { operation, mode: () => mode };
157
147
  }
158
148
  function commandPath(command) {