@stevezhou/sisu 0.2.2 → 0.3.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/dist/commands.js CHANGED
@@ -339,7 +339,8 @@ async function setTrainingCommand(optIn, http = http_1.defaultHttp) {
339
339
  }
340
340
  async function listModelsCommand(http = http_1.defaultHttp) {
341
341
  const { models, defaultModel } = await (0, models_1.fetchModelCatalog)(http);
342
- const current = (0, store_1.readSession)().last_model || defaultModel;
342
+ const last = (0, store_1.readSession)().last_model || '';
343
+ const current = models.some((row) => row.name === last) ? last : defaultModel;
343
344
  if (!current && !models.length)
344
345
  return 'no models available';
345
346
  const lines = models.map((row) => {
@@ -347,9 +348,6 @@ async function listModelsCommand(http = http_1.defaultHttp) {
347
348
  const extra = row.label !== row.name ? ` ${row.label}` : '';
348
349
  return `${mark}${row.name}${extra}`;
349
350
  });
350
- if (current && !models.some((row) => row.name === current)) {
351
- lines.unshift(`* ${current}`);
352
- }
353
351
  return lines.join('\n') || `* ${current}`;
354
352
  }
355
353
  async function setModelCommand(query, http = http_1.defaultHttp) {
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.toProviderMessages = toProviderMessages;
4
4
  exports.completeUrl = completeUrl;
5
5
  exports.openaiCompatUrl = openaiCompatUrl;
6
+ exports.completeHeaders = completeHeaders;
6
7
  exports.buildCompleteRequest = buildCompleteRequest;
7
8
  exports.isServerSideAgentPayload = isServerSideAgentPayload;
8
9
  exports.parseCompleteSse = parseCompleteSse;
@@ -11,6 +12,7 @@ exports.createSisuCloudModel = createSisuCloudModel;
11
12
  const client_1 = require("../client");
12
13
  const http_1 = require("../http");
13
14
  const sse_1 = require("../sse");
15
+ const store_1 = require("../store");
14
16
  const suite_1 = require("./suite");
15
17
  /** OpenAI/Poe wire: assistant.tool_calls is {id,type,function:{name,arguments:string}}. */
16
18
  function toProviderMessages(messages) {
@@ -39,6 +41,12 @@ function completeUrl(apiBase) {
39
41
  function openaiCompatUrl(apiBase) {
40
42
  return `${apiBase.replace(/\/+$/, '')}${suite_1.OPENAI_COMPAT_PATH}`;
41
43
  }
44
+ function completeHeaders(token) {
45
+ return {
46
+ ...(0, http_1.authHeaders)(token),
47
+ 'x-sisu-conversation-id': (0, store_1.ensureConversationId)(),
48
+ };
49
+ }
42
50
  function buildCompleteRequest(request, options = {}) {
43
51
  const stamp = (0, client_1.clientStamp)(options.client || 'cli');
44
52
  return {
@@ -135,7 +143,7 @@ function createSisuCloudModel(http, options) {
135
143
  }
136
144
  const sent = await http(completeUrl(options.apiBase), {
137
145
  method: 'POST',
138
- headers: (0, http_1.authHeaders)(options.token),
146
+ headers: completeHeaders(options.token),
139
147
  body: JSON.stringify(payload),
140
148
  });
141
149
  if (!sent.ok) {
@@ -61,17 +61,22 @@ function sisuGrokBuildEnv() {
61
61
  const auth = (0, store_1.readAuth)();
62
62
  const home = (0, store_1.getSisuHome)();
63
63
  const runtimeBase = auth ? sisuRuntimeApiBase(auth.api_base) : '';
64
+ const env = { ...process.env };
65
+ delete env.GROK_DEFAULT_MODEL;
64
66
  return {
65
- ...process.env,
67
+ ...env,
66
68
  GROK_HOME: process.env.GROK_HOME || home,
67
69
  SISU_HOME: home,
68
70
  GROK_TELEMETRY_ENABLED: process.env.GROK_TELEMETRY_ENABLED || '0',
69
71
  XAI_API_KEY: process.env.XAI_API_KEY || auth?.token || '',
70
72
  SISU_API_BASE: auth?.api_base || process.env.SISU_API_BASE || 'https://www.sisu.chat',
73
+ SISU_CONVERSATION_ID: (0, store_1.ensureConversationId)(),
71
74
  ...(runtimeBase
72
75
  ? {
73
76
  GROK_XAI_API_BASE_URL: runtimeBase,
74
77
  XAI_API_BASE_URL: runtimeBase,
78
+ GROK_MODELS_BASE_URL: runtimeBase,
79
+ GROK_MODELS_LIST_URL: `${runtimeBase}/models`,
75
80
  }
76
81
  : {}),
77
82
  };
@@ -8,23 +8,27 @@ const store_1 = require("../store");
8
8
  function normalizeModelKey(value) {
9
9
  return value.toLowerCase().replace(/[-_.\s]/g, '');
10
10
  }
11
- async function fetchModelCatalog(http = http_1.defaultHttp) {
12
- const auth = (0, store_1.requireAuth)();
13
- const response = await http(`${auth.api_base}/api/chat/models`, { headers: (0, http_1.authHeaders)(auth.token) });
14
- const body = await response.json().catch(() => ({}));
15
- if (!response.ok)
16
- throw new Error((0, http_1.errorDetail)(body, `models failed (${response.status})`));
17
- const rows = Array.isArray(body?.models) ? body.models : [];
11
+ function parseRuntimeCatalog(body) {
12
+ const rows = Array.isArray(body?.data) ? body.data : [];
18
13
  const models = rows
19
14
  .map((row) => {
20
- const name = String(row?.name || '').trim();
15
+ const name = String(row?.id || row?.name || '').trim();
21
16
  if (!name)
22
17
  return null;
23
- return { name, label: String(row.display_name || row.label || name) };
18
+ return { name, label: String(row?.name || row?.id || name).trim() || name };
24
19
  })
25
20
  .filter((row) => Boolean(row));
26
21
  return { models, defaultModel: String(body?.default_model || '') };
27
22
  }
23
+ async function fetchModelCatalog(http = http_1.defaultHttp) {
24
+ const auth = (0, store_1.requireAuth)();
25
+ const headers = (0, http_1.authHeaders)(auth.token);
26
+ const runtime = await http(`${auth.api_base}/api/runtime/v1/models`, { headers });
27
+ if (runtime.ok)
28
+ return parseRuntimeCatalog(await runtime.json().catch(() => ({})));
29
+ const body = await runtime.json().catch(() => ({}));
30
+ throw new Error((0, http_1.errorDetail)(body, `models failed (${runtime.status})`));
31
+ }
28
32
  function resolveCatalogModel(query, models) {
29
33
  const needle = normalizeModelKey(query);
30
34
  if (!needle)
package/dist/store.js CHANGED
@@ -9,11 +9,13 @@ exports.readAuth = readAuth;
9
9
  exports.writeAuth = writeAuth;
10
10
  exports.readSession = readSession;
11
11
  exports.writeSession = writeSession;
12
+ exports.ensureConversationId = ensureConversationId;
12
13
  exports.clearAuth = clearAuth;
13
14
  exports.readWorkspaces = readWorkspaces;
14
15
  exports.bindWorkspace = bindWorkspace;
15
16
  exports.requireAuth = requireAuth;
16
17
  exports.describeStatus = describeStatus;
18
+ const crypto_1 = require("crypto");
17
19
  const fs_1 = __importDefault(require("fs"));
18
20
  const os_1 = __importDefault(require("os"));
19
21
  const path_1 = __importDefault(require("path"));
@@ -79,6 +81,7 @@ function readAuth() {
79
81
  };
80
82
  }
81
83
  function writeAuth(record) {
84
+ const previous = readAuth();
82
85
  writeJson(authPath(), {
83
86
  token: record.token,
84
87
  email: record.email,
@@ -87,6 +90,12 @@ function writeAuth(record) {
87
90
  plan_code: record.plan_code || '',
88
91
  name: record.name || '',
89
92
  });
93
+ if (previous && previous.user_id !== record.user_id) {
94
+ const session = readSession();
95
+ if (session.last_conversation_id) {
96
+ writeSession({ ...session, last_conversation_id: undefined });
97
+ }
98
+ }
90
99
  }
91
100
  function readSession() {
92
101
  return readJson(sessionPath(), {});
@@ -94,6 +103,16 @@ function readSession() {
94
103
  function writeSession(record) {
95
104
  writeJson(sessionPath(), record);
96
105
  }
106
+ const UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
107
+ function ensureConversationId() {
108
+ const session = readSession();
109
+ const current = String(session.last_conversation_id || '').trim();
110
+ if (UUID_RE.test(current))
111
+ return current;
112
+ const id = (0, crypto_1.randomUUID)();
113
+ writeSession({ ...session, last_conversation_id: id });
114
+ return id;
115
+ }
97
116
  function clearAuth() {
98
117
  try {
99
118
  fs_1.default.unlinkSync(authPath());
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stevezhou/sisu",
3
- "version": "0.2.2",
3
+ "version": "0.3.0",
4
4
  "description": "SiSu CLI — 思溯 / SiSu · 思有所溯",
5
5
  "license": "UNLICENSED",
6
6
  "homepage": "https://www.sisu.chat",